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;
62 struct kmem_cache *smack_rule_cache;
65 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
70 } smk_mount_opts[] = {
71 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
75 static int match_opt_prefix(char *s, int l, char **arg)
79 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
80 size_t len = smk_mount_opts[i].len;
81 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
83 if (len == l || s[len] != '=')
86 return smk_mount_opts[i].opt;
91 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
92 static char *smk_bu_mess[] = {
93 "Bringup Error", /* Unused */
94 "Bringup", /* SMACK_BRINGUP_ALLOW */
95 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
96 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
99 static void smk_bu_mode(int mode, char *s)
105 if (mode & MAY_WRITE)
109 if (mode & MAY_APPEND)
111 if (mode & MAY_TRANSMUTE)
121 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
122 static int smk_bu_note(char *note, struct smack_known *sskp,
123 struct smack_known *oskp, int mode, int rc)
125 char acc[SMK_NUM_ACCESS_TYPE + 1];
129 if (rc > SMACK_UNCONFINED_OBJECT)
132 smk_bu_mode(mode, acc);
133 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
134 sskp->smk_known, oskp->smk_known, acc, note);
138 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
141 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
142 static int smk_bu_current(char *note, struct smack_known *oskp,
145 struct task_smack *tsp = smack_cred(current_cred());
146 char acc[SMK_NUM_ACCESS_TYPE + 1];
150 if (rc > SMACK_UNCONFINED_OBJECT)
153 smk_bu_mode(mode, acc);
154 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
155 tsp->smk_task->smk_known, oskp->smk_known,
156 acc, current->comm, note);
160 #define smk_bu_current(note, oskp, mode, RC) (RC)
163 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
164 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
166 struct task_smack *tsp = smack_cred(current_cred());
167 struct smack_known *smk_task = smk_of_task_struct(otp);
168 char acc[SMK_NUM_ACCESS_TYPE + 1];
172 if (rc > SMACK_UNCONFINED_OBJECT)
175 smk_bu_mode(mode, acc);
176 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
177 tsp->smk_task->smk_known, smk_task->smk_known, acc,
178 current->comm, otp->comm);
182 #define smk_bu_task(otp, mode, RC) (RC)
185 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
186 static int smk_bu_inode(struct inode *inode, int mode, int rc)
188 struct task_smack *tsp = smack_cred(current_cred());
189 struct inode_smack *isp = smack_inode(inode);
190 char acc[SMK_NUM_ACCESS_TYPE + 1];
192 if (isp->smk_flags & SMK_INODE_IMPURE)
193 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
194 inode->i_sb->s_id, inode->i_ino, current->comm);
198 if (rc > SMACK_UNCONFINED_OBJECT)
200 if (rc == SMACK_UNCONFINED_SUBJECT &&
201 (mode & (MAY_WRITE | MAY_APPEND)))
202 isp->smk_flags |= SMK_INODE_IMPURE;
204 smk_bu_mode(mode, acc);
206 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
207 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
208 inode->i_sb->s_id, inode->i_ino, current->comm);
212 #define smk_bu_inode(inode, mode, RC) (RC)
215 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
216 static int smk_bu_file(struct file *file, int mode, int rc)
218 struct task_smack *tsp = smack_cred(current_cred());
219 struct smack_known *sskp = tsp->smk_task;
220 struct inode *inode = file_inode(file);
221 struct inode_smack *isp = smack_inode(inode);
222 char acc[SMK_NUM_ACCESS_TYPE + 1];
224 if (isp->smk_flags & SMK_INODE_IMPURE)
225 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
226 inode->i_sb->s_id, inode->i_ino, current->comm);
230 if (rc > SMACK_UNCONFINED_OBJECT)
233 smk_bu_mode(mode, acc);
234 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
235 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
236 inode->i_sb->s_id, inode->i_ino, file,
241 #define smk_bu_file(file, mode, RC) (RC)
244 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
245 static int smk_bu_credfile(const struct cred *cred, struct file *file,
248 struct task_smack *tsp = smack_cred(cred);
249 struct smack_known *sskp = tsp->smk_task;
250 struct inode *inode = file_inode(file);
251 struct inode_smack *isp = smack_inode(inode);
252 char acc[SMK_NUM_ACCESS_TYPE + 1];
254 if (isp->smk_flags & SMK_INODE_IMPURE)
255 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
256 inode->i_sb->s_id, inode->i_ino, current->comm);
260 if (rc > SMACK_UNCONFINED_OBJECT)
263 smk_bu_mode(mode, acc);
264 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
265 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
266 inode->i_sb->s_id, inode->i_ino, file,
271 #define smk_bu_credfile(cred, file, mode, RC) (RC)
275 * smk_fetch - Fetch the smack label from a file.
276 * @name: type of the label (attribute)
277 * @ip: a pointer to the inode
278 * @dp: a pointer to the dentry
280 * Returns a pointer to the master list entry for the Smack label,
281 * NULL if there was no label to fetch, or an error code.
283 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
288 struct smack_known *skp = NULL;
290 if (!(ip->i_opflags & IOP_XATTR))
291 return ERR_PTR(-EOPNOTSUPP);
293 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
295 return ERR_PTR(-ENOMEM);
297 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
303 skp = smk_import_entry(buffer, rc);
311 * init_inode_smack - initialize an inode security blob
312 * @isp: the blob to initialize
313 * @skp: a pointer to the Smack label entry to use in the blob
316 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
318 struct inode_smack *isp = smack_inode(inode);
320 isp->smk_inode = skp;
322 mutex_init(&isp->smk_lock);
326 * init_task_smack - initialize a task security blob
327 * @tsp: blob to initialize
328 * @task: a pointer to the Smack label for the running task
329 * @forked: a pointer to the Smack label for the forked task
332 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
333 struct smack_known *forked)
335 tsp->smk_task = task;
336 tsp->smk_forked = forked;
337 INIT_LIST_HEAD(&tsp->smk_rules);
338 INIT_LIST_HEAD(&tsp->smk_relabel);
339 mutex_init(&tsp->smk_rules_lock);
343 * smk_copy_rules - copy a rule set
344 * @nhead: new rules header pointer
345 * @ohead: old rules header pointer
346 * @gfp: type of the memory for the allocation
348 * Returns 0 on success, -ENOMEM on error
350 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
353 struct smack_rule *nrp;
354 struct smack_rule *orp;
357 list_for_each_entry_rcu(orp, ohead, list) {
358 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
364 list_add_rcu(&nrp->list, nhead);
370 * smk_copy_relabel - copy smk_relabel labels list
371 * @nhead: new rules header pointer
372 * @ohead: old rules header pointer
373 * @gfp: type of the memory for the allocation
375 * Returns 0 on success, -ENOMEM on error
377 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
380 struct smack_known_list_elem *nklep;
381 struct smack_known_list_elem *oklep;
383 list_for_each_entry(oklep, ohead, list) {
384 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
386 smk_destroy_label_list(nhead);
389 nklep->smk_label = oklep->smk_label;
390 list_add(&nklep->list, nhead);
397 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
398 * @mode - input mode in form of PTRACE_MODE_*
400 * Returns a converted MAY_* mode usable by smack rules
402 static inline unsigned int smk_ptrace_mode(unsigned int mode)
404 if (mode & PTRACE_MODE_ATTACH)
405 return MAY_READWRITE;
406 if (mode & PTRACE_MODE_READ)
413 * smk_ptrace_rule_check - helper for ptrace access
414 * @tracer: tracer process
415 * @tracee_known: label entry of the process that's about to be traced
416 * @mode: ptrace attachment mode (PTRACE_MODE_*)
417 * @func: name of the function that called us, used for audit
419 * Returns 0 on access granted, -error on error
421 static int smk_ptrace_rule_check(struct task_struct *tracer,
422 struct smack_known *tracee_known,
423 unsigned int mode, const char *func)
426 struct smk_audit_info ad, *saip = NULL;
427 struct task_smack *tsp;
428 struct smack_known *tracer_known;
429 const struct cred *tracercred;
431 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
432 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
433 smk_ad_setfield_u_tsk(&ad, tracer);
438 tracercred = __task_cred(tracer);
439 tsp = smack_cred(tracercred);
440 tracer_known = smk_of_task(tsp);
442 if ((mode & PTRACE_MODE_ATTACH) &&
443 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
444 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
445 if (tracer_known->smk_known == tracee_known->smk_known)
447 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
449 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
455 smack_log(tracer_known->smk_known,
456 tracee_known->smk_known,
463 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
464 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
472 * We he, that is fun!
476 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
477 * @ctp: child task pointer
478 * @mode: ptrace attachment mode (PTRACE_MODE_*)
480 * Returns 0 if access is OK, an error code otherwise
482 * Do the capability checks.
484 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
486 struct smack_known *skp;
488 skp = smk_of_task_struct(ctp);
490 return smk_ptrace_rule_check(current, skp, mode, __func__);
494 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
495 * @ptp: parent task pointer
497 * Returns 0 if access is OK, an error code otherwise
499 * Do the capability checks, and require PTRACE_MODE_ATTACH.
501 static int smack_ptrace_traceme(struct task_struct *ptp)
504 struct smack_known *skp;
506 skp = smk_of_task(smack_cred(current_cred()));
508 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
513 * smack_syslog - Smack approval on syslog
514 * @type: message type
516 * Returns 0 on success, error code otherwise.
518 static int smack_syslog(int typefrom_file)
521 struct smack_known *skp = smk_of_current();
523 if (smack_privileged(CAP_MAC_OVERRIDE))
526 if (smack_syslog_label != NULL && smack_syslog_label != skp)
537 * smack_sb_alloc_security - allocate a superblock blob
538 * @sb: the superblock getting the blob
540 * Returns 0 on success or -ENOMEM on error.
542 static int smack_sb_alloc_security(struct super_block *sb)
544 struct superblock_smack *sbsp;
546 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
551 sbsp->smk_root = &smack_known_floor;
552 sbsp->smk_default = &smack_known_floor;
553 sbsp->smk_floor = &smack_known_floor;
554 sbsp->smk_hat = &smack_known_hat;
556 * SMK_SB_INITIALIZED will be zero from kzalloc.
558 sb->s_security = sbsp;
564 * smack_sb_free_security - free a superblock blob
565 * @sb: the superblock getting the blob
568 static void smack_sb_free_security(struct super_block *sb)
570 kfree(sb->s_security);
571 sb->s_security = NULL;
574 struct smack_mnt_opts {
575 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
578 static void smack_free_mnt_opts(void *mnt_opts)
580 struct smack_mnt_opts *opts = mnt_opts;
581 kfree(opts->fsdefault);
582 kfree(opts->fsfloor);
585 kfree(opts->fstransmute);
589 static int smack_add_opt(int token, const char *s, void **mnt_opts)
591 struct smack_mnt_opts *opts = *mnt_opts;
594 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
623 case Opt_fstransmute:
624 if (opts->fstransmute)
626 opts->fstransmute = s;
632 pr_warn("Smack: duplicate mount options\n");
637 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
638 * @fc: The new filesystem context.
639 * @src_fc: The source filesystem context being duplicated.
641 * Returns 0 on success or -ENOMEM on error.
643 static int smack_fs_context_dup(struct fs_context *fc,
644 struct fs_context *src_fc)
646 struct smack_mnt_opts *dst, *src = src_fc->security;
651 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
656 if (src->fsdefault) {
657 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
662 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
667 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
672 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
676 if (src->fstransmute) {
677 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
678 if (!dst->fstransmute)
684 static const struct fs_parameter_spec smack_param_specs[] = {
685 fsparam_string("fsdefault", Opt_fsdefault),
686 fsparam_string("fsfloor", Opt_fsfloor),
687 fsparam_string("fshat", Opt_fshat),
688 fsparam_string("fsroot", Opt_fsroot),
689 fsparam_string("fstransmute", Opt_fstransmute),
693 static const struct fs_parameter_description smack_fs_parameters = {
695 .specs = smack_param_specs,
699 * smack_fs_context_parse_param - Parse a single mount parameter
700 * @fc: The new filesystem context being constructed.
701 * @param: The parameter.
703 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
706 static int smack_fs_context_parse_param(struct fs_context *fc,
707 struct fs_parameter *param)
709 struct fs_parse_result result;
712 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
716 rc = smack_add_opt(opt, param->string, &fc->security);
718 param->string = NULL;
722 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
724 char *from = options, *to = options;
728 char *next = strchr(from, ',');
737 token = match_opt_prefix(from, len, &arg);
738 if (token != Opt_error) {
739 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
740 rc = smack_add_opt(token, arg, mnt_opts);
744 smack_free_mnt_opts(*mnt_opts);
749 if (!first) { // copy with preceding comma
754 memmove(to, from, len);
767 * smack_set_mnt_opts - set Smack specific mount options
768 * @sb: the file system superblock
769 * @opts: Smack mount options
770 * @kern_flags: mount option from kernel space or user space
771 * @set_kern_flags: where to store converted mount opts
773 * Returns 0 on success, an error code on failure
775 * Allow filesystems with binary mount data to explicitly set Smack mount
778 static int smack_set_mnt_opts(struct super_block *sb,
780 unsigned long kern_flags,
781 unsigned long *set_kern_flags)
783 struct dentry *root = sb->s_root;
784 struct inode *inode = d_backing_inode(root);
785 struct superblock_smack *sp = sb->s_security;
786 struct inode_smack *isp;
787 struct smack_known *skp;
788 struct smack_mnt_opts *opts = mnt_opts;
789 bool transmute = false;
791 if (sp->smk_flags & SMK_SB_INITIALIZED)
794 if (inode->i_security == NULL) {
795 int rc = lsm_inode_alloc(inode);
801 if (!smack_privileged(CAP_MAC_ADMIN)) {
803 * Unprivileged mounts don't get to specify Smack values.
808 * Unprivileged mounts get root and default from the caller.
810 skp = smk_of_current();
812 sp->smk_default = skp;
814 * For a handful of fs types with no user-controlled
815 * backing store it's okay to trust security labels
816 * in the filesystem. The rest are untrusted.
818 if (sb->s_user_ns != &init_user_ns &&
819 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
820 sb->s_magic != RAMFS_MAGIC) {
822 sp->smk_flags |= SMK_SB_UNTRUSTED;
826 sp->smk_flags |= SMK_SB_INITIALIZED;
829 if (opts->fsdefault) {
830 skp = smk_import_entry(opts->fsdefault, 0);
833 sp->smk_default = skp;
836 skp = smk_import_entry(opts->fsfloor, 0);
842 skp = smk_import_entry(opts->fshat, 0);
848 skp = smk_import_entry(opts->fsroot, 0);
853 if (opts->fstransmute) {
854 skp = smk_import_entry(opts->fstransmute, 0);
863 * Initialize the root inode.
865 init_inode_smack(inode, sp->smk_root);
868 isp = smack_inode(inode);
869 isp->smk_flags |= SMK_INODE_TRANSMUTE;
876 * smack_sb_statfs - Smack check on statfs
877 * @dentry: identifies the file system in question
879 * Returns 0 if current can read the floor of the filesystem,
880 * and error code otherwise
882 static int smack_sb_statfs(struct dentry *dentry)
884 struct superblock_smack *sbp = dentry->d_sb->s_security;
886 struct smk_audit_info ad;
888 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
889 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
891 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
892 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
901 * smack_bprm_set_creds - set creds for exec
902 * @bprm: the exec information
904 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
906 static int smack_bprm_set_creds(struct linux_binprm *bprm)
908 struct inode *inode = file_inode(bprm->file);
909 struct task_smack *bsp = smack_cred(bprm->cred);
910 struct inode_smack *isp;
911 struct superblock_smack *sbsp;
914 if (bprm->called_set_creds)
917 isp = smack_inode(inode);
918 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
921 sbsp = inode->i_sb->s_security;
922 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
923 isp->smk_task != sbsp->smk_root)
926 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
927 struct task_struct *tracer;
931 tracer = ptrace_parent(current);
932 if (likely(tracer != NULL))
933 rc = smk_ptrace_rule_check(tracer,
941 } else if (bprm->unsafe)
944 bsp->smk_task = isp->smk_task;
945 bprm->per_clear |= PER_CLEAR_ON_SETID;
947 /* Decide if this is a secure exec. */
948 if (bsp->smk_task != bsp->smk_forked)
949 bprm->secureexec = 1;
959 * smack_inode_alloc_security - allocate an inode blob
960 * @inode: the inode in need of a blob
962 * Returns 0 if it gets a blob, -ENOMEM otherwise
964 static int smack_inode_alloc_security(struct inode *inode)
966 struct smack_known *skp = smk_of_current();
968 init_inode_smack(inode, skp);
973 * smack_inode_init_security - copy out the smack from an inode
974 * @inode: the newly created inode
975 * @dir: containing directory object
977 * @name: where to put the attribute name
978 * @value: where to put the attribute value
979 * @len: where to put the length of the attribute
981 * Returns 0 if it all works out, -ENOMEM if there's no memory
983 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
984 const struct qstr *qstr, const char **name,
985 void **value, size_t *len)
987 struct inode_smack *issp = smack_inode(inode);
988 struct smack_known *skp = smk_of_current();
989 struct smack_known *isp = smk_of_inode(inode);
990 struct smack_known *dsp = smk_of_inode(dir);
994 *name = XATTR_SMACK_SUFFIX;
998 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1003 * If the access rule allows transmutation and
1004 * the directory requests transmutation then
1005 * by all means transmute.
1006 * Mark the inode as changed.
1008 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1009 smk_inode_transmutable(dir)) {
1011 issp->smk_flags |= SMK_INODE_CHANGED;
1014 *value = kstrdup(isp->smk_known, GFP_NOFS);
1018 *len = strlen(isp->smk_known);
1025 * smack_inode_link - Smack check on link
1026 * @old_dentry: the existing object
1028 * @new_dentry: the new object
1030 * Returns 0 if access is permitted, an error code otherwise
1032 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1033 struct dentry *new_dentry)
1035 struct smack_known *isp;
1036 struct smk_audit_info ad;
1039 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1040 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1042 isp = smk_of_inode(d_backing_inode(old_dentry));
1043 rc = smk_curacc(isp, MAY_WRITE, &ad);
1044 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1046 if (rc == 0 && d_is_positive(new_dentry)) {
1047 isp = smk_of_inode(d_backing_inode(new_dentry));
1048 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1049 rc = smk_curacc(isp, MAY_WRITE, &ad);
1050 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1057 * smack_inode_unlink - Smack check on inode deletion
1058 * @dir: containing directory object
1059 * @dentry: file to unlink
1061 * Returns 0 if current can write the containing directory
1062 * and the object, error code otherwise
1064 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1066 struct inode *ip = d_backing_inode(dentry);
1067 struct smk_audit_info ad;
1070 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1071 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1074 * You need write access to the thing you're unlinking
1076 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1077 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1080 * You also need write access to the containing directory
1082 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1083 smk_ad_setfield_u_fs_inode(&ad, dir);
1084 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1085 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1091 * smack_inode_rmdir - Smack check on directory deletion
1092 * @dir: containing directory object
1093 * @dentry: directory to unlink
1095 * Returns 0 if current can write the containing directory
1096 * and the directory, error code otherwise
1098 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1100 struct smk_audit_info ad;
1103 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1104 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1107 * You need write access to the thing you're removing
1109 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1110 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1113 * You also need write access to the containing directory
1115 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1116 smk_ad_setfield_u_fs_inode(&ad, dir);
1117 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1118 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1125 * smack_inode_rename - Smack check on rename
1126 * @old_inode: unused
1127 * @old_dentry: the old object
1128 * @new_inode: unused
1129 * @new_dentry: the new object
1131 * Read and write access is required on both the old and
1134 * Returns 0 if access is permitted, an error code otherwise
1136 static int smack_inode_rename(struct inode *old_inode,
1137 struct dentry *old_dentry,
1138 struct inode *new_inode,
1139 struct dentry *new_dentry)
1142 struct smack_known *isp;
1143 struct smk_audit_info ad;
1145 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1146 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1148 isp = smk_of_inode(d_backing_inode(old_dentry));
1149 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1150 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1152 if (rc == 0 && d_is_positive(new_dentry)) {
1153 isp = smk_of_inode(d_backing_inode(new_dentry));
1154 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1155 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1156 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1162 * smack_inode_permission - Smack version of permission()
1163 * @inode: the inode in question
1164 * @mask: the access requested
1166 * This is the important Smack hook.
1168 * Returns 0 if access is permitted, -EACCES otherwise
1170 static int smack_inode_permission(struct inode *inode, int mask)
1172 struct superblock_smack *sbsp = inode->i_sb->s_security;
1173 struct smk_audit_info ad;
1174 int no_block = mask & MAY_NOT_BLOCK;
1177 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1179 * No permission to check. Existence test. Yup, it's there.
1184 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1185 if (smk_of_inode(inode) != sbsp->smk_root)
1189 /* May be droppable after audit */
1192 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1193 smk_ad_setfield_u_fs_inode(&ad, inode);
1194 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1195 rc = smk_bu_inode(inode, mask, rc);
1200 * smack_inode_setattr - Smack check for setting attributes
1201 * @dentry: the object
1202 * @iattr: for the force flag
1204 * Returns 0 if access is permitted, an error code otherwise
1206 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1208 struct smk_audit_info ad;
1212 * Need to allow for clearing the setuid bit.
1214 if (iattr->ia_valid & ATTR_FORCE)
1216 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1217 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1219 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1220 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1225 * smack_inode_getattr - Smack check for getting attributes
1226 * @mnt: vfsmount of the object
1227 * @dentry: the object
1229 * Returns 0 if access is permitted, an error code otherwise
1231 static int smack_inode_getattr(const struct path *path)
1233 struct smk_audit_info ad;
1234 struct inode *inode = d_backing_inode(path->dentry);
1237 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1238 smk_ad_setfield_u_fs_path(&ad, *path);
1239 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1240 rc = smk_bu_inode(inode, MAY_READ, rc);
1245 * smack_inode_setxattr - Smack check for setting xattrs
1246 * @dentry: the object
1247 * @name: name of the attribute
1248 * @value: value of the attribute
1249 * @size: size of the value
1252 * This protects the Smack attribute explicitly.
1254 * Returns 0 if access is permitted, an error code otherwise
1256 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1257 const void *value, size_t size, int flags)
1259 struct smk_audit_info ad;
1260 struct smack_known *skp;
1262 int check_import = 0;
1267 * Check label validity here so import won't fail in post_setxattr
1269 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1270 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1271 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1274 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1275 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1279 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1281 if (size != TRANS_TRUE_SIZE ||
1282 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1285 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1287 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1290 if (rc == 0 && check_import) {
1291 skp = size ? smk_import_entry(value, size) : NULL;
1294 else if (skp == NULL || (check_star &&
1295 (skp == &smack_known_star || skp == &smack_known_web)))
1299 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1300 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1303 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1304 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1311 * smack_inode_post_setxattr - Apply the Smack update approved above
1313 * @name: attribute name
1314 * @value: attribute value
1315 * @size: attribute size
1318 * Set the pointer in the inode blob to the entry found
1319 * in the master label list.
1321 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1322 const void *value, size_t size, int flags)
1324 struct smack_known *skp;
1325 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1327 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1328 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1332 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1333 skp = smk_import_entry(value, size);
1335 isp->smk_inode = skp;
1336 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1337 skp = smk_import_entry(value, size);
1339 isp->smk_task = skp;
1340 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1341 skp = smk_import_entry(value, size);
1343 isp->smk_mmap = skp;
1350 * smack_inode_getxattr - Smack check on getxattr
1351 * @dentry: the object
1354 * Returns 0 if access is permitted, an error code otherwise
1356 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1358 struct smk_audit_info ad;
1361 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1362 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1364 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1365 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1370 * smack_inode_removexattr - Smack check on removexattr
1371 * @dentry: the object
1372 * @name: name of the attribute
1374 * Removing the Smack attribute requires CAP_MAC_ADMIN
1376 * Returns 0 if access is permitted, an error code otherwise
1378 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1380 struct inode_smack *isp;
1381 struct smk_audit_info ad;
1384 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1385 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1386 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1387 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1388 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1389 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1390 if (!smack_privileged(CAP_MAC_ADMIN))
1393 rc = cap_inode_removexattr(dentry, name);
1398 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1399 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1401 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1402 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1406 isp = smack_inode(d_backing_inode(dentry));
1408 * Don't do anything special for these.
1409 * XATTR_NAME_SMACKIPIN
1410 * XATTR_NAME_SMACKIPOUT
1412 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1413 struct super_block *sbp = dentry->d_sb;
1414 struct superblock_smack *sbsp = sbp->s_security;
1416 isp->smk_inode = sbsp->smk_default;
1417 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1418 isp->smk_task = NULL;
1419 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1420 isp->smk_mmap = NULL;
1421 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1422 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1428 * smack_inode_getsecurity - get smack xattrs
1429 * @inode: the object
1430 * @name: attribute name
1431 * @buffer: where to put the result
1432 * @alloc: duplicate memory
1434 * Returns the size of the attribute or an error code
1436 static int smack_inode_getsecurity(struct inode *inode,
1437 const char *name, void **buffer,
1440 struct socket_smack *ssp;
1441 struct socket *sock;
1442 struct super_block *sbp;
1443 struct inode *ip = (struct inode *)inode;
1444 struct smack_known *isp;
1446 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1447 isp = smk_of_inode(inode);
1450 * The rest of the Smack xattrs are only on sockets.
1453 if (sbp->s_magic != SOCKFS_MAGIC)
1456 sock = SOCKET_I(ip);
1457 if (sock == NULL || sock->sk == NULL)
1460 ssp = sock->sk->sk_security;
1462 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1464 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1471 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1472 if (*buffer == NULL)
1476 return strlen(isp->smk_known);
1481 * smack_inode_listsecurity - list the Smack attributes
1482 * @inode: the object
1483 * @buffer: where they go
1484 * @buffer_size: size of buffer
1486 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1489 int len = sizeof(XATTR_NAME_SMACK);
1491 if (buffer != NULL && len <= buffer_size)
1492 memcpy(buffer, XATTR_NAME_SMACK, len);
1498 * smack_inode_getsecid - Extract inode's security id
1499 * @inode: inode to extract the info from
1500 * @secid: where result will be saved
1502 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1504 struct smack_known *skp = smk_of_inode(inode);
1506 *secid = skp->smk_secid;
1514 * There is no smack_file_permission hook
1516 * Should access checks be done on each read or write?
1517 * UNICOS and SELinux say yes.
1518 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1520 * I'll say no for now. Smack does not do the frequent
1521 * label changing that SELinux does.
1525 * smack_file_alloc_security - assign a file security blob
1528 * The security blob for a file is a pointer to the master
1529 * label list, so no allocation is done.
1531 * f_security is the owner security information. It
1532 * isn't used on file access checks, it's for send_sigio.
1536 static int smack_file_alloc_security(struct file *file)
1538 struct smack_known **blob = smack_file(file);
1540 *blob = smk_of_current();
1545 * smack_file_ioctl - Smack check on ioctls
1550 * Relies heavily on the correct use of the ioctl command conventions.
1552 * Returns 0 if allowed, error code otherwise
1554 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1558 struct smk_audit_info ad;
1559 struct inode *inode = file_inode(file);
1561 if (unlikely(IS_PRIVATE(inode)))
1564 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1565 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1567 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1568 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1569 rc = smk_bu_file(file, MAY_WRITE, rc);
1572 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1573 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1574 rc = smk_bu_file(file, MAY_READ, rc);
1581 * smack_file_lock - Smack check on file locking
1585 * Returns 0 if current has lock access, error code otherwise
1587 static int smack_file_lock(struct file *file, unsigned int cmd)
1589 struct smk_audit_info ad;
1591 struct inode *inode = file_inode(file);
1593 if (unlikely(IS_PRIVATE(inode)))
1596 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1597 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1598 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1599 rc = smk_bu_file(file, MAY_LOCK, rc);
1604 * smack_file_fcntl - Smack check on fcntl
1606 * @cmd: what action to check
1609 * Generally these operations are harmless.
1610 * File locking operations present an obvious mechanism
1611 * for passing information, so they require write access.
1613 * Returns 0 if current has access, error code otherwise
1615 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1618 struct smk_audit_info ad;
1620 struct inode *inode = file_inode(file);
1622 if (unlikely(IS_PRIVATE(inode)))
1630 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1631 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1632 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1633 rc = smk_bu_file(file, MAY_LOCK, rc);
1637 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1638 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1639 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1640 rc = smk_bu_file(file, MAY_WRITE, rc);
1651 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1652 * if mapping anonymous memory.
1653 * @file contains the file structure for file to map (may be NULL).
1654 * @reqprot contains the protection requested by the application.
1655 * @prot contains the protection that will be applied by the kernel.
1656 * @flags contains the operational flags.
1657 * Return 0 if permission is granted.
1659 static int smack_mmap_file(struct file *file,
1660 unsigned long reqprot, unsigned long prot,
1661 unsigned long flags)
1663 struct smack_known *skp;
1664 struct smack_known *mkp;
1665 struct smack_rule *srp;
1666 struct task_smack *tsp;
1667 struct smack_known *okp;
1668 struct inode_smack *isp;
1669 struct superblock_smack *sbsp;
1678 if (unlikely(IS_PRIVATE(file_inode(file))))
1681 isp = smack_inode(file_inode(file));
1682 if (isp->smk_mmap == NULL)
1684 sbsp = file_inode(file)->i_sb->s_security;
1685 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1686 isp->smk_mmap != sbsp->smk_root)
1688 mkp = isp->smk_mmap;
1690 tsp = smack_cred(current_cred());
1691 skp = smk_of_current();
1696 * For each Smack rule associated with the subject
1697 * label verify that the SMACK64MMAP also has access
1698 * to that rule's object label.
1700 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1701 okp = srp->smk_object;
1703 * Matching labels always allows access.
1705 if (mkp->smk_known == okp->smk_known)
1708 * If there is a matching local rule take
1709 * that into account as well.
1711 may = smk_access_entry(srp->smk_subject->smk_known,
1715 may = srp->smk_access;
1717 may &= srp->smk_access;
1719 * If may is zero the SMACK64MMAP subject can't
1720 * possibly have less access.
1726 * Fetch the global list entry.
1727 * If there isn't one a SMACK64MMAP subject
1728 * can't have as much access as current.
1730 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1732 if (mmay == -ENOENT) {
1737 * If there is a local entry it modifies the
1738 * potential access, too.
1740 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1742 if (tmay != -ENOENT)
1746 * If there is any access available to current that is
1747 * not available to a SMACK64MMAP subject
1750 if ((may | mmay) != mmay) {
1762 * smack_file_set_fowner - set the file security blob value
1763 * @file: object in question
1766 static void smack_file_set_fowner(struct file *file)
1768 struct smack_known **blob = smack_file(file);
1770 *blob = smk_of_current();
1774 * smack_file_send_sigiotask - Smack on sigio
1775 * @tsk: The target task
1776 * @fown: the object the signal come from
1779 * Allow a privileged task to get signals even if it shouldn't
1781 * Returns 0 if a subject with the object's smack could
1782 * write to the task, an error code otherwise.
1784 static int smack_file_send_sigiotask(struct task_struct *tsk,
1785 struct fown_struct *fown, int signum)
1787 struct smack_known **blob;
1788 struct smack_known *skp;
1789 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1790 const struct cred *tcred;
1793 struct smk_audit_info ad;
1796 * struct fown_struct is never outside the context of a struct file
1798 file = container_of(fown, struct file, f_owner);
1800 /* we don't log here as rc can be overriden */
1801 blob = smack_file(file);
1803 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1804 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1807 tcred = __task_cred(tsk);
1808 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1812 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1813 smk_ad_setfield_u_tsk(&ad, tsk);
1814 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1819 * smack_file_receive - Smack file receive check
1822 * Returns 0 if current has access, error code otherwise
1824 static int smack_file_receive(struct file *file)
1828 struct smk_audit_info ad;
1829 struct inode *inode = file_inode(file);
1830 struct socket *sock;
1831 struct task_smack *tsp;
1832 struct socket_smack *ssp;
1834 if (unlikely(IS_PRIVATE(inode)))
1837 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1838 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1840 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1841 sock = SOCKET_I(inode);
1842 ssp = sock->sk->sk_security;
1843 tsp = smack_cred(current_cred());
1845 * If the receiving process can't write to the
1846 * passed socket or if the passed socket can't
1847 * write to the receiving process don't accept
1848 * the passed socket.
1850 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1851 rc = smk_bu_file(file, may, rc);
1854 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1855 rc = smk_bu_file(file, may, rc);
1859 * This code relies on bitmasks.
1861 if (file->f_mode & FMODE_READ)
1863 if (file->f_mode & FMODE_WRITE)
1866 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1867 rc = smk_bu_file(file, may, rc);
1872 * smack_file_open - Smack dentry open processing
1874 * @cred: task credential
1876 * Set the security blob in the file structure.
1877 * Allow the open only if the task has read access. There are
1878 * many read operations (e.g. fstat) that you can do with an
1879 * fd even if you have the file open write-only.
1883 static int smack_file_open(struct file *file)
1885 struct task_smack *tsp = smack_cred(file->f_cred);
1886 struct inode *inode = file_inode(file);
1887 struct smk_audit_info ad;
1890 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1891 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1892 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1893 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1903 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1904 * @new: the new credentials
1905 * @gfp: the atomicity of any memory allocations
1907 * Prepare a blank set of credentials for modification. This must allocate all
1908 * the memory the LSM module might require such that cred_transfer() can
1909 * complete without error.
1911 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1913 init_task_smack(smack_cred(cred), NULL, NULL);
1919 * smack_cred_free - "free" task-level security credentials
1920 * @cred: the credentials in question
1923 static void smack_cred_free(struct cred *cred)
1925 struct task_smack *tsp = smack_cred(cred);
1926 struct smack_rule *rp;
1927 struct list_head *l;
1928 struct list_head *n;
1930 smk_destroy_label_list(&tsp->smk_relabel);
1932 list_for_each_safe(l, n, &tsp->smk_rules) {
1933 rp = list_entry(l, struct smack_rule, list);
1934 list_del(&rp->list);
1935 kmem_cache_free(smack_rule_cache, rp);
1940 * smack_cred_prepare - prepare new set of credentials for modification
1941 * @new: the new credentials
1942 * @old: the original credentials
1943 * @gfp: the atomicity of any memory allocations
1945 * Prepare a new set of credentials for modification.
1947 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1950 struct task_smack *old_tsp = smack_cred(old);
1951 struct task_smack *new_tsp = smack_cred(new);
1954 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1956 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1960 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1966 * smack_cred_transfer - Transfer the old credentials to the new credentials
1967 * @new: the new credentials
1968 * @old: the original credentials
1970 * Fill in a set of blank credentials from another set of credentials.
1972 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1974 struct task_smack *old_tsp = smack_cred(old);
1975 struct task_smack *new_tsp = smack_cred(new);
1977 new_tsp->smk_task = old_tsp->smk_task;
1978 new_tsp->smk_forked = old_tsp->smk_task;
1979 mutex_init(&new_tsp->smk_rules_lock);
1980 INIT_LIST_HEAD(&new_tsp->smk_rules);
1982 /* cbs copy rule list */
1986 * smack_cred_getsecid - get the secid corresponding to a creds structure
1987 * @c: the object creds
1988 * @secid: where to put the result
1990 * Sets the secid to contain a u32 version of the smack label.
1992 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1994 struct smack_known *skp;
1997 skp = smk_of_task(smack_cred(cred));
1998 *secid = skp->smk_secid;
2003 * smack_kernel_act_as - Set the subjective context in a set of credentials
2004 * @new: points to the set of credentials to be modified.
2005 * @secid: specifies the security ID to be set
2007 * Set the security data for a kernel service.
2009 static int smack_kernel_act_as(struct cred *new, u32 secid)
2011 struct task_smack *new_tsp = smack_cred(new);
2013 new_tsp->smk_task = smack_from_secid(secid);
2018 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2019 * @new: points to the set of credentials to be modified
2020 * @inode: points to the inode to use as a reference
2022 * Set the file creation context in a set of credentials to the same
2023 * as the objective context of the specified inode
2025 static int smack_kernel_create_files_as(struct cred *new,
2026 struct inode *inode)
2028 struct inode_smack *isp = smack_inode(inode);
2029 struct task_smack *tsp = smack_cred(new);
2031 tsp->smk_forked = isp->smk_inode;
2032 tsp->smk_task = tsp->smk_forked;
2037 * smk_curacc_on_task - helper to log task related access
2038 * @p: the task object
2039 * @access: the access requested
2040 * @caller: name of the calling function for audit
2042 * Return 0 if access is permitted
2044 static int smk_curacc_on_task(struct task_struct *p, int access,
2047 struct smk_audit_info ad;
2048 struct smack_known *skp = smk_of_task_struct(p);
2051 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2052 smk_ad_setfield_u_tsk(&ad, p);
2053 rc = smk_curacc(skp, access, &ad);
2054 rc = smk_bu_task(p, access, rc);
2059 * smack_task_setpgid - Smack check on setting pgid
2060 * @p: the task object
2063 * Return 0 if write access is permitted
2065 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2067 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2071 * smack_task_getpgid - Smack access check for getpgid
2072 * @p: the object task
2074 * Returns 0 if current can read the object task, error code otherwise
2076 static int smack_task_getpgid(struct task_struct *p)
2078 return smk_curacc_on_task(p, MAY_READ, __func__);
2082 * smack_task_getsid - Smack access check for getsid
2083 * @p: the object task
2085 * Returns 0 if current can read the object task, error code otherwise
2087 static int smack_task_getsid(struct task_struct *p)
2089 return smk_curacc_on_task(p, MAY_READ, __func__);
2093 * smack_task_getsecid - get the secid of the task
2094 * @p: the object task
2095 * @secid: where to put the result
2097 * Sets the secid to contain a u32 version of the smack label.
2099 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2101 struct smack_known *skp = smk_of_task_struct(p);
2103 *secid = skp->smk_secid;
2107 * smack_task_setnice - Smack check on setting nice
2108 * @p: the task object
2111 * Return 0 if write access is permitted
2113 static int smack_task_setnice(struct task_struct *p, int nice)
2115 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2119 * smack_task_setioprio - Smack check on setting ioprio
2120 * @p: the task object
2123 * Return 0 if write access is permitted
2125 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2127 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2131 * smack_task_getioprio - Smack check on reading ioprio
2132 * @p: the task object
2134 * Return 0 if read access is permitted
2136 static int smack_task_getioprio(struct task_struct *p)
2138 return smk_curacc_on_task(p, MAY_READ, __func__);
2142 * smack_task_setscheduler - Smack check on setting scheduler
2143 * @p: the task object
2147 * Return 0 if read access is permitted
2149 static int smack_task_setscheduler(struct task_struct *p)
2151 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2155 * smack_task_getscheduler - Smack check on reading scheduler
2156 * @p: the task object
2158 * Return 0 if read access is permitted
2160 static int smack_task_getscheduler(struct task_struct *p)
2162 return smk_curacc_on_task(p, MAY_READ, __func__);
2166 * smack_task_movememory - Smack check on moving memory
2167 * @p: the task object
2169 * Return 0 if write access is permitted
2171 static int smack_task_movememory(struct task_struct *p)
2173 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2177 * smack_task_kill - Smack check on signal delivery
2178 * @p: the task object
2181 * @cred: identifies the cred to use in lieu of current's
2183 * Return 0 if write access is permitted
2186 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2187 int sig, const struct cred *cred)
2189 struct smk_audit_info ad;
2190 struct smack_known *skp;
2191 struct smack_known *tkp = smk_of_task_struct(p);
2195 return 0; /* null signal; existence test */
2197 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2198 smk_ad_setfield_u_tsk(&ad, p);
2200 * Sending a signal requires that the sender
2201 * can write the receiver.
2204 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2205 rc = smk_bu_task(p, MAY_DELIVER, rc);
2209 * If the cred isn't NULL we're dealing with some USB IO
2210 * specific behavior. This is not clean. For one thing
2211 * we can't take privilege into account.
2213 skp = smk_of_task(smack_cred(cred));
2214 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2215 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2220 * smack_task_to_inode - copy task smack into the inode blob
2221 * @p: task to copy from
2222 * @inode: inode to copy to
2224 * Sets the smack pointer in the inode security blob
2226 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2228 struct inode_smack *isp = smack_inode(inode);
2229 struct smack_known *skp = smk_of_task_struct(p);
2231 isp->smk_inode = skp;
2232 isp->smk_flags |= SMK_INODE_INSTANT;
2240 * smack_sk_alloc_security - Allocate a socket blob
2243 * @gfp_flags: memory allocation flags
2245 * Assign Smack pointers to current
2247 * Returns 0 on success, -ENOMEM is there's no memory
2249 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2251 struct smack_known *skp = smk_of_current();
2252 struct socket_smack *ssp;
2254 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2259 * Sockets created by kernel threads receive web label.
2261 if (unlikely(current->flags & PF_KTHREAD)) {
2262 ssp->smk_in = &smack_known_web;
2263 ssp->smk_out = &smack_known_web;
2268 ssp->smk_packet = NULL;
2270 sk->sk_security = ssp;
2276 * smack_sk_free_security - Free a socket blob
2279 * Clears the blob pointer
2281 static void smack_sk_free_security(struct sock *sk)
2283 #ifdef SMACK_IPV6_PORT_LABELING
2284 struct smk_port_label *spp;
2286 if (sk->sk_family == PF_INET6) {
2288 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2289 if (spp->smk_sock != sk)
2291 spp->smk_can_reuse = 1;
2297 kfree(sk->sk_security);
2301 * smack_ipv4host_label - check host based restrictions
2302 * @sip: the object end
2304 * looks for host based access restrictions
2306 * This version will only be appropriate for really small sets of single label
2307 * hosts. The caller is responsible for ensuring that the RCU read lock is
2308 * taken before calling this function.
2310 * Returns the label of the far end or NULL if it's not special.
2312 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2314 struct smk_net4addr *snp;
2315 struct in_addr *siap = &sip->sin_addr;
2317 if (siap->s_addr == 0)
2320 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2322 * we break after finding the first match because
2323 * the list is sorted from longest to shortest mask
2324 * so we have found the most specific match
2326 if (snp->smk_host.s_addr ==
2327 (siap->s_addr & snp->smk_mask.s_addr))
2328 return snp->smk_label;
2333 #if IS_ENABLED(CONFIG_IPV6)
2335 * smk_ipv6_localhost - Check for local ipv6 host address
2338 * Returns boolean true if this is the localhost address
2340 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2342 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2343 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2345 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2346 ntohs(be16p[7]) == 1)
2352 * smack_ipv6host_label - check host based restrictions
2353 * @sip: the object end
2355 * looks for host based access restrictions
2357 * This version will only be appropriate for really small sets of single label
2358 * hosts. The caller is responsible for ensuring that the RCU read lock is
2359 * taken before calling this function.
2361 * Returns the label of the far end or NULL if it's not special.
2363 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2365 struct smk_net6addr *snp;
2366 struct in6_addr *sap = &sip->sin6_addr;
2371 * It's local. Don't look for a host label.
2373 if (smk_ipv6_localhost(sip))
2376 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2378 * If the label is NULL the entry has
2379 * been renounced. Ignore it.
2381 if (snp->smk_label == NULL)
2384 * we break after finding the first match because
2385 * the list is sorted from longest to shortest mask
2386 * so we have found the most specific match
2388 for (found = 1, i = 0; i < 8; i++) {
2389 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2390 snp->smk_host.s6_addr16[i]) {
2396 return snp->smk_label;
2401 #endif /* CONFIG_IPV6 */
2404 * smack_netlabel - Set the secattr on a socket
2406 * @labeled: socket label scheme
2408 * Convert the outbound smack value (smk_out) to a
2409 * secattr and attach it to the socket.
2411 * Returns 0 on success or an error code
2413 static int smack_netlabel(struct sock *sk, int labeled)
2415 struct smack_known *skp;
2416 struct socket_smack *ssp = sk->sk_security;
2420 * Usually the netlabel code will handle changing the
2421 * packet labeling based on the label.
2422 * The case of a single label host is different, because
2423 * a single label host should never get a labeled packet
2424 * even though the label is usually associated with a packet
2428 bh_lock_sock_nested(sk);
2430 if (ssp->smk_out == smack_net_ambient ||
2431 labeled == SMACK_UNLABELED_SOCKET)
2432 netlbl_sock_delattr(sk);
2435 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2445 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2447 * @sap: the destination address
2449 * Set the correct secattr for the given socket based on the destination
2450 * address and perform any outbound access checks needed.
2452 * Returns 0 on success or an error code.
2455 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2457 struct smack_known *skp;
2460 struct smack_known *hkp;
2461 struct socket_smack *ssp = sk->sk_security;
2462 struct smk_audit_info ad;
2465 hkp = smack_ipv4host_label(sap);
2468 struct lsm_network_audit net;
2470 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2471 ad.a.u.net->family = sap->sin_family;
2472 ad.a.u.net->dport = sap->sin_port;
2473 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2475 sk_lbl = SMACK_UNLABELED_SOCKET;
2477 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2478 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2480 sk_lbl = SMACK_CIPSO_SOCKET;
2487 return smack_netlabel(sk, sk_lbl);
2490 #if IS_ENABLED(CONFIG_IPV6)
2492 * smk_ipv6_check - check Smack access
2493 * @subject: subject Smack label
2494 * @object: object Smack label
2496 * @act: the action being taken
2498 * Check an IPv6 access
2500 static int smk_ipv6_check(struct smack_known *subject,
2501 struct smack_known *object,
2502 struct sockaddr_in6 *address, int act)
2505 struct lsm_network_audit net;
2507 struct smk_audit_info ad;
2511 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2512 ad.a.u.net->family = PF_INET6;
2513 ad.a.u.net->dport = ntohs(address->sin6_port);
2514 if (act == SMK_RECEIVING)
2515 ad.a.u.net->v6info.saddr = address->sin6_addr;
2517 ad.a.u.net->v6info.daddr = address->sin6_addr;
2519 rc = smk_access(subject, object, MAY_WRITE, &ad);
2520 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2523 #endif /* CONFIG_IPV6 */
2525 #ifdef SMACK_IPV6_PORT_LABELING
2527 * smk_ipv6_port_label - Smack port access table management
2531 * Create or update the port list entry
2533 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2535 struct sock *sk = sock->sk;
2536 struct sockaddr_in6 *addr6;
2537 struct socket_smack *ssp = sock->sk->sk_security;
2538 struct smk_port_label *spp;
2539 unsigned short port = 0;
2541 if (address == NULL) {
2543 * This operation is changing the Smack information
2544 * on the bound socket. Take the changes to the port
2548 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2549 if (sk != spp->smk_sock)
2551 spp->smk_in = ssp->smk_in;
2552 spp->smk_out = ssp->smk_out;
2557 * A NULL address is only used for updating existing
2558 * bound entries. If there isn't one, it's OK.
2564 addr6 = (struct sockaddr_in6 *)address;
2565 port = ntohs(addr6->sin6_port);
2567 * This is a special case that is safely ignored.
2573 * Look for an existing port list entry.
2574 * This is an indication that a port is getting reused.
2577 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2578 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2580 if (spp->smk_can_reuse != 1) {
2584 spp->smk_port = port;
2586 spp->smk_in = ssp->smk_in;
2587 spp->smk_out = ssp->smk_out;
2588 spp->smk_can_reuse = 0;
2594 * A new port entry is required.
2596 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2600 spp->smk_port = port;
2602 spp->smk_in = ssp->smk_in;
2603 spp->smk_out = ssp->smk_out;
2604 spp->smk_sock_type = sock->type;
2605 spp->smk_can_reuse = 0;
2607 mutex_lock(&smack_ipv6_lock);
2608 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2609 mutex_unlock(&smack_ipv6_lock);
2614 * smk_ipv6_port_check - check Smack port access
2618 * Create or update the port list entry
2620 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2623 struct smk_port_label *spp;
2624 struct socket_smack *ssp = sk->sk_security;
2625 struct smack_known *skp = NULL;
2626 unsigned short port;
2627 struct smack_known *object;
2629 if (act == SMK_RECEIVING) {
2630 skp = smack_ipv6host_label(address);
2631 object = ssp->smk_in;
2634 object = smack_ipv6host_label(address);
2638 * The other end is a single label host.
2640 if (skp != NULL && object != NULL)
2641 return smk_ipv6_check(skp, object, address, act);
2643 skp = smack_net_ambient;
2645 object = smack_net_ambient;
2648 * It's remote, so port lookup does no good.
2650 if (!smk_ipv6_localhost(address))
2651 return smk_ipv6_check(skp, object, address, act);
2654 * It's local so the send check has to have passed.
2656 if (act == SMK_RECEIVING)
2659 port = ntohs(address->sin6_port);
2661 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2662 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2664 object = spp->smk_in;
2665 if (act == SMK_CONNECTING)
2666 ssp->smk_packet = spp->smk_out;
2671 return smk_ipv6_check(skp, object, address, act);
2673 #endif /* SMACK_IPV6_PORT_LABELING */
2676 * smack_inode_setsecurity - set smack xattrs
2677 * @inode: the object
2678 * @name: attribute name
2679 * @value: attribute value
2680 * @size: size of the attribute
2683 * Sets the named attribute in the appropriate blob
2685 * Returns 0 on success, or an error code
2687 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2688 const void *value, size_t size, int flags)
2690 struct smack_known *skp;
2691 struct inode_smack *nsp = smack_inode(inode);
2692 struct socket_smack *ssp;
2693 struct socket *sock;
2696 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2699 skp = smk_import_entry(value, size);
2701 return PTR_ERR(skp);
2703 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2704 nsp->smk_inode = skp;
2705 nsp->smk_flags |= SMK_INODE_INSTANT;
2709 * The rest of the Smack xattrs are only on sockets.
2711 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2714 sock = SOCKET_I(inode);
2715 if (sock == NULL || sock->sk == NULL)
2718 ssp = sock->sk->sk_security;
2720 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2722 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2724 if (sock->sk->sk_family == PF_INET) {
2725 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2728 "Smack: \"%s\" netlbl error %d.\n",
2734 #ifdef SMACK_IPV6_PORT_LABELING
2735 if (sock->sk->sk_family == PF_INET6)
2736 smk_ipv6_port_label(sock, NULL);
2743 * smack_socket_post_create - finish socket setup
2745 * @family: protocol family
2750 * Sets the netlabel information on the socket
2752 * Returns 0 on success, and error code otherwise
2754 static int smack_socket_post_create(struct socket *sock, int family,
2755 int type, int protocol, int kern)
2757 struct socket_smack *ssp;
2759 if (sock->sk == NULL)
2763 * Sockets created by kernel threads receive web label.
2765 if (unlikely(current->flags & PF_KTHREAD)) {
2766 ssp = sock->sk->sk_security;
2767 ssp->smk_in = &smack_known_web;
2768 ssp->smk_out = &smack_known_web;
2771 if (family != PF_INET)
2774 * Set the outbound netlbl.
2776 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2780 * smack_socket_socketpair - create socket pair
2781 * @socka: one socket
2782 * @sockb: another socket
2784 * Cross reference the peer labels for SO_PEERSEC
2786 * Returns 0 on success, and error code otherwise
2788 static int smack_socket_socketpair(struct socket *socka,
2789 struct socket *sockb)
2791 struct socket_smack *asp = socka->sk->sk_security;
2792 struct socket_smack *bsp = sockb->sk->sk_security;
2794 asp->smk_packet = bsp->smk_out;
2795 bsp->smk_packet = asp->smk_out;
2800 #ifdef SMACK_IPV6_PORT_LABELING
2802 * smack_socket_bind - record port binding information.
2804 * @address: the port address
2805 * @addrlen: size of the address
2807 * Records the label bound to a port.
2809 * Returns 0 on success, and error code otherwise
2811 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2814 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2815 if (addrlen < SIN6_LEN_RFC2133 ||
2816 address->sa_family != AF_INET6)
2818 smk_ipv6_port_label(sock, address);
2822 #endif /* SMACK_IPV6_PORT_LABELING */
2825 * smack_socket_connect - connect access check
2827 * @sap: the other end
2828 * @addrlen: size of sap
2830 * Verifies that a connection may be possible
2832 * Returns 0 on success, and error code otherwise
2834 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2838 #if IS_ENABLED(CONFIG_IPV6)
2839 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2841 #ifdef SMACK_IPV6_SECMARK_LABELING
2842 struct smack_known *rsp;
2843 struct socket_smack *ssp;
2846 if (sock->sk == NULL)
2849 #ifdef SMACK_IPV6_SECMARK_LABELING
2850 ssp = sock->sk->sk_security;
2853 switch (sock->sk->sk_family) {
2855 if (addrlen < sizeof(struct sockaddr_in) ||
2856 sap->sa_family != AF_INET)
2858 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2861 if (addrlen < SIN6_LEN_RFC2133 || sap->sa_family != AF_INET6)
2863 #ifdef SMACK_IPV6_SECMARK_LABELING
2864 rsp = smack_ipv6host_label(sip);
2866 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2869 #ifdef SMACK_IPV6_PORT_LABELING
2870 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2878 * smack_flags_to_may - convert S_ to MAY_ values
2879 * @flags: the S_ value
2881 * Returns the equivalent MAY_ value
2883 static int smack_flags_to_may(int flags)
2887 if (flags & S_IRUGO)
2889 if (flags & S_IWUGO)
2891 if (flags & S_IXUGO)
2898 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2903 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2905 struct smack_known **blob = smack_msg_msg(msg);
2907 *blob = smk_of_current();
2912 * smack_of_ipc - the smack pointer for the ipc
2915 * Returns a pointer to the smack value
2917 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2919 struct smack_known **blob = smack_ipc(isp);
2925 * smack_ipc_alloc_security - Set the security blob for ipc
2930 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2932 struct smack_known **blob = smack_ipc(isp);
2934 *blob = smk_of_current();
2939 * smk_curacc_shm : check if current has access on shm
2941 * @access : access requested
2943 * Returns 0 if current has the requested access, error code otherwise
2945 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2947 struct smack_known *ssp = smack_of_ipc(isp);
2948 struct smk_audit_info ad;
2952 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2953 ad.a.u.ipc_id = isp->id;
2955 rc = smk_curacc(ssp, access, &ad);
2956 rc = smk_bu_current("shm", ssp, access, rc);
2961 * smack_shm_associate - Smack access check for shm
2963 * @shmflg: access requested
2965 * Returns 0 if current has the requested access, error code otherwise
2967 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2971 may = smack_flags_to_may(shmflg);
2972 return smk_curacc_shm(isp, may);
2976 * smack_shm_shmctl - Smack access check for shm
2978 * @cmd: what it wants to do
2980 * Returns 0 if current has the requested access, error code otherwise
2982 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2996 may = MAY_READWRITE;
3001 * System level information.
3007 return smk_curacc_shm(isp, may);
3011 * smack_shm_shmat - Smack access for shmat
3014 * @shmflg: access requested
3016 * Returns 0 if current has the requested access, error code otherwise
3018 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3023 may = smack_flags_to_may(shmflg);
3024 return smk_curacc_shm(ipc, may);
3028 * smk_curacc_sem : check if current has access on sem
3030 * @access : access requested
3032 * Returns 0 if current has the requested access, error code otherwise
3034 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3036 struct smack_known *ssp = smack_of_ipc(isp);
3037 struct smk_audit_info ad;
3041 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3042 ad.a.u.ipc_id = isp->id;
3044 rc = smk_curacc(ssp, access, &ad);
3045 rc = smk_bu_current("sem", ssp, access, rc);
3050 * smack_sem_associate - Smack access check for sem
3052 * @semflg: access requested
3054 * Returns 0 if current has the requested access, error code otherwise
3056 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3060 may = smack_flags_to_may(semflg);
3061 return smk_curacc_sem(isp, may);
3065 * smack_sem_shmctl - Smack access check for sem
3067 * @cmd: what it wants to do
3069 * Returns 0 if current has the requested access, error code otherwise
3071 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3090 may = MAY_READWRITE;
3095 * System level information
3102 return smk_curacc_sem(isp, may);
3106 * smack_sem_semop - Smack checks of semaphore operations
3112 * Treated as read and write in all cases.
3114 * Returns 0 if access is allowed, error code otherwise
3116 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3117 unsigned nsops, int alter)
3119 return smk_curacc_sem(isp, MAY_READWRITE);
3123 * smk_curacc_msq : helper to check if current has access on msq
3125 * @access : access requested
3127 * return 0 if current has access, error otherwise
3129 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3131 struct smack_known *msp = smack_of_ipc(isp);
3132 struct smk_audit_info ad;
3136 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3137 ad.a.u.ipc_id = isp->id;
3139 rc = smk_curacc(msp, access, &ad);
3140 rc = smk_bu_current("msq", msp, access, rc);
3145 * smack_msg_queue_associate - Smack access check for msg_queue
3147 * @msqflg: access requested
3149 * Returns 0 if current has the requested access, error code otherwise
3151 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3155 may = smack_flags_to_may(msqflg);
3156 return smk_curacc_msq(isp, may);
3160 * smack_msg_queue_msgctl - Smack access check for msg_queue
3162 * @cmd: what it wants to do
3164 * Returns 0 if current has the requested access, error code otherwise
3166 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3178 may = MAY_READWRITE;
3183 * System level information
3190 return smk_curacc_msq(isp, may);
3194 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3197 * @msqflg: access requested
3199 * Returns 0 if current has the requested access, error code otherwise
3201 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3206 may = smack_flags_to_may(msqflg);
3207 return smk_curacc_msq(isp, may);
3211 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3218 * Returns 0 if current has read and write access, error code otherwise
3220 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3221 struct task_struct *target, long type, int mode)
3223 return smk_curacc_msq(isp, MAY_READWRITE);
3227 * smack_ipc_permission - Smack access for ipc_permission()
3228 * @ipp: the object permissions
3229 * @flag: access requested
3231 * Returns 0 if current has read and write access, error code otherwise
3233 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3235 struct smack_known **blob = smack_ipc(ipp);
3236 struct smack_known *iskp = *blob;
3237 int may = smack_flags_to_may(flag);
3238 struct smk_audit_info ad;
3242 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3243 ad.a.u.ipc_id = ipp->id;
3245 rc = smk_curacc(iskp, may, &ad);
3246 rc = smk_bu_current("svipc", iskp, may, rc);
3251 * smack_ipc_getsecid - Extract smack security id
3252 * @ipp: the object permissions
3253 * @secid: where result will be saved
3255 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3257 struct smack_known **blob = smack_ipc(ipp);
3258 struct smack_known *iskp = *blob;
3260 *secid = iskp->smk_secid;
3264 * smack_d_instantiate - Make sure the blob is correct on an inode
3265 * @opt_dentry: dentry where inode will be attached
3266 * @inode: the object
3268 * Set the inode's security blob if it hasn't been done already.
3270 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3272 struct super_block *sbp;
3273 struct superblock_smack *sbsp;
3274 struct inode_smack *isp;
3275 struct smack_known *skp;
3276 struct smack_known *ckp = smk_of_current();
3277 struct smack_known *final;
3278 char trattr[TRANS_TRUE_SIZE];
3286 isp = smack_inode(inode);
3288 mutex_lock(&isp->smk_lock);
3290 * If the inode is already instantiated
3291 * take the quick way out
3293 if (isp->smk_flags & SMK_INODE_INSTANT)
3297 sbsp = sbp->s_security;
3299 * We're going to use the superblock default label
3300 * if there's no label on the file.
3302 final = sbsp->smk_default;
3305 * If this is the root inode the superblock
3306 * may be in the process of initialization.
3307 * If that is the case use the root value out
3308 * of the superblock.
3310 if (opt_dentry->d_parent == opt_dentry) {
3311 switch (sbp->s_magic) {
3312 case CGROUP_SUPER_MAGIC:
3313 case CGROUP2_SUPER_MAGIC:
3315 * The cgroup filesystem is never mounted,
3316 * so there's no opportunity to set the mount
3319 sbsp->smk_root = &smack_known_star;
3320 sbsp->smk_default = &smack_known_star;
3321 isp->smk_inode = sbsp->smk_root;
3325 * What about shmem/tmpfs anonymous files with dentry
3326 * obtained from d_alloc_pseudo()?
3328 isp->smk_inode = smk_of_current();
3331 isp->smk_inode = smk_of_current();
3335 * Socket access is controlled by the socket
3336 * structures associated with the task involved.
3338 isp->smk_inode = &smack_known_star;
3341 isp->smk_inode = sbsp->smk_root;
3344 isp->smk_flags |= SMK_INODE_INSTANT;
3349 * This is pretty hackish.
3350 * Casey says that we shouldn't have to do
3351 * file system specific code, but it does help
3352 * with keeping it simple.
3354 switch (sbp->s_magic) {
3356 case CGROUP_SUPER_MAGIC:
3357 case CGROUP2_SUPER_MAGIC:
3359 * Casey says that it's a little embarrassing
3360 * that the smack file system doesn't do
3361 * extended attributes.
3363 * Cgroupfs is special
3365 final = &smack_known_star;
3367 case DEVPTS_SUPER_MAGIC:
3369 * devpts seems content with the label of the task.
3370 * Programs that change smack have to treat the
3375 case PROC_SUPER_MAGIC:
3377 * Casey says procfs appears not to care.
3378 * The superblock default suffices.
3383 * Device labels should come from the filesystem,
3384 * but watch out, because they're volitile,
3385 * getting recreated on every reboot.
3387 final = &smack_known_star;
3389 * If a smack value has been set we want to use it,
3390 * but since tmpfs isn't giving us the opportunity
3391 * to set mount options simulate setting the
3392 * superblock default.
3397 * This isn't an understood special case.
3398 * Get the value from the xattr.
3402 * UNIX domain sockets use lower level socket data.
3404 if (S_ISSOCK(inode->i_mode)) {
3405 final = &smack_known_star;
3409 * No xattr support means, alas, no SMACK label.
3410 * Use the aforeapplied default.
3411 * It would be curious if the label of the task
3412 * does not match that assigned.
3414 if (!(inode->i_opflags & IOP_XATTR))
3417 * Get the dentry for xattr.
3419 dp = dget(opt_dentry);
3420 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3421 if (!IS_ERR_OR_NULL(skp))
3425 * Transmuting directory
3427 if (S_ISDIR(inode->i_mode)) {
3429 * If this is a new directory and the label was
3430 * transmuted when the inode was initialized
3431 * set the transmute attribute on the directory
3432 * and mark the inode.
3434 * If there is a transmute attribute on the
3435 * directory mark the inode.
3437 if (isp->smk_flags & SMK_INODE_CHANGED) {
3438 isp->smk_flags &= ~SMK_INODE_CHANGED;
3439 rc = __vfs_setxattr(dp, inode,
3440 XATTR_NAME_SMACKTRANSMUTE,
3441 TRANS_TRUE, TRANS_TRUE_SIZE,
3444 rc = __vfs_getxattr(dp, inode,
3445 XATTR_NAME_SMACKTRANSMUTE, trattr,
3447 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3448 TRANS_TRUE_SIZE) != 0)
3452 transflag = SMK_INODE_TRANSMUTE;
3455 * Don't let the exec or mmap label be "*" or "@".
3457 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3458 if (IS_ERR(skp) || skp == &smack_known_star ||
3459 skp == &smack_known_web)
3461 isp->smk_task = skp;
3463 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3464 if (IS_ERR(skp) || skp == &smack_known_star ||
3465 skp == &smack_known_web)
3467 isp->smk_mmap = skp;
3474 isp->smk_inode = ckp;
3476 isp->smk_inode = final;
3478 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3481 mutex_unlock(&isp->smk_lock);
3486 * smack_getprocattr - Smack process attribute access
3487 * @p: the object task
3488 * @name: the name of the attribute in /proc/.../attr
3489 * @value: where to put the result
3491 * Places a copy of the task Smack into value
3493 * Returns the length of the smack label or an error code
3495 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3497 struct smack_known *skp = smk_of_task_struct(p);
3501 if (strcmp(name, "current") != 0)
3504 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3514 * smack_setprocattr - Smack process attribute setting
3515 * @name: the name of the attribute in /proc/.../attr
3516 * @value: the value to set
3517 * @size: the size of the value
3519 * Sets the Smack value of the task. Only setting self
3520 * is permitted and only with privilege
3522 * Returns the length of the smack label or an error code
3524 static int smack_setprocattr(const char *name, void *value, size_t size)
3526 struct task_smack *tsp = smack_cred(current_cred());
3528 struct smack_known *skp;
3529 struct smack_known_list_elem *sklep;
3532 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3535 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3538 if (strcmp(name, "current") != 0)
3541 skp = smk_import_entry(value, size);
3543 return PTR_ERR(skp);
3546 * No process is ever allowed the web ("@") label
3547 * and the star ("*") label.
3549 if (skp == &smack_known_web || skp == &smack_known_star)
3552 if (!smack_privileged(CAP_MAC_ADMIN)) {
3554 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3555 if (sklep->smk_label == skp) {
3563 new = prepare_creds();
3567 tsp = smack_cred(new);
3568 tsp->smk_task = skp;
3570 * process can change its label only once
3572 smk_destroy_label_list(&tsp->smk_relabel);
3579 * smack_unix_stream_connect - Smack access on UDS
3581 * @other: the other sock
3584 * Return 0 if a subject with the smack of sock could access
3585 * an object with the smack of other, otherwise an error code
3587 static int smack_unix_stream_connect(struct sock *sock,
3588 struct sock *other, struct sock *newsk)
3590 struct smack_known *skp;
3591 struct smack_known *okp;
3592 struct socket_smack *ssp = sock->sk_security;
3593 struct socket_smack *osp = other->sk_security;
3594 struct socket_smack *nsp = newsk->sk_security;
3595 struct smk_audit_info ad;
3598 struct lsm_network_audit net;
3601 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3605 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3606 smk_ad_setfield_u_net_sk(&ad, other);
3608 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3609 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3613 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3614 rc = smk_bu_note("UDS connect", okp, skp,
3620 * Cross reference the peer labels for SO_PEERSEC.
3623 nsp->smk_packet = ssp->smk_out;
3624 ssp->smk_packet = osp->smk_out;
3631 * smack_unix_may_send - Smack access on UDS
3633 * @other: the other socket
3635 * Return 0 if a subject with the smack of sock could access
3636 * an object with the smack of other, otherwise an error code
3638 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3640 struct socket_smack *ssp = sock->sk->sk_security;
3641 struct socket_smack *osp = other->sk->sk_security;
3642 struct smk_audit_info ad;
3646 struct lsm_network_audit net;
3648 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3649 smk_ad_setfield_u_net_sk(&ad, other->sk);
3652 if (smack_privileged(CAP_MAC_OVERRIDE))
3655 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3656 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3661 * smack_socket_sendmsg - Smack check based on destination host
3664 * @size: the size of the message
3666 * Return 0 if the current subject can write to the destination host.
3667 * For IPv4 this is only a question if the destination is a single label host.
3668 * For IPv6 this is a check against the label of the port.
3670 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3673 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3674 #if IS_ENABLED(CONFIG_IPV6)
3675 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3677 #ifdef SMACK_IPV6_SECMARK_LABELING
3678 struct socket_smack *ssp = sock->sk->sk_security;
3679 struct smack_known *rsp;
3684 * Perfectly reasonable for this to be NULL
3689 switch (sock->sk->sk_family) {
3691 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3692 sip->sin_family != AF_INET)
3694 rc = smack_netlabel_send(sock->sk, sip);
3696 #if IS_ENABLED(CONFIG_IPV6)
3698 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3699 sap->sin6_family != AF_INET6)
3701 #ifdef SMACK_IPV6_SECMARK_LABELING
3702 rsp = smack_ipv6host_label(sap);
3704 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3707 #ifdef SMACK_IPV6_PORT_LABELING
3708 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3710 #endif /* IS_ENABLED(CONFIG_IPV6) */
3717 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3718 * @sap: netlabel secattr
3719 * @ssp: socket security information
3721 * Returns a pointer to a Smack label entry found on the label list.
3723 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3724 struct socket_smack *ssp)
3726 struct smack_known *skp;
3731 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3733 * Looks like a CIPSO packet.
3734 * If there are flags but no level netlabel isn't
3735 * behaving the way we expect it to.
3737 * Look it up in the label table
3738 * Without guidance regarding the smack value
3739 * for the packet fall back on the network
3743 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3744 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3747 * Compare the catsets. Use the netlbl APIs.
3749 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3750 if ((skp->smk_netlabel.flags &
3751 NETLBL_SECATTR_MLS_CAT) == 0)
3755 for (acat = -1, kcat = -1; acat == kcat; ) {
3756 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3758 kcat = netlbl_catmap_walk(
3759 skp->smk_netlabel.attr.mls.cat,
3761 if (acat < 0 || kcat < 0)
3774 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3775 return &smack_known_web;
3776 return &smack_known_star;
3778 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3780 * Looks like a fallback, which gives us a secid.
3782 return smack_from_secid(sap->attr.secid);
3784 * Without guidance regarding the smack value
3785 * for the packet fall back on the network
3788 return smack_net_ambient;
3791 #if IS_ENABLED(CONFIG_IPV6)
3792 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3796 int proto = -EINVAL;
3797 struct ipv6hdr _ipv6h;
3798 struct ipv6hdr *ip6;
3800 struct tcphdr _tcph, *th;
3801 struct udphdr _udph, *uh;
3802 struct dccp_hdr _dccph, *dh;
3806 offset = skb_network_offset(skb);
3807 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3810 sip->sin6_addr = ip6->saddr;
3812 nexthdr = ip6->nexthdr;
3813 offset += sizeof(_ipv6h);
3814 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3821 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3823 sip->sin6_port = th->source;
3826 case IPPROTO_UDPLITE:
3827 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3829 sip->sin6_port = uh->source;
3832 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3834 sip->sin6_port = dh->dccph_sport;
3839 #endif /* CONFIG_IPV6 */
3842 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3846 * Returns 0 if the packet should be delivered, an error code otherwise
3848 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3850 struct netlbl_lsm_secattr secattr;
3851 struct socket_smack *ssp = sk->sk_security;
3852 struct smack_known *skp = NULL;
3854 struct smk_audit_info ad;
3855 u16 family = sk->sk_family;
3857 struct lsm_network_audit net;
3859 #if IS_ENABLED(CONFIG_IPV6)
3860 struct sockaddr_in6 sadd;
3863 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3865 #endif /* CONFIG_IPV6 */
3869 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3871 * If there is a secmark use it rather than the CIPSO label.
3872 * If there is no secmark fall back to CIPSO.
3873 * The secmark is assumed to reflect policy better.
3875 if (skb && skb->secmark != 0) {
3876 skp = smack_from_secid(skb->secmark);
3879 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3881 * Translate what netlabel gave us.
3883 netlbl_secattr_init(&secattr);
3885 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3887 skp = smack_from_secattr(&secattr, ssp);
3889 skp = smack_net_ambient;
3891 netlbl_secattr_destroy(&secattr);
3893 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3897 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3898 ad.a.u.net->family = family;
3899 ad.a.u.net->netif = skb->skb_iif;
3900 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3903 * Receiving a packet requires that the other end
3904 * be able to write here. Read access is not required.
3905 * This is the simplist possible security model
3908 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3909 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3912 netlbl_skbuff_err(skb, family, rc, 0);
3914 #if IS_ENABLED(CONFIG_IPV6)
3916 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3917 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3918 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3920 #ifdef SMACK_IPV6_SECMARK_LABELING
3921 if (skb && skb->secmark != 0)
3922 skp = smack_from_secid(skb->secmark);
3923 else if (smk_ipv6_localhost(&sadd))
3926 skp = smack_ipv6host_label(&sadd);
3928 skp = smack_net_ambient;
3930 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3931 ad.a.u.net->family = family;
3932 ad.a.u.net->netif = skb->skb_iif;
3933 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3934 #endif /* CONFIG_AUDIT */
3935 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3936 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3938 #endif /* SMACK_IPV6_SECMARK_LABELING */
3939 #ifdef SMACK_IPV6_PORT_LABELING
3940 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3941 #endif /* SMACK_IPV6_PORT_LABELING */
3943 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3944 ICMPV6_ADM_PROHIBITED, 0);
3946 #endif /* CONFIG_IPV6 */
3953 * smack_socket_getpeersec_stream - pull in packet label
3955 * @optval: user's destination
3956 * @optlen: size thereof
3959 * returns zero on success, an error code otherwise
3961 static int smack_socket_getpeersec_stream(struct socket *sock,
3962 char __user *optval,
3963 int __user *optlen, unsigned len)
3965 struct socket_smack *ssp;
3970 ssp = sock->sk->sk_security;
3971 if (ssp->smk_packet != NULL) {
3972 rcp = ssp->smk_packet->smk_known;
3973 slen = strlen(rcp) + 1;
3978 else if (copy_to_user(optval, rcp, slen) != 0)
3981 if (put_user(slen, optlen) != 0)
3989 * smack_socket_getpeersec_dgram - pull in packet label
3990 * @sock: the peer socket
3992 * @secid: pointer to where to put the secid of the packet
3994 * Sets the netlabel socket state on sk from parent
3996 static int smack_socket_getpeersec_dgram(struct socket *sock,
3997 struct sk_buff *skb, u32 *secid)
4000 struct netlbl_lsm_secattr secattr;
4001 struct socket_smack *ssp = NULL;
4002 struct smack_known *skp;
4003 int family = PF_UNSPEC;
4004 u32 s = 0; /* 0 is the invalid secid */
4008 if (skb->protocol == htons(ETH_P_IP))
4010 #if IS_ENABLED(CONFIG_IPV6)
4011 else if (skb->protocol == htons(ETH_P_IPV6))
4013 #endif /* CONFIG_IPV6 */
4015 if (family == PF_UNSPEC && sock != NULL)
4016 family = sock->sk->sk_family;
4020 ssp = sock->sk->sk_security;
4021 s = ssp->smk_out->smk_secid;
4024 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4030 * Translate what netlabel gave us.
4032 if (sock != NULL && sock->sk != NULL)
4033 ssp = sock->sk->sk_security;
4034 netlbl_secattr_init(&secattr);
4035 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4037 skp = smack_from_secattr(&secattr, ssp);
4040 netlbl_secattr_destroy(&secattr);
4043 #ifdef SMACK_IPV6_SECMARK_LABELING
4055 * smack_sock_graft - Initialize a newly created socket with an existing sock
4057 * @parent: parent socket
4059 * Set the smk_{in,out} state of an existing sock based on the process that
4060 * is creating the new socket.
4062 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4064 struct socket_smack *ssp;
4065 struct smack_known *skp = smk_of_current();
4068 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4071 ssp = sk->sk_security;
4074 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4078 * smack_inet_conn_request - Smack access check on connect
4079 * @sk: socket involved
4083 * Returns 0 if a task with the packet label could write to
4084 * the socket, otherwise an error code
4086 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4087 struct request_sock *req)
4089 u16 family = sk->sk_family;
4090 struct smack_known *skp;
4091 struct socket_smack *ssp = sk->sk_security;
4092 struct netlbl_lsm_secattr secattr;
4093 struct sockaddr_in addr;
4095 struct smack_known *hskp;
4097 struct smk_audit_info ad;
4099 struct lsm_network_audit net;
4102 #if IS_ENABLED(CONFIG_IPV6)
4103 if (family == PF_INET6) {
4105 * Handle mapped IPv4 packets arriving
4106 * via IPv6 sockets. Don't set up netlabel
4107 * processing on IPv6.
4109 if (skb->protocol == htons(ETH_P_IP))
4114 #endif /* CONFIG_IPV6 */
4116 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4118 * If there is a secmark use it rather than the CIPSO label.
4119 * If there is no secmark fall back to CIPSO.
4120 * The secmark is assumed to reflect policy better.
4122 if (skb && skb->secmark != 0) {
4123 skp = smack_from_secid(skb->secmark);
4126 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4128 netlbl_secattr_init(&secattr);
4129 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4131 skp = smack_from_secattr(&secattr, ssp);
4133 skp = &smack_known_huh;
4134 netlbl_secattr_destroy(&secattr);
4136 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4141 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4142 ad.a.u.net->family = family;
4143 ad.a.u.net->netif = skb->skb_iif;
4144 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4147 * Receiving a packet requires that the other end be able to write
4148 * here. Read access is not required.
4150 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4151 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4156 * Save the peer's label in the request_sock so we can later setup
4157 * smk_packet in the child socket so that SO_PEERCRED can report it.
4159 req->peer_secid = skp->smk_secid;
4162 * We need to decide if we want to label the incoming connection here
4163 * if we do we only need to label the request_sock and the stack will
4164 * propagate the wire-label to the sock when it is created.
4167 addr.sin_addr.s_addr = hdr->saddr;
4169 hskp = smack_ipv4host_label(&addr);
4173 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4175 netlbl_req_delattr(req);
4181 * smack_inet_csk_clone - Copy the connection information to the new socket
4182 * @sk: the new socket
4183 * @req: the connection's request_sock
4185 * Transfer the connection's peer label to the newly created socket.
4187 static void smack_inet_csk_clone(struct sock *sk,
4188 const struct request_sock *req)
4190 struct socket_smack *ssp = sk->sk_security;
4191 struct smack_known *skp;
4193 if (req->peer_secid != 0) {
4194 skp = smack_from_secid(req->peer_secid);
4195 ssp->smk_packet = skp;
4197 ssp->smk_packet = NULL;
4201 * Key management security hooks
4203 * Casey has not tested key support very heavily.
4204 * The permission check is most likely too restrictive.
4205 * If you care about keys please have a look.
4210 * smack_key_alloc - Set the key security blob
4212 * @cred: the credentials to use
4215 * No allocation required
4219 static int smack_key_alloc(struct key *key, const struct cred *cred,
4220 unsigned long flags)
4222 struct smack_known *skp = smk_of_task(smack_cred(cred));
4224 key->security = skp;
4229 * smack_key_free - Clear the key security blob
4232 * Clear the blob pointer
4234 static void smack_key_free(struct key *key)
4236 key->security = NULL;
4240 * smack_key_permission - Smack access on a key
4241 * @key_ref: gets to the object
4242 * @cred: the credentials to use
4243 * @perm: requested key permissions
4245 * Return 0 if the task has read and write to the object,
4246 * an error code otherwise
4248 static int smack_key_permission(key_ref_t key_ref,
4249 const struct cred *cred, unsigned perm)
4252 struct smk_audit_info ad;
4253 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4258 * Validate requested permissions
4260 if (perm & ~KEY_NEED_ALL)
4263 keyp = key_ref_to_ptr(key_ref);
4267 * If the key hasn't been initialized give it access so that
4270 if (keyp->security == NULL)
4273 * This should not occur
4278 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4282 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4283 ad.a.u.key_struct.key = keyp->serial;
4284 ad.a.u.key_struct.key_desc = keyp->description;
4286 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4287 request |= MAY_READ;
4288 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4289 request |= MAY_WRITE;
4290 rc = smk_access(tkp, keyp->security, request, &ad);
4291 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4296 * smack_key_getsecurity - Smack label tagging the key
4297 * @key points to the key to be queried
4298 * @_buffer points to a pointer that should be set to point to the
4299 * resulting string (if no label or an error occurs).
4300 * Return the length of the string (including terminating NUL) or -ve if
4302 * May also return 0 (and a NULL buffer pointer) if there is no label.
4304 static int smack_key_getsecurity(struct key *key, char **_buffer)
4306 struct smack_known *skp = key->security;
4310 if (key->security == NULL) {
4315 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4318 length = strlen(copy) + 1;
4324 #endif /* CONFIG_KEYS */
4329 * Audit requires a unique representation of each Smack specific
4330 * rule. This unique representation is used to distinguish the
4331 * object to be audited from remaining kernel objects and also
4332 * works as a glue between the audit hooks.
4334 * Since repository entries are added but never deleted, we'll use
4335 * the smack_known label address related to the given audit rule as
4336 * the needed unique representation. This also better fits the smack
4337 * model where nearly everything is a label.
4342 * smack_audit_rule_init - Initialize a smack audit rule
4343 * @field: audit rule fields given from user-space (audit.h)
4344 * @op: required testing operator (=, !=, >, <, ...)
4345 * @rulestr: smack label to be audited
4346 * @vrule: pointer to save our own audit rule representation
4348 * Prepare to audit cases where (@field @op @rulestr) is true.
4349 * The label to be audited is created if necessay.
4351 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4353 struct smack_known *skp;
4354 char **rule = (char **)vrule;
4357 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4360 if (op != Audit_equal && op != Audit_not_equal)
4363 skp = smk_import_entry(rulestr, 0);
4365 return PTR_ERR(skp);
4367 *rule = skp->smk_known;
4373 * smack_audit_rule_known - Distinguish Smack audit rules
4374 * @krule: rule of interest, in Audit kernel representation format
4376 * This is used to filter Smack rules from remaining Audit ones.
4377 * If it's proved that this rule belongs to us, the
4378 * audit_rule_match hook will be called to do the final judgement.
4380 static int smack_audit_rule_known(struct audit_krule *krule)
4382 struct audit_field *f;
4385 for (i = 0; i < krule->field_count; i++) {
4386 f = &krule->fields[i];
4388 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4396 * smack_audit_rule_match - Audit given object ?
4397 * @secid: security id for identifying the object to test
4398 * @field: audit rule flags given from user-space
4399 * @op: required testing operator
4400 * @vrule: smack internal rule presentation
4402 * The core Audit hook. It's used to take the decision of
4403 * whether to audit or not to audit a given object.
4405 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4407 struct smack_known *skp;
4410 if (unlikely(!rule)) {
4411 WARN_ONCE(1, "Smack: missing rule\n");
4415 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4418 skp = smack_from_secid(secid);
4421 * No need to do string comparisons. If a match occurs,
4422 * both pointers will point to the same smack_known
4425 if (op == Audit_equal)
4426 return (rule == skp->smk_known);
4427 if (op == Audit_not_equal)
4428 return (rule != skp->smk_known);
4434 * There is no need for a smack_audit_rule_free hook.
4435 * No memory was allocated.
4438 #endif /* CONFIG_AUDIT */
4441 * smack_ismaclabel - check if xattr @name references a smack MAC label
4442 * @name: Full xattr name to check.
4444 static int smack_ismaclabel(const char *name)
4446 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4451 * smack_secid_to_secctx - return the smack label for a secid
4452 * @secid: incoming integer
4453 * @secdata: destination
4454 * @seclen: how long it is
4456 * Exists for networking code.
4458 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4460 struct smack_known *skp = smack_from_secid(secid);
4463 *secdata = skp->smk_known;
4464 *seclen = strlen(skp->smk_known);
4469 * smack_secctx_to_secid - return the secid for a smack label
4470 * @secdata: smack label
4471 * @seclen: how long result is
4472 * @secid: outgoing integer
4474 * Exists for audit and networking code.
4476 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4478 struct smack_known *skp = smk_find_entry(secdata);
4481 *secid = skp->smk_secid;
4488 * There used to be a smack_release_secctx hook
4489 * that did nothing back when hooks were in a vector.
4490 * Now that there's a list such a hook adds cost.
4493 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4495 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4498 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4500 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4503 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4505 struct smack_known *skp = smk_of_inode(inode);
4507 *ctx = skp->smk_known;
4508 *ctxlen = strlen(skp->smk_known);
4512 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4515 struct task_smack *tsp;
4516 struct smack_known *skp;
4517 struct inode_smack *isp;
4518 struct cred *new_creds = *new;
4520 if (new_creds == NULL) {
4521 new_creds = prepare_creds();
4522 if (new_creds == NULL)
4526 tsp = smack_cred(new_creds);
4529 * Get label from overlay inode and set it in create_sid
4531 isp = smack_inode(d_inode(dentry->d_parent));
4532 skp = isp->smk_inode;
4533 tsp->smk_task = skp;
4538 static int smack_inode_copy_up_xattr(const char *name)
4541 * Return 1 if this is the smack access Smack attribute.
4543 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4549 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4551 const struct cred *old,
4554 struct task_smack *otsp = smack_cred(old);
4555 struct task_smack *ntsp = smack_cred(new);
4556 struct inode_smack *isp;
4560 * Use the process credential unless all of
4561 * the transmuting criteria are met
4563 ntsp->smk_task = otsp->smk_task;
4566 * the attribute of the containing directory
4568 isp = smack_inode(d_inode(dentry->d_parent));
4570 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4572 may = smk_access_entry(otsp->smk_task->smk_known,
4573 isp->smk_inode->smk_known,
4574 &otsp->smk_task->smk_rules);
4578 * If the directory is transmuting and the rule
4579 * providing access is transmuting use the containing
4580 * directory label instead of the process label.
4582 if (may > 0 && (may & MAY_TRANSMUTE))
4583 ntsp->smk_task = isp->smk_inode;
4588 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4589 .lbs_cred = sizeof(struct task_smack),
4590 .lbs_file = sizeof(struct smack_known *),
4591 .lbs_inode = sizeof(struct inode_smack),
4592 .lbs_ipc = sizeof(struct smack_known *),
4593 .lbs_msg_msg = sizeof(struct smack_known *),
4596 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4597 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4598 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4599 LSM_HOOK_INIT(syslog, smack_syslog),
4601 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4602 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4604 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4605 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4606 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4607 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4608 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4609 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4611 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4613 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4614 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4615 LSM_HOOK_INIT(inode_link, smack_inode_link),
4616 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4617 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4618 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4619 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4620 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4621 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4622 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4623 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4624 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4625 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4626 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4627 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4628 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4629 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4631 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4632 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4633 LSM_HOOK_INIT(file_lock, smack_file_lock),
4634 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4635 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4636 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4637 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4638 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4639 LSM_HOOK_INIT(file_receive, smack_file_receive),
4641 LSM_HOOK_INIT(file_open, smack_file_open),
4643 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4644 LSM_HOOK_INIT(cred_free, smack_cred_free),
4645 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4646 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4647 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4648 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4649 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4650 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4651 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4652 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4653 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4654 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4655 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4656 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4657 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4658 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4659 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4660 LSM_HOOK_INIT(task_kill, smack_task_kill),
4661 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4663 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4664 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4666 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4668 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4669 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4670 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4671 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4672 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4674 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4675 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4676 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4677 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4679 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4680 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4681 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4682 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4684 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4686 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4687 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4689 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4690 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4692 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4693 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4694 #ifdef SMACK_IPV6_PORT_LABELING
4695 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4697 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4698 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4699 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4700 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4701 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4702 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4703 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4704 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4705 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4706 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4708 /* key management security hooks */
4710 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4711 LSM_HOOK_INIT(key_free, smack_key_free),
4712 LSM_HOOK_INIT(key_permission, smack_key_permission),
4713 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4714 #endif /* CONFIG_KEYS */
4718 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4719 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4720 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4721 #endif /* CONFIG_AUDIT */
4723 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4724 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4725 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4726 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4727 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4728 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4729 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4730 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4731 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4735 static __init void init_smack_known_list(void)
4738 * Initialize rule list locks
4740 mutex_init(&smack_known_huh.smk_rules_lock);
4741 mutex_init(&smack_known_hat.smk_rules_lock);
4742 mutex_init(&smack_known_floor.smk_rules_lock);
4743 mutex_init(&smack_known_star.smk_rules_lock);
4744 mutex_init(&smack_known_web.smk_rules_lock);
4746 * Initialize rule lists
4748 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4749 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4750 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4751 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4752 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4754 * Create the known labels list
4756 smk_insert_entry(&smack_known_huh);
4757 smk_insert_entry(&smack_known_hat);
4758 smk_insert_entry(&smack_known_star);
4759 smk_insert_entry(&smack_known_floor);
4760 smk_insert_entry(&smack_known_web);
4764 * smack_init - initialize the smack system
4768 static __init int smack_init(void)
4770 struct cred *cred = (struct cred *) current->cred;
4771 struct task_smack *tsp;
4773 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4774 if (!smack_inode_cache)
4777 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4778 if (!smack_rule_cache) {
4779 kmem_cache_destroy(smack_inode_cache);
4784 * Set the security state for the initial task.
4786 tsp = smack_cred(cred);
4787 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4792 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4795 pr_info("Smack: Initializing.\n");
4796 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4797 pr_info("Smack: Netfilter enabled.\n");
4799 #ifdef SMACK_IPV6_PORT_LABELING
4800 pr_info("Smack: IPv6 port labeling enabled.\n");
4802 #ifdef SMACK_IPV6_SECMARK_LABELING
4803 pr_info("Smack: IPv6 Netfilter enabled.\n");
4806 /* initialize the smack_known_list */
4807 init_smack_known_list();
4813 * Smack requires early initialization in order to label
4814 * all processes and objects when they are created.
4816 DEFINE_LSM(smack) = {
4818 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4819 .blobs = &smack_blob_sizes,