1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
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>
47 #define TRANS_TRUE "TRUE"
48 #define TRANS_TRUE_SIZE 4
50 #define SMK_CONNECTING 0
51 #define SMK_RECEIVING 1
54 #ifdef SMACK_IPV6_PORT_LABELING
55 static DEFINE_MUTEX(smack_ipv6_lock);
56 static LIST_HEAD(smk_ipv6_port_list);
58 struct kmem_cache *smack_rule_cache;
59 int smack_enabled __initdata;
61 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
66 } smk_mount_opts[] = {
67 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
68 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
72 static int match_opt_prefix(char *s, int l, char **arg)
76 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
77 size_t len = smk_mount_opts[i].len;
78 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
80 if (len == l || s[len] != '=')
83 return smk_mount_opts[i].opt;
88 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
89 static char *smk_bu_mess[] = {
90 "Bringup Error", /* Unused */
91 "Bringup", /* SMACK_BRINGUP_ALLOW */
92 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
93 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
96 static void smk_bu_mode(int mode, char *s)
102 if (mode & MAY_WRITE)
106 if (mode & MAY_APPEND)
108 if (mode & MAY_TRANSMUTE)
118 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
119 static int smk_bu_note(char *note, struct smack_known *sskp,
120 struct smack_known *oskp, int mode, int rc)
122 char acc[SMK_NUM_ACCESS_TYPE + 1];
126 if (rc > SMACK_UNCONFINED_OBJECT)
129 smk_bu_mode(mode, acc);
130 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
131 sskp->smk_known, oskp->smk_known, acc, note);
135 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
138 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
139 static int smk_bu_current(char *note, struct smack_known *oskp,
142 struct task_smack *tsp = smack_cred(current_cred());
143 char acc[SMK_NUM_ACCESS_TYPE + 1];
147 if (rc > SMACK_UNCONFINED_OBJECT)
150 smk_bu_mode(mode, acc);
151 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
152 tsp->smk_task->smk_known, oskp->smk_known,
153 acc, current->comm, note);
157 #define smk_bu_current(note, oskp, mode, RC) (RC)
160 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
161 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
163 struct task_smack *tsp = smack_cred(current_cred());
164 struct smack_known *smk_task = smk_of_task_struct_obj(otp);
165 char acc[SMK_NUM_ACCESS_TYPE + 1];
169 if (rc > SMACK_UNCONFINED_OBJECT)
172 smk_bu_mode(mode, acc);
173 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
174 tsp->smk_task->smk_known, smk_task->smk_known, acc,
175 current->comm, otp->comm);
179 #define smk_bu_task(otp, mode, RC) (RC)
182 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
183 static int smk_bu_inode(struct inode *inode, int mode, int rc)
185 struct task_smack *tsp = smack_cred(current_cred());
186 struct inode_smack *isp = smack_inode(inode);
187 char acc[SMK_NUM_ACCESS_TYPE + 1];
189 if (isp->smk_flags & SMK_INODE_IMPURE)
190 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
191 inode->i_sb->s_id, inode->i_ino, current->comm);
195 if (rc > SMACK_UNCONFINED_OBJECT)
197 if (rc == SMACK_UNCONFINED_SUBJECT &&
198 (mode & (MAY_WRITE | MAY_APPEND)))
199 isp->smk_flags |= SMK_INODE_IMPURE;
201 smk_bu_mode(mode, acc);
203 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
204 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
205 inode->i_sb->s_id, inode->i_ino, current->comm);
209 #define smk_bu_inode(inode, mode, RC) (RC)
212 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
213 static int smk_bu_file(struct file *file, int mode, int rc)
215 struct task_smack *tsp = smack_cred(current_cred());
216 struct smack_known *sskp = tsp->smk_task;
217 struct inode *inode = file_inode(file);
218 struct inode_smack *isp = smack_inode(inode);
219 char acc[SMK_NUM_ACCESS_TYPE + 1];
221 if (isp->smk_flags & SMK_INODE_IMPURE)
222 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
223 inode->i_sb->s_id, inode->i_ino, current->comm);
227 if (rc > SMACK_UNCONFINED_OBJECT)
230 smk_bu_mode(mode, acc);
231 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
232 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
233 inode->i_sb->s_id, inode->i_ino, file,
238 #define smk_bu_file(file, mode, RC) (RC)
241 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
242 static int smk_bu_credfile(const struct cred *cred, struct file *file,
245 struct task_smack *tsp = smack_cred(cred);
246 struct smack_known *sskp = tsp->smk_task;
247 struct inode *inode = file_inode(file);
248 struct inode_smack *isp = smack_inode(inode);
249 char acc[SMK_NUM_ACCESS_TYPE + 1];
251 if (isp->smk_flags & SMK_INODE_IMPURE)
252 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
253 inode->i_sb->s_id, inode->i_ino, current->comm);
257 if (rc > SMACK_UNCONFINED_OBJECT)
260 smk_bu_mode(mode, acc);
261 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
262 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
263 inode->i_sb->s_id, inode->i_ino, file,
268 #define smk_bu_credfile(cred, file, mode, RC) (RC)
272 * smk_fetch - Fetch the smack label from a file.
273 * @name: type of the label (attribute)
274 * @ip: a pointer to the inode
275 * @dp: a pointer to the dentry
277 * Returns a pointer to the master list entry for the Smack label,
278 * NULL if there was no label to fetch, or an error code.
280 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
285 struct smack_known *skp = NULL;
287 if (!(ip->i_opflags & IOP_XATTR))
288 return ERR_PTR(-EOPNOTSUPP);
290 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
292 return ERR_PTR(-ENOMEM);
294 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
300 skp = smk_import_entry(buffer, rc);
308 * init_inode_smack - initialize an inode security blob
309 * @inode: inode to extract the info from
310 * @skp: a pointer to the Smack label entry to use in the blob
313 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
315 struct inode_smack *isp = smack_inode(inode);
317 isp->smk_inode = skp;
322 * init_task_smack - initialize a task security blob
323 * @tsp: blob to initialize
324 * @task: a pointer to the Smack label for the running task
325 * @forked: a pointer to the Smack label for the forked task
328 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
329 struct smack_known *forked)
331 tsp->smk_task = task;
332 tsp->smk_forked = forked;
333 INIT_LIST_HEAD(&tsp->smk_rules);
334 INIT_LIST_HEAD(&tsp->smk_relabel);
335 mutex_init(&tsp->smk_rules_lock);
339 * smk_copy_rules - copy a rule set
340 * @nhead: new rules header pointer
341 * @ohead: old rules header pointer
342 * @gfp: type of the memory for the allocation
344 * Returns 0 on success, -ENOMEM on error
346 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
349 struct smack_rule *nrp;
350 struct smack_rule *orp;
353 list_for_each_entry_rcu(orp, ohead, list) {
354 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
360 list_add_rcu(&nrp->list, nhead);
366 * smk_copy_relabel - copy smk_relabel labels list
367 * @nhead: new rules header pointer
368 * @ohead: old rules header pointer
369 * @gfp: type of the memory for the allocation
371 * Returns 0 on success, -ENOMEM on error
373 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
376 struct smack_known_list_elem *nklep;
377 struct smack_known_list_elem *oklep;
379 list_for_each_entry(oklep, ohead, list) {
380 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
382 smk_destroy_label_list(nhead);
385 nklep->smk_label = oklep->smk_label;
386 list_add(&nklep->list, nhead);
393 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
394 * @mode: input mode in form of PTRACE_MODE_*
396 * Returns a converted MAY_* mode usable by smack rules
398 static inline unsigned int smk_ptrace_mode(unsigned int mode)
400 if (mode & PTRACE_MODE_ATTACH)
401 return MAY_READWRITE;
402 if (mode & PTRACE_MODE_READ)
409 * smk_ptrace_rule_check - helper for ptrace access
410 * @tracer: tracer process
411 * @tracee_known: label entry of the process that's about to be traced
412 * @mode: ptrace attachment mode (PTRACE_MODE_*)
413 * @func: name of the function that called us, used for audit
415 * Returns 0 on access granted, -error on error
417 static int smk_ptrace_rule_check(struct task_struct *tracer,
418 struct smack_known *tracee_known,
419 unsigned int mode, const char *func)
422 struct smk_audit_info ad, *saip = NULL;
423 struct task_smack *tsp;
424 struct smack_known *tracer_known;
425 const struct cred *tracercred;
427 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
428 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
429 smk_ad_setfield_u_tsk(&ad, tracer);
434 tracercred = __task_cred(tracer);
435 tsp = smack_cred(tracercred);
436 tracer_known = smk_of_task(tsp);
438 if ((mode & PTRACE_MODE_ATTACH) &&
439 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
440 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
441 if (tracer_known->smk_known == tracee_known->smk_known)
443 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
445 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
451 smack_log(tracer_known->smk_known,
452 tracee_known->smk_known,
459 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
460 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
468 * We he, that is fun!
472 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
473 * @ctp: child task pointer
474 * @mode: ptrace attachment mode (PTRACE_MODE_*)
476 * Returns 0 if access is OK, an error code otherwise
478 * Do the capability checks.
480 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
482 struct smack_known *skp;
484 skp = smk_of_task_struct_obj(ctp);
486 return smk_ptrace_rule_check(current, skp, mode, __func__);
490 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
491 * @ptp: parent task pointer
493 * Returns 0 if access is OK, an error code otherwise
495 * Do the capability checks, and require PTRACE_MODE_ATTACH.
497 static int smack_ptrace_traceme(struct task_struct *ptp)
500 struct smack_known *skp;
502 skp = smk_of_task(smack_cred(current_cred()));
504 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
509 * smack_syslog - Smack approval on syslog
510 * @typefrom_file: unused
512 * Returns 0 on success, error code otherwise.
514 static int smack_syslog(int typefrom_file)
517 struct smack_known *skp = smk_of_current();
519 if (smack_privileged(CAP_MAC_OVERRIDE))
522 if (smack_syslog_label != NULL && smack_syslog_label != skp)
533 * smack_sb_alloc_security - allocate a superblock blob
534 * @sb: the superblock getting the blob
536 * Returns 0 on success or -ENOMEM on error.
538 static int smack_sb_alloc_security(struct super_block *sb)
540 struct superblock_smack *sbsp = smack_superblock(sb);
542 sbsp->smk_root = &smack_known_floor;
543 sbsp->smk_default = &smack_known_floor;
544 sbsp->smk_floor = &smack_known_floor;
545 sbsp->smk_hat = &smack_known_hat;
547 * SMK_SB_INITIALIZED will be zero from kzalloc.
553 struct smack_mnt_opts {
554 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
557 static void smack_free_mnt_opts(void *mnt_opts)
559 struct smack_mnt_opts *opts = mnt_opts;
560 kfree(opts->fsdefault);
561 kfree(opts->fsfloor);
564 kfree(opts->fstransmute);
568 static int smack_add_opt(int token, const char *s, void **mnt_opts)
570 struct smack_mnt_opts *opts = *mnt_opts;
573 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
602 case Opt_fstransmute:
603 if (opts->fstransmute)
605 opts->fstransmute = s;
611 pr_warn("Smack: duplicate mount options\n");
616 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
617 * @fc: The new filesystem context.
618 * @src_fc: The source filesystem context being duplicated.
620 * Returns 0 on success or -ENOMEM on error.
622 static int smack_fs_context_dup(struct fs_context *fc,
623 struct fs_context *src_fc)
625 struct smack_mnt_opts *dst, *src = src_fc->security;
630 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
635 if (src->fsdefault) {
636 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
641 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
646 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
651 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
655 if (src->fstransmute) {
656 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
657 if (!dst->fstransmute)
663 static const struct fs_parameter_spec smack_fs_parameters[] = {
664 fsparam_string("smackfsdef", Opt_fsdefault),
665 fsparam_string("smackfsdefault", Opt_fsdefault),
666 fsparam_string("smackfsfloor", Opt_fsfloor),
667 fsparam_string("smackfshat", Opt_fshat),
668 fsparam_string("smackfsroot", Opt_fsroot),
669 fsparam_string("smackfstransmute", Opt_fstransmute),
674 * smack_fs_context_parse_param - Parse a single mount parameter
675 * @fc: The new filesystem context being constructed.
676 * @param: The parameter.
678 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
681 static int smack_fs_context_parse_param(struct fs_context *fc,
682 struct fs_parameter *param)
684 struct fs_parse_result result;
687 opt = fs_parse(fc, smack_fs_parameters, param, &result);
691 rc = smack_add_opt(opt, param->string, &fc->security);
693 param->string = NULL;
697 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
699 char *from = options, *to = options;
703 char *next = strchr(from, ',');
712 token = match_opt_prefix(from, len, &arg);
713 if (token != Opt_error) {
714 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
715 rc = smack_add_opt(token, arg, mnt_opts);
719 smack_free_mnt_opts(*mnt_opts);
724 if (!first) { // copy with preceding comma
729 memmove(to, from, len);
742 * smack_set_mnt_opts - set Smack specific mount options
743 * @sb: the file system superblock
744 * @mnt_opts: Smack mount options
745 * @kern_flags: mount option from kernel space or user space
746 * @set_kern_flags: where to store converted mount opts
748 * Returns 0 on success, an error code on failure
750 * Allow filesystems with binary mount data to explicitly set Smack mount
753 static int smack_set_mnt_opts(struct super_block *sb,
755 unsigned long kern_flags,
756 unsigned long *set_kern_flags)
758 struct dentry *root = sb->s_root;
759 struct inode *inode = d_backing_inode(root);
760 struct superblock_smack *sp = smack_superblock(sb);
761 struct inode_smack *isp;
762 struct smack_known *skp;
763 struct smack_mnt_opts *opts = mnt_opts;
764 bool transmute = false;
766 if (sp->smk_flags & SMK_SB_INITIALIZED)
769 if (inode->i_security == NULL) {
770 int rc = lsm_inode_alloc(inode);
776 if (!smack_privileged(CAP_MAC_ADMIN)) {
778 * Unprivileged mounts don't get to specify Smack values.
783 * Unprivileged mounts get root and default from the caller.
785 skp = smk_of_current();
787 sp->smk_default = skp;
789 * For a handful of fs types with no user-controlled
790 * backing store it's okay to trust security labels
791 * in the filesystem. The rest are untrusted.
793 if (sb->s_user_ns != &init_user_ns &&
794 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
795 sb->s_magic != RAMFS_MAGIC) {
797 sp->smk_flags |= SMK_SB_UNTRUSTED;
801 sp->smk_flags |= SMK_SB_INITIALIZED;
804 if (opts->fsdefault) {
805 skp = smk_import_entry(opts->fsdefault, 0);
808 sp->smk_default = skp;
811 skp = smk_import_entry(opts->fsfloor, 0);
817 skp = smk_import_entry(opts->fshat, 0);
823 skp = smk_import_entry(opts->fsroot, 0);
828 if (opts->fstransmute) {
829 skp = smk_import_entry(opts->fstransmute, 0);
838 * Initialize the root inode.
840 init_inode_smack(inode, sp->smk_root);
843 isp = smack_inode(inode);
844 isp->smk_flags |= SMK_INODE_TRANSMUTE;
851 * smack_sb_statfs - Smack check on statfs
852 * @dentry: identifies the file system in question
854 * Returns 0 if current can read the floor of the filesystem,
855 * and error code otherwise
857 static int smack_sb_statfs(struct dentry *dentry)
859 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
861 struct smk_audit_info ad;
863 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
864 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
866 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
867 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
876 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
877 * @bprm: the exec information
879 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
881 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
883 struct inode *inode = file_inode(bprm->file);
884 struct task_smack *bsp = smack_cred(bprm->cred);
885 struct inode_smack *isp;
886 struct superblock_smack *sbsp;
889 isp = smack_inode(inode);
890 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
893 sbsp = smack_superblock(inode->i_sb);
894 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
895 isp->smk_task != sbsp->smk_root)
898 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
899 struct task_struct *tracer;
903 tracer = ptrace_parent(current);
904 if (likely(tracer != NULL))
905 rc = smk_ptrace_rule_check(tracer,
914 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
917 bsp->smk_task = isp->smk_task;
918 bprm->per_clear |= PER_CLEAR_ON_SETID;
920 /* Decide if this is a secure exec. */
921 if (bsp->smk_task != bsp->smk_forked)
922 bprm->secureexec = 1;
932 * smack_inode_alloc_security - allocate an inode blob
933 * @inode: the inode in need of a blob
937 static int smack_inode_alloc_security(struct inode *inode)
939 struct smack_known *skp = smk_of_current();
941 init_inode_smack(inode, skp);
946 * smack_inode_init_security - copy out the smack from an inode
947 * @inode: the newly created inode
948 * @dir: containing directory object
950 * @name: where to put the attribute name
951 * @value: where to put the attribute value
952 * @len: where to put the length of the attribute
954 * Returns 0 if it all works out, -ENOMEM if there's no memory
956 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
957 const struct qstr *qstr, const char **name,
958 void **value, size_t *len)
960 struct inode_smack *issp = smack_inode(inode);
961 struct smack_known *skp = smk_of_current();
962 struct smack_known *isp = smk_of_inode(inode);
963 struct smack_known *dsp = smk_of_inode(dir);
967 *name = XATTR_SMACK_SUFFIX;
971 may = smk_access_entry(skp->smk_known, dsp->smk_known,
976 * If the access rule allows transmutation and
977 * the directory requests transmutation then
978 * by all means transmute.
979 * Mark the inode as changed.
981 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
982 smk_inode_transmutable(dir)) {
984 issp->smk_flags |= SMK_INODE_CHANGED;
987 *value = kstrdup(isp->smk_known, GFP_NOFS);
991 *len = strlen(isp->smk_known);
998 * smack_inode_link - Smack check on link
999 * @old_dentry: the existing object
1001 * @new_dentry: the new object
1003 * Returns 0 if access is permitted, an error code otherwise
1005 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1006 struct dentry *new_dentry)
1008 struct smack_known *isp;
1009 struct smk_audit_info ad;
1012 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1013 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1015 isp = smk_of_inode(d_backing_inode(old_dentry));
1016 rc = smk_curacc(isp, MAY_WRITE, &ad);
1017 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1019 if (rc == 0 && d_is_positive(new_dentry)) {
1020 isp = smk_of_inode(d_backing_inode(new_dentry));
1021 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1022 rc = smk_curacc(isp, MAY_WRITE, &ad);
1023 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1030 * smack_inode_unlink - Smack check on inode deletion
1031 * @dir: containing directory object
1032 * @dentry: file to unlink
1034 * Returns 0 if current can write the containing directory
1035 * and the object, error code otherwise
1037 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1039 struct inode *ip = d_backing_inode(dentry);
1040 struct smk_audit_info ad;
1043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1044 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1047 * You need write access to the thing you're unlinking
1049 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1050 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1053 * You also need write access to the containing directory
1055 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1056 smk_ad_setfield_u_fs_inode(&ad, dir);
1057 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1058 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1064 * smack_inode_rmdir - Smack check on directory deletion
1065 * @dir: containing directory object
1066 * @dentry: directory to unlink
1068 * Returns 0 if current can write the containing directory
1069 * and the directory, error code otherwise
1071 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1073 struct smk_audit_info ad;
1076 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1077 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1080 * You need write access to the thing you're removing
1082 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1083 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1086 * You also need write access to the containing directory
1088 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1089 smk_ad_setfield_u_fs_inode(&ad, dir);
1090 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1091 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1098 * smack_inode_rename - Smack check on rename
1099 * @old_inode: unused
1100 * @old_dentry: the old object
1101 * @new_inode: unused
1102 * @new_dentry: the new object
1104 * Read and write access is required on both the old and
1107 * Returns 0 if access is permitted, an error code otherwise
1109 static int smack_inode_rename(struct inode *old_inode,
1110 struct dentry *old_dentry,
1111 struct inode *new_inode,
1112 struct dentry *new_dentry)
1115 struct smack_known *isp;
1116 struct smk_audit_info ad;
1118 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1119 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1121 isp = smk_of_inode(d_backing_inode(old_dentry));
1122 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1123 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1125 if (rc == 0 && d_is_positive(new_dentry)) {
1126 isp = smk_of_inode(d_backing_inode(new_dentry));
1127 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1128 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1129 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1135 * smack_inode_permission - Smack version of permission()
1136 * @inode: the inode in question
1137 * @mask: the access requested
1139 * This is the important Smack hook.
1141 * Returns 0 if access is permitted, an error code otherwise
1143 static int smack_inode_permission(struct inode *inode, int mask)
1145 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
1146 struct smk_audit_info ad;
1147 int no_block = mask & MAY_NOT_BLOCK;
1150 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1152 * No permission to check. Existence test. Yup, it's there.
1157 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1158 if (smk_of_inode(inode) != sbsp->smk_root)
1162 /* May be droppable after audit */
1165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1166 smk_ad_setfield_u_fs_inode(&ad, inode);
1167 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1168 rc = smk_bu_inode(inode, mask, rc);
1173 * smack_inode_setattr - Smack check for setting attributes
1174 * @dentry: the object
1175 * @iattr: for the force flag
1177 * Returns 0 if access is permitted, an error code otherwise
1179 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1181 struct smk_audit_info ad;
1185 * Need to allow for clearing the setuid bit.
1187 if (iattr->ia_valid & ATTR_FORCE)
1189 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1190 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1192 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1193 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1198 * smack_inode_getattr - Smack check for getting attributes
1199 * @path: path to extract the info from
1201 * Returns 0 if access is permitted, an error code otherwise
1203 static int smack_inode_getattr(const struct path *path)
1205 struct smk_audit_info ad;
1206 struct inode *inode = d_backing_inode(path->dentry);
1209 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1210 smk_ad_setfield_u_fs_path(&ad, *path);
1211 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1212 rc = smk_bu_inode(inode, MAY_READ, rc);
1217 * smack_inode_setxattr - Smack check for setting xattrs
1218 * @mnt_userns: active user namespace
1219 * @dentry: the object
1220 * @name: name of the attribute
1221 * @value: value of the attribute
1222 * @size: size of the value
1225 * This protects the Smack attribute explicitly.
1227 * Returns 0 if access is permitted, an error code otherwise
1229 static int smack_inode_setxattr(struct user_namespace *mnt_userns,
1230 struct dentry *dentry, const char *name,
1231 const void *value, size_t size, int flags)
1233 struct smk_audit_info ad;
1234 struct smack_known *skp;
1236 int check_import = 0;
1241 * Check label validity here so import won't fail in post_setxattr
1243 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1244 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1245 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1248 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1249 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1253 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1255 if (size != TRANS_TRUE_SIZE ||
1256 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1259 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1261 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1264 if (rc == 0 && check_import) {
1265 skp = size ? smk_import_entry(value, size) : NULL;
1268 else if (skp == NULL || (check_star &&
1269 (skp == &smack_known_star || skp == &smack_known_web)))
1273 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1274 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1277 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1278 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1285 * smack_inode_post_setxattr - Apply the Smack update approved above
1287 * @name: attribute name
1288 * @value: attribute value
1289 * @size: attribute size
1292 * Set the pointer in the inode blob to the entry found
1293 * in the master label list.
1295 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1296 const void *value, size_t size, int flags)
1298 struct smack_known *skp;
1299 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1301 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1302 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1306 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1307 skp = smk_import_entry(value, size);
1309 isp->smk_inode = skp;
1310 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1311 skp = smk_import_entry(value, size);
1313 isp->smk_task = skp;
1314 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1315 skp = smk_import_entry(value, size);
1317 isp->smk_mmap = skp;
1324 * smack_inode_getxattr - Smack check on getxattr
1325 * @dentry: the object
1328 * Returns 0 if access is permitted, an error code otherwise
1330 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1332 struct smk_audit_info ad;
1335 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1336 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1338 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1339 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1344 * smack_inode_removexattr - Smack check on removexattr
1345 * @mnt_userns: active user namespace
1346 * @dentry: the object
1347 * @name: name of the attribute
1349 * Removing the Smack attribute requires CAP_MAC_ADMIN
1351 * Returns 0 if access is permitted, an error code otherwise
1353 static int smack_inode_removexattr(struct user_namespace *mnt_userns,
1354 struct dentry *dentry, const char *name)
1356 struct inode_smack *isp;
1357 struct smk_audit_info ad;
1360 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1361 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1362 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1363 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1364 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1365 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1366 if (!smack_privileged(CAP_MAC_ADMIN))
1369 rc = cap_inode_removexattr(mnt_userns, dentry, name);
1374 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1375 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1377 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1378 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1382 isp = smack_inode(d_backing_inode(dentry));
1384 * Don't do anything special for these.
1385 * XATTR_NAME_SMACKIPIN
1386 * XATTR_NAME_SMACKIPOUT
1388 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1389 struct super_block *sbp = dentry->d_sb;
1390 struct superblock_smack *sbsp = smack_superblock(sbp);
1392 isp->smk_inode = sbsp->smk_default;
1393 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1394 isp->smk_task = NULL;
1395 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1396 isp->smk_mmap = NULL;
1397 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1398 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1404 * smack_inode_getsecurity - get smack xattrs
1405 * @mnt_userns: active user namespace
1406 * @inode: the object
1407 * @name: attribute name
1408 * @buffer: where to put the result
1409 * @alloc: duplicate memory
1411 * Returns the size of the attribute or an error code
1413 static int smack_inode_getsecurity(struct user_namespace *mnt_userns,
1414 struct inode *inode, const char *name,
1415 void **buffer, bool alloc)
1417 struct socket_smack *ssp;
1418 struct socket *sock;
1419 struct super_block *sbp;
1420 struct inode *ip = (struct inode *)inode;
1421 struct smack_known *isp;
1423 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1424 isp = smk_of_inode(inode);
1427 * The rest of the Smack xattrs are only on sockets.
1430 if (sbp->s_magic != SOCKFS_MAGIC)
1433 sock = SOCKET_I(ip);
1434 if (sock == NULL || sock->sk == NULL)
1437 ssp = sock->sk->sk_security;
1439 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1441 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1448 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1449 if (*buffer == NULL)
1453 return strlen(isp->smk_known);
1458 * smack_inode_listsecurity - list the Smack attributes
1459 * @inode: the object
1460 * @buffer: where they go
1461 * @buffer_size: size of buffer
1463 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1466 int len = sizeof(XATTR_NAME_SMACK);
1468 if (buffer != NULL && len <= buffer_size)
1469 memcpy(buffer, XATTR_NAME_SMACK, len);
1475 * smack_inode_getsecid - Extract inode's security id
1476 * @inode: inode to extract the info from
1477 * @secid: where result will be saved
1479 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1481 struct smack_known *skp = smk_of_inode(inode);
1483 *secid = skp->smk_secid;
1491 * There is no smack_file_permission hook
1493 * Should access checks be done on each read or write?
1494 * UNICOS and SELinux say yes.
1495 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1497 * I'll say no for now. Smack does not do the frequent
1498 * label changing that SELinux does.
1502 * smack_file_alloc_security - assign a file security blob
1505 * The security blob for a file is a pointer to the master
1506 * label list, so no allocation is done.
1508 * f_security is the owner security information. It
1509 * isn't used on file access checks, it's for send_sigio.
1513 static int smack_file_alloc_security(struct file *file)
1515 struct smack_known **blob = smack_file(file);
1517 *blob = smk_of_current();
1522 * smack_file_ioctl - Smack check on ioctls
1527 * Relies heavily on the correct use of the ioctl command conventions.
1529 * Returns 0 if allowed, error code otherwise
1531 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1535 struct smk_audit_info ad;
1536 struct inode *inode = file_inode(file);
1538 if (unlikely(IS_PRIVATE(inode)))
1541 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1542 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1544 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1545 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1546 rc = smk_bu_file(file, MAY_WRITE, rc);
1549 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1550 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1551 rc = smk_bu_file(file, MAY_READ, rc);
1558 * smack_file_lock - Smack check on file locking
1562 * Returns 0 if current has lock access, error code otherwise
1564 static int smack_file_lock(struct file *file, unsigned int cmd)
1566 struct smk_audit_info ad;
1568 struct inode *inode = file_inode(file);
1570 if (unlikely(IS_PRIVATE(inode)))
1573 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1574 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1575 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1576 rc = smk_bu_file(file, MAY_LOCK, rc);
1581 * smack_file_fcntl - Smack check on fcntl
1583 * @cmd: what action to check
1586 * Generally these operations are harmless.
1587 * File locking operations present an obvious mechanism
1588 * for passing information, so they require write access.
1590 * Returns 0 if current has access, error code otherwise
1592 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1595 struct smk_audit_info ad;
1597 struct inode *inode = file_inode(file);
1599 if (unlikely(IS_PRIVATE(inode)))
1607 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1608 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1609 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1610 rc = smk_bu_file(file, MAY_LOCK, rc);
1614 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1615 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1616 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1617 rc = smk_bu_file(file, MAY_WRITE, rc);
1627 * smack_mmap_file - Check permissions for a mmap operation.
1628 * @file: contains the file structure for file to map (may be NULL).
1629 * @reqprot: contains the protection requested by the application.
1630 * @prot: contains the protection that will be applied by the kernel.
1631 * @flags: contains the operational flags.
1633 * The @file may be NULL, e.g. if mapping anonymous memory.
1635 * Return 0 if permission is granted.
1637 static int smack_mmap_file(struct file *file,
1638 unsigned long reqprot, unsigned long prot,
1639 unsigned long flags)
1641 struct smack_known *skp;
1642 struct smack_known *mkp;
1643 struct smack_rule *srp;
1644 struct task_smack *tsp;
1645 struct smack_known *okp;
1646 struct inode_smack *isp;
1647 struct superblock_smack *sbsp;
1656 if (unlikely(IS_PRIVATE(file_inode(file))))
1659 isp = smack_inode(file_inode(file));
1660 if (isp->smk_mmap == NULL)
1662 sbsp = smack_superblock(file_inode(file)->i_sb);
1663 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1664 isp->smk_mmap != sbsp->smk_root)
1666 mkp = isp->smk_mmap;
1668 tsp = smack_cred(current_cred());
1669 skp = smk_of_current();
1674 * For each Smack rule associated with the subject
1675 * label verify that the SMACK64MMAP also has access
1676 * to that rule's object label.
1678 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1679 okp = srp->smk_object;
1681 * Matching labels always allows access.
1683 if (mkp->smk_known == okp->smk_known)
1686 * If there is a matching local rule take
1687 * that into account as well.
1689 may = smk_access_entry(srp->smk_subject->smk_known,
1693 may = srp->smk_access;
1695 may &= srp->smk_access;
1697 * If may is zero the SMACK64MMAP subject can't
1698 * possibly have less access.
1704 * Fetch the global list entry.
1705 * If there isn't one a SMACK64MMAP subject
1706 * can't have as much access as current.
1708 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1710 if (mmay == -ENOENT) {
1715 * If there is a local entry it modifies the
1716 * potential access, too.
1718 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1720 if (tmay != -ENOENT)
1724 * If there is any access available to current that is
1725 * not available to a SMACK64MMAP subject
1728 if ((may | mmay) != mmay) {
1740 * smack_file_set_fowner - set the file security blob value
1741 * @file: object in question
1744 static void smack_file_set_fowner(struct file *file)
1746 struct smack_known **blob = smack_file(file);
1748 *blob = smk_of_current();
1752 * smack_file_send_sigiotask - Smack on sigio
1753 * @tsk: The target task
1754 * @fown: the object the signal come from
1757 * Allow a privileged task to get signals even if it shouldn't
1759 * Returns 0 if a subject with the object's smack could
1760 * write to the task, an error code otherwise.
1762 static int smack_file_send_sigiotask(struct task_struct *tsk,
1763 struct fown_struct *fown, int signum)
1765 struct smack_known **blob;
1766 struct smack_known *skp;
1767 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1768 const struct cred *tcred;
1771 struct smk_audit_info ad;
1774 * struct fown_struct is never outside the context of a struct file
1776 file = container_of(fown, struct file, f_owner);
1778 /* we don't log here as rc can be overriden */
1779 blob = smack_file(file);
1781 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1782 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1785 tcred = __task_cred(tsk);
1786 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1790 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1791 smk_ad_setfield_u_tsk(&ad, tsk);
1792 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1797 * smack_file_receive - Smack file receive check
1800 * Returns 0 if current has access, error code otherwise
1802 static int smack_file_receive(struct file *file)
1806 struct smk_audit_info ad;
1807 struct inode *inode = file_inode(file);
1808 struct socket *sock;
1809 struct task_smack *tsp;
1810 struct socket_smack *ssp;
1812 if (unlikely(IS_PRIVATE(inode)))
1815 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1816 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1818 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1819 sock = SOCKET_I(inode);
1820 ssp = sock->sk->sk_security;
1821 tsp = smack_cred(current_cred());
1823 * If the receiving process can't write to the
1824 * passed socket or if the passed socket can't
1825 * write to the receiving process don't accept
1826 * the passed socket.
1828 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1829 rc = smk_bu_file(file, may, rc);
1832 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1833 rc = smk_bu_file(file, may, rc);
1837 * This code relies on bitmasks.
1839 if (file->f_mode & FMODE_READ)
1841 if (file->f_mode & FMODE_WRITE)
1844 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1845 rc = smk_bu_file(file, may, rc);
1850 * smack_file_open - Smack dentry open processing
1853 * Set the security blob in the file structure.
1854 * Allow the open only if the task has read access. There are
1855 * many read operations (e.g. fstat) that you can do with an
1856 * fd even if you have the file open write-only.
1858 * Returns 0 if current has access, error code otherwise
1860 static int smack_file_open(struct file *file)
1862 struct task_smack *tsp = smack_cred(file->f_cred);
1863 struct inode *inode = file_inode(file);
1864 struct smk_audit_info ad;
1867 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1868 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1869 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1870 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1880 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1881 * @cred: the new credentials
1882 * @gfp: the atomicity of any memory allocations
1884 * Prepare a blank set of credentials for modification. This must allocate all
1885 * the memory the LSM module might require such that cred_transfer() can
1886 * complete without error.
1888 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1890 init_task_smack(smack_cred(cred), NULL, NULL);
1896 * smack_cred_free - "free" task-level security credentials
1897 * @cred: the credentials in question
1900 static void smack_cred_free(struct cred *cred)
1902 struct task_smack *tsp = smack_cred(cred);
1903 struct smack_rule *rp;
1904 struct list_head *l;
1905 struct list_head *n;
1907 smk_destroy_label_list(&tsp->smk_relabel);
1909 list_for_each_safe(l, n, &tsp->smk_rules) {
1910 rp = list_entry(l, struct smack_rule, list);
1911 list_del(&rp->list);
1912 kmem_cache_free(smack_rule_cache, rp);
1917 * smack_cred_prepare - prepare new set of credentials for modification
1918 * @new: the new credentials
1919 * @old: the original credentials
1920 * @gfp: the atomicity of any memory allocations
1922 * Prepare a new set of credentials for modification.
1924 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1927 struct task_smack *old_tsp = smack_cred(old);
1928 struct task_smack *new_tsp = smack_cred(new);
1931 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1933 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1937 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1943 * smack_cred_transfer - Transfer the old credentials to the new credentials
1944 * @new: the new credentials
1945 * @old: the original credentials
1947 * Fill in a set of blank credentials from another set of credentials.
1949 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1951 struct task_smack *old_tsp = smack_cred(old);
1952 struct task_smack *new_tsp = smack_cred(new);
1954 new_tsp->smk_task = old_tsp->smk_task;
1955 new_tsp->smk_forked = old_tsp->smk_task;
1956 mutex_init(&new_tsp->smk_rules_lock);
1957 INIT_LIST_HEAD(&new_tsp->smk_rules);
1959 /* cbs copy rule list */
1963 * smack_cred_getsecid - get the secid corresponding to a creds structure
1964 * @cred: the object creds
1965 * @secid: where to put the result
1967 * Sets the secid to contain a u32 version of the smack label.
1969 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1971 struct smack_known *skp;
1974 skp = smk_of_task(smack_cred(cred));
1975 *secid = skp->smk_secid;
1980 * smack_kernel_act_as - Set the subjective context in a set of credentials
1981 * @new: points to the set of credentials to be modified.
1982 * @secid: specifies the security ID to be set
1984 * Set the security data for a kernel service.
1986 static int smack_kernel_act_as(struct cred *new, u32 secid)
1988 struct task_smack *new_tsp = smack_cred(new);
1990 new_tsp->smk_task = smack_from_secid(secid);
1995 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1996 * @new: points to the set of credentials to be modified
1997 * @inode: points to the inode to use as a reference
1999 * Set the file creation context in a set of credentials to the same
2000 * as the objective context of the specified inode
2002 static int smack_kernel_create_files_as(struct cred *new,
2003 struct inode *inode)
2005 struct inode_smack *isp = smack_inode(inode);
2006 struct task_smack *tsp = smack_cred(new);
2008 tsp->smk_forked = isp->smk_inode;
2009 tsp->smk_task = tsp->smk_forked;
2014 * smk_curacc_on_task - helper to log task related access
2015 * @p: the task object
2016 * @access: the access requested
2017 * @caller: name of the calling function for audit
2019 * Return 0 if access is permitted
2021 static int smk_curacc_on_task(struct task_struct *p, int access,
2024 struct smk_audit_info ad;
2025 struct smack_known *skp = smk_of_task_struct_obj(p);
2028 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2029 smk_ad_setfield_u_tsk(&ad, p);
2030 rc = smk_curacc(skp, access, &ad);
2031 rc = smk_bu_task(p, access, rc);
2036 * smack_task_setpgid - Smack check on setting pgid
2037 * @p: the task object
2040 * Return 0 if write access is permitted
2042 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2044 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2048 * smack_task_getpgid - Smack access check for getpgid
2049 * @p: the object task
2051 * Returns 0 if current can read the object task, error code otherwise
2053 static int smack_task_getpgid(struct task_struct *p)
2055 return smk_curacc_on_task(p, MAY_READ, __func__);
2059 * smack_task_getsid - Smack access check for getsid
2060 * @p: the object task
2062 * Returns 0 if current can read the object task, error code otherwise
2064 static int smack_task_getsid(struct task_struct *p)
2066 return smk_curacc_on_task(p, MAY_READ, __func__);
2070 * smack_current_getsecid_subj - get the subjective secid of the current task
2071 * @secid: where to put the result
2073 * Sets the secid to contain a u32 version of the task's subjective smack label.
2075 static void smack_current_getsecid_subj(u32 *secid)
2077 struct smack_known *skp = smk_of_current();
2079 *secid = skp->smk_secid;
2083 * smack_task_getsecid_obj - get the objective secid of the task
2085 * @secid: where to put the result
2087 * Sets the secid to contain a u32 version of the task's objective smack label.
2089 static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2091 struct smack_known *skp = smk_of_task_struct_obj(p);
2093 *secid = skp->smk_secid;
2097 * smack_task_setnice - Smack check on setting nice
2098 * @p: the task object
2101 * Return 0 if write access is permitted
2103 static int smack_task_setnice(struct task_struct *p, int nice)
2105 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2109 * smack_task_setioprio - Smack check on setting ioprio
2110 * @p: the task object
2113 * Return 0 if write access is permitted
2115 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2117 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2121 * smack_task_getioprio - Smack check on reading ioprio
2122 * @p: the task object
2124 * Return 0 if read access is permitted
2126 static int smack_task_getioprio(struct task_struct *p)
2128 return smk_curacc_on_task(p, MAY_READ, __func__);
2132 * smack_task_setscheduler - Smack check on setting scheduler
2133 * @p: the task object
2135 * Return 0 if read access is permitted
2137 static int smack_task_setscheduler(struct task_struct *p)
2139 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2143 * smack_task_getscheduler - Smack check on reading scheduler
2144 * @p: the task object
2146 * Return 0 if read access is permitted
2148 static int smack_task_getscheduler(struct task_struct *p)
2150 return smk_curacc_on_task(p, MAY_READ, __func__);
2154 * smack_task_movememory - Smack check on moving memory
2155 * @p: the task object
2157 * Return 0 if write access is permitted
2159 static int smack_task_movememory(struct task_struct *p)
2161 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2165 * smack_task_kill - Smack check on signal delivery
2166 * @p: the task object
2169 * @cred: identifies the cred to use in lieu of current's
2171 * Return 0 if write access is permitted
2174 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2175 int sig, const struct cred *cred)
2177 struct smk_audit_info ad;
2178 struct smack_known *skp;
2179 struct smack_known *tkp = smk_of_task_struct_obj(p);
2183 return 0; /* null signal; existence test */
2185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2186 smk_ad_setfield_u_tsk(&ad, p);
2188 * Sending a signal requires that the sender
2189 * can write the receiver.
2192 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2193 rc = smk_bu_task(p, MAY_DELIVER, rc);
2197 * If the cred isn't NULL we're dealing with some USB IO
2198 * specific behavior. This is not clean. For one thing
2199 * we can't take privilege into account.
2201 skp = smk_of_task(smack_cred(cred));
2202 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2203 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2208 * smack_task_to_inode - copy task smack into the inode blob
2209 * @p: task to copy from
2210 * @inode: inode to copy to
2212 * Sets the smack pointer in the inode security blob
2214 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2216 struct inode_smack *isp = smack_inode(inode);
2217 struct smack_known *skp = smk_of_task_struct_obj(p);
2219 isp->smk_inode = skp;
2220 isp->smk_flags |= SMK_INODE_INSTANT;
2228 * smack_sk_alloc_security - Allocate a socket blob
2231 * @gfp_flags: memory allocation flags
2233 * Assign Smack pointers to current
2235 * Returns 0 on success, -ENOMEM is there's no memory
2237 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2239 struct smack_known *skp = smk_of_current();
2240 struct socket_smack *ssp;
2242 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2247 * Sockets created by kernel threads receive web label.
2249 if (unlikely(current->flags & PF_KTHREAD)) {
2250 ssp->smk_in = &smack_known_web;
2251 ssp->smk_out = &smack_known_web;
2256 ssp->smk_packet = NULL;
2258 sk->sk_security = ssp;
2264 * smack_sk_free_security - Free a socket blob
2267 * Clears the blob pointer
2269 static void smack_sk_free_security(struct sock *sk)
2271 #ifdef SMACK_IPV6_PORT_LABELING
2272 struct smk_port_label *spp;
2274 if (sk->sk_family == PF_INET6) {
2276 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2277 if (spp->smk_sock != sk)
2279 spp->smk_can_reuse = 1;
2285 kfree(sk->sk_security);
2289 * smack_ipv4host_label - check host based restrictions
2290 * @sip: the object end
2292 * looks for host based access restrictions
2294 * This version will only be appropriate for really small sets of single label
2295 * hosts. The caller is responsible for ensuring that the RCU read lock is
2296 * taken before calling this function.
2298 * Returns the label of the far end or NULL if it's not special.
2300 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2302 struct smk_net4addr *snp;
2303 struct in_addr *siap = &sip->sin_addr;
2305 if (siap->s_addr == 0)
2308 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2310 * we break after finding the first match because
2311 * the list is sorted from longest to shortest mask
2312 * so we have found the most specific match
2314 if (snp->smk_host.s_addr ==
2315 (siap->s_addr & snp->smk_mask.s_addr))
2316 return snp->smk_label;
2322 * smk_ipv6_localhost - Check for local ipv6 host address
2325 * Returns boolean true if this is the localhost address
2327 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2329 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2330 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2332 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2333 ntohs(be16p[7]) == 1)
2339 * smack_ipv6host_label - check host based restrictions
2340 * @sip: the object end
2342 * looks for host based access restrictions
2344 * This version will only be appropriate for really small sets of single label
2345 * hosts. The caller is responsible for ensuring that the RCU read lock is
2346 * taken before calling this function.
2348 * Returns the label of the far end or NULL if it's not special.
2350 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2352 struct smk_net6addr *snp;
2353 struct in6_addr *sap = &sip->sin6_addr;
2358 * It's local. Don't look for a host label.
2360 if (smk_ipv6_localhost(sip))
2363 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2365 * If the label is NULL the entry has
2366 * been renounced. Ignore it.
2368 if (snp->smk_label == NULL)
2371 * we break after finding the first match because
2372 * the list is sorted from longest to shortest mask
2373 * so we have found the most specific match
2375 for (found = 1, i = 0; i < 8; i++) {
2376 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2377 snp->smk_host.s6_addr16[i]) {
2383 return snp->smk_label;
2390 * smack_netlbl_add - Set the secattr on a socket
2393 * Attach the outbound smack value (smk_out) to the socket.
2395 * Returns 0 on success or an error code
2397 static int smack_netlbl_add(struct sock *sk)
2399 struct socket_smack *ssp = sk->sk_security;
2400 struct smack_known *skp = ssp->smk_out;
2404 bh_lock_sock_nested(sk);
2406 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2409 ssp->smk_state = SMK_NETLBL_LABELED;
2412 ssp->smk_state = SMK_NETLBL_REQSKB;
2424 * smack_netlbl_delete - Remove the secattr from a socket
2427 * Remove the outbound smack value from a socket
2429 static void smack_netlbl_delete(struct sock *sk)
2431 struct socket_smack *ssp = sk->sk_security;
2434 * Take the label off the socket if one is set.
2436 if (ssp->smk_state != SMK_NETLBL_LABELED)
2440 bh_lock_sock_nested(sk);
2441 netlbl_sock_delattr(sk);
2444 ssp->smk_state = SMK_NETLBL_UNLABELED;
2448 * smk_ipv4_check - Perform IPv4 host access checks
2450 * @sap: the destination address
2452 * Set the correct secattr for the given socket based on the destination
2453 * address and perform any outbound access checks needed.
2455 * Returns 0 on success or an error code.
2458 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2460 struct smack_known *skp;
2462 struct smack_known *hkp;
2463 struct socket_smack *ssp = sk->sk_security;
2464 struct smk_audit_info ad;
2467 hkp = smack_ipv4host_label(sap);
2470 struct lsm_network_audit net;
2472 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2473 ad.a.u.net->family = sap->sin_family;
2474 ad.a.u.net->dport = sap->sin_port;
2475 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2478 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2479 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2481 * Clear the socket netlabel if it's set.
2484 smack_netlbl_delete(sk);
2492 * smk_ipv6_check - check Smack access
2493 * @subject: subject Smack label
2494 * @object: object Smack label
2496 * @act: the action being taken
2498 * Check an IPv6 access
2500 static int smk_ipv6_check(struct smack_known *subject,
2501 struct smack_known *object,
2502 struct sockaddr_in6 *address, int act)
2505 struct lsm_network_audit net;
2507 struct smk_audit_info ad;
2511 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2512 ad.a.u.net->family = PF_INET6;
2513 ad.a.u.net->dport = address->sin6_port;
2514 if (act == SMK_RECEIVING)
2515 ad.a.u.net->v6info.saddr = address->sin6_addr;
2517 ad.a.u.net->v6info.daddr = address->sin6_addr;
2519 rc = smk_access(subject, object, MAY_WRITE, &ad);
2520 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2524 #ifdef SMACK_IPV6_PORT_LABELING
2526 * smk_ipv6_port_label - Smack port access table management
2530 * Create or update the port list entry
2532 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2534 struct sock *sk = sock->sk;
2535 struct sockaddr_in6 *addr6;
2536 struct socket_smack *ssp = sock->sk->sk_security;
2537 struct smk_port_label *spp;
2538 unsigned short port = 0;
2540 if (address == NULL) {
2542 * This operation is changing the Smack information
2543 * on the bound socket. Take the changes to the port
2547 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2548 if (sk != spp->smk_sock)
2550 spp->smk_in = ssp->smk_in;
2551 spp->smk_out = ssp->smk_out;
2556 * A NULL address is only used for updating existing
2557 * bound entries. If there isn't one, it's OK.
2563 addr6 = (struct sockaddr_in6 *)address;
2564 port = ntohs(addr6->sin6_port);
2566 * This is a special case that is safely ignored.
2572 * Look for an existing port list entry.
2573 * This is an indication that a port is getting reused.
2576 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2577 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2579 if (spp->smk_can_reuse != 1) {
2583 spp->smk_port = port;
2585 spp->smk_in = ssp->smk_in;
2586 spp->smk_out = ssp->smk_out;
2587 spp->smk_can_reuse = 0;
2593 * A new port entry is required.
2595 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2599 spp->smk_port = port;
2601 spp->smk_in = ssp->smk_in;
2602 spp->smk_out = ssp->smk_out;
2603 spp->smk_sock_type = sock->type;
2604 spp->smk_can_reuse = 0;
2606 mutex_lock(&smack_ipv6_lock);
2607 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2608 mutex_unlock(&smack_ipv6_lock);
2613 * smk_ipv6_port_check - check Smack port access
2616 * @act: the action being taken
2618 * Create or update the port list entry
2620 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2623 struct smk_port_label *spp;
2624 struct socket_smack *ssp = sk->sk_security;
2625 struct smack_known *skp = NULL;
2626 unsigned short port;
2627 struct smack_known *object;
2629 if (act == SMK_RECEIVING) {
2630 skp = smack_ipv6host_label(address);
2631 object = ssp->smk_in;
2634 object = smack_ipv6host_label(address);
2638 * The other end is a single label host.
2640 if (skp != NULL && object != NULL)
2641 return smk_ipv6_check(skp, object, address, act);
2643 skp = smack_net_ambient;
2645 object = smack_net_ambient;
2648 * It's remote, so port lookup does no good.
2650 if (!smk_ipv6_localhost(address))
2651 return smk_ipv6_check(skp, object, address, act);
2654 * It's local so the send check has to have passed.
2656 if (act == SMK_RECEIVING)
2659 port = ntohs(address->sin6_port);
2661 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2662 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2664 object = spp->smk_in;
2665 if (act == SMK_CONNECTING)
2666 ssp->smk_packet = spp->smk_out;
2671 return smk_ipv6_check(skp, object, address, act);
2676 * smack_inode_setsecurity - set smack xattrs
2677 * @inode: the object
2678 * @name: attribute name
2679 * @value: attribute value
2680 * @size: size of the attribute
2683 * Sets the named attribute in the appropriate blob
2685 * Returns 0 on success, or an error code
2687 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2688 const void *value, size_t size, int flags)
2690 struct smack_known *skp;
2691 struct inode_smack *nsp = smack_inode(inode);
2692 struct socket_smack *ssp;
2693 struct socket *sock;
2696 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2699 skp = smk_import_entry(value, size);
2701 return PTR_ERR(skp);
2703 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2704 nsp->smk_inode = skp;
2705 nsp->smk_flags |= SMK_INODE_INSTANT;
2709 * The rest of the Smack xattrs are only on sockets.
2711 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2714 sock = SOCKET_I(inode);
2715 if (sock == NULL || sock->sk == NULL)
2718 ssp = sock->sk->sk_security;
2720 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2722 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2724 if (sock->sk->sk_family == PF_INET) {
2725 rc = smack_netlbl_add(sock->sk);
2728 "Smack: \"%s\" netlbl error %d.\n",
2734 #ifdef SMACK_IPV6_PORT_LABELING
2735 if (sock->sk->sk_family == PF_INET6)
2736 smk_ipv6_port_label(sock, NULL);
2743 * smack_socket_post_create - finish socket setup
2745 * @family: protocol family
2750 * Sets the netlabel information on the socket
2752 * Returns 0 on success, and error code otherwise
2754 static int smack_socket_post_create(struct socket *sock, int family,
2755 int type, int protocol, int kern)
2757 struct socket_smack *ssp;
2759 if (sock->sk == NULL)
2763 * Sockets created by kernel threads receive web label.
2765 if (unlikely(current->flags & PF_KTHREAD)) {
2766 ssp = sock->sk->sk_security;
2767 ssp->smk_in = &smack_known_web;
2768 ssp->smk_out = &smack_known_web;
2771 if (family != PF_INET)
2774 * Set the outbound netlbl.
2776 return smack_netlbl_add(sock->sk);
2780 * smack_socket_socketpair - create socket pair
2781 * @socka: one socket
2782 * @sockb: another socket
2784 * Cross reference the peer labels for SO_PEERSEC
2788 static int smack_socket_socketpair(struct socket *socka,
2789 struct socket *sockb)
2791 struct socket_smack *asp = socka->sk->sk_security;
2792 struct socket_smack *bsp = sockb->sk->sk_security;
2794 asp->smk_packet = bsp->smk_out;
2795 bsp->smk_packet = asp->smk_out;
2800 #ifdef SMACK_IPV6_PORT_LABELING
2802 * smack_socket_bind - record port binding information.
2804 * @address: the port address
2805 * @addrlen: size of the address
2807 * Records the label bound to a port.
2809 * Returns 0 on success, and error code otherwise
2811 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2814 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2815 if (addrlen < SIN6_LEN_RFC2133 ||
2816 address->sa_family != AF_INET6)
2818 smk_ipv6_port_label(sock, address);
2822 #endif /* SMACK_IPV6_PORT_LABELING */
2825 * smack_socket_connect - connect access check
2827 * @sap: the other end
2828 * @addrlen: size of sap
2830 * Verifies that a connection may be possible
2832 * Returns 0 on success, and error code otherwise
2834 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2839 if (sock->sk == NULL)
2841 if (sock->sk->sk_family != PF_INET &&
2842 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2844 if (addrlen < offsetofend(struct sockaddr, sa_family))
2846 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2847 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2848 struct smack_known *rsp = NULL;
2850 if (addrlen < SIN6_LEN_RFC2133)
2852 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2853 rsp = smack_ipv6host_label(sip);
2855 struct socket_smack *ssp = sock->sk->sk_security;
2857 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2860 #ifdef SMACK_IPV6_PORT_LABELING
2861 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2866 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2868 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
2873 * smack_flags_to_may - convert S_ to MAY_ values
2874 * @flags: the S_ value
2876 * Returns the equivalent MAY_ value
2878 static int smack_flags_to_may(int flags)
2882 if (flags & S_IRUGO)
2884 if (flags & S_IWUGO)
2886 if (flags & S_IXUGO)
2893 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2898 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2900 struct smack_known **blob = smack_msg_msg(msg);
2902 *blob = smk_of_current();
2907 * smack_of_ipc - the smack pointer for the ipc
2910 * Returns a pointer to the smack value
2912 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2914 struct smack_known **blob = smack_ipc(isp);
2920 * smack_ipc_alloc_security - Set the security blob for ipc
2925 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2927 struct smack_known **blob = smack_ipc(isp);
2929 *blob = smk_of_current();
2934 * smk_curacc_shm : check if current has access on shm
2936 * @access : access requested
2938 * Returns 0 if current has the requested access, error code otherwise
2940 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2942 struct smack_known *ssp = smack_of_ipc(isp);
2943 struct smk_audit_info ad;
2947 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2948 ad.a.u.ipc_id = isp->id;
2950 rc = smk_curacc(ssp, access, &ad);
2951 rc = smk_bu_current("shm", ssp, access, rc);
2956 * smack_shm_associate - Smack access check for shm
2958 * @shmflg: access requested
2960 * Returns 0 if current has the requested access, error code otherwise
2962 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2966 may = smack_flags_to_may(shmflg);
2967 return smk_curacc_shm(isp, may);
2971 * smack_shm_shmctl - Smack access check for shm
2973 * @cmd: what it wants to do
2975 * Returns 0 if current has the requested access, error code otherwise
2977 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2991 may = MAY_READWRITE;
2996 * System level information.
3002 return smk_curacc_shm(isp, may);
3006 * smack_shm_shmat - Smack access for shmat
3009 * @shmflg: access requested
3011 * Returns 0 if current has the requested access, error code otherwise
3013 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3018 may = smack_flags_to_may(shmflg);
3019 return smk_curacc_shm(isp, may);
3023 * smk_curacc_sem : check if current has access on sem
3025 * @access : access requested
3027 * Returns 0 if current has the requested access, error code otherwise
3029 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3031 struct smack_known *ssp = smack_of_ipc(isp);
3032 struct smk_audit_info ad;
3036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3037 ad.a.u.ipc_id = isp->id;
3039 rc = smk_curacc(ssp, access, &ad);
3040 rc = smk_bu_current("sem", ssp, access, rc);
3045 * smack_sem_associate - Smack access check for sem
3047 * @semflg: access requested
3049 * Returns 0 if current has the requested access, error code otherwise
3051 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3055 may = smack_flags_to_may(semflg);
3056 return smk_curacc_sem(isp, may);
3060 * smack_sem_semctl - Smack access check for sem
3062 * @cmd: what it wants to do
3064 * Returns 0 if current has the requested access, error code otherwise
3066 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3085 may = MAY_READWRITE;
3090 * System level information
3097 return smk_curacc_sem(isp, may);
3101 * smack_sem_semop - Smack checks of semaphore operations
3107 * Treated as read and write in all cases.
3109 * Returns 0 if access is allowed, error code otherwise
3111 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3112 unsigned nsops, int alter)
3114 return smk_curacc_sem(isp, MAY_READWRITE);
3118 * smk_curacc_msq : helper to check if current has access on msq
3120 * @access : access requested
3122 * return 0 if current has access, error otherwise
3124 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3126 struct smack_known *msp = smack_of_ipc(isp);
3127 struct smk_audit_info ad;
3131 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3132 ad.a.u.ipc_id = isp->id;
3134 rc = smk_curacc(msp, access, &ad);
3135 rc = smk_bu_current("msq", msp, access, rc);
3140 * smack_msg_queue_associate - Smack access check for msg_queue
3142 * @msqflg: access requested
3144 * Returns 0 if current has the requested access, error code otherwise
3146 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3150 may = smack_flags_to_may(msqflg);
3151 return smk_curacc_msq(isp, may);
3155 * smack_msg_queue_msgctl - Smack access check for msg_queue
3157 * @cmd: what it wants to do
3159 * Returns 0 if current has the requested access, error code otherwise
3161 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3173 may = MAY_READWRITE;
3178 * System level information
3185 return smk_curacc_msq(isp, may);
3189 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3192 * @msqflg: access requested
3194 * Returns 0 if current has the requested access, error code otherwise
3196 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3201 may = smack_flags_to_may(msqflg);
3202 return smk_curacc_msq(isp, may);
3206 * smack_msg_queue_msgrcv - Smack access check for msg_queue
3213 * Returns 0 if current has read and write access, error code otherwise
3215 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp,
3216 struct msg_msg *msg,
3217 struct task_struct *target, long type,
3220 return smk_curacc_msq(isp, MAY_READWRITE);
3224 * smack_ipc_permission - Smack access for ipc_permission()
3225 * @ipp: the object permissions
3226 * @flag: access requested
3228 * Returns 0 if current has read and write access, error code otherwise
3230 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3232 struct smack_known **blob = smack_ipc(ipp);
3233 struct smack_known *iskp = *blob;
3234 int may = smack_flags_to_may(flag);
3235 struct smk_audit_info ad;
3239 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3240 ad.a.u.ipc_id = ipp->id;
3242 rc = smk_curacc(iskp, may, &ad);
3243 rc = smk_bu_current("svipc", iskp, may, rc);
3248 * smack_ipc_getsecid - Extract smack security id
3249 * @ipp: the object permissions
3250 * @secid: where result will be saved
3252 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3254 struct smack_known **blob = smack_ipc(ipp);
3255 struct smack_known *iskp = *blob;
3257 *secid = iskp->smk_secid;
3261 * smack_d_instantiate - Make sure the blob is correct on an inode
3262 * @opt_dentry: dentry where inode will be attached
3263 * @inode: the object
3265 * Set the inode's security blob if it hasn't been done already.
3267 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3269 struct super_block *sbp;
3270 struct superblock_smack *sbsp;
3271 struct inode_smack *isp;
3272 struct smack_known *skp;
3273 struct smack_known *ckp = smk_of_current();
3274 struct smack_known *final;
3275 char trattr[TRANS_TRUE_SIZE];
3283 isp = smack_inode(inode);
3286 * If the inode is already instantiated
3287 * take the quick way out
3289 if (isp->smk_flags & SMK_INODE_INSTANT)
3293 sbsp = smack_superblock(sbp);
3295 * We're going to use the superblock default label
3296 * if there's no label on the file.
3298 final = sbsp->smk_default;
3301 * If this is the root inode the superblock
3302 * may be in the process of initialization.
3303 * If that is the case use the root value out
3304 * of the superblock.
3306 if (opt_dentry->d_parent == opt_dentry) {
3307 switch (sbp->s_magic) {
3308 case CGROUP_SUPER_MAGIC:
3309 case CGROUP2_SUPER_MAGIC:
3311 * The cgroup filesystem is never mounted,
3312 * so there's no opportunity to set the mount
3315 sbsp->smk_root = &smack_known_star;
3316 sbsp->smk_default = &smack_known_star;
3317 isp->smk_inode = sbsp->smk_root;
3321 * What about shmem/tmpfs anonymous files with dentry
3322 * obtained from d_alloc_pseudo()?
3324 isp->smk_inode = smk_of_current();
3327 isp->smk_inode = smk_of_current();
3331 * Socket access is controlled by the socket
3332 * structures associated with the task involved.
3334 isp->smk_inode = &smack_known_star;
3337 isp->smk_inode = sbsp->smk_root;
3340 isp->smk_flags |= SMK_INODE_INSTANT;
3345 * This is pretty hackish.
3346 * Casey says that we shouldn't have to do
3347 * file system specific code, but it does help
3348 * with keeping it simple.
3350 switch (sbp->s_magic) {
3352 case CGROUP_SUPER_MAGIC:
3353 case CGROUP2_SUPER_MAGIC:
3355 * Casey says that it's a little embarrassing
3356 * that the smack file system doesn't do
3357 * extended attributes.
3359 * Cgroupfs is special
3361 final = &smack_known_star;
3363 case DEVPTS_SUPER_MAGIC:
3365 * devpts seems content with the label of the task.
3366 * Programs that change smack have to treat the
3371 case PROC_SUPER_MAGIC:
3373 * Casey says procfs appears not to care.
3374 * The superblock default suffices.
3379 * Device labels should come from the filesystem,
3380 * but watch out, because they're volitile,
3381 * getting recreated on every reboot.
3383 final = &smack_known_star;
3385 * If a smack value has been set we want to use it,
3386 * but since tmpfs isn't giving us the opportunity
3387 * to set mount options simulate setting the
3388 * superblock default.
3393 * This isn't an understood special case.
3394 * Get the value from the xattr.
3398 * UNIX domain sockets use lower level socket data.
3400 if (S_ISSOCK(inode->i_mode)) {
3401 final = &smack_known_star;
3405 * No xattr support means, alas, no SMACK label.
3406 * Use the aforeapplied default.
3407 * It would be curious if the label of the task
3408 * does not match that assigned.
3410 if (!(inode->i_opflags & IOP_XATTR))
3413 * Get the dentry for xattr.
3415 dp = dget(opt_dentry);
3416 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3417 if (!IS_ERR_OR_NULL(skp))
3421 * Transmuting directory
3423 if (S_ISDIR(inode->i_mode)) {
3425 * If this is a new directory and the label was
3426 * transmuted when the inode was initialized
3427 * set the transmute attribute on the directory
3428 * and mark the inode.
3430 * If there is a transmute attribute on the
3431 * directory mark the inode.
3433 if (isp->smk_flags & SMK_INODE_CHANGED) {
3434 isp->smk_flags &= ~SMK_INODE_CHANGED;
3435 rc = __vfs_setxattr(&init_user_ns, dp, inode,
3436 XATTR_NAME_SMACKTRANSMUTE,
3437 TRANS_TRUE, TRANS_TRUE_SIZE,
3440 rc = __vfs_getxattr(dp, inode,
3441 XATTR_NAME_SMACKTRANSMUTE, trattr,
3443 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3444 TRANS_TRUE_SIZE) != 0)
3448 transflag = SMK_INODE_TRANSMUTE;
3451 * Don't let the exec or mmap label be "*" or "@".
3453 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3454 if (IS_ERR(skp) || skp == &smack_known_star ||
3455 skp == &smack_known_web)
3457 isp->smk_task = skp;
3459 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3460 if (IS_ERR(skp) || skp == &smack_known_star ||
3461 skp == &smack_known_web)
3463 isp->smk_mmap = skp;
3470 isp->smk_inode = ckp;
3472 isp->smk_inode = final;
3474 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3480 * smack_getprocattr - Smack process attribute access
3481 * @p: the object task
3482 * @name: the name of the attribute in /proc/.../attr
3483 * @value: where to put the result
3485 * Places a copy of the task Smack into value
3487 * Returns the length of the smack label or an error code
3489 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3491 struct smack_known *skp = smk_of_task_struct_obj(p);
3495 if (strcmp(name, "current") != 0)
3498 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3508 * smack_setprocattr - Smack process attribute setting
3509 * @name: the name of the attribute in /proc/.../attr
3510 * @value: the value to set
3511 * @size: the size of the value
3513 * Sets the Smack value of the task. Only setting self
3514 * is permitted and only with privilege
3516 * Returns the length of the smack label or an error code
3518 static int smack_setprocattr(const char *name, void *value, size_t size)
3520 struct task_smack *tsp = smack_cred(current_cred());
3522 struct smack_known *skp;
3523 struct smack_known_list_elem *sklep;
3526 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3529 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3532 if (strcmp(name, "current") != 0)
3535 skp = smk_import_entry(value, size);
3537 return PTR_ERR(skp);
3540 * No process is ever allowed the web ("@") label
3541 * and the star ("*") label.
3543 if (skp == &smack_known_web || skp == &smack_known_star)
3546 if (!smack_privileged(CAP_MAC_ADMIN)) {
3548 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3549 if (sklep->smk_label == skp) {
3557 new = prepare_creds();
3561 tsp = smack_cred(new);
3562 tsp->smk_task = skp;
3564 * process can change its label only once
3566 smk_destroy_label_list(&tsp->smk_relabel);
3573 * smack_unix_stream_connect - Smack access on UDS
3575 * @other: the other sock
3578 * Return 0 if a subject with the smack of sock could access
3579 * an object with the smack of other, otherwise an error code
3581 static int smack_unix_stream_connect(struct sock *sock,
3582 struct sock *other, struct sock *newsk)
3584 struct smack_known *skp;
3585 struct smack_known *okp;
3586 struct socket_smack *ssp = sock->sk_security;
3587 struct socket_smack *osp = other->sk_security;
3588 struct socket_smack *nsp = newsk->sk_security;
3589 struct smk_audit_info ad;
3592 struct lsm_network_audit net;
3595 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3599 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3600 smk_ad_setfield_u_net_sk(&ad, other);
3602 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3603 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3607 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3608 rc = smk_bu_note("UDS connect", okp, skp,
3614 * Cross reference the peer labels for SO_PEERSEC.
3617 nsp->smk_packet = ssp->smk_out;
3618 ssp->smk_packet = osp->smk_out;
3625 * smack_unix_may_send - Smack access on UDS
3627 * @other: the other socket
3629 * Return 0 if a subject with the smack of sock could access
3630 * an object with the smack of other, otherwise an error code
3632 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3634 struct socket_smack *ssp = sock->sk->sk_security;
3635 struct socket_smack *osp = other->sk->sk_security;
3636 struct smk_audit_info ad;
3640 struct lsm_network_audit net;
3642 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3643 smk_ad_setfield_u_net_sk(&ad, other->sk);
3646 if (smack_privileged(CAP_MAC_OVERRIDE))
3649 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3650 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3655 * smack_socket_sendmsg - Smack check based on destination host
3658 * @size: the size of the message
3660 * Return 0 if the current subject can write to the destination host.
3661 * For IPv4 this is only a question if the destination is a single label host.
3662 * For IPv6 this is a check against the label of the port.
3664 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3667 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3668 #if IS_ENABLED(CONFIG_IPV6)
3669 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3671 #ifdef SMACK_IPV6_SECMARK_LABELING
3672 struct socket_smack *ssp = sock->sk->sk_security;
3673 struct smack_known *rsp;
3678 * Perfectly reasonable for this to be NULL
3683 switch (sock->sk->sk_family) {
3685 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3686 sip->sin_family != AF_INET)
3688 rc = smk_ipv4_check(sock->sk, sip);
3690 #if IS_ENABLED(CONFIG_IPV6)
3692 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3693 sap->sin6_family != AF_INET6)
3695 #ifdef SMACK_IPV6_SECMARK_LABELING
3696 rsp = smack_ipv6host_label(sap);
3698 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3701 #ifdef SMACK_IPV6_PORT_LABELING
3702 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3704 #endif /* IS_ENABLED(CONFIG_IPV6) */
3711 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3712 * @sap: netlabel secattr
3713 * @ssp: socket security information
3715 * Returns a pointer to a Smack label entry found on the label list.
3717 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3718 struct socket_smack *ssp)
3720 struct smack_known *skp;
3726 * Netlabel found it in the cache.
3728 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3729 return (struct smack_known *)sap->cache->data;
3731 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3733 * Looks like a fallback, which gives us a secid.
3735 return smack_from_secid(sap->attr.secid);
3737 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3739 * Looks like a CIPSO packet.
3740 * If there are flags but no level netlabel isn't
3741 * behaving the way we expect it to.
3743 * Look it up in the label table
3744 * Without guidance regarding the smack value
3745 * for the packet fall back on the network
3749 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3750 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3753 * Compare the catsets. Use the netlbl APIs.
3755 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3756 if ((skp->smk_netlabel.flags &
3757 NETLBL_SECATTR_MLS_CAT) == 0)
3761 for (acat = -1, kcat = -1; acat == kcat; ) {
3762 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3764 kcat = netlbl_catmap_walk(
3765 skp->smk_netlabel.attr.mls.cat,
3767 if (acat < 0 || kcat < 0)
3780 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3781 return &smack_known_web;
3782 return &smack_known_star;
3785 * Without guidance regarding the smack value
3786 * for the packet fall back on the network
3789 return smack_net_ambient;
3792 #if IS_ENABLED(CONFIG_IPV6)
3793 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3797 int proto = -EINVAL;
3798 struct ipv6hdr _ipv6h;
3799 struct ipv6hdr *ip6;
3801 struct tcphdr _tcph, *th;
3802 struct udphdr _udph, *uh;
3803 struct dccp_hdr _dccph, *dh;
3807 offset = skb_network_offset(skb);
3808 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3811 sip->sin6_addr = ip6->saddr;
3813 nexthdr = ip6->nexthdr;
3814 offset += sizeof(_ipv6h);
3815 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3822 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3824 sip->sin6_port = th->source;
3827 case IPPROTO_UDPLITE:
3828 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3830 sip->sin6_port = uh->source;
3833 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3835 sip->sin6_port = dh->dccph_sport;
3840 #endif /* CONFIG_IPV6 */
3843 * smack_from_skb - Smack data from the secmark in an skb
3846 * Returns smack_known of the secmark or NULL if that won't work.
3848 #ifdef CONFIG_NETWORK_SECMARK
3849 static struct smack_known *smack_from_skb(struct sk_buff *skb)
3851 if (skb == NULL || skb->secmark == 0)
3854 return smack_from_secid(skb->secmark);
3857 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3864 * smack_from_netlbl - Smack data from the IP options in an skb
3865 * @sk: socket data came in on
3866 * @family: address family
3869 * Find the Smack label in the IP options. If it hasn't been
3870 * added to the netlabel cache, add it here.
3872 * Returns smack_known of the IP options or NULL if that won't work.
3874 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
3875 struct sk_buff *skb)
3877 struct netlbl_lsm_secattr secattr;
3878 struct socket_smack *ssp = NULL;
3879 struct smack_known *skp = NULL;
3881 netlbl_secattr_init(&secattr);
3884 ssp = sk->sk_security;
3886 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
3887 skp = smack_from_secattr(&secattr, ssp);
3888 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
3889 netlbl_cache_add(skb, family, &skp->smk_netlabel);
3892 netlbl_secattr_destroy(&secattr);
3898 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3902 * Returns 0 if the packet should be delivered, an error code otherwise
3904 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3906 struct socket_smack *ssp = sk->sk_security;
3907 struct smack_known *skp = NULL;
3909 struct smk_audit_info ad;
3910 u16 family = sk->sk_family;
3912 struct lsm_network_audit net;
3914 #if IS_ENABLED(CONFIG_IPV6)
3915 struct sockaddr_in6 sadd;
3918 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3920 #endif /* CONFIG_IPV6 */
3925 * If there is a secmark use it rather than the CIPSO label.
3926 * If there is no secmark fall back to CIPSO.
3927 * The secmark is assumed to reflect policy better.
3929 skp = smack_from_skb(skb);
3931 skp = smack_from_netlbl(sk, family, skb);
3933 skp = smack_net_ambient;
3937 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3938 ad.a.u.net->family = family;
3939 ad.a.u.net->netif = skb->skb_iif;
3940 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3943 * Receiving a packet requires that the other end
3944 * be able to write here. Read access is not required.
3945 * This is the simplist possible security model
3948 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3949 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3952 netlbl_skbuff_err(skb, family, rc, 0);
3954 #if IS_ENABLED(CONFIG_IPV6)
3956 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3957 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3958 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3960 #ifdef SMACK_IPV6_SECMARK_LABELING
3961 skp = smack_from_skb(skb);
3963 if (smk_ipv6_localhost(&sadd))
3965 skp = smack_ipv6host_label(&sadd);
3967 skp = smack_net_ambient;
3970 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3971 ad.a.u.net->family = family;
3972 ad.a.u.net->netif = skb->skb_iif;
3973 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3974 #endif /* CONFIG_AUDIT */
3975 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3976 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3978 #endif /* SMACK_IPV6_SECMARK_LABELING */
3979 #ifdef SMACK_IPV6_PORT_LABELING
3980 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3981 #endif /* SMACK_IPV6_PORT_LABELING */
3983 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3984 ICMPV6_ADM_PROHIBITED, 0);
3986 #endif /* CONFIG_IPV6 */
3993 * smack_socket_getpeersec_stream - pull in packet label
3995 * @optval: user's destination
3996 * @optlen: size thereof
3999 * returns zero on success, an error code otherwise
4001 static int smack_socket_getpeersec_stream(struct socket *sock,
4002 char __user *optval,
4003 int __user *optlen, unsigned len)
4005 struct socket_smack *ssp;
4010 ssp = sock->sk->sk_security;
4011 if (ssp->smk_packet != NULL) {
4012 rcp = ssp->smk_packet->smk_known;
4013 slen = strlen(rcp) + 1;
4018 else if (copy_to_user(optval, rcp, slen) != 0)
4021 if (put_user(slen, optlen) != 0)
4029 * smack_socket_getpeersec_dgram - pull in packet label
4030 * @sock: the peer socket
4032 * @secid: pointer to where to put the secid of the packet
4034 * Sets the netlabel socket state on sk from parent
4036 static int smack_socket_getpeersec_dgram(struct socket *sock,
4037 struct sk_buff *skb, u32 *secid)
4040 struct socket_smack *ssp = NULL;
4041 struct smack_known *skp;
4042 struct sock *sk = NULL;
4043 int family = PF_UNSPEC;
4044 u32 s = 0; /* 0 is the invalid secid */
4047 if (skb->protocol == htons(ETH_P_IP))
4049 #if IS_ENABLED(CONFIG_IPV6)
4050 else if (skb->protocol == htons(ETH_P_IPV6))
4052 #endif /* CONFIG_IPV6 */
4054 if (family == PF_UNSPEC && sock != NULL)
4055 family = sock->sk->sk_family;
4059 ssp = sock->sk->sk_security;
4060 s = ssp->smk_out->smk_secid;
4063 skp = smack_from_skb(skb);
4069 * Translate what netlabel gave us.
4073 skp = smack_from_netlbl(sk, family, skb);
4078 #ifdef SMACK_IPV6_SECMARK_LABELING
4079 skp = smack_from_skb(skb);
4092 * smack_sock_graft - Initialize a newly created socket with an existing sock
4094 * @parent: parent socket
4096 * Set the smk_{in,out} state of an existing sock based on the process that
4097 * is creating the new socket.
4099 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4101 struct socket_smack *ssp;
4102 struct smack_known *skp = smk_of_current();
4105 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4108 ssp = sk->sk_security;
4111 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4115 * smack_inet_conn_request - Smack access check on connect
4116 * @sk: socket involved
4120 * Returns 0 if a task with the packet label could write to
4121 * the socket, otherwise an error code
4123 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4124 struct request_sock *req)
4126 u16 family = sk->sk_family;
4127 struct smack_known *skp;
4128 struct socket_smack *ssp = sk->sk_security;
4129 struct sockaddr_in addr;
4131 struct smack_known *hskp;
4133 struct smk_audit_info ad;
4135 struct lsm_network_audit net;
4138 #if IS_ENABLED(CONFIG_IPV6)
4139 if (family == PF_INET6) {
4141 * Handle mapped IPv4 packets arriving
4142 * via IPv6 sockets. Don't set up netlabel
4143 * processing on IPv6.
4145 if (skb->protocol == htons(ETH_P_IP))
4150 #endif /* CONFIG_IPV6 */
4153 * If there is a secmark use it rather than the CIPSO label.
4154 * If there is no secmark fall back to CIPSO.
4155 * The secmark is assumed to reflect policy better.
4157 skp = smack_from_skb(skb);
4159 skp = smack_from_netlbl(sk, family, skb);
4161 skp = &smack_known_huh;
4165 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4166 ad.a.u.net->family = family;
4167 ad.a.u.net->netif = skb->skb_iif;
4168 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4171 * Receiving a packet requires that the other end be able to write
4172 * here. Read access is not required.
4174 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4175 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4180 * Save the peer's label in the request_sock so we can later setup
4181 * smk_packet in the child socket so that SO_PEERCRED can report it.
4183 req->peer_secid = skp->smk_secid;
4186 * We need to decide if we want to label the incoming connection here
4187 * if we do we only need to label the request_sock and the stack will
4188 * propagate the wire-label to the sock when it is created.
4191 addr.sin_addr.s_addr = hdr->saddr;
4193 hskp = smack_ipv4host_label(&addr);
4197 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4199 netlbl_req_delattr(req);
4205 * smack_inet_csk_clone - Copy the connection information to the new socket
4206 * @sk: the new socket
4207 * @req: the connection's request_sock
4209 * Transfer the connection's peer label to the newly created socket.
4211 static void smack_inet_csk_clone(struct sock *sk,
4212 const struct request_sock *req)
4214 struct socket_smack *ssp = sk->sk_security;
4215 struct smack_known *skp;
4217 if (req->peer_secid != 0) {
4218 skp = smack_from_secid(req->peer_secid);
4219 ssp->smk_packet = skp;
4221 ssp->smk_packet = NULL;
4225 * Key management security hooks
4227 * Casey has not tested key support very heavily.
4228 * The permission check is most likely too restrictive.
4229 * If you care about keys please have a look.
4234 * smack_key_alloc - Set the key security blob
4236 * @cred: the credentials to use
4239 * No allocation required
4243 static int smack_key_alloc(struct key *key, const struct cred *cred,
4244 unsigned long flags)
4246 struct smack_known *skp = smk_of_task(smack_cred(cred));
4248 key->security = skp;
4253 * smack_key_free - Clear the key security blob
4256 * Clear the blob pointer
4258 static void smack_key_free(struct key *key)
4260 key->security = NULL;
4264 * smack_key_permission - Smack access on a key
4265 * @key_ref: gets to the object
4266 * @cred: the credentials to use
4267 * @need_perm: requested key permission
4269 * Return 0 if the task has read and write to the object,
4270 * an error code otherwise
4272 static int smack_key_permission(key_ref_t key_ref,
4273 const struct cred *cred,
4274 enum key_need_perm need_perm)
4277 struct smk_audit_info ad;
4278 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4283 * Validate requested permissions
4285 switch (need_perm) {
4287 case KEY_NEED_SEARCH:
4289 request |= MAY_READ;
4291 case KEY_NEED_WRITE:
4293 case KEY_NEED_SETATTR:
4294 request |= MAY_WRITE;
4296 case KEY_NEED_UNSPECIFIED:
4297 case KEY_NEED_UNLINK:
4298 case KEY_SYSADMIN_OVERRIDE:
4299 case KEY_AUTHTOKEN_OVERRIDE:
4300 case KEY_DEFER_PERM_CHECK:
4306 keyp = key_ref_to_ptr(key_ref);
4310 * If the key hasn't been initialized give it access so that
4313 if (keyp->security == NULL)
4316 * This should not occur
4321 if (smack_privileged(CAP_MAC_OVERRIDE))
4325 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4326 ad.a.u.key_struct.key = keyp->serial;
4327 ad.a.u.key_struct.key_desc = keyp->description;
4329 rc = smk_access(tkp, keyp->security, request, &ad);
4330 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4335 * smack_key_getsecurity - Smack label tagging the key
4336 * @key points to the key to be queried
4337 * @_buffer points to a pointer that should be set to point to the
4338 * resulting string (if no label or an error occurs).
4339 * Return the length of the string (including terminating NUL) or -ve if
4341 * May also return 0 (and a NULL buffer pointer) if there is no label.
4343 static int smack_key_getsecurity(struct key *key, char **_buffer)
4345 struct smack_known *skp = key->security;
4349 if (key->security == NULL) {
4354 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4357 length = strlen(copy) + 1;
4364 #ifdef CONFIG_KEY_NOTIFICATIONS
4366 * smack_watch_key - Smack access to watch a key for notifications.
4367 * @key: The key to be watched
4369 * Return 0 if the @watch->cred has permission to read from the key object and
4370 * an error otherwise.
4372 static int smack_watch_key(struct key *key)
4374 struct smk_audit_info ad;
4375 struct smack_known *tkp = smk_of_current();
4381 * If the key hasn't been initialized give it access so that
4384 if (key->security == NULL)
4387 * This should not occur
4392 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4396 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4397 ad.a.u.key_struct.key = key->serial;
4398 ad.a.u.key_struct.key_desc = key->description;
4400 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4401 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4404 #endif /* CONFIG_KEY_NOTIFICATIONS */
4405 #endif /* CONFIG_KEYS */
4407 #ifdef CONFIG_WATCH_QUEUE
4409 * smack_post_notification - Smack access to post a notification to a queue
4410 * @w_cred: The credentials of the watcher.
4411 * @cred: The credentials of the event source (may be NULL).
4412 * @n: The notification message to be posted.
4414 static int smack_post_notification(const struct cred *w_cred,
4415 const struct cred *cred,
4416 struct watch_notification *n)
4418 struct smk_audit_info ad;
4419 struct smack_known *subj, *obj;
4422 /* Always let maintenance notifications through. */
4423 if (n->type == WATCH_TYPE_META)
4428 subj = smk_of_task(smack_cred(cred));
4429 obj = smk_of_task(smack_cred(w_cred));
4431 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4432 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4433 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4436 #endif /* CONFIG_WATCH_QUEUE */
4441 * Audit requires a unique representation of each Smack specific
4442 * rule. This unique representation is used to distinguish the
4443 * object to be audited from remaining kernel objects and also
4444 * works as a glue between the audit hooks.
4446 * Since repository entries are added but never deleted, we'll use
4447 * the smack_known label address related to the given audit rule as
4448 * the needed unique representation. This also better fits the smack
4449 * model where nearly everything is a label.
4454 * smack_audit_rule_init - Initialize a smack audit rule
4455 * @field: audit rule fields given from user-space (audit.h)
4456 * @op: required testing operator (=, !=, >, <, ...)
4457 * @rulestr: smack label to be audited
4458 * @vrule: pointer to save our own audit rule representation
4460 * Prepare to audit cases where (@field @op @rulestr) is true.
4461 * The label to be audited is created if necessay.
4463 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4465 struct smack_known *skp;
4466 char **rule = (char **)vrule;
4469 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4472 if (op != Audit_equal && op != Audit_not_equal)
4475 skp = smk_import_entry(rulestr, 0);
4477 return PTR_ERR(skp);
4479 *rule = skp->smk_known;
4485 * smack_audit_rule_known - Distinguish Smack audit rules
4486 * @krule: rule of interest, in Audit kernel representation format
4488 * This is used to filter Smack rules from remaining Audit ones.
4489 * If it's proved that this rule belongs to us, the
4490 * audit_rule_match hook will be called to do the final judgement.
4492 static int smack_audit_rule_known(struct audit_krule *krule)
4494 struct audit_field *f;
4497 for (i = 0; i < krule->field_count; i++) {
4498 f = &krule->fields[i];
4500 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4508 * smack_audit_rule_match - Audit given object ?
4509 * @secid: security id for identifying the object to test
4510 * @field: audit rule flags given from user-space
4511 * @op: required testing operator
4512 * @vrule: smack internal rule presentation
4514 * The core Audit hook. It's used to take the decision of
4515 * whether to audit or not to audit a given object.
4517 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4519 struct smack_known *skp;
4522 if (unlikely(!rule)) {
4523 WARN_ONCE(1, "Smack: missing rule\n");
4527 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4530 skp = smack_from_secid(secid);
4533 * No need to do string comparisons. If a match occurs,
4534 * both pointers will point to the same smack_known
4537 if (op == Audit_equal)
4538 return (rule == skp->smk_known);
4539 if (op == Audit_not_equal)
4540 return (rule != skp->smk_known);
4546 * There is no need for a smack_audit_rule_free hook.
4547 * No memory was allocated.
4550 #endif /* CONFIG_AUDIT */
4553 * smack_ismaclabel - check if xattr @name references a smack MAC label
4554 * @name: Full xattr name to check.
4556 static int smack_ismaclabel(const char *name)
4558 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4563 * smack_secid_to_secctx - return the smack label for a secid
4564 * @secid: incoming integer
4565 * @secdata: destination
4566 * @seclen: how long it is
4568 * Exists for networking code.
4570 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4572 struct smack_known *skp = smack_from_secid(secid);
4575 *secdata = skp->smk_known;
4576 *seclen = strlen(skp->smk_known);
4581 * smack_secctx_to_secid - return the secid for a smack label
4582 * @secdata: smack label
4583 * @seclen: how long result is
4584 * @secid: outgoing integer
4586 * Exists for audit and networking code.
4588 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4590 struct smack_known *skp = smk_find_entry(secdata);
4593 *secid = skp->smk_secid;
4600 * There used to be a smack_release_secctx hook
4601 * that did nothing back when hooks were in a vector.
4602 * Now that there's a list such a hook adds cost.
4605 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4607 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4611 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4613 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK,
4617 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4619 struct smack_known *skp = smk_of_inode(inode);
4621 *ctx = skp->smk_known;
4622 *ctxlen = strlen(skp->smk_known);
4626 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4629 struct task_smack *tsp;
4630 struct smack_known *skp;
4631 struct inode_smack *isp;
4632 struct cred *new_creds = *new;
4634 if (new_creds == NULL) {
4635 new_creds = prepare_creds();
4636 if (new_creds == NULL)
4640 tsp = smack_cred(new_creds);
4643 * Get label from overlay inode and set it in create_sid
4645 isp = smack_inode(d_inode(dentry));
4646 skp = isp->smk_inode;
4647 tsp->smk_task = skp;
4652 static int smack_inode_copy_up_xattr(const char *name)
4655 * Return 1 if this is the smack access Smack attribute.
4657 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4663 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4665 const struct cred *old,
4668 struct task_smack *otsp = smack_cred(old);
4669 struct task_smack *ntsp = smack_cred(new);
4670 struct inode_smack *isp;
4674 * Use the process credential unless all of
4675 * the transmuting criteria are met
4677 ntsp->smk_task = otsp->smk_task;
4680 * the attribute of the containing directory
4682 isp = smack_inode(d_inode(dentry->d_parent));
4684 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4686 may = smk_access_entry(otsp->smk_task->smk_known,
4687 isp->smk_inode->smk_known,
4688 &otsp->smk_task->smk_rules);
4692 * If the directory is transmuting and the rule
4693 * providing access is transmuting use the containing
4694 * directory label instead of the process label.
4696 if (may > 0 && (may & MAY_TRANSMUTE))
4697 ntsp->smk_task = isp->smk_inode;
4702 #ifdef CONFIG_IO_URING
4704 * smack_uring_override_creds - Is io_uring cred override allowed?
4705 * @new: the target creds
4707 * Check to see if the current task is allowed to override it's credentials
4708 * to service an io_uring operation.
4710 static int smack_uring_override_creds(const struct cred *new)
4712 struct task_smack *tsp = smack_cred(current_cred());
4713 struct task_smack *nsp = smack_cred(new);
4716 * Allow the degenerate case where the new Smack value is
4717 * the same as the current Smack value.
4719 if (tsp->smk_task == nsp->smk_task)
4722 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4729 * smack_uring_sqpoll - check if a io_uring polling thread can be created
4731 * Check to see if the current task is allowed to create a new io_uring
4732 * kernel polling thread.
4734 static int smack_uring_sqpoll(void)
4736 if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred()))
4742 #endif /* CONFIG_IO_URING */
4744 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4745 .lbs_cred = sizeof(struct task_smack),
4746 .lbs_file = sizeof(struct smack_known *),
4747 .lbs_inode = sizeof(struct inode_smack),
4748 .lbs_ipc = sizeof(struct smack_known *),
4749 .lbs_msg_msg = sizeof(struct smack_known *),
4750 .lbs_superblock = sizeof(struct superblock_smack),
4753 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4754 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4755 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4756 LSM_HOOK_INIT(syslog, smack_syslog),
4758 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4759 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4761 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4762 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4763 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4764 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4765 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4767 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
4769 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4770 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4771 LSM_HOOK_INIT(inode_link, smack_inode_link),
4772 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4773 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4774 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4775 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4776 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4777 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4778 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4779 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4780 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4781 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4782 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4783 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4784 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4785 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4787 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4788 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4789 LSM_HOOK_INIT(file_lock, smack_file_lock),
4790 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4791 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4792 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4793 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4794 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4795 LSM_HOOK_INIT(file_receive, smack_file_receive),
4797 LSM_HOOK_INIT(file_open, smack_file_open),
4799 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4800 LSM_HOOK_INIT(cred_free, smack_cred_free),
4801 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4802 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4803 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4804 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4805 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4806 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4807 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4808 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4809 LSM_HOOK_INIT(current_getsecid_subj, smack_current_getsecid_subj),
4810 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
4811 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4812 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4813 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4814 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4815 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4816 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4817 LSM_HOOK_INIT(task_kill, smack_task_kill),
4818 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4820 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4821 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4823 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4825 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4826 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4827 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4828 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4829 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4831 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4832 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4833 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4834 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4836 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4837 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4838 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4839 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4841 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4843 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4844 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4846 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4847 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4849 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4850 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4851 #ifdef SMACK_IPV6_PORT_LABELING
4852 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4854 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4855 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4856 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4857 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4858 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4859 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4860 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4861 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4862 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4863 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4865 /* key management security hooks */
4867 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4868 LSM_HOOK_INIT(key_free, smack_key_free),
4869 LSM_HOOK_INIT(key_permission, smack_key_permission),
4870 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4871 #ifdef CONFIG_KEY_NOTIFICATIONS
4872 LSM_HOOK_INIT(watch_key, smack_watch_key),
4874 #endif /* CONFIG_KEYS */
4876 #ifdef CONFIG_WATCH_QUEUE
4877 LSM_HOOK_INIT(post_notification, smack_post_notification),
4882 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4883 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4884 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4885 #endif /* CONFIG_AUDIT */
4887 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4888 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4889 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4890 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4891 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4892 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4893 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4894 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4895 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4896 #ifdef CONFIG_IO_URING
4897 LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds),
4898 LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll),
4903 static __init void init_smack_known_list(void)
4906 * Initialize rule list locks
4908 mutex_init(&smack_known_huh.smk_rules_lock);
4909 mutex_init(&smack_known_hat.smk_rules_lock);
4910 mutex_init(&smack_known_floor.smk_rules_lock);
4911 mutex_init(&smack_known_star.smk_rules_lock);
4912 mutex_init(&smack_known_web.smk_rules_lock);
4914 * Initialize rule lists
4916 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4917 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4918 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4919 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4920 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4922 * Create the known labels list
4924 smk_insert_entry(&smack_known_huh);
4925 smk_insert_entry(&smack_known_hat);
4926 smk_insert_entry(&smack_known_star);
4927 smk_insert_entry(&smack_known_floor);
4928 smk_insert_entry(&smack_known_web);
4932 * smack_init - initialize the smack system
4934 * Returns 0 on success, -ENOMEM is there's no memory
4936 static __init int smack_init(void)
4938 struct cred *cred = (struct cred *) current->cred;
4939 struct task_smack *tsp;
4941 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4942 if (!smack_rule_cache)
4946 * Set the security state for the initial task.
4948 tsp = smack_cred(cred);
4949 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4954 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4957 pr_info("Smack: Initializing.\n");
4958 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4959 pr_info("Smack: Netfilter enabled.\n");
4961 #ifdef SMACK_IPV6_PORT_LABELING
4962 pr_info("Smack: IPv6 port labeling enabled.\n");
4964 #ifdef SMACK_IPV6_SECMARK_LABELING
4965 pr_info("Smack: IPv6 Netfilter enabled.\n");
4968 /* initialize the smack_known_list */
4969 init_smack_known_list();
4975 * Smack requires early initialization in order to label
4976 * all processes and objects when they are created.
4978 DEFINE_LSM(smack) = {
4980 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4981 .blobs = &smack_blob_sizes,