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 write 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_WRITE, &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;
1156 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1157 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1158 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1169 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1170 * if mapping anonymous memory.
1171 * @file contains the file structure for file to map (may be NULL).
1172 * @reqprot contains the protection requested by the application.
1173 * @prot contains the protection that will be applied by the kernel.
1174 * @flags contains the operational flags.
1175 * Return 0 if permission is granted.
1177 static int smack_file_mmap(struct file *file,
1178 unsigned long reqprot, unsigned long prot,
1179 unsigned long flags, unsigned long addr,
1180 unsigned long addr_only)
1182 struct smack_known *skp;
1183 struct smack_rule *srp;
1184 struct task_smack *tsp;
1188 struct inode_smack *isp;
1195 /* do DAC check on address space usage */
1196 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1197 if (rc || addr_only)
1200 if (file == NULL || file->f_dentry == NULL)
1203 dp = file->f_dentry;
1205 if (dp->d_inode == NULL)
1208 isp = dp->d_inode->i_security;
1209 if (isp->smk_mmap == NULL)
1211 msmack = isp->smk_mmap;
1213 tsp = current_security();
1214 sp = smk_of_current();
1215 skp = smk_find_entry(sp);
1220 * For each Smack rule associated with the subject
1221 * label verify that the SMACK64MMAP also has access
1222 * to that rule's object label.
1224 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1225 osmack = srp->smk_object;
1227 * Matching labels always allows access.
1229 if (msmack == osmack)
1232 * If there is a matching local rule take
1233 * that into account as well.
1235 may = smk_access_entry(srp->smk_subject, osmack,
1238 may = srp->smk_access;
1240 may &= srp->smk_access;
1242 * If may is zero the SMACK64MMAP subject can't
1243 * possibly have less access.
1249 * Fetch the global list entry.
1250 * If there isn't one a SMACK64MMAP subject
1251 * can't have as much access as current.
1253 skp = smk_find_entry(msmack);
1254 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
1255 if (mmay == -ENOENT) {
1260 * If there is a local entry it modifies the
1261 * potential access, too.
1263 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1264 if (tmay != -ENOENT)
1268 * If there is any access available to current that is
1269 * not available to a SMACK64MMAP subject
1272 if ((may | mmay) != mmay) {
1284 * smack_file_set_fowner - set the file security blob value
1285 * @file: object in question
1288 * Further research may be required on this one.
1290 static int smack_file_set_fowner(struct file *file)
1292 file->f_security = smk_of_current();
1297 * smack_file_send_sigiotask - Smack on sigio
1298 * @tsk: The target task
1299 * @fown: the object the signal come from
1302 * Allow a privileged task to get signals even if it shouldn't
1304 * Returns 0 if a subject with the object's smack could
1305 * write to the task, an error code otherwise.
1307 static int smack_file_send_sigiotask(struct task_struct *tsk,
1308 struct fown_struct *fown, int signum)
1312 char *tsp = smk_of_task(tsk->cred->security);
1313 struct smk_audit_info ad;
1316 * struct fown_struct is never outside the context of a struct file
1318 file = container_of(fown, struct file, f_owner);
1320 /* we don't log here as rc can be overriden */
1321 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
1322 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1325 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1326 smk_ad_setfield_u_tsk(&ad, tsk);
1327 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
1332 * smack_file_receive - Smack file receive check
1335 * Returns 0 if current has access, error code otherwise
1337 static int smack_file_receive(struct file *file)
1340 struct smk_audit_info ad;
1342 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1343 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1345 * This code relies on bitmasks.
1347 if (file->f_mode & FMODE_READ)
1349 if (file->f_mode & FMODE_WRITE)
1352 return smk_curacc(file->f_security, may, &ad);
1356 * smack_dentry_open - Smack dentry open processing
1360 * Set the security blob in the file structure.
1364 static int smack_dentry_open(struct file *file, const struct cred *cred)
1366 struct inode_smack *isp = file->f_path.dentry->d_inode->i_security;
1368 file->f_security = isp->smk_inode;
1378 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1379 * @new: the new credentials
1380 * @gfp: the atomicity of any memory allocations
1382 * Prepare a blank set of credentials for modification. This must allocate all
1383 * the memory the LSM module might require such that cred_transfer() can
1384 * complete without error.
1386 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1388 struct task_smack *tsp;
1390 tsp = new_task_smack(NULL, NULL, gfp);
1394 cred->security = tsp;
1401 * smack_cred_free - "free" task-level security credentials
1402 * @cred: the credentials in question
1405 static void smack_cred_free(struct cred *cred)
1407 struct task_smack *tsp = cred->security;
1408 struct smack_rule *rp;
1409 struct list_head *l;
1410 struct list_head *n;
1414 cred->security = NULL;
1416 list_for_each_safe(l, n, &tsp->smk_rules) {
1417 rp = list_entry(l, struct smack_rule, list);
1418 list_del(&rp->list);
1425 * smack_cred_prepare - prepare new set of credentials for modification
1426 * @new: the new credentials
1427 * @old: the original credentials
1428 * @gfp: the atomicity of any memory allocations
1430 * Prepare a new set of credentials for modification.
1432 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1435 struct task_smack *old_tsp = old->security;
1436 struct task_smack *new_tsp;
1439 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1440 if (new_tsp == NULL)
1443 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1447 new->security = new_tsp;
1452 * smack_cred_transfer - Transfer the old credentials to the new credentials
1453 * @new: the new credentials
1454 * @old: the original credentials
1456 * Fill in a set of blank credentials from another set of credentials.
1458 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1460 struct task_smack *old_tsp = old->security;
1461 struct task_smack *new_tsp = new->security;
1463 new_tsp->smk_task = old_tsp->smk_task;
1464 new_tsp->smk_forked = old_tsp->smk_task;
1465 mutex_init(&new_tsp->smk_rules_lock);
1466 INIT_LIST_HEAD(&new_tsp->smk_rules);
1469 /* cbs copy rule list */
1473 * smack_kernel_act_as - Set the subjective context in a set of credentials
1474 * @new: points to the set of credentials to be modified.
1475 * @secid: specifies the security ID to be set
1477 * Set the security data for a kernel service.
1479 static int smack_kernel_act_as(struct cred *new, u32 secid)
1481 struct task_smack *new_tsp = new->security;
1482 char *smack = smack_from_secid(secid);
1487 new_tsp->smk_task = smack;
1492 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1493 * @new: points to the set of credentials to be modified
1494 * @inode: points to the inode to use as a reference
1496 * Set the file creation context in a set of credentials to the same
1497 * as the objective context of the specified inode
1499 static int smack_kernel_create_files_as(struct cred *new,
1500 struct inode *inode)
1502 struct inode_smack *isp = inode->i_security;
1503 struct task_smack *tsp = new->security;
1505 tsp->smk_forked = isp->smk_inode;
1506 tsp->smk_task = isp->smk_inode;
1511 * smk_curacc_on_task - helper to log task related access
1512 * @p: the task object
1513 * @access: the access requested
1514 * @caller: name of the calling function for audit
1516 * Return 0 if access is permitted
1518 static int smk_curacc_on_task(struct task_struct *p, int access,
1521 struct smk_audit_info ad;
1523 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1524 smk_ad_setfield_u_tsk(&ad, p);
1525 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
1529 * smack_task_setpgid - Smack check on setting pgid
1530 * @p: the task object
1533 * Return 0 if write access is permitted
1535 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1537 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1541 * smack_task_getpgid - Smack access check for getpgid
1542 * @p: the object task
1544 * Returns 0 if current can read the object task, error code otherwise
1546 static int smack_task_getpgid(struct task_struct *p)
1548 return smk_curacc_on_task(p, MAY_READ, __func__);
1552 * smack_task_getsid - Smack access check for getsid
1553 * @p: the object task
1555 * Returns 0 if current can read the object task, error code otherwise
1557 static int smack_task_getsid(struct task_struct *p)
1559 return smk_curacc_on_task(p, MAY_READ, __func__);
1563 * smack_task_getsecid - get the secid of the task
1564 * @p: the object task
1565 * @secid: where to put the result
1567 * Sets the secid to contain a u32 version of the smack label.
1569 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1571 *secid = smack_to_secid(smk_of_task(task_security(p)));
1575 * smack_task_setnice - Smack check on setting nice
1576 * @p: the task object
1579 * Return 0 if write access is permitted
1581 static int smack_task_setnice(struct task_struct *p, int nice)
1585 rc = cap_task_setnice(p, nice);
1587 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1592 * smack_task_setioprio - Smack check on setting ioprio
1593 * @p: the task object
1596 * Return 0 if write access is permitted
1598 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1602 rc = cap_task_setioprio(p, ioprio);
1604 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1609 * smack_task_getioprio - Smack check on reading ioprio
1610 * @p: the task object
1612 * Return 0 if read access is permitted
1614 static int smack_task_getioprio(struct task_struct *p)
1616 return smk_curacc_on_task(p, MAY_READ, __func__);
1620 * smack_task_setscheduler - Smack check on setting scheduler
1621 * @p: the task object
1625 * Return 0 if read access is permitted
1627 static int smack_task_setscheduler(struct task_struct *p)
1631 rc = cap_task_setscheduler(p);
1633 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1638 * smack_task_getscheduler - Smack check on reading scheduler
1639 * @p: the task object
1641 * Return 0 if read access is permitted
1643 static int smack_task_getscheduler(struct task_struct *p)
1645 return smk_curacc_on_task(p, MAY_READ, __func__);
1649 * smack_task_movememory - Smack check on moving memory
1650 * @p: the task object
1652 * Return 0 if write access is permitted
1654 static int smack_task_movememory(struct task_struct *p)
1656 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1660 * smack_task_kill - Smack check on signal delivery
1661 * @p: the task object
1664 * @secid: identifies the smack to use in lieu of current's
1666 * Return 0 if write access is permitted
1668 * The secid behavior is an artifact of an SELinux hack
1669 * in the USB code. Someday it may go away.
1671 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1674 struct smk_audit_info ad;
1676 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1677 smk_ad_setfield_u_tsk(&ad, p);
1679 * Sending a signal requires that the sender
1680 * can write the receiver.
1683 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1686 * If the secid isn't 0 we're dealing with some USB IO
1687 * specific behavior. This is not clean. For one thing
1688 * we can't take privilege into account.
1690 return smk_access(smack_from_secid(secid),
1691 smk_of_task(task_security(p)), MAY_WRITE, &ad);
1695 * smack_task_wait - Smack access check for waiting
1696 * @p: task to wait for
1700 static int smack_task_wait(struct task_struct *p)
1703 * Allow the operation to succeed.
1705 * In userless environments (e.g. phones) programs
1706 * get marked with SMACK64EXEC and even if the parent
1707 * and child shouldn't be talking the parent still
1708 * may expect to know when the child exits.
1714 * smack_task_to_inode - copy task smack into the inode blob
1715 * @p: task to copy from
1716 * @inode: inode to copy to
1718 * Sets the smack pointer in the inode security blob
1720 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1722 struct inode_smack *isp = inode->i_security;
1723 isp->smk_inode = smk_of_task(task_security(p));
1731 * smack_sk_alloc_security - Allocate a socket blob
1734 * @gfp_flags: memory allocation flags
1736 * Assign Smack pointers to current
1738 * Returns 0 on success, -ENOMEM is there's no memory
1740 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1742 char *csp = smk_of_current();
1743 struct socket_smack *ssp;
1745 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1751 ssp->smk_packet = NULL;
1753 sk->sk_security = ssp;
1759 * smack_sk_free_security - Free a socket blob
1762 * Clears the blob pointer
1764 static void smack_sk_free_security(struct sock *sk)
1766 kfree(sk->sk_security);
1770 * smack_host_label - check host based restrictions
1771 * @sip: the object end
1773 * looks for host based access restrictions
1775 * This version will only be appropriate for really small sets of single label
1776 * hosts. The caller is responsible for ensuring that the RCU read lock is
1777 * taken before calling this function.
1779 * Returns the label of the far end or NULL if it's not special.
1781 static char *smack_host_label(struct sockaddr_in *sip)
1783 struct smk_netlbladdr *snp;
1784 struct in_addr *siap = &sip->sin_addr;
1786 if (siap->s_addr == 0)
1789 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1791 * we break after finding the first match because
1792 * the list is sorted from longest to shortest mask
1793 * so we have found the most specific match
1795 if ((&snp->smk_host.sin_addr)->s_addr ==
1796 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1797 /* we have found the special CIPSO option */
1798 if (snp->smk_label == smack_cipso_option)
1800 return snp->smk_label;
1807 * smack_netlabel - Set the secattr on a socket
1809 * @labeled: socket label scheme
1811 * Convert the outbound smack value (smk_out) to a
1812 * secattr and attach it to the socket.
1814 * Returns 0 on success or an error code
1816 static int smack_netlabel(struct sock *sk, int labeled)
1818 struct smack_known *skp;
1819 struct socket_smack *ssp = sk->sk_security;
1823 * Usually the netlabel code will handle changing the
1824 * packet labeling based on the label.
1825 * The case of a single label host is different, because
1826 * a single label host should never get a labeled packet
1827 * even though the label is usually associated with a packet
1831 bh_lock_sock_nested(sk);
1833 if (ssp->smk_out == smack_net_ambient ||
1834 labeled == SMACK_UNLABELED_SOCKET)
1835 netlbl_sock_delattr(sk);
1837 skp = smk_find_entry(ssp->smk_out);
1838 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
1848 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1850 * @sap: the destination address
1852 * Set the correct secattr for the given socket based on the destination
1853 * address and perform any outbound access checks needed.
1855 * Returns 0 on success or an error code.
1858 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1863 struct socket_smack *ssp = sk->sk_security;
1864 struct smk_audit_info ad;
1867 hostsp = smack_host_label(sap);
1868 if (hostsp != NULL) {
1869 sk_lbl = SMACK_UNLABELED_SOCKET;
1871 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1872 ad.a.u.net.family = sap->sin_family;
1873 ad.a.u.net.dport = sap->sin_port;
1874 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1876 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
1878 sk_lbl = SMACK_CIPSO_SOCKET;
1885 return smack_netlabel(sk, sk_lbl);
1889 * smack_inode_setsecurity - set smack xattrs
1890 * @inode: the object
1891 * @name: attribute name
1892 * @value: attribute value
1893 * @size: size of the attribute
1896 * Sets the named attribute in the appropriate blob
1898 * Returns 0 on success, or an error code
1900 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1901 const void *value, size_t size, int flags)
1904 struct inode_smack *nsp = inode->i_security;
1905 struct socket_smack *ssp;
1906 struct socket *sock;
1909 if (value == NULL || size > SMK_LONGLABEL || size == 0)
1912 sp = smk_import(value, size);
1916 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1917 nsp->smk_inode = sp;
1918 nsp->smk_flags |= SMK_INODE_INSTANT;
1922 * The rest of the Smack xattrs are only on sockets.
1924 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1927 sock = SOCKET_I(inode);
1928 if (sock == NULL || sock->sk == NULL)
1931 ssp = sock->sk->sk_security;
1933 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1935 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1937 if (sock->sk->sk_family != PF_UNIX) {
1938 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1941 "Smack: \"%s\" netlbl error %d.\n",
1951 * smack_socket_post_create - finish socket setup
1953 * @family: protocol family
1958 * Sets the netlabel information on the socket
1960 * Returns 0 on success, and error code otherwise
1962 static int smack_socket_post_create(struct socket *sock, int family,
1963 int type, int protocol, int kern)
1965 if (family != PF_INET || sock->sk == NULL)
1968 * Set the outbound netlbl.
1970 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1974 * smack_socket_connect - connect access check
1976 * @sap: the other end
1977 * @addrlen: size of sap
1979 * Verifies that a connection may be possible
1981 * Returns 0 on success, and error code otherwise
1983 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1986 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1988 if (addrlen < sizeof(struct sockaddr_in))
1991 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
1995 * smack_flags_to_may - convert S_ to MAY_ values
1996 * @flags: the S_ value
1998 * Returns the equivalent MAY_ value
2000 static int smack_flags_to_may(int flags)
2004 if (flags & S_IRUGO)
2006 if (flags & S_IWUGO)
2008 if (flags & S_IXUGO)
2015 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2020 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2022 msg->security = smk_of_current();
2027 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2030 * Clears the blob pointer
2032 static void smack_msg_msg_free_security(struct msg_msg *msg)
2034 msg->security = NULL;
2038 * smack_of_shm - the smack pointer for the shm
2041 * Returns a pointer to the smack value
2043 static char *smack_of_shm(struct shmid_kernel *shp)
2045 return (char *)shp->shm_perm.security;
2049 * smack_shm_alloc_security - Set the security blob for shm
2054 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2056 struct kern_ipc_perm *isp = &shp->shm_perm;
2058 isp->security = smk_of_current();
2063 * smack_shm_free_security - Clear the security blob for shm
2066 * Clears the blob pointer
2068 static void smack_shm_free_security(struct shmid_kernel *shp)
2070 struct kern_ipc_perm *isp = &shp->shm_perm;
2072 isp->security = NULL;
2076 * smk_curacc_shm : check if current has access on shm
2078 * @access : access requested
2080 * Returns 0 if current has the requested access, error code otherwise
2082 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2084 char *ssp = smack_of_shm(shp);
2085 struct smk_audit_info ad;
2088 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2089 ad.a.u.ipc_id = shp->shm_perm.id;
2091 return smk_curacc(ssp, access, &ad);
2095 * smack_shm_associate - Smack access check for shm
2097 * @shmflg: access requested
2099 * Returns 0 if current has the requested access, error code otherwise
2101 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2105 may = smack_flags_to_may(shmflg);
2106 return smk_curacc_shm(shp, may);
2110 * smack_shm_shmctl - Smack access check for shm
2112 * @cmd: what it wants to do
2114 * Returns 0 if current has the requested access, error code otherwise
2116 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2129 may = MAY_READWRITE;
2134 * System level information.
2140 return smk_curacc_shm(shp, may);
2144 * smack_shm_shmat - Smack access for shmat
2147 * @shmflg: access requested
2149 * Returns 0 if current has the requested access, error code otherwise
2151 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2156 may = smack_flags_to_may(shmflg);
2157 return smk_curacc_shm(shp, may);
2161 * smack_of_sem - the smack pointer for the sem
2164 * Returns a pointer to the smack value
2166 static char *smack_of_sem(struct sem_array *sma)
2168 return (char *)sma->sem_perm.security;
2172 * smack_sem_alloc_security - Set the security blob for sem
2177 static int smack_sem_alloc_security(struct sem_array *sma)
2179 struct kern_ipc_perm *isp = &sma->sem_perm;
2181 isp->security = smk_of_current();
2186 * smack_sem_free_security - Clear the security blob for sem
2189 * Clears the blob pointer
2191 static void smack_sem_free_security(struct sem_array *sma)
2193 struct kern_ipc_perm *isp = &sma->sem_perm;
2195 isp->security = NULL;
2199 * smk_curacc_sem : check if current has access on sem
2201 * @access : access requested
2203 * Returns 0 if current has the requested access, error code otherwise
2205 static int smk_curacc_sem(struct sem_array *sma, int access)
2207 char *ssp = smack_of_sem(sma);
2208 struct smk_audit_info ad;
2211 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2212 ad.a.u.ipc_id = sma->sem_perm.id;
2214 return smk_curacc(ssp, access, &ad);
2218 * smack_sem_associate - Smack access check for sem
2220 * @semflg: access requested
2222 * Returns 0 if current has the requested access, error code otherwise
2224 static int smack_sem_associate(struct sem_array *sma, int semflg)
2228 may = smack_flags_to_may(semflg);
2229 return smk_curacc_sem(sma, may);
2233 * smack_sem_shmctl - Smack access check for sem
2235 * @cmd: what it wants to do
2237 * Returns 0 if current has the requested access, error code otherwise
2239 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2257 may = MAY_READWRITE;
2262 * System level information
2269 return smk_curacc_sem(sma, may);
2273 * smack_sem_semop - Smack checks of semaphore operations
2279 * Treated as read and write in all cases.
2281 * Returns 0 if access is allowed, error code otherwise
2283 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2284 unsigned nsops, int alter)
2286 return smk_curacc_sem(sma, MAY_READWRITE);
2290 * smack_msg_alloc_security - Set the security blob for msg
2295 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2297 struct kern_ipc_perm *kisp = &msq->q_perm;
2299 kisp->security = smk_of_current();
2304 * smack_msg_free_security - Clear the security blob for msg
2307 * Clears the blob pointer
2309 static void smack_msg_queue_free_security(struct msg_queue *msq)
2311 struct kern_ipc_perm *kisp = &msq->q_perm;
2313 kisp->security = NULL;
2317 * smack_of_msq - the smack pointer for the msq
2320 * Returns a pointer to the smack value
2322 static char *smack_of_msq(struct msg_queue *msq)
2324 return (char *)msq->q_perm.security;
2328 * smk_curacc_msq : helper to check if current has access on msq
2330 * @access : access requested
2332 * return 0 if current has access, error otherwise
2334 static int smk_curacc_msq(struct msg_queue *msq, int access)
2336 char *msp = smack_of_msq(msq);
2337 struct smk_audit_info ad;
2340 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2341 ad.a.u.ipc_id = msq->q_perm.id;
2343 return smk_curacc(msp, access, &ad);
2347 * smack_msg_queue_associate - Smack access check for msg_queue
2349 * @msqflg: access requested
2351 * Returns 0 if current has the requested access, error code otherwise
2353 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2357 may = smack_flags_to_may(msqflg);
2358 return smk_curacc_msq(msq, may);
2362 * smack_msg_queue_msgctl - Smack access check for msg_queue
2364 * @cmd: what it wants to do
2366 * Returns 0 if current has the requested access, error code otherwise
2368 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2379 may = MAY_READWRITE;
2384 * System level information
2391 return smk_curacc_msq(msq, may);
2395 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2398 * @msqflg: access requested
2400 * Returns 0 if current has the requested access, error code otherwise
2402 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2407 may = smack_flags_to_may(msqflg);
2408 return smk_curacc_msq(msq, may);
2412 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2419 * Returns 0 if current has read and write access, error code otherwise
2421 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2422 struct task_struct *target, long type, int mode)
2424 return smk_curacc_msq(msq, MAY_READWRITE);
2428 * smack_ipc_permission - Smack access for ipc_permission()
2429 * @ipp: the object permissions
2430 * @flag: access requested
2432 * Returns 0 if current has read and write access, error code otherwise
2434 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2436 char *isp = ipp->security;
2437 int may = smack_flags_to_may(flag);
2438 struct smk_audit_info ad;
2441 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2442 ad.a.u.ipc_id = ipp->id;
2444 return smk_curacc(isp, may, &ad);
2448 * smack_ipc_getsecid - Extract smack security id
2449 * @ipp: the object permissions
2450 * @secid: where result will be saved
2452 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2454 char *smack = ipp->security;
2456 *secid = smack_to_secid(smack);
2460 * smack_d_instantiate - Make sure the blob is correct on an inode
2461 * @opt_dentry: dentry where inode will be attached
2462 * @inode: the object
2464 * Set the inode's security blob if it hasn't been done already.
2466 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2468 struct super_block *sbp;
2469 struct superblock_smack *sbsp;
2470 struct inode_smack *isp;
2471 char *csp = smk_of_current();
2474 char trattr[TRANS_TRUE_SIZE];
2482 isp = inode->i_security;
2484 mutex_lock(&isp->smk_lock);
2486 * If the inode is already instantiated
2487 * take the quick way out
2489 if (isp->smk_flags & SMK_INODE_INSTANT)
2493 sbsp = sbp->s_security;
2495 * We're going to use the superblock default label
2496 * if there's no label on the file.
2498 final = sbsp->smk_default;
2501 * If this is the root inode the superblock
2502 * may be in the process of initialization.
2503 * If that is the case use the root value out
2504 * of the superblock.
2506 if (opt_dentry->d_parent == opt_dentry) {
2507 isp->smk_inode = sbsp->smk_root;
2508 isp->smk_flags |= SMK_INODE_INSTANT;
2513 * This is pretty hackish.
2514 * Casey says that we shouldn't have to do
2515 * file system specific code, but it does help
2516 * with keeping it simple.
2518 switch (sbp->s_magic) {
2521 * Casey says that it's a little embarrassing
2522 * that the smack file system doesn't do
2523 * extended attributes.
2525 final = smack_known_star.smk_known;
2529 * Casey says pipes are easy (?)
2531 final = smack_known_star.smk_known;
2533 case DEVPTS_SUPER_MAGIC:
2535 * devpts seems content with the label of the task.
2536 * Programs that change smack have to treat the
2543 * Socket access is controlled by the socket
2544 * structures associated with the task involved.
2546 final = smack_known_star.smk_known;
2548 case PROC_SUPER_MAGIC:
2550 * Casey says procfs appears not to care.
2551 * The superblock default suffices.
2556 * Device labels should come from the filesystem,
2557 * but watch out, because they're volitile,
2558 * getting recreated on every reboot.
2560 final = smack_known_star.smk_known;
2564 * If a smack value has been set we want to use it,
2565 * but since tmpfs isn't giving us the opportunity
2566 * to set mount options simulate setting the
2567 * superblock default.
2571 * This isn't an understood special case.
2572 * Get the value from the xattr.
2576 * UNIX domain sockets use lower level socket data.
2578 if (S_ISSOCK(inode->i_mode)) {
2579 final = smack_known_star.smk_known;
2583 * No xattr support means, alas, no SMACK label.
2584 * Use the aforeapplied default.
2585 * It would be curious if the label of the task
2586 * does not match that assigned.
2588 if (inode->i_op->getxattr == NULL)
2591 * Get the dentry for xattr.
2593 dp = dget(opt_dentry);
2594 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2595 if (fetched != NULL)
2599 * Transmuting directory
2601 if (S_ISDIR(inode->i_mode)) {
2603 * If this is a new directory and the label was
2604 * transmuted when the inode was initialized
2605 * set the transmute attribute on the directory
2606 * and mark the inode.
2608 * If there is a transmute attribute on the
2609 * directory mark the inode.
2611 if (isp->smk_flags & SMK_INODE_CHANGED) {
2612 isp->smk_flags &= ~SMK_INODE_CHANGED;
2613 rc = inode->i_op->setxattr(dp,
2614 XATTR_NAME_SMACKTRANSMUTE,
2615 TRANS_TRUE, TRANS_TRUE_SIZE,
2618 rc = inode->i_op->getxattr(dp,
2619 XATTR_NAME_SMACKTRANSMUTE, trattr,
2621 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2622 TRANS_TRUE_SIZE) != 0)
2626 transflag = SMK_INODE_TRANSMUTE;
2628 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2629 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2636 isp->smk_inode = csp;
2638 isp->smk_inode = final;
2640 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2643 mutex_unlock(&isp->smk_lock);
2648 * smack_getprocattr - Smack process attribute access
2649 * @p: the object task
2650 * @name: the name of the attribute in /proc/.../attr
2651 * @value: where to put the result
2653 * Places a copy of the task Smack into value
2655 * Returns the length of the smack label or an error code
2657 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2662 if (strcmp(name, "current") != 0)
2665 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
2675 * smack_setprocattr - Smack process attribute setting
2676 * @p: the object task
2677 * @name: the name of the attribute in /proc/.../attr
2678 * @value: the value to set
2679 * @size: the size of the value
2681 * Sets the Smack value of the task. Only setting self
2682 * is permitted and only with privilege
2684 * Returns the length of the smack label or an error code
2686 static int smack_setprocattr(struct task_struct *p, char *name,
2687 void *value, size_t size)
2689 struct task_smack *tsp;
2694 * Changing another process' Smack value is too dangerous
2695 * and supports no sane use case.
2700 if (!smack_privileged(CAP_MAC_ADMIN))
2703 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
2706 if (strcmp(name, "current") != 0)
2709 newsmack = smk_import(value, size);
2710 if (newsmack == NULL)
2714 * No process is ever allowed the web ("@") label.
2716 if (newsmack == smack_known_web.smk_known)
2719 new = prepare_creds();
2723 tsp = new->security;
2724 tsp->smk_task = newsmack;
2731 * smack_unix_stream_connect - Smack access on UDS
2733 * @other: the other sock
2736 * Return 0 if a subject with the smack of sock could access
2737 * an object with the smack of other, otherwise an error code
2739 static int smack_unix_stream_connect(struct sock *sock,
2740 struct sock *other, struct sock *newsk)
2742 struct socket_smack *ssp = sock->sk_security;
2743 struct socket_smack *osp = other->sk_security;
2744 struct socket_smack *nsp = newsk->sk_security;
2745 struct smk_audit_info ad;
2748 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2749 smk_ad_setfield_u_net_sk(&ad, other);
2751 if (!smack_privileged(CAP_MAC_OVERRIDE))
2752 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2755 * Cross reference the peer labels for SO_PEERSEC.
2758 nsp->smk_packet = ssp->smk_out;
2759 ssp->smk_packet = osp->smk_out;
2766 * smack_unix_may_send - Smack access on UDS
2768 * @other: the other socket
2770 * Return 0 if a subject with the smack of sock could access
2771 * an object with the smack of other, otherwise an error code
2773 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2775 struct socket_smack *ssp = sock->sk->sk_security;
2776 struct socket_smack *osp = other->sk->sk_security;
2777 struct smk_audit_info ad;
2780 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2781 smk_ad_setfield_u_net_sk(&ad, other->sk);
2783 if (!smack_privileged(CAP_MAC_OVERRIDE))
2784 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2790 * smack_socket_sendmsg - Smack check based on destination host
2793 * @size: the size of the message
2795 * Return 0 if the current subject can write to the destination
2796 * host. This is only a question if the destination is a single
2799 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2802 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2805 * Perfectly reasonable for this to be NULL
2807 if (sip == NULL || sip->sin_family != AF_INET)
2810 return smack_netlabel_send(sock->sk, sip);
2814 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2815 * @sap: netlabel secattr
2816 * @ssp: socket security information
2818 * Returns a pointer to a Smack label found on the label list.
2820 static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
2821 struct socket_smack *ssp)
2823 struct smack_known *kp;
2827 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2829 * Looks like a CIPSO packet.
2830 * If there are flags but no level netlabel isn't
2831 * behaving the way we expect it to.
2833 * Look it up in the label table
2834 * Without guidance regarding the smack value
2835 * for the packet fall back on the network
2839 list_for_each_entry(kp, &smack_known_list, list) {
2840 if (sap->attr.mls.lvl != kp->smk_netlabel.attr.mls.lvl)
2842 if (memcmp(sap->attr.mls.cat,
2843 kp->smk_netlabel.attr.mls.cat,
2852 return kp->smk_known;
2854 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
2855 return smack_known_web.smk_known;
2856 return smack_known_star.smk_known;
2858 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2860 * Looks like a fallback, which gives us a secid.
2862 sp = smack_from_secid(sap->attr.secid);
2864 * This has got to be a bug because it is
2865 * impossible to specify a fallback without
2866 * specifying the label, which will ensure
2867 * it has a secid, and the only way to get a
2868 * secid is from a fallback.
2874 * Without guidance regarding the smack value
2875 * for the packet fall back on the network
2878 return smack_net_ambient;
2882 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2886 * Returns 0 if the packet should be delivered, an error code otherwise
2888 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2890 struct netlbl_lsm_secattr secattr;
2891 struct socket_smack *ssp = sk->sk_security;
2894 struct smk_audit_info ad;
2895 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2899 * Translate what netlabel gave us.
2901 netlbl_secattr_init(&secattr);
2903 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2905 csp = smack_from_secattr(&secattr, ssp);
2907 csp = smack_net_ambient;
2909 netlbl_secattr_destroy(&secattr);
2912 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2913 ad.a.u.net.family = sk->sk_family;
2914 ad.a.u.net.netif = skb->skb_iif;
2915 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2918 * Receiving a packet requires that the other end
2919 * be able to write here. Read access is not required.
2920 * This is the simplist possible security model
2923 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
2925 netlbl_skbuff_err(skb, rc, 0);
2930 * smack_socket_getpeersec_stream - pull in packet label
2932 * @optval: user's destination
2933 * @optlen: size thereof
2936 * returns zero on success, an error code otherwise
2938 static int smack_socket_getpeersec_stream(struct socket *sock,
2939 char __user *optval,
2940 int __user *optlen, unsigned len)
2942 struct socket_smack *ssp;
2947 ssp = sock->sk->sk_security;
2948 if (ssp->smk_packet != NULL) {
2949 rcp = ssp->smk_packet;
2950 slen = strlen(rcp) + 1;
2955 else if (copy_to_user(optval, rcp, slen) != 0)
2958 if (put_user(slen, optlen) != 0)
2966 * smack_socket_getpeersec_dgram - pull in packet label
2967 * @sock: the peer socket
2969 * @secid: pointer to where to put the secid of the packet
2971 * Sets the netlabel socket state on sk from parent
2973 static int smack_socket_getpeersec_dgram(struct socket *sock,
2974 struct sk_buff *skb, u32 *secid)
2977 struct netlbl_lsm_secattr secattr;
2978 struct socket_smack *ssp = NULL;
2980 int family = PF_UNSPEC;
2981 u32 s = 0; /* 0 is the invalid secid */
2985 if (skb->protocol == htons(ETH_P_IP))
2987 else if (skb->protocol == htons(ETH_P_IPV6))
2990 if (family == PF_UNSPEC && sock != NULL)
2991 family = sock->sk->sk_family;
2993 if (family == PF_UNIX) {
2994 ssp = sock->sk->sk_security;
2995 s = smack_to_secid(ssp->smk_out);
2996 } else if (family == PF_INET || family == PF_INET6) {
2998 * Translate what netlabel gave us.
3000 if (sock != NULL && sock->sk != NULL)
3001 ssp = sock->sk->sk_security;
3002 netlbl_secattr_init(&secattr);
3003 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3005 sp = smack_from_secattr(&secattr, ssp);
3006 s = smack_to_secid(sp);
3008 netlbl_secattr_destroy(&secattr);
3017 * smack_sock_graft - Initialize a newly created socket with an existing sock
3019 * @parent: parent socket
3021 * Set the smk_{in,out} state of an existing sock based on the process that
3022 * is creating the new socket.
3024 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3026 struct socket_smack *ssp;
3029 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3032 ssp = sk->sk_security;
3033 ssp->smk_in = ssp->smk_out = smk_of_current();
3034 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3038 * smack_inet_conn_request - Smack access check on connect
3039 * @sk: socket involved
3043 * Returns 0 if a task with the packet label could write to
3044 * the socket, otherwise an error code
3046 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3047 struct request_sock *req)
3049 u16 family = sk->sk_family;
3050 struct smack_known *skp;
3051 struct socket_smack *ssp = sk->sk_security;
3052 struct netlbl_lsm_secattr secattr;
3053 struct sockaddr_in addr;
3058 struct smk_audit_info ad;
3060 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3061 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3064 netlbl_secattr_init(&secattr);
3065 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3067 sp = smack_from_secattr(&secattr, ssp);
3069 sp = smack_known_huh.smk_known;
3070 netlbl_secattr_destroy(&secattr);
3073 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3074 ad.a.u.net.family = family;
3075 ad.a.u.net.netif = skb->skb_iif;
3076 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3079 * Receiving a packet requires that the other end be able to write
3080 * here. Read access is not required.
3082 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
3087 * Save the peer's label in the request_sock so we can later setup
3088 * smk_packet in the child socket so that SO_PEERCRED can report it.
3090 req->peer_secid = smack_to_secid(sp);
3093 * We need to decide if we want to label the incoming connection here
3094 * if we do we only need to label the request_sock and the stack will
3095 * propagate the wire-label to the sock when it is created.
3098 addr.sin_addr.s_addr = hdr->saddr;
3100 hsp = smack_host_label(&addr);
3104 skp = smk_find_entry(sp);
3105 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3107 netlbl_req_delattr(req);
3113 * smack_inet_csk_clone - Copy the connection information to the new socket
3114 * @sk: the new socket
3115 * @req: the connection's request_sock
3117 * Transfer the connection's peer label to the newly created socket.
3119 static void smack_inet_csk_clone(struct sock *sk,
3120 const struct request_sock *req)
3122 struct socket_smack *ssp = sk->sk_security;
3124 if (req->peer_secid != 0)
3125 ssp->smk_packet = smack_from_secid(req->peer_secid);
3127 ssp->smk_packet = NULL;
3131 * Key management security hooks
3133 * Casey has not tested key support very heavily.
3134 * The permission check is most likely too restrictive.
3135 * If you care about keys please have a look.
3140 * smack_key_alloc - Set the key security blob
3142 * @cred: the credentials to use
3145 * No allocation required
3149 static int smack_key_alloc(struct key *key, const struct cred *cred,
3150 unsigned long flags)
3152 key->security = smk_of_task(cred->security);
3157 * smack_key_free - Clear the key security blob
3160 * Clear the blob pointer
3162 static void smack_key_free(struct key *key)
3164 key->security = NULL;
3168 * smack_key_permission - Smack access on a key
3169 * @key_ref: gets to the object
3170 * @cred: the credentials to use
3173 * Return 0 if the task has read and write to the object,
3174 * an error code otherwise
3176 static int smack_key_permission(key_ref_t key_ref,
3177 const struct cred *cred, key_perm_t perm)
3180 struct smk_audit_info ad;
3181 char *tsp = smk_of_task(cred->security);
3183 keyp = key_ref_to_ptr(key_ref);
3187 * If the key hasn't been initialized give it access so that
3190 if (keyp->security == NULL)
3193 * This should not occur
3198 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3199 ad.a.u.key_struct.key = keyp->serial;
3200 ad.a.u.key_struct.key_desc = keyp->description;
3202 return smk_access(tsp, keyp->security,
3203 MAY_READWRITE, &ad);
3205 #endif /* CONFIG_KEYS */
3210 * Audit requires a unique representation of each Smack specific
3211 * rule. This unique representation is used to distinguish the
3212 * object to be audited from remaining kernel objects and also
3213 * works as a glue between the audit hooks.
3215 * Since repository entries are added but never deleted, we'll use
3216 * the smack_known label address related to the given audit rule as
3217 * the needed unique representation. This also better fits the smack
3218 * model where nearly everything is a label.
3223 * smack_audit_rule_init - Initialize a smack audit rule
3224 * @field: audit rule fields given from user-space (audit.h)
3225 * @op: required testing operator (=, !=, >, <, ...)
3226 * @rulestr: smack label to be audited
3227 * @vrule: pointer to save our own audit rule representation
3229 * Prepare to audit cases where (@field @op @rulestr) is true.
3230 * The label to be audited is created if necessay.
3232 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3234 char **rule = (char **)vrule;
3237 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3240 if (op != Audit_equal && op != Audit_not_equal)
3243 *rule = smk_import(rulestr, 0);
3249 * smack_audit_rule_known - Distinguish Smack audit rules
3250 * @krule: rule of interest, in Audit kernel representation format
3252 * This is used to filter Smack rules from remaining Audit ones.
3253 * If it's proved that this rule belongs to us, the
3254 * audit_rule_match hook will be called to do the final judgement.
3256 static int smack_audit_rule_known(struct audit_krule *krule)
3258 struct audit_field *f;
3261 for (i = 0; i < krule->field_count; i++) {
3262 f = &krule->fields[i];
3264 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3272 * smack_audit_rule_match - Audit given object ?
3273 * @secid: security id for identifying the object to test
3274 * @field: audit rule flags given from user-space
3275 * @op: required testing operator
3276 * @vrule: smack internal rule presentation
3277 * @actx: audit context associated with the check
3279 * The core Audit hook. It's used to take the decision of
3280 * whether to audit or not to audit a given object.
3282 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3283 struct audit_context *actx)
3289 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3290 "Smack: missing rule\n");
3294 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3297 smack = smack_from_secid(secid);
3300 * No need to do string comparisons. If a match occurs,
3301 * both pointers will point to the same smack_known
3304 if (op == Audit_equal)
3305 return (rule == smack);
3306 if (op == Audit_not_equal)
3307 return (rule != smack);
3313 * smack_audit_rule_free - free smack rule representation
3314 * @vrule: rule to be freed.
3316 * No memory was allocated.
3318 static void smack_audit_rule_free(void *vrule)
3323 #endif /* CONFIG_AUDIT */
3326 * smack_secid_to_secctx - return the smack label for a secid
3327 * @secid: incoming integer
3328 * @secdata: destination
3329 * @seclen: how long it is
3331 * Exists for networking code.
3333 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3335 char *sp = smack_from_secid(secid);
3339 *seclen = strlen(sp);
3344 * smack_secctx_to_secid - return the secid for a smack label
3345 * @secdata: smack label
3346 * @seclen: how long result is
3347 * @secid: outgoing integer
3349 * Exists for audit and networking code.
3351 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3353 *secid = smack_to_secid(secdata);
3358 * smack_release_secctx - don't do anything.
3362 * Exists to make sure nothing gets done, and properly
3364 static void smack_release_secctx(char *secdata, u32 seclen)
3368 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3370 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3373 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3375 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3378 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3381 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3389 struct security_operations smack_ops = {
3392 .ptrace_access_check = smack_ptrace_access_check,
3393 .ptrace_traceme = smack_ptrace_traceme,
3394 .syslog = smack_syslog,
3396 .sb_alloc_security = smack_sb_alloc_security,
3397 .sb_free_security = smack_sb_free_security,
3398 .sb_copy_data = smack_sb_copy_data,
3399 .sb_kern_mount = smack_sb_kern_mount,
3400 .sb_statfs = smack_sb_statfs,
3401 .sb_mount = smack_sb_mount,
3402 .sb_umount = smack_sb_umount,
3404 .bprm_set_creds = smack_bprm_set_creds,
3405 .bprm_committing_creds = smack_bprm_committing_creds,
3406 .bprm_secureexec = smack_bprm_secureexec,
3408 .inode_alloc_security = smack_inode_alloc_security,
3409 .inode_free_security = smack_inode_free_security,
3410 .inode_init_security = smack_inode_init_security,
3411 .inode_link = smack_inode_link,
3412 .inode_unlink = smack_inode_unlink,
3413 .inode_rmdir = smack_inode_rmdir,
3414 .inode_rename = smack_inode_rename,
3415 .inode_permission = smack_inode_permission,
3416 .inode_setattr = smack_inode_setattr,
3417 .inode_getattr = smack_inode_getattr,
3418 .inode_setxattr = smack_inode_setxattr,
3419 .inode_post_setxattr = smack_inode_post_setxattr,
3420 .inode_getxattr = smack_inode_getxattr,
3421 .inode_removexattr = smack_inode_removexattr,
3422 .inode_getsecurity = smack_inode_getsecurity,
3423 .inode_setsecurity = smack_inode_setsecurity,
3424 .inode_listsecurity = smack_inode_listsecurity,
3425 .inode_getsecid = smack_inode_getsecid,
3427 .file_permission = smack_file_permission,
3428 .file_alloc_security = smack_file_alloc_security,
3429 .file_free_security = smack_file_free_security,
3430 .file_ioctl = smack_file_ioctl,
3431 .file_lock = smack_file_lock,
3432 .file_fcntl = smack_file_fcntl,
3433 .file_mmap = smack_file_mmap,
3434 .file_set_fowner = smack_file_set_fowner,
3435 .file_send_sigiotask = smack_file_send_sigiotask,
3436 .file_receive = smack_file_receive,
3438 .dentry_open = smack_dentry_open,
3440 .cred_alloc_blank = smack_cred_alloc_blank,
3441 .cred_free = smack_cred_free,
3442 .cred_prepare = smack_cred_prepare,
3443 .cred_transfer = smack_cred_transfer,
3444 .kernel_act_as = smack_kernel_act_as,
3445 .kernel_create_files_as = smack_kernel_create_files_as,
3446 .task_setpgid = smack_task_setpgid,
3447 .task_getpgid = smack_task_getpgid,
3448 .task_getsid = smack_task_getsid,
3449 .task_getsecid = smack_task_getsecid,
3450 .task_setnice = smack_task_setnice,
3451 .task_setioprio = smack_task_setioprio,
3452 .task_getioprio = smack_task_getioprio,
3453 .task_setscheduler = smack_task_setscheduler,
3454 .task_getscheduler = smack_task_getscheduler,
3455 .task_movememory = smack_task_movememory,
3456 .task_kill = smack_task_kill,
3457 .task_wait = smack_task_wait,
3458 .task_to_inode = smack_task_to_inode,
3460 .ipc_permission = smack_ipc_permission,
3461 .ipc_getsecid = smack_ipc_getsecid,
3463 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3464 .msg_msg_free_security = smack_msg_msg_free_security,
3466 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3467 .msg_queue_free_security = smack_msg_queue_free_security,
3468 .msg_queue_associate = smack_msg_queue_associate,
3469 .msg_queue_msgctl = smack_msg_queue_msgctl,
3470 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3471 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3473 .shm_alloc_security = smack_shm_alloc_security,
3474 .shm_free_security = smack_shm_free_security,
3475 .shm_associate = smack_shm_associate,
3476 .shm_shmctl = smack_shm_shmctl,
3477 .shm_shmat = smack_shm_shmat,
3479 .sem_alloc_security = smack_sem_alloc_security,
3480 .sem_free_security = smack_sem_free_security,
3481 .sem_associate = smack_sem_associate,
3482 .sem_semctl = smack_sem_semctl,
3483 .sem_semop = smack_sem_semop,
3485 .d_instantiate = smack_d_instantiate,
3487 .getprocattr = smack_getprocattr,
3488 .setprocattr = smack_setprocattr,
3490 .unix_stream_connect = smack_unix_stream_connect,
3491 .unix_may_send = smack_unix_may_send,
3493 .socket_post_create = smack_socket_post_create,
3494 .socket_connect = smack_socket_connect,
3495 .socket_sendmsg = smack_socket_sendmsg,
3496 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3497 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3498 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3499 .sk_alloc_security = smack_sk_alloc_security,
3500 .sk_free_security = smack_sk_free_security,
3501 .sock_graft = smack_sock_graft,
3502 .inet_conn_request = smack_inet_conn_request,
3503 .inet_csk_clone = smack_inet_csk_clone,
3505 /* key management security hooks */
3507 .key_alloc = smack_key_alloc,
3508 .key_free = smack_key_free,
3509 .key_permission = smack_key_permission,
3510 #endif /* CONFIG_KEYS */
3514 .audit_rule_init = smack_audit_rule_init,
3515 .audit_rule_known = smack_audit_rule_known,
3516 .audit_rule_match = smack_audit_rule_match,
3517 .audit_rule_free = smack_audit_rule_free,
3518 #endif /* CONFIG_AUDIT */
3520 .secid_to_secctx = smack_secid_to_secctx,
3521 .secctx_to_secid = smack_secctx_to_secid,
3522 .release_secctx = smack_release_secctx,
3523 .inode_notifysecctx = smack_inode_notifysecctx,
3524 .inode_setsecctx = smack_inode_setsecctx,
3525 .inode_getsecctx = smack_inode_getsecctx,
3529 static __init void init_smack_known_list(void)
3532 * Initialize rule list locks
3534 mutex_init(&smack_known_huh.smk_rules_lock);
3535 mutex_init(&smack_known_hat.smk_rules_lock);
3536 mutex_init(&smack_known_floor.smk_rules_lock);
3537 mutex_init(&smack_known_star.smk_rules_lock);
3538 mutex_init(&smack_known_invalid.smk_rules_lock);
3539 mutex_init(&smack_known_web.smk_rules_lock);
3541 * Initialize rule lists
3543 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3544 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3545 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3546 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3547 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3548 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3550 * Create the known labels list
3552 list_add(&smack_known_huh.list, &smack_known_list);
3553 list_add(&smack_known_hat.list, &smack_known_list);
3554 list_add(&smack_known_star.list, &smack_known_list);
3555 list_add(&smack_known_floor.list, &smack_known_list);
3556 list_add(&smack_known_invalid.list, &smack_known_list);
3557 list_add(&smack_known_web.list, &smack_known_list);
3561 * smack_init - initialize the smack system
3565 static __init int smack_init(void)
3568 struct task_smack *tsp;
3570 if (!security_module_enable(&smack_ops))
3573 tsp = new_task_smack(smack_known_floor.smk_known,
3574 smack_known_floor.smk_known, GFP_KERNEL);
3578 printk(KERN_INFO "Smack: Initializing.\n");
3581 * Set the security state for the initial task.
3583 cred = (struct cred *) current->cred;
3584 cred->security = tsp;
3586 /* initialize the smack_known_list */
3587 init_smack_known_list();
3592 if (register_security(&smack_ops))
3593 panic("smack: Unable to register with kernel.\n");
3599 * Smack requires early initialization in order to label
3600 * all processes and objects when they are created.
3602 security_initcall(smack_init);