e100: e100_phy_init() isolates selected PHY, causes 10 second boot delay
[linux-2.6.git] / security / smack / smack_lsm.c
blobc33b6bb9b6dd018de224c46c11c76aad314a44c9
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>
10 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
11 * Paul Moore <paul.moore@hp.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2,
15 * as published by the Free Software Foundation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
22 #include <linux/ext2_fs.h>
23 #include <linux/kd.h>
24 #include <asm/ioctls.h>
25 #include <linux/ip.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/mutex.h>
29 #include <linux/pipe_fs_i.h>
30 #include <net/netlabel.h>
31 #include <net/cipso_ipv4.h>
32 #include <linux/audit.h>
33 #include <linux/magic.h>
34 #include "smack.h"
36 #define task_security(task) (task_cred_xxx((task), security))
38 /**
39 * smk_fetch - Fetch the smack label from a file.
40 * @ip: a pointer to the inode
41 * @dp: a pointer to the dentry
43 * Returns a pointer to the master list entry for the Smack label
44 * or NULL if there was no label to fetch.
46 static char *smk_fetch(struct inode *ip, struct dentry *dp)
48 int rc;
49 char in[SMK_LABELLEN];
51 if (ip->i_op->getxattr == NULL)
52 return NULL;
54 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
55 if (rc < 0)
56 return NULL;
58 return smk_import(in, rc);
61 /**
62 * new_inode_smack - allocate an inode security blob
63 * @smack: a pointer to the Smack label to use in the blob
65 * Returns the new blob or NULL if there's no memory available
67 struct inode_smack *new_inode_smack(char *smack)
69 struct inode_smack *isp;
71 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
72 if (isp == NULL)
73 return NULL;
75 isp->smk_inode = smack;
76 isp->smk_flags = 0;
77 mutex_init(&isp->smk_lock);
79 return isp;
83 * LSM hooks.
84 * We he, that is fun!
87 /**
88 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
89 * @ctp: child task pointer
90 * @mode: ptrace attachment mode
92 * Returns 0 if access is OK, an error code otherwise
94 * Do the capability checks, and require read and write.
96 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
98 int rc;
99 struct smk_audit_info ad;
100 char *sp, *tsp;
102 rc = cap_ptrace_access_check(ctp, mode);
103 if (rc != 0)
104 return rc;
106 sp = current_security();
107 tsp = task_security(ctp);
108 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
109 smk_ad_setfield_u_tsk(&ad, ctp);
111 /* we won't log here, because rc can be overriden */
112 rc = smk_access(sp, tsp, MAY_READWRITE, NULL);
113 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
114 rc = 0;
116 smack_log(sp, tsp, MAY_READWRITE, rc, &ad);
117 return rc;
121 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
122 * @ptp: parent task pointer
124 * Returns 0 if access is OK, an error code otherwise
126 * Do the capability checks, and require read and write.
128 static int smack_ptrace_traceme(struct task_struct *ptp)
130 int rc;
131 struct smk_audit_info ad;
132 char *sp, *tsp;
134 rc = cap_ptrace_traceme(ptp);
135 if (rc != 0)
136 return rc;
138 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
139 smk_ad_setfield_u_tsk(&ad, ptp);
141 sp = current_security();
142 tsp = task_security(ptp);
143 /* we won't log here, because rc can be overriden */
144 rc = smk_access(tsp, sp, MAY_READWRITE, NULL);
145 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
146 rc = 0;
148 smack_log(tsp, sp, MAY_READWRITE, rc, &ad);
149 return rc;
153 * smack_syslog - Smack approval on syslog
154 * @type: message type
156 * Require that the task has the floor label
158 * Returns 0 on success, error code otherwise.
160 static int smack_syslog(int type)
162 int rc;
163 char *sp = current_security();
165 rc = cap_syslog(type);
166 if (rc != 0)
167 return rc;
169 if (capable(CAP_MAC_OVERRIDE))
170 return 0;
172 if (sp != smack_known_floor.smk_known)
173 rc = -EACCES;
175 return rc;
180 * Superblock Hooks.
184 * smack_sb_alloc_security - allocate a superblock blob
185 * @sb: the superblock getting the blob
187 * Returns 0 on success or -ENOMEM on error.
189 static int smack_sb_alloc_security(struct super_block *sb)
191 struct superblock_smack *sbsp;
193 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
195 if (sbsp == NULL)
196 return -ENOMEM;
198 sbsp->smk_root = smack_known_floor.smk_known;
199 sbsp->smk_default = smack_known_floor.smk_known;
200 sbsp->smk_floor = smack_known_floor.smk_known;
201 sbsp->smk_hat = smack_known_hat.smk_known;
202 sbsp->smk_initialized = 0;
203 spin_lock_init(&sbsp->smk_sblock);
205 sb->s_security = sbsp;
207 return 0;
211 * smack_sb_free_security - free a superblock blob
212 * @sb: the superblock getting the blob
215 static void smack_sb_free_security(struct super_block *sb)
217 kfree(sb->s_security);
218 sb->s_security = NULL;
222 * smack_sb_copy_data - copy mount options data for processing
223 * @orig: where to start
224 * @smackopts: mount options string
226 * Returns 0 on success or -ENOMEM on error.
228 * Copy the Smack specific mount options out of the mount
229 * options list.
231 static int smack_sb_copy_data(char *orig, char *smackopts)
233 char *cp, *commap, *otheropts, *dp;
235 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
236 if (otheropts == NULL)
237 return -ENOMEM;
239 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
240 if (strstr(cp, SMK_FSDEFAULT) == cp)
241 dp = smackopts;
242 else if (strstr(cp, SMK_FSFLOOR) == cp)
243 dp = smackopts;
244 else if (strstr(cp, SMK_FSHAT) == cp)
245 dp = smackopts;
246 else if (strstr(cp, SMK_FSROOT) == cp)
247 dp = smackopts;
248 else
249 dp = otheropts;
251 commap = strchr(cp, ',');
252 if (commap != NULL)
253 *commap = '\0';
255 if (*dp != '\0')
256 strcat(dp, ",");
257 strcat(dp, cp);
260 strcpy(orig, otheropts);
261 free_page((unsigned long)otheropts);
263 return 0;
267 * smack_sb_kern_mount - Smack specific mount processing
268 * @sb: the file system superblock
269 * @flags: the mount flags
270 * @data: the smack mount options
272 * Returns 0 on success, an error code on failure
274 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
276 struct dentry *root = sb->s_root;
277 struct inode *inode = root->d_inode;
278 struct superblock_smack *sp = sb->s_security;
279 struct inode_smack *isp;
280 char *op;
281 char *commap;
282 char *nsp;
284 spin_lock(&sp->smk_sblock);
285 if (sp->smk_initialized != 0) {
286 spin_unlock(&sp->smk_sblock);
287 return 0;
289 sp->smk_initialized = 1;
290 spin_unlock(&sp->smk_sblock);
292 for (op = data; op != NULL; op = commap) {
293 commap = strchr(op, ',');
294 if (commap != NULL)
295 *commap++ = '\0';
297 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
298 op += strlen(SMK_FSHAT);
299 nsp = smk_import(op, 0);
300 if (nsp != NULL)
301 sp->smk_hat = nsp;
302 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
303 op += strlen(SMK_FSFLOOR);
304 nsp = smk_import(op, 0);
305 if (nsp != NULL)
306 sp->smk_floor = nsp;
307 } else if (strncmp(op, SMK_FSDEFAULT,
308 strlen(SMK_FSDEFAULT)) == 0) {
309 op += strlen(SMK_FSDEFAULT);
310 nsp = smk_import(op, 0);
311 if (nsp != NULL)
312 sp->smk_default = nsp;
313 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
314 op += strlen(SMK_FSROOT);
315 nsp = smk_import(op, 0);
316 if (nsp != NULL)
317 sp->smk_root = nsp;
322 * Initialize the root inode.
324 isp = inode->i_security;
325 if (isp == NULL)
326 inode->i_security = new_inode_smack(sp->smk_root);
327 else
328 isp->smk_inode = sp->smk_root;
330 return 0;
334 * smack_sb_statfs - Smack check on statfs
335 * @dentry: identifies the file system in question
337 * Returns 0 if current can read the floor of the filesystem,
338 * and error code otherwise
340 static int smack_sb_statfs(struct dentry *dentry)
342 struct superblock_smack *sbp = dentry->d_sb->s_security;
343 int rc;
344 struct smk_audit_info ad;
346 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
347 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
349 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
350 return rc;
354 * smack_sb_mount - Smack check for mounting
355 * @dev_name: unused
356 * @path: mount point
357 * @type: unused
358 * @flags: unused
359 * @data: unused
361 * Returns 0 if current can write the floor of the filesystem
362 * being mounted on, an error code otherwise.
364 static int smack_sb_mount(char *dev_name, struct path *path,
365 char *type, unsigned long flags, void *data)
367 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
368 struct smk_audit_info ad;
370 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
371 smk_ad_setfield_u_fs_path(&ad, *path);
373 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
377 * smack_sb_umount - Smack check for unmounting
378 * @mnt: file system to unmount
379 * @flags: unused
381 * Returns 0 if current can write the floor of the filesystem
382 * being unmounted, an error code otherwise.
384 static int smack_sb_umount(struct vfsmount *mnt, int flags)
386 struct superblock_smack *sbp;
387 struct smk_audit_info ad;
389 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
390 smk_ad_setfield_u_fs_path_dentry(&ad, mnt->mnt_mountpoint);
391 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
393 sbp = mnt->mnt_sb->s_security;
394 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
398 * Inode hooks
402 * smack_inode_alloc_security - allocate an inode blob
403 * @inode: the inode in need of a blob
405 * Returns 0 if it gets a blob, -ENOMEM otherwise
407 static int smack_inode_alloc_security(struct inode *inode)
409 inode->i_security = new_inode_smack(current_security());
410 if (inode->i_security == NULL)
411 return -ENOMEM;
412 return 0;
416 * smack_inode_free_security - free an inode blob
417 * @inode: the inode with a blob
419 * Clears the blob pointer in inode
421 static void smack_inode_free_security(struct inode *inode)
423 kfree(inode->i_security);
424 inode->i_security = NULL;
428 * smack_inode_init_security - copy out the smack from an inode
429 * @inode: the inode
430 * @dir: unused
431 * @name: where to put the attribute name
432 * @value: where to put the attribute value
433 * @len: where to put the length of the attribute
435 * Returns 0 if it all works out, -ENOMEM if there's no memory
437 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
438 char **name, void **value, size_t *len)
440 char *isp = smk_of_inode(inode);
442 if (name) {
443 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
444 if (*name == NULL)
445 return -ENOMEM;
448 if (value) {
449 *value = kstrdup(isp, GFP_KERNEL);
450 if (*value == NULL)
451 return -ENOMEM;
454 if (len)
455 *len = strlen(isp) + 1;
457 return 0;
461 * smack_inode_link - Smack check on link
462 * @old_dentry: the existing object
463 * @dir: unused
464 * @new_dentry: the new object
466 * Returns 0 if access is permitted, an error code otherwise
468 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
469 struct dentry *new_dentry)
471 char *isp;
472 struct smk_audit_info ad;
473 int rc;
475 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
476 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
478 isp = smk_of_inode(old_dentry->d_inode);
479 rc = smk_curacc(isp, MAY_WRITE, &ad);
481 if (rc == 0 && new_dentry->d_inode != NULL) {
482 isp = smk_of_inode(new_dentry->d_inode);
483 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
484 rc = smk_curacc(isp, MAY_WRITE, &ad);
487 return rc;
491 * smack_inode_unlink - Smack check on inode deletion
492 * @dir: containing directory object
493 * @dentry: file to unlink
495 * Returns 0 if current can write the containing directory
496 * and the object, error code otherwise
498 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
500 struct inode *ip = dentry->d_inode;
501 struct smk_audit_info ad;
502 int rc;
504 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
505 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
508 * You need write access to the thing you're unlinking
510 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
511 if (rc == 0) {
513 * You also need write access to the containing directory
515 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
516 smk_ad_setfield_u_fs_inode(&ad, dir);
517 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
519 return rc;
523 * smack_inode_rmdir - Smack check on directory deletion
524 * @dir: containing directory object
525 * @dentry: directory to unlink
527 * Returns 0 if current can write the containing directory
528 * and the directory, error code otherwise
530 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
532 struct smk_audit_info ad;
533 int rc;
535 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
536 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
539 * You need write access to the thing you're removing
541 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
542 if (rc == 0) {
544 * You also need write access to the containing directory
546 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
547 smk_ad_setfield_u_fs_inode(&ad, dir);
548 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
551 return rc;
555 * smack_inode_rename - Smack check on rename
556 * @old_inode: the old directory
557 * @old_dentry: unused
558 * @new_inode: the new directory
559 * @new_dentry: unused
561 * Read and write access is required on both the old and
562 * new directories.
564 * Returns 0 if access is permitted, an error code otherwise
566 static int smack_inode_rename(struct inode *old_inode,
567 struct dentry *old_dentry,
568 struct inode *new_inode,
569 struct dentry *new_dentry)
571 int rc;
572 char *isp;
573 struct smk_audit_info ad;
575 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
576 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
578 isp = smk_of_inode(old_dentry->d_inode);
579 rc = smk_curacc(isp, MAY_READWRITE, &ad);
581 if (rc == 0 && new_dentry->d_inode != NULL) {
582 isp = smk_of_inode(new_dentry->d_inode);
583 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
584 rc = smk_curacc(isp, MAY_READWRITE, &ad);
586 return rc;
590 * smack_inode_permission - Smack version of permission()
591 * @inode: the inode in question
592 * @mask: the access requested
594 * This is the important Smack hook.
596 * Returns 0 if access is permitted, -EACCES otherwise
598 static int smack_inode_permission(struct inode *inode, int mask)
600 struct smk_audit_info ad;
602 * No permission to check. Existence test. Yup, it's there.
604 if (mask == 0)
605 return 0;
606 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
607 smk_ad_setfield_u_fs_inode(&ad, inode);
608 return smk_curacc(smk_of_inode(inode), mask, &ad);
612 * smack_inode_setattr - Smack check for setting attributes
613 * @dentry: the object
614 * @iattr: for the force flag
616 * Returns 0 if access is permitted, an error code otherwise
618 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
620 struct smk_audit_info ad;
622 * Need to allow for clearing the setuid bit.
624 if (iattr->ia_valid & ATTR_FORCE)
625 return 0;
626 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
627 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
629 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
633 * smack_inode_getattr - Smack check for getting attributes
634 * @mnt: unused
635 * @dentry: the object
637 * Returns 0 if access is permitted, an error code otherwise
639 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
641 struct smk_audit_info ad;
643 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
644 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
645 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
646 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
650 * smack_inode_setxattr - Smack check for setting xattrs
651 * @dentry: the object
652 * @name: name of the attribute
653 * @value: unused
654 * @size: unused
655 * @flags: unused
657 * This protects the Smack attribute explicitly.
659 * Returns 0 if access is permitted, an error code otherwise
661 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
662 const void *value, size_t size, int flags)
664 struct smk_audit_info ad;
665 int rc = 0;
667 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
668 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
669 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
670 if (!capable(CAP_MAC_ADMIN))
671 rc = -EPERM;
673 * check label validity here so import wont fail on
674 * post_setxattr
676 if (size == 0 || size >= SMK_LABELLEN ||
677 smk_import(value, size) == NULL)
678 rc = -EINVAL;
679 } else
680 rc = cap_inode_setxattr(dentry, name, value, size, flags);
682 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
683 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
685 if (rc == 0)
686 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
688 return rc;
692 * smack_inode_post_setxattr - Apply the Smack update approved above
693 * @dentry: object
694 * @name: attribute name
695 * @value: attribute value
696 * @size: attribute size
697 * @flags: unused
699 * Set the pointer in the inode blob to the entry found
700 * in the master label list.
702 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
703 const void *value, size_t size, int flags)
705 struct inode_smack *isp;
706 char *nsp;
709 * Not SMACK
711 if (strcmp(name, XATTR_NAME_SMACK))
712 return;
714 isp = dentry->d_inode->i_security;
717 * No locking is done here. This is a pointer
718 * assignment.
720 nsp = smk_import(value, size);
721 if (nsp != NULL)
722 isp->smk_inode = nsp;
723 else
724 isp->smk_inode = smack_known_invalid.smk_known;
726 return;
730 * smack_inode_getxattr - Smack check on getxattr
731 * @dentry: the object
732 * @name: unused
734 * Returns 0 if access is permitted, an error code otherwise
736 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
738 struct smk_audit_info ad;
740 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
741 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
743 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
747 * smack_inode_removexattr - Smack check on removexattr
748 * @dentry: the object
749 * @name: name of the attribute
751 * Removing the Smack attribute requires CAP_MAC_ADMIN
753 * Returns 0 if access is permitted, an error code otherwise
755 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
757 struct smk_audit_info ad;
758 int rc = 0;
760 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
761 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
762 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
763 if (!capable(CAP_MAC_ADMIN))
764 rc = -EPERM;
765 } else
766 rc = cap_inode_removexattr(dentry, name);
768 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
769 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
770 if (rc == 0)
771 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
773 return rc;
777 * smack_inode_getsecurity - get smack xattrs
778 * @inode: the object
779 * @name: attribute name
780 * @buffer: where to put the result
781 * @alloc: unused
783 * Returns the size of the attribute or an error code
785 static int smack_inode_getsecurity(const struct inode *inode,
786 const char *name, void **buffer,
787 bool alloc)
789 struct socket_smack *ssp;
790 struct socket *sock;
791 struct super_block *sbp;
792 struct inode *ip = (struct inode *)inode;
793 char *isp;
794 int ilen;
795 int rc = 0;
797 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
798 isp = smk_of_inode(inode);
799 ilen = strlen(isp) + 1;
800 *buffer = isp;
801 return ilen;
805 * The rest of the Smack xattrs are only on sockets.
807 sbp = ip->i_sb;
808 if (sbp->s_magic != SOCKFS_MAGIC)
809 return -EOPNOTSUPP;
811 sock = SOCKET_I(ip);
812 if (sock == NULL || sock->sk == NULL)
813 return -EOPNOTSUPP;
815 ssp = sock->sk->sk_security;
817 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
818 isp = ssp->smk_in;
819 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
820 isp = ssp->smk_out;
821 else
822 return -EOPNOTSUPP;
824 ilen = strlen(isp) + 1;
825 if (rc == 0) {
826 *buffer = isp;
827 rc = ilen;
830 return rc;
835 * smack_inode_listsecurity - list the Smack attributes
836 * @inode: the object
837 * @buffer: where they go
838 * @buffer_size: size of buffer
840 * Returns 0 on success, -EINVAL otherwise
842 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
843 size_t buffer_size)
845 int len = strlen(XATTR_NAME_SMACK);
847 if (buffer != NULL && len <= buffer_size) {
848 memcpy(buffer, XATTR_NAME_SMACK, len);
849 return len;
851 return -EINVAL;
855 * smack_inode_getsecid - Extract inode's security id
856 * @inode: inode to extract the info from
857 * @secid: where result will be saved
859 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
861 struct inode_smack *isp = inode->i_security;
863 *secid = smack_to_secid(isp->smk_inode);
867 * File Hooks
871 * smack_file_permission - Smack check on file operations
872 * @file: unused
873 * @mask: unused
875 * Returns 0
877 * Should access checks be done on each read or write?
878 * UNICOS and SELinux say yes.
879 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
881 * I'll say no for now. Smack does not do the frequent
882 * label changing that SELinux does.
884 static int smack_file_permission(struct file *file, int mask)
886 return 0;
890 * smack_file_alloc_security - assign a file security blob
891 * @file: the object
893 * The security blob for a file is a pointer to the master
894 * label list, so no allocation is done.
896 * Returns 0
898 static int smack_file_alloc_security(struct file *file)
900 file->f_security = current_security();
901 return 0;
905 * smack_file_free_security - clear a file security blob
906 * @file: the object
908 * The security blob for a file is a pointer to the master
909 * label list, so no memory is freed.
911 static void smack_file_free_security(struct file *file)
913 file->f_security = NULL;
917 * smack_file_ioctl - Smack check on ioctls
918 * @file: the object
919 * @cmd: what to do
920 * @arg: unused
922 * Relies heavily on the correct use of the ioctl command conventions.
924 * Returns 0 if allowed, error code otherwise
926 static int smack_file_ioctl(struct file *file, unsigned int cmd,
927 unsigned long arg)
929 int rc = 0;
930 struct smk_audit_info ad;
932 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
933 smk_ad_setfield_u_fs_path(&ad, file->f_path);
935 if (_IOC_DIR(cmd) & _IOC_WRITE)
936 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
938 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
939 rc = smk_curacc(file->f_security, MAY_READ, &ad);
941 return rc;
945 * smack_file_lock - Smack check on file locking
946 * @file: the object
947 * @cmd: unused
949 * Returns 0 if current has write access, error code otherwise
951 static int smack_file_lock(struct file *file, unsigned int cmd)
953 struct smk_audit_info ad;
955 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
956 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
957 return smk_curacc(file->f_security, MAY_WRITE, &ad);
961 * smack_file_fcntl - Smack check on fcntl
962 * @file: the object
963 * @cmd: what action to check
964 * @arg: unused
966 * Returns 0 if current has access, error code otherwise
968 static int smack_file_fcntl(struct file *file, unsigned int cmd,
969 unsigned long arg)
971 struct smk_audit_info ad;
972 int rc;
974 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
975 smk_ad_setfield_u_fs_path(&ad, file->f_path);
977 switch (cmd) {
978 case F_DUPFD:
979 case F_GETFD:
980 case F_GETFL:
981 case F_GETLK:
982 case F_GETOWN:
983 case F_GETSIG:
984 rc = smk_curacc(file->f_security, MAY_READ, &ad);
985 break;
986 case F_SETFD:
987 case F_SETFL:
988 case F_SETLK:
989 case F_SETLKW:
990 case F_SETOWN:
991 case F_SETSIG:
992 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
993 break;
994 default:
995 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
998 return rc;
1002 * smack_file_set_fowner - set the file security blob value
1003 * @file: object in question
1005 * Returns 0
1006 * Further research may be required on this one.
1008 static int smack_file_set_fowner(struct file *file)
1010 file->f_security = current_security();
1011 return 0;
1015 * smack_file_send_sigiotask - Smack on sigio
1016 * @tsk: The target task
1017 * @fown: the object the signal come from
1018 * @signum: unused
1020 * Allow a privileged task to get signals even if it shouldn't
1022 * Returns 0 if a subject with the object's smack could
1023 * write to the task, an error code otherwise.
1025 static int smack_file_send_sigiotask(struct task_struct *tsk,
1026 struct fown_struct *fown, int signum)
1028 struct file *file;
1029 int rc;
1030 char *tsp = tsk->cred->security;
1031 struct smk_audit_info ad;
1034 * struct fown_struct is never outside the context of a struct file
1036 file = container_of(fown, struct file, f_owner);
1037 /* we don't log here as rc can be overriden */
1038 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
1039 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1040 rc = 0;
1042 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1043 smk_ad_setfield_u_tsk(&ad, tsk);
1044 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
1045 return rc;
1049 * smack_file_receive - Smack file receive check
1050 * @file: the object
1052 * Returns 0 if current has access, error code otherwise
1054 static int smack_file_receive(struct file *file)
1056 int may = 0;
1057 struct smk_audit_info ad;
1059 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1060 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1062 * This code relies on bitmasks.
1064 if (file->f_mode & FMODE_READ)
1065 may = MAY_READ;
1066 if (file->f_mode & FMODE_WRITE)
1067 may |= MAY_WRITE;
1069 return smk_curacc(file->f_security, may, &ad);
1073 * Task hooks
1077 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1078 * @new: the new credentials
1079 * @gfp: the atomicity of any memory allocations
1081 * Prepare a blank set of credentials for modification. This must allocate all
1082 * the memory the LSM module might require such that cred_transfer() can
1083 * complete without error.
1085 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1087 cred->security = NULL;
1088 return 0;
1093 * smack_cred_free - "free" task-level security credentials
1094 * @cred: the credentials in question
1096 * Smack isn't using copies of blobs. Everyone
1097 * points to an immutable list. The blobs never go away.
1098 * There is no leak here.
1100 static void smack_cred_free(struct cred *cred)
1102 cred->security = NULL;
1106 * smack_cred_prepare - prepare new set of credentials for modification
1107 * @new: the new credentials
1108 * @old: the original credentials
1109 * @gfp: the atomicity of any memory allocations
1111 * Prepare a new set of credentials for modification.
1113 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1114 gfp_t gfp)
1116 new->security = old->security;
1117 return 0;
1121 * smack_cred_commit - commit new credentials
1122 * @new: the new credentials
1123 * @old: the original credentials
1125 static void smack_cred_commit(struct cred *new, const struct cred *old)
1130 * smack_cred_transfer - Transfer the old credentials to the new credentials
1131 * @new: the new credentials
1132 * @old: the original credentials
1134 * Fill in a set of blank credentials from another set of credentials.
1136 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1138 new->security = old->security;
1142 * smack_kernel_act_as - Set the subjective context in a set of credentials
1143 * @new: points to the set of credentials to be modified.
1144 * @secid: specifies the security ID to be set
1146 * Set the security data for a kernel service.
1148 static int smack_kernel_act_as(struct cred *new, u32 secid)
1150 char *smack = smack_from_secid(secid);
1152 if (smack == NULL)
1153 return -EINVAL;
1155 new->security = smack;
1156 return 0;
1160 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1161 * @new: points to the set of credentials to be modified
1162 * @inode: points to the inode to use as a reference
1164 * Set the file creation context in a set of credentials to the same
1165 * as the objective context of the specified inode
1167 static int smack_kernel_create_files_as(struct cred *new,
1168 struct inode *inode)
1170 struct inode_smack *isp = inode->i_security;
1172 new->security = isp->smk_inode;
1173 return 0;
1177 * smk_curacc_on_task - helper to log task related access
1178 * @p: the task object
1179 * @access : the access requested
1181 * Return 0 if access is permitted
1183 static int smk_curacc_on_task(struct task_struct *p, int access)
1185 struct smk_audit_info ad;
1187 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1188 smk_ad_setfield_u_tsk(&ad, p);
1189 return smk_curacc(task_security(p), access, &ad);
1193 * smack_task_setpgid - Smack check on setting pgid
1194 * @p: the task object
1195 * @pgid: unused
1197 * Return 0 if write access is permitted
1199 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1201 return smk_curacc_on_task(p, MAY_WRITE);
1205 * smack_task_getpgid - Smack access check for getpgid
1206 * @p: the object task
1208 * Returns 0 if current can read the object task, error code otherwise
1210 static int smack_task_getpgid(struct task_struct *p)
1212 return smk_curacc_on_task(p, MAY_READ);
1216 * smack_task_getsid - Smack access check for getsid
1217 * @p: the object task
1219 * Returns 0 if current can read the object task, error code otherwise
1221 static int smack_task_getsid(struct task_struct *p)
1223 return smk_curacc_on_task(p, MAY_READ);
1227 * smack_task_getsecid - get the secid of the task
1228 * @p: the object task
1229 * @secid: where to put the result
1231 * Sets the secid to contain a u32 version of the smack label.
1233 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1235 *secid = smack_to_secid(task_security(p));
1239 * smack_task_setnice - Smack check on setting nice
1240 * @p: the task object
1241 * @nice: unused
1243 * Return 0 if write access is permitted
1245 static int smack_task_setnice(struct task_struct *p, int nice)
1247 int rc;
1249 rc = cap_task_setnice(p, nice);
1250 if (rc == 0)
1251 rc = smk_curacc_on_task(p, MAY_WRITE);
1252 return rc;
1256 * smack_task_setioprio - Smack check on setting ioprio
1257 * @p: the task object
1258 * @ioprio: unused
1260 * Return 0 if write access is permitted
1262 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1264 int rc;
1266 rc = cap_task_setioprio(p, ioprio);
1267 if (rc == 0)
1268 rc = smk_curacc_on_task(p, MAY_WRITE);
1269 return rc;
1273 * smack_task_getioprio - Smack check on reading ioprio
1274 * @p: the task object
1276 * Return 0 if read access is permitted
1278 static int smack_task_getioprio(struct task_struct *p)
1280 return smk_curacc_on_task(p, MAY_READ);
1284 * smack_task_setscheduler - Smack check on setting scheduler
1285 * @p: the task object
1286 * @policy: unused
1287 * @lp: unused
1289 * Return 0 if read access is permitted
1291 static int smack_task_setscheduler(struct task_struct *p, int policy,
1292 struct sched_param *lp)
1294 int rc;
1296 rc = cap_task_setscheduler(p, policy, lp);
1297 if (rc == 0)
1298 rc = smk_curacc_on_task(p, MAY_WRITE);
1299 return rc;
1303 * smack_task_getscheduler - Smack check on reading scheduler
1304 * @p: the task object
1306 * Return 0 if read access is permitted
1308 static int smack_task_getscheduler(struct task_struct *p)
1310 return smk_curacc_on_task(p, MAY_READ);
1314 * smack_task_movememory - Smack check on moving memory
1315 * @p: the task object
1317 * Return 0 if write access is permitted
1319 static int smack_task_movememory(struct task_struct *p)
1321 return smk_curacc_on_task(p, MAY_WRITE);
1325 * smack_task_kill - Smack check on signal delivery
1326 * @p: the task object
1327 * @info: unused
1328 * @sig: unused
1329 * @secid: identifies the smack to use in lieu of current's
1331 * Return 0 if write access is permitted
1333 * The secid behavior is an artifact of an SELinux hack
1334 * in the USB code. Someday it may go away.
1336 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1337 int sig, u32 secid)
1339 struct smk_audit_info ad;
1341 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1342 smk_ad_setfield_u_tsk(&ad, p);
1344 * Sending a signal requires that the sender
1345 * can write the receiver.
1347 if (secid == 0)
1348 return smk_curacc(task_security(p), MAY_WRITE, &ad);
1350 * If the secid isn't 0 we're dealing with some USB IO
1351 * specific behavior. This is not clean. For one thing
1352 * we can't take privilege into account.
1354 return smk_access(smack_from_secid(secid), task_security(p),
1355 MAY_WRITE, &ad);
1359 * smack_task_wait - Smack access check for waiting
1360 * @p: task to wait for
1362 * Returns 0 if current can wait for p, error code otherwise
1364 static int smack_task_wait(struct task_struct *p)
1366 struct smk_audit_info ad;
1367 char *sp = current_security();
1368 char *tsp = task_security(p);
1369 int rc;
1371 /* we don't log here, we can be overriden */
1372 rc = smk_access(sp, tsp, MAY_WRITE, NULL);
1373 if (rc == 0)
1374 goto out_log;
1377 * Allow the operation to succeed if either task
1378 * has privilege to perform operations that might
1379 * account for the smack labels having gotten to
1380 * be different in the first place.
1382 * This breaks the strict subject/object access
1383 * control ideal, taking the object's privilege
1384 * state into account in the decision as well as
1385 * the smack value.
1387 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1388 rc = 0;
1389 /* we log only if we didn't get overriden */
1390 out_log:
1391 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1392 smk_ad_setfield_u_tsk(&ad, p);
1393 smack_log(sp, tsp, MAY_WRITE, rc, &ad);
1394 return rc;
1398 * smack_task_to_inode - copy task smack into the inode blob
1399 * @p: task to copy from
1400 * @inode: inode to copy to
1402 * Sets the smack pointer in the inode security blob
1404 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1406 struct inode_smack *isp = inode->i_security;
1407 isp->smk_inode = task_security(p);
1411 * Socket hooks.
1415 * smack_sk_alloc_security - Allocate a socket blob
1416 * @sk: the socket
1417 * @family: unused
1418 * @gfp_flags: memory allocation flags
1420 * Assign Smack pointers to current
1422 * Returns 0 on success, -ENOMEM is there's no memory
1424 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1426 char *csp = current_security();
1427 struct socket_smack *ssp;
1429 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1430 if (ssp == NULL)
1431 return -ENOMEM;
1433 ssp->smk_in = csp;
1434 ssp->smk_out = csp;
1435 ssp->smk_packet[0] = '\0';
1437 sk->sk_security = ssp;
1439 return 0;
1443 * smack_sk_free_security - Free a socket blob
1444 * @sk: the socket
1446 * Clears the blob pointer
1448 static void smack_sk_free_security(struct sock *sk)
1450 kfree(sk->sk_security);
1454 * smack_host_label - check host based restrictions
1455 * @sip: the object end
1457 * looks for host based access restrictions
1459 * This version will only be appropriate for really small sets of single label
1460 * hosts. The caller is responsible for ensuring that the RCU read lock is
1461 * taken before calling this function.
1463 * Returns the label of the far end or NULL if it's not special.
1465 static char *smack_host_label(struct sockaddr_in *sip)
1467 struct smk_netlbladdr *snp;
1468 struct in_addr *siap = &sip->sin_addr;
1470 if (siap->s_addr == 0)
1471 return NULL;
1473 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1475 * we break after finding the first match because
1476 * the list is sorted from longest to shortest mask
1477 * so we have found the most specific match
1479 if ((&snp->smk_host.sin_addr)->s_addr ==
1480 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1481 /* we have found the special CIPSO option */
1482 if (snp->smk_label == smack_cipso_option)
1483 return NULL;
1484 return snp->smk_label;
1487 return NULL;
1491 * smack_set_catset - convert a capset to netlabel mls categories
1492 * @catset: the Smack categories
1493 * @sap: where to put the netlabel categories
1495 * Allocates and fills attr.mls.cat
1497 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1499 unsigned char *cp;
1500 unsigned char m;
1501 int cat;
1502 int rc;
1503 int byte;
1505 if (!catset)
1506 return;
1508 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1509 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1510 sap->attr.mls.cat->startbit = 0;
1512 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1513 for (m = 0x80; m != 0; m >>= 1, cat++) {
1514 if ((m & *cp) == 0)
1515 continue;
1516 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1517 cat, GFP_ATOMIC);
1522 * smack_to_secattr - fill a secattr from a smack value
1523 * @smack: the smack value
1524 * @nlsp: where the result goes
1526 * Casey says that CIPSO is good enough for now.
1527 * It can be used to effect.
1528 * It can also be abused to effect when necessary.
1529 * Appologies to the TSIG group in general and GW in particular.
1531 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1533 struct smack_cipso cipso;
1534 int rc;
1536 nlsp->domain = smack;
1537 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1539 rc = smack_to_cipso(smack, &cipso);
1540 if (rc == 0) {
1541 nlsp->attr.mls.lvl = cipso.smk_level;
1542 smack_set_catset(cipso.smk_catset, nlsp);
1543 } else {
1544 nlsp->attr.mls.lvl = smack_cipso_direct;
1545 smack_set_catset(smack, nlsp);
1550 * smack_netlabel - Set the secattr on a socket
1551 * @sk: the socket
1552 * @labeled: socket label scheme
1554 * Convert the outbound smack value (smk_out) to a
1555 * secattr and attach it to the socket.
1557 * Returns 0 on success or an error code
1559 static int smack_netlabel(struct sock *sk, int labeled)
1561 struct socket_smack *ssp = sk->sk_security;
1562 struct netlbl_lsm_secattr secattr;
1563 int rc = 0;
1566 * Usually the netlabel code will handle changing the
1567 * packet labeling based on the label.
1568 * The case of a single label host is different, because
1569 * a single label host should never get a labeled packet
1570 * even though the label is usually associated with a packet
1571 * label.
1573 local_bh_disable();
1574 bh_lock_sock_nested(sk);
1576 if (ssp->smk_out == smack_net_ambient ||
1577 labeled == SMACK_UNLABELED_SOCKET)
1578 netlbl_sock_delattr(sk);
1579 else {
1580 netlbl_secattr_init(&secattr);
1581 smack_to_secattr(ssp->smk_out, &secattr);
1582 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
1583 netlbl_secattr_destroy(&secattr);
1586 bh_unlock_sock(sk);
1587 local_bh_enable();
1589 return rc;
1593 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1594 * @sk: the socket
1595 * @sap: the destination address
1597 * Set the correct secattr for the given socket based on the destination
1598 * address and perform any outbound access checks needed.
1600 * Returns 0 on success or an error code.
1603 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1605 int rc;
1606 int sk_lbl;
1607 char *hostsp;
1608 struct socket_smack *ssp = sk->sk_security;
1609 struct smk_audit_info ad;
1611 rcu_read_lock();
1612 hostsp = smack_host_label(sap);
1613 if (hostsp != NULL) {
1614 sk_lbl = SMACK_UNLABELED_SOCKET;
1615 #ifdef CONFIG_AUDIT
1616 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1617 ad.a.u.net.family = sap->sin_family;
1618 ad.a.u.net.dport = sap->sin_port;
1619 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1620 #endif
1621 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
1622 } else {
1623 sk_lbl = SMACK_CIPSO_SOCKET;
1624 rc = 0;
1626 rcu_read_unlock();
1627 if (rc != 0)
1628 return rc;
1630 return smack_netlabel(sk, sk_lbl);
1634 * smack_inode_setsecurity - set smack xattrs
1635 * @inode: the object
1636 * @name: attribute name
1637 * @value: attribute value
1638 * @size: size of the attribute
1639 * @flags: unused
1641 * Sets the named attribute in the appropriate blob
1643 * Returns 0 on success, or an error code
1645 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1646 const void *value, size_t size, int flags)
1648 char *sp;
1649 struct inode_smack *nsp = inode->i_security;
1650 struct socket_smack *ssp;
1651 struct socket *sock;
1652 int rc = 0;
1654 if (value == NULL || size > SMK_LABELLEN || size == 0)
1655 return -EACCES;
1657 sp = smk_import(value, size);
1658 if (sp == NULL)
1659 return -EINVAL;
1661 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1662 nsp->smk_inode = sp;
1663 nsp->smk_flags |= SMK_INODE_INSTANT;
1664 return 0;
1667 * The rest of the Smack xattrs are only on sockets.
1669 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1670 return -EOPNOTSUPP;
1672 sock = SOCKET_I(inode);
1673 if (sock == NULL || sock->sk == NULL)
1674 return -EOPNOTSUPP;
1676 ssp = sock->sk->sk_security;
1678 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1679 ssp->smk_in = sp;
1680 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1681 ssp->smk_out = sp;
1682 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1683 if (rc != 0)
1684 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1685 __func__, -rc);
1686 } else
1687 return -EOPNOTSUPP;
1689 return 0;
1693 * smack_socket_post_create - finish socket setup
1694 * @sock: the socket
1695 * @family: protocol family
1696 * @type: unused
1697 * @protocol: unused
1698 * @kern: unused
1700 * Sets the netlabel information on the socket
1702 * Returns 0 on success, and error code otherwise
1704 static int smack_socket_post_create(struct socket *sock, int family,
1705 int type, int protocol, int kern)
1707 if (family != PF_INET || sock->sk == NULL)
1708 return 0;
1710 * Set the outbound netlbl.
1712 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1716 * smack_socket_connect - connect access check
1717 * @sock: the socket
1718 * @sap: the other end
1719 * @addrlen: size of sap
1721 * Verifies that a connection may be possible
1723 * Returns 0 on success, and error code otherwise
1725 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1726 int addrlen)
1728 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1729 return 0;
1730 if (addrlen < sizeof(struct sockaddr_in))
1731 return -EINVAL;
1733 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
1737 * smack_flags_to_may - convert S_ to MAY_ values
1738 * @flags: the S_ value
1740 * Returns the equivalent MAY_ value
1742 static int smack_flags_to_may(int flags)
1744 int may = 0;
1746 if (flags & S_IRUGO)
1747 may |= MAY_READ;
1748 if (flags & S_IWUGO)
1749 may |= MAY_WRITE;
1750 if (flags & S_IXUGO)
1751 may |= MAY_EXEC;
1753 return may;
1757 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1758 * @msg: the object
1760 * Returns 0
1762 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1764 msg->security = current_security();
1765 return 0;
1769 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1770 * @msg: the object
1772 * Clears the blob pointer
1774 static void smack_msg_msg_free_security(struct msg_msg *msg)
1776 msg->security = NULL;
1780 * smack_of_shm - the smack pointer for the shm
1781 * @shp: the object
1783 * Returns a pointer to the smack value
1785 static char *smack_of_shm(struct shmid_kernel *shp)
1787 return (char *)shp->shm_perm.security;
1791 * smack_shm_alloc_security - Set the security blob for shm
1792 * @shp: the object
1794 * Returns 0
1796 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1798 struct kern_ipc_perm *isp = &shp->shm_perm;
1800 isp->security = current_security();
1801 return 0;
1805 * smack_shm_free_security - Clear the security blob for shm
1806 * @shp: the object
1808 * Clears the blob pointer
1810 static void smack_shm_free_security(struct shmid_kernel *shp)
1812 struct kern_ipc_perm *isp = &shp->shm_perm;
1814 isp->security = NULL;
1818 * smk_curacc_shm : check if current has access on shm
1819 * @shp : the object
1820 * @access : access requested
1822 * Returns 0 if current has the requested access, error code otherwise
1824 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
1826 char *ssp = smack_of_shm(shp);
1827 struct smk_audit_info ad;
1829 #ifdef CONFIG_AUDIT
1830 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1831 ad.a.u.ipc_id = shp->shm_perm.id;
1832 #endif
1833 return smk_curacc(ssp, access, &ad);
1837 * smack_shm_associate - Smack access check for shm
1838 * @shp: the object
1839 * @shmflg: access requested
1841 * Returns 0 if current has the requested access, error code otherwise
1843 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1845 int may;
1847 may = smack_flags_to_may(shmflg);
1848 return smk_curacc_shm(shp, may);
1852 * smack_shm_shmctl - Smack access check for shm
1853 * @shp: the object
1854 * @cmd: what it wants to do
1856 * Returns 0 if current has the requested access, error code otherwise
1858 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1860 int may;
1862 switch (cmd) {
1863 case IPC_STAT:
1864 case SHM_STAT:
1865 may = MAY_READ;
1866 break;
1867 case IPC_SET:
1868 case SHM_LOCK:
1869 case SHM_UNLOCK:
1870 case IPC_RMID:
1871 may = MAY_READWRITE;
1872 break;
1873 case IPC_INFO:
1874 case SHM_INFO:
1876 * System level information.
1878 return 0;
1879 default:
1880 return -EINVAL;
1882 return smk_curacc_shm(shp, may);
1886 * smack_shm_shmat - Smack access for shmat
1887 * @shp: the object
1888 * @shmaddr: unused
1889 * @shmflg: access requested
1891 * Returns 0 if current has the requested access, error code otherwise
1893 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1894 int shmflg)
1896 int may;
1898 may = smack_flags_to_may(shmflg);
1899 return smk_curacc_shm(shp, may);
1903 * smack_of_sem - the smack pointer for the sem
1904 * @sma: the object
1906 * Returns a pointer to the smack value
1908 static char *smack_of_sem(struct sem_array *sma)
1910 return (char *)sma->sem_perm.security;
1914 * smack_sem_alloc_security - Set the security blob for sem
1915 * @sma: the object
1917 * Returns 0
1919 static int smack_sem_alloc_security(struct sem_array *sma)
1921 struct kern_ipc_perm *isp = &sma->sem_perm;
1923 isp->security = current_security();
1924 return 0;
1928 * smack_sem_free_security - Clear the security blob for sem
1929 * @sma: the object
1931 * Clears the blob pointer
1933 static void smack_sem_free_security(struct sem_array *sma)
1935 struct kern_ipc_perm *isp = &sma->sem_perm;
1937 isp->security = NULL;
1941 * smk_curacc_sem : check if current has access on sem
1942 * @sma : the object
1943 * @access : access requested
1945 * Returns 0 if current has the requested access, error code otherwise
1947 static int smk_curacc_sem(struct sem_array *sma, int access)
1949 char *ssp = smack_of_sem(sma);
1950 struct smk_audit_info ad;
1952 #ifdef CONFIG_AUDIT
1953 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1954 ad.a.u.ipc_id = sma->sem_perm.id;
1955 #endif
1956 return smk_curacc(ssp, access, &ad);
1960 * smack_sem_associate - Smack access check for sem
1961 * @sma: the object
1962 * @semflg: access requested
1964 * Returns 0 if current has the requested access, error code otherwise
1966 static int smack_sem_associate(struct sem_array *sma, int semflg)
1968 int may;
1970 may = smack_flags_to_may(semflg);
1971 return smk_curacc_sem(sma, may);
1975 * smack_sem_shmctl - Smack access check for sem
1976 * @sma: the object
1977 * @cmd: what it wants to do
1979 * Returns 0 if current has the requested access, error code otherwise
1981 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1983 int may;
1985 switch (cmd) {
1986 case GETPID:
1987 case GETNCNT:
1988 case GETZCNT:
1989 case GETVAL:
1990 case GETALL:
1991 case IPC_STAT:
1992 case SEM_STAT:
1993 may = MAY_READ;
1994 break;
1995 case SETVAL:
1996 case SETALL:
1997 case IPC_RMID:
1998 case IPC_SET:
1999 may = MAY_READWRITE;
2000 break;
2001 case IPC_INFO:
2002 case SEM_INFO:
2004 * System level information
2006 return 0;
2007 default:
2008 return -EINVAL;
2011 return smk_curacc_sem(sma, may);
2015 * smack_sem_semop - Smack checks of semaphore operations
2016 * @sma: the object
2017 * @sops: unused
2018 * @nsops: unused
2019 * @alter: unused
2021 * Treated as read and write in all cases.
2023 * Returns 0 if access is allowed, error code otherwise
2025 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2026 unsigned nsops, int alter)
2028 return smk_curacc_sem(sma, MAY_READWRITE);
2032 * smack_msg_alloc_security - Set the security blob for msg
2033 * @msq: the object
2035 * Returns 0
2037 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2039 struct kern_ipc_perm *kisp = &msq->q_perm;
2041 kisp->security = current_security();
2042 return 0;
2046 * smack_msg_free_security - Clear the security blob for msg
2047 * @msq: the object
2049 * Clears the blob pointer
2051 static void smack_msg_queue_free_security(struct msg_queue *msq)
2053 struct kern_ipc_perm *kisp = &msq->q_perm;
2055 kisp->security = NULL;
2059 * smack_of_msq - the smack pointer for the msq
2060 * @msq: the object
2062 * Returns a pointer to the smack value
2064 static char *smack_of_msq(struct msg_queue *msq)
2066 return (char *)msq->q_perm.security;
2070 * smk_curacc_msq : helper to check if current has access on msq
2071 * @msq : the msq
2072 * @access : access requested
2074 * return 0 if current has access, error otherwise
2076 static int smk_curacc_msq(struct msg_queue *msq, int access)
2078 char *msp = smack_of_msq(msq);
2079 struct smk_audit_info ad;
2081 #ifdef CONFIG_AUDIT
2082 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2083 ad.a.u.ipc_id = msq->q_perm.id;
2084 #endif
2085 return smk_curacc(msp, access, &ad);
2089 * smack_msg_queue_associate - Smack access check for msg_queue
2090 * @msq: the object
2091 * @msqflg: access requested
2093 * Returns 0 if current has the requested access, error code otherwise
2095 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2097 int may;
2099 may = smack_flags_to_may(msqflg);
2100 return smk_curacc_msq(msq, may);
2104 * smack_msg_queue_msgctl - Smack access check for msg_queue
2105 * @msq: the object
2106 * @cmd: what it wants to do
2108 * Returns 0 if current has the requested access, error code otherwise
2110 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2112 int may;
2114 switch (cmd) {
2115 case IPC_STAT:
2116 case MSG_STAT:
2117 may = MAY_READ;
2118 break;
2119 case IPC_SET:
2120 case IPC_RMID:
2121 may = MAY_READWRITE;
2122 break;
2123 case IPC_INFO:
2124 case MSG_INFO:
2126 * System level information
2128 return 0;
2129 default:
2130 return -EINVAL;
2133 return smk_curacc_msq(msq, may);
2137 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2138 * @msq: the object
2139 * @msg: unused
2140 * @msqflg: access requested
2142 * Returns 0 if current has the requested access, error code otherwise
2144 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2145 int msqflg)
2147 int may;
2149 may = smack_flags_to_may(msqflg);
2150 return smk_curacc_msq(msq, may);
2154 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2155 * @msq: the object
2156 * @msg: unused
2157 * @target: unused
2158 * @type: unused
2159 * @mode: unused
2161 * Returns 0 if current has read and write access, error code otherwise
2163 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2164 struct task_struct *target, long type, int mode)
2166 return smk_curacc_msq(msq, MAY_READWRITE);
2170 * smack_ipc_permission - Smack access for ipc_permission()
2171 * @ipp: the object permissions
2172 * @flag: access requested
2174 * Returns 0 if current has read and write access, error code otherwise
2176 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2178 char *isp = ipp->security;
2179 int may = smack_flags_to_may(flag);
2180 struct smk_audit_info ad;
2182 #ifdef CONFIG_AUDIT
2183 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2184 ad.a.u.ipc_id = ipp->id;
2185 #endif
2186 return smk_curacc(isp, may, &ad);
2190 * smack_ipc_getsecid - Extract smack security id
2191 * @ipp: the object permissions
2192 * @secid: where result will be saved
2194 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2196 char *smack = ipp->security;
2198 *secid = smack_to_secid(smack);
2202 * smack_d_instantiate - Make sure the blob is correct on an inode
2203 * @opt_dentry: unused
2204 * @inode: the object
2206 * Set the inode's security blob if it hasn't been done already.
2208 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2210 struct super_block *sbp;
2211 struct superblock_smack *sbsp;
2212 struct inode_smack *isp;
2213 char *csp = current_security();
2214 char *fetched;
2215 char *final;
2216 struct dentry *dp;
2218 if (inode == NULL)
2219 return;
2221 isp = inode->i_security;
2223 mutex_lock(&isp->smk_lock);
2225 * If the inode is already instantiated
2226 * take the quick way out
2228 if (isp->smk_flags & SMK_INODE_INSTANT)
2229 goto unlockandout;
2231 sbp = inode->i_sb;
2232 sbsp = sbp->s_security;
2234 * We're going to use the superblock default label
2235 * if there's no label on the file.
2237 final = sbsp->smk_default;
2240 * If this is the root inode the superblock
2241 * may be in the process of initialization.
2242 * If that is the case use the root value out
2243 * of the superblock.
2245 if (opt_dentry->d_parent == opt_dentry) {
2246 isp->smk_inode = sbsp->smk_root;
2247 isp->smk_flags |= SMK_INODE_INSTANT;
2248 goto unlockandout;
2252 * This is pretty hackish.
2253 * Casey says that we shouldn't have to do
2254 * file system specific code, but it does help
2255 * with keeping it simple.
2257 switch (sbp->s_magic) {
2258 case SMACK_MAGIC:
2260 * Casey says that it's a little embarassing
2261 * that the smack file system doesn't do
2262 * extended attributes.
2264 final = smack_known_star.smk_known;
2265 break;
2266 case PIPEFS_MAGIC:
2268 * Casey says pipes are easy (?)
2270 final = smack_known_star.smk_known;
2271 break;
2272 case DEVPTS_SUPER_MAGIC:
2274 * devpts seems content with the label of the task.
2275 * Programs that change smack have to treat the
2276 * pty with respect.
2278 final = csp;
2279 break;
2280 case SOCKFS_MAGIC:
2282 * Casey says sockets get the smack of the task.
2284 final = csp;
2285 break;
2286 case PROC_SUPER_MAGIC:
2288 * Casey says procfs appears not to care.
2289 * The superblock default suffices.
2291 break;
2292 case TMPFS_MAGIC:
2294 * Device labels should come from the filesystem,
2295 * but watch out, because they're volitile,
2296 * getting recreated on every reboot.
2298 final = smack_known_star.smk_known;
2300 * No break.
2302 * If a smack value has been set we want to use it,
2303 * but since tmpfs isn't giving us the opportunity
2304 * to set mount options simulate setting the
2305 * superblock default.
2307 default:
2309 * This isn't an understood special case.
2310 * Get the value from the xattr.
2312 * No xattr support means, alas, no SMACK label.
2313 * Use the aforeapplied default.
2314 * It would be curious if the label of the task
2315 * does not match that assigned.
2317 if (inode->i_op->getxattr == NULL)
2318 break;
2320 * Get the dentry for xattr.
2322 if (opt_dentry == NULL) {
2323 dp = d_find_alias(inode);
2324 if (dp == NULL)
2325 break;
2326 } else {
2327 dp = dget(opt_dentry);
2328 if (dp == NULL)
2329 break;
2332 fetched = smk_fetch(inode, dp);
2333 if (fetched != NULL)
2334 final = fetched;
2336 dput(dp);
2337 break;
2340 if (final == NULL)
2341 isp->smk_inode = csp;
2342 else
2343 isp->smk_inode = final;
2345 isp->smk_flags |= SMK_INODE_INSTANT;
2347 unlockandout:
2348 mutex_unlock(&isp->smk_lock);
2349 return;
2353 * smack_getprocattr - Smack process attribute access
2354 * @p: the object task
2355 * @name: the name of the attribute in /proc/.../attr
2356 * @value: where to put the result
2358 * Places a copy of the task Smack into value
2360 * Returns the length of the smack label or an error code
2362 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2364 char *cp;
2365 int slen;
2367 if (strcmp(name, "current") != 0)
2368 return -EINVAL;
2370 cp = kstrdup(task_security(p), GFP_KERNEL);
2371 if (cp == NULL)
2372 return -ENOMEM;
2374 slen = strlen(cp);
2375 *value = cp;
2376 return slen;
2380 * smack_setprocattr - Smack process attribute setting
2381 * @p: the object task
2382 * @name: the name of the attribute in /proc/.../attr
2383 * @value: the value to set
2384 * @size: the size of the value
2386 * Sets the Smack value of the task. Only setting self
2387 * is permitted and only with privilege
2389 * Returns the length of the smack label or an error code
2391 static int smack_setprocattr(struct task_struct *p, char *name,
2392 void *value, size_t size)
2394 struct cred *new;
2395 char *newsmack;
2398 * Changing another process' Smack value is too dangerous
2399 * and supports no sane use case.
2401 if (p != current)
2402 return -EPERM;
2404 if (!capable(CAP_MAC_ADMIN))
2405 return -EPERM;
2407 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2408 return -EINVAL;
2410 if (strcmp(name, "current") != 0)
2411 return -EINVAL;
2413 newsmack = smk_import(value, size);
2414 if (newsmack == NULL)
2415 return -EINVAL;
2418 * No process is ever allowed the web ("@") label.
2420 if (newsmack == smack_known_web.smk_known)
2421 return -EPERM;
2423 new = prepare_creds();
2424 if (new == NULL)
2425 return -ENOMEM;
2426 new->security = newsmack;
2427 commit_creds(new);
2428 return size;
2432 * smack_unix_stream_connect - Smack access on UDS
2433 * @sock: one socket
2434 * @other: the other socket
2435 * @newsk: unused
2437 * Return 0 if a subject with the smack of sock could access
2438 * an object with the smack of other, otherwise an error code
2440 static int smack_unix_stream_connect(struct socket *sock,
2441 struct socket *other, struct sock *newsk)
2443 struct inode *sp = SOCK_INODE(sock);
2444 struct inode *op = SOCK_INODE(other);
2445 struct smk_audit_info ad;
2447 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2448 smk_ad_setfield_u_net_sk(&ad, other->sk);
2449 return smk_access(smk_of_inode(sp), smk_of_inode(op),
2450 MAY_READWRITE, &ad);
2454 * smack_unix_may_send - Smack access on UDS
2455 * @sock: one socket
2456 * @other: the other socket
2458 * Return 0 if a subject with the smack of sock could access
2459 * an object with the smack of other, otherwise an error code
2461 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2463 struct inode *sp = SOCK_INODE(sock);
2464 struct inode *op = SOCK_INODE(other);
2465 struct smk_audit_info ad;
2467 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2468 smk_ad_setfield_u_net_sk(&ad, other->sk);
2469 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE, &ad);
2473 * smack_socket_sendmsg - Smack check based on destination host
2474 * @sock: the socket
2475 * @msg: the message
2476 * @size: the size of the message
2478 * Return 0 if the current subject can write to the destination
2479 * host. This is only a question if the destination is a single
2480 * label host.
2482 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2483 int size)
2485 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2488 * Perfectly reasonable for this to be NULL
2490 if (sip == NULL || sip->sin_family != AF_INET)
2491 return 0;
2493 return smack_netlabel_send(sock->sk, sip);
2498 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2499 * @sap: netlabel secattr
2500 * @sip: where to put the result
2502 * Copies a smack label into sip
2504 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2506 char smack[SMK_LABELLEN];
2507 char *sp;
2508 int pcat;
2510 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2512 * Looks like a CIPSO packet.
2513 * If there are flags but no level netlabel isn't
2514 * behaving the way we expect it to.
2516 * Get the categories, if any
2517 * Without guidance regarding the smack value
2518 * for the packet fall back on the network
2519 * ambient value.
2521 memset(smack, '\0', SMK_LABELLEN);
2522 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2523 for (pcat = -1;;) {
2524 pcat = netlbl_secattr_catmap_walk(
2525 sap->attr.mls.cat, pcat + 1);
2526 if (pcat < 0)
2527 break;
2528 smack_catset_bit(pcat, smack);
2531 * If it is CIPSO using smack direct mapping
2532 * we are already done. WeeHee.
2534 if (sap->attr.mls.lvl == smack_cipso_direct) {
2535 memcpy(sip, smack, SMK_MAXLEN);
2536 return;
2539 * Look it up in the supplied table if it is not
2540 * a direct mapping.
2542 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2543 return;
2545 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2547 * Looks like a fallback, which gives us a secid.
2549 sp = smack_from_secid(sap->attr.secid);
2551 * This has got to be a bug because it is
2552 * impossible to specify a fallback without
2553 * specifying the label, which will ensure
2554 * it has a secid, and the only way to get a
2555 * secid is from a fallback.
2557 BUG_ON(sp == NULL);
2558 strncpy(sip, sp, SMK_MAXLEN);
2559 return;
2562 * Without guidance regarding the smack value
2563 * for the packet fall back on the network
2564 * ambient value.
2566 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2567 return;
2571 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2572 * @sk: socket
2573 * @skb: packet
2575 * Returns 0 if the packet should be delivered, an error code otherwise
2577 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2579 struct netlbl_lsm_secattr secattr;
2580 struct socket_smack *ssp = sk->sk_security;
2581 char smack[SMK_LABELLEN];
2582 char *csp;
2583 int rc;
2584 struct smk_audit_info ad;
2585 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2586 return 0;
2589 * Translate what netlabel gave us.
2591 netlbl_secattr_init(&secattr);
2593 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2594 if (rc == 0) {
2595 smack_from_secattr(&secattr, smack);
2596 csp = smack;
2597 } else
2598 csp = smack_net_ambient;
2600 netlbl_secattr_destroy(&secattr);
2602 #ifdef CONFIG_AUDIT
2603 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2604 ad.a.u.net.family = sk->sk_family;
2605 ad.a.u.net.netif = skb->iif;
2606 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2607 #endif
2609 * Receiving a packet requires that the other end
2610 * be able to write here. Read access is not required.
2611 * This is the simplist possible security model
2612 * for networking.
2614 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
2615 if (rc != 0)
2616 netlbl_skbuff_err(skb, rc, 0);
2617 return rc;
2621 * smack_socket_getpeersec_stream - pull in packet label
2622 * @sock: the socket
2623 * @optval: user's destination
2624 * @optlen: size thereof
2625 * @len: max thereof
2627 * returns zero on success, an error code otherwise
2629 static int smack_socket_getpeersec_stream(struct socket *sock,
2630 char __user *optval,
2631 int __user *optlen, unsigned len)
2633 struct socket_smack *ssp;
2634 int slen;
2635 int rc = 0;
2637 ssp = sock->sk->sk_security;
2638 slen = strlen(ssp->smk_packet) + 1;
2640 if (slen > len)
2641 rc = -ERANGE;
2642 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2643 rc = -EFAULT;
2645 if (put_user(slen, optlen) != 0)
2646 rc = -EFAULT;
2648 return rc;
2653 * smack_socket_getpeersec_dgram - pull in packet label
2654 * @sock: the socket
2655 * @skb: packet data
2656 * @secid: pointer to where to put the secid of the packet
2658 * Sets the netlabel socket state on sk from parent
2660 static int smack_socket_getpeersec_dgram(struct socket *sock,
2661 struct sk_buff *skb, u32 *secid)
2664 struct netlbl_lsm_secattr secattr;
2665 struct sock *sk;
2666 char smack[SMK_LABELLEN];
2667 int family = PF_INET;
2668 u32 s;
2669 int rc;
2672 * Only works for families with packets.
2674 if (sock != NULL) {
2675 sk = sock->sk;
2676 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2677 return 0;
2678 family = sk->sk_family;
2681 * Translate what netlabel gave us.
2683 netlbl_secattr_init(&secattr);
2684 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2685 if (rc == 0)
2686 smack_from_secattr(&secattr, smack);
2687 netlbl_secattr_destroy(&secattr);
2690 * Give up if we couldn't get anything
2692 if (rc != 0)
2693 return rc;
2695 s = smack_to_secid(smack);
2696 if (s == 0)
2697 return -EINVAL;
2699 *secid = s;
2700 return 0;
2704 * smack_sock_graft - Initialize a newly created socket with an existing sock
2705 * @sk: child sock
2706 * @parent: parent socket
2708 * Set the smk_{in,out} state of an existing sock based on the process that
2709 * is creating the new socket.
2711 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2713 struct socket_smack *ssp;
2715 if (sk == NULL ||
2716 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
2717 return;
2719 ssp = sk->sk_security;
2720 ssp->smk_in = ssp->smk_out = current_security();
2721 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
2725 * smack_inet_conn_request - Smack access check on connect
2726 * @sk: socket involved
2727 * @skb: packet
2728 * @req: unused
2730 * Returns 0 if a task with the packet label could write to
2731 * the socket, otherwise an error code
2733 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2734 struct request_sock *req)
2736 u16 family = sk->sk_family;
2737 struct socket_smack *ssp = sk->sk_security;
2738 struct netlbl_lsm_secattr secattr;
2739 struct sockaddr_in addr;
2740 struct iphdr *hdr;
2741 char smack[SMK_LABELLEN];
2742 int rc;
2743 struct smk_audit_info ad;
2745 /* handle mapped IPv4 packets arriving via IPv6 sockets */
2746 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
2747 family = PF_INET;
2749 netlbl_secattr_init(&secattr);
2750 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2751 if (rc == 0)
2752 smack_from_secattr(&secattr, smack);
2753 else
2754 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2755 netlbl_secattr_destroy(&secattr);
2757 #ifdef CONFIG_AUDIT
2758 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2759 ad.a.u.net.family = family;
2760 ad.a.u.net.netif = skb->iif;
2761 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2762 #endif
2764 * Receiving a packet requires that the other end be able to write
2765 * here. Read access is not required.
2767 rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);
2768 if (rc != 0)
2769 return rc;
2772 * Save the peer's label in the request_sock so we can later setup
2773 * smk_packet in the child socket so that SO_PEERCRED can report it.
2775 req->peer_secid = smack_to_secid(smack);
2778 * We need to decide if we want to label the incoming connection here
2779 * if we do we only need to label the request_sock and the stack will
2780 * propogate the wire-label to the sock when it is created.
2782 hdr = ip_hdr(skb);
2783 addr.sin_addr.s_addr = hdr->saddr;
2784 rcu_read_lock();
2785 if (smack_host_label(&addr) == NULL) {
2786 rcu_read_unlock();
2787 netlbl_secattr_init(&secattr);
2788 smack_to_secattr(smack, &secattr);
2789 rc = netlbl_req_setattr(req, &secattr);
2790 netlbl_secattr_destroy(&secattr);
2791 } else {
2792 rcu_read_unlock();
2793 netlbl_req_delattr(req);
2796 return rc;
2800 * smack_inet_csk_clone - Copy the connection information to the new socket
2801 * @sk: the new socket
2802 * @req: the connection's request_sock
2804 * Transfer the connection's peer label to the newly created socket.
2806 static void smack_inet_csk_clone(struct sock *sk,
2807 const struct request_sock *req)
2809 struct socket_smack *ssp = sk->sk_security;
2810 char *smack;
2812 if (req->peer_secid != 0) {
2813 smack = smack_from_secid(req->peer_secid);
2814 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2815 } else
2816 ssp->smk_packet[0] = '\0';
2820 * Key management security hooks
2822 * Casey has not tested key support very heavily.
2823 * The permission check is most likely too restrictive.
2824 * If you care about keys please have a look.
2826 #ifdef CONFIG_KEYS
2829 * smack_key_alloc - Set the key security blob
2830 * @key: object
2831 * @cred: the credentials to use
2832 * @flags: unused
2834 * No allocation required
2836 * Returns 0
2838 static int smack_key_alloc(struct key *key, const struct cred *cred,
2839 unsigned long flags)
2841 key->security = cred->security;
2842 return 0;
2846 * smack_key_free - Clear the key security blob
2847 * @key: the object
2849 * Clear the blob pointer
2851 static void smack_key_free(struct key *key)
2853 key->security = NULL;
2857 * smack_key_permission - Smack access on a key
2858 * @key_ref: gets to the object
2859 * @cred: the credentials to use
2860 * @perm: unused
2862 * Return 0 if the task has read and write to the object,
2863 * an error code otherwise
2865 static int smack_key_permission(key_ref_t key_ref,
2866 const struct cred *cred, key_perm_t perm)
2868 struct key *keyp;
2869 struct smk_audit_info ad;
2871 keyp = key_ref_to_ptr(key_ref);
2872 if (keyp == NULL)
2873 return -EINVAL;
2875 * If the key hasn't been initialized give it access so that
2876 * it may do so.
2878 if (keyp->security == NULL)
2879 return 0;
2881 * This should not occur
2883 if (cred->security == NULL)
2884 return -EACCES;
2885 #ifdef CONFIG_AUDIT
2886 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
2887 ad.a.u.key_struct.key = keyp->serial;
2888 ad.a.u.key_struct.key_desc = keyp->description;
2889 #endif
2890 return smk_access(cred->security, keyp->security,
2891 MAY_READWRITE, &ad);
2893 #endif /* CONFIG_KEYS */
2896 * Smack Audit hooks
2898 * Audit requires a unique representation of each Smack specific
2899 * rule. This unique representation is used to distinguish the
2900 * object to be audited from remaining kernel objects and also
2901 * works as a glue between the audit hooks.
2903 * Since repository entries are added but never deleted, we'll use
2904 * the smack_known label address related to the given audit rule as
2905 * the needed unique representation. This also better fits the smack
2906 * model where nearly everything is a label.
2908 #ifdef CONFIG_AUDIT
2911 * smack_audit_rule_init - Initialize a smack audit rule
2912 * @field: audit rule fields given from user-space (audit.h)
2913 * @op: required testing operator (=, !=, >, <, ...)
2914 * @rulestr: smack label to be audited
2915 * @vrule: pointer to save our own audit rule representation
2917 * Prepare to audit cases where (@field @op @rulestr) is true.
2918 * The label to be audited is created if necessay.
2920 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2922 char **rule = (char **)vrule;
2923 *rule = NULL;
2925 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2926 return -EINVAL;
2928 if (op != Audit_equal && op != Audit_not_equal)
2929 return -EINVAL;
2931 *rule = smk_import(rulestr, 0);
2933 return 0;
2937 * smack_audit_rule_known - Distinguish Smack audit rules
2938 * @krule: rule of interest, in Audit kernel representation format
2940 * This is used to filter Smack rules from remaining Audit ones.
2941 * If it's proved that this rule belongs to us, the
2942 * audit_rule_match hook will be called to do the final judgement.
2944 static int smack_audit_rule_known(struct audit_krule *krule)
2946 struct audit_field *f;
2947 int i;
2949 for (i = 0; i < krule->field_count; i++) {
2950 f = &krule->fields[i];
2952 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2953 return 1;
2956 return 0;
2960 * smack_audit_rule_match - Audit given object ?
2961 * @secid: security id for identifying the object to test
2962 * @field: audit rule flags given from user-space
2963 * @op: required testing operator
2964 * @vrule: smack internal rule presentation
2965 * @actx: audit context associated with the check
2967 * The core Audit hook. It's used to take the decision of
2968 * whether to audit or not to audit a given object.
2970 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2971 struct audit_context *actx)
2973 char *smack;
2974 char *rule = vrule;
2976 if (!rule) {
2977 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2978 "Smack: missing rule\n");
2979 return -ENOENT;
2982 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2983 return 0;
2985 smack = smack_from_secid(secid);
2988 * No need to do string comparisons. If a match occurs,
2989 * both pointers will point to the same smack_known
2990 * label.
2992 if (op == Audit_equal)
2993 return (rule == smack);
2994 if (op == Audit_not_equal)
2995 return (rule != smack);
2997 return 0;
3001 * smack_audit_rule_free - free smack rule representation
3002 * @vrule: rule to be freed.
3004 * No memory was allocated.
3006 static void smack_audit_rule_free(void *vrule)
3008 /* No-op */
3011 #endif /* CONFIG_AUDIT */
3014 * smack_secid_to_secctx - return the smack label for a secid
3015 * @secid: incoming integer
3016 * @secdata: destination
3017 * @seclen: how long it is
3019 * Exists for networking code.
3021 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3023 char *sp = smack_from_secid(secid);
3025 *secdata = sp;
3026 *seclen = strlen(sp);
3027 return 0;
3031 * smack_secctx_to_secid - return the secid for a smack label
3032 * @secdata: smack label
3033 * @seclen: how long result is
3034 * @secid: outgoing integer
3036 * Exists for audit and networking code.
3038 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3040 *secid = smack_to_secid(secdata);
3041 return 0;
3045 * smack_release_secctx - don't do anything.
3046 * @secdata: unused
3047 * @seclen: unused
3049 * Exists to make sure nothing gets done, and properly
3051 static void smack_release_secctx(char *secdata, u32 seclen)
3055 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3057 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3060 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3062 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3065 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3067 int len = 0;
3068 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3070 if (len < 0)
3071 return len;
3072 *ctxlen = len;
3073 return 0;
3076 struct security_operations smack_ops = {
3077 .name = "smack",
3079 .ptrace_access_check = smack_ptrace_access_check,
3080 .ptrace_traceme = smack_ptrace_traceme,
3081 .syslog = smack_syslog,
3083 .sb_alloc_security = smack_sb_alloc_security,
3084 .sb_free_security = smack_sb_free_security,
3085 .sb_copy_data = smack_sb_copy_data,
3086 .sb_kern_mount = smack_sb_kern_mount,
3087 .sb_statfs = smack_sb_statfs,
3088 .sb_mount = smack_sb_mount,
3089 .sb_umount = smack_sb_umount,
3091 .inode_alloc_security = smack_inode_alloc_security,
3092 .inode_free_security = smack_inode_free_security,
3093 .inode_init_security = smack_inode_init_security,
3094 .inode_link = smack_inode_link,
3095 .inode_unlink = smack_inode_unlink,
3096 .inode_rmdir = smack_inode_rmdir,
3097 .inode_rename = smack_inode_rename,
3098 .inode_permission = smack_inode_permission,
3099 .inode_setattr = smack_inode_setattr,
3100 .inode_getattr = smack_inode_getattr,
3101 .inode_setxattr = smack_inode_setxattr,
3102 .inode_post_setxattr = smack_inode_post_setxattr,
3103 .inode_getxattr = smack_inode_getxattr,
3104 .inode_removexattr = smack_inode_removexattr,
3105 .inode_getsecurity = smack_inode_getsecurity,
3106 .inode_setsecurity = smack_inode_setsecurity,
3107 .inode_listsecurity = smack_inode_listsecurity,
3108 .inode_getsecid = smack_inode_getsecid,
3110 .file_permission = smack_file_permission,
3111 .file_alloc_security = smack_file_alloc_security,
3112 .file_free_security = smack_file_free_security,
3113 .file_ioctl = smack_file_ioctl,
3114 .file_lock = smack_file_lock,
3115 .file_fcntl = smack_file_fcntl,
3116 .file_set_fowner = smack_file_set_fowner,
3117 .file_send_sigiotask = smack_file_send_sigiotask,
3118 .file_receive = smack_file_receive,
3120 .cred_alloc_blank = smack_cred_alloc_blank,
3121 .cred_free = smack_cred_free,
3122 .cred_prepare = smack_cred_prepare,
3123 .cred_commit = smack_cred_commit,
3124 .cred_transfer = smack_cred_transfer,
3125 .kernel_act_as = smack_kernel_act_as,
3126 .kernel_create_files_as = smack_kernel_create_files_as,
3127 .task_setpgid = smack_task_setpgid,
3128 .task_getpgid = smack_task_getpgid,
3129 .task_getsid = smack_task_getsid,
3130 .task_getsecid = smack_task_getsecid,
3131 .task_setnice = smack_task_setnice,
3132 .task_setioprio = smack_task_setioprio,
3133 .task_getioprio = smack_task_getioprio,
3134 .task_setscheduler = smack_task_setscheduler,
3135 .task_getscheduler = smack_task_getscheduler,
3136 .task_movememory = smack_task_movememory,
3137 .task_kill = smack_task_kill,
3138 .task_wait = smack_task_wait,
3139 .task_to_inode = smack_task_to_inode,
3141 .ipc_permission = smack_ipc_permission,
3142 .ipc_getsecid = smack_ipc_getsecid,
3144 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3145 .msg_msg_free_security = smack_msg_msg_free_security,
3147 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3148 .msg_queue_free_security = smack_msg_queue_free_security,
3149 .msg_queue_associate = smack_msg_queue_associate,
3150 .msg_queue_msgctl = smack_msg_queue_msgctl,
3151 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3152 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3154 .shm_alloc_security = smack_shm_alloc_security,
3155 .shm_free_security = smack_shm_free_security,
3156 .shm_associate = smack_shm_associate,
3157 .shm_shmctl = smack_shm_shmctl,
3158 .shm_shmat = smack_shm_shmat,
3160 .sem_alloc_security = smack_sem_alloc_security,
3161 .sem_free_security = smack_sem_free_security,
3162 .sem_associate = smack_sem_associate,
3163 .sem_semctl = smack_sem_semctl,
3164 .sem_semop = smack_sem_semop,
3166 .d_instantiate = smack_d_instantiate,
3168 .getprocattr = smack_getprocattr,
3169 .setprocattr = smack_setprocattr,
3171 .unix_stream_connect = smack_unix_stream_connect,
3172 .unix_may_send = smack_unix_may_send,
3174 .socket_post_create = smack_socket_post_create,
3175 .socket_connect = smack_socket_connect,
3176 .socket_sendmsg = smack_socket_sendmsg,
3177 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3178 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3179 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3180 .sk_alloc_security = smack_sk_alloc_security,
3181 .sk_free_security = smack_sk_free_security,
3182 .sock_graft = smack_sock_graft,
3183 .inet_conn_request = smack_inet_conn_request,
3184 .inet_csk_clone = smack_inet_csk_clone,
3186 /* key management security hooks */
3187 #ifdef CONFIG_KEYS
3188 .key_alloc = smack_key_alloc,
3189 .key_free = smack_key_free,
3190 .key_permission = smack_key_permission,
3191 #endif /* CONFIG_KEYS */
3193 /* Audit hooks */
3194 #ifdef CONFIG_AUDIT
3195 .audit_rule_init = smack_audit_rule_init,
3196 .audit_rule_known = smack_audit_rule_known,
3197 .audit_rule_match = smack_audit_rule_match,
3198 .audit_rule_free = smack_audit_rule_free,
3199 #endif /* CONFIG_AUDIT */
3201 .secid_to_secctx = smack_secid_to_secctx,
3202 .secctx_to_secid = smack_secctx_to_secid,
3203 .release_secctx = smack_release_secctx,
3204 .inode_notifysecctx = smack_inode_notifysecctx,
3205 .inode_setsecctx = smack_inode_setsecctx,
3206 .inode_getsecctx = smack_inode_getsecctx,
3210 static __init void init_smack_know_list(void)
3212 list_add(&smack_known_huh.list, &smack_known_list);
3213 list_add(&smack_known_hat.list, &smack_known_list);
3214 list_add(&smack_known_star.list, &smack_known_list);
3215 list_add(&smack_known_floor.list, &smack_known_list);
3216 list_add(&smack_known_invalid.list, &smack_known_list);
3217 list_add(&smack_known_web.list, &smack_known_list);
3221 * smack_init - initialize the smack system
3223 * Returns 0
3225 static __init int smack_init(void)
3227 struct cred *cred;
3229 if (!security_module_enable(&smack_ops))
3230 return 0;
3232 printk(KERN_INFO "Smack: Initializing.\n");
3235 * Set the security state for the initial task.
3237 cred = (struct cred *) current->cred;
3238 cred->security = &smack_known_floor.smk_known;
3240 /* initilize the smack_know_list */
3241 init_smack_know_list();
3243 * Initialize locks
3245 spin_lock_init(&smack_known_huh.smk_cipsolock);
3246 spin_lock_init(&smack_known_hat.smk_cipsolock);
3247 spin_lock_init(&smack_known_star.smk_cipsolock);
3248 spin_lock_init(&smack_known_floor.smk_cipsolock);
3249 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3252 * Register with LSM
3254 if (register_security(&smack_ops))
3255 panic("smack: Unable to register with kernel.\n");
3257 return 0;
3261 * Smack requires early initialization in order to label
3262 * all processes and objects when they are created.
3264 security_initcall(smack_init);