1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
14 * Copyright (C) 2010 Nokia Corporation
15 * Copyright (C) 2011 Intel Corporation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
23 #include <asm/ioctls.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/dccp.h>
28 #include <linux/icmpv6.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
38 #include <linux/msg.h>
39 #include <linux/shm.h>
40 #include <linux/binfmts.h>
41 #include <linux/parser.h>
42 #include <linux/fs_context.h>
43 #include <linux/fs_parser.h>
44 #include <linux/watch_queue.h>
45 #include <linux/io_uring.h>
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 #ifdef SMACK_IPV6_PORT_LABELING
56 static DEFINE_MUTEX(smack_ipv6_lock);
57 static LIST_HEAD(smk_ipv6_port_list);
59 struct kmem_cache *smack_rule_cache;
60 int smack_enabled __initdata;
62 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
67 } smk_mount_opts[] = {
68 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
69 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
73 static int match_opt_prefix(char *s, int l, char **arg)
77 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
78 size_t len = smk_mount_opts[i].len;
79 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
81 if (len == l || s[len] != '=')
84 return smk_mount_opts[i].opt;
89 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
90 static char *smk_bu_mess[] = {
91 "Bringup Error", /* Unused */
92 "Bringup", /* SMACK_BRINGUP_ALLOW */
93 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
94 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
97 static void smk_bu_mode(int mode, char *s)
103 if (mode & MAY_WRITE)
107 if (mode & MAY_APPEND)
109 if (mode & MAY_TRANSMUTE)
119 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
120 static int smk_bu_note(char *note, struct smack_known *sskp,
121 struct smack_known *oskp, int mode, int rc)
123 char acc[SMK_NUM_ACCESS_TYPE + 1];
127 if (rc > SMACK_UNCONFINED_OBJECT)
130 smk_bu_mode(mode, acc);
131 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
132 sskp->smk_known, oskp->smk_known, acc, note);
136 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
139 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
140 static int smk_bu_current(char *note, struct smack_known *oskp,
143 struct task_smack *tsp = smack_cred(current_cred());
144 char acc[SMK_NUM_ACCESS_TYPE + 1];
148 if (rc > SMACK_UNCONFINED_OBJECT)
151 smk_bu_mode(mode, acc);
152 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
153 tsp->smk_task->smk_known, oskp->smk_known,
154 acc, current->comm, note);
158 #define smk_bu_current(note, oskp, mode, RC) (RC)
161 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
162 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
164 struct task_smack *tsp = smack_cred(current_cred());
165 struct smack_known *smk_task = smk_of_task_struct_obj(otp);
166 char acc[SMK_NUM_ACCESS_TYPE + 1];
170 if (rc > SMACK_UNCONFINED_OBJECT)
173 smk_bu_mode(mode, acc);
174 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
175 tsp->smk_task->smk_known, smk_task->smk_known, acc,
176 current->comm, otp->comm);
180 #define smk_bu_task(otp, mode, RC) (RC)
183 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
184 static int smk_bu_inode(struct inode *inode, int mode, int rc)
186 struct task_smack *tsp = smack_cred(current_cred());
187 struct inode_smack *isp = smack_inode(inode);
188 char acc[SMK_NUM_ACCESS_TYPE + 1];
190 if (isp->smk_flags & SMK_INODE_IMPURE)
191 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
192 inode->i_sb->s_id, inode->i_ino, current->comm);
196 if (rc > SMACK_UNCONFINED_OBJECT)
198 if (rc == SMACK_UNCONFINED_SUBJECT &&
199 (mode & (MAY_WRITE | MAY_APPEND)))
200 isp->smk_flags |= SMK_INODE_IMPURE;
202 smk_bu_mode(mode, acc);
204 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
205 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
206 inode->i_sb->s_id, inode->i_ino, current->comm);
210 #define smk_bu_inode(inode, mode, RC) (RC)
213 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
214 static int smk_bu_file(struct file *file, int mode, int rc)
216 struct task_smack *tsp = smack_cred(current_cred());
217 struct smack_known *sskp = tsp->smk_task;
218 struct inode *inode = file_inode(file);
219 struct inode_smack *isp = smack_inode(inode);
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_file(file, mode, RC) (RC)
242 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
243 static int smk_bu_credfile(const struct cred *cred, struct file *file,
246 struct task_smack *tsp = smack_cred(cred);
247 struct smack_known *sskp = tsp->smk_task;
248 struct inode *inode = file_inode(file);
249 struct inode_smack *isp = smack_inode(inode);
250 char acc[SMK_NUM_ACCESS_TYPE + 1];
252 if (isp->smk_flags & SMK_INODE_IMPURE)
253 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
254 inode->i_sb->s_id, inode->i_ino, current->comm);
258 if (rc > SMACK_UNCONFINED_OBJECT)
261 smk_bu_mode(mode, acc);
262 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
263 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
264 inode->i_sb->s_id, inode->i_ino, file,
269 #define smk_bu_credfile(cred, file, mode, RC) (RC)
273 * smk_fetch - Fetch the smack label from a file.
274 * @name: type of the label (attribute)
275 * @ip: a pointer to the inode
276 * @dp: a pointer to the dentry
278 * Returns a pointer to the master list entry for the Smack label,
279 * NULL if there was no label to fetch, or an error code.
281 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
286 struct smack_known *skp = NULL;
288 if (!(ip->i_opflags & IOP_XATTR))
289 return ERR_PTR(-EOPNOTSUPP);
291 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
293 return ERR_PTR(-ENOMEM);
295 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
301 skp = smk_import_entry(buffer, rc);
309 * init_inode_smack - initialize an inode security blob
310 * @inode: inode to extract the info from
311 * @skp: a pointer to the Smack label entry to use in the blob
314 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
316 struct inode_smack *isp = smack_inode(inode);
318 isp->smk_inode = skp;
323 * init_task_smack - initialize a task security blob
324 * @tsp: blob to initialize
325 * @task: a pointer to the Smack label for the running task
326 * @forked: a pointer to the Smack label for the forked task
329 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
330 struct smack_known *forked)
332 tsp->smk_task = task;
333 tsp->smk_forked = forked;
334 INIT_LIST_HEAD(&tsp->smk_rules);
335 INIT_LIST_HEAD(&tsp->smk_relabel);
336 mutex_init(&tsp->smk_rules_lock);
340 * smk_copy_rules - copy a rule set
341 * @nhead: new rules header pointer
342 * @ohead: old rules header pointer
343 * @gfp: type of the memory for the allocation
345 * Returns 0 on success, -ENOMEM on error
347 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
350 struct smack_rule *nrp;
351 struct smack_rule *orp;
354 list_for_each_entry_rcu(orp, ohead, list) {
355 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
361 list_add_rcu(&nrp->list, nhead);
367 * smk_copy_relabel - copy smk_relabel labels list
368 * @nhead: new rules header pointer
369 * @ohead: old rules header pointer
370 * @gfp: type of the memory for the allocation
372 * Returns 0 on success, -ENOMEM on error
374 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
377 struct smack_known_list_elem *nklep;
378 struct smack_known_list_elem *oklep;
380 list_for_each_entry(oklep, ohead, list) {
381 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
383 smk_destroy_label_list(nhead);
386 nklep->smk_label = oklep->smk_label;
387 list_add(&nklep->list, nhead);
394 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
395 * @mode: input mode in form of PTRACE_MODE_*
397 * Returns a converted MAY_* mode usable by smack rules
399 static inline unsigned int smk_ptrace_mode(unsigned int mode)
401 if (mode & PTRACE_MODE_ATTACH)
402 return MAY_READWRITE;
403 if (mode & PTRACE_MODE_READ)
410 * smk_ptrace_rule_check - helper for ptrace access
411 * @tracer: tracer process
412 * @tracee_known: label entry of the process that's about to be traced
413 * @mode: ptrace attachment mode (PTRACE_MODE_*)
414 * @func: name of the function that called us, used for audit
416 * Returns 0 on access granted, -error on error
418 static int smk_ptrace_rule_check(struct task_struct *tracer,
419 struct smack_known *tracee_known,
420 unsigned int mode, const char *func)
423 struct smk_audit_info ad, *saip = NULL;
424 struct task_smack *tsp;
425 struct smack_known *tracer_known;
426 const struct cred *tracercred;
428 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
429 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
430 smk_ad_setfield_u_tsk(&ad, tracer);
435 tracercred = __task_cred(tracer);
436 tsp = smack_cred(tracercred);
437 tracer_known = smk_of_task(tsp);
439 if ((mode & PTRACE_MODE_ATTACH) &&
440 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
441 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
442 if (tracer_known->smk_known == tracee_known->smk_known)
444 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
446 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
452 smack_log(tracer_known->smk_known,
453 tracee_known->smk_known,
460 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
461 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
469 * We he, that is fun!
473 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
474 * @ctp: child task pointer
475 * @mode: ptrace attachment mode (PTRACE_MODE_*)
477 * Returns 0 if access is OK, an error code otherwise
479 * Do the capability checks.
481 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
483 struct smack_known *skp;
485 skp = smk_of_task_struct_obj(ctp);
487 return smk_ptrace_rule_check(current, skp, mode, __func__);
491 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
492 * @ptp: parent task pointer
494 * Returns 0 if access is OK, an error code otherwise
496 * Do the capability checks, and require PTRACE_MODE_ATTACH.
498 static int smack_ptrace_traceme(struct task_struct *ptp)
500 struct smack_known *skp;
502 skp = smk_of_task(smack_cred(current_cred()));
504 return smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
508 * smack_syslog - Smack approval on syslog
509 * @typefrom_file: unused
511 * Returns 0 on success, error code otherwise.
513 static int smack_syslog(int typefrom_file)
516 struct smack_known *skp = smk_of_current();
518 if (smack_privileged(CAP_MAC_OVERRIDE))
521 if (smack_syslog_label != NULL && smack_syslog_label != skp)
532 * smack_sb_alloc_security - allocate a superblock blob
533 * @sb: the superblock getting the blob
535 * Returns 0 on success or -ENOMEM on error.
537 static int smack_sb_alloc_security(struct super_block *sb)
539 struct superblock_smack *sbsp = smack_superblock(sb);
541 sbsp->smk_root = &smack_known_floor;
542 sbsp->smk_default = &smack_known_floor;
543 sbsp->smk_floor = &smack_known_floor;
544 sbsp->smk_hat = &smack_known_hat;
546 * SMK_SB_INITIALIZED will be zero from kzalloc.
552 struct smack_mnt_opts {
553 const char *fsdefault;
557 const char *fstransmute;
560 static void smack_free_mnt_opts(void *mnt_opts)
565 static int smack_add_opt(int token, const char *s, void **mnt_opts)
567 struct smack_mnt_opts *opts = *mnt_opts;
568 struct smack_known *skp;
571 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
579 skp = smk_import_entry(s, 0);
587 opts->fsdefault = skp->smk_known;
592 opts->fsfloor = skp->smk_known;
597 opts->fshat = skp->smk_known;
602 opts->fsroot = skp->smk_known;
604 case Opt_fstransmute:
605 if (opts->fstransmute)
607 opts->fstransmute = skp->smk_known;
613 pr_warn("Smack: duplicate mount options\n");
618 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
619 * @fc: The new filesystem context.
620 * @src_fc: The source filesystem context being duplicated.
622 * Returns 0 on success or -ENOMEM on error.
624 static int smack_fs_context_dup(struct fs_context *fc,
625 struct fs_context *src_fc)
627 struct smack_mnt_opts *dst, *src = src_fc->security;
632 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
637 dst->fsdefault = src->fsdefault;
638 dst->fsfloor = src->fsfloor;
639 dst->fshat = src->fshat;
640 dst->fsroot = src->fsroot;
641 dst->fstransmute = src->fstransmute;
646 static const struct fs_parameter_spec smack_fs_parameters[] = {
647 fsparam_string("smackfsdef", Opt_fsdefault),
648 fsparam_string("smackfsdefault", Opt_fsdefault),
649 fsparam_string("smackfsfloor", Opt_fsfloor),
650 fsparam_string("smackfshat", Opt_fshat),
651 fsparam_string("smackfsroot", Opt_fsroot),
652 fsparam_string("smackfstransmute", Opt_fstransmute),
657 * smack_fs_context_parse_param - Parse a single mount parameter
658 * @fc: The new filesystem context being constructed.
659 * @param: The parameter.
661 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
664 static int smack_fs_context_parse_param(struct fs_context *fc,
665 struct fs_parameter *param)
667 struct fs_parse_result result;
670 opt = fs_parse(fc, smack_fs_parameters, param, &result);
674 rc = smack_add_opt(opt, param->string, &fc->security);
676 param->string = NULL;
680 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
682 char *from = options, *to = options;
686 char *next = strchr(from, ',');
695 token = match_opt_prefix(from, len, &arg);
696 if (token != Opt_error) {
697 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
698 rc = smack_add_opt(token, arg, mnt_opts);
702 smack_free_mnt_opts(*mnt_opts);
707 if (!first) { // copy with preceding comma
712 memmove(to, from, len);
725 * smack_set_mnt_opts - set Smack specific mount options
726 * @sb: the file system superblock
727 * @mnt_opts: Smack mount options
728 * @kern_flags: mount option from kernel space or user space
729 * @set_kern_flags: where to store converted mount opts
731 * Returns 0 on success, an error code on failure
733 * Allow filesystems with binary mount data to explicitly set Smack mount
736 static int smack_set_mnt_opts(struct super_block *sb,
738 unsigned long kern_flags,
739 unsigned long *set_kern_flags)
741 struct dentry *root = sb->s_root;
742 struct inode *inode = d_backing_inode(root);
743 struct superblock_smack *sp = smack_superblock(sb);
744 struct inode_smack *isp;
745 struct smack_known *skp;
746 struct smack_mnt_opts *opts = mnt_opts;
747 bool transmute = false;
749 if (sp->smk_flags & SMK_SB_INITIALIZED)
752 if (!smack_privileged(CAP_MAC_ADMIN)) {
754 * Unprivileged mounts don't get to specify Smack values.
759 * Unprivileged mounts get root and default from the caller.
761 skp = smk_of_current();
763 sp->smk_default = skp;
765 * For a handful of fs types with no user-controlled
766 * backing store it's okay to trust security labels
767 * in the filesystem. The rest are untrusted.
769 if (sb->s_user_ns != &init_user_ns &&
770 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
771 sb->s_magic != RAMFS_MAGIC) {
773 sp->smk_flags |= SMK_SB_UNTRUSTED;
777 sp->smk_flags |= SMK_SB_INITIALIZED;
780 if (opts->fsdefault) {
781 skp = smk_import_entry(opts->fsdefault, 0);
784 sp->smk_default = skp;
787 skp = smk_import_entry(opts->fsfloor, 0);
793 skp = smk_import_entry(opts->fshat, 0);
799 skp = smk_import_entry(opts->fsroot, 0);
804 if (opts->fstransmute) {
805 skp = smk_import_entry(opts->fstransmute, 0);
814 * Initialize the root inode.
816 init_inode_smack(inode, sp->smk_root);
819 isp = smack_inode(inode);
820 isp->smk_flags |= SMK_INODE_TRANSMUTE;
827 * smack_sb_statfs - Smack check on statfs
828 * @dentry: identifies the file system in question
830 * Returns 0 if current can read the floor of the filesystem,
831 * and error code otherwise
833 static int smack_sb_statfs(struct dentry *dentry)
835 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
837 struct smk_audit_info ad;
839 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
840 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
842 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
843 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
852 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
853 * @bprm: the exec information
855 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
857 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
859 struct inode *inode = file_inode(bprm->file);
860 struct task_smack *bsp = smack_cred(bprm->cred);
861 struct inode_smack *isp;
862 struct superblock_smack *sbsp;
865 isp = smack_inode(inode);
866 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
869 sbsp = smack_superblock(inode->i_sb);
870 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
871 isp->smk_task != sbsp->smk_root)
874 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
875 struct task_struct *tracer;
879 tracer = ptrace_parent(current);
880 if (likely(tracer != NULL))
881 rc = smk_ptrace_rule_check(tracer,
890 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
893 bsp->smk_task = isp->smk_task;
894 bprm->per_clear |= PER_CLEAR_ON_SETID;
896 /* Decide if this is a secure exec. */
897 if (bsp->smk_task != bsp->smk_forked)
898 bprm->secureexec = 1;
908 * smack_inode_alloc_security - allocate an inode blob
909 * @inode: the inode in need of a blob
913 static int smack_inode_alloc_security(struct inode *inode)
915 struct smack_known *skp = smk_of_current();
917 init_inode_smack(inode, skp);
922 * smack_inode_init_security - copy out the smack from an inode
923 * @inode: the newly created inode
924 * @dir: containing directory object
926 * @name: where to put the attribute name
927 * @value: where to put the attribute value
928 * @len: where to put the length of the attribute
930 * Returns 0 if it all works out, -ENOMEM if there's no memory
932 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
933 const struct qstr *qstr, const char **name,
934 void **value, size_t *len)
936 struct inode_smack *issp = smack_inode(inode);
937 struct smack_known *skp = smk_of_current();
938 struct smack_known *isp = smk_of_inode(inode);
939 struct smack_known *dsp = smk_of_inode(dir);
943 *name = XATTR_SMACK_SUFFIX;
947 may = smk_access_entry(skp->smk_known, dsp->smk_known,
952 * If the access rule allows transmutation and
953 * the directory requests transmutation then
954 * by all means transmute.
955 * Mark the inode as changed.
957 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
958 smk_inode_transmutable(dir)) {
960 issp->smk_flags |= SMK_INODE_CHANGED;
963 *value = kstrdup(isp->smk_known, GFP_NOFS);
967 *len = strlen(isp->smk_known);
974 * smack_inode_link - Smack check on link
975 * @old_dentry: the existing object
977 * @new_dentry: the new object
979 * Returns 0 if access is permitted, an error code otherwise
981 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
982 struct dentry *new_dentry)
984 struct smack_known *isp;
985 struct smk_audit_info ad;
988 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
989 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
991 isp = smk_of_inode(d_backing_inode(old_dentry));
992 rc = smk_curacc(isp, MAY_WRITE, &ad);
993 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
995 if (rc == 0 && d_is_positive(new_dentry)) {
996 isp = smk_of_inode(d_backing_inode(new_dentry));
997 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
998 rc = smk_curacc(isp, MAY_WRITE, &ad);
999 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1006 * smack_inode_unlink - Smack check on inode deletion
1007 * @dir: containing directory object
1008 * @dentry: file to unlink
1010 * Returns 0 if current can write the containing directory
1011 * and the object, error code otherwise
1013 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1015 struct inode *ip = d_backing_inode(dentry);
1016 struct smk_audit_info ad;
1019 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1020 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1023 * You need write access to the thing you're unlinking
1025 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1026 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1029 * You also need write access to the containing directory
1031 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1032 smk_ad_setfield_u_fs_inode(&ad, dir);
1033 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1034 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1040 * smack_inode_rmdir - Smack check on directory deletion
1041 * @dir: containing directory object
1042 * @dentry: directory to unlink
1044 * Returns 0 if current can write the containing directory
1045 * and the directory, error code otherwise
1047 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1049 struct smk_audit_info ad;
1052 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1053 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1056 * You need write access to the thing you're removing
1058 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1059 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1062 * You also need write access to the containing directory
1064 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1065 smk_ad_setfield_u_fs_inode(&ad, dir);
1066 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1067 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1074 * smack_inode_rename - Smack check on rename
1075 * @old_inode: unused
1076 * @old_dentry: the old object
1077 * @new_inode: unused
1078 * @new_dentry: the new object
1080 * Read and write access is required on both the old and
1083 * Returns 0 if access is permitted, an error code otherwise
1085 static int smack_inode_rename(struct inode *old_inode,
1086 struct dentry *old_dentry,
1087 struct inode *new_inode,
1088 struct dentry *new_dentry)
1091 struct smack_known *isp;
1092 struct smk_audit_info ad;
1094 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1095 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1097 isp = smk_of_inode(d_backing_inode(old_dentry));
1098 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1099 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1101 if (rc == 0 && d_is_positive(new_dentry)) {
1102 isp = smk_of_inode(d_backing_inode(new_dentry));
1103 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1104 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1105 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1111 * smack_inode_permission - Smack version of permission()
1112 * @inode: the inode in question
1113 * @mask: the access requested
1115 * This is the important Smack hook.
1117 * Returns 0 if access is permitted, an error code otherwise
1119 static int smack_inode_permission(struct inode *inode, int mask)
1121 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
1122 struct smk_audit_info ad;
1123 int no_block = mask & MAY_NOT_BLOCK;
1126 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1128 * No permission to check. Existence test. Yup, it's there.
1133 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1134 if (smk_of_inode(inode) != sbsp->smk_root)
1138 /* May be droppable after audit */
1141 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1142 smk_ad_setfield_u_fs_inode(&ad, inode);
1143 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1144 rc = smk_bu_inode(inode, mask, rc);
1149 * smack_inode_setattr - Smack check for setting attributes
1150 * @dentry: the object
1151 * @iattr: for the force flag
1153 * Returns 0 if access is permitted, an error code otherwise
1155 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1157 struct smk_audit_info ad;
1161 * Need to allow for clearing the setuid bit.
1163 if (iattr->ia_valid & ATTR_FORCE)
1165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1166 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1168 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1169 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1174 * smack_inode_getattr - Smack check for getting attributes
1175 * @path: path to extract the info from
1177 * Returns 0 if access is permitted, an error code otherwise
1179 static int smack_inode_getattr(const struct path *path)
1181 struct smk_audit_info ad;
1182 struct inode *inode = d_backing_inode(path->dentry);
1185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1186 smk_ad_setfield_u_fs_path(&ad, *path);
1187 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1188 rc = smk_bu_inode(inode, MAY_READ, rc);
1193 * smack_inode_setxattr - Smack check for setting xattrs
1194 * @idmap: idmap of the mount
1195 * @dentry: the object
1196 * @name: name of the attribute
1197 * @value: value of the attribute
1198 * @size: size of the value
1201 * This protects the Smack attribute explicitly.
1203 * Returns 0 if access is permitted, an error code otherwise
1205 static int smack_inode_setxattr(struct mnt_idmap *idmap,
1206 struct dentry *dentry, const char *name,
1207 const void *value, size_t size, int flags)
1209 struct smk_audit_info ad;
1210 struct smack_known *skp;
1212 int check_import = 0;
1217 * Check label validity here so import won't fail in post_setxattr
1219 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1220 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1221 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1224 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1225 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1229 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1231 if (size != TRANS_TRUE_SIZE ||
1232 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1235 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1237 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1240 if (rc == 0 && check_import) {
1241 skp = size ? smk_import_entry(value, size) : NULL;
1244 else if (skp == NULL || (check_star &&
1245 (skp == &smack_known_star || skp == &smack_known_web)))
1249 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1250 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1253 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1254 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1261 * smack_inode_post_setxattr - Apply the Smack update approved above
1263 * @name: attribute name
1264 * @value: attribute value
1265 * @size: attribute size
1268 * Set the pointer in the inode blob to the entry found
1269 * in the master label list.
1271 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1272 const void *value, size_t size, int flags)
1274 struct smack_known *skp;
1275 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1277 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1278 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1282 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1283 skp = smk_import_entry(value, size);
1285 isp->smk_inode = skp;
1286 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1287 skp = smk_import_entry(value, size);
1289 isp->smk_task = skp;
1290 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1291 skp = smk_import_entry(value, size);
1293 isp->smk_mmap = skp;
1300 * smack_inode_getxattr - Smack check on getxattr
1301 * @dentry: the object
1304 * Returns 0 if access is permitted, an error code otherwise
1306 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1308 struct smk_audit_info ad;
1311 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1312 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1314 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1315 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1320 * smack_inode_removexattr - Smack check on removexattr
1321 * @idmap: idmap of the mount
1322 * @dentry: the object
1323 * @name: name of the attribute
1325 * Removing the Smack attribute requires CAP_MAC_ADMIN
1327 * Returns 0 if access is permitted, an error code otherwise
1329 static int smack_inode_removexattr(struct mnt_idmap *idmap,
1330 struct dentry *dentry, const char *name)
1332 struct inode_smack *isp;
1333 struct smk_audit_info ad;
1336 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1337 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1338 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1339 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1340 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1341 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1342 if (!smack_privileged(CAP_MAC_ADMIN))
1345 rc = cap_inode_removexattr(idmap, dentry, name);
1350 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1351 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1353 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1354 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1358 isp = smack_inode(d_backing_inode(dentry));
1360 * Don't do anything special for these.
1361 * XATTR_NAME_SMACKIPIN
1362 * XATTR_NAME_SMACKIPOUT
1364 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1365 struct super_block *sbp = dentry->d_sb;
1366 struct superblock_smack *sbsp = smack_superblock(sbp);
1368 isp->smk_inode = sbsp->smk_default;
1369 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1370 isp->smk_task = NULL;
1371 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1372 isp->smk_mmap = NULL;
1373 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1374 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1380 * smack_inode_set_acl - Smack check for setting posix acls
1381 * @idmap: idmap of the mnt this request came from
1382 * @dentry: the object
1383 * @acl_name: name of the posix acl
1384 * @kacl: the posix acls
1386 * Returns 0 if access is permitted, an error code otherwise
1388 static int smack_inode_set_acl(struct mnt_idmap *idmap,
1389 struct dentry *dentry, const char *acl_name,
1390 struct posix_acl *kacl)
1392 struct smk_audit_info ad;
1395 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1396 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1398 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1399 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1404 * smack_inode_get_acl - Smack check for getting posix acls
1405 * @idmap: idmap of the mnt this request came from
1406 * @dentry: the object
1407 * @acl_name: name of the posix acl
1409 * Returns 0 if access is permitted, an error code otherwise
1411 static int smack_inode_get_acl(struct mnt_idmap *idmap,
1412 struct dentry *dentry, const char *acl_name)
1414 struct smk_audit_info ad;
1417 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1418 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1420 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1421 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1426 * smack_inode_remove_acl - Smack check for getting posix acls
1427 * @idmap: idmap of the mnt this request came from
1428 * @dentry: the object
1429 * @acl_name: name of the posix acl
1431 * Returns 0 if access is permitted, an error code otherwise
1433 static int smack_inode_remove_acl(struct mnt_idmap *idmap,
1434 struct dentry *dentry, const char *acl_name)
1436 struct smk_audit_info ad;
1439 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1440 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1442 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1443 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1448 * smack_inode_getsecurity - get smack xattrs
1449 * @idmap: idmap of the mount
1450 * @inode: the object
1451 * @name: attribute name
1452 * @buffer: where to put the result
1453 * @alloc: duplicate memory
1455 * Returns the size of the attribute or an error code
1457 static int smack_inode_getsecurity(struct mnt_idmap *idmap,
1458 struct inode *inode, const char *name,
1459 void **buffer, bool alloc)
1461 struct socket_smack *ssp;
1462 struct socket *sock;
1463 struct super_block *sbp;
1464 struct inode *ip = inode;
1465 struct smack_known *isp;
1467 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1468 isp = smk_of_inode(inode);
1471 * The rest of the Smack xattrs are only on sockets.
1474 if (sbp->s_magic != SOCKFS_MAGIC)
1477 sock = SOCKET_I(ip);
1478 if (sock == NULL || sock->sk == NULL)
1481 ssp = sock->sk->sk_security;
1483 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1485 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1492 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1493 if (*buffer == NULL)
1497 return strlen(isp->smk_known);
1502 * smack_inode_listsecurity - list the Smack attributes
1503 * @inode: the object
1504 * @buffer: where they go
1505 * @buffer_size: size of buffer
1507 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1510 int len = sizeof(XATTR_NAME_SMACK);
1512 if (buffer != NULL && len <= buffer_size)
1513 memcpy(buffer, XATTR_NAME_SMACK, len);
1519 * smack_inode_getsecid - Extract inode's security id
1520 * @inode: inode to extract the info from
1521 * @secid: where result will be saved
1523 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1525 struct smack_known *skp = smk_of_inode(inode);
1527 *secid = skp->smk_secid;
1535 * There is no smack_file_permission hook
1537 * Should access checks be done on each read or write?
1538 * UNICOS and SELinux say yes.
1539 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1541 * I'll say no for now. Smack does not do the frequent
1542 * label changing that SELinux does.
1546 * smack_file_alloc_security - assign a file security blob
1549 * The security blob for a file is a pointer to the master
1550 * label list, so no allocation is done.
1552 * f_security is the owner security information. It
1553 * isn't used on file access checks, it's for send_sigio.
1557 static int smack_file_alloc_security(struct file *file)
1559 struct smack_known **blob = smack_file(file);
1561 *blob = smk_of_current();
1566 * smack_file_ioctl - Smack check on ioctls
1571 * Relies heavily on the correct use of the ioctl command conventions.
1573 * Returns 0 if allowed, error code otherwise
1575 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1579 struct smk_audit_info ad;
1580 struct inode *inode = file_inode(file);
1582 if (unlikely(IS_PRIVATE(inode)))
1585 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1586 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1588 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1589 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1590 rc = smk_bu_file(file, MAY_WRITE, rc);
1593 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1594 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1595 rc = smk_bu_file(file, MAY_READ, rc);
1602 * smack_file_lock - Smack check on file locking
1606 * Returns 0 if current has lock access, error code otherwise
1608 static int smack_file_lock(struct file *file, unsigned int cmd)
1610 struct smk_audit_info ad;
1612 struct inode *inode = file_inode(file);
1614 if (unlikely(IS_PRIVATE(inode)))
1617 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1618 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1619 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1620 rc = smk_bu_file(file, MAY_LOCK, rc);
1625 * smack_file_fcntl - Smack check on fcntl
1627 * @cmd: what action to check
1630 * Generally these operations are harmless.
1631 * File locking operations present an obvious mechanism
1632 * for passing information, so they require write access.
1634 * Returns 0 if current has access, error code otherwise
1636 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1639 struct smk_audit_info ad;
1641 struct inode *inode = file_inode(file);
1643 if (unlikely(IS_PRIVATE(inode)))
1651 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1652 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1653 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1654 rc = smk_bu_file(file, MAY_LOCK, rc);
1658 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1659 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1660 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1661 rc = smk_bu_file(file, MAY_WRITE, rc);
1671 * smack_mmap_file - Check permissions for a mmap operation.
1672 * @file: contains the file structure for file to map (may be NULL).
1673 * @reqprot: contains the protection requested by the application.
1674 * @prot: contains the protection that will be applied by the kernel.
1675 * @flags: contains the operational flags.
1677 * The @file may be NULL, e.g. if mapping anonymous memory.
1679 * Return 0 if permission is granted.
1681 static int smack_mmap_file(struct file *file,
1682 unsigned long reqprot, unsigned long prot,
1683 unsigned long flags)
1685 struct smack_known *skp;
1686 struct smack_known *mkp;
1687 struct smack_rule *srp;
1688 struct task_smack *tsp;
1689 struct smack_known *okp;
1690 struct inode_smack *isp;
1691 struct superblock_smack *sbsp;
1700 if (unlikely(IS_PRIVATE(file_inode(file))))
1703 isp = smack_inode(file_inode(file));
1704 if (isp->smk_mmap == NULL)
1706 sbsp = smack_superblock(file_inode(file)->i_sb);
1707 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1708 isp->smk_mmap != sbsp->smk_root)
1710 mkp = isp->smk_mmap;
1712 tsp = smack_cred(current_cred());
1713 skp = smk_of_current();
1718 * For each Smack rule associated with the subject
1719 * label verify that the SMACK64MMAP also has access
1720 * to that rule's object label.
1722 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1723 okp = srp->smk_object;
1725 * Matching labels always allows access.
1727 if (mkp->smk_known == okp->smk_known)
1730 * If there is a matching local rule take
1731 * that into account as well.
1733 may = smk_access_entry(srp->smk_subject->smk_known,
1737 may = srp->smk_access;
1739 may &= srp->smk_access;
1741 * If may is zero the SMACK64MMAP subject can't
1742 * possibly have less access.
1748 * Fetch the global list entry.
1749 * If there isn't one a SMACK64MMAP subject
1750 * can't have as much access as current.
1752 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1754 if (mmay == -ENOENT) {
1759 * If there is a local entry it modifies the
1760 * potential access, too.
1762 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1764 if (tmay != -ENOENT)
1768 * If there is any access available to current that is
1769 * not available to a SMACK64MMAP subject
1772 if ((may | mmay) != mmay) {
1784 * smack_file_set_fowner - set the file security blob value
1785 * @file: object in question
1788 static void smack_file_set_fowner(struct file *file)
1790 struct smack_known **blob = smack_file(file);
1792 *blob = smk_of_current();
1796 * smack_file_send_sigiotask - Smack on sigio
1797 * @tsk: The target task
1798 * @fown: the object the signal come from
1801 * Allow a privileged task to get signals even if it shouldn't
1803 * Returns 0 if a subject with the object's smack could
1804 * write to the task, an error code otherwise.
1806 static int smack_file_send_sigiotask(struct task_struct *tsk,
1807 struct fown_struct *fown, int signum)
1809 struct smack_known **blob;
1810 struct smack_known *skp;
1811 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1812 const struct cred *tcred;
1815 struct smk_audit_info ad;
1818 * struct fown_struct is never outside the context of a struct file
1820 file = container_of(fown, struct file, f_owner);
1822 /* we don't log here as rc can be overriden */
1823 blob = smack_file(file);
1825 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1826 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1829 tcred = __task_cred(tsk);
1830 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1834 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1835 smk_ad_setfield_u_tsk(&ad, tsk);
1836 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1841 * smack_file_receive - Smack file receive check
1844 * Returns 0 if current has access, error code otherwise
1846 static int smack_file_receive(struct file *file)
1850 struct smk_audit_info ad;
1851 struct inode *inode = file_inode(file);
1852 struct socket *sock;
1853 struct task_smack *tsp;
1854 struct socket_smack *ssp;
1856 if (unlikely(IS_PRIVATE(inode)))
1859 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1860 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1862 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1863 sock = SOCKET_I(inode);
1864 ssp = sock->sk->sk_security;
1865 tsp = smack_cred(current_cred());
1867 * If the receiving process can't write to the
1868 * passed socket or if the passed socket can't
1869 * write to the receiving process don't accept
1870 * the passed socket.
1872 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1873 rc = smk_bu_file(file, may, rc);
1876 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1877 rc = smk_bu_file(file, may, rc);
1881 * This code relies on bitmasks.
1883 if (file->f_mode & FMODE_READ)
1885 if (file->f_mode & FMODE_WRITE)
1888 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1889 rc = smk_bu_file(file, may, rc);
1894 * smack_file_open - Smack dentry open processing
1897 * Set the security blob in the file structure.
1898 * Allow the open only if the task has read access. There are
1899 * many read operations (e.g. fstat) that you can do with an
1900 * fd even if you have the file open write-only.
1902 * Returns 0 if current has access, error code otherwise
1904 static int smack_file_open(struct file *file)
1906 struct task_smack *tsp = smack_cred(file->f_cred);
1907 struct inode *inode = file_inode(file);
1908 struct smk_audit_info ad;
1911 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1912 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1913 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1914 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1924 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1925 * @cred: the new credentials
1926 * @gfp: the atomicity of any memory allocations
1928 * Prepare a blank set of credentials for modification. This must allocate all
1929 * the memory the LSM module might require such that cred_transfer() can
1930 * complete without error.
1932 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1934 init_task_smack(smack_cred(cred), NULL, NULL);
1940 * smack_cred_free - "free" task-level security credentials
1941 * @cred: the credentials in question
1944 static void smack_cred_free(struct cred *cred)
1946 struct task_smack *tsp = smack_cred(cred);
1947 struct smack_rule *rp;
1948 struct list_head *l;
1949 struct list_head *n;
1951 smk_destroy_label_list(&tsp->smk_relabel);
1953 list_for_each_safe(l, n, &tsp->smk_rules) {
1954 rp = list_entry(l, struct smack_rule, list);
1955 list_del(&rp->list);
1956 kmem_cache_free(smack_rule_cache, rp);
1961 * smack_cred_prepare - prepare new set of credentials for modification
1962 * @new: the new credentials
1963 * @old: the original credentials
1964 * @gfp: the atomicity of any memory allocations
1966 * Prepare a new set of credentials for modification.
1968 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1971 struct task_smack *old_tsp = smack_cred(old);
1972 struct task_smack *new_tsp = smack_cred(new);
1975 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1977 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1981 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1987 * smack_cred_transfer - Transfer the old credentials to the new credentials
1988 * @new: the new credentials
1989 * @old: the original credentials
1991 * Fill in a set of blank credentials from another set of credentials.
1993 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1995 struct task_smack *old_tsp = smack_cred(old);
1996 struct task_smack *new_tsp = smack_cred(new);
1998 new_tsp->smk_task = old_tsp->smk_task;
1999 new_tsp->smk_forked = old_tsp->smk_task;
2000 mutex_init(&new_tsp->smk_rules_lock);
2001 INIT_LIST_HEAD(&new_tsp->smk_rules);
2003 /* cbs copy rule list */
2007 * smack_cred_getsecid - get the secid corresponding to a creds structure
2008 * @cred: the object creds
2009 * @secid: where to put the result
2011 * Sets the secid to contain a u32 version of the smack label.
2013 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
2015 struct smack_known *skp;
2018 skp = smk_of_task(smack_cred(cred));
2019 *secid = skp->smk_secid;
2024 * smack_kernel_act_as - Set the subjective context in a set of credentials
2025 * @new: points to the set of credentials to be modified.
2026 * @secid: specifies the security ID to be set
2028 * Set the security data for a kernel service.
2030 static int smack_kernel_act_as(struct cred *new, u32 secid)
2032 struct task_smack *new_tsp = smack_cred(new);
2034 new_tsp->smk_task = smack_from_secid(secid);
2039 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2040 * @new: points to the set of credentials to be modified
2041 * @inode: points to the inode to use as a reference
2043 * Set the file creation context in a set of credentials to the same
2044 * as the objective context of the specified inode
2046 static int smack_kernel_create_files_as(struct cred *new,
2047 struct inode *inode)
2049 struct inode_smack *isp = smack_inode(inode);
2050 struct task_smack *tsp = smack_cred(new);
2052 tsp->smk_forked = isp->smk_inode;
2053 tsp->smk_task = tsp->smk_forked;
2058 * smk_curacc_on_task - helper to log task related access
2059 * @p: the task object
2060 * @access: the access requested
2061 * @caller: name of the calling function for audit
2063 * Return 0 if access is permitted
2065 static int smk_curacc_on_task(struct task_struct *p, int access,
2068 struct smk_audit_info ad;
2069 struct smack_known *skp = smk_of_task_struct_obj(p);
2072 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2073 smk_ad_setfield_u_tsk(&ad, p);
2074 rc = smk_curacc(skp, access, &ad);
2075 rc = smk_bu_task(p, access, rc);
2080 * smack_task_setpgid - Smack check on setting pgid
2081 * @p: the task object
2084 * Return 0 if write access is permitted
2086 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2088 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2092 * smack_task_getpgid - Smack access check for getpgid
2093 * @p: the object task
2095 * Returns 0 if current can read the object task, error code otherwise
2097 static int smack_task_getpgid(struct task_struct *p)
2099 return smk_curacc_on_task(p, MAY_READ, __func__);
2103 * smack_task_getsid - Smack access check for getsid
2104 * @p: the object task
2106 * Returns 0 if current can read the object task, error code otherwise
2108 static int smack_task_getsid(struct task_struct *p)
2110 return smk_curacc_on_task(p, MAY_READ, __func__);
2114 * smack_current_getsecid_subj - get the subjective secid of the current task
2115 * @secid: where to put the result
2117 * Sets the secid to contain a u32 version of the task's subjective smack label.
2119 static void smack_current_getsecid_subj(u32 *secid)
2121 struct smack_known *skp = smk_of_current();
2123 *secid = skp->smk_secid;
2127 * smack_task_getsecid_obj - get the objective secid of the task
2129 * @secid: where to put the result
2131 * Sets the secid to contain a u32 version of the task's objective smack label.
2133 static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2135 struct smack_known *skp = smk_of_task_struct_obj(p);
2137 *secid = skp->smk_secid;
2141 * smack_task_setnice - Smack check on setting nice
2142 * @p: the task object
2145 * Return 0 if write access is permitted
2147 static int smack_task_setnice(struct task_struct *p, int nice)
2149 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2153 * smack_task_setioprio - Smack check on setting ioprio
2154 * @p: the task object
2157 * Return 0 if write access is permitted
2159 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2161 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2165 * smack_task_getioprio - Smack check on reading ioprio
2166 * @p: the task object
2168 * Return 0 if read access is permitted
2170 static int smack_task_getioprio(struct task_struct *p)
2172 return smk_curacc_on_task(p, MAY_READ, __func__);
2176 * smack_task_setscheduler - Smack check on setting scheduler
2177 * @p: the task object
2179 * Return 0 if read access is permitted
2181 static int smack_task_setscheduler(struct task_struct *p)
2183 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2187 * smack_task_getscheduler - Smack check on reading scheduler
2188 * @p: the task object
2190 * Return 0 if read access is permitted
2192 static int smack_task_getscheduler(struct task_struct *p)
2194 return smk_curacc_on_task(p, MAY_READ, __func__);
2198 * smack_task_movememory - Smack check on moving memory
2199 * @p: the task object
2201 * Return 0 if write access is permitted
2203 static int smack_task_movememory(struct task_struct *p)
2205 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2209 * smack_task_kill - Smack check on signal delivery
2210 * @p: the task object
2213 * @cred: identifies the cred to use in lieu of current's
2215 * Return 0 if write access is permitted
2218 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2219 int sig, const struct cred *cred)
2221 struct smk_audit_info ad;
2222 struct smack_known *skp;
2223 struct smack_known *tkp = smk_of_task_struct_obj(p);
2227 return 0; /* null signal; existence test */
2229 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2230 smk_ad_setfield_u_tsk(&ad, p);
2232 * Sending a signal requires that the sender
2233 * can write the receiver.
2236 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2237 rc = smk_bu_task(p, MAY_DELIVER, rc);
2241 * If the cred isn't NULL we're dealing with some USB IO
2242 * specific behavior. This is not clean. For one thing
2243 * we can't take privilege into account.
2245 skp = smk_of_task(smack_cred(cred));
2246 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2247 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2252 * smack_task_to_inode - copy task smack into the inode blob
2253 * @p: task to copy from
2254 * @inode: inode to copy to
2256 * Sets the smack pointer in the inode security blob
2258 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2260 struct inode_smack *isp = smack_inode(inode);
2261 struct smack_known *skp = smk_of_task_struct_obj(p);
2263 isp->smk_inode = skp;
2264 isp->smk_flags |= SMK_INODE_INSTANT;
2272 * smack_sk_alloc_security - Allocate a socket blob
2275 * @gfp_flags: memory allocation flags
2277 * Assign Smack pointers to current
2279 * Returns 0 on success, -ENOMEM is there's no memory
2281 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2283 struct smack_known *skp = smk_of_current();
2284 struct socket_smack *ssp;
2286 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2291 * Sockets created by kernel threads receive web label.
2293 if (unlikely(current->flags & PF_KTHREAD)) {
2294 ssp->smk_in = &smack_known_web;
2295 ssp->smk_out = &smack_known_web;
2300 ssp->smk_packet = NULL;
2302 sk->sk_security = ssp;
2308 * smack_sk_free_security - Free a socket blob
2311 * Clears the blob pointer
2313 static void smack_sk_free_security(struct sock *sk)
2315 #ifdef SMACK_IPV6_PORT_LABELING
2316 struct smk_port_label *spp;
2318 if (sk->sk_family == PF_INET6) {
2320 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2321 if (spp->smk_sock != sk)
2323 spp->smk_can_reuse = 1;
2329 kfree(sk->sk_security);
2333 * smack_sk_clone_security - Copy security context
2334 * @sk: the old socket
2335 * @newsk: the new socket
2337 * Copy the security context of the old socket pointer to the cloned
2339 static void smack_sk_clone_security(const struct sock *sk, struct sock *newsk)
2341 struct socket_smack *ssp_old = sk->sk_security;
2342 struct socket_smack *ssp_new = newsk->sk_security;
2344 *ssp_new = *ssp_old;
2348 * smack_ipv4host_label - check host based restrictions
2349 * @sip: the object end
2351 * looks for host based access restrictions
2353 * This version will only be appropriate for really small sets of single label
2354 * hosts. The caller is responsible for ensuring that the RCU read lock is
2355 * taken before calling this function.
2357 * Returns the label of the far end or NULL if it's not special.
2359 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2361 struct smk_net4addr *snp;
2362 struct in_addr *siap = &sip->sin_addr;
2364 if (siap->s_addr == 0)
2367 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2369 * we break after finding the first match because
2370 * the list is sorted from longest to shortest mask
2371 * so we have found the most specific match
2373 if (snp->smk_host.s_addr ==
2374 (siap->s_addr & snp->smk_mask.s_addr))
2375 return snp->smk_label;
2381 * smk_ipv6_localhost - Check for local ipv6 host address
2384 * Returns boolean true if this is the localhost address
2386 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2388 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2389 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2391 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2392 ntohs(be16p[7]) == 1)
2398 * smack_ipv6host_label - check host based restrictions
2399 * @sip: the object end
2401 * looks for host based access restrictions
2403 * This version will only be appropriate for really small sets of single label
2404 * hosts. The caller is responsible for ensuring that the RCU read lock is
2405 * taken before calling this function.
2407 * Returns the label of the far end or NULL if it's not special.
2409 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2411 struct smk_net6addr *snp;
2412 struct in6_addr *sap = &sip->sin6_addr;
2417 * It's local. Don't look for a host label.
2419 if (smk_ipv6_localhost(sip))
2422 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2424 * If the label is NULL the entry has
2425 * been renounced. Ignore it.
2427 if (snp->smk_label == NULL)
2430 * we break after finding the first match because
2431 * the list is sorted from longest to shortest mask
2432 * so we have found the most specific match
2434 for (found = 1, i = 0; i < 8; i++) {
2435 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2436 snp->smk_host.s6_addr16[i]) {
2442 return snp->smk_label;
2449 * smack_netlbl_add - Set the secattr on a socket
2452 * Attach the outbound smack value (smk_out) to the socket.
2454 * Returns 0 on success or an error code
2456 static int smack_netlbl_add(struct sock *sk)
2458 struct socket_smack *ssp = sk->sk_security;
2459 struct smack_known *skp = ssp->smk_out;
2463 bh_lock_sock_nested(sk);
2465 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2468 ssp->smk_state = SMK_NETLBL_LABELED;
2471 ssp->smk_state = SMK_NETLBL_REQSKB;
2483 * smack_netlbl_delete - Remove the secattr from a socket
2486 * Remove the outbound smack value from a socket
2488 static void smack_netlbl_delete(struct sock *sk)
2490 struct socket_smack *ssp = sk->sk_security;
2493 * Take the label off the socket if one is set.
2495 if (ssp->smk_state != SMK_NETLBL_LABELED)
2499 bh_lock_sock_nested(sk);
2500 netlbl_sock_delattr(sk);
2503 ssp->smk_state = SMK_NETLBL_UNLABELED;
2507 * smk_ipv4_check - Perform IPv4 host access checks
2509 * @sap: the destination address
2511 * Set the correct secattr for the given socket based on the destination
2512 * address and perform any outbound access checks needed.
2514 * Returns 0 on success or an error code.
2517 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2519 struct smack_known *skp;
2521 struct smack_known *hkp;
2522 struct socket_smack *ssp = sk->sk_security;
2523 struct smk_audit_info ad;
2526 hkp = smack_ipv4host_label(sap);
2529 struct lsm_network_audit net;
2531 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2532 ad.a.u.net->family = sap->sin_family;
2533 ad.a.u.net->dport = sap->sin_port;
2534 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2537 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2538 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2540 * Clear the socket netlabel if it's set.
2543 smack_netlbl_delete(sk);
2551 * smk_ipv6_check - check Smack access
2552 * @subject: subject Smack label
2553 * @object: object Smack label
2555 * @act: the action being taken
2557 * Check an IPv6 access
2559 static int smk_ipv6_check(struct smack_known *subject,
2560 struct smack_known *object,
2561 struct sockaddr_in6 *address, int act)
2564 struct lsm_network_audit net;
2566 struct smk_audit_info ad;
2570 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2571 ad.a.u.net->family = PF_INET6;
2572 ad.a.u.net->dport = address->sin6_port;
2573 if (act == SMK_RECEIVING)
2574 ad.a.u.net->v6info.saddr = address->sin6_addr;
2576 ad.a.u.net->v6info.daddr = address->sin6_addr;
2578 rc = smk_access(subject, object, MAY_WRITE, &ad);
2579 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2583 #ifdef SMACK_IPV6_PORT_LABELING
2585 * smk_ipv6_port_label - Smack port access table management
2589 * Create or update the port list entry
2591 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2593 struct sock *sk = sock->sk;
2594 struct sockaddr_in6 *addr6;
2595 struct socket_smack *ssp = sock->sk->sk_security;
2596 struct smk_port_label *spp;
2597 unsigned short port = 0;
2599 if (address == NULL) {
2601 * This operation is changing the Smack information
2602 * on the bound socket. Take the changes to the port
2606 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2607 if (sk != spp->smk_sock)
2609 spp->smk_in = ssp->smk_in;
2610 spp->smk_out = ssp->smk_out;
2615 * A NULL address is only used for updating existing
2616 * bound entries. If there isn't one, it's OK.
2622 addr6 = (struct sockaddr_in6 *)address;
2623 port = ntohs(addr6->sin6_port);
2625 * This is a special case that is safely ignored.
2631 * Look for an existing port list entry.
2632 * This is an indication that a port is getting reused.
2635 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2636 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2638 if (spp->smk_can_reuse != 1) {
2642 spp->smk_port = port;
2644 spp->smk_in = ssp->smk_in;
2645 spp->smk_out = ssp->smk_out;
2646 spp->smk_can_reuse = 0;
2652 * A new port entry is required.
2654 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2658 spp->smk_port = port;
2660 spp->smk_in = ssp->smk_in;
2661 spp->smk_out = ssp->smk_out;
2662 spp->smk_sock_type = sock->type;
2663 spp->smk_can_reuse = 0;
2665 mutex_lock(&smack_ipv6_lock);
2666 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2667 mutex_unlock(&smack_ipv6_lock);
2672 * smk_ipv6_port_check - check Smack port access
2675 * @act: the action being taken
2677 * Create or update the port list entry
2679 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2682 struct smk_port_label *spp;
2683 struct socket_smack *ssp = sk->sk_security;
2684 struct smack_known *skp = NULL;
2685 unsigned short port;
2686 struct smack_known *object;
2688 if (act == SMK_RECEIVING) {
2689 skp = smack_ipv6host_label(address);
2690 object = ssp->smk_in;
2693 object = smack_ipv6host_label(address);
2697 * The other end is a single label host.
2699 if (skp != NULL && object != NULL)
2700 return smk_ipv6_check(skp, object, address, act);
2702 skp = smack_net_ambient;
2704 object = smack_net_ambient;
2707 * It's remote, so port lookup does no good.
2709 if (!smk_ipv6_localhost(address))
2710 return smk_ipv6_check(skp, object, address, act);
2713 * It's local so the send check has to have passed.
2715 if (act == SMK_RECEIVING)
2718 port = ntohs(address->sin6_port);
2720 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2721 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2723 object = spp->smk_in;
2724 if (act == SMK_CONNECTING)
2725 ssp->smk_packet = spp->smk_out;
2730 return smk_ipv6_check(skp, object, address, act);
2735 * smack_inode_setsecurity - set smack xattrs
2736 * @inode: the object
2737 * @name: attribute name
2738 * @value: attribute value
2739 * @size: size of the attribute
2742 * Sets the named attribute in the appropriate blob
2744 * Returns 0 on success, or an error code
2746 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2747 const void *value, size_t size, int flags)
2749 struct smack_known *skp;
2750 struct inode_smack *nsp = smack_inode(inode);
2751 struct socket_smack *ssp;
2752 struct socket *sock;
2755 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2758 skp = smk_import_entry(value, size);
2760 return PTR_ERR(skp);
2762 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2763 nsp->smk_inode = skp;
2764 nsp->smk_flags |= SMK_INODE_INSTANT;
2768 * The rest of the Smack xattrs are only on sockets.
2770 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2773 sock = SOCKET_I(inode);
2774 if (sock == NULL || sock->sk == NULL)
2777 ssp = sock->sk->sk_security;
2779 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2781 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2783 if (sock->sk->sk_family == PF_INET) {
2784 rc = smack_netlbl_add(sock->sk);
2787 "Smack: \"%s\" netlbl error %d.\n",
2793 #ifdef SMACK_IPV6_PORT_LABELING
2794 if (sock->sk->sk_family == PF_INET6)
2795 smk_ipv6_port_label(sock, NULL);
2802 * smack_socket_post_create - finish socket setup
2804 * @family: protocol family
2809 * Sets the netlabel information on the socket
2811 * Returns 0 on success, and error code otherwise
2813 static int smack_socket_post_create(struct socket *sock, int family,
2814 int type, int protocol, int kern)
2816 struct socket_smack *ssp;
2818 if (sock->sk == NULL)
2822 * Sockets created by kernel threads receive web label.
2824 if (unlikely(current->flags & PF_KTHREAD)) {
2825 ssp = sock->sk->sk_security;
2826 ssp->smk_in = &smack_known_web;
2827 ssp->smk_out = &smack_known_web;
2830 if (family != PF_INET)
2833 * Set the outbound netlbl.
2835 return smack_netlbl_add(sock->sk);
2839 * smack_socket_socketpair - create socket pair
2840 * @socka: one socket
2841 * @sockb: another socket
2843 * Cross reference the peer labels for SO_PEERSEC
2847 static int smack_socket_socketpair(struct socket *socka,
2848 struct socket *sockb)
2850 struct socket_smack *asp = socka->sk->sk_security;
2851 struct socket_smack *bsp = sockb->sk->sk_security;
2853 asp->smk_packet = bsp->smk_out;
2854 bsp->smk_packet = asp->smk_out;
2859 #ifdef SMACK_IPV6_PORT_LABELING
2861 * smack_socket_bind - record port binding information.
2863 * @address: the port address
2864 * @addrlen: size of the address
2866 * Records the label bound to a port.
2868 * Returns 0 on success, and error code otherwise
2870 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2873 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2874 if (addrlen < SIN6_LEN_RFC2133 ||
2875 address->sa_family != AF_INET6)
2877 smk_ipv6_port_label(sock, address);
2881 #endif /* SMACK_IPV6_PORT_LABELING */
2884 * smack_socket_connect - connect access check
2886 * @sap: the other end
2887 * @addrlen: size of sap
2889 * Verifies that a connection may be possible
2891 * Returns 0 on success, and error code otherwise
2893 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2898 if (sock->sk == NULL)
2900 if (sock->sk->sk_family != PF_INET &&
2901 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2903 if (addrlen < offsetofend(struct sockaddr, sa_family))
2905 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2906 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2907 struct smack_known *rsp = NULL;
2909 if (addrlen < SIN6_LEN_RFC2133)
2911 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2912 rsp = smack_ipv6host_label(sip);
2914 struct socket_smack *ssp = sock->sk->sk_security;
2916 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2919 #ifdef SMACK_IPV6_PORT_LABELING
2920 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2925 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2927 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
2932 * smack_flags_to_may - convert S_ to MAY_ values
2933 * @flags: the S_ value
2935 * Returns the equivalent MAY_ value
2937 static int smack_flags_to_may(int flags)
2941 if (flags & S_IRUGO)
2943 if (flags & S_IWUGO)
2945 if (flags & S_IXUGO)
2952 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2957 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2959 struct smack_known **blob = smack_msg_msg(msg);
2961 *blob = smk_of_current();
2966 * smack_of_ipc - the smack pointer for the ipc
2969 * Returns a pointer to the smack value
2971 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2973 struct smack_known **blob = smack_ipc(isp);
2979 * smack_ipc_alloc_security - Set the security blob for ipc
2984 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2986 struct smack_known **blob = smack_ipc(isp);
2988 *blob = smk_of_current();
2993 * smk_curacc_shm : check if current has access on shm
2995 * @access : access requested
2997 * Returns 0 if current has the requested access, error code otherwise
2999 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
3001 struct smack_known *ssp = smack_of_ipc(isp);
3002 struct smk_audit_info ad;
3006 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3007 ad.a.u.ipc_id = isp->id;
3009 rc = smk_curacc(ssp, access, &ad);
3010 rc = smk_bu_current("shm", ssp, access, rc);
3015 * smack_shm_associate - Smack access check for shm
3017 * @shmflg: access requested
3019 * Returns 0 if current has the requested access, error code otherwise
3021 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3025 may = smack_flags_to_may(shmflg);
3026 return smk_curacc_shm(isp, may);
3030 * smack_shm_shmctl - Smack access check for shm
3032 * @cmd: what it wants to do
3034 * Returns 0 if current has the requested access, error code otherwise
3036 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3050 may = MAY_READWRITE;
3055 * System level information.
3061 return smk_curacc_shm(isp, may);
3065 * smack_shm_shmat - Smack access for shmat
3068 * @shmflg: access requested
3070 * Returns 0 if current has the requested access, error code otherwise
3072 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3077 may = smack_flags_to_may(shmflg);
3078 return smk_curacc_shm(isp, may);
3082 * smk_curacc_sem : check if current has access on sem
3084 * @access : access requested
3086 * Returns 0 if current has the requested access, error code otherwise
3088 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3090 struct smack_known *ssp = smack_of_ipc(isp);
3091 struct smk_audit_info ad;
3095 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3096 ad.a.u.ipc_id = isp->id;
3098 rc = smk_curacc(ssp, access, &ad);
3099 rc = smk_bu_current("sem", ssp, access, rc);
3104 * smack_sem_associate - Smack access check for sem
3106 * @semflg: access requested
3108 * Returns 0 if current has the requested access, error code otherwise
3110 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3114 may = smack_flags_to_may(semflg);
3115 return smk_curacc_sem(isp, may);
3119 * smack_sem_semctl - Smack access check for sem
3121 * @cmd: what it wants to do
3123 * Returns 0 if current has the requested access, error code otherwise
3125 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3144 may = MAY_READWRITE;
3149 * System level information
3156 return smk_curacc_sem(isp, may);
3160 * smack_sem_semop - Smack checks of semaphore operations
3166 * Treated as read and write in all cases.
3168 * Returns 0 if access is allowed, error code otherwise
3170 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3171 unsigned nsops, int alter)
3173 return smk_curacc_sem(isp, MAY_READWRITE);
3177 * smk_curacc_msq : helper to check if current has access on msq
3179 * @access : access requested
3181 * return 0 if current has access, error otherwise
3183 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3185 struct smack_known *msp = smack_of_ipc(isp);
3186 struct smk_audit_info ad;
3190 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3191 ad.a.u.ipc_id = isp->id;
3193 rc = smk_curacc(msp, access, &ad);
3194 rc = smk_bu_current("msq", msp, access, rc);
3199 * smack_msg_queue_associate - Smack access check for msg_queue
3201 * @msqflg: access requested
3203 * Returns 0 if current has the requested access, error code otherwise
3205 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3209 may = smack_flags_to_may(msqflg);
3210 return smk_curacc_msq(isp, may);
3214 * smack_msg_queue_msgctl - Smack access check for msg_queue
3216 * @cmd: what it wants to do
3218 * Returns 0 if current has the requested access, error code otherwise
3220 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3232 may = MAY_READWRITE;
3237 * System level information
3244 return smk_curacc_msq(isp, may);
3248 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3251 * @msqflg: access requested
3253 * Returns 0 if current has the requested access, error code otherwise
3255 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3260 may = smack_flags_to_may(msqflg);
3261 return smk_curacc_msq(isp, may);
3265 * smack_msg_queue_msgrcv - Smack access check for msg_queue
3272 * Returns 0 if current has read and write access, error code otherwise
3274 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp,
3275 struct msg_msg *msg,
3276 struct task_struct *target, long type,
3279 return smk_curacc_msq(isp, MAY_READWRITE);
3283 * smack_ipc_permission - Smack access for ipc_permission()
3284 * @ipp: the object permissions
3285 * @flag: access requested
3287 * Returns 0 if current has read and write access, error code otherwise
3289 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3291 struct smack_known **blob = smack_ipc(ipp);
3292 struct smack_known *iskp = *blob;
3293 int may = smack_flags_to_may(flag);
3294 struct smk_audit_info ad;
3298 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3299 ad.a.u.ipc_id = ipp->id;
3301 rc = smk_curacc(iskp, may, &ad);
3302 rc = smk_bu_current("svipc", iskp, may, rc);
3307 * smack_ipc_getsecid - Extract smack security id
3308 * @ipp: the object permissions
3309 * @secid: where result will be saved
3311 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3313 struct smack_known **blob = smack_ipc(ipp);
3314 struct smack_known *iskp = *blob;
3316 *secid = iskp->smk_secid;
3320 * smack_d_instantiate - Make sure the blob is correct on an inode
3321 * @opt_dentry: dentry where inode will be attached
3322 * @inode: the object
3324 * Set the inode's security blob if it hasn't been done already.
3326 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3328 struct super_block *sbp;
3329 struct superblock_smack *sbsp;
3330 struct inode_smack *isp;
3331 struct smack_known *skp;
3332 struct smack_known *ckp = smk_of_current();
3333 struct smack_known *final;
3334 char trattr[TRANS_TRUE_SIZE];
3342 isp = smack_inode(inode);
3345 * If the inode is already instantiated
3346 * take the quick way out
3348 if (isp->smk_flags & SMK_INODE_INSTANT)
3352 sbsp = smack_superblock(sbp);
3354 * We're going to use the superblock default label
3355 * if there's no label on the file.
3357 final = sbsp->smk_default;
3360 * If this is the root inode the superblock
3361 * may be in the process of initialization.
3362 * If that is the case use the root value out
3363 * of the superblock.
3365 if (opt_dentry->d_parent == opt_dentry) {
3366 switch (sbp->s_magic) {
3367 case CGROUP_SUPER_MAGIC:
3368 case CGROUP2_SUPER_MAGIC:
3370 * The cgroup filesystem is never mounted,
3371 * so there's no opportunity to set the mount
3374 sbsp->smk_root = &smack_known_star;
3375 sbsp->smk_default = &smack_known_star;
3376 isp->smk_inode = sbsp->smk_root;
3380 * What about shmem/tmpfs anonymous files with dentry
3381 * obtained from d_alloc_pseudo()?
3383 isp->smk_inode = smk_of_current();
3386 isp->smk_inode = smk_of_current();
3390 * Socket access is controlled by the socket
3391 * structures associated with the task involved.
3393 isp->smk_inode = &smack_known_star;
3396 isp->smk_inode = sbsp->smk_root;
3399 isp->smk_flags |= SMK_INODE_INSTANT;
3404 * This is pretty hackish.
3405 * Casey says that we shouldn't have to do
3406 * file system specific code, but it does help
3407 * with keeping it simple.
3409 switch (sbp->s_magic) {
3411 case CGROUP_SUPER_MAGIC:
3412 case CGROUP2_SUPER_MAGIC:
3414 * Casey says that it's a little embarrassing
3415 * that the smack file system doesn't do
3416 * extended attributes.
3418 * Cgroupfs is special
3420 final = &smack_known_star;
3422 case DEVPTS_SUPER_MAGIC:
3424 * devpts seems content with the label of the task.
3425 * Programs that change smack have to treat the
3430 case PROC_SUPER_MAGIC:
3432 * Casey says procfs appears not to care.
3433 * The superblock default suffices.
3438 * Device labels should come from the filesystem,
3439 * but watch out, because they're volitile,
3440 * getting recreated on every reboot.
3442 final = &smack_known_star;
3444 * If a smack value has been set we want to use it,
3445 * but since tmpfs isn't giving us the opportunity
3446 * to set mount options simulate setting the
3447 * superblock default.
3452 * This isn't an understood special case.
3453 * Get the value from the xattr.
3457 * UNIX domain sockets use lower level socket data.
3459 if (S_ISSOCK(inode->i_mode)) {
3460 final = &smack_known_star;
3464 * No xattr support means, alas, no SMACK label.
3465 * Use the aforeapplied default.
3466 * It would be curious if the label of the task
3467 * does not match that assigned.
3469 if (!(inode->i_opflags & IOP_XATTR))
3472 * Get the dentry for xattr.
3474 dp = dget(opt_dentry);
3475 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3476 if (!IS_ERR_OR_NULL(skp))
3480 * Transmuting directory
3482 if (S_ISDIR(inode->i_mode)) {
3484 * If this is a new directory and the label was
3485 * transmuted when the inode was initialized
3486 * set the transmute attribute on the directory
3487 * and mark the inode.
3489 * If there is a transmute attribute on the
3490 * directory mark the inode.
3492 if (isp->smk_flags & SMK_INODE_CHANGED) {
3493 isp->smk_flags &= ~SMK_INODE_CHANGED;
3494 rc = __vfs_setxattr(&nop_mnt_idmap, dp, inode,
3495 XATTR_NAME_SMACKTRANSMUTE,
3496 TRANS_TRUE, TRANS_TRUE_SIZE,
3499 rc = __vfs_getxattr(dp, inode,
3500 XATTR_NAME_SMACKTRANSMUTE, trattr,
3502 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3503 TRANS_TRUE_SIZE) != 0)
3507 transflag = SMK_INODE_TRANSMUTE;
3510 * Don't let the exec or mmap label be "*" or "@".
3512 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3513 if (IS_ERR(skp) || skp == &smack_known_star ||
3514 skp == &smack_known_web)
3516 isp->smk_task = skp;
3518 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3519 if (IS_ERR(skp) || skp == &smack_known_star ||
3520 skp == &smack_known_web)
3522 isp->smk_mmap = skp;
3529 isp->smk_inode = ckp;
3531 isp->smk_inode = final;
3533 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3539 * smack_getprocattr - Smack process attribute access
3540 * @p: the object task
3541 * @name: the name of the attribute in /proc/.../attr
3542 * @value: where to put the result
3544 * Places a copy of the task Smack into value
3546 * Returns the length of the smack label or an error code
3548 static int smack_getprocattr(struct task_struct *p, const char *name, char **value)
3550 struct smack_known *skp = smk_of_task_struct_obj(p);
3554 if (strcmp(name, "current") != 0)
3557 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3567 * smack_setprocattr - Smack process attribute setting
3568 * @name: the name of the attribute in /proc/.../attr
3569 * @value: the value to set
3570 * @size: the size of the value
3572 * Sets the Smack value of the task. Only setting self
3573 * is permitted and only with privilege
3575 * Returns the length of the smack label or an error code
3577 static int smack_setprocattr(const char *name, void *value, size_t size)
3579 struct task_smack *tsp = smack_cred(current_cred());
3581 struct smack_known *skp;
3582 struct smack_known_list_elem *sklep;
3585 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3588 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3591 if (strcmp(name, "current") != 0)
3594 skp = smk_import_entry(value, size);
3596 return PTR_ERR(skp);
3599 * No process is ever allowed the web ("@") label
3600 * and the star ("*") label.
3602 if (skp == &smack_known_web || skp == &smack_known_star)
3605 if (!smack_privileged(CAP_MAC_ADMIN)) {
3607 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3608 if (sklep->smk_label == skp) {
3616 new = prepare_creds();
3620 tsp = smack_cred(new);
3621 tsp->smk_task = skp;
3623 * process can change its label only once
3625 smk_destroy_label_list(&tsp->smk_relabel);
3632 * smack_unix_stream_connect - Smack access on UDS
3634 * @other: the other sock
3637 * Return 0 if a subject with the smack of sock could access
3638 * an object with the smack of other, otherwise an error code
3640 static int smack_unix_stream_connect(struct sock *sock,
3641 struct sock *other, struct sock *newsk)
3643 struct smack_known *skp;
3644 struct smack_known *okp;
3645 struct socket_smack *ssp = sock->sk_security;
3646 struct socket_smack *osp = other->sk_security;
3647 struct socket_smack *nsp = newsk->sk_security;
3648 struct smk_audit_info ad;
3651 struct lsm_network_audit net;
3654 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3658 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3659 smk_ad_setfield_u_net_sk(&ad, other);
3661 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3662 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3666 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3667 rc = smk_bu_note("UDS connect", okp, skp,
3673 * Cross reference the peer labels for SO_PEERSEC.
3676 nsp->smk_packet = ssp->smk_out;
3677 ssp->smk_packet = osp->smk_out;
3684 * smack_unix_may_send - Smack access on UDS
3686 * @other: the other socket
3688 * Return 0 if a subject with the smack of sock could access
3689 * an object with the smack of other, otherwise an error code
3691 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3693 struct socket_smack *ssp = sock->sk->sk_security;
3694 struct socket_smack *osp = other->sk->sk_security;
3695 struct smk_audit_info ad;
3699 struct lsm_network_audit net;
3701 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3702 smk_ad_setfield_u_net_sk(&ad, other->sk);
3705 if (smack_privileged(CAP_MAC_OVERRIDE))
3708 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3709 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3714 * smack_socket_sendmsg - Smack check based on destination host
3717 * @size: the size of the message
3719 * Return 0 if the current subject can write to the destination host.
3720 * For IPv4 this is only a question if the destination is a single label host.
3721 * For IPv6 this is a check against the label of the port.
3723 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3726 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3727 #if IS_ENABLED(CONFIG_IPV6)
3728 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3730 #ifdef SMACK_IPV6_SECMARK_LABELING
3731 struct socket_smack *ssp = sock->sk->sk_security;
3732 struct smack_known *rsp;
3737 * Perfectly reasonable for this to be NULL
3742 switch (sock->sk->sk_family) {
3744 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3745 sip->sin_family != AF_INET)
3747 rc = smk_ipv4_check(sock->sk, sip);
3749 #if IS_ENABLED(CONFIG_IPV6)
3751 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3752 sap->sin6_family != AF_INET6)
3754 #ifdef SMACK_IPV6_SECMARK_LABELING
3755 rsp = smack_ipv6host_label(sap);
3757 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3760 #ifdef SMACK_IPV6_PORT_LABELING
3761 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3763 #endif /* IS_ENABLED(CONFIG_IPV6) */
3770 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3771 * @sap: netlabel secattr
3772 * @ssp: socket security information
3774 * Returns a pointer to a Smack label entry found on the label list.
3776 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3777 struct socket_smack *ssp)
3779 struct smack_known *skp;
3785 * Netlabel found it in the cache.
3787 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3788 return (struct smack_known *)sap->cache->data;
3790 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3792 * Looks like a fallback, which gives us a secid.
3794 return smack_from_secid(sap->attr.secid);
3796 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3798 * Looks like a CIPSO packet.
3799 * If there are flags but no level netlabel isn't
3800 * behaving the way we expect it to.
3802 * Look it up in the label table
3803 * Without guidance regarding the smack value
3804 * for the packet fall back on the network
3808 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3809 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3812 * Compare the catsets. Use the netlbl APIs.
3814 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3815 if ((skp->smk_netlabel.flags &
3816 NETLBL_SECATTR_MLS_CAT) == 0)
3820 for (acat = -1, kcat = -1; acat == kcat; ) {
3821 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3823 kcat = netlbl_catmap_walk(
3824 skp->smk_netlabel.attr.mls.cat,
3826 if (acat < 0 || kcat < 0)
3839 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3840 return &smack_known_web;
3841 return &smack_known_star;
3844 * Without guidance regarding the smack value
3845 * for the packet fall back on the network
3848 return smack_net_ambient;
3851 #if IS_ENABLED(CONFIG_IPV6)
3852 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3856 int proto = -EINVAL;
3857 struct ipv6hdr _ipv6h;
3858 struct ipv6hdr *ip6;
3860 struct tcphdr _tcph, *th;
3861 struct udphdr _udph, *uh;
3862 struct dccp_hdr _dccph, *dh;
3866 offset = skb_network_offset(skb);
3867 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3870 sip->sin6_addr = ip6->saddr;
3872 nexthdr = ip6->nexthdr;
3873 offset += sizeof(_ipv6h);
3874 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3881 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3883 sip->sin6_port = th->source;
3886 case IPPROTO_UDPLITE:
3887 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3889 sip->sin6_port = uh->source;
3892 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3894 sip->sin6_port = dh->dccph_sport;
3899 #endif /* CONFIG_IPV6 */
3902 * smack_from_skb - Smack data from the secmark in an skb
3905 * Returns smack_known of the secmark or NULL if that won't work.
3907 #ifdef CONFIG_NETWORK_SECMARK
3908 static struct smack_known *smack_from_skb(struct sk_buff *skb)
3910 if (skb == NULL || skb->secmark == 0)
3913 return smack_from_secid(skb->secmark);
3916 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3923 * smack_from_netlbl - Smack data from the IP options in an skb
3924 * @sk: socket data came in on
3925 * @family: address family
3928 * Find the Smack label in the IP options. If it hasn't been
3929 * added to the netlabel cache, add it here.
3931 * Returns smack_known of the IP options or NULL if that won't work.
3933 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
3934 struct sk_buff *skb)
3936 struct netlbl_lsm_secattr secattr;
3937 struct socket_smack *ssp = NULL;
3938 struct smack_known *skp = NULL;
3940 netlbl_secattr_init(&secattr);
3943 ssp = sk->sk_security;
3945 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
3946 skp = smack_from_secattr(&secattr, ssp);
3947 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
3948 netlbl_cache_add(skb, family, &skp->smk_netlabel);
3951 netlbl_secattr_destroy(&secattr);
3957 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3961 * Returns 0 if the packet should be delivered, an error code otherwise
3963 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3965 struct socket_smack *ssp = sk->sk_security;
3966 struct smack_known *skp = NULL;
3968 struct smk_audit_info ad;
3969 u16 family = sk->sk_family;
3971 struct lsm_network_audit net;
3973 #if IS_ENABLED(CONFIG_IPV6)
3974 struct sockaddr_in6 sadd;
3977 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3979 #endif /* CONFIG_IPV6 */
3984 * If there is a secmark use it rather than the CIPSO label.
3985 * If there is no secmark fall back to CIPSO.
3986 * The secmark is assumed to reflect policy better.
3988 skp = smack_from_skb(skb);
3990 skp = smack_from_netlbl(sk, family, skb);
3992 skp = smack_net_ambient;
3996 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3997 ad.a.u.net->family = family;
3998 ad.a.u.net->netif = skb->skb_iif;
3999 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4002 * Receiving a packet requires that the other end
4003 * be able to write here. Read access is not required.
4004 * This is the simplist possible security model
4007 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4008 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
4011 netlbl_skbuff_err(skb, family, rc, 0);
4013 #if IS_ENABLED(CONFIG_IPV6)
4015 proto = smk_skb_to_addr_ipv6(skb, &sadd);
4016 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
4017 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
4019 #ifdef SMACK_IPV6_SECMARK_LABELING
4020 skp = smack_from_skb(skb);
4022 if (smk_ipv6_localhost(&sadd))
4024 skp = smack_ipv6host_label(&sadd);
4026 skp = smack_net_ambient;
4029 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4030 ad.a.u.net->family = family;
4031 ad.a.u.net->netif = skb->skb_iif;
4032 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4033 #endif /* CONFIG_AUDIT */
4034 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4035 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4037 #endif /* SMACK_IPV6_SECMARK_LABELING */
4038 #ifdef SMACK_IPV6_PORT_LABELING
4039 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
4040 #endif /* SMACK_IPV6_PORT_LABELING */
4042 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4043 ICMPV6_ADM_PROHIBITED, 0);
4045 #endif /* CONFIG_IPV6 */
4052 * smack_socket_getpeersec_stream - pull in packet label
4054 * @optval: user's destination
4055 * @optlen: size thereof
4058 * returns zero on success, an error code otherwise
4060 static int smack_socket_getpeersec_stream(struct socket *sock,
4061 sockptr_t optval, sockptr_t optlen,
4064 struct socket_smack *ssp;
4069 ssp = sock->sk->sk_security;
4070 if (ssp->smk_packet != NULL) {
4071 rcp = ssp->smk_packet->smk_known;
4072 slen = strlen(rcp) + 1;
4079 if (copy_to_sockptr(optval, rcp, slen))
4082 if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
4089 * smack_socket_getpeersec_dgram - pull in packet label
4090 * @sock: the peer socket
4092 * @secid: pointer to where to put the secid of the packet
4094 * Sets the netlabel socket state on sk from parent
4096 static int smack_socket_getpeersec_dgram(struct socket *sock,
4097 struct sk_buff *skb, u32 *secid)
4100 struct socket_smack *ssp = NULL;
4101 struct smack_known *skp;
4102 struct sock *sk = NULL;
4103 int family = PF_UNSPEC;
4104 u32 s = 0; /* 0 is the invalid secid */
4107 if (skb->protocol == htons(ETH_P_IP))
4109 #if IS_ENABLED(CONFIG_IPV6)
4110 else if (skb->protocol == htons(ETH_P_IPV6))
4112 #endif /* CONFIG_IPV6 */
4114 if (family == PF_UNSPEC && sock != NULL)
4115 family = sock->sk->sk_family;
4119 ssp = sock->sk->sk_security;
4120 s = ssp->smk_out->smk_secid;
4123 skp = smack_from_skb(skb);
4129 * Translate what netlabel gave us.
4133 skp = smack_from_netlbl(sk, family, skb);
4138 #ifdef SMACK_IPV6_SECMARK_LABELING
4139 skp = smack_from_skb(skb);
4152 * smack_sock_graft - Initialize a newly created socket with an existing sock
4154 * @parent: parent socket
4156 * Set the smk_{in,out} state of an existing sock based on the process that
4157 * is creating the new socket.
4159 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4161 struct socket_smack *ssp;
4162 struct smack_known *skp = smk_of_current();
4165 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4168 ssp = sk->sk_security;
4171 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4175 * smack_inet_conn_request - Smack access check on connect
4176 * @sk: socket involved
4180 * Returns 0 if a task with the packet label could write to
4181 * the socket, otherwise an error code
4183 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4184 struct request_sock *req)
4186 u16 family = sk->sk_family;
4187 struct smack_known *skp;
4188 struct socket_smack *ssp = sk->sk_security;
4189 struct sockaddr_in addr;
4191 struct smack_known *hskp;
4193 struct smk_audit_info ad;
4195 struct lsm_network_audit net;
4198 #if IS_ENABLED(CONFIG_IPV6)
4199 if (family == PF_INET6) {
4201 * Handle mapped IPv4 packets arriving
4202 * via IPv6 sockets. Don't set up netlabel
4203 * processing on IPv6.
4205 if (skb->protocol == htons(ETH_P_IP))
4210 #endif /* CONFIG_IPV6 */
4213 * If there is a secmark use it rather than the CIPSO label.
4214 * If there is no secmark fall back to CIPSO.
4215 * The secmark is assumed to reflect policy better.
4217 skp = smack_from_skb(skb);
4219 skp = smack_from_netlbl(sk, family, skb);
4221 skp = &smack_known_huh;
4225 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4226 ad.a.u.net->family = family;
4227 ad.a.u.net->netif = skb->skb_iif;
4228 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4231 * Receiving a packet requires that the other end be able to write
4232 * here. Read access is not required.
4234 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4235 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4240 * Save the peer's label in the request_sock so we can later setup
4241 * smk_packet in the child socket so that SO_PEERCRED can report it.
4243 req->peer_secid = skp->smk_secid;
4246 * We need to decide if we want to label the incoming connection here
4247 * if we do we only need to label the request_sock and the stack will
4248 * propagate the wire-label to the sock when it is created.
4251 addr.sin_addr.s_addr = hdr->saddr;
4253 hskp = smack_ipv4host_label(&addr);
4257 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4259 netlbl_req_delattr(req);
4265 * smack_inet_csk_clone - Copy the connection information to the new socket
4266 * @sk: the new socket
4267 * @req: the connection's request_sock
4269 * Transfer the connection's peer label to the newly created socket.
4271 static void smack_inet_csk_clone(struct sock *sk,
4272 const struct request_sock *req)
4274 struct socket_smack *ssp = sk->sk_security;
4275 struct smack_known *skp;
4277 if (req->peer_secid != 0) {
4278 skp = smack_from_secid(req->peer_secid);
4279 ssp->smk_packet = skp;
4281 ssp->smk_packet = NULL;
4285 * Key management security hooks
4287 * Casey has not tested key support very heavily.
4288 * The permission check is most likely too restrictive.
4289 * If you care about keys please have a look.
4294 * smack_key_alloc - Set the key security blob
4296 * @cred: the credentials to use
4299 * No allocation required
4303 static int smack_key_alloc(struct key *key, const struct cred *cred,
4304 unsigned long flags)
4306 struct smack_known *skp = smk_of_task(smack_cred(cred));
4308 key->security = skp;
4313 * smack_key_free - Clear the key security blob
4316 * Clear the blob pointer
4318 static void smack_key_free(struct key *key)
4320 key->security = NULL;
4324 * smack_key_permission - Smack access on a key
4325 * @key_ref: gets to the object
4326 * @cred: the credentials to use
4327 * @need_perm: requested key permission
4329 * Return 0 if the task has read and write to the object,
4330 * an error code otherwise
4332 static int smack_key_permission(key_ref_t key_ref,
4333 const struct cred *cred,
4334 enum key_need_perm need_perm)
4337 struct smk_audit_info ad;
4338 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4343 * Validate requested permissions
4345 switch (need_perm) {
4347 case KEY_NEED_SEARCH:
4349 request |= MAY_READ;
4351 case KEY_NEED_WRITE:
4353 case KEY_NEED_SETATTR:
4354 request |= MAY_WRITE;
4356 case KEY_NEED_UNSPECIFIED:
4357 case KEY_NEED_UNLINK:
4358 case KEY_SYSADMIN_OVERRIDE:
4359 case KEY_AUTHTOKEN_OVERRIDE:
4360 case KEY_DEFER_PERM_CHECK:
4366 keyp = key_ref_to_ptr(key_ref);
4370 * If the key hasn't been initialized give it access so that
4373 if (keyp->security == NULL)
4376 * This should not occur
4381 if (smack_privileged(CAP_MAC_OVERRIDE))
4385 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4386 ad.a.u.key_struct.key = keyp->serial;
4387 ad.a.u.key_struct.key_desc = keyp->description;
4389 rc = smk_access(tkp, keyp->security, request, &ad);
4390 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4395 * smack_key_getsecurity - Smack label tagging the key
4396 * @key points to the key to be queried
4397 * @_buffer points to a pointer that should be set to point to the
4398 * resulting string (if no label or an error occurs).
4399 * Return the length of the string (including terminating NUL) or -ve if
4401 * May also return 0 (and a NULL buffer pointer) if there is no label.
4403 static int smack_key_getsecurity(struct key *key, char **_buffer)
4405 struct smack_known *skp = key->security;
4409 if (key->security == NULL) {
4414 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4417 length = strlen(copy) + 1;
4424 #ifdef CONFIG_KEY_NOTIFICATIONS
4426 * smack_watch_key - Smack access to watch a key for notifications.
4427 * @key: The key to be watched
4429 * Return 0 if the @watch->cred has permission to read from the key object and
4430 * an error otherwise.
4432 static int smack_watch_key(struct key *key)
4434 struct smk_audit_info ad;
4435 struct smack_known *tkp = smk_of_current();
4441 * If the key hasn't been initialized give it access so that
4444 if (key->security == NULL)
4447 * This should not occur
4452 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4456 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4457 ad.a.u.key_struct.key = key->serial;
4458 ad.a.u.key_struct.key_desc = key->description;
4460 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4461 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4464 #endif /* CONFIG_KEY_NOTIFICATIONS */
4465 #endif /* CONFIG_KEYS */
4467 #ifdef CONFIG_WATCH_QUEUE
4469 * smack_post_notification - Smack access to post a notification to a queue
4470 * @w_cred: The credentials of the watcher.
4471 * @cred: The credentials of the event source (may be NULL).
4472 * @n: The notification message to be posted.
4474 static int smack_post_notification(const struct cred *w_cred,
4475 const struct cred *cred,
4476 struct watch_notification *n)
4478 struct smk_audit_info ad;
4479 struct smack_known *subj, *obj;
4482 /* Always let maintenance notifications through. */
4483 if (n->type == WATCH_TYPE_META)
4488 subj = smk_of_task(smack_cred(cred));
4489 obj = smk_of_task(smack_cred(w_cred));
4491 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4492 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4493 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4496 #endif /* CONFIG_WATCH_QUEUE */
4501 * Audit requires a unique representation of each Smack specific
4502 * rule. This unique representation is used to distinguish the
4503 * object to be audited from remaining kernel objects and also
4504 * works as a glue between the audit hooks.
4506 * Since repository entries are added but never deleted, we'll use
4507 * the smack_known label address related to the given audit rule as
4508 * the needed unique representation. This also better fits the smack
4509 * model where nearly everything is a label.
4514 * smack_audit_rule_init - Initialize a smack audit rule
4515 * @field: audit rule fields given from user-space (audit.h)
4516 * @op: required testing operator (=, !=, >, <, ...)
4517 * @rulestr: smack label to be audited
4518 * @vrule: pointer to save our own audit rule representation
4520 * Prepare to audit cases where (@field @op @rulestr) is true.
4521 * The label to be audited is created if necessay.
4523 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4525 struct smack_known *skp;
4526 char **rule = (char **)vrule;
4529 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4532 if (op != Audit_equal && op != Audit_not_equal)
4535 skp = smk_import_entry(rulestr, 0);
4537 return PTR_ERR(skp);
4539 *rule = skp->smk_known;
4545 * smack_audit_rule_known - Distinguish Smack audit rules
4546 * @krule: rule of interest, in Audit kernel representation format
4548 * This is used to filter Smack rules from remaining Audit ones.
4549 * If it's proved that this rule belongs to us, the
4550 * audit_rule_match hook will be called to do the final judgement.
4552 static int smack_audit_rule_known(struct audit_krule *krule)
4554 struct audit_field *f;
4557 for (i = 0; i < krule->field_count; i++) {
4558 f = &krule->fields[i];
4560 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4568 * smack_audit_rule_match - Audit given object ?
4569 * @secid: security id for identifying the object to test
4570 * @field: audit rule flags given from user-space
4571 * @op: required testing operator
4572 * @vrule: smack internal rule presentation
4574 * The core Audit hook. It's used to take the decision of
4575 * whether to audit or not to audit a given object.
4577 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4579 struct smack_known *skp;
4582 if (unlikely(!rule)) {
4583 WARN_ONCE(1, "Smack: missing rule\n");
4587 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4590 skp = smack_from_secid(secid);
4593 * No need to do string comparisons. If a match occurs,
4594 * both pointers will point to the same smack_known
4597 if (op == Audit_equal)
4598 return (rule == skp->smk_known);
4599 if (op == Audit_not_equal)
4600 return (rule != skp->smk_known);
4606 * There is no need for a smack_audit_rule_free hook.
4607 * No memory was allocated.
4610 #endif /* CONFIG_AUDIT */
4613 * smack_ismaclabel - check if xattr @name references a smack MAC label
4614 * @name: Full xattr name to check.
4616 static int smack_ismaclabel(const char *name)
4618 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4623 * smack_secid_to_secctx - return the smack label for a secid
4624 * @secid: incoming integer
4625 * @secdata: destination
4626 * @seclen: how long it is
4628 * Exists for networking code.
4630 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4632 struct smack_known *skp = smack_from_secid(secid);
4635 *secdata = skp->smk_known;
4636 *seclen = strlen(skp->smk_known);
4641 * smack_secctx_to_secid - return the secid for a smack label
4642 * @secdata: smack label
4643 * @seclen: how long result is
4644 * @secid: outgoing integer
4646 * Exists for audit and networking code.
4648 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4650 struct smack_known *skp = smk_find_entry(secdata);
4653 *secid = skp->smk_secid;
4660 * There used to be a smack_release_secctx hook
4661 * that did nothing back when hooks were in a vector.
4662 * Now that there's a list such a hook adds cost.
4665 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4667 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4671 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4673 return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
4677 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4679 struct smack_known *skp = smk_of_inode(inode);
4681 *ctx = skp->smk_known;
4682 *ctxlen = strlen(skp->smk_known);
4686 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4689 struct task_smack *tsp;
4690 struct smack_known *skp;
4691 struct inode_smack *isp;
4692 struct cred *new_creds = *new;
4694 if (new_creds == NULL) {
4695 new_creds = prepare_creds();
4696 if (new_creds == NULL)
4700 tsp = smack_cred(new_creds);
4703 * Get label from overlay inode and set it in create_sid
4705 isp = smack_inode(d_inode(dentry));
4706 skp = isp->smk_inode;
4707 tsp->smk_task = skp;
4712 static int smack_inode_copy_up_xattr(const char *name)
4715 * Return 1 if this is the smack access Smack attribute.
4717 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4723 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4725 const struct cred *old,
4728 struct task_smack *otsp = smack_cred(old);
4729 struct task_smack *ntsp = smack_cred(new);
4730 struct inode_smack *isp;
4734 * Use the process credential unless all of
4735 * the transmuting criteria are met
4737 ntsp->smk_task = otsp->smk_task;
4740 * the attribute of the containing directory
4742 isp = smack_inode(d_inode(dentry->d_parent));
4744 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4746 may = smk_access_entry(otsp->smk_task->smk_known,
4747 isp->smk_inode->smk_known,
4748 &otsp->smk_task->smk_rules);
4752 * If the directory is transmuting and the rule
4753 * providing access is transmuting use the containing
4754 * directory label instead of the process label.
4756 if (may > 0 && (may & MAY_TRANSMUTE))
4757 ntsp->smk_task = isp->smk_inode;
4762 #ifdef CONFIG_IO_URING
4764 * smack_uring_override_creds - Is io_uring cred override allowed?
4765 * @new: the target creds
4767 * Check to see if the current task is allowed to override it's credentials
4768 * to service an io_uring operation.
4770 static int smack_uring_override_creds(const struct cred *new)
4772 struct task_smack *tsp = smack_cred(current_cred());
4773 struct task_smack *nsp = smack_cred(new);
4776 * Allow the degenerate case where the new Smack value is
4777 * the same as the current Smack value.
4779 if (tsp->smk_task == nsp->smk_task)
4782 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4789 * smack_uring_sqpoll - check if a io_uring polling thread can be created
4791 * Check to see if the current task is allowed to create a new io_uring
4792 * kernel polling thread.
4794 static int smack_uring_sqpoll(void)
4796 if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred()))
4803 * smack_uring_cmd - check on file operations for io_uring
4804 * @ioucmd: the command in question
4806 * Make a best guess about whether a io_uring "command" should
4807 * be allowed. Use the same logic used for determining if the
4808 * file could be opened for read in the absence of better criteria.
4810 static int smack_uring_cmd(struct io_uring_cmd *ioucmd)
4812 struct file *file = ioucmd->file;
4813 struct smk_audit_info ad;
4814 struct task_smack *tsp;
4815 struct inode *inode;
4821 tsp = smack_cred(file->f_cred);
4822 inode = file_inode(file);
4824 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
4825 smk_ad_setfield_u_fs_path(&ad, file->f_path);
4826 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
4827 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
4832 #endif /* CONFIG_IO_URING */
4834 struct lsm_blob_sizes smack_blob_sizes __ro_after_init = {
4835 .lbs_cred = sizeof(struct task_smack),
4836 .lbs_file = sizeof(struct smack_known *),
4837 .lbs_inode = sizeof(struct inode_smack),
4838 .lbs_ipc = sizeof(struct smack_known *),
4839 .lbs_msg_msg = sizeof(struct smack_known *),
4840 .lbs_superblock = sizeof(struct superblock_smack),
4843 static struct security_hook_list smack_hooks[] __ro_after_init = {
4844 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4845 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4846 LSM_HOOK_INIT(syslog, smack_syslog),
4848 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4849 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4851 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4852 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4853 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4854 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4855 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4857 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
4859 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4860 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4861 LSM_HOOK_INIT(inode_link, smack_inode_link),
4862 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4863 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4864 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4865 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4866 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4867 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4868 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4869 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4870 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4871 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4872 LSM_HOOK_INIT(inode_set_acl, smack_inode_set_acl),
4873 LSM_HOOK_INIT(inode_get_acl, smack_inode_get_acl),
4874 LSM_HOOK_INIT(inode_remove_acl, smack_inode_remove_acl),
4875 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4876 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4877 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4878 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4880 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4881 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4882 LSM_HOOK_INIT(file_lock, smack_file_lock),
4883 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4884 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4885 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4886 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4887 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4888 LSM_HOOK_INIT(file_receive, smack_file_receive),
4890 LSM_HOOK_INIT(file_open, smack_file_open),
4892 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4893 LSM_HOOK_INIT(cred_free, smack_cred_free),
4894 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4895 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4896 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4897 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4898 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4899 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4900 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4901 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4902 LSM_HOOK_INIT(current_getsecid_subj, smack_current_getsecid_subj),
4903 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
4904 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4905 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4906 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4907 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4908 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4909 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4910 LSM_HOOK_INIT(task_kill, smack_task_kill),
4911 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4913 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4914 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4916 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4918 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4919 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4920 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4921 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4922 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4924 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4925 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4926 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4927 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4929 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4930 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4931 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4932 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4934 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4936 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4937 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4939 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4940 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4942 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4943 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4944 #ifdef SMACK_IPV6_PORT_LABELING
4945 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4947 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4948 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4949 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4950 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4951 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4952 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4953 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4954 LSM_HOOK_INIT(sk_clone_security, smack_sk_clone_security),
4955 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4956 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4957 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4959 /* key management security hooks */
4961 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4962 LSM_HOOK_INIT(key_free, smack_key_free),
4963 LSM_HOOK_INIT(key_permission, smack_key_permission),
4964 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4965 #ifdef CONFIG_KEY_NOTIFICATIONS
4966 LSM_HOOK_INIT(watch_key, smack_watch_key),
4968 #endif /* CONFIG_KEYS */
4970 #ifdef CONFIG_WATCH_QUEUE
4971 LSM_HOOK_INIT(post_notification, smack_post_notification),
4976 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4977 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4978 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4979 #endif /* CONFIG_AUDIT */
4981 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4982 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4983 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4984 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4985 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4986 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4987 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4988 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4989 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4990 #ifdef CONFIG_IO_URING
4991 LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds),
4992 LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll),
4993 LSM_HOOK_INIT(uring_cmd, smack_uring_cmd),
4998 static __init void init_smack_known_list(void)
5001 * Initialize rule list locks
5003 mutex_init(&smack_known_huh.smk_rules_lock);
5004 mutex_init(&smack_known_hat.smk_rules_lock);
5005 mutex_init(&smack_known_floor.smk_rules_lock);
5006 mutex_init(&smack_known_star.smk_rules_lock);
5007 mutex_init(&smack_known_web.smk_rules_lock);
5009 * Initialize rule lists
5011 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
5012 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
5013 INIT_LIST_HEAD(&smack_known_star.smk_rules);
5014 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
5015 INIT_LIST_HEAD(&smack_known_web.smk_rules);
5017 * Create the known labels list
5019 smk_insert_entry(&smack_known_huh);
5020 smk_insert_entry(&smack_known_hat);
5021 smk_insert_entry(&smack_known_star);
5022 smk_insert_entry(&smack_known_floor);
5023 smk_insert_entry(&smack_known_web);
5027 * smack_init - initialize the smack system
5029 * Returns 0 on success, -ENOMEM is there's no memory
5031 static __init int smack_init(void)
5033 struct cred *cred = (struct cred *) current->cred;
5034 struct task_smack *tsp;
5036 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
5037 if (!smack_rule_cache)
5041 * Set the security state for the initial task.
5043 tsp = smack_cred(cred);
5044 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
5049 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
5052 pr_info("Smack: Initializing.\n");
5053 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
5054 pr_info("Smack: Netfilter enabled.\n");
5056 #ifdef SMACK_IPV6_PORT_LABELING
5057 pr_info("Smack: IPv6 port labeling enabled.\n");
5059 #ifdef SMACK_IPV6_SECMARK_LABELING
5060 pr_info("Smack: IPv6 Netfilter enabled.\n");
5063 /* initialize the smack_known_list */
5064 init_smack_known_list();
5070 * Smack requires early initialization in order to label
5071 * all processes and objects when they are created.
5073 DEFINE_LSM(smack) = {
5075 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
5076 .blobs = &smack_blob_sizes,