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 * init_task_smack - initialize a task security blob
330 * @tsp: blob to initialize
331 * @task: a pointer to the Smack label for the running task
332 * @forked: a pointer to the Smack label for the forked task
335 static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
336 struct smack_known *forked)
338 tsp->smk_task = task;
339 tsp->smk_forked = forked;
340 INIT_LIST_HEAD(&tsp->smk_rules);
341 INIT_LIST_HEAD(&tsp->smk_relabel);
342 mutex_init(&tsp->smk_rules_lock);
346 * smk_copy_rules - copy a rule set
347 * @nhead: new rules header pointer
348 * @ohead: old rules header pointer
349 * @gfp: type of the memory for the allocation
351 * Returns 0 on success, -ENOMEM on error
353 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
356 struct smack_rule *nrp;
357 struct smack_rule *orp;
360 list_for_each_entry_rcu(orp, ohead, list) {
361 nrp = kzalloc(sizeof(struct smack_rule), gfp);
367 list_add_rcu(&nrp->list, nhead);
373 * smk_copy_relabel - copy smk_relabel labels list
374 * @nhead: new rules header pointer
375 * @ohead: old rules header pointer
376 * @gfp: type of the memory for the allocation
378 * Returns 0 on success, -ENOMEM on error
380 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
383 struct smack_known_list_elem *nklep;
384 struct smack_known_list_elem *oklep;
386 list_for_each_entry(oklep, ohead, list) {
387 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
389 smk_destroy_label_list(nhead);
392 nklep->smk_label = oklep->smk_label;
393 list_add(&nklep->list, nhead);
400 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
401 * @mode - input mode in form of PTRACE_MODE_*
403 * Returns a converted MAY_* mode usable by smack rules
405 static inline unsigned int smk_ptrace_mode(unsigned int mode)
407 if (mode & PTRACE_MODE_ATTACH)
408 return MAY_READWRITE;
409 if (mode & PTRACE_MODE_READ)
416 * smk_ptrace_rule_check - helper for ptrace access
417 * @tracer: tracer process
418 * @tracee_known: label entry of the process that's about to be traced
419 * @mode: ptrace attachment mode (PTRACE_MODE_*)
420 * @func: name of the function that called us, used for audit
422 * Returns 0 on access granted, -error on error
424 static int smk_ptrace_rule_check(struct task_struct *tracer,
425 struct smack_known *tracee_known,
426 unsigned int mode, const char *func)
429 struct smk_audit_info ad, *saip = NULL;
430 struct task_smack *tsp;
431 struct smack_known *tracer_known;
432 const struct cred *tracercred;
434 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
435 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
436 smk_ad_setfield_u_tsk(&ad, tracer);
441 tracercred = __task_cred(tracer);
442 tsp = smack_cred(tracercred);
443 tracer_known = smk_of_task(tsp);
445 if ((mode & PTRACE_MODE_ATTACH) &&
446 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
447 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
448 if (tracer_known->smk_known == tracee_known->smk_known)
450 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
452 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
458 smack_log(tracer_known->smk_known,
459 tracee_known->smk_known,
466 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
467 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
475 * We he, that is fun!
479 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
480 * @ctp: child task pointer
481 * @mode: ptrace attachment mode (PTRACE_MODE_*)
483 * Returns 0 if access is OK, an error code otherwise
485 * Do the capability checks.
487 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
489 struct smack_known *skp;
491 skp = smk_of_task_struct(ctp);
493 return smk_ptrace_rule_check(current, skp, mode, __func__);
497 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
498 * @ptp: parent task pointer
500 * Returns 0 if access is OK, an error code otherwise
502 * Do the capability checks, and require PTRACE_MODE_ATTACH.
504 static int smack_ptrace_traceme(struct task_struct *ptp)
507 struct smack_known *skp;
509 skp = smk_of_task(smack_cred(current_cred()));
511 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
516 * smack_syslog - Smack approval on syslog
517 * @type: message type
519 * Returns 0 on success, error code otherwise.
521 static int smack_syslog(int typefrom_file)
524 struct smack_known *skp = smk_of_current();
526 if (smack_privileged(CAP_MAC_OVERRIDE))
529 if (smack_syslog_label != NULL && smack_syslog_label != skp)
541 * smack_sb_alloc_security - allocate a superblock blob
542 * @sb: the superblock getting the blob
544 * Returns 0 on success or -ENOMEM on error.
546 static int smack_sb_alloc_security(struct super_block *sb)
548 struct superblock_smack *sbsp;
550 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
555 sbsp->smk_root = &smack_known_floor;
556 sbsp->smk_default = &smack_known_floor;
557 sbsp->smk_floor = &smack_known_floor;
558 sbsp->smk_hat = &smack_known_hat;
560 * SMK_SB_INITIALIZED will be zero from kzalloc.
562 sb->s_security = sbsp;
568 * smack_sb_free_security - free a superblock blob
569 * @sb: the superblock getting the blob
572 static void smack_sb_free_security(struct super_block *sb)
574 kfree(sb->s_security);
575 sb->s_security = NULL;
578 struct smack_mnt_opts {
579 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
582 static void smack_free_mnt_opts(void *mnt_opts)
584 struct smack_mnt_opts *opts = mnt_opts;
585 kfree(opts->fsdefault);
586 kfree(opts->fsfloor);
589 kfree(opts->fstransmute);
593 static int smack_add_opt(int token, const char *s, void **mnt_opts)
595 struct smack_mnt_opts *opts = *mnt_opts;
598 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
627 case Opt_fstransmute:
628 if (opts->fstransmute)
630 opts->fstransmute = s;
636 pr_warn("Smack: duplicate mount options\n");
640 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
642 char *from = options, *to = options;
646 char *next = strchr(from, ',');
655 token = match_opt_prefix(from, len, &arg);
656 if (token != Opt_error) {
657 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
658 rc = smack_add_opt(token, arg, mnt_opts);
662 smack_free_mnt_opts(*mnt_opts);
667 if (!first) { // copy with preceding comma
672 memmove(to, from, len);
685 * smack_set_mnt_opts - set Smack specific mount options
686 * @sb: the file system superblock
687 * @opts: Smack mount options
688 * @kern_flags: mount option from kernel space or user space
689 * @set_kern_flags: where to store converted mount opts
691 * Returns 0 on success, an error code on failure
693 * Allow filesystems with binary mount data to explicitly set Smack mount
696 static int smack_set_mnt_opts(struct super_block *sb,
698 unsigned long kern_flags,
699 unsigned long *set_kern_flags)
701 struct dentry *root = sb->s_root;
702 struct inode *inode = d_backing_inode(root);
703 struct superblock_smack *sp = sb->s_security;
704 struct inode_smack *isp;
705 struct smack_known *skp;
706 struct smack_mnt_opts *opts = mnt_opts;
707 bool transmute = false;
709 if (sp->smk_flags & SMK_SB_INITIALIZED)
712 if (!smack_privileged(CAP_MAC_ADMIN)) {
714 * Unprivileged mounts don't get to specify Smack values.
719 * Unprivileged mounts get root and default from the caller.
721 skp = smk_of_current();
723 sp->smk_default = skp;
725 * For a handful of fs types with no user-controlled
726 * backing store it's okay to trust security labels
727 * in the filesystem. The rest are untrusted.
729 if (sb->s_user_ns != &init_user_ns &&
730 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
731 sb->s_magic != RAMFS_MAGIC) {
733 sp->smk_flags |= SMK_SB_UNTRUSTED;
737 sp->smk_flags |= SMK_SB_INITIALIZED;
740 if (opts->fsdefault) {
741 skp = smk_import_entry(opts->fsdefault, 0);
744 sp->smk_default = skp;
747 skp = smk_import_entry(opts->fsfloor, 0);
753 skp = smk_import_entry(opts->fshat, 0);
759 skp = smk_import_entry(opts->fsroot, 0);
764 if (opts->fstransmute) {
765 skp = smk_import_entry(opts->fstransmute, 0);
774 * Initialize the root inode.
776 isp = inode->i_security;
778 isp = new_inode_smack(sp->smk_root);
781 inode->i_security = isp;
783 isp->smk_inode = sp->smk_root;
786 isp->smk_flags |= SMK_INODE_TRANSMUTE;
792 * smack_sb_statfs - Smack check on statfs
793 * @dentry: identifies the file system in question
795 * Returns 0 if current can read the floor of the filesystem,
796 * and error code otherwise
798 static int smack_sb_statfs(struct dentry *dentry)
800 struct superblock_smack *sbp = dentry->d_sb->s_security;
802 struct smk_audit_info ad;
804 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
805 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
807 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
808 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
817 * smack_bprm_set_creds - set creds for exec
818 * @bprm: the exec information
820 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
822 static int smack_bprm_set_creds(struct linux_binprm *bprm)
824 struct inode *inode = file_inode(bprm->file);
825 struct task_smack *bsp = smack_cred(bprm->cred);
826 struct inode_smack *isp;
827 struct superblock_smack *sbsp;
830 if (bprm->called_set_creds)
833 isp = inode->i_security;
834 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
837 sbsp = inode->i_sb->s_security;
838 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
839 isp->smk_task != sbsp->smk_root)
842 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
843 struct task_struct *tracer;
847 tracer = ptrace_parent(current);
848 if (likely(tracer != NULL))
849 rc = smk_ptrace_rule_check(tracer,
857 } else if (bprm->unsafe)
860 bsp->smk_task = isp->smk_task;
861 bprm->per_clear |= PER_CLEAR_ON_SETID;
863 /* Decide if this is a secure exec. */
864 if (bsp->smk_task != bsp->smk_forked)
865 bprm->secureexec = 1;
875 * smack_inode_alloc_security - allocate an inode blob
876 * @inode: the inode in need of a blob
878 * Returns 0 if it gets a blob, -ENOMEM otherwise
880 static int smack_inode_alloc_security(struct inode *inode)
882 struct smack_known *skp = smk_of_current();
884 inode->i_security = new_inode_smack(skp);
885 if (inode->i_security == NULL)
891 * smack_inode_free_rcu - Free inode_smack blob from cache
892 * @head: the rcu_head for getting inode_smack pointer
894 * Call back function called from call_rcu() to free
895 * the i_security blob pointer in inode
897 static void smack_inode_free_rcu(struct rcu_head *head)
899 struct inode_smack *issp;
901 issp = container_of(head, struct inode_smack, smk_rcu);
902 kmem_cache_free(smack_inode_cache, issp);
906 * smack_inode_free_security - free an inode blob using call_rcu()
907 * @inode: the inode with a blob
909 * Clears the blob pointer in inode using RCU
911 static void smack_inode_free_security(struct inode *inode)
913 struct inode_smack *issp = inode->i_security;
916 * The inode may still be referenced in a path walk and
917 * a call to smack_inode_permission() can be made
918 * after smack_inode_free_security() is called.
919 * To avoid race condition free the i_security via RCU
920 * and leave the current inode->i_security pointer intact.
921 * The inode will be freed after the RCU grace period too.
923 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
927 * smack_inode_init_security - copy out the smack from an inode
928 * @inode: the newly created inode
929 * @dir: containing directory object
931 * @name: where to put the attribute name
932 * @value: where to put the attribute value
933 * @len: where to put the length of the attribute
935 * Returns 0 if it all works out, -ENOMEM if there's no memory
937 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
938 const struct qstr *qstr, const char **name,
939 void **value, size_t *len)
941 struct inode_smack *issp = inode->i_security;
942 struct smack_known *skp = smk_of_current();
943 struct smack_known *isp = smk_of_inode(inode);
944 struct smack_known *dsp = smk_of_inode(dir);
948 *name = XATTR_SMACK_SUFFIX;
952 may = smk_access_entry(skp->smk_known, dsp->smk_known,
957 * If the access rule allows transmutation and
958 * the directory requests transmutation then
959 * by all means transmute.
960 * Mark the inode as changed.
962 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
963 smk_inode_transmutable(dir)) {
965 issp->smk_flags |= SMK_INODE_CHANGED;
968 *value = kstrdup(isp->smk_known, GFP_NOFS);
972 *len = strlen(isp->smk_known);
979 * smack_inode_link - Smack check on link
980 * @old_dentry: the existing object
982 * @new_dentry: the new object
984 * Returns 0 if access is permitted, an error code otherwise
986 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
987 struct dentry *new_dentry)
989 struct smack_known *isp;
990 struct smk_audit_info ad;
993 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
994 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
996 isp = smk_of_inode(d_backing_inode(old_dentry));
997 rc = smk_curacc(isp, MAY_WRITE, &ad);
998 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1000 if (rc == 0 && d_is_positive(new_dentry)) {
1001 isp = smk_of_inode(d_backing_inode(new_dentry));
1002 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1003 rc = smk_curacc(isp, MAY_WRITE, &ad);
1004 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1011 * smack_inode_unlink - Smack check on inode deletion
1012 * @dir: containing directory object
1013 * @dentry: file to unlink
1015 * Returns 0 if current can write the containing directory
1016 * and the object, error code otherwise
1018 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1020 struct inode *ip = d_backing_inode(dentry);
1021 struct smk_audit_info ad;
1024 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1025 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1028 * You need write access to the thing you're unlinking
1030 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1031 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1034 * You also need write access to the containing directory
1036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1037 smk_ad_setfield_u_fs_inode(&ad, dir);
1038 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1039 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1045 * smack_inode_rmdir - Smack check on directory deletion
1046 * @dir: containing directory object
1047 * @dentry: directory to unlink
1049 * Returns 0 if current can write the containing directory
1050 * and the directory, error code otherwise
1052 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1054 struct smk_audit_info ad;
1057 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1058 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1061 * You need write access to the thing you're removing
1063 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1064 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1067 * You also need write access to the containing directory
1069 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1070 smk_ad_setfield_u_fs_inode(&ad, dir);
1071 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1072 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1079 * smack_inode_rename - Smack check on rename
1080 * @old_inode: unused
1081 * @old_dentry: the old object
1082 * @new_inode: unused
1083 * @new_dentry: the new object
1085 * Read and write access is required on both the old and
1088 * Returns 0 if access is permitted, an error code otherwise
1090 static int smack_inode_rename(struct inode *old_inode,
1091 struct dentry *old_dentry,
1092 struct inode *new_inode,
1093 struct dentry *new_dentry)
1096 struct smack_known *isp;
1097 struct smk_audit_info ad;
1099 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1100 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1102 isp = smk_of_inode(d_backing_inode(old_dentry));
1103 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1104 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1106 if (rc == 0 && d_is_positive(new_dentry)) {
1107 isp = smk_of_inode(d_backing_inode(new_dentry));
1108 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1109 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1110 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1116 * smack_inode_permission - Smack version of permission()
1117 * @inode: the inode in question
1118 * @mask: the access requested
1120 * This is the important Smack hook.
1122 * Returns 0 if access is permitted, -EACCES otherwise
1124 static int smack_inode_permission(struct inode *inode, int mask)
1126 struct superblock_smack *sbsp = inode->i_sb->s_security;
1127 struct smk_audit_info ad;
1128 int no_block = mask & MAY_NOT_BLOCK;
1131 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1133 * No permission to check. Existence test. Yup, it's there.
1138 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1139 if (smk_of_inode(inode) != sbsp->smk_root)
1143 /* May be droppable after audit */
1146 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1147 smk_ad_setfield_u_fs_inode(&ad, inode);
1148 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1149 rc = smk_bu_inode(inode, mask, rc);
1154 * smack_inode_setattr - Smack check for setting attributes
1155 * @dentry: the object
1156 * @iattr: for the force flag
1158 * Returns 0 if access is permitted, an error code otherwise
1160 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1162 struct smk_audit_info ad;
1166 * Need to allow for clearing the setuid bit.
1168 if (iattr->ia_valid & ATTR_FORCE)
1170 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1171 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1173 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1174 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1179 * smack_inode_getattr - Smack check for getting attributes
1180 * @mnt: vfsmount of the object
1181 * @dentry: the object
1183 * Returns 0 if access is permitted, an error code otherwise
1185 static int smack_inode_getattr(const struct path *path)
1187 struct smk_audit_info ad;
1188 struct inode *inode = d_backing_inode(path->dentry);
1191 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1192 smk_ad_setfield_u_fs_path(&ad, *path);
1193 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1194 rc = smk_bu_inode(inode, MAY_READ, rc);
1199 * smack_inode_setxattr - Smack check for setting xattrs
1200 * @dentry: the object
1201 * @name: name of the attribute
1202 * @value: value of the attribute
1203 * @size: size of the value
1206 * This protects the Smack attribute explicitly.
1208 * Returns 0 if access is permitted, an error code otherwise
1210 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1211 const void *value, size_t size, int flags)
1213 struct smk_audit_info ad;
1214 struct smack_known *skp;
1216 int check_import = 0;
1221 * Check label validity here so import won't fail in post_setxattr
1223 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1224 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1225 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1228 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1229 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1233 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1235 if (size != TRANS_TRUE_SIZE ||
1236 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1239 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1241 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1244 if (rc == 0 && check_import) {
1245 skp = size ? smk_import_entry(value, size) : NULL;
1248 else if (skp == NULL || (check_star &&
1249 (skp == &smack_known_star || skp == &smack_known_web)))
1253 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1254 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1257 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1258 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1265 * smack_inode_post_setxattr - Apply the Smack update approved above
1267 * @name: attribute name
1268 * @value: attribute value
1269 * @size: attribute size
1272 * Set the pointer in the inode blob to the entry found
1273 * in the master label list.
1275 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1276 const void *value, size_t size, int flags)
1278 struct smack_known *skp;
1279 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
1281 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1282 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1286 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1287 skp = smk_import_entry(value, size);
1289 isp->smk_inode = skp;
1290 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1291 skp = smk_import_entry(value, size);
1293 isp->smk_task = skp;
1294 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1295 skp = smk_import_entry(value, size);
1297 isp->smk_mmap = skp;
1304 * smack_inode_getxattr - Smack check on getxattr
1305 * @dentry: the object
1308 * Returns 0 if access is permitted, an error code otherwise
1310 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1312 struct smk_audit_info ad;
1315 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1316 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1318 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1319 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1324 * smack_inode_removexattr - Smack check on removexattr
1325 * @dentry: the object
1326 * @name: name of the attribute
1328 * Removing the Smack attribute requires CAP_MAC_ADMIN
1330 * Returns 0 if access is permitted, an error code otherwise
1332 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1334 struct inode_smack *isp;
1335 struct smk_audit_info ad;
1338 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1339 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1340 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1341 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1342 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1343 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1344 if (!smack_privileged(CAP_MAC_ADMIN))
1347 rc = cap_inode_removexattr(dentry, name);
1352 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1353 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1355 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1356 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1360 isp = d_backing_inode(dentry)->i_security;
1362 * Don't do anything special for these.
1363 * XATTR_NAME_SMACKIPIN
1364 * XATTR_NAME_SMACKIPOUT
1366 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1367 struct super_block *sbp = dentry->d_sb;
1368 struct superblock_smack *sbsp = sbp->s_security;
1370 isp->smk_inode = sbsp->smk_default;
1371 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1372 isp->smk_task = NULL;
1373 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1374 isp->smk_mmap = NULL;
1375 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1376 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1382 * smack_inode_getsecurity - get smack xattrs
1383 * @inode: the object
1384 * @name: attribute name
1385 * @buffer: where to put the result
1386 * @alloc: duplicate memory
1388 * Returns the size of the attribute or an error code
1390 static int smack_inode_getsecurity(struct inode *inode,
1391 const char *name, void **buffer,
1394 struct socket_smack *ssp;
1395 struct socket *sock;
1396 struct super_block *sbp;
1397 struct inode *ip = (struct inode *)inode;
1398 struct smack_known *isp;
1400 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1401 isp = smk_of_inode(inode);
1404 * The rest of the Smack xattrs are only on sockets.
1407 if (sbp->s_magic != SOCKFS_MAGIC)
1410 sock = SOCKET_I(ip);
1411 if (sock == NULL || sock->sk == NULL)
1414 ssp = sock->sk->sk_security;
1416 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1418 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1425 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1426 if (*buffer == NULL)
1430 return strlen(isp->smk_known);
1435 * smack_inode_listsecurity - list the Smack attributes
1436 * @inode: the object
1437 * @buffer: where they go
1438 * @buffer_size: size of buffer
1440 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1443 int len = sizeof(XATTR_NAME_SMACK);
1445 if (buffer != NULL && len <= buffer_size)
1446 memcpy(buffer, XATTR_NAME_SMACK, len);
1452 * smack_inode_getsecid - Extract inode's security id
1453 * @inode: inode to extract the info from
1454 * @secid: where result will be saved
1456 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1458 struct smack_known *skp = smk_of_inode(inode);
1460 *secid = skp->smk_secid;
1468 * There is no smack_file_permission hook
1470 * Should access checks be done on each read or write?
1471 * UNICOS and SELinux say yes.
1472 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1474 * I'll say no for now. Smack does not do the frequent
1475 * label changing that SELinux does.
1479 * smack_file_alloc_security - assign a file security blob
1482 * The security blob for a file is a pointer to the master
1483 * label list, so no allocation is done.
1485 * f_security is the owner security information. It
1486 * isn't used on file access checks, it's for send_sigio.
1490 static int smack_file_alloc_security(struct file *file)
1492 struct smack_known **blob = smack_file(file);
1494 *blob = smk_of_current();
1499 * smack_file_ioctl - Smack check on ioctls
1504 * Relies heavily on the correct use of the ioctl command conventions.
1506 * Returns 0 if allowed, error code otherwise
1508 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1512 struct smk_audit_info ad;
1513 struct inode *inode = file_inode(file);
1515 if (unlikely(IS_PRIVATE(inode)))
1518 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1519 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1521 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1522 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1523 rc = smk_bu_file(file, MAY_WRITE, rc);
1526 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1527 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1528 rc = smk_bu_file(file, MAY_READ, rc);
1535 * smack_file_lock - Smack check on file locking
1539 * Returns 0 if current has lock access, error code otherwise
1541 static int smack_file_lock(struct file *file, unsigned int cmd)
1543 struct smk_audit_info ad;
1545 struct inode *inode = file_inode(file);
1547 if (unlikely(IS_PRIVATE(inode)))
1550 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1551 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1552 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1553 rc = smk_bu_file(file, MAY_LOCK, rc);
1558 * smack_file_fcntl - Smack check on fcntl
1560 * @cmd: what action to check
1563 * Generally these operations are harmless.
1564 * File locking operations present an obvious mechanism
1565 * for passing information, so they require write access.
1567 * Returns 0 if current has access, error code otherwise
1569 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1572 struct smk_audit_info ad;
1574 struct inode *inode = file_inode(file);
1576 if (unlikely(IS_PRIVATE(inode)))
1584 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1585 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1586 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1587 rc = smk_bu_file(file, MAY_LOCK, rc);
1591 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1592 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1593 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1594 rc = smk_bu_file(file, MAY_WRITE, rc);
1605 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1606 * if mapping anonymous memory.
1607 * @file contains the file structure for file to map (may be NULL).
1608 * @reqprot contains the protection requested by the application.
1609 * @prot contains the protection that will be applied by the kernel.
1610 * @flags contains the operational flags.
1611 * Return 0 if permission is granted.
1613 static int smack_mmap_file(struct file *file,
1614 unsigned long reqprot, unsigned long prot,
1615 unsigned long flags)
1617 struct smack_known *skp;
1618 struct smack_known *mkp;
1619 struct smack_rule *srp;
1620 struct task_smack *tsp;
1621 struct smack_known *okp;
1622 struct inode_smack *isp;
1623 struct superblock_smack *sbsp;
1632 if (unlikely(IS_PRIVATE(file_inode(file))))
1635 isp = file_inode(file)->i_security;
1636 if (isp->smk_mmap == NULL)
1638 sbsp = file_inode(file)->i_sb->s_security;
1639 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1640 isp->smk_mmap != sbsp->smk_root)
1642 mkp = isp->smk_mmap;
1644 tsp = smack_cred(current_cred());
1645 skp = smk_of_current();
1650 * For each Smack rule associated with the subject
1651 * label verify that the SMACK64MMAP also has access
1652 * to that rule's object label.
1654 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1655 okp = srp->smk_object;
1657 * Matching labels always allows access.
1659 if (mkp->smk_known == okp->smk_known)
1662 * If there is a matching local rule take
1663 * that into account as well.
1665 may = smk_access_entry(srp->smk_subject->smk_known,
1669 may = srp->smk_access;
1671 may &= srp->smk_access;
1673 * If may is zero the SMACK64MMAP subject can't
1674 * possibly have less access.
1680 * Fetch the global list entry.
1681 * If there isn't one a SMACK64MMAP subject
1682 * can't have as much access as current.
1684 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1686 if (mmay == -ENOENT) {
1691 * If there is a local entry it modifies the
1692 * potential access, too.
1694 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1696 if (tmay != -ENOENT)
1700 * If there is any access available to current that is
1701 * not available to a SMACK64MMAP subject
1704 if ((may | mmay) != mmay) {
1716 * smack_file_set_fowner - set the file security blob value
1717 * @file: object in question
1720 static void smack_file_set_fowner(struct file *file)
1722 struct smack_known **blob = smack_file(file);
1724 *blob = smk_of_current();
1728 * smack_file_send_sigiotask - Smack on sigio
1729 * @tsk: The target task
1730 * @fown: the object the signal come from
1733 * Allow a privileged task to get signals even if it shouldn't
1735 * Returns 0 if a subject with the object's smack could
1736 * write to the task, an error code otherwise.
1738 static int smack_file_send_sigiotask(struct task_struct *tsk,
1739 struct fown_struct *fown, int signum)
1741 struct smack_known **blob;
1742 struct smack_known *skp;
1743 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1744 const struct cred *tcred;
1747 struct smk_audit_info ad;
1750 * struct fown_struct is never outside the context of a struct file
1752 file = container_of(fown, struct file, f_owner);
1754 /* we don't log here as rc can be overriden */
1755 blob = smack_file(file);
1757 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1758 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1761 tcred = __task_cred(tsk);
1762 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1766 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1767 smk_ad_setfield_u_tsk(&ad, tsk);
1768 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1773 * smack_file_receive - Smack file receive check
1776 * Returns 0 if current has access, error code otherwise
1778 static int smack_file_receive(struct file *file)
1782 struct smk_audit_info ad;
1783 struct inode *inode = file_inode(file);
1784 struct socket *sock;
1785 struct task_smack *tsp;
1786 struct socket_smack *ssp;
1788 if (unlikely(IS_PRIVATE(inode)))
1791 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1792 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1794 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1795 sock = SOCKET_I(inode);
1796 ssp = sock->sk->sk_security;
1797 tsp = smack_cred(current_cred());
1799 * If the receiving process can't write to the
1800 * passed socket or if the passed socket can't
1801 * write to the receiving process don't accept
1802 * the passed socket.
1804 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1805 rc = smk_bu_file(file, may, rc);
1808 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1809 rc = smk_bu_file(file, may, rc);
1813 * This code relies on bitmasks.
1815 if (file->f_mode & FMODE_READ)
1817 if (file->f_mode & FMODE_WRITE)
1820 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1821 rc = smk_bu_file(file, may, rc);
1826 * smack_file_open - Smack dentry open processing
1828 * @cred: task credential
1830 * Set the security blob in the file structure.
1831 * Allow the open only if the task has read access. There are
1832 * many read operations (e.g. fstat) that you can do with an
1833 * fd even if you have the file open write-only.
1837 static int smack_file_open(struct file *file)
1839 struct task_smack *tsp = smack_cred(file->f_cred);
1840 struct inode *inode = file_inode(file);
1841 struct smk_audit_info ad;
1844 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1845 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1846 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1847 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1857 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1858 * @new: the new credentials
1859 * @gfp: the atomicity of any memory allocations
1861 * Prepare a blank set of credentials for modification. This must allocate all
1862 * the memory the LSM module might require such that cred_transfer() can
1863 * complete without error.
1865 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1867 init_task_smack(smack_cred(cred), NULL, NULL);
1873 * smack_cred_free - "free" task-level security credentials
1874 * @cred: the credentials in question
1877 static void smack_cred_free(struct cred *cred)
1879 struct task_smack *tsp = smack_cred(cred);
1880 struct smack_rule *rp;
1881 struct list_head *l;
1882 struct list_head *n;
1884 smk_destroy_label_list(&tsp->smk_relabel);
1886 list_for_each_safe(l, n, &tsp->smk_rules) {
1887 rp = list_entry(l, struct smack_rule, list);
1888 list_del(&rp->list);
1894 * smack_cred_prepare - prepare new set of credentials for modification
1895 * @new: the new credentials
1896 * @old: the original credentials
1897 * @gfp: the atomicity of any memory allocations
1899 * Prepare a new set of credentials for modification.
1901 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1904 struct task_smack *old_tsp = smack_cred(old);
1905 struct task_smack *new_tsp = smack_cred(new);
1908 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1910 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1914 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1920 * smack_cred_transfer - Transfer the old credentials to the new credentials
1921 * @new: the new credentials
1922 * @old: the original credentials
1924 * Fill in a set of blank credentials from another set of credentials.
1926 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1928 struct task_smack *old_tsp = smack_cred(old);
1929 struct task_smack *new_tsp = smack_cred(new);
1931 new_tsp->smk_task = old_tsp->smk_task;
1932 new_tsp->smk_forked = old_tsp->smk_task;
1933 mutex_init(&new_tsp->smk_rules_lock);
1934 INIT_LIST_HEAD(&new_tsp->smk_rules);
1936 /* cbs copy rule list */
1940 * smack_cred_getsecid - get the secid corresponding to a creds structure
1941 * @c: the object creds
1942 * @secid: where to put the result
1944 * Sets the secid to contain a u32 version of the smack label.
1946 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1948 struct smack_known *skp;
1951 skp = smk_of_task(smack_cred(cred));
1952 *secid = skp->smk_secid;
1957 * smack_kernel_act_as - Set the subjective context in a set of credentials
1958 * @new: points to the set of credentials to be modified.
1959 * @secid: specifies the security ID to be set
1961 * Set the security data for a kernel service.
1963 static int smack_kernel_act_as(struct cred *new, u32 secid)
1965 struct task_smack *new_tsp = smack_cred(new);
1967 new_tsp->smk_task = smack_from_secid(secid);
1972 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1973 * @new: points to the set of credentials to be modified
1974 * @inode: points to the inode to use as a reference
1976 * Set the file creation context in a set of credentials to the same
1977 * as the objective context of the specified inode
1979 static int smack_kernel_create_files_as(struct cred *new,
1980 struct inode *inode)
1982 struct inode_smack *isp = inode->i_security;
1983 struct task_smack *tsp = smack_cred(new);
1985 tsp->smk_forked = isp->smk_inode;
1986 tsp->smk_task = tsp->smk_forked;
1991 * smk_curacc_on_task - helper to log task related access
1992 * @p: the task object
1993 * @access: the access requested
1994 * @caller: name of the calling function for audit
1996 * Return 0 if access is permitted
1998 static int smk_curacc_on_task(struct task_struct *p, int access,
2001 struct smk_audit_info ad;
2002 struct smack_known *skp = smk_of_task_struct(p);
2005 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2006 smk_ad_setfield_u_tsk(&ad, p);
2007 rc = smk_curacc(skp, access, &ad);
2008 rc = smk_bu_task(p, access, rc);
2013 * smack_task_setpgid - Smack check on setting pgid
2014 * @p: the task object
2017 * Return 0 if write access is permitted
2019 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2021 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2025 * smack_task_getpgid - Smack access check for getpgid
2026 * @p: the object task
2028 * Returns 0 if current can read the object task, error code otherwise
2030 static int smack_task_getpgid(struct task_struct *p)
2032 return smk_curacc_on_task(p, MAY_READ, __func__);
2036 * smack_task_getsid - Smack access check for getsid
2037 * @p: the object task
2039 * Returns 0 if current can read the object task, error code otherwise
2041 static int smack_task_getsid(struct task_struct *p)
2043 return smk_curacc_on_task(p, MAY_READ, __func__);
2047 * smack_task_getsecid - get the secid of the task
2048 * @p: the object task
2049 * @secid: where to put the result
2051 * Sets the secid to contain a u32 version of the smack label.
2053 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2055 struct smack_known *skp = smk_of_task_struct(p);
2057 *secid = skp->smk_secid;
2061 * smack_task_setnice - Smack check on setting nice
2062 * @p: the task object
2065 * Return 0 if write access is permitted
2067 static int smack_task_setnice(struct task_struct *p, int nice)
2069 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2073 * smack_task_setioprio - Smack check on setting ioprio
2074 * @p: the task object
2077 * Return 0 if write access is permitted
2079 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2081 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2085 * smack_task_getioprio - Smack check on reading ioprio
2086 * @p: the task object
2088 * Return 0 if read access is permitted
2090 static int smack_task_getioprio(struct task_struct *p)
2092 return smk_curacc_on_task(p, MAY_READ, __func__);
2096 * smack_task_setscheduler - Smack check on setting scheduler
2097 * @p: the task object
2101 * Return 0 if read access is permitted
2103 static int smack_task_setscheduler(struct task_struct *p)
2105 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2109 * smack_task_getscheduler - Smack check on reading scheduler
2110 * @p: the task object
2112 * Return 0 if read access is permitted
2114 static int smack_task_getscheduler(struct task_struct *p)
2116 return smk_curacc_on_task(p, MAY_READ, __func__);
2120 * smack_task_movememory - Smack check on moving memory
2121 * @p: the task object
2123 * Return 0 if write access is permitted
2125 static int smack_task_movememory(struct task_struct *p)
2127 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2131 * smack_task_kill - Smack check on signal delivery
2132 * @p: the task object
2135 * @cred: identifies the cred to use in lieu of current's
2137 * Return 0 if write access is permitted
2140 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2141 int sig, const struct cred *cred)
2143 struct smk_audit_info ad;
2144 struct smack_known *skp;
2145 struct smack_known *tkp = smk_of_task_struct(p);
2149 return 0; /* null signal; existence test */
2151 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2152 smk_ad_setfield_u_tsk(&ad, p);
2154 * Sending a signal requires that the sender
2155 * can write the receiver.
2158 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2159 rc = smk_bu_task(p, MAY_DELIVER, rc);
2163 * If the cred isn't NULL we're dealing with some USB IO
2164 * specific behavior. This is not clean. For one thing
2165 * we can't take privilege into account.
2167 skp = smk_of_task(smack_cred(cred));
2168 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2169 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2174 * smack_task_to_inode - copy task smack into the inode blob
2175 * @p: task to copy from
2176 * @inode: inode to copy to
2178 * Sets the smack pointer in the inode security blob
2180 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2182 struct inode_smack *isp = inode->i_security;
2183 struct smack_known *skp = smk_of_task_struct(p);
2185 isp->smk_inode = skp;
2186 isp->smk_flags |= SMK_INODE_INSTANT;
2194 * smack_sk_alloc_security - Allocate a socket blob
2197 * @gfp_flags: memory allocation flags
2199 * Assign Smack pointers to current
2201 * Returns 0 on success, -ENOMEM is there's no memory
2203 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2205 struct smack_known *skp = smk_of_current();
2206 struct socket_smack *ssp;
2208 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2213 * Sockets created by kernel threads receive web label.
2215 if (unlikely(current->flags & PF_KTHREAD)) {
2216 ssp->smk_in = &smack_known_web;
2217 ssp->smk_out = &smack_known_web;
2222 ssp->smk_packet = NULL;
2224 sk->sk_security = ssp;
2230 * smack_sk_free_security - Free a socket blob
2233 * Clears the blob pointer
2235 static void smack_sk_free_security(struct sock *sk)
2237 #ifdef SMACK_IPV6_PORT_LABELING
2238 struct smk_port_label *spp;
2240 if (sk->sk_family == PF_INET6) {
2242 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2243 if (spp->smk_sock != sk)
2245 spp->smk_can_reuse = 1;
2251 kfree(sk->sk_security);
2255 * smack_ipv4host_label - check host based restrictions
2256 * @sip: the object end
2258 * looks for host based access restrictions
2260 * This version will only be appropriate for really small sets of single label
2261 * hosts. The caller is responsible for ensuring that the RCU read lock is
2262 * taken before calling this function.
2264 * Returns the label of the far end or NULL if it's not special.
2266 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2268 struct smk_net4addr *snp;
2269 struct in_addr *siap = &sip->sin_addr;
2271 if (siap->s_addr == 0)
2274 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2276 * we break after finding the first match because
2277 * the list is sorted from longest to shortest mask
2278 * so we have found the most specific match
2280 if (snp->smk_host.s_addr ==
2281 (siap->s_addr & snp->smk_mask.s_addr))
2282 return snp->smk_label;
2287 #if IS_ENABLED(CONFIG_IPV6)
2289 * smk_ipv6_localhost - Check for local ipv6 host address
2292 * Returns boolean true if this is the localhost address
2294 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2296 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2297 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2299 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2300 ntohs(be16p[7]) == 1)
2306 * smack_ipv6host_label - check host based restrictions
2307 * @sip: the object end
2309 * looks for host based access restrictions
2311 * This version will only be appropriate for really small sets of single label
2312 * hosts. The caller is responsible for ensuring that the RCU read lock is
2313 * taken before calling this function.
2315 * Returns the label of the far end or NULL if it's not special.
2317 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2319 struct smk_net6addr *snp;
2320 struct in6_addr *sap = &sip->sin6_addr;
2325 * It's local. Don't look for a host label.
2327 if (smk_ipv6_localhost(sip))
2330 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2332 * If the label is NULL the entry has
2333 * been renounced. Ignore it.
2335 if (snp->smk_label == NULL)
2338 * we break after finding the first match because
2339 * the list is sorted from longest to shortest mask
2340 * so we have found the most specific match
2342 for (found = 1, i = 0; i < 8; i++) {
2343 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2344 snp->smk_host.s6_addr16[i]) {
2350 return snp->smk_label;
2355 #endif /* CONFIG_IPV6 */
2358 * smack_netlabel - Set the secattr on a socket
2360 * @labeled: socket label scheme
2362 * Convert the outbound smack value (smk_out) to a
2363 * secattr and attach it to the socket.
2365 * Returns 0 on success or an error code
2367 static int smack_netlabel(struct sock *sk, int labeled)
2369 struct smack_known *skp;
2370 struct socket_smack *ssp = sk->sk_security;
2374 * Usually the netlabel code will handle changing the
2375 * packet labeling based on the label.
2376 * The case of a single label host is different, because
2377 * a single label host should never get a labeled packet
2378 * even though the label is usually associated with a packet
2382 bh_lock_sock_nested(sk);
2384 if (ssp->smk_out == smack_net_ambient ||
2385 labeled == SMACK_UNLABELED_SOCKET)
2386 netlbl_sock_delattr(sk);
2389 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2399 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2401 * @sap: the destination address
2403 * Set the correct secattr for the given socket based on the destination
2404 * address and perform any outbound access checks needed.
2406 * Returns 0 on success or an error code.
2409 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2411 struct smack_known *skp;
2414 struct smack_known *hkp;
2415 struct socket_smack *ssp = sk->sk_security;
2416 struct smk_audit_info ad;
2419 hkp = smack_ipv4host_label(sap);
2422 struct lsm_network_audit net;
2424 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2425 ad.a.u.net->family = sap->sin_family;
2426 ad.a.u.net->dport = sap->sin_port;
2427 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2429 sk_lbl = SMACK_UNLABELED_SOCKET;
2431 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2432 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2434 sk_lbl = SMACK_CIPSO_SOCKET;
2441 return smack_netlabel(sk, sk_lbl);
2444 #if IS_ENABLED(CONFIG_IPV6)
2446 * smk_ipv6_check - check Smack access
2447 * @subject: subject Smack label
2448 * @object: object Smack label
2450 * @act: the action being taken
2452 * Check an IPv6 access
2454 static int smk_ipv6_check(struct smack_known *subject,
2455 struct smack_known *object,
2456 struct sockaddr_in6 *address, int act)
2459 struct lsm_network_audit net;
2461 struct smk_audit_info ad;
2465 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2466 ad.a.u.net->family = PF_INET6;
2467 ad.a.u.net->dport = ntohs(address->sin6_port);
2468 if (act == SMK_RECEIVING)
2469 ad.a.u.net->v6info.saddr = address->sin6_addr;
2471 ad.a.u.net->v6info.daddr = address->sin6_addr;
2473 rc = smk_access(subject, object, MAY_WRITE, &ad);
2474 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2477 #endif /* CONFIG_IPV6 */
2479 #ifdef SMACK_IPV6_PORT_LABELING
2481 * smk_ipv6_port_label - Smack port access table management
2485 * Create or update the port list entry
2487 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2489 struct sock *sk = sock->sk;
2490 struct sockaddr_in6 *addr6;
2491 struct socket_smack *ssp = sock->sk->sk_security;
2492 struct smk_port_label *spp;
2493 unsigned short port = 0;
2495 if (address == NULL) {
2497 * This operation is changing the Smack information
2498 * on the bound socket. Take the changes to the port
2502 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2503 if (sk != spp->smk_sock)
2505 spp->smk_in = ssp->smk_in;
2506 spp->smk_out = ssp->smk_out;
2511 * A NULL address is only used for updating existing
2512 * bound entries. If there isn't one, it's OK.
2518 addr6 = (struct sockaddr_in6 *)address;
2519 port = ntohs(addr6->sin6_port);
2521 * This is a special case that is safely ignored.
2527 * Look for an existing port list entry.
2528 * This is an indication that a port is getting reused.
2531 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2532 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2534 if (spp->smk_can_reuse != 1) {
2538 spp->smk_port = port;
2540 spp->smk_in = ssp->smk_in;
2541 spp->smk_out = ssp->smk_out;
2542 spp->smk_can_reuse = 0;
2548 * A new port entry is required.
2550 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2554 spp->smk_port = port;
2556 spp->smk_in = ssp->smk_in;
2557 spp->smk_out = ssp->smk_out;
2558 spp->smk_sock_type = sock->type;
2559 spp->smk_can_reuse = 0;
2561 mutex_lock(&smack_ipv6_lock);
2562 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2563 mutex_unlock(&smack_ipv6_lock);
2568 * smk_ipv6_port_check - check Smack port access
2572 * Create or update the port list entry
2574 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2577 struct smk_port_label *spp;
2578 struct socket_smack *ssp = sk->sk_security;
2579 struct smack_known *skp = NULL;
2580 unsigned short port;
2581 struct smack_known *object;
2583 if (act == SMK_RECEIVING) {
2584 skp = smack_ipv6host_label(address);
2585 object = ssp->smk_in;
2588 object = smack_ipv6host_label(address);
2592 * The other end is a single label host.
2594 if (skp != NULL && object != NULL)
2595 return smk_ipv6_check(skp, object, address, act);
2597 skp = smack_net_ambient;
2599 object = smack_net_ambient;
2602 * It's remote, so port lookup does no good.
2604 if (!smk_ipv6_localhost(address))
2605 return smk_ipv6_check(skp, object, address, act);
2608 * It's local so the send check has to have passed.
2610 if (act == SMK_RECEIVING)
2613 port = ntohs(address->sin6_port);
2615 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2616 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2618 object = spp->smk_in;
2619 if (act == SMK_CONNECTING)
2620 ssp->smk_packet = spp->smk_out;
2625 return smk_ipv6_check(skp, object, address, act);
2627 #endif /* SMACK_IPV6_PORT_LABELING */
2630 * smack_inode_setsecurity - set smack xattrs
2631 * @inode: the object
2632 * @name: attribute name
2633 * @value: attribute value
2634 * @size: size of the attribute
2637 * Sets the named attribute in the appropriate blob
2639 * Returns 0 on success, or an error code
2641 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2642 const void *value, size_t size, int flags)
2644 struct smack_known *skp;
2645 struct inode_smack *nsp = inode->i_security;
2646 struct socket_smack *ssp;
2647 struct socket *sock;
2650 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2653 skp = smk_import_entry(value, size);
2655 return PTR_ERR(skp);
2657 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2658 nsp->smk_inode = skp;
2659 nsp->smk_flags |= SMK_INODE_INSTANT;
2663 * The rest of the Smack xattrs are only on sockets.
2665 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2668 sock = SOCKET_I(inode);
2669 if (sock == NULL || sock->sk == NULL)
2672 ssp = sock->sk->sk_security;
2674 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2676 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2678 if (sock->sk->sk_family == PF_INET) {
2679 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2682 "Smack: \"%s\" netlbl error %d.\n",
2688 #ifdef SMACK_IPV6_PORT_LABELING
2689 if (sock->sk->sk_family == PF_INET6)
2690 smk_ipv6_port_label(sock, NULL);
2697 * smack_socket_post_create - finish socket setup
2699 * @family: protocol family
2704 * Sets the netlabel information on the socket
2706 * Returns 0 on success, and error code otherwise
2708 static int smack_socket_post_create(struct socket *sock, int family,
2709 int type, int protocol, int kern)
2711 struct socket_smack *ssp;
2713 if (sock->sk == NULL)
2717 * Sockets created by kernel threads receive web label.
2719 if (unlikely(current->flags & PF_KTHREAD)) {
2720 ssp = sock->sk->sk_security;
2721 ssp->smk_in = &smack_known_web;
2722 ssp->smk_out = &smack_known_web;
2725 if (family != PF_INET)
2728 * Set the outbound netlbl.
2730 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2734 * smack_socket_socketpair - create socket pair
2735 * @socka: one socket
2736 * @sockb: another socket
2738 * Cross reference the peer labels for SO_PEERSEC
2740 * Returns 0 on success, and error code otherwise
2742 static int smack_socket_socketpair(struct socket *socka,
2743 struct socket *sockb)
2745 struct socket_smack *asp = socka->sk->sk_security;
2746 struct socket_smack *bsp = sockb->sk->sk_security;
2748 asp->smk_packet = bsp->smk_out;
2749 bsp->smk_packet = asp->smk_out;
2754 #ifdef SMACK_IPV6_PORT_LABELING
2756 * smack_socket_bind - record port binding information.
2758 * @address: the port address
2759 * @addrlen: size of the address
2761 * Records the label bound to a port.
2765 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2768 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2769 smk_ipv6_port_label(sock, address);
2772 #endif /* SMACK_IPV6_PORT_LABELING */
2775 * smack_socket_connect - connect access check
2777 * @sap: the other end
2778 * @addrlen: size of sap
2780 * Verifies that a connection may be possible
2782 * Returns 0 on success, and error code otherwise
2784 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2788 #if IS_ENABLED(CONFIG_IPV6)
2789 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2791 #ifdef SMACK_IPV6_SECMARK_LABELING
2792 struct smack_known *rsp;
2793 struct socket_smack *ssp;
2796 if (sock->sk == NULL)
2799 #ifdef SMACK_IPV6_SECMARK_LABELING
2800 ssp = sock->sk->sk_security;
2803 switch (sock->sk->sk_family) {
2805 if (addrlen < sizeof(struct sockaddr_in))
2807 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2810 if (addrlen < sizeof(struct sockaddr_in6))
2812 #ifdef SMACK_IPV6_SECMARK_LABELING
2813 rsp = smack_ipv6host_label(sip);
2815 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2818 #ifdef SMACK_IPV6_PORT_LABELING
2819 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2827 * smack_flags_to_may - convert S_ to MAY_ values
2828 * @flags: the S_ value
2830 * Returns the equivalent MAY_ value
2832 static int smack_flags_to_may(int flags)
2836 if (flags & S_IRUGO)
2838 if (flags & S_IWUGO)
2840 if (flags & S_IXUGO)
2847 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2852 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2854 struct smack_known *skp = smk_of_current();
2856 msg->security = skp;
2861 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2864 * Clears the blob pointer
2866 static void smack_msg_msg_free_security(struct msg_msg *msg)
2868 msg->security = NULL;
2872 * smack_of_ipc - the smack pointer for the ipc
2875 * Returns a pointer to the smack value
2877 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2879 return (struct smack_known *)isp->security;
2883 * smack_ipc_alloc_security - Set the security blob for ipc
2888 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2890 struct smack_known *skp = smk_of_current();
2892 isp->security = skp;
2897 * smack_ipc_free_security - Clear the security blob for ipc
2900 * Clears the blob pointer
2902 static void smack_ipc_free_security(struct kern_ipc_perm *isp)
2904 isp->security = NULL;
2908 * smk_curacc_shm : check if current has access on shm
2910 * @access : access requested
2912 * Returns 0 if current has the requested access, error code otherwise
2914 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2916 struct smack_known *ssp = smack_of_ipc(isp);
2917 struct smk_audit_info ad;
2921 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2922 ad.a.u.ipc_id = isp->id;
2924 rc = smk_curacc(ssp, access, &ad);
2925 rc = smk_bu_current("shm", ssp, access, rc);
2930 * smack_shm_associate - Smack access check for shm
2932 * @shmflg: access requested
2934 * Returns 0 if current has the requested access, error code otherwise
2936 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2940 may = smack_flags_to_may(shmflg);
2941 return smk_curacc_shm(isp, may);
2945 * smack_shm_shmctl - Smack access check for shm
2947 * @cmd: what it wants to do
2949 * Returns 0 if current has the requested access, error code otherwise
2951 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2965 may = MAY_READWRITE;
2970 * System level information.
2976 return smk_curacc_shm(isp, may);
2980 * smack_shm_shmat - Smack access for shmat
2983 * @shmflg: access requested
2985 * Returns 0 if current has the requested access, error code otherwise
2987 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
2992 may = smack_flags_to_may(shmflg);
2993 return smk_curacc_shm(ipc, may);
2997 * smk_curacc_sem : check if current has access on sem
2999 * @access : access requested
3001 * Returns 0 if current has the requested access, error code otherwise
3003 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3005 struct smack_known *ssp = smack_of_ipc(isp);
3006 struct smk_audit_info ad;
3010 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3011 ad.a.u.ipc_id = isp->id;
3013 rc = smk_curacc(ssp, access, &ad);
3014 rc = smk_bu_current("sem", ssp, access, rc);
3019 * smack_sem_associate - Smack access check for sem
3021 * @semflg: access requested
3023 * Returns 0 if current has the requested access, error code otherwise
3025 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3029 may = smack_flags_to_may(semflg);
3030 return smk_curacc_sem(isp, may);
3034 * smack_sem_shmctl - Smack access check for sem
3036 * @cmd: what it wants to do
3038 * Returns 0 if current has the requested access, error code otherwise
3040 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3059 may = MAY_READWRITE;
3064 * System level information
3071 return smk_curacc_sem(isp, may);
3075 * smack_sem_semop - Smack checks of semaphore operations
3081 * Treated as read and write in all cases.
3083 * Returns 0 if access is allowed, error code otherwise
3085 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3086 unsigned nsops, int alter)
3088 return smk_curacc_sem(isp, MAY_READWRITE);
3092 * smk_curacc_msq : helper to check if current has access on msq
3094 * @access : access requested
3096 * return 0 if current has access, error otherwise
3098 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3100 struct smack_known *msp = smack_of_ipc(isp);
3101 struct smk_audit_info ad;
3105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3106 ad.a.u.ipc_id = isp->id;
3108 rc = smk_curacc(msp, access, &ad);
3109 rc = smk_bu_current("msq", msp, access, rc);
3114 * smack_msg_queue_associate - Smack access check for msg_queue
3116 * @msqflg: access requested
3118 * Returns 0 if current has the requested access, error code otherwise
3120 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3124 may = smack_flags_to_may(msqflg);
3125 return smk_curacc_msq(isp, may);
3129 * smack_msg_queue_msgctl - Smack access check for msg_queue
3131 * @cmd: what it wants to do
3133 * Returns 0 if current has the requested access, error code otherwise
3135 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3147 may = MAY_READWRITE;
3152 * System level information
3159 return smk_curacc_msq(isp, may);
3163 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3166 * @msqflg: access requested
3168 * Returns 0 if current has the requested access, error code otherwise
3170 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3175 may = smack_flags_to_may(msqflg);
3176 return smk_curacc_msq(isp, may);
3180 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3187 * Returns 0 if current has read and write access, error code otherwise
3189 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3190 struct task_struct *target, long type, int mode)
3192 return smk_curacc_msq(isp, MAY_READWRITE);
3196 * smack_ipc_permission - Smack access for ipc_permission()
3197 * @ipp: the object permissions
3198 * @flag: access requested
3200 * Returns 0 if current has read and write access, error code otherwise
3202 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3204 struct smack_known *iskp = ipp->security;
3205 int may = smack_flags_to_may(flag);
3206 struct smk_audit_info ad;
3210 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3211 ad.a.u.ipc_id = ipp->id;
3213 rc = smk_curacc(iskp, may, &ad);
3214 rc = smk_bu_current("svipc", iskp, may, rc);
3219 * smack_ipc_getsecid - Extract smack security id
3220 * @ipp: the object permissions
3221 * @secid: where result will be saved
3223 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3225 struct smack_known *iskp = ipp->security;
3227 *secid = iskp->smk_secid;
3231 * smack_d_instantiate - Make sure the blob is correct on an inode
3232 * @opt_dentry: dentry where inode will be attached
3233 * @inode: the object
3235 * Set the inode's security blob if it hasn't been done already.
3237 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3239 struct super_block *sbp;
3240 struct superblock_smack *sbsp;
3241 struct inode_smack *isp;
3242 struct smack_known *skp;
3243 struct smack_known *ckp = smk_of_current();
3244 struct smack_known *final;
3245 char trattr[TRANS_TRUE_SIZE];
3253 isp = inode->i_security;
3255 mutex_lock(&isp->smk_lock);
3257 * If the inode is already instantiated
3258 * take the quick way out
3260 if (isp->smk_flags & SMK_INODE_INSTANT)
3264 sbsp = sbp->s_security;
3266 * We're going to use the superblock default label
3267 * if there's no label on the file.
3269 final = sbsp->smk_default;
3272 * If this is the root inode the superblock
3273 * may be in the process of initialization.
3274 * If that is the case use the root value out
3275 * of the superblock.
3277 if (opt_dentry->d_parent == opt_dentry) {
3278 switch (sbp->s_magic) {
3279 case CGROUP_SUPER_MAGIC:
3280 case CGROUP2_SUPER_MAGIC:
3282 * The cgroup filesystem is never mounted,
3283 * so there's no opportunity to set the mount
3286 sbsp->smk_root = &smack_known_star;
3287 sbsp->smk_default = &smack_known_star;
3288 isp->smk_inode = sbsp->smk_root;
3292 * What about shmem/tmpfs anonymous files with dentry
3293 * obtained from d_alloc_pseudo()?
3295 isp->smk_inode = smk_of_current();
3298 isp->smk_inode = smk_of_current();
3302 * Socket access is controlled by the socket
3303 * structures associated with the task involved.
3305 isp->smk_inode = &smack_known_star;
3308 isp->smk_inode = sbsp->smk_root;
3311 isp->smk_flags |= SMK_INODE_INSTANT;
3316 * This is pretty hackish.
3317 * Casey says that we shouldn't have to do
3318 * file system specific code, but it does help
3319 * with keeping it simple.
3321 switch (sbp->s_magic) {
3323 case CGROUP_SUPER_MAGIC:
3324 case CGROUP2_SUPER_MAGIC:
3326 * Casey says that it's a little embarrassing
3327 * that the smack file system doesn't do
3328 * extended attributes.
3330 * Cgroupfs is special
3332 final = &smack_known_star;
3334 case DEVPTS_SUPER_MAGIC:
3336 * devpts seems content with the label of the task.
3337 * Programs that change smack have to treat the
3342 case PROC_SUPER_MAGIC:
3344 * Casey says procfs appears not to care.
3345 * The superblock default suffices.
3350 * Device labels should come from the filesystem,
3351 * but watch out, because they're volitile,
3352 * getting recreated on every reboot.
3354 final = &smack_known_star;
3358 * If a smack value has been set we want to use it,
3359 * but since tmpfs isn't giving us the opportunity
3360 * to set mount options simulate setting the
3361 * superblock default.
3365 * This isn't an understood special case.
3366 * Get the value from the xattr.
3370 * UNIX domain sockets use lower level socket data.
3372 if (S_ISSOCK(inode->i_mode)) {
3373 final = &smack_known_star;
3377 * No xattr support means, alas, no SMACK label.
3378 * Use the aforeapplied default.
3379 * It would be curious if the label of the task
3380 * does not match that assigned.
3382 if (!(inode->i_opflags & IOP_XATTR))
3385 * Get the dentry for xattr.
3387 dp = dget(opt_dentry);
3388 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3389 if (!IS_ERR_OR_NULL(skp))
3393 * Transmuting directory
3395 if (S_ISDIR(inode->i_mode)) {
3397 * If this is a new directory and the label was
3398 * transmuted when the inode was initialized
3399 * set the transmute attribute on the directory
3400 * and mark the inode.
3402 * If there is a transmute attribute on the
3403 * directory mark the inode.
3405 if (isp->smk_flags & SMK_INODE_CHANGED) {
3406 isp->smk_flags &= ~SMK_INODE_CHANGED;
3407 rc = __vfs_setxattr(dp, inode,
3408 XATTR_NAME_SMACKTRANSMUTE,
3409 TRANS_TRUE, TRANS_TRUE_SIZE,
3412 rc = __vfs_getxattr(dp, inode,
3413 XATTR_NAME_SMACKTRANSMUTE, trattr,
3415 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3416 TRANS_TRUE_SIZE) != 0)
3420 transflag = SMK_INODE_TRANSMUTE;
3423 * Don't let the exec or mmap label be "*" or "@".
3425 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3426 if (IS_ERR(skp) || skp == &smack_known_star ||
3427 skp == &smack_known_web)
3429 isp->smk_task = skp;
3431 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3432 if (IS_ERR(skp) || skp == &smack_known_star ||
3433 skp == &smack_known_web)
3435 isp->smk_mmap = skp;
3442 isp->smk_inode = ckp;
3444 isp->smk_inode = final;
3446 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3449 mutex_unlock(&isp->smk_lock);
3454 * smack_getprocattr - Smack process attribute access
3455 * @p: the object task
3456 * @name: the name of the attribute in /proc/.../attr
3457 * @value: where to put the result
3459 * Places a copy of the task Smack into value
3461 * Returns the length of the smack label or an error code
3463 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3465 struct smack_known *skp = smk_of_task_struct(p);
3469 if (strcmp(name, "current") != 0)
3472 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3482 * smack_setprocattr - Smack process attribute setting
3483 * @name: the name of the attribute in /proc/.../attr
3484 * @value: the value to set
3485 * @size: the size of the value
3487 * Sets the Smack value of the task. Only setting self
3488 * is permitted and only with privilege
3490 * Returns the length of the smack label or an error code
3492 static int smack_setprocattr(const char *name, void *value, size_t size)
3494 struct task_smack *tsp = smack_cred(current_cred());
3496 struct smack_known *skp;
3497 struct smack_known_list_elem *sklep;
3500 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3503 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3506 if (strcmp(name, "current") != 0)
3509 skp = smk_import_entry(value, size);
3511 return PTR_ERR(skp);
3514 * No process is ever allowed the web ("@") label
3515 * and the star ("*") label.
3517 if (skp == &smack_known_web || skp == &smack_known_star)
3520 if (!smack_privileged(CAP_MAC_ADMIN)) {
3522 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3523 if (sklep->smk_label == skp) {
3531 new = prepare_creds();
3535 tsp = smack_cred(new);
3536 tsp->smk_task = skp;
3538 * process can change its label only once
3540 smk_destroy_label_list(&tsp->smk_relabel);
3547 * smack_unix_stream_connect - Smack access on UDS
3549 * @other: the other sock
3552 * Return 0 if a subject with the smack of sock could access
3553 * an object with the smack of other, otherwise an error code
3555 static int smack_unix_stream_connect(struct sock *sock,
3556 struct sock *other, struct sock *newsk)
3558 struct smack_known *skp;
3559 struct smack_known *okp;
3560 struct socket_smack *ssp = sock->sk_security;
3561 struct socket_smack *osp = other->sk_security;
3562 struct socket_smack *nsp = newsk->sk_security;
3563 struct smk_audit_info ad;
3566 struct lsm_network_audit net;
3569 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3573 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3574 smk_ad_setfield_u_net_sk(&ad, other);
3576 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3577 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3581 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3582 rc = smk_bu_note("UDS connect", okp, skp,
3588 * Cross reference the peer labels for SO_PEERSEC.
3591 nsp->smk_packet = ssp->smk_out;
3592 ssp->smk_packet = osp->smk_out;
3599 * smack_unix_may_send - Smack access on UDS
3601 * @other: the other socket
3603 * Return 0 if a subject with the smack of sock could access
3604 * an object with the smack of other, otherwise an error code
3606 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3608 struct socket_smack *ssp = sock->sk->sk_security;
3609 struct socket_smack *osp = other->sk->sk_security;
3610 struct smk_audit_info ad;
3614 struct lsm_network_audit net;
3616 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3617 smk_ad_setfield_u_net_sk(&ad, other->sk);
3620 if (smack_privileged(CAP_MAC_OVERRIDE))
3623 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3624 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3629 * smack_socket_sendmsg - Smack check based on destination host
3632 * @size: the size of the message
3634 * Return 0 if the current subject can write to the destination host.
3635 * For IPv4 this is only a question if the destination is a single label host.
3636 * For IPv6 this is a check against the label of the port.
3638 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3641 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3642 #if IS_ENABLED(CONFIG_IPV6)
3643 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3645 #ifdef SMACK_IPV6_SECMARK_LABELING
3646 struct socket_smack *ssp = sock->sk->sk_security;
3647 struct smack_known *rsp;
3652 * Perfectly reasonable for this to be NULL
3657 switch (sock->sk->sk_family) {
3659 rc = smack_netlabel_send(sock->sk, sip);
3662 #ifdef SMACK_IPV6_SECMARK_LABELING
3663 rsp = smack_ipv6host_label(sap);
3665 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3668 #ifdef SMACK_IPV6_PORT_LABELING
3669 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3677 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3678 * @sap: netlabel secattr
3679 * @ssp: socket security information
3681 * Returns a pointer to a Smack label entry found on the label list.
3683 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3684 struct socket_smack *ssp)
3686 struct smack_known *skp;
3691 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3693 * Looks like a CIPSO packet.
3694 * If there are flags but no level netlabel isn't
3695 * behaving the way we expect it to.
3697 * Look it up in the label table
3698 * Without guidance regarding the smack value
3699 * for the packet fall back on the network
3703 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3704 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3707 * Compare the catsets. Use the netlbl APIs.
3709 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3710 if ((skp->smk_netlabel.flags &
3711 NETLBL_SECATTR_MLS_CAT) == 0)
3715 for (acat = -1, kcat = -1; acat == kcat; ) {
3716 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3718 kcat = netlbl_catmap_walk(
3719 skp->smk_netlabel.attr.mls.cat,
3721 if (acat < 0 || kcat < 0)
3734 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3735 return &smack_known_web;
3736 return &smack_known_star;
3738 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3740 * Looks like a fallback, which gives us a secid.
3742 return smack_from_secid(sap->attr.secid);
3744 * Without guidance regarding the smack value
3745 * for the packet fall back on the network
3748 return smack_net_ambient;
3751 #if IS_ENABLED(CONFIG_IPV6)
3752 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3756 int proto = -EINVAL;
3757 struct ipv6hdr _ipv6h;
3758 struct ipv6hdr *ip6;
3760 struct tcphdr _tcph, *th;
3761 struct udphdr _udph, *uh;
3762 struct dccp_hdr _dccph, *dh;
3766 offset = skb_network_offset(skb);
3767 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3770 sip->sin6_addr = ip6->saddr;
3772 nexthdr = ip6->nexthdr;
3773 offset += sizeof(_ipv6h);
3774 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3781 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3783 sip->sin6_port = th->source;
3786 case IPPROTO_UDPLITE:
3787 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3789 sip->sin6_port = uh->source;
3792 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3794 sip->sin6_port = dh->dccph_sport;
3799 #endif /* CONFIG_IPV6 */
3802 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3806 * Returns 0 if the packet should be delivered, an error code otherwise
3808 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3810 struct netlbl_lsm_secattr secattr;
3811 struct socket_smack *ssp = sk->sk_security;
3812 struct smack_known *skp = NULL;
3814 struct smk_audit_info ad;
3815 u16 family = sk->sk_family;
3817 struct lsm_network_audit net;
3819 #if IS_ENABLED(CONFIG_IPV6)
3820 struct sockaddr_in6 sadd;
3823 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3825 #endif /* CONFIG_IPV6 */
3829 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3831 * If there is a secmark use it rather than the CIPSO label.
3832 * If there is no secmark fall back to CIPSO.
3833 * The secmark is assumed to reflect policy better.
3835 if (skb && skb->secmark != 0) {
3836 skp = smack_from_secid(skb->secmark);
3839 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3841 * Translate what netlabel gave us.
3843 netlbl_secattr_init(&secattr);
3845 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3847 skp = smack_from_secattr(&secattr, ssp);
3849 skp = smack_net_ambient;
3851 netlbl_secattr_destroy(&secattr);
3853 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3857 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3858 ad.a.u.net->family = family;
3859 ad.a.u.net->netif = skb->skb_iif;
3860 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3863 * Receiving a packet requires that the other end
3864 * be able to write here. Read access is not required.
3865 * This is the simplist possible security model
3868 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3869 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3872 netlbl_skbuff_err(skb, family, rc, 0);
3874 #if IS_ENABLED(CONFIG_IPV6)
3876 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3877 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3878 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3880 #ifdef SMACK_IPV6_SECMARK_LABELING
3881 if (skb && skb->secmark != 0)
3882 skp = smack_from_secid(skb->secmark);
3884 skp = smack_ipv6host_label(&sadd);
3886 skp = smack_net_ambient;
3888 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3889 ad.a.u.net->family = family;
3890 ad.a.u.net->netif = skb->skb_iif;
3891 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3892 #endif /* CONFIG_AUDIT */
3893 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3894 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3896 #endif /* SMACK_IPV6_SECMARK_LABELING */
3897 #ifdef SMACK_IPV6_PORT_LABELING
3898 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3899 #endif /* SMACK_IPV6_PORT_LABELING */
3901 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3902 ICMPV6_ADM_PROHIBITED, 0);
3904 #endif /* CONFIG_IPV6 */
3911 * smack_socket_getpeersec_stream - pull in packet label
3913 * @optval: user's destination
3914 * @optlen: size thereof
3917 * returns zero on success, an error code otherwise
3919 static int smack_socket_getpeersec_stream(struct socket *sock,
3920 char __user *optval,
3921 int __user *optlen, unsigned len)
3923 struct socket_smack *ssp;
3928 ssp = sock->sk->sk_security;
3929 if (ssp->smk_packet != NULL) {
3930 rcp = ssp->smk_packet->smk_known;
3931 slen = strlen(rcp) + 1;
3936 else if (copy_to_user(optval, rcp, slen) != 0)
3939 if (put_user(slen, optlen) != 0)
3947 * smack_socket_getpeersec_dgram - pull in packet label
3948 * @sock: the peer socket
3950 * @secid: pointer to where to put the secid of the packet
3952 * Sets the netlabel socket state on sk from parent
3954 static int smack_socket_getpeersec_dgram(struct socket *sock,
3955 struct sk_buff *skb, u32 *secid)
3958 struct netlbl_lsm_secattr secattr;
3959 struct socket_smack *ssp = NULL;
3960 struct smack_known *skp;
3961 int family = PF_UNSPEC;
3962 u32 s = 0; /* 0 is the invalid secid */
3966 if (skb->protocol == htons(ETH_P_IP))
3968 #if IS_ENABLED(CONFIG_IPV6)
3969 else if (skb->protocol == htons(ETH_P_IPV6))
3971 #endif /* CONFIG_IPV6 */
3973 if (family == PF_UNSPEC && sock != NULL)
3974 family = sock->sk->sk_family;
3978 ssp = sock->sk->sk_security;
3979 s = ssp->smk_out->smk_secid;
3982 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3988 * Translate what netlabel gave us.
3990 if (sock != NULL && sock->sk != NULL)
3991 ssp = sock->sk->sk_security;
3992 netlbl_secattr_init(&secattr);
3993 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3995 skp = smack_from_secattr(&secattr, ssp);
3998 netlbl_secattr_destroy(&secattr);
4001 #ifdef SMACK_IPV6_SECMARK_LABELING
4013 * smack_sock_graft - Initialize a newly created socket with an existing sock
4015 * @parent: parent socket
4017 * Set the smk_{in,out} state of an existing sock based on the process that
4018 * is creating the new socket.
4020 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4022 struct socket_smack *ssp;
4023 struct smack_known *skp = smk_of_current();
4026 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4029 ssp = sk->sk_security;
4032 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4036 * smack_inet_conn_request - Smack access check on connect
4037 * @sk: socket involved
4041 * Returns 0 if a task with the packet label could write to
4042 * the socket, otherwise an error code
4044 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4045 struct request_sock *req)
4047 u16 family = sk->sk_family;
4048 struct smack_known *skp;
4049 struct socket_smack *ssp = sk->sk_security;
4050 struct netlbl_lsm_secattr secattr;
4051 struct sockaddr_in addr;
4053 struct smack_known *hskp;
4055 struct smk_audit_info ad;
4057 struct lsm_network_audit net;
4060 #if IS_ENABLED(CONFIG_IPV6)
4061 if (family == PF_INET6) {
4063 * Handle mapped IPv4 packets arriving
4064 * via IPv6 sockets. Don't set up netlabel
4065 * processing on IPv6.
4067 if (skb->protocol == htons(ETH_P_IP))
4072 #endif /* CONFIG_IPV6 */
4074 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4076 * If there is a secmark use it rather than the CIPSO label.
4077 * If there is no secmark fall back to CIPSO.
4078 * The secmark is assumed to reflect policy better.
4080 if (skb && skb->secmark != 0) {
4081 skp = smack_from_secid(skb->secmark);
4084 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4086 netlbl_secattr_init(&secattr);
4087 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4089 skp = smack_from_secattr(&secattr, ssp);
4091 skp = &smack_known_huh;
4092 netlbl_secattr_destroy(&secattr);
4094 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4099 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4100 ad.a.u.net->family = family;
4101 ad.a.u.net->netif = skb->skb_iif;
4102 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4105 * Receiving a packet requires that the other end be able to write
4106 * here. Read access is not required.
4108 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4109 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4114 * Save the peer's label in the request_sock so we can later setup
4115 * smk_packet in the child socket so that SO_PEERCRED can report it.
4117 req->peer_secid = skp->smk_secid;
4120 * We need to decide if we want to label the incoming connection here
4121 * if we do we only need to label the request_sock and the stack will
4122 * propagate the wire-label to the sock when it is created.
4125 addr.sin_addr.s_addr = hdr->saddr;
4127 hskp = smack_ipv4host_label(&addr);
4131 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4133 netlbl_req_delattr(req);
4139 * smack_inet_csk_clone - Copy the connection information to the new socket
4140 * @sk: the new socket
4141 * @req: the connection's request_sock
4143 * Transfer the connection's peer label to the newly created socket.
4145 static void smack_inet_csk_clone(struct sock *sk,
4146 const struct request_sock *req)
4148 struct socket_smack *ssp = sk->sk_security;
4149 struct smack_known *skp;
4151 if (req->peer_secid != 0) {
4152 skp = smack_from_secid(req->peer_secid);
4153 ssp->smk_packet = skp;
4155 ssp->smk_packet = NULL;
4159 * Key management security hooks
4161 * Casey has not tested key support very heavily.
4162 * The permission check is most likely too restrictive.
4163 * If you care about keys please have a look.
4168 * smack_key_alloc - Set the key security blob
4170 * @cred: the credentials to use
4173 * No allocation required
4177 static int smack_key_alloc(struct key *key, const struct cred *cred,
4178 unsigned long flags)
4180 struct smack_known *skp = smk_of_task(smack_cred(cred));
4182 key->security = skp;
4187 * smack_key_free - Clear the key security blob
4190 * Clear the blob pointer
4192 static void smack_key_free(struct key *key)
4194 key->security = NULL;
4198 * smack_key_permission - Smack access on a key
4199 * @key_ref: gets to the object
4200 * @cred: the credentials to use
4201 * @perm: requested key permissions
4203 * Return 0 if the task has read and write to the object,
4204 * an error code otherwise
4206 static int smack_key_permission(key_ref_t key_ref,
4207 const struct cred *cred, unsigned perm)
4210 struct smk_audit_info ad;
4211 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4216 * Validate requested permissions
4218 if (perm & ~KEY_NEED_ALL)
4221 keyp = key_ref_to_ptr(key_ref);
4225 * If the key hasn't been initialized give it access so that
4228 if (keyp->security == NULL)
4231 * This should not occur
4236 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4240 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4241 ad.a.u.key_struct.key = keyp->serial;
4242 ad.a.u.key_struct.key_desc = keyp->description;
4244 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4245 request |= MAY_READ;
4246 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4247 request |= MAY_WRITE;
4248 rc = smk_access(tkp, keyp->security, request, &ad);
4249 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4254 * smack_key_getsecurity - Smack label tagging the key
4255 * @key points to the key to be queried
4256 * @_buffer points to a pointer that should be set to point to the
4257 * resulting string (if no label or an error occurs).
4258 * Return the length of the string (including terminating NUL) or -ve if
4260 * May also return 0 (and a NULL buffer pointer) if there is no label.
4262 static int smack_key_getsecurity(struct key *key, char **_buffer)
4264 struct smack_known *skp = key->security;
4268 if (key->security == NULL) {
4273 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4276 length = strlen(copy) + 1;
4282 #endif /* CONFIG_KEYS */
4287 * Audit requires a unique representation of each Smack specific
4288 * rule. This unique representation is used to distinguish the
4289 * object to be audited from remaining kernel objects and also
4290 * works as a glue between the audit hooks.
4292 * Since repository entries are added but never deleted, we'll use
4293 * the smack_known label address related to the given audit rule as
4294 * the needed unique representation. This also better fits the smack
4295 * model where nearly everything is a label.
4300 * smack_audit_rule_init - Initialize a smack audit rule
4301 * @field: audit rule fields given from user-space (audit.h)
4302 * @op: required testing operator (=, !=, >, <, ...)
4303 * @rulestr: smack label to be audited
4304 * @vrule: pointer to save our own audit rule representation
4306 * Prepare to audit cases where (@field @op @rulestr) is true.
4307 * The label to be audited is created if necessay.
4309 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4311 struct smack_known *skp;
4312 char **rule = (char **)vrule;
4315 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4318 if (op != Audit_equal && op != Audit_not_equal)
4321 skp = smk_import_entry(rulestr, 0);
4323 return PTR_ERR(skp);
4325 *rule = skp->smk_known;
4331 * smack_audit_rule_known - Distinguish Smack audit rules
4332 * @krule: rule of interest, in Audit kernel representation format
4334 * This is used to filter Smack rules from remaining Audit ones.
4335 * If it's proved that this rule belongs to us, the
4336 * audit_rule_match hook will be called to do the final judgement.
4338 static int smack_audit_rule_known(struct audit_krule *krule)
4340 struct audit_field *f;
4343 for (i = 0; i < krule->field_count; i++) {
4344 f = &krule->fields[i];
4346 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4354 * smack_audit_rule_match - Audit given object ?
4355 * @secid: security id for identifying the object to test
4356 * @field: audit rule flags given from user-space
4357 * @op: required testing operator
4358 * @vrule: smack internal rule presentation
4359 * @actx: audit context associated with the check
4361 * The core Audit hook. It's used to take the decision of
4362 * whether to audit or not to audit a given object.
4364 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4365 struct audit_context *actx)
4367 struct smack_known *skp;
4370 if (unlikely(!rule)) {
4371 WARN_ONCE(1, "Smack: missing rule\n");
4375 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4378 skp = smack_from_secid(secid);
4381 * No need to do string comparisons. If a match occurs,
4382 * both pointers will point to the same smack_known
4385 if (op == Audit_equal)
4386 return (rule == skp->smk_known);
4387 if (op == Audit_not_equal)
4388 return (rule != skp->smk_known);
4394 * There is no need for a smack_audit_rule_free hook.
4395 * No memory was allocated.
4398 #endif /* CONFIG_AUDIT */
4401 * smack_ismaclabel - check if xattr @name references a smack MAC label
4402 * @name: Full xattr name to check.
4404 static int smack_ismaclabel(const char *name)
4406 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4411 * smack_secid_to_secctx - return the smack label for a secid
4412 * @secid: incoming integer
4413 * @secdata: destination
4414 * @seclen: how long it is
4416 * Exists for networking code.
4418 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4420 struct smack_known *skp = smack_from_secid(secid);
4423 *secdata = skp->smk_known;
4424 *seclen = strlen(skp->smk_known);
4429 * smack_secctx_to_secid - return the secid for a smack label
4430 * @secdata: smack label
4431 * @seclen: how long result is
4432 * @secid: outgoing integer
4434 * Exists for audit and networking code.
4436 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4438 struct smack_known *skp = smk_find_entry(secdata);
4441 *secid = skp->smk_secid;
4448 * There used to be a smack_release_secctx hook
4449 * that did nothing back when hooks were in a vector.
4450 * Now that there's a list such a hook adds cost.
4453 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4455 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4458 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4460 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4463 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4465 struct smack_known *skp = smk_of_inode(inode);
4467 *ctx = skp->smk_known;
4468 *ctxlen = strlen(skp->smk_known);
4472 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4475 struct task_smack *tsp;
4476 struct smack_known *skp;
4477 struct inode_smack *isp;
4478 struct cred *new_creds = *new;
4480 if (new_creds == NULL) {
4481 new_creds = prepare_creds();
4482 if (new_creds == NULL)
4486 tsp = smack_cred(new_creds);
4489 * Get label from overlay inode and set it in create_sid
4491 isp = d_inode(dentry->d_parent)->i_security;
4492 skp = isp->smk_inode;
4493 tsp->smk_task = skp;
4498 static int smack_inode_copy_up_xattr(const char *name)
4501 * Return 1 if this is the smack access Smack attribute.
4503 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4509 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4511 const struct cred *old,
4514 struct task_smack *otsp = smack_cred(old);
4515 struct task_smack *ntsp = smack_cred(new);
4516 struct inode_smack *isp;
4520 * Use the process credential unless all of
4521 * the transmuting criteria are met
4523 ntsp->smk_task = otsp->smk_task;
4526 * the attribute of the containing directory
4528 isp = d_inode(dentry->d_parent)->i_security;
4530 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4532 may = smk_access_entry(otsp->smk_task->smk_known,
4533 isp->smk_inode->smk_known,
4534 &otsp->smk_task->smk_rules);
4538 * If the directory is transmuting and the rule
4539 * providing access is transmuting use the containing
4540 * directory label instead of the process label.
4542 if (may > 0 && (may & MAY_TRANSMUTE))
4543 ntsp->smk_task = isp->smk_inode;
4548 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4549 .lbs_cred = sizeof(struct task_smack),
4550 .lbs_file = sizeof(struct smack_known *),
4553 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4554 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4555 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4556 LSM_HOOK_INIT(syslog, smack_syslog),
4558 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4559 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4560 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4561 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4562 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4563 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4565 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4567 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4568 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4569 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4570 LSM_HOOK_INIT(inode_link, smack_inode_link),
4571 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4572 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4573 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4574 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4575 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4576 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4577 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4578 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4579 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4580 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4581 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4582 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4583 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4584 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4586 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4587 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4588 LSM_HOOK_INIT(file_lock, smack_file_lock),
4589 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4590 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4591 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4592 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4593 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4594 LSM_HOOK_INIT(file_receive, smack_file_receive),
4596 LSM_HOOK_INIT(file_open, smack_file_open),
4598 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4599 LSM_HOOK_INIT(cred_free, smack_cred_free),
4600 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4601 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4602 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4603 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4604 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4605 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4606 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4607 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4608 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4609 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4610 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4611 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4612 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4613 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4614 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4615 LSM_HOOK_INIT(task_kill, smack_task_kill),
4616 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4618 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4619 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4621 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4622 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4624 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4625 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
4626 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4627 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4628 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4629 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4631 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4632 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
4633 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4634 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4635 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4637 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4638 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
4639 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4640 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4641 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4643 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4645 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4646 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4648 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4649 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4651 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4652 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4653 #ifdef SMACK_IPV6_PORT_LABELING
4654 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4656 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4657 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4658 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4659 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4660 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4661 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4662 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4663 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4664 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4665 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4667 /* key management security hooks */
4669 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4670 LSM_HOOK_INIT(key_free, smack_key_free),
4671 LSM_HOOK_INIT(key_permission, smack_key_permission),
4672 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4673 #endif /* CONFIG_KEYS */
4677 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4678 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4679 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4680 #endif /* CONFIG_AUDIT */
4682 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4683 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4684 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4685 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4686 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4687 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4688 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4689 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4690 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4694 static __init void init_smack_known_list(void)
4697 * Initialize rule list locks
4699 mutex_init(&smack_known_huh.smk_rules_lock);
4700 mutex_init(&smack_known_hat.smk_rules_lock);
4701 mutex_init(&smack_known_floor.smk_rules_lock);
4702 mutex_init(&smack_known_star.smk_rules_lock);
4703 mutex_init(&smack_known_web.smk_rules_lock);
4705 * Initialize rule lists
4707 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4708 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4709 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4710 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4711 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4713 * Create the known labels list
4715 smk_insert_entry(&smack_known_huh);
4716 smk_insert_entry(&smack_known_hat);
4717 smk_insert_entry(&smack_known_star);
4718 smk_insert_entry(&smack_known_floor);
4719 smk_insert_entry(&smack_known_web);
4723 * smack_init - initialize the smack system
4727 static __init int smack_init(void)
4729 struct cred *cred = (struct cred *) current->cred;
4730 struct task_smack *tsp;
4732 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4733 if (!smack_inode_cache)
4736 lsm_early_cred(cred);
4739 * Set the security state for the initial task.
4741 tsp = smack_cred(cred);
4742 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4747 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4750 pr_info("Smack: Initializing.\n");
4751 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4752 pr_info("Smack: Netfilter enabled.\n");
4754 #ifdef SMACK_IPV6_PORT_LABELING
4755 pr_info("Smack: IPv6 port labeling enabled.\n");
4757 #ifdef SMACK_IPV6_SECMARK_LABELING
4758 pr_info("Smack: IPv6 Netfilter enabled.\n");
4761 /* initialize the smack_known_list */
4762 init_smack_known_list();
4768 * Smack requires early initialization in order to label
4769 * all processes and objects when they are created.
4771 DEFINE_LSM(smack) = {
4773 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4774 .blobs = &smack_blob_sizes,