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/dccp.h>
31 #include <linux/slab.h>
32 #include <linux/mutex.h>
33 #include <linux/pipe_fs_i.h>
34 #include <net/cipso_ipv4.h>
37 #include <linux/audit.h>
38 #include <linux/magic.h>
39 #include <linux/dcache.h>
40 #include <linux/personality.h>
41 #include <linux/msg.h>
42 #include <linux/shm.h>
43 #include <linux/binfmts.h>
46 #define task_security(task) (task_cred_xxx((task), security))
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 LIST_HEAD(smk_ipv6_port_list);
58 * smk_fetch - Fetch the smack label from a file.
59 * @ip: a pointer to the inode
60 * @dp: a pointer to the dentry
62 * Returns a pointer to the master list entry for the Smack label
63 * or NULL if there was no label to fetch.
65 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
70 struct smack_known *skp = NULL;
72 if (ip->i_op->getxattr == NULL)
75 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
79 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
81 skp = smk_import_entry(buffer, rc);
89 * new_inode_smack - allocate an inode 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 struct inode_smack *new_inode_smack(char *smack)
96 struct inode_smack *isp;
98 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
102 isp->smk_inode = smack;
104 mutex_init(&isp->smk_lock);
110 * new_task_smack - allocate a task security blob
111 * @smack: a pointer to the Smack label to use in the blob
113 * Returns the new blob or NULL if there's no memory available
115 static struct task_smack *new_task_smack(struct smack_known *task,
116 struct smack_known *forked, gfp_t gfp)
118 struct task_smack *tsp;
120 tsp = kzalloc(sizeof(struct task_smack), gfp);
124 tsp->smk_task = task;
125 tsp->smk_forked = forked;
126 INIT_LIST_HEAD(&tsp->smk_rules);
127 mutex_init(&tsp->smk_rules_lock);
133 * smk_copy_rules - copy a rule set
134 * @nhead - new rules header pointer
135 * @ohead - old rules header pointer
137 * Returns 0 on success, -ENOMEM on error
139 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
142 struct smack_rule *nrp;
143 struct smack_rule *orp;
146 INIT_LIST_HEAD(nhead);
148 list_for_each_entry_rcu(orp, ohead, list) {
149 nrp = kzalloc(sizeof(struct smack_rule), gfp);
155 list_add_rcu(&nrp->list, nhead);
162 * We he, that is fun!
166 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
167 * @ctp: child task pointer
168 * @mode: ptrace attachment mode
170 * Returns 0 if access is OK, an error code otherwise
172 * Do the capability checks, and require read and write.
174 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
177 struct smk_audit_info ad;
178 struct smack_known *skp;
180 rc = cap_ptrace_access_check(ctp, mode);
184 skp = smk_of_task(task_security(ctp));
185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
186 smk_ad_setfield_u_tsk(&ad, ctp);
188 rc = smk_curacc(skp->smk_known, MAY_READWRITE, &ad);
193 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
194 * @ptp: parent task pointer
196 * Returns 0 if access is OK, an error code otherwise
198 * Do the capability checks, and require read and write.
200 static int smack_ptrace_traceme(struct task_struct *ptp)
203 struct smk_audit_info ad;
204 struct smack_known *skp;
206 rc = cap_ptrace_traceme(ptp);
210 skp = smk_of_task(task_security(ptp));
211 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
212 smk_ad_setfield_u_tsk(&ad, ptp);
214 rc = smk_curacc(skp->smk_known, MAY_READWRITE, &ad);
219 * smack_syslog - Smack approval on syslog
220 * @type: message type
222 * Require that the task has the floor label
224 * Returns 0 on success, error code otherwise.
226 static int smack_syslog(int typefrom_file)
229 struct smack_known *skp = smk_of_current();
231 if (smack_privileged(CAP_MAC_OVERRIDE))
234 if (skp != &smack_known_floor)
246 * smack_sb_alloc_security - allocate a superblock blob
247 * @sb: the superblock getting the blob
249 * Returns 0 on success or -ENOMEM on error.
251 static int smack_sb_alloc_security(struct super_block *sb)
253 struct superblock_smack *sbsp;
255 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
260 sbsp->smk_root = smack_known_floor.smk_known;
261 sbsp->smk_default = smack_known_floor.smk_known;
262 sbsp->smk_floor = smack_known_floor.smk_known;
263 sbsp->smk_hat = smack_known_hat.smk_known;
265 * smk_initialized will be zero from kzalloc.
267 sb->s_security = sbsp;
273 * smack_sb_free_security - free a superblock blob
274 * @sb: the superblock getting the blob
277 static void smack_sb_free_security(struct super_block *sb)
279 kfree(sb->s_security);
280 sb->s_security = NULL;
284 * smack_sb_copy_data - copy mount options data for processing
285 * @orig: where to start
286 * @smackopts: mount options string
288 * Returns 0 on success or -ENOMEM on error.
290 * Copy the Smack specific mount options out of the mount
293 static int smack_sb_copy_data(char *orig, char *smackopts)
295 char *cp, *commap, *otheropts, *dp;
297 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
298 if (otheropts == NULL)
301 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
302 if (strstr(cp, SMK_FSDEFAULT) == cp)
304 else if (strstr(cp, SMK_FSFLOOR) == cp)
306 else if (strstr(cp, SMK_FSHAT) == cp)
308 else if (strstr(cp, SMK_FSROOT) == cp)
310 else if (strstr(cp, SMK_FSTRANS) == cp)
315 commap = strchr(cp, ',');
324 strcpy(orig, otheropts);
325 free_page((unsigned long)otheropts);
331 * smack_sb_kern_mount - Smack specific mount processing
332 * @sb: the file system superblock
333 * @flags: the mount flags
334 * @data: the smack mount options
336 * Returns 0 on success, an error code on failure
338 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
340 struct dentry *root = sb->s_root;
341 struct inode *inode = root->d_inode;
342 struct superblock_smack *sp = sb->s_security;
343 struct inode_smack *isp;
349 if (sp->smk_initialized)
352 sp->smk_initialized = 1;
354 for (op = data; op != NULL; op = commap) {
355 commap = strchr(op, ',');
359 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
360 op += strlen(SMK_FSHAT);
361 nsp = smk_import(op, 0);
364 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
365 op += strlen(SMK_FSFLOOR);
366 nsp = smk_import(op, 0);
369 } else if (strncmp(op, SMK_FSDEFAULT,
370 strlen(SMK_FSDEFAULT)) == 0) {
371 op += strlen(SMK_FSDEFAULT);
372 nsp = smk_import(op, 0);
374 sp->smk_default = nsp;
375 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
376 op += strlen(SMK_FSROOT);
377 nsp = smk_import(op, 0);
380 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
381 op += strlen(SMK_FSTRANS);
382 nsp = smk_import(op, 0);
391 * Initialize the root inode.
393 isp = inode->i_security;
394 if (inode->i_security == NULL) {
395 inode->i_security = new_inode_smack(sp->smk_root);
396 isp = inode->i_security;
398 isp->smk_inode = sp->smk_root;
401 isp->smk_flags |= SMK_INODE_TRANSMUTE;
407 * smack_sb_statfs - Smack check on statfs
408 * @dentry: identifies the file system in question
410 * Returns 0 if current can read the floor of the filesystem,
411 * and error code otherwise
413 static int smack_sb_statfs(struct dentry *dentry)
415 struct superblock_smack *sbp = dentry->d_sb->s_security;
417 struct smk_audit_info ad;
419 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
420 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
422 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
427 * smack_sb_mount - Smack check for mounting
434 * Returns 0 if current can write the floor of the filesystem
435 * being mounted on, an error code otherwise.
437 static int smack_sb_mount(const char *dev_name, struct path *path,
438 const char *type, unsigned long flags, void *data)
440 struct superblock_smack *sbp = path->dentry->d_sb->s_security;
441 struct smk_audit_info ad;
443 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
444 smk_ad_setfield_u_fs_path(&ad, *path);
446 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
450 * smack_sb_umount - Smack check for unmounting
451 * @mnt: file system to unmount
454 * Returns 0 if current can write the floor of the filesystem
455 * being unmounted, an error code otherwise.
457 static int smack_sb_umount(struct vfsmount *mnt, int flags)
459 struct superblock_smack *sbp;
460 struct smk_audit_info ad;
463 path.dentry = mnt->mnt_root;
466 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
467 smk_ad_setfield_u_fs_path(&ad, path);
469 sbp = path.dentry->d_sb->s_security;
470 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
478 * smack_bprm_set_creds - set creds for exec
479 * @bprm: the exec information
481 * Returns 0 if it gets a blob, -ENOMEM otherwise
483 static int smack_bprm_set_creds(struct linux_binprm *bprm)
485 struct inode *inode = file_inode(bprm->file);
486 struct task_smack *bsp = bprm->cred->security;
487 struct inode_smack *isp;
490 rc = cap_bprm_set_creds(bprm);
494 if (bprm->cred_prepared)
497 isp = inode->i_security;
498 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
504 bsp->smk_task = isp->smk_task;
505 bprm->per_clear |= PER_CLEAR_ON_SETID;
511 * smack_bprm_committing_creds - Prepare to install the new credentials
514 * @bprm: binprm for exec
516 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
518 struct task_smack *bsp = bprm->cred->security;
520 if (bsp->smk_task != bsp->smk_forked)
521 current->pdeath_signal = 0;
525 * smack_bprm_secureexec - Return the decision to use secureexec.
526 * @bprm: binprm for exec
528 * Returns 0 on success.
530 static int smack_bprm_secureexec(struct linux_binprm *bprm)
532 struct task_smack *tsp = current_security();
533 int ret = cap_bprm_secureexec(bprm);
535 if (!ret && (tsp->smk_task != tsp->smk_forked))
546 * smack_inode_alloc_security - allocate an inode blob
547 * @inode: the inode in need of a blob
549 * Returns 0 if it gets a blob, -ENOMEM otherwise
551 static int smack_inode_alloc_security(struct inode *inode)
553 struct smack_known *skp = smk_of_current();
555 inode->i_security = new_inode_smack(skp->smk_known);
556 if (inode->i_security == NULL)
562 * smack_inode_free_security - free an inode blob
563 * @inode: the inode with a blob
565 * Clears the blob pointer in inode
567 static void smack_inode_free_security(struct inode *inode)
569 kfree(inode->i_security);
570 inode->i_security = NULL;
574 * smack_inode_init_security - copy out the smack from an inode
578 * @name: where to put the attribute name
579 * @value: where to put the attribute value
580 * @len: where to put the length of the attribute
582 * Returns 0 if it all works out, -ENOMEM if there's no memory
584 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
585 const struct qstr *qstr, const char **name,
586 void **value, size_t *len)
588 struct inode_smack *issp = inode->i_security;
589 struct smack_known *skp = smk_of_current();
590 char *isp = smk_of_inode(inode);
591 char *dsp = smk_of_inode(dir);
595 *name = XATTR_SMACK_SUFFIX;
599 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
603 * If the access rule allows transmutation and
604 * the directory requests transmutation then
605 * by all means transmute.
606 * Mark the inode as changed.
608 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
609 smk_inode_transmutable(dir)) {
611 issp->smk_flags |= SMK_INODE_CHANGED;
614 *value = kstrdup(isp, GFP_NOFS);
620 *len = strlen(isp) + 1;
626 * smack_inode_link - Smack check on link
627 * @old_dentry: the existing object
629 * @new_dentry: the new object
631 * Returns 0 if access is permitted, an error code otherwise
633 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
634 struct dentry *new_dentry)
637 struct smk_audit_info ad;
640 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
641 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
643 isp = smk_of_inode(old_dentry->d_inode);
644 rc = smk_curacc(isp, MAY_WRITE, &ad);
646 if (rc == 0 && new_dentry->d_inode != NULL) {
647 isp = smk_of_inode(new_dentry->d_inode);
648 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
649 rc = smk_curacc(isp, MAY_WRITE, &ad);
656 * smack_inode_unlink - Smack check on inode deletion
657 * @dir: containing directory object
658 * @dentry: file to unlink
660 * Returns 0 if current can write the containing directory
661 * and the object, error code otherwise
663 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
665 struct inode *ip = dentry->d_inode;
666 struct smk_audit_info ad;
669 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
670 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
673 * You need write access to the thing you're unlinking
675 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
678 * You also need write access to the containing directory
680 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
681 smk_ad_setfield_u_fs_inode(&ad, dir);
682 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
688 * smack_inode_rmdir - Smack check on directory deletion
689 * @dir: containing directory object
690 * @dentry: directory to unlink
692 * Returns 0 if current can write the containing directory
693 * and the directory, error code otherwise
695 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
697 struct smk_audit_info ad;
700 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
701 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
704 * You need write access to the thing you're removing
706 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
709 * You also need write access to the containing directory
711 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
712 smk_ad_setfield_u_fs_inode(&ad, dir);
713 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
720 * smack_inode_rename - Smack check on rename
721 * @old_inode: the old directory
722 * @old_dentry: unused
723 * @new_inode: the new directory
724 * @new_dentry: unused
726 * Read and write access is required on both the old and
729 * Returns 0 if access is permitted, an error code otherwise
731 static int smack_inode_rename(struct inode *old_inode,
732 struct dentry *old_dentry,
733 struct inode *new_inode,
734 struct dentry *new_dentry)
738 struct smk_audit_info ad;
740 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
741 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
743 isp = smk_of_inode(old_dentry->d_inode);
744 rc = smk_curacc(isp, MAY_READWRITE, &ad);
746 if (rc == 0 && new_dentry->d_inode != NULL) {
747 isp = smk_of_inode(new_dentry->d_inode);
748 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
749 rc = smk_curacc(isp, MAY_READWRITE, &ad);
755 * smack_inode_permission - Smack version of permission()
756 * @inode: the inode in question
757 * @mask: the access requested
759 * This is the important Smack hook.
761 * Returns 0 if access is permitted, -EACCES otherwise
763 static int smack_inode_permission(struct inode *inode, int mask)
765 struct smk_audit_info ad;
766 int no_block = mask & MAY_NOT_BLOCK;
768 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
770 * No permission to check. Existence test. Yup, it's there.
775 /* May be droppable after audit */
778 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
779 smk_ad_setfield_u_fs_inode(&ad, inode);
780 return smk_curacc(smk_of_inode(inode), mask, &ad);
784 * smack_inode_setattr - Smack check for setting attributes
785 * @dentry: the object
786 * @iattr: for the force flag
788 * Returns 0 if access is permitted, an error code otherwise
790 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
792 struct smk_audit_info ad;
794 * Need to allow for clearing the setuid bit.
796 if (iattr->ia_valid & ATTR_FORCE)
798 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
799 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
801 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
805 * smack_inode_getattr - Smack check for getting attributes
807 * @dentry: the object
809 * Returns 0 if access is permitted, an error code otherwise
811 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
813 struct smk_audit_info ad;
816 path.dentry = dentry;
819 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
820 smk_ad_setfield_u_fs_path(&ad, path);
821 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
825 * smack_inode_setxattr - Smack check for setting xattrs
826 * @dentry: the object
827 * @name: name of the attribute
832 * This protects the Smack attribute explicitly.
834 * Returns 0 if access is permitted, an error code otherwise
836 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
837 const void *value, size_t size, int flags)
839 struct smk_audit_info ad;
842 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
843 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
844 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
845 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
846 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
847 if (!smack_privileged(CAP_MAC_ADMIN))
850 * check label validity here so import wont fail on
853 if (size == 0 || size >= SMK_LONGLABEL ||
854 smk_import(value, size) == NULL)
856 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
857 if (!smack_privileged(CAP_MAC_ADMIN))
859 if (size != TRANS_TRUE_SIZE ||
860 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
863 rc = cap_inode_setxattr(dentry, name, value, size, flags);
865 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
866 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
869 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
875 * smack_inode_post_setxattr - Apply the Smack update approved above
877 * @name: attribute name
878 * @value: attribute value
879 * @size: attribute size
882 * Set the pointer in the inode blob to the entry found
883 * in the master label list.
885 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
886 const void *value, size_t size, int flags)
888 struct smack_known *skp;
889 struct inode_smack *isp = dentry->d_inode->i_security;
891 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
892 isp->smk_flags |= SMK_INODE_TRANSMUTE;
896 skp = smk_import_entry(value, size);
897 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
899 isp->smk_inode = skp->smk_known;
901 isp->smk_inode = smack_known_invalid.smk_known;
902 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
906 isp->smk_task = &smack_known_invalid;
907 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
911 isp->smk_mmap = &smack_known_invalid;
918 * smack_inode_getxattr - Smack check on getxattr
919 * @dentry: the object
922 * Returns 0 if access is permitted, an error code otherwise
924 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
926 struct smk_audit_info ad;
928 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
929 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
931 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
935 * smack_inode_removexattr - Smack check on removexattr
936 * @dentry: the object
937 * @name: name of the attribute
939 * Removing the Smack attribute requires CAP_MAC_ADMIN
941 * Returns 0 if access is permitted, an error code otherwise
943 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
945 struct inode_smack *isp;
946 struct smk_audit_info ad;
949 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
950 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
951 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
952 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
953 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
954 strcmp(name, XATTR_NAME_SMACKMMAP)) {
955 if (!smack_privileged(CAP_MAC_ADMIN))
958 rc = cap_inode_removexattr(dentry, name);
960 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
961 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
963 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
966 isp = dentry->d_inode->i_security;
967 isp->smk_task = NULL;
968 isp->smk_mmap = NULL;
975 * smack_inode_getsecurity - get smack xattrs
977 * @name: attribute name
978 * @buffer: where to put the result
981 * Returns the size of the attribute or an error code
983 static int smack_inode_getsecurity(const struct inode *inode,
984 const char *name, void **buffer,
987 struct socket_smack *ssp;
989 struct super_block *sbp;
990 struct inode *ip = (struct inode *)inode;
995 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
996 isp = smk_of_inode(inode);
997 ilen = strlen(isp) + 1;
1003 * The rest of the Smack xattrs are only on sockets.
1006 if (sbp->s_magic != SOCKFS_MAGIC)
1009 sock = SOCKET_I(ip);
1010 if (sock == NULL || sock->sk == NULL)
1013 ssp = sock->sk->sk_security;
1015 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1017 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1018 isp = ssp->smk_out->smk_known;
1022 ilen = strlen(isp) + 1;
1033 * smack_inode_listsecurity - list the Smack attributes
1034 * @inode: the object
1035 * @buffer: where they go
1036 * @buffer_size: size of buffer
1038 * Returns 0 on success, -EINVAL otherwise
1040 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1043 int len = strlen(XATTR_NAME_SMACK);
1045 if (buffer != NULL && len <= buffer_size) {
1046 memcpy(buffer, XATTR_NAME_SMACK, len);
1053 * smack_inode_getsecid - Extract inode's security id
1054 * @inode: inode to extract the info from
1055 * @secid: where result will be saved
1057 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1059 struct inode_smack *isp = inode->i_security;
1061 *secid = smack_to_secid(isp->smk_inode);
1069 * smack_file_permission - Smack check on file operations
1075 * Should access checks be done on each read or write?
1076 * UNICOS and SELinux say yes.
1077 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1079 * I'll say no for now. Smack does not do the frequent
1080 * label changing that SELinux does.
1082 static int smack_file_permission(struct file *file, int mask)
1088 * smack_file_alloc_security - assign a file security blob
1091 * The security blob for a file is a pointer to the master
1092 * label list, so no allocation is done.
1096 static int smack_file_alloc_security(struct file *file)
1098 struct smack_known *skp = smk_of_current();
1100 file->f_security = skp->smk_known;
1105 * smack_file_free_security - clear a file security blob
1108 * The security blob for a file is a pointer to the master
1109 * label list, so no memory is freed.
1111 static void smack_file_free_security(struct file *file)
1113 file->f_security = NULL;
1117 * smack_file_ioctl - Smack check on ioctls
1122 * Relies heavily on the correct use of the ioctl command conventions.
1124 * Returns 0 if allowed, error code otherwise
1126 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1130 struct smk_audit_info ad;
1132 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1133 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1135 if (_IOC_DIR(cmd) & _IOC_WRITE)
1136 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1138 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1139 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1145 * smack_file_lock - Smack check on file locking
1149 * Returns 0 if current has write access, error code otherwise
1151 static int smack_file_lock(struct file *file, unsigned int cmd)
1153 struct smk_audit_info ad;
1155 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1156 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1157 return smk_curacc(file->f_security, MAY_WRITE, &ad);
1161 * smack_file_fcntl - Smack check on fcntl
1163 * @cmd: what action to check
1166 * Generally these operations are harmless.
1167 * File locking operations present an obvious mechanism
1168 * for passing information, so they require write access.
1170 * Returns 0 if current has access, error code otherwise
1172 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1175 struct smk_audit_info ad;
1185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1186 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1187 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1198 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1199 * if mapping anonymous memory.
1200 * @file contains the file structure for file to map (may be NULL).
1201 * @reqprot contains the protection requested by the application.
1202 * @prot contains the protection that will be applied by the kernel.
1203 * @flags contains the operational flags.
1204 * Return 0 if permission is granted.
1206 static int smack_mmap_file(struct file *file,
1207 unsigned long reqprot, unsigned long prot,
1208 unsigned long flags)
1210 struct smack_known *skp;
1211 struct smack_known *mkp;
1212 struct smack_rule *srp;
1213 struct task_smack *tsp;
1215 struct inode_smack *isp;
1224 isp = file_inode(file)->i_security;
1225 if (isp->smk_mmap == NULL)
1227 mkp = isp->smk_mmap;
1229 tsp = current_security();
1230 skp = smk_of_current();
1235 * For each Smack rule associated with the subject
1236 * label verify that the SMACK64MMAP also has access
1237 * to that rule's object label.
1239 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1240 osmack = srp->smk_object;
1242 * Matching labels always allows access.
1244 if (mkp->smk_known == osmack)
1247 * If there is a matching local rule take
1248 * that into account as well.
1250 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1253 may = srp->smk_access;
1255 may &= srp->smk_access;
1257 * If may is zero the SMACK64MMAP subject can't
1258 * possibly have less access.
1264 * Fetch the global list entry.
1265 * If there isn't one a SMACK64MMAP subject
1266 * can't have as much access as current.
1268 mmay = smk_access_entry(mkp->smk_known, osmack,
1270 if (mmay == -ENOENT) {
1275 * If there is a local entry it modifies the
1276 * potential access, too.
1278 tmay = smk_access_entry(mkp->smk_known, osmack,
1280 if (tmay != -ENOENT)
1284 * If there is any access available to current that is
1285 * not available to a SMACK64MMAP subject
1288 if ((may | mmay) != mmay) {
1300 * smack_file_set_fowner - set the file security blob value
1301 * @file: object in question
1304 * Further research may be required on this one.
1306 static int smack_file_set_fowner(struct file *file)
1308 struct smack_known *skp = smk_of_current();
1310 file->f_security = skp->smk_known;
1315 * smack_file_send_sigiotask - Smack on sigio
1316 * @tsk: The target task
1317 * @fown: the object the signal come from
1320 * Allow a privileged task to get signals even if it shouldn't
1322 * Returns 0 if a subject with the object's smack could
1323 * write to the task, an error code otherwise.
1325 static int smack_file_send_sigiotask(struct task_struct *tsk,
1326 struct fown_struct *fown, int signum)
1328 struct smack_known *skp;
1329 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1332 struct smk_audit_info ad;
1335 * struct fown_struct is never outside the context of a struct file
1337 file = container_of(fown, struct file, f_owner);
1339 /* we don't log here as rc can be overriden */
1340 skp = smk_find_entry(file->f_security);
1341 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1342 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1345 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1346 smk_ad_setfield_u_tsk(&ad, tsk);
1347 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1352 * smack_file_receive - Smack file receive check
1355 * Returns 0 if current has access, error code otherwise
1357 static int smack_file_receive(struct file *file)
1360 struct smk_audit_info ad;
1362 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1363 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1365 * This code relies on bitmasks.
1367 if (file->f_mode & FMODE_READ)
1369 if (file->f_mode & FMODE_WRITE)
1372 return smk_curacc(file->f_security, may, &ad);
1376 * smack_file_open - Smack dentry open processing
1380 * Set the security blob in the file structure.
1384 static int smack_file_open(struct file *file, const struct cred *cred)
1386 struct inode_smack *isp = file_inode(file)->i_security;
1388 file->f_security = isp->smk_inode;
1398 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1399 * @new: the new credentials
1400 * @gfp: the atomicity of any memory allocations
1402 * Prepare a blank set of credentials for modification. This must allocate all
1403 * the memory the LSM module might require such that cred_transfer() can
1404 * complete without error.
1406 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1408 struct task_smack *tsp;
1410 tsp = new_task_smack(NULL, NULL, gfp);
1414 cred->security = tsp;
1421 * smack_cred_free - "free" task-level security credentials
1422 * @cred: the credentials in question
1425 static void smack_cred_free(struct cred *cred)
1427 struct task_smack *tsp = cred->security;
1428 struct smack_rule *rp;
1429 struct list_head *l;
1430 struct list_head *n;
1434 cred->security = NULL;
1436 list_for_each_safe(l, n, &tsp->smk_rules) {
1437 rp = list_entry(l, struct smack_rule, list);
1438 list_del(&rp->list);
1445 * smack_cred_prepare - prepare new set of credentials for modification
1446 * @new: the new credentials
1447 * @old: the original credentials
1448 * @gfp: the atomicity of any memory allocations
1450 * Prepare a new set of credentials for modification.
1452 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1455 struct task_smack *old_tsp = old->security;
1456 struct task_smack *new_tsp;
1459 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1460 if (new_tsp == NULL)
1463 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1467 new->security = new_tsp;
1472 * smack_cred_transfer - Transfer the old credentials to the new credentials
1473 * @new: the new credentials
1474 * @old: the original credentials
1476 * Fill in a set of blank credentials from another set of credentials.
1478 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1480 struct task_smack *old_tsp = old->security;
1481 struct task_smack *new_tsp = new->security;
1483 new_tsp->smk_task = old_tsp->smk_task;
1484 new_tsp->smk_forked = old_tsp->smk_task;
1485 mutex_init(&new_tsp->smk_rules_lock);
1486 INIT_LIST_HEAD(&new_tsp->smk_rules);
1489 /* cbs copy rule list */
1493 * smack_kernel_act_as - Set the subjective context in a set of credentials
1494 * @new: points to the set of credentials to be modified.
1495 * @secid: specifies the security ID to be set
1497 * Set the security data for a kernel service.
1499 static int smack_kernel_act_as(struct cred *new, u32 secid)
1501 struct task_smack *new_tsp = new->security;
1502 struct smack_known *skp = smack_from_secid(secid);
1507 new_tsp->smk_task = skp;
1512 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1513 * @new: points to the set of credentials to be modified
1514 * @inode: points to the inode to use as a reference
1516 * Set the file creation context in a set of credentials to the same
1517 * as the objective context of the specified inode
1519 static int smack_kernel_create_files_as(struct cred *new,
1520 struct inode *inode)
1522 struct inode_smack *isp = inode->i_security;
1523 struct task_smack *tsp = new->security;
1525 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1526 tsp->smk_task = tsp->smk_forked;
1531 * smk_curacc_on_task - helper to log task related access
1532 * @p: the task object
1533 * @access: the access requested
1534 * @caller: name of the calling function for audit
1536 * Return 0 if access is permitted
1538 static int smk_curacc_on_task(struct task_struct *p, int access,
1541 struct smk_audit_info ad;
1542 struct smack_known *skp = smk_of_task(task_security(p));
1544 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1545 smk_ad_setfield_u_tsk(&ad, p);
1546 return smk_curacc(skp->smk_known, access, &ad);
1550 * smack_task_setpgid - Smack check on setting pgid
1551 * @p: the task object
1554 * Return 0 if write access is permitted
1556 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1558 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1562 * smack_task_getpgid - Smack access check for getpgid
1563 * @p: the object task
1565 * Returns 0 if current can read the object task, error code otherwise
1567 static int smack_task_getpgid(struct task_struct *p)
1569 return smk_curacc_on_task(p, MAY_READ, __func__);
1573 * smack_task_getsid - Smack access check for getsid
1574 * @p: the object task
1576 * Returns 0 if current can read the object task, error code otherwise
1578 static int smack_task_getsid(struct task_struct *p)
1580 return smk_curacc_on_task(p, MAY_READ, __func__);
1584 * smack_task_getsecid - get the secid of the task
1585 * @p: the object task
1586 * @secid: where to put the result
1588 * Sets the secid to contain a u32 version of the smack label.
1590 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1592 struct smack_known *skp = smk_of_task(task_security(p));
1594 *secid = skp->smk_secid;
1598 * smack_task_setnice - Smack check on setting nice
1599 * @p: the task object
1602 * Return 0 if write access is permitted
1604 static int smack_task_setnice(struct task_struct *p, int nice)
1608 rc = cap_task_setnice(p, nice);
1610 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1615 * smack_task_setioprio - Smack check on setting ioprio
1616 * @p: the task object
1619 * Return 0 if write access is permitted
1621 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1625 rc = cap_task_setioprio(p, ioprio);
1627 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1632 * smack_task_getioprio - Smack check on reading ioprio
1633 * @p: the task object
1635 * Return 0 if read access is permitted
1637 static int smack_task_getioprio(struct task_struct *p)
1639 return smk_curacc_on_task(p, MAY_READ, __func__);
1643 * smack_task_setscheduler - Smack check on setting scheduler
1644 * @p: the task object
1648 * Return 0 if read access is permitted
1650 static int smack_task_setscheduler(struct task_struct *p)
1654 rc = cap_task_setscheduler(p);
1656 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1661 * smack_task_getscheduler - Smack check on reading scheduler
1662 * @p: the task object
1664 * Return 0 if read access is permitted
1666 static int smack_task_getscheduler(struct task_struct *p)
1668 return smk_curacc_on_task(p, MAY_READ, __func__);
1672 * smack_task_movememory - Smack check on moving memory
1673 * @p: the task object
1675 * Return 0 if write access is permitted
1677 static int smack_task_movememory(struct task_struct *p)
1679 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1683 * smack_task_kill - Smack check on signal delivery
1684 * @p: the task object
1687 * @secid: identifies the smack to use in lieu of current's
1689 * Return 0 if write access is permitted
1691 * The secid behavior is an artifact of an SELinux hack
1692 * in the USB code. Someday it may go away.
1694 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1697 struct smk_audit_info ad;
1698 struct smack_known *skp;
1699 struct smack_known *tkp = smk_of_task(task_security(p));
1701 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1702 smk_ad_setfield_u_tsk(&ad, p);
1704 * Sending a signal requires that the sender
1705 * can write the receiver.
1708 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
1710 * If the secid isn't 0 we're dealing with some USB IO
1711 * specific behavior. This is not clean. For one thing
1712 * we can't take privilege into account.
1714 skp = smack_from_secid(secid);
1715 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
1719 * smack_task_wait - Smack access check for waiting
1720 * @p: task to wait for
1724 static int smack_task_wait(struct task_struct *p)
1727 * Allow the operation to succeed.
1729 * In userless environments (e.g. phones) programs
1730 * get marked with SMACK64EXEC and even if the parent
1731 * and child shouldn't be talking the parent still
1732 * may expect to know when the child exits.
1738 * smack_task_to_inode - copy task smack into the inode blob
1739 * @p: task to copy from
1740 * @inode: inode to copy to
1742 * Sets the smack pointer in the inode security blob
1744 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1746 struct inode_smack *isp = inode->i_security;
1747 struct smack_known *skp = smk_of_task(task_security(p));
1749 isp->smk_inode = skp->smk_known;
1757 * smack_sk_alloc_security - Allocate a socket blob
1760 * @gfp_flags: memory allocation flags
1762 * Assign Smack pointers to current
1764 * Returns 0 on success, -ENOMEM is there's no memory
1766 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1768 struct smack_known *skp = smk_of_current();
1769 struct socket_smack *ssp;
1771 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1775 ssp->smk_in = skp->smk_known;
1777 ssp->smk_packet = NULL;
1779 sk->sk_security = ssp;
1785 * smack_sk_free_security - Free a socket blob
1788 * Clears the blob pointer
1790 static void smack_sk_free_security(struct sock *sk)
1792 kfree(sk->sk_security);
1796 * smack_host_label - check host based restrictions
1797 * @sip: the object end
1799 * looks for host based access restrictions
1801 * This version will only be appropriate for really small sets of single label
1802 * hosts. The caller is responsible for ensuring that the RCU read lock is
1803 * taken before calling this function.
1805 * Returns the label of the far end or NULL if it's not special.
1807 static char *smack_host_label(struct sockaddr_in *sip)
1809 struct smk_netlbladdr *snp;
1810 struct in_addr *siap = &sip->sin_addr;
1812 if (siap->s_addr == 0)
1815 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1817 * we break after finding the first match because
1818 * the list is sorted from longest to shortest mask
1819 * so we have found the most specific match
1821 if ((&snp->smk_host.sin_addr)->s_addr ==
1822 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1823 /* we have found the special CIPSO option */
1824 if (snp->smk_label == smack_cipso_option)
1826 return snp->smk_label;
1833 * smack_netlabel - Set the secattr on a socket
1835 * @labeled: socket label scheme
1837 * Convert the outbound smack value (smk_out) to a
1838 * secattr and attach it to the socket.
1840 * Returns 0 on success or an error code
1842 static int smack_netlabel(struct sock *sk, int labeled)
1844 struct smack_known *skp;
1845 struct socket_smack *ssp = sk->sk_security;
1849 * Usually the netlabel code will handle changing the
1850 * packet labeling based on the label.
1851 * The case of a single label host is different, because
1852 * a single label host should never get a labeled packet
1853 * even though the label is usually associated with a packet
1857 bh_lock_sock_nested(sk);
1859 if (ssp->smk_out == smack_net_ambient ||
1860 labeled == SMACK_UNLABELED_SOCKET)
1861 netlbl_sock_delattr(sk);
1864 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
1874 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1876 * @sap: the destination address
1878 * Set the correct secattr for the given socket based on the destination
1879 * address and perform any outbound access checks needed.
1881 * Returns 0 on success or an error code.
1884 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1886 struct smack_known *skp;
1890 struct socket_smack *ssp = sk->sk_security;
1891 struct smk_audit_info ad;
1894 hostsp = smack_host_label(sap);
1895 if (hostsp != NULL) {
1897 struct lsm_network_audit net;
1899 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1900 ad.a.u.net->family = sap->sin_family;
1901 ad.a.u.net->dport = sap->sin_port;
1902 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
1904 sk_lbl = SMACK_UNLABELED_SOCKET;
1906 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
1908 sk_lbl = SMACK_CIPSO_SOCKET;
1915 return smack_netlabel(sk, sk_lbl);
1919 * smk_ipv6_port_label - Smack port access table management
1923 * Create or update the port list entry
1925 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1927 struct sock *sk = sock->sk;
1928 struct sockaddr_in6 *addr6;
1929 struct socket_smack *ssp = sock->sk->sk_security;
1930 struct smk_port_label *spp;
1931 unsigned short port = 0;
1933 if (address == NULL) {
1935 * This operation is changing the Smack information
1936 * on the bound socket. Take the changes to the port
1939 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1940 if (sk != spp->smk_sock)
1942 spp->smk_in = ssp->smk_in;
1943 spp->smk_out = ssp->smk_out;
1947 * A NULL address is only used for updating existing
1948 * bound entries. If there isn't one, it's OK.
1953 addr6 = (struct sockaddr_in6 *)address;
1954 port = ntohs(addr6->sin6_port);
1956 * This is a special case that is safely ignored.
1962 * Look for an existing port list entry.
1963 * This is an indication that a port is getting reused.
1965 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1966 if (spp->smk_port != port)
1968 spp->smk_port = port;
1970 spp->smk_in = ssp->smk_in;
1971 spp->smk_out = ssp->smk_out;
1976 * A new port entry is required.
1978 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1982 spp->smk_port = port;
1984 spp->smk_in = ssp->smk_in;
1985 spp->smk_out = ssp->smk_out;
1987 list_add(&spp->list, &smk_ipv6_port_list);
1992 * smk_ipv6_port_check - check Smack port access
1996 * Create or update the port list entry
1998 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2003 struct smk_port_label *spp;
2004 struct socket_smack *ssp = sk->sk_security;
2005 struct smack_known *skp;
2006 unsigned short port = 0;
2008 struct smk_audit_info ad;
2010 struct lsm_network_audit net;
2013 if (act == SMK_RECEIVING) {
2014 skp = smack_net_ambient;
2015 object = ssp->smk_in;
2018 object = smack_net_ambient->smk_known;
2022 * Get the IP address and port from the address.
2024 port = ntohs(address->sin6_port);
2025 bep = (__be16 *)(&address->sin6_addr);
2026 be32p = (__be32 *)(&address->sin6_addr);
2029 * It's remote, so port lookup does no good.
2031 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2035 * It's local so the send check has to have passed.
2037 if (act == SMK_RECEIVING) {
2038 skp = &smack_known_web;
2042 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2043 if (spp->smk_port != port)
2045 object = spp->smk_in;
2046 if (act == SMK_CONNECTING)
2047 ssp->smk_packet = spp->smk_out->smk_known;
2054 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2055 ad.a.u.net->family = sk->sk_family;
2056 ad.a.u.net->dport = port;
2057 if (act == SMK_RECEIVING)
2058 ad.a.u.net->v6info.saddr = address->sin6_addr;
2060 ad.a.u.net->v6info.daddr = address->sin6_addr;
2062 return smk_access(skp, object, MAY_WRITE, &ad);
2066 * smack_inode_setsecurity - set smack xattrs
2067 * @inode: the object
2068 * @name: attribute name
2069 * @value: attribute value
2070 * @size: size of the attribute
2073 * Sets the named attribute in the appropriate blob
2075 * Returns 0 on success, or an error code
2077 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2078 const void *value, size_t size, int flags)
2080 struct smack_known *skp;
2081 struct inode_smack *nsp = inode->i_security;
2082 struct socket_smack *ssp;
2083 struct socket *sock;
2086 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2089 skp = smk_import_entry(value, size);
2093 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2094 nsp->smk_inode = skp->smk_known;
2095 nsp->smk_flags |= SMK_INODE_INSTANT;
2099 * The rest of the Smack xattrs are only on sockets.
2101 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2104 sock = SOCKET_I(inode);
2105 if (sock == NULL || sock->sk == NULL)
2108 ssp = sock->sk->sk_security;
2110 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2111 ssp->smk_in = skp->smk_known;
2112 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2114 if (sock->sk->sk_family == PF_INET) {
2115 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2118 "Smack: \"%s\" netlbl error %d.\n",
2124 if (sock->sk->sk_family == PF_INET6)
2125 smk_ipv6_port_label(sock, NULL);
2131 * smack_socket_post_create - finish socket setup
2133 * @family: protocol family
2138 * Sets the netlabel information on the socket
2140 * Returns 0 on success, and error code otherwise
2142 static int smack_socket_post_create(struct socket *sock, int family,
2143 int type, int protocol, int kern)
2145 if (family != PF_INET || sock->sk == NULL)
2148 * Set the outbound netlbl.
2150 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2154 * smack_socket_bind - record port binding information.
2156 * @address: the port address
2157 * @addrlen: size of the address
2159 * Records the label bound to a port.
2163 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2166 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2167 smk_ipv6_port_label(sock, address);
2173 * smack_socket_connect - connect access check
2175 * @sap: the other end
2176 * @addrlen: size of sap
2178 * Verifies that a connection may be possible
2180 * Returns 0 on success, and error code otherwise
2182 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2187 if (sock->sk == NULL)
2190 switch (sock->sk->sk_family) {
2192 if (addrlen < sizeof(struct sockaddr_in))
2194 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2197 if (addrlen < sizeof(struct sockaddr_in6))
2199 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2207 * smack_flags_to_may - convert S_ to MAY_ values
2208 * @flags: the S_ value
2210 * Returns the equivalent MAY_ value
2212 static int smack_flags_to_may(int flags)
2216 if (flags & S_IRUGO)
2218 if (flags & S_IWUGO)
2220 if (flags & S_IXUGO)
2227 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2232 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2234 struct smack_known *skp = smk_of_current();
2236 msg->security = skp->smk_known;
2241 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2244 * Clears the blob pointer
2246 static void smack_msg_msg_free_security(struct msg_msg *msg)
2248 msg->security = NULL;
2252 * smack_of_shm - the smack pointer for the shm
2255 * Returns a pointer to the smack value
2257 static char *smack_of_shm(struct shmid_kernel *shp)
2259 return (char *)shp->shm_perm.security;
2263 * smack_shm_alloc_security - Set the security blob for shm
2268 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2270 struct kern_ipc_perm *isp = &shp->shm_perm;
2271 struct smack_known *skp = smk_of_current();
2273 isp->security = skp->smk_known;
2278 * smack_shm_free_security - Clear the security blob for shm
2281 * Clears the blob pointer
2283 static void smack_shm_free_security(struct shmid_kernel *shp)
2285 struct kern_ipc_perm *isp = &shp->shm_perm;
2287 isp->security = NULL;
2291 * smk_curacc_shm : check if current has access on shm
2293 * @access : access requested
2295 * Returns 0 if current has the requested access, error code otherwise
2297 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2299 char *ssp = smack_of_shm(shp);
2300 struct smk_audit_info ad;
2303 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2304 ad.a.u.ipc_id = shp->shm_perm.id;
2306 return smk_curacc(ssp, access, &ad);
2310 * smack_shm_associate - Smack access check for shm
2312 * @shmflg: access requested
2314 * Returns 0 if current has the requested access, error code otherwise
2316 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2320 may = smack_flags_to_may(shmflg);
2321 return smk_curacc_shm(shp, may);
2325 * smack_shm_shmctl - Smack access check for shm
2327 * @cmd: what it wants to do
2329 * Returns 0 if current has the requested access, error code otherwise
2331 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2344 may = MAY_READWRITE;
2349 * System level information.
2355 return smk_curacc_shm(shp, may);
2359 * smack_shm_shmat - Smack access for shmat
2362 * @shmflg: access requested
2364 * Returns 0 if current has the requested access, error code otherwise
2366 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2371 may = smack_flags_to_may(shmflg);
2372 return smk_curacc_shm(shp, may);
2376 * smack_of_sem - the smack pointer for the sem
2379 * Returns a pointer to the smack value
2381 static char *smack_of_sem(struct sem_array *sma)
2383 return (char *)sma->sem_perm.security;
2387 * smack_sem_alloc_security - Set the security blob for sem
2392 static int smack_sem_alloc_security(struct sem_array *sma)
2394 struct kern_ipc_perm *isp = &sma->sem_perm;
2395 struct smack_known *skp = smk_of_current();
2397 isp->security = skp->smk_known;
2402 * smack_sem_free_security - Clear the security blob for sem
2405 * Clears the blob pointer
2407 static void smack_sem_free_security(struct sem_array *sma)
2409 struct kern_ipc_perm *isp = &sma->sem_perm;
2411 isp->security = NULL;
2415 * smk_curacc_sem : check if current has access on sem
2417 * @access : access requested
2419 * Returns 0 if current has the requested access, error code otherwise
2421 static int smk_curacc_sem(struct sem_array *sma, int access)
2423 char *ssp = smack_of_sem(sma);
2424 struct smk_audit_info ad;
2427 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2428 ad.a.u.ipc_id = sma->sem_perm.id;
2430 return smk_curacc(ssp, access, &ad);
2434 * smack_sem_associate - Smack access check for sem
2436 * @semflg: access requested
2438 * Returns 0 if current has the requested access, error code otherwise
2440 static int smack_sem_associate(struct sem_array *sma, int semflg)
2444 may = smack_flags_to_may(semflg);
2445 return smk_curacc_sem(sma, may);
2449 * smack_sem_shmctl - Smack access check for sem
2451 * @cmd: what it wants to do
2453 * Returns 0 if current has the requested access, error code otherwise
2455 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2473 may = MAY_READWRITE;
2478 * System level information
2485 return smk_curacc_sem(sma, may);
2489 * smack_sem_semop - Smack checks of semaphore operations
2495 * Treated as read and write in all cases.
2497 * Returns 0 if access is allowed, error code otherwise
2499 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2500 unsigned nsops, int alter)
2502 return smk_curacc_sem(sma, MAY_READWRITE);
2506 * smack_msg_alloc_security - Set the security blob for msg
2511 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2513 struct kern_ipc_perm *kisp = &msq->q_perm;
2514 struct smack_known *skp = smk_of_current();
2516 kisp->security = skp->smk_known;
2521 * smack_msg_free_security - Clear the security blob for msg
2524 * Clears the blob pointer
2526 static void smack_msg_queue_free_security(struct msg_queue *msq)
2528 struct kern_ipc_perm *kisp = &msq->q_perm;
2530 kisp->security = NULL;
2534 * smack_of_msq - the smack pointer for the msq
2537 * Returns a pointer to the smack value
2539 static char *smack_of_msq(struct msg_queue *msq)
2541 return (char *)msq->q_perm.security;
2545 * smk_curacc_msq : helper to check if current has access on msq
2547 * @access : access requested
2549 * return 0 if current has access, error otherwise
2551 static int smk_curacc_msq(struct msg_queue *msq, int access)
2553 char *msp = smack_of_msq(msq);
2554 struct smk_audit_info ad;
2557 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2558 ad.a.u.ipc_id = msq->q_perm.id;
2560 return smk_curacc(msp, access, &ad);
2564 * smack_msg_queue_associate - Smack access check for msg_queue
2566 * @msqflg: access requested
2568 * Returns 0 if current has the requested access, error code otherwise
2570 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2574 may = smack_flags_to_may(msqflg);
2575 return smk_curacc_msq(msq, may);
2579 * smack_msg_queue_msgctl - Smack access check for msg_queue
2581 * @cmd: what it wants to do
2583 * Returns 0 if current has the requested access, error code otherwise
2585 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2596 may = MAY_READWRITE;
2601 * System level information
2608 return smk_curacc_msq(msq, may);
2612 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2615 * @msqflg: access requested
2617 * Returns 0 if current has the requested access, error code otherwise
2619 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2624 may = smack_flags_to_may(msqflg);
2625 return smk_curacc_msq(msq, may);
2629 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2636 * Returns 0 if current has read and write access, error code otherwise
2638 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2639 struct task_struct *target, long type, int mode)
2641 return smk_curacc_msq(msq, MAY_READWRITE);
2645 * smack_ipc_permission - Smack access for ipc_permission()
2646 * @ipp: the object permissions
2647 * @flag: access requested
2649 * Returns 0 if current has read and write access, error code otherwise
2651 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2653 char *isp = ipp->security;
2654 int may = smack_flags_to_may(flag);
2655 struct smk_audit_info ad;
2658 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2659 ad.a.u.ipc_id = ipp->id;
2661 return smk_curacc(isp, may, &ad);
2665 * smack_ipc_getsecid - Extract smack security id
2666 * @ipp: the object permissions
2667 * @secid: where result will be saved
2669 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2671 char *smack = ipp->security;
2673 *secid = smack_to_secid(smack);
2677 * smack_d_instantiate - Make sure the blob is correct on an inode
2678 * @opt_dentry: dentry where inode will be attached
2679 * @inode: the object
2681 * Set the inode's security blob if it hasn't been done already.
2683 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2685 struct super_block *sbp;
2686 struct superblock_smack *sbsp;
2687 struct inode_smack *isp;
2688 struct smack_known *skp;
2689 struct smack_known *ckp = smk_of_current();
2691 char trattr[TRANS_TRUE_SIZE];
2699 isp = inode->i_security;
2701 mutex_lock(&isp->smk_lock);
2703 * If the inode is already instantiated
2704 * take the quick way out
2706 if (isp->smk_flags & SMK_INODE_INSTANT)
2710 sbsp = sbp->s_security;
2712 * We're going to use the superblock default label
2713 * if there's no label on the file.
2715 final = sbsp->smk_default;
2718 * If this is the root inode the superblock
2719 * may be in the process of initialization.
2720 * If that is the case use the root value out
2721 * of the superblock.
2723 if (opt_dentry->d_parent == opt_dentry) {
2724 isp->smk_inode = sbsp->smk_root;
2725 isp->smk_flags |= SMK_INODE_INSTANT;
2730 * This is pretty hackish.
2731 * Casey says that we shouldn't have to do
2732 * file system specific code, but it does help
2733 * with keeping it simple.
2735 switch (sbp->s_magic) {
2738 * Casey says that it's a little embarrassing
2739 * that the smack file system doesn't do
2740 * extended attributes.
2742 final = smack_known_star.smk_known;
2746 * Casey says pipes are easy (?)
2748 final = smack_known_star.smk_known;
2750 case DEVPTS_SUPER_MAGIC:
2752 * devpts seems content with the label of the task.
2753 * Programs that change smack have to treat the
2756 final = ckp->smk_known;
2760 * Socket access is controlled by the socket
2761 * structures associated with the task involved.
2763 final = smack_known_star.smk_known;
2765 case PROC_SUPER_MAGIC:
2767 * Casey says procfs appears not to care.
2768 * The superblock default suffices.
2773 * Device labels should come from the filesystem,
2774 * but watch out, because they're volitile,
2775 * getting recreated on every reboot.
2777 final = smack_known_star.smk_known;
2781 * If a smack value has been set we want to use it,
2782 * but since tmpfs isn't giving us the opportunity
2783 * to set mount options simulate setting the
2784 * superblock default.
2788 * This isn't an understood special case.
2789 * Get the value from the xattr.
2793 * UNIX domain sockets use lower level socket data.
2795 if (S_ISSOCK(inode->i_mode)) {
2796 final = smack_known_star.smk_known;
2800 * No xattr support means, alas, no SMACK label.
2801 * Use the aforeapplied default.
2802 * It would be curious if the label of the task
2803 * does not match that assigned.
2805 if (inode->i_op->getxattr == NULL)
2808 * Get the dentry for xattr.
2810 dp = dget(opt_dentry);
2811 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2813 final = skp->smk_known;
2816 * Transmuting directory
2818 if (S_ISDIR(inode->i_mode)) {
2820 * If this is a new directory and the label was
2821 * transmuted when the inode was initialized
2822 * set the transmute attribute on the directory
2823 * and mark the inode.
2825 * If there is a transmute attribute on the
2826 * directory mark the inode.
2828 if (isp->smk_flags & SMK_INODE_CHANGED) {
2829 isp->smk_flags &= ~SMK_INODE_CHANGED;
2830 rc = inode->i_op->setxattr(dp,
2831 XATTR_NAME_SMACKTRANSMUTE,
2832 TRANS_TRUE, TRANS_TRUE_SIZE,
2835 rc = inode->i_op->getxattr(dp,
2836 XATTR_NAME_SMACKTRANSMUTE, trattr,
2838 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2839 TRANS_TRUE_SIZE) != 0)
2843 transflag = SMK_INODE_TRANSMUTE;
2845 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2846 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2853 isp->smk_inode = ckp->smk_known;
2855 isp->smk_inode = final;
2857 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2860 mutex_unlock(&isp->smk_lock);
2865 * smack_getprocattr - Smack process attribute access
2866 * @p: the object task
2867 * @name: the name of the attribute in /proc/.../attr
2868 * @value: where to put the result
2870 * Places a copy of the task Smack into value
2872 * Returns the length of the smack label or an error code
2874 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2876 struct smack_known *skp = smk_of_task(task_security(p));
2880 if (strcmp(name, "current") != 0)
2883 cp = kstrdup(skp->smk_known, GFP_KERNEL);
2893 * smack_setprocattr - Smack process attribute setting
2894 * @p: the object task
2895 * @name: the name of the attribute in /proc/.../attr
2896 * @value: the value to set
2897 * @size: the size of the value
2899 * Sets the Smack value of the task. Only setting self
2900 * is permitted and only with privilege
2902 * Returns the length of the smack label or an error code
2904 static int smack_setprocattr(struct task_struct *p, char *name,
2905 void *value, size_t size)
2907 struct task_smack *tsp;
2909 struct smack_known *skp;
2912 * Changing another process' Smack value is too dangerous
2913 * and supports no sane use case.
2918 if (!smack_privileged(CAP_MAC_ADMIN))
2921 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
2924 if (strcmp(name, "current") != 0)
2927 skp = smk_import_entry(value, size);
2932 * No process is ever allowed the web ("@") label.
2934 if (skp == &smack_known_web)
2937 new = prepare_creds();
2941 tsp = new->security;
2942 tsp->smk_task = skp;
2949 * smack_unix_stream_connect - Smack access on UDS
2951 * @other: the other sock
2954 * Return 0 if a subject with the smack of sock could access
2955 * an object with the smack of other, otherwise an error code
2957 static int smack_unix_stream_connect(struct sock *sock,
2958 struct sock *other, struct sock *newsk)
2960 struct smack_known *skp;
2961 struct socket_smack *ssp = sock->sk_security;
2962 struct socket_smack *osp = other->sk_security;
2963 struct socket_smack *nsp = newsk->sk_security;
2964 struct smk_audit_info ad;
2968 struct lsm_network_audit net;
2970 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2971 smk_ad_setfield_u_net_sk(&ad, other);
2974 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
2976 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
2980 * Cross reference the peer labels for SO_PEERSEC.
2983 nsp->smk_packet = ssp->smk_out->smk_known;
2984 ssp->smk_packet = osp->smk_out->smk_known;
2991 * smack_unix_may_send - Smack access on UDS
2993 * @other: the other socket
2995 * Return 0 if a subject with the smack of sock could access
2996 * an object with the smack of other, otherwise an error code
2998 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3000 struct socket_smack *ssp = sock->sk->sk_security;
3001 struct socket_smack *osp = other->sk->sk_security;
3002 struct smack_known *skp;
3003 struct smk_audit_info ad;
3006 struct lsm_network_audit net;
3008 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3009 smk_ad_setfield_u_net_sk(&ad, other->sk);
3012 if (smack_privileged(CAP_MAC_OVERRIDE))
3016 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3020 * smack_socket_sendmsg - Smack check based on destination host
3023 * @size: the size of the message
3025 * Return 0 if the current subject can write to the destination host.
3026 * For IPv4 this is only a question if the destination is a single label host.
3027 * For IPv6 this is a check against the label of the port.
3029 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3032 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3033 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3037 * Perfectly reasonable for this to be NULL
3042 switch (sip->sin_family) {
3044 rc = smack_netlabel_send(sock->sk, sip);
3047 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3054 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3055 * @sap: netlabel secattr
3056 * @ssp: socket security information
3058 * Returns a pointer to a Smack label entry found on the label list.
3060 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3061 struct socket_smack *ssp)
3063 struct smack_known *skp;
3068 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3070 * Looks like a CIPSO packet.
3071 * If there are flags but no level netlabel isn't
3072 * behaving the way we expect it to.
3074 * Look it up in the label table
3075 * Without guidance regarding the smack value
3076 * for the packet fall back on the network
3080 list_for_each_entry(skp, &smack_known_list, list) {
3081 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3084 * Compare the catsets. Use the netlbl APIs.
3086 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3087 if ((skp->smk_netlabel.flags &
3088 NETLBL_SECATTR_MLS_CAT) == 0)
3092 for (acat = -1, kcat = -1; acat == kcat; ) {
3093 acat = netlbl_secattr_catmap_walk(
3094 sap->attr.mls.cat, acat + 1);
3095 kcat = netlbl_secattr_catmap_walk(
3096 skp->smk_netlabel.attr.mls.cat,
3098 if (acat < 0 || kcat < 0)
3111 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
3112 return &smack_known_web;
3113 return &smack_known_star;
3115 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3117 * Looks like a fallback, which gives us a secid.
3119 skp = smack_from_secid(sap->attr.secid);
3121 * This has got to be a bug because it is
3122 * impossible to specify a fallback without
3123 * specifying the label, which will ensure
3124 * it has a secid, and the only way to get a
3125 * secid is from a fallback.
3127 BUG_ON(skp == NULL);
3131 * Without guidance regarding the smack value
3132 * for the packet fall back on the network
3135 return smack_net_ambient;
3138 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3142 int proto = -EINVAL;
3143 struct ipv6hdr _ipv6h;
3144 struct ipv6hdr *ip6;
3146 struct tcphdr _tcph, *th;
3147 struct udphdr _udph, *uh;
3148 struct dccp_hdr _dccph, *dh;
3152 offset = skb_network_offset(skb);
3153 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3156 sip->sin6_addr = ip6->saddr;
3158 nexthdr = ip6->nexthdr;
3159 offset += sizeof(_ipv6h);
3160 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3167 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3169 sip->sin6_port = th->source;
3172 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3174 sip->sin6_port = uh->source;
3177 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3179 sip->sin6_port = dh->dccph_sport;
3186 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3190 * Returns 0 if the packet should be delivered, an error code otherwise
3192 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3194 struct netlbl_lsm_secattr secattr;
3195 struct socket_smack *ssp = sk->sk_security;
3196 struct smack_known *skp;
3197 struct sockaddr_in6 sadd;
3199 struct smk_audit_info ad;
3201 struct lsm_network_audit net;
3203 switch (sk->sk_family) {
3206 * Translate what netlabel gave us.
3208 netlbl_secattr_init(&secattr);
3210 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3212 skp = smack_from_secattr(&secattr, ssp);
3214 skp = smack_net_ambient;
3216 netlbl_secattr_destroy(&secattr);
3219 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3220 ad.a.u.net->family = sk->sk_family;
3221 ad.a.u.net->netif = skb->skb_iif;
3222 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3225 * Receiving a packet requires that the other end
3226 * be able to write here. Read access is not required.
3227 * This is the simplist possible security model
3230 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3232 netlbl_skbuff_err(skb, rc, 0);
3235 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3236 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3237 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3246 * smack_socket_getpeersec_stream - pull in packet label
3248 * @optval: user's destination
3249 * @optlen: size thereof
3252 * returns zero on success, an error code otherwise
3254 static int smack_socket_getpeersec_stream(struct socket *sock,
3255 char __user *optval,
3256 int __user *optlen, unsigned len)
3258 struct socket_smack *ssp;
3263 ssp = sock->sk->sk_security;
3264 if (ssp->smk_packet != NULL) {
3265 rcp = ssp->smk_packet;
3266 slen = strlen(rcp) + 1;
3271 else if (copy_to_user(optval, rcp, slen) != 0)
3274 if (put_user(slen, optlen) != 0)
3282 * smack_socket_getpeersec_dgram - pull in packet label
3283 * @sock: the peer socket
3285 * @secid: pointer to where to put the secid of the packet
3287 * Sets the netlabel socket state on sk from parent
3289 static int smack_socket_getpeersec_dgram(struct socket *sock,
3290 struct sk_buff *skb, u32 *secid)
3293 struct netlbl_lsm_secattr secattr;
3294 struct socket_smack *ssp = NULL;
3295 struct smack_known *skp;
3296 int family = PF_UNSPEC;
3297 u32 s = 0; /* 0 is the invalid secid */
3301 if (skb->protocol == htons(ETH_P_IP))
3303 else if (skb->protocol == htons(ETH_P_IPV6))
3306 if (family == PF_UNSPEC && sock != NULL)
3307 family = sock->sk->sk_family;
3309 if (family == PF_UNIX) {
3310 ssp = sock->sk->sk_security;
3311 s = ssp->smk_out->smk_secid;
3312 } else if (family == PF_INET || family == PF_INET6) {
3314 * Translate what netlabel gave us.
3316 if (sock != NULL && sock->sk != NULL)
3317 ssp = sock->sk->sk_security;
3318 netlbl_secattr_init(&secattr);
3319 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3321 skp = smack_from_secattr(&secattr, ssp);
3324 netlbl_secattr_destroy(&secattr);
3333 * smack_sock_graft - Initialize a newly created socket with an existing sock
3335 * @parent: parent socket
3337 * Set the smk_{in,out} state of an existing sock based on the process that
3338 * is creating the new socket.
3340 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3342 struct socket_smack *ssp;
3343 struct smack_known *skp = smk_of_current();
3346 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3349 ssp = sk->sk_security;
3350 ssp->smk_in = skp->smk_known;
3352 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3356 * smack_inet_conn_request - Smack access check on connect
3357 * @sk: socket involved
3361 * Returns 0 if a task with the packet label could write to
3362 * the socket, otherwise an error code
3364 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3365 struct request_sock *req)
3367 u16 family = sk->sk_family;
3368 struct smack_known *skp;
3369 struct socket_smack *ssp = sk->sk_security;
3370 struct netlbl_lsm_secattr secattr;
3371 struct sockaddr_in addr;
3375 struct smk_audit_info ad;
3377 struct lsm_network_audit net;
3380 if (family == PF_INET6) {
3382 * Handle mapped IPv4 packets arriving
3383 * via IPv6 sockets. Don't set up netlabel
3384 * processing on IPv6.
3386 if (skb->protocol == htons(ETH_P_IP))
3392 netlbl_secattr_init(&secattr);
3393 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3395 skp = smack_from_secattr(&secattr, ssp);
3397 skp = &smack_known_huh;
3398 netlbl_secattr_destroy(&secattr);
3401 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3402 ad.a.u.net->family = family;
3403 ad.a.u.net->netif = skb->skb_iif;
3404 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3407 * Receiving a packet requires that the other end be able to write
3408 * here. Read access is not required.
3410 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3415 * Save the peer's label in the request_sock so we can later setup
3416 * smk_packet in the child socket so that SO_PEERCRED can report it.
3418 req->peer_secid = skp->smk_secid;
3421 * We need to decide if we want to label the incoming connection here
3422 * if we do we only need to label the request_sock and the stack will
3423 * propagate the wire-label to the sock when it is created.
3426 addr.sin_addr.s_addr = hdr->saddr;
3428 hsp = smack_host_label(&addr);
3432 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3434 netlbl_req_delattr(req);
3440 * smack_inet_csk_clone - Copy the connection information to the new socket
3441 * @sk: the new socket
3442 * @req: the connection's request_sock
3444 * Transfer the connection's peer label to the newly created socket.
3446 static void smack_inet_csk_clone(struct sock *sk,
3447 const struct request_sock *req)
3449 struct socket_smack *ssp = sk->sk_security;
3450 struct smack_known *skp;
3452 if (req->peer_secid != 0) {
3453 skp = smack_from_secid(req->peer_secid);
3454 ssp->smk_packet = skp->smk_known;
3456 ssp->smk_packet = NULL;
3460 * Key management security hooks
3462 * Casey has not tested key support very heavily.
3463 * The permission check is most likely too restrictive.
3464 * If you care about keys please have a look.
3469 * smack_key_alloc - Set the key security blob
3471 * @cred: the credentials to use
3474 * No allocation required
3478 static int smack_key_alloc(struct key *key, const struct cred *cred,
3479 unsigned long flags)
3481 struct smack_known *skp = smk_of_task(cred->security);
3483 key->security = skp->smk_known;
3488 * smack_key_free - Clear the key security blob
3491 * Clear the blob pointer
3493 static void smack_key_free(struct key *key)
3495 key->security = NULL;
3499 * smack_key_permission - Smack access on a key
3500 * @key_ref: gets to the object
3501 * @cred: the credentials to use
3504 * Return 0 if the task has read and write to the object,
3505 * an error code otherwise
3507 static int smack_key_permission(key_ref_t key_ref,
3508 const struct cred *cred, key_perm_t perm)
3511 struct smk_audit_info ad;
3512 struct smack_known *tkp = smk_of_task(cred->security);
3514 keyp = key_ref_to_ptr(key_ref);
3518 * If the key hasn't been initialized give it access so that
3521 if (keyp->security == NULL)
3524 * This should not occur
3529 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3530 ad.a.u.key_struct.key = keyp->serial;
3531 ad.a.u.key_struct.key_desc = keyp->description;
3533 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
3535 #endif /* CONFIG_KEYS */
3540 * Audit requires a unique representation of each Smack specific
3541 * rule. This unique representation is used to distinguish the
3542 * object to be audited from remaining kernel objects and also
3543 * works as a glue between the audit hooks.
3545 * Since repository entries are added but never deleted, we'll use
3546 * the smack_known label address related to the given audit rule as
3547 * the needed unique representation. This also better fits the smack
3548 * model where nearly everything is a label.
3553 * smack_audit_rule_init - Initialize a smack audit rule
3554 * @field: audit rule fields given from user-space (audit.h)
3555 * @op: required testing operator (=, !=, >, <, ...)
3556 * @rulestr: smack label to be audited
3557 * @vrule: pointer to save our own audit rule representation
3559 * Prepare to audit cases where (@field @op @rulestr) is true.
3560 * The label to be audited is created if necessay.
3562 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3564 char **rule = (char **)vrule;
3567 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3570 if (op != Audit_equal && op != Audit_not_equal)
3573 *rule = smk_import(rulestr, 0);
3579 * smack_audit_rule_known - Distinguish Smack audit rules
3580 * @krule: rule of interest, in Audit kernel representation format
3582 * This is used to filter Smack rules from remaining Audit ones.
3583 * If it's proved that this rule belongs to us, the
3584 * audit_rule_match hook will be called to do the final judgement.
3586 static int smack_audit_rule_known(struct audit_krule *krule)
3588 struct audit_field *f;
3591 for (i = 0; i < krule->field_count; i++) {
3592 f = &krule->fields[i];
3594 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3602 * smack_audit_rule_match - Audit given object ?
3603 * @secid: security id for identifying the object to test
3604 * @field: audit rule flags given from user-space
3605 * @op: required testing operator
3606 * @vrule: smack internal rule presentation
3607 * @actx: audit context associated with the check
3609 * The core Audit hook. It's used to take the decision of
3610 * whether to audit or not to audit a given object.
3612 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3613 struct audit_context *actx)
3615 struct smack_known *skp;
3619 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
3620 "Smack: missing rule\n");
3624 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3627 skp = smack_from_secid(secid);
3630 * No need to do string comparisons. If a match occurs,
3631 * both pointers will point to the same smack_known
3634 if (op == Audit_equal)
3635 return (rule == skp->smk_known);
3636 if (op == Audit_not_equal)
3637 return (rule != skp->smk_known);
3643 * smack_audit_rule_free - free smack rule representation
3644 * @vrule: rule to be freed.
3646 * No memory was allocated.
3648 static void smack_audit_rule_free(void *vrule)
3653 #endif /* CONFIG_AUDIT */
3656 * smack_ismaclabel - check if xattr @name references a smack MAC label
3657 * @name: Full xattr name to check.
3659 static int smack_ismaclabel(const char *name)
3661 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3666 * smack_secid_to_secctx - return the smack label for a secid
3667 * @secid: incoming integer
3668 * @secdata: destination
3669 * @seclen: how long it is
3671 * Exists for networking code.
3673 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3675 struct smack_known *skp = smack_from_secid(secid);
3678 *secdata = skp->smk_known;
3679 *seclen = strlen(skp->smk_known);
3684 * smack_secctx_to_secid - return the secid for a smack label
3685 * @secdata: smack label
3686 * @seclen: how long result is
3687 * @secid: outgoing integer
3689 * Exists for audit and networking code.
3691 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3693 *secid = smack_to_secid(secdata);
3698 * smack_release_secctx - don't do anything.
3702 * Exists to make sure nothing gets done, and properly
3704 static void smack_release_secctx(char *secdata, u32 seclen)
3708 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3710 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3713 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3715 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3718 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3721 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3729 struct security_operations smack_ops = {
3732 .ptrace_access_check = smack_ptrace_access_check,
3733 .ptrace_traceme = smack_ptrace_traceme,
3734 .syslog = smack_syslog,
3736 .sb_alloc_security = smack_sb_alloc_security,
3737 .sb_free_security = smack_sb_free_security,
3738 .sb_copy_data = smack_sb_copy_data,
3739 .sb_kern_mount = smack_sb_kern_mount,
3740 .sb_statfs = smack_sb_statfs,
3741 .sb_mount = smack_sb_mount,
3742 .sb_umount = smack_sb_umount,
3744 .bprm_set_creds = smack_bprm_set_creds,
3745 .bprm_committing_creds = smack_bprm_committing_creds,
3746 .bprm_secureexec = smack_bprm_secureexec,
3748 .inode_alloc_security = smack_inode_alloc_security,
3749 .inode_free_security = smack_inode_free_security,
3750 .inode_init_security = smack_inode_init_security,
3751 .inode_link = smack_inode_link,
3752 .inode_unlink = smack_inode_unlink,
3753 .inode_rmdir = smack_inode_rmdir,
3754 .inode_rename = smack_inode_rename,
3755 .inode_permission = smack_inode_permission,
3756 .inode_setattr = smack_inode_setattr,
3757 .inode_getattr = smack_inode_getattr,
3758 .inode_setxattr = smack_inode_setxattr,
3759 .inode_post_setxattr = smack_inode_post_setxattr,
3760 .inode_getxattr = smack_inode_getxattr,
3761 .inode_removexattr = smack_inode_removexattr,
3762 .inode_getsecurity = smack_inode_getsecurity,
3763 .inode_setsecurity = smack_inode_setsecurity,
3764 .inode_listsecurity = smack_inode_listsecurity,
3765 .inode_getsecid = smack_inode_getsecid,
3767 .file_permission = smack_file_permission,
3768 .file_alloc_security = smack_file_alloc_security,
3769 .file_free_security = smack_file_free_security,
3770 .file_ioctl = smack_file_ioctl,
3771 .file_lock = smack_file_lock,
3772 .file_fcntl = smack_file_fcntl,
3773 .mmap_file = smack_mmap_file,
3774 .mmap_addr = cap_mmap_addr,
3775 .file_set_fowner = smack_file_set_fowner,
3776 .file_send_sigiotask = smack_file_send_sigiotask,
3777 .file_receive = smack_file_receive,
3779 .file_open = smack_file_open,
3781 .cred_alloc_blank = smack_cred_alloc_blank,
3782 .cred_free = smack_cred_free,
3783 .cred_prepare = smack_cred_prepare,
3784 .cred_transfer = smack_cred_transfer,
3785 .kernel_act_as = smack_kernel_act_as,
3786 .kernel_create_files_as = smack_kernel_create_files_as,
3787 .task_setpgid = smack_task_setpgid,
3788 .task_getpgid = smack_task_getpgid,
3789 .task_getsid = smack_task_getsid,
3790 .task_getsecid = smack_task_getsecid,
3791 .task_setnice = smack_task_setnice,
3792 .task_setioprio = smack_task_setioprio,
3793 .task_getioprio = smack_task_getioprio,
3794 .task_setscheduler = smack_task_setscheduler,
3795 .task_getscheduler = smack_task_getscheduler,
3796 .task_movememory = smack_task_movememory,
3797 .task_kill = smack_task_kill,
3798 .task_wait = smack_task_wait,
3799 .task_to_inode = smack_task_to_inode,
3801 .ipc_permission = smack_ipc_permission,
3802 .ipc_getsecid = smack_ipc_getsecid,
3804 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3805 .msg_msg_free_security = smack_msg_msg_free_security,
3807 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3808 .msg_queue_free_security = smack_msg_queue_free_security,
3809 .msg_queue_associate = smack_msg_queue_associate,
3810 .msg_queue_msgctl = smack_msg_queue_msgctl,
3811 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3812 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3814 .shm_alloc_security = smack_shm_alloc_security,
3815 .shm_free_security = smack_shm_free_security,
3816 .shm_associate = smack_shm_associate,
3817 .shm_shmctl = smack_shm_shmctl,
3818 .shm_shmat = smack_shm_shmat,
3820 .sem_alloc_security = smack_sem_alloc_security,
3821 .sem_free_security = smack_sem_free_security,
3822 .sem_associate = smack_sem_associate,
3823 .sem_semctl = smack_sem_semctl,
3824 .sem_semop = smack_sem_semop,
3826 .d_instantiate = smack_d_instantiate,
3828 .getprocattr = smack_getprocattr,
3829 .setprocattr = smack_setprocattr,
3831 .unix_stream_connect = smack_unix_stream_connect,
3832 .unix_may_send = smack_unix_may_send,
3834 .socket_post_create = smack_socket_post_create,
3835 .socket_bind = smack_socket_bind,
3836 .socket_connect = smack_socket_connect,
3837 .socket_sendmsg = smack_socket_sendmsg,
3838 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3839 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3840 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3841 .sk_alloc_security = smack_sk_alloc_security,
3842 .sk_free_security = smack_sk_free_security,
3843 .sock_graft = smack_sock_graft,
3844 .inet_conn_request = smack_inet_conn_request,
3845 .inet_csk_clone = smack_inet_csk_clone,
3847 /* key management security hooks */
3849 .key_alloc = smack_key_alloc,
3850 .key_free = smack_key_free,
3851 .key_permission = smack_key_permission,
3852 #endif /* CONFIG_KEYS */
3856 .audit_rule_init = smack_audit_rule_init,
3857 .audit_rule_known = smack_audit_rule_known,
3858 .audit_rule_match = smack_audit_rule_match,
3859 .audit_rule_free = smack_audit_rule_free,
3860 #endif /* CONFIG_AUDIT */
3862 .ismaclabel = smack_ismaclabel,
3863 .secid_to_secctx = smack_secid_to_secctx,
3864 .secctx_to_secid = smack_secctx_to_secid,
3865 .release_secctx = smack_release_secctx,
3866 .inode_notifysecctx = smack_inode_notifysecctx,
3867 .inode_setsecctx = smack_inode_setsecctx,
3868 .inode_getsecctx = smack_inode_getsecctx,
3872 static __init void init_smack_known_list(void)
3875 * Initialize rule list locks
3877 mutex_init(&smack_known_huh.smk_rules_lock);
3878 mutex_init(&smack_known_hat.smk_rules_lock);
3879 mutex_init(&smack_known_floor.smk_rules_lock);
3880 mutex_init(&smack_known_star.smk_rules_lock);
3881 mutex_init(&smack_known_invalid.smk_rules_lock);
3882 mutex_init(&smack_known_web.smk_rules_lock);
3884 * Initialize rule lists
3886 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3887 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3888 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3889 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3890 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3891 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3893 * Create the known labels list
3895 smk_insert_entry(&smack_known_huh);
3896 smk_insert_entry(&smack_known_hat);
3897 smk_insert_entry(&smack_known_star);
3898 smk_insert_entry(&smack_known_floor);
3899 smk_insert_entry(&smack_known_invalid);
3900 smk_insert_entry(&smack_known_web);
3904 * smack_init - initialize the smack system
3908 static __init int smack_init(void)
3911 struct task_smack *tsp;
3913 if (!security_module_enable(&smack_ops))
3916 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3921 printk(KERN_INFO "Smack: Initializing.\n");
3924 * Set the security state for the initial task.
3926 cred = (struct cred *) current->cred;
3927 cred->security = tsp;
3929 /* initialize the smack_known_list */
3930 init_smack_known_list();
3935 if (register_security(&smack_ops))
3936 panic("smack: Unable to register with kernel.\n");
3942 * Smack requires early initialization in order to label
3943 * all processes and objects when they are created.
3945 security_initcall(smack_init);