2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/dccp.h>
31 #include <linux/icmpv6.h>
32 #include <linux/slab.h>
33 #include <linux/mutex.h>
34 #include <linux/pipe_fs_i.h>
35 #include <net/cipso_ipv4.h>
38 #include <linux/audit.h>
39 #include <linux/magic.h>
40 #include <linux/dcache.h>
41 #include <linux/personality.h>
42 #include <linux/msg.h>
43 #include <linux/shm.h>
44 #include <linux/binfmts.h>
45 #include <linux/parser.h>
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 #ifdef SMACK_IPV6_PORT_LABELING
56 DEFINE_MUTEX(smack_ipv6_lock);
57 static LIST_HEAD(smk_ipv6_port_list);
59 static struct kmem_cache *smack_inode_cache;
62 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
67 } smk_mount_opts[] = {
68 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
72 static int match_opt_prefix(char *s, int l, char **arg)
76 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
77 size_t len = smk_mount_opts[i].len;
78 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
80 if (len == l || s[len] != '=')
83 return smk_mount_opts[i].opt;
88 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
89 static char *smk_bu_mess[] = {
90 "Bringup Error", /* Unused */
91 "Bringup", /* SMACK_BRINGUP_ALLOW */
92 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
93 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
96 static void smk_bu_mode(int mode, char *s)
102 if (mode & MAY_WRITE)
106 if (mode & MAY_APPEND)
108 if (mode & MAY_TRANSMUTE)
118 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
119 static int smk_bu_note(char *note, struct smack_known *sskp,
120 struct smack_known *oskp, int mode, int rc)
122 char acc[SMK_NUM_ACCESS_TYPE + 1];
126 if (rc > SMACK_UNCONFINED_OBJECT)
129 smk_bu_mode(mode, acc);
130 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
131 sskp->smk_known, oskp->smk_known, acc, note);
135 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
138 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
139 static int smk_bu_current(char *note, struct smack_known *oskp,
142 struct task_smack *tsp = smack_cred(current_cred());
143 char acc[SMK_NUM_ACCESS_TYPE + 1];
147 if (rc > SMACK_UNCONFINED_OBJECT)
150 smk_bu_mode(mode, acc);
151 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
152 tsp->smk_task->smk_known, oskp->smk_known,
153 acc, current->comm, note);
157 #define smk_bu_current(note, oskp, mode, RC) (RC)
160 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
161 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
163 struct task_smack *tsp = smack_cred(current_cred());
164 struct smack_known *smk_task = smk_of_task_struct(otp);
165 char acc[SMK_NUM_ACCESS_TYPE + 1];
169 if (rc > SMACK_UNCONFINED_OBJECT)
172 smk_bu_mode(mode, acc);
173 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
174 tsp->smk_task->smk_known, smk_task->smk_known, acc,
175 current->comm, otp->comm);
179 #define smk_bu_task(otp, mode, RC) (RC)
182 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
183 static int smk_bu_inode(struct inode *inode, int mode, int rc)
185 struct task_smack *tsp = smack_cred(current_cred());
186 struct inode_smack *isp = 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 = smack_cred(current_cred());
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 = smack_cred(cred);
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 = smack_cred(tracercred);
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(smack_cred(current_cred()));
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 = smack_cred(bprm->cred);
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 = smack_cred(current_cred());
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(smack_cred(tsk->cred));
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 = smack_cred(current_cred());
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 = smack_cred(file->f_cred);
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 = smack_cred(cred);
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 = smack_cred(old);
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 = smack_cred(old);
1965 struct task_smack *new_tsp = smack_cred(new);
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);
1972 /* cbs copy rule list */
1976 * smack_cred_getsecid - get the secid corresponding to a creds structure
1977 * @c: the object creds
1978 * @secid: where to put the result
1980 * Sets the secid to contain a u32 version of the smack label.
1982 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1984 struct smack_known *skp;
1987 skp = smk_of_task(smack_cred(cred));
1988 *secid = skp->smk_secid;
1993 * smack_kernel_act_as - Set the subjective context in a set of credentials
1994 * @new: points to the set of credentials to be modified.
1995 * @secid: specifies the security ID to be set
1997 * Set the security data for a kernel service.
1999 static int smack_kernel_act_as(struct cred *new, u32 secid)
2001 struct task_smack *new_tsp = smack_cred(new);
2003 new_tsp->smk_task = smack_from_secid(secid);
2008 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2009 * @new: points to the set of credentials to be modified
2010 * @inode: points to the inode to use as a reference
2012 * Set the file creation context in a set of credentials to the same
2013 * as the objective context of the specified inode
2015 static int smack_kernel_create_files_as(struct cred *new,
2016 struct inode *inode)
2018 struct inode_smack *isp = inode->i_security;
2019 struct task_smack *tsp = smack_cred(new);
2021 tsp->smk_forked = isp->smk_inode;
2022 tsp->smk_task = tsp->smk_forked;
2027 * smk_curacc_on_task - helper to log task related access
2028 * @p: the task object
2029 * @access: the access requested
2030 * @caller: name of the calling function for audit
2032 * Return 0 if access is permitted
2034 static int smk_curacc_on_task(struct task_struct *p, int access,
2037 struct smk_audit_info ad;
2038 struct smack_known *skp = smk_of_task_struct(p);
2041 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2042 smk_ad_setfield_u_tsk(&ad, p);
2043 rc = smk_curacc(skp, access, &ad);
2044 rc = smk_bu_task(p, access, rc);
2049 * smack_task_setpgid - Smack check on setting pgid
2050 * @p: the task object
2053 * Return 0 if write access is permitted
2055 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2057 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2061 * smack_task_getpgid - Smack access check for getpgid
2062 * @p: the object task
2064 * Returns 0 if current can read the object task, error code otherwise
2066 static int smack_task_getpgid(struct task_struct *p)
2068 return smk_curacc_on_task(p, MAY_READ, __func__);
2072 * smack_task_getsid - Smack access check for getsid
2073 * @p: the object task
2075 * Returns 0 if current can read the object task, error code otherwise
2077 static int smack_task_getsid(struct task_struct *p)
2079 return smk_curacc_on_task(p, MAY_READ, __func__);
2083 * smack_task_getsecid - get the secid of the task
2084 * @p: the object task
2085 * @secid: where to put the result
2087 * Sets the secid to contain a u32 version of the smack label.
2089 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2091 struct smack_known *skp = smk_of_task_struct(p);
2093 *secid = skp->smk_secid;
2097 * smack_task_setnice - Smack check on setting nice
2098 * @p: the task object
2101 * Return 0 if write access is permitted
2103 static int smack_task_setnice(struct task_struct *p, int nice)
2105 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2109 * smack_task_setioprio - Smack check on setting ioprio
2110 * @p: the task object
2113 * Return 0 if write access is permitted
2115 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2117 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2121 * smack_task_getioprio - Smack check on reading ioprio
2122 * @p: the task object
2124 * Return 0 if read access is permitted
2126 static int smack_task_getioprio(struct task_struct *p)
2128 return smk_curacc_on_task(p, MAY_READ, __func__);
2132 * smack_task_setscheduler - Smack check on setting scheduler
2133 * @p: the task object
2137 * Return 0 if read access is permitted
2139 static int smack_task_setscheduler(struct task_struct *p)
2141 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2145 * smack_task_getscheduler - Smack check on reading scheduler
2146 * @p: the task object
2148 * Return 0 if read access is permitted
2150 static int smack_task_getscheduler(struct task_struct *p)
2152 return smk_curacc_on_task(p, MAY_READ, __func__);
2156 * smack_task_movememory - Smack check on moving memory
2157 * @p: the task object
2159 * Return 0 if write access is permitted
2161 static int smack_task_movememory(struct task_struct *p)
2163 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2167 * smack_task_kill - Smack check on signal delivery
2168 * @p: the task object
2171 * @cred: identifies the cred to use in lieu of current's
2173 * Return 0 if write access is permitted
2176 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2177 int sig, const struct cred *cred)
2179 struct smk_audit_info ad;
2180 struct smack_known *skp;
2181 struct smack_known *tkp = smk_of_task_struct(p);
2185 return 0; /* null signal; existence test */
2187 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2188 smk_ad_setfield_u_tsk(&ad, p);
2190 * Sending a signal requires that the sender
2191 * can write the receiver.
2194 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2195 rc = smk_bu_task(p, MAY_DELIVER, rc);
2199 * If the cred isn't NULL we're dealing with some USB IO
2200 * specific behavior. This is not clean. For one thing
2201 * we can't take privilege into account.
2203 skp = smk_of_task(smack_cred(cred));
2204 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2205 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2210 * smack_task_to_inode - copy task smack into the inode blob
2211 * @p: task to copy from
2212 * @inode: inode to copy to
2214 * Sets the smack pointer in the inode security blob
2216 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2218 struct inode_smack *isp = inode->i_security;
2219 struct smack_known *skp = smk_of_task_struct(p);
2221 isp->smk_inode = skp;
2222 isp->smk_flags |= SMK_INODE_INSTANT;
2230 * smack_sk_alloc_security - Allocate a socket blob
2233 * @gfp_flags: memory allocation flags
2235 * Assign Smack pointers to current
2237 * Returns 0 on success, -ENOMEM is there's no memory
2239 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2241 struct smack_known *skp = smk_of_current();
2242 struct socket_smack *ssp;
2244 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2249 * Sockets created by kernel threads receive web label.
2251 if (unlikely(current->flags & PF_KTHREAD)) {
2252 ssp->smk_in = &smack_known_web;
2253 ssp->smk_out = &smack_known_web;
2258 ssp->smk_packet = NULL;
2260 sk->sk_security = ssp;
2266 * smack_sk_free_security - Free a socket blob
2269 * Clears the blob pointer
2271 static void smack_sk_free_security(struct sock *sk)
2273 #ifdef SMACK_IPV6_PORT_LABELING
2274 struct smk_port_label *spp;
2276 if (sk->sk_family == PF_INET6) {
2278 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2279 if (spp->smk_sock != sk)
2281 spp->smk_can_reuse = 1;
2287 kfree(sk->sk_security);
2291 * smack_ipv4host_label - check host based restrictions
2292 * @sip: the object end
2294 * looks for host based access restrictions
2296 * This version will only be appropriate for really small sets of single label
2297 * hosts. The caller is responsible for ensuring that the RCU read lock is
2298 * taken before calling this function.
2300 * Returns the label of the far end or NULL if it's not special.
2302 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2304 struct smk_net4addr *snp;
2305 struct in_addr *siap = &sip->sin_addr;
2307 if (siap->s_addr == 0)
2310 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2312 * we break after finding the first match because
2313 * the list is sorted from longest to shortest mask
2314 * so we have found the most specific match
2316 if (snp->smk_host.s_addr ==
2317 (siap->s_addr & snp->smk_mask.s_addr))
2318 return snp->smk_label;
2323 #if IS_ENABLED(CONFIG_IPV6)
2325 * smk_ipv6_localhost - Check for local ipv6 host address
2328 * Returns boolean true if this is the localhost address
2330 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2332 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2333 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2335 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2336 ntohs(be16p[7]) == 1)
2342 * smack_ipv6host_label - check host based restrictions
2343 * @sip: the object end
2345 * looks for host based access restrictions
2347 * This version will only be appropriate for really small sets of single label
2348 * hosts. The caller is responsible for ensuring that the RCU read lock is
2349 * taken before calling this function.
2351 * Returns the label of the far end or NULL if it's not special.
2353 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2355 struct smk_net6addr *snp;
2356 struct in6_addr *sap = &sip->sin6_addr;
2361 * It's local. Don't look for a host label.
2363 if (smk_ipv6_localhost(sip))
2366 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2368 * If the label is NULL the entry has
2369 * been renounced. Ignore it.
2371 if (snp->smk_label == NULL)
2374 * we break after finding the first match because
2375 * the list is sorted from longest to shortest mask
2376 * so we have found the most specific match
2378 for (found = 1, i = 0; i < 8; i++) {
2379 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2380 snp->smk_host.s6_addr16[i]) {
2386 return snp->smk_label;
2391 #endif /* CONFIG_IPV6 */
2394 * smack_netlabel - Set the secattr on a socket
2396 * @labeled: socket label scheme
2398 * Convert the outbound smack value (smk_out) to a
2399 * secattr and attach it to the socket.
2401 * Returns 0 on success or an error code
2403 static int smack_netlabel(struct sock *sk, int labeled)
2405 struct smack_known *skp;
2406 struct socket_smack *ssp = sk->sk_security;
2410 * Usually the netlabel code will handle changing the
2411 * packet labeling based on the label.
2412 * The case of a single label host is different, because
2413 * a single label host should never get a labeled packet
2414 * even though the label is usually associated with a packet
2418 bh_lock_sock_nested(sk);
2420 if (ssp->smk_out == smack_net_ambient ||
2421 labeled == SMACK_UNLABELED_SOCKET)
2422 netlbl_sock_delattr(sk);
2425 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2435 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2437 * @sap: the destination address
2439 * Set the correct secattr for the given socket based on the destination
2440 * address and perform any outbound access checks needed.
2442 * Returns 0 on success or an error code.
2445 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2447 struct smack_known *skp;
2450 struct smack_known *hkp;
2451 struct socket_smack *ssp = sk->sk_security;
2452 struct smk_audit_info ad;
2455 hkp = smack_ipv4host_label(sap);
2458 struct lsm_network_audit net;
2460 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2461 ad.a.u.net->family = sap->sin_family;
2462 ad.a.u.net->dport = sap->sin_port;
2463 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2465 sk_lbl = SMACK_UNLABELED_SOCKET;
2467 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2468 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2470 sk_lbl = SMACK_CIPSO_SOCKET;
2477 return smack_netlabel(sk, sk_lbl);
2480 #if IS_ENABLED(CONFIG_IPV6)
2482 * smk_ipv6_check - check Smack access
2483 * @subject: subject Smack label
2484 * @object: object Smack label
2486 * @act: the action being taken
2488 * Check an IPv6 access
2490 static int smk_ipv6_check(struct smack_known *subject,
2491 struct smack_known *object,
2492 struct sockaddr_in6 *address, int act)
2495 struct lsm_network_audit net;
2497 struct smk_audit_info ad;
2501 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2502 ad.a.u.net->family = PF_INET6;
2503 ad.a.u.net->dport = ntohs(address->sin6_port);
2504 if (act == SMK_RECEIVING)
2505 ad.a.u.net->v6info.saddr = address->sin6_addr;
2507 ad.a.u.net->v6info.daddr = address->sin6_addr;
2509 rc = smk_access(subject, object, MAY_WRITE, &ad);
2510 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2513 #endif /* CONFIG_IPV6 */
2515 #ifdef SMACK_IPV6_PORT_LABELING
2517 * smk_ipv6_port_label - Smack port access table management
2521 * Create or update the port list entry
2523 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2525 struct sock *sk = sock->sk;
2526 struct sockaddr_in6 *addr6;
2527 struct socket_smack *ssp = sock->sk->sk_security;
2528 struct smk_port_label *spp;
2529 unsigned short port = 0;
2531 if (address == NULL) {
2533 * This operation is changing the Smack information
2534 * on the bound socket. Take the changes to the port
2538 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2539 if (sk != spp->smk_sock)
2541 spp->smk_in = ssp->smk_in;
2542 spp->smk_out = ssp->smk_out;
2547 * A NULL address is only used for updating existing
2548 * bound entries. If there isn't one, it's OK.
2554 addr6 = (struct sockaddr_in6 *)address;
2555 port = ntohs(addr6->sin6_port);
2557 * This is a special case that is safely ignored.
2563 * Look for an existing port list entry.
2564 * This is an indication that a port is getting reused.
2567 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2568 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2570 if (spp->smk_can_reuse != 1) {
2574 spp->smk_port = port;
2576 spp->smk_in = ssp->smk_in;
2577 spp->smk_out = ssp->smk_out;
2578 spp->smk_can_reuse = 0;
2584 * A new port entry is required.
2586 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2590 spp->smk_port = port;
2592 spp->smk_in = ssp->smk_in;
2593 spp->smk_out = ssp->smk_out;
2594 spp->smk_sock_type = sock->type;
2595 spp->smk_can_reuse = 0;
2597 mutex_lock(&smack_ipv6_lock);
2598 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2599 mutex_unlock(&smack_ipv6_lock);
2604 * smk_ipv6_port_check - check Smack port access
2608 * Create or update the port list entry
2610 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2613 struct smk_port_label *spp;
2614 struct socket_smack *ssp = sk->sk_security;
2615 struct smack_known *skp = NULL;
2616 unsigned short port;
2617 struct smack_known *object;
2619 if (act == SMK_RECEIVING) {
2620 skp = smack_ipv6host_label(address);
2621 object = ssp->smk_in;
2624 object = smack_ipv6host_label(address);
2628 * The other end is a single label host.
2630 if (skp != NULL && object != NULL)
2631 return smk_ipv6_check(skp, object, address, act);
2633 skp = smack_net_ambient;
2635 object = smack_net_ambient;
2638 * It's remote, so port lookup does no good.
2640 if (!smk_ipv6_localhost(address))
2641 return smk_ipv6_check(skp, object, address, act);
2644 * It's local so the send check has to have passed.
2646 if (act == SMK_RECEIVING)
2649 port = ntohs(address->sin6_port);
2651 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2652 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2654 object = spp->smk_in;
2655 if (act == SMK_CONNECTING)
2656 ssp->smk_packet = spp->smk_out;
2661 return smk_ipv6_check(skp, object, address, act);
2663 #endif /* SMACK_IPV6_PORT_LABELING */
2666 * smack_inode_setsecurity - set smack xattrs
2667 * @inode: the object
2668 * @name: attribute name
2669 * @value: attribute value
2670 * @size: size of the attribute
2673 * Sets the named attribute in the appropriate blob
2675 * Returns 0 on success, or an error code
2677 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2678 const void *value, size_t size, int flags)
2680 struct smack_known *skp;
2681 struct inode_smack *nsp = inode->i_security;
2682 struct socket_smack *ssp;
2683 struct socket *sock;
2686 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2689 skp = smk_import_entry(value, size);
2691 return PTR_ERR(skp);
2693 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2694 nsp->smk_inode = skp;
2695 nsp->smk_flags |= SMK_INODE_INSTANT;
2699 * The rest of the Smack xattrs are only on sockets.
2701 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2704 sock = SOCKET_I(inode);
2705 if (sock == NULL || sock->sk == NULL)
2708 ssp = sock->sk->sk_security;
2710 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2712 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2714 if (sock->sk->sk_family == PF_INET) {
2715 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2718 "Smack: \"%s\" netlbl error %d.\n",
2724 #ifdef SMACK_IPV6_PORT_LABELING
2725 if (sock->sk->sk_family == PF_INET6)
2726 smk_ipv6_port_label(sock, NULL);
2733 * smack_socket_post_create - finish socket setup
2735 * @family: protocol family
2740 * Sets the netlabel information on the socket
2742 * Returns 0 on success, and error code otherwise
2744 static int smack_socket_post_create(struct socket *sock, int family,
2745 int type, int protocol, int kern)
2747 struct socket_smack *ssp;
2749 if (sock->sk == NULL)
2753 * Sockets created by kernel threads receive web label.
2755 if (unlikely(current->flags & PF_KTHREAD)) {
2756 ssp = sock->sk->sk_security;
2757 ssp->smk_in = &smack_known_web;
2758 ssp->smk_out = &smack_known_web;
2761 if (family != PF_INET)
2764 * Set the outbound netlbl.
2766 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2770 * smack_socket_socketpair - create socket pair
2771 * @socka: one socket
2772 * @sockb: another socket
2774 * Cross reference the peer labels for SO_PEERSEC
2776 * Returns 0 on success, and error code otherwise
2778 static int smack_socket_socketpair(struct socket *socka,
2779 struct socket *sockb)
2781 struct socket_smack *asp = socka->sk->sk_security;
2782 struct socket_smack *bsp = sockb->sk->sk_security;
2784 asp->smk_packet = bsp->smk_out;
2785 bsp->smk_packet = asp->smk_out;
2790 #ifdef SMACK_IPV6_PORT_LABELING
2792 * smack_socket_bind - record port binding information.
2794 * @address: the port address
2795 * @addrlen: size of the address
2797 * Records the label bound to a port.
2801 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2804 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2805 smk_ipv6_port_label(sock, address);
2808 #endif /* SMACK_IPV6_PORT_LABELING */
2811 * smack_socket_connect - connect access check
2813 * @sap: the other end
2814 * @addrlen: size of sap
2816 * Verifies that a connection may be possible
2818 * Returns 0 on success, and error code otherwise
2820 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2824 #if IS_ENABLED(CONFIG_IPV6)
2825 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2827 #ifdef SMACK_IPV6_SECMARK_LABELING
2828 struct smack_known *rsp;
2829 struct socket_smack *ssp;
2832 if (sock->sk == NULL)
2835 #ifdef SMACK_IPV6_SECMARK_LABELING
2836 ssp = sock->sk->sk_security;
2839 switch (sock->sk->sk_family) {
2841 if (addrlen < sizeof(struct sockaddr_in))
2843 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2846 if (addrlen < sizeof(struct sockaddr_in6))
2848 #ifdef SMACK_IPV6_SECMARK_LABELING
2849 rsp = smack_ipv6host_label(sip);
2851 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2854 #ifdef SMACK_IPV6_PORT_LABELING
2855 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2863 * smack_flags_to_may - convert S_ to MAY_ values
2864 * @flags: the S_ value
2866 * Returns the equivalent MAY_ value
2868 static int smack_flags_to_may(int flags)
2872 if (flags & S_IRUGO)
2874 if (flags & S_IWUGO)
2876 if (flags & S_IXUGO)
2883 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2888 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2890 struct smack_known *skp = smk_of_current();
2892 msg->security = skp;
2897 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2900 * Clears the blob pointer
2902 static void smack_msg_msg_free_security(struct msg_msg *msg)
2904 msg->security = NULL;
2908 * smack_of_ipc - the smack pointer for the ipc
2911 * Returns a pointer to the smack value
2913 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2915 return (struct smack_known *)isp->security;
2919 * smack_ipc_alloc_security - Set the security blob for ipc
2924 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2926 struct smack_known *skp = smk_of_current();
2928 isp->security = skp;
2933 * smack_ipc_free_security - Clear the security blob for ipc
2936 * Clears the blob pointer
2938 static void smack_ipc_free_security(struct kern_ipc_perm *isp)
2940 isp->security = NULL;
2944 * smk_curacc_shm : check if current has access on shm
2946 * @access : access requested
2948 * Returns 0 if current has the requested access, error code otherwise
2950 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2952 struct smack_known *ssp = smack_of_ipc(isp);
2953 struct smk_audit_info ad;
2957 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2958 ad.a.u.ipc_id = isp->id;
2960 rc = smk_curacc(ssp, access, &ad);
2961 rc = smk_bu_current("shm", ssp, access, rc);
2966 * smack_shm_associate - Smack access check for shm
2968 * @shmflg: access requested
2970 * Returns 0 if current has the requested access, error code otherwise
2972 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2976 may = smack_flags_to_may(shmflg);
2977 return smk_curacc_shm(isp, may);
2981 * smack_shm_shmctl - Smack access check for shm
2983 * @cmd: what it wants to do
2985 * Returns 0 if current has the requested access, error code otherwise
2987 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3001 may = MAY_READWRITE;
3006 * System level information.
3012 return smk_curacc_shm(isp, may);
3016 * smack_shm_shmat - Smack access for shmat
3019 * @shmflg: access requested
3021 * Returns 0 if current has the requested access, error code otherwise
3023 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3028 may = smack_flags_to_may(shmflg);
3029 return smk_curacc_shm(ipc, may);
3033 * smk_curacc_sem : check if current has access on sem
3035 * @access : access requested
3037 * Returns 0 if current has the requested access, error code otherwise
3039 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3041 struct smack_known *ssp = smack_of_ipc(isp);
3042 struct smk_audit_info ad;
3046 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3047 ad.a.u.ipc_id = isp->id;
3049 rc = smk_curacc(ssp, access, &ad);
3050 rc = smk_bu_current("sem", ssp, access, rc);
3055 * smack_sem_associate - Smack access check for sem
3057 * @semflg: access requested
3059 * Returns 0 if current has the requested access, error code otherwise
3061 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3065 may = smack_flags_to_may(semflg);
3066 return smk_curacc_sem(isp, may);
3070 * smack_sem_shmctl - Smack access check for sem
3072 * @cmd: what it wants to do
3074 * Returns 0 if current has the requested access, error code otherwise
3076 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3095 may = MAY_READWRITE;
3100 * System level information
3107 return smk_curacc_sem(isp, may);
3111 * smack_sem_semop - Smack checks of semaphore operations
3117 * Treated as read and write in all cases.
3119 * Returns 0 if access is allowed, error code otherwise
3121 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3122 unsigned nsops, int alter)
3124 return smk_curacc_sem(isp, MAY_READWRITE);
3128 * smk_curacc_msq : helper to check if current has access on msq
3130 * @access : access requested
3132 * return 0 if current has access, error otherwise
3134 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3136 struct smack_known *msp = smack_of_ipc(isp);
3137 struct smk_audit_info ad;
3141 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3142 ad.a.u.ipc_id = isp->id;
3144 rc = smk_curacc(msp, access, &ad);
3145 rc = smk_bu_current("msq", msp, access, rc);
3150 * smack_msg_queue_associate - Smack access check for msg_queue
3152 * @msqflg: access requested
3154 * Returns 0 if current has the requested access, error code otherwise
3156 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3160 may = smack_flags_to_may(msqflg);
3161 return smk_curacc_msq(isp, may);
3165 * smack_msg_queue_msgctl - Smack access check for msg_queue
3167 * @cmd: what it wants to do
3169 * Returns 0 if current has the requested access, error code otherwise
3171 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3183 may = MAY_READWRITE;
3188 * System level information
3195 return smk_curacc_msq(isp, may);
3199 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3202 * @msqflg: access requested
3204 * Returns 0 if current has the requested access, error code otherwise
3206 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3211 may = smack_flags_to_may(msqflg);
3212 return smk_curacc_msq(isp, may);
3216 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3223 * Returns 0 if current has read and write access, error code otherwise
3225 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3226 struct task_struct *target, long type, int mode)
3228 return smk_curacc_msq(isp, MAY_READWRITE);
3232 * smack_ipc_permission - Smack access for ipc_permission()
3233 * @ipp: the object permissions
3234 * @flag: access requested
3236 * Returns 0 if current has read and write access, error code otherwise
3238 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3240 struct smack_known *iskp = ipp->security;
3241 int may = smack_flags_to_may(flag);
3242 struct smk_audit_info ad;
3246 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3247 ad.a.u.ipc_id = ipp->id;
3249 rc = smk_curacc(iskp, may, &ad);
3250 rc = smk_bu_current("svipc", iskp, may, rc);
3255 * smack_ipc_getsecid - Extract smack security id
3256 * @ipp: the object permissions
3257 * @secid: where result will be saved
3259 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3261 struct smack_known *iskp = ipp->security;
3263 *secid = iskp->smk_secid;
3267 * smack_d_instantiate - Make sure the blob is correct on an inode
3268 * @opt_dentry: dentry where inode will be attached
3269 * @inode: the object
3271 * Set the inode's security blob if it hasn't been done already.
3273 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3275 struct super_block *sbp;
3276 struct superblock_smack *sbsp;
3277 struct inode_smack *isp;
3278 struct smack_known *skp;
3279 struct smack_known *ckp = smk_of_current();
3280 struct smack_known *final;
3281 char trattr[TRANS_TRUE_SIZE];
3289 isp = inode->i_security;
3291 mutex_lock(&isp->smk_lock);
3293 * If the inode is already instantiated
3294 * take the quick way out
3296 if (isp->smk_flags & SMK_INODE_INSTANT)
3300 sbsp = sbp->s_security;
3302 * We're going to use the superblock default label
3303 * if there's no label on the file.
3305 final = sbsp->smk_default;
3308 * If this is the root inode the superblock
3309 * may be in the process of initialization.
3310 * If that is the case use the root value out
3311 * of the superblock.
3313 if (opt_dentry->d_parent == opt_dentry) {
3314 switch (sbp->s_magic) {
3315 case CGROUP_SUPER_MAGIC:
3316 case CGROUP2_SUPER_MAGIC:
3318 * The cgroup filesystem is never mounted,
3319 * so there's no opportunity to set the mount
3322 sbsp->smk_root = &smack_known_star;
3323 sbsp->smk_default = &smack_known_star;
3324 isp->smk_inode = sbsp->smk_root;
3328 * What about shmem/tmpfs anonymous files with dentry
3329 * obtained from d_alloc_pseudo()?
3331 isp->smk_inode = smk_of_current();
3334 isp->smk_inode = smk_of_current();
3338 * Socket access is controlled by the socket
3339 * structures associated with the task involved.
3341 isp->smk_inode = &smack_known_star;
3344 isp->smk_inode = sbsp->smk_root;
3347 isp->smk_flags |= SMK_INODE_INSTANT;
3352 * This is pretty hackish.
3353 * Casey says that we shouldn't have to do
3354 * file system specific code, but it does help
3355 * with keeping it simple.
3357 switch (sbp->s_magic) {
3359 case CGROUP_SUPER_MAGIC:
3360 case CGROUP2_SUPER_MAGIC:
3362 * Casey says that it's a little embarrassing
3363 * that the smack file system doesn't do
3364 * extended attributes.
3366 * Cgroupfs is special
3368 final = &smack_known_star;
3370 case DEVPTS_SUPER_MAGIC:
3372 * devpts seems content with the label of the task.
3373 * Programs that change smack have to treat the
3378 case PROC_SUPER_MAGIC:
3380 * Casey says procfs appears not to care.
3381 * The superblock default suffices.
3386 * Device labels should come from the filesystem,
3387 * but watch out, because they're volitile,
3388 * getting recreated on every reboot.
3390 final = &smack_known_star;
3394 * If a smack value has been set we want to use it,
3395 * but since tmpfs isn't giving us the opportunity
3396 * to set mount options simulate setting the
3397 * superblock default.
3401 * This isn't an understood special case.
3402 * Get the value from the xattr.
3406 * UNIX domain sockets use lower level socket data.
3408 if (S_ISSOCK(inode->i_mode)) {
3409 final = &smack_known_star;
3413 * No xattr support means, alas, no SMACK label.
3414 * Use the aforeapplied default.
3415 * It would be curious if the label of the task
3416 * does not match that assigned.
3418 if (!(inode->i_opflags & IOP_XATTR))
3421 * Get the dentry for xattr.
3423 dp = dget(opt_dentry);
3424 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3425 if (!IS_ERR_OR_NULL(skp))
3429 * Transmuting directory
3431 if (S_ISDIR(inode->i_mode)) {
3433 * If this is a new directory and the label was
3434 * transmuted when the inode was initialized
3435 * set the transmute attribute on the directory
3436 * and mark the inode.
3438 * If there is a transmute attribute on the
3439 * directory mark the inode.
3441 if (isp->smk_flags & SMK_INODE_CHANGED) {
3442 isp->smk_flags &= ~SMK_INODE_CHANGED;
3443 rc = __vfs_setxattr(dp, inode,
3444 XATTR_NAME_SMACKTRANSMUTE,
3445 TRANS_TRUE, TRANS_TRUE_SIZE,
3448 rc = __vfs_getxattr(dp, inode,
3449 XATTR_NAME_SMACKTRANSMUTE, trattr,
3451 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3452 TRANS_TRUE_SIZE) != 0)
3456 transflag = SMK_INODE_TRANSMUTE;
3459 * Don't let the exec or mmap label be "*" or "@".
3461 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3462 if (IS_ERR(skp) || skp == &smack_known_star ||
3463 skp == &smack_known_web)
3465 isp->smk_task = skp;
3467 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3468 if (IS_ERR(skp) || skp == &smack_known_star ||
3469 skp == &smack_known_web)
3471 isp->smk_mmap = skp;
3478 isp->smk_inode = ckp;
3480 isp->smk_inode = final;
3482 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3485 mutex_unlock(&isp->smk_lock);
3490 * smack_getprocattr - Smack process attribute access
3491 * @p: the object task
3492 * @name: the name of the attribute in /proc/.../attr
3493 * @value: where to put the result
3495 * Places a copy of the task Smack into value
3497 * Returns the length of the smack label or an error code
3499 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3501 struct smack_known *skp = smk_of_task_struct(p);
3505 if (strcmp(name, "current") != 0)
3508 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3518 * smack_setprocattr - Smack process attribute setting
3519 * @name: the name of the attribute in /proc/.../attr
3520 * @value: the value to set
3521 * @size: the size of the value
3523 * Sets the Smack value of the task. Only setting self
3524 * is permitted and only with privilege
3526 * Returns the length of the smack label or an error code
3528 static int smack_setprocattr(const char *name, void *value, size_t size)
3530 struct task_smack *tsp = smack_cred(current_cred());
3532 struct smack_known *skp;
3533 struct smack_known_list_elem *sklep;
3536 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3539 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3542 if (strcmp(name, "current") != 0)
3545 skp = smk_import_entry(value, size);
3547 return PTR_ERR(skp);
3550 * No process is ever allowed the web ("@") label
3551 * and the star ("*") label.
3553 if (skp == &smack_known_web || skp == &smack_known_star)
3556 if (!smack_privileged(CAP_MAC_ADMIN)) {
3558 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3559 if (sklep->smk_label == skp) {
3567 new = prepare_creds();
3571 tsp = smack_cred(new);
3572 tsp->smk_task = skp;
3574 * process can change its label only once
3576 smk_destroy_label_list(&tsp->smk_relabel);
3583 * smack_unix_stream_connect - Smack access on UDS
3585 * @other: the other sock
3588 * Return 0 if a subject with the smack of sock could access
3589 * an object with the smack of other, otherwise an error code
3591 static int smack_unix_stream_connect(struct sock *sock,
3592 struct sock *other, struct sock *newsk)
3594 struct smack_known *skp;
3595 struct smack_known *okp;
3596 struct socket_smack *ssp = sock->sk_security;
3597 struct socket_smack *osp = other->sk_security;
3598 struct socket_smack *nsp = newsk->sk_security;
3599 struct smk_audit_info ad;
3602 struct lsm_network_audit net;
3605 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3609 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3610 smk_ad_setfield_u_net_sk(&ad, other);
3612 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3613 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3617 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3618 rc = smk_bu_note("UDS connect", okp, skp,
3624 * Cross reference the peer labels for SO_PEERSEC.
3627 nsp->smk_packet = ssp->smk_out;
3628 ssp->smk_packet = osp->smk_out;
3635 * smack_unix_may_send - Smack access on UDS
3637 * @other: the other socket
3639 * Return 0 if a subject with the smack of sock could access
3640 * an object with the smack of other, otherwise an error code
3642 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3644 struct socket_smack *ssp = sock->sk->sk_security;
3645 struct socket_smack *osp = other->sk->sk_security;
3646 struct smk_audit_info ad;
3650 struct lsm_network_audit net;
3652 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3653 smk_ad_setfield_u_net_sk(&ad, other->sk);
3656 if (smack_privileged(CAP_MAC_OVERRIDE))
3659 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3660 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3665 * smack_socket_sendmsg - Smack check based on destination host
3668 * @size: the size of the message
3670 * Return 0 if the current subject can write to the destination host.
3671 * For IPv4 this is only a question if the destination is a single label host.
3672 * For IPv6 this is a check against the label of the port.
3674 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3677 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3678 #if IS_ENABLED(CONFIG_IPV6)
3679 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3681 #ifdef SMACK_IPV6_SECMARK_LABELING
3682 struct socket_smack *ssp = sock->sk->sk_security;
3683 struct smack_known *rsp;
3688 * Perfectly reasonable for this to be NULL
3693 switch (sock->sk->sk_family) {
3695 rc = smack_netlabel_send(sock->sk, sip);
3698 #ifdef SMACK_IPV6_SECMARK_LABELING
3699 rsp = smack_ipv6host_label(sap);
3701 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3704 #ifdef SMACK_IPV6_PORT_LABELING
3705 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3713 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3714 * @sap: netlabel secattr
3715 * @ssp: socket security information
3717 * Returns a pointer to a Smack label entry found on the label list.
3719 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3720 struct socket_smack *ssp)
3722 struct smack_known *skp;
3727 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3729 * Looks like a CIPSO packet.
3730 * If there are flags but no level netlabel isn't
3731 * behaving the way we expect it to.
3733 * Look it up in the label table
3734 * Without guidance regarding the smack value
3735 * for the packet fall back on the network
3739 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3740 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3743 * Compare the catsets. Use the netlbl APIs.
3745 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3746 if ((skp->smk_netlabel.flags &
3747 NETLBL_SECATTR_MLS_CAT) == 0)
3751 for (acat = -1, kcat = -1; acat == kcat; ) {
3752 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3754 kcat = netlbl_catmap_walk(
3755 skp->smk_netlabel.attr.mls.cat,
3757 if (acat < 0 || kcat < 0)
3770 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3771 return &smack_known_web;
3772 return &smack_known_star;
3774 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3776 * Looks like a fallback, which gives us a secid.
3778 return smack_from_secid(sap->attr.secid);
3780 * Without guidance regarding the smack value
3781 * for the packet fall back on the network
3784 return smack_net_ambient;
3787 #if IS_ENABLED(CONFIG_IPV6)
3788 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3792 int proto = -EINVAL;
3793 struct ipv6hdr _ipv6h;
3794 struct ipv6hdr *ip6;
3796 struct tcphdr _tcph, *th;
3797 struct udphdr _udph, *uh;
3798 struct dccp_hdr _dccph, *dh;
3802 offset = skb_network_offset(skb);
3803 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3806 sip->sin6_addr = ip6->saddr;
3808 nexthdr = ip6->nexthdr;
3809 offset += sizeof(_ipv6h);
3810 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3817 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3819 sip->sin6_port = th->source;
3822 case IPPROTO_UDPLITE:
3823 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3825 sip->sin6_port = uh->source;
3828 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3830 sip->sin6_port = dh->dccph_sport;
3835 #endif /* CONFIG_IPV6 */
3838 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3842 * Returns 0 if the packet should be delivered, an error code otherwise
3844 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3846 struct netlbl_lsm_secattr secattr;
3847 struct socket_smack *ssp = sk->sk_security;
3848 struct smack_known *skp = NULL;
3850 struct smk_audit_info ad;
3851 u16 family = sk->sk_family;
3853 struct lsm_network_audit net;
3855 #if IS_ENABLED(CONFIG_IPV6)
3856 struct sockaddr_in6 sadd;
3859 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3861 #endif /* CONFIG_IPV6 */
3865 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3867 * If there is a secmark use it rather than the CIPSO label.
3868 * If there is no secmark fall back to CIPSO.
3869 * The secmark is assumed to reflect policy better.
3871 if (skb && skb->secmark != 0) {
3872 skp = smack_from_secid(skb->secmark);
3875 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3877 * Translate what netlabel gave us.
3879 netlbl_secattr_init(&secattr);
3881 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3883 skp = smack_from_secattr(&secattr, ssp);
3885 skp = smack_net_ambient;
3887 netlbl_secattr_destroy(&secattr);
3889 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3893 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3894 ad.a.u.net->family = family;
3895 ad.a.u.net->netif = skb->skb_iif;
3896 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3899 * Receiving a packet requires that the other end
3900 * be able to write here. Read access is not required.
3901 * This is the simplist possible security model
3904 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3905 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3908 netlbl_skbuff_err(skb, family, rc, 0);
3910 #if IS_ENABLED(CONFIG_IPV6)
3912 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3913 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3914 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3916 #ifdef SMACK_IPV6_SECMARK_LABELING
3917 if (skb && skb->secmark != 0)
3918 skp = smack_from_secid(skb->secmark);
3920 skp = smack_ipv6host_label(&sadd);
3922 skp = smack_net_ambient;
3924 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3925 ad.a.u.net->family = family;
3926 ad.a.u.net->netif = skb->skb_iif;
3927 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3928 #endif /* CONFIG_AUDIT */
3929 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3930 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3932 #endif /* SMACK_IPV6_SECMARK_LABELING */
3933 #ifdef SMACK_IPV6_PORT_LABELING
3934 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3935 #endif /* SMACK_IPV6_PORT_LABELING */
3937 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3938 ICMPV6_ADM_PROHIBITED, 0);
3940 #endif /* CONFIG_IPV6 */
3947 * smack_socket_getpeersec_stream - pull in packet label
3949 * @optval: user's destination
3950 * @optlen: size thereof
3953 * returns zero on success, an error code otherwise
3955 static int smack_socket_getpeersec_stream(struct socket *sock,
3956 char __user *optval,
3957 int __user *optlen, unsigned len)
3959 struct socket_smack *ssp;
3964 ssp = sock->sk->sk_security;
3965 if (ssp->smk_packet != NULL) {
3966 rcp = ssp->smk_packet->smk_known;
3967 slen = strlen(rcp) + 1;
3972 else if (copy_to_user(optval, rcp, slen) != 0)
3975 if (put_user(slen, optlen) != 0)
3983 * smack_socket_getpeersec_dgram - pull in packet label
3984 * @sock: the peer socket
3986 * @secid: pointer to where to put the secid of the packet
3988 * Sets the netlabel socket state on sk from parent
3990 static int smack_socket_getpeersec_dgram(struct socket *sock,
3991 struct sk_buff *skb, u32 *secid)
3994 struct netlbl_lsm_secattr secattr;
3995 struct socket_smack *ssp = NULL;
3996 struct smack_known *skp;
3997 int family = PF_UNSPEC;
3998 u32 s = 0; /* 0 is the invalid secid */
4002 if (skb->protocol == htons(ETH_P_IP))
4004 #if IS_ENABLED(CONFIG_IPV6)
4005 else if (skb->protocol == htons(ETH_P_IPV6))
4007 #endif /* CONFIG_IPV6 */
4009 if (family == PF_UNSPEC && sock != NULL)
4010 family = sock->sk->sk_family;
4014 ssp = sock->sk->sk_security;
4015 s = ssp->smk_out->smk_secid;
4018 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4024 * Translate what netlabel gave us.
4026 if (sock != NULL && sock->sk != NULL)
4027 ssp = sock->sk->sk_security;
4028 netlbl_secattr_init(&secattr);
4029 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4031 skp = smack_from_secattr(&secattr, ssp);
4034 netlbl_secattr_destroy(&secattr);
4037 #ifdef SMACK_IPV6_SECMARK_LABELING
4049 * smack_sock_graft - Initialize a newly created socket with an existing sock
4051 * @parent: parent socket
4053 * Set the smk_{in,out} state of an existing sock based on the process that
4054 * is creating the new socket.
4056 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4058 struct socket_smack *ssp;
4059 struct smack_known *skp = smk_of_current();
4062 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4065 ssp = sk->sk_security;
4068 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4072 * smack_inet_conn_request - Smack access check on connect
4073 * @sk: socket involved
4077 * Returns 0 if a task with the packet label could write to
4078 * the socket, otherwise an error code
4080 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4081 struct request_sock *req)
4083 u16 family = sk->sk_family;
4084 struct smack_known *skp;
4085 struct socket_smack *ssp = sk->sk_security;
4086 struct netlbl_lsm_secattr secattr;
4087 struct sockaddr_in addr;
4089 struct smack_known *hskp;
4091 struct smk_audit_info ad;
4093 struct lsm_network_audit net;
4096 #if IS_ENABLED(CONFIG_IPV6)
4097 if (family == PF_INET6) {
4099 * Handle mapped IPv4 packets arriving
4100 * via IPv6 sockets. Don't set up netlabel
4101 * processing on IPv6.
4103 if (skb->protocol == htons(ETH_P_IP))
4108 #endif /* CONFIG_IPV6 */
4110 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4112 * If there is a secmark use it rather than the CIPSO label.
4113 * If there is no secmark fall back to CIPSO.
4114 * The secmark is assumed to reflect policy better.
4116 if (skb && skb->secmark != 0) {
4117 skp = smack_from_secid(skb->secmark);
4120 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4122 netlbl_secattr_init(&secattr);
4123 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4125 skp = smack_from_secattr(&secattr, ssp);
4127 skp = &smack_known_huh;
4128 netlbl_secattr_destroy(&secattr);
4130 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4135 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4136 ad.a.u.net->family = family;
4137 ad.a.u.net->netif = skb->skb_iif;
4138 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4141 * Receiving a packet requires that the other end be able to write
4142 * here. Read access is not required.
4144 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4145 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4150 * Save the peer's label in the request_sock so we can later setup
4151 * smk_packet in the child socket so that SO_PEERCRED can report it.
4153 req->peer_secid = skp->smk_secid;
4156 * We need to decide if we want to label the incoming connection here
4157 * if we do we only need to label the request_sock and the stack will
4158 * propagate the wire-label to the sock when it is created.
4161 addr.sin_addr.s_addr = hdr->saddr;
4163 hskp = smack_ipv4host_label(&addr);
4167 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4169 netlbl_req_delattr(req);
4175 * smack_inet_csk_clone - Copy the connection information to the new socket
4176 * @sk: the new socket
4177 * @req: the connection's request_sock
4179 * Transfer the connection's peer label to the newly created socket.
4181 static void smack_inet_csk_clone(struct sock *sk,
4182 const struct request_sock *req)
4184 struct socket_smack *ssp = sk->sk_security;
4185 struct smack_known *skp;
4187 if (req->peer_secid != 0) {
4188 skp = smack_from_secid(req->peer_secid);
4189 ssp->smk_packet = skp;
4191 ssp->smk_packet = NULL;
4195 * Key management security hooks
4197 * Casey has not tested key support very heavily.
4198 * The permission check is most likely too restrictive.
4199 * If you care about keys please have a look.
4204 * smack_key_alloc - Set the key security blob
4206 * @cred: the credentials to use
4209 * No allocation required
4213 static int smack_key_alloc(struct key *key, const struct cred *cred,
4214 unsigned long flags)
4216 struct smack_known *skp = smk_of_task(smack_cred(cred));
4218 key->security = skp;
4223 * smack_key_free - Clear the key security blob
4226 * Clear the blob pointer
4228 static void smack_key_free(struct key *key)
4230 key->security = NULL;
4234 * smack_key_permission - Smack access on a key
4235 * @key_ref: gets to the object
4236 * @cred: the credentials to use
4237 * @perm: requested key permissions
4239 * Return 0 if the task has read and write to the object,
4240 * an error code otherwise
4242 static int smack_key_permission(key_ref_t key_ref,
4243 const struct cred *cred, unsigned perm)
4246 struct smk_audit_info ad;
4247 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4252 * Validate requested permissions
4254 if (perm & ~KEY_NEED_ALL)
4257 keyp = key_ref_to_ptr(key_ref);
4261 * If the key hasn't been initialized give it access so that
4264 if (keyp->security == NULL)
4267 * This should not occur
4272 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4276 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4277 ad.a.u.key_struct.key = keyp->serial;
4278 ad.a.u.key_struct.key_desc = keyp->description;
4280 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4281 request |= MAY_READ;
4282 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4283 request |= MAY_WRITE;
4284 rc = smk_access(tkp, keyp->security, request, &ad);
4285 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4290 * smack_key_getsecurity - Smack label tagging the key
4291 * @key points to the key to be queried
4292 * @_buffer points to a pointer that should be set to point to the
4293 * resulting string (if no label or an error occurs).
4294 * Return the length of the string (including terminating NUL) or -ve if
4296 * May also return 0 (and a NULL buffer pointer) if there is no label.
4298 static int smack_key_getsecurity(struct key *key, char **_buffer)
4300 struct smack_known *skp = key->security;
4304 if (key->security == NULL) {
4309 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4312 length = strlen(copy) + 1;
4318 #endif /* CONFIG_KEYS */
4323 * Audit requires a unique representation of each Smack specific
4324 * rule. This unique representation is used to distinguish the
4325 * object to be audited from remaining kernel objects and also
4326 * works as a glue between the audit hooks.
4328 * Since repository entries are added but never deleted, we'll use
4329 * the smack_known label address related to the given audit rule as
4330 * the needed unique representation. This also better fits the smack
4331 * model where nearly everything is a label.
4336 * smack_audit_rule_init - Initialize a smack audit rule
4337 * @field: audit rule fields given from user-space (audit.h)
4338 * @op: required testing operator (=, !=, >, <, ...)
4339 * @rulestr: smack label to be audited
4340 * @vrule: pointer to save our own audit rule representation
4342 * Prepare to audit cases where (@field @op @rulestr) is true.
4343 * The label to be audited is created if necessay.
4345 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4347 struct smack_known *skp;
4348 char **rule = (char **)vrule;
4351 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4354 if (op != Audit_equal && op != Audit_not_equal)
4357 skp = smk_import_entry(rulestr, 0);
4359 return PTR_ERR(skp);
4361 *rule = skp->smk_known;
4367 * smack_audit_rule_known - Distinguish Smack audit rules
4368 * @krule: rule of interest, in Audit kernel representation format
4370 * This is used to filter Smack rules from remaining Audit ones.
4371 * If it's proved that this rule belongs to us, the
4372 * audit_rule_match hook will be called to do the final judgement.
4374 static int smack_audit_rule_known(struct audit_krule *krule)
4376 struct audit_field *f;
4379 for (i = 0; i < krule->field_count; i++) {
4380 f = &krule->fields[i];
4382 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4390 * smack_audit_rule_match - Audit given object ?
4391 * @secid: security id for identifying the object to test
4392 * @field: audit rule flags given from user-space
4393 * @op: required testing operator
4394 * @vrule: smack internal rule presentation
4395 * @actx: audit context associated with the check
4397 * The core Audit hook. It's used to take the decision of
4398 * whether to audit or not to audit a given object.
4400 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4401 struct audit_context *actx)
4403 struct smack_known *skp;
4406 if (unlikely(!rule)) {
4407 WARN_ONCE(1, "Smack: missing rule\n");
4411 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4414 skp = smack_from_secid(secid);
4417 * No need to do string comparisons. If a match occurs,
4418 * both pointers will point to the same smack_known
4421 if (op == Audit_equal)
4422 return (rule == skp->smk_known);
4423 if (op == Audit_not_equal)
4424 return (rule != skp->smk_known);
4430 * There is no need for a smack_audit_rule_free hook.
4431 * No memory was allocated.
4434 #endif /* CONFIG_AUDIT */
4437 * smack_ismaclabel - check if xattr @name references a smack MAC label
4438 * @name: Full xattr name to check.
4440 static int smack_ismaclabel(const char *name)
4442 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4447 * smack_secid_to_secctx - return the smack label for a secid
4448 * @secid: incoming integer
4449 * @secdata: destination
4450 * @seclen: how long it is
4452 * Exists for networking code.
4454 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4456 struct smack_known *skp = smack_from_secid(secid);
4459 *secdata = skp->smk_known;
4460 *seclen = strlen(skp->smk_known);
4465 * smack_secctx_to_secid - return the secid for a smack label
4466 * @secdata: smack label
4467 * @seclen: how long result is
4468 * @secid: outgoing integer
4470 * Exists for audit and networking code.
4472 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4474 struct smack_known *skp = smk_find_entry(secdata);
4477 *secid = skp->smk_secid;
4484 * There used to be a smack_release_secctx hook
4485 * that did nothing back when hooks were in a vector.
4486 * Now that there's a list such a hook adds cost.
4489 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4491 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4494 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4496 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4499 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4501 struct smack_known *skp = smk_of_inode(inode);
4503 *ctx = skp->smk_known;
4504 *ctxlen = strlen(skp->smk_known);
4508 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4511 struct task_smack *tsp;
4512 struct smack_known *skp;
4513 struct inode_smack *isp;
4514 struct cred *new_creds = *new;
4516 if (new_creds == NULL) {
4517 new_creds = prepare_creds();
4518 if (new_creds == NULL)
4522 tsp = smack_cred(new_creds);
4525 * Get label from overlay inode and set it in create_sid
4527 isp = d_inode(dentry->d_parent)->i_security;
4528 skp = isp->smk_inode;
4529 tsp->smk_task = skp;
4534 static int smack_inode_copy_up_xattr(const char *name)
4537 * Return 1 if this is the smack access Smack attribute.
4539 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4545 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4547 const struct cred *old,
4550 struct task_smack *otsp = smack_cred(old);
4551 struct task_smack *ntsp = smack_cred(new);
4552 struct inode_smack *isp;
4556 * Use the process credential unless all of
4557 * the transmuting criteria are met
4559 ntsp->smk_task = otsp->smk_task;
4562 * the attribute of the containing directory
4564 isp = d_inode(dentry->d_parent)->i_security;
4566 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4568 may = smk_access_entry(otsp->smk_task->smk_known,
4569 isp->smk_inode->smk_known,
4570 &otsp->smk_task->smk_rules);
4574 * If the directory is transmuting and the rule
4575 * providing access is transmuting use the containing
4576 * directory label instead of the process label.
4578 if (may > 0 && (may & MAY_TRANSMUTE))
4579 ntsp->smk_task = isp->smk_inode;
4584 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4585 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4586 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4587 LSM_HOOK_INIT(syslog, smack_syslog),
4589 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4590 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4591 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4592 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4593 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4594 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4596 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4598 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4599 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4600 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4601 LSM_HOOK_INIT(inode_link, smack_inode_link),
4602 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4603 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4604 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4605 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4606 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4607 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4608 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4609 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4610 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4611 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4612 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4613 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4614 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4615 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4617 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4618 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4619 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4620 LSM_HOOK_INIT(file_lock, smack_file_lock),
4621 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4622 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4623 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4624 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4625 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4626 LSM_HOOK_INIT(file_receive, smack_file_receive),
4628 LSM_HOOK_INIT(file_open, smack_file_open),
4630 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4631 LSM_HOOK_INIT(cred_free, smack_cred_free),
4632 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4633 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4634 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4635 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4636 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4637 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4638 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4639 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4640 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4641 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4642 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4643 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4644 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4645 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4646 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4647 LSM_HOOK_INIT(task_kill, smack_task_kill),
4648 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4650 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4651 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4653 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4654 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4656 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4657 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
4658 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4659 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4660 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4661 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4663 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4664 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
4665 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4666 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4667 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4669 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4670 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
4671 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4672 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4673 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4675 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4677 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4678 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4680 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4681 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4683 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4684 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4685 #ifdef SMACK_IPV6_PORT_LABELING
4686 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4688 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4689 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4690 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4691 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4692 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4693 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4694 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4695 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4696 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4697 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4699 /* key management security hooks */
4701 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4702 LSM_HOOK_INIT(key_free, smack_key_free),
4703 LSM_HOOK_INIT(key_permission, smack_key_permission),
4704 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4705 #endif /* CONFIG_KEYS */
4709 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4710 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4711 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4712 #endif /* CONFIG_AUDIT */
4714 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4715 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4716 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4717 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4718 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4719 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4720 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4721 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4722 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4726 static __init void init_smack_known_list(void)
4729 * Initialize rule list locks
4731 mutex_init(&smack_known_huh.smk_rules_lock);
4732 mutex_init(&smack_known_hat.smk_rules_lock);
4733 mutex_init(&smack_known_floor.smk_rules_lock);
4734 mutex_init(&smack_known_star.smk_rules_lock);
4735 mutex_init(&smack_known_web.smk_rules_lock);
4737 * Initialize rule lists
4739 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4740 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4741 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4742 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4743 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4745 * Create the known labels list
4747 smk_insert_entry(&smack_known_huh);
4748 smk_insert_entry(&smack_known_hat);
4749 smk_insert_entry(&smack_known_star);
4750 smk_insert_entry(&smack_known_floor);
4751 smk_insert_entry(&smack_known_web);
4755 * smack_init - initialize the smack system
4759 static __init int smack_init(void)
4762 struct task_smack *tsp;
4764 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4765 if (!smack_inode_cache)
4768 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4771 kmem_cache_destroy(smack_inode_cache);
4777 pr_info("Smack: Initializing.\n");
4778 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4779 pr_info("Smack: Netfilter enabled.\n");
4781 #ifdef SMACK_IPV6_PORT_LABELING
4782 pr_info("Smack: IPv6 port labeling enabled.\n");
4784 #ifdef SMACK_IPV6_SECMARK_LABELING
4785 pr_info("Smack: IPv6 Netfilter enabled.\n");
4789 * Set the security state for the initial task.
4791 cred = (struct cred *) current->cred;
4792 cred->security = tsp;
4794 /* initialize the smack_known_list */
4795 init_smack_known_list();
4800 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4806 * Smack requires early initialization in order to label
4807 * all processes and objects when they are created.
4809 DEFINE_LSM(smack) = {
4811 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,