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_free_security - clear a file security blob
1502 * The security blob for a file is a pointer to the master
1503 * label list, so no memory is freed.
1505 static void smack_file_free_security(struct file *file)
1507 file->f_security = NULL;
1511 * smack_file_ioctl - Smack check on ioctls
1516 * Relies heavily on the correct use of the ioctl command conventions.
1518 * Returns 0 if allowed, error code otherwise
1520 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1524 struct smk_audit_info ad;
1525 struct inode *inode = file_inode(file);
1527 if (unlikely(IS_PRIVATE(inode)))
1530 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1531 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1533 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1534 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1535 rc = smk_bu_file(file, MAY_WRITE, rc);
1538 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1539 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1540 rc = smk_bu_file(file, MAY_READ, rc);
1547 * smack_file_lock - Smack check on file locking
1551 * Returns 0 if current has lock access, error code otherwise
1553 static int smack_file_lock(struct file *file, unsigned int cmd)
1555 struct smk_audit_info ad;
1557 struct inode *inode = file_inode(file);
1559 if (unlikely(IS_PRIVATE(inode)))
1562 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1563 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1564 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1565 rc = smk_bu_file(file, MAY_LOCK, rc);
1570 * smack_file_fcntl - Smack check on fcntl
1572 * @cmd: what action to check
1575 * Generally these operations are harmless.
1576 * File locking operations present an obvious mechanism
1577 * for passing information, so they require write access.
1579 * Returns 0 if current has access, error code otherwise
1581 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1584 struct smk_audit_info ad;
1586 struct inode *inode = file_inode(file);
1588 if (unlikely(IS_PRIVATE(inode)))
1596 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1597 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1598 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1599 rc = smk_bu_file(file, MAY_LOCK, rc);
1603 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1604 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1605 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1606 rc = smk_bu_file(file, MAY_WRITE, rc);
1617 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1618 * if mapping anonymous memory.
1619 * @file contains the file structure for file to map (may be NULL).
1620 * @reqprot contains the protection requested by the application.
1621 * @prot contains the protection that will be applied by the kernel.
1622 * @flags contains the operational flags.
1623 * Return 0 if permission is granted.
1625 static int smack_mmap_file(struct file *file,
1626 unsigned long reqprot, unsigned long prot,
1627 unsigned long flags)
1629 struct smack_known *skp;
1630 struct smack_known *mkp;
1631 struct smack_rule *srp;
1632 struct task_smack *tsp;
1633 struct smack_known *okp;
1634 struct inode_smack *isp;
1635 struct superblock_smack *sbsp;
1644 if (unlikely(IS_PRIVATE(file_inode(file))))
1647 isp = file_inode(file)->i_security;
1648 if (isp->smk_mmap == NULL)
1650 sbsp = file_inode(file)->i_sb->s_security;
1651 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1652 isp->smk_mmap != sbsp->smk_root)
1654 mkp = isp->smk_mmap;
1656 tsp = smack_cred(current_cred());
1657 skp = smk_of_current();
1662 * For each Smack rule associated with the subject
1663 * label verify that the SMACK64MMAP also has access
1664 * to that rule's object label.
1666 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1667 okp = srp->smk_object;
1669 * Matching labels always allows access.
1671 if (mkp->smk_known == okp->smk_known)
1674 * If there is a matching local rule take
1675 * that into account as well.
1677 may = smk_access_entry(srp->smk_subject->smk_known,
1681 may = srp->smk_access;
1683 may &= srp->smk_access;
1685 * If may is zero the SMACK64MMAP subject can't
1686 * possibly have less access.
1692 * Fetch the global list entry.
1693 * If there isn't one a SMACK64MMAP subject
1694 * can't have as much access as current.
1696 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1698 if (mmay == -ENOENT) {
1703 * If there is a local entry it modifies the
1704 * potential access, too.
1706 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1708 if (tmay != -ENOENT)
1712 * If there is any access available to current that is
1713 * not available to a SMACK64MMAP subject
1716 if ((may | mmay) != mmay) {
1728 * smack_file_set_fowner - set the file security blob value
1729 * @file: object in question
1732 static void smack_file_set_fowner(struct file *file)
1734 struct smack_known **blob = smack_file(file);
1736 *blob = smk_of_current();
1740 * smack_file_send_sigiotask - Smack on sigio
1741 * @tsk: The target task
1742 * @fown: the object the signal come from
1745 * Allow a privileged task to get signals even if it shouldn't
1747 * Returns 0 if a subject with the object's smack could
1748 * write to the task, an error code otherwise.
1750 static int smack_file_send_sigiotask(struct task_struct *tsk,
1751 struct fown_struct *fown, int signum)
1753 struct smack_known **blob;
1754 struct smack_known *skp;
1755 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1756 const struct cred *tcred;
1759 struct smk_audit_info ad;
1762 * struct fown_struct is never outside the context of a struct file
1764 file = container_of(fown, struct file, f_owner);
1766 /* we don't log here as rc can be overriden */
1767 blob = smack_file(file);
1769 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1770 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1773 tcred = __task_cred(tsk);
1774 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1778 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1779 smk_ad_setfield_u_tsk(&ad, tsk);
1780 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1785 * smack_file_receive - Smack file receive check
1788 * Returns 0 if current has access, error code otherwise
1790 static int smack_file_receive(struct file *file)
1794 struct smk_audit_info ad;
1795 struct inode *inode = file_inode(file);
1796 struct socket *sock;
1797 struct task_smack *tsp;
1798 struct socket_smack *ssp;
1800 if (unlikely(IS_PRIVATE(inode)))
1803 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1804 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1806 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1807 sock = SOCKET_I(inode);
1808 ssp = sock->sk->sk_security;
1809 tsp = smack_cred(current_cred());
1811 * If the receiving process can't write to the
1812 * passed socket or if the passed socket can't
1813 * write to the receiving process don't accept
1814 * the passed socket.
1816 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1817 rc = smk_bu_file(file, may, rc);
1820 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1821 rc = smk_bu_file(file, may, rc);
1825 * This code relies on bitmasks.
1827 if (file->f_mode & FMODE_READ)
1829 if (file->f_mode & FMODE_WRITE)
1832 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1833 rc = smk_bu_file(file, may, rc);
1838 * smack_file_open - Smack dentry open processing
1840 * @cred: task credential
1842 * Set the security blob in the file structure.
1843 * Allow the open only if the task has read access. There are
1844 * many read operations (e.g. fstat) that you can do with an
1845 * fd even if you have the file open write-only.
1849 static int smack_file_open(struct file *file)
1851 struct task_smack *tsp = smack_cred(file->f_cred);
1852 struct inode *inode = file_inode(file);
1853 struct smk_audit_info ad;
1856 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1857 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1858 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1859 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1869 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1870 * @new: the new credentials
1871 * @gfp: the atomicity of any memory allocations
1873 * Prepare a blank set of credentials for modification. This must allocate all
1874 * the memory the LSM module might require such that cred_transfer() can
1875 * complete without error.
1877 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1879 init_task_smack(smack_cred(cred), NULL, NULL);
1885 * smack_cred_free - "free" task-level security credentials
1886 * @cred: the credentials in question
1889 static void smack_cred_free(struct cred *cred)
1891 struct task_smack *tsp = smack_cred(cred);
1892 struct smack_rule *rp;
1893 struct list_head *l;
1894 struct list_head *n;
1896 smk_destroy_label_list(&tsp->smk_relabel);
1898 list_for_each_safe(l, n, &tsp->smk_rules) {
1899 rp = list_entry(l, struct smack_rule, list);
1900 list_del(&rp->list);
1906 * smack_cred_prepare - prepare new set of credentials for modification
1907 * @new: the new credentials
1908 * @old: the original credentials
1909 * @gfp: the atomicity of any memory allocations
1911 * Prepare a new set of credentials for modification.
1913 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1916 struct task_smack *old_tsp = smack_cred(old);
1917 struct task_smack *new_tsp = smack_cred(new);
1920 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1922 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1926 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1932 * smack_cred_transfer - Transfer the old credentials to the new credentials
1933 * @new: the new credentials
1934 * @old: the original credentials
1936 * Fill in a set of blank credentials from another set of credentials.
1938 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1940 struct task_smack *old_tsp = smack_cred(old);
1941 struct task_smack *new_tsp = smack_cred(new);
1943 new_tsp->smk_task = old_tsp->smk_task;
1944 new_tsp->smk_forked = old_tsp->smk_task;
1945 mutex_init(&new_tsp->smk_rules_lock);
1946 INIT_LIST_HEAD(&new_tsp->smk_rules);
1948 /* cbs copy rule list */
1952 * smack_cred_getsecid - get the secid corresponding to a creds structure
1953 * @c: the object creds
1954 * @secid: where to put the result
1956 * Sets the secid to contain a u32 version of the smack label.
1958 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1960 struct smack_known *skp;
1963 skp = smk_of_task(smack_cred(cred));
1964 *secid = skp->smk_secid;
1969 * smack_kernel_act_as - Set the subjective context in a set of credentials
1970 * @new: points to the set of credentials to be modified.
1971 * @secid: specifies the security ID to be set
1973 * Set the security data for a kernel service.
1975 static int smack_kernel_act_as(struct cred *new, u32 secid)
1977 struct task_smack *new_tsp = smack_cred(new);
1979 new_tsp->smk_task = smack_from_secid(secid);
1984 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1985 * @new: points to the set of credentials to be modified
1986 * @inode: points to the inode to use as a reference
1988 * Set the file creation context in a set of credentials to the same
1989 * as the objective context of the specified inode
1991 static int smack_kernel_create_files_as(struct cred *new,
1992 struct inode *inode)
1994 struct inode_smack *isp = inode->i_security;
1995 struct task_smack *tsp = smack_cred(new);
1997 tsp->smk_forked = isp->smk_inode;
1998 tsp->smk_task = tsp->smk_forked;
2003 * smk_curacc_on_task - helper to log task related access
2004 * @p: the task object
2005 * @access: the access requested
2006 * @caller: name of the calling function for audit
2008 * Return 0 if access is permitted
2010 static int smk_curacc_on_task(struct task_struct *p, int access,
2013 struct smk_audit_info ad;
2014 struct smack_known *skp = smk_of_task_struct(p);
2017 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2018 smk_ad_setfield_u_tsk(&ad, p);
2019 rc = smk_curacc(skp, access, &ad);
2020 rc = smk_bu_task(p, access, rc);
2025 * smack_task_setpgid - Smack check on setting pgid
2026 * @p: the task object
2029 * Return 0 if write access is permitted
2031 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2033 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2037 * smack_task_getpgid - Smack access check for getpgid
2038 * @p: the object task
2040 * Returns 0 if current can read the object task, error code otherwise
2042 static int smack_task_getpgid(struct task_struct *p)
2044 return smk_curacc_on_task(p, MAY_READ, __func__);
2048 * smack_task_getsid - Smack access check for getsid
2049 * @p: the object task
2051 * Returns 0 if current can read the object task, error code otherwise
2053 static int smack_task_getsid(struct task_struct *p)
2055 return smk_curacc_on_task(p, MAY_READ, __func__);
2059 * smack_task_getsecid - get the secid of the task
2060 * @p: the object task
2061 * @secid: where to put the result
2063 * Sets the secid to contain a u32 version of the smack label.
2065 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2067 struct smack_known *skp = smk_of_task_struct(p);
2069 *secid = skp->smk_secid;
2073 * smack_task_setnice - Smack check on setting nice
2074 * @p: the task object
2077 * Return 0 if write access is permitted
2079 static int smack_task_setnice(struct task_struct *p, int nice)
2081 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2085 * smack_task_setioprio - Smack check on setting ioprio
2086 * @p: the task object
2089 * Return 0 if write access is permitted
2091 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2093 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2097 * smack_task_getioprio - Smack check on reading ioprio
2098 * @p: the task object
2100 * Return 0 if read access is permitted
2102 static int smack_task_getioprio(struct task_struct *p)
2104 return smk_curacc_on_task(p, MAY_READ, __func__);
2108 * smack_task_setscheduler - Smack check on setting scheduler
2109 * @p: the task object
2113 * Return 0 if read access is permitted
2115 static int smack_task_setscheduler(struct task_struct *p)
2117 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2121 * smack_task_getscheduler - Smack check on reading scheduler
2122 * @p: the task object
2124 * Return 0 if read access is permitted
2126 static int smack_task_getscheduler(struct task_struct *p)
2128 return smk_curacc_on_task(p, MAY_READ, __func__);
2132 * smack_task_movememory - Smack check on moving memory
2133 * @p: the task object
2135 * Return 0 if write access is permitted
2137 static int smack_task_movememory(struct task_struct *p)
2139 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2143 * smack_task_kill - Smack check on signal delivery
2144 * @p: the task object
2147 * @cred: identifies the cred to use in lieu of current's
2149 * Return 0 if write access is permitted
2152 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2153 int sig, const struct cred *cred)
2155 struct smk_audit_info ad;
2156 struct smack_known *skp;
2157 struct smack_known *tkp = smk_of_task_struct(p);
2161 return 0; /* null signal; existence test */
2163 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2164 smk_ad_setfield_u_tsk(&ad, p);
2166 * Sending a signal requires that the sender
2167 * can write the receiver.
2170 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2171 rc = smk_bu_task(p, MAY_DELIVER, rc);
2175 * If the cred isn't NULL we're dealing with some USB IO
2176 * specific behavior. This is not clean. For one thing
2177 * we can't take privilege into account.
2179 skp = smk_of_task(smack_cred(cred));
2180 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2181 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2186 * smack_task_to_inode - copy task smack into the inode blob
2187 * @p: task to copy from
2188 * @inode: inode to copy to
2190 * Sets the smack pointer in the inode security blob
2192 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2194 struct inode_smack *isp = inode->i_security;
2195 struct smack_known *skp = smk_of_task_struct(p);
2197 isp->smk_inode = skp;
2198 isp->smk_flags |= SMK_INODE_INSTANT;
2206 * smack_sk_alloc_security - Allocate a socket blob
2209 * @gfp_flags: memory allocation flags
2211 * Assign Smack pointers to current
2213 * Returns 0 on success, -ENOMEM is there's no memory
2215 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2217 struct smack_known *skp = smk_of_current();
2218 struct socket_smack *ssp;
2220 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2225 * Sockets created by kernel threads receive web label.
2227 if (unlikely(current->flags & PF_KTHREAD)) {
2228 ssp->smk_in = &smack_known_web;
2229 ssp->smk_out = &smack_known_web;
2234 ssp->smk_packet = NULL;
2236 sk->sk_security = ssp;
2242 * smack_sk_free_security - Free a socket blob
2245 * Clears the blob pointer
2247 static void smack_sk_free_security(struct sock *sk)
2249 #ifdef SMACK_IPV6_PORT_LABELING
2250 struct smk_port_label *spp;
2252 if (sk->sk_family == PF_INET6) {
2254 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2255 if (spp->smk_sock != sk)
2257 spp->smk_can_reuse = 1;
2263 kfree(sk->sk_security);
2267 * smack_ipv4host_label - check host based restrictions
2268 * @sip: the object end
2270 * looks for host based access restrictions
2272 * This version will only be appropriate for really small sets of single label
2273 * hosts. The caller is responsible for ensuring that the RCU read lock is
2274 * taken before calling this function.
2276 * Returns the label of the far end or NULL if it's not special.
2278 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2280 struct smk_net4addr *snp;
2281 struct in_addr *siap = &sip->sin_addr;
2283 if (siap->s_addr == 0)
2286 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2288 * we break after finding the first match because
2289 * the list is sorted from longest to shortest mask
2290 * so we have found the most specific match
2292 if (snp->smk_host.s_addr ==
2293 (siap->s_addr & snp->smk_mask.s_addr))
2294 return snp->smk_label;
2299 #if IS_ENABLED(CONFIG_IPV6)
2301 * smk_ipv6_localhost - Check for local ipv6 host address
2304 * Returns boolean true if this is the localhost address
2306 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2308 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2309 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2311 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2312 ntohs(be16p[7]) == 1)
2318 * smack_ipv6host_label - check host based restrictions
2319 * @sip: the object end
2321 * looks for host based access restrictions
2323 * This version will only be appropriate for really small sets of single label
2324 * hosts. The caller is responsible for ensuring that the RCU read lock is
2325 * taken before calling this function.
2327 * Returns the label of the far end or NULL if it's not special.
2329 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2331 struct smk_net6addr *snp;
2332 struct in6_addr *sap = &sip->sin6_addr;
2337 * It's local. Don't look for a host label.
2339 if (smk_ipv6_localhost(sip))
2342 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2344 * If the label is NULL the entry has
2345 * been renounced. Ignore it.
2347 if (snp->smk_label == NULL)
2350 * we break after finding the first match because
2351 * the list is sorted from longest to shortest mask
2352 * so we have found the most specific match
2354 for (found = 1, i = 0; i < 8; i++) {
2355 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2356 snp->smk_host.s6_addr16[i]) {
2362 return snp->smk_label;
2367 #endif /* CONFIG_IPV6 */
2370 * smack_netlabel - Set the secattr on a socket
2372 * @labeled: socket label scheme
2374 * Convert the outbound smack value (smk_out) to a
2375 * secattr and attach it to the socket.
2377 * Returns 0 on success or an error code
2379 static int smack_netlabel(struct sock *sk, int labeled)
2381 struct smack_known *skp;
2382 struct socket_smack *ssp = sk->sk_security;
2386 * Usually the netlabel code will handle changing the
2387 * packet labeling based on the label.
2388 * The case of a single label host is different, because
2389 * a single label host should never get a labeled packet
2390 * even though the label is usually associated with a packet
2394 bh_lock_sock_nested(sk);
2396 if (ssp->smk_out == smack_net_ambient ||
2397 labeled == SMACK_UNLABELED_SOCKET)
2398 netlbl_sock_delattr(sk);
2401 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2411 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2413 * @sap: the destination address
2415 * Set the correct secattr for the given socket based on the destination
2416 * address and perform any outbound access checks needed.
2418 * Returns 0 on success or an error code.
2421 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2423 struct smack_known *skp;
2426 struct smack_known *hkp;
2427 struct socket_smack *ssp = sk->sk_security;
2428 struct smk_audit_info ad;
2431 hkp = smack_ipv4host_label(sap);
2434 struct lsm_network_audit net;
2436 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2437 ad.a.u.net->family = sap->sin_family;
2438 ad.a.u.net->dport = sap->sin_port;
2439 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2441 sk_lbl = SMACK_UNLABELED_SOCKET;
2443 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2444 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2446 sk_lbl = SMACK_CIPSO_SOCKET;
2453 return smack_netlabel(sk, sk_lbl);
2456 #if IS_ENABLED(CONFIG_IPV6)
2458 * smk_ipv6_check - check Smack access
2459 * @subject: subject Smack label
2460 * @object: object Smack label
2462 * @act: the action being taken
2464 * Check an IPv6 access
2466 static int smk_ipv6_check(struct smack_known *subject,
2467 struct smack_known *object,
2468 struct sockaddr_in6 *address, int act)
2471 struct lsm_network_audit net;
2473 struct smk_audit_info ad;
2477 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2478 ad.a.u.net->family = PF_INET6;
2479 ad.a.u.net->dport = ntohs(address->sin6_port);
2480 if (act == SMK_RECEIVING)
2481 ad.a.u.net->v6info.saddr = address->sin6_addr;
2483 ad.a.u.net->v6info.daddr = address->sin6_addr;
2485 rc = smk_access(subject, object, MAY_WRITE, &ad);
2486 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2489 #endif /* CONFIG_IPV6 */
2491 #ifdef SMACK_IPV6_PORT_LABELING
2493 * smk_ipv6_port_label - Smack port access table management
2497 * Create or update the port list entry
2499 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2501 struct sock *sk = sock->sk;
2502 struct sockaddr_in6 *addr6;
2503 struct socket_smack *ssp = sock->sk->sk_security;
2504 struct smk_port_label *spp;
2505 unsigned short port = 0;
2507 if (address == NULL) {
2509 * This operation is changing the Smack information
2510 * on the bound socket. Take the changes to the port
2514 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2515 if (sk != spp->smk_sock)
2517 spp->smk_in = ssp->smk_in;
2518 spp->smk_out = ssp->smk_out;
2523 * A NULL address is only used for updating existing
2524 * bound entries. If there isn't one, it's OK.
2530 addr6 = (struct sockaddr_in6 *)address;
2531 port = ntohs(addr6->sin6_port);
2533 * This is a special case that is safely ignored.
2539 * Look for an existing port list entry.
2540 * This is an indication that a port is getting reused.
2543 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2544 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2546 if (spp->smk_can_reuse != 1) {
2550 spp->smk_port = port;
2552 spp->smk_in = ssp->smk_in;
2553 spp->smk_out = ssp->smk_out;
2554 spp->smk_can_reuse = 0;
2560 * A new port entry is required.
2562 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2566 spp->smk_port = port;
2568 spp->smk_in = ssp->smk_in;
2569 spp->smk_out = ssp->smk_out;
2570 spp->smk_sock_type = sock->type;
2571 spp->smk_can_reuse = 0;
2573 mutex_lock(&smack_ipv6_lock);
2574 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2575 mutex_unlock(&smack_ipv6_lock);
2580 * smk_ipv6_port_check - check Smack port access
2584 * Create or update the port list entry
2586 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2589 struct smk_port_label *spp;
2590 struct socket_smack *ssp = sk->sk_security;
2591 struct smack_known *skp = NULL;
2592 unsigned short port;
2593 struct smack_known *object;
2595 if (act == SMK_RECEIVING) {
2596 skp = smack_ipv6host_label(address);
2597 object = ssp->smk_in;
2600 object = smack_ipv6host_label(address);
2604 * The other end is a single label host.
2606 if (skp != NULL && object != NULL)
2607 return smk_ipv6_check(skp, object, address, act);
2609 skp = smack_net_ambient;
2611 object = smack_net_ambient;
2614 * It's remote, so port lookup does no good.
2616 if (!smk_ipv6_localhost(address))
2617 return smk_ipv6_check(skp, object, address, act);
2620 * It's local so the send check has to have passed.
2622 if (act == SMK_RECEIVING)
2625 port = ntohs(address->sin6_port);
2627 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2628 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2630 object = spp->smk_in;
2631 if (act == SMK_CONNECTING)
2632 ssp->smk_packet = spp->smk_out;
2637 return smk_ipv6_check(skp, object, address, act);
2639 #endif /* SMACK_IPV6_PORT_LABELING */
2642 * smack_inode_setsecurity - set smack xattrs
2643 * @inode: the object
2644 * @name: attribute name
2645 * @value: attribute value
2646 * @size: size of the attribute
2649 * Sets the named attribute in the appropriate blob
2651 * Returns 0 on success, or an error code
2653 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2654 const void *value, size_t size, int flags)
2656 struct smack_known *skp;
2657 struct inode_smack *nsp = inode->i_security;
2658 struct socket_smack *ssp;
2659 struct socket *sock;
2662 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2665 skp = smk_import_entry(value, size);
2667 return PTR_ERR(skp);
2669 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2670 nsp->smk_inode = skp;
2671 nsp->smk_flags |= SMK_INODE_INSTANT;
2675 * The rest of the Smack xattrs are only on sockets.
2677 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2680 sock = SOCKET_I(inode);
2681 if (sock == NULL || sock->sk == NULL)
2684 ssp = sock->sk->sk_security;
2686 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2688 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2690 if (sock->sk->sk_family == PF_INET) {
2691 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2694 "Smack: \"%s\" netlbl error %d.\n",
2700 #ifdef SMACK_IPV6_PORT_LABELING
2701 if (sock->sk->sk_family == PF_INET6)
2702 smk_ipv6_port_label(sock, NULL);
2709 * smack_socket_post_create - finish socket setup
2711 * @family: protocol family
2716 * Sets the netlabel information on the socket
2718 * Returns 0 on success, and error code otherwise
2720 static int smack_socket_post_create(struct socket *sock, int family,
2721 int type, int protocol, int kern)
2723 struct socket_smack *ssp;
2725 if (sock->sk == NULL)
2729 * Sockets created by kernel threads receive web label.
2731 if (unlikely(current->flags & PF_KTHREAD)) {
2732 ssp = sock->sk->sk_security;
2733 ssp->smk_in = &smack_known_web;
2734 ssp->smk_out = &smack_known_web;
2737 if (family != PF_INET)
2740 * Set the outbound netlbl.
2742 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2746 * smack_socket_socketpair - create socket pair
2747 * @socka: one socket
2748 * @sockb: another socket
2750 * Cross reference the peer labels for SO_PEERSEC
2752 * Returns 0 on success, and error code otherwise
2754 static int smack_socket_socketpair(struct socket *socka,
2755 struct socket *sockb)
2757 struct socket_smack *asp = socka->sk->sk_security;
2758 struct socket_smack *bsp = sockb->sk->sk_security;
2760 asp->smk_packet = bsp->smk_out;
2761 bsp->smk_packet = asp->smk_out;
2766 #ifdef SMACK_IPV6_PORT_LABELING
2768 * smack_socket_bind - record port binding information.
2770 * @address: the port address
2771 * @addrlen: size of the address
2773 * Records the label bound to a port.
2777 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2780 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2781 smk_ipv6_port_label(sock, address);
2784 #endif /* SMACK_IPV6_PORT_LABELING */
2787 * smack_socket_connect - connect access check
2789 * @sap: the other end
2790 * @addrlen: size of sap
2792 * Verifies that a connection may be possible
2794 * Returns 0 on success, and error code otherwise
2796 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2800 #if IS_ENABLED(CONFIG_IPV6)
2801 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2803 #ifdef SMACK_IPV6_SECMARK_LABELING
2804 struct smack_known *rsp;
2805 struct socket_smack *ssp;
2808 if (sock->sk == NULL)
2811 #ifdef SMACK_IPV6_SECMARK_LABELING
2812 ssp = sock->sk->sk_security;
2815 switch (sock->sk->sk_family) {
2817 if (addrlen < sizeof(struct sockaddr_in))
2819 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2822 if (addrlen < sizeof(struct sockaddr_in6))
2824 #ifdef SMACK_IPV6_SECMARK_LABELING
2825 rsp = smack_ipv6host_label(sip);
2827 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2830 #ifdef SMACK_IPV6_PORT_LABELING
2831 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2839 * smack_flags_to_may - convert S_ to MAY_ values
2840 * @flags: the S_ value
2842 * Returns the equivalent MAY_ value
2844 static int smack_flags_to_may(int flags)
2848 if (flags & S_IRUGO)
2850 if (flags & S_IWUGO)
2852 if (flags & S_IXUGO)
2859 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2864 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2866 struct smack_known *skp = smk_of_current();
2868 msg->security = skp;
2873 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2876 * Clears the blob pointer
2878 static void smack_msg_msg_free_security(struct msg_msg *msg)
2880 msg->security = NULL;
2884 * smack_of_ipc - the smack pointer for the ipc
2887 * Returns a pointer to the smack value
2889 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2891 return (struct smack_known *)isp->security;
2895 * smack_ipc_alloc_security - Set the security blob for ipc
2900 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2902 struct smack_known *skp = smk_of_current();
2904 isp->security = skp;
2909 * smack_ipc_free_security - Clear the security blob for ipc
2912 * Clears the blob pointer
2914 static void smack_ipc_free_security(struct kern_ipc_perm *isp)
2916 isp->security = NULL;
2920 * smk_curacc_shm : check if current has access on shm
2922 * @access : access requested
2924 * Returns 0 if current has the requested access, error code otherwise
2926 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2928 struct smack_known *ssp = smack_of_ipc(isp);
2929 struct smk_audit_info ad;
2933 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2934 ad.a.u.ipc_id = isp->id;
2936 rc = smk_curacc(ssp, access, &ad);
2937 rc = smk_bu_current("shm", ssp, access, rc);
2942 * smack_shm_associate - Smack access check for shm
2944 * @shmflg: access requested
2946 * Returns 0 if current has the requested access, error code otherwise
2948 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2952 may = smack_flags_to_may(shmflg);
2953 return smk_curacc_shm(isp, may);
2957 * smack_shm_shmctl - Smack access check for shm
2959 * @cmd: what it wants to do
2961 * Returns 0 if current has the requested access, error code otherwise
2963 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2977 may = MAY_READWRITE;
2982 * System level information.
2988 return smk_curacc_shm(isp, may);
2992 * smack_shm_shmat - Smack access for shmat
2995 * @shmflg: access requested
2997 * Returns 0 if current has the requested access, error code otherwise
2999 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3004 may = smack_flags_to_may(shmflg);
3005 return smk_curacc_shm(ipc, may);
3009 * smk_curacc_sem : check if current has access on sem
3011 * @access : access requested
3013 * Returns 0 if current has the requested access, error code otherwise
3015 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3017 struct smack_known *ssp = smack_of_ipc(isp);
3018 struct smk_audit_info ad;
3022 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3023 ad.a.u.ipc_id = isp->id;
3025 rc = smk_curacc(ssp, access, &ad);
3026 rc = smk_bu_current("sem", ssp, access, rc);
3031 * smack_sem_associate - Smack access check for sem
3033 * @semflg: access requested
3035 * Returns 0 if current has the requested access, error code otherwise
3037 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3041 may = smack_flags_to_may(semflg);
3042 return smk_curacc_sem(isp, may);
3046 * smack_sem_shmctl - Smack access check for sem
3048 * @cmd: what it wants to do
3050 * Returns 0 if current has the requested access, error code otherwise
3052 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3071 may = MAY_READWRITE;
3076 * System level information
3083 return smk_curacc_sem(isp, may);
3087 * smack_sem_semop - Smack checks of semaphore operations
3093 * Treated as read and write in all cases.
3095 * Returns 0 if access is allowed, error code otherwise
3097 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3098 unsigned nsops, int alter)
3100 return smk_curacc_sem(isp, MAY_READWRITE);
3104 * smk_curacc_msq : helper to check if current has access on msq
3106 * @access : access requested
3108 * return 0 if current has access, error otherwise
3110 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3112 struct smack_known *msp = smack_of_ipc(isp);
3113 struct smk_audit_info ad;
3117 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3118 ad.a.u.ipc_id = isp->id;
3120 rc = smk_curacc(msp, access, &ad);
3121 rc = smk_bu_current("msq", msp, access, rc);
3126 * smack_msg_queue_associate - Smack access check for msg_queue
3128 * @msqflg: access requested
3130 * Returns 0 if current has the requested access, error code otherwise
3132 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3136 may = smack_flags_to_may(msqflg);
3137 return smk_curacc_msq(isp, may);
3141 * smack_msg_queue_msgctl - Smack access check for msg_queue
3143 * @cmd: what it wants to do
3145 * Returns 0 if current has the requested access, error code otherwise
3147 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3159 may = MAY_READWRITE;
3164 * System level information
3171 return smk_curacc_msq(isp, may);
3175 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3178 * @msqflg: access requested
3180 * Returns 0 if current has the requested access, error code otherwise
3182 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3187 may = smack_flags_to_may(msqflg);
3188 return smk_curacc_msq(isp, may);
3192 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3199 * Returns 0 if current has read and write access, error code otherwise
3201 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3202 struct task_struct *target, long type, int mode)
3204 return smk_curacc_msq(isp, MAY_READWRITE);
3208 * smack_ipc_permission - Smack access for ipc_permission()
3209 * @ipp: the object permissions
3210 * @flag: access requested
3212 * Returns 0 if current has read and write access, error code otherwise
3214 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3216 struct smack_known *iskp = ipp->security;
3217 int may = smack_flags_to_may(flag);
3218 struct smk_audit_info ad;
3222 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3223 ad.a.u.ipc_id = ipp->id;
3225 rc = smk_curacc(iskp, may, &ad);
3226 rc = smk_bu_current("svipc", iskp, may, rc);
3231 * smack_ipc_getsecid - Extract smack security id
3232 * @ipp: the object permissions
3233 * @secid: where result will be saved
3235 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3237 struct smack_known *iskp = ipp->security;
3239 *secid = iskp->smk_secid;
3243 * smack_d_instantiate - Make sure the blob is correct on an inode
3244 * @opt_dentry: dentry where inode will be attached
3245 * @inode: the object
3247 * Set the inode's security blob if it hasn't been done already.
3249 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3251 struct super_block *sbp;
3252 struct superblock_smack *sbsp;
3253 struct inode_smack *isp;
3254 struct smack_known *skp;
3255 struct smack_known *ckp = smk_of_current();
3256 struct smack_known *final;
3257 char trattr[TRANS_TRUE_SIZE];
3265 isp = inode->i_security;
3267 mutex_lock(&isp->smk_lock);
3269 * If the inode is already instantiated
3270 * take the quick way out
3272 if (isp->smk_flags & SMK_INODE_INSTANT)
3276 sbsp = sbp->s_security;
3278 * We're going to use the superblock default label
3279 * if there's no label on the file.
3281 final = sbsp->smk_default;
3284 * If this is the root inode the superblock
3285 * may be in the process of initialization.
3286 * If that is the case use the root value out
3287 * of the superblock.
3289 if (opt_dentry->d_parent == opt_dentry) {
3290 switch (sbp->s_magic) {
3291 case CGROUP_SUPER_MAGIC:
3292 case CGROUP2_SUPER_MAGIC:
3294 * The cgroup filesystem is never mounted,
3295 * so there's no opportunity to set the mount
3298 sbsp->smk_root = &smack_known_star;
3299 sbsp->smk_default = &smack_known_star;
3300 isp->smk_inode = sbsp->smk_root;
3304 * What about shmem/tmpfs anonymous files with dentry
3305 * obtained from d_alloc_pseudo()?
3307 isp->smk_inode = smk_of_current();
3310 isp->smk_inode = smk_of_current();
3314 * Socket access is controlled by the socket
3315 * structures associated with the task involved.
3317 isp->smk_inode = &smack_known_star;
3320 isp->smk_inode = sbsp->smk_root;
3323 isp->smk_flags |= SMK_INODE_INSTANT;
3328 * This is pretty hackish.
3329 * Casey says that we shouldn't have to do
3330 * file system specific code, but it does help
3331 * with keeping it simple.
3333 switch (sbp->s_magic) {
3335 case CGROUP_SUPER_MAGIC:
3336 case CGROUP2_SUPER_MAGIC:
3338 * Casey says that it's a little embarrassing
3339 * that the smack file system doesn't do
3340 * extended attributes.
3342 * Cgroupfs is special
3344 final = &smack_known_star;
3346 case DEVPTS_SUPER_MAGIC:
3348 * devpts seems content with the label of the task.
3349 * Programs that change smack have to treat the
3354 case PROC_SUPER_MAGIC:
3356 * Casey says procfs appears not to care.
3357 * The superblock default suffices.
3362 * Device labels should come from the filesystem,
3363 * but watch out, because they're volitile,
3364 * getting recreated on every reboot.
3366 final = &smack_known_star;
3370 * If a smack value has been set we want to use it,
3371 * but since tmpfs isn't giving us the opportunity
3372 * to set mount options simulate setting the
3373 * superblock default.
3377 * This isn't an understood special case.
3378 * Get the value from the xattr.
3382 * UNIX domain sockets use lower level socket data.
3384 if (S_ISSOCK(inode->i_mode)) {
3385 final = &smack_known_star;
3389 * No xattr support means, alas, no SMACK label.
3390 * Use the aforeapplied default.
3391 * It would be curious if the label of the task
3392 * does not match that assigned.
3394 if (!(inode->i_opflags & IOP_XATTR))
3397 * Get the dentry for xattr.
3399 dp = dget(opt_dentry);
3400 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3401 if (!IS_ERR_OR_NULL(skp))
3405 * Transmuting directory
3407 if (S_ISDIR(inode->i_mode)) {
3409 * If this is a new directory and the label was
3410 * transmuted when the inode was initialized
3411 * set the transmute attribute on the directory
3412 * and mark the inode.
3414 * If there is a transmute attribute on the
3415 * directory mark the inode.
3417 if (isp->smk_flags & SMK_INODE_CHANGED) {
3418 isp->smk_flags &= ~SMK_INODE_CHANGED;
3419 rc = __vfs_setxattr(dp, inode,
3420 XATTR_NAME_SMACKTRANSMUTE,
3421 TRANS_TRUE, TRANS_TRUE_SIZE,
3424 rc = __vfs_getxattr(dp, inode,
3425 XATTR_NAME_SMACKTRANSMUTE, trattr,
3427 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3428 TRANS_TRUE_SIZE) != 0)
3432 transflag = SMK_INODE_TRANSMUTE;
3435 * Don't let the exec or mmap label be "*" or "@".
3437 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3438 if (IS_ERR(skp) || skp == &smack_known_star ||
3439 skp == &smack_known_web)
3441 isp->smk_task = skp;
3443 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3444 if (IS_ERR(skp) || skp == &smack_known_star ||
3445 skp == &smack_known_web)
3447 isp->smk_mmap = skp;
3454 isp->smk_inode = ckp;
3456 isp->smk_inode = final;
3458 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3461 mutex_unlock(&isp->smk_lock);
3466 * smack_getprocattr - Smack process attribute access
3467 * @p: the object task
3468 * @name: the name of the attribute in /proc/.../attr
3469 * @value: where to put the result
3471 * Places a copy of the task Smack into value
3473 * Returns the length of the smack label or an error code
3475 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3477 struct smack_known *skp = smk_of_task_struct(p);
3481 if (strcmp(name, "current") != 0)
3484 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3494 * smack_setprocattr - Smack process attribute setting
3495 * @name: the name of the attribute in /proc/.../attr
3496 * @value: the value to set
3497 * @size: the size of the value
3499 * Sets the Smack value of the task. Only setting self
3500 * is permitted and only with privilege
3502 * Returns the length of the smack label or an error code
3504 static int smack_setprocattr(const char *name, void *value, size_t size)
3506 struct task_smack *tsp = smack_cred(current_cred());
3508 struct smack_known *skp;
3509 struct smack_known_list_elem *sklep;
3512 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3515 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3518 if (strcmp(name, "current") != 0)
3521 skp = smk_import_entry(value, size);
3523 return PTR_ERR(skp);
3526 * No process is ever allowed the web ("@") label
3527 * and the star ("*") label.
3529 if (skp == &smack_known_web || skp == &smack_known_star)
3532 if (!smack_privileged(CAP_MAC_ADMIN)) {
3534 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3535 if (sklep->smk_label == skp) {
3543 new = prepare_creds();
3547 tsp = smack_cred(new);
3548 tsp->smk_task = skp;
3550 * process can change its label only once
3552 smk_destroy_label_list(&tsp->smk_relabel);
3559 * smack_unix_stream_connect - Smack access on UDS
3561 * @other: the other sock
3564 * Return 0 if a subject with the smack of sock could access
3565 * an object with the smack of other, otherwise an error code
3567 static int smack_unix_stream_connect(struct sock *sock,
3568 struct sock *other, struct sock *newsk)
3570 struct smack_known *skp;
3571 struct smack_known *okp;
3572 struct socket_smack *ssp = sock->sk_security;
3573 struct socket_smack *osp = other->sk_security;
3574 struct socket_smack *nsp = newsk->sk_security;
3575 struct smk_audit_info ad;
3578 struct lsm_network_audit net;
3581 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3585 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3586 smk_ad_setfield_u_net_sk(&ad, other);
3588 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3589 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3593 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3594 rc = smk_bu_note("UDS connect", okp, skp,
3600 * Cross reference the peer labels for SO_PEERSEC.
3603 nsp->smk_packet = ssp->smk_out;
3604 ssp->smk_packet = osp->smk_out;
3611 * smack_unix_may_send - Smack access on UDS
3613 * @other: the other socket
3615 * Return 0 if a subject with the smack of sock could access
3616 * an object with the smack of other, otherwise an error code
3618 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3620 struct socket_smack *ssp = sock->sk->sk_security;
3621 struct socket_smack *osp = other->sk->sk_security;
3622 struct smk_audit_info ad;
3626 struct lsm_network_audit net;
3628 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3629 smk_ad_setfield_u_net_sk(&ad, other->sk);
3632 if (smack_privileged(CAP_MAC_OVERRIDE))
3635 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3636 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3641 * smack_socket_sendmsg - Smack check based on destination host
3644 * @size: the size of the message
3646 * Return 0 if the current subject can write to the destination host.
3647 * For IPv4 this is only a question if the destination is a single label host.
3648 * For IPv6 this is a check against the label of the port.
3650 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3653 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3654 #if IS_ENABLED(CONFIG_IPV6)
3655 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3657 #ifdef SMACK_IPV6_SECMARK_LABELING
3658 struct socket_smack *ssp = sock->sk->sk_security;
3659 struct smack_known *rsp;
3664 * Perfectly reasonable for this to be NULL
3669 switch (sock->sk->sk_family) {
3671 rc = smack_netlabel_send(sock->sk, sip);
3674 #ifdef SMACK_IPV6_SECMARK_LABELING
3675 rsp = smack_ipv6host_label(sap);
3677 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3680 #ifdef SMACK_IPV6_PORT_LABELING
3681 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3689 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3690 * @sap: netlabel secattr
3691 * @ssp: socket security information
3693 * Returns a pointer to a Smack label entry found on the label list.
3695 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3696 struct socket_smack *ssp)
3698 struct smack_known *skp;
3703 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3705 * Looks like a CIPSO packet.
3706 * If there are flags but no level netlabel isn't
3707 * behaving the way we expect it to.
3709 * Look it up in the label table
3710 * Without guidance regarding the smack value
3711 * for the packet fall back on the network
3715 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3716 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3719 * Compare the catsets. Use the netlbl APIs.
3721 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3722 if ((skp->smk_netlabel.flags &
3723 NETLBL_SECATTR_MLS_CAT) == 0)
3727 for (acat = -1, kcat = -1; acat == kcat; ) {
3728 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3730 kcat = netlbl_catmap_walk(
3731 skp->smk_netlabel.attr.mls.cat,
3733 if (acat < 0 || kcat < 0)
3746 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3747 return &smack_known_web;
3748 return &smack_known_star;
3750 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3752 * Looks like a fallback, which gives us a secid.
3754 return smack_from_secid(sap->attr.secid);
3756 * Without guidance regarding the smack value
3757 * for the packet fall back on the network
3760 return smack_net_ambient;
3763 #if IS_ENABLED(CONFIG_IPV6)
3764 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3768 int proto = -EINVAL;
3769 struct ipv6hdr _ipv6h;
3770 struct ipv6hdr *ip6;
3772 struct tcphdr _tcph, *th;
3773 struct udphdr _udph, *uh;
3774 struct dccp_hdr _dccph, *dh;
3778 offset = skb_network_offset(skb);
3779 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3782 sip->sin6_addr = ip6->saddr;
3784 nexthdr = ip6->nexthdr;
3785 offset += sizeof(_ipv6h);
3786 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3793 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3795 sip->sin6_port = th->source;
3798 case IPPROTO_UDPLITE:
3799 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3801 sip->sin6_port = uh->source;
3804 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3806 sip->sin6_port = dh->dccph_sport;
3811 #endif /* CONFIG_IPV6 */
3814 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3818 * Returns 0 if the packet should be delivered, an error code otherwise
3820 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3822 struct netlbl_lsm_secattr secattr;
3823 struct socket_smack *ssp = sk->sk_security;
3824 struct smack_known *skp = NULL;
3826 struct smk_audit_info ad;
3827 u16 family = sk->sk_family;
3829 struct lsm_network_audit net;
3831 #if IS_ENABLED(CONFIG_IPV6)
3832 struct sockaddr_in6 sadd;
3835 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3837 #endif /* CONFIG_IPV6 */
3841 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3843 * If there is a secmark use it rather than the CIPSO label.
3844 * If there is no secmark fall back to CIPSO.
3845 * The secmark is assumed to reflect policy better.
3847 if (skb && skb->secmark != 0) {
3848 skp = smack_from_secid(skb->secmark);
3851 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3853 * Translate what netlabel gave us.
3855 netlbl_secattr_init(&secattr);
3857 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3859 skp = smack_from_secattr(&secattr, ssp);
3861 skp = smack_net_ambient;
3863 netlbl_secattr_destroy(&secattr);
3865 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3869 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3870 ad.a.u.net->family = family;
3871 ad.a.u.net->netif = skb->skb_iif;
3872 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3875 * Receiving a packet requires that the other end
3876 * be able to write here. Read access is not required.
3877 * This is the simplist possible security model
3880 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3881 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3884 netlbl_skbuff_err(skb, family, rc, 0);
3886 #if IS_ENABLED(CONFIG_IPV6)
3888 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3889 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3890 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3892 #ifdef SMACK_IPV6_SECMARK_LABELING
3893 if (skb && skb->secmark != 0)
3894 skp = smack_from_secid(skb->secmark);
3896 skp = smack_ipv6host_label(&sadd);
3898 skp = smack_net_ambient;
3900 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3901 ad.a.u.net->family = family;
3902 ad.a.u.net->netif = skb->skb_iif;
3903 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3904 #endif /* CONFIG_AUDIT */
3905 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3906 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3908 #endif /* SMACK_IPV6_SECMARK_LABELING */
3909 #ifdef SMACK_IPV6_PORT_LABELING
3910 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3911 #endif /* SMACK_IPV6_PORT_LABELING */
3913 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3914 ICMPV6_ADM_PROHIBITED, 0);
3916 #endif /* CONFIG_IPV6 */
3923 * smack_socket_getpeersec_stream - pull in packet label
3925 * @optval: user's destination
3926 * @optlen: size thereof
3929 * returns zero on success, an error code otherwise
3931 static int smack_socket_getpeersec_stream(struct socket *sock,
3932 char __user *optval,
3933 int __user *optlen, unsigned len)
3935 struct socket_smack *ssp;
3940 ssp = sock->sk->sk_security;
3941 if (ssp->smk_packet != NULL) {
3942 rcp = ssp->smk_packet->smk_known;
3943 slen = strlen(rcp) + 1;
3948 else if (copy_to_user(optval, rcp, slen) != 0)
3951 if (put_user(slen, optlen) != 0)
3959 * smack_socket_getpeersec_dgram - pull in packet label
3960 * @sock: the peer socket
3962 * @secid: pointer to where to put the secid of the packet
3964 * Sets the netlabel socket state on sk from parent
3966 static int smack_socket_getpeersec_dgram(struct socket *sock,
3967 struct sk_buff *skb, u32 *secid)
3970 struct netlbl_lsm_secattr secattr;
3971 struct socket_smack *ssp = NULL;
3972 struct smack_known *skp;
3973 int family = PF_UNSPEC;
3974 u32 s = 0; /* 0 is the invalid secid */
3978 if (skb->protocol == htons(ETH_P_IP))
3980 #if IS_ENABLED(CONFIG_IPV6)
3981 else if (skb->protocol == htons(ETH_P_IPV6))
3983 #endif /* CONFIG_IPV6 */
3985 if (family == PF_UNSPEC && sock != NULL)
3986 family = sock->sk->sk_family;
3990 ssp = sock->sk->sk_security;
3991 s = ssp->smk_out->smk_secid;
3994 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4000 * Translate what netlabel gave us.
4002 if (sock != NULL && sock->sk != NULL)
4003 ssp = sock->sk->sk_security;
4004 netlbl_secattr_init(&secattr);
4005 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4007 skp = smack_from_secattr(&secattr, ssp);
4010 netlbl_secattr_destroy(&secattr);
4013 #ifdef SMACK_IPV6_SECMARK_LABELING
4025 * smack_sock_graft - Initialize a newly created socket with an existing sock
4027 * @parent: parent socket
4029 * Set the smk_{in,out} state of an existing sock based on the process that
4030 * is creating the new socket.
4032 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4034 struct socket_smack *ssp;
4035 struct smack_known *skp = smk_of_current();
4038 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4041 ssp = sk->sk_security;
4044 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4048 * smack_inet_conn_request - Smack access check on connect
4049 * @sk: socket involved
4053 * Returns 0 if a task with the packet label could write to
4054 * the socket, otherwise an error code
4056 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4057 struct request_sock *req)
4059 u16 family = sk->sk_family;
4060 struct smack_known *skp;
4061 struct socket_smack *ssp = sk->sk_security;
4062 struct netlbl_lsm_secattr secattr;
4063 struct sockaddr_in addr;
4065 struct smack_known *hskp;
4067 struct smk_audit_info ad;
4069 struct lsm_network_audit net;
4072 #if IS_ENABLED(CONFIG_IPV6)
4073 if (family == PF_INET6) {
4075 * Handle mapped IPv4 packets arriving
4076 * via IPv6 sockets. Don't set up netlabel
4077 * processing on IPv6.
4079 if (skb->protocol == htons(ETH_P_IP))
4084 #endif /* CONFIG_IPV6 */
4086 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4088 * If there is a secmark use it rather than the CIPSO label.
4089 * If there is no secmark fall back to CIPSO.
4090 * The secmark is assumed to reflect policy better.
4092 if (skb && skb->secmark != 0) {
4093 skp = smack_from_secid(skb->secmark);
4096 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4098 netlbl_secattr_init(&secattr);
4099 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4101 skp = smack_from_secattr(&secattr, ssp);
4103 skp = &smack_known_huh;
4104 netlbl_secattr_destroy(&secattr);
4106 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4111 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4112 ad.a.u.net->family = family;
4113 ad.a.u.net->netif = skb->skb_iif;
4114 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4117 * Receiving a packet requires that the other end be able to write
4118 * here. Read access is not required.
4120 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4121 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4126 * Save the peer's label in the request_sock so we can later setup
4127 * smk_packet in the child socket so that SO_PEERCRED can report it.
4129 req->peer_secid = skp->smk_secid;
4132 * We need to decide if we want to label the incoming connection here
4133 * if we do we only need to label the request_sock and the stack will
4134 * propagate the wire-label to the sock when it is created.
4137 addr.sin_addr.s_addr = hdr->saddr;
4139 hskp = smack_ipv4host_label(&addr);
4143 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4145 netlbl_req_delattr(req);
4151 * smack_inet_csk_clone - Copy the connection information to the new socket
4152 * @sk: the new socket
4153 * @req: the connection's request_sock
4155 * Transfer the connection's peer label to the newly created socket.
4157 static void smack_inet_csk_clone(struct sock *sk,
4158 const struct request_sock *req)
4160 struct socket_smack *ssp = sk->sk_security;
4161 struct smack_known *skp;
4163 if (req->peer_secid != 0) {
4164 skp = smack_from_secid(req->peer_secid);
4165 ssp->smk_packet = skp;
4167 ssp->smk_packet = NULL;
4171 * Key management security hooks
4173 * Casey has not tested key support very heavily.
4174 * The permission check is most likely too restrictive.
4175 * If you care about keys please have a look.
4180 * smack_key_alloc - Set the key security blob
4182 * @cred: the credentials to use
4185 * No allocation required
4189 static int smack_key_alloc(struct key *key, const struct cred *cred,
4190 unsigned long flags)
4192 struct smack_known *skp = smk_of_task(smack_cred(cred));
4194 key->security = skp;
4199 * smack_key_free - Clear the key security blob
4202 * Clear the blob pointer
4204 static void smack_key_free(struct key *key)
4206 key->security = NULL;
4210 * smack_key_permission - Smack access on a key
4211 * @key_ref: gets to the object
4212 * @cred: the credentials to use
4213 * @perm: requested key permissions
4215 * Return 0 if the task has read and write to the object,
4216 * an error code otherwise
4218 static int smack_key_permission(key_ref_t key_ref,
4219 const struct cred *cred, unsigned perm)
4222 struct smk_audit_info ad;
4223 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4228 * Validate requested permissions
4230 if (perm & ~KEY_NEED_ALL)
4233 keyp = key_ref_to_ptr(key_ref);
4237 * If the key hasn't been initialized give it access so that
4240 if (keyp->security == NULL)
4243 * This should not occur
4248 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4252 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4253 ad.a.u.key_struct.key = keyp->serial;
4254 ad.a.u.key_struct.key_desc = keyp->description;
4256 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4257 request |= MAY_READ;
4258 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4259 request |= MAY_WRITE;
4260 rc = smk_access(tkp, keyp->security, request, &ad);
4261 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4266 * smack_key_getsecurity - Smack label tagging the key
4267 * @key points to the key to be queried
4268 * @_buffer points to a pointer that should be set to point to the
4269 * resulting string (if no label or an error occurs).
4270 * Return the length of the string (including terminating NUL) or -ve if
4272 * May also return 0 (and a NULL buffer pointer) if there is no label.
4274 static int smack_key_getsecurity(struct key *key, char **_buffer)
4276 struct smack_known *skp = key->security;
4280 if (key->security == NULL) {
4285 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4288 length = strlen(copy) + 1;
4294 #endif /* CONFIG_KEYS */
4299 * Audit requires a unique representation of each Smack specific
4300 * rule. This unique representation is used to distinguish the
4301 * object to be audited from remaining kernel objects and also
4302 * works as a glue between the audit hooks.
4304 * Since repository entries are added but never deleted, we'll use
4305 * the smack_known label address related to the given audit rule as
4306 * the needed unique representation. This also better fits the smack
4307 * model where nearly everything is a label.
4312 * smack_audit_rule_init - Initialize a smack audit rule
4313 * @field: audit rule fields given from user-space (audit.h)
4314 * @op: required testing operator (=, !=, >, <, ...)
4315 * @rulestr: smack label to be audited
4316 * @vrule: pointer to save our own audit rule representation
4318 * Prepare to audit cases where (@field @op @rulestr) is true.
4319 * The label to be audited is created if necessay.
4321 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4323 struct smack_known *skp;
4324 char **rule = (char **)vrule;
4327 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4330 if (op != Audit_equal && op != Audit_not_equal)
4333 skp = smk_import_entry(rulestr, 0);
4335 return PTR_ERR(skp);
4337 *rule = skp->smk_known;
4343 * smack_audit_rule_known - Distinguish Smack audit rules
4344 * @krule: rule of interest, in Audit kernel representation format
4346 * This is used to filter Smack rules from remaining Audit ones.
4347 * If it's proved that this rule belongs to us, the
4348 * audit_rule_match hook will be called to do the final judgement.
4350 static int smack_audit_rule_known(struct audit_krule *krule)
4352 struct audit_field *f;
4355 for (i = 0; i < krule->field_count; i++) {
4356 f = &krule->fields[i];
4358 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4366 * smack_audit_rule_match - Audit given object ?
4367 * @secid: security id for identifying the object to test
4368 * @field: audit rule flags given from user-space
4369 * @op: required testing operator
4370 * @vrule: smack internal rule presentation
4371 * @actx: audit context associated with the check
4373 * The core Audit hook. It's used to take the decision of
4374 * whether to audit or not to audit a given object.
4376 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4377 struct audit_context *actx)
4379 struct smack_known *skp;
4382 if (unlikely(!rule)) {
4383 WARN_ONCE(1, "Smack: missing rule\n");
4387 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4390 skp = smack_from_secid(secid);
4393 * No need to do string comparisons. If a match occurs,
4394 * both pointers will point to the same smack_known
4397 if (op == Audit_equal)
4398 return (rule == skp->smk_known);
4399 if (op == Audit_not_equal)
4400 return (rule != skp->smk_known);
4406 * There is no need for a smack_audit_rule_free hook.
4407 * No memory was allocated.
4410 #endif /* CONFIG_AUDIT */
4413 * smack_ismaclabel - check if xattr @name references a smack MAC label
4414 * @name: Full xattr name to check.
4416 static int smack_ismaclabel(const char *name)
4418 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4423 * smack_secid_to_secctx - return the smack label for a secid
4424 * @secid: incoming integer
4425 * @secdata: destination
4426 * @seclen: how long it is
4428 * Exists for networking code.
4430 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4432 struct smack_known *skp = smack_from_secid(secid);
4435 *secdata = skp->smk_known;
4436 *seclen = strlen(skp->smk_known);
4441 * smack_secctx_to_secid - return the secid for a smack label
4442 * @secdata: smack label
4443 * @seclen: how long result is
4444 * @secid: outgoing integer
4446 * Exists for audit and networking code.
4448 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4450 struct smack_known *skp = smk_find_entry(secdata);
4453 *secid = skp->smk_secid;
4460 * There used to be a smack_release_secctx hook
4461 * that did nothing back when hooks were in a vector.
4462 * Now that there's a list such a hook adds cost.
4465 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4467 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4470 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4472 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4475 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4477 struct smack_known *skp = smk_of_inode(inode);
4479 *ctx = skp->smk_known;
4480 *ctxlen = strlen(skp->smk_known);
4484 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4487 struct task_smack *tsp;
4488 struct smack_known *skp;
4489 struct inode_smack *isp;
4490 struct cred *new_creds = *new;
4492 if (new_creds == NULL) {
4493 new_creds = prepare_creds();
4494 if (new_creds == NULL)
4498 tsp = smack_cred(new_creds);
4501 * Get label from overlay inode and set it in create_sid
4503 isp = d_inode(dentry->d_parent)->i_security;
4504 skp = isp->smk_inode;
4505 tsp->smk_task = skp;
4510 static int smack_inode_copy_up_xattr(const char *name)
4513 * Return 1 if this is the smack access Smack attribute.
4515 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4521 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4523 const struct cred *old,
4526 struct task_smack *otsp = smack_cred(old);
4527 struct task_smack *ntsp = smack_cred(new);
4528 struct inode_smack *isp;
4532 * Use the process credential unless all of
4533 * the transmuting criteria are met
4535 ntsp->smk_task = otsp->smk_task;
4538 * the attribute of the containing directory
4540 isp = d_inode(dentry->d_parent)->i_security;
4542 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4544 may = smk_access_entry(otsp->smk_task->smk_known,
4545 isp->smk_inode->smk_known,
4546 &otsp->smk_task->smk_rules);
4550 * If the directory is transmuting and the rule
4551 * providing access is transmuting use the containing
4552 * directory label instead of the process label.
4554 if (may > 0 && (may & MAY_TRANSMUTE))
4555 ntsp->smk_task = isp->smk_inode;
4560 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4561 .lbs_cred = sizeof(struct task_smack),
4564 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4565 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4566 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4567 LSM_HOOK_INIT(syslog, smack_syslog),
4569 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4570 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4571 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4572 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4573 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4574 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4576 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4578 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4579 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4580 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4581 LSM_HOOK_INIT(inode_link, smack_inode_link),
4582 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4583 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4584 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4585 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4586 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4587 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4588 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4589 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4590 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4591 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4592 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4593 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4594 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4595 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4597 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4598 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4599 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4600 LSM_HOOK_INIT(file_lock, smack_file_lock),
4601 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4602 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4603 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4604 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4605 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4606 LSM_HOOK_INIT(file_receive, smack_file_receive),
4608 LSM_HOOK_INIT(file_open, smack_file_open),
4610 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4611 LSM_HOOK_INIT(cred_free, smack_cred_free),
4612 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4613 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4614 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4615 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4616 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4617 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4618 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4619 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4620 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4621 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4622 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4623 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4624 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4625 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4626 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4627 LSM_HOOK_INIT(task_kill, smack_task_kill),
4628 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4630 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4631 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4633 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4634 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4636 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4637 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
4638 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4639 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4640 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4641 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4643 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4644 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
4645 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4646 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4647 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4649 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4650 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
4651 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4652 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4653 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4655 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4657 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4658 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4660 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4661 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4663 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4664 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4665 #ifdef SMACK_IPV6_PORT_LABELING
4666 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4668 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4669 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4670 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4671 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4672 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4673 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4674 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4675 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4676 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4677 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4679 /* key management security hooks */
4681 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4682 LSM_HOOK_INIT(key_free, smack_key_free),
4683 LSM_HOOK_INIT(key_permission, smack_key_permission),
4684 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4685 #endif /* CONFIG_KEYS */
4689 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4690 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4691 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4692 #endif /* CONFIG_AUDIT */
4694 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4695 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4696 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4697 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4698 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4699 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4700 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4701 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4702 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4706 static __init void init_smack_known_list(void)
4709 * Initialize rule list locks
4711 mutex_init(&smack_known_huh.smk_rules_lock);
4712 mutex_init(&smack_known_hat.smk_rules_lock);
4713 mutex_init(&smack_known_floor.smk_rules_lock);
4714 mutex_init(&smack_known_star.smk_rules_lock);
4715 mutex_init(&smack_known_web.smk_rules_lock);
4717 * Initialize rule lists
4719 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4720 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4721 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4722 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4723 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4725 * Create the known labels list
4727 smk_insert_entry(&smack_known_huh);
4728 smk_insert_entry(&smack_known_hat);
4729 smk_insert_entry(&smack_known_star);
4730 smk_insert_entry(&smack_known_floor);
4731 smk_insert_entry(&smack_known_web);
4735 * smack_init - initialize the smack system
4739 static __init int smack_init(void)
4741 struct cred *cred = (struct cred *) current->cred;
4742 struct task_smack *tsp;
4744 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4745 if (!smack_inode_cache)
4748 lsm_early_cred(cred);
4751 * Set the security state for the initial task.
4753 tsp = smack_cred(cred);
4754 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4759 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4762 pr_info("Smack: Initializing.\n");
4763 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4764 pr_info("Smack: Netfilter enabled.\n");
4766 #ifdef SMACK_IPV6_PORT_LABELING
4767 pr_info("Smack: IPv6 port labeling enabled.\n");
4769 #ifdef SMACK_IPV6_SECMARK_LABELING
4770 pr_info("Smack: IPv6 Netfilter enabled.\n");
4773 /* initialize the smack_known_list */
4774 init_smack_known_list();
4780 * Smack requires early initialization in order to label
4781 * all processes and objects when they are created.
4783 DEFINE_LSM(smack) = {
4785 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4786 .blobs = &smack_blob_sizes,