1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
8 * Casey Schaufler <casey@schaufler-ca.com>
9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
13 * Paul Moore <paul@paul-moore.com>
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, *fsfloor, *fshat, *fsroot, *fstransmute;
556 static void smack_free_mnt_opts(void *mnt_opts)
558 struct smack_mnt_opts *opts = mnt_opts;
559 kfree(opts->fsdefault);
560 kfree(opts->fsfloor);
563 kfree(opts->fstransmute);
567 static int smack_add_opt(int token, const char *s, void **mnt_opts)
569 struct smack_mnt_opts *opts = *mnt_opts;
572 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
601 case Opt_fstransmute:
602 if (opts->fstransmute)
604 opts->fstransmute = s;
610 pr_warn("Smack: duplicate mount options\n");
615 * smack_fs_context_submount - Initialise security data for a filesystem context
616 * @fc: The filesystem context.
617 * @reference: reference superblock
619 * Returns 0 on success or -ENOMEM on error.
621 static int smack_fs_context_submount(struct fs_context *fc,
622 struct super_block *reference)
624 struct superblock_smack *sbsp;
625 struct smack_mnt_opts *ctx;
626 struct inode_smack *isp;
628 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
633 sbsp = smack_superblock(reference);
634 isp = smack_inode(reference->s_root->d_inode);
636 if (sbsp->smk_default) {
637 ctx->fsdefault = kstrdup(sbsp->smk_default->smk_known, GFP_KERNEL);
642 if (sbsp->smk_floor) {
643 ctx->fsfloor = kstrdup(sbsp->smk_floor->smk_known, GFP_KERNEL);
649 ctx->fshat = kstrdup(sbsp->smk_hat->smk_known, GFP_KERNEL);
654 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
655 if (sbsp->smk_root) {
656 ctx->fstransmute = kstrdup(sbsp->smk_root->smk_known, GFP_KERNEL);
657 if (!ctx->fstransmute)
665 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
666 * @fc: The new filesystem context.
667 * @src_fc: The source filesystem context being duplicated.
669 * Returns 0 on success or -ENOMEM on error.
671 static int smack_fs_context_dup(struct fs_context *fc,
672 struct fs_context *src_fc)
674 struct smack_mnt_opts *dst, *src = src_fc->security;
679 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
684 if (src->fsdefault) {
685 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
690 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
695 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
700 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
704 if (src->fstransmute) {
705 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
706 if (!dst->fstransmute)
712 static const struct fs_parameter_spec smack_fs_parameters[] = {
713 fsparam_string("smackfsdef", Opt_fsdefault),
714 fsparam_string("smackfsdefault", Opt_fsdefault),
715 fsparam_string("smackfsfloor", Opt_fsfloor),
716 fsparam_string("smackfshat", Opt_fshat),
717 fsparam_string("smackfsroot", Opt_fsroot),
718 fsparam_string("smackfstransmute", Opt_fstransmute),
723 * smack_fs_context_parse_param - Parse a single mount parameter
724 * @fc: The new filesystem context being constructed.
725 * @param: The parameter.
727 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
730 static int smack_fs_context_parse_param(struct fs_context *fc,
731 struct fs_parameter *param)
733 struct fs_parse_result result;
736 opt = fs_parse(fc, smack_fs_parameters, param, &result);
740 rc = smack_add_opt(opt, param->string, &fc->security);
742 param->string = NULL;
746 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
748 char *from = options, *to = options;
752 char *next = strchr(from, ',');
761 token = match_opt_prefix(from, len, &arg);
762 if (token != Opt_error) {
763 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
764 rc = smack_add_opt(token, arg, mnt_opts);
768 smack_free_mnt_opts(*mnt_opts);
773 if (!first) { // copy with preceding comma
778 memmove(to, from, len);
791 * smack_set_mnt_opts - set Smack specific mount options
792 * @sb: the file system superblock
793 * @mnt_opts: Smack mount options
794 * @kern_flags: mount option from kernel space or user space
795 * @set_kern_flags: where to store converted mount opts
797 * Returns 0 on success, an error code on failure
799 * Allow filesystems with binary mount data to explicitly set Smack mount
802 static int smack_set_mnt_opts(struct super_block *sb,
804 unsigned long kern_flags,
805 unsigned long *set_kern_flags)
807 struct dentry *root = sb->s_root;
808 struct inode *inode = d_backing_inode(root);
809 struct superblock_smack *sp = smack_superblock(sb);
810 struct inode_smack *isp;
811 struct smack_known *skp;
812 struct smack_mnt_opts *opts = mnt_opts;
813 bool transmute = false;
815 if (sp->smk_flags & SMK_SB_INITIALIZED)
818 if (!smack_privileged(CAP_MAC_ADMIN)) {
820 * Unprivileged mounts don't get to specify Smack values.
825 * Unprivileged mounts get root and default from the caller.
827 skp = smk_of_current();
829 sp->smk_default = skp;
831 * For a handful of fs types with no user-controlled
832 * backing store it's okay to trust security labels
833 * in the filesystem. The rest are untrusted.
835 if (sb->s_user_ns != &init_user_ns &&
836 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
837 sb->s_magic != RAMFS_MAGIC) {
839 sp->smk_flags |= SMK_SB_UNTRUSTED;
843 sp->smk_flags |= SMK_SB_INITIALIZED;
846 if (opts->fsdefault) {
847 skp = smk_import_entry(opts->fsdefault, 0);
850 sp->smk_default = skp;
853 skp = smk_import_entry(opts->fsfloor, 0);
859 skp = smk_import_entry(opts->fshat, 0);
865 skp = smk_import_entry(opts->fsroot, 0);
870 if (opts->fstransmute) {
871 skp = smk_import_entry(opts->fstransmute, 0);
880 * Initialize the root inode.
882 init_inode_smack(inode, sp->smk_root);
885 isp = smack_inode(inode);
886 isp->smk_flags |= SMK_INODE_TRANSMUTE;
893 * smack_sb_statfs - Smack check on statfs
894 * @dentry: identifies the file system in question
896 * Returns 0 if current can read the floor of the filesystem,
897 * and error code otherwise
899 static int smack_sb_statfs(struct dentry *dentry)
901 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
903 struct smk_audit_info ad;
905 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
906 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
908 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
909 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
918 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
919 * @bprm: the exec information
921 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
923 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
925 struct inode *inode = file_inode(bprm->file);
926 struct task_smack *bsp = smack_cred(bprm->cred);
927 struct inode_smack *isp;
928 struct superblock_smack *sbsp;
931 isp = smack_inode(inode);
932 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
935 sbsp = smack_superblock(inode->i_sb);
936 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
937 isp->smk_task != sbsp->smk_root)
940 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
941 struct task_struct *tracer;
945 tracer = ptrace_parent(current);
946 if (likely(tracer != NULL))
947 rc = smk_ptrace_rule_check(tracer,
956 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
959 bsp->smk_task = isp->smk_task;
960 bprm->per_clear |= PER_CLEAR_ON_SETID;
962 /* Decide if this is a secure exec. */
963 if (bsp->smk_task != bsp->smk_forked)
964 bprm->secureexec = 1;
974 * smack_inode_alloc_security - allocate an inode blob
975 * @inode: the inode in need of a blob
979 static int smack_inode_alloc_security(struct inode *inode)
981 struct smack_known *skp = smk_of_current();
983 init_inode_smack(inode, skp);
988 * smack_inode_init_security - copy out the smack from an inode
989 * @inode: the newly created inode
990 * @dir: containing directory object
992 * @name: where to put the attribute name
993 * @value: where to put the attribute value
994 * @len: where to put the length of the attribute
996 * Returns 0 if it all works out, -ENOMEM if there's no memory
998 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
999 const struct qstr *qstr, const char **name,
1000 void **value, size_t *len)
1002 struct task_smack *tsp = smack_cred(current_cred());
1003 struct inode_smack *issp = smack_inode(inode);
1004 struct smack_known *skp = smk_of_task(tsp);
1005 struct smack_known *isp = smk_of_inode(inode);
1006 struct smack_known *dsp = smk_of_inode(dir);
1010 *name = XATTR_SMACK_SUFFIX;
1014 * If equal, transmuting already occurred in
1015 * smack_dentry_create_files_as(). No need to check again.
1017 if (tsp->smk_task != tsp->smk_transmuted) {
1019 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1025 * In addition to having smk_task equal to smk_transmuted,
1026 * if the access rule allows transmutation and the directory
1027 * requests transmutation then by all means transmute.
1028 * Mark the inode as changed.
1030 if ((tsp->smk_task == tsp->smk_transmuted) ||
1031 (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1032 smk_inode_transmutable(dir))) {
1034 * The caller of smack_dentry_create_files_as()
1035 * should have overridden the current cred, so the
1036 * inode label was already set correctly in
1037 * smack_inode_alloc_security().
1039 if (tsp->smk_task != tsp->smk_transmuted)
1041 issp->smk_flags |= SMK_INODE_CHANGED;
1044 *value = kstrdup(isp->smk_known, GFP_NOFS);
1048 *len = strlen(isp->smk_known);
1055 * smack_inode_link - Smack check on link
1056 * @old_dentry: the existing object
1058 * @new_dentry: the new object
1060 * Returns 0 if access is permitted, an error code otherwise
1062 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1063 struct dentry *new_dentry)
1065 struct smack_known *isp;
1066 struct smk_audit_info ad;
1069 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1070 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1072 isp = smk_of_inode(d_backing_inode(old_dentry));
1073 rc = smk_curacc(isp, MAY_WRITE, &ad);
1074 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1076 if (rc == 0 && d_is_positive(new_dentry)) {
1077 isp = smk_of_inode(d_backing_inode(new_dentry));
1078 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1079 rc = smk_curacc(isp, MAY_WRITE, &ad);
1080 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1087 * smack_inode_unlink - Smack check on inode deletion
1088 * @dir: containing directory object
1089 * @dentry: file to unlink
1091 * Returns 0 if current can write the containing directory
1092 * and the object, error code otherwise
1094 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1096 struct inode *ip = d_backing_inode(dentry);
1097 struct smk_audit_info ad;
1100 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1101 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1104 * You need write access to the thing you're unlinking
1106 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1107 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1110 * You also need write access to the containing directory
1112 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1113 smk_ad_setfield_u_fs_inode(&ad, dir);
1114 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1115 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1121 * smack_inode_rmdir - Smack check on directory deletion
1122 * @dir: containing directory object
1123 * @dentry: directory to unlink
1125 * Returns 0 if current can write the containing directory
1126 * and the directory, error code otherwise
1128 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1130 struct smk_audit_info ad;
1133 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1134 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1137 * You need write access to the thing you're removing
1139 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1140 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1143 * You also need write access to the containing directory
1145 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1146 smk_ad_setfield_u_fs_inode(&ad, dir);
1147 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1148 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1155 * smack_inode_rename - Smack check on rename
1156 * @old_inode: unused
1157 * @old_dentry: the old object
1158 * @new_inode: unused
1159 * @new_dentry: the new object
1161 * Read and write access is required on both the old and
1164 * Returns 0 if access is permitted, an error code otherwise
1166 static int smack_inode_rename(struct inode *old_inode,
1167 struct dentry *old_dentry,
1168 struct inode *new_inode,
1169 struct dentry *new_dentry)
1172 struct smack_known *isp;
1173 struct smk_audit_info ad;
1175 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1176 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1178 isp = smk_of_inode(d_backing_inode(old_dentry));
1179 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1180 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1182 if (rc == 0 && d_is_positive(new_dentry)) {
1183 isp = smk_of_inode(d_backing_inode(new_dentry));
1184 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1185 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1186 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1192 * smack_inode_permission - Smack version of permission()
1193 * @inode: the inode in question
1194 * @mask: the access requested
1196 * This is the important Smack hook.
1198 * Returns 0 if access is permitted, an error code otherwise
1200 static int smack_inode_permission(struct inode *inode, int mask)
1202 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
1203 struct smk_audit_info ad;
1204 int no_block = mask & MAY_NOT_BLOCK;
1207 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1209 * No permission to check. Existence test. Yup, it's there.
1214 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1215 if (smk_of_inode(inode) != sbsp->smk_root)
1219 /* May be droppable after audit */
1222 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1223 smk_ad_setfield_u_fs_inode(&ad, inode);
1224 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1225 rc = smk_bu_inode(inode, mask, rc);
1230 * smack_inode_setattr - Smack check for setting attributes
1231 * @dentry: the object
1232 * @iattr: for the force flag
1234 * Returns 0 if access is permitted, an error code otherwise
1236 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1238 struct smk_audit_info ad;
1242 * Need to allow for clearing the setuid bit.
1244 if (iattr->ia_valid & ATTR_FORCE)
1246 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1247 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1249 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1250 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1255 * smack_inode_getattr - Smack check for getting attributes
1256 * @path: path to extract the info from
1258 * Returns 0 if access is permitted, an error code otherwise
1260 static int smack_inode_getattr(const struct path *path)
1262 struct smk_audit_info ad;
1263 struct inode *inode = d_backing_inode(path->dentry);
1266 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1267 smk_ad_setfield_u_fs_path(&ad, *path);
1268 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1269 rc = smk_bu_inode(inode, MAY_READ, rc);
1274 * smack_inode_setxattr - Smack check for setting xattrs
1275 * @mnt_userns: active user namespace
1276 * @dentry: the object
1277 * @name: name of the attribute
1278 * @value: value of the attribute
1279 * @size: size of the value
1282 * This protects the Smack attribute explicitly.
1284 * Returns 0 if access is permitted, an error code otherwise
1286 static int smack_inode_setxattr(struct user_namespace *mnt_userns,
1287 struct dentry *dentry, const char *name,
1288 const void *value, size_t size, int flags)
1290 struct smk_audit_info ad;
1291 struct smack_known *skp;
1293 int check_import = 0;
1298 * Check label validity here so import won't fail in post_setxattr
1300 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1301 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1302 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1305 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1306 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1310 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1312 if (size != TRANS_TRUE_SIZE ||
1313 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1316 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1318 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1321 if (rc == 0 && check_import) {
1322 skp = size ? smk_import_entry(value, size) : NULL;
1325 else if (skp == NULL || (check_star &&
1326 (skp == &smack_known_star || skp == &smack_known_web)))
1330 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1331 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1334 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1335 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1342 * smack_inode_post_setxattr - Apply the Smack update approved above
1344 * @name: attribute name
1345 * @value: attribute value
1346 * @size: attribute size
1349 * Set the pointer in the inode blob to the entry found
1350 * in the master label list.
1352 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1353 const void *value, size_t size, int flags)
1355 struct smack_known *skp;
1356 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1358 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1359 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1363 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1364 skp = smk_import_entry(value, size);
1366 isp->smk_inode = skp;
1367 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1368 skp = smk_import_entry(value, size);
1370 isp->smk_task = skp;
1371 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1372 skp = smk_import_entry(value, size);
1374 isp->smk_mmap = skp;
1381 * smack_inode_getxattr - Smack check on getxattr
1382 * @dentry: the object
1385 * Returns 0 if access is permitted, an error code otherwise
1387 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1389 struct smk_audit_info ad;
1392 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1393 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1395 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1396 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1401 * smack_inode_removexattr - Smack check on removexattr
1402 * @mnt_userns: active user namespace
1403 * @dentry: the object
1404 * @name: name of the attribute
1406 * Removing the Smack attribute requires CAP_MAC_ADMIN
1408 * Returns 0 if access is permitted, an error code otherwise
1410 static int smack_inode_removexattr(struct user_namespace *mnt_userns,
1411 struct dentry *dentry, const char *name)
1413 struct inode_smack *isp;
1414 struct smk_audit_info ad;
1417 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1418 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1419 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1420 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1421 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1422 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1423 if (!smack_privileged(CAP_MAC_ADMIN))
1426 rc = cap_inode_removexattr(mnt_userns, dentry, name);
1431 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1432 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1434 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1435 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1439 isp = smack_inode(d_backing_inode(dentry));
1441 * Don't do anything special for these.
1442 * XATTR_NAME_SMACKIPIN
1443 * XATTR_NAME_SMACKIPOUT
1445 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1446 struct super_block *sbp = dentry->d_sb;
1447 struct superblock_smack *sbsp = smack_superblock(sbp);
1449 isp->smk_inode = sbsp->smk_default;
1450 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1451 isp->smk_task = NULL;
1452 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1453 isp->smk_mmap = NULL;
1454 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1455 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1461 * smack_inode_getsecurity - get smack xattrs
1462 * @mnt_userns: active user namespace
1463 * @inode: the object
1464 * @name: attribute name
1465 * @buffer: where to put the result
1466 * @alloc: duplicate memory
1468 * Returns the size of the attribute or an error code
1470 static int smack_inode_getsecurity(struct user_namespace *mnt_userns,
1471 struct inode *inode, const char *name,
1472 void **buffer, bool alloc)
1474 struct socket_smack *ssp;
1475 struct socket *sock;
1476 struct super_block *sbp;
1477 struct inode *ip = (struct inode *)inode;
1478 struct smack_known *isp;
1479 struct inode_smack *ispp;
1483 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1484 isp = smk_of_inode(inode);
1485 } else if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) {
1486 ispp = smack_inode(inode);
1487 if (ispp->smk_flags & SMK_INODE_TRANSMUTE)
1493 * The rest of the Smack xattrs are only on sockets.
1496 if (sbp->s_magic != SOCKFS_MAGIC)
1499 sock = SOCKET_I(ip);
1500 if (sock == NULL || sock->sk == NULL)
1503 ssp = sock->sk->sk_security;
1505 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1507 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1514 label = isp->smk_known;
1516 label_len = strlen(label);
1519 *buffer = kstrdup(label, GFP_KERNEL);
1520 if (*buffer == NULL)
1529 * smack_inode_listsecurity - list the Smack attributes
1530 * @inode: the object
1531 * @buffer: where they go
1532 * @buffer_size: size of buffer
1534 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1537 int len = sizeof(XATTR_NAME_SMACK);
1539 if (buffer != NULL && len <= buffer_size)
1540 memcpy(buffer, XATTR_NAME_SMACK, len);
1546 * smack_inode_getsecid - Extract inode's security id
1547 * @inode: inode to extract the info from
1548 * @secid: where result will be saved
1550 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1552 struct smack_known *skp = smk_of_inode(inode);
1554 *secid = skp->smk_secid;
1562 * There is no smack_file_permission hook
1564 * Should access checks be done on each read or write?
1565 * UNICOS and SELinux say yes.
1566 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1568 * I'll say no for now. Smack does not do the frequent
1569 * label changing that SELinux does.
1573 * smack_file_alloc_security - assign a file security blob
1576 * The security blob for a file is a pointer to the master
1577 * label list, so no allocation is done.
1579 * f_security is the owner security information. It
1580 * isn't used on file access checks, it's for send_sigio.
1584 static int smack_file_alloc_security(struct file *file)
1586 struct smack_known **blob = smack_file(file);
1588 *blob = smk_of_current();
1593 * smack_file_ioctl - Smack check on ioctls
1598 * Relies heavily on the correct use of the ioctl command conventions.
1600 * Returns 0 if allowed, error code otherwise
1602 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1606 struct smk_audit_info ad;
1607 struct inode *inode = file_inode(file);
1609 if (unlikely(IS_PRIVATE(inode)))
1612 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1613 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1615 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1616 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1617 rc = smk_bu_file(file, MAY_WRITE, rc);
1620 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1621 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1622 rc = smk_bu_file(file, MAY_READ, rc);
1629 * smack_file_lock - Smack check on file locking
1633 * Returns 0 if current has lock access, error code otherwise
1635 static int smack_file_lock(struct file *file, unsigned int cmd)
1637 struct smk_audit_info ad;
1639 struct inode *inode = file_inode(file);
1641 if (unlikely(IS_PRIVATE(inode)))
1644 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1645 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1646 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1647 rc = smk_bu_file(file, MAY_LOCK, rc);
1652 * smack_file_fcntl - Smack check on fcntl
1654 * @cmd: what action to check
1657 * Generally these operations are harmless.
1658 * File locking operations present an obvious mechanism
1659 * for passing information, so they require write access.
1661 * Returns 0 if current has access, error code otherwise
1663 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1666 struct smk_audit_info ad;
1668 struct inode *inode = file_inode(file);
1670 if (unlikely(IS_PRIVATE(inode)))
1678 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1679 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1680 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1681 rc = smk_bu_file(file, MAY_LOCK, rc);
1685 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1686 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1687 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1688 rc = smk_bu_file(file, MAY_WRITE, rc);
1698 * smack_mmap_file - Check permissions for a mmap operation.
1699 * @file: contains the file structure for file to map (may be NULL).
1700 * @reqprot: contains the protection requested by the application.
1701 * @prot: contains the protection that will be applied by the kernel.
1702 * @flags: contains the operational flags.
1704 * The @file may be NULL, e.g. if mapping anonymous memory.
1706 * Return 0 if permission is granted.
1708 static int smack_mmap_file(struct file *file,
1709 unsigned long reqprot, unsigned long prot,
1710 unsigned long flags)
1712 struct smack_known *skp;
1713 struct smack_known *mkp;
1714 struct smack_rule *srp;
1715 struct task_smack *tsp;
1716 struct smack_known *okp;
1717 struct inode_smack *isp;
1718 struct superblock_smack *sbsp;
1727 if (unlikely(IS_PRIVATE(file_inode(file))))
1730 isp = smack_inode(file_inode(file));
1731 if (isp->smk_mmap == NULL)
1733 sbsp = smack_superblock(file_inode(file)->i_sb);
1734 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1735 isp->smk_mmap != sbsp->smk_root)
1737 mkp = isp->smk_mmap;
1739 tsp = smack_cred(current_cred());
1740 skp = smk_of_current();
1745 * For each Smack rule associated with the subject
1746 * label verify that the SMACK64MMAP also has access
1747 * to that rule's object label.
1749 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1750 okp = srp->smk_object;
1752 * Matching labels always allows access.
1754 if (mkp->smk_known == okp->smk_known)
1757 * If there is a matching local rule take
1758 * that into account as well.
1760 may = smk_access_entry(srp->smk_subject->smk_known,
1764 may = srp->smk_access;
1766 may &= srp->smk_access;
1768 * If may is zero the SMACK64MMAP subject can't
1769 * possibly have less access.
1775 * Fetch the global list entry.
1776 * If there isn't one a SMACK64MMAP subject
1777 * can't have as much access as current.
1779 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1781 if (mmay == -ENOENT) {
1786 * If there is a local entry it modifies the
1787 * potential access, too.
1789 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1791 if (tmay != -ENOENT)
1795 * If there is any access available to current that is
1796 * not available to a SMACK64MMAP subject
1799 if ((may | mmay) != mmay) {
1811 * smack_file_set_fowner - set the file security blob value
1812 * @file: object in question
1815 static void smack_file_set_fowner(struct file *file)
1817 struct smack_known **blob = smack_file(file);
1819 *blob = smk_of_current();
1823 * smack_file_send_sigiotask - Smack on sigio
1824 * @tsk: The target task
1825 * @fown: the object the signal come from
1828 * Allow a privileged task to get signals even if it shouldn't
1830 * Returns 0 if a subject with the object's smack could
1831 * write to the task, an error code otherwise.
1833 static int smack_file_send_sigiotask(struct task_struct *tsk,
1834 struct fown_struct *fown, int signum)
1836 struct smack_known **blob;
1837 struct smack_known *skp;
1838 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1839 const struct cred *tcred;
1842 struct smk_audit_info ad;
1845 * struct fown_struct is never outside the context of a struct file
1847 file = container_of(fown, struct file, f_owner);
1849 /* we don't log here as rc can be overriden */
1850 blob = smack_file(file);
1852 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1853 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1856 tcred = __task_cred(tsk);
1857 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1861 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1862 smk_ad_setfield_u_tsk(&ad, tsk);
1863 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1868 * smack_file_receive - Smack file receive check
1871 * Returns 0 if current has access, error code otherwise
1873 static int smack_file_receive(struct file *file)
1877 struct smk_audit_info ad;
1878 struct inode *inode = file_inode(file);
1879 struct socket *sock;
1880 struct task_smack *tsp;
1881 struct socket_smack *ssp;
1883 if (unlikely(IS_PRIVATE(inode)))
1886 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1887 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1889 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1890 sock = SOCKET_I(inode);
1891 ssp = sock->sk->sk_security;
1892 tsp = smack_cred(current_cred());
1894 * If the receiving process can't write to the
1895 * passed socket or if the passed socket can't
1896 * write to the receiving process don't accept
1897 * the passed socket.
1899 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1900 rc = smk_bu_file(file, may, rc);
1903 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1904 rc = smk_bu_file(file, may, rc);
1908 * This code relies on bitmasks.
1910 if (file->f_mode & FMODE_READ)
1912 if (file->f_mode & FMODE_WRITE)
1915 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1916 rc = smk_bu_file(file, may, rc);
1921 * smack_file_open - Smack dentry open processing
1924 * Set the security blob in the file structure.
1925 * Allow the open only if the task has read access. There are
1926 * many read operations (e.g. fstat) that you can do with an
1927 * fd even if you have the file open write-only.
1929 * Returns 0 if current has access, error code otherwise
1931 static int smack_file_open(struct file *file)
1933 struct task_smack *tsp = smack_cred(file->f_cred);
1934 struct inode *inode = file_inode(file);
1935 struct smk_audit_info ad;
1938 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1939 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1940 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1941 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1951 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1952 * @cred: the new credentials
1953 * @gfp: the atomicity of any memory allocations
1955 * Prepare a blank set of credentials for modification. This must allocate all
1956 * the memory the LSM module might require such that cred_transfer() can
1957 * complete without error.
1959 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1961 init_task_smack(smack_cred(cred), NULL, NULL);
1967 * smack_cred_free - "free" task-level security credentials
1968 * @cred: the credentials in question
1971 static void smack_cred_free(struct cred *cred)
1973 struct task_smack *tsp = smack_cred(cred);
1974 struct smack_rule *rp;
1975 struct list_head *l;
1976 struct list_head *n;
1978 smk_destroy_label_list(&tsp->smk_relabel);
1980 list_for_each_safe(l, n, &tsp->smk_rules) {
1981 rp = list_entry(l, struct smack_rule, list);
1982 list_del(&rp->list);
1983 kmem_cache_free(smack_rule_cache, rp);
1988 * smack_cred_prepare - prepare new set of credentials for modification
1989 * @new: the new credentials
1990 * @old: the original credentials
1991 * @gfp: the atomicity of any memory allocations
1993 * Prepare a new set of credentials for modification.
1995 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1998 struct task_smack *old_tsp = smack_cred(old);
1999 struct task_smack *new_tsp = smack_cred(new);
2002 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
2004 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2008 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2014 * smack_cred_transfer - Transfer the old credentials to the new credentials
2015 * @new: the new credentials
2016 * @old: the original credentials
2018 * Fill in a set of blank credentials from another set of credentials.
2020 static void smack_cred_transfer(struct cred *new, const struct cred *old)
2022 struct task_smack *old_tsp = smack_cred(old);
2023 struct task_smack *new_tsp = smack_cred(new);
2025 new_tsp->smk_task = old_tsp->smk_task;
2026 new_tsp->smk_forked = old_tsp->smk_task;
2027 mutex_init(&new_tsp->smk_rules_lock);
2028 INIT_LIST_HEAD(&new_tsp->smk_rules);
2030 /* cbs copy rule list */
2034 * smack_cred_getsecid - get the secid corresponding to a creds structure
2035 * @cred: the object creds
2036 * @secid: where to put the result
2038 * Sets the secid to contain a u32 version of the smack label.
2040 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
2042 struct smack_known *skp;
2045 skp = smk_of_task(smack_cred(cred));
2046 *secid = skp->smk_secid;
2051 * smack_kernel_act_as - Set the subjective context in a set of credentials
2052 * @new: points to the set of credentials to be modified.
2053 * @secid: specifies the security ID to be set
2055 * Set the security data for a kernel service.
2057 static int smack_kernel_act_as(struct cred *new, u32 secid)
2059 struct task_smack *new_tsp = smack_cred(new);
2061 new_tsp->smk_task = smack_from_secid(secid);
2066 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2067 * @new: points to the set of credentials to be modified
2068 * @inode: points to the inode to use as a reference
2070 * Set the file creation context in a set of credentials to the same
2071 * as the objective context of the specified inode
2073 static int smack_kernel_create_files_as(struct cred *new,
2074 struct inode *inode)
2076 struct inode_smack *isp = smack_inode(inode);
2077 struct task_smack *tsp = smack_cred(new);
2079 tsp->smk_forked = isp->smk_inode;
2080 tsp->smk_task = tsp->smk_forked;
2085 * smk_curacc_on_task - helper to log task related access
2086 * @p: the task object
2087 * @access: the access requested
2088 * @caller: name of the calling function for audit
2090 * Return 0 if access is permitted
2092 static int smk_curacc_on_task(struct task_struct *p, int access,
2095 struct smk_audit_info ad;
2096 struct smack_known *skp = smk_of_task_struct_obj(p);
2099 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2100 smk_ad_setfield_u_tsk(&ad, p);
2101 rc = smk_curacc(skp, access, &ad);
2102 rc = smk_bu_task(p, access, rc);
2107 * smack_task_setpgid - Smack check on setting pgid
2108 * @p: the task object
2111 * Return 0 if write access is permitted
2113 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2115 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2119 * smack_task_getpgid - Smack access check for getpgid
2120 * @p: the object task
2122 * Returns 0 if current can read the object task, error code otherwise
2124 static int smack_task_getpgid(struct task_struct *p)
2126 return smk_curacc_on_task(p, MAY_READ, __func__);
2130 * smack_task_getsid - Smack access check for getsid
2131 * @p: the object task
2133 * Returns 0 if current can read the object task, error code otherwise
2135 static int smack_task_getsid(struct task_struct *p)
2137 return smk_curacc_on_task(p, MAY_READ, __func__);
2141 * smack_current_getsecid_subj - get the subjective secid of the current task
2142 * @secid: where to put the result
2144 * Sets the secid to contain a u32 version of the task's subjective smack label.
2146 static void smack_current_getsecid_subj(u32 *secid)
2148 struct smack_known *skp = smk_of_current();
2150 *secid = skp->smk_secid;
2154 * smack_task_getsecid_obj - get the objective secid of the task
2156 * @secid: where to put the result
2158 * Sets the secid to contain a u32 version of the task's objective smack label.
2160 static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2162 struct smack_known *skp = smk_of_task_struct_obj(p);
2164 *secid = skp->smk_secid;
2168 * smack_task_setnice - Smack check on setting nice
2169 * @p: the task object
2172 * Return 0 if write access is permitted
2174 static int smack_task_setnice(struct task_struct *p, int nice)
2176 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2180 * smack_task_setioprio - Smack check on setting ioprio
2181 * @p: the task object
2184 * Return 0 if write access is permitted
2186 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2188 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2192 * smack_task_getioprio - Smack check on reading ioprio
2193 * @p: the task object
2195 * Return 0 if read access is permitted
2197 static int smack_task_getioprio(struct task_struct *p)
2199 return smk_curacc_on_task(p, MAY_READ, __func__);
2203 * smack_task_setscheduler - Smack check on setting scheduler
2204 * @p: the task object
2206 * Return 0 if read access is permitted
2208 static int smack_task_setscheduler(struct task_struct *p)
2210 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2214 * smack_task_getscheduler - Smack check on reading scheduler
2215 * @p: the task object
2217 * Return 0 if read access is permitted
2219 static int smack_task_getscheduler(struct task_struct *p)
2221 return smk_curacc_on_task(p, MAY_READ, __func__);
2225 * smack_task_movememory - Smack check on moving memory
2226 * @p: the task object
2228 * Return 0 if write access is permitted
2230 static int smack_task_movememory(struct task_struct *p)
2232 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2236 * smack_task_kill - Smack check on signal delivery
2237 * @p: the task object
2240 * @cred: identifies the cred to use in lieu of current's
2242 * Return 0 if write access is permitted
2245 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2246 int sig, const struct cred *cred)
2248 struct smk_audit_info ad;
2249 struct smack_known *skp;
2250 struct smack_known *tkp = smk_of_task_struct_obj(p);
2254 return 0; /* null signal; existence test */
2256 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2257 smk_ad_setfield_u_tsk(&ad, p);
2259 * Sending a signal requires that the sender
2260 * can write the receiver.
2263 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2264 rc = smk_bu_task(p, MAY_DELIVER, rc);
2268 * If the cred isn't NULL we're dealing with some USB IO
2269 * specific behavior. This is not clean. For one thing
2270 * we can't take privilege into account.
2272 skp = smk_of_task(smack_cred(cred));
2273 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2274 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2279 * smack_task_to_inode - copy task smack into the inode blob
2280 * @p: task to copy from
2281 * @inode: inode to copy to
2283 * Sets the smack pointer in the inode security blob
2285 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2287 struct inode_smack *isp = smack_inode(inode);
2288 struct smack_known *skp = smk_of_task_struct_obj(p);
2290 isp->smk_inode = skp;
2291 isp->smk_flags |= SMK_INODE_INSTANT;
2299 * smack_sk_alloc_security - Allocate a socket blob
2302 * @gfp_flags: memory allocation flags
2304 * Assign Smack pointers to current
2306 * Returns 0 on success, -ENOMEM is there's no memory
2308 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2310 struct smack_known *skp = smk_of_current();
2311 struct socket_smack *ssp;
2313 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2318 * Sockets created by kernel threads receive web label.
2320 if (unlikely(current->flags & PF_KTHREAD)) {
2321 ssp->smk_in = &smack_known_web;
2322 ssp->smk_out = &smack_known_web;
2327 ssp->smk_packet = NULL;
2329 sk->sk_security = ssp;
2335 * smack_sk_free_security - Free a socket blob
2338 * Clears the blob pointer
2340 static void smack_sk_free_security(struct sock *sk)
2342 #ifdef SMACK_IPV6_PORT_LABELING
2343 struct smk_port_label *spp;
2345 if (sk->sk_family == PF_INET6) {
2347 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2348 if (spp->smk_sock != sk)
2350 spp->smk_can_reuse = 1;
2356 kfree(sk->sk_security);
2360 * smack_sk_clone_security - Copy security context
2361 * @sk: the old socket
2362 * @newsk: the new socket
2364 * Copy the security context of the old socket pointer to the cloned
2366 static void smack_sk_clone_security(const struct sock *sk, struct sock *newsk)
2368 struct socket_smack *ssp_old = sk->sk_security;
2369 struct socket_smack *ssp_new = newsk->sk_security;
2371 *ssp_new = *ssp_old;
2375 * smack_ipv4host_label - check host based restrictions
2376 * @sip: the object end
2378 * looks for host based access restrictions
2380 * This version will only be appropriate for really small sets of single label
2381 * hosts. The caller is responsible for ensuring that the RCU read lock is
2382 * taken before calling this function.
2384 * Returns the label of the far end or NULL if it's not special.
2386 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2388 struct smk_net4addr *snp;
2389 struct in_addr *siap = &sip->sin_addr;
2391 if (siap->s_addr == 0)
2394 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2396 * we break after finding the first match because
2397 * the list is sorted from longest to shortest mask
2398 * so we have found the most specific match
2400 if (snp->smk_host.s_addr ==
2401 (siap->s_addr & snp->smk_mask.s_addr))
2402 return snp->smk_label;
2408 * smk_ipv6_localhost - Check for local ipv6 host address
2411 * Returns boolean true if this is the localhost address
2413 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2415 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2416 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2418 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2419 ntohs(be16p[7]) == 1)
2425 * smack_ipv6host_label - check host based restrictions
2426 * @sip: the object end
2428 * looks for host based access restrictions
2430 * This version will only be appropriate for really small sets of single label
2431 * hosts. The caller is responsible for ensuring that the RCU read lock is
2432 * taken before calling this function.
2434 * Returns the label of the far end or NULL if it's not special.
2436 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2438 struct smk_net6addr *snp;
2439 struct in6_addr *sap = &sip->sin6_addr;
2444 * It's local. Don't look for a host label.
2446 if (smk_ipv6_localhost(sip))
2449 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2451 * If the label is NULL the entry has
2452 * been renounced. Ignore it.
2454 if (snp->smk_label == NULL)
2457 * we break after finding the first match because
2458 * the list is sorted from longest to shortest mask
2459 * so we have found the most specific match
2461 for (found = 1, i = 0; i < 8; i++) {
2462 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2463 snp->smk_host.s6_addr16[i]) {
2469 return snp->smk_label;
2476 * smack_netlbl_add - Set the secattr on a socket
2479 * Attach the outbound smack value (smk_out) to the socket.
2481 * Returns 0 on success or an error code
2483 static int smack_netlbl_add(struct sock *sk)
2485 struct socket_smack *ssp = sk->sk_security;
2486 struct smack_known *skp = ssp->smk_out;
2490 bh_lock_sock_nested(sk);
2492 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2495 ssp->smk_state = SMK_NETLBL_LABELED;
2498 ssp->smk_state = SMK_NETLBL_REQSKB;
2510 * smack_netlbl_delete - Remove the secattr from a socket
2513 * Remove the outbound smack value from a socket
2515 static void smack_netlbl_delete(struct sock *sk)
2517 struct socket_smack *ssp = sk->sk_security;
2520 * Take the label off the socket if one is set.
2522 if (ssp->smk_state != SMK_NETLBL_LABELED)
2526 bh_lock_sock_nested(sk);
2527 netlbl_sock_delattr(sk);
2530 ssp->smk_state = SMK_NETLBL_UNLABELED;
2534 * smk_ipv4_check - Perform IPv4 host access checks
2536 * @sap: the destination address
2538 * Set the correct secattr for the given socket based on the destination
2539 * address and perform any outbound access checks needed.
2541 * Returns 0 on success or an error code.
2544 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2546 struct smack_known *skp;
2548 struct smack_known *hkp;
2549 struct socket_smack *ssp = sk->sk_security;
2550 struct smk_audit_info ad;
2553 hkp = smack_ipv4host_label(sap);
2556 struct lsm_network_audit net;
2558 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2559 ad.a.u.net->family = sap->sin_family;
2560 ad.a.u.net->dport = sap->sin_port;
2561 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2564 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2565 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2567 * Clear the socket netlabel if it's set.
2570 smack_netlbl_delete(sk);
2578 * smk_ipv6_check - check Smack access
2579 * @subject: subject Smack label
2580 * @object: object Smack label
2582 * @act: the action being taken
2584 * Check an IPv6 access
2586 static int smk_ipv6_check(struct smack_known *subject,
2587 struct smack_known *object,
2588 struct sockaddr_in6 *address, int act)
2591 struct lsm_network_audit net;
2593 struct smk_audit_info ad;
2597 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2598 ad.a.u.net->family = PF_INET6;
2599 ad.a.u.net->dport = address->sin6_port;
2600 if (act == SMK_RECEIVING)
2601 ad.a.u.net->v6info.saddr = address->sin6_addr;
2603 ad.a.u.net->v6info.daddr = address->sin6_addr;
2605 rc = smk_access(subject, object, MAY_WRITE, &ad);
2606 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2610 #ifdef SMACK_IPV6_PORT_LABELING
2612 * smk_ipv6_port_label - Smack port access table management
2616 * Create or update the port list entry
2618 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2620 struct sock *sk = sock->sk;
2621 struct sockaddr_in6 *addr6;
2622 struct socket_smack *ssp = sock->sk->sk_security;
2623 struct smk_port_label *spp;
2624 unsigned short port = 0;
2626 if (address == NULL) {
2628 * This operation is changing the Smack information
2629 * on the bound socket. Take the changes to the port
2633 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2634 if (sk != spp->smk_sock)
2636 spp->smk_in = ssp->smk_in;
2637 spp->smk_out = ssp->smk_out;
2642 * A NULL address is only used for updating existing
2643 * bound entries. If there isn't one, it's OK.
2649 addr6 = (struct sockaddr_in6 *)address;
2650 port = ntohs(addr6->sin6_port);
2652 * This is a special case that is safely ignored.
2658 * Look for an existing port list entry.
2659 * This is an indication that a port is getting reused.
2662 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2663 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2665 if (spp->smk_can_reuse != 1) {
2669 spp->smk_port = port;
2671 spp->smk_in = ssp->smk_in;
2672 spp->smk_out = ssp->smk_out;
2673 spp->smk_can_reuse = 0;
2679 * A new port entry is required.
2681 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2685 spp->smk_port = port;
2687 spp->smk_in = ssp->smk_in;
2688 spp->smk_out = ssp->smk_out;
2689 spp->smk_sock_type = sock->type;
2690 spp->smk_can_reuse = 0;
2692 mutex_lock(&smack_ipv6_lock);
2693 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2694 mutex_unlock(&smack_ipv6_lock);
2699 * smk_ipv6_port_check - check Smack port access
2702 * @act: the action being taken
2704 * Create or update the port list entry
2706 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2709 struct smk_port_label *spp;
2710 struct socket_smack *ssp = sk->sk_security;
2711 struct smack_known *skp = NULL;
2712 unsigned short port;
2713 struct smack_known *object;
2715 if (act == SMK_RECEIVING) {
2716 skp = smack_ipv6host_label(address);
2717 object = ssp->smk_in;
2720 object = smack_ipv6host_label(address);
2724 * The other end is a single label host.
2726 if (skp != NULL && object != NULL)
2727 return smk_ipv6_check(skp, object, address, act);
2729 skp = smack_net_ambient;
2731 object = smack_net_ambient;
2734 * It's remote, so port lookup does no good.
2736 if (!smk_ipv6_localhost(address))
2737 return smk_ipv6_check(skp, object, address, act);
2740 * It's local so the send check has to have passed.
2742 if (act == SMK_RECEIVING)
2745 port = ntohs(address->sin6_port);
2747 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2748 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2750 object = spp->smk_in;
2751 if (act == SMK_CONNECTING)
2752 ssp->smk_packet = spp->smk_out;
2757 return smk_ipv6_check(skp, object, address, act);
2762 * smack_inode_setsecurity - set smack xattrs
2763 * @inode: the object
2764 * @name: attribute name
2765 * @value: attribute value
2766 * @size: size of the attribute
2769 * Sets the named attribute in the appropriate blob
2771 * Returns 0 on success, or an error code
2773 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2774 const void *value, size_t size, int flags)
2776 struct smack_known *skp;
2777 struct inode_smack *nsp = smack_inode(inode);
2778 struct socket_smack *ssp;
2779 struct socket *sock;
2782 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2785 skp = smk_import_entry(value, size);
2787 return PTR_ERR(skp);
2789 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2790 nsp->smk_inode = skp;
2791 nsp->smk_flags |= SMK_INODE_INSTANT;
2795 * The rest of the Smack xattrs are only on sockets.
2797 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2800 sock = SOCKET_I(inode);
2801 if (sock == NULL || sock->sk == NULL)
2804 ssp = sock->sk->sk_security;
2806 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2808 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2810 if (sock->sk->sk_family == PF_INET) {
2811 rc = smack_netlbl_add(sock->sk);
2814 "Smack: \"%s\" netlbl error %d.\n",
2820 #ifdef SMACK_IPV6_PORT_LABELING
2821 if (sock->sk->sk_family == PF_INET6)
2822 smk_ipv6_port_label(sock, NULL);
2829 * smack_socket_post_create - finish socket setup
2831 * @family: protocol family
2836 * Sets the netlabel information on the socket
2838 * Returns 0 on success, and error code otherwise
2840 static int smack_socket_post_create(struct socket *sock, int family,
2841 int type, int protocol, int kern)
2843 struct socket_smack *ssp;
2845 if (sock->sk == NULL)
2849 * Sockets created by kernel threads receive web label.
2851 if (unlikely(current->flags & PF_KTHREAD)) {
2852 ssp = sock->sk->sk_security;
2853 ssp->smk_in = &smack_known_web;
2854 ssp->smk_out = &smack_known_web;
2857 if (family != PF_INET)
2860 * Set the outbound netlbl.
2862 return smack_netlbl_add(sock->sk);
2866 * smack_socket_socketpair - create socket pair
2867 * @socka: one socket
2868 * @sockb: another socket
2870 * Cross reference the peer labels for SO_PEERSEC
2874 static int smack_socket_socketpair(struct socket *socka,
2875 struct socket *sockb)
2877 struct socket_smack *asp = socka->sk->sk_security;
2878 struct socket_smack *bsp = sockb->sk->sk_security;
2880 asp->smk_packet = bsp->smk_out;
2881 bsp->smk_packet = asp->smk_out;
2886 #ifdef SMACK_IPV6_PORT_LABELING
2888 * smack_socket_bind - record port binding information.
2890 * @address: the port address
2891 * @addrlen: size of the address
2893 * Records the label bound to a port.
2895 * Returns 0 on success, and error code otherwise
2897 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2900 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2901 if (addrlen < SIN6_LEN_RFC2133 ||
2902 address->sa_family != AF_INET6)
2904 smk_ipv6_port_label(sock, address);
2908 #endif /* SMACK_IPV6_PORT_LABELING */
2911 * smack_socket_connect - connect access check
2913 * @sap: the other end
2914 * @addrlen: size of sap
2916 * Verifies that a connection may be possible
2918 * Returns 0 on success, and error code otherwise
2920 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2925 if (sock->sk == NULL)
2927 if (sock->sk->sk_family != PF_INET &&
2928 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2930 if (addrlen < offsetofend(struct sockaddr, sa_family))
2932 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2933 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2934 struct smack_known *rsp = NULL;
2936 if (addrlen < SIN6_LEN_RFC2133)
2938 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2939 rsp = smack_ipv6host_label(sip);
2941 struct socket_smack *ssp = sock->sk->sk_security;
2943 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2946 #ifdef SMACK_IPV6_PORT_LABELING
2947 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2952 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2954 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
2959 * smack_flags_to_may - convert S_ to MAY_ values
2960 * @flags: the S_ value
2962 * Returns the equivalent MAY_ value
2964 static int smack_flags_to_may(int flags)
2968 if (flags & S_IRUGO)
2970 if (flags & S_IWUGO)
2972 if (flags & S_IXUGO)
2979 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2984 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2986 struct smack_known **blob = smack_msg_msg(msg);
2988 *blob = smk_of_current();
2993 * smack_of_ipc - the smack pointer for the ipc
2996 * Returns a pointer to the smack value
2998 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
3000 struct smack_known **blob = smack_ipc(isp);
3006 * smack_ipc_alloc_security - Set the security blob for ipc
3011 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
3013 struct smack_known **blob = smack_ipc(isp);
3015 *blob = smk_of_current();
3020 * smk_curacc_shm : check if current has access on shm
3022 * @access : access requested
3024 * Returns 0 if current has the requested access, error code otherwise
3026 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
3028 struct smack_known *ssp = smack_of_ipc(isp);
3029 struct smk_audit_info ad;
3033 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3034 ad.a.u.ipc_id = isp->id;
3036 rc = smk_curacc(ssp, access, &ad);
3037 rc = smk_bu_current("shm", ssp, access, rc);
3042 * smack_shm_associate - Smack access check for shm
3044 * @shmflg: access requested
3046 * Returns 0 if current has the requested access, error code otherwise
3048 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3052 may = smack_flags_to_may(shmflg);
3053 return smk_curacc_shm(isp, may);
3057 * smack_shm_shmctl - Smack access check for shm
3059 * @cmd: what it wants to do
3061 * Returns 0 if current has the requested access, error code otherwise
3063 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3077 may = MAY_READWRITE;
3082 * System level information.
3088 return smk_curacc_shm(isp, may);
3092 * smack_shm_shmat - Smack access for shmat
3095 * @shmflg: access requested
3097 * Returns 0 if current has the requested access, error code otherwise
3099 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3104 may = smack_flags_to_may(shmflg);
3105 return smk_curacc_shm(isp, may);
3109 * smk_curacc_sem : check if current has access on sem
3111 * @access : access requested
3113 * Returns 0 if current has the requested access, error code otherwise
3115 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3117 struct smack_known *ssp = smack_of_ipc(isp);
3118 struct smk_audit_info ad;
3122 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3123 ad.a.u.ipc_id = isp->id;
3125 rc = smk_curacc(ssp, access, &ad);
3126 rc = smk_bu_current("sem", ssp, access, rc);
3131 * smack_sem_associate - Smack access check for sem
3133 * @semflg: access requested
3135 * Returns 0 if current has the requested access, error code otherwise
3137 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3141 may = smack_flags_to_may(semflg);
3142 return smk_curacc_sem(isp, may);
3146 * smack_sem_semctl - Smack access check for sem
3148 * @cmd: what it wants to do
3150 * Returns 0 if current has the requested access, error code otherwise
3152 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3171 may = MAY_READWRITE;
3176 * System level information
3183 return smk_curacc_sem(isp, may);
3187 * smack_sem_semop - Smack checks of semaphore operations
3193 * Treated as read and write in all cases.
3195 * Returns 0 if access is allowed, error code otherwise
3197 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3198 unsigned nsops, int alter)
3200 return smk_curacc_sem(isp, MAY_READWRITE);
3204 * smk_curacc_msq : helper to check if current has access on msq
3206 * @access : access requested
3208 * return 0 if current has access, error otherwise
3210 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3212 struct smack_known *msp = smack_of_ipc(isp);
3213 struct smk_audit_info ad;
3217 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3218 ad.a.u.ipc_id = isp->id;
3220 rc = smk_curacc(msp, access, &ad);
3221 rc = smk_bu_current("msq", msp, access, rc);
3226 * smack_msg_queue_associate - Smack access check for msg_queue
3228 * @msqflg: access requested
3230 * Returns 0 if current has the requested access, error code otherwise
3232 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3236 may = smack_flags_to_may(msqflg);
3237 return smk_curacc_msq(isp, may);
3241 * smack_msg_queue_msgctl - Smack access check for msg_queue
3243 * @cmd: what it wants to do
3245 * Returns 0 if current has the requested access, error code otherwise
3247 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3259 may = MAY_READWRITE;
3264 * System level information
3271 return smk_curacc_msq(isp, may);
3275 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3278 * @msqflg: access requested
3280 * Returns 0 if current has the requested access, error code otherwise
3282 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3287 may = smack_flags_to_may(msqflg);
3288 return smk_curacc_msq(isp, may);
3292 * smack_msg_queue_msgrcv - Smack access check for msg_queue
3299 * Returns 0 if current has read and write access, error code otherwise
3301 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp,
3302 struct msg_msg *msg,
3303 struct task_struct *target, long type,
3306 return smk_curacc_msq(isp, MAY_READWRITE);
3310 * smack_ipc_permission - Smack access for ipc_permission()
3311 * @ipp: the object permissions
3312 * @flag: access requested
3314 * Returns 0 if current has read and write access, error code otherwise
3316 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3318 struct smack_known **blob = smack_ipc(ipp);
3319 struct smack_known *iskp = *blob;
3320 int may = smack_flags_to_may(flag);
3321 struct smk_audit_info ad;
3325 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3326 ad.a.u.ipc_id = ipp->id;
3328 rc = smk_curacc(iskp, may, &ad);
3329 rc = smk_bu_current("svipc", iskp, may, rc);
3334 * smack_ipc_getsecid - Extract smack security id
3335 * @ipp: the object permissions
3336 * @secid: where result will be saved
3338 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3340 struct smack_known **blob = smack_ipc(ipp);
3341 struct smack_known *iskp = *blob;
3343 *secid = iskp->smk_secid;
3347 * smack_d_instantiate - Make sure the blob is correct on an inode
3348 * @opt_dentry: dentry where inode will be attached
3349 * @inode: the object
3351 * Set the inode's security blob if it hasn't been done already.
3353 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3355 struct super_block *sbp;
3356 struct superblock_smack *sbsp;
3357 struct inode_smack *isp;
3358 struct smack_known *skp;
3359 struct smack_known *ckp = smk_of_current();
3360 struct smack_known *final;
3361 char trattr[TRANS_TRUE_SIZE];
3369 isp = smack_inode(inode);
3372 * If the inode is already instantiated
3373 * take the quick way out
3375 if (isp->smk_flags & SMK_INODE_INSTANT)
3379 sbsp = smack_superblock(sbp);
3381 * We're going to use the superblock default label
3382 * if there's no label on the file.
3384 final = sbsp->smk_default;
3387 * If this is the root inode the superblock
3388 * may be in the process of initialization.
3389 * If that is the case use the root value out
3390 * of the superblock.
3392 if (opt_dentry->d_parent == opt_dentry) {
3393 switch (sbp->s_magic) {
3394 case CGROUP_SUPER_MAGIC:
3395 case CGROUP2_SUPER_MAGIC:
3397 * The cgroup filesystem is never mounted,
3398 * so there's no opportunity to set the mount
3401 sbsp->smk_root = &smack_known_star;
3402 sbsp->smk_default = &smack_known_star;
3403 isp->smk_inode = sbsp->smk_root;
3407 * What about shmem/tmpfs anonymous files with dentry
3408 * obtained from d_alloc_pseudo()?
3410 isp->smk_inode = smk_of_current();
3413 isp->smk_inode = smk_of_current();
3417 * Socket access is controlled by the socket
3418 * structures associated with the task involved.
3420 isp->smk_inode = &smack_known_star;
3423 isp->smk_inode = sbsp->smk_root;
3426 isp->smk_flags |= SMK_INODE_INSTANT;
3431 * This is pretty hackish.
3432 * Casey says that we shouldn't have to do
3433 * file system specific code, but it does help
3434 * with keeping it simple.
3436 switch (sbp->s_magic) {
3438 case CGROUP_SUPER_MAGIC:
3439 case CGROUP2_SUPER_MAGIC:
3441 * Casey says that it's a little embarrassing
3442 * that the smack file system doesn't do
3443 * extended attributes.
3445 * Cgroupfs is special
3447 final = &smack_known_star;
3449 case DEVPTS_SUPER_MAGIC:
3451 * devpts seems content with the label of the task.
3452 * Programs that change smack have to treat the
3457 case PROC_SUPER_MAGIC:
3459 * Casey says procfs appears not to care.
3460 * The superblock default suffices.
3465 * Device labels should come from the filesystem,
3466 * but watch out, because they're volitile,
3467 * getting recreated on every reboot.
3469 final = &smack_known_star;
3471 * If a smack value has been set we want to use it,
3472 * but since tmpfs isn't giving us the opportunity
3473 * to set mount options simulate setting the
3474 * superblock default.
3479 * This isn't an understood special case.
3480 * Get the value from the xattr.
3484 * UNIX domain sockets use lower level socket data.
3486 if (S_ISSOCK(inode->i_mode)) {
3487 final = &smack_known_star;
3491 * No xattr support means, alas, no SMACK label.
3492 * Use the aforeapplied default.
3493 * It would be curious if the label of the task
3494 * does not match that assigned.
3496 if (!(inode->i_opflags & IOP_XATTR))
3499 * Get the dentry for xattr.
3501 dp = dget(opt_dentry);
3502 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3503 if (!IS_ERR_OR_NULL(skp))
3507 * Transmuting directory
3509 if (S_ISDIR(inode->i_mode)) {
3511 * If this is a new directory and the label was
3512 * transmuted when the inode was initialized
3513 * set the transmute attribute on the directory
3514 * and mark the inode.
3516 * If there is a transmute attribute on the
3517 * directory mark the inode.
3519 if (isp->smk_flags & SMK_INODE_CHANGED) {
3520 isp->smk_flags &= ~SMK_INODE_CHANGED;
3521 rc = __vfs_setxattr(&init_user_ns, dp, inode,
3522 XATTR_NAME_SMACKTRANSMUTE,
3523 TRANS_TRUE, TRANS_TRUE_SIZE,
3526 rc = __vfs_getxattr(dp, inode,
3527 XATTR_NAME_SMACKTRANSMUTE, trattr,
3529 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3530 TRANS_TRUE_SIZE) != 0)
3534 transflag = SMK_INODE_TRANSMUTE;
3537 * Don't let the exec or mmap label be "*" or "@".
3539 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3540 if (IS_ERR(skp) || skp == &smack_known_star ||
3541 skp == &smack_known_web)
3543 isp->smk_task = skp;
3545 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3546 if (IS_ERR(skp) || skp == &smack_known_star ||
3547 skp == &smack_known_web)
3549 isp->smk_mmap = skp;
3556 isp->smk_inode = ckp;
3558 isp->smk_inode = final;
3560 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3566 * smack_getprocattr - Smack process attribute access
3567 * @p: the object task
3568 * @name: the name of the attribute in /proc/.../attr
3569 * @value: where to put the result
3571 * Places a copy of the task Smack into value
3573 * Returns the length of the smack label or an error code
3575 static int smack_getprocattr(struct task_struct *p, const char *name, char **value)
3577 struct smack_known *skp = smk_of_task_struct_obj(p);
3581 if (strcmp(name, "current") != 0)
3584 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3594 * smack_setprocattr - Smack process attribute setting
3595 * @name: the name of the attribute in /proc/.../attr
3596 * @value: the value to set
3597 * @size: the size of the value
3599 * Sets the Smack value of the task. Only setting self
3600 * is permitted and only with privilege
3602 * Returns the length of the smack label or an error code
3604 static int smack_setprocattr(const char *name, void *value, size_t size)
3606 struct task_smack *tsp = smack_cred(current_cred());
3608 struct smack_known *skp;
3609 struct smack_known_list_elem *sklep;
3612 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3615 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3618 if (strcmp(name, "current") != 0)
3621 skp = smk_import_entry(value, size);
3623 return PTR_ERR(skp);
3626 * No process is ever allowed the web ("@") label
3627 * and the star ("*") label.
3629 if (skp == &smack_known_web || skp == &smack_known_star)
3632 if (!smack_privileged(CAP_MAC_ADMIN)) {
3634 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3635 if (sklep->smk_label == skp) {
3643 new = prepare_creds();
3647 tsp = smack_cred(new);
3648 tsp->smk_task = skp;
3650 * process can change its label only once
3652 smk_destroy_label_list(&tsp->smk_relabel);
3659 * smack_unix_stream_connect - Smack access on UDS
3661 * @other: the other sock
3664 * Return 0 if a subject with the smack of sock could access
3665 * an object with the smack of other, otherwise an error code
3667 static int smack_unix_stream_connect(struct sock *sock,
3668 struct sock *other, struct sock *newsk)
3670 struct smack_known *skp;
3671 struct smack_known *okp;
3672 struct socket_smack *ssp = sock->sk_security;
3673 struct socket_smack *osp = other->sk_security;
3674 struct socket_smack *nsp = newsk->sk_security;
3675 struct smk_audit_info ad;
3678 struct lsm_network_audit net;
3681 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3685 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3686 smk_ad_setfield_u_net_sk(&ad, other);
3688 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3689 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3693 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3694 rc = smk_bu_note("UDS connect", okp, skp,
3700 * Cross reference the peer labels for SO_PEERSEC.
3703 nsp->smk_packet = ssp->smk_out;
3704 ssp->smk_packet = osp->smk_out;
3711 * smack_unix_may_send - Smack access on UDS
3713 * @other: the other socket
3715 * Return 0 if a subject with the smack of sock could access
3716 * an object with the smack of other, otherwise an error code
3718 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3720 struct socket_smack *ssp = sock->sk->sk_security;
3721 struct socket_smack *osp = other->sk->sk_security;
3722 struct smk_audit_info ad;
3726 struct lsm_network_audit net;
3728 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3729 smk_ad_setfield_u_net_sk(&ad, other->sk);
3732 if (smack_privileged(CAP_MAC_OVERRIDE))
3735 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3736 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3741 * smack_socket_sendmsg - Smack check based on destination host
3744 * @size: the size of the message
3746 * Return 0 if the current subject can write to the destination host.
3747 * For IPv4 this is only a question if the destination is a single label host.
3748 * For IPv6 this is a check against the label of the port.
3750 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3753 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3754 #if IS_ENABLED(CONFIG_IPV6)
3755 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3757 #ifdef SMACK_IPV6_SECMARK_LABELING
3758 struct socket_smack *ssp = sock->sk->sk_security;
3759 struct smack_known *rsp;
3764 * Perfectly reasonable for this to be NULL
3769 switch (sock->sk->sk_family) {
3771 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3772 sip->sin_family != AF_INET)
3774 rc = smk_ipv4_check(sock->sk, sip);
3776 #if IS_ENABLED(CONFIG_IPV6)
3778 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3779 sap->sin6_family != AF_INET6)
3781 #ifdef SMACK_IPV6_SECMARK_LABELING
3782 rsp = smack_ipv6host_label(sap);
3784 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3787 #ifdef SMACK_IPV6_PORT_LABELING
3788 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3790 #endif /* IS_ENABLED(CONFIG_IPV6) */
3797 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3798 * @sap: netlabel secattr
3799 * @ssp: socket security information
3801 * Returns a pointer to a Smack label entry found on the label list.
3803 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3804 struct socket_smack *ssp)
3806 struct smack_known *skp;
3812 * Netlabel found it in the cache.
3814 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3815 return (struct smack_known *)sap->cache->data;
3817 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3819 * Looks like a fallback, which gives us a secid.
3821 return smack_from_secid(sap->attr.secid);
3823 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3825 * Looks like a CIPSO packet.
3826 * If there are flags but no level netlabel isn't
3827 * behaving the way we expect it to.
3829 * Look it up in the label table
3830 * Without guidance regarding the smack value
3831 * for the packet fall back on the network
3835 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3836 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3839 * Compare the catsets. Use the netlbl APIs.
3841 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3842 if ((skp->smk_netlabel.flags &
3843 NETLBL_SECATTR_MLS_CAT) == 0)
3847 for (acat = -1, kcat = -1; acat == kcat; ) {
3848 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3850 kcat = netlbl_catmap_walk(
3851 skp->smk_netlabel.attr.mls.cat,
3853 if (acat < 0 || kcat < 0)
3866 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3867 return &smack_known_web;
3868 return &smack_known_star;
3871 * Without guidance regarding the smack value
3872 * for the packet fall back on the network
3875 return smack_net_ambient;
3878 #if IS_ENABLED(CONFIG_IPV6)
3879 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3883 int proto = -EINVAL;
3884 struct ipv6hdr _ipv6h;
3885 struct ipv6hdr *ip6;
3887 struct tcphdr _tcph, *th;
3888 struct udphdr _udph, *uh;
3889 struct dccp_hdr _dccph, *dh;
3893 offset = skb_network_offset(skb);
3894 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3897 sip->sin6_addr = ip6->saddr;
3899 nexthdr = ip6->nexthdr;
3900 offset += sizeof(_ipv6h);
3901 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3908 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3910 sip->sin6_port = th->source;
3913 case IPPROTO_UDPLITE:
3914 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3916 sip->sin6_port = uh->source;
3919 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3921 sip->sin6_port = dh->dccph_sport;
3926 #endif /* CONFIG_IPV6 */
3929 * smack_from_skb - Smack data from the secmark in an skb
3932 * Returns smack_known of the secmark or NULL if that won't work.
3934 #ifdef CONFIG_NETWORK_SECMARK
3935 static struct smack_known *smack_from_skb(struct sk_buff *skb)
3937 if (skb == NULL || skb->secmark == 0)
3940 return smack_from_secid(skb->secmark);
3943 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3950 * smack_from_netlbl - Smack data from the IP options in an skb
3951 * @sk: socket data came in on
3952 * @family: address family
3955 * Find the Smack label in the IP options. If it hasn't been
3956 * added to the netlabel cache, add it here.
3958 * Returns smack_known of the IP options or NULL if that won't work.
3960 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
3961 struct sk_buff *skb)
3963 struct netlbl_lsm_secattr secattr;
3964 struct socket_smack *ssp = NULL;
3965 struct smack_known *skp = NULL;
3967 netlbl_secattr_init(&secattr);
3970 ssp = sk->sk_security;
3972 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
3973 skp = smack_from_secattr(&secattr, ssp);
3974 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
3975 netlbl_cache_add(skb, family, &skp->smk_netlabel);
3978 netlbl_secattr_destroy(&secattr);
3984 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3988 * Returns 0 if the packet should be delivered, an error code otherwise
3990 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3992 struct socket_smack *ssp = sk->sk_security;
3993 struct smack_known *skp = NULL;
3995 struct smk_audit_info ad;
3996 u16 family = sk->sk_family;
3998 struct lsm_network_audit net;
4000 #if IS_ENABLED(CONFIG_IPV6)
4001 struct sockaddr_in6 sadd;
4004 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4006 #endif /* CONFIG_IPV6 */
4011 * If there is a secmark use it rather than the CIPSO label.
4012 * If there is no secmark fall back to CIPSO.
4013 * The secmark is assumed to reflect policy better.
4015 skp = smack_from_skb(skb);
4017 skp = smack_from_netlbl(sk, family, skb);
4019 skp = smack_net_ambient;
4023 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4024 ad.a.u.net->family = family;
4025 ad.a.u.net->netif = skb->skb_iif;
4026 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4029 * Receiving a packet requires that the other end
4030 * be able to write here. Read access is not required.
4031 * This is the simplist possible security model
4034 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4035 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
4038 netlbl_skbuff_err(skb, family, rc, 0);
4040 #if IS_ENABLED(CONFIG_IPV6)
4042 proto = smk_skb_to_addr_ipv6(skb, &sadd);
4043 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
4044 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
4046 #ifdef SMACK_IPV6_SECMARK_LABELING
4047 skp = smack_from_skb(skb);
4049 if (smk_ipv6_localhost(&sadd))
4051 skp = smack_ipv6host_label(&sadd);
4053 skp = smack_net_ambient;
4056 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4057 ad.a.u.net->family = family;
4058 ad.a.u.net->netif = skb->skb_iif;
4059 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4060 #endif /* CONFIG_AUDIT */
4061 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4062 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4064 #endif /* SMACK_IPV6_SECMARK_LABELING */
4065 #ifdef SMACK_IPV6_PORT_LABELING
4066 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
4067 #endif /* SMACK_IPV6_PORT_LABELING */
4069 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4070 ICMPV6_ADM_PROHIBITED, 0);
4072 #endif /* CONFIG_IPV6 */
4079 * smack_socket_getpeersec_stream - pull in packet label
4081 * @optval: user's destination
4082 * @optlen: size thereof
4085 * returns zero on success, an error code otherwise
4087 static int smack_socket_getpeersec_stream(struct socket *sock,
4088 char __user *optval,
4089 int __user *optlen, unsigned len)
4091 struct socket_smack *ssp;
4096 ssp = sock->sk->sk_security;
4097 if (ssp->smk_packet != NULL) {
4098 rcp = ssp->smk_packet->smk_known;
4099 slen = strlen(rcp) + 1;
4104 else if (copy_to_user(optval, rcp, slen) != 0)
4107 if (put_user(slen, optlen) != 0)
4115 * smack_socket_getpeersec_dgram - pull in packet label
4116 * @sock: the peer socket
4118 * @secid: pointer to where to put the secid of the packet
4120 * Sets the netlabel socket state on sk from parent
4122 static int smack_socket_getpeersec_dgram(struct socket *sock,
4123 struct sk_buff *skb, u32 *secid)
4126 struct socket_smack *ssp = NULL;
4127 struct smack_known *skp;
4128 struct sock *sk = NULL;
4129 int family = PF_UNSPEC;
4130 u32 s = 0; /* 0 is the invalid secid */
4133 if (skb->protocol == htons(ETH_P_IP))
4135 #if IS_ENABLED(CONFIG_IPV6)
4136 else if (skb->protocol == htons(ETH_P_IPV6))
4138 #endif /* CONFIG_IPV6 */
4140 if (family == PF_UNSPEC && sock != NULL)
4141 family = sock->sk->sk_family;
4145 ssp = sock->sk->sk_security;
4146 s = ssp->smk_out->smk_secid;
4149 skp = smack_from_skb(skb);
4155 * Translate what netlabel gave us.
4159 skp = smack_from_netlbl(sk, family, skb);
4164 #ifdef SMACK_IPV6_SECMARK_LABELING
4165 skp = smack_from_skb(skb);
4178 * smack_sock_graft - Initialize a newly created socket with an existing sock
4180 * @parent: parent socket
4182 * Set the smk_{in,out} state of an existing sock based on the process that
4183 * is creating the new socket.
4185 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4187 struct socket_smack *ssp;
4188 struct smack_known *skp = smk_of_current();
4191 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4194 ssp = sk->sk_security;
4197 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4201 * smack_inet_conn_request - Smack access check on connect
4202 * @sk: socket involved
4206 * Returns 0 if a task with the packet label could write to
4207 * the socket, otherwise an error code
4209 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4210 struct request_sock *req)
4212 u16 family = sk->sk_family;
4213 struct smack_known *skp;
4214 struct socket_smack *ssp = sk->sk_security;
4215 struct sockaddr_in addr;
4217 struct smack_known *hskp;
4219 struct smk_audit_info ad;
4221 struct lsm_network_audit net;
4224 #if IS_ENABLED(CONFIG_IPV6)
4225 if (family == PF_INET6) {
4227 * Handle mapped IPv4 packets arriving
4228 * via IPv6 sockets. Don't set up netlabel
4229 * processing on IPv6.
4231 if (skb->protocol == htons(ETH_P_IP))
4236 #endif /* CONFIG_IPV6 */
4239 * If there is a secmark use it rather than the CIPSO label.
4240 * If there is no secmark fall back to CIPSO.
4241 * The secmark is assumed to reflect policy better.
4243 skp = smack_from_skb(skb);
4245 skp = smack_from_netlbl(sk, family, skb);
4247 skp = &smack_known_huh;
4251 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4252 ad.a.u.net->family = family;
4253 ad.a.u.net->netif = skb->skb_iif;
4254 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4257 * Receiving a packet requires that the other end be able to write
4258 * here. Read access is not required.
4260 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4261 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4266 * Save the peer's label in the request_sock so we can later setup
4267 * smk_packet in the child socket so that SO_PEERCRED can report it.
4269 req->peer_secid = skp->smk_secid;
4272 * We need to decide if we want to label the incoming connection here
4273 * if we do we only need to label the request_sock and the stack will
4274 * propagate the wire-label to the sock when it is created.
4277 addr.sin_addr.s_addr = hdr->saddr;
4279 hskp = smack_ipv4host_label(&addr);
4283 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4285 netlbl_req_delattr(req);
4291 * smack_inet_csk_clone - Copy the connection information to the new socket
4292 * @sk: the new socket
4293 * @req: the connection's request_sock
4295 * Transfer the connection's peer label to the newly created socket.
4297 static void smack_inet_csk_clone(struct sock *sk,
4298 const struct request_sock *req)
4300 struct socket_smack *ssp = sk->sk_security;
4301 struct smack_known *skp;
4303 if (req->peer_secid != 0) {
4304 skp = smack_from_secid(req->peer_secid);
4305 ssp->smk_packet = skp;
4307 ssp->smk_packet = NULL;
4311 * Key management security hooks
4313 * Casey has not tested key support very heavily.
4314 * The permission check is most likely too restrictive.
4315 * If you care about keys please have a look.
4320 * smack_key_alloc - Set the key security blob
4322 * @cred: the credentials to use
4325 * No allocation required
4329 static int smack_key_alloc(struct key *key, const struct cred *cred,
4330 unsigned long flags)
4332 struct smack_known *skp = smk_of_task(smack_cred(cred));
4334 key->security = skp;
4339 * smack_key_free - Clear the key security blob
4342 * Clear the blob pointer
4344 static void smack_key_free(struct key *key)
4346 key->security = NULL;
4350 * smack_key_permission - Smack access on a key
4351 * @key_ref: gets to the object
4352 * @cred: the credentials to use
4353 * @need_perm: requested key permission
4355 * Return 0 if the task has read and write to the object,
4356 * an error code otherwise
4358 static int smack_key_permission(key_ref_t key_ref,
4359 const struct cred *cred,
4360 enum key_need_perm need_perm)
4363 struct smk_audit_info ad;
4364 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4369 * Validate requested permissions
4371 switch (need_perm) {
4373 case KEY_NEED_SEARCH:
4375 request |= MAY_READ;
4377 case KEY_NEED_WRITE:
4379 case KEY_NEED_SETATTR:
4380 request |= MAY_WRITE;
4382 case KEY_NEED_UNSPECIFIED:
4383 case KEY_NEED_UNLINK:
4384 case KEY_SYSADMIN_OVERRIDE:
4385 case KEY_AUTHTOKEN_OVERRIDE:
4386 case KEY_DEFER_PERM_CHECK:
4392 keyp = key_ref_to_ptr(key_ref);
4396 * If the key hasn't been initialized give it access so that
4399 if (keyp->security == NULL)
4402 * This should not occur
4407 if (smack_privileged(CAP_MAC_OVERRIDE))
4411 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4412 ad.a.u.key_struct.key = keyp->serial;
4413 ad.a.u.key_struct.key_desc = keyp->description;
4415 rc = smk_access(tkp, keyp->security, request, &ad);
4416 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4421 * smack_key_getsecurity - Smack label tagging the key
4422 * @key points to the key to be queried
4423 * @_buffer points to a pointer that should be set to point to the
4424 * resulting string (if no label or an error occurs).
4425 * Return the length of the string (including terminating NUL) or -ve if
4427 * May also return 0 (and a NULL buffer pointer) if there is no label.
4429 static int smack_key_getsecurity(struct key *key, char **_buffer)
4431 struct smack_known *skp = key->security;
4435 if (key->security == NULL) {
4440 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4443 length = strlen(copy) + 1;
4450 #ifdef CONFIG_KEY_NOTIFICATIONS
4452 * smack_watch_key - Smack access to watch a key for notifications.
4453 * @key: The key to be watched
4455 * Return 0 if the @watch->cred has permission to read from the key object and
4456 * an error otherwise.
4458 static int smack_watch_key(struct key *key)
4460 struct smk_audit_info ad;
4461 struct smack_known *tkp = smk_of_current();
4467 * If the key hasn't been initialized give it access so that
4470 if (key->security == NULL)
4473 * This should not occur
4478 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4482 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4483 ad.a.u.key_struct.key = key->serial;
4484 ad.a.u.key_struct.key_desc = key->description;
4486 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4487 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4490 #endif /* CONFIG_KEY_NOTIFICATIONS */
4491 #endif /* CONFIG_KEYS */
4493 #ifdef CONFIG_WATCH_QUEUE
4495 * smack_post_notification - Smack access to post a notification to a queue
4496 * @w_cred: The credentials of the watcher.
4497 * @cred: The credentials of the event source (may be NULL).
4498 * @n: The notification message to be posted.
4500 static int smack_post_notification(const struct cred *w_cred,
4501 const struct cred *cred,
4502 struct watch_notification *n)
4504 struct smk_audit_info ad;
4505 struct smack_known *subj, *obj;
4508 /* Always let maintenance notifications through. */
4509 if (n->type == WATCH_TYPE_META)
4514 subj = smk_of_task(smack_cred(cred));
4515 obj = smk_of_task(smack_cred(w_cred));
4517 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4518 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4519 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4522 #endif /* CONFIG_WATCH_QUEUE */
4527 * Audit requires a unique representation of each Smack specific
4528 * rule. This unique representation is used to distinguish the
4529 * object to be audited from remaining kernel objects and also
4530 * works as a glue between the audit hooks.
4532 * Since repository entries are added but never deleted, we'll use
4533 * the smack_known label address related to the given audit rule as
4534 * the needed unique representation. This also better fits the smack
4535 * model where nearly everything is a label.
4540 * smack_audit_rule_init - Initialize a smack audit rule
4541 * @field: audit rule fields given from user-space (audit.h)
4542 * @op: required testing operator (=, !=, >, <, ...)
4543 * @rulestr: smack label to be audited
4544 * @vrule: pointer to save our own audit rule representation
4546 * Prepare to audit cases where (@field @op @rulestr) is true.
4547 * The label to be audited is created if necessay.
4549 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4551 struct smack_known *skp;
4552 char **rule = (char **)vrule;
4555 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4558 if (op != Audit_equal && op != Audit_not_equal)
4561 skp = smk_import_entry(rulestr, 0);
4563 return PTR_ERR(skp);
4565 *rule = skp->smk_known;
4571 * smack_audit_rule_known - Distinguish Smack audit rules
4572 * @krule: rule of interest, in Audit kernel representation format
4574 * This is used to filter Smack rules from remaining Audit ones.
4575 * If it's proved that this rule belongs to us, the
4576 * audit_rule_match hook will be called to do the final judgement.
4578 static int smack_audit_rule_known(struct audit_krule *krule)
4580 struct audit_field *f;
4583 for (i = 0; i < krule->field_count; i++) {
4584 f = &krule->fields[i];
4586 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4594 * smack_audit_rule_match - Audit given object ?
4595 * @secid: security id for identifying the object to test
4596 * @field: audit rule flags given from user-space
4597 * @op: required testing operator
4598 * @vrule: smack internal rule presentation
4600 * The core Audit hook. It's used to take the decision of
4601 * whether to audit or not to audit a given object.
4603 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4605 struct smack_known *skp;
4608 if (unlikely(!rule)) {
4609 WARN_ONCE(1, "Smack: missing rule\n");
4613 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4616 skp = smack_from_secid(secid);
4619 * No need to do string comparisons. If a match occurs,
4620 * both pointers will point to the same smack_known
4623 if (op == Audit_equal)
4624 return (rule == skp->smk_known);
4625 if (op == Audit_not_equal)
4626 return (rule != skp->smk_known);
4632 * There is no need for a smack_audit_rule_free hook.
4633 * No memory was allocated.
4636 #endif /* CONFIG_AUDIT */
4639 * smack_ismaclabel - check if xattr @name references a smack MAC label
4640 * @name: Full xattr name to check.
4642 static int smack_ismaclabel(const char *name)
4644 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4649 * smack_secid_to_secctx - return the smack label for a secid
4650 * @secid: incoming integer
4651 * @secdata: destination
4652 * @seclen: how long it is
4654 * Exists for networking code.
4656 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4658 struct smack_known *skp = smack_from_secid(secid);
4661 *secdata = skp->smk_known;
4662 *seclen = strlen(skp->smk_known);
4667 * smack_secctx_to_secid - return the secid for a smack label
4668 * @secdata: smack label
4669 * @seclen: how long result is
4670 * @secid: outgoing integer
4672 * Exists for audit and networking code.
4674 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4676 struct smack_known *skp = smk_find_entry(secdata);
4679 *secid = skp->smk_secid;
4686 * There used to be a smack_release_secctx hook
4687 * that did nothing back when hooks were in a vector.
4688 * Now that there's a list such a hook adds cost.
4691 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4693 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4697 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4699 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK,
4703 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4705 struct smack_known *skp = smk_of_inode(inode);
4707 *ctx = skp->smk_known;
4708 *ctxlen = strlen(skp->smk_known);
4712 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4715 struct task_smack *tsp;
4716 struct smack_known *skp;
4717 struct inode_smack *isp;
4718 struct cred *new_creds = *new;
4720 if (new_creds == NULL) {
4721 new_creds = prepare_creds();
4722 if (new_creds == NULL)
4726 tsp = smack_cred(new_creds);
4729 * Get label from overlay inode and set it in create_sid
4731 isp = smack_inode(d_inode(dentry));
4732 skp = isp->smk_inode;
4733 tsp->smk_task = skp;
4738 static int smack_inode_copy_up_xattr(const char *name)
4741 * Return 1 if this is the smack access Smack attribute.
4743 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4749 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4751 const struct cred *old,
4754 struct task_smack *otsp = smack_cred(old);
4755 struct task_smack *ntsp = smack_cred(new);
4756 struct inode_smack *isp;
4760 * Use the process credential unless all of
4761 * the transmuting criteria are met
4763 ntsp->smk_task = otsp->smk_task;
4766 * the attribute of the containing directory
4768 isp = smack_inode(d_inode(dentry->d_parent));
4770 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4772 may = smk_access_entry(otsp->smk_task->smk_known,
4773 isp->smk_inode->smk_known,
4774 &otsp->smk_task->smk_rules);
4778 * If the directory is transmuting and the rule
4779 * providing access is transmuting use the containing
4780 * directory label instead of the process label.
4782 if (may > 0 && (may & MAY_TRANSMUTE)) {
4783 ntsp->smk_task = isp->smk_inode;
4784 ntsp->smk_transmuted = ntsp->smk_task;
4790 #ifdef CONFIG_IO_URING
4792 * smack_uring_override_creds - Is io_uring cred override allowed?
4793 * @new: the target creds
4795 * Check to see if the current task is allowed to override it's credentials
4796 * to service an io_uring operation.
4798 static int smack_uring_override_creds(const struct cred *new)
4800 struct task_smack *tsp = smack_cred(current_cred());
4801 struct task_smack *nsp = smack_cred(new);
4804 * Allow the degenerate case where the new Smack value is
4805 * the same as the current Smack value.
4807 if (tsp->smk_task == nsp->smk_task)
4810 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4817 * smack_uring_sqpoll - check if a io_uring polling thread can be created
4819 * Check to see if the current task is allowed to create a new io_uring
4820 * kernel polling thread.
4822 static int smack_uring_sqpoll(void)
4824 if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred()))
4831 * smack_uring_cmd - check on file operations for io_uring
4832 * @ioucmd: the command in question
4834 * Make a best guess about whether a io_uring "command" should
4835 * be allowed. Use the same logic used for determining if the
4836 * file could be opened for read in the absence of better criteria.
4838 static int smack_uring_cmd(struct io_uring_cmd *ioucmd)
4840 struct file *file = ioucmd->file;
4841 struct smk_audit_info ad;
4842 struct task_smack *tsp;
4843 struct inode *inode;
4849 tsp = smack_cred(file->f_cred);
4850 inode = file_inode(file);
4852 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
4853 smk_ad_setfield_u_fs_path(&ad, file->f_path);
4854 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
4855 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
4860 #endif /* CONFIG_IO_URING */
4862 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4863 .lbs_cred = sizeof(struct task_smack),
4864 .lbs_file = sizeof(struct smack_known *),
4865 .lbs_inode = sizeof(struct inode_smack),
4866 .lbs_ipc = sizeof(struct smack_known *),
4867 .lbs_msg_msg = sizeof(struct smack_known *),
4868 .lbs_superblock = sizeof(struct superblock_smack),
4871 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4872 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4873 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4874 LSM_HOOK_INIT(syslog, smack_syslog),
4876 LSM_HOOK_INIT(fs_context_submount, smack_fs_context_submount),
4877 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4878 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4880 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4881 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4882 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4883 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4884 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4886 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
4888 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4889 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4890 LSM_HOOK_INIT(inode_link, smack_inode_link),
4891 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4892 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4893 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4894 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4895 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4896 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4897 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4898 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4899 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4900 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4901 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4902 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4903 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4904 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4906 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4907 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4908 LSM_HOOK_INIT(file_lock, smack_file_lock),
4909 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4910 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4911 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4912 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4913 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4914 LSM_HOOK_INIT(file_receive, smack_file_receive),
4916 LSM_HOOK_INIT(file_open, smack_file_open),
4918 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4919 LSM_HOOK_INIT(cred_free, smack_cred_free),
4920 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4921 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4922 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4923 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4924 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4925 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4926 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4927 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4928 LSM_HOOK_INIT(current_getsecid_subj, smack_current_getsecid_subj),
4929 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
4930 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4931 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4932 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4933 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4934 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4935 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4936 LSM_HOOK_INIT(task_kill, smack_task_kill),
4937 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4939 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4940 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4942 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4944 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4945 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4946 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4947 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4948 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4950 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4951 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4952 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4953 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4955 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4956 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4957 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4958 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4960 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4962 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4963 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4965 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4966 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4968 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4969 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4970 #ifdef SMACK_IPV6_PORT_LABELING
4971 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4973 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4974 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4975 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4976 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4977 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4978 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4979 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4980 LSM_HOOK_INIT(sk_clone_security, smack_sk_clone_security),
4981 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4982 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4983 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4985 /* key management security hooks */
4987 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4988 LSM_HOOK_INIT(key_free, smack_key_free),
4989 LSM_HOOK_INIT(key_permission, smack_key_permission),
4990 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4991 #ifdef CONFIG_KEY_NOTIFICATIONS
4992 LSM_HOOK_INIT(watch_key, smack_watch_key),
4994 #endif /* CONFIG_KEYS */
4996 #ifdef CONFIG_WATCH_QUEUE
4997 LSM_HOOK_INIT(post_notification, smack_post_notification),
5002 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
5003 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
5004 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
5005 #endif /* CONFIG_AUDIT */
5007 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
5008 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
5009 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
5010 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
5011 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
5012 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
5013 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
5014 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
5015 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
5016 #ifdef CONFIG_IO_URING
5017 LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds),
5018 LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll),
5019 LSM_HOOK_INIT(uring_cmd, smack_uring_cmd),
5024 static __init void init_smack_known_list(void)
5027 * Initialize rule list locks
5029 mutex_init(&smack_known_huh.smk_rules_lock);
5030 mutex_init(&smack_known_hat.smk_rules_lock);
5031 mutex_init(&smack_known_floor.smk_rules_lock);
5032 mutex_init(&smack_known_star.smk_rules_lock);
5033 mutex_init(&smack_known_web.smk_rules_lock);
5035 * Initialize rule lists
5037 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
5038 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
5039 INIT_LIST_HEAD(&smack_known_star.smk_rules);
5040 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
5041 INIT_LIST_HEAD(&smack_known_web.smk_rules);
5043 * Create the known labels list
5045 smk_insert_entry(&smack_known_huh);
5046 smk_insert_entry(&smack_known_hat);
5047 smk_insert_entry(&smack_known_star);
5048 smk_insert_entry(&smack_known_floor);
5049 smk_insert_entry(&smack_known_web);
5053 * smack_init - initialize the smack system
5055 * Returns 0 on success, -ENOMEM is there's no memory
5057 static __init int smack_init(void)
5059 struct cred *cred = (struct cred *) current->cred;
5060 struct task_smack *tsp;
5062 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
5063 if (!smack_rule_cache)
5067 * Set the security state for the initial task.
5069 tsp = smack_cred(cred);
5070 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
5075 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
5078 pr_info("Smack: Initializing.\n");
5079 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
5080 pr_info("Smack: Netfilter enabled.\n");
5082 #ifdef SMACK_IPV6_PORT_LABELING
5083 pr_info("Smack: IPv6 port labeling enabled.\n");
5085 #ifdef SMACK_IPV6_SECMARK_LABELING
5086 pr_info("Smack: IPv6 Netfilter enabled.\n");
5089 /* initialize the smack_known_list */
5090 init_smack_known_list();
5096 * Smack requires early initialization in order to label
5097 * all processes and objects when they are created.
5099 DEFINE_LSM(smack) = {
5101 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
5102 .blobs = &smack_blob_sizes,