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>
43 #define task_security(task) (task_cred_xxx((task), security))
45 #define TRANS_TRUE "TRUE"
46 #define TRANS_TRUE_SIZE 4
48 #define SMK_CONNECTING 0
49 #define SMK_RECEIVING 1
52 LIST_HEAD(smk_ipv6_port_list);
55 * smk_fetch - Fetch the smack label from a file.
56 * @ip: a pointer to the inode
57 * @dp: a pointer to the dentry
59 * Returns a pointer to the master list entry for the Smack label
60 * or NULL if there was no label to fetch.
62 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
67 struct smack_known *skp = NULL;
69 if (ip->i_op->getxattr == NULL)
72 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
76 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
78 skp = smk_import_entry(buffer, rc);
86 * new_inode_smack - allocate an inode security blob
87 * @smack: a pointer to the Smack label to use in the blob
89 * Returns the new blob or NULL if there's no memory available
91 struct inode_smack *new_inode_smack(char *smack)
93 struct inode_smack *isp;
95 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
99 isp->smk_inode = smack;
101 mutex_init(&isp->smk_lock);
107 * new_task_smack - allocate a task security blob
108 * @smack: a pointer to the Smack label to use in the blob
110 * Returns the new blob or NULL if there's no memory available
112 static struct task_smack *new_task_smack(struct smack_known *task,
113 struct smack_known *forked, gfp_t gfp)
115 struct task_smack *tsp;
117 tsp = kzalloc(sizeof(struct task_smack), gfp);
121 tsp->smk_task = task;
122 tsp->smk_forked = forked;
123 INIT_LIST_HEAD(&tsp->smk_rules);
124 mutex_init(&tsp->smk_rules_lock);
130 * smk_copy_rules - copy a rule set
131 * @nhead - new rules header pointer
132 * @ohead - old rules header pointer
134 * Returns 0 on success, -ENOMEM on error
136 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
139 struct smack_rule *nrp;
140 struct smack_rule *orp;
143 INIT_LIST_HEAD(nhead);
145 list_for_each_entry_rcu(orp, ohead, list) {
146 nrp = kzalloc(sizeof(struct smack_rule), gfp);
152 list_add_rcu(&nrp->list, nhead);
159 * We he, that is fun!
163 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
164 * @ctp: child task pointer
165 * @mode: ptrace attachment mode
167 * Returns 0 if access is OK, an error code otherwise
169 * Do the capability checks, and require read and write.
171 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
174 struct smk_audit_info ad;
175 struct smack_known *skp;
177 rc = cap_ptrace_access_check(ctp, mode);
181 skp = smk_of_task(task_security(ctp));
182 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
183 smk_ad_setfield_u_tsk(&ad, ctp);
185 rc = smk_curacc(skp->smk_known, MAY_READWRITE, &ad);
190 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
191 * @ptp: parent task pointer
193 * Returns 0 if access is OK, an error code otherwise
195 * Do the capability checks, and require read and write.
197 static int smack_ptrace_traceme(struct task_struct *ptp)
200 struct smk_audit_info ad;
201 struct smack_known *skp;
203 rc = cap_ptrace_traceme(ptp);
207 skp = smk_of_task(task_security(ptp));
208 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
209 smk_ad_setfield_u_tsk(&ad, ptp);
211 rc = smk_curacc(skp->smk_known, MAY_READWRITE, &ad);
216 * smack_syslog - Smack approval on syslog
217 * @type: message type
219 * Require that the task has the floor label
221 * Returns 0 on success, error code otherwise.
223 static int smack_syslog(int typefrom_file)
226 struct smack_known *skp = smk_of_current();
228 if (smack_privileged(CAP_MAC_OVERRIDE))
231 if (skp != &smack_known_floor)
243 * smack_sb_alloc_security - allocate a superblock blob
244 * @sb: the superblock getting the blob
246 * Returns 0 on success or -ENOMEM on error.
248 static int smack_sb_alloc_security(struct super_block *sb)
250 struct superblock_smack *sbsp;
252 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
257 sbsp->smk_root = smack_known_floor.smk_known;
258 sbsp->smk_default = smack_known_floor.smk_known;
259 sbsp->smk_floor = smack_known_floor.smk_known;
260 sbsp->smk_hat = smack_known_hat.smk_known;
261 sbsp->smk_initialized = 0;
263 sb->s_security = sbsp;
269 * smack_sb_free_security - free a superblock blob
270 * @sb: the superblock getting the blob
273 static void smack_sb_free_security(struct super_block *sb)
275 kfree(sb->s_security);
276 sb->s_security = NULL;
280 * smack_sb_copy_data - copy mount options data for processing
281 * @orig: where to start
282 * @smackopts: mount options string
284 * Returns 0 on success or -ENOMEM on error.
286 * Copy the Smack specific mount options out of the mount
289 static int smack_sb_copy_data(char *orig, char *smackopts)
291 char *cp, *commap, *otheropts, *dp;
293 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
294 if (otheropts == NULL)
297 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
298 if (strstr(cp, SMK_FSDEFAULT) == cp)
300 else if (strstr(cp, SMK_FSFLOOR) == cp)
302 else if (strstr(cp, SMK_FSHAT) == cp)
304 else if (strstr(cp, SMK_FSROOT) == cp)
309 commap = strchr(cp, ',');
318 strcpy(orig, otheropts);
319 free_page((unsigned long)otheropts);
325 * smack_sb_kern_mount - Smack specific mount processing
326 * @sb: the file system superblock
327 * @flags: the mount flags
328 * @data: the smack mount options
330 * Returns 0 on success, an error code on failure
332 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
334 struct dentry *root = sb->s_root;
335 struct inode *inode = root->d_inode;
336 struct superblock_smack *sp = sb->s_security;
337 struct inode_smack *isp;
342 if (sp->smk_initialized != 0)
345 sp->smk_initialized = 1;
347 for (op = data; op != NULL; op = commap) {
348 commap = strchr(op, ',');
352 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
353 op += strlen(SMK_FSHAT);
354 nsp = smk_import(op, 0);
357 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
358 op += strlen(SMK_FSFLOOR);
359 nsp = smk_import(op, 0);
362 } else if (strncmp(op, SMK_FSDEFAULT,
363 strlen(SMK_FSDEFAULT)) == 0) {
364 op += strlen(SMK_FSDEFAULT);
365 nsp = smk_import(op, 0);
367 sp->smk_default = nsp;
368 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
369 op += strlen(SMK_FSROOT);
370 nsp = smk_import(op, 0);
377 * Initialize the root inode.
379 isp = inode->i_security;
381 inode->i_security = new_inode_smack(sp->smk_root);
383 isp->smk_inode = sp->smk_root;
389 * smack_sb_statfs - Smack check on statfs
390 * @dentry: identifies the file system in question
392 * Returns 0 if current can read the floor of the filesystem,
393 * and error code otherwise
395 static int smack_sb_statfs(struct dentry *dentry)
397 struct superblock_smack *sbp = dentry->d_sb->s_security;
399 struct smk_audit_info ad;
401 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
402 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
404 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
409 * smack_sb_mount - Smack check for mounting
416 * Returns 0 if current can write the floor of the filesystem
417 * being mounted on, an error code otherwise.
419 static int smack_sb_mount(char *dev_name, struct path *path,
420 char *type, unsigned long flags, void *data)
422 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
423 struct smk_audit_info ad;
425 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
426 smk_ad_setfield_u_fs_path(&ad, *path);
428 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
432 * smack_sb_umount - Smack check for unmounting
433 * @mnt: file system to unmount
436 * Returns 0 if current can write the floor of the filesystem
437 * being unmounted, an error code otherwise.
439 static int smack_sb_umount(struct vfsmount *mnt, int flags)
441 struct superblock_smack *sbp;
442 struct smk_audit_info ad;
445 path.dentry = mnt->mnt_root;
448 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
449 smk_ad_setfield_u_fs_path(&ad, path);
451 sbp = mnt->mnt_sb->s_security;
452 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
460 * smack_bprm_set_creds - set creds for exec
461 * @bprm: the exec information
463 * Returns 0 if it gets a blob, -ENOMEM otherwise
465 static int smack_bprm_set_creds(struct linux_binprm *bprm)
467 struct inode *inode = bprm->file->f_path.dentry->d_inode;
468 struct task_smack *bsp = bprm->cred->security;
469 struct inode_smack *isp;
472 rc = cap_bprm_set_creds(bprm);
476 if (bprm->cred_prepared)
479 isp = inode->i_security;
480 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
486 bsp->smk_task = isp->smk_task;
487 bprm->per_clear |= PER_CLEAR_ON_SETID;
493 * smack_bprm_committing_creds - Prepare to install the new credentials
496 * @bprm: binprm for exec
498 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
500 struct task_smack *bsp = bprm->cred->security;
502 if (bsp->smk_task != bsp->smk_forked)
503 current->pdeath_signal = 0;
507 * smack_bprm_secureexec - Return the decision to use secureexec.
508 * @bprm: binprm for exec
510 * Returns 0 on success.
512 static int smack_bprm_secureexec(struct linux_binprm *bprm)
514 struct task_smack *tsp = current_security();
515 int ret = cap_bprm_secureexec(bprm);
517 if (!ret && (tsp->smk_task != tsp->smk_forked))
528 * smack_inode_alloc_security - allocate an inode blob
529 * @inode: the inode in need of a blob
531 * Returns 0 if it gets a blob, -ENOMEM otherwise
533 static int smack_inode_alloc_security(struct inode *inode)
535 struct smack_known *skp = smk_of_current();
537 inode->i_security = new_inode_smack(skp->smk_known);
538 if (inode->i_security == NULL)
544 * smack_inode_free_security - free an inode blob
545 * @inode: the inode with a blob
547 * Clears the blob pointer in inode
549 static void smack_inode_free_security(struct inode *inode)
551 kfree(inode->i_security);
552 inode->i_security = NULL;
556 * smack_inode_init_security - copy out the smack from an inode
560 * @name: where to put the attribute name
561 * @value: where to put the attribute value
562 * @len: where to put the length of the attribute
564 * Returns 0 if it all works out, -ENOMEM if there's no memory
566 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
567 const struct qstr *qstr, char **name,
568 void **value, size_t *len)
570 struct inode_smack *issp = inode->i_security;
571 struct smack_known *skp = smk_of_current();
572 char *isp = smk_of_inode(inode);
573 char *dsp = smk_of_inode(dir);
577 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
584 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
588 * If the access rule allows transmutation and
589 * the directory requests transmutation then
590 * by all means transmute.
591 * Mark the inode as changed.
593 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
594 smk_inode_transmutable(dir)) {
596 issp->smk_flags |= SMK_INODE_CHANGED;
599 *value = kstrdup(isp, GFP_KERNEL);
605 *len = strlen(isp) + 1;
611 * smack_inode_link - Smack check on link
612 * @old_dentry: the existing object
614 * @new_dentry: the new object
616 * Returns 0 if access is permitted, an error code otherwise
618 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
619 struct dentry *new_dentry)
622 struct smk_audit_info ad;
625 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
626 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
628 isp = smk_of_inode(old_dentry->d_inode);
629 rc = smk_curacc(isp, MAY_WRITE, &ad);
631 if (rc == 0 && new_dentry->d_inode != NULL) {
632 isp = smk_of_inode(new_dentry->d_inode);
633 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
634 rc = smk_curacc(isp, MAY_WRITE, &ad);
641 * smack_inode_unlink - Smack check on inode deletion
642 * @dir: containing directory object
643 * @dentry: file to unlink
645 * Returns 0 if current can write the containing directory
646 * and the object, error code otherwise
648 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
650 struct inode *ip = dentry->d_inode;
651 struct smk_audit_info ad;
654 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
655 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
658 * You need write access to the thing you're unlinking
660 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
663 * You also need write access to the containing directory
665 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
666 smk_ad_setfield_u_fs_inode(&ad, dir);
667 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
673 * smack_inode_rmdir - Smack check on directory deletion
674 * @dir: containing directory object
675 * @dentry: directory to unlink
677 * Returns 0 if current can write the containing directory
678 * and the directory, error code otherwise
680 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
682 struct smk_audit_info ad;
685 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
686 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
689 * You need write access to the thing you're removing
691 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
694 * You also need write access to the containing directory
696 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
697 smk_ad_setfield_u_fs_inode(&ad, dir);
698 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
705 * smack_inode_rename - Smack check on rename
706 * @old_inode: the old directory
707 * @old_dentry: unused
708 * @new_inode: the new directory
709 * @new_dentry: unused
711 * Read and write access is required on both the old and
714 * Returns 0 if access is permitted, an error code otherwise
716 static int smack_inode_rename(struct inode *old_inode,
717 struct dentry *old_dentry,
718 struct inode *new_inode,
719 struct dentry *new_dentry)
723 struct smk_audit_info ad;
725 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
726 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
728 isp = smk_of_inode(old_dentry->d_inode);
729 rc = smk_curacc(isp, MAY_READWRITE, &ad);
731 if (rc == 0 && new_dentry->d_inode != NULL) {
732 isp = smk_of_inode(new_dentry->d_inode);
733 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
734 rc = smk_curacc(isp, MAY_READWRITE, &ad);
740 * smack_inode_permission - Smack version of permission()
741 * @inode: the inode in question
742 * @mask: the access requested
743 * @flags: special case
745 * This is the important Smack hook.
747 * Returns 0 if access is permitted, -EACCES otherwise
749 static int smack_inode_permission(struct inode *inode, int mask, unsigned flags)
751 struct smk_audit_info ad;
753 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
755 * No permission to check. Existence test. Yup, it's there.
760 /* May be droppable after audit */
761 if (flags & IPERM_FLAG_RCU)
764 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
765 smk_ad_setfield_u_fs_inode(&ad, inode);
766 return smk_curacc(smk_of_inode(inode), mask, &ad);
770 * smack_inode_setattr - Smack check for setting attributes
771 * @dentry: the object
772 * @iattr: for the force flag
774 * Returns 0 if access is permitted, an error code otherwise
776 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
778 struct smk_audit_info ad;
780 * Need to allow for clearing the setuid bit.
782 if (iattr->ia_valid & ATTR_FORCE)
784 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
785 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
787 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
791 * smack_inode_getattr - Smack check for getting attributes
793 * @dentry: the object
795 * Returns 0 if access is permitted, an error code otherwise
797 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
799 struct smk_audit_info ad;
802 path.dentry = dentry;
805 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
806 smk_ad_setfield_u_fs_path(&ad, path);
807 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
811 * smack_inode_setxattr - Smack check for setting xattrs
812 * @dentry: the object
813 * @name: name of the attribute
818 * This protects the Smack attribute explicitly.
820 * Returns 0 if access is permitted, an error code otherwise
822 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
823 const void *value, size_t size, int flags)
825 struct smk_audit_info ad;
828 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
829 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
830 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
831 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
832 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
833 if (!smack_privileged(CAP_MAC_ADMIN))
836 * check label validity here so import wont fail on
839 if (size == 0 || size >= SMK_LONGLABEL ||
840 smk_import(value, size) == NULL)
842 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
843 if (!smack_privileged(CAP_MAC_ADMIN))
845 if (size != TRANS_TRUE_SIZE ||
846 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
849 rc = cap_inode_setxattr(dentry, name, value, size, flags);
851 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
852 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
855 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
861 * smack_inode_post_setxattr - Apply the Smack update approved above
863 * @name: attribute name
864 * @value: attribute value
865 * @size: attribute size
868 * Set the pointer in the inode blob to the entry found
869 * in the master label list.
871 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
872 const void *value, size_t size, int flags)
874 struct smack_known *skp;
875 struct inode_smack *isp = dentry->d_inode->i_security;
877 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
878 isp->smk_flags |= SMK_INODE_TRANSMUTE;
882 skp = smk_import_entry(value, size);
883 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
885 isp->smk_inode = skp->smk_known;
887 isp->smk_inode = smack_known_invalid.smk_known;
888 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
892 isp->smk_task = &smack_known_invalid;
893 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
897 isp->smk_mmap = &smack_known_invalid;
904 * smack_inode_getxattr - Smack check on getxattr
905 * @dentry: the object
908 * Returns 0 if access is permitted, an error code otherwise
910 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
912 struct smk_audit_info ad;
914 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
915 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
917 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
921 * smack_inode_removexattr - Smack check on removexattr
922 * @dentry: the object
923 * @name: name of the attribute
925 * Removing the Smack attribute requires CAP_MAC_ADMIN
927 * Returns 0 if access is permitted, an error code otherwise
929 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
931 struct inode_smack *isp;
932 struct smk_audit_info ad;
935 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
936 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
937 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
938 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
939 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
940 strcmp(name, XATTR_NAME_SMACKMMAP)) {
941 if (!smack_privileged(CAP_MAC_ADMIN))
944 rc = cap_inode_removexattr(dentry, name);
946 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
947 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
949 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
952 isp = dentry->d_inode->i_security;
953 isp->smk_task = NULL;
954 isp->smk_mmap = NULL;
961 * smack_inode_getsecurity - get smack xattrs
963 * @name: attribute name
964 * @buffer: where to put the result
967 * Returns the size of the attribute or an error code
969 static int smack_inode_getsecurity(const struct inode *inode,
970 const char *name, void **buffer,
973 struct socket_smack *ssp;
975 struct super_block *sbp;
976 struct inode *ip = (struct inode *)inode;
981 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
982 isp = smk_of_inode(inode);
983 ilen = strlen(isp) + 1;
989 * The rest of the Smack xattrs are only on sockets.
992 if (sbp->s_magic != SOCKFS_MAGIC)
996 if (sock == NULL || sock->sk == NULL)
999 ssp = sock->sk->sk_security;
1001 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1003 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1004 isp = ssp->smk_out->smk_known;
1008 ilen = strlen(isp) + 1;
1019 * smack_inode_listsecurity - list the Smack attributes
1020 * @inode: the object
1021 * @buffer: where they go
1022 * @buffer_size: size of buffer
1024 * Returns 0 on success, -EINVAL otherwise
1026 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1029 int len = strlen(XATTR_NAME_SMACK);
1031 if (buffer != NULL && len <= buffer_size) {
1032 memcpy(buffer, XATTR_NAME_SMACK, len);
1039 * smack_inode_getsecid - Extract inode's security id
1040 * @inode: inode to extract the info from
1041 * @secid: where result will be saved
1043 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1045 struct inode_smack *isp = inode->i_security;
1047 *secid = smack_to_secid(isp->smk_inode);
1055 * smack_file_permission - Smack check on file operations
1061 * Should access checks be done on each read or write?
1062 * UNICOS and SELinux say yes.
1063 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1065 * I'll say no for now. Smack does not do the frequent
1066 * label changing that SELinux does.
1068 static int smack_file_permission(struct file *file, int mask)
1074 * smack_file_alloc_security - assign a file security blob
1077 * The security blob for a file is a pointer to the master
1078 * label list, so no allocation is done.
1082 static int smack_file_alloc_security(struct file *file)
1084 struct smack_known *skp = smk_of_current();
1086 file->f_security = skp->smk_known;
1091 * smack_file_free_security - clear a file security blob
1094 * The security blob for a file is a pointer to the master
1095 * label list, so no memory is freed.
1097 static void smack_file_free_security(struct file *file)
1099 file->f_security = NULL;
1103 * smack_file_ioctl - Smack check on ioctls
1108 * Relies heavily on the correct use of the ioctl command conventions.
1110 * Returns 0 if allowed, error code otherwise
1112 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1116 struct smk_audit_info ad;
1118 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1119 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1121 if (_IOC_DIR(cmd) & _IOC_WRITE)
1122 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1124 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1125 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1131 * smack_file_lock - Smack check on file locking
1135 * Returns 0 if current has write access, error code otherwise
1137 static int smack_file_lock(struct file *file, unsigned int cmd)
1139 struct smk_audit_info ad;
1141 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1142 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1143 return smk_curacc(file->f_security, MAY_WRITE, &ad);
1147 * smack_file_fcntl - Smack check on fcntl
1149 * @cmd: what action to check
1152 * Generally these operations are harmless.
1153 * File locking operations present an obvious mechanism
1154 * for passing information, so they require write access.
1156 * Returns 0 if current has access, error code otherwise
1158 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1161 struct smk_audit_info ad;
1171 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1172 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1173 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1184 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1185 * if mapping anonymous memory.
1186 * @file contains the file structure for file to map (may be NULL).
1187 * @reqprot contains the protection requested by the application.
1188 * @prot contains the protection that will be applied by the kernel.
1189 * @flags contains the operational flags.
1190 * Return 0 if permission is granted.
1192 static int smack_file_mmap(struct file *file,
1193 unsigned long reqprot, unsigned long prot,
1194 unsigned long flags, unsigned long addr,
1195 unsigned long addr_only)
1197 struct smack_known *skp;
1198 struct smack_known *mkp;
1199 struct smack_rule *srp;
1200 struct task_smack *tsp;
1202 struct inode_smack *isp;
1209 /* do DAC check on address space usage */
1210 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1211 if (rc || addr_only)
1214 if (file == NULL || file->f_dentry == NULL)
1217 dp = file->f_dentry;
1219 if (dp->d_inode == NULL)
1222 isp = dp->d_inode->i_security;
1223 if (isp->smk_mmap == NULL)
1225 mkp = isp->smk_mmap;
1227 tsp = current_security();
1228 skp = smk_of_current();
1233 * For each Smack rule associated with the subject
1234 * label verify that the SMACK64MMAP also has access
1235 * to that rule's object label.
1237 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1238 osmack = srp->smk_object;
1240 * Matching labels always allows access.
1242 if (mkp->smk_known == osmack)
1245 * If there is a matching local rule take
1246 * that into account as well.
1248 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1251 may = srp->smk_access;
1253 may &= srp->smk_access;
1255 * If may is zero the SMACK64MMAP subject can't
1256 * possibly have less access.
1262 * Fetch the global list entry.
1263 * If there isn't one a SMACK64MMAP subject
1264 * can't have as much access as current.
1266 mmay = smk_access_entry(mkp->smk_known, osmack,
1268 if (mmay == -ENOENT) {
1273 * If there is a local entry it modifies the
1274 * potential access, too.
1276 tmay = smk_access_entry(mkp->smk_known, osmack,
1278 if (tmay != -ENOENT)
1282 * If there is any access available to current that is
1283 * not available to a SMACK64MMAP subject
1286 if ((may | mmay) != mmay) {
1298 * smack_file_set_fowner - set the file security blob value
1299 * @file: object in question
1302 * Further research may be required on this one.
1304 static int smack_file_set_fowner(struct file *file)
1306 struct smack_known *skp = smk_of_current();
1308 file->f_security = skp->smk_known;
1313 * smack_file_send_sigiotask - Smack on sigio
1314 * @tsk: The target task
1315 * @fown: the object the signal come from
1318 * Allow a privileged task to get signals even if it shouldn't
1320 * Returns 0 if a subject with the object's smack could
1321 * write to the task, an error code otherwise.
1323 static int smack_file_send_sigiotask(struct task_struct *tsk,
1324 struct fown_struct *fown, int signum)
1326 struct smack_known *skp;
1327 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1330 struct smk_audit_info ad;
1333 * struct fown_struct is never outside the context of a struct file
1335 file = container_of(fown, struct file, f_owner);
1337 /* we don't log here as rc can be overriden */
1338 skp = smk_find_entry(file->f_security);
1339 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1340 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1343 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1344 smk_ad_setfield_u_tsk(&ad, tsk);
1345 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1350 * smack_file_receive - Smack file receive check
1353 * Returns 0 if current has access, error code otherwise
1355 static int smack_file_receive(struct file *file)
1358 struct smk_audit_info ad;
1360 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1361 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1363 * This code relies on bitmasks.
1365 if (file->f_mode & FMODE_READ)
1367 if (file->f_mode & FMODE_WRITE)
1370 return smk_curacc(file->f_security, may, &ad);
1374 * smack_dentry_open - Smack dentry open processing
1378 * Set the security blob in the file structure.
1382 static int smack_dentry_open(struct file *file, const struct cred *cred)
1384 struct inode_smack *isp = file->f_path.dentry->d_inode->i_security;
1386 file->f_security = isp->smk_inode;
1396 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1397 * @new: the new credentials
1398 * @gfp: the atomicity of any memory allocations
1400 * Prepare a blank set of credentials for modification. This must allocate all
1401 * the memory the LSM module might require such that cred_transfer() can
1402 * complete without error.
1404 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1406 struct task_smack *tsp;
1408 tsp = new_task_smack(NULL, NULL, gfp);
1412 cred->security = tsp;
1419 * smack_cred_free - "free" task-level security credentials
1420 * @cred: the credentials in question
1423 static void smack_cred_free(struct cred *cred)
1425 struct task_smack *tsp = cred->security;
1426 struct smack_rule *rp;
1427 struct list_head *l;
1428 struct list_head *n;
1432 cred->security = NULL;
1434 list_for_each_safe(l, n, &tsp->smk_rules) {
1435 rp = list_entry(l, struct smack_rule, list);
1436 list_del(&rp->list);
1443 * smack_cred_prepare - prepare new set of credentials for modification
1444 * @new: the new credentials
1445 * @old: the original credentials
1446 * @gfp: the atomicity of any memory allocations
1448 * Prepare a new set of credentials for modification.
1450 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1453 struct task_smack *old_tsp = old->security;
1454 struct task_smack *new_tsp;
1457 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1458 if (new_tsp == NULL)
1461 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1465 new->security = new_tsp;
1470 * smack_cred_transfer - Transfer the old credentials to the new credentials
1471 * @new: the new credentials
1472 * @old: the original credentials
1474 * Fill in a set of blank credentials from another set of credentials.
1476 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1478 struct task_smack *old_tsp = old->security;
1479 struct task_smack *new_tsp = new->security;
1481 new_tsp->smk_task = old_tsp->smk_task;
1482 new_tsp->smk_forked = old_tsp->smk_task;
1483 mutex_init(&new_tsp->smk_rules_lock);
1484 INIT_LIST_HEAD(&new_tsp->smk_rules);
1487 /* cbs copy rule list */
1491 * smack_kernel_act_as - Set the subjective context in a set of credentials
1492 * @new: points to the set of credentials to be modified.
1493 * @secid: specifies the security ID to be set
1495 * Set the security data for a kernel service.
1497 static int smack_kernel_act_as(struct cred *new, u32 secid)
1499 struct task_smack *new_tsp = new->security;
1500 struct smack_known *skp = smack_from_secid(secid);
1505 new_tsp->smk_task = skp;
1510 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1511 * @new: points to the set of credentials to be modified
1512 * @inode: points to the inode to use as a reference
1514 * Set the file creation context in a set of credentials to the same
1515 * as the objective context of the specified inode
1517 static int smack_kernel_create_files_as(struct cred *new,
1518 struct inode *inode)
1520 struct inode_smack *isp = inode->i_security;
1521 struct task_smack *tsp = new->security;
1523 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1524 tsp->smk_task = tsp->smk_forked;
1529 * smk_curacc_on_task - helper to log task related access
1530 * @p: the task object
1531 * @access: the access requested
1532 * @caller: name of the calling function for audit
1534 * Return 0 if access is permitted
1536 static int smk_curacc_on_task(struct task_struct *p, int access,
1539 struct smk_audit_info ad;
1540 struct smack_known *skp = smk_of_task(task_security(p));
1542 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1543 smk_ad_setfield_u_tsk(&ad, p);
1544 return smk_curacc(skp->smk_known, access, &ad);
1548 * smack_task_setpgid - Smack check on setting pgid
1549 * @p: the task object
1552 * Return 0 if write access is permitted
1554 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1556 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1560 * smack_task_getpgid - Smack access check for getpgid
1561 * @p: the object task
1563 * Returns 0 if current can read the object task, error code otherwise
1565 static int smack_task_getpgid(struct task_struct *p)
1567 return smk_curacc_on_task(p, MAY_READ, __func__);
1571 * smack_task_getsid - Smack access check for getsid
1572 * @p: the object task
1574 * Returns 0 if current can read the object task, error code otherwise
1576 static int smack_task_getsid(struct task_struct *p)
1578 return smk_curacc_on_task(p, MAY_READ, __func__);
1582 * smack_task_getsecid - get the secid of the task
1583 * @p: the object task
1584 * @secid: where to put the result
1586 * Sets the secid to contain a u32 version of the smack label.
1588 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1590 struct smack_known *skp = smk_of_task(task_security(p));
1592 *secid = skp->smk_secid;
1596 * smack_task_setnice - Smack check on setting nice
1597 * @p: the task object
1600 * Return 0 if write access is permitted
1602 static int smack_task_setnice(struct task_struct *p, int nice)
1606 rc = cap_task_setnice(p, nice);
1608 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1613 * smack_task_setioprio - Smack check on setting ioprio
1614 * @p: the task object
1617 * Return 0 if write access is permitted
1619 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1623 rc = cap_task_setioprio(p, ioprio);
1625 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1630 * smack_task_getioprio - Smack check on reading ioprio
1631 * @p: the task object
1633 * Return 0 if read access is permitted
1635 static int smack_task_getioprio(struct task_struct *p)
1637 return smk_curacc_on_task(p, MAY_READ, __func__);
1641 * smack_task_setscheduler - Smack check on setting scheduler
1642 * @p: the task object
1646 * Return 0 if read access is permitted
1648 static int smack_task_setscheduler(struct task_struct *p)
1652 rc = cap_task_setscheduler(p);
1654 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1659 * smack_task_getscheduler - Smack check on reading scheduler
1660 * @p: the task object
1662 * Return 0 if read access is permitted
1664 static int smack_task_getscheduler(struct task_struct *p)
1666 return smk_curacc_on_task(p, MAY_READ, __func__);
1670 * smack_task_movememory - Smack check on moving memory
1671 * @p: the task object
1673 * Return 0 if write access is permitted
1675 static int smack_task_movememory(struct task_struct *p)
1677 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1681 * smack_task_kill - Smack check on signal delivery
1682 * @p: the task object
1685 * @secid: identifies the smack to use in lieu of current's
1687 * Return 0 if write access is permitted
1689 * The secid behavior is an artifact of an SELinux hack
1690 * in the USB code. Someday it may go away.
1692 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1695 struct smk_audit_info ad;
1696 struct smack_known *skp;
1697 struct smack_known *tkp = smk_of_task(task_security(p));
1699 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1700 smk_ad_setfield_u_tsk(&ad, p);
1702 * Sending a signal requires that the sender
1703 * can write the receiver.
1706 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
1708 * If the secid isn't 0 we're dealing with some USB IO
1709 * specific behavior. This is not clean. For one thing
1710 * we can't take privilege into account.
1712 skp = smack_from_secid(secid);
1713 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
1717 * smack_task_wait - Smack access check for waiting
1718 * @p: task to wait for
1722 static int smack_task_wait(struct task_struct *p)
1725 * Allow the operation to succeed.
1727 * In userless environments (e.g. phones) programs
1728 * get marked with SMACK64EXEC and even if the parent
1729 * and child shouldn't be talking the parent still
1730 * may expect to know when the child exits.
1736 * smack_task_to_inode - copy task smack into the inode blob
1737 * @p: task to copy from
1738 * @inode: inode to copy to
1740 * Sets the smack pointer in the inode security blob
1742 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1744 struct inode_smack *isp = inode->i_security;
1745 struct smack_known *skp = smk_of_task(task_security(p));
1747 isp->smk_inode = skp->smk_known;
1755 * smack_sk_alloc_security - Allocate a socket blob
1758 * @gfp_flags: memory allocation flags
1760 * Assign Smack pointers to current
1762 * Returns 0 on success, -ENOMEM is there's no memory
1764 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1766 struct smack_known *skp = smk_of_current();
1767 struct socket_smack *ssp;
1769 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1773 ssp->smk_in = skp->smk_known;
1775 ssp->smk_packet = NULL;
1777 sk->sk_security = ssp;
1783 * smack_sk_free_security - Free a socket blob
1786 * Clears the blob pointer
1788 static void smack_sk_free_security(struct sock *sk)
1790 kfree(sk->sk_security);
1794 * smack_host_label - check host based restrictions
1795 * @sip: the object end
1797 * looks for host based access restrictions
1799 * This version will only be appropriate for really small sets of single label
1800 * hosts. The caller is responsible for ensuring that the RCU read lock is
1801 * taken before calling this function.
1803 * Returns the label of the far end or NULL if it's not special.
1805 static char *smack_host_label(struct sockaddr_in *sip)
1807 struct smk_netlbladdr *snp;
1808 struct in_addr *siap = &sip->sin_addr;
1810 if (siap->s_addr == 0)
1813 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1815 * we break after finding the first match because
1816 * the list is sorted from longest to shortest mask
1817 * so we have found the most specific match
1819 if ((&snp->smk_host.sin_addr)->s_addr ==
1820 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1821 /* we have found the special CIPSO option */
1822 if (snp->smk_label == smack_cipso_option)
1824 return snp->smk_label;
1831 * smack_netlabel - Set the secattr on a socket
1833 * @labeled: socket label scheme
1835 * Convert the outbound smack value (smk_out) to a
1836 * secattr and attach it to the socket.
1838 * Returns 0 on success or an error code
1840 static int smack_netlabel(struct sock *sk, int labeled)
1842 struct smack_known *skp;
1843 struct socket_smack *ssp = sk->sk_security;
1847 * Usually the netlabel code will handle changing the
1848 * packet labeling based on the label.
1849 * The case of a single label host is different, because
1850 * a single label host should never get a labeled packet
1851 * even though the label is usually associated with a packet
1855 bh_lock_sock_nested(sk);
1857 if (ssp->smk_out == smack_net_ambient ||
1858 labeled == SMACK_UNLABELED_SOCKET)
1859 netlbl_sock_delattr(sk);
1862 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
1872 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1874 * @sap: the destination address
1876 * Set the correct secattr for the given socket based on the destination
1877 * address and perform any outbound access checks needed.
1879 * Returns 0 on success or an error code.
1882 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1884 struct smack_known *skp;
1888 struct socket_smack *ssp = sk->sk_security;
1889 struct smk_audit_info ad;
1892 hostsp = smack_host_label(sap);
1893 if (hostsp != NULL) {
1894 sk_lbl = SMACK_UNLABELED_SOCKET;
1896 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1897 ad.a.u.net.family = sap->sin_family;
1898 ad.a.u.net.dport = sap->sin_port;
1899 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1902 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
1904 sk_lbl = SMACK_CIPSO_SOCKET;
1911 return smack_netlabel(sk, sk_lbl);
1915 * smk_ipv6_port_label - Smack port access table management
1919 * Create or update the port list entry
1921 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1923 struct sock *sk = sock->sk;
1924 struct sockaddr_in6 *addr6;
1925 struct socket_smack *ssp = sock->sk->sk_security;
1926 struct smk_port_label *spp;
1927 unsigned short port = 0;
1929 if (address == NULL) {
1931 * This operation is changing the Smack information
1932 * on the bound socket. Take the changes to the port
1935 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1936 if (sk != spp->smk_sock)
1938 spp->smk_in = ssp->smk_in;
1939 spp->smk_out = ssp->smk_out;
1943 * A NULL address is only used for updating existing
1944 * bound entries. If there isn't one, it's OK.
1949 addr6 = (struct sockaddr_in6 *)address;
1950 port = ntohs(addr6->sin6_port);
1952 * This is a special case that is safely ignored.
1958 * Look for an existing port list entry.
1959 * This is an indication that a port is getting reused.
1961 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1962 if (spp->smk_port != port)
1964 spp->smk_port = port;
1966 spp->smk_in = ssp->smk_in;
1967 spp->smk_out = ssp->smk_out;
1972 * A new port entry is required.
1974 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1978 spp->smk_port = port;
1980 spp->smk_in = ssp->smk_in;
1981 spp->smk_out = ssp->smk_out;
1983 list_add(&spp->list, &smk_ipv6_port_list);
1988 * smk_ipv6_port_check - check Smack port access
1992 * Create or update the port list entry
1994 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr *address,
1999 struct sockaddr_in6 *addr6;
2000 struct smk_port_label *spp;
2001 struct socket_smack *ssp = sk->sk_security;
2002 struct smack_known *skp;
2003 unsigned short port = 0;
2005 struct smk_audit_info ad;
2007 if (act == SMK_RECEIVING) {
2008 skp = smack_net_ambient;
2009 object = ssp->smk_in;
2012 object = smack_net_ambient->smk_known;
2016 * Get the IP address and port from the address.
2018 addr6 = (struct sockaddr_in6 *)address;
2019 port = ntohs(addr6->sin6_port);
2020 bep = (__be16 *)(&addr6->sin6_addr);
2021 be32p = (__be32 *)(&addr6->sin6_addr);
2024 * It's remote, so port lookup does no good.
2026 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2030 * It's local so the send check has to have passed.
2032 if (act == SMK_RECEIVING) {
2033 skp = &smack_known_web;
2037 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2038 if (spp->smk_port != port)
2040 object = spp->smk_in;
2041 if (act == SMK_CONNECTING)
2042 ssp->smk_packet = spp->smk_out->smk_known;
2049 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2050 ad.a.u.net.family = sk->sk_family;
2051 ad.a.u.net.dport = port;
2052 if (act == SMK_RECEIVING)
2053 ad.a.u.net.v6info.saddr = addr6->sin6_addr;
2055 ad.a.u.net.v6info.daddr = addr6->sin6_addr;
2057 return smk_access(skp, object, MAY_WRITE, &ad);
2061 * smack_inode_setsecurity - set smack xattrs
2062 * @inode: the object
2063 * @name: attribute name
2064 * @value: attribute value
2065 * @size: size of the attribute
2068 * Sets the named attribute in the appropriate blob
2070 * Returns 0 on success, or an error code
2072 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2073 const void *value, size_t size, int flags)
2075 struct smack_known *skp;
2076 struct inode_smack *nsp = inode->i_security;
2077 struct socket_smack *ssp;
2078 struct socket *sock;
2081 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2084 skp = smk_import_entry(value, size);
2088 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2089 nsp->smk_inode = skp->smk_known;
2090 nsp->smk_flags |= SMK_INODE_INSTANT;
2094 * The rest of the Smack xattrs are only on sockets.
2096 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2099 sock = SOCKET_I(inode);
2100 if (sock == NULL || sock->sk == NULL)
2103 ssp = sock->sk->sk_security;
2105 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2106 ssp->smk_in = skp->smk_known;
2107 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2109 if (sock->sk->sk_family == PF_INET) {
2110 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2113 "Smack: \"%s\" netlbl error %d.\n",
2119 if (sock->sk->sk_family == PF_INET6)
2120 smk_ipv6_port_label(sock, NULL);
2126 * smack_socket_post_create - finish socket setup
2128 * @family: protocol family
2133 * Sets the netlabel information on the socket
2135 * Returns 0 on success, and error code otherwise
2137 static int smack_socket_post_create(struct socket *sock, int family,
2138 int type, int protocol, int kern)
2140 if (family != PF_INET || sock->sk == NULL)
2143 * Set the outbound netlbl.
2145 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2149 * smack_socket_bind - record port binding information.
2151 * @address: the port address
2152 * @addrlen: size of the address
2154 * Records the label bound to a port.
2158 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2161 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2162 smk_ipv6_port_label(sock, address);
2168 * smack_socket_connect - connect access check
2170 * @sap: the other end
2171 * @addrlen: size of sap
2173 * Verifies that a connection may be possible
2175 * Returns 0 on success, and error code otherwise
2177 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2182 if (sock->sk == NULL)
2185 switch (sock->sk->sk_family) {
2187 if (addrlen < sizeof(struct sockaddr_in))
2189 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2192 if (addrlen < sizeof(struct sockaddr_in6))
2194 rc = smk_ipv6_port_check(sock->sk, sap, SMK_CONNECTING);
2201 * smack_flags_to_may - convert S_ to MAY_ values
2202 * @flags: the S_ value
2204 * Returns the equivalent MAY_ value
2206 static int smack_flags_to_may(int flags)
2210 if (flags & S_IRUGO)
2212 if (flags & S_IWUGO)
2214 if (flags & S_IXUGO)
2221 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2226 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2228 struct smack_known *skp = smk_of_current();
2230 msg->security = skp->smk_known;
2235 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2238 * Clears the blob pointer
2240 static void smack_msg_msg_free_security(struct msg_msg *msg)
2242 msg->security = NULL;
2246 * smack_of_shm - the smack pointer for the shm
2249 * Returns a pointer to the smack value
2251 static char *smack_of_shm(struct shmid_kernel *shp)
2253 return (char *)shp->shm_perm.security;
2257 * smack_shm_alloc_security - Set the security blob for shm
2262 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2264 struct kern_ipc_perm *isp = &shp->shm_perm;
2265 struct smack_known *skp = smk_of_current();
2267 isp->security = skp->smk_known;
2272 * smack_shm_free_security - Clear the security blob for shm
2275 * Clears the blob pointer
2277 static void smack_shm_free_security(struct shmid_kernel *shp)
2279 struct kern_ipc_perm *isp = &shp->shm_perm;
2281 isp->security = NULL;
2285 * smk_curacc_shm : check if current has access on shm
2287 * @access : access requested
2289 * Returns 0 if current has the requested access, error code otherwise
2291 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2293 char *ssp = smack_of_shm(shp);
2294 struct smk_audit_info ad;
2297 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2298 ad.a.u.ipc_id = shp->shm_perm.id;
2300 return smk_curacc(ssp, access, &ad);
2304 * smack_shm_associate - Smack access check for shm
2306 * @shmflg: access requested
2308 * Returns 0 if current has the requested access, error code otherwise
2310 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2314 may = smack_flags_to_may(shmflg);
2315 return smk_curacc_shm(shp, may);
2319 * smack_shm_shmctl - Smack access check for shm
2321 * @cmd: what it wants to do
2323 * Returns 0 if current has the requested access, error code otherwise
2325 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2338 may = MAY_READWRITE;
2343 * System level information.
2349 return smk_curacc_shm(shp, may);
2353 * smack_shm_shmat - Smack access for shmat
2356 * @shmflg: access requested
2358 * Returns 0 if current has the requested access, error code otherwise
2360 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2365 may = smack_flags_to_may(shmflg);
2366 return smk_curacc_shm(shp, may);
2370 * smack_of_sem - the smack pointer for the sem
2373 * Returns a pointer to the smack value
2375 static char *smack_of_sem(struct sem_array *sma)
2377 return (char *)sma->sem_perm.security;
2381 * smack_sem_alloc_security - Set the security blob for sem
2386 static int smack_sem_alloc_security(struct sem_array *sma)
2388 struct kern_ipc_perm *isp = &sma->sem_perm;
2389 struct smack_known *skp = smk_of_current();
2391 isp->security = skp->smk_known;
2396 * smack_sem_free_security - Clear the security blob for sem
2399 * Clears the blob pointer
2401 static void smack_sem_free_security(struct sem_array *sma)
2403 struct kern_ipc_perm *isp = &sma->sem_perm;
2405 isp->security = NULL;
2409 * smk_curacc_sem : check if current has access on sem
2411 * @access : access requested
2413 * Returns 0 if current has the requested access, error code otherwise
2415 static int smk_curacc_sem(struct sem_array *sma, int access)
2417 char *ssp = smack_of_sem(sma);
2418 struct smk_audit_info ad;
2421 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2422 ad.a.u.ipc_id = sma->sem_perm.id;
2424 return smk_curacc(ssp, access, &ad);
2428 * smack_sem_associate - Smack access check for sem
2430 * @semflg: access requested
2432 * Returns 0 if current has the requested access, error code otherwise
2434 static int smack_sem_associate(struct sem_array *sma, int semflg)
2438 may = smack_flags_to_may(semflg);
2439 return smk_curacc_sem(sma, may);
2443 * smack_sem_shmctl - Smack access check for sem
2445 * @cmd: what it wants to do
2447 * Returns 0 if current has the requested access, error code otherwise
2449 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2467 may = MAY_READWRITE;
2472 * System level information
2479 return smk_curacc_sem(sma, may);
2483 * smack_sem_semop - Smack checks of semaphore operations
2489 * Treated as read and write in all cases.
2491 * Returns 0 if access is allowed, error code otherwise
2493 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2494 unsigned nsops, int alter)
2496 return smk_curacc_sem(sma, MAY_READWRITE);
2500 * smack_msg_alloc_security - Set the security blob for msg
2505 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2507 struct kern_ipc_perm *kisp = &msq->q_perm;
2508 struct smack_known *skp = smk_of_current();
2510 kisp->security = skp->smk_known;
2515 * smack_msg_free_security - Clear the security blob for msg
2518 * Clears the blob pointer
2520 static void smack_msg_queue_free_security(struct msg_queue *msq)
2522 struct kern_ipc_perm *kisp = &msq->q_perm;
2524 kisp->security = NULL;
2528 * smack_of_msq - the smack pointer for the msq
2531 * Returns a pointer to the smack value
2533 static char *smack_of_msq(struct msg_queue *msq)
2535 return (char *)msq->q_perm.security;
2539 * smk_curacc_msq : helper to check if current has access on msq
2541 * @access : access requested
2543 * return 0 if current has access, error otherwise
2545 static int smk_curacc_msq(struct msg_queue *msq, int access)
2547 char *msp = smack_of_msq(msq);
2548 struct smk_audit_info ad;
2551 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2552 ad.a.u.ipc_id = msq->q_perm.id;
2554 return smk_curacc(msp, access, &ad);
2558 * smack_msg_queue_associate - Smack access check for msg_queue
2560 * @msqflg: access requested
2562 * Returns 0 if current has the requested access, error code otherwise
2564 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2568 may = smack_flags_to_may(msqflg);
2569 return smk_curacc_msq(msq, may);
2573 * smack_msg_queue_msgctl - Smack access check for msg_queue
2575 * @cmd: what it wants to do
2577 * Returns 0 if current has the requested access, error code otherwise
2579 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2590 may = MAY_READWRITE;
2595 * System level information
2602 return smk_curacc_msq(msq, may);
2606 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2609 * @msqflg: access requested
2611 * Returns 0 if current has the requested access, error code otherwise
2613 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2618 may = smack_flags_to_may(msqflg);
2619 return smk_curacc_msq(msq, may);
2623 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2630 * Returns 0 if current has read and write access, error code otherwise
2632 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2633 struct task_struct *target, long type, int mode)
2635 return smk_curacc_msq(msq, MAY_READWRITE);
2639 * smack_ipc_permission - Smack access for ipc_permission()
2640 * @ipp: the object permissions
2641 * @flag: access requested
2643 * Returns 0 if current has read and write access, error code otherwise
2645 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2647 char *isp = ipp->security;
2648 int may = smack_flags_to_may(flag);
2649 struct smk_audit_info ad;
2652 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2653 ad.a.u.ipc_id = ipp->id;
2655 return smk_curacc(isp, may, &ad);
2659 * smack_ipc_getsecid - Extract smack security id
2660 * @ipp: the object permissions
2661 * @secid: where result will be saved
2663 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2665 char *smack = ipp->security;
2667 *secid = smack_to_secid(smack);
2671 * smack_d_instantiate - Make sure the blob is correct on an inode
2672 * @opt_dentry: dentry where inode will be attached
2673 * @inode: the object
2675 * Set the inode's security blob if it hasn't been done already.
2677 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2679 struct super_block *sbp;
2680 struct superblock_smack *sbsp;
2681 struct inode_smack *isp;
2682 struct smack_known *skp;
2683 struct smack_known *ckp = smk_of_current();
2685 char trattr[TRANS_TRUE_SIZE];
2693 isp = inode->i_security;
2695 mutex_lock(&isp->smk_lock);
2697 * If the inode is already instantiated
2698 * take the quick way out
2700 if (isp->smk_flags & SMK_INODE_INSTANT)
2704 sbsp = sbp->s_security;
2706 * We're going to use the superblock default label
2707 * if there's no label on the file.
2709 final = sbsp->smk_default;
2712 * If this is the root inode the superblock
2713 * may be in the process of initialization.
2714 * If that is the case use the root value out
2715 * of the superblock.
2717 if (opt_dentry->d_parent == opt_dentry) {
2718 isp->smk_inode = sbsp->smk_root;
2719 isp->smk_flags |= SMK_INODE_INSTANT;
2724 * This is pretty hackish.
2725 * Casey says that we shouldn't have to do
2726 * file system specific code, but it does help
2727 * with keeping it simple.
2729 switch (sbp->s_magic) {
2732 * Casey says that it's a little embarrassing
2733 * that the smack file system doesn't do
2734 * extended attributes.
2736 final = smack_known_star.smk_known;
2740 * Casey says pipes are easy (?)
2742 final = smack_known_star.smk_known;
2744 case DEVPTS_SUPER_MAGIC:
2746 * devpts seems content with the label of the task.
2747 * Programs that change smack have to treat the
2750 final = ckp->smk_known;
2754 * Socket access is controlled by the socket
2755 * structures associated with the task involved.
2757 final = smack_known_star.smk_known;
2759 case PROC_SUPER_MAGIC:
2761 * Casey says procfs appears not to care.
2762 * The superblock default suffices.
2767 * Device labels should come from the filesystem,
2768 * but watch out, because they're volitile,
2769 * getting recreated on every reboot.
2771 final = smack_known_star.smk_known;
2775 * If a smack value has been set we want to use it,
2776 * but since tmpfs isn't giving us the opportunity
2777 * to set mount options simulate setting the
2778 * superblock default.
2782 * This isn't an understood special case.
2783 * Get the value from the xattr.
2787 * UNIX domain sockets use lower level socket data.
2789 if (S_ISSOCK(inode->i_mode)) {
2790 final = smack_known_star.smk_known;
2794 * No xattr support means, alas, no SMACK label.
2795 * Use the aforeapplied default.
2796 * It would be curious if the label of the task
2797 * does not match that assigned.
2799 if (inode->i_op->getxattr == NULL)
2802 * Get the dentry for xattr.
2804 dp = dget(opt_dentry);
2805 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2807 final = skp->smk_known;
2810 * Transmuting directory
2812 if (S_ISDIR(inode->i_mode)) {
2814 * If this is a new directory and the label was
2815 * transmuted when the inode was initialized
2816 * set the transmute attribute on the directory
2817 * and mark the inode.
2819 * If there is a transmute attribute on the
2820 * directory mark the inode.
2822 if (isp->smk_flags & SMK_INODE_CHANGED) {
2823 isp->smk_flags &= ~SMK_INODE_CHANGED;
2824 rc = inode->i_op->setxattr(dp,
2825 XATTR_NAME_SMACKTRANSMUTE,
2826 TRANS_TRUE, TRANS_TRUE_SIZE,
2829 rc = inode->i_op->getxattr(dp,
2830 XATTR_NAME_SMACKTRANSMUTE, trattr,
2832 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2833 TRANS_TRUE_SIZE) != 0)
2837 transflag = SMK_INODE_TRANSMUTE;
2839 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2840 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2847 isp->smk_inode = ckp->smk_known;
2849 isp->smk_inode = final;
2851 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2854 mutex_unlock(&isp->smk_lock);
2859 * smack_getprocattr - Smack process attribute access
2860 * @p: the object task
2861 * @name: the name of the attribute in /proc/.../attr
2862 * @value: where to put the result
2864 * Places a copy of the task Smack into value
2866 * Returns the length of the smack label or an error code
2868 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2870 struct smack_known *skp = smk_of_task(task_security(p));
2874 if (strcmp(name, "current") != 0)
2877 cp = kstrdup(skp->smk_known, GFP_KERNEL);
2887 * smack_setprocattr - Smack process attribute setting
2888 * @p: the object task
2889 * @name: the name of the attribute in /proc/.../attr
2890 * @value: the value to set
2891 * @size: the size of the value
2893 * Sets the Smack value of the task. Only setting self
2894 * is permitted and only with privilege
2896 * Returns the length of the smack label or an error code
2898 static int smack_setprocattr(struct task_struct *p, char *name,
2899 void *value, size_t size)
2901 struct task_smack *tsp;
2903 struct smack_known *skp;
2906 * Changing another process' Smack value is too dangerous
2907 * and supports no sane use case.
2912 if (!smack_privileged(CAP_MAC_ADMIN))
2915 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
2918 if (strcmp(name, "current") != 0)
2921 skp = smk_import_entry(value, size);
2926 * No process is ever allowed the web ("@") label.
2928 if (skp == &smack_known_web)
2931 new = prepare_creds();
2935 tsp = new->security;
2936 tsp->smk_task = skp;
2943 * smack_unix_stream_connect - Smack access on UDS
2945 * @other: the other sock
2948 * Return 0 if a subject with the smack of sock could access
2949 * an object with the smack of other, otherwise an error code
2951 static int smack_unix_stream_connect(struct sock *sock,
2952 struct sock *other, struct sock *newsk)
2954 struct smack_known *skp;
2955 struct socket_smack *ssp = sock->sk_security;
2956 struct socket_smack *osp = other->sk_security;
2957 struct socket_smack *nsp = newsk->sk_security;
2958 struct smk_audit_info ad;
2961 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2962 smk_ad_setfield_u_net_sk(&ad, other);
2964 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
2966 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
2970 * Cross reference the peer labels for SO_PEERSEC.
2973 nsp->smk_packet = ssp->smk_out->smk_known;
2974 ssp->smk_packet = osp->smk_out->smk_known;
2981 * smack_unix_may_send - Smack access on UDS
2983 * @other: the other socket
2985 * Return 0 if a subject with the smack of sock could access
2986 * an object with the smack of other, otherwise an error code
2988 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2990 struct socket_smack *ssp = sock->sk->sk_security;
2991 struct socket_smack *osp = other->sk->sk_security;
2992 struct smack_known *skp;
2993 struct smk_audit_info ad;
2995 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2996 smk_ad_setfield_u_net_sk(&ad, other->sk);
2998 if (smack_privileged(CAP_MAC_OVERRIDE))
3002 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3006 * smack_socket_sendmsg - Smack check based on destination host
3009 * @size: the size of the message
3011 * Return 0 if the current subject can write to the destination host.
3012 * For IPv4 this is only a question if the destination is a single label host.
3013 * For IPv6 this is a check against the label of the port.
3015 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3018 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3019 struct sockaddr *sap = (struct sockaddr *) msg->msg_name;
3023 * Perfectly reasonable for this to be NULL
3028 switch (sip->sin_family) {
3030 rc = smack_netlabel_send(sock->sk, sip);
3033 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3040 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3041 * @sap: netlabel secattr
3042 * @ssp: socket security information
3044 * Returns a pointer to a Smack label entry found on the label list.
3046 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3047 struct socket_smack *ssp)
3049 struct smack_known *skp;
3052 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3054 * Looks like a CIPSO packet.
3055 * If there are flags but no level netlabel isn't
3056 * behaving the way we expect it to.
3058 * Look it up in the label table
3059 * Without guidance regarding the smack value
3060 * for the packet fall back on the network
3064 list_for_each_entry(skp, &smack_known_list, list) {
3065 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3067 if (memcmp(sap->attr.mls.cat,
3068 skp->smk_netlabel.attr.mls.cat,
3079 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
3080 return &smack_known_web;
3081 return &smack_known_star;
3083 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3085 * Looks like a fallback, which gives us a secid.
3087 skp = smack_from_secid(sap->attr.secid);
3089 * This has got to be a bug because it is
3090 * impossible to specify a fallback without
3091 * specifying the label, which will ensure
3092 * it has a secid, and the only way to get a
3093 * secid is from a fallback.
3095 BUG_ON(skp == NULL);
3099 * Without guidance regarding the smack value
3100 * for the packet fall back on the network
3103 return smack_net_ambient;
3106 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr *sap)
3108 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
3111 int proto = -EINVAL;
3112 struct ipv6hdr _ipv6h;
3113 struct ipv6hdr *ip6;
3115 struct tcphdr _tcph, *th;
3116 struct udphdr _udph, *uh;
3117 struct dccp_hdr _dccph, *dh;
3121 offset = skb_network_offset(skb);
3122 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3125 sip->sin6_addr = ip6->saddr;
3127 nexthdr = ip6->nexthdr;
3128 offset += sizeof(_ipv6h);
3129 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3136 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3138 sip->sin6_port = th->source;
3141 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3143 sip->sin6_port = uh->source;
3146 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3148 sip->sin6_port = dh->dccph_sport;
3155 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3159 * Returns 0 if the packet should be delivered, an error code otherwise
3161 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3163 struct netlbl_lsm_secattr secattr;
3164 struct socket_smack *ssp = sk->sk_security;
3165 struct smack_known *skp;
3166 struct sockaddr sadd;
3168 struct smk_audit_info ad;
3169 switch (sk->sk_family) {
3172 * Translate what netlabel gave us.
3174 netlbl_secattr_init(&secattr);
3176 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3178 skp = smack_from_secattr(&secattr, ssp);
3180 skp = smack_net_ambient;
3182 netlbl_secattr_destroy(&secattr);
3185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3186 ad.a.u.net.family = sk->sk_family;
3187 ad.a.u.net.netif = skb->skb_iif;
3188 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3191 * Receiving a packet requires that the other end
3192 * be able to write here. Read access is not required.
3193 * This is the simplist possible security model
3196 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3198 netlbl_skbuff_err(skb, rc, 0);
3201 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3202 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3203 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3212 * smack_socket_getpeersec_stream - pull in packet label
3214 * @optval: user's destination
3215 * @optlen: size thereof
3218 * returns zero on success, an error code otherwise
3220 static int smack_socket_getpeersec_stream(struct socket *sock,
3221 char __user *optval,
3222 int __user *optlen, unsigned len)
3224 struct socket_smack *ssp;
3229 ssp = sock->sk->sk_security;
3230 if (ssp->smk_packet != NULL) {
3231 rcp = ssp->smk_packet;
3232 slen = strlen(rcp) + 1;
3237 else if (copy_to_user(optval, rcp, slen) != 0)
3240 if (put_user(slen, optlen) != 0)
3248 * smack_socket_getpeersec_dgram - pull in packet label
3249 * @sock: the peer socket
3251 * @secid: pointer to where to put the secid of the packet
3253 * Sets the netlabel socket state on sk from parent
3255 static int smack_socket_getpeersec_dgram(struct socket *sock,
3256 struct sk_buff *skb, u32 *secid)
3259 struct netlbl_lsm_secattr secattr;
3260 struct socket_smack *ssp = NULL;
3261 struct smack_known *skp;
3262 int family = PF_UNSPEC;
3263 u32 s = 0; /* 0 is the invalid secid */
3267 if (skb->protocol == htons(ETH_P_IP))
3269 else if (skb->protocol == htons(ETH_P_IPV6))
3272 if (family == PF_UNSPEC && sock != NULL)
3273 family = sock->sk->sk_family;
3275 if (family == PF_UNIX) {
3276 ssp = sock->sk->sk_security;
3277 s = ssp->smk_out->smk_secid;
3278 } else if (family == PF_INET || family == PF_INET6) {
3280 * Translate what netlabel gave us.
3282 if (sock != NULL && sock->sk != NULL)
3283 ssp = sock->sk->sk_security;
3284 netlbl_secattr_init(&secattr);
3285 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3287 skp = smack_from_secattr(&secattr, ssp);
3290 netlbl_secattr_destroy(&secattr);
3299 * smack_sock_graft - Initialize a newly created socket with an existing sock
3301 * @parent: parent socket
3303 * Set the smk_{in,out} state of an existing sock based on the process that
3304 * is creating the new socket.
3306 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3308 struct socket_smack *ssp;
3309 struct smack_known *skp = smk_of_current();
3312 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3315 ssp = sk->sk_security;
3316 ssp->smk_in = skp->smk_known;
3318 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3322 * smack_inet_conn_request - Smack access check on connect
3323 * @sk: socket involved
3327 * Returns 0 if a task with the packet label could write to
3328 * the socket, otherwise an error code
3330 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3331 struct request_sock *req)
3333 u16 family = sk->sk_family;
3334 struct smack_known *skp;
3335 struct socket_smack *ssp = sk->sk_security;
3336 struct netlbl_lsm_secattr secattr;
3337 struct sockaddr_in addr;
3341 struct smk_audit_info ad;
3343 if (family == PF_INET6) {
3345 * Handle mapped IPv4 packets arriving
3346 * via IPv6 sockets. Don't set up netlabel
3347 * processing on IPv6.
3349 if (skb->protocol == htons(ETH_P_IP))
3355 netlbl_secattr_init(&secattr);
3356 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3358 skp = smack_from_secattr(&secattr, ssp);
3360 skp = &smack_known_huh;
3361 netlbl_secattr_destroy(&secattr);
3364 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3365 ad.a.u.net.family = family;
3366 ad.a.u.net.netif = skb->skb_iif;
3367 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3370 * Receiving a packet requires that the other end be able to write
3371 * here. Read access is not required.
3373 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3378 * Save the peer's label in the request_sock so we can later setup
3379 * smk_packet in the child socket so that SO_PEERCRED can report it.
3381 req->peer_secid = skp->smk_secid;
3384 * We need to decide if we want to label the incoming connection here
3385 * if we do we only need to label the request_sock and the stack will
3386 * propagate the wire-label to the sock when it is created.
3389 addr.sin_addr.s_addr = hdr->saddr;
3391 hsp = smack_host_label(&addr);
3395 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3397 netlbl_req_delattr(req);
3403 * smack_inet_csk_clone - Copy the connection information to the new socket
3404 * @sk: the new socket
3405 * @req: the connection's request_sock
3407 * Transfer the connection's peer label to the newly created socket.
3409 static void smack_inet_csk_clone(struct sock *sk,
3410 const struct request_sock *req)
3412 struct socket_smack *ssp = sk->sk_security;
3413 struct smack_known *skp;
3415 if (req->peer_secid != 0) {
3416 skp = smack_from_secid(req->peer_secid);
3417 ssp->smk_packet = skp->smk_known;
3419 ssp->smk_packet = NULL;
3423 * Key management security hooks
3425 * Casey has not tested key support very heavily.
3426 * The permission check is most likely too restrictive.
3427 * If you care about keys please have a look.
3432 * smack_key_alloc - Set the key security blob
3434 * @cred: the credentials to use
3437 * No allocation required
3441 static int smack_key_alloc(struct key *key, const struct cred *cred,
3442 unsigned long flags)
3444 struct smack_known *skp = smk_of_task(cred->security);
3446 key->security = skp->smk_known;
3451 * smack_key_free - Clear the key security blob
3454 * Clear the blob pointer
3456 static void smack_key_free(struct key *key)
3458 key->security = NULL;
3462 * smack_key_permission - Smack access on a key
3463 * @key_ref: gets to the object
3464 * @cred: the credentials to use
3467 * Return 0 if the task has read and write to the object,
3468 * an error code otherwise
3470 static int smack_key_permission(key_ref_t key_ref,
3471 const struct cred *cred, key_perm_t perm)
3474 struct smk_audit_info ad;
3475 struct smack_known *tkp = smk_of_task(cred->security);
3477 keyp = key_ref_to_ptr(key_ref);
3481 * If the key hasn't been initialized give it access so that
3484 if (keyp->security == NULL)
3487 * This should not occur
3492 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3493 ad.a.u.key_struct.key = keyp->serial;
3494 ad.a.u.key_struct.key_desc = keyp->description;
3496 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
3498 #endif /* CONFIG_KEYS */
3503 * Audit requires a unique representation of each Smack specific
3504 * rule. This unique representation is used to distinguish the
3505 * object to be audited from remaining kernel objects and also
3506 * works as a glue between the audit hooks.
3508 * Since repository entries are added but never deleted, we'll use
3509 * the smack_known label address related to the given audit rule as
3510 * the needed unique representation. This also better fits the smack
3511 * model where nearly everything is a label.
3516 * smack_audit_rule_init - Initialize a smack audit rule
3517 * @field: audit rule fields given from user-space (audit.h)
3518 * @op: required testing operator (=, !=, >, <, ...)
3519 * @rulestr: smack label to be audited
3520 * @vrule: pointer to save our own audit rule representation
3522 * Prepare to audit cases where (@field @op @rulestr) is true.
3523 * The label to be audited is created if necessay.
3525 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3527 char **rule = (char **)vrule;
3530 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3533 if (op != Audit_equal && op != Audit_not_equal)
3536 *rule = smk_import(rulestr, 0);
3542 * smack_audit_rule_known - Distinguish Smack audit rules
3543 * @krule: rule of interest, in Audit kernel representation format
3545 * This is used to filter Smack rules from remaining Audit ones.
3546 * If it's proved that this rule belongs to us, the
3547 * audit_rule_match hook will be called to do the final judgement.
3549 static int smack_audit_rule_known(struct audit_krule *krule)
3551 struct audit_field *f;
3554 for (i = 0; i < krule->field_count; i++) {
3555 f = &krule->fields[i];
3557 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3565 * smack_audit_rule_match - Audit given object ?
3566 * @secid: security id for identifying the object to test
3567 * @field: audit rule flags given from user-space
3568 * @op: required testing operator
3569 * @vrule: smack internal rule presentation
3570 * @actx: audit context associated with the check
3572 * The core Audit hook. It's used to take the decision of
3573 * whether to audit or not to audit a given object.
3575 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3576 struct audit_context *actx)
3578 struct smack_known *skp;
3582 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3583 "Smack: missing rule\n");
3587 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3590 skp = smack_from_secid(secid);
3593 * No need to do string comparisons. If a match occurs,
3594 * both pointers will point to the same smack_known
3597 if (op == Audit_equal)
3598 return (rule == skp->smk_known);
3599 if (op == Audit_not_equal)
3600 return (rule != skp->smk_known);
3606 * smack_audit_rule_free - free smack rule representation
3607 * @vrule: rule to be freed.
3609 * No memory was allocated.
3611 static void smack_audit_rule_free(void *vrule)
3616 #endif /* CONFIG_AUDIT */
3619 * smack_secid_to_secctx - return the smack label for a secid
3620 * @secid: incoming integer
3621 * @secdata: destination
3622 * @seclen: how long it is
3624 * Exists for networking code.
3626 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3628 struct smack_known *skp = smack_from_secid(secid);
3631 *secdata = skp->smk_known;
3632 *seclen = strlen(skp->smk_known);
3637 * smack_secctx_to_secid - return the secid for a smack label
3638 * @secdata: smack label
3639 * @seclen: how long result is
3640 * @secid: outgoing integer
3642 * Exists for audit and networking code.
3644 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3646 *secid = smack_to_secid(secdata);
3651 * smack_release_secctx - don't do anything.
3655 * Exists to make sure nothing gets done, and properly
3657 static void smack_release_secctx(char *secdata, u32 seclen)
3661 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3663 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3666 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3668 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3671 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3674 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3682 struct security_operations smack_ops = {
3685 .ptrace_access_check = smack_ptrace_access_check,
3686 .ptrace_traceme = smack_ptrace_traceme,
3687 .syslog = smack_syslog,
3689 .sb_alloc_security = smack_sb_alloc_security,
3690 .sb_free_security = smack_sb_free_security,
3691 .sb_copy_data = smack_sb_copy_data,
3692 .sb_kern_mount = smack_sb_kern_mount,
3693 .sb_statfs = smack_sb_statfs,
3694 .sb_mount = smack_sb_mount,
3695 .sb_umount = smack_sb_umount,
3697 .bprm_set_creds = smack_bprm_set_creds,
3698 .bprm_committing_creds = smack_bprm_committing_creds,
3699 .bprm_secureexec = smack_bprm_secureexec,
3701 .inode_alloc_security = smack_inode_alloc_security,
3702 .inode_free_security = smack_inode_free_security,
3703 .inode_init_security = smack_inode_init_security,
3704 .inode_link = smack_inode_link,
3705 .inode_unlink = smack_inode_unlink,
3706 .inode_rmdir = smack_inode_rmdir,
3707 .inode_rename = smack_inode_rename,
3708 .inode_permission = smack_inode_permission,
3709 .inode_setattr = smack_inode_setattr,
3710 .inode_getattr = smack_inode_getattr,
3711 .inode_setxattr = smack_inode_setxattr,
3712 .inode_post_setxattr = smack_inode_post_setxattr,
3713 .inode_getxattr = smack_inode_getxattr,
3714 .inode_removexattr = smack_inode_removexattr,
3715 .inode_getsecurity = smack_inode_getsecurity,
3716 .inode_setsecurity = smack_inode_setsecurity,
3717 .inode_listsecurity = smack_inode_listsecurity,
3718 .inode_getsecid = smack_inode_getsecid,
3720 .file_permission = smack_file_permission,
3721 .file_alloc_security = smack_file_alloc_security,
3722 .file_free_security = smack_file_free_security,
3723 .file_ioctl = smack_file_ioctl,
3724 .file_lock = smack_file_lock,
3725 .file_fcntl = smack_file_fcntl,
3726 .file_mmap = smack_file_mmap,
3727 .file_set_fowner = smack_file_set_fowner,
3728 .file_send_sigiotask = smack_file_send_sigiotask,
3729 .file_receive = smack_file_receive,
3731 .dentry_open = smack_dentry_open,
3733 .cred_alloc_blank = smack_cred_alloc_blank,
3734 .cred_free = smack_cred_free,
3735 .cred_prepare = smack_cred_prepare,
3736 .cred_transfer = smack_cred_transfer,
3737 .kernel_act_as = smack_kernel_act_as,
3738 .kernel_create_files_as = smack_kernel_create_files_as,
3739 .task_setpgid = smack_task_setpgid,
3740 .task_getpgid = smack_task_getpgid,
3741 .task_getsid = smack_task_getsid,
3742 .task_getsecid = smack_task_getsecid,
3743 .task_setnice = smack_task_setnice,
3744 .task_setioprio = smack_task_setioprio,
3745 .task_getioprio = smack_task_getioprio,
3746 .task_setscheduler = smack_task_setscheduler,
3747 .task_getscheduler = smack_task_getscheduler,
3748 .task_movememory = smack_task_movememory,
3749 .task_kill = smack_task_kill,
3750 .task_wait = smack_task_wait,
3751 .task_to_inode = smack_task_to_inode,
3753 .ipc_permission = smack_ipc_permission,
3754 .ipc_getsecid = smack_ipc_getsecid,
3756 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3757 .msg_msg_free_security = smack_msg_msg_free_security,
3759 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3760 .msg_queue_free_security = smack_msg_queue_free_security,
3761 .msg_queue_associate = smack_msg_queue_associate,
3762 .msg_queue_msgctl = smack_msg_queue_msgctl,
3763 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3764 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3766 .shm_alloc_security = smack_shm_alloc_security,
3767 .shm_free_security = smack_shm_free_security,
3768 .shm_associate = smack_shm_associate,
3769 .shm_shmctl = smack_shm_shmctl,
3770 .shm_shmat = smack_shm_shmat,
3772 .sem_alloc_security = smack_sem_alloc_security,
3773 .sem_free_security = smack_sem_free_security,
3774 .sem_associate = smack_sem_associate,
3775 .sem_semctl = smack_sem_semctl,
3776 .sem_semop = smack_sem_semop,
3778 .d_instantiate = smack_d_instantiate,
3780 .getprocattr = smack_getprocattr,
3781 .setprocattr = smack_setprocattr,
3783 .unix_stream_connect = smack_unix_stream_connect,
3784 .unix_may_send = smack_unix_may_send,
3786 .socket_post_create = smack_socket_post_create,
3787 .socket_bind = smack_socket_bind,
3788 .socket_connect = smack_socket_connect,
3789 .socket_sendmsg = smack_socket_sendmsg,
3790 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3791 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3792 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3793 .sk_alloc_security = smack_sk_alloc_security,
3794 .sk_free_security = smack_sk_free_security,
3795 .sock_graft = smack_sock_graft,
3796 .inet_conn_request = smack_inet_conn_request,
3797 .inet_csk_clone = smack_inet_csk_clone,
3799 /* key management security hooks */
3801 .key_alloc = smack_key_alloc,
3802 .key_free = smack_key_free,
3803 .key_permission = smack_key_permission,
3804 #endif /* CONFIG_KEYS */
3808 .audit_rule_init = smack_audit_rule_init,
3809 .audit_rule_known = smack_audit_rule_known,
3810 .audit_rule_match = smack_audit_rule_match,
3811 .audit_rule_free = smack_audit_rule_free,
3812 #endif /* CONFIG_AUDIT */
3814 .secid_to_secctx = smack_secid_to_secctx,
3815 .secctx_to_secid = smack_secctx_to_secid,
3816 .release_secctx = smack_release_secctx,
3817 .inode_notifysecctx = smack_inode_notifysecctx,
3818 .inode_setsecctx = smack_inode_setsecctx,
3819 .inode_getsecctx = smack_inode_getsecctx,
3823 static __init void init_smack_known_list(void)
3826 * Initialize rule list locks
3828 mutex_init(&smack_known_huh.smk_rules_lock);
3829 mutex_init(&smack_known_hat.smk_rules_lock);
3830 mutex_init(&smack_known_floor.smk_rules_lock);
3831 mutex_init(&smack_known_star.smk_rules_lock);
3832 mutex_init(&smack_known_invalid.smk_rules_lock);
3833 mutex_init(&smack_known_web.smk_rules_lock);
3835 * Initialize rule lists
3837 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3838 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3839 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3840 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3841 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3842 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3844 * Create the known labels list
3846 list_add(&smack_known_huh.list, &smack_known_list);
3847 list_add(&smack_known_hat.list, &smack_known_list);
3848 list_add(&smack_known_star.list, &smack_known_list);
3849 list_add(&smack_known_floor.list, &smack_known_list);
3850 list_add(&smack_known_invalid.list, &smack_known_list);
3851 list_add(&smack_known_web.list, &smack_known_list);
3855 * smack_init - initialize the smack system
3859 static __init int smack_init(void)
3862 struct task_smack *tsp;
3864 if (!security_module_enable(&smack_ops))
3867 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3872 printk(KERN_INFO "Smack: Initializing.\n");
3875 * Set the security state for the initial task.
3877 cred = (struct cred *) current->cred;
3878 cred->security = tsp;
3880 /* initialize the smack_known_list */
3881 init_smack_known_list();
3886 if (register_security(&smack_ops))
3887 panic("smack: Unable to register with kernel.\n");
3893 * Smack requires early initialization in order to label
3894 * all processes and objects when they are created.
3896 security_initcall(smack_init);