1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
8 * Casey Schaufler <casey@schaufler-ca.com>
9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
13 * Paul Moore <paul@paul-moore.com>
14 * Copyright (C) 2010 Nokia Corporation
15 * Copyright (C) 2011 Intel Corporation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
23 #include <asm/ioctls.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/dccp.h>
28 #include <linux/icmpv6.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
38 #include <linux/msg.h>
39 #include <linux/shm.h>
40 #include <linux/binfmts.h>
41 #include <linux/parser.h>
42 #include <linux/fs_context.h>
43 #include <linux/fs_parser.h>
46 #define TRANS_TRUE "TRUE"
47 #define TRANS_TRUE_SIZE 4
49 #define SMK_CONNECTING 0
50 #define SMK_RECEIVING 1
53 static DEFINE_MUTEX(smack_ipv6_lock);
54 static LIST_HEAD(smk_ipv6_port_list);
55 struct kmem_cache *smack_rule_cache;
58 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
63 } smk_mount_opts[] = {
64 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
65 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
69 static int match_opt_prefix(char *s, int l, char **arg)
73 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
74 size_t len = smk_mount_opts[i].len;
75 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
77 if (len == l || s[len] != '=')
80 return smk_mount_opts[i].opt;
85 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
86 static char *smk_bu_mess[] = {
87 "Bringup Error", /* Unused */
88 "Bringup", /* SMACK_BRINGUP_ALLOW */
89 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
90 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
93 static void smk_bu_mode(int mode, char *s)
103 if (mode & MAY_APPEND)
105 if (mode & MAY_TRANSMUTE)
115 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
116 static int smk_bu_note(char *note, struct smack_known *sskp,
117 struct smack_known *oskp, int mode, int rc)
119 char acc[SMK_NUM_ACCESS_TYPE + 1];
123 if (rc > SMACK_UNCONFINED_OBJECT)
126 smk_bu_mode(mode, acc);
127 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
128 sskp->smk_known, oskp->smk_known, acc, note);
132 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
135 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
136 static int smk_bu_current(char *note, struct smack_known *oskp,
139 struct task_smack *tsp = smack_cred(current_cred());
140 char acc[SMK_NUM_ACCESS_TYPE + 1];
144 if (rc > SMACK_UNCONFINED_OBJECT)
147 smk_bu_mode(mode, acc);
148 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
149 tsp->smk_task->smk_known, oskp->smk_known,
150 acc, current->comm, note);
154 #define smk_bu_current(note, oskp, mode, RC) (RC)
157 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
158 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
160 struct task_smack *tsp = smack_cred(current_cred());
161 struct smack_known *smk_task = smk_of_task_struct(otp);
162 char acc[SMK_NUM_ACCESS_TYPE + 1];
166 if (rc > SMACK_UNCONFINED_OBJECT)
169 smk_bu_mode(mode, acc);
170 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
171 tsp->smk_task->smk_known, smk_task->smk_known, acc,
172 current->comm, otp->comm);
176 #define smk_bu_task(otp, mode, RC) (RC)
179 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
180 static int smk_bu_inode(struct inode *inode, int mode, int rc)
182 struct task_smack *tsp = smack_cred(current_cred());
183 struct inode_smack *isp = smack_inode(inode);
184 char acc[SMK_NUM_ACCESS_TYPE + 1];
186 if (isp->smk_flags & SMK_INODE_IMPURE)
187 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
188 inode->i_sb->s_id, inode->i_ino, current->comm);
192 if (rc > SMACK_UNCONFINED_OBJECT)
194 if (rc == SMACK_UNCONFINED_SUBJECT &&
195 (mode & (MAY_WRITE | MAY_APPEND)))
196 isp->smk_flags |= SMK_INODE_IMPURE;
198 smk_bu_mode(mode, acc);
200 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
201 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
202 inode->i_sb->s_id, inode->i_ino, current->comm);
206 #define smk_bu_inode(inode, mode, RC) (RC)
209 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
210 static int smk_bu_file(struct file *file, int mode, int rc)
212 struct task_smack *tsp = smack_cred(current_cred());
213 struct smack_known *sskp = tsp->smk_task;
214 struct inode *inode = file_inode(file);
215 struct inode_smack *isp = smack_inode(inode);
216 char acc[SMK_NUM_ACCESS_TYPE + 1];
218 if (isp->smk_flags & SMK_INODE_IMPURE)
219 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
220 inode->i_sb->s_id, inode->i_ino, current->comm);
224 if (rc > SMACK_UNCONFINED_OBJECT)
227 smk_bu_mode(mode, acc);
228 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
229 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
230 inode->i_sb->s_id, inode->i_ino, file,
235 #define smk_bu_file(file, mode, RC) (RC)
238 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
239 static int smk_bu_credfile(const struct cred *cred, struct file *file,
242 struct task_smack *tsp = smack_cred(cred);
243 struct smack_known *sskp = tsp->smk_task;
244 struct inode *inode = file_inode(file);
245 struct inode_smack *isp = smack_inode(inode);
246 char acc[SMK_NUM_ACCESS_TYPE + 1];
248 if (isp->smk_flags & SMK_INODE_IMPURE)
249 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
250 inode->i_sb->s_id, inode->i_ino, current->comm);
254 if (rc > SMACK_UNCONFINED_OBJECT)
257 smk_bu_mode(mode, acc);
258 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
259 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
260 inode->i_sb->s_id, inode->i_ino, file,
265 #define smk_bu_credfile(cred, file, mode, RC) (RC)
269 * smk_fetch - Fetch the smack label from a file.
270 * @name: type of the label (attribute)
271 * @ip: a pointer to the inode
272 * @dp: a pointer to the dentry
274 * Returns a pointer to the master list entry for the Smack label,
275 * NULL if there was no label to fetch, or an error code.
277 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
282 struct smack_known *skp = NULL;
284 if (!(ip->i_opflags & IOP_XATTR))
285 return ERR_PTR(-EOPNOTSUPP);
287 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
289 return ERR_PTR(-ENOMEM);
291 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
297 skp = smk_import_entry(buffer, rc);
305 * init_inode_smack - initialize an inode security blob
306 * @inode: inode to extract the info from
307 * @skp: a pointer to the Smack label entry to use in the blob
310 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
312 struct inode_smack *isp = smack_inode(inode);
314 isp->smk_inode = skp;
319 * init_task_smack - initialize a task security blob
320 * @tsp: blob to initialize
321 * @task: a pointer to the Smack label for the running task
322 * @forked: a pointer to the Smack label for the forked task
325 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
326 struct smack_known *forked)
328 tsp->smk_task = task;
329 tsp->smk_forked = forked;
330 INIT_LIST_HEAD(&tsp->smk_rules);
331 INIT_LIST_HEAD(&tsp->smk_relabel);
332 mutex_init(&tsp->smk_rules_lock);
336 * smk_copy_rules - copy a rule set
337 * @nhead: new rules header pointer
338 * @ohead: old rules header pointer
339 * @gfp: type of the memory for the allocation
341 * Returns 0 on success, -ENOMEM on error
343 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
346 struct smack_rule *nrp;
347 struct smack_rule *orp;
350 list_for_each_entry_rcu(orp, ohead, list) {
351 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
357 list_add_rcu(&nrp->list, nhead);
363 * smk_copy_relabel - copy smk_relabel labels list
364 * @nhead: new rules header pointer
365 * @ohead: old rules header pointer
366 * @gfp: type of the memory for the allocation
368 * Returns 0 on success, -ENOMEM on error
370 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
373 struct smack_known_list_elem *nklep;
374 struct smack_known_list_elem *oklep;
376 list_for_each_entry(oklep, ohead, list) {
377 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
379 smk_destroy_label_list(nhead);
382 nklep->smk_label = oklep->smk_label;
383 list_add(&nklep->list, nhead);
390 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
391 * @mode - input mode in form of PTRACE_MODE_*
393 * Returns a converted MAY_* mode usable by smack rules
395 static inline unsigned int smk_ptrace_mode(unsigned int mode)
397 if (mode & PTRACE_MODE_ATTACH)
398 return MAY_READWRITE;
399 if (mode & PTRACE_MODE_READ)
406 * smk_ptrace_rule_check - helper for ptrace access
407 * @tracer: tracer process
408 * @tracee_known: label entry of the process that's about to be traced
409 * @mode: ptrace attachment mode (PTRACE_MODE_*)
410 * @func: name of the function that called us, used for audit
412 * Returns 0 on access granted, -error on error
414 static int smk_ptrace_rule_check(struct task_struct *tracer,
415 struct smack_known *tracee_known,
416 unsigned int mode, const char *func)
419 struct smk_audit_info ad, *saip = NULL;
420 struct task_smack *tsp;
421 struct smack_known *tracer_known;
422 const struct cred *tracercred;
424 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
425 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
426 smk_ad_setfield_u_tsk(&ad, tracer);
431 tracercred = __task_cred(tracer);
432 tsp = smack_cred(tracercred);
433 tracer_known = smk_of_task(tsp);
435 if ((mode & PTRACE_MODE_ATTACH) &&
436 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
437 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
438 if (tracer_known->smk_known == tracee_known->smk_known)
440 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
442 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
448 smack_log(tracer_known->smk_known,
449 tracee_known->smk_known,
456 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
457 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
465 * We he, that is fun!
469 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
470 * @ctp: child task pointer
471 * @mode: ptrace attachment mode (PTRACE_MODE_*)
473 * Returns 0 if access is OK, an error code otherwise
475 * Do the capability checks.
477 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
479 struct smack_known *skp;
481 skp = smk_of_task_struct(ctp);
483 return smk_ptrace_rule_check(current, skp, mode, __func__);
487 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
488 * @ptp: parent task pointer
490 * Returns 0 if access is OK, an error code otherwise
492 * Do the capability checks, and require PTRACE_MODE_ATTACH.
494 static int smack_ptrace_traceme(struct task_struct *ptp)
497 struct smack_known *skp;
499 skp = smk_of_task(smack_cred(current_cred()));
501 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
506 * smack_syslog - Smack approval on syslog
507 * @typefrom_file: unused
509 * Returns 0 on success, error code otherwise.
511 static int smack_syslog(int typefrom_file)
514 struct smack_known *skp = smk_of_current();
516 if (smack_privileged(CAP_MAC_OVERRIDE))
519 if (smack_syslog_label != NULL && smack_syslog_label != skp)
530 * smack_sb_alloc_security - allocate a superblock blob
531 * @sb: the superblock getting the blob
533 * Returns 0 on success or -ENOMEM on error.
535 static int smack_sb_alloc_security(struct super_block *sb)
537 struct superblock_smack *sbsp;
539 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
544 sbsp->smk_root = &smack_known_floor;
545 sbsp->smk_default = &smack_known_floor;
546 sbsp->smk_floor = &smack_known_floor;
547 sbsp->smk_hat = &smack_known_hat;
549 * SMK_SB_INITIALIZED will be zero from kzalloc.
551 sb->s_security = sbsp;
557 * smack_sb_free_security - free a superblock blob
558 * @sb: the superblock getting the blob
561 static void smack_sb_free_security(struct super_block *sb)
563 kfree(sb->s_security);
564 sb->s_security = NULL;
567 struct smack_mnt_opts {
568 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
571 static void smack_free_mnt_opts(void *mnt_opts)
573 struct smack_mnt_opts *opts = mnt_opts;
574 kfree(opts->fsdefault);
575 kfree(opts->fsfloor);
578 kfree(opts->fstransmute);
582 static int smack_add_opt(int token, const char *s, void **mnt_opts)
584 struct smack_mnt_opts *opts = *mnt_opts;
587 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
616 case Opt_fstransmute:
617 if (opts->fstransmute)
619 opts->fstransmute = s;
625 pr_warn("Smack: duplicate mount options\n");
630 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
631 * @fc: The new filesystem context.
632 * @src_fc: The source filesystem context being duplicated.
634 * Returns 0 on success or -ENOMEM on error.
636 static int smack_fs_context_dup(struct fs_context *fc,
637 struct fs_context *src_fc)
639 struct smack_mnt_opts *dst, *src = src_fc->security;
644 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
649 if (src->fsdefault) {
650 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
655 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
660 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
665 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
669 if (src->fstransmute) {
670 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
671 if (!dst->fstransmute)
677 static const struct fs_parameter_spec smack_fs_parameters[] = {
678 fsparam_string("smackfsdef", Opt_fsdefault),
679 fsparam_string("smackfsdefault", Opt_fsdefault),
680 fsparam_string("smackfsfloor", Opt_fsfloor),
681 fsparam_string("smackfshat", Opt_fshat),
682 fsparam_string("smackfsroot", Opt_fsroot),
683 fsparam_string("smackfstransmute", Opt_fstransmute),
688 * smack_fs_context_parse_param - Parse a single mount parameter
689 * @fc: The new filesystem context being constructed.
690 * @param: The parameter.
692 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
695 static int smack_fs_context_parse_param(struct fs_context *fc,
696 struct fs_parameter *param)
698 struct fs_parse_result result;
701 opt = fs_parse(fc, smack_fs_parameters, param, &result);
705 rc = smack_add_opt(opt, param->string, &fc->security);
707 param->string = NULL;
711 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
713 char *from = options, *to = options;
717 char *next = strchr(from, ',');
726 token = match_opt_prefix(from, len, &arg);
727 if (token != Opt_error) {
728 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
729 rc = smack_add_opt(token, arg, mnt_opts);
733 smack_free_mnt_opts(*mnt_opts);
738 if (!first) { // copy with preceding comma
743 memmove(to, from, len);
756 * smack_set_mnt_opts - set Smack specific mount options
757 * @sb: the file system superblock
758 * @mnt_opts: Smack mount options
759 * @kern_flags: mount option from kernel space or user space
760 * @set_kern_flags: where to store converted mount opts
762 * Returns 0 on success, an error code on failure
764 * Allow filesystems with binary mount data to explicitly set Smack mount
767 static int smack_set_mnt_opts(struct super_block *sb,
769 unsigned long kern_flags,
770 unsigned long *set_kern_flags)
772 struct dentry *root = sb->s_root;
773 struct inode *inode = d_backing_inode(root);
774 struct superblock_smack *sp = sb->s_security;
775 struct inode_smack *isp;
776 struct smack_known *skp;
777 struct smack_mnt_opts *opts = mnt_opts;
778 bool transmute = false;
780 if (sp->smk_flags & SMK_SB_INITIALIZED)
783 if (inode->i_security == NULL) {
784 int rc = lsm_inode_alloc(inode);
790 if (!smack_privileged(CAP_MAC_ADMIN)) {
792 * Unprivileged mounts don't get to specify Smack values.
797 * Unprivileged mounts get root and default from the caller.
799 skp = smk_of_current();
801 sp->smk_default = skp;
803 * For a handful of fs types with no user-controlled
804 * backing store it's okay to trust security labels
805 * in the filesystem. The rest are untrusted.
807 if (sb->s_user_ns != &init_user_ns &&
808 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
809 sb->s_magic != RAMFS_MAGIC) {
811 sp->smk_flags |= SMK_SB_UNTRUSTED;
815 sp->smk_flags |= SMK_SB_INITIALIZED;
818 if (opts->fsdefault) {
819 skp = smk_import_entry(opts->fsdefault, 0);
822 sp->smk_default = skp;
825 skp = smk_import_entry(opts->fsfloor, 0);
831 skp = smk_import_entry(opts->fshat, 0);
837 skp = smk_import_entry(opts->fsroot, 0);
842 if (opts->fstransmute) {
843 skp = smk_import_entry(opts->fstransmute, 0);
852 * Initialize the root inode.
854 init_inode_smack(inode, sp->smk_root);
857 isp = smack_inode(inode);
858 isp->smk_flags |= SMK_INODE_TRANSMUTE;
865 * smack_sb_statfs - Smack check on statfs
866 * @dentry: identifies the file system in question
868 * Returns 0 if current can read the floor of the filesystem,
869 * and error code otherwise
871 static int smack_sb_statfs(struct dentry *dentry)
873 struct superblock_smack *sbp = dentry->d_sb->s_security;
875 struct smk_audit_info ad;
877 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
878 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
880 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
881 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
890 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
891 * @bprm: the exec information
893 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
895 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
897 struct inode *inode = file_inode(bprm->file);
898 struct task_smack *bsp = smack_cred(bprm->cred);
899 struct inode_smack *isp;
900 struct superblock_smack *sbsp;
903 isp = smack_inode(inode);
904 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
907 sbsp = inode->i_sb->s_security;
908 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
909 isp->smk_task != sbsp->smk_root)
912 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
913 struct task_struct *tracer;
917 tracer = ptrace_parent(current);
918 if (likely(tracer != NULL))
919 rc = smk_ptrace_rule_check(tracer,
928 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
931 bsp->smk_task = isp->smk_task;
932 bprm->per_clear |= PER_CLEAR_ON_SETID;
934 /* Decide if this is a secure exec. */
935 if (bsp->smk_task != bsp->smk_forked)
936 bprm->secureexec = 1;
946 * smack_inode_alloc_security - allocate an inode blob
947 * @inode: the inode in need of a blob
951 static int smack_inode_alloc_security(struct inode *inode)
953 struct smack_known *skp = smk_of_current();
955 init_inode_smack(inode, skp);
960 * smack_inode_init_security - copy out the smack from an inode
961 * @inode: the newly created inode
962 * @dir: containing directory object
964 * @name: where to put the attribute name
965 * @value: where to put the attribute value
966 * @len: where to put the length of the attribute
968 * Returns 0 if it all works out, -ENOMEM if there's no memory
970 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
971 const struct qstr *qstr, const char **name,
972 void **value, size_t *len)
974 struct inode_smack *issp = smack_inode(inode);
975 struct smack_known *skp = smk_of_current();
976 struct smack_known *isp = smk_of_inode(inode);
977 struct smack_known *dsp = smk_of_inode(dir);
981 *name = XATTR_SMACK_SUFFIX;
985 may = smk_access_entry(skp->smk_known, dsp->smk_known,
990 * If the access rule allows transmutation and
991 * the directory requests transmutation then
992 * by all means transmute.
993 * Mark the inode as changed.
995 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
996 smk_inode_transmutable(dir)) {
998 issp->smk_flags |= SMK_INODE_CHANGED;
1001 *value = kstrdup(isp->smk_known, GFP_NOFS);
1005 *len = strlen(isp->smk_known);
1012 * smack_inode_link - Smack check on link
1013 * @old_dentry: the existing object
1015 * @new_dentry: the new object
1017 * Returns 0 if access is permitted, an error code otherwise
1019 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1020 struct dentry *new_dentry)
1022 struct smack_known *isp;
1023 struct smk_audit_info ad;
1026 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1027 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1029 isp = smk_of_inode(d_backing_inode(old_dentry));
1030 rc = smk_curacc(isp, MAY_WRITE, &ad);
1031 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1033 if (rc == 0 && d_is_positive(new_dentry)) {
1034 isp = smk_of_inode(d_backing_inode(new_dentry));
1035 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1036 rc = smk_curacc(isp, MAY_WRITE, &ad);
1037 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1044 * smack_inode_unlink - Smack check on inode deletion
1045 * @dir: containing directory object
1046 * @dentry: file to unlink
1048 * Returns 0 if current can write the containing directory
1049 * and the object, error code otherwise
1051 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1053 struct inode *ip = d_backing_inode(dentry);
1054 struct smk_audit_info ad;
1057 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1058 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1061 * You need write access to the thing you're unlinking
1063 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1064 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1067 * You also need write access to the containing directory
1069 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1070 smk_ad_setfield_u_fs_inode(&ad, dir);
1071 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1072 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1078 * smack_inode_rmdir - Smack check on directory deletion
1079 * @dir: containing directory object
1080 * @dentry: directory to unlink
1082 * Returns 0 if current can write the containing directory
1083 * and the directory, error code otherwise
1085 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1087 struct smk_audit_info ad;
1090 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1091 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1094 * You need write access to the thing you're removing
1096 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1097 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1100 * You also need write access to the containing directory
1102 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1103 smk_ad_setfield_u_fs_inode(&ad, dir);
1104 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1105 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1112 * smack_inode_rename - Smack check on rename
1113 * @old_inode: unused
1114 * @old_dentry: the old object
1115 * @new_inode: unused
1116 * @new_dentry: the new object
1118 * Read and write access is required on both the old and
1121 * Returns 0 if access is permitted, an error code otherwise
1123 static int smack_inode_rename(struct inode *old_inode,
1124 struct dentry *old_dentry,
1125 struct inode *new_inode,
1126 struct dentry *new_dentry)
1129 struct smack_known *isp;
1130 struct smk_audit_info ad;
1132 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1133 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1135 isp = smk_of_inode(d_backing_inode(old_dentry));
1136 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1137 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1139 if (rc == 0 && d_is_positive(new_dentry)) {
1140 isp = smk_of_inode(d_backing_inode(new_dentry));
1141 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1142 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1143 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1149 * smack_inode_permission - Smack version of permission()
1150 * @inode: the inode in question
1151 * @mask: the access requested
1153 * This is the important Smack hook.
1155 * Returns 0 if access is permitted, an error code otherwise
1157 static int smack_inode_permission(struct inode *inode, int mask)
1159 struct superblock_smack *sbsp = inode->i_sb->s_security;
1160 struct smk_audit_info ad;
1161 int no_block = mask & MAY_NOT_BLOCK;
1164 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1166 * No permission to check. Existence test. Yup, it's there.
1171 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1172 if (smk_of_inode(inode) != sbsp->smk_root)
1176 /* May be droppable after audit */
1179 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1180 smk_ad_setfield_u_fs_inode(&ad, inode);
1181 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1182 rc = smk_bu_inode(inode, mask, rc);
1187 * smack_inode_setattr - Smack check for setting attributes
1188 * @dentry: the object
1189 * @iattr: for the force flag
1191 * Returns 0 if access is permitted, an error code otherwise
1193 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1195 struct smk_audit_info ad;
1199 * Need to allow for clearing the setuid bit.
1201 if (iattr->ia_valid & ATTR_FORCE)
1203 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1204 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1206 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1207 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1212 * smack_inode_getattr - Smack check for getting attributes
1213 * @path: path to extract the info from
1215 * Returns 0 if access is permitted, an error code otherwise
1217 static int smack_inode_getattr(const struct path *path)
1219 struct smk_audit_info ad;
1220 struct inode *inode = d_backing_inode(path->dentry);
1223 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1224 smk_ad_setfield_u_fs_path(&ad, *path);
1225 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1226 rc = smk_bu_inode(inode, MAY_READ, rc);
1231 * smack_inode_setxattr - Smack check for setting xattrs
1232 * @dentry: the object
1233 * @name: name of the attribute
1234 * @value: value of the attribute
1235 * @size: size of the value
1238 * This protects the Smack attribute explicitly.
1240 * Returns 0 if access is permitted, an error code otherwise
1242 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1243 const void *value, size_t size, int flags)
1245 struct smk_audit_info ad;
1246 struct smack_known *skp;
1248 int check_import = 0;
1253 * Check label validity here so import won't fail in post_setxattr
1255 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1256 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1257 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1260 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1261 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1265 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1267 if (size != TRANS_TRUE_SIZE ||
1268 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1271 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1273 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1276 if (rc == 0 && check_import) {
1277 skp = size ? smk_import_entry(value, size) : NULL;
1280 else if (skp == NULL || (check_star &&
1281 (skp == &smack_known_star || skp == &smack_known_web)))
1285 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1286 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1289 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1290 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1297 * smack_inode_post_setxattr - Apply the Smack update approved above
1299 * @name: attribute name
1300 * @value: attribute value
1301 * @size: attribute size
1304 * Set the pointer in the inode blob to the entry found
1305 * in the master label list.
1307 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1308 const void *value, size_t size, int flags)
1310 struct smack_known *skp;
1311 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1313 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1314 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1318 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1319 skp = smk_import_entry(value, size);
1321 isp->smk_inode = skp;
1322 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1323 skp = smk_import_entry(value, size);
1325 isp->smk_task = skp;
1326 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1327 skp = smk_import_entry(value, size);
1329 isp->smk_mmap = skp;
1336 * smack_inode_getxattr - Smack check on getxattr
1337 * @dentry: the object
1340 * Returns 0 if access is permitted, an error code otherwise
1342 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1344 struct smk_audit_info ad;
1347 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1348 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1350 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1351 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1356 * smack_inode_removexattr - Smack check on removexattr
1357 * @dentry: the object
1358 * @name: name of the attribute
1360 * Removing the Smack attribute requires CAP_MAC_ADMIN
1362 * Returns 0 if access is permitted, an error code otherwise
1364 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1366 struct inode_smack *isp;
1367 struct smk_audit_info ad;
1370 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1371 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1372 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1373 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1374 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1375 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1376 if (!smack_privileged(CAP_MAC_ADMIN))
1379 rc = cap_inode_removexattr(dentry, name);
1384 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1385 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1387 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1388 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1392 isp = smack_inode(d_backing_inode(dentry));
1394 * Don't do anything special for these.
1395 * XATTR_NAME_SMACKIPIN
1396 * XATTR_NAME_SMACKIPOUT
1398 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1399 struct super_block *sbp = dentry->d_sb;
1400 struct superblock_smack *sbsp = sbp->s_security;
1402 isp->smk_inode = sbsp->smk_default;
1403 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1404 isp->smk_task = NULL;
1405 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1406 isp->smk_mmap = NULL;
1407 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1408 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1414 * smack_inode_getsecurity - get smack xattrs
1415 * @inode: the object
1416 * @name: attribute name
1417 * @buffer: where to put the result
1418 * @alloc: duplicate memory
1420 * Returns the size of the attribute or an error code
1422 static int smack_inode_getsecurity(struct inode *inode,
1423 const char *name, void **buffer,
1426 struct socket_smack *ssp;
1427 struct socket *sock;
1428 struct super_block *sbp;
1429 struct inode *ip = (struct inode *)inode;
1430 struct smack_known *isp;
1432 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1433 isp = smk_of_inode(inode);
1436 * The rest of the Smack xattrs are only on sockets.
1439 if (sbp->s_magic != SOCKFS_MAGIC)
1442 sock = SOCKET_I(ip);
1443 if (sock == NULL || sock->sk == NULL)
1446 ssp = sock->sk->sk_security;
1448 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1450 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1457 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1458 if (*buffer == NULL)
1462 return strlen(isp->smk_known);
1467 * smack_inode_listsecurity - list the Smack attributes
1468 * @inode: the object
1469 * @buffer: where they go
1470 * @buffer_size: size of buffer
1472 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1475 int len = sizeof(XATTR_NAME_SMACK);
1477 if (buffer != NULL && len <= buffer_size)
1478 memcpy(buffer, XATTR_NAME_SMACK, len);
1484 * smack_inode_getsecid - Extract inode's security id
1485 * @inode: inode to extract the info from
1486 * @secid: where result will be saved
1488 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1490 struct smack_known *skp = smk_of_inode(inode);
1492 *secid = skp->smk_secid;
1500 * There is no smack_file_permission hook
1502 * Should access checks be done on each read or write?
1503 * UNICOS and SELinux say yes.
1504 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1506 * I'll say no for now. Smack does not do the frequent
1507 * label changing that SELinux does.
1511 * smack_file_alloc_security - assign a file security blob
1514 * The security blob for a file is a pointer to the master
1515 * label list, so no allocation is done.
1517 * f_security is the owner security information. It
1518 * isn't used on file access checks, it's for send_sigio.
1522 static int smack_file_alloc_security(struct file *file)
1524 struct smack_known **blob = smack_file(file);
1526 *blob = smk_of_current();
1531 * smack_file_ioctl - Smack check on ioctls
1536 * Relies heavily on the correct use of the ioctl command conventions.
1538 * Returns 0 if allowed, error code otherwise
1540 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1544 struct smk_audit_info ad;
1545 struct inode *inode = file_inode(file);
1547 if (unlikely(IS_PRIVATE(inode)))
1550 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1551 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1553 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1554 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1555 rc = smk_bu_file(file, MAY_WRITE, rc);
1558 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1559 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1560 rc = smk_bu_file(file, MAY_READ, rc);
1567 * smack_file_lock - Smack check on file locking
1571 * Returns 0 if current has lock access, error code otherwise
1573 static int smack_file_lock(struct file *file, unsigned int cmd)
1575 struct smk_audit_info ad;
1577 struct inode *inode = file_inode(file);
1579 if (unlikely(IS_PRIVATE(inode)))
1582 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1583 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1584 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1585 rc = smk_bu_file(file, MAY_LOCK, rc);
1590 * smack_file_fcntl - Smack check on fcntl
1592 * @cmd: what action to check
1595 * Generally these operations are harmless.
1596 * File locking operations present an obvious mechanism
1597 * for passing information, so they require write access.
1599 * Returns 0 if current has access, error code otherwise
1601 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1604 struct smk_audit_info ad;
1606 struct inode *inode = file_inode(file);
1608 if (unlikely(IS_PRIVATE(inode)))
1616 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1617 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1618 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1619 rc = smk_bu_file(file, MAY_LOCK, rc);
1623 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1624 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1625 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1626 rc = smk_bu_file(file, MAY_WRITE, rc);
1637 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1638 * if mapping anonymous memory.
1639 * @file contains the file structure for file to map (may be NULL).
1640 * @reqprot contains the protection requested by the application.
1641 * @prot contains the protection that will be applied by the kernel.
1642 * @flags contains the operational flags.
1643 * Return 0 if permission is granted.
1645 static int smack_mmap_file(struct file *file,
1646 unsigned long reqprot, unsigned long prot,
1647 unsigned long flags)
1649 struct smack_known *skp;
1650 struct smack_known *mkp;
1651 struct smack_rule *srp;
1652 struct task_smack *tsp;
1653 struct smack_known *okp;
1654 struct inode_smack *isp;
1655 struct superblock_smack *sbsp;
1664 if (unlikely(IS_PRIVATE(file_inode(file))))
1667 isp = smack_inode(file_inode(file));
1668 if (isp->smk_mmap == NULL)
1670 sbsp = file_inode(file)->i_sb->s_security;
1671 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1672 isp->smk_mmap != sbsp->smk_root)
1674 mkp = isp->smk_mmap;
1676 tsp = smack_cred(current_cred());
1677 skp = smk_of_current();
1682 * For each Smack rule associated with the subject
1683 * label verify that the SMACK64MMAP also has access
1684 * to that rule's object label.
1686 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1687 okp = srp->smk_object;
1689 * Matching labels always allows access.
1691 if (mkp->smk_known == okp->smk_known)
1694 * If there is a matching local rule take
1695 * that into account as well.
1697 may = smk_access_entry(srp->smk_subject->smk_known,
1701 may = srp->smk_access;
1703 may &= srp->smk_access;
1705 * If may is zero the SMACK64MMAP subject can't
1706 * possibly have less access.
1712 * Fetch the global list entry.
1713 * If there isn't one a SMACK64MMAP subject
1714 * can't have as much access as current.
1716 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1718 if (mmay == -ENOENT) {
1723 * If there is a local entry it modifies the
1724 * potential access, too.
1726 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1728 if (tmay != -ENOENT)
1732 * If there is any access available to current that is
1733 * not available to a SMACK64MMAP subject
1736 if ((may | mmay) != mmay) {
1748 * smack_file_set_fowner - set the file security blob value
1749 * @file: object in question
1752 static void smack_file_set_fowner(struct file *file)
1754 struct smack_known **blob = smack_file(file);
1756 *blob = smk_of_current();
1760 * smack_file_send_sigiotask - Smack on sigio
1761 * @tsk: The target task
1762 * @fown: the object the signal come from
1765 * Allow a privileged task to get signals even if it shouldn't
1767 * Returns 0 if a subject with the object's smack could
1768 * write to the task, an error code otherwise.
1770 static int smack_file_send_sigiotask(struct task_struct *tsk,
1771 struct fown_struct *fown, int signum)
1773 struct smack_known **blob;
1774 struct smack_known *skp;
1775 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1776 const struct cred *tcred;
1779 struct smk_audit_info ad;
1782 * struct fown_struct is never outside the context of a struct file
1784 file = container_of(fown, struct file, f_owner);
1786 /* we don't log here as rc can be overriden */
1787 blob = smack_file(file);
1789 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1790 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1793 tcred = __task_cred(tsk);
1794 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1798 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1799 smk_ad_setfield_u_tsk(&ad, tsk);
1800 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1805 * smack_file_receive - Smack file receive check
1808 * Returns 0 if current has access, error code otherwise
1810 static int smack_file_receive(struct file *file)
1814 struct smk_audit_info ad;
1815 struct inode *inode = file_inode(file);
1816 struct socket *sock;
1817 struct task_smack *tsp;
1818 struct socket_smack *ssp;
1820 if (unlikely(IS_PRIVATE(inode)))
1823 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1824 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1826 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1827 sock = SOCKET_I(inode);
1828 ssp = sock->sk->sk_security;
1829 tsp = smack_cred(current_cred());
1831 * If the receiving process can't write to the
1832 * passed socket or if the passed socket can't
1833 * write to the receiving process don't accept
1834 * the passed socket.
1836 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1837 rc = smk_bu_file(file, may, rc);
1840 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1841 rc = smk_bu_file(file, may, rc);
1845 * This code relies on bitmasks.
1847 if (file->f_mode & FMODE_READ)
1849 if (file->f_mode & FMODE_WRITE)
1852 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1853 rc = smk_bu_file(file, may, rc);
1858 * smack_file_open - Smack dentry open processing
1861 * Set the security blob in the file structure.
1862 * Allow the open only if the task has read access. There are
1863 * many read operations (e.g. fstat) that you can do with an
1864 * fd even if you have the file open write-only.
1866 * Returns 0 if current has access, error code otherwise
1868 static int smack_file_open(struct file *file)
1870 struct task_smack *tsp = smack_cred(file->f_cred);
1871 struct inode *inode = file_inode(file);
1872 struct smk_audit_info ad;
1875 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1876 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1877 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1878 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1888 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1889 * @cred: the new credentials
1890 * @gfp: the atomicity of any memory allocations
1892 * Prepare a blank set of credentials for modification. This must allocate all
1893 * the memory the LSM module might require such that cred_transfer() can
1894 * complete without error.
1896 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1898 init_task_smack(smack_cred(cred), NULL, NULL);
1904 * smack_cred_free - "free" task-level security credentials
1905 * @cred: the credentials in question
1908 static void smack_cred_free(struct cred *cred)
1910 struct task_smack *tsp = smack_cred(cred);
1911 struct smack_rule *rp;
1912 struct list_head *l;
1913 struct list_head *n;
1915 smk_destroy_label_list(&tsp->smk_relabel);
1917 list_for_each_safe(l, n, &tsp->smk_rules) {
1918 rp = list_entry(l, struct smack_rule, list);
1919 list_del(&rp->list);
1920 kmem_cache_free(smack_rule_cache, rp);
1925 * smack_cred_prepare - prepare new set of credentials for modification
1926 * @new: the new credentials
1927 * @old: the original credentials
1928 * @gfp: the atomicity of any memory allocations
1930 * Prepare a new set of credentials for modification.
1932 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1935 struct task_smack *old_tsp = smack_cred(old);
1936 struct task_smack *new_tsp = smack_cred(new);
1939 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1941 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1945 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1951 * smack_cred_transfer - Transfer the old credentials to the new credentials
1952 * @new: the new credentials
1953 * @old: the original credentials
1955 * Fill in a set of blank credentials from another set of credentials.
1957 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1959 struct task_smack *old_tsp = smack_cred(old);
1960 struct task_smack *new_tsp = smack_cred(new);
1962 new_tsp->smk_task = old_tsp->smk_task;
1963 new_tsp->smk_forked = old_tsp->smk_task;
1964 mutex_init(&new_tsp->smk_rules_lock);
1965 INIT_LIST_HEAD(&new_tsp->smk_rules);
1967 /* cbs copy rule list */
1971 * smack_cred_getsecid - get the secid corresponding to a creds structure
1972 * @cred: the object creds
1973 * @secid: where to put the result
1975 * Sets the secid to contain a u32 version of the smack label.
1977 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1979 struct smack_known *skp;
1982 skp = smk_of_task(smack_cred(cred));
1983 *secid = skp->smk_secid;
1988 * smack_kernel_act_as - Set the subjective context in a set of credentials
1989 * @new: points to the set of credentials to be modified.
1990 * @secid: specifies the security ID to be set
1992 * Set the security data for a kernel service.
1994 static int smack_kernel_act_as(struct cred *new, u32 secid)
1996 struct task_smack *new_tsp = smack_cred(new);
1998 new_tsp->smk_task = smack_from_secid(secid);
2003 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2004 * @new: points to the set of credentials to be modified
2005 * @inode: points to the inode to use as a reference
2007 * Set the file creation context in a set of credentials to the same
2008 * as the objective context of the specified inode
2010 static int smack_kernel_create_files_as(struct cred *new,
2011 struct inode *inode)
2013 struct inode_smack *isp = smack_inode(inode);
2014 struct task_smack *tsp = smack_cred(new);
2016 tsp->smk_forked = isp->smk_inode;
2017 tsp->smk_task = tsp->smk_forked;
2022 * smk_curacc_on_task - helper to log task related access
2023 * @p: the task object
2024 * @access: the access requested
2025 * @caller: name of the calling function for audit
2027 * Return 0 if access is permitted
2029 static int smk_curacc_on_task(struct task_struct *p, int access,
2032 struct smk_audit_info ad;
2033 struct smack_known *skp = smk_of_task_struct(p);
2036 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2037 smk_ad_setfield_u_tsk(&ad, p);
2038 rc = smk_curacc(skp, access, &ad);
2039 rc = smk_bu_task(p, access, rc);
2044 * smack_task_setpgid - Smack check on setting pgid
2045 * @p: the task object
2048 * Return 0 if write access is permitted
2050 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2052 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2056 * smack_task_getpgid - Smack access check for getpgid
2057 * @p: the object task
2059 * Returns 0 if current can read the object task, error code otherwise
2061 static int smack_task_getpgid(struct task_struct *p)
2063 return smk_curacc_on_task(p, MAY_READ, __func__);
2067 * smack_task_getsid - Smack access check for getsid
2068 * @p: the object task
2070 * Returns 0 if current can read the object task, error code otherwise
2072 static int smack_task_getsid(struct task_struct *p)
2074 return smk_curacc_on_task(p, MAY_READ, __func__);
2078 * smack_task_getsecid - get the secid of the task
2079 * @p: the object task
2080 * @secid: where to put the result
2082 * Sets the secid to contain a u32 version of the smack label.
2084 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2086 struct smack_known *skp = smk_of_task_struct(p);
2088 *secid = skp->smk_secid;
2092 * smack_task_setnice - Smack check on setting nice
2093 * @p: the task object
2096 * Return 0 if write access is permitted
2098 static int smack_task_setnice(struct task_struct *p, int nice)
2100 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2104 * smack_task_setioprio - Smack check on setting ioprio
2105 * @p: the task object
2108 * Return 0 if write access is permitted
2110 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2112 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2116 * smack_task_getioprio - Smack check on reading ioprio
2117 * @p: the task object
2119 * Return 0 if read access is permitted
2121 static int smack_task_getioprio(struct task_struct *p)
2123 return smk_curacc_on_task(p, MAY_READ, __func__);
2127 * smack_task_setscheduler - Smack check on setting scheduler
2128 * @p: the task object
2130 * Return 0 if read access is permitted
2132 static int smack_task_setscheduler(struct task_struct *p)
2134 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2138 * smack_task_getscheduler - Smack check on reading scheduler
2139 * @p: the task object
2141 * Return 0 if read access is permitted
2143 static int smack_task_getscheduler(struct task_struct *p)
2145 return smk_curacc_on_task(p, MAY_READ, __func__);
2149 * smack_task_movememory - Smack check on moving memory
2150 * @p: the task object
2152 * Return 0 if write access is permitted
2154 static int smack_task_movememory(struct task_struct *p)
2156 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2160 * smack_task_kill - Smack check on signal delivery
2161 * @p: the task object
2164 * @cred: identifies the cred to use in lieu of current's
2166 * Return 0 if write access is permitted
2169 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2170 int sig, const struct cred *cred)
2172 struct smk_audit_info ad;
2173 struct smack_known *skp;
2174 struct smack_known *tkp = smk_of_task_struct(p);
2178 return 0; /* null signal; existence test */
2180 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2181 smk_ad_setfield_u_tsk(&ad, p);
2183 * Sending a signal requires that the sender
2184 * can write the receiver.
2187 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2188 rc = smk_bu_task(p, MAY_DELIVER, rc);
2192 * If the cred isn't NULL we're dealing with some USB IO
2193 * specific behavior. This is not clean. For one thing
2194 * we can't take privilege into account.
2196 skp = smk_of_task(smack_cred(cred));
2197 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2198 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2203 * smack_task_to_inode - copy task smack into the inode blob
2204 * @p: task to copy from
2205 * @inode: inode to copy to
2207 * Sets the smack pointer in the inode security blob
2209 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2211 struct inode_smack *isp = smack_inode(inode);
2212 struct smack_known *skp = smk_of_task_struct(p);
2214 isp->smk_inode = skp;
2215 isp->smk_flags |= SMK_INODE_INSTANT;
2223 * smack_sk_alloc_security - Allocate a socket blob
2226 * @gfp_flags: memory allocation flags
2228 * Assign Smack pointers to current
2230 * Returns 0 on success, -ENOMEM is there's no memory
2232 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2234 struct smack_known *skp = smk_of_current();
2235 struct socket_smack *ssp;
2237 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2242 * Sockets created by kernel threads receive web label.
2244 if (unlikely(current->flags & PF_KTHREAD)) {
2245 ssp->smk_in = &smack_known_web;
2246 ssp->smk_out = &smack_known_web;
2251 ssp->smk_packet = NULL;
2253 sk->sk_security = ssp;
2259 * smack_sk_free_security - Free a socket blob
2262 * Clears the blob pointer
2264 static void smack_sk_free_security(struct sock *sk)
2266 #ifdef SMACK_IPV6_PORT_LABELING
2267 struct smk_port_label *spp;
2269 if (sk->sk_family == PF_INET6) {
2271 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2272 if (spp->smk_sock != sk)
2274 spp->smk_can_reuse = 1;
2280 kfree(sk->sk_security);
2284 * smack_ipv4host_label - check host based restrictions
2285 * @sip: the object end
2287 * looks for host based access restrictions
2289 * This version will only be appropriate for really small sets of single label
2290 * hosts. The caller is responsible for ensuring that the RCU read lock is
2291 * taken before calling this function.
2293 * Returns the label of the far end or NULL if it's not special.
2295 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2297 struct smk_net4addr *snp;
2298 struct in_addr *siap = &sip->sin_addr;
2300 if (siap->s_addr == 0)
2303 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2305 * we break after finding the first match because
2306 * the list is sorted from longest to shortest mask
2307 * so we have found the most specific match
2309 if (snp->smk_host.s_addr ==
2310 (siap->s_addr & snp->smk_mask.s_addr))
2311 return snp->smk_label;
2317 * smk_ipv6_localhost - Check for local ipv6 host address
2320 * Returns boolean true if this is the localhost address
2322 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2324 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2325 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2327 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2328 ntohs(be16p[7]) == 1)
2334 * smack_ipv6host_label - check host based restrictions
2335 * @sip: the object end
2337 * looks for host based access restrictions
2339 * This version will only be appropriate for really small sets of single label
2340 * hosts. The caller is responsible for ensuring that the RCU read lock is
2341 * taken before calling this function.
2343 * Returns the label of the far end or NULL if it's not special.
2345 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2347 struct smk_net6addr *snp;
2348 struct in6_addr *sap = &sip->sin6_addr;
2353 * It's local. Don't look for a host label.
2355 if (smk_ipv6_localhost(sip))
2358 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2360 * If the label is NULL the entry has
2361 * been renounced. Ignore it.
2363 if (snp->smk_label == NULL)
2366 * we break after finding the first match because
2367 * the list is sorted from longest to shortest mask
2368 * so we have found the most specific match
2370 for (found = 1, i = 0; i < 8; i++) {
2371 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2372 snp->smk_host.s6_addr16[i]) {
2378 return snp->smk_label;
2385 * smack_netlabel - Set the secattr on a socket
2387 * @labeled: socket label scheme
2389 * Convert the outbound smack value (smk_out) to a
2390 * secattr and attach it to the socket.
2392 * Returns 0 on success or an error code
2394 static int smack_netlabel(struct sock *sk, int labeled)
2396 struct smack_known *skp;
2397 struct socket_smack *ssp = sk->sk_security;
2401 * Usually the netlabel code will handle changing the
2402 * packet labeling based on the label.
2403 * The case of a single label host is different, because
2404 * a single label host should never get a labeled packet
2405 * even though the label is usually associated with a packet
2409 bh_lock_sock_nested(sk);
2411 if (ssp->smk_out == smack_net_ambient ||
2412 labeled == SMACK_UNLABELED_SOCKET)
2413 netlbl_sock_delattr(sk);
2416 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2426 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2428 * @sap: the destination address
2430 * Set the correct secattr for the given socket based on the destination
2431 * address and perform any outbound access checks needed.
2433 * Returns 0 on success or an error code.
2436 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2438 struct smack_known *skp;
2441 struct smack_known *hkp;
2442 struct socket_smack *ssp = sk->sk_security;
2443 struct smk_audit_info ad;
2446 hkp = smack_ipv4host_label(sap);
2449 struct lsm_network_audit net;
2451 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2452 ad.a.u.net->family = sap->sin_family;
2453 ad.a.u.net->dport = sap->sin_port;
2454 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2456 sk_lbl = SMACK_UNLABELED_SOCKET;
2458 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2459 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2461 sk_lbl = SMACK_CIPSO_SOCKET;
2468 return smack_netlabel(sk, sk_lbl);
2472 * smk_ipv6_check - check Smack access
2473 * @subject: subject Smack label
2474 * @object: object Smack label
2476 * @act: the action being taken
2478 * Check an IPv6 access
2480 static int smk_ipv6_check(struct smack_known *subject,
2481 struct smack_known *object,
2482 struct sockaddr_in6 *address, int act)
2485 struct lsm_network_audit net;
2487 struct smk_audit_info ad;
2491 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2492 ad.a.u.net->family = PF_INET6;
2493 ad.a.u.net->dport = ntohs(address->sin6_port);
2494 if (act == SMK_RECEIVING)
2495 ad.a.u.net->v6info.saddr = address->sin6_addr;
2497 ad.a.u.net->v6info.daddr = address->sin6_addr;
2499 rc = smk_access(subject, object, MAY_WRITE, &ad);
2500 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2504 #ifdef SMACK_IPV6_PORT_LABELING
2506 * smk_ipv6_port_label - Smack port access table management
2510 * Create or update the port list entry
2512 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2514 struct sock *sk = sock->sk;
2515 struct sockaddr_in6 *addr6;
2516 struct socket_smack *ssp = sock->sk->sk_security;
2517 struct smk_port_label *spp;
2518 unsigned short port = 0;
2520 if (address == NULL) {
2522 * This operation is changing the Smack information
2523 * on the bound socket. Take the changes to the port
2527 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2528 if (sk != spp->smk_sock)
2530 spp->smk_in = ssp->smk_in;
2531 spp->smk_out = ssp->smk_out;
2536 * A NULL address is only used for updating existing
2537 * bound entries. If there isn't one, it's OK.
2543 addr6 = (struct sockaddr_in6 *)address;
2544 port = ntohs(addr6->sin6_port);
2546 * This is a special case that is safely ignored.
2552 * Look for an existing port list entry.
2553 * This is an indication that a port is getting reused.
2556 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2557 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2559 if (spp->smk_can_reuse != 1) {
2563 spp->smk_port = port;
2565 spp->smk_in = ssp->smk_in;
2566 spp->smk_out = ssp->smk_out;
2567 spp->smk_can_reuse = 0;
2573 * A new port entry is required.
2575 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2579 spp->smk_port = port;
2581 spp->smk_in = ssp->smk_in;
2582 spp->smk_out = ssp->smk_out;
2583 spp->smk_sock_type = sock->type;
2584 spp->smk_can_reuse = 0;
2586 mutex_lock(&smack_ipv6_lock);
2587 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2588 mutex_unlock(&smack_ipv6_lock);
2594 * smk_ipv6_port_check - check Smack port access
2597 * @act: the action being taken
2599 * Create or update the port list entry
2601 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2604 struct smk_port_label *spp;
2605 struct socket_smack *ssp = sk->sk_security;
2606 struct smack_known *skp = NULL;
2607 unsigned short port;
2608 struct smack_known *object;
2610 if (act == SMK_RECEIVING) {
2611 skp = smack_ipv6host_label(address);
2612 object = ssp->smk_in;
2615 object = smack_ipv6host_label(address);
2619 * The other end is a single label host.
2621 if (skp != NULL && object != NULL)
2622 return smk_ipv6_check(skp, object, address, act);
2624 skp = smack_net_ambient;
2626 object = smack_net_ambient;
2629 * It's remote, so port lookup does no good.
2631 if (!smk_ipv6_localhost(address))
2632 return smk_ipv6_check(skp, object, address, act);
2635 * It's local so the send check has to have passed.
2637 if (act == SMK_RECEIVING)
2640 port = ntohs(address->sin6_port);
2642 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2643 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2645 object = spp->smk_in;
2646 if (act == SMK_CONNECTING)
2647 ssp->smk_packet = spp->smk_out;
2652 return smk_ipv6_check(skp, object, address, act);
2656 * smack_inode_setsecurity - set smack xattrs
2657 * @inode: the object
2658 * @name: attribute name
2659 * @value: attribute value
2660 * @size: size of the attribute
2663 * Sets the named attribute in the appropriate blob
2665 * Returns 0 on success, or an error code
2667 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2668 const void *value, size_t size, int flags)
2670 struct smack_known *skp;
2671 struct inode_smack *nsp = smack_inode(inode);
2672 struct socket_smack *ssp;
2673 struct socket *sock;
2676 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2679 skp = smk_import_entry(value, size);
2681 return PTR_ERR(skp);
2683 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2684 nsp->smk_inode = skp;
2685 nsp->smk_flags |= SMK_INODE_INSTANT;
2689 * The rest of the Smack xattrs are only on sockets.
2691 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2694 sock = SOCKET_I(inode);
2695 if (sock == NULL || sock->sk == NULL)
2698 ssp = sock->sk->sk_security;
2700 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2702 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2704 if (sock->sk->sk_family == PF_INET) {
2705 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2708 "Smack: \"%s\" netlbl error %d.\n",
2714 #ifdef SMACK_IPV6_PORT_LABELING
2715 if (sock->sk->sk_family == PF_INET6)
2716 smk_ipv6_port_label(sock, NULL);
2723 * smack_socket_post_create - finish socket setup
2725 * @family: protocol family
2730 * Sets the netlabel information on the socket
2732 * Returns 0 on success, and error code otherwise
2734 static int smack_socket_post_create(struct socket *sock, int family,
2735 int type, int protocol, int kern)
2737 struct socket_smack *ssp;
2739 if (sock->sk == NULL)
2743 * Sockets created by kernel threads receive web label.
2745 if (unlikely(current->flags & PF_KTHREAD)) {
2746 ssp = sock->sk->sk_security;
2747 ssp->smk_in = &smack_known_web;
2748 ssp->smk_out = &smack_known_web;
2751 if (family != PF_INET)
2754 * Set the outbound netlbl.
2756 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2760 * smack_socket_socketpair - create socket pair
2761 * @socka: one socket
2762 * @sockb: another socket
2764 * Cross reference the peer labels for SO_PEERSEC
2768 static int smack_socket_socketpair(struct socket *socka,
2769 struct socket *sockb)
2771 struct socket_smack *asp = socka->sk->sk_security;
2772 struct socket_smack *bsp = sockb->sk->sk_security;
2774 asp->smk_packet = bsp->smk_out;
2775 bsp->smk_packet = asp->smk_out;
2780 #ifdef SMACK_IPV6_PORT_LABELING
2782 * smack_socket_bind - record port binding information.
2784 * @address: the port address
2785 * @addrlen: size of the address
2787 * Records the label bound to a port.
2789 * Returns 0 on success, and error code otherwise
2791 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2794 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2795 if (addrlen < SIN6_LEN_RFC2133 ||
2796 address->sa_family != AF_INET6)
2798 smk_ipv6_port_label(sock, address);
2802 #endif /* SMACK_IPV6_PORT_LABELING */
2805 * smack_socket_connect - connect access check
2807 * @sap: the other end
2808 * @addrlen: size of sap
2810 * Verifies that a connection may be possible
2812 * Returns 0 on success, and error code otherwise
2814 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2819 if (sock->sk == NULL)
2821 if (sock->sk->sk_family != PF_INET &&
2822 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2824 if (addrlen < offsetofend(struct sockaddr, sa_family))
2826 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2827 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2828 struct smack_known *rsp = NULL;
2830 if (addrlen < SIN6_LEN_RFC2133)
2832 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2833 rsp = smack_ipv6host_label(sip);
2835 struct socket_smack *ssp = sock->sk->sk_security;
2837 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2840 if (__is_defined(SMACK_IPV6_PORT_LABELING))
2841 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2845 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2847 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2852 * smack_flags_to_may - convert S_ to MAY_ values
2853 * @flags: the S_ value
2855 * Returns the equivalent MAY_ value
2857 static int smack_flags_to_may(int flags)
2861 if (flags & S_IRUGO)
2863 if (flags & S_IWUGO)
2865 if (flags & S_IXUGO)
2872 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2877 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2879 struct smack_known **blob = smack_msg_msg(msg);
2881 *blob = smk_of_current();
2886 * smack_of_ipc - the smack pointer for the ipc
2889 * Returns a pointer to the smack value
2891 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2893 struct smack_known **blob = smack_ipc(isp);
2899 * smack_ipc_alloc_security - Set the security blob for ipc
2904 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2906 struct smack_known **blob = smack_ipc(isp);
2908 *blob = smk_of_current();
2913 * smk_curacc_shm : check if current has access on shm
2915 * @access : access requested
2917 * Returns 0 if current has the requested access, error code otherwise
2919 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2921 struct smack_known *ssp = smack_of_ipc(isp);
2922 struct smk_audit_info ad;
2926 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2927 ad.a.u.ipc_id = isp->id;
2929 rc = smk_curacc(ssp, access, &ad);
2930 rc = smk_bu_current("shm", ssp, access, rc);
2935 * smack_shm_associate - Smack access check for shm
2937 * @shmflg: access requested
2939 * Returns 0 if current has the requested access, error code otherwise
2941 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2945 may = smack_flags_to_may(shmflg);
2946 return smk_curacc_shm(isp, may);
2950 * smack_shm_shmctl - Smack access check for shm
2952 * @cmd: what it wants to do
2954 * Returns 0 if current has the requested access, error code otherwise
2956 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2970 may = MAY_READWRITE;
2975 * System level information.
2981 return smk_curacc_shm(isp, may);
2985 * smack_shm_shmat - Smack access for shmat
2988 * @shmflg: access requested
2990 * Returns 0 if current has the requested access, error code otherwise
2992 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
2997 may = smack_flags_to_may(shmflg);
2998 return smk_curacc_shm(isp, may);
3002 * smk_curacc_sem : check if current has access on sem
3004 * @access : access requested
3006 * Returns 0 if current has the requested access, error code otherwise
3008 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3010 struct smack_known *ssp = smack_of_ipc(isp);
3011 struct smk_audit_info ad;
3015 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3016 ad.a.u.ipc_id = isp->id;
3018 rc = smk_curacc(ssp, access, &ad);
3019 rc = smk_bu_current("sem", ssp, access, rc);
3024 * smack_sem_associate - Smack access check for sem
3026 * @semflg: access requested
3028 * Returns 0 if current has the requested access, error code otherwise
3030 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3034 may = smack_flags_to_may(semflg);
3035 return smk_curacc_sem(isp, may);
3039 * smack_sem_shmctl - Smack access check for sem
3041 * @cmd: what it wants to do
3043 * Returns 0 if current has the requested access, error code otherwise
3045 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3064 may = MAY_READWRITE;
3069 * System level information
3076 return smk_curacc_sem(isp, may);
3080 * smack_sem_semop - Smack checks of semaphore operations
3086 * Treated as read and write in all cases.
3088 * Returns 0 if access is allowed, error code otherwise
3090 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3091 unsigned nsops, int alter)
3093 return smk_curacc_sem(isp, MAY_READWRITE);
3097 * smk_curacc_msq : helper to check if current has access on msq
3099 * @access : access requested
3101 * return 0 if current has access, error otherwise
3103 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3105 struct smack_known *msp = smack_of_ipc(isp);
3106 struct smk_audit_info ad;
3110 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3111 ad.a.u.ipc_id = isp->id;
3113 rc = smk_curacc(msp, access, &ad);
3114 rc = smk_bu_current("msq", msp, access, rc);
3119 * smack_msg_queue_associate - Smack access check for msg_queue
3121 * @msqflg: access requested
3123 * Returns 0 if current has the requested access, error code otherwise
3125 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3129 may = smack_flags_to_may(msqflg);
3130 return smk_curacc_msq(isp, may);
3134 * smack_msg_queue_msgctl - Smack access check for msg_queue
3136 * @cmd: what it wants to do
3138 * Returns 0 if current has the requested access, error code otherwise
3140 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3152 may = MAY_READWRITE;
3157 * System level information
3164 return smk_curacc_msq(isp, may);
3168 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3171 * @msqflg: access requested
3173 * Returns 0 if current has the requested access, error code otherwise
3175 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3180 may = smack_flags_to_may(msqflg);
3181 return smk_curacc_msq(isp, may);
3185 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3192 * Returns 0 if current has read and write access, error code otherwise
3194 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3195 struct task_struct *target, long type, int mode)
3197 return smk_curacc_msq(isp, MAY_READWRITE);
3201 * smack_ipc_permission - Smack access for ipc_permission()
3202 * @ipp: the object permissions
3203 * @flag: access requested
3205 * Returns 0 if current has read and write access, error code otherwise
3207 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3209 struct smack_known **blob = smack_ipc(ipp);
3210 struct smack_known *iskp = *blob;
3211 int may = smack_flags_to_may(flag);
3212 struct smk_audit_info ad;
3216 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3217 ad.a.u.ipc_id = ipp->id;
3219 rc = smk_curacc(iskp, may, &ad);
3220 rc = smk_bu_current("svipc", iskp, may, rc);
3225 * smack_ipc_getsecid - Extract smack security id
3226 * @ipp: the object permissions
3227 * @secid: where result will be saved
3229 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3231 struct smack_known **blob = smack_ipc(ipp);
3232 struct smack_known *iskp = *blob;
3234 *secid = iskp->smk_secid;
3238 * smack_d_instantiate - Make sure the blob is correct on an inode
3239 * @opt_dentry: dentry where inode will be attached
3240 * @inode: the object
3242 * Set the inode's security blob if it hasn't been done already.
3244 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3246 struct super_block *sbp;
3247 struct superblock_smack *sbsp;
3248 struct inode_smack *isp;
3249 struct smack_known *skp;
3250 struct smack_known *ckp = smk_of_current();
3251 struct smack_known *final;
3252 char trattr[TRANS_TRUE_SIZE];
3260 isp = smack_inode(inode);
3263 * If the inode is already instantiated
3264 * take the quick way out
3266 if (isp->smk_flags & SMK_INODE_INSTANT)
3270 sbsp = sbp->s_security;
3272 * We're going to use the superblock default label
3273 * if there's no label on the file.
3275 final = sbsp->smk_default;
3278 * If this is the root inode the superblock
3279 * may be in the process of initialization.
3280 * If that is the case use the root value out
3281 * of the superblock.
3283 if (opt_dentry->d_parent == opt_dentry) {
3284 switch (sbp->s_magic) {
3285 case CGROUP_SUPER_MAGIC:
3286 case CGROUP2_SUPER_MAGIC:
3288 * The cgroup filesystem is never mounted,
3289 * so there's no opportunity to set the mount
3292 sbsp->smk_root = &smack_known_star;
3293 sbsp->smk_default = &smack_known_star;
3294 isp->smk_inode = sbsp->smk_root;
3298 * What about shmem/tmpfs anonymous files with dentry
3299 * obtained from d_alloc_pseudo()?
3301 isp->smk_inode = smk_of_current();
3304 isp->smk_inode = smk_of_current();
3308 * Socket access is controlled by the socket
3309 * structures associated with the task involved.
3311 isp->smk_inode = &smack_known_star;
3314 isp->smk_inode = sbsp->smk_root;
3317 isp->smk_flags |= SMK_INODE_INSTANT;
3322 * This is pretty hackish.
3323 * Casey says that we shouldn't have to do
3324 * file system specific code, but it does help
3325 * with keeping it simple.
3327 switch (sbp->s_magic) {
3329 case CGROUP_SUPER_MAGIC:
3330 case CGROUP2_SUPER_MAGIC:
3332 * Casey says that it's a little embarrassing
3333 * that the smack file system doesn't do
3334 * extended attributes.
3336 * Cgroupfs is special
3338 final = &smack_known_star;
3340 case DEVPTS_SUPER_MAGIC:
3342 * devpts seems content with the label of the task.
3343 * Programs that change smack have to treat the
3348 case PROC_SUPER_MAGIC:
3350 * Casey says procfs appears not to care.
3351 * The superblock default suffices.
3356 * Device labels should come from the filesystem,
3357 * but watch out, because they're volitile,
3358 * getting recreated on every reboot.
3360 final = &smack_known_star;
3362 * If a smack value has been set we want to use it,
3363 * but since tmpfs isn't giving us the opportunity
3364 * to set mount options simulate setting the
3365 * superblock default.
3370 * This isn't an understood special case.
3371 * Get the value from the xattr.
3375 * UNIX domain sockets use lower level socket data.
3377 if (S_ISSOCK(inode->i_mode)) {
3378 final = &smack_known_star;
3382 * No xattr support means, alas, no SMACK label.
3383 * Use the aforeapplied default.
3384 * It would be curious if the label of the task
3385 * does not match that assigned.
3387 if (!(inode->i_opflags & IOP_XATTR))
3390 * Get the dentry for xattr.
3392 dp = dget(opt_dentry);
3393 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3394 if (!IS_ERR_OR_NULL(skp))
3398 * Transmuting directory
3400 if (S_ISDIR(inode->i_mode)) {
3402 * If this is a new directory and the label was
3403 * transmuted when the inode was initialized
3404 * set the transmute attribute on the directory
3405 * and mark the inode.
3407 * If there is a transmute attribute on the
3408 * directory mark the inode.
3410 if (isp->smk_flags & SMK_INODE_CHANGED) {
3411 isp->smk_flags &= ~SMK_INODE_CHANGED;
3412 rc = __vfs_setxattr(dp, inode,
3413 XATTR_NAME_SMACKTRANSMUTE,
3414 TRANS_TRUE, TRANS_TRUE_SIZE,
3417 rc = __vfs_getxattr(dp, inode,
3418 XATTR_NAME_SMACKTRANSMUTE, trattr,
3420 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3421 TRANS_TRUE_SIZE) != 0)
3425 transflag = SMK_INODE_TRANSMUTE;
3428 * Don't let the exec or mmap label be "*" or "@".
3430 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3431 if (IS_ERR(skp) || skp == &smack_known_star ||
3432 skp == &smack_known_web)
3434 isp->smk_task = skp;
3436 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3437 if (IS_ERR(skp) || skp == &smack_known_star ||
3438 skp == &smack_known_web)
3440 isp->smk_mmap = skp;
3447 isp->smk_inode = ckp;
3449 isp->smk_inode = final;
3451 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3457 * smack_getprocattr - Smack process attribute access
3458 * @p: the object task
3459 * @name: the name of the attribute in /proc/.../attr
3460 * @value: where to put the result
3462 * Places a copy of the task Smack into value
3464 * Returns the length of the smack label or an error code
3466 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3468 struct smack_known *skp = smk_of_task_struct(p);
3472 if (strcmp(name, "current") != 0)
3475 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3485 * smack_setprocattr - Smack process attribute setting
3486 * @name: the name of the attribute in /proc/.../attr
3487 * @value: the value to set
3488 * @size: the size of the value
3490 * Sets the Smack value of the task. Only setting self
3491 * is permitted and only with privilege
3493 * Returns the length of the smack label or an error code
3495 static int smack_setprocattr(const char *name, void *value, size_t size)
3497 struct task_smack *tsp = smack_cred(current_cred());
3499 struct smack_known *skp;
3500 struct smack_known_list_elem *sklep;
3503 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3506 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3509 if (strcmp(name, "current") != 0)
3512 skp = smk_import_entry(value, size);
3514 return PTR_ERR(skp);
3517 * No process is ever allowed the web ("@") label
3518 * and the star ("*") label.
3520 if (skp == &smack_known_web || skp == &smack_known_star)
3523 if (!smack_privileged(CAP_MAC_ADMIN)) {
3525 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3526 if (sklep->smk_label == skp) {
3534 new = prepare_creds();
3538 tsp = smack_cred(new);
3539 tsp->smk_task = skp;
3541 * process can change its label only once
3543 smk_destroy_label_list(&tsp->smk_relabel);
3550 * smack_unix_stream_connect - Smack access on UDS
3552 * @other: the other sock
3555 * Return 0 if a subject with the smack of sock could access
3556 * an object with the smack of other, otherwise an error code
3558 static int smack_unix_stream_connect(struct sock *sock,
3559 struct sock *other, struct sock *newsk)
3561 struct smack_known *skp;
3562 struct smack_known *okp;
3563 struct socket_smack *ssp = sock->sk_security;
3564 struct socket_smack *osp = other->sk_security;
3565 struct socket_smack *nsp = newsk->sk_security;
3566 struct smk_audit_info ad;
3569 struct lsm_network_audit net;
3572 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3576 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3577 smk_ad_setfield_u_net_sk(&ad, other);
3579 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3580 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3584 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3585 rc = smk_bu_note("UDS connect", okp, skp,
3591 * Cross reference the peer labels for SO_PEERSEC.
3594 nsp->smk_packet = ssp->smk_out;
3595 ssp->smk_packet = osp->smk_out;
3602 * smack_unix_may_send - Smack access on UDS
3604 * @other: the other socket
3606 * Return 0 if a subject with the smack of sock could access
3607 * an object with the smack of other, otherwise an error code
3609 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3611 struct socket_smack *ssp = sock->sk->sk_security;
3612 struct socket_smack *osp = other->sk->sk_security;
3613 struct smk_audit_info ad;
3617 struct lsm_network_audit net;
3619 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3620 smk_ad_setfield_u_net_sk(&ad, other->sk);
3623 if (smack_privileged(CAP_MAC_OVERRIDE))
3626 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3627 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3632 * smack_socket_sendmsg - Smack check based on destination host
3635 * @size: the size of the message
3637 * Return 0 if the current subject can write to the destination host.
3638 * For IPv4 this is only a question if the destination is a single label host.
3639 * For IPv6 this is a check against the label of the port.
3641 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3644 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3645 #if IS_ENABLED(CONFIG_IPV6)
3646 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3648 #ifdef SMACK_IPV6_SECMARK_LABELING
3649 struct socket_smack *ssp = sock->sk->sk_security;
3650 struct smack_known *rsp;
3655 * Perfectly reasonable for this to be NULL
3660 switch (sock->sk->sk_family) {
3662 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3663 sip->sin_family != AF_INET)
3665 rc = smack_netlabel_send(sock->sk, sip);
3667 #if IS_ENABLED(CONFIG_IPV6)
3669 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3670 sap->sin6_family != AF_INET6)
3672 #ifdef SMACK_IPV6_SECMARK_LABELING
3673 rsp = smack_ipv6host_label(sap);
3675 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3678 #ifdef SMACK_IPV6_PORT_LABELING
3679 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3681 #endif /* IS_ENABLED(CONFIG_IPV6) */
3688 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3689 * @sap: netlabel secattr
3690 * @ssp: socket security information
3692 * Returns a pointer to a Smack label entry found on the label list.
3694 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3695 struct socket_smack *ssp)
3697 struct smack_known *skp;
3702 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3704 * Looks like a CIPSO packet.
3705 * If there are flags but no level netlabel isn't
3706 * behaving the way we expect it to.
3708 * Look it up in the label table
3709 * Without guidance regarding the smack value
3710 * for the packet fall back on the network
3714 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3715 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3718 * Compare the catsets. Use the netlbl APIs.
3720 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3721 if ((skp->smk_netlabel.flags &
3722 NETLBL_SECATTR_MLS_CAT) == 0)
3726 for (acat = -1, kcat = -1; acat == kcat; ) {
3727 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3729 kcat = netlbl_catmap_walk(
3730 skp->smk_netlabel.attr.mls.cat,
3732 if (acat < 0 || kcat < 0)
3745 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3746 return &smack_known_web;
3747 return &smack_known_star;
3749 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3751 * Looks like a fallback, which gives us a secid.
3753 return smack_from_secid(sap->attr.secid);
3755 * Without guidance regarding the smack value
3756 * for the packet fall back on the network
3759 return smack_net_ambient;
3762 #if IS_ENABLED(CONFIG_IPV6)
3763 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3767 int proto = -EINVAL;
3768 struct ipv6hdr _ipv6h;
3769 struct ipv6hdr *ip6;
3771 struct tcphdr _tcph, *th;
3772 struct udphdr _udph, *uh;
3773 struct dccp_hdr _dccph, *dh;
3777 offset = skb_network_offset(skb);
3778 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3781 sip->sin6_addr = ip6->saddr;
3783 nexthdr = ip6->nexthdr;
3784 offset += sizeof(_ipv6h);
3785 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3792 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3794 sip->sin6_port = th->source;
3797 case IPPROTO_UDPLITE:
3798 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3800 sip->sin6_port = uh->source;
3803 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3805 sip->sin6_port = dh->dccph_sport;
3810 #endif /* CONFIG_IPV6 */
3813 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3817 * Returns 0 if the packet should be delivered, an error code otherwise
3819 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3821 struct netlbl_lsm_secattr secattr;
3822 struct socket_smack *ssp = sk->sk_security;
3823 struct smack_known *skp = NULL;
3825 struct smk_audit_info ad;
3826 u16 family = sk->sk_family;
3828 struct lsm_network_audit net;
3830 #if IS_ENABLED(CONFIG_IPV6)
3831 struct sockaddr_in6 sadd;
3834 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3836 #endif /* CONFIG_IPV6 */
3840 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3842 * If there is a secmark use it rather than the CIPSO label.
3843 * If there is no secmark fall back to CIPSO.
3844 * The secmark is assumed to reflect policy better.
3846 if (skb && skb->secmark != 0) {
3847 skp = smack_from_secid(skb->secmark);
3850 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3852 * Translate what netlabel gave us.
3854 netlbl_secattr_init(&secattr);
3856 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3858 skp = smack_from_secattr(&secattr, ssp);
3860 skp = smack_net_ambient;
3862 netlbl_secattr_destroy(&secattr);
3864 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3868 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3869 ad.a.u.net->family = family;
3870 ad.a.u.net->netif = skb->skb_iif;
3871 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3874 * Receiving a packet requires that the other end
3875 * be able to write here. Read access is not required.
3876 * This is the simplist possible security model
3879 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3880 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3883 netlbl_skbuff_err(skb, family, rc, 0);
3885 #if IS_ENABLED(CONFIG_IPV6)
3887 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3888 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3889 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3891 #ifdef SMACK_IPV6_SECMARK_LABELING
3892 if (skb && skb->secmark != 0)
3893 skp = smack_from_secid(skb->secmark);
3894 else if (smk_ipv6_localhost(&sadd))
3897 skp = smack_ipv6host_label(&sadd);
3899 skp = smack_net_ambient;
3903 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3904 ad.a.u.net->family = family;
3905 ad.a.u.net->netif = skb->skb_iif;
3906 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3907 #endif /* CONFIG_AUDIT */
3908 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3909 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3911 #endif /* SMACK_IPV6_SECMARK_LABELING */
3912 #ifdef SMACK_IPV6_PORT_LABELING
3913 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3914 #endif /* SMACK_IPV6_PORT_LABELING */
3916 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3917 ICMPV6_ADM_PROHIBITED, 0);
3919 #endif /* CONFIG_IPV6 */
3926 * smack_socket_getpeersec_stream - pull in packet label
3928 * @optval: user's destination
3929 * @optlen: size thereof
3932 * returns zero on success, an error code otherwise
3934 static int smack_socket_getpeersec_stream(struct socket *sock,
3935 char __user *optval,
3936 int __user *optlen, unsigned len)
3938 struct socket_smack *ssp;
3943 ssp = sock->sk->sk_security;
3944 if (ssp->smk_packet != NULL) {
3945 rcp = ssp->smk_packet->smk_known;
3946 slen = strlen(rcp) + 1;
3951 else if (copy_to_user(optval, rcp, slen) != 0)
3954 if (put_user(slen, optlen) != 0)
3962 * smack_socket_getpeersec_dgram - pull in packet label
3963 * @sock: the peer socket
3965 * @secid: pointer to where to put the secid of the packet
3967 * Sets the netlabel socket state on sk from parent
3969 static int smack_socket_getpeersec_dgram(struct socket *sock,
3970 struct sk_buff *skb, u32 *secid)
3973 struct netlbl_lsm_secattr secattr;
3974 struct socket_smack *ssp = NULL;
3975 struct smack_known *skp;
3976 int family = PF_UNSPEC;
3977 u32 s = 0; /* 0 is the invalid secid */
3981 if (skb->protocol == htons(ETH_P_IP))
3983 #if IS_ENABLED(CONFIG_IPV6)
3984 else if (skb->protocol == htons(ETH_P_IPV6))
3986 #endif /* CONFIG_IPV6 */
3988 if (family == PF_UNSPEC && sock != NULL)
3989 family = sock->sk->sk_family;
3993 ssp = sock->sk->sk_security;
3994 s = ssp->smk_out->smk_secid;
3997 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4003 * Translate what netlabel gave us.
4005 if (sock != NULL && sock->sk != NULL)
4006 ssp = sock->sk->sk_security;
4007 netlbl_secattr_init(&secattr);
4008 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4010 skp = smack_from_secattr(&secattr, ssp);
4013 netlbl_secattr_destroy(&secattr);
4016 #ifdef SMACK_IPV6_SECMARK_LABELING
4028 * smack_sock_graft - Initialize a newly created socket with an existing sock
4030 * @parent: parent socket
4032 * Set the smk_{in,out} state of an existing sock based on the process that
4033 * is creating the new socket.
4035 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4037 struct socket_smack *ssp;
4038 struct smack_known *skp = smk_of_current();
4041 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4044 ssp = sk->sk_security;
4047 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4051 * smack_inet_conn_request - Smack access check on connect
4052 * @sk: socket involved
4056 * Returns 0 if a task with the packet label could write to
4057 * the socket, otherwise an error code
4059 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4060 struct request_sock *req)
4062 u16 family = sk->sk_family;
4063 struct smack_known *skp;
4064 struct socket_smack *ssp = sk->sk_security;
4065 struct netlbl_lsm_secattr secattr;
4066 struct sockaddr_in addr;
4068 struct smack_known *hskp;
4070 struct smk_audit_info ad;
4072 struct lsm_network_audit net;
4075 #if IS_ENABLED(CONFIG_IPV6)
4076 if (family == PF_INET6) {
4078 * Handle mapped IPv4 packets arriving
4079 * via IPv6 sockets. Don't set up netlabel
4080 * processing on IPv6.
4082 if (skb->protocol == htons(ETH_P_IP))
4087 #endif /* CONFIG_IPV6 */
4089 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4091 * If there is a secmark use it rather than the CIPSO label.
4092 * If there is no secmark fall back to CIPSO.
4093 * The secmark is assumed to reflect policy better.
4095 if (skb && skb->secmark != 0) {
4096 skp = smack_from_secid(skb->secmark);
4099 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4101 netlbl_secattr_init(&secattr);
4102 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4104 skp = smack_from_secattr(&secattr, ssp);
4106 skp = &smack_known_huh;
4107 netlbl_secattr_destroy(&secattr);
4109 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4114 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4115 ad.a.u.net->family = family;
4116 ad.a.u.net->netif = skb->skb_iif;
4117 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4120 * Receiving a packet requires that the other end be able to write
4121 * here. Read access is not required.
4123 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4124 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4129 * Save the peer's label in the request_sock so we can later setup
4130 * smk_packet in the child socket so that SO_PEERCRED can report it.
4132 req->peer_secid = skp->smk_secid;
4135 * We need to decide if we want to label the incoming connection here
4136 * if we do we only need to label the request_sock and the stack will
4137 * propagate the wire-label to the sock when it is created.
4140 addr.sin_addr.s_addr = hdr->saddr;
4142 hskp = smack_ipv4host_label(&addr);
4146 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4148 netlbl_req_delattr(req);
4154 * smack_inet_csk_clone - Copy the connection information to the new socket
4155 * @sk: the new socket
4156 * @req: the connection's request_sock
4158 * Transfer the connection's peer label to the newly created socket.
4160 static void smack_inet_csk_clone(struct sock *sk,
4161 const struct request_sock *req)
4163 struct socket_smack *ssp = sk->sk_security;
4164 struct smack_known *skp;
4166 if (req->peer_secid != 0) {
4167 skp = smack_from_secid(req->peer_secid);
4168 ssp->smk_packet = skp;
4170 ssp->smk_packet = NULL;
4174 * Key management security hooks
4176 * Casey has not tested key support very heavily.
4177 * The permission check is most likely too restrictive.
4178 * If you care about keys please have a look.
4183 * smack_key_alloc - Set the key security blob
4185 * @cred: the credentials to use
4188 * No allocation required
4192 static int smack_key_alloc(struct key *key, const struct cred *cred,
4193 unsigned long flags)
4195 struct smack_known *skp = smk_of_task(smack_cred(cred));
4197 key->security = skp;
4202 * smack_key_free - Clear the key security blob
4205 * Clear the blob pointer
4207 static void smack_key_free(struct key *key)
4209 key->security = NULL;
4213 * smack_key_permission - Smack access on a key
4214 * @key_ref: gets to the object
4215 * @cred: the credentials to use
4216 * @perm: requested key permissions
4218 * Return 0 if the task has read and write to the object,
4219 * an error code otherwise
4221 static int smack_key_permission(key_ref_t key_ref,
4222 const struct cred *cred, unsigned perm)
4225 struct smk_audit_info ad;
4226 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4231 * Validate requested permissions
4233 if (perm & ~KEY_NEED_ALL)
4236 keyp = key_ref_to_ptr(key_ref);
4240 * If the key hasn't been initialized give it access so that
4243 if (keyp->security == NULL)
4246 * This should not occur
4251 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4255 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4256 ad.a.u.key_struct.key = keyp->serial;
4257 ad.a.u.key_struct.key_desc = keyp->description;
4259 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4260 request |= MAY_READ;
4261 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4262 request |= MAY_WRITE;
4263 rc = smk_access(tkp, keyp->security, request, &ad);
4264 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4269 * smack_key_getsecurity - Smack label tagging the key
4270 * @key points to the key to be queried
4271 * @_buffer points to a pointer that should be set to point to the
4272 * resulting string (if no label or an error occurs).
4273 * Return the length of the string (including terminating NUL) or -ve if
4275 * May also return 0 (and a NULL buffer pointer) if there is no label.
4277 static int smack_key_getsecurity(struct key *key, char **_buffer)
4279 struct smack_known *skp = key->security;
4283 if (key->security == NULL) {
4288 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4291 length = strlen(copy) + 1;
4297 #endif /* CONFIG_KEYS */
4302 * Audit requires a unique representation of each Smack specific
4303 * rule. This unique representation is used to distinguish the
4304 * object to be audited from remaining kernel objects and also
4305 * works as a glue between the audit hooks.
4307 * Since repository entries are added but never deleted, we'll use
4308 * the smack_known label address related to the given audit rule as
4309 * the needed unique representation. This also better fits the smack
4310 * model where nearly everything is a label.
4315 * smack_audit_rule_init - Initialize a smack audit rule
4316 * @field: audit rule fields given from user-space (audit.h)
4317 * @op: required testing operator (=, !=, >, <, ...)
4318 * @rulestr: smack label to be audited
4319 * @vrule: pointer to save our own audit rule representation
4321 * Prepare to audit cases where (@field @op @rulestr) is true.
4322 * The label to be audited is created if necessay.
4324 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4326 struct smack_known *skp;
4327 char **rule = (char **)vrule;
4330 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4333 if (op != Audit_equal && op != Audit_not_equal)
4336 skp = smk_import_entry(rulestr, 0);
4338 return PTR_ERR(skp);
4340 *rule = skp->smk_known;
4346 * smack_audit_rule_known - Distinguish Smack audit rules
4347 * @krule: rule of interest, in Audit kernel representation format
4349 * This is used to filter Smack rules from remaining Audit ones.
4350 * If it's proved that this rule belongs to us, the
4351 * audit_rule_match hook will be called to do the final judgement.
4353 static int smack_audit_rule_known(struct audit_krule *krule)
4355 struct audit_field *f;
4358 for (i = 0; i < krule->field_count; i++) {
4359 f = &krule->fields[i];
4361 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4369 * smack_audit_rule_match - Audit given object ?
4370 * @secid: security id for identifying the object to test
4371 * @field: audit rule flags given from user-space
4372 * @op: required testing operator
4373 * @vrule: smack internal rule presentation
4375 * The core Audit hook. It's used to take the decision of
4376 * whether to audit or not to audit a given object.
4378 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4380 struct smack_known *skp;
4383 if (unlikely(!rule)) {
4384 WARN_ONCE(1, "Smack: missing rule\n");
4388 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4391 skp = smack_from_secid(secid);
4394 * No need to do string comparisons. If a match occurs,
4395 * both pointers will point to the same smack_known
4398 if (op == Audit_equal)
4399 return (rule == skp->smk_known);
4400 if (op == Audit_not_equal)
4401 return (rule != skp->smk_known);
4407 * There is no need for a smack_audit_rule_free hook.
4408 * No memory was allocated.
4411 #endif /* CONFIG_AUDIT */
4414 * smack_ismaclabel - check if xattr @name references a smack MAC label
4415 * @name: Full xattr name to check.
4417 static int smack_ismaclabel(const char *name)
4419 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4424 * smack_secid_to_secctx - return the smack label for a secid
4425 * @secid: incoming integer
4426 * @secdata: destination
4427 * @seclen: how long it is
4429 * Exists for networking code.
4431 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4433 struct smack_known *skp = smack_from_secid(secid);
4436 *secdata = skp->smk_known;
4437 *seclen = strlen(skp->smk_known);
4442 * smack_secctx_to_secid - return the secid for a smack label
4443 * @secdata: smack label
4444 * @seclen: how long result is
4445 * @secid: outgoing integer
4447 * Exists for audit and networking code.
4449 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4451 struct smack_known *skp = smk_find_entry(secdata);
4454 *secid = skp->smk_secid;
4461 * There used to be a smack_release_secctx hook
4462 * that did nothing back when hooks were in a vector.
4463 * Now that there's a list such a hook adds cost.
4466 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4468 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4471 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4473 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4476 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4478 struct smack_known *skp = smk_of_inode(inode);
4480 *ctx = skp->smk_known;
4481 *ctxlen = strlen(skp->smk_known);
4485 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4488 struct task_smack *tsp;
4489 struct smack_known *skp;
4490 struct inode_smack *isp;
4491 struct cred *new_creds = *new;
4493 if (new_creds == NULL) {
4494 new_creds = prepare_creds();
4495 if (new_creds == NULL)
4499 tsp = smack_cred(new_creds);
4502 * Get label from overlay inode and set it in create_sid
4504 isp = smack_inode(d_inode(dentry->d_parent));
4505 skp = isp->smk_inode;
4506 tsp->smk_task = skp;
4511 static int smack_inode_copy_up_xattr(const char *name)
4514 * Return 1 if this is the smack access Smack attribute.
4516 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4522 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4524 const struct cred *old,
4527 struct task_smack *otsp = smack_cred(old);
4528 struct task_smack *ntsp = smack_cred(new);
4529 struct inode_smack *isp;
4533 * Use the process credential unless all of
4534 * the transmuting criteria are met
4536 ntsp->smk_task = otsp->smk_task;
4539 * the attribute of the containing directory
4541 isp = smack_inode(d_inode(dentry->d_parent));
4543 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4545 may = smk_access_entry(otsp->smk_task->smk_known,
4546 isp->smk_inode->smk_known,
4547 &otsp->smk_task->smk_rules);
4551 * If the directory is transmuting and the rule
4552 * providing access is transmuting use the containing
4553 * directory label instead of the process label.
4555 if (may > 0 && (may & MAY_TRANSMUTE))
4556 ntsp->smk_task = isp->smk_inode;
4561 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4562 .lbs_cred = sizeof(struct task_smack),
4563 .lbs_file = sizeof(struct smack_known *),
4564 .lbs_inode = sizeof(struct inode_smack),
4565 .lbs_ipc = sizeof(struct smack_known *),
4566 .lbs_msg_msg = sizeof(struct smack_known *),
4569 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4570 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4571 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4572 LSM_HOOK_INIT(syslog, smack_syslog),
4574 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4575 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4577 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4578 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4579 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4580 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4581 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4582 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4584 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
4586 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4587 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4588 LSM_HOOK_INIT(inode_link, smack_inode_link),
4589 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4590 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4591 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4592 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4593 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4594 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4595 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4596 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4597 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4598 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4599 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4600 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4601 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4602 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4604 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4605 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4606 LSM_HOOK_INIT(file_lock, smack_file_lock),
4607 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4608 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4609 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4610 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4611 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4612 LSM_HOOK_INIT(file_receive, smack_file_receive),
4614 LSM_HOOK_INIT(file_open, smack_file_open),
4616 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4617 LSM_HOOK_INIT(cred_free, smack_cred_free),
4618 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4619 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4620 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4621 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4622 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4623 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4624 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4625 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4626 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4627 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4628 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4629 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4630 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4631 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4632 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4633 LSM_HOOK_INIT(task_kill, smack_task_kill),
4634 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4636 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4637 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4639 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4641 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4642 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4643 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4644 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4645 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4647 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4648 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4649 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4650 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4652 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4653 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4654 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4655 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4657 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4659 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4660 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4662 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4663 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4665 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4666 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4667 #ifdef SMACK_IPV6_PORT_LABELING
4668 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4670 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4671 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4672 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4673 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4674 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4675 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4676 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4677 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4678 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4679 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4681 /* key management security hooks */
4683 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4684 LSM_HOOK_INIT(key_free, smack_key_free),
4685 LSM_HOOK_INIT(key_permission, smack_key_permission),
4686 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4687 #endif /* CONFIG_KEYS */
4691 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4692 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4693 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4694 #endif /* CONFIG_AUDIT */
4696 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4697 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4698 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4699 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4700 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4701 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4702 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4703 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4704 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4708 static __init void init_smack_known_list(void)
4711 * Initialize rule list locks
4713 mutex_init(&smack_known_huh.smk_rules_lock);
4714 mutex_init(&smack_known_hat.smk_rules_lock);
4715 mutex_init(&smack_known_floor.smk_rules_lock);
4716 mutex_init(&smack_known_star.smk_rules_lock);
4717 mutex_init(&smack_known_web.smk_rules_lock);
4719 * Initialize rule lists
4721 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4722 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4723 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4724 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4725 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4727 * Create the known labels list
4729 smk_insert_entry(&smack_known_huh);
4730 smk_insert_entry(&smack_known_hat);
4731 smk_insert_entry(&smack_known_star);
4732 smk_insert_entry(&smack_known_floor);
4733 smk_insert_entry(&smack_known_web);
4737 * smack_init - initialize the smack system
4739 * Returns 0 on success, -ENOMEM is there's no memory
4741 static __init int smack_init(void)
4743 struct cred *cred = (struct cred *) current->cred;
4744 struct task_smack *tsp;
4746 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4747 if (!smack_rule_cache)
4751 * Set the security state for the initial task.
4753 tsp = smack_cred(cred);
4754 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4759 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4762 pr_info("Smack: Initializing.\n");
4763 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4764 pr_info("Smack: Netfilter enabled.\n");
4766 #ifdef SMACK_IPV6_PORT_LABELING
4767 pr_info("Smack: IPv6 port labeling enabled.\n");
4769 #ifdef SMACK_IPV6_SECMARK_LABELING
4770 pr_info("Smack: IPv6 Netfilter enabled.\n");
4773 /* initialize the smack_known_list */
4774 init_smack_known_list();
4780 * Smack requires early initialization in order to label
4781 * all processes and objects when they are created.
4783 DEFINE_LSM(smack) = {
4785 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4786 .blobs = &smack_blob_sizes,