typhoon: repair firmware loading
[linux/fpc-iii.git] / security / smack / smack_lsm.c
blob0278bc08304440df4af600670312e7bd3bce764c
1 /*
2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
6 * Author:
7 * Casey Schaufler <casey@schaufler-ca.com>
9 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
16 #include <linux/xattr.h>
17 #include <linux/pagemap.h>
18 #include <linux/mount.h>
19 #include <linux/stat.h>
20 #include <linux/ext2_fs.h>
21 #include <linux/kd.h>
22 #include <asm/ioctls.h>
23 #include <linux/tcp.h>
24 #include <linux/udp.h>
25 #include <linux/mutex.h>
26 #include <linux/pipe_fs_i.h>
27 #include <net/netlabel.h>
28 #include <net/cipso_ipv4.h>
29 #include <linux/audit.h>
31 #include "smack.h"
33 #define task_security(task) (task_cred_xxx((task), security))
36 * I hope these are the hokeyist lines of code in the module. Casey.
38 #define DEVPTS_SUPER_MAGIC 0x1cd1
39 #define SOCKFS_MAGIC 0x534F434B
40 #define TMPFS_MAGIC 0x01021994
42 /**
43 * smk_fetch - Fetch the smack label from a file.
44 * @ip: a pointer to the inode
45 * @dp: a pointer to the dentry
47 * Returns a pointer to the master list entry for the Smack label
48 * or NULL if there was no label to fetch.
50 static char *smk_fetch(struct inode *ip, struct dentry *dp)
52 int rc;
53 char in[SMK_LABELLEN];
55 if (ip->i_op->getxattr == NULL)
56 return NULL;
58 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
59 if (rc < 0)
60 return NULL;
62 return smk_import(in, rc);
65 /**
66 * new_inode_smack - allocate an inode security blob
67 * @smack: a pointer to the Smack label to use in the blob
69 * Returns the new blob or NULL if there's no memory available
71 struct inode_smack *new_inode_smack(char *smack)
73 struct inode_smack *isp;
75 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
76 if (isp == NULL)
77 return NULL;
79 isp->smk_inode = smack;
80 isp->smk_flags = 0;
81 mutex_init(&isp->smk_lock);
83 return isp;
87 * LSM hooks.
88 * We he, that is fun!
91 /**
92 * smack_ptrace_may_access - Smack approval on PTRACE_ATTACH
93 * @ctp: child task pointer
95 * Returns 0 if access is OK, an error code otherwise
97 * Do the capability checks, and require read and write.
99 static int smack_ptrace_may_access(struct task_struct *ctp, unsigned int mode)
101 int rc;
103 rc = cap_ptrace_may_access(ctp, mode);
104 if (rc != 0)
105 return rc;
107 rc = smk_access(current_security(), task_security(ctp), MAY_READWRITE);
108 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
109 return 0;
110 return rc;
114 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
115 * @ptp: parent task pointer
117 * Returns 0 if access is OK, an error code otherwise
119 * Do the capability checks, and require read and write.
121 static int smack_ptrace_traceme(struct task_struct *ptp)
123 int rc;
125 rc = cap_ptrace_traceme(ptp);
126 if (rc != 0)
127 return rc;
129 rc = smk_access(task_security(ptp), current_security(), MAY_READWRITE);
130 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
131 return 0;
132 return rc;
136 * smack_syslog - Smack approval on syslog
137 * @type: message type
139 * Require that the task has the floor label
141 * Returns 0 on success, error code otherwise.
143 static int smack_syslog(int type)
145 int rc;
146 char *sp = current_security();
148 rc = cap_syslog(type);
149 if (rc != 0)
150 return rc;
152 if (capable(CAP_MAC_OVERRIDE))
153 return 0;
155 if (sp != smack_known_floor.smk_known)
156 rc = -EACCES;
158 return rc;
163 * Superblock Hooks.
167 * smack_sb_alloc_security - allocate a superblock blob
168 * @sb: the superblock getting the blob
170 * Returns 0 on success or -ENOMEM on error.
172 static int smack_sb_alloc_security(struct super_block *sb)
174 struct superblock_smack *sbsp;
176 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
178 if (sbsp == NULL)
179 return -ENOMEM;
181 sbsp->smk_root = smack_known_floor.smk_known;
182 sbsp->smk_default = smack_known_floor.smk_known;
183 sbsp->smk_floor = smack_known_floor.smk_known;
184 sbsp->smk_hat = smack_known_hat.smk_known;
185 sbsp->smk_initialized = 0;
186 spin_lock_init(&sbsp->smk_sblock);
188 sb->s_security = sbsp;
190 return 0;
194 * smack_sb_free_security - free a superblock blob
195 * @sb: the superblock getting the blob
198 static void smack_sb_free_security(struct super_block *sb)
200 kfree(sb->s_security);
201 sb->s_security = NULL;
205 * smack_sb_copy_data - copy mount options data for processing
206 * @type: file system type
207 * @orig: where to start
208 * @smackopts
210 * Returns 0 on success or -ENOMEM on error.
212 * Copy the Smack specific mount options out of the mount
213 * options list.
215 static int smack_sb_copy_data(char *orig, char *smackopts)
217 char *cp, *commap, *otheropts, *dp;
219 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
220 if (otheropts == NULL)
221 return -ENOMEM;
223 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
224 if (strstr(cp, SMK_FSDEFAULT) == cp)
225 dp = smackopts;
226 else if (strstr(cp, SMK_FSFLOOR) == cp)
227 dp = smackopts;
228 else if (strstr(cp, SMK_FSHAT) == cp)
229 dp = smackopts;
230 else if (strstr(cp, SMK_FSROOT) == cp)
231 dp = smackopts;
232 else
233 dp = otheropts;
235 commap = strchr(cp, ',');
236 if (commap != NULL)
237 *commap = '\0';
239 if (*dp != '\0')
240 strcat(dp, ",");
241 strcat(dp, cp);
244 strcpy(orig, otheropts);
245 free_page((unsigned long)otheropts);
247 return 0;
251 * smack_sb_kern_mount - Smack specific mount processing
252 * @sb: the file system superblock
253 * @flags: the mount flags
254 * @data: the smack mount options
256 * Returns 0 on success, an error code on failure
258 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
260 struct dentry *root = sb->s_root;
261 struct inode *inode = root->d_inode;
262 struct superblock_smack *sp = sb->s_security;
263 struct inode_smack *isp;
264 char *op;
265 char *commap;
266 char *nsp;
268 spin_lock(&sp->smk_sblock);
269 if (sp->smk_initialized != 0) {
270 spin_unlock(&sp->smk_sblock);
271 return 0;
273 sp->smk_initialized = 1;
274 spin_unlock(&sp->smk_sblock);
276 for (op = data; op != NULL; op = commap) {
277 commap = strchr(op, ',');
278 if (commap != NULL)
279 *commap++ = '\0';
281 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
282 op += strlen(SMK_FSHAT);
283 nsp = smk_import(op, 0);
284 if (nsp != NULL)
285 sp->smk_hat = nsp;
286 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
287 op += strlen(SMK_FSFLOOR);
288 nsp = smk_import(op, 0);
289 if (nsp != NULL)
290 sp->smk_floor = nsp;
291 } else if (strncmp(op, SMK_FSDEFAULT,
292 strlen(SMK_FSDEFAULT)) == 0) {
293 op += strlen(SMK_FSDEFAULT);
294 nsp = smk_import(op, 0);
295 if (nsp != NULL)
296 sp->smk_default = nsp;
297 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
298 op += strlen(SMK_FSROOT);
299 nsp = smk_import(op, 0);
300 if (nsp != NULL)
301 sp->smk_root = nsp;
306 * Initialize the root inode.
308 isp = inode->i_security;
309 if (isp == NULL)
310 inode->i_security = new_inode_smack(sp->smk_root);
311 else
312 isp->smk_inode = sp->smk_root;
314 return 0;
318 * smack_sb_statfs - Smack check on statfs
319 * @dentry: identifies the file system in question
321 * Returns 0 if current can read the floor of the filesystem,
322 * and error code otherwise
324 static int smack_sb_statfs(struct dentry *dentry)
326 struct superblock_smack *sbp = dentry->d_sb->s_security;
328 return smk_curacc(sbp->smk_floor, MAY_READ);
332 * smack_sb_mount - Smack check for mounting
333 * @dev_name: unused
334 * @nd: mount point
335 * @type: unused
336 * @flags: unused
337 * @data: unused
339 * Returns 0 if current can write the floor of the filesystem
340 * being mounted on, an error code otherwise.
342 static int smack_sb_mount(char *dev_name, struct path *path,
343 char *type, unsigned long flags, void *data)
345 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
347 return smk_curacc(sbp->smk_floor, MAY_WRITE);
351 * smack_sb_umount - Smack check for unmounting
352 * @mnt: file system to unmount
353 * @flags: unused
355 * Returns 0 if current can write the floor of the filesystem
356 * being unmounted, an error code otherwise.
358 static int smack_sb_umount(struct vfsmount *mnt, int flags)
360 struct superblock_smack *sbp;
362 sbp = mnt->mnt_sb->s_security;
364 return smk_curacc(sbp->smk_floor, MAY_WRITE);
368 * Inode hooks
372 * smack_inode_alloc_security - allocate an inode blob
373 * @inode - the inode in need of a blob
375 * Returns 0 if it gets a blob, -ENOMEM otherwise
377 static int smack_inode_alloc_security(struct inode *inode)
379 inode->i_security = new_inode_smack(current_security());
380 if (inode->i_security == NULL)
381 return -ENOMEM;
382 return 0;
386 * smack_inode_free_security - free an inode blob
387 * @inode - the inode with a blob
389 * Clears the blob pointer in inode
391 static void smack_inode_free_security(struct inode *inode)
393 kfree(inode->i_security);
394 inode->i_security = NULL;
398 * smack_inode_init_security - copy out the smack from an inode
399 * @inode: the inode
400 * @dir: unused
401 * @name: where to put the attribute name
402 * @value: where to put the attribute value
403 * @len: where to put the length of the attribute
405 * Returns 0 if it all works out, -ENOMEM if there's no memory
407 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
408 char **name, void **value, size_t *len)
410 char *isp = smk_of_inode(inode);
412 if (name) {
413 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
414 if (*name == NULL)
415 return -ENOMEM;
418 if (value) {
419 *value = kstrdup(isp, GFP_KERNEL);
420 if (*value == NULL)
421 return -ENOMEM;
424 if (len)
425 *len = strlen(isp) + 1;
427 return 0;
431 * smack_inode_link - Smack check on link
432 * @old_dentry: the existing object
433 * @dir: unused
434 * @new_dentry: the new object
436 * Returns 0 if access is permitted, an error code otherwise
438 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
439 struct dentry *new_dentry)
441 int rc;
442 char *isp;
444 isp = smk_of_inode(old_dentry->d_inode);
445 rc = smk_curacc(isp, MAY_WRITE);
447 if (rc == 0 && new_dentry->d_inode != NULL) {
448 isp = smk_of_inode(new_dentry->d_inode);
449 rc = smk_curacc(isp, MAY_WRITE);
452 return rc;
456 * smack_inode_unlink - Smack check on inode deletion
457 * @dir: containing directory object
458 * @dentry: file to unlink
460 * Returns 0 if current can write the containing directory
461 * and the object, error code otherwise
463 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
465 struct inode *ip = dentry->d_inode;
466 int rc;
469 * You need write access to the thing you're unlinking
471 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
472 if (rc == 0)
474 * You also need write access to the containing directory
476 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
478 return rc;
482 * smack_inode_rmdir - Smack check on directory deletion
483 * @dir: containing directory object
484 * @dentry: directory to unlink
486 * Returns 0 if current can write the containing directory
487 * and the directory, error code otherwise
489 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
491 int rc;
494 * You need write access to the thing you're removing
496 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
497 if (rc == 0)
499 * You also need write access to the containing directory
501 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
503 return rc;
507 * smack_inode_rename - Smack check on rename
508 * @old_inode: the old directory
509 * @old_dentry: unused
510 * @new_inode: the new directory
511 * @new_dentry: unused
513 * Read and write access is required on both the old and
514 * new directories.
516 * Returns 0 if access is permitted, an error code otherwise
518 static int smack_inode_rename(struct inode *old_inode,
519 struct dentry *old_dentry,
520 struct inode *new_inode,
521 struct dentry *new_dentry)
523 int rc;
524 char *isp;
526 isp = smk_of_inode(old_dentry->d_inode);
527 rc = smk_curacc(isp, MAY_READWRITE);
529 if (rc == 0 && new_dentry->d_inode != NULL) {
530 isp = smk_of_inode(new_dentry->d_inode);
531 rc = smk_curacc(isp, MAY_READWRITE);
534 return rc;
538 * smack_inode_permission - Smack version of permission()
539 * @inode: the inode in question
540 * @mask: the access requested
541 * @nd: unused
543 * This is the important Smack hook.
545 * Returns 0 if access is permitted, -EACCES otherwise
547 static int smack_inode_permission(struct inode *inode, int mask)
550 * No permission to check. Existence test. Yup, it's there.
552 if (mask == 0)
553 return 0;
555 return smk_curacc(smk_of_inode(inode), mask);
559 * smack_inode_setattr - Smack check for setting attributes
560 * @dentry: the object
561 * @iattr: for the force flag
563 * Returns 0 if access is permitted, an error code otherwise
565 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
568 * Need to allow for clearing the setuid bit.
570 if (iattr->ia_valid & ATTR_FORCE)
571 return 0;
573 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
577 * smack_inode_getattr - Smack check for getting attributes
578 * @mnt: unused
579 * @dentry: the object
581 * Returns 0 if access is permitted, an error code otherwise
583 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
585 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
589 * smack_inode_setxattr - Smack check for setting xattrs
590 * @dentry: the object
591 * @name: name of the attribute
592 * @value: unused
593 * @size: unused
594 * @flags: unused
596 * This protects the Smack attribute explicitly.
598 * Returns 0 if access is permitted, an error code otherwise
600 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
601 const void *value, size_t size, int flags)
603 int rc = 0;
605 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
606 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
607 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
608 if (!capable(CAP_MAC_ADMIN))
609 rc = -EPERM;
610 } else
611 rc = cap_inode_setxattr(dentry, name, value, size, flags);
613 if (rc == 0)
614 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
616 return rc;
620 * smack_inode_post_setxattr - Apply the Smack update approved above
621 * @dentry: object
622 * @name: attribute name
623 * @value: attribute value
624 * @size: attribute size
625 * @flags: unused
627 * Set the pointer in the inode blob to the entry found
628 * in the master label list.
630 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
631 const void *value, size_t size, int flags)
633 struct inode_smack *isp;
634 char *nsp;
637 * Not SMACK
639 if (strcmp(name, XATTR_NAME_SMACK))
640 return;
642 if (size >= SMK_LABELLEN)
643 return;
645 isp = dentry->d_inode->i_security;
648 * No locking is done here. This is a pointer
649 * assignment.
651 nsp = smk_import(value, size);
652 if (nsp != NULL)
653 isp->smk_inode = nsp;
654 else
655 isp->smk_inode = smack_known_invalid.smk_known;
657 return;
661 * smack_inode_getxattr - Smack check on getxattr
662 * @dentry: the object
663 * @name: unused
665 * Returns 0 if access is permitted, an error code otherwise
667 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
669 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
673 * smack_inode_removexattr - Smack check on removexattr
674 * @dentry: the object
675 * @name: name of the attribute
677 * Removing the Smack attribute requires CAP_MAC_ADMIN
679 * Returns 0 if access is permitted, an error code otherwise
681 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
683 int rc = 0;
685 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
686 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
687 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
688 if (!capable(CAP_MAC_ADMIN))
689 rc = -EPERM;
690 } else
691 rc = cap_inode_removexattr(dentry, name);
693 if (rc == 0)
694 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
696 return rc;
700 * smack_inode_getsecurity - get smack xattrs
701 * @inode: the object
702 * @name: attribute name
703 * @buffer: where to put the result
704 * @size: size of the buffer
705 * @err: unused
707 * Returns the size of the attribute or an error code
709 static int smack_inode_getsecurity(const struct inode *inode,
710 const char *name, void **buffer,
711 bool alloc)
713 struct socket_smack *ssp;
714 struct socket *sock;
715 struct super_block *sbp;
716 struct inode *ip = (struct inode *)inode;
717 char *isp;
718 int ilen;
719 int rc = 0;
721 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
722 isp = smk_of_inode(inode);
723 ilen = strlen(isp) + 1;
724 *buffer = isp;
725 return ilen;
729 * The rest of the Smack xattrs are only on sockets.
731 sbp = ip->i_sb;
732 if (sbp->s_magic != SOCKFS_MAGIC)
733 return -EOPNOTSUPP;
735 sock = SOCKET_I(ip);
736 if (sock == NULL || sock->sk == NULL)
737 return -EOPNOTSUPP;
739 ssp = sock->sk->sk_security;
741 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
742 isp = ssp->smk_in;
743 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
744 isp = ssp->smk_out;
745 else
746 return -EOPNOTSUPP;
748 ilen = strlen(isp) + 1;
749 if (rc == 0) {
750 *buffer = isp;
751 rc = ilen;
754 return rc;
759 * smack_inode_listsecurity - list the Smack attributes
760 * @inode: the object
761 * @buffer: where they go
762 * @buffer_size: size of buffer
764 * Returns 0 on success, -EINVAL otherwise
766 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
767 size_t buffer_size)
769 int len = strlen(XATTR_NAME_SMACK);
771 if (buffer != NULL && len <= buffer_size) {
772 memcpy(buffer, XATTR_NAME_SMACK, len);
773 return len;
775 return -EINVAL;
779 * smack_inode_getsecid - Extract inode's security id
780 * @inode: inode to extract the info from
781 * @secid: where result will be saved
783 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
785 struct inode_smack *isp = inode->i_security;
787 *secid = smack_to_secid(isp->smk_inode);
791 * File Hooks
795 * smack_file_permission - Smack check on file operations
796 * @file: unused
797 * @mask: unused
799 * Returns 0
801 * Should access checks be done on each read or write?
802 * UNICOS and SELinux say yes.
803 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
805 * I'll say no for now. Smack does not do the frequent
806 * label changing that SELinux does.
808 static int smack_file_permission(struct file *file, int mask)
810 return 0;
814 * smack_file_alloc_security - assign a file security blob
815 * @file: the object
817 * The security blob for a file is a pointer to the master
818 * label list, so no allocation is done.
820 * Returns 0
822 static int smack_file_alloc_security(struct file *file)
824 file->f_security = current_security();
825 return 0;
829 * smack_file_free_security - clear a file security blob
830 * @file: the object
832 * The security blob for a file is a pointer to the master
833 * label list, so no memory is freed.
835 static void smack_file_free_security(struct file *file)
837 file->f_security = NULL;
841 * smack_file_ioctl - Smack check on ioctls
842 * @file: the object
843 * @cmd: what to do
844 * @arg: unused
846 * Relies heavily on the correct use of the ioctl command conventions.
848 * Returns 0 if allowed, error code otherwise
850 static int smack_file_ioctl(struct file *file, unsigned int cmd,
851 unsigned long arg)
853 int rc = 0;
855 if (_IOC_DIR(cmd) & _IOC_WRITE)
856 rc = smk_curacc(file->f_security, MAY_WRITE);
858 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
859 rc = smk_curacc(file->f_security, MAY_READ);
861 return rc;
865 * smack_file_lock - Smack check on file locking
866 * @file: the object
867 * @cmd unused
869 * Returns 0 if current has write access, error code otherwise
871 static int smack_file_lock(struct file *file, unsigned int cmd)
873 return smk_curacc(file->f_security, MAY_WRITE);
877 * smack_file_fcntl - Smack check on fcntl
878 * @file: the object
879 * @cmd: what action to check
880 * @arg: unused
882 * Returns 0 if current has access, error code otherwise
884 static int smack_file_fcntl(struct file *file, unsigned int cmd,
885 unsigned long arg)
887 int rc;
889 switch (cmd) {
890 case F_DUPFD:
891 case F_GETFD:
892 case F_GETFL:
893 case F_GETLK:
894 case F_GETOWN:
895 case F_GETSIG:
896 rc = smk_curacc(file->f_security, MAY_READ);
897 break;
898 case F_SETFD:
899 case F_SETFL:
900 case F_SETLK:
901 case F_SETLKW:
902 case F_SETOWN:
903 case F_SETSIG:
904 rc = smk_curacc(file->f_security, MAY_WRITE);
905 break;
906 default:
907 rc = smk_curacc(file->f_security, MAY_READWRITE);
910 return rc;
914 * smack_file_set_fowner - set the file security blob value
915 * @file: object in question
917 * Returns 0
918 * Further research may be required on this one.
920 static int smack_file_set_fowner(struct file *file)
922 file->f_security = current_security();
923 return 0;
927 * smack_file_send_sigiotask - Smack on sigio
928 * @tsk: The target task
929 * @fown: the object the signal come from
930 * @signum: unused
932 * Allow a privileged task to get signals even if it shouldn't
934 * Returns 0 if a subject with the object's smack could
935 * write to the task, an error code otherwise.
937 static int smack_file_send_sigiotask(struct task_struct *tsk,
938 struct fown_struct *fown, int signum)
940 struct file *file;
941 int rc;
944 * struct fown_struct is never outside the context of a struct file
946 file = container_of(fown, struct file, f_owner);
947 rc = smk_access(file->f_security, tsk->cred->security, MAY_WRITE);
948 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
949 return 0;
950 return rc;
954 * smack_file_receive - Smack file receive check
955 * @file: the object
957 * Returns 0 if current has access, error code otherwise
959 static int smack_file_receive(struct file *file)
961 int may = 0;
964 * This code relies on bitmasks.
966 if (file->f_mode & FMODE_READ)
967 may = MAY_READ;
968 if (file->f_mode & FMODE_WRITE)
969 may |= MAY_WRITE;
971 return smk_curacc(file->f_security, may);
975 * Task hooks
979 * smack_cred_free - "free" task-level security credentials
980 * @cred: the credentials in question
982 * Smack isn't using copies of blobs. Everyone
983 * points to an immutable list. The blobs never go away.
984 * There is no leak here.
986 static void smack_cred_free(struct cred *cred)
988 cred->security = NULL;
992 * smack_cred_prepare - prepare new set of credentials for modification
993 * @new: the new credentials
994 * @old: the original credentials
995 * @gfp: the atomicity of any memory allocations
997 * Prepare a new set of credentials for modification.
999 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1000 gfp_t gfp)
1002 new->security = old->security;
1003 return 0;
1007 * commit new credentials
1008 * @new: the new credentials
1009 * @old: the original credentials
1011 static void smack_cred_commit(struct cred *new, const struct cred *old)
1016 * smack_kernel_act_as - Set the subjective context in a set of credentials
1017 * @new points to the set of credentials to be modified.
1018 * @secid specifies the security ID to be set
1020 * Set the security data for a kernel service.
1022 static int smack_kernel_act_as(struct cred *new, u32 secid)
1024 char *smack = smack_from_secid(secid);
1026 if (smack == NULL)
1027 return -EINVAL;
1029 new->security = smack;
1030 return 0;
1034 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1035 * @new points to the set of credentials to be modified
1036 * @inode points to the inode to use as a reference
1038 * Set the file creation context in a set of credentials to the same
1039 * as the objective context of the specified inode
1041 static int smack_kernel_create_files_as(struct cred *new,
1042 struct inode *inode)
1044 struct inode_smack *isp = inode->i_security;
1046 new->security = isp->smk_inode;
1047 return 0;
1051 * smack_task_setpgid - Smack check on setting pgid
1052 * @p: the task object
1053 * @pgid: unused
1055 * Return 0 if write access is permitted
1057 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1059 return smk_curacc(task_security(p), MAY_WRITE);
1063 * smack_task_getpgid - Smack access check for getpgid
1064 * @p: the object task
1066 * Returns 0 if current can read the object task, error code otherwise
1068 static int smack_task_getpgid(struct task_struct *p)
1070 return smk_curacc(task_security(p), MAY_READ);
1074 * smack_task_getsid - Smack access check for getsid
1075 * @p: the object task
1077 * Returns 0 if current can read the object task, error code otherwise
1079 static int smack_task_getsid(struct task_struct *p)
1081 return smk_curacc(task_security(p), MAY_READ);
1085 * smack_task_getsecid - get the secid of the task
1086 * @p: the object task
1087 * @secid: where to put the result
1089 * Sets the secid to contain a u32 version of the smack label.
1091 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1093 *secid = smack_to_secid(task_security(p));
1097 * smack_task_setnice - Smack check on setting nice
1098 * @p: the task object
1099 * @nice: unused
1101 * Return 0 if write access is permitted
1103 static int smack_task_setnice(struct task_struct *p, int nice)
1105 int rc;
1107 rc = cap_task_setnice(p, nice);
1108 if (rc == 0)
1109 rc = smk_curacc(task_security(p), MAY_WRITE);
1110 return rc;
1114 * smack_task_setioprio - Smack check on setting ioprio
1115 * @p: the task object
1116 * @ioprio: unused
1118 * Return 0 if write access is permitted
1120 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1122 int rc;
1124 rc = cap_task_setioprio(p, ioprio);
1125 if (rc == 0)
1126 rc = smk_curacc(task_security(p), MAY_WRITE);
1127 return rc;
1131 * smack_task_getioprio - Smack check on reading ioprio
1132 * @p: the task object
1134 * Return 0 if read access is permitted
1136 static int smack_task_getioprio(struct task_struct *p)
1138 return smk_curacc(task_security(p), MAY_READ);
1142 * smack_task_setscheduler - Smack check on setting scheduler
1143 * @p: the task object
1144 * @policy: unused
1145 * @lp: unused
1147 * Return 0 if read access is permitted
1149 static int smack_task_setscheduler(struct task_struct *p, int policy,
1150 struct sched_param *lp)
1152 int rc;
1154 rc = cap_task_setscheduler(p, policy, lp);
1155 if (rc == 0)
1156 rc = smk_curacc(task_security(p), MAY_WRITE);
1157 return rc;
1161 * smack_task_getscheduler - Smack check on reading scheduler
1162 * @p: the task object
1164 * Return 0 if read access is permitted
1166 static int smack_task_getscheduler(struct task_struct *p)
1168 return smk_curacc(task_security(p), MAY_READ);
1172 * smack_task_movememory - Smack check on moving memory
1173 * @p: the task object
1175 * Return 0 if write access is permitted
1177 static int smack_task_movememory(struct task_struct *p)
1179 return smk_curacc(task_security(p), MAY_WRITE);
1183 * smack_task_kill - Smack check on signal delivery
1184 * @p: the task object
1185 * @info: unused
1186 * @sig: unused
1187 * @secid: identifies the smack to use in lieu of current's
1189 * Return 0 if write access is permitted
1191 * The secid behavior is an artifact of an SELinux hack
1192 * in the USB code. Someday it may go away.
1194 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1195 int sig, u32 secid)
1198 * Sending a signal requires that the sender
1199 * can write the receiver.
1201 if (secid == 0)
1202 return smk_curacc(task_security(p), MAY_WRITE);
1204 * If the secid isn't 0 we're dealing with some USB IO
1205 * specific behavior. This is not clean. For one thing
1206 * we can't take privilege into account.
1208 return smk_access(smack_from_secid(secid), task_security(p), MAY_WRITE);
1212 * smack_task_wait - Smack access check for waiting
1213 * @p: task to wait for
1215 * Returns 0 if current can wait for p, error code otherwise
1217 static int smack_task_wait(struct task_struct *p)
1219 int rc;
1221 rc = smk_access(current_security(), task_security(p), MAY_WRITE);
1222 if (rc == 0)
1223 return 0;
1226 * Allow the operation to succeed if either task
1227 * has privilege to perform operations that might
1228 * account for the smack labels having gotten to
1229 * be different in the first place.
1231 * This breaks the strict subject/object access
1232 * control ideal, taking the object's privilege
1233 * state into account in the decision as well as
1234 * the smack value.
1236 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1237 return 0;
1239 return rc;
1243 * smack_task_to_inode - copy task smack into the inode blob
1244 * @p: task to copy from
1245 * inode: inode to copy to
1247 * Sets the smack pointer in the inode security blob
1249 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1251 struct inode_smack *isp = inode->i_security;
1252 isp->smk_inode = task_security(p);
1256 * Socket hooks.
1260 * smack_sk_alloc_security - Allocate a socket blob
1261 * @sk: the socket
1262 * @family: unused
1263 * @priority: memory allocation priority
1265 * Assign Smack pointers to current
1267 * Returns 0 on success, -ENOMEM is there's no memory
1269 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1271 char *csp = current_security();
1272 struct socket_smack *ssp;
1274 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1275 if (ssp == NULL)
1276 return -ENOMEM;
1278 ssp->smk_in = csp;
1279 ssp->smk_out = csp;
1280 ssp->smk_labeled = SMACK_CIPSO_SOCKET;
1281 ssp->smk_packet[0] = '\0';
1283 sk->sk_security = ssp;
1285 return 0;
1289 * smack_sk_free_security - Free a socket blob
1290 * @sk: the socket
1292 * Clears the blob pointer
1294 static void smack_sk_free_security(struct sock *sk)
1296 kfree(sk->sk_security);
1300 * smack_set_catset - convert a capset to netlabel mls categories
1301 * @catset: the Smack categories
1302 * @sap: where to put the netlabel categories
1304 * Allocates and fills attr.mls.cat
1306 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1308 unsigned char *cp;
1309 unsigned char m;
1310 int cat;
1311 int rc;
1312 int byte;
1314 if (!catset)
1315 return;
1317 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1318 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1319 sap->attr.mls.cat->startbit = 0;
1321 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1322 for (m = 0x80; m != 0; m >>= 1, cat++) {
1323 if ((m & *cp) == 0)
1324 continue;
1325 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1326 cat, GFP_ATOMIC);
1331 * smack_to_secattr - fill a secattr from a smack value
1332 * @smack: the smack value
1333 * @nlsp: where the result goes
1335 * Casey says that CIPSO is good enough for now.
1336 * It can be used to effect.
1337 * It can also be abused to effect when necessary.
1338 * Appologies to the TSIG group in general and GW in particular.
1340 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1342 struct smack_cipso cipso;
1343 int rc;
1345 nlsp->domain = smack;
1346 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1348 rc = smack_to_cipso(smack, &cipso);
1349 if (rc == 0) {
1350 nlsp->attr.mls.lvl = cipso.smk_level;
1351 smack_set_catset(cipso.smk_catset, nlsp);
1352 } else {
1353 nlsp->attr.mls.lvl = smack_cipso_direct;
1354 smack_set_catset(smack, nlsp);
1359 * smack_netlabel - Set the secattr on a socket
1360 * @sk: the socket
1361 * @labeled: socket label scheme
1363 * Convert the outbound smack value (smk_out) to a
1364 * secattr and attach it to the socket.
1366 * Returns 0 on success or an error code
1368 static int smack_netlabel(struct sock *sk, int labeled)
1370 struct socket_smack *ssp;
1371 struct netlbl_lsm_secattr secattr;
1372 int rc = 0;
1374 ssp = sk->sk_security;
1376 * Usually the netlabel code will handle changing the
1377 * packet labeling based on the label.
1378 * The case of a single label host is different, because
1379 * a single label host should never get a labeled packet
1380 * even though the label is usually associated with a packet
1381 * label.
1383 local_bh_disable();
1384 bh_lock_sock_nested(sk);
1386 if (ssp->smk_out == smack_net_ambient ||
1387 labeled == SMACK_UNLABELED_SOCKET)
1388 netlbl_sock_delattr(sk);
1389 else {
1390 netlbl_secattr_init(&secattr);
1391 smack_to_secattr(ssp->smk_out, &secattr);
1392 rc = netlbl_sock_setattr(sk, &secattr);
1393 netlbl_secattr_destroy(&secattr);
1396 bh_unlock_sock(sk);
1397 local_bh_enable();
1399 * Remember the label scheme used so that it is not
1400 * necessary to do the netlabel setting if it has not
1401 * changed the next time through.
1403 * The -EDESTADDRREQ case is an indication that there's
1404 * a single level host involved.
1406 if (rc == 0)
1407 ssp->smk_labeled = labeled;
1409 return rc;
1413 * smack_inode_setsecurity - set smack xattrs
1414 * @inode: the object
1415 * @name: attribute name
1416 * @value: attribute value
1417 * @size: size of the attribute
1418 * @flags: unused
1420 * Sets the named attribute in the appropriate blob
1422 * Returns 0 on success, or an error code
1424 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1425 const void *value, size_t size, int flags)
1427 char *sp;
1428 struct inode_smack *nsp = inode->i_security;
1429 struct socket_smack *ssp;
1430 struct socket *sock;
1431 int rc = 0;
1433 if (value == NULL || size > SMK_LABELLEN)
1434 return -EACCES;
1436 sp = smk_import(value, size);
1437 if (sp == NULL)
1438 return -EINVAL;
1440 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1441 nsp->smk_inode = sp;
1442 return 0;
1445 * The rest of the Smack xattrs are only on sockets.
1447 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1448 return -EOPNOTSUPP;
1450 sock = SOCKET_I(inode);
1451 if (sock == NULL || sock->sk == NULL)
1452 return -EOPNOTSUPP;
1454 ssp = sock->sk->sk_security;
1456 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1457 ssp->smk_in = sp;
1458 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1459 ssp->smk_out = sp;
1460 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1461 if (rc != 0)
1462 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1463 __func__, -rc);
1464 } else
1465 return -EOPNOTSUPP;
1467 return 0;
1471 * smack_socket_post_create - finish socket setup
1472 * @sock: the socket
1473 * @family: protocol family
1474 * @type: unused
1475 * @protocol: unused
1476 * @kern: unused
1478 * Sets the netlabel information on the socket
1480 * Returns 0 on success, and error code otherwise
1482 static int smack_socket_post_create(struct socket *sock, int family,
1483 int type, int protocol, int kern)
1485 if (family != PF_INET || sock->sk == NULL)
1486 return 0;
1488 * Set the outbound netlbl.
1490 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1495 * smack_host_label - check host based restrictions
1496 * @sip: the object end
1498 * looks for host based access restrictions
1500 * This version will only be appropriate for really small
1501 * sets of single label hosts. Because of the masking
1502 * it cannot shortcut out on the first match. There are
1503 * numerious ways to address the problem, but none of them
1504 * have been applied here.
1506 * Returns the label of the far end or NULL if it's not special.
1508 static char *smack_host_label(struct sockaddr_in *sip)
1510 struct smk_netlbladdr *snp;
1511 char *bestlabel = NULL;
1512 struct in_addr *siap = &sip->sin_addr;
1513 struct in_addr *liap;
1514 struct in_addr *miap;
1515 struct in_addr bestmask;
1517 if (siap->s_addr == 0)
1518 return NULL;
1520 bestmask.s_addr = 0;
1522 for (snp = smack_netlbladdrs; snp != NULL; snp = snp->smk_next) {
1523 liap = &snp->smk_host.sin_addr;
1524 miap = &snp->smk_mask;
1526 * If the addresses match after applying the list entry mask
1527 * the entry matches the address. If it doesn't move along to
1528 * the next entry.
1530 if ((liap->s_addr & miap->s_addr) !=
1531 (siap->s_addr & miap->s_addr))
1532 continue;
1534 * If the list entry mask identifies a single address
1535 * it can't get any more specific.
1537 if (miap->s_addr == 0xffffffff)
1538 return snp->smk_label;
1540 * If the list entry mask is less specific than the best
1541 * already found this entry is uninteresting.
1543 if ((miap->s_addr | bestmask.s_addr) == bestmask.s_addr)
1544 continue;
1546 * This is better than any entry found so far.
1548 bestmask.s_addr = miap->s_addr;
1549 bestlabel = snp->smk_label;
1552 return bestlabel;
1556 * smack_socket_connect - connect access check
1557 * @sock: the socket
1558 * @sap: the other end
1559 * @addrlen: size of sap
1561 * Verifies that a connection may be possible
1563 * Returns 0 on success, and error code otherwise
1565 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1566 int addrlen)
1568 struct socket_smack *ssp = sock->sk->sk_security;
1569 char *hostsp;
1570 int rc;
1572 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1573 return 0;
1575 if (addrlen < sizeof(struct sockaddr_in))
1576 return -EINVAL;
1578 hostsp = smack_host_label((struct sockaddr_in *)sap);
1579 if (hostsp == NULL) {
1580 if (ssp->smk_labeled != SMACK_CIPSO_SOCKET)
1581 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1582 return 0;
1585 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE);
1586 if (rc != 0)
1587 return rc;
1589 if (ssp->smk_labeled != SMACK_UNLABELED_SOCKET)
1590 return smack_netlabel(sock->sk, SMACK_UNLABELED_SOCKET);
1591 return 0;
1595 * smack_flags_to_may - convert S_ to MAY_ values
1596 * @flags: the S_ value
1598 * Returns the equivalent MAY_ value
1600 static int smack_flags_to_may(int flags)
1602 int may = 0;
1604 if (flags & S_IRUGO)
1605 may |= MAY_READ;
1606 if (flags & S_IWUGO)
1607 may |= MAY_WRITE;
1608 if (flags & S_IXUGO)
1609 may |= MAY_EXEC;
1611 return may;
1615 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1616 * @msg: the object
1618 * Returns 0
1620 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1622 msg->security = current_security();
1623 return 0;
1627 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1628 * @msg: the object
1630 * Clears the blob pointer
1632 static void smack_msg_msg_free_security(struct msg_msg *msg)
1634 msg->security = NULL;
1638 * smack_of_shm - the smack pointer for the shm
1639 * @shp: the object
1641 * Returns a pointer to the smack value
1643 static char *smack_of_shm(struct shmid_kernel *shp)
1645 return (char *)shp->shm_perm.security;
1649 * smack_shm_alloc_security - Set the security blob for shm
1650 * @shp: the object
1652 * Returns 0
1654 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1656 struct kern_ipc_perm *isp = &shp->shm_perm;
1658 isp->security = current_security();
1659 return 0;
1663 * smack_shm_free_security - Clear the security blob for shm
1664 * @shp: the object
1666 * Clears the blob pointer
1668 static void smack_shm_free_security(struct shmid_kernel *shp)
1670 struct kern_ipc_perm *isp = &shp->shm_perm;
1672 isp->security = NULL;
1676 * smack_shm_associate - Smack access check for shm
1677 * @shp: the object
1678 * @shmflg: access requested
1680 * Returns 0 if current has the requested access, error code otherwise
1682 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1684 char *ssp = smack_of_shm(shp);
1685 int may;
1687 may = smack_flags_to_may(shmflg);
1688 return smk_curacc(ssp, may);
1692 * smack_shm_shmctl - Smack access check for shm
1693 * @shp: the object
1694 * @cmd: what it wants to do
1696 * Returns 0 if current has the requested access, error code otherwise
1698 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1700 char *ssp;
1701 int may;
1703 switch (cmd) {
1704 case IPC_STAT:
1705 case SHM_STAT:
1706 may = MAY_READ;
1707 break;
1708 case IPC_SET:
1709 case SHM_LOCK:
1710 case SHM_UNLOCK:
1711 case IPC_RMID:
1712 may = MAY_READWRITE;
1713 break;
1714 case IPC_INFO:
1715 case SHM_INFO:
1717 * System level information.
1719 return 0;
1720 default:
1721 return -EINVAL;
1724 ssp = smack_of_shm(shp);
1725 return smk_curacc(ssp, may);
1729 * smack_shm_shmat - Smack access for shmat
1730 * @shp: the object
1731 * @shmaddr: unused
1732 * @shmflg: access requested
1734 * Returns 0 if current has the requested access, error code otherwise
1736 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1737 int shmflg)
1739 char *ssp = smack_of_shm(shp);
1740 int may;
1742 may = smack_flags_to_may(shmflg);
1743 return smk_curacc(ssp, may);
1747 * smack_of_sem - the smack pointer for the sem
1748 * @sma: the object
1750 * Returns a pointer to the smack value
1752 static char *smack_of_sem(struct sem_array *sma)
1754 return (char *)sma->sem_perm.security;
1758 * smack_sem_alloc_security - Set the security blob for sem
1759 * @sma: the object
1761 * Returns 0
1763 static int smack_sem_alloc_security(struct sem_array *sma)
1765 struct kern_ipc_perm *isp = &sma->sem_perm;
1767 isp->security = current_security();
1768 return 0;
1772 * smack_sem_free_security - Clear the security blob for sem
1773 * @sma: the object
1775 * Clears the blob pointer
1777 static void smack_sem_free_security(struct sem_array *sma)
1779 struct kern_ipc_perm *isp = &sma->sem_perm;
1781 isp->security = NULL;
1785 * smack_sem_associate - Smack access check for sem
1786 * @sma: the object
1787 * @semflg: access requested
1789 * Returns 0 if current has the requested access, error code otherwise
1791 static int smack_sem_associate(struct sem_array *sma, int semflg)
1793 char *ssp = smack_of_sem(sma);
1794 int may;
1796 may = smack_flags_to_may(semflg);
1797 return smk_curacc(ssp, may);
1801 * smack_sem_shmctl - Smack access check for sem
1802 * @sma: the object
1803 * @cmd: what it wants to do
1805 * Returns 0 if current has the requested access, error code otherwise
1807 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1809 char *ssp;
1810 int may;
1812 switch (cmd) {
1813 case GETPID:
1814 case GETNCNT:
1815 case GETZCNT:
1816 case GETVAL:
1817 case GETALL:
1818 case IPC_STAT:
1819 case SEM_STAT:
1820 may = MAY_READ;
1821 break;
1822 case SETVAL:
1823 case SETALL:
1824 case IPC_RMID:
1825 case IPC_SET:
1826 may = MAY_READWRITE;
1827 break;
1828 case IPC_INFO:
1829 case SEM_INFO:
1831 * System level information
1833 return 0;
1834 default:
1835 return -EINVAL;
1838 ssp = smack_of_sem(sma);
1839 return smk_curacc(ssp, may);
1843 * smack_sem_semop - Smack checks of semaphore operations
1844 * @sma: the object
1845 * @sops: unused
1846 * @nsops: unused
1847 * @alter: unused
1849 * Treated as read and write in all cases.
1851 * Returns 0 if access is allowed, error code otherwise
1853 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1854 unsigned nsops, int alter)
1856 char *ssp = smack_of_sem(sma);
1858 return smk_curacc(ssp, MAY_READWRITE);
1862 * smack_msg_alloc_security - Set the security blob for msg
1863 * @msq: the object
1865 * Returns 0
1867 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1869 struct kern_ipc_perm *kisp = &msq->q_perm;
1871 kisp->security = current_security();
1872 return 0;
1876 * smack_msg_free_security - Clear the security blob for msg
1877 * @msq: the object
1879 * Clears the blob pointer
1881 static void smack_msg_queue_free_security(struct msg_queue *msq)
1883 struct kern_ipc_perm *kisp = &msq->q_perm;
1885 kisp->security = NULL;
1889 * smack_of_msq - the smack pointer for the msq
1890 * @msq: the object
1892 * Returns a pointer to the smack value
1894 static char *smack_of_msq(struct msg_queue *msq)
1896 return (char *)msq->q_perm.security;
1900 * smack_msg_queue_associate - Smack access check for msg_queue
1901 * @msq: the object
1902 * @msqflg: access requested
1904 * Returns 0 if current has the requested access, error code otherwise
1906 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1908 char *msp = smack_of_msq(msq);
1909 int may;
1911 may = smack_flags_to_may(msqflg);
1912 return smk_curacc(msp, may);
1916 * smack_msg_queue_msgctl - Smack access check for msg_queue
1917 * @msq: the object
1918 * @cmd: what it wants to do
1920 * Returns 0 if current has the requested access, error code otherwise
1922 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1924 char *msp;
1925 int may;
1927 switch (cmd) {
1928 case IPC_STAT:
1929 case MSG_STAT:
1930 may = MAY_READ;
1931 break;
1932 case IPC_SET:
1933 case IPC_RMID:
1934 may = MAY_READWRITE;
1935 break;
1936 case IPC_INFO:
1937 case MSG_INFO:
1939 * System level information
1941 return 0;
1942 default:
1943 return -EINVAL;
1946 msp = smack_of_msq(msq);
1947 return smk_curacc(msp, may);
1951 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1952 * @msq: the object
1953 * @msg: unused
1954 * @msqflg: access requested
1956 * Returns 0 if current has the requested access, error code otherwise
1958 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1959 int msqflg)
1961 char *msp = smack_of_msq(msq);
1962 int rc;
1964 rc = smack_flags_to_may(msqflg);
1965 return smk_curacc(msp, rc);
1969 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1970 * @msq: the object
1971 * @msg: unused
1972 * @target: unused
1973 * @type: unused
1974 * @mode: unused
1976 * Returns 0 if current has read and write access, error code otherwise
1978 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1979 struct task_struct *target, long type, int mode)
1981 char *msp = smack_of_msq(msq);
1983 return smk_curacc(msp, MAY_READWRITE);
1987 * smack_ipc_permission - Smack access for ipc_permission()
1988 * @ipp: the object permissions
1989 * @flag: access requested
1991 * Returns 0 if current has read and write access, error code otherwise
1993 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1995 char *isp = ipp->security;
1996 int may;
1998 may = smack_flags_to_may(flag);
1999 return smk_curacc(isp, may);
2003 * smack_ipc_getsecid - Extract smack security id
2004 * @ipcp: the object permissions
2005 * @secid: where result will be saved
2007 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2009 char *smack = ipp->security;
2011 *secid = smack_to_secid(smack);
2015 * smack_d_instantiate - Make sure the blob is correct on an inode
2016 * @opt_dentry: unused
2017 * @inode: the object
2019 * Set the inode's security blob if it hasn't been done already.
2021 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2023 struct super_block *sbp;
2024 struct superblock_smack *sbsp;
2025 struct inode_smack *isp;
2026 char *csp = current_security();
2027 char *fetched;
2028 char *final;
2029 struct dentry *dp;
2031 if (inode == NULL)
2032 return;
2034 isp = inode->i_security;
2036 mutex_lock(&isp->smk_lock);
2038 * If the inode is already instantiated
2039 * take the quick way out
2041 if (isp->smk_flags & SMK_INODE_INSTANT)
2042 goto unlockandout;
2044 sbp = inode->i_sb;
2045 sbsp = sbp->s_security;
2047 * We're going to use the superblock default label
2048 * if there's no label on the file.
2050 final = sbsp->smk_default;
2053 * If this is the root inode the superblock
2054 * may be in the process of initialization.
2055 * If that is the case use the root value out
2056 * of the superblock.
2058 if (opt_dentry->d_parent == opt_dentry) {
2059 isp->smk_inode = sbsp->smk_root;
2060 isp->smk_flags |= SMK_INODE_INSTANT;
2061 goto unlockandout;
2065 * This is pretty hackish.
2066 * Casey says that we shouldn't have to do
2067 * file system specific code, but it does help
2068 * with keeping it simple.
2070 switch (sbp->s_magic) {
2071 case SMACK_MAGIC:
2073 * Casey says that it's a little embarassing
2074 * that the smack file system doesn't do
2075 * extended attributes.
2077 final = smack_known_star.smk_known;
2078 break;
2079 case PIPEFS_MAGIC:
2081 * Casey says pipes are easy (?)
2083 final = smack_known_star.smk_known;
2084 break;
2085 case DEVPTS_SUPER_MAGIC:
2087 * devpts seems content with the label of the task.
2088 * Programs that change smack have to treat the
2089 * pty with respect.
2091 final = csp;
2092 break;
2093 case SOCKFS_MAGIC:
2095 * Casey says sockets get the smack of the task.
2097 final = csp;
2098 break;
2099 case PROC_SUPER_MAGIC:
2101 * Casey says procfs appears not to care.
2102 * The superblock default suffices.
2104 break;
2105 case TMPFS_MAGIC:
2107 * Device labels should come from the filesystem,
2108 * but watch out, because they're volitile,
2109 * getting recreated on every reboot.
2111 final = smack_known_star.smk_known;
2113 * No break.
2115 * If a smack value has been set we want to use it,
2116 * but since tmpfs isn't giving us the opportunity
2117 * to set mount options simulate setting the
2118 * superblock default.
2120 default:
2122 * This isn't an understood special case.
2123 * Get the value from the xattr.
2125 * No xattr support means, alas, no SMACK label.
2126 * Use the aforeapplied default.
2127 * It would be curious if the label of the task
2128 * does not match that assigned.
2130 if (inode->i_op->getxattr == NULL)
2131 break;
2133 * Get the dentry for xattr.
2135 if (opt_dentry == NULL) {
2136 dp = d_find_alias(inode);
2137 if (dp == NULL)
2138 break;
2139 } else {
2140 dp = dget(opt_dentry);
2141 if (dp == NULL)
2142 break;
2145 fetched = smk_fetch(inode, dp);
2146 if (fetched != NULL)
2147 final = fetched;
2149 dput(dp);
2150 break;
2153 if (final == NULL)
2154 isp->smk_inode = csp;
2155 else
2156 isp->smk_inode = final;
2158 isp->smk_flags |= SMK_INODE_INSTANT;
2160 unlockandout:
2161 mutex_unlock(&isp->smk_lock);
2162 return;
2166 * smack_getprocattr - Smack process attribute access
2167 * @p: the object task
2168 * @name: the name of the attribute in /proc/.../attr
2169 * @value: where to put the result
2171 * Places a copy of the task Smack into value
2173 * Returns the length of the smack label or an error code
2175 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2177 char *cp;
2178 int slen;
2180 if (strcmp(name, "current") != 0)
2181 return -EINVAL;
2183 cp = kstrdup(task_security(p), GFP_KERNEL);
2184 if (cp == NULL)
2185 return -ENOMEM;
2187 slen = strlen(cp);
2188 *value = cp;
2189 return slen;
2193 * smack_setprocattr - Smack process attribute setting
2194 * @p: the object task
2195 * @name: the name of the attribute in /proc/.../attr
2196 * @value: the value to set
2197 * @size: the size of the value
2199 * Sets the Smack value of the task. Only setting self
2200 * is permitted and only with privilege
2202 * Returns the length of the smack label or an error code
2204 static int smack_setprocattr(struct task_struct *p, char *name,
2205 void *value, size_t size)
2207 struct cred *new;
2208 char *newsmack;
2211 * Changing another process' Smack value is too dangerous
2212 * and supports no sane use case.
2214 if (p != current)
2215 return -EPERM;
2217 if (!capable(CAP_MAC_ADMIN))
2218 return -EPERM;
2220 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2221 return -EINVAL;
2223 if (strcmp(name, "current") != 0)
2224 return -EINVAL;
2226 newsmack = smk_import(value, size);
2227 if (newsmack == NULL)
2228 return -EINVAL;
2231 * No process is ever allowed the web ("@") label.
2233 if (newsmack == smack_known_web.smk_known)
2234 return -EPERM;
2236 new = prepare_creds();
2237 if (new == NULL)
2238 return -ENOMEM;
2239 new->security = newsmack;
2240 commit_creds(new);
2241 return size;
2245 * smack_unix_stream_connect - Smack access on UDS
2246 * @sock: one socket
2247 * @other: the other socket
2248 * @newsk: unused
2250 * Return 0 if a subject with the smack of sock could access
2251 * an object with the smack of other, otherwise an error code
2253 static int smack_unix_stream_connect(struct socket *sock,
2254 struct socket *other, struct sock *newsk)
2256 struct inode *sp = SOCK_INODE(sock);
2257 struct inode *op = SOCK_INODE(other);
2259 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2263 * smack_unix_may_send - Smack access on UDS
2264 * @sock: one socket
2265 * @other: the other socket
2267 * Return 0 if a subject with the smack of sock could access
2268 * an object with the smack of other, otherwise an error code
2270 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2272 struct inode *sp = SOCK_INODE(sock);
2273 struct inode *op = SOCK_INODE(other);
2275 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2279 * smack_socket_sendmsg - Smack check based on destination host
2280 * @sock: the socket
2281 * @msghdr: the message
2282 * @size: the size of the message
2284 * Return 0 if the current subject can write to the destination
2285 * host. This is only a question if the destination is a single
2286 * label host.
2288 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2289 int size)
2291 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2292 struct socket_smack *ssp = sock->sk->sk_security;
2293 char *hostsp;
2294 int rc;
2297 * Perfectly reasonable for this to be NULL
2299 if (sip == NULL || sip->sin_family != PF_INET)
2300 return 0;
2302 hostsp = smack_host_label(sip);
2303 if (hostsp == NULL) {
2304 if (ssp->smk_labeled != SMACK_CIPSO_SOCKET)
2305 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2306 return 0;
2309 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE);
2310 if (rc != 0)
2311 return rc;
2313 if (ssp->smk_labeled != SMACK_UNLABELED_SOCKET)
2314 return smack_netlabel(sock->sk, SMACK_UNLABELED_SOCKET);
2316 return 0;
2322 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
2323 * pair to smack
2324 * @sap: netlabel secattr
2325 * @sip: where to put the result
2327 * Copies a smack label into sip
2329 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2331 char smack[SMK_LABELLEN];
2332 char *sp;
2333 int pcat;
2335 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2337 * Looks like a CIPSO packet.
2338 * If there are flags but no level netlabel isn't
2339 * behaving the way we expect it to.
2341 * Get the categories, if any
2342 * Without guidance regarding the smack value
2343 * for the packet fall back on the network
2344 * ambient value.
2346 memset(smack, '\0', SMK_LABELLEN);
2347 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2348 for (pcat = -1;;) {
2349 pcat = netlbl_secattr_catmap_walk(
2350 sap->attr.mls.cat, pcat + 1);
2351 if (pcat < 0)
2352 break;
2353 smack_catset_bit(pcat, smack);
2356 * If it is CIPSO using smack direct mapping
2357 * we are already done. WeeHee.
2359 if (sap->attr.mls.lvl == smack_cipso_direct) {
2360 memcpy(sip, smack, SMK_MAXLEN);
2361 return;
2364 * Look it up in the supplied table if it is not
2365 * a direct mapping.
2367 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2368 return;
2370 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2372 * Looks like a fallback, which gives us a secid.
2374 sp = smack_from_secid(sap->attr.secid);
2376 * This has got to be a bug because it is
2377 * impossible to specify a fallback without
2378 * specifying the label, which will ensure
2379 * it has a secid, and the only way to get a
2380 * secid is from a fallback.
2382 BUG_ON(sp == NULL);
2383 strncpy(sip, sp, SMK_MAXLEN);
2384 return;
2387 * Without guidance regarding the smack value
2388 * for the packet fall back on the network
2389 * ambient value.
2391 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2392 return;
2396 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2397 * @sk: socket
2398 * @skb: packet
2400 * Returns 0 if the packet should be delivered, an error code otherwise
2402 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2404 struct netlbl_lsm_secattr secattr;
2405 struct socket_smack *ssp = sk->sk_security;
2406 char smack[SMK_LABELLEN];
2407 char *csp;
2408 int rc;
2410 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2411 return 0;
2414 * Translate what netlabel gave us.
2416 netlbl_secattr_init(&secattr);
2418 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2419 if (rc == 0) {
2420 smack_from_secattr(&secattr, smack);
2421 csp = smack;
2422 } else
2423 csp = smack_net_ambient;
2425 netlbl_secattr_destroy(&secattr);
2428 * Receiving a packet requires that the other end
2429 * be able to write here. Read access is not required.
2430 * This is the simplist possible security model
2431 * for networking.
2433 rc = smk_access(csp, ssp->smk_in, MAY_WRITE);
2434 if (rc != 0)
2435 netlbl_skbuff_err(skb, rc, 0);
2436 return rc;
2440 * smack_socket_getpeersec_stream - pull in packet label
2441 * @sock: the socket
2442 * @optval: user's destination
2443 * @optlen: size thereof
2444 * @len: max thereoe
2446 * returns zero on success, an error code otherwise
2448 static int smack_socket_getpeersec_stream(struct socket *sock,
2449 char __user *optval,
2450 int __user *optlen, unsigned len)
2452 struct socket_smack *ssp;
2453 int slen;
2454 int rc = 0;
2456 ssp = sock->sk->sk_security;
2457 slen = strlen(ssp->smk_packet) + 1;
2459 if (slen > len)
2460 rc = -ERANGE;
2461 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2462 rc = -EFAULT;
2464 if (put_user(slen, optlen) != 0)
2465 rc = -EFAULT;
2467 return rc;
2472 * smack_socket_getpeersec_dgram - pull in packet label
2473 * @sock: the socket
2474 * @skb: packet data
2475 * @secid: pointer to where to put the secid of the packet
2477 * Sets the netlabel socket state on sk from parent
2479 static int smack_socket_getpeersec_dgram(struct socket *sock,
2480 struct sk_buff *skb, u32 *secid)
2483 struct netlbl_lsm_secattr secattr;
2484 struct sock *sk;
2485 char smack[SMK_LABELLEN];
2486 int family = PF_INET;
2487 u32 s;
2488 int rc;
2491 * Only works for families with packets.
2493 if (sock != NULL) {
2494 sk = sock->sk;
2495 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2496 return 0;
2497 family = sk->sk_family;
2500 * Translate what netlabel gave us.
2502 netlbl_secattr_init(&secattr);
2503 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2504 if (rc == 0)
2505 smack_from_secattr(&secattr, smack);
2506 netlbl_secattr_destroy(&secattr);
2509 * Give up if we couldn't get anything
2511 if (rc != 0)
2512 return rc;
2514 s = smack_to_secid(smack);
2515 if (s == 0)
2516 return -EINVAL;
2518 *secid = s;
2519 return 0;
2523 * smack_sock_graft - graft access state between two sockets
2524 * @sk: fresh sock
2525 * @parent: donor socket
2527 * Sets the netlabel socket state on sk from parent
2529 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2531 struct socket_smack *ssp;
2532 int rc;
2534 if (sk == NULL)
2535 return;
2537 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2538 return;
2540 ssp = sk->sk_security;
2541 ssp->smk_in = ssp->smk_out = current_security();
2542 ssp->smk_packet[0] = '\0';
2544 rc = smack_netlabel(sk, SMACK_CIPSO_SOCKET);
2545 if (rc != 0)
2546 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
2547 __func__, -rc);
2551 * smack_inet_conn_request - Smack access check on connect
2552 * @sk: socket involved
2553 * @skb: packet
2554 * @req: unused
2556 * Returns 0 if a task with the packet label could write to
2557 * the socket, otherwise an error code
2559 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2560 struct request_sock *req)
2562 struct netlbl_lsm_secattr skb_secattr;
2563 struct socket_smack *ssp = sk->sk_security;
2564 char smack[SMK_LABELLEN];
2565 int rc;
2567 if (skb == NULL)
2568 return -EACCES;
2570 netlbl_secattr_init(&skb_secattr);
2571 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
2572 if (rc == 0)
2573 smack_from_secattr(&skb_secattr, smack);
2574 else
2575 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2576 netlbl_secattr_destroy(&skb_secattr);
2578 * Receiving a packet requires that the other end
2579 * be able to write here. Read access is not required.
2581 * If the request is successful save the peer's label
2582 * so that SO_PEERCRED can report it.
2584 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2585 if (rc == 0)
2586 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2588 return rc;
2592 * Key management security hooks
2594 * Casey has not tested key support very heavily.
2595 * The permission check is most likely too restrictive.
2596 * If you care about keys please have a look.
2598 #ifdef CONFIG_KEYS
2601 * smack_key_alloc - Set the key security blob
2602 * @key: object
2603 * @cred: the credentials to use
2604 * @flags: unused
2606 * No allocation required
2608 * Returns 0
2610 static int smack_key_alloc(struct key *key, const struct cred *cred,
2611 unsigned long flags)
2613 key->security = cred->security;
2614 return 0;
2618 * smack_key_free - Clear the key security blob
2619 * @key: the object
2621 * Clear the blob pointer
2623 static void smack_key_free(struct key *key)
2625 key->security = NULL;
2629 * smack_key_permission - Smack access on a key
2630 * @key_ref: gets to the object
2631 * @cred: the credentials to use
2632 * @perm: unused
2634 * Return 0 if the task has read and write to the object,
2635 * an error code otherwise
2637 static int smack_key_permission(key_ref_t key_ref,
2638 const struct cred *cred, key_perm_t perm)
2640 struct key *keyp;
2642 keyp = key_ref_to_ptr(key_ref);
2643 if (keyp == NULL)
2644 return -EINVAL;
2646 * If the key hasn't been initialized give it access so that
2647 * it may do so.
2649 if (keyp->security == NULL)
2650 return 0;
2652 * This should not occur
2654 if (cred->security == NULL)
2655 return -EACCES;
2657 return smk_access(cred->security, keyp->security, MAY_READWRITE);
2659 #endif /* CONFIG_KEYS */
2662 * Smack Audit hooks
2664 * Audit requires a unique representation of each Smack specific
2665 * rule. This unique representation is used to distinguish the
2666 * object to be audited from remaining kernel objects and also
2667 * works as a glue between the audit hooks.
2669 * Since repository entries are added but never deleted, we'll use
2670 * the smack_known label address related to the given audit rule as
2671 * the needed unique representation. This also better fits the smack
2672 * model where nearly everything is a label.
2674 #ifdef CONFIG_AUDIT
2677 * smack_audit_rule_init - Initialize a smack audit rule
2678 * @field: audit rule fields given from user-space (audit.h)
2679 * @op: required testing operator (=, !=, >, <, ...)
2680 * @rulestr: smack label to be audited
2681 * @vrule: pointer to save our own audit rule representation
2683 * Prepare to audit cases where (@field @op @rulestr) is true.
2684 * The label to be audited is created if necessay.
2686 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2688 char **rule = (char **)vrule;
2689 *rule = NULL;
2691 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2692 return -EINVAL;
2694 if (op != Audit_equal && op != Audit_not_equal)
2695 return -EINVAL;
2697 *rule = smk_import(rulestr, 0);
2699 return 0;
2703 * smack_audit_rule_known - Distinguish Smack audit rules
2704 * @krule: rule of interest, in Audit kernel representation format
2706 * This is used to filter Smack rules from remaining Audit ones.
2707 * If it's proved that this rule belongs to us, the
2708 * audit_rule_match hook will be called to do the final judgement.
2710 static int smack_audit_rule_known(struct audit_krule *krule)
2712 struct audit_field *f;
2713 int i;
2715 for (i = 0; i < krule->field_count; i++) {
2716 f = &krule->fields[i];
2718 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2719 return 1;
2722 return 0;
2726 * smack_audit_rule_match - Audit given object ?
2727 * @secid: security id for identifying the object to test
2728 * @field: audit rule flags given from user-space
2729 * @op: required testing operator
2730 * @vrule: smack internal rule presentation
2731 * @actx: audit context associated with the check
2733 * The core Audit hook. It's used to take the decision of
2734 * whether to audit or not to audit a given object.
2736 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2737 struct audit_context *actx)
2739 char *smack;
2740 char *rule = vrule;
2742 if (!rule) {
2743 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2744 "Smack: missing rule\n");
2745 return -ENOENT;
2748 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2749 return 0;
2751 smack = smack_from_secid(secid);
2754 * No need to do string comparisons. If a match occurs,
2755 * both pointers will point to the same smack_known
2756 * label.
2758 if (op == Audit_equal)
2759 return (rule == smack);
2760 if (op == Audit_not_equal)
2761 return (rule != smack);
2763 return 0;
2767 * smack_audit_rule_free - free smack rule representation
2768 * @vrule: rule to be freed.
2770 * No memory was allocated.
2772 static void smack_audit_rule_free(void *vrule)
2774 /* No-op */
2777 #endif /* CONFIG_AUDIT */
2780 * smack_secid_to_secctx - return the smack label for a secid
2781 * @secid: incoming integer
2782 * @secdata: destination
2783 * @seclen: how long it is
2785 * Exists for networking code.
2787 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2789 char *sp = smack_from_secid(secid);
2791 *secdata = sp;
2792 *seclen = strlen(sp);
2793 return 0;
2797 * smack_secctx_to_secid - return the secid for a smack label
2798 * @secdata: smack label
2799 * @seclen: how long result is
2800 * @secid: outgoing integer
2802 * Exists for audit and networking code.
2804 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
2806 *secid = smack_to_secid(secdata);
2807 return 0;
2811 * smack_release_secctx - don't do anything.
2812 * @key_ref: unused
2813 * @context: unused
2814 * @perm: unused
2816 * Exists to make sure nothing gets done, and properly
2818 static void smack_release_secctx(char *secdata, u32 seclen)
2822 struct security_operations smack_ops = {
2823 .name = "smack",
2825 .ptrace_may_access = smack_ptrace_may_access,
2826 .ptrace_traceme = smack_ptrace_traceme,
2827 .capget = cap_capget,
2828 .capset = cap_capset,
2829 .capable = cap_capable,
2830 .syslog = smack_syslog,
2831 .settime = cap_settime,
2832 .vm_enough_memory = cap_vm_enough_memory,
2834 .bprm_set_creds = cap_bprm_set_creds,
2835 .bprm_secureexec = cap_bprm_secureexec,
2837 .sb_alloc_security = smack_sb_alloc_security,
2838 .sb_free_security = smack_sb_free_security,
2839 .sb_copy_data = smack_sb_copy_data,
2840 .sb_kern_mount = smack_sb_kern_mount,
2841 .sb_statfs = smack_sb_statfs,
2842 .sb_mount = smack_sb_mount,
2843 .sb_umount = smack_sb_umount,
2845 .inode_alloc_security = smack_inode_alloc_security,
2846 .inode_free_security = smack_inode_free_security,
2847 .inode_init_security = smack_inode_init_security,
2848 .inode_link = smack_inode_link,
2849 .inode_unlink = smack_inode_unlink,
2850 .inode_rmdir = smack_inode_rmdir,
2851 .inode_rename = smack_inode_rename,
2852 .inode_permission = smack_inode_permission,
2853 .inode_setattr = smack_inode_setattr,
2854 .inode_getattr = smack_inode_getattr,
2855 .inode_setxattr = smack_inode_setxattr,
2856 .inode_post_setxattr = smack_inode_post_setxattr,
2857 .inode_getxattr = smack_inode_getxattr,
2858 .inode_removexattr = smack_inode_removexattr,
2859 .inode_need_killpriv = cap_inode_need_killpriv,
2860 .inode_killpriv = cap_inode_killpriv,
2861 .inode_getsecurity = smack_inode_getsecurity,
2862 .inode_setsecurity = smack_inode_setsecurity,
2863 .inode_listsecurity = smack_inode_listsecurity,
2864 .inode_getsecid = smack_inode_getsecid,
2866 .file_permission = smack_file_permission,
2867 .file_alloc_security = smack_file_alloc_security,
2868 .file_free_security = smack_file_free_security,
2869 .file_ioctl = smack_file_ioctl,
2870 .file_lock = smack_file_lock,
2871 .file_fcntl = smack_file_fcntl,
2872 .file_set_fowner = smack_file_set_fowner,
2873 .file_send_sigiotask = smack_file_send_sigiotask,
2874 .file_receive = smack_file_receive,
2876 .cred_free = smack_cred_free,
2877 .cred_prepare = smack_cred_prepare,
2878 .cred_commit = smack_cred_commit,
2879 .kernel_act_as = smack_kernel_act_as,
2880 .kernel_create_files_as = smack_kernel_create_files_as,
2881 .task_fix_setuid = cap_task_fix_setuid,
2882 .task_setpgid = smack_task_setpgid,
2883 .task_getpgid = smack_task_getpgid,
2884 .task_getsid = smack_task_getsid,
2885 .task_getsecid = smack_task_getsecid,
2886 .task_setnice = smack_task_setnice,
2887 .task_setioprio = smack_task_setioprio,
2888 .task_getioprio = smack_task_getioprio,
2889 .task_setscheduler = smack_task_setscheduler,
2890 .task_getscheduler = smack_task_getscheduler,
2891 .task_movememory = smack_task_movememory,
2892 .task_kill = smack_task_kill,
2893 .task_wait = smack_task_wait,
2894 .task_to_inode = smack_task_to_inode,
2895 .task_prctl = cap_task_prctl,
2897 .ipc_permission = smack_ipc_permission,
2898 .ipc_getsecid = smack_ipc_getsecid,
2900 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
2901 .msg_msg_free_security = smack_msg_msg_free_security,
2903 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
2904 .msg_queue_free_security = smack_msg_queue_free_security,
2905 .msg_queue_associate = smack_msg_queue_associate,
2906 .msg_queue_msgctl = smack_msg_queue_msgctl,
2907 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
2908 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
2910 .shm_alloc_security = smack_shm_alloc_security,
2911 .shm_free_security = smack_shm_free_security,
2912 .shm_associate = smack_shm_associate,
2913 .shm_shmctl = smack_shm_shmctl,
2914 .shm_shmat = smack_shm_shmat,
2916 .sem_alloc_security = smack_sem_alloc_security,
2917 .sem_free_security = smack_sem_free_security,
2918 .sem_associate = smack_sem_associate,
2919 .sem_semctl = smack_sem_semctl,
2920 .sem_semop = smack_sem_semop,
2922 .netlink_send = cap_netlink_send,
2923 .netlink_recv = cap_netlink_recv,
2925 .d_instantiate = smack_d_instantiate,
2927 .getprocattr = smack_getprocattr,
2928 .setprocattr = smack_setprocattr,
2930 .unix_stream_connect = smack_unix_stream_connect,
2931 .unix_may_send = smack_unix_may_send,
2933 .socket_post_create = smack_socket_post_create,
2934 .socket_connect = smack_socket_connect,
2935 .socket_sendmsg = smack_socket_sendmsg,
2936 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
2937 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
2938 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
2939 .sk_alloc_security = smack_sk_alloc_security,
2940 .sk_free_security = smack_sk_free_security,
2941 .sock_graft = smack_sock_graft,
2942 .inet_conn_request = smack_inet_conn_request,
2944 /* key management security hooks */
2945 #ifdef CONFIG_KEYS
2946 .key_alloc = smack_key_alloc,
2947 .key_free = smack_key_free,
2948 .key_permission = smack_key_permission,
2949 #endif /* CONFIG_KEYS */
2951 /* Audit hooks */
2952 #ifdef CONFIG_AUDIT
2953 .audit_rule_init = smack_audit_rule_init,
2954 .audit_rule_known = smack_audit_rule_known,
2955 .audit_rule_match = smack_audit_rule_match,
2956 .audit_rule_free = smack_audit_rule_free,
2957 #endif /* CONFIG_AUDIT */
2959 .secid_to_secctx = smack_secid_to_secctx,
2960 .secctx_to_secid = smack_secctx_to_secid,
2961 .release_secctx = smack_release_secctx,
2965 * smack_init - initialize the smack system
2967 * Returns 0
2969 static __init int smack_init(void)
2971 struct cred *cred;
2973 if (!security_module_enable(&smack_ops))
2974 return 0;
2976 printk(KERN_INFO "Smack: Initializing.\n");
2979 * Set the security state for the initial task.
2981 cred = (struct cred *) current->cred;
2982 cred->security = &smack_known_floor.smk_known;
2985 * Initialize locks
2987 spin_lock_init(&smack_known_huh.smk_cipsolock);
2988 spin_lock_init(&smack_known_hat.smk_cipsolock);
2989 spin_lock_init(&smack_known_star.smk_cipsolock);
2990 spin_lock_init(&smack_known_floor.smk_cipsolock);
2991 spin_lock_init(&smack_known_invalid.smk_cipsolock);
2994 * Register with LSM
2996 if (register_security(&smack_ops))
2997 panic("smack: Unable to register with kernel.\n");
2999 return 0;
3003 * Smack requires early initialization in order to label
3004 * all processes and objects when they are created.
3006 security_initcall(smack_init);