1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
8 * Casey Schaufler <casey@schaufler-ca.com>
9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
13 * Paul Moore <paul@paul-moore.com>
14 * Copyright (C) 2010 Nokia Corporation
15 * Copyright (C) 2011 Intel Corporation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
23 #include <asm/ioctls.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/dccp.h>
28 #include <linux/icmpv6.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
38 #include <linux/msg.h>
39 #include <linux/shm.h>
40 #include <linux/binfmts.h>
41 #include <linux/parser.h>
42 #include <linux/fs_context.h>
43 #include <linux/fs_parser.h>
44 #include <linux/watch_queue.h>
47 #define TRANS_TRUE "TRUE"
48 #define TRANS_TRUE_SIZE 4
50 #define SMK_CONNECTING 0
51 #define SMK_RECEIVING 1
54 #ifdef SMACK_IPV6_PORT_LABELING
55 static DEFINE_MUTEX(smack_ipv6_lock);
56 static LIST_HEAD(smk_ipv6_port_list);
58 struct kmem_cache *smack_rule_cache;
59 int smack_enabled __initdata;
61 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
66 } smk_mount_opts[] = {
67 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
68 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
72 static int match_opt_prefix(char *s, int l, char **arg)
76 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
77 size_t len = smk_mount_opts[i].len;
78 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
80 if (len == l || s[len] != '=')
83 return smk_mount_opts[i].opt;
88 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
89 static char *smk_bu_mess[] = {
90 "Bringup Error", /* Unused */
91 "Bringup", /* SMACK_BRINGUP_ALLOW */
92 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
93 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
96 static void smk_bu_mode(int mode, char *s)
102 if (mode & MAY_WRITE)
106 if (mode & MAY_APPEND)
108 if (mode & MAY_TRANSMUTE)
118 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
119 static int smk_bu_note(char *note, struct smack_known *sskp,
120 struct smack_known *oskp, int mode, int rc)
122 char acc[SMK_NUM_ACCESS_TYPE + 1];
126 if (rc > SMACK_UNCONFINED_OBJECT)
129 smk_bu_mode(mode, acc);
130 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
131 sskp->smk_known, oskp->smk_known, acc, note);
135 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
138 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
139 static int smk_bu_current(char *note, struct smack_known *oskp,
142 struct task_smack *tsp = smack_cred(current_cred());
143 char acc[SMK_NUM_ACCESS_TYPE + 1];
147 if (rc > SMACK_UNCONFINED_OBJECT)
150 smk_bu_mode(mode, acc);
151 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
152 tsp->smk_task->smk_known, oskp->smk_known,
153 acc, current->comm, note);
157 #define smk_bu_current(note, oskp, mode, RC) (RC)
160 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
161 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
163 struct task_smack *tsp = smack_cred(current_cred());
164 struct smack_known *smk_task = smk_of_task_struct_obj(otp);
165 char acc[SMK_NUM_ACCESS_TYPE + 1];
169 if (rc > SMACK_UNCONFINED_OBJECT)
172 smk_bu_mode(mode, acc);
173 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
174 tsp->smk_task->smk_known, smk_task->smk_known, acc,
175 current->comm, otp->comm);
179 #define smk_bu_task(otp, mode, RC) (RC)
182 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
183 static int smk_bu_inode(struct inode *inode, int mode, int rc)
185 struct task_smack *tsp = smack_cred(current_cred());
186 struct inode_smack *isp = smack_inode(inode);
187 char acc[SMK_NUM_ACCESS_TYPE + 1];
189 if (isp->smk_flags & SMK_INODE_IMPURE)
190 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
191 inode->i_sb->s_id, inode->i_ino, current->comm);
195 if (rc > SMACK_UNCONFINED_OBJECT)
197 if (rc == SMACK_UNCONFINED_SUBJECT &&
198 (mode & (MAY_WRITE | MAY_APPEND)))
199 isp->smk_flags |= SMK_INODE_IMPURE;
201 smk_bu_mode(mode, acc);
203 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
204 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
205 inode->i_sb->s_id, inode->i_ino, current->comm);
209 #define smk_bu_inode(inode, mode, RC) (RC)
212 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
213 static int smk_bu_file(struct file *file, int mode, int rc)
215 struct task_smack *tsp = smack_cred(current_cred());
216 struct smack_known *sskp = tsp->smk_task;
217 struct inode *inode = file_inode(file);
218 struct inode_smack *isp = smack_inode(inode);
219 char acc[SMK_NUM_ACCESS_TYPE + 1];
221 if (isp->smk_flags & SMK_INODE_IMPURE)
222 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
223 inode->i_sb->s_id, inode->i_ino, current->comm);
227 if (rc > SMACK_UNCONFINED_OBJECT)
230 smk_bu_mode(mode, acc);
231 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
232 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
233 inode->i_sb->s_id, inode->i_ino, file,
238 #define smk_bu_file(file, mode, RC) (RC)
241 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
242 static int smk_bu_credfile(const struct cred *cred, struct file *file,
245 struct task_smack *tsp = smack_cred(cred);
246 struct smack_known *sskp = tsp->smk_task;
247 struct inode *inode = file_inode(file);
248 struct inode_smack *isp = smack_inode(inode);
249 char acc[SMK_NUM_ACCESS_TYPE + 1];
251 if (isp->smk_flags & SMK_INODE_IMPURE)
252 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
253 inode->i_sb->s_id, inode->i_ino, current->comm);
257 if (rc > SMACK_UNCONFINED_OBJECT)
260 smk_bu_mode(mode, acc);
261 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
262 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
263 inode->i_sb->s_id, inode->i_ino, file,
268 #define smk_bu_credfile(cred, file, mode, RC) (RC)
272 * smk_fetch - Fetch the smack label from a file.
273 * @name: type of the label (attribute)
274 * @ip: a pointer to the inode
275 * @dp: a pointer to the dentry
277 * Returns a pointer to the master list entry for the Smack label,
278 * NULL if there was no label to fetch, or an error code.
280 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
285 struct smack_known *skp = NULL;
287 if (!(ip->i_opflags & IOP_XATTR))
288 return ERR_PTR(-EOPNOTSUPP);
290 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
292 return ERR_PTR(-ENOMEM);
294 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
300 skp = smk_import_entry(buffer, rc);
308 * init_inode_smack - initialize an inode security blob
309 * @inode: inode to extract the info from
310 * @skp: a pointer to the Smack label entry to use in the blob
313 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
315 struct inode_smack *isp = smack_inode(inode);
317 isp->smk_inode = skp;
322 * init_task_smack - initialize a task security blob
323 * @tsp: blob to initialize
324 * @task: a pointer to the Smack label for the running task
325 * @forked: a pointer to the Smack label for the forked task
328 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
329 struct smack_known *forked)
331 tsp->smk_task = task;
332 tsp->smk_forked = forked;
333 INIT_LIST_HEAD(&tsp->smk_rules);
334 INIT_LIST_HEAD(&tsp->smk_relabel);
335 mutex_init(&tsp->smk_rules_lock);
339 * smk_copy_rules - copy a rule set
340 * @nhead: new rules header pointer
341 * @ohead: old rules header pointer
342 * @gfp: type of the memory for the allocation
344 * Returns 0 on success, -ENOMEM on error
346 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
349 struct smack_rule *nrp;
350 struct smack_rule *orp;
353 list_for_each_entry_rcu(orp, ohead, list) {
354 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
360 list_add_rcu(&nrp->list, nhead);
366 * smk_copy_relabel - copy smk_relabel labels list
367 * @nhead: new rules header pointer
368 * @ohead: old rules header pointer
369 * @gfp: type of the memory for the allocation
371 * Returns 0 on success, -ENOMEM on error
373 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
376 struct smack_known_list_elem *nklep;
377 struct smack_known_list_elem *oklep;
379 list_for_each_entry(oklep, ohead, list) {
380 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
382 smk_destroy_label_list(nhead);
385 nklep->smk_label = oklep->smk_label;
386 list_add(&nklep->list, nhead);
393 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
394 * @mode - input mode in form of PTRACE_MODE_*
396 * Returns a converted MAY_* mode usable by smack rules
398 static inline unsigned int smk_ptrace_mode(unsigned int mode)
400 if (mode & PTRACE_MODE_ATTACH)
401 return MAY_READWRITE;
402 if (mode & PTRACE_MODE_READ)
409 * smk_ptrace_rule_check - helper for ptrace access
410 * @tracer: tracer process
411 * @tracee_known: label entry of the process that's about to be traced
412 * @mode: ptrace attachment mode (PTRACE_MODE_*)
413 * @func: name of the function that called us, used for audit
415 * Returns 0 on access granted, -error on error
417 static int smk_ptrace_rule_check(struct task_struct *tracer,
418 struct smack_known *tracee_known,
419 unsigned int mode, const char *func)
422 struct smk_audit_info ad, *saip = NULL;
423 struct task_smack *tsp;
424 struct smack_known *tracer_known;
425 const struct cred *tracercred;
427 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
428 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
429 smk_ad_setfield_u_tsk(&ad, tracer);
434 tracercred = __task_cred(tracer);
435 tsp = smack_cred(tracercred);
436 tracer_known = smk_of_task(tsp);
438 if ((mode & PTRACE_MODE_ATTACH) &&
439 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
440 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
441 if (tracer_known->smk_known == tracee_known->smk_known)
443 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
445 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
451 smack_log(tracer_known->smk_known,
452 tracee_known->smk_known,
459 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
460 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
468 * We he, that is fun!
472 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
473 * @ctp: child task pointer
474 * @mode: ptrace attachment mode (PTRACE_MODE_*)
476 * Returns 0 if access is OK, an error code otherwise
478 * Do the capability checks.
480 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
482 struct smack_known *skp;
484 skp = smk_of_task_struct_obj(ctp);
486 return smk_ptrace_rule_check(current, skp, mode, __func__);
490 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
491 * @ptp: parent task pointer
493 * Returns 0 if access is OK, an error code otherwise
495 * Do the capability checks, and require PTRACE_MODE_ATTACH.
497 static int smack_ptrace_traceme(struct task_struct *ptp)
500 struct smack_known *skp;
502 skp = smk_of_task(smack_cred(current_cred()));
504 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
509 * smack_syslog - Smack approval on syslog
510 * @typefrom_file: unused
512 * Returns 0 on success, error code otherwise.
514 static int smack_syslog(int typefrom_file)
517 struct smack_known *skp = smk_of_current();
519 if (smack_privileged(CAP_MAC_OVERRIDE))
522 if (smack_syslog_label != NULL && smack_syslog_label != skp)
533 * smack_sb_alloc_security - allocate a superblock blob
534 * @sb: the superblock getting the blob
536 * Returns 0 on success or -ENOMEM on error.
538 static int smack_sb_alloc_security(struct super_block *sb)
540 struct superblock_smack *sbsp = smack_superblock(sb);
542 sbsp->smk_root = &smack_known_floor;
543 sbsp->smk_default = &smack_known_floor;
544 sbsp->smk_floor = &smack_known_floor;
545 sbsp->smk_hat = &smack_known_hat;
547 * SMK_SB_INITIALIZED will be zero from kzalloc.
553 struct smack_mnt_opts {
554 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
557 static void smack_free_mnt_opts(void *mnt_opts)
559 struct smack_mnt_opts *opts = mnt_opts;
560 kfree(opts->fsdefault);
561 kfree(opts->fsfloor);
564 kfree(opts->fstransmute);
568 static int smack_add_opt(int token, const char *s, void **mnt_opts)
570 struct smack_mnt_opts *opts = *mnt_opts;
573 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
602 case Opt_fstransmute:
603 if (opts->fstransmute)
605 opts->fstransmute = s;
611 pr_warn("Smack: duplicate mount options\n");
616 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
617 * @fc: The new filesystem context.
618 * @src_fc: The source filesystem context being duplicated.
620 * Returns 0 on success or -ENOMEM on error.
622 static int smack_fs_context_dup(struct fs_context *fc,
623 struct fs_context *src_fc)
625 struct smack_mnt_opts *dst, *src = src_fc->security;
630 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
635 if (src->fsdefault) {
636 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
641 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
646 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
651 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
655 if (src->fstransmute) {
656 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
657 if (!dst->fstransmute)
663 static const struct fs_parameter_spec smack_fs_parameters[] = {
664 fsparam_string("smackfsdef", Opt_fsdefault),
665 fsparam_string("smackfsdefault", Opt_fsdefault),
666 fsparam_string("smackfsfloor", Opt_fsfloor),
667 fsparam_string("smackfshat", Opt_fshat),
668 fsparam_string("smackfsroot", Opt_fsroot),
669 fsparam_string("smackfstransmute", Opt_fstransmute),
674 * smack_fs_context_parse_param - Parse a single mount parameter
675 * @fc: The new filesystem context being constructed.
676 * @param: The parameter.
678 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
681 static int smack_fs_context_parse_param(struct fs_context *fc,
682 struct fs_parameter *param)
684 struct fs_parse_result result;
687 opt = fs_parse(fc, smack_fs_parameters, param, &result);
691 rc = smack_add_opt(opt, param->string, &fc->security);
693 param->string = NULL;
697 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
699 char *from = options, *to = options;
703 char *next = strchr(from, ',');
712 token = match_opt_prefix(from, len, &arg);
713 if (token != Opt_error) {
714 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
715 rc = smack_add_opt(token, arg, mnt_opts);
719 smack_free_mnt_opts(*mnt_opts);
724 if (!first) { // copy with preceding comma
729 memmove(to, from, len);
742 * smack_set_mnt_opts - set Smack specific mount options
743 * @sb: the file system superblock
744 * @mnt_opts: Smack mount options
745 * @kern_flags: mount option from kernel space or user space
746 * @set_kern_flags: where to store converted mount opts
748 * Returns 0 on success, an error code on failure
750 * Allow filesystems with binary mount data to explicitly set Smack mount
753 static int smack_set_mnt_opts(struct super_block *sb,
755 unsigned long kern_flags,
756 unsigned long *set_kern_flags)
758 struct dentry *root = sb->s_root;
759 struct inode *inode = d_backing_inode(root);
760 struct superblock_smack *sp = smack_superblock(sb);
761 struct inode_smack *isp;
762 struct smack_known *skp;
763 struct smack_mnt_opts *opts = mnt_opts;
764 bool transmute = false;
766 if (sp->smk_flags & SMK_SB_INITIALIZED)
769 if (inode->i_security == NULL) {
770 int rc = lsm_inode_alloc(inode);
776 if (!smack_privileged(CAP_MAC_ADMIN)) {
778 * Unprivileged mounts don't get to specify Smack values.
783 * Unprivileged mounts get root and default from the caller.
785 skp = smk_of_current();
787 sp->smk_default = skp;
789 * For a handful of fs types with no user-controlled
790 * backing store it's okay to trust security labels
791 * in the filesystem. The rest are untrusted.
793 if (sb->s_user_ns != &init_user_ns &&
794 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
795 sb->s_magic != RAMFS_MAGIC) {
797 sp->smk_flags |= SMK_SB_UNTRUSTED;
801 sp->smk_flags |= SMK_SB_INITIALIZED;
804 if (opts->fsdefault) {
805 skp = smk_import_entry(opts->fsdefault, 0);
808 sp->smk_default = skp;
811 skp = smk_import_entry(opts->fsfloor, 0);
817 skp = smk_import_entry(opts->fshat, 0);
823 skp = smk_import_entry(opts->fsroot, 0);
828 if (opts->fstransmute) {
829 skp = smk_import_entry(opts->fstransmute, 0);
838 * Initialize the root inode.
840 init_inode_smack(inode, sp->smk_root);
843 isp = smack_inode(inode);
844 isp->smk_flags |= SMK_INODE_TRANSMUTE;
851 * smack_sb_statfs - Smack check on statfs
852 * @dentry: identifies the file system in question
854 * Returns 0 if current can read the floor of the filesystem,
855 * and error code otherwise
857 static int smack_sb_statfs(struct dentry *dentry)
859 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
861 struct smk_audit_info ad;
863 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
864 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
866 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
867 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
876 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
877 * @bprm: the exec information
879 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
881 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
883 struct inode *inode = file_inode(bprm->file);
884 struct task_smack *bsp = smack_cred(bprm->cred);
885 struct inode_smack *isp;
886 struct superblock_smack *sbsp;
889 isp = smack_inode(inode);
890 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
893 sbsp = smack_superblock(inode->i_sb);
894 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
895 isp->smk_task != sbsp->smk_root)
898 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
899 struct task_struct *tracer;
903 tracer = ptrace_parent(current);
904 if (likely(tracer != NULL))
905 rc = smk_ptrace_rule_check(tracer,
914 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
917 bsp->smk_task = isp->smk_task;
918 bprm->per_clear |= PER_CLEAR_ON_SETID;
920 /* Decide if this is a secure exec. */
921 if (bsp->smk_task != bsp->smk_forked)
922 bprm->secureexec = 1;
932 * smack_inode_alloc_security - allocate an inode blob
933 * @inode: the inode in need of a blob
937 static int smack_inode_alloc_security(struct inode *inode)
939 struct smack_known *skp = smk_of_current();
941 init_inode_smack(inode, skp);
946 * smack_inode_init_security - copy out the smack from an inode
947 * @inode: the newly created inode
948 * @dir: containing directory object
950 * @name: where to put the attribute name
951 * @value: where to put the attribute value
952 * @len: where to put the length of the attribute
954 * Returns 0 if it all works out, -ENOMEM if there's no memory
956 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
957 const struct qstr *qstr, const char **name,
958 void **value, size_t *len)
960 struct inode_smack *issp = smack_inode(inode);
961 struct smack_known *skp = smk_of_current();
962 struct smack_known *isp = smk_of_inode(inode);
963 struct smack_known *dsp = smk_of_inode(dir);
967 *name = XATTR_SMACK_SUFFIX;
971 may = smk_access_entry(skp->smk_known, dsp->smk_known,
976 * If the access rule allows transmutation and
977 * the directory requests transmutation then
978 * by all means transmute.
979 * Mark the inode as changed.
981 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
982 smk_inode_transmutable(dir)) {
984 issp->smk_flags |= SMK_INODE_CHANGED;
987 *value = kstrdup(isp->smk_known, GFP_NOFS);
991 *len = strlen(isp->smk_known);
998 * smack_inode_link - Smack check on link
999 * @old_dentry: the existing object
1001 * @new_dentry: the new object
1003 * Returns 0 if access is permitted, an error code otherwise
1005 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1006 struct dentry *new_dentry)
1008 struct smack_known *isp;
1009 struct smk_audit_info ad;
1012 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1013 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1015 isp = smk_of_inode(d_backing_inode(old_dentry));
1016 rc = smk_curacc(isp, MAY_WRITE, &ad);
1017 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1019 if (rc == 0 && d_is_positive(new_dentry)) {
1020 isp = smk_of_inode(d_backing_inode(new_dentry));
1021 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1022 rc = smk_curacc(isp, MAY_WRITE, &ad);
1023 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1030 * smack_inode_unlink - Smack check on inode deletion
1031 * @dir: containing directory object
1032 * @dentry: file to unlink
1034 * Returns 0 if current can write the containing directory
1035 * and the object, error code otherwise
1037 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1039 struct inode *ip = d_backing_inode(dentry);
1040 struct smk_audit_info ad;
1043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1044 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1047 * You need write access to the thing you're unlinking
1049 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1050 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1053 * You also need write access to the containing directory
1055 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1056 smk_ad_setfield_u_fs_inode(&ad, dir);
1057 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1058 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1064 * smack_inode_rmdir - Smack check on directory deletion
1065 * @dir: containing directory object
1066 * @dentry: directory to unlink
1068 * Returns 0 if current can write the containing directory
1069 * and the directory, error code otherwise
1071 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1073 struct smk_audit_info ad;
1076 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1077 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1080 * You need write access to the thing you're removing
1082 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1083 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1086 * You also need write access to the containing directory
1088 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1089 smk_ad_setfield_u_fs_inode(&ad, dir);
1090 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1091 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1098 * smack_inode_rename - Smack check on rename
1099 * @old_inode: unused
1100 * @old_dentry: the old object
1101 * @new_inode: unused
1102 * @new_dentry: the new object
1104 * Read and write access is required on both the old and
1107 * Returns 0 if access is permitted, an error code otherwise
1109 static int smack_inode_rename(struct inode *old_inode,
1110 struct dentry *old_dentry,
1111 struct inode *new_inode,
1112 struct dentry *new_dentry)
1115 struct smack_known *isp;
1116 struct smk_audit_info ad;
1118 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1119 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1121 isp = smk_of_inode(d_backing_inode(old_dentry));
1122 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1123 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1125 if (rc == 0 && d_is_positive(new_dentry)) {
1126 isp = smk_of_inode(d_backing_inode(new_dentry));
1127 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1128 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1129 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1135 * smack_inode_permission - Smack version of permission()
1136 * @inode: the inode in question
1137 * @mask: the access requested
1139 * This is the important Smack hook.
1141 * Returns 0 if access is permitted, an error code otherwise
1143 static int smack_inode_permission(struct inode *inode, int mask)
1145 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
1146 struct smk_audit_info ad;
1147 int no_block = mask & MAY_NOT_BLOCK;
1150 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1152 * No permission to check. Existence test. Yup, it's there.
1157 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1158 if (smk_of_inode(inode) != sbsp->smk_root)
1162 /* May be droppable after audit */
1165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1166 smk_ad_setfield_u_fs_inode(&ad, inode);
1167 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1168 rc = smk_bu_inode(inode, mask, rc);
1173 * smack_inode_setattr - Smack check for setting attributes
1174 * @dentry: the object
1175 * @iattr: for the force flag
1177 * Returns 0 if access is permitted, an error code otherwise
1179 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1181 struct smk_audit_info ad;
1185 * Need to allow for clearing the setuid bit.
1187 if (iattr->ia_valid & ATTR_FORCE)
1189 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1190 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1192 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1193 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1198 * smack_inode_getattr - Smack check for getting attributes
1199 * @path: path to extract the info from
1201 * Returns 0 if access is permitted, an error code otherwise
1203 static int smack_inode_getattr(const struct path *path)
1205 struct smk_audit_info ad;
1206 struct inode *inode = d_backing_inode(path->dentry);
1209 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1210 smk_ad_setfield_u_fs_path(&ad, *path);
1211 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1212 rc = smk_bu_inode(inode, MAY_READ, rc);
1217 * smack_inode_setxattr - Smack check for setting xattrs
1218 * @dentry: the object
1219 * @name: name of the attribute
1220 * @value: value of the attribute
1221 * @size: size of the value
1224 * This protects the Smack attribute explicitly.
1226 * Returns 0 if access is permitted, an error code otherwise
1228 static int smack_inode_setxattr(struct user_namespace *mnt_userns,
1229 struct dentry *dentry, const char *name,
1230 const void *value, size_t size, int flags)
1232 struct smk_audit_info ad;
1233 struct smack_known *skp;
1235 int check_import = 0;
1240 * Check label validity here so import won't fail in post_setxattr
1242 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1243 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1244 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1247 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1248 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1252 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1254 if (size != TRANS_TRUE_SIZE ||
1255 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1258 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1260 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1263 if (rc == 0 && check_import) {
1264 skp = size ? smk_import_entry(value, size) : NULL;
1267 else if (skp == NULL || (check_star &&
1268 (skp == &smack_known_star || skp == &smack_known_web)))
1272 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1273 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1276 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1277 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1284 * smack_inode_post_setxattr - Apply the Smack update approved above
1286 * @name: attribute name
1287 * @value: attribute value
1288 * @size: attribute size
1291 * Set the pointer in the inode blob to the entry found
1292 * in the master label list.
1294 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1295 const void *value, size_t size, int flags)
1297 struct smack_known *skp;
1298 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1300 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1301 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1305 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1306 skp = smk_import_entry(value, size);
1308 isp->smk_inode = skp;
1309 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1310 skp = smk_import_entry(value, size);
1312 isp->smk_task = skp;
1313 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1314 skp = smk_import_entry(value, size);
1316 isp->smk_mmap = skp;
1323 * smack_inode_getxattr - Smack check on getxattr
1324 * @dentry: the object
1327 * Returns 0 if access is permitted, an error code otherwise
1329 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1331 struct smk_audit_info ad;
1334 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1335 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1337 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1338 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1343 * smack_inode_removexattr - Smack check on removexattr
1344 * @dentry: the object
1345 * @name: name of the attribute
1347 * Removing the Smack attribute requires CAP_MAC_ADMIN
1349 * Returns 0 if access is permitted, an error code otherwise
1351 static int smack_inode_removexattr(struct user_namespace *mnt_userns,
1352 struct dentry *dentry, const char *name)
1354 struct inode_smack *isp;
1355 struct smk_audit_info ad;
1358 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1359 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1360 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1361 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1362 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1363 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1364 if (!smack_privileged(CAP_MAC_ADMIN))
1367 rc = cap_inode_removexattr(mnt_userns, dentry, name);
1372 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1373 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1375 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1376 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1380 isp = smack_inode(d_backing_inode(dentry));
1382 * Don't do anything special for these.
1383 * XATTR_NAME_SMACKIPIN
1384 * XATTR_NAME_SMACKIPOUT
1386 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1387 struct super_block *sbp = dentry->d_sb;
1388 struct superblock_smack *sbsp = smack_superblock(sbp);
1390 isp->smk_inode = sbsp->smk_default;
1391 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1392 isp->smk_task = NULL;
1393 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1394 isp->smk_mmap = NULL;
1395 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1396 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1402 * smack_inode_getsecurity - get smack xattrs
1403 * @inode: the object
1404 * @name: attribute name
1405 * @buffer: where to put the result
1406 * @alloc: duplicate memory
1408 * Returns the size of the attribute or an error code
1410 static int smack_inode_getsecurity(struct user_namespace *mnt_userns,
1411 struct inode *inode, const char *name,
1412 void **buffer, bool alloc)
1414 struct socket_smack *ssp;
1415 struct socket *sock;
1416 struct super_block *sbp;
1417 struct inode *ip = (struct inode *)inode;
1418 struct smack_known *isp;
1420 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1421 isp = smk_of_inode(inode);
1424 * The rest of the Smack xattrs are only on sockets.
1427 if (sbp->s_magic != SOCKFS_MAGIC)
1430 sock = SOCKET_I(ip);
1431 if (sock == NULL || sock->sk == NULL)
1434 ssp = sock->sk->sk_security;
1436 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1438 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1445 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1446 if (*buffer == NULL)
1450 return strlen(isp->smk_known);
1455 * smack_inode_listsecurity - list the Smack attributes
1456 * @inode: the object
1457 * @buffer: where they go
1458 * @buffer_size: size of buffer
1460 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1463 int len = sizeof(XATTR_NAME_SMACK);
1465 if (buffer != NULL && len <= buffer_size)
1466 memcpy(buffer, XATTR_NAME_SMACK, len);
1472 * smack_inode_getsecid - Extract inode's security id
1473 * @inode: inode to extract the info from
1474 * @secid: where result will be saved
1476 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1478 struct smack_known *skp = smk_of_inode(inode);
1480 *secid = skp->smk_secid;
1488 * There is no smack_file_permission hook
1490 * Should access checks be done on each read or write?
1491 * UNICOS and SELinux say yes.
1492 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1494 * I'll say no for now. Smack does not do the frequent
1495 * label changing that SELinux does.
1499 * smack_file_alloc_security - assign a file security blob
1502 * The security blob for a file is a pointer to the master
1503 * label list, so no allocation is done.
1505 * f_security is the owner security information. It
1506 * isn't used on file access checks, it's for send_sigio.
1510 static int smack_file_alloc_security(struct file *file)
1512 struct smack_known **blob = smack_file(file);
1514 *blob = smk_of_current();
1519 * smack_file_ioctl - Smack check on ioctls
1524 * Relies heavily on the correct use of the ioctl command conventions.
1526 * Returns 0 if allowed, error code otherwise
1528 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1532 struct smk_audit_info ad;
1533 struct inode *inode = file_inode(file);
1535 if (unlikely(IS_PRIVATE(inode)))
1538 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1539 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1541 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1542 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1543 rc = smk_bu_file(file, MAY_WRITE, rc);
1546 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1547 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1548 rc = smk_bu_file(file, MAY_READ, rc);
1555 * smack_file_lock - Smack check on file locking
1559 * Returns 0 if current has lock access, error code otherwise
1561 static int smack_file_lock(struct file *file, unsigned int cmd)
1563 struct smk_audit_info ad;
1565 struct inode *inode = file_inode(file);
1567 if (unlikely(IS_PRIVATE(inode)))
1570 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1571 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1572 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1573 rc = smk_bu_file(file, MAY_LOCK, rc);
1578 * smack_file_fcntl - Smack check on fcntl
1580 * @cmd: what action to check
1583 * Generally these operations are harmless.
1584 * File locking operations present an obvious mechanism
1585 * for passing information, so they require write access.
1587 * Returns 0 if current has access, error code otherwise
1589 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1592 struct smk_audit_info ad;
1594 struct inode *inode = file_inode(file);
1596 if (unlikely(IS_PRIVATE(inode)))
1604 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1605 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1606 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1607 rc = smk_bu_file(file, MAY_LOCK, rc);
1611 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1612 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1613 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1614 rc = smk_bu_file(file, MAY_WRITE, rc);
1625 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1626 * if mapping anonymous memory.
1627 * @file contains the file structure for file to map (may be NULL).
1628 * @reqprot contains the protection requested by the application.
1629 * @prot contains the protection that will be applied by the kernel.
1630 * @flags contains the operational flags.
1631 * Return 0 if permission is granted.
1633 static int smack_mmap_file(struct file *file,
1634 unsigned long reqprot, unsigned long prot,
1635 unsigned long flags)
1637 struct smack_known *skp;
1638 struct smack_known *mkp;
1639 struct smack_rule *srp;
1640 struct task_smack *tsp;
1641 struct smack_known *okp;
1642 struct inode_smack *isp;
1643 struct superblock_smack *sbsp;
1652 if (unlikely(IS_PRIVATE(file_inode(file))))
1655 isp = smack_inode(file_inode(file));
1656 if (isp->smk_mmap == NULL)
1658 sbsp = smack_superblock(file_inode(file)->i_sb);
1659 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1660 isp->smk_mmap != sbsp->smk_root)
1662 mkp = isp->smk_mmap;
1664 tsp = smack_cred(current_cred());
1665 skp = smk_of_current();
1670 * For each Smack rule associated with the subject
1671 * label verify that the SMACK64MMAP also has access
1672 * to that rule's object label.
1674 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1675 okp = srp->smk_object;
1677 * Matching labels always allows access.
1679 if (mkp->smk_known == okp->smk_known)
1682 * If there is a matching local rule take
1683 * that into account as well.
1685 may = smk_access_entry(srp->smk_subject->smk_known,
1689 may = srp->smk_access;
1691 may &= srp->smk_access;
1693 * If may is zero the SMACK64MMAP subject can't
1694 * possibly have less access.
1700 * Fetch the global list entry.
1701 * If there isn't one a SMACK64MMAP subject
1702 * can't have as much access as current.
1704 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1706 if (mmay == -ENOENT) {
1711 * If there is a local entry it modifies the
1712 * potential access, too.
1714 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1716 if (tmay != -ENOENT)
1720 * If there is any access available to current that is
1721 * not available to a SMACK64MMAP subject
1724 if ((may | mmay) != mmay) {
1736 * smack_file_set_fowner - set the file security blob value
1737 * @file: object in question
1740 static void smack_file_set_fowner(struct file *file)
1742 struct smack_known **blob = smack_file(file);
1744 *blob = smk_of_current();
1748 * smack_file_send_sigiotask - Smack on sigio
1749 * @tsk: The target task
1750 * @fown: the object the signal come from
1753 * Allow a privileged task to get signals even if it shouldn't
1755 * Returns 0 if a subject with the object's smack could
1756 * write to the task, an error code otherwise.
1758 static int smack_file_send_sigiotask(struct task_struct *tsk,
1759 struct fown_struct *fown, int signum)
1761 struct smack_known **blob;
1762 struct smack_known *skp;
1763 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1764 const struct cred *tcred;
1767 struct smk_audit_info ad;
1770 * struct fown_struct is never outside the context of a struct file
1772 file = container_of(fown, struct file, f_owner);
1774 /* we don't log here as rc can be overriden */
1775 blob = smack_file(file);
1777 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1778 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1781 tcred = __task_cred(tsk);
1782 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1786 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1787 smk_ad_setfield_u_tsk(&ad, tsk);
1788 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1793 * smack_file_receive - Smack file receive check
1796 * Returns 0 if current has access, error code otherwise
1798 static int smack_file_receive(struct file *file)
1802 struct smk_audit_info ad;
1803 struct inode *inode = file_inode(file);
1804 struct socket *sock;
1805 struct task_smack *tsp;
1806 struct socket_smack *ssp;
1808 if (unlikely(IS_PRIVATE(inode)))
1811 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1812 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1814 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1815 sock = SOCKET_I(inode);
1816 ssp = sock->sk->sk_security;
1817 tsp = smack_cred(current_cred());
1819 * If the receiving process can't write to the
1820 * passed socket or if the passed socket can't
1821 * write to the receiving process don't accept
1822 * the passed socket.
1824 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1825 rc = smk_bu_file(file, may, rc);
1828 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1829 rc = smk_bu_file(file, may, rc);
1833 * This code relies on bitmasks.
1835 if (file->f_mode & FMODE_READ)
1837 if (file->f_mode & FMODE_WRITE)
1840 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1841 rc = smk_bu_file(file, may, rc);
1846 * smack_file_open - Smack dentry open processing
1849 * Set the security blob in the file structure.
1850 * Allow the open only if the task has read access. There are
1851 * many read operations (e.g. fstat) that you can do with an
1852 * fd even if you have the file open write-only.
1854 * Returns 0 if current has access, error code otherwise
1856 static int smack_file_open(struct file *file)
1858 struct task_smack *tsp = smack_cred(file->f_cred);
1859 struct inode *inode = file_inode(file);
1860 struct smk_audit_info ad;
1863 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1864 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1865 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1866 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1876 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1877 * @cred: the new credentials
1878 * @gfp: the atomicity of any memory allocations
1880 * Prepare a blank set of credentials for modification. This must allocate all
1881 * the memory the LSM module might require such that cred_transfer() can
1882 * complete without error.
1884 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1886 init_task_smack(smack_cred(cred), NULL, NULL);
1892 * smack_cred_free - "free" task-level security credentials
1893 * @cred: the credentials in question
1896 static void smack_cred_free(struct cred *cred)
1898 struct task_smack *tsp = smack_cred(cred);
1899 struct smack_rule *rp;
1900 struct list_head *l;
1901 struct list_head *n;
1903 smk_destroy_label_list(&tsp->smk_relabel);
1905 list_for_each_safe(l, n, &tsp->smk_rules) {
1906 rp = list_entry(l, struct smack_rule, list);
1907 list_del(&rp->list);
1908 kmem_cache_free(smack_rule_cache, rp);
1913 * smack_cred_prepare - prepare new set of credentials for modification
1914 * @new: the new credentials
1915 * @old: the original credentials
1916 * @gfp: the atomicity of any memory allocations
1918 * Prepare a new set of credentials for modification.
1920 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1923 struct task_smack *old_tsp = smack_cred(old);
1924 struct task_smack *new_tsp = smack_cred(new);
1927 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1929 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1933 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1939 * smack_cred_transfer - Transfer the old credentials to the new credentials
1940 * @new: the new credentials
1941 * @old: the original credentials
1943 * Fill in a set of blank credentials from another set of credentials.
1945 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1947 struct task_smack *old_tsp = smack_cred(old);
1948 struct task_smack *new_tsp = smack_cred(new);
1950 new_tsp->smk_task = old_tsp->smk_task;
1951 new_tsp->smk_forked = old_tsp->smk_task;
1952 mutex_init(&new_tsp->smk_rules_lock);
1953 INIT_LIST_HEAD(&new_tsp->smk_rules);
1955 /* cbs copy rule list */
1959 * smack_cred_getsecid - get the secid corresponding to a creds structure
1960 * @cred: the object creds
1961 * @secid: where to put the result
1963 * Sets the secid to contain a u32 version of the smack label.
1965 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1967 struct smack_known *skp;
1970 skp = smk_of_task(smack_cred(cred));
1971 *secid = skp->smk_secid;
1976 * smack_kernel_act_as - Set the subjective context in a set of credentials
1977 * @new: points to the set of credentials to be modified.
1978 * @secid: specifies the security ID to be set
1980 * Set the security data for a kernel service.
1982 static int smack_kernel_act_as(struct cred *new, u32 secid)
1984 struct task_smack *new_tsp = smack_cred(new);
1986 new_tsp->smk_task = smack_from_secid(secid);
1991 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1992 * @new: points to the set of credentials to be modified
1993 * @inode: points to the inode to use as a reference
1995 * Set the file creation context in a set of credentials to the same
1996 * as the objective context of the specified inode
1998 static int smack_kernel_create_files_as(struct cred *new,
1999 struct inode *inode)
2001 struct inode_smack *isp = smack_inode(inode);
2002 struct task_smack *tsp = smack_cred(new);
2004 tsp->smk_forked = isp->smk_inode;
2005 tsp->smk_task = tsp->smk_forked;
2010 * smk_curacc_on_task - helper to log task related access
2011 * @p: the task object
2012 * @access: the access requested
2013 * @caller: name of the calling function for audit
2015 * Return 0 if access is permitted
2017 static int smk_curacc_on_task(struct task_struct *p, int access,
2020 struct smk_audit_info ad;
2021 struct smack_known *skp = smk_of_task_struct_obj(p);
2024 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2025 smk_ad_setfield_u_tsk(&ad, p);
2026 rc = smk_curacc(skp, access, &ad);
2027 rc = smk_bu_task(p, access, rc);
2032 * smack_task_setpgid - Smack check on setting pgid
2033 * @p: the task object
2036 * Return 0 if write access is permitted
2038 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2040 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2044 * smack_task_getpgid - Smack access check for getpgid
2045 * @p: the object task
2047 * Returns 0 if current can read the object task, error code otherwise
2049 static int smack_task_getpgid(struct task_struct *p)
2051 return smk_curacc_on_task(p, MAY_READ, __func__);
2055 * smack_task_getsid - Smack access check for getsid
2056 * @p: the object task
2058 * Returns 0 if current can read the object task, error code otherwise
2060 static int smack_task_getsid(struct task_struct *p)
2062 return smk_curacc_on_task(p, MAY_READ, __func__);
2066 * smack_task_getsecid_subj - get the subjective secid of the task
2068 * @secid: where to put the result
2070 * Sets the secid to contain a u32 version of the task's subjective smack label.
2072 static void smack_task_getsecid_subj(struct task_struct *p, u32 *secid)
2074 struct smack_known *skp = smk_of_task_struct_subj(p);
2076 *secid = skp->smk_secid;
2080 * smack_task_getsecid_obj - get the objective secid of the task
2082 * @secid: where to put the result
2084 * Sets the secid to contain a u32 version of the task's objective smack label.
2086 static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2088 struct smack_known *skp = smk_of_task_struct_obj(p);
2090 *secid = skp->smk_secid;
2094 * smack_task_setnice - Smack check on setting nice
2095 * @p: the task object
2098 * Return 0 if write access is permitted
2100 static int smack_task_setnice(struct task_struct *p, int nice)
2102 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2106 * smack_task_setioprio - Smack check on setting ioprio
2107 * @p: the task object
2110 * Return 0 if write access is permitted
2112 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2114 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2118 * smack_task_getioprio - Smack check on reading ioprio
2119 * @p: the task object
2121 * Return 0 if read access is permitted
2123 static int smack_task_getioprio(struct task_struct *p)
2125 return smk_curacc_on_task(p, MAY_READ, __func__);
2129 * smack_task_setscheduler - Smack check on setting scheduler
2130 * @p: the task object
2132 * Return 0 if read access is permitted
2134 static int smack_task_setscheduler(struct task_struct *p)
2136 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2140 * smack_task_getscheduler - Smack check on reading scheduler
2141 * @p: the task object
2143 * Return 0 if read access is permitted
2145 static int smack_task_getscheduler(struct task_struct *p)
2147 return smk_curacc_on_task(p, MAY_READ, __func__);
2151 * smack_task_movememory - Smack check on moving memory
2152 * @p: the task object
2154 * Return 0 if write access is permitted
2156 static int smack_task_movememory(struct task_struct *p)
2158 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2162 * smack_task_kill - Smack check on signal delivery
2163 * @p: the task object
2166 * @cred: identifies the cred to use in lieu of current's
2168 * Return 0 if write access is permitted
2171 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2172 int sig, const struct cred *cred)
2174 struct smk_audit_info ad;
2175 struct smack_known *skp;
2176 struct smack_known *tkp = smk_of_task_struct_obj(p);
2180 return 0; /* null signal; existence test */
2182 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2183 smk_ad_setfield_u_tsk(&ad, p);
2185 * Sending a signal requires that the sender
2186 * can write the receiver.
2189 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2190 rc = smk_bu_task(p, MAY_DELIVER, rc);
2194 * If the cred isn't NULL we're dealing with some USB IO
2195 * specific behavior. This is not clean. For one thing
2196 * we can't take privilege into account.
2198 skp = smk_of_task(smack_cred(cred));
2199 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2200 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2205 * smack_task_to_inode - copy task smack into the inode blob
2206 * @p: task to copy from
2207 * @inode: inode to copy to
2209 * Sets the smack pointer in the inode security blob
2211 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2213 struct inode_smack *isp = smack_inode(inode);
2214 struct smack_known *skp = smk_of_task_struct_obj(p);
2216 isp->smk_inode = skp;
2217 isp->smk_flags |= SMK_INODE_INSTANT;
2225 * smack_sk_alloc_security - Allocate a socket blob
2228 * @gfp_flags: memory allocation flags
2230 * Assign Smack pointers to current
2232 * Returns 0 on success, -ENOMEM is there's no memory
2234 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2236 struct smack_known *skp = smk_of_current();
2237 struct socket_smack *ssp;
2239 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2244 * Sockets created by kernel threads receive web label.
2246 if (unlikely(current->flags & PF_KTHREAD)) {
2247 ssp->smk_in = &smack_known_web;
2248 ssp->smk_out = &smack_known_web;
2253 ssp->smk_packet = NULL;
2255 sk->sk_security = ssp;
2261 * smack_sk_free_security - Free a socket blob
2264 * Clears the blob pointer
2266 static void smack_sk_free_security(struct sock *sk)
2268 #ifdef SMACK_IPV6_PORT_LABELING
2269 struct smk_port_label *spp;
2271 if (sk->sk_family == PF_INET6) {
2273 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2274 if (spp->smk_sock != sk)
2276 spp->smk_can_reuse = 1;
2282 kfree(sk->sk_security);
2286 * smack_ipv4host_label - check host based restrictions
2287 * @sip: the object end
2289 * looks for host based access restrictions
2291 * This version will only be appropriate for really small sets of single label
2292 * hosts. The caller is responsible for ensuring that the RCU read lock is
2293 * taken before calling this function.
2295 * Returns the label of the far end or NULL if it's not special.
2297 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2299 struct smk_net4addr *snp;
2300 struct in_addr *siap = &sip->sin_addr;
2302 if (siap->s_addr == 0)
2305 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2307 * we break after finding the first match because
2308 * the list is sorted from longest to shortest mask
2309 * so we have found the most specific match
2311 if (snp->smk_host.s_addr ==
2312 (siap->s_addr & snp->smk_mask.s_addr))
2313 return snp->smk_label;
2319 * smk_ipv6_localhost - Check for local ipv6 host address
2322 * Returns boolean true if this is the localhost address
2324 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2326 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2327 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2329 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2330 ntohs(be16p[7]) == 1)
2336 * smack_ipv6host_label - check host based restrictions
2337 * @sip: the object end
2339 * looks for host based access restrictions
2341 * This version will only be appropriate for really small sets of single label
2342 * hosts. The caller is responsible for ensuring that the RCU read lock is
2343 * taken before calling this function.
2345 * Returns the label of the far end or NULL if it's not special.
2347 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2349 struct smk_net6addr *snp;
2350 struct in6_addr *sap = &sip->sin6_addr;
2355 * It's local. Don't look for a host label.
2357 if (smk_ipv6_localhost(sip))
2360 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2362 * If the label is NULL the entry has
2363 * been renounced. Ignore it.
2365 if (snp->smk_label == NULL)
2368 * we break after finding the first match because
2369 * the list is sorted from longest to shortest mask
2370 * so we have found the most specific match
2372 for (found = 1, i = 0; i < 8; i++) {
2373 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2374 snp->smk_host.s6_addr16[i]) {
2380 return snp->smk_label;
2387 * smack_netlbl_add - Set the secattr on a socket
2390 * Attach the outbound smack value (smk_out) to the socket.
2392 * Returns 0 on success or an error code
2394 static int smack_netlbl_add(struct sock *sk)
2396 struct socket_smack *ssp = sk->sk_security;
2397 struct smack_known *skp = ssp->smk_out;
2401 bh_lock_sock_nested(sk);
2403 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2406 ssp->smk_state = SMK_NETLBL_LABELED;
2409 ssp->smk_state = SMK_NETLBL_REQSKB;
2421 * smack_netlbl_delete - Remove the secattr from a socket
2424 * Remove the outbound smack value from a socket
2426 static void smack_netlbl_delete(struct sock *sk)
2428 struct socket_smack *ssp = sk->sk_security;
2431 * Take the label off the socket if one is set.
2433 if (ssp->smk_state != SMK_NETLBL_LABELED)
2437 bh_lock_sock_nested(sk);
2438 netlbl_sock_delattr(sk);
2441 ssp->smk_state = SMK_NETLBL_UNLABELED;
2445 * smk_ipv4_check - Perform IPv4 host access checks
2447 * @sap: the destination address
2449 * Set the correct secattr for the given socket based on the destination
2450 * address and perform any outbound access checks needed.
2452 * Returns 0 on success or an error code.
2455 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2457 struct smack_known *skp;
2459 struct smack_known *hkp;
2460 struct socket_smack *ssp = sk->sk_security;
2461 struct smk_audit_info ad;
2464 hkp = smack_ipv4host_label(sap);
2467 struct lsm_network_audit net;
2469 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2470 ad.a.u.net->family = sap->sin_family;
2471 ad.a.u.net->dport = sap->sin_port;
2472 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2475 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2476 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2478 * Clear the socket netlabel if it's set.
2481 smack_netlbl_delete(sk);
2489 * smk_ipv6_check - check Smack access
2490 * @subject: subject Smack label
2491 * @object: object Smack label
2493 * @act: the action being taken
2495 * Check an IPv6 access
2497 static int smk_ipv6_check(struct smack_known *subject,
2498 struct smack_known *object,
2499 struct sockaddr_in6 *address, int act)
2502 struct lsm_network_audit net;
2504 struct smk_audit_info ad;
2508 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2509 ad.a.u.net->family = PF_INET6;
2510 ad.a.u.net->dport = address->sin6_port;
2511 if (act == SMK_RECEIVING)
2512 ad.a.u.net->v6info.saddr = address->sin6_addr;
2514 ad.a.u.net->v6info.daddr = address->sin6_addr;
2516 rc = smk_access(subject, object, MAY_WRITE, &ad);
2517 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2521 #ifdef SMACK_IPV6_PORT_LABELING
2523 * smk_ipv6_port_label - Smack port access table management
2527 * Create or update the port list entry
2529 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2531 struct sock *sk = sock->sk;
2532 struct sockaddr_in6 *addr6;
2533 struct socket_smack *ssp = sock->sk->sk_security;
2534 struct smk_port_label *spp;
2535 unsigned short port = 0;
2537 if (address == NULL) {
2539 * This operation is changing the Smack information
2540 * on the bound socket. Take the changes to the port
2544 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2545 if (sk != spp->smk_sock)
2547 spp->smk_in = ssp->smk_in;
2548 spp->smk_out = ssp->smk_out;
2553 * A NULL address is only used for updating existing
2554 * bound entries. If there isn't one, it's OK.
2560 addr6 = (struct sockaddr_in6 *)address;
2561 port = ntohs(addr6->sin6_port);
2563 * This is a special case that is safely ignored.
2569 * Look for an existing port list entry.
2570 * This is an indication that a port is getting reused.
2573 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2574 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2576 if (spp->smk_can_reuse != 1) {
2580 spp->smk_port = port;
2582 spp->smk_in = ssp->smk_in;
2583 spp->smk_out = ssp->smk_out;
2584 spp->smk_can_reuse = 0;
2590 * A new port entry is required.
2592 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2596 spp->smk_port = port;
2598 spp->smk_in = ssp->smk_in;
2599 spp->smk_out = ssp->smk_out;
2600 spp->smk_sock_type = sock->type;
2601 spp->smk_can_reuse = 0;
2603 mutex_lock(&smack_ipv6_lock);
2604 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2605 mutex_unlock(&smack_ipv6_lock);
2610 * smk_ipv6_port_check - check Smack port access
2613 * @act: the action being taken
2615 * Create or update the port list entry
2617 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2620 struct smk_port_label *spp;
2621 struct socket_smack *ssp = sk->sk_security;
2622 struct smack_known *skp = NULL;
2623 unsigned short port;
2624 struct smack_known *object;
2626 if (act == SMK_RECEIVING) {
2627 skp = smack_ipv6host_label(address);
2628 object = ssp->smk_in;
2631 object = smack_ipv6host_label(address);
2635 * The other end is a single label host.
2637 if (skp != NULL && object != NULL)
2638 return smk_ipv6_check(skp, object, address, act);
2640 skp = smack_net_ambient;
2642 object = smack_net_ambient;
2645 * It's remote, so port lookup does no good.
2647 if (!smk_ipv6_localhost(address))
2648 return smk_ipv6_check(skp, object, address, act);
2651 * It's local so the send check has to have passed.
2653 if (act == SMK_RECEIVING)
2656 port = ntohs(address->sin6_port);
2658 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2659 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2661 object = spp->smk_in;
2662 if (act == SMK_CONNECTING)
2663 ssp->smk_packet = spp->smk_out;
2668 return smk_ipv6_check(skp, object, address, act);
2673 * smack_inode_setsecurity - set smack xattrs
2674 * @inode: the object
2675 * @name: attribute name
2676 * @value: attribute value
2677 * @size: size of the attribute
2680 * Sets the named attribute in the appropriate blob
2682 * Returns 0 on success, or an error code
2684 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2685 const void *value, size_t size, int flags)
2687 struct smack_known *skp;
2688 struct inode_smack *nsp = smack_inode(inode);
2689 struct socket_smack *ssp;
2690 struct socket *sock;
2693 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2696 skp = smk_import_entry(value, size);
2698 return PTR_ERR(skp);
2700 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2701 nsp->smk_inode = skp;
2702 nsp->smk_flags |= SMK_INODE_INSTANT;
2706 * The rest of the Smack xattrs are only on sockets.
2708 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2711 sock = SOCKET_I(inode);
2712 if (sock == NULL || sock->sk == NULL)
2715 ssp = sock->sk->sk_security;
2717 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2719 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2721 if (sock->sk->sk_family == PF_INET) {
2722 rc = smack_netlbl_add(sock->sk);
2725 "Smack: \"%s\" netlbl error %d.\n",
2731 #ifdef SMACK_IPV6_PORT_LABELING
2732 if (sock->sk->sk_family == PF_INET6)
2733 smk_ipv6_port_label(sock, NULL);
2740 * smack_socket_post_create - finish socket setup
2742 * @family: protocol family
2747 * Sets the netlabel information on the socket
2749 * Returns 0 on success, and error code otherwise
2751 static int smack_socket_post_create(struct socket *sock, int family,
2752 int type, int protocol, int kern)
2754 struct socket_smack *ssp;
2756 if (sock->sk == NULL)
2760 * Sockets created by kernel threads receive web label.
2762 if (unlikely(current->flags & PF_KTHREAD)) {
2763 ssp = sock->sk->sk_security;
2764 ssp->smk_in = &smack_known_web;
2765 ssp->smk_out = &smack_known_web;
2768 if (family != PF_INET)
2771 * Set the outbound netlbl.
2773 return smack_netlbl_add(sock->sk);
2777 * smack_socket_socketpair - create socket pair
2778 * @socka: one socket
2779 * @sockb: another socket
2781 * Cross reference the peer labels for SO_PEERSEC
2785 static int smack_socket_socketpair(struct socket *socka,
2786 struct socket *sockb)
2788 struct socket_smack *asp = socka->sk->sk_security;
2789 struct socket_smack *bsp = sockb->sk->sk_security;
2791 asp->smk_packet = bsp->smk_out;
2792 bsp->smk_packet = asp->smk_out;
2797 #ifdef SMACK_IPV6_PORT_LABELING
2799 * smack_socket_bind - record port binding information.
2801 * @address: the port address
2802 * @addrlen: size of the address
2804 * Records the label bound to a port.
2806 * Returns 0 on success, and error code otherwise
2808 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2811 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2812 if (addrlen < SIN6_LEN_RFC2133 ||
2813 address->sa_family != AF_INET6)
2815 smk_ipv6_port_label(sock, address);
2819 #endif /* SMACK_IPV6_PORT_LABELING */
2822 * smack_socket_connect - connect access check
2824 * @sap: the other end
2825 * @addrlen: size of sap
2827 * Verifies that a connection may be possible
2829 * Returns 0 on success, and error code otherwise
2831 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2836 if (sock->sk == NULL)
2838 if (sock->sk->sk_family != PF_INET &&
2839 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2841 if (addrlen < offsetofend(struct sockaddr, sa_family))
2843 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2844 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2845 struct smack_known *rsp = NULL;
2847 if (addrlen < SIN6_LEN_RFC2133)
2849 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2850 rsp = smack_ipv6host_label(sip);
2852 struct socket_smack *ssp = sock->sk->sk_security;
2854 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2857 #ifdef SMACK_IPV6_PORT_LABELING
2858 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2863 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2865 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
2870 * smack_flags_to_may - convert S_ to MAY_ values
2871 * @flags: the S_ value
2873 * Returns the equivalent MAY_ value
2875 static int smack_flags_to_may(int flags)
2879 if (flags & S_IRUGO)
2881 if (flags & S_IWUGO)
2883 if (flags & S_IXUGO)
2890 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2895 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2897 struct smack_known **blob = smack_msg_msg(msg);
2899 *blob = smk_of_current();
2904 * smack_of_ipc - the smack pointer for the ipc
2907 * Returns a pointer to the smack value
2909 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2911 struct smack_known **blob = smack_ipc(isp);
2917 * smack_ipc_alloc_security - Set the security blob for ipc
2922 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2924 struct smack_known **blob = smack_ipc(isp);
2926 *blob = smk_of_current();
2931 * smk_curacc_shm : check if current has access on shm
2933 * @access : access requested
2935 * Returns 0 if current has the requested access, error code otherwise
2937 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2939 struct smack_known *ssp = smack_of_ipc(isp);
2940 struct smk_audit_info ad;
2944 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2945 ad.a.u.ipc_id = isp->id;
2947 rc = smk_curacc(ssp, access, &ad);
2948 rc = smk_bu_current("shm", ssp, access, rc);
2953 * smack_shm_associate - Smack access check for shm
2955 * @shmflg: access requested
2957 * Returns 0 if current has the requested access, error code otherwise
2959 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2963 may = smack_flags_to_may(shmflg);
2964 return smk_curacc_shm(isp, may);
2968 * smack_shm_shmctl - Smack access check for shm
2970 * @cmd: what it wants to do
2972 * Returns 0 if current has the requested access, error code otherwise
2974 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2988 may = MAY_READWRITE;
2993 * System level information.
2999 return smk_curacc_shm(isp, may);
3003 * smack_shm_shmat - Smack access for shmat
3006 * @shmflg: access requested
3008 * Returns 0 if current has the requested access, error code otherwise
3010 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3015 may = smack_flags_to_may(shmflg);
3016 return smk_curacc_shm(isp, may);
3020 * smk_curacc_sem : check if current has access on sem
3022 * @access : access requested
3024 * Returns 0 if current has the requested access, error code otherwise
3026 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3028 struct smack_known *ssp = smack_of_ipc(isp);
3029 struct smk_audit_info ad;
3033 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3034 ad.a.u.ipc_id = isp->id;
3036 rc = smk_curacc(ssp, access, &ad);
3037 rc = smk_bu_current("sem", ssp, access, rc);
3042 * smack_sem_associate - Smack access check for sem
3044 * @semflg: access requested
3046 * Returns 0 if current has the requested access, error code otherwise
3048 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3052 may = smack_flags_to_may(semflg);
3053 return smk_curacc_sem(isp, may);
3057 * smack_sem_shmctl - Smack access check for sem
3059 * @cmd: what it wants to do
3061 * Returns 0 if current has the requested access, error code otherwise
3063 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3082 may = MAY_READWRITE;
3087 * System level information
3094 return smk_curacc_sem(isp, may);
3098 * smack_sem_semop - Smack checks of semaphore operations
3104 * Treated as read and write in all cases.
3106 * Returns 0 if access is allowed, error code otherwise
3108 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3109 unsigned nsops, int alter)
3111 return smk_curacc_sem(isp, MAY_READWRITE);
3115 * smk_curacc_msq : helper to check if current has access on msq
3117 * @access : access requested
3119 * return 0 if current has access, error otherwise
3121 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3123 struct smack_known *msp = smack_of_ipc(isp);
3124 struct smk_audit_info ad;
3128 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3129 ad.a.u.ipc_id = isp->id;
3131 rc = smk_curacc(msp, access, &ad);
3132 rc = smk_bu_current("msq", msp, access, rc);
3137 * smack_msg_queue_associate - Smack access check for msg_queue
3139 * @msqflg: access requested
3141 * Returns 0 if current has the requested access, error code otherwise
3143 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3147 may = smack_flags_to_may(msqflg);
3148 return smk_curacc_msq(isp, may);
3152 * smack_msg_queue_msgctl - Smack access check for msg_queue
3154 * @cmd: what it wants to do
3156 * Returns 0 if current has the requested access, error code otherwise
3158 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3170 may = MAY_READWRITE;
3175 * System level information
3182 return smk_curacc_msq(isp, may);
3186 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3189 * @msqflg: access requested
3191 * Returns 0 if current has the requested access, error code otherwise
3193 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3198 may = smack_flags_to_may(msqflg);
3199 return smk_curacc_msq(isp, may);
3203 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3210 * Returns 0 if current has read and write access, error code otherwise
3212 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3213 struct task_struct *target, long type, int mode)
3215 return smk_curacc_msq(isp, MAY_READWRITE);
3219 * smack_ipc_permission - Smack access for ipc_permission()
3220 * @ipp: the object permissions
3221 * @flag: access requested
3223 * Returns 0 if current has read and write access, error code otherwise
3225 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3227 struct smack_known **blob = smack_ipc(ipp);
3228 struct smack_known *iskp = *blob;
3229 int may = smack_flags_to_may(flag);
3230 struct smk_audit_info ad;
3234 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3235 ad.a.u.ipc_id = ipp->id;
3237 rc = smk_curacc(iskp, may, &ad);
3238 rc = smk_bu_current("svipc", iskp, may, rc);
3243 * smack_ipc_getsecid - Extract smack security id
3244 * @ipp: the object permissions
3245 * @secid: where result will be saved
3247 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3249 struct smack_known **blob = smack_ipc(ipp);
3250 struct smack_known *iskp = *blob;
3252 *secid = iskp->smk_secid;
3256 * smack_d_instantiate - Make sure the blob is correct on an inode
3257 * @opt_dentry: dentry where inode will be attached
3258 * @inode: the object
3260 * Set the inode's security blob if it hasn't been done already.
3262 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3264 struct super_block *sbp;
3265 struct superblock_smack *sbsp;
3266 struct inode_smack *isp;
3267 struct smack_known *skp;
3268 struct smack_known *ckp = smk_of_current();
3269 struct smack_known *final;
3270 char trattr[TRANS_TRUE_SIZE];
3278 isp = smack_inode(inode);
3281 * If the inode is already instantiated
3282 * take the quick way out
3284 if (isp->smk_flags & SMK_INODE_INSTANT)
3288 sbsp = smack_superblock(sbp);
3290 * We're going to use the superblock default label
3291 * if there's no label on the file.
3293 final = sbsp->smk_default;
3296 * If this is the root inode the superblock
3297 * may be in the process of initialization.
3298 * If that is the case use the root value out
3299 * of the superblock.
3301 if (opt_dentry->d_parent == opt_dentry) {
3302 switch (sbp->s_magic) {
3303 case CGROUP_SUPER_MAGIC:
3304 case CGROUP2_SUPER_MAGIC:
3306 * The cgroup filesystem is never mounted,
3307 * so there's no opportunity to set the mount
3310 sbsp->smk_root = &smack_known_star;
3311 sbsp->smk_default = &smack_known_star;
3312 isp->smk_inode = sbsp->smk_root;
3316 * What about shmem/tmpfs anonymous files with dentry
3317 * obtained from d_alloc_pseudo()?
3319 isp->smk_inode = smk_of_current();
3322 isp->smk_inode = smk_of_current();
3326 * Socket access is controlled by the socket
3327 * structures associated with the task involved.
3329 isp->smk_inode = &smack_known_star;
3332 isp->smk_inode = sbsp->smk_root;
3335 isp->smk_flags |= SMK_INODE_INSTANT;
3340 * This is pretty hackish.
3341 * Casey says that we shouldn't have to do
3342 * file system specific code, but it does help
3343 * with keeping it simple.
3345 switch (sbp->s_magic) {
3347 case CGROUP_SUPER_MAGIC:
3348 case CGROUP2_SUPER_MAGIC:
3350 * Casey says that it's a little embarrassing
3351 * that the smack file system doesn't do
3352 * extended attributes.
3354 * Cgroupfs is special
3356 final = &smack_known_star;
3358 case DEVPTS_SUPER_MAGIC:
3360 * devpts seems content with the label of the task.
3361 * Programs that change smack have to treat the
3366 case PROC_SUPER_MAGIC:
3368 * Casey says procfs appears not to care.
3369 * The superblock default suffices.
3374 * Device labels should come from the filesystem,
3375 * but watch out, because they're volitile,
3376 * getting recreated on every reboot.
3378 final = &smack_known_star;
3380 * If a smack value has been set we want to use it,
3381 * but since tmpfs isn't giving us the opportunity
3382 * to set mount options simulate setting the
3383 * superblock default.
3388 * This isn't an understood special case.
3389 * Get the value from the xattr.
3393 * UNIX domain sockets use lower level socket data.
3395 if (S_ISSOCK(inode->i_mode)) {
3396 final = &smack_known_star;
3400 * No xattr support means, alas, no SMACK label.
3401 * Use the aforeapplied default.
3402 * It would be curious if the label of the task
3403 * does not match that assigned.
3405 if (!(inode->i_opflags & IOP_XATTR))
3408 * Get the dentry for xattr.
3410 dp = dget(opt_dentry);
3411 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3412 if (!IS_ERR_OR_NULL(skp))
3416 * Transmuting directory
3418 if (S_ISDIR(inode->i_mode)) {
3420 * If this is a new directory and the label was
3421 * transmuted when the inode was initialized
3422 * set the transmute attribute on the directory
3423 * and mark the inode.
3425 * If there is a transmute attribute on the
3426 * directory mark the inode.
3428 if (isp->smk_flags & SMK_INODE_CHANGED) {
3429 isp->smk_flags &= ~SMK_INODE_CHANGED;
3430 rc = __vfs_setxattr(&init_user_ns, dp, inode,
3431 XATTR_NAME_SMACKTRANSMUTE,
3432 TRANS_TRUE, TRANS_TRUE_SIZE,
3435 rc = __vfs_getxattr(dp, inode,
3436 XATTR_NAME_SMACKTRANSMUTE, trattr,
3438 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3439 TRANS_TRUE_SIZE) != 0)
3443 transflag = SMK_INODE_TRANSMUTE;
3446 * Don't let the exec or mmap label be "*" or "@".
3448 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3449 if (IS_ERR(skp) || skp == &smack_known_star ||
3450 skp == &smack_known_web)
3452 isp->smk_task = skp;
3454 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3455 if (IS_ERR(skp) || skp == &smack_known_star ||
3456 skp == &smack_known_web)
3458 isp->smk_mmap = skp;
3465 isp->smk_inode = ckp;
3467 isp->smk_inode = final;
3469 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3475 * smack_getprocattr - Smack process attribute access
3476 * @p: the object task
3477 * @name: the name of the attribute in /proc/.../attr
3478 * @value: where to put the result
3480 * Places a copy of the task Smack into value
3482 * Returns the length of the smack label or an error code
3484 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3486 struct smack_known *skp = smk_of_task_struct_obj(p);
3490 if (strcmp(name, "current") != 0)
3493 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3503 * smack_setprocattr - Smack process attribute setting
3504 * @name: the name of the attribute in /proc/.../attr
3505 * @value: the value to set
3506 * @size: the size of the value
3508 * Sets the Smack value of the task. Only setting self
3509 * is permitted and only with privilege
3511 * Returns the length of the smack label or an error code
3513 static int smack_setprocattr(const char *name, void *value, size_t size)
3515 struct task_smack *tsp = smack_cred(current_cred());
3517 struct smack_known *skp;
3518 struct smack_known_list_elem *sklep;
3521 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3524 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3527 if (strcmp(name, "current") != 0)
3530 skp = smk_import_entry(value, size);
3532 return PTR_ERR(skp);
3535 * No process is ever allowed the web ("@") label
3536 * and the star ("*") label.
3538 if (skp == &smack_known_web || skp == &smack_known_star)
3541 if (!smack_privileged(CAP_MAC_ADMIN)) {
3543 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3544 if (sklep->smk_label == skp) {
3552 new = prepare_creds();
3556 tsp = smack_cred(new);
3557 tsp->smk_task = skp;
3559 * process can change its label only once
3561 smk_destroy_label_list(&tsp->smk_relabel);
3568 * smack_unix_stream_connect - Smack access on UDS
3570 * @other: the other sock
3573 * Return 0 if a subject with the smack of sock could access
3574 * an object with the smack of other, otherwise an error code
3576 static int smack_unix_stream_connect(struct sock *sock,
3577 struct sock *other, struct sock *newsk)
3579 struct smack_known *skp;
3580 struct smack_known *okp;
3581 struct socket_smack *ssp = sock->sk_security;
3582 struct socket_smack *osp = other->sk_security;
3583 struct socket_smack *nsp = newsk->sk_security;
3584 struct smk_audit_info ad;
3587 struct lsm_network_audit net;
3590 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3594 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3595 smk_ad_setfield_u_net_sk(&ad, other);
3597 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3598 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3602 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3603 rc = smk_bu_note("UDS connect", okp, skp,
3609 * Cross reference the peer labels for SO_PEERSEC.
3612 nsp->smk_packet = ssp->smk_out;
3613 ssp->smk_packet = osp->smk_out;
3620 * smack_unix_may_send - Smack access on UDS
3622 * @other: the other socket
3624 * Return 0 if a subject with the smack of sock could access
3625 * an object with the smack of other, otherwise an error code
3627 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3629 struct socket_smack *ssp = sock->sk->sk_security;
3630 struct socket_smack *osp = other->sk->sk_security;
3631 struct smk_audit_info ad;
3635 struct lsm_network_audit net;
3637 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3638 smk_ad_setfield_u_net_sk(&ad, other->sk);
3641 if (smack_privileged(CAP_MAC_OVERRIDE))
3644 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3645 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3650 * smack_socket_sendmsg - Smack check based on destination host
3653 * @size: the size of the message
3655 * Return 0 if the current subject can write to the destination host.
3656 * For IPv4 this is only a question if the destination is a single label host.
3657 * For IPv6 this is a check against the label of the port.
3659 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3662 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3663 #if IS_ENABLED(CONFIG_IPV6)
3664 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3666 #ifdef SMACK_IPV6_SECMARK_LABELING
3667 struct socket_smack *ssp = sock->sk->sk_security;
3668 struct smack_known *rsp;
3673 * Perfectly reasonable for this to be NULL
3678 switch (sock->sk->sk_family) {
3680 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3681 sip->sin_family != AF_INET)
3683 rc = smk_ipv4_check(sock->sk, sip);
3685 #if IS_ENABLED(CONFIG_IPV6)
3687 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3688 sap->sin6_family != AF_INET6)
3690 #ifdef SMACK_IPV6_SECMARK_LABELING
3691 rsp = smack_ipv6host_label(sap);
3693 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3696 #ifdef SMACK_IPV6_PORT_LABELING
3697 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3699 #endif /* IS_ENABLED(CONFIG_IPV6) */
3706 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3707 * @sap: netlabel secattr
3708 * @ssp: socket security information
3710 * Returns a pointer to a Smack label entry found on the label list.
3712 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3713 struct socket_smack *ssp)
3715 struct smack_known *skp;
3721 * Netlabel found it in the cache.
3723 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3724 return (struct smack_known *)sap->cache->data;
3726 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3728 * Looks like a fallback, which gives us a secid.
3730 return smack_from_secid(sap->attr.secid);
3732 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3734 * Looks like a CIPSO packet.
3735 * If there are flags but no level netlabel isn't
3736 * behaving the way we expect it to.
3738 * Look it up in the label table
3739 * Without guidance regarding the smack value
3740 * for the packet fall back on the network
3744 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3745 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3748 * Compare the catsets. Use the netlbl APIs.
3750 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3751 if ((skp->smk_netlabel.flags &
3752 NETLBL_SECATTR_MLS_CAT) == 0)
3756 for (acat = -1, kcat = -1; acat == kcat; ) {
3757 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3759 kcat = netlbl_catmap_walk(
3760 skp->smk_netlabel.attr.mls.cat,
3762 if (acat < 0 || kcat < 0)
3775 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3776 return &smack_known_web;
3777 return &smack_known_star;
3780 * Without guidance regarding the smack value
3781 * for the packet fall back on the network
3784 return smack_net_ambient;
3787 #if IS_ENABLED(CONFIG_IPV6)
3788 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3792 int proto = -EINVAL;
3793 struct ipv6hdr _ipv6h;
3794 struct ipv6hdr *ip6;
3796 struct tcphdr _tcph, *th;
3797 struct udphdr _udph, *uh;
3798 struct dccp_hdr _dccph, *dh;
3802 offset = skb_network_offset(skb);
3803 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3806 sip->sin6_addr = ip6->saddr;
3808 nexthdr = ip6->nexthdr;
3809 offset += sizeof(_ipv6h);
3810 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3817 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3819 sip->sin6_port = th->source;
3822 case IPPROTO_UDPLITE:
3823 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3825 sip->sin6_port = uh->source;
3828 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3830 sip->sin6_port = dh->dccph_sport;
3835 #endif /* CONFIG_IPV6 */
3838 * smack_from_skb - Smack data from the secmark in an skb
3841 * Returns smack_known of the secmark or NULL if that won't work.
3843 #ifdef CONFIG_NETWORK_SECMARK
3844 static struct smack_known *smack_from_skb(struct sk_buff *skb)
3846 if (skb == NULL || skb->secmark == 0)
3849 return smack_from_secid(skb->secmark);
3852 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3859 * smack_from_netlbl - Smack data from the IP options in an skb
3860 * @sk: socket data came in on
3861 * @family: address family
3864 * Find the Smack label in the IP options. If it hasn't been
3865 * added to the netlabel cache, add it here.
3867 * Returns smack_known of the IP options or NULL if that won't work.
3869 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
3870 struct sk_buff *skb)
3872 struct netlbl_lsm_secattr secattr;
3873 struct socket_smack *ssp = NULL;
3874 struct smack_known *skp = NULL;
3876 netlbl_secattr_init(&secattr);
3879 ssp = sk->sk_security;
3881 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
3882 skp = smack_from_secattr(&secattr, ssp);
3883 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
3884 netlbl_cache_add(skb, family, &skp->smk_netlabel);
3887 netlbl_secattr_destroy(&secattr);
3893 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3897 * Returns 0 if the packet should be delivered, an error code otherwise
3899 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3901 struct socket_smack *ssp = sk->sk_security;
3902 struct smack_known *skp = NULL;
3904 struct smk_audit_info ad;
3905 u16 family = sk->sk_family;
3907 struct lsm_network_audit net;
3909 #if IS_ENABLED(CONFIG_IPV6)
3910 struct sockaddr_in6 sadd;
3913 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3915 #endif /* CONFIG_IPV6 */
3920 * If there is a secmark use it rather than the CIPSO label.
3921 * If there is no secmark fall back to CIPSO.
3922 * The secmark is assumed to reflect policy better.
3924 skp = smack_from_skb(skb);
3926 skp = smack_from_netlbl(sk, family, skb);
3928 skp = smack_net_ambient;
3932 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3933 ad.a.u.net->family = family;
3934 ad.a.u.net->netif = skb->skb_iif;
3935 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3938 * Receiving a packet requires that the other end
3939 * be able to write here. Read access is not required.
3940 * This is the simplist possible security model
3943 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3944 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3947 netlbl_skbuff_err(skb, family, rc, 0);
3949 #if IS_ENABLED(CONFIG_IPV6)
3951 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3952 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3953 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3955 #ifdef SMACK_IPV6_SECMARK_LABELING
3956 skp = smack_from_skb(skb);
3958 if (smk_ipv6_localhost(&sadd))
3960 skp = smack_ipv6host_label(&sadd);
3962 skp = smack_net_ambient;
3965 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3966 ad.a.u.net->family = family;
3967 ad.a.u.net->netif = skb->skb_iif;
3968 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3969 #endif /* CONFIG_AUDIT */
3970 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3971 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3973 #endif /* SMACK_IPV6_SECMARK_LABELING */
3974 #ifdef SMACK_IPV6_PORT_LABELING
3975 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3976 #endif /* SMACK_IPV6_PORT_LABELING */
3978 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3979 ICMPV6_ADM_PROHIBITED, 0);
3981 #endif /* CONFIG_IPV6 */
3988 * smack_socket_getpeersec_stream - pull in packet label
3990 * @optval: user's destination
3991 * @optlen: size thereof
3994 * returns zero on success, an error code otherwise
3996 static int smack_socket_getpeersec_stream(struct socket *sock,
3997 char __user *optval,
3998 int __user *optlen, unsigned len)
4000 struct socket_smack *ssp;
4005 ssp = sock->sk->sk_security;
4006 if (ssp->smk_packet != NULL) {
4007 rcp = ssp->smk_packet->smk_known;
4008 slen = strlen(rcp) + 1;
4013 else if (copy_to_user(optval, rcp, slen) != 0)
4016 if (put_user(slen, optlen) != 0)
4024 * smack_socket_getpeersec_dgram - pull in packet label
4025 * @sock: the peer socket
4027 * @secid: pointer to where to put the secid of the packet
4029 * Sets the netlabel socket state on sk from parent
4031 static int smack_socket_getpeersec_dgram(struct socket *sock,
4032 struct sk_buff *skb, u32 *secid)
4035 struct socket_smack *ssp = NULL;
4036 struct smack_known *skp;
4037 struct sock *sk = NULL;
4038 int family = PF_UNSPEC;
4039 u32 s = 0; /* 0 is the invalid secid */
4042 if (skb->protocol == htons(ETH_P_IP))
4044 #if IS_ENABLED(CONFIG_IPV6)
4045 else if (skb->protocol == htons(ETH_P_IPV6))
4047 #endif /* CONFIG_IPV6 */
4049 if (family == PF_UNSPEC && sock != NULL)
4050 family = sock->sk->sk_family;
4054 ssp = sock->sk->sk_security;
4055 s = ssp->smk_out->smk_secid;
4058 skp = smack_from_skb(skb);
4064 * Translate what netlabel gave us.
4068 skp = smack_from_netlbl(sk, family, skb);
4073 #ifdef SMACK_IPV6_SECMARK_LABELING
4074 skp = smack_from_skb(skb);
4087 * smack_sock_graft - Initialize a newly created socket with an existing sock
4089 * @parent: parent socket
4091 * Set the smk_{in,out} state of an existing sock based on the process that
4092 * is creating the new socket.
4094 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4096 struct socket_smack *ssp;
4097 struct smack_known *skp = smk_of_current();
4100 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4103 ssp = sk->sk_security;
4106 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4110 * smack_inet_conn_request - Smack access check on connect
4111 * @sk: socket involved
4115 * Returns 0 if a task with the packet label could write to
4116 * the socket, otherwise an error code
4118 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4119 struct request_sock *req)
4121 u16 family = sk->sk_family;
4122 struct smack_known *skp;
4123 struct socket_smack *ssp = sk->sk_security;
4124 struct sockaddr_in addr;
4126 struct smack_known *hskp;
4128 struct smk_audit_info ad;
4130 struct lsm_network_audit net;
4133 #if IS_ENABLED(CONFIG_IPV6)
4134 if (family == PF_INET6) {
4136 * Handle mapped IPv4 packets arriving
4137 * via IPv6 sockets. Don't set up netlabel
4138 * processing on IPv6.
4140 if (skb->protocol == htons(ETH_P_IP))
4145 #endif /* CONFIG_IPV6 */
4148 * If there is a secmark use it rather than the CIPSO label.
4149 * If there is no secmark fall back to CIPSO.
4150 * The secmark is assumed to reflect policy better.
4152 skp = smack_from_skb(skb);
4154 skp = smack_from_netlbl(sk, family, skb);
4156 skp = &smack_known_huh;
4160 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4161 ad.a.u.net->family = family;
4162 ad.a.u.net->netif = skb->skb_iif;
4163 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4166 * Receiving a packet requires that the other end be able to write
4167 * here. Read access is not required.
4169 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4170 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4175 * Save the peer's label in the request_sock so we can later setup
4176 * smk_packet in the child socket so that SO_PEERCRED can report it.
4178 req->peer_secid = skp->smk_secid;
4181 * We need to decide if we want to label the incoming connection here
4182 * if we do we only need to label the request_sock and the stack will
4183 * propagate the wire-label to the sock when it is created.
4186 addr.sin_addr.s_addr = hdr->saddr;
4188 hskp = smack_ipv4host_label(&addr);
4192 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4194 netlbl_req_delattr(req);
4200 * smack_inet_csk_clone - Copy the connection information to the new socket
4201 * @sk: the new socket
4202 * @req: the connection's request_sock
4204 * Transfer the connection's peer label to the newly created socket.
4206 static void smack_inet_csk_clone(struct sock *sk,
4207 const struct request_sock *req)
4209 struct socket_smack *ssp = sk->sk_security;
4210 struct smack_known *skp;
4212 if (req->peer_secid != 0) {
4213 skp = smack_from_secid(req->peer_secid);
4214 ssp->smk_packet = skp;
4216 ssp->smk_packet = NULL;
4220 * Key management security hooks
4222 * Casey has not tested key support very heavily.
4223 * The permission check is most likely too restrictive.
4224 * If you care about keys please have a look.
4229 * smack_key_alloc - Set the key security blob
4231 * @cred: the credentials to use
4234 * No allocation required
4238 static int smack_key_alloc(struct key *key, const struct cred *cred,
4239 unsigned long flags)
4241 struct smack_known *skp = smk_of_task(smack_cred(cred));
4243 key->security = skp;
4248 * smack_key_free - Clear the key security blob
4251 * Clear the blob pointer
4253 static void smack_key_free(struct key *key)
4255 key->security = NULL;
4259 * smack_key_permission - Smack access on a key
4260 * @key_ref: gets to the object
4261 * @cred: the credentials to use
4262 * @need_perm: requested key permission
4264 * Return 0 if the task has read and write to the object,
4265 * an error code otherwise
4267 static int smack_key_permission(key_ref_t key_ref,
4268 const struct cred *cred,
4269 enum key_need_perm need_perm)
4272 struct smk_audit_info ad;
4273 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4278 * Validate requested permissions
4280 switch (need_perm) {
4282 case KEY_NEED_SEARCH:
4284 request |= MAY_READ;
4286 case KEY_NEED_WRITE:
4288 case KEY_NEED_SETATTR:
4289 request |= MAY_WRITE;
4291 case KEY_NEED_UNSPECIFIED:
4292 case KEY_NEED_UNLINK:
4293 case KEY_SYSADMIN_OVERRIDE:
4294 case KEY_AUTHTOKEN_OVERRIDE:
4295 case KEY_DEFER_PERM_CHECK:
4301 keyp = key_ref_to_ptr(key_ref);
4305 * If the key hasn't been initialized give it access so that
4308 if (keyp->security == NULL)
4311 * This should not occur
4316 if (smack_privileged(CAP_MAC_OVERRIDE))
4320 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4321 ad.a.u.key_struct.key = keyp->serial;
4322 ad.a.u.key_struct.key_desc = keyp->description;
4324 rc = smk_access(tkp, keyp->security, request, &ad);
4325 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4330 * smack_key_getsecurity - Smack label tagging the key
4331 * @key points to the key to be queried
4332 * @_buffer points to a pointer that should be set to point to the
4333 * resulting string (if no label or an error occurs).
4334 * Return the length of the string (including terminating NUL) or -ve if
4336 * May also return 0 (and a NULL buffer pointer) if there is no label.
4338 static int smack_key_getsecurity(struct key *key, char **_buffer)
4340 struct smack_known *skp = key->security;
4344 if (key->security == NULL) {
4349 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4352 length = strlen(copy) + 1;
4359 #ifdef CONFIG_KEY_NOTIFICATIONS
4361 * smack_watch_key - Smack access to watch a key for notifications.
4362 * @key: The key to be watched
4364 * Return 0 if the @watch->cred has permission to read from the key object and
4365 * an error otherwise.
4367 static int smack_watch_key(struct key *key)
4369 struct smk_audit_info ad;
4370 struct smack_known *tkp = smk_of_current();
4376 * If the key hasn't been initialized give it access so that
4379 if (key->security == NULL)
4382 * This should not occur
4387 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4391 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4392 ad.a.u.key_struct.key = key->serial;
4393 ad.a.u.key_struct.key_desc = key->description;
4395 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4396 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4399 #endif /* CONFIG_KEY_NOTIFICATIONS */
4400 #endif /* CONFIG_KEYS */
4402 #ifdef CONFIG_WATCH_QUEUE
4404 * smack_post_notification - Smack access to post a notification to a queue
4405 * @w_cred: The credentials of the watcher.
4406 * @cred: The credentials of the event source (may be NULL).
4407 * @n: The notification message to be posted.
4409 static int smack_post_notification(const struct cred *w_cred,
4410 const struct cred *cred,
4411 struct watch_notification *n)
4413 struct smk_audit_info ad;
4414 struct smack_known *subj, *obj;
4417 /* Always let maintenance notifications through. */
4418 if (n->type == WATCH_TYPE_META)
4423 subj = smk_of_task(smack_cred(cred));
4424 obj = smk_of_task(smack_cred(w_cred));
4426 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4427 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4428 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4431 #endif /* CONFIG_WATCH_QUEUE */
4436 * Audit requires a unique representation of each Smack specific
4437 * rule. This unique representation is used to distinguish the
4438 * object to be audited from remaining kernel objects and also
4439 * works as a glue between the audit hooks.
4441 * Since repository entries are added but never deleted, we'll use
4442 * the smack_known label address related to the given audit rule as
4443 * the needed unique representation. This also better fits the smack
4444 * model where nearly everything is a label.
4449 * smack_audit_rule_init - Initialize a smack audit rule
4450 * @field: audit rule fields given from user-space (audit.h)
4451 * @op: required testing operator (=, !=, >, <, ...)
4452 * @rulestr: smack label to be audited
4453 * @vrule: pointer to save our own audit rule representation
4455 * Prepare to audit cases where (@field @op @rulestr) is true.
4456 * The label to be audited is created if necessay.
4458 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4460 struct smack_known *skp;
4461 char **rule = (char **)vrule;
4464 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4467 if (op != Audit_equal && op != Audit_not_equal)
4470 skp = smk_import_entry(rulestr, 0);
4472 return PTR_ERR(skp);
4474 *rule = skp->smk_known;
4480 * smack_audit_rule_known - Distinguish Smack audit rules
4481 * @krule: rule of interest, in Audit kernel representation format
4483 * This is used to filter Smack rules from remaining Audit ones.
4484 * If it's proved that this rule belongs to us, the
4485 * audit_rule_match hook will be called to do the final judgement.
4487 static int smack_audit_rule_known(struct audit_krule *krule)
4489 struct audit_field *f;
4492 for (i = 0; i < krule->field_count; i++) {
4493 f = &krule->fields[i];
4495 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4503 * smack_audit_rule_match - Audit given object ?
4504 * @secid: security id for identifying the object to test
4505 * @field: audit rule flags given from user-space
4506 * @op: required testing operator
4507 * @vrule: smack internal rule presentation
4509 * The core Audit hook. It's used to take the decision of
4510 * whether to audit or not to audit a given object.
4512 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4514 struct smack_known *skp;
4517 if (unlikely(!rule)) {
4518 WARN_ONCE(1, "Smack: missing rule\n");
4522 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4525 skp = smack_from_secid(secid);
4528 * No need to do string comparisons. If a match occurs,
4529 * both pointers will point to the same smack_known
4532 if (op == Audit_equal)
4533 return (rule == skp->smk_known);
4534 if (op == Audit_not_equal)
4535 return (rule != skp->smk_known);
4541 * There is no need for a smack_audit_rule_free hook.
4542 * No memory was allocated.
4545 #endif /* CONFIG_AUDIT */
4548 * smack_ismaclabel - check if xattr @name references a smack MAC label
4549 * @name: Full xattr name to check.
4551 static int smack_ismaclabel(const char *name)
4553 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4558 * smack_secid_to_secctx - return the smack label for a secid
4559 * @secid: incoming integer
4560 * @secdata: destination
4561 * @seclen: how long it is
4563 * Exists for networking code.
4565 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4567 struct smack_known *skp = smack_from_secid(secid);
4570 *secdata = skp->smk_known;
4571 *seclen = strlen(skp->smk_known);
4576 * smack_secctx_to_secid - return the secid for a smack label
4577 * @secdata: smack label
4578 * @seclen: how long result is
4579 * @secid: outgoing integer
4581 * Exists for audit and networking code.
4583 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4585 struct smack_known *skp = smk_find_entry(secdata);
4588 *secid = skp->smk_secid;
4595 * There used to be a smack_release_secctx hook
4596 * that did nothing back when hooks were in a vector.
4597 * Now that there's a list such a hook adds cost.
4600 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4602 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4606 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4608 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK,
4612 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4614 struct smack_known *skp = smk_of_inode(inode);
4616 *ctx = skp->smk_known;
4617 *ctxlen = strlen(skp->smk_known);
4621 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4624 struct task_smack *tsp;
4625 struct smack_known *skp;
4626 struct inode_smack *isp;
4627 struct cred *new_creds = *new;
4629 if (new_creds == NULL) {
4630 new_creds = prepare_creds();
4631 if (new_creds == NULL)
4635 tsp = smack_cred(new_creds);
4638 * Get label from overlay inode and set it in create_sid
4640 isp = smack_inode(d_inode(dentry->d_parent));
4641 skp = isp->smk_inode;
4642 tsp->smk_task = skp;
4647 static int smack_inode_copy_up_xattr(const char *name)
4650 * Return 1 if this is the smack access Smack attribute.
4652 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4658 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4660 const struct cred *old,
4663 struct task_smack *otsp = smack_cred(old);
4664 struct task_smack *ntsp = smack_cred(new);
4665 struct inode_smack *isp;
4669 * Use the process credential unless all of
4670 * the transmuting criteria are met
4672 ntsp->smk_task = otsp->smk_task;
4675 * the attribute of the containing directory
4677 isp = smack_inode(d_inode(dentry->d_parent));
4679 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4681 may = smk_access_entry(otsp->smk_task->smk_known,
4682 isp->smk_inode->smk_known,
4683 &otsp->smk_task->smk_rules);
4687 * If the directory is transmuting and the rule
4688 * providing access is transmuting use the containing
4689 * directory label instead of the process label.
4691 if (may > 0 && (may & MAY_TRANSMUTE))
4692 ntsp->smk_task = isp->smk_inode;
4697 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4698 .lbs_cred = sizeof(struct task_smack),
4699 .lbs_file = sizeof(struct smack_known *),
4700 .lbs_inode = sizeof(struct inode_smack),
4701 .lbs_ipc = sizeof(struct smack_known *),
4702 .lbs_msg_msg = sizeof(struct smack_known *),
4703 .lbs_superblock = sizeof(struct superblock_smack),
4706 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4707 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4708 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4709 LSM_HOOK_INIT(syslog, smack_syslog),
4711 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4712 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4714 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4715 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4716 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4717 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4718 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4720 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
4722 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4723 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4724 LSM_HOOK_INIT(inode_link, smack_inode_link),
4725 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4726 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4727 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4728 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4729 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4730 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4731 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4732 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4733 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4734 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4735 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4736 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4737 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4738 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4740 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4741 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4742 LSM_HOOK_INIT(file_lock, smack_file_lock),
4743 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4744 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4745 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4746 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4747 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4748 LSM_HOOK_INIT(file_receive, smack_file_receive),
4750 LSM_HOOK_INIT(file_open, smack_file_open),
4752 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4753 LSM_HOOK_INIT(cred_free, smack_cred_free),
4754 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4755 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4756 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4757 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4758 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4759 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4760 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4761 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4762 LSM_HOOK_INIT(task_getsecid_subj, smack_task_getsecid_subj),
4763 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
4764 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4765 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4766 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4767 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4768 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4769 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4770 LSM_HOOK_INIT(task_kill, smack_task_kill),
4771 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4773 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4774 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4776 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4778 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4779 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4780 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4781 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4782 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4784 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4785 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4786 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4787 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4789 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4790 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4791 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4792 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4794 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4796 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4797 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4799 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4800 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4802 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4803 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4804 #ifdef SMACK_IPV6_PORT_LABELING
4805 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4807 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4808 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4809 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4810 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4811 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4812 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4813 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4814 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4815 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4816 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4818 /* key management security hooks */
4820 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4821 LSM_HOOK_INIT(key_free, smack_key_free),
4822 LSM_HOOK_INIT(key_permission, smack_key_permission),
4823 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4824 #ifdef CONFIG_KEY_NOTIFICATIONS
4825 LSM_HOOK_INIT(watch_key, smack_watch_key),
4827 #endif /* CONFIG_KEYS */
4829 #ifdef CONFIG_WATCH_QUEUE
4830 LSM_HOOK_INIT(post_notification, smack_post_notification),
4835 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4836 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4837 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4838 #endif /* CONFIG_AUDIT */
4840 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4841 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4842 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4843 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4844 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4845 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4846 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4847 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4848 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4852 static __init void init_smack_known_list(void)
4855 * Initialize rule list locks
4857 mutex_init(&smack_known_huh.smk_rules_lock);
4858 mutex_init(&smack_known_hat.smk_rules_lock);
4859 mutex_init(&smack_known_floor.smk_rules_lock);
4860 mutex_init(&smack_known_star.smk_rules_lock);
4861 mutex_init(&smack_known_web.smk_rules_lock);
4863 * Initialize rule lists
4865 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4866 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4867 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4868 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4869 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4871 * Create the known labels list
4873 smk_insert_entry(&smack_known_huh);
4874 smk_insert_entry(&smack_known_hat);
4875 smk_insert_entry(&smack_known_star);
4876 smk_insert_entry(&smack_known_floor);
4877 smk_insert_entry(&smack_known_web);
4881 * smack_init - initialize the smack system
4883 * Returns 0 on success, -ENOMEM is there's no memory
4885 static __init int smack_init(void)
4887 struct cred *cred = (struct cred *) current->cred;
4888 struct task_smack *tsp;
4890 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4891 if (!smack_rule_cache)
4895 * Set the security state for the initial task.
4897 tsp = smack_cred(cred);
4898 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4903 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4906 pr_info("Smack: Initializing.\n");
4907 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4908 pr_info("Smack: Netfilter enabled.\n");
4910 #ifdef SMACK_IPV6_PORT_LABELING
4911 pr_info("Smack: IPv6 port labeling enabled.\n");
4913 #ifdef SMACK_IPV6_SECMARK_LABELING
4914 pr_info("Smack: IPv6 Netfilter enabled.\n");
4917 /* initialize the smack_known_list */
4918 init_smack_known_list();
4924 * Smack requires early initialization in order to label
4925 * all processes and objects when they are created.
4927 DEFINE_LSM(smack) = {
4929 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4930 .blobs = &smack_blob_sizes,