1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
8 * Casey Schaufler <casey@schaufler-ca.com>
9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
13 * Paul Moore <paul@paul-moore.com>
14 * Copyright (C) 2010 Nokia Corporation
15 * Copyright (C) 2011 Intel Corporation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
23 #include <asm/ioctls.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/dccp.h>
28 #include <linux/icmpv6.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
38 #include <linux/msg.h>
39 #include <linux/shm.h>
40 #include <linux/binfmts.h>
41 #include <linux/parser.h>
42 #include <linux/fs_context.h>
43 #include <linux/fs_parser.h>
46 #define TRANS_TRUE "TRUE"
47 #define TRANS_TRUE_SIZE 4
49 #define SMK_CONNECTING 0
50 #define SMK_RECEIVING 1
53 #ifdef SMACK_IPV6_PORT_LABELING
54 DEFINE_MUTEX(smack_ipv6_lock);
55 static LIST_HEAD(smk_ipv6_port_list);
57 static struct kmem_cache *smack_inode_cache;
58 struct kmem_cache *smack_rule_cache;
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(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;
319 mutex_init(&isp->smk_lock);
323 * init_task_smack - initialize a task security blob
324 * @tsp: blob to initialize
325 * @task: a pointer to the Smack label for the running task
326 * @forked: a pointer to the Smack label for the forked task
329 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
330 struct smack_known *forked)
332 tsp->smk_task = task;
333 tsp->smk_forked = forked;
334 INIT_LIST_HEAD(&tsp->smk_rules);
335 INIT_LIST_HEAD(&tsp->smk_relabel);
336 mutex_init(&tsp->smk_rules_lock);
340 * smk_copy_rules - copy a rule set
341 * @nhead: new rules header pointer
342 * @ohead: old rules header pointer
343 * @gfp: type of the memory for the allocation
345 * Returns 0 on success, -ENOMEM on error
347 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
350 struct smack_rule *nrp;
351 struct smack_rule *orp;
354 list_for_each_entry_rcu(orp, ohead, list) {
355 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
361 list_add_rcu(&nrp->list, nhead);
367 * smk_copy_relabel - copy smk_relabel labels list
368 * @nhead: new rules header pointer
369 * @ohead: old rules header pointer
370 * @gfp: type of the memory for the allocation
372 * Returns 0 on success, -ENOMEM on error
374 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
377 struct smack_known_list_elem *nklep;
378 struct smack_known_list_elem *oklep;
380 list_for_each_entry(oklep, ohead, list) {
381 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
383 smk_destroy_label_list(nhead);
386 nklep->smk_label = oklep->smk_label;
387 list_add(&nklep->list, nhead);
394 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
395 * @mode - input mode in form of PTRACE_MODE_*
397 * Returns a converted MAY_* mode usable by smack rules
399 static inline unsigned int smk_ptrace_mode(unsigned int mode)
401 if (mode & PTRACE_MODE_ATTACH)
402 return MAY_READWRITE;
403 if (mode & PTRACE_MODE_READ)
410 * smk_ptrace_rule_check - helper for ptrace access
411 * @tracer: tracer process
412 * @tracee_known: label entry of the process that's about to be traced
413 * @mode: ptrace attachment mode (PTRACE_MODE_*)
414 * @func: name of the function that called us, used for audit
416 * Returns 0 on access granted, -error on error
418 static int smk_ptrace_rule_check(struct task_struct *tracer,
419 struct smack_known *tracee_known,
420 unsigned int mode, const char *func)
423 struct smk_audit_info ad, *saip = NULL;
424 struct task_smack *tsp;
425 struct smack_known *tracer_known;
426 const struct cred *tracercred;
428 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
429 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
430 smk_ad_setfield_u_tsk(&ad, tracer);
435 tracercred = __task_cred(tracer);
436 tsp = smack_cred(tracercred);
437 tracer_known = smk_of_task(tsp);
439 if ((mode & PTRACE_MODE_ATTACH) &&
440 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
441 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
442 if (tracer_known->smk_known == tracee_known->smk_known)
444 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
446 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
452 smack_log(tracer_known->smk_known,
453 tracee_known->smk_known,
460 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
461 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
469 * We he, that is fun!
473 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
474 * @ctp: child task pointer
475 * @mode: ptrace attachment mode (PTRACE_MODE_*)
477 * Returns 0 if access is OK, an error code otherwise
479 * Do the capability checks.
481 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
483 struct smack_known *skp;
485 skp = smk_of_task_struct(ctp);
487 return smk_ptrace_rule_check(current, skp, mode, __func__);
491 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
492 * @ptp: parent task pointer
494 * Returns 0 if access is OK, an error code otherwise
496 * Do the capability checks, and require PTRACE_MODE_ATTACH.
498 static int smack_ptrace_traceme(struct task_struct *ptp)
501 struct smack_known *skp;
503 skp = smk_of_task(smack_cred(current_cred()));
505 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
510 * smack_syslog - Smack approval on syslog
511 * @typefrom_file: unused
513 * Returns 0 on success, error code otherwise.
515 static int smack_syslog(int typefrom_file)
518 struct smack_known *skp = smk_of_current();
520 if (smack_privileged(CAP_MAC_OVERRIDE))
523 if (smack_syslog_label != NULL && smack_syslog_label != skp)
534 * smack_sb_alloc_security - allocate a superblock blob
535 * @sb: the superblock getting the blob
537 * Returns 0 on success or -ENOMEM on error.
539 static int smack_sb_alloc_security(struct super_block *sb)
541 struct superblock_smack *sbsp;
543 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
548 sbsp->smk_root = &smack_known_floor;
549 sbsp->smk_default = &smack_known_floor;
550 sbsp->smk_floor = &smack_known_floor;
551 sbsp->smk_hat = &smack_known_hat;
553 * SMK_SB_INITIALIZED will be zero from kzalloc.
555 sb->s_security = sbsp;
561 * smack_sb_free_security - free a superblock blob
562 * @sb: the superblock getting the blob
565 static void smack_sb_free_security(struct super_block *sb)
567 kfree(sb->s_security);
568 sb->s_security = NULL;
571 struct smack_mnt_opts {
572 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
575 static void smack_free_mnt_opts(void *mnt_opts)
577 struct smack_mnt_opts *opts = mnt_opts;
578 kfree(opts->fsdefault);
579 kfree(opts->fsfloor);
582 kfree(opts->fstransmute);
586 static int smack_add_opt(int token, const char *s, void **mnt_opts)
588 struct smack_mnt_opts *opts = *mnt_opts;
591 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
620 case Opt_fstransmute:
621 if (opts->fstransmute)
623 opts->fstransmute = s;
629 pr_warn("Smack: duplicate mount options\n");
634 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
635 * @fc: The new filesystem context.
636 * @src_fc: The source filesystem context being duplicated.
638 * Returns 0 on success or -ENOMEM on error.
640 static int smack_fs_context_dup(struct fs_context *fc,
641 struct fs_context *src_fc)
643 struct smack_mnt_opts *dst, *src = src_fc->security;
648 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
653 if (src->fsdefault) {
654 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
659 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
664 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
669 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
673 if (src->fstransmute) {
674 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
675 if (!dst->fstransmute)
681 static const struct fs_parameter_spec smack_param_specs[] = {
682 fsparam_string("smackfsdef", Opt_fsdefault),
683 fsparam_string("smackfsdefault", Opt_fsdefault),
684 fsparam_string("smackfsfloor", Opt_fsfloor),
685 fsparam_string("smackfshat", Opt_fshat),
686 fsparam_string("smackfsroot", Opt_fsroot),
687 fsparam_string("smackfstransmute", Opt_fstransmute),
691 static const struct fs_parameter_description smack_fs_parameters = {
692 .specs = smack_param_specs,
696 * smack_fs_context_parse_param - Parse a single mount parameter
697 * @fc: The new filesystem context being constructed.
698 * @param: The parameter.
700 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
703 static int smack_fs_context_parse_param(struct fs_context *fc,
704 struct fs_parameter *param)
706 struct fs_parse_result result;
709 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
713 rc = smack_add_opt(opt, param->string, &fc->security);
715 param->string = NULL;
719 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
721 char *from = options, *to = options;
725 char *next = strchr(from, ',');
734 token = match_opt_prefix(from, len, &arg);
735 if (token != Opt_error) {
736 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
737 rc = smack_add_opt(token, arg, mnt_opts);
741 smack_free_mnt_opts(*mnt_opts);
746 if (!first) { // copy with preceding comma
751 memmove(to, from, len);
764 * smack_set_mnt_opts - set Smack specific mount options
765 * @sb: the file system superblock
766 * @mnt_opts: Smack mount options
767 * @kern_flags: mount option from kernel space or user space
768 * @set_kern_flags: where to store converted mount opts
770 * Returns 0 on success, an error code on failure
772 * Allow filesystems with binary mount data to explicitly set Smack mount
775 static int smack_set_mnt_opts(struct super_block *sb,
777 unsigned long kern_flags,
778 unsigned long *set_kern_flags)
780 struct dentry *root = sb->s_root;
781 struct inode *inode = d_backing_inode(root);
782 struct superblock_smack *sp = sb->s_security;
783 struct inode_smack *isp;
784 struct smack_known *skp;
785 struct smack_mnt_opts *opts = mnt_opts;
786 bool transmute = false;
788 if (sp->smk_flags & SMK_SB_INITIALIZED)
791 if (inode->i_security == NULL) {
792 int rc = lsm_inode_alloc(inode);
798 if (!smack_privileged(CAP_MAC_ADMIN)) {
800 * Unprivileged mounts don't get to specify Smack values.
805 * Unprivileged mounts get root and default from the caller.
807 skp = smk_of_current();
809 sp->smk_default = skp;
811 * For a handful of fs types with no user-controlled
812 * backing store it's okay to trust security labels
813 * in the filesystem. The rest are untrusted.
815 if (sb->s_user_ns != &init_user_ns &&
816 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
817 sb->s_magic != RAMFS_MAGIC) {
819 sp->smk_flags |= SMK_SB_UNTRUSTED;
823 sp->smk_flags |= SMK_SB_INITIALIZED;
826 if (opts->fsdefault) {
827 skp = smk_import_entry(opts->fsdefault, 0);
830 sp->smk_default = skp;
833 skp = smk_import_entry(opts->fsfloor, 0);
839 skp = smk_import_entry(opts->fshat, 0);
845 skp = smk_import_entry(opts->fsroot, 0);
850 if (opts->fstransmute) {
851 skp = smk_import_entry(opts->fstransmute, 0);
860 * Initialize the root inode.
862 init_inode_smack(inode, sp->smk_root);
865 isp = smack_inode(inode);
866 isp->smk_flags |= SMK_INODE_TRANSMUTE;
873 * smack_sb_statfs - Smack check on statfs
874 * @dentry: identifies the file system in question
876 * Returns 0 if current can read the floor of the filesystem,
877 * and error code otherwise
879 static int smack_sb_statfs(struct dentry *dentry)
881 struct superblock_smack *sbp = dentry->d_sb->s_security;
883 struct smk_audit_info ad;
885 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
886 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
888 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
889 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
898 * smack_bprm_set_creds - set creds for exec
899 * @bprm: the exec information
901 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
903 static int smack_bprm_set_creds(struct linux_binprm *bprm)
905 struct inode *inode = file_inode(bprm->file);
906 struct task_smack *bsp = smack_cred(bprm->cred);
907 struct inode_smack *isp;
908 struct superblock_smack *sbsp;
911 if (bprm->called_set_creds)
914 isp = smack_inode(inode);
915 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
918 sbsp = inode->i_sb->s_security;
919 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
920 isp->smk_task != sbsp->smk_root)
923 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
924 struct task_struct *tracer;
928 tracer = ptrace_parent(current);
929 if (likely(tracer != NULL))
930 rc = smk_ptrace_rule_check(tracer,
939 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
942 bsp->smk_task = isp->smk_task;
943 bprm->per_clear |= PER_CLEAR_ON_SETID;
945 /* Decide if this is a secure exec. */
946 if (bsp->smk_task != bsp->smk_forked)
947 bprm->secureexec = 1;
957 * smack_inode_alloc_security - allocate an inode blob
958 * @inode: the inode in need of a blob
962 static int smack_inode_alloc_security(struct inode *inode)
964 struct smack_known *skp = smk_of_current();
966 init_inode_smack(inode, skp);
971 * smack_inode_init_security - copy out the smack from an inode
972 * @inode: the newly created inode
973 * @dir: containing directory object
975 * @name: where to put the attribute name
976 * @value: where to put the attribute value
977 * @len: where to put the length of the attribute
979 * Returns 0 if it all works out, -ENOMEM if there's no memory
981 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
982 const struct qstr *qstr, const char **name,
983 void **value, size_t *len)
985 struct inode_smack *issp = smack_inode(inode);
986 struct smack_known *skp = smk_of_current();
987 struct smack_known *isp = smk_of_inode(inode);
988 struct smack_known *dsp = smk_of_inode(dir);
992 *name = XATTR_SMACK_SUFFIX;
996 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1001 * If the access rule allows transmutation and
1002 * the directory requests transmutation then
1003 * by all means transmute.
1004 * Mark the inode as changed.
1006 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1007 smk_inode_transmutable(dir)) {
1009 issp->smk_flags |= SMK_INODE_CHANGED;
1012 *value = kstrdup(isp->smk_known, GFP_NOFS);
1016 *len = strlen(isp->smk_known);
1023 * smack_inode_link - Smack check on link
1024 * @old_dentry: the existing object
1026 * @new_dentry: the new object
1028 * Returns 0 if access is permitted, an error code otherwise
1030 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1031 struct dentry *new_dentry)
1033 struct smack_known *isp;
1034 struct smk_audit_info ad;
1037 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1038 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1040 isp = smk_of_inode(d_backing_inode(old_dentry));
1041 rc = smk_curacc(isp, MAY_WRITE, &ad);
1042 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1044 if (rc == 0 && d_is_positive(new_dentry)) {
1045 isp = smk_of_inode(d_backing_inode(new_dentry));
1046 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1047 rc = smk_curacc(isp, MAY_WRITE, &ad);
1048 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1055 * smack_inode_unlink - Smack check on inode deletion
1056 * @dir: containing directory object
1057 * @dentry: file to unlink
1059 * Returns 0 if current can write the containing directory
1060 * and the object, error code otherwise
1062 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1064 struct inode *ip = d_backing_inode(dentry);
1065 struct smk_audit_info ad;
1068 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1069 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1072 * You need write access to the thing you're unlinking
1074 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1075 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1078 * You also need write access to the containing directory
1080 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1081 smk_ad_setfield_u_fs_inode(&ad, dir);
1082 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1083 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1089 * smack_inode_rmdir - Smack check on directory deletion
1090 * @dir: containing directory object
1091 * @dentry: directory to unlink
1093 * Returns 0 if current can write the containing directory
1094 * and the directory, error code otherwise
1096 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1098 struct smk_audit_info ad;
1101 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1102 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1105 * You need write access to the thing you're removing
1107 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1108 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1111 * You also need write access to the containing directory
1113 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1114 smk_ad_setfield_u_fs_inode(&ad, dir);
1115 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1116 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1123 * smack_inode_rename - Smack check on rename
1124 * @old_inode: unused
1125 * @old_dentry: the old object
1126 * @new_inode: unused
1127 * @new_dentry: the new object
1129 * Read and write access is required on both the old and
1132 * Returns 0 if access is permitted, an error code otherwise
1134 static int smack_inode_rename(struct inode *old_inode,
1135 struct dentry *old_dentry,
1136 struct inode *new_inode,
1137 struct dentry *new_dentry)
1140 struct smack_known *isp;
1141 struct smk_audit_info ad;
1143 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1144 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1146 isp = smk_of_inode(d_backing_inode(old_dentry));
1147 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1148 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1150 if (rc == 0 && d_is_positive(new_dentry)) {
1151 isp = smk_of_inode(d_backing_inode(new_dentry));
1152 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1153 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1154 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1160 * smack_inode_permission - Smack version of permission()
1161 * @inode: the inode in question
1162 * @mask: the access requested
1164 * This is the important Smack hook.
1166 * Returns 0 if access is permitted, an error code otherwise
1168 static int smack_inode_permission(struct inode *inode, int mask)
1170 struct superblock_smack *sbsp = inode->i_sb->s_security;
1171 struct smk_audit_info ad;
1172 int no_block = mask & MAY_NOT_BLOCK;
1175 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1177 * No permission to check. Existence test. Yup, it's there.
1182 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1183 if (smk_of_inode(inode) != sbsp->smk_root)
1187 /* May be droppable after audit */
1190 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1191 smk_ad_setfield_u_fs_inode(&ad, inode);
1192 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1193 rc = smk_bu_inode(inode, mask, rc);
1198 * smack_inode_setattr - Smack check for setting attributes
1199 * @dentry: the object
1200 * @iattr: for the force flag
1202 * Returns 0 if access is permitted, an error code otherwise
1204 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1206 struct smk_audit_info ad;
1210 * Need to allow for clearing the setuid bit.
1212 if (iattr->ia_valid & ATTR_FORCE)
1214 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1215 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1217 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1218 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1223 * smack_inode_getattr - Smack check for getting attributes
1224 * @path: path to extract the info from
1226 * Returns 0 if access is permitted, an error code otherwise
1228 static int smack_inode_getattr(const struct path *path)
1230 struct smk_audit_info ad;
1231 struct inode *inode = d_backing_inode(path->dentry);
1234 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1235 smk_ad_setfield_u_fs_path(&ad, *path);
1236 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1237 rc = smk_bu_inode(inode, MAY_READ, rc);
1242 * smack_inode_setxattr - Smack check for setting xattrs
1243 * @dentry: the object
1244 * @name: name of the attribute
1245 * @value: value of the attribute
1246 * @size: size of the value
1249 * This protects the Smack attribute explicitly.
1251 * Returns 0 if access is permitted, an error code otherwise
1253 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1254 const void *value, size_t size, int flags)
1256 struct smk_audit_info ad;
1257 struct smack_known *skp;
1259 int check_import = 0;
1264 * Check label validity here so import won't fail in post_setxattr
1266 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1267 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1268 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1271 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1272 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1276 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1278 if (size != TRANS_TRUE_SIZE ||
1279 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1282 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1284 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1287 if (rc == 0 && check_import) {
1288 skp = size ? smk_import_entry(value, size) : NULL;
1291 else if (skp == NULL || (check_star &&
1292 (skp == &smack_known_star || skp == &smack_known_web)))
1296 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1297 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1300 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1301 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1308 * smack_inode_post_setxattr - Apply the Smack update approved above
1310 * @name: attribute name
1311 * @value: attribute value
1312 * @size: attribute size
1315 * Set the pointer in the inode blob to the entry found
1316 * in the master label list.
1318 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1319 const void *value, size_t size, int flags)
1321 struct smack_known *skp;
1322 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1324 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1325 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1329 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1330 skp = smk_import_entry(value, size);
1332 isp->smk_inode = skp;
1333 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1334 skp = smk_import_entry(value, size);
1336 isp->smk_task = skp;
1337 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1338 skp = smk_import_entry(value, size);
1340 isp->smk_mmap = skp;
1347 * smack_inode_getxattr - Smack check on getxattr
1348 * @dentry: the object
1351 * Returns 0 if access is permitted, an error code otherwise
1353 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1355 struct smk_audit_info ad;
1358 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1359 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1361 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1362 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1367 * smack_inode_removexattr - Smack check on removexattr
1368 * @dentry: the object
1369 * @name: name of the attribute
1371 * Removing the Smack attribute requires CAP_MAC_ADMIN
1373 * Returns 0 if access is permitted, an error code otherwise
1375 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1377 struct inode_smack *isp;
1378 struct smk_audit_info ad;
1381 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1382 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1383 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1384 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1385 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1386 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1387 if (!smack_privileged(CAP_MAC_ADMIN))
1390 rc = cap_inode_removexattr(dentry, name);
1395 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1396 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1398 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1399 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1403 isp = smack_inode(d_backing_inode(dentry));
1405 * Don't do anything special for these.
1406 * XATTR_NAME_SMACKIPIN
1407 * XATTR_NAME_SMACKIPOUT
1409 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1410 struct super_block *sbp = dentry->d_sb;
1411 struct superblock_smack *sbsp = sbp->s_security;
1413 isp->smk_inode = sbsp->smk_default;
1414 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1415 isp->smk_task = NULL;
1416 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1417 isp->smk_mmap = NULL;
1418 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1419 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1425 * smack_inode_getsecurity - get smack xattrs
1426 * @inode: the object
1427 * @name: attribute name
1428 * @buffer: where to put the result
1429 * @alloc: duplicate memory
1431 * Returns the size of the attribute or an error code
1433 static int smack_inode_getsecurity(struct inode *inode,
1434 const char *name, void **buffer,
1437 struct socket_smack *ssp;
1438 struct socket *sock;
1439 struct super_block *sbp;
1440 struct inode *ip = (struct inode *)inode;
1441 struct smack_known *isp;
1443 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1444 isp = smk_of_inode(inode);
1447 * The rest of the Smack xattrs are only on sockets.
1450 if (sbp->s_magic != SOCKFS_MAGIC)
1453 sock = SOCKET_I(ip);
1454 if (sock == NULL || sock->sk == NULL)
1457 ssp = sock->sk->sk_security;
1459 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1461 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1468 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1469 if (*buffer == NULL)
1473 return strlen(isp->smk_known);
1478 * smack_inode_listsecurity - list the Smack attributes
1479 * @inode: the object
1480 * @buffer: where they go
1481 * @buffer_size: size of buffer
1483 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1486 int len = sizeof(XATTR_NAME_SMACK);
1488 if (buffer != NULL && len <= buffer_size)
1489 memcpy(buffer, XATTR_NAME_SMACK, len);
1495 * smack_inode_getsecid - Extract inode's security id
1496 * @inode: inode to extract the info from
1497 * @secid: where result will be saved
1499 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1501 struct smack_known *skp = smk_of_inode(inode);
1503 *secid = skp->smk_secid;
1511 * There is no smack_file_permission hook
1513 * Should access checks be done on each read or write?
1514 * UNICOS and SELinux say yes.
1515 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1517 * I'll say no for now. Smack does not do the frequent
1518 * label changing that SELinux does.
1522 * smack_file_alloc_security - assign a file security blob
1525 * The security blob for a file is a pointer to the master
1526 * label list, so no allocation is done.
1528 * f_security is the owner security information. It
1529 * isn't used on file access checks, it's for send_sigio.
1533 static int smack_file_alloc_security(struct file *file)
1535 struct smack_known **blob = smack_file(file);
1537 *blob = smk_of_current();
1542 * smack_file_ioctl - Smack check on ioctls
1547 * Relies heavily on the correct use of the ioctl command conventions.
1549 * Returns 0 if allowed, error code otherwise
1551 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1555 struct smk_audit_info ad;
1556 struct inode *inode = file_inode(file);
1558 if (unlikely(IS_PRIVATE(inode)))
1561 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1562 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1564 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1565 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1566 rc = smk_bu_file(file, MAY_WRITE, rc);
1569 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1570 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1571 rc = smk_bu_file(file, MAY_READ, rc);
1578 * smack_file_lock - Smack check on file locking
1582 * Returns 0 if current has lock access, error code otherwise
1584 static int smack_file_lock(struct file *file, unsigned int cmd)
1586 struct smk_audit_info ad;
1588 struct inode *inode = file_inode(file);
1590 if (unlikely(IS_PRIVATE(inode)))
1593 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1594 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1595 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1596 rc = smk_bu_file(file, MAY_LOCK, rc);
1601 * smack_file_fcntl - Smack check on fcntl
1603 * @cmd: what action to check
1606 * Generally these operations are harmless.
1607 * File locking operations present an obvious mechanism
1608 * for passing information, so they require write access.
1610 * Returns 0 if current has access, error code otherwise
1612 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1615 struct smk_audit_info ad;
1617 struct inode *inode = file_inode(file);
1619 if (unlikely(IS_PRIVATE(inode)))
1627 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1628 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1629 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1630 rc = smk_bu_file(file, MAY_LOCK, rc);
1634 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1635 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1636 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1637 rc = smk_bu_file(file, MAY_WRITE, rc);
1648 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1649 * if mapping anonymous memory.
1650 * @file contains the file structure for file to map (may be NULL).
1651 * @reqprot contains the protection requested by the application.
1652 * @prot contains the protection that will be applied by the kernel.
1653 * @flags contains the operational flags.
1654 * Return 0 if permission is granted.
1656 static int smack_mmap_file(struct file *file,
1657 unsigned long reqprot, unsigned long prot,
1658 unsigned long flags)
1660 struct smack_known *skp;
1661 struct smack_known *mkp;
1662 struct smack_rule *srp;
1663 struct task_smack *tsp;
1664 struct smack_known *okp;
1665 struct inode_smack *isp;
1666 struct superblock_smack *sbsp;
1675 if (unlikely(IS_PRIVATE(file_inode(file))))
1678 isp = smack_inode(file_inode(file));
1679 if (isp->smk_mmap == NULL)
1681 sbsp = file_inode(file)->i_sb->s_security;
1682 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1683 isp->smk_mmap != sbsp->smk_root)
1685 mkp = isp->smk_mmap;
1687 tsp = smack_cred(current_cred());
1688 skp = smk_of_current();
1693 * For each Smack rule associated with the subject
1694 * label verify that the SMACK64MMAP also has access
1695 * to that rule's object label.
1697 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1698 okp = srp->smk_object;
1700 * Matching labels always allows access.
1702 if (mkp->smk_known == okp->smk_known)
1705 * If there is a matching local rule take
1706 * that into account as well.
1708 may = smk_access_entry(srp->smk_subject->smk_known,
1712 may = srp->smk_access;
1714 may &= srp->smk_access;
1716 * If may is zero the SMACK64MMAP subject can't
1717 * possibly have less access.
1723 * Fetch the global list entry.
1724 * If there isn't one a SMACK64MMAP subject
1725 * can't have as much access as current.
1727 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1729 if (mmay == -ENOENT) {
1734 * If there is a local entry it modifies the
1735 * potential access, too.
1737 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1739 if (tmay != -ENOENT)
1743 * If there is any access available to current that is
1744 * not available to a SMACK64MMAP subject
1747 if ((may | mmay) != mmay) {
1759 * smack_file_set_fowner - set the file security blob value
1760 * @file: object in question
1763 static void smack_file_set_fowner(struct file *file)
1765 struct smack_known **blob = smack_file(file);
1767 *blob = smk_of_current();
1771 * smack_file_send_sigiotask - Smack on sigio
1772 * @tsk: The target task
1773 * @fown: the object the signal come from
1776 * Allow a privileged task to get signals even if it shouldn't
1778 * Returns 0 if a subject with the object's smack could
1779 * write to the task, an error code otherwise.
1781 static int smack_file_send_sigiotask(struct task_struct *tsk,
1782 struct fown_struct *fown, int signum)
1784 struct smack_known **blob;
1785 struct smack_known *skp;
1786 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1787 const struct cred *tcred;
1790 struct smk_audit_info ad;
1793 * struct fown_struct is never outside the context of a struct file
1795 file = container_of(fown, struct file, f_owner);
1797 /* we don't log here as rc can be overriden */
1798 blob = smack_file(file);
1800 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1801 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1804 tcred = __task_cred(tsk);
1805 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1809 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1810 smk_ad_setfield_u_tsk(&ad, tsk);
1811 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1816 * smack_file_receive - Smack file receive check
1819 * Returns 0 if current has access, error code otherwise
1821 static int smack_file_receive(struct file *file)
1825 struct smk_audit_info ad;
1826 struct inode *inode = file_inode(file);
1827 struct socket *sock;
1828 struct task_smack *tsp;
1829 struct socket_smack *ssp;
1831 if (unlikely(IS_PRIVATE(inode)))
1834 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1835 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1837 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1838 sock = SOCKET_I(inode);
1839 ssp = sock->sk->sk_security;
1840 tsp = smack_cred(current_cred());
1842 * If the receiving process can't write to the
1843 * passed socket or if the passed socket can't
1844 * write to the receiving process don't accept
1845 * the passed socket.
1847 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1848 rc = smk_bu_file(file, may, rc);
1851 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1852 rc = smk_bu_file(file, may, rc);
1856 * This code relies on bitmasks.
1858 if (file->f_mode & FMODE_READ)
1860 if (file->f_mode & FMODE_WRITE)
1863 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1864 rc = smk_bu_file(file, may, rc);
1869 * smack_file_open - Smack dentry open processing
1872 * Set the security blob in the file structure.
1873 * Allow the open only if the task has read access. There are
1874 * many read operations (e.g. fstat) that you can do with an
1875 * fd even if you have the file open write-only.
1877 * Returns 0 if current has access, error code otherwise
1879 static int smack_file_open(struct file *file)
1881 struct task_smack *tsp = smack_cred(file->f_cred);
1882 struct inode *inode = file_inode(file);
1883 struct smk_audit_info ad;
1886 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1887 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1888 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1889 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1899 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1900 * @cred: the new credentials
1901 * @gfp: the atomicity of any memory allocations
1903 * Prepare a blank set of credentials for modification. This must allocate all
1904 * the memory the LSM module might require such that cred_transfer() can
1905 * complete without error.
1907 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1909 init_task_smack(smack_cred(cred), NULL, NULL);
1915 * smack_cred_free - "free" task-level security credentials
1916 * @cred: the credentials in question
1919 static void smack_cred_free(struct cred *cred)
1921 struct task_smack *tsp = smack_cred(cred);
1922 struct smack_rule *rp;
1923 struct list_head *l;
1924 struct list_head *n;
1926 smk_destroy_label_list(&tsp->smk_relabel);
1928 list_for_each_safe(l, n, &tsp->smk_rules) {
1929 rp = list_entry(l, struct smack_rule, list);
1930 list_del(&rp->list);
1931 kmem_cache_free(smack_rule_cache, rp);
1936 * smack_cred_prepare - prepare new set of credentials for modification
1937 * @new: the new credentials
1938 * @old: the original credentials
1939 * @gfp: the atomicity of any memory allocations
1941 * Prepare a new set of credentials for modification.
1943 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1946 struct task_smack *old_tsp = smack_cred(old);
1947 struct task_smack *new_tsp = smack_cred(new);
1950 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1952 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1956 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1962 * smack_cred_transfer - Transfer the old credentials to the new credentials
1963 * @new: the new credentials
1964 * @old: the original credentials
1966 * Fill in a set of blank credentials from another set of credentials.
1968 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1970 struct task_smack *old_tsp = smack_cred(old);
1971 struct task_smack *new_tsp = smack_cred(new);
1973 new_tsp->smk_task = old_tsp->smk_task;
1974 new_tsp->smk_forked = old_tsp->smk_task;
1975 mutex_init(&new_tsp->smk_rules_lock);
1976 INIT_LIST_HEAD(&new_tsp->smk_rules);
1978 /* cbs copy rule list */
1982 * smack_cred_getsecid - get the secid corresponding to a creds structure
1983 * @cred: the object creds
1984 * @secid: where to put the result
1986 * Sets the secid to contain a u32 version of the smack label.
1988 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1990 struct smack_known *skp;
1993 skp = smk_of_task(smack_cred(cred));
1994 *secid = skp->smk_secid;
1999 * smack_kernel_act_as - Set the subjective context in a set of credentials
2000 * @new: points to the set of credentials to be modified.
2001 * @secid: specifies the security ID to be set
2003 * Set the security data for a kernel service.
2005 static int smack_kernel_act_as(struct cred *new, u32 secid)
2007 struct task_smack *new_tsp = smack_cred(new);
2009 new_tsp->smk_task = smack_from_secid(secid);
2014 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2015 * @new: points to the set of credentials to be modified
2016 * @inode: points to the inode to use as a reference
2018 * Set the file creation context in a set of credentials to the same
2019 * as the objective context of the specified inode
2021 static int smack_kernel_create_files_as(struct cred *new,
2022 struct inode *inode)
2024 struct inode_smack *isp = smack_inode(inode);
2025 struct task_smack *tsp = smack_cred(new);
2027 tsp->smk_forked = isp->smk_inode;
2028 tsp->smk_task = tsp->smk_forked;
2033 * smk_curacc_on_task - helper to log task related access
2034 * @p: the task object
2035 * @access: the access requested
2036 * @caller: name of the calling function for audit
2038 * Return 0 if access is permitted
2040 static int smk_curacc_on_task(struct task_struct *p, int access,
2043 struct smk_audit_info ad;
2044 struct smack_known *skp = smk_of_task_struct(p);
2047 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2048 smk_ad_setfield_u_tsk(&ad, p);
2049 rc = smk_curacc(skp, access, &ad);
2050 rc = smk_bu_task(p, access, rc);
2055 * smack_task_setpgid - Smack check on setting pgid
2056 * @p: the task object
2059 * Return 0 if write access is permitted
2061 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2063 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2067 * smack_task_getpgid - Smack access check for getpgid
2068 * @p: the object task
2070 * Returns 0 if current can read the object task, error code otherwise
2072 static int smack_task_getpgid(struct task_struct *p)
2074 return smk_curacc_on_task(p, MAY_READ, __func__);
2078 * smack_task_getsid - Smack access check for getsid
2079 * @p: the object task
2081 * Returns 0 if current can read the object task, error code otherwise
2083 static int smack_task_getsid(struct task_struct *p)
2085 return smk_curacc_on_task(p, MAY_READ, __func__);
2089 * smack_task_getsecid - get the secid of the task
2090 * @p: the object task
2091 * @secid: where to put the result
2093 * Sets the secid to contain a u32 version of the smack label.
2095 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2097 struct smack_known *skp = smk_of_task_struct(p);
2099 *secid = skp->smk_secid;
2103 * smack_task_setnice - Smack check on setting nice
2104 * @p: the task object
2107 * Return 0 if write access is permitted
2109 static int smack_task_setnice(struct task_struct *p, int nice)
2111 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2115 * smack_task_setioprio - Smack check on setting ioprio
2116 * @p: the task object
2119 * Return 0 if write access is permitted
2121 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2123 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2127 * smack_task_getioprio - Smack check on reading ioprio
2128 * @p: the task object
2130 * Return 0 if read access is permitted
2132 static int smack_task_getioprio(struct task_struct *p)
2134 return smk_curacc_on_task(p, MAY_READ, __func__);
2138 * smack_task_setscheduler - Smack check on setting scheduler
2139 * @p: the task object
2141 * Return 0 if read access is permitted
2143 static int smack_task_setscheduler(struct task_struct *p)
2145 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2149 * smack_task_getscheduler - Smack check on reading scheduler
2150 * @p: the task object
2152 * Return 0 if read access is permitted
2154 static int smack_task_getscheduler(struct task_struct *p)
2156 return smk_curacc_on_task(p, MAY_READ, __func__);
2160 * smack_task_movememory - Smack check on moving memory
2161 * @p: the task object
2163 * Return 0 if write access is permitted
2165 static int smack_task_movememory(struct task_struct *p)
2167 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2171 * smack_task_kill - Smack check on signal delivery
2172 * @p: the task object
2175 * @cred: identifies the cred to use in lieu of current's
2177 * Return 0 if write access is permitted
2180 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2181 int sig, const struct cred *cred)
2183 struct smk_audit_info ad;
2184 struct smack_known *skp;
2185 struct smack_known *tkp = smk_of_task_struct(p);
2189 return 0; /* null signal; existence test */
2191 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2192 smk_ad_setfield_u_tsk(&ad, p);
2194 * Sending a signal requires that the sender
2195 * can write the receiver.
2198 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2199 rc = smk_bu_task(p, MAY_DELIVER, rc);
2203 * If the cred isn't NULL we're dealing with some USB IO
2204 * specific behavior. This is not clean. For one thing
2205 * we can't take privilege into account.
2207 skp = smk_of_task(smack_cred(cred));
2208 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2209 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2214 * smack_task_to_inode - copy task smack into the inode blob
2215 * @p: task to copy from
2216 * @inode: inode to copy to
2218 * Sets the smack pointer in the inode security blob
2220 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2222 struct inode_smack *isp = smack_inode(inode);
2223 struct smack_known *skp = smk_of_task_struct(p);
2225 isp->smk_inode = skp;
2226 isp->smk_flags |= SMK_INODE_INSTANT;
2234 * smack_sk_alloc_security - Allocate a socket blob
2237 * @gfp_flags: memory allocation flags
2239 * Assign Smack pointers to current
2241 * Returns 0 on success, -ENOMEM is there's no memory
2243 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2245 struct smack_known *skp = smk_of_current();
2246 struct socket_smack *ssp;
2248 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2253 * Sockets created by kernel threads receive web label.
2255 if (unlikely(current->flags & PF_KTHREAD)) {
2256 ssp->smk_in = &smack_known_web;
2257 ssp->smk_out = &smack_known_web;
2262 ssp->smk_packet = NULL;
2264 sk->sk_security = ssp;
2270 * smack_sk_free_security - Free a socket blob
2273 * Clears the blob pointer
2275 static void smack_sk_free_security(struct sock *sk)
2277 #ifdef SMACK_IPV6_PORT_LABELING
2278 struct smk_port_label *spp;
2280 if (sk->sk_family == PF_INET6) {
2282 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2283 if (spp->smk_sock != sk)
2285 spp->smk_can_reuse = 1;
2291 kfree(sk->sk_security);
2295 * smack_ipv4host_label - check host based restrictions
2296 * @sip: the object end
2298 * looks for host based access restrictions
2300 * This version will only be appropriate for really small sets of single label
2301 * hosts. The caller is responsible for ensuring that the RCU read lock is
2302 * taken before calling this function.
2304 * Returns the label of the far end or NULL if it's not special.
2306 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2308 struct smk_net4addr *snp;
2309 struct in_addr *siap = &sip->sin_addr;
2311 if (siap->s_addr == 0)
2314 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2316 * we break after finding the first match because
2317 * the list is sorted from longest to shortest mask
2318 * so we have found the most specific match
2320 if (snp->smk_host.s_addr ==
2321 (siap->s_addr & snp->smk_mask.s_addr))
2322 return snp->smk_label;
2327 #if IS_ENABLED(CONFIG_IPV6)
2329 * smk_ipv6_localhost - Check for local ipv6 host address
2332 * Returns boolean true if this is the localhost address
2334 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2336 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2337 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2339 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2340 ntohs(be16p[7]) == 1)
2346 * smack_ipv6host_label - check host based restrictions
2347 * @sip: the object end
2349 * looks for host based access restrictions
2351 * This version will only be appropriate for really small sets of single label
2352 * hosts. The caller is responsible for ensuring that the RCU read lock is
2353 * taken before calling this function.
2355 * Returns the label of the far end or NULL if it's not special.
2357 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2359 struct smk_net6addr *snp;
2360 struct in6_addr *sap = &sip->sin6_addr;
2365 * It's local. Don't look for a host label.
2367 if (smk_ipv6_localhost(sip))
2370 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2372 * If the label is NULL the entry has
2373 * been renounced. Ignore it.
2375 if (snp->smk_label == NULL)
2378 * we break after finding the first match because
2379 * the list is sorted from longest to shortest mask
2380 * so we have found the most specific match
2382 for (found = 1, i = 0; i < 8; i++) {
2383 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2384 snp->smk_host.s6_addr16[i]) {
2390 return snp->smk_label;
2395 #endif /* CONFIG_IPV6 */
2398 * smack_netlabel - Set the secattr on a socket
2400 * @labeled: socket label scheme
2402 * Convert the outbound smack value (smk_out) to a
2403 * secattr and attach it to the socket.
2405 * Returns 0 on success or an error code
2407 static int smack_netlabel(struct sock *sk, int labeled)
2409 struct smack_known *skp;
2410 struct socket_smack *ssp = sk->sk_security;
2414 * Usually the netlabel code will handle changing the
2415 * packet labeling based on the label.
2416 * The case of a single label host is different, because
2417 * a single label host should never get a labeled packet
2418 * even though the label is usually associated with a packet
2422 bh_lock_sock_nested(sk);
2424 if (ssp->smk_out == smack_net_ambient ||
2425 labeled == SMACK_UNLABELED_SOCKET)
2426 netlbl_sock_delattr(sk);
2429 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2439 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2441 * @sap: the destination address
2443 * Set the correct secattr for the given socket based on the destination
2444 * address and perform any outbound access checks needed.
2446 * Returns 0 on success or an error code.
2449 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2451 struct smack_known *skp;
2454 struct smack_known *hkp;
2455 struct socket_smack *ssp = sk->sk_security;
2456 struct smk_audit_info ad;
2459 hkp = smack_ipv4host_label(sap);
2462 struct lsm_network_audit net;
2464 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2465 ad.a.u.net->family = sap->sin_family;
2466 ad.a.u.net->dport = sap->sin_port;
2467 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2469 sk_lbl = SMACK_UNLABELED_SOCKET;
2471 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2472 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2474 sk_lbl = SMACK_CIPSO_SOCKET;
2481 return smack_netlabel(sk, sk_lbl);
2484 #if IS_ENABLED(CONFIG_IPV6)
2486 * smk_ipv6_check - check Smack access
2487 * @subject: subject Smack label
2488 * @object: object Smack label
2490 * @act: the action being taken
2492 * Check an IPv6 access
2494 static int smk_ipv6_check(struct smack_known *subject,
2495 struct smack_known *object,
2496 struct sockaddr_in6 *address, int act)
2499 struct lsm_network_audit net;
2501 struct smk_audit_info ad;
2505 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2506 ad.a.u.net->family = PF_INET6;
2507 ad.a.u.net->dport = ntohs(address->sin6_port);
2508 if (act == SMK_RECEIVING)
2509 ad.a.u.net->v6info.saddr = address->sin6_addr;
2511 ad.a.u.net->v6info.daddr = address->sin6_addr;
2513 rc = smk_access(subject, object, MAY_WRITE, &ad);
2514 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2517 #endif /* CONFIG_IPV6 */
2519 #ifdef SMACK_IPV6_PORT_LABELING
2521 * smk_ipv6_port_label - Smack port access table management
2525 * Create or update the port list entry
2527 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2529 struct sock *sk = sock->sk;
2530 struct sockaddr_in6 *addr6;
2531 struct socket_smack *ssp = sock->sk->sk_security;
2532 struct smk_port_label *spp;
2533 unsigned short port = 0;
2535 if (address == NULL) {
2537 * This operation is changing the Smack information
2538 * on the bound socket. Take the changes to the port
2542 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2543 if (sk != spp->smk_sock)
2545 spp->smk_in = ssp->smk_in;
2546 spp->smk_out = ssp->smk_out;
2551 * A NULL address is only used for updating existing
2552 * bound entries. If there isn't one, it's OK.
2558 addr6 = (struct sockaddr_in6 *)address;
2559 port = ntohs(addr6->sin6_port);
2561 * This is a special case that is safely ignored.
2567 * Look for an existing port list entry.
2568 * This is an indication that a port is getting reused.
2571 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2572 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2574 if (spp->smk_can_reuse != 1) {
2578 spp->smk_port = port;
2580 spp->smk_in = ssp->smk_in;
2581 spp->smk_out = ssp->smk_out;
2582 spp->smk_can_reuse = 0;
2588 * A new port entry is required.
2590 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2594 spp->smk_port = port;
2596 spp->smk_in = ssp->smk_in;
2597 spp->smk_out = ssp->smk_out;
2598 spp->smk_sock_type = sock->type;
2599 spp->smk_can_reuse = 0;
2601 mutex_lock(&smack_ipv6_lock);
2602 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2603 mutex_unlock(&smack_ipv6_lock);
2608 * smk_ipv6_port_check - check Smack port access
2611 * @act: the action being taken
2613 * Create or update the port list entry
2615 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2618 struct smk_port_label *spp;
2619 struct socket_smack *ssp = sk->sk_security;
2620 struct smack_known *skp = NULL;
2621 unsigned short port;
2622 struct smack_known *object;
2624 if (act == SMK_RECEIVING) {
2625 skp = smack_ipv6host_label(address);
2626 object = ssp->smk_in;
2629 object = smack_ipv6host_label(address);
2633 * The other end is a single label host.
2635 if (skp != NULL && object != NULL)
2636 return smk_ipv6_check(skp, object, address, act);
2638 skp = smack_net_ambient;
2640 object = smack_net_ambient;
2643 * It's remote, so port lookup does no good.
2645 if (!smk_ipv6_localhost(address))
2646 return smk_ipv6_check(skp, object, address, act);
2649 * It's local so the send check has to have passed.
2651 if (act == SMK_RECEIVING)
2654 port = ntohs(address->sin6_port);
2656 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2657 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2659 object = spp->smk_in;
2660 if (act == SMK_CONNECTING)
2661 ssp->smk_packet = spp->smk_out;
2666 return smk_ipv6_check(skp, object, address, act);
2668 #endif /* SMACK_IPV6_PORT_LABELING */
2671 * smack_inode_setsecurity - set smack xattrs
2672 * @inode: the object
2673 * @name: attribute name
2674 * @value: attribute value
2675 * @size: size of the attribute
2678 * Sets the named attribute in the appropriate blob
2680 * Returns 0 on success, or an error code
2682 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2683 const void *value, size_t size, int flags)
2685 struct smack_known *skp;
2686 struct inode_smack *nsp = smack_inode(inode);
2687 struct socket_smack *ssp;
2688 struct socket *sock;
2691 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2694 skp = smk_import_entry(value, size);
2696 return PTR_ERR(skp);
2698 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2699 nsp->smk_inode = skp;
2700 nsp->smk_flags |= SMK_INODE_INSTANT;
2704 * The rest of the Smack xattrs are only on sockets.
2706 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2709 sock = SOCKET_I(inode);
2710 if (sock == NULL || sock->sk == NULL)
2713 ssp = sock->sk->sk_security;
2715 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2717 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2719 if (sock->sk->sk_family == PF_INET) {
2720 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2723 "Smack: \"%s\" netlbl error %d.\n",
2729 #ifdef SMACK_IPV6_PORT_LABELING
2730 if (sock->sk->sk_family == PF_INET6)
2731 smk_ipv6_port_label(sock, NULL);
2738 * smack_socket_post_create - finish socket setup
2740 * @family: protocol family
2745 * Sets the netlabel information on the socket
2747 * Returns 0 on success, and error code otherwise
2749 static int smack_socket_post_create(struct socket *sock, int family,
2750 int type, int protocol, int kern)
2752 struct socket_smack *ssp;
2754 if (sock->sk == NULL)
2758 * Sockets created by kernel threads receive web label.
2760 if (unlikely(current->flags & PF_KTHREAD)) {
2761 ssp = sock->sk->sk_security;
2762 ssp->smk_in = &smack_known_web;
2763 ssp->smk_out = &smack_known_web;
2766 if (family != PF_INET)
2769 * Set the outbound netlbl.
2771 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2775 * smack_socket_socketpair - create socket pair
2776 * @socka: one socket
2777 * @sockb: another socket
2779 * Cross reference the peer labels for SO_PEERSEC
2783 static int smack_socket_socketpair(struct socket *socka,
2784 struct socket *sockb)
2786 struct socket_smack *asp = socka->sk->sk_security;
2787 struct socket_smack *bsp = sockb->sk->sk_security;
2789 asp->smk_packet = bsp->smk_out;
2790 bsp->smk_packet = asp->smk_out;
2795 #ifdef SMACK_IPV6_PORT_LABELING
2797 * smack_socket_bind - record port binding information.
2799 * @address: the port address
2800 * @addrlen: size of the address
2802 * Records the label bound to a port.
2804 * Returns 0 on success, and error code otherwise
2806 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2809 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2810 if (addrlen < SIN6_LEN_RFC2133 ||
2811 address->sa_family != AF_INET6)
2813 smk_ipv6_port_label(sock, address);
2817 #endif /* SMACK_IPV6_PORT_LABELING */
2820 * smack_socket_connect - connect access check
2822 * @sap: the other end
2823 * @addrlen: size of sap
2825 * Verifies that a connection may be possible
2827 * Returns 0 on success, and error code otherwise
2829 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2833 #if IS_ENABLED(CONFIG_IPV6)
2834 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2836 #ifdef SMACK_IPV6_SECMARK_LABELING
2837 struct smack_known *rsp;
2838 struct socket_smack *ssp;
2841 if (sock->sk == NULL)
2844 #ifdef SMACK_IPV6_SECMARK_LABELING
2845 ssp = sock->sk->sk_security;
2848 switch (sock->sk->sk_family) {
2850 if (addrlen < sizeof(struct sockaddr_in) ||
2851 sap->sa_family != AF_INET)
2853 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2856 if (addrlen < SIN6_LEN_RFC2133 || sap->sa_family != AF_INET6)
2858 #ifdef SMACK_IPV6_SECMARK_LABELING
2859 rsp = smack_ipv6host_label(sip);
2861 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2864 #ifdef SMACK_IPV6_PORT_LABELING
2865 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
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_shmctl - 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_msgsnd - 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, struct msg_msg *msg,
3216 struct task_struct *target, long type, int mode)
3218 return smk_curacc_msq(isp, MAY_READWRITE);
3222 * smack_ipc_permission - Smack access for ipc_permission()
3223 * @ipp: the object permissions
3224 * @flag: access requested
3226 * Returns 0 if current has read and write access, error code otherwise
3228 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3230 struct smack_known **blob = smack_ipc(ipp);
3231 struct smack_known *iskp = *blob;
3232 int may = smack_flags_to_may(flag);
3233 struct smk_audit_info ad;
3237 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3238 ad.a.u.ipc_id = ipp->id;
3240 rc = smk_curacc(iskp, may, &ad);
3241 rc = smk_bu_current("svipc", iskp, may, rc);
3246 * smack_ipc_getsecid - Extract smack security id
3247 * @ipp: the object permissions
3248 * @secid: where result will be saved
3250 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3252 struct smack_known **blob = smack_ipc(ipp);
3253 struct smack_known *iskp = *blob;
3255 *secid = iskp->smk_secid;
3259 * smack_d_instantiate - Make sure the blob is correct on an inode
3260 * @opt_dentry: dentry where inode will be attached
3261 * @inode: the object
3263 * Set the inode's security blob if it hasn't been done already.
3265 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3267 struct super_block *sbp;
3268 struct superblock_smack *sbsp;
3269 struct inode_smack *isp;
3270 struct smack_known *skp;
3271 struct smack_known *ckp = smk_of_current();
3272 struct smack_known *final;
3273 char trattr[TRANS_TRUE_SIZE];
3281 isp = smack_inode(inode);
3283 mutex_lock(&isp->smk_lock);
3285 * If the inode is already instantiated
3286 * take the quick way out
3288 if (isp->smk_flags & SMK_INODE_INSTANT)
3292 sbsp = sbp->s_security;
3294 * We're going to use the superblock default label
3295 * if there's no label on the file.
3297 final = sbsp->smk_default;
3300 * If this is the root inode the superblock
3301 * may be in the process of initialization.
3302 * If that is the case use the root value out
3303 * of the superblock.
3305 if (opt_dentry->d_parent == opt_dentry) {
3306 switch (sbp->s_magic) {
3307 case CGROUP_SUPER_MAGIC:
3308 case CGROUP2_SUPER_MAGIC:
3310 * The cgroup filesystem is never mounted,
3311 * so there's no opportunity to set the mount
3314 sbsp->smk_root = &smack_known_star;
3315 sbsp->smk_default = &smack_known_star;
3316 isp->smk_inode = sbsp->smk_root;
3320 * What about shmem/tmpfs anonymous files with dentry
3321 * obtained from d_alloc_pseudo()?
3323 isp->smk_inode = smk_of_current();
3326 isp->smk_inode = smk_of_current();
3330 * Socket access is controlled by the socket
3331 * structures associated with the task involved.
3333 isp->smk_inode = &smack_known_star;
3336 isp->smk_inode = sbsp->smk_root;
3339 isp->smk_flags |= SMK_INODE_INSTANT;
3344 * This is pretty hackish.
3345 * Casey says that we shouldn't have to do
3346 * file system specific code, but it does help
3347 * with keeping it simple.
3349 switch (sbp->s_magic) {
3351 case CGROUP_SUPER_MAGIC:
3352 case CGROUP2_SUPER_MAGIC:
3354 * Casey says that it's a little embarrassing
3355 * that the smack file system doesn't do
3356 * extended attributes.
3358 * Cgroupfs is special
3360 final = &smack_known_star;
3362 case DEVPTS_SUPER_MAGIC:
3364 * devpts seems content with the label of the task.
3365 * Programs that change smack have to treat the
3370 case PROC_SUPER_MAGIC:
3372 * Casey says procfs appears not to care.
3373 * The superblock default suffices.
3378 * Device labels should come from the filesystem,
3379 * but watch out, because they're volitile,
3380 * getting recreated on every reboot.
3382 final = &smack_known_star;
3384 * If a smack value has been set we want to use it,
3385 * but since tmpfs isn't giving us the opportunity
3386 * to set mount options simulate setting the
3387 * superblock default.
3392 * This isn't an understood special case.
3393 * Get the value from the xattr.
3397 * UNIX domain sockets use lower level socket data.
3399 if (S_ISSOCK(inode->i_mode)) {
3400 final = &smack_known_star;
3404 * No xattr support means, alas, no SMACK label.
3405 * Use the aforeapplied default.
3406 * It would be curious if the label of the task
3407 * does not match that assigned.
3409 if (!(inode->i_opflags & IOP_XATTR))
3412 * Get the dentry for xattr.
3414 dp = dget(opt_dentry);
3415 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3416 if (!IS_ERR_OR_NULL(skp))
3420 * Transmuting directory
3422 if (S_ISDIR(inode->i_mode)) {
3424 * If this is a new directory and the label was
3425 * transmuted when the inode was initialized
3426 * set the transmute attribute on the directory
3427 * and mark the inode.
3429 * If there is a transmute attribute on the
3430 * directory mark the inode.
3432 if (isp->smk_flags & SMK_INODE_CHANGED) {
3433 isp->smk_flags &= ~SMK_INODE_CHANGED;
3434 rc = __vfs_setxattr(dp, inode,
3435 XATTR_NAME_SMACKTRANSMUTE,
3436 TRANS_TRUE, TRANS_TRUE_SIZE,
3439 rc = __vfs_getxattr(dp, inode,
3440 XATTR_NAME_SMACKTRANSMUTE, trattr,
3442 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3443 TRANS_TRUE_SIZE) != 0)
3447 transflag = SMK_INODE_TRANSMUTE;
3450 * Don't let the exec or mmap label be "*" or "@".
3452 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3453 if (IS_ERR(skp) || skp == &smack_known_star ||
3454 skp == &smack_known_web)
3456 isp->smk_task = skp;
3458 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3459 if (IS_ERR(skp) || skp == &smack_known_star ||
3460 skp == &smack_known_web)
3462 isp->smk_mmap = skp;
3469 isp->smk_inode = ckp;
3471 isp->smk_inode = final;
3473 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3476 mutex_unlock(&isp->smk_lock);
3481 * smack_getprocattr - Smack process attribute access
3482 * @p: the object task
3483 * @name: the name of the attribute in /proc/.../attr
3484 * @value: where to put the result
3486 * Places a copy of the task Smack into value
3488 * Returns the length of the smack label or an error code
3490 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3492 struct smack_known *skp = smk_of_task_struct(p);
3496 if (strcmp(name, "current") != 0)
3499 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3509 * smack_setprocattr - Smack process attribute setting
3510 * @name: the name of the attribute in /proc/.../attr
3511 * @value: the value to set
3512 * @size: the size of the value
3514 * Sets the Smack value of the task. Only setting self
3515 * is permitted and only with privilege
3517 * Returns the length of the smack label or an error code
3519 static int smack_setprocattr(const char *name, void *value, size_t size)
3521 struct task_smack *tsp = smack_cred(current_cred());
3523 struct smack_known *skp;
3524 struct smack_known_list_elem *sklep;
3527 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3530 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3533 if (strcmp(name, "current") != 0)
3536 skp = smk_import_entry(value, size);
3538 return PTR_ERR(skp);
3541 * No process is ever allowed the web ("@") label
3542 * and the star ("*") label.
3544 if (skp == &smack_known_web || skp == &smack_known_star)
3547 if (!smack_privileged(CAP_MAC_ADMIN)) {
3549 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3550 if (sklep->smk_label == skp) {
3558 new = prepare_creds();
3562 tsp = smack_cred(new);
3563 tsp->smk_task = skp;
3565 * process can change its label only once
3567 smk_destroy_label_list(&tsp->smk_relabel);
3574 * smack_unix_stream_connect - Smack access on UDS
3576 * @other: the other sock
3579 * Return 0 if a subject with the smack of sock could access
3580 * an object with the smack of other, otherwise an error code
3582 static int smack_unix_stream_connect(struct sock *sock,
3583 struct sock *other, struct sock *newsk)
3585 struct smack_known *skp;
3586 struct smack_known *okp;
3587 struct socket_smack *ssp = sock->sk_security;
3588 struct socket_smack *osp = other->sk_security;
3589 struct socket_smack *nsp = newsk->sk_security;
3590 struct smk_audit_info ad;
3593 struct lsm_network_audit net;
3596 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3600 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3601 smk_ad_setfield_u_net_sk(&ad, other);
3603 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3604 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3608 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3609 rc = smk_bu_note("UDS connect", okp, skp,
3615 * Cross reference the peer labels for SO_PEERSEC.
3618 nsp->smk_packet = ssp->smk_out;
3619 ssp->smk_packet = osp->smk_out;
3626 * smack_unix_may_send - Smack access on UDS
3628 * @other: the other socket
3630 * Return 0 if a subject with the smack of sock could access
3631 * an object with the smack of other, otherwise an error code
3633 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3635 struct socket_smack *ssp = sock->sk->sk_security;
3636 struct socket_smack *osp = other->sk->sk_security;
3637 struct smk_audit_info ad;
3641 struct lsm_network_audit net;
3643 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3644 smk_ad_setfield_u_net_sk(&ad, other->sk);
3647 if (smack_privileged(CAP_MAC_OVERRIDE))
3650 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3651 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3656 * smack_socket_sendmsg - Smack check based on destination host
3659 * @size: the size of the message
3661 * Return 0 if the current subject can write to the destination host.
3662 * For IPv4 this is only a question if the destination is a single label host.
3663 * For IPv6 this is a check against the label of the port.
3665 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3668 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3669 #if IS_ENABLED(CONFIG_IPV6)
3670 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3672 #ifdef SMACK_IPV6_SECMARK_LABELING
3673 struct socket_smack *ssp = sock->sk->sk_security;
3674 struct smack_known *rsp;
3679 * Perfectly reasonable for this to be NULL
3684 switch (sock->sk->sk_family) {
3686 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3687 sip->sin_family != AF_INET)
3689 rc = smack_netlabel_send(sock->sk, sip);
3691 #if IS_ENABLED(CONFIG_IPV6)
3693 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3694 sap->sin6_family != AF_INET6)
3696 #ifdef SMACK_IPV6_SECMARK_LABELING
3697 rsp = smack_ipv6host_label(sap);
3699 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3702 #ifdef SMACK_IPV6_PORT_LABELING
3703 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3705 #endif /* IS_ENABLED(CONFIG_IPV6) */
3712 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3713 * @sap: netlabel secattr
3714 * @ssp: socket security information
3716 * Returns a pointer to a Smack label entry found on the label list.
3718 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3719 struct socket_smack *ssp)
3721 struct smack_known *skp;
3726 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3728 * Looks like a CIPSO packet.
3729 * If there are flags but no level netlabel isn't
3730 * behaving the way we expect it to.
3732 * Look it up in the label table
3733 * Without guidance regarding the smack value
3734 * for the packet fall back on the network
3738 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3739 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3742 * Compare the catsets. Use the netlbl APIs.
3744 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3745 if ((skp->smk_netlabel.flags &
3746 NETLBL_SECATTR_MLS_CAT) == 0)
3750 for (acat = -1, kcat = -1; acat == kcat; ) {
3751 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3753 kcat = netlbl_catmap_walk(
3754 skp->smk_netlabel.attr.mls.cat,
3756 if (acat < 0 || kcat < 0)
3769 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3770 return &smack_known_web;
3771 return &smack_known_star;
3773 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3775 * Looks like a fallback, which gives us a secid.
3777 return smack_from_secid(sap->attr.secid);
3779 * Without guidance regarding the smack value
3780 * for the packet fall back on the network
3783 return smack_net_ambient;
3786 #if IS_ENABLED(CONFIG_IPV6)
3787 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3791 int proto = -EINVAL;
3792 struct ipv6hdr _ipv6h;
3793 struct ipv6hdr *ip6;
3795 struct tcphdr _tcph, *th;
3796 struct udphdr _udph, *uh;
3797 struct dccp_hdr _dccph, *dh;
3801 offset = skb_network_offset(skb);
3802 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3805 sip->sin6_addr = ip6->saddr;
3807 nexthdr = ip6->nexthdr;
3808 offset += sizeof(_ipv6h);
3809 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3816 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3818 sip->sin6_port = th->source;
3821 case IPPROTO_UDPLITE:
3822 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3824 sip->sin6_port = uh->source;
3827 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3829 sip->sin6_port = dh->dccph_sport;
3834 #endif /* CONFIG_IPV6 */
3837 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3841 * Returns 0 if the packet should be delivered, an error code otherwise
3843 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3845 struct netlbl_lsm_secattr secattr;
3846 struct socket_smack *ssp = sk->sk_security;
3847 struct smack_known *skp = NULL;
3849 struct smk_audit_info ad;
3850 u16 family = sk->sk_family;
3852 struct lsm_network_audit net;
3854 #if IS_ENABLED(CONFIG_IPV6)
3855 struct sockaddr_in6 sadd;
3858 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3860 #endif /* CONFIG_IPV6 */
3864 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3866 * If there is a secmark use it rather than the CIPSO label.
3867 * If there is no secmark fall back to CIPSO.
3868 * The secmark is assumed to reflect policy better.
3870 if (skb && skb->secmark != 0) {
3871 skp = smack_from_secid(skb->secmark);
3874 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3876 * Translate what netlabel gave us.
3878 netlbl_secattr_init(&secattr);
3880 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3882 skp = smack_from_secattr(&secattr, ssp);
3884 skp = smack_net_ambient;
3886 netlbl_secattr_destroy(&secattr);
3888 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3892 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3893 ad.a.u.net->family = family;
3894 ad.a.u.net->netif = skb->skb_iif;
3895 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3898 * Receiving a packet requires that the other end
3899 * be able to write here. Read access is not required.
3900 * This is the simplist possible security model
3903 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3904 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3907 netlbl_skbuff_err(skb, family, rc, 0);
3909 #if IS_ENABLED(CONFIG_IPV6)
3911 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3912 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3913 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3915 #ifdef SMACK_IPV6_SECMARK_LABELING
3916 if (skb && skb->secmark != 0)
3917 skp = smack_from_secid(skb->secmark);
3918 else if (smk_ipv6_localhost(&sadd))
3921 skp = smack_ipv6host_label(&sadd);
3923 skp = smack_net_ambient;
3927 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3928 ad.a.u.net->family = family;
3929 ad.a.u.net->netif = skb->skb_iif;
3930 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3931 #endif /* CONFIG_AUDIT */
3932 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3933 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3935 #endif /* SMACK_IPV6_SECMARK_LABELING */
3936 #ifdef SMACK_IPV6_PORT_LABELING
3937 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3938 #endif /* SMACK_IPV6_PORT_LABELING */
3940 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3941 ICMPV6_ADM_PROHIBITED, 0);
3943 #endif /* CONFIG_IPV6 */
3950 * smack_socket_getpeersec_stream - pull in packet label
3952 * @optval: user's destination
3953 * @optlen: size thereof
3956 * returns zero on success, an error code otherwise
3958 static int smack_socket_getpeersec_stream(struct socket *sock,
3959 char __user *optval,
3960 int __user *optlen, unsigned len)
3962 struct socket_smack *ssp;
3967 ssp = sock->sk->sk_security;
3968 if (ssp->smk_packet != NULL) {
3969 rcp = ssp->smk_packet->smk_known;
3970 slen = strlen(rcp) + 1;
3975 else if (copy_to_user(optval, rcp, slen) != 0)
3978 if (put_user(slen, optlen) != 0)
3986 * smack_socket_getpeersec_dgram - pull in packet label
3987 * @sock: the peer socket
3989 * @secid: pointer to where to put the secid of the packet
3991 * Sets the netlabel socket state on sk from parent
3993 static int smack_socket_getpeersec_dgram(struct socket *sock,
3994 struct sk_buff *skb, u32 *secid)
3997 struct netlbl_lsm_secattr secattr;
3998 struct socket_smack *ssp = NULL;
3999 struct smack_known *skp;
4000 int family = PF_UNSPEC;
4001 u32 s = 0; /* 0 is the invalid secid */
4005 if (skb->protocol == htons(ETH_P_IP))
4007 #if IS_ENABLED(CONFIG_IPV6)
4008 else if (skb->protocol == htons(ETH_P_IPV6))
4010 #endif /* CONFIG_IPV6 */
4012 if (family == PF_UNSPEC && sock != NULL)
4013 family = sock->sk->sk_family;
4017 ssp = sock->sk->sk_security;
4018 s = ssp->smk_out->smk_secid;
4021 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4027 * Translate what netlabel gave us.
4029 if (sock != NULL && sock->sk != NULL)
4030 ssp = sock->sk->sk_security;
4031 netlbl_secattr_init(&secattr);
4032 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4034 skp = smack_from_secattr(&secattr, ssp);
4037 netlbl_secattr_destroy(&secattr);
4040 #ifdef SMACK_IPV6_SECMARK_LABELING
4052 * smack_sock_graft - Initialize a newly created socket with an existing sock
4054 * @parent: parent socket
4056 * Set the smk_{in,out} state of an existing sock based on the process that
4057 * is creating the new socket.
4059 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4061 struct socket_smack *ssp;
4062 struct smack_known *skp = smk_of_current();
4065 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4068 ssp = sk->sk_security;
4071 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4075 * smack_inet_conn_request - Smack access check on connect
4076 * @sk: socket involved
4080 * Returns 0 if a task with the packet label could write to
4081 * the socket, otherwise an error code
4083 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4084 struct request_sock *req)
4086 u16 family = sk->sk_family;
4087 struct smack_known *skp;
4088 struct socket_smack *ssp = sk->sk_security;
4089 struct netlbl_lsm_secattr secattr;
4090 struct sockaddr_in addr;
4092 struct smack_known *hskp;
4094 struct smk_audit_info ad;
4096 struct lsm_network_audit net;
4099 #if IS_ENABLED(CONFIG_IPV6)
4100 if (family == PF_INET6) {
4102 * Handle mapped IPv4 packets arriving
4103 * via IPv6 sockets. Don't set up netlabel
4104 * processing on IPv6.
4106 if (skb->protocol == htons(ETH_P_IP))
4111 #endif /* CONFIG_IPV6 */
4113 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4115 * If there is a secmark use it rather than the CIPSO label.
4116 * If there is no secmark fall back to CIPSO.
4117 * The secmark is assumed to reflect policy better.
4119 if (skb && skb->secmark != 0) {
4120 skp = smack_from_secid(skb->secmark);
4123 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4125 netlbl_secattr_init(&secattr);
4126 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4128 skp = smack_from_secattr(&secattr, ssp);
4130 skp = &smack_known_huh;
4131 netlbl_secattr_destroy(&secattr);
4133 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4138 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4139 ad.a.u.net->family = family;
4140 ad.a.u.net->netif = skb->skb_iif;
4141 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4144 * Receiving a packet requires that the other end be able to write
4145 * here. Read access is not required.
4147 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4148 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4153 * Save the peer's label in the request_sock so we can later setup
4154 * smk_packet in the child socket so that SO_PEERCRED can report it.
4156 req->peer_secid = skp->smk_secid;
4159 * We need to decide if we want to label the incoming connection here
4160 * if we do we only need to label the request_sock and the stack will
4161 * propagate the wire-label to the sock when it is created.
4164 addr.sin_addr.s_addr = hdr->saddr;
4166 hskp = smack_ipv4host_label(&addr);
4170 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4172 netlbl_req_delattr(req);
4178 * smack_inet_csk_clone - Copy the connection information to the new socket
4179 * @sk: the new socket
4180 * @req: the connection's request_sock
4182 * Transfer the connection's peer label to the newly created socket.
4184 static void smack_inet_csk_clone(struct sock *sk,
4185 const struct request_sock *req)
4187 struct socket_smack *ssp = sk->sk_security;
4188 struct smack_known *skp;
4190 if (req->peer_secid != 0) {
4191 skp = smack_from_secid(req->peer_secid);
4192 ssp->smk_packet = skp;
4194 ssp->smk_packet = NULL;
4198 * Key management security hooks
4200 * Casey has not tested key support very heavily.
4201 * The permission check is most likely too restrictive.
4202 * If you care about keys please have a look.
4207 * smack_key_alloc - Set the key security blob
4209 * @cred: the credentials to use
4212 * No allocation required
4216 static int smack_key_alloc(struct key *key, const struct cred *cred,
4217 unsigned long flags)
4219 struct smack_known *skp = smk_of_task(smack_cred(cred));
4221 key->security = skp;
4226 * smack_key_free - Clear the key security blob
4229 * Clear the blob pointer
4231 static void smack_key_free(struct key *key)
4233 key->security = NULL;
4237 * smack_key_permission - Smack access on a key
4238 * @key_ref: gets to the object
4239 * @cred: the credentials to use
4240 * @perm: requested key permissions
4242 * Return 0 if the task has read and write to the object,
4243 * an error code otherwise
4245 static int smack_key_permission(key_ref_t key_ref,
4246 const struct cred *cred, unsigned perm)
4249 struct smk_audit_info ad;
4250 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4255 * Validate requested permissions
4257 if (perm & ~KEY_NEED_ALL)
4260 keyp = key_ref_to_ptr(key_ref);
4264 * If the key hasn't been initialized give it access so that
4267 if (keyp->security == NULL)
4270 * This should not occur
4275 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4279 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4280 ad.a.u.key_struct.key = keyp->serial;
4281 ad.a.u.key_struct.key_desc = keyp->description;
4283 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4284 request |= MAY_READ;
4285 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4286 request |= MAY_WRITE;
4287 rc = smk_access(tkp, keyp->security, request, &ad);
4288 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4293 * smack_key_getsecurity - Smack label tagging the key
4294 * @key points to the key to be queried
4295 * @_buffer points to a pointer that should be set to point to the
4296 * resulting string (if no label or an error occurs).
4297 * Return the length of the string (including terminating NUL) or -ve if
4299 * May also return 0 (and a NULL buffer pointer) if there is no label.
4301 static int smack_key_getsecurity(struct key *key, char **_buffer)
4303 struct smack_known *skp = key->security;
4307 if (key->security == NULL) {
4312 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4315 length = strlen(copy) + 1;
4321 #endif /* CONFIG_KEYS */
4326 * Audit requires a unique representation of each Smack specific
4327 * rule. This unique representation is used to distinguish the
4328 * object to be audited from remaining kernel objects and also
4329 * works as a glue between the audit hooks.
4331 * Since repository entries are added but never deleted, we'll use
4332 * the smack_known label address related to the given audit rule as
4333 * the needed unique representation. This also better fits the smack
4334 * model where nearly everything is a label.
4339 * smack_audit_rule_init - Initialize a smack audit rule
4340 * @field: audit rule fields given from user-space (audit.h)
4341 * @op: required testing operator (=, !=, >, <, ...)
4342 * @rulestr: smack label to be audited
4343 * @vrule: pointer to save our own audit rule representation
4345 * Prepare to audit cases where (@field @op @rulestr) is true.
4346 * The label to be audited is created if necessay.
4348 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4350 struct smack_known *skp;
4351 char **rule = (char **)vrule;
4354 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4357 if (op != Audit_equal && op != Audit_not_equal)
4360 skp = smk_import_entry(rulestr, 0);
4362 return PTR_ERR(skp);
4364 *rule = skp->smk_known;
4370 * smack_audit_rule_known - Distinguish Smack audit rules
4371 * @krule: rule of interest, in Audit kernel representation format
4373 * This is used to filter Smack rules from remaining Audit ones.
4374 * If it's proved that this rule belongs to us, the
4375 * audit_rule_match hook will be called to do the final judgement.
4377 static int smack_audit_rule_known(struct audit_krule *krule)
4379 struct audit_field *f;
4382 for (i = 0; i < krule->field_count; i++) {
4383 f = &krule->fields[i];
4385 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4393 * smack_audit_rule_match - Audit given object ?
4394 * @secid: security id for identifying the object to test
4395 * @field: audit rule flags given from user-space
4396 * @op: required testing operator
4397 * @vrule: smack internal rule presentation
4399 * The core Audit hook. It's used to take the decision of
4400 * whether to audit or not to audit a given object.
4402 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4404 struct smack_known *skp;
4407 if (unlikely(!rule)) {
4408 WARN_ONCE(1, "Smack: missing rule\n");
4412 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4415 skp = smack_from_secid(secid);
4418 * No need to do string comparisons. If a match occurs,
4419 * both pointers will point to the same smack_known
4422 if (op == Audit_equal)
4423 return (rule == skp->smk_known);
4424 if (op == Audit_not_equal)
4425 return (rule != skp->smk_known);
4431 * There is no need for a smack_audit_rule_free hook.
4432 * No memory was allocated.
4435 #endif /* CONFIG_AUDIT */
4438 * smack_ismaclabel - check if xattr @name references a smack MAC label
4439 * @name: Full xattr name to check.
4441 static int smack_ismaclabel(const char *name)
4443 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4448 * smack_secid_to_secctx - return the smack label for a secid
4449 * @secid: incoming integer
4450 * @secdata: destination
4451 * @seclen: how long it is
4453 * Exists for networking code.
4455 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4457 struct smack_known *skp = smack_from_secid(secid);
4460 *secdata = skp->smk_known;
4461 *seclen = strlen(skp->smk_known);
4466 * smack_secctx_to_secid - return the secid for a smack label
4467 * @secdata: smack label
4468 * @seclen: how long result is
4469 * @secid: outgoing integer
4471 * Exists for audit and networking code.
4473 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4475 struct smack_known *skp = smk_find_entry(secdata);
4478 *secid = skp->smk_secid;
4485 * There used to be a smack_release_secctx hook
4486 * that did nothing back when hooks were in a vector.
4487 * Now that there's a list such a hook adds cost.
4490 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4492 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4495 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4497 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4500 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4502 struct smack_known *skp = smk_of_inode(inode);
4504 *ctx = skp->smk_known;
4505 *ctxlen = strlen(skp->smk_known);
4509 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4512 struct task_smack *tsp;
4513 struct smack_known *skp;
4514 struct inode_smack *isp;
4515 struct cred *new_creds = *new;
4517 if (new_creds == NULL) {
4518 new_creds = prepare_creds();
4519 if (new_creds == NULL)
4523 tsp = smack_cred(new_creds);
4526 * Get label from overlay inode and set it in create_sid
4528 isp = smack_inode(d_inode(dentry->d_parent));
4529 skp = isp->smk_inode;
4530 tsp->smk_task = skp;
4535 static int smack_inode_copy_up_xattr(const char *name)
4538 * Return 1 if this is the smack access Smack attribute.
4540 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4546 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4548 const struct cred *old,
4551 struct task_smack *otsp = smack_cred(old);
4552 struct task_smack *ntsp = smack_cred(new);
4553 struct inode_smack *isp;
4557 * Use the process credential unless all of
4558 * the transmuting criteria are met
4560 ntsp->smk_task = otsp->smk_task;
4563 * the attribute of the containing directory
4565 isp = smack_inode(d_inode(dentry->d_parent));
4567 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4569 may = smk_access_entry(otsp->smk_task->smk_known,
4570 isp->smk_inode->smk_known,
4571 &otsp->smk_task->smk_rules);
4575 * If the directory is transmuting and the rule
4576 * providing access is transmuting use the containing
4577 * directory label instead of the process label.
4579 if (may > 0 && (may & MAY_TRANSMUTE))
4580 ntsp->smk_task = isp->smk_inode;
4585 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4586 .lbs_cred = sizeof(struct task_smack),
4587 .lbs_file = sizeof(struct smack_known *),
4588 .lbs_inode = sizeof(struct inode_smack),
4589 .lbs_ipc = sizeof(struct smack_known *),
4590 .lbs_msg_msg = sizeof(struct smack_known *),
4593 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4594 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4595 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4596 LSM_HOOK_INIT(syslog, smack_syslog),
4598 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4599 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4601 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4602 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4603 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4604 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4605 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4606 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4608 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4610 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4611 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4612 LSM_HOOK_INIT(inode_link, smack_inode_link),
4613 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4614 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4615 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4616 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4617 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4618 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4619 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4620 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4621 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4622 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4623 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4624 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4625 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4626 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4628 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4629 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4630 LSM_HOOK_INIT(file_lock, smack_file_lock),
4631 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4632 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4633 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4634 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4635 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4636 LSM_HOOK_INIT(file_receive, smack_file_receive),
4638 LSM_HOOK_INIT(file_open, smack_file_open),
4640 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4641 LSM_HOOK_INIT(cred_free, smack_cred_free),
4642 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4643 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4644 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4645 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4646 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4647 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4648 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4649 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4650 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4651 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4652 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4653 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4654 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4655 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4656 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4657 LSM_HOOK_INIT(task_kill, smack_task_kill),
4658 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4660 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4661 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4663 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4665 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4666 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4667 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4668 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4669 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4671 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4672 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4673 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4674 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4676 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4677 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4678 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4679 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4681 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4683 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4684 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4686 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4687 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4689 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4690 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4691 #ifdef SMACK_IPV6_PORT_LABELING
4692 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4694 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4695 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4696 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4697 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4698 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4699 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4700 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4701 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4702 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4703 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4705 /* key management security hooks */
4707 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4708 LSM_HOOK_INIT(key_free, smack_key_free),
4709 LSM_HOOK_INIT(key_permission, smack_key_permission),
4710 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4711 #endif /* CONFIG_KEYS */
4715 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4716 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4717 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4718 #endif /* CONFIG_AUDIT */
4720 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4721 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4722 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4723 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4724 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4725 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4726 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4727 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4728 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4732 static __init void init_smack_known_list(void)
4735 * Initialize rule list locks
4737 mutex_init(&smack_known_huh.smk_rules_lock);
4738 mutex_init(&smack_known_hat.smk_rules_lock);
4739 mutex_init(&smack_known_floor.smk_rules_lock);
4740 mutex_init(&smack_known_star.smk_rules_lock);
4741 mutex_init(&smack_known_web.smk_rules_lock);
4743 * Initialize rule lists
4745 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4746 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4747 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4748 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4749 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4751 * Create the known labels list
4753 smk_insert_entry(&smack_known_huh);
4754 smk_insert_entry(&smack_known_hat);
4755 smk_insert_entry(&smack_known_star);
4756 smk_insert_entry(&smack_known_floor);
4757 smk_insert_entry(&smack_known_web);
4761 * smack_init - initialize the smack system
4763 * Returns 0 on success, -ENOMEM is there's no memory
4765 static __init int smack_init(void)
4767 struct cred *cred = (struct cred *) current->cred;
4768 struct task_smack *tsp;
4770 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4771 if (!smack_inode_cache)
4774 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4775 if (!smack_rule_cache) {
4776 kmem_cache_destroy(smack_inode_cache);
4781 * Set the security state for the initial task.
4783 tsp = smack_cred(cred);
4784 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4789 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4792 pr_info("Smack: Initializing.\n");
4793 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4794 pr_info("Smack: Netfilter enabled.\n");
4796 #ifdef SMACK_IPV6_PORT_LABELING
4797 pr_info("Smack: IPv6 port labeling enabled.\n");
4799 #ifdef SMACK_IPV6_SECMARK_LABELING
4800 pr_info("Smack: IPv6 Netfilter enabled.\n");
4803 /* initialize the smack_known_list */
4804 init_smack_known_list();
4810 * Smack requires early initialization in order to label
4811 * all processes and objects when they are created.
4813 DEFINE_LSM(smack) = {
4815 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4816 .blobs = &smack_blob_sizes,