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 = current_security();
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 = current_security();
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 = current_security();
186 struct inode_smack *isp = inode->i_security;
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 = current_security();
216 struct smack_known *sskp = tsp->smk_task;
217 struct inode *inode = file_inode(file);
218 struct inode_smack *isp = inode->i_security;
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 = cred->security;
246 struct smack_known *sskp = tsp->smk_task;
247 struct inode *inode = file_inode(file);
248 struct inode_smack *isp = inode->i_security;
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 * new_inode_smack - allocate an inode security blob
309 * @skp: a pointer to the Smack label entry to use in the blob
311 * Returns the new blob or NULL if there's no memory available
313 static struct inode_smack *new_inode_smack(struct smack_known *skp)
315 struct inode_smack *isp;
317 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
321 isp->smk_inode = skp;
323 mutex_init(&isp->smk_lock);
329 * new_task_smack - allocate a task security blob
330 * @task: a pointer to the Smack label for the running task
331 * @forked: a pointer to the Smack label for the forked task
332 * @gfp: type of the memory for the allocation
334 * Returns the new blob or NULL if there's no memory available
336 static struct task_smack *new_task_smack(struct smack_known *task,
337 struct smack_known *forked, gfp_t gfp)
339 struct task_smack *tsp;
341 tsp = kzalloc(sizeof(struct task_smack), gfp);
345 tsp->smk_task = task;
346 tsp->smk_forked = forked;
347 INIT_LIST_HEAD(&tsp->smk_rules);
348 INIT_LIST_HEAD(&tsp->smk_relabel);
349 mutex_init(&tsp->smk_rules_lock);
355 * smk_copy_rules - copy a rule set
356 * @nhead: new rules header pointer
357 * @ohead: old rules header pointer
358 * @gfp: type of the memory for the allocation
360 * Returns 0 on success, -ENOMEM on error
362 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
365 struct smack_rule *nrp;
366 struct smack_rule *orp;
369 list_for_each_entry_rcu(orp, ohead, list) {
370 nrp = kzalloc(sizeof(struct smack_rule), gfp);
376 list_add_rcu(&nrp->list, nhead);
382 * smk_copy_relabel - copy smk_relabel labels list
383 * @nhead: new rules header pointer
384 * @ohead: old rules header pointer
385 * @gfp: type of the memory for the allocation
387 * Returns 0 on success, -ENOMEM on error
389 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
392 struct smack_known_list_elem *nklep;
393 struct smack_known_list_elem *oklep;
395 list_for_each_entry(oklep, ohead, list) {
396 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
398 smk_destroy_label_list(nhead);
401 nklep->smk_label = oklep->smk_label;
402 list_add(&nklep->list, nhead);
409 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
410 * @mode - input mode in form of PTRACE_MODE_*
412 * Returns a converted MAY_* mode usable by smack rules
414 static inline unsigned int smk_ptrace_mode(unsigned int mode)
416 if (mode & PTRACE_MODE_ATTACH)
417 return MAY_READWRITE;
418 if (mode & PTRACE_MODE_READ)
425 * smk_ptrace_rule_check - helper for ptrace access
426 * @tracer: tracer process
427 * @tracee_known: label entry of the process that's about to be traced
428 * @mode: ptrace attachment mode (PTRACE_MODE_*)
429 * @func: name of the function that called us, used for audit
431 * Returns 0 on access granted, -error on error
433 static int smk_ptrace_rule_check(struct task_struct *tracer,
434 struct smack_known *tracee_known,
435 unsigned int mode, const char *func)
438 struct smk_audit_info ad, *saip = NULL;
439 struct task_smack *tsp;
440 struct smack_known *tracer_known;
441 const struct cred *tracercred;
443 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
444 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
445 smk_ad_setfield_u_tsk(&ad, tracer);
450 tracercred = __task_cred(tracer);
451 tsp = tracercred->security;
452 tracer_known = smk_of_task(tsp);
454 if ((mode & PTRACE_MODE_ATTACH) &&
455 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
456 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
457 if (tracer_known->smk_known == tracee_known->smk_known)
459 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
461 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
467 smack_log(tracer_known->smk_known,
468 tracee_known->smk_known,
475 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
476 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
484 * We he, that is fun!
488 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
489 * @ctp: child task pointer
490 * @mode: ptrace attachment mode (PTRACE_MODE_*)
492 * Returns 0 if access is OK, an error code otherwise
494 * Do the capability checks.
496 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
498 struct smack_known *skp;
500 skp = smk_of_task_struct(ctp);
502 return smk_ptrace_rule_check(current, skp, mode, __func__);
506 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
507 * @ptp: parent task pointer
509 * Returns 0 if access is OK, an error code otherwise
511 * Do the capability checks, and require PTRACE_MODE_ATTACH.
513 static int smack_ptrace_traceme(struct task_struct *ptp)
516 struct smack_known *skp;
518 skp = smk_of_task(current_security());
520 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
525 * smack_syslog - Smack approval on syslog
526 * @type: message type
528 * Returns 0 on success, error code otherwise.
530 static int smack_syslog(int typefrom_file)
533 struct smack_known *skp = smk_of_current();
535 if (smack_privileged(CAP_MAC_OVERRIDE))
538 if (smack_syslog_label != NULL && smack_syslog_label != skp)
550 * smack_sb_alloc_security - allocate a superblock blob
551 * @sb: the superblock getting the blob
553 * Returns 0 on success or -ENOMEM on error.
555 static int smack_sb_alloc_security(struct super_block *sb)
557 struct superblock_smack *sbsp;
559 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
564 sbsp->smk_root = &smack_known_floor;
565 sbsp->smk_default = &smack_known_floor;
566 sbsp->smk_floor = &smack_known_floor;
567 sbsp->smk_hat = &smack_known_hat;
569 * SMK_SB_INITIALIZED will be zero from kzalloc.
571 sb->s_security = sbsp;
577 * smack_sb_free_security - free a superblock blob
578 * @sb: the superblock getting the blob
581 static void smack_sb_free_security(struct super_block *sb)
583 kfree(sb->s_security);
584 sb->s_security = NULL;
587 struct smack_mnt_opts {
588 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
591 static void smack_free_mnt_opts(void *mnt_opts)
593 struct smack_mnt_opts *opts = mnt_opts;
594 kfree(opts->fsdefault);
595 kfree(opts->fsfloor);
598 kfree(opts->fstransmute);
602 static int smack_add_opt(int token, const char *s, void **mnt_opts)
604 struct smack_mnt_opts *opts = *mnt_opts;
607 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
636 case Opt_fstransmute:
637 if (opts->fstransmute)
639 opts->fstransmute = s;
645 pr_warn("Smack: duplicate mount options\n");
649 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
651 char *from = options, *to = options;
655 char *next = strchr(from, ',');
664 token = match_opt_prefix(from, len, &arg);
665 if (token != Opt_error) {
666 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
667 rc = smack_add_opt(token, arg, mnt_opts);
671 smack_free_mnt_opts(*mnt_opts);
676 if (!first) { // copy with preceding comma
681 memmove(to, from, len);
694 * smack_set_mnt_opts - set Smack specific mount options
695 * @sb: the file system superblock
696 * @opts: Smack mount options
697 * @kern_flags: mount option from kernel space or user space
698 * @set_kern_flags: where to store converted mount opts
700 * Returns 0 on success, an error code on failure
702 * Allow filesystems with binary mount data to explicitly set Smack mount
705 static int smack_set_mnt_opts(struct super_block *sb,
707 unsigned long kern_flags,
708 unsigned long *set_kern_flags)
710 struct dentry *root = sb->s_root;
711 struct inode *inode = d_backing_inode(root);
712 struct superblock_smack *sp = sb->s_security;
713 struct inode_smack *isp;
714 struct smack_known *skp;
715 struct smack_mnt_opts *opts = mnt_opts;
716 bool transmute = false;
718 if (sp->smk_flags & SMK_SB_INITIALIZED)
721 if (!smack_privileged(CAP_MAC_ADMIN)) {
723 * Unprivileged mounts don't get to specify Smack values.
728 * Unprivileged mounts get root and default from the caller.
730 skp = smk_of_current();
732 sp->smk_default = skp;
734 * For a handful of fs types with no user-controlled
735 * backing store it's okay to trust security labels
736 * in the filesystem. The rest are untrusted.
738 if (sb->s_user_ns != &init_user_ns &&
739 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
740 sb->s_magic != RAMFS_MAGIC) {
742 sp->smk_flags |= SMK_SB_UNTRUSTED;
746 sp->smk_flags |= SMK_SB_INITIALIZED;
749 if (opts->fsdefault) {
750 skp = smk_import_entry(opts->fsdefault, 0);
753 sp->smk_default = skp;
756 skp = smk_import_entry(opts->fsfloor, 0);
762 skp = smk_import_entry(opts->fshat, 0);
768 skp = smk_import_entry(opts->fsroot, 0);
773 if (opts->fstransmute) {
774 skp = smk_import_entry(opts->fstransmute, 0);
783 * Initialize the root inode.
785 isp = inode->i_security;
787 isp = new_inode_smack(sp->smk_root);
790 inode->i_security = isp;
792 isp->smk_inode = sp->smk_root;
795 isp->smk_flags |= SMK_INODE_TRANSMUTE;
801 * smack_sb_statfs - Smack check on statfs
802 * @dentry: identifies the file system in question
804 * Returns 0 if current can read the floor of the filesystem,
805 * and error code otherwise
807 static int smack_sb_statfs(struct dentry *dentry)
809 struct superblock_smack *sbp = dentry->d_sb->s_security;
811 struct smk_audit_info ad;
813 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
814 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
816 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
817 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
826 * smack_bprm_set_creds - set creds for exec
827 * @bprm: the exec information
829 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
831 static int smack_bprm_set_creds(struct linux_binprm *bprm)
833 struct inode *inode = file_inode(bprm->file);
834 struct task_smack *bsp = bprm->cred->security;
835 struct inode_smack *isp;
836 struct superblock_smack *sbsp;
839 if (bprm->called_set_creds)
842 isp = inode->i_security;
843 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
846 sbsp = inode->i_sb->s_security;
847 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
848 isp->smk_task != sbsp->smk_root)
851 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
852 struct task_struct *tracer;
856 tracer = ptrace_parent(current);
857 if (likely(tracer != NULL))
858 rc = smk_ptrace_rule_check(tracer,
866 } else if (bprm->unsafe)
869 bsp->smk_task = isp->smk_task;
870 bprm->per_clear |= PER_CLEAR_ON_SETID;
872 /* Decide if this is a secure exec. */
873 if (bsp->smk_task != bsp->smk_forked)
874 bprm->secureexec = 1;
884 * smack_inode_alloc_security - allocate an inode blob
885 * @inode: the inode in need of a blob
887 * Returns 0 if it gets a blob, -ENOMEM otherwise
889 static int smack_inode_alloc_security(struct inode *inode)
891 struct smack_known *skp = smk_of_current();
893 inode->i_security = new_inode_smack(skp);
894 if (inode->i_security == NULL)
900 * smack_inode_free_rcu - Free inode_smack blob from cache
901 * @head: the rcu_head for getting inode_smack pointer
903 * Call back function called from call_rcu() to free
904 * the i_security blob pointer in inode
906 static void smack_inode_free_rcu(struct rcu_head *head)
908 struct inode_smack *issp;
910 issp = container_of(head, struct inode_smack, smk_rcu);
911 kmem_cache_free(smack_inode_cache, issp);
915 * smack_inode_free_security - free an inode blob using call_rcu()
916 * @inode: the inode with a blob
918 * Clears the blob pointer in inode using RCU
920 static void smack_inode_free_security(struct inode *inode)
922 struct inode_smack *issp = inode->i_security;
925 * The inode may still be referenced in a path walk and
926 * a call to smack_inode_permission() can be made
927 * after smack_inode_free_security() is called.
928 * To avoid race condition free the i_security via RCU
929 * and leave the current inode->i_security pointer intact.
930 * The inode will be freed after the RCU grace period too.
932 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
936 * smack_inode_init_security - copy out the smack from an inode
937 * @inode: the newly created inode
938 * @dir: containing directory object
940 * @name: where to put the attribute name
941 * @value: where to put the attribute value
942 * @len: where to put the length of the attribute
944 * Returns 0 if it all works out, -ENOMEM if there's no memory
946 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
947 const struct qstr *qstr, const char **name,
948 void **value, size_t *len)
950 struct inode_smack *issp = inode->i_security;
951 struct smack_known *skp = smk_of_current();
952 struct smack_known *isp = smk_of_inode(inode);
953 struct smack_known *dsp = smk_of_inode(dir);
957 *name = XATTR_SMACK_SUFFIX;
961 may = smk_access_entry(skp->smk_known, dsp->smk_known,
966 * If the access rule allows transmutation and
967 * the directory requests transmutation then
968 * by all means transmute.
969 * Mark the inode as changed.
971 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
972 smk_inode_transmutable(dir)) {
974 issp->smk_flags |= SMK_INODE_CHANGED;
977 *value = kstrdup(isp->smk_known, GFP_NOFS);
981 *len = strlen(isp->smk_known);
988 * smack_inode_link - Smack check on link
989 * @old_dentry: the existing object
991 * @new_dentry: the new object
993 * Returns 0 if access is permitted, an error code otherwise
995 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
996 struct dentry *new_dentry)
998 struct smack_known *isp;
999 struct smk_audit_info ad;
1002 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1003 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1005 isp = smk_of_inode(d_backing_inode(old_dentry));
1006 rc = smk_curacc(isp, MAY_WRITE, &ad);
1007 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1009 if (rc == 0 && d_is_positive(new_dentry)) {
1010 isp = smk_of_inode(d_backing_inode(new_dentry));
1011 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1012 rc = smk_curacc(isp, MAY_WRITE, &ad);
1013 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1020 * smack_inode_unlink - Smack check on inode deletion
1021 * @dir: containing directory object
1022 * @dentry: file to unlink
1024 * Returns 0 if current can write the containing directory
1025 * and the object, error code otherwise
1027 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1029 struct inode *ip = d_backing_inode(dentry);
1030 struct smk_audit_info ad;
1033 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1034 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1037 * You need write access to the thing you're unlinking
1039 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1040 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1043 * You also need write access to the containing directory
1045 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1046 smk_ad_setfield_u_fs_inode(&ad, dir);
1047 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1048 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1054 * smack_inode_rmdir - Smack check on directory deletion
1055 * @dir: containing directory object
1056 * @dentry: directory to unlink
1058 * Returns 0 if current can write the containing directory
1059 * and the directory, error code otherwise
1061 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1063 struct smk_audit_info ad;
1066 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1067 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1070 * You need write access to the thing you're removing
1072 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1073 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1076 * You also need write access to the containing directory
1078 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1079 smk_ad_setfield_u_fs_inode(&ad, dir);
1080 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1081 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1088 * smack_inode_rename - Smack check on rename
1089 * @old_inode: unused
1090 * @old_dentry: the old object
1091 * @new_inode: unused
1092 * @new_dentry: the new object
1094 * Read and write access is required on both the old and
1097 * Returns 0 if access is permitted, an error code otherwise
1099 static int smack_inode_rename(struct inode *old_inode,
1100 struct dentry *old_dentry,
1101 struct inode *new_inode,
1102 struct dentry *new_dentry)
1105 struct smack_known *isp;
1106 struct smk_audit_info ad;
1108 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1109 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1111 isp = smk_of_inode(d_backing_inode(old_dentry));
1112 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1113 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1115 if (rc == 0 && d_is_positive(new_dentry)) {
1116 isp = smk_of_inode(d_backing_inode(new_dentry));
1117 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1118 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1119 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1125 * smack_inode_permission - Smack version of permission()
1126 * @inode: the inode in question
1127 * @mask: the access requested
1129 * This is the important Smack hook.
1131 * Returns 0 if access is permitted, -EACCES otherwise
1133 static int smack_inode_permission(struct inode *inode, int mask)
1135 struct superblock_smack *sbsp = inode->i_sb->s_security;
1136 struct smk_audit_info ad;
1137 int no_block = mask & MAY_NOT_BLOCK;
1140 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1142 * No permission to check. Existence test. Yup, it's there.
1147 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1148 if (smk_of_inode(inode) != sbsp->smk_root)
1152 /* May be droppable after audit */
1155 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1156 smk_ad_setfield_u_fs_inode(&ad, inode);
1157 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1158 rc = smk_bu_inode(inode, mask, rc);
1163 * smack_inode_setattr - Smack check for setting attributes
1164 * @dentry: the object
1165 * @iattr: for the force flag
1167 * Returns 0 if access is permitted, an error code otherwise
1169 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1171 struct smk_audit_info ad;
1175 * Need to allow for clearing the setuid bit.
1177 if (iattr->ia_valid & ATTR_FORCE)
1179 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1180 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1182 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1183 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1188 * smack_inode_getattr - Smack check for getting attributes
1189 * @mnt: vfsmount of the object
1190 * @dentry: the object
1192 * Returns 0 if access is permitted, an error code otherwise
1194 static int smack_inode_getattr(const struct path *path)
1196 struct smk_audit_info ad;
1197 struct inode *inode = d_backing_inode(path->dentry);
1200 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1201 smk_ad_setfield_u_fs_path(&ad, *path);
1202 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1203 rc = smk_bu_inode(inode, MAY_READ, rc);
1208 * smack_inode_setxattr - Smack check for setting xattrs
1209 * @dentry: the object
1210 * @name: name of the attribute
1211 * @value: value of the attribute
1212 * @size: size of the value
1215 * This protects the Smack attribute explicitly.
1217 * Returns 0 if access is permitted, an error code otherwise
1219 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1220 const void *value, size_t size, int flags)
1222 struct smk_audit_info ad;
1223 struct smack_known *skp;
1225 int check_import = 0;
1230 * Check label validity here so import won't fail in post_setxattr
1232 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1233 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1234 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1237 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1238 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1242 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1244 if (size != TRANS_TRUE_SIZE ||
1245 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1248 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1250 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1253 if (rc == 0 && check_import) {
1254 skp = size ? smk_import_entry(value, size) : NULL;
1257 else if (skp == NULL || (check_star &&
1258 (skp == &smack_known_star || skp == &smack_known_web)))
1262 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1263 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1266 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1267 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1274 * smack_inode_post_setxattr - Apply the Smack update approved above
1276 * @name: attribute name
1277 * @value: attribute value
1278 * @size: attribute size
1281 * Set the pointer in the inode blob to the entry found
1282 * in the master label list.
1284 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1285 const void *value, size_t size, int flags)
1287 struct smack_known *skp;
1288 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
1290 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1291 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1295 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1296 skp = smk_import_entry(value, size);
1298 isp->smk_inode = skp;
1299 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1300 skp = smk_import_entry(value, size);
1302 isp->smk_task = skp;
1303 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1304 skp = smk_import_entry(value, size);
1306 isp->smk_mmap = skp;
1313 * smack_inode_getxattr - Smack check on getxattr
1314 * @dentry: the object
1317 * Returns 0 if access is permitted, an error code otherwise
1319 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1321 struct smk_audit_info ad;
1324 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1325 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1327 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1328 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1333 * smack_inode_removexattr - Smack check on removexattr
1334 * @dentry: the object
1335 * @name: name of the attribute
1337 * Removing the Smack attribute requires CAP_MAC_ADMIN
1339 * Returns 0 if access is permitted, an error code otherwise
1341 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1343 struct inode_smack *isp;
1344 struct smk_audit_info ad;
1347 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1348 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1349 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1350 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1351 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1352 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1353 if (!smack_privileged(CAP_MAC_ADMIN))
1356 rc = cap_inode_removexattr(dentry, name);
1361 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1362 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1364 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1365 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1369 isp = d_backing_inode(dentry)->i_security;
1371 * Don't do anything special for these.
1372 * XATTR_NAME_SMACKIPIN
1373 * XATTR_NAME_SMACKIPOUT
1375 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1376 struct super_block *sbp = dentry->d_sb;
1377 struct superblock_smack *sbsp = sbp->s_security;
1379 isp->smk_inode = sbsp->smk_default;
1380 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1381 isp->smk_task = NULL;
1382 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1383 isp->smk_mmap = NULL;
1384 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1385 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1391 * smack_inode_getsecurity - get smack xattrs
1392 * @inode: the object
1393 * @name: attribute name
1394 * @buffer: where to put the result
1395 * @alloc: duplicate memory
1397 * Returns the size of the attribute or an error code
1399 static int smack_inode_getsecurity(struct inode *inode,
1400 const char *name, void **buffer,
1403 struct socket_smack *ssp;
1404 struct socket *sock;
1405 struct super_block *sbp;
1406 struct inode *ip = (struct inode *)inode;
1407 struct smack_known *isp;
1409 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1410 isp = smk_of_inode(inode);
1413 * The rest of the Smack xattrs are only on sockets.
1416 if (sbp->s_magic != SOCKFS_MAGIC)
1419 sock = SOCKET_I(ip);
1420 if (sock == NULL || sock->sk == NULL)
1423 ssp = sock->sk->sk_security;
1425 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1427 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1434 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1435 if (*buffer == NULL)
1439 return strlen(isp->smk_known);
1444 * smack_inode_listsecurity - list the Smack attributes
1445 * @inode: the object
1446 * @buffer: where they go
1447 * @buffer_size: size of buffer
1449 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1452 int len = sizeof(XATTR_NAME_SMACK);
1454 if (buffer != NULL && len <= buffer_size)
1455 memcpy(buffer, XATTR_NAME_SMACK, len);
1461 * smack_inode_getsecid - Extract inode's security id
1462 * @inode: inode to extract the info from
1463 * @secid: where result will be saved
1465 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1467 struct smack_known *skp = smk_of_inode(inode);
1469 *secid = skp->smk_secid;
1477 * There is no smack_file_permission hook
1479 * Should access checks be done on each read or write?
1480 * UNICOS and SELinux say yes.
1481 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1483 * I'll say no for now. Smack does not do the frequent
1484 * label changing that SELinux does.
1488 * smack_file_alloc_security - assign a file security blob
1491 * The security blob for a file is a pointer to the master
1492 * label list, so no allocation is done.
1494 * f_security is the owner security information. It
1495 * isn't used on file access checks, it's for send_sigio.
1499 static int smack_file_alloc_security(struct file *file)
1501 struct smack_known *skp = smk_of_current();
1503 file->f_security = skp;
1508 * smack_file_free_security - clear a file security blob
1511 * The security blob for a file is a pointer to the master
1512 * label list, so no memory is freed.
1514 static void smack_file_free_security(struct file *file)
1516 file->f_security = NULL;
1520 * smack_file_ioctl - Smack check on ioctls
1525 * Relies heavily on the correct use of the ioctl command conventions.
1527 * Returns 0 if allowed, error code otherwise
1529 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1533 struct smk_audit_info ad;
1534 struct inode *inode = file_inode(file);
1536 if (unlikely(IS_PRIVATE(inode)))
1539 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1540 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1542 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1543 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1544 rc = smk_bu_file(file, MAY_WRITE, rc);
1547 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1548 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1549 rc = smk_bu_file(file, MAY_READ, rc);
1556 * smack_file_lock - Smack check on file locking
1560 * Returns 0 if current has lock access, error code otherwise
1562 static int smack_file_lock(struct file *file, unsigned int cmd)
1564 struct smk_audit_info ad;
1566 struct inode *inode = file_inode(file);
1568 if (unlikely(IS_PRIVATE(inode)))
1571 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1572 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1573 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1574 rc = smk_bu_file(file, MAY_LOCK, rc);
1579 * smack_file_fcntl - Smack check on fcntl
1581 * @cmd: what action to check
1584 * Generally these operations are harmless.
1585 * File locking operations present an obvious mechanism
1586 * for passing information, so they require write access.
1588 * Returns 0 if current has access, error code otherwise
1590 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1593 struct smk_audit_info ad;
1595 struct inode *inode = file_inode(file);
1597 if (unlikely(IS_PRIVATE(inode)))
1605 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1606 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1607 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1608 rc = smk_bu_file(file, MAY_LOCK, rc);
1612 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1613 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1614 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1615 rc = smk_bu_file(file, MAY_WRITE, rc);
1626 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1627 * if mapping anonymous memory.
1628 * @file contains the file structure for file to map (may be NULL).
1629 * @reqprot contains the protection requested by the application.
1630 * @prot contains the protection that will be applied by the kernel.
1631 * @flags contains the operational flags.
1632 * Return 0 if permission is granted.
1634 static int smack_mmap_file(struct file *file,
1635 unsigned long reqprot, unsigned long prot,
1636 unsigned long flags)
1638 struct smack_known *skp;
1639 struct smack_known *mkp;
1640 struct smack_rule *srp;
1641 struct task_smack *tsp;
1642 struct smack_known *okp;
1643 struct inode_smack *isp;
1644 struct superblock_smack *sbsp;
1653 if (unlikely(IS_PRIVATE(file_inode(file))))
1656 isp = file_inode(file)->i_security;
1657 if (isp->smk_mmap == NULL)
1659 sbsp = file_inode(file)->i_sb->s_security;
1660 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1661 isp->smk_mmap != sbsp->smk_root)
1663 mkp = isp->smk_mmap;
1665 tsp = current_security();
1666 skp = smk_of_current();
1671 * For each Smack rule associated with the subject
1672 * label verify that the SMACK64MMAP also has access
1673 * to that rule's object label.
1675 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1676 okp = srp->smk_object;
1678 * Matching labels always allows access.
1680 if (mkp->smk_known == okp->smk_known)
1683 * If there is a matching local rule take
1684 * that into account as well.
1686 may = smk_access_entry(srp->smk_subject->smk_known,
1690 may = srp->smk_access;
1692 may &= srp->smk_access;
1694 * If may is zero the SMACK64MMAP subject can't
1695 * possibly have less access.
1701 * Fetch the global list entry.
1702 * If there isn't one a SMACK64MMAP subject
1703 * can't have as much access as current.
1705 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1707 if (mmay == -ENOENT) {
1712 * If there is a local entry it modifies the
1713 * potential access, too.
1715 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1717 if (tmay != -ENOENT)
1721 * If there is any access available to current that is
1722 * not available to a SMACK64MMAP subject
1725 if ((may | mmay) != mmay) {
1737 * smack_file_set_fowner - set the file security blob value
1738 * @file: object in question
1741 static void smack_file_set_fowner(struct file *file)
1743 file->f_security = smk_of_current();
1747 * smack_file_send_sigiotask - Smack on sigio
1748 * @tsk: The target task
1749 * @fown: the object the signal come from
1752 * Allow a privileged task to get signals even if it shouldn't
1754 * Returns 0 if a subject with the object's smack could
1755 * write to the task, an error code otherwise.
1757 static int smack_file_send_sigiotask(struct task_struct *tsk,
1758 struct fown_struct *fown, int signum)
1760 struct smack_known *skp;
1761 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1762 const struct cred *tcred;
1765 struct smk_audit_info ad;
1768 * struct fown_struct is never outside the context of a struct file
1770 file = container_of(fown, struct file, f_owner);
1772 /* we don't log here as rc can be overriden */
1773 skp = file->f_security;
1774 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1775 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1778 tcred = __task_cred(tsk);
1779 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1783 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1784 smk_ad_setfield_u_tsk(&ad, tsk);
1785 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1790 * smack_file_receive - Smack file receive check
1793 * Returns 0 if current has access, error code otherwise
1795 static int smack_file_receive(struct file *file)
1799 struct smk_audit_info ad;
1800 struct inode *inode = file_inode(file);
1801 struct socket *sock;
1802 struct task_smack *tsp;
1803 struct socket_smack *ssp;
1805 if (unlikely(IS_PRIVATE(inode)))
1808 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1809 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1811 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1812 sock = SOCKET_I(inode);
1813 ssp = sock->sk->sk_security;
1814 tsp = current_security();
1816 * If the receiving process can't write to the
1817 * passed socket or if the passed socket can't
1818 * write to the receiving process don't accept
1819 * the passed socket.
1821 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1822 rc = smk_bu_file(file, may, rc);
1825 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1826 rc = smk_bu_file(file, may, rc);
1830 * This code relies on bitmasks.
1832 if (file->f_mode & FMODE_READ)
1834 if (file->f_mode & FMODE_WRITE)
1837 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1838 rc = smk_bu_file(file, may, rc);
1843 * smack_file_open - Smack dentry open processing
1845 * @cred: task credential
1847 * Set the security blob in the file structure.
1848 * Allow the open only if the task has read access. There are
1849 * many read operations (e.g. fstat) that you can do with an
1850 * fd even if you have the file open write-only.
1854 static int smack_file_open(struct file *file)
1856 struct task_smack *tsp = file->f_cred->security;
1857 struct inode *inode = file_inode(file);
1858 struct smk_audit_info ad;
1861 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1862 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1863 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1864 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1874 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1875 * @new: the new credentials
1876 * @gfp: the atomicity of any memory allocations
1878 * Prepare a blank set of credentials for modification. This must allocate all
1879 * the memory the LSM module might require such that cred_transfer() can
1880 * complete without error.
1882 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1884 struct task_smack *tsp;
1886 tsp = new_task_smack(NULL, NULL, gfp);
1890 cred->security = tsp;
1897 * smack_cred_free - "free" task-level security credentials
1898 * @cred: the credentials in question
1901 static void smack_cred_free(struct cred *cred)
1903 struct task_smack *tsp = cred->security;
1904 struct smack_rule *rp;
1905 struct list_head *l;
1906 struct list_head *n;
1910 cred->security = NULL;
1912 smk_destroy_label_list(&tsp->smk_relabel);
1914 list_for_each_safe(l, n, &tsp->smk_rules) {
1915 rp = list_entry(l, struct smack_rule, list);
1916 list_del(&rp->list);
1923 * smack_cred_prepare - prepare new set of credentials for modification
1924 * @new: the new credentials
1925 * @old: the original credentials
1926 * @gfp: the atomicity of any memory allocations
1928 * Prepare a new set of credentials for modification.
1930 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1933 struct task_smack *old_tsp = old->security;
1934 struct task_smack *new_tsp;
1937 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1938 if (new_tsp == NULL)
1941 new->security = new_tsp;
1943 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1947 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1956 * smack_cred_transfer - Transfer the old credentials to the new credentials
1957 * @new: the new credentials
1958 * @old: the original credentials
1960 * Fill in a set of blank credentials from another set of credentials.
1962 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1964 struct task_smack *old_tsp = old->security;
1965 struct task_smack *new_tsp = new->security;
1967 new_tsp->smk_task = old_tsp->smk_task;
1968 new_tsp->smk_forked = old_tsp->smk_task;
1969 mutex_init(&new_tsp->smk_rules_lock);
1970 INIT_LIST_HEAD(&new_tsp->smk_rules);
1973 /* cbs copy rule list */
1977 * smack_cred_getsecid - get the secid corresponding to a creds structure
1978 * @c: the object creds
1979 * @secid: where to put the result
1981 * Sets the secid to contain a u32 version of the smack label.
1983 static void smack_cred_getsecid(const struct cred *c, u32 *secid)
1985 struct smack_known *skp;
1988 skp = smk_of_task(c->security);
1989 *secid = skp->smk_secid;
1994 * smack_kernel_act_as - Set the subjective context in a set of credentials
1995 * @new: points to the set of credentials to be modified.
1996 * @secid: specifies the security ID to be set
1998 * Set the security data for a kernel service.
2000 static int smack_kernel_act_as(struct cred *new, u32 secid)
2002 struct task_smack *new_tsp = new->security;
2004 new_tsp->smk_task = smack_from_secid(secid);
2009 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2010 * @new: points to the set of credentials to be modified
2011 * @inode: points to the inode to use as a reference
2013 * Set the file creation context in a set of credentials to the same
2014 * as the objective context of the specified inode
2016 static int smack_kernel_create_files_as(struct cred *new,
2017 struct inode *inode)
2019 struct inode_smack *isp = inode->i_security;
2020 struct task_smack *tsp = new->security;
2022 tsp->smk_forked = isp->smk_inode;
2023 tsp->smk_task = tsp->smk_forked;
2028 * smk_curacc_on_task - helper to log task related access
2029 * @p: the task object
2030 * @access: the access requested
2031 * @caller: name of the calling function for audit
2033 * Return 0 if access is permitted
2035 static int smk_curacc_on_task(struct task_struct *p, int access,
2038 struct smk_audit_info ad;
2039 struct smack_known *skp = smk_of_task_struct(p);
2042 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2043 smk_ad_setfield_u_tsk(&ad, p);
2044 rc = smk_curacc(skp, access, &ad);
2045 rc = smk_bu_task(p, access, rc);
2050 * smack_task_setpgid - Smack check on setting pgid
2051 * @p: the task object
2054 * Return 0 if write access is permitted
2056 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2058 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2062 * smack_task_getpgid - Smack access check for getpgid
2063 * @p: the object task
2065 * Returns 0 if current can read the object task, error code otherwise
2067 static int smack_task_getpgid(struct task_struct *p)
2069 return smk_curacc_on_task(p, MAY_READ, __func__);
2073 * smack_task_getsid - Smack access check for getsid
2074 * @p: the object task
2076 * Returns 0 if current can read the object task, error code otherwise
2078 static int smack_task_getsid(struct task_struct *p)
2080 return smk_curacc_on_task(p, MAY_READ, __func__);
2084 * smack_task_getsecid - get the secid of the task
2085 * @p: the object task
2086 * @secid: where to put the result
2088 * Sets the secid to contain a u32 version of the smack label.
2090 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2092 struct smack_known *skp = smk_of_task_struct(p);
2094 *secid = skp->smk_secid;
2098 * smack_task_setnice - Smack check on setting nice
2099 * @p: the task object
2102 * Return 0 if write access is permitted
2104 static int smack_task_setnice(struct task_struct *p, int nice)
2106 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2110 * smack_task_setioprio - Smack check on setting ioprio
2111 * @p: the task object
2114 * Return 0 if write access is permitted
2116 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2118 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2122 * smack_task_getioprio - Smack check on reading ioprio
2123 * @p: the task object
2125 * Return 0 if read access is permitted
2127 static int smack_task_getioprio(struct task_struct *p)
2129 return smk_curacc_on_task(p, MAY_READ, __func__);
2133 * smack_task_setscheduler - Smack check on setting scheduler
2134 * @p: the task object
2138 * Return 0 if read access is permitted
2140 static int smack_task_setscheduler(struct task_struct *p)
2142 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2146 * smack_task_getscheduler - Smack check on reading scheduler
2147 * @p: the task object
2149 * Return 0 if read access is permitted
2151 static int smack_task_getscheduler(struct task_struct *p)
2153 return smk_curacc_on_task(p, MAY_READ, __func__);
2157 * smack_task_movememory - Smack check on moving memory
2158 * @p: the task object
2160 * Return 0 if write access is permitted
2162 static int smack_task_movememory(struct task_struct *p)
2164 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2168 * smack_task_kill - Smack check on signal delivery
2169 * @p: the task object
2172 * @cred: identifies the cred to use in lieu of current's
2174 * Return 0 if write access is permitted
2177 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2178 int sig, const struct cred *cred)
2180 struct smk_audit_info ad;
2181 struct smack_known *skp;
2182 struct smack_known *tkp = smk_of_task_struct(p);
2186 return 0; /* null signal; existence test */
2188 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2189 smk_ad_setfield_u_tsk(&ad, p);
2191 * Sending a signal requires that the sender
2192 * can write the receiver.
2195 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2196 rc = smk_bu_task(p, MAY_DELIVER, rc);
2200 * If the cred isn't NULL we're dealing with some USB IO
2201 * specific behavior. This is not clean. For one thing
2202 * we can't take privilege into account.
2204 skp = smk_of_task(cred->security);
2205 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2206 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2211 * smack_task_to_inode - copy task smack into the inode blob
2212 * @p: task to copy from
2213 * @inode: inode to copy to
2215 * Sets the smack pointer in the inode security blob
2217 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2219 struct inode_smack *isp = inode->i_security;
2220 struct smack_known *skp = smk_of_task_struct(p);
2222 isp->smk_inode = skp;
2223 isp->smk_flags |= SMK_INODE_INSTANT;
2231 * smack_sk_alloc_security - Allocate a socket blob
2234 * @gfp_flags: memory allocation flags
2236 * Assign Smack pointers to current
2238 * Returns 0 on success, -ENOMEM is there's no memory
2240 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2242 struct smack_known *skp = smk_of_current();
2243 struct socket_smack *ssp;
2245 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2250 * Sockets created by kernel threads receive web label.
2252 if (unlikely(current->flags & PF_KTHREAD)) {
2253 ssp->smk_in = &smack_known_web;
2254 ssp->smk_out = &smack_known_web;
2259 ssp->smk_packet = NULL;
2261 sk->sk_security = ssp;
2267 * smack_sk_free_security - Free a socket blob
2270 * Clears the blob pointer
2272 static void smack_sk_free_security(struct sock *sk)
2274 #ifdef SMACK_IPV6_PORT_LABELING
2275 struct smk_port_label *spp;
2277 if (sk->sk_family == PF_INET6) {
2279 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2280 if (spp->smk_sock != sk)
2282 spp->smk_can_reuse = 1;
2288 kfree(sk->sk_security);
2292 * smack_ipv4host_label - check host based restrictions
2293 * @sip: the object end
2295 * looks for host based access restrictions
2297 * This version will only be appropriate for really small sets of single label
2298 * hosts. The caller is responsible for ensuring that the RCU read lock is
2299 * taken before calling this function.
2301 * Returns the label of the far end or NULL if it's not special.
2303 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2305 struct smk_net4addr *snp;
2306 struct in_addr *siap = &sip->sin_addr;
2308 if (siap->s_addr == 0)
2311 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2313 * we break after finding the first match because
2314 * the list is sorted from longest to shortest mask
2315 * so we have found the most specific match
2317 if (snp->smk_host.s_addr ==
2318 (siap->s_addr & snp->smk_mask.s_addr))
2319 return snp->smk_label;
2324 #if IS_ENABLED(CONFIG_IPV6)
2326 * smk_ipv6_localhost - Check for local ipv6 host address
2329 * Returns boolean true if this is the localhost address
2331 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2333 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2334 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2336 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2337 ntohs(be16p[7]) == 1)
2343 * smack_ipv6host_label - check host based restrictions
2344 * @sip: the object end
2346 * looks for host based access restrictions
2348 * This version will only be appropriate for really small sets of single label
2349 * hosts. The caller is responsible for ensuring that the RCU read lock is
2350 * taken before calling this function.
2352 * Returns the label of the far end or NULL if it's not special.
2354 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2356 struct smk_net6addr *snp;
2357 struct in6_addr *sap = &sip->sin6_addr;
2362 * It's local. Don't look for a host label.
2364 if (smk_ipv6_localhost(sip))
2367 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2369 * If the label is NULL the entry has
2370 * been renounced. Ignore it.
2372 if (snp->smk_label == NULL)
2375 * we break after finding the first match because
2376 * the list is sorted from longest to shortest mask
2377 * so we have found the most specific match
2379 for (found = 1, i = 0; i < 8; i++) {
2380 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2381 snp->smk_host.s6_addr16[i]) {
2387 return snp->smk_label;
2392 #endif /* CONFIG_IPV6 */
2395 * smack_netlabel - Set the secattr on a socket
2397 * @labeled: socket label scheme
2399 * Convert the outbound smack value (smk_out) to a
2400 * secattr and attach it to the socket.
2402 * Returns 0 on success or an error code
2404 static int smack_netlabel(struct sock *sk, int labeled)
2406 struct smack_known *skp;
2407 struct socket_smack *ssp = sk->sk_security;
2411 * Usually the netlabel code will handle changing the
2412 * packet labeling based on the label.
2413 * The case of a single label host is different, because
2414 * a single label host should never get a labeled packet
2415 * even though the label is usually associated with a packet
2419 bh_lock_sock_nested(sk);
2421 if (ssp->smk_out == smack_net_ambient ||
2422 labeled == SMACK_UNLABELED_SOCKET)
2423 netlbl_sock_delattr(sk);
2426 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2436 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2438 * @sap: the destination address
2440 * Set the correct secattr for the given socket based on the destination
2441 * address and perform any outbound access checks needed.
2443 * Returns 0 on success or an error code.
2446 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2448 struct smack_known *skp;
2451 struct smack_known *hkp;
2452 struct socket_smack *ssp = sk->sk_security;
2453 struct smk_audit_info ad;
2456 hkp = smack_ipv4host_label(sap);
2459 struct lsm_network_audit net;
2461 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2462 ad.a.u.net->family = sap->sin_family;
2463 ad.a.u.net->dport = sap->sin_port;
2464 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2466 sk_lbl = SMACK_UNLABELED_SOCKET;
2468 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2469 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2471 sk_lbl = SMACK_CIPSO_SOCKET;
2478 return smack_netlabel(sk, sk_lbl);
2481 #if IS_ENABLED(CONFIG_IPV6)
2483 * smk_ipv6_check - check Smack access
2484 * @subject: subject Smack label
2485 * @object: object Smack label
2487 * @act: the action being taken
2489 * Check an IPv6 access
2491 static int smk_ipv6_check(struct smack_known *subject,
2492 struct smack_known *object,
2493 struct sockaddr_in6 *address, int act)
2496 struct lsm_network_audit net;
2498 struct smk_audit_info ad;
2502 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2503 ad.a.u.net->family = PF_INET6;
2504 ad.a.u.net->dport = ntohs(address->sin6_port);
2505 if (act == SMK_RECEIVING)
2506 ad.a.u.net->v6info.saddr = address->sin6_addr;
2508 ad.a.u.net->v6info.daddr = address->sin6_addr;
2510 rc = smk_access(subject, object, MAY_WRITE, &ad);
2511 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2514 #endif /* CONFIG_IPV6 */
2516 #ifdef SMACK_IPV6_PORT_LABELING
2518 * smk_ipv6_port_label - Smack port access table management
2522 * Create or update the port list entry
2524 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2526 struct sock *sk = sock->sk;
2527 struct sockaddr_in6 *addr6;
2528 struct socket_smack *ssp = sock->sk->sk_security;
2529 struct smk_port_label *spp;
2530 unsigned short port = 0;
2532 if (address == NULL) {
2534 * This operation is changing the Smack information
2535 * on the bound socket. Take the changes to the port
2539 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2540 if (sk != spp->smk_sock)
2542 spp->smk_in = ssp->smk_in;
2543 spp->smk_out = ssp->smk_out;
2548 * A NULL address is only used for updating existing
2549 * bound entries. If there isn't one, it's OK.
2555 addr6 = (struct sockaddr_in6 *)address;
2556 port = ntohs(addr6->sin6_port);
2558 * This is a special case that is safely ignored.
2564 * Look for an existing port list entry.
2565 * This is an indication that a port is getting reused.
2568 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2569 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2571 if (spp->smk_can_reuse != 1) {
2575 spp->smk_port = port;
2577 spp->smk_in = ssp->smk_in;
2578 spp->smk_out = ssp->smk_out;
2579 spp->smk_can_reuse = 0;
2585 * A new port entry is required.
2587 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2591 spp->smk_port = port;
2593 spp->smk_in = ssp->smk_in;
2594 spp->smk_out = ssp->smk_out;
2595 spp->smk_sock_type = sock->type;
2596 spp->smk_can_reuse = 0;
2598 mutex_lock(&smack_ipv6_lock);
2599 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2600 mutex_unlock(&smack_ipv6_lock);
2605 * smk_ipv6_port_check - check Smack port access
2609 * Create or update the port list entry
2611 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2614 struct smk_port_label *spp;
2615 struct socket_smack *ssp = sk->sk_security;
2616 struct smack_known *skp = NULL;
2617 unsigned short port;
2618 struct smack_known *object;
2620 if (act == SMK_RECEIVING) {
2621 skp = smack_ipv6host_label(address);
2622 object = ssp->smk_in;
2625 object = smack_ipv6host_label(address);
2629 * The other end is a single label host.
2631 if (skp != NULL && object != NULL)
2632 return smk_ipv6_check(skp, object, address, act);
2634 skp = smack_net_ambient;
2636 object = smack_net_ambient;
2639 * It's remote, so port lookup does no good.
2641 if (!smk_ipv6_localhost(address))
2642 return smk_ipv6_check(skp, object, address, act);
2645 * It's local so the send check has to have passed.
2647 if (act == SMK_RECEIVING)
2650 port = ntohs(address->sin6_port);
2652 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2653 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2655 object = spp->smk_in;
2656 if (act == SMK_CONNECTING)
2657 ssp->smk_packet = spp->smk_out;
2662 return smk_ipv6_check(skp, object, address, act);
2664 #endif /* SMACK_IPV6_PORT_LABELING */
2667 * smack_inode_setsecurity - set smack xattrs
2668 * @inode: the object
2669 * @name: attribute name
2670 * @value: attribute value
2671 * @size: size of the attribute
2674 * Sets the named attribute in the appropriate blob
2676 * Returns 0 on success, or an error code
2678 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2679 const void *value, size_t size, int flags)
2681 struct smack_known *skp;
2682 struct inode_smack *nsp = inode->i_security;
2683 struct socket_smack *ssp;
2684 struct socket *sock;
2687 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2690 skp = smk_import_entry(value, size);
2692 return PTR_ERR(skp);
2694 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2695 nsp->smk_inode = skp;
2696 nsp->smk_flags |= SMK_INODE_INSTANT;
2700 * The rest of the Smack xattrs are only on sockets.
2702 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2705 sock = SOCKET_I(inode);
2706 if (sock == NULL || sock->sk == NULL)
2709 ssp = sock->sk->sk_security;
2711 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2713 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2715 if (sock->sk->sk_family == PF_INET) {
2716 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2719 "Smack: \"%s\" netlbl error %d.\n",
2725 #ifdef SMACK_IPV6_PORT_LABELING
2726 if (sock->sk->sk_family == PF_INET6)
2727 smk_ipv6_port_label(sock, NULL);
2734 * smack_socket_post_create - finish socket setup
2736 * @family: protocol family
2741 * Sets the netlabel information on the socket
2743 * Returns 0 on success, and error code otherwise
2745 static int smack_socket_post_create(struct socket *sock, int family,
2746 int type, int protocol, int kern)
2748 struct socket_smack *ssp;
2750 if (sock->sk == NULL)
2754 * Sockets created by kernel threads receive web label.
2756 if (unlikely(current->flags & PF_KTHREAD)) {
2757 ssp = sock->sk->sk_security;
2758 ssp->smk_in = &smack_known_web;
2759 ssp->smk_out = &smack_known_web;
2762 if (family != PF_INET)
2765 * Set the outbound netlbl.
2767 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2771 * smack_socket_socketpair - create socket pair
2772 * @socka: one socket
2773 * @sockb: another socket
2775 * Cross reference the peer labels for SO_PEERSEC
2777 * Returns 0 on success, and error code otherwise
2779 static int smack_socket_socketpair(struct socket *socka,
2780 struct socket *sockb)
2782 struct socket_smack *asp = socka->sk->sk_security;
2783 struct socket_smack *bsp = sockb->sk->sk_security;
2785 asp->smk_packet = bsp->smk_out;
2786 bsp->smk_packet = asp->smk_out;
2791 #ifdef SMACK_IPV6_PORT_LABELING
2793 * smack_socket_bind - record port binding information.
2795 * @address: the port address
2796 * @addrlen: size of the address
2798 * Records the label bound to a port.
2802 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2805 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2806 smk_ipv6_port_label(sock, address);
2809 #endif /* SMACK_IPV6_PORT_LABELING */
2812 * smack_socket_connect - connect access check
2814 * @sap: the other end
2815 * @addrlen: size of sap
2817 * Verifies that a connection may be possible
2819 * Returns 0 on success, and error code otherwise
2821 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2825 #if IS_ENABLED(CONFIG_IPV6)
2826 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2828 #ifdef SMACK_IPV6_SECMARK_LABELING
2829 struct smack_known *rsp;
2830 struct socket_smack *ssp;
2833 if (sock->sk == NULL)
2836 #ifdef SMACK_IPV6_SECMARK_LABELING
2837 ssp = sock->sk->sk_security;
2840 switch (sock->sk->sk_family) {
2842 if (addrlen < sizeof(struct sockaddr_in))
2844 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2847 if (addrlen < sizeof(struct sockaddr_in6))
2849 #ifdef SMACK_IPV6_SECMARK_LABELING
2850 rsp = smack_ipv6host_label(sip);
2852 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2855 #ifdef SMACK_IPV6_PORT_LABELING
2856 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2864 * smack_flags_to_may - convert S_ to MAY_ values
2865 * @flags: the S_ value
2867 * Returns the equivalent MAY_ value
2869 static int smack_flags_to_may(int flags)
2873 if (flags & S_IRUGO)
2875 if (flags & S_IWUGO)
2877 if (flags & S_IXUGO)
2884 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2889 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2891 struct smack_known *skp = smk_of_current();
2893 msg->security = skp;
2898 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2901 * Clears the blob pointer
2903 static void smack_msg_msg_free_security(struct msg_msg *msg)
2905 msg->security = NULL;
2909 * smack_of_ipc - the smack pointer for the ipc
2912 * Returns a pointer to the smack value
2914 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2916 return (struct smack_known *)isp->security;
2920 * smack_ipc_alloc_security - Set the security blob for ipc
2925 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2927 struct smack_known *skp = smk_of_current();
2929 isp->security = skp;
2934 * smack_ipc_free_security - Clear the security blob for ipc
2937 * Clears the blob pointer
2939 static void smack_ipc_free_security(struct kern_ipc_perm *isp)
2941 isp->security = NULL;
2945 * smk_curacc_shm : check if current has access on shm
2947 * @access : access requested
2949 * Returns 0 if current has the requested access, error code otherwise
2951 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2953 struct smack_known *ssp = smack_of_ipc(isp);
2954 struct smk_audit_info ad;
2958 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2959 ad.a.u.ipc_id = isp->id;
2961 rc = smk_curacc(ssp, access, &ad);
2962 rc = smk_bu_current("shm", ssp, access, rc);
2967 * smack_shm_associate - Smack access check for shm
2969 * @shmflg: access requested
2971 * Returns 0 if current has the requested access, error code otherwise
2973 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2977 may = smack_flags_to_may(shmflg);
2978 return smk_curacc_shm(isp, may);
2982 * smack_shm_shmctl - Smack access check for shm
2984 * @cmd: what it wants to do
2986 * Returns 0 if current has the requested access, error code otherwise
2988 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3002 may = MAY_READWRITE;
3007 * System level information.
3013 return smk_curacc_shm(isp, may);
3017 * smack_shm_shmat - Smack access for shmat
3020 * @shmflg: access requested
3022 * Returns 0 if current has the requested access, error code otherwise
3024 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3029 may = smack_flags_to_may(shmflg);
3030 return smk_curacc_shm(ipc, may);
3034 * smk_curacc_sem : check if current has access on sem
3036 * @access : access requested
3038 * Returns 0 if current has the requested access, error code otherwise
3040 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3042 struct smack_known *ssp = smack_of_ipc(isp);
3043 struct smk_audit_info ad;
3047 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3048 ad.a.u.ipc_id = isp->id;
3050 rc = smk_curacc(ssp, access, &ad);
3051 rc = smk_bu_current("sem", ssp, access, rc);
3056 * smack_sem_associate - Smack access check for sem
3058 * @semflg: access requested
3060 * Returns 0 if current has the requested access, error code otherwise
3062 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3066 may = smack_flags_to_may(semflg);
3067 return smk_curacc_sem(isp, may);
3071 * smack_sem_shmctl - Smack access check for sem
3073 * @cmd: what it wants to do
3075 * Returns 0 if current has the requested access, error code otherwise
3077 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3096 may = MAY_READWRITE;
3101 * System level information
3108 return smk_curacc_sem(isp, may);
3112 * smack_sem_semop - Smack checks of semaphore operations
3118 * Treated as read and write in all cases.
3120 * Returns 0 if access is allowed, error code otherwise
3122 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3123 unsigned nsops, int alter)
3125 return smk_curacc_sem(isp, MAY_READWRITE);
3129 * smk_curacc_msq : helper to check if current has access on msq
3131 * @access : access requested
3133 * return 0 if current has access, error otherwise
3135 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3137 struct smack_known *msp = smack_of_ipc(isp);
3138 struct smk_audit_info ad;
3142 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3143 ad.a.u.ipc_id = isp->id;
3145 rc = smk_curacc(msp, access, &ad);
3146 rc = smk_bu_current("msq", msp, access, rc);
3151 * smack_msg_queue_associate - Smack access check for msg_queue
3153 * @msqflg: access requested
3155 * Returns 0 if current has the requested access, error code otherwise
3157 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3161 may = smack_flags_to_may(msqflg);
3162 return smk_curacc_msq(isp, may);
3166 * smack_msg_queue_msgctl - Smack access check for msg_queue
3168 * @cmd: what it wants to do
3170 * Returns 0 if current has the requested access, error code otherwise
3172 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3184 may = MAY_READWRITE;
3189 * System level information
3196 return smk_curacc_msq(isp, may);
3200 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3203 * @msqflg: access requested
3205 * Returns 0 if current has the requested access, error code otherwise
3207 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3212 may = smack_flags_to_may(msqflg);
3213 return smk_curacc_msq(isp, may);
3217 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3224 * Returns 0 if current has read and write access, error code otherwise
3226 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3227 struct task_struct *target, long type, int mode)
3229 return smk_curacc_msq(isp, MAY_READWRITE);
3233 * smack_ipc_permission - Smack access for ipc_permission()
3234 * @ipp: the object permissions
3235 * @flag: access requested
3237 * Returns 0 if current has read and write access, error code otherwise
3239 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3241 struct smack_known *iskp = ipp->security;
3242 int may = smack_flags_to_may(flag);
3243 struct smk_audit_info ad;
3247 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3248 ad.a.u.ipc_id = ipp->id;
3250 rc = smk_curacc(iskp, may, &ad);
3251 rc = smk_bu_current("svipc", iskp, may, rc);
3256 * smack_ipc_getsecid - Extract smack security id
3257 * @ipp: the object permissions
3258 * @secid: where result will be saved
3260 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3262 struct smack_known *iskp = ipp->security;
3264 *secid = iskp->smk_secid;
3268 * smack_d_instantiate - Make sure the blob is correct on an inode
3269 * @opt_dentry: dentry where inode will be attached
3270 * @inode: the object
3272 * Set the inode's security blob if it hasn't been done already.
3274 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3276 struct super_block *sbp;
3277 struct superblock_smack *sbsp;
3278 struct inode_smack *isp;
3279 struct smack_known *skp;
3280 struct smack_known *ckp = smk_of_current();
3281 struct smack_known *final;
3282 char trattr[TRANS_TRUE_SIZE];
3290 isp = inode->i_security;
3292 mutex_lock(&isp->smk_lock);
3294 * If the inode is already instantiated
3295 * take the quick way out
3297 if (isp->smk_flags & SMK_INODE_INSTANT)
3301 sbsp = sbp->s_security;
3303 * We're going to use the superblock default label
3304 * if there's no label on the file.
3306 final = sbsp->smk_default;
3309 * If this is the root inode the superblock
3310 * may be in the process of initialization.
3311 * If that is the case use the root value out
3312 * of the superblock.
3314 if (opt_dentry->d_parent == opt_dentry) {
3315 switch (sbp->s_magic) {
3316 case CGROUP_SUPER_MAGIC:
3317 case CGROUP2_SUPER_MAGIC:
3319 * The cgroup filesystem is never mounted,
3320 * so there's no opportunity to set the mount
3323 sbsp->smk_root = &smack_known_star;
3324 sbsp->smk_default = &smack_known_star;
3325 isp->smk_inode = sbsp->smk_root;
3329 * What about shmem/tmpfs anonymous files with dentry
3330 * obtained from d_alloc_pseudo()?
3332 isp->smk_inode = smk_of_current();
3335 isp->smk_inode = smk_of_current();
3339 * Socket access is controlled by the socket
3340 * structures associated with the task involved.
3342 isp->smk_inode = &smack_known_star;
3345 isp->smk_inode = sbsp->smk_root;
3348 isp->smk_flags |= SMK_INODE_INSTANT;
3353 * This is pretty hackish.
3354 * Casey says that we shouldn't have to do
3355 * file system specific code, but it does help
3356 * with keeping it simple.
3358 switch (sbp->s_magic) {
3360 case CGROUP_SUPER_MAGIC:
3361 case CGROUP2_SUPER_MAGIC:
3363 * Casey says that it's a little embarrassing
3364 * that the smack file system doesn't do
3365 * extended attributes.
3367 * Cgroupfs is special
3369 final = &smack_known_star;
3371 case DEVPTS_SUPER_MAGIC:
3373 * devpts seems content with the label of the task.
3374 * Programs that change smack have to treat the
3379 case PROC_SUPER_MAGIC:
3381 * Casey says procfs appears not to care.
3382 * The superblock default suffices.
3387 * Device labels should come from the filesystem,
3388 * but watch out, because they're volitile,
3389 * getting recreated on every reboot.
3391 final = &smack_known_star;
3395 * If a smack value has been set we want to use it,
3396 * but since tmpfs isn't giving us the opportunity
3397 * to set mount options simulate setting the
3398 * superblock default.
3402 * This isn't an understood special case.
3403 * Get the value from the xattr.
3407 * UNIX domain sockets use lower level socket data.
3409 if (S_ISSOCK(inode->i_mode)) {
3410 final = &smack_known_star;
3414 * No xattr support means, alas, no SMACK label.
3415 * Use the aforeapplied default.
3416 * It would be curious if the label of the task
3417 * does not match that assigned.
3419 if (!(inode->i_opflags & IOP_XATTR))
3422 * Get the dentry for xattr.
3424 dp = dget(opt_dentry);
3425 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3426 if (!IS_ERR_OR_NULL(skp))
3430 * Transmuting directory
3432 if (S_ISDIR(inode->i_mode)) {
3434 * If this is a new directory and the label was
3435 * transmuted when the inode was initialized
3436 * set the transmute attribute on the directory
3437 * and mark the inode.
3439 * If there is a transmute attribute on the
3440 * directory mark the inode.
3442 if (isp->smk_flags & SMK_INODE_CHANGED) {
3443 isp->smk_flags &= ~SMK_INODE_CHANGED;
3444 rc = __vfs_setxattr(dp, inode,
3445 XATTR_NAME_SMACKTRANSMUTE,
3446 TRANS_TRUE, TRANS_TRUE_SIZE,
3449 rc = __vfs_getxattr(dp, inode,
3450 XATTR_NAME_SMACKTRANSMUTE, trattr,
3452 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3453 TRANS_TRUE_SIZE) != 0)
3457 transflag = SMK_INODE_TRANSMUTE;
3460 * Don't let the exec or mmap label be "*" or "@".
3462 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3463 if (IS_ERR(skp) || skp == &smack_known_star ||
3464 skp == &smack_known_web)
3466 isp->smk_task = skp;
3468 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3469 if (IS_ERR(skp) || skp == &smack_known_star ||
3470 skp == &smack_known_web)
3472 isp->smk_mmap = skp;
3479 isp->smk_inode = ckp;
3481 isp->smk_inode = final;
3483 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3486 mutex_unlock(&isp->smk_lock);
3491 * smack_getprocattr - Smack process attribute access
3492 * @p: the object task
3493 * @name: the name of the attribute in /proc/.../attr
3494 * @value: where to put the result
3496 * Places a copy of the task Smack into value
3498 * Returns the length of the smack label or an error code
3500 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3502 struct smack_known *skp = smk_of_task_struct(p);
3506 if (strcmp(name, "current") != 0)
3509 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3519 * smack_setprocattr - Smack process attribute setting
3520 * @name: the name of the attribute in /proc/.../attr
3521 * @value: the value to set
3522 * @size: the size of the value
3524 * Sets the Smack value of the task. Only setting self
3525 * is permitted and only with privilege
3527 * Returns the length of the smack label or an error code
3529 static int smack_setprocattr(const char *name, void *value, size_t size)
3531 struct task_smack *tsp = current_security();
3533 struct smack_known *skp;
3534 struct smack_known_list_elem *sklep;
3537 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3540 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3543 if (strcmp(name, "current") != 0)
3546 skp = smk_import_entry(value, size);
3548 return PTR_ERR(skp);
3551 * No process is ever allowed the web ("@") label
3552 * and the star ("*") label.
3554 if (skp == &smack_known_web || skp == &smack_known_star)
3557 if (!smack_privileged(CAP_MAC_ADMIN)) {
3559 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3560 if (sklep->smk_label == skp) {
3568 new = prepare_creds();
3572 tsp = new->security;
3573 tsp->smk_task = skp;
3575 * process can change its label only once
3577 smk_destroy_label_list(&tsp->smk_relabel);
3584 * smack_unix_stream_connect - Smack access on UDS
3586 * @other: the other sock
3589 * Return 0 if a subject with the smack of sock could access
3590 * an object with the smack of other, otherwise an error code
3592 static int smack_unix_stream_connect(struct sock *sock,
3593 struct sock *other, struct sock *newsk)
3595 struct smack_known *skp;
3596 struct smack_known *okp;
3597 struct socket_smack *ssp = sock->sk_security;
3598 struct socket_smack *osp = other->sk_security;
3599 struct socket_smack *nsp = newsk->sk_security;
3600 struct smk_audit_info ad;
3603 struct lsm_network_audit net;
3606 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3610 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3611 smk_ad_setfield_u_net_sk(&ad, other);
3613 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3614 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3618 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3619 rc = smk_bu_note("UDS connect", okp, skp,
3625 * Cross reference the peer labels for SO_PEERSEC.
3628 nsp->smk_packet = ssp->smk_out;
3629 ssp->smk_packet = osp->smk_out;
3636 * smack_unix_may_send - Smack access on UDS
3638 * @other: the other socket
3640 * Return 0 if a subject with the smack of sock could access
3641 * an object with the smack of other, otherwise an error code
3643 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3645 struct socket_smack *ssp = sock->sk->sk_security;
3646 struct socket_smack *osp = other->sk->sk_security;
3647 struct smk_audit_info ad;
3651 struct lsm_network_audit net;
3653 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3654 smk_ad_setfield_u_net_sk(&ad, other->sk);
3657 if (smack_privileged(CAP_MAC_OVERRIDE))
3660 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3661 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3666 * smack_socket_sendmsg - Smack check based on destination host
3669 * @size: the size of the message
3671 * Return 0 if the current subject can write to the destination host.
3672 * For IPv4 this is only a question if the destination is a single label host.
3673 * For IPv6 this is a check against the label of the port.
3675 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3678 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3679 #if IS_ENABLED(CONFIG_IPV6)
3680 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3682 #ifdef SMACK_IPV6_SECMARK_LABELING
3683 struct socket_smack *ssp = sock->sk->sk_security;
3684 struct smack_known *rsp;
3689 * Perfectly reasonable for this to be NULL
3694 switch (sock->sk->sk_family) {
3696 rc = smack_netlabel_send(sock->sk, sip);
3699 #ifdef SMACK_IPV6_SECMARK_LABELING
3700 rsp = smack_ipv6host_label(sap);
3702 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3705 #ifdef SMACK_IPV6_PORT_LABELING
3706 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3714 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3715 * @sap: netlabel secattr
3716 * @ssp: socket security information
3718 * Returns a pointer to a Smack label entry found on the label list.
3720 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3721 struct socket_smack *ssp)
3723 struct smack_known *skp;
3728 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3730 * Looks like a CIPSO packet.
3731 * If there are flags but no level netlabel isn't
3732 * behaving the way we expect it to.
3734 * Look it up in the label table
3735 * Without guidance regarding the smack value
3736 * for the packet fall back on the network
3740 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3741 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3744 * Compare the catsets. Use the netlbl APIs.
3746 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3747 if ((skp->smk_netlabel.flags &
3748 NETLBL_SECATTR_MLS_CAT) == 0)
3752 for (acat = -1, kcat = -1; acat == kcat; ) {
3753 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3755 kcat = netlbl_catmap_walk(
3756 skp->smk_netlabel.attr.mls.cat,
3758 if (acat < 0 || kcat < 0)
3771 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3772 return &smack_known_web;
3773 return &smack_known_star;
3775 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3777 * Looks like a fallback, which gives us a secid.
3779 return smack_from_secid(sap->attr.secid);
3781 * Without guidance regarding the smack value
3782 * for the packet fall back on the network
3785 return smack_net_ambient;
3788 #if IS_ENABLED(CONFIG_IPV6)
3789 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3793 int proto = -EINVAL;
3794 struct ipv6hdr _ipv6h;
3795 struct ipv6hdr *ip6;
3797 struct tcphdr _tcph, *th;
3798 struct udphdr _udph, *uh;
3799 struct dccp_hdr _dccph, *dh;
3803 offset = skb_network_offset(skb);
3804 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3807 sip->sin6_addr = ip6->saddr;
3809 nexthdr = ip6->nexthdr;
3810 offset += sizeof(_ipv6h);
3811 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3818 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3820 sip->sin6_port = th->source;
3823 case IPPROTO_UDPLITE:
3824 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3826 sip->sin6_port = uh->source;
3829 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3831 sip->sin6_port = dh->dccph_sport;
3836 #endif /* CONFIG_IPV6 */
3839 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3843 * Returns 0 if the packet should be delivered, an error code otherwise
3845 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3847 struct netlbl_lsm_secattr secattr;
3848 struct socket_smack *ssp = sk->sk_security;
3849 struct smack_known *skp = NULL;
3851 struct smk_audit_info ad;
3852 u16 family = sk->sk_family;
3854 struct lsm_network_audit net;
3856 #if IS_ENABLED(CONFIG_IPV6)
3857 struct sockaddr_in6 sadd;
3860 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3862 #endif /* CONFIG_IPV6 */
3866 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3868 * If there is a secmark use it rather than the CIPSO label.
3869 * If there is no secmark fall back to CIPSO.
3870 * The secmark is assumed to reflect policy better.
3872 if (skb && skb->secmark != 0) {
3873 skp = smack_from_secid(skb->secmark);
3876 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3878 * Translate what netlabel gave us.
3880 netlbl_secattr_init(&secattr);
3882 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3884 skp = smack_from_secattr(&secattr, ssp);
3886 skp = smack_net_ambient;
3888 netlbl_secattr_destroy(&secattr);
3890 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3894 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3895 ad.a.u.net->family = family;
3896 ad.a.u.net->netif = skb->skb_iif;
3897 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3900 * Receiving a packet requires that the other end
3901 * be able to write here. Read access is not required.
3902 * This is the simplist possible security model
3905 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3906 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3909 netlbl_skbuff_err(skb, family, rc, 0);
3911 #if IS_ENABLED(CONFIG_IPV6)
3913 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3914 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3915 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3917 #ifdef SMACK_IPV6_SECMARK_LABELING
3918 if (skb && skb->secmark != 0)
3919 skp = smack_from_secid(skb->secmark);
3921 skp = smack_ipv6host_label(&sadd);
3923 skp = smack_net_ambient;
3925 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3926 ad.a.u.net->family = family;
3927 ad.a.u.net->netif = skb->skb_iif;
3928 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3929 #endif /* CONFIG_AUDIT */
3930 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3931 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3933 #endif /* SMACK_IPV6_SECMARK_LABELING */
3934 #ifdef SMACK_IPV6_PORT_LABELING
3935 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3936 #endif /* SMACK_IPV6_PORT_LABELING */
3938 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3939 ICMPV6_ADM_PROHIBITED, 0);
3941 #endif /* CONFIG_IPV6 */
3948 * smack_socket_getpeersec_stream - pull in packet label
3950 * @optval: user's destination
3951 * @optlen: size thereof
3954 * returns zero on success, an error code otherwise
3956 static int smack_socket_getpeersec_stream(struct socket *sock,
3957 char __user *optval,
3958 int __user *optlen, unsigned len)
3960 struct socket_smack *ssp;
3965 ssp = sock->sk->sk_security;
3966 if (ssp->smk_packet != NULL) {
3967 rcp = ssp->smk_packet->smk_known;
3968 slen = strlen(rcp) + 1;
3973 else if (copy_to_user(optval, rcp, slen) != 0)
3976 if (put_user(slen, optlen) != 0)
3984 * smack_socket_getpeersec_dgram - pull in packet label
3985 * @sock: the peer socket
3987 * @secid: pointer to where to put the secid of the packet
3989 * Sets the netlabel socket state on sk from parent
3991 static int smack_socket_getpeersec_dgram(struct socket *sock,
3992 struct sk_buff *skb, u32 *secid)
3995 struct netlbl_lsm_secattr secattr;
3996 struct socket_smack *ssp = NULL;
3997 struct smack_known *skp;
3998 int family = PF_UNSPEC;
3999 u32 s = 0; /* 0 is the invalid secid */
4003 if (skb->protocol == htons(ETH_P_IP))
4005 #if IS_ENABLED(CONFIG_IPV6)
4006 else if (skb->protocol == htons(ETH_P_IPV6))
4008 #endif /* CONFIG_IPV6 */
4010 if (family == PF_UNSPEC && sock != NULL)
4011 family = sock->sk->sk_family;
4015 ssp = sock->sk->sk_security;
4016 s = ssp->smk_out->smk_secid;
4019 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4025 * Translate what netlabel gave us.
4027 if (sock != NULL && sock->sk != NULL)
4028 ssp = sock->sk->sk_security;
4029 netlbl_secattr_init(&secattr);
4030 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4032 skp = smack_from_secattr(&secattr, ssp);
4035 netlbl_secattr_destroy(&secattr);
4038 #ifdef SMACK_IPV6_SECMARK_LABELING
4050 * smack_sock_graft - Initialize a newly created socket with an existing sock
4052 * @parent: parent socket
4054 * Set the smk_{in,out} state of an existing sock based on the process that
4055 * is creating the new socket.
4057 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4059 struct socket_smack *ssp;
4060 struct smack_known *skp = smk_of_current();
4063 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4066 ssp = sk->sk_security;
4069 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4073 * smack_inet_conn_request - Smack access check on connect
4074 * @sk: socket involved
4078 * Returns 0 if a task with the packet label could write to
4079 * the socket, otherwise an error code
4081 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4082 struct request_sock *req)
4084 u16 family = sk->sk_family;
4085 struct smack_known *skp;
4086 struct socket_smack *ssp = sk->sk_security;
4087 struct netlbl_lsm_secattr secattr;
4088 struct sockaddr_in addr;
4090 struct smack_known *hskp;
4092 struct smk_audit_info ad;
4094 struct lsm_network_audit net;
4097 #if IS_ENABLED(CONFIG_IPV6)
4098 if (family == PF_INET6) {
4100 * Handle mapped IPv4 packets arriving
4101 * via IPv6 sockets. Don't set up netlabel
4102 * processing on IPv6.
4104 if (skb->protocol == htons(ETH_P_IP))
4109 #endif /* CONFIG_IPV6 */
4111 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4113 * If there is a secmark use it rather than the CIPSO label.
4114 * If there is no secmark fall back to CIPSO.
4115 * The secmark is assumed to reflect policy better.
4117 if (skb && skb->secmark != 0) {
4118 skp = smack_from_secid(skb->secmark);
4121 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4123 netlbl_secattr_init(&secattr);
4124 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4126 skp = smack_from_secattr(&secattr, ssp);
4128 skp = &smack_known_huh;
4129 netlbl_secattr_destroy(&secattr);
4131 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4136 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4137 ad.a.u.net->family = family;
4138 ad.a.u.net->netif = skb->skb_iif;
4139 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4142 * Receiving a packet requires that the other end be able to write
4143 * here. Read access is not required.
4145 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4146 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4151 * Save the peer's label in the request_sock so we can later setup
4152 * smk_packet in the child socket so that SO_PEERCRED can report it.
4154 req->peer_secid = skp->smk_secid;
4157 * We need to decide if we want to label the incoming connection here
4158 * if we do we only need to label the request_sock and the stack will
4159 * propagate the wire-label to the sock when it is created.
4162 addr.sin_addr.s_addr = hdr->saddr;
4164 hskp = smack_ipv4host_label(&addr);
4168 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4170 netlbl_req_delattr(req);
4176 * smack_inet_csk_clone - Copy the connection information to the new socket
4177 * @sk: the new socket
4178 * @req: the connection's request_sock
4180 * Transfer the connection's peer label to the newly created socket.
4182 static void smack_inet_csk_clone(struct sock *sk,
4183 const struct request_sock *req)
4185 struct socket_smack *ssp = sk->sk_security;
4186 struct smack_known *skp;
4188 if (req->peer_secid != 0) {
4189 skp = smack_from_secid(req->peer_secid);
4190 ssp->smk_packet = skp;
4192 ssp->smk_packet = NULL;
4196 * Key management security hooks
4198 * Casey has not tested key support very heavily.
4199 * The permission check is most likely too restrictive.
4200 * If you care about keys please have a look.
4205 * smack_key_alloc - Set the key security blob
4207 * @cred: the credentials to use
4210 * No allocation required
4214 static int smack_key_alloc(struct key *key, const struct cred *cred,
4215 unsigned long flags)
4217 struct smack_known *skp = smk_of_task(cred->security);
4219 key->security = skp;
4224 * smack_key_free - Clear the key security blob
4227 * Clear the blob pointer
4229 static void smack_key_free(struct key *key)
4231 key->security = NULL;
4235 * smack_key_permission - Smack access on a key
4236 * @key_ref: gets to the object
4237 * @cred: the credentials to use
4238 * @perm: requested key permissions
4240 * Return 0 if the task has read and write to the object,
4241 * an error code otherwise
4243 static int smack_key_permission(key_ref_t key_ref,
4244 const struct cred *cred, unsigned perm)
4247 struct smk_audit_info ad;
4248 struct smack_known *tkp = smk_of_task(cred->security);
4253 * Validate requested permissions
4255 if (perm & ~KEY_NEED_ALL)
4258 keyp = key_ref_to_ptr(key_ref);
4262 * If the key hasn't been initialized give it access so that
4265 if (keyp->security == NULL)
4268 * This should not occur
4273 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4277 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4278 ad.a.u.key_struct.key = keyp->serial;
4279 ad.a.u.key_struct.key_desc = keyp->description;
4281 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4282 request |= MAY_READ;
4283 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4284 request |= MAY_WRITE;
4285 rc = smk_access(tkp, keyp->security, request, &ad);
4286 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4291 * smack_key_getsecurity - Smack label tagging the key
4292 * @key points to the key to be queried
4293 * @_buffer points to a pointer that should be set to point to the
4294 * resulting string (if no label or an error occurs).
4295 * Return the length of the string (including terminating NUL) or -ve if
4297 * May also return 0 (and a NULL buffer pointer) if there is no label.
4299 static int smack_key_getsecurity(struct key *key, char **_buffer)
4301 struct smack_known *skp = key->security;
4305 if (key->security == NULL) {
4310 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4313 length = strlen(copy) + 1;
4319 #endif /* CONFIG_KEYS */
4324 * Audit requires a unique representation of each Smack specific
4325 * rule. This unique representation is used to distinguish the
4326 * object to be audited from remaining kernel objects and also
4327 * works as a glue between the audit hooks.
4329 * Since repository entries are added but never deleted, we'll use
4330 * the smack_known label address related to the given audit rule as
4331 * the needed unique representation. This also better fits the smack
4332 * model where nearly everything is a label.
4337 * smack_audit_rule_init - Initialize a smack audit rule
4338 * @field: audit rule fields given from user-space (audit.h)
4339 * @op: required testing operator (=, !=, >, <, ...)
4340 * @rulestr: smack label to be audited
4341 * @vrule: pointer to save our own audit rule representation
4343 * Prepare to audit cases where (@field @op @rulestr) is true.
4344 * The label to be audited is created if necessay.
4346 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4348 struct smack_known *skp;
4349 char **rule = (char **)vrule;
4352 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4355 if (op != Audit_equal && op != Audit_not_equal)
4358 skp = smk_import_entry(rulestr, 0);
4360 return PTR_ERR(skp);
4362 *rule = skp->smk_known;
4368 * smack_audit_rule_known - Distinguish Smack audit rules
4369 * @krule: rule of interest, in Audit kernel representation format
4371 * This is used to filter Smack rules from remaining Audit ones.
4372 * If it's proved that this rule belongs to us, the
4373 * audit_rule_match hook will be called to do the final judgement.
4375 static int smack_audit_rule_known(struct audit_krule *krule)
4377 struct audit_field *f;
4380 for (i = 0; i < krule->field_count; i++) {
4381 f = &krule->fields[i];
4383 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4391 * smack_audit_rule_match - Audit given object ?
4392 * @secid: security id for identifying the object to test
4393 * @field: audit rule flags given from user-space
4394 * @op: required testing operator
4395 * @vrule: smack internal rule presentation
4396 * @actx: audit context associated with the check
4398 * The core Audit hook. It's used to take the decision of
4399 * whether to audit or not to audit a given object.
4401 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4402 struct audit_context *actx)
4404 struct smack_known *skp;
4407 if (unlikely(!rule)) {
4408 WARN_ONCE(1, "Smack: missing rule\n");
4412 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4415 skp = smack_from_secid(secid);
4418 * No need to do string comparisons. If a match occurs,
4419 * both pointers will point to the same smack_known
4422 if (op == Audit_equal)
4423 return (rule == skp->smk_known);
4424 if (op == Audit_not_equal)
4425 return (rule != skp->smk_known);
4431 * There is no need for a smack_audit_rule_free hook.
4432 * No memory was allocated.
4435 #endif /* CONFIG_AUDIT */
4438 * smack_ismaclabel - check if xattr @name references a smack MAC label
4439 * @name: Full xattr name to check.
4441 static int smack_ismaclabel(const char *name)
4443 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4448 * smack_secid_to_secctx - return the smack label for a secid
4449 * @secid: incoming integer
4450 * @secdata: destination
4451 * @seclen: how long it is
4453 * Exists for networking code.
4455 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4457 struct smack_known *skp = smack_from_secid(secid);
4460 *secdata = skp->smk_known;
4461 *seclen = strlen(skp->smk_known);
4466 * smack_secctx_to_secid - return the secid for a smack label
4467 * @secdata: smack label
4468 * @seclen: how long result is
4469 * @secid: outgoing integer
4471 * Exists for audit and networking code.
4473 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4475 struct smack_known *skp = smk_find_entry(secdata);
4478 *secid = skp->smk_secid;
4485 * There used to be a smack_release_secctx hook
4486 * that did nothing back when hooks were in a vector.
4487 * Now that there's a list such a hook adds cost.
4490 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4492 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4495 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4497 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4500 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4502 struct smack_known *skp = smk_of_inode(inode);
4504 *ctx = skp->smk_known;
4505 *ctxlen = strlen(skp->smk_known);
4509 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4512 struct task_smack *tsp;
4513 struct smack_known *skp;
4514 struct inode_smack *isp;
4515 struct cred *new_creds = *new;
4517 if (new_creds == NULL) {
4518 new_creds = prepare_creds();
4519 if (new_creds == NULL)
4523 tsp = new_creds->security;
4526 * Get label from overlay inode and set it in create_sid
4528 isp = d_inode(dentry->d_parent)->i_security;
4529 skp = isp->smk_inode;
4530 tsp->smk_task = skp;
4535 static int smack_inode_copy_up_xattr(const char *name)
4538 * Return 1 if this is the smack access Smack attribute.
4540 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4546 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4548 const struct cred *old,
4551 struct task_smack *otsp = old->security;
4552 struct task_smack *ntsp = new->security;
4553 struct inode_smack *isp;
4557 * Use the process credential unless all of
4558 * the transmuting criteria are met
4560 ntsp->smk_task = otsp->smk_task;
4563 * the attribute of the containing directory
4565 isp = d_inode(dentry->d_parent)->i_security;
4567 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4569 may = smk_access_entry(otsp->smk_task->smk_known,
4570 isp->smk_inode->smk_known,
4571 &otsp->smk_task->smk_rules);
4575 * If the directory is transmuting and the rule
4576 * providing access is transmuting use the containing
4577 * directory label instead of the process label.
4579 if (may > 0 && (may & MAY_TRANSMUTE))
4580 ntsp->smk_task = isp->smk_inode;
4585 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4586 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4587 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4588 LSM_HOOK_INIT(syslog, smack_syslog),
4590 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4591 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4592 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4593 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4594 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4595 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4597 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4599 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4600 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4601 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4602 LSM_HOOK_INIT(inode_link, smack_inode_link),
4603 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4604 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4605 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4606 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4607 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4608 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4609 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4610 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4611 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4612 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4613 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4614 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4615 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4616 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4618 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4619 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4620 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4621 LSM_HOOK_INIT(file_lock, smack_file_lock),
4622 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4623 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4624 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4625 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4626 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4627 LSM_HOOK_INIT(file_receive, smack_file_receive),
4629 LSM_HOOK_INIT(file_open, smack_file_open),
4631 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4632 LSM_HOOK_INIT(cred_free, smack_cred_free),
4633 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4634 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4635 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4636 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4637 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4638 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4639 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4640 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4641 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4642 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4643 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4644 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4645 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4646 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4647 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4648 LSM_HOOK_INIT(task_kill, smack_task_kill),
4649 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4651 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4652 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4654 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4655 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4657 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4658 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
4659 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4660 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4661 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4662 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4664 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4665 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
4666 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4667 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4668 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4670 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4671 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
4672 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4673 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4674 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4676 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4678 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4679 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4681 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4682 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4684 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4685 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4686 #ifdef SMACK_IPV6_PORT_LABELING
4687 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4689 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4690 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4691 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4692 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4693 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4694 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4695 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4696 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4697 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4698 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4700 /* key management security hooks */
4702 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4703 LSM_HOOK_INIT(key_free, smack_key_free),
4704 LSM_HOOK_INIT(key_permission, smack_key_permission),
4705 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4706 #endif /* CONFIG_KEYS */
4710 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4711 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4712 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4713 #endif /* CONFIG_AUDIT */
4715 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4716 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4717 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4718 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4719 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4720 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4721 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4722 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4723 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4727 static __init void init_smack_known_list(void)
4730 * Initialize rule list locks
4732 mutex_init(&smack_known_huh.smk_rules_lock);
4733 mutex_init(&smack_known_hat.smk_rules_lock);
4734 mutex_init(&smack_known_floor.smk_rules_lock);
4735 mutex_init(&smack_known_star.smk_rules_lock);
4736 mutex_init(&smack_known_web.smk_rules_lock);
4738 * Initialize rule lists
4740 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4741 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4742 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4743 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4744 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4746 * Create the known labels list
4748 smk_insert_entry(&smack_known_huh);
4749 smk_insert_entry(&smack_known_hat);
4750 smk_insert_entry(&smack_known_star);
4751 smk_insert_entry(&smack_known_floor);
4752 smk_insert_entry(&smack_known_web);
4756 * smack_init - initialize the smack system
4760 static __init int smack_init(void)
4763 struct task_smack *tsp;
4765 if (!security_module_enable("smack"))
4768 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4769 if (!smack_inode_cache)
4772 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4775 kmem_cache_destroy(smack_inode_cache);
4781 pr_info("Smack: Initializing.\n");
4782 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4783 pr_info("Smack: Netfilter enabled.\n");
4785 #ifdef SMACK_IPV6_PORT_LABELING
4786 pr_info("Smack: IPv6 port labeling enabled.\n");
4788 #ifdef SMACK_IPV6_SECMARK_LABELING
4789 pr_info("Smack: IPv6 Netfilter enabled.\n");
4793 * Set the security state for the initial task.
4795 cred = (struct cred *) current->cred;
4796 cred->security = tsp;
4798 /* initialize the smack_known_list */
4799 init_smack_known_list();
4804 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4810 * Smack requires early initialization in order to label
4811 * all processes and objects when they are created.
4813 DEFINE_LSM(smack) = {