2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/dccp.h>
31 #include <linux/icmpv6.h>
32 #include <linux/slab.h>
33 #include <linux/mutex.h>
34 #include <linux/pipe_fs_i.h>
35 #include <net/cipso_ipv4.h>
38 #include <linux/audit.h>
39 #include <linux/magic.h>
40 #include <linux/dcache.h>
41 #include <linux/personality.h>
42 #include <linux/msg.h>
43 #include <linux/shm.h>
44 #include <linux/binfmts.h>
45 #include <linux/parser.h>
46 #include <linux/fs_context.h>
47 #include <linux/fs_parser.h>
50 #define TRANS_TRUE "TRUE"
51 #define TRANS_TRUE_SIZE 4
53 #define SMK_CONNECTING 0
54 #define SMK_RECEIVING 1
57 #ifdef SMACK_IPV6_PORT_LABELING
58 DEFINE_MUTEX(smack_ipv6_lock);
59 static LIST_HEAD(smk_ipv6_port_list);
61 static struct kmem_cache *smack_inode_cache;
64 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
69 } smk_mount_opts[] = {
70 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
74 static int match_opt_prefix(char *s, int l, char **arg)
78 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
79 size_t len = smk_mount_opts[i].len;
80 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
82 if (len == l || s[len] != '=')
85 return smk_mount_opts[i].opt;
90 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
91 static char *smk_bu_mess[] = {
92 "Bringup Error", /* Unused */
93 "Bringup", /* SMACK_BRINGUP_ALLOW */
94 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
95 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
98 static void smk_bu_mode(int mode, char *s)
104 if (mode & MAY_WRITE)
108 if (mode & MAY_APPEND)
110 if (mode & MAY_TRANSMUTE)
120 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
121 static int smk_bu_note(char *note, struct smack_known *sskp,
122 struct smack_known *oskp, int mode, int rc)
124 char acc[SMK_NUM_ACCESS_TYPE + 1];
128 if (rc > SMACK_UNCONFINED_OBJECT)
131 smk_bu_mode(mode, acc);
132 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
133 sskp->smk_known, oskp->smk_known, acc, note);
137 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
140 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
141 static int smk_bu_current(char *note, struct smack_known *oskp,
144 struct task_smack *tsp = current_security();
145 char acc[SMK_NUM_ACCESS_TYPE + 1];
149 if (rc > SMACK_UNCONFINED_OBJECT)
152 smk_bu_mode(mode, acc);
153 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
154 tsp->smk_task->smk_known, oskp->smk_known,
155 acc, current->comm, note);
159 #define smk_bu_current(note, oskp, mode, RC) (RC)
162 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
163 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
165 struct task_smack *tsp = current_security();
166 struct smack_known *smk_task = smk_of_task_struct(otp);
167 char acc[SMK_NUM_ACCESS_TYPE + 1];
171 if (rc > SMACK_UNCONFINED_OBJECT)
174 smk_bu_mode(mode, acc);
175 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
176 tsp->smk_task->smk_known, smk_task->smk_known, acc,
177 current->comm, otp->comm);
181 #define smk_bu_task(otp, mode, RC) (RC)
184 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
185 static int smk_bu_inode(struct inode *inode, int mode, int rc)
187 struct task_smack *tsp = current_security();
188 struct inode_smack *isp = inode->i_security;
189 char acc[SMK_NUM_ACCESS_TYPE + 1];
191 if (isp->smk_flags & SMK_INODE_IMPURE)
192 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
193 inode->i_sb->s_id, inode->i_ino, current->comm);
197 if (rc > SMACK_UNCONFINED_OBJECT)
199 if (rc == SMACK_UNCONFINED_SUBJECT &&
200 (mode & (MAY_WRITE | MAY_APPEND)))
201 isp->smk_flags |= SMK_INODE_IMPURE;
203 smk_bu_mode(mode, acc);
205 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
206 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
207 inode->i_sb->s_id, inode->i_ino, current->comm);
211 #define smk_bu_inode(inode, mode, RC) (RC)
214 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
215 static int smk_bu_file(struct file *file, int mode, int rc)
217 struct task_smack *tsp = current_security();
218 struct smack_known *sskp = tsp->smk_task;
219 struct inode *inode = file_inode(file);
220 struct inode_smack *isp = inode->i_security;
221 char acc[SMK_NUM_ACCESS_TYPE + 1];
223 if (isp->smk_flags & SMK_INODE_IMPURE)
224 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
225 inode->i_sb->s_id, inode->i_ino, current->comm);
229 if (rc > SMACK_UNCONFINED_OBJECT)
232 smk_bu_mode(mode, acc);
233 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
234 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
235 inode->i_sb->s_id, inode->i_ino, file,
240 #define smk_bu_file(file, mode, RC) (RC)
243 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
244 static int smk_bu_credfile(const struct cred *cred, struct file *file,
247 struct task_smack *tsp = cred->security;
248 struct smack_known *sskp = tsp->smk_task;
249 struct inode *inode = file_inode(file);
250 struct inode_smack *isp = inode->i_security;
251 char acc[SMK_NUM_ACCESS_TYPE + 1];
253 if (isp->smk_flags & SMK_INODE_IMPURE)
254 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
255 inode->i_sb->s_id, inode->i_ino, current->comm);
259 if (rc > SMACK_UNCONFINED_OBJECT)
262 smk_bu_mode(mode, acc);
263 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
264 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
265 inode->i_sb->s_id, inode->i_ino, file,
270 #define smk_bu_credfile(cred, file, mode, RC) (RC)
274 * smk_fetch - Fetch the smack label from a file.
275 * @name: type of the label (attribute)
276 * @ip: a pointer to the inode
277 * @dp: a pointer to the dentry
279 * Returns a pointer to the master list entry for the Smack label,
280 * NULL if there was no label to fetch, or an error code.
282 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
287 struct smack_known *skp = NULL;
289 if (!(ip->i_opflags & IOP_XATTR))
290 return ERR_PTR(-EOPNOTSUPP);
292 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
294 return ERR_PTR(-ENOMEM);
296 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
302 skp = smk_import_entry(buffer, rc);
310 * new_inode_smack - allocate an inode security blob
311 * @skp: a pointer to the Smack label entry to use in the blob
313 * Returns the new blob or NULL if there's no memory available
315 static struct inode_smack *new_inode_smack(struct smack_known *skp)
317 struct inode_smack *isp;
319 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
323 isp->smk_inode = skp;
325 mutex_init(&isp->smk_lock);
331 * new_task_smack - allocate a task security blob
332 * @task: a pointer to the Smack label for the running task
333 * @forked: a pointer to the Smack label for the forked task
334 * @gfp: type of the memory for the allocation
336 * Returns the new blob or NULL if there's no memory available
338 static struct task_smack *new_task_smack(struct smack_known *task,
339 struct smack_known *forked, gfp_t gfp)
341 struct task_smack *tsp;
343 tsp = kzalloc(sizeof(struct task_smack), gfp);
347 tsp->smk_task = task;
348 tsp->smk_forked = forked;
349 INIT_LIST_HEAD(&tsp->smk_rules);
350 INIT_LIST_HEAD(&tsp->smk_relabel);
351 mutex_init(&tsp->smk_rules_lock);
357 * smk_copy_rules - copy a rule set
358 * @nhead: new rules header pointer
359 * @ohead: old rules header pointer
360 * @gfp: type of the memory for the allocation
362 * Returns 0 on success, -ENOMEM on error
364 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
367 struct smack_rule *nrp;
368 struct smack_rule *orp;
371 list_for_each_entry_rcu(orp, ohead, list) {
372 nrp = kzalloc(sizeof(struct smack_rule), gfp);
378 list_add_rcu(&nrp->list, nhead);
384 * smk_copy_relabel - copy smk_relabel labels list
385 * @nhead: new rules header pointer
386 * @ohead: old rules header pointer
387 * @gfp: type of the memory for the allocation
389 * Returns 0 on success, -ENOMEM on error
391 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
394 struct smack_known_list_elem *nklep;
395 struct smack_known_list_elem *oklep;
397 list_for_each_entry(oklep, ohead, list) {
398 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
400 smk_destroy_label_list(nhead);
403 nklep->smk_label = oklep->smk_label;
404 list_add(&nklep->list, nhead);
411 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
412 * @mode - input mode in form of PTRACE_MODE_*
414 * Returns a converted MAY_* mode usable by smack rules
416 static inline unsigned int smk_ptrace_mode(unsigned int mode)
418 if (mode & PTRACE_MODE_ATTACH)
419 return MAY_READWRITE;
420 if (mode & PTRACE_MODE_READ)
427 * smk_ptrace_rule_check - helper for ptrace access
428 * @tracer: tracer process
429 * @tracee_known: label entry of the process that's about to be traced
430 * @mode: ptrace attachment mode (PTRACE_MODE_*)
431 * @func: name of the function that called us, used for audit
433 * Returns 0 on access granted, -error on error
435 static int smk_ptrace_rule_check(struct task_struct *tracer,
436 struct smack_known *tracee_known,
437 unsigned int mode, const char *func)
440 struct smk_audit_info ad, *saip = NULL;
441 struct task_smack *tsp;
442 struct smack_known *tracer_known;
443 const struct cred *tracercred;
445 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
446 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
447 smk_ad_setfield_u_tsk(&ad, tracer);
452 tracercred = __task_cred(tracer);
453 tsp = tracercred->security;
454 tracer_known = smk_of_task(tsp);
456 if ((mode & PTRACE_MODE_ATTACH) &&
457 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
458 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
459 if (tracer_known->smk_known == tracee_known->smk_known)
461 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
463 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
469 smack_log(tracer_known->smk_known,
470 tracee_known->smk_known,
477 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
478 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
486 * We he, that is fun!
490 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
491 * @ctp: child task pointer
492 * @mode: ptrace attachment mode (PTRACE_MODE_*)
494 * Returns 0 if access is OK, an error code otherwise
496 * Do the capability checks.
498 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
500 struct smack_known *skp;
502 skp = smk_of_task_struct(ctp);
504 return smk_ptrace_rule_check(current, skp, mode, __func__);
508 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
509 * @ptp: parent task pointer
511 * Returns 0 if access is OK, an error code otherwise
513 * Do the capability checks, and require PTRACE_MODE_ATTACH.
515 static int smack_ptrace_traceme(struct task_struct *ptp)
518 struct smack_known *skp;
520 skp = smk_of_task(current_security());
522 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
527 * smack_syslog - Smack approval on syslog
528 * @type: message type
530 * Returns 0 on success, error code otherwise.
532 static int smack_syslog(int typefrom_file)
535 struct smack_known *skp = smk_of_current();
537 if (smack_privileged(CAP_MAC_OVERRIDE))
540 if (smack_syslog_label != NULL && smack_syslog_label != skp)
551 * smack_sb_alloc_security - allocate a superblock blob
552 * @sb: the superblock getting the blob
554 * Returns 0 on success or -ENOMEM on error.
556 static int smack_sb_alloc_security(struct super_block *sb)
558 struct superblock_smack *sbsp;
560 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
565 sbsp->smk_root = &smack_known_floor;
566 sbsp->smk_default = &smack_known_floor;
567 sbsp->smk_floor = &smack_known_floor;
568 sbsp->smk_hat = &smack_known_hat;
570 * SMK_SB_INITIALIZED will be zero from kzalloc.
572 sb->s_security = sbsp;
578 * smack_sb_free_security - free a superblock blob
579 * @sb: the superblock getting the blob
582 static void smack_sb_free_security(struct super_block *sb)
584 kfree(sb->s_security);
585 sb->s_security = NULL;
588 struct smack_mnt_opts {
589 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
592 static void smack_free_mnt_opts(void *mnt_opts)
594 struct smack_mnt_opts *opts = mnt_opts;
595 kfree(opts->fsdefault);
596 kfree(opts->fsfloor);
599 kfree(opts->fstransmute);
603 static int smack_add_opt(int token, const char *s, void **mnt_opts)
605 struct smack_mnt_opts *opts = *mnt_opts;
608 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
637 case Opt_fstransmute:
638 if (opts->fstransmute)
640 opts->fstransmute = s;
646 pr_warn("Smack: duplicate mount options\n");
651 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
652 * @fc: The new filesystem context.
653 * @src_fc: The source filesystem context being duplicated.
655 * Returns 0 on success or -ENOMEM on error.
657 static int smack_fs_context_dup(struct fs_context *fc,
658 struct fs_context *src_fc)
660 struct smack_mnt_opts *dst, *src = src_fc->security;
665 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
670 if (src->fsdefault) {
671 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
676 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
681 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
686 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
690 if (src->fstransmute) {
691 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
692 if (!dst->fstransmute)
698 static const struct fs_parameter_spec smack_param_specs[] = {
699 fsparam_string("fsdefault", Opt_fsdefault),
700 fsparam_string("fsfloor", Opt_fsfloor),
701 fsparam_string("fshat", Opt_fshat),
702 fsparam_string("fsroot", Opt_fsroot),
703 fsparam_string("fstransmute", Opt_fstransmute),
707 static const struct fs_parameter_description smack_fs_parameters = {
709 .specs = smack_param_specs,
713 * smack_fs_context_parse_param - Parse a single mount parameter
714 * @fc: The new filesystem context being constructed.
715 * @param: The parameter.
717 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
720 static int smack_fs_context_parse_param(struct fs_context *fc,
721 struct fs_parameter *param)
723 struct fs_parse_result result;
726 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
730 rc = smack_add_opt(opt, param->string, &fc->security);
732 param->string = NULL;
736 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
738 char *from = options, *to = options;
742 char *next = strchr(from, ',');
751 token = match_opt_prefix(from, len, &arg);
752 if (token != Opt_error) {
753 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
754 rc = smack_add_opt(token, arg, mnt_opts);
758 smack_free_mnt_opts(*mnt_opts);
763 if (!first) { // copy with preceding comma
768 memmove(to, from, len);
781 * smack_set_mnt_opts - set Smack specific mount options
782 * @sb: the file system superblock
783 * @opts: Smack mount options
784 * @kern_flags: mount option from kernel space or user space
785 * @set_kern_flags: where to store converted mount opts
787 * Returns 0 on success, an error code on failure
789 * Allow filesystems with binary mount data to explicitly set Smack mount
792 static int smack_set_mnt_opts(struct super_block *sb,
794 unsigned long kern_flags,
795 unsigned long *set_kern_flags)
797 struct dentry *root = sb->s_root;
798 struct inode *inode = d_backing_inode(root);
799 struct superblock_smack *sp = sb->s_security;
800 struct inode_smack *isp;
801 struct smack_known *skp;
802 struct smack_mnt_opts *opts = mnt_opts;
803 bool transmute = false;
805 if (sp->smk_flags & SMK_SB_INITIALIZED)
808 if (!smack_privileged(CAP_MAC_ADMIN)) {
810 * Unprivileged mounts don't get to specify Smack values.
815 * Unprivileged mounts get root and default from the caller.
817 skp = smk_of_current();
819 sp->smk_default = skp;
821 * For a handful of fs types with no user-controlled
822 * backing store it's okay to trust security labels
823 * in the filesystem. The rest are untrusted.
825 if (sb->s_user_ns != &init_user_ns &&
826 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
827 sb->s_magic != RAMFS_MAGIC) {
829 sp->smk_flags |= SMK_SB_UNTRUSTED;
833 sp->smk_flags |= SMK_SB_INITIALIZED;
836 if (opts->fsdefault) {
837 skp = smk_import_entry(opts->fsdefault, 0);
840 sp->smk_default = skp;
843 skp = smk_import_entry(opts->fsfloor, 0);
849 skp = smk_import_entry(opts->fshat, 0);
855 skp = smk_import_entry(opts->fsroot, 0);
860 if (opts->fstransmute) {
861 skp = smk_import_entry(opts->fstransmute, 0);
870 * Initialize the root inode.
872 isp = inode->i_security;
874 isp = new_inode_smack(sp->smk_root);
877 inode->i_security = isp;
879 isp->smk_inode = sp->smk_root;
882 isp->smk_flags |= SMK_INODE_TRANSMUTE;
888 * smack_sb_statfs - Smack check on statfs
889 * @dentry: identifies the file system in question
891 * Returns 0 if current can read the floor of the filesystem,
892 * and error code otherwise
894 static int smack_sb_statfs(struct dentry *dentry)
896 struct superblock_smack *sbp = dentry->d_sb->s_security;
898 struct smk_audit_info ad;
900 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
901 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
903 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
904 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
913 * smack_bprm_set_creds - set creds for exec
914 * @bprm: the exec information
916 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
918 static int smack_bprm_set_creds(struct linux_binprm *bprm)
920 struct inode *inode = file_inode(bprm->file);
921 struct task_smack *bsp = bprm->cred->security;
922 struct inode_smack *isp;
923 struct superblock_smack *sbsp;
926 if (bprm->called_set_creds)
929 isp = inode->i_security;
930 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
933 sbsp = inode->i_sb->s_security;
934 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
935 isp->smk_task != sbsp->smk_root)
938 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
939 struct task_struct *tracer;
943 tracer = ptrace_parent(current);
944 if (likely(tracer != NULL))
945 rc = smk_ptrace_rule_check(tracer,
953 } else if (bprm->unsafe)
956 bsp->smk_task = isp->smk_task;
957 bprm->per_clear |= PER_CLEAR_ON_SETID;
959 /* Decide if this is a secure exec. */
960 if (bsp->smk_task != bsp->smk_forked)
961 bprm->secureexec = 1;
971 * smack_inode_alloc_security - allocate an inode blob
972 * @inode: the inode in need of a blob
974 * Returns 0 if it gets a blob, -ENOMEM otherwise
976 static int smack_inode_alloc_security(struct inode *inode)
978 struct smack_known *skp = smk_of_current();
980 inode->i_security = new_inode_smack(skp);
981 if (inode->i_security == NULL)
987 * smack_inode_free_rcu - Free inode_smack blob from cache
988 * @head: the rcu_head for getting inode_smack pointer
990 * Call back function called from call_rcu() to free
991 * the i_security blob pointer in inode
993 static void smack_inode_free_rcu(struct rcu_head *head)
995 struct inode_smack *issp;
997 issp = container_of(head, struct inode_smack, smk_rcu);
998 kmem_cache_free(smack_inode_cache, issp);
1002 * smack_inode_free_security - free an inode blob using call_rcu()
1003 * @inode: the inode with a blob
1005 * Clears the blob pointer in inode using RCU
1007 static void smack_inode_free_security(struct inode *inode)
1009 struct inode_smack *issp = inode->i_security;
1012 * The inode may still be referenced in a path walk and
1013 * a call to smack_inode_permission() can be made
1014 * after smack_inode_free_security() is called.
1015 * To avoid race condition free the i_security via RCU
1016 * and leave the current inode->i_security pointer intact.
1017 * The inode will be freed after the RCU grace period too.
1019 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
1023 * smack_inode_init_security - copy out the smack from an inode
1024 * @inode: the newly created inode
1025 * @dir: containing directory object
1027 * @name: where to put the attribute name
1028 * @value: where to put the attribute value
1029 * @len: where to put the length of the attribute
1031 * Returns 0 if it all works out, -ENOMEM if there's no memory
1033 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
1034 const struct qstr *qstr, const char **name,
1035 void **value, size_t *len)
1037 struct inode_smack *issp = inode->i_security;
1038 struct smack_known *skp = smk_of_current();
1039 struct smack_known *isp = smk_of_inode(inode);
1040 struct smack_known *dsp = smk_of_inode(dir);
1044 *name = XATTR_SMACK_SUFFIX;
1048 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1053 * If the access rule allows transmutation and
1054 * the directory requests transmutation then
1055 * by all means transmute.
1056 * Mark the inode as changed.
1058 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1059 smk_inode_transmutable(dir)) {
1061 issp->smk_flags |= SMK_INODE_CHANGED;
1064 *value = kstrdup(isp->smk_known, GFP_NOFS);
1068 *len = strlen(isp->smk_known);
1075 * smack_inode_link - Smack check on link
1076 * @old_dentry: the existing object
1078 * @new_dentry: the new object
1080 * Returns 0 if access is permitted, an error code otherwise
1082 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1083 struct dentry *new_dentry)
1085 struct smack_known *isp;
1086 struct smk_audit_info ad;
1089 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1090 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1092 isp = smk_of_inode(d_backing_inode(old_dentry));
1093 rc = smk_curacc(isp, MAY_WRITE, &ad);
1094 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1096 if (rc == 0 && d_is_positive(new_dentry)) {
1097 isp = smk_of_inode(d_backing_inode(new_dentry));
1098 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1099 rc = smk_curacc(isp, MAY_WRITE, &ad);
1100 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1107 * smack_inode_unlink - Smack check on inode deletion
1108 * @dir: containing directory object
1109 * @dentry: file to unlink
1111 * Returns 0 if current can write the containing directory
1112 * and the object, error code otherwise
1114 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1116 struct inode *ip = d_backing_inode(dentry);
1117 struct smk_audit_info ad;
1120 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1121 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1124 * You need write access to the thing you're unlinking
1126 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1127 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1130 * You also need write access to the containing directory
1132 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1133 smk_ad_setfield_u_fs_inode(&ad, dir);
1134 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1135 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1141 * smack_inode_rmdir - Smack check on directory deletion
1142 * @dir: containing directory object
1143 * @dentry: directory to unlink
1145 * Returns 0 if current can write the containing directory
1146 * and the directory, error code otherwise
1148 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1150 struct smk_audit_info ad;
1153 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1154 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1157 * You need write access to the thing you're removing
1159 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1160 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1163 * You also need write access to the containing directory
1165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1166 smk_ad_setfield_u_fs_inode(&ad, dir);
1167 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1168 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1175 * smack_inode_rename - Smack check on rename
1176 * @old_inode: unused
1177 * @old_dentry: the old object
1178 * @new_inode: unused
1179 * @new_dentry: the new object
1181 * Read and write access is required on both the old and
1184 * Returns 0 if access is permitted, an error code otherwise
1186 static int smack_inode_rename(struct inode *old_inode,
1187 struct dentry *old_dentry,
1188 struct inode *new_inode,
1189 struct dentry *new_dentry)
1192 struct smack_known *isp;
1193 struct smk_audit_info ad;
1195 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1196 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1198 isp = smk_of_inode(d_backing_inode(old_dentry));
1199 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1200 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1202 if (rc == 0 && d_is_positive(new_dentry)) {
1203 isp = smk_of_inode(d_backing_inode(new_dentry));
1204 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1205 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1206 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1212 * smack_inode_permission - Smack version of permission()
1213 * @inode: the inode in question
1214 * @mask: the access requested
1216 * This is the important Smack hook.
1218 * Returns 0 if access is permitted, -EACCES otherwise
1220 static int smack_inode_permission(struct inode *inode, int mask)
1222 struct superblock_smack *sbsp = inode->i_sb->s_security;
1223 struct smk_audit_info ad;
1224 int no_block = mask & MAY_NOT_BLOCK;
1227 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1229 * No permission to check. Existence test. Yup, it's there.
1234 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1235 if (smk_of_inode(inode) != sbsp->smk_root)
1239 /* May be droppable after audit */
1242 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1243 smk_ad_setfield_u_fs_inode(&ad, inode);
1244 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1245 rc = smk_bu_inode(inode, mask, rc);
1250 * smack_inode_setattr - Smack check for setting attributes
1251 * @dentry: the object
1252 * @iattr: for the force flag
1254 * Returns 0 if access is permitted, an error code otherwise
1256 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1258 struct smk_audit_info ad;
1262 * Need to allow for clearing the setuid bit.
1264 if (iattr->ia_valid & ATTR_FORCE)
1266 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1267 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1269 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1270 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1275 * smack_inode_getattr - Smack check for getting attributes
1276 * @mnt: vfsmount of the object
1277 * @dentry: the object
1279 * Returns 0 if access is permitted, an error code otherwise
1281 static int smack_inode_getattr(const struct path *path)
1283 struct smk_audit_info ad;
1284 struct inode *inode = d_backing_inode(path->dentry);
1287 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1288 smk_ad_setfield_u_fs_path(&ad, *path);
1289 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1290 rc = smk_bu_inode(inode, MAY_READ, rc);
1295 * smack_inode_setxattr - Smack check for setting xattrs
1296 * @dentry: the object
1297 * @name: name of the attribute
1298 * @value: value of the attribute
1299 * @size: size of the value
1302 * This protects the Smack attribute explicitly.
1304 * Returns 0 if access is permitted, an error code otherwise
1306 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1307 const void *value, size_t size, int flags)
1309 struct smk_audit_info ad;
1310 struct smack_known *skp;
1312 int check_import = 0;
1317 * Check label validity here so import won't fail in post_setxattr
1319 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1320 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1321 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1324 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1325 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1329 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1331 if (size != TRANS_TRUE_SIZE ||
1332 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1335 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1337 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1340 if (rc == 0 && check_import) {
1341 skp = size ? smk_import_entry(value, size) : NULL;
1344 else if (skp == NULL || (check_star &&
1345 (skp == &smack_known_star || skp == &smack_known_web)))
1349 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1350 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1353 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1354 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1361 * smack_inode_post_setxattr - Apply the Smack update approved above
1363 * @name: attribute name
1364 * @value: attribute value
1365 * @size: attribute size
1368 * Set the pointer in the inode blob to the entry found
1369 * in the master label list.
1371 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1372 const void *value, size_t size, int flags)
1374 struct smack_known *skp;
1375 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
1377 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1378 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1382 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1383 skp = smk_import_entry(value, size);
1385 isp->smk_inode = skp;
1386 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1387 skp = smk_import_entry(value, size);
1389 isp->smk_task = skp;
1390 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1391 skp = smk_import_entry(value, size);
1393 isp->smk_mmap = skp;
1400 * smack_inode_getxattr - Smack check on getxattr
1401 * @dentry: the object
1404 * Returns 0 if access is permitted, an error code otherwise
1406 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1408 struct smk_audit_info ad;
1411 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1412 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1414 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1415 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1420 * smack_inode_removexattr - Smack check on removexattr
1421 * @dentry: the object
1422 * @name: name of the attribute
1424 * Removing the Smack attribute requires CAP_MAC_ADMIN
1426 * Returns 0 if access is permitted, an error code otherwise
1428 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1430 struct inode_smack *isp;
1431 struct smk_audit_info ad;
1434 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1435 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1436 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1437 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1438 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1439 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1440 if (!smack_privileged(CAP_MAC_ADMIN))
1443 rc = cap_inode_removexattr(dentry, name);
1448 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1449 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1451 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1452 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1456 isp = d_backing_inode(dentry)->i_security;
1458 * Don't do anything special for these.
1459 * XATTR_NAME_SMACKIPIN
1460 * XATTR_NAME_SMACKIPOUT
1462 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1463 struct super_block *sbp = dentry->d_sb;
1464 struct superblock_smack *sbsp = sbp->s_security;
1466 isp->smk_inode = sbsp->smk_default;
1467 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1468 isp->smk_task = NULL;
1469 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1470 isp->smk_mmap = NULL;
1471 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1472 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1478 * smack_inode_getsecurity - get smack xattrs
1479 * @inode: the object
1480 * @name: attribute name
1481 * @buffer: where to put the result
1482 * @alloc: duplicate memory
1484 * Returns the size of the attribute or an error code
1486 static int smack_inode_getsecurity(struct inode *inode,
1487 const char *name, void **buffer,
1490 struct socket_smack *ssp;
1491 struct socket *sock;
1492 struct super_block *sbp;
1493 struct inode *ip = (struct inode *)inode;
1494 struct smack_known *isp;
1496 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1497 isp = smk_of_inode(inode);
1500 * The rest of the Smack xattrs are only on sockets.
1503 if (sbp->s_magic != SOCKFS_MAGIC)
1506 sock = SOCKET_I(ip);
1507 if (sock == NULL || sock->sk == NULL)
1510 ssp = sock->sk->sk_security;
1512 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1514 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1521 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1522 if (*buffer == NULL)
1526 return strlen(isp->smk_known);
1531 * smack_inode_listsecurity - list the Smack attributes
1532 * @inode: the object
1533 * @buffer: where they go
1534 * @buffer_size: size of buffer
1536 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1539 int len = sizeof(XATTR_NAME_SMACK);
1541 if (buffer != NULL && len <= buffer_size)
1542 memcpy(buffer, XATTR_NAME_SMACK, len);
1548 * smack_inode_getsecid - Extract inode's security id
1549 * @inode: inode to extract the info from
1550 * @secid: where result will be saved
1552 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1554 struct smack_known *skp = smk_of_inode(inode);
1556 *secid = skp->smk_secid;
1564 * There is no smack_file_permission hook
1566 * Should access checks be done on each read or write?
1567 * UNICOS and SELinux say yes.
1568 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1570 * I'll say no for now. Smack does not do the frequent
1571 * label changing that SELinux does.
1575 * smack_file_alloc_security - assign a file security blob
1578 * The security blob for a file is a pointer to the master
1579 * label list, so no allocation is done.
1581 * f_security is the owner security information. It
1582 * isn't used on file access checks, it's for send_sigio.
1586 static int smack_file_alloc_security(struct file *file)
1588 struct smack_known *skp = smk_of_current();
1590 file->f_security = skp;
1595 * smack_file_free_security - clear a file security blob
1598 * The security blob for a file is a pointer to the master
1599 * label list, so no memory is freed.
1601 static void smack_file_free_security(struct file *file)
1603 file->f_security = NULL;
1607 * smack_file_ioctl - Smack check on ioctls
1612 * Relies heavily on the correct use of the ioctl command conventions.
1614 * Returns 0 if allowed, error code otherwise
1616 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1620 struct smk_audit_info ad;
1621 struct inode *inode = file_inode(file);
1623 if (unlikely(IS_PRIVATE(inode)))
1626 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1627 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1629 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1630 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1631 rc = smk_bu_file(file, MAY_WRITE, rc);
1634 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1635 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1636 rc = smk_bu_file(file, MAY_READ, rc);
1643 * smack_file_lock - Smack check on file locking
1647 * Returns 0 if current has lock access, error code otherwise
1649 static int smack_file_lock(struct file *file, unsigned int cmd)
1651 struct smk_audit_info ad;
1653 struct inode *inode = file_inode(file);
1655 if (unlikely(IS_PRIVATE(inode)))
1658 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1659 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1660 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1661 rc = smk_bu_file(file, MAY_LOCK, rc);
1666 * smack_file_fcntl - Smack check on fcntl
1668 * @cmd: what action to check
1671 * Generally these operations are harmless.
1672 * File locking operations present an obvious mechanism
1673 * for passing information, so they require write access.
1675 * Returns 0 if current has access, error code otherwise
1677 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1680 struct smk_audit_info ad;
1682 struct inode *inode = file_inode(file);
1684 if (unlikely(IS_PRIVATE(inode)))
1692 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1693 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1694 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1695 rc = smk_bu_file(file, MAY_LOCK, rc);
1699 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1700 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1701 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1702 rc = smk_bu_file(file, MAY_WRITE, rc);
1713 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1714 * if mapping anonymous memory.
1715 * @file contains the file structure for file to map (may be NULL).
1716 * @reqprot contains the protection requested by the application.
1717 * @prot contains the protection that will be applied by the kernel.
1718 * @flags contains the operational flags.
1719 * Return 0 if permission is granted.
1721 static int smack_mmap_file(struct file *file,
1722 unsigned long reqprot, unsigned long prot,
1723 unsigned long flags)
1725 struct smack_known *skp;
1726 struct smack_known *mkp;
1727 struct smack_rule *srp;
1728 struct task_smack *tsp;
1729 struct smack_known *okp;
1730 struct inode_smack *isp;
1731 struct superblock_smack *sbsp;
1740 if (unlikely(IS_PRIVATE(file_inode(file))))
1743 isp = file_inode(file)->i_security;
1744 if (isp->smk_mmap == NULL)
1746 sbsp = file_inode(file)->i_sb->s_security;
1747 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1748 isp->smk_mmap != sbsp->smk_root)
1750 mkp = isp->smk_mmap;
1752 tsp = current_security();
1753 skp = smk_of_current();
1758 * For each Smack rule associated with the subject
1759 * label verify that the SMACK64MMAP also has access
1760 * to that rule's object label.
1762 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1763 okp = srp->smk_object;
1765 * Matching labels always allows access.
1767 if (mkp->smk_known == okp->smk_known)
1770 * If there is a matching local rule take
1771 * that into account as well.
1773 may = smk_access_entry(srp->smk_subject->smk_known,
1777 may = srp->smk_access;
1779 may &= srp->smk_access;
1781 * If may is zero the SMACK64MMAP subject can't
1782 * possibly have less access.
1788 * Fetch the global list entry.
1789 * If there isn't one a SMACK64MMAP subject
1790 * can't have as much access as current.
1792 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1794 if (mmay == -ENOENT) {
1799 * If there is a local entry it modifies the
1800 * potential access, too.
1802 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1804 if (tmay != -ENOENT)
1808 * If there is any access available to current that is
1809 * not available to a SMACK64MMAP subject
1812 if ((may | mmay) != mmay) {
1824 * smack_file_set_fowner - set the file security blob value
1825 * @file: object in question
1828 static void smack_file_set_fowner(struct file *file)
1830 file->f_security = smk_of_current();
1834 * smack_file_send_sigiotask - Smack on sigio
1835 * @tsk: The target task
1836 * @fown: the object the signal come from
1839 * Allow a privileged task to get signals even if it shouldn't
1841 * Returns 0 if a subject with the object's smack could
1842 * write to the task, an error code otherwise.
1844 static int smack_file_send_sigiotask(struct task_struct *tsk,
1845 struct fown_struct *fown, int signum)
1847 struct smack_known *skp;
1848 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1849 const struct cred *tcred;
1852 struct smk_audit_info ad;
1855 * struct fown_struct is never outside the context of a struct file
1857 file = container_of(fown, struct file, f_owner);
1859 /* we don't log here as rc can be overriden */
1860 skp = file->f_security;
1861 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1862 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1865 tcred = __task_cred(tsk);
1866 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1870 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1871 smk_ad_setfield_u_tsk(&ad, tsk);
1872 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1877 * smack_file_receive - Smack file receive check
1880 * Returns 0 if current has access, error code otherwise
1882 static int smack_file_receive(struct file *file)
1886 struct smk_audit_info ad;
1887 struct inode *inode = file_inode(file);
1888 struct socket *sock;
1889 struct task_smack *tsp;
1890 struct socket_smack *ssp;
1892 if (unlikely(IS_PRIVATE(inode)))
1895 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1896 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1898 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1899 sock = SOCKET_I(inode);
1900 ssp = sock->sk->sk_security;
1901 tsp = current_security();
1903 * If the receiving process can't write to the
1904 * passed socket or if the passed socket can't
1905 * write to the receiving process don't accept
1906 * the passed socket.
1908 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1909 rc = smk_bu_file(file, may, rc);
1912 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1913 rc = smk_bu_file(file, may, rc);
1917 * This code relies on bitmasks.
1919 if (file->f_mode & FMODE_READ)
1921 if (file->f_mode & FMODE_WRITE)
1924 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1925 rc = smk_bu_file(file, may, rc);
1930 * smack_file_open - Smack dentry open processing
1932 * @cred: task credential
1934 * Set the security blob in the file structure.
1935 * Allow the open only if the task has read access. There are
1936 * many read operations (e.g. fstat) that you can do with an
1937 * fd even if you have the file open write-only.
1941 static int smack_file_open(struct file *file)
1943 struct task_smack *tsp = file->f_cred->security;
1944 struct inode *inode = file_inode(file);
1945 struct smk_audit_info ad;
1948 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1949 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1950 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1951 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1961 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1962 * @new: the new credentials
1963 * @gfp: the atomicity of any memory allocations
1965 * Prepare a blank set of credentials for modification. This must allocate all
1966 * the memory the LSM module might require such that cred_transfer() can
1967 * complete without error.
1969 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1971 struct task_smack *tsp;
1973 tsp = new_task_smack(NULL, NULL, gfp);
1977 cred->security = tsp;
1984 * smack_cred_free - "free" task-level security credentials
1985 * @cred: the credentials in question
1988 static void smack_cred_free(struct cred *cred)
1990 struct task_smack *tsp = cred->security;
1991 struct smack_rule *rp;
1992 struct list_head *l;
1993 struct list_head *n;
1997 cred->security = NULL;
1999 smk_destroy_label_list(&tsp->smk_relabel);
2001 list_for_each_safe(l, n, &tsp->smk_rules) {
2002 rp = list_entry(l, struct smack_rule, list);
2003 list_del(&rp->list);
2010 * smack_cred_prepare - prepare new set of credentials for modification
2011 * @new: the new credentials
2012 * @old: the original credentials
2013 * @gfp: the atomicity of any memory allocations
2015 * Prepare a new set of credentials for modification.
2017 static int smack_cred_prepare(struct cred *new, const struct cred *old,
2020 struct task_smack *old_tsp = old->security;
2021 struct task_smack *new_tsp;
2024 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
2025 if (new_tsp == NULL)
2028 new->security = new_tsp;
2030 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2034 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2043 * smack_cred_transfer - Transfer the old credentials to the new credentials
2044 * @new: the new credentials
2045 * @old: the original credentials
2047 * Fill in a set of blank credentials from another set of credentials.
2049 static void smack_cred_transfer(struct cred *new, const struct cred *old)
2051 struct task_smack *old_tsp = old->security;
2052 struct task_smack *new_tsp = new->security;
2054 new_tsp->smk_task = old_tsp->smk_task;
2055 new_tsp->smk_forked = old_tsp->smk_task;
2056 mutex_init(&new_tsp->smk_rules_lock);
2057 INIT_LIST_HEAD(&new_tsp->smk_rules);
2060 /* cbs copy rule list */
2064 * smack_cred_getsecid - get the secid corresponding to a creds structure
2065 * @c: the object creds
2066 * @secid: where to put the result
2068 * Sets the secid to contain a u32 version of the smack label.
2070 static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2072 struct smack_known *skp;
2075 skp = smk_of_task(c->security);
2076 *secid = skp->smk_secid;
2081 * smack_kernel_act_as - Set the subjective context in a set of credentials
2082 * @new: points to the set of credentials to be modified.
2083 * @secid: specifies the security ID to be set
2085 * Set the security data for a kernel service.
2087 static int smack_kernel_act_as(struct cred *new, u32 secid)
2089 struct task_smack *new_tsp = new->security;
2091 new_tsp->smk_task = smack_from_secid(secid);
2096 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2097 * @new: points to the set of credentials to be modified
2098 * @inode: points to the inode to use as a reference
2100 * Set the file creation context in a set of credentials to the same
2101 * as the objective context of the specified inode
2103 static int smack_kernel_create_files_as(struct cred *new,
2104 struct inode *inode)
2106 struct inode_smack *isp = inode->i_security;
2107 struct task_smack *tsp = new->security;
2109 tsp->smk_forked = isp->smk_inode;
2110 tsp->smk_task = tsp->smk_forked;
2115 * smk_curacc_on_task - helper to log task related access
2116 * @p: the task object
2117 * @access: the access requested
2118 * @caller: name of the calling function for audit
2120 * Return 0 if access is permitted
2122 static int smk_curacc_on_task(struct task_struct *p, int access,
2125 struct smk_audit_info ad;
2126 struct smack_known *skp = smk_of_task_struct(p);
2129 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2130 smk_ad_setfield_u_tsk(&ad, p);
2131 rc = smk_curacc(skp, access, &ad);
2132 rc = smk_bu_task(p, access, rc);
2137 * smack_task_setpgid - Smack check on setting pgid
2138 * @p: the task object
2141 * Return 0 if write access is permitted
2143 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2145 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2149 * smack_task_getpgid - Smack access check for getpgid
2150 * @p: the object task
2152 * Returns 0 if current can read the object task, error code otherwise
2154 static int smack_task_getpgid(struct task_struct *p)
2156 return smk_curacc_on_task(p, MAY_READ, __func__);
2160 * smack_task_getsid - Smack access check for getsid
2161 * @p: the object task
2163 * Returns 0 if current can read the object task, error code otherwise
2165 static int smack_task_getsid(struct task_struct *p)
2167 return smk_curacc_on_task(p, MAY_READ, __func__);
2171 * smack_task_getsecid - get the secid of the task
2172 * @p: the object task
2173 * @secid: where to put the result
2175 * Sets the secid to contain a u32 version of the smack label.
2177 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2179 struct smack_known *skp = smk_of_task_struct(p);
2181 *secid = skp->smk_secid;
2185 * smack_task_setnice - Smack check on setting nice
2186 * @p: the task object
2189 * Return 0 if write access is permitted
2191 static int smack_task_setnice(struct task_struct *p, int nice)
2193 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2197 * smack_task_setioprio - Smack check on setting ioprio
2198 * @p: the task object
2201 * Return 0 if write access is permitted
2203 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2205 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2209 * smack_task_getioprio - Smack check on reading ioprio
2210 * @p: the task object
2212 * Return 0 if read access is permitted
2214 static int smack_task_getioprio(struct task_struct *p)
2216 return smk_curacc_on_task(p, MAY_READ, __func__);
2220 * smack_task_setscheduler - Smack check on setting scheduler
2221 * @p: the task object
2225 * Return 0 if read access is permitted
2227 static int smack_task_setscheduler(struct task_struct *p)
2229 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2233 * smack_task_getscheduler - Smack check on reading scheduler
2234 * @p: the task object
2236 * Return 0 if read access is permitted
2238 static int smack_task_getscheduler(struct task_struct *p)
2240 return smk_curacc_on_task(p, MAY_READ, __func__);
2244 * smack_task_movememory - Smack check on moving memory
2245 * @p: the task object
2247 * Return 0 if write access is permitted
2249 static int smack_task_movememory(struct task_struct *p)
2251 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2255 * smack_task_kill - Smack check on signal delivery
2256 * @p: the task object
2259 * @cred: identifies the cred to use in lieu of current's
2261 * Return 0 if write access is permitted
2264 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2265 int sig, const struct cred *cred)
2267 struct smk_audit_info ad;
2268 struct smack_known *skp;
2269 struct smack_known *tkp = smk_of_task_struct(p);
2273 return 0; /* null signal; existence test */
2275 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2276 smk_ad_setfield_u_tsk(&ad, p);
2278 * Sending a signal requires that the sender
2279 * can write the receiver.
2282 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2283 rc = smk_bu_task(p, MAY_DELIVER, rc);
2287 * If the cred isn't NULL we're dealing with some USB IO
2288 * specific behavior. This is not clean. For one thing
2289 * we can't take privilege into account.
2291 skp = smk_of_task(cred->security);
2292 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2293 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2298 * smack_task_to_inode - copy task smack into the inode blob
2299 * @p: task to copy from
2300 * @inode: inode to copy to
2302 * Sets the smack pointer in the inode security blob
2304 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2306 struct inode_smack *isp = inode->i_security;
2307 struct smack_known *skp = smk_of_task_struct(p);
2309 isp->smk_inode = skp;
2310 isp->smk_flags |= SMK_INODE_INSTANT;
2318 * smack_sk_alloc_security - Allocate a socket blob
2321 * @gfp_flags: memory allocation flags
2323 * Assign Smack pointers to current
2325 * Returns 0 on success, -ENOMEM is there's no memory
2327 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2329 struct smack_known *skp = smk_of_current();
2330 struct socket_smack *ssp;
2332 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2337 * Sockets created by kernel threads receive web label.
2339 if (unlikely(current->flags & PF_KTHREAD)) {
2340 ssp->smk_in = &smack_known_web;
2341 ssp->smk_out = &smack_known_web;
2346 ssp->smk_packet = NULL;
2348 sk->sk_security = ssp;
2354 * smack_sk_free_security - Free a socket blob
2357 * Clears the blob pointer
2359 static void smack_sk_free_security(struct sock *sk)
2361 #ifdef SMACK_IPV6_PORT_LABELING
2362 struct smk_port_label *spp;
2364 if (sk->sk_family == PF_INET6) {
2366 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2367 if (spp->smk_sock != sk)
2369 spp->smk_can_reuse = 1;
2375 kfree(sk->sk_security);
2379 * smack_ipv4host_label - check host based restrictions
2380 * @sip: the object end
2382 * looks for host based access restrictions
2384 * This version will only be appropriate for really small sets of single label
2385 * hosts. The caller is responsible for ensuring that the RCU read lock is
2386 * taken before calling this function.
2388 * Returns the label of the far end or NULL if it's not special.
2390 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2392 struct smk_net4addr *snp;
2393 struct in_addr *siap = &sip->sin_addr;
2395 if (siap->s_addr == 0)
2398 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2400 * we break after finding the first match because
2401 * the list is sorted from longest to shortest mask
2402 * so we have found the most specific match
2404 if (snp->smk_host.s_addr ==
2405 (siap->s_addr & snp->smk_mask.s_addr))
2406 return snp->smk_label;
2411 #if IS_ENABLED(CONFIG_IPV6)
2413 * smk_ipv6_localhost - Check for local ipv6 host address
2416 * Returns boolean true if this is the localhost address
2418 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2420 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2421 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2423 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2424 ntohs(be16p[7]) == 1)
2430 * smack_ipv6host_label - check host based restrictions
2431 * @sip: the object end
2433 * looks for host based access restrictions
2435 * This version will only be appropriate for really small sets of single label
2436 * hosts. The caller is responsible for ensuring that the RCU read lock is
2437 * taken before calling this function.
2439 * Returns the label of the far end or NULL if it's not special.
2441 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2443 struct smk_net6addr *snp;
2444 struct in6_addr *sap = &sip->sin6_addr;
2449 * It's local. Don't look for a host label.
2451 if (smk_ipv6_localhost(sip))
2454 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2456 * If the label is NULL the entry has
2457 * been renounced. Ignore it.
2459 if (snp->smk_label == NULL)
2462 * we break after finding the first match because
2463 * the list is sorted from longest to shortest mask
2464 * so we have found the most specific match
2466 for (found = 1, i = 0; i < 8; i++) {
2467 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2468 snp->smk_host.s6_addr16[i]) {
2474 return snp->smk_label;
2479 #endif /* CONFIG_IPV6 */
2482 * smack_netlabel - Set the secattr on a socket
2484 * @labeled: socket label scheme
2486 * Convert the outbound smack value (smk_out) to a
2487 * secattr and attach it to the socket.
2489 * Returns 0 on success or an error code
2491 static int smack_netlabel(struct sock *sk, int labeled)
2493 struct smack_known *skp;
2494 struct socket_smack *ssp = sk->sk_security;
2498 * Usually the netlabel code will handle changing the
2499 * packet labeling based on the label.
2500 * The case of a single label host is different, because
2501 * a single label host should never get a labeled packet
2502 * even though the label is usually associated with a packet
2506 bh_lock_sock_nested(sk);
2508 if (ssp->smk_out == smack_net_ambient ||
2509 labeled == SMACK_UNLABELED_SOCKET)
2510 netlbl_sock_delattr(sk);
2513 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2523 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2525 * @sap: the destination address
2527 * Set the correct secattr for the given socket based on the destination
2528 * address and perform any outbound access checks needed.
2530 * Returns 0 on success or an error code.
2533 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2535 struct smack_known *skp;
2538 struct smack_known *hkp;
2539 struct socket_smack *ssp = sk->sk_security;
2540 struct smk_audit_info ad;
2543 hkp = smack_ipv4host_label(sap);
2546 struct lsm_network_audit net;
2548 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2549 ad.a.u.net->family = sap->sin_family;
2550 ad.a.u.net->dport = sap->sin_port;
2551 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2553 sk_lbl = SMACK_UNLABELED_SOCKET;
2555 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2556 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2558 sk_lbl = SMACK_CIPSO_SOCKET;
2565 return smack_netlabel(sk, sk_lbl);
2568 #if IS_ENABLED(CONFIG_IPV6)
2570 * smk_ipv6_check - check Smack access
2571 * @subject: subject Smack label
2572 * @object: object Smack label
2574 * @act: the action being taken
2576 * Check an IPv6 access
2578 static int smk_ipv6_check(struct smack_known *subject,
2579 struct smack_known *object,
2580 struct sockaddr_in6 *address, int act)
2583 struct lsm_network_audit net;
2585 struct smk_audit_info ad;
2589 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2590 ad.a.u.net->family = PF_INET6;
2591 ad.a.u.net->dport = ntohs(address->sin6_port);
2592 if (act == SMK_RECEIVING)
2593 ad.a.u.net->v6info.saddr = address->sin6_addr;
2595 ad.a.u.net->v6info.daddr = address->sin6_addr;
2597 rc = smk_access(subject, object, MAY_WRITE, &ad);
2598 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2601 #endif /* CONFIG_IPV6 */
2603 #ifdef SMACK_IPV6_PORT_LABELING
2605 * smk_ipv6_port_label - Smack port access table management
2609 * Create or update the port list entry
2611 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2613 struct sock *sk = sock->sk;
2614 struct sockaddr_in6 *addr6;
2615 struct socket_smack *ssp = sock->sk->sk_security;
2616 struct smk_port_label *spp;
2617 unsigned short port = 0;
2619 if (address == NULL) {
2621 * This operation is changing the Smack information
2622 * on the bound socket. Take the changes to the port
2626 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2627 if (sk != spp->smk_sock)
2629 spp->smk_in = ssp->smk_in;
2630 spp->smk_out = ssp->smk_out;
2635 * A NULL address is only used for updating existing
2636 * bound entries. If there isn't one, it's OK.
2642 addr6 = (struct sockaddr_in6 *)address;
2643 port = ntohs(addr6->sin6_port);
2645 * This is a special case that is safely ignored.
2651 * Look for an existing port list entry.
2652 * This is an indication that a port is getting reused.
2655 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2656 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2658 if (spp->smk_can_reuse != 1) {
2662 spp->smk_port = port;
2664 spp->smk_in = ssp->smk_in;
2665 spp->smk_out = ssp->smk_out;
2666 spp->smk_can_reuse = 0;
2672 * A new port entry is required.
2674 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2678 spp->smk_port = port;
2680 spp->smk_in = ssp->smk_in;
2681 spp->smk_out = ssp->smk_out;
2682 spp->smk_sock_type = sock->type;
2683 spp->smk_can_reuse = 0;
2685 mutex_lock(&smack_ipv6_lock);
2686 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2687 mutex_unlock(&smack_ipv6_lock);
2692 * smk_ipv6_port_check - check Smack port access
2696 * Create or update the port list entry
2698 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2701 struct smk_port_label *spp;
2702 struct socket_smack *ssp = sk->sk_security;
2703 struct smack_known *skp = NULL;
2704 unsigned short port;
2705 struct smack_known *object;
2707 if (act == SMK_RECEIVING) {
2708 skp = smack_ipv6host_label(address);
2709 object = ssp->smk_in;
2712 object = smack_ipv6host_label(address);
2716 * The other end is a single label host.
2718 if (skp != NULL && object != NULL)
2719 return smk_ipv6_check(skp, object, address, act);
2721 skp = smack_net_ambient;
2723 object = smack_net_ambient;
2726 * It's remote, so port lookup does no good.
2728 if (!smk_ipv6_localhost(address))
2729 return smk_ipv6_check(skp, object, address, act);
2732 * It's local so the send check has to have passed.
2734 if (act == SMK_RECEIVING)
2737 port = ntohs(address->sin6_port);
2739 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2740 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2742 object = spp->smk_in;
2743 if (act == SMK_CONNECTING)
2744 ssp->smk_packet = spp->smk_out;
2749 return smk_ipv6_check(skp, object, address, act);
2751 #endif /* SMACK_IPV6_PORT_LABELING */
2754 * smack_inode_setsecurity - set smack xattrs
2755 * @inode: the object
2756 * @name: attribute name
2757 * @value: attribute value
2758 * @size: size of the attribute
2761 * Sets the named attribute in the appropriate blob
2763 * Returns 0 on success, or an error code
2765 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2766 const void *value, size_t size, int flags)
2768 struct smack_known *skp;
2769 struct inode_smack *nsp = inode->i_security;
2770 struct socket_smack *ssp;
2771 struct socket *sock;
2774 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2777 skp = smk_import_entry(value, size);
2779 return PTR_ERR(skp);
2781 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2782 nsp->smk_inode = skp;
2783 nsp->smk_flags |= SMK_INODE_INSTANT;
2787 * The rest of the Smack xattrs are only on sockets.
2789 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2792 sock = SOCKET_I(inode);
2793 if (sock == NULL || sock->sk == NULL)
2796 ssp = sock->sk->sk_security;
2798 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2800 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2802 if (sock->sk->sk_family == PF_INET) {
2803 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2806 "Smack: \"%s\" netlbl error %d.\n",
2812 #ifdef SMACK_IPV6_PORT_LABELING
2813 if (sock->sk->sk_family == PF_INET6)
2814 smk_ipv6_port_label(sock, NULL);
2821 * smack_socket_post_create - finish socket setup
2823 * @family: protocol family
2828 * Sets the netlabel information on the socket
2830 * Returns 0 on success, and error code otherwise
2832 static int smack_socket_post_create(struct socket *sock, int family,
2833 int type, int protocol, int kern)
2835 struct socket_smack *ssp;
2837 if (sock->sk == NULL)
2841 * Sockets created by kernel threads receive web label.
2843 if (unlikely(current->flags & PF_KTHREAD)) {
2844 ssp = sock->sk->sk_security;
2845 ssp->smk_in = &smack_known_web;
2846 ssp->smk_out = &smack_known_web;
2849 if (family != PF_INET)
2852 * Set the outbound netlbl.
2854 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2858 * smack_socket_socketpair - create socket pair
2859 * @socka: one socket
2860 * @sockb: another socket
2862 * Cross reference the peer labels for SO_PEERSEC
2864 * Returns 0 on success, and error code otherwise
2866 static int smack_socket_socketpair(struct socket *socka,
2867 struct socket *sockb)
2869 struct socket_smack *asp = socka->sk->sk_security;
2870 struct socket_smack *bsp = sockb->sk->sk_security;
2872 asp->smk_packet = bsp->smk_out;
2873 bsp->smk_packet = asp->smk_out;
2878 #ifdef SMACK_IPV6_PORT_LABELING
2880 * smack_socket_bind - record port binding information.
2882 * @address: the port address
2883 * @addrlen: size of the address
2885 * Records the label bound to a port.
2889 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2892 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2893 smk_ipv6_port_label(sock, address);
2896 #endif /* SMACK_IPV6_PORT_LABELING */
2899 * smack_socket_connect - connect access check
2901 * @sap: the other end
2902 * @addrlen: size of sap
2904 * Verifies that a connection may be possible
2906 * Returns 0 on success, and error code otherwise
2908 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2912 #if IS_ENABLED(CONFIG_IPV6)
2913 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2915 #ifdef SMACK_IPV6_SECMARK_LABELING
2916 struct smack_known *rsp;
2917 struct socket_smack *ssp;
2920 if (sock->sk == NULL)
2923 #ifdef SMACK_IPV6_SECMARK_LABELING
2924 ssp = sock->sk->sk_security;
2927 switch (sock->sk->sk_family) {
2929 if (addrlen < sizeof(struct sockaddr_in))
2931 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2934 if (addrlen < sizeof(struct sockaddr_in6))
2936 #ifdef SMACK_IPV6_SECMARK_LABELING
2937 rsp = smack_ipv6host_label(sip);
2939 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2942 #ifdef SMACK_IPV6_PORT_LABELING
2943 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2951 * smack_flags_to_may - convert S_ to MAY_ values
2952 * @flags: the S_ value
2954 * Returns the equivalent MAY_ value
2956 static int smack_flags_to_may(int flags)
2960 if (flags & S_IRUGO)
2962 if (flags & S_IWUGO)
2964 if (flags & S_IXUGO)
2971 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2976 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2978 struct smack_known *skp = smk_of_current();
2980 msg->security = skp;
2985 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2988 * Clears the blob pointer
2990 static void smack_msg_msg_free_security(struct msg_msg *msg)
2992 msg->security = NULL;
2996 * smack_of_ipc - the smack pointer for the ipc
2999 * Returns a pointer to the smack value
3001 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
3003 return (struct smack_known *)isp->security;
3007 * smack_ipc_alloc_security - Set the security blob for ipc
3012 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
3014 struct smack_known *skp = smk_of_current();
3016 isp->security = skp;
3021 * smack_ipc_free_security - Clear the security blob for ipc
3024 * Clears the blob pointer
3026 static void smack_ipc_free_security(struct kern_ipc_perm *isp)
3028 isp->security = NULL;
3032 * smk_curacc_shm : check if current has access on shm
3034 * @access : access requested
3036 * Returns 0 if current has the requested access, error code otherwise
3038 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
3040 struct smack_known *ssp = smack_of_ipc(isp);
3041 struct smk_audit_info ad;
3045 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3046 ad.a.u.ipc_id = isp->id;
3048 rc = smk_curacc(ssp, access, &ad);
3049 rc = smk_bu_current("shm", ssp, access, rc);
3054 * smack_shm_associate - Smack access check for shm
3056 * @shmflg: access requested
3058 * Returns 0 if current has the requested access, error code otherwise
3060 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3064 may = smack_flags_to_may(shmflg);
3065 return smk_curacc_shm(isp, may);
3069 * smack_shm_shmctl - Smack access check for shm
3071 * @cmd: what it wants to do
3073 * Returns 0 if current has the requested access, error code otherwise
3075 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3089 may = MAY_READWRITE;
3094 * System level information.
3100 return smk_curacc_shm(isp, may);
3104 * smack_shm_shmat - Smack access for shmat
3107 * @shmflg: access requested
3109 * Returns 0 if current has the requested access, error code otherwise
3111 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3116 may = smack_flags_to_may(shmflg);
3117 return smk_curacc_shm(ipc, may);
3121 * smk_curacc_sem : check if current has access on sem
3123 * @access : access requested
3125 * Returns 0 if current has the requested access, error code otherwise
3127 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3129 struct smack_known *ssp = smack_of_ipc(isp);
3130 struct smk_audit_info ad;
3134 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3135 ad.a.u.ipc_id = isp->id;
3137 rc = smk_curacc(ssp, access, &ad);
3138 rc = smk_bu_current("sem", ssp, access, rc);
3143 * smack_sem_associate - Smack access check for sem
3145 * @semflg: access requested
3147 * Returns 0 if current has the requested access, error code otherwise
3149 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3153 may = smack_flags_to_may(semflg);
3154 return smk_curacc_sem(isp, may);
3158 * smack_sem_shmctl - Smack access check for sem
3160 * @cmd: what it wants to do
3162 * Returns 0 if current has the requested access, error code otherwise
3164 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3183 may = MAY_READWRITE;
3188 * System level information
3195 return smk_curacc_sem(isp, may);
3199 * smack_sem_semop - Smack checks of semaphore operations
3205 * Treated as read and write in all cases.
3207 * Returns 0 if access is allowed, error code otherwise
3209 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3210 unsigned nsops, int alter)
3212 return smk_curacc_sem(isp, MAY_READWRITE);
3216 * smk_curacc_msq : helper to check if current has access on msq
3218 * @access : access requested
3220 * return 0 if current has access, error otherwise
3222 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3224 struct smack_known *msp = smack_of_ipc(isp);
3225 struct smk_audit_info ad;
3229 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3230 ad.a.u.ipc_id = isp->id;
3232 rc = smk_curacc(msp, access, &ad);
3233 rc = smk_bu_current("msq", msp, access, rc);
3238 * smack_msg_queue_associate - Smack access check for msg_queue
3240 * @msqflg: access requested
3242 * Returns 0 if current has the requested access, error code otherwise
3244 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3248 may = smack_flags_to_may(msqflg);
3249 return smk_curacc_msq(isp, may);
3253 * smack_msg_queue_msgctl - Smack access check for msg_queue
3255 * @cmd: what it wants to do
3257 * Returns 0 if current has the requested access, error code otherwise
3259 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3271 may = MAY_READWRITE;
3276 * System level information
3283 return smk_curacc_msq(isp, may);
3287 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3290 * @msqflg: access requested
3292 * Returns 0 if current has the requested access, error code otherwise
3294 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3299 may = smack_flags_to_may(msqflg);
3300 return smk_curacc_msq(isp, may);
3304 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3311 * Returns 0 if current has read and write access, error code otherwise
3313 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3314 struct task_struct *target, long type, int mode)
3316 return smk_curacc_msq(isp, MAY_READWRITE);
3320 * smack_ipc_permission - Smack access for ipc_permission()
3321 * @ipp: the object permissions
3322 * @flag: access requested
3324 * Returns 0 if current has read and write access, error code otherwise
3326 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3328 struct smack_known *iskp = ipp->security;
3329 int may = smack_flags_to_may(flag);
3330 struct smk_audit_info ad;
3334 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3335 ad.a.u.ipc_id = ipp->id;
3337 rc = smk_curacc(iskp, may, &ad);
3338 rc = smk_bu_current("svipc", iskp, may, rc);
3343 * smack_ipc_getsecid - Extract smack security id
3344 * @ipp: the object permissions
3345 * @secid: where result will be saved
3347 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3349 struct smack_known *iskp = ipp->security;
3351 *secid = iskp->smk_secid;
3355 * smack_d_instantiate - Make sure the blob is correct on an inode
3356 * @opt_dentry: dentry where inode will be attached
3357 * @inode: the object
3359 * Set the inode's security blob if it hasn't been done already.
3361 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3363 struct super_block *sbp;
3364 struct superblock_smack *sbsp;
3365 struct inode_smack *isp;
3366 struct smack_known *skp;
3367 struct smack_known *ckp = smk_of_current();
3368 struct smack_known *final;
3369 char trattr[TRANS_TRUE_SIZE];
3377 isp = inode->i_security;
3379 mutex_lock(&isp->smk_lock);
3381 * If the inode is already instantiated
3382 * take the quick way out
3384 if (isp->smk_flags & SMK_INODE_INSTANT)
3388 sbsp = sbp->s_security;
3390 * We're going to use the superblock default label
3391 * if there's no label on the file.
3393 final = sbsp->smk_default;
3396 * If this is the root inode the superblock
3397 * may be in the process of initialization.
3398 * If that is the case use the root value out
3399 * of the superblock.
3401 if (opt_dentry->d_parent == opt_dentry) {
3402 switch (sbp->s_magic) {
3403 case CGROUP_SUPER_MAGIC:
3404 case CGROUP2_SUPER_MAGIC:
3406 * The cgroup filesystem is never mounted,
3407 * so there's no opportunity to set the mount
3410 sbsp->smk_root = &smack_known_star;
3411 sbsp->smk_default = &smack_known_star;
3412 isp->smk_inode = sbsp->smk_root;
3416 * What about shmem/tmpfs anonymous files with dentry
3417 * obtained from d_alloc_pseudo()?
3419 isp->smk_inode = smk_of_current();
3422 isp->smk_inode = smk_of_current();
3426 * Socket access is controlled by the socket
3427 * structures associated with the task involved.
3429 isp->smk_inode = &smack_known_star;
3432 isp->smk_inode = sbsp->smk_root;
3435 isp->smk_flags |= SMK_INODE_INSTANT;
3440 * This is pretty hackish.
3441 * Casey says that we shouldn't have to do
3442 * file system specific code, but it does help
3443 * with keeping it simple.
3445 switch (sbp->s_magic) {
3447 case CGROUP_SUPER_MAGIC:
3448 case CGROUP2_SUPER_MAGIC:
3450 * Casey says that it's a little embarrassing
3451 * that the smack file system doesn't do
3452 * extended attributes.
3454 * Cgroupfs is special
3456 final = &smack_known_star;
3458 case DEVPTS_SUPER_MAGIC:
3460 * devpts seems content with the label of the task.
3461 * Programs that change smack have to treat the
3466 case PROC_SUPER_MAGIC:
3468 * Casey says procfs appears not to care.
3469 * The superblock default suffices.
3474 * Device labels should come from the filesystem,
3475 * but watch out, because they're volitile,
3476 * getting recreated on every reboot.
3478 final = &smack_known_star;
3482 * If a smack value has been set we want to use it,
3483 * but since tmpfs isn't giving us the opportunity
3484 * to set mount options simulate setting the
3485 * superblock default.
3489 * This isn't an understood special case.
3490 * Get the value from the xattr.
3494 * UNIX domain sockets use lower level socket data.
3496 if (S_ISSOCK(inode->i_mode)) {
3497 final = &smack_known_star;
3501 * No xattr support means, alas, no SMACK label.
3502 * Use the aforeapplied default.
3503 * It would be curious if the label of the task
3504 * does not match that assigned.
3506 if (!(inode->i_opflags & IOP_XATTR))
3509 * Get the dentry for xattr.
3511 dp = dget(opt_dentry);
3512 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3513 if (!IS_ERR_OR_NULL(skp))
3517 * Transmuting directory
3519 if (S_ISDIR(inode->i_mode)) {
3521 * If this is a new directory and the label was
3522 * transmuted when the inode was initialized
3523 * set the transmute attribute on the directory
3524 * and mark the inode.
3526 * If there is a transmute attribute on the
3527 * directory mark the inode.
3529 if (isp->smk_flags & SMK_INODE_CHANGED) {
3530 isp->smk_flags &= ~SMK_INODE_CHANGED;
3531 rc = __vfs_setxattr(dp, inode,
3532 XATTR_NAME_SMACKTRANSMUTE,
3533 TRANS_TRUE, TRANS_TRUE_SIZE,
3536 rc = __vfs_getxattr(dp, inode,
3537 XATTR_NAME_SMACKTRANSMUTE, trattr,
3539 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3540 TRANS_TRUE_SIZE) != 0)
3544 transflag = SMK_INODE_TRANSMUTE;
3547 * Don't let the exec or mmap label be "*" or "@".
3549 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3550 if (IS_ERR(skp) || skp == &smack_known_star ||
3551 skp == &smack_known_web)
3553 isp->smk_task = skp;
3555 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3556 if (IS_ERR(skp) || skp == &smack_known_star ||
3557 skp == &smack_known_web)
3559 isp->smk_mmap = skp;
3566 isp->smk_inode = ckp;
3568 isp->smk_inode = final;
3570 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3573 mutex_unlock(&isp->smk_lock);
3578 * smack_getprocattr - Smack process attribute access
3579 * @p: the object task
3580 * @name: the name of the attribute in /proc/.../attr
3581 * @value: where to put the result
3583 * Places a copy of the task Smack into value
3585 * Returns the length of the smack label or an error code
3587 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3589 struct smack_known *skp = smk_of_task_struct(p);
3593 if (strcmp(name, "current") != 0)
3596 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3606 * smack_setprocattr - Smack process attribute setting
3607 * @name: the name of the attribute in /proc/.../attr
3608 * @value: the value to set
3609 * @size: the size of the value
3611 * Sets the Smack value of the task. Only setting self
3612 * is permitted and only with privilege
3614 * Returns the length of the smack label or an error code
3616 static int smack_setprocattr(const char *name, void *value, size_t size)
3618 struct task_smack *tsp = current_security();
3620 struct smack_known *skp;
3621 struct smack_known_list_elem *sklep;
3624 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3627 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3630 if (strcmp(name, "current") != 0)
3633 skp = smk_import_entry(value, size);
3635 return PTR_ERR(skp);
3638 * No process is ever allowed the web ("@") label
3639 * and the star ("*") label.
3641 if (skp == &smack_known_web || skp == &smack_known_star)
3644 if (!smack_privileged(CAP_MAC_ADMIN)) {
3646 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3647 if (sklep->smk_label == skp) {
3655 new = prepare_creds();
3659 tsp = new->security;
3660 tsp->smk_task = skp;
3662 * process can change its label only once
3664 smk_destroy_label_list(&tsp->smk_relabel);
3671 * smack_unix_stream_connect - Smack access on UDS
3673 * @other: the other sock
3676 * Return 0 if a subject with the smack of sock could access
3677 * an object with the smack of other, otherwise an error code
3679 static int smack_unix_stream_connect(struct sock *sock,
3680 struct sock *other, struct sock *newsk)
3682 struct smack_known *skp;
3683 struct smack_known *okp;
3684 struct socket_smack *ssp = sock->sk_security;
3685 struct socket_smack *osp = other->sk_security;
3686 struct socket_smack *nsp = newsk->sk_security;
3687 struct smk_audit_info ad;
3690 struct lsm_network_audit net;
3693 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3697 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3698 smk_ad_setfield_u_net_sk(&ad, other);
3700 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3701 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3705 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3706 rc = smk_bu_note("UDS connect", okp, skp,
3712 * Cross reference the peer labels for SO_PEERSEC.
3715 nsp->smk_packet = ssp->smk_out;
3716 ssp->smk_packet = osp->smk_out;
3723 * smack_unix_may_send - Smack access on UDS
3725 * @other: the other socket
3727 * Return 0 if a subject with the smack of sock could access
3728 * an object with the smack of other, otherwise an error code
3730 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3732 struct socket_smack *ssp = sock->sk->sk_security;
3733 struct socket_smack *osp = other->sk->sk_security;
3734 struct smk_audit_info ad;
3738 struct lsm_network_audit net;
3740 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3741 smk_ad_setfield_u_net_sk(&ad, other->sk);
3744 if (smack_privileged(CAP_MAC_OVERRIDE))
3747 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3748 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3753 * smack_socket_sendmsg - Smack check based on destination host
3756 * @size: the size of the message
3758 * Return 0 if the current subject can write to the destination host.
3759 * For IPv4 this is only a question if the destination is a single label host.
3760 * For IPv6 this is a check against the label of the port.
3762 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3765 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3766 #if IS_ENABLED(CONFIG_IPV6)
3767 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3769 #ifdef SMACK_IPV6_SECMARK_LABELING
3770 struct socket_smack *ssp = sock->sk->sk_security;
3771 struct smack_known *rsp;
3776 * Perfectly reasonable for this to be NULL
3781 switch (sock->sk->sk_family) {
3783 rc = smack_netlabel_send(sock->sk, sip);
3786 #ifdef SMACK_IPV6_SECMARK_LABELING
3787 rsp = smack_ipv6host_label(sap);
3789 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3792 #ifdef SMACK_IPV6_PORT_LABELING
3793 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3801 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3802 * @sap: netlabel secattr
3803 * @ssp: socket security information
3805 * Returns a pointer to a Smack label entry found on the label list.
3807 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3808 struct socket_smack *ssp)
3810 struct smack_known *skp;
3815 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3817 * Looks like a CIPSO packet.
3818 * If there are flags but no level netlabel isn't
3819 * behaving the way we expect it to.
3821 * Look it up in the label table
3822 * Without guidance regarding the smack value
3823 * for the packet fall back on the network
3827 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3828 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3831 * Compare the catsets. Use the netlbl APIs.
3833 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3834 if ((skp->smk_netlabel.flags &
3835 NETLBL_SECATTR_MLS_CAT) == 0)
3839 for (acat = -1, kcat = -1; acat == kcat; ) {
3840 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3842 kcat = netlbl_catmap_walk(
3843 skp->smk_netlabel.attr.mls.cat,
3845 if (acat < 0 || kcat < 0)
3858 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3859 return &smack_known_web;
3860 return &smack_known_star;
3862 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3864 * Looks like a fallback, which gives us a secid.
3866 return smack_from_secid(sap->attr.secid);
3868 * Without guidance regarding the smack value
3869 * for the packet fall back on the network
3872 return smack_net_ambient;
3875 #if IS_ENABLED(CONFIG_IPV6)
3876 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3880 int proto = -EINVAL;
3881 struct ipv6hdr _ipv6h;
3882 struct ipv6hdr *ip6;
3884 struct tcphdr _tcph, *th;
3885 struct udphdr _udph, *uh;
3886 struct dccp_hdr _dccph, *dh;
3890 offset = skb_network_offset(skb);
3891 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3894 sip->sin6_addr = ip6->saddr;
3896 nexthdr = ip6->nexthdr;
3897 offset += sizeof(_ipv6h);
3898 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3905 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3907 sip->sin6_port = th->source;
3910 case IPPROTO_UDPLITE:
3911 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3913 sip->sin6_port = uh->source;
3916 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3918 sip->sin6_port = dh->dccph_sport;
3923 #endif /* CONFIG_IPV6 */
3926 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3930 * Returns 0 if the packet should be delivered, an error code otherwise
3932 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3934 struct netlbl_lsm_secattr secattr;
3935 struct socket_smack *ssp = sk->sk_security;
3936 struct smack_known *skp = NULL;
3938 struct smk_audit_info ad;
3939 u16 family = sk->sk_family;
3941 struct lsm_network_audit net;
3943 #if IS_ENABLED(CONFIG_IPV6)
3944 struct sockaddr_in6 sadd;
3947 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3949 #endif /* CONFIG_IPV6 */
3953 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3955 * If there is a secmark use it rather than the CIPSO label.
3956 * If there is no secmark fall back to CIPSO.
3957 * The secmark is assumed to reflect policy better.
3959 if (skb && skb->secmark != 0) {
3960 skp = smack_from_secid(skb->secmark);
3963 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3965 * Translate what netlabel gave us.
3967 netlbl_secattr_init(&secattr);
3969 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3971 skp = smack_from_secattr(&secattr, ssp);
3973 skp = smack_net_ambient;
3975 netlbl_secattr_destroy(&secattr);
3977 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3981 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3982 ad.a.u.net->family = family;
3983 ad.a.u.net->netif = skb->skb_iif;
3984 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3987 * Receiving a packet requires that the other end
3988 * be able to write here. Read access is not required.
3989 * This is the simplist possible security model
3992 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3993 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3996 netlbl_skbuff_err(skb, family, rc, 0);
3998 #if IS_ENABLED(CONFIG_IPV6)
4000 proto = smk_skb_to_addr_ipv6(skb, &sadd);
4001 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
4002 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
4004 #ifdef SMACK_IPV6_SECMARK_LABELING
4005 if (skb && skb->secmark != 0)
4006 skp = smack_from_secid(skb->secmark);
4008 skp = smack_ipv6host_label(&sadd);
4010 skp = smack_net_ambient;
4012 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4013 ad.a.u.net->family = family;
4014 ad.a.u.net->netif = skb->skb_iif;
4015 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4016 #endif /* CONFIG_AUDIT */
4017 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4018 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4020 #endif /* SMACK_IPV6_SECMARK_LABELING */
4021 #ifdef SMACK_IPV6_PORT_LABELING
4022 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
4023 #endif /* SMACK_IPV6_PORT_LABELING */
4025 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4026 ICMPV6_ADM_PROHIBITED, 0);
4028 #endif /* CONFIG_IPV6 */
4035 * smack_socket_getpeersec_stream - pull in packet label
4037 * @optval: user's destination
4038 * @optlen: size thereof
4041 * returns zero on success, an error code otherwise
4043 static int smack_socket_getpeersec_stream(struct socket *sock,
4044 char __user *optval,
4045 int __user *optlen, unsigned len)
4047 struct socket_smack *ssp;
4052 ssp = sock->sk->sk_security;
4053 if (ssp->smk_packet != NULL) {
4054 rcp = ssp->smk_packet->smk_known;
4055 slen = strlen(rcp) + 1;
4060 else if (copy_to_user(optval, rcp, slen) != 0)
4063 if (put_user(slen, optlen) != 0)
4071 * smack_socket_getpeersec_dgram - pull in packet label
4072 * @sock: the peer socket
4074 * @secid: pointer to where to put the secid of the packet
4076 * Sets the netlabel socket state on sk from parent
4078 static int smack_socket_getpeersec_dgram(struct socket *sock,
4079 struct sk_buff *skb, u32 *secid)
4082 struct netlbl_lsm_secattr secattr;
4083 struct socket_smack *ssp = NULL;
4084 struct smack_known *skp;
4085 int family = PF_UNSPEC;
4086 u32 s = 0; /* 0 is the invalid secid */
4090 if (skb->protocol == htons(ETH_P_IP))
4092 #if IS_ENABLED(CONFIG_IPV6)
4093 else if (skb->protocol == htons(ETH_P_IPV6))
4095 #endif /* CONFIG_IPV6 */
4097 if (family == PF_UNSPEC && sock != NULL)
4098 family = sock->sk->sk_family;
4102 ssp = sock->sk->sk_security;
4103 s = ssp->smk_out->smk_secid;
4106 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4112 * Translate what netlabel gave us.
4114 if (sock != NULL && sock->sk != NULL)
4115 ssp = sock->sk->sk_security;
4116 netlbl_secattr_init(&secattr);
4117 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4119 skp = smack_from_secattr(&secattr, ssp);
4122 netlbl_secattr_destroy(&secattr);
4125 #ifdef SMACK_IPV6_SECMARK_LABELING
4137 * smack_sock_graft - Initialize a newly created socket with an existing sock
4139 * @parent: parent socket
4141 * Set the smk_{in,out} state of an existing sock based on the process that
4142 * is creating the new socket.
4144 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4146 struct socket_smack *ssp;
4147 struct smack_known *skp = smk_of_current();
4150 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4153 ssp = sk->sk_security;
4156 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4160 * smack_inet_conn_request - Smack access check on connect
4161 * @sk: socket involved
4165 * Returns 0 if a task with the packet label could write to
4166 * the socket, otherwise an error code
4168 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4169 struct request_sock *req)
4171 u16 family = sk->sk_family;
4172 struct smack_known *skp;
4173 struct socket_smack *ssp = sk->sk_security;
4174 struct netlbl_lsm_secattr secattr;
4175 struct sockaddr_in addr;
4177 struct smack_known *hskp;
4179 struct smk_audit_info ad;
4181 struct lsm_network_audit net;
4184 #if IS_ENABLED(CONFIG_IPV6)
4185 if (family == PF_INET6) {
4187 * Handle mapped IPv4 packets arriving
4188 * via IPv6 sockets. Don't set up netlabel
4189 * processing on IPv6.
4191 if (skb->protocol == htons(ETH_P_IP))
4196 #endif /* CONFIG_IPV6 */
4198 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4200 * If there is a secmark use it rather than the CIPSO label.
4201 * If there is no secmark fall back to CIPSO.
4202 * The secmark is assumed to reflect policy better.
4204 if (skb && skb->secmark != 0) {
4205 skp = smack_from_secid(skb->secmark);
4208 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4210 netlbl_secattr_init(&secattr);
4211 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4213 skp = smack_from_secattr(&secattr, ssp);
4215 skp = &smack_known_huh;
4216 netlbl_secattr_destroy(&secattr);
4218 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4223 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4224 ad.a.u.net->family = family;
4225 ad.a.u.net->netif = skb->skb_iif;
4226 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4229 * Receiving a packet requires that the other end be able to write
4230 * here. Read access is not required.
4232 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4233 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4238 * Save the peer's label in the request_sock so we can later setup
4239 * smk_packet in the child socket so that SO_PEERCRED can report it.
4241 req->peer_secid = skp->smk_secid;
4244 * We need to decide if we want to label the incoming connection here
4245 * if we do we only need to label the request_sock and the stack will
4246 * propagate the wire-label to the sock when it is created.
4249 addr.sin_addr.s_addr = hdr->saddr;
4251 hskp = smack_ipv4host_label(&addr);
4255 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4257 netlbl_req_delattr(req);
4263 * smack_inet_csk_clone - Copy the connection information to the new socket
4264 * @sk: the new socket
4265 * @req: the connection's request_sock
4267 * Transfer the connection's peer label to the newly created socket.
4269 static void smack_inet_csk_clone(struct sock *sk,
4270 const struct request_sock *req)
4272 struct socket_smack *ssp = sk->sk_security;
4273 struct smack_known *skp;
4275 if (req->peer_secid != 0) {
4276 skp = smack_from_secid(req->peer_secid);
4277 ssp->smk_packet = skp;
4279 ssp->smk_packet = NULL;
4283 * Key management security hooks
4285 * Casey has not tested key support very heavily.
4286 * The permission check is most likely too restrictive.
4287 * If you care about keys please have a look.
4292 * smack_key_alloc - Set the key security blob
4294 * @cred: the credentials to use
4297 * No allocation required
4301 static int smack_key_alloc(struct key *key, const struct cred *cred,
4302 unsigned long flags)
4304 struct smack_known *skp = smk_of_task(cred->security);
4306 key->security = skp;
4311 * smack_key_free - Clear the key security blob
4314 * Clear the blob pointer
4316 static void smack_key_free(struct key *key)
4318 key->security = NULL;
4322 * smack_key_permission - Smack access on a key
4323 * @key_ref: gets to the object
4324 * @cred: the credentials to use
4325 * @perm: requested key permissions
4327 * Return 0 if the task has read and write to the object,
4328 * an error code otherwise
4330 static int smack_key_permission(key_ref_t key_ref,
4331 const struct cred *cred, unsigned perm)
4334 struct smk_audit_info ad;
4335 struct smack_known *tkp = smk_of_task(cred->security);
4340 * Validate requested permissions
4342 if (perm & ~KEY_NEED_ALL)
4345 keyp = key_ref_to_ptr(key_ref);
4349 * If the key hasn't been initialized give it access so that
4352 if (keyp->security == NULL)
4355 * This should not occur
4360 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4364 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4365 ad.a.u.key_struct.key = keyp->serial;
4366 ad.a.u.key_struct.key_desc = keyp->description;
4368 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4369 request |= MAY_READ;
4370 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4371 request |= MAY_WRITE;
4372 rc = smk_access(tkp, keyp->security, request, &ad);
4373 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4378 * smack_key_getsecurity - Smack label tagging the key
4379 * @key points to the key to be queried
4380 * @_buffer points to a pointer that should be set to point to the
4381 * resulting string (if no label or an error occurs).
4382 * Return the length of the string (including terminating NUL) or -ve if
4384 * May also return 0 (and a NULL buffer pointer) if there is no label.
4386 static int smack_key_getsecurity(struct key *key, char **_buffer)
4388 struct smack_known *skp = key->security;
4392 if (key->security == NULL) {
4397 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4400 length = strlen(copy) + 1;
4406 #endif /* CONFIG_KEYS */
4411 * Audit requires a unique representation of each Smack specific
4412 * rule. This unique representation is used to distinguish the
4413 * object to be audited from remaining kernel objects and also
4414 * works as a glue between the audit hooks.
4416 * Since repository entries are added but never deleted, we'll use
4417 * the smack_known label address related to the given audit rule as
4418 * the needed unique representation. This also better fits the smack
4419 * model where nearly everything is a label.
4424 * smack_audit_rule_init - Initialize a smack audit rule
4425 * @field: audit rule fields given from user-space (audit.h)
4426 * @op: required testing operator (=, !=, >, <, ...)
4427 * @rulestr: smack label to be audited
4428 * @vrule: pointer to save our own audit rule representation
4430 * Prepare to audit cases where (@field @op @rulestr) is true.
4431 * The label to be audited is created if necessay.
4433 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4435 struct smack_known *skp;
4436 char **rule = (char **)vrule;
4439 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4442 if (op != Audit_equal && op != Audit_not_equal)
4445 skp = smk_import_entry(rulestr, 0);
4447 return PTR_ERR(skp);
4449 *rule = skp->smk_known;
4455 * smack_audit_rule_known - Distinguish Smack audit rules
4456 * @krule: rule of interest, in Audit kernel representation format
4458 * This is used to filter Smack rules from remaining Audit ones.
4459 * If it's proved that this rule belongs to us, the
4460 * audit_rule_match hook will be called to do the final judgement.
4462 static int smack_audit_rule_known(struct audit_krule *krule)
4464 struct audit_field *f;
4467 for (i = 0; i < krule->field_count; i++) {
4468 f = &krule->fields[i];
4470 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4478 * smack_audit_rule_match - Audit given object ?
4479 * @secid: security id for identifying the object to test
4480 * @field: audit rule flags given from user-space
4481 * @op: required testing operator
4482 * @vrule: smack internal rule presentation
4483 * @actx: audit context associated with the check
4485 * The core Audit hook. It's used to take the decision of
4486 * whether to audit or not to audit a given object.
4488 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4489 struct audit_context *actx)
4491 struct smack_known *skp;
4494 if (unlikely(!rule)) {
4495 WARN_ONCE(1, "Smack: missing rule\n");
4499 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4502 skp = smack_from_secid(secid);
4505 * No need to do string comparisons. If a match occurs,
4506 * both pointers will point to the same smack_known
4509 if (op == Audit_equal)
4510 return (rule == skp->smk_known);
4511 if (op == Audit_not_equal)
4512 return (rule != skp->smk_known);
4518 * There is no need for a smack_audit_rule_free hook.
4519 * No memory was allocated.
4522 #endif /* CONFIG_AUDIT */
4525 * smack_ismaclabel - check if xattr @name references a smack MAC label
4526 * @name: Full xattr name to check.
4528 static int smack_ismaclabel(const char *name)
4530 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4535 * smack_secid_to_secctx - return the smack label for a secid
4536 * @secid: incoming integer
4537 * @secdata: destination
4538 * @seclen: how long it is
4540 * Exists for networking code.
4542 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4544 struct smack_known *skp = smack_from_secid(secid);
4547 *secdata = skp->smk_known;
4548 *seclen = strlen(skp->smk_known);
4553 * smack_secctx_to_secid - return the secid for a smack label
4554 * @secdata: smack label
4555 * @seclen: how long result is
4556 * @secid: outgoing integer
4558 * Exists for audit and networking code.
4560 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4562 struct smack_known *skp = smk_find_entry(secdata);
4565 *secid = skp->smk_secid;
4572 * There used to be a smack_release_secctx hook
4573 * that did nothing back when hooks were in a vector.
4574 * Now that there's a list such a hook adds cost.
4577 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4579 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4582 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4584 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4587 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4589 struct smack_known *skp = smk_of_inode(inode);
4591 *ctx = skp->smk_known;
4592 *ctxlen = strlen(skp->smk_known);
4596 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4599 struct task_smack *tsp;
4600 struct smack_known *skp;
4601 struct inode_smack *isp;
4602 struct cred *new_creds = *new;
4604 if (new_creds == NULL) {
4605 new_creds = prepare_creds();
4606 if (new_creds == NULL)
4610 tsp = new_creds->security;
4613 * Get label from overlay inode and set it in create_sid
4615 isp = d_inode(dentry->d_parent)->i_security;
4616 skp = isp->smk_inode;
4617 tsp->smk_task = skp;
4622 static int smack_inode_copy_up_xattr(const char *name)
4625 * Return 1 if this is the smack access Smack attribute.
4627 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4633 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4635 const struct cred *old,
4638 struct task_smack *otsp = old->security;
4639 struct task_smack *ntsp = new->security;
4640 struct inode_smack *isp;
4644 * Use the process credential unless all of
4645 * the transmuting criteria are met
4647 ntsp->smk_task = otsp->smk_task;
4650 * the attribute of the containing directory
4652 isp = d_inode(dentry->d_parent)->i_security;
4654 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4656 may = smk_access_entry(otsp->smk_task->smk_known,
4657 isp->smk_inode->smk_known,
4658 &otsp->smk_task->smk_rules);
4662 * If the directory is transmuting and the rule
4663 * providing access is transmuting use the containing
4664 * directory label instead of the process label.
4666 if (may > 0 && (may & MAY_TRANSMUTE))
4667 ntsp->smk_task = isp->smk_inode;
4672 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4673 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4674 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4675 LSM_HOOK_INIT(syslog, smack_syslog),
4677 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4678 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4680 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4681 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4682 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4683 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4684 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4685 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4687 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4689 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4690 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4691 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4692 LSM_HOOK_INIT(inode_link, smack_inode_link),
4693 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4694 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4695 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4696 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4697 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4698 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4699 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4700 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4701 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4702 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4703 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4704 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4705 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4706 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4708 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4709 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4710 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4711 LSM_HOOK_INIT(file_lock, smack_file_lock),
4712 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4713 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4714 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4715 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4716 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4717 LSM_HOOK_INIT(file_receive, smack_file_receive),
4719 LSM_HOOK_INIT(file_open, smack_file_open),
4721 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4722 LSM_HOOK_INIT(cred_free, smack_cred_free),
4723 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4724 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4725 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4726 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4727 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4728 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4729 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4730 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4731 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4732 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4733 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4734 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4735 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4736 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4737 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4738 LSM_HOOK_INIT(task_kill, smack_task_kill),
4739 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4741 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4742 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4744 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4745 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4747 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4748 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
4749 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4750 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4751 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4752 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4754 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4755 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
4756 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4757 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4758 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4760 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4761 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
4762 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4763 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4764 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4766 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4768 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4769 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4771 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4772 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4774 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4775 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4776 #ifdef SMACK_IPV6_PORT_LABELING
4777 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4779 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4780 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4781 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4782 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4783 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4784 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4785 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4786 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4787 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4788 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4790 /* key management security hooks */
4792 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4793 LSM_HOOK_INIT(key_free, smack_key_free),
4794 LSM_HOOK_INIT(key_permission, smack_key_permission),
4795 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4796 #endif /* CONFIG_KEYS */
4800 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4801 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4802 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4803 #endif /* CONFIG_AUDIT */
4805 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4806 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4807 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4808 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4809 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4810 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4811 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4812 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4813 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4817 static __init void init_smack_known_list(void)
4820 * Initialize rule list locks
4822 mutex_init(&smack_known_huh.smk_rules_lock);
4823 mutex_init(&smack_known_hat.smk_rules_lock);
4824 mutex_init(&smack_known_floor.smk_rules_lock);
4825 mutex_init(&smack_known_star.smk_rules_lock);
4826 mutex_init(&smack_known_web.smk_rules_lock);
4828 * Initialize rule lists
4830 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4831 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4832 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4833 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4834 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4836 * Create the known labels list
4838 smk_insert_entry(&smack_known_huh);
4839 smk_insert_entry(&smack_known_hat);
4840 smk_insert_entry(&smack_known_star);
4841 smk_insert_entry(&smack_known_floor);
4842 smk_insert_entry(&smack_known_web);
4846 * smack_init - initialize the smack system
4850 static __init int smack_init(void)
4853 struct task_smack *tsp;
4855 if (!security_module_enable("smack"))
4858 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4859 if (!smack_inode_cache)
4862 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4865 kmem_cache_destroy(smack_inode_cache);
4871 pr_info("Smack: Initializing.\n");
4872 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4873 pr_info("Smack: Netfilter enabled.\n");
4875 #ifdef SMACK_IPV6_PORT_LABELING
4876 pr_info("Smack: IPv6 port labeling enabled.\n");
4878 #ifdef SMACK_IPV6_SECMARK_LABELING
4879 pr_info("Smack: IPv6 Netfilter enabled.\n");
4883 * Set the security state for the initial task.
4885 cred = (struct cred *) current->cred;
4886 cred->security = tsp;
4888 /* initialize the smack_known_list */
4889 init_smack_known_list();
4894 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4900 * Smack requires early initialization in order to label
4901 * all processes and objects when they are created.
4903 DEFINE_LSM(smack) = {