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 <linux/pipe_fs_i.h>
32 #include <net/cipso_ipv4.h>
35 #include <linux/audit.h>
36 #include <linux/magic.h>
37 #include <linux/dcache.h>
38 #include <linux/personality.h>
39 #include <linux/msg.h>
40 #include <linux/shm.h>
41 #include <linux/binfmts.h>
42 #include <linux/parser.h>
43 #include <linux/fs_context.h>
44 #include <linux/fs_parser.h>
47 #define TRANS_TRUE "TRUE"
48 #define TRANS_TRUE_SIZE 4
50 #define SMK_CONNECTING 0
51 #define SMK_RECEIVING 1
54 static DEFINE_MUTEX(smack_ipv6_lock);
55 static LIST_HEAD(smk_ipv6_port_list);
56 static struct kmem_cache *smack_inode_cache;
57 struct kmem_cache *smack_rule_cache;
60 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
65 } smk_mount_opts[] = {
66 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
67 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
71 static int match_opt_prefix(char *s, int l, char **arg)
75 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
76 size_t len = smk_mount_opts[i].len;
77 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
79 if (len == l || s[len] != '=')
82 return smk_mount_opts[i].opt;
87 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
88 static char *smk_bu_mess[] = {
89 "Bringup Error", /* Unused */
90 "Bringup", /* SMACK_BRINGUP_ALLOW */
91 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
92 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
95 static void smk_bu_mode(int mode, char *s)
101 if (mode & MAY_WRITE)
105 if (mode & MAY_APPEND)
107 if (mode & MAY_TRANSMUTE)
117 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
118 static int smk_bu_note(char *note, struct smack_known *sskp,
119 struct smack_known *oskp, int mode, int rc)
121 char acc[SMK_NUM_ACCESS_TYPE + 1];
125 if (rc > SMACK_UNCONFINED_OBJECT)
128 smk_bu_mode(mode, acc);
129 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
130 sskp->smk_known, oskp->smk_known, acc, note);
134 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
137 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
138 static int smk_bu_current(char *note, struct smack_known *oskp,
141 struct task_smack *tsp = smack_cred(current_cred());
142 char acc[SMK_NUM_ACCESS_TYPE + 1];
146 if (rc > SMACK_UNCONFINED_OBJECT)
149 smk_bu_mode(mode, acc);
150 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
151 tsp->smk_task->smk_known, oskp->smk_known,
152 acc, current->comm, note);
156 #define smk_bu_current(note, oskp, mode, RC) (RC)
159 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
160 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
162 struct task_smack *tsp = smack_cred(current_cred());
163 struct smack_known *smk_task = smk_of_task_struct(otp);
164 char acc[SMK_NUM_ACCESS_TYPE + 1];
168 if (rc > SMACK_UNCONFINED_OBJECT)
171 smk_bu_mode(mode, acc);
172 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
173 tsp->smk_task->smk_known, smk_task->smk_known, acc,
174 current->comm, otp->comm);
178 #define smk_bu_task(otp, mode, RC) (RC)
181 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
182 static int smk_bu_inode(struct inode *inode, int mode, int rc)
184 struct task_smack *tsp = smack_cred(current_cred());
185 struct inode_smack *isp = smack_inode(inode);
186 char acc[SMK_NUM_ACCESS_TYPE + 1];
188 if (isp->smk_flags & SMK_INODE_IMPURE)
189 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
190 inode->i_sb->s_id, inode->i_ino, current->comm);
194 if (rc > SMACK_UNCONFINED_OBJECT)
196 if (rc == SMACK_UNCONFINED_SUBJECT &&
197 (mode & (MAY_WRITE | MAY_APPEND)))
198 isp->smk_flags |= SMK_INODE_IMPURE;
200 smk_bu_mode(mode, acc);
202 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
203 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
204 inode->i_sb->s_id, inode->i_ino, current->comm);
208 #define smk_bu_inode(inode, mode, RC) (RC)
211 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
212 static int smk_bu_file(struct file *file, int mode, int rc)
214 struct task_smack *tsp = smack_cred(current_cred());
215 struct smack_known *sskp = tsp->smk_task;
216 struct inode *inode = file_inode(file);
217 struct inode_smack *isp = smack_inode(inode);
218 char acc[SMK_NUM_ACCESS_TYPE + 1];
220 if (isp->smk_flags & SMK_INODE_IMPURE)
221 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
222 inode->i_sb->s_id, inode->i_ino, current->comm);
226 if (rc > SMACK_UNCONFINED_OBJECT)
229 smk_bu_mode(mode, acc);
230 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
231 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
232 inode->i_sb->s_id, inode->i_ino, file,
237 #define smk_bu_file(file, mode, RC) (RC)
240 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
241 static int smk_bu_credfile(const struct cred *cred, struct file *file,
244 struct task_smack *tsp = smack_cred(cred);
245 struct smack_known *sskp = tsp->smk_task;
246 struct inode *inode = file_inode(file);
247 struct inode_smack *isp = smack_inode(inode);
248 char acc[SMK_NUM_ACCESS_TYPE + 1];
250 if (isp->smk_flags & SMK_INODE_IMPURE)
251 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
252 inode->i_sb->s_id, inode->i_ino, current->comm);
256 if (rc > SMACK_UNCONFINED_OBJECT)
259 smk_bu_mode(mode, acc);
260 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
261 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
262 inode->i_sb->s_id, inode->i_ino, file,
267 #define smk_bu_credfile(cred, file, mode, RC) (RC)
271 * smk_fetch - Fetch the smack label from a file.
272 * @name: type of the label (attribute)
273 * @ip: a pointer to the inode
274 * @dp: a pointer to the dentry
276 * Returns a pointer to the master list entry for the Smack label,
277 * NULL if there was no label to fetch, or an error code.
279 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
284 struct smack_known *skp = NULL;
286 if (!(ip->i_opflags & IOP_XATTR))
287 return ERR_PTR(-EOPNOTSUPP);
289 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
291 return ERR_PTR(-ENOMEM);
293 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
299 skp = smk_import_entry(buffer, rc);
307 * init_inode_smack - initialize an inode security blob
308 * @inode: inode to extract the info from
309 * @skp: a pointer to the Smack label entry to use in the blob
312 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
314 struct inode_smack *isp = smack_inode(inode);
316 isp->smk_inode = skp;
318 mutex_init(&isp->smk_lock);
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(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;
542 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
547 sbsp->smk_root = &smack_known_floor;
548 sbsp->smk_default = &smack_known_floor;
549 sbsp->smk_floor = &smack_known_floor;
550 sbsp->smk_hat = &smack_known_hat;
552 * SMK_SB_INITIALIZED will be zero from kzalloc.
554 sb->s_security = sbsp;
560 * smack_sb_free_security - free a superblock blob
561 * @sb: the superblock getting the blob
564 static void smack_sb_free_security(struct super_block *sb)
566 kfree(sb->s_security);
567 sb->s_security = NULL;
570 struct smack_mnt_opts {
571 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
574 static void smack_free_mnt_opts(void *mnt_opts)
576 struct smack_mnt_opts *opts = mnt_opts;
577 kfree(opts->fsdefault);
578 kfree(opts->fsfloor);
581 kfree(opts->fstransmute);
585 static int smack_add_opt(int token, const char *s, void **mnt_opts)
587 struct smack_mnt_opts *opts = *mnt_opts;
590 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
619 case Opt_fstransmute:
620 if (opts->fstransmute)
622 opts->fstransmute = s;
628 pr_warn("Smack: duplicate mount options\n");
633 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
634 * @fc: The new filesystem context.
635 * @src_fc: The source filesystem context being duplicated.
637 * Returns 0 on success or -ENOMEM on error.
639 static int smack_fs_context_dup(struct fs_context *fc,
640 struct fs_context *src_fc)
642 struct smack_mnt_opts *dst, *src = src_fc->security;
647 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
652 if (src->fsdefault) {
653 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
658 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
663 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
668 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
672 if (src->fstransmute) {
673 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
674 if (!dst->fstransmute)
680 static const struct fs_parameter_spec smack_param_specs[] = {
681 fsparam_string("smackfsdef", Opt_fsdefault),
682 fsparam_string("smackfsdefault", Opt_fsdefault),
683 fsparam_string("smackfsfloor", Opt_fsfloor),
684 fsparam_string("smackfshat", Opt_fshat),
685 fsparam_string("smackfsroot", Opt_fsroot),
686 fsparam_string("smackfstransmute", Opt_fstransmute),
690 static const struct fs_parameter_description smack_fs_parameters = {
692 .specs = smack_param_specs,
696 * smack_fs_context_parse_param - Parse a single mount parameter
697 * @fc: The new filesystem context being constructed.
698 * @param: The parameter.
700 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
703 static int smack_fs_context_parse_param(struct fs_context *fc,
704 struct fs_parameter *param)
706 struct fs_parse_result result;
709 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
713 rc = smack_add_opt(opt, param->string, &fc->security);
715 param->string = NULL;
719 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
721 char *from = options, *to = options;
725 char *next = strchr(from, ',');
734 token = match_opt_prefix(from, len, &arg);
735 if (token != Opt_error) {
736 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
737 rc = smack_add_opt(token, arg, mnt_opts);
741 smack_free_mnt_opts(*mnt_opts);
746 if (!first) { // copy with preceding comma
751 memmove(to, from, len);
764 * smack_set_mnt_opts - set Smack specific mount options
765 * @sb: the file system superblock
766 * @mnt_opts: Smack mount options
767 * @kern_flags: mount option from kernel space or user space
768 * @set_kern_flags: where to store converted mount opts
770 * Returns 0 on success, an error code on failure
772 * Allow filesystems with binary mount data to explicitly set Smack mount
775 static int smack_set_mnt_opts(struct super_block *sb,
777 unsigned long kern_flags,
778 unsigned long *set_kern_flags)
780 struct dentry *root = sb->s_root;
781 struct inode *inode = d_backing_inode(root);
782 struct superblock_smack *sp = sb->s_security;
783 struct inode_smack *isp;
784 struct smack_known *skp;
785 struct smack_mnt_opts *opts = mnt_opts;
786 bool transmute = false;
788 if (sp->smk_flags & SMK_SB_INITIALIZED)
791 if (inode->i_security == NULL) {
792 int rc = lsm_inode_alloc(inode);
798 if (!smack_privileged(CAP_MAC_ADMIN)) {
800 * Unprivileged mounts don't get to specify Smack values.
805 * Unprivileged mounts get root and default from the caller.
807 skp = smk_of_current();
809 sp->smk_default = skp;
811 * For a handful of fs types with no user-controlled
812 * backing store it's okay to trust security labels
813 * in the filesystem. The rest are untrusted.
815 if (sb->s_user_ns != &init_user_ns &&
816 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
817 sb->s_magic != RAMFS_MAGIC) {
819 sp->smk_flags |= SMK_SB_UNTRUSTED;
823 sp->smk_flags |= SMK_SB_INITIALIZED;
826 if (opts->fsdefault) {
827 skp = smk_import_entry(opts->fsdefault, 0);
830 sp->smk_default = skp;
833 skp = smk_import_entry(opts->fsfloor, 0);
839 skp = smk_import_entry(opts->fshat, 0);
845 skp = smk_import_entry(opts->fsroot, 0);
850 if (opts->fstransmute) {
851 skp = smk_import_entry(opts->fstransmute, 0);
860 * Initialize the root inode.
862 init_inode_smack(inode, sp->smk_root);
865 isp = smack_inode(inode);
866 isp->smk_flags |= SMK_INODE_TRANSMUTE;
873 * smack_sb_statfs - Smack check on statfs
874 * @dentry: identifies the file system in question
876 * Returns 0 if current can read the floor of the filesystem,
877 * and error code otherwise
879 static int smack_sb_statfs(struct dentry *dentry)
881 struct superblock_smack *sbp = dentry->d_sb->s_security;
883 struct smk_audit_info ad;
885 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
886 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
888 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
889 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
898 * smack_bprm_set_creds - set creds for exec
899 * @bprm: the exec information
901 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
903 static int smack_bprm_set_creds(struct linux_binprm *bprm)
905 struct inode *inode = file_inode(bprm->file);
906 struct task_smack *bsp = smack_cred(bprm->cred);
907 struct inode_smack *isp;
908 struct superblock_smack *sbsp;
911 if (bprm->called_set_creds)
914 isp = smack_inode(inode);
915 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
918 sbsp = inode->i_sb->s_security;
919 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
920 isp->smk_task != sbsp->smk_root)
923 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
924 struct task_struct *tracer;
928 tracer = ptrace_parent(current);
929 if (likely(tracer != NULL))
930 rc = smk_ptrace_rule_check(tracer,
939 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
942 bsp->smk_task = isp->smk_task;
943 bprm->per_clear |= PER_CLEAR_ON_SETID;
945 /* Decide if this is a secure exec. */
946 if (bsp->smk_task != bsp->smk_forked)
947 bprm->secureexec = 1;
957 * smack_inode_alloc_security - allocate an inode blob
958 * @inode: the inode in need of a blob
962 static int smack_inode_alloc_security(struct inode *inode)
964 struct smack_known *skp = smk_of_current();
966 init_inode_smack(inode, skp);
971 * smack_inode_init_security - copy out the smack from an inode
972 * @inode: the newly created inode
973 * @dir: containing directory object
975 * @name: where to put the attribute name
976 * @value: where to put the attribute value
977 * @len: where to put the length of the attribute
979 * Returns 0 if it all works out, -ENOMEM if there's no memory
981 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
982 const struct qstr *qstr, const char **name,
983 void **value, size_t *len)
985 struct inode_smack *issp = smack_inode(inode);
986 struct smack_known *skp = smk_of_current();
987 struct smack_known *isp = smk_of_inode(inode);
988 struct smack_known *dsp = smk_of_inode(dir);
992 *name = XATTR_SMACK_SUFFIX;
996 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1001 * If the access rule allows transmutation and
1002 * the directory requests transmutation then
1003 * by all means transmute.
1004 * Mark the inode as changed.
1006 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1007 smk_inode_transmutable(dir)) {
1009 issp->smk_flags |= SMK_INODE_CHANGED;
1012 *value = kstrdup(isp->smk_known, GFP_NOFS);
1016 *len = strlen(isp->smk_known);
1023 * smack_inode_link - Smack check on link
1024 * @old_dentry: the existing object
1026 * @new_dentry: the new object
1028 * Returns 0 if access is permitted, an error code otherwise
1030 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1031 struct dentry *new_dentry)
1033 struct smack_known *isp;
1034 struct smk_audit_info ad;
1037 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1038 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1040 isp = smk_of_inode(d_backing_inode(old_dentry));
1041 rc = smk_curacc(isp, MAY_WRITE, &ad);
1042 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1044 if (rc == 0 && d_is_positive(new_dentry)) {
1045 isp = smk_of_inode(d_backing_inode(new_dentry));
1046 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1047 rc = smk_curacc(isp, MAY_WRITE, &ad);
1048 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1055 * smack_inode_unlink - Smack check on inode deletion
1056 * @dir: containing directory object
1057 * @dentry: file to unlink
1059 * Returns 0 if current can write the containing directory
1060 * and the object, error code otherwise
1062 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1064 struct inode *ip = d_backing_inode(dentry);
1065 struct smk_audit_info ad;
1068 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1069 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1072 * You need write access to the thing you're unlinking
1074 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1075 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1078 * You also need write access to the containing directory
1080 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1081 smk_ad_setfield_u_fs_inode(&ad, dir);
1082 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1083 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1089 * smack_inode_rmdir - Smack check on directory deletion
1090 * @dir: containing directory object
1091 * @dentry: directory to unlink
1093 * Returns 0 if current can write the containing directory
1094 * and the directory, error code otherwise
1096 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1098 struct smk_audit_info ad;
1101 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1102 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1105 * You need write access to the thing you're removing
1107 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1108 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1111 * You also need write access to the containing directory
1113 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1114 smk_ad_setfield_u_fs_inode(&ad, dir);
1115 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1116 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1123 * smack_inode_rename - Smack check on rename
1124 * @old_inode: unused
1125 * @old_dentry: the old object
1126 * @new_inode: unused
1127 * @new_dentry: the new object
1129 * Read and write access is required on both the old and
1132 * Returns 0 if access is permitted, an error code otherwise
1134 static int smack_inode_rename(struct inode *old_inode,
1135 struct dentry *old_dentry,
1136 struct inode *new_inode,
1137 struct dentry *new_dentry)
1140 struct smack_known *isp;
1141 struct smk_audit_info ad;
1143 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1144 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1146 isp = smk_of_inode(d_backing_inode(old_dentry));
1147 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1148 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1150 if (rc == 0 && d_is_positive(new_dentry)) {
1151 isp = smk_of_inode(d_backing_inode(new_dentry));
1152 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1153 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1154 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1160 * smack_inode_permission - Smack version of permission()
1161 * @inode: the inode in question
1162 * @mask: the access requested
1164 * This is the important Smack hook.
1166 * Returns 0 if access is permitted, an error code otherwise
1168 static int smack_inode_permission(struct inode *inode, int mask)
1170 struct superblock_smack *sbsp = inode->i_sb->s_security;
1171 struct smk_audit_info ad;
1172 int no_block = mask & MAY_NOT_BLOCK;
1175 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1177 * No permission to check. Existence test. Yup, it's there.
1182 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1183 if (smk_of_inode(inode) != sbsp->smk_root)
1187 /* May be droppable after audit */
1190 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1191 smk_ad_setfield_u_fs_inode(&ad, inode);
1192 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1193 rc = smk_bu_inode(inode, mask, rc);
1198 * smack_inode_setattr - Smack check for setting attributes
1199 * @dentry: the object
1200 * @iattr: for the force flag
1202 * Returns 0 if access is permitted, an error code otherwise
1204 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1206 struct smk_audit_info ad;
1210 * Need to allow for clearing the setuid bit.
1212 if (iattr->ia_valid & ATTR_FORCE)
1214 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1215 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1217 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1218 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1223 * smack_inode_getattr - Smack check for getting attributes
1224 * @path: path to extract the info from
1226 * Returns 0 if access is permitted, an error code otherwise
1228 static int smack_inode_getattr(const struct path *path)
1230 struct smk_audit_info ad;
1231 struct inode *inode = d_backing_inode(path->dentry);
1234 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1235 smk_ad_setfield_u_fs_path(&ad, *path);
1236 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1237 rc = smk_bu_inode(inode, MAY_READ, rc);
1242 * smack_inode_setxattr - Smack check for setting xattrs
1243 * @dentry: the object
1244 * @name: name of the attribute
1245 * @value: value of the attribute
1246 * @size: size of the value
1249 * This protects the Smack attribute explicitly.
1251 * Returns 0 if access is permitted, an error code otherwise
1253 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1254 const void *value, size_t size, int flags)
1256 struct smk_audit_info ad;
1257 struct smack_known *skp;
1259 int check_import = 0;
1264 * Check label validity here so import won't fail in post_setxattr
1266 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1267 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1268 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1271 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1272 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1276 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1278 if (size != TRANS_TRUE_SIZE ||
1279 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1282 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1284 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1287 if (rc == 0 && check_import) {
1288 skp = size ? smk_import_entry(value, size) : NULL;
1291 else if (skp == NULL || (check_star &&
1292 (skp == &smack_known_star || skp == &smack_known_web)))
1296 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1297 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1300 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1301 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1308 * smack_inode_post_setxattr - Apply the Smack update approved above
1310 * @name: attribute name
1311 * @value: attribute value
1312 * @size: attribute size
1315 * Set the pointer in the inode blob to the entry found
1316 * in the master label list.
1318 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1319 const void *value, size_t size, int flags)
1321 struct smack_known *skp;
1322 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1324 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1325 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1329 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1330 skp = smk_import_entry(value, size);
1332 isp->smk_inode = skp;
1333 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1334 skp = smk_import_entry(value, size);
1336 isp->smk_task = skp;
1337 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1338 skp = smk_import_entry(value, size);
1340 isp->smk_mmap = skp;
1347 * smack_inode_getxattr - Smack check on getxattr
1348 * @dentry: the object
1351 * Returns 0 if access is permitted, an error code otherwise
1353 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1355 struct smk_audit_info ad;
1358 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1359 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1361 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1362 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1367 * smack_inode_removexattr - Smack check on removexattr
1368 * @dentry: the object
1369 * @name: name of the attribute
1371 * Removing the Smack attribute requires CAP_MAC_ADMIN
1373 * Returns 0 if access is permitted, an error code otherwise
1375 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1377 struct inode_smack *isp;
1378 struct smk_audit_info ad;
1381 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1382 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1383 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1384 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1385 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1386 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1387 if (!smack_privileged(CAP_MAC_ADMIN))
1390 rc = cap_inode_removexattr(dentry, name);
1395 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1396 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1398 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1399 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1403 isp = smack_inode(d_backing_inode(dentry));
1405 * Don't do anything special for these.
1406 * XATTR_NAME_SMACKIPIN
1407 * XATTR_NAME_SMACKIPOUT
1409 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1410 struct super_block *sbp = dentry->d_sb;
1411 struct superblock_smack *sbsp = sbp->s_security;
1413 isp->smk_inode = sbsp->smk_default;
1414 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1415 isp->smk_task = NULL;
1416 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1417 isp->smk_mmap = NULL;
1418 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1419 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1425 * smack_inode_getsecurity - get smack xattrs
1426 * @inode: the object
1427 * @name: attribute name
1428 * @buffer: where to put the result
1429 * @alloc: duplicate memory
1431 * Returns the size of the attribute or an error code
1433 static int smack_inode_getsecurity(struct inode *inode,
1434 const char *name, void **buffer,
1437 struct socket_smack *ssp;
1438 struct socket *sock;
1439 struct super_block *sbp;
1440 struct inode *ip = (struct inode *)inode;
1441 struct smack_known *isp;
1443 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1444 isp = smk_of_inode(inode);
1447 * The rest of the Smack xattrs are only on sockets.
1450 if (sbp->s_magic != SOCKFS_MAGIC)
1453 sock = SOCKET_I(ip);
1454 if (sock == NULL || sock->sk == NULL)
1457 ssp = sock->sk->sk_security;
1459 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1461 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1468 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1469 if (*buffer == NULL)
1473 return strlen(isp->smk_known);
1478 * smack_inode_listsecurity - list the Smack attributes
1479 * @inode: the object
1480 * @buffer: where they go
1481 * @buffer_size: size of buffer
1483 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1486 int len = sizeof(XATTR_NAME_SMACK);
1488 if (buffer != NULL && len <= buffer_size)
1489 memcpy(buffer, XATTR_NAME_SMACK, len);
1495 * smack_inode_getsecid - Extract inode's security id
1496 * @inode: inode to extract the info from
1497 * @secid: where result will be saved
1499 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1501 struct smack_known *skp = smk_of_inode(inode);
1503 *secid = skp->smk_secid;
1511 * There is no smack_file_permission hook
1513 * Should access checks be done on each read or write?
1514 * UNICOS and SELinux say yes.
1515 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1517 * I'll say no for now. Smack does not do the frequent
1518 * label changing that SELinux does.
1522 * smack_file_alloc_security - assign a file security blob
1525 * The security blob for a file is a pointer to the master
1526 * label list, so no allocation is done.
1528 * f_security is the owner security information. It
1529 * isn't used on file access checks, it's for send_sigio.
1533 static int smack_file_alloc_security(struct file *file)
1535 struct smack_known **blob = smack_file(file);
1537 *blob = smk_of_current();
1542 * smack_file_ioctl - Smack check on ioctls
1547 * Relies heavily on the correct use of the ioctl command conventions.
1549 * Returns 0 if allowed, error code otherwise
1551 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1555 struct smk_audit_info ad;
1556 struct inode *inode = file_inode(file);
1558 if (unlikely(IS_PRIVATE(inode)))
1561 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1562 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1564 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1565 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1566 rc = smk_bu_file(file, MAY_WRITE, rc);
1569 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1570 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1571 rc = smk_bu_file(file, MAY_READ, rc);
1578 * smack_file_lock - Smack check on file locking
1582 * Returns 0 if current has lock access, error code otherwise
1584 static int smack_file_lock(struct file *file, unsigned int cmd)
1586 struct smk_audit_info ad;
1588 struct inode *inode = file_inode(file);
1590 if (unlikely(IS_PRIVATE(inode)))
1593 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1594 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1595 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1596 rc = smk_bu_file(file, MAY_LOCK, rc);
1601 * smack_file_fcntl - Smack check on fcntl
1603 * @cmd: what action to check
1606 * Generally these operations are harmless.
1607 * File locking operations present an obvious mechanism
1608 * for passing information, so they require write access.
1610 * Returns 0 if current has access, error code otherwise
1612 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1615 struct smk_audit_info ad;
1617 struct inode *inode = file_inode(file);
1619 if (unlikely(IS_PRIVATE(inode)))
1627 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1628 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1629 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1630 rc = smk_bu_file(file, MAY_LOCK, rc);
1634 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1635 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1636 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1637 rc = smk_bu_file(file, MAY_WRITE, rc);
1648 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1649 * if mapping anonymous memory.
1650 * @file contains the file structure for file to map (may be NULL).
1651 * @reqprot contains the protection requested by the application.
1652 * @prot contains the protection that will be applied by the kernel.
1653 * @flags contains the operational flags.
1654 * Return 0 if permission is granted.
1656 static int smack_mmap_file(struct file *file,
1657 unsigned long reqprot, unsigned long prot,
1658 unsigned long flags)
1660 struct smack_known *skp;
1661 struct smack_known *mkp;
1662 struct smack_rule *srp;
1663 struct task_smack *tsp;
1664 struct smack_known *okp;
1665 struct inode_smack *isp;
1666 struct superblock_smack *sbsp;
1675 if (unlikely(IS_PRIVATE(file_inode(file))))
1678 isp = smack_inode(file_inode(file));
1679 if (isp->smk_mmap == NULL)
1681 sbsp = file_inode(file)->i_sb->s_security;
1682 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1683 isp->smk_mmap != sbsp->smk_root)
1685 mkp = isp->smk_mmap;
1687 tsp = smack_cred(current_cred());
1688 skp = smk_of_current();
1693 * For each Smack rule associated with the subject
1694 * label verify that the SMACK64MMAP also has access
1695 * to that rule's object label.
1697 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1698 okp = srp->smk_object;
1700 * Matching labels always allows access.
1702 if (mkp->smk_known == okp->smk_known)
1705 * If there is a matching local rule take
1706 * that into account as well.
1708 may = smk_access_entry(srp->smk_subject->smk_known,
1712 may = srp->smk_access;
1714 may &= srp->smk_access;
1716 * If may is zero the SMACK64MMAP subject can't
1717 * possibly have less access.
1723 * Fetch the global list entry.
1724 * If there isn't one a SMACK64MMAP subject
1725 * can't have as much access as current.
1727 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1729 if (mmay == -ENOENT) {
1734 * If there is a local entry it modifies the
1735 * potential access, too.
1737 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1739 if (tmay != -ENOENT)
1743 * If there is any access available to current that is
1744 * not available to a SMACK64MMAP subject
1747 if ((may | mmay) != mmay) {
1759 * smack_file_set_fowner - set the file security blob value
1760 * @file: object in question
1763 static void smack_file_set_fowner(struct file *file)
1765 struct smack_known **blob = smack_file(file);
1767 *blob = smk_of_current();
1771 * smack_file_send_sigiotask - Smack on sigio
1772 * @tsk: The target task
1773 * @fown: the object the signal come from
1776 * Allow a privileged task to get signals even if it shouldn't
1778 * Returns 0 if a subject with the object's smack could
1779 * write to the task, an error code otherwise.
1781 static int smack_file_send_sigiotask(struct task_struct *tsk,
1782 struct fown_struct *fown, int signum)
1784 struct smack_known **blob;
1785 struct smack_known *skp;
1786 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1787 const struct cred *tcred;
1790 struct smk_audit_info ad;
1793 * struct fown_struct is never outside the context of a struct file
1795 file = container_of(fown, struct file, f_owner);
1797 /* we don't log here as rc can be overriden */
1798 blob = smack_file(file);
1800 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1801 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1804 tcred = __task_cred(tsk);
1805 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1809 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1810 smk_ad_setfield_u_tsk(&ad, tsk);
1811 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1816 * smack_file_receive - Smack file receive check
1819 * Returns 0 if current has access, error code otherwise
1821 static int smack_file_receive(struct file *file)
1825 struct smk_audit_info ad;
1826 struct inode *inode = file_inode(file);
1827 struct socket *sock;
1828 struct task_smack *tsp;
1829 struct socket_smack *ssp;
1831 if (unlikely(IS_PRIVATE(inode)))
1834 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1835 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1837 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1838 sock = SOCKET_I(inode);
1839 ssp = sock->sk->sk_security;
1840 tsp = smack_cred(current_cred());
1842 * If the receiving process can't write to the
1843 * passed socket or if the passed socket can't
1844 * write to the receiving process don't accept
1845 * the passed socket.
1847 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1848 rc = smk_bu_file(file, may, rc);
1851 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1852 rc = smk_bu_file(file, may, rc);
1856 * This code relies on bitmasks.
1858 if (file->f_mode & FMODE_READ)
1860 if (file->f_mode & FMODE_WRITE)
1863 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1864 rc = smk_bu_file(file, may, rc);
1869 * smack_file_open - Smack dentry open processing
1872 * Set the security blob in the file structure.
1873 * Allow the open only if the task has read access. There are
1874 * many read operations (e.g. fstat) that you can do with an
1875 * fd even if you have the file open write-only.
1877 * Returns 0 if current has access, error code otherwise
1879 static int smack_file_open(struct file *file)
1881 struct task_smack *tsp = smack_cred(file->f_cred);
1882 struct inode *inode = file_inode(file);
1883 struct smk_audit_info ad;
1886 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1887 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1888 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1889 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1899 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1900 * @cred: the new credentials
1901 * @gfp: the atomicity of any memory allocations
1903 * Prepare a blank set of credentials for modification. This must allocate all
1904 * the memory the LSM module might require such that cred_transfer() can
1905 * complete without error.
1907 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1909 init_task_smack(smack_cred(cred), NULL, NULL);
1915 * smack_cred_free - "free" task-level security credentials
1916 * @cred: the credentials in question
1919 static void smack_cred_free(struct cred *cred)
1921 struct task_smack *tsp = smack_cred(cred);
1922 struct smack_rule *rp;
1923 struct list_head *l;
1924 struct list_head *n;
1926 smk_destroy_label_list(&tsp->smk_relabel);
1928 list_for_each_safe(l, n, &tsp->smk_rules) {
1929 rp = list_entry(l, struct smack_rule, list);
1930 list_del(&rp->list);
1931 kmem_cache_free(smack_rule_cache, rp);
1936 * smack_cred_prepare - prepare new set of credentials for modification
1937 * @new: the new credentials
1938 * @old: the original credentials
1939 * @gfp: the atomicity of any memory allocations
1941 * Prepare a new set of credentials for modification.
1943 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1946 struct task_smack *old_tsp = smack_cred(old);
1947 struct task_smack *new_tsp = smack_cred(new);
1950 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1952 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1956 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1962 * smack_cred_transfer - Transfer the old credentials to the new credentials
1963 * @new: the new credentials
1964 * @old: the original credentials
1966 * Fill in a set of blank credentials from another set of credentials.
1968 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1970 struct task_smack *old_tsp = smack_cred(old);
1971 struct task_smack *new_tsp = smack_cred(new);
1973 new_tsp->smk_task = old_tsp->smk_task;
1974 new_tsp->smk_forked = old_tsp->smk_task;
1975 mutex_init(&new_tsp->smk_rules_lock);
1976 INIT_LIST_HEAD(&new_tsp->smk_rules);
1978 /* cbs copy rule list */
1982 * smack_cred_getsecid - get the secid corresponding to a creds structure
1983 * @cred: the object creds
1984 * @secid: where to put the result
1986 * Sets the secid to contain a u32 version of the smack label.
1988 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1990 struct smack_known *skp;
1993 skp = smk_of_task(smack_cred(cred));
1994 *secid = skp->smk_secid;
1999 * smack_kernel_act_as - Set the subjective context in a set of credentials
2000 * @new: points to the set of credentials to be modified.
2001 * @secid: specifies the security ID to be set
2003 * Set the security data for a kernel service.
2005 static int smack_kernel_act_as(struct cred *new, u32 secid)
2007 struct task_smack *new_tsp = smack_cred(new);
2009 new_tsp->smk_task = smack_from_secid(secid);
2014 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2015 * @new: points to the set of credentials to be modified
2016 * @inode: points to the inode to use as a reference
2018 * Set the file creation context in a set of credentials to the same
2019 * as the objective context of the specified inode
2021 static int smack_kernel_create_files_as(struct cred *new,
2022 struct inode *inode)
2024 struct inode_smack *isp = smack_inode(inode);
2025 struct task_smack *tsp = smack_cred(new);
2027 tsp->smk_forked = isp->smk_inode;
2028 tsp->smk_task = tsp->smk_forked;
2033 * smk_curacc_on_task - helper to log task related access
2034 * @p: the task object
2035 * @access: the access requested
2036 * @caller: name of the calling function for audit
2038 * Return 0 if access is permitted
2040 static int smk_curacc_on_task(struct task_struct *p, int access,
2043 struct smk_audit_info ad;
2044 struct smack_known *skp = smk_of_task_struct(p);
2047 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2048 smk_ad_setfield_u_tsk(&ad, p);
2049 rc = smk_curacc(skp, access, &ad);
2050 rc = smk_bu_task(p, access, rc);
2055 * smack_task_setpgid - Smack check on setting pgid
2056 * @p: the task object
2059 * Return 0 if write access is permitted
2061 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2063 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2067 * smack_task_getpgid - Smack access check for getpgid
2068 * @p: the object task
2070 * Returns 0 if current can read the object task, error code otherwise
2072 static int smack_task_getpgid(struct task_struct *p)
2074 return smk_curacc_on_task(p, MAY_READ, __func__);
2078 * smack_task_getsid - Smack access check for getsid
2079 * @p: the object task
2081 * Returns 0 if current can read the object task, error code otherwise
2083 static int smack_task_getsid(struct task_struct *p)
2085 return smk_curacc_on_task(p, MAY_READ, __func__);
2089 * smack_task_getsecid - get the secid of the task
2090 * @p: the object task
2091 * @secid: where to put the result
2093 * Sets the secid to contain a u32 version of the smack label.
2095 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2097 struct smack_known *skp = smk_of_task_struct(p);
2099 *secid = skp->smk_secid;
2103 * smack_task_setnice - Smack check on setting nice
2104 * @p: the task object
2107 * Return 0 if write access is permitted
2109 static int smack_task_setnice(struct task_struct *p, int nice)
2111 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2115 * smack_task_setioprio - Smack check on setting ioprio
2116 * @p: the task object
2119 * Return 0 if write access is permitted
2121 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2123 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2127 * smack_task_getioprio - Smack check on reading ioprio
2128 * @p: the task object
2130 * Return 0 if read access is permitted
2132 static int smack_task_getioprio(struct task_struct *p)
2134 return smk_curacc_on_task(p, MAY_READ, __func__);
2138 * smack_task_setscheduler - Smack check on setting scheduler
2139 * @p: the task object
2141 * Return 0 if read access is permitted
2143 static int smack_task_setscheduler(struct task_struct *p)
2145 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2149 * smack_task_getscheduler - Smack check on reading scheduler
2150 * @p: the task object
2152 * Return 0 if read access is permitted
2154 static int smack_task_getscheduler(struct task_struct *p)
2156 return smk_curacc_on_task(p, MAY_READ, __func__);
2160 * smack_task_movememory - Smack check on moving memory
2161 * @p: the task object
2163 * Return 0 if write access is permitted
2165 static int smack_task_movememory(struct task_struct *p)
2167 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2171 * smack_task_kill - Smack check on signal delivery
2172 * @p: the task object
2175 * @cred: identifies the cred to use in lieu of current's
2177 * Return 0 if write access is permitted
2180 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2181 int sig, const struct cred *cred)
2183 struct smk_audit_info ad;
2184 struct smack_known *skp;
2185 struct smack_known *tkp = smk_of_task_struct(p);
2189 return 0; /* null signal; existence test */
2191 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2192 smk_ad_setfield_u_tsk(&ad, p);
2194 * Sending a signal requires that the sender
2195 * can write the receiver.
2198 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2199 rc = smk_bu_task(p, MAY_DELIVER, rc);
2203 * If the cred isn't NULL we're dealing with some USB IO
2204 * specific behavior. This is not clean. For one thing
2205 * we can't take privilege into account.
2207 skp = smk_of_task(smack_cred(cred));
2208 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2209 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2214 * smack_task_to_inode - copy task smack into the inode blob
2215 * @p: task to copy from
2216 * @inode: inode to copy to
2218 * Sets the smack pointer in the inode security blob
2220 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2222 struct inode_smack *isp = smack_inode(inode);
2223 struct smack_known *skp = smk_of_task_struct(p);
2225 isp->smk_inode = skp;
2226 isp->smk_flags |= SMK_INODE_INSTANT;
2234 * smack_sk_alloc_security - Allocate a socket blob
2237 * @gfp_flags: memory allocation flags
2239 * Assign Smack pointers to current
2241 * Returns 0 on success, -ENOMEM is there's no memory
2243 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2245 struct smack_known *skp = smk_of_current();
2246 struct socket_smack *ssp;
2248 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2253 * Sockets created by kernel threads receive web label.
2255 if (unlikely(current->flags & PF_KTHREAD)) {
2256 ssp->smk_in = &smack_known_web;
2257 ssp->smk_out = &smack_known_web;
2262 ssp->smk_packet = NULL;
2264 sk->sk_security = ssp;
2270 * smack_sk_free_security - Free a socket blob
2273 * Clears the blob pointer
2275 static void smack_sk_free_security(struct sock *sk)
2277 #ifdef SMACK_IPV6_PORT_LABELING
2278 struct smk_port_label *spp;
2280 if (sk->sk_family == PF_INET6) {
2282 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2283 if (spp->smk_sock != sk)
2285 spp->smk_can_reuse = 1;
2291 kfree(sk->sk_security);
2295 * smack_ipv4host_label - check host based restrictions
2296 * @sip: the object end
2298 * looks for host based access restrictions
2300 * This version will only be appropriate for really small sets of single label
2301 * hosts. The caller is responsible for ensuring that the RCU read lock is
2302 * taken before calling this function.
2304 * Returns the label of the far end or NULL if it's not special.
2306 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2308 struct smk_net4addr *snp;
2309 struct in_addr *siap = &sip->sin_addr;
2311 if (siap->s_addr == 0)
2314 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2316 * we break after finding the first match because
2317 * the list is sorted from longest to shortest mask
2318 * so we have found the most specific match
2320 if (snp->smk_host.s_addr ==
2321 (siap->s_addr & snp->smk_mask.s_addr))
2322 return snp->smk_label;
2328 * smk_ipv6_localhost - Check for local ipv6 host address
2331 * Returns boolean true if this is the localhost address
2333 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2335 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2336 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2338 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2339 ntohs(be16p[7]) == 1)
2345 * smack_ipv6host_label - check host based restrictions
2346 * @sip: the object end
2348 * looks for host based access restrictions
2350 * This version will only be appropriate for really small sets of single label
2351 * hosts. The caller is responsible for ensuring that the RCU read lock is
2352 * taken before calling this function.
2354 * Returns the label of the far end or NULL if it's not special.
2356 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2358 struct smk_net6addr *snp;
2359 struct in6_addr *sap = &sip->sin6_addr;
2364 * It's local. Don't look for a host label.
2366 if (smk_ipv6_localhost(sip))
2369 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2371 * If the label is NULL the entry has
2372 * been renounced. Ignore it.
2374 if (snp->smk_label == NULL)
2377 * we break after finding the first match because
2378 * the list is sorted from longest to shortest mask
2379 * so we have found the most specific match
2381 for (found = 1, i = 0; i < 8; i++) {
2382 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2383 snp->smk_host.s6_addr16[i]) {
2389 return snp->smk_label;
2396 * smack_netlabel - Set the secattr on a socket
2398 * @labeled: socket label scheme
2400 * Convert the outbound smack value (smk_out) to a
2401 * secattr and attach it to the socket.
2403 * Returns 0 on success or an error code
2405 static int smack_netlabel(struct sock *sk, int labeled)
2407 struct smack_known *skp;
2408 struct socket_smack *ssp = sk->sk_security;
2412 * Usually the netlabel code will handle changing the
2413 * packet labeling based on the label.
2414 * The case of a single label host is different, because
2415 * a single label host should never get a labeled packet
2416 * even though the label is usually associated with a packet
2420 bh_lock_sock_nested(sk);
2422 if (ssp->smk_out == smack_net_ambient ||
2423 labeled == SMACK_UNLABELED_SOCKET)
2424 netlbl_sock_delattr(sk);
2427 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2437 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2439 * @sap: the destination address
2441 * Set the correct secattr for the given socket based on the destination
2442 * address and perform any outbound access checks needed.
2444 * Returns 0 on success or an error code.
2447 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2449 struct smack_known *skp;
2452 struct smack_known *hkp;
2453 struct socket_smack *ssp = sk->sk_security;
2454 struct smk_audit_info ad;
2457 hkp = smack_ipv4host_label(sap);
2460 struct lsm_network_audit net;
2462 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2463 ad.a.u.net->family = sap->sin_family;
2464 ad.a.u.net->dport = sap->sin_port;
2465 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2467 sk_lbl = SMACK_UNLABELED_SOCKET;
2469 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2470 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2472 sk_lbl = SMACK_CIPSO_SOCKET;
2479 return smack_netlabel(sk, sk_lbl);
2483 * smk_ipv6_check - check Smack access
2484 * @subject: subject Smack label
2485 * @object: object Smack label
2487 * @act: the action being taken
2489 * Check an IPv6 access
2491 static int smk_ipv6_check(struct smack_known *subject,
2492 struct smack_known *object,
2493 struct sockaddr_in6 *address, int act)
2496 struct lsm_network_audit net;
2498 struct smk_audit_info ad;
2502 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2503 ad.a.u.net->family = PF_INET6;
2504 ad.a.u.net->dport = ntohs(address->sin6_port);
2505 if (act == SMK_RECEIVING)
2506 ad.a.u.net->v6info.saddr = address->sin6_addr;
2508 ad.a.u.net->v6info.daddr = address->sin6_addr;
2510 rc = smk_access(subject, object, MAY_WRITE, &ad);
2511 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2515 #ifdef SMACK_IPV6_PORT_LABELING
2517 * smk_ipv6_port_label - Smack port access table management
2521 * Create or update the port list entry
2523 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2525 struct sock *sk = sock->sk;
2526 struct sockaddr_in6 *addr6;
2527 struct socket_smack *ssp = sock->sk->sk_security;
2528 struct smk_port_label *spp;
2529 unsigned short port = 0;
2531 if (address == NULL) {
2533 * This operation is changing the Smack information
2534 * on the bound socket. Take the changes to the port
2538 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2539 if (sk != spp->smk_sock)
2541 spp->smk_in = ssp->smk_in;
2542 spp->smk_out = ssp->smk_out;
2547 * A NULL address is only used for updating existing
2548 * bound entries. If there isn't one, it's OK.
2554 addr6 = (struct sockaddr_in6 *)address;
2555 port = ntohs(addr6->sin6_port);
2557 * This is a special case that is safely ignored.
2563 * Look for an existing port list entry.
2564 * This is an indication that a port is getting reused.
2567 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2568 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2570 if (spp->smk_can_reuse != 1) {
2574 spp->smk_port = port;
2576 spp->smk_in = ssp->smk_in;
2577 spp->smk_out = ssp->smk_out;
2578 spp->smk_can_reuse = 0;
2584 * A new port entry is required.
2586 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2590 spp->smk_port = port;
2592 spp->smk_in = ssp->smk_in;
2593 spp->smk_out = ssp->smk_out;
2594 spp->smk_sock_type = sock->type;
2595 spp->smk_can_reuse = 0;
2597 mutex_lock(&smack_ipv6_lock);
2598 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2599 mutex_unlock(&smack_ipv6_lock);
2605 * smk_ipv6_port_check - check Smack port access
2608 * @act: the action being taken
2610 * Create or update the port list entry
2612 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2615 struct smk_port_label *spp;
2616 struct socket_smack *ssp = sk->sk_security;
2617 struct smack_known *skp = NULL;
2618 unsigned short port;
2619 struct smack_known *object;
2621 if (act == SMK_RECEIVING) {
2622 skp = smack_ipv6host_label(address);
2623 object = ssp->smk_in;
2626 object = smack_ipv6host_label(address);
2630 * The other end is a single label host.
2632 if (skp != NULL && object != NULL)
2633 return smk_ipv6_check(skp, object, address, act);
2635 skp = smack_net_ambient;
2637 object = smack_net_ambient;
2640 * It's remote, so port lookup does no good.
2642 if (!smk_ipv6_localhost(address))
2643 return smk_ipv6_check(skp, object, address, act);
2646 * It's local so the send check has to have passed.
2648 if (act == SMK_RECEIVING)
2651 port = ntohs(address->sin6_port);
2653 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2654 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2656 object = spp->smk_in;
2657 if (act == SMK_CONNECTING)
2658 ssp->smk_packet = spp->smk_out;
2663 return smk_ipv6_check(skp, object, address, act);
2667 * smack_inode_setsecurity - set smack xattrs
2668 * @inode: the object
2669 * @name: attribute name
2670 * @value: attribute value
2671 * @size: size of the attribute
2674 * Sets the named attribute in the appropriate blob
2676 * Returns 0 on success, or an error code
2678 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2679 const void *value, size_t size, int flags)
2681 struct smack_known *skp;
2682 struct inode_smack *nsp = smack_inode(inode);
2683 struct socket_smack *ssp;
2684 struct socket *sock;
2687 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2690 skp = smk_import_entry(value, size);
2692 return PTR_ERR(skp);
2694 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2695 nsp->smk_inode = skp;
2696 nsp->smk_flags |= SMK_INODE_INSTANT;
2700 * The rest of the Smack xattrs are only on sockets.
2702 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2705 sock = SOCKET_I(inode);
2706 if (sock == NULL || sock->sk == NULL)
2709 ssp = sock->sk->sk_security;
2711 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2713 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2715 if (sock->sk->sk_family == PF_INET) {
2716 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2719 "Smack: \"%s\" netlbl error %d.\n",
2725 #ifdef SMACK_IPV6_PORT_LABELING
2726 if (sock->sk->sk_family == PF_INET6)
2727 smk_ipv6_port_label(sock, NULL);
2734 * smack_socket_post_create - finish socket setup
2736 * @family: protocol family
2741 * Sets the netlabel information on the socket
2743 * Returns 0 on success, and error code otherwise
2745 static int smack_socket_post_create(struct socket *sock, int family,
2746 int type, int protocol, int kern)
2748 struct socket_smack *ssp;
2750 if (sock->sk == NULL)
2754 * Sockets created by kernel threads receive web label.
2756 if (unlikely(current->flags & PF_KTHREAD)) {
2757 ssp = sock->sk->sk_security;
2758 ssp->smk_in = &smack_known_web;
2759 ssp->smk_out = &smack_known_web;
2762 if (family != PF_INET)
2765 * Set the outbound netlbl.
2767 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2771 * smack_socket_socketpair - create socket pair
2772 * @socka: one socket
2773 * @sockb: another socket
2775 * Cross reference the peer labels for SO_PEERSEC
2779 static int smack_socket_socketpair(struct socket *socka,
2780 struct socket *sockb)
2782 struct socket_smack *asp = socka->sk->sk_security;
2783 struct socket_smack *bsp = sockb->sk->sk_security;
2785 asp->smk_packet = bsp->smk_out;
2786 bsp->smk_packet = asp->smk_out;
2791 #ifdef SMACK_IPV6_PORT_LABELING
2793 * smack_socket_bind - record port binding information.
2795 * @address: the port address
2796 * @addrlen: size of the address
2798 * Records the label bound to a port.
2800 * Returns 0 on success, and error code otherwise
2802 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2805 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2806 if (addrlen < SIN6_LEN_RFC2133 ||
2807 address->sa_family != AF_INET6)
2809 smk_ipv6_port_label(sock, address);
2813 #endif /* SMACK_IPV6_PORT_LABELING */
2816 * smack_socket_connect - connect access check
2818 * @sap: the other end
2819 * @addrlen: size of sap
2821 * Verifies that a connection may be possible
2823 * Returns 0 on success, and error code otherwise
2825 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2830 if (sock->sk == NULL)
2832 if (sock->sk->sk_family != PF_INET &&
2833 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2835 if (addrlen < offsetofend(struct sockaddr, sa_family))
2837 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2838 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2839 struct smack_known *rsp = NULL;
2841 if (addrlen < SIN6_LEN_RFC2133)
2843 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2844 rsp = smack_ipv6host_label(sip);
2846 struct socket_smack *ssp = sock->sk->sk_security;
2848 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2851 if (__is_defined(SMACK_IPV6_PORT_LABELING))
2852 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2856 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2858 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2863 * smack_flags_to_may - convert S_ to MAY_ values
2864 * @flags: the S_ value
2866 * Returns the equivalent MAY_ value
2868 static int smack_flags_to_may(int flags)
2872 if (flags & S_IRUGO)
2874 if (flags & S_IWUGO)
2876 if (flags & S_IXUGO)
2883 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2888 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2890 struct smack_known **blob = smack_msg_msg(msg);
2892 *blob = smk_of_current();
2897 * smack_of_ipc - the smack pointer for the ipc
2900 * Returns a pointer to the smack value
2902 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2904 struct smack_known **blob = smack_ipc(isp);
2910 * smack_ipc_alloc_security - Set the security blob for ipc
2915 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2917 struct smack_known **blob = smack_ipc(isp);
2919 *blob = smk_of_current();
2924 * smk_curacc_shm : check if current has access on shm
2926 * @access : access requested
2928 * Returns 0 if current has the requested access, error code otherwise
2930 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2932 struct smack_known *ssp = smack_of_ipc(isp);
2933 struct smk_audit_info ad;
2937 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2938 ad.a.u.ipc_id = isp->id;
2940 rc = smk_curacc(ssp, access, &ad);
2941 rc = smk_bu_current("shm", ssp, access, rc);
2946 * smack_shm_associate - Smack access check for shm
2948 * @shmflg: access requested
2950 * Returns 0 if current has the requested access, error code otherwise
2952 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2956 may = smack_flags_to_may(shmflg);
2957 return smk_curacc_shm(isp, may);
2961 * smack_shm_shmctl - Smack access check for shm
2963 * @cmd: what it wants to do
2965 * Returns 0 if current has the requested access, error code otherwise
2967 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2981 may = MAY_READWRITE;
2986 * System level information.
2992 return smk_curacc_shm(isp, may);
2996 * smack_shm_shmat - Smack access for shmat
2999 * @shmflg: access requested
3001 * Returns 0 if current has the requested access, error code otherwise
3003 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3008 may = smack_flags_to_may(shmflg);
3009 return smk_curacc_shm(isp, may);
3013 * smk_curacc_sem : check if current has access on sem
3015 * @access : access requested
3017 * Returns 0 if current has the requested access, error code otherwise
3019 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3021 struct smack_known *ssp = smack_of_ipc(isp);
3022 struct smk_audit_info ad;
3026 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3027 ad.a.u.ipc_id = isp->id;
3029 rc = smk_curacc(ssp, access, &ad);
3030 rc = smk_bu_current("sem", ssp, access, rc);
3035 * smack_sem_associate - Smack access check for sem
3037 * @semflg: access requested
3039 * Returns 0 if current has the requested access, error code otherwise
3041 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3045 may = smack_flags_to_may(semflg);
3046 return smk_curacc_sem(isp, may);
3050 * smack_sem_shmctl - Smack access check for sem
3052 * @cmd: what it wants to do
3054 * Returns 0 if current has the requested access, error code otherwise
3056 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3075 may = MAY_READWRITE;
3080 * System level information
3087 return smk_curacc_sem(isp, may);
3091 * smack_sem_semop - Smack checks of semaphore operations
3097 * Treated as read and write in all cases.
3099 * Returns 0 if access is allowed, error code otherwise
3101 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3102 unsigned nsops, int alter)
3104 return smk_curacc_sem(isp, MAY_READWRITE);
3108 * smk_curacc_msq : helper to check if current has access on msq
3110 * @access : access requested
3112 * return 0 if current has access, error otherwise
3114 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3116 struct smack_known *msp = smack_of_ipc(isp);
3117 struct smk_audit_info ad;
3121 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3122 ad.a.u.ipc_id = isp->id;
3124 rc = smk_curacc(msp, access, &ad);
3125 rc = smk_bu_current("msq", msp, access, rc);
3130 * smack_msg_queue_associate - Smack access check for msg_queue
3132 * @msqflg: access requested
3134 * Returns 0 if current has the requested access, error code otherwise
3136 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3140 may = smack_flags_to_may(msqflg);
3141 return smk_curacc_msq(isp, may);
3145 * smack_msg_queue_msgctl - Smack access check for msg_queue
3147 * @cmd: what it wants to do
3149 * Returns 0 if current has the requested access, error code otherwise
3151 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3163 may = MAY_READWRITE;
3168 * System level information
3175 return smk_curacc_msq(isp, may);
3179 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3182 * @msqflg: access requested
3184 * Returns 0 if current has the requested access, error code otherwise
3186 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3191 may = smack_flags_to_may(msqflg);
3192 return smk_curacc_msq(isp, may);
3196 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3203 * Returns 0 if current has read and write access, error code otherwise
3205 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3206 struct task_struct *target, long type, int mode)
3208 return smk_curacc_msq(isp, MAY_READWRITE);
3212 * smack_ipc_permission - Smack access for ipc_permission()
3213 * @ipp: the object permissions
3214 * @flag: access requested
3216 * Returns 0 if current has read and write access, error code otherwise
3218 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3220 struct smack_known **blob = smack_ipc(ipp);
3221 struct smack_known *iskp = *blob;
3222 int may = smack_flags_to_may(flag);
3223 struct smk_audit_info ad;
3227 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3228 ad.a.u.ipc_id = ipp->id;
3230 rc = smk_curacc(iskp, may, &ad);
3231 rc = smk_bu_current("svipc", iskp, may, rc);
3236 * smack_ipc_getsecid - Extract smack security id
3237 * @ipp: the object permissions
3238 * @secid: where result will be saved
3240 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3242 struct smack_known **blob = smack_ipc(ipp);
3243 struct smack_known *iskp = *blob;
3245 *secid = iskp->smk_secid;
3249 * smack_d_instantiate - Make sure the blob is correct on an inode
3250 * @opt_dentry: dentry where inode will be attached
3251 * @inode: the object
3253 * Set the inode's security blob if it hasn't been done already.
3255 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3257 struct super_block *sbp;
3258 struct superblock_smack *sbsp;
3259 struct inode_smack *isp;
3260 struct smack_known *skp;
3261 struct smack_known *ckp = smk_of_current();
3262 struct smack_known *final;
3263 char trattr[TRANS_TRUE_SIZE];
3271 isp = smack_inode(inode);
3273 mutex_lock(&isp->smk_lock);
3275 * If the inode is already instantiated
3276 * take the quick way out
3278 if (isp->smk_flags & SMK_INODE_INSTANT)
3282 sbsp = sbp->s_security;
3284 * We're going to use the superblock default label
3285 * if there's no label on the file.
3287 final = sbsp->smk_default;
3290 * If this is the root inode the superblock
3291 * may be in the process of initialization.
3292 * If that is the case use the root value out
3293 * of the superblock.
3295 if (opt_dentry->d_parent == opt_dentry) {
3296 switch (sbp->s_magic) {
3297 case CGROUP_SUPER_MAGIC:
3298 case CGROUP2_SUPER_MAGIC:
3300 * The cgroup filesystem is never mounted,
3301 * so there's no opportunity to set the mount
3304 sbsp->smk_root = &smack_known_star;
3305 sbsp->smk_default = &smack_known_star;
3306 isp->smk_inode = sbsp->smk_root;
3310 * What about shmem/tmpfs anonymous files with dentry
3311 * obtained from d_alloc_pseudo()?
3313 isp->smk_inode = smk_of_current();
3316 isp->smk_inode = smk_of_current();
3320 * Socket access is controlled by the socket
3321 * structures associated with the task involved.
3323 isp->smk_inode = &smack_known_star;
3326 isp->smk_inode = sbsp->smk_root;
3329 isp->smk_flags |= SMK_INODE_INSTANT;
3334 * This is pretty hackish.
3335 * Casey says that we shouldn't have to do
3336 * file system specific code, but it does help
3337 * with keeping it simple.
3339 switch (sbp->s_magic) {
3341 case CGROUP_SUPER_MAGIC:
3342 case CGROUP2_SUPER_MAGIC:
3344 * Casey says that it's a little embarrassing
3345 * that the smack file system doesn't do
3346 * extended attributes.
3348 * Cgroupfs is special
3350 final = &smack_known_star;
3352 case DEVPTS_SUPER_MAGIC:
3354 * devpts seems content with the label of the task.
3355 * Programs that change smack have to treat the
3360 case PROC_SUPER_MAGIC:
3362 * Casey says procfs appears not to care.
3363 * The superblock default suffices.
3368 * Device labels should come from the filesystem,
3369 * but watch out, because they're volitile,
3370 * getting recreated on every reboot.
3372 final = &smack_known_star;
3374 * If a smack value has been set we want to use it,
3375 * but since tmpfs isn't giving us the opportunity
3376 * to set mount options simulate setting the
3377 * superblock default.
3382 * This isn't an understood special case.
3383 * Get the value from the xattr.
3387 * UNIX domain sockets use lower level socket data.
3389 if (S_ISSOCK(inode->i_mode)) {
3390 final = &smack_known_star;
3394 * No xattr support means, alas, no SMACK label.
3395 * Use the aforeapplied default.
3396 * It would be curious if the label of the task
3397 * does not match that assigned.
3399 if (!(inode->i_opflags & IOP_XATTR))
3402 * Get the dentry for xattr.
3404 dp = dget(opt_dentry);
3405 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3406 if (!IS_ERR_OR_NULL(skp))
3410 * Transmuting directory
3412 if (S_ISDIR(inode->i_mode)) {
3414 * If this is a new directory and the label was
3415 * transmuted when the inode was initialized
3416 * set the transmute attribute on the directory
3417 * and mark the inode.
3419 * If there is a transmute attribute on the
3420 * directory mark the inode.
3422 if (isp->smk_flags & SMK_INODE_CHANGED) {
3423 isp->smk_flags &= ~SMK_INODE_CHANGED;
3424 rc = __vfs_setxattr(dp, inode,
3425 XATTR_NAME_SMACKTRANSMUTE,
3426 TRANS_TRUE, TRANS_TRUE_SIZE,
3429 rc = __vfs_getxattr(dp, inode,
3430 XATTR_NAME_SMACKTRANSMUTE, trattr,
3432 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3433 TRANS_TRUE_SIZE) != 0)
3437 transflag = SMK_INODE_TRANSMUTE;
3440 * Don't let the exec or mmap label be "*" or "@".
3442 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3443 if (IS_ERR(skp) || skp == &smack_known_star ||
3444 skp == &smack_known_web)
3446 isp->smk_task = skp;
3448 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3449 if (IS_ERR(skp) || skp == &smack_known_star ||
3450 skp == &smack_known_web)
3452 isp->smk_mmap = skp;
3459 isp->smk_inode = ckp;
3461 isp->smk_inode = final;
3463 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3466 mutex_unlock(&isp->smk_lock);
3471 * smack_getprocattr - Smack process attribute access
3472 * @p: the object task
3473 * @name: the name of the attribute in /proc/.../attr
3474 * @value: where to put the result
3476 * Places a copy of the task Smack into value
3478 * Returns the length of the smack label or an error code
3480 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3482 struct smack_known *skp = smk_of_task_struct(p);
3486 if (strcmp(name, "current") != 0)
3489 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3499 * smack_setprocattr - Smack process attribute setting
3500 * @name: the name of the attribute in /proc/.../attr
3501 * @value: the value to set
3502 * @size: the size of the value
3504 * Sets the Smack value of the task. Only setting self
3505 * is permitted and only with privilege
3507 * Returns the length of the smack label or an error code
3509 static int smack_setprocattr(const char *name, void *value, size_t size)
3511 struct task_smack *tsp = smack_cred(current_cred());
3513 struct smack_known *skp;
3514 struct smack_known_list_elem *sklep;
3517 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3520 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3523 if (strcmp(name, "current") != 0)
3526 skp = smk_import_entry(value, size);
3528 return PTR_ERR(skp);
3531 * No process is ever allowed the web ("@") label
3532 * and the star ("*") label.
3534 if (skp == &smack_known_web || skp == &smack_known_star)
3537 if (!smack_privileged(CAP_MAC_ADMIN)) {
3539 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3540 if (sklep->smk_label == skp) {
3548 new = prepare_creds();
3552 tsp = smack_cred(new);
3553 tsp->smk_task = skp;
3555 * process can change its label only once
3557 smk_destroy_label_list(&tsp->smk_relabel);
3564 * smack_unix_stream_connect - Smack access on UDS
3566 * @other: the other sock
3569 * Return 0 if a subject with the smack of sock could access
3570 * an object with the smack of other, otherwise an error code
3572 static int smack_unix_stream_connect(struct sock *sock,
3573 struct sock *other, struct sock *newsk)
3575 struct smack_known *skp;
3576 struct smack_known *okp;
3577 struct socket_smack *ssp = sock->sk_security;
3578 struct socket_smack *osp = other->sk_security;
3579 struct socket_smack *nsp = newsk->sk_security;
3580 struct smk_audit_info ad;
3583 struct lsm_network_audit net;
3586 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3590 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3591 smk_ad_setfield_u_net_sk(&ad, other);
3593 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3594 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3598 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3599 rc = smk_bu_note("UDS connect", okp, skp,
3605 * Cross reference the peer labels for SO_PEERSEC.
3608 nsp->smk_packet = ssp->smk_out;
3609 ssp->smk_packet = osp->smk_out;
3616 * smack_unix_may_send - Smack access on UDS
3618 * @other: the other socket
3620 * Return 0 if a subject with the smack of sock could access
3621 * an object with the smack of other, otherwise an error code
3623 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3625 struct socket_smack *ssp = sock->sk->sk_security;
3626 struct socket_smack *osp = other->sk->sk_security;
3627 struct smk_audit_info ad;
3631 struct lsm_network_audit net;
3633 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3634 smk_ad_setfield_u_net_sk(&ad, other->sk);
3637 if (smack_privileged(CAP_MAC_OVERRIDE))
3640 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3641 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3646 * smack_socket_sendmsg - Smack check based on destination host
3649 * @size: the size of the message
3651 * Return 0 if the current subject can write to the destination host.
3652 * For IPv4 this is only a question if the destination is a single label host.
3653 * For IPv6 this is a check against the label of the port.
3655 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3658 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3659 #if IS_ENABLED(CONFIG_IPV6)
3660 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3662 #ifdef SMACK_IPV6_SECMARK_LABELING
3663 struct socket_smack *ssp = sock->sk->sk_security;
3664 struct smack_known *rsp;
3669 * Perfectly reasonable for this to be NULL
3674 switch (sock->sk->sk_family) {
3676 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3677 sip->sin_family != AF_INET)
3679 rc = smack_netlabel_send(sock->sk, sip);
3681 #if IS_ENABLED(CONFIG_IPV6)
3683 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3684 sap->sin6_family != AF_INET6)
3686 #ifdef SMACK_IPV6_SECMARK_LABELING
3687 rsp = smack_ipv6host_label(sap);
3689 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3692 #ifdef SMACK_IPV6_PORT_LABELING
3693 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3695 #endif /* IS_ENABLED(CONFIG_IPV6) */
3702 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3703 * @sap: netlabel secattr
3704 * @ssp: socket security information
3706 * Returns a pointer to a Smack label entry found on the label list.
3708 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3709 struct socket_smack *ssp)
3711 struct smack_known *skp;
3716 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3718 * Looks like a CIPSO packet.
3719 * If there are flags but no level netlabel isn't
3720 * behaving the way we expect it to.
3722 * Look it up in the label table
3723 * Without guidance regarding the smack value
3724 * for the packet fall back on the network
3728 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3729 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3732 * Compare the catsets. Use the netlbl APIs.
3734 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3735 if ((skp->smk_netlabel.flags &
3736 NETLBL_SECATTR_MLS_CAT) == 0)
3740 for (acat = -1, kcat = -1; acat == kcat; ) {
3741 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3743 kcat = netlbl_catmap_walk(
3744 skp->smk_netlabel.attr.mls.cat,
3746 if (acat < 0 || kcat < 0)
3759 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3760 return &smack_known_web;
3761 return &smack_known_star;
3763 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3765 * Looks like a fallback, which gives us a secid.
3767 return smack_from_secid(sap->attr.secid);
3769 * Without guidance regarding the smack value
3770 * for the packet fall back on the network
3773 return smack_net_ambient;
3776 #if IS_ENABLED(CONFIG_IPV6)
3777 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3781 int proto = -EINVAL;
3782 struct ipv6hdr _ipv6h;
3783 struct ipv6hdr *ip6;
3785 struct tcphdr _tcph, *th;
3786 struct udphdr _udph, *uh;
3787 struct dccp_hdr _dccph, *dh;
3791 offset = skb_network_offset(skb);
3792 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3795 sip->sin6_addr = ip6->saddr;
3797 nexthdr = ip6->nexthdr;
3798 offset += sizeof(_ipv6h);
3799 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3806 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3808 sip->sin6_port = th->source;
3811 case IPPROTO_UDPLITE:
3812 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3814 sip->sin6_port = uh->source;
3817 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3819 sip->sin6_port = dh->dccph_sport;
3824 #endif /* CONFIG_IPV6 */
3827 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3831 * Returns 0 if the packet should be delivered, an error code otherwise
3833 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3835 struct netlbl_lsm_secattr secattr;
3836 struct socket_smack *ssp = sk->sk_security;
3837 struct smack_known *skp = NULL;
3839 struct smk_audit_info ad;
3840 u16 family = sk->sk_family;
3842 struct lsm_network_audit net;
3844 #if IS_ENABLED(CONFIG_IPV6)
3845 struct sockaddr_in6 sadd;
3848 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3850 #endif /* CONFIG_IPV6 */
3854 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3856 * If there is a secmark use it rather than the CIPSO label.
3857 * If there is no secmark fall back to CIPSO.
3858 * The secmark is assumed to reflect policy better.
3860 if (skb && skb->secmark != 0) {
3861 skp = smack_from_secid(skb->secmark);
3864 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3866 * Translate what netlabel gave us.
3868 netlbl_secattr_init(&secattr);
3870 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3872 skp = smack_from_secattr(&secattr, ssp);
3874 skp = smack_net_ambient;
3876 netlbl_secattr_destroy(&secattr);
3878 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3882 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3883 ad.a.u.net->family = family;
3884 ad.a.u.net->netif = skb->skb_iif;
3885 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3888 * Receiving a packet requires that the other end
3889 * be able to write here. Read access is not required.
3890 * This is the simplist possible security model
3893 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3894 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3897 netlbl_skbuff_err(skb, family, rc, 0);
3899 #if IS_ENABLED(CONFIG_IPV6)
3901 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3902 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3903 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3905 #ifdef SMACK_IPV6_SECMARK_LABELING
3906 if (skb && skb->secmark != 0)
3907 skp = smack_from_secid(skb->secmark);
3908 else if (smk_ipv6_localhost(&sadd))
3911 skp = smack_ipv6host_label(&sadd);
3913 skp = smack_net_ambient;
3917 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3918 ad.a.u.net->family = family;
3919 ad.a.u.net->netif = skb->skb_iif;
3920 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3921 #endif /* CONFIG_AUDIT */
3922 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3923 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3925 #endif /* SMACK_IPV6_SECMARK_LABELING */
3926 #ifdef SMACK_IPV6_PORT_LABELING
3927 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3928 #endif /* SMACK_IPV6_PORT_LABELING */
3930 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3931 ICMPV6_ADM_PROHIBITED, 0);
3933 #endif /* CONFIG_IPV6 */
3940 * smack_socket_getpeersec_stream - pull in packet label
3942 * @optval: user's destination
3943 * @optlen: size thereof
3946 * returns zero on success, an error code otherwise
3948 static int smack_socket_getpeersec_stream(struct socket *sock,
3949 char __user *optval,
3950 int __user *optlen, unsigned len)
3952 struct socket_smack *ssp;
3957 ssp = sock->sk->sk_security;
3958 if (ssp->smk_packet != NULL) {
3959 rcp = ssp->smk_packet->smk_known;
3960 slen = strlen(rcp) + 1;
3965 else if (copy_to_user(optval, rcp, slen) != 0)
3968 if (put_user(slen, optlen) != 0)
3976 * smack_socket_getpeersec_dgram - pull in packet label
3977 * @sock: the peer socket
3979 * @secid: pointer to where to put the secid of the packet
3981 * Sets the netlabel socket state on sk from parent
3983 static int smack_socket_getpeersec_dgram(struct socket *sock,
3984 struct sk_buff *skb, u32 *secid)
3987 struct netlbl_lsm_secattr secattr;
3988 struct socket_smack *ssp = NULL;
3989 struct smack_known *skp;
3990 int family = PF_UNSPEC;
3991 u32 s = 0; /* 0 is the invalid secid */
3995 if (skb->protocol == htons(ETH_P_IP))
3997 #if IS_ENABLED(CONFIG_IPV6)
3998 else if (skb->protocol == htons(ETH_P_IPV6))
4000 #endif /* CONFIG_IPV6 */
4002 if (family == PF_UNSPEC && sock != NULL)
4003 family = sock->sk->sk_family;
4007 ssp = sock->sk->sk_security;
4008 s = ssp->smk_out->smk_secid;
4011 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4017 * Translate what netlabel gave us.
4019 if (sock != NULL && sock->sk != NULL)
4020 ssp = sock->sk->sk_security;
4021 netlbl_secattr_init(&secattr);
4022 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4024 skp = smack_from_secattr(&secattr, ssp);
4027 netlbl_secattr_destroy(&secattr);
4030 #ifdef SMACK_IPV6_SECMARK_LABELING
4042 * smack_sock_graft - Initialize a newly created socket with an existing sock
4044 * @parent: parent socket
4046 * Set the smk_{in,out} state of an existing sock based on the process that
4047 * is creating the new socket.
4049 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4051 struct socket_smack *ssp;
4052 struct smack_known *skp = smk_of_current();
4055 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4058 ssp = sk->sk_security;
4061 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4065 * smack_inet_conn_request - Smack access check on connect
4066 * @sk: socket involved
4070 * Returns 0 if a task with the packet label could write to
4071 * the socket, otherwise an error code
4073 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4074 struct request_sock *req)
4076 u16 family = sk->sk_family;
4077 struct smack_known *skp;
4078 struct socket_smack *ssp = sk->sk_security;
4079 struct netlbl_lsm_secattr secattr;
4080 struct sockaddr_in addr;
4082 struct smack_known *hskp;
4084 struct smk_audit_info ad;
4086 struct lsm_network_audit net;
4089 #if IS_ENABLED(CONFIG_IPV6)
4090 if (family == PF_INET6) {
4092 * Handle mapped IPv4 packets arriving
4093 * via IPv6 sockets. Don't set up netlabel
4094 * processing on IPv6.
4096 if (skb->protocol == htons(ETH_P_IP))
4101 #endif /* CONFIG_IPV6 */
4103 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4105 * If there is a secmark use it rather than the CIPSO label.
4106 * If there is no secmark fall back to CIPSO.
4107 * The secmark is assumed to reflect policy better.
4109 if (skb && skb->secmark != 0) {
4110 skp = smack_from_secid(skb->secmark);
4113 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4115 netlbl_secattr_init(&secattr);
4116 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4118 skp = smack_from_secattr(&secattr, ssp);
4120 skp = &smack_known_huh;
4121 netlbl_secattr_destroy(&secattr);
4123 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4128 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4129 ad.a.u.net->family = family;
4130 ad.a.u.net->netif = skb->skb_iif;
4131 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4134 * Receiving a packet requires that the other end be able to write
4135 * here. Read access is not required.
4137 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4138 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4143 * Save the peer's label in the request_sock so we can later setup
4144 * smk_packet in the child socket so that SO_PEERCRED can report it.
4146 req->peer_secid = skp->smk_secid;
4149 * We need to decide if we want to label the incoming connection here
4150 * if we do we only need to label the request_sock and the stack will
4151 * propagate the wire-label to the sock when it is created.
4154 addr.sin_addr.s_addr = hdr->saddr;
4156 hskp = smack_ipv4host_label(&addr);
4160 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4162 netlbl_req_delattr(req);
4168 * smack_inet_csk_clone - Copy the connection information to the new socket
4169 * @sk: the new socket
4170 * @req: the connection's request_sock
4172 * Transfer the connection's peer label to the newly created socket.
4174 static void smack_inet_csk_clone(struct sock *sk,
4175 const struct request_sock *req)
4177 struct socket_smack *ssp = sk->sk_security;
4178 struct smack_known *skp;
4180 if (req->peer_secid != 0) {
4181 skp = smack_from_secid(req->peer_secid);
4182 ssp->smk_packet = skp;
4184 ssp->smk_packet = NULL;
4188 * Key management security hooks
4190 * Casey has not tested key support very heavily.
4191 * The permission check is most likely too restrictive.
4192 * If you care about keys please have a look.
4197 * smack_key_alloc - Set the key security blob
4199 * @cred: the credentials to use
4202 * No allocation required
4206 static int smack_key_alloc(struct key *key, const struct cred *cred,
4207 unsigned long flags)
4209 struct smack_known *skp = smk_of_task(smack_cred(cred));
4211 key->security = skp;
4216 * smack_key_free - Clear the key security blob
4219 * Clear the blob pointer
4221 static void smack_key_free(struct key *key)
4223 key->security = NULL;
4227 * smack_key_permission - Smack access on a key
4228 * @key_ref: gets to the object
4229 * @cred: the credentials to use
4230 * @perm: requested key permissions
4232 * Return 0 if the task has read and write to the object,
4233 * an error code otherwise
4235 static int smack_key_permission(key_ref_t key_ref,
4236 const struct cred *cred, unsigned perm)
4239 struct smk_audit_info ad;
4240 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4245 * Validate requested permissions
4247 if (perm & ~KEY_NEED_ALL)
4250 keyp = key_ref_to_ptr(key_ref);
4254 * If the key hasn't been initialized give it access so that
4257 if (keyp->security == NULL)
4260 * This should not occur
4265 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4269 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4270 ad.a.u.key_struct.key = keyp->serial;
4271 ad.a.u.key_struct.key_desc = keyp->description;
4273 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4274 request |= MAY_READ;
4275 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4276 request |= MAY_WRITE;
4277 rc = smk_access(tkp, keyp->security, request, &ad);
4278 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4283 * smack_key_getsecurity - Smack label tagging the key
4284 * @key points to the key to be queried
4285 * @_buffer points to a pointer that should be set to point to the
4286 * resulting string (if no label or an error occurs).
4287 * Return the length of the string (including terminating NUL) or -ve if
4289 * May also return 0 (and a NULL buffer pointer) if there is no label.
4291 static int smack_key_getsecurity(struct key *key, char **_buffer)
4293 struct smack_known *skp = key->security;
4297 if (key->security == NULL) {
4302 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4305 length = strlen(copy) + 1;
4311 #endif /* CONFIG_KEYS */
4316 * Audit requires a unique representation of each Smack specific
4317 * rule. This unique representation is used to distinguish the
4318 * object to be audited from remaining kernel objects and also
4319 * works as a glue between the audit hooks.
4321 * Since repository entries are added but never deleted, we'll use
4322 * the smack_known label address related to the given audit rule as
4323 * the needed unique representation. This also better fits the smack
4324 * model where nearly everything is a label.
4329 * smack_audit_rule_init - Initialize a smack audit rule
4330 * @field: audit rule fields given from user-space (audit.h)
4331 * @op: required testing operator (=, !=, >, <, ...)
4332 * @rulestr: smack label to be audited
4333 * @vrule: pointer to save our own audit rule representation
4335 * Prepare to audit cases where (@field @op @rulestr) is true.
4336 * The label to be audited is created if necessay.
4338 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4340 struct smack_known *skp;
4341 char **rule = (char **)vrule;
4344 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4347 if (op != Audit_equal && op != Audit_not_equal)
4350 skp = smk_import_entry(rulestr, 0);
4352 return PTR_ERR(skp);
4354 *rule = skp->smk_known;
4360 * smack_audit_rule_known - Distinguish Smack audit rules
4361 * @krule: rule of interest, in Audit kernel representation format
4363 * This is used to filter Smack rules from remaining Audit ones.
4364 * If it's proved that this rule belongs to us, the
4365 * audit_rule_match hook will be called to do the final judgement.
4367 static int smack_audit_rule_known(struct audit_krule *krule)
4369 struct audit_field *f;
4372 for (i = 0; i < krule->field_count; i++) {
4373 f = &krule->fields[i];
4375 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4383 * smack_audit_rule_match - Audit given object ?
4384 * @secid: security id for identifying the object to test
4385 * @field: audit rule flags given from user-space
4386 * @op: required testing operator
4387 * @vrule: smack internal rule presentation
4389 * The core Audit hook. It's used to take the decision of
4390 * whether to audit or not to audit a given object.
4392 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4394 struct smack_known *skp;
4397 if (unlikely(!rule)) {
4398 WARN_ONCE(1, "Smack: missing rule\n");
4402 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4405 skp = smack_from_secid(secid);
4408 * No need to do string comparisons. If a match occurs,
4409 * both pointers will point to the same smack_known
4412 if (op == Audit_equal)
4413 return (rule == skp->smk_known);
4414 if (op == Audit_not_equal)
4415 return (rule != skp->smk_known);
4421 * There is no need for a smack_audit_rule_free hook.
4422 * No memory was allocated.
4425 #endif /* CONFIG_AUDIT */
4428 * smack_ismaclabel - check if xattr @name references a smack MAC label
4429 * @name: Full xattr name to check.
4431 static int smack_ismaclabel(const char *name)
4433 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4438 * smack_secid_to_secctx - return the smack label for a secid
4439 * @secid: incoming integer
4440 * @secdata: destination
4441 * @seclen: how long it is
4443 * Exists for networking code.
4445 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4447 struct smack_known *skp = smack_from_secid(secid);
4450 *secdata = skp->smk_known;
4451 *seclen = strlen(skp->smk_known);
4456 * smack_secctx_to_secid - return the secid for a smack label
4457 * @secdata: smack label
4458 * @seclen: how long result is
4459 * @secid: outgoing integer
4461 * Exists for audit and networking code.
4463 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4465 struct smack_known *skp = smk_find_entry(secdata);
4468 *secid = skp->smk_secid;
4475 * There used to be a smack_release_secctx hook
4476 * that did nothing back when hooks were in a vector.
4477 * Now that there's a list such a hook adds cost.
4480 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4482 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4485 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4487 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4490 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4492 struct smack_known *skp = smk_of_inode(inode);
4494 *ctx = skp->smk_known;
4495 *ctxlen = strlen(skp->smk_known);
4499 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4502 struct task_smack *tsp;
4503 struct smack_known *skp;
4504 struct inode_smack *isp;
4505 struct cred *new_creds = *new;
4507 if (new_creds == NULL) {
4508 new_creds = prepare_creds();
4509 if (new_creds == NULL)
4513 tsp = smack_cred(new_creds);
4516 * Get label from overlay inode and set it in create_sid
4518 isp = smack_inode(d_inode(dentry->d_parent));
4519 skp = isp->smk_inode;
4520 tsp->smk_task = skp;
4525 static int smack_inode_copy_up_xattr(const char *name)
4528 * Return 1 if this is the smack access Smack attribute.
4530 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4536 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4538 const struct cred *old,
4541 struct task_smack *otsp = smack_cred(old);
4542 struct task_smack *ntsp = smack_cred(new);
4543 struct inode_smack *isp;
4547 * Use the process credential unless all of
4548 * the transmuting criteria are met
4550 ntsp->smk_task = otsp->smk_task;
4553 * the attribute of the containing directory
4555 isp = smack_inode(d_inode(dentry->d_parent));
4557 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4559 may = smk_access_entry(otsp->smk_task->smk_known,
4560 isp->smk_inode->smk_known,
4561 &otsp->smk_task->smk_rules);
4565 * If the directory is transmuting and the rule
4566 * providing access is transmuting use the containing
4567 * directory label instead of the process label.
4569 if (may > 0 && (may & MAY_TRANSMUTE))
4570 ntsp->smk_task = isp->smk_inode;
4575 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4576 .lbs_cred = sizeof(struct task_smack),
4577 .lbs_file = sizeof(struct smack_known *),
4578 .lbs_inode = sizeof(struct inode_smack),
4579 .lbs_ipc = sizeof(struct smack_known *),
4580 .lbs_msg_msg = sizeof(struct smack_known *),
4583 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4584 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4585 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4586 LSM_HOOK_INIT(syslog, smack_syslog),
4588 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4589 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4591 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4592 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4593 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4594 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4595 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4596 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4598 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4600 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4601 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4602 LSM_HOOK_INIT(inode_link, smack_inode_link),
4603 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4604 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4605 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4606 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4607 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4608 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4609 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4610 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4611 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4612 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4613 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4614 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4615 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4616 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4618 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4619 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4620 LSM_HOOK_INIT(file_lock, smack_file_lock),
4621 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4622 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4623 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4624 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4625 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4626 LSM_HOOK_INIT(file_receive, smack_file_receive),
4628 LSM_HOOK_INIT(file_open, smack_file_open),
4630 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4631 LSM_HOOK_INIT(cred_free, smack_cred_free),
4632 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4633 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4634 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4635 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4636 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4637 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4638 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4639 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4640 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4641 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4642 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4643 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4644 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4645 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4646 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4647 LSM_HOOK_INIT(task_kill, smack_task_kill),
4648 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4650 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4651 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4653 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4655 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4656 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4657 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4658 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4659 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4661 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4662 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4663 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4664 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4666 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4667 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4668 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4669 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4671 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4673 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4674 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4676 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4677 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4679 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4680 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4681 #ifdef SMACK_IPV6_PORT_LABELING
4682 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4684 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4685 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4686 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4687 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4688 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4689 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4690 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4691 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4692 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4693 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4695 /* key management security hooks */
4697 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4698 LSM_HOOK_INIT(key_free, smack_key_free),
4699 LSM_HOOK_INIT(key_permission, smack_key_permission),
4700 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4701 #endif /* CONFIG_KEYS */
4705 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4706 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4707 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4708 #endif /* CONFIG_AUDIT */
4710 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4711 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4712 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4713 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4714 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4715 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4716 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4717 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4718 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4722 static __init void init_smack_known_list(void)
4725 * Initialize rule list locks
4727 mutex_init(&smack_known_huh.smk_rules_lock);
4728 mutex_init(&smack_known_hat.smk_rules_lock);
4729 mutex_init(&smack_known_floor.smk_rules_lock);
4730 mutex_init(&smack_known_star.smk_rules_lock);
4731 mutex_init(&smack_known_web.smk_rules_lock);
4733 * Initialize rule lists
4735 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4736 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4737 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4738 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4739 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4741 * Create the known labels list
4743 smk_insert_entry(&smack_known_huh);
4744 smk_insert_entry(&smack_known_hat);
4745 smk_insert_entry(&smack_known_star);
4746 smk_insert_entry(&smack_known_floor);
4747 smk_insert_entry(&smack_known_web);
4751 * smack_init - initialize the smack system
4753 * Returns 0 on success, -ENOMEM is there's no memory
4755 static __init int smack_init(void)
4757 struct cred *cred = (struct cred *) current->cred;
4758 struct task_smack *tsp;
4760 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4761 if (!smack_inode_cache)
4764 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4765 if (!smack_rule_cache) {
4766 kmem_cache_destroy(smack_inode_cache);
4771 * Set the security state for the initial task.
4773 tsp = smack_cred(cred);
4774 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4779 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4782 pr_info("Smack: Initializing.\n");
4783 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4784 pr_info("Smack: Netfilter enabled.\n");
4786 #ifdef SMACK_IPV6_PORT_LABELING
4787 pr_info("Smack: IPv6 port labeling enabled.\n");
4789 #ifdef SMACK_IPV6_SECMARK_LABELING
4790 pr_info("Smack: IPv6 Netfilter enabled.\n");
4793 /* initialize the smack_known_list */
4794 init_smack_known_list();
4800 * Smack requires early initialization in order to label
4801 * all processes and objects when they are created.
4803 DEFINE_LSM(smack) = {
4805 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4806 .blobs = &smack_blob_sizes,