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
56 * Smack uses multiple xattrs.
57 * SMACK64 - for access control,
58 * SMACK64TRANSMUTE - label initialization,
59 * Not saved on files - SMACK64IPIN and SMACK64IPOUT,
60 * Must be set explicitly - SMACK64EXEC and SMACK64MMAP
62 #define SMACK_INODE_INIT_XATTRS 2
64 #ifdef SMACK_IPV6_PORT_LABELING
65 static DEFINE_MUTEX(smack_ipv6_lock);
66 static LIST_HEAD(smk_ipv6_port_list);
68 struct kmem_cache *smack_rule_cache;
69 int smack_enabled __initdata;
71 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
76 } smk_mount_opts[] = {
77 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
78 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
82 static int match_opt_prefix(char *s, int l, char **arg)
86 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
87 size_t len = smk_mount_opts[i].len;
88 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
90 if (len == l || s[len] != '=')
93 return smk_mount_opts[i].opt;
98 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
99 static char *smk_bu_mess[] = {
100 "Bringup Error", /* Unused */
101 "Bringup", /* SMACK_BRINGUP_ALLOW */
102 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
103 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
106 static void smk_bu_mode(int mode, char *s)
112 if (mode & MAY_WRITE)
116 if (mode & MAY_APPEND)
118 if (mode & MAY_TRANSMUTE)
128 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
129 static int smk_bu_note(char *note, struct smack_known *sskp,
130 struct smack_known *oskp, int mode, int rc)
132 char acc[SMK_NUM_ACCESS_TYPE + 1];
136 if (rc > SMACK_UNCONFINED_OBJECT)
139 smk_bu_mode(mode, acc);
140 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
141 sskp->smk_known, oskp->smk_known, acc, note);
145 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
148 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
149 static int smk_bu_current(char *note, struct smack_known *oskp,
152 struct task_smack *tsp = smack_cred(current_cred());
153 char acc[SMK_NUM_ACCESS_TYPE + 1];
157 if (rc > SMACK_UNCONFINED_OBJECT)
160 smk_bu_mode(mode, acc);
161 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
162 tsp->smk_task->smk_known, oskp->smk_known,
163 acc, current->comm, note);
167 #define smk_bu_current(note, oskp, mode, RC) (RC)
170 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
171 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
173 struct task_smack *tsp = smack_cred(current_cred());
174 struct smack_known *smk_task = smk_of_task_struct_obj(otp);
175 char acc[SMK_NUM_ACCESS_TYPE + 1];
179 if (rc > SMACK_UNCONFINED_OBJECT)
182 smk_bu_mode(mode, acc);
183 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
184 tsp->smk_task->smk_known, smk_task->smk_known, acc,
185 current->comm, otp->comm);
189 #define smk_bu_task(otp, mode, RC) (RC)
192 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
193 static int smk_bu_inode(struct inode *inode, int mode, int rc)
195 struct task_smack *tsp = smack_cred(current_cred());
196 struct inode_smack *isp = smack_inode(inode);
197 char acc[SMK_NUM_ACCESS_TYPE + 1];
199 if (isp->smk_flags & SMK_INODE_IMPURE)
200 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
201 inode->i_sb->s_id, inode->i_ino, current->comm);
205 if (rc > SMACK_UNCONFINED_OBJECT)
207 if (rc == SMACK_UNCONFINED_SUBJECT &&
208 (mode & (MAY_WRITE | MAY_APPEND)))
209 isp->smk_flags |= SMK_INODE_IMPURE;
211 smk_bu_mode(mode, acc);
213 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
214 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
215 inode->i_sb->s_id, inode->i_ino, current->comm);
219 #define smk_bu_inode(inode, mode, RC) (RC)
222 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
223 static int smk_bu_file(struct file *file, int mode, int rc)
225 struct task_smack *tsp = smack_cred(current_cred());
226 struct smack_known *sskp = tsp->smk_task;
227 struct inode *inode = file_inode(file);
228 struct inode_smack *isp = smack_inode(inode);
229 char acc[SMK_NUM_ACCESS_TYPE + 1];
231 if (isp->smk_flags & SMK_INODE_IMPURE)
232 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
233 inode->i_sb->s_id, inode->i_ino, current->comm);
237 if (rc > SMACK_UNCONFINED_OBJECT)
240 smk_bu_mode(mode, acc);
241 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
242 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
243 inode->i_sb->s_id, inode->i_ino, file,
248 #define smk_bu_file(file, mode, RC) (RC)
251 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
252 static int smk_bu_credfile(const struct cred *cred, struct file *file,
255 struct task_smack *tsp = smack_cred(cred);
256 struct smack_known *sskp = tsp->smk_task;
257 struct inode *inode = file_inode(file);
258 struct inode_smack *isp = smack_inode(inode);
259 char acc[SMK_NUM_ACCESS_TYPE + 1];
261 if (isp->smk_flags & SMK_INODE_IMPURE)
262 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
263 inode->i_sb->s_id, inode->i_ino, current->comm);
267 if (rc > SMACK_UNCONFINED_OBJECT)
270 smk_bu_mode(mode, acc);
271 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
272 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
273 inode->i_sb->s_id, inode->i_ino, file,
278 #define smk_bu_credfile(cred, file, mode, RC) (RC)
282 * smk_fetch - Fetch the smack label from a file.
283 * @name: type of the label (attribute)
284 * @ip: a pointer to the inode
285 * @dp: a pointer to the dentry
287 * Returns a pointer to the master list entry for the Smack label,
288 * NULL if there was no label to fetch, or an error code.
290 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
295 struct smack_known *skp = NULL;
297 if (!(ip->i_opflags & IOP_XATTR))
298 return ERR_PTR(-EOPNOTSUPP);
300 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
302 return ERR_PTR(-ENOMEM);
304 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
310 skp = smk_import_entry(buffer, rc);
318 * init_inode_smack - initialize an inode security blob
319 * @inode: inode to extract the info from
320 * @skp: a pointer to the Smack label entry to use in the blob
323 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
325 struct inode_smack *isp = smack_inode(inode);
327 isp->smk_inode = skp;
332 * init_task_smack - initialize a task security blob
333 * @tsp: blob to initialize
334 * @task: a pointer to the Smack label for the running task
335 * @forked: a pointer to the Smack label for the forked task
338 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
339 struct smack_known *forked)
341 tsp->smk_task = task;
342 tsp->smk_forked = forked;
343 INIT_LIST_HEAD(&tsp->smk_rules);
344 INIT_LIST_HEAD(&tsp->smk_relabel);
345 mutex_init(&tsp->smk_rules_lock);
349 * smk_copy_rules - copy a rule set
350 * @nhead: new rules header pointer
351 * @ohead: old rules header pointer
352 * @gfp: type of the memory for the allocation
354 * Returns 0 on success, -ENOMEM on error
356 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
359 struct smack_rule *nrp;
360 struct smack_rule *orp;
363 list_for_each_entry_rcu(orp, ohead, list) {
364 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
370 list_add_rcu(&nrp->list, nhead);
376 * smk_copy_relabel - copy smk_relabel labels list
377 * @nhead: new rules header pointer
378 * @ohead: old rules header pointer
379 * @gfp: type of the memory for the allocation
381 * Returns 0 on success, -ENOMEM on error
383 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
386 struct smack_known_list_elem *nklep;
387 struct smack_known_list_elem *oklep;
389 list_for_each_entry(oklep, ohead, list) {
390 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
392 smk_destroy_label_list(nhead);
395 nklep->smk_label = oklep->smk_label;
396 list_add(&nklep->list, nhead);
403 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
404 * @mode: input mode in form of PTRACE_MODE_*
406 * Returns a converted MAY_* mode usable by smack rules
408 static inline unsigned int smk_ptrace_mode(unsigned int mode)
410 if (mode & PTRACE_MODE_ATTACH)
411 return MAY_READWRITE;
412 if (mode & PTRACE_MODE_READ)
419 * smk_ptrace_rule_check - helper for ptrace access
420 * @tracer: tracer process
421 * @tracee_known: label entry of the process that's about to be traced
422 * @mode: ptrace attachment mode (PTRACE_MODE_*)
423 * @func: name of the function that called us, used for audit
425 * Returns 0 on access granted, -error on error
427 static int smk_ptrace_rule_check(struct task_struct *tracer,
428 struct smack_known *tracee_known,
429 unsigned int mode, const char *func)
432 struct smk_audit_info ad, *saip = NULL;
433 struct task_smack *tsp;
434 struct smack_known *tracer_known;
435 const struct cred *tracercred;
437 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
438 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
439 smk_ad_setfield_u_tsk(&ad, tracer);
444 tracercred = __task_cred(tracer);
445 tsp = smack_cred(tracercred);
446 tracer_known = smk_of_task(tsp);
448 if ((mode & PTRACE_MODE_ATTACH) &&
449 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
450 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
451 if (tracer_known->smk_known == tracee_known->smk_known)
453 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
455 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
461 smack_log(tracer_known->smk_known,
462 tracee_known->smk_known,
469 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
470 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
478 * We he, that is fun!
482 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
483 * @ctp: child task pointer
484 * @mode: ptrace attachment mode (PTRACE_MODE_*)
486 * Returns 0 if access is OK, an error code otherwise
488 * Do the capability checks.
490 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
492 struct smack_known *skp;
494 skp = smk_of_task_struct_obj(ctp);
496 return smk_ptrace_rule_check(current, skp, mode, __func__);
500 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
501 * @ptp: parent task pointer
503 * Returns 0 if access is OK, an error code otherwise
505 * Do the capability checks, and require PTRACE_MODE_ATTACH.
507 static int smack_ptrace_traceme(struct task_struct *ptp)
509 struct smack_known *skp;
511 skp = smk_of_task(smack_cred(current_cred()));
513 return smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
517 * smack_syslog - Smack approval on syslog
518 * @typefrom_file: unused
520 * Returns 0 on success, error code otherwise.
522 static int smack_syslog(int typefrom_file)
525 struct smack_known *skp = smk_of_current();
527 if (smack_privileged(CAP_MAC_OVERRIDE))
530 if (smack_syslog_label != NULL && smack_syslog_label != skp)
541 * smack_sb_alloc_security - allocate a superblock blob
542 * @sb: the superblock getting the blob
544 * Returns 0 on success or -ENOMEM on error.
546 static int smack_sb_alloc_security(struct super_block *sb)
548 struct superblock_smack *sbsp = smack_superblock(sb);
550 sbsp->smk_root = &smack_known_floor;
551 sbsp->smk_default = &smack_known_floor;
552 sbsp->smk_floor = &smack_known_floor;
553 sbsp->smk_hat = &smack_known_hat;
555 * SMK_SB_INITIALIZED will be zero from kzalloc.
561 struct smack_mnt_opts {
562 const char *fsdefault;
566 const char *fstransmute;
569 static void smack_free_mnt_opts(void *mnt_opts)
574 static int smack_add_opt(int token, const char *s, void **mnt_opts)
576 struct smack_mnt_opts *opts = *mnt_opts;
577 struct smack_known *skp;
580 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
588 skp = smk_import_entry(s, 0);
596 opts->fsdefault = skp->smk_known;
601 opts->fsfloor = skp->smk_known;
606 opts->fshat = skp->smk_known;
611 opts->fsroot = skp->smk_known;
613 case Opt_fstransmute:
614 if (opts->fstransmute)
616 opts->fstransmute = skp->smk_known;
622 pr_warn("Smack: duplicate mount options\n");
627 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
628 * @fc: The new filesystem context.
629 * @src_fc: The source filesystem context being duplicated.
631 * Returns 0 on success or -ENOMEM on error.
633 static int smack_fs_context_dup(struct fs_context *fc,
634 struct fs_context *src_fc)
636 struct smack_mnt_opts *dst, *src = src_fc->security;
641 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
646 dst->fsdefault = src->fsdefault;
647 dst->fsfloor = src->fsfloor;
648 dst->fshat = src->fshat;
649 dst->fsroot = src->fsroot;
650 dst->fstransmute = src->fstransmute;
655 static const struct fs_parameter_spec smack_fs_parameters[] = {
656 fsparam_string("smackfsdef", Opt_fsdefault),
657 fsparam_string("smackfsdefault", Opt_fsdefault),
658 fsparam_string("smackfsfloor", Opt_fsfloor),
659 fsparam_string("smackfshat", Opt_fshat),
660 fsparam_string("smackfsroot", Opt_fsroot),
661 fsparam_string("smackfstransmute", Opt_fstransmute),
666 * smack_fs_context_parse_param - Parse a single mount parameter
667 * @fc: The new filesystem context being constructed.
668 * @param: The parameter.
670 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
673 static int smack_fs_context_parse_param(struct fs_context *fc,
674 struct fs_parameter *param)
676 struct fs_parse_result result;
679 opt = fs_parse(fc, smack_fs_parameters, param, &result);
683 rc = smack_add_opt(opt, param->string, &fc->security);
685 param->string = NULL;
689 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
691 char *from = options, *to = options;
695 char *next = strchr(from, ',');
704 token = match_opt_prefix(from, len, &arg);
705 if (token != Opt_error) {
706 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
707 rc = smack_add_opt(token, arg, mnt_opts);
711 smack_free_mnt_opts(*mnt_opts);
716 if (!first) { // copy with preceding comma
721 memmove(to, from, len);
734 * smack_set_mnt_opts - set Smack specific mount options
735 * @sb: the file system superblock
736 * @mnt_opts: Smack mount options
737 * @kern_flags: mount option from kernel space or user space
738 * @set_kern_flags: where to store converted mount opts
740 * Returns 0 on success, an error code on failure
742 * Allow filesystems with binary mount data to explicitly set Smack mount
745 static int smack_set_mnt_opts(struct super_block *sb,
747 unsigned long kern_flags,
748 unsigned long *set_kern_flags)
750 struct dentry *root = sb->s_root;
751 struct inode *inode = d_backing_inode(root);
752 struct superblock_smack *sp = smack_superblock(sb);
753 struct inode_smack *isp;
754 struct smack_known *skp;
755 struct smack_mnt_opts *opts = mnt_opts;
756 bool transmute = false;
758 if (sp->smk_flags & SMK_SB_INITIALIZED)
761 if (!smack_privileged(CAP_MAC_ADMIN)) {
763 * Unprivileged mounts don't get to specify Smack values.
768 * Unprivileged mounts get root and default from the caller.
770 skp = smk_of_current();
772 sp->smk_default = skp;
774 * For a handful of fs types with no user-controlled
775 * backing store it's okay to trust security labels
776 * in the filesystem. The rest are untrusted.
778 if (sb->s_user_ns != &init_user_ns &&
779 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
780 sb->s_magic != RAMFS_MAGIC) {
782 sp->smk_flags |= SMK_SB_UNTRUSTED;
786 sp->smk_flags |= SMK_SB_INITIALIZED;
789 if (opts->fsdefault) {
790 skp = smk_import_entry(opts->fsdefault, 0);
793 sp->smk_default = skp;
796 skp = smk_import_entry(opts->fsfloor, 0);
802 skp = smk_import_entry(opts->fshat, 0);
808 skp = smk_import_entry(opts->fsroot, 0);
813 if (opts->fstransmute) {
814 skp = smk_import_entry(opts->fstransmute, 0);
823 * Initialize the root inode.
825 init_inode_smack(inode, sp->smk_root);
828 isp = smack_inode(inode);
829 isp->smk_flags |= SMK_INODE_TRANSMUTE;
836 * smack_sb_statfs - Smack check on statfs
837 * @dentry: identifies the file system in question
839 * Returns 0 if current can read the floor of the filesystem,
840 * and error code otherwise
842 static int smack_sb_statfs(struct dentry *dentry)
844 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
846 struct smk_audit_info ad;
848 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
849 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
851 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
852 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
861 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
862 * @bprm: the exec information
864 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
866 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
868 struct inode *inode = file_inode(bprm->file);
869 struct task_smack *bsp = smack_cred(bprm->cred);
870 struct inode_smack *isp;
871 struct superblock_smack *sbsp;
874 isp = smack_inode(inode);
875 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
878 sbsp = smack_superblock(inode->i_sb);
879 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
880 isp->smk_task != sbsp->smk_root)
883 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
884 struct task_struct *tracer;
888 tracer = ptrace_parent(current);
889 if (likely(tracer != NULL))
890 rc = smk_ptrace_rule_check(tracer,
899 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
902 bsp->smk_task = isp->smk_task;
903 bprm->per_clear |= PER_CLEAR_ON_SETID;
905 /* Decide if this is a secure exec. */
906 if (bsp->smk_task != bsp->smk_forked)
907 bprm->secureexec = 1;
917 * smack_inode_alloc_security - allocate an inode blob
918 * @inode: the inode in need of a blob
922 static int smack_inode_alloc_security(struct inode *inode)
924 struct smack_known *skp = smk_of_current();
926 init_inode_smack(inode, skp);
931 * smack_inode_init_security - copy out the smack from an inode
932 * @inode: the newly created inode
933 * @dir: containing directory object
935 * @xattrs: where to put the attributes
936 * @xattr_count: current number of LSM-provided xattrs (updated)
938 * Returns 0 if it all works out, -ENOMEM if there's no memory
940 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
941 const struct qstr *qstr,
942 struct xattr *xattrs, int *xattr_count)
944 struct task_smack *tsp = smack_cred(current_cred());
945 struct smack_known *skp = smk_of_task(tsp);
946 struct smack_known *isp = smk_of_inode(inode);
947 struct smack_known *dsp = smk_of_inode(dir);
948 struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count);
953 * If equal, transmuting already occurred in
954 * smack_dentry_create_files_as(). No need to check again.
956 if (tsp->smk_task != tsp->smk_transmuted) {
958 may = smk_access_entry(skp->smk_known, dsp->smk_known,
964 * In addition to having smk_task equal to smk_transmuted,
965 * if the access rule allows transmutation and the directory
966 * requests transmutation then by all means transmute.
967 * Mark the inode as changed.
969 if ((tsp->smk_task == tsp->smk_transmuted) ||
970 (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
971 smk_inode_transmutable(dir))) {
972 struct xattr *xattr_transmute;
975 * The caller of smack_dentry_create_files_as()
976 * should have overridden the current cred, so the
977 * inode label was already set correctly in
978 * smack_inode_alloc_security().
980 if (tsp->smk_task != tsp->smk_transmuted)
982 xattr_transmute = lsm_get_xattr_slot(xattrs,
984 if (xattr_transmute) {
985 xattr_transmute->value = kmemdup(TRANS_TRUE,
988 if (!xattr_transmute->value)
991 xattr_transmute->value_len = TRANS_TRUE_SIZE;
992 xattr_transmute->name = XATTR_SMACK_TRANSMUTE;
996 xattr->value = kstrdup(isp->smk_known, GFP_NOFS);
1000 xattr->value_len = strlen(isp->smk_known);
1001 xattr->name = XATTR_SMACK_SUFFIX;
1008 * smack_inode_link - Smack check on link
1009 * @old_dentry: the existing object
1011 * @new_dentry: the new object
1013 * Returns 0 if access is permitted, an error code otherwise
1015 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1016 struct dentry *new_dentry)
1018 struct smack_known *isp;
1019 struct smk_audit_info ad;
1022 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1023 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1025 isp = smk_of_inode(d_backing_inode(old_dentry));
1026 rc = smk_curacc(isp, MAY_WRITE, &ad);
1027 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1029 if (rc == 0 && d_is_positive(new_dentry)) {
1030 isp = smk_of_inode(d_backing_inode(new_dentry));
1031 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1032 rc = smk_curacc(isp, MAY_WRITE, &ad);
1033 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1040 * smack_inode_unlink - Smack check on inode deletion
1041 * @dir: containing directory object
1042 * @dentry: file to unlink
1044 * Returns 0 if current can write the containing directory
1045 * and the object, error code otherwise
1047 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1049 struct inode *ip = d_backing_inode(dentry);
1050 struct smk_audit_info ad;
1053 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1054 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1057 * You need write access to the thing you're unlinking
1059 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1060 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1063 * You also need write access to the containing directory
1065 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1066 smk_ad_setfield_u_fs_inode(&ad, dir);
1067 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1068 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1074 * smack_inode_rmdir - Smack check on directory deletion
1075 * @dir: containing directory object
1076 * @dentry: directory to unlink
1078 * Returns 0 if current can write the containing directory
1079 * and the directory, error code otherwise
1081 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1083 struct smk_audit_info ad;
1086 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1087 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1090 * You need write access to the thing you're removing
1092 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1093 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1096 * You also need write access to the containing directory
1098 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1099 smk_ad_setfield_u_fs_inode(&ad, dir);
1100 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1101 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1108 * smack_inode_rename - Smack check on rename
1109 * @old_inode: unused
1110 * @old_dentry: the old object
1111 * @new_inode: unused
1112 * @new_dentry: the new object
1114 * Read and write access is required on both the old and
1117 * Returns 0 if access is permitted, an error code otherwise
1119 static int smack_inode_rename(struct inode *old_inode,
1120 struct dentry *old_dentry,
1121 struct inode *new_inode,
1122 struct dentry *new_dentry)
1125 struct smack_known *isp;
1126 struct smk_audit_info ad;
1128 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1129 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1131 isp = smk_of_inode(d_backing_inode(old_dentry));
1132 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1133 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1135 if (rc == 0 && d_is_positive(new_dentry)) {
1136 isp = smk_of_inode(d_backing_inode(new_dentry));
1137 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1138 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1139 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1145 * smack_inode_permission - Smack version of permission()
1146 * @inode: the inode in question
1147 * @mask: the access requested
1149 * This is the important Smack hook.
1151 * Returns 0 if access is permitted, an error code otherwise
1153 static int smack_inode_permission(struct inode *inode, int mask)
1155 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
1156 struct smk_audit_info ad;
1157 int no_block = mask & MAY_NOT_BLOCK;
1160 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1162 * No permission to check. Existence test. Yup, it's there.
1167 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1168 if (smk_of_inode(inode) != sbsp->smk_root)
1172 /* May be droppable after audit */
1175 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1176 smk_ad_setfield_u_fs_inode(&ad, inode);
1177 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1178 rc = smk_bu_inode(inode, mask, rc);
1183 * smack_inode_setattr - Smack check for setting attributes
1184 * @dentry: the object
1185 * @iattr: for the force flag
1187 * Returns 0 if access is permitted, an error code otherwise
1189 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1191 struct smk_audit_info ad;
1195 * Need to allow for clearing the setuid bit.
1197 if (iattr->ia_valid & ATTR_FORCE)
1199 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1200 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1202 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1203 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1208 * smack_inode_getattr - Smack check for getting attributes
1209 * @path: path to extract the info from
1211 * Returns 0 if access is permitted, an error code otherwise
1213 static int smack_inode_getattr(const struct path *path)
1215 struct smk_audit_info ad;
1216 struct inode *inode = d_backing_inode(path->dentry);
1219 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1220 smk_ad_setfield_u_fs_path(&ad, *path);
1221 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1222 rc = smk_bu_inode(inode, MAY_READ, rc);
1227 * smack_inode_setxattr - Smack check for setting xattrs
1228 * @idmap: idmap of the mount
1229 * @dentry: the object
1230 * @name: name of the attribute
1231 * @value: value of the attribute
1232 * @size: size of the value
1235 * This protects the Smack attribute explicitly.
1237 * Returns 0 if access is permitted, an error code otherwise
1239 static int smack_inode_setxattr(struct mnt_idmap *idmap,
1240 struct dentry *dentry, const char *name,
1241 const void *value, size_t size, int flags)
1243 struct smk_audit_info ad;
1244 struct smack_known *skp;
1246 int check_import = 0;
1251 * Check label validity here so import won't fail in post_setxattr
1253 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1254 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1255 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1258 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1259 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1263 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1265 if (size != TRANS_TRUE_SIZE ||
1266 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1269 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1271 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1274 if (rc == 0 && check_import) {
1275 skp = size ? smk_import_entry(value, size) : NULL;
1278 else if (skp == NULL || (check_star &&
1279 (skp == &smack_known_star || skp == &smack_known_web)))
1283 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1284 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1287 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1288 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1295 * smack_inode_post_setxattr - Apply the Smack update approved above
1297 * @name: attribute name
1298 * @value: attribute value
1299 * @size: attribute size
1302 * Set the pointer in the inode blob to the entry found
1303 * in the master label list.
1305 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1306 const void *value, size_t size, int flags)
1308 struct smack_known *skp;
1309 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1311 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1312 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1316 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1317 skp = smk_import_entry(value, size);
1319 isp->smk_inode = skp;
1320 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1321 skp = smk_import_entry(value, size);
1323 isp->smk_task = skp;
1324 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1325 skp = smk_import_entry(value, size);
1327 isp->smk_mmap = skp;
1334 * smack_inode_getxattr - Smack check on getxattr
1335 * @dentry: the object
1338 * Returns 0 if access is permitted, an error code otherwise
1340 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1342 struct smk_audit_info ad;
1345 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1346 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1348 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1349 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1354 * smack_inode_removexattr - Smack check on removexattr
1355 * @idmap: idmap of the mount
1356 * @dentry: the object
1357 * @name: name of the attribute
1359 * Removing the Smack attribute requires CAP_MAC_ADMIN
1361 * Returns 0 if access is permitted, an error code otherwise
1363 static int smack_inode_removexattr(struct mnt_idmap *idmap,
1364 struct dentry *dentry, const char *name)
1366 struct inode_smack *isp;
1367 struct smk_audit_info ad;
1370 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1371 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1372 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1373 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1374 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1375 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1376 if (!smack_privileged(CAP_MAC_ADMIN))
1379 rc = cap_inode_removexattr(idmap, dentry, name);
1384 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1385 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1387 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1388 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1392 isp = smack_inode(d_backing_inode(dentry));
1394 * Don't do anything special for these.
1395 * XATTR_NAME_SMACKIPIN
1396 * XATTR_NAME_SMACKIPOUT
1398 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1399 struct super_block *sbp = dentry->d_sb;
1400 struct superblock_smack *sbsp = smack_superblock(sbp);
1402 isp->smk_inode = sbsp->smk_default;
1403 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1404 isp->smk_task = NULL;
1405 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1406 isp->smk_mmap = NULL;
1407 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1408 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1414 * smack_inode_set_acl - Smack check for setting posix acls
1415 * @idmap: idmap of the mnt this request came from
1416 * @dentry: the object
1417 * @acl_name: name of the posix acl
1418 * @kacl: the posix acls
1420 * Returns 0 if access is permitted, an error code otherwise
1422 static int smack_inode_set_acl(struct mnt_idmap *idmap,
1423 struct dentry *dentry, const char *acl_name,
1424 struct posix_acl *kacl)
1426 struct smk_audit_info ad;
1429 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1430 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1432 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1433 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1438 * smack_inode_get_acl - Smack check for getting posix acls
1439 * @idmap: idmap of the mnt this request came from
1440 * @dentry: the object
1441 * @acl_name: name of the posix acl
1443 * Returns 0 if access is permitted, an error code otherwise
1445 static int smack_inode_get_acl(struct mnt_idmap *idmap,
1446 struct dentry *dentry, const char *acl_name)
1448 struct smk_audit_info ad;
1451 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1452 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1454 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1455 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1460 * smack_inode_remove_acl - Smack check for getting posix acls
1461 * @idmap: idmap of the mnt this request came from
1462 * @dentry: the object
1463 * @acl_name: name of the posix acl
1465 * Returns 0 if access is permitted, an error code otherwise
1467 static int smack_inode_remove_acl(struct mnt_idmap *idmap,
1468 struct dentry *dentry, const char *acl_name)
1470 struct smk_audit_info ad;
1473 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1474 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1476 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1477 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1482 * smack_inode_getsecurity - get smack xattrs
1483 * @idmap: idmap of the mount
1484 * @inode: the object
1485 * @name: attribute name
1486 * @buffer: where to put the result
1487 * @alloc: duplicate memory
1489 * Returns the size of the attribute or an error code
1491 static int smack_inode_getsecurity(struct mnt_idmap *idmap,
1492 struct inode *inode, const char *name,
1493 void **buffer, bool alloc)
1495 struct socket_smack *ssp;
1496 struct socket *sock;
1497 struct super_block *sbp;
1498 struct inode *ip = inode;
1499 struct smack_known *isp;
1500 struct inode_smack *ispp;
1504 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1505 isp = smk_of_inode(inode);
1506 } else if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) {
1507 ispp = smack_inode(inode);
1508 if (ispp->smk_flags & SMK_INODE_TRANSMUTE)
1514 * The rest of the Smack xattrs are only on sockets.
1517 if (sbp->s_magic != SOCKFS_MAGIC)
1520 sock = SOCKET_I(ip);
1521 if (sock == NULL || sock->sk == NULL)
1524 ssp = sock->sk->sk_security;
1526 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1528 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1535 label = isp->smk_known;
1537 label_len = strlen(label);
1540 *buffer = kstrdup(label, GFP_KERNEL);
1541 if (*buffer == NULL)
1550 * smack_inode_listsecurity - list the Smack attributes
1551 * @inode: the object
1552 * @buffer: where they go
1553 * @buffer_size: size of buffer
1555 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1558 int len = sizeof(XATTR_NAME_SMACK);
1560 if (buffer != NULL && len <= buffer_size)
1561 memcpy(buffer, XATTR_NAME_SMACK, len);
1567 * smack_inode_getsecid - Extract inode's security id
1568 * @inode: inode to extract the info from
1569 * @secid: where result will be saved
1571 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1573 struct smack_known *skp = smk_of_inode(inode);
1575 *secid = skp->smk_secid;
1583 * There is no smack_file_permission hook
1585 * Should access checks be done on each read or write?
1586 * UNICOS and SELinux say yes.
1587 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1589 * I'll say no for now. Smack does not do the frequent
1590 * label changing that SELinux does.
1594 * smack_file_alloc_security - assign a file security blob
1597 * The security blob for a file is a pointer to the master
1598 * label list, so no allocation is done.
1600 * f_security is the owner security information. It
1601 * isn't used on file access checks, it's for send_sigio.
1605 static int smack_file_alloc_security(struct file *file)
1607 struct smack_known **blob = smack_file(file);
1609 *blob = smk_of_current();
1614 * smack_file_ioctl - Smack check on ioctls
1619 * Relies heavily on the correct use of the ioctl command conventions.
1621 * Returns 0 if allowed, error code otherwise
1623 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1627 struct smk_audit_info ad;
1628 struct inode *inode = file_inode(file);
1630 if (unlikely(IS_PRIVATE(inode)))
1633 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1634 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1636 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1637 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1638 rc = smk_bu_file(file, MAY_WRITE, rc);
1641 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1642 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1643 rc = smk_bu_file(file, MAY_READ, rc);
1650 * smack_file_lock - Smack check on file locking
1654 * Returns 0 if current has lock access, error code otherwise
1656 static int smack_file_lock(struct file *file, unsigned int cmd)
1658 struct smk_audit_info ad;
1660 struct inode *inode = file_inode(file);
1662 if (unlikely(IS_PRIVATE(inode)))
1665 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1666 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1667 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1668 rc = smk_bu_file(file, MAY_LOCK, rc);
1673 * smack_file_fcntl - Smack check on fcntl
1675 * @cmd: what action to check
1678 * Generally these operations are harmless.
1679 * File locking operations present an obvious mechanism
1680 * for passing information, so they require write access.
1682 * Returns 0 if current has access, error code otherwise
1684 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1687 struct smk_audit_info ad;
1689 struct inode *inode = file_inode(file);
1691 if (unlikely(IS_PRIVATE(inode)))
1699 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1700 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1701 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1702 rc = smk_bu_file(file, MAY_LOCK, rc);
1706 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1707 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1708 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1709 rc = smk_bu_file(file, MAY_WRITE, rc);
1719 * smack_mmap_file - Check permissions for a mmap operation.
1720 * @file: contains the file structure for file to map (may be NULL).
1721 * @reqprot: contains the protection requested by the application.
1722 * @prot: contains the protection that will be applied by the kernel.
1723 * @flags: contains the operational flags.
1725 * The @file may be NULL, e.g. if mapping anonymous memory.
1727 * Return 0 if permission is granted.
1729 static int smack_mmap_file(struct file *file,
1730 unsigned long reqprot, unsigned long prot,
1731 unsigned long flags)
1733 struct smack_known *skp;
1734 struct smack_known *mkp;
1735 struct smack_rule *srp;
1736 struct task_smack *tsp;
1737 struct smack_known *okp;
1738 struct inode_smack *isp;
1739 struct superblock_smack *sbsp;
1748 if (unlikely(IS_PRIVATE(file_inode(file))))
1751 isp = smack_inode(file_inode(file));
1752 if (isp->smk_mmap == NULL)
1754 sbsp = smack_superblock(file_inode(file)->i_sb);
1755 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1756 isp->smk_mmap != sbsp->smk_root)
1758 mkp = isp->smk_mmap;
1760 tsp = smack_cred(current_cred());
1761 skp = smk_of_current();
1766 * For each Smack rule associated with the subject
1767 * label verify that the SMACK64MMAP also has access
1768 * to that rule's object label.
1770 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1771 okp = srp->smk_object;
1773 * Matching labels always allows access.
1775 if (mkp->smk_known == okp->smk_known)
1778 * If there is a matching local rule take
1779 * that into account as well.
1781 may = smk_access_entry(srp->smk_subject->smk_known,
1785 may = srp->smk_access;
1787 may &= srp->smk_access;
1789 * If may is zero the SMACK64MMAP subject can't
1790 * possibly have less access.
1796 * Fetch the global list entry.
1797 * If there isn't one a SMACK64MMAP subject
1798 * can't have as much access as current.
1800 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1802 if (mmay == -ENOENT) {
1807 * If there is a local entry it modifies the
1808 * potential access, too.
1810 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1812 if (tmay != -ENOENT)
1816 * If there is any access available to current that is
1817 * not available to a SMACK64MMAP subject
1820 if ((may | mmay) != mmay) {
1832 * smack_file_set_fowner - set the file security blob value
1833 * @file: object in question
1836 static void smack_file_set_fowner(struct file *file)
1838 struct smack_known **blob = smack_file(file);
1840 *blob = smk_of_current();
1844 * smack_file_send_sigiotask - Smack on sigio
1845 * @tsk: The target task
1846 * @fown: the object the signal come from
1849 * Allow a privileged task to get signals even if it shouldn't
1851 * Returns 0 if a subject with the object's smack could
1852 * write to the task, an error code otherwise.
1854 static int smack_file_send_sigiotask(struct task_struct *tsk,
1855 struct fown_struct *fown, int signum)
1857 struct smack_known **blob;
1858 struct smack_known *skp;
1859 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1860 const struct cred *tcred;
1863 struct smk_audit_info ad;
1866 * struct fown_struct is never outside the context of a struct file
1868 file = container_of(fown, struct file, f_owner);
1870 /* we don't log here as rc can be overriden */
1871 blob = smack_file(file);
1873 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1874 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1877 tcred = __task_cred(tsk);
1878 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1882 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1883 smk_ad_setfield_u_tsk(&ad, tsk);
1884 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1889 * smack_file_receive - Smack file receive check
1892 * Returns 0 if current has access, error code otherwise
1894 static int smack_file_receive(struct file *file)
1898 struct smk_audit_info ad;
1899 struct inode *inode = file_inode(file);
1900 struct socket *sock;
1901 struct task_smack *tsp;
1902 struct socket_smack *ssp;
1904 if (unlikely(IS_PRIVATE(inode)))
1907 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1908 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1910 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1911 sock = SOCKET_I(inode);
1912 ssp = sock->sk->sk_security;
1913 tsp = smack_cred(current_cred());
1915 * If the receiving process can't write to the
1916 * passed socket or if the passed socket can't
1917 * write to the receiving process don't accept
1918 * the passed socket.
1920 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1921 rc = smk_bu_file(file, may, rc);
1924 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1925 rc = smk_bu_file(file, may, rc);
1929 * This code relies on bitmasks.
1931 if (file->f_mode & FMODE_READ)
1933 if (file->f_mode & FMODE_WRITE)
1936 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1937 rc = smk_bu_file(file, may, rc);
1942 * smack_file_open - Smack dentry open processing
1945 * Set the security blob in the file structure.
1946 * Allow the open only if the task has read access. There are
1947 * many read operations (e.g. fstat) that you can do with an
1948 * fd even if you have the file open write-only.
1950 * Returns 0 if current has access, error code otherwise
1952 static int smack_file_open(struct file *file)
1954 struct task_smack *tsp = smack_cred(file->f_cred);
1955 struct inode *inode = file_inode(file);
1956 struct smk_audit_info ad;
1959 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1960 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1961 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1962 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1972 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1973 * @cred: the new credentials
1974 * @gfp: the atomicity of any memory allocations
1976 * Prepare a blank set of credentials for modification. This must allocate all
1977 * the memory the LSM module might require such that cred_transfer() can
1978 * complete without error.
1980 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1982 init_task_smack(smack_cred(cred), NULL, NULL);
1988 * smack_cred_free - "free" task-level security credentials
1989 * @cred: the credentials in question
1992 static void smack_cred_free(struct cred *cred)
1994 struct task_smack *tsp = smack_cred(cred);
1995 struct smack_rule *rp;
1996 struct list_head *l;
1997 struct list_head *n;
1999 smk_destroy_label_list(&tsp->smk_relabel);
2001 list_for_each_safe(l, n, &tsp->smk_rules) {
2002 rp = list_entry(l, struct smack_rule, list);
2003 list_del(&rp->list);
2004 kmem_cache_free(smack_rule_cache, rp);
2009 * smack_cred_prepare - prepare new set of credentials for modification
2010 * @new: the new credentials
2011 * @old: the original credentials
2012 * @gfp: the atomicity of any memory allocations
2014 * Prepare a new set of credentials for modification.
2016 static int smack_cred_prepare(struct cred *new, const struct cred *old,
2019 struct task_smack *old_tsp = smack_cred(old);
2020 struct task_smack *new_tsp = smack_cred(new);
2023 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
2025 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2029 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2035 * smack_cred_transfer - Transfer the old credentials to the new credentials
2036 * @new: the new credentials
2037 * @old: the original credentials
2039 * Fill in a set of blank credentials from another set of credentials.
2041 static void smack_cred_transfer(struct cred *new, const struct cred *old)
2043 struct task_smack *old_tsp = smack_cred(old);
2044 struct task_smack *new_tsp = smack_cred(new);
2046 new_tsp->smk_task = old_tsp->smk_task;
2047 new_tsp->smk_forked = old_tsp->smk_task;
2048 mutex_init(&new_tsp->smk_rules_lock);
2049 INIT_LIST_HEAD(&new_tsp->smk_rules);
2051 /* cbs copy rule list */
2055 * smack_cred_getsecid - get the secid corresponding to a creds structure
2056 * @cred: the object creds
2057 * @secid: where to put the result
2059 * Sets the secid to contain a u32 version of the smack label.
2061 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
2063 struct smack_known *skp;
2066 skp = smk_of_task(smack_cred(cred));
2067 *secid = skp->smk_secid;
2072 * smack_kernel_act_as - Set the subjective context in a set of credentials
2073 * @new: points to the set of credentials to be modified.
2074 * @secid: specifies the security ID to be set
2076 * Set the security data for a kernel service.
2078 static int smack_kernel_act_as(struct cred *new, u32 secid)
2080 struct task_smack *new_tsp = smack_cred(new);
2082 new_tsp->smk_task = smack_from_secid(secid);
2087 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2088 * @new: points to the set of credentials to be modified
2089 * @inode: points to the inode to use as a reference
2091 * Set the file creation context in a set of credentials to the same
2092 * as the objective context of the specified inode
2094 static int smack_kernel_create_files_as(struct cred *new,
2095 struct inode *inode)
2097 struct inode_smack *isp = smack_inode(inode);
2098 struct task_smack *tsp = smack_cred(new);
2100 tsp->smk_forked = isp->smk_inode;
2101 tsp->smk_task = tsp->smk_forked;
2106 * smk_curacc_on_task - helper to log task related access
2107 * @p: the task object
2108 * @access: the access requested
2109 * @caller: name of the calling function for audit
2111 * Return 0 if access is permitted
2113 static int smk_curacc_on_task(struct task_struct *p, int access,
2116 struct smk_audit_info ad;
2117 struct smack_known *skp = smk_of_task_struct_obj(p);
2120 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2121 smk_ad_setfield_u_tsk(&ad, p);
2122 rc = smk_curacc(skp, access, &ad);
2123 rc = smk_bu_task(p, access, rc);
2128 * smack_task_setpgid - Smack check on setting pgid
2129 * @p: the task object
2132 * Return 0 if write access is permitted
2134 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2136 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2140 * smack_task_getpgid - Smack access check for getpgid
2141 * @p: the object task
2143 * Returns 0 if current can read the object task, error code otherwise
2145 static int smack_task_getpgid(struct task_struct *p)
2147 return smk_curacc_on_task(p, MAY_READ, __func__);
2151 * smack_task_getsid - Smack access check for getsid
2152 * @p: the object task
2154 * Returns 0 if current can read the object task, error code otherwise
2156 static int smack_task_getsid(struct task_struct *p)
2158 return smk_curacc_on_task(p, MAY_READ, __func__);
2162 * smack_current_getsecid_subj - get the subjective secid of the current task
2163 * @secid: where to put the result
2165 * Sets the secid to contain a u32 version of the task's subjective smack label.
2167 static void smack_current_getsecid_subj(u32 *secid)
2169 struct smack_known *skp = smk_of_current();
2171 *secid = skp->smk_secid;
2175 * smack_task_getsecid_obj - get the objective secid of the task
2177 * @secid: where to put the result
2179 * Sets the secid to contain a u32 version of the task's objective smack label.
2181 static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2183 struct smack_known *skp = smk_of_task_struct_obj(p);
2185 *secid = skp->smk_secid;
2189 * smack_task_setnice - Smack check on setting nice
2190 * @p: the task object
2193 * Return 0 if write access is permitted
2195 static int smack_task_setnice(struct task_struct *p, int nice)
2197 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2201 * smack_task_setioprio - Smack check on setting ioprio
2202 * @p: the task object
2205 * Return 0 if write access is permitted
2207 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2209 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2213 * smack_task_getioprio - Smack check on reading ioprio
2214 * @p: the task object
2216 * Return 0 if read access is permitted
2218 static int smack_task_getioprio(struct task_struct *p)
2220 return smk_curacc_on_task(p, MAY_READ, __func__);
2224 * smack_task_setscheduler - Smack check on setting scheduler
2225 * @p: the task object
2227 * Return 0 if read access is permitted
2229 static int smack_task_setscheduler(struct task_struct *p)
2231 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2235 * smack_task_getscheduler - Smack check on reading scheduler
2236 * @p: the task object
2238 * Return 0 if read access is permitted
2240 static int smack_task_getscheduler(struct task_struct *p)
2242 return smk_curacc_on_task(p, MAY_READ, __func__);
2246 * smack_task_movememory - Smack check on moving memory
2247 * @p: the task object
2249 * Return 0 if write access is permitted
2251 static int smack_task_movememory(struct task_struct *p)
2253 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2257 * smack_task_kill - Smack check on signal delivery
2258 * @p: the task object
2261 * @cred: identifies the cred to use in lieu of current's
2263 * Return 0 if write access is permitted
2266 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2267 int sig, const struct cred *cred)
2269 struct smk_audit_info ad;
2270 struct smack_known *skp;
2271 struct smack_known *tkp = smk_of_task_struct_obj(p);
2275 return 0; /* null signal; existence test */
2277 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2278 smk_ad_setfield_u_tsk(&ad, p);
2280 * Sending a signal requires that the sender
2281 * can write the receiver.
2284 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2285 rc = smk_bu_task(p, MAY_DELIVER, rc);
2289 * If the cred isn't NULL we're dealing with some USB IO
2290 * specific behavior. This is not clean. For one thing
2291 * we can't take privilege into account.
2293 skp = smk_of_task(smack_cred(cred));
2294 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2295 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2300 * smack_task_to_inode - copy task smack into the inode blob
2301 * @p: task to copy from
2302 * @inode: inode to copy to
2304 * Sets the smack pointer in the inode security blob
2306 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2308 struct inode_smack *isp = smack_inode(inode);
2309 struct smack_known *skp = smk_of_task_struct_obj(p);
2311 isp->smk_inode = skp;
2312 isp->smk_flags |= SMK_INODE_INSTANT;
2320 * smack_sk_alloc_security - Allocate a socket blob
2323 * @gfp_flags: memory allocation flags
2325 * Assign Smack pointers to current
2327 * Returns 0 on success, -ENOMEM is there's no memory
2329 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2331 struct smack_known *skp = smk_of_current();
2332 struct socket_smack *ssp;
2334 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2339 * Sockets created by kernel threads receive web label.
2341 if (unlikely(current->flags & PF_KTHREAD)) {
2342 ssp->smk_in = &smack_known_web;
2343 ssp->smk_out = &smack_known_web;
2348 ssp->smk_packet = NULL;
2350 sk->sk_security = ssp;
2356 * smack_sk_free_security - Free a socket blob
2359 * Clears the blob pointer
2361 static void smack_sk_free_security(struct sock *sk)
2363 #ifdef SMACK_IPV6_PORT_LABELING
2364 struct smk_port_label *spp;
2366 if (sk->sk_family == PF_INET6) {
2368 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2369 if (spp->smk_sock != sk)
2371 spp->smk_can_reuse = 1;
2377 kfree(sk->sk_security);
2381 * smack_sk_clone_security - Copy security context
2382 * @sk: the old socket
2383 * @newsk: the new socket
2385 * Copy the security context of the old socket pointer to the cloned
2387 static void smack_sk_clone_security(const struct sock *sk, struct sock *newsk)
2389 struct socket_smack *ssp_old = sk->sk_security;
2390 struct socket_smack *ssp_new = newsk->sk_security;
2392 *ssp_new = *ssp_old;
2396 * smack_ipv4host_label - check host based restrictions
2397 * @sip: the object end
2399 * looks for host based access restrictions
2401 * This version will only be appropriate for really small sets of single label
2402 * hosts. The caller is responsible for ensuring that the RCU read lock is
2403 * taken before calling this function.
2405 * Returns the label of the far end or NULL if it's not special.
2407 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2409 struct smk_net4addr *snp;
2410 struct in_addr *siap = &sip->sin_addr;
2412 if (siap->s_addr == 0)
2415 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2417 * we break after finding the first match because
2418 * the list is sorted from longest to shortest mask
2419 * so we have found the most specific match
2421 if (snp->smk_host.s_addr ==
2422 (siap->s_addr & snp->smk_mask.s_addr))
2423 return snp->smk_label;
2429 * smk_ipv6_localhost - Check for local ipv6 host address
2432 * Returns boolean true if this is the localhost address
2434 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2436 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2437 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2439 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2440 ntohs(be16p[7]) == 1)
2446 * smack_ipv6host_label - check host based restrictions
2447 * @sip: the object end
2449 * looks for host based access restrictions
2451 * This version will only be appropriate for really small sets of single label
2452 * hosts. The caller is responsible for ensuring that the RCU read lock is
2453 * taken before calling this function.
2455 * Returns the label of the far end or NULL if it's not special.
2457 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2459 struct smk_net6addr *snp;
2460 struct in6_addr *sap = &sip->sin6_addr;
2465 * It's local. Don't look for a host label.
2467 if (smk_ipv6_localhost(sip))
2470 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2472 * If the label is NULL the entry has
2473 * been renounced. Ignore it.
2475 if (snp->smk_label == NULL)
2478 * we break after finding the first match because
2479 * the list is sorted from longest to shortest mask
2480 * so we have found the most specific match
2482 for (found = 1, i = 0; i < 8; i++) {
2483 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2484 snp->smk_host.s6_addr16[i]) {
2490 return snp->smk_label;
2497 * smack_netlbl_add - Set the secattr on a socket
2500 * Attach the outbound smack value (smk_out) to the socket.
2502 * Returns 0 on success or an error code
2504 static int smack_netlbl_add(struct sock *sk)
2506 struct socket_smack *ssp = sk->sk_security;
2507 struct smack_known *skp = ssp->smk_out;
2511 bh_lock_sock_nested(sk);
2513 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2516 ssp->smk_state = SMK_NETLBL_LABELED;
2519 ssp->smk_state = SMK_NETLBL_REQSKB;
2531 * smack_netlbl_delete - Remove the secattr from a socket
2534 * Remove the outbound smack value from a socket
2536 static void smack_netlbl_delete(struct sock *sk)
2538 struct socket_smack *ssp = sk->sk_security;
2541 * Take the label off the socket if one is set.
2543 if (ssp->smk_state != SMK_NETLBL_LABELED)
2547 bh_lock_sock_nested(sk);
2548 netlbl_sock_delattr(sk);
2551 ssp->smk_state = SMK_NETLBL_UNLABELED;
2555 * smk_ipv4_check - Perform IPv4 host access checks
2557 * @sap: the destination address
2559 * Set the correct secattr for the given socket based on the destination
2560 * address and perform any outbound access checks needed.
2562 * Returns 0 on success or an error code.
2565 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2567 struct smack_known *skp;
2569 struct smack_known *hkp;
2570 struct socket_smack *ssp = sk->sk_security;
2571 struct smk_audit_info ad;
2574 hkp = smack_ipv4host_label(sap);
2577 struct lsm_network_audit net;
2579 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2580 ad.a.u.net->family = sap->sin_family;
2581 ad.a.u.net->dport = sap->sin_port;
2582 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2585 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2586 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2588 * Clear the socket netlabel if it's set.
2591 smack_netlbl_delete(sk);
2599 * smk_ipv6_check - check Smack access
2600 * @subject: subject Smack label
2601 * @object: object Smack label
2603 * @act: the action being taken
2605 * Check an IPv6 access
2607 static int smk_ipv6_check(struct smack_known *subject,
2608 struct smack_known *object,
2609 struct sockaddr_in6 *address, int act)
2612 struct lsm_network_audit net;
2614 struct smk_audit_info ad;
2618 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2619 ad.a.u.net->family = PF_INET6;
2620 ad.a.u.net->dport = address->sin6_port;
2621 if (act == SMK_RECEIVING)
2622 ad.a.u.net->v6info.saddr = address->sin6_addr;
2624 ad.a.u.net->v6info.daddr = address->sin6_addr;
2626 rc = smk_access(subject, object, MAY_WRITE, &ad);
2627 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2631 #ifdef SMACK_IPV6_PORT_LABELING
2633 * smk_ipv6_port_label - Smack port access table management
2637 * Create or update the port list entry
2639 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2641 struct sock *sk = sock->sk;
2642 struct sockaddr_in6 *addr6;
2643 struct socket_smack *ssp = sock->sk->sk_security;
2644 struct smk_port_label *spp;
2645 unsigned short port = 0;
2647 if (address == NULL) {
2649 * This operation is changing the Smack information
2650 * on the bound socket. Take the changes to the port
2654 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2655 if (sk != spp->smk_sock)
2657 spp->smk_in = ssp->smk_in;
2658 spp->smk_out = ssp->smk_out;
2663 * A NULL address is only used for updating existing
2664 * bound entries. If there isn't one, it's OK.
2670 addr6 = (struct sockaddr_in6 *)address;
2671 port = ntohs(addr6->sin6_port);
2673 * This is a special case that is safely ignored.
2679 * Look for an existing port list entry.
2680 * This is an indication that a port is getting reused.
2683 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2684 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2686 if (spp->smk_can_reuse != 1) {
2690 spp->smk_port = port;
2692 spp->smk_in = ssp->smk_in;
2693 spp->smk_out = ssp->smk_out;
2694 spp->smk_can_reuse = 0;
2700 * A new port entry is required.
2702 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2706 spp->smk_port = port;
2708 spp->smk_in = ssp->smk_in;
2709 spp->smk_out = ssp->smk_out;
2710 spp->smk_sock_type = sock->type;
2711 spp->smk_can_reuse = 0;
2713 mutex_lock(&smack_ipv6_lock);
2714 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2715 mutex_unlock(&smack_ipv6_lock);
2720 * smk_ipv6_port_check - check Smack port access
2723 * @act: the action being taken
2725 * Create or update the port list entry
2727 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2730 struct smk_port_label *spp;
2731 struct socket_smack *ssp = sk->sk_security;
2732 struct smack_known *skp = NULL;
2733 unsigned short port;
2734 struct smack_known *object;
2736 if (act == SMK_RECEIVING) {
2737 skp = smack_ipv6host_label(address);
2738 object = ssp->smk_in;
2741 object = smack_ipv6host_label(address);
2745 * The other end is a single label host.
2747 if (skp != NULL && object != NULL)
2748 return smk_ipv6_check(skp, object, address, act);
2750 skp = smack_net_ambient;
2752 object = smack_net_ambient;
2755 * It's remote, so port lookup does no good.
2757 if (!smk_ipv6_localhost(address))
2758 return smk_ipv6_check(skp, object, address, act);
2761 * It's local so the send check has to have passed.
2763 if (act == SMK_RECEIVING)
2766 port = ntohs(address->sin6_port);
2768 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2769 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2771 object = spp->smk_in;
2772 if (act == SMK_CONNECTING)
2773 ssp->smk_packet = spp->smk_out;
2778 return smk_ipv6_check(skp, object, address, act);
2783 * smack_inode_setsecurity - set smack xattrs
2784 * @inode: the object
2785 * @name: attribute name
2786 * @value: attribute value
2787 * @size: size of the attribute
2790 * Sets the named attribute in the appropriate blob
2792 * Returns 0 on success, or an error code
2794 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2795 const void *value, size_t size, int flags)
2797 struct smack_known *skp;
2798 struct inode_smack *nsp = smack_inode(inode);
2799 struct socket_smack *ssp;
2800 struct socket *sock;
2803 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2806 skp = smk_import_entry(value, size);
2808 return PTR_ERR(skp);
2810 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2811 nsp->smk_inode = skp;
2812 nsp->smk_flags |= SMK_INODE_INSTANT;
2816 * The rest of the Smack xattrs are only on sockets.
2818 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2821 sock = SOCKET_I(inode);
2822 if (sock == NULL || sock->sk == NULL)
2825 ssp = sock->sk->sk_security;
2827 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2829 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2831 if (sock->sk->sk_family == PF_INET) {
2832 rc = smack_netlbl_add(sock->sk);
2835 "Smack: \"%s\" netlbl error %d.\n",
2841 #ifdef SMACK_IPV6_PORT_LABELING
2842 if (sock->sk->sk_family == PF_INET6)
2843 smk_ipv6_port_label(sock, NULL);
2850 * smack_socket_post_create - finish socket setup
2852 * @family: protocol family
2857 * Sets the netlabel information on the socket
2859 * Returns 0 on success, and error code otherwise
2861 static int smack_socket_post_create(struct socket *sock, int family,
2862 int type, int protocol, int kern)
2864 struct socket_smack *ssp;
2866 if (sock->sk == NULL)
2870 * Sockets created by kernel threads receive web label.
2872 if (unlikely(current->flags & PF_KTHREAD)) {
2873 ssp = sock->sk->sk_security;
2874 ssp->smk_in = &smack_known_web;
2875 ssp->smk_out = &smack_known_web;
2878 if (family != PF_INET)
2881 * Set the outbound netlbl.
2883 return smack_netlbl_add(sock->sk);
2887 * smack_socket_socketpair - create socket pair
2888 * @socka: one socket
2889 * @sockb: another socket
2891 * Cross reference the peer labels for SO_PEERSEC
2895 static int smack_socket_socketpair(struct socket *socka,
2896 struct socket *sockb)
2898 struct socket_smack *asp = socka->sk->sk_security;
2899 struct socket_smack *bsp = sockb->sk->sk_security;
2901 asp->smk_packet = bsp->smk_out;
2902 bsp->smk_packet = asp->smk_out;
2907 #ifdef SMACK_IPV6_PORT_LABELING
2909 * smack_socket_bind - record port binding information.
2911 * @address: the port address
2912 * @addrlen: size of the address
2914 * Records the label bound to a port.
2916 * Returns 0 on success, and error code otherwise
2918 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2921 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2922 if (addrlen < SIN6_LEN_RFC2133 ||
2923 address->sa_family != AF_INET6)
2925 smk_ipv6_port_label(sock, address);
2929 #endif /* SMACK_IPV6_PORT_LABELING */
2932 * smack_socket_connect - connect access check
2934 * @sap: the other end
2935 * @addrlen: size of sap
2937 * Verifies that a connection may be possible
2939 * Returns 0 on success, and error code otherwise
2941 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2946 if (sock->sk == NULL)
2948 if (sock->sk->sk_family != PF_INET &&
2949 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2951 if (addrlen < offsetofend(struct sockaddr, sa_family))
2953 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2954 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2955 struct smack_known *rsp = NULL;
2957 if (addrlen < SIN6_LEN_RFC2133)
2959 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2960 rsp = smack_ipv6host_label(sip);
2962 struct socket_smack *ssp = sock->sk->sk_security;
2964 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2967 #ifdef SMACK_IPV6_PORT_LABELING
2968 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2973 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2975 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
2980 * smack_flags_to_may - convert S_ to MAY_ values
2981 * @flags: the S_ value
2983 * Returns the equivalent MAY_ value
2985 static int smack_flags_to_may(int flags)
2989 if (flags & S_IRUGO)
2991 if (flags & S_IWUGO)
2993 if (flags & S_IXUGO)
3000 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
3005 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
3007 struct smack_known **blob = smack_msg_msg(msg);
3009 *blob = smk_of_current();
3014 * smack_of_ipc - the smack pointer for the ipc
3017 * Returns a pointer to the smack value
3019 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
3021 struct smack_known **blob = smack_ipc(isp);
3027 * smack_ipc_alloc_security - Set the security blob for ipc
3032 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
3034 struct smack_known **blob = smack_ipc(isp);
3036 *blob = smk_of_current();
3041 * smk_curacc_shm : check if current has access on shm
3043 * @access : access requested
3045 * Returns 0 if current has the requested access, error code otherwise
3047 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
3049 struct smack_known *ssp = smack_of_ipc(isp);
3050 struct smk_audit_info ad;
3054 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3055 ad.a.u.ipc_id = isp->id;
3057 rc = smk_curacc(ssp, access, &ad);
3058 rc = smk_bu_current("shm", ssp, access, rc);
3063 * smack_shm_associate - Smack access check for shm
3065 * @shmflg: access requested
3067 * Returns 0 if current has the requested access, error code otherwise
3069 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3073 may = smack_flags_to_may(shmflg);
3074 return smk_curacc_shm(isp, may);
3078 * smack_shm_shmctl - Smack access check for shm
3080 * @cmd: what it wants to do
3082 * Returns 0 if current has the requested access, error code otherwise
3084 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3098 may = MAY_READWRITE;
3103 * System level information.
3109 return smk_curacc_shm(isp, may);
3113 * smack_shm_shmat - Smack access for shmat
3116 * @shmflg: access requested
3118 * Returns 0 if current has the requested access, error code otherwise
3120 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3125 may = smack_flags_to_may(shmflg);
3126 return smk_curacc_shm(isp, may);
3130 * smk_curacc_sem : check if current has access on sem
3132 * @access : access requested
3134 * Returns 0 if current has the requested access, error code otherwise
3136 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3138 struct smack_known *ssp = smack_of_ipc(isp);
3139 struct smk_audit_info ad;
3143 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3144 ad.a.u.ipc_id = isp->id;
3146 rc = smk_curacc(ssp, access, &ad);
3147 rc = smk_bu_current("sem", ssp, access, rc);
3152 * smack_sem_associate - Smack access check for sem
3154 * @semflg: access requested
3156 * Returns 0 if current has the requested access, error code otherwise
3158 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3162 may = smack_flags_to_may(semflg);
3163 return smk_curacc_sem(isp, may);
3167 * smack_sem_semctl - Smack access check for sem
3169 * @cmd: what it wants to do
3171 * Returns 0 if current has the requested access, error code otherwise
3173 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3192 may = MAY_READWRITE;
3197 * System level information
3204 return smk_curacc_sem(isp, may);
3208 * smack_sem_semop - Smack checks of semaphore operations
3214 * Treated as read and write in all cases.
3216 * Returns 0 if access is allowed, error code otherwise
3218 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3219 unsigned nsops, int alter)
3221 return smk_curacc_sem(isp, MAY_READWRITE);
3225 * smk_curacc_msq : helper to check if current has access on msq
3227 * @access : access requested
3229 * return 0 if current has access, error otherwise
3231 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3233 struct smack_known *msp = smack_of_ipc(isp);
3234 struct smk_audit_info ad;
3238 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3239 ad.a.u.ipc_id = isp->id;
3241 rc = smk_curacc(msp, access, &ad);
3242 rc = smk_bu_current("msq", msp, access, rc);
3247 * smack_msg_queue_associate - Smack access check for msg_queue
3249 * @msqflg: access requested
3251 * Returns 0 if current has the requested access, error code otherwise
3253 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3257 may = smack_flags_to_may(msqflg);
3258 return smk_curacc_msq(isp, may);
3262 * smack_msg_queue_msgctl - Smack access check for msg_queue
3264 * @cmd: what it wants to do
3266 * Returns 0 if current has the requested access, error code otherwise
3268 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3280 may = MAY_READWRITE;
3285 * System level information
3292 return smk_curacc_msq(isp, may);
3296 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3299 * @msqflg: access requested
3301 * Returns 0 if current has the requested access, error code otherwise
3303 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3308 may = smack_flags_to_may(msqflg);
3309 return smk_curacc_msq(isp, may);
3313 * smack_msg_queue_msgrcv - Smack access check for msg_queue
3320 * Returns 0 if current has read and write access, error code otherwise
3322 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp,
3323 struct msg_msg *msg,
3324 struct task_struct *target, long type,
3327 return smk_curacc_msq(isp, MAY_READWRITE);
3331 * smack_ipc_permission - Smack access for ipc_permission()
3332 * @ipp: the object permissions
3333 * @flag: access requested
3335 * Returns 0 if current has read and write access, error code otherwise
3337 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3339 struct smack_known **blob = smack_ipc(ipp);
3340 struct smack_known *iskp = *blob;
3341 int may = smack_flags_to_may(flag);
3342 struct smk_audit_info ad;
3346 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3347 ad.a.u.ipc_id = ipp->id;
3349 rc = smk_curacc(iskp, may, &ad);
3350 rc = smk_bu_current("svipc", iskp, may, rc);
3355 * smack_ipc_getsecid - Extract smack security id
3356 * @ipp: the object permissions
3357 * @secid: where result will be saved
3359 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3361 struct smack_known **blob = smack_ipc(ipp);
3362 struct smack_known *iskp = *blob;
3364 *secid = iskp->smk_secid;
3368 * smack_d_instantiate - Make sure the blob is correct on an inode
3369 * @opt_dentry: dentry where inode will be attached
3370 * @inode: the object
3372 * Set the inode's security blob if it hasn't been done already.
3374 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3376 struct super_block *sbp;
3377 struct superblock_smack *sbsp;
3378 struct inode_smack *isp;
3379 struct smack_known *skp;
3380 struct smack_known *ckp = smk_of_current();
3381 struct smack_known *final;
3382 char trattr[TRANS_TRUE_SIZE];
3390 isp = smack_inode(inode);
3393 * If the inode is already instantiated
3394 * take the quick way out
3396 if (isp->smk_flags & SMK_INODE_INSTANT)
3400 sbsp = smack_superblock(sbp);
3402 * We're going to use the superblock default label
3403 * if there's no label on the file.
3405 final = sbsp->smk_default;
3408 * If this is the root inode the superblock
3409 * may be in the process of initialization.
3410 * If that is the case use the root value out
3411 * of the superblock.
3413 if (opt_dentry->d_parent == opt_dentry) {
3414 switch (sbp->s_magic) {
3415 case CGROUP_SUPER_MAGIC:
3416 case CGROUP2_SUPER_MAGIC:
3418 * The cgroup filesystem is never mounted,
3419 * so there's no opportunity to set the mount
3422 sbsp->smk_root = &smack_known_star;
3423 sbsp->smk_default = &smack_known_star;
3424 isp->smk_inode = sbsp->smk_root;
3428 * What about shmem/tmpfs anonymous files with dentry
3429 * obtained from d_alloc_pseudo()?
3431 isp->smk_inode = smk_of_current();
3434 isp->smk_inode = smk_of_current();
3438 * Socket access is controlled by the socket
3439 * structures associated with the task involved.
3441 isp->smk_inode = &smack_known_star;
3444 isp->smk_inode = sbsp->smk_root;
3447 isp->smk_flags |= SMK_INODE_INSTANT;
3452 * This is pretty hackish.
3453 * Casey says that we shouldn't have to do
3454 * file system specific code, but it does help
3455 * with keeping it simple.
3457 switch (sbp->s_magic) {
3459 case CGROUP_SUPER_MAGIC:
3460 case CGROUP2_SUPER_MAGIC:
3462 * Casey says that it's a little embarrassing
3463 * that the smack file system doesn't do
3464 * extended attributes.
3466 * Cgroupfs is special
3468 final = &smack_known_star;
3470 case DEVPTS_SUPER_MAGIC:
3472 * devpts seems content with the label of the task.
3473 * Programs that change smack have to treat the
3478 case PROC_SUPER_MAGIC:
3480 * Casey says procfs appears not to care.
3481 * The superblock default suffices.
3486 * Device labels should come from the filesystem,
3487 * but watch out, because they're volitile,
3488 * getting recreated on every reboot.
3490 final = &smack_known_star;
3492 * If a smack value has been set we want to use it,
3493 * but since tmpfs isn't giving us the opportunity
3494 * to set mount options simulate setting the
3495 * superblock default.
3500 * This isn't an understood special case.
3501 * Get the value from the xattr.
3505 * UNIX domain sockets use lower level socket data.
3507 if (S_ISSOCK(inode->i_mode)) {
3508 final = &smack_known_star;
3512 * No xattr support means, alas, no SMACK label.
3513 * Use the aforeapplied default.
3514 * It would be curious if the label of the task
3515 * does not match that assigned.
3517 if (!(inode->i_opflags & IOP_XATTR))
3520 * Get the dentry for xattr.
3522 dp = dget(opt_dentry);
3523 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3524 if (!IS_ERR_OR_NULL(skp))
3528 * Transmuting directory
3530 if (S_ISDIR(inode->i_mode)) {
3532 * If this is a new directory and the label was
3533 * transmuted when the inode was initialized
3534 * set the transmute attribute on the directory
3535 * and mark the inode.
3537 * If there is a transmute attribute on the
3538 * directory mark the inode.
3540 rc = __vfs_getxattr(dp, inode,
3541 XATTR_NAME_SMACKTRANSMUTE, trattr,
3543 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3544 TRANS_TRUE_SIZE) != 0)
3547 transflag = SMK_INODE_TRANSMUTE;
3550 * Don't let the exec or mmap label be "*" or "@".
3552 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3553 if (IS_ERR(skp) || skp == &smack_known_star ||
3554 skp == &smack_known_web)
3556 isp->smk_task = skp;
3558 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3559 if (IS_ERR(skp) || skp == &smack_known_star ||
3560 skp == &smack_known_web)
3562 isp->smk_mmap = skp;
3569 isp->smk_inode = ckp;
3571 isp->smk_inode = final;
3573 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3579 * smack_getprocattr - Smack process attribute access
3580 * @p: the object task
3581 * @name: the name of the attribute in /proc/.../attr
3582 * @value: where to put the result
3584 * Places a copy of the task Smack into value
3586 * Returns the length of the smack label or an error code
3588 static int smack_getprocattr(struct task_struct *p, const char *name, char **value)
3590 struct smack_known *skp = smk_of_task_struct_obj(p);
3594 if (strcmp(name, "current") != 0)
3597 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3607 * smack_setprocattr - Smack process attribute setting
3608 * @name: the name of the attribute in /proc/.../attr
3609 * @value: the value to set
3610 * @size: the size of the value
3612 * Sets the Smack value of the task. Only setting self
3613 * is permitted and only with privilege
3615 * Returns the length of the smack label or an error code
3617 static int smack_setprocattr(const char *name, void *value, size_t size)
3619 struct task_smack *tsp = smack_cred(current_cred());
3621 struct smack_known *skp;
3622 struct smack_known_list_elem *sklep;
3625 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3628 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3631 if (strcmp(name, "current") != 0)
3634 skp = smk_import_entry(value, size);
3636 return PTR_ERR(skp);
3639 * No process is ever allowed the web ("@") label
3640 * and the star ("*") label.
3642 if (skp == &smack_known_web || skp == &smack_known_star)
3645 if (!smack_privileged(CAP_MAC_ADMIN)) {
3647 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3648 if (sklep->smk_label == skp) {
3656 new = prepare_creds();
3660 tsp = smack_cred(new);
3661 tsp->smk_task = skp;
3663 * process can change its label only once
3665 smk_destroy_label_list(&tsp->smk_relabel);
3672 * smack_unix_stream_connect - Smack access on UDS
3674 * @other: the other sock
3677 * Return 0 if a subject with the smack of sock could access
3678 * an object with the smack of other, otherwise an error code
3680 static int smack_unix_stream_connect(struct sock *sock,
3681 struct sock *other, struct sock *newsk)
3683 struct smack_known *skp;
3684 struct smack_known *okp;
3685 struct socket_smack *ssp = sock->sk_security;
3686 struct socket_smack *osp = other->sk_security;
3687 struct socket_smack *nsp = newsk->sk_security;
3688 struct smk_audit_info ad;
3691 struct lsm_network_audit net;
3694 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3698 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3699 smk_ad_setfield_u_net_sk(&ad, other);
3701 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3702 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3706 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3707 rc = smk_bu_note("UDS connect", okp, skp,
3713 * Cross reference the peer labels for SO_PEERSEC.
3716 nsp->smk_packet = ssp->smk_out;
3717 ssp->smk_packet = osp->smk_out;
3724 * smack_unix_may_send - Smack access on UDS
3726 * @other: the other socket
3728 * Return 0 if a subject with the smack of sock could access
3729 * an object with the smack of other, otherwise an error code
3731 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3733 struct socket_smack *ssp = sock->sk->sk_security;
3734 struct socket_smack *osp = other->sk->sk_security;
3735 struct smk_audit_info ad;
3739 struct lsm_network_audit net;
3741 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3742 smk_ad_setfield_u_net_sk(&ad, other->sk);
3745 if (smack_privileged(CAP_MAC_OVERRIDE))
3748 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3749 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3754 * smack_socket_sendmsg - Smack check based on destination host
3757 * @size: the size of the message
3759 * Return 0 if the current subject can write to the destination host.
3760 * For IPv4 this is only a question if the destination is a single label host.
3761 * For IPv6 this is a check against the label of the port.
3763 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3766 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3767 #if IS_ENABLED(CONFIG_IPV6)
3768 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3770 #ifdef SMACK_IPV6_SECMARK_LABELING
3771 struct socket_smack *ssp = sock->sk->sk_security;
3772 struct smack_known *rsp;
3777 * Perfectly reasonable for this to be NULL
3782 switch (sock->sk->sk_family) {
3784 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3785 sip->sin_family != AF_INET)
3787 rc = smk_ipv4_check(sock->sk, sip);
3789 #if IS_ENABLED(CONFIG_IPV6)
3791 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3792 sap->sin6_family != AF_INET6)
3794 #ifdef SMACK_IPV6_SECMARK_LABELING
3795 rsp = smack_ipv6host_label(sap);
3797 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3800 #ifdef SMACK_IPV6_PORT_LABELING
3801 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3803 #endif /* IS_ENABLED(CONFIG_IPV6) */
3810 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3811 * @sap: netlabel secattr
3812 * @ssp: socket security information
3814 * Returns a pointer to a Smack label entry found on the label list.
3816 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3817 struct socket_smack *ssp)
3819 struct smack_known *skp;
3825 * Netlabel found it in the cache.
3827 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3828 return (struct smack_known *)sap->cache->data;
3830 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3832 * Looks like a fallback, which gives us a secid.
3834 return smack_from_secid(sap->attr.secid);
3836 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3838 * Looks like a CIPSO packet.
3839 * If there are flags but no level netlabel isn't
3840 * behaving the way we expect it to.
3842 * Look it up in the label table
3843 * Without guidance regarding the smack value
3844 * for the packet fall back on the network
3848 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3849 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3852 * Compare the catsets. Use the netlbl APIs.
3854 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3855 if ((skp->smk_netlabel.flags &
3856 NETLBL_SECATTR_MLS_CAT) == 0)
3860 for (acat = -1, kcat = -1; acat == kcat; ) {
3861 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3863 kcat = netlbl_catmap_walk(
3864 skp->smk_netlabel.attr.mls.cat,
3866 if (acat < 0 || kcat < 0)
3879 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3880 return &smack_known_web;
3881 return &smack_known_star;
3884 * Without guidance regarding the smack value
3885 * for the packet fall back on the network
3888 return smack_net_ambient;
3891 #if IS_ENABLED(CONFIG_IPV6)
3892 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3896 int proto = -EINVAL;
3897 struct ipv6hdr _ipv6h;
3898 struct ipv6hdr *ip6;
3900 struct tcphdr _tcph, *th;
3901 struct udphdr _udph, *uh;
3902 struct dccp_hdr _dccph, *dh;
3906 offset = skb_network_offset(skb);
3907 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3910 sip->sin6_addr = ip6->saddr;
3912 nexthdr = ip6->nexthdr;
3913 offset += sizeof(_ipv6h);
3914 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3921 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3923 sip->sin6_port = th->source;
3926 case IPPROTO_UDPLITE:
3927 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3929 sip->sin6_port = uh->source;
3932 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3934 sip->sin6_port = dh->dccph_sport;
3939 #endif /* CONFIG_IPV6 */
3942 * smack_from_skb - Smack data from the secmark in an skb
3945 * Returns smack_known of the secmark or NULL if that won't work.
3947 #ifdef CONFIG_NETWORK_SECMARK
3948 static struct smack_known *smack_from_skb(struct sk_buff *skb)
3950 if (skb == NULL || skb->secmark == 0)
3953 return smack_from_secid(skb->secmark);
3956 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3963 * smack_from_netlbl - Smack data from the IP options in an skb
3964 * @sk: socket data came in on
3965 * @family: address family
3968 * Find the Smack label in the IP options. If it hasn't been
3969 * added to the netlabel cache, add it here.
3971 * Returns smack_known of the IP options or NULL if that won't work.
3973 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
3974 struct sk_buff *skb)
3976 struct netlbl_lsm_secattr secattr;
3977 struct socket_smack *ssp = NULL;
3978 struct smack_known *skp = NULL;
3980 netlbl_secattr_init(&secattr);
3983 ssp = sk->sk_security;
3985 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
3986 skp = smack_from_secattr(&secattr, ssp);
3987 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
3988 netlbl_cache_add(skb, family, &skp->smk_netlabel);
3991 netlbl_secattr_destroy(&secattr);
3997 * smack_socket_sock_rcv_skb - Smack packet delivery access check
4001 * Returns 0 if the packet should be delivered, an error code otherwise
4003 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4005 struct socket_smack *ssp = sk->sk_security;
4006 struct smack_known *skp = NULL;
4008 struct smk_audit_info ad;
4009 u16 family = sk->sk_family;
4011 struct lsm_network_audit net;
4013 #if IS_ENABLED(CONFIG_IPV6)
4014 struct sockaddr_in6 sadd;
4017 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4019 #endif /* CONFIG_IPV6 */
4024 * If there is a secmark use it rather than the CIPSO label.
4025 * If there is no secmark fall back to CIPSO.
4026 * The secmark is assumed to reflect policy better.
4028 skp = smack_from_skb(skb);
4030 skp = smack_from_netlbl(sk, family, skb);
4032 skp = smack_net_ambient;
4036 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4037 ad.a.u.net->family = family;
4038 ad.a.u.net->netif = skb->skb_iif;
4039 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4042 * Receiving a packet requires that the other end
4043 * be able to write here. Read access is not required.
4044 * This is the simplist possible security model
4047 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4048 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
4051 netlbl_skbuff_err(skb, family, rc, 0);
4053 #if IS_ENABLED(CONFIG_IPV6)
4055 proto = smk_skb_to_addr_ipv6(skb, &sadd);
4056 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
4057 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
4059 #ifdef SMACK_IPV6_SECMARK_LABELING
4060 skp = smack_from_skb(skb);
4062 if (smk_ipv6_localhost(&sadd))
4064 skp = smack_ipv6host_label(&sadd);
4066 skp = smack_net_ambient;
4069 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4070 ad.a.u.net->family = family;
4071 ad.a.u.net->netif = skb->skb_iif;
4072 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4073 #endif /* CONFIG_AUDIT */
4074 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4075 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4077 #endif /* SMACK_IPV6_SECMARK_LABELING */
4078 #ifdef SMACK_IPV6_PORT_LABELING
4079 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
4080 #endif /* SMACK_IPV6_PORT_LABELING */
4082 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4083 ICMPV6_ADM_PROHIBITED, 0);
4085 #endif /* CONFIG_IPV6 */
4092 * smack_socket_getpeersec_stream - pull in packet label
4094 * @optval: user's destination
4095 * @optlen: size thereof
4098 * returns zero on success, an error code otherwise
4100 static int smack_socket_getpeersec_stream(struct socket *sock,
4101 sockptr_t optval, sockptr_t optlen,
4104 struct socket_smack *ssp;
4109 ssp = sock->sk->sk_security;
4110 if (ssp->smk_packet != NULL) {
4111 rcp = ssp->smk_packet->smk_known;
4112 slen = strlen(rcp) + 1;
4119 if (copy_to_sockptr(optval, rcp, slen))
4122 if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
4129 * smack_socket_getpeersec_dgram - pull in packet label
4130 * @sock: the peer socket
4132 * @secid: pointer to where to put the secid of the packet
4134 * Sets the netlabel socket state on sk from parent
4136 static int smack_socket_getpeersec_dgram(struct socket *sock,
4137 struct sk_buff *skb, u32 *secid)
4140 struct socket_smack *ssp = NULL;
4141 struct smack_known *skp;
4142 struct sock *sk = NULL;
4143 int family = PF_UNSPEC;
4144 u32 s = 0; /* 0 is the invalid secid */
4147 if (skb->protocol == htons(ETH_P_IP))
4149 #if IS_ENABLED(CONFIG_IPV6)
4150 else if (skb->protocol == htons(ETH_P_IPV6))
4152 #endif /* CONFIG_IPV6 */
4154 if (family == PF_UNSPEC && sock != NULL)
4155 family = sock->sk->sk_family;
4159 ssp = sock->sk->sk_security;
4160 s = ssp->smk_out->smk_secid;
4163 skp = smack_from_skb(skb);
4169 * Translate what netlabel gave us.
4173 skp = smack_from_netlbl(sk, family, skb);
4178 #ifdef SMACK_IPV6_SECMARK_LABELING
4179 skp = smack_from_skb(skb);
4192 * smack_sock_graft - Initialize a newly created socket with an existing sock
4194 * @parent: parent socket
4196 * Set the smk_{in,out} state of an existing sock based on the process that
4197 * is creating the new socket.
4199 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4201 struct socket_smack *ssp;
4202 struct smack_known *skp = smk_of_current();
4205 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4208 ssp = sk->sk_security;
4211 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4215 * smack_inet_conn_request - Smack access check on connect
4216 * @sk: socket involved
4220 * Returns 0 if a task with the packet label could write to
4221 * the socket, otherwise an error code
4223 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4224 struct request_sock *req)
4226 u16 family = sk->sk_family;
4227 struct smack_known *skp;
4228 struct socket_smack *ssp = sk->sk_security;
4229 struct sockaddr_in addr;
4231 struct smack_known *hskp;
4233 struct smk_audit_info ad;
4235 struct lsm_network_audit net;
4238 #if IS_ENABLED(CONFIG_IPV6)
4239 if (family == PF_INET6) {
4241 * Handle mapped IPv4 packets arriving
4242 * via IPv6 sockets. Don't set up netlabel
4243 * processing on IPv6.
4245 if (skb->protocol == htons(ETH_P_IP))
4250 #endif /* CONFIG_IPV6 */
4253 * If there is a secmark use it rather than the CIPSO label.
4254 * If there is no secmark fall back to CIPSO.
4255 * The secmark is assumed to reflect policy better.
4257 skp = smack_from_skb(skb);
4259 skp = smack_from_netlbl(sk, family, skb);
4261 skp = &smack_known_huh;
4265 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4266 ad.a.u.net->family = family;
4267 ad.a.u.net->netif = skb->skb_iif;
4268 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4271 * Receiving a packet requires that the other end be able to write
4272 * here. Read access is not required.
4274 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4275 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4280 * Save the peer's label in the request_sock so we can later setup
4281 * smk_packet in the child socket so that SO_PEERCRED can report it.
4283 req->peer_secid = skp->smk_secid;
4286 * We need to decide if we want to label the incoming connection here
4287 * if we do we only need to label the request_sock and the stack will
4288 * propagate the wire-label to the sock when it is created.
4291 addr.sin_addr.s_addr = hdr->saddr;
4293 hskp = smack_ipv4host_label(&addr);
4297 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4299 netlbl_req_delattr(req);
4305 * smack_inet_csk_clone - Copy the connection information to the new socket
4306 * @sk: the new socket
4307 * @req: the connection's request_sock
4309 * Transfer the connection's peer label to the newly created socket.
4311 static void smack_inet_csk_clone(struct sock *sk,
4312 const struct request_sock *req)
4314 struct socket_smack *ssp = sk->sk_security;
4315 struct smack_known *skp;
4317 if (req->peer_secid != 0) {
4318 skp = smack_from_secid(req->peer_secid);
4319 ssp->smk_packet = skp;
4321 ssp->smk_packet = NULL;
4325 * Key management security hooks
4327 * Casey has not tested key support very heavily.
4328 * The permission check is most likely too restrictive.
4329 * If you care about keys please have a look.
4334 * smack_key_alloc - Set the key security blob
4336 * @cred: the credentials to use
4339 * No allocation required
4343 static int smack_key_alloc(struct key *key, const struct cred *cred,
4344 unsigned long flags)
4346 struct smack_known *skp = smk_of_task(smack_cred(cred));
4348 key->security = skp;
4353 * smack_key_free - Clear the key security blob
4356 * Clear the blob pointer
4358 static void smack_key_free(struct key *key)
4360 key->security = NULL;
4364 * smack_key_permission - Smack access on a key
4365 * @key_ref: gets to the object
4366 * @cred: the credentials to use
4367 * @need_perm: requested key permission
4369 * Return 0 if the task has read and write to the object,
4370 * an error code otherwise
4372 static int smack_key_permission(key_ref_t key_ref,
4373 const struct cred *cred,
4374 enum key_need_perm need_perm)
4377 struct smk_audit_info ad;
4378 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4383 * Validate requested permissions
4385 switch (need_perm) {
4387 case KEY_NEED_SEARCH:
4389 request |= MAY_READ;
4391 case KEY_NEED_WRITE:
4393 case KEY_NEED_SETATTR:
4394 request |= MAY_WRITE;
4396 case KEY_NEED_UNSPECIFIED:
4397 case KEY_NEED_UNLINK:
4398 case KEY_SYSADMIN_OVERRIDE:
4399 case KEY_AUTHTOKEN_OVERRIDE:
4400 case KEY_DEFER_PERM_CHECK:
4406 keyp = key_ref_to_ptr(key_ref);
4410 * If the key hasn't been initialized give it access so that
4413 if (keyp->security == NULL)
4416 * This should not occur
4421 if (smack_privileged(CAP_MAC_OVERRIDE))
4425 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4426 ad.a.u.key_struct.key = keyp->serial;
4427 ad.a.u.key_struct.key_desc = keyp->description;
4429 rc = smk_access(tkp, keyp->security, request, &ad);
4430 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4435 * smack_key_getsecurity - Smack label tagging the key
4436 * @key points to the key to be queried
4437 * @_buffer points to a pointer that should be set to point to the
4438 * resulting string (if no label or an error occurs).
4439 * Return the length of the string (including terminating NUL) or -ve if
4441 * May also return 0 (and a NULL buffer pointer) if there is no label.
4443 static int smack_key_getsecurity(struct key *key, char **_buffer)
4445 struct smack_known *skp = key->security;
4449 if (key->security == NULL) {
4454 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4457 length = strlen(copy) + 1;
4464 #ifdef CONFIG_KEY_NOTIFICATIONS
4466 * smack_watch_key - Smack access to watch a key for notifications.
4467 * @key: The key to be watched
4469 * Return 0 if the @watch->cred has permission to read from the key object and
4470 * an error otherwise.
4472 static int smack_watch_key(struct key *key)
4474 struct smk_audit_info ad;
4475 struct smack_known *tkp = smk_of_current();
4481 * If the key hasn't been initialized give it access so that
4484 if (key->security == NULL)
4487 * This should not occur
4492 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4496 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4497 ad.a.u.key_struct.key = key->serial;
4498 ad.a.u.key_struct.key_desc = key->description;
4500 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4501 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4504 #endif /* CONFIG_KEY_NOTIFICATIONS */
4505 #endif /* CONFIG_KEYS */
4507 #ifdef CONFIG_WATCH_QUEUE
4509 * smack_post_notification - Smack access to post a notification to a queue
4510 * @w_cred: The credentials of the watcher.
4511 * @cred: The credentials of the event source (may be NULL).
4512 * @n: The notification message to be posted.
4514 static int smack_post_notification(const struct cred *w_cred,
4515 const struct cred *cred,
4516 struct watch_notification *n)
4518 struct smk_audit_info ad;
4519 struct smack_known *subj, *obj;
4522 /* Always let maintenance notifications through. */
4523 if (n->type == WATCH_TYPE_META)
4528 subj = smk_of_task(smack_cred(cred));
4529 obj = smk_of_task(smack_cred(w_cred));
4531 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4532 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4533 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4536 #endif /* CONFIG_WATCH_QUEUE */
4541 * Audit requires a unique representation of each Smack specific
4542 * rule. This unique representation is used to distinguish the
4543 * object to be audited from remaining kernel objects and also
4544 * works as a glue between the audit hooks.
4546 * Since repository entries are added but never deleted, we'll use
4547 * the smack_known label address related to the given audit rule as
4548 * the needed unique representation. This also better fits the smack
4549 * model where nearly everything is a label.
4554 * smack_audit_rule_init - Initialize a smack audit rule
4555 * @field: audit rule fields given from user-space (audit.h)
4556 * @op: required testing operator (=, !=, >, <, ...)
4557 * @rulestr: smack label to be audited
4558 * @vrule: pointer to save our own audit rule representation
4560 * Prepare to audit cases where (@field @op @rulestr) is true.
4561 * The label to be audited is created if necessay.
4563 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4565 struct smack_known *skp;
4566 char **rule = (char **)vrule;
4569 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4572 if (op != Audit_equal && op != Audit_not_equal)
4575 skp = smk_import_entry(rulestr, 0);
4577 return PTR_ERR(skp);
4579 *rule = skp->smk_known;
4585 * smack_audit_rule_known - Distinguish Smack audit rules
4586 * @krule: rule of interest, in Audit kernel representation format
4588 * This is used to filter Smack rules from remaining Audit ones.
4589 * If it's proved that this rule belongs to us, the
4590 * audit_rule_match hook will be called to do the final judgement.
4592 static int smack_audit_rule_known(struct audit_krule *krule)
4594 struct audit_field *f;
4597 for (i = 0; i < krule->field_count; i++) {
4598 f = &krule->fields[i];
4600 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4608 * smack_audit_rule_match - Audit given object ?
4609 * @secid: security id for identifying the object to test
4610 * @field: audit rule flags given from user-space
4611 * @op: required testing operator
4612 * @vrule: smack internal rule presentation
4614 * The core Audit hook. It's used to take the decision of
4615 * whether to audit or not to audit a given object.
4617 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4619 struct smack_known *skp;
4622 if (unlikely(!rule)) {
4623 WARN_ONCE(1, "Smack: missing rule\n");
4627 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4630 skp = smack_from_secid(secid);
4633 * No need to do string comparisons. If a match occurs,
4634 * both pointers will point to the same smack_known
4637 if (op == Audit_equal)
4638 return (rule == skp->smk_known);
4639 if (op == Audit_not_equal)
4640 return (rule != skp->smk_known);
4646 * There is no need for a smack_audit_rule_free hook.
4647 * No memory was allocated.
4650 #endif /* CONFIG_AUDIT */
4653 * smack_ismaclabel - check if xattr @name references a smack MAC label
4654 * @name: Full xattr name to check.
4656 static int smack_ismaclabel(const char *name)
4658 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4663 * smack_secid_to_secctx - return the smack label for a secid
4664 * @secid: incoming integer
4665 * @secdata: destination
4666 * @seclen: how long it is
4668 * Exists for networking code.
4670 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4672 struct smack_known *skp = smack_from_secid(secid);
4675 *secdata = skp->smk_known;
4676 *seclen = strlen(skp->smk_known);
4681 * smack_secctx_to_secid - return the secid for a smack label
4682 * @secdata: smack label
4683 * @seclen: how long result is
4684 * @secid: outgoing integer
4686 * Exists for audit and networking code.
4688 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4690 struct smack_known *skp = smk_find_entry(secdata);
4693 *secid = skp->smk_secid;
4700 * There used to be a smack_release_secctx hook
4701 * that did nothing back when hooks were in a vector.
4702 * Now that there's a list such a hook adds cost.
4705 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4707 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4711 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4713 return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
4717 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4719 struct smack_known *skp = smk_of_inode(inode);
4721 *ctx = skp->smk_known;
4722 *ctxlen = strlen(skp->smk_known);
4726 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4729 struct task_smack *tsp;
4730 struct smack_known *skp;
4731 struct inode_smack *isp;
4732 struct cred *new_creds = *new;
4734 if (new_creds == NULL) {
4735 new_creds = prepare_creds();
4736 if (new_creds == NULL)
4740 tsp = smack_cred(new_creds);
4743 * Get label from overlay inode and set it in create_sid
4745 isp = smack_inode(d_inode(dentry));
4746 skp = isp->smk_inode;
4747 tsp->smk_task = skp;
4752 static int smack_inode_copy_up_xattr(const char *name)
4755 * Return 1 if this is the smack access Smack attribute.
4757 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4763 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4765 const struct cred *old,
4768 struct task_smack *otsp = smack_cred(old);
4769 struct task_smack *ntsp = smack_cred(new);
4770 struct inode_smack *isp;
4774 * Use the process credential unless all of
4775 * the transmuting criteria are met
4777 ntsp->smk_task = otsp->smk_task;
4780 * the attribute of the containing directory
4782 isp = smack_inode(d_inode(dentry->d_parent));
4784 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4786 may = smk_access_entry(otsp->smk_task->smk_known,
4787 isp->smk_inode->smk_known,
4788 &otsp->smk_task->smk_rules);
4792 * If the directory is transmuting and the rule
4793 * providing access is transmuting use the containing
4794 * directory label instead of the process label.
4796 if (may > 0 && (may & MAY_TRANSMUTE)) {
4797 ntsp->smk_task = isp->smk_inode;
4798 ntsp->smk_transmuted = ntsp->smk_task;
4804 #ifdef CONFIG_IO_URING
4806 * smack_uring_override_creds - Is io_uring cred override allowed?
4807 * @new: the target creds
4809 * Check to see if the current task is allowed to override it's credentials
4810 * to service an io_uring operation.
4812 static int smack_uring_override_creds(const struct cred *new)
4814 struct task_smack *tsp = smack_cred(current_cred());
4815 struct task_smack *nsp = smack_cred(new);
4818 * Allow the degenerate case where the new Smack value is
4819 * the same as the current Smack value.
4821 if (tsp->smk_task == nsp->smk_task)
4824 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4831 * smack_uring_sqpoll - check if a io_uring polling thread can be created
4833 * Check to see if the current task is allowed to create a new io_uring
4834 * kernel polling thread.
4836 static int smack_uring_sqpoll(void)
4838 if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred()))
4845 * smack_uring_cmd - check on file operations for io_uring
4846 * @ioucmd: the command in question
4848 * Make a best guess about whether a io_uring "command" should
4849 * be allowed. Use the same logic used for determining if the
4850 * file could be opened for read in the absence of better criteria.
4852 static int smack_uring_cmd(struct io_uring_cmd *ioucmd)
4854 struct file *file = ioucmd->file;
4855 struct smk_audit_info ad;
4856 struct task_smack *tsp;
4857 struct inode *inode;
4863 tsp = smack_cred(file->f_cred);
4864 inode = file_inode(file);
4866 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
4867 smk_ad_setfield_u_fs_path(&ad, file->f_path);
4868 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
4869 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
4874 #endif /* CONFIG_IO_URING */
4876 struct lsm_blob_sizes smack_blob_sizes __ro_after_init = {
4877 .lbs_cred = sizeof(struct task_smack),
4878 .lbs_file = sizeof(struct smack_known *),
4879 .lbs_inode = sizeof(struct inode_smack),
4880 .lbs_ipc = sizeof(struct smack_known *),
4881 .lbs_msg_msg = sizeof(struct smack_known *),
4882 .lbs_superblock = sizeof(struct superblock_smack),
4883 .lbs_xattr_count = SMACK_INODE_INIT_XATTRS,
4886 static struct security_hook_list smack_hooks[] __ro_after_init = {
4887 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4888 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4889 LSM_HOOK_INIT(syslog, smack_syslog),
4891 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4892 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4894 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4895 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4896 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4897 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4898 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4900 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
4902 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4903 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4904 LSM_HOOK_INIT(inode_link, smack_inode_link),
4905 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4906 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4907 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4908 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4909 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4910 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4911 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4912 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4913 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4914 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4915 LSM_HOOK_INIT(inode_set_acl, smack_inode_set_acl),
4916 LSM_HOOK_INIT(inode_get_acl, smack_inode_get_acl),
4917 LSM_HOOK_INIT(inode_remove_acl, smack_inode_remove_acl),
4918 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4919 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4920 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4921 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4923 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4924 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4925 LSM_HOOK_INIT(file_lock, smack_file_lock),
4926 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4927 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4928 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4929 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4930 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4931 LSM_HOOK_INIT(file_receive, smack_file_receive),
4933 LSM_HOOK_INIT(file_open, smack_file_open),
4935 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4936 LSM_HOOK_INIT(cred_free, smack_cred_free),
4937 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4938 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4939 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4940 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4941 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4942 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4943 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4944 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4945 LSM_HOOK_INIT(current_getsecid_subj, smack_current_getsecid_subj),
4946 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
4947 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4948 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4949 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4950 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4951 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4952 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4953 LSM_HOOK_INIT(task_kill, smack_task_kill),
4954 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4956 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4957 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4959 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4961 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4962 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4963 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4964 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4965 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4967 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4968 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4969 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4970 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4972 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4973 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4974 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4975 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4977 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4979 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4980 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4982 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4983 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4985 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4986 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4987 #ifdef SMACK_IPV6_PORT_LABELING
4988 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4990 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4991 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4992 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4993 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4994 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4995 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4996 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4997 LSM_HOOK_INIT(sk_clone_security, smack_sk_clone_security),
4998 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4999 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
5000 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
5002 /* key management security hooks */
5004 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
5005 LSM_HOOK_INIT(key_free, smack_key_free),
5006 LSM_HOOK_INIT(key_permission, smack_key_permission),
5007 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
5008 #ifdef CONFIG_KEY_NOTIFICATIONS
5009 LSM_HOOK_INIT(watch_key, smack_watch_key),
5011 #endif /* CONFIG_KEYS */
5013 #ifdef CONFIG_WATCH_QUEUE
5014 LSM_HOOK_INIT(post_notification, smack_post_notification),
5019 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
5020 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
5021 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
5022 #endif /* CONFIG_AUDIT */
5024 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
5025 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
5026 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
5027 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
5028 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
5029 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
5030 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
5031 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
5032 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
5033 #ifdef CONFIG_IO_URING
5034 LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds),
5035 LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll),
5036 LSM_HOOK_INIT(uring_cmd, smack_uring_cmd),
5041 static __init void init_smack_known_list(void)
5044 * Initialize rule list locks
5046 mutex_init(&smack_known_huh.smk_rules_lock);
5047 mutex_init(&smack_known_hat.smk_rules_lock);
5048 mutex_init(&smack_known_floor.smk_rules_lock);
5049 mutex_init(&smack_known_star.smk_rules_lock);
5050 mutex_init(&smack_known_web.smk_rules_lock);
5052 * Initialize rule lists
5054 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
5055 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
5056 INIT_LIST_HEAD(&smack_known_star.smk_rules);
5057 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
5058 INIT_LIST_HEAD(&smack_known_web.smk_rules);
5060 * Create the known labels list
5062 smk_insert_entry(&smack_known_huh);
5063 smk_insert_entry(&smack_known_hat);
5064 smk_insert_entry(&smack_known_star);
5065 smk_insert_entry(&smack_known_floor);
5066 smk_insert_entry(&smack_known_web);
5070 * smack_init - initialize the smack system
5072 * Returns 0 on success, -ENOMEM is there's no memory
5074 static __init int smack_init(void)
5076 struct cred *cred = (struct cred *) current->cred;
5077 struct task_smack *tsp;
5079 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
5080 if (!smack_rule_cache)
5084 * Set the security state for the initial task.
5086 tsp = smack_cred(cred);
5087 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
5092 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
5095 pr_info("Smack: Initializing.\n");
5096 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
5097 pr_info("Smack: Netfilter enabled.\n");
5099 #ifdef SMACK_IPV6_PORT_LABELING
5100 pr_info("Smack: IPv6 port labeling enabled.\n");
5102 #ifdef SMACK_IPV6_SECMARK_LABELING
5103 pr_info("Smack: IPv6 Netfilter enabled.\n");
5106 /* initialize the smack_known_list */
5107 init_smack_known_list();
5113 * Smack requires early initialization in order to label
5114 * all processes and objects when they are created.
5116 DEFINE_LSM(smack) = {
5118 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
5119 .blobs = &smack_blob_sizes,