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 static DEFINE_MUTEX(smack_ipv6_lock);
55 static LIST_HEAD(smk_ipv6_port_list);
56 struct kmem_cache *smack_rule_cache;
59 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
64 } smk_mount_opts[] = {
65 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
66 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
70 static int match_opt_prefix(char *s, int l, char **arg)
74 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
75 size_t len = smk_mount_opts[i].len;
76 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
78 if (len == l || s[len] != '=')
81 return smk_mount_opts[i].opt;
86 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
87 static char *smk_bu_mess[] = {
88 "Bringup Error", /* Unused */
89 "Bringup", /* SMACK_BRINGUP_ALLOW */
90 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
91 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
94 static void smk_bu_mode(int mode, char *s)
100 if (mode & MAY_WRITE)
104 if (mode & MAY_APPEND)
106 if (mode & MAY_TRANSMUTE)
116 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
117 static int smk_bu_note(char *note, struct smack_known *sskp,
118 struct smack_known *oskp, int mode, int rc)
120 char acc[SMK_NUM_ACCESS_TYPE + 1];
124 if (rc > SMACK_UNCONFINED_OBJECT)
127 smk_bu_mode(mode, acc);
128 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
129 sskp->smk_known, oskp->smk_known, acc, note);
133 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
136 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
137 static int smk_bu_current(char *note, struct smack_known *oskp,
140 struct task_smack *tsp = smack_cred(current_cred());
141 char acc[SMK_NUM_ACCESS_TYPE + 1];
145 if (rc > SMACK_UNCONFINED_OBJECT)
148 smk_bu_mode(mode, acc);
149 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
150 tsp->smk_task->smk_known, oskp->smk_known,
151 acc, current->comm, note);
155 #define smk_bu_current(note, oskp, mode, RC) (RC)
158 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
159 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
161 struct task_smack *tsp = smack_cred(current_cred());
162 struct smack_known *smk_task = smk_of_task_struct_obj(otp);
163 char acc[SMK_NUM_ACCESS_TYPE + 1];
167 if (rc > SMACK_UNCONFINED_OBJECT)
170 smk_bu_mode(mode, acc);
171 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
172 tsp->smk_task->smk_known, smk_task->smk_known, acc,
173 current->comm, otp->comm);
177 #define smk_bu_task(otp, mode, RC) (RC)
180 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
181 static int smk_bu_inode(struct inode *inode, int mode, int rc)
183 struct task_smack *tsp = smack_cred(current_cred());
184 struct inode_smack *isp = smack_inode(inode);
185 char acc[SMK_NUM_ACCESS_TYPE + 1];
187 if (isp->smk_flags & SMK_INODE_IMPURE)
188 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
189 inode->i_sb->s_id, inode->i_ino, current->comm);
193 if (rc > SMACK_UNCONFINED_OBJECT)
195 if (rc == SMACK_UNCONFINED_SUBJECT &&
196 (mode & (MAY_WRITE | MAY_APPEND)))
197 isp->smk_flags |= SMK_INODE_IMPURE;
199 smk_bu_mode(mode, acc);
201 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
202 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
203 inode->i_sb->s_id, inode->i_ino, current->comm);
207 #define smk_bu_inode(inode, mode, RC) (RC)
210 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
211 static int smk_bu_file(struct file *file, int mode, int rc)
213 struct task_smack *tsp = smack_cred(current_cred());
214 struct smack_known *sskp = tsp->smk_task;
215 struct inode *inode = file_inode(file);
216 struct inode_smack *isp = smack_inode(inode);
217 char acc[SMK_NUM_ACCESS_TYPE + 1];
219 if (isp->smk_flags & SMK_INODE_IMPURE)
220 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
221 inode->i_sb->s_id, inode->i_ino, current->comm);
225 if (rc > SMACK_UNCONFINED_OBJECT)
228 smk_bu_mode(mode, acc);
229 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
230 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
231 inode->i_sb->s_id, inode->i_ino, file,
236 #define smk_bu_file(file, mode, RC) (RC)
239 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
240 static int smk_bu_credfile(const struct cred *cred, struct file *file,
243 struct task_smack *tsp = smack_cred(cred);
244 struct smack_known *sskp = tsp->smk_task;
245 struct inode *inode = file_inode(file);
246 struct inode_smack *isp = smack_inode(inode);
247 char acc[SMK_NUM_ACCESS_TYPE + 1];
249 if (isp->smk_flags & SMK_INODE_IMPURE)
250 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
251 inode->i_sb->s_id, inode->i_ino, current->comm);
255 if (rc > SMACK_UNCONFINED_OBJECT)
258 smk_bu_mode(mode, acc);
259 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
260 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
261 inode->i_sb->s_id, inode->i_ino, file,
266 #define smk_bu_credfile(cred, file, mode, RC) (RC)
270 * smk_fetch - Fetch the smack label from a file.
271 * @name: type of the label (attribute)
272 * @ip: a pointer to the inode
273 * @dp: a pointer to the dentry
275 * Returns a pointer to the master list entry for the Smack label,
276 * NULL if there was no label to fetch, or an error code.
278 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
283 struct smack_known *skp = NULL;
285 if (!(ip->i_opflags & IOP_XATTR))
286 return ERR_PTR(-EOPNOTSUPP);
288 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
290 return ERR_PTR(-ENOMEM);
292 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
298 skp = smk_import_entry(buffer, rc);
306 * init_inode_smack - initialize an inode security blob
307 * @inode: inode to extract the info from
308 * @skp: a pointer to the Smack label entry to use in the blob
311 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
313 struct inode_smack *isp = smack_inode(inode);
315 isp->smk_inode = skp;
320 * init_task_smack - initialize a task security blob
321 * @tsp: blob to initialize
322 * @task: a pointer to the Smack label for the running task
323 * @forked: a pointer to the Smack label for the forked task
326 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
327 struct smack_known *forked)
329 tsp->smk_task = task;
330 tsp->smk_forked = forked;
331 INIT_LIST_HEAD(&tsp->smk_rules);
332 INIT_LIST_HEAD(&tsp->smk_relabel);
333 mutex_init(&tsp->smk_rules_lock);
337 * smk_copy_rules - copy a rule set
338 * @nhead: new rules header pointer
339 * @ohead: old rules header pointer
340 * @gfp: type of the memory for the allocation
342 * Returns 0 on success, -ENOMEM on error
344 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
347 struct smack_rule *nrp;
348 struct smack_rule *orp;
351 list_for_each_entry_rcu(orp, ohead, list) {
352 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
358 list_add_rcu(&nrp->list, nhead);
364 * smk_copy_relabel - copy smk_relabel labels list
365 * @nhead: new rules header pointer
366 * @ohead: old rules header pointer
367 * @gfp: type of the memory for the allocation
369 * Returns 0 on success, -ENOMEM on error
371 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
374 struct smack_known_list_elem *nklep;
375 struct smack_known_list_elem *oklep;
377 list_for_each_entry(oklep, ohead, list) {
378 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
380 smk_destroy_label_list(nhead);
383 nklep->smk_label = oklep->smk_label;
384 list_add(&nklep->list, nhead);
391 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
392 * @mode - input mode in form of PTRACE_MODE_*
394 * Returns a converted MAY_* mode usable by smack rules
396 static inline unsigned int smk_ptrace_mode(unsigned int mode)
398 if (mode & PTRACE_MODE_ATTACH)
399 return MAY_READWRITE;
400 if (mode & PTRACE_MODE_READ)
407 * smk_ptrace_rule_check - helper for ptrace access
408 * @tracer: tracer process
409 * @tracee_known: label entry of the process that's about to be traced
410 * @mode: ptrace attachment mode (PTRACE_MODE_*)
411 * @func: name of the function that called us, used for audit
413 * Returns 0 on access granted, -error on error
415 static int smk_ptrace_rule_check(struct task_struct *tracer,
416 struct smack_known *tracee_known,
417 unsigned int mode, const char *func)
420 struct smk_audit_info ad, *saip = NULL;
421 struct task_smack *tsp;
422 struct smack_known *tracer_known;
423 const struct cred *tracercred;
425 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
426 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
427 smk_ad_setfield_u_tsk(&ad, tracer);
432 tracercred = __task_cred(tracer);
433 tsp = smack_cred(tracercred);
434 tracer_known = smk_of_task(tsp);
436 if ((mode & PTRACE_MODE_ATTACH) &&
437 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
438 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
439 if (tracer_known->smk_known == tracee_known->smk_known)
441 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
443 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
449 smack_log(tracer_known->smk_known,
450 tracee_known->smk_known,
457 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
458 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
466 * We he, that is fun!
470 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
471 * @ctp: child task pointer
472 * @mode: ptrace attachment mode (PTRACE_MODE_*)
474 * Returns 0 if access is OK, an error code otherwise
476 * Do the capability checks.
478 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
480 struct smack_known *skp;
482 skp = smk_of_task_struct_obj(ctp);
484 return smk_ptrace_rule_check(current, skp, mode, __func__);
488 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
489 * @ptp: parent task pointer
491 * Returns 0 if access is OK, an error code otherwise
493 * Do the capability checks, and require PTRACE_MODE_ATTACH.
495 static int smack_ptrace_traceme(struct task_struct *ptp)
498 struct smack_known *skp;
500 skp = smk_of_task(smack_cred(current_cred()));
502 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
507 * smack_syslog - Smack approval on syslog
508 * @typefrom_file: unused
510 * Returns 0 on success, error code otherwise.
512 static int smack_syslog(int typefrom_file)
515 struct smack_known *skp = smk_of_current();
517 if (smack_privileged(CAP_MAC_OVERRIDE))
520 if (smack_syslog_label != NULL && smack_syslog_label != skp)
531 * smack_sb_alloc_security - allocate a superblock blob
532 * @sb: the superblock getting the blob
534 * Returns 0 on success or -ENOMEM on error.
536 static int smack_sb_alloc_security(struct super_block *sb)
538 struct superblock_smack *sbsp;
540 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
545 sbsp->smk_root = &smack_known_floor;
546 sbsp->smk_default = &smack_known_floor;
547 sbsp->smk_floor = &smack_known_floor;
548 sbsp->smk_hat = &smack_known_hat;
550 * SMK_SB_INITIALIZED will be zero from kzalloc.
552 sb->s_security = sbsp;
558 * smack_sb_free_security - free a superblock blob
559 * @sb: the superblock getting the blob
562 static void smack_sb_free_security(struct super_block *sb)
564 kfree(sb->s_security);
565 sb->s_security = NULL;
568 struct smack_mnt_opts {
569 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
572 static void smack_free_mnt_opts(void *mnt_opts)
574 struct smack_mnt_opts *opts = mnt_opts;
575 kfree(opts->fsdefault);
576 kfree(opts->fsfloor);
579 kfree(opts->fstransmute);
583 static int smack_add_opt(int token, const char *s, void **mnt_opts)
585 struct smack_mnt_opts *opts = *mnt_opts;
588 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
617 case Opt_fstransmute:
618 if (opts->fstransmute)
620 opts->fstransmute = s;
626 pr_warn("Smack: duplicate mount options\n");
631 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
632 * @fc: The new filesystem context.
633 * @src_fc: The source filesystem context being duplicated.
635 * Returns 0 on success or -ENOMEM on error.
637 static int smack_fs_context_dup(struct fs_context *fc,
638 struct fs_context *src_fc)
640 struct smack_mnt_opts *dst, *src = src_fc->security;
645 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
650 if (src->fsdefault) {
651 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
656 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
661 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
666 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
670 if (src->fstransmute) {
671 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
672 if (!dst->fstransmute)
678 static const struct fs_parameter_spec smack_fs_parameters[] = {
679 fsparam_string("smackfsdef", Opt_fsdefault),
680 fsparam_string("smackfsdefault", Opt_fsdefault),
681 fsparam_string("smackfsfloor", Opt_fsfloor),
682 fsparam_string("smackfshat", Opt_fshat),
683 fsparam_string("smackfsroot", Opt_fsroot),
684 fsparam_string("smackfstransmute", Opt_fstransmute),
689 * smack_fs_context_parse_param - Parse a single mount parameter
690 * @fc: The new filesystem context being constructed.
691 * @param: The parameter.
693 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
696 static int smack_fs_context_parse_param(struct fs_context *fc,
697 struct fs_parameter *param)
699 struct fs_parse_result result;
702 opt = fs_parse(fc, smack_fs_parameters, param, &result);
706 rc = smack_add_opt(opt, param->string, &fc->security);
708 param->string = NULL;
712 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
714 char *from = options, *to = options;
718 char *next = strchr(from, ',');
727 token = match_opt_prefix(from, len, &arg);
728 if (token != Opt_error) {
729 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
730 rc = smack_add_opt(token, arg, mnt_opts);
734 smack_free_mnt_opts(*mnt_opts);
739 if (!first) { // copy with preceding comma
744 memmove(to, from, len);
757 * smack_set_mnt_opts - set Smack specific mount options
758 * @sb: the file system superblock
759 * @mnt_opts: Smack mount options
760 * @kern_flags: mount option from kernel space or user space
761 * @set_kern_flags: where to store converted mount opts
763 * Returns 0 on success, an error code on failure
765 * Allow filesystems with binary mount data to explicitly set Smack mount
768 static int smack_set_mnt_opts(struct super_block *sb,
770 unsigned long kern_flags,
771 unsigned long *set_kern_flags)
773 struct dentry *root = sb->s_root;
774 struct inode *inode = d_backing_inode(root);
775 struct superblock_smack *sp = sb->s_security;
776 struct inode_smack *isp;
777 struct smack_known *skp;
778 struct smack_mnt_opts *opts = mnt_opts;
779 bool transmute = false;
781 if (sp->smk_flags & SMK_SB_INITIALIZED)
784 if (inode->i_security == NULL) {
785 int rc = lsm_inode_alloc(inode);
791 if (!smack_privileged(CAP_MAC_ADMIN)) {
793 * Unprivileged mounts don't get to specify Smack values.
798 * Unprivileged mounts get root and default from the caller.
800 skp = smk_of_current();
802 sp->smk_default = skp;
804 * For a handful of fs types with no user-controlled
805 * backing store it's okay to trust security labels
806 * in the filesystem. The rest are untrusted.
808 if (sb->s_user_ns != &init_user_ns &&
809 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
810 sb->s_magic != RAMFS_MAGIC) {
812 sp->smk_flags |= SMK_SB_UNTRUSTED;
816 sp->smk_flags |= SMK_SB_INITIALIZED;
819 if (opts->fsdefault) {
820 skp = smk_import_entry(opts->fsdefault, 0);
823 sp->smk_default = skp;
826 skp = smk_import_entry(opts->fsfloor, 0);
832 skp = smk_import_entry(opts->fshat, 0);
838 skp = smk_import_entry(opts->fsroot, 0);
843 if (opts->fstransmute) {
844 skp = smk_import_entry(opts->fstransmute, 0);
853 * Initialize the root inode.
855 init_inode_smack(inode, sp->smk_root);
858 isp = smack_inode(inode);
859 isp->smk_flags |= SMK_INODE_TRANSMUTE;
866 * smack_sb_statfs - Smack check on statfs
867 * @dentry: identifies the file system in question
869 * Returns 0 if current can read the floor of the filesystem,
870 * and error code otherwise
872 static int smack_sb_statfs(struct dentry *dentry)
874 struct superblock_smack *sbp = dentry->d_sb->s_security;
876 struct smk_audit_info ad;
878 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
879 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
881 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
882 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
891 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
892 * @bprm: the exec information
894 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
896 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
898 struct inode *inode = file_inode(bprm->file);
899 struct task_smack *bsp = smack_cred(bprm->cred);
900 struct inode_smack *isp;
901 struct superblock_smack *sbsp;
904 isp = smack_inode(inode);
905 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
908 sbsp = inode->i_sb->s_security;
909 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
910 isp->smk_task != sbsp->smk_root)
913 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
914 struct task_struct *tracer;
918 tracer = ptrace_parent(current);
919 if (likely(tracer != NULL))
920 rc = smk_ptrace_rule_check(tracer,
929 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
932 bsp->smk_task = isp->smk_task;
933 bprm->per_clear |= PER_CLEAR_ON_SETID;
935 /* Decide if this is a secure exec. */
936 if (bsp->smk_task != bsp->smk_forked)
937 bprm->secureexec = 1;
947 * smack_inode_alloc_security - allocate an inode blob
948 * @inode: the inode in need of a blob
952 static int smack_inode_alloc_security(struct inode *inode)
954 struct smack_known *skp = smk_of_current();
956 init_inode_smack(inode, skp);
961 * smack_inode_init_security - copy out the smack from an inode
962 * @inode: the newly created inode
963 * @dir: containing directory object
965 * @name: where to put the attribute name
966 * @value: where to put the attribute value
967 * @len: where to put the length of the attribute
969 * Returns 0 if it all works out, -ENOMEM if there's no memory
971 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
972 const struct qstr *qstr, const char **name,
973 void **value, size_t *len)
975 struct inode_smack *issp = smack_inode(inode);
976 struct smack_known *skp = smk_of_current();
977 struct smack_known *isp = smk_of_inode(inode);
978 struct smack_known *dsp = smk_of_inode(dir);
982 *name = XATTR_SMACK_SUFFIX;
986 may = smk_access_entry(skp->smk_known, dsp->smk_known,
991 * If the access rule allows transmutation and
992 * the directory requests transmutation then
993 * by all means transmute.
994 * Mark the inode as changed.
996 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
997 smk_inode_transmutable(dir)) {
999 issp->smk_flags |= SMK_INODE_CHANGED;
1002 *value = kstrdup(isp->smk_known, GFP_NOFS);
1006 *len = strlen(isp->smk_known);
1013 * smack_inode_link - Smack check on link
1014 * @old_dentry: the existing object
1016 * @new_dentry: the new object
1018 * Returns 0 if access is permitted, an error code otherwise
1020 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1021 struct dentry *new_dentry)
1023 struct smack_known *isp;
1024 struct smk_audit_info ad;
1027 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1028 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1030 isp = smk_of_inode(d_backing_inode(old_dentry));
1031 rc = smk_curacc(isp, MAY_WRITE, &ad);
1032 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1034 if (rc == 0 && d_is_positive(new_dentry)) {
1035 isp = smk_of_inode(d_backing_inode(new_dentry));
1036 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1037 rc = smk_curacc(isp, MAY_WRITE, &ad);
1038 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1045 * smack_inode_unlink - Smack check on inode deletion
1046 * @dir: containing directory object
1047 * @dentry: file to unlink
1049 * Returns 0 if current can write the containing directory
1050 * and the object, error code otherwise
1052 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1054 struct inode *ip = d_backing_inode(dentry);
1055 struct smk_audit_info ad;
1058 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1059 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1062 * You need write access to the thing you're unlinking
1064 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1065 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1068 * You also need write access to the containing directory
1070 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1071 smk_ad_setfield_u_fs_inode(&ad, dir);
1072 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1073 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1079 * smack_inode_rmdir - Smack check on directory deletion
1080 * @dir: containing directory object
1081 * @dentry: directory to unlink
1083 * Returns 0 if current can write the containing directory
1084 * and the directory, error code otherwise
1086 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1088 struct smk_audit_info ad;
1091 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1092 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1095 * You need write access to the thing you're removing
1097 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1098 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1101 * You also need write access to the containing directory
1103 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1104 smk_ad_setfield_u_fs_inode(&ad, dir);
1105 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1106 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1113 * smack_inode_rename - Smack check on rename
1114 * @old_inode: unused
1115 * @old_dentry: the old object
1116 * @new_inode: unused
1117 * @new_dentry: the new object
1119 * Read and write access is required on both the old and
1122 * Returns 0 if access is permitted, an error code otherwise
1124 static int smack_inode_rename(struct inode *old_inode,
1125 struct dentry *old_dentry,
1126 struct inode *new_inode,
1127 struct dentry *new_dentry)
1130 struct smack_known *isp;
1131 struct smk_audit_info ad;
1133 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1134 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1136 isp = smk_of_inode(d_backing_inode(old_dentry));
1137 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1138 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1140 if (rc == 0 && d_is_positive(new_dentry)) {
1141 isp = smk_of_inode(d_backing_inode(new_dentry));
1142 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1143 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1144 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1150 * smack_inode_permission - Smack version of permission()
1151 * @inode: the inode in question
1152 * @mask: the access requested
1154 * This is the important Smack hook.
1156 * Returns 0 if access is permitted, an error code otherwise
1158 static int smack_inode_permission(struct inode *inode, int mask)
1160 struct superblock_smack *sbsp = inode->i_sb->s_security;
1161 struct smk_audit_info ad;
1162 int no_block = mask & MAY_NOT_BLOCK;
1165 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1167 * No permission to check. Existence test. Yup, it's there.
1172 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1173 if (smk_of_inode(inode) != sbsp->smk_root)
1177 /* May be droppable after audit */
1180 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1181 smk_ad_setfield_u_fs_inode(&ad, inode);
1182 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1183 rc = smk_bu_inode(inode, mask, rc);
1188 * smack_inode_setattr - Smack check for setting attributes
1189 * @dentry: the object
1190 * @iattr: for the force flag
1192 * Returns 0 if access is permitted, an error code otherwise
1194 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1196 struct smk_audit_info ad;
1200 * Need to allow for clearing the setuid bit.
1202 if (iattr->ia_valid & ATTR_FORCE)
1204 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1205 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1207 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1208 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1213 * smack_inode_getattr - Smack check for getting attributes
1214 * @path: path to extract the info from
1216 * Returns 0 if access is permitted, an error code otherwise
1218 static int smack_inode_getattr(const struct path *path)
1220 struct smk_audit_info ad;
1221 struct inode *inode = d_backing_inode(path->dentry);
1224 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1225 smk_ad_setfield_u_fs_path(&ad, *path);
1226 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1227 rc = smk_bu_inode(inode, MAY_READ, rc);
1232 * smack_inode_setxattr - Smack check for setting xattrs
1233 * @dentry: the object
1234 * @name: name of the attribute
1235 * @value: value of the attribute
1236 * @size: size of the value
1239 * This protects the Smack attribute explicitly.
1241 * Returns 0 if access is permitted, an error code otherwise
1243 static int smack_inode_setxattr(struct user_namespace *mnt_userns,
1244 struct dentry *dentry, const char *name,
1245 const void *value, size_t size, int flags)
1247 struct smk_audit_info ad;
1248 struct smack_known *skp;
1250 int check_import = 0;
1255 * Check label validity here so import won't fail in post_setxattr
1257 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1258 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1259 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1262 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1263 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1267 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1269 if (size != TRANS_TRUE_SIZE ||
1270 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1273 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1275 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1278 if (rc == 0 && check_import) {
1279 skp = size ? smk_import_entry(value, size) : NULL;
1282 else if (skp == NULL || (check_star &&
1283 (skp == &smack_known_star || skp == &smack_known_web)))
1287 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1288 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1291 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1292 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1299 * smack_inode_post_setxattr - Apply the Smack update approved above
1301 * @name: attribute name
1302 * @value: attribute value
1303 * @size: attribute size
1306 * Set the pointer in the inode blob to the entry found
1307 * in the master label list.
1309 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1310 const void *value, size_t size, int flags)
1312 struct smack_known *skp;
1313 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1315 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1316 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1320 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1321 skp = smk_import_entry(value, size);
1323 isp->smk_inode = skp;
1324 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1325 skp = smk_import_entry(value, size);
1327 isp->smk_task = skp;
1328 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1329 skp = smk_import_entry(value, size);
1331 isp->smk_mmap = skp;
1338 * smack_inode_getxattr - Smack check on getxattr
1339 * @dentry: the object
1342 * Returns 0 if access is permitted, an error code otherwise
1344 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1346 struct smk_audit_info ad;
1349 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1350 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1352 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1353 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1358 * smack_inode_removexattr - Smack check on removexattr
1359 * @dentry: the object
1360 * @name: name of the attribute
1362 * Removing the Smack attribute requires CAP_MAC_ADMIN
1364 * Returns 0 if access is permitted, an error code otherwise
1366 static int smack_inode_removexattr(struct user_namespace *mnt_userns,
1367 struct dentry *dentry, const char *name)
1369 struct inode_smack *isp;
1370 struct smk_audit_info ad;
1373 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1374 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1375 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1376 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1377 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1378 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1379 if (!smack_privileged(CAP_MAC_ADMIN))
1382 rc = cap_inode_removexattr(mnt_userns, dentry, name);
1387 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1388 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1390 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1391 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1395 isp = smack_inode(d_backing_inode(dentry));
1397 * Don't do anything special for these.
1398 * XATTR_NAME_SMACKIPIN
1399 * XATTR_NAME_SMACKIPOUT
1401 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1402 struct super_block *sbp = dentry->d_sb;
1403 struct superblock_smack *sbsp = sbp->s_security;
1405 isp->smk_inode = sbsp->smk_default;
1406 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1407 isp->smk_task = NULL;
1408 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1409 isp->smk_mmap = NULL;
1410 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1411 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1417 * smack_inode_getsecurity - get smack xattrs
1418 * @inode: the object
1419 * @name: attribute name
1420 * @buffer: where to put the result
1421 * @alloc: duplicate memory
1423 * Returns the size of the attribute or an error code
1425 static int smack_inode_getsecurity(struct user_namespace *mnt_userns,
1426 struct inode *inode, const char *name,
1427 void **buffer, bool alloc)
1429 struct socket_smack *ssp;
1430 struct socket *sock;
1431 struct super_block *sbp;
1432 struct inode *ip = (struct inode *)inode;
1433 struct smack_known *isp;
1435 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1436 isp = smk_of_inode(inode);
1439 * The rest of the Smack xattrs are only on sockets.
1442 if (sbp->s_magic != SOCKFS_MAGIC)
1445 sock = SOCKET_I(ip);
1446 if (sock == NULL || sock->sk == NULL)
1449 ssp = sock->sk->sk_security;
1451 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1453 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1460 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1461 if (*buffer == NULL)
1465 return strlen(isp->smk_known);
1470 * smack_inode_listsecurity - list the Smack attributes
1471 * @inode: the object
1472 * @buffer: where they go
1473 * @buffer_size: size of buffer
1475 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1478 int len = sizeof(XATTR_NAME_SMACK);
1480 if (buffer != NULL && len <= buffer_size)
1481 memcpy(buffer, XATTR_NAME_SMACK, len);
1487 * smack_inode_getsecid - Extract inode's security id
1488 * @inode: inode to extract the info from
1489 * @secid: where result will be saved
1491 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1493 struct smack_known *skp = smk_of_inode(inode);
1495 *secid = skp->smk_secid;
1503 * There is no smack_file_permission hook
1505 * Should access checks be done on each read or write?
1506 * UNICOS and SELinux say yes.
1507 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1509 * I'll say no for now. Smack does not do the frequent
1510 * label changing that SELinux does.
1514 * smack_file_alloc_security - assign a file security blob
1517 * The security blob for a file is a pointer to the master
1518 * label list, so no allocation is done.
1520 * f_security is the owner security information. It
1521 * isn't used on file access checks, it's for send_sigio.
1525 static int smack_file_alloc_security(struct file *file)
1527 struct smack_known **blob = smack_file(file);
1529 *blob = smk_of_current();
1534 * smack_file_ioctl - Smack check on ioctls
1539 * Relies heavily on the correct use of the ioctl command conventions.
1541 * Returns 0 if allowed, error code otherwise
1543 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1547 struct smk_audit_info ad;
1548 struct inode *inode = file_inode(file);
1550 if (unlikely(IS_PRIVATE(inode)))
1553 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1554 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1556 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1557 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1558 rc = smk_bu_file(file, MAY_WRITE, rc);
1561 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1562 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1563 rc = smk_bu_file(file, MAY_READ, rc);
1570 * smack_file_lock - Smack check on file locking
1574 * Returns 0 if current has lock access, error code otherwise
1576 static int smack_file_lock(struct file *file, unsigned int cmd)
1578 struct smk_audit_info ad;
1580 struct inode *inode = file_inode(file);
1582 if (unlikely(IS_PRIVATE(inode)))
1585 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1586 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1587 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1588 rc = smk_bu_file(file, MAY_LOCK, rc);
1593 * smack_file_fcntl - Smack check on fcntl
1595 * @cmd: what action to check
1598 * Generally these operations are harmless.
1599 * File locking operations present an obvious mechanism
1600 * for passing information, so they require write access.
1602 * Returns 0 if current has access, error code otherwise
1604 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1607 struct smk_audit_info ad;
1609 struct inode *inode = file_inode(file);
1611 if (unlikely(IS_PRIVATE(inode)))
1619 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1620 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1621 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1622 rc = smk_bu_file(file, MAY_LOCK, rc);
1626 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1627 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1628 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1629 rc = smk_bu_file(file, MAY_WRITE, rc);
1640 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1641 * if mapping anonymous memory.
1642 * @file contains the file structure for file to map (may be NULL).
1643 * @reqprot contains the protection requested by the application.
1644 * @prot contains the protection that will be applied by the kernel.
1645 * @flags contains the operational flags.
1646 * Return 0 if permission is granted.
1648 static int smack_mmap_file(struct file *file,
1649 unsigned long reqprot, unsigned long prot,
1650 unsigned long flags)
1652 struct smack_known *skp;
1653 struct smack_known *mkp;
1654 struct smack_rule *srp;
1655 struct task_smack *tsp;
1656 struct smack_known *okp;
1657 struct inode_smack *isp;
1658 struct superblock_smack *sbsp;
1667 if (unlikely(IS_PRIVATE(file_inode(file))))
1670 isp = smack_inode(file_inode(file));
1671 if (isp->smk_mmap == NULL)
1673 sbsp = file_inode(file)->i_sb->s_security;
1674 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1675 isp->smk_mmap != sbsp->smk_root)
1677 mkp = isp->smk_mmap;
1679 tsp = smack_cred(current_cred());
1680 skp = smk_of_current();
1685 * For each Smack rule associated with the subject
1686 * label verify that the SMACK64MMAP also has access
1687 * to that rule's object label.
1689 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1690 okp = srp->smk_object;
1692 * Matching labels always allows access.
1694 if (mkp->smk_known == okp->smk_known)
1697 * If there is a matching local rule take
1698 * that into account as well.
1700 may = smk_access_entry(srp->smk_subject->smk_known,
1704 may = srp->smk_access;
1706 may &= srp->smk_access;
1708 * If may is zero the SMACK64MMAP subject can't
1709 * possibly have less access.
1715 * Fetch the global list entry.
1716 * If there isn't one a SMACK64MMAP subject
1717 * can't have as much access as current.
1719 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1721 if (mmay == -ENOENT) {
1726 * If there is a local entry it modifies the
1727 * potential access, too.
1729 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1731 if (tmay != -ENOENT)
1735 * If there is any access available to current that is
1736 * not available to a SMACK64MMAP subject
1739 if ((may | mmay) != mmay) {
1751 * smack_file_set_fowner - set the file security blob value
1752 * @file: object in question
1755 static void smack_file_set_fowner(struct file *file)
1757 struct smack_known **blob = smack_file(file);
1759 *blob = smk_of_current();
1763 * smack_file_send_sigiotask - Smack on sigio
1764 * @tsk: The target task
1765 * @fown: the object the signal come from
1768 * Allow a privileged task to get signals even if it shouldn't
1770 * Returns 0 if a subject with the object's smack could
1771 * write to the task, an error code otherwise.
1773 static int smack_file_send_sigiotask(struct task_struct *tsk,
1774 struct fown_struct *fown, int signum)
1776 struct smack_known **blob;
1777 struct smack_known *skp;
1778 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1779 const struct cred *tcred;
1782 struct smk_audit_info ad;
1785 * struct fown_struct is never outside the context of a struct file
1787 file = container_of(fown, struct file, f_owner);
1789 /* we don't log here as rc can be overriden */
1790 blob = smack_file(file);
1792 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1793 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1796 tcred = __task_cred(tsk);
1797 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1801 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1802 smk_ad_setfield_u_tsk(&ad, tsk);
1803 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1808 * smack_file_receive - Smack file receive check
1811 * Returns 0 if current has access, error code otherwise
1813 static int smack_file_receive(struct file *file)
1817 struct smk_audit_info ad;
1818 struct inode *inode = file_inode(file);
1819 struct socket *sock;
1820 struct task_smack *tsp;
1821 struct socket_smack *ssp;
1823 if (unlikely(IS_PRIVATE(inode)))
1826 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1827 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1829 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1830 sock = SOCKET_I(inode);
1831 ssp = sock->sk->sk_security;
1832 tsp = smack_cred(current_cred());
1834 * If the receiving process can't write to the
1835 * passed socket or if the passed socket can't
1836 * write to the receiving process don't accept
1837 * the passed socket.
1839 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1840 rc = smk_bu_file(file, may, rc);
1843 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1844 rc = smk_bu_file(file, may, rc);
1848 * This code relies on bitmasks.
1850 if (file->f_mode & FMODE_READ)
1852 if (file->f_mode & FMODE_WRITE)
1855 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1856 rc = smk_bu_file(file, may, rc);
1861 * smack_file_open - Smack dentry open processing
1864 * Set the security blob in the file structure.
1865 * Allow the open only if the task has read access. There are
1866 * many read operations (e.g. fstat) that you can do with an
1867 * fd even if you have the file open write-only.
1869 * Returns 0 if current has access, error code otherwise
1871 static int smack_file_open(struct file *file)
1873 struct task_smack *tsp = smack_cred(file->f_cred);
1874 struct inode *inode = file_inode(file);
1875 struct smk_audit_info ad;
1878 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1879 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1880 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1881 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1891 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1892 * @cred: the new credentials
1893 * @gfp: the atomicity of any memory allocations
1895 * Prepare a blank set of credentials for modification. This must allocate all
1896 * the memory the LSM module might require such that cred_transfer() can
1897 * complete without error.
1899 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1901 init_task_smack(smack_cred(cred), NULL, NULL);
1907 * smack_cred_free - "free" task-level security credentials
1908 * @cred: the credentials in question
1911 static void smack_cred_free(struct cred *cred)
1913 struct task_smack *tsp = smack_cred(cred);
1914 struct smack_rule *rp;
1915 struct list_head *l;
1916 struct list_head *n;
1918 smk_destroy_label_list(&tsp->smk_relabel);
1920 list_for_each_safe(l, n, &tsp->smk_rules) {
1921 rp = list_entry(l, struct smack_rule, list);
1922 list_del(&rp->list);
1923 kmem_cache_free(smack_rule_cache, rp);
1928 * smack_cred_prepare - prepare new set of credentials for modification
1929 * @new: the new credentials
1930 * @old: the original credentials
1931 * @gfp: the atomicity of any memory allocations
1933 * Prepare a new set of credentials for modification.
1935 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1938 struct task_smack *old_tsp = smack_cred(old);
1939 struct task_smack *new_tsp = smack_cred(new);
1942 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1944 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1948 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1954 * smack_cred_transfer - Transfer the old credentials to the new credentials
1955 * @new: the new credentials
1956 * @old: the original credentials
1958 * Fill in a set of blank credentials from another set of credentials.
1960 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1962 struct task_smack *old_tsp = smack_cred(old);
1963 struct task_smack *new_tsp = smack_cred(new);
1965 new_tsp->smk_task = old_tsp->smk_task;
1966 new_tsp->smk_forked = old_tsp->smk_task;
1967 mutex_init(&new_tsp->smk_rules_lock);
1968 INIT_LIST_HEAD(&new_tsp->smk_rules);
1970 /* cbs copy rule list */
1974 * smack_cred_getsecid - get the secid corresponding to a creds structure
1975 * @cred: the object creds
1976 * @secid: where to put the result
1978 * Sets the secid to contain a u32 version of the smack label.
1980 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1982 struct smack_known *skp;
1985 skp = smk_of_task(smack_cred(cred));
1986 *secid = skp->smk_secid;
1991 * smack_kernel_act_as - Set the subjective context in a set of credentials
1992 * @new: points to the set of credentials to be modified.
1993 * @secid: specifies the security ID to be set
1995 * Set the security data for a kernel service.
1997 static int smack_kernel_act_as(struct cred *new, u32 secid)
1999 struct task_smack *new_tsp = smack_cred(new);
2001 new_tsp->smk_task = smack_from_secid(secid);
2006 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2007 * @new: points to the set of credentials to be modified
2008 * @inode: points to the inode to use as a reference
2010 * Set the file creation context in a set of credentials to the same
2011 * as the objective context of the specified inode
2013 static int smack_kernel_create_files_as(struct cred *new,
2014 struct inode *inode)
2016 struct inode_smack *isp = smack_inode(inode);
2017 struct task_smack *tsp = smack_cred(new);
2019 tsp->smk_forked = isp->smk_inode;
2020 tsp->smk_task = tsp->smk_forked;
2025 * smk_curacc_on_task - helper to log task related access
2026 * @p: the task object
2027 * @access: the access requested
2028 * @caller: name of the calling function for audit
2030 * Return 0 if access is permitted
2032 static int smk_curacc_on_task(struct task_struct *p, int access,
2035 struct smk_audit_info ad;
2036 struct smack_known *skp = smk_of_task_struct_subj(p);
2039 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2040 smk_ad_setfield_u_tsk(&ad, p);
2041 rc = smk_curacc(skp, access, &ad);
2042 rc = smk_bu_task(p, access, rc);
2047 * smack_task_setpgid - Smack check on setting pgid
2048 * @p: the task object
2051 * Return 0 if write access is permitted
2053 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2055 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2059 * smack_task_getpgid - Smack access check for getpgid
2060 * @p: the object task
2062 * Returns 0 if current can read the object task, error code otherwise
2064 static int smack_task_getpgid(struct task_struct *p)
2066 return smk_curacc_on_task(p, MAY_READ, __func__);
2070 * smack_task_getsid - Smack access check for getsid
2071 * @p: the object task
2073 * Returns 0 if current can read the object task, error code otherwise
2075 static int smack_task_getsid(struct task_struct *p)
2077 return smk_curacc_on_task(p, MAY_READ, __func__);
2081 * smack_task_getsecid_subj - get the subjective secid of the task
2083 * @secid: where to put the result
2085 * Sets the secid to contain a u32 version of the task's subjective smack label.
2087 static void smack_task_getsecid_subj(struct task_struct *p, u32 *secid)
2089 struct smack_known *skp = smk_of_task_struct_subj(p);
2091 *secid = skp->smk_secid;
2095 * smack_task_getsecid_obj - get the objective secid of the task
2097 * @secid: where to put the result
2099 * Sets the secid to contain a u32 version of the task's objective smack label.
2101 static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2103 struct smack_known *skp = smk_of_task_struct_obj(p);
2105 *secid = skp->smk_secid;
2109 * smack_task_setnice - Smack check on setting nice
2110 * @p: the task object
2113 * Return 0 if write access is permitted
2115 static int smack_task_setnice(struct task_struct *p, int nice)
2117 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2121 * smack_task_setioprio - Smack check on setting ioprio
2122 * @p: the task object
2125 * Return 0 if write access is permitted
2127 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2129 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2133 * smack_task_getioprio - Smack check on reading ioprio
2134 * @p: the task object
2136 * Return 0 if read access is permitted
2138 static int smack_task_getioprio(struct task_struct *p)
2140 return smk_curacc_on_task(p, MAY_READ, __func__);
2144 * smack_task_setscheduler - Smack check on setting scheduler
2145 * @p: the task object
2147 * Return 0 if read access is permitted
2149 static int smack_task_setscheduler(struct task_struct *p)
2151 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2155 * smack_task_getscheduler - Smack check on reading scheduler
2156 * @p: the task object
2158 * Return 0 if read access is permitted
2160 static int smack_task_getscheduler(struct task_struct *p)
2162 return smk_curacc_on_task(p, MAY_READ, __func__);
2166 * smack_task_movememory - Smack check on moving memory
2167 * @p: the task object
2169 * Return 0 if write access is permitted
2171 static int smack_task_movememory(struct task_struct *p)
2173 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2177 * smack_task_kill - Smack check on signal delivery
2178 * @p: the task object
2181 * @cred: identifies the cred to use in lieu of current's
2183 * Return 0 if write access is permitted
2186 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2187 int sig, const struct cred *cred)
2189 struct smk_audit_info ad;
2190 struct smack_known *skp;
2191 struct smack_known *tkp = smk_of_task_struct_obj(p);
2195 return 0; /* null signal; existence test */
2197 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2198 smk_ad_setfield_u_tsk(&ad, p);
2200 * Sending a signal requires that the sender
2201 * can write the receiver.
2204 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2205 rc = smk_bu_task(p, MAY_DELIVER, rc);
2209 * If the cred isn't NULL we're dealing with some USB IO
2210 * specific behavior. This is not clean. For one thing
2211 * we can't take privilege into account.
2213 skp = smk_of_task(smack_cred(cred));
2214 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2215 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2220 * smack_task_to_inode - copy task smack into the inode blob
2221 * @p: task to copy from
2222 * @inode: inode to copy to
2224 * Sets the smack pointer in the inode security blob
2226 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2228 struct inode_smack *isp = smack_inode(inode);
2229 struct smack_known *skp = smk_of_task_struct_obj(p);
2231 isp->smk_inode = skp;
2232 isp->smk_flags |= SMK_INODE_INSTANT;
2240 * smack_sk_alloc_security - Allocate a socket blob
2243 * @gfp_flags: memory allocation flags
2245 * Assign Smack pointers to current
2247 * Returns 0 on success, -ENOMEM is there's no memory
2249 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2251 struct smack_known *skp = smk_of_current();
2252 struct socket_smack *ssp;
2254 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2259 * Sockets created by kernel threads receive web label.
2261 if (unlikely(current->flags & PF_KTHREAD)) {
2262 ssp->smk_in = &smack_known_web;
2263 ssp->smk_out = &smack_known_web;
2268 ssp->smk_packet = NULL;
2270 sk->sk_security = ssp;
2276 * smack_sk_free_security - Free a socket blob
2279 * Clears the blob pointer
2281 static void smack_sk_free_security(struct sock *sk)
2283 #ifdef SMACK_IPV6_PORT_LABELING
2284 struct smk_port_label *spp;
2286 if (sk->sk_family == PF_INET6) {
2288 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2289 if (spp->smk_sock != sk)
2291 spp->smk_can_reuse = 1;
2297 kfree(sk->sk_security);
2301 * smack_ipv4host_label - check host based restrictions
2302 * @sip: the object end
2304 * looks for host based access restrictions
2306 * This version will only be appropriate for really small sets of single label
2307 * hosts. The caller is responsible for ensuring that the RCU read lock is
2308 * taken before calling this function.
2310 * Returns the label of the far end or NULL if it's not special.
2312 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2314 struct smk_net4addr *snp;
2315 struct in_addr *siap = &sip->sin_addr;
2317 if (siap->s_addr == 0)
2320 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2322 * we break after finding the first match because
2323 * the list is sorted from longest to shortest mask
2324 * so we have found the most specific match
2326 if (snp->smk_host.s_addr ==
2327 (siap->s_addr & snp->smk_mask.s_addr))
2328 return snp->smk_label;
2334 * smk_ipv6_localhost - Check for local ipv6 host address
2337 * Returns boolean true if this is the localhost address
2339 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2341 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2342 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2344 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2345 ntohs(be16p[7]) == 1)
2351 * smack_ipv6host_label - check host based restrictions
2352 * @sip: the object end
2354 * looks for host based access restrictions
2356 * This version will only be appropriate for really small sets of single label
2357 * hosts. The caller is responsible for ensuring that the RCU read lock is
2358 * taken before calling this function.
2360 * Returns the label of the far end or NULL if it's not special.
2362 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2364 struct smk_net6addr *snp;
2365 struct in6_addr *sap = &sip->sin6_addr;
2370 * It's local. Don't look for a host label.
2372 if (smk_ipv6_localhost(sip))
2375 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2377 * If the label is NULL the entry has
2378 * been renounced. Ignore it.
2380 if (snp->smk_label == NULL)
2383 * we break after finding the first match because
2384 * the list is sorted from longest to shortest mask
2385 * so we have found the most specific match
2387 for (found = 1, i = 0; i < 8; i++) {
2388 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2389 snp->smk_host.s6_addr16[i]) {
2395 return snp->smk_label;
2402 * smack_netlbl_add - Set the secattr on a socket
2405 * Attach the outbound smack value (smk_out) to the socket.
2407 * Returns 0 on success or an error code
2409 static int smack_netlbl_add(struct sock *sk)
2411 struct socket_smack *ssp = sk->sk_security;
2412 struct smack_known *skp = ssp->smk_out;
2416 bh_lock_sock_nested(sk);
2418 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2421 ssp->smk_state = SMK_NETLBL_LABELED;
2424 ssp->smk_state = SMK_NETLBL_REQSKB;
2436 * smack_netlbl_delete - Remove the secattr from a socket
2439 * Remove the outbound smack value from a socket
2441 static void smack_netlbl_delete(struct sock *sk)
2443 struct socket_smack *ssp = sk->sk_security;
2446 * Take the label off the socket if one is set.
2448 if (ssp->smk_state != SMK_NETLBL_LABELED)
2452 bh_lock_sock_nested(sk);
2453 netlbl_sock_delattr(sk);
2456 ssp->smk_state = SMK_NETLBL_UNLABELED;
2460 * smk_ipv4_check - Perform IPv4 host access checks
2462 * @sap: the destination address
2464 * Set the correct secattr for the given socket based on the destination
2465 * address and perform any outbound access checks needed.
2467 * Returns 0 on success or an error code.
2470 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2472 struct smack_known *skp;
2474 struct smack_known *hkp;
2475 struct socket_smack *ssp = sk->sk_security;
2476 struct smk_audit_info ad;
2479 hkp = smack_ipv4host_label(sap);
2482 struct lsm_network_audit net;
2484 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2485 ad.a.u.net->family = sap->sin_family;
2486 ad.a.u.net->dport = sap->sin_port;
2487 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2490 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2491 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2493 * Clear the socket netlabel if it's set.
2496 smack_netlbl_delete(sk);
2504 * smk_ipv6_check - check Smack access
2505 * @subject: subject Smack label
2506 * @object: object Smack label
2508 * @act: the action being taken
2510 * Check an IPv6 access
2512 static int smk_ipv6_check(struct smack_known *subject,
2513 struct smack_known *object,
2514 struct sockaddr_in6 *address, int act)
2517 struct lsm_network_audit net;
2519 struct smk_audit_info ad;
2523 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2524 ad.a.u.net->family = PF_INET6;
2525 ad.a.u.net->dport = ntohs(address->sin6_port);
2526 if (act == SMK_RECEIVING)
2527 ad.a.u.net->v6info.saddr = address->sin6_addr;
2529 ad.a.u.net->v6info.daddr = address->sin6_addr;
2531 rc = smk_access(subject, object, MAY_WRITE, &ad);
2532 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2536 #ifdef SMACK_IPV6_PORT_LABELING
2538 * smk_ipv6_port_label - Smack port access table management
2542 * Create or update the port list entry
2544 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2546 struct sock *sk = sock->sk;
2547 struct sockaddr_in6 *addr6;
2548 struct socket_smack *ssp = sock->sk->sk_security;
2549 struct smk_port_label *spp;
2550 unsigned short port = 0;
2552 if (address == NULL) {
2554 * This operation is changing the Smack information
2555 * on the bound socket. Take the changes to the port
2559 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2560 if (sk != spp->smk_sock)
2562 spp->smk_in = ssp->smk_in;
2563 spp->smk_out = ssp->smk_out;
2568 * A NULL address is only used for updating existing
2569 * bound entries. If there isn't one, it's OK.
2575 addr6 = (struct sockaddr_in6 *)address;
2576 port = ntohs(addr6->sin6_port);
2578 * This is a special case that is safely ignored.
2584 * Look for an existing port list entry.
2585 * This is an indication that a port is getting reused.
2588 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2589 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2591 if (spp->smk_can_reuse != 1) {
2595 spp->smk_port = port;
2597 spp->smk_in = ssp->smk_in;
2598 spp->smk_out = ssp->smk_out;
2599 spp->smk_can_reuse = 0;
2605 * A new port entry is required.
2607 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2611 spp->smk_port = port;
2613 spp->smk_in = ssp->smk_in;
2614 spp->smk_out = ssp->smk_out;
2615 spp->smk_sock_type = sock->type;
2616 spp->smk_can_reuse = 0;
2618 mutex_lock(&smack_ipv6_lock);
2619 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2620 mutex_unlock(&smack_ipv6_lock);
2626 * smk_ipv6_port_check - check Smack port access
2629 * @act: the action being taken
2631 * Create or update the port list entry
2633 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2636 struct smk_port_label *spp;
2637 struct socket_smack *ssp = sk->sk_security;
2638 struct smack_known *skp = NULL;
2639 unsigned short port;
2640 struct smack_known *object;
2642 if (act == SMK_RECEIVING) {
2643 skp = smack_ipv6host_label(address);
2644 object = ssp->smk_in;
2647 object = smack_ipv6host_label(address);
2651 * The other end is a single label host.
2653 if (skp != NULL && object != NULL)
2654 return smk_ipv6_check(skp, object, address, act);
2656 skp = smack_net_ambient;
2658 object = smack_net_ambient;
2661 * It's remote, so port lookup does no good.
2663 if (!smk_ipv6_localhost(address))
2664 return smk_ipv6_check(skp, object, address, act);
2667 * It's local so the send check has to have passed.
2669 if (act == SMK_RECEIVING)
2672 port = ntohs(address->sin6_port);
2674 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2675 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2677 object = spp->smk_in;
2678 if (act == SMK_CONNECTING)
2679 ssp->smk_packet = spp->smk_out;
2684 return smk_ipv6_check(skp, object, address, act);
2688 * smack_inode_setsecurity - set smack xattrs
2689 * @inode: the object
2690 * @name: attribute name
2691 * @value: attribute value
2692 * @size: size of the attribute
2695 * Sets the named attribute in the appropriate blob
2697 * Returns 0 on success, or an error code
2699 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2700 const void *value, size_t size, int flags)
2702 struct smack_known *skp;
2703 struct inode_smack *nsp = smack_inode(inode);
2704 struct socket_smack *ssp;
2705 struct socket *sock;
2708 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2711 skp = smk_import_entry(value, size);
2713 return PTR_ERR(skp);
2715 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2716 nsp->smk_inode = skp;
2717 nsp->smk_flags |= SMK_INODE_INSTANT;
2721 * The rest of the Smack xattrs are only on sockets.
2723 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2726 sock = SOCKET_I(inode);
2727 if (sock == NULL || sock->sk == NULL)
2730 ssp = sock->sk->sk_security;
2732 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2734 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2736 if (sock->sk->sk_family == PF_INET) {
2737 rc = smack_netlbl_add(sock->sk);
2740 "Smack: \"%s\" netlbl error %d.\n",
2746 #ifdef SMACK_IPV6_PORT_LABELING
2747 if (sock->sk->sk_family == PF_INET6)
2748 smk_ipv6_port_label(sock, NULL);
2755 * smack_socket_post_create - finish socket setup
2757 * @family: protocol family
2762 * Sets the netlabel information on the socket
2764 * Returns 0 on success, and error code otherwise
2766 static int smack_socket_post_create(struct socket *sock, int family,
2767 int type, int protocol, int kern)
2769 struct socket_smack *ssp;
2771 if (sock->sk == NULL)
2775 * Sockets created by kernel threads receive web label.
2777 if (unlikely(current->flags & PF_KTHREAD)) {
2778 ssp = sock->sk->sk_security;
2779 ssp->smk_in = &smack_known_web;
2780 ssp->smk_out = &smack_known_web;
2783 if (family != PF_INET)
2786 * Set the outbound netlbl.
2788 return smack_netlbl_add(sock->sk);
2792 * smack_socket_socketpair - create socket pair
2793 * @socka: one socket
2794 * @sockb: another socket
2796 * Cross reference the peer labels for SO_PEERSEC
2800 static int smack_socket_socketpair(struct socket *socka,
2801 struct socket *sockb)
2803 struct socket_smack *asp = socka->sk->sk_security;
2804 struct socket_smack *bsp = sockb->sk->sk_security;
2806 asp->smk_packet = bsp->smk_out;
2807 bsp->smk_packet = asp->smk_out;
2812 #ifdef SMACK_IPV6_PORT_LABELING
2814 * smack_socket_bind - record port binding information.
2816 * @address: the port address
2817 * @addrlen: size of the address
2819 * Records the label bound to a port.
2821 * Returns 0 on success, and error code otherwise
2823 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2826 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2827 if (addrlen < SIN6_LEN_RFC2133 ||
2828 address->sa_family != AF_INET6)
2830 smk_ipv6_port_label(sock, address);
2834 #endif /* SMACK_IPV6_PORT_LABELING */
2837 * smack_socket_connect - connect access check
2839 * @sap: the other end
2840 * @addrlen: size of sap
2842 * Verifies that a connection may be possible
2844 * Returns 0 on success, and error code otherwise
2846 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2851 if (sock->sk == NULL)
2853 if (sock->sk->sk_family != PF_INET &&
2854 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2856 if (addrlen < offsetofend(struct sockaddr, sa_family))
2858 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2859 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2860 struct smack_known *rsp = NULL;
2862 if (addrlen < SIN6_LEN_RFC2133)
2864 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2865 rsp = smack_ipv6host_label(sip);
2867 struct socket_smack *ssp = sock->sk->sk_security;
2869 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2872 if (__is_defined(SMACK_IPV6_PORT_LABELING))
2873 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2877 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2879 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
2884 * smack_flags_to_may - convert S_ to MAY_ values
2885 * @flags: the S_ value
2887 * Returns the equivalent MAY_ value
2889 static int smack_flags_to_may(int flags)
2893 if (flags & S_IRUGO)
2895 if (flags & S_IWUGO)
2897 if (flags & S_IXUGO)
2904 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2909 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2911 struct smack_known **blob = smack_msg_msg(msg);
2913 *blob = smk_of_current();
2918 * smack_of_ipc - the smack pointer for the ipc
2921 * Returns a pointer to the smack value
2923 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2925 struct smack_known **blob = smack_ipc(isp);
2931 * smack_ipc_alloc_security - Set the security blob for ipc
2936 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2938 struct smack_known **blob = smack_ipc(isp);
2940 *blob = smk_of_current();
2945 * smk_curacc_shm : check if current has access on shm
2947 * @access : access requested
2949 * Returns 0 if current has the requested access, error code otherwise
2951 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2953 struct smack_known *ssp = smack_of_ipc(isp);
2954 struct smk_audit_info ad;
2958 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2959 ad.a.u.ipc_id = isp->id;
2961 rc = smk_curacc(ssp, access, &ad);
2962 rc = smk_bu_current("shm", ssp, access, rc);
2967 * smack_shm_associate - Smack access check for shm
2969 * @shmflg: access requested
2971 * Returns 0 if current has the requested access, error code otherwise
2973 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2977 may = smack_flags_to_may(shmflg);
2978 return smk_curacc_shm(isp, may);
2982 * smack_shm_shmctl - Smack access check for shm
2984 * @cmd: what it wants to do
2986 * Returns 0 if current has the requested access, error code otherwise
2988 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3002 may = MAY_READWRITE;
3007 * System level information.
3013 return smk_curacc_shm(isp, may);
3017 * smack_shm_shmat - Smack access for shmat
3020 * @shmflg: access requested
3022 * Returns 0 if current has the requested access, error code otherwise
3024 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3029 may = smack_flags_to_may(shmflg);
3030 return smk_curacc_shm(isp, may);
3034 * smk_curacc_sem : check if current has access on sem
3036 * @access : access requested
3038 * Returns 0 if current has the requested access, error code otherwise
3040 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3042 struct smack_known *ssp = smack_of_ipc(isp);
3043 struct smk_audit_info ad;
3047 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3048 ad.a.u.ipc_id = isp->id;
3050 rc = smk_curacc(ssp, access, &ad);
3051 rc = smk_bu_current("sem", ssp, access, rc);
3056 * smack_sem_associate - Smack access check for sem
3058 * @semflg: access requested
3060 * Returns 0 if current has the requested access, error code otherwise
3062 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3066 may = smack_flags_to_may(semflg);
3067 return smk_curacc_sem(isp, may);
3071 * smack_sem_shmctl - Smack access check for sem
3073 * @cmd: what it wants to do
3075 * Returns 0 if current has the requested access, error code otherwise
3077 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3096 may = MAY_READWRITE;
3101 * System level information
3108 return smk_curacc_sem(isp, may);
3112 * smack_sem_semop - Smack checks of semaphore operations
3118 * Treated as read and write in all cases.
3120 * Returns 0 if access is allowed, error code otherwise
3122 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3123 unsigned nsops, int alter)
3125 return smk_curacc_sem(isp, MAY_READWRITE);
3129 * smk_curacc_msq : helper to check if current has access on msq
3131 * @access : access requested
3133 * return 0 if current has access, error otherwise
3135 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3137 struct smack_known *msp = smack_of_ipc(isp);
3138 struct smk_audit_info ad;
3142 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3143 ad.a.u.ipc_id = isp->id;
3145 rc = smk_curacc(msp, access, &ad);
3146 rc = smk_bu_current("msq", msp, access, rc);
3151 * smack_msg_queue_associate - Smack access check for msg_queue
3153 * @msqflg: access requested
3155 * Returns 0 if current has the requested access, error code otherwise
3157 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3161 may = smack_flags_to_may(msqflg);
3162 return smk_curacc_msq(isp, may);
3166 * smack_msg_queue_msgctl - Smack access check for msg_queue
3168 * @cmd: what it wants to do
3170 * Returns 0 if current has the requested access, error code otherwise
3172 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3184 may = MAY_READWRITE;
3189 * System level information
3196 return smk_curacc_msq(isp, may);
3200 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3203 * @msqflg: access requested
3205 * Returns 0 if current has the requested access, error code otherwise
3207 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3212 may = smack_flags_to_may(msqflg);
3213 return smk_curacc_msq(isp, may);
3217 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3224 * Returns 0 if current has read and write access, error code otherwise
3226 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3227 struct task_struct *target, long type, int mode)
3229 return smk_curacc_msq(isp, MAY_READWRITE);
3233 * smack_ipc_permission - Smack access for ipc_permission()
3234 * @ipp: the object permissions
3235 * @flag: access requested
3237 * Returns 0 if current has read and write access, error code otherwise
3239 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3241 struct smack_known **blob = smack_ipc(ipp);
3242 struct smack_known *iskp = *blob;
3243 int may = smack_flags_to_may(flag);
3244 struct smk_audit_info ad;
3248 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3249 ad.a.u.ipc_id = ipp->id;
3251 rc = smk_curacc(iskp, may, &ad);
3252 rc = smk_bu_current("svipc", iskp, may, rc);
3257 * smack_ipc_getsecid - Extract smack security id
3258 * @ipp: the object permissions
3259 * @secid: where result will be saved
3261 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3263 struct smack_known **blob = smack_ipc(ipp);
3264 struct smack_known *iskp = *blob;
3266 *secid = iskp->smk_secid;
3270 * smack_d_instantiate - Make sure the blob is correct on an inode
3271 * @opt_dentry: dentry where inode will be attached
3272 * @inode: the object
3274 * Set the inode's security blob if it hasn't been done already.
3276 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3278 struct super_block *sbp;
3279 struct superblock_smack *sbsp;
3280 struct inode_smack *isp;
3281 struct smack_known *skp;
3282 struct smack_known *ckp = smk_of_current();
3283 struct smack_known *final;
3284 char trattr[TRANS_TRUE_SIZE];
3292 isp = smack_inode(inode);
3295 * If the inode is already instantiated
3296 * take the quick way out
3298 if (isp->smk_flags & SMK_INODE_INSTANT)
3302 sbsp = sbp->s_security;
3304 * We're going to use the superblock default label
3305 * if there's no label on the file.
3307 final = sbsp->smk_default;
3310 * If this is the root inode the superblock
3311 * may be in the process of initialization.
3312 * If that is the case use the root value out
3313 * of the superblock.
3315 if (opt_dentry->d_parent == opt_dentry) {
3316 switch (sbp->s_magic) {
3317 case CGROUP_SUPER_MAGIC:
3318 case CGROUP2_SUPER_MAGIC:
3320 * The cgroup filesystem is never mounted,
3321 * so there's no opportunity to set the mount
3324 sbsp->smk_root = &smack_known_star;
3325 sbsp->smk_default = &smack_known_star;
3326 isp->smk_inode = sbsp->smk_root;
3330 * What about shmem/tmpfs anonymous files with dentry
3331 * obtained from d_alloc_pseudo()?
3333 isp->smk_inode = smk_of_current();
3336 isp->smk_inode = smk_of_current();
3340 * Socket access is controlled by the socket
3341 * structures associated with the task involved.
3343 isp->smk_inode = &smack_known_star;
3346 isp->smk_inode = sbsp->smk_root;
3349 isp->smk_flags |= SMK_INODE_INSTANT;
3354 * This is pretty hackish.
3355 * Casey says that we shouldn't have to do
3356 * file system specific code, but it does help
3357 * with keeping it simple.
3359 switch (sbp->s_magic) {
3361 case CGROUP_SUPER_MAGIC:
3362 case CGROUP2_SUPER_MAGIC:
3364 * Casey says that it's a little embarrassing
3365 * that the smack file system doesn't do
3366 * extended attributes.
3368 * Cgroupfs is special
3370 final = &smack_known_star;
3372 case DEVPTS_SUPER_MAGIC:
3374 * devpts seems content with the label of the task.
3375 * Programs that change smack have to treat the
3380 case PROC_SUPER_MAGIC:
3382 * Casey says procfs appears not to care.
3383 * The superblock default suffices.
3388 * Device labels should come from the filesystem,
3389 * but watch out, because they're volitile,
3390 * getting recreated on every reboot.
3392 final = &smack_known_star;
3394 * If a smack value has been set we want to use it,
3395 * but since tmpfs isn't giving us the opportunity
3396 * to set mount options simulate setting the
3397 * superblock default.
3402 * This isn't an understood special case.
3403 * Get the value from the xattr.
3407 * UNIX domain sockets use lower level socket data.
3409 if (S_ISSOCK(inode->i_mode)) {
3410 final = &smack_known_star;
3414 * No xattr support means, alas, no SMACK label.
3415 * Use the aforeapplied default.
3416 * It would be curious if the label of the task
3417 * does not match that assigned.
3419 if (!(inode->i_opflags & IOP_XATTR))
3422 * Get the dentry for xattr.
3424 dp = dget(opt_dentry);
3425 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3426 if (!IS_ERR_OR_NULL(skp))
3430 * Transmuting directory
3432 if (S_ISDIR(inode->i_mode)) {
3434 * If this is a new directory and the label was
3435 * transmuted when the inode was initialized
3436 * set the transmute attribute on the directory
3437 * and mark the inode.
3439 * If there is a transmute attribute on the
3440 * directory mark the inode.
3442 if (isp->smk_flags & SMK_INODE_CHANGED) {
3443 isp->smk_flags &= ~SMK_INODE_CHANGED;
3444 rc = __vfs_setxattr(&init_user_ns, dp, inode,
3445 XATTR_NAME_SMACKTRANSMUTE,
3446 TRANS_TRUE, TRANS_TRUE_SIZE,
3449 rc = __vfs_getxattr(dp, inode,
3450 XATTR_NAME_SMACKTRANSMUTE, trattr,
3452 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3453 TRANS_TRUE_SIZE) != 0)
3457 transflag = SMK_INODE_TRANSMUTE;
3460 * Don't let the exec or mmap label be "*" or "@".
3462 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3463 if (IS_ERR(skp) || skp == &smack_known_star ||
3464 skp == &smack_known_web)
3466 isp->smk_task = skp;
3468 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3469 if (IS_ERR(skp) || skp == &smack_known_star ||
3470 skp == &smack_known_web)
3472 isp->smk_mmap = skp;
3479 isp->smk_inode = ckp;
3481 isp->smk_inode = final;
3483 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3489 * smack_getprocattr - Smack process attribute access
3490 * @p: the object task
3491 * @name: the name of the attribute in /proc/.../attr
3492 * @value: where to put the result
3494 * Places a copy of the task Smack into value
3496 * Returns the length of the smack label or an error code
3498 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3500 struct smack_known *skp = smk_of_task_struct_subj(p);
3504 if (strcmp(name, "current") != 0)
3507 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3517 * smack_setprocattr - Smack process attribute setting
3518 * @name: the name of the attribute in /proc/.../attr
3519 * @value: the value to set
3520 * @size: the size of the value
3522 * Sets the Smack value of the task. Only setting self
3523 * is permitted and only with privilege
3525 * Returns the length of the smack label or an error code
3527 static int smack_setprocattr(const char *name, void *value, size_t size)
3529 struct task_smack *tsp = smack_cred(current_cred());
3531 struct smack_known *skp;
3532 struct smack_known_list_elem *sklep;
3535 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3538 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3541 if (strcmp(name, "current") != 0)
3544 skp = smk_import_entry(value, size);
3546 return PTR_ERR(skp);
3549 * No process is ever allowed the web ("@") label
3550 * and the star ("*") label.
3552 if (skp == &smack_known_web || skp == &smack_known_star)
3555 if (!smack_privileged(CAP_MAC_ADMIN)) {
3557 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3558 if (sklep->smk_label == skp) {
3566 new = prepare_creds();
3570 tsp = smack_cred(new);
3571 tsp->smk_task = skp;
3573 * process can change its label only once
3575 smk_destroy_label_list(&tsp->smk_relabel);
3582 * smack_unix_stream_connect - Smack access on UDS
3584 * @other: the other sock
3587 * Return 0 if a subject with the smack of sock could access
3588 * an object with the smack of other, otherwise an error code
3590 static int smack_unix_stream_connect(struct sock *sock,
3591 struct sock *other, struct sock *newsk)
3593 struct smack_known *skp;
3594 struct smack_known *okp;
3595 struct socket_smack *ssp = sock->sk_security;
3596 struct socket_smack *osp = other->sk_security;
3597 struct socket_smack *nsp = newsk->sk_security;
3598 struct smk_audit_info ad;
3601 struct lsm_network_audit net;
3604 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3608 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3609 smk_ad_setfield_u_net_sk(&ad, other);
3611 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3612 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3616 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3617 rc = smk_bu_note("UDS connect", okp, skp,
3623 * Cross reference the peer labels for SO_PEERSEC.
3626 nsp->smk_packet = ssp->smk_out;
3627 ssp->smk_packet = osp->smk_out;
3634 * smack_unix_may_send - Smack access on UDS
3636 * @other: the other socket
3638 * Return 0 if a subject with the smack of sock could access
3639 * an object with the smack of other, otherwise an error code
3641 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3643 struct socket_smack *ssp = sock->sk->sk_security;
3644 struct socket_smack *osp = other->sk->sk_security;
3645 struct smk_audit_info ad;
3649 struct lsm_network_audit net;
3651 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3652 smk_ad_setfield_u_net_sk(&ad, other->sk);
3655 if (smack_privileged(CAP_MAC_OVERRIDE))
3658 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3659 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3664 * smack_socket_sendmsg - Smack check based on destination host
3667 * @size: the size of the message
3669 * Return 0 if the current subject can write to the destination host.
3670 * For IPv4 this is only a question if the destination is a single label host.
3671 * For IPv6 this is a check against the label of the port.
3673 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3676 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3677 #if IS_ENABLED(CONFIG_IPV6)
3678 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3680 #ifdef SMACK_IPV6_SECMARK_LABELING
3681 struct socket_smack *ssp = sock->sk->sk_security;
3682 struct smack_known *rsp;
3687 * Perfectly reasonable for this to be NULL
3692 switch (sock->sk->sk_family) {
3694 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3695 sip->sin_family != AF_INET)
3697 rc = smk_ipv4_check(sock->sk, sip);
3699 #if IS_ENABLED(CONFIG_IPV6)
3701 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3702 sap->sin6_family != AF_INET6)
3704 #ifdef SMACK_IPV6_SECMARK_LABELING
3705 rsp = smack_ipv6host_label(sap);
3707 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3710 #ifdef SMACK_IPV6_PORT_LABELING
3711 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3713 #endif /* IS_ENABLED(CONFIG_IPV6) */
3720 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3721 * @sap: netlabel secattr
3722 * @ssp: socket security information
3724 * Returns a pointer to a Smack label entry found on the label list.
3726 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3727 struct socket_smack *ssp)
3729 struct smack_known *skp;
3735 * Netlabel found it in the cache.
3737 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3738 return (struct smack_known *)sap->cache->data;
3740 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3742 * Looks like a fallback, which gives us a secid.
3744 return smack_from_secid(sap->attr.secid);
3746 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3748 * Looks like a CIPSO packet.
3749 * If there are flags but no level netlabel isn't
3750 * behaving the way we expect it to.
3752 * Look it up in the label table
3753 * Without guidance regarding the smack value
3754 * for the packet fall back on the network
3758 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3759 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3762 * Compare the catsets. Use the netlbl APIs.
3764 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3765 if ((skp->smk_netlabel.flags &
3766 NETLBL_SECATTR_MLS_CAT) == 0)
3770 for (acat = -1, kcat = -1; acat == kcat; ) {
3771 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3773 kcat = netlbl_catmap_walk(
3774 skp->smk_netlabel.attr.mls.cat,
3776 if (acat < 0 || kcat < 0)
3789 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3790 return &smack_known_web;
3791 return &smack_known_star;
3794 * Without guidance regarding the smack value
3795 * for the packet fall back on the network
3798 return smack_net_ambient;
3801 #if IS_ENABLED(CONFIG_IPV6)
3802 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3806 int proto = -EINVAL;
3807 struct ipv6hdr _ipv6h;
3808 struct ipv6hdr *ip6;
3810 struct tcphdr _tcph, *th;
3811 struct udphdr _udph, *uh;
3812 struct dccp_hdr _dccph, *dh;
3816 offset = skb_network_offset(skb);
3817 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3820 sip->sin6_addr = ip6->saddr;
3822 nexthdr = ip6->nexthdr;
3823 offset += sizeof(_ipv6h);
3824 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3831 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3833 sip->sin6_port = th->source;
3836 case IPPROTO_UDPLITE:
3837 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3839 sip->sin6_port = uh->source;
3842 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3844 sip->sin6_port = dh->dccph_sport;
3849 #endif /* CONFIG_IPV6 */
3852 * smack_from_skb - Smack data from the secmark in an skb
3855 * Returns smack_known of the secmark or NULL if that won't work.
3857 #ifdef CONFIG_NETWORK_SECMARK
3858 static struct smack_known *smack_from_skb(struct sk_buff *skb)
3860 if (skb == NULL || skb->secmark == 0)
3863 return smack_from_secid(skb->secmark);
3866 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3873 * smack_from_netlbl - Smack data from the IP options in an skb
3874 * @sk: socket data came in on
3875 * @family: address family
3878 * Find the Smack label in the IP options. If it hasn't been
3879 * added to the netlabel cache, add it here.
3881 * Returns smack_known of the IP options or NULL if that won't work.
3883 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
3884 struct sk_buff *skb)
3886 struct netlbl_lsm_secattr secattr;
3887 struct socket_smack *ssp = NULL;
3888 struct smack_known *skp = NULL;
3890 netlbl_secattr_init(&secattr);
3893 ssp = sk->sk_security;
3895 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
3896 skp = smack_from_secattr(&secattr, ssp);
3897 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
3898 netlbl_cache_add(skb, family, &skp->smk_netlabel);
3901 netlbl_secattr_destroy(&secattr);
3907 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3911 * Returns 0 if the packet should be delivered, an error code otherwise
3913 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3915 struct socket_smack *ssp = sk->sk_security;
3916 struct smack_known *skp = NULL;
3918 struct smk_audit_info ad;
3919 u16 family = sk->sk_family;
3921 struct lsm_network_audit net;
3923 #if IS_ENABLED(CONFIG_IPV6)
3924 struct sockaddr_in6 sadd;
3927 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3929 #endif /* CONFIG_IPV6 */
3934 * If there is a secmark use it rather than the CIPSO label.
3935 * If there is no secmark fall back to CIPSO.
3936 * The secmark is assumed to reflect policy better.
3938 skp = smack_from_skb(skb);
3940 skp = smack_from_netlbl(sk, family, skb);
3942 skp = smack_net_ambient;
3946 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3947 ad.a.u.net->family = family;
3948 ad.a.u.net->netif = skb->skb_iif;
3949 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3952 * Receiving a packet requires that the other end
3953 * be able to write here. Read access is not required.
3954 * This is the simplist possible security model
3957 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3958 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3961 netlbl_skbuff_err(skb, family, rc, 0);
3963 #if IS_ENABLED(CONFIG_IPV6)
3965 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3966 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3967 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3969 #ifdef SMACK_IPV6_SECMARK_LABELING
3970 skp = smack_from_skb(skb);
3972 if (smk_ipv6_localhost(&sadd))
3974 skp = smack_ipv6host_label(&sadd);
3976 skp = smack_net_ambient;
3979 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3980 ad.a.u.net->family = family;
3981 ad.a.u.net->netif = skb->skb_iif;
3982 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3983 #endif /* CONFIG_AUDIT */
3984 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3985 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3987 #endif /* SMACK_IPV6_SECMARK_LABELING */
3988 #ifdef SMACK_IPV6_PORT_LABELING
3989 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3990 #endif /* SMACK_IPV6_PORT_LABELING */
3992 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3993 ICMPV6_ADM_PROHIBITED, 0);
3995 #endif /* CONFIG_IPV6 */
4002 * smack_socket_getpeersec_stream - pull in packet label
4004 * @optval: user's destination
4005 * @optlen: size thereof
4008 * returns zero on success, an error code otherwise
4010 static int smack_socket_getpeersec_stream(struct socket *sock,
4011 char __user *optval,
4012 int __user *optlen, unsigned len)
4014 struct socket_smack *ssp;
4019 ssp = sock->sk->sk_security;
4020 if (ssp->smk_packet != NULL) {
4021 rcp = ssp->smk_packet->smk_known;
4022 slen = strlen(rcp) + 1;
4027 else if (copy_to_user(optval, rcp, slen) != 0)
4030 if (put_user(slen, optlen) != 0)
4038 * smack_socket_getpeersec_dgram - pull in packet label
4039 * @sock: the peer socket
4041 * @secid: pointer to where to put the secid of the packet
4043 * Sets the netlabel socket state on sk from parent
4045 static int smack_socket_getpeersec_dgram(struct socket *sock,
4046 struct sk_buff *skb, u32 *secid)
4049 struct socket_smack *ssp = NULL;
4050 struct smack_known *skp;
4051 struct sock *sk = NULL;
4052 int family = PF_UNSPEC;
4053 u32 s = 0; /* 0 is the invalid secid */
4056 if (skb->protocol == htons(ETH_P_IP))
4058 #if IS_ENABLED(CONFIG_IPV6)
4059 else if (skb->protocol == htons(ETH_P_IPV6))
4061 #endif /* CONFIG_IPV6 */
4063 if (family == PF_UNSPEC && sock != NULL)
4064 family = sock->sk->sk_family;
4068 ssp = sock->sk->sk_security;
4069 s = ssp->smk_out->smk_secid;
4072 skp = smack_from_skb(skb);
4078 * Translate what netlabel gave us.
4082 skp = smack_from_netlbl(sk, family, skb);
4087 #ifdef SMACK_IPV6_SECMARK_LABELING
4088 skp = smack_from_skb(skb);
4101 * smack_sock_graft - Initialize a newly created socket with an existing sock
4103 * @parent: parent socket
4105 * Set the smk_{in,out} state of an existing sock based on the process that
4106 * is creating the new socket.
4108 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4110 struct socket_smack *ssp;
4111 struct smack_known *skp = smk_of_current();
4114 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4117 ssp = sk->sk_security;
4120 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4124 * smack_inet_conn_request - Smack access check on connect
4125 * @sk: socket involved
4129 * Returns 0 if a task with the packet label could write to
4130 * the socket, otherwise an error code
4132 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4133 struct request_sock *req)
4135 u16 family = sk->sk_family;
4136 struct smack_known *skp;
4137 struct socket_smack *ssp = sk->sk_security;
4138 struct sockaddr_in addr;
4140 struct smack_known *hskp;
4142 struct smk_audit_info ad;
4144 struct lsm_network_audit net;
4147 #if IS_ENABLED(CONFIG_IPV6)
4148 if (family == PF_INET6) {
4150 * Handle mapped IPv4 packets arriving
4151 * via IPv6 sockets. Don't set up netlabel
4152 * processing on IPv6.
4154 if (skb->protocol == htons(ETH_P_IP))
4159 #endif /* CONFIG_IPV6 */
4162 * If there is a secmark use it rather than the CIPSO label.
4163 * If there is no secmark fall back to CIPSO.
4164 * The secmark is assumed to reflect policy better.
4166 skp = smack_from_skb(skb);
4168 skp = smack_from_netlbl(sk, family, skb);
4170 skp = &smack_known_huh;
4174 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4175 ad.a.u.net->family = family;
4176 ad.a.u.net->netif = skb->skb_iif;
4177 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4180 * Receiving a packet requires that the other end be able to write
4181 * here. Read access is not required.
4183 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4184 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4189 * Save the peer's label in the request_sock so we can later setup
4190 * smk_packet in the child socket so that SO_PEERCRED can report it.
4192 req->peer_secid = skp->smk_secid;
4195 * We need to decide if we want to label the incoming connection here
4196 * if we do we only need to label the request_sock and the stack will
4197 * propagate the wire-label to the sock when it is created.
4200 addr.sin_addr.s_addr = hdr->saddr;
4202 hskp = smack_ipv4host_label(&addr);
4206 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4208 netlbl_req_delattr(req);
4214 * smack_inet_csk_clone - Copy the connection information to the new socket
4215 * @sk: the new socket
4216 * @req: the connection's request_sock
4218 * Transfer the connection's peer label to the newly created socket.
4220 static void smack_inet_csk_clone(struct sock *sk,
4221 const struct request_sock *req)
4223 struct socket_smack *ssp = sk->sk_security;
4224 struct smack_known *skp;
4226 if (req->peer_secid != 0) {
4227 skp = smack_from_secid(req->peer_secid);
4228 ssp->smk_packet = skp;
4230 ssp->smk_packet = NULL;
4234 * Key management security hooks
4236 * Casey has not tested key support very heavily.
4237 * The permission check is most likely too restrictive.
4238 * If you care about keys please have a look.
4243 * smack_key_alloc - Set the key security blob
4245 * @cred: the credentials to use
4248 * No allocation required
4252 static int smack_key_alloc(struct key *key, const struct cred *cred,
4253 unsigned long flags)
4255 struct smack_known *skp = smk_of_task(smack_cred(cred));
4257 key->security = skp;
4262 * smack_key_free - Clear the key security blob
4265 * Clear the blob pointer
4267 static void smack_key_free(struct key *key)
4269 key->security = NULL;
4273 * smack_key_permission - Smack access on a key
4274 * @key_ref: gets to the object
4275 * @cred: the credentials to use
4276 * @need_perm: requested key permission
4278 * Return 0 if the task has read and write to the object,
4279 * an error code otherwise
4281 static int smack_key_permission(key_ref_t key_ref,
4282 const struct cred *cred,
4283 enum key_need_perm need_perm)
4286 struct smk_audit_info ad;
4287 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4292 * Validate requested permissions
4294 switch (need_perm) {
4296 case KEY_NEED_SEARCH:
4298 request |= MAY_READ;
4300 case KEY_NEED_WRITE:
4302 case KEY_NEED_SETATTR:
4303 request |= MAY_WRITE;
4305 case KEY_NEED_UNSPECIFIED:
4306 case KEY_NEED_UNLINK:
4307 case KEY_SYSADMIN_OVERRIDE:
4308 case KEY_AUTHTOKEN_OVERRIDE:
4309 case KEY_DEFER_PERM_CHECK:
4315 keyp = key_ref_to_ptr(key_ref);
4319 * If the key hasn't been initialized give it access so that
4322 if (keyp->security == NULL)
4325 * This should not occur
4330 if (smack_privileged(CAP_MAC_OVERRIDE))
4334 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4335 ad.a.u.key_struct.key = keyp->serial;
4336 ad.a.u.key_struct.key_desc = keyp->description;
4338 rc = smk_access(tkp, keyp->security, request, &ad);
4339 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4344 * smack_key_getsecurity - Smack label tagging the key
4345 * @key points to the key to be queried
4346 * @_buffer points to a pointer that should be set to point to the
4347 * resulting string (if no label or an error occurs).
4348 * Return the length of the string (including terminating NUL) or -ve if
4350 * May also return 0 (and a NULL buffer pointer) if there is no label.
4352 static int smack_key_getsecurity(struct key *key, char **_buffer)
4354 struct smack_known *skp = key->security;
4358 if (key->security == NULL) {
4363 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4366 length = strlen(copy) + 1;
4373 #ifdef CONFIG_KEY_NOTIFICATIONS
4375 * smack_watch_key - Smack access to watch a key for notifications.
4376 * @key: The key to be watched
4378 * Return 0 if the @watch->cred has permission to read from the key object and
4379 * an error otherwise.
4381 static int smack_watch_key(struct key *key)
4383 struct smk_audit_info ad;
4384 struct smack_known *tkp = smk_of_current();
4390 * If the key hasn't been initialized give it access so that
4393 if (key->security == NULL)
4396 * This should not occur
4401 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4405 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4406 ad.a.u.key_struct.key = key->serial;
4407 ad.a.u.key_struct.key_desc = key->description;
4409 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4410 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4413 #endif /* CONFIG_KEY_NOTIFICATIONS */
4414 #endif /* CONFIG_KEYS */
4416 #ifdef CONFIG_WATCH_QUEUE
4418 * smack_post_notification - Smack access to post a notification to a queue
4419 * @w_cred: The credentials of the watcher.
4420 * @cred: The credentials of the event source (may be NULL).
4421 * @n: The notification message to be posted.
4423 static int smack_post_notification(const struct cred *w_cred,
4424 const struct cred *cred,
4425 struct watch_notification *n)
4427 struct smk_audit_info ad;
4428 struct smack_known *subj, *obj;
4431 /* Always let maintenance notifications through. */
4432 if (n->type == WATCH_TYPE_META)
4437 subj = smk_of_task(smack_cred(cred));
4438 obj = smk_of_task(smack_cred(w_cred));
4440 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4441 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4442 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4445 #endif /* CONFIG_WATCH_QUEUE */
4450 * Audit requires a unique representation of each Smack specific
4451 * rule. This unique representation is used to distinguish the
4452 * object to be audited from remaining kernel objects and also
4453 * works as a glue between the audit hooks.
4455 * Since repository entries are added but never deleted, we'll use
4456 * the smack_known label address related to the given audit rule as
4457 * the needed unique representation. This also better fits the smack
4458 * model where nearly everything is a label.
4463 * smack_audit_rule_init - Initialize a smack audit rule
4464 * @field: audit rule fields given from user-space (audit.h)
4465 * @op: required testing operator (=, !=, >, <, ...)
4466 * @rulestr: smack label to be audited
4467 * @vrule: pointer to save our own audit rule representation
4469 * Prepare to audit cases where (@field @op @rulestr) is true.
4470 * The label to be audited is created if necessay.
4472 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4474 struct smack_known *skp;
4475 char **rule = (char **)vrule;
4478 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4481 if (op != Audit_equal && op != Audit_not_equal)
4484 skp = smk_import_entry(rulestr, 0);
4486 return PTR_ERR(skp);
4488 *rule = skp->smk_known;
4494 * smack_audit_rule_known - Distinguish Smack audit rules
4495 * @krule: rule of interest, in Audit kernel representation format
4497 * This is used to filter Smack rules from remaining Audit ones.
4498 * If it's proved that this rule belongs to us, the
4499 * audit_rule_match hook will be called to do the final judgement.
4501 static int smack_audit_rule_known(struct audit_krule *krule)
4503 struct audit_field *f;
4506 for (i = 0; i < krule->field_count; i++) {
4507 f = &krule->fields[i];
4509 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4517 * smack_audit_rule_match - Audit given object ?
4518 * @secid: security id for identifying the object to test
4519 * @field: audit rule flags given from user-space
4520 * @op: required testing operator
4521 * @vrule: smack internal rule presentation
4523 * The core Audit hook. It's used to take the decision of
4524 * whether to audit or not to audit a given object.
4526 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4528 struct smack_known *skp;
4531 if (unlikely(!rule)) {
4532 WARN_ONCE(1, "Smack: missing rule\n");
4536 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4539 skp = smack_from_secid(secid);
4542 * No need to do string comparisons. If a match occurs,
4543 * both pointers will point to the same smack_known
4546 if (op == Audit_equal)
4547 return (rule == skp->smk_known);
4548 if (op == Audit_not_equal)
4549 return (rule != skp->smk_known);
4555 * There is no need for a smack_audit_rule_free hook.
4556 * No memory was allocated.
4559 #endif /* CONFIG_AUDIT */
4562 * smack_ismaclabel - check if xattr @name references a smack MAC label
4563 * @name: Full xattr name to check.
4565 static int smack_ismaclabel(const char *name)
4567 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4572 * smack_secid_to_secctx - return the smack label for a secid
4573 * @secid: incoming integer
4574 * @secdata: destination
4575 * @seclen: how long it is
4577 * Exists for networking code.
4579 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4581 struct smack_known *skp = smack_from_secid(secid);
4584 *secdata = skp->smk_known;
4585 *seclen = strlen(skp->smk_known);
4590 * smack_secctx_to_secid - return the secid for a smack label
4591 * @secdata: smack label
4592 * @seclen: how long result is
4593 * @secid: outgoing integer
4595 * Exists for audit and networking code.
4597 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4599 struct smack_known *skp = smk_find_entry(secdata);
4602 *secid = skp->smk_secid;
4609 * There used to be a smack_release_secctx hook
4610 * that did nothing back when hooks were in a vector.
4611 * Now that there's a list such a hook adds cost.
4614 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4616 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4620 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4622 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK,
4626 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4628 struct smack_known *skp = smk_of_inode(inode);
4630 *ctx = skp->smk_known;
4631 *ctxlen = strlen(skp->smk_known);
4635 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4638 struct task_smack *tsp;
4639 struct smack_known *skp;
4640 struct inode_smack *isp;
4641 struct cred *new_creds = *new;
4643 if (new_creds == NULL) {
4644 new_creds = prepare_creds();
4645 if (new_creds == NULL)
4649 tsp = smack_cred(new_creds);
4652 * Get label from overlay inode and set it in create_sid
4654 isp = smack_inode(d_inode(dentry->d_parent));
4655 skp = isp->smk_inode;
4656 tsp->smk_task = skp;
4661 static int smack_inode_copy_up_xattr(const char *name)
4664 * Return 1 if this is the smack access Smack attribute.
4666 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4672 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4674 const struct cred *old,
4677 struct task_smack *otsp = smack_cred(old);
4678 struct task_smack *ntsp = smack_cred(new);
4679 struct inode_smack *isp;
4683 * Use the process credential unless all of
4684 * the transmuting criteria are met
4686 ntsp->smk_task = otsp->smk_task;
4689 * the attribute of the containing directory
4691 isp = smack_inode(d_inode(dentry->d_parent));
4693 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4695 may = smk_access_entry(otsp->smk_task->smk_known,
4696 isp->smk_inode->smk_known,
4697 &otsp->smk_task->smk_rules);
4701 * If the directory is transmuting and the rule
4702 * providing access is transmuting use the containing
4703 * directory label instead of the process label.
4705 if (may > 0 && (may & MAY_TRANSMUTE))
4706 ntsp->smk_task = isp->smk_inode;
4711 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4712 .lbs_cred = sizeof(struct task_smack),
4713 .lbs_file = sizeof(struct smack_known *),
4714 .lbs_inode = sizeof(struct inode_smack),
4715 .lbs_ipc = sizeof(struct smack_known *),
4716 .lbs_msg_msg = sizeof(struct smack_known *),
4719 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4720 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4721 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4722 LSM_HOOK_INIT(syslog, smack_syslog),
4724 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4725 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4727 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4728 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4729 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4730 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4731 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4732 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4734 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
4736 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4737 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4738 LSM_HOOK_INIT(inode_link, smack_inode_link),
4739 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4740 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4741 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4742 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4743 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4744 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4745 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4746 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4747 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4748 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4749 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4750 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4751 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4752 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4754 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4755 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4756 LSM_HOOK_INIT(file_lock, smack_file_lock),
4757 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4758 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4759 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4760 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4761 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4762 LSM_HOOK_INIT(file_receive, smack_file_receive),
4764 LSM_HOOK_INIT(file_open, smack_file_open),
4766 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4767 LSM_HOOK_INIT(cred_free, smack_cred_free),
4768 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4769 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4770 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4771 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4772 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4773 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4774 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4775 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4776 LSM_HOOK_INIT(task_getsecid_subj, smack_task_getsecid_subj),
4777 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
4778 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4779 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4780 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4781 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4782 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4783 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4784 LSM_HOOK_INIT(task_kill, smack_task_kill),
4785 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4787 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4788 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4790 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4792 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4793 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4794 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4795 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4796 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4798 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4799 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4800 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4801 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4803 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4804 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4805 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4806 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4808 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4810 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4811 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4813 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4814 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4816 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4817 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4818 #ifdef SMACK_IPV6_PORT_LABELING
4819 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4821 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4822 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4823 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4824 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4825 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4826 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4827 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4828 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4829 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4830 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4832 /* key management security hooks */
4834 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4835 LSM_HOOK_INIT(key_free, smack_key_free),
4836 LSM_HOOK_INIT(key_permission, smack_key_permission),
4837 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4838 #ifdef CONFIG_KEY_NOTIFICATIONS
4839 LSM_HOOK_INIT(watch_key, smack_watch_key),
4841 #endif /* CONFIG_KEYS */
4843 #ifdef CONFIG_WATCH_QUEUE
4844 LSM_HOOK_INIT(post_notification, smack_post_notification),
4849 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4850 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4851 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4852 #endif /* CONFIG_AUDIT */
4854 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4855 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4856 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4857 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4858 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4859 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4860 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4861 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4862 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4866 static __init void init_smack_known_list(void)
4869 * Initialize rule list locks
4871 mutex_init(&smack_known_huh.smk_rules_lock);
4872 mutex_init(&smack_known_hat.smk_rules_lock);
4873 mutex_init(&smack_known_floor.smk_rules_lock);
4874 mutex_init(&smack_known_star.smk_rules_lock);
4875 mutex_init(&smack_known_web.smk_rules_lock);
4877 * Initialize rule lists
4879 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4880 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4881 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4882 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4883 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4885 * Create the known labels list
4887 smk_insert_entry(&smack_known_huh);
4888 smk_insert_entry(&smack_known_hat);
4889 smk_insert_entry(&smack_known_star);
4890 smk_insert_entry(&smack_known_floor);
4891 smk_insert_entry(&smack_known_web);
4895 * smack_init - initialize the smack system
4897 * Returns 0 on success, -ENOMEM is there's no memory
4899 static __init int smack_init(void)
4901 struct cred *cred = (struct cred *) current->cred;
4902 struct task_smack *tsp;
4904 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4905 if (!smack_rule_cache)
4909 * Set the security state for the initial task.
4911 tsp = smack_cred(cred);
4912 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4917 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4920 pr_info("Smack: Initializing.\n");
4921 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4922 pr_info("Smack: Netfilter enabled.\n");
4924 #ifdef SMACK_IPV6_PORT_LABELING
4925 pr_info("Smack: IPv6 port labeling enabled.\n");
4927 #ifdef SMACK_IPV6_SECMARK_LABELING
4928 pr_info("Smack: IPv6 Netfilter enabled.\n");
4931 /* initialize the smack_known_list */
4932 init_smack_known_list();
4938 * Smack requires early initialization in order to label
4939 * all processes and objects when they are created.
4941 DEFINE_LSM(smack) = {
4943 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4944 .blobs = &smack_blob_sizes,