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 TRANS_TRUE "TRUE"
47 #define TRANS_TRUE_SIZE 4
49 #define SMK_CONNECTING 0
50 #define SMK_RECEIVING 1
53 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
54 LIST_HEAD(smk_ipv6_port_list);
55 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
56 static struct kmem_cache *smack_inode_cache;
59 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
60 static char *smk_bu_mess[] = {
61 "Bringup Error", /* Unused */
62 "Bringup", /* SMACK_BRINGUP_ALLOW */
63 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
64 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
67 static void smk_bu_mode(int mode, char *s)
77 if (mode & MAY_APPEND)
79 if (mode & MAY_TRANSMUTE)
89 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
90 static int smk_bu_note(char *note, struct smack_known *sskp,
91 struct smack_known *oskp, int mode, int rc)
93 char acc[SMK_NUM_ACCESS_TYPE + 1];
97 if (rc > SMACK_UNCONFINED_OBJECT)
100 smk_bu_mode(mode, acc);
101 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
102 sskp->smk_known, oskp->smk_known, acc, note);
106 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
109 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
110 static int smk_bu_current(char *note, struct smack_known *oskp,
113 struct task_smack *tsp = current_security();
114 char acc[SMK_NUM_ACCESS_TYPE + 1];
118 if (rc > SMACK_UNCONFINED_OBJECT)
121 smk_bu_mode(mode, acc);
122 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
123 tsp->smk_task->smk_known, oskp->smk_known,
124 acc, current->comm, note);
128 #define smk_bu_current(note, oskp, mode, RC) (RC)
131 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
132 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
134 struct task_smack *tsp = current_security();
135 struct smack_known *smk_task = smk_of_task_struct(otp);
136 char acc[SMK_NUM_ACCESS_TYPE + 1];
140 if (rc > SMACK_UNCONFINED_OBJECT)
143 smk_bu_mode(mode, acc);
144 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
145 tsp->smk_task->smk_known, smk_task->smk_known, acc,
146 current->comm, otp->comm);
150 #define smk_bu_task(otp, mode, RC) (RC)
153 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
154 static int smk_bu_inode(struct inode *inode, int mode, int rc)
156 struct task_smack *tsp = current_security();
157 struct inode_smack *isp = inode->i_security;
158 char acc[SMK_NUM_ACCESS_TYPE + 1];
160 if (isp->smk_flags & SMK_INODE_IMPURE)
161 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
162 inode->i_sb->s_id, inode->i_ino, current->comm);
166 if (rc > SMACK_UNCONFINED_OBJECT)
168 if (rc == SMACK_UNCONFINED_SUBJECT &&
169 (mode & (MAY_WRITE | MAY_APPEND)))
170 isp->smk_flags |= SMK_INODE_IMPURE;
172 smk_bu_mode(mode, acc);
174 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
175 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
176 inode->i_sb->s_id, inode->i_ino, current->comm);
180 #define smk_bu_inode(inode, mode, RC) (RC)
183 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
184 static int smk_bu_file(struct file *file, int mode, int rc)
186 struct task_smack *tsp = current_security();
187 struct smack_known *sskp = tsp->smk_task;
188 struct inode *inode = file_inode(file);
189 struct inode_smack *isp = inode->i_security;
190 char acc[SMK_NUM_ACCESS_TYPE + 1];
192 if (isp->smk_flags & SMK_INODE_IMPURE)
193 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
194 inode->i_sb->s_id, inode->i_ino, current->comm);
198 if (rc > SMACK_UNCONFINED_OBJECT)
201 smk_bu_mode(mode, acc);
202 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
203 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
204 inode->i_sb->s_id, inode->i_ino, file,
209 #define smk_bu_file(file, mode, RC) (RC)
212 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
213 static int smk_bu_credfile(const struct cred *cred, struct file *file,
216 struct task_smack *tsp = cred->security;
217 struct smack_known *sskp = tsp->smk_task;
218 struct inode *inode = file->f_inode;
219 struct inode_smack *isp = inode->i_security;
220 char acc[SMK_NUM_ACCESS_TYPE + 1];
222 if (isp->smk_flags & SMK_INODE_IMPURE)
223 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
224 inode->i_sb->s_id, inode->i_ino, current->comm);
228 if (rc > SMACK_UNCONFINED_OBJECT)
231 smk_bu_mode(mode, acc);
232 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
233 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
234 inode->i_sb->s_id, inode->i_ino, file,
239 #define smk_bu_credfile(cred, file, mode, RC) (RC)
243 * smk_fetch - Fetch the smack label from a file.
244 * @name: type of the label (attribute)
245 * @ip: a pointer to the inode
246 * @dp: a pointer to the dentry
248 * Returns a pointer to the master list entry for the Smack label
249 * or NULL if there was no label to fetch.
251 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
256 struct smack_known *skp = NULL;
258 if (ip->i_op->getxattr == NULL)
261 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
265 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
267 skp = smk_import_entry(buffer, rc);
275 * new_inode_smack - allocate an inode security blob
276 * @skp: a pointer to the Smack label entry to use in the blob
278 * Returns the new blob or NULL if there's no memory available
280 struct inode_smack *new_inode_smack(struct smack_known *skp)
282 struct inode_smack *isp;
284 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
288 isp->smk_inode = skp;
290 mutex_init(&isp->smk_lock);
296 * new_task_smack - allocate a task security blob
297 * @task: a pointer to the Smack label for the running task
298 * @forked: a pointer to the Smack label for the forked task
299 * @gfp: type of the memory for the allocation
301 * Returns the new blob or NULL if there's no memory available
303 static struct task_smack *new_task_smack(struct smack_known *task,
304 struct smack_known *forked, gfp_t gfp)
306 struct task_smack *tsp;
308 tsp = kzalloc(sizeof(struct task_smack), gfp);
312 tsp->smk_task = task;
313 tsp->smk_forked = forked;
314 INIT_LIST_HEAD(&tsp->smk_rules);
315 mutex_init(&tsp->smk_rules_lock);
321 * smk_copy_rules - copy a rule set
322 * @nhead: new rules header pointer
323 * @ohead: old rules header pointer
324 * @gfp: type of the memory for the allocation
326 * Returns 0 on success, -ENOMEM on error
328 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
331 struct smack_rule *nrp;
332 struct smack_rule *orp;
335 INIT_LIST_HEAD(nhead);
337 list_for_each_entry_rcu(orp, ohead, list) {
338 nrp = kzalloc(sizeof(struct smack_rule), gfp);
344 list_add_rcu(&nrp->list, nhead);
350 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
351 * @mode - input mode in form of PTRACE_MODE_*
353 * Returns a converted MAY_* mode usable by smack rules
355 static inline unsigned int smk_ptrace_mode(unsigned int mode)
358 case PTRACE_MODE_READ:
360 case PTRACE_MODE_ATTACH:
361 return MAY_READWRITE;
368 * smk_ptrace_rule_check - helper for ptrace access
369 * @tracer: tracer process
370 * @tracee_known: label entry of the process that's about to be traced
371 * @mode: ptrace attachment mode (PTRACE_MODE_*)
372 * @func: name of the function that called us, used for audit
374 * Returns 0 on access granted, -error on error
376 static int smk_ptrace_rule_check(struct task_struct *tracer,
377 struct smack_known *tracee_known,
378 unsigned int mode, const char *func)
381 struct smk_audit_info ad, *saip = NULL;
382 struct task_smack *tsp;
383 struct smack_known *tracer_known;
385 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
386 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
387 smk_ad_setfield_u_tsk(&ad, tracer);
392 tsp = __task_cred(tracer)->security;
393 tracer_known = smk_of_task(tsp);
395 if ((mode & PTRACE_MODE_ATTACH) &&
396 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
397 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
398 if (tracer_known->smk_known == tracee_known->smk_known)
400 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
402 else if (capable(CAP_SYS_PTRACE))
408 smack_log(tracer_known->smk_known,
409 tracee_known->smk_known,
416 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
417 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
425 * We he, that is fun!
429 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
430 * @ctp: child task pointer
431 * @mode: ptrace attachment mode (PTRACE_MODE_*)
433 * Returns 0 if access is OK, an error code otherwise
435 * Do the capability checks.
437 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
439 struct smack_known *skp;
441 skp = smk_of_task_struct(ctp);
443 return smk_ptrace_rule_check(current, skp, mode, __func__);
447 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
448 * @ptp: parent task pointer
450 * Returns 0 if access is OK, an error code otherwise
452 * Do the capability checks, and require PTRACE_MODE_ATTACH.
454 static int smack_ptrace_traceme(struct task_struct *ptp)
457 struct smack_known *skp;
459 skp = smk_of_task(current_security());
461 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
466 * smack_syslog - Smack approval on syslog
467 * @type: message type
469 * Returns 0 on success, error code otherwise.
471 static int smack_syslog(int typefrom_file)
474 struct smack_known *skp = smk_of_current();
476 if (smack_privileged(CAP_MAC_OVERRIDE))
479 if (smack_syslog_label != NULL && smack_syslog_label != skp)
491 * smack_sb_alloc_security - allocate a superblock blob
492 * @sb: the superblock getting the blob
494 * Returns 0 on success or -ENOMEM on error.
496 static int smack_sb_alloc_security(struct super_block *sb)
498 struct superblock_smack *sbsp;
500 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
505 sbsp->smk_root = &smack_known_floor;
506 sbsp->smk_default = &smack_known_floor;
507 sbsp->smk_floor = &smack_known_floor;
508 sbsp->smk_hat = &smack_known_hat;
510 * smk_initialized will be zero from kzalloc.
512 sb->s_security = sbsp;
518 * smack_sb_free_security - free a superblock blob
519 * @sb: the superblock getting the blob
522 static void smack_sb_free_security(struct super_block *sb)
524 kfree(sb->s_security);
525 sb->s_security = NULL;
529 * smack_sb_copy_data - copy mount options data for processing
530 * @orig: where to start
531 * @smackopts: mount options string
533 * Returns 0 on success or -ENOMEM on error.
535 * Copy the Smack specific mount options out of the mount
538 static int smack_sb_copy_data(char *orig, char *smackopts)
540 char *cp, *commap, *otheropts, *dp;
542 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
543 if (otheropts == NULL)
546 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
547 if (strstr(cp, SMK_FSDEFAULT) == cp)
549 else if (strstr(cp, SMK_FSFLOOR) == cp)
551 else if (strstr(cp, SMK_FSHAT) == cp)
553 else if (strstr(cp, SMK_FSROOT) == cp)
555 else if (strstr(cp, SMK_FSTRANS) == cp)
560 commap = strchr(cp, ',');
569 strcpy(orig, otheropts);
570 free_page((unsigned long)otheropts);
576 * smack_sb_kern_mount - Smack specific mount processing
577 * @sb: the file system superblock
578 * @flags: the mount flags
579 * @data: the smack mount options
581 * Returns 0 on success, an error code on failure
583 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
585 struct dentry *root = sb->s_root;
586 struct inode *inode = d_backing_inode(root);
587 struct superblock_smack *sp = sb->s_security;
588 struct inode_smack *isp;
589 struct smack_known *skp;
595 if (sp->smk_initialized)
598 sp->smk_initialized = 1;
600 for (op = data; op != NULL; op = commap) {
601 commap = strchr(op, ',');
605 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
606 op += strlen(SMK_FSHAT);
607 skp = smk_import_entry(op, 0);
612 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
613 op += strlen(SMK_FSFLOOR);
614 skp = smk_import_entry(op, 0);
619 } else if (strncmp(op, SMK_FSDEFAULT,
620 strlen(SMK_FSDEFAULT)) == 0) {
621 op += strlen(SMK_FSDEFAULT);
622 skp = smk_import_entry(op, 0);
624 sp->smk_default = skp;
627 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
628 op += strlen(SMK_FSROOT);
629 skp = smk_import_entry(op, 0);
634 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
635 op += strlen(SMK_FSTRANS);
636 skp = smk_import_entry(op, 0);
645 if (!smack_privileged(CAP_MAC_ADMIN)) {
647 * Unprivileged mounts don't get to specify Smack values.
652 * Unprivileged mounts get root and default from the caller.
654 skp = smk_of_current();
656 sp->smk_default = skp;
659 * Initialize the root inode.
661 isp = inode->i_security;
663 isp = new_inode_smack(sp->smk_root);
666 inode->i_security = isp;
668 isp->smk_inode = sp->smk_root;
671 isp->smk_flags |= SMK_INODE_TRANSMUTE;
677 * smack_sb_statfs - Smack check on statfs
678 * @dentry: identifies the file system in question
680 * Returns 0 if current can read the floor of the filesystem,
681 * and error code otherwise
683 static int smack_sb_statfs(struct dentry *dentry)
685 struct superblock_smack *sbp = dentry->d_sb->s_security;
687 struct smk_audit_info ad;
689 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
690 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
692 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
693 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
702 * smack_bprm_set_creds - set creds for exec
703 * @bprm: the exec information
705 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
707 static int smack_bprm_set_creds(struct linux_binprm *bprm)
709 struct inode *inode = file_inode(bprm->file);
710 struct task_smack *bsp = bprm->cred->security;
711 struct inode_smack *isp;
714 if (bprm->cred_prepared)
717 isp = inode->i_security;
718 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
721 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
722 struct task_struct *tracer;
726 tracer = ptrace_parent(current);
727 if (likely(tracer != NULL))
728 rc = smk_ptrace_rule_check(tracer,
736 } else if (bprm->unsafe)
739 bsp->smk_task = isp->smk_task;
740 bprm->per_clear |= PER_CLEAR_ON_SETID;
746 * smack_bprm_committing_creds - Prepare to install the new credentials
749 * @bprm: binprm for exec
751 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
753 struct task_smack *bsp = bprm->cred->security;
755 if (bsp->smk_task != bsp->smk_forked)
756 current->pdeath_signal = 0;
760 * smack_bprm_secureexec - Return the decision to use secureexec.
761 * @bprm: binprm for exec
763 * Returns 0 on success.
765 static int smack_bprm_secureexec(struct linux_binprm *bprm)
767 struct task_smack *tsp = current_security();
769 if (tsp->smk_task != tsp->smk_forked)
780 * smack_inode_alloc_security - allocate an inode blob
781 * @inode: the inode in need of a blob
783 * Returns 0 if it gets a blob, -ENOMEM otherwise
785 static int smack_inode_alloc_security(struct inode *inode)
787 struct smack_known *skp = smk_of_current();
789 inode->i_security = new_inode_smack(skp);
790 if (inode->i_security == NULL)
796 * smack_inode_free_security - free an inode blob
797 * @inode: the inode with a blob
799 * Clears the blob pointer in inode
801 static void smack_inode_free_security(struct inode *inode)
803 kmem_cache_free(smack_inode_cache, inode->i_security);
804 inode->i_security = NULL;
808 * smack_inode_init_security - copy out the smack from an inode
809 * @inode: the newly created inode
810 * @dir: containing directory object
812 * @name: where to put the attribute name
813 * @value: where to put the attribute value
814 * @len: where to put the length of the attribute
816 * Returns 0 if it all works out, -ENOMEM if there's no memory
818 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
819 const struct qstr *qstr, const char **name,
820 void **value, size_t *len)
822 struct inode_smack *issp = inode->i_security;
823 struct smack_known *skp = smk_of_current();
824 struct smack_known *isp = smk_of_inode(inode);
825 struct smack_known *dsp = smk_of_inode(dir);
829 *name = XATTR_SMACK_SUFFIX;
833 may = smk_access_entry(skp->smk_known, dsp->smk_known,
838 * If the access rule allows transmutation and
839 * the directory requests transmutation then
840 * by all means transmute.
841 * Mark the inode as changed.
843 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
844 smk_inode_transmutable(dir)) {
846 issp->smk_flags |= SMK_INODE_CHANGED;
849 *value = kstrdup(isp->smk_known, GFP_NOFS);
853 *len = strlen(isp->smk_known);
860 * smack_inode_link - Smack check on link
861 * @old_dentry: the existing object
863 * @new_dentry: the new object
865 * Returns 0 if access is permitted, an error code otherwise
867 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
868 struct dentry *new_dentry)
870 struct smack_known *isp;
871 struct smk_audit_info ad;
874 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
875 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
877 isp = smk_of_inode(d_backing_inode(old_dentry));
878 rc = smk_curacc(isp, MAY_WRITE, &ad);
879 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
881 if (rc == 0 && d_is_positive(new_dentry)) {
882 isp = smk_of_inode(d_backing_inode(new_dentry));
883 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
884 rc = smk_curacc(isp, MAY_WRITE, &ad);
885 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
892 * smack_inode_unlink - Smack check on inode deletion
893 * @dir: containing directory object
894 * @dentry: file to unlink
896 * Returns 0 if current can write the containing directory
897 * and the object, error code otherwise
899 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
901 struct inode *ip = d_backing_inode(dentry);
902 struct smk_audit_info ad;
905 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
906 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
909 * You need write access to the thing you're unlinking
911 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
912 rc = smk_bu_inode(ip, MAY_WRITE, rc);
915 * You also need write access to the containing directory
917 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
918 smk_ad_setfield_u_fs_inode(&ad, dir);
919 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
920 rc = smk_bu_inode(dir, MAY_WRITE, rc);
926 * smack_inode_rmdir - Smack check on directory deletion
927 * @dir: containing directory object
928 * @dentry: directory to unlink
930 * Returns 0 if current can write the containing directory
931 * and the directory, error code otherwise
933 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
935 struct smk_audit_info ad;
938 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
939 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
942 * You need write access to the thing you're removing
944 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
945 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
948 * You also need write access to the containing directory
950 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
951 smk_ad_setfield_u_fs_inode(&ad, dir);
952 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
953 rc = smk_bu_inode(dir, MAY_WRITE, rc);
960 * smack_inode_rename - Smack check on rename
962 * @old_dentry: the old object
964 * @new_dentry: the new object
966 * Read and write access is required on both the old and
969 * Returns 0 if access is permitted, an error code otherwise
971 static int smack_inode_rename(struct inode *old_inode,
972 struct dentry *old_dentry,
973 struct inode *new_inode,
974 struct dentry *new_dentry)
977 struct smack_known *isp;
978 struct smk_audit_info ad;
980 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
981 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
983 isp = smk_of_inode(d_backing_inode(old_dentry));
984 rc = smk_curacc(isp, MAY_READWRITE, &ad);
985 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
987 if (rc == 0 && d_is_positive(new_dentry)) {
988 isp = smk_of_inode(d_backing_inode(new_dentry));
989 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
990 rc = smk_curacc(isp, MAY_READWRITE, &ad);
991 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
997 * smack_inode_permission - Smack version of permission()
998 * @inode: the inode in question
999 * @mask: the access requested
1001 * This is the important Smack hook.
1003 * Returns 0 if access is permitted, -EACCES otherwise
1005 static int smack_inode_permission(struct inode *inode, int mask)
1007 struct smk_audit_info ad;
1008 int no_block = mask & MAY_NOT_BLOCK;
1011 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1013 * No permission to check. Existence test. Yup, it's there.
1018 /* May be droppable after audit */
1021 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1022 smk_ad_setfield_u_fs_inode(&ad, inode);
1023 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1024 rc = smk_bu_inode(inode, mask, rc);
1029 * smack_inode_setattr - Smack check for setting attributes
1030 * @dentry: the object
1031 * @iattr: for the force flag
1033 * Returns 0 if access is permitted, an error code otherwise
1035 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1037 struct smk_audit_info ad;
1041 * Need to allow for clearing the setuid bit.
1043 if (iattr->ia_valid & ATTR_FORCE)
1045 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1046 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1048 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1049 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1054 * smack_inode_getattr - Smack check for getting attributes
1055 * @mnt: vfsmount of the object
1056 * @dentry: the object
1058 * Returns 0 if access is permitted, an error code otherwise
1060 static int smack_inode_getattr(const struct path *path)
1062 struct smk_audit_info ad;
1063 struct inode *inode = d_backing_inode(path->dentry);
1066 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1067 smk_ad_setfield_u_fs_path(&ad, *path);
1068 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1069 rc = smk_bu_inode(inode, MAY_READ, rc);
1074 * smack_inode_setxattr - Smack check for setting xattrs
1075 * @dentry: the object
1076 * @name: name of the attribute
1077 * @value: value of the attribute
1078 * @size: size of the value
1081 * This protects the Smack attribute explicitly.
1083 * Returns 0 if access is permitted, an error code otherwise
1085 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1086 const void *value, size_t size, int flags)
1088 struct smk_audit_info ad;
1089 struct smack_known *skp;
1091 int check_import = 0;
1096 * Check label validity here so import won't fail in post_setxattr
1098 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1099 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1100 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1103 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1104 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1108 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1110 if (size != TRANS_TRUE_SIZE ||
1111 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1114 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1116 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1119 if (rc == 0 && check_import) {
1120 skp = size ? smk_import_entry(value, size) : NULL;
1121 if (skp == NULL || (check_star &&
1122 (skp == &smack_known_star || skp == &smack_known_web)))
1126 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1127 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1130 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1131 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1138 * smack_inode_post_setxattr - Apply the Smack update approved above
1140 * @name: attribute name
1141 * @value: attribute value
1142 * @size: attribute size
1145 * Set the pointer in the inode blob to the entry found
1146 * in the master label list.
1148 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1149 const void *value, size_t size, int flags)
1151 struct smack_known *skp;
1152 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
1154 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1155 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1159 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1160 skp = smk_import_entry(value, size);
1162 isp->smk_inode = skp;
1164 isp->smk_inode = &smack_known_invalid;
1165 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1166 skp = smk_import_entry(value, size);
1168 isp->smk_task = skp;
1170 isp->smk_task = &smack_known_invalid;
1171 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1172 skp = smk_import_entry(value, size);
1174 isp->smk_mmap = skp;
1176 isp->smk_mmap = &smack_known_invalid;
1183 * smack_inode_getxattr - Smack check on getxattr
1184 * @dentry: the object
1187 * Returns 0 if access is permitted, an error code otherwise
1189 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1191 struct smk_audit_info ad;
1194 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1195 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1197 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1198 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1203 * smack_inode_removexattr - Smack check on removexattr
1204 * @dentry: the object
1205 * @name: name of the attribute
1207 * Removing the Smack attribute requires CAP_MAC_ADMIN
1209 * Returns 0 if access is permitted, an error code otherwise
1211 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1213 struct inode_smack *isp;
1214 struct smk_audit_info ad;
1217 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1218 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1219 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1220 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1221 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1222 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1223 if (!smack_privileged(CAP_MAC_ADMIN))
1226 rc = cap_inode_removexattr(dentry, name);
1231 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1232 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1234 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1235 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1239 isp = d_backing_inode(dentry)->i_security;
1241 * Don't do anything special for these.
1242 * XATTR_NAME_SMACKIPIN
1243 * XATTR_NAME_SMACKIPOUT
1244 * XATTR_NAME_SMACKEXEC
1246 if (strcmp(name, XATTR_NAME_SMACK) == 0)
1247 isp->smk_task = NULL;
1248 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1249 isp->smk_mmap = NULL;
1250 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1251 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1257 * smack_inode_getsecurity - get smack xattrs
1258 * @inode: the object
1259 * @name: attribute name
1260 * @buffer: where to put the result
1263 * Returns the size of the attribute or an error code
1265 static int smack_inode_getsecurity(const struct inode *inode,
1266 const char *name, void **buffer,
1269 struct socket_smack *ssp;
1270 struct socket *sock;
1271 struct super_block *sbp;
1272 struct inode *ip = (struct inode *)inode;
1273 struct smack_known *isp;
1277 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1278 isp = smk_of_inode(inode);
1279 ilen = strlen(isp->smk_known);
1280 *buffer = isp->smk_known;
1285 * The rest of the Smack xattrs are only on sockets.
1288 if (sbp->s_magic != SOCKFS_MAGIC)
1291 sock = SOCKET_I(ip);
1292 if (sock == NULL || sock->sk == NULL)
1295 ssp = sock->sk->sk_security;
1297 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1299 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1304 ilen = strlen(isp->smk_known);
1306 *buffer = isp->smk_known;
1315 * smack_inode_listsecurity - list the Smack attributes
1316 * @inode: the object
1317 * @buffer: where they go
1318 * @buffer_size: size of buffer
1320 * Returns 0 on success, -EINVAL otherwise
1322 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1325 int len = sizeof(XATTR_NAME_SMACK);
1327 if (buffer != NULL && len <= buffer_size)
1328 memcpy(buffer, XATTR_NAME_SMACK, len);
1334 * smack_inode_getsecid - Extract inode's security id
1335 * @inode: inode to extract the info from
1336 * @secid: where result will be saved
1338 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1340 struct inode_smack *isp = inode->i_security;
1342 *secid = isp->smk_inode->smk_secid;
1350 * smack_file_permission - Smack check on file operations
1356 * Should access checks be done on each read or write?
1357 * UNICOS and SELinux say yes.
1358 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1360 * I'll say no for now. Smack does not do the frequent
1361 * label changing that SELinux does.
1363 static int smack_file_permission(struct file *file, int mask)
1369 * smack_file_alloc_security - assign a file security blob
1372 * The security blob for a file is a pointer to the master
1373 * label list, so no allocation is done.
1375 * f_security is the owner security information. It
1376 * isn't used on file access checks, it's for send_sigio.
1380 static int smack_file_alloc_security(struct file *file)
1382 struct smack_known *skp = smk_of_current();
1384 file->f_security = skp;
1389 * smack_file_free_security - clear a file security blob
1392 * The security blob for a file is a pointer to the master
1393 * label list, so no memory is freed.
1395 static void smack_file_free_security(struct file *file)
1397 file->f_security = NULL;
1401 * smack_file_ioctl - Smack check on ioctls
1406 * Relies heavily on the correct use of the ioctl command conventions.
1408 * Returns 0 if allowed, error code otherwise
1410 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1414 struct smk_audit_info ad;
1415 struct inode *inode = file_inode(file);
1417 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1418 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1420 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1421 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1422 rc = smk_bu_file(file, MAY_WRITE, rc);
1425 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1426 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1427 rc = smk_bu_file(file, MAY_READ, rc);
1434 * smack_file_lock - Smack check on file locking
1438 * Returns 0 if current has lock access, error code otherwise
1440 static int smack_file_lock(struct file *file, unsigned int cmd)
1442 struct smk_audit_info ad;
1444 struct inode *inode = file_inode(file);
1446 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1447 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1448 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1449 rc = smk_bu_file(file, MAY_LOCK, rc);
1454 * smack_file_fcntl - Smack check on fcntl
1456 * @cmd: what action to check
1459 * Generally these operations are harmless.
1460 * File locking operations present an obvious mechanism
1461 * for passing information, so they require write access.
1463 * Returns 0 if current has access, error code otherwise
1465 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1468 struct smk_audit_info ad;
1470 struct inode *inode = file_inode(file);
1477 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1478 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1479 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1480 rc = smk_bu_file(file, MAY_LOCK, rc);
1484 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1485 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1486 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1487 rc = smk_bu_file(file, MAY_WRITE, rc);
1498 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1499 * if mapping anonymous memory.
1500 * @file contains the file structure for file to map (may be NULL).
1501 * @reqprot contains the protection requested by the application.
1502 * @prot contains the protection that will be applied by the kernel.
1503 * @flags contains the operational flags.
1504 * Return 0 if permission is granted.
1506 static int smack_mmap_file(struct file *file,
1507 unsigned long reqprot, unsigned long prot,
1508 unsigned long flags)
1510 struct smack_known *skp;
1511 struct smack_known *mkp;
1512 struct smack_rule *srp;
1513 struct task_smack *tsp;
1514 struct smack_known *okp;
1515 struct inode_smack *isp;
1524 isp = file_inode(file)->i_security;
1525 if (isp->smk_mmap == NULL)
1527 mkp = isp->smk_mmap;
1529 tsp = current_security();
1530 skp = smk_of_current();
1535 * For each Smack rule associated with the subject
1536 * label verify that the SMACK64MMAP also has access
1537 * to that rule's object label.
1539 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1540 okp = srp->smk_object;
1542 * Matching labels always allows access.
1544 if (mkp->smk_known == okp->smk_known)
1547 * If there is a matching local rule take
1548 * that into account as well.
1550 may = smk_access_entry(srp->smk_subject->smk_known,
1554 may = srp->smk_access;
1556 may &= srp->smk_access;
1558 * If may is zero the SMACK64MMAP subject can't
1559 * possibly have less access.
1565 * Fetch the global list entry.
1566 * If there isn't one a SMACK64MMAP subject
1567 * can't have as much access as current.
1569 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1571 if (mmay == -ENOENT) {
1576 * If there is a local entry it modifies the
1577 * potential access, too.
1579 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1581 if (tmay != -ENOENT)
1585 * If there is any access available to current that is
1586 * not available to a SMACK64MMAP subject
1589 if ((may | mmay) != mmay) {
1601 * smack_file_set_fowner - set the file security blob value
1602 * @file: object in question
1605 static void smack_file_set_fowner(struct file *file)
1607 file->f_security = smk_of_current();
1611 * smack_file_send_sigiotask - Smack on sigio
1612 * @tsk: The target task
1613 * @fown: the object the signal come from
1616 * Allow a privileged task to get signals even if it shouldn't
1618 * Returns 0 if a subject with the object's smack could
1619 * write to the task, an error code otherwise.
1621 static int smack_file_send_sigiotask(struct task_struct *tsk,
1622 struct fown_struct *fown, int signum)
1624 struct smack_known *skp;
1625 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1628 struct smk_audit_info ad;
1631 * struct fown_struct is never outside the context of a struct file
1633 file = container_of(fown, struct file, f_owner);
1635 /* we don't log here as rc can be overriden */
1636 skp = file->f_security;
1637 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1638 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
1639 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1642 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1643 smk_ad_setfield_u_tsk(&ad, tsk);
1644 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
1649 * smack_file_receive - Smack file receive check
1652 * Returns 0 if current has access, error code otherwise
1654 static int smack_file_receive(struct file *file)
1658 struct smk_audit_info ad;
1659 struct inode *inode = file_inode(file);
1661 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1662 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1664 * This code relies on bitmasks.
1666 if (file->f_mode & FMODE_READ)
1668 if (file->f_mode & FMODE_WRITE)
1671 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1672 rc = smk_bu_file(file, may, rc);
1677 * smack_file_open - Smack dentry open processing
1679 * @cred: task credential
1681 * Set the security blob in the file structure.
1682 * Allow the open only if the task has read access. There are
1683 * many read operations (e.g. fstat) that you can do with an
1684 * fd even if you have the file open write-only.
1688 static int smack_file_open(struct file *file, const struct cred *cred)
1690 struct task_smack *tsp = cred->security;
1691 struct inode *inode = file_inode(file);
1692 struct smk_audit_info ad;
1695 if (smack_privileged(CAP_MAC_OVERRIDE))
1698 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1699 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1700 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
1701 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
1711 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1712 * @new: the new credentials
1713 * @gfp: the atomicity of any memory allocations
1715 * Prepare a blank set of credentials for modification. This must allocate all
1716 * the memory the LSM module might require such that cred_transfer() can
1717 * complete without error.
1719 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1721 struct task_smack *tsp;
1723 tsp = new_task_smack(NULL, NULL, gfp);
1727 cred->security = tsp;
1734 * smack_cred_free - "free" task-level security credentials
1735 * @cred: the credentials in question
1738 static void smack_cred_free(struct cred *cred)
1740 struct task_smack *tsp = cred->security;
1741 struct smack_rule *rp;
1742 struct list_head *l;
1743 struct list_head *n;
1747 cred->security = NULL;
1749 list_for_each_safe(l, n, &tsp->smk_rules) {
1750 rp = list_entry(l, struct smack_rule, list);
1751 list_del(&rp->list);
1758 * smack_cred_prepare - prepare new set of credentials for modification
1759 * @new: the new credentials
1760 * @old: the original credentials
1761 * @gfp: the atomicity of any memory allocations
1763 * Prepare a new set of credentials for modification.
1765 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1768 struct task_smack *old_tsp = old->security;
1769 struct task_smack *new_tsp;
1772 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1773 if (new_tsp == NULL)
1776 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1780 new->security = new_tsp;
1785 * smack_cred_transfer - Transfer the old credentials to the new credentials
1786 * @new: the new credentials
1787 * @old: the original credentials
1789 * Fill in a set of blank credentials from another set of credentials.
1791 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1793 struct task_smack *old_tsp = old->security;
1794 struct task_smack *new_tsp = new->security;
1796 new_tsp->smk_task = old_tsp->smk_task;
1797 new_tsp->smk_forked = old_tsp->smk_task;
1798 mutex_init(&new_tsp->smk_rules_lock);
1799 INIT_LIST_HEAD(&new_tsp->smk_rules);
1802 /* cbs copy rule list */
1806 * smack_kernel_act_as - Set the subjective context in a set of credentials
1807 * @new: points to the set of credentials to be modified.
1808 * @secid: specifies the security ID to be set
1810 * Set the security data for a kernel service.
1812 static int smack_kernel_act_as(struct cred *new, u32 secid)
1814 struct task_smack *new_tsp = new->security;
1815 struct smack_known *skp = smack_from_secid(secid);
1820 new_tsp->smk_task = skp;
1825 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1826 * @new: points to the set of credentials to be modified
1827 * @inode: points to the inode to use as a reference
1829 * Set the file creation context in a set of credentials to the same
1830 * as the objective context of the specified inode
1832 static int smack_kernel_create_files_as(struct cred *new,
1833 struct inode *inode)
1835 struct inode_smack *isp = inode->i_security;
1836 struct task_smack *tsp = new->security;
1838 tsp->smk_forked = isp->smk_inode;
1839 tsp->smk_task = tsp->smk_forked;
1844 * smk_curacc_on_task - helper to log task related access
1845 * @p: the task object
1846 * @access: the access requested
1847 * @caller: name of the calling function for audit
1849 * Return 0 if access is permitted
1851 static int smk_curacc_on_task(struct task_struct *p, int access,
1854 struct smk_audit_info ad;
1855 struct smack_known *skp = smk_of_task_struct(p);
1858 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1859 smk_ad_setfield_u_tsk(&ad, p);
1860 rc = smk_curacc(skp, access, &ad);
1861 rc = smk_bu_task(p, access, rc);
1866 * smack_task_setpgid - Smack check on setting pgid
1867 * @p: the task object
1870 * Return 0 if write access is permitted
1872 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1874 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1878 * smack_task_getpgid - Smack access check for getpgid
1879 * @p: the object task
1881 * Returns 0 if current can read the object task, error code otherwise
1883 static int smack_task_getpgid(struct task_struct *p)
1885 return smk_curacc_on_task(p, MAY_READ, __func__);
1889 * smack_task_getsid - Smack access check for getsid
1890 * @p: the object task
1892 * Returns 0 if current can read the object task, error code otherwise
1894 static int smack_task_getsid(struct task_struct *p)
1896 return smk_curacc_on_task(p, MAY_READ, __func__);
1900 * smack_task_getsecid - get the secid of the task
1901 * @p: the object task
1902 * @secid: where to put the result
1904 * Sets the secid to contain a u32 version of the smack label.
1906 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1908 struct smack_known *skp = smk_of_task_struct(p);
1910 *secid = skp->smk_secid;
1914 * smack_task_setnice - Smack check on setting nice
1915 * @p: the task object
1918 * Return 0 if write access is permitted
1920 static int smack_task_setnice(struct task_struct *p, int nice)
1922 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1926 * smack_task_setioprio - Smack check on setting ioprio
1927 * @p: the task object
1930 * Return 0 if write access is permitted
1932 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1934 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1938 * smack_task_getioprio - Smack check on reading ioprio
1939 * @p: the task object
1941 * Return 0 if read access is permitted
1943 static int smack_task_getioprio(struct task_struct *p)
1945 return smk_curacc_on_task(p, MAY_READ, __func__);
1949 * smack_task_setscheduler - Smack check on setting scheduler
1950 * @p: the task object
1954 * Return 0 if read access is permitted
1956 static int smack_task_setscheduler(struct task_struct *p)
1958 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1962 * smack_task_getscheduler - Smack check on reading scheduler
1963 * @p: the task object
1965 * Return 0 if read access is permitted
1967 static int smack_task_getscheduler(struct task_struct *p)
1969 return smk_curacc_on_task(p, MAY_READ, __func__);
1973 * smack_task_movememory - Smack check on moving memory
1974 * @p: the task object
1976 * Return 0 if write access is permitted
1978 static int smack_task_movememory(struct task_struct *p)
1980 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1984 * smack_task_kill - Smack check on signal delivery
1985 * @p: the task object
1988 * @secid: identifies the smack to use in lieu of current's
1990 * Return 0 if write access is permitted
1992 * The secid behavior is an artifact of an SELinux hack
1993 * in the USB code. Someday it may go away.
1995 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1998 struct smk_audit_info ad;
1999 struct smack_known *skp;
2000 struct smack_known *tkp = smk_of_task_struct(p);
2003 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2004 smk_ad_setfield_u_tsk(&ad, p);
2006 * Sending a signal requires that the sender
2007 * can write the receiver.
2010 rc = smk_curacc(tkp, MAY_WRITE, &ad);
2011 rc = smk_bu_task(p, MAY_WRITE, rc);
2015 * If the secid isn't 0 we're dealing with some USB IO
2016 * specific behavior. This is not clean. For one thing
2017 * we can't take privilege into account.
2019 skp = smack_from_secid(secid);
2020 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2021 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
2026 * smack_task_wait - Smack access check for waiting
2027 * @p: task to wait for
2031 static int smack_task_wait(struct task_struct *p)
2034 * Allow the operation to succeed.
2036 * In userless environments (e.g. phones) programs
2037 * get marked with SMACK64EXEC and even if the parent
2038 * and child shouldn't be talking the parent still
2039 * may expect to know when the child exits.
2045 * smack_task_to_inode - copy task smack into the inode blob
2046 * @p: task to copy from
2047 * @inode: inode to copy to
2049 * Sets the smack pointer in the inode security blob
2051 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2053 struct inode_smack *isp = inode->i_security;
2054 struct smack_known *skp = smk_of_task_struct(p);
2056 isp->smk_inode = skp;
2064 * smack_sk_alloc_security - Allocate a socket blob
2067 * @gfp_flags: memory allocation flags
2069 * Assign Smack pointers to current
2071 * Returns 0 on success, -ENOMEM is there's no memory
2073 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2075 struct smack_known *skp = smk_of_current();
2076 struct socket_smack *ssp;
2078 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2084 ssp->smk_packet = NULL;
2086 sk->sk_security = ssp;
2092 * smack_sk_free_security - Free a socket blob
2095 * Clears the blob pointer
2097 static void smack_sk_free_security(struct sock *sk)
2099 kfree(sk->sk_security);
2103 * smack_host_label - check host based restrictions
2104 * @sip: the object end
2106 * looks for host based access restrictions
2108 * This version will only be appropriate for really small sets of single label
2109 * hosts. The caller is responsible for ensuring that the RCU read lock is
2110 * taken before calling this function.
2112 * Returns the label of the far end or NULL if it's not special.
2114 static struct smack_known *smack_host_label(struct sockaddr_in *sip)
2116 struct smk_netlbladdr *snp;
2117 struct in_addr *siap = &sip->sin_addr;
2119 if (siap->s_addr == 0)
2122 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2124 * we break after finding the first match because
2125 * the list is sorted from longest to shortest mask
2126 * so we have found the most specific match
2128 if ((&snp->smk_host.sin_addr)->s_addr ==
2129 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2130 /* we have found the special CIPSO option */
2131 if (snp->smk_label == &smack_cipso_option)
2133 return snp->smk_label;
2140 * smack_netlabel - Set the secattr on a socket
2142 * @labeled: socket label scheme
2144 * Convert the outbound smack value (smk_out) to a
2145 * secattr and attach it to the socket.
2147 * Returns 0 on success or an error code
2149 static int smack_netlabel(struct sock *sk, int labeled)
2151 struct smack_known *skp;
2152 struct socket_smack *ssp = sk->sk_security;
2156 * Usually the netlabel code will handle changing the
2157 * packet labeling based on the label.
2158 * The case of a single label host is different, because
2159 * a single label host should never get a labeled packet
2160 * even though the label is usually associated with a packet
2164 bh_lock_sock_nested(sk);
2166 if (ssp->smk_out == smack_net_ambient ||
2167 labeled == SMACK_UNLABELED_SOCKET)
2168 netlbl_sock_delattr(sk);
2171 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2181 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2183 * @sap: the destination address
2185 * Set the correct secattr for the given socket based on the destination
2186 * address and perform any outbound access checks needed.
2188 * Returns 0 on success or an error code.
2191 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2193 struct smack_known *skp;
2196 struct smack_known *hkp;
2197 struct socket_smack *ssp = sk->sk_security;
2198 struct smk_audit_info ad;
2201 hkp = smack_host_label(sap);
2204 struct lsm_network_audit net;
2206 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2207 ad.a.u.net->family = sap->sin_family;
2208 ad.a.u.net->dport = sap->sin_port;
2209 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2211 sk_lbl = SMACK_UNLABELED_SOCKET;
2213 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2214 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2216 sk_lbl = SMACK_CIPSO_SOCKET;
2223 return smack_netlabel(sk, sk_lbl);
2226 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2228 * smk_ipv6_port_label - Smack port access table management
2232 * Create or update the port list entry
2234 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2236 struct sock *sk = sock->sk;
2237 struct sockaddr_in6 *addr6;
2238 struct socket_smack *ssp = sock->sk->sk_security;
2239 struct smk_port_label *spp;
2240 unsigned short port = 0;
2242 if (address == NULL) {
2244 * This operation is changing the Smack information
2245 * on the bound socket. Take the changes to the port
2248 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2249 if (sk != spp->smk_sock)
2251 spp->smk_in = ssp->smk_in;
2252 spp->smk_out = ssp->smk_out;
2256 * A NULL address is only used for updating existing
2257 * bound entries. If there isn't one, it's OK.
2262 addr6 = (struct sockaddr_in6 *)address;
2263 port = ntohs(addr6->sin6_port);
2265 * This is a special case that is safely ignored.
2271 * Look for an existing port list entry.
2272 * This is an indication that a port is getting reused.
2274 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2275 if (spp->smk_port != port)
2277 spp->smk_port = port;
2279 spp->smk_in = ssp->smk_in;
2280 spp->smk_out = ssp->smk_out;
2285 * A new port entry is required.
2287 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2291 spp->smk_port = port;
2293 spp->smk_in = ssp->smk_in;
2294 spp->smk_out = ssp->smk_out;
2296 list_add(&spp->list, &smk_ipv6_port_list);
2301 * smk_ipv6_port_check - check Smack port access
2305 * Create or update the port list entry
2307 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2312 struct smk_port_label *spp;
2313 struct socket_smack *ssp = sk->sk_security;
2314 struct smack_known *skp;
2315 unsigned short port = 0;
2316 struct smack_known *object;
2317 struct smk_audit_info ad;
2320 struct lsm_network_audit net;
2323 if (act == SMK_RECEIVING) {
2324 skp = smack_net_ambient;
2325 object = ssp->smk_in;
2328 object = smack_net_ambient;
2332 * Get the IP address and port from the address.
2334 port = ntohs(address->sin6_port);
2335 bep = (__be16 *)(&address->sin6_addr);
2336 be32p = (__be32 *)(&address->sin6_addr);
2339 * It's remote, so port lookup does no good.
2341 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2345 * It's local so the send check has to have passed.
2347 if (act == SMK_RECEIVING) {
2348 skp = &smack_known_web;
2352 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2353 if (spp->smk_port != port)
2355 object = spp->smk_in;
2356 if (act == SMK_CONNECTING)
2357 ssp->smk_packet = spp->smk_out;
2364 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2365 ad.a.u.net->family = sk->sk_family;
2366 ad.a.u.net->dport = port;
2367 if (act == SMK_RECEIVING)
2368 ad.a.u.net->v6info.saddr = address->sin6_addr;
2370 ad.a.u.net->v6info.daddr = address->sin6_addr;
2372 rc = smk_access(skp, object, MAY_WRITE, &ad);
2373 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2376 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2379 * smack_inode_setsecurity - set smack xattrs
2380 * @inode: the object
2381 * @name: attribute name
2382 * @value: attribute value
2383 * @size: size of the attribute
2386 * Sets the named attribute in the appropriate blob
2388 * Returns 0 on success, or an error code
2390 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2391 const void *value, size_t size, int flags)
2393 struct smack_known *skp;
2394 struct inode_smack *nsp = inode->i_security;
2395 struct socket_smack *ssp;
2396 struct socket *sock;
2399 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2402 skp = smk_import_entry(value, size);
2406 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2407 nsp->smk_inode = skp;
2408 nsp->smk_flags |= SMK_INODE_INSTANT;
2412 * The rest of the Smack xattrs are only on sockets.
2414 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2417 sock = SOCKET_I(inode);
2418 if (sock == NULL || sock->sk == NULL)
2421 ssp = sock->sk->sk_security;
2423 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2425 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2427 if (sock->sk->sk_family == PF_INET) {
2428 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2431 "Smack: \"%s\" netlbl error %d.\n",
2437 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2438 if (sock->sk->sk_family == PF_INET6)
2439 smk_ipv6_port_label(sock, NULL);
2440 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2446 * smack_socket_post_create - finish socket setup
2448 * @family: protocol family
2453 * Sets the netlabel information on the socket
2455 * Returns 0 on success, and error code otherwise
2457 static int smack_socket_post_create(struct socket *sock, int family,
2458 int type, int protocol, int kern)
2460 struct socket_smack *ssp;
2462 if (sock->sk == NULL)
2466 * Sockets created by kernel threads receive web label.
2468 if (unlikely(current->flags & PF_KTHREAD)) {
2469 ssp = sock->sk->sk_security;
2470 ssp->smk_in = &smack_known_web;
2471 ssp->smk_out = &smack_known_web;
2474 if (family != PF_INET)
2477 * Set the outbound netlbl.
2479 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2482 #ifndef CONFIG_SECURITY_SMACK_NETFILTER
2484 * smack_socket_bind - record port binding information.
2486 * @address: the port address
2487 * @addrlen: size of the address
2489 * Records the label bound to a port.
2493 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2496 #if IS_ENABLED(CONFIG_IPV6)
2497 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2498 smk_ipv6_port_label(sock, address);
2503 #endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
2506 * smack_socket_connect - connect access check
2508 * @sap: the other end
2509 * @addrlen: size of sap
2511 * Verifies that a connection may be possible
2513 * Returns 0 on success, and error code otherwise
2515 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2520 if (sock->sk == NULL)
2523 switch (sock->sk->sk_family) {
2525 if (addrlen < sizeof(struct sockaddr_in))
2527 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2530 if (addrlen < sizeof(struct sockaddr_in6))
2532 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2533 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2535 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2542 * smack_flags_to_may - convert S_ to MAY_ values
2543 * @flags: the S_ value
2545 * Returns the equivalent MAY_ value
2547 static int smack_flags_to_may(int flags)
2551 if (flags & S_IRUGO)
2553 if (flags & S_IWUGO)
2555 if (flags & S_IXUGO)
2562 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2567 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2569 struct smack_known *skp = smk_of_current();
2571 msg->security = skp;
2576 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2579 * Clears the blob pointer
2581 static void smack_msg_msg_free_security(struct msg_msg *msg)
2583 msg->security = NULL;
2587 * smack_of_shm - the smack pointer for the shm
2590 * Returns a pointer to the smack value
2592 static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
2594 return (struct smack_known *)shp->shm_perm.security;
2598 * smack_shm_alloc_security - Set the security blob for shm
2603 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2605 struct kern_ipc_perm *isp = &shp->shm_perm;
2606 struct smack_known *skp = smk_of_current();
2608 isp->security = skp;
2613 * smack_shm_free_security - Clear the security blob for shm
2616 * Clears the blob pointer
2618 static void smack_shm_free_security(struct shmid_kernel *shp)
2620 struct kern_ipc_perm *isp = &shp->shm_perm;
2622 isp->security = NULL;
2626 * smk_curacc_shm : check if current has access on shm
2628 * @access : access requested
2630 * Returns 0 if current has the requested access, error code otherwise
2632 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2634 struct smack_known *ssp = smack_of_shm(shp);
2635 struct smk_audit_info ad;
2639 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2640 ad.a.u.ipc_id = shp->shm_perm.id;
2642 rc = smk_curacc(ssp, access, &ad);
2643 rc = smk_bu_current("shm", ssp, access, rc);
2648 * smack_shm_associate - Smack access check for shm
2650 * @shmflg: access requested
2652 * Returns 0 if current has the requested access, error code otherwise
2654 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2658 may = smack_flags_to_may(shmflg);
2659 return smk_curacc_shm(shp, may);
2663 * smack_shm_shmctl - Smack access check for shm
2665 * @cmd: what it wants to do
2667 * Returns 0 if current has the requested access, error code otherwise
2669 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2682 may = MAY_READWRITE;
2687 * System level information.
2693 return smk_curacc_shm(shp, may);
2697 * smack_shm_shmat - Smack access for shmat
2700 * @shmflg: access requested
2702 * Returns 0 if current has the requested access, error code otherwise
2704 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2709 may = smack_flags_to_may(shmflg);
2710 return smk_curacc_shm(shp, may);
2714 * smack_of_sem - the smack pointer for the sem
2717 * Returns a pointer to the smack value
2719 static struct smack_known *smack_of_sem(struct sem_array *sma)
2721 return (struct smack_known *)sma->sem_perm.security;
2725 * smack_sem_alloc_security - Set the security blob for sem
2730 static int smack_sem_alloc_security(struct sem_array *sma)
2732 struct kern_ipc_perm *isp = &sma->sem_perm;
2733 struct smack_known *skp = smk_of_current();
2735 isp->security = skp;
2740 * smack_sem_free_security - Clear the security blob for sem
2743 * Clears the blob pointer
2745 static void smack_sem_free_security(struct sem_array *sma)
2747 struct kern_ipc_perm *isp = &sma->sem_perm;
2749 isp->security = NULL;
2753 * smk_curacc_sem : check if current has access on sem
2755 * @access : access requested
2757 * Returns 0 if current has the requested access, error code otherwise
2759 static int smk_curacc_sem(struct sem_array *sma, int access)
2761 struct smack_known *ssp = smack_of_sem(sma);
2762 struct smk_audit_info ad;
2766 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2767 ad.a.u.ipc_id = sma->sem_perm.id;
2769 rc = smk_curacc(ssp, access, &ad);
2770 rc = smk_bu_current("sem", ssp, access, rc);
2775 * smack_sem_associate - Smack access check for sem
2777 * @semflg: access requested
2779 * Returns 0 if current has the requested access, error code otherwise
2781 static int smack_sem_associate(struct sem_array *sma, int semflg)
2785 may = smack_flags_to_may(semflg);
2786 return smk_curacc_sem(sma, may);
2790 * smack_sem_shmctl - Smack access check for sem
2792 * @cmd: what it wants to do
2794 * Returns 0 if current has the requested access, error code otherwise
2796 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2814 may = MAY_READWRITE;
2819 * System level information
2826 return smk_curacc_sem(sma, may);
2830 * smack_sem_semop - Smack checks of semaphore operations
2836 * Treated as read and write in all cases.
2838 * Returns 0 if access is allowed, error code otherwise
2840 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2841 unsigned nsops, int alter)
2843 return smk_curacc_sem(sma, MAY_READWRITE);
2847 * smack_msg_alloc_security - Set the security blob for msg
2852 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2854 struct kern_ipc_perm *kisp = &msq->q_perm;
2855 struct smack_known *skp = smk_of_current();
2857 kisp->security = skp;
2862 * smack_msg_free_security - Clear the security blob for msg
2865 * Clears the blob pointer
2867 static void smack_msg_queue_free_security(struct msg_queue *msq)
2869 struct kern_ipc_perm *kisp = &msq->q_perm;
2871 kisp->security = NULL;
2875 * smack_of_msq - the smack pointer for the msq
2878 * Returns a pointer to the smack label entry
2880 static struct smack_known *smack_of_msq(struct msg_queue *msq)
2882 return (struct smack_known *)msq->q_perm.security;
2886 * smk_curacc_msq : helper to check if current has access on msq
2888 * @access : access requested
2890 * return 0 if current has access, error otherwise
2892 static int smk_curacc_msq(struct msg_queue *msq, int access)
2894 struct smack_known *msp = smack_of_msq(msq);
2895 struct smk_audit_info ad;
2899 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2900 ad.a.u.ipc_id = msq->q_perm.id;
2902 rc = smk_curacc(msp, access, &ad);
2903 rc = smk_bu_current("msq", msp, access, rc);
2908 * smack_msg_queue_associate - Smack access check for msg_queue
2910 * @msqflg: access requested
2912 * Returns 0 if current has the requested access, error code otherwise
2914 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2918 may = smack_flags_to_may(msqflg);
2919 return smk_curacc_msq(msq, may);
2923 * smack_msg_queue_msgctl - Smack access check for msg_queue
2925 * @cmd: what it wants to do
2927 * Returns 0 if current has the requested access, error code otherwise
2929 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2940 may = MAY_READWRITE;
2945 * System level information
2952 return smk_curacc_msq(msq, may);
2956 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2959 * @msqflg: access requested
2961 * Returns 0 if current has the requested access, error code otherwise
2963 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2968 may = smack_flags_to_may(msqflg);
2969 return smk_curacc_msq(msq, may);
2973 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2980 * Returns 0 if current has read and write access, error code otherwise
2982 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2983 struct task_struct *target, long type, int mode)
2985 return smk_curacc_msq(msq, MAY_READWRITE);
2989 * smack_ipc_permission - Smack access for ipc_permission()
2990 * @ipp: the object permissions
2991 * @flag: access requested
2993 * Returns 0 if current has read and write access, error code otherwise
2995 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2997 struct smack_known *iskp = ipp->security;
2998 int may = smack_flags_to_may(flag);
2999 struct smk_audit_info ad;
3003 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3004 ad.a.u.ipc_id = ipp->id;
3006 rc = smk_curacc(iskp, may, &ad);
3007 rc = smk_bu_current("svipc", iskp, may, rc);
3012 * smack_ipc_getsecid - Extract smack security id
3013 * @ipp: the object permissions
3014 * @secid: where result will be saved
3016 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3018 struct smack_known *iskp = ipp->security;
3020 *secid = iskp->smk_secid;
3024 * smack_d_instantiate - Make sure the blob is correct on an inode
3025 * @opt_dentry: dentry where inode will be attached
3026 * @inode: the object
3028 * Set the inode's security blob if it hasn't been done already.
3030 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3032 struct super_block *sbp;
3033 struct superblock_smack *sbsp;
3034 struct inode_smack *isp;
3035 struct smack_known *skp;
3036 struct smack_known *ckp = smk_of_current();
3037 struct smack_known *final;
3038 char trattr[TRANS_TRUE_SIZE];
3046 isp = inode->i_security;
3048 mutex_lock(&isp->smk_lock);
3050 * If the inode is already instantiated
3051 * take the quick way out
3053 if (isp->smk_flags & SMK_INODE_INSTANT)
3057 sbsp = sbp->s_security;
3059 * We're going to use the superblock default label
3060 * if there's no label on the file.
3062 final = sbsp->smk_default;
3065 * If this is the root inode the superblock
3066 * may be in the process of initialization.
3067 * If that is the case use the root value out
3068 * of the superblock.
3070 if (opt_dentry->d_parent == opt_dentry) {
3071 switch (sbp->s_magic) {
3072 case CGROUP_SUPER_MAGIC:
3074 * The cgroup filesystem is never mounted,
3075 * so there's no opportunity to set the mount
3078 sbsp->smk_root = &smack_known_star;
3079 sbsp->smk_default = &smack_known_star;
3080 isp->smk_inode = sbsp->smk_root;
3084 * What about shmem/tmpfs anonymous files with dentry
3085 * obtained from d_alloc_pseudo()?
3087 isp->smk_inode = smk_of_current();
3090 isp->smk_inode = sbsp->smk_root;
3093 isp->smk_flags |= SMK_INODE_INSTANT;
3098 * This is pretty hackish.
3099 * Casey says that we shouldn't have to do
3100 * file system specific code, but it does help
3101 * with keeping it simple.
3103 switch (sbp->s_magic) {
3107 case CGROUP_SUPER_MAGIC:
3109 * Casey says that it's a little embarrassing
3110 * that the smack file system doesn't do
3111 * extended attributes.
3113 * Casey says pipes are easy (?)
3115 * Socket access is controlled by the socket
3116 * structures associated with the task involved.
3118 * Cgroupfs is special
3120 final = &smack_known_star;
3122 case DEVPTS_SUPER_MAGIC:
3124 * devpts seems content with the label of the task.
3125 * Programs that change smack have to treat the
3130 case PROC_SUPER_MAGIC:
3132 * Casey says procfs appears not to care.
3133 * The superblock default suffices.
3138 * Device labels should come from the filesystem,
3139 * but watch out, because they're volitile,
3140 * getting recreated on every reboot.
3142 final = &smack_known_star;
3146 * If a smack value has been set we want to use it,
3147 * but since tmpfs isn't giving us the opportunity
3148 * to set mount options simulate setting the
3149 * superblock default.
3153 * This isn't an understood special case.
3154 * Get the value from the xattr.
3158 * UNIX domain sockets use lower level socket data.
3160 if (S_ISSOCK(inode->i_mode)) {
3161 final = &smack_known_star;
3165 * No xattr support means, alas, no SMACK label.
3166 * Use the aforeapplied default.
3167 * It would be curious if the label of the task
3168 * does not match that assigned.
3170 if (inode->i_op->getxattr == NULL)
3173 * Get the dentry for xattr.
3175 dp = dget(opt_dentry);
3176 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3181 * Transmuting directory
3183 if (S_ISDIR(inode->i_mode)) {
3185 * If this is a new directory and the label was
3186 * transmuted when the inode was initialized
3187 * set the transmute attribute on the directory
3188 * and mark the inode.
3190 * If there is a transmute attribute on the
3191 * directory mark the inode.
3193 if (isp->smk_flags & SMK_INODE_CHANGED) {
3194 isp->smk_flags &= ~SMK_INODE_CHANGED;
3195 rc = inode->i_op->setxattr(dp,
3196 XATTR_NAME_SMACKTRANSMUTE,
3197 TRANS_TRUE, TRANS_TRUE_SIZE,
3200 rc = inode->i_op->getxattr(dp,
3201 XATTR_NAME_SMACKTRANSMUTE, trattr,
3203 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3204 TRANS_TRUE_SIZE) != 0)
3208 transflag = SMK_INODE_TRANSMUTE;
3211 * Don't let the exec or mmap label be "*" or "@".
3213 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3214 if (skp == &smack_known_star || skp == &smack_known_web)
3216 isp->smk_task = skp;
3217 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3218 if (skp == &smack_known_star || skp == &smack_known_web)
3220 isp->smk_mmap = skp;
3227 isp->smk_inode = ckp;
3229 isp->smk_inode = final;
3231 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3234 mutex_unlock(&isp->smk_lock);
3239 * smack_getprocattr - Smack process attribute access
3240 * @p: the object task
3241 * @name: the name of the attribute in /proc/.../attr
3242 * @value: where to put the result
3244 * Places a copy of the task Smack into value
3246 * Returns the length of the smack label or an error code
3248 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3250 struct smack_known *skp = smk_of_task_struct(p);
3254 if (strcmp(name, "current") != 0)
3257 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3267 * smack_setprocattr - Smack process attribute setting
3268 * @p: the object task
3269 * @name: the name of the attribute in /proc/.../attr
3270 * @value: the value to set
3271 * @size: the size of the value
3273 * Sets the Smack value of the task. Only setting self
3274 * is permitted and only with privilege
3276 * Returns the length of the smack label or an error code
3278 static int smack_setprocattr(struct task_struct *p, char *name,
3279 void *value, size_t size)
3281 struct task_smack *tsp;
3283 struct smack_known *skp;
3286 * Changing another process' Smack value is too dangerous
3287 * and supports no sane use case.
3292 if (!smack_privileged(CAP_MAC_ADMIN))
3295 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3298 if (strcmp(name, "current") != 0)
3301 skp = smk_import_entry(value, size);
3306 * No process is ever allowed the web ("@") label.
3308 if (skp == &smack_known_web)
3311 new = prepare_creds();
3315 tsp = new->security;
3316 tsp->smk_task = skp;
3323 * smack_unix_stream_connect - Smack access on UDS
3325 * @other: the other sock
3328 * Return 0 if a subject with the smack of sock could access
3329 * an object with the smack of other, otherwise an error code
3331 static int smack_unix_stream_connect(struct sock *sock,
3332 struct sock *other, struct sock *newsk)
3334 struct smack_known *skp;
3335 struct smack_known *okp;
3336 struct socket_smack *ssp = sock->sk_security;
3337 struct socket_smack *osp = other->sk_security;
3338 struct socket_smack *nsp = newsk->sk_security;
3339 struct smk_audit_info ad;
3342 struct lsm_network_audit net;
3345 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3349 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3350 smk_ad_setfield_u_net_sk(&ad, other);
3352 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3353 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3357 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3358 rc = smk_bu_note("UDS connect", okp, skp,
3364 * Cross reference the peer labels for SO_PEERSEC.
3367 nsp->smk_packet = ssp->smk_out;
3368 ssp->smk_packet = osp->smk_out;
3375 * smack_unix_may_send - Smack access on UDS
3377 * @other: the other socket
3379 * Return 0 if a subject with the smack of sock could access
3380 * an object with the smack of other, otherwise an error code
3382 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3384 struct socket_smack *ssp = sock->sk->sk_security;
3385 struct socket_smack *osp = other->sk->sk_security;
3386 struct smk_audit_info ad;
3390 struct lsm_network_audit net;
3392 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3393 smk_ad_setfield_u_net_sk(&ad, other->sk);
3396 if (smack_privileged(CAP_MAC_OVERRIDE))
3399 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3400 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3405 * smack_socket_sendmsg - Smack check based on destination host
3408 * @size: the size of the message
3410 * Return 0 if the current subject can write to the destination host.
3411 * For IPv4 this is only a question if the destination is a single label host.
3412 * For IPv6 this is a check against the label of the port.
3414 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3417 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3418 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3419 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3420 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
3424 * Perfectly reasonable for this to be NULL
3429 switch (sip->sin_family) {
3431 rc = smack_netlabel_send(sock->sk, sip);
3434 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3435 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3436 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
3443 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3444 * @sap: netlabel secattr
3445 * @ssp: socket security information
3447 * Returns a pointer to a Smack label entry found on the label list.
3449 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3450 struct socket_smack *ssp)
3452 struct smack_known *skp;
3457 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3459 * Looks like a CIPSO packet.
3460 * If there are flags but no level netlabel isn't
3461 * behaving the way we expect it to.
3463 * Look it up in the label table
3464 * Without guidance regarding the smack value
3465 * for the packet fall back on the network
3469 list_for_each_entry(skp, &smack_known_list, list) {
3470 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3473 * Compare the catsets. Use the netlbl APIs.
3475 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3476 if ((skp->smk_netlabel.flags &
3477 NETLBL_SECATTR_MLS_CAT) == 0)
3481 for (acat = -1, kcat = -1; acat == kcat; ) {
3482 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3484 kcat = netlbl_catmap_walk(
3485 skp->smk_netlabel.attr.mls.cat,
3487 if (acat < 0 || kcat < 0)
3500 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3501 return &smack_known_web;
3502 return &smack_known_star;
3504 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3506 * Looks like a fallback, which gives us a secid.
3508 skp = smack_from_secid(sap->attr.secid);
3510 * This has got to be a bug because it is
3511 * impossible to specify a fallback without
3512 * specifying the label, which will ensure
3513 * it has a secid, and the only way to get a
3514 * secid is from a fallback.
3516 BUG_ON(skp == NULL);
3520 * Without guidance regarding the smack value
3521 * for the packet fall back on the network
3524 return smack_net_ambient;
3527 #if IS_ENABLED(CONFIG_IPV6)
3528 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3532 int proto = -EINVAL;
3533 struct ipv6hdr _ipv6h;
3534 struct ipv6hdr *ip6;
3536 struct tcphdr _tcph, *th;
3537 struct udphdr _udph, *uh;
3538 struct dccp_hdr _dccph, *dh;
3542 offset = skb_network_offset(skb);
3543 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3546 sip->sin6_addr = ip6->saddr;
3548 nexthdr = ip6->nexthdr;
3549 offset += sizeof(_ipv6h);
3550 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3557 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3559 sip->sin6_port = th->source;
3562 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3564 sip->sin6_port = uh->source;
3567 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3569 sip->sin6_port = dh->dccph_sport;
3574 #endif /* CONFIG_IPV6 */
3577 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3581 * Returns 0 if the packet should be delivered, an error code otherwise
3583 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3585 struct netlbl_lsm_secattr secattr;
3586 struct socket_smack *ssp = sk->sk_security;
3587 struct smack_known *skp = NULL;
3589 struct smk_audit_info ad;
3591 struct lsm_network_audit net;
3593 #if IS_ENABLED(CONFIG_IPV6)
3594 struct sockaddr_in6 sadd;
3596 #endif /* CONFIG_IPV6 */
3598 switch (sk->sk_family) {
3600 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3602 * If there is a secmark use it rather than the CIPSO label.
3603 * If there is no secmark fall back to CIPSO.
3604 * The secmark is assumed to reflect policy better.
3606 if (skb && skb->secmark != 0) {
3607 skp = smack_from_secid(skb->secmark);
3610 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3612 * Translate what netlabel gave us.
3614 netlbl_secattr_init(&secattr);
3616 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3618 skp = smack_from_secattr(&secattr, ssp);
3620 skp = smack_net_ambient;
3622 netlbl_secattr_destroy(&secattr);
3624 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3628 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3629 ad.a.u.net->family = sk->sk_family;
3630 ad.a.u.net->netif = skb->skb_iif;
3631 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3634 * Receiving a packet requires that the other end
3635 * be able to write here. Read access is not required.
3636 * This is the simplist possible security model
3639 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3640 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3643 netlbl_skbuff_err(skb, rc, 0);
3645 #if IS_ENABLED(CONFIG_IPV6)
3647 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3648 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3650 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3651 if (skb && skb->secmark != 0)
3652 skp = smack_from_secid(skb->secmark);
3654 skp = smack_net_ambient;
3656 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3657 ad.a.u.net->family = sk->sk_family;
3658 ad.a.u.net->netif = skb->skb_iif;
3659 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3660 #endif /* CONFIG_AUDIT */
3661 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3662 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3664 #else /* CONFIG_SECURITY_SMACK_NETFILTER */
3665 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3666 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3668 #endif /* CONFIG_IPV6 */
3675 * smack_socket_getpeersec_stream - pull in packet label
3677 * @optval: user's destination
3678 * @optlen: size thereof
3681 * returns zero on success, an error code otherwise
3683 static int smack_socket_getpeersec_stream(struct socket *sock,
3684 char __user *optval,
3685 int __user *optlen, unsigned len)
3687 struct socket_smack *ssp;
3692 ssp = sock->sk->sk_security;
3693 if (ssp->smk_packet != NULL) {
3694 rcp = ssp->smk_packet->smk_known;
3695 slen = strlen(rcp) + 1;
3700 else if (copy_to_user(optval, rcp, slen) != 0)
3703 if (put_user(slen, optlen) != 0)
3711 * smack_socket_getpeersec_dgram - pull in packet label
3712 * @sock: the peer socket
3714 * @secid: pointer to where to put the secid of the packet
3716 * Sets the netlabel socket state on sk from parent
3718 static int smack_socket_getpeersec_dgram(struct socket *sock,
3719 struct sk_buff *skb, u32 *secid)
3722 struct netlbl_lsm_secattr secattr;
3723 struct socket_smack *ssp = NULL;
3724 struct smack_known *skp;
3725 int family = PF_UNSPEC;
3726 u32 s = 0; /* 0 is the invalid secid */
3730 if (skb->protocol == htons(ETH_P_IP))
3732 #if IS_ENABLED(CONFIG_IPV6)
3733 else if (skb->protocol == htons(ETH_P_IPV6))
3735 #endif /* CONFIG_IPV6 */
3737 if (family == PF_UNSPEC && sock != NULL)
3738 family = sock->sk->sk_family;
3742 ssp = sock->sk->sk_security;
3743 s = ssp->smk_out->smk_secid;
3746 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3752 * Translate what netlabel gave us.
3754 if (sock != NULL && sock->sk != NULL)
3755 ssp = sock->sk->sk_security;
3756 netlbl_secattr_init(&secattr);
3757 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3759 skp = smack_from_secattr(&secattr, ssp);
3762 netlbl_secattr_destroy(&secattr);
3764 #if IS_ENABLED(CONFIG_IPV6)
3766 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3768 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3770 #endif /* CONFIG_IPV6 */
3779 * smack_sock_graft - Initialize a newly created socket with an existing sock
3781 * @parent: parent socket
3783 * Set the smk_{in,out} state of an existing sock based on the process that
3784 * is creating the new socket.
3786 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3788 struct socket_smack *ssp;
3789 struct smack_known *skp = smk_of_current();
3792 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3795 ssp = sk->sk_security;
3798 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3802 * smack_inet_conn_request - Smack access check on connect
3803 * @sk: socket involved
3807 * Returns 0 if a task with the packet label could write to
3808 * the socket, otherwise an error code
3810 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3811 struct request_sock *req)
3813 u16 family = sk->sk_family;
3814 struct smack_known *skp;
3815 struct socket_smack *ssp = sk->sk_security;
3816 struct netlbl_lsm_secattr secattr;
3817 struct sockaddr_in addr;
3819 struct smack_known *hskp;
3821 struct smk_audit_info ad;
3823 struct lsm_network_audit net;
3826 #if IS_ENABLED(CONFIG_IPV6)
3827 if (family == PF_INET6) {
3829 * Handle mapped IPv4 packets arriving
3830 * via IPv6 sockets. Don't set up netlabel
3831 * processing on IPv6.
3833 if (skb->protocol == htons(ETH_P_IP))
3838 #endif /* CONFIG_IPV6 */
3840 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3842 * If there is a secmark use it rather than the CIPSO label.
3843 * If there is no secmark fall back to CIPSO.
3844 * The secmark is assumed to reflect policy better.
3846 if (skb && skb->secmark != 0) {
3847 skp = smack_from_secid(skb->secmark);
3850 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3852 netlbl_secattr_init(&secattr);
3853 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3855 skp = smack_from_secattr(&secattr, ssp);
3857 skp = &smack_known_huh;
3858 netlbl_secattr_destroy(&secattr);
3860 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3865 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3866 ad.a.u.net->family = family;
3867 ad.a.u.net->netif = skb->skb_iif;
3868 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3871 * Receiving a packet requires that the other end be able to write
3872 * here. Read access is not required.
3874 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3875 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
3880 * Save the peer's label in the request_sock so we can later setup
3881 * smk_packet in the child socket so that SO_PEERCRED can report it.
3883 req->peer_secid = skp->smk_secid;
3886 * We need to decide if we want to label the incoming connection here
3887 * if we do we only need to label the request_sock and the stack will
3888 * propagate the wire-label to the sock when it is created.
3891 addr.sin_addr.s_addr = hdr->saddr;
3893 hskp = smack_host_label(&addr);
3897 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3899 netlbl_req_delattr(req);
3905 * smack_inet_csk_clone - Copy the connection information to the new socket
3906 * @sk: the new socket
3907 * @req: the connection's request_sock
3909 * Transfer the connection's peer label to the newly created socket.
3911 static void smack_inet_csk_clone(struct sock *sk,
3912 const struct request_sock *req)
3914 struct socket_smack *ssp = sk->sk_security;
3915 struct smack_known *skp;
3917 if (req->peer_secid != 0) {
3918 skp = smack_from_secid(req->peer_secid);
3919 ssp->smk_packet = skp;
3921 ssp->smk_packet = NULL;
3925 * Key management security hooks
3927 * Casey has not tested key support very heavily.
3928 * The permission check is most likely too restrictive.
3929 * If you care about keys please have a look.
3934 * smack_key_alloc - Set the key security blob
3936 * @cred: the credentials to use
3939 * No allocation required
3943 static int smack_key_alloc(struct key *key, const struct cred *cred,
3944 unsigned long flags)
3946 struct smack_known *skp = smk_of_task(cred->security);
3948 key->security = skp;
3953 * smack_key_free - Clear the key security blob
3956 * Clear the blob pointer
3958 static void smack_key_free(struct key *key)
3960 key->security = NULL;
3964 * smack_key_permission - Smack access on a key
3965 * @key_ref: gets to the object
3966 * @cred: the credentials to use
3967 * @perm: requested key permissions
3969 * Return 0 if the task has read and write to the object,
3970 * an error code otherwise
3972 static int smack_key_permission(key_ref_t key_ref,
3973 const struct cred *cred, unsigned perm)
3976 struct smk_audit_info ad;
3977 struct smack_known *tkp = smk_of_task(cred->security);
3981 keyp = key_ref_to_ptr(key_ref);
3985 * If the key hasn't been initialized give it access so that
3988 if (keyp->security == NULL)
3991 * This should not occur
3996 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3997 ad.a.u.key_struct.key = keyp->serial;
3998 ad.a.u.key_struct.key_desc = keyp->description;
4000 if (perm & KEY_NEED_READ)
4002 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4003 request = MAY_WRITE;
4004 rc = smk_access(tkp, keyp->security, request, &ad);
4005 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4010 * smack_key_getsecurity - Smack label tagging the key
4011 * @key points to the key to be queried
4012 * @_buffer points to a pointer that should be set to point to the
4013 * resulting string (if no label or an error occurs).
4014 * Return the length of the string (including terminating NUL) or -ve if
4016 * May also return 0 (and a NULL buffer pointer) if there is no label.
4018 static int smack_key_getsecurity(struct key *key, char **_buffer)
4020 struct smack_known *skp = key->security;
4024 if (key->security == NULL) {
4029 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4032 length = strlen(copy) + 1;
4038 #endif /* CONFIG_KEYS */
4043 * Audit requires a unique representation of each Smack specific
4044 * rule. This unique representation is used to distinguish the
4045 * object to be audited from remaining kernel objects and also
4046 * works as a glue between the audit hooks.
4048 * Since repository entries are added but never deleted, we'll use
4049 * the smack_known label address related to the given audit rule as
4050 * the needed unique representation. This also better fits the smack
4051 * model where nearly everything is a label.
4056 * smack_audit_rule_init - Initialize a smack audit rule
4057 * @field: audit rule fields given from user-space (audit.h)
4058 * @op: required testing operator (=, !=, >, <, ...)
4059 * @rulestr: smack label to be audited
4060 * @vrule: pointer to save our own audit rule representation
4062 * Prepare to audit cases where (@field @op @rulestr) is true.
4063 * The label to be audited is created if necessay.
4065 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4067 struct smack_known *skp;
4068 char **rule = (char **)vrule;
4071 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4074 if (op != Audit_equal && op != Audit_not_equal)
4077 skp = smk_import_entry(rulestr, 0);
4079 *rule = skp->smk_known;
4085 * smack_audit_rule_known - Distinguish Smack audit rules
4086 * @krule: rule of interest, in Audit kernel representation format
4088 * This is used to filter Smack rules from remaining Audit ones.
4089 * If it's proved that this rule belongs to us, the
4090 * audit_rule_match hook will be called to do the final judgement.
4092 static int smack_audit_rule_known(struct audit_krule *krule)
4094 struct audit_field *f;
4097 for (i = 0; i < krule->field_count; i++) {
4098 f = &krule->fields[i];
4100 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4108 * smack_audit_rule_match - Audit given object ?
4109 * @secid: security id for identifying the object to test
4110 * @field: audit rule flags given from user-space
4111 * @op: required testing operator
4112 * @vrule: smack internal rule presentation
4113 * @actx: audit context associated with the check
4115 * The core Audit hook. It's used to take the decision of
4116 * whether to audit or not to audit a given object.
4118 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4119 struct audit_context *actx)
4121 struct smack_known *skp;
4124 if (unlikely(!rule)) {
4125 WARN_ONCE(1, "Smack: missing rule\n");
4129 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4132 skp = smack_from_secid(secid);
4135 * No need to do string comparisons. If a match occurs,
4136 * both pointers will point to the same smack_known
4139 if (op == Audit_equal)
4140 return (rule == skp->smk_known);
4141 if (op == Audit_not_equal)
4142 return (rule != skp->smk_known);
4148 * smack_audit_rule_free - free smack rule representation
4149 * @vrule: rule to be freed.
4151 * No memory was allocated.
4153 static void smack_audit_rule_free(void *vrule)
4158 #endif /* CONFIG_AUDIT */
4161 * smack_ismaclabel - check if xattr @name references a smack MAC label
4162 * @name: Full xattr name to check.
4164 static int smack_ismaclabel(const char *name)
4166 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4171 * smack_secid_to_secctx - return the smack label for a secid
4172 * @secid: incoming integer
4173 * @secdata: destination
4174 * @seclen: how long it is
4176 * Exists for networking code.
4178 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4180 struct smack_known *skp = smack_from_secid(secid);
4183 *secdata = skp->smk_known;
4184 *seclen = strlen(skp->smk_known);
4189 * smack_secctx_to_secid - return the secid for a smack label
4190 * @secdata: smack label
4191 * @seclen: how long result is
4192 * @secid: outgoing integer
4194 * Exists for audit and networking code.
4196 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4198 struct smack_known *skp = smk_find_entry(secdata);
4201 *secid = skp->smk_secid;
4208 * smack_release_secctx - don't do anything.
4212 * Exists to make sure nothing gets done, and properly
4214 static void smack_release_secctx(char *secdata, u32 seclen)
4218 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4220 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4223 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4225 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4228 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4231 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4239 struct security_hook_list smack_hooks[] = {
4240 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4241 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4242 LSM_HOOK_INIT(syslog, smack_syslog),
4244 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4245 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4246 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4247 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4248 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4250 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4251 LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
4252 LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
4254 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4255 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4256 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4257 LSM_HOOK_INIT(inode_link, smack_inode_link),
4258 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4259 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4260 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4261 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4262 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4263 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4264 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4265 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4266 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4267 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4268 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4269 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4270 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4271 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4273 LSM_HOOK_INIT(file_permission, smack_file_permission),
4274 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4275 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4276 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4277 LSM_HOOK_INIT(file_lock, smack_file_lock),
4278 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4279 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4280 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4281 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4282 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4283 LSM_HOOK_INIT(file_receive, smack_file_receive),
4285 LSM_HOOK_INIT(file_open, smack_file_open),
4287 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4288 LSM_HOOK_INIT(cred_free, smack_cred_free),
4289 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4290 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4291 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4292 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4293 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4294 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4295 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4296 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4297 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4298 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4299 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4300 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4301 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4302 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4303 LSM_HOOK_INIT(task_kill, smack_task_kill),
4304 LSM_HOOK_INIT(task_wait, smack_task_wait),
4305 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4307 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4308 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4310 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4311 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4313 LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security),
4314 LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security),
4315 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4316 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4317 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4318 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4320 LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security),
4321 LSM_HOOK_INIT(shm_free_security, smack_shm_free_security),
4322 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4323 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4324 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4326 LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security),
4327 LSM_HOOK_INIT(sem_free_security, smack_sem_free_security),
4328 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4329 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4330 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4332 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4334 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4335 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4337 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4338 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4340 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4341 #ifndef CONFIG_SECURITY_SMACK_NETFILTER
4342 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4343 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4344 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4345 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4346 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4347 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4348 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4349 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4350 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4351 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4352 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4353 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4355 /* key management security hooks */
4357 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4358 LSM_HOOK_INIT(key_free, smack_key_free),
4359 LSM_HOOK_INIT(key_permission, smack_key_permission),
4360 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4361 #endif /* CONFIG_KEYS */
4365 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4366 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4367 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4368 LSM_HOOK_INIT(audit_rule_free, smack_audit_rule_free),
4369 #endif /* CONFIG_AUDIT */
4371 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4372 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4373 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4374 LSM_HOOK_INIT(release_secctx, smack_release_secctx),
4375 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4376 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4377 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4381 static __init void init_smack_known_list(void)
4384 * Initialize rule list locks
4386 mutex_init(&smack_known_huh.smk_rules_lock);
4387 mutex_init(&smack_known_hat.smk_rules_lock);
4388 mutex_init(&smack_known_floor.smk_rules_lock);
4389 mutex_init(&smack_known_star.smk_rules_lock);
4390 mutex_init(&smack_known_invalid.smk_rules_lock);
4391 mutex_init(&smack_known_web.smk_rules_lock);
4393 * Initialize rule lists
4395 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4396 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4397 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4398 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4399 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4400 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4402 * Create the known labels list
4404 smk_insert_entry(&smack_known_huh);
4405 smk_insert_entry(&smack_known_hat);
4406 smk_insert_entry(&smack_known_star);
4407 smk_insert_entry(&smack_known_floor);
4408 smk_insert_entry(&smack_known_invalid);
4409 smk_insert_entry(&smack_known_web);
4413 * smack_init - initialize the smack system
4417 static __init int smack_init(void)
4420 struct task_smack *tsp;
4422 if (!security_module_enable("smack"))
4427 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4428 if (!smack_inode_cache)
4431 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4434 kmem_cache_destroy(smack_inode_cache);
4438 printk(KERN_INFO "Smack: Initializing.\n");
4441 * Set the security state for the initial task.
4443 cred = (struct cred *) current->cred;
4444 cred->security = tsp;
4446 /* initialize the smack_known_list */
4447 init_smack_known_list();
4452 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks));
4458 * Smack requires early initialization in order to label
4459 * all processes and objects when they are created.
4461 security_initcall(smack_init);