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/icmpv6.h>
28 #include <linux/slab.h>
29 #include <linux/mutex.h>
30 #include <net/cipso_ipv4.h>
33 #include <linux/audit.h>
34 #include <linux/magic.h>
35 #include <linux/dcache.h>
36 #include <linux/personality.h>
37 #include <linux/msg.h>
38 #include <linux/shm.h>
39 #include <uapi/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/cmd.h>
46 #include <uapi/linux/lsm.h>
49 #define TRANS_TRUE "TRUE"
50 #define TRANS_TRUE_SIZE 4
52 #define SMK_CONNECTING 0
53 #define SMK_RECEIVING 1
57 * Smack uses multiple xattrs.
58 * SMACK64 - for access control,
59 * SMACK64TRANSMUTE - label initialization,
60 * Not saved on files - SMACK64IPIN and SMACK64IPOUT,
61 * Must be set explicitly - SMACK64EXEC and SMACK64MMAP
63 #define SMACK_INODE_INIT_XATTRS 2
65 #ifdef SMACK_IPV6_PORT_LABELING
66 static DEFINE_MUTEX(smack_ipv6_lock);
67 static LIST_HEAD(smk_ipv6_port_list);
69 struct kmem_cache *smack_rule_cache;
70 int smack_enabled __initdata;
72 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
77 } smk_mount_opts[] = {
78 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
79 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
83 static int match_opt_prefix(char *s, int l, char **arg)
87 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
88 size_t len = smk_mount_opts[i].len;
89 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
91 if (len == l || s[len] != '=')
94 return smk_mount_opts[i].opt;
99 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
100 static char *smk_bu_mess[] = {
101 "Bringup Error", /* Unused */
102 "Bringup", /* SMACK_BRINGUP_ALLOW */
103 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
104 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
107 static void smk_bu_mode(int mode, char *s)
109 smack_str_from_perm(s, mode);
113 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
114 static int smk_bu_note(char *note, struct smack_known *sskp,
115 struct smack_known *oskp, int mode, int rc)
117 char acc[SMK_NUM_ACCESS_TYPE + 1];
121 if (rc > SMACK_UNCONFINED_OBJECT)
124 smk_bu_mode(mode, acc);
125 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
126 sskp->smk_known, oskp->smk_known, acc, note);
130 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
133 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
134 static int smk_bu_current(char *note, struct smack_known *oskp,
137 struct task_smack *tsp = smack_cred(current_cred());
138 char acc[SMK_NUM_ACCESS_TYPE + 1];
142 if (rc > SMACK_UNCONFINED_OBJECT)
145 smk_bu_mode(mode, acc);
146 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
147 tsp->smk_task->smk_known, oskp->smk_known,
148 acc, current->comm, note);
152 #define smk_bu_current(note, oskp, mode, RC) (RC)
155 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
156 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
158 struct task_smack *tsp = smack_cred(current_cred());
159 struct smack_known *smk_task = smk_of_task_struct_obj(otp);
160 char acc[SMK_NUM_ACCESS_TYPE + 1];
164 if (rc > SMACK_UNCONFINED_OBJECT)
167 smk_bu_mode(mode, acc);
168 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
169 tsp->smk_task->smk_known, smk_task->smk_known, acc,
170 current->comm, otp->comm);
174 #define smk_bu_task(otp, mode, RC) (RC)
177 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
178 static int smk_bu_inode(struct inode *inode, int mode, int rc)
180 struct task_smack *tsp = smack_cred(current_cred());
181 struct inode_smack *isp = smack_inode(inode);
182 char acc[SMK_NUM_ACCESS_TYPE + 1];
184 if (isp->smk_flags & SMK_INODE_IMPURE)
185 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
186 inode->i_sb->s_id, inode->i_ino, current->comm);
190 if (rc > SMACK_UNCONFINED_OBJECT)
192 if (rc == SMACK_UNCONFINED_SUBJECT &&
193 (mode & (MAY_WRITE | MAY_APPEND)))
194 isp->smk_flags |= SMK_INODE_IMPURE;
196 smk_bu_mode(mode, acc);
198 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
199 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
200 inode->i_sb->s_id, inode->i_ino, current->comm);
204 #define smk_bu_inode(inode, mode, RC) (RC)
207 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
208 static int smk_bu_file(struct file *file, int mode, int rc)
210 struct task_smack *tsp = smack_cred(current_cred());
211 struct smack_known *sskp = tsp->smk_task;
212 struct inode *inode = file_inode(file);
213 struct inode_smack *isp = smack_inode(inode);
214 char acc[SMK_NUM_ACCESS_TYPE + 1];
216 if (isp->smk_flags & SMK_INODE_IMPURE)
217 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
218 inode->i_sb->s_id, inode->i_ino, current->comm);
222 if (rc > SMACK_UNCONFINED_OBJECT)
225 smk_bu_mode(mode, acc);
226 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
227 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
228 inode->i_sb->s_id, inode->i_ino, file,
233 #define smk_bu_file(file, mode, RC) (RC)
236 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
237 static int smk_bu_credfile(const struct cred *cred, struct file *file,
240 struct task_smack *tsp = smack_cred(cred);
241 struct smack_known *sskp = tsp->smk_task;
242 struct inode *inode = file_inode(file);
243 struct inode_smack *isp = smack_inode(inode);
244 char acc[SMK_NUM_ACCESS_TYPE + 1];
246 if (isp->smk_flags & SMK_INODE_IMPURE)
247 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
248 inode->i_sb->s_id, inode->i_ino, current->comm);
252 if (rc > SMACK_UNCONFINED_OBJECT)
255 smk_bu_mode(mode, acc);
256 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
257 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
258 inode->i_sb->s_id, inode->i_ino, file,
263 #define smk_bu_credfile(cred, file, mode, RC) (RC)
267 * smk_fetch - Fetch the smack label from a file.
268 * @name: type of the label (attribute)
269 * @ip: a pointer to the inode
270 * @dp: a pointer to the dentry
272 * Returns a pointer to the master list entry for the Smack label,
273 * NULL if there was no label to fetch, or an error code.
275 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
280 struct smack_known *skp = NULL;
282 if (!(ip->i_opflags & IOP_XATTR))
283 return ERR_PTR(-EOPNOTSUPP);
285 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
287 return ERR_PTR(-ENOMEM);
289 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
295 skp = smk_import_entry(buffer, rc);
303 * init_inode_smack - initialize an inode security blob
304 * @inode: inode to extract the info from
305 * @skp: a pointer to the Smack label entry to use in the blob
308 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
310 struct inode_smack *isp = smack_inode(inode);
312 isp->smk_inode = skp;
317 * init_task_smack - initialize a task security blob
318 * @tsp: blob to initialize
319 * @task: a pointer to the Smack label for the running task
320 * @forked: a pointer to the Smack label for the forked task
323 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
324 struct smack_known *forked)
326 tsp->smk_task = task;
327 tsp->smk_forked = forked;
328 INIT_LIST_HEAD(&tsp->smk_rules);
329 INIT_LIST_HEAD(&tsp->smk_relabel);
330 mutex_init(&tsp->smk_rules_lock);
334 * smk_copy_rules - copy a rule set
335 * @nhead: new rules header pointer
336 * @ohead: old rules header pointer
337 * @gfp: type of the memory for the allocation
339 * Returns 0 on success, -ENOMEM on error
341 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
344 struct smack_rule *nrp;
345 struct smack_rule *orp;
348 list_for_each_entry_rcu(orp, ohead, list) {
349 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
355 list_add_rcu(&nrp->list, nhead);
361 * smk_copy_relabel - copy smk_relabel labels list
362 * @nhead: new rules header pointer
363 * @ohead: old rules header pointer
364 * @gfp: type of the memory for the allocation
366 * Returns 0 on success, -ENOMEM on error
368 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
371 struct smack_known_list_elem *nklep;
372 struct smack_known_list_elem *oklep;
374 list_for_each_entry(oklep, ohead, list) {
375 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
377 smk_destroy_label_list(nhead);
380 nklep->smk_label = oklep->smk_label;
381 list_add(&nklep->list, nhead);
388 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
389 * @mode: input mode in form of PTRACE_MODE_*
391 * Returns a converted MAY_* mode usable by smack rules
393 static inline unsigned int smk_ptrace_mode(unsigned int mode)
395 if (mode & PTRACE_MODE_ATTACH)
396 return MAY_READWRITE;
397 if (mode & PTRACE_MODE_READ)
404 * smk_ptrace_rule_check - helper for ptrace access
405 * @tracer: tracer process
406 * @tracee_known: label entry of the process that's about to be traced
407 * @mode: ptrace attachment mode (PTRACE_MODE_*)
408 * @func: name of the function that called us, used for audit
410 * Returns 0 on access granted, -error on error
412 static int smk_ptrace_rule_check(struct task_struct *tracer,
413 struct smack_known *tracee_known,
414 unsigned int mode, const char *func)
417 struct smk_audit_info ad, *saip = NULL;
418 struct task_smack *tsp;
419 struct smack_known *tracer_known;
420 const struct cred *tracercred;
422 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
423 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
424 smk_ad_setfield_u_tsk(&ad, tracer);
429 tracercred = __task_cred(tracer);
430 tsp = smack_cred(tracercred);
431 tracer_known = smk_of_task(tsp);
433 if ((mode & PTRACE_MODE_ATTACH) &&
434 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
435 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
436 if (tracer_known->smk_known == tracee_known->smk_known)
438 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
440 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
446 smack_log(tracer_known->smk_known,
447 tracee_known->smk_known,
454 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
455 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
463 * We he, that is fun!
467 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
468 * @ctp: child task pointer
469 * @mode: ptrace attachment mode (PTRACE_MODE_*)
471 * Returns 0 if access is OK, an error code otherwise
473 * Do the capability checks.
475 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
477 struct smack_known *skp;
479 skp = smk_of_task_struct_obj(ctp);
481 return smk_ptrace_rule_check(current, skp, mode, __func__);
485 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
486 * @ptp: parent task pointer
488 * Returns 0 if access is OK, an error code otherwise
490 * Do the capability checks, and require PTRACE_MODE_ATTACH.
492 static int smack_ptrace_traceme(struct task_struct *ptp)
494 struct smack_known *skp;
496 skp = smk_of_task(smack_cred(current_cred()));
498 return smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
502 * smack_syslog - Smack approval on syslog
503 * @typefrom_file: unused
505 * Returns 0 on success, error code otherwise.
507 static int smack_syslog(int typefrom_file)
510 struct smack_known *skp = smk_of_current();
512 if (smack_privileged(CAP_MAC_OVERRIDE))
515 if (smack_syslog_label != NULL && smack_syslog_label != skp)
526 * smack_sb_alloc_security - allocate a superblock blob
527 * @sb: the superblock getting the blob
529 * Returns 0 on success or -ENOMEM on error.
531 static int smack_sb_alloc_security(struct super_block *sb)
533 struct superblock_smack *sbsp = smack_superblock(sb);
535 sbsp->smk_root = &smack_known_floor;
536 sbsp->smk_default = &smack_known_floor;
537 sbsp->smk_floor = &smack_known_floor;
538 sbsp->smk_hat = &smack_known_hat;
540 * SMK_SB_INITIALIZED will be zero from kzalloc.
546 struct smack_mnt_opts {
547 const char *fsdefault;
551 const char *fstransmute;
554 static void smack_free_mnt_opts(void *mnt_opts)
559 static int smack_add_opt(int token, const char *s, void **mnt_opts)
561 struct smack_mnt_opts *opts = *mnt_opts;
562 struct smack_known *skp;
565 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
573 skp = smk_import_entry(s, 0);
581 opts->fsdefault = skp->smk_known;
586 opts->fsfloor = skp->smk_known;
591 opts->fshat = skp->smk_known;
596 opts->fsroot = skp->smk_known;
598 case Opt_fstransmute:
599 if (opts->fstransmute)
601 opts->fstransmute = skp->smk_known;
607 pr_warn("Smack: duplicate mount options\n");
612 * smack_fs_context_submount - Initialise security data for a filesystem context
613 * @fc: The filesystem context.
614 * @reference: reference superblock
616 * Returns 0 on success or -ENOMEM on error.
618 static int smack_fs_context_submount(struct fs_context *fc,
619 struct super_block *reference)
621 struct superblock_smack *sbsp;
622 struct smack_mnt_opts *ctx;
623 struct inode_smack *isp;
625 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
630 sbsp = smack_superblock(reference);
631 isp = smack_inode(reference->s_root->d_inode);
633 if (sbsp->smk_default) {
634 ctx->fsdefault = kstrdup(sbsp->smk_default->smk_known, GFP_KERNEL);
639 if (sbsp->smk_floor) {
640 ctx->fsfloor = kstrdup(sbsp->smk_floor->smk_known, GFP_KERNEL);
646 ctx->fshat = kstrdup(sbsp->smk_hat->smk_known, GFP_KERNEL);
651 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
652 if (sbsp->smk_root) {
653 ctx->fstransmute = kstrdup(sbsp->smk_root->smk_known, GFP_KERNEL);
654 if (!ctx->fstransmute)
662 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
663 * @fc: The new filesystem context.
664 * @src_fc: The source filesystem context being duplicated.
666 * Returns 0 on success or -ENOMEM on error.
668 static int smack_fs_context_dup(struct fs_context *fc,
669 struct fs_context *src_fc)
671 struct smack_mnt_opts *dst, *src = src_fc->security;
676 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
681 dst->fsdefault = src->fsdefault;
682 dst->fsfloor = src->fsfloor;
683 dst->fshat = src->fshat;
684 dst->fsroot = src->fsroot;
685 dst->fstransmute = src->fstransmute;
690 static const struct fs_parameter_spec smack_fs_parameters[] = {
691 fsparam_string("smackfsdef", Opt_fsdefault),
692 fsparam_string("smackfsdefault", Opt_fsdefault),
693 fsparam_string("smackfsfloor", Opt_fsfloor),
694 fsparam_string("smackfshat", Opt_fshat),
695 fsparam_string("smackfsroot", Opt_fsroot),
696 fsparam_string("smackfstransmute", Opt_fstransmute),
701 * smack_fs_context_parse_param - Parse a single mount parameter
702 * @fc: The new filesystem context being constructed.
703 * @param: The parameter.
705 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
708 static int smack_fs_context_parse_param(struct fs_context *fc,
709 struct fs_parameter *param)
711 struct fs_parse_result result;
714 opt = fs_parse(fc, smack_fs_parameters, param, &result);
718 rc = smack_add_opt(opt, param->string, &fc->security);
720 param->string = NULL;
724 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
726 char *from = options, *to = options;
730 char *next = strchr(from, ',');
739 token = match_opt_prefix(from, len, &arg);
740 if (token != Opt_error) {
741 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
742 rc = smack_add_opt(token, arg, mnt_opts);
746 smack_free_mnt_opts(*mnt_opts);
751 if (!first) { // copy with preceding comma
756 memmove(to, from, len);
769 * smack_set_mnt_opts - set Smack specific mount options
770 * @sb: the file system superblock
771 * @mnt_opts: Smack mount options
772 * @kern_flags: mount option from kernel space or user space
773 * @set_kern_flags: where to store converted mount opts
775 * Returns 0 on success, an error code on failure
777 * Allow filesystems with binary mount data to explicitly set Smack mount
780 static int smack_set_mnt_opts(struct super_block *sb,
782 unsigned long kern_flags,
783 unsigned long *set_kern_flags)
785 struct dentry *root = sb->s_root;
786 struct inode *inode = d_backing_inode(root);
787 struct superblock_smack *sp = smack_superblock(sb);
788 struct inode_smack *isp;
789 struct smack_known *skp;
790 struct smack_mnt_opts *opts = mnt_opts;
791 bool transmute = false;
793 if (sp->smk_flags & SMK_SB_INITIALIZED)
796 if (!smack_privileged(CAP_MAC_ADMIN)) {
798 * Unprivileged mounts don't get to specify Smack values.
803 * Unprivileged mounts get root and default from the caller.
805 skp = smk_of_current();
807 sp->smk_default = skp;
809 * For a handful of fs types with no user-controlled
810 * backing store it's okay to trust security labels
811 * in the filesystem. The rest are untrusted.
813 if (sb->s_user_ns != &init_user_ns &&
814 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
815 sb->s_magic != RAMFS_MAGIC) {
817 sp->smk_flags |= SMK_SB_UNTRUSTED;
821 sp->smk_flags |= SMK_SB_INITIALIZED;
824 if (opts->fsdefault) {
825 skp = smk_import_entry(opts->fsdefault, 0);
828 sp->smk_default = skp;
831 skp = smk_import_entry(opts->fsfloor, 0);
837 skp = smk_import_entry(opts->fshat, 0);
843 skp = smk_import_entry(opts->fsroot, 0);
848 if (opts->fstransmute) {
849 skp = smk_import_entry(opts->fstransmute, 0);
858 * Initialize the root inode.
860 init_inode_smack(inode, sp->smk_root);
863 isp = smack_inode(inode);
864 isp->smk_flags |= SMK_INODE_TRANSMUTE;
871 * smack_sb_statfs - Smack check on statfs
872 * @dentry: identifies the file system in question
874 * Returns 0 if current can read the floor of the filesystem,
875 * and error code otherwise
877 static int smack_sb_statfs(struct dentry *dentry)
879 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
881 struct smk_audit_info ad;
883 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
884 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
886 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
887 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
896 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
897 * @bprm: the exec information
899 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
901 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
903 struct inode *inode = file_inode(bprm->file);
904 struct task_smack *bsp = smack_cred(bprm->cred);
905 struct inode_smack *isp;
906 struct superblock_smack *sbsp;
909 isp = smack_inode(inode);
910 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
913 sbsp = smack_superblock(inode->i_sb);
914 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
915 isp->smk_task != sbsp->smk_root)
918 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
919 struct task_struct *tracer;
923 tracer = ptrace_parent(current);
924 if (likely(tracer != NULL))
925 rc = smk_ptrace_rule_check(tracer,
934 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
937 bsp->smk_task = isp->smk_task;
938 bprm->per_clear |= PER_CLEAR_ON_SETID;
940 /* Decide if this is a secure exec. */
941 if (bsp->smk_task != bsp->smk_forked)
942 bprm->secureexec = 1;
952 * smack_inode_alloc_security - allocate an inode blob
953 * @inode: the inode in need of a blob
957 static int smack_inode_alloc_security(struct inode *inode)
959 struct smack_known *skp = smk_of_current();
961 init_inode_smack(inode, skp);
966 * smack_inode_init_security - copy out the smack from an inode
967 * @inode: the newly created inode
968 * @dir: containing directory object
970 * @xattrs: where to put the attributes
971 * @xattr_count: current number of LSM-provided xattrs (updated)
973 * Returns 0 if it all works out, -ENOMEM if there's no memory
975 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
976 const struct qstr *qstr,
977 struct xattr *xattrs, int *xattr_count)
979 struct task_smack *tsp = smack_cred(current_cred());
980 struct inode_smack *issp = smack_inode(inode);
981 struct smack_known *skp = smk_of_task(tsp);
982 struct smack_known *isp = smk_of_inode(inode);
983 struct smack_known *dsp = smk_of_inode(dir);
984 struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count);
988 * If equal, transmuting already occurred in
989 * smack_dentry_create_files_as(). No need to check again.
991 if (tsp->smk_task != tsp->smk_transmuted) {
993 may = smk_access_entry(skp->smk_known, dsp->smk_known,
999 * In addition to having smk_task equal to smk_transmuted,
1000 * if the access rule allows transmutation and the directory
1001 * requests transmutation then by all means transmute.
1002 * Mark the inode as changed.
1004 if ((tsp->smk_task == tsp->smk_transmuted) ||
1005 (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1006 smk_inode_transmutable(dir))) {
1007 struct xattr *xattr_transmute;
1010 * The caller of smack_dentry_create_files_as()
1011 * should have overridden the current cred, so the
1012 * inode label was already set correctly in
1013 * smack_inode_alloc_security().
1015 if (tsp->smk_task != tsp->smk_transmuted)
1016 isp = issp->smk_inode = dsp;
1018 issp->smk_flags |= SMK_INODE_TRANSMUTE;
1019 xattr_transmute = lsm_get_xattr_slot(xattrs,
1021 if (xattr_transmute) {
1022 xattr_transmute->value = kmemdup(TRANS_TRUE,
1025 if (!xattr_transmute->value)
1028 xattr_transmute->value_len = TRANS_TRUE_SIZE;
1029 xattr_transmute->name = XATTR_SMACK_TRANSMUTE;
1033 issp->smk_flags |= SMK_INODE_INSTANT;
1036 xattr->value = kstrdup(isp->smk_known, GFP_NOFS);
1040 xattr->value_len = strlen(isp->smk_known);
1041 xattr->name = XATTR_SMACK_SUFFIX;
1048 * smack_inode_link - Smack check on link
1049 * @old_dentry: the existing object
1051 * @new_dentry: the new object
1053 * Returns 0 if access is permitted, an error code otherwise
1055 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1056 struct dentry *new_dentry)
1058 struct smack_known *isp;
1059 struct smk_audit_info ad;
1062 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1063 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1065 isp = smk_of_inode(d_backing_inode(old_dentry));
1066 rc = smk_curacc(isp, MAY_WRITE, &ad);
1067 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1069 if (rc == 0 && d_is_positive(new_dentry)) {
1070 isp = smk_of_inode(d_backing_inode(new_dentry));
1071 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1072 rc = smk_curacc(isp, MAY_WRITE, &ad);
1073 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1080 * smack_inode_unlink - Smack check on inode deletion
1081 * @dir: containing directory object
1082 * @dentry: file to unlink
1084 * Returns 0 if current can write the containing directory
1085 * and the object, error code otherwise
1087 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1089 struct inode *ip = d_backing_inode(dentry);
1090 struct smk_audit_info ad;
1093 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1094 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1097 * You need write access to the thing you're unlinking
1099 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1100 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1103 * You also need write access to the containing directory
1105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1106 smk_ad_setfield_u_fs_inode(&ad, dir);
1107 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1108 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1114 * smack_inode_rmdir - Smack check on directory deletion
1115 * @dir: containing directory object
1116 * @dentry: directory to unlink
1118 * Returns 0 if current can write the containing directory
1119 * and the directory, error code otherwise
1121 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1123 struct smk_audit_info ad;
1126 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1127 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1130 * You need write access to the thing you're removing
1132 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1133 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1136 * You also need write access to the containing directory
1138 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1139 smk_ad_setfield_u_fs_inode(&ad, dir);
1140 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1141 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1148 * smack_inode_rename - Smack check on rename
1149 * @old_inode: unused
1150 * @old_dentry: the old object
1151 * @new_inode: unused
1152 * @new_dentry: the new object
1154 * Read and write access is required on both the old and
1157 * Returns 0 if access is permitted, an error code otherwise
1159 static int smack_inode_rename(struct inode *old_inode,
1160 struct dentry *old_dentry,
1161 struct inode *new_inode,
1162 struct dentry *new_dentry)
1165 struct smack_known *isp;
1166 struct smk_audit_info ad;
1168 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1169 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1171 isp = smk_of_inode(d_backing_inode(old_dentry));
1172 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1173 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1175 if (rc == 0 && d_is_positive(new_dentry)) {
1176 isp = smk_of_inode(d_backing_inode(new_dentry));
1177 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1178 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1179 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1185 * smack_inode_permission - Smack version of permission()
1186 * @inode: the inode in question
1187 * @mask: the access requested
1189 * This is the important Smack hook.
1191 * Returns 0 if access is permitted, an error code otherwise
1193 static int smack_inode_permission(struct inode *inode, int mask)
1195 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
1196 struct smk_audit_info ad;
1197 int no_block = mask & MAY_NOT_BLOCK;
1200 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1202 * No permission to check. Existence test. Yup, it's there.
1207 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1208 if (smk_of_inode(inode) != sbsp->smk_root)
1212 /* May be droppable after audit */
1215 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1216 smk_ad_setfield_u_fs_inode(&ad, inode);
1217 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1218 rc = smk_bu_inode(inode, mask, rc);
1223 * smack_inode_setattr - Smack check for setting attributes
1224 * @idmap: idmap of the mount
1225 * @dentry: the object
1226 * @iattr: for the force flag
1228 * Returns 0 if access is permitted, an error code otherwise
1230 static int smack_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
1231 struct iattr *iattr)
1233 struct smk_audit_info ad;
1237 * Need to allow for clearing the setuid bit.
1239 if (iattr->ia_valid & ATTR_FORCE)
1241 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1242 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1244 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1245 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1250 * smack_inode_getattr - Smack check for getting attributes
1251 * @path: path to extract the info from
1253 * Returns 0 if access is permitted, an error code otherwise
1255 static int smack_inode_getattr(const struct path *path)
1257 struct smk_audit_info ad;
1258 struct inode *inode = d_backing_inode(path->dentry);
1261 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1262 smk_ad_setfield_u_fs_path(&ad, *path);
1263 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1264 rc = smk_bu_inode(inode, MAY_READ, rc);
1269 * smack_inode_xattr_skipcap - Skip the xattr capability checks?
1270 * @name: name of the xattr
1272 * Returns 1 to indicate that Smack "owns" the access control rights to xattrs
1273 * named @name; the LSM layer should avoid enforcing any traditional
1274 * capability based access controls on this xattr. Returns 0 to indicate that
1275 * Smack does not "own" the access control rights to xattrs named @name and is
1276 * deferring to the LSM layer for further access controls, including capability
1279 static int smack_inode_xattr_skipcap(const char *name)
1281 if (strncmp(name, XATTR_SMACK_SUFFIX, strlen(XATTR_SMACK_SUFFIX)))
1284 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1285 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1286 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1287 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1288 strcmp(name, XATTR_NAME_SMACKMMAP) == 0 ||
1289 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1296 * smack_inode_setxattr - Smack check for setting xattrs
1297 * @idmap: idmap of the mount
1298 * @dentry: the object
1299 * @name: name of the attribute
1300 * @value: value of the attribute
1301 * @size: size of the value
1304 * This protects the Smack attribute explicitly.
1306 * Returns 0 if access is permitted, an error code otherwise
1308 static int smack_inode_setxattr(struct mnt_idmap *idmap,
1309 struct dentry *dentry, const char *name,
1310 const void *value, size_t size, int flags)
1312 struct smk_audit_info ad;
1313 struct smack_known *skp;
1315 int check_import = 0;
1320 * Check label validity here so import won't fail in post_setxattr
1322 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1323 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1324 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1327 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1328 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1332 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1334 if (!S_ISDIR(d_backing_inode(dentry)->i_mode) ||
1335 size != TRANS_TRUE_SIZE ||
1336 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1340 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1343 if (rc == 0 && check_import) {
1344 skp = size ? smk_import_entry(value, size) : NULL;
1347 else if (skp == NULL || (check_star &&
1348 (skp == &smack_known_star || skp == &smack_known_web)))
1352 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1353 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1356 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1357 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1364 * smack_inode_post_setxattr - Apply the Smack update approved above
1366 * @name: attribute name
1367 * @value: attribute value
1368 * @size: attribute size
1371 * Set the pointer in the inode blob to the entry found
1372 * in the master label list.
1374 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1375 const void *value, size_t size, int flags)
1377 struct smack_known *skp;
1378 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1380 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1381 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1385 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1386 skp = smk_import_entry(value, size);
1388 isp->smk_inode = skp;
1389 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1390 skp = smk_import_entry(value, size);
1392 isp->smk_task = skp;
1393 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1394 skp = smk_import_entry(value, size);
1396 isp->smk_mmap = skp;
1403 * smack_inode_getxattr - Smack check on getxattr
1404 * @dentry: the object
1407 * Returns 0 if access is permitted, an error code otherwise
1409 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1411 struct smk_audit_info ad;
1414 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1415 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1417 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1418 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1423 * smack_inode_removexattr - Smack check on removexattr
1424 * @idmap: idmap of the mount
1425 * @dentry: the object
1426 * @name: name of the attribute
1428 * Removing the Smack attribute requires CAP_MAC_ADMIN
1430 * Returns 0 if access is permitted, an error code otherwise
1432 static int smack_inode_removexattr(struct mnt_idmap *idmap,
1433 struct dentry *dentry, const char *name)
1435 struct inode_smack *isp;
1436 struct smk_audit_info ad;
1439 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1440 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1441 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1442 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1443 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1444 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1445 if (!smack_privileged(CAP_MAC_ADMIN))
1452 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1453 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1455 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1456 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1460 isp = smack_inode(d_backing_inode(dentry));
1462 * Don't do anything special for these.
1463 * XATTR_NAME_SMACKIPIN
1464 * XATTR_NAME_SMACKIPOUT
1466 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1467 struct super_block *sbp = dentry->d_sb;
1468 struct superblock_smack *sbsp = smack_superblock(sbp);
1470 isp->smk_inode = sbsp->smk_default;
1471 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1472 isp->smk_task = NULL;
1473 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1474 isp->smk_mmap = NULL;
1475 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1476 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1482 * smack_inode_set_acl - Smack check for setting posix acls
1483 * @idmap: idmap of the mnt this request came from
1484 * @dentry: the object
1485 * @acl_name: name of the posix acl
1486 * @kacl: the posix acls
1488 * Returns 0 if access is permitted, an error code otherwise
1490 static int smack_inode_set_acl(struct mnt_idmap *idmap,
1491 struct dentry *dentry, const char *acl_name,
1492 struct posix_acl *kacl)
1494 struct smk_audit_info ad;
1497 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1498 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1500 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1501 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1506 * smack_inode_get_acl - Smack check for getting posix acls
1507 * @idmap: idmap of the mnt this request came from
1508 * @dentry: the object
1509 * @acl_name: name of the posix acl
1511 * Returns 0 if access is permitted, an error code otherwise
1513 static int smack_inode_get_acl(struct mnt_idmap *idmap,
1514 struct dentry *dentry, const char *acl_name)
1516 struct smk_audit_info ad;
1519 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1520 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1522 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1523 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1528 * smack_inode_remove_acl - Smack check for getting posix acls
1529 * @idmap: idmap of the mnt this request came from
1530 * @dentry: the object
1531 * @acl_name: name of the posix acl
1533 * Returns 0 if access is permitted, an error code otherwise
1535 static int smack_inode_remove_acl(struct mnt_idmap *idmap,
1536 struct dentry *dentry, const char *acl_name)
1538 struct smk_audit_info ad;
1541 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1542 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1544 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1545 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1550 * smack_inode_getsecurity - get smack xattrs
1551 * @idmap: idmap of the mount
1552 * @inode: the object
1553 * @name: attribute name
1554 * @buffer: where to put the result
1555 * @alloc: duplicate memory
1557 * Returns the size of the attribute or an error code
1559 static int smack_inode_getsecurity(struct mnt_idmap *idmap,
1560 struct inode *inode, const char *name,
1561 void **buffer, bool alloc)
1563 struct socket_smack *ssp;
1564 struct socket *sock;
1565 struct super_block *sbp;
1566 struct inode *ip = inode;
1567 struct smack_known *isp;
1568 struct inode_smack *ispp;
1572 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1573 isp = smk_of_inode(inode);
1574 } else if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) {
1575 ispp = smack_inode(inode);
1576 if (ispp->smk_flags & SMK_INODE_TRANSMUTE)
1582 * The rest of the Smack xattrs are only on sockets.
1585 if (sbp->s_magic != SOCKFS_MAGIC)
1588 sock = SOCKET_I(ip);
1589 if (sock == NULL || sock->sk == NULL)
1592 ssp = smack_sock(sock->sk);
1594 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1596 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1603 label = isp->smk_known;
1605 label_len = strlen(label);
1608 *buffer = kstrdup(label, GFP_KERNEL);
1609 if (*buffer == NULL)
1618 * smack_inode_listsecurity - list the Smack attributes
1619 * @inode: the object
1620 * @buffer: where they go
1621 * @buffer_size: size of buffer
1623 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1626 int len = sizeof(XATTR_NAME_SMACK);
1628 if (buffer != NULL && len <= buffer_size)
1629 memcpy(buffer, XATTR_NAME_SMACK, len);
1635 * smack_inode_getlsmprop - Extract inode's security id
1636 * @inode: inode to extract the info from
1637 * @prop: where result will be saved
1639 static void smack_inode_getlsmprop(struct inode *inode, struct lsm_prop *prop)
1641 prop->smack.skp = smk_of_inode(inode);
1649 * There is no smack_file_permission hook
1651 * Should access checks be done on each read or write?
1652 * UNICOS and SELinux say yes.
1653 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1655 * I'll say no for now. Smack does not do the frequent
1656 * label changing that SELinux does.
1660 * smack_file_alloc_security - assign a file security blob
1663 * The security blob for a file is a pointer to the master
1664 * label list, so no allocation is done.
1666 * f_security is the owner security information. It
1667 * isn't used on file access checks, it's for send_sigio.
1671 static int smack_file_alloc_security(struct file *file)
1673 struct smack_known **blob = smack_file(file);
1675 *blob = smk_of_current();
1680 * smack_file_ioctl - Smack check on ioctls
1685 * Relies heavily on the correct use of the ioctl command conventions.
1687 * Returns 0 if allowed, error code otherwise
1689 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1693 struct smk_audit_info ad;
1694 struct inode *inode = file_inode(file);
1696 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);
1702 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1703 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1704 rc = smk_bu_file(file, MAY_WRITE, rc);
1707 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1708 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1709 rc = smk_bu_file(file, MAY_READ, rc);
1716 * smack_file_lock - Smack check on file locking
1720 * Returns 0 if current has lock access, error code otherwise
1722 static int smack_file_lock(struct file *file, unsigned int cmd)
1724 struct smk_audit_info ad;
1726 struct inode *inode = file_inode(file);
1728 if (unlikely(IS_PRIVATE(inode)))
1731 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1732 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1733 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1734 rc = smk_bu_file(file, MAY_LOCK, rc);
1739 * smack_file_fcntl - Smack check on fcntl
1741 * @cmd: what action to check
1744 * Generally these operations are harmless.
1745 * File locking operations present an obvious mechanism
1746 * for passing information, so they require write access.
1748 * Returns 0 if current has access, error code otherwise
1750 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1753 struct smk_audit_info ad;
1755 struct inode *inode = file_inode(file);
1757 if (unlikely(IS_PRIVATE(inode)))
1765 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1766 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1767 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1768 rc = smk_bu_file(file, MAY_LOCK, rc);
1772 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1773 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1774 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1775 rc = smk_bu_file(file, MAY_WRITE, rc);
1785 * smack_mmap_file - Check permissions for a mmap operation.
1786 * @file: contains the file structure for file to map (may be NULL).
1787 * @reqprot: contains the protection requested by the application.
1788 * @prot: contains the protection that will be applied by the kernel.
1789 * @flags: contains the operational flags.
1791 * The @file may be NULL, e.g. if mapping anonymous memory.
1793 * Return 0 if permission is granted.
1795 static int smack_mmap_file(struct file *file,
1796 unsigned long reqprot, unsigned long prot,
1797 unsigned long flags)
1799 struct smack_known *skp;
1800 struct smack_known *mkp;
1801 struct smack_rule *srp;
1802 struct task_smack *tsp;
1803 struct smack_known *okp;
1804 struct inode_smack *isp;
1805 struct superblock_smack *sbsp;
1814 if (unlikely(IS_PRIVATE(file_inode(file))))
1817 isp = smack_inode(file_inode(file));
1818 if (isp->smk_mmap == NULL)
1820 sbsp = smack_superblock(file_inode(file)->i_sb);
1821 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1822 isp->smk_mmap != sbsp->smk_root)
1824 mkp = isp->smk_mmap;
1826 tsp = smack_cred(current_cred());
1827 skp = smk_of_current();
1832 * For each Smack rule associated with the subject
1833 * label verify that the SMACK64MMAP also has access
1834 * to that rule's object label.
1836 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1837 okp = srp->smk_object;
1839 * Matching labels always allows access.
1841 if (mkp->smk_known == okp->smk_known)
1844 * If there is a matching local rule take
1845 * that into account as well.
1847 may = smk_access_entry(srp->smk_subject->smk_known,
1851 may = srp->smk_access;
1853 may &= srp->smk_access;
1855 * If may is zero the SMACK64MMAP subject can't
1856 * possibly have less access.
1862 * Fetch the global list entry.
1863 * If there isn't one a SMACK64MMAP subject
1864 * can't have as much access as current.
1866 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1868 if (mmay == -ENOENT) {
1873 * If there is a local entry it modifies the
1874 * potential access, too.
1876 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1878 if (tmay != -ENOENT)
1882 * If there is any access available to current that is
1883 * not available to a SMACK64MMAP subject
1886 if ((may | mmay) != mmay) {
1898 * smack_file_set_fowner - set the file security blob value
1899 * @file: object in question
1902 static void smack_file_set_fowner(struct file *file)
1904 struct smack_known **blob = smack_file(file);
1906 *blob = smk_of_current();
1910 * smack_file_send_sigiotask - Smack on sigio
1911 * @tsk: The target task
1912 * @fown: the object the signal come from
1915 * Allow a privileged task to get signals even if it shouldn't
1917 * Returns 0 if a subject with the object's smack could
1918 * write to the task, an error code otherwise.
1920 static int smack_file_send_sigiotask(struct task_struct *tsk,
1921 struct fown_struct *fown, int signum)
1923 struct smack_known **blob;
1924 struct smack_known *skp;
1925 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1926 const struct cred *tcred;
1929 struct smk_audit_info ad;
1932 * struct fown_struct is never outside the context of a struct file
1936 /* we don't log here as rc can be overridden */
1937 blob = smack_file(file);
1939 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1940 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1943 tcred = __task_cred(tsk);
1944 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1948 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1949 smk_ad_setfield_u_tsk(&ad, tsk);
1950 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1955 * smack_file_receive - Smack file receive check
1958 * Returns 0 if current has access, error code otherwise
1960 static int smack_file_receive(struct file *file)
1964 struct smk_audit_info ad;
1965 struct inode *inode = file_inode(file);
1966 struct socket *sock;
1967 struct task_smack *tsp;
1968 struct socket_smack *ssp;
1970 if (unlikely(IS_PRIVATE(inode)))
1973 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1974 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1976 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1977 sock = SOCKET_I(inode);
1978 ssp = smack_sock(sock->sk);
1979 tsp = smack_cred(current_cred());
1981 * If the receiving process can't write to the
1982 * passed socket or if the passed socket can't
1983 * write to the receiving process don't accept
1984 * the passed socket.
1986 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1987 rc = smk_bu_file(file, may, rc);
1990 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1991 rc = smk_bu_file(file, may, rc);
1995 * This code relies on bitmasks.
1997 if (file->f_mode & FMODE_READ)
1999 if (file->f_mode & FMODE_WRITE)
2002 rc = smk_curacc(smk_of_inode(inode), may, &ad);
2003 rc = smk_bu_file(file, may, rc);
2008 * smack_file_open - Smack dentry open processing
2011 * Set the security blob in the file structure.
2012 * Allow the open only if the task has read access. There are
2013 * many read operations (e.g. fstat) that you can do with an
2014 * fd even if you have the file open write-only.
2016 * Returns 0 if current has access, error code otherwise
2018 static int smack_file_open(struct file *file)
2020 struct task_smack *tsp = smack_cred(file->f_cred);
2021 struct inode *inode = file_inode(file);
2022 struct smk_audit_info ad;
2025 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
2026 smk_ad_setfield_u_fs_path(&ad, file->f_path);
2027 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
2028 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
2038 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
2039 * @cred: the new credentials
2040 * @gfp: the atomicity of any memory allocations
2042 * Prepare a blank set of credentials for modification. This must allocate all
2043 * the memory the LSM module might require such that cred_transfer() can
2044 * complete without error.
2046 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
2048 init_task_smack(smack_cred(cred), NULL, NULL);
2054 * smack_cred_free - "free" task-level security credentials
2055 * @cred: the credentials in question
2058 static void smack_cred_free(struct cred *cred)
2060 struct task_smack *tsp = smack_cred(cred);
2061 struct smack_rule *rp;
2062 struct list_head *l;
2063 struct list_head *n;
2065 smk_destroy_label_list(&tsp->smk_relabel);
2067 list_for_each_safe(l, n, &tsp->smk_rules) {
2068 rp = list_entry(l, struct smack_rule, list);
2069 list_del(&rp->list);
2070 kmem_cache_free(smack_rule_cache, rp);
2075 * smack_cred_prepare - prepare new set of credentials for modification
2076 * @new: the new credentials
2077 * @old: the original credentials
2078 * @gfp: the atomicity of any memory allocations
2080 * Prepare a new set of credentials for modification.
2082 static int smack_cred_prepare(struct cred *new, const struct cred *old,
2085 struct task_smack *old_tsp = smack_cred(old);
2086 struct task_smack *new_tsp = smack_cred(new);
2089 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
2091 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2095 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2101 * smack_cred_transfer - Transfer the old credentials to the new credentials
2102 * @new: the new credentials
2103 * @old: the original credentials
2105 * Fill in a set of blank credentials from another set of credentials.
2107 static void smack_cred_transfer(struct cred *new, const struct cred *old)
2109 struct task_smack *old_tsp = smack_cred(old);
2110 struct task_smack *new_tsp = smack_cred(new);
2112 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
2116 * smack_cred_getsecid - get the secid corresponding to a creds structure
2117 * @cred: the object creds
2118 * @secid: where to put the result
2120 * Sets the secid to contain a u32 version of the smack label.
2122 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
2124 struct smack_known *skp;
2127 skp = smk_of_task(smack_cred(cred));
2128 *secid = skp->smk_secid;
2133 * smack_cred_getlsmprop - get the Smack label for a creds structure
2134 * @cred: the object creds
2135 * @prop: where to put the data
2137 * Sets the Smack part of the ref
2139 static void smack_cred_getlsmprop(const struct cred *cred,
2140 struct lsm_prop *prop)
2143 prop->smack.skp = smk_of_task(smack_cred(cred));
2148 * smack_kernel_act_as - Set the subjective context in a set of credentials
2149 * @new: points to the set of credentials to be modified.
2150 * @secid: specifies the security ID to be set
2152 * Set the security data for a kernel service.
2154 static int smack_kernel_act_as(struct cred *new, u32 secid)
2156 struct task_smack *new_tsp = smack_cred(new);
2158 new_tsp->smk_task = smack_from_secid(secid);
2163 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2164 * @new: points to the set of credentials to be modified
2165 * @inode: points to the inode to use as a reference
2167 * Set the file creation context in a set of credentials to the same
2168 * as the objective context of the specified inode
2170 static int smack_kernel_create_files_as(struct cred *new,
2171 struct inode *inode)
2173 struct inode_smack *isp = smack_inode(inode);
2174 struct task_smack *tsp = smack_cred(new);
2176 tsp->smk_forked = isp->smk_inode;
2177 tsp->smk_task = tsp->smk_forked;
2182 * smk_curacc_on_task - helper to log task related access
2183 * @p: the task object
2184 * @access: the access requested
2185 * @caller: name of the calling function for audit
2187 * Return 0 if access is permitted
2189 static int smk_curacc_on_task(struct task_struct *p, int access,
2192 struct smk_audit_info ad;
2193 struct smack_known *skp = smk_of_task_struct_obj(p);
2196 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2197 smk_ad_setfield_u_tsk(&ad, p);
2198 rc = smk_curacc(skp, access, &ad);
2199 rc = smk_bu_task(p, access, rc);
2204 * smack_task_setpgid - Smack check on setting pgid
2205 * @p: the task object
2208 * Return 0 if write access is permitted
2210 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2212 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2216 * smack_task_getpgid - Smack access check for getpgid
2217 * @p: the object task
2219 * Returns 0 if current can read the object task, error code otherwise
2221 static int smack_task_getpgid(struct task_struct *p)
2223 return smk_curacc_on_task(p, MAY_READ, __func__);
2227 * smack_task_getsid - Smack access check for getsid
2228 * @p: the object task
2230 * Returns 0 if current can read the object task, error code otherwise
2232 static int smack_task_getsid(struct task_struct *p)
2234 return smk_curacc_on_task(p, MAY_READ, __func__);
2238 * smack_current_getlsmprop_subj - get the subjective secid of the current task
2239 * @prop: where to put the result
2241 * Sets the secid to contain a u32 version of the task's subjective smack label.
2243 static void smack_current_getlsmprop_subj(struct lsm_prop *prop)
2245 prop->smack.skp = smk_of_current();
2249 * smack_task_getlsmprop_obj - get the objective data of the task
2251 * @prop: where to put the result
2253 * Sets the secid to contain a u32 version of the task's objective smack label.
2255 static void smack_task_getlsmprop_obj(struct task_struct *p,
2256 struct lsm_prop *prop)
2258 prop->smack.skp = smk_of_task_struct_obj(p);
2262 * smack_task_setnice - Smack check on setting nice
2263 * @p: the task object
2266 * Return 0 if write access is permitted
2268 static int smack_task_setnice(struct task_struct *p, int nice)
2270 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2274 * smack_task_setioprio - Smack check on setting ioprio
2275 * @p: the task object
2278 * Return 0 if write access is permitted
2280 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2282 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2286 * smack_task_getioprio - Smack check on reading ioprio
2287 * @p: the task object
2289 * Return 0 if read access is permitted
2291 static int smack_task_getioprio(struct task_struct *p)
2293 return smk_curacc_on_task(p, MAY_READ, __func__);
2297 * smack_task_setscheduler - Smack check on setting scheduler
2298 * @p: the task object
2300 * Return 0 if read access is permitted
2302 static int smack_task_setscheduler(struct task_struct *p)
2304 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2308 * smack_task_getscheduler - Smack check on reading scheduler
2309 * @p: the task object
2311 * Return 0 if read access is permitted
2313 static int smack_task_getscheduler(struct task_struct *p)
2315 return smk_curacc_on_task(p, MAY_READ, __func__);
2319 * smack_task_movememory - Smack check on moving memory
2320 * @p: the task object
2322 * Return 0 if write access is permitted
2324 static int smack_task_movememory(struct task_struct *p)
2326 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2330 * smack_task_kill - Smack check on signal delivery
2331 * @p: the task object
2334 * @cred: identifies the cred to use in lieu of current's
2336 * Return 0 if write access is permitted
2339 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2340 int sig, const struct cred *cred)
2342 struct smk_audit_info ad;
2343 struct smack_known *skp;
2344 struct smack_known *tkp = smk_of_task_struct_obj(p);
2348 return 0; /* null signal; existence test */
2350 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2351 smk_ad_setfield_u_tsk(&ad, p);
2353 * Sending a signal requires that the sender
2354 * can write the receiver.
2357 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2358 rc = smk_bu_task(p, MAY_DELIVER, rc);
2362 * If the cred isn't NULL we're dealing with some USB IO
2363 * specific behavior. This is not clean. For one thing
2364 * we can't take privilege into account.
2366 skp = smk_of_task(smack_cred(cred));
2367 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2368 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2373 * smack_task_to_inode - copy task smack into the inode blob
2374 * @p: task to copy from
2375 * @inode: inode to copy to
2377 * Sets the smack pointer in the inode security blob
2379 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2381 struct inode_smack *isp = smack_inode(inode);
2382 struct smack_known *skp = smk_of_task_struct_obj(p);
2384 isp->smk_inode = skp;
2385 isp->smk_flags |= SMK_INODE_INSTANT;
2393 * smack_sk_alloc_security - Allocate a socket blob
2396 * @gfp_flags: memory allocation flags
2398 * Assign Smack pointers to current
2400 * Returns 0 on success, -ENOMEM is there's no memory
2402 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2404 struct smack_known *skp = smk_of_current();
2405 struct socket_smack *ssp = smack_sock(sk);
2408 * Sockets created by kernel threads receive web label.
2410 if (unlikely(current->flags & PF_KTHREAD)) {
2411 ssp->smk_in = &smack_known_web;
2412 ssp->smk_out = &smack_known_web;
2417 ssp->smk_packet = NULL;
2422 #ifdef SMACK_IPV6_PORT_LABELING
2424 * smack_sk_free_security - Free a socket blob
2427 * Clears the blob pointer
2429 static void smack_sk_free_security(struct sock *sk)
2431 struct smk_port_label *spp;
2433 if (sk->sk_family == PF_INET6) {
2435 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2436 if (spp->smk_sock != sk)
2438 spp->smk_can_reuse = 1;
2447 * smack_sk_clone_security - Copy security context
2448 * @sk: the old socket
2449 * @newsk: the new socket
2451 * Copy the security context of the old socket pointer to the cloned
2453 static void smack_sk_clone_security(const struct sock *sk, struct sock *newsk)
2455 struct socket_smack *ssp_old = smack_sock(sk);
2456 struct socket_smack *ssp_new = smack_sock(newsk);
2458 *ssp_new = *ssp_old;
2462 * smack_ipv4host_label - check host based restrictions
2463 * @sip: the object end
2465 * looks for host based access restrictions
2467 * This version will only be appropriate for really small sets of single label
2468 * hosts. The caller is responsible for ensuring that the RCU read lock is
2469 * taken before calling this function.
2471 * Returns the label of the far end or NULL if it's not special.
2473 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2475 struct smk_net4addr *snp;
2476 struct in_addr *siap = &sip->sin_addr;
2478 if (siap->s_addr == 0)
2481 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2483 * we break after finding the first match because
2484 * the list is sorted from longest to shortest mask
2485 * so we have found the most specific match
2487 if (snp->smk_host.s_addr ==
2488 (siap->s_addr & snp->smk_mask.s_addr))
2489 return snp->smk_label;
2494 #if IS_ENABLED(CONFIG_IPV6)
2496 * smk_ipv6_localhost - Check for local ipv6 host address
2499 * Returns boolean true if this is the localhost address
2501 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2503 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2504 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2506 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2507 ntohs(be16p[7]) == 1)
2513 * smack_ipv6host_label - check host based restrictions
2514 * @sip: the object end
2516 * looks for host based access restrictions
2518 * This version will only be appropriate for really small sets of single label
2519 * hosts. The caller is responsible for ensuring that the RCU read lock is
2520 * taken before calling this function.
2522 * Returns the label of the far end or NULL if it's not special.
2524 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2526 struct smk_net6addr *snp;
2527 struct in6_addr *sap = &sip->sin6_addr;
2532 * It's local. Don't look for a host label.
2534 if (smk_ipv6_localhost(sip))
2537 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2539 * If the label is NULL the entry has
2540 * been renounced. Ignore it.
2542 if (snp->smk_label == NULL)
2545 * we break after finding the first match because
2546 * the list is sorted from longest to shortest mask
2547 * so we have found the most specific match
2549 for (found = 1, i = 0; i < 8; i++) {
2550 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2551 snp->smk_host.s6_addr16[i]) {
2557 return snp->smk_label;
2562 #endif /* CONFIG_IPV6 */
2565 * smack_netlbl_add - Set the secattr on a socket
2568 * Attach the outbound smack value (smk_out) to the socket.
2570 * Returns 0 on success or an error code
2572 static int smack_netlbl_add(struct sock *sk)
2574 struct socket_smack *ssp = smack_sock(sk);
2575 struct smack_known *skp = ssp->smk_out;
2579 bh_lock_sock_nested(sk);
2581 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel,
2582 netlbl_sk_lock_check(sk));
2585 ssp->smk_state = SMK_NETLBL_LABELED;
2588 ssp->smk_state = SMK_NETLBL_REQSKB;
2600 * smack_netlbl_delete - Remove the secattr from a socket
2603 * Remove the outbound smack value from a socket
2605 static void smack_netlbl_delete(struct sock *sk)
2607 struct socket_smack *ssp = smack_sock(sk);
2610 * Take the label off the socket if one is set.
2612 if (ssp->smk_state != SMK_NETLBL_LABELED)
2616 bh_lock_sock_nested(sk);
2617 netlbl_sock_delattr(sk);
2620 ssp->smk_state = SMK_NETLBL_UNLABELED;
2624 * smk_ipv4_check - Perform IPv4 host access checks
2626 * @sap: the destination address
2628 * Set the correct secattr for the given socket based on the destination
2629 * address and perform any outbound access checks needed.
2631 * Returns 0 on success or an error code.
2634 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2636 struct smack_known *skp;
2638 struct smack_known *hkp;
2639 struct socket_smack *ssp = smack_sock(sk);
2640 struct smk_audit_info ad;
2643 hkp = smack_ipv4host_label(sap);
2646 struct lsm_network_audit net;
2648 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2649 ad.a.u.net->family = sap->sin_family;
2650 ad.a.u.net->dport = sap->sin_port;
2651 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2654 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2655 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2657 * Clear the socket netlabel if it's set.
2660 smack_netlbl_delete(sk);
2667 #if IS_ENABLED(CONFIG_IPV6)
2669 * smk_ipv6_check - check Smack access
2670 * @subject: subject Smack label
2671 * @object: object Smack label
2673 * @act: the action being taken
2675 * Check an IPv6 access
2677 static int smk_ipv6_check(struct smack_known *subject,
2678 struct smack_known *object,
2679 struct sockaddr_in6 *address, int act)
2682 struct lsm_network_audit net;
2684 struct smk_audit_info ad;
2688 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2689 ad.a.u.net->family = PF_INET6;
2690 ad.a.u.net->dport = address->sin6_port;
2691 if (act == SMK_RECEIVING)
2692 ad.a.u.net->v6info.saddr = address->sin6_addr;
2694 ad.a.u.net->v6info.daddr = address->sin6_addr;
2696 rc = smk_access(subject, object, MAY_WRITE, &ad);
2697 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2700 #endif /* CONFIG_IPV6 */
2702 #ifdef SMACK_IPV6_PORT_LABELING
2704 * smk_ipv6_port_label - Smack port access table management
2708 * Create or update the port list entry
2710 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2712 struct sock *sk = sock->sk;
2713 struct sockaddr_in6 *addr6;
2714 struct socket_smack *ssp = smack_sock(sock->sk);
2715 struct smk_port_label *spp;
2716 unsigned short port = 0;
2718 if (address == NULL) {
2720 * This operation is changing the Smack information
2721 * on the bound socket. Take the changes to the port
2725 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2726 if (sk != spp->smk_sock)
2728 spp->smk_in = ssp->smk_in;
2729 spp->smk_out = ssp->smk_out;
2734 * A NULL address is only used for updating existing
2735 * bound entries. If there isn't one, it's OK.
2741 addr6 = (struct sockaddr_in6 *)address;
2742 port = ntohs(addr6->sin6_port);
2744 * This is a special case that is safely ignored.
2750 * Look for an existing port list entry.
2751 * This is an indication that a port is getting reused.
2754 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2755 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2757 if (spp->smk_can_reuse != 1) {
2761 spp->smk_port = port;
2763 spp->smk_in = ssp->smk_in;
2764 spp->smk_out = ssp->smk_out;
2765 spp->smk_can_reuse = 0;
2771 * A new port entry is required.
2773 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2777 spp->smk_port = port;
2779 spp->smk_in = ssp->smk_in;
2780 spp->smk_out = ssp->smk_out;
2781 spp->smk_sock_type = sock->type;
2782 spp->smk_can_reuse = 0;
2784 mutex_lock(&smack_ipv6_lock);
2785 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2786 mutex_unlock(&smack_ipv6_lock);
2791 * smk_ipv6_port_check - check Smack port access
2794 * @act: the action being taken
2796 * Create or update the port list entry
2798 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2801 struct smk_port_label *spp;
2802 struct socket_smack *ssp = smack_sock(sk);
2803 struct smack_known *skp = NULL;
2804 unsigned short port;
2805 struct smack_known *object;
2807 if (act == SMK_RECEIVING) {
2808 skp = smack_ipv6host_label(address);
2809 object = ssp->smk_in;
2812 object = smack_ipv6host_label(address);
2816 * The other end is a single label host.
2818 if (skp != NULL && object != NULL)
2819 return smk_ipv6_check(skp, object, address, act);
2821 skp = smack_net_ambient;
2823 object = smack_net_ambient;
2826 * It's remote, so port lookup does no good.
2828 if (!smk_ipv6_localhost(address))
2829 return smk_ipv6_check(skp, object, address, act);
2832 * It's local so the send check has to have passed.
2834 if (act == SMK_RECEIVING)
2837 port = ntohs(address->sin6_port);
2839 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2840 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2842 object = spp->smk_in;
2843 if (act == SMK_CONNECTING)
2844 ssp->smk_packet = spp->smk_out;
2849 return smk_ipv6_check(skp, object, address, act);
2854 * smack_inode_setsecurity - set smack xattrs
2855 * @inode: the object
2856 * @name: attribute name
2857 * @value: attribute value
2858 * @size: size of the attribute
2861 * Sets the named attribute in the appropriate blob
2863 * Returns 0 on success, or an error code
2865 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2866 const void *value, size_t size, int flags)
2868 struct smack_known *skp;
2869 struct inode_smack *nsp = smack_inode(inode);
2870 struct socket_smack *ssp;
2871 struct socket *sock;
2874 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2877 if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) {
2878 if (!S_ISDIR(inode->i_mode) || size != TRANS_TRUE_SIZE ||
2879 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
2882 nsp->smk_flags |= SMK_INODE_TRANSMUTE;
2886 skp = smk_import_entry(value, size);
2888 return PTR_ERR(skp);
2890 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2891 nsp->smk_inode = skp;
2892 nsp->smk_flags |= SMK_INODE_INSTANT;
2896 * The rest of the Smack xattrs are only on sockets.
2898 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2901 sock = SOCKET_I(inode);
2902 if (sock == NULL || sock->sk == NULL)
2905 ssp = smack_sock(sock->sk);
2907 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2909 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2911 if (sock->sk->sk_family == PF_INET) {
2912 rc = smack_netlbl_add(sock->sk);
2915 "Smack: \"%s\" netlbl error %d.\n",
2921 #ifdef SMACK_IPV6_PORT_LABELING
2922 if (sock->sk->sk_family == PF_INET6)
2923 smk_ipv6_port_label(sock, NULL);
2930 * smack_socket_post_create - finish socket setup
2932 * @family: protocol family
2937 * Sets the netlabel information on the socket
2939 * Returns 0 on success, and error code otherwise
2941 static int smack_socket_post_create(struct socket *sock, int family,
2942 int type, int protocol, int kern)
2944 struct socket_smack *ssp;
2946 if (sock->sk == NULL)
2950 * Sockets created by kernel threads receive web label.
2952 if (unlikely(current->flags & PF_KTHREAD)) {
2953 ssp = smack_sock(sock->sk);
2954 ssp->smk_in = &smack_known_web;
2955 ssp->smk_out = &smack_known_web;
2958 if (family != PF_INET)
2961 * Set the outbound netlbl.
2963 return smack_netlbl_add(sock->sk);
2967 * smack_socket_socketpair - create socket pair
2968 * @socka: one socket
2969 * @sockb: another socket
2971 * Cross reference the peer labels for SO_PEERSEC
2975 static int smack_socket_socketpair(struct socket *socka,
2976 struct socket *sockb)
2978 struct socket_smack *asp = smack_sock(socka->sk);
2979 struct socket_smack *bsp = smack_sock(sockb->sk);
2981 asp->smk_packet = bsp->smk_out;
2982 bsp->smk_packet = asp->smk_out;
2987 #ifdef SMACK_IPV6_PORT_LABELING
2989 * smack_socket_bind - record port binding information.
2991 * @address: the port address
2992 * @addrlen: size of the address
2994 * Records the label bound to a port.
2996 * Returns 0 on success, and error code otherwise
2998 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
3001 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
3002 if (addrlen < SIN6_LEN_RFC2133 ||
3003 address->sa_family != AF_INET6)
3005 smk_ipv6_port_label(sock, address);
3009 #endif /* SMACK_IPV6_PORT_LABELING */
3012 * smack_socket_connect - connect access check
3014 * @sap: the other end
3015 * @addrlen: size of sap
3017 * Verifies that a connection may be possible
3019 * Returns 0 on success, and error code otherwise
3021 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
3026 if (sock->sk == NULL)
3028 if (sock->sk->sk_family != PF_INET &&
3029 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
3031 if (addrlen < offsetofend(struct sockaddr, sa_family))
3034 #if IS_ENABLED(CONFIG_IPV6)
3035 if (sap->sa_family == AF_INET6) {
3036 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
3037 struct smack_known *rsp = NULL;
3039 if (addrlen < SIN6_LEN_RFC2133)
3041 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
3042 rsp = smack_ipv6host_label(sip);
3044 struct socket_smack *ssp = smack_sock(sock->sk);
3046 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
3049 #ifdef SMACK_IPV6_PORT_LABELING
3050 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
3055 #endif /* CONFIG_IPV6 */
3057 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
3059 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
3064 * smack_flags_to_may - convert S_ to MAY_ values
3065 * @flags: the S_ value
3067 * Returns the equivalent MAY_ value
3069 static int smack_flags_to_may(int flags)
3073 if (flags & S_IRUGO)
3075 if (flags & S_IWUGO)
3077 if (flags & S_IXUGO)
3084 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
3089 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
3091 struct smack_known **blob = smack_msg_msg(msg);
3093 *blob = smk_of_current();
3098 * smack_of_ipc - the smack pointer for the ipc
3101 * Returns a pointer to the smack value
3103 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
3105 struct smack_known **blob = smack_ipc(isp);
3111 * smack_ipc_alloc_security - Set the security blob for ipc
3116 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
3118 struct smack_known **blob = smack_ipc(isp);
3120 *blob = smk_of_current();
3125 * smk_curacc_shm : check if current has access on shm
3127 * @access : access requested
3129 * Returns 0 if current has the requested access, error code otherwise
3131 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
3133 struct smack_known *ssp = smack_of_ipc(isp);
3134 struct smk_audit_info ad;
3138 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3139 ad.a.u.ipc_id = isp->id;
3141 rc = smk_curacc(ssp, access, &ad);
3142 rc = smk_bu_current("shm", ssp, access, rc);
3147 * smack_shm_associate - Smack access check for shm
3149 * @shmflg: access requested
3151 * Returns 0 if current has the requested access, error code otherwise
3153 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3157 may = smack_flags_to_may(shmflg);
3158 return smk_curacc_shm(isp, may);
3162 * smack_shm_shmctl - Smack access check for shm
3164 * @cmd: what it wants to do
3166 * Returns 0 if current has the requested access, error code otherwise
3168 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3182 may = MAY_READWRITE;
3187 * System level information.
3193 return smk_curacc_shm(isp, may);
3197 * smack_shm_shmat - Smack access for shmat
3200 * @shmflg: access requested
3202 * Returns 0 if current has the requested access, error code otherwise
3204 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3209 may = smack_flags_to_may(shmflg);
3210 return smk_curacc_shm(isp, may);
3214 * smk_curacc_sem : check if current has access on sem
3216 * @access : access requested
3218 * Returns 0 if current has the requested access, error code otherwise
3220 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3222 struct smack_known *ssp = smack_of_ipc(isp);
3223 struct smk_audit_info ad;
3227 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3228 ad.a.u.ipc_id = isp->id;
3230 rc = smk_curacc(ssp, access, &ad);
3231 rc = smk_bu_current("sem", ssp, access, rc);
3236 * smack_sem_associate - Smack access check for sem
3238 * @semflg: access requested
3240 * Returns 0 if current has the requested access, error code otherwise
3242 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3246 may = smack_flags_to_may(semflg);
3247 return smk_curacc_sem(isp, may);
3251 * smack_sem_semctl - Smack access check for sem
3253 * @cmd: what it wants to do
3255 * Returns 0 if current has the requested access, error code otherwise
3257 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3276 may = MAY_READWRITE;
3281 * System level information
3288 return smk_curacc_sem(isp, may);
3292 * smack_sem_semop - Smack checks of semaphore operations
3298 * Treated as read and write in all cases.
3300 * Returns 0 if access is allowed, error code otherwise
3302 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3303 unsigned nsops, int alter)
3305 return smk_curacc_sem(isp, MAY_READWRITE);
3309 * smk_curacc_msq : helper to check if current has access on msq
3311 * @access : access requested
3313 * return 0 if current has access, error otherwise
3315 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3317 struct smack_known *msp = smack_of_ipc(isp);
3318 struct smk_audit_info ad;
3322 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3323 ad.a.u.ipc_id = isp->id;
3325 rc = smk_curacc(msp, access, &ad);
3326 rc = smk_bu_current("msq", msp, access, rc);
3331 * smack_msg_queue_associate - Smack access check for msg_queue
3333 * @msqflg: access requested
3335 * Returns 0 if current has the requested access, error code otherwise
3337 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3341 may = smack_flags_to_may(msqflg);
3342 return smk_curacc_msq(isp, may);
3346 * smack_msg_queue_msgctl - Smack access check for msg_queue
3348 * @cmd: what it wants to do
3350 * Returns 0 if current has the requested access, error code otherwise
3352 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3364 may = MAY_READWRITE;
3369 * System level information
3376 return smk_curacc_msq(isp, may);
3380 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3383 * @msqflg: access requested
3385 * Returns 0 if current has the requested access, error code otherwise
3387 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3392 may = smack_flags_to_may(msqflg);
3393 return smk_curacc_msq(isp, may);
3397 * smack_msg_queue_msgrcv - Smack access check for msg_queue
3404 * Returns 0 if current has read and write access, error code otherwise
3406 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp,
3407 struct msg_msg *msg,
3408 struct task_struct *target, long type,
3411 return smk_curacc_msq(isp, MAY_READWRITE);
3415 * smack_ipc_permission - Smack access for ipc_permission()
3416 * @ipp: the object permissions
3417 * @flag: access requested
3419 * Returns 0 if current has read and write access, error code otherwise
3421 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3423 struct smack_known **blob = smack_ipc(ipp);
3424 struct smack_known *iskp = *blob;
3425 int may = smack_flags_to_may(flag);
3426 struct smk_audit_info ad;
3430 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3431 ad.a.u.ipc_id = ipp->id;
3433 rc = smk_curacc(iskp, may, &ad);
3434 rc = smk_bu_current("svipc", iskp, may, rc);
3439 * smack_ipc_getlsmprop - Extract smack security data
3440 * @ipp: the object permissions
3441 * @prop: where result will be saved
3443 static void smack_ipc_getlsmprop(struct kern_ipc_perm *ipp, struct lsm_prop *prop)
3445 struct smack_known **iskpp = smack_ipc(ipp);
3447 prop->smack.skp = *iskpp;
3451 * smack_d_instantiate - Make sure the blob is correct on an inode
3452 * @opt_dentry: dentry where inode will be attached
3453 * @inode: the object
3455 * Set the inode's security blob if it hasn't been done already.
3457 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3459 struct super_block *sbp;
3460 struct superblock_smack *sbsp;
3461 struct inode_smack *isp;
3462 struct smack_known *skp;
3463 struct smack_known *ckp = smk_of_current();
3464 struct smack_known *final;
3465 char trattr[TRANS_TRUE_SIZE];
3473 isp = smack_inode(inode);
3476 * If the inode is already instantiated
3477 * take the quick way out
3479 if (isp->smk_flags & SMK_INODE_INSTANT)
3483 sbsp = smack_superblock(sbp);
3485 * We're going to use the superblock default label
3486 * if there's no label on the file.
3488 final = sbsp->smk_default;
3491 * If this is the root inode the superblock
3492 * may be in the process of initialization.
3493 * If that is the case use the root value out
3494 * of the superblock.
3496 if (opt_dentry->d_parent == opt_dentry) {
3497 switch (sbp->s_magic) {
3498 case CGROUP_SUPER_MAGIC:
3499 case CGROUP2_SUPER_MAGIC:
3501 * The cgroup filesystem is never mounted,
3502 * so there's no opportunity to set the mount
3505 sbsp->smk_root = &smack_known_star;
3506 sbsp->smk_default = &smack_known_star;
3507 isp->smk_inode = sbsp->smk_root;
3511 * What about shmem/tmpfs anonymous files with dentry
3512 * obtained from d_alloc_pseudo()?
3514 isp->smk_inode = smk_of_current();
3517 isp->smk_inode = smk_of_current();
3521 * Socket access is controlled by the socket
3522 * structures associated with the task involved.
3524 isp->smk_inode = &smack_known_star;
3527 isp->smk_inode = sbsp->smk_root;
3530 isp->smk_flags |= SMK_INODE_INSTANT;
3535 * This is pretty hackish.
3536 * Casey says that we shouldn't have to do
3537 * file system specific code, but it does help
3538 * with keeping it simple.
3540 switch (sbp->s_magic) {
3542 case CGROUP_SUPER_MAGIC:
3543 case CGROUP2_SUPER_MAGIC:
3545 * Casey says that it's a little embarrassing
3546 * that the smack file system doesn't do
3547 * extended attributes.
3549 * Cgroupfs is special
3551 final = &smack_known_star;
3553 case DEVPTS_SUPER_MAGIC:
3555 * devpts seems content with the label of the task.
3556 * Programs that change smack have to treat the
3561 case PROC_SUPER_MAGIC:
3563 * Casey says procfs appears not to care.
3564 * The superblock default suffices.
3569 * Device labels should come from the filesystem,
3570 * but watch out, because they're volitile,
3571 * getting recreated on every reboot.
3573 final = &smack_known_star;
3575 * If a smack value has been set we want to use it,
3576 * but since tmpfs isn't giving us the opportunity
3577 * to set mount options simulate setting the
3578 * superblock default.
3583 * This isn't an understood special case.
3584 * Get the value from the xattr.
3588 * UNIX domain sockets use lower level socket data.
3590 if (S_ISSOCK(inode->i_mode)) {
3591 final = &smack_known_star;
3595 * No xattr support means, alas, no SMACK label.
3596 * Use the aforeapplied default.
3597 * It would be curious if the label of the task
3598 * does not match that assigned.
3600 if (!(inode->i_opflags & IOP_XATTR))
3603 * Get the dentry for xattr.
3605 dp = dget(opt_dentry);
3606 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3607 if (!IS_ERR_OR_NULL(skp))
3611 * Transmuting directory
3613 if (S_ISDIR(inode->i_mode)) {
3615 * If this is a new directory and the label was
3616 * transmuted when the inode was initialized
3617 * set the transmute attribute on the directory
3618 * and mark the inode.
3620 * If there is a transmute attribute on the
3621 * directory mark the inode.
3623 rc = __vfs_getxattr(dp, inode,
3624 XATTR_NAME_SMACKTRANSMUTE, trattr,
3626 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3627 TRANS_TRUE_SIZE) != 0)
3630 transflag = SMK_INODE_TRANSMUTE;
3633 * Don't let the exec or mmap label be "*" or "@".
3635 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3636 if (IS_ERR(skp) || skp == &smack_known_star ||
3637 skp == &smack_known_web)
3639 isp->smk_task = skp;
3641 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3642 if (IS_ERR(skp) || skp == &smack_known_star ||
3643 skp == &smack_known_web)
3645 isp->smk_mmap = skp;
3652 isp->smk_inode = ckp;
3654 isp->smk_inode = final;
3656 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3662 * smack_getselfattr - Smack current process attribute
3663 * @attr: which attribute to fetch
3664 * @ctx: buffer to receive the result
3665 * @size: available size in, actual size out
3668 * Fill the passed user space @ctx with the details of the requested
3671 * Returns the number of attributes on success, an error code otherwise.
3672 * There will only ever be one attribute.
3674 static int smack_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx,
3675 u32 *size, u32 flags)
3678 struct smack_known *skp;
3680 if (attr != LSM_ATTR_CURRENT)
3683 skp = smk_of_current();
3684 rc = lsm_fill_user_ctx(ctx, size,
3685 skp->smk_known, strlen(skp->smk_known) + 1,
3687 return (!rc ? 1 : rc);
3691 * smack_getprocattr - Smack process attribute access
3692 * @p: the object task
3693 * @name: the name of the attribute in /proc/.../attr
3694 * @value: where to put the result
3696 * Places a copy of the task Smack into value
3698 * Returns the length of the smack label or an error code
3700 static int smack_getprocattr(struct task_struct *p, const char *name, char **value)
3702 struct smack_known *skp = smk_of_task_struct_obj(p);
3706 if (strcmp(name, "current") != 0)
3709 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3719 * do_setattr - Smack process attribute setting
3720 * @attr: the ID of the attribute
3721 * @value: the value to set
3722 * @size: the size of the value
3724 * Sets the Smack value of the task. Only setting self
3725 * is permitted and only with privilege
3727 * Returns the length of the smack label or an error code
3729 static int do_setattr(u64 attr, void *value, size_t size)
3731 struct task_smack *tsp = smack_cred(current_cred());
3733 struct smack_known *skp;
3734 struct smack_known_list_elem *sklep;
3737 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3740 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3743 if (attr != LSM_ATTR_CURRENT)
3746 skp = smk_import_entry(value, size);
3748 return PTR_ERR(skp);
3751 * No process is ever allowed the web ("@") label
3752 * and the star ("*") label.
3754 if (skp == &smack_known_web || skp == &smack_known_star)
3757 if (!smack_privileged(CAP_MAC_ADMIN)) {
3759 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3760 if (sklep->smk_label == skp) {
3768 new = prepare_creds();
3772 tsp = smack_cred(new);
3773 tsp->smk_task = skp;
3775 * process can change its label only once
3777 smk_destroy_label_list(&tsp->smk_relabel);
3784 * smack_setselfattr - Set a Smack process attribute
3785 * @attr: which attribute to set
3786 * @ctx: buffer containing the data
3787 * @size: size of @ctx
3790 * Fill the passed user space @ctx with the details of the requested
3793 * Returns 0 on success, an error code otherwise.
3795 static int smack_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
3796 u32 size, u32 flags)
3800 rc = do_setattr(attr, ctx->ctx, ctx->ctx_len);
3807 * smack_setprocattr - Smack process attribute setting
3808 * @name: the name of the attribute in /proc/.../attr
3809 * @value: the value to set
3810 * @size: the size of the value
3812 * Sets the Smack value of the task. Only setting self
3813 * is permitted and only with privilege
3815 * Returns the length of the smack label or an error code
3817 static int smack_setprocattr(const char *name, void *value, size_t size)
3819 int attr = lsm_name_to_attr(name);
3821 if (attr != LSM_ATTR_UNDEF)
3822 return do_setattr(attr, value, size);
3827 * smack_unix_stream_connect - Smack access on UDS
3829 * @other: the other sock
3832 * Return 0 if a subject with the smack of sock could access
3833 * an object with the smack of other, otherwise an error code
3835 static int smack_unix_stream_connect(struct sock *sock,
3836 struct sock *other, struct sock *newsk)
3838 struct smack_known *skp;
3839 struct smack_known *okp;
3840 struct socket_smack *ssp = smack_sock(sock);
3841 struct socket_smack *osp = smack_sock(other);
3842 struct socket_smack *nsp = smack_sock(newsk);
3843 struct smk_audit_info ad;
3846 struct lsm_network_audit net;
3849 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3853 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3854 smk_ad_setfield_u_net_sk(&ad, other);
3856 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3857 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3861 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3862 rc = smk_bu_note("UDS connect", okp, skp,
3869 * Cross reference the peer labels for SO_PEERSEC.
3871 nsp->smk_packet = ssp->smk_out;
3872 ssp->smk_packet = osp->smk_out;
3875 * new/child/established socket must inherit listening socket labels
3877 nsp->smk_out = osp->smk_out;
3878 nsp->smk_in = osp->smk_in;
3885 * smack_unix_may_send - Smack access on UDS
3887 * @other: the other socket
3889 * Return 0 if a subject with the smack of sock could access
3890 * an object with the smack of other, otherwise an error code
3892 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3894 struct socket_smack *ssp = smack_sock(sock->sk);
3895 struct socket_smack *osp = smack_sock(other->sk);
3896 struct smk_audit_info ad;
3900 struct lsm_network_audit net;
3902 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3903 smk_ad_setfield_u_net_sk(&ad, other->sk);
3906 if (smack_privileged(CAP_MAC_OVERRIDE))
3909 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3910 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3915 * smack_socket_sendmsg - Smack check based on destination host
3918 * @size: the size of the message
3920 * Return 0 if the current subject can write to the destination host.
3921 * For IPv4 this is only a question if the destination is a single label host.
3922 * For IPv6 this is a check against the label of the port.
3924 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3927 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3928 #if IS_ENABLED(CONFIG_IPV6)
3929 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3931 #ifdef SMACK_IPV6_SECMARK_LABELING
3932 struct socket_smack *ssp = smack_sock(sock->sk);
3933 struct smack_known *rsp;
3938 * Perfectly reasonable for this to be NULL
3943 switch (sock->sk->sk_family) {
3945 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3946 sip->sin_family != AF_INET)
3948 rc = smk_ipv4_check(sock->sk, sip);
3950 #if IS_ENABLED(CONFIG_IPV6)
3952 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3953 sap->sin6_family != AF_INET6)
3955 #ifdef SMACK_IPV6_SECMARK_LABELING
3956 rsp = smack_ipv6host_label(sap);
3958 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3961 #ifdef SMACK_IPV6_PORT_LABELING
3962 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3964 #endif /* IS_ENABLED(CONFIG_IPV6) */
3971 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3972 * @sap: netlabel secattr
3973 * @ssp: socket security information
3975 * Returns a pointer to a Smack label entry found on the label list.
3977 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3978 struct socket_smack *ssp)
3980 struct smack_known *skp;
3986 * Netlabel found it in the cache.
3988 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3989 return (struct smack_known *)sap->cache->data;
3991 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3993 * Looks like a fallback, which gives us a secid.
3995 return smack_from_secid(sap->attr.secid);
3997 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3999 * Looks like a CIPSO packet.
4000 * If there are flags but no level netlabel isn't
4001 * behaving the way we expect it to.
4003 * Look it up in the label table
4004 * Without guidance regarding the smack value
4005 * for the packet fall back on the network
4009 list_for_each_entry_rcu(skp, &smack_known_list, list) {
4010 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
4013 * Compare the catsets. Use the netlbl APIs.
4015 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
4016 if ((skp->smk_netlabel.flags &
4017 NETLBL_SECATTR_MLS_CAT) == 0)
4021 for (acat = -1, kcat = -1; acat == kcat; ) {
4022 acat = netlbl_catmap_walk(sap->attr.mls.cat,
4024 kcat = netlbl_catmap_walk(
4025 skp->smk_netlabel.attr.mls.cat,
4027 if (acat < 0 || kcat < 0)
4040 if (ssp != NULL && ssp->smk_in == &smack_known_star)
4041 return &smack_known_web;
4042 return &smack_known_star;
4045 * Without guidance regarding the smack value
4046 * for the packet fall back on the network
4049 return smack_net_ambient;
4052 #if IS_ENABLED(CONFIG_IPV6)
4053 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
4057 int proto = -EINVAL;
4058 struct ipv6hdr _ipv6h;
4059 struct ipv6hdr *ip6;
4061 struct tcphdr _tcph, *th;
4062 struct udphdr _udph, *uh;
4066 offset = skb_network_offset(skb);
4067 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4070 sip->sin6_addr = ip6->saddr;
4072 nexthdr = ip6->nexthdr;
4073 offset += sizeof(_ipv6h);
4074 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
4081 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4083 sip->sin6_port = th->source;
4086 case IPPROTO_UDPLITE:
4087 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4089 sip->sin6_port = uh->source;
4094 #endif /* CONFIG_IPV6 */
4097 * smack_from_skb - Smack data from the secmark in an skb
4100 * Returns smack_known of the secmark or NULL if that won't work.
4102 #ifdef CONFIG_NETWORK_SECMARK
4103 static struct smack_known *smack_from_skb(struct sk_buff *skb)
4105 if (skb == NULL || skb->secmark == 0)
4108 return smack_from_secid(skb->secmark);
4111 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
4118 * smack_from_netlbl - Smack data from the IP options in an skb
4119 * @sk: socket data came in on
4120 * @family: address family
4123 * Find the Smack label in the IP options. If it hasn't been
4124 * added to the netlabel cache, add it here.
4126 * Returns smack_known of the IP options or NULL if that won't work.
4128 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
4129 struct sk_buff *skb)
4131 struct netlbl_lsm_secattr secattr;
4132 struct socket_smack *ssp = NULL;
4133 struct smack_known *skp = NULL;
4135 netlbl_secattr_init(&secattr);
4138 ssp = smack_sock(sk);
4140 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
4141 skp = smack_from_secattr(&secattr, ssp);
4142 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
4143 netlbl_cache_add(skb, family, &skp->smk_netlabel);
4146 netlbl_secattr_destroy(&secattr);
4152 * smack_socket_sock_rcv_skb - Smack packet delivery access check
4156 * Returns 0 if the packet should be delivered, an error code otherwise
4158 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4160 struct socket_smack *ssp = smack_sock(sk);
4161 struct smack_known *skp = NULL;
4163 struct smk_audit_info ad;
4164 u16 family = sk->sk_family;
4166 struct lsm_network_audit net;
4168 #if IS_ENABLED(CONFIG_IPV6)
4169 struct sockaddr_in6 sadd;
4172 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4174 #endif /* CONFIG_IPV6 */
4179 * If there is a secmark use it rather than the CIPSO label.
4180 * If there is no secmark fall back to CIPSO.
4181 * The secmark is assumed to reflect policy better.
4183 skp = smack_from_skb(skb);
4185 skp = smack_from_netlbl(sk, family, skb);
4187 skp = smack_net_ambient;
4191 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4192 ad.a.u.net->family = family;
4193 ad.a.u.net->netif = skb->skb_iif;
4194 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4197 * Receiving a packet requires that the other end
4198 * be able to write here. Read access is not required.
4199 * This is the simplest possible security model
4202 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4203 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
4206 netlbl_skbuff_err(skb, family, rc, 0);
4208 #if IS_ENABLED(CONFIG_IPV6)
4210 proto = smk_skb_to_addr_ipv6(skb, &sadd);
4211 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
4212 proto != IPPROTO_TCP)
4214 #ifdef SMACK_IPV6_SECMARK_LABELING
4215 skp = smack_from_skb(skb);
4217 if (smk_ipv6_localhost(&sadd))
4219 skp = smack_ipv6host_label(&sadd);
4221 skp = smack_net_ambient;
4224 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4225 ad.a.u.net->family = family;
4226 ad.a.u.net->netif = skb->skb_iif;
4227 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4228 #endif /* CONFIG_AUDIT */
4229 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4230 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4232 #endif /* SMACK_IPV6_SECMARK_LABELING */
4233 #ifdef SMACK_IPV6_PORT_LABELING
4234 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
4235 #endif /* SMACK_IPV6_PORT_LABELING */
4237 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4238 ICMPV6_ADM_PROHIBITED, 0);
4240 #endif /* CONFIG_IPV6 */
4247 * smack_socket_getpeersec_stream - pull in packet label
4249 * @optval: user's destination
4250 * @optlen: size thereof
4253 * returns zero on success, an error code otherwise
4255 static int smack_socket_getpeersec_stream(struct socket *sock,
4256 sockptr_t optval, sockptr_t optlen,
4259 struct socket_smack *ssp;
4264 ssp = smack_sock(sock->sk);
4265 if (ssp->smk_packet != NULL) {
4266 rcp = ssp->smk_packet->smk_known;
4267 slen = strlen(rcp) + 1;
4274 if (copy_to_sockptr(optval, rcp, slen))
4277 if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
4284 * smack_socket_getpeersec_dgram - pull in packet label
4285 * @sock: the peer socket
4287 * @secid: pointer to where to put the secid of the packet
4289 * Sets the netlabel socket state on sk from parent
4291 static int smack_socket_getpeersec_dgram(struct socket *sock,
4292 struct sk_buff *skb, u32 *secid)
4295 struct socket_smack *ssp = NULL;
4296 struct smack_known *skp;
4297 struct sock *sk = NULL;
4298 int family = PF_UNSPEC;
4299 u32 s = 0; /* 0 is the invalid secid */
4302 if (skb->protocol == htons(ETH_P_IP))
4304 #if IS_ENABLED(CONFIG_IPV6)
4305 else if (skb->protocol == htons(ETH_P_IPV6))
4307 #endif /* CONFIG_IPV6 */
4309 if (family == PF_UNSPEC && sock != NULL)
4310 family = sock->sk->sk_family;
4314 ssp = smack_sock(sock->sk);
4315 s = ssp->smk_out->smk_secid;
4318 skp = smack_from_skb(skb);
4324 * Translate what netlabel gave us.
4328 skp = smack_from_netlbl(sk, family, skb);
4333 #ifdef SMACK_IPV6_SECMARK_LABELING
4334 skp = smack_from_skb(skb);
4347 * smack_inet_conn_request - Smack access check on connect
4348 * @sk: socket involved
4352 * Returns 0 if a task with the packet label could write to
4353 * the socket, otherwise an error code
4355 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4356 struct request_sock *req)
4358 u16 family = sk->sk_family;
4359 struct smack_known *skp;
4360 struct socket_smack *ssp = smack_sock(sk);
4361 struct sockaddr_in addr;
4363 struct smack_known *hskp;
4365 struct smk_audit_info ad;
4367 struct lsm_network_audit net;
4370 #if IS_ENABLED(CONFIG_IPV6)
4371 if (family == PF_INET6) {
4373 * Handle mapped IPv4 packets arriving
4374 * via IPv6 sockets. Don't set up netlabel
4375 * processing on IPv6.
4377 if (skb->protocol == htons(ETH_P_IP))
4382 #endif /* CONFIG_IPV6 */
4385 * If there is a secmark use it rather than the CIPSO label.
4386 * If there is no secmark fall back to CIPSO.
4387 * The secmark is assumed to reflect policy better.
4389 skp = smack_from_skb(skb);
4391 skp = smack_from_netlbl(sk, family, skb);
4393 skp = &smack_known_huh;
4397 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4398 ad.a.u.net->family = family;
4399 ad.a.u.net->netif = skb->skb_iif;
4400 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4403 * Receiving a packet requires that the other end be able to write
4404 * here. Read access is not required.
4406 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4407 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4412 * Save the peer's label in the request_sock so we can later setup
4413 * smk_packet in the child socket so that SO_PEERCRED can report it.
4415 req->peer_secid = skp->smk_secid;
4418 * We need to decide if we want to label the incoming connection here
4419 * if we do we only need to label the request_sock and the stack will
4420 * propagate the wire-label to the sock when it is created.
4423 addr.sin_addr.s_addr = hdr->saddr;
4425 hskp = smack_ipv4host_label(&addr);
4429 rc = netlbl_req_setattr(req, &ssp->smk_out->smk_netlabel);
4431 netlbl_req_delattr(req);
4437 * smack_inet_csk_clone - Copy the connection information to the new socket
4438 * @sk: the new socket
4439 * @req: the connection's request_sock
4441 * Transfer the connection's peer label to the newly created socket.
4443 static void smack_inet_csk_clone(struct sock *sk,
4444 const struct request_sock *req)
4446 struct socket_smack *ssp = smack_sock(sk);
4447 struct smack_known *skp;
4449 if (req->peer_secid != 0) {
4450 skp = smack_from_secid(req->peer_secid);
4451 ssp->smk_packet = skp;
4453 ssp->smk_packet = NULL;
4457 * Key management security hooks
4459 * Casey has not tested key support very heavily.
4460 * The permission check is most likely too restrictive.
4461 * If you care about keys please have a look.
4466 * smack_key_alloc - Set the key security blob
4468 * @cred: the credentials to use
4471 * No allocation required
4475 static int smack_key_alloc(struct key *key, const struct cred *cred,
4476 unsigned long flags)
4478 struct smack_known **blob = smack_key(key);
4479 struct smack_known *skp = smk_of_task(smack_cred(cred));
4486 * smack_key_permission - Smack access on a key
4487 * @key_ref: gets to the object
4488 * @cred: the credentials to use
4489 * @need_perm: requested key permission
4491 * Return 0 if the task has read and write to the object,
4492 * an error code otherwise
4494 static int smack_key_permission(key_ref_t key_ref,
4495 const struct cred *cred,
4496 enum key_need_perm need_perm)
4498 struct smack_known **blob;
4499 struct smack_known *skp;
4501 struct smk_audit_info ad;
4502 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4507 * Validate requested permissions
4509 switch (need_perm) {
4511 case KEY_NEED_SEARCH:
4513 request |= MAY_READ;
4515 case KEY_NEED_WRITE:
4517 case KEY_NEED_SETATTR:
4518 request |= MAY_WRITE;
4520 case KEY_NEED_UNSPECIFIED:
4521 case KEY_NEED_UNLINK:
4522 case KEY_SYSADMIN_OVERRIDE:
4523 case KEY_AUTHTOKEN_OVERRIDE:
4524 case KEY_DEFER_PERM_CHECK:
4530 keyp = key_ref_to_ptr(key_ref);
4534 * If the key hasn't been initialized give it access so that
4537 blob = smack_key(keyp);
4542 * This should not occur
4547 if (smack_privileged(CAP_MAC_OVERRIDE))
4551 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4552 ad.a.u.key_struct.key = keyp->serial;
4553 ad.a.u.key_struct.key_desc = keyp->description;
4555 rc = smk_access(tkp, skp, request, &ad);
4556 rc = smk_bu_note("key access", tkp, skp, request, rc);
4561 * smack_key_getsecurity - Smack label tagging the key
4562 * @key points to the key to be queried
4563 * @_buffer points to a pointer that should be set to point to the
4564 * resulting string (if no label or an error occurs).
4565 * Return the length of the string (including terminating NUL) or -ve if
4567 * May also return 0 (and a NULL buffer pointer) if there is no label.
4569 static int smack_key_getsecurity(struct key *key, char **_buffer)
4571 struct smack_known **blob = smack_key(key);
4572 struct smack_known *skp = *blob;
4581 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4584 length = strlen(copy) + 1;
4591 #ifdef CONFIG_KEY_NOTIFICATIONS
4593 * smack_watch_key - Smack access to watch a key for notifications.
4594 * @key: The key to be watched
4596 * Return 0 if the @watch->cred has permission to read from the key object and
4597 * an error otherwise.
4599 static int smack_watch_key(struct key *key)
4601 struct smk_audit_info ad;
4602 struct smack_known *tkp = smk_of_current();
4603 struct smack_known **blob = smack_key(key);
4607 * This should not occur
4612 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4616 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4617 ad.a.u.key_struct.key = key->serial;
4618 ad.a.u.key_struct.key_desc = key->description;
4620 rc = smk_access(tkp, *blob, MAY_READ, &ad);
4621 rc = smk_bu_note("key watch", tkp, *blob, MAY_READ, rc);
4624 #endif /* CONFIG_KEY_NOTIFICATIONS */
4625 #endif /* CONFIG_KEYS */
4627 #ifdef CONFIG_WATCH_QUEUE
4629 * smack_post_notification - Smack access to post a notification to a queue
4630 * @w_cred: The credentials of the watcher.
4631 * @cred: The credentials of the event source (may be NULL).
4632 * @n: The notification message to be posted.
4634 static int smack_post_notification(const struct cred *w_cred,
4635 const struct cred *cred,
4636 struct watch_notification *n)
4638 struct smk_audit_info ad;
4639 struct smack_known *subj, *obj;
4642 /* Always let maintenance notifications through. */
4643 if (n->type == WATCH_TYPE_META)
4648 subj = smk_of_task(smack_cred(cred));
4649 obj = smk_of_task(smack_cred(w_cred));
4651 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4652 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4653 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4656 #endif /* CONFIG_WATCH_QUEUE */
4661 * Audit requires a unique representation of each Smack specific
4662 * rule. This unique representation is used to distinguish the
4663 * object to be audited from remaining kernel objects and also
4664 * works as a glue between the audit hooks.
4666 * Since repository entries are added but never deleted, we'll use
4667 * the smack_known label address related to the given audit rule as
4668 * the needed unique representation. This also better fits the smack
4669 * model where nearly everything is a label.
4674 * smack_audit_rule_init - Initialize a smack audit rule
4675 * @field: audit rule fields given from user-space (audit.h)
4676 * @op: required testing operator (=, !=, >, <, ...)
4677 * @rulestr: smack label to be audited
4678 * @vrule: pointer to save our own audit rule representation
4679 * @gfp: type of the memory for the allocation
4681 * Prepare to audit cases where (@field @op @rulestr) is true.
4682 * The label to be audited is created if necessary.
4684 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule,
4687 struct smack_known *skp;
4688 char **rule = (char **)vrule;
4691 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4694 if (op != Audit_equal && op != Audit_not_equal)
4697 skp = smk_import_entry(rulestr, 0);
4699 return PTR_ERR(skp);
4701 *rule = skp->smk_known;
4707 * smack_audit_rule_known - Distinguish Smack audit rules
4708 * @krule: rule of interest, in Audit kernel representation format
4710 * This is used to filter Smack rules from remaining Audit ones.
4711 * If it's proved that this rule belongs to us, the
4712 * audit_rule_match hook will be called to do the final judgement.
4714 static int smack_audit_rule_known(struct audit_krule *krule)
4716 struct audit_field *f;
4719 for (i = 0; i < krule->field_count; i++) {
4720 f = &krule->fields[i];
4722 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4730 * smack_audit_rule_match - Audit given object ?
4731 * @prop: security id for identifying the object to test
4732 * @field: audit rule flags given from user-space
4733 * @op: required testing operator
4734 * @vrule: smack internal rule presentation
4736 * The core Audit hook. It's used to take the decision of
4737 * whether to audit or not to audit a given object.
4739 static int smack_audit_rule_match(struct lsm_prop *prop, u32 field, u32 op,
4742 struct smack_known *skp = prop->smack.skp;
4745 if (unlikely(!rule)) {
4746 WARN_ONCE(1, "Smack: missing rule\n");
4750 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4754 * No need to do string comparisons. If a match occurs,
4755 * both pointers will point to the same smack_known
4758 if (op == Audit_equal)
4759 return (rule == skp->smk_known);
4760 if (op == Audit_not_equal)
4761 return (rule != skp->smk_known);
4767 * There is no need for a smack_audit_rule_free hook.
4768 * No memory was allocated.
4771 #endif /* CONFIG_AUDIT */
4774 * smack_ismaclabel - check if xattr @name references a smack MAC label
4775 * @name: Full xattr name to check.
4777 static int smack_ismaclabel(const char *name)
4779 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4783 * smack_to_secctx - fill a lsm_context
4787 * Fill the passed @cp and return the length of the string
4789 static int smack_to_secctx(struct smack_known *skp, struct lsm_context *cp)
4791 int len = strlen(skp->smk_known);
4794 cp->context = skp->smk_known;
4796 cp->id = LSM_ID_SMACK;
4802 * smack_secid_to_secctx - return the smack label for a secid
4803 * @secid: incoming integer
4806 * Exists for networking code.
4808 static int smack_secid_to_secctx(u32 secid, struct lsm_context *cp)
4810 return smack_to_secctx(smack_from_secid(secid), cp);
4814 * smack_lsmprop_to_secctx - return the smack label
4815 * @prop: includes incoming Smack data
4818 * Exists for audit code.
4820 static int smack_lsmprop_to_secctx(struct lsm_prop *prop,
4821 struct lsm_context *cp)
4823 return smack_to_secctx(prop->smack.skp, cp);
4827 * smack_secctx_to_secid - return the secid for a smack label
4828 * @secdata: smack label
4829 * @seclen: how long result is
4830 * @secid: outgoing integer
4832 * Exists for audit and networking code.
4834 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4836 struct smack_known *skp = smk_find_entry(secdata);
4839 *secid = skp->smk_secid;
4846 * There used to be a smack_release_secctx hook
4847 * that did nothing back when hooks were in a vector.
4848 * Now that there's a list such a hook adds cost.
4851 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4853 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4857 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4859 return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
4860 ctx, ctxlen, 0, NULL);
4863 static int smack_inode_getsecctx(struct inode *inode, struct lsm_context *cp)
4865 struct smack_known *skp = smk_of_inode(inode);
4867 cp->context = skp->smk_known;
4868 cp->len = strlen(skp->smk_known);
4869 cp->id = LSM_ID_SMACK;
4873 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4876 struct task_smack *tsp;
4877 struct smack_known *skp;
4878 struct inode_smack *isp;
4879 struct cred *new_creds = *new;
4881 if (new_creds == NULL) {
4882 new_creds = prepare_creds();
4883 if (new_creds == NULL)
4887 tsp = smack_cred(new_creds);
4890 * Get label from overlay inode and set it in create_sid
4892 isp = smack_inode(d_inode(dentry));
4893 skp = isp->smk_inode;
4894 tsp->smk_task = skp;
4899 static int smack_inode_copy_up_xattr(struct dentry *src, const char *name)
4902 * Return -ECANCELED if this is the smack access Smack attribute.
4904 if (!strcmp(name, XATTR_NAME_SMACK))
4910 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4912 const struct cred *old,
4915 struct task_smack *otsp = smack_cred(old);
4916 struct task_smack *ntsp = smack_cred(new);
4917 struct inode_smack *isp;
4921 * Use the process credential unless all of
4922 * the transmuting criteria are met
4924 ntsp->smk_task = otsp->smk_task;
4927 * the attribute of the containing directory
4929 isp = smack_inode(d_inode(dentry->d_parent));
4931 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4933 may = smk_access_entry(otsp->smk_task->smk_known,
4934 isp->smk_inode->smk_known,
4935 &otsp->smk_task->smk_rules);
4939 * If the directory is transmuting and the rule
4940 * providing access is transmuting use the containing
4941 * directory label instead of the process label.
4943 if (may > 0 && (may & MAY_TRANSMUTE)) {
4944 ntsp->smk_task = isp->smk_inode;
4945 ntsp->smk_transmuted = ntsp->smk_task;
4951 #ifdef CONFIG_IO_URING
4953 * smack_uring_override_creds - Is io_uring cred override allowed?
4954 * @new: the target creds
4956 * Check to see if the current task is allowed to override it's credentials
4957 * to service an io_uring operation.
4959 static int smack_uring_override_creds(const struct cred *new)
4961 struct task_smack *tsp = smack_cred(current_cred());
4962 struct task_smack *nsp = smack_cred(new);
4965 * Allow the degenerate case where the new Smack value is
4966 * the same as the current Smack value.
4968 if (tsp->smk_task == nsp->smk_task)
4971 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4978 * smack_uring_sqpoll - check if a io_uring polling thread can be created
4980 * Check to see if the current task is allowed to create a new io_uring
4981 * kernel polling thread.
4983 static int smack_uring_sqpoll(void)
4985 if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred()))
4992 * smack_uring_cmd - check on file operations for io_uring
4993 * @ioucmd: the command in question
4995 * Make a best guess about whether a io_uring "command" should
4996 * be allowed. Use the same logic used for determining if the
4997 * file could be opened for read in the absence of better criteria.
4999 static int smack_uring_cmd(struct io_uring_cmd *ioucmd)
5001 struct file *file = ioucmd->file;
5002 struct smk_audit_info ad;
5003 struct task_smack *tsp;
5004 struct inode *inode;
5010 tsp = smack_cred(file->f_cred);
5011 inode = file_inode(file);
5013 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
5014 smk_ad_setfield_u_fs_path(&ad, file->f_path);
5015 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
5016 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
5021 #endif /* CONFIG_IO_URING */
5023 struct lsm_blob_sizes smack_blob_sizes __ro_after_init = {
5024 .lbs_cred = sizeof(struct task_smack),
5025 .lbs_file = sizeof(struct smack_known *),
5026 .lbs_inode = sizeof(struct inode_smack),
5027 .lbs_ipc = sizeof(struct smack_known *),
5028 .lbs_key = sizeof(struct smack_known *),
5029 .lbs_msg_msg = sizeof(struct smack_known *),
5030 .lbs_sock = sizeof(struct socket_smack),
5031 .lbs_superblock = sizeof(struct superblock_smack),
5032 .lbs_xattr_count = SMACK_INODE_INIT_XATTRS,
5035 static const struct lsm_id smack_lsmid = {
5040 static struct security_hook_list smack_hooks[] __ro_after_init = {
5041 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
5042 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
5043 LSM_HOOK_INIT(syslog, smack_syslog),
5045 LSM_HOOK_INIT(fs_context_submount, smack_fs_context_submount),
5046 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
5047 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
5049 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
5050 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
5051 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
5052 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
5053 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
5055 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
5057 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
5058 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
5059 LSM_HOOK_INIT(inode_link, smack_inode_link),
5060 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
5061 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
5062 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
5063 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
5064 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
5065 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
5066 LSM_HOOK_INIT(inode_xattr_skipcap, smack_inode_xattr_skipcap),
5067 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
5068 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
5069 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
5070 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
5071 LSM_HOOK_INIT(inode_set_acl, smack_inode_set_acl),
5072 LSM_HOOK_INIT(inode_get_acl, smack_inode_get_acl),
5073 LSM_HOOK_INIT(inode_remove_acl, smack_inode_remove_acl),
5074 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
5075 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
5076 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
5077 LSM_HOOK_INIT(inode_getlsmprop, smack_inode_getlsmprop),
5079 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
5080 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
5081 LSM_HOOK_INIT(file_ioctl_compat, smack_file_ioctl),
5082 LSM_HOOK_INIT(file_lock, smack_file_lock),
5083 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
5084 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
5085 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
5086 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
5087 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
5088 LSM_HOOK_INIT(file_receive, smack_file_receive),
5090 LSM_HOOK_INIT(file_open, smack_file_open),
5092 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
5093 LSM_HOOK_INIT(cred_free, smack_cred_free),
5094 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
5095 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
5096 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
5097 LSM_HOOK_INIT(cred_getlsmprop, smack_cred_getlsmprop),
5098 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
5099 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
5100 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
5101 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
5102 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
5103 LSM_HOOK_INIT(current_getlsmprop_subj, smack_current_getlsmprop_subj),
5104 LSM_HOOK_INIT(task_getlsmprop_obj, smack_task_getlsmprop_obj),
5105 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
5106 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
5107 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
5108 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
5109 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
5110 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
5111 LSM_HOOK_INIT(task_kill, smack_task_kill),
5112 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
5114 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
5115 LSM_HOOK_INIT(ipc_getlsmprop, smack_ipc_getlsmprop),
5117 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
5119 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
5120 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
5121 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
5122 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
5123 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
5125 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
5126 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
5127 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
5128 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
5130 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
5131 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
5132 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
5133 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
5135 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
5137 LSM_HOOK_INIT(getselfattr, smack_getselfattr),
5138 LSM_HOOK_INIT(setselfattr, smack_setselfattr),
5139 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
5140 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
5142 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
5143 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
5145 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
5146 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
5147 #ifdef SMACK_IPV6_PORT_LABELING
5148 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
5150 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
5151 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
5152 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
5153 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
5154 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
5155 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
5156 #ifdef SMACK_IPV6_PORT_LABELING
5157 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
5159 LSM_HOOK_INIT(sk_clone_security, smack_sk_clone_security),
5160 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
5161 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
5163 /* key management security hooks */
5165 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
5166 LSM_HOOK_INIT(key_permission, smack_key_permission),
5167 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
5168 #ifdef CONFIG_KEY_NOTIFICATIONS
5169 LSM_HOOK_INIT(watch_key, smack_watch_key),
5171 #endif /* CONFIG_KEYS */
5173 #ifdef CONFIG_WATCH_QUEUE
5174 LSM_HOOK_INIT(post_notification, smack_post_notification),
5179 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
5180 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
5181 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
5182 #endif /* CONFIG_AUDIT */
5184 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
5185 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
5186 LSM_HOOK_INIT(lsmprop_to_secctx, smack_lsmprop_to_secctx),
5187 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
5188 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
5189 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
5190 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
5191 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
5192 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
5193 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
5194 #ifdef CONFIG_IO_URING
5195 LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds),
5196 LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll),
5197 LSM_HOOK_INIT(uring_cmd, smack_uring_cmd),
5202 static __init void init_smack_known_list(void)
5205 * Initialize rule list locks
5207 mutex_init(&smack_known_huh.smk_rules_lock);
5208 mutex_init(&smack_known_hat.smk_rules_lock);
5209 mutex_init(&smack_known_floor.smk_rules_lock);
5210 mutex_init(&smack_known_star.smk_rules_lock);
5211 mutex_init(&smack_known_web.smk_rules_lock);
5213 * Initialize rule lists
5215 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
5216 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
5217 INIT_LIST_HEAD(&smack_known_star.smk_rules);
5218 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
5219 INIT_LIST_HEAD(&smack_known_web.smk_rules);
5221 * Create the known labels list
5223 smk_insert_entry(&smack_known_huh);
5224 smk_insert_entry(&smack_known_hat);
5225 smk_insert_entry(&smack_known_star);
5226 smk_insert_entry(&smack_known_floor);
5227 smk_insert_entry(&smack_known_web);
5231 * smack_init - initialize the smack system
5233 * Returns 0 on success, -ENOMEM is there's no memory
5235 static __init int smack_init(void)
5237 struct cred *cred = (struct cred *) current->cred;
5238 struct task_smack *tsp;
5240 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
5241 if (!smack_rule_cache)
5245 * Set the security state for the initial task.
5247 tsp = smack_cred(cred);
5248 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
5253 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), &smack_lsmid);
5256 pr_info("Smack: Initializing.\n");
5257 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
5258 pr_info("Smack: Netfilter enabled.\n");
5260 #ifdef SMACK_IPV6_PORT_LABELING
5261 pr_info("Smack: IPv6 port labeling enabled.\n");
5263 #ifdef SMACK_IPV6_SECMARK_LABELING
5264 pr_info("Smack: IPv6 Netfilter enabled.\n");
5267 /* initialize the smack_known_list */
5268 init_smack_known_list();
5274 * Smack requires early initialization in order to label
5275 * all processes and objects when they are created.
5277 DEFINE_LSM(smack) = {
5279 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
5280 .blobs = &smack_blob_sizes,