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);
586 *len = strlen(isp) + 1;
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
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;
959 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
960 isp = smk_of_inode(inode);
961 ilen = strlen(isp) + 1;
967 * The rest of the Smack xattrs are only on sockets.
970 if (sbp->s_magic != SOCKFS_MAGIC)
974 if (sock == NULL || sock->sk == NULL)
977 ssp = sock->sk->sk_security;
979 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
981 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
986 ilen = strlen(isp) + 1;
997 * smack_inode_listsecurity - list the Smack attributes
999 * @buffer: where they go
1000 * @buffer_size: size of buffer
1002 * Returns 0 on success, -EINVAL otherwise
1004 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1007 int len = strlen(XATTR_NAME_SMACK);
1009 if (buffer != NULL && len <= buffer_size) {
1010 memcpy(buffer, XATTR_NAME_SMACK, len);
1017 * smack_inode_getsecid - Extract inode's security id
1018 * @inode: inode to extract the info from
1019 * @secid: where result will be saved
1021 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1023 struct inode_smack *isp = inode->i_security;
1025 *secid = smack_to_secid(isp->smk_inode);
1033 * smack_file_permission - Smack check on file operations
1039 * Should access checks be done on each read or write?
1040 * UNICOS and SELinux say yes.
1041 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1043 * I'll say no for now. Smack does not do the frequent
1044 * label changing that SELinux does.
1046 static int smack_file_permission(struct file *file, int mask)
1052 * smack_file_alloc_security - assign a file security blob
1055 * The security blob for a file is a pointer to the master
1056 * label list, so no allocation is done.
1060 static int smack_file_alloc_security(struct file *file)
1062 file->f_security = smk_of_current();
1067 * smack_file_free_security - clear a file security blob
1070 * The security blob for a file is a pointer to the master
1071 * label list, so no memory is freed.
1073 static void smack_file_free_security(struct file *file)
1075 file->f_security = NULL;
1079 * smack_file_ioctl - Smack check on ioctls
1084 * Relies heavily on the correct use of the ioctl command conventions.
1086 * Returns 0 if allowed, error code otherwise
1088 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1092 struct smk_audit_info ad;
1094 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1095 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1097 if (_IOC_DIR(cmd) & _IOC_WRITE)
1098 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1100 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1101 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1107 * smack_file_lock - Smack check on file locking
1111 * Returns 0 if current has write access, error code otherwise
1113 static int smack_file_lock(struct file *file, unsigned int cmd)
1115 struct smk_audit_info ad;
1117 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1118 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1119 return smk_curacc(file->f_security, MAY_WRITE, &ad);
1123 * smack_file_fcntl - Smack check on fcntl
1125 * @cmd: what action to check
1128 * Generally these operations are harmless.
1129 * File locking operations present an obvious mechanism
1130 * for passing information, so they require write access.
1132 * Returns 0 if current has access, error code otherwise
1134 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1137 struct smk_audit_info ad;
1147 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1148 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1149 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1160 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1161 * if mapping anonymous memory.
1162 * @file contains the file structure for file to map (may be NULL).
1163 * @reqprot contains the protection requested by the application.
1164 * @prot contains the protection that will be applied by the kernel.
1165 * @flags contains the operational flags.
1166 * Return 0 if permission is granted.
1168 static int smack_file_mmap(struct file *file,
1169 unsigned long reqprot, unsigned long prot,
1170 unsigned long flags, unsigned long addr,
1171 unsigned long addr_only)
1173 struct smack_known *skp;
1174 struct smack_rule *srp;
1175 struct task_smack *tsp;
1179 struct inode_smack *isp;
1186 /* do DAC check on address space usage */
1187 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1188 if (rc || addr_only)
1191 if (file == NULL || file->f_dentry == NULL)
1194 dp = file->f_dentry;
1196 if (dp->d_inode == NULL)
1199 isp = dp->d_inode->i_security;
1200 if (isp->smk_mmap == NULL)
1202 msmack = isp->smk_mmap;
1204 tsp = current_security();
1205 sp = smk_of_current();
1206 skp = smk_find_entry(sp);
1211 * For each Smack rule associated with the subject
1212 * label verify that the SMACK64MMAP also has access
1213 * to that rule's object label.
1215 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1216 osmack = srp->smk_object;
1218 * Matching labels always allows access.
1220 if (msmack == osmack)
1223 * If there is a matching local rule take
1224 * that into account as well.
1226 may = smk_access_entry(srp->smk_subject, osmack,
1229 may = srp->smk_access;
1231 may &= srp->smk_access;
1233 * If may is zero the SMACK64MMAP subject can't
1234 * possibly have less access.
1240 * Fetch the global list entry.
1241 * If there isn't one a SMACK64MMAP subject
1242 * can't have as much access as current.
1244 skp = smk_find_entry(msmack);
1245 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
1246 if (mmay == -ENOENT) {
1251 * If there is a local entry it modifies the
1252 * potential access, too.
1254 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1255 if (tmay != -ENOENT)
1259 * If there is any access available to current that is
1260 * not available to a SMACK64MMAP subject
1263 if ((may | mmay) != mmay) {
1275 * smack_file_set_fowner - set the file security blob value
1276 * @file: object in question
1279 * Further research may be required on this one.
1281 static int smack_file_set_fowner(struct file *file)
1283 file->f_security = smk_of_current();
1288 * smack_file_send_sigiotask - Smack on sigio
1289 * @tsk: The target task
1290 * @fown: the object the signal come from
1293 * Allow a privileged task to get signals even if it shouldn't
1295 * Returns 0 if a subject with the object's smack could
1296 * write to the task, an error code otherwise.
1298 static int smack_file_send_sigiotask(struct task_struct *tsk,
1299 struct fown_struct *fown, int signum)
1303 char *tsp = smk_of_task(tsk->cred->security);
1304 struct smk_audit_info ad;
1307 * struct fown_struct is never outside the context of a struct file
1309 file = container_of(fown, struct file, f_owner);
1311 /* we don't log here as rc can be overriden */
1312 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
1313 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1316 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1317 smk_ad_setfield_u_tsk(&ad, tsk);
1318 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
1323 * smack_file_receive - Smack file receive check
1326 * Returns 0 if current has access, error code otherwise
1328 static int smack_file_receive(struct file *file)
1331 struct smk_audit_info ad;
1333 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1334 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1336 * This code relies on bitmasks.
1338 if (file->f_mode & FMODE_READ)
1340 if (file->f_mode & FMODE_WRITE)
1343 return smk_curacc(file->f_security, may, &ad);
1347 * smack_dentry_open - Smack dentry open processing
1351 * Set the security blob in the file structure.
1355 static int smack_dentry_open(struct file *file, const struct cred *cred)
1357 struct inode_smack *isp = file->f_path.dentry->d_inode->i_security;
1359 file->f_security = isp->smk_inode;
1369 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1370 * @new: the new credentials
1371 * @gfp: the atomicity of any memory allocations
1373 * Prepare a blank set of credentials for modification. This must allocate all
1374 * the memory the LSM module might require such that cred_transfer() can
1375 * complete without error.
1377 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1379 struct task_smack *tsp;
1381 tsp = new_task_smack(NULL, NULL, gfp);
1385 cred->security = tsp;
1392 * smack_cred_free - "free" task-level security credentials
1393 * @cred: the credentials in question
1396 static void smack_cred_free(struct cred *cred)
1398 struct task_smack *tsp = cred->security;
1399 struct smack_rule *rp;
1400 struct list_head *l;
1401 struct list_head *n;
1405 cred->security = NULL;
1407 list_for_each_safe(l, n, &tsp->smk_rules) {
1408 rp = list_entry(l, struct smack_rule, list);
1409 list_del(&rp->list);
1416 * smack_cred_prepare - prepare new set of credentials for modification
1417 * @new: the new credentials
1418 * @old: the original credentials
1419 * @gfp: the atomicity of any memory allocations
1421 * Prepare a new set of credentials for modification.
1423 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1426 struct task_smack *old_tsp = old->security;
1427 struct task_smack *new_tsp;
1430 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1431 if (new_tsp == NULL)
1434 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1438 new->security = new_tsp;
1443 * smack_cred_transfer - Transfer the old credentials to the new credentials
1444 * @new: the new credentials
1445 * @old: the original credentials
1447 * Fill in a set of blank credentials from another set of credentials.
1449 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1451 struct task_smack *old_tsp = old->security;
1452 struct task_smack *new_tsp = new->security;
1454 new_tsp->smk_task = old_tsp->smk_task;
1455 new_tsp->smk_forked = old_tsp->smk_task;
1456 mutex_init(&new_tsp->smk_rules_lock);
1457 INIT_LIST_HEAD(&new_tsp->smk_rules);
1460 /* cbs copy rule list */
1464 * smack_kernel_act_as - Set the subjective context in a set of credentials
1465 * @new: points to the set of credentials to be modified.
1466 * @secid: specifies the security ID to be set
1468 * Set the security data for a kernel service.
1470 static int smack_kernel_act_as(struct cred *new, u32 secid)
1472 struct task_smack *new_tsp = new->security;
1473 char *smack = smack_from_secid(secid);
1478 new_tsp->smk_task = smack;
1483 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1484 * @new: points to the set of credentials to be modified
1485 * @inode: points to the inode to use as a reference
1487 * Set the file creation context in a set of credentials to the same
1488 * as the objective context of the specified inode
1490 static int smack_kernel_create_files_as(struct cred *new,
1491 struct inode *inode)
1493 struct inode_smack *isp = inode->i_security;
1494 struct task_smack *tsp = new->security;
1496 tsp->smk_forked = isp->smk_inode;
1497 tsp->smk_task = isp->smk_inode;
1502 * smk_curacc_on_task - helper to log task related access
1503 * @p: the task object
1504 * @access: the access requested
1505 * @caller: name of the calling function for audit
1507 * Return 0 if access is permitted
1509 static int smk_curacc_on_task(struct task_struct *p, int access,
1512 struct smk_audit_info ad;
1514 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1515 smk_ad_setfield_u_tsk(&ad, p);
1516 return smk_curacc(smk_of_task_struct(p), access, &ad);
1520 * smack_task_setpgid - Smack check on setting pgid
1521 * @p: the task object
1524 * Return 0 if write access is permitted
1526 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1528 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1532 * smack_task_getpgid - Smack access check for getpgid
1533 * @p: the object task
1535 * Returns 0 if current can read the object task, error code otherwise
1537 static int smack_task_getpgid(struct task_struct *p)
1539 return smk_curacc_on_task(p, MAY_READ, __func__);
1543 * smack_task_getsid - Smack access check for getsid
1544 * @p: the object task
1546 * Returns 0 if current can read the object task, error code otherwise
1548 static int smack_task_getsid(struct task_struct *p)
1550 return smk_curacc_on_task(p, MAY_READ, __func__);
1554 * smack_task_getsecid - get the secid of the task
1555 * @p: the object task
1556 * @secid: where to put the result
1558 * Sets the secid to contain a u32 version of the smack label.
1560 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1562 *secid = smack_to_secid(smk_of_task_struct(p));
1566 * smack_task_setnice - Smack check on setting nice
1567 * @p: the task object
1570 * Return 0 if write access is permitted
1572 static int smack_task_setnice(struct task_struct *p, int nice)
1576 rc = cap_task_setnice(p, nice);
1578 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1583 * smack_task_setioprio - Smack check on setting ioprio
1584 * @p: the task object
1587 * Return 0 if write access is permitted
1589 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1593 rc = cap_task_setioprio(p, ioprio);
1595 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1600 * smack_task_getioprio - Smack check on reading ioprio
1601 * @p: the task object
1603 * Return 0 if read access is permitted
1605 static int smack_task_getioprio(struct task_struct *p)
1607 return smk_curacc_on_task(p, MAY_READ, __func__);
1611 * smack_task_setscheduler - Smack check on setting scheduler
1612 * @p: the task object
1616 * Return 0 if read access is permitted
1618 static int smack_task_setscheduler(struct task_struct *p)
1622 rc = cap_task_setscheduler(p);
1624 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1629 * smack_task_getscheduler - Smack check on reading scheduler
1630 * @p: the task object
1632 * Return 0 if read access is permitted
1634 static int smack_task_getscheduler(struct task_struct *p)
1636 return smk_curacc_on_task(p, MAY_READ, __func__);
1640 * smack_task_movememory - Smack check on moving memory
1641 * @p: the task object
1643 * Return 0 if write access is permitted
1645 static int smack_task_movememory(struct task_struct *p)
1647 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1651 * smack_task_kill - Smack check on signal delivery
1652 * @p: the task object
1655 * @secid: identifies the smack to use in lieu of current's
1657 * Return 0 if write access is permitted
1659 * The secid behavior is an artifact of an SELinux hack
1660 * in the USB code. Someday it may go away.
1662 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1665 struct smk_audit_info ad;
1667 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1668 smk_ad_setfield_u_tsk(&ad, p);
1670 * Sending a signal requires that the sender
1671 * can write the receiver.
1674 return smk_curacc(smk_of_task_struct(p), MAY_WRITE,
1677 * If the secid isn't 0 we're dealing with some USB IO
1678 * specific behavior. This is not clean. For one thing
1679 * we can't take privilege into account.
1681 return smk_access(smack_from_secid(secid),
1682 smk_of_task_struct(p), MAY_WRITE, &ad);
1686 * smack_task_wait - Smack access check for waiting
1687 * @p: task to wait for
1689 * Returns 0 if current can wait for p, error code otherwise
1691 static int smack_task_wait(struct task_struct *p)
1693 struct smk_audit_info ad;
1694 char *sp = smk_of_current();
1699 tsp = smk_of_forked(__task_cred(p)->security);
1702 /* we don't log here, we can be overriden */
1703 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
1708 * Allow the operation to succeed if either task
1709 * has privilege to perform operations that might
1710 * account for the smack labels having gotten to
1711 * be different in the first place.
1713 * This breaks the strict subject/object access
1714 * control ideal, taking the object's privilege
1715 * state into account in the decision as well as
1718 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1720 /* we log only if we didn't get overriden */
1722 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1723 smk_ad_setfield_u_tsk(&ad, p);
1724 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
1729 * smack_task_to_inode - copy task smack into the inode blob
1730 * @p: task to copy from
1731 * @inode: inode to copy to
1733 * Sets the smack pointer in the inode security blob
1735 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1737 struct inode_smack *isp = inode->i_security;
1738 isp->smk_inode = smk_of_task_struct(p);
1746 * smack_sk_alloc_security - Allocate a socket blob
1749 * @gfp_flags: memory allocation flags
1751 * Assign Smack pointers to current
1753 * Returns 0 on success, -ENOMEM is there's no memory
1755 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1757 char *csp = smk_of_current();
1758 struct socket_smack *ssp;
1760 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1766 ssp->smk_packet = NULL;
1768 sk->sk_security = ssp;
1774 * smack_sk_free_security - Free a socket blob
1777 * Clears the blob pointer
1779 static void smack_sk_free_security(struct sock *sk)
1781 kfree(sk->sk_security);
1785 * smack_host_label - check host based restrictions
1786 * @sip: the object end
1788 * looks for host based access restrictions
1790 * This version will only be appropriate for really small sets of single label
1791 * hosts. The caller is responsible for ensuring that the RCU read lock is
1792 * taken before calling this function.
1794 * Returns the label of the far end or NULL if it's not special.
1796 static char *smack_host_label(struct sockaddr_in *sip)
1798 struct smk_netlbladdr *snp;
1799 struct in_addr *siap = &sip->sin_addr;
1801 if (siap->s_addr == 0)
1804 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1806 * we break after finding the first match because
1807 * the list is sorted from longest to shortest mask
1808 * so we have found the most specific match
1810 if ((&snp->smk_host.sin_addr)->s_addr ==
1811 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1812 /* we have found the special CIPSO option */
1813 if (snp->smk_label == smack_cipso_option)
1815 return snp->smk_label;
1822 * smack_set_catset - convert a capset to netlabel mls categories
1823 * @catset: the Smack categories
1824 * @sap: where to put the netlabel categories
1826 * Allocates and fills attr.mls.cat
1828 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1839 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1840 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1841 sap->attr.mls.cat->startbit = 0;
1843 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1844 for (m = 0x80; m != 0; m >>= 1, cat++) {
1847 rc = netlbl_secattr_catmap_setbit(&sap->attr.mls.cat,
1853 * smack_to_secattr - fill a secattr from a smack value
1854 * @smack: the smack value
1855 * @nlsp: where the result goes
1857 * Casey says that CIPSO is good enough for now.
1858 * It can be used to effect.
1859 * It can also be abused to effect when necessary.
1860 * Apologies to the TSIG group in general and GW in particular.
1862 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1864 struct smack_cipso cipso;
1867 nlsp->domain = smack;
1868 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1870 rc = smack_to_cipso(smack, &cipso);
1872 nlsp->attr.mls.lvl = cipso.smk_level;
1873 smack_set_catset(cipso.smk_catset, nlsp);
1875 nlsp->attr.mls.lvl = smack_cipso_direct;
1876 smack_set_catset(smack, nlsp);
1881 * smack_netlabel - Set the secattr on a socket
1883 * @labeled: socket label scheme
1885 * Convert the outbound smack value (smk_out) to a
1886 * secattr and attach it to the socket.
1888 * Returns 0 on success or an error code
1890 static int smack_netlabel(struct sock *sk, int labeled)
1892 struct socket_smack *ssp = sk->sk_security;
1893 struct netlbl_lsm_secattr secattr;
1897 * Usually the netlabel code will handle changing the
1898 * packet labeling based on the label.
1899 * The case of a single label host is different, because
1900 * a single label host should never get a labeled packet
1901 * even though the label is usually associated with a packet
1905 bh_lock_sock_nested(sk);
1907 if (ssp->smk_out == smack_net_ambient ||
1908 labeled == SMACK_UNLABELED_SOCKET)
1909 netlbl_sock_delattr(sk);
1911 netlbl_secattr_init(&secattr);
1912 smack_to_secattr(ssp->smk_out, &secattr);
1913 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
1914 netlbl_secattr_destroy(&secattr);
1924 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1926 * @sap: the destination address
1928 * Set the correct secattr for the given socket based on the destination
1929 * address and perform any outbound access checks needed.
1931 * Returns 0 on success or an error code.
1934 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1939 struct socket_smack *ssp = sk->sk_security;
1940 struct smk_audit_info ad;
1943 hostsp = smack_host_label(sap);
1944 if (hostsp != NULL) {
1945 sk_lbl = SMACK_UNLABELED_SOCKET;
1947 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1948 ad.a.u.net.family = sap->sin_family;
1949 ad.a.u.net.dport = sap->sin_port;
1950 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1952 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
1954 sk_lbl = SMACK_CIPSO_SOCKET;
1961 return smack_netlabel(sk, sk_lbl);
1965 * smack_inode_setsecurity - set smack xattrs
1966 * @inode: the object
1967 * @name: attribute name
1968 * @value: attribute value
1969 * @size: size of the attribute
1972 * Sets the named attribute in the appropriate blob
1974 * Returns 0 on success, or an error code
1976 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1977 const void *value, size_t size, int flags)
1980 struct inode_smack *nsp = inode->i_security;
1981 struct socket_smack *ssp;
1982 struct socket *sock;
1985 if (value == NULL || size > SMK_LABELLEN || size == 0)
1988 sp = smk_import(value, size);
1992 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1993 nsp->smk_inode = sp;
1994 nsp->smk_flags |= SMK_INODE_INSTANT;
1998 * The rest of the Smack xattrs are only on sockets.
2000 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2003 sock = SOCKET_I(inode);
2004 if (sock == NULL || sock->sk == NULL)
2007 ssp = sock->sk->sk_security;
2009 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2011 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2013 if (sock->sk->sk_family != PF_UNIX) {
2014 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2017 "Smack: \"%s\" netlbl error %d.\n",
2027 * smack_socket_post_create - finish socket setup
2029 * @family: protocol family
2034 * Sets the netlabel information on the socket
2036 * Returns 0 on success, and error code otherwise
2038 static int smack_socket_post_create(struct socket *sock, int family,
2039 int type, int protocol, int kern)
2041 if (family != PF_INET || sock->sk == NULL)
2044 * Set the outbound netlbl.
2046 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2050 * smack_socket_connect - connect access check
2052 * @sap: the other end
2053 * @addrlen: size of sap
2055 * Verifies that a connection may be possible
2057 * Returns 0 on success, and error code otherwise
2059 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2062 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
2064 if (addrlen < sizeof(struct sockaddr_in))
2067 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2071 * smack_flags_to_may - convert S_ to MAY_ values
2072 * @flags: the S_ value
2074 * Returns the equivalent MAY_ value
2076 static int smack_flags_to_may(int flags)
2080 if (flags & S_IRUGO)
2082 if (flags & S_IWUGO)
2084 if (flags & S_IXUGO)
2091 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2096 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2098 msg->security = smk_of_current();
2103 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2106 * Clears the blob pointer
2108 static void smack_msg_msg_free_security(struct msg_msg *msg)
2110 msg->security = NULL;
2114 * smack_of_shm - the smack pointer for the shm
2117 * Returns a pointer to the smack value
2119 static char *smack_of_shm(struct shmid_kernel *shp)
2121 return (char *)shp->shm_perm.security;
2125 * smack_shm_alloc_security - Set the security blob for shm
2130 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2132 struct kern_ipc_perm *isp = &shp->shm_perm;
2134 isp->security = smk_of_current();
2139 * smack_shm_free_security - Clear the security blob for shm
2142 * Clears the blob pointer
2144 static void smack_shm_free_security(struct shmid_kernel *shp)
2146 struct kern_ipc_perm *isp = &shp->shm_perm;
2148 isp->security = NULL;
2152 * smk_curacc_shm : check if current has access on shm
2154 * @access : access requested
2156 * Returns 0 if current has the requested access, error code otherwise
2158 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2160 char *ssp = smack_of_shm(shp);
2161 struct smk_audit_info ad;
2164 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2165 ad.a.u.ipc_id = shp->shm_perm.id;
2167 return smk_curacc(ssp, access, &ad);
2171 * smack_shm_associate - Smack access check for shm
2173 * @shmflg: access requested
2175 * Returns 0 if current has the requested access, error code otherwise
2177 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2181 may = smack_flags_to_may(shmflg);
2182 return smk_curacc_shm(shp, may);
2186 * smack_shm_shmctl - Smack access check for shm
2188 * @cmd: what it wants to do
2190 * Returns 0 if current has the requested access, error code otherwise
2192 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2205 may = MAY_READWRITE;
2210 * System level information.
2216 return smk_curacc_shm(shp, may);
2220 * smack_shm_shmat - Smack access for shmat
2223 * @shmflg: access requested
2225 * Returns 0 if current has the requested access, error code otherwise
2227 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2232 may = smack_flags_to_may(shmflg);
2233 return smk_curacc_shm(shp, may);
2237 * smack_of_sem - the smack pointer for the sem
2240 * Returns a pointer to the smack value
2242 static char *smack_of_sem(struct sem_array *sma)
2244 return (char *)sma->sem_perm.security;
2248 * smack_sem_alloc_security - Set the security blob for sem
2253 static int smack_sem_alloc_security(struct sem_array *sma)
2255 struct kern_ipc_perm *isp = &sma->sem_perm;
2257 isp->security = smk_of_current();
2262 * smack_sem_free_security - Clear the security blob for sem
2265 * Clears the blob pointer
2267 static void smack_sem_free_security(struct sem_array *sma)
2269 struct kern_ipc_perm *isp = &sma->sem_perm;
2271 isp->security = NULL;
2275 * smk_curacc_sem : check if current has access on sem
2277 * @access : access requested
2279 * Returns 0 if current has the requested access, error code otherwise
2281 static int smk_curacc_sem(struct sem_array *sma, int access)
2283 char *ssp = smack_of_sem(sma);
2284 struct smk_audit_info ad;
2287 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2288 ad.a.u.ipc_id = sma->sem_perm.id;
2290 return smk_curacc(ssp, access, &ad);
2294 * smack_sem_associate - Smack access check for sem
2296 * @semflg: access requested
2298 * Returns 0 if current has the requested access, error code otherwise
2300 static int smack_sem_associate(struct sem_array *sma, int semflg)
2304 may = smack_flags_to_may(semflg);
2305 return smk_curacc_sem(sma, may);
2309 * smack_sem_shmctl - Smack access check for sem
2311 * @cmd: what it wants to do
2313 * Returns 0 if current has the requested access, error code otherwise
2315 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2333 may = MAY_READWRITE;
2338 * System level information
2345 return smk_curacc_sem(sma, may);
2349 * smack_sem_semop - Smack checks of semaphore operations
2355 * Treated as read and write in all cases.
2357 * Returns 0 if access is allowed, error code otherwise
2359 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2360 unsigned nsops, int alter)
2362 return smk_curacc_sem(sma, MAY_READWRITE);
2366 * smack_msg_alloc_security - Set the security blob for msg
2371 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2373 struct kern_ipc_perm *kisp = &msq->q_perm;
2375 kisp->security = smk_of_current();
2380 * smack_msg_free_security - Clear the security blob for msg
2383 * Clears the blob pointer
2385 static void smack_msg_queue_free_security(struct msg_queue *msq)
2387 struct kern_ipc_perm *kisp = &msq->q_perm;
2389 kisp->security = NULL;
2393 * smack_of_msq - the smack pointer for the msq
2396 * Returns a pointer to the smack value
2398 static char *smack_of_msq(struct msg_queue *msq)
2400 return (char *)msq->q_perm.security;
2404 * smk_curacc_msq : helper to check if current has access on msq
2406 * @access : access requested
2408 * return 0 if current has access, error otherwise
2410 static int smk_curacc_msq(struct msg_queue *msq, int access)
2412 char *msp = smack_of_msq(msq);
2413 struct smk_audit_info ad;
2416 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2417 ad.a.u.ipc_id = msq->q_perm.id;
2419 return smk_curacc(msp, access, &ad);
2423 * smack_msg_queue_associate - Smack access check for msg_queue
2425 * @msqflg: access requested
2427 * Returns 0 if current has the requested access, error code otherwise
2429 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2433 may = smack_flags_to_may(msqflg);
2434 return smk_curacc_msq(msq, may);
2438 * smack_msg_queue_msgctl - Smack access check for msg_queue
2440 * @cmd: what it wants to do
2442 * Returns 0 if current has the requested access, error code otherwise
2444 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2455 may = MAY_READWRITE;
2460 * System level information
2467 return smk_curacc_msq(msq, may);
2471 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2474 * @msqflg: access requested
2476 * Returns 0 if current has the requested access, error code otherwise
2478 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2483 may = smack_flags_to_may(msqflg);
2484 return smk_curacc_msq(msq, may);
2488 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2495 * Returns 0 if current has read and write access, error code otherwise
2497 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2498 struct task_struct *target, long type, int mode)
2500 return smk_curacc_msq(msq, MAY_READWRITE);
2504 * smack_ipc_permission - Smack access for ipc_permission()
2505 * @ipp: the object permissions
2506 * @flag: access requested
2508 * Returns 0 if current has read and write access, error code otherwise
2510 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2512 char *isp = ipp->security;
2513 int may = smack_flags_to_may(flag);
2514 struct smk_audit_info ad;
2517 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2518 ad.a.u.ipc_id = ipp->id;
2520 return smk_curacc(isp, may, &ad);
2524 * smack_ipc_getsecid - Extract smack security id
2525 * @ipp: the object permissions
2526 * @secid: where result will be saved
2528 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2530 char *smack = ipp->security;
2532 *secid = smack_to_secid(smack);
2536 * smack_d_instantiate - Make sure the blob is correct on an inode
2537 * @opt_dentry: dentry where inode will be attached
2538 * @inode: the object
2540 * Set the inode's security blob if it hasn't been done already.
2542 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2544 struct super_block *sbp;
2545 struct superblock_smack *sbsp;
2546 struct inode_smack *isp;
2547 char *csp = smk_of_current();
2550 char trattr[TRANS_TRUE_SIZE];
2557 isp = inode->i_security;
2559 mutex_lock(&isp->smk_lock);
2561 * If the inode is already instantiated
2562 * take the quick way out
2564 if (isp->smk_flags & SMK_INODE_INSTANT)
2568 sbsp = sbp->s_security;
2570 * We're going to use the superblock default label
2571 * if there's no label on the file.
2573 final = sbsp->smk_default;
2576 * If this is the root inode the superblock
2577 * may be in the process of initialization.
2578 * If that is the case use the root value out
2579 * of the superblock.
2581 if (opt_dentry->d_parent == opt_dentry) {
2582 isp->smk_inode = sbsp->smk_root;
2583 isp->smk_flags |= SMK_INODE_INSTANT;
2588 * This is pretty hackish.
2589 * Casey says that we shouldn't have to do
2590 * file system specific code, but it does help
2591 * with keeping it simple.
2593 switch (sbp->s_magic) {
2596 * Casey says that it's a little embarrassing
2597 * that the smack file system doesn't do
2598 * extended attributes.
2600 final = smack_known_star.smk_known;
2604 * Casey says pipes are easy (?)
2606 final = smack_known_star.smk_known;
2608 case DEVPTS_SUPER_MAGIC:
2610 * devpts seems content with the label of the task.
2611 * Programs that change smack have to treat the
2618 * Socket access is controlled by the socket
2619 * structures associated with the task involved.
2621 final = smack_known_star.smk_known;
2623 case PROC_SUPER_MAGIC:
2625 * Casey says procfs appears not to care.
2626 * The superblock default suffices.
2631 * Device labels should come from the filesystem,
2632 * but watch out, because they're volitile,
2633 * getting recreated on every reboot.
2635 final = smack_known_star.smk_known;
2639 * If a smack value has been set we want to use it,
2640 * but since tmpfs isn't giving us the opportunity
2641 * to set mount options simulate setting the
2642 * superblock default.
2646 * This isn't an understood special case.
2647 * Get the value from the xattr.
2651 * UNIX domain sockets use lower level socket data.
2653 if (S_ISSOCK(inode->i_mode)) {
2654 final = smack_known_star.smk_known;
2658 * No xattr support means, alas, no SMACK label.
2659 * Use the aforeapplied default.
2660 * It would be curious if the label of the task
2661 * does not match that assigned.
2663 if (inode->i_op->getxattr == NULL)
2666 * Get the dentry for xattr.
2668 dp = dget(opt_dentry);
2669 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2670 if (fetched != NULL) {
2672 if (S_ISDIR(inode->i_mode)) {
2674 inode->i_op->getxattr(dp,
2675 XATTR_NAME_SMACKTRANSMUTE,
2676 trattr, TRANS_TRUE_SIZE);
2677 if (strncmp(trattr, TRANS_TRUE,
2678 TRANS_TRUE_SIZE) == 0)
2679 transflag = SMK_INODE_TRANSMUTE;
2682 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2683 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2690 isp->smk_inode = csp;
2692 isp->smk_inode = final;
2694 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2697 mutex_unlock(&isp->smk_lock);
2702 * smack_getprocattr - Smack process attribute access
2703 * @p: the object task
2704 * @name: the name of the attribute in /proc/.../attr
2705 * @value: where to put the result
2707 * Places a copy of the task Smack into value
2709 * Returns the length of the smack label or an error code
2711 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2716 if (strcmp(name, "current") != 0)
2719 cp = kstrdup(smk_of_task_struct(p), GFP_KERNEL);
2729 * smack_setprocattr - Smack process attribute setting
2730 * @p: the object task
2731 * @name: the name of the attribute in /proc/.../attr
2732 * @value: the value to set
2733 * @size: the size of the value
2735 * Sets the Smack value of the task. Only setting self
2736 * is permitted and only with privilege
2738 * Returns the length of the smack label or an error code
2740 static int smack_setprocattr(struct task_struct *p, char *name,
2741 void *value, size_t size)
2744 struct task_smack *tsp;
2745 struct task_smack *oldtsp;
2750 * Changing another process' Smack value is too dangerous
2751 * and supports no sane use case.
2756 if (!capable(CAP_MAC_ADMIN))
2759 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2762 if (strcmp(name, "current") != 0)
2765 newsmack = smk_import(value, size);
2766 if (newsmack == NULL)
2770 * No process is ever allowed the web ("@") label.
2772 if (newsmack == smack_known_web.smk_known)
2775 oldtsp = p->cred->security;
2776 new = prepare_creds();
2780 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
2785 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2789 new->security = tsp;
2795 * smack_unix_stream_connect - Smack access on UDS
2797 * @other: the other sock
2800 * Return 0 if a subject with the smack of sock could access
2801 * an object with the smack of other, otherwise an error code
2803 static int smack_unix_stream_connect(struct sock *sock,
2804 struct sock *other, struct sock *newsk)
2806 struct socket_smack *ssp = sock->sk_security;
2807 struct socket_smack *osp = other->sk_security;
2808 struct socket_smack *nsp = newsk->sk_security;
2809 struct smk_audit_info ad;
2812 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2813 smk_ad_setfield_u_net_sk(&ad, other);
2815 if (!capable(CAP_MAC_OVERRIDE))
2816 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2819 * Cross reference the peer labels for SO_PEERSEC.
2822 nsp->smk_packet = ssp->smk_out;
2823 ssp->smk_packet = osp->smk_out;
2830 * smack_unix_may_send - Smack access on UDS
2832 * @other: the other socket
2834 * Return 0 if a subject with the smack of sock could access
2835 * an object with the smack of other, otherwise an error code
2837 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2839 struct socket_smack *ssp = sock->sk->sk_security;
2840 struct socket_smack *osp = other->sk->sk_security;
2841 struct smk_audit_info ad;
2844 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2845 smk_ad_setfield_u_net_sk(&ad, other->sk);
2847 if (!capable(CAP_MAC_OVERRIDE))
2848 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2854 * smack_socket_sendmsg - Smack check based on destination host
2857 * @size: the size of the message
2859 * Return 0 if the current subject can write to the destination
2860 * host. This is only a question if the destination is a single
2863 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2866 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2869 * Perfectly reasonable for this to be NULL
2871 if (sip == NULL || sip->sin_family != AF_INET)
2874 return smack_netlabel_send(sock->sk, sip);
2878 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2879 * @sap: netlabel secattr
2880 * @ssp: socket security information
2882 * Returns a pointer to a Smack label found on the label list.
2884 static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
2885 struct socket_smack *ssp)
2887 struct smack_known *skp;
2888 char smack[SMK_LABELLEN];
2892 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2894 * Looks like a CIPSO packet.
2895 * If there are flags but no level netlabel isn't
2896 * behaving the way we expect it to.
2898 * Get the categories, if any
2899 * Without guidance regarding the smack value
2900 * for the packet fall back on the network
2903 memset(smack, '\0', SMK_LABELLEN);
2904 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2906 pcat = netlbl_secattr_catmap_walk(
2907 sap->attr.mls.cat, pcat + 1);
2910 smack_catset_bit(pcat, smack);
2913 * If it is CIPSO using smack direct mapping
2914 * we are already done. WeeHee.
2916 if (sap->attr.mls.lvl == smack_cipso_direct) {
2918 * The label sent is usually on the label list.
2920 * If it is not we may still want to allow the
2923 * If the recipient is accepting all packets
2924 * because it is using the star ("*") label
2925 * for SMACK64IPIN provide the web ("@") label
2926 * so that a directed response will succeed.
2927 * This is not very correct from a MAC point
2928 * of view, but gets around the problem that
2929 * locking prevents adding the newly discovered
2930 * label to the list.
2931 * The case where the recipient is not using
2932 * the star label should obviously fail.
2933 * The easy way to do this is to provide the
2934 * star label as the subject label.
2936 skp = smk_find_entry(smack);
2938 return skp->smk_known;
2940 ssp->smk_in == smack_known_star.smk_known)
2941 return smack_known_web.smk_known;
2942 return smack_known_star.smk_known;
2945 * Look it up in the supplied table if it is not
2948 sp = smack_from_cipso(sap->attr.mls.lvl, smack);
2951 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
2952 return smack_known_web.smk_known;
2953 return smack_known_star.smk_known;
2955 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2957 * Looks like a fallback, which gives us a secid.
2959 sp = smack_from_secid(sap->attr.secid);
2961 * This has got to be a bug because it is
2962 * impossible to specify a fallback without
2963 * specifying the label, which will ensure
2964 * it has a secid, and the only way to get a
2965 * secid is from a fallback.
2971 * Without guidance regarding the smack value
2972 * for the packet fall back on the network
2975 return smack_net_ambient;
2979 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2983 * Returns 0 if the packet should be delivered, an error code otherwise
2985 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2987 struct netlbl_lsm_secattr secattr;
2988 struct socket_smack *ssp = sk->sk_security;
2991 struct smk_audit_info ad;
2992 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2996 * Translate what netlabel gave us.
2998 netlbl_secattr_init(&secattr);
3000 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3002 csp = smack_from_secattr(&secattr, ssp);
3004 csp = smack_net_ambient;
3006 netlbl_secattr_destroy(&secattr);
3009 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3010 ad.a.u.net.family = sk->sk_family;
3011 ad.a.u.net.netif = skb->skb_iif;
3012 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3015 * Receiving a packet requires that the other end
3016 * be able to write here. Read access is not required.
3017 * This is the simplist possible security model
3020 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
3022 netlbl_skbuff_err(skb, rc, 0);
3027 * smack_socket_getpeersec_stream - pull in packet label
3029 * @optval: user's destination
3030 * @optlen: size thereof
3033 * returns zero on success, an error code otherwise
3035 static int smack_socket_getpeersec_stream(struct socket *sock,
3036 char __user *optval,
3037 int __user *optlen, unsigned len)
3039 struct socket_smack *ssp;
3044 ssp = sock->sk->sk_security;
3045 if (ssp->smk_packet != NULL) {
3046 rcp = ssp->smk_packet;
3047 slen = strlen(rcp) + 1;
3052 else if (copy_to_user(optval, rcp, slen) != 0)
3055 if (put_user(slen, optlen) != 0)
3063 * smack_socket_getpeersec_dgram - pull in packet label
3064 * @sock: the peer socket
3066 * @secid: pointer to where to put the secid of the packet
3068 * Sets the netlabel socket state on sk from parent
3070 static int smack_socket_getpeersec_dgram(struct socket *sock,
3071 struct sk_buff *skb, u32 *secid)
3074 struct netlbl_lsm_secattr secattr;
3075 struct socket_smack *ssp = NULL;
3077 int family = PF_UNSPEC;
3078 u32 s = 0; /* 0 is the invalid secid */
3082 if (skb->protocol == htons(ETH_P_IP))
3084 else if (skb->protocol == htons(ETH_P_IPV6))
3087 if (family == PF_UNSPEC && sock != NULL)
3088 family = sock->sk->sk_family;
3090 if (family == PF_UNIX) {
3091 ssp = sock->sk->sk_security;
3092 s = smack_to_secid(ssp->smk_out);
3093 } else if (family == PF_INET || family == PF_INET6) {
3095 * Translate what netlabel gave us.
3097 if (sock != NULL && sock->sk != NULL)
3098 ssp = sock->sk->sk_security;
3099 netlbl_secattr_init(&secattr);
3100 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3102 sp = smack_from_secattr(&secattr, ssp);
3103 s = smack_to_secid(sp);
3105 netlbl_secattr_destroy(&secattr);
3114 * smack_sock_graft - Initialize a newly created socket with an existing sock
3116 * @parent: parent socket
3118 * Set the smk_{in,out} state of an existing sock based on the process that
3119 * is creating the new socket.
3121 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3123 struct socket_smack *ssp;
3126 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3129 ssp = sk->sk_security;
3130 ssp->smk_in = ssp->smk_out = smk_of_current();
3131 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3135 * smack_inet_conn_request - Smack access check on connect
3136 * @sk: socket involved
3140 * Returns 0 if a task with the packet label could write to
3141 * the socket, otherwise an error code
3143 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3144 struct request_sock *req)
3146 u16 family = sk->sk_family;
3147 struct socket_smack *ssp = sk->sk_security;
3148 struct netlbl_lsm_secattr secattr;
3149 struct sockaddr_in addr;
3153 struct smk_audit_info ad;
3155 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3156 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3159 netlbl_secattr_init(&secattr);
3160 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3162 sp = smack_from_secattr(&secattr, ssp);
3164 sp = smack_known_huh.smk_known;
3165 netlbl_secattr_destroy(&secattr);
3168 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3169 ad.a.u.net.family = family;
3170 ad.a.u.net.netif = skb->skb_iif;
3171 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3174 * Receiving a packet requires that the other end be able to write
3175 * here. Read access is not required.
3177 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
3182 * Save the peer's label in the request_sock so we can later setup
3183 * smk_packet in the child socket so that SO_PEERCRED can report it.
3185 req->peer_secid = smack_to_secid(sp);
3188 * We need to decide if we want to label the incoming connection here
3189 * if we do we only need to label the request_sock and the stack will
3190 * propagate the wire-label to the sock when it is created.
3193 addr.sin_addr.s_addr = hdr->saddr;
3195 if (smack_host_label(&addr) == NULL) {
3197 netlbl_secattr_init(&secattr);
3198 smack_to_secattr(sp, &secattr);
3199 rc = netlbl_req_setattr(req, &secattr);
3200 netlbl_secattr_destroy(&secattr);
3203 netlbl_req_delattr(req);
3210 * smack_inet_csk_clone - Copy the connection information to the new socket
3211 * @sk: the new socket
3212 * @req: the connection's request_sock
3214 * Transfer the connection's peer label to the newly created socket.
3216 static void smack_inet_csk_clone(struct sock *sk,
3217 const struct request_sock *req)
3219 struct socket_smack *ssp = sk->sk_security;
3221 if (req->peer_secid != 0)
3222 ssp->smk_packet = smack_from_secid(req->peer_secid);
3224 ssp->smk_packet = NULL;
3228 * Key management security hooks
3230 * Casey has not tested key support very heavily.
3231 * The permission check is most likely too restrictive.
3232 * If you care about keys please have a look.
3237 * smack_key_alloc - Set the key security blob
3239 * @cred: the credentials to use
3242 * No allocation required
3246 static int smack_key_alloc(struct key *key, const struct cred *cred,
3247 unsigned long flags)
3249 key->security = smk_of_task(cred->security);
3254 * smack_key_free - Clear the key security blob
3257 * Clear the blob pointer
3259 static void smack_key_free(struct key *key)
3261 key->security = NULL;
3265 * smack_key_permission - Smack access on a key
3266 * @key_ref: gets to the object
3267 * @cred: the credentials to use
3270 * Return 0 if the task has read and write to the object,
3271 * an error code otherwise
3273 static int smack_key_permission(key_ref_t key_ref,
3274 const struct cred *cred, key_perm_t perm)
3277 struct smk_audit_info ad;
3278 char *tsp = smk_of_task(cred->security);
3280 keyp = key_ref_to_ptr(key_ref);
3284 * If the key hasn't been initialized give it access so that
3287 if (keyp->security == NULL)
3290 * This should not occur
3295 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3296 ad.a.u.key_struct.key = keyp->serial;
3297 ad.a.u.key_struct.key_desc = keyp->description;
3299 return smk_access(tsp, keyp->security,
3300 MAY_READWRITE, &ad);
3302 #endif /* CONFIG_KEYS */
3307 * Audit requires a unique representation of each Smack specific
3308 * rule. This unique representation is used to distinguish the
3309 * object to be audited from remaining kernel objects and also
3310 * works as a glue between the audit hooks.
3312 * Since repository entries are added but never deleted, we'll use
3313 * the smack_known label address related to the given audit rule as
3314 * the needed unique representation. This also better fits the smack
3315 * model where nearly everything is a label.
3320 * smack_audit_rule_init - Initialize a smack audit rule
3321 * @field: audit rule fields given from user-space (audit.h)
3322 * @op: required testing operator (=, !=, >, <, ...)
3323 * @rulestr: smack label to be audited
3324 * @vrule: pointer to save our own audit rule representation
3326 * Prepare to audit cases where (@field @op @rulestr) is true.
3327 * The label to be audited is created if necessay.
3329 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3331 char **rule = (char **)vrule;
3334 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3337 if (op != Audit_equal && op != Audit_not_equal)
3340 *rule = smk_import(rulestr, 0);
3346 * smack_audit_rule_known - Distinguish Smack audit rules
3347 * @krule: rule of interest, in Audit kernel representation format
3349 * This is used to filter Smack rules from remaining Audit ones.
3350 * If it's proved that this rule belongs to us, the
3351 * audit_rule_match hook will be called to do the final judgement.
3353 static int smack_audit_rule_known(struct audit_krule *krule)
3355 struct audit_field *f;
3358 for (i = 0; i < krule->field_count; i++) {
3359 f = &krule->fields[i];
3361 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3369 * smack_audit_rule_match - Audit given object ?
3370 * @secid: security id for identifying the object to test
3371 * @field: audit rule flags given from user-space
3372 * @op: required testing operator
3373 * @vrule: smack internal rule presentation
3374 * @actx: audit context associated with the check