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)
440 struct smack_known *skp;
442 rc = cap_ptrace_access_check(ctp, mode);
446 skp = smk_of_task_struct(ctp);
448 rc = smk_ptrace_rule_check(current, skp, mode, __func__);
453 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
454 * @ptp: parent task pointer
456 * Returns 0 if access is OK, an error code otherwise
458 * Do the capability checks, and require PTRACE_MODE_ATTACH.
460 static int smack_ptrace_traceme(struct task_struct *ptp)
463 struct smack_known *skp;
465 rc = cap_ptrace_traceme(ptp);
469 skp = smk_of_task(current_security());
471 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
476 * smack_syslog - Smack approval on syslog
477 * @type: message type
479 * Returns 0 on success, error code otherwise.
481 static int smack_syslog(int typefrom_file)
484 struct smack_known *skp = smk_of_current();
486 if (smack_privileged(CAP_MAC_OVERRIDE))
489 if (smack_syslog_label != NULL && smack_syslog_label != skp)
501 * smack_sb_alloc_security - allocate a superblock blob
502 * @sb: the superblock getting the blob
504 * Returns 0 on success or -ENOMEM on error.
506 static int smack_sb_alloc_security(struct super_block *sb)
508 struct superblock_smack *sbsp;
510 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
515 sbsp->smk_root = &smack_known_floor;
516 sbsp->smk_default = &smack_known_floor;
517 sbsp->smk_floor = &smack_known_floor;
518 sbsp->smk_hat = &smack_known_hat;
520 * smk_initialized will be zero from kzalloc.
522 sb->s_security = sbsp;
528 * smack_sb_free_security - free a superblock blob
529 * @sb: the superblock getting the blob
532 static void smack_sb_free_security(struct super_block *sb)
534 kfree(sb->s_security);
535 sb->s_security = NULL;
539 * smack_sb_copy_data - copy mount options data for processing
540 * @orig: where to start
541 * @smackopts: mount options string
543 * Returns 0 on success or -ENOMEM on error.
545 * Copy the Smack specific mount options out of the mount
548 static int smack_sb_copy_data(char *orig, char *smackopts)
550 char *cp, *commap, *otheropts, *dp;
552 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
553 if (otheropts == NULL)
556 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
557 if (strstr(cp, SMK_FSDEFAULT) == cp)
559 else if (strstr(cp, SMK_FSFLOOR) == cp)
561 else if (strstr(cp, SMK_FSHAT) == cp)
563 else if (strstr(cp, SMK_FSROOT) == cp)
565 else if (strstr(cp, SMK_FSTRANS) == cp)
570 commap = strchr(cp, ',');
579 strcpy(orig, otheropts);
580 free_page((unsigned long)otheropts);
586 * smack_sb_kern_mount - Smack specific mount processing
587 * @sb: the file system superblock
588 * @flags: the mount flags
589 * @data: the smack mount options
591 * Returns 0 on success, an error code on failure
593 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
595 struct dentry *root = sb->s_root;
596 struct inode *inode = root->d_inode;
597 struct superblock_smack *sp = sb->s_security;
598 struct inode_smack *isp;
599 struct smack_known *skp;
605 if (sp->smk_initialized)
608 sp->smk_initialized = 1;
610 for (op = data; op != NULL; op = commap) {
611 commap = strchr(op, ',');
615 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
616 op += strlen(SMK_FSHAT);
617 skp = smk_import_entry(op, 0);
622 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
623 op += strlen(SMK_FSFLOOR);
624 skp = smk_import_entry(op, 0);
629 } else if (strncmp(op, SMK_FSDEFAULT,
630 strlen(SMK_FSDEFAULT)) == 0) {
631 op += strlen(SMK_FSDEFAULT);
632 skp = smk_import_entry(op, 0);
634 sp->smk_default = skp;
637 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
638 op += strlen(SMK_FSROOT);
639 skp = smk_import_entry(op, 0);
644 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
645 op += strlen(SMK_FSTRANS);
646 skp = smk_import_entry(op, 0);
655 if (!smack_privileged(CAP_MAC_ADMIN)) {
657 * Unprivileged mounts don't get to specify Smack values.
662 * Unprivileged mounts get root and default from the caller.
664 skp = smk_of_current();
666 sp->smk_default = skp;
669 * Initialize the root inode.
671 isp = inode->i_security;
673 isp = new_inode_smack(sp->smk_root);
676 inode->i_security = isp;
678 isp->smk_inode = sp->smk_root;
681 isp->smk_flags |= SMK_INODE_TRANSMUTE;
687 * smack_sb_statfs - Smack check on statfs
688 * @dentry: identifies the file system in question
690 * Returns 0 if current can read the floor of the filesystem,
691 * and error code otherwise
693 static int smack_sb_statfs(struct dentry *dentry)
695 struct superblock_smack *sbp = dentry->d_sb->s_security;
697 struct smk_audit_info ad;
699 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
700 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
702 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
703 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
712 * smack_bprm_set_creds - set creds for exec
713 * @bprm: the exec information
715 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
717 static int smack_bprm_set_creds(struct linux_binprm *bprm)
719 struct inode *inode = file_inode(bprm->file);
720 struct task_smack *bsp = bprm->cred->security;
721 struct inode_smack *isp;
724 rc = cap_bprm_set_creds(bprm);
728 if (bprm->cred_prepared)
731 isp = inode->i_security;
732 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
735 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
736 struct task_struct *tracer;
740 tracer = ptrace_parent(current);
741 if (likely(tracer != NULL))
742 rc = smk_ptrace_rule_check(tracer,
750 } else if (bprm->unsafe)
753 bsp->smk_task = isp->smk_task;
754 bprm->per_clear |= PER_CLEAR_ON_SETID;
760 * smack_bprm_committing_creds - Prepare to install the new credentials
763 * @bprm: binprm for exec
765 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
767 struct task_smack *bsp = bprm->cred->security;
769 if (bsp->smk_task != bsp->smk_forked)
770 current->pdeath_signal = 0;
774 * smack_bprm_secureexec - Return the decision to use secureexec.
775 * @bprm: binprm for exec
777 * Returns 0 on success.
779 static int smack_bprm_secureexec(struct linux_binprm *bprm)
781 struct task_smack *tsp = current_security();
782 int ret = cap_bprm_secureexec(bprm);
784 if (!ret && (tsp->smk_task != tsp->smk_forked))
795 * smack_inode_alloc_security - allocate an inode blob
796 * @inode: the inode in need of a blob
798 * Returns 0 if it gets a blob, -ENOMEM otherwise
800 static int smack_inode_alloc_security(struct inode *inode)
802 struct smack_known *skp = smk_of_current();
804 inode->i_security = new_inode_smack(skp);
805 if (inode->i_security == NULL)
811 * smack_inode_free_security - free an inode blob
812 * @inode: the inode with a blob
814 * Clears the blob pointer in inode
816 static void smack_inode_free_security(struct inode *inode)
818 kmem_cache_free(smack_inode_cache, inode->i_security);
819 inode->i_security = NULL;
823 * smack_inode_init_security - copy out the smack from an inode
824 * @inode: the newly created inode
825 * @dir: containing directory object
827 * @name: where to put the attribute name
828 * @value: where to put the attribute value
829 * @len: where to put the length of the attribute
831 * Returns 0 if it all works out, -ENOMEM if there's no memory
833 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
834 const struct qstr *qstr, const char **name,
835 void **value, size_t *len)
837 struct inode_smack *issp = inode->i_security;
838 struct smack_known *skp = smk_of_current();
839 struct smack_known *isp = smk_of_inode(inode);
840 struct smack_known *dsp = smk_of_inode(dir);
844 *name = XATTR_SMACK_SUFFIX;
848 may = smk_access_entry(skp->smk_known, dsp->smk_known,
853 * If the access rule allows transmutation and
854 * the directory requests transmutation then
855 * by all means transmute.
856 * Mark the inode as changed.
858 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
859 smk_inode_transmutable(dir)) {
861 issp->smk_flags |= SMK_INODE_CHANGED;
864 *value = kstrdup(isp->smk_known, GFP_NOFS);
868 *len = strlen(isp->smk_known);
875 * smack_inode_link - Smack check on link
876 * @old_dentry: the existing object
878 * @new_dentry: the new object
880 * Returns 0 if access is permitted, an error code otherwise
882 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
883 struct dentry *new_dentry)
885 struct smack_known *isp;
886 struct smk_audit_info ad;
889 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
890 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
892 isp = smk_of_inode(old_dentry->d_inode);
893 rc = smk_curacc(isp, MAY_WRITE, &ad);
894 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
896 if (rc == 0 && d_is_positive(new_dentry)) {
897 isp = smk_of_inode(new_dentry->d_inode);
898 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
899 rc = smk_curacc(isp, MAY_WRITE, &ad);
900 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
907 * smack_inode_unlink - Smack check on inode deletion
908 * @dir: containing directory object
909 * @dentry: file to unlink
911 * Returns 0 if current can write the containing directory
912 * and the object, error code otherwise
914 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
916 struct inode *ip = dentry->d_inode;
917 struct smk_audit_info ad;
920 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
921 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
924 * You need write access to the thing you're unlinking
926 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
927 rc = smk_bu_inode(ip, MAY_WRITE, rc);
930 * You also need write access to the containing directory
932 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
933 smk_ad_setfield_u_fs_inode(&ad, dir);
934 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
935 rc = smk_bu_inode(dir, MAY_WRITE, rc);
941 * smack_inode_rmdir - Smack check on directory deletion
942 * @dir: containing directory object
943 * @dentry: directory to unlink
945 * Returns 0 if current can write the containing directory
946 * and the directory, error code otherwise
948 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
950 struct smk_audit_info ad;
953 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
954 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
957 * You need write access to the thing you're removing
959 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
960 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
963 * You also need write access to the containing directory
965 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
966 smk_ad_setfield_u_fs_inode(&ad, dir);
967 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
968 rc = smk_bu_inode(dir, MAY_WRITE, rc);
975 * smack_inode_rename - Smack check on rename
977 * @old_dentry: the old object
979 * @new_dentry: the new object
981 * Read and write access is required on both the old and
984 * Returns 0 if access is permitted, an error code otherwise
986 static int smack_inode_rename(struct inode *old_inode,
987 struct dentry *old_dentry,
988 struct inode *new_inode,
989 struct dentry *new_dentry)
992 struct smack_known *isp;
993 struct smk_audit_info ad;
995 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
996 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
998 isp = smk_of_inode(old_dentry->d_inode);
999 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1000 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
1002 if (rc == 0 && d_is_positive(new_dentry)) {
1003 isp = smk_of_inode(new_dentry->d_inode);
1004 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1005 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1006 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
1012 * smack_inode_permission - Smack version of permission()
1013 * @inode: the inode in question
1014 * @mask: the access requested
1016 * This is the important Smack hook.
1018 * Returns 0 if access is permitted, -EACCES otherwise
1020 static int smack_inode_permission(struct inode *inode, int mask)
1022 struct smk_audit_info ad;
1023 int no_block = mask & MAY_NOT_BLOCK;
1026 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1028 * No permission to check. Existence test. Yup, it's there.
1033 /* May be droppable after audit */
1036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1037 smk_ad_setfield_u_fs_inode(&ad, inode);
1038 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1039 rc = smk_bu_inode(inode, mask, rc);
1044 * smack_inode_setattr - Smack check for setting attributes
1045 * @dentry: the object
1046 * @iattr: for the force flag
1048 * Returns 0 if access is permitted, an error code otherwise
1050 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1052 struct smk_audit_info ad;
1056 * Need to allow for clearing the setuid bit.
1058 if (iattr->ia_valid & ATTR_FORCE)
1060 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1061 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1063 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1064 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1069 * smack_inode_getattr - Smack check for getting attributes
1070 * @mnt: vfsmount of the object
1071 * @dentry: the object
1073 * Returns 0 if access is permitted, an error code otherwise
1075 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1077 struct smk_audit_info ad;
1081 path.dentry = dentry;
1084 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1085 smk_ad_setfield_u_fs_path(&ad, path);
1086 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1087 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1092 * smack_inode_setxattr - Smack check for setting xattrs
1093 * @dentry: the object
1094 * @name: name of the attribute
1095 * @value: value of the attribute
1096 * @size: size of the value
1099 * This protects the Smack attribute explicitly.
1101 * Returns 0 if access is permitted, an error code otherwise
1103 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1104 const void *value, size_t size, int flags)
1106 struct smk_audit_info ad;
1107 struct smack_known *skp;
1109 int check_import = 0;
1114 * Check label validity here so import won't fail in post_setxattr
1116 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1117 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1118 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1121 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1122 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1126 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1128 if (size != TRANS_TRUE_SIZE ||
1129 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1132 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1134 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1137 if (rc == 0 && check_import) {
1138 skp = size ? smk_import_entry(value, size) : NULL;
1139 if (skp == NULL || (check_star &&
1140 (skp == &smack_known_star || skp == &smack_known_web)))
1144 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1145 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1148 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1149 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1156 * smack_inode_post_setxattr - Apply the Smack update approved above
1158 * @name: attribute name
1159 * @value: attribute value
1160 * @size: attribute size
1163 * Set the pointer in the inode blob to the entry found
1164 * in the master label list.
1166 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1167 const void *value, size_t size, int flags)
1169 struct smack_known *skp;
1170 struct inode_smack *isp = dentry->d_inode->i_security;
1172 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1173 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1177 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1178 skp = smk_import_entry(value, size);
1180 isp->smk_inode = skp;
1182 isp->smk_inode = &smack_known_invalid;
1183 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1184 skp = smk_import_entry(value, size);
1186 isp->smk_task = skp;
1188 isp->smk_task = &smack_known_invalid;
1189 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1190 skp = smk_import_entry(value, size);
1192 isp->smk_mmap = skp;
1194 isp->smk_mmap = &smack_known_invalid;
1201 * smack_inode_getxattr - Smack check on getxattr
1202 * @dentry: the object
1205 * Returns 0 if access is permitted, an error code otherwise
1207 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1209 struct smk_audit_info ad;
1212 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1213 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1215 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1216 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1221 * smack_inode_removexattr - Smack check on removexattr
1222 * @dentry: the object
1223 * @name: name of the attribute
1225 * Removing the Smack attribute requires CAP_MAC_ADMIN
1227 * Returns 0 if access is permitted, an error code otherwise
1229 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1231 struct inode_smack *isp;
1232 struct smk_audit_info ad;
1235 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1236 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1237 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1238 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1239 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1240 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1241 if (!smack_privileged(CAP_MAC_ADMIN))
1244 rc = cap_inode_removexattr(dentry, name);
1249 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1250 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1252 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1253 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1257 isp = dentry->d_inode->i_security;
1259 * Don't do anything special for these.
1260 * XATTR_NAME_SMACKIPIN
1261 * XATTR_NAME_SMACKIPOUT
1262 * XATTR_NAME_SMACKEXEC
1264 if (strcmp(name, XATTR_NAME_SMACK) == 0)
1265 isp->smk_task = NULL;
1266 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1267 isp->smk_mmap = NULL;
1268 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1269 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1275 * smack_inode_getsecurity - get smack xattrs
1276 * @inode: the object
1277 * @name: attribute name
1278 * @buffer: where to put the result
1281 * Returns the size of the attribute or an error code
1283 static int smack_inode_getsecurity(const struct inode *inode,
1284 const char *name, void **buffer,
1287 struct socket_smack *ssp;
1288 struct socket *sock;
1289 struct super_block *sbp;
1290 struct inode *ip = (struct inode *)inode;
1291 struct smack_known *isp;
1295 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1296 isp = smk_of_inode(inode);
1297 ilen = strlen(isp->smk_known);
1298 *buffer = isp->smk_known;
1303 * The rest of the Smack xattrs are only on sockets.
1306 if (sbp->s_magic != SOCKFS_MAGIC)
1309 sock = SOCKET_I(ip);
1310 if (sock == NULL || sock->sk == NULL)
1313 ssp = sock->sk->sk_security;
1315 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1317 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1322 ilen = strlen(isp->smk_known);
1324 *buffer = isp->smk_known;
1333 * smack_inode_listsecurity - list the Smack attributes
1334 * @inode: the object
1335 * @buffer: where they go
1336 * @buffer_size: size of buffer
1338 * Returns 0 on success, -EINVAL otherwise
1340 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1343 int len = sizeof(XATTR_NAME_SMACK);
1345 if (buffer != NULL && len <= buffer_size)
1346 memcpy(buffer, XATTR_NAME_SMACK, len);
1352 * smack_inode_getsecid - Extract inode's security id
1353 * @inode: inode to extract the info from
1354 * @secid: where result will be saved
1356 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1358 struct inode_smack *isp = inode->i_security;
1360 *secid = isp->smk_inode->smk_secid;
1368 * smack_file_permission - Smack check on file operations
1374 * Should access checks be done on each read or write?
1375 * UNICOS and SELinux say yes.
1376 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1378 * I'll say no for now. Smack does not do the frequent
1379 * label changing that SELinux does.
1381 static int smack_file_permission(struct file *file, int mask)
1387 * smack_file_alloc_security - assign a file security blob
1390 * The security blob for a file is a pointer to the master
1391 * label list, so no allocation is done.
1393 * f_security is the owner security information. It
1394 * isn't used on file access checks, it's for send_sigio.
1398 static int smack_file_alloc_security(struct file *file)
1400 struct smack_known *skp = smk_of_current();
1402 file->f_security = skp;
1407 * smack_file_free_security - clear a file security blob
1410 * The security blob for a file is a pointer to the master
1411 * label list, so no memory is freed.
1413 static void smack_file_free_security(struct file *file)
1415 file->f_security = NULL;
1419 * smack_file_ioctl - Smack check on ioctls
1424 * Relies heavily on the correct use of the ioctl command conventions.
1426 * Returns 0 if allowed, error code otherwise
1428 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1432 struct smk_audit_info ad;
1433 struct inode *inode = file_inode(file);
1435 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1436 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1438 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1439 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1440 rc = smk_bu_file(file, MAY_WRITE, rc);
1443 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1444 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1445 rc = smk_bu_file(file, MAY_READ, rc);
1452 * smack_file_lock - Smack check on file locking
1456 * Returns 0 if current has lock access, error code otherwise
1458 static int smack_file_lock(struct file *file, unsigned int cmd)
1460 struct smk_audit_info ad;
1462 struct inode *inode = file_inode(file);
1464 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1465 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1466 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1467 rc = smk_bu_file(file, MAY_LOCK, rc);
1472 * smack_file_fcntl - Smack check on fcntl
1474 * @cmd: what action to check
1477 * Generally these operations are harmless.
1478 * File locking operations present an obvious mechanism
1479 * for passing information, so they require write access.
1481 * Returns 0 if current has access, error code otherwise
1483 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1486 struct smk_audit_info ad;
1488 struct inode *inode = file_inode(file);
1495 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1496 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1497 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1498 rc = smk_bu_file(file, MAY_LOCK, rc);
1502 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1503 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1504 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1505 rc = smk_bu_file(file, MAY_WRITE, rc);
1516 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1517 * if mapping anonymous memory.
1518 * @file contains the file structure for file to map (may be NULL).
1519 * @reqprot contains the protection requested by the application.
1520 * @prot contains the protection that will be applied by the kernel.
1521 * @flags contains the operational flags.
1522 * Return 0 if permission is granted.
1524 static int smack_mmap_file(struct file *file,
1525 unsigned long reqprot, unsigned long prot,
1526 unsigned long flags)
1528 struct smack_known *skp;
1529 struct smack_known *mkp;
1530 struct smack_rule *srp;
1531 struct task_smack *tsp;
1532 struct smack_known *okp;
1533 struct inode_smack *isp;
1542 isp = file_inode(file)->i_security;
1543 if (isp->smk_mmap == NULL)
1545 mkp = isp->smk_mmap;
1547 tsp = current_security();
1548 skp = smk_of_current();
1553 * For each Smack rule associated with the subject
1554 * label verify that the SMACK64MMAP also has access
1555 * to that rule's object label.
1557 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1558 okp = srp->smk_object;
1560 * Matching labels always allows access.
1562 if (mkp->smk_known == okp->smk_known)
1565 * If there is a matching local rule take
1566 * that into account as well.
1568 may = smk_access_entry(srp->smk_subject->smk_known,
1572 may = srp->smk_access;
1574 may &= srp->smk_access;
1576 * If may is zero the SMACK64MMAP subject can't
1577 * possibly have less access.
1583 * Fetch the global list entry.
1584 * If there isn't one a SMACK64MMAP subject
1585 * can't have as much access as current.
1587 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1589 if (mmay == -ENOENT) {
1594 * If there is a local entry it modifies the
1595 * potential access, too.
1597 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1599 if (tmay != -ENOENT)
1603 * If there is any access available to current that is
1604 * not available to a SMACK64MMAP subject
1607 if ((may | mmay) != mmay) {
1619 * smack_file_set_fowner - set the file security blob value
1620 * @file: object in question
1623 static void smack_file_set_fowner(struct file *file)
1625 file->f_security = smk_of_current();
1629 * smack_file_send_sigiotask - Smack on sigio
1630 * @tsk: The target task
1631 * @fown: the object the signal come from
1634 * Allow a privileged task to get signals even if it shouldn't
1636 * Returns 0 if a subject with the object's smack could
1637 * write to the task, an error code otherwise.
1639 static int smack_file_send_sigiotask(struct task_struct *tsk,
1640 struct fown_struct *fown, int signum)
1642 struct smack_known *skp;
1643 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1646 struct smk_audit_info ad;
1649 * struct fown_struct is never outside the context of a struct file
1651 file = container_of(fown, struct file, f_owner);
1653 /* we don't log here as rc can be overriden */
1654 skp = file->f_security;
1655 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1656 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
1657 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1660 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1661 smk_ad_setfield_u_tsk(&ad, tsk);
1662 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
1667 * smack_file_receive - Smack file receive check
1670 * Returns 0 if current has access, error code otherwise
1672 static int smack_file_receive(struct file *file)
1676 struct smk_audit_info ad;
1677 struct inode *inode = file_inode(file);
1679 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1680 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1682 * This code relies on bitmasks.
1684 if (file->f_mode & FMODE_READ)
1686 if (file->f_mode & FMODE_WRITE)
1689 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1690 rc = smk_bu_file(file, may, rc);
1695 * smack_file_open - Smack dentry open processing
1697 * @cred: task credential
1699 * Set the security blob in the file structure.
1700 * Allow the open only if the task has read access. There are
1701 * many read operations (e.g. fstat) that you can do with an
1702 * fd even if you have the file open write-only.
1706 static int smack_file_open(struct file *file, const struct cred *cred)
1708 struct task_smack *tsp = cred->security;
1709 struct inode *inode = file_inode(file);
1710 struct smk_audit_info ad;
1713 if (smack_privileged(CAP_MAC_OVERRIDE))
1716 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1717 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1718 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
1719 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
1729 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1730 * @new: the new credentials
1731 * @gfp: the atomicity of any memory allocations
1733 * Prepare a blank set of credentials for modification. This must allocate all
1734 * the memory the LSM module might require such that cred_transfer() can
1735 * complete without error.
1737 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1739 struct task_smack *tsp;
1741 tsp = new_task_smack(NULL, NULL, gfp);
1745 cred->security = tsp;
1752 * smack_cred_free - "free" task-level security credentials
1753 * @cred: the credentials in question
1756 static void smack_cred_free(struct cred *cred)
1758 struct task_smack *tsp = cred->security;
1759 struct smack_rule *rp;
1760 struct list_head *l;
1761 struct list_head *n;
1765 cred->security = NULL;
1767 list_for_each_safe(l, n, &tsp->smk_rules) {
1768 rp = list_entry(l, struct smack_rule, list);
1769 list_del(&rp->list);
1776 * smack_cred_prepare - prepare new set of credentials for modification
1777 * @new: the new credentials
1778 * @old: the original credentials
1779 * @gfp: the atomicity of any memory allocations
1781 * Prepare a new set of credentials for modification.
1783 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1786 struct task_smack *old_tsp = old->security;
1787 struct task_smack *new_tsp;
1790 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1791 if (new_tsp == NULL)
1794 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1798 new->security = new_tsp;
1803 * smack_cred_transfer - Transfer the old credentials to the new credentials
1804 * @new: the new credentials
1805 * @old: the original credentials
1807 * Fill in a set of blank credentials from another set of credentials.
1809 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1811 struct task_smack *old_tsp = old->security;
1812 struct task_smack *new_tsp = new->security;
1814 new_tsp->smk_task = old_tsp->smk_task;
1815 new_tsp->smk_forked = old_tsp->smk_task;
1816 mutex_init(&new_tsp->smk_rules_lock);
1817 INIT_LIST_HEAD(&new_tsp->smk_rules);
1820 /* cbs copy rule list */
1824 * smack_kernel_act_as - Set the subjective context in a set of credentials
1825 * @new: points to the set of credentials to be modified.
1826 * @secid: specifies the security ID to be set
1828 * Set the security data for a kernel service.
1830 static int smack_kernel_act_as(struct cred *new, u32 secid)
1832 struct task_smack *new_tsp = new->security;
1833 struct smack_known *skp = smack_from_secid(secid);
1838 new_tsp->smk_task = skp;
1843 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1844 * @new: points to the set of credentials to be modified
1845 * @inode: points to the inode to use as a reference
1847 * Set the file creation context in a set of credentials to the same
1848 * as the objective context of the specified inode
1850 static int smack_kernel_create_files_as(struct cred *new,
1851 struct inode *inode)
1853 struct inode_smack *isp = inode->i_security;
1854 struct task_smack *tsp = new->security;
1856 tsp->smk_forked = isp->smk_inode;
1857 tsp->smk_task = tsp->smk_forked;
1862 * smk_curacc_on_task - helper to log task related access
1863 * @p: the task object
1864 * @access: the access requested
1865 * @caller: name of the calling function for audit
1867 * Return 0 if access is permitted
1869 static int smk_curacc_on_task(struct task_struct *p, int access,
1872 struct smk_audit_info ad;
1873 struct smack_known *skp = smk_of_task_struct(p);
1876 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1877 smk_ad_setfield_u_tsk(&ad, p);
1878 rc = smk_curacc(skp, access, &ad);
1879 rc = smk_bu_task(p, access, rc);
1884 * smack_task_setpgid - Smack check on setting pgid
1885 * @p: the task object
1888 * Return 0 if write access is permitted
1890 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1892 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1896 * smack_task_getpgid - Smack access check for getpgid
1897 * @p: the object task
1899 * Returns 0 if current can read the object task, error code otherwise
1901 static int smack_task_getpgid(struct task_struct *p)
1903 return smk_curacc_on_task(p, MAY_READ, __func__);
1907 * smack_task_getsid - Smack access check for getsid
1908 * @p: the object task
1910 * Returns 0 if current can read the object task, error code otherwise
1912 static int smack_task_getsid(struct task_struct *p)
1914 return smk_curacc_on_task(p, MAY_READ, __func__);
1918 * smack_task_getsecid - get the secid of the task
1919 * @p: the object task
1920 * @secid: where to put the result
1922 * Sets the secid to contain a u32 version of the smack label.
1924 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1926 struct smack_known *skp = smk_of_task_struct(p);
1928 *secid = skp->smk_secid;
1932 * smack_task_setnice - Smack check on setting nice
1933 * @p: the task object
1936 * Return 0 if write access is permitted
1938 static int smack_task_setnice(struct task_struct *p, int nice)
1942 rc = cap_task_setnice(p, nice);
1944 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1949 * smack_task_setioprio - Smack check on setting ioprio
1950 * @p: the task object
1953 * Return 0 if write access is permitted
1955 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1959 rc = cap_task_setioprio(p, ioprio);
1961 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1966 * smack_task_getioprio - Smack check on reading ioprio
1967 * @p: the task object
1969 * Return 0 if read access is permitted
1971 static int smack_task_getioprio(struct task_struct *p)
1973 return smk_curacc_on_task(p, MAY_READ, __func__);
1977 * smack_task_setscheduler - Smack check on setting scheduler
1978 * @p: the task object
1982 * Return 0 if read access is permitted
1984 static int smack_task_setscheduler(struct task_struct *p)
1988 rc = cap_task_setscheduler(p);
1990 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1995 * smack_task_getscheduler - Smack check on reading scheduler
1996 * @p: the task object
1998 * Return 0 if read access is permitted
2000 static int smack_task_getscheduler(struct task_struct *p)
2002 return smk_curacc_on_task(p, MAY_READ, __func__);
2006 * smack_task_movememory - Smack check on moving memory
2007 * @p: the task object
2009 * Return 0 if write access is permitted
2011 static int smack_task_movememory(struct task_struct *p)
2013 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2017 * smack_task_kill - Smack check on signal delivery
2018 * @p: the task object
2021 * @secid: identifies the smack to use in lieu of current's
2023 * Return 0 if write access is permitted
2025 * The secid behavior is an artifact of an SELinux hack
2026 * in the USB code. Someday it may go away.
2028 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2031 struct smk_audit_info ad;
2032 struct smack_known *skp;
2033 struct smack_known *tkp = smk_of_task_struct(p);
2036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2037 smk_ad_setfield_u_tsk(&ad, p);
2039 * Sending a signal requires that the sender
2040 * can write the receiver.
2043 rc = smk_curacc(tkp, MAY_WRITE, &ad);
2044 rc = smk_bu_task(p, MAY_WRITE, rc);
2048 * If the secid isn't 0 we're dealing with some USB IO
2049 * specific behavior. This is not clean. For one thing
2050 * we can't take privilege into account.
2052 skp = smack_from_secid(secid);
2053 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2054 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
2059 * smack_task_wait - Smack access check for waiting
2060 * @p: task to wait for
2064 static int smack_task_wait(struct task_struct *p)
2067 * Allow the operation to succeed.
2069 * In userless environments (e.g. phones) programs
2070 * get marked with SMACK64EXEC and even if the parent
2071 * and child shouldn't be talking the parent still
2072 * may expect to know when the child exits.
2078 * smack_task_to_inode - copy task smack into the inode blob
2079 * @p: task to copy from
2080 * @inode: inode to copy to
2082 * Sets the smack pointer in the inode security blob
2084 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2086 struct inode_smack *isp = inode->i_security;
2087 struct smack_known *skp = smk_of_task_struct(p);
2089 isp->smk_inode = skp;
2097 * smack_sk_alloc_security - Allocate a socket blob
2100 * @gfp_flags: memory allocation flags
2102 * Assign Smack pointers to current
2104 * Returns 0 on success, -ENOMEM is there's no memory
2106 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2108 struct smack_known *skp = smk_of_current();
2109 struct socket_smack *ssp;
2111 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2117 ssp->smk_packet = NULL;
2119 sk->sk_security = ssp;
2125 * smack_sk_free_security - Free a socket blob
2128 * Clears the blob pointer
2130 static void smack_sk_free_security(struct sock *sk)
2132 kfree(sk->sk_security);
2136 * smack_host_label - check host based restrictions
2137 * @sip: the object end
2139 * looks for host based access restrictions
2141 * This version will only be appropriate for really small sets of single label
2142 * hosts. The caller is responsible for ensuring that the RCU read lock is
2143 * taken before calling this function.
2145 * Returns the label of the far end or NULL if it's not special.
2147 static struct smack_known *smack_host_label(struct sockaddr_in *sip)
2149 struct smk_netlbladdr *snp;
2150 struct in_addr *siap = &sip->sin_addr;
2152 if (siap->s_addr == 0)
2155 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2157 * we break after finding the first match because
2158 * the list is sorted from longest to shortest mask
2159 * so we have found the most specific match
2161 if ((&snp->smk_host.sin_addr)->s_addr ==
2162 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2163 /* we have found the special CIPSO option */
2164 if (snp->smk_label == &smack_cipso_option)
2166 return snp->smk_label;
2173 * smack_netlabel - Set the secattr on a socket
2175 * @labeled: socket label scheme
2177 * Convert the outbound smack value (smk_out) to a
2178 * secattr and attach it to the socket.
2180 * Returns 0 on success or an error code
2182 static int smack_netlabel(struct sock *sk, int labeled)
2184 struct smack_known *skp;
2185 struct socket_smack *ssp = sk->sk_security;
2189 * Usually the netlabel code will handle changing the
2190 * packet labeling based on the label.
2191 * The case of a single label host is different, because
2192 * a single label host should never get a labeled packet
2193 * even though the label is usually associated with a packet
2197 bh_lock_sock_nested(sk);
2199 if (ssp->smk_out == smack_net_ambient ||
2200 labeled == SMACK_UNLABELED_SOCKET)
2201 netlbl_sock_delattr(sk);
2204 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2214 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2216 * @sap: the destination address
2218 * Set the correct secattr for the given socket based on the destination
2219 * address and perform any outbound access checks needed.
2221 * Returns 0 on success or an error code.
2224 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2226 struct smack_known *skp;
2229 struct smack_known *hkp;
2230 struct socket_smack *ssp = sk->sk_security;
2231 struct smk_audit_info ad;
2234 hkp = smack_host_label(sap);
2237 struct lsm_network_audit net;
2239 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2240 ad.a.u.net->family = sap->sin_family;
2241 ad.a.u.net->dport = sap->sin_port;
2242 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2244 sk_lbl = SMACK_UNLABELED_SOCKET;
2246 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2247 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2249 sk_lbl = SMACK_CIPSO_SOCKET;
2256 return smack_netlabel(sk, sk_lbl);
2259 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2261 * smk_ipv6_port_label - Smack port access table management
2265 * Create or update the port list entry
2267 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2269 struct sock *sk = sock->sk;
2270 struct sockaddr_in6 *addr6;
2271 struct socket_smack *ssp = sock->sk->sk_security;
2272 struct smk_port_label *spp;
2273 unsigned short port = 0;
2275 if (address == NULL) {
2277 * This operation is changing the Smack information
2278 * on the bound socket. Take the changes to the port
2281 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2282 if (sk != spp->smk_sock)
2284 spp->smk_in = ssp->smk_in;
2285 spp->smk_out = ssp->smk_out;
2289 * A NULL address is only used for updating existing
2290 * bound entries. If there isn't one, it's OK.
2295 addr6 = (struct sockaddr_in6 *)address;
2296 port = ntohs(addr6->sin6_port);
2298 * This is a special case that is safely ignored.
2304 * Look for an existing port list entry.
2305 * This is an indication that a port is getting reused.
2307 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2308 if (spp->smk_port != port)
2310 spp->smk_port = port;
2312 spp->smk_in = ssp->smk_in;
2313 spp->smk_out = ssp->smk_out;
2318 * A new port entry is required.
2320 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2324 spp->smk_port = port;
2326 spp->smk_in = ssp->smk_in;
2327 spp->smk_out = ssp->smk_out;
2329 list_add(&spp->list, &smk_ipv6_port_list);
2334 * smk_ipv6_port_check - check Smack port access
2338 * Create or update the port list entry
2340 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2345 struct smk_port_label *spp;
2346 struct socket_smack *ssp = sk->sk_security;
2347 struct smack_known *skp;
2348 unsigned short port = 0;
2349 struct smack_known *object;
2350 struct smk_audit_info ad;
2353 struct lsm_network_audit net;
2356 if (act == SMK_RECEIVING) {
2357 skp = smack_net_ambient;
2358 object = ssp->smk_in;
2361 object = smack_net_ambient;
2365 * Get the IP address and port from the address.
2367 port = ntohs(address->sin6_port);
2368 bep = (__be16 *)(&address->sin6_addr);
2369 be32p = (__be32 *)(&address->sin6_addr);
2372 * It's remote, so port lookup does no good.
2374 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2378 * It's local so the send check has to have passed.
2380 if (act == SMK_RECEIVING) {
2381 skp = &smack_known_web;
2385 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2386 if (spp->smk_port != port)
2388 object = spp->smk_in;
2389 if (act == SMK_CONNECTING)
2390 ssp->smk_packet = spp->smk_out;
2397 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2398 ad.a.u.net->family = sk->sk_family;
2399 ad.a.u.net->dport = port;
2400 if (act == SMK_RECEIVING)
2401 ad.a.u.net->v6info.saddr = address->sin6_addr;
2403 ad.a.u.net->v6info.daddr = address->sin6_addr;
2405 rc = smk_access(skp, object, MAY_WRITE, &ad);
2406 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2409 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2412 * smack_inode_setsecurity - set smack xattrs
2413 * @inode: the object
2414 * @name: attribute name
2415 * @value: attribute value
2416 * @size: size of the attribute
2419 * Sets the named attribute in the appropriate blob
2421 * Returns 0 on success, or an error code
2423 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2424 const void *value, size_t size, int flags)
2426 struct smack_known *skp;
2427 struct inode_smack *nsp = inode->i_security;
2428 struct socket_smack *ssp;
2429 struct socket *sock;
2432 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2435 skp = smk_import_entry(value, size);
2439 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2440 nsp->smk_inode = skp;
2441 nsp->smk_flags |= SMK_INODE_INSTANT;
2445 * The rest of the Smack xattrs are only on sockets.
2447 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2450 sock = SOCKET_I(inode);
2451 if (sock == NULL || sock->sk == NULL)
2454 ssp = sock->sk->sk_security;
2456 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2458 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2460 if (sock->sk->sk_family == PF_INET) {
2461 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2464 "Smack: \"%s\" netlbl error %d.\n",
2470 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2471 if (sock->sk->sk_family == PF_INET6)
2472 smk_ipv6_port_label(sock, NULL);
2473 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2479 * smack_socket_post_create - finish socket setup
2481 * @family: protocol family
2486 * Sets the netlabel information on the socket
2488 * Returns 0 on success, and error code otherwise
2490 static int smack_socket_post_create(struct socket *sock, int family,
2491 int type, int protocol, int kern)
2493 struct socket_smack *ssp;
2495 if (sock->sk == NULL)
2499 * Sockets created by kernel threads receive web label.
2501 if (unlikely(current->flags & PF_KTHREAD)) {
2502 ssp = sock->sk->sk_security;
2503 ssp->smk_in = &smack_known_web;
2504 ssp->smk_out = &smack_known_web;
2507 if (family != PF_INET)
2510 * Set the outbound netlbl.
2512 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2515 #ifndef CONFIG_SECURITY_SMACK_NETFILTER
2517 * smack_socket_bind - record port binding information.
2519 * @address: the port address
2520 * @addrlen: size of the address
2522 * Records the label bound to a port.
2526 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2529 #if IS_ENABLED(CONFIG_IPV6)
2530 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2531 smk_ipv6_port_label(sock, address);
2536 #endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
2539 * smack_socket_connect - connect access check
2541 * @sap: the other end
2542 * @addrlen: size of sap
2544 * Verifies that a connection may be possible
2546 * Returns 0 on success, and error code otherwise
2548 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2553 if (sock->sk == NULL)
2556 switch (sock->sk->sk_family) {
2558 if (addrlen < sizeof(struct sockaddr_in))
2560 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2563 if (addrlen < sizeof(struct sockaddr_in6))
2565 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2566 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2568 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2575 * smack_flags_to_may - convert S_ to MAY_ values
2576 * @flags: the S_ value
2578 * Returns the equivalent MAY_ value
2580 static int smack_flags_to_may(int flags)
2584 if (flags & S_IRUGO)
2586 if (flags & S_IWUGO)
2588 if (flags & S_IXUGO)
2595 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2600 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2602 struct smack_known *skp = smk_of_current();
2604 msg->security = skp;
2609 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2612 * Clears the blob pointer
2614 static void smack_msg_msg_free_security(struct msg_msg *msg)
2616 msg->security = NULL;
2620 * smack_of_shm - the smack pointer for the shm
2623 * Returns a pointer to the smack value
2625 static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
2627 return (struct smack_known *)shp->shm_perm.security;
2631 * smack_shm_alloc_security - Set the security blob for shm
2636 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2638 struct kern_ipc_perm *isp = &shp->shm_perm;
2639 struct smack_known *skp = smk_of_current();
2641 isp->security = skp;
2646 * smack_shm_free_security - Clear the security blob for shm
2649 * Clears the blob pointer
2651 static void smack_shm_free_security(struct shmid_kernel *shp)
2653 struct kern_ipc_perm *isp = &shp->shm_perm;
2655 isp->security = NULL;
2659 * smk_curacc_shm : check if current has access on shm
2661 * @access : access requested
2663 * Returns 0 if current has the requested access, error code otherwise
2665 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2667 struct smack_known *ssp = smack_of_shm(shp);
2668 struct smk_audit_info ad;
2672 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2673 ad.a.u.ipc_id = shp->shm_perm.id;
2675 rc = smk_curacc(ssp, access, &ad);
2676 rc = smk_bu_current("shm", ssp, access, rc);
2681 * smack_shm_associate - Smack access check for shm
2683 * @shmflg: access requested
2685 * Returns 0 if current has the requested access, error code otherwise
2687 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2691 may = smack_flags_to_may(shmflg);
2692 return smk_curacc_shm(shp, may);
2696 * smack_shm_shmctl - Smack access check for shm
2698 * @cmd: what it wants to do
2700 * Returns 0 if current has the requested access, error code otherwise
2702 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2715 may = MAY_READWRITE;
2720 * System level information.
2726 return smk_curacc_shm(shp, may);
2730 * smack_shm_shmat - Smack access for shmat
2733 * @shmflg: access requested
2735 * Returns 0 if current has the requested access, error code otherwise
2737 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2742 may = smack_flags_to_may(shmflg);
2743 return smk_curacc_shm(shp, may);
2747 * smack_of_sem - the smack pointer for the sem
2750 * Returns a pointer to the smack value
2752 static struct smack_known *smack_of_sem(struct sem_array *sma)
2754 return (struct smack_known *)sma->sem_perm.security;
2758 * smack_sem_alloc_security - Set the security blob for sem
2763 static int smack_sem_alloc_security(struct sem_array *sma)
2765 struct kern_ipc_perm *isp = &sma->sem_perm;
2766 struct smack_known *skp = smk_of_current();
2768 isp->security = skp;
2773 * smack_sem_free_security - Clear the security blob for sem
2776 * Clears the blob pointer
2778 static void smack_sem_free_security(struct sem_array *sma)
2780 struct kern_ipc_perm *isp = &sma->sem_perm;
2782 isp->security = NULL;
2786 * smk_curacc_sem : check if current has access on sem
2788 * @access : access requested
2790 * Returns 0 if current has the requested access, error code otherwise
2792 static int smk_curacc_sem(struct sem_array *sma, int access)
2794 struct smack_known *ssp = smack_of_sem(sma);
2795 struct smk_audit_info ad;
2799 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2800 ad.a.u.ipc_id = sma->sem_perm.id;
2802 rc = smk_curacc(ssp, access, &ad);
2803 rc = smk_bu_current("sem", ssp, access, rc);
2808 * smack_sem_associate - Smack access check for sem
2810 * @semflg: access requested
2812 * Returns 0 if current has the requested access, error code otherwise
2814 static int smack_sem_associate(struct sem_array *sma, int semflg)
2818 may = smack_flags_to_may(semflg);
2819 return smk_curacc_sem(sma, may);
2823 * smack_sem_shmctl - Smack access check for sem
2825 * @cmd: what it wants to do
2827 * Returns 0 if current has the requested access, error code otherwise
2829 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2847 may = MAY_READWRITE;
2852 * System level information
2859 return smk_curacc_sem(sma, may);
2863 * smack_sem_semop - Smack checks of semaphore operations
2869 * Treated as read and write in all cases.
2871 * Returns 0 if access is allowed, error code otherwise
2873 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2874 unsigned nsops, int alter)
2876 return smk_curacc_sem(sma, MAY_READWRITE);
2880 * smack_msg_alloc_security - Set the security blob for msg
2885 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2887 struct kern_ipc_perm *kisp = &msq->q_perm;
2888 struct smack_known *skp = smk_of_current();
2890 kisp->security = skp;
2895 * smack_msg_free_security - Clear the security blob for msg
2898 * Clears the blob pointer
2900 static void smack_msg_queue_free_security(struct msg_queue *msq)
2902 struct kern_ipc_perm *kisp = &msq->q_perm;
2904 kisp->security = NULL;
2908 * smack_of_msq - the smack pointer for the msq
2911 * Returns a pointer to the smack label entry
2913 static struct smack_known *smack_of_msq(struct msg_queue *msq)
2915 return (struct smack_known *)msq->q_perm.security;
2919 * smk_curacc_msq : helper to check if current has access on msq
2921 * @access : access requested
2923 * return 0 if current has access, error otherwise
2925 static int smk_curacc_msq(struct msg_queue *msq, int access)
2927 struct smack_known *msp = smack_of_msq(msq);
2928 struct smk_audit_info ad;
2932 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2933 ad.a.u.ipc_id = msq->q_perm.id;
2935 rc = smk_curacc(msp, access, &ad);
2936 rc = smk_bu_current("msq", msp, access, rc);
2941 * smack_msg_queue_associate - Smack access check for msg_queue
2943 * @msqflg: access requested
2945 * Returns 0 if current has the requested access, error code otherwise
2947 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2951 may = smack_flags_to_may(msqflg);
2952 return smk_curacc_msq(msq, may);
2956 * smack_msg_queue_msgctl - Smack access check for msg_queue
2958 * @cmd: what it wants to do
2960 * Returns 0 if current has the requested access, error code otherwise
2962 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2973 may = MAY_READWRITE;
2978 * System level information
2985 return smk_curacc_msq(msq, may);
2989 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2992 * @msqflg: access requested
2994 * Returns 0 if current has the requested access, error code otherwise
2996 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
3001 may = smack_flags_to_may(msqflg);
3002 return smk_curacc_msq(msq, may);
3006 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3013 * Returns 0 if current has read and write access, error code otherwise
3015 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3016 struct task_struct *target, long type, int mode)
3018 return smk_curacc_msq(msq, MAY_READWRITE);
3022 * smack_ipc_permission - Smack access for ipc_permission()
3023 * @ipp: the object permissions
3024 * @flag: access requested
3026 * Returns 0 if current has read and write access, error code otherwise
3028 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3030 struct smack_known *iskp = ipp->security;
3031 int may = smack_flags_to_may(flag);
3032 struct smk_audit_info ad;
3036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3037 ad.a.u.ipc_id = ipp->id;
3039 rc = smk_curacc(iskp, may, &ad);
3040 rc = smk_bu_current("svipc", iskp, may, rc);
3045 * smack_ipc_getsecid - Extract smack security id
3046 * @ipp: the object permissions
3047 * @secid: where result will be saved
3049 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3051 struct smack_known *iskp = ipp->security;
3053 *secid = iskp->smk_secid;
3057 * smack_d_instantiate - Make sure the blob is correct on an inode
3058 * @opt_dentry: dentry where inode will be attached
3059 * @inode: the object
3061 * Set the inode's security blob if it hasn't been done already.
3063 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3065 struct super_block *sbp;
3066 struct superblock_smack *sbsp;
3067 struct inode_smack *isp;
3068 struct smack_known *skp;
3069 struct smack_known *ckp = smk_of_current();
3070 struct smack_known *final;
3071 char trattr[TRANS_TRUE_SIZE];
3079 isp = inode->i_security;
3081 mutex_lock(&isp->smk_lock);
3083 * If the inode is already instantiated
3084 * take the quick way out
3086 if (isp->smk_flags & SMK_INODE_INSTANT)
3090 sbsp = sbp->s_security;
3092 * We're going to use the superblock default label
3093 * if there's no label on the file.
3095 final = sbsp->smk_default;
3098 * If this is the root inode the superblock
3099 * may be in the process of initialization.
3100 * If that is the case use the root value out
3101 * of the superblock.
3103 if (opt_dentry->d_parent == opt_dentry) {
3104 switch (sbp->s_magic) {
3105 case CGROUP_SUPER_MAGIC:
3107 * The cgroup filesystem is never mounted,
3108 * so there's no opportunity to set the mount
3111 sbsp->smk_root = &smack_known_star;
3112 sbsp->smk_default = &smack_known_star;
3113 isp->smk_inode = sbsp->smk_root;
3117 * What about shmem/tmpfs anonymous files with dentry
3118 * obtained from d_alloc_pseudo()?
3120 isp->smk_inode = smk_of_current();
3123 isp->smk_inode = sbsp->smk_root;
3126 isp->smk_flags |= SMK_INODE_INSTANT;
3131 * This is pretty hackish.
3132 * Casey says that we shouldn't have to do
3133 * file system specific code, but it does help
3134 * with keeping it simple.
3136 switch (sbp->s_magic) {
3140 case CGROUP_SUPER_MAGIC:
3142 * Casey says that it's a little embarrassing
3143 * that the smack file system doesn't do
3144 * extended attributes.
3146 * Casey says pipes are easy (?)
3148 * Socket access is controlled by the socket
3149 * structures associated with the task involved.
3151 * Cgroupfs is special
3153 final = &smack_known_star;
3155 case DEVPTS_SUPER_MAGIC:
3157 * devpts seems content with the label of the task.
3158 * Programs that change smack have to treat the
3163 case PROC_SUPER_MAGIC:
3165 * Casey says procfs appears not to care.
3166 * The superblock default suffices.
3171 * Device labels should come from the filesystem,
3172 * but watch out, because they're volitile,
3173 * getting recreated on every reboot.
3175 final = &smack_known_star;
3179 * If a smack value has been set we want to use it,
3180 * but since tmpfs isn't giving us the opportunity
3181 * to set mount options simulate setting the
3182 * superblock default.
3186 * This isn't an understood special case.
3187 * Get the value from the xattr.
3191 * UNIX domain sockets use lower level socket data.
3193 if (S_ISSOCK(inode->i_mode)) {
3194 final = &smack_known_star;
3198 * No xattr support means, alas, no SMACK label.
3199 * Use the aforeapplied default.
3200 * It would be curious if the label of the task
3201 * does not match that assigned.
3203 if (inode->i_op->getxattr == NULL)
3206 * Get the dentry for xattr.
3208 dp = dget(opt_dentry);
3209 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3214 * Transmuting directory
3216 if (S_ISDIR(inode->i_mode)) {
3218 * If this is a new directory and the label was
3219 * transmuted when the inode was initialized
3220 * set the transmute attribute on the directory
3221 * and mark the inode.
3223 * If there is a transmute attribute on the
3224 * directory mark the inode.
3226 if (isp->smk_flags & SMK_INODE_CHANGED) {
3227 isp->smk_flags &= ~SMK_INODE_CHANGED;
3228 rc = inode->i_op->setxattr(dp,
3229 XATTR_NAME_SMACKTRANSMUTE,
3230 TRANS_TRUE, TRANS_TRUE_SIZE,
3233 rc = inode->i_op->getxattr(dp,
3234 XATTR_NAME_SMACKTRANSMUTE, trattr,
3236 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3237 TRANS_TRUE_SIZE) != 0)
3241 transflag = SMK_INODE_TRANSMUTE;
3244 * Don't let the exec or mmap label be "*" or "@".
3246 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3247 if (skp == &smack_known_star || skp == &smack_known_web)
3249 isp->smk_task = skp;
3250 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3251 if (skp == &smack_known_star || skp == &smack_known_web)
3253 isp->smk_mmap = skp;
3260 isp->smk_inode = ckp;
3262 isp->smk_inode = final;
3264 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3267 mutex_unlock(&isp->smk_lock);
3272 * smack_getprocattr - Smack process attribute access
3273 * @p: the object task
3274 * @name: the name of the attribute in /proc/.../attr
3275 * @value: where to put the result
3277 * Places a copy of the task Smack into value
3279 * Returns the length of the smack label or an error code
3281 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3283 struct smack_known *skp = smk_of_task_struct(p);
3287 if (strcmp(name, "current") != 0)
3290 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3300 * smack_setprocattr - Smack process attribute setting
3301 * @p: the object task
3302 * @name: the name of the attribute in /proc/.../attr
3303 * @value: the value to set
3304 * @size: the size of the value
3306 * Sets the Smack value of the task. Only setting self
3307 * is permitted and only with privilege
3309 * Returns the length of the smack label or an error code
3311 static int smack_setprocattr(struct task_struct *p, char *name,
3312 void *value, size_t size)
3314 struct task_smack *tsp;
3316 struct smack_known *skp;
3319 * Changing another process' Smack value is too dangerous
3320 * and supports no sane use case.
3325 if (!smack_privileged(CAP_MAC_ADMIN))
3328 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3331 if (strcmp(name, "current") != 0)
3334 skp = smk_import_entry(value, size);
3339 * No process is ever allowed the web ("@") label.
3341 if (skp == &smack_known_web)
3344 new = prepare_creds();
3348 tsp = new->security;
3349 tsp->smk_task = skp;
3356 * smack_unix_stream_connect - Smack access on UDS
3358 * @other: the other sock
3361 * Return 0 if a subject with the smack of sock could access
3362 * an object with the smack of other, otherwise an error code
3364 static int smack_unix_stream_connect(struct sock *sock,
3365 struct sock *other, struct sock *newsk)
3367 struct smack_known *skp;
3368 struct smack_known *okp;
3369 struct socket_smack *ssp = sock->sk_security;
3370 struct socket_smack *osp = other->sk_security;
3371 struct socket_smack *nsp = newsk->sk_security;
3372 struct smk_audit_info ad;
3375 struct lsm_network_audit net;
3378 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3382 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3383 smk_ad_setfield_u_net_sk(&ad, other);
3385 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3386 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3390 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3391 rc = smk_bu_note("UDS connect", okp, skp,
3397 * Cross reference the peer labels for SO_PEERSEC.
3400 nsp->smk_packet = ssp->smk_out;
3401 ssp->smk_packet = osp->smk_out;
3408 * smack_unix_may_send - Smack access on UDS
3410 * @other: the other socket
3412 * Return 0 if a subject with the smack of sock could access
3413 * an object with the smack of other, otherwise an error code
3415 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3417 struct socket_smack *ssp = sock->sk->sk_security;
3418 struct socket_smack *osp = other->sk->sk_security;
3419 struct smk_audit_info ad;
3423 struct lsm_network_audit net;
3425 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3426 smk_ad_setfield_u_net_sk(&ad, other->sk);
3429 if (smack_privileged(CAP_MAC_OVERRIDE))
3432 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3433 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3438 * smack_socket_sendmsg - Smack check based on destination host
3441 * @size: the size of the message
3443 * Return 0 if the current subject can write to the destination host.
3444 * For IPv4 this is only a question if the destination is a single label host.
3445 * For IPv6 this is a check against the label of the port.
3447 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3450 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3451 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3452 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3453 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
3457 * Perfectly reasonable for this to be NULL
3462 switch (sip->sin_family) {
3464 rc = smack_netlabel_send(sock->sk, sip);
3467 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3468 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3469 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
3476 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3477 * @sap: netlabel secattr
3478 * @ssp: socket security information
3480 * Returns a pointer to a Smack label entry found on the label list.
3482 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3483 struct socket_smack *ssp)
3485 struct smack_known *skp;
3490 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3492 * Looks like a CIPSO packet.
3493 * If there are flags but no level netlabel isn't
3494 * behaving the way we expect it to.
3496 * Look it up in the label table
3497 * Without guidance regarding the smack value
3498 * for the packet fall back on the network
3502 list_for_each_entry(skp, &smack_known_list, list) {
3503 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3506 * Compare the catsets. Use the netlbl APIs.
3508 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3509 if ((skp->smk_netlabel.flags &
3510 NETLBL_SECATTR_MLS_CAT) == 0)
3514 for (acat = -1, kcat = -1; acat == kcat; ) {
3515 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3517 kcat = netlbl_catmap_walk(
3518 skp->smk_netlabel.attr.mls.cat,
3520 if (acat < 0 || kcat < 0)
3533 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3534 return &smack_known_web;
3535 return &smack_known_star;
3537 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3539 * Looks like a fallback, which gives us a secid.
3541 skp = smack_from_secid(sap->attr.secid);
3543 * This has got to be a bug because it is
3544 * impossible to specify a fallback without
3545 * specifying the label, which will ensure
3546 * it has a secid, and the only way to get a
3547 * secid is from a fallback.
3549 BUG_ON(skp == NULL);
3553 * Without guidance regarding the smack value
3554 * for the packet fall back on the network
3557 return smack_net_ambient;
3560 #if IS_ENABLED(CONFIG_IPV6)
3561 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3565 int proto = -EINVAL;
3566 struct ipv6hdr _ipv6h;
3567 struct ipv6hdr *ip6;
3569 struct tcphdr _tcph, *th;
3570 struct udphdr _udph, *uh;
3571 struct dccp_hdr _dccph, *dh;
3575 offset = skb_network_offset(skb);
3576 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3579 sip->sin6_addr = ip6->saddr;
3581 nexthdr = ip6->nexthdr;
3582 offset += sizeof(_ipv6h);
3583 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3590 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3592 sip->sin6_port = th->source;
3595 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3597 sip->sin6_port = uh->source;
3600 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3602 sip->sin6_port = dh->dccph_sport;
3607 #endif /* CONFIG_IPV6 */
3610 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3614 * Returns 0 if the packet should be delivered, an error code otherwise
3616 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3618 struct netlbl_lsm_secattr secattr;
3619 struct socket_smack *ssp = sk->sk_security;
3620 struct smack_known *skp = NULL;
3622 struct smk_audit_info ad;
3624 struct lsm_network_audit net;
3626 #if IS_ENABLED(CONFIG_IPV6)
3627 struct sockaddr_in6 sadd;
3629 #endif /* CONFIG_IPV6 */
3631 switch (sk->sk_family) {
3633 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3635 * If there is a secmark use it rather than the CIPSO label.
3636 * If there is no secmark fall back to CIPSO.
3637 * The secmark is assumed to reflect policy better.
3639 if (skb && skb->secmark != 0) {
3640 skp = smack_from_secid(skb->secmark);
3643 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3645 * Translate what netlabel gave us.
3647 netlbl_secattr_init(&secattr);
3649 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3651 skp = smack_from_secattr(&secattr, ssp);
3653 skp = smack_net_ambient;
3655 netlbl_secattr_destroy(&secattr);
3657 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3661 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3662 ad.a.u.net->family = sk->sk_family;
3663 ad.a.u.net->netif = skb->skb_iif;
3664 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3667 * Receiving a packet requires that the other end
3668 * be able to write here. Read access is not required.
3669 * This is the simplist possible security model
3672 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3673 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3676 netlbl_skbuff_err(skb, rc, 0);
3678 #if IS_ENABLED(CONFIG_IPV6)
3680 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3681 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3683 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3684 if (skb && skb->secmark != 0)
3685 skp = smack_from_secid(skb->secmark);
3687 skp = smack_net_ambient;
3689 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3690 ad.a.u.net->family = sk->sk_family;
3691 ad.a.u.net->netif = skb->skb_iif;
3692 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3693 #endif /* CONFIG_AUDIT */
3694 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3695 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3697 #else /* CONFIG_SECURITY_SMACK_NETFILTER */
3698 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3699 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3701 #endif /* CONFIG_IPV6 */
3708 * smack_socket_getpeersec_stream - pull in packet label
3710 * @optval: user's destination
3711 * @optlen: size thereof
3714 * returns zero on success, an error code otherwise
3716 static int smack_socket_getpeersec_stream(struct socket *sock,
3717 char __user *optval,
3718 int __user *optlen, unsigned len)
3720 struct socket_smack *ssp;
3725 ssp = sock->sk->sk_security;
3726 if (ssp->smk_packet != NULL) {
3727 rcp = ssp->smk_packet->smk_known;
3728 slen = strlen(rcp) + 1;
3733 else if (copy_to_user(optval, rcp, slen) != 0)
3736 if (put_user(slen, optlen) != 0)
3744 * smack_socket_getpeersec_dgram - pull in packet label
3745 * @sock: the peer socket
3747 * @secid: pointer to where to put the secid of the packet
3749 * Sets the netlabel socket state on sk from parent
3751 static int smack_socket_getpeersec_dgram(struct socket *sock,
3752 struct sk_buff *skb, u32 *secid)
3755 struct netlbl_lsm_secattr secattr;
3756 struct socket_smack *ssp = NULL;
3757 struct smack_known *skp;
3758 int family = PF_UNSPEC;
3759 u32 s = 0; /* 0 is the invalid secid */
3763 if (skb->protocol == htons(ETH_P_IP))
3765 #if IS_ENABLED(CONFIG_IPV6)
3766 else if (skb->protocol == htons(ETH_P_IPV6))
3768 #endif /* CONFIG_IPV6 */
3770 if (family == PF_UNSPEC && sock != NULL)
3771 family = sock->sk->sk_family;
3775 ssp = sock->sk->sk_security;
3776 s = ssp->smk_out->smk_secid;
3779 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3785 * Translate what netlabel gave us.
3787 if (sock != NULL && sock->sk != NULL)
3788 ssp = sock->sk->sk_security;
3789 netlbl_secattr_init(&secattr);
3790 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3792 skp = smack_from_secattr(&secattr, ssp);
3795 netlbl_secattr_destroy(&secattr);
3797 #if IS_ENABLED(CONFIG_IPV6)
3799 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3801 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3803 #endif /* CONFIG_IPV6 */
3812 * smack_sock_graft - Initialize a newly created socket with an existing sock
3814 * @parent: parent socket
3816 * Set the smk_{in,out} state of an existing sock based on the process that
3817 * is creating the new socket.
3819 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3821 struct socket_smack *ssp;
3822 struct smack_known *skp = smk_of_current();
3825 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3828 ssp = sk->sk_security;
3831 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3835 * smack_inet_conn_request - Smack access check on connect
3836 * @sk: socket involved
3840 * Returns 0 if a task with the packet label could write to
3841 * the socket, otherwise an error code
3843 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3844 struct request_sock *req)
3846 u16 family = sk->sk_family;
3847 struct smack_known *skp;
3848 struct socket_smack *ssp = sk->sk_security;
3849 struct netlbl_lsm_secattr secattr;
3850 struct sockaddr_in addr;
3852 struct smack_known *hskp;
3854 struct smk_audit_info ad;
3856 struct lsm_network_audit net;
3859 #if IS_ENABLED(CONFIG_IPV6)
3860 if (family == PF_INET6) {
3862 * Handle mapped IPv4 packets arriving
3863 * via IPv6 sockets. Don't set up netlabel
3864 * processing on IPv6.
3866 if (skb->protocol == htons(ETH_P_IP))
3871 #endif /* CONFIG_IPV6 */
3873 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3875 * If there is a secmark use it rather than the CIPSO label.
3876 * If there is no secmark fall back to CIPSO.
3877 * The secmark is assumed to reflect policy better.
3879 if (skb && skb->secmark != 0) {
3880 skp = smack_from_secid(skb->secmark);
3883 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3885 netlbl_secattr_init(&secattr);
3886 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3888 skp = smack_from_secattr(&secattr, ssp);
3890 skp = &smack_known_huh;
3891 netlbl_secattr_destroy(&secattr);
3893 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3898 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3899 ad.a.u.net->family = family;
3900 ad.a.u.net->netif = skb->skb_iif;
3901 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3904 * Receiving a packet requires that the other end be able to write
3905 * here. Read access is not required.
3907 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3908 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
3913 * Save the peer's label in the request_sock so we can later setup
3914 * smk_packet in the child socket so that SO_PEERCRED can report it.
3916 req->peer_secid = skp->smk_secid;
3919 * We need to decide if we want to label the incoming connection here
3920 * if we do we only need to label the request_sock and the stack will
3921 * propagate the wire-label to the sock when it is created.
3924 addr.sin_addr.s_addr = hdr->saddr;
3926 hskp = smack_host_label(&addr);
3930 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3932 netlbl_req_delattr(req);
3938 * smack_inet_csk_clone - Copy the connection information to the new socket
3939 * @sk: the new socket
3940 * @req: the connection's request_sock
3942 * Transfer the connection's peer label to the newly created socket.
3944 static void smack_inet_csk_clone(struct sock *sk,
3945 const struct request_sock *req)
3947 struct socket_smack *ssp = sk->sk_security;
3948 struct smack_known *skp;
3950 if (req->peer_secid != 0) {
3951 skp = smack_from_secid(req->peer_secid);
3952 ssp->smk_packet = skp;
3954 ssp->smk_packet = NULL;
3958 * Key management security hooks
3960 * Casey has not tested key support very heavily.
3961 * The permission check is most likely too restrictive.
3962 * If you care about keys please have a look.
3967 * smack_key_alloc - Set the key security blob
3969 * @cred: the credentials to use
3972 * No allocation required
3976 static int smack_key_alloc(struct key *key, const struct cred *cred,
3977 unsigned long flags)
3979 struct smack_known *skp = smk_of_task(cred->security);
3981 key->security = skp;
3986 * smack_key_free - Clear the key security blob
3989 * Clear the blob pointer
3991 static void smack_key_free(struct key *key)
3993 key->security = NULL;
3997 * smack_key_permission - Smack access on a key
3998 * @key_ref: gets to the object
3999 * @cred: the credentials to use
4000 * @perm: requested key permissions
4002 * Return 0 if the task has read and write to the object,
4003 * an error code otherwise
4005 static int smack_key_permission(key_ref_t key_ref,
4006 const struct cred *cred, unsigned perm)
4009 struct smk_audit_info ad;
4010 struct smack_known *tkp = smk_of_task(cred->security);
4014 keyp = key_ref_to_ptr(key_ref);
4018 * If the key hasn't been initialized give it access so that
4021 if (keyp->security == NULL)
4024 * This should not occur
4029 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4030 ad.a.u.key_struct.key = keyp->serial;
4031 ad.a.u.key_struct.key_desc = keyp->description;
4033 if (perm & KEY_NEED_READ)
4035 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4036 request = MAY_WRITE;
4037 rc = smk_access(tkp, keyp->security, request, &ad);
4038 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4043 * smack_key_getsecurity - Smack label tagging the key
4044 * @key points to the key to be queried
4045 * @_buffer points to a pointer that should be set to point to the
4046 * resulting string (if no label or an error occurs).
4047 * Return the length of the string (including terminating NUL) or -ve if
4049 * May also return 0 (and a NULL buffer pointer) if there is no label.
4051 static int smack_key_getsecurity(struct key *key, char **_buffer)
4053 struct smack_known *skp = key->security;
4057 if (key->security == NULL) {
4062 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4065 length = strlen(copy) + 1;
4071 #endif /* CONFIG_KEYS */
4076 * Audit requires a unique representation of each Smack specific
4077 * rule. This unique representation is used to distinguish the
4078 * object to be audited from remaining kernel objects and also
4079 * works as a glue between the audit hooks.
4081 * Since repository entries are added but never deleted, we'll use
4082 * the smack_known label address related to the given audit rule as
4083 * the needed unique representation. This also better fits the smack
4084 * model where nearly everything is a label.
4089 * smack_audit_rule_init - Initialize a smack audit rule
4090 * @field: audit rule fields given from user-space (audit.h)
4091 * @op: required testing operator (=, !=, >, <, ...)
4092 * @rulestr: smack label to be audited
4093 * @vrule: pointer to save our own audit rule representation
4095 * Prepare to audit cases where (@field @op @rulestr) is true.
4096 * The label to be audited is created if necessay.
4098 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4100 struct smack_known *skp;
4101 char **rule = (char **)vrule;
4104 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4107 if (op != Audit_equal && op != Audit_not_equal)
4110 skp = smk_import_entry(rulestr, 0);
4112 *rule = skp->smk_known;
4118 * smack_audit_rule_known - Distinguish Smack audit rules
4119 * @krule: rule of interest, in Audit kernel representation format
4121 * This is used to filter Smack rules from remaining Audit ones.
4122 * If it's proved that this rule belongs to us, the
4123 * audit_rule_match hook will be called to do the final judgement.
4125 static int smack_audit_rule_known(struct audit_krule *krule)
4127 struct audit_field *f;
4130 for (i = 0; i < krule->field_count; i++) {
4131 f = &krule->fields[i];
4133 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4141 * smack_audit_rule_match - Audit given object ?
4142 * @secid: security id for identifying the object to test
4143 * @field: audit rule flags given from user-space
4144 * @op: required testing operator
4145 * @vrule: smack internal rule presentation
4146 * @actx: audit context associated with the check
4148 * The core Audit hook. It's used to take the decision of
4149 * whether to audit or not to audit a given object.
4151 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4152 struct audit_context *actx)
4154 struct smack_known *skp;
4157 if (unlikely(!rule)) {
4158 WARN_ONCE(1, "Smack: missing rule\n");
4162 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4165 skp = smack_from_secid(secid);
4168 * No need to do string comparisons. If a match occurs,
4169 * both pointers will point to the same smack_known
4172 if (op == Audit_equal)
4173 return (rule == skp->smk_known);
4174 if (op == Audit_not_equal)
4175 return (rule != skp->smk_known);
4181 * smack_audit_rule_free - free smack rule representation
4182 * @vrule: rule to be freed.
4184 * No memory was allocated.
4186 static void smack_audit_rule_free(void *vrule)
4191 #endif /* CONFIG_AUDIT */
4194 * smack_ismaclabel - check if xattr @name references a smack MAC label
4195 * @name: Full xattr name to check.
4197 static int smack_ismaclabel(const char *name)
4199 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4204 * smack_secid_to_secctx - return the smack label for a secid
4205 * @secid: incoming integer
4206 * @secdata: destination
4207 * @seclen: how long it is
4209 * Exists for networking code.
4211 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4213 struct smack_known *skp = smack_from_secid(secid);
4216 *secdata = skp->smk_known;
4217 *seclen = strlen(skp->smk_known);
4222 * smack_secctx_to_secid - return the secid for a smack label
4223 * @secdata: smack label
4224 * @seclen: how long result is
4225 * @secid: outgoing integer
4227 * Exists for audit and networking code.
4229 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4231 struct smack_known *skp = smk_find_entry(secdata);
4234 *secid = skp->smk_secid;
4241 * smack_release_secctx - don't do anything.
4245 * Exists to make sure nothing gets done, and properly
4247 static void smack_release_secctx(char *secdata, u32 seclen)
4251 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4253 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4256 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4258 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4261 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4264 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4272 struct security_operations smack_ops = {
4275 .ptrace_access_check = smack_ptrace_access_check,
4276 .ptrace_traceme = smack_ptrace_traceme,
4277 .syslog = smack_syslog,
4279 .sb_alloc_security = smack_sb_alloc_security,
4280 .sb_free_security = smack_sb_free_security,
4281 .sb_copy_data = smack_sb_copy_data,
4282 .sb_kern_mount = smack_sb_kern_mount,
4283 .sb_statfs = smack_sb_statfs,
4285 .bprm_set_creds = smack_bprm_set_creds,
4286 .bprm_committing_creds = smack_bprm_committing_creds,
4287 .bprm_secureexec = smack_bprm_secureexec,
4289 .inode_alloc_security = smack_inode_alloc_security,
4290 .inode_free_security = smack_inode_free_security,
4291 .inode_init_security = smack_inode_init_security,
4292 .inode_link = smack_inode_link,
4293 .inode_unlink = smack_inode_unlink,
4294 .inode_rmdir = smack_inode_rmdir,
4295 .inode_rename = smack_inode_rename,
4296 .inode_permission = smack_inode_permission,
4297 .inode_setattr = smack_inode_setattr,
4298 .inode_getattr = smack_inode_getattr,
4299 .inode_setxattr = smack_inode_setxattr,
4300 .inode_post_setxattr = smack_inode_post_setxattr,
4301 .inode_getxattr = smack_inode_getxattr,
4302 .inode_removexattr = smack_inode_removexattr,
4303 .inode_getsecurity = smack_inode_getsecurity,
4304 .inode_setsecurity = smack_inode_setsecurity,
4305 .inode_listsecurity = smack_inode_listsecurity,
4306 .inode_getsecid = smack_inode_getsecid,
4308 .file_permission = smack_file_permission,
4309 .file_alloc_security = smack_file_alloc_security,
4310 .file_free_security = smack_file_free_security,
4311 .file_ioctl = smack_file_ioctl,
4312 .file_lock = smack_file_lock,
4313 .file_fcntl = smack_file_fcntl,
4314 .mmap_file = smack_mmap_file,
4315 .mmap_addr = cap_mmap_addr,
4316 .file_set_fowner = smack_file_set_fowner,
4317 .file_send_sigiotask = smack_file_send_sigiotask,
4318 .file_receive = smack_file_receive,
4320 .file_open = smack_file_open,
4322 .cred_alloc_blank = smack_cred_alloc_blank,
4323 .cred_free = smack_cred_free,
4324 .cred_prepare = smack_cred_prepare,
4325 .cred_transfer = smack_cred_transfer,
4326 .kernel_act_as = smack_kernel_act_as,
4327 .kernel_create_files_as = smack_kernel_create_files_as,
4328 .task_setpgid = smack_task_setpgid,
4329 .task_getpgid = smack_task_getpgid,
4330 .task_getsid = smack_task_getsid,
4331 .task_getsecid = smack_task_getsecid,
4332 .task_setnice = smack_task_setnice,
4333 .task_setioprio = smack_task_setioprio,
4334 .task_getioprio = smack_task_getioprio,
4335 .task_setscheduler = smack_task_setscheduler,
4336 .task_getscheduler = smack_task_getscheduler,
4337 .task_movememory = smack_task_movememory,
4338 .task_kill = smack_task_kill,
4339 .task_wait = smack_task_wait,
4340 .task_to_inode = smack_task_to_inode,
4342 .ipc_permission = smack_ipc_permission,
4343 .ipc_getsecid = smack_ipc_getsecid,
4345 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4346 .msg_msg_free_security = smack_msg_msg_free_security,
4348 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4349 .msg_queue_free_security = smack_msg_queue_free_security,
4350 .msg_queue_associate = smack_msg_queue_associate,
4351 .msg_queue_msgctl = smack_msg_queue_msgctl,
4352 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4353 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4355 .shm_alloc_security = smack_shm_alloc_security,
4356 .shm_free_security = smack_shm_free_security,
4357 .shm_associate = smack_shm_associate,
4358 .shm_shmctl = smack_shm_shmctl,
4359 .shm_shmat = smack_shm_shmat,
4361 .sem_alloc_security = smack_sem_alloc_security,
4362 .sem_free_security = smack_sem_free_security,
4363 .sem_associate = smack_sem_associate,
4364 .sem_semctl = smack_sem_semctl,
4365 .sem_semop = smack_sem_semop,
4367 .d_instantiate = smack_d_instantiate,
4369 .getprocattr = smack_getprocattr,
4370 .setprocattr = smack_setprocattr,
4372 .unix_stream_connect = smack_unix_stream_connect,
4373 .unix_may_send = smack_unix_may_send,
4375 .socket_post_create = smack_socket_post_create,
4376 #ifndef CONFIG_SECURITY_SMACK_NETFILTER
4377 .socket_bind = smack_socket_bind,
4378 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4379 .socket_connect = smack_socket_connect,
4380 .socket_sendmsg = smack_socket_sendmsg,
4381 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4382 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4383 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4384 .sk_alloc_security = smack_sk_alloc_security,
4385 .sk_free_security = smack_sk_free_security,
4386 .sock_graft = smack_sock_graft,
4387 .inet_conn_request = smack_inet_conn_request,
4388 .inet_csk_clone = smack_inet_csk_clone,
4390 /* key management security hooks */
4392 .key_alloc = smack_key_alloc,
4393 .key_free = smack_key_free,
4394 .key_permission = smack_key_permission,
4395 .key_getsecurity = smack_key_getsecurity,
4396 #endif /* CONFIG_KEYS */
4400 .audit_rule_init = smack_audit_rule_init,
4401 .audit_rule_known = smack_audit_rule_known,
4402 .audit_rule_match = smack_audit_rule_match,
4403 .audit_rule_free = smack_audit_rule_free,
4404 #endif /* CONFIG_AUDIT */
4406 .ismaclabel = smack_ismaclabel,
4407 .secid_to_secctx = smack_secid_to_secctx,
4408 .secctx_to_secid = smack_secctx_to_secid,
4409 .release_secctx = smack_release_secctx,
4410 .inode_notifysecctx = smack_inode_notifysecctx,
4411 .inode_setsecctx = smack_inode_setsecctx,
4412 .inode_getsecctx = smack_inode_getsecctx,
4416 static __init void init_smack_known_list(void)
4419 * Initialize rule list locks
4421 mutex_init(&smack_known_huh.smk_rules_lock);
4422 mutex_init(&smack_known_hat.smk_rules_lock);
4423 mutex_init(&smack_known_floor.smk_rules_lock);
4424 mutex_init(&smack_known_star.smk_rules_lock);
4425 mutex_init(&smack_known_invalid.smk_rules_lock);
4426 mutex_init(&smack_known_web.smk_rules_lock);
4428 * Initialize rule lists
4430 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4431 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4432 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4433 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4434 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4435 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4437 * Create the known labels list
4439 smk_insert_entry(&smack_known_huh);
4440 smk_insert_entry(&smack_known_hat);
4441 smk_insert_entry(&smack_known_star);
4442 smk_insert_entry(&smack_known_floor);
4443 smk_insert_entry(&smack_known_invalid);
4444 smk_insert_entry(&smack_known_web);
4448 * smack_init - initialize the smack system
4452 static __init int smack_init(void)
4455 struct task_smack *tsp;
4457 if (!security_module_enable(&smack_ops))
4462 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4463 if (!smack_inode_cache)
4466 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4469 kmem_cache_destroy(smack_inode_cache);
4473 printk(KERN_INFO "Smack: Initializing.\n");
4476 * Set the security state for the initial task.
4478 cred = (struct cred *) current->cred;
4479 cred->security = tsp;
4481 /* initialize the smack_known_list */
4482 init_smack_known_list();
4487 if (register_security(&smack_ops))
4488 panic("smack: Unable to register with kernel.\n");
4494 * Smack requires early initialization in order to label
4495 * all processes and objects when they are created.
4497 security_initcall(smack_init);