2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/dccp.h>
31 #include <linux/icmpv6.h>
32 #include <linux/slab.h>
33 #include <linux/mutex.h>
34 #include <linux/pipe_fs_i.h>
35 #include <net/cipso_ipv4.h>
38 #include <linux/audit.h>
39 #include <linux/magic.h>
40 #include <linux/dcache.h>
41 #include <linux/personality.h>
42 #include <linux/msg.h>
43 #include <linux/shm.h>
44 #include <linux/binfmts.h>
45 #include <linux/parser.h>
46 #include <linux/fs_context.h>
47 #include <linux/fs_parser.h>
50 #define TRANS_TRUE "TRUE"
51 #define TRANS_TRUE_SIZE 4
53 #define SMK_CONNECTING 0
54 #define SMK_RECEIVING 1
57 #ifdef SMACK_IPV6_PORT_LABELING
58 DEFINE_MUTEX(smack_ipv6_lock);
59 static LIST_HEAD(smk_ipv6_port_list);
61 static struct kmem_cache *smack_inode_cache;
62 struct kmem_cache *smack_rule_cache;
65 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
70 } smk_mount_opts[] = {
71 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
72 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
76 static int match_opt_prefix(char *s, int l, char **arg)
80 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
81 size_t len = smk_mount_opts[i].len;
82 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
84 if (len == l || s[len] != '=')
87 return smk_mount_opts[i].opt;
92 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
93 static char *smk_bu_mess[] = {
94 "Bringup Error", /* Unused */
95 "Bringup", /* SMACK_BRINGUP_ALLOW */
96 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
97 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
100 static void smk_bu_mode(int mode, char *s)
106 if (mode & MAY_WRITE)
110 if (mode & MAY_APPEND)
112 if (mode & MAY_TRANSMUTE)
122 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
123 static int smk_bu_note(char *note, struct smack_known *sskp,
124 struct smack_known *oskp, int mode, int rc)
126 char acc[SMK_NUM_ACCESS_TYPE + 1];
130 if (rc > SMACK_UNCONFINED_OBJECT)
133 smk_bu_mode(mode, acc);
134 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
135 sskp->smk_known, oskp->smk_known, acc, note);
139 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
142 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
143 static int smk_bu_current(char *note, struct smack_known *oskp,
146 struct task_smack *tsp = smack_cred(current_cred());
147 char acc[SMK_NUM_ACCESS_TYPE + 1];
151 if (rc > SMACK_UNCONFINED_OBJECT)
154 smk_bu_mode(mode, acc);
155 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
156 tsp->smk_task->smk_known, oskp->smk_known,
157 acc, current->comm, note);
161 #define smk_bu_current(note, oskp, mode, RC) (RC)
164 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
165 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
167 struct task_smack *tsp = smack_cred(current_cred());
168 struct smack_known *smk_task = smk_of_task_struct(otp);
169 char acc[SMK_NUM_ACCESS_TYPE + 1];
173 if (rc > SMACK_UNCONFINED_OBJECT)
176 smk_bu_mode(mode, acc);
177 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
178 tsp->smk_task->smk_known, smk_task->smk_known, acc,
179 current->comm, otp->comm);
183 #define smk_bu_task(otp, mode, RC) (RC)
186 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
187 static int smk_bu_inode(struct inode *inode, int mode, int rc)
189 struct task_smack *tsp = smack_cred(current_cred());
190 struct inode_smack *isp = smack_inode(inode);
191 char acc[SMK_NUM_ACCESS_TYPE + 1];
193 if (isp->smk_flags & SMK_INODE_IMPURE)
194 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
195 inode->i_sb->s_id, inode->i_ino, current->comm);
199 if (rc > SMACK_UNCONFINED_OBJECT)
201 if (rc == SMACK_UNCONFINED_SUBJECT &&
202 (mode & (MAY_WRITE | MAY_APPEND)))
203 isp->smk_flags |= SMK_INODE_IMPURE;
205 smk_bu_mode(mode, acc);
207 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
208 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
209 inode->i_sb->s_id, inode->i_ino, current->comm);
213 #define smk_bu_inode(inode, mode, RC) (RC)
216 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
217 static int smk_bu_file(struct file *file, int mode, int rc)
219 struct task_smack *tsp = smack_cred(current_cred());
220 struct smack_known *sskp = tsp->smk_task;
221 struct inode *inode = file_inode(file);
222 struct inode_smack *isp = smack_inode(inode);
223 char acc[SMK_NUM_ACCESS_TYPE + 1];
225 if (isp->smk_flags & SMK_INODE_IMPURE)
226 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
227 inode->i_sb->s_id, inode->i_ino, current->comm);
231 if (rc > SMACK_UNCONFINED_OBJECT)
234 smk_bu_mode(mode, acc);
235 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
236 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
237 inode->i_sb->s_id, inode->i_ino, file,
242 #define smk_bu_file(file, mode, RC) (RC)
245 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
246 static int smk_bu_credfile(const struct cred *cred, struct file *file,
249 struct task_smack *tsp = smack_cred(cred);
250 struct smack_known *sskp = tsp->smk_task;
251 struct inode *inode = file_inode(file);
252 struct inode_smack *isp = smack_inode(inode);
253 char acc[SMK_NUM_ACCESS_TYPE + 1];
255 if (isp->smk_flags & SMK_INODE_IMPURE)
256 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
257 inode->i_sb->s_id, inode->i_ino, current->comm);
261 if (rc > SMACK_UNCONFINED_OBJECT)
264 smk_bu_mode(mode, acc);
265 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
266 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
267 inode->i_sb->s_id, inode->i_ino, file,
272 #define smk_bu_credfile(cred, file, mode, RC) (RC)
276 * smk_fetch - Fetch the smack label from a file.
277 * @name: type of the label (attribute)
278 * @ip: a pointer to the inode
279 * @dp: a pointer to the dentry
281 * Returns a pointer to the master list entry for the Smack label,
282 * NULL if there was no label to fetch, or an error code.
284 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
289 struct smack_known *skp = NULL;
291 if (!(ip->i_opflags & IOP_XATTR))
292 return ERR_PTR(-EOPNOTSUPP);
294 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
296 return ERR_PTR(-ENOMEM);
298 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
304 skp = smk_import_entry(buffer, rc);
312 * init_inode_smack - initialize an inode security blob
313 * @isp: the blob to initialize
314 * @skp: a pointer to the Smack label entry to use in the blob
317 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
319 struct inode_smack *isp = smack_inode(inode);
321 isp->smk_inode = skp;
323 mutex_init(&isp->smk_lock);
327 * init_task_smack - initialize a task security blob
328 * @tsp: blob to initialize
329 * @task: a pointer to the Smack label for the running task
330 * @forked: a pointer to the Smack label for the forked task
333 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
334 struct smack_known *forked)
336 tsp->smk_task = task;
337 tsp->smk_forked = forked;
338 INIT_LIST_HEAD(&tsp->smk_rules);
339 INIT_LIST_HEAD(&tsp->smk_relabel);
340 mutex_init(&tsp->smk_rules_lock);
344 * smk_copy_rules - copy a rule set
345 * @nhead: new rules header pointer
346 * @ohead: old rules header pointer
347 * @gfp: type of the memory for the allocation
349 * Returns 0 on success, -ENOMEM on error
351 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
354 struct smack_rule *nrp;
355 struct smack_rule *orp;
358 list_for_each_entry_rcu(orp, ohead, list) {
359 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
365 list_add_rcu(&nrp->list, nhead);
371 * smk_copy_relabel - copy smk_relabel labels list
372 * @nhead: new rules header pointer
373 * @ohead: old rules header pointer
374 * @gfp: type of the memory for the allocation
376 * Returns 0 on success, -ENOMEM on error
378 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
381 struct smack_known_list_elem *nklep;
382 struct smack_known_list_elem *oklep;
384 list_for_each_entry(oklep, ohead, list) {
385 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
387 smk_destroy_label_list(nhead);
390 nklep->smk_label = oklep->smk_label;
391 list_add(&nklep->list, nhead);
398 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
399 * @mode - input mode in form of PTRACE_MODE_*
401 * Returns a converted MAY_* mode usable by smack rules
403 static inline unsigned int smk_ptrace_mode(unsigned int mode)
405 if (mode & PTRACE_MODE_ATTACH)
406 return MAY_READWRITE;
407 if (mode & PTRACE_MODE_READ)
414 * smk_ptrace_rule_check - helper for ptrace access
415 * @tracer: tracer process
416 * @tracee_known: label entry of the process that's about to be traced
417 * @mode: ptrace attachment mode (PTRACE_MODE_*)
418 * @func: name of the function that called us, used for audit
420 * Returns 0 on access granted, -error on error
422 static int smk_ptrace_rule_check(struct task_struct *tracer,
423 struct smack_known *tracee_known,
424 unsigned int mode, const char *func)
427 struct smk_audit_info ad, *saip = NULL;
428 struct task_smack *tsp;
429 struct smack_known *tracer_known;
430 const struct cred *tracercred;
432 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
433 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
434 smk_ad_setfield_u_tsk(&ad, tracer);
439 tracercred = __task_cred(tracer);
440 tsp = smack_cred(tracercred);
441 tracer_known = smk_of_task(tsp);
443 if ((mode & PTRACE_MODE_ATTACH) &&
444 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
445 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
446 if (tracer_known->smk_known == tracee_known->smk_known)
448 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
450 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
456 smack_log(tracer_known->smk_known,
457 tracee_known->smk_known,
464 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
465 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
473 * We he, that is fun!
477 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
478 * @ctp: child task pointer
479 * @mode: ptrace attachment mode (PTRACE_MODE_*)
481 * Returns 0 if access is OK, an error code otherwise
483 * Do the capability checks.
485 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
487 struct smack_known *skp;
489 skp = smk_of_task_struct(ctp);
491 return smk_ptrace_rule_check(current, skp, mode, __func__);
495 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
496 * @ptp: parent task pointer
498 * Returns 0 if access is OK, an error code otherwise
500 * Do the capability checks, and require PTRACE_MODE_ATTACH.
502 static int smack_ptrace_traceme(struct task_struct *ptp)
505 struct smack_known *skp;
507 skp = smk_of_task(smack_cred(current_cred()));
509 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
514 * smack_syslog - Smack approval on syslog
515 * @type: message type
517 * Returns 0 on success, error code otherwise.
519 static int smack_syslog(int typefrom_file)
522 struct smack_known *skp = smk_of_current();
524 if (smack_privileged(CAP_MAC_OVERRIDE))
527 if (smack_syslog_label != NULL && smack_syslog_label != skp)
538 * smack_sb_alloc_security - allocate a superblock blob
539 * @sb: the superblock getting the blob
541 * Returns 0 on success or -ENOMEM on error.
543 static int smack_sb_alloc_security(struct super_block *sb)
545 struct superblock_smack *sbsp;
547 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
552 sbsp->smk_root = &smack_known_floor;
553 sbsp->smk_default = &smack_known_floor;
554 sbsp->smk_floor = &smack_known_floor;
555 sbsp->smk_hat = &smack_known_hat;
557 * SMK_SB_INITIALIZED will be zero from kzalloc.
559 sb->s_security = sbsp;
565 * smack_sb_free_security - free a superblock blob
566 * @sb: the superblock getting the blob
569 static void smack_sb_free_security(struct super_block *sb)
571 kfree(sb->s_security);
572 sb->s_security = NULL;
575 struct smack_mnt_opts {
576 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
579 static void smack_free_mnt_opts(void *mnt_opts)
581 struct smack_mnt_opts *opts = mnt_opts;
582 kfree(opts->fsdefault);
583 kfree(opts->fsfloor);
586 kfree(opts->fstransmute);
590 static int smack_add_opt(int token, const char *s, void **mnt_opts)
592 struct smack_mnt_opts *opts = *mnt_opts;
595 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
624 case Opt_fstransmute:
625 if (opts->fstransmute)
627 opts->fstransmute = s;
633 pr_warn("Smack: duplicate mount options\n");
638 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
639 * @fc: The new filesystem context.
640 * @src_fc: The source filesystem context being duplicated.
642 * Returns 0 on success or -ENOMEM on error.
644 static int smack_fs_context_dup(struct fs_context *fc,
645 struct fs_context *src_fc)
647 struct smack_mnt_opts *dst, *src = src_fc->security;
652 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
657 if (src->fsdefault) {
658 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
663 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
668 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
673 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
677 if (src->fstransmute) {
678 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
679 if (!dst->fstransmute)
685 static const struct fs_parameter_spec smack_param_specs[] = {
686 fsparam_string("smackfsdef", Opt_fsdefault),
687 fsparam_string("smackfsdefault", Opt_fsdefault),
688 fsparam_string("smackfsfloor", Opt_fsfloor),
689 fsparam_string("smackfshat", Opt_fshat),
690 fsparam_string("smackfsroot", Opt_fsroot),
691 fsparam_string("smackfstransmute", Opt_fstransmute),
695 static const struct fs_parameter_description smack_fs_parameters = {
697 .specs = smack_param_specs,
701 * smack_fs_context_parse_param - Parse a single mount parameter
702 * @fc: The new filesystem context being constructed.
703 * @param: The parameter.
705 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
708 static int smack_fs_context_parse_param(struct fs_context *fc,
709 struct fs_parameter *param)
711 struct fs_parse_result result;
714 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
718 rc = smack_add_opt(opt, param->string, &fc->security);
720 param->string = NULL;
724 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
726 char *from = options, *to = options;
730 char *next = strchr(from, ',');
739 token = match_opt_prefix(from, len, &arg);
740 if (token != Opt_error) {
741 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
742 rc = smack_add_opt(token, arg, mnt_opts);
746 smack_free_mnt_opts(*mnt_opts);
751 if (!first) { // copy with preceding comma
756 memmove(to, from, len);
769 * smack_set_mnt_opts - set Smack specific mount options
770 * @sb: the file system superblock
771 * @opts: Smack mount options
772 * @kern_flags: mount option from kernel space or user space
773 * @set_kern_flags: where to store converted mount opts
775 * Returns 0 on success, an error code on failure
777 * Allow filesystems with binary mount data to explicitly set Smack mount
780 static int smack_set_mnt_opts(struct super_block *sb,
782 unsigned long kern_flags,
783 unsigned long *set_kern_flags)
785 struct dentry *root = sb->s_root;
786 struct inode *inode = d_backing_inode(root);
787 struct superblock_smack *sp = sb->s_security;
788 struct inode_smack *isp;
789 struct smack_known *skp;
790 struct smack_mnt_opts *opts = mnt_opts;
791 bool transmute = false;
793 if (sp->smk_flags & SMK_SB_INITIALIZED)
796 if (inode->i_security == NULL) {
797 int rc = lsm_inode_alloc(inode);
803 if (!smack_privileged(CAP_MAC_ADMIN)) {
805 * Unprivileged mounts don't get to specify Smack values.
810 * Unprivileged mounts get root and default from the caller.
812 skp = smk_of_current();
814 sp->smk_default = skp;
816 * For a handful of fs types with no user-controlled
817 * backing store it's okay to trust security labels
818 * in the filesystem. The rest are untrusted.
820 if (sb->s_user_ns != &init_user_ns &&
821 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
822 sb->s_magic != RAMFS_MAGIC) {
824 sp->smk_flags |= SMK_SB_UNTRUSTED;
828 sp->smk_flags |= SMK_SB_INITIALIZED;
831 if (opts->fsdefault) {
832 skp = smk_import_entry(opts->fsdefault, 0);
835 sp->smk_default = skp;
838 skp = smk_import_entry(opts->fsfloor, 0);
844 skp = smk_import_entry(opts->fshat, 0);
850 skp = smk_import_entry(opts->fsroot, 0);
855 if (opts->fstransmute) {
856 skp = smk_import_entry(opts->fstransmute, 0);
865 * Initialize the root inode.
867 init_inode_smack(inode, sp->smk_root);
870 isp = smack_inode(inode);
871 isp->smk_flags |= SMK_INODE_TRANSMUTE;
878 * smack_sb_statfs - Smack check on statfs
879 * @dentry: identifies the file system in question
881 * Returns 0 if current can read the floor of the filesystem,
882 * and error code otherwise
884 static int smack_sb_statfs(struct dentry *dentry)
886 struct superblock_smack *sbp = dentry->d_sb->s_security;
888 struct smk_audit_info ad;
890 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
891 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
893 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
894 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
903 * smack_bprm_set_creds - set creds for exec
904 * @bprm: the exec information
906 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
908 static int smack_bprm_set_creds(struct linux_binprm *bprm)
910 struct inode *inode = file_inode(bprm->file);
911 struct task_smack *bsp = smack_cred(bprm->cred);
912 struct inode_smack *isp;
913 struct superblock_smack *sbsp;
916 if (bprm->called_set_creds)
919 isp = smack_inode(inode);
920 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
923 sbsp = inode->i_sb->s_security;
924 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
925 isp->smk_task != sbsp->smk_root)
928 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
929 struct task_struct *tracer;
933 tracer = ptrace_parent(current);
934 if (likely(tracer != NULL))
935 rc = smk_ptrace_rule_check(tracer,
943 } else if (bprm->unsafe)
946 bsp->smk_task = isp->smk_task;
947 bprm->per_clear |= PER_CLEAR_ON_SETID;
949 /* Decide if this is a secure exec. */
950 if (bsp->smk_task != bsp->smk_forked)
951 bprm->secureexec = 1;
961 * smack_inode_alloc_security - allocate an inode blob
962 * @inode: the inode in need of a blob
964 * Returns 0 if it gets a blob, -ENOMEM otherwise
966 static int smack_inode_alloc_security(struct inode *inode)
968 struct smack_known *skp = smk_of_current();
970 init_inode_smack(inode, skp);
975 * smack_inode_init_security - copy out the smack from an inode
976 * @inode: the newly created inode
977 * @dir: containing directory object
979 * @name: where to put the attribute name
980 * @value: where to put the attribute value
981 * @len: where to put the length of the attribute
983 * Returns 0 if it all works out, -ENOMEM if there's no memory
985 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
986 const struct qstr *qstr, const char **name,
987 void **value, size_t *len)
989 struct inode_smack *issp = smack_inode(inode);
990 struct smack_known *skp = smk_of_current();
991 struct smack_known *isp = smk_of_inode(inode);
992 struct smack_known *dsp = smk_of_inode(dir);
996 *name = XATTR_SMACK_SUFFIX;
1000 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1005 * If the access rule allows transmutation and
1006 * the directory requests transmutation then
1007 * by all means transmute.
1008 * Mark the inode as changed.
1010 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1011 smk_inode_transmutable(dir)) {
1013 issp->smk_flags |= SMK_INODE_CHANGED;
1016 *value = kstrdup(isp->smk_known, GFP_NOFS);
1020 *len = strlen(isp->smk_known);
1027 * smack_inode_link - Smack check on link
1028 * @old_dentry: the existing object
1030 * @new_dentry: the new object
1032 * Returns 0 if access is permitted, an error code otherwise
1034 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1035 struct dentry *new_dentry)
1037 struct smack_known *isp;
1038 struct smk_audit_info ad;
1041 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1042 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1044 isp = smk_of_inode(d_backing_inode(old_dentry));
1045 rc = smk_curacc(isp, MAY_WRITE, &ad);
1046 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1048 if (rc == 0 && d_is_positive(new_dentry)) {
1049 isp = smk_of_inode(d_backing_inode(new_dentry));
1050 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1051 rc = smk_curacc(isp, MAY_WRITE, &ad);
1052 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1059 * smack_inode_unlink - Smack check on inode deletion
1060 * @dir: containing directory object
1061 * @dentry: file to unlink
1063 * Returns 0 if current can write the containing directory
1064 * and the object, error code otherwise
1066 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1068 struct inode *ip = d_backing_inode(dentry);
1069 struct smk_audit_info ad;
1072 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1073 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1076 * You need write access to the thing you're unlinking
1078 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1079 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1082 * You also need write access to the containing directory
1084 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1085 smk_ad_setfield_u_fs_inode(&ad, dir);
1086 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1087 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1093 * smack_inode_rmdir - Smack check on directory deletion
1094 * @dir: containing directory object
1095 * @dentry: directory to unlink
1097 * Returns 0 if current can write the containing directory
1098 * and the directory, error code otherwise
1100 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1102 struct smk_audit_info ad;
1105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1106 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1109 * You need write access to the thing you're removing
1111 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1112 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1115 * You also need write access to the containing directory
1117 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1118 smk_ad_setfield_u_fs_inode(&ad, dir);
1119 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1120 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1127 * smack_inode_rename - Smack check on rename
1128 * @old_inode: unused
1129 * @old_dentry: the old object
1130 * @new_inode: unused
1131 * @new_dentry: the new object
1133 * Read and write access is required on both the old and
1136 * Returns 0 if access is permitted, an error code otherwise
1138 static int smack_inode_rename(struct inode *old_inode,
1139 struct dentry *old_dentry,
1140 struct inode *new_inode,
1141 struct dentry *new_dentry)
1144 struct smack_known *isp;
1145 struct smk_audit_info ad;
1147 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1148 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1150 isp = smk_of_inode(d_backing_inode(old_dentry));
1151 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1152 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1154 if (rc == 0 && d_is_positive(new_dentry)) {
1155 isp = smk_of_inode(d_backing_inode(new_dentry));
1156 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1157 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1158 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1164 * smack_inode_permission - Smack version of permission()
1165 * @inode: the inode in question
1166 * @mask: the access requested
1168 * This is the important Smack hook.
1170 * Returns 0 if access is permitted, -EACCES otherwise
1172 static int smack_inode_permission(struct inode *inode, int mask)
1174 struct superblock_smack *sbsp = inode->i_sb->s_security;
1175 struct smk_audit_info ad;
1176 int no_block = mask & MAY_NOT_BLOCK;
1179 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1181 * No permission to check. Existence test. Yup, it's there.
1186 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1187 if (smk_of_inode(inode) != sbsp->smk_root)
1191 /* May be droppable after audit */
1194 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1195 smk_ad_setfield_u_fs_inode(&ad, inode);
1196 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1197 rc = smk_bu_inode(inode, mask, rc);
1202 * smack_inode_setattr - Smack check for setting attributes
1203 * @dentry: the object
1204 * @iattr: for the force flag
1206 * Returns 0 if access is permitted, an error code otherwise
1208 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1210 struct smk_audit_info ad;
1214 * Need to allow for clearing the setuid bit.
1216 if (iattr->ia_valid & ATTR_FORCE)
1218 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1219 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1221 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1222 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1227 * smack_inode_getattr - Smack check for getting attributes
1228 * @mnt: vfsmount of the object
1229 * @dentry: the object
1231 * Returns 0 if access is permitted, an error code otherwise
1233 static int smack_inode_getattr(const struct path *path)
1235 struct smk_audit_info ad;
1236 struct inode *inode = d_backing_inode(path->dentry);
1239 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1240 smk_ad_setfield_u_fs_path(&ad, *path);
1241 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1242 rc = smk_bu_inode(inode, MAY_READ, rc);
1247 * smack_inode_setxattr - Smack check for setting xattrs
1248 * @dentry: the object
1249 * @name: name of the attribute
1250 * @value: value of the attribute
1251 * @size: size of the value
1254 * This protects the Smack attribute explicitly.
1256 * Returns 0 if access is permitted, an error code otherwise
1258 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1259 const void *value, size_t size, int flags)
1261 struct smk_audit_info ad;
1262 struct smack_known *skp;
1264 int check_import = 0;
1269 * Check label validity here so import won't fail in post_setxattr
1271 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1272 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1273 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1276 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1277 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1281 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1283 if (size != TRANS_TRUE_SIZE ||
1284 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1287 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1289 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1292 if (rc == 0 && check_import) {
1293 skp = size ? smk_import_entry(value, size) : NULL;
1296 else if (skp == NULL || (check_star &&
1297 (skp == &smack_known_star || skp == &smack_known_web)))
1301 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1302 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1305 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1306 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1313 * smack_inode_post_setxattr - Apply the Smack update approved above
1315 * @name: attribute name
1316 * @value: attribute value
1317 * @size: attribute size
1320 * Set the pointer in the inode blob to the entry found
1321 * in the master label list.
1323 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1324 const void *value, size_t size, int flags)
1326 struct smack_known *skp;
1327 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1329 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1330 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1334 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1335 skp = smk_import_entry(value, size);
1337 isp->smk_inode = skp;
1338 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1339 skp = smk_import_entry(value, size);
1341 isp->smk_task = skp;
1342 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1343 skp = smk_import_entry(value, size);
1345 isp->smk_mmap = skp;
1352 * smack_inode_getxattr - Smack check on getxattr
1353 * @dentry: the object
1356 * Returns 0 if access is permitted, an error code otherwise
1358 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1360 struct smk_audit_info ad;
1363 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1364 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1366 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1367 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1372 * smack_inode_removexattr - Smack check on removexattr
1373 * @dentry: the object
1374 * @name: name of the attribute
1376 * Removing the Smack attribute requires CAP_MAC_ADMIN
1378 * Returns 0 if access is permitted, an error code otherwise
1380 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1382 struct inode_smack *isp;
1383 struct smk_audit_info ad;
1386 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1387 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1388 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1389 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1390 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1391 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1392 if (!smack_privileged(CAP_MAC_ADMIN))
1395 rc = cap_inode_removexattr(dentry, name);
1400 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1401 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1403 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1404 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1408 isp = smack_inode(d_backing_inode(dentry));
1410 * Don't do anything special for these.
1411 * XATTR_NAME_SMACKIPIN
1412 * XATTR_NAME_SMACKIPOUT
1414 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1415 struct super_block *sbp = dentry->d_sb;
1416 struct superblock_smack *sbsp = sbp->s_security;
1418 isp->smk_inode = sbsp->smk_default;
1419 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1420 isp->smk_task = NULL;
1421 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1422 isp->smk_mmap = NULL;
1423 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1424 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1430 * smack_inode_getsecurity - get smack xattrs
1431 * @inode: the object
1432 * @name: attribute name
1433 * @buffer: where to put the result
1434 * @alloc: duplicate memory
1436 * Returns the size of the attribute or an error code
1438 static int smack_inode_getsecurity(struct inode *inode,
1439 const char *name, void **buffer,
1442 struct socket_smack *ssp;
1443 struct socket *sock;
1444 struct super_block *sbp;
1445 struct inode *ip = (struct inode *)inode;
1446 struct smack_known *isp;
1448 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1449 isp = smk_of_inode(inode);
1452 * The rest of the Smack xattrs are only on sockets.
1455 if (sbp->s_magic != SOCKFS_MAGIC)
1458 sock = SOCKET_I(ip);
1459 if (sock == NULL || sock->sk == NULL)
1462 ssp = sock->sk->sk_security;
1464 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1466 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1473 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1474 if (*buffer == NULL)
1478 return strlen(isp->smk_known);
1483 * smack_inode_listsecurity - list the Smack attributes
1484 * @inode: the object
1485 * @buffer: where they go
1486 * @buffer_size: size of buffer
1488 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1491 int len = sizeof(XATTR_NAME_SMACK);
1493 if (buffer != NULL && len <= buffer_size)
1494 memcpy(buffer, XATTR_NAME_SMACK, len);
1500 * smack_inode_getsecid - Extract inode's security id
1501 * @inode: inode to extract the info from
1502 * @secid: where result will be saved
1504 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1506 struct smack_known *skp = smk_of_inode(inode);
1508 *secid = skp->smk_secid;
1516 * There is no smack_file_permission hook
1518 * Should access checks be done on each read or write?
1519 * UNICOS and SELinux say yes.
1520 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1522 * I'll say no for now. Smack does not do the frequent
1523 * label changing that SELinux does.
1527 * smack_file_alloc_security - assign a file security blob
1530 * The security blob for a file is a pointer to the master
1531 * label list, so no allocation is done.
1533 * f_security is the owner security information. It
1534 * isn't used on file access checks, it's for send_sigio.
1538 static int smack_file_alloc_security(struct file *file)
1540 struct smack_known **blob = smack_file(file);
1542 *blob = smk_of_current();
1547 * smack_file_ioctl - Smack check on ioctls
1552 * Relies heavily on the correct use of the ioctl command conventions.
1554 * Returns 0 if allowed, error code otherwise
1556 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1560 struct smk_audit_info ad;
1561 struct inode *inode = file_inode(file);
1563 if (unlikely(IS_PRIVATE(inode)))
1566 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1567 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1569 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1570 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1571 rc = smk_bu_file(file, MAY_WRITE, rc);
1574 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1575 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1576 rc = smk_bu_file(file, MAY_READ, rc);
1583 * smack_file_lock - Smack check on file locking
1587 * Returns 0 if current has lock access, error code otherwise
1589 static int smack_file_lock(struct file *file, unsigned int cmd)
1591 struct smk_audit_info ad;
1593 struct inode *inode = file_inode(file);
1595 if (unlikely(IS_PRIVATE(inode)))
1598 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1599 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1600 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1601 rc = smk_bu_file(file, MAY_LOCK, rc);
1606 * smack_file_fcntl - Smack check on fcntl
1608 * @cmd: what action to check
1611 * Generally these operations are harmless.
1612 * File locking operations present an obvious mechanism
1613 * for passing information, so they require write access.
1615 * Returns 0 if current has access, error code otherwise
1617 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1620 struct smk_audit_info ad;
1622 struct inode *inode = file_inode(file);
1624 if (unlikely(IS_PRIVATE(inode)))
1632 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1633 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1634 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1635 rc = smk_bu_file(file, MAY_LOCK, rc);
1639 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1640 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1641 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1642 rc = smk_bu_file(file, MAY_WRITE, rc);
1653 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1654 * if mapping anonymous memory.
1655 * @file contains the file structure for file to map (may be NULL).
1656 * @reqprot contains the protection requested by the application.
1657 * @prot contains the protection that will be applied by the kernel.
1658 * @flags contains the operational flags.
1659 * Return 0 if permission is granted.
1661 static int smack_mmap_file(struct file *file,
1662 unsigned long reqprot, unsigned long prot,
1663 unsigned long flags)
1665 struct smack_known *skp;
1666 struct smack_known *mkp;
1667 struct smack_rule *srp;
1668 struct task_smack *tsp;
1669 struct smack_known *okp;
1670 struct inode_smack *isp;
1671 struct superblock_smack *sbsp;
1680 if (unlikely(IS_PRIVATE(file_inode(file))))
1683 isp = smack_inode(file_inode(file));
1684 if (isp->smk_mmap == NULL)
1686 sbsp = file_inode(file)->i_sb->s_security;
1687 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1688 isp->smk_mmap != sbsp->smk_root)
1690 mkp = isp->smk_mmap;
1692 tsp = smack_cred(current_cred());
1693 skp = smk_of_current();
1698 * For each Smack rule associated with the subject
1699 * label verify that the SMACK64MMAP also has access
1700 * to that rule's object label.
1702 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1703 okp = srp->smk_object;
1705 * Matching labels always allows access.
1707 if (mkp->smk_known == okp->smk_known)
1710 * If there is a matching local rule take
1711 * that into account as well.
1713 may = smk_access_entry(srp->smk_subject->smk_known,
1717 may = srp->smk_access;
1719 may &= srp->smk_access;
1721 * If may is zero the SMACK64MMAP subject can't
1722 * possibly have less access.
1728 * Fetch the global list entry.
1729 * If there isn't one a SMACK64MMAP subject
1730 * can't have as much access as current.
1732 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1734 if (mmay == -ENOENT) {
1739 * If there is a local entry it modifies the
1740 * potential access, too.
1742 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1744 if (tmay != -ENOENT)
1748 * If there is any access available to current that is
1749 * not available to a SMACK64MMAP subject
1752 if ((may | mmay) != mmay) {
1764 * smack_file_set_fowner - set the file security blob value
1765 * @file: object in question
1768 static void smack_file_set_fowner(struct file *file)
1770 struct smack_known **blob = smack_file(file);
1772 *blob = smk_of_current();
1776 * smack_file_send_sigiotask - Smack on sigio
1777 * @tsk: The target task
1778 * @fown: the object the signal come from
1781 * Allow a privileged task to get signals even if it shouldn't
1783 * Returns 0 if a subject with the object's smack could
1784 * write to the task, an error code otherwise.
1786 static int smack_file_send_sigiotask(struct task_struct *tsk,
1787 struct fown_struct *fown, int signum)
1789 struct smack_known **blob;
1790 struct smack_known *skp;
1791 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1792 const struct cred *tcred;
1795 struct smk_audit_info ad;
1798 * struct fown_struct is never outside the context of a struct file
1800 file = container_of(fown, struct file, f_owner);
1802 /* we don't log here as rc can be overriden */
1803 blob = smack_file(file);
1805 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1806 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1809 tcred = __task_cred(tsk);
1810 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1814 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1815 smk_ad_setfield_u_tsk(&ad, tsk);
1816 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1821 * smack_file_receive - Smack file receive check
1824 * Returns 0 if current has access, error code otherwise
1826 static int smack_file_receive(struct file *file)
1830 struct smk_audit_info ad;
1831 struct inode *inode = file_inode(file);
1832 struct socket *sock;
1833 struct task_smack *tsp;
1834 struct socket_smack *ssp;
1836 if (unlikely(IS_PRIVATE(inode)))
1839 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1840 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1842 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1843 sock = SOCKET_I(inode);
1844 ssp = sock->sk->sk_security;
1845 tsp = smack_cred(current_cred());
1847 * If the receiving process can't write to the
1848 * passed socket or if the passed socket can't
1849 * write to the receiving process don't accept
1850 * the passed socket.
1852 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1853 rc = smk_bu_file(file, may, rc);
1856 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1857 rc = smk_bu_file(file, may, rc);
1861 * This code relies on bitmasks.
1863 if (file->f_mode & FMODE_READ)
1865 if (file->f_mode & FMODE_WRITE)
1868 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1869 rc = smk_bu_file(file, may, rc);
1874 * smack_file_open - Smack dentry open processing
1876 * @cred: task credential
1878 * Set the security blob in the file structure.
1879 * Allow the open only if the task has read access. There are
1880 * many read operations (e.g. fstat) that you can do with an
1881 * fd even if you have the file open write-only.
1885 static int smack_file_open(struct file *file)
1887 struct task_smack *tsp = smack_cred(file->f_cred);
1888 struct inode *inode = file_inode(file);
1889 struct smk_audit_info ad;
1892 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1893 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1894 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1895 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1905 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1906 * @new: the new credentials
1907 * @gfp: the atomicity of any memory allocations
1909 * Prepare a blank set of credentials for modification. This must allocate all
1910 * the memory the LSM module might require such that cred_transfer() can
1911 * complete without error.
1913 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1915 init_task_smack(smack_cred(cred), NULL, NULL);
1921 * smack_cred_free - "free" task-level security credentials
1922 * @cred: the credentials in question
1925 static void smack_cred_free(struct cred *cred)
1927 struct task_smack *tsp = smack_cred(cred);
1928 struct smack_rule *rp;
1929 struct list_head *l;
1930 struct list_head *n;
1932 smk_destroy_label_list(&tsp->smk_relabel);
1934 list_for_each_safe(l, n, &tsp->smk_rules) {
1935 rp = list_entry(l, struct smack_rule, list);
1936 list_del(&rp->list);
1937 kmem_cache_free(smack_rule_cache, rp);
1942 * smack_cred_prepare - prepare new set of credentials for modification
1943 * @new: the new credentials
1944 * @old: the original credentials
1945 * @gfp: the atomicity of any memory allocations
1947 * Prepare a new set of credentials for modification.
1949 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1952 struct task_smack *old_tsp = smack_cred(old);
1953 struct task_smack *new_tsp = smack_cred(new);
1956 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1958 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1962 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1968 * smack_cred_transfer - Transfer the old credentials to the new credentials
1969 * @new: the new credentials
1970 * @old: the original credentials
1972 * Fill in a set of blank credentials from another set of credentials.
1974 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1976 struct task_smack *old_tsp = smack_cred(old);
1977 struct task_smack *new_tsp = smack_cred(new);
1979 new_tsp->smk_task = old_tsp->smk_task;
1980 new_tsp->smk_forked = old_tsp->smk_task;
1981 mutex_init(&new_tsp->smk_rules_lock);
1982 INIT_LIST_HEAD(&new_tsp->smk_rules);
1984 /* cbs copy rule list */
1988 * smack_cred_getsecid - get the secid corresponding to a creds structure
1989 * @c: the object creds
1990 * @secid: where to put the result
1992 * Sets the secid to contain a u32 version of the smack label.
1994 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1996 struct smack_known *skp;
1999 skp = smk_of_task(smack_cred(cred));
2000 *secid = skp->smk_secid;
2005 * smack_kernel_act_as - Set the subjective context in a set of credentials
2006 * @new: points to the set of credentials to be modified.
2007 * @secid: specifies the security ID to be set
2009 * Set the security data for a kernel service.
2011 static int smack_kernel_act_as(struct cred *new, u32 secid)
2013 struct task_smack *new_tsp = smack_cred(new);
2015 new_tsp->smk_task = smack_from_secid(secid);
2020 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2021 * @new: points to the set of credentials to be modified
2022 * @inode: points to the inode to use as a reference
2024 * Set the file creation context in a set of credentials to the same
2025 * as the objective context of the specified inode
2027 static int smack_kernel_create_files_as(struct cred *new,
2028 struct inode *inode)
2030 struct inode_smack *isp = smack_inode(inode);
2031 struct task_smack *tsp = smack_cred(new);
2033 tsp->smk_forked = isp->smk_inode;
2034 tsp->smk_task = tsp->smk_forked;
2039 * smk_curacc_on_task - helper to log task related access
2040 * @p: the task object
2041 * @access: the access requested
2042 * @caller: name of the calling function for audit
2044 * Return 0 if access is permitted
2046 static int smk_curacc_on_task(struct task_struct *p, int access,
2049 struct smk_audit_info ad;
2050 struct smack_known *skp = smk_of_task_struct(p);
2053 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2054 smk_ad_setfield_u_tsk(&ad, p);
2055 rc = smk_curacc(skp, access, &ad);
2056 rc = smk_bu_task(p, access, rc);
2061 * smack_task_setpgid - Smack check on setting pgid
2062 * @p: the task object
2065 * Return 0 if write access is permitted
2067 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2069 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2073 * smack_task_getpgid - Smack access check for getpgid
2074 * @p: the object task
2076 * Returns 0 if current can read the object task, error code otherwise
2078 static int smack_task_getpgid(struct task_struct *p)
2080 return smk_curacc_on_task(p, MAY_READ, __func__);
2084 * smack_task_getsid - Smack access check for getsid
2085 * @p: the object task
2087 * Returns 0 if current can read the object task, error code otherwise
2089 static int smack_task_getsid(struct task_struct *p)
2091 return smk_curacc_on_task(p, MAY_READ, __func__);
2095 * smack_task_getsecid - get the secid of the task
2096 * @p: the object task
2097 * @secid: where to put the result
2099 * Sets the secid to contain a u32 version of the smack label.
2101 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2103 struct smack_known *skp = smk_of_task_struct(p);
2105 *secid = skp->smk_secid;
2109 * smack_task_setnice - Smack check on setting nice
2110 * @p: the task object
2113 * Return 0 if write access is permitted
2115 static int smack_task_setnice(struct task_struct *p, int nice)
2117 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2121 * smack_task_setioprio - Smack check on setting ioprio
2122 * @p: the task object
2125 * Return 0 if write access is permitted
2127 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2129 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2133 * smack_task_getioprio - Smack check on reading ioprio
2134 * @p: the task object
2136 * Return 0 if read access is permitted
2138 static int smack_task_getioprio(struct task_struct *p)
2140 return smk_curacc_on_task(p, MAY_READ, __func__);
2144 * smack_task_setscheduler - Smack check on setting scheduler
2145 * @p: the task object
2149 * Return 0 if read access is permitted
2151 static int smack_task_setscheduler(struct task_struct *p)
2153 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2157 * smack_task_getscheduler - Smack check on reading scheduler
2158 * @p: the task object
2160 * Return 0 if read access is permitted
2162 static int smack_task_getscheduler(struct task_struct *p)
2164 return smk_curacc_on_task(p, MAY_READ, __func__);
2168 * smack_task_movememory - Smack check on moving memory
2169 * @p: the task object
2171 * Return 0 if write access is permitted
2173 static int smack_task_movememory(struct task_struct *p)
2175 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2179 * smack_task_kill - Smack check on signal delivery
2180 * @p: the task object
2183 * @cred: identifies the cred to use in lieu of current's
2185 * Return 0 if write access is permitted
2188 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2189 int sig, const struct cred *cred)
2191 struct smk_audit_info ad;
2192 struct smack_known *skp;
2193 struct smack_known *tkp = smk_of_task_struct(p);
2197 return 0; /* null signal; existence test */
2199 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2200 smk_ad_setfield_u_tsk(&ad, p);
2202 * Sending a signal requires that the sender
2203 * can write the receiver.
2206 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2207 rc = smk_bu_task(p, MAY_DELIVER, rc);
2211 * If the cred isn't NULL we're dealing with some USB IO
2212 * specific behavior. This is not clean. For one thing
2213 * we can't take privilege into account.
2215 skp = smk_of_task(smack_cred(cred));
2216 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2217 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2222 * smack_task_to_inode - copy task smack into the inode blob
2223 * @p: task to copy from
2224 * @inode: inode to copy to
2226 * Sets the smack pointer in the inode security blob
2228 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2230 struct inode_smack *isp = smack_inode(inode);
2231 struct smack_known *skp = smk_of_task_struct(p);
2233 isp->smk_inode = skp;
2234 isp->smk_flags |= SMK_INODE_INSTANT;
2242 * smack_sk_alloc_security - Allocate a socket blob
2245 * @gfp_flags: memory allocation flags
2247 * Assign Smack pointers to current
2249 * Returns 0 on success, -ENOMEM is there's no memory
2251 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2253 struct smack_known *skp = smk_of_current();
2254 struct socket_smack *ssp;
2256 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2261 * Sockets created by kernel threads receive web label.
2263 if (unlikely(current->flags & PF_KTHREAD)) {
2264 ssp->smk_in = &smack_known_web;
2265 ssp->smk_out = &smack_known_web;
2270 ssp->smk_packet = NULL;
2272 sk->sk_security = ssp;
2278 * smack_sk_free_security - Free a socket blob
2281 * Clears the blob pointer
2283 static void smack_sk_free_security(struct sock *sk)
2285 #ifdef SMACK_IPV6_PORT_LABELING
2286 struct smk_port_label *spp;
2288 if (sk->sk_family == PF_INET6) {
2290 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2291 if (spp->smk_sock != sk)
2293 spp->smk_can_reuse = 1;
2299 kfree(sk->sk_security);
2303 * smack_ipv4host_label - check host based restrictions
2304 * @sip: the object end
2306 * looks for host based access restrictions
2308 * This version will only be appropriate for really small sets of single label
2309 * hosts. The caller is responsible for ensuring that the RCU read lock is
2310 * taken before calling this function.
2312 * Returns the label of the far end or NULL if it's not special.
2314 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2316 struct smk_net4addr *snp;
2317 struct in_addr *siap = &sip->sin_addr;
2319 if (siap->s_addr == 0)
2322 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2324 * we break after finding the first match because
2325 * the list is sorted from longest to shortest mask
2326 * so we have found the most specific match
2328 if (snp->smk_host.s_addr ==
2329 (siap->s_addr & snp->smk_mask.s_addr))
2330 return snp->smk_label;
2335 #if IS_ENABLED(CONFIG_IPV6)
2337 * smk_ipv6_localhost - Check for local ipv6 host address
2340 * Returns boolean true if this is the localhost address
2342 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2344 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2345 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2347 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2348 ntohs(be16p[7]) == 1)
2354 * smack_ipv6host_label - check host based restrictions
2355 * @sip: the object end
2357 * looks for host based access restrictions
2359 * This version will only be appropriate for really small sets of single label
2360 * hosts. The caller is responsible for ensuring that the RCU read lock is
2361 * taken before calling this function.
2363 * Returns the label of the far end or NULL if it's not special.
2365 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2367 struct smk_net6addr *snp;
2368 struct in6_addr *sap = &sip->sin6_addr;
2373 * It's local. Don't look for a host label.
2375 if (smk_ipv6_localhost(sip))
2378 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2380 * If the label is NULL the entry has
2381 * been renounced. Ignore it.
2383 if (snp->smk_label == NULL)
2386 * we break after finding the first match because
2387 * the list is sorted from longest to shortest mask
2388 * so we have found the most specific match
2390 for (found = 1, i = 0; i < 8; i++) {
2391 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2392 snp->smk_host.s6_addr16[i]) {
2398 return snp->smk_label;
2403 #endif /* CONFIG_IPV6 */
2406 * smack_netlabel - Set the secattr on a socket
2408 * @labeled: socket label scheme
2410 * Convert the outbound smack value (smk_out) to a
2411 * secattr and attach it to the socket.
2413 * Returns 0 on success or an error code
2415 static int smack_netlabel(struct sock *sk, int labeled)
2417 struct smack_known *skp;
2418 struct socket_smack *ssp = sk->sk_security;
2422 * Usually the netlabel code will handle changing the
2423 * packet labeling based on the label.
2424 * The case of a single label host is different, because
2425 * a single label host should never get a labeled packet
2426 * even though the label is usually associated with a packet
2430 bh_lock_sock_nested(sk);
2432 if (ssp->smk_out == smack_net_ambient ||
2433 labeled == SMACK_UNLABELED_SOCKET)
2434 netlbl_sock_delattr(sk);
2437 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2447 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2449 * @sap: the destination address
2451 * Set the correct secattr for the given socket based on the destination
2452 * address and perform any outbound access checks needed.
2454 * Returns 0 on success or an error code.
2457 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2459 struct smack_known *skp;
2462 struct smack_known *hkp;
2463 struct socket_smack *ssp = sk->sk_security;
2464 struct smk_audit_info ad;
2467 hkp = smack_ipv4host_label(sap);
2470 struct lsm_network_audit net;
2472 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2473 ad.a.u.net->family = sap->sin_family;
2474 ad.a.u.net->dport = sap->sin_port;
2475 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2477 sk_lbl = SMACK_UNLABELED_SOCKET;
2479 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2480 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2482 sk_lbl = SMACK_CIPSO_SOCKET;
2489 return smack_netlabel(sk, sk_lbl);
2492 #if IS_ENABLED(CONFIG_IPV6)
2494 * smk_ipv6_check - check Smack access
2495 * @subject: subject Smack label
2496 * @object: object Smack label
2498 * @act: the action being taken
2500 * Check an IPv6 access
2502 static int smk_ipv6_check(struct smack_known *subject,
2503 struct smack_known *object,
2504 struct sockaddr_in6 *address, int act)
2507 struct lsm_network_audit net;
2509 struct smk_audit_info ad;
2513 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2514 ad.a.u.net->family = PF_INET6;
2515 ad.a.u.net->dport = ntohs(address->sin6_port);
2516 if (act == SMK_RECEIVING)
2517 ad.a.u.net->v6info.saddr = address->sin6_addr;
2519 ad.a.u.net->v6info.daddr = address->sin6_addr;
2521 rc = smk_access(subject, object, MAY_WRITE, &ad);
2522 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2525 #endif /* CONFIG_IPV6 */
2527 #ifdef SMACK_IPV6_PORT_LABELING
2529 * smk_ipv6_port_label - Smack port access table management
2533 * Create or update the port list entry
2535 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2537 struct sock *sk = sock->sk;
2538 struct sockaddr_in6 *addr6;
2539 struct socket_smack *ssp = sock->sk->sk_security;
2540 struct smk_port_label *spp;
2541 unsigned short port = 0;
2543 if (address == NULL) {
2545 * This operation is changing the Smack information
2546 * on the bound socket. Take the changes to the port
2550 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2551 if (sk != spp->smk_sock)
2553 spp->smk_in = ssp->smk_in;
2554 spp->smk_out = ssp->smk_out;
2559 * A NULL address is only used for updating existing
2560 * bound entries. If there isn't one, it's OK.
2566 addr6 = (struct sockaddr_in6 *)address;
2567 port = ntohs(addr6->sin6_port);
2569 * This is a special case that is safely ignored.
2575 * Look for an existing port list entry.
2576 * This is an indication that a port is getting reused.
2579 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2580 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2582 if (spp->smk_can_reuse != 1) {
2586 spp->smk_port = port;
2588 spp->smk_in = ssp->smk_in;
2589 spp->smk_out = ssp->smk_out;
2590 spp->smk_can_reuse = 0;
2596 * A new port entry is required.
2598 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2602 spp->smk_port = port;
2604 spp->smk_in = ssp->smk_in;
2605 spp->smk_out = ssp->smk_out;
2606 spp->smk_sock_type = sock->type;
2607 spp->smk_can_reuse = 0;
2609 mutex_lock(&smack_ipv6_lock);
2610 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2611 mutex_unlock(&smack_ipv6_lock);
2616 * smk_ipv6_port_check - check Smack port access
2620 * Create or update the port list entry
2622 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2625 struct smk_port_label *spp;
2626 struct socket_smack *ssp = sk->sk_security;
2627 struct smack_known *skp = NULL;
2628 unsigned short port;
2629 struct smack_known *object;
2631 if (act == SMK_RECEIVING) {
2632 skp = smack_ipv6host_label(address);
2633 object = ssp->smk_in;
2636 object = smack_ipv6host_label(address);
2640 * The other end is a single label host.
2642 if (skp != NULL && object != NULL)
2643 return smk_ipv6_check(skp, object, address, act);
2645 skp = smack_net_ambient;
2647 object = smack_net_ambient;
2650 * It's remote, so port lookup does no good.
2652 if (!smk_ipv6_localhost(address))
2653 return smk_ipv6_check(skp, object, address, act);
2656 * It's local so the send check has to have passed.
2658 if (act == SMK_RECEIVING)
2661 port = ntohs(address->sin6_port);
2663 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2664 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2666 object = spp->smk_in;
2667 if (act == SMK_CONNECTING)
2668 ssp->smk_packet = spp->smk_out;
2673 return smk_ipv6_check(skp, object, address, act);
2675 #endif /* SMACK_IPV6_PORT_LABELING */
2678 * smack_inode_setsecurity - set smack xattrs
2679 * @inode: the object
2680 * @name: attribute name
2681 * @value: attribute value
2682 * @size: size of the attribute
2685 * Sets the named attribute in the appropriate blob
2687 * Returns 0 on success, or an error code
2689 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2690 const void *value, size_t size, int flags)
2692 struct smack_known *skp;
2693 struct inode_smack *nsp = smack_inode(inode);
2694 struct socket_smack *ssp;
2695 struct socket *sock;
2698 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2701 skp = smk_import_entry(value, size);
2703 return PTR_ERR(skp);
2705 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2706 nsp->smk_inode = skp;
2707 nsp->smk_flags |= SMK_INODE_INSTANT;
2711 * The rest of the Smack xattrs are only on sockets.
2713 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2716 sock = SOCKET_I(inode);
2717 if (sock == NULL || sock->sk == NULL)
2720 ssp = sock->sk->sk_security;
2722 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2724 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2726 if (sock->sk->sk_family == PF_INET) {
2727 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2730 "Smack: \"%s\" netlbl error %d.\n",
2736 #ifdef SMACK_IPV6_PORT_LABELING
2737 if (sock->sk->sk_family == PF_INET6)
2738 smk_ipv6_port_label(sock, NULL);
2745 * smack_socket_post_create - finish socket setup
2747 * @family: protocol family
2752 * Sets the netlabel information on the socket
2754 * Returns 0 on success, and error code otherwise
2756 static int smack_socket_post_create(struct socket *sock, int family,
2757 int type, int protocol, int kern)
2759 struct socket_smack *ssp;
2761 if (sock->sk == NULL)
2765 * Sockets created by kernel threads receive web label.
2767 if (unlikely(current->flags & PF_KTHREAD)) {
2768 ssp = sock->sk->sk_security;
2769 ssp->smk_in = &smack_known_web;
2770 ssp->smk_out = &smack_known_web;
2773 if (family != PF_INET)
2776 * Set the outbound netlbl.
2778 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2782 * smack_socket_socketpair - create socket pair
2783 * @socka: one socket
2784 * @sockb: another socket
2786 * Cross reference the peer labels for SO_PEERSEC
2788 * Returns 0 on success, and error code otherwise
2790 static int smack_socket_socketpair(struct socket *socka,
2791 struct socket *sockb)
2793 struct socket_smack *asp = socka->sk->sk_security;
2794 struct socket_smack *bsp = sockb->sk->sk_security;
2796 asp->smk_packet = bsp->smk_out;
2797 bsp->smk_packet = asp->smk_out;
2802 #ifdef SMACK_IPV6_PORT_LABELING
2804 * smack_socket_bind - record port binding information.
2806 * @address: the port address
2807 * @addrlen: size of the address
2809 * Records the label bound to a port.
2811 * Returns 0 on success, and error code otherwise
2813 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2816 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2817 if (addrlen < SIN6_LEN_RFC2133 ||
2818 address->sa_family != AF_INET6)
2820 smk_ipv6_port_label(sock, address);
2824 #endif /* SMACK_IPV6_PORT_LABELING */
2827 * smack_socket_connect - connect access check
2829 * @sap: the other end
2830 * @addrlen: size of sap
2832 * Verifies that a connection may be possible
2834 * Returns 0 on success, and error code otherwise
2836 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2840 #if IS_ENABLED(CONFIG_IPV6)
2841 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2843 #ifdef SMACK_IPV6_SECMARK_LABELING
2844 struct smack_known *rsp;
2845 struct socket_smack *ssp;
2848 if (sock->sk == NULL)
2851 #ifdef SMACK_IPV6_SECMARK_LABELING
2852 ssp = sock->sk->sk_security;
2855 switch (sock->sk->sk_family) {
2857 if (addrlen < sizeof(struct sockaddr_in) ||
2858 sap->sa_family != AF_INET)
2860 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2863 if (addrlen < SIN6_LEN_RFC2133 || sap->sa_family != AF_INET6)
2865 #ifdef SMACK_IPV6_SECMARK_LABELING
2866 rsp = smack_ipv6host_label(sip);
2868 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2871 #ifdef SMACK_IPV6_PORT_LABELING
2872 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2880 * smack_flags_to_may - convert S_ to MAY_ values
2881 * @flags: the S_ value
2883 * Returns the equivalent MAY_ value
2885 static int smack_flags_to_may(int flags)
2889 if (flags & S_IRUGO)
2891 if (flags & S_IWUGO)
2893 if (flags & S_IXUGO)
2900 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2905 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2907 struct smack_known **blob = smack_msg_msg(msg);
2909 *blob = smk_of_current();
2914 * smack_of_ipc - the smack pointer for the ipc
2917 * Returns a pointer to the smack value
2919 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2921 struct smack_known **blob = smack_ipc(isp);
2927 * smack_ipc_alloc_security - Set the security blob for ipc
2932 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2934 struct smack_known **blob = smack_ipc(isp);
2936 *blob = smk_of_current();
2941 * smk_curacc_shm : check if current has access on shm
2943 * @access : access requested
2945 * Returns 0 if current has the requested access, error code otherwise
2947 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2949 struct smack_known *ssp = smack_of_ipc(isp);
2950 struct smk_audit_info ad;
2954 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2955 ad.a.u.ipc_id = isp->id;
2957 rc = smk_curacc(ssp, access, &ad);
2958 rc = smk_bu_current("shm", ssp, access, rc);
2963 * smack_shm_associate - Smack access check for shm
2965 * @shmflg: access requested
2967 * Returns 0 if current has the requested access, error code otherwise
2969 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2973 may = smack_flags_to_may(shmflg);
2974 return smk_curacc_shm(isp, may);
2978 * smack_shm_shmctl - Smack access check for shm
2980 * @cmd: what it wants to do
2982 * Returns 0 if current has the requested access, error code otherwise
2984 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2998 may = MAY_READWRITE;
3003 * System level information.
3009 return smk_curacc_shm(isp, may);
3013 * smack_shm_shmat - Smack access for shmat
3016 * @shmflg: access requested
3018 * Returns 0 if current has the requested access, error code otherwise
3020 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3025 may = smack_flags_to_may(shmflg);
3026 return smk_curacc_shm(ipc, may);
3030 * smk_curacc_sem : check if current has access on sem
3032 * @access : access requested
3034 * Returns 0 if current has the requested access, error code otherwise
3036 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3038 struct smack_known *ssp = smack_of_ipc(isp);
3039 struct smk_audit_info ad;
3043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3044 ad.a.u.ipc_id = isp->id;
3046 rc = smk_curacc(ssp, access, &ad);
3047 rc = smk_bu_current("sem", ssp, access, rc);
3052 * smack_sem_associate - Smack access check for sem
3054 * @semflg: access requested
3056 * Returns 0 if current has the requested access, error code otherwise
3058 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3062 may = smack_flags_to_may(semflg);
3063 return smk_curacc_sem(isp, may);
3067 * smack_sem_shmctl - Smack access check for sem
3069 * @cmd: what it wants to do
3071 * Returns 0 if current has the requested access, error code otherwise
3073 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3092 may = MAY_READWRITE;
3097 * System level information
3104 return smk_curacc_sem(isp, may);
3108 * smack_sem_semop - Smack checks of semaphore operations
3114 * Treated as read and write in all cases.
3116 * Returns 0 if access is allowed, error code otherwise
3118 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3119 unsigned nsops, int alter)
3121 return smk_curacc_sem(isp, MAY_READWRITE);
3125 * smk_curacc_msq : helper to check if current has access on msq
3127 * @access : access requested
3129 * return 0 if current has access, error otherwise
3131 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3133 struct smack_known *msp = smack_of_ipc(isp);
3134 struct smk_audit_info ad;
3138 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3139 ad.a.u.ipc_id = isp->id;
3141 rc = smk_curacc(msp, access, &ad);
3142 rc = smk_bu_current("msq", msp, access, rc);
3147 * smack_msg_queue_associate - Smack access check for msg_queue
3149 * @msqflg: access requested
3151 * Returns 0 if current has the requested access, error code otherwise
3153 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3157 may = smack_flags_to_may(msqflg);
3158 return smk_curacc_msq(isp, may);
3162 * smack_msg_queue_msgctl - Smack access check for msg_queue
3164 * @cmd: what it wants to do
3166 * Returns 0 if current has the requested access, error code otherwise
3168 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3180 may = MAY_READWRITE;
3185 * System level information
3192 return smk_curacc_msq(isp, may);
3196 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3199 * @msqflg: access requested
3201 * Returns 0 if current has the requested access, error code otherwise
3203 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3208 may = smack_flags_to_may(msqflg);
3209 return smk_curacc_msq(isp, may);
3213 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3220 * Returns 0 if current has read and write access, error code otherwise
3222 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3223 struct task_struct *target, long type, int mode)
3225 return smk_curacc_msq(isp, MAY_READWRITE);
3229 * smack_ipc_permission - Smack access for ipc_permission()
3230 * @ipp: the object permissions
3231 * @flag: access requested
3233 * Returns 0 if current has read and write access, error code otherwise
3235 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3237 struct smack_known **blob = smack_ipc(ipp);
3238 struct smack_known *iskp = *blob;
3239 int may = smack_flags_to_may(flag);
3240 struct smk_audit_info ad;
3244 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3245 ad.a.u.ipc_id = ipp->id;
3247 rc = smk_curacc(iskp, may, &ad);
3248 rc = smk_bu_current("svipc", iskp, may, rc);
3253 * smack_ipc_getsecid - Extract smack security id
3254 * @ipp: the object permissions
3255 * @secid: where result will be saved
3257 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3259 struct smack_known **blob = smack_ipc(ipp);
3260 struct smack_known *iskp = *blob;
3262 *secid = iskp->smk_secid;
3266 * smack_d_instantiate - Make sure the blob is correct on an inode
3267 * @opt_dentry: dentry where inode will be attached
3268 * @inode: the object
3270 * Set the inode's security blob if it hasn't been done already.
3272 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3274 struct super_block *sbp;
3275 struct superblock_smack *sbsp;
3276 struct inode_smack *isp;
3277 struct smack_known *skp;
3278 struct smack_known *ckp = smk_of_current();
3279 struct smack_known *final;
3280 char trattr[TRANS_TRUE_SIZE];
3288 isp = smack_inode(inode);
3290 mutex_lock(&isp->smk_lock);
3292 * If the inode is already instantiated
3293 * take the quick way out
3295 if (isp->smk_flags & SMK_INODE_INSTANT)
3299 sbsp = sbp->s_security;
3301 * We're going to use the superblock default label
3302 * if there's no label on the file.
3304 final = sbsp->smk_default;
3307 * If this is the root inode the superblock
3308 * may be in the process of initialization.
3309 * If that is the case use the root value out
3310 * of the superblock.
3312 if (opt_dentry->d_parent == opt_dentry) {
3313 switch (sbp->s_magic) {
3314 case CGROUP_SUPER_MAGIC:
3315 case CGROUP2_SUPER_MAGIC:
3317 * The cgroup filesystem is never mounted,
3318 * so there's no opportunity to set the mount
3321 sbsp->smk_root = &smack_known_star;
3322 sbsp->smk_default = &smack_known_star;
3323 isp->smk_inode = sbsp->smk_root;
3327 * What about shmem/tmpfs anonymous files with dentry
3328 * obtained from d_alloc_pseudo()?
3330 isp->smk_inode = smk_of_current();
3333 isp->smk_inode = smk_of_current();
3337 * Socket access is controlled by the socket
3338 * structures associated with the task involved.
3340 isp->smk_inode = &smack_known_star;
3343 isp->smk_inode = sbsp->smk_root;
3346 isp->smk_flags |= SMK_INODE_INSTANT;
3351 * This is pretty hackish.
3352 * Casey says that we shouldn't have to do
3353 * file system specific code, but it does help
3354 * with keeping it simple.
3356 switch (sbp->s_magic) {
3358 case CGROUP_SUPER_MAGIC:
3359 case CGROUP2_SUPER_MAGIC:
3361 * Casey says that it's a little embarrassing
3362 * that the smack file system doesn't do
3363 * extended attributes.
3365 * Cgroupfs is special
3367 final = &smack_known_star;
3369 case DEVPTS_SUPER_MAGIC:
3371 * devpts seems content with the label of the task.
3372 * Programs that change smack have to treat the
3377 case PROC_SUPER_MAGIC:
3379 * Casey says procfs appears not to care.
3380 * The superblock default suffices.
3385 * Device labels should come from the filesystem,
3386 * but watch out, because they're volitile,
3387 * getting recreated on every reboot.
3389 final = &smack_known_star;
3391 * If a smack value has been set we want to use it,
3392 * but since tmpfs isn't giving us the opportunity
3393 * to set mount options simulate setting the
3394 * superblock default.
3399 * This isn't an understood special case.
3400 * Get the value from the xattr.
3404 * UNIX domain sockets use lower level socket data.
3406 if (S_ISSOCK(inode->i_mode)) {
3407 final = &smack_known_star;
3411 * No xattr support means, alas, no SMACK label.
3412 * Use the aforeapplied default.
3413 * It would be curious if the label of the task
3414 * does not match that assigned.
3416 if (!(inode->i_opflags & IOP_XATTR))
3419 * Get the dentry for xattr.
3421 dp = dget(opt_dentry);
3422 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3423 if (!IS_ERR_OR_NULL(skp))
3427 * Transmuting directory
3429 if (S_ISDIR(inode->i_mode)) {
3431 * If this is a new directory and the label was
3432 * transmuted when the inode was initialized
3433 * set the transmute attribute on the directory
3434 * and mark the inode.
3436 * If there is a transmute attribute on the
3437 * directory mark the inode.
3439 if (isp->smk_flags & SMK_INODE_CHANGED) {
3440 isp->smk_flags &= ~SMK_INODE_CHANGED;
3441 rc = __vfs_setxattr(dp, inode,
3442 XATTR_NAME_SMACKTRANSMUTE,
3443 TRANS_TRUE, TRANS_TRUE_SIZE,
3446 rc = __vfs_getxattr(dp, inode,
3447 XATTR_NAME_SMACKTRANSMUTE, trattr,
3449 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3450 TRANS_TRUE_SIZE) != 0)
3454 transflag = SMK_INODE_TRANSMUTE;
3457 * Don't let the exec or mmap label be "*" or "@".
3459 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3460 if (IS_ERR(skp) || skp == &smack_known_star ||
3461 skp == &smack_known_web)
3463 isp->smk_task = skp;
3465 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3466 if (IS_ERR(skp) || skp == &smack_known_star ||
3467 skp == &smack_known_web)
3469 isp->smk_mmap = skp;
3476 isp->smk_inode = ckp;
3478 isp->smk_inode = final;
3480 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3483 mutex_unlock(&isp->smk_lock);
3488 * smack_getprocattr - Smack process attribute access
3489 * @p: the object task
3490 * @name: the name of the attribute in /proc/.../attr
3491 * @value: where to put the result
3493 * Places a copy of the task Smack into value
3495 * Returns the length of the smack label or an error code
3497 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3499 struct smack_known *skp = smk_of_task_struct(p);
3503 if (strcmp(name, "current") != 0)
3506 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3516 * smack_setprocattr - Smack process attribute setting
3517 * @name: the name of the attribute in /proc/.../attr
3518 * @value: the value to set
3519 * @size: the size of the value
3521 * Sets the Smack value of the task. Only setting self
3522 * is permitted and only with privilege
3524 * Returns the length of the smack label or an error code
3526 static int smack_setprocattr(const char *name, void *value, size_t size)
3528 struct task_smack *tsp = smack_cred(current_cred());
3530 struct smack_known *skp;
3531 struct smack_known_list_elem *sklep;
3534 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3537 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3540 if (strcmp(name, "current") != 0)
3543 skp = smk_import_entry(value, size);
3545 return PTR_ERR(skp);
3548 * No process is ever allowed the web ("@") label
3549 * and the star ("*") label.
3551 if (skp == &smack_known_web || skp == &smack_known_star)
3554 if (!smack_privileged(CAP_MAC_ADMIN)) {
3556 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3557 if (sklep->smk_label == skp) {
3565 new = prepare_creds();
3569 tsp = smack_cred(new);
3570 tsp->smk_task = skp;
3572 * process can change its label only once
3574 smk_destroy_label_list(&tsp->smk_relabel);
3581 * smack_unix_stream_connect - Smack access on UDS
3583 * @other: the other sock
3586 * Return 0 if a subject with the smack of sock could access
3587 * an object with the smack of other, otherwise an error code
3589 static int smack_unix_stream_connect(struct sock *sock,
3590 struct sock *other, struct sock *newsk)
3592 struct smack_known *skp;
3593 struct smack_known *okp;
3594 struct socket_smack *ssp = sock->sk_security;
3595 struct socket_smack *osp = other->sk_security;
3596 struct socket_smack *nsp = newsk->sk_security;
3597 struct smk_audit_info ad;
3600 struct lsm_network_audit net;
3603 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3607 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3608 smk_ad_setfield_u_net_sk(&ad, other);
3610 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3611 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3615 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3616 rc = smk_bu_note("UDS connect", okp, skp,
3622 * Cross reference the peer labels for SO_PEERSEC.
3625 nsp->smk_packet = ssp->smk_out;
3626 ssp->smk_packet = osp->smk_out;
3633 * smack_unix_may_send - Smack access on UDS
3635 * @other: the other socket
3637 * Return 0 if a subject with the smack of sock could access
3638 * an object with the smack of other, otherwise an error code
3640 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3642 struct socket_smack *ssp = sock->sk->sk_security;
3643 struct socket_smack *osp = other->sk->sk_security;
3644 struct smk_audit_info ad;
3648 struct lsm_network_audit net;
3650 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3651 smk_ad_setfield_u_net_sk(&ad, other->sk);
3654 if (smack_privileged(CAP_MAC_OVERRIDE))
3657 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3658 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3663 * smack_socket_sendmsg - Smack check based on destination host
3666 * @size: the size of the message
3668 * Return 0 if the current subject can write to the destination host.
3669 * For IPv4 this is only a question if the destination is a single label host.
3670 * For IPv6 this is a check against the label of the port.
3672 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3675 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3676 #if IS_ENABLED(CONFIG_IPV6)
3677 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3679 #ifdef SMACK_IPV6_SECMARK_LABELING
3680 struct socket_smack *ssp = sock->sk->sk_security;
3681 struct smack_known *rsp;
3686 * Perfectly reasonable for this to be NULL
3691 switch (sock->sk->sk_family) {
3693 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3694 sip->sin_family != AF_INET)
3696 rc = smack_netlabel_send(sock->sk, sip);
3698 #if IS_ENABLED(CONFIG_IPV6)
3700 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3701 sap->sin6_family != AF_INET6)
3703 #ifdef SMACK_IPV6_SECMARK_LABELING
3704 rsp = smack_ipv6host_label(sap);
3706 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3709 #ifdef SMACK_IPV6_PORT_LABELING
3710 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3712 #endif /* IS_ENABLED(CONFIG_IPV6) */
3719 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3720 * @sap: netlabel secattr
3721 * @ssp: socket security information
3723 * Returns a pointer to a Smack label entry found on the label list.
3725 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3726 struct socket_smack *ssp)
3728 struct smack_known *skp;
3733 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3735 * Looks like a CIPSO packet.
3736 * If there are flags but no level netlabel isn't
3737 * behaving the way we expect it to.
3739 * Look it up in the label table
3740 * Without guidance regarding the smack value
3741 * for the packet fall back on the network
3745 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3746 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3749 * Compare the catsets. Use the netlbl APIs.
3751 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3752 if ((skp->smk_netlabel.flags &
3753 NETLBL_SECATTR_MLS_CAT) == 0)
3757 for (acat = -1, kcat = -1; acat == kcat; ) {
3758 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3760 kcat = netlbl_catmap_walk(
3761 skp->smk_netlabel.attr.mls.cat,
3763 if (acat < 0 || kcat < 0)
3776 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3777 return &smack_known_web;
3778 return &smack_known_star;
3780 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3782 * Looks like a fallback, which gives us a secid.
3784 return smack_from_secid(sap->attr.secid);
3786 * Without guidance regarding the smack value
3787 * for the packet fall back on the network
3790 return smack_net_ambient;
3793 #if IS_ENABLED(CONFIG_IPV6)
3794 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3798 int proto = -EINVAL;
3799 struct ipv6hdr _ipv6h;
3800 struct ipv6hdr *ip6;
3802 struct tcphdr _tcph, *th;
3803 struct udphdr _udph, *uh;
3804 struct dccp_hdr _dccph, *dh;
3808 offset = skb_network_offset(skb);
3809 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3812 sip->sin6_addr = ip6->saddr;
3814 nexthdr = ip6->nexthdr;
3815 offset += sizeof(_ipv6h);
3816 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3823 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3825 sip->sin6_port = th->source;
3828 case IPPROTO_UDPLITE:
3829 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3831 sip->sin6_port = uh->source;
3834 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3836 sip->sin6_port = dh->dccph_sport;
3841 #endif /* CONFIG_IPV6 */
3844 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3848 * Returns 0 if the packet should be delivered, an error code otherwise
3850 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3852 struct netlbl_lsm_secattr secattr;
3853 struct socket_smack *ssp = sk->sk_security;
3854 struct smack_known *skp = NULL;
3856 struct smk_audit_info ad;
3857 u16 family = sk->sk_family;
3859 struct lsm_network_audit net;
3861 #if IS_ENABLED(CONFIG_IPV6)
3862 struct sockaddr_in6 sadd;
3865 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3867 #endif /* CONFIG_IPV6 */
3871 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3873 * If there is a secmark use it rather than the CIPSO label.
3874 * If there is no secmark fall back to CIPSO.
3875 * The secmark is assumed to reflect policy better.
3877 if (skb && skb->secmark != 0) {
3878 skp = smack_from_secid(skb->secmark);
3881 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3883 * Translate what netlabel gave us.
3885 netlbl_secattr_init(&secattr);
3887 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3889 skp = smack_from_secattr(&secattr, ssp);
3891 skp = smack_net_ambient;
3893 netlbl_secattr_destroy(&secattr);
3895 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3899 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3900 ad.a.u.net->family = family;
3901 ad.a.u.net->netif = skb->skb_iif;
3902 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3905 * Receiving a packet requires that the other end
3906 * be able to write here. Read access is not required.
3907 * This is the simplist possible security model
3910 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3911 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3914 netlbl_skbuff_err(skb, family, rc, 0);
3916 #if IS_ENABLED(CONFIG_IPV6)
3918 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3919 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3920 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3922 #ifdef SMACK_IPV6_SECMARK_LABELING
3923 if (skb && skb->secmark != 0)
3924 skp = smack_from_secid(skb->secmark);
3925 else if (smk_ipv6_localhost(&sadd))
3928 skp = smack_ipv6host_label(&sadd);
3930 skp = smack_net_ambient;
3932 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3933 ad.a.u.net->family = family;
3934 ad.a.u.net->netif = skb->skb_iif;
3935 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3936 #endif /* CONFIG_AUDIT */
3937 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3938 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3940 #endif /* SMACK_IPV6_SECMARK_LABELING */
3941 #ifdef SMACK_IPV6_PORT_LABELING
3942 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3943 #endif /* SMACK_IPV6_PORT_LABELING */
3945 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3946 ICMPV6_ADM_PROHIBITED, 0);
3948 #endif /* CONFIG_IPV6 */
3955 * smack_socket_getpeersec_stream - pull in packet label
3957 * @optval: user's destination
3958 * @optlen: size thereof
3961 * returns zero on success, an error code otherwise
3963 static int smack_socket_getpeersec_stream(struct socket *sock,
3964 char __user *optval,
3965 int __user *optlen, unsigned len)
3967 struct socket_smack *ssp;
3972 ssp = sock->sk->sk_security;
3973 if (ssp->smk_packet != NULL) {
3974 rcp = ssp->smk_packet->smk_known;
3975 slen = strlen(rcp) + 1;
3980 else if (copy_to_user(optval, rcp, slen) != 0)
3983 if (put_user(slen, optlen) != 0)
3991 * smack_socket_getpeersec_dgram - pull in packet label
3992 * @sock: the peer socket
3994 * @secid: pointer to where to put the secid of the packet
3996 * Sets the netlabel socket state on sk from parent
3998 static int smack_socket_getpeersec_dgram(struct socket *sock,
3999 struct sk_buff *skb, u32 *secid)
4002 struct netlbl_lsm_secattr secattr;
4003 struct socket_smack *ssp = NULL;
4004 struct smack_known *skp;
4005 int family = PF_UNSPEC;
4006 u32 s = 0; /* 0 is the invalid secid */
4010 if (skb->protocol == htons(ETH_P_IP))
4012 #if IS_ENABLED(CONFIG_IPV6)
4013 else if (skb->protocol == htons(ETH_P_IPV6))
4015 #endif /* CONFIG_IPV6 */
4017 if (family == PF_UNSPEC && sock != NULL)
4018 family = sock->sk->sk_family;
4022 ssp = sock->sk->sk_security;
4023 s = ssp->smk_out->smk_secid;
4026 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4032 * Translate what netlabel gave us.
4034 if (sock != NULL && sock->sk != NULL)
4035 ssp = sock->sk->sk_security;
4036 netlbl_secattr_init(&secattr);
4037 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4039 skp = smack_from_secattr(&secattr, ssp);
4042 netlbl_secattr_destroy(&secattr);
4045 #ifdef SMACK_IPV6_SECMARK_LABELING
4057 * smack_sock_graft - Initialize a newly created socket with an existing sock
4059 * @parent: parent socket
4061 * Set the smk_{in,out} state of an existing sock based on the process that
4062 * is creating the new socket.
4064 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4066 struct socket_smack *ssp;
4067 struct smack_known *skp = smk_of_current();
4070 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4073 ssp = sk->sk_security;
4076 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4080 * smack_inet_conn_request - Smack access check on connect
4081 * @sk: socket involved
4085 * Returns 0 if a task with the packet label could write to
4086 * the socket, otherwise an error code
4088 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4089 struct request_sock *req)
4091 u16 family = sk->sk_family;
4092 struct smack_known *skp;
4093 struct socket_smack *ssp = sk->sk_security;
4094 struct netlbl_lsm_secattr secattr;
4095 struct sockaddr_in addr;
4097 struct smack_known *hskp;
4099 struct smk_audit_info ad;
4101 struct lsm_network_audit net;
4104 #if IS_ENABLED(CONFIG_IPV6)
4105 if (family == PF_INET6) {
4107 * Handle mapped IPv4 packets arriving
4108 * via IPv6 sockets. Don't set up netlabel
4109 * processing on IPv6.
4111 if (skb->protocol == htons(ETH_P_IP))
4116 #endif /* CONFIG_IPV6 */
4118 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4120 * If there is a secmark use it rather than the CIPSO label.
4121 * If there is no secmark fall back to CIPSO.
4122 * The secmark is assumed to reflect policy better.
4124 if (skb && skb->secmark != 0) {
4125 skp = smack_from_secid(skb->secmark);
4128 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4130 netlbl_secattr_init(&secattr);
4131 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4133 skp = smack_from_secattr(&secattr, ssp);
4135 skp = &smack_known_huh;
4136 netlbl_secattr_destroy(&secattr);
4138 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4143 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4144 ad.a.u.net->family = family;
4145 ad.a.u.net->netif = skb->skb_iif;
4146 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4149 * Receiving a packet requires that the other end be able to write
4150 * here. Read access is not required.
4152 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4153 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4158 * Save the peer's label in the request_sock so we can later setup
4159 * smk_packet in the child socket so that SO_PEERCRED can report it.
4161 req->peer_secid = skp->smk_secid;
4164 * We need to decide if we want to label the incoming connection here
4165 * if we do we only need to label the request_sock and the stack will
4166 * propagate the wire-label to the sock when it is created.
4169 addr.sin_addr.s_addr = hdr->saddr;
4171 hskp = smack_ipv4host_label(&addr);
4175 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4177 netlbl_req_delattr(req);
4183 * smack_inet_csk_clone - Copy the connection information to the new socket
4184 * @sk: the new socket
4185 * @req: the connection's request_sock
4187 * Transfer the connection's peer label to the newly created socket.
4189 static void smack_inet_csk_clone(struct sock *sk,
4190 const struct request_sock *req)
4192 struct socket_smack *ssp = sk->sk_security;
4193 struct smack_known *skp;
4195 if (req->peer_secid != 0) {
4196 skp = smack_from_secid(req->peer_secid);
4197 ssp->smk_packet = skp;
4199 ssp->smk_packet = NULL;
4203 * Key management security hooks
4205 * Casey has not tested key support very heavily.
4206 * The permission check is most likely too restrictive.
4207 * If you care about keys please have a look.
4212 * smack_key_alloc - Set the key security blob
4214 * @cred: the credentials to use
4217 * No allocation required
4221 static int smack_key_alloc(struct key *key, const struct cred *cred,
4222 unsigned long flags)
4224 struct smack_known *skp = smk_of_task(smack_cred(cred));
4226 key->security = skp;
4231 * smack_key_free - Clear the key security blob
4234 * Clear the blob pointer
4236 static void smack_key_free(struct key *key)
4238 key->security = NULL;
4242 * smack_key_permission - Smack access on a key
4243 * @key_ref: gets to the object
4244 * @cred: the credentials to use
4245 * @perm: requested key permissions
4247 * Return 0 if the task has read and write to the object,
4248 * an error code otherwise
4250 static int smack_key_permission(key_ref_t key_ref,
4251 const struct cred *cred, unsigned perm)
4254 struct smk_audit_info ad;
4255 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4260 * Validate requested permissions
4262 if (perm & ~KEY_NEED_ALL)
4265 keyp = key_ref_to_ptr(key_ref);
4269 * If the key hasn't been initialized give it access so that
4272 if (keyp->security == NULL)
4275 * This should not occur
4280 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4284 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4285 ad.a.u.key_struct.key = keyp->serial;
4286 ad.a.u.key_struct.key_desc = keyp->description;
4288 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4289 request |= MAY_READ;
4290 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4291 request |= MAY_WRITE;
4292 rc = smk_access(tkp, keyp->security, request, &ad);
4293 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4298 * smack_key_getsecurity - Smack label tagging the key
4299 * @key points to the key to be queried
4300 * @_buffer points to a pointer that should be set to point to the
4301 * resulting string (if no label or an error occurs).
4302 * Return the length of the string (including terminating NUL) or -ve if
4304 * May also return 0 (and a NULL buffer pointer) if there is no label.
4306 static int smack_key_getsecurity(struct key *key, char **_buffer)
4308 struct smack_known *skp = key->security;
4312 if (key->security == NULL) {
4317 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4320 length = strlen(copy) + 1;
4326 #endif /* CONFIG_KEYS */
4331 * Audit requires a unique representation of each Smack specific
4332 * rule. This unique representation is used to distinguish the
4333 * object to be audited from remaining kernel objects and also
4334 * works as a glue between the audit hooks.
4336 * Since repository entries are added but never deleted, we'll use
4337 * the smack_known label address related to the given audit rule as
4338 * the needed unique representation. This also better fits the smack
4339 * model where nearly everything is a label.
4344 * smack_audit_rule_init - Initialize a smack audit rule
4345 * @field: audit rule fields given from user-space (audit.h)
4346 * @op: required testing operator (=, !=, >, <, ...)
4347 * @rulestr: smack label to be audited
4348 * @vrule: pointer to save our own audit rule representation
4350 * Prepare to audit cases where (@field @op @rulestr) is true.
4351 * The label to be audited is created if necessay.
4353 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4355 struct smack_known *skp;
4356 char **rule = (char **)vrule;
4359 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4362 if (op != Audit_equal && op != Audit_not_equal)
4365 skp = smk_import_entry(rulestr, 0);
4367 return PTR_ERR(skp);
4369 *rule = skp->smk_known;
4375 * smack_audit_rule_known - Distinguish Smack audit rules
4376 * @krule: rule of interest, in Audit kernel representation format
4378 * This is used to filter Smack rules from remaining Audit ones.
4379 * If it's proved that this rule belongs to us, the
4380 * audit_rule_match hook will be called to do the final judgement.
4382 static int smack_audit_rule_known(struct audit_krule *krule)
4384 struct audit_field *f;
4387 for (i = 0; i < krule->field_count; i++) {
4388 f = &krule->fields[i];
4390 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4398 * smack_audit_rule_match - Audit given object ?
4399 * @secid: security id for identifying the object to test
4400 * @field: audit rule flags given from user-space
4401 * @op: required testing operator
4402 * @vrule: smack internal rule presentation
4404 * The core Audit hook. It's used to take the decision of
4405 * whether to audit or not to audit a given object.
4407 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4409 struct smack_known *skp;
4412 if (unlikely(!rule)) {
4413 WARN_ONCE(1, "Smack: missing rule\n");
4417 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4420 skp = smack_from_secid(secid);
4423 * No need to do string comparisons. If a match occurs,
4424 * both pointers will point to the same smack_known
4427 if (op == Audit_equal)
4428 return (rule == skp->smk_known);
4429 if (op == Audit_not_equal)
4430 return (rule != skp->smk_known);
4436 * There is no need for a smack_audit_rule_free hook.
4437 * No memory was allocated.
4440 #endif /* CONFIG_AUDIT */
4443 * smack_ismaclabel - check if xattr @name references a smack MAC label
4444 * @name: Full xattr name to check.
4446 static int smack_ismaclabel(const char *name)
4448 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4453 * smack_secid_to_secctx - return the smack label for a secid
4454 * @secid: incoming integer
4455 * @secdata: destination
4456 * @seclen: how long it is
4458 * Exists for networking code.
4460 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4462 struct smack_known *skp = smack_from_secid(secid);
4465 *secdata = skp->smk_known;
4466 *seclen = strlen(skp->smk_known);
4471 * smack_secctx_to_secid - return the secid for a smack label
4472 * @secdata: smack label
4473 * @seclen: how long result is
4474 * @secid: outgoing integer
4476 * Exists for audit and networking code.
4478 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4480 struct smack_known *skp = smk_find_entry(secdata);
4483 *secid = skp->smk_secid;
4490 * There used to be a smack_release_secctx hook
4491 * that did nothing back when hooks were in a vector.
4492 * Now that there's a list such a hook adds cost.
4495 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4497 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4500 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4502 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4505 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4507 struct smack_known *skp = smk_of_inode(inode);
4509 *ctx = skp->smk_known;
4510 *ctxlen = strlen(skp->smk_known);
4514 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4517 struct task_smack *tsp;
4518 struct smack_known *skp;
4519 struct inode_smack *isp;
4520 struct cred *new_creds = *new;
4522 if (new_creds == NULL) {
4523 new_creds = prepare_creds();
4524 if (new_creds == NULL)
4528 tsp = smack_cred(new_creds);
4531 * Get label from overlay inode and set it in create_sid
4533 isp = smack_inode(d_inode(dentry->d_parent));
4534 skp = isp->smk_inode;
4535 tsp->smk_task = skp;
4540 static int smack_inode_copy_up_xattr(const char *name)
4543 * Return 1 if this is the smack access Smack attribute.
4545 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4551 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4553 const struct cred *old,
4556 struct task_smack *otsp = smack_cred(old);
4557 struct task_smack *ntsp = smack_cred(new);
4558 struct inode_smack *isp;
4562 * Use the process credential unless all of
4563 * the transmuting criteria are met
4565 ntsp->smk_task = otsp->smk_task;
4568 * the attribute of the containing directory
4570 isp = smack_inode(d_inode(dentry->d_parent));
4572 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4574 may = smk_access_entry(otsp->smk_task->smk_known,
4575 isp->smk_inode->smk_known,
4576 &otsp->smk_task->smk_rules);
4580 * If the directory is transmuting and the rule
4581 * providing access is transmuting use the containing
4582 * directory label instead of the process label.
4584 if (may > 0 && (may & MAY_TRANSMUTE))
4585 ntsp->smk_task = isp->smk_inode;
4590 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4591 .lbs_cred = sizeof(struct task_smack),
4592 .lbs_file = sizeof(struct smack_known *),
4593 .lbs_inode = sizeof(struct inode_smack),
4594 .lbs_ipc = sizeof(struct smack_known *),
4595 .lbs_msg_msg = sizeof(struct smack_known *),
4598 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4599 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4600 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4601 LSM_HOOK_INIT(syslog, smack_syslog),
4603 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4604 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4606 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4607 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4608 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4609 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4610 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4611 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4613 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4615 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4616 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4617 LSM_HOOK_INIT(inode_link, smack_inode_link),
4618 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4619 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4620 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4621 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4622 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4623 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4624 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4625 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4626 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4627 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4628 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4629 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4630 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4631 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4633 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4634 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4635 LSM_HOOK_INIT(file_lock, smack_file_lock),
4636 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4637 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4638 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4639 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4640 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4641 LSM_HOOK_INIT(file_receive, smack_file_receive),
4643 LSM_HOOK_INIT(file_open, smack_file_open),
4645 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4646 LSM_HOOK_INIT(cred_free, smack_cred_free),
4647 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4648 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4649 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4650 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4651 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4652 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4653 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4654 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4655 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4656 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4657 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4658 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4659 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4660 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4661 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4662 LSM_HOOK_INIT(task_kill, smack_task_kill),
4663 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4665 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4666 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4668 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4670 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4671 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4672 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4673 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4674 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4676 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4677 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4678 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4679 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4681 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4682 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4683 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4684 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4686 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4688 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4689 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4691 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4692 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4694 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4695 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4696 #ifdef SMACK_IPV6_PORT_LABELING
4697 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4699 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4700 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4701 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4702 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4703 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4704 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4705 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4706 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4707 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4708 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4710 /* key management security hooks */
4712 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4713 LSM_HOOK_INIT(key_free, smack_key_free),
4714 LSM_HOOK_INIT(key_permission, smack_key_permission),
4715 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4716 #endif /* CONFIG_KEYS */
4720 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4721 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4722 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4723 #endif /* CONFIG_AUDIT */
4725 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4726 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4727 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4728 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4729 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4730 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4731 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4732 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4733 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4737 static __init void init_smack_known_list(void)
4740 * Initialize rule list locks
4742 mutex_init(&smack_known_huh.smk_rules_lock);
4743 mutex_init(&smack_known_hat.smk_rules_lock);
4744 mutex_init(&smack_known_floor.smk_rules_lock);
4745 mutex_init(&smack_known_star.smk_rules_lock);
4746 mutex_init(&smack_known_web.smk_rules_lock);
4748 * Initialize rule lists
4750 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4751 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4752 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4753 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4754 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4756 * Create the known labels list
4758 smk_insert_entry(&smack_known_huh);
4759 smk_insert_entry(&smack_known_hat);
4760 smk_insert_entry(&smack_known_star);
4761 smk_insert_entry(&smack_known_floor);
4762 smk_insert_entry(&smack_known_web);
4766 * smack_init - initialize the smack system
4770 static __init int smack_init(void)
4772 struct cred *cred = (struct cred *) current->cred;
4773 struct task_smack *tsp;
4775 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4776 if (!smack_inode_cache)
4779 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4780 if (!smack_rule_cache) {
4781 kmem_cache_destroy(smack_inode_cache);
4786 * Set the security state for the initial task.
4788 tsp = smack_cred(cred);
4789 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4794 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4797 pr_info("Smack: Initializing.\n");
4798 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4799 pr_info("Smack: Netfilter enabled.\n");
4801 #ifdef SMACK_IPV6_PORT_LABELING
4802 pr_info("Smack: IPv6 port labeling enabled.\n");
4804 #ifdef SMACK_IPV6_SECMARK_LABELING
4805 pr_info("Smack: IPv6 Netfilter enabled.\n");
4808 /* initialize the smack_known_list */
4809 init_smack_known_list();
4815 * Smack requires early initialization in order to label
4816 * all processes and objects when they are created.
4818 DEFINE_LSM(smack) = {
4820 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4821 .blobs = &smack_blob_sizes,