2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/dccp.h>
31 #include <linux/slab.h>
32 #include <linux/mutex.h>
33 #include <linux/pipe_fs_i.h>
34 #include <net/cipso_ipv4.h>
37 #include <linux/audit.h>
38 #include <linux/magic.h>
39 #include <linux/dcache.h>
40 #include <linux/personality.h>
41 #include <linux/msg.h>
42 #include <linux/shm.h>
43 #include <linux/binfmts.h>
46 #define task_security(task) (task_cred_xxx((task), security))
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 LIST_HEAD(smk_ipv6_port_list);
58 * smk_fetch - Fetch the smack label from a file.
59 * @ip: a pointer to the inode
60 * @dp: a pointer to the dentry
62 * Returns a pointer to the master list entry for the Smack label
63 * or NULL if there was no label to fetch.
65 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
70 struct smack_known *skp = NULL;
72 if (ip->i_op->getxattr == NULL)
75 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
79 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
81 skp = smk_import_entry(buffer, rc);
89 * new_inode_smack - allocate an inode security blob
90 * @smack: a pointer to the Smack label to use in the blob
92 * Returns the new blob or NULL if there's no memory available
94 struct inode_smack *new_inode_smack(char *smack)
96 struct inode_smack *isp;
98 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
102 isp->smk_inode = smack;
104 mutex_init(&isp->smk_lock);
110 * new_task_smack - allocate a task security blob
111 * @smack: a pointer to the Smack label to use in the blob
113 * Returns the new blob or NULL if there's no memory available
115 static struct task_smack *new_task_smack(struct smack_known *task,
116 struct smack_known *forked, gfp_t gfp)
118 struct task_smack *tsp;
120 tsp = kzalloc(sizeof(struct task_smack), gfp);
124 tsp->smk_task = task;
125 tsp->smk_forked = forked;
126 INIT_LIST_HEAD(&tsp->smk_rules);
127 mutex_init(&tsp->smk_rules_lock);
133 * smk_copy_rules - copy a rule set
134 * @nhead - new rules header pointer
135 * @ohead - old rules header pointer
137 * Returns 0 on success, -ENOMEM on error
139 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
142 struct smack_rule *nrp;
143 struct smack_rule *orp;
146 INIT_LIST_HEAD(nhead);
148 list_for_each_entry_rcu(orp, ohead, list) {
149 nrp = kzalloc(sizeof(struct smack_rule), gfp);
155 list_add_rcu(&nrp->list, nhead);
161 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
162 * @mode - input mode in form of PTRACE_MODE_*
164 * Returns a converted MAY_* mode usable by smack rules
166 static inline unsigned int smk_ptrace_mode(unsigned int mode)
169 case PTRACE_MODE_READ:
171 case PTRACE_MODE_ATTACH:
172 return MAY_READWRITE;
179 * smk_ptrace_rule_check - helper for ptrace access
180 * @tracer: tracer process
181 * @tracee_label: label of the process that's about to be traced
182 * @mode: ptrace attachment mode (PTRACE_MODE_*)
183 * @func: name of the function that called us, used for audit
185 * Returns 0 on access granted, -error on error
187 static int smk_ptrace_rule_check(struct task_struct *tracer, char *tracee_label,
188 unsigned int mode, const char *func)
191 struct smk_audit_info ad, *saip = NULL;
192 struct task_smack *tsp;
193 struct smack_known *skp;
195 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
196 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
197 smk_ad_setfield_u_tsk(&ad, tracer);
201 tsp = task_security(tracer);
202 skp = smk_of_task(tsp);
204 rc = smk_tskacc(tsp, tracee_label, smk_ptrace_mode(mode), saip);
210 * We he, that is fun!
214 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
215 * @ctp: child task pointer
216 * @mode: ptrace attachment mode (PTRACE_MODE_*)
218 * Returns 0 if access is OK, an error code otherwise
220 * Do the capability checks.
222 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
225 struct smack_known *skp;
227 rc = cap_ptrace_access_check(ctp, mode);
231 skp = smk_of_task(task_security(ctp));
233 rc = smk_ptrace_rule_check(current, skp->smk_known, mode, __func__);
238 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
239 * @ptp: parent task pointer
241 * Returns 0 if access is OK, an error code otherwise
243 * Do the capability checks, and require PTRACE_MODE_ATTACH.
245 static int smack_ptrace_traceme(struct task_struct *ptp)
248 struct smack_known *skp;
250 rc = cap_ptrace_traceme(ptp);
254 skp = smk_of_task(current_security());
256 rc = smk_ptrace_rule_check(ptp, skp->smk_known,
257 PTRACE_MODE_ATTACH, __func__);
262 * smack_syslog - Smack approval on syslog
263 * @type: message type
265 * Returns 0 on success, error code otherwise.
267 static int smack_syslog(int typefrom_file)
270 struct smack_known *skp = smk_of_current();
272 if (smack_privileged(CAP_MAC_OVERRIDE))
275 if (smack_syslog_label != NULL && smack_syslog_label != skp)
287 * smack_sb_alloc_security - allocate a superblock blob
288 * @sb: the superblock getting the blob
290 * Returns 0 on success or -ENOMEM on error.
292 static int smack_sb_alloc_security(struct super_block *sb)
294 struct superblock_smack *sbsp;
296 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
301 sbsp->smk_root = smack_known_floor.smk_known;
302 sbsp->smk_default = smack_known_floor.smk_known;
303 sbsp->smk_floor = smack_known_floor.smk_known;
304 sbsp->smk_hat = smack_known_hat.smk_known;
306 * smk_initialized will be zero from kzalloc.
308 sb->s_security = sbsp;
314 * smack_sb_free_security - free a superblock blob
315 * @sb: the superblock getting the blob
318 static void smack_sb_free_security(struct super_block *sb)
320 kfree(sb->s_security);
321 sb->s_security = NULL;
325 * smack_sb_copy_data - copy mount options data for processing
326 * @orig: where to start
327 * @smackopts: mount options string
329 * Returns 0 on success or -ENOMEM on error.
331 * Copy the Smack specific mount options out of the mount
334 static int smack_sb_copy_data(char *orig, char *smackopts)
336 char *cp, *commap, *otheropts, *dp;
338 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
339 if (otheropts == NULL)
342 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
343 if (strstr(cp, SMK_FSDEFAULT) == cp)
345 else if (strstr(cp, SMK_FSFLOOR) == cp)
347 else if (strstr(cp, SMK_FSHAT) == cp)
349 else if (strstr(cp, SMK_FSROOT) == cp)
351 else if (strstr(cp, SMK_FSTRANS) == cp)
356 commap = strchr(cp, ',');
365 strcpy(orig, otheropts);
366 free_page((unsigned long)otheropts);
372 * smack_sb_kern_mount - Smack specific mount processing
373 * @sb: the file system superblock
374 * @flags: the mount flags
375 * @data: the smack mount options
377 * Returns 0 on success, an error code on failure
379 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
381 struct dentry *root = sb->s_root;
382 struct inode *inode = root->d_inode;
383 struct superblock_smack *sp = sb->s_security;
384 struct inode_smack *isp;
385 struct smack_known *skp;
392 if (sp->smk_initialized)
395 sp->smk_initialized = 1;
397 for (op = data; op != NULL; op = commap) {
398 commap = strchr(op, ',');
402 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
403 op += strlen(SMK_FSHAT);
404 nsp = smk_import(op, 0);
409 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
410 op += strlen(SMK_FSFLOOR);
411 nsp = smk_import(op, 0);
416 } else if (strncmp(op, SMK_FSDEFAULT,
417 strlen(SMK_FSDEFAULT)) == 0) {
418 op += strlen(SMK_FSDEFAULT);
419 nsp = smk_import(op, 0);
421 sp->smk_default = nsp;
424 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
425 op += strlen(SMK_FSROOT);
426 nsp = smk_import(op, 0);
431 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
432 op += strlen(SMK_FSTRANS);
433 nsp = smk_import(op, 0);
442 if (!smack_privileged(CAP_MAC_ADMIN)) {
444 * Unprivileged mounts don't get to specify Smack values.
449 * Unprivileged mounts get root and default from the caller.
451 skp = smk_of_current();
452 sp->smk_root = skp->smk_known;
453 sp->smk_default = skp->smk_known;
456 * Initialize the root inode.
458 isp = inode->i_security;
460 isp = new_inode_smack(sp->smk_root);
463 inode->i_security = isp;
465 isp->smk_inode = sp->smk_root;
468 isp->smk_flags |= SMK_INODE_TRANSMUTE;
474 * smack_sb_statfs - Smack check on statfs
475 * @dentry: identifies the file system in question
477 * Returns 0 if current can read the floor of the filesystem,
478 * and error code otherwise
480 static int smack_sb_statfs(struct dentry *dentry)
482 struct superblock_smack *sbp = dentry->d_sb->s_security;
484 struct smk_audit_info ad;
486 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
487 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
489 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
498 * smack_bprm_set_creds - set creds for exec
499 * @bprm: the exec information
501 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
503 static int smack_bprm_set_creds(struct linux_binprm *bprm)
505 struct inode *inode = file_inode(bprm->file);
506 struct task_smack *bsp = bprm->cred->security;
507 struct inode_smack *isp;
510 rc = cap_bprm_set_creds(bprm);
514 if (bprm->cred_prepared)
517 isp = inode->i_security;
518 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
521 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
522 struct task_struct *tracer;
526 tracer = ptrace_parent(current);
527 if (likely(tracer != NULL))
528 rc = smk_ptrace_rule_check(tracer,
529 isp->smk_task->smk_known,
536 } else if (bprm->unsafe)
539 bsp->smk_task = isp->smk_task;
540 bprm->per_clear |= PER_CLEAR_ON_SETID;
546 * smack_bprm_committing_creds - Prepare to install the new credentials
549 * @bprm: binprm for exec
551 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
553 struct task_smack *bsp = bprm->cred->security;
555 if (bsp->smk_task != bsp->smk_forked)
556 current->pdeath_signal = 0;
560 * smack_bprm_secureexec - Return the decision to use secureexec.
561 * @bprm: binprm for exec
563 * Returns 0 on success.
565 static int smack_bprm_secureexec(struct linux_binprm *bprm)
567 struct task_smack *tsp = current_security();
568 int ret = cap_bprm_secureexec(bprm);
570 if (!ret && (tsp->smk_task != tsp->smk_forked))
581 * smack_inode_alloc_security - allocate an inode blob
582 * @inode: the inode in need of a blob
584 * Returns 0 if it gets a blob, -ENOMEM otherwise
586 static int smack_inode_alloc_security(struct inode *inode)
588 struct smack_known *skp = smk_of_current();
590 inode->i_security = new_inode_smack(skp->smk_known);
591 if (inode->i_security == NULL)
597 * smack_inode_free_security - free an inode blob
598 * @inode: the inode with a blob
600 * Clears the blob pointer in inode
602 static void smack_inode_free_security(struct inode *inode)
604 kfree(inode->i_security);
605 inode->i_security = NULL;
609 * smack_inode_init_security - copy out the smack from an inode
613 * @name: where to put the attribute name
614 * @value: where to put the attribute value
615 * @len: where to put the length of the attribute
617 * Returns 0 if it all works out, -ENOMEM if there's no memory
619 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
620 const struct qstr *qstr, const char **name,
621 void **value, size_t *len)
623 struct inode_smack *issp = inode->i_security;
624 struct smack_known *skp = smk_of_current();
625 char *isp = smk_of_inode(inode);
626 char *dsp = smk_of_inode(dir);
630 *name = XATTR_SMACK_SUFFIX;
634 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
638 * If the access rule allows transmutation and
639 * the directory requests transmutation then
640 * by all means transmute.
641 * Mark the inode as changed.
643 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
644 smk_inode_transmutable(dir)) {
646 issp->smk_flags |= SMK_INODE_CHANGED;
649 *value = kstrdup(isp, GFP_NOFS);
655 *len = strlen(isp) + 1;
661 * smack_inode_link - Smack check on link
662 * @old_dentry: the existing object
664 * @new_dentry: the new object
666 * Returns 0 if access is permitted, an error code otherwise
668 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
669 struct dentry *new_dentry)
672 struct smk_audit_info ad;
675 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
676 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
678 isp = smk_of_inode(old_dentry->d_inode);
679 rc = smk_curacc(isp, MAY_WRITE, &ad);
681 if (rc == 0 && new_dentry->d_inode != NULL) {
682 isp = smk_of_inode(new_dentry->d_inode);
683 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
684 rc = smk_curacc(isp, MAY_WRITE, &ad);
691 * smack_inode_unlink - Smack check on inode deletion
692 * @dir: containing directory object
693 * @dentry: file to unlink
695 * Returns 0 if current can write the containing directory
696 * and the object, error code otherwise
698 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
700 struct inode *ip = dentry->d_inode;
701 struct smk_audit_info ad;
704 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
705 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
708 * You need write access to the thing you're unlinking
710 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
713 * You also need write access to the containing directory
715 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
716 smk_ad_setfield_u_fs_inode(&ad, dir);
717 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
723 * smack_inode_rmdir - Smack check on directory deletion
724 * @dir: containing directory object
725 * @dentry: directory to unlink
727 * Returns 0 if current can write the containing directory
728 * and the directory, error code otherwise
730 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
732 struct smk_audit_info ad;
735 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
736 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
739 * You need write access to the thing you're removing
741 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
744 * You also need write access to the containing directory
746 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
747 smk_ad_setfield_u_fs_inode(&ad, dir);
748 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
755 * smack_inode_rename - Smack check on rename
756 * @old_inode: the old directory
757 * @old_dentry: unused
758 * @new_inode: the new directory
759 * @new_dentry: unused
761 * Read and write access is required on both the old and
764 * Returns 0 if access is permitted, an error code otherwise
766 static int smack_inode_rename(struct inode *old_inode,
767 struct dentry *old_dentry,
768 struct inode *new_inode,
769 struct dentry *new_dentry)
773 struct smk_audit_info ad;
775 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
776 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
778 isp = smk_of_inode(old_dentry->d_inode);
779 rc = smk_curacc(isp, MAY_READWRITE, &ad);
781 if (rc == 0 && new_dentry->d_inode != NULL) {
782 isp = smk_of_inode(new_dentry->d_inode);
783 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
784 rc = smk_curacc(isp, MAY_READWRITE, &ad);
790 * smack_inode_permission - Smack version of permission()
791 * @inode: the inode in question
792 * @mask: the access requested
794 * This is the important Smack hook.
796 * Returns 0 if access is permitted, -EACCES otherwise
798 static int smack_inode_permission(struct inode *inode, int mask)
800 struct smk_audit_info ad;
801 int no_block = mask & MAY_NOT_BLOCK;
803 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
805 * No permission to check. Existence test. Yup, it's there.
810 /* May be droppable after audit */
813 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
814 smk_ad_setfield_u_fs_inode(&ad, inode);
815 return smk_curacc(smk_of_inode(inode), mask, &ad);
819 * smack_inode_setattr - Smack check for setting attributes
820 * @dentry: the object
821 * @iattr: for the force flag
823 * Returns 0 if access is permitted, an error code otherwise
825 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
827 struct smk_audit_info ad;
829 * Need to allow for clearing the setuid bit.
831 if (iattr->ia_valid & ATTR_FORCE)
833 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
834 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
836 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
840 * smack_inode_getattr - Smack check for getting attributes
842 * @dentry: the object
844 * Returns 0 if access is permitted, an error code otherwise
846 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
848 struct smk_audit_info ad;
851 path.dentry = dentry;
854 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
855 smk_ad_setfield_u_fs_path(&ad, path);
856 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
860 * smack_inode_setxattr - Smack check for setting xattrs
861 * @dentry: the object
862 * @name: name of the attribute
867 * This protects the Smack attribute explicitly.
869 * Returns 0 if access is permitted, an error code otherwise
871 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
872 const void *value, size_t size, int flags)
874 struct smk_audit_info ad;
875 struct smack_known *skp;
877 int check_import = 0;
882 * Check label validity here so import won't fail in post_setxattr
884 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
885 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
886 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
889 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
890 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
894 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
896 if (size != TRANS_TRUE_SIZE ||
897 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
900 rc = cap_inode_setxattr(dentry, name, value, size, flags);
902 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
905 if (rc == 0 && check_import) {
906 skp = smk_import_entry(value, size);
907 if (skp == NULL || (check_star &&
908 (skp == &smack_known_star || skp == &smack_known_web)))
912 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
913 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
916 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
922 * smack_inode_post_setxattr - Apply the Smack update approved above
924 * @name: attribute name
925 * @value: attribute value
926 * @size: attribute size
929 * Set the pointer in the inode blob to the entry found
930 * in the master label list.
932 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
933 const void *value, size_t size, int flags)
935 struct smack_known *skp;
936 struct inode_smack *isp = dentry->d_inode->i_security;
938 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
939 isp->smk_flags |= SMK_INODE_TRANSMUTE;
943 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
944 skp = smk_import_entry(value, size);
946 isp->smk_inode = skp->smk_known;
948 isp->smk_inode = smack_known_invalid.smk_known;
949 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
950 skp = smk_import_entry(value, size);
954 isp->smk_task = &smack_known_invalid;
955 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
956 skp = smk_import_entry(value, size);
960 isp->smk_mmap = &smack_known_invalid;
967 * smack_inode_getxattr - Smack check on getxattr
968 * @dentry: the object
971 * Returns 0 if access is permitted, an error code otherwise
973 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
975 struct smk_audit_info ad;
977 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
978 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
980 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
984 * smack_inode_removexattr - Smack check on removexattr
985 * @dentry: the object
986 * @name: name of the attribute
988 * Removing the Smack attribute requires CAP_MAC_ADMIN
990 * Returns 0 if access is permitted, an error code otherwise
992 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
994 struct inode_smack *isp;
995 struct smk_audit_info ad;
998 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
999 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1000 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1001 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1002 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1003 strcmp(name, XATTR_NAME_SMACKMMAP)) {
1004 if (!smack_privileged(CAP_MAC_ADMIN))
1007 rc = cap_inode_removexattr(dentry, name);
1009 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1010 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1012 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1015 isp = dentry->d_inode->i_security;
1016 isp->smk_task = NULL;
1017 isp->smk_mmap = NULL;
1024 * smack_inode_getsecurity - get smack xattrs
1025 * @inode: the object
1026 * @name: attribute name
1027 * @buffer: where to put the result
1030 * Returns the size of the attribute or an error code
1032 static int smack_inode_getsecurity(const struct inode *inode,
1033 const char *name, void **buffer,
1036 struct socket_smack *ssp;
1037 struct socket *sock;
1038 struct super_block *sbp;
1039 struct inode *ip = (struct inode *)inode;
1044 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1045 isp = smk_of_inode(inode);
1046 ilen = strlen(isp) + 1;
1052 * The rest of the Smack xattrs are only on sockets.
1055 if (sbp->s_magic != SOCKFS_MAGIC)
1058 sock = SOCKET_I(ip);
1059 if (sock == NULL || sock->sk == NULL)
1062 ssp = sock->sk->sk_security;
1064 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1066 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1067 isp = ssp->smk_out->smk_known;
1071 ilen = strlen(isp) + 1;
1082 * smack_inode_listsecurity - list the Smack attributes
1083 * @inode: the object
1084 * @buffer: where they go
1085 * @buffer_size: size of buffer
1087 * Returns 0 on success, -EINVAL otherwise
1089 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1092 int len = strlen(XATTR_NAME_SMACK);
1094 if (buffer != NULL && len <= buffer_size) {
1095 memcpy(buffer, XATTR_NAME_SMACK, len);
1102 * smack_inode_getsecid - Extract inode's security id
1103 * @inode: inode to extract the info from
1104 * @secid: where result will be saved
1106 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1108 struct inode_smack *isp = inode->i_security;
1110 *secid = smack_to_secid(isp->smk_inode);
1118 * smack_file_permission - Smack check on file operations
1124 * Should access checks be done on each read or write?
1125 * UNICOS and SELinux say yes.
1126 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1128 * I'll say no for now. Smack does not do the frequent
1129 * label changing that SELinux does.
1131 static int smack_file_permission(struct file *file, int mask)
1137 * smack_file_alloc_security - assign a file security blob
1140 * The security blob for a file is a pointer to the master
1141 * label list, so no allocation is done.
1145 static int smack_file_alloc_security(struct file *file)
1147 struct smack_known *skp = smk_of_current();
1149 file->f_security = skp->smk_known;
1154 * smack_file_free_security - clear a file security blob
1157 * The security blob for a file is a pointer to the master
1158 * label list, so no memory is freed.
1160 static void smack_file_free_security(struct file *file)
1162 file->f_security = NULL;
1166 * smack_file_ioctl - Smack check on ioctls
1171 * Relies heavily on the correct use of the ioctl command conventions.
1173 * Returns 0 if allowed, error code otherwise
1175 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1179 struct smk_audit_info ad;
1181 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1182 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1184 if (_IOC_DIR(cmd) & _IOC_WRITE)
1185 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1187 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1188 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1194 * smack_file_lock - Smack check on file locking
1198 * Returns 0 if current has lock access, error code otherwise
1200 static int smack_file_lock(struct file *file, unsigned int cmd)
1202 struct smk_audit_info ad;
1204 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1205 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1206 return smk_curacc(file->f_security, MAY_LOCK, &ad);
1210 * smack_file_fcntl - Smack check on fcntl
1212 * @cmd: what action to check
1215 * Generally these operations are harmless.
1216 * File locking operations present an obvious mechanism
1217 * for passing information, so they require write access.
1219 * Returns 0 if current has access, error code otherwise
1221 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1224 struct smk_audit_info ad;
1233 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1234 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1235 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1239 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1240 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1241 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1252 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1253 * if mapping anonymous memory.
1254 * @file contains the file structure for file to map (may be NULL).
1255 * @reqprot contains the protection requested by the application.
1256 * @prot contains the protection that will be applied by the kernel.
1257 * @flags contains the operational flags.
1258 * Return 0 if permission is granted.
1260 static int smack_mmap_file(struct file *file,
1261 unsigned long reqprot, unsigned long prot,
1262 unsigned long flags)
1264 struct smack_known *skp;
1265 struct smack_known *mkp;
1266 struct smack_rule *srp;
1267 struct task_smack *tsp;
1269 struct inode_smack *isp;
1278 isp = file_inode(file)->i_security;
1279 if (isp->smk_mmap == NULL)
1281 mkp = isp->smk_mmap;
1283 tsp = current_security();
1284 skp = smk_of_current();
1289 * For each Smack rule associated with the subject
1290 * label verify that the SMACK64MMAP also has access
1291 * to that rule's object label.
1293 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1294 osmack = srp->smk_object;
1296 * Matching labels always allows access.
1298 if (mkp->smk_known == osmack)
1301 * If there is a matching local rule take
1302 * that into account as well.
1304 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1307 may = srp->smk_access;
1309 may &= srp->smk_access;
1311 * If may is zero the SMACK64MMAP subject can't
1312 * possibly have less access.
1318 * Fetch the global list entry.
1319 * If there isn't one a SMACK64MMAP subject
1320 * can't have as much access as current.
1322 mmay = smk_access_entry(mkp->smk_known, osmack,
1324 if (mmay == -ENOENT) {
1329 * If there is a local entry it modifies the
1330 * potential access, too.
1332 tmay = smk_access_entry(mkp->smk_known, osmack,
1334 if (tmay != -ENOENT)
1338 * If there is any access available to current that is
1339 * not available to a SMACK64MMAP subject
1342 if ((may | mmay) != mmay) {
1354 * smack_file_set_fowner - set the file security blob value
1355 * @file: object in question
1358 * Further research may be required on this one.
1360 static int smack_file_set_fowner(struct file *file)
1362 struct smack_known *skp = smk_of_current();
1364 file->f_security = skp->smk_known;
1369 * smack_file_send_sigiotask - Smack on sigio
1370 * @tsk: The target task
1371 * @fown: the object the signal come from
1374 * Allow a privileged task to get signals even if it shouldn't
1376 * Returns 0 if a subject with the object's smack could
1377 * write to the task, an error code otherwise.
1379 static int smack_file_send_sigiotask(struct task_struct *tsk,
1380 struct fown_struct *fown, int signum)
1382 struct smack_known *skp;
1383 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1386 struct smk_audit_info ad;
1389 * struct fown_struct is never outside the context of a struct file
1391 file = container_of(fown, struct file, f_owner);
1393 /* we don't log here as rc can be overriden */
1394 skp = smk_find_entry(file->f_security);
1395 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1396 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1399 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1400 smk_ad_setfield_u_tsk(&ad, tsk);
1401 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1406 * smack_file_receive - Smack file receive check
1409 * Returns 0 if current has access, error code otherwise
1411 static int smack_file_receive(struct file *file)
1414 struct smk_audit_info ad;
1416 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1417 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1419 * This code relies on bitmasks.
1421 if (file->f_mode & FMODE_READ)
1423 if (file->f_mode & FMODE_WRITE)
1426 return smk_curacc(file->f_security, may, &ad);
1430 * smack_file_open - Smack dentry open processing
1434 * Set the security blob in the file structure.
1438 static int smack_file_open(struct file *file, const struct cred *cred)
1440 struct inode_smack *isp = file_inode(file)->i_security;
1442 file->f_security = isp->smk_inode;
1452 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1453 * @new: the new credentials
1454 * @gfp: the atomicity of any memory allocations
1456 * Prepare a blank set of credentials for modification. This must allocate all
1457 * the memory the LSM module might require such that cred_transfer() can
1458 * complete without error.
1460 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1462 struct task_smack *tsp;
1464 tsp = new_task_smack(NULL, NULL, gfp);
1468 cred->security = tsp;
1475 * smack_cred_free - "free" task-level security credentials
1476 * @cred: the credentials in question
1479 static void smack_cred_free(struct cred *cred)
1481 struct task_smack *tsp = cred->security;
1482 struct smack_rule *rp;
1483 struct list_head *l;
1484 struct list_head *n;
1488 cred->security = NULL;
1490 list_for_each_safe(l, n, &tsp->smk_rules) {
1491 rp = list_entry(l, struct smack_rule, list);
1492 list_del(&rp->list);
1499 * smack_cred_prepare - prepare new set of credentials for modification
1500 * @new: the new credentials
1501 * @old: the original credentials
1502 * @gfp: the atomicity of any memory allocations
1504 * Prepare a new set of credentials for modification.
1506 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1509 struct task_smack *old_tsp = old->security;
1510 struct task_smack *new_tsp;
1513 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1514 if (new_tsp == NULL)
1517 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1521 new->security = new_tsp;
1526 * smack_cred_transfer - Transfer the old credentials to the new credentials
1527 * @new: the new credentials
1528 * @old: the original credentials
1530 * Fill in a set of blank credentials from another set of credentials.
1532 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1534 struct task_smack *old_tsp = old->security;
1535 struct task_smack *new_tsp = new->security;
1537 new_tsp->smk_task = old_tsp->smk_task;
1538 new_tsp->smk_forked = old_tsp->smk_task;
1539 mutex_init(&new_tsp->smk_rules_lock);
1540 INIT_LIST_HEAD(&new_tsp->smk_rules);
1543 /* cbs copy rule list */
1547 * smack_kernel_act_as - Set the subjective context in a set of credentials
1548 * @new: points to the set of credentials to be modified.
1549 * @secid: specifies the security ID to be set
1551 * Set the security data for a kernel service.
1553 static int smack_kernel_act_as(struct cred *new, u32 secid)
1555 struct task_smack *new_tsp = new->security;
1556 struct smack_known *skp = smack_from_secid(secid);
1561 new_tsp->smk_task = skp;
1566 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1567 * @new: points to the set of credentials to be modified
1568 * @inode: points to the inode to use as a reference
1570 * Set the file creation context in a set of credentials to the same
1571 * as the objective context of the specified inode
1573 static int smack_kernel_create_files_as(struct cred *new,
1574 struct inode *inode)
1576 struct inode_smack *isp = inode->i_security;
1577 struct task_smack *tsp = new->security;
1579 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1580 tsp->smk_task = tsp->smk_forked;
1585 * smk_curacc_on_task - helper to log task related access
1586 * @p: the task object
1587 * @access: the access requested
1588 * @caller: name of the calling function for audit
1590 * Return 0 if access is permitted
1592 static int smk_curacc_on_task(struct task_struct *p, int access,
1595 struct smk_audit_info ad;
1596 struct smack_known *skp = smk_of_task(task_security(p));
1598 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1599 smk_ad_setfield_u_tsk(&ad, p);
1600 return smk_curacc(skp->smk_known, access, &ad);
1604 * smack_task_setpgid - Smack check on setting pgid
1605 * @p: the task object
1608 * Return 0 if write access is permitted
1610 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1612 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1616 * smack_task_getpgid - Smack access check for getpgid
1617 * @p: the object task
1619 * Returns 0 if current can read the object task, error code otherwise
1621 static int smack_task_getpgid(struct task_struct *p)
1623 return smk_curacc_on_task(p, MAY_READ, __func__);
1627 * smack_task_getsid - Smack access check for getsid
1628 * @p: the object task
1630 * Returns 0 if current can read the object task, error code otherwise
1632 static int smack_task_getsid(struct task_struct *p)
1634 return smk_curacc_on_task(p, MAY_READ, __func__);
1638 * smack_task_getsecid - get the secid of the task
1639 * @p: the object task
1640 * @secid: where to put the result
1642 * Sets the secid to contain a u32 version of the smack label.
1644 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1646 struct smack_known *skp = smk_of_task(task_security(p));
1648 *secid = skp->smk_secid;
1652 * smack_task_setnice - Smack check on setting nice
1653 * @p: the task object
1656 * Return 0 if write access is permitted
1658 static int smack_task_setnice(struct task_struct *p, int nice)
1662 rc = cap_task_setnice(p, nice);
1664 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1669 * smack_task_setioprio - Smack check on setting ioprio
1670 * @p: the task object
1673 * Return 0 if write access is permitted
1675 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1679 rc = cap_task_setioprio(p, ioprio);
1681 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1686 * smack_task_getioprio - Smack check on reading ioprio
1687 * @p: the task object
1689 * Return 0 if read access is permitted
1691 static int smack_task_getioprio(struct task_struct *p)
1693 return smk_curacc_on_task(p, MAY_READ, __func__);
1697 * smack_task_setscheduler - Smack check on setting scheduler
1698 * @p: the task object
1702 * Return 0 if read access is permitted
1704 static int smack_task_setscheduler(struct task_struct *p)
1708 rc = cap_task_setscheduler(p);
1710 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1715 * smack_task_getscheduler - Smack check on reading scheduler
1716 * @p: the task object
1718 * Return 0 if read access is permitted
1720 static int smack_task_getscheduler(struct task_struct *p)
1722 return smk_curacc_on_task(p, MAY_READ, __func__);
1726 * smack_task_movememory - Smack check on moving memory
1727 * @p: the task object
1729 * Return 0 if write access is permitted
1731 static int smack_task_movememory(struct task_struct *p)
1733 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1737 * smack_task_kill - Smack check on signal delivery
1738 * @p: the task object
1741 * @secid: identifies the smack to use in lieu of current's
1743 * Return 0 if write access is permitted
1745 * The secid behavior is an artifact of an SELinux hack
1746 * in the USB code. Someday it may go away.
1748 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1751 struct smk_audit_info ad;
1752 struct smack_known *skp;
1753 struct smack_known *tkp = smk_of_task(task_security(p));
1755 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1756 smk_ad_setfield_u_tsk(&ad, p);
1758 * Sending a signal requires that the sender
1759 * can write the receiver.
1762 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
1764 * If the secid isn't 0 we're dealing with some USB IO
1765 * specific behavior. This is not clean. For one thing
1766 * we can't take privilege into account.
1768 skp = smack_from_secid(secid);
1769 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
1773 * smack_task_wait - Smack access check for waiting
1774 * @p: task to wait for
1778 static int smack_task_wait(struct task_struct *p)
1781 * Allow the operation to succeed.
1783 * In userless environments (e.g. phones) programs
1784 * get marked with SMACK64EXEC and even if the parent
1785 * and child shouldn't be talking the parent still
1786 * may expect to know when the child exits.
1792 * smack_task_to_inode - copy task smack into the inode blob
1793 * @p: task to copy from
1794 * @inode: inode to copy to
1796 * Sets the smack pointer in the inode security blob
1798 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1800 struct inode_smack *isp = inode->i_security;
1801 struct smack_known *skp = smk_of_task(task_security(p));
1803 isp->smk_inode = skp->smk_known;
1811 * smack_sk_alloc_security - Allocate a socket blob
1814 * @gfp_flags: memory allocation flags
1816 * Assign Smack pointers to current
1818 * Returns 0 on success, -ENOMEM is there's no memory
1820 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1822 struct smack_known *skp = smk_of_current();
1823 struct socket_smack *ssp;
1825 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1829 ssp->smk_in = skp->smk_known;
1831 ssp->smk_packet = NULL;
1833 sk->sk_security = ssp;
1839 * smack_sk_free_security - Free a socket blob
1842 * Clears the blob pointer
1844 static void smack_sk_free_security(struct sock *sk)
1846 kfree(sk->sk_security);
1850 * smack_host_label - check host based restrictions
1851 * @sip: the object end
1853 * looks for host based access restrictions
1855 * This version will only be appropriate for really small sets of single label
1856 * hosts. The caller is responsible for ensuring that the RCU read lock is
1857 * taken before calling this function.
1859 * Returns the label of the far end or NULL if it's not special.
1861 static char *smack_host_label(struct sockaddr_in *sip)
1863 struct smk_netlbladdr *snp;
1864 struct in_addr *siap = &sip->sin_addr;
1866 if (siap->s_addr == 0)
1869 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1871 * we break after finding the first match because
1872 * the list is sorted from longest to shortest mask
1873 * so we have found the most specific match
1875 if ((&snp->smk_host.sin_addr)->s_addr ==
1876 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1877 /* we have found the special CIPSO option */
1878 if (snp->smk_label == smack_cipso_option)
1880 return snp->smk_label;
1887 * smack_netlabel - Set the secattr on a socket
1889 * @labeled: socket label scheme
1891 * Convert the outbound smack value (smk_out) to a
1892 * secattr and attach it to the socket.
1894 * Returns 0 on success or an error code
1896 static int smack_netlabel(struct sock *sk, int labeled)
1898 struct smack_known *skp;
1899 struct socket_smack *ssp = sk->sk_security;
1903 * Usually the netlabel code will handle changing the
1904 * packet labeling based on the label.
1905 * The case of a single label host is different, because
1906 * a single label host should never get a labeled packet
1907 * even though the label is usually associated with a packet
1911 bh_lock_sock_nested(sk);
1913 if (ssp->smk_out == smack_net_ambient ||
1914 labeled == SMACK_UNLABELED_SOCKET)
1915 netlbl_sock_delattr(sk);
1918 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
1928 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1930 * @sap: the destination address
1932 * Set the correct secattr for the given socket based on the destination
1933 * address and perform any outbound access checks needed.
1935 * Returns 0 on success or an error code.
1938 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1940 struct smack_known *skp;
1944 struct socket_smack *ssp = sk->sk_security;
1945 struct smk_audit_info ad;
1948 hostsp = smack_host_label(sap);
1949 if (hostsp != NULL) {
1951 struct lsm_network_audit net;
1953 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1954 ad.a.u.net->family = sap->sin_family;
1955 ad.a.u.net->dport = sap->sin_port;
1956 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
1958 sk_lbl = SMACK_UNLABELED_SOCKET;
1960 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
1962 sk_lbl = SMACK_CIPSO_SOCKET;
1969 return smack_netlabel(sk, sk_lbl);
1973 * smk_ipv6_port_label - Smack port access table management
1977 * Create or update the port list entry
1979 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1981 struct sock *sk = sock->sk;
1982 struct sockaddr_in6 *addr6;
1983 struct socket_smack *ssp = sock->sk->sk_security;
1984 struct smk_port_label *spp;
1985 unsigned short port = 0;
1987 if (address == NULL) {
1989 * This operation is changing the Smack information
1990 * on the bound socket. Take the changes to the port
1993 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1994 if (sk != spp->smk_sock)
1996 spp->smk_in = ssp->smk_in;
1997 spp->smk_out = ssp->smk_out;
2001 * A NULL address is only used for updating existing
2002 * bound entries. If there isn't one, it's OK.
2007 addr6 = (struct sockaddr_in6 *)address;
2008 port = ntohs(addr6->sin6_port);
2010 * This is a special case that is safely ignored.
2016 * Look for an existing port list entry.
2017 * This is an indication that a port is getting reused.
2019 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2020 if (spp->smk_port != port)
2022 spp->smk_port = port;
2024 spp->smk_in = ssp->smk_in;
2025 spp->smk_out = ssp->smk_out;
2030 * A new port entry is required.
2032 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2036 spp->smk_port = port;
2038 spp->smk_in = ssp->smk_in;
2039 spp->smk_out = ssp->smk_out;
2041 list_add(&spp->list, &smk_ipv6_port_list);
2046 * smk_ipv6_port_check - check Smack port access
2050 * Create or update the port list entry
2052 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2057 struct smk_port_label *spp;
2058 struct socket_smack *ssp = sk->sk_security;
2059 struct smack_known *skp;
2060 unsigned short port = 0;
2062 struct smk_audit_info ad;
2064 struct lsm_network_audit net;
2067 if (act == SMK_RECEIVING) {
2068 skp = smack_net_ambient;
2069 object = ssp->smk_in;
2072 object = smack_net_ambient->smk_known;
2076 * Get the IP address and port from the address.
2078 port = ntohs(address->sin6_port);
2079 bep = (__be16 *)(&address->sin6_addr);
2080 be32p = (__be32 *)(&address->sin6_addr);
2083 * It's remote, so port lookup does no good.
2085 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2089 * It's local so the send check has to have passed.
2091 if (act == SMK_RECEIVING) {
2092 skp = &smack_known_web;
2096 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2097 if (spp->smk_port != port)
2099 object = spp->smk_in;
2100 if (act == SMK_CONNECTING)
2101 ssp->smk_packet = spp->smk_out->smk_known;
2108 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2109 ad.a.u.net->family = sk->sk_family;
2110 ad.a.u.net->dport = port;
2111 if (act == SMK_RECEIVING)
2112 ad.a.u.net->v6info.saddr = address->sin6_addr;
2114 ad.a.u.net->v6info.daddr = address->sin6_addr;
2116 return smk_access(skp, object, MAY_WRITE, &ad);
2120 * smack_inode_setsecurity - set smack xattrs
2121 * @inode: the object
2122 * @name: attribute name
2123 * @value: attribute value
2124 * @size: size of the attribute
2127 * Sets the named attribute in the appropriate blob
2129 * Returns 0 on success, or an error code
2131 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2132 const void *value, size_t size, int flags)
2134 struct smack_known *skp;
2135 struct inode_smack *nsp = inode->i_security;
2136 struct socket_smack *ssp;
2137 struct socket *sock;
2140 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2143 skp = smk_import_entry(value, size);
2147 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2148 nsp->smk_inode = skp->smk_known;
2149 nsp->smk_flags |= SMK_INODE_INSTANT;
2153 * The rest of the Smack xattrs are only on sockets.
2155 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2158 sock = SOCKET_I(inode);
2159 if (sock == NULL || sock->sk == NULL)
2162 ssp = sock->sk->sk_security;
2164 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2165 ssp->smk_in = skp->smk_known;
2166 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2168 if (sock->sk->sk_family == PF_INET) {
2169 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2172 "Smack: \"%s\" netlbl error %d.\n",
2178 if (sock->sk->sk_family == PF_INET6)
2179 smk_ipv6_port_label(sock, NULL);
2185 * smack_socket_post_create - finish socket setup
2187 * @family: protocol family
2192 * Sets the netlabel information on the socket
2194 * Returns 0 on success, and error code otherwise
2196 static int smack_socket_post_create(struct socket *sock, int family,
2197 int type, int protocol, int kern)
2199 if (family != PF_INET || sock->sk == NULL)
2202 * Set the outbound netlbl.
2204 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2208 * smack_socket_bind - record port binding information.
2210 * @address: the port address
2211 * @addrlen: size of the address
2213 * Records the label bound to a port.
2217 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2220 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2221 smk_ipv6_port_label(sock, address);
2227 * smack_socket_connect - connect access check
2229 * @sap: the other end
2230 * @addrlen: size of sap
2232 * Verifies that a connection may be possible
2234 * Returns 0 on success, and error code otherwise
2236 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2241 if (sock->sk == NULL)
2244 switch (sock->sk->sk_family) {
2246 if (addrlen < sizeof(struct sockaddr_in))
2248 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2251 if (addrlen < sizeof(struct sockaddr_in6))
2253 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2261 * smack_flags_to_may - convert S_ to MAY_ values
2262 * @flags: the S_ value
2264 * Returns the equivalent MAY_ value
2266 static int smack_flags_to_may(int flags)
2270 if (flags & S_IRUGO)
2272 if (flags & S_IWUGO)
2274 if (flags & S_IXUGO)
2281 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2286 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2288 struct smack_known *skp = smk_of_current();
2290 msg->security = skp->smk_known;
2295 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2298 * Clears the blob pointer
2300 static void smack_msg_msg_free_security(struct msg_msg *msg)
2302 msg->security = NULL;
2306 * smack_of_shm - the smack pointer for the shm
2309 * Returns a pointer to the smack value
2311 static char *smack_of_shm(struct shmid_kernel *shp)
2313 return (char *)shp->shm_perm.security;
2317 * smack_shm_alloc_security - Set the security blob for shm
2322 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2324 struct kern_ipc_perm *isp = &shp->shm_perm;
2325 struct smack_known *skp = smk_of_current();
2327 isp->security = skp->smk_known;
2332 * smack_shm_free_security - Clear the security blob for shm
2335 * Clears the blob pointer
2337 static void smack_shm_free_security(struct shmid_kernel *shp)
2339 struct kern_ipc_perm *isp = &shp->shm_perm;
2341 isp->security = NULL;
2345 * smk_curacc_shm : check if current has access on shm
2347 * @access : access requested
2349 * Returns 0 if current has the requested access, error code otherwise
2351 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2353 char *ssp = smack_of_shm(shp);
2354 struct smk_audit_info ad;
2357 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2358 ad.a.u.ipc_id = shp->shm_perm.id;
2360 return smk_curacc(ssp, access, &ad);
2364 * smack_shm_associate - Smack access check for shm
2366 * @shmflg: access requested
2368 * Returns 0 if current has the requested access, error code otherwise
2370 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2374 may = smack_flags_to_may(shmflg);
2375 return smk_curacc_shm(shp, may);
2379 * smack_shm_shmctl - Smack access check for shm
2381 * @cmd: what it wants to do
2383 * Returns 0 if current has the requested access, error code otherwise
2385 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2398 may = MAY_READWRITE;
2403 * System level information.
2409 return smk_curacc_shm(shp, may);
2413 * smack_shm_shmat - Smack access for shmat
2416 * @shmflg: access requested
2418 * Returns 0 if current has the requested access, error code otherwise
2420 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2425 may = smack_flags_to_may(shmflg);
2426 return smk_curacc_shm(shp, may);
2430 * smack_of_sem - the smack pointer for the sem
2433 * Returns a pointer to the smack value
2435 static char *smack_of_sem(struct sem_array *sma)
2437 return (char *)sma->sem_perm.security;
2441 * smack_sem_alloc_security - Set the security blob for sem
2446 static int smack_sem_alloc_security(struct sem_array *sma)
2448 struct kern_ipc_perm *isp = &sma->sem_perm;
2449 struct smack_known *skp = smk_of_current();
2451 isp->security = skp->smk_known;
2456 * smack_sem_free_security - Clear the security blob for sem
2459 * Clears the blob pointer
2461 static void smack_sem_free_security(struct sem_array *sma)
2463 struct kern_ipc_perm *isp = &sma->sem_perm;
2465 isp->security = NULL;
2469 * smk_curacc_sem : check if current has access on sem
2471 * @access : access requested
2473 * Returns 0 if current has the requested access, error code otherwise
2475 static int smk_curacc_sem(struct sem_array *sma, int access)
2477 char *ssp = smack_of_sem(sma);
2478 struct smk_audit_info ad;
2481 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2482 ad.a.u.ipc_id = sma->sem_perm.id;
2484 return smk_curacc(ssp, access, &ad);
2488 * smack_sem_associate - Smack access check for sem
2490 * @semflg: access requested
2492 * Returns 0 if current has the requested access, error code otherwise
2494 static int smack_sem_associate(struct sem_array *sma, int semflg)
2498 may = smack_flags_to_may(semflg);
2499 return smk_curacc_sem(sma, may);
2503 * smack_sem_shmctl - Smack access check for sem
2505 * @cmd: what it wants to do
2507 * Returns 0 if current has the requested access, error code otherwise
2509 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2527 may = MAY_READWRITE;
2532 * System level information
2539 return smk_curacc_sem(sma, may);
2543 * smack_sem_semop - Smack checks of semaphore operations
2549 * Treated as read and write in all cases.
2551 * Returns 0 if access is allowed, error code otherwise
2553 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2554 unsigned nsops, int alter)
2556 return smk_curacc_sem(sma, MAY_READWRITE);
2560 * smack_msg_alloc_security - Set the security blob for msg
2565 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2567 struct kern_ipc_perm *kisp = &msq->q_perm;
2568 struct smack_known *skp = smk_of_current();
2570 kisp->security = skp->smk_known;
2575 * smack_msg_free_security - Clear the security blob for msg
2578 * Clears the blob pointer
2580 static void smack_msg_queue_free_security(struct msg_queue *msq)
2582 struct kern_ipc_perm *kisp = &msq->q_perm;
2584 kisp->security = NULL;
2588 * smack_of_msq - the smack pointer for the msq
2591 * Returns a pointer to the smack value
2593 static char *smack_of_msq(struct msg_queue *msq)
2595 return (char *)msq->q_perm.security;
2599 * smk_curacc_msq : helper to check if current has access on msq
2601 * @access : access requested
2603 * return 0 if current has access, error otherwise
2605 static int smk_curacc_msq(struct msg_queue *msq, int access)
2607 char *msp = smack_of_msq(msq);
2608 struct smk_audit_info ad;
2611 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2612 ad.a.u.ipc_id = msq->q_perm.id;
2614 return smk_curacc(msp, access, &ad);
2618 * smack_msg_queue_associate - Smack access check for msg_queue
2620 * @msqflg: access requested
2622 * Returns 0 if current has the requested access, error code otherwise
2624 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2628 may = smack_flags_to_may(msqflg);
2629 return smk_curacc_msq(msq, may);
2633 * smack_msg_queue_msgctl - Smack access check for msg_queue
2635 * @cmd: what it wants to do
2637 * Returns 0 if current has the requested access, error code otherwise
2639 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2650 may = MAY_READWRITE;
2655 * System level information
2662 return smk_curacc_msq(msq, may);
2666 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2669 * @msqflg: access requested
2671 * Returns 0 if current has the requested access, error code otherwise
2673 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2678 may = smack_flags_to_may(msqflg);
2679 return smk_curacc_msq(msq, may);
2683 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2690 * Returns 0 if current has read and write access, error code otherwise
2692 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2693 struct task_struct *target, long type, int mode)
2695 return smk_curacc_msq(msq, MAY_READWRITE);
2699 * smack_ipc_permission - Smack access for ipc_permission()
2700 * @ipp: the object permissions
2701 * @flag: access requested
2703 * Returns 0 if current has read and write access, error code otherwise
2705 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2707 char *isp = ipp->security;
2708 int may = smack_flags_to_may(flag);
2709 struct smk_audit_info ad;
2712 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2713 ad.a.u.ipc_id = ipp->id;
2715 return smk_curacc(isp, may, &ad);
2719 * smack_ipc_getsecid - Extract smack security id
2720 * @ipp: the object permissions
2721 * @secid: where result will be saved
2723 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2725 char *smack = ipp->security;
2727 *secid = smack_to_secid(smack);
2731 * smack_d_instantiate - Make sure the blob is correct on an inode
2732 * @opt_dentry: dentry where inode will be attached
2733 * @inode: the object
2735 * Set the inode's security blob if it hasn't been done already.
2737 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2739 struct super_block *sbp;
2740 struct superblock_smack *sbsp;
2741 struct inode_smack *isp;
2742 struct smack_known *skp;
2743 struct smack_known *ckp = smk_of_current();
2745 char trattr[TRANS_TRUE_SIZE];
2753 isp = inode->i_security;
2755 mutex_lock(&isp->smk_lock);
2757 * If the inode is already instantiated
2758 * take the quick way out
2760 if (isp->smk_flags & SMK_INODE_INSTANT)
2764 sbsp = sbp->s_security;
2766 * We're going to use the superblock default label
2767 * if there's no label on the file.
2769 final = sbsp->smk_default;
2772 * If this is the root inode the superblock
2773 * may be in the process of initialization.
2774 * If that is the case use the root value out
2775 * of the superblock.
2777 if (opt_dentry->d_parent == opt_dentry) {
2778 if (sbp->s_magic == CGROUP_SUPER_MAGIC) {
2780 * The cgroup filesystem is never mounted,
2781 * so there's no opportunity to set the mount
2784 sbsp->smk_root = smack_known_star.smk_known;
2785 sbsp->smk_default = smack_known_star.smk_known;
2787 isp->smk_inode = sbsp->smk_root;
2788 isp->smk_flags |= SMK_INODE_INSTANT;
2793 * This is pretty hackish.
2794 * Casey says that we shouldn't have to do
2795 * file system specific code, but it does help
2796 * with keeping it simple.
2798 switch (sbp->s_magic) {
2802 case CGROUP_SUPER_MAGIC:
2804 * Casey says that it's a little embarrassing
2805 * that the smack file system doesn't do
2806 * extended attributes.
2808 * Casey says pipes are easy (?)
2810 * Socket access is controlled by the socket
2811 * structures associated with the task involved.
2813 * Cgroupfs is special
2815 final = smack_known_star.smk_known;
2817 case DEVPTS_SUPER_MAGIC:
2819 * devpts seems content with the label of the task.
2820 * Programs that change smack have to treat the
2823 final = ckp->smk_known;
2825 case PROC_SUPER_MAGIC:
2827 * Casey says procfs appears not to care.
2828 * The superblock default suffices.
2833 * Device labels should come from the filesystem,
2834 * but watch out, because they're volitile,
2835 * getting recreated on every reboot.
2837 final = smack_known_star.smk_known;
2841 * If a smack value has been set we want to use it,
2842 * but since tmpfs isn't giving us the opportunity
2843 * to set mount options simulate setting the
2844 * superblock default.
2848 * This isn't an understood special case.
2849 * Get the value from the xattr.
2853 * UNIX domain sockets use lower level socket data.
2855 if (S_ISSOCK(inode->i_mode)) {
2856 final = smack_known_star.smk_known;
2860 * No xattr support means, alas, no SMACK label.
2861 * Use the aforeapplied default.
2862 * It would be curious if the label of the task
2863 * does not match that assigned.
2865 if (inode->i_op->getxattr == NULL)
2868 * Get the dentry for xattr.
2870 dp = dget(opt_dentry);
2871 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2873 final = skp->smk_known;
2876 * Transmuting directory
2878 if (S_ISDIR(inode->i_mode)) {
2880 * If this is a new directory and the label was
2881 * transmuted when the inode was initialized
2882 * set the transmute attribute on the directory
2883 * and mark the inode.
2885 * If there is a transmute attribute on the
2886 * directory mark the inode.
2888 if (isp->smk_flags & SMK_INODE_CHANGED) {
2889 isp->smk_flags &= ~SMK_INODE_CHANGED;
2890 rc = inode->i_op->setxattr(dp,
2891 XATTR_NAME_SMACKTRANSMUTE,
2892 TRANS_TRUE, TRANS_TRUE_SIZE,
2895 rc = inode->i_op->getxattr(dp,
2896 XATTR_NAME_SMACKTRANSMUTE, trattr,
2898 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2899 TRANS_TRUE_SIZE) != 0)
2903 transflag = SMK_INODE_TRANSMUTE;
2906 * Don't let the exec or mmap label be "*" or "@".
2908 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2909 if (skp == &smack_known_star || skp == &smack_known_web)
2911 isp->smk_task = skp;
2912 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2913 if (skp == &smack_known_star || skp == &smack_known_web)
2915 isp->smk_mmap = skp;
2922 isp->smk_inode = ckp->smk_known;
2924 isp->smk_inode = final;
2926 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2929 mutex_unlock(&isp->smk_lock);
2934 * smack_getprocattr - Smack process attribute access
2935 * @p: the object task
2936 * @name: the name of the attribute in /proc/.../attr
2937 * @value: where to put the result
2939 * Places a copy of the task Smack into value
2941 * Returns the length of the smack label or an error code
2943 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2945 struct smack_known *skp = smk_of_task(task_security(p));
2949 if (strcmp(name, "current") != 0)
2952 cp = kstrdup(skp->smk_known, GFP_KERNEL);
2962 * smack_setprocattr - Smack process attribute setting
2963 * @p: the object task
2964 * @name: the name of the attribute in /proc/.../attr
2965 * @value: the value to set
2966 * @size: the size of the value
2968 * Sets the Smack value of the task. Only setting self
2969 * is permitted and only with privilege
2971 * Returns the length of the smack label or an error code
2973 static int smack_setprocattr(struct task_struct *p, char *name,
2974 void *value, size_t size)
2976 struct task_smack *tsp;
2978 struct smack_known *skp;
2981 * Changing another process' Smack value is too dangerous
2982 * and supports no sane use case.
2987 if (!smack_privileged(CAP_MAC_ADMIN))
2990 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
2993 if (strcmp(name, "current") != 0)
2996 skp = smk_import_entry(value, size);
3001 * No process is ever allowed the web ("@") label.
3003 if (skp == &smack_known_web)
3006 new = prepare_creds();
3010 tsp = new->security;
3011 tsp->smk_task = skp;
3018 * smack_unix_stream_connect - Smack access on UDS
3020 * @other: the other sock
3023 * Return 0 if a subject with the smack of sock could access
3024 * an object with the smack of other, otherwise an error code
3026 static int smack_unix_stream_connect(struct sock *sock,
3027 struct sock *other, struct sock *newsk)
3029 struct smack_known *skp;
3030 struct socket_smack *ssp = sock->sk_security;
3031 struct socket_smack *osp = other->sk_security;
3032 struct socket_smack *nsp = newsk->sk_security;
3033 struct smk_audit_info ad;
3037 struct lsm_network_audit net;
3039 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3040 smk_ad_setfield_u_net_sk(&ad, other);
3043 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3045 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3049 * Cross reference the peer labels for SO_PEERSEC.
3052 nsp->smk_packet = ssp->smk_out->smk_known;
3053 ssp->smk_packet = osp->smk_out->smk_known;
3060 * smack_unix_may_send - Smack access on UDS
3062 * @other: the other socket
3064 * Return 0 if a subject with the smack of sock could access
3065 * an object with the smack of other, otherwise an error code
3067 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3069 struct socket_smack *ssp = sock->sk->sk_security;
3070 struct socket_smack *osp = other->sk->sk_security;
3071 struct smack_known *skp;
3072 struct smk_audit_info ad;
3075 struct lsm_network_audit net;
3077 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3078 smk_ad_setfield_u_net_sk(&ad, other->sk);
3081 if (smack_privileged(CAP_MAC_OVERRIDE))
3085 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3089 * smack_socket_sendmsg - Smack check based on destination host
3092 * @size: the size of the message
3094 * Return 0 if the current subject can write to the destination host.
3095 * For IPv4 this is only a question if the destination is a single label host.
3096 * For IPv6 this is a check against the label of the port.
3098 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3101 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3102 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3106 * Perfectly reasonable for this to be NULL
3111 switch (sip->sin_family) {
3113 rc = smack_netlabel_send(sock->sk, sip);
3116 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3123 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3124 * @sap: netlabel secattr
3125 * @ssp: socket security information
3127 * Returns a pointer to a Smack label entry found on the label list.
3129 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3130 struct socket_smack *ssp)
3132 struct smack_known *skp;
3137 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3139 * Looks like a CIPSO packet.
3140 * If there are flags but no level netlabel isn't
3141 * behaving the way we expect it to.
3143 * Look it up in the label table
3144 * Without guidance regarding the smack value
3145 * for the packet fall back on the network
3149 list_for_each_entry(skp, &smack_known_list, list) {
3150 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3153 * Compare the catsets. Use the netlbl APIs.
3155 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3156 if ((skp->smk_netlabel.flags &
3157 NETLBL_SECATTR_MLS_CAT) == 0)
3161 for (acat = -1, kcat = -1; acat == kcat; ) {
3162 acat = netlbl_secattr_catmap_walk(
3163 sap->attr.mls.cat, acat + 1);
3164 kcat = netlbl_secattr_catmap_walk(
3165 skp->smk_netlabel.attr.mls.cat,
3167 if (acat < 0 || kcat < 0)
3180 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
3181 return &smack_known_web;
3182 return &smack_known_star;
3184 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3186 * Looks like a fallback, which gives us a secid.
3188 skp = smack_from_secid(sap->attr.secid);
3190 * This has got to be a bug because it is
3191 * impossible to specify a fallback without
3192 * specifying the label, which will ensure
3193 * it has a secid, and the only way to get a
3194 * secid is from a fallback.
3196 BUG_ON(skp == NULL);
3200 * Without guidance regarding the smack value
3201 * for the packet fall back on the network
3204 return smack_net_ambient;
3207 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3211 int proto = -EINVAL;
3212 struct ipv6hdr _ipv6h;
3213 struct ipv6hdr *ip6;
3215 struct tcphdr _tcph, *th;
3216 struct udphdr _udph, *uh;
3217 struct dccp_hdr _dccph, *dh;
3221 offset = skb_network_offset(skb);
3222 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3225 sip->sin6_addr = ip6->saddr;
3227 nexthdr = ip6->nexthdr;
3228 offset += sizeof(_ipv6h);
3229 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3236 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3238 sip->sin6_port = th->source;
3241 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3243 sip->sin6_port = uh->source;
3246 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3248 sip->sin6_port = dh->dccph_sport;
3255 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3259 * Returns 0 if the packet should be delivered, an error code otherwise
3261 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3263 struct netlbl_lsm_secattr secattr;
3264 struct socket_smack *ssp = sk->sk_security;
3265 struct smack_known *skp;
3266 struct sockaddr_in6 sadd;
3268 struct smk_audit_info ad;
3270 struct lsm_network_audit net;
3272 switch (sk->sk_family) {
3275 * Translate what netlabel gave us.
3277 netlbl_secattr_init(&secattr);
3279 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3281 skp = smack_from_secattr(&secattr, ssp);
3283 skp = smack_net_ambient;
3285 netlbl_secattr_destroy(&secattr);
3288 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3289 ad.a.u.net->family = sk->sk_family;
3290 ad.a.u.net->netif = skb->skb_iif;
3291 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3294 * Receiving a packet requires that the other end
3295 * be able to write here. Read access is not required.
3296 * This is the simplist possible security model
3299 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3301 netlbl_skbuff_err(skb, rc, 0);
3304 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3305 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3306 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3315 * smack_socket_getpeersec_stream - pull in packet label
3317 * @optval: user's destination
3318 * @optlen: size thereof
3321 * returns zero on success, an error code otherwise
3323 static int smack_socket_getpeersec_stream(struct socket *sock,
3324 char __user *optval,
3325 int __user *optlen, unsigned len)
3327 struct socket_smack *ssp;
3332 ssp = sock->sk->sk_security;
3333 if (ssp->smk_packet != NULL) {
3334 rcp = ssp->smk_packet;
3335 slen = strlen(rcp) + 1;
3340 else if (copy_to_user(optval, rcp, slen) != 0)
3343 if (put_user(slen, optlen) != 0)
3351 * smack_socket_getpeersec_dgram - pull in packet label
3352 * @sock: the peer socket
3354 * @secid: pointer to where to put the secid of the packet
3356 * Sets the netlabel socket state on sk from parent
3358 static int smack_socket_getpeersec_dgram(struct socket *sock,
3359 struct sk_buff *skb, u32 *secid)
3362 struct netlbl_lsm_secattr secattr;
3363 struct socket_smack *ssp = NULL;
3364 struct smack_known *skp;
3365 int family = PF_UNSPEC;
3366 u32 s = 0; /* 0 is the invalid secid */
3370 if (skb->protocol == htons(ETH_P_IP))
3372 else if (skb->protocol == htons(ETH_P_IPV6))
3375 if (family == PF_UNSPEC && sock != NULL)
3376 family = sock->sk->sk_family;
3378 if (family == PF_UNIX) {
3379 ssp = sock->sk->sk_security;
3380 s = ssp->smk_out->smk_secid;
3381 } else if (family == PF_INET || family == PF_INET6) {
3383 * Translate what netlabel gave us.
3385 if (sock != NULL && sock->sk != NULL)
3386 ssp = sock->sk->sk_security;
3387 netlbl_secattr_init(&secattr);
3388 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3390 skp = smack_from_secattr(&secattr, ssp);
3393 netlbl_secattr_destroy(&secattr);
3402 * smack_sock_graft - Initialize a newly created socket with an existing sock
3404 * @parent: parent socket
3406 * Set the smk_{in,out} state of an existing sock based on the process that
3407 * is creating the new socket.
3409 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3411 struct socket_smack *ssp;
3412 struct smack_known *skp = smk_of_current();
3415 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3418 ssp = sk->sk_security;
3419 ssp->smk_in = skp->smk_known;
3421 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3425 * smack_inet_conn_request - Smack access check on connect
3426 * @sk: socket involved
3430 * Returns 0 if a task with the packet label could write to
3431 * the socket, otherwise an error code
3433 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3434 struct request_sock *req)
3436 u16 family = sk->sk_family;
3437 struct smack_known *skp;
3438 struct socket_smack *ssp = sk->sk_security;
3439 struct netlbl_lsm_secattr secattr;
3440 struct sockaddr_in addr;
3444 struct smk_audit_info ad;
3446 struct lsm_network_audit net;
3449 if (family == PF_INET6) {
3451 * Handle mapped IPv4 packets arriving
3452 * via IPv6 sockets. Don't set up netlabel
3453 * processing on IPv6.
3455 if (skb->protocol == htons(ETH_P_IP))
3461 netlbl_secattr_init(&secattr);
3462 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3464 skp = smack_from_secattr(&secattr, ssp);
3466 skp = &smack_known_huh;
3467 netlbl_secattr_destroy(&secattr);
3470 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3471 ad.a.u.net->family = family;
3472 ad.a.u.net->netif = skb->skb_iif;
3473 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3476 * Receiving a packet requires that the other end be able to write
3477 * here. Read access is not required.
3479 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3484 * Save the peer's label in the request_sock so we can later setup
3485 * smk_packet in the child socket so that SO_PEERCRED can report it.
3487 req->peer_secid = skp->smk_secid;
3490 * We need to decide if we want to label the incoming connection here
3491 * if we do we only need to label the request_sock and the stack will
3492 * propagate the wire-label to the sock when it is created.
3495 addr.sin_addr.s_addr = hdr->saddr;
3497 hsp = smack_host_label(&addr);
3501 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3503 netlbl_req_delattr(req);
3509 * smack_inet_csk_clone - Copy the connection information to the new socket
3510 * @sk: the new socket
3511 * @req: the connection's request_sock
3513 * Transfer the connection's peer label to the newly created socket.
3515 static void smack_inet_csk_clone(struct sock *sk,
3516 const struct request_sock *req)
3518 struct socket_smack *ssp = sk->sk_security;
3519 struct smack_known *skp;
3521 if (req->peer_secid != 0) {
3522 skp = smack_from_secid(req->peer_secid);
3523 ssp->smk_packet = skp->smk_known;
3525 ssp->smk_packet = NULL;
3529 * Key management security hooks
3531 * Casey has not tested key support very heavily.
3532 * The permission check is most likely too restrictive.
3533 * If you care about keys please have a look.
3538 * smack_key_alloc - Set the key security blob
3540 * @cred: the credentials to use
3543 * No allocation required
3547 static int smack_key_alloc(struct key *key, const struct cred *cred,
3548 unsigned long flags)
3550 struct smack_known *skp = smk_of_task(cred->security);
3552 key->security = skp->smk_known;
3557 * smack_key_free - Clear the key security blob
3560 * Clear the blob pointer
3562 static void smack_key_free(struct key *key)
3564 key->security = NULL;
3568 * smack_key_permission - Smack access on a key
3569 * @key_ref: gets to the object
3570 * @cred: the credentials to use
3573 * Return 0 if the task has read and write to the object,
3574 * an error code otherwise
3576 static int smack_key_permission(key_ref_t key_ref,
3577 const struct cred *cred, unsigned perm)
3580 struct smk_audit_info ad;
3581 struct smack_known *tkp = smk_of_task(cred->security);
3584 keyp = key_ref_to_ptr(key_ref);
3588 * If the key hasn't been initialized give it access so that
3591 if (keyp->security == NULL)
3594 * This should not occur
3599 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3600 ad.a.u.key_struct.key = keyp->serial;
3601 ad.a.u.key_struct.key_desc = keyp->description;
3603 if (perm & KEY_NEED_READ)
3605 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
3606 request = MAY_WRITE;
3607 return smk_access(tkp, keyp->security, request, &ad);
3609 #endif /* CONFIG_KEYS */
3614 * Audit requires a unique representation of each Smack specific
3615 * rule. This unique representation is used to distinguish the
3616 * object to be audited from remaining kernel objects and also
3617 * works as a glue between the audit hooks.
3619 * Since repository entries are added but never deleted, we'll use
3620 * the smack_known label address related to the given audit rule as
3621 * the needed unique representation. This also better fits the smack
3622 * model where nearly everything is a label.
3627 * smack_audit_rule_init - Initialize a smack audit rule
3628 * @field: audit rule fields given from user-space (audit.h)
3629 * @op: required testing operator (=, !=, >, <, ...)
3630 * @rulestr: smack label to be audited
3631 * @vrule: pointer to save our own audit rule representation
3633 * Prepare to audit cases where (@field @op @rulestr) is true.
3634 * The label to be audited is created if necessay.
3636 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3638 char **rule = (char **)vrule;
3641 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3644 if (op != Audit_equal && op != Audit_not_equal)
3647 *rule = smk_import(rulestr, 0);
3653 * smack_audit_rule_known - Distinguish Smack audit rules
3654 * @krule: rule of interest, in Audit kernel representation format
3656 * This is used to filter Smack rules from remaining Audit ones.
3657 * If it's proved that this rule belongs to us, the
3658 * audit_rule_match hook will be called to do the final judgement.
3660 static int smack_audit_rule_known(struct audit_krule *krule)
3662 struct audit_field *f;
3665 for (i = 0; i < krule->field_count; i++) {
3666 f = &krule->fields[i];
3668 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3676 * smack_audit_rule_match - Audit given object ?
3677 * @secid: security id for identifying the object to test
3678 * @field: audit rule flags given from user-space
3679 * @op: required testing operator
3680 * @vrule: smack internal rule presentation
3681 * @actx: audit context associated with the check
3683 * The core Audit hook. It's used to take the decision of
3684 * whether to audit or not to audit a given object.
3686 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3687 struct audit_context *actx)
3689 struct smack_known *skp;
3692 if (unlikely(!rule)) {
3693 WARN_ONCE(1, "Smack: missing rule\n");
3697 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3700 skp = smack_from_secid(secid);
3703 * No need to do string comparisons. If a match occurs,
3704 * both pointers will point to the same smack_known
3707 if (op == Audit_equal)
3708 return (rule == skp->smk_known);
3709 if (op == Audit_not_equal)
3710 return (rule != skp->smk_known);
3716 * smack_audit_rule_free - free smack rule representation
3717 * @vrule: rule to be freed.
3719 * No memory was allocated.
3721 static void smack_audit_rule_free(void *vrule)
3726 #endif /* CONFIG_AUDIT */
3729 * smack_ismaclabel - check if xattr @name references a smack MAC label
3730 * @name: Full xattr name to check.
3732 static int smack_ismaclabel(const char *name)
3734 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3739 * smack_secid_to_secctx - return the smack label for a secid
3740 * @secid: incoming integer
3741 * @secdata: destination
3742 * @seclen: how long it is
3744 * Exists for networking code.
3746 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3748 struct smack_known *skp = smack_from_secid(secid);
3751 *secdata = skp->smk_known;
3752 *seclen = strlen(skp->smk_known);
3757 * smack_secctx_to_secid - return the secid for a smack label
3758 * @secdata: smack label
3759 * @seclen: how long result is
3760 * @secid: outgoing integer
3762 * Exists for audit and networking code.
3764 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3766 *secid = smack_to_secid(secdata);
3771 * smack_release_secctx - don't do anything.
3775 * Exists to make sure nothing gets done, and properly
3777 static void smack_release_secctx(char *secdata, u32 seclen)
3781 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3783 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3786 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3788 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3791 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3794 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3802 struct security_operations smack_ops = {
3805 .ptrace_access_check = smack_ptrace_access_check,
3806 .ptrace_traceme = smack_ptrace_traceme,
3807 .syslog = smack_syslog,
3809 .sb_alloc_security = smack_sb_alloc_security,
3810 .sb_free_security = smack_sb_free_security,
3811 .sb_copy_data = smack_sb_copy_data,
3812 .sb_kern_mount = smack_sb_kern_mount,
3813 .sb_statfs = smack_sb_statfs,
3815 .bprm_set_creds = smack_bprm_set_creds,
3816 .bprm_committing_creds = smack_bprm_committing_creds,
3817 .bprm_secureexec = smack_bprm_secureexec,
3819 .inode_alloc_security = smack_inode_alloc_security,
3820 .inode_free_security = smack_inode_free_security,
3821 .inode_init_security = smack_inode_init_security,
3822 .inode_link = smack_inode_link,
3823 .inode_unlink = smack_inode_unlink,
3824 .inode_rmdir = smack_inode_rmdir,
3825 .inode_rename = smack_inode_rename,
3826 .inode_permission = smack_inode_permission,
3827 .inode_setattr = smack_inode_setattr,
3828 .inode_getattr = smack_inode_getattr,
3829 .inode_setxattr = smack_inode_setxattr,
3830 .inode_post_setxattr = smack_inode_post_setxattr,
3831 .inode_getxattr = smack_inode_getxattr,
3832 .inode_removexattr = smack_inode_removexattr,
3833 .inode_getsecurity = smack_inode_getsecurity,
3834 .inode_setsecurity = smack_inode_setsecurity,
3835 .inode_listsecurity = smack_inode_listsecurity,
3836 .inode_getsecid = smack_inode_getsecid,
3838 .file_permission = smack_file_permission,
3839 .file_alloc_security = smack_file_alloc_security,
3840 .file_free_security = smack_file_free_security,
3841 .file_ioctl = smack_file_ioctl,
3842 .file_lock = smack_file_lock,
3843 .file_fcntl = smack_file_fcntl,
3844 .mmap_file = smack_mmap_file,
3845 .mmap_addr = cap_mmap_addr,
3846 .file_set_fowner = smack_file_set_fowner,
3847 .file_send_sigiotask = smack_file_send_sigiotask,
3848 .file_receive = smack_file_receive,
3850 .file_open = smack_file_open,
3852 .cred_alloc_blank = smack_cred_alloc_blank,
3853 .cred_free = smack_cred_free,
3854 .cred_prepare = smack_cred_prepare,
3855 .cred_transfer = smack_cred_transfer,
3856 .kernel_act_as = smack_kernel_act_as,
3857 .kernel_create_files_as = smack_kernel_create_files_as,
3858 .task_setpgid = smack_task_setpgid,
3859 .task_getpgid = smack_task_getpgid,
3860 .task_getsid = smack_task_getsid,
3861 .task_getsecid = smack_task_getsecid,
3862 .task_setnice = smack_task_setnice,
3863 .task_setioprio = smack_task_setioprio,
3864 .task_getioprio = smack_task_getioprio,
3865 .task_setscheduler = smack_task_setscheduler,
3866 .task_getscheduler = smack_task_getscheduler,
3867 .task_movememory = smack_task_movememory,
3868 .task_kill = smack_task_kill,
3869 .task_wait = smack_task_wait,
3870 .task_to_inode = smack_task_to_inode,
3872 .ipc_permission = smack_ipc_permission,
3873 .ipc_getsecid = smack_ipc_getsecid,
3875 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3876 .msg_msg_free_security = smack_msg_msg_free_security,
3878 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3879 .msg_queue_free_security = smack_msg_queue_free_security,
3880 .msg_queue_associate = smack_msg_queue_associate,
3881 .msg_queue_msgctl = smack_msg_queue_msgctl,
3882 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3883 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3885 .shm_alloc_security = smack_shm_alloc_security,
3886 .shm_free_security = smack_shm_free_security,
3887 .shm_associate = smack_shm_associate,
3888 .shm_shmctl = smack_shm_shmctl,
3889 .shm_shmat = smack_shm_shmat,
3891 .sem_alloc_security = smack_sem_alloc_security,
3892 .sem_free_security = smack_sem_free_security,
3893 .sem_associate = smack_sem_associate,
3894 .sem_semctl = smack_sem_semctl,
3895 .sem_semop = smack_sem_semop,
3897 .d_instantiate = smack_d_instantiate,
3899 .getprocattr = smack_getprocattr,
3900 .setprocattr = smack_setprocattr,
3902 .unix_stream_connect = smack_unix_stream_connect,
3903 .unix_may_send = smack_unix_may_send,
3905 .socket_post_create = smack_socket_post_create,
3906 .socket_bind = smack_socket_bind,
3907 .socket_connect = smack_socket_connect,
3908 .socket_sendmsg = smack_socket_sendmsg,
3909 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3910 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3911 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3912 .sk_alloc_security = smack_sk_alloc_security,
3913 .sk_free_security = smack_sk_free_security,
3914 .sock_graft = smack_sock_graft,
3915 .inet_conn_request = smack_inet_conn_request,
3916 .inet_csk_clone = smack_inet_csk_clone,
3918 /* key management security hooks */
3920 .key_alloc = smack_key_alloc,
3921 .key_free = smack_key_free,
3922 .key_permission = smack_key_permission,
3923 #endif /* CONFIG_KEYS */
3927 .audit_rule_init = smack_audit_rule_init,
3928 .audit_rule_known = smack_audit_rule_known,
3929 .audit_rule_match = smack_audit_rule_match,
3930 .audit_rule_free = smack_audit_rule_free,
3931 #endif /* CONFIG_AUDIT */
3933 .ismaclabel = smack_ismaclabel,
3934 .secid_to_secctx = smack_secid_to_secctx,
3935 .secctx_to_secid = smack_secctx_to_secid,
3936 .release_secctx = smack_release_secctx,
3937 .inode_notifysecctx = smack_inode_notifysecctx,
3938 .inode_setsecctx = smack_inode_setsecctx,
3939 .inode_getsecctx = smack_inode_getsecctx,
3943 static __init void init_smack_known_list(void)
3946 * Initialize rule list locks
3948 mutex_init(&smack_known_huh.smk_rules_lock);
3949 mutex_init(&smack_known_hat.smk_rules_lock);
3950 mutex_init(&smack_known_floor.smk_rules_lock);
3951 mutex_init(&smack_known_star.smk_rules_lock);
3952 mutex_init(&smack_known_invalid.smk_rules_lock);
3953 mutex_init(&smack_known_web.smk_rules_lock);
3955 * Initialize rule lists
3957 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3958 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3959 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3960 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3961 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3962 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3964 * Create the known labels list
3966 smk_insert_entry(&smack_known_huh);
3967 smk_insert_entry(&smack_known_hat);
3968 smk_insert_entry(&smack_known_star);
3969 smk_insert_entry(&smack_known_floor);
3970 smk_insert_entry(&smack_known_invalid);
3971 smk_insert_entry(&smack_known_web);
3975 * smack_init - initialize the smack system
3979 static __init int smack_init(void)
3982 struct task_smack *tsp;
3984 if (!security_module_enable(&smack_ops))
3987 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3992 printk(KERN_INFO "Smack: Initializing.\n");
3995 * Set the security state for the initial task.
3997 cred = (struct cred *) current->cred;
3998 cred->security = tsp;
4000 /* initialize the smack_known_list */
4001 init_smack_known_list();
4006 if (register_security(&smack_ops))
4007 panic("smack: Unable to register with kernel.\n");
4013 * Smack requires early initialization in order to label
4014 * all processes and objects when they are created.
4016 security_initcall(smack_init);