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, 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 = kstrdup(XATTR_SMACK_SUFFIX, GFP_NOFS);
602 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
606 * If the access rule allows transmutation and
607 * the directory requests transmutation then
608 * by all means transmute.
609 * Mark the inode as changed.
611 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
612 smk_inode_transmutable(dir)) {
614 issp->smk_flags |= SMK_INODE_CHANGED;
617 *value = kstrdup(isp, GFP_NOFS);
623 *len = strlen(isp) + 1;
629 * smack_inode_link - Smack check on link
630 * @old_dentry: the existing object
632 * @new_dentry: the new object
634 * Returns 0 if access is permitted, an error code otherwise
636 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
637 struct dentry *new_dentry)
640 struct smk_audit_info ad;
643 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
644 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
646 isp = smk_of_inode(old_dentry->d_inode);
647 rc = smk_curacc(isp, MAY_WRITE, &ad);
649 if (rc == 0 && new_dentry->d_inode != NULL) {
650 isp = smk_of_inode(new_dentry->d_inode);
651 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
652 rc = smk_curacc(isp, MAY_WRITE, &ad);
659 * smack_inode_unlink - Smack check on inode deletion
660 * @dir: containing directory object
661 * @dentry: file to unlink
663 * Returns 0 if current can write the containing directory
664 * and the object, error code otherwise
666 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
668 struct inode *ip = dentry->d_inode;
669 struct smk_audit_info ad;
672 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
673 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
676 * You need write access to the thing you're unlinking
678 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
681 * You also need write access to the containing directory
683 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
684 smk_ad_setfield_u_fs_inode(&ad, dir);
685 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
691 * smack_inode_rmdir - Smack check on directory deletion
692 * @dir: containing directory object
693 * @dentry: directory to unlink
695 * Returns 0 if current can write the containing directory
696 * and the directory, error code otherwise
698 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
700 struct smk_audit_info ad;
703 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
704 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
707 * You need write access to the thing you're removing
709 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
712 * You also need write access to the containing directory
714 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
715 smk_ad_setfield_u_fs_inode(&ad, dir);
716 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
723 * smack_inode_rename - Smack check on rename
724 * @old_inode: the old directory
725 * @old_dentry: unused
726 * @new_inode: the new directory
727 * @new_dentry: unused
729 * Read and write access is required on both the old and
732 * Returns 0 if access is permitted, an error code otherwise
734 static int smack_inode_rename(struct inode *old_inode,
735 struct dentry *old_dentry,
736 struct inode *new_inode,
737 struct dentry *new_dentry)
741 struct smk_audit_info ad;
743 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
744 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
746 isp = smk_of_inode(old_dentry->d_inode);
747 rc = smk_curacc(isp, MAY_READWRITE, &ad);
749 if (rc == 0 && new_dentry->d_inode != NULL) {
750 isp = smk_of_inode(new_dentry->d_inode);
751 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
752 rc = smk_curacc(isp, MAY_READWRITE, &ad);
758 * smack_inode_permission - Smack version of permission()
759 * @inode: the inode in question
760 * @mask: the access requested
762 * This is the important Smack hook.
764 * Returns 0 if access is permitted, -EACCES otherwise
766 static int smack_inode_permission(struct inode *inode, int mask)
768 struct smk_audit_info ad;
769 int no_block = mask & MAY_NOT_BLOCK;
771 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
773 * No permission to check. Existence test. Yup, it's there.
778 /* May be droppable after audit */
781 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
782 smk_ad_setfield_u_fs_inode(&ad, inode);
783 return smk_curacc(smk_of_inode(inode), mask, &ad);
787 * smack_inode_setattr - Smack check for setting attributes
788 * @dentry: the object
789 * @iattr: for the force flag
791 * Returns 0 if access is permitted, an error code otherwise
793 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
795 struct smk_audit_info ad;
797 * Need to allow for clearing the setuid bit.
799 if (iattr->ia_valid & ATTR_FORCE)
801 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
802 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
804 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
808 * smack_inode_getattr - Smack check for getting attributes
810 * @dentry: the object
812 * Returns 0 if access is permitted, an error code otherwise
814 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
816 struct smk_audit_info ad;
819 path.dentry = dentry;
822 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
823 smk_ad_setfield_u_fs_path(&ad, path);
824 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
828 * smack_inode_setxattr - Smack check for setting xattrs
829 * @dentry: the object
830 * @name: name of the attribute
835 * This protects the Smack attribute explicitly.
837 * Returns 0 if access is permitted, an error code otherwise
839 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
840 const void *value, size_t size, int flags)
842 struct smk_audit_info ad;
845 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
846 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
847 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
848 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
849 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
850 if (!smack_privileged(CAP_MAC_ADMIN))
853 * check label validity here so import wont fail on
856 if (size == 0 || size >= SMK_LONGLABEL ||
857 smk_import(value, size) == NULL)
859 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
860 if (!smack_privileged(CAP_MAC_ADMIN))
862 if (size != TRANS_TRUE_SIZE ||
863 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
866 rc = cap_inode_setxattr(dentry, name, value, size, flags);
868 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
869 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
872 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
878 * smack_inode_post_setxattr - Apply the Smack update approved above
880 * @name: attribute name
881 * @value: attribute value
882 * @size: attribute size
885 * Set the pointer in the inode blob to the entry found
886 * in the master label list.
888 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
889 const void *value, size_t size, int flags)
891 struct smack_known *skp;
892 struct inode_smack *isp = dentry->d_inode->i_security;
894 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
895 isp->smk_flags |= SMK_INODE_TRANSMUTE;
899 skp = smk_import_entry(value, size);
900 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
902 isp->smk_inode = skp->smk_known;
904 isp->smk_inode = smack_known_invalid.smk_known;
905 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
909 isp->smk_task = &smack_known_invalid;
910 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
914 isp->smk_mmap = &smack_known_invalid;
921 * smack_inode_getxattr - Smack check on getxattr
922 * @dentry: the object
925 * Returns 0 if access is permitted, an error code otherwise
927 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
929 struct smk_audit_info ad;
931 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
932 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
934 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
938 * smack_inode_removexattr - Smack check on removexattr
939 * @dentry: the object
940 * @name: name of the attribute
942 * Removing the Smack attribute requires CAP_MAC_ADMIN
944 * Returns 0 if access is permitted, an error code otherwise
946 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
948 struct inode_smack *isp;
949 struct smk_audit_info ad;
952 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
953 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
954 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
955 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
956 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
957 strcmp(name, XATTR_NAME_SMACKMMAP)) {
958 if (!smack_privileged(CAP_MAC_ADMIN))
961 rc = cap_inode_removexattr(dentry, name);
963 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
964 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
966 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
969 isp = dentry->d_inode->i_security;
970 isp->smk_task = NULL;
971 isp->smk_mmap = NULL;
978 * smack_inode_getsecurity - get smack xattrs
980 * @name: attribute name
981 * @buffer: where to put the result
984 * Returns the size of the attribute or an error code
986 static int smack_inode_getsecurity(const struct inode *inode,
987 const char *name, void **buffer,
990 struct socket_smack *ssp;
992 struct super_block *sbp;
993 struct inode *ip = (struct inode *)inode;
998 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
999 isp = smk_of_inode(inode);
1000 ilen = strlen(isp) + 1;
1006 * The rest of the Smack xattrs are only on sockets.
1009 if (sbp->s_magic != SOCKFS_MAGIC)
1012 sock = SOCKET_I(ip);
1013 if (sock == NULL || sock->sk == NULL)
1016 ssp = sock->sk->sk_security;
1018 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1020 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1021 isp = ssp->smk_out->smk_known;
1025 ilen = strlen(isp) + 1;
1036 * smack_inode_listsecurity - list the Smack attributes
1037 * @inode: the object
1038 * @buffer: where they go
1039 * @buffer_size: size of buffer
1041 * Returns 0 on success, -EINVAL otherwise
1043 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1046 int len = strlen(XATTR_NAME_SMACK);
1048 if (buffer != NULL && len <= buffer_size) {
1049 memcpy(buffer, XATTR_NAME_SMACK, len);
1056 * smack_inode_getsecid - Extract inode's security id
1057 * @inode: inode to extract the info from
1058 * @secid: where result will be saved
1060 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1062 struct inode_smack *isp = inode->i_security;
1064 *secid = smack_to_secid(isp->smk_inode);
1072 * smack_file_permission - Smack check on file operations
1078 * Should access checks be done on each read or write?
1079 * UNICOS and SELinux say yes.
1080 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1082 * I'll say no for now. Smack does not do the frequent
1083 * label changing that SELinux does.
1085 static int smack_file_permission(struct file *file, int mask)
1091 * smack_file_alloc_security - assign a file security blob
1094 * The security blob for a file is a pointer to the master
1095 * label list, so no allocation is done.
1099 static int smack_file_alloc_security(struct file *file)
1101 struct smack_known *skp = smk_of_current();
1103 file->f_security = skp->smk_known;
1108 * smack_file_free_security - clear a file security blob
1111 * The security blob for a file is a pointer to the master
1112 * label list, so no memory is freed.
1114 static void smack_file_free_security(struct file *file)
1116 file->f_security = NULL;
1120 * smack_file_ioctl - Smack check on ioctls
1125 * Relies heavily on the correct use of the ioctl command conventions.
1127 * Returns 0 if allowed, error code otherwise
1129 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1133 struct smk_audit_info ad;
1135 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1136 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1138 if (_IOC_DIR(cmd) & _IOC_WRITE)
1139 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1141 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1142 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1148 * smack_file_lock - Smack check on file locking
1152 * Returns 0 if current has write access, error code otherwise
1154 static int smack_file_lock(struct file *file, unsigned int cmd)
1156 struct smk_audit_info ad;
1158 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1159 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1160 return smk_curacc(file->f_security, MAY_WRITE, &ad);
1164 * smack_file_fcntl - Smack check on fcntl
1166 * @cmd: what action to check
1169 * Generally these operations are harmless.
1170 * File locking operations present an obvious mechanism
1171 * for passing information, so they require write access.
1173 * Returns 0 if current has access, error code otherwise
1175 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1178 struct smk_audit_info ad;
1188 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1189 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1190 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1201 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1202 * if mapping anonymous memory.
1203 * @file contains the file structure for file to map (may be NULL).
1204 * @reqprot contains the protection requested by the application.
1205 * @prot contains the protection that will be applied by the kernel.
1206 * @flags contains the operational flags.
1207 * Return 0 if permission is granted.
1209 static int smack_mmap_file(struct file *file,
1210 unsigned long reqprot, unsigned long prot,
1211 unsigned long flags)
1213 struct smack_known *skp;
1214 struct smack_known *mkp;
1215 struct smack_rule *srp;
1216 struct task_smack *tsp;
1218 struct inode_smack *isp;
1227 isp = file_inode(file)->i_security;
1228 if (isp->smk_mmap == NULL)
1230 mkp = isp->smk_mmap;
1232 tsp = current_security();
1233 skp = smk_of_current();
1238 * For each Smack rule associated with the subject
1239 * label verify that the SMACK64MMAP also has access
1240 * to that rule's object label.
1242 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1243 osmack = srp->smk_object;
1245 * Matching labels always allows access.
1247 if (mkp->smk_known == osmack)
1250 * If there is a matching local rule take
1251 * that into account as well.
1253 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1256 may = srp->smk_access;
1258 may &= srp->smk_access;
1260 * If may is zero the SMACK64MMAP subject can't
1261 * possibly have less access.
1267 * Fetch the global list entry.
1268 * If there isn't one a SMACK64MMAP subject
1269 * can't have as much access as current.
1271 mmay = smk_access_entry(mkp->smk_known, osmack,
1273 if (mmay == -ENOENT) {
1278 * If there is a local entry it modifies the
1279 * potential access, too.
1281 tmay = smk_access_entry(mkp->smk_known, osmack,
1283 if (tmay != -ENOENT)
1287 * If there is any access available to current that is
1288 * not available to a SMACK64MMAP subject
1291 if ((may | mmay) != mmay) {
1303 * smack_file_set_fowner - set the file security blob value
1304 * @file: object in question
1307 * Further research may be required on this one.
1309 static int smack_file_set_fowner(struct file *file)
1311 struct smack_known *skp = smk_of_current();
1313 file->f_security = skp->smk_known;
1318 * smack_file_send_sigiotask - Smack on sigio
1319 * @tsk: The target task
1320 * @fown: the object the signal come from
1323 * Allow a privileged task to get signals even if it shouldn't
1325 * Returns 0 if a subject with the object's smack could
1326 * write to the task, an error code otherwise.
1328 static int smack_file_send_sigiotask(struct task_struct *tsk,
1329 struct fown_struct *fown, int signum)
1331 struct smack_known *skp;
1332 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1335 struct smk_audit_info ad;
1338 * struct fown_struct is never outside the context of a struct file
1340 file = container_of(fown, struct file, f_owner);
1342 /* we don't log here as rc can be overriden */
1343 skp = smk_find_entry(file->f_security);
1344 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1345 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1348 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1349 smk_ad_setfield_u_tsk(&ad, tsk);
1350 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1355 * smack_file_receive - Smack file receive check
1358 * Returns 0 if current has access, error code otherwise
1360 static int smack_file_receive(struct file *file)
1363 struct smk_audit_info ad;
1365 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1366 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1368 * This code relies on bitmasks.
1370 if (file->f_mode & FMODE_READ)
1372 if (file->f_mode & FMODE_WRITE)
1375 return smk_curacc(file->f_security, may, &ad);
1379 * smack_file_open - Smack dentry open processing
1383 * Set the security blob in the file structure.
1387 static int smack_file_open(struct file *file, const struct cred *cred)
1389 struct inode_smack *isp = file_inode(file)->i_security;
1391 file->f_security = isp->smk_inode;
1401 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1402 * @new: the new credentials
1403 * @gfp: the atomicity of any memory allocations
1405 * Prepare a blank set of credentials for modification. This must allocate all
1406 * the memory the LSM module might require such that cred_transfer() can
1407 * complete without error.
1409 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1411 struct task_smack *tsp;
1413 tsp = new_task_smack(NULL, NULL, gfp);
1417 cred->security = tsp;
1424 * smack_cred_free - "free" task-level security credentials
1425 * @cred: the credentials in question
1428 static void smack_cred_free(struct cred *cred)
1430 struct task_smack *tsp = cred->security;
1431 struct smack_rule *rp;
1432 struct list_head *l;
1433 struct list_head *n;
1437 cred->security = NULL;
1439 list_for_each_safe(l, n, &tsp->smk_rules) {
1440 rp = list_entry(l, struct smack_rule, list);
1441 list_del(&rp->list);
1448 * smack_cred_prepare - prepare new set of credentials for modification
1449 * @new: the new credentials
1450 * @old: the original credentials
1451 * @gfp: the atomicity of any memory allocations
1453 * Prepare a new set of credentials for modification.
1455 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1458 struct task_smack *old_tsp = old->security;
1459 struct task_smack *new_tsp;
1462 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1463 if (new_tsp == NULL)
1466 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1470 new->security = new_tsp;
1475 * smack_cred_transfer - Transfer the old credentials to the new credentials
1476 * @new: the new credentials
1477 * @old: the original credentials
1479 * Fill in a set of blank credentials from another set of credentials.
1481 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1483 struct task_smack *old_tsp = old->security;
1484 struct task_smack *new_tsp = new->security;
1486 new_tsp->smk_task = old_tsp->smk_task;
1487 new_tsp->smk_forked = old_tsp->smk_task;
1488 mutex_init(&new_tsp->smk_rules_lock);
1489 INIT_LIST_HEAD(&new_tsp->smk_rules);
1492 /* cbs copy rule list */
1496 * smack_kernel_act_as - Set the subjective context in a set of credentials
1497 * @new: points to the set of credentials to be modified.
1498 * @secid: specifies the security ID to be set
1500 * Set the security data for a kernel service.
1502 static int smack_kernel_act_as(struct cred *new, u32 secid)
1504 struct task_smack *new_tsp = new->security;
1505 struct smack_known *skp = smack_from_secid(secid);
1510 new_tsp->smk_task = skp;
1515 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1516 * @new: points to the set of credentials to be modified
1517 * @inode: points to the inode to use as a reference
1519 * Set the file creation context in a set of credentials to the same
1520 * as the objective context of the specified inode
1522 static int smack_kernel_create_files_as(struct cred *new,
1523 struct inode *inode)
1525 struct inode_smack *isp = inode->i_security;
1526 struct task_smack *tsp = new->security;
1528 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1529 tsp->smk_task = tsp->smk_forked;
1534 * smk_curacc_on_task - helper to log task related access
1535 * @p: the task object
1536 * @access: the access requested
1537 * @caller: name of the calling function for audit
1539 * Return 0 if access is permitted
1541 static int smk_curacc_on_task(struct task_struct *p, int access,
1544 struct smk_audit_info ad;
1545 struct smack_known *skp = smk_of_task(task_security(p));
1547 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1548 smk_ad_setfield_u_tsk(&ad, p);
1549 return smk_curacc(skp->smk_known, access, &ad);
1553 * smack_task_setpgid - Smack check on setting pgid
1554 * @p: the task object
1557 * Return 0 if write access is permitted
1559 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1561 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1565 * smack_task_getpgid - Smack access check for getpgid
1566 * @p: the object task
1568 * Returns 0 if current can read the object task, error code otherwise
1570 static int smack_task_getpgid(struct task_struct *p)
1572 return smk_curacc_on_task(p, MAY_READ, __func__);
1576 * smack_task_getsid - Smack access check for getsid
1577 * @p: the object task
1579 * Returns 0 if current can read the object task, error code otherwise
1581 static int smack_task_getsid(struct task_struct *p)
1583 return smk_curacc_on_task(p, MAY_READ, __func__);
1587 * smack_task_getsecid - get the secid of the task
1588 * @p: the object task
1589 * @secid: where to put the result
1591 * Sets the secid to contain a u32 version of the smack label.
1593 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1595 struct smack_known *skp = smk_of_task(task_security(p));
1597 *secid = skp->smk_secid;
1601 * smack_task_setnice - Smack check on setting nice
1602 * @p: the task object
1605 * Return 0 if write access is permitted
1607 static int smack_task_setnice(struct task_struct *p, int nice)
1611 rc = cap_task_setnice(p, nice);
1613 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1618 * smack_task_setioprio - Smack check on setting ioprio
1619 * @p: the task object
1622 * Return 0 if write access is permitted
1624 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1628 rc = cap_task_setioprio(p, ioprio);
1630 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1635 * smack_task_getioprio - Smack check on reading ioprio
1636 * @p: the task object
1638 * Return 0 if read access is permitted
1640 static int smack_task_getioprio(struct task_struct *p)
1642 return smk_curacc_on_task(p, MAY_READ, __func__);
1646 * smack_task_setscheduler - Smack check on setting scheduler
1647 * @p: the task object
1651 * Return 0 if read access is permitted
1653 static int smack_task_setscheduler(struct task_struct *p)
1657 rc = cap_task_setscheduler(p);
1659 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1664 * smack_task_getscheduler - Smack check on reading scheduler
1665 * @p: the task object
1667 * Return 0 if read access is permitted
1669 static int smack_task_getscheduler(struct task_struct *p)
1671 return smk_curacc_on_task(p, MAY_READ, __func__);
1675 * smack_task_movememory - Smack check on moving memory
1676 * @p: the task object
1678 * Return 0 if write access is permitted
1680 static int smack_task_movememory(struct task_struct *p)
1682 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1686 * smack_task_kill - Smack check on signal delivery
1687 * @p: the task object
1690 * @secid: identifies the smack to use in lieu of current's
1692 * Return 0 if write access is permitted
1694 * The secid behavior is an artifact of an SELinux hack
1695 * in the USB code. Someday it may go away.
1697 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1700 struct smk_audit_info ad;
1701 struct smack_known *skp;
1702 struct smack_known *tkp = smk_of_task(task_security(p));
1704 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1705 smk_ad_setfield_u_tsk(&ad, p);
1707 * Sending a signal requires that the sender
1708 * can write the receiver.
1711 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
1713 * If the secid isn't 0 we're dealing with some USB IO
1714 * specific behavior. This is not clean. For one thing
1715 * we can't take privilege into account.
1717 skp = smack_from_secid(secid);
1718 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
1722 * smack_task_wait - Smack access check for waiting
1723 * @p: task to wait for
1727 static int smack_task_wait(struct task_struct *p)
1730 * Allow the operation to succeed.
1732 * In userless environments (e.g. phones) programs
1733 * get marked with SMACK64EXEC and even if the parent
1734 * and child shouldn't be talking the parent still
1735 * may expect to know when the child exits.
1741 * smack_task_to_inode - copy task smack into the inode blob
1742 * @p: task to copy from
1743 * @inode: inode to copy to
1745 * Sets the smack pointer in the inode security blob
1747 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1749 struct inode_smack *isp = inode->i_security;
1750 struct smack_known *skp = smk_of_task(task_security(p));
1752 isp->smk_inode = skp->smk_known;
1760 * smack_sk_alloc_security - Allocate a socket blob
1763 * @gfp_flags: memory allocation flags
1765 * Assign Smack pointers to current
1767 * Returns 0 on success, -ENOMEM is there's no memory
1769 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1771 struct smack_known *skp = smk_of_current();
1772 struct socket_smack *ssp;
1774 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1778 ssp->smk_in = skp->smk_known;
1780 ssp->smk_packet = NULL;
1782 sk->sk_security = ssp;
1788 * smack_sk_free_security - Free a socket blob
1791 * Clears the blob pointer
1793 static void smack_sk_free_security(struct sock *sk)
1795 kfree(sk->sk_security);
1799 * smack_host_label - check host based restrictions
1800 * @sip: the object end
1802 * looks for host based access restrictions
1804 * This version will only be appropriate for really small sets of single label
1805 * hosts. The caller is responsible for ensuring that the RCU read lock is
1806 * taken before calling this function.
1808 * Returns the label of the far end or NULL if it's not special.
1810 static char *smack_host_label(struct sockaddr_in *sip)
1812 struct smk_netlbladdr *snp;
1813 struct in_addr *siap = &sip->sin_addr;
1815 if (siap->s_addr == 0)
1818 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1820 * we break after finding the first match because
1821 * the list is sorted from longest to shortest mask
1822 * so we have found the most specific match
1824 if ((&snp->smk_host.sin_addr)->s_addr ==
1825 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1826 /* we have found the special CIPSO option */
1827 if (snp->smk_label == smack_cipso_option)
1829 return snp->smk_label;
1836 * smack_netlabel - Set the secattr on a socket
1838 * @labeled: socket label scheme
1840 * Convert the outbound smack value (smk_out) to a
1841 * secattr and attach it to the socket.
1843 * Returns 0 on success or an error code
1845 static int smack_netlabel(struct sock *sk, int labeled)
1847 struct smack_known *skp;
1848 struct socket_smack *ssp = sk->sk_security;
1852 * Usually the netlabel code will handle changing the
1853 * packet labeling based on the label.
1854 * The case of a single label host is different, because
1855 * a single label host should never get a labeled packet
1856 * even though the label is usually associated with a packet
1860 bh_lock_sock_nested(sk);
1862 if (ssp->smk_out == smack_net_ambient ||
1863 labeled == SMACK_UNLABELED_SOCKET)
1864 netlbl_sock_delattr(sk);
1867 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
1877 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1879 * @sap: the destination address
1881 * Set the correct secattr for the given socket based on the destination
1882 * address and perform any outbound access checks needed.
1884 * Returns 0 on success or an error code.
1887 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1889 struct smack_known *skp;
1893 struct socket_smack *ssp = sk->sk_security;
1894 struct smk_audit_info ad;
1897 hostsp = smack_host_label(sap);
1898 if (hostsp != NULL) {
1900 struct lsm_network_audit net;
1902 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1903 ad.a.u.net->family = sap->sin_family;
1904 ad.a.u.net->dport = sap->sin_port;
1905 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
1907 sk_lbl = SMACK_UNLABELED_SOCKET;
1909 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
1911 sk_lbl = SMACK_CIPSO_SOCKET;
1918 return smack_netlabel(sk, sk_lbl);
1922 * smk_ipv6_port_label - Smack port access table management
1926 * Create or update the port list entry
1928 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1930 struct sock *sk = sock->sk;
1931 struct sockaddr_in6 *addr6;
1932 struct socket_smack *ssp = sock->sk->sk_security;
1933 struct smk_port_label *spp;
1934 unsigned short port = 0;
1936 if (address == NULL) {
1938 * This operation is changing the Smack information
1939 * on the bound socket. Take the changes to the port
1942 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1943 if (sk != spp->smk_sock)
1945 spp->smk_in = ssp->smk_in;
1946 spp->smk_out = ssp->smk_out;
1950 * A NULL address is only used for updating existing
1951 * bound entries. If there isn't one, it's OK.
1956 addr6 = (struct sockaddr_in6 *)address;
1957 port = ntohs(addr6->sin6_port);
1959 * This is a special case that is safely ignored.
1965 * Look for an existing port list entry.
1966 * This is an indication that a port is getting reused.
1968 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1969 if (spp->smk_port != port)
1971 spp->smk_port = port;
1973 spp->smk_in = ssp->smk_in;
1974 spp->smk_out = ssp->smk_out;
1979 * A new port entry is required.
1981 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1985 spp->smk_port = port;
1987 spp->smk_in = ssp->smk_in;
1988 spp->smk_out = ssp->smk_out;
1990 list_add(&spp->list, &smk_ipv6_port_list);
1995 * smk_ipv6_port_check - check Smack port access
1999 * Create or update the port list entry
2001 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr *address,
2006 struct sockaddr_in6 *addr6;
2007 struct smk_port_label *spp;
2008 struct socket_smack *ssp = sk->sk_security;
2009 struct smack_known *skp;
2010 unsigned short port = 0;
2012 struct smk_audit_info ad;
2014 struct lsm_network_audit net;
2017 if (act == SMK_RECEIVING) {
2018 skp = smack_net_ambient;
2019 object = ssp->smk_in;
2022 object = smack_net_ambient->smk_known;
2026 * Get the IP address and port from the address.
2028 addr6 = (struct sockaddr_in6 *)address;
2029 port = ntohs(addr6->sin6_port);
2030 bep = (__be16 *)(&addr6->sin6_addr);
2031 be32p = (__be32 *)(&addr6->sin6_addr);
2034 * It's remote, so port lookup does no good.
2036 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2040 * It's local so the send check has to have passed.
2042 if (act == SMK_RECEIVING) {
2043 skp = &smack_known_web;
2047 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2048 if (spp->smk_port != port)
2050 object = spp->smk_in;
2051 if (act == SMK_CONNECTING)
2052 ssp->smk_packet = spp->smk_out->smk_known;
2059 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2060 ad.a.u.net->family = sk->sk_family;
2061 ad.a.u.net->dport = port;
2062 if (act == SMK_RECEIVING)
2063 ad.a.u.net->v6info.saddr = addr6->sin6_addr;
2065 ad.a.u.net->v6info.daddr = addr6->sin6_addr;
2067 return smk_access(skp, object, MAY_WRITE, &ad);
2071 * smack_inode_setsecurity - set smack xattrs
2072 * @inode: the object
2073 * @name: attribute name
2074 * @value: attribute value
2075 * @size: size of the attribute
2078 * Sets the named attribute in the appropriate blob
2080 * Returns 0 on success, or an error code
2082 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2083 const void *value, size_t size, int flags)
2085 struct smack_known *skp;
2086 struct inode_smack *nsp = inode->i_security;
2087 struct socket_smack *ssp;
2088 struct socket *sock;
2091 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2094 skp = smk_import_entry(value, size);
2098 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2099 nsp->smk_inode = skp->smk_known;
2100 nsp->smk_flags |= SMK_INODE_INSTANT;
2104 * The rest of the Smack xattrs are only on sockets.
2106 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2109 sock = SOCKET_I(inode);
2110 if (sock == NULL || sock->sk == NULL)
2113 ssp = sock->sk->sk_security;
2115 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2116 ssp->smk_in = skp->smk_known;
2117 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2119 if (sock->sk->sk_family == PF_INET) {
2120 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2123 "Smack: \"%s\" netlbl error %d.\n",
2129 if (sock->sk->sk_family == PF_INET6)
2130 smk_ipv6_port_label(sock, NULL);
2136 * smack_socket_post_create - finish socket setup
2138 * @family: protocol family
2143 * Sets the netlabel information on the socket
2145 * Returns 0 on success, and error code otherwise
2147 static int smack_socket_post_create(struct socket *sock, int family,
2148 int type, int protocol, int kern)
2150 if (family != PF_INET || sock->sk == NULL)
2153 * Set the outbound netlbl.
2155 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2159 * smack_socket_bind - record port binding information.
2161 * @address: the port address
2162 * @addrlen: size of the address
2164 * Records the label bound to a port.
2168 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2171 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2172 smk_ipv6_port_label(sock, address);
2178 * smack_socket_connect - connect access check
2180 * @sap: the other end
2181 * @addrlen: size of sap
2183 * Verifies that a connection may be possible
2185 * Returns 0 on success, and error code otherwise
2187 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2192 if (sock->sk == NULL)
2195 switch (sock->sk->sk_family) {
2197 if (addrlen < sizeof(struct sockaddr_in))
2199 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2202 if (addrlen < sizeof(struct sockaddr_in6))
2204 rc = smk_ipv6_port_check(sock->sk, sap, SMK_CONNECTING);
2211 * smack_flags_to_may - convert S_ to MAY_ values
2212 * @flags: the S_ value
2214 * Returns the equivalent MAY_ value
2216 static int smack_flags_to_may(int flags)
2220 if (flags & S_IRUGO)
2222 if (flags & S_IWUGO)
2224 if (flags & S_IXUGO)
2231 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2236 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2238 struct smack_known *skp = smk_of_current();
2240 msg->security = skp->smk_known;
2245 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2248 * Clears the blob pointer
2250 static void smack_msg_msg_free_security(struct msg_msg *msg)
2252 msg->security = NULL;
2256 * smack_of_shm - the smack pointer for the shm
2259 * Returns a pointer to the smack value
2261 static char *smack_of_shm(struct shmid_kernel *shp)
2263 return (char *)shp->shm_perm.security;
2267 * smack_shm_alloc_security - Set the security blob for shm
2272 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2274 struct kern_ipc_perm *isp = &shp->shm_perm;
2275 struct smack_known *skp = smk_of_current();
2277 isp->security = skp->smk_known;
2282 * smack_shm_free_security - Clear the security blob for shm
2285 * Clears the blob pointer
2287 static void smack_shm_free_security(struct shmid_kernel *shp)
2289 struct kern_ipc_perm *isp = &shp->shm_perm;
2291 isp->security = NULL;
2295 * smk_curacc_shm : check if current has access on shm
2297 * @access : access requested
2299 * Returns 0 if current has the requested access, error code otherwise
2301 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2303 char *ssp = smack_of_shm(shp);
2304 struct smk_audit_info ad;
2307 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2308 ad.a.u.ipc_id = shp->shm_perm.id;
2310 return smk_curacc(ssp, access, &ad);
2314 * smack_shm_associate - Smack access check for shm
2316 * @shmflg: access requested
2318 * Returns 0 if current has the requested access, error code otherwise
2320 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2324 may = smack_flags_to_may(shmflg);
2325 return smk_curacc_shm(shp, may);
2329 * smack_shm_shmctl - Smack access check for shm
2331 * @cmd: what it wants to do
2333 * Returns 0 if current has the requested access, error code otherwise
2335 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2348 may = MAY_READWRITE;
2353 * System level information.
2359 return smk_curacc_shm(shp, may);
2363 * smack_shm_shmat - Smack access for shmat
2366 * @shmflg: access requested
2368 * Returns 0 if current has the requested access, error code otherwise
2370 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2375 may = smack_flags_to_may(shmflg);
2376 return smk_curacc_shm(shp, may);
2380 * smack_of_sem - the smack pointer for the sem
2383 * Returns a pointer to the smack value
2385 static char *smack_of_sem(struct sem_array *sma)
2387 return (char *)sma->sem_perm.security;
2391 * smack_sem_alloc_security - Set the security blob for sem
2396 static int smack_sem_alloc_security(struct sem_array *sma)
2398 struct kern_ipc_perm *isp = &sma->sem_perm;
2399 struct smack_known *skp = smk_of_current();
2401 isp->security = skp->smk_known;
2406 * smack_sem_free_security - Clear the security blob for sem
2409 * Clears the blob pointer
2411 static void smack_sem_free_security(struct sem_array *sma)
2413 struct kern_ipc_perm *isp = &sma->sem_perm;
2415 isp->security = NULL;
2419 * smk_curacc_sem : check if current has access on sem
2421 * @access : access requested
2423 * Returns 0 if current has the requested access, error code otherwise
2425 static int smk_curacc_sem(struct sem_array *sma, int access)
2427 char *ssp = smack_of_sem(sma);
2428 struct smk_audit_info ad;
2431 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2432 ad.a.u.ipc_id = sma->sem_perm.id;
2434 return smk_curacc(ssp, access, &ad);
2438 * smack_sem_associate - Smack access check for sem
2440 * @semflg: access requested
2442 * Returns 0 if current has the requested access, error code otherwise
2444 static int smack_sem_associate(struct sem_array *sma, int semflg)
2448 may = smack_flags_to_may(semflg);
2449 return smk_curacc_sem(sma, may);
2453 * smack_sem_shmctl - Smack access check for sem
2455 * @cmd: what it wants to do
2457 * Returns 0 if current has the requested access, error code otherwise
2459 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2477 may = MAY_READWRITE;
2482 * System level information
2489 return smk_curacc_sem(sma, may);
2493 * smack_sem_semop - Smack checks of semaphore operations
2499 * Treated as read and write in all cases.
2501 * Returns 0 if access is allowed, error code otherwise
2503 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2504 unsigned nsops, int alter)
2506 return smk_curacc_sem(sma, MAY_READWRITE);
2510 * smack_msg_alloc_security - Set the security blob for msg
2515 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2517 struct kern_ipc_perm *kisp = &msq->q_perm;
2518 struct smack_known *skp = smk_of_current();
2520 kisp->security = skp->smk_known;
2525 * smack_msg_free_security - Clear the security blob for msg
2528 * Clears the blob pointer
2530 static void smack_msg_queue_free_security(struct msg_queue *msq)
2532 struct kern_ipc_perm *kisp = &msq->q_perm;
2534 kisp->security = NULL;
2538 * smack_of_msq - the smack pointer for the msq
2541 * Returns a pointer to the smack value
2543 static char *smack_of_msq(struct msg_queue *msq)
2545 return (char *)msq->q_perm.security;
2549 * smk_curacc_msq : helper to check if current has access on msq
2551 * @access : access requested
2553 * return 0 if current has access, error otherwise
2555 static int smk_curacc_msq(struct msg_queue *msq, int access)
2557 char *msp = smack_of_msq(msq);
2558 struct smk_audit_info ad;
2561 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2562 ad.a.u.ipc_id = msq->q_perm.id;
2564 return smk_curacc(msp, access, &ad);
2568 * smack_msg_queue_associate - Smack access check for msg_queue
2570 * @msqflg: access requested
2572 * Returns 0 if current has the requested access, error code otherwise
2574 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2578 may = smack_flags_to_may(msqflg);
2579 return smk_curacc_msq(msq, may);
2583 * smack_msg_queue_msgctl - Smack access check for msg_queue
2585 * @cmd: what it wants to do
2587 * Returns 0 if current has the requested access, error code otherwise
2589 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2600 may = MAY_READWRITE;
2605 * System level information
2612 return smk_curacc_msq(msq, may);
2616 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2619 * @msqflg: access requested
2621 * Returns 0 if current has the requested access, error code otherwise
2623 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2628 may = smack_flags_to_may(msqflg);
2629 return smk_curacc_msq(msq, may);
2633 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2640 * Returns 0 if current has read and write access, error code otherwise
2642 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2643 struct task_struct *target, long type, int mode)
2645 return smk_curacc_msq(msq, MAY_READWRITE);
2649 * smack_ipc_permission - Smack access for ipc_permission()
2650 * @ipp: the object permissions
2651 * @flag: access requested
2653 * Returns 0 if current has read and write access, error code otherwise
2655 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2657 char *isp = ipp->security;
2658 int may = smack_flags_to_may(flag);
2659 struct smk_audit_info ad;
2662 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2663 ad.a.u.ipc_id = ipp->id;
2665 return smk_curacc(isp, may, &ad);
2669 * smack_ipc_getsecid - Extract smack security id
2670 * @ipp: the object permissions
2671 * @secid: where result will be saved
2673 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2675 char *smack = ipp->security;
2677 *secid = smack_to_secid(smack);
2681 * smack_d_instantiate - Make sure the blob is correct on an inode
2682 * @opt_dentry: dentry where inode will be attached
2683 * @inode: the object
2685 * Set the inode's security blob if it hasn't been done already.
2687 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2689 struct super_block *sbp;
2690 struct superblock_smack *sbsp;
2691 struct inode_smack *isp;
2692 struct smack_known *skp;
2693 struct smack_known *ckp = smk_of_current();
2695 char trattr[TRANS_TRUE_SIZE];
2703 isp = inode->i_security;
2705 mutex_lock(&isp->smk_lock);
2707 * If the inode is already instantiated
2708 * take the quick way out
2710 if (isp->smk_flags & SMK_INODE_INSTANT)
2714 sbsp = sbp->s_security;
2716 * We're going to use the superblock default label
2717 * if there's no label on the file.
2719 final = sbsp->smk_default;
2722 * If this is the root inode the superblock
2723 * may be in the process of initialization.
2724 * If that is the case use the root value out
2725 * of the superblock.
2727 if (opt_dentry->d_parent == opt_dentry) {
2728 isp->smk_inode = sbsp->smk_root;
2729 isp->smk_flags |= SMK_INODE_INSTANT;
2734 * This is pretty hackish.
2735 * Casey says that we shouldn't have to do
2736 * file system specific code, but it does help
2737 * with keeping it simple.
2739 switch (sbp->s_magic) {
2742 * Casey says that it's a little embarrassing
2743 * that the smack file system doesn't do
2744 * extended attributes.
2746 final = smack_known_star.smk_known;
2750 * Casey says pipes are easy (?)
2752 final = smack_known_star.smk_known;
2754 case DEVPTS_SUPER_MAGIC:
2756 * devpts seems content with the label of the task.
2757 * Programs that change smack have to treat the
2760 final = ckp->smk_known;
2764 * Socket access is controlled by the socket
2765 * structures associated with the task involved.
2767 final = smack_known_star.smk_known;
2769 case PROC_SUPER_MAGIC:
2771 * Casey says procfs appears not to care.
2772 * The superblock default suffices.
2777 * Device labels should come from the filesystem,
2778 * but watch out, because they're volitile,
2779 * getting recreated on every reboot.
2781 final = smack_known_star.smk_known;
2785 * If a smack value has been set we want to use it,
2786 * but since tmpfs isn't giving us the opportunity
2787 * to set mount options simulate setting the
2788 * superblock default.
2792 * This isn't an understood special case.
2793 * Get the value from the xattr.
2797 * UNIX domain sockets use lower level socket data.
2799 if (S_ISSOCK(inode->i_mode)) {
2800 final = smack_known_star.smk_known;
2804 * No xattr support means, alas, no SMACK label.
2805 * Use the aforeapplied default.
2806 * It would be curious if the label of the task
2807 * does not match that assigned.
2809 if (inode->i_op->getxattr == NULL)
2812 * Get the dentry for xattr.
2814 dp = dget(opt_dentry);
2815 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2817 final = skp->smk_known;
2820 * Transmuting directory
2822 if (S_ISDIR(inode->i_mode)) {
2824 * If this is a new directory and the label was
2825 * transmuted when the inode was initialized
2826 * set the transmute attribute on the directory
2827 * and mark the inode.
2829 * If there is a transmute attribute on the
2830 * directory mark the inode.
2832 if (isp->smk_flags & SMK_INODE_CHANGED) {
2833 isp->smk_flags &= ~SMK_INODE_CHANGED;
2834 rc = inode->i_op->setxattr(dp,
2835 XATTR_NAME_SMACKTRANSMUTE,
2836 TRANS_TRUE, TRANS_TRUE_SIZE,
2839 rc = inode->i_op->getxattr(dp,
2840 XATTR_NAME_SMACKTRANSMUTE, trattr,
2842 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2843 TRANS_TRUE_SIZE) != 0)
2847 transflag = SMK_INODE_TRANSMUTE;
2849 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2850 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2857 isp->smk_inode = ckp->smk_known;
2859 isp->smk_inode = final;
2861 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2864 mutex_unlock(&isp->smk_lock);
2869 * smack_getprocattr - Smack process attribute access
2870 * @p: the object task
2871 * @name: the name of the attribute in /proc/.../attr
2872 * @value: where to put the result
2874 * Places a copy of the task Smack into value
2876 * Returns the length of the smack label or an error code
2878 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2880 struct smack_known *skp = smk_of_task(task_security(p));
2884 if (strcmp(name, "current") != 0)
2887 cp = kstrdup(skp->smk_known, GFP_KERNEL);
2897 * smack_setprocattr - Smack process attribute setting
2898 * @p: the object task
2899 * @name: the name of the attribute in /proc/.../attr
2900 * @value: the value to set
2901 * @size: the size of the value
2903 * Sets the Smack value of the task. Only setting self
2904 * is permitted and only with privilege
2906 * Returns the length of the smack label or an error code
2908 static int smack_setprocattr(struct task_struct *p, char *name,
2909 void *value, size_t size)
2911 struct task_smack *tsp;
2913 struct smack_known *skp;
2916 * Changing another process' Smack value is too dangerous
2917 * and supports no sane use case.
2922 if (!smack_privileged(CAP_MAC_ADMIN))
2925 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
2928 if (strcmp(name, "current") != 0)
2931 skp = smk_import_entry(value, size);
2936 * No process is ever allowed the web ("@") label.
2938 if (skp == &smack_known_web)
2941 new = prepare_creds();
2945 tsp = new->security;
2946 tsp->smk_task = skp;
2953 * smack_unix_stream_connect - Smack access on UDS
2955 * @other: the other sock
2958 * Return 0 if a subject with the smack of sock could access
2959 * an object with the smack of other, otherwise an error code
2961 static int smack_unix_stream_connect(struct sock *sock,
2962 struct sock *other, struct sock *newsk)
2964 struct smack_known *skp;
2965 struct socket_smack *ssp = sock->sk_security;
2966 struct socket_smack *osp = other->sk_security;
2967 struct socket_smack *nsp = newsk->sk_security;
2968 struct smk_audit_info ad;
2972 struct lsm_network_audit net;
2974 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2975 smk_ad_setfield_u_net_sk(&ad, other);
2978 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
2980 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
2984 * Cross reference the peer labels for SO_PEERSEC.
2987 nsp->smk_packet = ssp->smk_out->smk_known;
2988 ssp->smk_packet = osp->smk_out->smk_known;
2995 * smack_unix_may_send - Smack access on UDS
2997 * @other: the other socket
2999 * Return 0 if a subject with the smack of sock could access
3000 * an object with the smack of other, otherwise an error code
3002 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3004 struct socket_smack *ssp = sock->sk->sk_security;
3005 struct socket_smack *osp = other->sk->sk_security;
3006 struct smack_known *skp;
3007 struct smk_audit_info ad;
3010 struct lsm_network_audit net;
3012 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3013 smk_ad_setfield_u_net_sk(&ad, other->sk);
3016 if (smack_privileged(CAP_MAC_OVERRIDE))
3020 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3024 * smack_socket_sendmsg - Smack check based on destination host
3027 * @size: the size of the message
3029 * Return 0 if the current subject can write to the destination host.
3030 * For IPv4 this is only a question if the destination is a single label host.
3031 * For IPv6 this is a check against the label of the port.
3033 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3036 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3037 struct sockaddr *sap = (struct sockaddr *) msg->msg_name;
3041 * Perfectly reasonable for this to be NULL
3046 switch (sip->sin_family) {
3048 rc = smack_netlabel_send(sock->sk, sip);
3051 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3058 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3059 * @sap: netlabel secattr
3060 * @ssp: socket security information
3062 * Returns a pointer to a Smack label entry found on the label list.
3064 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3065 struct socket_smack *ssp)
3067 struct smack_known *skp;
3070 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3072 * Looks like a CIPSO packet.
3073 * If there are flags but no level netlabel isn't
3074 * behaving the way we expect it to.
3076 * Look it up in the label table
3077 * Without guidance regarding the smack value
3078 * for the packet fall back on the network
3082 list_for_each_entry(skp, &smack_known_list, list) {
3083 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3085 if (memcmp(sap->attr.mls.cat,
3086 skp->smk_netlabel.attr.mls.cat,
3097 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
3098 return &smack_known_web;
3099 return &smack_known_star;
3101 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3103 * Looks like a fallback, which gives us a secid.
3105 skp = smack_from_secid(sap->attr.secid);
3107 * This has got to be a bug because it is
3108 * impossible to specify a fallback without
3109 * specifying the label, which will ensure
3110 * it has a secid, and the only way to get a
3111 * secid is from a fallback.
3113 BUG_ON(skp == NULL);
3117 * Without guidance regarding the smack value
3118 * for the packet fall back on the network
3121 return smack_net_ambient;
3124 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr *sap)
3126 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
3129 int proto = -EINVAL;
3130 struct ipv6hdr _ipv6h;
3131 struct ipv6hdr *ip6;
3133 struct tcphdr _tcph, *th;
3134 struct udphdr _udph, *uh;
3135 struct dccp_hdr _dccph, *dh;
3139 offset = skb_network_offset(skb);
3140 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3143 sip->sin6_addr = ip6->saddr;
3145 nexthdr = ip6->nexthdr;
3146 offset += sizeof(_ipv6h);
3147 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3154 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3156 sip->sin6_port = th->source;
3159 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3161 sip->sin6_port = uh->source;
3164 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3166 sip->sin6_port = dh->dccph_sport;
3173 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3177 * Returns 0 if the packet should be delivered, an error code otherwise
3179 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3181 struct netlbl_lsm_secattr secattr;
3182 struct socket_smack *ssp = sk->sk_security;
3183 struct smack_known *skp;
3184 struct sockaddr sadd;
3186 struct smk_audit_info ad;
3188 struct lsm_network_audit net;
3190 switch (sk->sk_family) {
3193 * Translate what netlabel gave us.
3195 netlbl_secattr_init(&secattr);
3197 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3199 skp = smack_from_secattr(&secattr, ssp);
3201 skp = smack_net_ambient;
3203 netlbl_secattr_destroy(&secattr);
3206 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3207 ad.a.u.net->family = sk->sk_family;
3208 ad.a.u.net->netif = skb->skb_iif;
3209 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3212 * Receiving a packet requires that the other end
3213 * be able to write here. Read access is not required.
3214 * This is the simplist possible security model
3217 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3219 netlbl_skbuff_err(skb, rc, 0);
3222 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3223 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3224 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3233 * smack_socket_getpeersec_stream - pull in packet label
3235 * @optval: user's destination
3236 * @optlen: size thereof
3239 * returns zero on success, an error code otherwise
3241 static int smack_socket_getpeersec_stream(struct socket *sock,
3242 char __user *optval,
3243 int __user *optlen, unsigned len)
3245 struct socket_smack *ssp;
3250 ssp = sock->sk->sk_security;
3251 if (ssp->smk_packet != NULL) {
3252 rcp = ssp->smk_packet;
3253 slen = strlen(rcp) + 1;
3258 else if (copy_to_user(optval, rcp, slen) != 0)
3261 if (put_user(slen, optlen) != 0)
3269 * smack_socket_getpeersec_dgram - pull in packet label
3270 * @sock: the peer socket
3272 * @secid: pointer to where to put the secid of the packet
3274 * Sets the netlabel socket state on sk from parent
3276 static int smack_socket_getpeersec_dgram(struct socket *sock,
3277 struct sk_buff *skb, u32 *secid)
3280 struct netlbl_lsm_secattr secattr;
3281 struct socket_smack *ssp = NULL;
3282 struct smack_known *skp;
3283 int family = PF_UNSPEC;
3284 u32 s = 0; /* 0 is the invalid secid */
3288 if (skb->protocol == htons(ETH_P_IP))
3290 else if (skb->protocol == htons(ETH_P_IPV6))
3293 if (family == PF_UNSPEC && sock != NULL)
3294 family = sock->sk->sk_family;
3296 if (family == PF_UNIX) {
3297 ssp = sock->sk->sk_security;
3298 s = ssp->smk_out->smk_secid;
3299 } else if (family == PF_INET || family == PF_INET6) {
3301 * Translate what netlabel gave us.
3303 if (sock != NULL && sock->sk != NULL)
3304 ssp = sock->sk->sk_security;
3305 netlbl_secattr_init(&secattr);
3306 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3308 skp = smack_from_secattr(&secattr, ssp);
3311 netlbl_secattr_destroy(&secattr);
3320 * smack_sock_graft - Initialize a newly created socket with an existing sock
3322 * @parent: parent socket
3324 * Set the smk_{in,out} state of an existing sock based on the process that
3325 * is creating the new socket.
3327 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3329 struct socket_smack *ssp;
3330 struct smack_known *skp = smk_of_current();
3333 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3336 ssp = sk->sk_security;
3337 ssp->smk_in = skp->smk_known;
3339 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3343 * smack_inet_conn_request - Smack access check on connect
3344 * @sk: socket involved
3348 * Returns 0 if a task with the packet label could write to
3349 * the socket, otherwise an error code
3351 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3352 struct request_sock *req)
3354 u16 family = sk->sk_family;
3355 struct smack_known *skp;
3356 struct socket_smack *ssp = sk->sk_security;
3357 struct netlbl_lsm_secattr secattr;
3358 struct sockaddr_in addr;
3362 struct smk_audit_info ad;
3364 struct lsm_network_audit net;
3367 if (family == PF_INET6) {
3369 * Handle mapped IPv4 packets arriving
3370 * via IPv6 sockets. Don't set up netlabel
3371 * processing on IPv6.
3373 if (skb->protocol == htons(ETH_P_IP))
3379 netlbl_secattr_init(&secattr);
3380 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3382 skp = smack_from_secattr(&secattr, ssp);
3384 skp = &smack_known_huh;
3385 netlbl_secattr_destroy(&secattr);
3388 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3389 ad.a.u.net->family = family;
3390 ad.a.u.net->netif = skb->skb_iif;
3391 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3394 * Receiving a packet requires that the other end be able to write
3395 * here. Read access is not required.
3397 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3402 * Save the peer's label in the request_sock so we can later setup
3403 * smk_packet in the child socket so that SO_PEERCRED can report it.
3405 req->peer_secid = skp->smk_secid;
3408 * We need to decide if we want to label the incoming connection here
3409 * if we do we only need to label the request_sock and the stack will
3410 * propagate the wire-label to the sock when it is created.
3413 addr.sin_addr.s_addr = hdr->saddr;
3415 hsp = smack_host_label(&addr);
3419 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3421 netlbl_req_delattr(req);
3427 * smack_inet_csk_clone - Copy the connection information to the new socket
3428 * @sk: the new socket
3429 * @req: the connection's request_sock
3431 * Transfer the connection's peer label to the newly created socket.
3433 static void smack_inet_csk_clone(struct sock *sk,
3434 const struct request_sock *req)
3436 struct socket_smack *ssp = sk->sk_security;
3437 struct smack_known *skp;
3439 if (req->peer_secid != 0) {
3440 skp = smack_from_secid(req->peer_secid);
3441 ssp->smk_packet = skp->smk_known;
3443 ssp->smk_packet = NULL;
3447 * Key management security hooks
3449 * Casey has not tested key support very heavily.
3450 * The permission check is most likely too restrictive.
3451 * If you care about keys please have a look.
3456 * smack_key_alloc - Set the key security blob
3458 * @cred: the credentials to use
3461 * No allocation required
3465 static int smack_key_alloc(struct key *key, const struct cred *cred,
3466 unsigned long flags)
3468 struct smack_known *skp = smk_of_task(cred->security);
3470 key->security = skp->smk_known;
3475 * smack_key_free - Clear the key security blob
3478 * Clear the blob pointer
3480 static void smack_key_free(struct key *key)
3482 key->security = NULL;
3486 * smack_key_permission - Smack access on a key
3487 * @key_ref: gets to the object
3488 * @cred: the credentials to use
3491 * Return 0 if the task has read and write to the object,
3492 * an error code otherwise
3494 static int smack_key_permission(key_ref_t key_ref,
3495 const struct cred *cred, key_perm_t perm)
3498 struct smk_audit_info ad;
3499 struct smack_known *tkp = smk_of_task(cred->security);
3501 keyp = key_ref_to_ptr(key_ref);
3505 * If the key hasn't been initialized give it access so that
3508 if (keyp->security == NULL)
3511 * This should not occur
3516 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3517 ad.a.u.key_struct.key = keyp->serial;
3518 ad.a.u.key_struct.key_desc = keyp->description;
3520 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
3522 #endif /* CONFIG_KEYS */
3527 * Audit requires a unique representation of each Smack specific
3528 * rule. This unique representation is used to distinguish the
3529 * object to be audited from remaining kernel objects and also
3530 * works as a glue between the audit hooks.
3532 * Since repository entries are added but never deleted, we'll use
3533 * the smack_known label address related to the given audit rule as
3534 * the needed unique representation. This also better fits the smack
3535 * model where nearly everything is a label.
3540 * smack_audit_rule_init - Initialize a smack audit rule
3541 * @field: audit rule fields given from user-space (audit.h)
3542 * @op: required testing operator (=, !=, >, <, ...)
3543 * @rulestr: smack label to be audited
3544 * @vrule: pointer to save our own audit rule representation
3546 * Prepare to audit cases where (@field @op @rulestr) is true.
3547 * The label to be audited is created if necessay.
3549 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3551 char **rule = (char **)vrule;
3554 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3557 if (op != Audit_equal && op != Audit_not_equal)
3560 *rule = smk_import(rulestr, 0);
3566 * smack_audit_rule_known - Distinguish Smack audit rules
3567 * @krule: rule of interest, in Audit kernel representation format
3569 * This is used to filter Smack rules from remaining Audit ones.
3570 * If it's proved that this rule belongs to us, the
3571 * audit_rule_match hook will be called to do the final judgement.
3573 static int smack_audit_rule_known(struct audit_krule *krule)
3575 struct audit_field *f;
3578 for (i = 0; i < krule->field_count; i++) {
3579 f = &krule->fields[i];
3581 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3589 * smack_audit_rule_match - Audit given object ?
3590 * @secid: security id for identifying the object to test
3591 * @field: audit rule flags given from user-space
3592 * @op: required testing operator
3593 * @vrule: smack internal rule presentation
3594 * @actx: audit context associated with the check
3596 * The core Audit hook. It's used to take the decision of
3597 * whether to audit or not to audit a given object.
3599 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3600 struct audit_context *actx)
3602 struct smack_known *skp;
3606 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
3607 "Smack: missing rule\n");
3611 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3614 skp = smack_from_secid(secid);
3617 * No need to do string comparisons. If a match occurs,
3618 * both pointers will point to the same smack_known
3621 if (op == Audit_equal)
3622 return (rule == skp->smk_known);
3623 if (op == Audit_not_equal)
3624 return (rule != skp->smk_known);
3630 * smack_audit_rule_free - free smack rule representation
3631 * @vrule: rule to be freed.
3633 * No memory was allocated.
3635 static void smack_audit_rule_free(void *vrule)
3640 #endif /* CONFIG_AUDIT */
3643 * smack_secid_to_secctx - return the smack label for a secid
3644 * @secid: incoming integer
3645 * @secdata: destination
3646 * @seclen: how long it is
3648 * Exists for networking code.
3650 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3652 struct smack_known *skp = smack_from_secid(secid);
3655 *secdata = skp->smk_known;
3656 *seclen = strlen(skp->smk_known);
3661 * smack_secctx_to_secid - return the secid for a smack label
3662 * @secdata: smack label
3663 * @seclen: how long result is
3664 * @secid: outgoing integer
3666 * Exists for audit and networking code.
3668 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3670 *secid = smack_to_secid(secdata);
3675 * smack_release_secctx - don't do anything.
3679 * Exists to make sure nothing gets done, and properly
3681 static void smack_release_secctx(char *secdata, u32 seclen)
3685 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3687 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3690 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3692 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3695 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3698 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3706 struct security_operations smack_ops = {
3709 .ptrace_access_check = smack_ptrace_access_check,
3710 .ptrace_traceme = smack_ptrace_traceme,
3711 .syslog = smack_syslog,
3713 .sb_alloc_security = smack_sb_alloc_security,
3714 .sb_free_security = smack_sb_free_security,
3715 .sb_copy_data = smack_sb_copy_data,
3716 .sb_kern_mount = smack_sb_kern_mount,
3717 .sb_statfs = smack_sb_statfs,
3718 .sb_mount = smack_sb_mount,
3719 .sb_umount = smack_sb_umount,
3721 .bprm_set_creds = smack_bprm_set_creds,
3722 .bprm_committing_creds = smack_bprm_committing_creds,
3723 .bprm_secureexec = smack_bprm_secureexec,
3725 .inode_alloc_security = smack_inode_alloc_security,
3726 .inode_free_security = smack_inode_free_security,
3727 .inode_init_security = smack_inode_init_security,
3728 .inode_link = smack_inode_link,
3729 .inode_unlink = smack_inode_unlink,
3730 .inode_rmdir = smack_inode_rmdir,
3731 .inode_rename = smack_inode_rename,
3732 .inode_permission = smack_inode_permission,
3733 .inode_setattr = smack_inode_setattr,
3734 .inode_getattr = smack_inode_getattr,
3735 .inode_setxattr = smack_inode_setxattr,
3736 .inode_post_setxattr = smack_inode_post_setxattr,
3737 .inode_getxattr = smack_inode_getxattr,
3738 .inode_removexattr = smack_inode_removexattr,
3739 .inode_getsecurity = smack_inode_getsecurity,
3740 .inode_setsecurity = smack_inode_setsecurity,
3741 .inode_listsecurity = smack_inode_listsecurity,
3742 .inode_getsecid = smack_inode_getsecid,
3744 .file_permission = smack_file_permission,
3745 .file_alloc_security = smack_file_alloc_security,
3746 .file_free_security = smack_file_free_security,
3747 .file_ioctl = smack_file_ioctl,
3748 .file_lock = smack_file_lock,
3749 .file_fcntl = smack_file_fcntl,
3750 .mmap_file = smack_mmap_file,
3751 .mmap_addr = cap_mmap_addr,
3752 .file_set_fowner = smack_file_set_fowner,
3753 .file_send_sigiotask = smack_file_send_sigiotask,
3754 .file_receive = smack_file_receive,
3756 .file_open = smack_file_open,
3758 .cred_alloc_blank = smack_cred_alloc_blank,
3759 .cred_free = smack_cred_free,
3760 .cred_prepare = smack_cred_prepare,
3761 .cred_transfer = smack_cred_transfer,
3762 .kernel_act_as = smack_kernel_act_as,
3763 .kernel_create_files_as = smack_kernel_create_files_as,
3764 .task_setpgid = smack_task_setpgid,
3765 .task_getpgid = smack_task_getpgid,
3766 .task_getsid = smack_task_getsid,
3767 .task_getsecid = smack_task_getsecid,
3768 .task_setnice = smack_task_setnice,
3769 .task_setioprio = smack_task_setioprio,
3770 .task_getioprio = smack_task_getioprio,
3771 .task_setscheduler = smack_task_setscheduler,
3772 .task_getscheduler = smack_task_getscheduler,
3773 .task_movememory = smack_task_movememory,
3774 .task_kill = smack_task_kill,
3775 .task_wait = smack_task_wait,
3776 .task_to_inode = smack_task_to_inode,
3778 .ipc_permission = smack_ipc_permission,
3779 .ipc_getsecid = smack_ipc_getsecid,
3781 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3782 .msg_msg_free_security = smack_msg_msg_free_security,
3784 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3785 .msg_queue_free_security = smack_msg_queue_free_security,
3786 .msg_queue_associate = smack_msg_queue_associate,
3787 .msg_queue_msgctl = smack_msg_queue_msgctl,
3788 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3789 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3791 .shm_alloc_security = smack_shm_alloc_security,
3792 .shm_free_security = smack_shm_free_security,
3793 .shm_associate = smack_shm_associate,
3794 .shm_shmctl = smack_shm_shmctl,
3795 .shm_shmat = smack_shm_shmat,
3797 .sem_alloc_security = smack_sem_alloc_security,
3798 .sem_free_security = smack_sem_free_security,
3799 .sem_associate = smack_sem_associate,
3800 .sem_semctl = smack_sem_semctl,
3801 .sem_semop = smack_sem_semop,
3803 .d_instantiate = smack_d_instantiate,
3805 .getprocattr = smack_getprocattr,
3806 .setprocattr = smack_setprocattr,
3808 .unix_stream_connect = smack_unix_stream_connect,
3809 .unix_may_send = smack_unix_may_send,
3811 .socket_post_create = smack_socket_post_create,
3812 .socket_bind = smack_socket_bind,
3813 .socket_connect = smack_socket_connect,
3814 .socket_sendmsg = smack_socket_sendmsg,
3815 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3816 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3817 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3818 .sk_alloc_security = smack_sk_alloc_security,
3819 .sk_free_security = smack_sk_free_security,
3820 .sock_graft = smack_sock_graft,
3821 .inet_conn_request = smack_inet_conn_request,
3822 .inet_csk_clone = smack_inet_csk_clone,
3824 /* key management security hooks */
3826 .key_alloc = smack_key_alloc,
3827 .key_free = smack_key_free,
3828 .key_permission = smack_key_permission,
3829 #endif /* CONFIG_KEYS */
3833 .audit_rule_init = smack_audit_rule_init,
3834 .audit_rule_known = smack_audit_rule_known,
3835 .audit_rule_match = smack_audit_rule_match,
3836 .audit_rule_free = smack_audit_rule_free,
3837 #endif /* CONFIG_AUDIT */
3839 .secid_to_secctx = smack_secid_to_secctx,
3840 .secctx_to_secid = smack_secctx_to_secid,
3841 .release_secctx = smack_release_secctx,
3842 .inode_notifysecctx = smack_inode_notifysecctx,
3843 .inode_setsecctx = smack_inode_setsecctx,
3844 .inode_getsecctx = smack_inode_getsecctx,
3848 static __init void init_smack_known_list(void)
3851 * Initialize rule list locks
3853 mutex_init(&smack_known_huh.smk_rules_lock);
3854 mutex_init(&smack_known_hat.smk_rules_lock);
3855 mutex_init(&smack_known_floor.smk_rules_lock);
3856 mutex_init(&smack_known_star.smk_rules_lock);
3857 mutex_init(&smack_known_invalid.smk_rules_lock);
3858 mutex_init(&smack_known_web.smk_rules_lock);
3860 * Initialize rule lists
3862 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3863 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3864 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3865 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3866 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3867 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3869 * Create the known labels list
3871 list_add(&smack_known_huh.list, &smack_known_list);
3872 list_add(&smack_known_hat.list, &smack_known_list);
3873 list_add(&smack_known_star.list, &smack_known_list);
3874 list_add(&smack_known_floor.list, &smack_known_list);
3875 list_add(&smack_known_invalid.list, &smack_known_list);
3876 list_add(&smack_known_web.list, &smack_known_list);
3880 * smack_init - initialize the smack system
3884 static __init int smack_init(void)
3887 struct task_smack *tsp;
3889 if (!security_module_enable(&smack_ops))
3892 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3897 printk(KERN_INFO "Smack: Initializing.\n");
3900 * Set the security state for the initial task.
3902 cred = (struct cred *) current->cred;
3903 cred->security = tsp;
3905 /* initialize the smack_known_list */
3906 init_smack_known_list();
3911 if (register_security(&smack_ops))
3912 panic("smack: Unable to register with kernel.\n");
3918 * Smack requires early initialization in order to label
3919 * all processes and objects when they are created.
3921 security_initcall(smack_init);