1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
8 * Casey Schaufler <casey@schaufler-ca.com>
9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
13 * Paul Moore <paul@paul-moore.com>
14 * Copyright (C) 2010 Nokia Corporation
15 * Copyright (C) 2011 Intel Corporation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
23 #include <asm/ioctls.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/dccp.h>
28 #include <linux/icmpv6.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
38 #include <linux/msg.h>
39 #include <linux/shm.h>
40 #include <linux/binfmts.h>
41 #include <linux/parser.h>
42 #include <linux/fs_context.h>
43 #include <linux/fs_parser.h>
44 #include <linux/watch_queue.h>
47 #define TRANS_TRUE "TRUE"
48 #define TRANS_TRUE_SIZE 4
50 #define SMK_CONNECTING 0
51 #define SMK_RECEIVING 1
54 #ifdef SMACK_IPV6_PORT_LABELING
55 DEFINE_MUTEX(smack_ipv6_lock);
56 static LIST_HEAD(smk_ipv6_port_list);
58 static struct kmem_cache *smack_inode_cache;
59 struct kmem_cache *smack_rule_cache;
62 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
67 } smk_mount_opts[] = {
68 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
69 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
73 static int match_opt_prefix(char *s, int l, char **arg)
77 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
78 size_t len = smk_mount_opts[i].len;
79 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
81 if (len == l || s[len] != '=')
84 return smk_mount_opts[i].opt;
89 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
90 static char *smk_bu_mess[] = {
91 "Bringup Error", /* Unused */
92 "Bringup", /* SMACK_BRINGUP_ALLOW */
93 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
94 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
97 static void smk_bu_mode(int mode, char *s)
103 if (mode & MAY_WRITE)
107 if (mode & MAY_APPEND)
109 if (mode & MAY_TRANSMUTE)
119 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
120 static int smk_bu_note(char *note, struct smack_known *sskp,
121 struct smack_known *oskp, int mode, int rc)
123 char acc[SMK_NUM_ACCESS_TYPE + 1];
127 if (rc > SMACK_UNCONFINED_OBJECT)
130 smk_bu_mode(mode, acc);
131 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
132 sskp->smk_known, oskp->smk_known, acc, note);
136 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
139 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
140 static int smk_bu_current(char *note, struct smack_known *oskp,
143 struct task_smack *tsp = smack_cred(current_cred());
144 char acc[SMK_NUM_ACCESS_TYPE + 1];
148 if (rc > SMACK_UNCONFINED_OBJECT)
151 smk_bu_mode(mode, acc);
152 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
153 tsp->smk_task->smk_known, oskp->smk_known,
154 acc, current->comm, note);
158 #define smk_bu_current(note, oskp, mode, RC) (RC)
161 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
162 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
164 struct task_smack *tsp = smack_cred(current_cred());
165 struct smack_known *smk_task = smk_of_task_struct(otp);
166 char acc[SMK_NUM_ACCESS_TYPE + 1];
170 if (rc > SMACK_UNCONFINED_OBJECT)
173 smk_bu_mode(mode, acc);
174 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
175 tsp->smk_task->smk_known, smk_task->smk_known, acc,
176 current->comm, otp->comm);
180 #define smk_bu_task(otp, mode, RC) (RC)
183 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
184 static int smk_bu_inode(struct inode *inode, int mode, int rc)
186 struct task_smack *tsp = smack_cred(current_cred());
187 struct inode_smack *isp = smack_inode(inode);
188 char acc[SMK_NUM_ACCESS_TYPE + 1];
190 if (isp->smk_flags & SMK_INODE_IMPURE)
191 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
192 inode->i_sb->s_id, inode->i_ino, current->comm);
196 if (rc > SMACK_UNCONFINED_OBJECT)
198 if (rc == SMACK_UNCONFINED_SUBJECT &&
199 (mode & (MAY_WRITE | MAY_APPEND)))
200 isp->smk_flags |= SMK_INODE_IMPURE;
202 smk_bu_mode(mode, acc);
204 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
205 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
206 inode->i_sb->s_id, inode->i_ino, current->comm);
210 #define smk_bu_inode(inode, mode, RC) (RC)
213 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
214 static int smk_bu_file(struct file *file, int mode, int rc)
216 struct task_smack *tsp = smack_cred(current_cred());
217 struct smack_known *sskp = tsp->smk_task;
218 struct inode *inode = file_inode(file);
219 struct inode_smack *isp = smack_inode(inode);
220 char acc[SMK_NUM_ACCESS_TYPE + 1];
222 if (isp->smk_flags & SMK_INODE_IMPURE)
223 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
224 inode->i_sb->s_id, inode->i_ino, current->comm);
228 if (rc > SMACK_UNCONFINED_OBJECT)
231 smk_bu_mode(mode, acc);
232 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
233 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
234 inode->i_sb->s_id, inode->i_ino, file,
239 #define smk_bu_file(file, mode, RC) (RC)
242 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
243 static int smk_bu_credfile(const struct cred *cred, struct file *file,
246 struct task_smack *tsp = smack_cred(cred);
247 struct smack_known *sskp = tsp->smk_task;
248 struct inode *inode = file_inode(file);
249 struct inode_smack *isp = smack_inode(inode);
250 char acc[SMK_NUM_ACCESS_TYPE + 1];
252 if (isp->smk_flags & SMK_INODE_IMPURE)
253 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
254 inode->i_sb->s_id, inode->i_ino, current->comm);
258 if (rc > SMACK_UNCONFINED_OBJECT)
261 smk_bu_mode(mode, acc);
262 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
263 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
264 inode->i_sb->s_id, inode->i_ino, file,
269 #define smk_bu_credfile(cred, file, mode, RC) (RC)
273 * smk_fetch - Fetch the smack label from a file.
274 * @name: type of the label (attribute)
275 * @ip: a pointer to the inode
276 * @dp: a pointer to the dentry
278 * Returns a pointer to the master list entry for the Smack label,
279 * NULL if there was no label to fetch, or an error code.
281 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
286 struct smack_known *skp = NULL;
288 if (!(ip->i_opflags & IOP_XATTR))
289 return ERR_PTR(-EOPNOTSUPP);
291 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
293 return ERR_PTR(-ENOMEM);
295 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
301 skp = smk_import_entry(buffer, rc);
309 * init_inode_smack - initialize an inode security blob
310 * @inode: inode to extract the info from
311 * @skp: a pointer to the Smack label entry to use in the blob
314 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
316 struct inode_smack *isp = smack_inode(inode);
318 isp->smk_inode = skp;
320 mutex_init(&isp->smk_lock);
324 * init_task_smack - initialize a task security blob
325 * @tsp: blob to initialize
326 * @task: a pointer to the Smack label for the running task
327 * @forked: a pointer to the Smack label for the forked task
330 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
331 struct smack_known *forked)
333 tsp->smk_task = task;
334 tsp->smk_forked = forked;
335 INIT_LIST_HEAD(&tsp->smk_rules);
336 INIT_LIST_HEAD(&tsp->smk_relabel);
337 mutex_init(&tsp->smk_rules_lock);
341 * smk_copy_rules - copy a rule set
342 * @nhead: new rules header pointer
343 * @ohead: old rules header pointer
344 * @gfp: type of the memory for the allocation
346 * Returns 0 on success, -ENOMEM on error
348 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
351 struct smack_rule *nrp;
352 struct smack_rule *orp;
355 list_for_each_entry_rcu(orp, ohead, list) {
356 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
362 list_add_rcu(&nrp->list, nhead);
368 * smk_copy_relabel - copy smk_relabel labels list
369 * @nhead: new rules header pointer
370 * @ohead: old rules header pointer
371 * @gfp: type of the memory for the allocation
373 * Returns 0 on success, -ENOMEM on error
375 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
378 struct smack_known_list_elem *nklep;
379 struct smack_known_list_elem *oklep;
381 list_for_each_entry(oklep, ohead, list) {
382 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
384 smk_destroy_label_list(nhead);
387 nklep->smk_label = oklep->smk_label;
388 list_add(&nklep->list, nhead);
395 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
396 * @mode - input mode in form of PTRACE_MODE_*
398 * Returns a converted MAY_* mode usable by smack rules
400 static inline unsigned int smk_ptrace_mode(unsigned int mode)
402 if (mode & PTRACE_MODE_ATTACH)
403 return MAY_READWRITE;
404 if (mode & PTRACE_MODE_READ)
411 * smk_ptrace_rule_check - helper for ptrace access
412 * @tracer: tracer process
413 * @tracee_known: label entry of the process that's about to be traced
414 * @mode: ptrace attachment mode (PTRACE_MODE_*)
415 * @func: name of the function that called us, used for audit
417 * Returns 0 on access granted, -error on error
419 static int smk_ptrace_rule_check(struct task_struct *tracer,
420 struct smack_known *tracee_known,
421 unsigned int mode, const char *func)
424 struct smk_audit_info ad, *saip = NULL;
425 struct task_smack *tsp;
426 struct smack_known *tracer_known;
427 const struct cred *tracercred;
429 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
430 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
431 smk_ad_setfield_u_tsk(&ad, tracer);
436 tracercred = __task_cred(tracer);
437 tsp = smack_cred(tracercred);
438 tracer_known = smk_of_task(tsp);
440 if ((mode & PTRACE_MODE_ATTACH) &&
441 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
442 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
443 if (tracer_known->smk_known == tracee_known->smk_known)
445 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
447 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
453 smack_log(tracer_known->smk_known,
454 tracee_known->smk_known,
461 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
462 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
470 * We he, that is fun!
474 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
475 * @ctp: child task pointer
476 * @mode: ptrace attachment mode (PTRACE_MODE_*)
478 * Returns 0 if access is OK, an error code otherwise
480 * Do the capability checks.
482 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
484 struct smack_known *skp;
486 skp = smk_of_task_struct(ctp);
488 return smk_ptrace_rule_check(current, skp, mode, __func__);
492 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
493 * @ptp: parent task pointer
495 * Returns 0 if access is OK, an error code otherwise
497 * Do the capability checks, and require PTRACE_MODE_ATTACH.
499 static int smack_ptrace_traceme(struct task_struct *ptp)
502 struct smack_known *skp;
504 skp = smk_of_task(smack_cred(current_cred()));
506 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
511 * smack_syslog - Smack approval on syslog
512 * @typefrom_file: unused
514 * Returns 0 on success, error code otherwise.
516 static int smack_syslog(int typefrom_file)
519 struct smack_known *skp = smk_of_current();
521 if (smack_privileged(CAP_MAC_OVERRIDE))
524 if (smack_syslog_label != NULL && smack_syslog_label != skp)
535 * smack_sb_alloc_security - allocate a superblock blob
536 * @sb: the superblock getting the blob
538 * Returns 0 on success or -ENOMEM on error.
540 static int smack_sb_alloc_security(struct super_block *sb)
542 struct superblock_smack *sbsp;
544 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
549 sbsp->smk_root = &smack_known_floor;
550 sbsp->smk_default = &smack_known_floor;
551 sbsp->smk_floor = &smack_known_floor;
552 sbsp->smk_hat = &smack_known_hat;
554 * SMK_SB_INITIALIZED will be zero from kzalloc.
556 sb->s_security = sbsp;
562 * smack_sb_free_security - free a superblock blob
563 * @sb: the superblock getting the blob
566 static void smack_sb_free_security(struct super_block *sb)
568 kfree(sb->s_security);
569 sb->s_security = NULL;
572 struct smack_mnt_opts {
573 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
576 static void smack_free_mnt_opts(void *mnt_opts)
578 struct smack_mnt_opts *opts = mnt_opts;
579 kfree(opts->fsdefault);
580 kfree(opts->fsfloor);
583 kfree(opts->fstransmute);
587 static int smack_add_opt(int token, const char *s, void **mnt_opts)
589 struct smack_mnt_opts *opts = *mnt_opts;
592 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
621 case Opt_fstransmute:
622 if (opts->fstransmute)
624 opts->fstransmute = s;
630 pr_warn("Smack: duplicate mount options\n");
635 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
636 * @fc: The new filesystem context.
637 * @src_fc: The source filesystem context being duplicated.
639 * Returns 0 on success or -ENOMEM on error.
641 static int smack_fs_context_dup(struct fs_context *fc,
642 struct fs_context *src_fc)
644 struct smack_mnt_opts *dst, *src = src_fc->security;
649 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
654 if (src->fsdefault) {
655 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
660 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
665 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
670 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
674 if (src->fstransmute) {
675 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
676 if (!dst->fstransmute)
682 static const struct fs_parameter_spec smack_fs_parameters[] = {
683 fsparam_string("smackfsdef", Opt_fsdefault),
684 fsparam_string("smackfsdefault", Opt_fsdefault),
685 fsparam_string("smackfsfloor", Opt_fsfloor),
686 fsparam_string("smackfshat", Opt_fshat),
687 fsparam_string("smackfsroot", Opt_fsroot),
688 fsparam_string("smackfstransmute", Opt_fstransmute),
693 * smack_fs_context_parse_param - Parse a single mount parameter
694 * @fc: The new filesystem context being constructed.
695 * @param: The parameter.
697 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
700 static int smack_fs_context_parse_param(struct fs_context *fc,
701 struct fs_parameter *param)
703 struct fs_parse_result result;
706 opt = fs_parse(fc, smack_fs_parameters, param, &result);
710 rc = smack_add_opt(opt, param->string, &fc->security);
712 param->string = NULL;
716 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
718 char *from = options, *to = options;
722 char *next = strchr(from, ',');
731 token = match_opt_prefix(from, len, &arg);
732 if (token != Opt_error) {
733 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
734 rc = smack_add_opt(token, arg, mnt_opts);
738 smack_free_mnt_opts(*mnt_opts);
743 if (!first) { // copy with preceding comma
748 memmove(to, from, len);
761 * smack_set_mnt_opts - set Smack specific mount options
762 * @sb: the file system superblock
763 * @mnt_opts: Smack mount options
764 * @kern_flags: mount option from kernel space or user space
765 * @set_kern_flags: where to store converted mount opts
767 * Returns 0 on success, an error code on failure
769 * Allow filesystems with binary mount data to explicitly set Smack mount
772 static int smack_set_mnt_opts(struct super_block *sb,
774 unsigned long kern_flags,
775 unsigned long *set_kern_flags)
777 struct dentry *root = sb->s_root;
778 struct inode *inode = d_backing_inode(root);
779 struct superblock_smack *sp = sb->s_security;
780 struct inode_smack *isp;
781 struct smack_known *skp;
782 struct smack_mnt_opts *opts = mnt_opts;
783 bool transmute = false;
785 if (sp->smk_flags & SMK_SB_INITIALIZED)
788 if (inode->i_security == NULL) {
789 int rc = lsm_inode_alloc(inode);
795 if (!smack_privileged(CAP_MAC_ADMIN)) {
797 * Unprivileged mounts don't get to specify Smack values.
802 * Unprivileged mounts get root and default from the caller.
804 skp = smk_of_current();
806 sp->smk_default = skp;
808 * For a handful of fs types with no user-controlled
809 * backing store it's okay to trust security labels
810 * in the filesystem. The rest are untrusted.
812 if (sb->s_user_ns != &init_user_ns &&
813 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
814 sb->s_magic != RAMFS_MAGIC) {
816 sp->smk_flags |= SMK_SB_UNTRUSTED;
820 sp->smk_flags |= SMK_SB_INITIALIZED;
823 if (opts->fsdefault) {
824 skp = smk_import_entry(opts->fsdefault, 0);
827 sp->smk_default = skp;
830 skp = smk_import_entry(opts->fsfloor, 0);
836 skp = smk_import_entry(opts->fshat, 0);
842 skp = smk_import_entry(opts->fsroot, 0);
847 if (opts->fstransmute) {
848 skp = smk_import_entry(opts->fstransmute, 0);
857 * Initialize the root inode.
859 init_inode_smack(inode, sp->smk_root);
862 isp = smack_inode(inode);
863 isp->smk_flags |= SMK_INODE_TRANSMUTE;
870 * smack_sb_statfs - Smack check on statfs
871 * @dentry: identifies the file system in question
873 * Returns 0 if current can read the floor of the filesystem,
874 * and error code otherwise
876 static int smack_sb_statfs(struct dentry *dentry)
878 struct superblock_smack *sbp = dentry->d_sb->s_security;
880 struct smk_audit_info ad;
882 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
883 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
885 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
886 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
895 * smack_bprm_set_creds - set creds for exec
896 * @bprm: the exec information
898 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
900 static int smack_bprm_set_creds(struct linux_binprm *bprm)
902 struct inode *inode = file_inode(bprm->file);
903 struct task_smack *bsp = smack_cred(bprm->cred);
904 struct inode_smack *isp;
905 struct superblock_smack *sbsp;
908 if (bprm->called_set_creds)
911 isp = smack_inode(inode);
912 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
915 sbsp = inode->i_sb->s_security;
916 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
917 isp->smk_task != sbsp->smk_root)
920 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
921 struct task_struct *tracer;
925 tracer = ptrace_parent(current);
926 if (likely(tracer != NULL))
927 rc = smk_ptrace_rule_check(tracer,
936 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
939 bsp->smk_task = isp->smk_task;
940 bprm->per_clear |= PER_CLEAR_ON_SETID;
942 /* Decide if this is a secure exec. */
943 if (bsp->smk_task != bsp->smk_forked)
944 bprm->secureexec = 1;
954 * smack_inode_alloc_security - allocate an inode blob
955 * @inode: the inode in need of a blob
959 static int smack_inode_alloc_security(struct inode *inode)
961 struct smack_known *skp = smk_of_current();
963 init_inode_smack(inode, skp);
968 * smack_inode_init_security - copy out the smack from an inode
969 * @inode: the newly created inode
970 * @dir: containing directory object
972 * @name: where to put the attribute name
973 * @value: where to put the attribute value
974 * @len: where to put the length of the attribute
976 * Returns 0 if it all works out, -ENOMEM if there's no memory
978 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
979 const struct qstr *qstr, const char **name,
980 void **value, size_t *len)
982 struct inode_smack *issp = smack_inode(inode);
983 struct smack_known *skp = smk_of_current();
984 struct smack_known *isp = smk_of_inode(inode);
985 struct smack_known *dsp = smk_of_inode(dir);
989 *name = XATTR_SMACK_SUFFIX;
993 may = smk_access_entry(skp->smk_known, dsp->smk_known,
998 * If the access rule allows transmutation and
999 * the directory requests transmutation then
1000 * by all means transmute.
1001 * Mark the inode as changed.
1003 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1004 smk_inode_transmutable(dir)) {
1006 issp->smk_flags |= SMK_INODE_CHANGED;
1009 *value = kstrdup(isp->smk_known, GFP_NOFS);
1013 *len = strlen(isp->smk_known);
1020 * smack_inode_link - Smack check on link
1021 * @old_dentry: the existing object
1023 * @new_dentry: the new object
1025 * Returns 0 if access is permitted, an error code otherwise
1027 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1028 struct dentry *new_dentry)
1030 struct smack_known *isp;
1031 struct smk_audit_info ad;
1034 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1035 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1037 isp = smk_of_inode(d_backing_inode(old_dentry));
1038 rc = smk_curacc(isp, MAY_WRITE, &ad);
1039 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1041 if (rc == 0 && d_is_positive(new_dentry)) {
1042 isp = smk_of_inode(d_backing_inode(new_dentry));
1043 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1044 rc = smk_curacc(isp, MAY_WRITE, &ad);
1045 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1052 * smack_inode_unlink - Smack check on inode deletion
1053 * @dir: containing directory object
1054 * @dentry: file to unlink
1056 * Returns 0 if current can write the containing directory
1057 * and the object, error code otherwise
1059 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1061 struct inode *ip = d_backing_inode(dentry);
1062 struct smk_audit_info ad;
1065 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1066 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1069 * You need write access to the thing you're unlinking
1071 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1072 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1075 * You also need write access to the containing directory
1077 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1078 smk_ad_setfield_u_fs_inode(&ad, dir);
1079 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1080 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1086 * smack_inode_rmdir - Smack check on directory deletion
1087 * @dir: containing directory object
1088 * @dentry: directory to unlink
1090 * Returns 0 if current can write the containing directory
1091 * and the directory, error code otherwise
1093 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1095 struct smk_audit_info ad;
1098 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1099 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1102 * You need write access to the thing you're removing
1104 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1105 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1108 * You also need write access to the containing directory
1110 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1111 smk_ad_setfield_u_fs_inode(&ad, dir);
1112 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1113 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1120 * smack_inode_rename - Smack check on rename
1121 * @old_inode: unused
1122 * @old_dentry: the old object
1123 * @new_inode: unused
1124 * @new_dentry: the new object
1126 * Read and write access is required on both the old and
1129 * Returns 0 if access is permitted, an error code otherwise
1131 static int smack_inode_rename(struct inode *old_inode,
1132 struct dentry *old_dentry,
1133 struct inode *new_inode,
1134 struct dentry *new_dentry)
1137 struct smack_known *isp;
1138 struct smk_audit_info ad;
1140 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1141 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1143 isp = smk_of_inode(d_backing_inode(old_dentry));
1144 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1145 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1147 if (rc == 0 && d_is_positive(new_dentry)) {
1148 isp = smk_of_inode(d_backing_inode(new_dentry));
1149 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1150 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1151 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1157 * smack_inode_permission - Smack version of permission()
1158 * @inode: the inode in question
1159 * @mask: the access requested
1161 * This is the important Smack hook.
1163 * Returns 0 if access is permitted, an error code otherwise
1165 static int smack_inode_permission(struct inode *inode, int mask)
1167 struct superblock_smack *sbsp = inode->i_sb->s_security;
1168 struct smk_audit_info ad;
1169 int no_block = mask & MAY_NOT_BLOCK;
1172 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1174 * No permission to check. Existence test. Yup, it's there.
1179 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1180 if (smk_of_inode(inode) != sbsp->smk_root)
1184 /* May be droppable after audit */
1187 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1188 smk_ad_setfield_u_fs_inode(&ad, inode);
1189 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1190 rc = smk_bu_inode(inode, mask, rc);
1195 * smack_inode_setattr - Smack check for setting attributes
1196 * @dentry: the object
1197 * @iattr: for the force flag
1199 * Returns 0 if access is permitted, an error code otherwise
1201 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1203 struct smk_audit_info ad;
1207 * Need to allow for clearing the setuid bit.
1209 if (iattr->ia_valid & ATTR_FORCE)
1211 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1212 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1214 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1215 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1220 * smack_inode_getattr - Smack check for getting attributes
1221 * @path: path to extract the info from
1223 * Returns 0 if access is permitted, an error code otherwise
1225 static int smack_inode_getattr(const struct path *path)
1227 struct smk_audit_info ad;
1228 struct inode *inode = d_backing_inode(path->dentry);
1231 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1232 smk_ad_setfield_u_fs_path(&ad, *path);
1233 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1234 rc = smk_bu_inode(inode, MAY_READ, rc);
1239 * smack_inode_setxattr - Smack check for setting xattrs
1240 * @dentry: the object
1241 * @name: name of the attribute
1242 * @value: value of the attribute
1243 * @size: size of the value
1246 * This protects the Smack attribute explicitly.
1248 * Returns 0 if access is permitted, an error code otherwise
1250 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1251 const void *value, size_t size, int flags)
1253 struct smk_audit_info ad;
1254 struct smack_known *skp;
1256 int check_import = 0;
1261 * Check label validity here so import won't fail in post_setxattr
1263 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1264 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1265 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1268 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1269 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1273 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1275 if (size != TRANS_TRUE_SIZE ||
1276 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1279 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1281 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1284 if (rc == 0 && check_import) {
1285 skp = size ? smk_import_entry(value, size) : NULL;
1288 else if (skp == NULL || (check_star &&
1289 (skp == &smack_known_star || skp == &smack_known_web)))
1293 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1294 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1297 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1298 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1305 * smack_inode_post_setxattr - Apply the Smack update approved above
1307 * @name: attribute name
1308 * @value: attribute value
1309 * @size: attribute size
1312 * Set the pointer in the inode blob to the entry found
1313 * in the master label list.
1315 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1316 const void *value, size_t size, int flags)
1318 struct smack_known *skp;
1319 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1321 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1322 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1326 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1327 skp = smk_import_entry(value, size);
1329 isp->smk_inode = skp;
1330 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1331 skp = smk_import_entry(value, size);
1333 isp->smk_task = skp;
1334 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1335 skp = smk_import_entry(value, size);
1337 isp->smk_mmap = skp;
1344 * smack_inode_getxattr - Smack check on getxattr
1345 * @dentry: the object
1348 * Returns 0 if access is permitted, an error code otherwise
1350 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1352 struct smk_audit_info ad;
1355 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1356 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1358 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1359 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1364 * smack_inode_removexattr - Smack check on removexattr
1365 * @dentry: the object
1366 * @name: name of the attribute
1368 * Removing the Smack attribute requires CAP_MAC_ADMIN
1370 * Returns 0 if access is permitted, an error code otherwise
1372 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1374 struct inode_smack *isp;
1375 struct smk_audit_info ad;
1378 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1379 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1380 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1381 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1382 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1383 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1384 if (!smack_privileged(CAP_MAC_ADMIN))
1387 rc = cap_inode_removexattr(dentry, name);
1392 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1393 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1395 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1396 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1400 isp = smack_inode(d_backing_inode(dentry));
1402 * Don't do anything special for these.
1403 * XATTR_NAME_SMACKIPIN
1404 * XATTR_NAME_SMACKIPOUT
1406 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1407 struct super_block *sbp = dentry->d_sb;
1408 struct superblock_smack *sbsp = sbp->s_security;
1410 isp->smk_inode = sbsp->smk_default;
1411 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1412 isp->smk_task = NULL;
1413 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1414 isp->smk_mmap = NULL;
1415 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1416 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1422 * smack_inode_getsecurity - get smack xattrs
1423 * @inode: the object
1424 * @name: attribute name
1425 * @buffer: where to put the result
1426 * @alloc: duplicate memory
1428 * Returns the size of the attribute or an error code
1430 static int smack_inode_getsecurity(struct inode *inode,
1431 const char *name, void **buffer,
1434 struct socket_smack *ssp;
1435 struct socket *sock;
1436 struct super_block *sbp;
1437 struct inode *ip = (struct inode *)inode;
1438 struct smack_known *isp;
1440 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1441 isp = smk_of_inode(inode);
1444 * The rest of the Smack xattrs are only on sockets.
1447 if (sbp->s_magic != SOCKFS_MAGIC)
1450 sock = SOCKET_I(ip);
1451 if (sock == NULL || sock->sk == NULL)
1454 ssp = sock->sk->sk_security;
1456 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1458 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1465 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1466 if (*buffer == NULL)
1470 return strlen(isp->smk_known);
1475 * smack_inode_listsecurity - list the Smack attributes
1476 * @inode: the object
1477 * @buffer: where they go
1478 * @buffer_size: size of buffer
1480 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1483 int len = sizeof(XATTR_NAME_SMACK);
1485 if (buffer != NULL && len <= buffer_size)
1486 memcpy(buffer, XATTR_NAME_SMACK, len);
1492 * smack_inode_getsecid - Extract inode's security id
1493 * @inode: inode to extract the info from
1494 * @secid: where result will be saved
1496 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1498 struct smack_known *skp = smk_of_inode(inode);
1500 *secid = skp->smk_secid;
1508 * There is no smack_file_permission hook
1510 * Should access checks be done on each read or write?
1511 * UNICOS and SELinux say yes.
1512 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1514 * I'll say no for now. Smack does not do the frequent
1515 * label changing that SELinux does.
1519 * smack_file_alloc_security - assign a file security blob
1522 * The security blob for a file is a pointer to the master
1523 * label list, so no allocation is done.
1525 * f_security is the owner security information. It
1526 * isn't used on file access checks, it's for send_sigio.
1530 static int smack_file_alloc_security(struct file *file)
1532 struct smack_known **blob = smack_file(file);
1534 *blob = smk_of_current();
1539 * smack_file_ioctl - Smack check on ioctls
1544 * Relies heavily on the correct use of the ioctl command conventions.
1546 * Returns 0 if allowed, error code otherwise
1548 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1552 struct smk_audit_info ad;
1553 struct inode *inode = file_inode(file);
1555 if (unlikely(IS_PRIVATE(inode)))
1558 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1559 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1561 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1562 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1563 rc = smk_bu_file(file, MAY_WRITE, rc);
1566 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1567 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1568 rc = smk_bu_file(file, MAY_READ, rc);
1575 * smack_file_lock - Smack check on file locking
1579 * Returns 0 if current has lock access, error code otherwise
1581 static int smack_file_lock(struct file *file, unsigned int cmd)
1583 struct smk_audit_info ad;
1585 struct inode *inode = file_inode(file);
1587 if (unlikely(IS_PRIVATE(inode)))
1590 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1591 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1592 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1593 rc = smk_bu_file(file, MAY_LOCK, rc);
1598 * smack_file_fcntl - Smack check on fcntl
1600 * @cmd: what action to check
1603 * Generally these operations are harmless.
1604 * File locking operations present an obvious mechanism
1605 * for passing information, so they require write access.
1607 * Returns 0 if current has access, error code otherwise
1609 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1612 struct smk_audit_info ad;
1614 struct inode *inode = file_inode(file);
1616 if (unlikely(IS_PRIVATE(inode)))
1624 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1625 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1626 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1627 rc = smk_bu_file(file, MAY_LOCK, rc);
1631 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1632 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1633 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1634 rc = smk_bu_file(file, MAY_WRITE, rc);
1645 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1646 * if mapping anonymous memory.
1647 * @file contains the file structure for file to map (may be NULL).
1648 * @reqprot contains the protection requested by the application.
1649 * @prot contains the protection that will be applied by the kernel.
1650 * @flags contains the operational flags.
1651 * Return 0 if permission is granted.
1653 static int smack_mmap_file(struct file *file,
1654 unsigned long reqprot, unsigned long prot,
1655 unsigned long flags)
1657 struct smack_known *skp;
1658 struct smack_known *mkp;
1659 struct smack_rule *srp;
1660 struct task_smack *tsp;
1661 struct smack_known *okp;
1662 struct inode_smack *isp;
1663 struct superblock_smack *sbsp;
1672 if (unlikely(IS_PRIVATE(file_inode(file))))
1675 isp = smack_inode(file_inode(file));
1676 if (isp->smk_mmap == NULL)
1678 sbsp = file_inode(file)->i_sb->s_security;
1679 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1680 isp->smk_mmap != sbsp->smk_root)
1682 mkp = isp->smk_mmap;
1684 tsp = smack_cred(current_cred());
1685 skp = smk_of_current();
1690 * For each Smack rule associated with the subject
1691 * label verify that the SMACK64MMAP also has access
1692 * to that rule's object label.
1694 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1695 okp = srp->smk_object;
1697 * Matching labels always allows access.
1699 if (mkp->smk_known == okp->smk_known)
1702 * If there is a matching local rule take
1703 * that into account as well.
1705 may = smk_access_entry(srp->smk_subject->smk_known,
1709 may = srp->smk_access;
1711 may &= srp->smk_access;
1713 * If may is zero the SMACK64MMAP subject can't
1714 * possibly have less access.
1720 * Fetch the global list entry.
1721 * If there isn't one a SMACK64MMAP subject
1722 * can't have as much access as current.
1724 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1726 if (mmay == -ENOENT) {
1731 * If there is a local entry it modifies the
1732 * potential access, too.
1734 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1736 if (tmay != -ENOENT)
1740 * If there is any access available to current that is
1741 * not available to a SMACK64MMAP subject
1744 if ((may | mmay) != mmay) {
1756 * smack_file_set_fowner - set the file security blob value
1757 * @file: object in question
1760 static void smack_file_set_fowner(struct file *file)
1762 struct smack_known **blob = smack_file(file);
1764 *blob = smk_of_current();
1768 * smack_file_send_sigiotask - Smack on sigio
1769 * @tsk: The target task
1770 * @fown: the object the signal come from
1773 * Allow a privileged task to get signals even if it shouldn't
1775 * Returns 0 if a subject with the object's smack could
1776 * write to the task, an error code otherwise.
1778 static int smack_file_send_sigiotask(struct task_struct *tsk,
1779 struct fown_struct *fown, int signum)
1781 struct smack_known **blob;
1782 struct smack_known *skp;
1783 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1784 const struct cred *tcred;
1787 struct smk_audit_info ad;
1790 * struct fown_struct is never outside the context of a struct file
1792 file = container_of(fown, struct file, f_owner);
1794 /* we don't log here as rc can be overriden */
1795 blob = smack_file(file);
1797 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1798 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1801 tcred = __task_cred(tsk);
1802 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1806 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1807 smk_ad_setfield_u_tsk(&ad, tsk);
1808 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1813 * smack_file_receive - Smack file receive check
1816 * Returns 0 if current has access, error code otherwise
1818 static int smack_file_receive(struct file *file)
1822 struct smk_audit_info ad;
1823 struct inode *inode = file_inode(file);
1824 struct socket *sock;
1825 struct task_smack *tsp;
1826 struct socket_smack *ssp;
1828 if (unlikely(IS_PRIVATE(inode)))
1831 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1832 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1834 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1835 sock = SOCKET_I(inode);
1836 ssp = sock->sk->sk_security;
1837 tsp = smack_cred(current_cred());
1839 * If the receiving process can't write to the
1840 * passed socket or if the passed socket can't
1841 * write to the receiving process don't accept
1842 * the passed socket.
1844 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1845 rc = smk_bu_file(file, may, rc);
1848 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1849 rc = smk_bu_file(file, may, rc);
1853 * This code relies on bitmasks.
1855 if (file->f_mode & FMODE_READ)
1857 if (file->f_mode & FMODE_WRITE)
1860 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1861 rc = smk_bu_file(file, may, rc);
1866 * smack_file_open - Smack dentry open processing
1869 * Set the security blob in the file structure.
1870 * Allow the open only if the task has read access. There are
1871 * many read operations (e.g. fstat) that you can do with an
1872 * fd even if you have the file open write-only.
1874 * Returns 0 if current has access, error code otherwise
1876 static int smack_file_open(struct file *file)
1878 struct task_smack *tsp = smack_cred(file->f_cred);
1879 struct inode *inode = file_inode(file);
1880 struct smk_audit_info ad;
1883 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1884 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1885 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1886 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1896 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1897 * @cred: the new credentials
1898 * @gfp: the atomicity of any memory allocations
1900 * Prepare a blank set of credentials for modification. This must allocate all
1901 * the memory the LSM module might require such that cred_transfer() can
1902 * complete without error.
1904 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1906 init_task_smack(smack_cred(cred), NULL, NULL);
1912 * smack_cred_free - "free" task-level security credentials
1913 * @cred: the credentials in question
1916 static void smack_cred_free(struct cred *cred)
1918 struct task_smack *tsp = smack_cred(cred);
1919 struct smack_rule *rp;
1920 struct list_head *l;
1921 struct list_head *n;
1923 smk_destroy_label_list(&tsp->smk_relabel);
1925 list_for_each_safe(l, n, &tsp->smk_rules) {
1926 rp = list_entry(l, struct smack_rule, list);
1927 list_del(&rp->list);
1928 kmem_cache_free(smack_rule_cache, rp);
1933 * smack_cred_prepare - prepare new set of credentials for modification
1934 * @new: the new credentials
1935 * @old: the original credentials
1936 * @gfp: the atomicity of any memory allocations
1938 * Prepare a new set of credentials for modification.
1940 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1943 struct task_smack *old_tsp = smack_cred(old);
1944 struct task_smack *new_tsp = smack_cred(new);
1947 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1949 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1953 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1959 * smack_cred_transfer - Transfer the old credentials to the new credentials
1960 * @new: the new credentials
1961 * @old: the original credentials
1963 * Fill in a set of blank credentials from another set of credentials.
1965 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1967 struct task_smack *old_tsp = smack_cred(old);
1968 struct task_smack *new_tsp = smack_cred(new);
1970 new_tsp->smk_task = old_tsp->smk_task;
1971 new_tsp->smk_forked = old_tsp->smk_task;
1972 mutex_init(&new_tsp->smk_rules_lock);
1973 INIT_LIST_HEAD(&new_tsp->smk_rules);
1975 /* cbs copy rule list */
1979 * smack_cred_getsecid - get the secid corresponding to a creds structure
1980 * @cred: the object creds
1981 * @secid: where to put the result
1983 * Sets the secid to contain a u32 version of the smack label.
1985 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1987 struct smack_known *skp;
1990 skp = smk_of_task(smack_cred(cred));
1991 *secid = skp->smk_secid;
1996 * smack_kernel_act_as - Set the subjective context in a set of credentials
1997 * @new: points to the set of credentials to be modified.
1998 * @secid: specifies the security ID to be set
2000 * Set the security data for a kernel service.
2002 static int smack_kernel_act_as(struct cred *new, u32 secid)
2004 struct task_smack *new_tsp = smack_cred(new);
2006 new_tsp->smk_task = smack_from_secid(secid);
2011 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2012 * @new: points to the set of credentials to be modified
2013 * @inode: points to the inode to use as a reference
2015 * Set the file creation context in a set of credentials to the same
2016 * as the objective context of the specified inode
2018 static int smack_kernel_create_files_as(struct cred *new,
2019 struct inode *inode)
2021 struct inode_smack *isp = smack_inode(inode);
2022 struct task_smack *tsp = smack_cred(new);
2024 tsp->smk_forked = isp->smk_inode;
2025 tsp->smk_task = tsp->smk_forked;
2030 * smk_curacc_on_task - helper to log task related access
2031 * @p: the task object
2032 * @access: the access requested
2033 * @caller: name of the calling function for audit
2035 * Return 0 if access is permitted
2037 static int smk_curacc_on_task(struct task_struct *p, int access,
2040 struct smk_audit_info ad;
2041 struct smack_known *skp = smk_of_task_struct(p);
2044 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2045 smk_ad_setfield_u_tsk(&ad, p);
2046 rc = smk_curacc(skp, access, &ad);
2047 rc = smk_bu_task(p, access, rc);
2052 * smack_task_setpgid - Smack check on setting pgid
2053 * @p: the task object
2056 * Return 0 if write access is permitted
2058 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2060 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2064 * smack_task_getpgid - Smack access check for getpgid
2065 * @p: the object task
2067 * Returns 0 if current can read the object task, error code otherwise
2069 static int smack_task_getpgid(struct task_struct *p)
2071 return smk_curacc_on_task(p, MAY_READ, __func__);
2075 * smack_task_getsid - Smack access check for getsid
2076 * @p: the object task
2078 * Returns 0 if current can read the object task, error code otherwise
2080 static int smack_task_getsid(struct task_struct *p)
2082 return smk_curacc_on_task(p, MAY_READ, __func__);
2086 * smack_task_getsecid - get the secid of the task
2087 * @p: the object task
2088 * @secid: where to put the result
2090 * Sets the secid to contain a u32 version of the smack label.
2092 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2094 struct smack_known *skp = smk_of_task_struct(p);
2096 *secid = skp->smk_secid;
2100 * smack_task_setnice - Smack check on setting nice
2101 * @p: the task object
2104 * Return 0 if write access is permitted
2106 static int smack_task_setnice(struct task_struct *p, int nice)
2108 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2112 * smack_task_setioprio - Smack check on setting ioprio
2113 * @p: the task object
2116 * Return 0 if write access is permitted
2118 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2120 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2124 * smack_task_getioprio - Smack check on reading ioprio
2125 * @p: the task object
2127 * Return 0 if read access is permitted
2129 static int smack_task_getioprio(struct task_struct *p)
2131 return smk_curacc_on_task(p, MAY_READ, __func__);
2135 * smack_task_setscheduler - Smack check on setting scheduler
2136 * @p: the task object
2138 * Return 0 if read access is permitted
2140 static int smack_task_setscheduler(struct task_struct *p)
2142 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2146 * smack_task_getscheduler - Smack check on reading scheduler
2147 * @p: the task object
2149 * Return 0 if read access is permitted
2151 static int smack_task_getscheduler(struct task_struct *p)
2153 return smk_curacc_on_task(p, MAY_READ, __func__);
2157 * smack_task_movememory - Smack check on moving memory
2158 * @p: the task object
2160 * Return 0 if write access is permitted
2162 static int smack_task_movememory(struct task_struct *p)
2164 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2168 * smack_task_kill - Smack check on signal delivery
2169 * @p: the task object
2172 * @cred: identifies the cred to use in lieu of current's
2174 * Return 0 if write access is permitted
2177 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2178 int sig, const struct cred *cred)
2180 struct smk_audit_info ad;
2181 struct smack_known *skp;
2182 struct smack_known *tkp = smk_of_task_struct(p);
2186 return 0; /* null signal; existence test */
2188 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2189 smk_ad_setfield_u_tsk(&ad, p);
2191 * Sending a signal requires that the sender
2192 * can write the receiver.
2195 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2196 rc = smk_bu_task(p, MAY_DELIVER, rc);
2200 * If the cred isn't NULL we're dealing with some USB IO
2201 * specific behavior. This is not clean. For one thing
2202 * we can't take privilege into account.
2204 skp = smk_of_task(smack_cred(cred));
2205 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2206 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2211 * smack_task_to_inode - copy task smack into the inode blob
2212 * @p: task to copy from
2213 * @inode: inode to copy to
2215 * Sets the smack pointer in the inode security blob
2217 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2219 struct inode_smack *isp = smack_inode(inode);
2220 struct smack_known *skp = smk_of_task_struct(p);
2222 isp->smk_inode = skp;
2223 isp->smk_flags |= SMK_INODE_INSTANT;
2231 * smack_sk_alloc_security - Allocate a socket blob
2234 * @gfp_flags: memory allocation flags
2236 * Assign Smack pointers to current
2238 * Returns 0 on success, -ENOMEM is there's no memory
2240 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2242 struct smack_known *skp = smk_of_current();
2243 struct socket_smack *ssp;
2245 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2250 * Sockets created by kernel threads receive web label.
2252 if (unlikely(current->flags & PF_KTHREAD)) {
2253 ssp->smk_in = &smack_known_web;
2254 ssp->smk_out = &smack_known_web;
2259 ssp->smk_packet = NULL;
2261 sk->sk_security = ssp;
2267 * smack_sk_free_security - Free a socket blob
2270 * Clears the blob pointer
2272 static void smack_sk_free_security(struct sock *sk)
2274 #ifdef SMACK_IPV6_PORT_LABELING
2275 struct smk_port_label *spp;
2277 if (sk->sk_family == PF_INET6) {
2279 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2280 if (spp->smk_sock != sk)
2282 spp->smk_can_reuse = 1;
2288 kfree(sk->sk_security);
2292 * smack_ipv4host_label - check host based restrictions
2293 * @sip: the object end
2295 * looks for host based access restrictions
2297 * This version will only be appropriate for really small sets of single label
2298 * hosts. The caller is responsible for ensuring that the RCU read lock is
2299 * taken before calling this function.
2301 * Returns the label of the far end or NULL if it's not special.
2303 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2305 struct smk_net4addr *snp;
2306 struct in_addr *siap = &sip->sin_addr;
2308 if (siap->s_addr == 0)
2311 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2313 * we break after finding the first match because
2314 * the list is sorted from longest to shortest mask
2315 * so we have found the most specific match
2317 if (snp->smk_host.s_addr ==
2318 (siap->s_addr & snp->smk_mask.s_addr))
2319 return snp->smk_label;
2324 #if IS_ENABLED(CONFIG_IPV6)
2326 * smk_ipv6_localhost - Check for local ipv6 host address
2329 * Returns boolean true if this is the localhost address
2331 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2333 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2334 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2336 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2337 ntohs(be16p[7]) == 1)
2343 * smack_ipv6host_label - check host based restrictions
2344 * @sip: the object end
2346 * looks for host based access restrictions
2348 * This version will only be appropriate for really small sets of single label
2349 * hosts. The caller is responsible for ensuring that the RCU read lock is
2350 * taken before calling this function.
2352 * Returns the label of the far end or NULL if it's not special.
2354 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2356 struct smk_net6addr *snp;
2357 struct in6_addr *sap = &sip->sin6_addr;
2362 * It's local. Don't look for a host label.
2364 if (smk_ipv6_localhost(sip))
2367 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2369 * If the label is NULL the entry has
2370 * been renounced. Ignore it.
2372 if (snp->smk_label == NULL)
2375 * we break after finding the first match because
2376 * the list is sorted from longest to shortest mask
2377 * so we have found the most specific match
2379 for (found = 1, i = 0; i < 8; i++) {
2380 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2381 snp->smk_host.s6_addr16[i]) {
2387 return snp->smk_label;
2392 #endif /* CONFIG_IPV6 */
2395 * smack_netlabel - Set the secattr on a socket
2397 * @labeled: socket label scheme
2399 * Convert the outbound smack value (smk_out) to a
2400 * secattr and attach it to the socket.
2402 * Returns 0 on success or an error code
2404 static int smack_netlabel(struct sock *sk, int labeled)
2406 struct smack_known *skp;
2407 struct socket_smack *ssp = sk->sk_security;
2411 * Usually the netlabel code will handle changing the
2412 * packet labeling based on the label.
2413 * The case of a single label host is different, because
2414 * a single label host should never get a labeled packet
2415 * even though the label is usually associated with a packet
2419 bh_lock_sock_nested(sk);
2421 if (ssp->smk_out == smack_net_ambient ||
2422 labeled == SMACK_UNLABELED_SOCKET)
2423 netlbl_sock_delattr(sk);
2426 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2436 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2438 * @sap: the destination address
2440 * Set the correct secattr for the given socket based on the destination
2441 * address and perform any outbound access checks needed.
2443 * Returns 0 on success or an error code.
2446 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2448 struct smack_known *skp;
2451 struct smack_known *hkp;
2452 struct socket_smack *ssp = sk->sk_security;
2453 struct smk_audit_info ad;
2456 hkp = smack_ipv4host_label(sap);
2459 struct lsm_network_audit net;
2461 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2462 ad.a.u.net->family = sap->sin_family;
2463 ad.a.u.net->dport = sap->sin_port;
2464 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2466 sk_lbl = SMACK_UNLABELED_SOCKET;
2468 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2469 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2471 sk_lbl = SMACK_CIPSO_SOCKET;
2478 return smack_netlabel(sk, sk_lbl);
2481 #if IS_ENABLED(CONFIG_IPV6)
2483 * smk_ipv6_check - check Smack access
2484 * @subject: subject Smack label
2485 * @object: object Smack label
2487 * @act: the action being taken
2489 * Check an IPv6 access
2491 static int smk_ipv6_check(struct smack_known *subject,
2492 struct smack_known *object,
2493 struct sockaddr_in6 *address, int act)
2496 struct lsm_network_audit net;
2498 struct smk_audit_info ad;
2502 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2503 ad.a.u.net->family = PF_INET6;
2504 ad.a.u.net->dport = ntohs(address->sin6_port);
2505 if (act == SMK_RECEIVING)
2506 ad.a.u.net->v6info.saddr = address->sin6_addr;
2508 ad.a.u.net->v6info.daddr = address->sin6_addr;
2510 rc = smk_access(subject, object, MAY_WRITE, &ad);
2511 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2514 #endif /* CONFIG_IPV6 */
2516 #ifdef SMACK_IPV6_PORT_LABELING
2518 * smk_ipv6_port_label - Smack port access table management
2522 * Create or update the port list entry
2524 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2526 struct sock *sk = sock->sk;
2527 struct sockaddr_in6 *addr6;
2528 struct socket_smack *ssp = sock->sk->sk_security;
2529 struct smk_port_label *spp;
2530 unsigned short port = 0;
2532 if (address == NULL) {
2534 * This operation is changing the Smack information
2535 * on the bound socket. Take the changes to the port
2539 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2540 if (sk != spp->smk_sock)
2542 spp->smk_in = ssp->smk_in;
2543 spp->smk_out = ssp->smk_out;
2548 * A NULL address is only used for updating existing
2549 * bound entries. If there isn't one, it's OK.
2555 addr6 = (struct sockaddr_in6 *)address;
2556 port = ntohs(addr6->sin6_port);
2558 * This is a special case that is safely ignored.
2564 * Look for an existing port list entry.
2565 * This is an indication that a port is getting reused.
2568 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2569 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2571 if (spp->smk_can_reuse != 1) {
2575 spp->smk_port = port;
2577 spp->smk_in = ssp->smk_in;
2578 spp->smk_out = ssp->smk_out;
2579 spp->smk_can_reuse = 0;
2585 * A new port entry is required.
2587 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2591 spp->smk_port = port;
2593 spp->smk_in = ssp->smk_in;
2594 spp->smk_out = ssp->smk_out;
2595 spp->smk_sock_type = sock->type;
2596 spp->smk_can_reuse = 0;
2598 mutex_lock(&smack_ipv6_lock);
2599 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2600 mutex_unlock(&smack_ipv6_lock);
2605 * smk_ipv6_port_check - check Smack port access
2608 * @act: the action being taken
2610 * Create or update the port list entry
2612 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2615 struct smk_port_label *spp;
2616 struct socket_smack *ssp = sk->sk_security;
2617 struct smack_known *skp = NULL;
2618 unsigned short port;
2619 struct smack_known *object;
2621 if (act == SMK_RECEIVING) {
2622 skp = smack_ipv6host_label(address);
2623 object = ssp->smk_in;
2626 object = smack_ipv6host_label(address);
2630 * The other end is a single label host.
2632 if (skp != NULL && object != NULL)
2633 return smk_ipv6_check(skp, object, address, act);
2635 skp = smack_net_ambient;
2637 object = smack_net_ambient;
2640 * It's remote, so port lookup does no good.
2642 if (!smk_ipv6_localhost(address))
2643 return smk_ipv6_check(skp, object, address, act);
2646 * It's local so the send check has to have passed.
2648 if (act == SMK_RECEIVING)
2651 port = ntohs(address->sin6_port);
2653 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2654 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2656 object = spp->smk_in;
2657 if (act == SMK_CONNECTING)
2658 ssp->smk_packet = spp->smk_out;
2663 return smk_ipv6_check(skp, object, address, act);
2665 #endif /* SMACK_IPV6_PORT_LABELING */
2668 * smack_inode_setsecurity - set smack xattrs
2669 * @inode: the object
2670 * @name: attribute name
2671 * @value: attribute value
2672 * @size: size of the attribute
2675 * Sets the named attribute in the appropriate blob
2677 * Returns 0 on success, or an error code
2679 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2680 const void *value, size_t size, int flags)
2682 struct smack_known *skp;
2683 struct inode_smack *nsp = smack_inode(inode);
2684 struct socket_smack *ssp;
2685 struct socket *sock;
2688 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2691 skp = smk_import_entry(value, size);
2693 return PTR_ERR(skp);
2695 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2696 nsp->smk_inode = skp;
2697 nsp->smk_flags |= SMK_INODE_INSTANT;
2701 * The rest of the Smack xattrs are only on sockets.
2703 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2706 sock = SOCKET_I(inode);
2707 if (sock == NULL || sock->sk == NULL)
2710 ssp = sock->sk->sk_security;
2712 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2714 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2716 if (sock->sk->sk_family == PF_INET) {
2717 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2720 "Smack: \"%s\" netlbl error %d.\n",
2726 #ifdef SMACK_IPV6_PORT_LABELING
2727 if (sock->sk->sk_family == PF_INET6)
2728 smk_ipv6_port_label(sock, NULL);
2735 * smack_socket_post_create - finish socket setup
2737 * @family: protocol family
2742 * Sets the netlabel information on the socket
2744 * Returns 0 on success, and error code otherwise
2746 static int smack_socket_post_create(struct socket *sock, int family,
2747 int type, int protocol, int kern)
2749 struct socket_smack *ssp;
2751 if (sock->sk == NULL)
2755 * Sockets created by kernel threads receive web label.
2757 if (unlikely(current->flags & PF_KTHREAD)) {
2758 ssp = sock->sk->sk_security;
2759 ssp->smk_in = &smack_known_web;
2760 ssp->smk_out = &smack_known_web;
2763 if (family != PF_INET)
2766 * Set the outbound netlbl.
2768 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2772 * smack_socket_socketpair - create socket pair
2773 * @socka: one socket
2774 * @sockb: another socket
2776 * Cross reference the peer labels for SO_PEERSEC
2780 static int smack_socket_socketpair(struct socket *socka,
2781 struct socket *sockb)
2783 struct socket_smack *asp = socka->sk->sk_security;
2784 struct socket_smack *bsp = sockb->sk->sk_security;
2786 asp->smk_packet = bsp->smk_out;
2787 bsp->smk_packet = asp->smk_out;
2792 #ifdef SMACK_IPV6_PORT_LABELING
2794 * smack_socket_bind - record port binding information.
2796 * @address: the port address
2797 * @addrlen: size of the address
2799 * Records the label bound to a port.
2801 * Returns 0 on success, and error code otherwise
2803 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2806 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2807 if (addrlen < SIN6_LEN_RFC2133 ||
2808 address->sa_family != AF_INET6)
2810 smk_ipv6_port_label(sock, address);
2814 #endif /* SMACK_IPV6_PORT_LABELING */
2817 * smack_socket_connect - connect access check
2819 * @sap: the other end
2820 * @addrlen: size of sap
2822 * Verifies that a connection may be possible
2824 * Returns 0 on success, and error code otherwise
2826 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2831 if (sock->sk == NULL)
2833 if (sock->sk->sk_family != PF_INET &&
2834 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2836 if (addrlen < offsetofend(struct sockaddr, sa_family))
2838 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2839 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2840 #ifdef SMACK_IPV6_SECMARK_LABELING
2841 struct smack_known *rsp;
2844 if (addrlen < SIN6_LEN_RFC2133)
2846 #ifdef SMACK_IPV6_SECMARK_LABELING
2847 rsp = smack_ipv6host_label(sip);
2849 struct socket_smack *ssp = sock->sk->sk_security;
2851 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2855 #ifdef SMACK_IPV6_PORT_LABELING
2856 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2860 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2862 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2867 * smack_flags_to_may - convert S_ to MAY_ values
2868 * @flags: the S_ value
2870 * Returns the equivalent MAY_ value
2872 static int smack_flags_to_may(int flags)
2876 if (flags & S_IRUGO)
2878 if (flags & S_IWUGO)
2880 if (flags & S_IXUGO)
2887 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2892 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2894 struct smack_known **blob = smack_msg_msg(msg);
2896 *blob = smk_of_current();
2901 * smack_of_ipc - the smack pointer for the ipc
2904 * Returns a pointer to the smack value
2906 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2908 struct smack_known **blob = smack_ipc(isp);
2914 * smack_ipc_alloc_security - Set the security blob for ipc
2919 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2921 struct smack_known **blob = smack_ipc(isp);
2923 *blob = smk_of_current();
2928 * smk_curacc_shm : check if current has access on shm
2930 * @access : access requested
2932 * Returns 0 if current has the requested access, error code otherwise
2934 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2936 struct smack_known *ssp = smack_of_ipc(isp);
2937 struct smk_audit_info ad;
2941 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2942 ad.a.u.ipc_id = isp->id;
2944 rc = smk_curacc(ssp, access, &ad);
2945 rc = smk_bu_current("shm", ssp, access, rc);
2950 * smack_shm_associate - Smack access check for shm
2952 * @shmflg: access requested
2954 * Returns 0 if current has the requested access, error code otherwise
2956 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2960 may = smack_flags_to_may(shmflg);
2961 return smk_curacc_shm(isp, may);
2965 * smack_shm_shmctl - Smack access check for shm
2967 * @cmd: what it wants to do
2969 * Returns 0 if current has the requested access, error code otherwise
2971 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2985 may = MAY_READWRITE;
2990 * System level information.
2996 return smk_curacc_shm(isp, may);
3000 * smack_shm_shmat - Smack access for shmat
3003 * @shmflg: access requested
3005 * Returns 0 if current has the requested access, error code otherwise
3007 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3012 may = smack_flags_to_may(shmflg);
3013 return smk_curacc_shm(isp, may);
3017 * smk_curacc_sem : check if current has access on sem
3019 * @access : access requested
3021 * Returns 0 if current has the requested access, error code otherwise
3023 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3025 struct smack_known *ssp = smack_of_ipc(isp);
3026 struct smk_audit_info ad;
3030 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3031 ad.a.u.ipc_id = isp->id;
3033 rc = smk_curacc(ssp, access, &ad);
3034 rc = smk_bu_current("sem", ssp, access, rc);
3039 * smack_sem_associate - Smack access check for sem
3041 * @semflg: access requested
3043 * Returns 0 if current has the requested access, error code otherwise
3045 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3049 may = smack_flags_to_may(semflg);
3050 return smk_curacc_sem(isp, may);
3054 * smack_sem_shmctl - Smack access check for sem
3056 * @cmd: what it wants to do
3058 * Returns 0 if current has the requested access, error code otherwise
3060 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3079 may = MAY_READWRITE;
3084 * System level information
3091 return smk_curacc_sem(isp, may);
3095 * smack_sem_semop - Smack checks of semaphore operations
3101 * Treated as read and write in all cases.
3103 * Returns 0 if access is allowed, error code otherwise
3105 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3106 unsigned nsops, int alter)
3108 return smk_curacc_sem(isp, MAY_READWRITE);
3112 * smk_curacc_msq : helper to check if current has access on msq
3114 * @access : access requested
3116 * return 0 if current has access, error otherwise
3118 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3120 struct smack_known *msp = smack_of_ipc(isp);
3121 struct smk_audit_info ad;
3125 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3126 ad.a.u.ipc_id = isp->id;
3128 rc = smk_curacc(msp, access, &ad);
3129 rc = smk_bu_current("msq", msp, access, rc);
3134 * smack_msg_queue_associate - Smack access check for msg_queue
3136 * @msqflg: access requested
3138 * Returns 0 if current has the requested access, error code otherwise
3140 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3144 may = smack_flags_to_may(msqflg);
3145 return smk_curacc_msq(isp, may);
3149 * smack_msg_queue_msgctl - Smack access check for msg_queue
3151 * @cmd: what it wants to do
3153 * Returns 0 if current has the requested access, error code otherwise
3155 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3167 may = MAY_READWRITE;
3172 * System level information
3179 return smk_curacc_msq(isp, may);
3183 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3186 * @msqflg: access requested
3188 * Returns 0 if current has the requested access, error code otherwise
3190 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3195 may = smack_flags_to_may(msqflg);
3196 return smk_curacc_msq(isp, may);
3200 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3207 * Returns 0 if current has read and write access, error code otherwise
3209 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3210 struct task_struct *target, long type, int mode)
3212 return smk_curacc_msq(isp, MAY_READWRITE);
3216 * smack_ipc_permission - Smack access for ipc_permission()
3217 * @ipp: the object permissions
3218 * @flag: access requested
3220 * Returns 0 if current has read and write access, error code otherwise
3222 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3224 struct smack_known **blob = smack_ipc(ipp);
3225 struct smack_known *iskp = *blob;
3226 int may = smack_flags_to_may(flag);
3227 struct smk_audit_info ad;
3231 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3232 ad.a.u.ipc_id = ipp->id;
3234 rc = smk_curacc(iskp, may, &ad);
3235 rc = smk_bu_current("svipc", iskp, may, rc);
3240 * smack_ipc_getsecid - Extract smack security id
3241 * @ipp: the object permissions
3242 * @secid: where result will be saved
3244 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3246 struct smack_known **blob = smack_ipc(ipp);
3247 struct smack_known *iskp = *blob;
3249 *secid = iskp->smk_secid;
3253 * smack_d_instantiate - Make sure the blob is correct on an inode
3254 * @opt_dentry: dentry where inode will be attached
3255 * @inode: the object
3257 * Set the inode's security blob if it hasn't been done already.
3259 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3261 struct super_block *sbp;
3262 struct superblock_smack *sbsp;
3263 struct inode_smack *isp;
3264 struct smack_known *skp;
3265 struct smack_known *ckp = smk_of_current();
3266 struct smack_known *final;
3267 char trattr[TRANS_TRUE_SIZE];
3275 isp = smack_inode(inode);
3277 mutex_lock(&isp->smk_lock);
3279 * If the inode is already instantiated
3280 * take the quick way out
3282 if (isp->smk_flags & SMK_INODE_INSTANT)
3286 sbsp = sbp->s_security;
3288 * We're going to use the superblock default label
3289 * if there's no label on the file.
3291 final = sbsp->smk_default;
3294 * If this is the root inode the superblock
3295 * may be in the process of initialization.
3296 * If that is the case use the root value out
3297 * of the superblock.
3299 if (opt_dentry->d_parent == opt_dentry) {
3300 switch (sbp->s_magic) {
3301 case CGROUP_SUPER_MAGIC:
3302 case CGROUP2_SUPER_MAGIC:
3304 * The cgroup filesystem is never mounted,
3305 * so there's no opportunity to set the mount
3308 sbsp->smk_root = &smack_known_star;
3309 sbsp->smk_default = &smack_known_star;
3310 isp->smk_inode = sbsp->smk_root;
3314 * What about shmem/tmpfs anonymous files with dentry
3315 * obtained from d_alloc_pseudo()?
3317 isp->smk_inode = smk_of_current();
3320 isp->smk_inode = smk_of_current();
3324 * Socket access is controlled by the socket
3325 * structures associated with the task involved.
3327 isp->smk_inode = &smack_known_star;
3330 isp->smk_inode = sbsp->smk_root;
3333 isp->smk_flags |= SMK_INODE_INSTANT;
3338 * This is pretty hackish.
3339 * Casey says that we shouldn't have to do
3340 * file system specific code, but it does help
3341 * with keeping it simple.
3343 switch (sbp->s_magic) {
3345 case CGROUP_SUPER_MAGIC:
3346 case CGROUP2_SUPER_MAGIC:
3348 * Casey says that it's a little embarrassing
3349 * that the smack file system doesn't do
3350 * extended attributes.
3352 * Cgroupfs is special
3354 final = &smack_known_star;
3356 case DEVPTS_SUPER_MAGIC:
3358 * devpts seems content with the label of the task.
3359 * Programs that change smack have to treat the
3364 case PROC_SUPER_MAGIC:
3366 * Casey says procfs appears not to care.
3367 * The superblock default suffices.
3372 * Device labels should come from the filesystem,
3373 * but watch out, because they're volitile,
3374 * getting recreated on every reboot.
3376 final = &smack_known_star;
3378 * If a smack value has been set we want to use it,
3379 * but since tmpfs isn't giving us the opportunity
3380 * to set mount options simulate setting the
3381 * superblock default.
3386 * This isn't an understood special case.
3387 * Get the value from the xattr.
3391 * UNIX domain sockets use lower level socket data.
3393 if (S_ISSOCK(inode->i_mode)) {
3394 final = &smack_known_star;
3398 * No xattr support means, alas, no SMACK label.
3399 * Use the aforeapplied default.
3400 * It would be curious if the label of the task
3401 * does not match that assigned.
3403 if (!(inode->i_opflags & IOP_XATTR))
3406 * Get the dentry for xattr.
3408 dp = dget(opt_dentry);
3409 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3410 if (!IS_ERR_OR_NULL(skp))
3414 * Transmuting directory
3416 if (S_ISDIR(inode->i_mode)) {
3418 * If this is a new directory and the label was
3419 * transmuted when the inode was initialized
3420 * set the transmute attribute on the directory
3421 * and mark the inode.
3423 * If there is a transmute attribute on the
3424 * directory mark the inode.
3426 if (isp->smk_flags & SMK_INODE_CHANGED) {
3427 isp->smk_flags &= ~SMK_INODE_CHANGED;
3428 rc = __vfs_setxattr(dp, inode,
3429 XATTR_NAME_SMACKTRANSMUTE,
3430 TRANS_TRUE, TRANS_TRUE_SIZE,
3433 rc = __vfs_getxattr(dp, inode,
3434 XATTR_NAME_SMACKTRANSMUTE, trattr,
3436 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3437 TRANS_TRUE_SIZE) != 0)
3441 transflag = SMK_INODE_TRANSMUTE;
3444 * Don't let the exec or mmap label be "*" or "@".
3446 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3447 if (IS_ERR(skp) || skp == &smack_known_star ||
3448 skp == &smack_known_web)
3450 isp->smk_task = skp;
3452 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3453 if (IS_ERR(skp) || skp == &smack_known_star ||
3454 skp == &smack_known_web)
3456 isp->smk_mmap = skp;
3463 isp->smk_inode = ckp;
3465 isp->smk_inode = final;
3467 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3470 mutex_unlock(&isp->smk_lock);
3475 * smack_getprocattr - Smack process attribute access
3476 * @p: the object task
3477 * @name: the name of the attribute in /proc/.../attr
3478 * @value: where to put the result
3480 * Places a copy of the task Smack into value
3482 * Returns the length of the smack label or an error code
3484 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3486 struct smack_known *skp = smk_of_task_struct(p);
3490 if (strcmp(name, "current") != 0)
3493 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3503 * smack_setprocattr - Smack process attribute setting
3504 * @name: the name of the attribute in /proc/.../attr
3505 * @value: the value to set
3506 * @size: the size of the value
3508 * Sets the Smack value of the task. Only setting self
3509 * is permitted and only with privilege
3511 * Returns the length of the smack label or an error code
3513 static int smack_setprocattr(const char *name, void *value, size_t size)
3515 struct task_smack *tsp = smack_cred(current_cred());
3517 struct smack_known *skp;
3518 struct smack_known_list_elem *sklep;
3521 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3524 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3527 if (strcmp(name, "current") != 0)
3530 skp = smk_import_entry(value, size);
3532 return PTR_ERR(skp);
3535 * No process is ever allowed the web ("@") label
3536 * and the star ("*") label.
3538 if (skp == &smack_known_web || skp == &smack_known_star)
3541 if (!smack_privileged(CAP_MAC_ADMIN)) {
3543 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3544 if (sklep->smk_label == skp) {
3552 new = prepare_creds();
3556 tsp = smack_cred(new);
3557 tsp->smk_task = skp;
3559 * process can change its label only once
3561 smk_destroy_label_list(&tsp->smk_relabel);
3568 * smack_unix_stream_connect - Smack access on UDS
3570 * @other: the other sock
3573 * Return 0 if a subject with the smack of sock could access
3574 * an object with the smack of other, otherwise an error code
3576 static int smack_unix_stream_connect(struct sock *sock,
3577 struct sock *other, struct sock *newsk)
3579 struct smack_known *skp;
3580 struct smack_known *okp;
3581 struct socket_smack *ssp = sock->sk_security;
3582 struct socket_smack *osp = other->sk_security;
3583 struct socket_smack *nsp = newsk->sk_security;
3584 struct smk_audit_info ad;
3587 struct lsm_network_audit net;
3590 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3594 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3595 smk_ad_setfield_u_net_sk(&ad, other);
3597 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3598 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3602 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3603 rc = smk_bu_note("UDS connect", okp, skp,
3609 * Cross reference the peer labels for SO_PEERSEC.
3612 nsp->smk_packet = ssp->smk_out;
3613 ssp->smk_packet = osp->smk_out;
3620 * smack_unix_may_send - Smack access on UDS
3622 * @other: the other socket
3624 * Return 0 if a subject with the smack of sock could access
3625 * an object with the smack of other, otherwise an error code
3627 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3629 struct socket_smack *ssp = sock->sk->sk_security;
3630 struct socket_smack *osp = other->sk->sk_security;
3631 struct smk_audit_info ad;
3635 struct lsm_network_audit net;
3637 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3638 smk_ad_setfield_u_net_sk(&ad, other->sk);
3641 if (smack_privileged(CAP_MAC_OVERRIDE))
3644 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3645 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3650 * smack_socket_sendmsg - Smack check based on destination host
3653 * @size: the size of the message
3655 * Return 0 if the current subject can write to the destination host.
3656 * For IPv4 this is only a question if the destination is a single label host.
3657 * For IPv6 this is a check against the label of the port.
3659 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3662 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3663 #if IS_ENABLED(CONFIG_IPV6)
3664 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3666 #ifdef SMACK_IPV6_SECMARK_LABELING
3667 struct socket_smack *ssp = sock->sk->sk_security;
3668 struct smack_known *rsp;
3673 * Perfectly reasonable for this to be NULL
3678 switch (sock->sk->sk_family) {
3680 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3681 sip->sin_family != AF_INET)
3683 rc = smack_netlabel_send(sock->sk, sip);
3685 #if IS_ENABLED(CONFIG_IPV6)
3687 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3688 sap->sin6_family != AF_INET6)
3690 #ifdef SMACK_IPV6_SECMARK_LABELING
3691 rsp = smack_ipv6host_label(sap);
3693 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3696 #ifdef SMACK_IPV6_PORT_LABELING
3697 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3699 #endif /* IS_ENABLED(CONFIG_IPV6) */
3706 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3707 * @sap: netlabel secattr
3708 * @ssp: socket security information
3710 * Returns a pointer to a Smack label entry found on the label list.
3712 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3713 struct socket_smack *ssp)
3715 struct smack_known *skp;
3720 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3722 * Looks like a CIPSO packet.
3723 * If there are flags but no level netlabel isn't
3724 * behaving the way we expect it to.
3726 * Look it up in the label table
3727 * Without guidance regarding the smack value
3728 * for the packet fall back on the network
3732 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3733 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3736 * Compare the catsets. Use the netlbl APIs.
3738 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3739 if ((skp->smk_netlabel.flags &
3740 NETLBL_SECATTR_MLS_CAT) == 0)
3744 for (acat = -1, kcat = -1; acat == kcat; ) {
3745 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3747 kcat = netlbl_catmap_walk(
3748 skp->smk_netlabel.attr.mls.cat,
3750 if (acat < 0 || kcat < 0)
3763 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3764 return &smack_known_web;
3765 return &smack_known_star;
3767 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3769 * Looks like a fallback, which gives us a secid.
3771 return smack_from_secid(sap->attr.secid);
3773 * Without guidance regarding the smack value
3774 * for the packet fall back on the network
3777 return smack_net_ambient;
3780 #if IS_ENABLED(CONFIG_IPV6)
3781 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3785 int proto = -EINVAL;
3786 struct ipv6hdr _ipv6h;
3787 struct ipv6hdr *ip6;
3789 struct tcphdr _tcph, *th;
3790 struct udphdr _udph, *uh;
3791 struct dccp_hdr _dccph, *dh;
3795 offset = skb_network_offset(skb);
3796 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3799 sip->sin6_addr = ip6->saddr;
3801 nexthdr = ip6->nexthdr;
3802 offset += sizeof(_ipv6h);
3803 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3810 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3812 sip->sin6_port = th->source;
3815 case IPPROTO_UDPLITE:
3816 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3818 sip->sin6_port = uh->source;
3821 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3823 sip->sin6_port = dh->dccph_sport;
3828 #endif /* CONFIG_IPV6 */
3831 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3835 * Returns 0 if the packet should be delivered, an error code otherwise
3837 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3839 struct netlbl_lsm_secattr secattr;
3840 struct socket_smack *ssp = sk->sk_security;
3841 struct smack_known *skp = NULL;
3843 struct smk_audit_info ad;
3844 u16 family = sk->sk_family;
3846 struct lsm_network_audit net;
3848 #if IS_ENABLED(CONFIG_IPV6)
3849 struct sockaddr_in6 sadd;
3852 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3854 #endif /* CONFIG_IPV6 */
3858 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3860 * If there is a secmark use it rather than the CIPSO label.
3861 * If there is no secmark fall back to CIPSO.
3862 * The secmark is assumed to reflect policy better.
3864 if (skb && skb->secmark != 0) {
3865 skp = smack_from_secid(skb->secmark);
3868 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3870 * Translate what netlabel gave us.
3872 netlbl_secattr_init(&secattr);
3874 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3876 skp = smack_from_secattr(&secattr, ssp);
3878 skp = smack_net_ambient;
3880 netlbl_secattr_destroy(&secattr);
3882 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3886 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3887 ad.a.u.net->family = family;
3888 ad.a.u.net->netif = skb->skb_iif;
3889 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3892 * Receiving a packet requires that the other end
3893 * be able to write here. Read access is not required.
3894 * This is the simplist possible security model
3897 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3898 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3901 netlbl_skbuff_err(skb, family, rc, 0);
3903 #if IS_ENABLED(CONFIG_IPV6)
3905 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3906 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3907 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3909 #ifdef SMACK_IPV6_SECMARK_LABELING
3910 if (skb && skb->secmark != 0)
3911 skp = smack_from_secid(skb->secmark);
3912 else if (smk_ipv6_localhost(&sadd))
3915 skp = smack_ipv6host_label(&sadd);
3917 skp = smack_net_ambient;
3921 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3922 ad.a.u.net->family = family;
3923 ad.a.u.net->netif = skb->skb_iif;
3924 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3925 #endif /* CONFIG_AUDIT */
3926 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3927 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3929 #endif /* SMACK_IPV6_SECMARK_LABELING */
3930 #ifdef SMACK_IPV6_PORT_LABELING
3931 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3932 #endif /* SMACK_IPV6_PORT_LABELING */
3934 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3935 ICMPV6_ADM_PROHIBITED, 0);
3937 #endif /* CONFIG_IPV6 */
3944 * smack_socket_getpeersec_stream - pull in packet label
3946 * @optval: user's destination
3947 * @optlen: size thereof
3950 * returns zero on success, an error code otherwise
3952 static int smack_socket_getpeersec_stream(struct socket *sock,
3953 char __user *optval,
3954 int __user *optlen, unsigned len)
3956 struct socket_smack *ssp;
3961 ssp = sock->sk->sk_security;
3962 if (ssp->smk_packet != NULL) {
3963 rcp = ssp->smk_packet->smk_known;
3964 slen = strlen(rcp) + 1;
3969 else if (copy_to_user(optval, rcp, slen) != 0)
3972 if (put_user(slen, optlen) != 0)
3980 * smack_socket_getpeersec_dgram - pull in packet label
3981 * @sock: the peer socket
3983 * @secid: pointer to where to put the secid of the packet
3985 * Sets the netlabel socket state on sk from parent
3987 static int smack_socket_getpeersec_dgram(struct socket *sock,
3988 struct sk_buff *skb, u32 *secid)
3991 struct netlbl_lsm_secattr secattr;
3992 struct socket_smack *ssp = NULL;
3993 struct smack_known *skp;
3994 int family = PF_UNSPEC;
3995 u32 s = 0; /* 0 is the invalid secid */
3999 if (skb->protocol == htons(ETH_P_IP))
4001 #if IS_ENABLED(CONFIG_IPV6)
4002 else if (skb->protocol == htons(ETH_P_IPV6))
4004 #endif /* CONFIG_IPV6 */
4006 if (family == PF_UNSPEC && sock != NULL)
4007 family = sock->sk->sk_family;
4011 ssp = sock->sk->sk_security;
4012 s = ssp->smk_out->smk_secid;
4015 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4021 * Translate what netlabel gave us.
4023 if (sock != NULL && sock->sk != NULL)
4024 ssp = sock->sk->sk_security;
4025 netlbl_secattr_init(&secattr);
4026 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4028 skp = smack_from_secattr(&secattr, ssp);
4031 netlbl_secattr_destroy(&secattr);
4034 #ifdef SMACK_IPV6_SECMARK_LABELING
4046 * smack_sock_graft - Initialize a newly created socket with an existing sock
4048 * @parent: parent socket
4050 * Set the smk_{in,out} state of an existing sock based on the process that
4051 * is creating the new socket.
4053 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4055 struct socket_smack *ssp;
4056 struct smack_known *skp = smk_of_current();
4059 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4062 ssp = sk->sk_security;
4065 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4069 * smack_inet_conn_request - Smack access check on connect
4070 * @sk: socket involved
4074 * Returns 0 if a task with the packet label could write to
4075 * the socket, otherwise an error code
4077 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4078 struct request_sock *req)
4080 u16 family = sk->sk_family;
4081 struct smack_known *skp;
4082 struct socket_smack *ssp = sk->sk_security;
4083 struct netlbl_lsm_secattr secattr;
4084 struct sockaddr_in addr;
4086 struct smack_known *hskp;
4088 struct smk_audit_info ad;
4090 struct lsm_network_audit net;
4093 #if IS_ENABLED(CONFIG_IPV6)
4094 if (family == PF_INET6) {
4096 * Handle mapped IPv4 packets arriving
4097 * via IPv6 sockets. Don't set up netlabel
4098 * processing on IPv6.
4100 if (skb->protocol == htons(ETH_P_IP))
4105 #endif /* CONFIG_IPV6 */
4107 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4109 * If there is a secmark use it rather than the CIPSO label.
4110 * If there is no secmark fall back to CIPSO.
4111 * The secmark is assumed to reflect policy better.
4113 if (skb && skb->secmark != 0) {
4114 skp = smack_from_secid(skb->secmark);
4117 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4119 netlbl_secattr_init(&secattr);
4120 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4122 skp = smack_from_secattr(&secattr, ssp);
4124 skp = &smack_known_huh;
4125 netlbl_secattr_destroy(&secattr);
4127 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4132 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4133 ad.a.u.net->family = family;
4134 ad.a.u.net->netif = skb->skb_iif;
4135 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4138 * Receiving a packet requires that the other end be able to write
4139 * here. Read access is not required.
4141 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4142 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4147 * Save the peer's label in the request_sock so we can later setup
4148 * smk_packet in the child socket so that SO_PEERCRED can report it.
4150 req->peer_secid = skp->smk_secid;
4153 * We need to decide if we want to label the incoming connection here
4154 * if we do we only need to label the request_sock and the stack will
4155 * propagate the wire-label to the sock when it is created.
4158 addr.sin_addr.s_addr = hdr->saddr;
4160 hskp = smack_ipv4host_label(&addr);
4164 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4166 netlbl_req_delattr(req);
4172 * smack_inet_csk_clone - Copy the connection information to the new socket
4173 * @sk: the new socket
4174 * @req: the connection's request_sock
4176 * Transfer the connection's peer label to the newly created socket.
4178 static void smack_inet_csk_clone(struct sock *sk,
4179 const struct request_sock *req)
4181 struct socket_smack *ssp = sk->sk_security;
4182 struct smack_known *skp;
4184 if (req->peer_secid != 0) {
4185 skp = smack_from_secid(req->peer_secid);
4186 ssp->smk_packet = skp;
4188 ssp->smk_packet = NULL;
4192 * Key management security hooks
4194 * Casey has not tested key support very heavily.
4195 * The permission check is most likely too restrictive.
4196 * If you care about keys please have a look.
4201 * smack_key_alloc - Set the key security blob
4203 * @cred: the credentials to use
4206 * No allocation required
4210 static int smack_key_alloc(struct key *key, const struct cred *cred,
4211 unsigned long flags)
4213 struct smack_known *skp = smk_of_task(smack_cred(cred));
4215 key->security = skp;
4220 * smack_key_free - Clear the key security blob
4223 * Clear the blob pointer
4225 static void smack_key_free(struct key *key)
4227 key->security = NULL;
4231 * smack_key_permission - Smack access on a key
4232 * @key_ref: gets to the object
4233 * @cred: the credentials to use
4234 * @need_perm: requested key permission
4236 * Return 0 if the task has read and write to the object,
4237 * an error code otherwise
4239 static int smack_key_permission(key_ref_t key_ref,
4240 const struct cred *cred,
4241 enum key_need_perm need_perm)
4244 struct smk_audit_info ad;
4245 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4250 * Validate requested permissions
4252 switch (need_perm) {
4254 case KEY_NEED_SEARCH:
4256 request |= MAY_READ;
4258 case KEY_NEED_WRITE:
4260 case KEY_NEED_SETATTR:
4261 request |= MAY_WRITE;
4263 case KEY_NEED_UNSPECIFIED:
4264 case KEY_NEED_UNLINK:
4265 case KEY_SYSADMIN_OVERRIDE:
4266 case KEY_AUTHTOKEN_OVERRIDE:
4267 case KEY_DEFER_PERM_CHECK:
4273 keyp = key_ref_to_ptr(key_ref);
4277 * If the key hasn't been initialized give it access so that
4280 if (keyp->security == NULL)
4283 * This should not occur
4288 if (smack_privileged(CAP_MAC_OVERRIDE))
4292 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4293 ad.a.u.key_struct.key = keyp->serial;
4294 ad.a.u.key_struct.key_desc = keyp->description;
4296 rc = smk_access(tkp, keyp->security, request, &ad);
4297 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4302 * smack_key_getsecurity - Smack label tagging the key
4303 * @key points to the key to be queried
4304 * @_buffer points to a pointer that should be set to point to the
4305 * resulting string (if no label or an error occurs).
4306 * Return the length of the string (including terminating NUL) or -ve if
4308 * May also return 0 (and a NULL buffer pointer) if there is no label.
4310 static int smack_key_getsecurity(struct key *key, char **_buffer)
4312 struct smack_known *skp = key->security;
4316 if (key->security == NULL) {
4321 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4324 length = strlen(copy) + 1;
4331 #ifdef CONFIG_KEY_NOTIFICATIONS
4333 * smack_watch_key - Smack access to watch a key for notifications.
4334 * @key: The key to be watched
4336 * Return 0 if the @watch->cred has permission to read from the key object and
4337 * an error otherwise.
4339 static int smack_watch_key(struct key *key)
4341 struct smk_audit_info ad;
4342 struct smack_known *tkp = smk_of_current();
4348 * If the key hasn't been initialized give it access so that
4351 if (key->security == NULL)
4354 * This should not occur
4359 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4363 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4364 ad.a.u.key_struct.key = key->serial;
4365 ad.a.u.key_struct.key_desc = key->description;
4367 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4368 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4371 #endif /* CONFIG_KEY_NOTIFICATIONS */
4372 #endif /* CONFIG_KEYS */
4374 #ifdef CONFIG_WATCH_QUEUE
4376 * smack_post_notification - Smack access to post a notification to a queue
4377 * @w_cred: The credentials of the watcher.
4378 * @cred: The credentials of the event source (may be NULL).
4379 * @n: The notification message to be posted.
4381 static int smack_post_notification(const struct cred *w_cred,
4382 const struct cred *cred,
4383 struct watch_notification *n)
4385 struct smk_audit_info ad;
4386 struct smack_known *subj, *obj;
4389 /* Always let maintenance notifications through. */
4390 if (n->type == WATCH_TYPE_META)
4395 subj = smk_of_task(smack_cred(cred));
4396 obj = smk_of_task(smack_cred(w_cred));
4398 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4399 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4400 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4403 #endif /* CONFIG_WATCH_QUEUE */
4408 * Audit requires a unique representation of each Smack specific
4409 * rule. This unique representation is used to distinguish the
4410 * object to be audited from remaining kernel objects and also
4411 * works as a glue between the audit hooks.
4413 * Since repository entries are added but never deleted, we'll use
4414 * the smack_known label address related to the given audit rule as
4415 * the needed unique representation. This also better fits the smack
4416 * model where nearly everything is a label.
4421 * smack_audit_rule_init - Initialize a smack audit rule
4422 * @field: audit rule fields given from user-space (audit.h)
4423 * @op: required testing operator (=, !=, >, <, ...)
4424 * @rulestr: smack label to be audited
4425 * @vrule: pointer to save our own audit rule representation
4427 * Prepare to audit cases where (@field @op @rulestr) is true.
4428 * The label to be audited is created if necessay.
4430 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4432 struct smack_known *skp;
4433 char **rule = (char **)vrule;
4436 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4439 if (op != Audit_equal && op != Audit_not_equal)
4442 skp = smk_import_entry(rulestr, 0);
4444 return PTR_ERR(skp);
4446 *rule = skp->smk_known;
4452 * smack_audit_rule_known - Distinguish Smack audit rules
4453 * @krule: rule of interest, in Audit kernel representation format
4455 * This is used to filter Smack rules from remaining Audit ones.
4456 * If it's proved that this rule belongs to us, the
4457 * audit_rule_match hook will be called to do the final judgement.
4459 static int smack_audit_rule_known(struct audit_krule *krule)
4461 struct audit_field *f;
4464 for (i = 0; i < krule->field_count; i++) {
4465 f = &krule->fields[i];
4467 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4475 * smack_audit_rule_match - Audit given object ?
4476 * @secid: security id for identifying the object to test
4477 * @field: audit rule flags given from user-space
4478 * @op: required testing operator
4479 * @vrule: smack internal rule presentation
4481 * The core Audit hook. It's used to take the decision of
4482 * whether to audit or not to audit a given object.
4484 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4486 struct smack_known *skp;
4489 if (unlikely(!rule)) {
4490 WARN_ONCE(1, "Smack: missing rule\n");
4494 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4497 skp = smack_from_secid(secid);
4500 * No need to do string comparisons. If a match occurs,
4501 * both pointers will point to the same smack_known
4504 if (op == Audit_equal)
4505 return (rule == skp->smk_known);
4506 if (op == Audit_not_equal)
4507 return (rule != skp->smk_known);
4513 * There is no need for a smack_audit_rule_free hook.
4514 * No memory was allocated.
4517 #endif /* CONFIG_AUDIT */
4520 * smack_ismaclabel - check if xattr @name references a smack MAC label
4521 * @name: Full xattr name to check.
4523 static int smack_ismaclabel(const char *name)
4525 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4530 * smack_secid_to_secctx - return the smack label for a secid
4531 * @secid: incoming integer
4532 * @secdata: destination
4533 * @seclen: how long it is
4535 * Exists for networking code.
4537 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4539 struct smack_known *skp = smack_from_secid(secid);
4542 *secdata = skp->smk_known;
4543 *seclen = strlen(skp->smk_known);
4548 * smack_secctx_to_secid - return the secid for a smack label
4549 * @secdata: smack label
4550 * @seclen: how long result is
4551 * @secid: outgoing integer
4553 * Exists for audit and networking code.
4555 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4557 struct smack_known *skp = smk_find_entry(secdata);
4560 *secid = skp->smk_secid;
4567 * There used to be a smack_release_secctx hook
4568 * that did nothing back when hooks were in a vector.
4569 * Now that there's a list such a hook adds cost.
4572 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4574 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4577 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4579 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4582 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4584 struct smack_known *skp = smk_of_inode(inode);
4586 *ctx = skp->smk_known;
4587 *ctxlen = strlen(skp->smk_known);
4591 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4594 struct task_smack *tsp;
4595 struct smack_known *skp;
4596 struct inode_smack *isp;
4597 struct cred *new_creds = *new;
4599 if (new_creds == NULL) {
4600 new_creds = prepare_creds();
4601 if (new_creds == NULL)
4605 tsp = smack_cred(new_creds);
4608 * Get label from overlay inode and set it in create_sid
4610 isp = smack_inode(d_inode(dentry->d_parent));
4611 skp = isp->smk_inode;
4612 tsp->smk_task = skp;
4617 static int smack_inode_copy_up_xattr(const char *name)
4620 * Return 1 if this is the smack access Smack attribute.
4622 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4628 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4630 const struct cred *old,
4633 struct task_smack *otsp = smack_cred(old);
4634 struct task_smack *ntsp = smack_cred(new);
4635 struct inode_smack *isp;
4639 * Use the process credential unless all of
4640 * the transmuting criteria are met
4642 ntsp->smk_task = otsp->smk_task;
4645 * the attribute of the containing directory
4647 isp = smack_inode(d_inode(dentry->d_parent));
4649 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4651 may = smk_access_entry(otsp->smk_task->smk_known,
4652 isp->smk_inode->smk_known,
4653 &otsp->smk_task->smk_rules);
4657 * If the directory is transmuting and the rule
4658 * providing access is transmuting use the containing
4659 * directory label instead of the process label.
4661 if (may > 0 && (may & MAY_TRANSMUTE))
4662 ntsp->smk_task = isp->smk_inode;
4667 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4668 .lbs_cred = sizeof(struct task_smack),
4669 .lbs_file = sizeof(struct smack_known *),
4670 .lbs_inode = sizeof(struct inode_smack),
4671 .lbs_ipc = sizeof(struct smack_known *),
4672 .lbs_msg_msg = sizeof(struct smack_known *),
4675 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4676 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4677 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4678 LSM_HOOK_INIT(syslog, smack_syslog),
4680 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4681 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4683 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4684 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4685 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4686 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4687 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4688 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4690 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4692 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4693 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4694 LSM_HOOK_INIT(inode_link, smack_inode_link),
4695 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4696 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4697 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4698 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4699 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4700 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4701 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4702 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4703 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4704 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4705 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4706 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4707 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4708 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4710 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4711 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4712 LSM_HOOK_INIT(file_lock, smack_file_lock),
4713 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4714 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4715 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4716 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4717 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4718 LSM_HOOK_INIT(file_receive, smack_file_receive),
4720 LSM_HOOK_INIT(file_open, smack_file_open),
4722 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4723 LSM_HOOK_INIT(cred_free, smack_cred_free),
4724 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4725 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4726 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4727 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4728 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4729 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4730 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4731 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4732 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4733 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4734 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4735 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4736 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4737 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4738 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4739 LSM_HOOK_INIT(task_kill, smack_task_kill),
4740 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4742 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4743 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4745 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4747 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4748 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4749 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4750 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4751 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4753 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4754 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4755 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4756 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4758 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4759 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4760 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4761 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4763 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4765 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4766 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4768 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4769 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4771 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4772 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4773 #ifdef SMACK_IPV6_PORT_LABELING
4774 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4776 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4777 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4778 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4779 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4780 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4781 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4782 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4783 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4784 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4785 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4787 /* key management security hooks */
4789 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4790 LSM_HOOK_INIT(key_free, smack_key_free),
4791 LSM_HOOK_INIT(key_permission, smack_key_permission),
4792 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4793 #ifdef CONFIG_KEY_NOTIFICATIONS
4794 LSM_HOOK_INIT(watch_key, smack_watch_key),
4796 #endif /* CONFIG_KEYS */
4798 #ifdef CONFIG_WATCH_QUEUE
4799 LSM_HOOK_INIT(post_notification, smack_post_notification),
4804 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4805 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4806 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4807 #endif /* CONFIG_AUDIT */
4809 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4810 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4811 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4812 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4813 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4814 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4815 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4816 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4817 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4821 static __init void init_smack_known_list(void)
4824 * Initialize rule list locks
4826 mutex_init(&smack_known_huh.smk_rules_lock);
4827 mutex_init(&smack_known_hat.smk_rules_lock);
4828 mutex_init(&smack_known_floor.smk_rules_lock);
4829 mutex_init(&smack_known_star.smk_rules_lock);
4830 mutex_init(&smack_known_web.smk_rules_lock);
4832 * Initialize rule lists
4834 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4835 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4836 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4837 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4838 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4840 * Create the known labels list
4842 smk_insert_entry(&smack_known_huh);
4843 smk_insert_entry(&smack_known_hat);
4844 smk_insert_entry(&smack_known_star);
4845 smk_insert_entry(&smack_known_floor);
4846 smk_insert_entry(&smack_known_web);
4850 * smack_init - initialize the smack system
4852 * Returns 0 on success, -ENOMEM is there's no memory
4854 static __init int smack_init(void)
4856 struct cred *cred = (struct cred *) current->cred;
4857 struct task_smack *tsp;
4859 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4860 if (!smack_inode_cache)
4863 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4864 if (!smack_rule_cache) {
4865 kmem_cache_destroy(smack_inode_cache);
4870 * Set the security state for the initial task.
4872 tsp = smack_cred(cred);
4873 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4878 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4881 pr_info("Smack: Initializing.\n");
4882 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4883 pr_info("Smack: Netfilter enabled.\n");
4885 #ifdef SMACK_IPV6_PORT_LABELING
4886 pr_info("Smack: IPv6 port labeling enabled.\n");
4888 #ifdef SMACK_IPV6_SECMARK_LABELING
4889 pr_info("Smack: IPv6 Netfilter enabled.\n");
4892 /* initialize the smack_known_list */
4893 init_smack_known_list();
4899 * Smack requires early initialization in order to label
4900 * all processes and objects when they are created.
4902 DEFINE_LSM(smack) = {
4904 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4905 .blobs = &smack_blob_sizes,