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, mode, &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;
840 struct smack_known *skp;
842 int check_import = 0;
847 * Check label validity here so import won't fail in post_setxattr
849 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
850 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
851 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
854 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
855 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
859 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
861 if (size != TRANS_TRUE_SIZE ||
862 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
865 rc = cap_inode_setxattr(dentry, name, value, size, flags);
867 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
870 if (rc == 0 && check_import) {
871 skp = smk_import_entry(value, size);
872 if (skp == NULL || (check_star &&
873 (skp == &smack_known_star || skp == &smack_known_web)))
877 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
878 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
881 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
887 * smack_inode_post_setxattr - Apply the Smack update approved above
889 * @name: attribute name
890 * @value: attribute value
891 * @size: attribute size
894 * Set the pointer in the inode blob to the entry found
895 * in the master label list.
897 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
898 const void *value, size_t size, int flags)
900 struct smack_known *skp;
901 struct inode_smack *isp = dentry->d_inode->i_security;
903 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
904 isp->smk_flags |= SMK_INODE_TRANSMUTE;
908 skp = smk_import_entry(value, size);
909 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
911 isp->smk_inode = skp->smk_known;
913 isp->smk_inode = smack_known_invalid.smk_known;
914 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
918 isp->smk_task = &smack_known_invalid;
919 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
923 isp->smk_mmap = &smack_known_invalid;
930 * smack_inode_getxattr - Smack check on getxattr
931 * @dentry: the object
934 * Returns 0 if access is permitted, an error code otherwise
936 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
938 struct smk_audit_info ad;
940 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
941 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
943 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
947 * smack_inode_removexattr - Smack check on removexattr
948 * @dentry: the object
949 * @name: name of the attribute
951 * Removing the Smack attribute requires CAP_MAC_ADMIN
953 * Returns 0 if access is permitted, an error code otherwise
955 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
957 struct inode_smack *isp;
958 struct smk_audit_info ad;
961 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
962 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
963 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
964 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
965 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
966 strcmp(name, XATTR_NAME_SMACKMMAP)) {
967 if (!smack_privileged(CAP_MAC_ADMIN))
970 rc = cap_inode_removexattr(dentry, name);
972 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
973 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
975 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
978 isp = dentry->d_inode->i_security;
979 isp->smk_task = NULL;
980 isp->smk_mmap = NULL;
987 * smack_inode_getsecurity - get smack xattrs
989 * @name: attribute name
990 * @buffer: where to put the result
993 * Returns the size of the attribute or an error code
995 static int smack_inode_getsecurity(const struct inode *inode,
996 const char *name, void **buffer,
999 struct socket_smack *ssp;
1000 struct socket *sock;
1001 struct super_block *sbp;
1002 struct inode *ip = (struct inode *)inode;
1007 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1008 isp = smk_of_inode(inode);
1009 ilen = strlen(isp) + 1;
1015 * The rest of the Smack xattrs are only on sockets.
1018 if (sbp->s_magic != SOCKFS_MAGIC)
1021 sock = SOCKET_I(ip);
1022 if (sock == NULL || sock->sk == NULL)
1025 ssp = sock->sk->sk_security;
1027 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1029 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1030 isp = ssp->smk_out->smk_known;
1034 ilen = strlen(isp) + 1;
1045 * smack_inode_listsecurity - list the Smack attributes
1046 * @inode: the object
1047 * @buffer: where they go
1048 * @buffer_size: size of buffer
1050 * Returns 0 on success, -EINVAL otherwise
1052 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1055 int len = strlen(XATTR_NAME_SMACK);
1057 if (buffer != NULL && len <= buffer_size) {
1058 memcpy(buffer, XATTR_NAME_SMACK, len);
1065 * smack_inode_getsecid - Extract inode's security id
1066 * @inode: inode to extract the info from
1067 * @secid: where result will be saved
1069 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1071 struct inode_smack *isp = inode->i_security;
1073 *secid = smack_to_secid(isp->smk_inode);
1081 * smack_file_permission - Smack check on file operations
1087 * Should access checks be done on each read or write?
1088 * UNICOS and SELinux say yes.
1089 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1091 * I'll say no for now. Smack does not do the frequent
1092 * label changing that SELinux does.
1094 static int smack_file_permission(struct file *file, int mask)
1100 * smack_file_alloc_security - assign a file security blob
1103 * The security blob for a file is a pointer to the master
1104 * label list, so no allocation is done.
1108 static int smack_file_alloc_security(struct file *file)
1110 struct smack_known *skp = smk_of_current();
1112 file->f_security = skp->smk_known;
1117 * smack_file_free_security - clear a file security blob
1120 * The security blob for a file is a pointer to the master
1121 * label list, so no memory is freed.
1123 static void smack_file_free_security(struct file *file)
1125 file->f_security = NULL;
1129 * smack_file_ioctl - Smack check on ioctls
1134 * Relies heavily on the correct use of the ioctl command conventions.
1136 * Returns 0 if allowed, error code otherwise
1138 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1142 struct smk_audit_info ad;
1144 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1145 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1147 if (_IOC_DIR(cmd) & _IOC_WRITE)
1148 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1150 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1151 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1157 * smack_file_lock - Smack check on file locking
1161 * Returns 0 if current has lock access, error code otherwise
1163 static int smack_file_lock(struct file *file, unsigned int cmd)
1165 struct smk_audit_info ad;
1167 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1168 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1169 return smk_curacc(file->f_security, MAY_LOCK, &ad);
1173 * smack_file_fcntl - Smack check on fcntl
1175 * @cmd: what action to check
1178 * Generally these operations are harmless.
1179 * File locking operations present an obvious mechanism
1180 * for passing information, so they require write access.
1182 * Returns 0 if current has access, error code otherwise
1184 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1187 struct smk_audit_info ad;
1196 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1197 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1198 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1202 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1203 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1204 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1215 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1216 * if mapping anonymous memory.
1217 * @file contains the file structure for file to map (may be NULL).
1218 * @reqprot contains the protection requested by the application.
1219 * @prot contains the protection that will be applied by the kernel.
1220 * @flags contains the operational flags.
1221 * Return 0 if permission is granted.
1223 static int smack_mmap_file(struct file *file,
1224 unsigned long reqprot, unsigned long prot,
1225 unsigned long flags)
1227 struct smack_known *skp;
1228 struct smack_known *mkp;
1229 struct smack_rule *srp;
1230 struct task_smack *tsp;
1232 struct inode_smack *isp;
1241 isp = file_inode(file)->i_security;
1242 if (isp->smk_mmap == NULL)
1244 mkp = isp->smk_mmap;
1246 tsp = current_security();
1247 skp = smk_of_current();
1252 * For each Smack rule associated with the subject
1253 * label verify that the SMACK64MMAP also has access
1254 * to that rule's object label.
1256 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1257 osmack = srp->smk_object;
1259 * Matching labels always allows access.
1261 if (mkp->smk_known == osmack)
1264 * If there is a matching local rule take
1265 * that into account as well.
1267 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1270 may = srp->smk_access;
1272 may &= srp->smk_access;
1274 * If may is zero the SMACK64MMAP subject can't
1275 * possibly have less access.
1281 * Fetch the global list entry.
1282 * If there isn't one a SMACK64MMAP subject
1283 * can't have as much access as current.
1285 mmay = smk_access_entry(mkp->smk_known, osmack,
1287 if (mmay == -ENOENT) {
1292 * If there is a local entry it modifies the
1293 * potential access, too.
1295 tmay = smk_access_entry(mkp->smk_known, osmack,
1297 if (tmay != -ENOENT)
1301 * If there is any access available to current that is
1302 * not available to a SMACK64MMAP subject
1305 if ((may | mmay) != mmay) {
1317 * smack_file_set_fowner - set the file security blob value
1318 * @file: object in question
1321 * Further research may be required on this one.
1323 static int smack_file_set_fowner(struct file *file)
1325 struct smack_known *skp = smk_of_current();
1327 file->f_security = skp->smk_known;
1332 * smack_file_send_sigiotask - Smack on sigio
1333 * @tsk: The target task
1334 * @fown: the object the signal come from
1337 * Allow a privileged task to get signals even if it shouldn't
1339 * Returns 0 if a subject with the object's smack could
1340 * write to the task, an error code otherwise.
1342 static int smack_file_send_sigiotask(struct task_struct *tsk,
1343 struct fown_struct *fown, int signum)
1345 struct smack_known *skp;
1346 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1349 struct smk_audit_info ad;
1352 * struct fown_struct is never outside the context of a struct file
1354 file = container_of(fown, struct file, f_owner);
1356 /* we don't log here as rc can be overriden */
1357 skp = smk_find_entry(file->f_security);
1358 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1359 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1362 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1363 smk_ad_setfield_u_tsk(&ad, tsk);
1364 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1369 * smack_file_receive - Smack file receive check
1372 * Returns 0 if current has access, error code otherwise
1374 static int smack_file_receive(struct file *file)
1377 struct smk_audit_info ad;
1379 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1380 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1382 * This code relies on bitmasks.
1384 if (file->f_mode & FMODE_READ)
1386 if (file->f_mode & FMODE_WRITE)
1389 return smk_curacc(file->f_security, may, &ad);
1393 * smack_file_open - Smack dentry open processing
1397 * Set the security blob in the file structure.
1401 static int smack_file_open(struct file *file, const struct cred *cred)
1403 struct inode_smack *isp = file_inode(file)->i_security;
1405 file->f_security = isp->smk_inode;
1415 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1416 * @new: the new credentials
1417 * @gfp: the atomicity of any memory allocations
1419 * Prepare a blank set of credentials for modification. This must allocate all
1420 * the memory the LSM module might require such that cred_transfer() can
1421 * complete without error.
1423 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1425 struct task_smack *tsp;
1427 tsp = new_task_smack(NULL, NULL, gfp);
1431 cred->security = tsp;
1438 * smack_cred_free - "free" task-level security credentials
1439 * @cred: the credentials in question
1442 static void smack_cred_free(struct cred *cred)
1444 struct task_smack *tsp = cred->security;
1445 struct smack_rule *rp;
1446 struct list_head *l;
1447 struct list_head *n;
1451 cred->security = NULL;
1453 list_for_each_safe(l, n, &tsp->smk_rules) {
1454 rp = list_entry(l, struct smack_rule, list);
1455 list_del(&rp->list);
1462 * smack_cred_prepare - prepare new set of credentials for modification
1463 * @new: the new credentials
1464 * @old: the original credentials
1465 * @gfp: the atomicity of any memory allocations
1467 * Prepare a new set of credentials for modification.
1469 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1472 struct task_smack *old_tsp = old->security;
1473 struct task_smack *new_tsp;
1476 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1477 if (new_tsp == NULL)
1480 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1484 new->security = new_tsp;
1489 * smack_cred_transfer - Transfer the old credentials to the new credentials
1490 * @new: the new credentials
1491 * @old: the original credentials
1493 * Fill in a set of blank credentials from another set of credentials.
1495 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1497 struct task_smack *old_tsp = old->security;
1498 struct task_smack *new_tsp = new->security;
1500 new_tsp->smk_task = old_tsp->smk_task;
1501 new_tsp->smk_forked = old_tsp->smk_task;
1502 mutex_init(&new_tsp->smk_rules_lock);
1503 INIT_LIST_HEAD(&new_tsp->smk_rules);
1506 /* cbs copy rule list */
1510 * smack_kernel_act_as - Set the subjective context in a set of credentials
1511 * @new: points to the set of credentials to be modified.
1512 * @secid: specifies the security ID to be set
1514 * Set the security data for a kernel service.
1516 static int smack_kernel_act_as(struct cred *new, u32 secid)
1518 struct task_smack *new_tsp = new->security;
1519 struct smack_known *skp = smack_from_secid(secid);
1524 new_tsp->smk_task = skp;
1529 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1530 * @new: points to the set of credentials to be modified
1531 * @inode: points to the inode to use as a reference
1533 * Set the file creation context in a set of credentials to the same
1534 * as the objective context of the specified inode
1536 static int smack_kernel_create_files_as(struct cred *new,
1537 struct inode *inode)
1539 struct inode_smack *isp = inode->i_security;
1540 struct task_smack *tsp = new->security;
1542 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1543 tsp->smk_task = tsp->smk_forked;
1548 * smk_curacc_on_task - helper to log task related access
1549 * @p: the task object
1550 * @access: the access requested
1551 * @caller: name of the calling function for audit
1553 * Return 0 if access is permitted
1555 static int smk_curacc_on_task(struct task_struct *p, int access,
1558 struct smk_audit_info ad;
1559 struct smack_known *skp = smk_of_task(task_security(p));
1561 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1562 smk_ad_setfield_u_tsk(&ad, p);
1563 return smk_curacc(skp->smk_known, access, &ad);
1567 * smack_task_setpgid - Smack check on setting pgid
1568 * @p: the task object
1571 * Return 0 if write access is permitted
1573 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1575 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1579 * smack_task_getpgid - Smack access check for getpgid
1580 * @p: the object task
1582 * Returns 0 if current can read the object task, error code otherwise
1584 static int smack_task_getpgid(struct task_struct *p)
1586 return smk_curacc_on_task(p, MAY_READ, __func__);
1590 * smack_task_getsid - Smack access check for getsid
1591 * @p: the object task
1593 * Returns 0 if current can read the object task, error code otherwise
1595 static int smack_task_getsid(struct task_struct *p)
1597 return smk_curacc_on_task(p, MAY_READ, __func__);
1601 * smack_task_getsecid - get the secid of the task
1602 * @p: the object task
1603 * @secid: where to put the result
1605 * Sets the secid to contain a u32 version of the smack label.
1607 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1609 struct smack_known *skp = smk_of_task(task_security(p));
1611 *secid = skp->smk_secid;
1615 * smack_task_setnice - Smack check on setting nice
1616 * @p: the task object
1619 * Return 0 if write access is permitted
1621 static int smack_task_setnice(struct task_struct *p, int nice)
1625 rc = cap_task_setnice(p, nice);
1627 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1632 * smack_task_setioprio - Smack check on setting ioprio
1633 * @p: the task object
1636 * Return 0 if write access is permitted
1638 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1642 rc = cap_task_setioprio(p, ioprio);
1644 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1649 * smack_task_getioprio - Smack check on reading ioprio
1650 * @p: the task object
1652 * Return 0 if read access is permitted
1654 static int smack_task_getioprio(struct task_struct *p)
1656 return smk_curacc_on_task(p, MAY_READ, __func__);
1660 * smack_task_setscheduler - Smack check on setting scheduler
1661 * @p: the task object
1665 * Return 0 if read access is permitted
1667 static int smack_task_setscheduler(struct task_struct *p)
1671 rc = cap_task_setscheduler(p);
1673 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1678 * smack_task_getscheduler - Smack check on reading scheduler
1679 * @p: the task object
1681 * Return 0 if read access is permitted
1683 static int smack_task_getscheduler(struct task_struct *p)
1685 return smk_curacc_on_task(p, MAY_READ, __func__);
1689 * smack_task_movememory - Smack check on moving memory
1690 * @p: the task object
1692 * Return 0 if write access is permitted
1694 static int smack_task_movememory(struct task_struct *p)
1696 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1700 * smack_task_kill - Smack check on signal delivery
1701 * @p: the task object
1704 * @secid: identifies the smack to use in lieu of current's
1706 * Return 0 if write access is permitted
1708 * The secid behavior is an artifact of an SELinux hack
1709 * in the USB code. Someday it may go away.
1711 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1714 struct smk_audit_info ad;
1715 struct smack_known *skp;
1716 struct smack_known *tkp = smk_of_task(task_security(p));
1718 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1719 smk_ad_setfield_u_tsk(&ad, p);
1721 * Sending a signal requires that the sender
1722 * can write the receiver.
1725 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
1727 * If the secid isn't 0 we're dealing with some USB IO
1728 * specific behavior. This is not clean. For one thing
1729 * we can't take privilege into account.
1731 skp = smack_from_secid(secid);
1732 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
1736 * smack_task_wait - Smack access check for waiting
1737 * @p: task to wait for
1741 static int smack_task_wait(struct task_struct *p)
1744 * Allow the operation to succeed.
1746 * In userless environments (e.g. phones) programs
1747 * get marked with SMACK64EXEC and even if the parent
1748 * and child shouldn't be talking the parent still
1749 * may expect to know when the child exits.
1755 * smack_task_to_inode - copy task smack into the inode blob
1756 * @p: task to copy from
1757 * @inode: inode to copy to
1759 * Sets the smack pointer in the inode security blob
1761 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1763 struct inode_smack *isp = inode->i_security;
1764 struct smack_known *skp = smk_of_task(task_security(p));
1766 isp->smk_inode = skp->smk_known;
1774 * smack_sk_alloc_security - Allocate a socket blob
1777 * @gfp_flags: memory allocation flags
1779 * Assign Smack pointers to current
1781 * Returns 0 on success, -ENOMEM is there's no memory
1783 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1785 struct smack_known *skp = smk_of_current();
1786 struct socket_smack *ssp;
1788 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1792 ssp->smk_in = skp->smk_known;
1794 ssp->smk_packet = NULL;
1796 sk->sk_security = ssp;
1802 * smack_sk_free_security - Free a socket blob
1805 * Clears the blob pointer
1807 static void smack_sk_free_security(struct sock *sk)
1809 kfree(sk->sk_security);
1813 * smack_host_label - check host based restrictions
1814 * @sip: the object end
1816 * looks for host based access restrictions
1818 * This version will only be appropriate for really small sets of single label
1819 * hosts. The caller is responsible for ensuring that the RCU read lock is
1820 * taken before calling this function.
1822 * Returns the label of the far end or NULL if it's not special.
1824 static char *smack_host_label(struct sockaddr_in *sip)
1826 struct smk_netlbladdr *snp;
1827 struct in_addr *siap = &sip->sin_addr;
1829 if (siap->s_addr == 0)
1832 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1834 * we break after finding the first match because
1835 * the list is sorted from longest to shortest mask
1836 * so we have found the most specific match
1838 if ((&snp->smk_host.sin_addr)->s_addr ==
1839 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1840 /* we have found the special CIPSO option */
1841 if (snp->smk_label == smack_cipso_option)
1843 return snp->smk_label;
1850 * smack_netlabel - Set the secattr on a socket
1852 * @labeled: socket label scheme
1854 * Convert the outbound smack value (smk_out) to a
1855 * secattr and attach it to the socket.
1857 * Returns 0 on success or an error code
1859 static int smack_netlabel(struct sock *sk, int labeled)
1861 struct smack_known *skp;
1862 struct socket_smack *ssp = sk->sk_security;
1866 * Usually the netlabel code will handle changing the
1867 * packet labeling based on the label.
1868 * The case of a single label host is different, because
1869 * a single label host should never get a labeled packet
1870 * even though the label is usually associated with a packet
1874 bh_lock_sock_nested(sk);
1876 if (ssp->smk_out == smack_net_ambient ||
1877 labeled == SMACK_UNLABELED_SOCKET)
1878 netlbl_sock_delattr(sk);
1881 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
1891 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1893 * @sap: the destination address
1895 * Set the correct secattr for the given socket based on the destination
1896 * address and perform any outbound access checks needed.
1898 * Returns 0 on success or an error code.
1901 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1903 struct smack_known *skp;
1907 struct socket_smack *ssp = sk->sk_security;
1908 struct smk_audit_info ad;
1911 hostsp = smack_host_label(sap);
1912 if (hostsp != NULL) {
1914 struct lsm_network_audit net;
1916 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1917 ad.a.u.net->family = sap->sin_family;
1918 ad.a.u.net->dport = sap->sin_port;
1919 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
1921 sk_lbl = SMACK_UNLABELED_SOCKET;
1923 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
1925 sk_lbl = SMACK_CIPSO_SOCKET;
1932 return smack_netlabel(sk, sk_lbl);
1936 * smk_ipv6_port_label - Smack port access table management
1940 * Create or update the port list entry
1942 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1944 struct sock *sk = sock->sk;
1945 struct sockaddr_in6 *addr6;
1946 struct socket_smack *ssp = sock->sk->sk_security;
1947 struct smk_port_label *spp;
1948 unsigned short port = 0;
1950 if (address == NULL) {
1952 * This operation is changing the Smack information
1953 * on the bound socket. Take the changes to the port
1956 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1957 if (sk != spp->smk_sock)
1959 spp->smk_in = ssp->smk_in;
1960 spp->smk_out = ssp->smk_out;
1964 * A NULL address is only used for updating existing
1965 * bound entries. If there isn't one, it's OK.
1970 addr6 = (struct sockaddr_in6 *)address;
1971 port = ntohs(addr6->sin6_port);
1973 * This is a special case that is safely ignored.
1979 * Look for an existing port list entry.
1980 * This is an indication that a port is getting reused.
1982 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1983 if (spp->smk_port != port)
1985 spp->smk_port = port;
1987 spp->smk_in = ssp->smk_in;
1988 spp->smk_out = ssp->smk_out;
1993 * A new port entry is required.
1995 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1999 spp->smk_port = port;
2001 spp->smk_in = ssp->smk_in;
2002 spp->smk_out = ssp->smk_out;
2004 list_add(&spp->list, &smk_ipv6_port_list);
2009 * smk_ipv6_port_check - check Smack port access
2013 * Create or update the port list entry
2015 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2020 struct smk_port_label *spp;
2021 struct socket_smack *ssp = sk->sk_security;
2022 struct smack_known *skp;
2023 unsigned short port = 0;
2025 struct smk_audit_info ad;
2027 struct lsm_network_audit net;
2030 if (act == SMK_RECEIVING) {
2031 skp = smack_net_ambient;
2032 object = ssp->smk_in;
2035 object = smack_net_ambient->smk_known;
2039 * Get the IP address and port from the address.
2041 port = ntohs(address->sin6_port);
2042 bep = (__be16 *)(&address->sin6_addr);
2043 be32p = (__be32 *)(&address->sin6_addr);
2046 * It's remote, so port lookup does no good.
2048 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2052 * It's local so the send check has to have passed.
2054 if (act == SMK_RECEIVING) {
2055 skp = &smack_known_web;
2059 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2060 if (spp->smk_port != port)
2062 object = spp->smk_in;
2063 if (act == SMK_CONNECTING)
2064 ssp->smk_packet = spp->smk_out->smk_known;
2071 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2072 ad.a.u.net->family = sk->sk_family;
2073 ad.a.u.net->dport = port;
2074 if (act == SMK_RECEIVING)
2075 ad.a.u.net->v6info.saddr = address->sin6_addr;
2077 ad.a.u.net->v6info.daddr = address->sin6_addr;
2079 return smk_access(skp, object, MAY_WRITE, &ad);
2083 * smack_inode_setsecurity - set smack xattrs
2084 * @inode: the object
2085 * @name: attribute name
2086 * @value: attribute value
2087 * @size: size of the attribute
2090 * Sets the named attribute in the appropriate blob
2092 * Returns 0 on success, or an error code
2094 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2095 const void *value, size_t size, int flags)
2097 struct smack_known *skp;
2098 struct inode_smack *nsp = inode->i_security;
2099 struct socket_smack *ssp;
2100 struct socket *sock;
2103 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2106 skp = smk_import_entry(value, size);
2110 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2111 nsp->smk_inode = skp->smk_known;
2112 nsp->smk_flags |= SMK_INODE_INSTANT;
2116 * The rest of the Smack xattrs are only on sockets.
2118 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2121 sock = SOCKET_I(inode);
2122 if (sock == NULL || sock->sk == NULL)
2125 ssp = sock->sk->sk_security;
2127 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2128 ssp->smk_in = skp->smk_known;
2129 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2131 if (sock->sk->sk_family == PF_INET) {
2132 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2135 "Smack: \"%s\" netlbl error %d.\n",
2141 if (sock->sk->sk_family == PF_INET6)
2142 smk_ipv6_port_label(sock, NULL);
2148 * smack_socket_post_create - finish socket setup
2150 * @family: protocol family
2155 * Sets the netlabel information on the socket
2157 * Returns 0 on success, and error code otherwise
2159 static int smack_socket_post_create(struct socket *sock, int family,
2160 int type, int protocol, int kern)
2162 if (family != PF_INET || sock->sk == NULL)
2165 * Set the outbound netlbl.
2167 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2171 * smack_socket_bind - record port binding information.
2173 * @address: the port address
2174 * @addrlen: size of the address
2176 * Records the label bound to a port.
2180 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2183 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2184 smk_ipv6_port_label(sock, address);
2190 * smack_socket_connect - connect access check
2192 * @sap: the other end
2193 * @addrlen: size of sap
2195 * Verifies that a connection may be possible
2197 * Returns 0 on success, and error code otherwise
2199 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2204 if (sock->sk == NULL)
2207 switch (sock->sk->sk_family) {
2209 if (addrlen < sizeof(struct sockaddr_in))
2211 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2214 if (addrlen < sizeof(struct sockaddr_in6))
2216 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2224 * smack_flags_to_may - convert S_ to MAY_ values
2225 * @flags: the S_ value
2227 * Returns the equivalent MAY_ value
2229 static int smack_flags_to_may(int flags)
2233 if (flags & S_IRUGO)
2235 if (flags & S_IWUGO)
2237 if (flags & S_IXUGO)
2244 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2249 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2251 struct smack_known *skp = smk_of_current();
2253 msg->security = skp->smk_known;
2258 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2261 * Clears the blob pointer
2263 static void smack_msg_msg_free_security(struct msg_msg *msg)
2265 msg->security = NULL;
2269 * smack_of_shm - the smack pointer for the shm
2272 * Returns a pointer to the smack value
2274 static char *smack_of_shm(struct shmid_kernel *shp)
2276 return (char *)shp->shm_perm.security;
2280 * smack_shm_alloc_security - Set the security blob for shm
2285 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2287 struct kern_ipc_perm *isp = &shp->shm_perm;
2288 struct smack_known *skp = smk_of_current();
2290 isp->security = skp->smk_known;
2295 * smack_shm_free_security - Clear the security blob for shm
2298 * Clears the blob pointer
2300 static void smack_shm_free_security(struct shmid_kernel *shp)
2302 struct kern_ipc_perm *isp = &shp->shm_perm;
2304 isp->security = NULL;
2308 * smk_curacc_shm : check if current has access on shm
2310 * @access : access requested
2312 * Returns 0 if current has the requested access, error code otherwise
2314 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2316 char *ssp = smack_of_shm(shp);
2317 struct smk_audit_info ad;
2320 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2321 ad.a.u.ipc_id = shp->shm_perm.id;
2323 return smk_curacc(ssp, access, &ad);
2327 * smack_shm_associate - Smack access check for shm
2329 * @shmflg: access requested
2331 * Returns 0 if current has the requested access, error code otherwise
2333 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2337 may = smack_flags_to_may(shmflg);
2338 return smk_curacc_shm(shp, may);
2342 * smack_shm_shmctl - Smack access check for shm
2344 * @cmd: what it wants to do
2346 * Returns 0 if current has the requested access, error code otherwise
2348 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2361 may = MAY_READWRITE;
2366 * System level information.
2372 return smk_curacc_shm(shp, may);
2376 * smack_shm_shmat - Smack access for shmat
2379 * @shmflg: access requested
2381 * Returns 0 if current has the requested access, error code otherwise
2383 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2388 may = smack_flags_to_may(shmflg);
2389 return smk_curacc_shm(shp, may);
2393 * smack_of_sem - the smack pointer for the sem
2396 * Returns a pointer to the smack value
2398 static char *smack_of_sem(struct sem_array *sma)
2400 return (char *)sma->sem_perm.security;
2404 * smack_sem_alloc_security - Set the security blob for sem
2409 static int smack_sem_alloc_security(struct sem_array *sma)
2411 struct kern_ipc_perm *isp = &sma->sem_perm;
2412 struct smack_known *skp = smk_of_current();
2414 isp->security = skp->smk_known;
2419 * smack_sem_free_security - Clear the security blob for sem
2422 * Clears the blob pointer
2424 static void smack_sem_free_security(struct sem_array *sma)
2426 struct kern_ipc_perm *isp = &sma->sem_perm;
2428 isp->security = NULL;
2432 * smk_curacc_sem : check if current has access on sem
2434 * @access : access requested
2436 * Returns 0 if current has the requested access, error code otherwise
2438 static int smk_curacc_sem(struct sem_array *sma, int access)
2440 char *ssp = smack_of_sem(sma);
2441 struct smk_audit_info ad;
2444 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2445 ad.a.u.ipc_id = sma->sem_perm.id;
2447 return smk_curacc(ssp, access, &ad);
2451 * smack_sem_associate - Smack access check for sem
2453 * @semflg: access requested
2455 * Returns 0 if current has the requested access, error code otherwise
2457 static int smack_sem_associate(struct sem_array *sma, int semflg)
2461 may = smack_flags_to_may(semflg);
2462 return smk_curacc_sem(sma, may);
2466 * smack_sem_shmctl - Smack access check for sem
2468 * @cmd: what it wants to do
2470 * Returns 0 if current has the requested access, error code otherwise
2472 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2490 may = MAY_READWRITE;
2495 * System level information
2502 return smk_curacc_sem(sma, may);
2506 * smack_sem_semop - Smack checks of semaphore operations
2512 * Treated as read and write in all cases.
2514 * Returns 0 if access is allowed, error code otherwise
2516 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2517 unsigned nsops, int alter)
2519 return smk_curacc_sem(sma, MAY_READWRITE);
2523 * smack_msg_alloc_security - Set the security blob for msg
2528 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2530 struct kern_ipc_perm *kisp = &msq->q_perm;
2531 struct smack_known *skp = smk_of_current();
2533 kisp->security = skp->smk_known;
2538 * smack_msg_free_security - Clear the security blob for msg
2541 * Clears the blob pointer
2543 static void smack_msg_queue_free_security(struct msg_queue *msq)
2545 struct kern_ipc_perm *kisp = &msq->q_perm;
2547 kisp->security = NULL;
2551 * smack_of_msq - the smack pointer for the msq
2554 * Returns a pointer to the smack value
2556 static char *smack_of_msq(struct msg_queue *msq)
2558 return (char *)msq->q_perm.security;
2562 * smk_curacc_msq : helper to check if current has access on msq
2564 * @access : access requested
2566 * return 0 if current has access, error otherwise
2568 static int smk_curacc_msq(struct msg_queue *msq, int access)
2570 char *msp = smack_of_msq(msq);
2571 struct smk_audit_info ad;
2574 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2575 ad.a.u.ipc_id = msq->q_perm.id;
2577 return smk_curacc(msp, access, &ad);
2581 * smack_msg_queue_associate - Smack access check for msg_queue
2583 * @msqflg: access requested
2585 * Returns 0 if current has the requested access, error code otherwise
2587 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2591 may = smack_flags_to_may(msqflg);
2592 return smk_curacc_msq(msq, may);
2596 * smack_msg_queue_msgctl - Smack access check for msg_queue
2598 * @cmd: what it wants to do
2600 * Returns 0 if current has the requested access, error code otherwise
2602 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2613 may = MAY_READWRITE;
2618 * System level information
2625 return smk_curacc_msq(msq, may);
2629 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2632 * @msqflg: access requested
2634 * Returns 0 if current has the requested access, error code otherwise
2636 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2641 may = smack_flags_to_may(msqflg);
2642 return smk_curacc_msq(msq, may);
2646 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2653 * Returns 0 if current has read and write access, error code otherwise
2655 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2656 struct task_struct *target, long type, int mode)
2658 return smk_curacc_msq(msq, MAY_READWRITE);
2662 * smack_ipc_permission - Smack access for ipc_permission()
2663 * @ipp: the object permissions
2664 * @flag: access requested
2666 * Returns 0 if current has read and write access, error code otherwise
2668 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2670 char *isp = ipp->security;
2671 int may = smack_flags_to_may(flag);
2672 struct smk_audit_info ad;
2675 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2676 ad.a.u.ipc_id = ipp->id;
2678 return smk_curacc(isp, may, &ad);
2682 * smack_ipc_getsecid - Extract smack security id
2683 * @ipp: the object permissions
2684 * @secid: where result will be saved
2686 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2688 char *smack = ipp->security;
2690 *secid = smack_to_secid(smack);
2694 * smack_d_instantiate - Make sure the blob is correct on an inode
2695 * @opt_dentry: dentry where inode will be attached
2696 * @inode: the object
2698 * Set the inode's security blob if it hasn't been done already.
2700 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2702 struct super_block *sbp;
2703 struct superblock_smack *sbsp;
2704 struct inode_smack *isp;
2705 struct smack_known *skp;
2706 struct smack_known *ckp = smk_of_current();
2708 char trattr[TRANS_TRUE_SIZE];
2716 isp = inode->i_security;
2718 mutex_lock(&isp->smk_lock);
2720 * If the inode is already instantiated
2721 * take the quick way out
2723 if (isp->smk_flags & SMK_INODE_INSTANT)
2727 sbsp = sbp->s_security;
2729 * We're going to use the superblock default label
2730 * if there's no label on the file.
2732 final = sbsp->smk_default;
2735 * If this is the root inode the superblock
2736 * may be in the process of initialization.
2737 * If that is the case use the root value out
2738 * of the superblock.
2740 if (opt_dentry->d_parent == opt_dentry) {
2741 isp->smk_inode = sbsp->smk_root;
2742 isp->smk_flags |= SMK_INODE_INSTANT;
2747 * This is pretty hackish.
2748 * Casey says that we shouldn't have to do
2749 * file system specific code, but it does help
2750 * with keeping it simple.
2752 switch (sbp->s_magic) {
2755 * Casey says that it's a little embarrassing
2756 * that the smack file system doesn't do
2757 * extended attributes.
2759 final = smack_known_star.smk_known;
2763 * Casey says pipes are easy (?)
2765 final = smack_known_star.smk_known;
2767 case DEVPTS_SUPER_MAGIC:
2769 * devpts seems content with the label of the task.
2770 * Programs that change smack have to treat the
2773 final = ckp->smk_known;
2777 * Socket access is controlled by the socket
2778 * structures associated with the task involved.
2780 final = smack_known_star.smk_known;
2782 case PROC_SUPER_MAGIC:
2784 * Casey says procfs appears not to care.
2785 * The superblock default suffices.
2790 * Device labels should come from the filesystem,
2791 * but watch out, because they're volitile,
2792 * getting recreated on every reboot.
2794 final = smack_known_star.smk_known;
2798 * If a smack value has been set we want to use it,
2799 * but since tmpfs isn't giving us the opportunity
2800 * to set mount options simulate setting the
2801 * superblock default.
2805 * This isn't an understood special case.
2806 * Get the value from the xattr.
2810 * UNIX domain sockets use lower level socket data.
2812 if (S_ISSOCK(inode->i_mode)) {
2813 final = smack_known_star.smk_known;
2817 * No xattr support means, alas, no SMACK label.
2818 * Use the aforeapplied default.
2819 * It would be curious if the label of the task
2820 * does not match that assigned.
2822 if (inode->i_op->getxattr == NULL)
2825 * Get the dentry for xattr.
2827 dp = dget(opt_dentry);
2828 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2830 final = skp->smk_known;
2833 * Transmuting directory
2835 if (S_ISDIR(inode->i_mode)) {
2837 * If this is a new directory and the label was
2838 * transmuted when the inode was initialized
2839 * set the transmute attribute on the directory
2840 * and mark the inode.
2842 * If there is a transmute attribute on the
2843 * directory mark the inode.
2845 if (isp->smk_flags & SMK_INODE_CHANGED) {
2846 isp->smk_flags &= ~SMK_INODE_CHANGED;
2847 rc = inode->i_op->setxattr(dp,
2848 XATTR_NAME_SMACKTRANSMUTE,
2849 TRANS_TRUE, TRANS_TRUE_SIZE,
2852 rc = inode->i_op->getxattr(dp,
2853 XATTR_NAME_SMACKTRANSMUTE, trattr,
2855 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2856 TRANS_TRUE_SIZE) != 0)
2860 transflag = SMK_INODE_TRANSMUTE;
2863 * Don't let the exec or mmap label be "*" or "@".
2865 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2866 if (skp == &smack_known_star || skp == &smack_known_web)
2868 isp->smk_task = skp;
2869 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2870 if (skp == &smack_known_star || skp == &smack_known_web)
2872 isp->smk_mmap = skp;
2879 isp->smk_inode = ckp->smk_known;
2881 isp->smk_inode = final;
2883 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2886 mutex_unlock(&isp->smk_lock);
2891 * smack_getprocattr - Smack process attribute access
2892 * @p: the object task
2893 * @name: the name of the attribute in /proc/.../attr
2894 * @value: where to put the result
2896 * Places a copy of the task Smack into value
2898 * Returns the length of the smack label or an error code
2900 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2902 struct smack_known *skp = smk_of_task(task_security(p));
2906 if (strcmp(name, "current") != 0)
2909 cp = kstrdup(skp->smk_known, GFP_KERNEL);
2919 * smack_setprocattr - Smack process attribute setting
2920 * @p: the object task
2921 * @name: the name of the attribute in /proc/.../attr
2922 * @value: the value to set
2923 * @size: the size of the value
2925 * Sets the Smack value of the task. Only setting self
2926 * is permitted and only with privilege
2928 * Returns the length of the smack label or an error code
2930 static int smack_setprocattr(struct task_struct *p, char *name,
2931 void *value, size_t size)
2933 struct task_smack *tsp;
2935 struct smack_known *skp;
2938 * Changing another process' Smack value is too dangerous
2939 * and supports no sane use case.
2944 if (!smack_privileged(CAP_MAC_ADMIN))
2947 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
2950 if (strcmp(name, "current") != 0)
2953 skp = smk_import_entry(value, size);
2958 * No process is ever allowed the web ("@") label.
2960 if (skp == &smack_known_web)
2963 new = prepare_creds();
2967 tsp = new->security;
2968 tsp->smk_task = skp;
2975 * smack_unix_stream_connect - Smack access on UDS
2977 * @other: the other sock
2980 * Return 0 if a subject with the smack of sock could access
2981 * an object with the smack of other, otherwise an error code
2983 static int smack_unix_stream_connect(struct sock *sock,
2984 struct sock *other, struct sock *newsk)
2986 struct smack_known *skp;
2987 struct socket_smack *ssp = sock->sk_security;
2988 struct socket_smack *osp = other->sk_security;
2989 struct socket_smack *nsp = newsk->sk_security;
2990 struct smk_audit_info ad;
2994 struct lsm_network_audit net;
2996 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2997 smk_ad_setfield_u_net_sk(&ad, other);
3000 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3002 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3006 * Cross reference the peer labels for SO_PEERSEC.
3009 nsp->smk_packet = ssp->smk_out->smk_known;
3010 ssp->smk_packet = osp->smk_out->smk_known;
3017 * smack_unix_may_send - Smack access on UDS
3019 * @other: the other socket
3021 * Return 0 if a subject with the smack of sock could access
3022 * an object with the smack of other, otherwise an error code
3024 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3026 struct socket_smack *ssp = sock->sk->sk_security;
3027 struct socket_smack *osp = other->sk->sk_security;
3028 struct smack_known *skp;
3029 struct smk_audit_info ad;
3032 struct lsm_network_audit net;
3034 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3035 smk_ad_setfield_u_net_sk(&ad, other->sk);
3038 if (smack_privileged(CAP_MAC_OVERRIDE))
3042 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3046 * smack_socket_sendmsg - Smack check based on destination host
3049 * @size: the size of the message
3051 * Return 0 if the current subject can write to the destination host.
3052 * For IPv4 this is only a question if the destination is a single label host.
3053 * For IPv6 this is a check against the label of the port.
3055 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3058 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3059 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3063 * Perfectly reasonable for this to be NULL
3068 switch (sip->sin_family) {
3070 rc = smack_netlabel_send(sock->sk, sip);
3073 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3080 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3081 * @sap: netlabel secattr
3082 * @ssp: socket security information
3084 * Returns a pointer to a Smack label entry found on the label list.
3086 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3087 struct socket_smack *ssp)
3089 struct smack_known *skp;
3094 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3096 * Looks like a CIPSO packet.
3097 * If there are flags but no level netlabel isn't
3098 * behaving the way we expect it to.
3100 * Look it up in the label table
3101 * Without guidance regarding the smack value
3102 * for the packet fall back on the network
3106 list_for_each_entry(skp, &smack_known_list, list) {
3107 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3110 * Compare the catsets. Use the netlbl APIs.
3112 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3113 if ((skp->smk_netlabel.flags &
3114 NETLBL_SECATTR_MLS_CAT) == 0)
3118 for (acat = -1, kcat = -1; acat == kcat; ) {
3119 acat = netlbl_secattr_catmap_walk(
3120 sap->attr.mls.cat, acat + 1);
3121 kcat = netlbl_secattr_catmap_walk(
3122 skp->smk_netlabel.attr.mls.cat,
3124 if (acat < 0 || kcat < 0)
3137 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
3138 return &smack_known_web;
3139 return &smack_known_star;
3141 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3143 * Looks like a fallback, which gives us a secid.
3145 skp = smack_from_secid(sap->attr.secid);
3147 * This has got to be a bug because it is
3148 * impossible to specify a fallback without
3149 * specifying the label, which will ensure
3150 * it has a secid, and the only way to get a
3151 * secid is from a fallback.
3153 BUG_ON(skp == NULL);
3157 * Without guidance regarding the smack value
3158 * for the packet fall back on the network
3161 return smack_net_ambient;
3164 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3168 int proto = -EINVAL;
3169 struct ipv6hdr _ipv6h;
3170 struct ipv6hdr *ip6;
3172 struct tcphdr _tcph, *th;
3173 struct udphdr _udph, *uh;
3174 struct dccp_hdr _dccph, *dh;
3178 offset = skb_network_offset(skb);
3179 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3182 sip->sin6_addr = ip6->saddr;
3184 nexthdr = ip6->nexthdr;
3185 offset += sizeof(_ipv6h);
3186 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3193 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3195 sip->sin6_port = th->source;
3198 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3200 sip->sin6_port = uh->source;
3203 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3205 sip->sin6_port = dh->dccph_sport;
3212 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3216 * Returns 0 if the packet should be delivered, an error code otherwise
3218 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3220 struct netlbl_lsm_secattr secattr;
3221 struct socket_smack *ssp = sk->sk_security;
3222 struct smack_known *skp;
3223 struct sockaddr_in6 sadd;
3225 struct smk_audit_info ad;
3227 struct lsm_network_audit net;
3229 switch (sk->sk_family) {
3232 * Translate what netlabel gave us.
3234 netlbl_secattr_init(&secattr);
3236 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3238 skp = smack_from_secattr(&secattr, ssp);
3240 skp = smack_net_ambient;
3242 netlbl_secattr_destroy(&secattr);
3245 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3246 ad.a.u.net->family = sk->sk_family;
3247 ad.a.u.net->netif = skb->skb_iif;
3248 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3251 * Receiving a packet requires that the other end
3252 * be able to write here. Read access is not required.
3253 * This is the simplist possible security model
3256 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3258 netlbl_skbuff_err(skb, rc, 0);
3261 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3262 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3263 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3272 * smack_socket_getpeersec_stream - pull in packet label
3274 * @optval: user's destination
3275 * @optlen: size thereof
3278 * returns zero on success, an error code otherwise
3280 static int smack_socket_getpeersec_stream(struct socket *sock,
3281 char __user *optval,
3282 int __user *optlen, unsigned len)
3284 struct socket_smack *ssp;
3289 ssp = sock->sk->sk_security;
3290 if (ssp->smk_packet != NULL) {
3291 rcp = ssp->smk_packet;
3292 slen = strlen(rcp) + 1;
3297 else if (copy_to_user(optval, rcp, slen) != 0)
3300 if (put_user(slen, optlen) != 0)
3308 * smack_socket_getpeersec_dgram - pull in packet label
3309 * @sock: the peer socket
3311 * @secid: pointer to where to put the secid of the packet
3313 * Sets the netlabel socket state on sk from parent
3315 static int smack_socket_getpeersec_dgram(struct socket *sock,
3316 struct sk_buff *skb, u32 *secid)
3319 struct netlbl_lsm_secattr secattr;
3320 struct socket_smack *ssp = NULL;
3321 struct smack_known *skp;
3322 int family = PF_UNSPEC;
3323 u32 s = 0; /* 0 is the invalid secid */
3327 if (skb->protocol == htons(ETH_P_IP))
3329 else if (skb->protocol == htons(ETH_P_IPV6))
3332 if (family == PF_UNSPEC && sock != NULL)
3333 family = sock->sk->sk_family;
3335 if (family == PF_UNIX) {
3336 ssp = sock->sk->sk_security;
3337 s = ssp->smk_out->smk_secid;
3338 } else if (family == PF_INET || family == PF_INET6) {
3340 * Translate what netlabel gave us.
3342 if (sock != NULL && sock->sk != NULL)
3343 ssp = sock->sk->sk_security;
3344 netlbl_secattr_init(&secattr);
3345 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3347 skp = smack_from_secattr(&secattr, ssp);
3350 netlbl_secattr_destroy(&secattr);
3359 * smack_sock_graft - Initialize a newly created socket with an existing sock
3361 * @parent: parent socket
3363 * Set the smk_{in,out} state of an existing sock based on the process that
3364 * is creating the new socket.
3366 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3368 struct socket_smack *ssp;
3369 struct smack_known *skp = smk_of_current();
3372 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3375 ssp = sk->sk_security;
3376 ssp->smk_in = skp->smk_known;
3378 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3382 * smack_inet_conn_request - Smack access check on connect
3383 * @sk: socket involved
3387 * Returns 0 if a task with the packet label could write to
3388 * the socket, otherwise an error code
3390 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3391 struct request_sock *req)
3393 u16 family = sk->sk_family;
3394 struct smack_known *skp;
3395 struct socket_smack *ssp = sk->sk_security;
3396 struct netlbl_lsm_secattr secattr;
3397 struct sockaddr_in addr;
3401 struct smk_audit_info ad;
3403 struct lsm_network_audit net;
3406 if (family == PF_INET6) {
3408 * Handle mapped IPv4 packets arriving
3409 * via IPv6 sockets. Don't set up netlabel
3410 * processing on IPv6.
3412 if (skb->protocol == htons(ETH_P_IP))
3418 netlbl_secattr_init(&secattr);
3419 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3421 skp = smack_from_secattr(&secattr, ssp);
3423 skp = &smack_known_huh;
3424 netlbl_secattr_destroy(&secattr);
3427 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3428 ad.a.u.net->family = family;
3429 ad.a.u.net->netif = skb->skb_iif;
3430 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3433 * Receiving a packet requires that the other end be able to write
3434 * here. Read access is not required.
3436 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3441 * Save the peer's label in the request_sock so we can later setup
3442 * smk_packet in the child socket so that SO_PEERCRED can report it.
3444 req->peer_secid = skp->smk_secid;
3447 * We need to decide if we want to label the incoming connection here
3448 * if we do we only need to label the request_sock and the stack will
3449 * propagate the wire-label to the sock when it is created.
3452 addr.sin_addr.s_addr = hdr->saddr;
3454 hsp = smack_host_label(&addr);
3458 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3460 netlbl_req_delattr(req);
3466 * smack_inet_csk_clone - Copy the connection information to the new socket
3467 * @sk: the new socket
3468 * @req: the connection's request_sock
3470 * Transfer the connection's peer label to the newly created socket.
3472 static void smack_inet_csk_clone(struct sock *sk,
3473 const struct request_sock *req)
3475 struct socket_smack *ssp = sk->sk_security;
3476 struct smack_known *skp;
3478 if (req->peer_secid != 0) {
3479 skp = smack_from_secid(req->peer_secid);
3480 ssp->smk_packet = skp->smk_known;
3482 ssp->smk_packet = NULL;
3486 * Key management security hooks
3488 * Casey has not tested key support very heavily.
3489 * The permission check is most likely too restrictive.
3490 * If you care about keys please have a look.
3495 * smack_key_alloc - Set the key security blob
3497 * @cred: the credentials to use
3500 * No allocation required
3504 static int smack_key_alloc(struct key *key, const struct cred *cred,
3505 unsigned long flags)
3507 struct smack_known *skp = smk_of_task(cred->security);
3509 key->security = skp->smk_known;
3514 * smack_key_free - Clear the key security blob
3517 * Clear the blob pointer
3519 static void smack_key_free(struct key *key)
3521 key->security = NULL;
3525 * smack_key_permission - Smack access on a key
3526 * @key_ref: gets to the object
3527 * @cred: the credentials to use
3530 * Return 0 if the task has read and write to the object,
3531 * an error code otherwise
3533 static int smack_key_permission(key_ref_t key_ref,
3534 const struct cred *cred, key_perm_t perm)
3537 struct smk_audit_info ad;
3538 struct smack_known *tkp = smk_of_task(cred->security);
3540 keyp = key_ref_to_ptr(key_ref);
3544 * If the key hasn't been initialized give it access so that
3547 if (keyp->security == NULL)
3550 * This should not occur
3555 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3556 ad.a.u.key_struct.key = keyp->serial;
3557 ad.a.u.key_struct.key_desc = keyp->description;
3559 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
3561 #endif /* CONFIG_KEYS */
3566 * Audit requires a unique representation of each Smack specific
3567 * rule. This unique representation is used to distinguish the
3568 * object to be audited from remaining kernel objects and also
3569 * works as a glue between the audit hooks.
3571 * Since repository entries are added but never deleted, we'll use
3572 * the smack_known label address related to the given audit rule as
3573 * the needed unique representation. This also better fits the smack
3574 * model where nearly everything is a label.
3579 * smack_audit_rule_init - Initialize a smack audit rule
3580 * @field: audit rule fields given from user-space (audit.h)
3581 * @op: required testing operator (=, !=, >, <, ...)
3582 * @rulestr: smack label to be audited
3583 * @vrule: pointer to save our own audit rule representation
3585 * Prepare to audit cases where (@field @op @rulestr) is true.
3586 * The label to be audited is created if necessay.
3588 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3590 char **rule = (char **)vrule;
3593 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3596 if (op != Audit_equal && op != Audit_not_equal)
3599 *rule = smk_import(rulestr, 0);
3605 * smack_audit_rule_known - Distinguish Smack audit rules
3606 * @krule: rule of interest, in Audit kernel representation format
3608 * This is used to filter Smack rules from remaining Audit ones.
3609 * If it's proved that this rule belongs to us, the
3610 * audit_rule_match hook will be called to do the final judgement.
3612 static int smack_audit_rule_known(struct audit_krule *krule)
3614 struct audit_field *f;
3617 for (i = 0; i < krule->field_count; i++) {
3618 f = &krule->fields[i];
3620 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3628 * smack_audit_rule_match - Audit given object ?
3629 * @secid: security id for identifying the object to test
3630 * @field: audit rule flags given from user-space
3631 * @op: required testing operator
3632 * @vrule: smack internal rule presentation
3633 * @actx: audit context associated with the check
3635 * The core Audit hook. It's used to take the decision of
3636 * whether to audit or not to audit a given object.
3638 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3639 struct audit_context *actx)
3641 struct smack_known *skp;
3645 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
3646 "Smack: missing rule\n");
3650 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3653 skp = smack_from_secid(secid);
3656 * No need to do string comparisons. If a match occurs,
3657 * both pointers will point to the same smack_known
3660 if (op == Audit_equal)
3661 return (rule == skp->smk_known);
3662 if (op == Audit_not_equal)
3663 return (rule != skp->smk_known);
3669 * smack_audit_rule_free - free smack rule representation
3670 * @vrule: rule to be freed.
3672 * No memory was allocated.
3674 static void smack_audit_rule_free(void *vrule)
3679 #endif /* CONFIG_AUDIT */
3682 * smack_ismaclabel - check if xattr @name references a smack MAC label
3683 * @name: Full xattr name to check.
3685 static int smack_ismaclabel(const char *name)
3687 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3692 * smack_secid_to_secctx - return the smack label for a secid
3693 * @secid: incoming integer
3694 * @secdata: destination
3695 * @seclen: how long it is
3697 * Exists for networking code.
3699 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3701 struct smack_known *skp = smack_from_secid(secid);
3704 *secdata = skp->smk_known;
3705 *seclen = strlen(skp->smk_known);
3710 * smack_secctx_to_secid - return the secid for a smack label
3711 * @secdata: smack label
3712 * @seclen: how long result is
3713 * @secid: outgoing integer
3715 * Exists for audit and networking code.
3717 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3719 *secid = smack_to_secid(secdata);
3724 * smack_release_secctx - don't do anything.
3728 * Exists to make sure nothing gets done, and properly
3730 static void smack_release_secctx(char *secdata, u32 seclen)
3734 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3736 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3739 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3741 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3744 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3747 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3755 struct security_operations smack_ops = {
3758 .ptrace_access_check = smack_ptrace_access_check,
3759 .ptrace_traceme = smack_ptrace_traceme,
3760 .syslog = smack_syslog,
3762 .sb_alloc_security = smack_sb_alloc_security,
3763 .sb_free_security = smack_sb_free_security,
3764 .sb_copy_data = smack_sb_copy_data,
3765 .sb_kern_mount = smack_sb_kern_mount,
3766 .sb_statfs = smack_sb_statfs,
3767 .sb_mount = smack_sb_mount,
3768 .sb_umount = smack_sb_umount,
3770 .bprm_set_creds = smack_bprm_set_creds,
3771 .bprm_committing_creds = smack_bprm_committing_creds,
3772 .bprm_secureexec = smack_bprm_secureexec,
3774 .inode_alloc_security = smack_inode_alloc_security,
3775 .inode_free_security = smack_inode_free_security,
3776 .inode_init_security = smack_inode_init_security,
3777 .inode_link = smack_inode_link,
3778 .inode_unlink = smack_inode_unlink,
3779 .inode_rmdir = smack_inode_rmdir,
3780 .inode_rename = smack_inode_rename,
3781 .inode_permission = smack_inode_permission,
3782 .inode_setattr = smack_inode_setattr,
3783 .inode_getattr = smack_inode_getattr,
3784 .inode_setxattr = smack_inode_setxattr,
3785 .inode_post_setxattr = smack_inode_post_setxattr,
3786 .inode_getxattr = smack_inode_getxattr,
3787 .inode_removexattr = smack_inode_removexattr,
3788 .inode_getsecurity = smack_inode_getsecurity,
3789 .inode_setsecurity = smack_inode_setsecurity,
3790 .inode_listsecurity = smack_inode_listsecurity,
3791 .inode_getsecid = smack_inode_getsecid,
3793 .file_permission = smack_file_permission,
3794 .file_alloc_security = smack_file_alloc_security,
3795 .file_free_security = smack_file_free_security,
3796 .file_ioctl = smack_file_ioctl,
3797 .file_lock = smack_file_lock,
3798 .file_fcntl = smack_file_fcntl,
3799 .mmap_file = smack_mmap_file,
3800 .mmap_addr = cap_mmap_addr,
3801 .file_set_fowner = smack_file_set_fowner,
3802 .file_send_sigiotask = smack_file_send_sigiotask,
3803 .file_receive = smack_file_receive,
3805 .file_open = smack_file_open,
3807 .cred_alloc_blank = smack_cred_alloc_blank,
3808 .cred_free = smack_cred_free,
3809 .cred_prepare = smack_cred_prepare,
3810 .cred_transfer = smack_cred_transfer,
3811 .kernel_act_as = smack_kernel_act_as,
3812 .kernel_create_files_as = smack_kernel_create_files_as,
3813 .task_setpgid = smack_task_setpgid,
3814 .task_getpgid = smack_task_getpgid,
3815 .task_getsid = smack_task_getsid,
3816 .task_getsecid = smack_task_getsecid,
3817 .task_setnice = smack_task_setnice,
3818 .task_setioprio = smack_task_setioprio,
3819 .task_getioprio = smack_task_getioprio,
3820 .task_setscheduler = smack_task_setscheduler,
3821 .task_getscheduler = smack_task_getscheduler,
3822 .task_movememory = smack_task_movememory,
3823 .task_kill = smack_task_kill,
3824 .task_wait = smack_task_wait,
3825 .task_to_inode = smack_task_to_inode,
3827 .ipc_permission = smack_ipc_permission,
3828 .ipc_getsecid = smack_ipc_getsecid,
3830 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3831 .msg_msg_free_security = smack_msg_msg_free_security,
3833 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3834 .msg_queue_free_security = smack_msg_queue_free_security,
3835 .msg_queue_associate = smack_msg_queue_associate,
3836 .msg_queue_msgctl = smack_msg_queue_msgctl,
3837 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3838 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3840 .shm_alloc_security = smack_shm_alloc_security,
3841 .shm_free_security = smack_shm_free_security,
3842 .shm_associate = smack_shm_associate,
3843 .shm_shmctl = smack_shm_shmctl,
3844 .shm_shmat = smack_shm_shmat,
3846 .sem_alloc_security = smack_sem_alloc_security,
3847 .sem_free_security = smack_sem_free_security,
3848 .sem_associate = smack_sem_associate,
3849 .sem_semctl = smack_sem_semctl,
3850 .sem_semop = smack_sem_semop,
3852 .d_instantiate = smack_d_instantiate,
3854 .getprocattr = smack_getprocattr,
3855 .setprocattr = smack_setprocattr,
3857 .unix_stream_connect = smack_unix_stream_connect,
3858 .unix_may_send = smack_unix_may_send,
3860 .socket_post_create = smack_socket_post_create,
3861 .socket_bind = smack_socket_bind,
3862 .socket_connect = smack_socket_connect,
3863 .socket_sendmsg = smack_socket_sendmsg,
3864 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3865 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3866 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3867 .sk_alloc_security = smack_sk_alloc_security,
3868 .sk_free_security = smack_sk_free_security,
3869 .sock_graft = smack_sock_graft,
3870 .inet_conn_request = smack_inet_conn_request,
3871 .inet_csk_clone = smack_inet_csk_clone,
3873 /* key management security hooks */
3875 .key_alloc = smack_key_alloc,
3876 .key_free = smack_key_free,
3877 .key_permission = smack_key_permission,
3878 #endif /* CONFIG_KEYS */
3882 .audit_rule_init = smack_audit_rule_init,
3883 .audit_rule_known = smack_audit_rule_known,
3884 .audit_rule_match = smack_audit_rule_match,
3885 .audit_rule_free = smack_audit_rule_free,
3886 #endif /* CONFIG_AUDIT */
3888 .ismaclabel = smack_ismaclabel,
3889 .secid_to_secctx = smack_secid_to_secctx,
3890 .secctx_to_secid = smack_secctx_to_secid,
3891 .release_secctx = smack_release_secctx,
3892 .inode_notifysecctx = smack_inode_notifysecctx,
3893 .inode_setsecctx = smack_inode_setsecctx,
3894 .inode_getsecctx = smack_inode_getsecctx,
3898 static __init void init_smack_known_list(void)
3901 * Initialize rule list locks
3903 mutex_init(&smack_known_huh.smk_rules_lock);
3904 mutex_init(&smack_known_hat.smk_rules_lock);
3905 mutex_init(&smack_known_floor.smk_rules_lock);
3906 mutex_init(&smack_known_star.smk_rules_lock);
3907 mutex_init(&smack_known_invalid.smk_rules_lock);
3908 mutex_init(&smack_known_web.smk_rules_lock);
3910 * Initialize rule lists
3912 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3913 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3914 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3915 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3916 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3917 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3919 * Create the known labels list
3921 smk_insert_entry(&smack_known_huh);
3922 smk_insert_entry(&smack_known_hat);
3923 smk_insert_entry(&smack_known_star);
3924 smk_insert_entry(&smack_known_floor);
3925 smk_insert_entry(&smack_known_invalid);
3926 smk_insert_entry(&smack_known_web);
3930 * smack_init - initialize the smack system
3934 static __init int smack_init(void)
3937 struct task_smack *tsp;
3939 if (!security_module_enable(&smack_ops))
3942 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3947 printk(KERN_INFO "Smack: Initializing.\n");
3950 * Set the security state for the initial task.
3952 cred = (struct cred *) current->cred;
3953 cred->security = tsp;
3955 /* initialize the smack_known_list */
3956 init_smack_known_list();
3961 if (register_security(&smack_ops))
3962 panic("smack: Unable to register with kernel.\n");
3968 * Smack requires early initialization in order to label
3969 * all processes and objects when they are created.
3971 security_initcall(smack_init);