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>
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 #ifdef SMACK_IPV6_PORT_LABELING
56 DEFINE_MUTEX(smack_ipv6_lock);
57 static LIST_HEAD(smk_ipv6_port_list);
59 static struct kmem_cache *smack_inode_cache;
62 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
67 } smk_mount_opts[] = {
68 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
72 static int match_opt_prefix(char *s, int l, char **arg)
76 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
77 size_t len = smk_mount_opts[i].len;
78 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
80 if (len == l || s[len] != '=')
83 return smk_mount_opts[i].opt;
88 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
89 static char *smk_bu_mess[] = {
90 "Bringup Error", /* Unused */
91 "Bringup", /* SMACK_BRINGUP_ALLOW */
92 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
93 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
96 static void smk_bu_mode(int mode, char *s)
102 if (mode & MAY_WRITE)
106 if (mode & MAY_APPEND)
108 if (mode & MAY_TRANSMUTE)
118 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
119 static int smk_bu_note(char *note, struct smack_known *sskp,
120 struct smack_known *oskp, int mode, int rc)
122 char acc[SMK_NUM_ACCESS_TYPE + 1];
126 if (rc > SMACK_UNCONFINED_OBJECT)
129 smk_bu_mode(mode, acc);
130 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
131 sskp->smk_known, oskp->smk_known, acc, note);
135 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
138 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
139 static int smk_bu_current(char *note, struct smack_known *oskp,
142 struct task_smack *tsp = smack_cred(current_cred());
143 char acc[SMK_NUM_ACCESS_TYPE + 1];
147 if (rc > SMACK_UNCONFINED_OBJECT)
150 smk_bu_mode(mode, acc);
151 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
152 tsp->smk_task->smk_known, oskp->smk_known,
153 acc, current->comm, note);
157 #define smk_bu_current(note, oskp, mode, RC) (RC)
160 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
161 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
163 struct task_smack *tsp = smack_cred(current_cred());
164 struct smack_known *smk_task = smk_of_task_struct(otp);
165 char acc[SMK_NUM_ACCESS_TYPE + 1];
169 if (rc > SMACK_UNCONFINED_OBJECT)
172 smk_bu_mode(mode, acc);
173 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
174 tsp->smk_task->smk_known, smk_task->smk_known, acc,
175 current->comm, otp->comm);
179 #define smk_bu_task(otp, mode, RC) (RC)
182 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
183 static int smk_bu_inode(struct inode *inode, int mode, int rc)
185 struct task_smack *tsp = smack_cred(current_cred());
186 struct inode_smack *isp = smack_inode(inode);
187 char acc[SMK_NUM_ACCESS_TYPE + 1];
189 if (isp->smk_flags & SMK_INODE_IMPURE)
190 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
191 inode->i_sb->s_id, inode->i_ino, current->comm);
195 if (rc > SMACK_UNCONFINED_OBJECT)
197 if (rc == SMACK_UNCONFINED_SUBJECT &&
198 (mode & (MAY_WRITE | MAY_APPEND)))
199 isp->smk_flags |= SMK_INODE_IMPURE;
201 smk_bu_mode(mode, acc);
203 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
204 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
205 inode->i_sb->s_id, inode->i_ino, current->comm);
209 #define smk_bu_inode(inode, mode, RC) (RC)
212 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
213 static int smk_bu_file(struct file *file, int mode, int rc)
215 struct task_smack *tsp = smack_cred(current_cred());
216 struct smack_known *sskp = tsp->smk_task;
217 struct inode *inode = file_inode(file);
218 struct inode_smack *isp = smack_inode(inode);
219 char acc[SMK_NUM_ACCESS_TYPE + 1];
221 if (isp->smk_flags & SMK_INODE_IMPURE)
222 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
223 inode->i_sb->s_id, inode->i_ino, current->comm);
227 if (rc > SMACK_UNCONFINED_OBJECT)
230 smk_bu_mode(mode, acc);
231 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
232 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
233 inode->i_sb->s_id, inode->i_ino, file,
238 #define smk_bu_file(file, mode, RC) (RC)
241 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
242 static int smk_bu_credfile(const struct cred *cred, struct file *file,
245 struct task_smack *tsp = smack_cred(cred);
246 struct smack_known *sskp = tsp->smk_task;
247 struct inode *inode = file_inode(file);
248 struct inode_smack *isp = smack_inode(inode);
249 char acc[SMK_NUM_ACCESS_TYPE + 1];
251 if (isp->smk_flags & SMK_INODE_IMPURE)
252 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
253 inode->i_sb->s_id, inode->i_ino, current->comm);
257 if (rc > SMACK_UNCONFINED_OBJECT)
260 smk_bu_mode(mode, acc);
261 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
262 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
263 inode->i_sb->s_id, inode->i_ino, file,
268 #define smk_bu_credfile(cred, file, mode, RC) (RC)
272 * smk_fetch - Fetch the smack label from a file.
273 * @name: type of the label (attribute)
274 * @ip: a pointer to the inode
275 * @dp: a pointer to the dentry
277 * Returns a pointer to the master list entry for the Smack label,
278 * NULL if there was no label to fetch, or an error code.
280 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
285 struct smack_known *skp = NULL;
287 if (!(ip->i_opflags & IOP_XATTR))
288 return ERR_PTR(-EOPNOTSUPP);
290 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
292 return ERR_PTR(-ENOMEM);
294 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
300 skp = smk_import_entry(buffer, rc);
308 * init_inode_smack - initialize an inode security blob
309 * @isp: the blob to initialize
310 * @skp: a pointer to the Smack label entry to use in the blob
313 static void init_inode_smack(struct inode *inode, struct smack_known *skp)
315 struct inode_smack *isp = smack_inode(inode);
317 isp->smk_inode = skp;
319 mutex_init(&isp->smk_lock);
323 * init_task_smack - initialize a task security blob
324 * @tsp: blob to initialize
325 * @task: a pointer to the Smack label for the running task
326 * @forked: a pointer to the Smack label for the forked task
329 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
330 struct smack_known *forked)
332 tsp->smk_task = task;
333 tsp->smk_forked = forked;
334 INIT_LIST_HEAD(&tsp->smk_rules);
335 INIT_LIST_HEAD(&tsp->smk_relabel);
336 mutex_init(&tsp->smk_rules_lock);
340 * smk_copy_rules - copy a rule set
341 * @nhead: new rules header pointer
342 * @ohead: old rules header pointer
343 * @gfp: type of the memory for the allocation
345 * Returns 0 on success, -ENOMEM on error
347 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
350 struct smack_rule *nrp;
351 struct smack_rule *orp;
354 list_for_each_entry_rcu(orp, ohead, list) {
355 nrp = kzalloc(sizeof(struct smack_rule), gfp);
361 list_add_rcu(&nrp->list, nhead);
367 * smk_copy_relabel - copy smk_relabel labels list
368 * @nhead: new rules header pointer
369 * @ohead: old rules header pointer
370 * @gfp: type of the memory for the allocation
372 * Returns 0 on success, -ENOMEM on error
374 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
377 struct smack_known_list_elem *nklep;
378 struct smack_known_list_elem *oklep;
380 list_for_each_entry(oklep, ohead, list) {
381 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
383 smk_destroy_label_list(nhead);
386 nklep->smk_label = oklep->smk_label;
387 list_add(&nklep->list, nhead);
394 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
395 * @mode - input mode in form of PTRACE_MODE_*
397 * Returns a converted MAY_* mode usable by smack rules
399 static inline unsigned int smk_ptrace_mode(unsigned int mode)
401 if (mode & PTRACE_MODE_ATTACH)
402 return MAY_READWRITE;
403 if (mode & PTRACE_MODE_READ)
410 * smk_ptrace_rule_check - helper for ptrace access
411 * @tracer: tracer process
412 * @tracee_known: label entry of the process that's about to be traced
413 * @mode: ptrace attachment mode (PTRACE_MODE_*)
414 * @func: name of the function that called us, used for audit
416 * Returns 0 on access granted, -error on error
418 static int smk_ptrace_rule_check(struct task_struct *tracer,
419 struct smack_known *tracee_known,
420 unsigned int mode, const char *func)
423 struct smk_audit_info ad, *saip = NULL;
424 struct task_smack *tsp;
425 struct smack_known *tracer_known;
426 const struct cred *tracercred;
428 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
429 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
430 smk_ad_setfield_u_tsk(&ad, tracer);
435 tracercred = __task_cred(tracer);
436 tsp = smack_cred(tracercred);
437 tracer_known = smk_of_task(tsp);
439 if ((mode & PTRACE_MODE_ATTACH) &&
440 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
441 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
442 if (tracer_known->smk_known == tracee_known->smk_known)
444 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
446 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
452 smack_log(tracer_known->smk_known,
453 tracee_known->smk_known,
460 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
461 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
469 * We he, that is fun!
473 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
474 * @ctp: child task pointer
475 * @mode: ptrace attachment mode (PTRACE_MODE_*)
477 * Returns 0 if access is OK, an error code otherwise
479 * Do the capability checks.
481 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
483 struct smack_known *skp;
485 skp = smk_of_task_struct(ctp);
487 return smk_ptrace_rule_check(current, skp, mode, __func__);
491 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
492 * @ptp: parent task pointer
494 * Returns 0 if access is OK, an error code otherwise
496 * Do the capability checks, and require PTRACE_MODE_ATTACH.
498 static int smack_ptrace_traceme(struct task_struct *ptp)
501 struct smack_known *skp;
503 skp = smk_of_task(smack_cred(current_cred()));
505 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
510 * smack_syslog - Smack approval on syslog
511 * @type: message type
513 * Returns 0 on success, error code otherwise.
515 static int smack_syslog(int typefrom_file)
518 struct smack_known *skp = smk_of_current();
520 if (smack_privileged(CAP_MAC_OVERRIDE))
523 if (smack_syslog_label != NULL && smack_syslog_label != skp)
535 * smack_sb_alloc_security - allocate a superblock blob
536 * @sb: the superblock getting the blob
538 * Returns 0 on success or -ENOMEM on error.
540 static int smack_sb_alloc_security(struct super_block *sb)
542 struct superblock_smack *sbsp;
544 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
549 sbsp->smk_root = &smack_known_floor;
550 sbsp->smk_default = &smack_known_floor;
551 sbsp->smk_floor = &smack_known_floor;
552 sbsp->smk_hat = &smack_known_hat;
554 * SMK_SB_INITIALIZED will be zero from kzalloc.
556 sb->s_security = sbsp;
562 * smack_sb_free_security - free a superblock blob
563 * @sb: the superblock getting the blob
566 static void smack_sb_free_security(struct super_block *sb)
568 kfree(sb->s_security);
569 sb->s_security = NULL;
572 struct smack_mnt_opts {
573 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
576 static void smack_free_mnt_opts(void *mnt_opts)
578 struct smack_mnt_opts *opts = mnt_opts;
579 kfree(opts->fsdefault);
580 kfree(opts->fsfloor);
583 kfree(opts->fstransmute);
587 static int smack_add_opt(int token, const char *s, void **mnt_opts)
589 struct smack_mnt_opts *opts = *mnt_opts;
592 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
621 case Opt_fstransmute:
622 if (opts->fstransmute)
624 opts->fstransmute = s;
630 pr_warn("Smack: duplicate mount options\n");
634 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
636 char *from = options, *to = options;
640 char *next = strchr(from, ',');
649 token = match_opt_prefix(from, len, &arg);
650 if (token != Opt_error) {
651 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
652 rc = smack_add_opt(token, arg, mnt_opts);
656 smack_free_mnt_opts(*mnt_opts);
661 if (!first) { // copy with preceding comma
666 memmove(to, from, len);
679 * smack_set_mnt_opts - set Smack specific mount options
680 * @sb: the file system superblock
681 * @opts: Smack mount options
682 * @kern_flags: mount option from kernel space or user space
683 * @set_kern_flags: where to store converted mount opts
685 * Returns 0 on success, an error code on failure
687 * Allow filesystems with binary mount data to explicitly set Smack mount
690 static int smack_set_mnt_opts(struct super_block *sb,
692 unsigned long kern_flags,
693 unsigned long *set_kern_flags)
695 struct dentry *root = sb->s_root;
696 struct inode *inode = d_backing_inode(root);
697 struct superblock_smack *sp = sb->s_security;
698 struct inode_smack *isp;
699 struct smack_known *skp;
700 struct smack_mnt_opts *opts = mnt_opts;
701 bool transmute = false;
703 if (sp->smk_flags & SMK_SB_INITIALIZED)
706 if (inode->i_security == NULL) {
707 int rc = lsm_inode_alloc(inode);
713 if (!smack_privileged(CAP_MAC_ADMIN)) {
715 * Unprivileged mounts don't get to specify Smack values.
720 * Unprivileged mounts get root and default from the caller.
722 skp = smk_of_current();
724 sp->smk_default = skp;
726 * For a handful of fs types with no user-controlled
727 * backing store it's okay to trust security labels
728 * in the filesystem. The rest are untrusted.
730 if (sb->s_user_ns != &init_user_ns &&
731 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
732 sb->s_magic != RAMFS_MAGIC) {
734 sp->smk_flags |= SMK_SB_UNTRUSTED;
738 sp->smk_flags |= SMK_SB_INITIALIZED;
741 if (opts->fsdefault) {
742 skp = smk_import_entry(opts->fsdefault, 0);
745 sp->smk_default = skp;
748 skp = smk_import_entry(opts->fsfloor, 0);
754 skp = smk_import_entry(opts->fshat, 0);
760 skp = smk_import_entry(opts->fsroot, 0);
765 if (opts->fstransmute) {
766 skp = smk_import_entry(opts->fstransmute, 0);
775 * Initialize the root inode.
777 init_inode_smack(inode, sp->smk_root);
780 isp = smack_inode(inode);
781 isp->smk_flags |= SMK_INODE_TRANSMUTE;
788 * smack_sb_statfs - Smack check on statfs
789 * @dentry: identifies the file system in question
791 * Returns 0 if current can read the floor of the filesystem,
792 * and error code otherwise
794 static int smack_sb_statfs(struct dentry *dentry)
796 struct superblock_smack *sbp = dentry->d_sb->s_security;
798 struct smk_audit_info ad;
800 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
801 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
803 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
804 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
813 * smack_bprm_set_creds - set creds for exec
814 * @bprm: the exec information
816 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
818 static int smack_bprm_set_creds(struct linux_binprm *bprm)
820 struct inode *inode = file_inode(bprm->file);
821 struct task_smack *bsp = smack_cred(bprm->cred);
822 struct inode_smack *isp;
823 struct superblock_smack *sbsp;
826 if (bprm->called_set_creds)
829 isp = smack_inode(inode);
830 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
833 sbsp = inode->i_sb->s_security;
834 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
835 isp->smk_task != sbsp->smk_root)
838 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
839 struct task_struct *tracer;
843 tracer = ptrace_parent(current);
844 if (likely(tracer != NULL))
845 rc = smk_ptrace_rule_check(tracer,
853 } else if (bprm->unsafe)
856 bsp->smk_task = isp->smk_task;
857 bprm->per_clear |= PER_CLEAR_ON_SETID;
859 /* Decide if this is a secure exec. */
860 if (bsp->smk_task != bsp->smk_forked)
861 bprm->secureexec = 1;
871 * smack_inode_alloc_security - allocate an inode blob
872 * @inode: the inode in need of a blob
874 * Returns 0 if it gets a blob, -ENOMEM otherwise
876 static int smack_inode_alloc_security(struct inode *inode)
878 struct smack_known *skp = smk_of_current();
880 init_inode_smack(inode, skp);
885 * smack_inode_init_security - copy out the smack from an inode
886 * @inode: the newly created inode
887 * @dir: containing directory object
889 * @name: where to put the attribute name
890 * @value: where to put the attribute value
891 * @len: where to put the length of the attribute
893 * Returns 0 if it all works out, -ENOMEM if there's no memory
895 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
896 const struct qstr *qstr, const char **name,
897 void **value, size_t *len)
899 struct inode_smack *issp = smack_inode(inode);
900 struct smack_known *skp = smk_of_current();
901 struct smack_known *isp = smk_of_inode(inode);
902 struct smack_known *dsp = smk_of_inode(dir);
906 *name = XATTR_SMACK_SUFFIX;
910 may = smk_access_entry(skp->smk_known, dsp->smk_known,
915 * If the access rule allows transmutation and
916 * the directory requests transmutation then
917 * by all means transmute.
918 * Mark the inode as changed.
920 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
921 smk_inode_transmutable(dir)) {
923 issp->smk_flags |= SMK_INODE_CHANGED;
926 *value = kstrdup(isp->smk_known, GFP_NOFS);
930 *len = strlen(isp->smk_known);
937 * smack_inode_link - Smack check on link
938 * @old_dentry: the existing object
940 * @new_dentry: the new object
942 * Returns 0 if access is permitted, an error code otherwise
944 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
945 struct dentry *new_dentry)
947 struct smack_known *isp;
948 struct smk_audit_info ad;
951 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
952 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
954 isp = smk_of_inode(d_backing_inode(old_dentry));
955 rc = smk_curacc(isp, MAY_WRITE, &ad);
956 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
958 if (rc == 0 && d_is_positive(new_dentry)) {
959 isp = smk_of_inode(d_backing_inode(new_dentry));
960 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
961 rc = smk_curacc(isp, MAY_WRITE, &ad);
962 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
969 * smack_inode_unlink - Smack check on inode deletion
970 * @dir: containing directory object
971 * @dentry: file to unlink
973 * Returns 0 if current can write the containing directory
974 * and the object, error code otherwise
976 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
978 struct inode *ip = d_backing_inode(dentry);
979 struct smk_audit_info ad;
982 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
983 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
986 * You need write access to the thing you're unlinking
988 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
989 rc = smk_bu_inode(ip, MAY_WRITE, rc);
992 * You also need write access to the containing directory
994 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
995 smk_ad_setfield_u_fs_inode(&ad, dir);
996 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
997 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1003 * smack_inode_rmdir - Smack check on directory deletion
1004 * @dir: containing directory object
1005 * @dentry: directory to unlink
1007 * Returns 0 if current can write the containing directory
1008 * and the directory, error code otherwise
1010 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1012 struct smk_audit_info ad;
1015 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1016 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1019 * You need write access to the thing you're removing
1021 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1022 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1025 * You also need write access to the containing directory
1027 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1028 smk_ad_setfield_u_fs_inode(&ad, dir);
1029 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1030 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1037 * smack_inode_rename - Smack check on rename
1038 * @old_inode: unused
1039 * @old_dentry: the old object
1040 * @new_inode: unused
1041 * @new_dentry: the new object
1043 * Read and write access is required on both the old and
1046 * Returns 0 if access is permitted, an error code otherwise
1048 static int smack_inode_rename(struct inode *old_inode,
1049 struct dentry *old_dentry,
1050 struct inode *new_inode,
1051 struct dentry *new_dentry)
1054 struct smack_known *isp;
1055 struct smk_audit_info ad;
1057 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1058 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1060 isp = smk_of_inode(d_backing_inode(old_dentry));
1061 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1062 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1064 if (rc == 0 && d_is_positive(new_dentry)) {
1065 isp = smk_of_inode(d_backing_inode(new_dentry));
1066 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1067 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1068 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1074 * smack_inode_permission - Smack version of permission()
1075 * @inode: the inode in question
1076 * @mask: the access requested
1078 * This is the important Smack hook.
1080 * Returns 0 if access is permitted, -EACCES otherwise
1082 static int smack_inode_permission(struct inode *inode, int mask)
1084 struct superblock_smack *sbsp = inode->i_sb->s_security;
1085 struct smk_audit_info ad;
1086 int no_block = mask & MAY_NOT_BLOCK;
1089 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1091 * No permission to check. Existence test. Yup, it's there.
1096 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1097 if (smk_of_inode(inode) != sbsp->smk_root)
1101 /* May be droppable after audit */
1104 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1105 smk_ad_setfield_u_fs_inode(&ad, inode);
1106 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1107 rc = smk_bu_inode(inode, mask, rc);
1112 * smack_inode_setattr - Smack check for setting attributes
1113 * @dentry: the object
1114 * @iattr: for the force flag
1116 * Returns 0 if access is permitted, an error code otherwise
1118 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1120 struct smk_audit_info ad;
1124 * Need to allow for clearing the setuid bit.
1126 if (iattr->ia_valid & ATTR_FORCE)
1128 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1129 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1131 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1132 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1137 * smack_inode_getattr - Smack check for getting attributes
1138 * @mnt: vfsmount of the object
1139 * @dentry: the object
1141 * Returns 0 if access is permitted, an error code otherwise
1143 static int smack_inode_getattr(const struct path *path)
1145 struct smk_audit_info ad;
1146 struct inode *inode = d_backing_inode(path->dentry);
1149 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1150 smk_ad_setfield_u_fs_path(&ad, *path);
1151 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1152 rc = smk_bu_inode(inode, MAY_READ, rc);
1157 * smack_inode_setxattr - Smack check for setting xattrs
1158 * @dentry: the object
1159 * @name: name of the attribute
1160 * @value: value of the attribute
1161 * @size: size of the value
1164 * This protects the Smack attribute explicitly.
1166 * Returns 0 if access is permitted, an error code otherwise
1168 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1169 const void *value, size_t size, int flags)
1171 struct smk_audit_info ad;
1172 struct smack_known *skp;
1174 int check_import = 0;
1179 * Check label validity here so import won't fail in post_setxattr
1181 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1182 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1183 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1186 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1187 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1191 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1193 if (size != TRANS_TRUE_SIZE ||
1194 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1197 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1199 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1202 if (rc == 0 && check_import) {
1203 skp = size ? smk_import_entry(value, size) : NULL;
1206 else if (skp == NULL || (check_star &&
1207 (skp == &smack_known_star || skp == &smack_known_web)))
1211 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1212 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1215 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1216 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1223 * smack_inode_post_setxattr - Apply the Smack update approved above
1225 * @name: attribute name
1226 * @value: attribute value
1227 * @size: attribute size
1230 * Set the pointer in the inode blob to the entry found
1231 * in the master label list.
1233 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1234 const void *value, size_t size, int flags)
1236 struct smack_known *skp;
1237 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1239 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1240 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1244 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1245 skp = smk_import_entry(value, size);
1247 isp->smk_inode = skp;
1248 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1249 skp = smk_import_entry(value, size);
1251 isp->smk_task = skp;
1252 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1253 skp = smk_import_entry(value, size);
1255 isp->smk_mmap = skp;
1262 * smack_inode_getxattr - Smack check on getxattr
1263 * @dentry: the object
1266 * Returns 0 if access is permitted, an error code otherwise
1268 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1270 struct smk_audit_info ad;
1273 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1274 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1276 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1277 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1282 * smack_inode_removexattr - Smack check on removexattr
1283 * @dentry: the object
1284 * @name: name of the attribute
1286 * Removing the Smack attribute requires CAP_MAC_ADMIN
1288 * Returns 0 if access is permitted, an error code otherwise
1290 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1292 struct inode_smack *isp;
1293 struct smk_audit_info ad;
1296 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1297 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1298 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1299 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1300 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1301 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1302 if (!smack_privileged(CAP_MAC_ADMIN))
1305 rc = cap_inode_removexattr(dentry, name);
1310 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1311 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1313 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1314 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1318 isp = smack_inode(d_backing_inode(dentry));
1320 * Don't do anything special for these.
1321 * XATTR_NAME_SMACKIPIN
1322 * XATTR_NAME_SMACKIPOUT
1324 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1325 struct super_block *sbp = dentry->d_sb;
1326 struct superblock_smack *sbsp = sbp->s_security;
1328 isp->smk_inode = sbsp->smk_default;
1329 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1330 isp->smk_task = NULL;
1331 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1332 isp->smk_mmap = NULL;
1333 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1334 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1340 * smack_inode_getsecurity - get smack xattrs
1341 * @inode: the object
1342 * @name: attribute name
1343 * @buffer: where to put the result
1344 * @alloc: duplicate memory
1346 * Returns the size of the attribute or an error code
1348 static int smack_inode_getsecurity(struct inode *inode,
1349 const char *name, void **buffer,
1352 struct socket_smack *ssp;
1353 struct socket *sock;
1354 struct super_block *sbp;
1355 struct inode *ip = (struct inode *)inode;
1356 struct smack_known *isp;
1358 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1359 isp = smk_of_inode(inode);
1362 * The rest of the Smack xattrs are only on sockets.
1365 if (sbp->s_magic != SOCKFS_MAGIC)
1368 sock = SOCKET_I(ip);
1369 if (sock == NULL || sock->sk == NULL)
1372 ssp = sock->sk->sk_security;
1374 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1376 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1383 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1384 if (*buffer == NULL)
1388 return strlen(isp->smk_known);
1393 * smack_inode_listsecurity - list the Smack attributes
1394 * @inode: the object
1395 * @buffer: where they go
1396 * @buffer_size: size of buffer
1398 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1401 int len = sizeof(XATTR_NAME_SMACK);
1403 if (buffer != NULL && len <= buffer_size)
1404 memcpy(buffer, XATTR_NAME_SMACK, len);
1410 * smack_inode_getsecid - Extract inode's security id
1411 * @inode: inode to extract the info from
1412 * @secid: where result will be saved
1414 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1416 struct smack_known *skp = smk_of_inode(inode);
1418 *secid = skp->smk_secid;
1426 * There is no smack_file_permission hook
1428 * Should access checks be done on each read or write?
1429 * UNICOS and SELinux say yes.
1430 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1432 * I'll say no for now. Smack does not do the frequent
1433 * label changing that SELinux does.
1437 * smack_file_alloc_security - assign a file security blob
1440 * The security blob for a file is a pointer to the master
1441 * label list, so no allocation is done.
1443 * f_security is the owner security information. It
1444 * isn't used on file access checks, it's for send_sigio.
1448 static int smack_file_alloc_security(struct file *file)
1450 struct smack_known **blob = smack_file(file);
1452 *blob = smk_of_current();
1457 * smack_file_ioctl - Smack check on ioctls
1462 * Relies heavily on the correct use of the ioctl command conventions.
1464 * Returns 0 if allowed, error code otherwise
1466 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1470 struct smk_audit_info ad;
1471 struct inode *inode = file_inode(file);
1473 if (unlikely(IS_PRIVATE(inode)))
1476 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1477 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1479 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1480 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1481 rc = smk_bu_file(file, MAY_WRITE, rc);
1484 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1485 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1486 rc = smk_bu_file(file, MAY_READ, rc);
1493 * smack_file_lock - Smack check on file locking
1497 * Returns 0 if current has lock access, error code otherwise
1499 static int smack_file_lock(struct file *file, unsigned int cmd)
1501 struct smk_audit_info ad;
1503 struct inode *inode = file_inode(file);
1505 if (unlikely(IS_PRIVATE(inode)))
1508 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1509 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1510 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1511 rc = smk_bu_file(file, MAY_LOCK, rc);
1516 * smack_file_fcntl - Smack check on fcntl
1518 * @cmd: what action to check
1521 * Generally these operations are harmless.
1522 * File locking operations present an obvious mechanism
1523 * for passing information, so they require write access.
1525 * Returns 0 if current has access, error code otherwise
1527 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1530 struct smk_audit_info ad;
1532 struct inode *inode = file_inode(file);
1534 if (unlikely(IS_PRIVATE(inode)))
1542 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1543 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1544 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1545 rc = smk_bu_file(file, MAY_LOCK, rc);
1549 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1550 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1551 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1552 rc = smk_bu_file(file, MAY_WRITE, rc);
1563 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1564 * if mapping anonymous memory.
1565 * @file contains the file structure for file to map (may be NULL).
1566 * @reqprot contains the protection requested by the application.
1567 * @prot contains the protection that will be applied by the kernel.
1568 * @flags contains the operational flags.
1569 * Return 0 if permission is granted.
1571 static int smack_mmap_file(struct file *file,
1572 unsigned long reqprot, unsigned long prot,
1573 unsigned long flags)
1575 struct smack_known *skp;
1576 struct smack_known *mkp;
1577 struct smack_rule *srp;
1578 struct task_smack *tsp;
1579 struct smack_known *okp;
1580 struct inode_smack *isp;
1581 struct superblock_smack *sbsp;
1590 if (unlikely(IS_PRIVATE(file_inode(file))))
1593 isp = smack_inode(file_inode(file));
1594 if (isp->smk_mmap == NULL)
1596 sbsp = file_inode(file)->i_sb->s_security;
1597 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1598 isp->smk_mmap != sbsp->smk_root)
1600 mkp = isp->smk_mmap;
1602 tsp = smack_cred(current_cred());
1603 skp = smk_of_current();
1608 * For each Smack rule associated with the subject
1609 * label verify that the SMACK64MMAP also has access
1610 * to that rule's object label.
1612 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1613 okp = srp->smk_object;
1615 * Matching labels always allows access.
1617 if (mkp->smk_known == okp->smk_known)
1620 * If there is a matching local rule take
1621 * that into account as well.
1623 may = smk_access_entry(srp->smk_subject->smk_known,
1627 may = srp->smk_access;
1629 may &= srp->smk_access;
1631 * If may is zero the SMACK64MMAP subject can't
1632 * possibly have less access.
1638 * Fetch the global list entry.
1639 * If there isn't one a SMACK64MMAP subject
1640 * can't have as much access as current.
1642 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1644 if (mmay == -ENOENT) {
1649 * If there is a local entry it modifies the
1650 * potential access, too.
1652 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1654 if (tmay != -ENOENT)
1658 * If there is any access available to current that is
1659 * not available to a SMACK64MMAP subject
1662 if ((may | mmay) != mmay) {
1674 * smack_file_set_fowner - set the file security blob value
1675 * @file: object in question
1678 static void smack_file_set_fowner(struct file *file)
1680 struct smack_known **blob = smack_file(file);
1682 *blob = smk_of_current();
1686 * smack_file_send_sigiotask - Smack on sigio
1687 * @tsk: The target task
1688 * @fown: the object the signal come from
1691 * Allow a privileged task to get signals even if it shouldn't
1693 * Returns 0 if a subject with the object's smack could
1694 * write to the task, an error code otherwise.
1696 static int smack_file_send_sigiotask(struct task_struct *tsk,
1697 struct fown_struct *fown, int signum)
1699 struct smack_known **blob;
1700 struct smack_known *skp;
1701 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1702 const struct cred *tcred;
1705 struct smk_audit_info ad;
1708 * struct fown_struct is never outside the context of a struct file
1710 file = container_of(fown, struct file, f_owner);
1712 /* we don't log here as rc can be overriden */
1713 blob = smack_file(file);
1715 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1716 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1719 tcred = __task_cred(tsk);
1720 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1724 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1725 smk_ad_setfield_u_tsk(&ad, tsk);
1726 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1731 * smack_file_receive - Smack file receive check
1734 * Returns 0 if current has access, error code otherwise
1736 static int smack_file_receive(struct file *file)
1740 struct smk_audit_info ad;
1741 struct inode *inode = file_inode(file);
1742 struct socket *sock;
1743 struct task_smack *tsp;
1744 struct socket_smack *ssp;
1746 if (unlikely(IS_PRIVATE(inode)))
1749 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1750 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1752 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1753 sock = SOCKET_I(inode);
1754 ssp = sock->sk->sk_security;
1755 tsp = smack_cred(current_cred());
1757 * If the receiving process can't write to the
1758 * passed socket or if the passed socket can't
1759 * write to the receiving process don't accept
1760 * the passed socket.
1762 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1763 rc = smk_bu_file(file, may, rc);
1766 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1767 rc = smk_bu_file(file, may, rc);
1771 * This code relies on bitmasks.
1773 if (file->f_mode & FMODE_READ)
1775 if (file->f_mode & FMODE_WRITE)
1778 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1779 rc = smk_bu_file(file, may, rc);
1784 * smack_file_open - Smack dentry open processing
1786 * @cred: task credential
1788 * Set the security blob in the file structure.
1789 * Allow the open only if the task has read access. There are
1790 * many read operations (e.g. fstat) that you can do with an
1791 * fd even if you have the file open write-only.
1795 static int smack_file_open(struct file *file)
1797 struct task_smack *tsp = smack_cred(file->f_cred);
1798 struct inode *inode = file_inode(file);
1799 struct smk_audit_info ad;
1802 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1803 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1804 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1805 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1815 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1816 * @new: the new credentials
1817 * @gfp: the atomicity of any memory allocations
1819 * Prepare a blank set of credentials for modification. This must allocate all
1820 * the memory the LSM module might require such that cred_transfer() can
1821 * complete without error.
1823 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1825 init_task_smack(smack_cred(cred), NULL, NULL);
1831 * smack_cred_free - "free" task-level security credentials
1832 * @cred: the credentials in question
1835 static void smack_cred_free(struct cred *cred)
1837 struct task_smack *tsp = smack_cred(cred);
1838 struct smack_rule *rp;
1839 struct list_head *l;
1840 struct list_head *n;
1842 smk_destroy_label_list(&tsp->smk_relabel);
1844 list_for_each_safe(l, n, &tsp->smk_rules) {
1845 rp = list_entry(l, struct smack_rule, list);
1846 list_del(&rp->list);
1852 * smack_cred_prepare - prepare new set of credentials for modification
1853 * @new: the new credentials
1854 * @old: the original credentials
1855 * @gfp: the atomicity of any memory allocations
1857 * Prepare a new set of credentials for modification.
1859 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1862 struct task_smack *old_tsp = smack_cred(old);
1863 struct task_smack *new_tsp = smack_cred(new);
1866 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1868 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1872 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1878 * smack_cred_transfer - Transfer the old credentials to the new credentials
1879 * @new: the new credentials
1880 * @old: the original credentials
1882 * Fill in a set of blank credentials from another set of credentials.
1884 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1886 struct task_smack *old_tsp = smack_cred(old);
1887 struct task_smack *new_tsp = smack_cred(new);
1889 new_tsp->smk_task = old_tsp->smk_task;
1890 new_tsp->smk_forked = old_tsp->smk_task;
1891 mutex_init(&new_tsp->smk_rules_lock);
1892 INIT_LIST_HEAD(&new_tsp->smk_rules);
1894 /* cbs copy rule list */
1898 * smack_cred_getsecid - get the secid corresponding to a creds structure
1899 * @c: the object creds
1900 * @secid: where to put the result
1902 * Sets the secid to contain a u32 version of the smack label.
1904 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1906 struct smack_known *skp;
1909 skp = smk_of_task(smack_cred(cred));
1910 *secid = skp->smk_secid;
1915 * smack_kernel_act_as - Set the subjective context in a set of credentials
1916 * @new: points to the set of credentials to be modified.
1917 * @secid: specifies the security ID to be set
1919 * Set the security data for a kernel service.
1921 static int smack_kernel_act_as(struct cred *new, u32 secid)
1923 struct task_smack *new_tsp = smack_cred(new);
1925 new_tsp->smk_task = smack_from_secid(secid);
1930 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1931 * @new: points to the set of credentials to be modified
1932 * @inode: points to the inode to use as a reference
1934 * Set the file creation context in a set of credentials to the same
1935 * as the objective context of the specified inode
1937 static int smack_kernel_create_files_as(struct cred *new,
1938 struct inode *inode)
1940 struct inode_smack *isp = smack_inode(inode);
1941 struct task_smack *tsp = smack_cred(new);
1943 tsp->smk_forked = isp->smk_inode;
1944 tsp->smk_task = tsp->smk_forked;
1949 * smk_curacc_on_task - helper to log task related access
1950 * @p: the task object
1951 * @access: the access requested
1952 * @caller: name of the calling function for audit
1954 * Return 0 if access is permitted
1956 static int smk_curacc_on_task(struct task_struct *p, int access,
1959 struct smk_audit_info ad;
1960 struct smack_known *skp = smk_of_task_struct(p);
1963 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1964 smk_ad_setfield_u_tsk(&ad, p);
1965 rc = smk_curacc(skp, access, &ad);
1966 rc = smk_bu_task(p, access, rc);
1971 * smack_task_setpgid - Smack check on setting pgid
1972 * @p: the task object
1975 * Return 0 if write access is permitted
1977 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1979 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1983 * smack_task_getpgid - Smack access check for getpgid
1984 * @p: the object task
1986 * Returns 0 if current can read the object task, error code otherwise
1988 static int smack_task_getpgid(struct task_struct *p)
1990 return smk_curacc_on_task(p, MAY_READ, __func__);
1994 * smack_task_getsid - Smack access check for getsid
1995 * @p: the object task
1997 * Returns 0 if current can read the object task, error code otherwise
1999 static int smack_task_getsid(struct task_struct *p)
2001 return smk_curacc_on_task(p, MAY_READ, __func__);
2005 * smack_task_getsecid - get the secid of the task
2006 * @p: the object task
2007 * @secid: where to put the result
2009 * Sets the secid to contain a u32 version of the smack label.
2011 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2013 struct smack_known *skp = smk_of_task_struct(p);
2015 *secid = skp->smk_secid;
2019 * smack_task_setnice - Smack check on setting nice
2020 * @p: the task object
2023 * Return 0 if write access is permitted
2025 static int smack_task_setnice(struct task_struct *p, int nice)
2027 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2031 * smack_task_setioprio - Smack check on setting ioprio
2032 * @p: the task object
2035 * Return 0 if write access is permitted
2037 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2039 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2043 * smack_task_getioprio - Smack check on reading ioprio
2044 * @p: the task object
2046 * Return 0 if read access is permitted
2048 static int smack_task_getioprio(struct task_struct *p)
2050 return smk_curacc_on_task(p, MAY_READ, __func__);
2054 * smack_task_setscheduler - Smack check on setting scheduler
2055 * @p: the task object
2059 * Return 0 if read access is permitted
2061 static int smack_task_setscheduler(struct task_struct *p)
2063 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2067 * smack_task_getscheduler - Smack check on reading scheduler
2068 * @p: the task object
2070 * Return 0 if read access is permitted
2072 static int smack_task_getscheduler(struct task_struct *p)
2074 return smk_curacc_on_task(p, MAY_READ, __func__);
2078 * smack_task_movememory - Smack check on moving memory
2079 * @p: the task object
2081 * Return 0 if write access is permitted
2083 static int smack_task_movememory(struct task_struct *p)
2085 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2089 * smack_task_kill - Smack check on signal delivery
2090 * @p: the task object
2093 * @cred: identifies the cred to use in lieu of current's
2095 * Return 0 if write access is permitted
2098 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2099 int sig, const struct cred *cred)
2101 struct smk_audit_info ad;
2102 struct smack_known *skp;
2103 struct smack_known *tkp = smk_of_task_struct(p);
2107 return 0; /* null signal; existence test */
2109 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2110 smk_ad_setfield_u_tsk(&ad, p);
2112 * Sending a signal requires that the sender
2113 * can write the receiver.
2116 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2117 rc = smk_bu_task(p, MAY_DELIVER, rc);
2121 * If the cred isn't NULL we're dealing with some USB IO
2122 * specific behavior. This is not clean. For one thing
2123 * we can't take privilege into account.
2125 skp = smk_of_task(smack_cred(cred));
2126 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2127 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2132 * smack_task_to_inode - copy task smack into the inode blob
2133 * @p: task to copy from
2134 * @inode: inode to copy to
2136 * Sets the smack pointer in the inode security blob
2138 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2140 struct inode_smack *isp = smack_inode(inode);
2141 struct smack_known *skp = smk_of_task_struct(p);
2143 isp->smk_inode = skp;
2144 isp->smk_flags |= SMK_INODE_INSTANT;
2152 * smack_sk_alloc_security - Allocate a socket blob
2155 * @gfp_flags: memory allocation flags
2157 * Assign Smack pointers to current
2159 * Returns 0 on success, -ENOMEM is there's no memory
2161 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2163 struct smack_known *skp = smk_of_current();
2164 struct socket_smack *ssp;
2166 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2171 * Sockets created by kernel threads receive web label.
2173 if (unlikely(current->flags & PF_KTHREAD)) {
2174 ssp->smk_in = &smack_known_web;
2175 ssp->smk_out = &smack_known_web;
2180 ssp->smk_packet = NULL;
2182 sk->sk_security = ssp;
2188 * smack_sk_free_security - Free a socket blob
2191 * Clears the blob pointer
2193 static void smack_sk_free_security(struct sock *sk)
2195 #ifdef SMACK_IPV6_PORT_LABELING
2196 struct smk_port_label *spp;
2198 if (sk->sk_family == PF_INET6) {
2200 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2201 if (spp->smk_sock != sk)
2203 spp->smk_can_reuse = 1;
2209 kfree(sk->sk_security);
2213 * smack_ipv4host_label - check host based restrictions
2214 * @sip: the object end
2216 * looks for host based access restrictions
2218 * This version will only be appropriate for really small sets of single label
2219 * hosts. The caller is responsible for ensuring that the RCU read lock is
2220 * taken before calling this function.
2222 * Returns the label of the far end or NULL if it's not special.
2224 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2226 struct smk_net4addr *snp;
2227 struct in_addr *siap = &sip->sin_addr;
2229 if (siap->s_addr == 0)
2232 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2234 * we break after finding the first match because
2235 * the list is sorted from longest to shortest mask
2236 * so we have found the most specific match
2238 if (snp->smk_host.s_addr ==
2239 (siap->s_addr & snp->smk_mask.s_addr))
2240 return snp->smk_label;
2245 #if IS_ENABLED(CONFIG_IPV6)
2247 * smk_ipv6_localhost - Check for local ipv6 host address
2250 * Returns boolean true if this is the localhost address
2252 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2254 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2255 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2257 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2258 ntohs(be16p[7]) == 1)
2264 * smack_ipv6host_label - check host based restrictions
2265 * @sip: the object end
2267 * looks for host based access restrictions
2269 * This version will only be appropriate for really small sets of single label
2270 * hosts. The caller is responsible for ensuring that the RCU read lock is
2271 * taken before calling this function.
2273 * Returns the label of the far end or NULL if it's not special.
2275 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2277 struct smk_net6addr *snp;
2278 struct in6_addr *sap = &sip->sin6_addr;
2283 * It's local. Don't look for a host label.
2285 if (smk_ipv6_localhost(sip))
2288 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2290 * If the label is NULL the entry has
2291 * been renounced. Ignore it.
2293 if (snp->smk_label == NULL)
2296 * we break after finding the first match because
2297 * the list is sorted from longest to shortest mask
2298 * so we have found the most specific match
2300 for (found = 1, i = 0; i < 8; i++) {
2301 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2302 snp->smk_host.s6_addr16[i]) {
2308 return snp->smk_label;
2313 #endif /* CONFIG_IPV6 */
2316 * smack_netlabel - Set the secattr on a socket
2318 * @labeled: socket label scheme
2320 * Convert the outbound smack value (smk_out) to a
2321 * secattr and attach it to the socket.
2323 * Returns 0 on success or an error code
2325 static int smack_netlabel(struct sock *sk, int labeled)
2327 struct smack_known *skp;
2328 struct socket_smack *ssp = sk->sk_security;
2332 * Usually the netlabel code will handle changing the
2333 * packet labeling based on the label.
2334 * The case of a single label host is different, because
2335 * a single label host should never get a labeled packet
2336 * even though the label is usually associated with a packet
2340 bh_lock_sock_nested(sk);
2342 if (ssp->smk_out == smack_net_ambient ||
2343 labeled == SMACK_UNLABELED_SOCKET)
2344 netlbl_sock_delattr(sk);
2347 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2357 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2359 * @sap: the destination address
2361 * Set the correct secattr for the given socket based on the destination
2362 * address and perform any outbound access checks needed.
2364 * Returns 0 on success or an error code.
2367 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2369 struct smack_known *skp;
2372 struct smack_known *hkp;
2373 struct socket_smack *ssp = sk->sk_security;
2374 struct smk_audit_info ad;
2377 hkp = smack_ipv4host_label(sap);
2380 struct lsm_network_audit net;
2382 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2383 ad.a.u.net->family = sap->sin_family;
2384 ad.a.u.net->dport = sap->sin_port;
2385 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2387 sk_lbl = SMACK_UNLABELED_SOCKET;
2389 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2390 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2392 sk_lbl = SMACK_CIPSO_SOCKET;
2399 return smack_netlabel(sk, sk_lbl);
2402 #if IS_ENABLED(CONFIG_IPV6)
2404 * smk_ipv6_check - check Smack access
2405 * @subject: subject Smack label
2406 * @object: object Smack label
2408 * @act: the action being taken
2410 * Check an IPv6 access
2412 static int smk_ipv6_check(struct smack_known *subject,
2413 struct smack_known *object,
2414 struct sockaddr_in6 *address, int act)
2417 struct lsm_network_audit net;
2419 struct smk_audit_info ad;
2423 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2424 ad.a.u.net->family = PF_INET6;
2425 ad.a.u.net->dport = ntohs(address->sin6_port);
2426 if (act == SMK_RECEIVING)
2427 ad.a.u.net->v6info.saddr = address->sin6_addr;
2429 ad.a.u.net->v6info.daddr = address->sin6_addr;
2431 rc = smk_access(subject, object, MAY_WRITE, &ad);
2432 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2435 #endif /* CONFIG_IPV6 */
2437 #ifdef SMACK_IPV6_PORT_LABELING
2439 * smk_ipv6_port_label - Smack port access table management
2443 * Create or update the port list entry
2445 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2447 struct sock *sk = sock->sk;
2448 struct sockaddr_in6 *addr6;
2449 struct socket_smack *ssp = sock->sk->sk_security;
2450 struct smk_port_label *spp;
2451 unsigned short port = 0;
2453 if (address == NULL) {
2455 * This operation is changing the Smack information
2456 * on the bound socket. Take the changes to the port
2460 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2461 if (sk != spp->smk_sock)
2463 spp->smk_in = ssp->smk_in;
2464 spp->smk_out = ssp->smk_out;
2469 * A NULL address is only used for updating existing
2470 * bound entries. If there isn't one, it's OK.
2476 addr6 = (struct sockaddr_in6 *)address;
2477 port = ntohs(addr6->sin6_port);
2479 * This is a special case that is safely ignored.
2485 * Look for an existing port list entry.
2486 * This is an indication that a port is getting reused.
2489 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2490 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2492 if (spp->smk_can_reuse != 1) {
2496 spp->smk_port = port;
2498 spp->smk_in = ssp->smk_in;
2499 spp->smk_out = ssp->smk_out;
2500 spp->smk_can_reuse = 0;
2506 * A new port entry is required.
2508 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2512 spp->smk_port = port;
2514 spp->smk_in = ssp->smk_in;
2515 spp->smk_out = ssp->smk_out;
2516 spp->smk_sock_type = sock->type;
2517 spp->smk_can_reuse = 0;
2519 mutex_lock(&smack_ipv6_lock);
2520 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2521 mutex_unlock(&smack_ipv6_lock);
2526 * smk_ipv6_port_check - check Smack port access
2530 * Create or update the port list entry
2532 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2535 struct smk_port_label *spp;
2536 struct socket_smack *ssp = sk->sk_security;
2537 struct smack_known *skp = NULL;
2538 unsigned short port;
2539 struct smack_known *object;
2541 if (act == SMK_RECEIVING) {
2542 skp = smack_ipv6host_label(address);
2543 object = ssp->smk_in;
2546 object = smack_ipv6host_label(address);
2550 * The other end is a single label host.
2552 if (skp != NULL && object != NULL)
2553 return smk_ipv6_check(skp, object, address, act);
2555 skp = smack_net_ambient;
2557 object = smack_net_ambient;
2560 * It's remote, so port lookup does no good.
2562 if (!smk_ipv6_localhost(address))
2563 return smk_ipv6_check(skp, object, address, act);
2566 * It's local so the send check has to have passed.
2568 if (act == SMK_RECEIVING)
2571 port = ntohs(address->sin6_port);
2573 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2574 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2576 object = spp->smk_in;
2577 if (act == SMK_CONNECTING)
2578 ssp->smk_packet = spp->smk_out;
2583 return smk_ipv6_check(skp, object, address, act);
2585 #endif /* SMACK_IPV6_PORT_LABELING */
2588 * smack_inode_setsecurity - set smack xattrs
2589 * @inode: the object
2590 * @name: attribute name
2591 * @value: attribute value
2592 * @size: size of the attribute
2595 * Sets the named attribute in the appropriate blob
2597 * Returns 0 on success, or an error code
2599 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2600 const void *value, size_t size, int flags)
2602 struct smack_known *skp;
2603 struct inode_smack *nsp = smack_inode(inode);
2604 struct socket_smack *ssp;
2605 struct socket *sock;
2608 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2611 skp = smk_import_entry(value, size);
2613 return PTR_ERR(skp);
2615 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2616 nsp->smk_inode = skp;
2617 nsp->smk_flags |= SMK_INODE_INSTANT;
2621 * The rest of the Smack xattrs are only on sockets.
2623 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2626 sock = SOCKET_I(inode);
2627 if (sock == NULL || sock->sk == NULL)
2630 ssp = sock->sk->sk_security;
2632 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2634 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2636 if (sock->sk->sk_family == PF_INET) {
2637 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2640 "Smack: \"%s\" netlbl error %d.\n",
2646 #ifdef SMACK_IPV6_PORT_LABELING
2647 if (sock->sk->sk_family == PF_INET6)
2648 smk_ipv6_port_label(sock, NULL);
2655 * smack_socket_post_create - finish socket setup
2657 * @family: protocol family
2662 * Sets the netlabel information on the socket
2664 * Returns 0 on success, and error code otherwise
2666 static int smack_socket_post_create(struct socket *sock, int family,
2667 int type, int protocol, int kern)
2669 struct socket_smack *ssp;
2671 if (sock->sk == NULL)
2675 * Sockets created by kernel threads receive web label.
2677 if (unlikely(current->flags & PF_KTHREAD)) {
2678 ssp = sock->sk->sk_security;
2679 ssp->smk_in = &smack_known_web;
2680 ssp->smk_out = &smack_known_web;
2683 if (family != PF_INET)
2686 * Set the outbound netlbl.
2688 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2692 * smack_socket_socketpair - create socket pair
2693 * @socka: one socket
2694 * @sockb: another socket
2696 * Cross reference the peer labels for SO_PEERSEC
2698 * Returns 0 on success, and error code otherwise
2700 static int smack_socket_socketpair(struct socket *socka,
2701 struct socket *sockb)
2703 struct socket_smack *asp = socka->sk->sk_security;
2704 struct socket_smack *bsp = sockb->sk->sk_security;
2706 asp->smk_packet = bsp->smk_out;
2707 bsp->smk_packet = asp->smk_out;
2712 #ifdef SMACK_IPV6_PORT_LABELING
2714 * smack_socket_bind - record port binding information.
2716 * @address: the port address
2717 * @addrlen: size of the address
2719 * Records the label bound to a port.
2723 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2726 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2727 smk_ipv6_port_label(sock, address);
2730 #endif /* SMACK_IPV6_PORT_LABELING */
2733 * smack_socket_connect - connect access check
2735 * @sap: the other end
2736 * @addrlen: size of sap
2738 * Verifies that a connection may be possible
2740 * Returns 0 on success, and error code otherwise
2742 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2746 #if IS_ENABLED(CONFIG_IPV6)
2747 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2749 #ifdef SMACK_IPV6_SECMARK_LABELING
2750 struct smack_known *rsp;
2751 struct socket_smack *ssp;
2754 if (sock->sk == NULL)
2757 #ifdef SMACK_IPV6_SECMARK_LABELING
2758 ssp = sock->sk->sk_security;
2761 switch (sock->sk->sk_family) {
2763 if (addrlen < sizeof(struct sockaddr_in))
2765 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2768 if (addrlen < sizeof(struct sockaddr_in6))
2770 #ifdef SMACK_IPV6_SECMARK_LABELING
2771 rsp = smack_ipv6host_label(sip);
2773 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2776 #ifdef SMACK_IPV6_PORT_LABELING
2777 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2785 * smack_flags_to_may - convert S_ to MAY_ values
2786 * @flags: the S_ value
2788 * Returns the equivalent MAY_ value
2790 static int smack_flags_to_may(int flags)
2794 if (flags & S_IRUGO)
2796 if (flags & S_IWUGO)
2798 if (flags & S_IXUGO)
2805 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2810 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2812 struct smack_known **blob = smack_msg_msg(msg);
2814 *blob = smk_of_current();
2819 * smack_of_ipc - the smack pointer for the ipc
2822 * Returns a pointer to the smack value
2824 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2826 struct smack_known **blob = smack_ipc(isp);
2832 * smack_ipc_alloc_security - Set the security blob for ipc
2837 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2839 struct smack_known **blob = smack_ipc(isp);
2841 *blob = smk_of_current();
2846 * smk_curacc_shm : check if current has access on shm
2848 * @access : access requested
2850 * Returns 0 if current has the requested access, error code otherwise
2852 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2854 struct smack_known *ssp = smack_of_ipc(isp);
2855 struct smk_audit_info ad;
2859 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2860 ad.a.u.ipc_id = isp->id;
2862 rc = smk_curacc(ssp, access, &ad);
2863 rc = smk_bu_current("shm", ssp, access, rc);
2868 * smack_shm_associate - Smack access check for shm
2870 * @shmflg: access requested
2872 * Returns 0 if current has the requested access, error code otherwise
2874 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2878 may = smack_flags_to_may(shmflg);
2879 return smk_curacc_shm(isp, may);
2883 * smack_shm_shmctl - Smack access check for shm
2885 * @cmd: what it wants to do
2887 * Returns 0 if current has the requested access, error code otherwise
2889 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2903 may = MAY_READWRITE;
2908 * System level information.
2914 return smk_curacc_shm(isp, may);
2918 * smack_shm_shmat - Smack access for shmat
2921 * @shmflg: access requested
2923 * Returns 0 if current has the requested access, error code otherwise
2925 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
2930 may = smack_flags_to_may(shmflg);
2931 return smk_curacc_shm(ipc, may);
2935 * smk_curacc_sem : check if current has access on sem
2937 * @access : access requested
2939 * Returns 0 if current has the requested access, error code otherwise
2941 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
2943 struct smack_known *ssp = smack_of_ipc(isp);
2944 struct smk_audit_info ad;
2948 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2949 ad.a.u.ipc_id = isp->id;
2951 rc = smk_curacc(ssp, access, &ad);
2952 rc = smk_bu_current("sem", ssp, access, rc);
2957 * smack_sem_associate - Smack access check for sem
2959 * @semflg: access requested
2961 * Returns 0 if current has the requested access, error code otherwise
2963 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
2967 may = smack_flags_to_may(semflg);
2968 return smk_curacc_sem(isp, may);
2972 * smack_sem_shmctl - Smack access check for sem
2974 * @cmd: what it wants to do
2976 * Returns 0 if current has the requested access, error code otherwise
2978 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
2997 may = MAY_READWRITE;
3002 * System level information
3009 return smk_curacc_sem(isp, may);
3013 * smack_sem_semop - Smack checks of semaphore operations
3019 * Treated as read and write in all cases.
3021 * Returns 0 if access is allowed, error code otherwise
3023 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3024 unsigned nsops, int alter)
3026 return smk_curacc_sem(isp, MAY_READWRITE);
3030 * smk_curacc_msq : helper to check if current has access on msq
3032 * @access : access requested
3034 * return 0 if current has access, error otherwise
3036 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3038 struct smack_known *msp = smack_of_ipc(isp);
3039 struct smk_audit_info ad;
3043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3044 ad.a.u.ipc_id = isp->id;
3046 rc = smk_curacc(msp, access, &ad);
3047 rc = smk_bu_current("msq", msp, access, rc);
3052 * smack_msg_queue_associate - Smack access check for msg_queue
3054 * @msqflg: access requested
3056 * Returns 0 if current has the requested access, error code otherwise
3058 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3062 may = smack_flags_to_may(msqflg);
3063 return smk_curacc_msq(isp, may);
3067 * smack_msg_queue_msgctl - Smack access check for msg_queue
3069 * @cmd: what it wants to do
3071 * Returns 0 if current has the requested access, error code otherwise
3073 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3085 may = MAY_READWRITE;
3090 * System level information
3097 return smk_curacc_msq(isp, may);
3101 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3104 * @msqflg: access requested
3106 * Returns 0 if current has the requested access, error code otherwise
3108 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3113 may = smack_flags_to_may(msqflg);
3114 return smk_curacc_msq(isp, may);
3118 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3125 * Returns 0 if current has read and write access, error code otherwise
3127 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3128 struct task_struct *target, long type, int mode)
3130 return smk_curacc_msq(isp, MAY_READWRITE);
3134 * smack_ipc_permission - Smack access for ipc_permission()
3135 * @ipp: the object permissions
3136 * @flag: access requested
3138 * Returns 0 if current has read and write access, error code otherwise
3140 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3142 struct smack_known **blob = smack_ipc(ipp);
3143 struct smack_known *iskp = *blob;
3144 int may = smack_flags_to_may(flag);
3145 struct smk_audit_info ad;
3149 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3150 ad.a.u.ipc_id = ipp->id;
3152 rc = smk_curacc(iskp, may, &ad);
3153 rc = smk_bu_current("svipc", iskp, may, rc);
3158 * smack_ipc_getsecid - Extract smack security id
3159 * @ipp: the object permissions
3160 * @secid: where result will be saved
3162 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3164 struct smack_known **blob = smack_ipc(ipp);
3165 struct smack_known *iskp = *blob;
3167 *secid = iskp->smk_secid;
3171 * smack_d_instantiate - Make sure the blob is correct on an inode
3172 * @opt_dentry: dentry where inode will be attached
3173 * @inode: the object
3175 * Set the inode's security blob if it hasn't been done already.
3177 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3179 struct super_block *sbp;
3180 struct superblock_smack *sbsp;
3181 struct inode_smack *isp;
3182 struct smack_known *skp;
3183 struct smack_known *ckp = smk_of_current();
3184 struct smack_known *final;
3185 char trattr[TRANS_TRUE_SIZE];
3193 isp = smack_inode(inode);
3195 mutex_lock(&isp->smk_lock);
3197 * If the inode is already instantiated
3198 * take the quick way out
3200 if (isp->smk_flags & SMK_INODE_INSTANT)
3204 sbsp = sbp->s_security;
3206 * We're going to use the superblock default label
3207 * if there's no label on the file.
3209 final = sbsp->smk_default;
3212 * If this is the root inode the superblock
3213 * may be in the process of initialization.
3214 * If that is the case use the root value out
3215 * of the superblock.
3217 if (opt_dentry->d_parent == opt_dentry) {
3218 switch (sbp->s_magic) {
3219 case CGROUP_SUPER_MAGIC:
3220 case CGROUP2_SUPER_MAGIC:
3222 * The cgroup filesystem is never mounted,
3223 * so there's no opportunity to set the mount
3226 sbsp->smk_root = &smack_known_star;
3227 sbsp->smk_default = &smack_known_star;
3228 isp->smk_inode = sbsp->smk_root;
3232 * What about shmem/tmpfs anonymous files with dentry
3233 * obtained from d_alloc_pseudo()?
3235 isp->smk_inode = smk_of_current();
3238 isp->smk_inode = smk_of_current();
3242 * Socket access is controlled by the socket
3243 * structures associated with the task involved.
3245 isp->smk_inode = &smack_known_star;
3248 isp->smk_inode = sbsp->smk_root;
3251 isp->smk_flags |= SMK_INODE_INSTANT;
3256 * This is pretty hackish.
3257 * Casey says that we shouldn't have to do
3258 * file system specific code, but it does help
3259 * with keeping it simple.
3261 switch (sbp->s_magic) {
3263 case CGROUP_SUPER_MAGIC:
3264 case CGROUP2_SUPER_MAGIC:
3266 * Casey says that it's a little embarrassing
3267 * that the smack file system doesn't do
3268 * extended attributes.
3270 * Cgroupfs is special
3272 final = &smack_known_star;
3274 case DEVPTS_SUPER_MAGIC:
3276 * devpts seems content with the label of the task.
3277 * Programs that change smack have to treat the
3282 case PROC_SUPER_MAGIC:
3284 * Casey says procfs appears not to care.
3285 * The superblock default suffices.
3290 * Device labels should come from the filesystem,
3291 * but watch out, because they're volitile,
3292 * getting recreated on every reboot.
3294 final = &smack_known_star;
3296 * If a smack value has been set we want to use it,
3297 * but since tmpfs isn't giving us the opportunity
3298 * to set mount options simulate setting the
3299 * superblock default.
3304 * This isn't an understood special case.
3305 * Get the value from the xattr.
3309 * UNIX domain sockets use lower level socket data.
3311 if (S_ISSOCK(inode->i_mode)) {
3312 final = &smack_known_star;
3316 * No xattr support means, alas, no SMACK label.
3317 * Use the aforeapplied default.
3318 * It would be curious if the label of the task
3319 * does not match that assigned.
3321 if (!(inode->i_opflags & IOP_XATTR))
3324 * Get the dentry for xattr.
3326 dp = dget(opt_dentry);
3327 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3328 if (!IS_ERR_OR_NULL(skp))
3332 * Transmuting directory
3334 if (S_ISDIR(inode->i_mode)) {
3336 * If this is a new directory and the label was
3337 * transmuted when the inode was initialized
3338 * set the transmute attribute on the directory
3339 * and mark the inode.
3341 * If there is a transmute attribute on the
3342 * directory mark the inode.
3344 if (isp->smk_flags & SMK_INODE_CHANGED) {
3345 isp->smk_flags &= ~SMK_INODE_CHANGED;
3346 rc = __vfs_setxattr(dp, inode,
3347 XATTR_NAME_SMACKTRANSMUTE,
3348 TRANS_TRUE, TRANS_TRUE_SIZE,
3351 rc = __vfs_getxattr(dp, inode,
3352 XATTR_NAME_SMACKTRANSMUTE, trattr,
3354 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3355 TRANS_TRUE_SIZE) != 0)
3359 transflag = SMK_INODE_TRANSMUTE;
3362 * Don't let the exec or mmap label be "*" or "@".
3364 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3365 if (IS_ERR(skp) || skp == &smack_known_star ||
3366 skp == &smack_known_web)
3368 isp->smk_task = skp;
3370 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3371 if (IS_ERR(skp) || skp == &smack_known_star ||
3372 skp == &smack_known_web)
3374 isp->smk_mmap = skp;
3381 isp->smk_inode = ckp;
3383 isp->smk_inode = final;
3385 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3388 mutex_unlock(&isp->smk_lock);
3393 * smack_getprocattr - Smack process attribute access
3394 * @p: the object task
3395 * @name: the name of the attribute in /proc/.../attr
3396 * @value: where to put the result
3398 * Places a copy of the task Smack into value
3400 * Returns the length of the smack label or an error code
3402 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3404 struct smack_known *skp = smk_of_task_struct(p);
3408 if (strcmp(name, "current") != 0)
3411 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3421 * smack_setprocattr - Smack process attribute setting
3422 * @name: the name of the attribute in /proc/.../attr
3423 * @value: the value to set
3424 * @size: the size of the value
3426 * Sets the Smack value of the task. Only setting self
3427 * is permitted and only with privilege
3429 * Returns the length of the smack label or an error code
3431 static int smack_setprocattr(const char *name, void *value, size_t size)
3433 struct task_smack *tsp = smack_cred(current_cred());
3435 struct smack_known *skp;
3436 struct smack_known_list_elem *sklep;
3439 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3442 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3445 if (strcmp(name, "current") != 0)
3448 skp = smk_import_entry(value, size);
3450 return PTR_ERR(skp);
3453 * No process is ever allowed the web ("@") label
3454 * and the star ("*") label.
3456 if (skp == &smack_known_web || skp == &smack_known_star)
3459 if (!smack_privileged(CAP_MAC_ADMIN)) {
3461 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3462 if (sklep->smk_label == skp) {
3470 new = prepare_creds();
3474 tsp = smack_cred(new);
3475 tsp->smk_task = skp;
3477 * process can change its label only once
3479 smk_destroy_label_list(&tsp->smk_relabel);
3486 * smack_unix_stream_connect - Smack access on UDS
3488 * @other: the other sock
3491 * Return 0 if a subject with the smack of sock could access
3492 * an object with the smack of other, otherwise an error code
3494 static int smack_unix_stream_connect(struct sock *sock,
3495 struct sock *other, struct sock *newsk)
3497 struct smack_known *skp;
3498 struct smack_known *okp;
3499 struct socket_smack *ssp = sock->sk_security;
3500 struct socket_smack *osp = other->sk_security;
3501 struct socket_smack *nsp = newsk->sk_security;
3502 struct smk_audit_info ad;
3505 struct lsm_network_audit net;
3508 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3512 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3513 smk_ad_setfield_u_net_sk(&ad, other);
3515 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3516 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3520 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3521 rc = smk_bu_note("UDS connect", okp, skp,
3527 * Cross reference the peer labels for SO_PEERSEC.
3530 nsp->smk_packet = ssp->smk_out;
3531 ssp->smk_packet = osp->smk_out;
3538 * smack_unix_may_send - Smack access on UDS
3540 * @other: the other socket
3542 * Return 0 if a subject with the smack of sock could access
3543 * an object with the smack of other, otherwise an error code
3545 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3547 struct socket_smack *ssp = sock->sk->sk_security;
3548 struct socket_smack *osp = other->sk->sk_security;
3549 struct smk_audit_info ad;
3553 struct lsm_network_audit net;
3555 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3556 smk_ad_setfield_u_net_sk(&ad, other->sk);
3559 if (smack_privileged(CAP_MAC_OVERRIDE))
3562 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3563 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3568 * smack_socket_sendmsg - Smack check based on destination host
3571 * @size: the size of the message
3573 * Return 0 if the current subject can write to the destination host.
3574 * For IPv4 this is only a question if the destination is a single label host.
3575 * For IPv6 this is a check against the label of the port.
3577 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3580 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3581 #if IS_ENABLED(CONFIG_IPV6)
3582 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3584 #ifdef SMACK_IPV6_SECMARK_LABELING
3585 struct socket_smack *ssp = sock->sk->sk_security;
3586 struct smack_known *rsp;
3591 * Perfectly reasonable for this to be NULL
3596 switch (sock->sk->sk_family) {
3598 rc = smack_netlabel_send(sock->sk, sip);
3601 #ifdef SMACK_IPV6_SECMARK_LABELING
3602 rsp = smack_ipv6host_label(sap);
3604 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3607 #ifdef SMACK_IPV6_PORT_LABELING
3608 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3616 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3617 * @sap: netlabel secattr
3618 * @ssp: socket security information
3620 * Returns a pointer to a Smack label entry found on the label list.
3622 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3623 struct socket_smack *ssp)
3625 struct smack_known *skp;
3630 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3632 * Looks like a CIPSO packet.
3633 * If there are flags but no level netlabel isn't
3634 * behaving the way we expect it to.
3636 * Look it up in the label table
3637 * Without guidance regarding the smack value
3638 * for the packet fall back on the network
3642 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3643 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3646 * Compare the catsets. Use the netlbl APIs.
3648 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3649 if ((skp->smk_netlabel.flags &
3650 NETLBL_SECATTR_MLS_CAT) == 0)
3654 for (acat = -1, kcat = -1; acat == kcat; ) {
3655 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3657 kcat = netlbl_catmap_walk(
3658 skp->smk_netlabel.attr.mls.cat,
3660 if (acat < 0 || kcat < 0)
3673 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3674 return &smack_known_web;
3675 return &smack_known_star;
3677 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3679 * Looks like a fallback, which gives us a secid.
3681 return smack_from_secid(sap->attr.secid);
3683 * Without guidance regarding the smack value
3684 * for the packet fall back on the network
3687 return smack_net_ambient;
3690 #if IS_ENABLED(CONFIG_IPV6)
3691 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3695 int proto = -EINVAL;
3696 struct ipv6hdr _ipv6h;
3697 struct ipv6hdr *ip6;
3699 struct tcphdr _tcph, *th;
3700 struct udphdr _udph, *uh;
3701 struct dccp_hdr _dccph, *dh;
3705 offset = skb_network_offset(skb);
3706 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3709 sip->sin6_addr = ip6->saddr;
3711 nexthdr = ip6->nexthdr;
3712 offset += sizeof(_ipv6h);
3713 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3720 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3722 sip->sin6_port = th->source;
3725 case IPPROTO_UDPLITE:
3726 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3728 sip->sin6_port = uh->source;
3731 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3733 sip->sin6_port = dh->dccph_sport;
3738 #endif /* CONFIG_IPV6 */
3741 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3745 * Returns 0 if the packet should be delivered, an error code otherwise
3747 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3749 struct netlbl_lsm_secattr secattr;
3750 struct socket_smack *ssp = sk->sk_security;
3751 struct smack_known *skp = NULL;
3753 struct smk_audit_info ad;
3754 u16 family = sk->sk_family;
3756 struct lsm_network_audit net;
3758 #if IS_ENABLED(CONFIG_IPV6)
3759 struct sockaddr_in6 sadd;
3762 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3764 #endif /* CONFIG_IPV6 */
3768 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3770 * If there is a secmark use it rather than the CIPSO label.
3771 * If there is no secmark fall back to CIPSO.
3772 * The secmark is assumed to reflect policy better.
3774 if (skb && skb->secmark != 0) {
3775 skp = smack_from_secid(skb->secmark);
3778 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3780 * Translate what netlabel gave us.
3782 netlbl_secattr_init(&secattr);
3784 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3786 skp = smack_from_secattr(&secattr, ssp);
3788 skp = smack_net_ambient;
3790 netlbl_secattr_destroy(&secattr);
3792 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3796 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3797 ad.a.u.net->family = family;
3798 ad.a.u.net->netif = skb->skb_iif;
3799 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3802 * Receiving a packet requires that the other end
3803 * be able to write here. Read access is not required.
3804 * This is the simplist possible security model
3807 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3808 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3811 netlbl_skbuff_err(skb, family, rc, 0);
3813 #if IS_ENABLED(CONFIG_IPV6)
3815 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3816 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3817 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3819 #ifdef SMACK_IPV6_SECMARK_LABELING
3820 if (skb && skb->secmark != 0)
3821 skp = smack_from_secid(skb->secmark);
3823 skp = smack_ipv6host_label(&sadd);
3825 skp = smack_net_ambient;
3827 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3828 ad.a.u.net->family = family;
3829 ad.a.u.net->netif = skb->skb_iif;
3830 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3831 #endif /* CONFIG_AUDIT */
3832 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3833 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3835 #endif /* SMACK_IPV6_SECMARK_LABELING */
3836 #ifdef SMACK_IPV6_PORT_LABELING
3837 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3838 #endif /* SMACK_IPV6_PORT_LABELING */
3840 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3841 ICMPV6_ADM_PROHIBITED, 0);
3843 #endif /* CONFIG_IPV6 */
3850 * smack_socket_getpeersec_stream - pull in packet label
3852 * @optval: user's destination
3853 * @optlen: size thereof
3856 * returns zero on success, an error code otherwise
3858 static int smack_socket_getpeersec_stream(struct socket *sock,
3859 char __user *optval,
3860 int __user *optlen, unsigned len)
3862 struct socket_smack *ssp;
3867 ssp = sock->sk->sk_security;
3868 if (ssp->smk_packet != NULL) {
3869 rcp = ssp->smk_packet->smk_known;
3870 slen = strlen(rcp) + 1;
3875 else if (copy_to_user(optval, rcp, slen) != 0)
3878 if (put_user(slen, optlen) != 0)
3886 * smack_socket_getpeersec_dgram - pull in packet label
3887 * @sock: the peer socket
3889 * @secid: pointer to where to put the secid of the packet
3891 * Sets the netlabel socket state on sk from parent
3893 static int smack_socket_getpeersec_dgram(struct socket *sock,
3894 struct sk_buff *skb, u32 *secid)
3897 struct netlbl_lsm_secattr secattr;
3898 struct socket_smack *ssp = NULL;
3899 struct smack_known *skp;
3900 int family = PF_UNSPEC;
3901 u32 s = 0; /* 0 is the invalid secid */
3905 if (skb->protocol == htons(ETH_P_IP))
3907 #if IS_ENABLED(CONFIG_IPV6)
3908 else if (skb->protocol == htons(ETH_P_IPV6))
3910 #endif /* CONFIG_IPV6 */
3912 if (family == PF_UNSPEC && sock != NULL)
3913 family = sock->sk->sk_family;
3917 ssp = sock->sk->sk_security;
3918 s = ssp->smk_out->smk_secid;
3921 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3927 * Translate what netlabel gave us.
3929 if (sock != NULL && sock->sk != NULL)
3930 ssp = sock->sk->sk_security;
3931 netlbl_secattr_init(&secattr);
3932 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3934 skp = smack_from_secattr(&secattr, ssp);
3937 netlbl_secattr_destroy(&secattr);
3940 #ifdef SMACK_IPV6_SECMARK_LABELING
3952 * smack_sock_graft - Initialize a newly created socket with an existing sock
3954 * @parent: parent socket
3956 * Set the smk_{in,out} state of an existing sock based on the process that
3957 * is creating the new socket.
3959 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3961 struct socket_smack *ssp;
3962 struct smack_known *skp = smk_of_current();
3965 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3968 ssp = sk->sk_security;
3971 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3975 * smack_inet_conn_request - Smack access check on connect
3976 * @sk: socket involved
3980 * Returns 0 if a task with the packet label could write to
3981 * the socket, otherwise an error code
3983 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3984 struct request_sock *req)
3986 u16 family = sk->sk_family;
3987 struct smack_known *skp;
3988 struct socket_smack *ssp = sk->sk_security;
3989 struct netlbl_lsm_secattr secattr;
3990 struct sockaddr_in addr;
3992 struct smack_known *hskp;
3994 struct smk_audit_info ad;
3996 struct lsm_network_audit net;
3999 #if IS_ENABLED(CONFIG_IPV6)
4000 if (family == PF_INET6) {
4002 * Handle mapped IPv4 packets arriving
4003 * via IPv6 sockets. Don't set up netlabel
4004 * processing on IPv6.
4006 if (skb->protocol == htons(ETH_P_IP))
4011 #endif /* CONFIG_IPV6 */
4013 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4015 * If there is a secmark use it rather than the CIPSO label.
4016 * If there is no secmark fall back to CIPSO.
4017 * The secmark is assumed to reflect policy better.
4019 if (skb && skb->secmark != 0) {
4020 skp = smack_from_secid(skb->secmark);
4023 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4025 netlbl_secattr_init(&secattr);
4026 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4028 skp = smack_from_secattr(&secattr, ssp);
4030 skp = &smack_known_huh;
4031 netlbl_secattr_destroy(&secattr);
4033 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4038 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4039 ad.a.u.net->family = family;
4040 ad.a.u.net->netif = skb->skb_iif;
4041 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4044 * Receiving a packet requires that the other end be able to write
4045 * here. Read access is not required.
4047 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4048 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4053 * Save the peer's label in the request_sock so we can later setup
4054 * smk_packet in the child socket so that SO_PEERCRED can report it.
4056 req->peer_secid = skp->smk_secid;
4059 * We need to decide if we want to label the incoming connection here
4060 * if we do we only need to label the request_sock and the stack will
4061 * propagate the wire-label to the sock when it is created.
4064 addr.sin_addr.s_addr = hdr->saddr;
4066 hskp = smack_ipv4host_label(&addr);
4070 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4072 netlbl_req_delattr(req);
4078 * smack_inet_csk_clone - Copy the connection information to the new socket
4079 * @sk: the new socket
4080 * @req: the connection's request_sock
4082 * Transfer the connection's peer label to the newly created socket.
4084 static void smack_inet_csk_clone(struct sock *sk,
4085 const struct request_sock *req)
4087 struct socket_smack *ssp = sk->sk_security;
4088 struct smack_known *skp;
4090 if (req->peer_secid != 0) {
4091 skp = smack_from_secid(req->peer_secid);
4092 ssp->smk_packet = skp;
4094 ssp->smk_packet = NULL;
4098 * Key management security hooks
4100 * Casey has not tested key support very heavily.
4101 * The permission check is most likely too restrictive.
4102 * If you care about keys please have a look.
4107 * smack_key_alloc - Set the key security blob
4109 * @cred: the credentials to use
4112 * No allocation required
4116 static int smack_key_alloc(struct key *key, const struct cred *cred,
4117 unsigned long flags)
4119 struct smack_known *skp = smk_of_task(smack_cred(cred));
4121 key->security = skp;
4126 * smack_key_free - Clear the key security blob
4129 * Clear the blob pointer
4131 static void smack_key_free(struct key *key)
4133 key->security = NULL;
4137 * smack_key_permission - Smack access on a key
4138 * @key_ref: gets to the object
4139 * @cred: the credentials to use
4140 * @perm: requested key permissions
4142 * Return 0 if the task has read and write to the object,
4143 * an error code otherwise
4145 static int smack_key_permission(key_ref_t key_ref,
4146 const struct cred *cred, unsigned perm)
4149 struct smk_audit_info ad;
4150 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4155 * Validate requested permissions
4157 if (perm & ~KEY_NEED_ALL)
4160 keyp = key_ref_to_ptr(key_ref);
4164 * If the key hasn't been initialized give it access so that
4167 if (keyp->security == NULL)
4170 * This should not occur
4175 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4179 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4180 ad.a.u.key_struct.key = keyp->serial;
4181 ad.a.u.key_struct.key_desc = keyp->description;
4183 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4184 request |= MAY_READ;
4185 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4186 request |= MAY_WRITE;
4187 rc = smk_access(tkp, keyp->security, request, &ad);
4188 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4193 * smack_key_getsecurity - Smack label tagging the key
4194 * @key points to the key to be queried
4195 * @_buffer points to a pointer that should be set to point to the
4196 * resulting string (if no label or an error occurs).
4197 * Return the length of the string (including terminating NUL) or -ve if
4199 * May also return 0 (and a NULL buffer pointer) if there is no label.
4201 static int smack_key_getsecurity(struct key *key, char **_buffer)
4203 struct smack_known *skp = key->security;
4207 if (key->security == NULL) {
4212 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4215 length = strlen(copy) + 1;
4221 #endif /* CONFIG_KEYS */
4226 * Audit requires a unique representation of each Smack specific
4227 * rule. This unique representation is used to distinguish the
4228 * object to be audited from remaining kernel objects and also
4229 * works as a glue between the audit hooks.
4231 * Since repository entries are added but never deleted, we'll use
4232 * the smack_known label address related to the given audit rule as
4233 * the needed unique representation. This also better fits the smack
4234 * model where nearly everything is a label.
4239 * smack_audit_rule_init - Initialize a smack audit rule
4240 * @field: audit rule fields given from user-space (audit.h)
4241 * @op: required testing operator (=, !=, >, <, ...)
4242 * @rulestr: smack label to be audited
4243 * @vrule: pointer to save our own audit rule representation
4245 * Prepare to audit cases where (@field @op @rulestr) is true.
4246 * The label to be audited is created if necessay.
4248 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4250 struct smack_known *skp;
4251 char **rule = (char **)vrule;
4254 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4257 if (op != Audit_equal && op != Audit_not_equal)
4260 skp = smk_import_entry(rulestr, 0);
4262 return PTR_ERR(skp);
4264 *rule = skp->smk_known;
4270 * smack_audit_rule_known - Distinguish Smack audit rules
4271 * @krule: rule of interest, in Audit kernel representation format
4273 * This is used to filter Smack rules from remaining Audit ones.
4274 * If it's proved that this rule belongs to us, the
4275 * audit_rule_match hook will be called to do the final judgement.
4277 static int smack_audit_rule_known(struct audit_krule *krule)
4279 struct audit_field *f;
4282 for (i = 0; i < krule->field_count; i++) {
4283 f = &krule->fields[i];
4285 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4293 * smack_audit_rule_match - Audit given object ?
4294 * @secid: security id for identifying the object to test
4295 * @field: audit rule flags given from user-space
4296 * @op: required testing operator
4297 * @vrule: smack internal rule presentation
4299 * The core Audit hook. It's used to take the decision of
4300 * whether to audit or not to audit a given object.
4302 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4304 struct smack_known *skp;
4307 if (unlikely(!rule)) {
4308 WARN_ONCE(1, "Smack: missing rule\n");
4312 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4315 skp = smack_from_secid(secid);
4318 * No need to do string comparisons. If a match occurs,
4319 * both pointers will point to the same smack_known
4322 if (op == Audit_equal)
4323 return (rule == skp->smk_known);
4324 if (op == Audit_not_equal)
4325 return (rule != skp->smk_known);
4331 * There is no need for a smack_audit_rule_free hook.
4332 * No memory was allocated.
4335 #endif /* CONFIG_AUDIT */
4338 * smack_ismaclabel - check if xattr @name references a smack MAC label
4339 * @name: Full xattr name to check.
4341 static int smack_ismaclabel(const char *name)
4343 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4348 * smack_secid_to_secctx - return the smack label for a secid
4349 * @secid: incoming integer
4350 * @secdata: destination
4351 * @seclen: how long it is
4353 * Exists for networking code.
4355 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4357 struct smack_known *skp = smack_from_secid(secid);
4360 *secdata = skp->smk_known;
4361 *seclen = strlen(skp->smk_known);
4366 * smack_secctx_to_secid - return the secid for a smack label
4367 * @secdata: smack label
4368 * @seclen: how long result is
4369 * @secid: outgoing integer
4371 * Exists for audit and networking code.
4373 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4375 struct smack_known *skp = smk_find_entry(secdata);
4378 *secid = skp->smk_secid;
4385 * There used to be a smack_release_secctx hook
4386 * that did nothing back when hooks were in a vector.
4387 * Now that there's a list such a hook adds cost.
4390 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4392 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4395 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4397 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4400 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4402 struct smack_known *skp = smk_of_inode(inode);
4404 *ctx = skp->smk_known;
4405 *ctxlen = strlen(skp->smk_known);
4409 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4412 struct task_smack *tsp;
4413 struct smack_known *skp;
4414 struct inode_smack *isp;
4415 struct cred *new_creds = *new;
4417 if (new_creds == NULL) {
4418 new_creds = prepare_creds();
4419 if (new_creds == NULL)
4423 tsp = smack_cred(new_creds);
4426 * Get label from overlay inode and set it in create_sid
4428 isp = smack_inode(d_inode(dentry->d_parent));
4429 skp = isp->smk_inode;
4430 tsp->smk_task = skp;
4435 static int smack_inode_copy_up_xattr(const char *name)
4438 * Return 1 if this is the smack access Smack attribute.
4440 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4446 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4448 const struct cred *old,
4451 struct task_smack *otsp = smack_cred(old);
4452 struct task_smack *ntsp = smack_cred(new);
4453 struct inode_smack *isp;
4457 * Use the process credential unless all of
4458 * the transmuting criteria are met
4460 ntsp->smk_task = otsp->smk_task;
4463 * the attribute of the containing directory
4465 isp = smack_inode(d_inode(dentry->d_parent));
4467 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4469 may = smk_access_entry(otsp->smk_task->smk_known,
4470 isp->smk_inode->smk_known,
4471 &otsp->smk_task->smk_rules);
4475 * If the directory is transmuting and the rule
4476 * providing access is transmuting use the containing
4477 * directory label instead of the process label.
4479 if (may > 0 && (may & MAY_TRANSMUTE))
4480 ntsp->smk_task = isp->smk_inode;
4485 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4486 .lbs_cred = sizeof(struct task_smack),
4487 .lbs_file = sizeof(struct smack_known *),
4488 .lbs_inode = sizeof(struct inode_smack),
4489 .lbs_ipc = sizeof(struct smack_known *),
4490 .lbs_msg_msg = sizeof(struct smack_known *),
4493 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4494 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4495 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4496 LSM_HOOK_INIT(syslog, smack_syslog),
4498 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4499 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4500 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4501 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4502 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4503 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4505 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4507 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4508 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4509 LSM_HOOK_INIT(inode_link, smack_inode_link),
4510 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4511 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4512 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4513 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4514 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4515 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4516 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4517 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4518 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4519 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4520 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4521 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4522 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4523 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4525 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4526 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4527 LSM_HOOK_INIT(file_lock, smack_file_lock),
4528 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4529 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4530 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4531 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4532 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4533 LSM_HOOK_INIT(file_receive, smack_file_receive),
4535 LSM_HOOK_INIT(file_open, smack_file_open),
4537 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4538 LSM_HOOK_INIT(cred_free, smack_cred_free),
4539 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4540 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4541 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4542 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4543 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4544 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4545 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4546 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4547 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4548 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4549 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4550 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4551 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4552 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4553 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4554 LSM_HOOK_INIT(task_kill, smack_task_kill),
4555 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4557 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4558 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4560 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4562 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4563 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4564 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4565 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4566 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4568 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4569 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4570 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4571 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4573 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4574 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4575 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4576 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4578 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4580 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4581 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4583 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4584 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4586 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4587 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4588 #ifdef SMACK_IPV6_PORT_LABELING
4589 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4591 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4592 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4593 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4594 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4595 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4596 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4597 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4598 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4599 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4600 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4602 /* key management security hooks */
4604 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4605 LSM_HOOK_INIT(key_free, smack_key_free),
4606 LSM_HOOK_INIT(key_permission, smack_key_permission),
4607 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4608 #endif /* CONFIG_KEYS */
4612 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4613 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4614 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4615 #endif /* CONFIG_AUDIT */
4617 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4618 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4619 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4620 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4621 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4622 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4623 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4624 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4625 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4629 static __init void init_smack_known_list(void)
4632 * Initialize rule list locks
4634 mutex_init(&smack_known_huh.smk_rules_lock);
4635 mutex_init(&smack_known_hat.smk_rules_lock);
4636 mutex_init(&smack_known_floor.smk_rules_lock);
4637 mutex_init(&smack_known_star.smk_rules_lock);
4638 mutex_init(&smack_known_web.smk_rules_lock);
4640 * Initialize rule lists
4642 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4643 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4644 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4645 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4646 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4648 * Create the known labels list
4650 smk_insert_entry(&smack_known_huh);
4651 smk_insert_entry(&smack_known_hat);
4652 smk_insert_entry(&smack_known_star);
4653 smk_insert_entry(&smack_known_floor);
4654 smk_insert_entry(&smack_known_web);
4658 * smack_init - initialize the smack system
4662 static __init int smack_init(void)
4664 struct cred *cred = (struct cred *) current->cred;
4665 struct task_smack *tsp;
4667 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4668 if (!smack_inode_cache)
4672 * Set the security state for the initial task.
4674 tsp = smack_cred(cred);
4675 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4680 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4683 pr_info("Smack: Initializing.\n");
4684 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4685 pr_info("Smack: Netfilter enabled.\n");
4687 #ifdef SMACK_IPV6_PORT_LABELING
4688 pr_info("Smack: IPv6 port labeling enabled.\n");
4690 #ifdef SMACK_IPV6_SECMARK_LABELING
4691 pr_info("Smack: IPv6 Netfilter enabled.\n");
4694 /* initialize the smack_known_list */
4695 init_smack_known_list();
4701 * Smack requires early initialization in order to label
4702 * all processes and objects when they are created.
4704 DEFINE_LSM(smack) = {
4706 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4707 .blobs = &smack_blob_sizes,