2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/slab.h>
31 #include <linux/mutex.h>
32 #include <linux/pipe_fs_i.h>
33 #include <net/netlabel.h>
34 #include <net/cipso_ipv4.h>
35 #include <linux/audit.h>
36 #include <linux/magic.h>
37 #include <linux/dcache.h>
38 #include <linux/personality.h>
41 #define TRANS_TRUE "TRUE"
42 #define TRANS_TRUE_SIZE 4
45 * smk_fetch - Fetch the smack label from a file.
46 * @ip: a pointer to the inode
47 * @dp: a pointer to the dentry
49 * Returns a pointer to the master list entry for the Smack label
50 * or NULL if there was no label to fetch.
52 static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
55 char in[SMK_LABELLEN];
57 if (ip->i_op->getxattr == NULL)
60 rc = ip->i_op->getxattr(dp, name, in, SMK_LABELLEN);
64 return smk_import(in, rc);
68 * new_inode_smack - allocate an inode security blob
69 * @smack: a pointer to the Smack label to use in the blob
71 * Returns the new blob or NULL if there's no memory available
73 struct inode_smack *new_inode_smack(char *smack)
75 struct inode_smack *isp;
77 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
81 isp->smk_inode = smack;
83 mutex_init(&isp->smk_lock);
89 * new_task_smack - allocate a task security blob
90 * @smack: a pointer to the Smack label to use in the blob
92 * Returns the new blob or NULL if there's no memory available
94 static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
96 struct task_smack *tsp;
98 tsp = kzalloc(sizeof(struct task_smack), gfp);
102 tsp->smk_task = task;
103 tsp->smk_forked = forked;
104 INIT_LIST_HEAD(&tsp->smk_rules);
105 mutex_init(&tsp->smk_rules_lock);
111 * smk_copy_rules - copy a rule set
112 * @nhead - new rules header pointer
113 * @ohead - old rules header pointer
115 * Returns 0 on success, -ENOMEM on error
117 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
120 struct smack_rule *nrp;
121 struct smack_rule *orp;
124 INIT_LIST_HEAD(nhead);
126 list_for_each_entry_rcu(orp, ohead, list) {
127 nrp = kzalloc(sizeof(struct smack_rule), gfp);
133 list_add_rcu(&nrp->list, nhead);
140 * We he, that is fun!
144 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
145 * @ctp: child task pointer
146 * @mode: ptrace attachment mode
148 * Returns 0 if access is OK, an error code otherwise
150 * Do the capability checks, and require read and write.
152 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
155 struct smk_audit_info ad;
158 rc = cap_ptrace_access_check(ctp, mode);
162 tsp = smk_of_task_struct(ctp);
163 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
164 smk_ad_setfield_u_tsk(&ad, ctp);
166 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
171 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
172 * @ptp: parent task pointer
174 * Returns 0 if access is OK, an error code otherwise
176 * Do the capability checks, and require read and write.
178 static int smack_ptrace_traceme(struct task_struct *ptp)
181 struct smk_audit_info ad;
184 rc = cap_ptrace_traceme(ptp);
188 tsp = smk_of_task_struct(ptp);
189 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
190 smk_ad_setfield_u_tsk(&ad, ptp);
192 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
197 * smack_syslog - Smack approval on syslog
198 * @type: message type
200 * Require that the task has the floor label
202 * Returns 0 on success, error code otherwise.
204 static int smack_syslog(int typefrom_file)
207 char *sp = smk_of_current();
209 if (capable(CAP_MAC_OVERRIDE))
212 if (sp != smack_known_floor.smk_known)
224 * smack_sb_alloc_security - allocate a superblock blob
225 * @sb: the superblock getting the blob
227 * Returns 0 on success or -ENOMEM on error.
229 static int smack_sb_alloc_security(struct super_block *sb)
231 struct superblock_smack *sbsp;
233 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
238 sbsp->smk_root = smack_known_floor.smk_known;
239 sbsp->smk_default = smack_known_floor.smk_known;
240 sbsp->smk_floor = smack_known_floor.smk_known;
241 sbsp->smk_hat = smack_known_hat.smk_known;
242 sbsp->smk_initialized = 0;
243 spin_lock_init(&sbsp->smk_sblock);
245 sb->s_security = sbsp;
251 * smack_sb_free_security - free a superblock blob
252 * @sb: the superblock getting the blob
255 static void smack_sb_free_security(struct super_block *sb)
257 kfree(sb->s_security);
258 sb->s_security = NULL;
262 * smack_sb_copy_data - copy mount options data for processing
263 * @orig: where to start
264 * @smackopts: mount options string
266 * Returns 0 on success or -ENOMEM on error.
268 * Copy the Smack specific mount options out of the mount
271 static int smack_sb_copy_data(char *orig, char *smackopts)
273 char *cp, *commap, *otheropts, *dp;
275 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
276 if (otheropts == NULL)
279 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
280 if (strstr(cp, SMK_FSDEFAULT) == cp)
282 else if (strstr(cp, SMK_FSFLOOR) == cp)
284 else if (strstr(cp, SMK_FSHAT) == cp)
286 else if (strstr(cp, SMK_FSROOT) == cp)
291 commap = strchr(cp, ',');
300 strcpy(orig, otheropts);
301 free_page((unsigned long)otheropts);
307 * smack_sb_kern_mount - Smack specific mount processing
308 * @sb: the file system superblock
309 * @flags: the mount flags
310 * @data: the smack mount options
312 * Returns 0 on success, an error code on failure
314 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
316 struct dentry *root = sb->s_root;
317 struct inode *inode = root->d_inode;
318 struct superblock_smack *sp = sb->s_security;
319 struct inode_smack *isp;
324 spin_lock(&sp->smk_sblock);
325 if (sp->smk_initialized != 0) {
326 spin_unlock(&sp->smk_sblock);
329 sp->smk_initialized = 1;
330 spin_unlock(&sp->smk_sblock);
332 for (op = data; op != NULL; op = commap) {
333 commap = strchr(op, ',');
337 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
338 op += strlen(SMK_FSHAT);
339 nsp = smk_import(op, 0);
342 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
343 op += strlen(SMK_FSFLOOR);
344 nsp = smk_import(op, 0);
347 } else if (strncmp(op, SMK_FSDEFAULT,
348 strlen(SMK_FSDEFAULT)) == 0) {
349 op += strlen(SMK_FSDEFAULT);
350 nsp = smk_import(op, 0);
352 sp->smk_default = nsp;
353 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
354 op += strlen(SMK_FSROOT);
355 nsp = smk_import(op, 0);
362 * Initialize the root inode.
364 isp = inode->i_security;
366 inode->i_security = new_inode_smack(sp->smk_root);
368 isp->smk_inode = sp->smk_root;
374 * smack_sb_statfs - Smack check on statfs
375 * @dentry: identifies the file system in question
377 * Returns 0 if current can read the floor of the filesystem,
378 * and error code otherwise
380 static int smack_sb_statfs(struct dentry *dentry)
382 struct superblock_smack *sbp = dentry->d_sb->s_security;
384 struct smk_audit_info ad;
386 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
387 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
389 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
394 * smack_sb_mount - Smack check for mounting
401 * Returns 0 if current can write the floor of the filesystem
402 * being mounted on, an error code otherwise.
404 static int smack_sb_mount(char *dev_name, struct path *path,
405 char *type, unsigned long flags, void *data)
407 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
408 struct smk_audit_info ad;
410 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
411 smk_ad_setfield_u_fs_path(&ad, *path);
413 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
417 * smack_sb_umount - Smack check for unmounting
418 * @mnt: file system to unmount
421 * Returns 0 if current can write the floor of the filesystem
422 * being unmounted, an error code otherwise.
424 static int smack_sb_umount(struct vfsmount *mnt, int flags)
426 struct superblock_smack *sbp;
427 struct smk_audit_info ad;
430 path.dentry = mnt->mnt_root;
433 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
434 smk_ad_setfield_u_fs_path(&ad, path);
436 sbp = mnt->mnt_sb->s_security;
437 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
445 * smack_bprm_set_creds - set creds for exec
446 * @bprm: the exec information
448 * Returns 0 if it gets a blob, -ENOMEM otherwise
450 static int smack_bprm_set_creds(struct linux_binprm *bprm)
452 struct inode *inode = bprm->file->f_path.dentry->d_inode;
453 struct task_smack *bsp = bprm->cred->security;
454 struct inode_smack *isp;
457 rc = cap_bprm_set_creds(bprm);
461 if (bprm->cred_prepared)
464 isp = inode->i_security;
465 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
471 bsp->smk_task = isp->smk_task;
472 bprm->per_clear |= PER_CLEAR_ON_SETID;
478 * smack_bprm_committing_creds - Prepare to install the new credentials
481 * @bprm: binprm for exec
483 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
485 struct task_smack *bsp = bprm->cred->security;
487 if (bsp->smk_task != bsp->smk_forked)
488 current->pdeath_signal = 0;
492 * smack_bprm_secureexec - Return the decision to use secureexec.
493 * @bprm: binprm for exec
495 * Returns 0 on success.
497 static int smack_bprm_secureexec(struct linux_binprm *bprm)
499 struct task_smack *tsp = current_security();
500 int ret = cap_bprm_secureexec(bprm);
502 if (!ret && (tsp->smk_task != tsp->smk_forked))
513 * smack_inode_alloc_security - allocate an inode blob
514 * @inode: the inode in need of a blob
516 * Returns 0 if it gets a blob, -ENOMEM otherwise
518 static int smack_inode_alloc_security(struct inode *inode)
520 inode->i_security = new_inode_smack(smk_of_current());
521 if (inode->i_security == NULL)
527 * smack_inode_free_security - free an inode blob
528 * @inode: the inode with a blob
530 * Clears the blob pointer in inode
532 static void smack_inode_free_security(struct inode *inode)
534 kfree(inode->i_security);
535 inode->i_security = NULL;
539 * smack_inode_init_security - copy out the smack from an inode
543 * @name: where to put the attribute name
544 * @value: where to put the attribute value
545 * @len: where to put the length of the attribute
547 * Returns 0 if it all works out, -ENOMEM if there's no memory
549 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
550 const struct qstr *qstr, char **name,
551 void **value, size_t *len)
553 struct smack_known *skp;
554 char *csp = smk_of_current();
555 char *isp = smk_of_inode(inode);
556 char *dsp = smk_of_inode(dir);
560 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
566 skp = smk_find_entry(csp);
568 may = smk_access_entry(csp, dsp, &skp->smk_rules);
572 * If the access rule allows transmutation and
573 * the directory requests transmutation then
574 * by all means transmute.
576 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
577 smk_inode_transmutable(dir))
580 *value = kstrdup(isp, GFP_KERNEL);
592 * smack_inode_link - Smack check on link
593 * @old_dentry: the existing object
595 * @new_dentry: the new object
597 * Returns 0 if access is permitted, an error code otherwise
599 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
600 struct dentry *new_dentry)
603 struct smk_audit_info ad;
606 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
607 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
609 isp = smk_of_inode(old_dentry->d_inode);
610 rc = smk_curacc(isp, MAY_WRITE, &ad);
612 if (rc == 0 && new_dentry->d_inode != NULL) {
613 isp = smk_of_inode(new_dentry->d_inode);
614 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
615 rc = smk_curacc(isp, MAY_WRITE, &ad);
622 * smack_inode_unlink - Smack check on inode deletion
623 * @dir: containing directory object
624 * @dentry: file to unlink
626 * Returns 0 if current can write the containing directory
627 * and the object, error code otherwise
629 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
631 struct inode *ip = dentry->d_inode;
632 struct smk_audit_info ad;
635 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
636 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
639 * You need write access to the thing you're unlinking
641 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
644 * You also need write access to the containing directory
646 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
647 smk_ad_setfield_u_fs_inode(&ad, dir);
648 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
654 * smack_inode_rmdir - Smack check on directory deletion
655 * @dir: containing directory object
656 * @dentry: directory to unlink
658 * Returns 0 if current can write the containing directory
659 * and the directory, error code otherwise
661 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
663 struct smk_audit_info ad;
666 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
667 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
670 * You need write access to the thing you're removing
672 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
675 * You also need write access to the containing directory
677 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
678 smk_ad_setfield_u_fs_inode(&ad, dir);
679 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
686 * smack_inode_rename - Smack check on rename
687 * @old_inode: the old directory
688 * @old_dentry: unused
689 * @new_inode: the new directory
690 * @new_dentry: unused
692 * Read and write access is required on both the old and
695 * Returns 0 if access is permitted, an error code otherwise
697 static int smack_inode_rename(struct inode *old_inode,
698 struct dentry *old_dentry,
699 struct inode *new_inode,
700 struct dentry *new_dentry)
704 struct smk_audit_info ad;
706 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
707 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
709 isp = smk_of_inode(old_dentry->d_inode);
710 rc = smk_curacc(isp, MAY_READWRITE, &ad);
712 if (rc == 0 && new_dentry->d_inode != NULL) {
713 isp = smk_of_inode(new_dentry->d_inode);
714 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
715 rc = smk_curacc(isp, MAY_READWRITE, &ad);
721 * smack_inode_permission - Smack version of permission()
722 * @inode: the inode in question
723 * @mask: the access requested
725 * This is the important Smack hook.
727 * Returns 0 if access is permitted, -EACCES otherwise
729 static int smack_inode_permission(struct inode *inode, int mask)
731 struct smk_audit_info ad;
732 int no_block = mask & MAY_NOT_BLOCK;
734 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
736 * No permission to check. Existence test. Yup, it's there.
741 /* May be droppable after audit */
744 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
745 smk_ad_setfield_u_fs_inode(&ad, inode);
746 return smk_curacc(smk_of_inode(inode), mask, &ad);
750 * smack_inode_setattr - Smack check for setting attributes
751 * @dentry: the object
752 * @iattr: for the force flag
754 * Returns 0 if access is permitted, an error code otherwise
756 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
758 struct smk_audit_info ad;
760 * Need to allow for clearing the setuid bit.
762 if (iattr->ia_valid & ATTR_FORCE)
764 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
765 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
767 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
771 * smack_inode_getattr - Smack check for getting attributes
773 * @dentry: the object
775 * Returns 0 if access is permitted, an error code otherwise
777 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
779 struct smk_audit_info ad;
782 path.dentry = dentry;
785 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
786 smk_ad_setfield_u_fs_path(&ad, path);
787 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
791 * smack_inode_setxattr - Smack check for setting xattrs
792 * @dentry: the object
793 * @name: name of the attribute
798 * This protects the Smack attribute explicitly.
800 * Returns 0 if access is permitted, an error code otherwise
802 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
803 const void *value, size_t size, int flags)
805 struct smk_audit_info ad;
808 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
809 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
810 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
811 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
812 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
813 if (!capable(CAP_MAC_ADMIN))
816 * check label validity here so import wont fail on
819 if (size == 0 || size >= SMK_LABELLEN ||
820 smk_import(value, size) == NULL)
822 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
823 if (!capable(CAP_MAC_ADMIN))
825 if (size != TRANS_TRUE_SIZE ||
826 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
829 rc = cap_inode_setxattr(dentry, name, value, size, flags);
831 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
832 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
835 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
841 * smack_inode_post_setxattr - Apply the Smack update approved above
843 * @name: attribute name
844 * @value: attribute value
845 * @size: attribute size
848 * Set the pointer in the inode blob to the entry found
849 * in the master label list.
851 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
852 const void *value, size_t size, int flags)
855 struct inode_smack *isp = dentry->d_inode->i_security;
857 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
858 nsp = smk_import(value, size);
860 isp->smk_inode = nsp;
862 isp->smk_inode = smack_known_invalid.smk_known;
863 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
864 nsp = smk_import(value, size);
868 isp->smk_task = smack_known_invalid.smk_known;
869 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
870 nsp = smk_import(value, size);
874 isp->smk_mmap = smack_known_invalid.smk_known;
875 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
876 isp->smk_flags |= SMK_INODE_TRANSMUTE;
882 * smack_inode_getxattr - Smack check on getxattr
883 * @dentry: the object
886 * Returns 0 if access is permitted, an error code otherwise
888 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
890 struct smk_audit_info ad;
892 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
893 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
895 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
899 * smack_inode_removexattr - Smack check on removexattr
900 * @dentry: the object
901 * @name: name of the attribute
903 * Removing the Smack attribute requires CAP_MAC_ADMIN
905 * Returns 0 if access is permitted, an error code otherwise
907 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
909 struct inode_smack *isp;
910 struct smk_audit_info ad;
913 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
914 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
915 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
916 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
917 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
918 strcmp(name, XATTR_NAME_SMACKMMAP)) {
919 if (!capable(CAP_MAC_ADMIN))
922 rc = cap_inode_removexattr(dentry, name);
924 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
925 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
927 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
930 isp = dentry->d_inode->i_security;
931 isp->smk_task = NULL;
932 isp->smk_mmap = NULL;
939 * smack_inode_getsecurity - get smack xattrs
941 * @name: attribute name
942 * @buffer: where to put the result
943 * @alloc: duplicate memory
945 * Returns the size of the attribute or an error code
947 static int smack_inode_getsecurity(const struct inode *inode,
948 const char *name, void **buffer,
951 struct socket_smack *ssp;
953 struct super_block *sbp;
954 struct inode *ip = (struct inode *)inode;
957 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
958 isp = smk_of_inode(inode);
961 * The rest of the Smack xattrs are only on sockets.
964 if (sbp->s_magic != SOCKFS_MAGIC)
968 if (sock == NULL || sock->sk == NULL)
971 ssp = sock->sk->sk_security;
973 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
975 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
982 *buffer = kstrdup(isp, GFP_KERNEL);
992 * smack_inode_listsecurity - list the Smack attributes
994 * @buffer: where they go
995 * @buffer_size: size of buffer
997 * Returns 0 on success, -EINVAL otherwise
999 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1002 int len = strlen(XATTR_NAME_SMACK);
1004 if (buffer != NULL && len <= buffer_size) {
1005 memcpy(buffer, XATTR_NAME_SMACK, len);
1012 * smack_inode_getsecid - Extract inode's security id
1013 * @inode: inode to extract the info from
1014 * @secid: where result will be saved
1016 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1018 struct inode_smack *isp = inode->i_security;
1020 *secid = smack_to_secid(isp->smk_inode);
1028 * smack_file_permission - Smack check on file operations
1034 * Should access checks be done on each read or write?
1035 * UNICOS and SELinux say yes.
1036 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1038 * I'll say no for now. Smack does not do the frequent
1039 * label changing that SELinux does.
1041 static int smack_file_permission(struct file *file, int mask)
1047 * smack_file_alloc_security - assign a file security blob
1050 * The security blob for a file is a pointer to the master
1051 * label list, so no allocation is done.
1055 static int smack_file_alloc_security(struct file *file)
1057 file->f_security = smk_of_current();
1062 * smack_file_free_security - clear a file security blob
1065 * The security blob for a file is a pointer to the master
1066 * label list, so no memory is freed.
1068 static void smack_file_free_security(struct file *file)
1070 file->f_security = NULL;
1074 * smack_file_ioctl - Smack check on ioctls
1079 * Relies heavily on the correct use of the ioctl command conventions.
1081 * Returns 0 if allowed, error code otherwise
1083 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1087 struct smk_audit_info ad;
1089 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1090 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1092 if (_IOC_DIR(cmd) & _IOC_WRITE)
1093 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1095 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1096 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1102 * smack_file_lock - Smack check on file locking
1106 * Returns 0 if current has write access, error code otherwise
1108 static int smack_file_lock(struct file *file, unsigned int cmd)
1110 struct smk_audit_info ad;
1112 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1113 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1114 return smk_curacc(file->f_security, MAY_WRITE, &ad);
1118 * smack_file_fcntl - Smack check on fcntl
1120 * @cmd: what action to check
1123 * Generally these operations are harmless.
1124 * File locking operations present an obvious mechanism
1125 * for passing information, so they require write access.
1127 * Returns 0 if current has access, error code otherwise
1129 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1132 struct smk_audit_info ad;
1142 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1143 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1144 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1155 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1156 * if mapping anonymous memory.
1157 * @file contains the file structure for file to map (may be NULL).
1158 * @reqprot contains the protection requested by the application.
1159 * @prot contains the protection that will be applied by the kernel.
1160 * @flags contains the operational flags.
1161 * Return 0 if permission is granted.
1163 static int smack_file_mmap(struct file *file,
1164 unsigned long reqprot, unsigned long prot,
1165 unsigned long flags, unsigned long addr,
1166 unsigned long addr_only)
1168 struct smack_known *skp;
1169 struct smack_rule *srp;
1170 struct task_smack *tsp;
1174 struct inode_smack *isp;
1181 /* do DAC check on address space usage */
1182 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1183 if (rc || addr_only)
1186 if (file == NULL || file->f_dentry == NULL)
1189 dp = file->f_dentry;
1191 if (dp->d_inode == NULL)
1194 isp = dp->d_inode->i_security;
1195 if (isp->smk_mmap == NULL)
1197 msmack = isp->smk_mmap;
1199 tsp = current_security();
1200 sp = smk_of_current();
1201 skp = smk_find_entry(sp);
1206 * For each Smack rule associated with the subject
1207 * label verify that the SMACK64MMAP also has access
1208 * to that rule's object label.
1210 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1211 osmack = srp->smk_object;
1213 * Matching labels always allows access.
1215 if (msmack == osmack)
1218 * If there is a matching local rule take
1219 * that into account as well.
1221 may = smk_access_entry(srp->smk_subject, osmack,
1224 may = srp->smk_access;
1226 may &= srp->smk_access;
1228 * If may is zero the SMACK64MMAP subject can't
1229 * possibly have less access.
1235 * Fetch the global list entry.
1236 * If there isn't one a SMACK64MMAP subject
1237 * can't have as much access as current.
1239 skp = smk_find_entry(msmack);
1240 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
1241 if (mmay == -ENOENT) {
1246 * If there is a local entry it modifies the
1247 * potential access, too.
1249 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1250 if (tmay != -ENOENT)
1254 * If there is any access available to current that is
1255 * not available to a SMACK64MMAP subject
1258 if ((may | mmay) != mmay) {
1270 * smack_file_set_fowner - set the file security blob value
1271 * @file: object in question
1274 * Further research may be required on this one.
1276 static int smack_file_set_fowner(struct file *file)
1278 file->f_security = smk_of_current();
1283 * smack_file_send_sigiotask - Smack on sigio
1284 * @tsk: The target task
1285 * @fown: the object the signal come from
1288 * Allow a privileged task to get signals even if it shouldn't
1290 * Returns 0 if a subject with the object's smack could
1291 * write to the task, an error code otherwise.
1293 static int smack_file_send_sigiotask(struct task_struct *tsk,
1294 struct fown_struct *fown, int signum)
1298 char *tsp = smk_of_task(tsk->cred->security);
1299 struct smk_audit_info ad;
1302 * struct fown_struct is never outside the context of a struct file
1304 file = container_of(fown, struct file, f_owner);
1306 /* we don't log here as rc can be overriden */
1307 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
1308 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1311 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1312 smk_ad_setfield_u_tsk(&ad, tsk);
1313 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
1318 * smack_file_receive - Smack file receive check
1321 * Returns 0 if current has access, error code otherwise
1323 static int smack_file_receive(struct file *file)
1326 struct smk_audit_info ad;
1328 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1329 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1331 * This code relies on bitmasks.
1333 if (file->f_mode & FMODE_READ)
1335 if (file->f_mode & FMODE_WRITE)
1338 return smk_curacc(file->f_security, may, &ad);
1342 * smack_dentry_open - Smack dentry open processing
1346 * Set the security blob in the file structure.
1350 static int smack_dentry_open(struct file *file, const struct cred *cred)
1352 struct inode_smack *isp = file->f_path.dentry->d_inode->i_security;
1354 file->f_security = isp->smk_inode;
1364 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1365 * @new: the new credentials
1366 * @gfp: the atomicity of any memory allocations
1368 * Prepare a blank set of credentials for modification. This must allocate all
1369 * the memory the LSM module might require such that cred_transfer() can
1370 * complete without error.
1372 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1374 struct task_smack *tsp;
1376 tsp = new_task_smack(NULL, NULL, gfp);
1380 cred->security = tsp;
1387 * smack_cred_free - "free" task-level security credentials
1388 * @cred: the credentials in question
1391 static void smack_cred_free(struct cred *cred)
1393 struct task_smack *tsp = cred->security;
1394 struct smack_rule *rp;
1395 struct list_head *l;
1396 struct list_head *n;
1400 cred->security = NULL;
1402 list_for_each_safe(l, n, &tsp->smk_rules) {
1403 rp = list_entry(l, struct smack_rule, list);
1404 list_del(&rp->list);
1411 * smack_cred_prepare - prepare new set of credentials for modification
1412 * @new: the new credentials
1413 * @old: the original credentials
1414 * @gfp: the atomicity of any memory allocations
1416 * Prepare a new set of credentials for modification.
1418 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1421 struct task_smack *old_tsp = old->security;
1422 struct task_smack *new_tsp;
1425 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1426 if (new_tsp == NULL)
1429 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1433 new->security = new_tsp;
1438 * smack_cred_transfer - Transfer the old credentials to the new credentials
1439 * @new: the new credentials
1440 * @old: the original credentials
1442 * Fill in a set of blank credentials from another set of credentials.
1444 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1446 struct task_smack *old_tsp = old->security;
1447 struct task_smack *new_tsp = new->security;
1449 new_tsp->smk_task = old_tsp->smk_task;
1450 new_tsp->smk_forked = old_tsp->smk_task;
1451 mutex_init(&new_tsp->smk_rules_lock);
1452 INIT_LIST_HEAD(&new_tsp->smk_rules);
1455 /* cbs copy rule list */
1459 * smack_kernel_act_as - Set the subjective context in a set of credentials
1460 * @new: points to the set of credentials to be modified.
1461 * @secid: specifies the security ID to be set
1463 * Set the security data for a kernel service.
1465 static int smack_kernel_act_as(struct cred *new, u32 secid)
1467 struct task_smack *new_tsp = new->security;
1468 char *smack = smack_from_secid(secid);
1473 new_tsp->smk_task = smack;
1478 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1479 * @new: points to the set of credentials to be modified
1480 * @inode: points to the inode to use as a reference
1482 * Set the file creation context in a set of credentials to the same
1483 * as the objective context of the specified inode
1485 static int smack_kernel_create_files_as(struct cred *new,
1486 struct inode *inode)
1488 struct inode_smack *isp = inode->i_security;
1489 struct task_smack *tsp = new->security;
1491 tsp->smk_forked = isp->smk_inode;
1492 tsp->smk_task = isp->smk_inode;
1497 * smk_curacc_on_task - helper to log task related access
1498 * @p: the task object
1499 * @access: the access requested
1500 * @caller: name of the calling function for audit
1502 * Return 0 if access is permitted
1504 static int smk_curacc_on_task(struct task_struct *p, int access,
1507 struct smk_audit_info ad;
1509 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1510 smk_ad_setfield_u_tsk(&ad, p);
1511 return smk_curacc(smk_of_task_struct(p), access, &ad);
1515 * smack_task_setpgid - Smack check on setting pgid
1516 * @p: the task object
1519 * Return 0 if write access is permitted
1521 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1523 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1527 * smack_task_getpgid - Smack access check for getpgid
1528 * @p: the object task
1530 * Returns 0 if current can read the object task, error code otherwise
1532 static int smack_task_getpgid(struct task_struct *p)
1534 return smk_curacc_on_task(p, MAY_READ, __func__);
1538 * smack_task_getsid - Smack access check for getsid
1539 * @p: the object task
1541 * Returns 0 if current can read the object task, error code otherwise
1543 static int smack_task_getsid(struct task_struct *p)
1545 return smk_curacc_on_task(p, MAY_READ, __func__);
1549 * smack_task_getsecid - get the secid of the task
1550 * @p: the object task
1551 * @secid: where to put the result
1553 * Sets the secid to contain a u32 version of the smack label.
1555 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1557 *secid = smack_to_secid(smk_of_task_struct(p));
1561 * smack_task_setnice - Smack check on setting nice
1562 * @p: the task object
1565 * Return 0 if write access is permitted
1567 static int smack_task_setnice(struct task_struct *p, int nice)
1571 rc = cap_task_setnice(p, nice);
1573 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1578 * smack_task_setioprio - Smack check on setting ioprio
1579 * @p: the task object
1582 * Return 0 if write access is permitted
1584 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1588 rc = cap_task_setioprio(p, ioprio);
1590 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1595 * smack_task_getioprio - Smack check on reading ioprio
1596 * @p: the task object
1598 * Return 0 if read access is permitted
1600 static int smack_task_getioprio(struct task_struct *p)
1602 return smk_curacc_on_task(p, MAY_READ, __func__);
1606 * smack_task_setscheduler - Smack check on setting scheduler
1607 * @p: the task object
1611 * Return 0 if read access is permitted
1613 static int smack_task_setscheduler(struct task_struct *p)
1617 rc = cap_task_setscheduler(p);
1619 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1624 * smack_task_getscheduler - Smack check on reading scheduler
1625 * @p: the task object
1627 * Return 0 if read access is permitted
1629 static int smack_task_getscheduler(struct task_struct *p)
1631 return smk_curacc_on_task(p, MAY_READ, __func__);
1635 * smack_task_movememory - Smack check on moving memory
1636 * @p: the task object
1638 * Return 0 if write access is permitted
1640 static int smack_task_movememory(struct task_struct *p)
1642 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1646 * smack_task_kill - Smack check on signal delivery
1647 * @p: the task object
1650 * @secid: identifies the smack to use in lieu of current's
1652 * Return 0 if write access is permitted
1654 * The secid behavior is an artifact of an SELinux hack
1655 * in the USB code. Someday it may go away.
1657 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1660 struct smk_audit_info ad;
1662 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1663 smk_ad_setfield_u_tsk(&ad, p);
1665 * Sending a signal requires that the sender
1666 * can write the receiver.
1669 return smk_curacc(smk_of_task_struct(p), MAY_WRITE,
1672 * If the secid isn't 0 we're dealing with some USB IO
1673 * specific behavior. This is not clean. For one thing
1674 * we can't take privilege into account.
1676 return smk_access(smack_from_secid(secid),
1677 smk_of_task_struct(p), MAY_WRITE, &ad);
1681 * smack_task_wait - Smack access check for waiting
1682 * @p: task to wait for
1684 * Returns 0 if current can wait for p, error code otherwise
1686 static int smack_task_wait(struct task_struct *p)
1688 struct smk_audit_info ad;
1689 char *sp = smk_of_current();
1694 tsp = smk_of_forked(__task_cred(p)->security);
1697 /* we don't log here, we can be overriden */
1698 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
1703 * Allow the operation to succeed if either task
1704 * has privilege to perform operations that might
1705 * account for the smack labels having gotten to
1706 * be different in the first place.
1708 * This breaks the strict subject/object access
1709 * control ideal, taking the object's privilege
1710 * state into account in the decision as well as
1713 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1715 /* we log only if we didn't get overriden */
1717 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1718 smk_ad_setfield_u_tsk(&ad, p);
1719 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
1724 * smack_task_to_inode - copy task smack into the inode blob
1725 * @p: task to copy from
1726 * @inode: inode to copy to
1728 * Sets the smack pointer in the inode security blob
1730 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1732 struct inode_smack *isp = inode->i_security;
1733 isp->smk_inode = smk_of_task_struct(p);
1741 * smack_sk_alloc_security - Allocate a socket blob
1744 * @gfp_flags: memory allocation flags
1746 * Assign Smack pointers to current
1748 * Returns 0 on success, -ENOMEM is there's no memory
1750 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1752 char *csp = smk_of_current();
1753 struct socket_smack *ssp;
1755 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1761 ssp->smk_packet = NULL;
1763 sk->sk_security = ssp;
1769 * smack_sk_free_security - Free a socket blob
1772 * Clears the blob pointer
1774 static void smack_sk_free_security(struct sock *sk)
1776 kfree(sk->sk_security);
1780 * smack_host_label - check host based restrictions
1781 * @sip: the object end
1783 * looks for host based access restrictions
1785 * This version will only be appropriate for really small sets of single label
1786 * hosts. The caller is responsible for ensuring that the RCU read lock is
1787 * taken before calling this function.
1789 * Returns the label of the far end or NULL if it's not special.
1791 static char *smack_host_label(struct sockaddr_in *sip)
1793 struct smk_netlbladdr *snp;
1794 struct in_addr *siap = &sip->sin_addr;
1796 if (siap->s_addr == 0)
1799 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1801 * we break after finding the first match because
1802 * the list is sorted from longest to shortest mask
1803 * so we have found the most specific match
1805 if ((&snp->smk_host.sin_addr)->s_addr ==
1806 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1807 /* we have found the special CIPSO option */
1808 if (snp->smk_label == smack_cipso_option)
1810 return snp->smk_label;
1817 * smack_set_catset - convert a capset to netlabel mls categories
1818 * @catset: the Smack categories
1819 * @sap: where to put the netlabel categories
1821 * Allocates and fills attr.mls.cat
1823 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1834 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1835 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1836 sap->attr.mls.cat->startbit = 0;
1838 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1839 for (m = 0x80; m != 0; m >>= 1, cat++) {
1842 rc = netlbl_secattr_catmap_setbit(&sap->attr.mls.cat,
1848 * smack_to_secattr - fill a secattr from a smack value
1849 * @smack: the smack value
1850 * @nlsp: where the result goes
1852 * Casey says that CIPSO is good enough for now.
1853 * It can be used to effect.
1854 * It can also be abused to effect when necessary.
1855 * Apologies to the TSIG group in general and GW in particular.
1857 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1859 struct smack_cipso cipso;
1862 nlsp->domain = smack;
1863 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1865 rc = smack_to_cipso(smack, &cipso);
1867 nlsp->attr.mls.lvl = cipso.smk_level;
1868 smack_set_catset(cipso.smk_catset, nlsp);
1870 nlsp->attr.mls.lvl = smack_cipso_direct;
1871 smack_set_catset(smack, nlsp);
1876 * smack_netlabel - Set the secattr on a socket
1878 * @labeled: socket label scheme
1880 * Convert the outbound smack value (smk_out) to a
1881 * secattr and attach it to the socket.
1883 * Returns 0 on success or an error code
1885 static int smack_netlabel(struct sock *sk, int labeled)
1887 struct socket_smack *ssp = sk->sk_security;
1888 struct netlbl_lsm_secattr secattr;
1892 * Usually the netlabel code will handle changing the
1893 * packet labeling based on the label.
1894 * The case of a single label host is different, because
1895 * a single label host should never get a labeled packet
1896 * even though the label is usually associated with a packet
1900 bh_lock_sock_nested(sk);
1902 if (ssp->smk_out == smack_net_ambient ||
1903 labeled == SMACK_UNLABELED_SOCKET)
1904 netlbl_sock_delattr(sk);
1906 netlbl_secattr_init(&secattr);
1907 smack_to_secattr(ssp->smk_out, &secattr);
1908 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
1909 netlbl_secattr_destroy(&secattr);
1919 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1921 * @sap: the destination address
1923 * Set the correct secattr for the given socket based on the destination
1924 * address and perform any outbound access checks needed.
1926 * Returns 0 on success or an error code.
1929 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1934 struct socket_smack *ssp = sk->sk_security;
1935 struct smk_audit_info ad;
1938 hostsp = smack_host_label(sap);
1939 if (hostsp != NULL) {
1940 sk_lbl = SMACK_UNLABELED_SOCKET;
1942 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1943 ad.a.u.net.family = sap->sin_family;
1944 ad.a.u.net.dport = sap->sin_port;
1945 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1947 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
1949 sk_lbl = SMACK_CIPSO_SOCKET;
1956 return smack_netlabel(sk, sk_lbl);
1960 * smack_inode_setsecurity - set smack xattrs
1961 * @inode: the object
1962 * @name: attribute name
1963 * @value: attribute value
1964 * @size: size of the attribute
1967 * Sets the named attribute in the appropriate blob
1969 * Returns 0 on success, or an error code
1971 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1972 const void *value, size_t size, int flags)
1975 struct inode_smack *nsp = inode->i_security;
1976 struct socket_smack *ssp;
1977 struct socket *sock;
1980 if (value == NULL || size > SMK_LABELLEN || size == 0)
1983 sp = smk_import(value, size);
1987 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1988 nsp->smk_inode = sp;
1989 nsp->smk_flags |= SMK_INODE_INSTANT;
1993 * The rest of the Smack xattrs are only on sockets.
1995 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1998 sock = SOCKET_I(inode);
1999 if (sock == NULL || sock->sk == NULL)
2002 ssp = sock->sk->sk_security;
2004 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2006 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2008 if (sock->sk->sk_family != PF_UNIX) {
2009 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2012 "Smack: \"%s\" netlbl error %d.\n",
2022 * smack_socket_post_create - finish socket setup
2024 * @family: protocol family
2029 * Sets the netlabel information on the socket
2031 * Returns 0 on success, and error code otherwise
2033 static int smack_socket_post_create(struct socket *sock, int family,
2034 int type, int protocol, int kern)
2036 if (family != PF_INET || sock->sk == NULL)
2039 * Set the outbound netlbl.
2041 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2045 * smack_socket_connect - connect access check
2047 * @sap: the other end
2048 * @addrlen: size of sap
2050 * Verifies that a connection may be possible
2052 * Returns 0 on success, and error code otherwise
2054 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2057 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
2059 if (addrlen < sizeof(struct sockaddr_in))
2062 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2066 * smack_flags_to_may - convert S_ to MAY_ values
2067 * @flags: the S_ value
2069 * Returns the equivalent MAY_ value
2071 static int smack_flags_to_may(int flags)
2075 if (flags & S_IRUGO)
2077 if (flags & S_IWUGO)
2079 if (flags & S_IXUGO)
2086 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2091 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2093 msg->security = smk_of_current();
2098 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2101 * Clears the blob pointer
2103 static void smack_msg_msg_free_security(struct msg_msg *msg)
2105 msg->security = NULL;
2109 * smack_of_shm - the smack pointer for the shm
2112 * Returns a pointer to the smack value
2114 static char *smack_of_shm(struct shmid_kernel *shp)
2116 return (char *)shp->shm_perm.security;
2120 * smack_shm_alloc_security - Set the security blob for shm
2125 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2127 struct kern_ipc_perm *isp = &shp->shm_perm;
2129 isp->security = smk_of_current();
2134 * smack_shm_free_security - Clear the security blob for shm
2137 * Clears the blob pointer
2139 static void smack_shm_free_security(struct shmid_kernel *shp)
2141 struct kern_ipc_perm *isp = &shp->shm_perm;
2143 isp->security = NULL;
2147 * smk_curacc_shm : check if current has access on shm
2149 * @access : access requested
2151 * Returns 0 if current has the requested access, error code otherwise
2153 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2155 char *ssp = smack_of_shm(shp);
2156 struct smk_audit_info ad;
2159 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2160 ad.a.u.ipc_id = shp->shm_perm.id;
2162 return smk_curacc(ssp, access, &ad);
2166 * smack_shm_associate - Smack access check for shm
2168 * @shmflg: access requested
2170 * Returns 0 if current has the requested access, error code otherwise
2172 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2176 may = smack_flags_to_may(shmflg);
2177 return smk_curacc_shm(shp, may);
2181 * smack_shm_shmctl - Smack access check for shm
2183 * @cmd: what it wants to do
2185 * Returns 0 if current has the requested access, error code otherwise
2187 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2200 may = MAY_READWRITE;
2205 * System level information.
2211 return smk_curacc_shm(shp, may);
2215 * smack_shm_shmat - Smack access for shmat
2218 * @shmflg: access requested
2220 * Returns 0 if current has the requested access, error code otherwise
2222 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2227 may = smack_flags_to_may(shmflg);
2228 return smk_curacc_shm(shp, may);
2232 * smack_of_sem - the smack pointer for the sem
2235 * Returns a pointer to the smack value
2237 static char *smack_of_sem(struct sem_array *sma)
2239 return (char *)sma->sem_perm.security;
2243 * smack_sem_alloc_security - Set the security blob for sem
2248 static int smack_sem_alloc_security(struct sem_array *sma)
2250 struct kern_ipc_perm *isp = &sma->sem_perm;
2252 isp->security = smk_of_current();
2257 * smack_sem_free_security - Clear the security blob for sem
2260 * Clears the blob pointer
2262 static void smack_sem_free_security(struct sem_array *sma)
2264 struct kern_ipc_perm *isp = &sma->sem_perm;
2266 isp->security = NULL;
2270 * smk_curacc_sem : check if current has access on sem
2272 * @access : access requested
2274 * Returns 0 if current has the requested access, error code otherwise
2276 static int smk_curacc_sem(struct sem_array *sma, int access)
2278 char *ssp = smack_of_sem(sma);
2279 struct smk_audit_info ad;
2282 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2283 ad.a.u.ipc_id = sma->sem_perm.id;
2285 return smk_curacc(ssp, access, &ad);
2289 * smack_sem_associate - Smack access check for sem
2291 * @semflg: access requested
2293 * Returns 0 if current has the requested access, error code otherwise
2295 static int smack_sem_associate(struct sem_array *sma, int semflg)
2299 may = smack_flags_to_may(semflg);
2300 return smk_curacc_sem(sma, may);
2304 * smack_sem_shmctl - Smack access check for sem
2306 * @cmd: what it wants to do
2308 * Returns 0 if current has the requested access, error code otherwise
2310 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2328 may = MAY_READWRITE;
2333 * System level information
2340 return smk_curacc_sem(sma, may);
2344 * smack_sem_semop - Smack checks of semaphore operations
2350 * Treated as read and write in all cases.
2352 * Returns 0 if access is allowed, error code otherwise
2354 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2355 unsigned nsops, int alter)
2357 return smk_curacc_sem(sma, MAY_READWRITE);
2361 * smack_msg_alloc_security - Set the security blob for msg
2366 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2368 struct kern_ipc_perm *kisp = &msq->q_perm;
2370 kisp->security = smk_of_current();
2375 * smack_msg_free_security - Clear the security blob for msg
2378 * Clears the blob pointer
2380 static void smack_msg_queue_free_security(struct msg_queue *msq)
2382 struct kern_ipc_perm *kisp = &msq->q_perm;
2384 kisp->security = NULL;
2388 * smack_of_msq - the smack pointer for the msq
2391 * Returns a pointer to the smack value
2393 static char *smack_of_msq(struct msg_queue *msq)
2395 return (char *)msq->q_perm.security;
2399 * smk_curacc_msq : helper to check if current has access on msq
2401 * @access : access requested
2403 * return 0 if current has access, error otherwise
2405 static int smk_curacc_msq(struct msg_queue *msq, int access)
2407 char *msp = smack_of_msq(msq);
2408 struct smk_audit_info ad;
2411 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2412 ad.a.u.ipc_id = msq->q_perm.id;
2414 return smk_curacc(msp, access, &ad);
2418 * smack_msg_queue_associate - Smack access check for msg_queue
2420 * @msqflg: access requested
2422 * Returns 0 if current has the requested access, error code otherwise
2424 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2428 may = smack_flags_to_may(msqflg);
2429 return smk_curacc_msq(msq, may);
2433 * smack_msg_queue_msgctl - Smack access check for msg_queue
2435 * @cmd: what it wants to do
2437 * Returns 0 if current has the requested access, error code otherwise
2439 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2450 may = MAY_READWRITE;
2455 * System level information
2462 return smk_curacc_msq(msq, may);
2466 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2469 * @msqflg: access requested
2471 * Returns 0 if current has the requested access, error code otherwise
2473 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2478 may = smack_flags_to_may(msqflg);
2479 return smk_curacc_msq(msq, may);
2483 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2490 * Returns 0 if current has read and write access, error code otherwise
2492 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2493 struct task_struct *target, long type, int mode)
2495 return smk_curacc_msq(msq, MAY_READWRITE);
2499 * smack_ipc_permission - Smack access for ipc_permission()
2500 * @ipp: the object permissions
2501 * @flag: access requested
2503 * Returns 0 if current has read and write access, error code otherwise
2505 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2507 char *isp = ipp->security;
2508 int may = smack_flags_to_may(flag);
2509 struct smk_audit_info ad;
2512 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2513 ad.a.u.ipc_id = ipp->id;
2515 return smk_curacc(isp, may, &ad);
2519 * smack_ipc_getsecid - Extract smack security id
2520 * @ipp: the object permissions
2521 * @secid: where result will be saved
2523 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2525 char *smack = ipp->security;
2527 *secid = smack_to_secid(smack);
2531 * smack_d_instantiate - Make sure the blob is correct on an inode
2532 * @opt_dentry: dentry where inode will be attached
2533 * @inode: the object
2535 * Set the inode's security blob if it hasn't been done already.
2537 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2539 struct super_block *sbp;
2540 struct superblock_smack *sbsp;
2541 struct inode_smack *isp;
2542 char *csp = smk_of_current();
2545 char trattr[TRANS_TRUE_SIZE];
2552 isp = inode->i_security;
2554 mutex_lock(&isp->smk_lock);
2556 * If the inode is already instantiated
2557 * take the quick way out
2559 if (isp->smk_flags & SMK_INODE_INSTANT)
2563 sbsp = sbp->s_security;
2565 * We're going to use the superblock default label
2566 * if there's no label on the file.
2568 final = sbsp->smk_default;
2571 * If this is the root inode the superblock
2572 * may be in the process of initialization.
2573 * If that is the case use the root value out
2574 * of the superblock.
2576 if (opt_dentry->d_parent == opt_dentry) {
2577 isp->smk_inode = sbsp->smk_root;
2578 isp->smk_flags |= SMK_INODE_INSTANT;
2583 * This is pretty hackish.
2584 * Casey says that we shouldn't have to do
2585 * file system specific code, but it does help
2586 * with keeping it simple.
2588 switch (sbp->s_magic) {
2591 * Casey says that it's a little embarrassing
2592 * that the smack file system doesn't do
2593 * extended attributes.
2595 final = smack_known_star.smk_known;
2599 * Casey says pipes are easy (?)
2601 final = smack_known_star.smk_known;
2603 case DEVPTS_SUPER_MAGIC:
2605 * devpts seems content with the label of the task.
2606 * Programs that change smack have to treat the
2613 * Socket access is controlled by the socket
2614 * structures associated with the task involved.
2616 final = smack_known_star.smk_known;
2618 case PROC_SUPER_MAGIC:
2620 * Casey says procfs appears not to care.
2621 * The superblock default suffices.
2626 * Device labels should come from the filesystem,
2627 * but watch out, because they're volitile,
2628 * getting recreated on every reboot.
2630 final = smack_known_star.smk_known;
2634 * If a smack value has been set we want to use it,
2635 * but since tmpfs isn't giving us the opportunity
2636 * to set mount options simulate setting the
2637 * superblock default.
2641 * This isn't an understood special case.
2642 * Get the value from the xattr.
2646 * UNIX domain sockets use lower level socket data.
2648 if (S_ISSOCK(inode->i_mode)) {
2649 final = smack_known_star.smk_known;
2653 * No xattr support means, alas, no SMACK label.
2654 * Use the aforeapplied default.
2655 * It would be curious if the label of the task
2656 * does not match that assigned.
2658 if (inode->i_op->getxattr == NULL)
2661 * Get the dentry for xattr.
2663 dp = dget(opt_dentry);
2664 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2665 if (fetched != NULL) {
2667 if (S_ISDIR(inode->i_mode)) {
2669 inode->i_op->getxattr(dp,
2670 XATTR_NAME_SMACKTRANSMUTE,
2671 trattr, TRANS_TRUE_SIZE);
2672 if (strncmp(trattr, TRANS_TRUE,
2673 TRANS_TRUE_SIZE) == 0)
2674 transflag = SMK_INODE_TRANSMUTE;
2677 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2678 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2685 isp->smk_inode = csp;
2687 isp->smk_inode = final;
2689 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2692 mutex_unlock(&isp->smk_lock);
2697 * smack_getprocattr - Smack process attribute access
2698 * @p: the object task
2699 * @name: the name of the attribute in /proc/.../attr
2700 * @value: where to put the result
2702 * Places a copy of the task Smack into value
2704 * Returns the length of the smack label or an error code
2706 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2711 if (strcmp(name, "current") != 0)
2714 cp = kstrdup(smk_of_task_struct(p), GFP_KERNEL);
2724 * smack_setprocattr - Smack process attribute setting
2725 * @p: the object task
2726 * @name: the name of the attribute in /proc/.../attr
2727 * @value: the value to set
2728 * @size: the size of the value
2730 * Sets the Smack value of the task. Only setting self
2731 * is permitted and only with privilege
2733 * Returns the length of the smack label or an error code
2735 static int smack_setprocattr(struct task_struct *p, char *name,
2736 void *value, size_t size)
2739 struct task_smack *tsp;
2740 struct task_smack *oldtsp;
2745 * Changing another process' Smack value is too dangerous
2746 * and supports no sane use case.
2751 if (!capable(CAP_MAC_ADMIN))
2754 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2757 if (strcmp(name, "current") != 0)
2760 newsmack = smk_import(value, size);
2761 if (newsmack == NULL)
2765 * No process is ever allowed the web ("@") label.
2767 if (newsmack == smack_known_web.smk_known)
2770 oldtsp = p->cred->security;
2771 new = prepare_creds();
2775 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
2780 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2784 new->security = tsp;
2790 * smack_unix_stream_connect - Smack access on UDS
2792 * @other: the other sock
2795 * Return 0 if a subject with the smack of sock could access
2796 * an object with the smack of other, otherwise an error code
2798 static int smack_unix_stream_connect(struct sock *sock,
2799 struct sock *other, struct sock *newsk)
2801 struct socket_smack *ssp = sock->sk_security;
2802 struct socket_smack *osp = other->sk_security;
2803 struct socket_smack *nsp = newsk->sk_security;
2804 struct smk_audit_info ad;
2807 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2808 smk_ad_setfield_u_net_sk(&ad, other);
2810 if (!capable(CAP_MAC_OVERRIDE))
2811 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2814 * Cross reference the peer labels for SO_PEERSEC.
2817 nsp->smk_packet = ssp->smk_out;
2818 ssp->smk_packet = osp->smk_out;
2825 * smack_unix_may_send - Smack access on UDS
2827 * @other: the other socket
2829 * Return 0 if a subject with the smack of sock could access
2830 * an object with the smack of other, otherwise an error code
2832 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2834 struct socket_smack *ssp = sock->sk->sk_security;
2835 struct socket_smack *osp = other->sk->sk_security;
2836 struct smk_audit_info ad;
2839 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2840 smk_ad_setfield_u_net_sk(&ad, other->sk);
2842 if (!capable(CAP_MAC_OVERRIDE))
2843 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2849 * smack_socket_sendmsg - Smack check based on destination host
2852 * @size: the size of the message
2854 * Return 0 if the current subject can write to the destination
2855 * host. This is only a question if the destination is a single
2858 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2861 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2864 * Perfectly reasonable for this to be NULL
2866 if (sip == NULL || sip->sin_family != AF_INET)
2869 return smack_netlabel_send(sock->sk, sip);
2873 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2874 * @sap: netlabel secattr
2875 * @ssp: socket security information
2877 * Returns a pointer to a Smack label found on the label list.
2879 static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
2880 struct socket_smack *ssp)
2882 struct smack_known *skp;
2883 char smack[SMK_LABELLEN];
2887 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2889 * Looks like a CIPSO packet.
2890 * If there are flags but no level netlabel isn't
2891 * behaving the way we expect it to.
2893 * Get the categories, if any
2894 * Without guidance regarding the smack value
2895 * for the packet fall back on the network
2898 memset(smack, '\0', SMK_LABELLEN);
2899 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2901 pcat = netlbl_secattr_catmap_walk(
2902 sap->attr.mls.cat, pcat + 1);
2905 smack_catset_bit(pcat, smack);
2908 * If it is CIPSO using smack direct mapping
2909 * we are already done. WeeHee.
2911 if (sap->attr.mls.lvl == smack_cipso_direct) {
2913 * The label sent is usually on the label list.
2915 * If it is not we may still want to allow the
2918 * If the recipient is accepting all packets
2919 * because it is using the star ("*") label
2920 * for SMACK64IPIN provide the web ("@") label
2921 * so that a directed response will succeed.
2922 * This is not very correct from a MAC point
2923 * of view, but gets around the problem that
2924 * locking prevents adding the newly discovered
2925 * label to the list.
2926 * The case where the recipient is not using
2927 * the star label should obviously fail.
2928 * The easy way to do this is to provide the
2929 * star label as the subject label.
2931 skp = smk_find_entry(smack);
2933 return skp->smk_known;
2935 ssp->smk_in == smack_known_star.smk_known)
2936 return smack_known_web.smk_known;
2937 return smack_known_star.smk_known;
2940 * Look it up in the supplied table if it is not
2943 sp = smack_from_cipso(sap->attr.mls.lvl, smack);
2946 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
2947 return smack_known_web.smk_known;
2948 return smack_known_star.smk_known;
2950 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2952 * Looks like a fallback, which gives us a secid.
2954 sp = smack_from_secid(sap->attr.secid);
2956 * This has got to be a bug because it is
2957 * impossible to specify a fallback without
2958 * specifying the label, which will ensure
2959 * it has a secid, and the only way to get a
2960 * secid is from a fallback.
2966 * Without guidance regarding the smack value
2967 * for the packet fall back on the network
2970 return smack_net_ambient;
2974 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2978 * Returns 0 if the packet should be delivered, an error code otherwise
2980 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2982 struct netlbl_lsm_secattr secattr;
2983 struct socket_smack *ssp = sk->sk_security;
2986 struct smk_audit_info ad;
2987 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2991 * Translate what netlabel gave us.
2993 netlbl_secattr_init(&secattr);
2995 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2997 csp = smack_from_secattr(&secattr, ssp);
2999 csp = smack_net_ambient;
3001 netlbl_secattr_destroy(&secattr);
3004 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3005 ad.a.u.net.family = sk->sk_family;
3006 ad.a.u.net.netif = skb->skb_iif;
3007 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3010 * Receiving a packet requires that the other end
3011 * be able to write here. Read access is not required.
3012 * This is the simplist possible security model
3015 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
3017 netlbl_skbuff_err(skb, rc, 0);
3022 * smack_socket_getpeersec_stream - pull in packet label
3024 * @optval: user's destination
3025 * @optlen: size thereof
3028 * returns zero on success, an error code otherwise
3030 static int smack_socket_getpeersec_stream(struct socket *sock,
3031 char __user *optval,
3032 int __user *optlen, unsigned len)
3034 struct socket_smack *ssp;
3039 ssp = sock->sk->sk_security;
3040 if (ssp->smk_packet != NULL) {
3041 rcp = ssp->smk_packet;
3042 slen = strlen(rcp) + 1;
3047 else if (copy_to_user(optval, rcp, slen) != 0)
3050 if (put_user(slen, optlen) != 0)
3058 * smack_socket_getpeersec_dgram - pull in packet label
3059 * @sock: the peer socket
3061 * @secid: pointer to where to put the secid of the packet
3063 * Sets the netlabel socket state on sk from parent
3065 static int smack_socket_getpeersec_dgram(struct socket *sock,
3066 struct sk_buff *skb, u32 *secid)
3069 struct netlbl_lsm_secattr secattr;
3070 struct socket_smack *ssp = NULL;
3072 int family = PF_UNSPEC;
3073 u32 s = 0; /* 0 is the invalid secid */
3077 if (skb->protocol == htons(ETH_P_IP))
3079 else if (skb->protocol == htons(ETH_P_IPV6))
3082 if (family == PF_UNSPEC && sock != NULL)
3083 family = sock->sk->sk_family;
3085 if (family == PF_UNIX) {
3086 ssp = sock->sk->sk_security;
3087 s = smack_to_secid(ssp->smk_out);
3088 } else if (family == PF_INET || family == PF_INET6) {
3090 * Translate what netlabel gave us.
3092 if (sock != NULL && sock->sk != NULL)
3093 ssp = sock->sk->sk_security;
3094 netlbl_secattr_init(&secattr);
3095 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3097 sp = smack_from_secattr(&secattr, ssp);
3098 s = smack_to_secid(sp);
3100 netlbl_secattr_destroy(&secattr);
3109 * smack_sock_graft - Initialize a newly created socket with an existing sock
3111 * @parent: parent socket
3113 * Set the smk_{in,out} state of an existing sock based on the process that
3114 * is creating the new socket.
3116 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3118 struct socket_smack *ssp;
3121 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3124 ssp = sk->sk_security;
3125 ssp->smk_in = ssp->smk_out = smk_of_current();
3126 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3130 * smack_inet_conn_request - Smack access check on connect
3131 * @sk: socket involved
3135 * Returns 0 if a task with the packet label could write to
3136 * the socket, otherwise an error code
3138 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3139 struct request_sock *req)
3141 u16 family = sk->sk_family;
3142 struct socket_smack *ssp = sk->sk_security;
3143 struct netlbl_lsm_secattr secattr;
3144 struct sockaddr_in addr;
3148 struct smk_audit_info ad;
3150 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3151 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3154 netlbl_secattr_init(&secattr);
3155 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3157 sp = smack_from_secattr(&secattr, ssp);
3159 sp = smack_known_huh.smk_known;
3160 netlbl_secattr_destroy(&secattr);
3163 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3164 ad.a.u.net.family = family;
3165 ad.a.u.net.netif = skb->skb_iif;
3166 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3169 * Receiving a packet requires that the other end be able to write
3170 * here. Read access is not required.
3172 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
3177 * Save the peer's label in the request_sock so we can later setup
3178 * smk_packet in the child socket so that SO_PEERCRED can report it.
3180 req->peer_secid = smack_to_secid(sp);
3183 * We need to decide if we want to label the incoming connection here
3184 * if we do we only need to label the request_sock and the stack will
3185 * propagate the wire-label to the sock when it is created.
3188 addr.sin_addr.s_addr = hdr->saddr;
3190 if (smack_host_label(&addr) == NULL) {
3192 netlbl_secattr_init(&secattr);
3193 smack_to_secattr(sp, &secattr);
3194 rc = netlbl_req_setattr(req, &secattr);
3195 netlbl_secattr_destroy(&secattr);
3198 netlbl_req_delattr(req);
3205 * smack_inet_csk_clone - Copy the connection information to the new socket
3206 * @sk: the new socket
3207 * @req: the connection's request_sock
3209 * Transfer the connection's peer label to the newly created socket.
3211 static void smack_inet_csk_clone(struct sock *sk,
3212 const struct request_sock *req)
3214 struct socket_smack *ssp = sk->sk_security;
3216 if (req->peer_secid != 0)
3217 ssp->smk_packet = smack_from_secid(req->peer_secid);
3219 ssp->smk_packet = NULL;
3223 * Key management security hooks
3225 * Casey has not tested key support very heavily.
3226 * The permission check is most likely too restrictive.
3227 * If you care about keys please have a look.
3232 * smack_key_alloc - Set the key security blob
3234 * @cred: the credentials to use
3237 * No allocation required
3241 static int smack_key_alloc(struct key *key, const struct cred *cred,
3242 unsigned long flags)
3244 key->security = smk_of_task(cred->security);
3249 * smack_key_free - Clear the key security blob
3252 * Clear the blob pointer
3254 static void smack_key_free(struct key *key)
3256 key->security = NULL;
3260 * smack_key_permission - Smack access on a key
3261 * @key_ref: gets to the object
3262 * @cred: the credentials to use
3265 * Return 0 if the task has read and write to the object,
3266 * an error code otherwise
3268 static int smack_key_permission(key_ref_t key_ref,
3269 const struct cred *cred, key_perm_t perm)
3272 struct smk_audit_info ad;
3273 char *tsp = smk_of_task(cred->security);
3275 keyp = key_ref_to_ptr(key_ref);
3279 * If the key hasn't been initialized give it access so that
3282 if (keyp->security == NULL)
3285 * This should not occur
3290 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3291 ad.a.u.key_struct.key = keyp->serial;
3292 ad.a.u.key_struct.key_desc = keyp->description;
3294 return smk_access(tsp, keyp->security,
3295 MAY_READWRITE, &ad);
3297 #endif /* CONFIG_KEYS */
3302 * Audit requires a unique representation of each Smack specific
3303 * rule. This unique representation is used to distinguish the
3304 * object to be audited from remaining kernel objects and also
3305 * works as a glue between the audit hooks.
3307 * Since repository entries are added but never deleted, we'll use
3308 * the smack_known label address related to the given audit rule as
3309 * the needed unique representation. This also better fits the smack
3310 * model where nearly everything is a label.
3315 * smack_audit_rule_init - Initialize a smack audit rule
3316 * @field: audit rule fields given from user-space (audit.h)
3317 * @op: required testing operator (=, !=, >, <, ...)
3318 * @rulestr: smack label to be audited
3319 * @vrule: pointer to save our own audit rule representation
3321 * Prepare to audit cases where (@field @op @rulestr) is true.
3322 * The label to be audited is created if necessay.
3324 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3326 char **rule = (char **)vrule;
3329 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3332 if (op != Audit_equal && op != Audit_not_equal)
3335 *rule = smk_import(rulestr, 0);
3341 * smack_audit_rule_known - Distinguish Smack audit rules
3342 * @krule: rule of interest, in Audit kernel representation format
3344 * This is used to filter Smack rules from remaining Audit ones.
3345 * If it's proved that this rule belongs to us, the
3346 * audit_rule_match hook will be called to do the final judgement.
3348 static int smack_audit_rule_known(struct audit_krule *krule)
3350 struct audit_field *f;
3353 for (i = 0; i < krule->field_count; i++) {
3354 f = &krule->fields[i];
3356 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3364 * smack_audit_rule_match - Audit given object ?
3365 * @secid: security id for identifying the object to test
3366 * @field: audit rule flags given from user-space
3367 * @op: required testing operator
3368 * @vrule: smack internal rule presentation
3369 * @actx: audit context associated with the check
3371 * The core Audit hook. It's used to take the decision of
3372 * whether to audit or not to audit a given object.
3374 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3375 struct audit_context *actx)
3381 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3382 "Smack: missing rule\n");
3386 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3389 smack = smack_from_secid(secid);
3392 * No need to do string comparisons. If a match occurs,
3393 * both pointers will point to the same smack_known
3396 if (op == Audit_equal)
3397 return (rule == smack);
3398 if (op == Audit_not_equal)
3399 return (rule != smack);
3405 * smack_audit_rule_free - free smack rule representation
3406 * @vrule: rule to be freed.
3408 * No memory was allocated.
3410 static void smack_audit_rule_free(void *vrule)
3415 #endif /* CONFIG_AUDIT */
3418 * smack_secid_to_secctx - return the smack label for a secid
3419 * @secid: incoming integer
3420 * @secdata: destination
3421 * @seclen: how long it is
3423 * Exists for networking code.
3425 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3427 char *sp = smack_from_secid(secid);
3431 *seclen = strlen(sp);
3436 * smack_secctx_to_secid - return the secid for a smack label
3437 * @secdata: smack label
3438 * @seclen: how long result is
3439 * @secid: outgoing integer
3441 * Exists for audit and networking code.
3443 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3445 *secid = smack_to_secid(secdata);
3450 * smack_release_secctx - don't do anything.
3454 * Exists to make sure nothing gets done, and properly
3456 static void smack_release_secctx(char *secdata, u32 seclen)
3460 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3462 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3465 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3467 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3470 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3473 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3481 struct security_operations smack_ops = {
3484 .ptrace_access_check = smack_ptrace_access_check,
3485 .ptrace_traceme = smack_ptrace_traceme,
3486 .syslog = smack_syslog,
3488 .sb_alloc_security = smack_sb_alloc_security,
3489 .sb_free_security = smack_sb_free_security,
3490 .sb_copy_data = smack_sb_copy_data,
3491 .sb_kern_mount = smack_sb_kern_mount,
3492 .sb_statfs = smack_sb_statfs,
3493 .sb_mount = smack_sb_mount,
3494 .sb_umount = smack_sb_umount,
3496 .bprm_set_creds = smack_bprm_set_creds,
3497 .bprm_committing_creds = smack_bprm_committing_creds,
3498 .bprm_secureexec = smack_bprm_secureexec,
3500 .inode_alloc_security = smack_inode_alloc_security,
3501 .inode_free_security = smack_inode_free_security,
3502 .inode_init_security = smack_inode_init_security,
3503 .inode_link = smack_inode_link,
3504 .inode_unlink = smack_inode_unlink,
3505 .inode_rmdir = smack_inode_rmdir,
3506 .inode_rename = smack_inode_rename,
3507 .inode_permission = smack_inode_permission,
3508 .inode_setattr = smack_inode_setattr,
3509 .inode_getattr = smack_inode_getattr,
3510 .inode_setxattr = smack_inode_setxattr,
3511 .inode_post_setxattr = smack_inode_post_setxattr,
3512 .inode_getxattr = smack_inode_getxattr,
3513 .inode_removexattr = smack_inode_removexattr,
3514 .inode_getsecurity = smack_inode_getsecurity,
3515 .inode_setsecurity = smack_inode_setsecurity,
3516 .inode_listsecurity = smack_inode_listsecurity,
3517 .inode_getsecid = smack_inode_getsecid,
3519 .file_permission = smack_file_permission,
3520 .file_alloc_security = smack_file_alloc_security,
3521 .file_free_security = smack_file_free_security,
3522 .file_ioctl = smack_file_ioctl,
3523 .file_lock = smack_file_lock,
3524 .file_fcntl = smack_file_fcntl,
3525 .file_mmap = smack_file_mmap,
3526 .file_set_fowner = smack_file_set_fowner,
3527 .file_send_sigiotask = smack_file_send_sigiotask,
3528 .file_receive = smack_file_receive,
3530 .dentry_open = smack_dentry_open,
3532 .cred_alloc_blank = smack_cred_alloc_blank,
3533 .cred_free = smack_cred_free,
3534 .cred_prepare = smack_cred_prepare,
3535 .cred_transfer = smack_cred_transfer,
3536 .kernel_act_as = smack_kernel_act_as,
3537 .kernel_create_files_as = smack_kernel_create_files_as,
3538 .task_setpgid = smack_task_setpgid,
3539 .task_getpgid = smack_task_getpgid,
3540 .task_getsid = smack_task_getsid,
3541 .task_getsecid = smack_task_getsecid,
3542 .task_setnice = smack_task_setnice,
3543 .task_setioprio = smack_task_setioprio,
3544 .task_getioprio = smack_task_getioprio,
3545 .task_setscheduler = smack_task_setscheduler,
3546 .task_getscheduler = smack_task_getscheduler,
3547 .task_movememory = smack_task_movememory,
3548 .task_kill = smack_task_kill,
3549 .task_wait = smack_task_wait,
3550 .task_to_inode = smack_task_to_inode,
3552 .ipc_permission = smack_ipc_permission,
3553 .ipc_getsecid = smack_ipc_getsecid,
3555 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3556 .msg_msg_free_security = smack_msg_msg_free_security,
3558 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3559 .msg_queue_free_security = smack_msg_queue_free_security,
3560 .msg_queue_associate = smack_msg_queue_associate,
3561 .msg_queue_msgctl = smack_msg_queue_msgctl,
3562 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3563 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3565 .shm_alloc_security = smack_shm_alloc_security,
3566 .shm_free_security = smack_shm_free_security,
3567 .shm_associate = smack_shm_associate,
3568 .shm_shmctl = smack_shm_shmctl,
3569 .shm_shmat = smack_shm_shmat,
3571 .sem_alloc_security = smack_sem_alloc_security,
3572 .sem_free_security = smack_sem_free_security,
3573 .sem_associate = smack_sem_associate,
3574 .sem_semctl = smack_sem_semctl,
3575 .sem_semop = smack_sem_semop,
3577 .d_instantiate = smack_d_instantiate,
3579 .getprocattr = smack_getprocattr,
3580 .setprocattr = smack_setprocattr,
3582 .unix_stream_connect = smack_unix_stream_connect,
3583 .unix_may_send = smack_unix_may_send,
3585 .socket_post_create = smack_socket_post_create,
3586 .socket_connect = smack_socket_connect,
3587 .socket_sendmsg = smack_socket_sendmsg,
3588 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3589 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3590 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3591 .sk_alloc_security = smack_sk_alloc_security,
3592 .sk_free_security = smack_sk_free_security,
3593 .sock_graft = smack_sock_graft,
3594 .inet_conn_request = smack_inet_conn_request,
3595 .inet_csk_clone = smack_inet_csk_clone,
3597 /* key management security hooks */
3599 .key_alloc = smack_key_alloc,
3600 .key_free = smack_key_free,
3601 .key_permission = smack_key_permission,
3602 #endif /* CONFIG_KEYS */
3606 .audit_rule_init = smack_audit_rule_init,
3607 .audit_rule_known = smack_audit_rule_known,
3608 .audit_rule_match = smack_audit_rule_match,
3609 .audit_rule_free = smack_audit_rule_free,
3610 #endif /* CONFIG_AUDIT */
3612 .secid_to_secctx = smack_secid_to_secctx,
3613 .secctx_to_secid = smack_secctx_to_secid,
3614 .release_secctx = smack_release_secctx,
3615 .inode_notifysecctx = smack_inode_notifysecctx,
3616 .inode_setsecctx = smack_inode_setsecctx,
3617 .inode_getsecctx = smack_inode_getsecctx,
3621 static __init void init_smack_know_list(void)
3623 list_add(&smack_known_huh.list, &smack_known_list);
3624 list_add(&smack_known_hat.list, &smack_known_list);
3625 list_add(&smack_known_star.list, &smack_known_list);
3626 list_add(&smack_known_floor.list, &smack_known_list);
3627 list_add(&smack_known_invalid.list, &smack_known_list);
3628 list_add(&smack_known_web.list, &smack_known_list);
3632 * smack_init - initialize the smack system
3636 static __init int smack_init(void)
3639 struct task_smack *tsp;
3641 if (!security_module_enable(&smack_ops))
3644 tsp = new_task_smack(smack_known_floor.smk_known,
3645 smack_known_floor.smk_known, GFP_KERNEL);
3649 printk(KERN_INFO "Smack: Initializing.\n");
3652 * Set the security state for the initial task.
3654 cred = (struct cred *) current->cred;
3655 cred->security = tsp;
3657 /* initialize the smack_know_list */
3658 init_smack_know_list();
3662 spin_lock_init(&smack_known_huh.smk_cipsolock);
3663 spin_lock_init(&smack_known_hat.smk_cipsolock);
3664 spin_lock_init(&smack_known_star.smk_cipsolock);
3665 spin_lock_init(&smack_known_floor.smk_cipsolock);
3666 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3671 if (register_security(&smack_ops))
3672 panic("smack: Unable to register with kernel.\n");
3678 * Smack requires early initialization in order to label
3679 * all processes and objects when they are created.
3681 security_initcall(smack_init);