1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
8 * Casey Schaufler <casey@schaufler-ca.com>
9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
13 * Paul Moore <paul@paul-moore.com>
14 * Copyright (C) 2010 Nokia Corporation
15 * Copyright (C) 2011 Intel Corporation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
23 #include <asm/ioctls.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/dccp.h>
28 #include <linux/icmpv6.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
38 #include <linux/msg.h>
39 #include <linux/shm.h>
40 #include <uapi/linux/shm.h>
41 #include <linux/binfmts.h>
42 #include <linux/parser.h>
43 #include <linux/fs_context.h>
44 #include <linux/fs_parser.h>
45 #include <linux/watch_queue.h>
46 #include <linux/io_uring/cmd.h>
47 #include <uapi/linux/lsm.h>
50 #define TRANS_TRUE "TRUE"
51 #define TRANS_TRUE_SIZE 4
53 #define SMK_CONNECTING 0
54 #define SMK_RECEIVING 1
58 * Smack uses multiple xattrs.
59 * SMACK64 - for access control,
60 * SMACK64TRANSMUTE - label initialization,
61 * Not saved on files - SMACK64IPIN and SMACK64IPOUT,
62 * Must be set explicitly - SMACK64EXEC and SMACK64MMAP
64 #define SMACK_INODE_INIT_XATTRS 2
66 #ifdef SMACK_IPV6_PORT_LABELING
67 static DEFINE_MUTEX(smack_ipv6_lock);
68 static LIST_HEAD(smk_ipv6_port_list);
70 struct kmem_cache *smack_rule_cache;
71 int smack_enabled __initdata;
73 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
78 } smk_mount_opts[] = {
79 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
80 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
84 static int match_opt_prefix(char *s, int l, char **arg)
88 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
89 size_t len = smk_mount_opts[i].len;
90 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
92 if (len == l || s[len] != '=')
95 return smk_mount_opts[i].opt;
100 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
101 static char *smk_bu_mess[] = {
102 "Bringup Error", /* Unused */
103 "Bringup", /* SMACK_BRINGUP_ALLOW */
104 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
105 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
108 static void smk_bu_mode(int mode, char *s)
114 if (mode & MAY_WRITE)
118 if (mode & MAY_APPEND)
120 if (mode & MAY_TRANSMUTE)
130 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
131 static int smk_bu_note(char *note, struct smack_known *sskp,
132 struct smack_known *oskp, int mode, int rc)
134 char acc[SMK_NUM_ACCESS_TYPE + 1];
138 if (rc > SMACK_UNCONFINED_OBJECT)
141 smk_bu_mode(mode, acc);
142 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
143 sskp->smk_known, oskp->smk_known, acc, note);
147 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
150 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
151 static int smk_bu_current(char *note, struct smack_known *oskp,
154 struct task_smack *tsp = smack_cred(current_cred());
155 char acc[SMK_NUM_ACCESS_TYPE + 1];
159 if (rc > SMACK_UNCONFINED_OBJECT)
162 smk_bu_mode(mode, acc);
163 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
164 tsp->smk_task->smk_known, oskp->smk_known,
165 acc, current->comm, note);
169 #define smk_bu_current(note, oskp, mode, RC) (RC)
172 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
173 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
175 struct task_smack *tsp = smack_cred(current_cred());
176 struct smack_known *smk_task = smk_of_task_struct_obj(otp);
177 char acc[SMK_NUM_ACCESS_TYPE + 1];
181 if (rc > SMACK_UNCONFINED_OBJECT)
184 smk_bu_mode(mode, acc);
185 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
186 tsp->smk_task->smk_known, smk_task->smk_known, acc,
187 current->comm, otp->comm);
191 #define smk_bu_task(otp, mode, RC) (RC)
194 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
195 static int smk_bu_inode(struct inode *inode, int mode, int rc)
197 struct task_smack *tsp = smack_cred(current_cred());
198 struct inode_smack *isp = smack_inode(inode);
199 char acc[SMK_NUM_ACCESS_TYPE + 1];
201 if (isp->smk_flags & SMK_INODE_IMPURE)
202 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
203 inode->i_sb->s_id, inode->i_ino, current->comm);
207 if (rc > SMACK_UNCONFINED_OBJECT)
209 if (rc == SMACK_UNCONFINED_SUBJECT &&
210 (mode & (MAY_WRITE | MAY_APPEND)))
211 isp->smk_flags |= SMK_INODE_IMPURE;
213 smk_bu_mode(mode, acc);
215 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
216 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
217 inode->i_sb->s_id, inode->i_ino, current->comm);
221 #define smk_bu_inode(inode, mode, RC) (RC)
224 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
225 static int smk_bu_file(struct file *file, int mode, int rc)
227 struct task_smack *tsp = smack_cred(current_cred());
228 struct smack_known *sskp = tsp->smk_task;
229 struct inode *inode = file_inode(file);
230 struct inode_smack *isp = smack_inode(inode);
231 char acc[SMK_NUM_ACCESS_TYPE + 1];
233 if (isp->smk_flags & SMK_INODE_IMPURE)
234 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
235 inode->i_sb->s_id, inode->i_ino, current->comm);
239 if (rc > SMACK_UNCONFINED_OBJECT)
242 smk_bu_mode(mode, acc);
243 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
244 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
245 inode->i_sb->s_id, inode->i_ino, file,
250 #define smk_bu_file(file, mode, RC) (RC)
253 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
254 static int smk_bu_credfile(const struct cred *cred, struct file *file,
257 struct task_smack *tsp = smack_cred(cred);
258 struct smack_known *sskp = tsp->smk_task;
259 struct inode *inode = file_inode(file);
260 struct inode_smack *isp = smack_inode(inode);
261 char acc[SMK_NUM_ACCESS_TYPE + 1];
263 if (isp->smk_flags & SMK_INODE_IMPURE)
264 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
265 inode->i_sb->s_id, inode->i_ino, current->comm);
269 if (rc > SMACK_UNCONFINED_OBJECT)
272 smk_bu_mode(mode, acc);
273 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
274 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
275 inode->i_sb->s_id, inode->i_ino, file,
280 #define smk_bu_credfile(cred, file, mode, RC) (RC)
284 * smk_fetch - Fetch the smack label from a file.
285 * @name: type of the label (attribute)
286 * @ip: a pointer to the inode
287 * @dp: a pointer to the dentry
289 * Returns a pointer to the master list entry for the Smack label,
290 * NULL if there was no label to fetch, or an error code.
292 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
297 struct smack_known *skp = NULL;
299 if (!(ip->i_opflags & IOP_XATTR))
300 return ERR_PTR(-EOPNOTSUPP);
302 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
304 return ERR_PTR(-ENOMEM);
306 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
312 skp = smk_import_entry(buffer, rc);
320 * init_inode_smack - initialize an inode security blob
321 * @inode: inode to extract the info from
322 * @skp: a pointer to the Smack label entry to use in the blob
325 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
327 struct inode_smack *isp = smack_inode(inode);
329 isp->smk_inode = skp;
334 * init_task_smack - initialize a task security blob
335 * @tsp: blob to initialize
336 * @task: a pointer to the Smack label for the running task
337 * @forked: a pointer to the Smack label for the forked task
340 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
341 struct smack_known *forked)
343 tsp->smk_task = task;
344 tsp->smk_forked = forked;
345 INIT_LIST_HEAD(&tsp->smk_rules);
346 INIT_LIST_HEAD(&tsp->smk_relabel);
347 mutex_init(&tsp->smk_rules_lock);
351 * smk_copy_rules - copy a rule set
352 * @nhead: new rules header pointer
353 * @ohead: old rules header pointer
354 * @gfp: type of the memory for the allocation
356 * Returns 0 on success, -ENOMEM on error
358 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
361 struct smack_rule *nrp;
362 struct smack_rule *orp;
365 list_for_each_entry_rcu(orp, ohead, list) {
366 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
372 list_add_rcu(&nrp->list, nhead);
378 * smk_copy_relabel - copy smk_relabel labels list
379 * @nhead: new rules header pointer
380 * @ohead: old rules header pointer
381 * @gfp: type of the memory for the allocation
383 * Returns 0 on success, -ENOMEM on error
385 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
388 struct smack_known_list_elem *nklep;
389 struct smack_known_list_elem *oklep;
391 list_for_each_entry(oklep, ohead, list) {
392 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
394 smk_destroy_label_list(nhead);
397 nklep->smk_label = oklep->smk_label;
398 list_add(&nklep->list, nhead);
405 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
406 * @mode: input mode in form of PTRACE_MODE_*
408 * Returns a converted MAY_* mode usable by smack rules
410 static inline unsigned int smk_ptrace_mode(unsigned int mode)
412 if (mode & PTRACE_MODE_ATTACH)
413 return MAY_READWRITE;
414 if (mode & PTRACE_MODE_READ)
421 * smk_ptrace_rule_check - helper for ptrace access
422 * @tracer: tracer process
423 * @tracee_known: label entry of the process that's about to be traced
424 * @mode: ptrace attachment mode (PTRACE_MODE_*)
425 * @func: name of the function that called us, used for audit
427 * Returns 0 on access granted, -error on error
429 static int smk_ptrace_rule_check(struct task_struct *tracer,
430 struct smack_known *tracee_known,
431 unsigned int mode, const char *func)
434 struct smk_audit_info ad, *saip = NULL;
435 struct task_smack *tsp;
436 struct smack_known *tracer_known;
437 const struct cred *tracercred;
439 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
440 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
441 smk_ad_setfield_u_tsk(&ad, tracer);
446 tracercred = __task_cred(tracer);
447 tsp = smack_cred(tracercred);
448 tracer_known = smk_of_task(tsp);
450 if ((mode & PTRACE_MODE_ATTACH) &&
451 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
452 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
453 if (tracer_known->smk_known == tracee_known->smk_known)
455 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
457 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
463 smack_log(tracer_known->smk_known,
464 tracee_known->smk_known,
471 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
472 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
480 * We he, that is fun!
484 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
485 * @ctp: child task pointer
486 * @mode: ptrace attachment mode (PTRACE_MODE_*)
488 * Returns 0 if access is OK, an error code otherwise
490 * Do the capability checks.
492 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
494 struct smack_known *skp;
496 skp = smk_of_task_struct_obj(ctp);
498 return smk_ptrace_rule_check(current, skp, mode, __func__);
502 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
503 * @ptp: parent task pointer
505 * Returns 0 if access is OK, an error code otherwise
507 * Do the capability checks, and require PTRACE_MODE_ATTACH.
509 static int smack_ptrace_traceme(struct task_struct *ptp)
511 struct smack_known *skp;
513 skp = smk_of_task(smack_cred(current_cred()));
515 return smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
519 * smack_syslog - Smack approval on syslog
520 * @typefrom_file: unused
522 * Returns 0 on success, error code otherwise.
524 static int smack_syslog(int typefrom_file)
527 struct smack_known *skp = smk_of_current();
529 if (smack_privileged(CAP_MAC_OVERRIDE))
532 if (smack_syslog_label != NULL && smack_syslog_label != skp)
543 * smack_sb_alloc_security - allocate a superblock blob
544 * @sb: the superblock getting the blob
546 * Returns 0 on success or -ENOMEM on error.
548 static int smack_sb_alloc_security(struct super_block *sb)
550 struct superblock_smack *sbsp = smack_superblock(sb);
552 sbsp->smk_root = &smack_known_floor;
553 sbsp->smk_default = &smack_known_floor;
554 sbsp->smk_floor = &smack_known_floor;
555 sbsp->smk_hat = &smack_known_hat;
557 * SMK_SB_INITIALIZED will be zero from kzalloc.
563 struct smack_mnt_opts {
564 const char *fsdefault;
568 const char *fstransmute;
571 static void smack_free_mnt_opts(void *mnt_opts)
576 static int smack_add_opt(int token, const char *s, void **mnt_opts)
578 struct smack_mnt_opts *opts = *mnt_opts;
579 struct smack_known *skp;
582 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
590 skp = smk_import_entry(s, 0);
598 opts->fsdefault = skp->smk_known;
603 opts->fsfloor = skp->smk_known;
608 opts->fshat = skp->smk_known;
613 opts->fsroot = skp->smk_known;
615 case Opt_fstransmute:
616 if (opts->fstransmute)
618 opts->fstransmute = skp->smk_known;
624 pr_warn("Smack: duplicate mount options\n");
629 * smack_fs_context_submount - Initialise security data for a filesystem context
630 * @fc: The filesystem context.
631 * @reference: reference superblock
633 * Returns 0 on success or -ENOMEM on error.
635 static int smack_fs_context_submount(struct fs_context *fc,
636 struct super_block *reference)
638 struct superblock_smack *sbsp;
639 struct smack_mnt_opts *ctx;
640 struct inode_smack *isp;
642 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
647 sbsp = smack_superblock(reference);
648 isp = smack_inode(reference->s_root->d_inode);
650 if (sbsp->smk_default) {
651 ctx->fsdefault = kstrdup(sbsp->smk_default->smk_known, GFP_KERNEL);
656 if (sbsp->smk_floor) {
657 ctx->fsfloor = kstrdup(sbsp->smk_floor->smk_known, GFP_KERNEL);
663 ctx->fshat = kstrdup(sbsp->smk_hat->smk_known, GFP_KERNEL);
668 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
669 if (sbsp->smk_root) {
670 ctx->fstransmute = kstrdup(sbsp->smk_root->smk_known, GFP_KERNEL);
671 if (!ctx->fstransmute)
679 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
680 * @fc: The new filesystem context.
681 * @src_fc: The source filesystem context being duplicated.
683 * Returns 0 on success or -ENOMEM on error.
685 static int smack_fs_context_dup(struct fs_context *fc,
686 struct fs_context *src_fc)
688 struct smack_mnt_opts *dst, *src = src_fc->security;
693 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
698 dst->fsdefault = src->fsdefault;
699 dst->fsfloor = src->fsfloor;
700 dst->fshat = src->fshat;
701 dst->fsroot = src->fsroot;
702 dst->fstransmute = src->fstransmute;
707 static const struct fs_parameter_spec smack_fs_parameters[] = {
708 fsparam_string("smackfsdef", Opt_fsdefault),
709 fsparam_string("smackfsdefault", Opt_fsdefault),
710 fsparam_string("smackfsfloor", Opt_fsfloor),
711 fsparam_string("smackfshat", Opt_fshat),
712 fsparam_string("smackfsroot", Opt_fsroot),
713 fsparam_string("smackfstransmute", Opt_fstransmute),
718 * smack_fs_context_parse_param - Parse a single mount parameter
719 * @fc: The new filesystem context being constructed.
720 * @param: The parameter.
722 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
725 static int smack_fs_context_parse_param(struct fs_context *fc,
726 struct fs_parameter *param)
728 struct fs_parse_result result;
731 opt = fs_parse(fc, smack_fs_parameters, param, &result);
735 rc = smack_add_opt(opt, param->string, &fc->security);
737 param->string = NULL;
741 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
743 char *from = options, *to = options;
747 char *next = strchr(from, ',');
756 token = match_opt_prefix(from, len, &arg);
757 if (token != Opt_error) {
758 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
759 rc = smack_add_opt(token, arg, mnt_opts);
763 smack_free_mnt_opts(*mnt_opts);
768 if (!first) { // copy with preceding comma
773 memmove(to, from, len);
786 * smack_set_mnt_opts - set Smack specific mount options
787 * @sb: the file system superblock
788 * @mnt_opts: Smack mount options
789 * @kern_flags: mount option from kernel space or user space
790 * @set_kern_flags: where to store converted mount opts
792 * Returns 0 on success, an error code on failure
794 * Allow filesystems with binary mount data to explicitly set Smack mount
797 static int smack_set_mnt_opts(struct super_block *sb,
799 unsigned long kern_flags,
800 unsigned long *set_kern_flags)
802 struct dentry *root = sb->s_root;
803 struct inode *inode = d_backing_inode(root);
804 struct superblock_smack *sp = smack_superblock(sb);
805 struct inode_smack *isp;
806 struct smack_known *skp;
807 struct smack_mnt_opts *opts = mnt_opts;
808 bool transmute = false;
810 if (sp->smk_flags & SMK_SB_INITIALIZED)
813 if (!smack_privileged(CAP_MAC_ADMIN)) {
815 * Unprivileged mounts don't get to specify Smack values.
820 * Unprivileged mounts get root and default from the caller.
822 skp = smk_of_current();
824 sp->smk_default = skp;
826 * For a handful of fs types with no user-controlled
827 * backing store it's okay to trust security labels
828 * in the filesystem. The rest are untrusted.
830 if (sb->s_user_ns != &init_user_ns &&
831 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
832 sb->s_magic != RAMFS_MAGIC) {
834 sp->smk_flags |= SMK_SB_UNTRUSTED;
838 sp->smk_flags |= SMK_SB_INITIALIZED;
841 if (opts->fsdefault) {
842 skp = smk_import_entry(opts->fsdefault, 0);
845 sp->smk_default = skp;
848 skp = smk_import_entry(opts->fsfloor, 0);
854 skp = smk_import_entry(opts->fshat, 0);
860 skp = smk_import_entry(opts->fsroot, 0);
865 if (opts->fstransmute) {
866 skp = smk_import_entry(opts->fstransmute, 0);
875 * Initialize the root inode.
877 init_inode_smack(inode, sp->smk_root);
880 isp = smack_inode(inode);
881 isp->smk_flags |= SMK_INODE_TRANSMUTE;
888 * smack_sb_statfs - Smack check on statfs
889 * @dentry: identifies the file system in question
891 * Returns 0 if current can read the floor of the filesystem,
892 * and error code otherwise
894 static int smack_sb_statfs(struct dentry *dentry)
896 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
898 struct smk_audit_info ad;
900 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
901 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
903 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
904 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
913 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
914 * @bprm: the exec information
916 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
918 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
920 struct inode *inode = file_inode(bprm->file);
921 struct task_smack *bsp = smack_cred(bprm->cred);
922 struct inode_smack *isp;
923 struct superblock_smack *sbsp;
926 isp = smack_inode(inode);
927 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
930 sbsp = smack_superblock(inode->i_sb);
931 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
932 isp->smk_task != sbsp->smk_root)
935 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
936 struct task_struct *tracer;
940 tracer = ptrace_parent(current);
941 if (likely(tracer != NULL))
942 rc = smk_ptrace_rule_check(tracer,
951 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
954 bsp->smk_task = isp->smk_task;
955 bprm->per_clear |= PER_CLEAR_ON_SETID;
957 /* Decide if this is a secure exec. */
958 if (bsp->smk_task != bsp->smk_forked)
959 bprm->secureexec = 1;
969 * smack_inode_alloc_security - allocate an inode blob
970 * @inode: the inode in need of a blob
974 static int smack_inode_alloc_security(struct inode *inode)
976 struct smack_known *skp = smk_of_current();
978 init_inode_smack(inode, skp);
983 * smack_inode_init_security - copy out the smack from an inode
984 * @inode: the newly created inode
985 * @dir: containing directory object
987 * @xattrs: where to put the attributes
988 * @xattr_count: current number of LSM-provided xattrs (updated)
990 * Returns 0 if it all works out, -ENOMEM if there's no memory
992 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
993 const struct qstr *qstr,
994 struct xattr *xattrs, int *xattr_count)
996 struct task_smack *tsp = smack_cred(current_cred());
997 struct smack_known *skp = smk_of_task(tsp);
998 struct smack_known *isp = smk_of_inode(inode);
999 struct smack_known *dsp = smk_of_inode(dir);
1000 struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count);
1005 * If equal, transmuting already occurred in
1006 * smack_dentry_create_files_as(). No need to check again.
1008 if (tsp->smk_task != tsp->smk_transmuted) {
1010 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1016 * In addition to having smk_task equal to smk_transmuted,
1017 * if the access rule allows transmutation and the directory
1018 * requests transmutation then by all means transmute.
1019 * Mark the inode as changed.
1021 if ((tsp->smk_task == tsp->smk_transmuted) ||
1022 (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1023 smk_inode_transmutable(dir))) {
1024 struct xattr *xattr_transmute;
1027 * The caller of smack_dentry_create_files_as()
1028 * should have overridden the current cred, so the
1029 * inode label was already set correctly in
1030 * smack_inode_alloc_security().
1032 if (tsp->smk_task != tsp->smk_transmuted)
1034 xattr_transmute = lsm_get_xattr_slot(xattrs,
1036 if (xattr_transmute) {
1037 xattr_transmute->value = kmemdup(TRANS_TRUE,
1040 if (!xattr_transmute->value)
1043 xattr_transmute->value_len = TRANS_TRUE_SIZE;
1044 xattr_transmute->name = XATTR_SMACK_TRANSMUTE;
1048 xattr->value = kstrdup(isp->smk_known, GFP_NOFS);
1052 xattr->value_len = strlen(isp->smk_known);
1053 xattr->name = XATTR_SMACK_SUFFIX;
1060 * smack_inode_link - Smack check on link
1061 * @old_dentry: the existing object
1063 * @new_dentry: the new object
1065 * Returns 0 if access is permitted, an error code otherwise
1067 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1068 struct dentry *new_dentry)
1070 struct smack_known *isp;
1071 struct smk_audit_info ad;
1074 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1075 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1077 isp = smk_of_inode(d_backing_inode(old_dentry));
1078 rc = smk_curacc(isp, MAY_WRITE, &ad);
1079 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1081 if (rc == 0 && d_is_positive(new_dentry)) {
1082 isp = smk_of_inode(d_backing_inode(new_dentry));
1083 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1084 rc = smk_curacc(isp, MAY_WRITE, &ad);
1085 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1092 * smack_inode_unlink - Smack check on inode deletion
1093 * @dir: containing directory object
1094 * @dentry: file to unlink
1096 * Returns 0 if current can write the containing directory
1097 * and the object, error code otherwise
1099 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1101 struct inode *ip = d_backing_inode(dentry);
1102 struct smk_audit_info ad;
1105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1106 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1109 * You need write access to the thing you're unlinking
1111 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1112 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1115 * You also need write access to the containing directory
1117 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1118 smk_ad_setfield_u_fs_inode(&ad, dir);
1119 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1120 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1126 * smack_inode_rmdir - Smack check on directory deletion
1127 * @dir: containing directory object
1128 * @dentry: directory to unlink
1130 * Returns 0 if current can write the containing directory
1131 * and the directory, error code otherwise
1133 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1135 struct smk_audit_info ad;
1138 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1139 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1142 * You need write access to the thing you're removing
1144 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1145 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1148 * You also need write access to the containing directory
1150 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1151 smk_ad_setfield_u_fs_inode(&ad, dir);
1152 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1153 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1160 * smack_inode_rename - Smack check on rename
1161 * @old_inode: unused
1162 * @old_dentry: the old object
1163 * @new_inode: unused
1164 * @new_dentry: the new object
1166 * Read and write access is required on both the old and
1169 * Returns 0 if access is permitted, an error code otherwise
1171 static int smack_inode_rename(struct inode *old_inode,
1172 struct dentry *old_dentry,
1173 struct inode *new_inode,
1174 struct dentry *new_dentry)
1177 struct smack_known *isp;
1178 struct smk_audit_info ad;
1180 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1181 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1183 isp = smk_of_inode(d_backing_inode(old_dentry));
1184 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1185 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1187 if (rc == 0 && d_is_positive(new_dentry)) {
1188 isp = smk_of_inode(d_backing_inode(new_dentry));
1189 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1190 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1191 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1197 * smack_inode_permission - Smack version of permission()
1198 * @inode: the inode in question
1199 * @mask: the access requested
1201 * This is the important Smack hook.
1203 * Returns 0 if access is permitted, an error code otherwise
1205 static int smack_inode_permission(struct inode *inode, int mask)
1207 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
1208 struct smk_audit_info ad;
1209 int no_block = mask & MAY_NOT_BLOCK;
1212 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1214 * No permission to check. Existence test. Yup, it's there.
1219 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1220 if (smk_of_inode(inode) != sbsp->smk_root)
1224 /* May be droppable after audit */
1227 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1228 smk_ad_setfield_u_fs_inode(&ad, inode);
1229 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1230 rc = smk_bu_inode(inode, mask, rc);
1235 * smack_inode_setattr - Smack check for setting attributes
1236 * @dentry: the object
1237 * @iattr: for the force flag
1239 * Returns 0 if access is permitted, an error code otherwise
1241 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1243 struct smk_audit_info ad;
1247 * Need to allow for clearing the setuid bit.
1249 if (iattr->ia_valid & ATTR_FORCE)
1251 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1252 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1254 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1255 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1260 * smack_inode_getattr - Smack check for getting attributes
1261 * @path: path to extract the info from
1263 * Returns 0 if access is permitted, an error code otherwise
1265 static int smack_inode_getattr(const struct path *path)
1267 struct smk_audit_info ad;
1268 struct inode *inode = d_backing_inode(path->dentry);
1271 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1272 smk_ad_setfield_u_fs_path(&ad, *path);
1273 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1274 rc = smk_bu_inode(inode, MAY_READ, rc);
1279 * smack_inode_setxattr - Smack check for setting xattrs
1280 * @idmap: idmap of the mount
1281 * @dentry: the object
1282 * @name: name of the attribute
1283 * @value: value of the attribute
1284 * @size: size of the value
1287 * This protects the Smack attribute explicitly.
1289 * Returns 0 if access is permitted, an error code otherwise
1291 static int smack_inode_setxattr(struct mnt_idmap *idmap,
1292 struct dentry *dentry, const char *name,
1293 const void *value, size_t size, int flags)
1295 struct smk_audit_info ad;
1296 struct smack_known *skp;
1298 int check_import = 0;
1303 * Check label validity here so import won't fail in post_setxattr
1305 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1306 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1307 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1310 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1311 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1315 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1317 if (!S_ISDIR(d_backing_inode(dentry)->i_mode) ||
1318 size != TRANS_TRUE_SIZE ||
1319 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1322 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1324 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1327 if (rc == 0 && check_import) {
1328 skp = size ? smk_import_entry(value, size) : NULL;
1331 else if (skp == NULL || (check_star &&
1332 (skp == &smack_known_star || skp == &smack_known_web)))
1336 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1337 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1340 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1341 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1348 * smack_inode_post_setxattr - Apply the Smack update approved above
1350 * @name: attribute name
1351 * @value: attribute value
1352 * @size: attribute size
1355 * Set the pointer in the inode blob to the entry found
1356 * in the master label list.
1358 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1359 const void *value, size_t size, int flags)
1361 struct smack_known *skp;
1362 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1364 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1365 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1369 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1370 skp = smk_import_entry(value, size);
1372 isp->smk_inode = skp;
1373 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1374 skp = smk_import_entry(value, size);
1376 isp->smk_task = skp;
1377 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1378 skp = smk_import_entry(value, size);
1380 isp->smk_mmap = skp;
1387 * smack_inode_getxattr - Smack check on getxattr
1388 * @dentry: the object
1391 * Returns 0 if access is permitted, an error code otherwise
1393 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1395 struct smk_audit_info ad;
1398 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1399 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1401 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1402 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1407 * smack_inode_removexattr - Smack check on removexattr
1408 * @idmap: idmap of the mount
1409 * @dentry: the object
1410 * @name: name of the attribute
1412 * Removing the Smack attribute requires CAP_MAC_ADMIN
1414 * Returns 0 if access is permitted, an error code otherwise
1416 static int smack_inode_removexattr(struct mnt_idmap *idmap,
1417 struct dentry *dentry, const char *name)
1419 struct inode_smack *isp;
1420 struct smk_audit_info ad;
1423 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1424 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1425 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1426 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1427 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1428 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1429 if (!smack_privileged(CAP_MAC_ADMIN))
1432 rc = cap_inode_removexattr(idmap, dentry, name);
1437 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1438 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1440 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1441 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1445 isp = smack_inode(d_backing_inode(dentry));
1447 * Don't do anything special for these.
1448 * XATTR_NAME_SMACKIPIN
1449 * XATTR_NAME_SMACKIPOUT
1451 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1452 struct super_block *sbp = dentry->d_sb;
1453 struct superblock_smack *sbsp = smack_superblock(sbp);
1455 isp->smk_inode = sbsp->smk_default;
1456 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1457 isp->smk_task = NULL;
1458 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1459 isp->smk_mmap = NULL;
1460 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1461 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1467 * smack_inode_set_acl - Smack check for setting posix acls
1468 * @idmap: idmap of the mnt this request came from
1469 * @dentry: the object
1470 * @acl_name: name of the posix acl
1471 * @kacl: the posix acls
1473 * Returns 0 if access is permitted, an error code otherwise
1475 static int smack_inode_set_acl(struct mnt_idmap *idmap,
1476 struct dentry *dentry, const char *acl_name,
1477 struct posix_acl *kacl)
1479 struct smk_audit_info ad;
1482 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1483 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1485 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1486 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1491 * smack_inode_get_acl - Smack check for getting posix acls
1492 * @idmap: idmap of the mnt this request came from
1493 * @dentry: the object
1494 * @acl_name: name of the posix acl
1496 * Returns 0 if access is permitted, an error code otherwise
1498 static int smack_inode_get_acl(struct mnt_idmap *idmap,
1499 struct dentry *dentry, const char *acl_name)
1501 struct smk_audit_info ad;
1504 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1505 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1507 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1508 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1513 * smack_inode_remove_acl - Smack check for getting posix acls
1514 * @idmap: idmap of the mnt this request came from
1515 * @dentry: the object
1516 * @acl_name: name of the posix acl
1518 * Returns 0 if access is permitted, an error code otherwise
1520 static int smack_inode_remove_acl(struct mnt_idmap *idmap,
1521 struct dentry *dentry, const char *acl_name)
1523 struct smk_audit_info ad;
1526 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1527 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1529 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1530 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1535 * smack_inode_getsecurity - get smack xattrs
1536 * @idmap: idmap of the mount
1537 * @inode: the object
1538 * @name: attribute name
1539 * @buffer: where to put the result
1540 * @alloc: duplicate memory
1542 * Returns the size of the attribute or an error code
1544 static int smack_inode_getsecurity(struct mnt_idmap *idmap,
1545 struct inode *inode, const char *name,
1546 void **buffer, bool alloc)
1548 struct socket_smack *ssp;
1549 struct socket *sock;
1550 struct super_block *sbp;
1551 struct inode *ip = inode;
1552 struct smack_known *isp;
1553 struct inode_smack *ispp;
1557 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1558 isp = smk_of_inode(inode);
1559 } else if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) {
1560 ispp = smack_inode(inode);
1561 if (ispp->smk_flags & SMK_INODE_TRANSMUTE)
1567 * The rest of the Smack xattrs are only on sockets.
1570 if (sbp->s_magic != SOCKFS_MAGIC)
1573 sock = SOCKET_I(ip);
1574 if (sock == NULL || sock->sk == NULL)
1577 ssp = sock->sk->sk_security;
1579 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1581 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1588 label = isp->smk_known;
1590 label_len = strlen(label);
1593 *buffer = kstrdup(label, GFP_KERNEL);
1594 if (*buffer == NULL)
1603 * smack_inode_listsecurity - list the Smack attributes
1604 * @inode: the object
1605 * @buffer: where they go
1606 * @buffer_size: size of buffer
1608 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1611 int len = sizeof(XATTR_NAME_SMACK);
1613 if (buffer != NULL && len <= buffer_size)
1614 memcpy(buffer, XATTR_NAME_SMACK, len);
1620 * smack_inode_getsecid - Extract inode's security id
1621 * @inode: inode to extract the info from
1622 * @secid: where result will be saved
1624 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1626 struct smack_known *skp = smk_of_inode(inode);
1628 *secid = skp->smk_secid;
1636 * There is no smack_file_permission hook
1638 * Should access checks be done on each read or write?
1639 * UNICOS and SELinux say yes.
1640 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1642 * I'll say no for now. Smack does not do the frequent
1643 * label changing that SELinux does.
1647 * smack_file_alloc_security - assign a file security blob
1650 * The security blob for a file is a pointer to the master
1651 * label list, so no allocation is done.
1653 * f_security is the owner security information. It
1654 * isn't used on file access checks, it's for send_sigio.
1658 static int smack_file_alloc_security(struct file *file)
1660 struct smack_known **blob = smack_file(file);
1662 *blob = smk_of_current();
1667 * smack_file_ioctl - Smack check on ioctls
1672 * Relies heavily on the correct use of the ioctl command conventions.
1674 * Returns 0 if allowed, error code otherwise
1676 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1680 struct smk_audit_info ad;
1681 struct inode *inode = file_inode(file);
1683 if (unlikely(IS_PRIVATE(inode)))
1686 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1687 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1689 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1690 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1691 rc = smk_bu_file(file, MAY_WRITE, rc);
1694 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1695 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1696 rc = smk_bu_file(file, MAY_READ, rc);
1703 * smack_file_lock - Smack check on file locking
1707 * Returns 0 if current has lock access, error code otherwise
1709 static int smack_file_lock(struct file *file, unsigned int cmd)
1711 struct smk_audit_info ad;
1713 struct inode *inode = file_inode(file);
1715 if (unlikely(IS_PRIVATE(inode)))
1718 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1719 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1720 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1721 rc = smk_bu_file(file, MAY_LOCK, rc);
1726 * smack_file_fcntl - Smack check on fcntl
1728 * @cmd: what action to check
1731 * Generally these operations are harmless.
1732 * File locking operations present an obvious mechanism
1733 * for passing information, so they require write access.
1735 * Returns 0 if current has access, error code otherwise
1737 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1740 struct smk_audit_info ad;
1742 struct inode *inode = file_inode(file);
1744 if (unlikely(IS_PRIVATE(inode)))
1752 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1753 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1754 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1755 rc = smk_bu_file(file, MAY_LOCK, rc);
1759 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1760 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1761 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1762 rc = smk_bu_file(file, MAY_WRITE, rc);
1772 * smack_mmap_file - Check permissions for a mmap operation.
1773 * @file: contains the file structure for file to map (may be NULL).
1774 * @reqprot: contains the protection requested by the application.
1775 * @prot: contains the protection that will be applied by the kernel.
1776 * @flags: contains the operational flags.
1778 * The @file may be NULL, e.g. if mapping anonymous memory.
1780 * Return 0 if permission is granted.
1782 static int smack_mmap_file(struct file *file,
1783 unsigned long reqprot, unsigned long prot,
1784 unsigned long flags)
1786 struct smack_known *skp;
1787 struct smack_known *mkp;
1788 struct smack_rule *srp;
1789 struct task_smack *tsp;
1790 struct smack_known *okp;
1791 struct inode_smack *isp;
1792 struct superblock_smack *sbsp;
1801 if (unlikely(IS_PRIVATE(file_inode(file))))
1804 isp = smack_inode(file_inode(file));
1805 if (isp->smk_mmap == NULL)
1807 sbsp = smack_superblock(file_inode(file)->i_sb);
1808 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1809 isp->smk_mmap != sbsp->smk_root)
1811 mkp = isp->smk_mmap;
1813 tsp = smack_cred(current_cred());
1814 skp = smk_of_current();
1819 * For each Smack rule associated with the subject
1820 * label verify that the SMACK64MMAP also has access
1821 * to that rule's object label.
1823 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1824 okp = srp->smk_object;
1826 * Matching labels always allows access.
1828 if (mkp->smk_known == okp->smk_known)
1831 * If there is a matching local rule take
1832 * that into account as well.
1834 may = smk_access_entry(srp->smk_subject->smk_known,
1838 may = srp->smk_access;
1840 may &= srp->smk_access;
1842 * If may is zero the SMACK64MMAP subject can't
1843 * possibly have less access.
1849 * Fetch the global list entry.
1850 * If there isn't one a SMACK64MMAP subject
1851 * can't have as much access as current.
1853 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1855 if (mmay == -ENOENT) {
1860 * If there is a local entry it modifies the
1861 * potential access, too.
1863 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1865 if (tmay != -ENOENT)
1869 * If there is any access available to current that is
1870 * not available to a SMACK64MMAP subject
1873 if ((may | mmay) != mmay) {
1885 * smack_file_set_fowner - set the file security blob value
1886 * @file: object in question
1889 static void smack_file_set_fowner(struct file *file)
1891 struct smack_known **blob = smack_file(file);
1893 *blob = smk_of_current();
1897 * smack_file_send_sigiotask - Smack on sigio
1898 * @tsk: The target task
1899 * @fown: the object the signal come from
1902 * Allow a privileged task to get signals even if it shouldn't
1904 * Returns 0 if a subject with the object's smack could
1905 * write to the task, an error code otherwise.
1907 static int smack_file_send_sigiotask(struct task_struct *tsk,
1908 struct fown_struct *fown, int signum)
1910 struct smack_known **blob;
1911 struct smack_known *skp;
1912 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1913 const struct cred *tcred;
1916 struct smk_audit_info ad;
1919 * struct fown_struct is never outside the context of a struct file
1921 file = container_of(fown, struct file, f_owner);
1923 /* we don't log here as rc can be overriden */
1924 blob = smack_file(file);
1926 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1927 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1930 tcred = __task_cred(tsk);
1931 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1935 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1936 smk_ad_setfield_u_tsk(&ad, tsk);
1937 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1942 * smack_file_receive - Smack file receive check
1945 * Returns 0 if current has access, error code otherwise
1947 static int smack_file_receive(struct file *file)
1951 struct smk_audit_info ad;
1952 struct inode *inode = file_inode(file);
1953 struct socket *sock;
1954 struct task_smack *tsp;
1955 struct socket_smack *ssp;
1957 if (unlikely(IS_PRIVATE(inode)))
1960 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1961 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1963 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1964 sock = SOCKET_I(inode);
1965 ssp = sock->sk->sk_security;
1966 tsp = smack_cred(current_cred());
1968 * If the receiving process can't write to the
1969 * passed socket or if the passed socket can't
1970 * write to the receiving process don't accept
1971 * the passed socket.
1973 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1974 rc = smk_bu_file(file, may, rc);
1977 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1978 rc = smk_bu_file(file, may, rc);
1982 * This code relies on bitmasks.
1984 if (file->f_mode & FMODE_READ)
1986 if (file->f_mode & FMODE_WRITE)
1989 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1990 rc = smk_bu_file(file, may, rc);
1995 * smack_file_open - Smack dentry open processing
1998 * Set the security blob in the file structure.
1999 * Allow the open only if the task has read access. There are
2000 * many read operations (e.g. fstat) that you can do with an
2001 * fd even if you have the file open write-only.
2003 * Returns 0 if current has access, error code otherwise
2005 static int smack_file_open(struct file *file)
2007 struct task_smack *tsp = smack_cred(file->f_cred);
2008 struct inode *inode = file_inode(file);
2009 struct smk_audit_info ad;
2012 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
2013 smk_ad_setfield_u_fs_path(&ad, file->f_path);
2014 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
2015 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
2025 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
2026 * @cred: the new credentials
2027 * @gfp: the atomicity of any memory allocations
2029 * Prepare a blank set of credentials for modification. This must allocate all
2030 * the memory the LSM module might require such that cred_transfer() can
2031 * complete without error.
2033 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
2035 init_task_smack(smack_cred(cred), NULL, NULL);
2041 * smack_cred_free - "free" task-level security credentials
2042 * @cred: the credentials in question
2045 static void smack_cred_free(struct cred *cred)
2047 struct task_smack *tsp = smack_cred(cred);
2048 struct smack_rule *rp;
2049 struct list_head *l;
2050 struct list_head *n;
2052 smk_destroy_label_list(&tsp->smk_relabel);
2054 list_for_each_safe(l, n, &tsp->smk_rules) {
2055 rp = list_entry(l, struct smack_rule, list);
2056 list_del(&rp->list);
2057 kmem_cache_free(smack_rule_cache, rp);
2062 * smack_cred_prepare - prepare new set of credentials for modification
2063 * @new: the new credentials
2064 * @old: the original credentials
2065 * @gfp: the atomicity of any memory allocations
2067 * Prepare a new set of credentials for modification.
2069 static int smack_cred_prepare(struct cred *new, const struct cred *old,
2072 struct task_smack *old_tsp = smack_cred(old);
2073 struct task_smack *new_tsp = smack_cred(new);
2076 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
2078 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2082 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2088 * smack_cred_transfer - Transfer the old credentials to the new credentials
2089 * @new: the new credentials
2090 * @old: the original credentials
2092 * Fill in a set of blank credentials from another set of credentials.
2094 static void smack_cred_transfer(struct cred *new, const struct cred *old)
2096 struct task_smack *old_tsp = smack_cred(old);
2097 struct task_smack *new_tsp = smack_cred(new);
2099 new_tsp->smk_task = old_tsp->smk_task;
2100 new_tsp->smk_forked = old_tsp->smk_task;
2101 mutex_init(&new_tsp->smk_rules_lock);
2102 INIT_LIST_HEAD(&new_tsp->smk_rules);
2104 /* cbs copy rule list */
2108 * smack_cred_getsecid - get the secid corresponding to a creds structure
2109 * @cred: the object creds
2110 * @secid: where to put the result
2112 * Sets the secid to contain a u32 version of the smack label.
2114 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
2116 struct smack_known *skp;
2119 skp = smk_of_task(smack_cred(cred));
2120 *secid = skp->smk_secid;
2125 * smack_kernel_act_as - Set the subjective context in a set of credentials
2126 * @new: points to the set of credentials to be modified.
2127 * @secid: specifies the security ID to be set
2129 * Set the security data for a kernel service.
2131 static int smack_kernel_act_as(struct cred *new, u32 secid)
2133 struct task_smack *new_tsp = smack_cred(new);
2135 new_tsp->smk_task = smack_from_secid(secid);
2140 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2141 * @new: points to the set of credentials to be modified
2142 * @inode: points to the inode to use as a reference
2144 * Set the file creation context in a set of credentials to the same
2145 * as the objective context of the specified inode
2147 static int smack_kernel_create_files_as(struct cred *new,
2148 struct inode *inode)
2150 struct inode_smack *isp = smack_inode(inode);
2151 struct task_smack *tsp = smack_cred(new);
2153 tsp->smk_forked = isp->smk_inode;
2154 tsp->smk_task = tsp->smk_forked;
2159 * smk_curacc_on_task - helper to log task related access
2160 * @p: the task object
2161 * @access: the access requested
2162 * @caller: name of the calling function for audit
2164 * Return 0 if access is permitted
2166 static int smk_curacc_on_task(struct task_struct *p, int access,
2169 struct smk_audit_info ad;
2170 struct smack_known *skp = smk_of_task_struct_obj(p);
2173 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2174 smk_ad_setfield_u_tsk(&ad, p);
2175 rc = smk_curacc(skp, access, &ad);
2176 rc = smk_bu_task(p, access, rc);
2181 * smack_task_setpgid - Smack check on setting pgid
2182 * @p: the task object
2185 * Return 0 if write access is permitted
2187 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2189 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2193 * smack_task_getpgid - Smack access check for getpgid
2194 * @p: the object task
2196 * Returns 0 if current can read the object task, error code otherwise
2198 static int smack_task_getpgid(struct task_struct *p)
2200 return smk_curacc_on_task(p, MAY_READ, __func__);
2204 * smack_task_getsid - Smack access check for getsid
2205 * @p: the object task
2207 * Returns 0 if current can read the object task, error code otherwise
2209 static int smack_task_getsid(struct task_struct *p)
2211 return smk_curacc_on_task(p, MAY_READ, __func__);
2215 * smack_current_getsecid_subj - get the subjective secid of the current task
2216 * @secid: where to put the result
2218 * Sets the secid to contain a u32 version of the task's subjective smack label.
2220 static void smack_current_getsecid_subj(u32 *secid)
2222 struct smack_known *skp = smk_of_current();
2224 *secid = skp->smk_secid;
2228 * smack_task_getsecid_obj - get the objective secid of the task
2230 * @secid: where to put the result
2232 * Sets the secid to contain a u32 version of the task's objective smack label.
2234 static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2236 struct smack_known *skp = smk_of_task_struct_obj(p);
2238 *secid = skp->smk_secid;
2242 * smack_task_setnice - Smack check on setting nice
2243 * @p: the task object
2246 * Return 0 if write access is permitted
2248 static int smack_task_setnice(struct task_struct *p, int nice)
2250 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2254 * smack_task_setioprio - Smack check on setting ioprio
2255 * @p: the task object
2258 * Return 0 if write access is permitted
2260 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2262 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2266 * smack_task_getioprio - Smack check on reading ioprio
2267 * @p: the task object
2269 * Return 0 if read access is permitted
2271 static int smack_task_getioprio(struct task_struct *p)
2273 return smk_curacc_on_task(p, MAY_READ, __func__);
2277 * smack_task_setscheduler - Smack check on setting scheduler
2278 * @p: the task object
2280 * Return 0 if read access is permitted
2282 static int smack_task_setscheduler(struct task_struct *p)
2284 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2288 * smack_task_getscheduler - Smack check on reading scheduler
2289 * @p: the task object
2291 * Return 0 if read access is permitted
2293 static int smack_task_getscheduler(struct task_struct *p)
2295 return smk_curacc_on_task(p, MAY_READ, __func__);
2299 * smack_task_movememory - Smack check on moving memory
2300 * @p: the task object
2302 * Return 0 if write access is permitted
2304 static int smack_task_movememory(struct task_struct *p)
2306 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2310 * smack_task_kill - Smack check on signal delivery
2311 * @p: the task object
2314 * @cred: identifies the cred to use in lieu of current's
2316 * Return 0 if write access is permitted
2319 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2320 int sig, const struct cred *cred)
2322 struct smk_audit_info ad;
2323 struct smack_known *skp;
2324 struct smack_known *tkp = smk_of_task_struct_obj(p);
2328 return 0; /* null signal; existence test */
2330 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2331 smk_ad_setfield_u_tsk(&ad, p);
2333 * Sending a signal requires that the sender
2334 * can write the receiver.
2337 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2338 rc = smk_bu_task(p, MAY_DELIVER, rc);
2342 * If the cred isn't NULL we're dealing with some USB IO
2343 * specific behavior. This is not clean. For one thing
2344 * we can't take privilege into account.
2346 skp = smk_of_task(smack_cred(cred));
2347 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2348 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2353 * smack_task_to_inode - copy task smack into the inode blob
2354 * @p: task to copy from
2355 * @inode: inode to copy to
2357 * Sets the smack pointer in the inode security blob
2359 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2361 struct inode_smack *isp = smack_inode(inode);
2362 struct smack_known *skp = smk_of_task_struct_obj(p);
2364 isp->smk_inode = skp;
2365 isp->smk_flags |= SMK_INODE_INSTANT;
2373 * smack_sk_alloc_security - Allocate a socket blob
2376 * @gfp_flags: memory allocation flags
2378 * Assign Smack pointers to current
2380 * Returns 0 on success, -ENOMEM is there's no memory
2382 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2384 struct smack_known *skp = smk_of_current();
2385 struct socket_smack *ssp;
2387 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2392 * Sockets created by kernel threads receive web label.
2394 if (unlikely(current->flags & PF_KTHREAD)) {
2395 ssp->smk_in = &smack_known_web;
2396 ssp->smk_out = &smack_known_web;
2401 ssp->smk_packet = NULL;
2403 sk->sk_security = ssp;
2409 * smack_sk_free_security - Free a socket blob
2412 * Clears the blob pointer
2414 static void smack_sk_free_security(struct sock *sk)
2416 #ifdef SMACK_IPV6_PORT_LABELING
2417 struct smk_port_label *spp;
2419 if (sk->sk_family == PF_INET6) {
2421 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2422 if (spp->smk_sock != sk)
2424 spp->smk_can_reuse = 1;
2430 kfree(sk->sk_security);
2434 * smack_sk_clone_security - Copy security context
2435 * @sk: the old socket
2436 * @newsk: the new socket
2438 * Copy the security context of the old socket pointer to the cloned
2440 static void smack_sk_clone_security(const struct sock *sk, struct sock *newsk)
2442 struct socket_smack *ssp_old = sk->sk_security;
2443 struct socket_smack *ssp_new = newsk->sk_security;
2445 *ssp_new = *ssp_old;
2449 * smack_ipv4host_label - check host based restrictions
2450 * @sip: the object end
2452 * looks for host based access restrictions
2454 * This version will only be appropriate for really small sets of single label
2455 * hosts. The caller is responsible for ensuring that the RCU read lock is
2456 * taken before calling this function.
2458 * Returns the label of the far end or NULL if it's not special.
2460 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2462 struct smk_net4addr *snp;
2463 struct in_addr *siap = &sip->sin_addr;
2465 if (siap->s_addr == 0)
2468 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2470 * we break after finding the first match because
2471 * the list is sorted from longest to shortest mask
2472 * so we have found the most specific match
2474 if (snp->smk_host.s_addr ==
2475 (siap->s_addr & snp->smk_mask.s_addr))
2476 return snp->smk_label;
2482 * smk_ipv6_localhost - Check for local ipv6 host address
2485 * Returns boolean true if this is the localhost address
2487 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2489 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2490 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2492 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2493 ntohs(be16p[7]) == 1)
2499 * smack_ipv6host_label - check host based restrictions
2500 * @sip: the object end
2502 * looks for host based access restrictions
2504 * This version will only be appropriate for really small sets of single label
2505 * hosts. The caller is responsible for ensuring that the RCU read lock is
2506 * taken before calling this function.
2508 * Returns the label of the far end or NULL if it's not special.
2510 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2512 struct smk_net6addr *snp;
2513 struct in6_addr *sap = &sip->sin6_addr;
2518 * It's local. Don't look for a host label.
2520 if (smk_ipv6_localhost(sip))
2523 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2525 * If the label is NULL the entry has
2526 * been renounced. Ignore it.
2528 if (snp->smk_label == NULL)
2531 * we break after finding the first match because
2532 * the list is sorted from longest to shortest mask
2533 * so we have found the most specific match
2535 for (found = 1, i = 0; i < 8; i++) {
2536 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2537 snp->smk_host.s6_addr16[i]) {
2543 return snp->smk_label;
2550 * smack_netlbl_add - Set the secattr on a socket
2553 * Attach the outbound smack value (smk_out) to the socket.
2555 * Returns 0 on success or an error code
2557 static int smack_netlbl_add(struct sock *sk)
2559 struct socket_smack *ssp = sk->sk_security;
2560 struct smack_known *skp = ssp->smk_out;
2564 bh_lock_sock_nested(sk);
2566 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2569 ssp->smk_state = SMK_NETLBL_LABELED;
2572 ssp->smk_state = SMK_NETLBL_REQSKB;
2584 * smack_netlbl_delete - Remove the secattr from a socket
2587 * Remove the outbound smack value from a socket
2589 static void smack_netlbl_delete(struct sock *sk)
2591 struct socket_smack *ssp = sk->sk_security;
2594 * Take the label off the socket if one is set.
2596 if (ssp->smk_state != SMK_NETLBL_LABELED)
2600 bh_lock_sock_nested(sk);
2601 netlbl_sock_delattr(sk);
2604 ssp->smk_state = SMK_NETLBL_UNLABELED;
2608 * smk_ipv4_check - Perform IPv4 host access checks
2610 * @sap: the destination address
2612 * Set the correct secattr for the given socket based on the destination
2613 * address and perform any outbound access checks needed.
2615 * Returns 0 on success or an error code.
2618 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2620 struct smack_known *skp;
2622 struct smack_known *hkp;
2623 struct socket_smack *ssp = sk->sk_security;
2624 struct smk_audit_info ad;
2627 hkp = smack_ipv4host_label(sap);
2630 struct lsm_network_audit net;
2632 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2633 ad.a.u.net->family = sap->sin_family;
2634 ad.a.u.net->dport = sap->sin_port;
2635 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2638 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2639 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2641 * Clear the socket netlabel if it's set.
2644 smack_netlbl_delete(sk);
2652 * smk_ipv6_check - check Smack access
2653 * @subject: subject Smack label
2654 * @object: object Smack label
2656 * @act: the action being taken
2658 * Check an IPv6 access
2660 static int smk_ipv6_check(struct smack_known *subject,
2661 struct smack_known *object,
2662 struct sockaddr_in6 *address, int act)
2665 struct lsm_network_audit net;
2667 struct smk_audit_info ad;
2671 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2672 ad.a.u.net->family = PF_INET6;
2673 ad.a.u.net->dport = address->sin6_port;
2674 if (act == SMK_RECEIVING)
2675 ad.a.u.net->v6info.saddr = address->sin6_addr;
2677 ad.a.u.net->v6info.daddr = address->sin6_addr;
2679 rc = smk_access(subject, object, MAY_WRITE, &ad);
2680 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2684 #ifdef SMACK_IPV6_PORT_LABELING
2686 * smk_ipv6_port_label - Smack port access table management
2690 * Create or update the port list entry
2692 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2694 struct sock *sk = sock->sk;
2695 struct sockaddr_in6 *addr6;
2696 struct socket_smack *ssp = sock->sk->sk_security;
2697 struct smk_port_label *spp;
2698 unsigned short port = 0;
2700 if (address == NULL) {
2702 * This operation is changing the Smack information
2703 * on the bound socket. Take the changes to the port
2707 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2708 if (sk != spp->smk_sock)
2710 spp->smk_in = ssp->smk_in;
2711 spp->smk_out = ssp->smk_out;
2716 * A NULL address is only used for updating existing
2717 * bound entries. If there isn't one, it's OK.
2723 addr6 = (struct sockaddr_in6 *)address;
2724 port = ntohs(addr6->sin6_port);
2726 * This is a special case that is safely ignored.
2732 * Look for an existing port list entry.
2733 * This is an indication that a port is getting reused.
2736 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2737 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2739 if (spp->smk_can_reuse != 1) {
2743 spp->smk_port = port;
2745 spp->smk_in = ssp->smk_in;
2746 spp->smk_out = ssp->smk_out;
2747 spp->smk_can_reuse = 0;
2753 * A new port entry is required.
2755 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2759 spp->smk_port = port;
2761 spp->smk_in = ssp->smk_in;
2762 spp->smk_out = ssp->smk_out;
2763 spp->smk_sock_type = sock->type;
2764 spp->smk_can_reuse = 0;
2766 mutex_lock(&smack_ipv6_lock);
2767 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2768 mutex_unlock(&smack_ipv6_lock);
2773 * smk_ipv6_port_check - check Smack port access
2776 * @act: the action being taken
2778 * Create or update the port list entry
2780 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2783 struct smk_port_label *spp;
2784 struct socket_smack *ssp = sk->sk_security;
2785 struct smack_known *skp = NULL;
2786 unsigned short port;
2787 struct smack_known *object;
2789 if (act == SMK_RECEIVING) {
2790 skp = smack_ipv6host_label(address);
2791 object = ssp->smk_in;
2794 object = smack_ipv6host_label(address);
2798 * The other end is a single label host.
2800 if (skp != NULL && object != NULL)
2801 return smk_ipv6_check(skp, object, address, act);
2803 skp = smack_net_ambient;
2805 object = smack_net_ambient;
2808 * It's remote, so port lookup does no good.
2810 if (!smk_ipv6_localhost(address))
2811 return smk_ipv6_check(skp, object, address, act);
2814 * It's local so the send check has to have passed.
2816 if (act == SMK_RECEIVING)
2819 port = ntohs(address->sin6_port);
2821 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2822 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2824 object = spp->smk_in;
2825 if (act == SMK_CONNECTING)
2826 ssp->smk_packet = spp->smk_out;
2831 return smk_ipv6_check(skp, object, address, act);
2836 * smack_inode_setsecurity - set smack xattrs
2837 * @inode: the object
2838 * @name: attribute name
2839 * @value: attribute value
2840 * @size: size of the attribute
2843 * Sets the named attribute in the appropriate blob
2845 * Returns 0 on success, or an error code
2847 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2848 const void *value, size_t size, int flags)
2850 struct smack_known *skp;
2851 struct inode_smack *nsp = smack_inode(inode);
2852 struct socket_smack *ssp;
2853 struct socket *sock;
2856 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2859 if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) {
2860 if (!S_ISDIR(inode->i_mode) || size != TRANS_TRUE_SIZE ||
2861 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
2864 nsp->smk_flags |= SMK_INODE_TRANSMUTE;
2868 skp = smk_import_entry(value, size);
2870 return PTR_ERR(skp);
2872 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2873 nsp->smk_inode = skp;
2874 nsp->smk_flags |= SMK_INODE_INSTANT;
2878 * The rest of the Smack xattrs are only on sockets.
2880 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2883 sock = SOCKET_I(inode);
2884 if (sock == NULL || sock->sk == NULL)
2887 ssp = sock->sk->sk_security;
2889 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2891 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2893 if (sock->sk->sk_family == PF_INET) {
2894 rc = smack_netlbl_add(sock->sk);
2897 "Smack: \"%s\" netlbl error %d.\n",
2903 #ifdef SMACK_IPV6_PORT_LABELING
2904 if (sock->sk->sk_family == PF_INET6)
2905 smk_ipv6_port_label(sock, NULL);
2912 * smack_socket_post_create - finish socket setup
2914 * @family: protocol family
2919 * Sets the netlabel information on the socket
2921 * Returns 0 on success, and error code otherwise
2923 static int smack_socket_post_create(struct socket *sock, int family,
2924 int type, int protocol, int kern)
2926 struct socket_smack *ssp;
2928 if (sock->sk == NULL)
2932 * Sockets created by kernel threads receive web label.
2934 if (unlikely(current->flags & PF_KTHREAD)) {
2935 ssp = sock->sk->sk_security;
2936 ssp->smk_in = &smack_known_web;
2937 ssp->smk_out = &smack_known_web;
2940 if (family != PF_INET)
2943 * Set the outbound netlbl.
2945 return smack_netlbl_add(sock->sk);
2949 * smack_socket_socketpair - create socket pair
2950 * @socka: one socket
2951 * @sockb: another socket
2953 * Cross reference the peer labels for SO_PEERSEC
2957 static int smack_socket_socketpair(struct socket *socka,
2958 struct socket *sockb)
2960 struct socket_smack *asp = socka->sk->sk_security;
2961 struct socket_smack *bsp = sockb->sk->sk_security;
2963 asp->smk_packet = bsp->smk_out;
2964 bsp->smk_packet = asp->smk_out;
2969 #ifdef SMACK_IPV6_PORT_LABELING
2971 * smack_socket_bind - record port binding information.
2973 * @address: the port address
2974 * @addrlen: size of the address
2976 * Records the label bound to a port.
2978 * Returns 0 on success, and error code otherwise
2980 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2983 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2984 if (addrlen < SIN6_LEN_RFC2133 ||
2985 address->sa_family != AF_INET6)
2987 smk_ipv6_port_label(sock, address);
2991 #endif /* SMACK_IPV6_PORT_LABELING */
2994 * smack_socket_connect - connect access check
2996 * @sap: the other end
2997 * @addrlen: size of sap
2999 * Verifies that a connection may be possible
3001 * Returns 0 on success, and error code otherwise
3003 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
3008 if (sock->sk == NULL)
3010 if (sock->sk->sk_family != PF_INET &&
3011 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
3013 if (addrlen < offsetofend(struct sockaddr, sa_family))
3015 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
3016 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
3017 struct smack_known *rsp = NULL;
3019 if (addrlen < SIN6_LEN_RFC2133)
3021 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
3022 rsp = smack_ipv6host_label(sip);
3024 struct socket_smack *ssp = sock->sk->sk_security;
3026 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
3029 #ifdef SMACK_IPV6_PORT_LABELING
3030 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
3035 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
3037 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
3042 * smack_flags_to_may - convert S_ to MAY_ values
3043 * @flags: the S_ value
3045 * Returns the equivalent MAY_ value
3047 static int smack_flags_to_may(int flags)
3051 if (flags & S_IRUGO)
3053 if (flags & S_IWUGO)
3055 if (flags & S_IXUGO)
3062 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
3067 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
3069 struct smack_known **blob = smack_msg_msg(msg);
3071 *blob = smk_of_current();
3076 * smack_of_ipc - the smack pointer for the ipc
3079 * Returns a pointer to the smack value
3081 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
3083 struct smack_known **blob = smack_ipc(isp);
3089 * smack_ipc_alloc_security - Set the security blob for ipc
3094 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
3096 struct smack_known **blob = smack_ipc(isp);
3098 *blob = smk_of_current();
3103 * smk_curacc_shm : check if current has access on shm
3105 * @access : access requested
3107 * Returns 0 if current has the requested access, error code otherwise
3109 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
3111 struct smack_known *ssp = smack_of_ipc(isp);
3112 struct smk_audit_info ad;
3116 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3117 ad.a.u.ipc_id = isp->id;
3119 rc = smk_curacc(ssp, access, &ad);
3120 rc = smk_bu_current("shm", ssp, access, rc);
3125 * smack_shm_associate - Smack access check for shm
3127 * @shmflg: access requested
3129 * Returns 0 if current has the requested access, error code otherwise
3131 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3135 may = smack_flags_to_may(shmflg);
3136 return smk_curacc_shm(isp, may);
3140 * smack_shm_shmctl - Smack access check for shm
3142 * @cmd: what it wants to do
3144 * Returns 0 if current has the requested access, error code otherwise
3146 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3160 may = MAY_READWRITE;
3165 * System level information.
3171 return smk_curacc_shm(isp, may);
3175 * smack_shm_shmat - Smack access for shmat
3178 * @shmflg: access requested
3180 * Returns 0 if current has the requested access, error code otherwise
3182 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3187 may = smack_flags_to_may(shmflg);
3188 return smk_curacc_shm(isp, may);
3192 * smk_curacc_sem : check if current has access on sem
3194 * @access : access requested
3196 * Returns 0 if current has the requested access, error code otherwise
3198 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3200 struct smack_known *ssp = smack_of_ipc(isp);
3201 struct smk_audit_info ad;
3205 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3206 ad.a.u.ipc_id = isp->id;
3208 rc = smk_curacc(ssp, access, &ad);
3209 rc = smk_bu_current("sem", ssp, access, rc);
3214 * smack_sem_associate - Smack access check for sem
3216 * @semflg: access requested
3218 * Returns 0 if current has the requested access, error code otherwise
3220 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3224 may = smack_flags_to_may(semflg);
3225 return smk_curacc_sem(isp, may);
3229 * smack_sem_semctl - Smack access check for sem
3231 * @cmd: what it wants to do
3233 * Returns 0 if current has the requested access, error code otherwise
3235 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3254 may = MAY_READWRITE;
3259 * System level information
3266 return smk_curacc_sem(isp, may);
3270 * smack_sem_semop - Smack checks of semaphore operations
3276 * Treated as read and write in all cases.
3278 * Returns 0 if access is allowed, error code otherwise
3280 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3281 unsigned nsops, int alter)
3283 return smk_curacc_sem(isp, MAY_READWRITE);
3287 * smk_curacc_msq : helper to check if current has access on msq
3289 * @access : access requested
3291 * return 0 if current has access, error otherwise
3293 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3295 struct smack_known *msp = smack_of_ipc(isp);
3296 struct smk_audit_info ad;
3300 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3301 ad.a.u.ipc_id = isp->id;
3303 rc = smk_curacc(msp, access, &ad);
3304 rc = smk_bu_current("msq", msp, access, rc);
3309 * smack_msg_queue_associate - Smack access check for msg_queue
3311 * @msqflg: access requested
3313 * Returns 0 if current has the requested access, error code otherwise
3315 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3319 may = smack_flags_to_may(msqflg);
3320 return smk_curacc_msq(isp, may);
3324 * smack_msg_queue_msgctl - Smack access check for msg_queue
3326 * @cmd: what it wants to do
3328 * Returns 0 if current has the requested access, error code otherwise
3330 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3342 may = MAY_READWRITE;
3347 * System level information
3354 return smk_curacc_msq(isp, may);
3358 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3361 * @msqflg: access requested
3363 * Returns 0 if current has the requested access, error code otherwise
3365 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3370 may = smack_flags_to_may(msqflg);
3371 return smk_curacc_msq(isp, may);
3375 * smack_msg_queue_msgrcv - Smack access check for msg_queue
3382 * Returns 0 if current has read and write access, error code otherwise
3384 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp,
3385 struct msg_msg *msg,
3386 struct task_struct *target, long type,
3389 return smk_curacc_msq(isp, MAY_READWRITE);
3393 * smack_ipc_permission - Smack access for ipc_permission()
3394 * @ipp: the object permissions
3395 * @flag: access requested
3397 * Returns 0 if current has read and write access, error code otherwise
3399 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3401 struct smack_known **blob = smack_ipc(ipp);
3402 struct smack_known *iskp = *blob;
3403 int may = smack_flags_to_may(flag);
3404 struct smk_audit_info ad;
3408 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3409 ad.a.u.ipc_id = ipp->id;
3411 rc = smk_curacc(iskp, may, &ad);
3412 rc = smk_bu_current("svipc", iskp, may, rc);
3417 * smack_ipc_getsecid - Extract smack security id
3418 * @ipp: the object permissions
3419 * @secid: where result will be saved
3421 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3423 struct smack_known **blob = smack_ipc(ipp);
3424 struct smack_known *iskp = *blob;
3426 *secid = iskp->smk_secid;
3430 * smack_d_instantiate - Make sure the blob is correct on an inode
3431 * @opt_dentry: dentry where inode will be attached
3432 * @inode: the object
3434 * Set the inode's security blob if it hasn't been done already.
3436 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3438 struct super_block *sbp;
3439 struct superblock_smack *sbsp;
3440 struct inode_smack *isp;
3441 struct smack_known *skp;
3442 struct smack_known *ckp = smk_of_current();
3443 struct smack_known *final;
3444 char trattr[TRANS_TRUE_SIZE];
3452 isp = smack_inode(inode);
3455 * If the inode is already instantiated
3456 * take the quick way out
3458 if (isp->smk_flags & SMK_INODE_INSTANT)
3462 sbsp = smack_superblock(sbp);
3464 * We're going to use the superblock default label
3465 * if there's no label on the file.
3467 final = sbsp->smk_default;
3470 * If this is the root inode the superblock
3471 * may be in the process of initialization.
3472 * If that is the case use the root value out
3473 * of the superblock.
3475 if (opt_dentry->d_parent == opt_dentry) {
3476 switch (sbp->s_magic) {
3477 case CGROUP_SUPER_MAGIC:
3478 case CGROUP2_SUPER_MAGIC:
3480 * The cgroup filesystem is never mounted,
3481 * so there's no opportunity to set the mount
3484 sbsp->smk_root = &smack_known_star;
3485 sbsp->smk_default = &smack_known_star;
3486 isp->smk_inode = sbsp->smk_root;
3490 * What about shmem/tmpfs anonymous files with dentry
3491 * obtained from d_alloc_pseudo()?
3493 isp->smk_inode = smk_of_current();
3496 isp->smk_inode = smk_of_current();
3500 * Socket access is controlled by the socket
3501 * structures associated with the task involved.
3503 isp->smk_inode = &smack_known_star;
3506 isp->smk_inode = sbsp->smk_root;
3509 isp->smk_flags |= SMK_INODE_INSTANT;
3514 * This is pretty hackish.
3515 * Casey says that we shouldn't have to do
3516 * file system specific code, but it does help
3517 * with keeping it simple.
3519 switch (sbp->s_magic) {
3521 case CGROUP_SUPER_MAGIC:
3522 case CGROUP2_SUPER_MAGIC:
3524 * Casey says that it's a little embarrassing
3525 * that the smack file system doesn't do
3526 * extended attributes.
3528 * Cgroupfs is special
3530 final = &smack_known_star;
3532 case DEVPTS_SUPER_MAGIC:
3534 * devpts seems content with the label of the task.
3535 * Programs that change smack have to treat the
3540 case PROC_SUPER_MAGIC:
3542 * Casey says procfs appears not to care.
3543 * The superblock default suffices.
3548 * Device labels should come from the filesystem,
3549 * but watch out, because they're volitile,
3550 * getting recreated on every reboot.
3552 final = &smack_known_star;
3554 * If a smack value has been set we want to use it,
3555 * but since tmpfs isn't giving us the opportunity
3556 * to set mount options simulate setting the
3557 * superblock default.
3562 * This isn't an understood special case.
3563 * Get the value from the xattr.
3567 * UNIX domain sockets use lower level socket data.
3569 if (S_ISSOCK(inode->i_mode)) {
3570 final = &smack_known_star;
3574 * No xattr support means, alas, no SMACK label.
3575 * Use the aforeapplied default.
3576 * It would be curious if the label of the task
3577 * does not match that assigned.
3579 if (!(inode->i_opflags & IOP_XATTR))
3582 * Get the dentry for xattr.
3584 dp = dget(opt_dentry);
3585 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3586 if (!IS_ERR_OR_NULL(skp))
3590 * Transmuting directory
3592 if (S_ISDIR(inode->i_mode)) {
3594 * If this is a new directory and the label was
3595 * transmuted when the inode was initialized
3596 * set the transmute attribute on the directory
3597 * and mark the inode.
3599 * If there is a transmute attribute on the
3600 * directory mark the inode.
3602 rc = __vfs_getxattr(dp, inode,
3603 XATTR_NAME_SMACKTRANSMUTE, trattr,
3605 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3606 TRANS_TRUE_SIZE) != 0)
3609 transflag = SMK_INODE_TRANSMUTE;
3612 * Don't let the exec or mmap label be "*" or "@".
3614 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3615 if (IS_ERR(skp) || skp == &smack_known_star ||
3616 skp == &smack_known_web)
3618 isp->smk_task = skp;
3620 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3621 if (IS_ERR(skp) || skp == &smack_known_star ||
3622 skp == &smack_known_web)
3624 isp->smk_mmap = skp;
3631 isp->smk_inode = ckp;
3633 isp->smk_inode = final;
3635 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3641 * smack_getselfattr - Smack current process attribute
3642 * @attr: which attribute to fetch
3643 * @ctx: buffer to receive the result
3644 * @size: available size in, actual size out
3647 * Fill the passed user space @ctx with the details of the requested
3650 * Returns the number of attributes on success, an error code otherwise.
3651 * There will only ever be one attribute.
3653 static int smack_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx,
3654 u32 *size, u32 flags)
3657 struct smack_known *skp;
3659 if (attr != LSM_ATTR_CURRENT)
3662 skp = smk_of_current();
3663 rc = lsm_fill_user_ctx(ctx, size,
3664 skp->smk_known, strlen(skp->smk_known) + 1,
3666 return (!rc ? 1 : rc);
3670 * smack_getprocattr - Smack process attribute access
3671 * @p: the object task
3672 * @name: the name of the attribute in /proc/.../attr
3673 * @value: where to put the result
3675 * Places a copy of the task Smack into value
3677 * Returns the length of the smack label or an error code
3679 static int smack_getprocattr(struct task_struct *p, const char *name, char **value)
3681 struct smack_known *skp = smk_of_task_struct_obj(p);
3685 if (strcmp(name, "current") != 0)
3688 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3698 * do_setattr - Smack process attribute setting
3699 * @attr: the ID of the attribute
3700 * @value: the value to set
3701 * @size: the size of the value
3703 * Sets the Smack value of the task. Only setting self
3704 * is permitted and only with privilege
3706 * Returns the length of the smack label or an error code
3708 static int do_setattr(u64 attr, void *value, size_t size)
3710 struct task_smack *tsp = smack_cred(current_cred());
3712 struct smack_known *skp;
3713 struct smack_known_list_elem *sklep;
3716 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3719 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3722 if (attr != LSM_ATTR_CURRENT)
3725 skp = smk_import_entry(value, size);
3727 return PTR_ERR(skp);
3730 * No process is ever allowed the web ("@") label
3731 * and the star ("*") label.
3733 if (skp == &smack_known_web || skp == &smack_known_star)
3736 if (!smack_privileged(CAP_MAC_ADMIN)) {
3738 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3739 if (sklep->smk_label == skp) {
3747 new = prepare_creds();
3751 tsp = smack_cred(new);
3752 tsp->smk_task = skp;
3754 * process can change its label only once
3756 smk_destroy_label_list(&tsp->smk_relabel);
3763 * smack_setselfattr - Set a Smack process attribute
3764 * @attr: which attribute to set
3765 * @ctx: buffer containing the data
3766 * @size: size of @ctx
3769 * Fill the passed user space @ctx with the details of the requested
3772 * Returns 0 on success, an error code otherwise.
3774 static int smack_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
3775 u32 size, u32 flags)
3779 rc = do_setattr(attr, ctx->ctx, ctx->ctx_len);
3786 * smack_setprocattr - Smack process attribute setting
3787 * @name: the name of the attribute in /proc/.../attr
3788 * @value: the value to set
3789 * @size: the size of the value
3791 * Sets the Smack value of the task. Only setting self
3792 * is permitted and only with privilege
3794 * Returns the length of the smack label or an error code
3796 static int smack_setprocattr(const char *name, void *value, size_t size)
3798 int attr = lsm_name_to_attr(name);
3800 if (attr != LSM_ATTR_UNDEF)
3801 return do_setattr(attr, value, size);
3806 * smack_unix_stream_connect - Smack access on UDS
3808 * @other: the other sock
3811 * Return 0 if a subject with the smack of sock could access
3812 * an object with the smack of other, otherwise an error code
3814 static int smack_unix_stream_connect(struct sock *sock,
3815 struct sock *other, struct sock *newsk)
3817 struct smack_known *skp;
3818 struct smack_known *okp;
3819 struct socket_smack *ssp = sock->sk_security;
3820 struct socket_smack *osp = other->sk_security;
3821 struct socket_smack *nsp = newsk->sk_security;
3822 struct smk_audit_info ad;
3825 struct lsm_network_audit net;
3828 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3832 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3833 smk_ad_setfield_u_net_sk(&ad, other);
3835 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3836 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3840 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3841 rc = smk_bu_note("UDS connect", okp, skp,
3847 * Cross reference the peer labels for SO_PEERSEC.
3850 nsp->smk_packet = ssp->smk_out;
3851 ssp->smk_packet = osp->smk_out;
3858 * smack_unix_may_send - Smack access on UDS
3860 * @other: the other socket
3862 * Return 0 if a subject with the smack of sock could access
3863 * an object with the smack of other, otherwise an error code
3865 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3867 struct socket_smack *ssp = sock->sk->sk_security;
3868 struct socket_smack *osp = other->sk->sk_security;
3869 struct smk_audit_info ad;
3873 struct lsm_network_audit net;
3875 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3876 smk_ad_setfield_u_net_sk(&ad, other->sk);
3879 if (smack_privileged(CAP_MAC_OVERRIDE))
3882 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3883 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3888 * smack_socket_sendmsg - Smack check based on destination host
3891 * @size: the size of the message
3893 * Return 0 if the current subject can write to the destination host.
3894 * For IPv4 this is only a question if the destination is a single label host.
3895 * For IPv6 this is a check against the label of the port.
3897 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3900 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3901 #if IS_ENABLED(CONFIG_IPV6)
3902 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3904 #ifdef SMACK_IPV6_SECMARK_LABELING
3905 struct socket_smack *ssp = sock->sk->sk_security;
3906 struct smack_known *rsp;
3911 * Perfectly reasonable for this to be NULL
3916 switch (sock->sk->sk_family) {
3918 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3919 sip->sin_family != AF_INET)
3921 rc = smk_ipv4_check(sock->sk, sip);
3923 #if IS_ENABLED(CONFIG_IPV6)
3925 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3926 sap->sin6_family != AF_INET6)
3928 #ifdef SMACK_IPV6_SECMARK_LABELING
3929 rsp = smack_ipv6host_label(sap);
3931 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3934 #ifdef SMACK_IPV6_PORT_LABELING
3935 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3937 #endif /* IS_ENABLED(CONFIG_IPV6) */
3944 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3945 * @sap: netlabel secattr
3946 * @ssp: socket security information
3948 * Returns a pointer to a Smack label entry found on the label list.
3950 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3951 struct socket_smack *ssp)
3953 struct smack_known *skp;
3959 * Netlabel found it in the cache.
3961 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3962 return (struct smack_known *)sap->cache->data;
3964 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3966 * Looks like a fallback, which gives us a secid.
3968 return smack_from_secid(sap->attr.secid);
3970 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3972 * Looks like a CIPSO packet.
3973 * If there are flags but no level netlabel isn't
3974 * behaving the way we expect it to.
3976 * Look it up in the label table
3977 * Without guidance regarding the smack value
3978 * for the packet fall back on the network
3982 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3983 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3986 * Compare the catsets. Use the netlbl APIs.
3988 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3989 if ((skp->smk_netlabel.flags &
3990 NETLBL_SECATTR_MLS_CAT) == 0)
3994 for (acat = -1, kcat = -1; acat == kcat; ) {
3995 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3997 kcat = netlbl_catmap_walk(
3998 skp->smk_netlabel.attr.mls.cat,
4000 if (acat < 0 || kcat < 0)
4013 if (ssp != NULL && ssp->smk_in == &smack_known_star)
4014 return &smack_known_web;
4015 return &smack_known_star;
4018 * Without guidance regarding the smack value
4019 * for the packet fall back on the network
4022 return smack_net_ambient;
4025 #if IS_ENABLED(CONFIG_IPV6)
4026 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
4030 int proto = -EINVAL;
4031 struct ipv6hdr _ipv6h;
4032 struct ipv6hdr *ip6;
4034 struct tcphdr _tcph, *th;
4035 struct udphdr _udph, *uh;
4036 struct dccp_hdr _dccph, *dh;
4040 offset = skb_network_offset(skb);
4041 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4044 sip->sin6_addr = ip6->saddr;
4046 nexthdr = ip6->nexthdr;
4047 offset += sizeof(_ipv6h);
4048 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
4055 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4057 sip->sin6_port = th->source;
4060 case IPPROTO_UDPLITE:
4061 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4063 sip->sin6_port = uh->source;
4066 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4068 sip->sin6_port = dh->dccph_sport;
4073 #endif /* CONFIG_IPV6 */
4076 * smack_from_skb - Smack data from the secmark in an skb
4079 * Returns smack_known of the secmark or NULL if that won't work.
4081 #ifdef CONFIG_NETWORK_SECMARK
4082 static struct smack_known *smack_from_skb(struct sk_buff *skb)
4084 if (skb == NULL || skb->secmark == 0)
4087 return smack_from_secid(skb->secmark);
4090 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
4097 * smack_from_netlbl - Smack data from the IP options in an skb
4098 * @sk: socket data came in on
4099 * @family: address family
4102 * Find the Smack label in the IP options. If it hasn't been
4103 * added to the netlabel cache, add it here.
4105 * Returns smack_known of the IP options or NULL if that won't work.
4107 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
4108 struct sk_buff *skb)
4110 struct netlbl_lsm_secattr secattr;
4111 struct socket_smack *ssp = NULL;
4112 struct smack_known *skp = NULL;
4114 netlbl_secattr_init(&secattr);
4117 ssp = sk->sk_security;
4119 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
4120 skp = smack_from_secattr(&secattr, ssp);
4121 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
4122 netlbl_cache_add(skb, family, &skp->smk_netlabel);
4125 netlbl_secattr_destroy(&secattr);
4131 * smack_socket_sock_rcv_skb - Smack packet delivery access check
4135 * Returns 0 if the packet should be delivered, an error code otherwise
4137 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4139 struct socket_smack *ssp = sk->sk_security;
4140 struct smack_known *skp = NULL;
4142 struct smk_audit_info ad;
4143 u16 family = sk->sk_family;
4145 struct lsm_network_audit net;
4147 #if IS_ENABLED(CONFIG_IPV6)
4148 struct sockaddr_in6 sadd;
4151 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4153 #endif /* CONFIG_IPV6 */
4158 * If there is a secmark use it rather than the CIPSO label.
4159 * If there is no secmark fall back to CIPSO.
4160 * The secmark is assumed to reflect policy better.
4162 skp = smack_from_skb(skb);
4164 skp = smack_from_netlbl(sk, family, skb);
4166 skp = smack_net_ambient;
4170 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4171 ad.a.u.net->family = family;
4172 ad.a.u.net->netif = skb->skb_iif;
4173 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4176 * Receiving a packet requires that the other end
4177 * be able to write here. Read access is not required.
4178 * This is the simplist possible security model
4181 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4182 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
4185 netlbl_skbuff_err(skb, family, rc, 0);
4187 #if IS_ENABLED(CONFIG_IPV6)
4189 proto = smk_skb_to_addr_ipv6(skb, &sadd);
4190 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
4191 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
4193 #ifdef SMACK_IPV6_SECMARK_LABELING
4194 skp = smack_from_skb(skb);
4196 if (smk_ipv6_localhost(&sadd))
4198 skp = smack_ipv6host_label(&sadd);
4200 skp = smack_net_ambient;
4203 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4204 ad.a.u.net->family = family;
4205 ad.a.u.net->netif = skb->skb_iif;
4206 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4207 #endif /* CONFIG_AUDIT */
4208 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4209 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4211 #endif /* SMACK_IPV6_SECMARK_LABELING */
4212 #ifdef SMACK_IPV6_PORT_LABELING
4213 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
4214 #endif /* SMACK_IPV6_PORT_LABELING */
4216 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4217 ICMPV6_ADM_PROHIBITED, 0);
4219 #endif /* CONFIG_IPV6 */
4226 * smack_socket_getpeersec_stream - pull in packet label
4228 * @optval: user's destination
4229 * @optlen: size thereof
4232 * returns zero on success, an error code otherwise
4234 static int smack_socket_getpeersec_stream(struct socket *sock,
4235 sockptr_t optval, sockptr_t optlen,
4238 struct socket_smack *ssp;
4243 ssp = sock->sk->sk_security;
4244 if (ssp->smk_packet != NULL) {
4245 rcp = ssp->smk_packet->smk_known;
4246 slen = strlen(rcp) + 1;
4253 if (copy_to_sockptr(optval, rcp, slen))
4256 if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
4263 * smack_socket_getpeersec_dgram - pull in packet label
4264 * @sock: the peer socket
4266 * @secid: pointer to where to put the secid of the packet
4268 * Sets the netlabel socket state on sk from parent
4270 static int smack_socket_getpeersec_dgram(struct socket *sock,
4271 struct sk_buff *skb, u32 *secid)
4274 struct socket_smack *ssp = NULL;
4275 struct smack_known *skp;
4276 struct sock *sk = NULL;
4277 int family = PF_UNSPEC;
4278 u32 s = 0; /* 0 is the invalid secid */
4281 if (skb->protocol == htons(ETH_P_IP))
4283 #if IS_ENABLED(CONFIG_IPV6)
4284 else if (skb->protocol == htons(ETH_P_IPV6))
4286 #endif /* CONFIG_IPV6 */
4288 if (family == PF_UNSPEC && sock != NULL)
4289 family = sock->sk->sk_family;
4293 ssp = sock->sk->sk_security;
4294 s = ssp->smk_out->smk_secid;
4297 skp = smack_from_skb(skb);
4303 * Translate what netlabel gave us.
4307 skp = smack_from_netlbl(sk, family, skb);
4312 #ifdef SMACK_IPV6_SECMARK_LABELING
4313 skp = smack_from_skb(skb);
4326 * smack_sock_graft - Initialize a newly created socket with an existing sock
4328 * @parent: parent socket
4330 * Set the smk_{in,out} state of an existing sock based on the process that
4331 * is creating the new socket.
4333 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4335 struct socket_smack *ssp;
4336 struct smack_known *skp = smk_of_current();
4339 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4342 ssp = sk->sk_security;
4345 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4349 * smack_inet_conn_request - Smack access check on connect
4350 * @sk: socket involved
4354 * Returns 0 if a task with the packet label could write to
4355 * the socket, otherwise an error code
4357 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4358 struct request_sock *req)
4360 u16 family = sk->sk_family;
4361 struct smack_known *skp;
4362 struct socket_smack *ssp = sk->sk_security;
4363 struct sockaddr_in addr;
4365 struct smack_known *hskp;
4367 struct smk_audit_info ad;
4369 struct lsm_network_audit net;
4372 #if IS_ENABLED(CONFIG_IPV6)
4373 if (family == PF_INET6) {
4375 * Handle mapped IPv4 packets arriving
4376 * via IPv6 sockets. Don't set up netlabel
4377 * processing on IPv6.
4379 if (skb->protocol == htons(ETH_P_IP))
4384 #endif /* CONFIG_IPV6 */
4387 * If there is a secmark use it rather than the CIPSO label.
4388 * If there is no secmark fall back to CIPSO.
4389 * The secmark is assumed to reflect policy better.
4391 skp = smack_from_skb(skb);
4393 skp = smack_from_netlbl(sk, family, skb);
4395 skp = &smack_known_huh;
4399 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4400 ad.a.u.net->family = family;
4401 ad.a.u.net->netif = skb->skb_iif;
4402 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4405 * Receiving a packet requires that the other end be able to write
4406 * here. Read access is not required.
4408 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4409 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4414 * Save the peer's label in the request_sock so we can later setup
4415 * smk_packet in the child socket so that SO_PEERCRED can report it.
4417 req->peer_secid = skp->smk_secid;
4420 * We need to decide if we want to label the incoming connection here
4421 * if we do we only need to label the request_sock and the stack will
4422 * propagate the wire-label to the sock when it is created.
4425 addr.sin_addr.s_addr = hdr->saddr;
4427 hskp = smack_ipv4host_label(&addr);
4431 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4433 netlbl_req_delattr(req);
4439 * smack_inet_csk_clone - Copy the connection information to the new socket
4440 * @sk: the new socket
4441 * @req: the connection's request_sock
4443 * Transfer the connection's peer label to the newly created socket.
4445 static void smack_inet_csk_clone(struct sock *sk,
4446 const struct request_sock *req)
4448 struct socket_smack *ssp = sk->sk_security;
4449 struct smack_known *skp;
4451 if (req->peer_secid != 0) {
4452 skp = smack_from_secid(req->peer_secid);
4453 ssp->smk_packet = skp;
4455 ssp->smk_packet = NULL;
4459 * Key management security hooks
4461 * Casey has not tested key support very heavily.
4462 * The permission check is most likely too restrictive.
4463 * If you care about keys please have a look.
4468 * smack_key_alloc - Set the key security blob
4470 * @cred: the credentials to use
4473 * No allocation required
4477 static int smack_key_alloc(struct key *key, const struct cred *cred,
4478 unsigned long flags)
4480 struct smack_known *skp = smk_of_task(smack_cred(cred));
4482 key->security = skp;
4487 * smack_key_free - Clear the key security blob
4490 * Clear the blob pointer
4492 static void smack_key_free(struct key *key)
4494 key->security = NULL;
4498 * smack_key_permission - Smack access on a key
4499 * @key_ref: gets to the object
4500 * @cred: the credentials to use
4501 * @need_perm: requested key permission
4503 * Return 0 if the task has read and write to the object,
4504 * an error code otherwise
4506 static int smack_key_permission(key_ref_t key_ref,
4507 const struct cred *cred,
4508 enum key_need_perm need_perm)
4511 struct smk_audit_info ad;
4512 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4517 * Validate requested permissions
4519 switch (need_perm) {
4521 case KEY_NEED_SEARCH:
4523 request |= MAY_READ;
4525 case KEY_NEED_WRITE:
4527 case KEY_NEED_SETATTR:
4528 request |= MAY_WRITE;
4530 case KEY_NEED_UNSPECIFIED:
4531 case KEY_NEED_UNLINK:
4532 case KEY_SYSADMIN_OVERRIDE:
4533 case KEY_AUTHTOKEN_OVERRIDE:
4534 case KEY_DEFER_PERM_CHECK:
4540 keyp = key_ref_to_ptr(key_ref);
4544 * If the key hasn't been initialized give it access so that
4547 if (keyp->security == NULL)
4550 * This should not occur
4555 if (smack_privileged(CAP_MAC_OVERRIDE))
4559 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4560 ad.a.u.key_struct.key = keyp->serial;
4561 ad.a.u.key_struct.key_desc = keyp->description;
4563 rc = smk_access(tkp, keyp->security, request, &ad);
4564 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4569 * smack_key_getsecurity - Smack label tagging the key
4570 * @key points to the key to be queried
4571 * @_buffer points to a pointer that should be set to point to the
4572 * resulting string (if no label or an error occurs).
4573 * Return the length of the string (including terminating NUL) or -ve if
4575 * May also return 0 (and a NULL buffer pointer) if there is no label.
4577 static int smack_key_getsecurity(struct key *key, char **_buffer)
4579 struct smack_known *skp = key->security;
4583 if (key->security == NULL) {
4588 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4591 length = strlen(copy) + 1;
4598 #ifdef CONFIG_KEY_NOTIFICATIONS
4600 * smack_watch_key - Smack access to watch a key for notifications.
4601 * @key: The key to be watched
4603 * Return 0 if the @watch->cred has permission to read from the key object and
4604 * an error otherwise.
4606 static int smack_watch_key(struct key *key)
4608 struct smk_audit_info ad;
4609 struct smack_known *tkp = smk_of_current();
4615 * If the key hasn't been initialized give it access so that
4618 if (key->security == NULL)
4621 * This should not occur
4626 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4630 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4631 ad.a.u.key_struct.key = key->serial;
4632 ad.a.u.key_struct.key_desc = key->description;
4634 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4635 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4638 #endif /* CONFIG_KEY_NOTIFICATIONS */
4639 #endif /* CONFIG_KEYS */
4641 #ifdef CONFIG_WATCH_QUEUE
4643 * smack_post_notification - Smack access to post a notification to a queue
4644 * @w_cred: The credentials of the watcher.
4645 * @cred: The credentials of the event source (may be NULL).
4646 * @n: The notification message to be posted.
4648 static int smack_post_notification(const struct cred *w_cred,
4649 const struct cred *cred,
4650 struct watch_notification *n)
4652 struct smk_audit_info ad;
4653 struct smack_known *subj, *obj;
4656 /* Always let maintenance notifications through. */
4657 if (n->type == WATCH_TYPE_META)
4662 subj = smk_of_task(smack_cred(cred));
4663 obj = smk_of_task(smack_cred(w_cred));
4665 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4666 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4667 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4670 #endif /* CONFIG_WATCH_QUEUE */
4675 * Audit requires a unique representation of each Smack specific
4676 * rule. This unique representation is used to distinguish the
4677 * object to be audited from remaining kernel objects and also
4678 * works as a glue between the audit hooks.
4680 * Since repository entries are added but never deleted, we'll use
4681 * the smack_known label address related to the given audit rule as
4682 * the needed unique representation. This also better fits the smack
4683 * model where nearly everything is a label.
4688 * smack_audit_rule_init - Initialize a smack audit rule
4689 * @field: audit rule fields given from user-space (audit.h)
4690 * @op: required testing operator (=, !=, >, <, ...)
4691 * @rulestr: smack label to be audited
4692 * @vrule: pointer to save our own audit rule representation
4694 * Prepare to audit cases where (@field @op @rulestr) is true.
4695 * The label to be audited is created if necessay.
4697 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4699 struct smack_known *skp;
4700 char **rule = (char **)vrule;
4703 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4706 if (op != Audit_equal && op != Audit_not_equal)
4709 skp = smk_import_entry(rulestr, 0);
4711 return PTR_ERR(skp);
4713 *rule = skp->smk_known;
4719 * smack_audit_rule_known - Distinguish Smack audit rules
4720 * @krule: rule of interest, in Audit kernel representation format
4722 * This is used to filter Smack rules from remaining Audit ones.
4723 * If it's proved that this rule belongs to us, the
4724 * audit_rule_match hook will be called to do the final judgement.
4726 static int smack_audit_rule_known(struct audit_krule *krule)
4728 struct audit_field *f;
4731 for (i = 0; i < krule->field_count; i++) {
4732 f = &krule->fields[i];
4734 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4742 * smack_audit_rule_match - Audit given object ?
4743 * @secid: security id for identifying the object to test
4744 * @field: audit rule flags given from user-space
4745 * @op: required testing operator
4746 * @vrule: smack internal rule presentation
4748 * The core Audit hook. It's used to take the decision of
4749 * whether to audit or not to audit a given object.
4751 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4753 struct smack_known *skp;
4756 if (unlikely(!rule)) {
4757 WARN_ONCE(1, "Smack: missing rule\n");
4761 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4764 skp = smack_from_secid(secid);
4767 * No need to do string comparisons. If a match occurs,
4768 * both pointers will point to the same smack_known
4771 if (op == Audit_equal)
4772 return (rule == skp->smk_known);
4773 if (op == Audit_not_equal)
4774 return (rule != skp->smk_known);
4780 * There is no need for a smack_audit_rule_free hook.
4781 * No memory was allocated.
4784 #endif /* CONFIG_AUDIT */
4787 * smack_ismaclabel - check if xattr @name references a smack MAC label
4788 * @name: Full xattr name to check.
4790 static int smack_ismaclabel(const char *name)
4792 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4797 * smack_secid_to_secctx - return the smack label for a secid
4798 * @secid: incoming integer
4799 * @secdata: destination
4800 * @seclen: how long it is
4802 * Exists for networking code.
4804 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4806 struct smack_known *skp = smack_from_secid(secid);
4809 *secdata = skp->smk_known;
4810 *seclen = strlen(skp->smk_known);
4815 * smack_secctx_to_secid - return the secid for a smack label
4816 * @secdata: smack label
4817 * @seclen: how long result is
4818 * @secid: outgoing integer
4820 * Exists for audit and networking code.
4822 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4824 struct smack_known *skp = smk_find_entry(secdata);
4827 *secid = skp->smk_secid;
4834 * There used to be a smack_release_secctx hook
4835 * that did nothing back when hooks were in a vector.
4836 * Now that there's a list such a hook adds cost.
4839 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4841 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4845 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4847 return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
4851 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4853 struct smack_known *skp = smk_of_inode(inode);
4855 *ctx = skp->smk_known;
4856 *ctxlen = strlen(skp->smk_known);
4860 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4863 struct task_smack *tsp;
4864 struct smack_known *skp;
4865 struct inode_smack *isp;
4866 struct cred *new_creds = *new;
4868 if (new_creds == NULL) {
4869 new_creds = prepare_creds();
4870 if (new_creds == NULL)
4874 tsp = smack_cred(new_creds);
4877 * Get label from overlay inode and set it in create_sid
4879 isp = smack_inode(d_inode(dentry));
4880 skp = isp->smk_inode;
4881 tsp->smk_task = skp;
4886 static int smack_inode_copy_up_xattr(const char *name)
4889 * Return 1 if this is the smack access Smack attribute.
4891 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4897 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4899 const struct cred *old,
4902 struct task_smack *otsp = smack_cred(old);
4903 struct task_smack *ntsp = smack_cred(new);
4904 struct inode_smack *isp;
4908 * Use the process credential unless all of
4909 * the transmuting criteria are met
4911 ntsp->smk_task = otsp->smk_task;
4914 * the attribute of the containing directory
4916 isp = smack_inode(d_inode(dentry->d_parent));
4918 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4920 may = smk_access_entry(otsp->smk_task->smk_known,
4921 isp->smk_inode->smk_known,
4922 &otsp->smk_task->smk_rules);
4926 * If the directory is transmuting and the rule
4927 * providing access is transmuting use the containing
4928 * directory label instead of the process label.
4930 if (may > 0 && (may & MAY_TRANSMUTE)) {
4931 ntsp->smk_task = isp->smk_inode;
4932 ntsp->smk_transmuted = ntsp->smk_task;
4938 #ifdef CONFIG_IO_URING
4940 * smack_uring_override_creds - Is io_uring cred override allowed?
4941 * @new: the target creds
4943 * Check to see if the current task is allowed to override it's credentials
4944 * to service an io_uring operation.
4946 static int smack_uring_override_creds(const struct cred *new)
4948 struct task_smack *tsp = smack_cred(current_cred());
4949 struct task_smack *nsp = smack_cred(new);
4952 * Allow the degenerate case where the new Smack value is
4953 * the same as the current Smack value.
4955 if (tsp->smk_task == nsp->smk_task)
4958 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4965 * smack_uring_sqpoll - check if a io_uring polling thread can be created
4967 * Check to see if the current task is allowed to create a new io_uring
4968 * kernel polling thread.
4970 static int smack_uring_sqpoll(void)
4972 if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred()))
4979 * smack_uring_cmd - check on file operations for io_uring
4980 * @ioucmd: the command in question
4982 * Make a best guess about whether a io_uring "command" should
4983 * be allowed. Use the same logic used for determining if the
4984 * file could be opened for read in the absence of better criteria.
4986 static int smack_uring_cmd(struct io_uring_cmd *ioucmd)
4988 struct file *file = ioucmd->file;
4989 struct smk_audit_info ad;
4990 struct task_smack *tsp;
4991 struct inode *inode;
4997 tsp = smack_cred(file->f_cred);
4998 inode = file_inode(file);
5000 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
5001 smk_ad_setfield_u_fs_path(&ad, file->f_path);
5002 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
5003 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
5008 #endif /* CONFIG_IO_URING */
5010 struct lsm_blob_sizes smack_blob_sizes __ro_after_init = {
5011 .lbs_cred = sizeof(struct task_smack),
5012 .lbs_file = sizeof(struct smack_known *),
5013 .lbs_inode = sizeof(struct inode_smack),
5014 .lbs_ipc = sizeof(struct smack_known *),
5015 .lbs_msg_msg = sizeof(struct smack_known *),
5016 .lbs_superblock = sizeof(struct superblock_smack),
5017 .lbs_xattr_count = SMACK_INODE_INIT_XATTRS,
5020 static const struct lsm_id smack_lsmid = {
5025 static struct security_hook_list smack_hooks[] __ro_after_init = {
5026 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
5027 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
5028 LSM_HOOK_INIT(syslog, smack_syslog),
5030 LSM_HOOK_INIT(fs_context_submount, smack_fs_context_submount),
5031 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
5032 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
5034 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
5035 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
5036 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
5037 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
5038 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
5040 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
5042 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
5043 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
5044 LSM_HOOK_INIT(inode_link, smack_inode_link),
5045 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
5046 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
5047 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
5048 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
5049 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
5050 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
5051 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
5052 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
5053 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
5054 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
5055 LSM_HOOK_INIT(inode_set_acl, smack_inode_set_acl),
5056 LSM_HOOK_INIT(inode_get_acl, smack_inode_get_acl),
5057 LSM_HOOK_INIT(inode_remove_acl, smack_inode_remove_acl),
5058 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
5059 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
5060 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
5061 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
5063 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
5064 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
5065 LSM_HOOK_INIT(file_ioctl_compat, smack_file_ioctl),
5066 LSM_HOOK_INIT(file_lock, smack_file_lock),
5067 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
5068 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
5069 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
5070 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
5071 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
5072 LSM_HOOK_INIT(file_receive, smack_file_receive),
5074 LSM_HOOK_INIT(file_open, smack_file_open),
5076 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
5077 LSM_HOOK_INIT(cred_free, smack_cred_free),
5078 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
5079 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
5080 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
5081 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
5082 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
5083 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
5084 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
5085 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
5086 LSM_HOOK_INIT(current_getsecid_subj, smack_current_getsecid_subj),
5087 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
5088 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
5089 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
5090 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
5091 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
5092 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
5093 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
5094 LSM_HOOK_INIT(task_kill, smack_task_kill),
5095 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
5097 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
5098 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
5100 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
5102 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
5103 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
5104 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
5105 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
5106 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
5108 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
5109 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
5110 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
5111 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
5113 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
5114 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
5115 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
5116 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
5118 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
5120 LSM_HOOK_INIT(getselfattr, smack_getselfattr),
5121 LSM_HOOK_INIT(setselfattr, smack_setselfattr),
5122 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
5123 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
5125 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
5126 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
5128 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
5129 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
5130 #ifdef SMACK_IPV6_PORT_LABELING
5131 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
5133 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
5134 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
5135 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
5136 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
5137 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
5138 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
5139 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
5140 LSM_HOOK_INIT(sk_clone_security, smack_sk_clone_security),
5141 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
5142 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
5143 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
5145 /* key management security hooks */
5147 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
5148 LSM_HOOK_INIT(key_free, smack_key_free),
5149 LSM_HOOK_INIT(key_permission, smack_key_permission),
5150 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
5151 #ifdef CONFIG_KEY_NOTIFICATIONS
5152 LSM_HOOK_INIT(watch_key, smack_watch_key),
5154 #endif /* CONFIG_KEYS */
5156 #ifdef CONFIG_WATCH_QUEUE
5157 LSM_HOOK_INIT(post_notification, smack_post_notification),
5162 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
5163 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
5164 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
5165 #endif /* CONFIG_AUDIT */
5167 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
5168 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
5169 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
5170 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
5171 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
5172 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
5173 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
5174 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
5175 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
5176 #ifdef CONFIG_IO_URING
5177 LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds),
5178 LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll),
5179 LSM_HOOK_INIT(uring_cmd, smack_uring_cmd),
5184 static __init void init_smack_known_list(void)
5187 * Initialize rule list locks
5189 mutex_init(&smack_known_huh.smk_rules_lock);
5190 mutex_init(&smack_known_hat.smk_rules_lock);
5191 mutex_init(&smack_known_floor.smk_rules_lock);
5192 mutex_init(&smack_known_star.smk_rules_lock);
5193 mutex_init(&smack_known_web.smk_rules_lock);
5195 * Initialize rule lists
5197 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
5198 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
5199 INIT_LIST_HEAD(&smack_known_star.smk_rules);
5200 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
5201 INIT_LIST_HEAD(&smack_known_web.smk_rules);
5203 * Create the known labels list
5205 smk_insert_entry(&smack_known_huh);
5206 smk_insert_entry(&smack_known_hat);
5207 smk_insert_entry(&smack_known_star);
5208 smk_insert_entry(&smack_known_floor);
5209 smk_insert_entry(&smack_known_web);
5213 * smack_init - initialize the smack system
5215 * Returns 0 on success, -ENOMEM is there's no memory
5217 static __init int smack_init(void)
5219 struct cred *cred = (struct cred *) current->cred;
5220 struct task_smack *tsp;
5222 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
5223 if (!smack_rule_cache)
5227 * Set the security state for the initial task.
5229 tsp = smack_cred(cred);
5230 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
5235 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), &smack_lsmid);
5238 pr_info("Smack: Initializing.\n");
5239 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
5240 pr_info("Smack: Netfilter enabled.\n");
5242 #ifdef SMACK_IPV6_PORT_LABELING
5243 pr_info("Smack: IPv6 port labeling enabled.\n");
5245 #ifdef SMACK_IPV6_SECMARK_LABELING
5246 pr_info("Smack: IPv6 Netfilter enabled.\n");
5249 /* initialize the smack_known_list */
5250 init_smack_known_list();
5256 * Smack requires early initialization in order to label
5257 * all processes and objects when they are created.
5259 DEFINE_LSM(smack) = {
5261 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
5262 .blobs = &smack_blob_sizes,