1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
8 * Casey Schaufler <casey@schaufler-ca.com>
9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
13 * Paul Moore <paul@paul-moore.com>
14 * Copyright (C) 2010 Nokia Corporation
15 * Copyright (C) 2011 Intel Corporation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
23 #include <asm/ioctls.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/dccp.h>
28 #include <linux/icmpv6.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
38 #include <linux/msg.h>
39 #include <linux/shm.h>
40 #include <linux/binfmts.h>
41 #include <linux/parser.h>
42 #include <linux/fs_context.h>
43 #include <linux/fs_parser.h>
44 #include <linux/watch_queue.h>
45 #include <linux/io_uring.h>
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
56 * Smack uses multiple xattrs.
57 * SMACK64 - for access control,
58 * SMACK64TRANSMUTE - label initialization,
59 * Not saved on files - SMACK64IPIN and SMACK64IPOUT,
60 * Must be set explicitly - SMACK64EXEC and SMACK64MMAP
62 #define SMACK_INODE_INIT_XATTRS 2
64 #ifdef SMACK_IPV6_PORT_LABELING
65 static DEFINE_MUTEX(smack_ipv6_lock);
66 static LIST_HEAD(smk_ipv6_port_list);
68 struct kmem_cache *smack_rule_cache;
69 int smack_enabled __initdata;
71 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
76 } smk_mount_opts[] = {
77 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
78 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
82 static int match_opt_prefix(char *s, int l, char **arg)
86 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
87 size_t len = smk_mount_opts[i].len;
88 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
90 if (len == l || s[len] != '=')
93 return smk_mount_opts[i].opt;
98 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
99 static char *smk_bu_mess[] = {
100 "Bringup Error", /* Unused */
101 "Bringup", /* SMACK_BRINGUP_ALLOW */
102 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
103 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
106 static void smk_bu_mode(int mode, char *s)
112 if (mode & MAY_WRITE)
116 if (mode & MAY_APPEND)
118 if (mode & MAY_TRANSMUTE)
128 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
129 static int smk_bu_note(char *note, struct smack_known *sskp,
130 struct smack_known *oskp, int mode, int rc)
132 char acc[SMK_NUM_ACCESS_TYPE + 1];
136 if (rc > SMACK_UNCONFINED_OBJECT)
139 smk_bu_mode(mode, acc);
140 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
141 sskp->smk_known, oskp->smk_known, acc, note);
145 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
148 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
149 static int smk_bu_current(char *note, struct smack_known *oskp,
152 struct task_smack *tsp = smack_cred(current_cred());
153 char acc[SMK_NUM_ACCESS_TYPE + 1];
157 if (rc > SMACK_UNCONFINED_OBJECT)
160 smk_bu_mode(mode, acc);
161 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
162 tsp->smk_task->smk_known, oskp->smk_known,
163 acc, current->comm, note);
167 #define smk_bu_current(note, oskp, mode, RC) (RC)
170 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
171 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
173 struct task_smack *tsp = smack_cred(current_cred());
174 struct smack_known *smk_task = smk_of_task_struct_obj(otp);
175 char acc[SMK_NUM_ACCESS_TYPE + 1];
179 if (rc > SMACK_UNCONFINED_OBJECT)
182 smk_bu_mode(mode, acc);
183 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
184 tsp->smk_task->smk_known, smk_task->smk_known, acc,
185 current->comm, otp->comm);
189 #define smk_bu_task(otp, mode, RC) (RC)
192 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
193 static int smk_bu_inode(struct inode *inode, int mode, int rc)
195 struct task_smack *tsp = smack_cred(current_cred());
196 struct inode_smack *isp = smack_inode(inode);
197 char acc[SMK_NUM_ACCESS_TYPE + 1];
199 if (isp->smk_flags & SMK_INODE_IMPURE)
200 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
201 inode->i_sb->s_id, inode->i_ino, current->comm);
205 if (rc > SMACK_UNCONFINED_OBJECT)
207 if (rc == SMACK_UNCONFINED_SUBJECT &&
208 (mode & (MAY_WRITE | MAY_APPEND)))
209 isp->smk_flags |= SMK_INODE_IMPURE;
211 smk_bu_mode(mode, acc);
213 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
214 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
215 inode->i_sb->s_id, inode->i_ino, current->comm);
219 #define smk_bu_inode(inode, mode, RC) (RC)
222 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
223 static int smk_bu_file(struct file *file, int mode, int rc)
225 struct task_smack *tsp = smack_cred(current_cred());
226 struct smack_known *sskp = tsp->smk_task;
227 struct inode *inode = file_inode(file);
228 struct inode_smack *isp = smack_inode(inode);
229 char acc[SMK_NUM_ACCESS_TYPE + 1];
231 if (isp->smk_flags & SMK_INODE_IMPURE)
232 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
233 inode->i_sb->s_id, inode->i_ino, current->comm);
237 if (rc > SMACK_UNCONFINED_OBJECT)
240 smk_bu_mode(mode, acc);
241 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
242 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
243 inode->i_sb->s_id, inode->i_ino, file,
248 #define smk_bu_file(file, mode, RC) (RC)
251 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
252 static int smk_bu_credfile(const struct cred *cred, struct file *file,
255 struct task_smack *tsp = smack_cred(cred);
256 struct smack_known *sskp = tsp->smk_task;
257 struct inode *inode = file_inode(file);
258 struct inode_smack *isp = smack_inode(inode);
259 char acc[SMK_NUM_ACCESS_TYPE + 1];
261 if (isp->smk_flags & SMK_INODE_IMPURE)
262 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
263 inode->i_sb->s_id, inode->i_ino, current->comm);
267 if (rc > SMACK_UNCONFINED_OBJECT)
270 smk_bu_mode(mode, acc);
271 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
272 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
273 inode->i_sb->s_id, inode->i_ino, file,
278 #define smk_bu_credfile(cred, file, mode, RC) (RC)
282 * smk_fetch - Fetch the smack label from a file.
283 * @name: type of the label (attribute)
284 * @ip: a pointer to the inode
285 * @dp: a pointer to the dentry
287 * Returns a pointer to the master list entry for the Smack label,
288 * NULL if there was no label to fetch, or an error code.
290 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
295 struct smack_known *skp = NULL;
297 if (!(ip->i_opflags & IOP_XATTR))
298 return ERR_PTR(-EOPNOTSUPP);
300 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
302 return ERR_PTR(-ENOMEM);
304 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
310 skp = smk_import_entry(buffer, rc);
318 * init_inode_smack - initialize an inode security blob
319 * @inode: inode to extract the info from
320 * @skp: a pointer to the Smack label entry to use in the blob
323 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
325 struct inode_smack *isp = smack_inode(inode);
327 isp->smk_inode = skp;
332 * init_task_smack - initialize a task security blob
333 * @tsp: blob to initialize
334 * @task: a pointer to the Smack label for the running task
335 * @forked: a pointer to the Smack label for the forked task
338 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
339 struct smack_known *forked)
341 tsp->smk_task = task;
342 tsp->smk_forked = forked;
343 INIT_LIST_HEAD(&tsp->smk_rules);
344 INIT_LIST_HEAD(&tsp->smk_relabel);
345 mutex_init(&tsp->smk_rules_lock);
349 * smk_copy_rules - copy a rule set
350 * @nhead: new rules header pointer
351 * @ohead: old rules header pointer
352 * @gfp: type of the memory for the allocation
354 * Returns 0 on success, -ENOMEM on error
356 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
359 struct smack_rule *nrp;
360 struct smack_rule *orp;
363 list_for_each_entry_rcu(orp, ohead, list) {
364 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
370 list_add_rcu(&nrp->list, nhead);
376 * smk_copy_relabel - copy smk_relabel labels list
377 * @nhead: new rules header pointer
378 * @ohead: old rules header pointer
379 * @gfp: type of the memory for the allocation
381 * Returns 0 on success, -ENOMEM on error
383 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
386 struct smack_known_list_elem *nklep;
387 struct smack_known_list_elem *oklep;
389 list_for_each_entry(oklep, ohead, list) {
390 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
392 smk_destroy_label_list(nhead);
395 nklep->smk_label = oklep->smk_label;
396 list_add(&nklep->list, nhead);
403 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
404 * @mode: input mode in form of PTRACE_MODE_*
406 * Returns a converted MAY_* mode usable by smack rules
408 static inline unsigned int smk_ptrace_mode(unsigned int mode)
410 if (mode & PTRACE_MODE_ATTACH)
411 return MAY_READWRITE;
412 if (mode & PTRACE_MODE_READ)
419 * smk_ptrace_rule_check - helper for ptrace access
420 * @tracer: tracer process
421 * @tracee_known: label entry of the process that's about to be traced
422 * @mode: ptrace attachment mode (PTRACE_MODE_*)
423 * @func: name of the function that called us, used for audit
425 * Returns 0 on access granted, -error on error
427 static int smk_ptrace_rule_check(struct task_struct *tracer,
428 struct smack_known *tracee_known,
429 unsigned int mode, const char *func)
432 struct smk_audit_info ad, *saip = NULL;
433 struct task_smack *tsp;
434 struct smack_known *tracer_known;
435 const struct cred *tracercred;
437 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
438 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
439 smk_ad_setfield_u_tsk(&ad, tracer);
444 tracercred = __task_cred(tracer);
445 tsp = smack_cred(tracercred);
446 tracer_known = smk_of_task(tsp);
448 if ((mode & PTRACE_MODE_ATTACH) &&
449 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
450 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
451 if (tracer_known->smk_known == tracee_known->smk_known)
453 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
455 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
461 smack_log(tracer_known->smk_known,
462 tracee_known->smk_known,
469 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
470 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
478 * We he, that is fun!
482 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
483 * @ctp: child task pointer
484 * @mode: ptrace attachment mode (PTRACE_MODE_*)
486 * Returns 0 if access is OK, an error code otherwise
488 * Do the capability checks.
490 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
492 struct smack_known *skp;
494 skp = smk_of_task_struct_obj(ctp);
496 return smk_ptrace_rule_check(current, skp, mode, __func__);
500 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
501 * @ptp: parent task pointer
503 * Returns 0 if access is OK, an error code otherwise
505 * Do the capability checks, and require PTRACE_MODE_ATTACH.
507 static int smack_ptrace_traceme(struct task_struct *ptp)
509 struct smack_known *skp;
511 skp = smk_of_task(smack_cred(current_cred()));
513 return smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
517 * smack_syslog - Smack approval on syslog
518 * @typefrom_file: unused
520 * Returns 0 on success, error code otherwise.
522 static int smack_syslog(int typefrom_file)
525 struct smack_known *skp = smk_of_current();
527 if (smack_privileged(CAP_MAC_OVERRIDE))
530 if (smack_syslog_label != NULL && smack_syslog_label != skp)
541 * smack_sb_alloc_security - allocate a superblock blob
542 * @sb: the superblock getting the blob
544 * Returns 0 on success or -ENOMEM on error.
546 static int smack_sb_alloc_security(struct super_block *sb)
548 struct superblock_smack *sbsp = smack_superblock(sb);
550 sbsp->smk_root = &smack_known_floor;
551 sbsp->smk_default = &smack_known_floor;
552 sbsp->smk_floor = &smack_known_floor;
553 sbsp->smk_hat = &smack_known_hat;
555 * SMK_SB_INITIALIZED will be zero from kzalloc.
561 struct smack_mnt_opts {
562 const char *fsdefault;
566 const char *fstransmute;
569 static void smack_free_mnt_opts(void *mnt_opts)
574 static int smack_add_opt(int token, const char *s, void **mnt_opts)
576 struct smack_mnt_opts *opts = *mnt_opts;
577 struct smack_known *skp;
580 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
588 skp = smk_import_entry(s, 0);
596 opts->fsdefault = skp->smk_known;
601 opts->fsfloor = skp->smk_known;
606 opts->fshat = skp->smk_known;
611 opts->fsroot = skp->smk_known;
613 case Opt_fstransmute:
614 if (opts->fstransmute)
616 opts->fstransmute = skp->smk_known;
622 pr_warn("Smack: duplicate mount options\n");
627 * smack_fs_context_submount - Initialise security data for a filesystem context
628 * @fc: The filesystem context.
629 * @reference: reference superblock
631 * Returns 0 on success or -ENOMEM on error.
633 static int smack_fs_context_submount(struct fs_context *fc,
634 struct super_block *reference)
636 struct superblock_smack *sbsp;
637 struct smack_mnt_opts *ctx;
638 struct inode_smack *isp;
640 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
645 sbsp = smack_superblock(reference);
646 isp = smack_inode(reference->s_root->d_inode);
648 if (sbsp->smk_default) {
649 ctx->fsdefault = kstrdup(sbsp->smk_default->smk_known, GFP_KERNEL);
654 if (sbsp->smk_floor) {
655 ctx->fsfloor = kstrdup(sbsp->smk_floor->smk_known, GFP_KERNEL);
661 ctx->fshat = kstrdup(sbsp->smk_hat->smk_known, GFP_KERNEL);
666 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
667 if (sbsp->smk_root) {
668 ctx->fstransmute = kstrdup(sbsp->smk_root->smk_known, GFP_KERNEL);
669 if (!ctx->fstransmute)
677 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
678 * @fc: The new filesystem context.
679 * @src_fc: The source filesystem context being duplicated.
681 * Returns 0 on success or -ENOMEM on error.
683 static int smack_fs_context_dup(struct fs_context *fc,
684 struct fs_context *src_fc)
686 struct smack_mnt_opts *dst, *src = src_fc->security;
691 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
696 dst->fsdefault = src->fsdefault;
697 dst->fsfloor = src->fsfloor;
698 dst->fshat = src->fshat;
699 dst->fsroot = src->fsroot;
700 dst->fstransmute = src->fstransmute;
705 static const struct fs_parameter_spec smack_fs_parameters[] = {
706 fsparam_string("smackfsdef", Opt_fsdefault),
707 fsparam_string("smackfsdefault", Opt_fsdefault),
708 fsparam_string("smackfsfloor", Opt_fsfloor),
709 fsparam_string("smackfshat", Opt_fshat),
710 fsparam_string("smackfsroot", Opt_fsroot),
711 fsparam_string("smackfstransmute", Opt_fstransmute),
716 * smack_fs_context_parse_param - Parse a single mount parameter
717 * @fc: The new filesystem context being constructed.
718 * @param: The parameter.
720 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
723 static int smack_fs_context_parse_param(struct fs_context *fc,
724 struct fs_parameter *param)
726 struct fs_parse_result result;
729 opt = fs_parse(fc, smack_fs_parameters, param, &result);
733 rc = smack_add_opt(opt, param->string, &fc->security);
735 param->string = NULL;
739 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
741 char *from = options, *to = options;
745 char *next = strchr(from, ',');
754 token = match_opt_prefix(from, len, &arg);
755 if (token != Opt_error) {
756 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
757 rc = smack_add_opt(token, arg, mnt_opts);
761 smack_free_mnt_opts(*mnt_opts);
766 if (!first) { // copy with preceding comma
771 memmove(to, from, len);
784 * smack_set_mnt_opts - set Smack specific mount options
785 * @sb: the file system superblock
786 * @mnt_opts: Smack mount options
787 * @kern_flags: mount option from kernel space or user space
788 * @set_kern_flags: where to store converted mount opts
790 * Returns 0 on success, an error code on failure
792 * Allow filesystems with binary mount data to explicitly set Smack mount
795 static int smack_set_mnt_opts(struct super_block *sb,
797 unsigned long kern_flags,
798 unsigned long *set_kern_flags)
800 struct dentry *root = sb->s_root;
801 struct inode *inode = d_backing_inode(root);
802 struct superblock_smack *sp = smack_superblock(sb);
803 struct inode_smack *isp;
804 struct smack_known *skp;
805 struct smack_mnt_opts *opts = mnt_opts;
806 bool transmute = false;
808 if (sp->smk_flags & SMK_SB_INITIALIZED)
811 if (!smack_privileged(CAP_MAC_ADMIN)) {
813 * Unprivileged mounts don't get to specify Smack values.
818 * Unprivileged mounts get root and default from the caller.
820 skp = smk_of_current();
822 sp->smk_default = skp;
824 * For a handful of fs types with no user-controlled
825 * backing store it's okay to trust security labels
826 * in the filesystem. The rest are untrusted.
828 if (sb->s_user_ns != &init_user_ns &&
829 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
830 sb->s_magic != RAMFS_MAGIC) {
832 sp->smk_flags |= SMK_SB_UNTRUSTED;
836 sp->smk_flags |= SMK_SB_INITIALIZED;
839 if (opts->fsdefault) {
840 skp = smk_import_entry(opts->fsdefault, 0);
843 sp->smk_default = skp;
846 skp = smk_import_entry(opts->fsfloor, 0);
852 skp = smk_import_entry(opts->fshat, 0);
858 skp = smk_import_entry(opts->fsroot, 0);
863 if (opts->fstransmute) {
864 skp = smk_import_entry(opts->fstransmute, 0);
873 * Initialize the root inode.
875 init_inode_smack(inode, sp->smk_root);
878 isp = smack_inode(inode);
879 isp->smk_flags |= SMK_INODE_TRANSMUTE;
886 * smack_sb_statfs - Smack check on statfs
887 * @dentry: identifies the file system in question
889 * Returns 0 if current can read the floor of the filesystem,
890 * and error code otherwise
892 static int smack_sb_statfs(struct dentry *dentry)
894 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
896 struct smk_audit_info ad;
898 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
899 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
901 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
902 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
911 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
912 * @bprm: the exec information
914 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
916 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
918 struct inode *inode = file_inode(bprm->file);
919 struct task_smack *bsp = smack_cred(bprm->cred);
920 struct inode_smack *isp;
921 struct superblock_smack *sbsp;
924 isp = smack_inode(inode);
925 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
928 sbsp = smack_superblock(inode->i_sb);
929 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
930 isp->smk_task != sbsp->smk_root)
933 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
934 struct task_struct *tracer;
938 tracer = ptrace_parent(current);
939 if (likely(tracer != NULL))
940 rc = smk_ptrace_rule_check(tracer,
949 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
952 bsp->smk_task = isp->smk_task;
953 bprm->per_clear |= PER_CLEAR_ON_SETID;
955 /* Decide if this is a secure exec. */
956 if (bsp->smk_task != bsp->smk_forked)
957 bprm->secureexec = 1;
967 * smack_inode_alloc_security - allocate an inode blob
968 * @inode: the inode in need of a blob
972 static int smack_inode_alloc_security(struct inode *inode)
974 struct smack_known *skp = smk_of_current();
976 init_inode_smack(inode, skp);
981 * smack_inode_init_security - copy out the smack from an inode
982 * @inode: the newly created inode
983 * @dir: containing directory object
985 * @xattrs: where to put the attributes
986 * @xattr_count: current number of LSM-provided xattrs (updated)
988 * Returns 0 if it all works out, -ENOMEM if there's no memory
990 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
991 const struct qstr *qstr,
992 struct xattr *xattrs, int *xattr_count)
994 struct task_smack *tsp = smack_cred(current_cred());
995 struct smack_known *skp = smk_of_task(tsp);
996 struct smack_known *isp = smk_of_inode(inode);
997 struct smack_known *dsp = smk_of_inode(dir);
998 struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count);
1003 * If equal, transmuting already occurred in
1004 * smack_dentry_create_files_as(). No need to check again.
1006 if (tsp->smk_task != tsp->smk_transmuted) {
1008 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1014 * In addition to having smk_task equal to smk_transmuted,
1015 * if the access rule allows transmutation and the directory
1016 * requests transmutation then by all means transmute.
1017 * Mark the inode as changed.
1019 if ((tsp->smk_task == tsp->smk_transmuted) ||
1020 (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1021 smk_inode_transmutable(dir))) {
1022 struct xattr *xattr_transmute;
1025 * The caller of smack_dentry_create_files_as()
1026 * should have overridden the current cred, so the
1027 * inode label was already set correctly in
1028 * smack_inode_alloc_security().
1030 if (tsp->smk_task != tsp->smk_transmuted)
1032 xattr_transmute = lsm_get_xattr_slot(xattrs,
1034 if (xattr_transmute) {
1035 xattr_transmute->value = kmemdup(TRANS_TRUE,
1038 if (!xattr_transmute->value)
1041 xattr_transmute->value_len = TRANS_TRUE_SIZE;
1042 xattr_transmute->name = XATTR_SMACK_TRANSMUTE;
1046 xattr->value = kstrdup(isp->smk_known, GFP_NOFS);
1050 xattr->value_len = strlen(isp->smk_known);
1051 xattr->name = XATTR_SMACK_SUFFIX;
1058 * smack_inode_link - Smack check on link
1059 * @old_dentry: the existing object
1061 * @new_dentry: the new object
1063 * Returns 0 if access is permitted, an error code otherwise
1065 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1066 struct dentry *new_dentry)
1068 struct smack_known *isp;
1069 struct smk_audit_info ad;
1072 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1073 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1075 isp = smk_of_inode(d_backing_inode(old_dentry));
1076 rc = smk_curacc(isp, MAY_WRITE, &ad);
1077 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1079 if (rc == 0 && d_is_positive(new_dentry)) {
1080 isp = smk_of_inode(d_backing_inode(new_dentry));
1081 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1082 rc = smk_curacc(isp, MAY_WRITE, &ad);
1083 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1090 * smack_inode_unlink - Smack check on inode deletion
1091 * @dir: containing directory object
1092 * @dentry: file to unlink
1094 * Returns 0 if current can write the containing directory
1095 * and the object, error code otherwise
1097 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1099 struct inode *ip = d_backing_inode(dentry);
1100 struct smk_audit_info ad;
1103 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1104 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1107 * You need write access to the thing you're unlinking
1109 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1110 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1113 * You also need write access to the containing directory
1115 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1116 smk_ad_setfield_u_fs_inode(&ad, dir);
1117 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1118 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1124 * smack_inode_rmdir - Smack check on directory deletion
1125 * @dir: containing directory object
1126 * @dentry: directory to unlink
1128 * Returns 0 if current can write the containing directory
1129 * and the directory, error code otherwise
1131 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1133 struct smk_audit_info ad;
1136 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1137 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1140 * You need write access to the thing you're removing
1142 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1143 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1146 * You also need write access to the containing directory
1148 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1149 smk_ad_setfield_u_fs_inode(&ad, dir);
1150 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1151 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1158 * smack_inode_rename - Smack check on rename
1159 * @old_inode: unused
1160 * @old_dentry: the old object
1161 * @new_inode: unused
1162 * @new_dentry: the new object
1164 * Read and write access is required on both the old and
1167 * Returns 0 if access is permitted, an error code otherwise
1169 static int smack_inode_rename(struct inode *old_inode,
1170 struct dentry *old_dentry,
1171 struct inode *new_inode,
1172 struct dentry *new_dentry)
1175 struct smack_known *isp;
1176 struct smk_audit_info ad;
1178 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1179 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1181 isp = smk_of_inode(d_backing_inode(old_dentry));
1182 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1183 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1185 if (rc == 0 && d_is_positive(new_dentry)) {
1186 isp = smk_of_inode(d_backing_inode(new_dentry));
1187 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1188 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1189 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1195 * smack_inode_permission - Smack version of permission()
1196 * @inode: the inode in question
1197 * @mask: the access requested
1199 * This is the important Smack hook.
1201 * Returns 0 if access is permitted, an error code otherwise
1203 static int smack_inode_permission(struct inode *inode, int mask)
1205 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
1206 struct smk_audit_info ad;
1207 int no_block = mask & MAY_NOT_BLOCK;
1210 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1212 * No permission to check. Existence test. Yup, it's there.
1217 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1218 if (smk_of_inode(inode) != sbsp->smk_root)
1222 /* May be droppable after audit */
1225 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1226 smk_ad_setfield_u_fs_inode(&ad, inode);
1227 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1228 rc = smk_bu_inode(inode, mask, rc);
1233 * smack_inode_setattr - Smack check for setting attributes
1234 * @dentry: the object
1235 * @iattr: for the force flag
1237 * Returns 0 if access is permitted, an error code otherwise
1239 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1241 struct smk_audit_info ad;
1245 * Need to allow for clearing the setuid bit.
1247 if (iattr->ia_valid & ATTR_FORCE)
1249 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1250 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1252 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1253 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1258 * smack_inode_getattr - Smack check for getting attributes
1259 * @path: path to extract the info from
1261 * Returns 0 if access is permitted, an error code otherwise
1263 static int smack_inode_getattr(const struct path *path)
1265 struct smk_audit_info ad;
1266 struct inode *inode = d_backing_inode(path->dentry);
1269 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1270 smk_ad_setfield_u_fs_path(&ad, *path);
1271 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1272 rc = smk_bu_inode(inode, MAY_READ, rc);
1277 * smack_inode_setxattr - Smack check for setting xattrs
1278 * @idmap: idmap of the mount
1279 * @dentry: the object
1280 * @name: name of the attribute
1281 * @value: value of the attribute
1282 * @size: size of the value
1285 * This protects the Smack attribute explicitly.
1287 * Returns 0 if access is permitted, an error code otherwise
1289 static int smack_inode_setxattr(struct mnt_idmap *idmap,
1290 struct dentry *dentry, const char *name,
1291 const void *value, size_t size, int flags)
1293 struct smk_audit_info ad;
1294 struct smack_known *skp;
1296 int check_import = 0;
1301 * Check label validity here so import won't fail in post_setxattr
1303 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1304 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1305 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1308 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1309 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1313 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1315 if (size != TRANS_TRUE_SIZE ||
1316 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1319 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1321 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1324 if (rc == 0 && check_import) {
1325 skp = size ? smk_import_entry(value, size) : NULL;
1328 else if (skp == NULL || (check_star &&
1329 (skp == &smack_known_star || skp == &smack_known_web)))
1333 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1334 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1337 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1338 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1345 * smack_inode_post_setxattr - Apply the Smack update approved above
1347 * @name: attribute name
1348 * @value: attribute value
1349 * @size: attribute size
1352 * Set the pointer in the inode blob to the entry found
1353 * in the master label list.
1355 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1356 const void *value, size_t size, int flags)
1358 struct smack_known *skp;
1359 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1361 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1362 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1366 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1367 skp = smk_import_entry(value, size);
1369 isp->smk_inode = skp;
1370 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1371 skp = smk_import_entry(value, size);
1373 isp->smk_task = skp;
1374 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1375 skp = smk_import_entry(value, size);
1377 isp->smk_mmap = skp;
1384 * smack_inode_getxattr - Smack check on getxattr
1385 * @dentry: the object
1388 * Returns 0 if access is permitted, an error code otherwise
1390 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1392 struct smk_audit_info ad;
1395 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1396 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1398 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1399 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1404 * smack_inode_removexattr - Smack check on removexattr
1405 * @idmap: idmap of the mount
1406 * @dentry: the object
1407 * @name: name of the attribute
1409 * Removing the Smack attribute requires CAP_MAC_ADMIN
1411 * Returns 0 if access is permitted, an error code otherwise
1413 static int smack_inode_removexattr(struct mnt_idmap *idmap,
1414 struct dentry *dentry, const char *name)
1416 struct inode_smack *isp;
1417 struct smk_audit_info ad;
1420 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1421 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1422 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1423 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1424 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1425 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1426 if (!smack_privileged(CAP_MAC_ADMIN))
1429 rc = cap_inode_removexattr(idmap, dentry, name);
1434 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1435 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1437 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1438 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1442 isp = smack_inode(d_backing_inode(dentry));
1444 * Don't do anything special for these.
1445 * XATTR_NAME_SMACKIPIN
1446 * XATTR_NAME_SMACKIPOUT
1448 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1449 struct super_block *sbp = dentry->d_sb;
1450 struct superblock_smack *sbsp = smack_superblock(sbp);
1452 isp->smk_inode = sbsp->smk_default;
1453 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1454 isp->smk_task = NULL;
1455 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1456 isp->smk_mmap = NULL;
1457 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1458 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1464 * smack_inode_set_acl - Smack check for setting posix acls
1465 * @idmap: idmap of the mnt this request came from
1466 * @dentry: the object
1467 * @acl_name: name of the posix acl
1468 * @kacl: the posix acls
1470 * Returns 0 if access is permitted, an error code otherwise
1472 static int smack_inode_set_acl(struct mnt_idmap *idmap,
1473 struct dentry *dentry, const char *acl_name,
1474 struct posix_acl *kacl)
1476 struct smk_audit_info ad;
1479 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1480 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1482 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1483 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1488 * smack_inode_get_acl - Smack check for getting posix acls
1489 * @idmap: idmap of the mnt this request came from
1490 * @dentry: the object
1491 * @acl_name: name of the posix acl
1493 * Returns 0 if access is permitted, an error code otherwise
1495 static int smack_inode_get_acl(struct mnt_idmap *idmap,
1496 struct dentry *dentry, const char *acl_name)
1498 struct smk_audit_info ad;
1501 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1502 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1504 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1505 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1510 * smack_inode_remove_acl - Smack check for getting posix acls
1511 * @idmap: idmap of the mnt this request came from
1512 * @dentry: the object
1513 * @acl_name: name of the posix acl
1515 * Returns 0 if access is permitted, an error code otherwise
1517 static int smack_inode_remove_acl(struct mnt_idmap *idmap,
1518 struct dentry *dentry, const char *acl_name)
1520 struct smk_audit_info ad;
1523 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1524 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1526 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1527 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1532 * smack_inode_getsecurity - get smack xattrs
1533 * @idmap: idmap of the mount
1534 * @inode: the object
1535 * @name: attribute name
1536 * @buffer: where to put the result
1537 * @alloc: duplicate memory
1539 * Returns the size of the attribute or an error code
1541 static int smack_inode_getsecurity(struct mnt_idmap *idmap,
1542 struct inode *inode, const char *name,
1543 void **buffer, bool alloc)
1545 struct socket_smack *ssp;
1546 struct socket *sock;
1547 struct super_block *sbp;
1548 struct inode *ip = inode;
1549 struct smack_known *isp;
1550 struct inode_smack *ispp;
1554 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1555 isp = smk_of_inode(inode);
1556 } else if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) {
1557 ispp = smack_inode(inode);
1558 if (ispp->smk_flags & SMK_INODE_TRANSMUTE)
1564 * The rest of the Smack xattrs are only on sockets.
1567 if (sbp->s_magic != SOCKFS_MAGIC)
1570 sock = SOCKET_I(ip);
1571 if (sock == NULL || sock->sk == NULL)
1574 ssp = sock->sk->sk_security;
1576 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1578 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1585 label = isp->smk_known;
1587 label_len = strlen(label);
1590 *buffer = kstrdup(label, GFP_KERNEL);
1591 if (*buffer == NULL)
1600 * smack_inode_listsecurity - list the Smack attributes
1601 * @inode: the object
1602 * @buffer: where they go
1603 * @buffer_size: size of buffer
1605 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1608 int len = sizeof(XATTR_NAME_SMACK);
1610 if (buffer != NULL && len <= buffer_size)
1611 memcpy(buffer, XATTR_NAME_SMACK, len);
1617 * smack_inode_getsecid - Extract inode's security id
1618 * @inode: inode to extract the info from
1619 * @secid: where result will be saved
1621 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1623 struct smack_known *skp = smk_of_inode(inode);
1625 *secid = skp->smk_secid;
1633 * There is no smack_file_permission hook
1635 * Should access checks be done on each read or write?
1636 * UNICOS and SELinux say yes.
1637 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1639 * I'll say no for now. Smack does not do the frequent
1640 * label changing that SELinux does.
1644 * smack_file_alloc_security - assign a file security blob
1647 * The security blob for a file is a pointer to the master
1648 * label list, so no allocation is done.
1650 * f_security is the owner security information. It
1651 * isn't used on file access checks, it's for send_sigio.
1655 static int smack_file_alloc_security(struct file *file)
1657 struct smack_known **blob = smack_file(file);
1659 *blob = smk_of_current();
1664 * smack_file_ioctl - Smack check on ioctls
1669 * Relies heavily on the correct use of the ioctl command conventions.
1671 * Returns 0 if allowed, error code otherwise
1673 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1677 struct smk_audit_info ad;
1678 struct inode *inode = file_inode(file);
1680 if (unlikely(IS_PRIVATE(inode)))
1683 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1684 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1686 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1687 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1688 rc = smk_bu_file(file, MAY_WRITE, rc);
1691 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1692 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1693 rc = smk_bu_file(file, MAY_READ, rc);
1700 * smack_file_lock - Smack check on file locking
1704 * Returns 0 if current has lock access, error code otherwise
1706 static int smack_file_lock(struct file *file, unsigned int cmd)
1708 struct smk_audit_info ad;
1710 struct inode *inode = file_inode(file);
1712 if (unlikely(IS_PRIVATE(inode)))
1715 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1716 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1717 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1718 rc = smk_bu_file(file, MAY_LOCK, rc);
1723 * smack_file_fcntl - Smack check on fcntl
1725 * @cmd: what action to check
1728 * Generally these operations are harmless.
1729 * File locking operations present an obvious mechanism
1730 * for passing information, so they require write access.
1732 * Returns 0 if current has access, error code otherwise
1734 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1737 struct smk_audit_info ad;
1739 struct inode *inode = file_inode(file);
1741 if (unlikely(IS_PRIVATE(inode)))
1749 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1750 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1751 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1752 rc = smk_bu_file(file, MAY_LOCK, rc);
1756 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1757 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1758 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1759 rc = smk_bu_file(file, MAY_WRITE, rc);
1769 * smack_mmap_file - Check permissions for a mmap operation.
1770 * @file: contains the file structure for file to map (may be NULL).
1771 * @reqprot: contains the protection requested by the application.
1772 * @prot: contains the protection that will be applied by the kernel.
1773 * @flags: contains the operational flags.
1775 * The @file may be NULL, e.g. if mapping anonymous memory.
1777 * Return 0 if permission is granted.
1779 static int smack_mmap_file(struct file *file,
1780 unsigned long reqprot, unsigned long prot,
1781 unsigned long flags)
1783 struct smack_known *skp;
1784 struct smack_known *mkp;
1785 struct smack_rule *srp;
1786 struct task_smack *tsp;
1787 struct smack_known *okp;
1788 struct inode_smack *isp;
1789 struct superblock_smack *sbsp;
1798 if (unlikely(IS_PRIVATE(file_inode(file))))
1801 isp = smack_inode(file_inode(file));
1802 if (isp->smk_mmap == NULL)
1804 sbsp = smack_superblock(file_inode(file)->i_sb);
1805 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1806 isp->smk_mmap != sbsp->smk_root)
1808 mkp = isp->smk_mmap;
1810 tsp = smack_cred(current_cred());
1811 skp = smk_of_current();
1816 * For each Smack rule associated with the subject
1817 * label verify that the SMACK64MMAP also has access
1818 * to that rule's object label.
1820 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1821 okp = srp->smk_object;
1823 * Matching labels always allows access.
1825 if (mkp->smk_known == okp->smk_known)
1828 * If there is a matching local rule take
1829 * that into account as well.
1831 may = smk_access_entry(srp->smk_subject->smk_known,
1835 may = srp->smk_access;
1837 may &= srp->smk_access;
1839 * If may is zero the SMACK64MMAP subject can't
1840 * possibly have less access.
1846 * Fetch the global list entry.
1847 * If there isn't one a SMACK64MMAP subject
1848 * can't have as much access as current.
1850 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1852 if (mmay == -ENOENT) {
1857 * If there is a local entry it modifies the
1858 * potential access, too.
1860 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1862 if (tmay != -ENOENT)
1866 * If there is any access available to current that is
1867 * not available to a SMACK64MMAP subject
1870 if ((may | mmay) != mmay) {
1882 * smack_file_set_fowner - set the file security blob value
1883 * @file: object in question
1886 static void smack_file_set_fowner(struct file *file)
1888 struct smack_known **blob = smack_file(file);
1890 *blob = smk_of_current();
1894 * smack_file_send_sigiotask - Smack on sigio
1895 * @tsk: The target task
1896 * @fown: the object the signal come from
1899 * Allow a privileged task to get signals even if it shouldn't
1901 * Returns 0 if a subject with the object's smack could
1902 * write to the task, an error code otherwise.
1904 static int smack_file_send_sigiotask(struct task_struct *tsk,
1905 struct fown_struct *fown, int signum)
1907 struct smack_known **blob;
1908 struct smack_known *skp;
1909 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1910 const struct cred *tcred;
1913 struct smk_audit_info ad;
1916 * struct fown_struct is never outside the context of a struct file
1918 file = container_of(fown, struct file, f_owner);
1920 /* we don't log here as rc can be overriden */
1921 blob = smack_file(file);
1923 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1924 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1927 tcred = __task_cred(tsk);
1928 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1932 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1933 smk_ad_setfield_u_tsk(&ad, tsk);
1934 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1939 * smack_file_receive - Smack file receive check
1942 * Returns 0 if current has access, error code otherwise
1944 static int smack_file_receive(struct file *file)
1948 struct smk_audit_info ad;
1949 struct inode *inode = file_inode(file);
1950 struct socket *sock;
1951 struct task_smack *tsp;
1952 struct socket_smack *ssp;
1954 if (unlikely(IS_PRIVATE(inode)))
1957 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1958 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1960 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1961 sock = SOCKET_I(inode);
1962 ssp = sock->sk->sk_security;
1963 tsp = smack_cred(current_cred());
1965 * If the receiving process can't write to the
1966 * passed socket or if the passed socket can't
1967 * write to the receiving process don't accept
1968 * the passed socket.
1970 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1971 rc = smk_bu_file(file, may, rc);
1974 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1975 rc = smk_bu_file(file, may, rc);
1979 * This code relies on bitmasks.
1981 if (file->f_mode & FMODE_READ)
1983 if (file->f_mode & FMODE_WRITE)
1986 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1987 rc = smk_bu_file(file, may, rc);
1992 * smack_file_open - Smack dentry open processing
1995 * Set the security blob in the file structure.
1996 * Allow the open only if the task has read access. There are
1997 * many read operations (e.g. fstat) that you can do with an
1998 * fd even if you have the file open write-only.
2000 * Returns 0 if current has access, error code otherwise
2002 static int smack_file_open(struct file *file)
2004 struct task_smack *tsp = smack_cred(file->f_cred);
2005 struct inode *inode = file_inode(file);
2006 struct smk_audit_info ad;
2009 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
2010 smk_ad_setfield_u_fs_path(&ad, file->f_path);
2011 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
2012 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
2022 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
2023 * @cred: the new credentials
2024 * @gfp: the atomicity of any memory allocations
2026 * Prepare a blank set of credentials for modification. This must allocate all
2027 * the memory the LSM module might require such that cred_transfer() can
2028 * complete without error.
2030 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
2032 init_task_smack(smack_cred(cred), NULL, NULL);
2038 * smack_cred_free - "free" task-level security credentials
2039 * @cred: the credentials in question
2042 static void smack_cred_free(struct cred *cred)
2044 struct task_smack *tsp = smack_cred(cred);
2045 struct smack_rule *rp;
2046 struct list_head *l;
2047 struct list_head *n;
2049 smk_destroy_label_list(&tsp->smk_relabel);
2051 list_for_each_safe(l, n, &tsp->smk_rules) {
2052 rp = list_entry(l, struct smack_rule, list);
2053 list_del(&rp->list);
2054 kmem_cache_free(smack_rule_cache, rp);
2059 * smack_cred_prepare - prepare new set of credentials for modification
2060 * @new: the new credentials
2061 * @old: the original credentials
2062 * @gfp: the atomicity of any memory allocations
2064 * Prepare a new set of credentials for modification.
2066 static int smack_cred_prepare(struct cred *new, const struct cred *old,
2069 struct task_smack *old_tsp = smack_cred(old);
2070 struct task_smack *new_tsp = smack_cred(new);
2073 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
2075 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2079 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2085 * smack_cred_transfer - Transfer the old credentials to the new credentials
2086 * @new: the new credentials
2087 * @old: the original credentials
2089 * Fill in a set of blank credentials from another set of credentials.
2091 static void smack_cred_transfer(struct cred *new, const struct cred *old)
2093 struct task_smack *old_tsp = smack_cred(old);
2094 struct task_smack *new_tsp = smack_cred(new);
2096 new_tsp->smk_task = old_tsp->smk_task;
2097 new_tsp->smk_forked = old_tsp->smk_task;
2098 mutex_init(&new_tsp->smk_rules_lock);
2099 INIT_LIST_HEAD(&new_tsp->smk_rules);
2101 /* cbs copy rule list */
2105 * smack_cred_getsecid - get the secid corresponding to a creds structure
2106 * @cred: the object creds
2107 * @secid: where to put the result
2109 * Sets the secid to contain a u32 version of the smack label.
2111 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
2113 struct smack_known *skp;
2116 skp = smk_of_task(smack_cred(cred));
2117 *secid = skp->smk_secid;
2122 * smack_kernel_act_as - Set the subjective context in a set of credentials
2123 * @new: points to the set of credentials to be modified.
2124 * @secid: specifies the security ID to be set
2126 * Set the security data for a kernel service.
2128 static int smack_kernel_act_as(struct cred *new, u32 secid)
2130 struct task_smack *new_tsp = smack_cred(new);
2132 new_tsp->smk_task = smack_from_secid(secid);
2137 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2138 * @new: points to the set of credentials to be modified
2139 * @inode: points to the inode to use as a reference
2141 * Set the file creation context in a set of credentials to the same
2142 * as the objective context of the specified inode
2144 static int smack_kernel_create_files_as(struct cred *new,
2145 struct inode *inode)
2147 struct inode_smack *isp = smack_inode(inode);
2148 struct task_smack *tsp = smack_cred(new);
2150 tsp->smk_forked = isp->smk_inode;
2151 tsp->smk_task = tsp->smk_forked;
2156 * smk_curacc_on_task - helper to log task related access
2157 * @p: the task object
2158 * @access: the access requested
2159 * @caller: name of the calling function for audit
2161 * Return 0 if access is permitted
2163 static int smk_curacc_on_task(struct task_struct *p, int access,
2166 struct smk_audit_info ad;
2167 struct smack_known *skp = smk_of_task_struct_obj(p);
2170 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2171 smk_ad_setfield_u_tsk(&ad, p);
2172 rc = smk_curacc(skp, access, &ad);
2173 rc = smk_bu_task(p, access, rc);
2178 * smack_task_setpgid - Smack check on setting pgid
2179 * @p: the task object
2182 * Return 0 if write access is permitted
2184 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2186 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2190 * smack_task_getpgid - Smack access check for getpgid
2191 * @p: the object task
2193 * Returns 0 if current can read the object task, error code otherwise
2195 static int smack_task_getpgid(struct task_struct *p)
2197 return smk_curacc_on_task(p, MAY_READ, __func__);
2201 * smack_task_getsid - Smack access check for getsid
2202 * @p: the object task
2204 * Returns 0 if current can read the object task, error code otherwise
2206 static int smack_task_getsid(struct task_struct *p)
2208 return smk_curacc_on_task(p, MAY_READ, __func__);
2212 * smack_current_getsecid_subj - get the subjective secid of the current task
2213 * @secid: where to put the result
2215 * Sets the secid to contain a u32 version of the task's subjective smack label.
2217 static void smack_current_getsecid_subj(u32 *secid)
2219 struct smack_known *skp = smk_of_current();
2221 *secid = skp->smk_secid;
2225 * smack_task_getsecid_obj - get the objective secid of the task
2227 * @secid: where to put the result
2229 * Sets the secid to contain a u32 version of the task's objective smack label.
2231 static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2233 struct smack_known *skp = smk_of_task_struct_obj(p);
2235 *secid = skp->smk_secid;
2239 * smack_task_setnice - Smack check on setting nice
2240 * @p: the task object
2243 * Return 0 if write access is permitted
2245 static int smack_task_setnice(struct task_struct *p, int nice)
2247 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2251 * smack_task_setioprio - Smack check on setting ioprio
2252 * @p: the task object
2255 * Return 0 if write access is permitted
2257 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2259 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2263 * smack_task_getioprio - Smack check on reading ioprio
2264 * @p: the task object
2266 * Return 0 if read access is permitted
2268 static int smack_task_getioprio(struct task_struct *p)
2270 return smk_curacc_on_task(p, MAY_READ, __func__);
2274 * smack_task_setscheduler - Smack check on setting scheduler
2275 * @p: the task object
2277 * Return 0 if read access is permitted
2279 static int smack_task_setscheduler(struct task_struct *p)
2281 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2285 * smack_task_getscheduler - Smack check on reading scheduler
2286 * @p: the task object
2288 * Return 0 if read access is permitted
2290 static int smack_task_getscheduler(struct task_struct *p)
2292 return smk_curacc_on_task(p, MAY_READ, __func__);
2296 * smack_task_movememory - Smack check on moving memory
2297 * @p: the task object
2299 * Return 0 if write access is permitted
2301 static int smack_task_movememory(struct task_struct *p)
2303 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2307 * smack_task_kill - Smack check on signal delivery
2308 * @p: the task object
2311 * @cred: identifies the cred to use in lieu of current's
2313 * Return 0 if write access is permitted
2316 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2317 int sig, const struct cred *cred)
2319 struct smk_audit_info ad;
2320 struct smack_known *skp;
2321 struct smack_known *tkp = smk_of_task_struct_obj(p);
2325 return 0; /* null signal; existence test */
2327 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2328 smk_ad_setfield_u_tsk(&ad, p);
2330 * Sending a signal requires that the sender
2331 * can write the receiver.
2334 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2335 rc = smk_bu_task(p, MAY_DELIVER, rc);
2339 * If the cred isn't NULL we're dealing with some USB IO
2340 * specific behavior. This is not clean. For one thing
2341 * we can't take privilege into account.
2343 skp = smk_of_task(smack_cred(cred));
2344 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2345 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2350 * smack_task_to_inode - copy task smack into the inode blob
2351 * @p: task to copy from
2352 * @inode: inode to copy to
2354 * Sets the smack pointer in the inode security blob
2356 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2358 struct inode_smack *isp = smack_inode(inode);
2359 struct smack_known *skp = smk_of_task_struct_obj(p);
2361 isp->smk_inode = skp;
2362 isp->smk_flags |= SMK_INODE_INSTANT;
2370 * smack_sk_alloc_security - Allocate a socket blob
2373 * @gfp_flags: memory allocation flags
2375 * Assign Smack pointers to current
2377 * Returns 0 on success, -ENOMEM is there's no memory
2379 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2381 struct smack_known *skp = smk_of_current();
2382 struct socket_smack *ssp;
2384 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2389 * Sockets created by kernel threads receive web label.
2391 if (unlikely(current->flags & PF_KTHREAD)) {
2392 ssp->smk_in = &smack_known_web;
2393 ssp->smk_out = &smack_known_web;
2398 ssp->smk_packet = NULL;
2400 sk->sk_security = ssp;
2406 * smack_sk_free_security - Free a socket blob
2409 * Clears the blob pointer
2411 static void smack_sk_free_security(struct sock *sk)
2413 #ifdef SMACK_IPV6_PORT_LABELING
2414 struct smk_port_label *spp;
2416 if (sk->sk_family == PF_INET6) {
2418 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2419 if (spp->smk_sock != sk)
2421 spp->smk_can_reuse = 1;
2427 kfree(sk->sk_security);
2431 * smack_sk_clone_security - Copy security context
2432 * @sk: the old socket
2433 * @newsk: the new socket
2435 * Copy the security context of the old socket pointer to the cloned
2437 static void smack_sk_clone_security(const struct sock *sk, struct sock *newsk)
2439 struct socket_smack *ssp_old = sk->sk_security;
2440 struct socket_smack *ssp_new = newsk->sk_security;
2442 *ssp_new = *ssp_old;
2446 * smack_ipv4host_label - check host based restrictions
2447 * @sip: the object end
2449 * looks for host based access restrictions
2451 * This version will only be appropriate for really small sets of single label
2452 * hosts. The caller is responsible for ensuring that the RCU read lock is
2453 * taken before calling this function.
2455 * Returns the label of the far end or NULL if it's not special.
2457 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2459 struct smk_net4addr *snp;
2460 struct in_addr *siap = &sip->sin_addr;
2462 if (siap->s_addr == 0)
2465 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2467 * we break after finding the first match because
2468 * the list is sorted from longest to shortest mask
2469 * so we have found the most specific match
2471 if (snp->smk_host.s_addr ==
2472 (siap->s_addr & snp->smk_mask.s_addr))
2473 return snp->smk_label;
2479 * smk_ipv6_localhost - Check for local ipv6 host address
2482 * Returns boolean true if this is the localhost address
2484 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2486 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2487 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2489 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2490 ntohs(be16p[7]) == 1)
2496 * smack_ipv6host_label - check host based restrictions
2497 * @sip: the object end
2499 * looks for host based access restrictions
2501 * This version will only be appropriate for really small sets of single label
2502 * hosts. The caller is responsible for ensuring that the RCU read lock is
2503 * taken before calling this function.
2505 * Returns the label of the far end or NULL if it's not special.
2507 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2509 struct smk_net6addr *snp;
2510 struct in6_addr *sap = &sip->sin6_addr;
2515 * It's local. Don't look for a host label.
2517 if (smk_ipv6_localhost(sip))
2520 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2522 * If the label is NULL the entry has
2523 * been renounced. Ignore it.
2525 if (snp->smk_label == NULL)
2528 * we break after finding the first match because
2529 * the list is sorted from longest to shortest mask
2530 * so we have found the most specific match
2532 for (found = 1, i = 0; i < 8; i++) {
2533 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2534 snp->smk_host.s6_addr16[i]) {
2540 return snp->smk_label;
2547 * smack_netlbl_add - Set the secattr on a socket
2550 * Attach the outbound smack value (smk_out) to the socket.
2552 * Returns 0 on success or an error code
2554 static int smack_netlbl_add(struct sock *sk)
2556 struct socket_smack *ssp = sk->sk_security;
2557 struct smack_known *skp = ssp->smk_out;
2561 bh_lock_sock_nested(sk);
2563 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2566 ssp->smk_state = SMK_NETLBL_LABELED;
2569 ssp->smk_state = SMK_NETLBL_REQSKB;
2581 * smack_netlbl_delete - Remove the secattr from a socket
2584 * Remove the outbound smack value from a socket
2586 static void smack_netlbl_delete(struct sock *sk)
2588 struct socket_smack *ssp = sk->sk_security;
2591 * Take the label off the socket if one is set.
2593 if (ssp->smk_state != SMK_NETLBL_LABELED)
2597 bh_lock_sock_nested(sk);
2598 netlbl_sock_delattr(sk);
2601 ssp->smk_state = SMK_NETLBL_UNLABELED;
2605 * smk_ipv4_check - Perform IPv4 host access checks
2607 * @sap: the destination address
2609 * Set the correct secattr for the given socket based on the destination
2610 * address and perform any outbound access checks needed.
2612 * Returns 0 on success or an error code.
2615 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2617 struct smack_known *skp;
2619 struct smack_known *hkp;
2620 struct socket_smack *ssp = sk->sk_security;
2621 struct smk_audit_info ad;
2624 hkp = smack_ipv4host_label(sap);
2627 struct lsm_network_audit net;
2629 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2630 ad.a.u.net->family = sap->sin_family;
2631 ad.a.u.net->dport = sap->sin_port;
2632 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2635 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2636 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2638 * Clear the socket netlabel if it's set.
2641 smack_netlbl_delete(sk);
2649 * smk_ipv6_check - check Smack access
2650 * @subject: subject Smack label
2651 * @object: object Smack label
2653 * @act: the action being taken
2655 * Check an IPv6 access
2657 static int smk_ipv6_check(struct smack_known *subject,
2658 struct smack_known *object,
2659 struct sockaddr_in6 *address, int act)
2662 struct lsm_network_audit net;
2664 struct smk_audit_info ad;
2668 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2669 ad.a.u.net->family = PF_INET6;
2670 ad.a.u.net->dport = address->sin6_port;
2671 if (act == SMK_RECEIVING)
2672 ad.a.u.net->v6info.saddr = address->sin6_addr;
2674 ad.a.u.net->v6info.daddr = address->sin6_addr;
2676 rc = smk_access(subject, object, MAY_WRITE, &ad);
2677 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2681 #ifdef SMACK_IPV6_PORT_LABELING
2683 * smk_ipv6_port_label - Smack port access table management
2687 * Create or update the port list entry
2689 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2691 struct sock *sk = sock->sk;
2692 struct sockaddr_in6 *addr6;
2693 struct socket_smack *ssp = sock->sk->sk_security;
2694 struct smk_port_label *spp;
2695 unsigned short port = 0;
2697 if (address == NULL) {
2699 * This operation is changing the Smack information
2700 * on the bound socket. Take the changes to the port
2704 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2705 if (sk != spp->smk_sock)
2707 spp->smk_in = ssp->smk_in;
2708 spp->smk_out = ssp->smk_out;
2713 * A NULL address is only used for updating existing
2714 * bound entries. If there isn't one, it's OK.
2720 addr6 = (struct sockaddr_in6 *)address;
2721 port = ntohs(addr6->sin6_port);
2723 * This is a special case that is safely ignored.
2729 * Look for an existing port list entry.
2730 * This is an indication that a port is getting reused.
2733 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2734 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2736 if (spp->smk_can_reuse != 1) {
2740 spp->smk_port = port;
2742 spp->smk_in = ssp->smk_in;
2743 spp->smk_out = ssp->smk_out;
2744 spp->smk_can_reuse = 0;
2750 * A new port entry is required.
2752 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2756 spp->smk_port = port;
2758 spp->smk_in = ssp->smk_in;
2759 spp->smk_out = ssp->smk_out;
2760 spp->smk_sock_type = sock->type;
2761 spp->smk_can_reuse = 0;
2763 mutex_lock(&smack_ipv6_lock);
2764 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2765 mutex_unlock(&smack_ipv6_lock);
2770 * smk_ipv6_port_check - check Smack port access
2773 * @act: the action being taken
2775 * Create or update the port list entry
2777 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2780 struct smk_port_label *spp;
2781 struct socket_smack *ssp = sk->sk_security;
2782 struct smack_known *skp = NULL;
2783 unsigned short port;
2784 struct smack_known *object;
2786 if (act == SMK_RECEIVING) {
2787 skp = smack_ipv6host_label(address);
2788 object = ssp->smk_in;
2791 object = smack_ipv6host_label(address);
2795 * The other end is a single label host.
2797 if (skp != NULL && object != NULL)
2798 return smk_ipv6_check(skp, object, address, act);
2800 skp = smack_net_ambient;
2802 object = smack_net_ambient;
2805 * It's remote, so port lookup does no good.
2807 if (!smk_ipv6_localhost(address))
2808 return smk_ipv6_check(skp, object, address, act);
2811 * It's local so the send check has to have passed.
2813 if (act == SMK_RECEIVING)
2816 port = ntohs(address->sin6_port);
2818 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2819 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2821 object = spp->smk_in;
2822 if (act == SMK_CONNECTING)
2823 ssp->smk_packet = spp->smk_out;
2828 return smk_ipv6_check(skp, object, address, act);
2833 * smack_inode_setsecurity - set smack xattrs
2834 * @inode: the object
2835 * @name: attribute name
2836 * @value: attribute value
2837 * @size: size of the attribute
2840 * Sets the named attribute in the appropriate blob
2842 * Returns 0 on success, or an error code
2844 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2845 const void *value, size_t size, int flags)
2847 struct smack_known *skp;
2848 struct inode_smack *nsp = smack_inode(inode);
2849 struct socket_smack *ssp;
2850 struct socket *sock;
2853 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2856 skp = smk_import_entry(value, size);
2858 return PTR_ERR(skp);
2860 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2861 nsp->smk_inode = skp;
2862 nsp->smk_flags |= SMK_INODE_INSTANT;
2866 * The rest of the Smack xattrs are only on sockets.
2868 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2871 sock = SOCKET_I(inode);
2872 if (sock == NULL || sock->sk == NULL)
2875 ssp = sock->sk->sk_security;
2877 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2879 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2881 if (sock->sk->sk_family == PF_INET) {
2882 rc = smack_netlbl_add(sock->sk);
2885 "Smack: \"%s\" netlbl error %d.\n",
2891 #ifdef SMACK_IPV6_PORT_LABELING
2892 if (sock->sk->sk_family == PF_INET6)
2893 smk_ipv6_port_label(sock, NULL);
2900 * smack_socket_post_create - finish socket setup
2902 * @family: protocol family
2907 * Sets the netlabel information on the socket
2909 * Returns 0 on success, and error code otherwise
2911 static int smack_socket_post_create(struct socket *sock, int family,
2912 int type, int protocol, int kern)
2914 struct socket_smack *ssp;
2916 if (sock->sk == NULL)
2920 * Sockets created by kernel threads receive web label.
2922 if (unlikely(current->flags & PF_KTHREAD)) {
2923 ssp = sock->sk->sk_security;
2924 ssp->smk_in = &smack_known_web;
2925 ssp->smk_out = &smack_known_web;
2928 if (family != PF_INET)
2931 * Set the outbound netlbl.
2933 return smack_netlbl_add(sock->sk);
2937 * smack_socket_socketpair - create socket pair
2938 * @socka: one socket
2939 * @sockb: another socket
2941 * Cross reference the peer labels for SO_PEERSEC
2945 static int smack_socket_socketpair(struct socket *socka,
2946 struct socket *sockb)
2948 struct socket_smack *asp = socka->sk->sk_security;
2949 struct socket_smack *bsp = sockb->sk->sk_security;
2951 asp->smk_packet = bsp->smk_out;
2952 bsp->smk_packet = asp->smk_out;
2957 #ifdef SMACK_IPV6_PORT_LABELING
2959 * smack_socket_bind - record port binding information.
2961 * @address: the port address
2962 * @addrlen: size of the address
2964 * Records the label bound to a port.
2966 * Returns 0 on success, and error code otherwise
2968 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2971 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2972 if (addrlen < SIN6_LEN_RFC2133 ||
2973 address->sa_family != AF_INET6)
2975 smk_ipv6_port_label(sock, address);
2979 #endif /* SMACK_IPV6_PORT_LABELING */
2982 * smack_socket_connect - connect access check
2984 * @sap: the other end
2985 * @addrlen: size of sap
2987 * Verifies that a connection may be possible
2989 * Returns 0 on success, and error code otherwise
2991 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2996 if (sock->sk == NULL)
2998 if (sock->sk->sk_family != PF_INET &&
2999 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
3001 if (addrlen < offsetofend(struct sockaddr, sa_family))
3003 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
3004 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
3005 struct smack_known *rsp = NULL;
3007 if (addrlen < SIN6_LEN_RFC2133)
3009 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
3010 rsp = smack_ipv6host_label(sip);
3012 struct socket_smack *ssp = sock->sk->sk_security;
3014 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
3017 #ifdef SMACK_IPV6_PORT_LABELING
3018 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
3023 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
3025 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
3030 * smack_flags_to_may - convert S_ to MAY_ values
3031 * @flags: the S_ value
3033 * Returns the equivalent MAY_ value
3035 static int smack_flags_to_may(int flags)
3039 if (flags & S_IRUGO)
3041 if (flags & S_IWUGO)
3043 if (flags & S_IXUGO)
3050 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
3055 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
3057 struct smack_known **blob = smack_msg_msg(msg);
3059 *blob = smk_of_current();
3064 * smack_of_ipc - the smack pointer for the ipc
3067 * Returns a pointer to the smack value
3069 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
3071 struct smack_known **blob = smack_ipc(isp);
3077 * smack_ipc_alloc_security - Set the security blob for ipc
3082 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
3084 struct smack_known **blob = smack_ipc(isp);
3086 *blob = smk_of_current();
3091 * smk_curacc_shm : check if current has access on shm
3093 * @access : access requested
3095 * Returns 0 if current has the requested access, error code otherwise
3097 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
3099 struct smack_known *ssp = smack_of_ipc(isp);
3100 struct smk_audit_info ad;
3104 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3105 ad.a.u.ipc_id = isp->id;
3107 rc = smk_curacc(ssp, access, &ad);
3108 rc = smk_bu_current("shm", ssp, access, rc);
3113 * smack_shm_associate - Smack access check for shm
3115 * @shmflg: access requested
3117 * Returns 0 if current has the requested access, error code otherwise
3119 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3123 may = smack_flags_to_may(shmflg);
3124 return smk_curacc_shm(isp, may);
3128 * smack_shm_shmctl - Smack access check for shm
3130 * @cmd: what it wants to do
3132 * Returns 0 if current has the requested access, error code otherwise
3134 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3148 may = MAY_READWRITE;
3153 * System level information.
3159 return smk_curacc_shm(isp, may);
3163 * smack_shm_shmat - Smack access for shmat
3166 * @shmflg: access requested
3168 * Returns 0 if current has the requested access, error code otherwise
3170 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3175 may = smack_flags_to_may(shmflg);
3176 return smk_curacc_shm(isp, may);
3180 * smk_curacc_sem : check if current has access on sem
3182 * @access : access requested
3184 * Returns 0 if current has the requested access, error code otherwise
3186 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3188 struct smack_known *ssp = smack_of_ipc(isp);
3189 struct smk_audit_info ad;
3193 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3194 ad.a.u.ipc_id = isp->id;
3196 rc = smk_curacc(ssp, access, &ad);
3197 rc = smk_bu_current("sem", ssp, access, rc);
3202 * smack_sem_associate - Smack access check for sem
3204 * @semflg: access requested
3206 * Returns 0 if current has the requested access, error code otherwise
3208 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3212 may = smack_flags_to_may(semflg);
3213 return smk_curacc_sem(isp, may);
3217 * smack_sem_semctl - Smack access check for sem
3219 * @cmd: what it wants to do
3221 * Returns 0 if current has the requested access, error code otherwise
3223 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3242 may = MAY_READWRITE;
3247 * System level information
3254 return smk_curacc_sem(isp, may);
3258 * smack_sem_semop - Smack checks of semaphore operations
3264 * Treated as read and write in all cases.
3266 * Returns 0 if access is allowed, error code otherwise
3268 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3269 unsigned nsops, int alter)
3271 return smk_curacc_sem(isp, MAY_READWRITE);
3275 * smk_curacc_msq : helper to check if current has access on msq
3277 * @access : access requested
3279 * return 0 if current has access, error otherwise
3281 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3283 struct smack_known *msp = smack_of_ipc(isp);
3284 struct smk_audit_info ad;
3288 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3289 ad.a.u.ipc_id = isp->id;
3291 rc = smk_curacc(msp, access, &ad);
3292 rc = smk_bu_current("msq", msp, access, rc);
3297 * smack_msg_queue_associate - Smack access check for msg_queue
3299 * @msqflg: access requested
3301 * Returns 0 if current has the requested access, error code otherwise
3303 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3307 may = smack_flags_to_may(msqflg);
3308 return smk_curacc_msq(isp, may);
3312 * smack_msg_queue_msgctl - Smack access check for msg_queue
3314 * @cmd: what it wants to do
3316 * Returns 0 if current has the requested access, error code otherwise
3318 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3330 may = MAY_READWRITE;
3335 * System level information
3342 return smk_curacc_msq(isp, may);
3346 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3349 * @msqflg: access requested
3351 * Returns 0 if current has the requested access, error code otherwise
3353 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3358 may = smack_flags_to_may(msqflg);
3359 return smk_curacc_msq(isp, may);
3363 * smack_msg_queue_msgrcv - Smack access check for msg_queue
3370 * Returns 0 if current has read and write access, error code otherwise
3372 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp,
3373 struct msg_msg *msg,
3374 struct task_struct *target, long type,
3377 return smk_curacc_msq(isp, MAY_READWRITE);
3381 * smack_ipc_permission - Smack access for ipc_permission()
3382 * @ipp: the object permissions
3383 * @flag: access requested
3385 * Returns 0 if current has read and write access, error code otherwise
3387 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3389 struct smack_known **blob = smack_ipc(ipp);
3390 struct smack_known *iskp = *blob;
3391 int may = smack_flags_to_may(flag);
3392 struct smk_audit_info ad;
3396 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3397 ad.a.u.ipc_id = ipp->id;
3399 rc = smk_curacc(iskp, may, &ad);
3400 rc = smk_bu_current("svipc", iskp, may, rc);
3405 * smack_ipc_getsecid - Extract smack security id
3406 * @ipp: the object permissions
3407 * @secid: where result will be saved
3409 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3411 struct smack_known **blob = smack_ipc(ipp);
3412 struct smack_known *iskp = *blob;
3414 *secid = iskp->smk_secid;
3418 * smack_d_instantiate - Make sure the blob is correct on an inode
3419 * @opt_dentry: dentry where inode will be attached
3420 * @inode: the object
3422 * Set the inode's security blob if it hasn't been done already.
3424 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3426 struct super_block *sbp;
3427 struct superblock_smack *sbsp;
3428 struct inode_smack *isp;
3429 struct smack_known *skp;
3430 struct smack_known *ckp = smk_of_current();
3431 struct smack_known *final;
3432 char trattr[TRANS_TRUE_SIZE];
3440 isp = smack_inode(inode);
3443 * If the inode is already instantiated
3444 * take the quick way out
3446 if (isp->smk_flags & SMK_INODE_INSTANT)
3450 sbsp = smack_superblock(sbp);
3452 * We're going to use the superblock default label
3453 * if there's no label on the file.
3455 final = sbsp->smk_default;
3458 * If this is the root inode the superblock
3459 * may be in the process of initialization.
3460 * If that is the case use the root value out
3461 * of the superblock.
3463 if (opt_dentry->d_parent == opt_dentry) {
3464 switch (sbp->s_magic) {
3465 case CGROUP_SUPER_MAGIC:
3466 case CGROUP2_SUPER_MAGIC:
3468 * The cgroup filesystem is never mounted,
3469 * so there's no opportunity to set the mount
3472 sbsp->smk_root = &smack_known_star;
3473 sbsp->smk_default = &smack_known_star;
3474 isp->smk_inode = sbsp->smk_root;
3478 * What about shmem/tmpfs anonymous files with dentry
3479 * obtained from d_alloc_pseudo()?
3481 isp->smk_inode = smk_of_current();
3484 isp->smk_inode = smk_of_current();
3488 * Socket access is controlled by the socket
3489 * structures associated with the task involved.
3491 isp->smk_inode = &smack_known_star;
3494 isp->smk_inode = sbsp->smk_root;
3497 isp->smk_flags |= SMK_INODE_INSTANT;
3502 * This is pretty hackish.
3503 * Casey says that we shouldn't have to do
3504 * file system specific code, but it does help
3505 * with keeping it simple.
3507 switch (sbp->s_magic) {
3509 case CGROUP_SUPER_MAGIC:
3510 case CGROUP2_SUPER_MAGIC:
3512 * Casey says that it's a little embarrassing
3513 * that the smack file system doesn't do
3514 * extended attributes.
3516 * Cgroupfs is special
3518 final = &smack_known_star;
3520 case DEVPTS_SUPER_MAGIC:
3522 * devpts seems content with the label of the task.
3523 * Programs that change smack have to treat the
3528 case PROC_SUPER_MAGIC:
3530 * Casey says procfs appears not to care.
3531 * The superblock default suffices.
3536 * Device labels should come from the filesystem,
3537 * but watch out, because they're volitile,
3538 * getting recreated on every reboot.
3540 final = &smack_known_star;
3542 * If a smack value has been set we want to use it,
3543 * but since tmpfs isn't giving us the opportunity
3544 * to set mount options simulate setting the
3545 * superblock default.
3550 * This isn't an understood special case.
3551 * Get the value from the xattr.
3555 * UNIX domain sockets use lower level socket data.
3557 if (S_ISSOCK(inode->i_mode)) {
3558 final = &smack_known_star;
3562 * No xattr support means, alas, no SMACK label.
3563 * Use the aforeapplied default.
3564 * It would be curious if the label of the task
3565 * does not match that assigned.
3567 if (!(inode->i_opflags & IOP_XATTR))
3570 * Get the dentry for xattr.
3572 dp = dget(opt_dentry);
3573 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3574 if (!IS_ERR_OR_NULL(skp))
3578 * Transmuting directory
3580 if (S_ISDIR(inode->i_mode)) {
3582 * If this is a new directory and the label was
3583 * transmuted when the inode was initialized
3584 * set the transmute attribute on the directory
3585 * and mark the inode.
3587 * If there is a transmute attribute on the
3588 * directory mark the inode.
3590 rc = __vfs_getxattr(dp, inode,
3591 XATTR_NAME_SMACKTRANSMUTE, trattr,
3593 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3594 TRANS_TRUE_SIZE) != 0)
3597 transflag = SMK_INODE_TRANSMUTE;
3600 * Don't let the exec or mmap label be "*" or "@".
3602 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3603 if (IS_ERR(skp) || skp == &smack_known_star ||
3604 skp == &smack_known_web)
3606 isp->smk_task = skp;
3608 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3609 if (IS_ERR(skp) || skp == &smack_known_star ||
3610 skp == &smack_known_web)
3612 isp->smk_mmap = skp;
3619 isp->smk_inode = ckp;
3621 isp->smk_inode = final;
3623 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3629 * smack_getprocattr - Smack process attribute access
3630 * @p: the object task
3631 * @name: the name of the attribute in /proc/.../attr
3632 * @value: where to put the result
3634 * Places a copy of the task Smack into value
3636 * Returns the length of the smack label or an error code
3638 static int smack_getprocattr(struct task_struct *p, const char *name, char **value)
3640 struct smack_known *skp = smk_of_task_struct_obj(p);
3644 if (strcmp(name, "current") != 0)
3647 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3657 * smack_setprocattr - Smack process attribute setting
3658 * @name: the name of the attribute in /proc/.../attr
3659 * @value: the value to set
3660 * @size: the size of the value
3662 * Sets the Smack value of the task. Only setting self
3663 * is permitted and only with privilege
3665 * Returns the length of the smack label or an error code
3667 static int smack_setprocattr(const char *name, void *value, size_t size)
3669 struct task_smack *tsp = smack_cred(current_cred());
3671 struct smack_known *skp;
3672 struct smack_known_list_elem *sklep;
3675 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3678 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3681 if (strcmp(name, "current") != 0)
3684 skp = smk_import_entry(value, size);
3686 return PTR_ERR(skp);
3689 * No process is ever allowed the web ("@") label
3690 * and the star ("*") label.
3692 if (skp == &smack_known_web || skp == &smack_known_star)
3695 if (!smack_privileged(CAP_MAC_ADMIN)) {
3697 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3698 if (sklep->smk_label == skp) {
3706 new = prepare_creds();
3710 tsp = smack_cred(new);
3711 tsp->smk_task = skp;
3713 * process can change its label only once
3715 smk_destroy_label_list(&tsp->smk_relabel);
3722 * smack_unix_stream_connect - Smack access on UDS
3724 * @other: the other sock
3727 * Return 0 if a subject with the smack of sock could access
3728 * an object with the smack of other, otherwise an error code
3730 static int smack_unix_stream_connect(struct sock *sock,
3731 struct sock *other, struct sock *newsk)
3733 struct smack_known *skp;
3734 struct smack_known *okp;
3735 struct socket_smack *ssp = sock->sk_security;
3736 struct socket_smack *osp = other->sk_security;
3737 struct socket_smack *nsp = newsk->sk_security;
3738 struct smk_audit_info ad;
3741 struct lsm_network_audit net;
3744 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3748 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3749 smk_ad_setfield_u_net_sk(&ad, other);
3751 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3752 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3756 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3757 rc = smk_bu_note("UDS connect", okp, skp,
3763 * Cross reference the peer labels for SO_PEERSEC.
3766 nsp->smk_packet = ssp->smk_out;
3767 ssp->smk_packet = osp->smk_out;
3774 * smack_unix_may_send - Smack access on UDS
3776 * @other: the other socket
3778 * Return 0 if a subject with the smack of sock could access
3779 * an object with the smack of other, otherwise an error code
3781 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3783 struct socket_smack *ssp = sock->sk->sk_security;
3784 struct socket_smack *osp = other->sk->sk_security;
3785 struct smk_audit_info ad;
3789 struct lsm_network_audit net;
3791 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3792 smk_ad_setfield_u_net_sk(&ad, other->sk);
3795 if (smack_privileged(CAP_MAC_OVERRIDE))
3798 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3799 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3804 * smack_socket_sendmsg - Smack check based on destination host
3807 * @size: the size of the message
3809 * Return 0 if the current subject can write to the destination host.
3810 * For IPv4 this is only a question if the destination is a single label host.
3811 * For IPv6 this is a check against the label of the port.
3813 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3816 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3817 #if IS_ENABLED(CONFIG_IPV6)
3818 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3820 #ifdef SMACK_IPV6_SECMARK_LABELING
3821 struct socket_smack *ssp = sock->sk->sk_security;
3822 struct smack_known *rsp;
3827 * Perfectly reasonable for this to be NULL
3832 switch (sock->sk->sk_family) {
3834 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3835 sip->sin_family != AF_INET)
3837 rc = smk_ipv4_check(sock->sk, sip);
3839 #if IS_ENABLED(CONFIG_IPV6)
3841 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3842 sap->sin6_family != AF_INET6)
3844 #ifdef SMACK_IPV6_SECMARK_LABELING
3845 rsp = smack_ipv6host_label(sap);
3847 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3850 #ifdef SMACK_IPV6_PORT_LABELING
3851 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3853 #endif /* IS_ENABLED(CONFIG_IPV6) */
3860 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3861 * @sap: netlabel secattr
3862 * @ssp: socket security information
3864 * Returns a pointer to a Smack label entry found on the label list.
3866 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3867 struct socket_smack *ssp)
3869 struct smack_known *skp;
3875 * Netlabel found it in the cache.
3877 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3878 return (struct smack_known *)sap->cache->data;
3880 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3882 * Looks like a fallback, which gives us a secid.
3884 return smack_from_secid(sap->attr.secid);
3886 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3888 * Looks like a CIPSO packet.
3889 * If there are flags but no level netlabel isn't
3890 * behaving the way we expect it to.
3892 * Look it up in the label table
3893 * Without guidance regarding the smack value
3894 * for the packet fall back on the network
3898 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3899 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3902 * Compare the catsets. Use the netlbl APIs.
3904 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3905 if ((skp->smk_netlabel.flags &
3906 NETLBL_SECATTR_MLS_CAT) == 0)
3910 for (acat = -1, kcat = -1; acat == kcat; ) {
3911 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3913 kcat = netlbl_catmap_walk(
3914 skp->smk_netlabel.attr.mls.cat,
3916 if (acat < 0 || kcat < 0)
3929 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3930 return &smack_known_web;
3931 return &smack_known_star;
3934 * Without guidance regarding the smack value
3935 * for the packet fall back on the network
3938 return smack_net_ambient;
3941 #if IS_ENABLED(CONFIG_IPV6)
3942 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3946 int proto = -EINVAL;
3947 struct ipv6hdr _ipv6h;
3948 struct ipv6hdr *ip6;
3950 struct tcphdr _tcph, *th;
3951 struct udphdr _udph, *uh;
3952 struct dccp_hdr _dccph, *dh;
3956 offset = skb_network_offset(skb);
3957 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3960 sip->sin6_addr = ip6->saddr;
3962 nexthdr = ip6->nexthdr;
3963 offset += sizeof(_ipv6h);
3964 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3971 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3973 sip->sin6_port = th->source;
3976 case IPPROTO_UDPLITE:
3977 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3979 sip->sin6_port = uh->source;
3982 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3984 sip->sin6_port = dh->dccph_sport;
3989 #endif /* CONFIG_IPV6 */
3992 * smack_from_skb - Smack data from the secmark in an skb
3995 * Returns smack_known of the secmark or NULL if that won't work.
3997 #ifdef CONFIG_NETWORK_SECMARK
3998 static struct smack_known *smack_from_skb(struct sk_buff *skb)
4000 if (skb == NULL || skb->secmark == 0)
4003 return smack_from_secid(skb->secmark);
4006 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
4013 * smack_from_netlbl - Smack data from the IP options in an skb
4014 * @sk: socket data came in on
4015 * @family: address family
4018 * Find the Smack label in the IP options. If it hasn't been
4019 * added to the netlabel cache, add it here.
4021 * Returns smack_known of the IP options or NULL if that won't work.
4023 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
4024 struct sk_buff *skb)
4026 struct netlbl_lsm_secattr secattr;
4027 struct socket_smack *ssp = NULL;
4028 struct smack_known *skp = NULL;
4030 netlbl_secattr_init(&secattr);
4033 ssp = sk->sk_security;
4035 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
4036 skp = smack_from_secattr(&secattr, ssp);
4037 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
4038 netlbl_cache_add(skb, family, &skp->smk_netlabel);
4041 netlbl_secattr_destroy(&secattr);
4047 * smack_socket_sock_rcv_skb - Smack packet delivery access check
4051 * Returns 0 if the packet should be delivered, an error code otherwise
4053 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4055 struct socket_smack *ssp = sk->sk_security;
4056 struct smack_known *skp = NULL;
4058 struct smk_audit_info ad;
4059 u16 family = sk->sk_family;
4061 struct lsm_network_audit net;
4063 #if IS_ENABLED(CONFIG_IPV6)
4064 struct sockaddr_in6 sadd;
4067 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4069 #endif /* CONFIG_IPV6 */
4074 * If there is a secmark use it rather than the CIPSO label.
4075 * If there is no secmark fall back to CIPSO.
4076 * The secmark is assumed to reflect policy better.
4078 skp = smack_from_skb(skb);
4080 skp = smack_from_netlbl(sk, family, skb);
4082 skp = smack_net_ambient;
4086 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4087 ad.a.u.net->family = family;
4088 ad.a.u.net->netif = skb->skb_iif;
4089 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4092 * Receiving a packet requires that the other end
4093 * be able to write here. Read access is not required.
4094 * This is the simplist possible security model
4097 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4098 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
4101 netlbl_skbuff_err(skb, family, rc, 0);
4103 #if IS_ENABLED(CONFIG_IPV6)
4105 proto = smk_skb_to_addr_ipv6(skb, &sadd);
4106 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
4107 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
4109 #ifdef SMACK_IPV6_SECMARK_LABELING
4110 skp = smack_from_skb(skb);
4112 if (smk_ipv6_localhost(&sadd))
4114 skp = smack_ipv6host_label(&sadd);
4116 skp = smack_net_ambient;
4119 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4120 ad.a.u.net->family = family;
4121 ad.a.u.net->netif = skb->skb_iif;
4122 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4123 #endif /* CONFIG_AUDIT */
4124 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4125 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4127 #endif /* SMACK_IPV6_SECMARK_LABELING */
4128 #ifdef SMACK_IPV6_PORT_LABELING
4129 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
4130 #endif /* SMACK_IPV6_PORT_LABELING */
4132 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4133 ICMPV6_ADM_PROHIBITED, 0);
4135 #endif /* CONFIG_IPV6 */
4142 * smack_socket_getpeersec_stream - pull in packet label
4144 * @optval: user's destination
4145 * @optlen: size thereof
4148 * returns zero on success, an error code otherwise
4150 static int smack_socket_getpeersec_stream(struct socket *sock,
4151 sockptr_t optval, sockptr_t optlen,
4154 struct socket_smack *ssp;
4159 ssp = sock->sk->sk_security;
4160 if (ssp->smk_packet != NULL) {
4161 rcp = ssp->smk_packet->smk_known;
4162 slen = strlen(rcp) + 1;
4169 if (copy_to_sockptr(optval, rcp, slen))
4172 if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
4179 * smack_socket_getpeersec_dgram - pull in packet label
4180 * @sock: the peer socket
4182 * @secid: pointer to where to put the secid of the packet
4184 * Sets the netlabel socket state on sk from parent
4186 static int smack_socket_getpeersec_dgram(struct socket *sock,
4187 struct sk_buff *skb, u32 *secid)
4190 struct socket_smack *ssp = NULL;
4191 struct smack_known *skp;
4192 struct sock *sk = NULL;
4193 int family = PF_UNSPEC;
4194 u32 s = 0; /* 0 is the invalid secid */
4197 if (skb->protocol == htons(ETH_P_IP))
4199 #if IS_ENABLED(CONFIG_IPV6)
4200 else if (skb->protocol == htons(ETH_P_IPV6))
4202 #endif /* CONFIG_IPV6 */
4204 if (family == PF_UNSPEC && sock != NULL)
4205 family = sock->sk->sk_family;
4209 ssp = sock->sk->sk_security;
4210 s = ssp->smk_out->smk_secid;
4213 skp = smack_from_skb(skb);
4219 * Translate what netlabel gave us.
4223 skp = smack_from_netlbl(sk, family, skb);
4228 #ifdef SMACK_IPV6_SECMARK_LABELING
4229 skp = smack_from_skb(skb);
4242 * smack_sock_graft - Initialize a newly created socket with an existing sock
4244 * @parent: parent socket
4246 * Set the smk_{in,out} state of an existing sock based on the process that
4247 * is creating the new socket.
4249 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4251 struct socket_smack *ssp;
4252 struct smack_known *skp = smk_of_current();
4255 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4258 ssp = sk->sk_security;
4261 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4265 * smack_inet_conn_request - Smack access check on connect
4266 * @sk: socket involved
4270 * Returns 0 if a task with the packet label could write to
4271 * the socket, otherwise an error code
4273 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4274 struct request_sock *req)
4276 u16 family = sk->sk_family;
4277 struct smack_known *skp;
4278 struct socket_smack *ssp = sk->sk_security;
4279 struct sockaddr_in addr;
4281 struct smack_known *hskp;
4283 struct smk_audit_info ad;
4285 struct lsm_network_audit net;
4288 #if IS_ENABLED(CONFIG_IPV6)
4289 if (family == PF_INET6) {
4291 * Handle mapped IPv4 packets arriving
4292 * via IPv6 sockets. Don't set up netlabel
4293 * processing on IPv6.
4295 if (skb->protocol == htons(ETH_P_IP))
4300 #endif /* CONFIG_IPV6 */
4303 * If there is a secmark use it rather than the CIPSO label.
4304 * If there is no secmark fall back to CIPSO.
4305 * The secmark is assumed to reflect policy better.
4307 skp = smack_from_skb(skb);
4309 skp = smack_from_netlbl(sk, family, skb);
4311 skp = &smack_known_huh;
4315 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4316 ad.a.u.net->family = family;
4317 ad.a.u.net->netif = skb->skb_iif;
4318 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4321 * Receiving a packet requires that the other end be able to write
4322 * here. Read access is not required.
4324 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4325 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4330 * Save the peer's label in the request_sock so we can later setup
4331 * smk_packet in the child socket so that SO_PEERCRED can report it.
4333 req->peer_secid = skp->smk_secid;
4336 * We need to decide if we want to label the incoming connection here
4337 * if we do we only need to label the request_sock and the stack will
4338 * propagate the wire-label to the sock when it is created.
4341 addr.sin_addr.s_addr = hdr->saddr;
4343 hskp = smack_ipv4host_label(&addr);
4347 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4349 netlbl_req_delattr(req);
4355 * smack_inet_csk_clone - Copy the connection information to the new socket
4356 * @sk: the new socket
4357 * @req: the connection's request_sock
4359 * Transfer the connection's peer label to the newly created socket.
4361 static void smack_inet_csk_clone(struct sock *sk,
4362 const struct request_sock *req)
4364 struct socket_smack *ssp = sk->sk_security;
4365 struct smack_known *skp;
4367 if (req->peer_secid != 0) {
4368 skp = smack_from_secid(req->peer_secid);
4369 ssp->smk_packet = skp;
4371 ssp->smk_packet = NULL;
4375 * Key management security hooks
4377 * Casey has not tested key support very heavily.
4378 * The permission check is most likely too restrictive.
4379 * If you care about keys please have a look.
4384 * smack_key_alloc - Set the key security blob
4386 * @cred: the credentials to use
4389 * No allocation required
4393 static int smack_key_alloc(struct key *key, const struct cred *cred,
4394 unsigned long flags)
4396 struct smack_known *skp = smk_of_task(smack_cred(cred));
4398 key->security = skp;
4403 * smack_key_free - Clear the key security blob
4406 * Clear the blob pointer
4408 static void smack_key_free(struct key *key)
4410 key->security = NULL;
4414 * smack_key_permission - Smack access on a key
4415 * @key_ref: gets to the object
4416 * @cred: the credentials to use
4417 * @need_perm: requested key permission
4419 * Return 0 if the task has read and write to the object,
4420 * an error code otherwise
4422 static int smack_key_permission(key_ref_t key_ref,
4423 const struct cred *cred,
4424 enum key_need_perm need_perm)
4427 struct smk_audit_info ad;
4428 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4433 * Validate requested permissions
4435 switch (need_perm) {
4437 case KEY_NEED_SEARCH:
4439 request |= MAY_READ;
4441 case KEY_NEED_WRITE:
4443 case KEY_NEED_SETATTR:
4444 request |= MAY_WRITE;
4446 case KEY_NEED_UNSPECIFIED:
4447 case KEY_NEED_UNLINK:
4448 case KEY_SYSADMIN_OVERRIDE:
4449 case KEY_AUTHTOKEN_OVERRIDE:
4450 case KEY_DEFER_PERM_CHECK:
4456 keyp = key_ref_to_ptr(key_ref);
4460 * If the key hasn't been initialized give it access so that
4463 if (keyp->security == NULL)
4466 * This should not occur
4471 if (smack_privileged(CAP_MAC_OVERRIDE))
4475 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4476 ad.a.u.key_struct.key = keyp->serial;
4477 ad.a.u.key_struct.key_desc = keyp->description;
4479 rc = smk_access(tkp, keyp->security, request, &ad);
4480 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4485 * smack_key_getsecurity - Smack label tagging the key
4486 * @key points to the key to be queried
4487 * @_buffer points to a pointer that should be set to point to the
4488 * resulting string (if no label or an error occurs).
4489 * Return the length of the string (including terminating NUL) or -ve if
4491 * May also return 0 (and a NULL buffer pointer) if there is no label.
4493 static int smack_key_getsecurity(struct key *key, char **_buffer)
4495 struct smack_known *skp = key->security;
4499 if (key->security == NULL) {
4504 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4507 length = strlen(copy) + 1;
4514 #ifdef CONFIG_KEY_NOTIFICATIONS
4516 * smack_watch_key - Smack access to watch a key for notifications.
4517 * @key: The key to be watched
4519 * Return 0 if the @watch->cred has permission to read from the key object and
4520 * an error otherwise.
4522 static int smack_watch_key(struct key *key)
4524 struct smk_audit_info ad;
4525 struct smack_known *tkp = smk_of_current();
4531 * If the key hasn't been initialized give it access so that
4534 if (key->security == NULL)
4537 * This should not occur
4542 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4546 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4547 ad.a.u.key_struct.key = key->serial;
4548 ad.a.u.key_struct.key_desc = key->description;
4550 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4551 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4554 #endif /* CONFIG_KEY_NOTIFICATIONS */
4555 #endif /* CONFIG_KEYS */
4557 #ifdef CONFIG_WATCH_QUEUE
4559 * smack_post_notification - Smack access to post a notification to a queue
4560 * @w_cred: The credentials of the watcher.
4561 * @cred: The credentials of the event source (may be NULL).
4562 * @n: The notification message to be posted.
4564 static int smack_post_notification(const struct cred *w_cred,
4565 const struct cred *cred,
4566 struct watch_notification *n)
4568 struct smk_audit_info ad;
4569 struct smack_known *subj, *obj;
4572 /* Always let maintenance notifications through. */
4573 if (n->type == WATCH_TYPE_META)
4578 subj = smk_of_task(smack_cred(cred));
4579 obj = smk_of_task(smack_cred(w_cred));
4581 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4582 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4583 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4586 #endif /* CONFIG_WATCH_QUEUE */
4591 * Audit requires a unique representation of each Smack specific
4592 * rule. This unique representation is used to distinguish the
4593 * object to be audited from remaining kernel objects and also
4594 * works as a glue between the audit hooks.
4596 * Since repository entries are added but never deleted, we'll use
4597 * the smack_known label address related to the given audit rule as
4598 * the needed unique representation. This also better fits the smack
4599 * model where nearly everything is a label.
4604 * smack_audit_rule_init - Initialize a smack audit rule
4605 * @field: audit rule fields given from user-space (audit.h)
4606 * @op: required testing operator (=, !=, >, <, ...)
4607 * @rulestr: smack label to be audited
4608 * @vrule: pointer to save our own audit rule representation
4610 * Prepare to audit cases where (@field @op @rulestr) is true.
4611 * The label to be audited is created if necessay.
4613 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4615 struct smack_known *skp;
4616 char **rule = (char **)vrule;
4619 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4622 if (op != Audit_equal && op != Audit_not_equal)
4625 skp = smk_import_entry(rulestr, 0);
4627 return PTR_ERR(skp);
4629 *rule = skp->smk_known;
4635 * smack_audit_rule_known - Distinguish Smack audit rules
4636 * @krule: rule of interest, in Audit kernel representation format
4638 * This is used to filter Smack rules from remaining Audit ones.
4639 * If it's proved that this rule belongs to us, the
4640 * audit_rule_match hook will be called to do the final judgement.
4642 static int smack_audit_rule_known(struct audit_krule *krule)
4644 struct audit_field *f;
4647 for (i = 0; i < krule->field_count; i++) {
4648 f = &krule->fields[i];
4650 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4658 * smack_audit_rule_match - Audit given object ?
4659 * @secid: security id for identifying the object to test
4660 * @field: audit rule flags given from user-space
4661 * @op: required testing operator
4662 * @vrule: smack internal rule presentation
4664 * The core Audit hook. It's used to take the decision of
4665 * whether to audit or not to audit a given object.
4667 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4669 struct smack_known *skp;
4672 if (unlikely(!rule)) {
4673 WARN_ONCE(1, "Smack: missing rule\n");
4677 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4680 skp = smack_from_secid(secid);
4683 * No need to do string comparisons. If a match occurs,
4684 * both pointers will point to the same smack_known
4687 if (op == Audit_equal)
4688 return (rule == skp->smk_known);
4689 if (op == Audit_not_equal)
4690 return (rule != skp->smk_known);
4696 * There is no need for a smack_audit_rule_free hook.
4697 * No memory was allocated.
4700 #endif /* CONFIG_AUDIT */
4703 * smack_ismaclabel - check if xattr @name references a smack MAC label
4704 * @name: Full xattr name to check.
4706 static int smack_ismaclabel(const char *name)
4708 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4713 * smack_secid_to_secctx - return the smack label for a secid
4714 * @secid: incoming integer
4715 * @secdata: destination
4716 * @seclen: how long it is
4718 * Exists for networking code.
4720 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4722 struct smack_known *skp = smack_from_secid(secid);
4725 *secdata = skp->smk_known;
4726 *seclen = strlen(skp->smk_known);
4731 * smack_secctx_to_secid - return the secid for a smack label
4732 * @secdata: smack label
4733 * @seclen: how long result is
4734 * @secid: outgoing integer
4736 * Exists for audit and networking code.
4738 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4740 struct smack_known *skp = smk_find_entry(secdata);
4743 *secid = skp->smk_secid;
4750 * There used to be a smack_release_secctx hook
4751 * that did nothing back when hooks were in a vector.
4752 * Now that there's a list such a hook adds cost.
4755 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4757 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4761 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4763 return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
4767 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4769 struct smack_known *skp = smk_of_inode(inode);
4771 *ctx = skp->smk_known;
4772 *ctxlen = strlen(skp->smk_known);
4776 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4779 struct task_smack *tsp;
4780 struct smack_known *skp;
4781 struct inode_smack *isp;
4782 struct cred *new_creds = *new;
4784 if (new_creds == NULL) {
4785 new_creds = prepare_creds();
4786 if (new_creds == NULL)
4790 tsp = smack_cred(new_creds);
4793 * Get label from overlay inode and set it in create_sid
4795 isp = smack_inode(d_inode(dentry));
4796 skp = isp->smk_inode;
4797 tsp->smk_task = skp;
4802 static int smack_inode_copy_up_xattr(const char *name)
4805 * Return 1 if this is the smack access Smack attribute.
4807 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4813 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4815 const struct cred *old,
4818 struct task_smack *otsp = smack_cred(old);
4819 struct task_smack *ntsp = smack_cred(new);
4820 struct inode_smack *isp;
4824 * Use the process credential unless all of
4825 * the transmuting criteria are met
4827 ntsp->smk_task = otsp->smk_task;
4830 * the attribute of the containing directory
4832 isp = smack_inode(d_inode(dentry->d_parent));
4834 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4836 may = smk_access_entry(otsp->smk_task->smk_known,
4837 isp->smk_inode->smk_known,
4838 &otsp->smk_task->smk_rules);
4842 * If the directory is transmuting and the rule
4843 * providing access is transmuting use the containing
4844 * directory label instead of the process label.
4846 if (may > 0 && (may & MAY_TRANSMUTE)) {
4847 ntsp->smk_task = isp->smk_inode;
4848 ntsp->smk_transmuted = ntsp->smk_task;
4854 #ifdef CONFIG_IO_URING
4856 * smack_uring_override_creds - Is io_uring cred override allowed?
4857 * @new: the target creds
4859 * Check to see if the current task is allowed to override it's credentials
4860 * to service an io_uring operation.
4862 static int smack_uring_override_creds(const struct cred *new)
4864 struct task_smack *tsp = smack_cred(current_cred());
4865 struct task_smack *nsp = smack_cred(new);
4868 * Allow the degenerate case where the new Smack value is
4869 * the same as the current Smack value.
4871 if (tsp->smk_task == nsp->smk_task)
4874 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4881 * smack_uring_sqpoll - check if a io_uring polling thread can be created
4883 * Check to see if the current task is allowed to create a new io_uring
4884 * kernel polling thread.
4886 static int smack_uring_sqpoll(void)
4888 if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred()))
4895 * smack_uring_cmd - check on file operations for io_uring
4896 * @ioucmd: the command in question
4898 * Make a best guess about whether a io_uring "command" should
4899 * be allowed. Use the same logic used for determining if the
4900 * file could be opened for read in the absence of better criteria.
4902 static int smack_uring_cmd(struct io_uring_cmd *ioucmd)
4904 struct file *file = ioucmd->file;
4905 struct smk_audit_info ad;
4906 struct task_smack *tsp;
4907 struct inode *inode;
4913 tsp = smack_cred(file->f_cred);
4914 inode = file_inode(file);
4916 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
4917 smk_ad_setfield_u_fs_path(&ad, file->f_path);
4918 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
4919 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
4924 #endif /* CONFIG_IO_URING */
4926 struct lsm_blob_sizes smack_blob_sizes __ro_after_init = {
4927 .lbs_cred = sizeof(struct task_smack),
4928 .lbs_file = sizeof(struct smack_known *),
4929 .lbs_inode = sizeof(struct inode_smack),
4930 .lbs_ipc = sizeof(struct smack_known *),
4931 .lbs_msg_msg = sizeof(struct smack_known *),
4932 .lbs_superblock = sizeof(struct superblock_smack),
4933 .lbs_xattr_count = SMACK_INODE_INIT_XATTRS,
4936 static struct security_hook_list smack_hooks[] __ro_after_init = {
4937 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4938 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4939 LSM_HOOK_INIT(syslog, smack_syslog),
4941 LSM_HOOK_INIT(fs_context_submount, smack_fs_context_submount),
4942 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4943 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4945 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4946 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4947 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4948 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4949 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4951 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
4953 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4954 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4955 LSM_HOOK_INIT(inode_link, smack_inode_link),
4956 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4957 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4958 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4959 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4960 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4961 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4962 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4963 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4964 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4965 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4966 LSM_HOOK_INIT(inode_set_acl, smack_inode_set_acl),
4967 LSM_HOOK_INIT(inode_get_acl, smack_inode_get_acl),
4968 LSM_HOOK_INIT(inode_remove_acl, smack_inode_remove_acl),
4969 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4970 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4971 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4972 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4974 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4975 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4976 LSM_HOOK_INIT(file_lock, smack_file_lock),
4977 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4978 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4979 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4980 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4981 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4982 LSM_HOOK_INIT(file_receive, smack_file_receive),
4984 LSM_HOOK_INIT(file_open, smack_file_open),
4986 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4987 LSM_HOOK_INIT(cred_free, smack_cred_free),
4988 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4989 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4990 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4991 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4992 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4993 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4994 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4995 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4996 LSM_HOOK_INIT(current_getsecid_subj, smack_current_getsecid_subj),
4997 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
4998 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4999 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
5000 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
5001 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
5002 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
5003 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
5004 LSM_HOOK_INIT(task_kill, smack_task_kill),
5005 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
5007 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
5008 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
5010 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
5012 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
5013 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
5014 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
5015 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
5016 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
5018 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
5019 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
5020 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
5021 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
5023 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
5024 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
5025 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
5026 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
5028 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
5030 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
5031 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
5033 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
5034 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
5036 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
5037 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
5038 #ifdef SMACK_IPV6_PORT_LABELING
5039 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
5041 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
5042 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
5043 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
5044 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
5045 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
5046 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
5047 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
5048 LSM_HOOK_INIT(sk_clone_security, smack_sk_clone_security),
5049 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
5050 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
5051 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
5053 /* key management security hooks */
5055 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
5056 LSM_HOOK_INIT(key_free, smack_key_free),
5057 LSM_HOOK_INIT(key_permission, smack_key_permission),
5058 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
5059 #ifdef CONFIG_KEY_NOTIFICATIONS
5060 LSM_HOOK_INIT(watch_key, smack_watch_key),
5062 #endif /* CONFIG_KEYS */
5064 #ifdef CONFIG_WATCH_QUEUE
5065 LSM_HOOK_INIT(post_notification, smack_post_notification),
5070 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
5071 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
5072 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
5073 #endif /* CONFIG_AUDIT */
5075 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
5076 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
5077 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
5078 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
5079 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
5080 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
5081 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
5082 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
5083 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
5084 #ifdef CONFIG_IO_URING
5085 LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds),
5086 LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll),
5087 LSM_HOOK_INIT(uring_cmd, smack_uring_cmd),
5092 static __init void init_smack_known_list(void)
5095 * Initialize rule list locks
5097 mutex_init(&smack_known_huh.smk_rules_lock);
5098 mutex_init(&smack_known_hat.smk_rules_lock);
5099 mutex_init(&smack_known_floor.smk_rules_lock);
5100 mutex_init(&smack_known_star.smk_rules_lock);
5101 mutex_init(&smack_known_web.smk_rules_lock);
5103 * Initialize rule lists
5105 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
5106 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
5107 INIT_LIST_HEAD(&smack_known_star.smk_rules);
5108 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
5109 INIT_LIST_HEAD(&smack_known_web.smk_rules);
5111 * Create the known labels list
5113 smk_insert_entry(&smack_known_huh);
5114 smk_insert_entry(&smack_known_hat);
5115 smk_insert_entry(&smack_known_star);
5116 smk_insert_entry(&smack_known_floor);
5117 smk_insert_entry(&smack_known_web);
5121 * smack_init - initialize the smack system
5123 * Returns 0 on success, -ENOMEM is there's no memory
5125 static __init int smack_init(void)
5127 struct cred *cred = (struct cred *) current->cred;
5128 struct task_smack *tsp;
5130 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
5131 if (!smack_rule_cache)
5135 * Set the security state for the initial task.
5137 tsp = smack_cred(cred);
5138 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
5143 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
5146 pr_info("Smack: Initializing.\n");
5147 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
5148 pr_info("Smack: Netfilter enabled.\n");
5150 #ifdef SMACK_IPV6_PORT_LABELING
5151 pr_info("Smack: IPv6 port labeling enabled.\n");
5153 #ifdef SMACK_IPV6_SECMARK_LABELING
5154 pr_info("Smack: IPv6 Netfilter enabled.\n");
5157 /* initialize the smack_known_list */
5158 init_smack_known_list();
5164 * Smack requires early initialization in order to label
5165 * all processes and objects when they are created.
5167 DEFINE_LSM(smack) = {
5169 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
5170 .blobs = &smack_blob_sizes,