1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
8 * Casey Schaufler <casey@schaufler-ca.com>
9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
13 * Paul Moore <paul@paul-moore.com>
14 * Copyright (C) 2010 Nokia Corporation
15 * Copyright (C) 2011 Intel Corporation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
23 #include <asm/ioctls.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/dccp.h>
28 #include <linux/icmpv6.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
38 #include <linux/msg.h>
39 #include <linux/shm.h>
40 #include <linux/binfmts.h>
41 #include <linux/parser.h>
42 #include <linux/fs_context.h>
43 #include <linux/fs_parser.h>
44 #include <linux/watch_queue.h>
45 #include <linux/io_uring.h>
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 #ifdef SMACK_IPV6_PORT_LABELING
56 static DEFINE_MUTEX(smack_ipv6_lock);
57 static LIST_HEAD(smk_ipv6_port_list);
59 struct kmem_cache *smack_rule_cache;
60 int smack_enabled __initdata;
62 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
67 } smk_mount_opts[] = {
68 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
69 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
73 static int match_opt_prefix(char *s, int l, char **arg)
77 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
78 size_t len = smk_mount_opts[i].len;
79 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
81 if (len == l || s[len] != '=')
84 return smk_mount_opts[i].opt;
89 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
90 static char *smk_bu_mess[] = {
91 "Bringup Error", /* Unused */
92 "Bringup", /* SMACK_BRINGUP_ALLOW */
93 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
94 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
97 static void smk_bu_mode(int mode, char *s)
103 if (mode & MAY_WRITE)
107 if (mode & MAY_APPEND)
109 if (mode & MAY_TRANSMUTE)
119 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
120 static int smk_bu_note(char *note, struct smack_known *sskp,
121 struct smack_known *oskp, int mode, int rc)
123 char acc[SMK_NUM_ACCESS_TYPE + 1];
127 if (rc > SMACK_UNCONFINED_OBJECT)
130 smk_bu_mode(mode, acc);
131 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
132 sskp->smk_known, oskp->smk_known, acc, note);
136 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
139 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
140 static int smk_bu_current(char *note, struct smack_known *oskp,
143 struct task_smack *tsp = smack_cred(current_cred());
144 char acc[SMK_NUM_ACCESS_TYPE + 1];
148 if (rc > SMACK_UNCONFINED_OBJECT)
151 smk_bu_mode(mode, acc);
152 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
153 tsp->smk_task->smk_known, oskp->smk_known,
154 acc, current->comm, note);
158 #define smk_bu_current(note, oskp, mode, RC) (RC)
161 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
162 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
164 struct task_smack *tsp = smack_cred(current_cred());
165 struct smack_known *smk_task = smk_of_task_struct_obj(otp);
166 char acc[SMK_NUM_ACCESS_TYPE + 1];
170 if (rc > SMACK_UNCONFINED_OBJECT)
173 smk_bu_mode(mode, acc);
174 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
175 tsp->smk_task->smk_known, smk_task->smk_known, acc,
176 current->comm, otp->comm);
180 #define smk_bu_task(otp, mode, RC) (RC)
183 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
184 static int smk_bu_inode(struct inode *inode, int mode, int rc)
186 struct task_smack *tsp = smack_cred(current_cred());
187 struct inode_smack *isp = smack_inode(inode);
188 char acc[SMK_NUM_ACCESS_TYPE + 1];
190 if (isp->smk_flags & SMK_INODE_IMPURE)
191 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
192 inode->i_sb->s_id, inode->i_ino, current->comm);
196 if (rc > SMACK_UNCONFINED_OBJECT)
198 if (rc == SMACK_UNCONFINED_SUBJECT &&
199 (mode & (MAY_WRITE | MAY_APPEND)))
200 isp->smk_flags |= SMK_INODE_IMPURE;
202 smk_bu_mode(mode, acc);
204 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
205 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
206 inode->i_sb->s_id, inode->i_ino, current->comm);
210 #define smk_bu_inode(inode, mode, RC) (RC)
213 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
214 static int smk_bu_file(struct file *file, int mode, int rc)
216 struct task_smack *tsp = smack_cred(current_cred());
217 struct smack_known *sskp = tsp->smk_task;
218 struct inode *inode = file_inode(file);
219 struct inode_smack *isp = smack_inode(inode);
220 char acc[SMK_NUM_ACCESS_TYPE + 1];
222 if (isp->smk_flags & SMK_INODE_IMPURE)
223 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
224 inode->i_sb->s_id, inode->i_ino, current->comm);
228 if (rc > SMACK_UNCONFINED_OBJECT)
231 smk_bu_mode(mode, acc);
232 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
233 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
234 inode->i_sb->s_id, inode->i_ino, file,
239 #define smk_bu_file(file, mode, RC) (RC)
242 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
243 static int smk_bu_credfile(const struct cred *cred, struct file *file,
246 struct task_smack *tsp = smack_cred(cred);
247 struct smack_known *sskp = tsp->smk_task;
248 struct inode *inode = file_inode(file);
249 struct inode_smack *isp = smack_inode(inode);
250 char acc[SMK_NUM_ACCESS_TYPE + 1];
252 if (isp->smk_flags & SMK_INODE_IMPURE)
253 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
254 inode->i_sb->s_id, inode->i_ino, current->comm);
258 if (rc > SMACK_UNCONFINED_OBJECT)
261 smk_bu_mode(mode, acc);
262 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
263 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
264 inode->i_sb->s_id, inode->i_ino, file,
269 #define smk_bu_credfile(cred, file, mode, RC) (RC)
273 * smk_fetch - Fetch the smack label from a file.
274 * @name: type of the label (attribute)
275 * @ip: a pointer to the inode
276 * @dp: a pointer to the dentry
278 * Returns a pointer to the master list entry for the Smack label,
279 * NULL if there was no label to fetch, or an error code.
281 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
286 struct smack_known *skp = NULL;
288 if (!(ip->i_opflags & IOP_XATTR))
289 return ERR_PTR(-EOPNOTSUPP);
291 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
293 return ERR_PTR(-ENOMEM);
295 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
301 skp = smk_import_entry(buffer, rc);
309 * init_inode_smack - initialize an inode security blob
310 * @inode: inode to extract the info from
311 * @skp: a pointer to the Smack label entry to use in the blob
314 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
316 struct inode_smack *isp = smack_inode(inode);
318 isp->smk_inode = skp;
323 * init_task_smack - initialize a task security blob
324 * @tsp: blob to initialize
325 * @task: a pointer to the Smack label for the running task
326 * @forked: a pointer to the Smack label for the forked task
329 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
330 struct smack_known *forked)
332 tsp->smk_task = task;
333 tsp->smk_forked = forked;
334 INIT_LIST_HEAD(&tsp->smk_rules);
335 INIT_LIST_HEAD(&tsp->smk_relabel);
336 mutex_init(&tsp->smk_rules_lock);
340 * smk_copy_rules - copy a rule set
341 * @nhead: new rules header pointer
342 * @ohead: old rules header pointer
343 * @gfp: type of the memory for the allocation
345 * Returns 0 on success, -ENOMEM on error
347 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
350 struct smack_rule *nrp;
351 struct smack_rule *orp;
354 list_for_each_entry_rcu(orp, ohead, list) {
355 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
361 list_add_rcu(&nrp->list, nhead);
367 * smk_copy_relabel - copy smk_relabel labels list
368 * @nhead: new rules header pointer
369 * @ohead: old rules header pointer
370 * @gfp: type of the memory for the allocation
372 * Returns 0 on success, -ENOMEM on error
374 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
377 struct smack_known_list_elem *nklep;
378 struct smack_known_list_elem *oklep;
380 list_for_each_entry(oklep, ohead, list) {
381 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
383 smk_destroy_label_list(nhead);
386 nklep->smk_label = oklep->smk_label;
387 list_add(&nklep->list, nhead);
394 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
395 * @mode: input mode in form of PTRACE_MODE_*
397 * Returns a converted MAY_* mode usable by smack rules
399 static inline unsigned int smk_ptrace_mode(unsigned int mode)
401 if (mode & PTRACE_MODE_ATTACH)
402 return MAY_READWRITE;
403 if (mode & PTRACE_MODE_READ)
410 * smk_ptrace_rule_check - helper for ptrace access
411 * @tracer: tracer process
412 * @tracee_known: label entry of the process that's about to be traced
413 * @mode: ptrace attachment mode (PTRACE_MODE_*)
414 * @func: name of the function that called us, used for audit
416 * Returns 0 on access granted, -error on error
418 static int smk_ptrace_rule_check(struct task_struct *tracer,
419 struct smack_known *tracee_known,
420 unsigned int mode, const char *func)
423 struct smk_audit_info ad, *saip = NULL;
424 struct task_smack *tsp;
425 struct smack_known *tracer_known;
426 const struct cred *tracercred;
428 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
429 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
430 smk_ad_setfield_u_tsk(&ad, tracer);
435 tracercred = __task_cred(tracer);
436 tsp = smack_cred(tracercred);
437 tracer_known = smk_of_task(tsp);
439 if ((mode & PTRACE_MODE_ATTACH) &&
440 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
441 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
442 if (tracer_known->smk_known == tracee_known->smk_known)
444 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
446 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
452 smack_log(tracer_known->smk_known,
453 tracee_known->smk_known,
460 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
461 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
469 * We he, that is fun!
473 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
474 * @ctp: child task pointer
475 * @mode: ptrace attachment mode (PTRACE_MODE_*)
477 * Returns 0 if access is OK, an error code otherwise
479 * Do the capability checks.
481 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
483 struct smack_known *skp;
485 skp = smk_of_task_struct_obj(ctp);
487 return smk_ptrace_rule_check(current, skp, mode, __func__);
491 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
492 * @ptp: parent task pointer
494 * Returns 0 if access is OK, an error code otherwise
496 * Do the capability checks, and require PTRACE_MODE_ATTACH.
498 static int smack_ptrace_traceme(struct task_struct *ptp)
500 struct smack_known *skp;
502 skp = smk_of_task(smack_cred(current_cred()));
504 return smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
508 * smack_syslog - Smack approval on syslog
509 * @typefrom_file: unused
511 * Returns 0 on success, error code otherwise.
513 static int smack_syslog(int typefrom_file)
516 struct smack_known *skp = smk_of_current();
518 if (smack_privileged(CAP_MAC_OVERRIDE))
521 if (smack_syslog_label != NULL && smack_syslog_label != skp)
532 * smack_sb_alloc_security - allocate a superblock blob
533 * @sb: the superblock getting the blob
535 * Returns 0 on success or -ENOMEM on error.
537 static int smack_sb_alloc_security(struct super_block *sb)
539 struct superblock_smack *sbsp = smack_superblock(sb);
541 sbsp->smk_root = &smack_known_floor;
542 sbsp->smk_default = &smack_known_floor;
543 sbsp->smk_floor = &smack_known_floor;
544 sbsp->smk_hat = &smack_known_hat;
546 * SMK_SB_INITIALIZED will be zero from kzalloc.
552 struct smack_mnt_opts {
553 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
556 static void smack_free_mnt_opts(void *mnt_opts)
558 struct smack_mnt_opts *opts = mnt_opts;
559 kfree(opts->fsdefault);
560 kfree(opts->fsfloor);
563 kfree(opts->fstransmute);
567 static int smack_add_opt(int token, const char *s, void **mnt_opts)
569 struct smack_mnt_opts *opts = *mnt_opts;
572 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
601 case Opt_fstransmute:
602 if (opts->fstransmute)
604 opts->fstransmute = s;
610 pr_warn("Smack: duplicate mount options\n");
615 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
616 * @fc: The new filesystem context.
617 * @src_fc: The source filesystem context being duplicated.
619 * Returns 0 on success or -ENOMEM on error.
621 static int smack_fs_context_dup(struct fs_context *fc,
622 struct fs_context *src_fc)
624 struct smack_mnt_opts *dst, *src = src_fc->security;
629 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
634 if (src->fsdefault) {
635 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
640 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
645 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
650 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
654 if (src->fstransmute) {
655 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
656 if (!dst->fstransmute)
662 static const struct fs_parameter_spec smack_fs_parameters[] = {
663 fsparam_string("smackfsdef", Opt_fsdefault),
664 fsparam_string("smackfsdefault", Opt_fsdefault),
665 fsparam_string("smackfsfloor", Opt_fsfloor),
666 fsparam_string("smackfshat", Opt_fshat),
667 fsparam_string("smackfsroot", Opt_fsroot),
668 fsparam_string("smackfstransmute", Opt_fstransmute),
673 * smack_fs_context_parse_param - Parse a single mount parameter
674 * @fc: The new filesystem context being constructed.
675 * @param: The parameter.
677 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
680 static int smack_fs_context_parse_param(struct fs_context *fc,
681 struct fs_parameter *param)
683 struct fs_parse_result result;
686 opt = fs_parse(fc, smack_fs_parameters, param, &result);
690 rc = smack_add_opt(opt, param->string, &fc->security);
692 param->string = NULL;
696 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
698 char *from = options, *to = options;
702 char *next = strchr(from, ',');
711 token = match_opt_prefix(from, len, &arg);
712 if (token != Opt_error) {
713 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
714 rc = smack_add_opt(token, arg, mnt_opts);
718 smack_free_mnt_opts(*mnt_opts);
723 if (!first) { // copy with preceding comma
728 memmove(to, from, len);
741 * smack_set_mnt_opts - set Smack specific mount options
742 * @sb: the file system superblock
743 * @mnt_opts: Smack mount options
744 * @kern_flags: mount option from kernel space or user space
745 * @set_kern_flags: where to store converted mount opts
747 * Returns 0 on success, an error code on failure
749 * Allow filesystems with binary mount data to explicitly set Smack mount
752 static int smack_set_mnt_opts(struct super_block *sb,
754 unsigned long kern_flags,
755 unsigned long *set_kern_flags)
757 struct dentry *root = sb->s_root;
758 struct inode *inode = d_backing_inode(root);
759 struct superblock_smack *sp = smack_superblock(sb);
760 struct inode_smack *isp;
761 struct smack_known *skp;
762 struct smack_mnt_opts *opts = mnt_opts;
763 bool transmute = false;
765 if (sp->smk_flags & SMK_SB_INITIALIZED)
768 if (!smack_privileged(CAP_MAC_ADMIN)) {
770 * Unprivileged mounts don't get to specify Smack values.
775 * Unprivileged mounts get root and default from the caller.
777 skp = smk_of_current();
779 sp->smk_default = skp;
781 * For a handful of fs types with no user-controlled
782 * backing store it's okay to trust security labels
783 * in the filesystem. The rest are untrusted.
785 if (sb->s_user_ns != &init_user_ns &&
786 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
787 sb->s_magic != RAMFS_MAGIC) {
789 sp->smk_flags |= SMK_SB_UNTRUSTED;
793 sp->smk_flags |= SMK_SB_INITIALIZED;
796 if (opts->fsdefault) {
797 skp = smk_import_entry(opts->fsdefault, 0);
800 sp->smk_default = skp;
803 skp = smk_import_entry(opts->fsfloor, 0);
809 skp = smk_import_entry(opts->fshat, 0);
815 skp = smk_import_entry(opts->fsroot, 0);
820 if (opts->fstransmute) {
821 skp = smk_import_entry(opts->fstransmute, 0);
830 * Initialize the root inode.
832 init_inode_smack(inode, sp->smk_root);
835 isp = smack_inode(inode);
836 isp->smk_flags |= SMK_INODE_TRANSMUTE;
843 * smack_sb_statfs - Smack check on statfs
844 * @dentry: identifies the file system in question
846 * Returns 0 if current can read the floor of the filesystem,
847 * and error code otherwise
849 static int smack_sb_statfs(struct dentry *dentry)
851 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
853 struct smk_audit_info ad;
855 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
856 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
858 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
859 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
868 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
869 * @bprm: the exec information
871 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
873 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
875 struct inode *inode = file_inode(bprm->file);
876 struct task_smack *bsp = smack_cred(bprm->cred);
877 struct inode_smack *isp;
878 struct superblock_smack *sbsp;
881 isp = smack_inode(inode);
882 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
885 sbsp = smack_superblock(inode->i_sb);
886 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
887 isp->smk_task != sbsp->smk_root)
890 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
891 struct task_struct *tracer;
895 tracer = ptrace_parent(current);
896 if (likely(tracer != NULL))
897 rc = smk_ptrace_rule_check(tracer,
906 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
909 bsp->smk_task = isp->smk_task;
910 bprm->per_clear |= PER_CLEAR_ON_SETID;
912 /* Decide if this is a secure exec. */
913 if (bsp->smk_task != bsp->smk_forked)
914 bprm->secureexec = 1;
924 * smack_inode_alloc_security - allocate an inode blob
925 * @inode: the inode in need of a blob
929 static int smack_inode_alloc_security(struct inode *inode)
931 struct smack_known *skp = smk_of_current();
933 init_inode_smack(inode, skp);
938 * smack_inode_init_security - copy out the smack from an inode
939 * @inode: the newly created inode
940 * @dir: containing directory object
942 * @name: where to put the attribute name
943 * @value: where to put the attribute value
944 * @len: where to put the length of the attribute
946 * Returns 0 if it all works out, -ENOMEM if there's no memory
948 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
949 const struct qstr *qstr, const char **name,
950 void **value, size_t *len)
952 struct inode_smack *issp = smack_inode(inode);
953 struct smack_known *skp = smk_of_current();
954 struct smack_known *isp = smk_of_inode(inode);
955 struct smack_known *dsp = smk_of_inode(dir);
959 *name = XATTR_SMACK_SUFFIX;
963 may = smk_access_entry(skp->smk_known, dsp->smk_known,
968 * If the access rule allows transmutation and
969 * the directory requests transmutation then
970 * by all means transmute.
971 * Mark the inode as changed.
973 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
974 smk_inode_transmutable(dir)) {
976 issp->smk_flags |= SMK_INODE_CHANGED;
979 *value = kstrdup(isp->smk_known, GFP_NOFS);
983 *len = strlen(isp->smk_known);
990 * smack_inode_link - Smack check on link
991 * @old_dentry: the existing object
993 * @new_dentry: the new object
995 * Returns 0 if access is permitted, an error code otherwise
997 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
998 struct dentry *new_dentry)
1000 struct smack_known *isp;
1001 struct smk_audit_info ad;
1004 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1005 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1007 isp = smk_of_inode(d_backing_inode(old_dentry));
1008 rc = smk_curacc(isp, MAY_WRITE, &ad);
1009 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1011 if (rc == 0 && d_is_positive(new_dentry)) {
1012 isp = smk_of_inode(d_backing_inode(new_dentry));
1013 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1014 rc = smk_curacc(isp, MAY_WRITE, &ad);
1015 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1022 * smack_inode_unlink - Smack check on inode deletion
1023 * @dir: containing directory object
1024 * @dentry: file to unlink
1026 * Returns 0 if current can write the containing directory
1027 * and the object, error code otherwise
1029 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1031 struct inode *ip = d_backing_inode(dentry);
1032 struct smk_audit_info ad;
1035 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1036 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1039 * You need write access to the thing you're unlinking
1041 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1042 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1045 * You also need write access to the containing directory
1047 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1048 smk_ad_setfield_u_fs_inode(&ad, dir);
1049 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1050 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1056 * smack_inode_rmdir - Smack check on directory deletion
1057 * @dir: containing directory object
1058 * @dentry: directory to unlink
1060 * Returns 0 if current can write the containing directory
1061 * and the directory, error code otherwise
1063 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1065 struct smk_audit_info ad;
1068 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1069 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1072 * You need write access to the thing you're removing
1074 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1075 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1078 * You also need write access to the containing directory
1080 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1081 smk_ad_setfield_u_fs_inode(&ad, dir);
1082 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1083 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1090 * smack_inode_rename - Smack check on rename
1091 * @old_inode: unused
1092 * @old_dentry: the old object
1093 * @new_inode: unused
1094 * @new_dentry: the new object
1096 * Read and write access is required on both the old and
1099 * Returns 0 if access is permitted, an error code otherwise
1101 static int smack_inode_rename(struct inode *old_inode,
1102 struct dentry *old_dentry,
1103 struct inode *new_inode,
1104 struct dentry *new_dentry)
1107 struct smack_known *isp;
1108 struct smk_audit_info ad;
1110 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1111 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1113 isp = smk_of_inode(d_backing_inode(old_dentry));
1114 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1115 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1117 if (rc == 0 && d_is_positive(new_dentry)) {
1118 isp = smk_of_inode(d_backing_inode(new_dentry));
1119 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1120 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1121 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1127 * smack_inode_permission - Smack version of permission()
1128 * @inode: the inode in question
1129 * @mask: the access requested
1131 * This is the important Smack hook.
1133 * Returns 0 if access is permitted, an error code otherwise
1135 static int smack_inode_permission(struct inode *inode, int mask)
1137 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
1138 struct smk_audit_info ad;
1139 int no_block = mask & MAY_NOT_BLOCK;
1142 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1144 * No permission to check. Existence test. Yup, it's there.
1149 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1150 if (smk_of_inode(inode) != sbsp->smk_root)
1154 /* May be droppable after audit */
1157 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1158 smk_ad_setfield_u_fs_inode(&ad, inode);
1159 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1160 rc = smk_bu_inode(inode, mask, rc);
1165 * smack_inode_setattr - Smack check for setting attributes
1166 * @dentry: the object
1167 * @iattr: for the force flag
1169 * Returns 0 if access is permitted, an error code otherwise
1171 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1173 struct smk_audit_info ad;
1177 * Need to allow for clearing the setuid bit.
1179 if (iattr->ia_valid & ATTR_FORCE)
1181 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1182 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1184 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1185 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1190 * smack_inode_getattr - Smack check for getting attributes
1191 * @path: path to extract the info from
1193 * Returns 0 if access is permitted, an error code otherwise
1195 static int smack_inode_getattr(const struct path *path)
1197 struct smk_audit_info ad;
1198 struct inode *inode = d_backing_inode(path->dentry);
1201 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1202 smk_ad_setfield_u_fs_path(&ad, *path);
1203 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1204 rc = smk_bu_inode(inode, MAY_READ, rc);
1209 * smack_inode_setxattr - Smack check for setting xattrs
1210 * @mnt_userns: active user namespace
1211 * @dentry: the object
1212 * @name: name of the attribute
1213 * @value: value of the attribute
1214 * @size: size of the value
1217 * This protects the Smack attribute explicitly.
1219 * Returns 0 if access is permitted, an error code otherwise
1221 static int smack_inode_setxattr(struct user_namespace *mnt_userns,
1222 struct dentry *dentry, const char *name,
1223 const void *value, size_t size, int flags)
1225 struct smk_audit_info ad;
1226 struct smack_known *skp;
1228 int check_import = 0;
1233 * Check label validity here so import won't fail in post_setxattr
1235 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1236 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1237 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1240 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1241 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1245 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1247 if (size != TRANS_TRUE_SIZE ||
1248 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1251 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1253 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1256 if (rc == 0 && check_import) {
1257 skp = size ? smk_import_entry(value, size) : NULL;
1260 else if (skp == NULL || (check_star &&
1261 (skp == &smack_known_star || skp == &smack_known_web)))
1265 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1266 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1269 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1270 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1277 * smack_inode_post_setxattr - Apply the Smack update approved above
1279 * @name: attribute name
1280 * @value: attribute value
1281 * @size: attribute size
1284 * Set the pointer in the inode blob to the entry found
1285 * in the master label list.
1287 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1288 const void *value, size_t size, int flags)
1290 struct smack_known *skp;
1291 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1293 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1294 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1298 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1299 skp = smk_import_entry(value, size);
1301 isp->smk_inode = skp;
1302 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1303 skp = smk_import_entry(value, size);
1305 isp->smk_task = skp;
1306 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1307 skp = smk_import_entry(value, size);
1309 isp->smk_mmap = skp;
1316 * smack_inode_getxattr - Smack check on getxattr
1317 * @dentry: the object
1320 * Returns 0 if access is permitted, an error code otherwise
1322 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1324 struct smk_audit_info ad;
1327 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1328 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1330 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1331 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1336 * smack_inode_removexattr - Smack check on removexattr
1337 * @mnt_userns: active user namespace
1338 * @dentry: the object
1339 * @name: name of the attribute
1341 * Removing the Smack attribute requires CAP_MAC_ADMIN
1343 * Returns 0 if access is permitted, an error code otherwise
1345 static int smack_inode_removexattr(struct user_namespace *mnt_userns,
1346 struct dentry *dentry, const char *name)
1348 struct inode_smack *isp;
1349 struct smk_audit_info ad;
1352 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1353 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1354 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1355 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1356 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1357 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1358 if (!smack_privileged(CAP_MAC_ADMIN))
1361 rc = cap_inode_removexattr(mnt_userns, dentry, name);
1366 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1367 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1369 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1370 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1374 isp = smack_inode(d_backing_inode(dentry));
1376 * Don't do anything special for these.
1377 * XATTR_NAME_SMACKIPIN
1378 * XATTR_NAME_SMACKIPOUT
1380 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1381 struct super_block *sbp = dentry->d_sb;
1382 struct superblock_smack *sbsp = smack_superblock(sbp);
1384 isp->smk_inode = sbsp->smk_default;
1385 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1386 isp->smk_task = NULL;
1387 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1388 isp->smk_mmap = NULL;
1389 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1390 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1396 * smack_inode_getsecurity - get smack xattrs
1397 * @mnt_userns: active user namespace
1398 * @inode: the object
1399 * @name: attribute name
1400 * @buffer: where to put the result
1401 * @alloc: duplicate memory
1403 * Returns the size of the attribute or an error code
1405 static int smack_inode_getsecurity(struct user_namespace *mnt_userns,
1406 struct inode *inode, const char *name,
1407 void **buffer, bool alloc)
1409 struct socket_smack *ssp;
1410 struct socket *sock;
1411 struct super_block *sbp;
1412 struct inode *ip = (struct inode *)inode;
1413 struct smack_known *isp;
1415 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1416 isp = smk_of_inode(inode);
1419 * The rest of the Smack xattrs are only on sockets.
1422 if (sbp->s_magic != SOCKFS_MAGIC)
1425 sock = SOCKET_I(ip);
1426 if (sock == NULL || sock->sk == NULL)
1429 ssp = sock->sk->sk_security;
1431 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1433 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1440 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1441 if (*buffer == NULL)
1445 return strlen(isp->smk_known);
1450 * smack_inode_listsecurity - list the Smack attributes
1451 * @inode: the object
1452 * @buffer: where they go
1453 * @buffer_size: size of buffer
1455 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1458 int len = sizeof(XATTR_NAME_SMACK);
1460 if (buffer != NULL && len <= buffer_size)
1461 memcpy(buffer, XATTR_NAME_SMACK, len);
1467 * smack_inode_getsecid - Extract inode's security id
1468 * @inode: inode to extract the info from
1469 * @secid: where result will be saved
1471 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1473 struct smack_known *skp = smk_of_inode(inode);
1475 *secid = skp->smk_secid;
1483 * There is no smack_file_permission hook
1485 * Should access checks be done on each read or write?
1486 * UNICOS and SELinux say yes.
1487 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1489 * I'll say no for now. Smack does not do the frequent
1490 * label changing that SELinux does.
1494 * smack_file_alloc_security - assign a file security blob
1497 * The security blob for a file is a pointer to the master
1498 * label list, so no allocation is done.
1500 * f_security is the owner security information. It
1501 * isn't used on file access checks, it's for send_sigio.
1505 static int smack_file_alloc_security(struct file *file)
1507 struct smack_known **blob = smack_file(file);
1509 *blob = smk_of_current();
1514 * smack_file_ioctl - Smack check on ioctls
1519 * Relies heavily on the correct use of the ioctl command conventions.
1521 * Returns 0 if allowed, error code otherwise
1523 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1527 struct smk_audit_info ad;
1528 struct inode *inode = file_inode(file);
1530 if (unlikely(IS_PRIVATE(inode)))
1533 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1534 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1536 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1537 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1538 rc = smk_bu_file(file, MAY_WRITE, rc);
1541 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1542 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1543 rc = smk_bu_file(file, MAY_READ, rc);
1550 * smack_file_lock - Smack check on file locking
1554 * Returns 0 if current has lock access, error code otherwise
1556 static int smack_file_lock(struct file *file, unsigned int cmd)
1558 struct smk_audit_info ad;
1560 struct inode *inode = file_inode(file);
1562 if (unlikely(IS_PRIVATE(inode)))
1565 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1566 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1567 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1568 rc = smk_bu_file(file, MAY_LOCK, rc);
1573 * smack_file_fcntl - Smack check on fcntl
1575 * @cmd: what action to check
1578 * Generally these operations are harmless.
1579 * File locking operations present an obvious mechanism
1580 * for passing information, so they require write access.
1582 * Returns 0 if current has access, error code otherwise
1584 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1587 struct smk_audit_info ad;
1589 struct inode *inode = file_inode(file);
1591 if (unlikely(IS_PRIVATE(inode)))
1599 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1600 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1601 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1602 rc = smk_bu_file(file, MAY_LOCK, rc);
1606 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1607 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1608 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1609 rc = smk_bu_file(file, MAY_WRITE, rc);
1619 * smack_mmap_file - Check permissions for a mmap operation.
1620 * @file: contains the file structure for file to map (may be NULL).
1621 * @reqprot: contains the protection requested by the application.
1622 * @prot: contains the protection that will be applied by the kernel.
1623 * @flags: contains the operational flags.
1625 * The @file may be NULL, e.g. if mapping anonymous memory.
1627 * Return 0 if permission is granted.
1629 static int smack_mmap_file(struct file *file,
1630 unsigned long reqprot, unsigned long prot,
1631 unsigned long flags)
1633 struct smack_known *skp;
1634 struct smack_known *mkp;
1635 struct smack_rule *srp;
1636 struct task_smack *tsp;
1637 struct smack_known *okp;
1638 struct inode_smack *isp;
1639 struct superblock_smack *sbsp;
1648 if (unlikely(IS_PRIVATE(file_inode(file))))
1651 isp = smack_inode(file_inode(file));
1652 if (isp->smk_mmap == NULL)
1654 sbsp = smack_superblock(file_inode(file)->i_sb);
1655 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1656 isp->smk_mmap != sbsp->smk_root)
1658 mkp = isp->smk_mmap;
1660 tsp = smack_cred(current_cred());
1661 skp = smk_of_current();
1666 * For each Smack rule associated with the subject
1667 * label verify that the SMACK64MMAP also has access
1668 * to that rule's object label.
1670 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1671 okp = srp->smk_object;
1673 * Matching labels always allows access.
1675 if (mkp->smk_known == okp->smk_known)
1678 * If there is a matching local rule take
1679 * that into account as well.
1681 may = smk_access_entry(srp->smk_subject->smk_known,
1685 may = srp->smk_access;
1687 may &= srp->smk_access;
1689 * If may is zero the SMACK64MMAP subject can't
1690 * possibly have less access.
1696 * Fetch the global list entry.
1697 * If there isn't one a SMACK64MMAP subject
1698 * can't have as much access as current.
1700 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1702 if (mmay == -ENOENT) {
1707 * If there is a local entry it modifies the
1708 * potential access, too.
1710 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1712 if (tmay != -ENOENT)
1716 * If there is any access available to current that is
1717 * not available to a SMACK64MMAP subject
1720 if ((may | mmay) != mmay) {
1732 * smack_file_set_fowner - set the file security blob value
1733 * @file: object in question
1736 static void smack_file_set_fowner(struct file *file)
1738 struct smack_known **blob = smack_file(file);
1740 *blob = smk_of_current();
1744 * smack_file_send_sigiotask - Smack on sigio
1745 * @tsk: The target task
1746 * @fown: the object the signal come from
1749 * Allow a privileged task to get signals even if it shouldn't
1751 * Returns 0 if a subject with the object's smack could
1752 * write to the task, an error code otherwise.
1754 static int smack_file_send_sigiotask(struct task_struct *tsk,
1755 struct fown_struct *fown, int signum)
1757 struct smack_known **blob;
1758 struct smack_known *skp;
1759 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1760 const struct cred *tcred;
1763 struct smk_audit_info ad;
1766 * struct fown_struct is never outside the context of a struct file
1768 file = container_of(fown, struct file, f_owner);
1770 /* we don't log here as rc can be overriden */
1771 blob = smack_file(file);
1773 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1774 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1777 tcred = __task_cred(tsk);
1778 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1782 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1783 smk_ad_setfield_u_tsk(&ad, tsk);
1784 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1789 * smack_file_receive - Smack file receive check
1792 * Returns 0 if current has access, error code otherwise
1794 static int smack_file_receive(struct file *file)
1798 struct smk_audit_info ad;
1799 struct inode *inode = file_inode(file);
1800 struct socket *sock;
1801 struct task_smack *tsp;
1802 struct socket_smack *ssp;
1804 if (unlikely(IS_PRIVATE(inode)))
1807 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1808 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1810 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1811 sock = SOCKET_I(inode);
1812 ssp = sock->sk->sk_security;
1813 tsp = smack_cred(current_cred());
1815 * If the receiving process can't write to the
1816 * passed socket or if the passed socket can't
1817 * write to the receiving process don't accept
1818 * the passed socket.
1820 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1821 rc = smk_bu_file(file, may, rc);
1824 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1825 rc = smk_bu_file(file, may, rc);
1829 * This code relies on bitmasks.
1831 if (file->f_mode & FMODE_READ)
1833 if (file->f_mode & FMODE_WRITE)
1836 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1837 rc = smk_bu_file(file, may, rc);
1842 * smack_file_open - Smack dentry open processing
1845 * Set the security blob in the file structure.
1846 * Allow the open only if the task has read access. There are
1847 * many read operations (e.g. fstat) that you can do with an
1848 * fd even if you have the file open write-only.
1850 * Returns 0 if current has access, error code otherwise
1852 static int smack_file_open(struct file *file)
1854 struct task_smack *tsp = smack_cred(file->f_cred);
1855 struct inode *inode = file_inode(file);
1856 struct smk_audit_info ad;
1859 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1860 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1861 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1862 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1872 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1873 * @cred: the new credentials
1874 * @gfp: the atomicity of any memory allocations
1876 * Prepare a blank set of credentials for modification. This must allocate all
1877 * the memory the LSM module might require such that cred_transfer() can
1878 * complete without error.
1880 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1882 init_task_smack(smack_cred(cred), NULL, NULL);
1888 * smack_cred_free - "free" task-level security credentials
1889 * @cred: the credentials in question
1892 static void smack_cred_free(struct cred *cred)
1894 struct task_smack *tsp = smack_cred(cred);
1895 struct smack_rule *rp;
1896 struct list_head *l;
1897 struct list_head *n;
1899 smk_destroy_label_list(&tsp->smk_relabel);
1901 list_for_each_safe(l, n, &tsp->smk_rules) {
1902 rp = list_entry(l, struct smack_rule, list);
1903 list_del(&rp->list);
1904 kmem_cache_free(smack_rule_cache, rp);
1909 * smack_cred_prepare - prepare new set of credentials for modification
1910 * @new: the new credentials
1911 * @old: the original credentials
1912 * @gfp: the atomicity of any memory allocations
1914 * Prepare a new set of credentials for modification.
1916 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1919 struct task_smack *old_tsp = smack_cred(old);
1920 struct task_smack *new_tsp = smack_cred(new);
1923 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1925 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1929 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1935 * smack_cred_transfer - Transfer the old credentials to the new credentials
1936 * @new: the new credentials
1937 * @old: the original credentials
1939 * Fill in a set of blank credentials from another set of credentials.
1941 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1943 struct task_smack *old_tsp = smack_cred(old);
1944 struct task_smack *new_tsp = smack_cred(new);
1946 new_tsp->smk_task = old_tsp->smk_task;
1947 new_tsp->smk_forked = old_tsp->smk_task;
1948 mutex_init(&new_tsp->smk_rules_lock);
1949 INIT_LIST_HEAD(&new_tsp->smk_rules);
1951 /* cbs copy rule list */
1955 * smack_cred_getsecid - get the secid corresponding to a creds structure
1956 * @cred: the object creds
1957 * @secid: where to put the result
1959 * Sets the secid to contain a u32 version of the smack label.
1961 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1963 struct smack_known *skp;
1966 skp = smk_of_task(smack_cred(cred));
1967 *secid = skp->smk_secid;
1972 * smack_kernel_act_as - Set the subjective context in a set of credentials
1973 * @new: points to the set of credentials to be modified.
1974 * @secid: specifies the security ID to be set
1976 * Set the security data for a kernel service.
1978 static int smack_kernel_act_as(struct cred *new, u32 secid)
1980 struct task_smack *new_tsp = smack_cred(new);
1982 new_tsp->smk_task = smack_from_secid(secid);
1987 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1988 * @new: points to the set of credentials to be modified
1989 * @inode: points to the inode to use as a reference
1991 * Set the file creation context in a set of credentials to the same
1992 * as the objective context of the specified inode
1994 static int smack_kernel_create_files_as(struct cred *new,
1995 struct inode *inode)
1997 struct inode_smack *isp = smack_inode(inode);
1998 struct task_smack *tsp = smack_cred(new);
2000 tsp->smk_forked = isp->smk_inode;
2001 tsp->smk_task = tsp->smk_forked;
2006 * smk_curacc_on_task - helper to log task related access
2007 * @p: the task object
2008 * @access: the access requested
2009 * @caller: name of the calling function for audit
2011 * Return 0 if access is permitted
2013 static int smk_curacc_on_task(struct task_struct *p, int access,
2016 struct smk_audit_info ad;
2017 struct smack_known *skp = smk_of_task_struct_obj(p);
2020 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2021 smk_ad_setfield_u_tsk(&ad, p);
2022 rc = smk_curacc(skp, access, &ad);
2023 rc = smk_bu_task(p, access, rc);
2028 * smack_task_setpgid - Smack check on setting pgid
2029 * @p: the task object
2032 * Return 0 if write access is permitted
2034 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2036 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2040 * smack_task_getpgid - Smack access check for getpgid
2041 * @p: the object task
2043 * Returns 0 if current can read the object task, error code otherwise
2045 static int smack_task_getpgid(struct task_struct *p)
2047 return smk_curacc_on_task(p, MAY_READ, __func__);
2051 * smack_task_getsid - Smack access check for getsid
2052 * @p: the object task
2054 * Returns 0 if current can read the object task, error code otherwise
2056 static int smack_task_getsid(struct task_struct *p)
2058 return smk_curacc_on_task(p, MAY_READ, __func__);
2062 * smack_current_getsecid_subj - get the subjective secid of the current task
2063 * @secid: where to put the result
2065 * Sets the secid to contain a u32 version of the task's subjective smack label.
2067 static void smack_current_getsecid_subj(u32 *secid)
2069 struct smack_known *skp = smk_of_current();
2071 *secid = skp->smk_secid;
2075 * smack_task_getsecid_obj - get the objective secid of the task
2077 * @secid: where to put the result
2079 * Sets the secid to contain a u32 version of the task's objective smack label.
2081 static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2083 struct smack_known *skp = smk_of_task_struct_obj(p);
2085 *secid = skp->smk_secid;
2089 * smack_task_setnice - Smack check on setting nice
2090 * @p: the task object
2093 * Return 0 if write access is permitted
2095 static int smack_task_setnice(struct task_struct *p, int nice)
2097 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2101 * smack_task_setioprio - Smack check on setting ioprio
2102 * @p: the task object
2105 * Return 0 if write access is permitted
2107 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2109 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2113 * smack_task_getioprio - Smack check on reading ioprio
2114 * @p: the task object
2116 * Return 0 if read access is permitted
2118 static int smack_task_getioprio(struct task_struct *p)
2120 return smk_curacc_on_task(p, MAY_READ, __func__);
2124 * smack_task_setscheduler - Smack check on setting scheduler
2125 * @p: the task object
2127 * Return 0 if read access is permitted
2129 static int smack_task_setscheduler(struct task_struct *p)
2131 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2135 * smack_task_getscheduler - Smack check on reading scheduler
2136 * @p: the task object
2138 * Return 0 if read access is permitted
2140 static int smack_task_getscheduler(struct task_struct *p)
2142 return smk_curacc_on_task(p, MAY_READ, __func__);
2146 * smack_task_movememory - Smack check on moving memory
2147 * @p: the task object
2149 * Return 0 if write access is permitted
2151 static int smack_task_movememory(struct task_struct *p)
2153 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2157 * smack_task_kill - Smack check on signal delivery
2158 * @p: the task object
2161 * @cred: identifies the cred to use in lieu of current's
2163 * Return 0 if write access is permitted
2166 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2167 int sig, const struct cred *cred)
2169 struct smk_audit_info ad;
2170 struct smack_known *skp;
2171 struct smack_known *tkp = smk_of_task_struct_obj(p);
2175 return 0; /* null signal; existence test */
2177 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2178 smk_ad_setfield_u_tsk(&ad, p);
2180 * Sending a signal requires that the sender
2181 * can write the receiver.
2184 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2185 rc = smk_bu_task(p, MAY_DELIVER, rc);
2189 * If the cred isn't NULL we're dealing with some USB IO
2190 * specific behavior. This is not clean. For one thing
2191 * we can't take privilege into account.
2193 skp = smk_of_task(smack_cred(cred));
2194 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2195 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2200 * smack_task_to_inode - copy task smack into the inode blob
2201 * @p: task to copy from
2202 * @inode: inode to copy to
2204 * Sets the smack pointer in the inode security blob
2206 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2208 struct inode_smack *isp = smack_inode(inode);
2209 struct smack_known *skp = smk_of_task_struct_obj(p);
2211 isp->smk_inode = skp;
2212 isp->smk_flags |= SMK_INODE_INSTANT;
2220 * smack_sk_alloc_security - Allocate a socket blob
2223 * @gfp_flags: memory allocation flags
2225 * Assign Smack pointers to current
2227 * Returns 0 on success, -ENOMEM is there's no memory
2229 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2231 struct smack_known *skp = smk_of_current();
2232 struct socket_smack *ssp;
2234 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2239 * Sockets created by kernel threads receive web label.
2241 if (unlikely(current->flags & PF_KTHREAD)) {
2242 ssp->smk_in = &smack_known_web;
2243 ssp->smk_out = &smack_known_web;
2248 ssp->smk_packet = NULL;
2250 sk->sk_security = ssp;
2256 * smack_sk_free_security - Free a socket blob
2259 * Clears the blob pointer
2261 static void smack_sk_free_security(struct sock *sk)
2263 #ifdef SMACK_IPV6_PORT_LABELING
2264 struct smk_port_label *spp;
2266 if (sk->sk_family == PF_INET6) {
2268 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2269 if (spp->smk_sock != sk)
2271 spp->smk_can_reuse = 1;
2277 kfree(sk->sk_security);
2281 * smack_sk_clone_security - Copy security context
2282 * @sk: the old socket
2283 * @newsk: the new socket
2285 * Copy the security context of the old socket pointer to the cloned
2287 static void smack_sk_clone_security(const struct sock *sk, struct sock *newsk)
2289 struct socket_smack *ssp_old = sk->sk_security;
2290 struct socket_smack *ssp_new = newsk->sk_security;
2292 *ssp_new = *ssp_old;
2296 * smack_ipv4host_label - check host based restrictions
2297 * @sip: the object end
2299 * looks for host based access restrictions
2301 * This version will only be appropriate for really small sets of single label
2302 * hosts. The caller is responsible for ensuring that the RCU read lock is
2303 * taken before calling this function.
2305 * Returns the label of the far end or NULL if it's not special.
2307 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2309 struct smk_net4addr *snp;
2310 struct in_addr *siap = &sip->sin_addr;
2312 if (siap->s_addr == 0)
2315 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2317 * we break after finding the first match because
2318 * the list is sorted from longest to shortest mask
2319 * so we have found the most specific match
2321 if (snp->smk_host.s_addr ==
2322 (siap->s_addr & snp->smk_mask.s_addr))
2323 return snp->smk_label;
2329 * smk_ipv6_localhost - Check for local ipv6 host address
2332 * Returns boolean true if this is the localhost address
2334 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2336 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2337 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2339 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2340 ntohs(be16p[7]) == 1)
2346 * smack_ipv6host_label - check host based restrictions
2347 * @sip: the object end
2349 * looks for host based access restrictions
2351 * This version will only be appropriate for really small sets of single label
2352 * hosts. The caller is responsible for ensuring that the RCU read lock is
2353 * taken before calling this function.
2355 * Returns the label of the far end or NULL if it's not special.
2357 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2359 struct smk_net6addr *snp;
2360 struct in6_addr *sap = &sip->sin6_addr;
2365 * It's local. Don't look for a host label.
2367 if (smk_ipv6_localhost(sip))
2370 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2372 * If the label is NULL the entry has
2373 * been renounced. Ignore it.
2375 if (snp->smk_label == NULL)
2378 * we break after finding the first match because
2379 * the list is sorted from longest to shortest mask
2380 * so we have found the most specific match
2382 for (found = 1, i = 0; i < 8; i++) {
2383 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2384 snp->smk_host.s6_addr16[i]) {
2390 return snp->smk_label;
2397 * smack_netlbl_add - Set the secattr on a socket
2400 * Attach the outbound smack value (smk_out) to the socket.
2402 * Returns 0 on success or an error code
2404 static int smack_netlbl_add(struct sock *sk)
2406 struct socket_smack *ssp = sk->sk_security;
2407 struct smack_known *skp = ssp->smk_out;
2411 bh_lock_sock_nested(sk);
2413 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2416 ssp->smk_state = SMK_NETLBL_LABELED;
2419 ssp->smk_state = SMK_NETLBL_REQSKB;
2431 * smack_netlbl_delete - Remove the secattr from a socket
2434 * Remove the outbound smack value from a socket
2436 static void smack_netlbl_delete(struct sock *sk)
2438 struct socket_smack *ssp = sk->sk_security;
2441 * Take the label off the socket if one is set.
2443 if (ssp->smk_state != SMK_NETLBL_LABELED)
2447 bh_lock_sock_nested(sk);
2448 netlbl_sock_delattr(sk);
2451 ssp->smk_state = SMK_NETLBL_UNLABELED;
2455 * smk_ipv4_check - Perform IPv4 host access checks
2457 * @sap: the destination address
2459 * Set the correct secattr for the given socket based on the destination
2460 * address and perform any outbound access checks needed.
2462 * Returns 0 on success or an error code.
2465 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2467 struct smack_known *skp;
2469 struct smack_known *hkp;
2470 struct socket_smack *ssp = sk->sk_security;
2471 struct smk_audit_info ad;
2474 hkp = smack_ipv4host_label(sap);
2477 struct lsm_network_audit net;
2479 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2480 ad.a.u.net->family = sap->sin_family;
2481 ad.a.u.net->dport = sap->sin_port;
2482 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2485 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2486 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2488 * Clear the socket netlabel if it's set.
2491 smack_netlbl_delete(sk);
2499 * smk_ipv6_check - check Smack access
2500 * @subject: subject Smack label
2501 * @object: object Smack label
2503 * @act: the action being taken
2505 * Check an IPv6 access
2507 static int smk_ipv6_check(struct smack_known *subject,
2508 struct smack_known *object,
2509 struct sockaddr_in6 *address, int act)
2512 struct lsm_network_audit net;
2514 struct smk_audit_info ad;
2518 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2519 ad.a.u.net->family = PF_INET6;
2520 ad.a.u.net->dport = address->sin6_port;
2521 if (act == SMK_RECEIVING)
2522 ad.a.u.net->v6info.saddr = address->sin6_addr;
2524 ad.a.u.net->v6info.daddr = address->sin6_addr;
2526 rc = smk_access(subject, object, MAY_WRITE, &ad);
2527 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2531 #ifdef SMACK_IPV6_PORT_LABELING
2533 * smk_ipv6_port_label - Smack port access table management
2537 * Create or update the port list entry
2539 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2541 struct sock *sk = sock->sk;
2542 struct sockaddr_in6 *addr6;
2543 struct socket_smack *ssp = sock->sk->sk_security;
2544 struct smk_port_label *spp;
2545 unsigned short port = 0;
2547 if (address == NULL) {
2549 * This operation is changing the Smack information
2550 * on the bound socket. Take the changes to the port
2554 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2555 if (sk != spp->smk_sock)
2557 spp->smk_in = ssp->smk_in;
2558 spp->smk_out = ssp->smk_out;
2563 * A NULL address is only used for updating existing
2564 * bound entries. If there isn't one, it's OK.
2570 addr6 = (struct sockaddr_in6 *)address;
2571 port = ntohs(addr6->sin6_port);
2573 * This is a special case that is safely ignored.
2579 * Look for an existing port list entry.
2580 * This is an indication that a port is getting reused.
2583 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2584 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2586 if (spp->smk_can_reuse != 1) {
2590 spp->smk_port = port;
2592 spp->smk_in = ssp->smk_in;
2593 spp->smk_out = ssp->smk_out;
2594 spp->smk_can_reuse = 0;
2600 * A new port entry is required.
2602 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2606 spp->smk_port = port;
2608 spp->smk_in = ssp->smk_in;
2609 spp->smk_out = ssp->smk_out;
2610 spp->smk_sock_type = sock->type;
2611 spp->smk_can_reuse = 0;
2613 mutex_lock(&smack_ipv6_lock);
2614 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2615 mutex_unlock(&smack_ipv6_lock);
2620 * smk_ipv6_port_check - check Smack port access
2623 * @act: the action being taken
2625 * Create or update the port list entry
2627 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2630 struct smk_port_label *spp;
2631 struct socket_smack *ssp = sk->sk_security;
2632 struct smack_known *skp = NULL;
2633 unsigned short port;
2634 struct smack_known *object;
2636 if (act == SMK_RECEIVING) {
2637 skp = smack_ipv6host_label(address);
2638 object = ssp->smk_in;
2641 object = smack_ipv6host_label(address);
2645 * The other end is a single label host.
2647 if (skp != NULL && object != NULL)
2648 return smk_ipv6_check(skp, object, address, act);
2650 skp = smack_net_ambient;
2652 object = smack_net_ambient;
2655 * It's remote, so port lookup does no good.
2657 if (!smk_ipv6_localhost(address))
2658 return smk_ipv6_check(skp, object, address, act);
2661 * It's local so the send check has to have passed.
2663 if (act == SMK_RECEIVING)
2666 port = ntohs(address->sin6_port);
2668 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2669 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2671 object = spp->smk_in;
2672 if (act == SMK_CONNECTING)
2673 ssp->smk_packet = spp->smk_out;
2678 return smk_ipv6_check(skp, object, address, act);
2683 * smack_inode_setsecurity - set smack xattrs
2684 * @inode: the object
2685 * @name: attribute name
2686 * @value: attribute value
2687 * @size: size of the attribute
2690 * Sets the named attribute in the appropriate blob
2692 * Returns 0 on success, or an error code
2694 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2695 const void *value, size_t size, int flags)
2697 struct smack_known *skp;
2698 struct inode_smack *nsp = smack_inode(inode);
2699 struct socket_smack *ssp;
2700 struct socket *sock;
2703 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2706 skp = smk_import_entry(value, size);
2708 return PTR_ERR(skp);
2710 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2711 nsp->smk_inode = skp;
2712 nsp->smk_flags |= SMK_INODE_INSTANT;
2716 * The rest of the Smack xattrs are only on sockets.
2718 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2721 sock = SOCKET_I(inode);
2722 if (sock == NULL || sock->sk == NULL)
2725 ssp = sock->sk->sk_security;
2727 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2729 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2731 if (sock->sk->sk_family == PF_INET) {
2732 rc = smack_netlbl_add(sock->sk);
2735 "Smack: \"%s\" netlbl error %d.\n",
2741 #ifdef SMACK_IPV6_PORT_LABELING
2742 if (sock->sk->sk_family == PF_INET6)
2743 smk_ipv6_port_label(sock, NULL);
2750 * smack_socket_post_create - finish socket setup
2752 * @family: protocol family
2757 * Sets the netlabel information on the socket
2759 * Returns 0 on success, and error code otherwise
2761 static int smack_socket_post_create(struct socket *sock, int family,
2762 int type, int protocol, int kern)
2764 struct socket_smack *ssp;
2766 if (sock->sk == NULL)
2770 * Sockets created by kernel threads receive web label.
2772 if (unlikely(current->flags & PF_KTHREAD)) {
2773 ssp = sock->sk->sk_security;
2774 ssp->smk_in = &smack_known_web;
2775 ssp->smk_out = &smack_known_web;
2778 if (family != PF_INET)
2781 * Set the outbound netlbl.
2783 return smack_netlbl_add(sock->sk);
2787 * smack_socket_socketpair - create socket pair
2788 * @socka: one socket
2789 * @sockb: another socket
2791 * Cross reference the peer labels for SO_PEERSEC
2795 static int smack_socket_socketpair(struct socket *socka,
2796 struct socket *sockb)
2798 struct socket_smack *asp = socka->sk->sk_security;
2799 struct socket_smack *bsp = sockb->sk->sk_security;
2801 asp->smk_packet = bsp->smk_out;
2802 bsp->smk_packet = asp->smk_out;
2807 #ifdef SMACK_IPV6_PORT_LABELING
2809 * smack_socket_bind - record port binding information.
2811 * @address: the port address
2812 * @addrlen: size of the address
2814 * Records the label bound to a port.
2816 * Returns 0 on success, and error code otherwise
2818 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2821 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2822 if (addrlen < SIN6_LEN_RFC2133 ||
2823 address->sa_family != AF_INET6)
2825 smk_ipv6_port_label(sock, address);
2829 #endif /* SMACK_IPV6_PORT_LABELING */
2832 * smack_socket_connect - connect access check
2834 * @sap: the other end
2835 * @addrlen: size of sap
2837 * Verifies that a connection may be possible
2839 * Returns 0 on success, and error code otherwise
2841 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2846 if (sock->sk == NULL)
2848 if (sock->sk->sk_family != PF_INET &&
2849 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2851 if (addrlen < offsetofend(struct sockaddr, sa_family))
2853 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2854 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2855 struct smack_known *rsp = NULL;
2857 if (addrlen < SIN6_LEN_RFC2133)
2859 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2860 rsp = smack_ipv6host_label(sip);
2862 struct socket_smack *ssp = sock->sk->sk_security;
2864 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2867 #ifdef SMACK_IPV6_PORT_LABELING
2868 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2873 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2875 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
2880 * smack_flags_to_may - convert S_ to MAY_ values
2881 * @flags: the S_ value
2883 * Returns the equivalent MAY_ value
2885 static int smack_flags_to_may(int flags)
2889 if (flags & S_IRUGO)
2891 if (flags & S_IWUGO)
2893 if (flags & S_IXUGO)
2900 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2905 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2907 struct smack_known **blob = smack_msg_msg(msg);
2909 *blob = smk_of_current();
2914 * smack_of_ipc - the smack pointer for the ipc
2917 * Returns a pointer to the smack value
2919 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2921 struct smack_known **blob = smack_ipc(isp);
2927 * smack_ipc_alloc_security - Set the security blob for ipc
2932 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2934 struct smack_known **blob = smack_ipc(isp);
2936 *blob = smk_of_current();
2941 * smk_curacc_shm : check if current has access on shm
2943 * @access : access requested
2945 * Returns 0 if current has the requested access, error code otherwise
2947 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2949 struct smack_known *ssp = smack_of_ipc(isp);
2950 struct smk_audit_info ad;
2954 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2955 ad.a.u.ipc_id = isp->id;
2957 rc = smk_curacc(ssp, access, &ad);
2958 rc = smk_bu_current("shm", ssp, access, rc);
2963 * smack_shm_associate - Smack access check for shm
2965 * @shmflg: access requested
2967 * Returns 0 if current has the requested access, error code otherwise
2969 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2973 may = smack_flags_to_may(shmflg);
2974 return smk_curacc_shm(isp, may);
2978 * smack_shm_shmctl - Smack access check for shm
2980 * @cmd: what it wants to do
2982 * Returns 0 if current has the requested access, error code otherwise
2984 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2998 may = MAY_READWRITE;
3003 * System level information.
3009 return smk_curacc_shm(isp, may);
3013 * smack_shm_shmat - Smack access for shmat
3016 * @shmflg: access requested
3018 * Returns 0 if current has the requested access, error code otherwise
3020 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3025 may = smack_flags_to_may(shmflg);
3026 return smk_curacc_shm(isp, may);
3030 * smk_curacc_sem : check if current has access on sem
3032 * @access : access requested
3034 * Returns 0 if current has the requested access, error code otherwise
3036 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3038 struct smack_known *ssp = smack_of_ipc(isp);
3039 struct smk_audit_info ad;
3043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3044 ad.a.u.ipc_id = isp->id;
3046 rc = smk_curacc(ssp, access, &ad);
3047 rc = smk_bu_current("sem", ssp, access, rc);
3052 * smack_sem_associate - Smack access check for sem
3054 * @semflg: access requested
3056 * Returns 0 if current has the requested access, error code otherwise
3058 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3062 may = smack_flags_to_may(semflg);
3063 return smk_curacc_sem(isp, may);
3067 * smack_sem_semctl - Smack access check for sem
3069 * @cmd: what it wants to do
3071 * Returns 0 if current has the requested access, error code otherwise
3073 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3092 may = MAY_READWRITE;
3097 * System level information
3104 return smk_curacc_sem(isp, may);
3108 * smack_sem_semop - Smack checks of semaphore operations
3114 * Treated as read and write in all cases.
3116 * Returns 0 if access is allowed, error code otherwise
3118 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3119 unsigned nsops, int alter)
3121 return smk_curacc_sem(isp, MAY_READWRITE);
3125 * smk_curacc_msq : helper to check if current has access on msq
3127 * @access : access requested
3129 * return 0 if current has access, error otherwise
3131 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3133 struct smack_known *msp = smack_of_ipc(isp);
3134 struct smk_audit_info ad;
3138 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3139 ad.a.u.ipc_id = isp->id;
3141 rc = smk_curacc(msp, access, &ad);
3142 rc = smk_bu_current("msq", msp, access, rc);
3147 * smack_msg_queue_associate - Smack access check for msg_queue
3149 * @msqflg: access requested
3151 * Returns 0 if current has the requested access, error code otherwise
3153 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3157 may = smack_flags_to_may(msqflg);
3158 return smk_curacc_msq(isp, may);
3162 * smack_msg_queue_msgctl - Smack access check for msg_queue
3164 * @cmd: what it wants to do
3166 * Returns 0 if current has the requested access, error code otherwise
3168 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3180 may = MAY_READWRITE;
3185 * System level information
3192 return smk_curacc_msq(isp, may);
3196 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3199 * @msqflg: access requested
3201 * Returns 0 if current has the requested access, error code otherwise
3203 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3208 may = smack_flags_to_may(msqflg);
3209 return smk_curacc_msq(isp, may);
3213 * smack_msg_queue_msgrcv - Smack access check for msg_queue
3220 * Returns 0 if current has read and write access, error code otherwise
3222 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp,
3223 struct msg_msg *msg,
3224 struct task_struct *target, long type,
3227 return smk_curacc_msq(isp, MAY_READWRITE);
3231 * smack_ipc_permission - Smack access for ipc_permission()
3232 * @ipp: the object permissions
3233 * @flag: access requested
3235 * Returns 0 if current has read and write access, error code otherwise
3237 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3239 struct smack_known **blob = smack_ipc(ipp);
3240 struct smack_known *iskp = *blob;
3241 int may = smack_flags_to_may(flag);
3242 struct smk_audit_info ad;
3246 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3247 ad.a.u.ipc_id = ipp->id;
3249 rc = smk_curacc(iskp, may, &ad);
3250 rc = smk_bu_current("svipc", iskp, may, rc);
3255 * smack_ipc_getsecid - Extract smack security id
3256 * @ipp: the object permissions
3257 * @secid: where result will be saved
3259 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3261 struct smack_known **blob = smack_ipc(ipp);
3262 struct smack_known *iskp = *blob;
3264 *secid = iskp->smk_secid;
3268 * smack_d_instantiate - Make sure the blob is correct on an inode
3269 * @opt_dentry: dentry where inode will be attached
3270 * @inode: the object
3272 * Set the inode's security blob if it hasn't been done already.
3274 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3276 struct super_block *sbp;
3277 struct superblock_smack *sbsp;
3278 struct inode_smack *isp;
3279 struct smack_known *skp;
3280 struct smack_known *ckp = smk_of_current();
3281 struct smack_known *final;
3282 char trattr[TRANS_TRUE_SIZE];
3290 isp = smack_inode(inode);
3293 * If the inode is already instantiated
3294 * take the quick way out
3296 if (isp->smk_flags & SMK_INODE_INSTANT)
3300 sbsp = smack_superblock(sbp);
3302 * We're going to use the superblock default label
3303 * if there's no label on the file.
3305 final = sbsp->smk_default;
3308 * If this is the root inode the superblock
3309 * may be in the process of initialization.
3310 * If that is the case use the root value out
3311 * of the superblock.
3313 if (opt_dentry->d_parent == opt_dentry) {
3314 switch (sbp->s_magic) {
3315 case CGROUP_SUPER_MAGIC:
3316 case CGROUP2_SUPER_MAGIC:
3318 * The cgroup filesystem is never mounted,
3319 * so there's no opportunity to set the mount
3322 sbsp->smk_root = &smack_known_star;
3323 sbsp->smk_default = &smack_known_star;
3324 isp->smk_inode = sbsp->smk_root;
3328 * What about shmem/tmpfs anonymous files with dentry
3329 * obtained from d_alloc_pseudo()?
3331 isp->smk_inode = smk_of_current();
3334 isp->smk_inode = smk_of_current();
3338 * Socket access is controlled by the socket
3339 * structures associated with the task involved.
3341 isp->smk_inode = &smack_known_star;
3344 isp->smk_inode = sbsp->smk_root;
3347 isp->smk_flags |= SMK_INODE_INSTANT;
3352 * This is pretty hackish.
3353 * Casey says that we shouldn't have to do
3354 * file system specific code, but it does help
3355 * with keeping it simple.
3357 switch (sbp->s_magic) {
3359 case CGROUP_SUPER_MAGIC:
3360 case CGROUP2_SUPER_MAGIC:
3362 * Casey says that it's a little embarrassing
3363 * that the smack file system doesn't do
3364 * extended attributes.
3366 * Cgroupfs is special
3368 final = &smack_known_star;
3370 case DEVPTS_SUPER_MAGIC:
3372 * devpts seems content with the label of the task.
3373 * Programs that change smack have to treat the
3378 case PROC_SUPER_MAGIC:
3380 * Casey says procfs appears not to care.
3381 * The superblock default suffices.
3386 * Device labels should come from the filesystem,
3387 * but watch out, because they're volitile,
3388 * getting recreated on every reboot.
3390 final = &smack_known_star;
3392 * If a smack value has been set we want to use it,
3393 * but since tmpfs isn't giving us the opportunity
3394 * to set mount options simulate setting the
3395 * superblock default.
3400 * This isn't an understood special case.
3401 * Get the value from the xattr.
3405 * UNIX domain sockets use lower level socket data.
3407 if (S_ISSOCK(inode->i_mode)) {
3408 final = &smack_known_star;
3412 * No xattr support means, alas, no SMACK label.
3413 * Use the aforeapplied default.
3414 * It would be curious if the label of the task
3415 * does not match that assigned.
3417 if (!(inode->i_opflags & IOP_XATTR))
3420 * Get the dentry for xattr.
3422 dp = dget(opt_dentry);
3423 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3424 if (!IS_ERR_OR_NULL(skp))
3428 * Transmuting directory
3430 if (S_ISDIR(inode->i_mode)) {
3432 * If this is a new directory and the label was
3433 * transmuted when the inode was initialized
3434 * set the transmute attribute on the directory
3435 * and mark the inode.
3437 * If there is a transmute attribute on the
3438 * directory mark the inode.
3440 if (isp->smk_flags & SMK_INODE_CHANGED) {
3441 isp->smk_flags &= ~SMK_INODE_CHANGED;
3442 rc = __vfs_setxattr(&init_user_ns, dp, inode,
3443 XATTR_NAME_SMACKTRANSMUTE,
3444 TRANS_TRUE, TRANS_TRUE_SIZE,
3447 rc = __vfs_getxattr(dp, inode,
3448 XATTR_NAME_SMACKTRANSMUTE, trattr,
3450 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3451 TRANS_TRUE_SIZE) != 0)
3455 transflag = SMK_INODE_TRANSMUTE;
3458 * Don't let the exec or mmap label be "*" or "@".
3460 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3461 if (IS_ERR(skp) || skp == &smack_known_star ||
3462 skp == &smack_known_web)
3464 isp->smk_task = skp;
3466 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3467 if (IS_ERR(skp) || skp == &smack_known_star ||
3468 skp == &smack_known_web)
3470 isp->smk_mmap = skp;
3477 isp->smk_inode = ckp;
3479 isp->smk_inode = final;
3481 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3487 * smack_getprocattr - Smack process attribute access
3488 * @p: the object task
3489 * @name: the name of the attribute in /proc/.../attr
3490 * @value: where to put the result
3492 * Places a copy of the task Smack into value
3494 * Returns the length of the smack label or an error code
3496 static int smack_getprocattr(struct task_struct *p, const char *name, char **value)
3498 struct smack_known *skp = smk_of_task_struct_obj(p);
3502 if (strcmp(name, "current") != 0)
3505 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3515 * smack_setprocattr - Smack process attribute setting
3516 * @name: the name of the attribute in /proc/.../attr
3517 * @value: the value to set
3518 * @size: the size of the value
3520 * Sets the Smack value of the task. Only setting self
3521 * is permitted and only with privilege
3523 * Returns the length of the smack label or an error code
3525 static int smack_setprocattr(const char *name, void *value, size_t size)
3527 struct task_smack *tsp = smack_cred(current_cred());
3529 struct smack_known *skp;
3530 struct smack_known_list_elem *sklep;
3533 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3536 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3539 if (strcmp(name, "current") != 0)
3542 skp = smk_import_entry(value, size);
3544 return PTR_ERR(skp);
3547 * No process is ever allowed the web ("@") label
3548 * and the star ("*") label.
3550 if (skp == &smack_known_web || skp == &smack_known_star)
3553 if (!smack_privileged(CAP_MAC_ADMIN)) {
3555 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3556 if (sklep->smk_label == skp) {
3564 new = prepare_creds();
3568 tsp = smack_cred(new);
3569 tsp->smk_task = skp;
3571 * process can change its label only once
3573 smk_destroy_label_list(&tsp->smk_relabel);
3580 * smack_unix_stream_connect - Smack access on UDS
3582 * @other: the other sock
3585 * Return 0 if a subject with the smack of sock could access
3586 * an object with the smack of other, otherwise an error code
3588 static int smack_unix_stream_connect(struct sock *sock,
3589 struct sock *other, struct sock *newsk)
3591 struct smack_known *skp;
3592 struct smack_known *okp;
3593 struct socket_smack *ssp = sock->sk_security;
3594 struct socket_smack *osp = other->sk_security;
3595 struct socket_smack *nsp = newsk->sk_security;
3596 struct smk_audit_info ad;
3599 struct lsm_network_audit net;
3602 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3606 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3607 smk_ad_setfield_u_net_sk(&ad, other);
3609 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3610 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3614 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3615 rc = smk_bu_note("UDS connect", okp, skp,
3621 * Cross reference the peer labels for SO_PEERSEC.
3624 nsp->smk_packet = ssp->smk_out;
3625 ssp->smk_packet = osp->smk_out;
3632 * smack_unix_may_send - Smack access on UDS
3634 * @other: the other socket
3636 * Return 0 if a subject with the smack of sock could access
3637 * an object with the smack of other, otherwise an error code
3639 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3641 struct socket_smack *ssp = sock->sk->sk_security;
3642 struct socket_smack *osp = other->sk->sk_security;
3643 struct smk_audit_info ad;
3647 struct lsm_network_audit net;
3649 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3650 smk_ad_setfield_u_net_sk(&ad, other->sk);
3653 if (smack_privileged(CAP_MAC_OVERRIDE))
3656 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3657 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3662 * smack_socket_sendmsg - Smack check based on destination host
3665 * @size: the size of the message
3667 * Return 0 if the current subject can write to the destination host.
3668 * For IPv4 this is only a question if the destination is a single label host.
3669 * For IPv6 this is a check against the label of the port.
3671 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3674 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3675 #if IS_ENABLED(CONFIG_IPV6)
3676 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3678 #ifdef SMACK_IPV6_SECMARK_LABELING
3679 struct socket_smack *ssp = sock->sk->sk_security;
3680 struct smack_known *rsp;
3685 * Perfectly reasonable for this to be NULL
3690 switch (sock->sk->sk_family) {
3692 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3693 sip->sin_family != AF_INET)
3695 rc = smk_ipv4_check(sock->sk, sip);
3697 #if IS_ENABLED(CONFIG_IPV6)
3699 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3700 sap->sin6_family != AF_INET6)
3702 #ifdef SMACK_IPV6_SECMARK_LABELING
3703 rsp = smack_ipv6host_label(sap);
3705 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3708 #ifdef SMACK_IPV6_PORT_LABELING
3709 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3711 #endif /* IS_ENABLED(CONFIG_IPV6) */
3718 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3719 * @sap: netlabel secattr
3720 * @ssp: socket security information
3722 * Returns a pointer to a Smack label entry found on the label list.
3724 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3725 struct socket_smack *ssp)
3727 struct smack_known *skp;
3733 * Netlabel found it in the cache.
3735 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3736 return (struct smack_known *)sap->cache->data;
3738 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3740 * Looks like a fallback, which gives us a secid.
3742 return smack_from_secid(sap->attr.secid);
3744 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3746 * Looks like a CIPSO packet.
3747 * If there are flags but no level netlabel isn't
3748 * behaving the way we expect it to.
3750 * Look it up in the label table
3751 * Without guidance regarding the smack value
3752 * for the packet fall back on the network
3756 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3757 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3760 * Compare the catsets. Use the netlbl APIs.
3762 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3763 if ((skp->smk_netlabel.flags &
3764 NETLBL_SECATTR_MLS_CAT) == 0)
3768 for (acat = -1, kcat = -1; acat == kcat; ) {
3769 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3771 kcat = netlbl_catmap_walk(
3772 skp->smk_netlabel.attr.mls.cat,
3774 if (acat < 0 || kcat < 0)
3787 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3788 return &smack_known_web;
3789 return &smack_known_star;
3792 * Without guidance regarding the smack value
3793 * for the packet fall back on the network
3796 return smack_net_ambient;
3799 #if IS_ENABLED(CONFIG_IPV6)
3800 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3804 int proto = -EINVAL;
3805 struct ipv6hdr _ipv6h;
3806 struct ipv6hdr *ip6;
3808 struct tcphdr _tcph, *th;
3809 struct udphdr _udph, *uh;
3810 struct dccp_hdr _dccph, *dh;
3814 offset = skb_network_offset(skb);
3815 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3818 sip->sin6_addr = ip6->saddr;
3820 nexthdr = ip6->nexthdr;
3821 offset += sizeof(_ipv6h);
3822 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3829 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3831 sip->sin6_port = th->source;
3834 case IPPROTO_UDPLITE:
3835 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3837 sip->sin6_port = uh->source;
3840 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3842 sip->sin6_port = dh->dccph_sport;
3847 #endif /* CONFIG_IPV6 */
3850 * smack_from_skb - Smack data from the secmark in an skb
3853 * Returns smack_known of the secmark or NULL if that won't work.
3855 #ifdef CONFIG_NETWORK_SECMARK
3856 static struct smack_known *smack_from_skb(struct sk_buff *skb)
3858 if (skb == NULL || skb->secmark == 0)
3861 return smack_from_secid(skb->secmark);
3864 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3871 * smack_from_netlbl - Smack data from the IP options in an skb
3872 * @sk: socket data came in on
3873 * @family: address family
3876 * Find the Smack label in the IP options. If it hasn't been
3877 * added to the netlabel cache, add it here.
3879 * Returns smack_known of the IP options or NULL if that won't work.
3881 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
3882 struct sk_buff *skb)
3884 struct netlbl_lsm_secattr secattr;
3885 struct socket_smack *ssp = NULL;
3886 struct smack_known *skp = NULL;
3888 netlbl_secattr_init(&secattr);
3891 ssp = sk->sk_security;
3893 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
3894 skp = smack_from_secattr(&secattr, ssp);
3895 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
3896 netlbl_cache_add(skb, family, &skp->smk_netlabel);
3899 netlbl_secattr_destroy(&secattr);
3905 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3909 * Returns 0 if the packet should be delivered, an error code otherwise
3911 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3913 struct socket_smack *ssp = sk->sk_security;
3914 struct smack_known *skp = NULL;
3916 struct smk_audit_info ad;
3917 u16 family = sk->sk_family;
3919 struct lsm_network_audit net;
3921 #if IS_ENABLED(CONFIG_IPV6)
3922 struct sockaddr_in6 sadd;
3925 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3927 #endif /* CONFIG_IPV6 */
3932 * If there is a secmark use it rather than the CIPSO label.
3933 * If there is no secmark fall back to CIPSO.
3934 * The secmark is assumed to reflect policy better.
3936 skp = smack_from_skb(skb);
3938 skp = smack_from_netlbl(sk, family, skb);
3940 skp = smack_net_ambient;
3944 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3945 ad.a.u.net->family = family;
3946 ad.a.u.net->netif = skb->skb_iif;
3947 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3950 * Receiving a packet requires that the other end
3951 * be able to write here. Read access is not required.
3952 * This is the simplist possible security model
3955 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3956 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3959 netlbl_skbuff_err(skb, family, rc, 0);
3961 #if IS_ENABLED(CONFIG_IPV6)
3963 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3964 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3965 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3967 #ifdef SMACK_IPV6_SECMARK_LABELING
3968 skp = smack_from_skb(skb);
3970 if (smk_ipv6_localhost(&sadd))
3972 skp = smack_ipv6host_label(&sadd);
3974 skp = smack_net_ambient;
3977 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3978 ad.a.u.net->family = family;
3979 ad.a.u.net->netif = skb->skb_iif;
3980 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3981 #endif /* CONFIG_AUDIT */
3982 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3983 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3985 #endif /* SMACK_IPV6_SECMARK_LABELING */
3986 #ifdef SMACK_IPV6_PORT_LABELING
3987 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3988 #endif /* SMACK_IPV6_PORT_LABELING */
3990 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3991 ICMPV6_ADM_PROHIBITED, 0);
3993 #endif /* CONFIG_IPV6 */
4000 * smack_socket_getpeersec_stream - pull in packet label
4002 * @optval: user's destination
4003 * @optlen: size thereof
4006 * returns zero on success, an error code otherwise
4008 static int smack_socket_getpeersec_stream(struct socket *sock,
4009 char __user *optval,
4010 int __user *optlen, unsigned len)
4012 struct socket_smack *ssp;
4017 ssp = sock->sk->sk_security;
4018 if (ssp->smk_packet != NULL) {
4019 rcp = ssp->smk_packet->smk_known;
4020 slen = strlen(rcp) + 1;
4025 else if (copy_to_user(optval, rcp, slen) != 0)
4028 if (put_user(slen, optlen) != 0)
4036 * smack_socket_getpeersec_dgram - pull in packet label
4037 * @sock: the peer socket
4039 * @secid: pointer to where to put the secid of the packet
4041 * Sets the netlabel socket state on sk from parent
4043 static int smack_socket_getpeersec_dgram(struct socket *sock,
4044 struct sk_buff *skb, u32 *secid)
4047 struct socket_smack *ssp = NULL;
4048 struct smack_known *skp;
4049 struct sock *sk = NULL;
4050 int family = PF_UNSPEC;
4051 u32 s = 0; /* 0 is the invalid secid */
4054 if (skb->protocol == htons(ETH_P_IP))
4056 #if IS_ENABLED(CONFIG_IPV6)
4057 else if (skb->protocol == htons(ETH_P_IPV6))
4059 #endif /* CONFIG_IPV6 */
4061 if (family == PF_UNSPEC && sock != NULL)
4062 family = sock->sk->sk_family;
4066 ssp = sock->sk->sk_security;
4067 s = ssp->smk_out->smk_secid;
4070 skp = smack_from_skb(skb);
4076 * Translate what netlabel gave us.
4080 skp = smack_from_netlbl(sk, family, skb);
4085 #ifdef SMACK_IPV6_SECMARK_LABELING
4086 skp = smack_from_skb(skb);
4099 * smack_sock_graft - Initialize a newly created socket with an existing sock
4101 * @parent: parent socket
4103 * Set the smk_{in,out} state of an existing sock based on the process that
4104 * is creating the new socket.
4106 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4108 struct socket_smack *ssp;
4109 struct smack_known *skp = smk_of_current();
4112 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4115 ssp = sk->sk_security;
4118 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4122 * smack_inet_conn_request - Smack access check on connect
4123 * @sk: socket involved
4127 * Returns 0 if a task with the packet label could write to
4128 * the socket, otherwise an error code
4130 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4131 struct request_sock *req)
4133 u16 family = sk->sk_family;
4134 struct smack_known *skp;
4135 struct socket_smack *ssp = sk->sk_security;
4136 struct sockaddr_in addr;
4138 struct smack_known *hskp;
4140 struct smk_audit_info ad;
4142 struct lsm_network_audit net;
4145 #if IS_ENABLED(CONFIG_IPV6)
4146 if (family == PF_INET6) {
4148 * Handle mapped IPv4 packets arriving
4149 * via IPv6 sockets. Don't set up netlabel
4150 * processing on IPv6.
4152 if (skb->protocol == htons(ETH_P_IP))
4157 #endif /* CONFIG_IPV6 */
4160 * If there is a secmark use it rather than the CIPSO label.
4161 * If there is no secmark fall back to CIPSO.
4162 * The secmark is assumed to reflect policy better.
4164 skp = smack_from_skb(skb);
4166 skp = smack_from_netlbl(sk, family, skb);
4168 skp = &smack_known_huh;
4172 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4173 ad.a.u.net->family = family;
4174 ad.a.u.net->netif = skb->skb_iif;
4175 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4178 * Receiving a packet requires that the other end be able to write
4179 * here. Read access is not required.
4181 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4182 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4187 * Save the peer's label in the request_sock so we can later setup
4188 * smk_packet in the child socket so that SO_PEERCRED can report it.
4190 req->peer_secid = skp->smk_secid;
4193 * We need to decide if we want to label the incoming connection here
4194 * if we do we only need to label the request_sock and the stack will
4195 * propagate the wire-label to the sock when it is created.
4198 addr.sin_addr.s_addr = hdr->saddr;
4200 hskp = smack_ipv4host_label(&addr);
4204 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4206 netlbl_req_delattr(req);
4212 * smack_inet_csk_clone - Copy the connection information to the new socket
4213 * @sk: the new socket
4214 * @req: the connection's request_sock
4216 * Transfer the connection's peer label to the newly created socket.
4218 static void smack_inet_csk_clone(struct sock *sk,
4219 const struct request_sock *req)
4221 struct socket_smack *ssp = sk->sk_security;
4222 struct smack_known *skp;
4224 if (req->peer_secid != 0) {
4225 skp = smack_from_secid(req->peer_secid);
4226 ssp->smk_packet = skp;
4228 ssp->smk_packet = NULL;
4232 * Key management security hooks
4234 * Casey has not tested key support very heavily.
4235 * The permission check is most likely too restrictive.
4236 * If you care about keys please have a look.
4241 * smack_key_alloc - Set the key security blob
4243 * @cred: the credentials to use
4246 * No allocation required
4250 static int smack_key_alloc(struct key *key, const struct cred *cred,
4251 unsigned long flags)
4253 struct smack_known *skp = smk_of_task(smack_cred(cred));
4255 key->security = skp;
4260 * smack_key_free - Clear the key security blob
4263 * Clear the blob pointer
4265 static void smack_key_free(struct key *key)
4267 key->security = NULL;
4271 * smack_key_permission - Smack access on a key
4272 * @key_ref: gets to the object
4273 * @cred: the credentials to use
4274 * @need_perm: requested key permission
4276 * Return 0 if the task has read and write to the object,
4277 * an error code otherwise
4279 static int smack_key_permission(key_ref_t key_ref,
4280 const struct cred *cred,
4281 enum key_need_perm need_perm)
4284 struct smk_audit_info ad;
4285 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4290 * Validate requested permissions
4292 switch (need_perm) {
4294 case KEY_NEED_SEARCH:
4296 request |= MAY_READ;
4298 case KEY_NEED_WRITE:
4300 case KEY_NEED_SETATTR:
4301 request |= MAY_WRITE;
4303 case KEY_NEED_UNSPECIFIED:
4304 case KEY_NEED_UNLINK:
4305 case KEY_SYSADMIN_OVERRIDE:
4306 case KEY_AUTHTOKEN_OVERRIDE:
4307 case KEY_DEFER_PERM_CHECK:
4313 keyp = key_ref_to_ptr(key_ref);
4317 * If the key hasn't been initialized give it access so that
4320 if (keyp->security == NULL)
4323 * This should not occur
4328 if (smack_privileged(CAP_MAC_OVERRIDE))
4332 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4333 ad.a.u.key_struct.key = keyp->serial;
4334 ad.a.u.key_struct.key_desc = keyp->description;
4336 rc = smk_access(tkp, keyp->security, request, &ad);
4337 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4342 * smack_key_getsecurity - Smack label tagging the key
4343 * @key points to the key to be queried
4344 * @_buffer points to a pointer that should be set to point to the
4345 * resulting string (if no label or an error occurs).
4346 * Return the length of the string (including terminating NUL) or -ve if
4348 * May also return 0 (and a NULL buffer pointer) if there is no label.
4350 static int smack_key_getsecurity(struct key *key, char **_buffer)
4352 struct smack_known *skp = key->security;
4356 if (key->security == NULL) {
4361 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4364 length = strlen(copy) + 1;
4371 #ifdef CONFIG_KEY_NOTIFICATIONS
4373 * smack_watch_key - Smack access to watch a key for notifications.
4374 * @key: The key to be watched
4376 * Return 0 if the @watch->cred has permission to read from the key object and
4377 * an error otherwise.
4379 static int smack_watch_key(struct key *key)
4381 struct smk_audit_info ad;
4382 struct smack_known *tkp = smk_of_current();
4388 * If the key hasn't been initialized give it access so that
4391 if (key->security == NULL)
4394 * This should not occur
4399 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4403 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4404 ad.a.u.key_struct.key = key->serial;
4405 ad.a.u.key_struct.key_desc = key->description;
4407 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4408 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4411 #endif /* CONFIG_KEY_NOTIFICATIONS */
4412 #endif /* CONFIG_KEYS */
4414 #ifdef CONFIG_WATCH_QUEUE
4416 * smack_post_notification - Smack access to post a notification to a queue
4417 * @w_cred: The credentials of the watcher.
4418 * @cred: The credentials of the event source (may be NULL).
4419 * @n: The notification message to be posted.
4421 static int smack_post_notification(const struct cred *w_cred,
4422 const struct cred *cred,
4423 struct watch_notification *n)
4425 struct smk_audit_info ad;
4426 struct smack_known *subj, *obj;
4429 /* Always let maintenance notifications through. */
4430 if (n->type == WATCH_TYPE_META)
4435 subj = smk_of_task(smack_cred(cred));
4436 obj = smk_of_task(smack_cred(w_cred));
4438 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4439 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4440 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4443 #endif /* CONFIG_WATCH_QUEUE */
4448 * Audit requires a unique representation of each Smack specific
4449 * rule. This unique representation is used to distinguish the
4450 * object to be audited from remaining kernel objects and also
4451 * works as a glue between the audit hooks.
4453 * Since repository entries are added but never deleted, we'll use
4454 * the smack_known label address related to the given audit rule as
4455 * the needed unique representation. This also better fits the smack
4456 * model where nearly everything is a label.
4461 * smack_audit_rule_init - Initialize a smack audit rule
4462 * @field: audit rule fields given from user-space (audit.h)
4463 * @op: required testing operator (=, !=, >, <, ...)
4464 * @rulestr: smack label to be audited
4465 * @vrule: pointer to save our own audit rule representation
4467 * Prepare to audit cases where (@field @op @rulestr) is true.
4468 * The label to be audited is created if necessay.
4470 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4472 struct smack_known *skp;
4473 char **rule = (char **)vrule;
4476 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4479 if (op != Audit_equal && op != Audit_not_equal)
4482 skp = smk_import_entry(rulestr, 0);
4484 return PTR_ERR(skp);
4486 *rule = skp->smk_known;
4492 * smack_audit_rule_known - Distinguish Smack audit rules
4493 * @krule: rule of interest, in Audit kernel representation format
4495 * This is used to filter Smack rules from remaining Audit ones.
4496 * If it's proved that this rule belongs to us, the
4497 * audit_rule_match hook will be called to do the final judgement.
4499 static int smack_audit_rule_known(struct audit_krule *krule)
4501 struct audit_field *f;
4504 for (i = 0; i < krule->field_count; i++) {
4505 f = &krule->fields[i];
4507 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4515 * smack_audit_rule_match - Audit given object ?
4516 * @secid: security id for identifying the object to test
4517 * @field: audit rule flags given from user-space
4518 * @op: required testing operator
4519 * @vrule: smack internal rule presentation
4521 * The core Audit hook. It's used to take the decision of
4522 * whether to audit or not to audit a given object.
4524 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4526 struct smack_known *skp;
4529 if (unlikely(!rule)) {
4530 WARN_ONCE(1, "Smack: missing rule\n");
4534 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4537 skp = smack_from_secid(secid);
4540 * No need to do string comparisons. If a match occurs,
4541 * both pointers will point to the same smack_known
4544 if (op == Audit_equal)
4545 return (rule == skp->smk_known);
4546 if (op == Audit_not_equal)
4547 return (rule != skp->smk_known);
4553 * There is no need for a smack_audit_rule_free hook.
4554 * No memory was allocated.
4557 #endif /* CONFIG_AUDIT */
4560 * smack_ismaclabel - check if xattr @name references a smack MAC label
4561 * @name: Full xattr name to check.
4563 static int smack_ismaclabel(const char *name)
4565 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4570 * smack_secid_to_secctx - return the smack label for a secid
4571 * @secid: incoming integer
4572 * @secdata: destination
4573 * @seclen: how long it is
4575 * Exists for networking code.
4577 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4579 struct smack_known *skp = smack_from_secid(secid);
4582 *secdata = skp->smk_known;
4583 *seclen = strlen(skp->smk_known);
4588 * smack_secctx_to_secid - return the secid for a smack label
4589 * @secdata: smack label
4590 * @seclen: how long result is
4591 * @secid: outgoing integer
4593 * Exists for audit and networking code.
4595 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4597 struct smack_known *skp = smk_find_entry(secdata);
4600 *secid = skp->smk_secid;
4607 * There used to be a smack_release_secctx hook
4608 * that did nothing back when hooks were in a vector.
4609 * Now that there's a list such a hook adds cost.
4612 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4614 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4618 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4620 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK,
4624 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4626 struct smack_known *skp = smk_of_inode(inode);
4628 *ctx = skp->smk_known;
4629 *ctxlen = strlen(skp->smk_known);
4633 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4636 struct task_smack *tsp;
4637 struct smack_known *skp;
4638 struct inode_smack *isp;
4639 struct cred *new_creds = *new;
4641 if (new_creds == NULL) {
4642 new_creds = prepare_creds();
4643 if (new_creds == NULL)
4647 tsp = smack_cred(new_creds);
4650 * Get label from overlay inode and set it in create_sid
4652 isp = smack_inode(d_inode(dentry));
4653 skp = isp->smk_inode;
4654 tsp->smk_task = skp;
4659 static int smack_inode_copy_up_xattr(const char *name)
4662 * Return 1 if this is the smack access Smack attribute.
4664 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4670 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4672 const struct cred *old,
4675 struct task_smack *otsp = smack_cred(old);
4676 struct task_smack *ntsp = smack_cred(new);
4677 struct inode_smack *isp;
4681 * Use the process credential unless all of
4682 * the transmuting criteria are met
4684 ntsp->smk_task = otsp->smk_task;
4687 * the attribute of the containing directory
4689 isp = smack_inode(d_inode(dentry->d_parent));
4691 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4693 may = smk_access_entry(otsp->smk_task->smk_known,
4694 isp->smk_inode->smk_known,
4695 &otsp->smk_task->smk_rules);
4699 * If the directory is transmuting and the rule
4700 * providing access is transmuting use the containing
4701 * directory label instead of the process label.
4703 if (may > 0 && (may & MAY_TRANSMUTE))
4704 ntsp->smk_task = isp->smk_inode;
4709 #ifdef CONFIG_IO_URING
4711 * smack_uring_override_creds - Is io_uring cred override allowed?
4712 * @new: the target creds
4714 * Check to see if the current task is allowed to override it's credentials
4715 * to service an io_uring operation.
4717 static int smack_uring_override_creds(const struct cred *new)
4719 struct task_smack *tsp = smack_cred(current_cred());
4720 struct task_smack *nsp = smack_cred(new);
4723 * Allow the degenerate case where the new Smack value is
4724 * the same as the current Smack value.
4726 if (tsp->smk_task == nsp->smk_task)
4729 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4736 * smack_uring_sqpoll - check if a io_uring polling thread can be created
4738 * Check to see if the current task is allowed to create a new io_uring
4739 * kernel polling thread.
4741 static int smack_uring_sqpoll(void)
4743 if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred()))
4750 * smack_uring_cmd - check on file operations for io_uring
4751 * @ioucmd: the command in question
4753 * Make a best guess about whether a io_uring "command" should
4754 * be allowed. Use the same logic used for determining if the
4755 * file could be opened for read in the absence of better criteria.
4757 static int smack_uring_cmd(struct io_uring_cmd *ioucmd)
4759 struct file *file = ioucmd->file;
4760 struct smk_audit_info ad;
4761 struct task_smack *tsp;
4762 struct inode *inode;
4768 tsp = smack_cred(file->f_cred);
4769 inode = file_inode(file);
4771 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
4772 smk_ad_setfield_u_fs_path(&ad, file->f_path);
4773 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
4774 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
4779 #endif /* CONFIG_IO_URING */
4781 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4782 .lbs_cred = sizeof(struct task_smack),
4783 .lbs_file = sizeof(struct smack_known *),
4784 .lbs_inode = sizeof(struct inode_smack),
4785 .lbs_ipc = sizeof(struct smack_known *),
4786 .lbs_msg_msg = sizeof(struct smack_known *),
4787 .lbs_superblock = sizeof(struct superblock_smack),
4790 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4791 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4792 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4793 LSM_HOOK_INIT(syslog, smack_syslog),
4795 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4796 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4798 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4799 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4800 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4801 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4802 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4804 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
4806 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4807 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4808 LSM_HOOK_INIT(inode_link, smack_inode_link),
4809 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4810 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4811 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4812 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4813 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4814 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4815 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4816 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4817 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4818 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4819 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4820 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4821 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4822 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4824 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4825 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4826 LSM_HOOK_INIT(file_lock, smack_file_lock),
4827 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4828 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4829 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4830 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4831 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4832 LSM_HOOK_INIT(file_receive, smack_file_receive),
4834 LSM_HOOK_INIT(file_open, smack_file_open),
4836 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4837 LSM_HOOK_INIT(cred_free, smack_cred_free),
4838 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4839 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4840 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4841 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4842 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4843 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4844 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4845 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4846 LSM_HOOK_INIT(current_getsecid_subj, smack_current_getsecid_subj),
4847 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
4848 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4849 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4850 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4851 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4852 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4853 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4854 LSM_HOOK_INIT(task_kill, smack_task_kill),
4855 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4857 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4858 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4860 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4862 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4863 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4864 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4865 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4866 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4868 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4869 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4870 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4871 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4873 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4874 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4875 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4876 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4878 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4880 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4881 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4883 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4884 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4886 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4887 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4888 #ifdef SMACK_IPV6_PORT_LABELING
4889 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4891 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4892 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4893 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4894 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4895 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4896 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4897 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4898 LSM_HOOK_INIT(sk_clone_security, smack_sk_clone_security),
4899 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4900 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4901 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4903 /* key management security hooks */
4905 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4906 LSM_HOOK_INIT(key_free, smack_key_free),
4907 LSM_HOOK_INIT(key_permission, smack_key_permission),
4908 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4909 #ifdef CONFIG_KEY_NOTIFICATIONS
4910 LSM_HOOK_INIT(watch_key, smack_watch_key),
4912 #endif /* CONFIG_KEYS */
4914 #ifdef CONFIG_WATCH_QUEUE
4915 LSM_HOOK_INIT(post_notification, smack_post_notification),
4920 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4921 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4922 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4923 #endif /* CONFIG_AUDIT */
4925 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4926 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4927 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4928 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4929 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4930 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4931 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4932 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4933 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4934 #ifdef CONFIG_IO_URING
4935 LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds),
4936 LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll),
4937 LSM_HOOK_INIT(uring_cmd, smack_uring_cmd),
4942 static __init void init_smack_known_list(void)
4945 * Initialize rule list locks
4947 mutex_init(&smack_known_huh.smk_rules_lock);
4948 mutex_init(&smack_known_hat.smk_rules_lock);
4949 mutex_init(&smack_known_floor.smk_rules_lock);
4950 mutex_init(&smack_known_star.smk_rules_lock);
4951 mutex_init(&smack_known_web.smk_rules_lock);
4953 * Initialize rule lists
4955 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4956 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4957 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4958 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4959 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4961 * Create the known labels list
4963 smk_insert_entry(&smack_known_huh);
4964 smk_insert_entry(&smack_known_hat);
4965 smk_insert_entry(&smack_known_star);
4966 smk_insert_entry(&smack_known_floor);
4967 smk_insert_entry(&smack_known_web);
4971 * smack_init - initialize the smack system
4973 * Returns 0 on success, -ENOMEM is there's no memory
4975 static __init int smack_init(void)
4977 struct cred *cred = (struct cred *) current->cred;
4978 struct task_smack *tsp;
4980 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4981 if (!smack_rule_cache)
4985 * Set the security state for the initial task.
4987 tsp = smack_cred(cred);
4988 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4993 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4996 pr_info("Smack: Initializing.\n");
4997 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4998 pr_info("Smack: Netfilter enabled.\n");
5000 #ifdef SMACK_IPV6_PORT_LABELING
5001 pr_info("Smack: IPv6 port labeling enabled.\n");
5003 #ifdef SMACK_IPV6_SECMARK_LABELING
5004 pr_info("Smack: IPv6 Netfilter enabled.\n");
5007 /* initialize the smack_known_list */
5008 init_smack_known_list();
5014 * Smack requires early initialization in order to label
5015 * all processes and objects when they are created.
5017 DEFINE_LSM(smack) = {
5019 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
5020 .blobs = &smack_blob_sizes,