2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/slab.h>
31 #include <linux/mutex.h>
32 #include <linux/pipe_fs_i.h>
33 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
40 #define task_security(task) (task_cred_xxx((task), security))
42 #define TRANS_TRUE "TRUE"
43 #define TRANS_TRUE_SIZE 4
46 * smk_fetch - Fetch the smack label from a file.
47 * @ip: a pointer to the inode
48 * @dp: a pointer to the dentry
50 * Returns a pointer to the master list entry for the Smack label
51 * or NULL if there was no label to fetch.
53 static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
59 if (ip->i_op->getxattr == NULL)
62 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
66 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
68 result = smk_import(buffer, rc);
76 * new_inode_smack - allocate an inode security blob
77 * @smack: a pointer to the Smack label to use in the blob
79 * Returns the new blob or NULL if there's no memory available
81 struct inode_smack *new_inode_smack(char *smack)
83 struct inode_smack *isp;
85 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
89 isp->smk_inode = smack;
91 mutex_init(&isp->smk_lock);
97 * new_task_smack - allocate a task security blob
98 * @smack: a pointer to the Smack label to use in the blob
100 * Returns the new blob or NULL if there's no memory available
102 static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
104 struct task_smack *tsp;
106 tsp = kzalloc(sizeof(struct task_smack), gfp);
110 tsp->smk_task = task;
111 tsp->smk_forked = forked;
112 INIT_LIST_HEAD(&tsp->smk_rules);
113 mutex_init(&tsp->smk_rules_lock);
119 * smk_copy_rules - copy a rule set
120 * @nhead - new rules header pointer
121 * @ohead - old rules header pointer
123 * Returns 0 on success, -ENOMEM on error
125 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
128 struct smack_rule *nrp;
129 struct smack_rule *orp;
132 INIT_LIST_HEAD(nhead);
134 list_for_each_entry_rcu(orp, ohead, list) {
135 nrp = kzalloc(sizeof(struct smack_rule), gfp);
141 list_add_rcu(&nrp->list, nhead);
148 * We he, that is fun!
152 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
153 * @ctp: child task pointer
154 * @mode: ptrace attachment mode
156 * Returns 0 if access is OK, an error code otherwise
158 * Do the capability checks, and require read and write.
160 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
163 struct smk_audit_info ad;
166 rc = cap_ptrace_access_check(ctp, mode);
170 tsp = smk_of_task(task_security(ctp));
171 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
172 smk_ad_setfield_u_tsk(&ad, ctp);
174 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
179 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
180 * @ptp: parent task pointer
182 * Returns 0 if access is OK, an error code otherwise
184 * Do the capability checks, and require read and write.
186 static int smack_ptrace_traceme(struct task_struct *ptp)
189 struct smk_audit_info ad;
192 rc = cap_ptrace_traceme(ptp);
196 tsp = smk_of_task(task_security(ptp));
197 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
198 smk_ad_setfield_u_tsk(&ad, ptp);
200 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
205 * smack_syslog - Smack approval on syslog
206 * @type: message type
208 * Require that the task has the floor label
210 * Returns 0 on success, error code otherwise.
212 static int smack_syslog(int typefrom_file)
215 char *sp = smk_of_current();
217 if (smack_privileged(CAP_MAC_OVERRIDE))
220 if (sp != smack_known_floor.smk_known)
232 * smack_sb_alloc_security - allocate a superblock blob
233 * @sb: the superblock getting the blob
235 * Returns 0 on success or -ENOMEM on error.
237 static int smack_sb_alloc_security(struct super_block *sb)
239 struct superblock_smack *sbsp;
241 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
246 sbsp->smk_root = smack_known_floor.smk_known;
247 sbsp->smk_default = smack_known_floor.smk_known;
248 sbsp->smk_floor = smack_known_floor.smk_known;
249 sbsp->smk_hat = smack_known_hat.smk_known;
250 sbsp->smk_initialized = 0;
252 sb->s_security = sbsp;
258 * smack_sb_free_security - free a superblock blob
259 * @sb: the superblock getting the blob
262 static void smack_sb_free_security(struct super_block *sb)
264 kfree(sb->s_security);
265 sb->s_security = NULL;
269 * smack_sb_copy_data - copy mount options data for processing
270 * @orig: where to start
271 * @smackopts: mount options string
273 * Returns 0 on success or -ENOMEM on error.
275 * Copy the Smack specific mount options out of the mount
278 static int smack_sb_copy_data(char *orig, char *smackopts)
280 char *cp, *commap, *otheropts, *dp;
282 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
283 if (otheropts == NULL)
286 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
287 if (strstr(cp, SMK_FSDEFAULT) == cp)
289 else if (strstr(cp, SMK_FSFLOOR) == cp)
291 else if (strstr(cp, SMK_FSHAT) == cp)
293 else if (strstr(cp, SMK_FSROOT) == cp)
298 commap = strchr(cp, ',');
307 strcpy(orig, otheropts);
308 free_page((unsigned long)otheropts);
314 * smack_sb_kern_mount - Smack specific mount processing
315 * @sb: the file system superblock
316 * @flags: the mount flags
317 * @data: the smack mount options
319 * Returns 0 on success, an error code on failure
321 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
323 struct dentry *root = sb->s_root;
324 struct inode *inode = root->d_inode;
325 struct superblock_smack *sp = sb->s_security;
326 struct inode_smack *isp;
331 if (sp->smk_initialized != 0)
334 sp->smk_initialized = 1;
336 for (op = data; op != NULL; op = commap) {
337 commap = strchr(op, ',');
341 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
342 op += strlen(SMK_FSHAT);
343 nsp = smk_import(op, 0);
346 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
347 op += strlen(SMK_FSFLOOR);
348 nsp = smk_import(op, 0);
351 } else if (strncmp(op, SMK_FSDEFAULT,
352 strlen(SMK_FSDEFAULT)) == 0) {
353 op += strlen(SMK_FSDEFAULT);
354 nsp = smk_import(op, 0);
356 sp->smk_default = nsp;
357 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
358 op += strlen(SMK_FSROOT);
359 nsp = smk_import(op, 0);
366 * Initialize the root inode.
368 isp = inode->i_security;
370 inode->i_security = new_inode_smack(sp->smk_root);
372 isp->smk_inode = sp->smk_root;
378 * smack_sb_statfs - Smack check on statfs
379 * @dentry: identifies the file system in question
381 * Returns 0 if current can read the floor of the filesystem,
382 * and error code otherwise
384 static int smack_sb_statfs(struct dentry *dentry)
386 struct superblock_smack *sbp = dentry->d_sb->s_security;
388 struct smk_audit_info ad;
390 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
391 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
393 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
398 * smack_sb_mount - Smack check for mounting
405 * Returns 0 if current can write the floor of the filesystem
406 * being mounted on, an error code otherwise.
408 static int smack_sb_mount(char *dev_name, struct path *path,
409 char *type, unsigned long flags, void *data)
411 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
412 struct smk_audit_info ad;
414 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
415 smk_ad_setfield_u_fs_path(&ad, *path);
417 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
421 * smack_sb_umount - Smack check for unmounting
422 * @mnt: file system to unmount
425 * Returns 0 if current can write the floor of the filesystem
426 * being unmounted, an error code otherwise.
428 static int smack_sb_umount(struct vfsmount *mnt, int flags)
430 struct superblock_smack *sbp;
431 struct smk_audit_info ad;
434 path.dentry = mnt->mnt_root;
437 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
438 smk_ad_setfield_u_fs_path(&ad, path);
440 sbp = mnt->mnt_sb->s_security;
441 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
449 * smack_bprm_set_creds - set creds for exec
450 * @bprm: the exec information
452 * Returns 0 if it gets a blob, -ENOMEM otherwise
454 static int smack_bprm_set_creds(struct linux_binprm *bprm)
456 struct inode *inode = bprm->file->f_path.dentry->d_inode;
457 struct task_smack *bsp = bprm->cred->security;
458 struct inode_smack *isp;
461 rc = cap_bprm_set_creds(bprm);
465 if (bprm->cred_prepared)
468 isp = inode->i_security;
469 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
475 bsp->smk_task = isp->smk_task;
476 bprm->per_clear |= PER_CLEAR_ON_SETID;
482 * smack_bprm_committing_creds - Prepare to install the new credentials
485 * @bprm: binprm for exec
487 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
489 struct task_smack *bsp = bprm->cred->security;
491 if (bsp->smk_task != bsp->smk_forked)
492 current->pdeath_signal = 0;
496 * smack_bprm_secureexec - Return the decision to use secureexec.
497 * @bprm: binprm for exec
499 * Returns 0 on success.
501 static int smack_bprm_secureexec(struct linux_binprm *bprm)
503 struct task_smack *tsp = current_security();
504 int ret = cap_bprm_secureexec(bprm);
506 if (!ret && (tsp->smk_task != tsp->smk_forked))
517 * smack_inode_alloc_security - allocate an inode blob
518 * @inode: the inode in need of a blob
520 * Returns 0 if it gets a blob, -ENOMEM otherwise
522 static int smack_inode_alloc_security(struct inode *inode)
524 inode->i_security = new_inode_smack(smk_of_current());
525 if (inode->i_security == NULL)
531 * smack_inode_free_security - free an inode blob
532 * @inode: the inode with a blob
534 * Clears the blob pointer in inode
536 static void smack_inode_free_security(struct inode *inode)
538 kfree(inode->i_security);
539 inode->i_security = NULL;
543 * smack_inode_init_security - copy out the smack from an inode
547 * @name: where to put the attribute name
548 * @value: where to put the attribute value
549 * @len: where to put the length of the attribute
551 * Returns 0 if it all works out, -ENOMEM if there's no memory
553 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
554 const struct qstr *qstr, char **name,
555 void **value, size_t *len)
557 struct smack_known *skp;
558 struct inode_smack *issp = inode->i_security;
559 char *csp = smk_of_current();
560 char *isp = smk_of_inode(inode);
561 char *dsp = smk_of_inode(dir);
565 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
571 skp = smk_find_entry(csp);
573 may = smk_access_entry(csp, dsp, &skp->smk_rules);
577 * If the access rule allows transmutation and
578 * the directory requests transmutation then
579 * by all means transmute.
580 * Mark the inode as changed.
582 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
583 smk_inode_transmutable(dir)) {
585 issp->smk_flags |= SMK_INODE_CHANGED;
588 *value = kstrdup(isp, GFP_KERNEL);
594 *len = strlen(isp) + 1;
600 * smack_inode_link - Smack check on link
601 * @old_dentry: the existing object
603 * @new_dentry: the new object
605 * Returns 0 if access is permitted, an error code otherwise
607 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
608 struct dentry *new_dentry)
611 struct smk_audit_info ad;
614 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
615 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
617 isp = smk_of_inode(old_dentry->d_inode);
618 rc = smk_curacc(isp, MAY_WRITE, &ad);
620 if (rc == 0 && new_dentry->d_inode != NULL) {
621 isp = smk_of_inode(new_dentry->d_inode);
622 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
623 rc = smk_curacc(isp, MAY_WRITE, &ad);
630 * smack_inode_unlink - Smack check on inode deletion
631 * @dir: containing directory object
632 * @dentry: file to unlink
634 * Returns 0 if current can write the containing directory
635 * and the object, error code otherwise
637 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
639 struct inode *ip = dentry->d_inode;
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, dentry);
647 * You need write access to the thing you're unlinking
649 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
652 * You also need write access to the containing directory
654 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
655 smk_ad_setfield_u_fs_inode(&ad, dir);
656 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
662 * smack_inode_rmdir - Smack check on directory deletion
663 * @dir: containing directory object
664 * @dentry: directory to unlink
666 * Returns 0 if current can write the containing directory
667 * and the directory, error code otherwise
669 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
671 struct smk_audit_info ad;
674 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
675 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
678 * You need write access to the thing you're removing
680 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
683 * You also need write access to the containing directory
685 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
686 smk_ad_setfield_u_fs_inode(&ad, dir);
687 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
694 * smack_inode_rename - Smack check on rename
695 * @old_inode: the old directory
696 * @old_dentry: unused
697 * @new_inode: the new directory
698 * @new_dentry: unused
700 * Read and write access is required on both the old and
703 * Returns 0 if access is permitted, an error code otherwise
705 static int smack_inode_rename(struct inode *old_inode,
706 struct dentry *old_dentry,
707 struct inode *new_inode,
708 struct dentry *new_dentry)
712 struct smk_audit_info ad;
714 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
715 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
717 isp = smk_of_inode(old_dentry->d_inode);
718 rc = smk_curacc(isp, MAY_READWRITE, &ad);
720 if (rc == 0 && new_dentry->d_inode != NULL) {
721 isp = smk_of_inode(new_dentry->d_inode);
722 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
723 rc = smk_curacc(isp, MAY_READWRITE, &ad);
729 * smack_inode_permission - Smack version of permission()
730 * @inode: the inode in question
731 * @mask: the access requested
732 * @flags: special case
734 * This is the important Smack hook.
736 * Returns 0 if access is permitted, -EACCES otherwise
738 static int smack_inode_permission(struct inode *inode, int mask, unsigned flags)
740 struct smk_audit_info ad;
742 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
744 * No permission to check. Existence test. Yup, it's there.
749 /* May be droppable after audit */
750 if (flags & IPERM_FLAG_RCU)
753 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
754 smk_ad_setfield_u_fs_inode(&ad, inode);
755 return smk_curacc(smk_of_inode(inode), mask, &ad);
759 * smack_inode_setattr - Smack check for setting attributes
760 * @dentry: the object
761 * @iattr: for the force flag
763 * Returns 0 if access is permitted, an error code otherwise
765 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
767 struct smk_audit_info ad;
769 * Need to allow for clearing the setuid bit.
771 if (iattr->ia_valid & ATTR_FORCE)
773 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
774 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
776 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
780 * smack_inode_getattr - Smack check for getting attributes
782 * @dentry: the object
784 * Returns 0 if access is permitted, an error code otherwise
786 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
788 struct smk_audit_info ad;
791 path.dentry = dentry;
794 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
795 smk_ad_setfield_u_fs_path(&ad, path);
796 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
800 * smack_inode_setxattr - Smack check for setting xattrs
801 * @dentry: the object
802 * @name: name of the attribute
807 * This protects the Smack attribute explicitly.
809 * Returns 0 if access is permitted, an error code otherwise
811 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
812 const void *value, size_t size, int flags)
814 struct smk_audit_info ad;
817 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
818 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
819 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
820 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
821 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
822 if (!smack_privileged(CAP_MAC_ADMIN))
825 * check label validity here so import wont fail on
828 if (size == 0 || size >= SMK_LONGLABEL ||
829 smk_import(value, size) == NULL)
831 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
832 if (!smack_privileged(CAP_MAC_ADMIN))
834 if (size != TRANS_TRUE_SIZE ||
835 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
838 rc = cap_inode_setxattr(dentry, name, value, size, flags);
840 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
841 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
844 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
850 * smack_inode_post_setxattr - Apply the Smack update approved above
852 * @name: attribute name
853 * @value: attribute value
854 * @size: attribute size
857 * Set the pointer in the inode blob to the entry found
858 * in the master label list.
860 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
861 const void *value, size_t size, int flags)
864 struct inode_smack *isp = dentry->d_inode->i_security;
866 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
867 nsp = smk_import(value, size);
869 isp->smk_inode = nsp;
871 isp->smk_inode = smack_known_invalid.smk_known;
872 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
873 nsp = smk_import(value, size);
877 isp->smk_task = smack_known_invalid.smk_known;
878 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
879 nsp = smk_import(value, size);
883 isp->smk_mmap = smack_known_invalid.smk_known;
884 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
885 isp->smk_flags |= SMK_INODE_TRANSMUTE;
891 * smack_inode_getxattr - Smack check on getxattr
892 * @dentry: the object
895 * Returns 0 if access is permitted, an error code otherwise
897 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
899 struct smk_audit_info ad;
901 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
902 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
904 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
908 * smack_inode_removexattr - Smack check on removexattr
909 * @dentry: the object
910 * @name: name of the attribute
912 * Removing the Smack attribute requires CAP_MAC_ADMIN
914 * Returns 0 if access is permitted, an error code otherwise
916 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
918 struct inode_smack *isp;
919 struct smk_audit_info ad;
922 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
923 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
924 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
925 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
926 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
927 strcmp(name, XATTR_NAME_SMACKMMAP)) {
928 if (!smack_privileged(CAP_MAC_ADMIN))
931 rc = cap_inode_removexattr(dentry, name);
933 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
934 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
936 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
939 isp = dentry->d_inode->i_security;
940 isp->smk_task = NULL;
941 isp->smk_mmap = NULL;
948 * smack_inode_getsecurity - get smack xattrs
950 * @name: attribute name
951 * @buffer: where to put the result
954 * Returns the size of the attribute or an error code
956 static int smack_inode_getsecurity(const struct inode *inode,
957 const char *name, void **buffer,
960 struct socket_smack *ssp;
962 struct super_block *sbp;
963 struct inode *ip = (struct inode *)inode;
968 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
969 isp = smk_of_inode(inode);
970 ilen = strlen(isp) + 1;
976 * The rest of the Smack xattrs are only on sockets.
979 if (sbp->s_magic != SOCKFS_MAGIC)
983 if (sock == NULL || sock->sk == NULL)
986 ssp = sock->sk->sk_security;
988 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
990 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
995 ilen = strlen(isp) + 1;
1006 * smack_inode_listsecurity - list the Smack attributes
1007 * @inode: the object
1008 * @buffer: where they go
1009 * @buffer_size: size of buffer
1011 * Returns 0 on success, -EINVAL otherwise
1013 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1016 int len = strlen(XATTR_NAME_SMACK);
1018 if (buffer != NULL && len <= buffer_size) {
1019 memcpy(buffer, XATTR_NAME_SMACK, len);
1026 * smack_inode_getsecid - Extract inode's security id
1027 * @inode: inode to extract the info from
1028 * @secid: where result will be saved
1030 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1032 struct inode_smack *isp = inode->i_security;
1034 *secid = smack_to_secid(isp->smk_inode);
1042 * smack_file_permission - Smack check on file operations
1048 * Should access checks be done on each read or write?
1049 * UNICOS and SELinux say yes.
1050 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1052 * I'll say no for now. Smack does not do the frequent
1053 * label changing that SELinux does.
1055 static int smack_file_permission(struct file *file, int mask)
1061 * smack_file_alloc_security - assign a file security blob
1064 * The security blob for a file is a pointer to the master
1065 * label list, so no allocation is done.
1069 static int smack_file_alloc_security(struct file *file)
1071 file->f_security = smk_of_current();
1076 * smack_file_free_security - clear a file security blob
1079 * The security blob for a file is a pointer to the master
1080 * label list, so no memory is freed.
1082 static void smack_file_free_security(struct file *file)
1084 file->f_security = NULL;
1088 * smack_file_ioctl - Smack check on ioctls
1093 * Relies heavily on the correct use of the ioctl command conventions.
1095 * Returns 0 if allowed, error code otherwise
1097 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1101 struct smk_audit_info ad;
1103 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1104 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1106 if (_IOC_DIR(cmd) & _IOC_WRITE)
1107 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1109 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1110 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1116 * smack_file_lock - Smack check on file locking
1120 * Returns 0 if current has lock access, error code otherwise
1122 static int smack_file_lock(struct file *file, unsigned int cmd)
1124 struct smk_audit_info ad;
1126 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1127 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1128 return smk_curacc(file->f_security, MAY_LOCK, &ad);
1132 * smack_file_fcntl - Smack check on fcntl
1134 * @cmd: what action to check
1137 * Generally these operations are harmless.
1138 * File locking operations present an obvious mechanism
1139 * for passing information, so they require write access.
1141 * Returns 0 if current has access, error code otherwise
1143 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1146 struct smk_audit_info ad;
1155 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1156 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1157 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1161 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1162 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1163 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1174 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1175 * if mapping anonymous memory.
1176 * @file contains the file structure for file to map (may be NULL).
1177 * @reqprot contains the protection requested by the application.
1178 * @prot contains the protection that will be applied by the kernel.
1179 * @flags contains the operational flags.
1180 * Return 0 if permission is granted.
1182 static int smack_file_mmap(struct file *file,
1183 unsigned long reqprot, unsigned long prot,
1184 unsigned long flags, unsigned long addr,
1185 unsigned long addr_only)
1187 struct smack_known *skp;
1188 struct smack_rule *srp;
1189 struct task_smack *tsp;
1193 struct inode_smack *isp;
1200 /* do DAC check on address space usage */
1201 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1202 if (rc || addr_only)
1205 if (file == NULL || file->f_dentry == NULL)
1208 dp = file->f_dentry;
1210 if (dp->d_inode == NULL)
1213 isp = dp->d_inode->i_security;
1214 if (isp->smk_mmap == NULL)
1216 msmack = isp->smk_mmap;
1218 tsp = current_security();
1219 sp = smk_of_current();
1220 skp = smk_find_entry(sp);
1225 * For each Smack rule associated with the subject
1226 * label verify that the SMACK64MMAP also has access
1227 * to that rule's object label.
1229 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1230 osmack = srp->smk_object;
1232 * Matching labels always allows access.
1234 if (msmack == osmack)
1237 * If there is a matching local rule take
1238 * that into account as well.
1240 may = smk_access_entry(srp->smk_subject, osmack,
1243 may = srp->smk_access;
1245 may &= srp->smk_access;
1247 * If may is zero the SMACK64MMAP subject can't
1248 * possibly have less access.
1254 * Fetch the global list entry.
1255 * If there isn't one a SMACK64MMAP subject
1256 * can't have as much access as current.
1258 skp = smk_find_entry(msmack);
1259 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
1260 if (mmay == -ENOENT) {
1265 * If there is a local entry it modifies the
1266 * potential access, too.
1268 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1269 if (tmay != -ENOENT)
1273 * If there is any access available to current that is
1274 * not available to a SMACK64MMAP subject
1277 if ((may | mmay) != mmay) {
1289 * smack_file_set_fowner - set the file security blob value
1290 * @file: object in question
1293 * Further research may be required on this one.
1295 static int smack_file_set_fowner(struct file *file)
1297 file->f_security = smk_of_current();
1302 * smack_file_send_sigiotask - Smack on sigio
1303 * @tsk: The target task
1304 * @fown: the object the signal come from
1307 * Allow a privileged task to get signals even if it shouldn't
1309 * Returns 0 if a subject with the object's smack could
1310 * write to the task, an error code otherwise.
1312 static int smack_file_send_sigiotask(struct task_struct *tsk,
1313 struct fown_struct *fown, int signum)
1317 char *tsp = smk_of_task(tsk->cred->security);
1318 struct smk_audit_info ad;
1321 * struct fown_struct is never outside the context of a struct file
1323 file = container_of(fown, struct file, f_owner);
1325 /* we don't log here as rc can be overriden */
1326 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
1327 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1330 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1331 smk_ad_setfield_u_tsk(&ad, tsk);
1332 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
1337 * smack_file_receive - Smack file receive check
1340 * Returns 0 if current has access, error code otherwise
1342 static int smack_file_receive(struct file *file)
1345 struct smk_audit_info ad;
1347 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1348 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1350 * This code relies on bitmasks.
1352 if (file->f_mode & FMODE_READ)
1354 if (file->f_mode & FMODE_WRITE)
1357 return smk_curacc(file->f_security, may, &ad);
1361 * smack_dentry_open - Smack dentry open processing
1365 * Set the security blob in the file structure.
1369 static int smack_dentry_open(struct file *file, const struct cred *cred)
1371 struct inode_smack *isp = file->f_path.dentry->d_inode->i_security;
1373 file->f_security = isp->smk_inode;
1383 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1384 * @new: the new credentials
1385 * @gfp: the atomicity of any memory allocations
1387 * Prepare a blank set of credentials for modification. This must allocate all
1388 * the memory the LSM module might require such that cred_transfer() can
1389 * complete without error.
1391 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1393 struct task_smack *tsp;
1395 tsp = new_task_smack(NULL, NULL, gfp);
1399 cred->security = tsp;
1406 * smack_cred_free - "free" task-level security credentials
1407 * @cred: the credentials in question
1410 static void smack_cred_free(struct cred *cred)
1412 struct task_smack *tsp = cred->security;
1413 struct smack_rule *rp;
1414 struct list_head *l;
1415 struct list_head *n;
1419 cred->security = NULL;
1421 list_for_each_safe(l, n, &tsp->smk_rules) {
1422 rp = list_entry(l, struct smack_rule, list);
1423 list_del(&rp->list);
1430 * smack_cred_prepare - prepare new set of credentials for modification
1431 * @new: the new credentials
1432 * @old: the original credentials
1433 * @gfp: the atomicity of any memory allocations
1435 * Prepare a new set of credentials for modification.
1437 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1440 struct task_smack *old_tsp = old->security;
1441 struct task_smack *new_tsp;
1444 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1445 if (new_tsp == NULL)
1448 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1452 new->security = new_tsp;
1457 * smack_cred_transfer - Transfer the old credentials to the new credentials
1458 * @new: the new credentials
1459 * @old: the original credentials
1461 * Fill in a set of blank credentials from another set of credentials.
1463 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1465 struct task_smack *old_tsp = old->security;
1466 struct task_smack *new_tsp = new->security;
1468 new_tsp->smk_task = old_tsp->smk_task;
1469 new_tsp->smk_forked = old_tsp->smk_task;
1470 mutex_init(&new_tsp->smk_rules_lock);
1471 INIT_LIST_HEAD(&new_tsp->smk_rules);
1474 /* cbs copy rule list */
1478 * smack_kernel_act_as - Set the subjective context in a set of credentials
1479 * @new: points to the set of credentials to be modified.
1480 * @secid: specifies the security ID to be set
1482 * Set the security data for a kernel service.
1484 static int smack_kernel_act_as(struct cred *new, u32 secid)
1486 struct task_smack *new_tsp = new->security;
1487 char *smack = smack_from_secid(secid);
1492 new_tsp->smk_task = smack;
1497 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1498 * @new: points to the set of credentials to be modified
1499 * @inode: points to the inode to use as a reference
1501 * Set the file creation context in a set of credentials to the same
1502 * as the objective context of the specified inode
1504 static int smack_kernel_create_files_as(struct cred *new,
1505 struct inode *inode)
1507 struct inode_smack *isp = inode->i_security;
1508 struct task_smack *tsp = new->security;
1510 tsp->smk_forked = isp->smk_inode;
1511 tsp->smk_task = isp->smk_inode;
1516 * smk_curacc_on_task - helper to log task related access
1517 * @p: the task object
1518 * @access: the access requested
1519 * @caller: name of the calling function for audit
1521 * Return 0 if access is permitted
1523 static int smk_curacc_on_task(struct task_struct *p, int access,
1526 struct smk_audit_info ad;
1528 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1529 smk_ad_setfield_u_tsk(&ad, p);
1530 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
1534 * smack_task_setpgid - Smack check on setting pgid
1535 * @p: the task object
1538 * Return 0 if write access is permitted
1540 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1542 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1546 * smack_task_getpgid - Smack access check for getpgid
1547 * @p: the object task
1549 * Returns 0 if current can read the object task, error code otherwise
1551 static int smack_task_getpgid(struct task_struct *p)
1553 return smk_curacc_on_task(p, MAY_READ, __func__);
1557 * smack_task_getsid - Smack access check for getsid
1558 * @p: the object task
1560 * Returns 0 if current can read the object task, error code otherwise
1562 static int smack_task_getsid(struct task_struct *p)
1564 return smk_curacc_on_task(p, MAY_READ, __func__);
1568 * smack_task_getsecid - get the secid of the task
1569 * @p: the object task
1570 * @secid: where to put the result
1572 * Sets the secid to contain a u32 version of the smack label.
1574 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1576 *secid = smack_to_secid(smk_of_task(task_security(p)));
1580 * smack_task_setnice - Smack check on setting nice
1581 * @p: the task object
1584 * Return 0 if write access is permitted
1586 static int smack_task_setnice(struct task_struct *p, int nice)
1590 rc = cap_task_setnice(p, nice);
1592 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1597 * smack_task_setioprio - Smack check on setting ioprio
1598 * @p: the task object
1601 * Return 0 if write access is permitted
1603 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1607 rc = cap_task_setioprio(p, ioprio);
1609 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1614 * smack_task_getioprio - Smack check on reading ioprio
1615 * @p: the task object
1617 * Return 0 if read access is permitted
1619 static int smack_task_getioprio(struct task_struct *p)
1621 return smk_curacc_on_task(p, MAY_READ, __func__);
1625 * smack_task_setscheduler - Smack check on setting scheduler
1626 * @p: the task object
1630 * Return 0 if read access is permitted
1632 static int smack_task_setscheduler(struct task_struct *p)
1636 rc = cap_task_setscheduler(p);
1638 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1643 * smack_task_getscheduler - Smack check on reading scheduler
1644 * @p: the task object
1646 * Return 0 if read access is permitted
1648 static int smack_task_getscheduler(struct task_struct *p)
1650 return smk_curacc_on_task(p, MAY_READ, __func__);
1654 * smack_task_movememory - Smack check on moving memory
1655 * @p: the task object
1657 * Return 0 if write access is permitted
1659 static int smack_task_movememory(struct task_struct *p)
1661 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1665 * smack_task_kill - Smack check on signal delivery
1666 * @p: the task object
1669 * @secid: identifies the smack to use in lieu of current's
1671 * Return 0 if write access is permitted
1673 * The secid behavior is an artifact of an SELinux hack
1674 * in the USB code. Someday it may go away.
1676 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1679 struct smk_audit_info ad;
1681 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1682 smk_ad_setfield_u_tsk(&ad, p);
1684 * Sending a signal requires that the sender
1685 * can write the receiver.
1688 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1691 * If the secid isn't 0 we're dealing with some USB IO
1692 * specific behavior. This is not clean. For one thing
1693 * we can't take privilege into account.
1695 return smk_access(smack_from_secid(secid),
1696 smk_of_task(task_security(p)), MAY_WRITE, &ad);
1700 * smack_task_wait - Smack access check for waiting
1701 * @p: task to wait for
1705 static int smack_task_wait(struct task_struct *p)
1708 * Allow the operation to succeed.
1710 * In userless environments (e.g. phones) programs
1711 * get marked with SMACK64EXEC and even if the parent
1712 * and child shouldn't be talking the parent still
1713 * may expect to know when the child exits.
1719 * smack_task_to_inode - copy task smack into the inode blob
1720 * @p: task to copy from
1721 * @inode: inode to copy to
1723 * Sets the smack pointer in the inode security blob
1725 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1727 struct inode_smack *isp = inode->i_security;
1728 isp->smk_inode = smk_of_task(task_security(p));
1736 * smack_sk_alloc_security - Allocate a socket blob
1739 * @gfp_flags: memory allocation flags
1741 * Assign Smack pointers to current
1743 * Returns 0 on success, -ENOMEM is there's no memory
1745 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1747 char *csp = smk_of_current();
1748 struct socket_smack *ssp;
1750 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1756 ssp->smk_packet = NULL;
1758 sk->sk_security = ssp;
1764 * smack_sk_free_security - Free a socket blob
1767 * Clears the blob pointer
1769 static void smack_sk_free_security(struct sock *sk)
1771 kfree(sk->sk_security);
1775 * smack_host_label - check host based restrictions
1776 * @sip: the object end
1778 * looks for host based access restrictions
1780 * This version will only be appropriate for really small sets of single label
1781 * hosts. The caller is responsible for ensuring that the RCU read lock is
1782 * taken before calling this function.
1784 * Returns the label of the far end or NULL if it's not special.
1786 static char *smack_host_label(struct sockaddr_in *sip)
1788 struct smk_netlbladdr *snp;
1789 struct in_addr *siap = &sip->sin_addr;
1791 if (siap->s_addr == 0)
1794 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1796 * we break after finding the first match because
1797 * the list is sorted from longest to shortest mask
1798 * so we have found the most specific match
1800 if ((&snp->smk_host.sin_addr)->s_addr ==
1801 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1802 /* we have found the special CIPSO option */
1803 if (snp->smk_label == smack_cipso_option)
1805 return snp->smk_label;
1812 * smack_netlabel - Set the secattr on a socket
1814 * @labeled: socket label scheme
1816 * Convert the outbound smack value (smk_out) to a
1817 * secattr and attach it to the socket.
1819 * Returns 0 on success or an error code
1821 static int smack_netlabel(struct sock *sk, int labeled)
1823 struct smack_known *skp;
1824 struct socket_smack *ssp = sk->sk_security;
1828 * Usually the netlabel code will handle changing the
1829 * packet labeling based on the label.
1830 * The case of a single label host is different, because
1831 * a single label host should never get a labeled packet
1832 * even though the label is usually associated with a packet
1836 bh_lock_sock_nested(sk);
1838 if (ssp->smk_out == smack_net_ambient ||
1839 labeled == SMACK_UNLABELED_SOCKET)
1840 netlbl_sock_delattr(sk);
1842 skp = smk_find_entry(ssp->smk_out);
1843 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
1853 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1855 * @sap: the destination address
1857 * Set the correct secattr for the given socket based on the destination
1858 * address and perform any outbound access checks needed.
1860 * Returns 0 on success or an error code.
1863 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1868 struct socket_smack *ssp = sk->sk_security;
1869 struct smk_audit_info ad;
1872 hostsp = smack_host_label(sap);
1873 if (hostsp != NULL) {
1874 sk_lbl = SMACK_UNLABELED_SOCKET;
1876 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1877 ad.a.u.net.family = sap->sin_family;
1878 ad.a.u.net.dport = sap->sin_port;
1879 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1881 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
1883 sk_lbl = SMACK_CIPSO_SOCKET;
1890 return smack_netlabel(sk, sk_lbl);
1894 * smack_inode_setsecurity - set smack xattrs
1895 * @inode: the object
1896 * @name: attribute name
1897 * @value: attribute value
1898 * @size: size of the attribute
1901 * Sets the named attribute in the appropriate blob
1903 * Returns 0 on success, or an error code
1905 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1906 const void *value, size_t size, int flags)
1909 struct inode_smack *nsp = inode->i_security;
1910 struct socket_smack *ssp;
1911 struct socket *sock;
1914 if (value == NULL || size > SMK_LONGLABEL || size == 0)
1917 sp = smk_import(value, size);
1921 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1922 nsp->smk_inode = sp;
1923 nsp->smk_flags |= SMK_INODE_INSTANT;
1927 * The rest of the Smack xattrs are only on sockets.
1929 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1932 sock = SOCKET_I(inode);
1933 if (sock == NULL || sock->sk == NULL)
1936 ssp = sock->sk->sk_security;
1938 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1940 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1942 if (sock->sk->sk_family != PF_UNIX) {
1943 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1946 "Smack: \"%s\" netlbl error %d.\n",
1956 * smack_socket_post_create - finish socket setup
1958 * @family: protocol family
1963 * Sets the netlabel information on the socket
1965 * Returns 0 on success, and error code otherwise
1967 static int smack_socket_post_create(struct socket *sock, int family,
1968 int type, int protocol, int kern)
1970 if (family != PF_INET || sock->sk == NULL)
1973 * Set the outbound netlbl.
1975 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1979 * smack_socket_connect - connect access check
1981 * @sap: the other end
1982 * @addrlen: size of sap
1984 * Verifies that a connection may be possible
1986 * Returns 0 on success, and error code otherwise
1988 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1991 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1993 if (addrlen < sizeof(struct sockaddr_in))
1996 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2000 * smack_flags_to_may - convert S_ to MAY_ values
2001 * @flags: the S_ value
2003 * Returns the equivalent MAY_ value
2005 static int smack_flags_to_may(int flags)
2009 if (flags & S_IRUGO)
2011 if (flags & S_IWUGO)
2013 if (flags & S_IXUGO)
2020 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2025 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2027 msg->security = smk_of_current();
2032 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2035 * Clears the blob pointer
2037 static void smack_msg_msg_free_security(struct msg_msg *msg)
2039 msg->security = NULL;
2043 * smack_of_shm - the smack pointer for the shm
2046 * Returns a pointer to the smack value
2048 static char *smack_of_shm(struct shmid_kernel *shp)
2050 return (char *)shp->shm_perm.security;
2054 * smack_shm_alloc_security - Set the security blob for shm
2059 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2061 struct kern_ipc_perm *isp = &shp->shm_perm;
2063 isp->security = smk_of_current();
2068 * smack_shm_free_security - Clear the security blob for shm
2071 * Clears the blob pointer
2073 static void smack_shm_free_security(struct shmid_kernel *shp)
2075 struct kern_ipc_perm *isp = &shp->shm_perm;
2077 isp->security = NULL;
2081 * smk_curacc_shm : check if current has access on shm
2083 * @access : access requested
2085 * Returns 0 if current has the requested access, error code otherwise
2087 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2089 char *ssp = smack_of_shm(shp);
2090 struct smk_audit_info ad;
2093 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2094 ad.a.u.ipc_id = shp->shm_perm.id;
2096 return smk_curacc(ssp, access, &ad);
2100 * smack_shm_associate - Smack access check for shm
2102 * @shmflg: access requested
2104 * Returns 0 if current has the requested access, error code otherwise
2106 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2110 may = smack_flags_to_may(shmflg);
2111 return smk_curacc_shm(shp, may);
2115 * smack_shm_shmctl - Smack access check for shm
2117 * @cmd: what it wants to do
2119 * Returns 0 if current has the requested access, error code otherwise
2121 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2134 may = MAY_READWRITE;
2139 * System level information.
2145 return smk_curacc_shm(shp, may);
2149 * smack_shm_shmat - Smack access for shmat
2152 * @shmflg: access requested
2154 * Returns 0 if current has the requested access, error code otherwise
2156 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2161 may = smack_flags_to_may(shmflg);
2162 return smk_curacc_shm(shp, may);
2166 * smack_of_sem - the smack pointer for the sem
2169 * Returns a pointer to the smack value
2171 static char *smack_of_sem(struct sem_array *sma)
2173 return (char *)sma->sem_perm.security;
2177 * smack_sem_alloc_security - Set the security blob for sem
2182 static int smack_sem_alloc_security(struct sem_array *sma)
2184 struct kern_ipc_perm *isp = &sma->sem_perm;
2186 isp->security = smk_of_current();
2191 * smack_sem_free_security - Clear the security blob for sem
2194 * Clears the blob pointer
2196 static void smack_sem_free_security(struct sem_array *sma)
2198 struct kern_ipc_perm *isp = &sma->sem_perm;
2200 isp->security = NULL;
2204 * smk_curacc_sem : check if current has access on sem
2206 * @access : access requested
2208 * Returns 0 if current has the requested access, error code otherwise
2210 static int smk_curacc_sem(struct sem_array *sma, int access)
2212 char *ssp = smack_of_sem(sma);
2213 struct smk_audit_info ad;
2216 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2217 ad.a.u.ipc_id = sma->sem_perm.id;
2219 return smk_curacc(ssp, access, &ad);
2223 * smack_sem_associate - Smack access check for sem
2225 * @semflg: access requested
2227 * Returns 0 if current has the requested access, error code otherwise
2229 static int smack_sem_associate(struct sem_array *sma, int semflg)
2233 may = smack_flags_to_may(semflg);
2234 return smk_curacc_sem(sma, may);
2238 * smack_sem_shmctl - Smack access check for sem
2240 * @cmd: what it wants to do
2242 * Returns 0 if current has the requested access, error code otherwise
2244 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2262 may = MAY_READWRITE;
2267 * System level information
2274 return smk_curacc_sem(sma, may);
2278 * smack_sem_semop - Smack checks of semaphore operations
2284 * Treated as read and write in all cases.
2286 * Returns 0 if access is allowed, error code otherwise
2288 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2289 unsigned nsops, int alter)
2291 return smk_curacc_sem(sma, MAY_READWRITE);
2295 * smack_msg_alloc_security - Set the security blob for msg
2300 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2302 struct kern_ipc_perm *kisp = &msq->q_perm;
2304 kisp->security = smk_of_current();
2309 * smack_msg_free_security - Clear the security blob for msg
2312 * Clears the blob pointer
2314 static void smack_msg_queue_free_security(struct msg_queue *msq)
2316 struct kern_ipc_perm *kisp = &msq->q_perm;
2318 kisp->security = NULL;
2322 * smack_of_msq - the smack pointer for the msq
2325 * Returns a pointer to the smack value
2327 static char *smack_of_msq(struct msg_queue *msq)
2329 return (char *)msq->q_perm.security;
2333 * smk_curacc_msq : helper to check if current has access on msq
2335 * @access : access requested
2337 * return 0 if current has access, error otherwise
2339 static int smk_curacc_msq(struct msg_queue *msq, int access)
2341 char *msp = smack_of_msq(msq);
2342 struct smk_audit_info ad;
2345 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2346 ad.a.u.ipc_id = msq->q_perm.id;
2348 return smk_curacc(msp, access, &ad);
2352 * smack_msg_queue_associate - Smack access check for msg_queue
2354 * @msqflg: access requested
2356 * Returns 0 if current has the requested access, error code otherwise
2358 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2362 may = smack_flags_to_may(msqflg);
2363 return smk_curacc_msq(msq, may);
2367 * smack_msg_queue_msgctl - Smack access check for msg_queue
2369 * @cmd: what it wants to do
2371 * Returns 0 if current has the requested access, error code otherwise
2373 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2384 may = MAY_READWRITE;
2389 * System level information
2396 return smk_curacc_msq(msq, may);
2400 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2403 * @msqflg: access requested
2405 * Returns 0 if current has the requested access, error code otherwise
2407 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2412 may = smack_flags_to_may(msqflg);
2413 return smk_curacc_msq(msq, may);
2417 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2424 * Returns 0 if current has read and write access, error code otherwise
2426 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2427 struct task_struct *target, long type, int mode)
2429 return smk_curacc_msq(msq, MAY_READWRITE);
2433 * smack_ipc_permission - Smack access for ipc_permission()
2434 * @ipp: the object permissions
2435 * @flag: access requested
2437 * Returns 0 if current has read and write access, error code otherwise
2439 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2441 char *isp = ipp->security;
2442 int may = smack_flags_to_may(flag);
2443 struct smk_audit_info ad;
2446 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2447 ad.a.u.ipc_id = ipp->id;
2449 return smk_curacc(isp, may, &ad);
2453 * smack_ipc_getsecid - Extract smack security id
2454 * @ipp: the object permissions
2455 * @secid: where result will be saved
2457 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2459 char *smack = ipp->security;
2461 *secid = smack_to_secid(smack);
2465 * smack_d_instantiate - Make sure the blob is correct on an inode
2466 * @opt_dentry: dentry where inode will be attached
2467 * @inode: the object
2469 * Set the inode's security blob if it hasn't been done already.
2471 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2473 struct super_block *sbp;
2474 struct superblock_smack *sbsp;
2475 struct inode_smack *isp;
2476 char *csp = smk_of_current();
2479 char trattr[TRANS_TRUE_SIZE];
2487 isp = inode->i_security;
2489 mutex_lock(&isp->smk_lock);
2491 * If the inode is already instantiated
2492 * take the quick way out
2494 if (isp->smk_flags & SMK_INODE_INSTANT)
2498 sbsp = sbp->s_security;
2500 * We're going to use the superblock default label
2501 * if there's no label on the file.
2503 final = sbsp->smk_default;
2506 * If this is the root inode the superblock
2507 * may be in the process of initialization.
2508 * If that is the case use the root value out
2509 * of the superblock.
2511 if (opt_dentry->d_parent == opt_dentry) {
2512 isp->smk_inode = sbsp->smk_root;
2513 isp->smk_flags |= SMK_INODE_INSTANT;
2518 * This is pretty hackish.
2519 * Casey says that we shouldn't have to do
2520 * file system specific code, but it does help
2521 * with keeping it simple.
2523 switch (sbp->s_magic) {
2526 * Casey says that it's a little embarrassing
2527 * that the smack file system doesn't do
2528 * extended attributes.
2530 final = smack_known_star.smk_known;
2534 * Casey says pipes are easy (?)
2536 final = smack_known_star.smk_known;
2538 case DEVPTS_SUPER_MAGIC:
2540 * devpts seems content with the label of the task.
2541 * Programs that change smack have to treat the
2548 * Socket access is controlled by the socket
2549 * structures associated with the task involved.
2551 final = smack_known_star.smk_known;
2553 case PROC_SUPER_MAGIC:
2555 * Casey says procfs appears not to care.
2556 * The superblock default suffices.
2561 * Device labels should come from the filesystem,
2562 * but watch out, because they're volitile,
2563 * getting recreated on every reboot.
2565 final = smack_known_star.smk_known;
2569 * If a smack value has been set we want to use it,
2570 * but since tmpfs isn't giving us the opportunity
2571 * to set mount options simulate setting the
2572 * superblock default.
2576 * This isn't an understood special case.
2577 * Get the value from the xattr.
2581 * UNIX domain sockets use lower level socket data.
2583 if (S_ISSOCK(inode->i_mode)) {
2584 final = smack_known_star.smk_known;
2588 * No xattr support means, alas, no SMACK label.
2589 * Use the aforeapplied default.
2590 * It would be curious if the label of the task
2591 * does not match that assigned.
2593 if (inode->i_op->getxattr == NULL)
2596 * Get the dentry for xattr.
2598 dp = dget(opt_dentry);
2599 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2600 if (fetched != NULL)
2604 * Transmuting directory
2606 if (S_ISDIR(inode->i_mode)) {
2608 * If this is a new directory and the label was
2609 * transmuted when the inode was initialized
2610 * set the transmute attribute on the directory
2611 * and mark the inode.
2613 * If there is a transmute attribute on the
2614 * directory mark the inode.
2616 if (isp->smk_flags & SMK_INODE_CHANGED) {
2617 isp->smk_flags &= ~SMK_INODE_CHANGED;
2618 rc = inode->i_op->setxattr(dp,
2619 XATTR_NAME_SMACKTRANSMUTE,
2620 TRANS_TRUE, TRANS_TRUE_SIZE,
2623 rc = inode->i_op->getxattr(dp,
2624 XATTR_NAME_SMACKTRANSMUTE, trattr,
2626 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2627 TRANS_TRUE_SIZE) != 0)
2631 transflag = SMK_INODE_TRANSMUTE;
2633 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2634 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2641 isp->smk_inode = csp;
2643 isp->smk_inode = final;
2645 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2648 mutex_unlock(&isp->smk_lock);
2653 * smack_getprocattr - Smack process attribute access
2654 * @p: the object task
2655 * @name: the name of the attribute in /proc/.../attr
2656 * @value: where to put the result
2658 * Places a copy of the task Smack into value
2660 * Returns the length of the smack label or an error code
2662 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2667 if (strcmp(name, "current") != 0)
2670 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
2680 * smack_setprocattr - Smack process attribute setting
2681 * @p: the object task
2682 * @name: the name of the attribute in /proc/.../attr
2683 * @value: the value to set
2684 * @size: the size of the value
2686 * Sets the Smack value of the task. Only setting self
2687 * is permitted and only with privilege
2689 * Returns the length of the smack label or an error code
2691 static int smack_setprocattr(struct task_struct *p, char *name,
2692 void *value, size_t size)
2694 struct task_smack *tsp;
2699 * Changing another process' Smack value is too dangerous
2700 * and supports no sane use case.
2705 if (!smack_privileged(CAP_MAC_ADMIN))
2708 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
2711 if (strcmp(name, "current") != 0)
2714 newsmack = smk_import(value, size);
2715 if (newsmack == NULL)
2719 * No process is ever allowed the web ("@") label.
2721 if (newsmack == smack_known_web.smk_known)
2724 new = prepare_creds();
2728 tsp = new->security;
2729 tsp->smk_task = newsmack;
2736 * smack_unix_stream_connect - Smack access on UDS
2738 * @other: the other sock
2741 * Return 0 if a subject with the smack of sock could access
2742 * an object with the smack of other, otherwise an error code
2744 static int smack_unix_stream_connect(struct sock *sock,
2745 struct sock *other, struct sock *newsk)
2747 struct socket_smack *ssp = sock->sk_security;
2748 struct socket_smack *osp = other->sk_security;
2749 struct socket_smack *nsp = newsk->sk_security;
2750 struct smk_audit_info ad;
2753 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2754 smk_ad_setfield_u_net_sk(&ad, other);
2756 if (!smack_privileged(CAP_MAC_OVERRIDE))
2757 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2760 * Cross reference the peer labels for SO_PEERSEC.
2763 nsp->smk_packet = ssp->smk_out;
2764 ssp->smk_packet = osp->smk_out;
2771 * smack_unix_may_send - Smack access on UDS
2773 * @other: the other socket
2775 * Return 0 if a subject with the smack of sock could access
2776 * an object with the smack of other, otherwise an error code
2778 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2780 struct socket_smack *ssp = sock->sk->sk_security;
2781 struct socket_smack *osp = other->sk->sk_security;
2782 struct smk_audit_info ad;
2785 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2786 smk_ad_setfield_u_net_sk(&ad, other->sk);
2788 if (!smack_privileged(CAP_MAC_OVERRIDE))
2789 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2795 * smack_socket_sendmsg - Smack check based on destination host
2798 * @size: the size of the message
2800 * Return 0 if the current subject can write to the destination
2801 * host. This is only a question if the destination is a single
2804 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2807 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2810 * Perfectly reasonable for this to be NULL
2812 if (sip == NULL || sip->sin_family != AF_INET)
2815 return smack_netlabel_send(sock->sk, sip);
2819 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2820 * @sap: netlabel secattr
2821 * @ssp: socket security information
2823 * Returns a pointer to a Smack label found on the label list.
2825 static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
2826 struct socket_smack *ssp)
2828 struct smack_known *kp;
2834 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2836 * Looks like a CIPSO packet.
2837 * If there are flags but no level netlabel isn't
2838 * behaving the way we expect it to.
2840 * Look it up in the label table
2841 * Without guidance regarding the smack value
2842 * for the packet fall back on the network
2846 list_for_each_entry(kp, &smack_known_list, list) {
2847 if (sap->attr.mls.lvl != kp->smk_netlabel.attr.mls.lvl)
2850 * Compare the catsets. Use the netlbl APIs.
2852 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
2853 if ((kp->smk_netlabel.flags &
2854 NETLBL_SECATTR_MLS_CAT) == 0)
2858 for (acat = -1, kcat = -1; acat == kcat; ) {
2859 acat = netlbl_secattr_catmap_walk(
2860 sap->attr.mls.cat, acat + 1);
2861 kcat = netlbl_secattr_catmap_walk(
2862 kp->smk_netlabel.attr.mls.cat,
2864 if (acat < 0 || kcat < 0)
2875 return kp->smk_known;
2877 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
2878 return smack_known_web.smk_known;
2879 return smack_known_star.smk_known;
2881 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2883 * Looks like a fallback, which gives us a secid.
2885 sp = smack_from_secid(sap->attr.secid);
2887 * This has got to be a bug because it is
2888 * impossible to specify a fallback without
2889 * specifying the label, which will ensure
2890 * it has a secid, and the only way to get a
2891 * secid is from a fallback.
2897 * Without guidance regarding the smack value
2898 * for the packet fall back on the network
2901 return smack_net_ambient;
2905 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2909 * Returns 0 if the packet should be delivered, an error code otherwise
2911 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2913 struct netlbl_lsm_secattr secattr;
2914 struct socket_smack *ssp = sk->sk_security;
2917 struct smk_audit_info ad;
2918 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2922 * Translate what netlabel gave us.
2924 netlbl_secattr_init(&secattr);
2926 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2928 csp = smack_from_secattr(&secattr, ssp);
2930 csp = smack_net_ambient;
2932 netlbl_secattr_destroy(&secattr);
2935 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2936 ad.a.u.net.family = sk->sk_family;
2937 ad.a.u.net.netif = skb->skb_iif;
2938 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2941 * Receiving a packet requires that the other end
2942 * be able to write here. Read access is not required.
2943 * This is the simplist possible security model
2946 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
2948 netlbl_skbuff_err(skb, rc, 0);
2953 * smack_socket_getpeersec_stream - pull in packet label
2955 * @optval: user's destination
2956 * @optlen: size thereof
2959 * returns zero on success, an error code otherwise
2961 static int smack_socket_getpeersec_stream(struct socket *sock,
2962 char __user *optval,
2963 int __user *optlen, unsigned len)
2965 struct socket_smack *ssp;
2970 ssp = sock->sk->sk_security;
2971 if (ssp->smk_packet != NULL) {
2972 rcp = ssp->smk_packet;
2973 slen = strlen(rcp) + 1;
2978 else if (copy_to_user(optval, rcp, slen) != 0)
2981 if (put_user(slen, optlen) != 0)
2989 * smack_socket_getpeersec_dgram - pull in packet label
2990 * @sock: the peer socket
2992 * @secid: pointer to where to put the secid of the packet
2994 * Sets the netlabel socket state on sk from parent
2996 static int smack_socket_getpeersec_dgram(struct socket *sock,
2997 struct sk_buff *skb, u32 *secid)
3000 struct netlbl_lsm_secattr secattr;
3001 struct socket_smack *ssp = NULL;
3003 int family = PF_UNSPEC;
3004 u32 s = 0; /* 0 is the invalid secid */
3008 if (skb->protocol == htons(ETH_P_IP))
3010 else if (skb->protocol == htons(ETH_P_IPV6))
3013 if (family == PF_UNSPEC && sock != NULL)
3014 family = sock->sk->sk_family;
3016 if (family == PF_UNIX) {
3017 ssp = sock->sk->sk_security;
3018 s = smack_to_secid(ssp->smk_out);
3019 } else if (family == PF_INET || family == PF_INET6) {
3021 * Translate what netlabel gave us.
3023 if (sock != NULL && sock->sk != NULL)
3024 ssp = sock->sk->sk_security;
3025 netlbl_secattr_init(&secattr);
3026 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3028 sp = smack_from_secattr(&secattr, ssp);
3029 s = smack_to_secid(sp);
3031 netlbl_secattr_destroy(&secattr);
3040 * smack_sock_graft - Initialize a newly created socket with an existing sock
3042 * @parent: parent socket
3044 * Set the smk_{in,out} state of an existing sock based on the process that
3045 * is creating the new socket.
3047 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3049 struct socket_smack *ssp;
3052 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3055 ssp = sk->sk_security;
3056 ssp->smk_in = ssp->smk_out = smk_of_current();
3057 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3061 * smack_inet_conn_request - Smack access check on connect
3062 * @sk: socket involved
3066 * Returns 0 if a task with the packet label could write to
3067 * the socket, otherwise an error code
3069 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3070 struct request_sock *req)
3072 u16 family = sk->sk_family;
3073 struct smack_known *skp;
3074 struct socket_smack *ssp = sk->sk_security;
3075 struct netlbl_lsm_secattr secattr;
3076 struct sockaddr_in addr;
3081 struct smk_audit_info ad;
3083 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3084 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3087 netlbl_secattr_init(&secattr);
3088 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3090 sp = smack_from_secattr(&secattr, ssp);
3092 sp = smack_known_huh.smk_known;
3093 netlbl_secattr_destroy(&secattr);
3096 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3097 ad.a.u.net.family = family;
3098 ad.a.u.net.netif = skb->skb_iif;
3099 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3102 * Receiving a packet requires that the other end be able to write
3103 * here. Read access is not required.
3105 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
3110 * Save the peer's label in the request_sock so we can later setup
3111 * smk_packet in the child socket so that SO_PEERCRED can report it.
3113 req->peer_secid = smack_to_secid(sp);
3116 * We need to decide if we want to label the incoming connection here
3117 * if we do we only need to label the request_sock and the stack will
3118 * propagate the wire-label to the sock when it is created.
3121 addr.sin_addr.s_addr = hdr->saddr;
3123 hsp = smack_host_label(&addr);
3127 skp = smk_find_entry(sp);
3128 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3130 netlbl_req_delattr(req);
3136 * smack_inet_csk_clone - Copy the connection information to the new socket
3137 * @sk: the new socket
3138 * @req: the connection's request_sock
3140 * Transfer the connection's peer label to the newly created socket.
3142 static void smack_inet_csk_clone(struct sock *sk,
3143 const struct request_sock *req)
3145 struct socket_smack *ssp = sk->sk_security;
3147 if (req->peer_secid != 0)
3148 ssp->smk_packet = smack_from_secid(req->peer_secid);
3150 ssp->smk_packet = NULL;
3154 * Key management security hooks
3156 * Casey has not tested key support very heavily.
3157 * The permission check is most likely too restrictive.
3158 * If you care about keys please have a look.
3163 * smack_key_alloc - Set the key security blob
3165 * @cred: the credentials to use
3168 * No allocation required
3172 static int smack_key_alloc(struct key *key, const struct cred *cred,
3173 unsigned long flags)
3175 key->security = smk_of_task(cred->security);
3180 * smack_key_free - Clear the key security blob
3183 * Clear the blob pointer
3185 static void smack_key_free(struct key *key)
3187 key->security = NULL;
3191 * smack_key_permission - Smack access on a key
3192 * @key_ref: gets to the object
3193 * @cred: the credentials to use
3196 * Return 0 if the task has read and write to the object,
3197 * an error code otherwise
3199 static int smack_key_permission(key_ref_t key_ref,
3200 const struct cred *cred, key_perm_t perm)
3203 struct smk_audit_info ad;
3204 char *tsp = smk_of_task(cred->security);
3206 keyp = key_ref_to_ptr(key_ref);
3210 * If the key hasn't been initialized give it access so that
3213 if (keyp->security == NULL)
3216 * This should not occur
3221 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3222 ad.a.u.key_struct.key = keyp->serial;
3223 ad.a.u.key_struct.key_desc = keyp->description;
3225 return smk_access(tsp, keyp->security,
3226 MAY_READWRITE, &ad);
3228 #endif /* CONFIG_KEYS */
3233 * Audit requires a unique representation of each Smack specific
3234 * rule. This unique representation is used to distinguish the
3235 * object to be audited from remaining kernel objects and also
3236 * works as a glue between the audit hooks.
3238 * Since repository entries are added but never deleted, we'll use
3239 * the smack_known label address related to the given audit rule as
3240 * the needed unique representation. This also better fits the smack
3241 * model where nearly everything is a label.
3246 * smack_audit_rule_init - Initialize a smack audit rule
3247 * @field: audit rule fields given from user-space (audit.h)
3248 * @op: required testing operator (=, !=, >, <, ...)
3249 * @rulestr: smack label to be audited
3250 * @vrule: pointer to save our own audit rule representation
3252 * Prepare to audit cases where (@field @op @rulestr) is true.
3253 * The label to be audited is created if necessay.
3255 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3257 char **rule = (char **)vrule;
3260 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3263 if (op != Audit_equal && op != Audit_not_equal)
3266 *rule = smk_import(rulestr, 0);
3272 * smack_audit_rule_known - Distinguish Smack audit rules
3273 * @krule: rule of interest, in Audit kernel representation format
3275 * This is used to filter Smack rules from remaining Audit ones.
3276 * If it's proved that this rule belongs to us, the
3277 * audit_rule_match hook will be called to do the final judgement.
3279 static int smack_audit_rule_known(struct audit_krule *krule)
3281 struct audit_field *f;
3284 for (i = 0; i < krule->field_count; i++) {
3285 f = &krule->fields[i];
3287 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3295 * smack_audit_rule_match - Audit given object ?
3296 * @secid: security id for identifying the object to test
3297 * @field: audit rule flags given from user-space
3298 * @op: required testing operator
3299 * @vrule: smack internal rule presentation
3300 * @actx: audit context associated with the check
3302 * The core Audit hook. It's used to take the decision of
3303 * whether to audit or not to audit a given object.
3305 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3306 struct audit_context *actx)
3312 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3313 "Smack: missing rule\n");
3317 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3320 smack = smack_from_secid(secid);
3323 * No need to do string comparisons. If a match occurs,
3324 * both pointers will point to the same smack_known
3327 if (op == Audit_equal)
3328 return (rule == smack);
3329 if (op == Audit_not_equal)
3330 return (rule != smack);
3336 * smack_audit_rule_free - free smack rule representation
3337 * @vrule: rule to be freed.
3339 * No memory was allocated.
3341 static void smack_audit_rule_free(void *vrule)
3346 #endif /* CONFIG_AUDIT */
3349 * smack_secid_to_secctx - return the smack label for a secid
3350 * @secid: incoming integer
3351 * @secdata: destination
3352 * @seclen: how long it is
3354 * Exists for networking code.
3356 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3358 char *sp = smack_from_secid(secid);
3362 *seclen = strlen(sp);
3367 * smack_secctx_to_secid - return the secid for a smack label
3368 * @secdata: smack label
3369 * @seclen: how long result is
3370 * @secid: outgoing integer
3372 * Exists for audit and networking code.
3374 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3376 *secid = smack_to_secid(secdata);
3381 * smack_release_secctx - don't do anything.
3385 * Exists to make sure nothing gets done, and properly
3387 static void smack_release_secctx(char *secdata, u32 seclen)
3391 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3393 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3396 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3398 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3401 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3404 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3412 struct security_operations smack_ops = {
3415 .ptrace_access_check = smack_ptrace_access_check,
3416 .ptrace_traceme = smack_ptrace_traceme,
3417 .syslog = smack_syslog,
3419 .sb_alloc_security = smack_sb_alloc_security,
3420 .sb_free_security = smack_sb_free_security,
3421 .sb_copy_data = smack_sb_copy_data,
3422 .sb_kern_mount = smack_sb_kern_mount,
3423 .sb_statfs = smack_sb_statfs,
3424 .sb_mount = smack_sb_mount,
3425 .sb_umount = smack_sb_umount,
3427 .bprm_set_creds = smack_bprm_set_creds,
3428 .bprm_committing_creds = smack_bprm_committing_creds,
3429 .bprm_secureexec = smack_bprm_secureexec,
3431 .inode_alloc_security = smack_inode_alloc_security,
3432 .inode_free_security = smack_inode_free_security,
3433 .inode_init_security = smack_inode_init_security,
3434 .inode_link = smack_inode_link,
3435 .inode_unlink = smack_inode_unlink,
3436 .inode_rmdir = smack_inode_rmdir,
3437 .inode_rename = smack_inode_rename,
3438 .inode_permission = smack_inode_permission,
3439 .inode_setattr = smack_inode_setattr,
3440 .inode_getattr = smack_inode_getattr,
3441 .inode_setxattr = smack_inode_setxattr,
3442 .inode_post_setxattr = smack_inode_post_setxattr,
3443 .inode_getxattr = smack_inode_getxattr,
3444 .inode_removexattr = smack_inode_removexattr,
3445 .inode_getsecurity = smack_inode_getsecurity,
3446 .inode_setsecurity = smack_inode_setsecurity,
3447 .inode_listsecurity = smack_inode_listsecurity,
3448 .inode_getsecid = smack_inode_getsecid,
3450 .file_permission = smack_file_permission,
3451 .file_alloc_security = smack_file_alloc_security,
3452 .file_free_security = smack_file_free_security,
3453 .file_ioctl = smack_file_ioctl,
3454 .file_lock = smack_file_lock,
3455 .file_fcntl = smack_file_fcntl,
3456 .file_mmap = smack_file_mmap,
3457 .file_set_fowner = smack_file_set_fowner,
3458 .file_send_sigiotask = smack_file_send_sigiotask,
3459 .file_receive = smack_file_receive,
3461 .dentry_open = smack_dentry_open,
3463 .cred_alloc_blank = smack_cred_alloc_blank,
3464 .cred_free = smack_cred_free,
3465 .cred_prepare = smack_cred_prepare,
3466 .cred_transfer = smack_cred_transfer,
3467 .kernel_act_as = smack_kernel_act_as,
3468 .kernel_create_files_as = smack_kernel_create_files_as,
3469 .task_setpgid = smack_task_setpgid,
3470 .task_getpgid = smack_task_getpgid,
3471 .task_getsid = smack_task_getsid,
3472 .task_getsecid = smack_task_getsecid,
3473 .task_setnice = smack_task_setnice,
3474 .task_setioprio = smack_task_setioprio,
3475 .task_getioprio = smack_task_getioprio,
3476 .task_setscheduler = smack_task_setscheduler,
3477 .task_getscheduler = smack_task_getscheduler,
3478 .task_movememory = smack_task_movememory,
3479 .task_kill = smack_task_kill,
3480 .task_wait = smack_task_wait,
3481 .task_to_inode = smack_task_to_inode,
3483 .ipc_permission = smack_ipc_permission,
3484 .ipc_getsecid = smack_ipc_getsecid,
3486 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3487 .msg_msg_free_security = smack_msg_msg_free_security,
3489 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3490 .msg_queue_free_security = smack_msg_queue_free_security,
3491 .msg_queue_associate = smack_msg_queue_associate,
3492 .msg_queue_msgctl = smack_msg_queue_msgctl,
3493 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3494 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3496 .shm_alloc_security = smack_shm_alloc_security,
3497 .shm_free_security = smack_shm_free_security,
3498 .shm_associate = smack_shm_associate,
3499 .shm_shmctl = smack_shm_shmctl,
3500 .shm_shmat = smack_shm_shmat,
3502 .sem_alloc_security = smack_sem_alloc_security,
3503 .sem_free_security = smack_sem_free_security,
3504 .sem_associate = smack_sem_associate,
3505 .sem_semctl = smack_sem_semctl,
3506 .sem_semop = smack_sem_semop,
3508 .d_instantiate = smack_d_instantiate,
3510 .getprocattr = smack_getprocattr,
3511 .setprocattr = smack_setprocattr,
3513 .unix_stream_connect = smack_unix_stream_connect,
3514 .unix_may_send = smack_unix_may_send,
3516 .socket_post_create = smack_socket_post_create,
3517 .socket_connect = smack_socket_connect,
3518 .socket_sendmsg = smack_socket_sendmsg,
3519 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3520 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3521 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3522 .sk_alloc_security = smack_sk_alloc_security,
3523 .sk_free_security = smack_sk_free_security,
3524 .sock_graft = smack_sock_graft,
3525 .inet_conn_request = smack_inet_conn_request,
3526 .inet_csk_clone = smack_inet_csk_clone,
3528 /* key management security hooks */
3530 .key_alloc = smack_key_alloc,
3531 .key_free = smack_key_free,
3532 .key_permission = smack_key_permission,
3533 #endif /* CONFIG_KEYS */
3537 .audit_rule_init = smack_audit_rule_init,
3538 .audit_rule_known = smack_audit_rule_known,
3539 .audit_rule_match = smack_audit_rule_match,
3540 .audit_rule_free = smack_audit_rule_free,
3541 #endif /* CONFIG_AUDIT */
3543 .secid_to_secctx = smack_secid_to_secctx,
3544 .secctx_to_secid = smack_secctx_to_secid,
3545 .release_secctx = smack_release_secctx,
3546 .inode_notifysecctx = smack_inode_notifysecctx,
3547 .inode_setsecctx = smack_inode_setsecctx,
3548 .inode_getsecctx = smack_inode_getsecctx,
3552 static __init void init_smack_known_list(void)
3555 * Initialize rule list locks
3557 mutex_init(&smack_known_huh.smk_rules_lock);
3558 mutex_init(&smack_known_hat.smk_rules_lock);
3559 mutex_init(&smack_known_floor.smk_rules_lock);
3560 mutex_init(&smack_known_star.smk_rules_lock);
3561 mutex_init(&smack_known_invalid.smk_rules_lock);
3562 mutex_init(&smack_known_web.smk_rules_lock);
3564 * Initialize rule lists
3566 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3567 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3568 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3569 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3570 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3571 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3573 * Create the known labels list
3575 list_add(&smack_known_huh.list, &smack_known_list);
3576 list_add(&smack_known_hat.list, &smack_known_list);
3577 list_add(&smack_known_star.list, &smack_known_list);
3578 list_add(&smack_known_floor.list, &smack_known_list);
3579 list_add(&smack_known_invalid.list, &smack_known_list);
3580 list_add(&smack_known_web.list, &smack_known_list);
3584 * smack_init - initialize the smack system
3588 static __init int smack_init(void)
3591 struct task_smack *tsp;
3593 if (!security_module_enable(&smack_ops))
3596 tsp = new_task_smack(smack_known_floor.smk_known,
3597 smack_known_floor.smk_known, GFP_KERNEL);
3601 printk(KERN_INFO "Smack: Initializing.\n");
3604 * Set the security state for the initial task.
3606 cred = (struct cred *) current->cred;
3607 cred->security = tsp;
3609 /* initialize the smack_known_list */
3610 init_smack_known_list();
3615 if (register_security(&smack_ops))
3616 panic("smack: Unable to register with kernel.\n");
3622 * Smack requires early initialization in order to label
3623 * all processes and objects when they are created.
3625 security_initcall(smack_init);