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 static const match_table_t smk_mount_tokens = {
63 {Opt_fsdefault, SMK_FSDEFAULT "%s"},
64 {Opt_fsfloor, SMK_FSFLOOR "%s"},
65 {Opt_fshat, SMK_FSHAT "%s"},
66 {Opt_fsroot, SMK_FSROOT "%s"},
67 {Opt_fstransmute, SMK_FSTRANS "%s"},
71 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
72 static char *smk_bu_mess[] = {
73 "Bringup Error", /* Unused */
74 "Bringup", /* SMACK_BRINGUP_ALLOW */
75 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
76 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
79 static void smk_bu_mode(int mode, char *s)
89 if (mode & MAY_APPEND)
91 if (mode & MAY_TRANSMUTE)
101 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
102 static int smk_bu_note(char *note, struct smack_known *sskp,
103 struct smack_known *oskp, int mode, int rc)
105 char acc[SMK_NUM_ACCESS_TYPE + 1];
109 if (rc > SMACK_UNCONFINED_OBJECT)
112 smk_bu_mode(mode, acc);
113 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
114 sskp->smk_known, oskp->smk_known, acc, note);
118 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
121 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
122 static int smk_bu_current(char *note, struct smack_known *oskp,
125 struct task_smack *tsp = current_security();
126 char acc[SMK_NUM_ACCESS_TYPE + 1];
130 if (rc > SMACK_UNCONFINED_OBJECT)
133 smk_bu_mode(mode, acc);
134 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
135 tsp->smk_task->smk_known, oskp->smk_known,
136 acc, current->comm, note);
140 #define smk_bu_current(note, oskp, mode, RC) (RC)
143 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
144 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
146 struct task_smack *tsp = current_security();
147 struct smack_known *smk_task = smk_of_task_struct(otp);
148 char acc[SMK_NUM_ACCESS_TYPE + 1];
152 if (rc > SMACK_UNCONFINED_OBJECT)
155 smk_bu_mode(mode, acc);
156 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
157 tsp->smk_task->smk_known, smk_task->smk_known, acc,
158 current->comm, otp->comm);
162 #define smk_bu_task(otp, mode, RC) (RC)
165 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
166 static int smk_bu_inode(struct inode *inode, int mode, int rc)
168 struct task_smack *tsp = current_security();
169 struct inode_smack *isp = inode->i_security;
170 char acc[SMK_NUM_ACCESS_TYPE + 1];
172 if (isp->smk_flags & SMK_INODE_IMPURE)
173 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
174 inode->i_sb->s_id, inode->i_ino, current->comm);
178 if (rc > SMACK_UNCONFINED_OBJECT)
180 if (rc == SMACK_UNCONFINED_SUBJECT &&
181 (mode & (MAY_WRITE | MAY_APPEND)))
182 isp->smk_flags |= SMK_INODE_IMPURE;
184 smk_bu_mode(mode, acc);
186 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
187 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
188 inode->i_sb->s_id, inode->i_ino, current->comm);
192 #define smk_bu_inode(inode, mode, RC) (RC)
195 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
196 static int smk_bu_file(struct file *file, int mode, int rc)
198 struct task_smack *tsp = current_security();
199 struct smack_known *sskp = tsp->smk_task;
200 struct inode *inode = file_inode(file);
201 struct inode_smack *isp = inode->i_security;
202 char acc[SMK_NUM_ACCESS_TYPE + 1];
204 if (isp->smk_flags & SMK_INODE_IMPURE)
205 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
206 inode->i_sb->s_id, inode->i_ino, current->comm);
210 if (rc > SMACK_UNCONFINED_OBJECT)
213 smk_bu_mode(mode, acc);
214 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
215 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
216 inode->i_sb->s_id, inode->i_ino, file,
221 #define smk_bu_file(file, mode, RC) (RC)
224 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
225 static int smk_bu_credfile(const struct cred *cred, struct file *file,
228 struct task_smack *tsp = cred->security;
229 struct smack_known *sskp = tsp->smk_task;
230 struct inode *inode = file_inode(file);
231 struct inode_smack *isp = inode->i_security;
232 char acc[SMK_NUM_ACCESS_TYPE + 1];
234 if (isp->smk_flags & SMK_INODE_IMPURE)
235 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
236 inode->i_sb->s_id, inode->i_ino, current->comm);
240 if (rc > SMACK_UNCONFINED_OBJECT)
243 smk_bu_mode(mode, acc);
244 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
245 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
246 inode->i_sb->s_id, inode->i_ino, file,
251 #define smk_bu_credfile(cred, file, mode, RC) (RC)
255 * smk_fetch - Fetch the smack label from a file.
256 * @name: type of the label (attribute)
257 * @ip: a pointer to the inode
258 * @dp: a pointer to the dentry
260 * Returns a pointer to the master list entry for the Smack label,
261 * NULL if there was no label to fetch, or an error code.
263 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
268 struct smack_known *skp = NULL;
270 if (!(ip->i_opflags & IOP_XATTR))
271 return ERR_PTR(-EOPNOTSUPP);
273 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
275 return ERR_PTR(-ENOMEM);
277 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
283 skp = smk_import_entry(buffer, rc);
291 * new_inode_smack - allocate an inode security blob
292 * @skp: a pointer to the Smack label entry to use in the blob
294 * Returns the new blob or NULL if there's no memory available
296 static struct inode_smack *new_inode_smack(struct smack_known *skp)
298 struct inode_smack *isp;
300 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
304 isp->smk_inode = skp;
306 mutex_init(&isp->smk_lock);
312 * new_task_smack - allocate a task security blob
313 * @task: a pointer to the Smack label for the running task
314 * @forked: a pointer to the Smack label for the forked task
315 * @gfp: type of the memory for the allocation
317 * Returns the new blob or NULL if there's no memory available
319 static struct task_smack *new_task_smack(struct smack_known *task,
320 struct smack_known *forked, gfp_t gfp)
322 struct task_smack *tsp;
324 tsp = kzalloc(sizeof(struct task_smack), gfp);
328 tsp->smk_task = task;
329 tsp->smk_forked = forked;
330 INIT_LIST_HEAD(&tsp->smk_rules);
331 INIT_LIST_HEAD(&tsp->smk_relabel);
332 mutex_init(&tsp->smk_rules_lock);
338 * smk_copy_rules - copy a rule set
339 * @nhead: new rules header pointer
340 * @ohead: old rules header pointer
341 * @gfp: type of the memory for the allocation
343 * Returns 0 on success, -ENOMEM on error
345 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
348 struct smack_rule *nrp;
349 struct smack_rule *orp;
352 list_for_each_entry_rcu(orp, ohead, list) {
353 nrp = kzalloc(sizeof(struct smack_rule), gfp);
359 list_add_rcu(&nrp->list, nhead);
365 * smk_copy_relabel - copy smk_relabel labels list
366 * @nhead: new rules header pointer
367 * @ohead: old rules header pointer
368 * @gfp: type of the memory for the allocation
370 * Returns 0 on success, -ENOMEM on error
372 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
375 struct smack_known_list_elem *nklep;
376 struct smack_known_list_elem *oklep;
378 list_for_each_entry(oklep, ohead, list) {
379 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
381 smk_destroy_label_list(nhead);
384 nklep->smk_label = oklep->smk_label;
385 list_add(&nklep->list, nhead);
392 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
393 * @mode - input mode in form of PTRACE_MODE_*
395 * Returns a converted MAY_* mode usable by smack rules
397 static inline unsigned int smk_ptrace_mode(unsigned int mode)
399 if (mode & PTRACE_MODE_ATTACH)
400 return MAY_READWRITE;
401 if (mode & PTRACE_MODE_READ)
408 * smk_ptrace_rule_check - helper for ptrace access
409 * @tracer: tracer process
410 * @tracee_known: label entry of the process that's about to be traced
411 * @mode: ptrace attachment mode (PTRACE_MODE_*)
412 * @func: name of the function that called us, used for audit
414 * Returns 0 on access granted, -error on error
416 static int smk_ptrace_rule_check(struct task_struct *tracer,
417 struct smack_known *tracee_known,
418 unsigned int mode, const char *func)
421 struct smk_audit_info ad, *saip = NULL;
422 struct task_smack *tsp;
423 struct smack_known *tracer_known;
424 const struct cred *tracercred;
426 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
427 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
428 smk_ad_setfield_u_tsk(&ad, tracer);
433 tracercred = __task_cred(tracer);
434 tsp = tracercred->security;
435 tracer_known = smk_of_task(tsp);
437 if ((mode & PTRACE_MODE_ATTACH) &&
438 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
439 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
440 if (tracer_known->smk_known == tracee_known->smk_known)
442 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
444 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
450 smack_log(tracer_known->smk_known,
451 tracee_known->smk_known,
458 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
459 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
467 * We he, that is fun!
471 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
472 * @ctp: child task pointer
473 * @mode: ptrace attachment mode (PTRACE_MODE_*)
475 * Returns 0 if access is OK, an error code otherwise
477 * Do the capability checks.
479 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
481 struct smack_known *skp;
483 skp = smk_of_task_struct(ctp);
485 return smk_ptrace_rule_check(current, skp, mode, __func__);
489 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
490 * @ptp: parent task pointer
492 * Returns 0 if access is OK, an error code otherwise
494 * Do the capability checks, and require PTRACE_MODE_ATTACH.
496 static int smack_ptrace_traceme(struct task_struct *ptp)
499 struct smack_known *skp;
501 skp = smk_of_task(current_security());
503 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
508 * smack_syslog - Smack approval on syslog
509 * @type: message type
511 * Returns 0 on success, error code otherwise.
513 static int smack_syslog(int typefrom_file)
516 struct smack_known *skp = smk_of_current();
518 if (smack_privileged(CAP_MAC_OVERRIDE))
521 if (smack_syslog_label != NULL && smack_syslog_label != skp)
533 * smack_sb_alloc_security - allocate a superblock blob
534 * @sb: the superblock getting the blob
536 * Returns 0 on success or -ENOMEM on error.
538 static int smack_sb_alloc_security(struct super_block *sb)
540 struct superblock_smack *sbsp;
542 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
547 sbsp->smk_root = &smack_known_floor;
548 sbsp->smk_default = &smack_known_floor;
549 sbsp->smk_floor = &smack_known_floor;
550 sbsp->smk_hat = &smack_known_hat;
552 * SMK_SB_INITIALIZED will be zero from kzalloc.
554 sb->s_security = sbsp;
560 * smack_sb_free_security - free a superblock blob
561 * @sb: the superblock getting the blob
564 static void smack_sb_free_security(struct super_block *sb)
566 kfree(sb->s_security);
567 sb->s_security = NULL;
571 * smack_sb_copy_data - copy mount options data for processing
572 * @orig: where to start
573 * @smackopts: mount options string
575 * Returns 0 on success or -ENOMEM on error.
577 * Copy the Smack specific mount options out of the mount
580 static int smack_sb_copy_data(char *orig, char *smackopts)
582 char *cp, *commap, *otheropts, *dp;
584 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
585 if (otheropts == NULL)
588 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
589 if (strstr(cp, SMK_FSDEFAULT) == cp)
591 else if (strstr(cp, SMK_FSFLOOR) == cp)
593 else if (strstr(cp, SMK_FSHAT) == cp)
595 else if (strstr(cp, SMK_FSROOT) == cp)
597 else if (strstr(cp, SMK_FSTRANS) == cp)
602 commap = strchr(cp, ',');
611 strcpy(orig, otheropts);
612 free_page((unsigned long)otheropts);
618 * smack_parse_opts_str - parse Smack specific mount options
619 * @options: mount options string
620 * @opts: where to store converted mount opts
622 * Returns 0 on success or -ENOMEM on error.
624 * converts Smack specific mount options to generic security option format
626 static int smack_parse_opts_str(char *options,
627 struct security_mnt_opts *opts)
630 char *fsdefault = NULL;
631 char *fsfloor = NULL;
634 char *fstransmute = NULL;
636 int num_mnt_opts = 0;
639 opts->num_mnt_opts = 0;
644 while ((p = strsep(&options, ",")) != NULL) {
645 substring_t args[MAX_OPT_ARGS];
650 token = match_token(p, smk_mount_tokens, args);
656 fsdefault = match_strdup(&args[0]);
663 fsfloor = match_strdup(&args[0]);
670 fshat = match_strdup(&args[0]);
677 fsroot = match_strdup(&args[0]);
681 case Opt_fstransmute:
684 fstransmute = match_strdup(&args[0]);
690 pr_warn("Smack: unknown mount option\n");
695 opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_KERNEL);
699 opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
701 if (!opts->mnt_opts_flags)
705 opts->mnt_opts[num_mnt_opts] = fsdefault;
706 opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
709 opts->mnt_opts[num_mnt_opts] = fsfloor;
710 opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
713 opts->mnt_opts[num_mnt_opts] = fshat;
714 opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
717 opts->mnt_opts[num_mnt_opts] = fsroot;
718 opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
721 opts->mnt_opts[num_mnt_opts] = fstransmute;
722 opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
725 opts->num_mnt_opts = num_mnt_opts;
730 pr_warn("Smack: duplicate mount options\n");
738 security_free_mnt_opts(opts);
743 * smack_set_mnt_opts - set Smack specific mount options
744 * @sb: the file system superblock
745 * @opts: Smack mount options
746 * @kern_flags: mount option from kernel space or user space
747 * @set_kern_flags: where to store converted mount opts
749 * Returns 0 on success, an error code on failure
751 * Allow filesystems with binary mount data to explicitly set Smack mount
754 static int smack_set_mnt_opts(struct super_block *sb,
755 struct security_mnt_opts *opts,
756 unsigned long kern_flags,
757 unsigned long *set_kern_flags)
759 struct dentry *root = sb->s_root;
760 struct inode *inode = d_backing_inode(root);
761 struct superblock_smack *sp = sb->s_security;
762 struct inode_smack *isp;
763 struct smack_known *skp;
765 int num_opts = opts->num_mnt_opts;
768 if (sp->smk_flags & SMK_SB_INITIALIZED)
771 if (!smack_privileged(CAP_MAC_ADMIN)) {
773 * Unprivileged mounts don't get to specify Smack values.
778 * Unprivileged mounts get root and default from the caller.
780 skp = smk_of_current();
782 sp->smk_default = skp;
784 * For a handful of fs types with no user-controlled
785 * backing store it's okay to trust security labels
786 * in the filesystem. The rest are untrusted.
788 if (sb->s_user_ns != &init_user_ns &&
789 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
790 sb->s_magic != RAMFS_MAGIC) {
792 sp->smk_flags |= SMK_SB_UNTRUSTED;
796 sp->smk_flags |= SMK_SB_INITIALIZED;
798 for (i = 0; i < num_opts; i++) {
799 switch (opts->mnt_opts_flags[i]) {
801 skp = smk_import_entry(opts->mnt_opts[i], 0);
804 sp->smk_default = skp;
807 skp = smk_import_entry(opts->mnt_opts[i], 0);
813 skp = smk_import_entry(opts->mnt_opts[i], 0);
819 skp = smk_import_entry(opts->mnt_opts[i], 0);
825 skp = smk_import_entry(opts->mnt_opts[i], 0);
837 * Initialize the root inode.
839 isp = inode->i_security;
841 isp = new_inode_smack(sp->smk_root);
844 inode->i_security = isp;
846 isp->smk_inode = sp->smk_root;
849 isp->smk_flags |= SMK_INODE_TRANSMUTE;
855 * smack_sb_kern_mount - Smack specific mount processing
856 * @sb: the file system superblock
857 * @flags: the mount flags
858 * @data: the smack mount options
860 * Returns 0 on success, an error code on failure
862 static int smack_sb_kern_mount(struct super_block *sb, int flags,
863 struct security_mnt_opts *opts)
865 return smack_set_mnt_opts(sb, opts, 0, NULL);
869 * smack_sb_statfs - Smack check on statfs
870 * @dentry: identifies the file system in question
872 * Returns 0 if current can read the floor of the filesystem,
873 * and error code otherwise
875 static int smack_sb_statfs(struct dentry *dentry)
877 struct superblock_smack *sbp = dentry->d_sb->s_security;
879 struct smk_audit_info ad;
881 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
882 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
884 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
885 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
894 * smack_bprm_set_creds - set creds for exec
895 * @bprm: the exec information
897 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
899 static int smack_bprm_set_creds(struct linux_binprm *bprm)
901 struct inode *inode = file_inode(bprm->file);
902 struct task_smack *bsp = bprm->cred->security;
903 struct inode_smack *isp;
904 struct superblock_smack *sbsp;
907 if (bprm->called_set_creds)
910 isp = inode->i_security;
911 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
914 sbsp = inode->i_sb->s_security;
915 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
916 isp->smk_task != sbsp->smk_root)
919 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
920 struct task_struct *tracer;
924 tracer = ptrace_parent(current);
925 if (likely(tracer != NULL))
926 rc = smk_ptrace_rule_check(tracer,
934 } else if (bprm->unsafe)
937 bsp->smk_task = isp->smk_task;
938 bprm->per_clear |= PER_CLEAR_ON_SETID;
940 /* Decide if this is a secure exec. */
941 if (bsp->smk_task != bsp->smk_forked)
942 bprm->secureexec = 1;
952 * smack_inode_alloc_security - allocate an inode blob
953 * @inode: the inode in need of a blob
955 * Returns 0 if it gets a blob, -ENOMEM otherwise
957 static int smack_inode_alloc_security(struct inode *inode)
959 struct smack_known *skp = smk_of_current();
961 inode->i_security = new_inode_smack(skp);
962 if (inode->i_security == NULL)
968 * smack_inode_free_rcu - Free inode_smack blob from cache
969 * @head: the rcu_head for getting inode_smack pointer
971 * Call back function called from call_rcu() to free
972 * the i_security blob pointer in inode
974 static void smack_inode_free_rcu(struct rcu_head *head)
976 struct inode_smack *issp;
978 issp = container_of(head, struct inode_smack, smk_rcu);
979 kmem_cache_free(smack_inode_cache, issp);
983 * smack_inode_free_security - free an inode blob using call_rcu()
984 * @inode: the inode with a blob
986 * Clears the blob pointer in inode using RCU
988 static void smack_inode_free_security(struct inode *inode)
990 struct inode_smack *issp = inode->i_security;
993 * The inode may still be referenced in a path walk and
994 * a call to smack_inode_permission() can be made
995 * after smack_inode_free_security() is called.
996 * To avoid race condition free the i_security via RCU
997 * and leave the current inode->i_security pointer intact.
998 * The inode will be freed after the RCU grace period too.
1000 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
1004 * smack_inode_init_security - copy out the smack from an inode
1005 * @inode: the newly created inode
1006 * @dir: containing directory object
1008 * @name: where to put the attribute name
1009 * @value: where to put the attribute value
1010 * @len: where to put the length of the attribute
1012 * Returns 0 if it all works out, -ENOMEM if there's no memory
1014 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
1015 const struct qstr *qstr, const char **name,
1016 void **value, size_t *len)
1018 struct inode_smack *issp = inode->i_security;
1019 struct smack_known *skp = smk_of_current();
1020 struct smack_known *isp = smk_of_inode(inode);
1021 struct smack_known *dsp = smk_of_inode(dir);
1025 *name = XATTR_SMACK_SUFFIX;
1029 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1034 * If the access rule allows transmutation and
1035 * the directory requests transmutation then
1036 * by all means transmute.
1037 * Mark the inode as changed.
1039 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1040 smk_inode_transmutable(dir)) {
1042 issp->smk_flags |= SMK_INODE_CHANGED;
1045 *value = kstrdup(isp->smk_known, GFP_NOFS);
1049 *len = strlen(isp->smk_known);
1056 * smack_inode_link - Smack check on link
1057 * @old_dentry: the existing object
1059 * @new_dentry: the new object
1061 * Returns 0 if access is permitted, an error code otherwise
1063 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1064 struct dentry *new_dentry)
1066 struct smack_known *isp;
1067 struct smk_audit_info ad;
1070 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1071 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1073 isp = smk_of_inode(d_backing_inode(old_dentry));
1074 rc = smk_curacc(isp, MAY_WRITE, &ad);
1075 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1077 if (rc == 0 && d_is_positive(new_dentry)) {
1078 isp = smk_of_inode(d_backing_inode(new_dentry));
1079 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1080 rc = smk_curacc(isp, MAY_WRITE, &ad);
1081 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1088 * smack_inode_unlink - Smack check on inode deletion
1089 * @dir: containing directory object
1090 * @dentry: file to unlink
1092 * Returns 0 if current can write the containing directory
1093 * and the object, error code otherwise
1095 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1097 struct inode *ip = d_backing_inode(dentry);
1098 struct smk_audit_info ad;
1101 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1102 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1105 * You need write access to the thing you're unlinking
1107 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1108 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1111 * You also need write access to the containing directory
1113 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1114 smk_ad_setfield_u_fs_inode(&ad, dir);
1115 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1116 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1122 * smack_inode_rmdir - Smack check on directory deletion
1123 * @dir: containing directory object
1124 * @dentry: directory to unlink
1126 * Returns 0 if current can write the containing directory
1127 * and the directory, error code otherwise
1129 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1131 struct smk_audit_info ad;
1134 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1135 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1138 * You need write access to the thing you're removing
1140 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1141 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1144 * You also need write access to the containing directory
1146 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1147 smk_ad_setfield_u_fs_inode(&ad, dir);
1148 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1149 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1156 * smack_inode_rename - Smack check on rename
1157 * @old_inode: unused
1158 * @old_dentry: the old object
1159 * @new_inode: unused
1160 * @new_dentry: the new object
1162 * Read and write access is required on both the old and
1165 * Returns 0 if access is permitted, an error code otherwise
1167 static int smack_inode_rename(struct inode *old_inode,
1168 struct dentry *old_dentry,
1169 struct inode *new_inode,
1170 struct dentry *new_dentry)
1173 struct smack_known *isp;
1174 struct smk_audit_info ad;
1176 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1177 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1179 isp = smk_of_inode(d_backing_inode(old_dentry));
1180 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1181 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1183 if (rc == 0 && d_is_positive(new_dentry)) {
1184 isp = smk_of_inode(d_backing_inode(new_dentry));
1185 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1186 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1187 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1193 * smack_inode_permission - Smack version of permission()
1194 * @inode: the inode in question
1195 * @mask: the access requested
1197 * This is the important Smack hook.
1199 * Returns 0 if access is permitted, -EACCES otherwise
1201 static int smack_inode_permission(struct inode *inode, int mask)
1203 struct superblock_smack *sbsp = inode->i_sb->s_security;
1204 struct smk_audit_info ad;
1205 int no_block = mask & MAY_NOT_BLOCK;
1208 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1210 * No permission to check. Existence test. Yup, it's there.
1215 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1216 if (smk_of_inode(inode) != sbsp->smk_root)
1220 /* May be droppable after audit */
1223 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1224 smk_ad_setfield_u_fs_inode(&ad, inode);
1225 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1226 rc = smk_bu_inode(inode, mask, rc);
1231 * smack_inode_setattr - Smack check for setting attributes
1232 * @dentry: the object
1233 * @iattr: for the force flag
1235 * Returns 0 if access is permitted, an error code otherwise
1237 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1239 struct smk_audit_info ad;
1243 * Need to allow for clearing the setuid bit.
1245 if (iattr->ia_valid & ATTR_FORCE)
1247 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1248 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1250 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1251 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1256 * smack_inode_getattr - Smack check for getting attributes
1257 * @mnt: vfsmount of the object
1258 * @dentry: the object
1260 * Returns 0 if access is permitted, an error code otherwise
1262 static int smack_inode_getattr(const struct path *path)
1264 struct smk_audit_info ad;
1265 struct inode *inode = d_backing_inode(path->dentry);
1268 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1269 smk_ad_setfield_u_fs_path(&ad, *path);
1270 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1271 rc = smk_bu_inode(inode, MAY_READ, rc);
1276 * smack_inode_setxattr - Smack check for setting xattrs
1277 * @dentry: the object
1278 * @name: name of the attribute
1279 * @value: value of the attribute
1280 * @size: size of the value
1283 * This protects the Smack attribute explicitly.
1285 * Returns 0 if access is permitted, an error code otherwise
1287 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1288 const void *value, size_t size, int flags)
1290 struct smk_audit_info ad;
1291 struct smack_known *skp;
1293 int check_import = 0;
1298 * Check label validity here so import won't fail in post_setxattr
1300 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1301 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1302 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1305 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1306 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1310 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1312 if (size != TRANS_TRUE_SIZE ||
1313 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1316 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1318 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1321 if (rc == 0 && check_import) {
1322 skp = size ? smk_import_entry(value, size) : NULL;
1325 else if (skp == NULL || (check_star &&
1326 (skp == &smack_known_star || skp == &smack_known_web)))
1330 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1331 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1334 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1335 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1342 * smack_inode_post_setxattr - Apply the Smack update approved above
1344 * @name: attribute name
1345 * @value: attribute value
1346 * @size: attribute size
1349 * Set the pointer in the inode blob to the entry found
1350 * in the master label list.
1352 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1353 const void *value, size_t size, int flags)
1355 struct smack_known *skp;
1356 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
1358 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1359 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1363 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1364 skp = smk_import_entry(value, size);
1366 isp->smk_inode = skp;
1367 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1368 skp = smk_import_entry(value, size);
1370 isp->smk_task = skp;
1371 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1372 skp = smk_import_entry(value, size);
1374 isp->smk_mmap = skp;
1381 * smack_inode_getxattr - Smack check on getxattr
1382 * @dentry: the object
1385 * Returns 0 if access is permitted, an error code otherwise
1387 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1389 struct smk_audit_info ad;
1392 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1393 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1395 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1396 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1401 * smack_inode_removexattr - Smack check on removexattr
1402 * @dentry: the object
1403 * @name: name of the attribute
1405 * Removing the Smack attribute requires CAP_MAC_ADMIN
1407 * Returns 0 if access is permitted, an error code otherwise
1409 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1411 struct inode_smack *isp;
1412 struct smk_audit_info ad;
1415 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1416 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1417 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1418 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1419 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1420 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1421 if (!smack_privileged(CAP_MAC_ADMIN))
1424 rc = cap_inode_removexattr(dentry, name);
1429 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1430 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1432 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1433 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1437 isp = d_backing_inode(dentry)->i_security;
1439 * Don't do anything special for these.
1440 * XATTR_NAME_SMACKIPIN
1441 * XATTR_NAME_SMACKIPOUT
1443 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1444 struct super_block *sbp = dentry->d_sb;
1445 struct superblock_smack *sbsp = sbp->s_security;
1447 isp->smk_inode = sbsp->smk_default;
1448 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1449 isp->smk_task = NULL;
1450 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1451 isp->smk_mmap = NULL;
1452 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1453 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1459 * smack_inode_getsecurity - get smack xattrs
1460 * @inode: the object
1461 * @name: attribute name
1462 * @buffer: where to put the result
1463 * @alloc: duplicate memory
1465 * Returns the size of the attribute or an error code
1467 static int smack_inode_getsecurity(struct inode *inode,
1468 const char *name, void **buffer,
1471 struct socket_smack *ssp;
1472 struct socket *sock;
1473 struct super_block *sbp;
1474 struct inode *ip = (struct inode *)inode;
1475 struct smack_known *isp;
1477 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1478 isp = smk_of_inode(inode);
1481 * The rest of the Smack xattrs are only on sockets.
1484 if (sbp->s_magic != SOCKFS_MAGIC)
1487 sock = SOCKET_I(ip);
1488 if (sock == NULL || sock->sk == NULL)
1491 ssp = sock->sk->sk_security;
1493 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1495 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1502 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1503 if (*buffer == NULL)
1507 return strlen(isp->smk_known);
1512 * smack_inode_listsecurity - list the Smack attributes
1513 * @inode: the object
1514 * @buffer: where they go
1515 * @buffer_size: size of buffer
1517 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1520 int len = sizeof(XATTR_NAME_SMACK);
1522 if (buffer != NULL && len <= buffer_size)
1523 memcpy(buffer, XATTR_NAME_SMACK, len);
1529 * smack_inode_getsecid - Extract inode's security id
1530 * @inode: inode to extract the info from
1531 * @secid: where result will be saved
1533 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1535 struct smack_known *skp = smk_of_inode(inode);
1537 *secid = skp->smk_secid;
1545 * There is no smack_file_permission hook
1547 * Should access checks be done on each read or write?
1548 * UNICOS and SELinux say yes.
1549 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1551 * I'll say no for now. Smack does not do the frequent
1552 * label changing that SELinux does.
1556 * smack_file_alloc_security - assign a file security blob
1559 * The security blob for a file is a pointer to the master
1560 * label list, so no allocation is done.
1562 * f_security is the owner security information. It
1563 * isn't used on file access checks, it's for send_sigio.
1567 static int smack_file_alloc_security(struct file *file)
1569 struct smack_known *skp = smk_of_current();
1571 file->f_security = skp;
1576 * smack_file_free_security - clear a file security blob
1579 * The security blob for a file is a pointer to the master
1580 * label list, so no memory is freed.
1582 static void smack_file_free_security(struct file *file)
1584 file->f_security = NULL;
1588 * smack_file_ioctl - Smack check on ioctls
1593 * Relies heavily on the correct use of the ioctl command conventions.
1595 * Returns 0 if allowed, error code otherwise
1597 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1601 struct smk_audit_info ad;
1602 struct inode *inode = file_inode(file);
1604 if (unlikely(IS_PRIVATE(inode)))
1607 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1608 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1610 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1611 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1612 rc = smk_bu_file(file, MAY_WRITE, rc);
1615 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1616 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1617 rc = smk_bu_file(file, MAY_READ, rc);
1624 * smack_file_lock - Smack check on file locking
1628 * Returns 0 if current has lock access, error code otherwise
1630 static int smack_file_lock(struct file *file, unsigned int cmd)
1632 struct smk_audit_info ad;
1634 struct inode *inode = file_inode(file);
1636 if (unlikely(IS_PRIVATE(inode)))
1639 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1640 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1641 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1642 rc = smk_bu_file(file, MAY_LOCK, rc);
1647 * smack_file_fcntl - Smack check on fcntl
1649 * @cmd: what action to check
1652 * Generally these operations are harmless.
1653 * File locking operations present an obvious mechanism
1654 * for passing information, so they require write access.
1656 * Returns 0 if current has access, error code otherwise
1658 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1661 struct smk_audit_info ad;
1663 struct inode *inode = file_inode(file);
1665 if (unlikely(IS_PRIVATE(inode)))
1673 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1674 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1675 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1676 rc = smk_bu_file(file, MAY_LOCK, rc);
1680 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1681 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1682 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1683 rc = smk_bu_file(file, MAY_WRITE, rc);
1694 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1695 * if mapping anonymous memory.
1696 * @file contains the file structure for file to map (may be NULL).
1697 * @reqprot contains the protection requested by the application.
1698 * @prot contains the protection that will be applied by the kernel.
1699 * @flags contains the operational flags.
1700 * Return 0 if permission is granted.
1702 static int smack_mmap_file(struct file *file,
1703 unsigned long reqprot, unsigned long prot,
1704 unsigned long flags)
1706 struct smack_known *skp;
1707 struct smack_known *mkp;
1708 struct smack_rule *srp;
1709 struct task_smack *tsp;
1710 struct smack_known *okp;
1711 struct inode_smack *isp;
1712 struct superblock_smack *sbsp;
1721 if (unlikely(IS_PRIVATE(file_inode(file))))
1724 isp = file_inode(file)->i_security;
1725 if (isp->smk_mmap == NULL)
1727 sbsp = file_inode(file)->i_sb->s_security;
1728 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1729 isp->smk_mmap != sbsp->smk_root)
1731 mkp = isp->smk_mmap;
1733 tsp = current_security();
1734 skp = smk_of_current();
1739 * For each Smack rule associated with the subject
1740 * label verify that the SMACK64MMAP also has access
1741 * to that rule's object label.
1743 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1744 okp = srp->smk_object;
1746 * Matching labels always allows access.
1748 if (mkp->smk_known == okp->smk_known)
1751 * If there is a matching local rule take
1752 * that into account as well.
1754 may = smk_access_entry(srp->smk_subject->smk_known,
1758 may = srp->smk_access;
1760 may &= srp->smk_access;
1762 * If may is zero the SMACK64MMAP subject can't
1763 * possibly have less access.
1769 * Fetch the global list entry.
1770 * If there isn't one a SMACK64MMAP subject
1771 * can't have as much access as current.
1773 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1775 if (mmay == -ENOENT) {
1780 * If there is a local entry it modifies the
1781 * potential access, too.
1783 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1785 if (tmay != -ENOENT)
1789 * If there is any access available to current that is
1790 * not available to a SMACK64MMAP subject
1793 if ((may | mmay) != mmay) {
1805 * smack_file_set_fowner - set the file security blob value
1806 * @file: object in question
1809 static void smack_file_set_fowner(struct file *file)
1811 file->f_security = smk_of_current();
1815 * smack_file_send_sigiotask - Smack on sigio
1816 * @tsk: The target task
1817 * @fown: the object the signal come from
1820 * Allow a privileged task to get signals even if it shouldn't
1822 * Returns 0 if a subject with the object's smack could
1823 * write to the task, an error code otherwise.
1825 static int smack_file_send_sigiotask(struct task_struct *tsk,
1826 struct fown_struct *fown, int signum)
1828 struct smack_known *skp;
1829 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1830 const struct cred *tcred;
1833 struct smk_audit_info ad;
1836 * struct fown_struct is never outside the context of a struct file
1838 file = container_of(fown, struct file, f_owner);
1840 /* we don't log here as rc can be overriden */
1841 skp = file->f_security;
1842 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1843 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1846 tcred = __task_cred(tsk);
1847 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1851 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1852 smk_ad_setfield_u_tsk(&ad, tsk);
1853 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1858 * smack_file_receive - Smack file receive check
1861 * Returns 0 if current has access, error code otherwise
1863 static int smack_file_receive(struct file *file)
1867 struct smk_audit_info ad;
1868 struct inode *inode = file_inode(file);
1869 struct socket *sock;
1870 struct task_smack *tsp;
1871 struct socket_smack *ssp;
1873 if (unlikely(IS_PRIVATE(inode)))
1876 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1877 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1879 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1880 sock = SOCKET_I(inode);
1881 ssp = sock->sk->sk_security;
1882 tsp = current_security();
1884 * If the receiving process can't write to the
1885 * passed socket or if the passed socket can't
1886 * write to the receiving process don't accept
1887 * the passed socket.
1889 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1890 rc = smk_bu_file(file, may, rc);
1893 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1894 rc = smk_bu_file(file, may, rc);
1898 * This code relies on bitmasks.
1900 if (file->f_mode & FMODE_READ)
1902 if (file->f_mode & FMODE_WRITE)
1905 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1906 rc = smk_bu_file(file, may, rc);
1911 * smack_file_open - Smack dentry open processing
1913 * @cred: task credential
1915 * Set the security blob in the file structure.
1916 * Allow the open only if the task has read access. There are
1917 * many read operations (e.g. fstat) that you can do with an
1918 * fd even if you have the file open write-only.
1922 static int smack_file_open(struct file *file)
1924 struct task_smack *tsp = file->f_cred->security;
1925 struct inode *inode = file_inode(file);
1926 struct smk_audit_info ad;
1929 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1930 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1931 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1932 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1942 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1943 * @new: the new credentials
1944 * @gfp: the atomicity of any memory allocations
1946 * Prepare a blank set of credentials for modification. This must allocate all
1947 * the memory the LSM module might require such that cred_transfer() can
1948 * complete without error.
1950 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1952 struct task_smack *tsp;
1954 tsp = new_task_smack(NULL, NULL, gfp);
1958 cred->security = tsp;
1965 * smack_cred_free - "free" task-level security credentials
1966 * @cred: the credentials in question
1969 static void smack_cred_free(struct cred *cred)
1971 struct task_smack *tsp = cred->security;
1972 struct smack_rule *rp;
1973 struct list_head *l;
1974 struct list_head *n;
1978 cred->security = NULL;
1980 smk_destroy_label_list(&tsp->smk_relabel);
1982 list_for_each_safe(l, n, &tsp->smk_rules) {
1983 rp = list_entry(l, struct smack_rule, list);
1984 list_del(&rp->list);
1991 * smack_cred_prepare - prepare new set of credentials for modification
1992 * @new: the new credentials
1993 * @old: the original credentials
1994 * @gfp: the atomicity of any memory allocations
1996 * Prepare a new set of credentials for modification.
1998 static int smack_cred_prepare(struct cred *new, const struct cred *old,
2001 struct task_smack *old_tsp = old->security;
2002 struct task_smack *new_tsp;
2005 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
2006 if (new_tsp == NULL)
2009 new->security = new_tsp;
2011 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2015 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2024 * smack_cred_transfer - Transfer the old credentials to the new credentials
2025 * @new: the new credentials
2026 * @old: the original credentials
2028 * Fill in a set of blank credentials from another set of credentials.
2030 static void smack_cred_transfer(struct cred *new, const struct cred *old)
2032 struct task_smack *old_tsp = old->security;
2033 struct task_smack *new_tsp = new->security;
2035 new_tsp->smk_task = old_tsp->smk_task;
2036 new_tsp->smk_forked = old_tsp->smk_task;
2037 mutex_init(&new_tsp->smk_rules_lock);
2038 INIT_LIST_HEAD(&new_tsp->smk_rules);
2041 /* cbs copy rule list */
2045 * smack_cred_getsecid - get the secid corresponding to a creds structure
2046 * @c: the object creds
2047 * @secid: where to put the result
2049 * Sets the secid to contain a u32 version of the smack label.
2051 static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2053 struct smack_known *skp;
2056 skp = smk_of_task(c->security);
2057 *secid = skp->smk_secid;
2062 * smack_kernel_act_as - Set the subjective context in a set of credentials
2063 * @new: points to the set of credentials to be modified.
2064 * @secid: specifies the security ID to be set
2066 * Set the security data for a kernel service.
2068 static int smack_kernel_act_as(struct cred *new, u32 secid)
2070 struct task_smack *new_tsp = new->security;
2072 new_tsp->smk_task = smack_from_secid(secid);
2077 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2078 * @new: points to the set of credentials to be modified
2079 * @inode: points to the inode to use as a reference
2081 * Set the file creation context in a set of credentials to the same
2082 * as the objective context of the specified inode
2084 static int smack_kernel_create_files_as(struct cred *new,
2085 struct inode *inode)
2087 struct inode_smack *isp = inode->i_security;
2088 struct task_smack *tsp = new->security;
2090 tsp->smk_forked = isp->smk_inode;
2091 tsp->smk_task = tsp->smk_forked;
2096 * smk_curacc_on_task - helper to log task related access
2097 * @p: the task object
2098 * @access: the access requested
2099 * @caller: name of the calling function for audit
2101 * Return 0 if access is permitted
2103 static int smk_curacc_on_task(struct task_struct *p, int access,
2106 struct smk_audit_info ad;
2107 struct smack_known *skp = smk_of_task_struct(p);
2110 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2111 smk_ad_setfield_u_tsk(&ad, p);
2112 rc = smk_curacc(skp, access, &ad);
2113 rc = smk_bu_task(p, access, rc);
2118 * smack_task_setpgid - Smack check on setting pgid
2119 * @p: the task object
2122 * Return 0 if write access is permitted
2124 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2126 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2130 * smack_task_getpgid - Smack access check for getpgid
2131 * @p: the object task
2133 * Returns 0 if current can read the object task, error code otherwise
2135 static int smack_task_getpgid(struct task_struct *p)
2137 return smk_curacc_on_task(p, MAY_READ, __func__);
2141 * smack_task_getsid - Smack access check for getsid
2142 * @p: the object task
2144 * Returns 0 if current can read the object task, error code otherwise
2146 static int smack_task_getsid(struct task_struct *p)
2148 return smk_curacc_on_task(p, MAY_READ, __func__);
2152 * smack_task_getsecid - get the secid of the task
2153 * @p: the object task
2154 * @secid: where to put the result
2156 * Sets the secid to contain a u32 version of the smack label.
2158 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2160 struct smack_known *skp = smk_of_task_struct(p);
2162 *secid = skp->smk_secid;
2166 * smack_task_setnice - Smack check on setting nice
2167 * @p: the task object
2170 * Return 0 if write access is permitted
2172 static int smack_task_setnice(struct task_struct *p, int nice)
2174 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2178 * smack_task_setioprio - Smack check on setting ioprio
2179 * @p: the task object
2182 * Return 0 if write access is permitted
2184 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2186 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2190 * smack_task_getioprio - Smack check on reading ioprio
2191 * @p: the task object
2193 * Return 0 if read access is permitted
2195 static int smack_task_getioprio(struct task_struct *p)
2197 return smk_curacc_on_task(p, MAY_READ, __func__);
2201 * smack_task_setscheduler - Smack check on setting scheduler
2202 * @p: the task object
2206 * Return 0 if read access is permitted
2208 static int smack_task_setscheduler(struct task_struct *p)
2210 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2214 * smack_task_getscheduler - Smack check on reading scheduler
2215 * @p: the task object
2217 * Return 0 if read access is permitted
2219 static int smack_task_getscheduler(struct task_struct *p)
2221 return smk_curacc_on_task(p, MAY_READ, __func__);
2225 * smack_task_movememory - Smack check on moving memory
2226 * @p: the task object
2228 * Return 0 if write access is permitted
2230 static int smack_task_movememory(struct task_struct *p)
2232 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2236 * smack_task_kill - Smack check on signal delivery
2237 * @p: the task object
2240 * @cred: identifies the cred to use in lieu of current's
2242 * Return 0 if write access is permitted
2245 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2246 int sig, const struct cred *cred)
2248 struct smk_audit_info ad;
2249 struct smack_known *skp;
2250 struct smack_known *tkp = smk_of_task_struct(p);
2254 return 0; /* null signal; existence test */
2256 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2257 smk_ad_setfield_u_tsk(&ad, p);
2259 * Sending a signal requires that the sender
2260 * can write the receiver.
2263 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2264 rc = smk_bu_task(p, MAY_DELIVER, rc);
2268 * If the cred isn't NULL we're dealing with some USB IO
2269 * specific behavior. This is not clean. For one thing
2270 * we can't take privilege into account.
2272 skp = smk_of_task(cred->security);
2273 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2274 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2279 * smack_task_to_inode - copy task smack into the inode blob
2280 * @p: task to copy from
2281 * @inode: inode to copy to
2283 * Sets the smack pointer in the inode security blob
2285 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2287 struct inode_smack *isp = inode->i_security;
2288 struct smack_known *skp = smk_of_task_struct(p);
2290 isp->smk_inode = skp;
2291 isp->smk_flags |= SMK_INODE_INSTANT;
2299 * smack_sk_alloc_security - Allocate a socket blob
2302 * @gfp_flags: memory allocation flags
2304 * Assign Smack pointers to current
2306 * Returns 0 on success, -ENOMEM is there's no memory
2308 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2310 struct smack_known *skp = smk_of_current();
2311 struct socket_smack *ssp;
2313 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2318 * Sockets created by kernel threads receive web label.
2320 if (unlikely(current->flags & PF_KTHREAD)) {
2321 ssp->smk_in = &smack_known_web;
2322 ssp->smk_out = &smack_known_web;
2327 ssp->smk_packet = NULL;
2329 sk->sk_security = ssp;
2335 * smack_sk_free_security - Free a socket blob
2338 * Clears the blob pointer
2340 static void smack_sk_free_security(struct sock *sk)
2342 #ifdef SMACK_IPV6_PORT_LABELING
2343 struct smk_port_label *spp;
2345 if (sk->sk_family == PF_INET6) {
2347 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2348 if (spp->smk_sock != sk)
2350 spp->smk_can_reuse = 1;
2356 kfree(sk->sk_security);
2360 * smack_ipv4host_label - check host based restrictions
2361 * @sip: the object end
2363 * looks for host based access restrictions
2365 * This version will only be appropriate for really small sets of single label
2366 * hosts. The caller is responsible for ensuring that the RCU read lock is
2367 * taken before calling this function.
2369 * Returns the label of the far end or NULL if it's not special.
2371 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2373 struct smk_net4addr *snp;
2374 struct in_addr *siap = &sip->sin_addr;
2376 if (siap->s_addr == 0)
2379 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2381 * we break after finding the first match because
2382 * the list is sorted from longest to shortest mask
2383 * so we have found the most specific match
2385 if (snp->smk_host.s_addr ==
2386 (siap->s_addr & snp->smk_mask.s_addr))
2387 return snp->smk_label;
2392 #if IS_ENABLED(CONFIG_IPV6)
2394 * smk_ipv6_localhost - Check for local ipv6 host address
2397 * Returns boolean true if this is the localhost address
2399 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2401 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2402 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2404 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2405 ntohs(be16p[7]) == 1)
2411 * smack_ipv6host_label - check host based restrictions
2412 * @sip: the object end
2414 * looks for host based access restrictions
2416 * This version will only be appropriate for really small sets of single label
2417 * hosts. The caller is responsible for ensuring that the RCU read lock is
2418 * taken before calling this function.
2420 * Returns the label of the far end or NULL if it's not special.
2422 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2424 struct smk_net6addr *snp;
2425 struct in6_addr *sap = &sip->sin6_addr;
2430 * It's local. Don't look for a host label.
2432 if (smk_ipv6_localhost(sip))
2435 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2437 * If the label is NULL the entry has
2438 * been renounced. Ignore it.
2440 if (snp->smk_label == NULL)
2443 * we break after finding the first match because
2444 * the list is sorted from longest to shortest mask
2445 * so we have found the most specific match
2447 for (found = 1, i = 0; i < 8; i++) {
2448 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2449 snp->smk_host.s6_addr16[i]) {
2455 return snp->smk_label;
2460 #endif /* CONFIG_IPV6 */
2463 * smack_netlabel - Set the secattr on a socket
2465 * @labeled: socket label scheme
2467 * Convert the outbound smack value (smk_out) to a
2468 * secattr and attach it to the socket.
2470 * Returns 0 on success or an error code
2472 static int smack_netlabel(struct sock *sk, int labeled)
2474 struct smack_known *skp;
2475 struct socket_smack *ssp = sk->sk_security;
2479 * Usually the netlabel code will handle changing the
2480 * packet labeling based on the label.
2481 * The case of a single label host is different, because
2482 * a single label host should never get a labeled packet
2483 * even though the label is usually associated with a packet
2487 bh_lock_sock_nested(sk);
2489 if (ssp->smk_out == smack_net_ambient ||
2490 labeled == SMACK_UNLABELED_SOCKET)
2491 netlbl_sock_delattr(sk);
2494 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2504 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2506 * @sap: the destination address
2508 * Set the correct secattr for the given socket based on the destination
2509 * address and perform any outbound access checks needed.
2511 * Returns 0 on success or an error code.
2514 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2516 struct smack_known *skp;
2519 struct smack_known *hkp;
2520 struct socket_smack *ssp = sk->sk_security;
2521 struct smk_audit_info ad;
2524 hkp = smack_ipv4host_label(sap);
2527 struct lsm_network_audit net;
2529 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2530 ad.a.u.net->family = sap->sin_family;
2531 ad.a.u.net->dport = sap->sin_port;
2532 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2534 sk_lbl = SMACK_UNLABELED_SOCKET;
2536 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2537 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2539 sk_lbl = SMACK_CIPSO_SOCKET;
2546 return smack_netlabel(sk, sk_lbl);
2549 #if IS_ENABLED(CONFIG_IPV6)
2551 * smk_ipv6_check - check Smack access
2552 * @subject: subject Smack label
2553 * @object: object Smack label
2555 * @act: the action being taken
2557 * Check an IPv6 access
2559 static int smk_ipv6_check(struct smack_known *subject,
2560 struct smack_known *object,
2561 struct sockaddr_in6 *address, int act)
2564 struct lsm_network_audit net;
2566 struct smk_audit_info ad;
2570 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2571 ad.a.u.net->family = PF_INET6;
2572 ad.a.u.net->dport = ntohs(address->sin6_port);
2573 if (act == SMK_RECEIVING)
2574 ad.a.u.net->v6info.saddr = address->sin6_addr;
2576 ad.a.u.net->v6info.daddr = address->sin6_addr;
2578 rc = smk_access(subject, object, MAY_WRITE, &ad);
2579 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2582 #endif /* CONFIG_IPV6 */
2584 #ifdef SMACK_IPV6_PORT_LABELING
2586 * smk_ipv6_port_label - Smack port access table management
2590 * Create or update the port list entry
2592 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2594 struct sock *sk = sock->sk;
2595 struct sockaddr_in6 *addr6;
2596 struct socket_smack *ssp = sock->sk->sk_security;
2597 struct smk_port_label *spp;
2598 unsigned short port = 0;
2600 if (address == NULL) {
2602 * This operation is changing the Smack information
2603 * on the bound socket. Take the changes to the port
2607 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2608 if (sk != spp->smk_sock)
2610 spp->smk_in = ssp->smk_in;
2611 spp->smk_out = ssp->smk_out;
2616 * A NULL address is only used for updating existing
2617 * bound entries. If there isn't one, it's OK.
2623 addr6 = (struct sockaddr_in6 *)address;
2624 port = ntohs(addr6->sin6_port);
2626 * This is a special case that is safely ignored.
2632 * Look for an existing port list entry.
2633 * This is an indication that a port is getting reused.
2636 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2637 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2639 if (spp->smk_can_reuse != 1) {
2643 spp->smk_port = port;
2645 spp->smk_in = ssp->smk_in;
2646 spp->smk_out = ssp->smk_out;
2647 spp->smk_can_reuse = 0;
2653 * A new port entry is required.
2655 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2659 spp->smk_port = port;
2661 spp->smk_in = ssp->smk_in;
2662 spp->smk_out = ssp->smk_out;
2663 spp->smk_sock_type = sock->type;
2664 spp->smk_can_reuse = 0;
2666 mutex_lock(&smack_ipv6_lock);
2667 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2668 mutex_unlock(&smack_ipv6_lock);
2673 * smk_ipv6_port_check - check Smack port access
2677 * Create or update the port list entry
2679 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2682 struct smk_port_label *spp;
2683 struct socket_smack *ssp = sk->sk_security;
2684 struct smack_known *skp = NULL;
2685 unsigned short port;
2686 struct smack_known *object;
2688 if (act == SMK_RECEIVING) {
2689 skp = smack_ipv6host_label(address);
2690 object = ssp->smk_in;
2693 object = smack_ipv6host_label(address);
2697 * The other end is a single label host.
2699 if (skp != NULL && object != NULL)
2700 return smk_ipv6_check(skp, object, address, act);
2702 skp = smack_net_ambient;
2704 object = smack_net_ambient;
2707 * It's remote, so port lookup does no good.
2709 if (!smk_ipv6_localhost(address))
2710 return smk_ipv6_check(skp, object, address, act);
2713 * It's local so the send check has to have passed.
2715 if (act == SMK_RECEIVING)
2718 port = ntohs(address->sin6_port);
2720 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2721 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2723 object = spp->smk_in;
2724 if (act == SMK_CONNECTING)
2725 ssp->smk_packet = spp->smk_out;
2730 return smk_ipv6_check(skp, object, address, act);
2732 #endif /* SMACK_IPV6_PORT_LABELING */
2735 * smack_inode_setsecurity - set smack xattrs
2736 * @inode: the object
2737 * @name: attribute name
2738 * @value: attribute value
2739 * @size: size of the attribute
2742 * Sets the named attribute in the appropriate blob
2744 * Returns 0 on success, or an error code
2746 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2747 const void *value, size_t size, int flags)
2749 struct smack_known *skp;
2750 struct inode_smack *nsp = inode->i_security;
2751 struct socket_smack *ssp;
2752 struct socket *sock;
2755 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2758 skp = smk_import_entry(value, size);
2760 return PTR_ERR(skp);
2762 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2763 nsp->smk_inode = skp;
2764 nsp->smk_flags |= SMK_INODE_INSTANT;
2768 * The rest of the Smack xattrs are only on sockets.
2770 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2773 sock = SOCKET_I(inode);
2774 if (sock == NULL || sock->sk == NULL)
2777 ssp = sock->sk->sk_security;
2779 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2781 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2783 if (sock->sk->sk_family == PF_INET) {
2784 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2787 "Smack: \"%s\" netlbl error %d.\n",
2793 #ifdef SMACK_IPV6_PORT_LABELING
2794 if (sock->sk->sk_family == PF_INET6)
2795 smk_ipv6_port_label(sock, NULL);
2802 * smack_socket_post_create - finish socket setup
2804 * @family: protocol family
2809 * Sets the netlabel information on the socket
2811 * Returns 0 on success, and error code otherwise
2813 static int smack_socket_post_create(struct socket *sock, int family,
2814 int type, int protocol, int kern)
2816 struct socket_smack *ssp;
2818 if (sock->sk == NULL)
2822 * Sockets created by kernel threads receive web label.
2824 if (unlikely(current->flags & PF_KTHREAD)) {
2825 ssp = sock->sk->sk_security;
2826 ssp->smk_in = &smack_known_web;
2827 ssp->smk_out = &smack_known_web;
2830 if (family != PF_INET)
2833 * Set the outbound netlbl.
2835 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2839 * smack_socket_socketpair - create socket pair
2840 * @socka: one socket
2841 * @sockb: another socket
2843 * Cross reference the peer labels for SO_PEERSEC
2845 * Returns 0 on success, and error code otherwise
2847 static int smack_socket_socketpair(struct socket *socka,
2848 struct socket *sockb)
2850 struct socket_smack *asp = socka->sk->sk_security;
2851 struct socket_smack *bsp = sockb->sk->sk_security;
2853 asp->smk_packet = bsp->smk_out;
2854 bsp->smk_packet = asp->smk_out;
2859 #ifdef SMACK_IPV6_PORT_LABELING
2861 * smack_socket_bind - record port binding information.
2863 * @address: the port address
2864 * @addrlen: size of the address
2866 * Records the label bound to a port.
2870 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2873 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2874 smk_ipv6_port_label(sock, address);
2877 #endif /* SMACK_IPV6_PORT_LABELING */
2880 * smack_socket_connect - connect access check
2882 * @sap: the other end
2883 * @addrlen: size of sap
2885 * Verifies that a connection may be possible
2887 * Returns 0 on success, and error code otherwise
2889 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2893 #if IS_ENABLED(CONFIG_IPV6)
2894 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2896 #ifdef SMACK_IPV6_SECMARK_LABELING
2897 struct smack_known *rsp;
2898 struct socket_smack *ssp;
2901 if (sock->sk == NULL)
2904 #ifdef SMACK_IPV6_SECMARK_LABELING
2905 ssp = sock->sk->sk_security;
2908 switch (sock->sk->sk_family) {
2910 if (addrlen < sizeof(struct sockaddr_in))
2912 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2915 if (addrlen < sizeof(struct sockaddr_in6))
2917 #ifdef SMACK_IPV6_SECMARK_LABELING
2918 rsp = smack_ipv6host_label(sip);
2920 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2923 #ifdef SMACK_IPV6_PORT_LABELING
2924 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2932 * smack_flags_to_may - convert S_ to MAY_ values
2933 * @flags: the S_ value
2935 * Returns the equivalent MAY_ value
2937 static int smack_flags_to_may(int flags)
2941 if (flags & S_IRUGO)
2943 if (flags & S_IWUGO)
2945 if (flags & S_IXUGO)
2952 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2957 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2959 struct smack_known *skp = smk_of_current();
2961 msg->security = skp;
2966 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2969 * Clears the blob pointer
2971 static void smack_msg_msg_free_security(struct msg_msg *msg)
2973 msg->security = NULL;
2977 * smack_of_ipc - the smack pointer for the ipc
2980 * Returns a pointer to the smack value
2982 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2984 return (struct smack_known *)isp->security;
2988 * smack_ipc_alloc_security - Set the security blob for ipc
2993 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2995 struct smack_known *skp = smk_of_current();
2997 isp->security = skp;
3002 * smack_ipc_free_security - Clear the security blob for ipc
3005 * Clears the blob pointer
3007 static void smack_ipc_free_security(struct kern_ipc_perm *isp)
3009 isp->security = NULL;
3013 * smk_curacc_shm : check if current has access on shm
3015 * @access : access requested
3017 * Returns 0 if current has the requested access, error code otherwise
3019 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
3021 struct smack_known *ssp = smack_of_ipc(isp);
3022 struct smk_audit_info ad;
3026 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3027 ad.a.u.ipc_id = isp->id;
3029 rc = smk_curacc(ssp, access, &ad);
3030 rc = smk_bu_current("shm", ssp, access, rc);
3035 * smack_shm_associate - Smack access check for shm
3037 * @shmflg: access requested
3039 * Returns 0 if current has the requested access, error code otherwise
3041 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3045 may = smack_flags_to_may(shmflg);
3046 return smk_curacc_shm(isp, may);
3050 * smack_shm_shmctl - Smack access check for shm
3052 * @cmd: what it wants to do
3054 * Returns 0 if current has the requested access, error code otherwise
3056 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3070 may = MAY_READWRITE;
3075 * System level information.
3081 return smk_curacc_shm(isp, may);
3085 * smack_shm_shmat - Smack access for shmat
3088 * @shmflg: access requested
3090 * Returns 0 if current has the requested access, error code otherwise
3092 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3097 may = smack_flags_to_may(shmflg);
3098 return smk_curacc_shm(ipc, may);
3102 * smk_curacc_sem : check if current has access on sem
3104 * @access : access requested
3106 * Returns 0 if current has the requested access, error code otherwise
3108 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3110 struct smack_known *ssp = smack_of_ipc(isp);
3111 struct smk_audit_info ad;
3115 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3116 ad.a.u.ipc_id = isp->id;
3118 rc = smk_curacc(ssp, access, &ad);
3119 rc = smk_bu_current("sem", ssp, access, rc);
3124 * smack_sem_associate - Smack access check for sem
3126 * @semflg: access requested
3128 * Returns 0 if current has the requested access, error code otherwise
3130 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3134 may = smack_flags_to_may(semflg);
3135 return smk_curacc_sem(isp, may);
3139 * smack_sem_shmctl - Smack access check for sem
3141 * @cmd: what it wants to do
3143 * Returns 0 if current has the requested access, error code otherwise
3145 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3164 may = MAY_READWRITE;
3169 * System level information
3176 return smk_curacc_sem(isp, may);
3180 * smack_sem_semop - Smack checks of semaphore operations
3186 * Treated as read and write in all cases.
3188 * Returns 0 if access is allowed, error code otherwise
3190 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3191 unsigned nsops, int alter)
3193 return smk_curacc_sem(isp, MAY_READWRITE);
3197 * smk_curacc_msq : helper to check if current has access on msq
3199 * @access : access requested
3201 * return 0 if current has access, error otherwise
3203 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3205 struct smack_known *msp = smack_of_ipc(isp);
3206 struct smk_audit_info ad;
3210 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3211 ad.a.u.ipc_id = isp->id;
3213 rc = smk_curacc(msp, access, &ad);
3214 rc = smk_bu_current("msq", msp, access, rc);
3219 * smack_msg_queue_associate - Smack access check for msg_queue
3221 * @msqflg: access requested
3223 * Returns 0 if current has the requested access, error code otherwise
3225 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3229 may = smack_flags_to_may(msqflg);
3230 return smk_curacc_msq(isp, may);
3234 * smack_msg_queue_msgctl - Smack access check for msg_queue
3236 * @cmd: what it wants to do
3238 * Returns 0 if current has the requested access, error code otherwise
3240 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3252 may = MAY_READWRITE;
3257 * System level information
3264 return smk_curacc_msq(isp, may);
3268 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3271 * @msqflg: access requested
3273 * Returns 0 if current has the requested access, error code otherwise
3275 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3280 may = smack_flags_to_may(msqflg);
3281 return smk_curacc_msq(isp, may);
3285 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3292 * Returns 0 if current has read and write access, error code otherwise
3294 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3295 struct task_struct *target, long type, int mode)
3297 return smk_curacc_msq(isp, MAY_READWRITE);
3301 * smack_ipc_permission - Smack access for ipc_permission()
3302 * @ipp: the object permissions
3303 * @flag: access requested
3305 * Returns 0 if current has read and write access, error code otherwise
3307 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3309 struct smack_known *iskp = ipp->security;
3310 int may = smack_flags_to_may(flag);
3311 struct smk_audit_info ad;
3315 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3316 ad.a.u.ipc_id = ipp->id;
3318 rc = smk_curacc(iskp, may, &ad);
3319 rc = smk_bu_current("svipc", iskp, may, rc);
3324 * smack_ipc_getsecid - Extract smack security id
3325 * @ipp: the object permissions
3326 * @secid: where result will be saved
3328 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3330 struct smack_known *iskp = ipp->security;
3332 *secid = iskp->smk_secid;
3336 * smack_d_instantiate - Make sure the blob is correct on an inode
3337 * @opt_dentry: dentry where inode will be attached
3338 * @inode: the object
3340 * Set the inode's security blob if it hasn't been done already.
3342 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3344 struct super_block *sbp;
3345 struct superblock_smack *sbsp;
3346 struct inode_smack *isp;
3347 struct smack_known *skp;
3348 struct smack_known *ckp = smk_of_current();
3349 struct smack_known *final;
3350 char trattr[TRANS_TRUE_SIZE];
3358 isp = inode->i_security;
3360 mutex_lock(&isp->smk_lock);
3362 * If the inode is already instantiated
3363 * take the quick way out
3365 if (isp->smk_flags & SMK_INODE_INSTANT)
3369 sbsp = sbp->s_security;
3371 * We're going to use the superblock default label
3372 * if there's no label on the file.
3374 final = sbsp->smk_default;
3377 * If this is the root inode the superblock
3378 * may be in the process of initialization.
3379 * If that is the case use the root value out
3380 * of the superblock.
3382 if (opt_dentry->d_parent == opt_dentry) {
3383 switch (sbp->s_magic) {
3384 case CGROUP_SUPER_MAGIC:
3385 case CGROUP2_SUPER_MAGIC:
3387 * The cgroup filesystem is never mounted,
3388 * so there's no opportunity to set the mount
3391 sbsp->smk_root = &smack_known_star;
3392 sbsp->smk_default = &smack_known_star;
3393 isp->smk_inode = sbsp->smk_root;
3397 * What about shmem/tmpfs anonymous files with dentry
3398 * obtained from d_alloc_pseudo()?
3400 isp->smk_inode = smk_of_current();
3403 isp->smk_inode = smk_of_current();
3407 * Socket access is controlled by the socket
3408 * structures associated with the task involved.
3410 isp->smk_inode = &smack_known_star;
3413 isp->smk_inode = sbsp->smk_root;
3416 isp->smk_flags |= SMK_INODE_INSTANT;
3421 * This is pretty hackish.
3422 * Casey says that we shouldn't have to do
3423 * file system specific code, but it does help
3424 * with keeping it simple.
3426 switch (sbp->s_magic) {
3428 case CGROUP_SUPER_MAGIC:
3429 case CGROUP2_SUPER_MAGIC:
3431 * Casey says that it's a little embarrassing
3432 * that the smack file system doesn't do
3433 * extended attributes.
3435 * Cgroupfs is special
3437 final = &smack_known_star;
3439 case DEVPTS_SUPER_MAGIC:
3441 * devpts seems content with the label of the task.
3442 * Programs that change smack have to treat the
3447 case PROC_SUPER_MAGIC:
3449 * Casey says procfs appears not to care.
3450 * The superblock default suffices.
3455 * Device labels should come from the filesystem,
3456 * but watch out, because they're volitile,
3457 * getting recreated on every reboot.
3459 final = &smack_known_star;
3463 * If a smack value has been set we want to use it,
3464 * but since tmpfs isn't giving us the opportunity
3465 * to set mount options simulate setting the
3466 * superblock default.
3470 * This isn't an understood special case.
3471 * Get the value from the xattr.
3475 * UNIX domain sockets use lower level socket data.
3477 if (S_ISSOCK(inode->i_mode)) {
3478 final = &smack_known_star;
3482 * No xattr support means, alas, no SMACK label.
3483 * Use the aforeapplied default.
3484 * It would be curious if the label of the task
3485 * does not match that assigned.
3487 if (!(inode->i_opflags & IOP_XATTR))
3490 * Get the dentry for xattr.
3492 dp = dget(opt_dentry);
3493 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3494 if (!IS_ERR_OR_NULL(skp))
3498 * Transmuting directory
3500 if (S_ISDIR(inode->i_mode)) {
3502 * If this is a new directory and the label was
3503 * transmuted when the inode was initialized
3504 * set the transmute attribute on the directory
3505 * and mark the inode.
3507 * If there is a transmute attribute on the
3508 * directory mark the inode.
3510 if (isp->smk_flags & SMK_INODE_CHANGED) {
3511 isp->smk_flags &= ~SMK_INODE_CHANGED;
3512 rc = __vfs_setxattr(dp, inode,
3513 XATTR_NAME_SMACKTRANSMUTE,
3514 TRANS_TRUE, TRANS_TRUE_SIZE,
3517 rc = __vfs_getxattr(dp, inode,
3518 XATTR_NAME_SMACKTRANSMUTE, trattr,
3520 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3521 TRANS_TRUE_SIZE) != 0)
3525 transflag = SMK_INODE_TRANSMUTE;
3528 * Don't let the exec or mmap label be "*" or "@".
3530 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3531 if (IS_ERR(skp) || skp == &smack_known_star ||
3532 skp == &smack_known_web)
3534 isp->smk_task = skp;
3536 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3537 if (IS_ERR(skp) || skp == &smack_known_star ||
3538 skp == &smack_known_web)
3540 isp->smk_mmap = skp;
3547 isp->smk_inode = ckp;
3549 isp->smk_inode = final;
3551 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3554 mutex_unlock(&isp->smk_lock);
3559 * smack_getprocattr - Smack process attribute access
3560 * @p: the object task
3561 * @name: the name of the attribute in /proc/.../attr
3562 * @value: where to put the result
3564 * Places a copy of the task Smack into value
3566 * Returns the length of the smack label or an error code
3568 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3570 struct smack_known *skp = smk_of_task_struct(p);
3574 if (strcmp(name, "current") != 0)
3577 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3587 * smack_setprocattr - Smack process attribute setting
3588 * @name: the name of the attribute in /proc/.../attr
3589 * @value: the value to set
3590 * @size: the size of the value
3592 * Sets the Smack value of the task. Only setting self
3593 * is permitted and only with privilege
3595 * Returns the length of the smack label or an error code
3597 static int smack_setprocattr(const char *name, void *value, size_t size)
3599 struct task_smack *tsp = current_security();
3601 struct smack_known *skp;
3602 struct smack_known_list_elem *sklep;
3605 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3608 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3611 if (strcmp(name, "current") != 0)
3614 skp = smk_import_entry(value, size);
3616 return PTR_ERR(skp);
3619 * No process is ever allowed the web ("@") label
3620 * and the star ("*") label.
3622 if (skp == &smack_known_web || skp == &smack_known_star)
3625 if (!smack_privileged(CAP_MAC_ADMIN)) {
3627 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3628 if (sklep->smk_label == skp) {
3636 new = prepare_creds();
3640 tsp = new->security;
3641 tsp->smk_task = skp;
3643 * process can change its label only once
3645 smk_destroy_label_list(&tsp->smk_relabel);
3652 * smack_unix_stream_connect - Smack access on UDS
3654 * @other: the other sock
3657 * Return 0 if a subject with the smack of sock could access
3658 * an object with the smack of other, otherwise an error code
3660 static int smack_unix_stream_connect(struct sock *sock,
3661 struct sock *other, struct sock *newsk)
3663 struct smack_known *skp;
3664 struct smack_known *okp;
3665 struct socket_smack *ssp = sock->sk_security;
3666 struct socket_smack *osp = other->sk_security;
3667 struct socket_smack *nsp = newsk->sk_security;
3668 struct smk_audit_info ad;
3671 struct lsm_network_audit net;
3674 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3678 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3679 smk_ad_setfield_u_net_sk(&ad, other);
3681 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3682 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3686 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3687 rc = smk_bu_note("UDS connect", okp, skp,
3693 * Cross reference the peer labels for SO_PEERSEC.
3696 nsp->smk_packet = ssp->smk_out;
3697 ssp->smk_packet = osp->smk_out;
3704 * smack_unix_may_send - Smack access on UDS
3706 * @other: the other socket
3708 * Return 0 if a subject with the smack of sock could access
3709 * an object with the smack of other, otherwise an error code
3711 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3713 struct socket_smack *ssp = sock->sk->sk_security;
3714 struct socket_smack *osp = other->sk->sk_security;
3715 struct smk_audit_info ad;
3719 struct lsm_network_audit net;
3721 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3722 smk_ad_setfield_u_net_sk(&ad, other->sk);
3725 if (smack_privileged(CAP_MAC_OVERRIDE))
3728 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3729 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3734 * smack_socket_sendmsg - Smack check based on destination host
3737 * @size: the size of the message
3739 * Return 0 if the current subject can write to the destination host.
3740 * For IPv4 this is only a question if the destination is a single label host.
3741 * For IPv6 this is a check against the label of the port.
3743 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3746 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3747 #if IS_ENABLED(CONFIG_IPV6)
3748 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3750 #ifdef SMACK_IPV6_SECMARK_LABELING
3751 struct socket_smack *ssp = sock->sk->sk_security;
3752 struct smack_known *rsp;
3757 * Perfectly reasonable for this to be NULL
3762 switch (sock->sk->sk_family) {
3764 rc = smack_netlabel_send(sock->sk, sip);
3767 #ifdef SMACK_IPV6_SECMARK_LABELING
3768 rsp = smack_ipv6host_label(sap);
3770 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3773 #ifdef SMACK_IPV6_PORT_LABELING
3774 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3782 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3783 * @sap: netlabel secattr
3784 * @ssp: socket security information
3786 * Returns a pointer to a Smack label entry found on the label list.
3788 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3789 struct socket_smack *ssp)
3791 struct smack_known *skp;
3796 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3798 * Looks like a CIPSO packet.
3799 * If there are flags but no level netlabel isn't
3800 * behaving the way we expect it to.
3802 * Look it up in the label table
3803 * Without guidance regarding the smack value
3804 * for the packet fall back on the network
3808 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3809 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3812 * Compare the catsets. Use the netlbl APIs.
3814 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3815 if ((skp->smk_netlabel.flags &
3816 NETLBL_SECATTR_MLS_CAT) == 0)
3820 for (acat = -1, kcat = -1; acat == kcat; ) {
3821 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3823 kcat = netlbl_catmap_walk(
3824 skp->smk_netlabel.attr.mls.cat,
3826 if (acat < 0 || kcat < 0)
3839 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3840 return &smack_known_web;
3841 return &smack_known_star;
3843 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3845 * Looks like a fallback, which gives us a secid.
3847 return smack_from_secid(sap->attr.secid);
3849 * Without guidance regarding the smack value
3850 * for the packet fall back on the network
3853 return smack_net_ambient;
3856 #if IS_ENABLED(CONFIG_IPV6)
3857 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3861 int proto = -EINVAL;
3862 struct ipv6hdr _ipv6h;
3863 struct ipv6hdr *ip6;
3865 struct tcphdr _tcph, *th;
3866 struct udphdr _udph, *uh;
3867 struct dccp_hdr _dccph, *dh;
3871 offset = skb_network_offset(skb);
3872 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3875 sip->sin6_addr = ip6->saddr;
3877 nexthdr = ip6->nexthdr;
3878 offset += sizeof(_ipv6h);
3879 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3886 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3888 sip->sin6_port = th->source;
3891 case IPPROTO_UDPLITE:
3892 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3894 sip->sin6_port = uh->source;
3897 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3899 sip->sin6_port = dh->dccph_sport;
3904 #endif /* CONFIG_IPV6 */
3907 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3911 * Returns 0 if the packet should be delivered, an error code otherwise
3913 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3915 struct netlbl_lsm_secattr secattr;
3916 struct socket_smack *ssp = sk->sk_security;
3917 struct smack_known *skp = NULL;
3919 struct smk_audit_info ad;
3920 u16 family = sk->sk_family;
3922 struct lsm_network_audit net;
3924 #if IS_ENABLED(CONFIG_IPV6)
3925 struct sockaddr_in6 sadd;
3928 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3930 #endif /* CONFIG_IPV6 */
3934 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3936 * If there is a secmark use it rather than the CIPSO label.
3937 * If there is no secmark fall back to CIPSO.
3938 * The secmark is assumed to reflect policy better.
3940 if (skb && skb->secmark != 0) {
3941 skp = smack_from_secid(skb->secmark);
3944 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3946 * Translate what netlabel gave us.
3948 netlbl_secattr_init(&secattr);
3950 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3952 skp = smack_from_secattr(&secattr, ssp);
3954 skp = smack_net_ambient;
3956 netlbl_secattr_destroy(&secattr);
3958 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3962 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3963 ad.a.u.net->family = family;
3964 ad.a.u.net->netif = skb->skb_iif;
3965 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3968 * Receiving a packet requires that the other end
3969 * be able to write here. Read access is not required.
3970 * This is the simplist possible security model
3973 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3974 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3977 netlbl_skbuff_err(skb, family, rc, 0);
3979 #if IS_ENABLED(CONFIG_IPV6)
3981 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3982 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3983 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3985 #ifdef SMACK_IPV6_SECMARK_LABELING
3986 if (skb && skb->secmark != 0)
3987 skp = smack_from_secid(skb->secmark);
3989 skp = smack_ipv6host_label(&sadd);
3991 skp = smack_net_ambient;
3993 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3994 ad.a.u.net->family = family;
3995 ad.a.u.net->netif = skb->skb_iif;
3996 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3997 #endif /* CONFIG_AUDIT */
3998 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3999 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4001 #endif /* SMACK_IPV6_SECMARK_LABELING */
4002 #ifdef SMACK_IPV6_PORT_LABELING
4003 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
4004 #endif /* SMACK_IPV6_PORT_LABELING */
4006 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4007 ICMPV6_ADM_PROHIBITED, 0);
4009 #endif /* CONFIG_IPV6 */
4016 * smack_socket_getpeersec_stream - pull in packet label
4018 * @optval: user's destination
4019 * @optlen: size thereof
4022 * returns zero on success, an error code otherwise
4024 static int smack_socket_getpeersec_stream(struct socket *sock,
4025 char __user *optval,
4026 int __user *optlen, unsigned len)
4028 struct socket_smack *ssp;
4033 ssp = sock->sk->sk_security;
4034 if (ssp->smk_packet != NULL) {
4035 rcp = ssp->smk_packet->smk_known;
4036 slen = strlen(rcp) + 1;
4041 else if (copy_to_user(optval, rcp, slen) != 0)
4044 if (put_user(slen, optlen) != 0)
4052 * smack_socket_getpeersec_dgram - pull in packet label
4053 * @sock: the peer socket
4055 * @secid: pointer to where to put the secid of the packet
4057 * Sets the netlabel socket state on sk from parent
4059 static int smack_socket_getpeersec_dgram(struct socket *sock,
4060 struct sk_buff *skb, u32 *secid)
4063 struct netlbl_lsm_secattr secattr;
4064 struct socket_smack *ssp = NULL;
4065 struct smack_known *skp;
4066 int family = PF_UNSPEC;
4067 u32 s = 0; /* 0 is the invalid secid */
4071 if (skb->protocol == htons(ETH_P_IP))
4073 #if IS_ENABLED(CONFIG_IPV6)
4074 else if (skb->protocol == htons(ETH_P_IPV6))
4076 #endif /* CONFIG_IPV6 */
4078 if (family == PF_UNSPEC && sock != NULL)
4079 family = sock->sk->sk_family;
4083 ssp = sock->sk->sk_security;
4084 s = ssp->smk_out->smk_secid;
4087 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4093 * Translate what netlabel gave us.
4095 if (sock != NULL && sock->sk != NULL)
4096 ssp = sock->sk->sk_security;
4097 netlbl_secattr_init(&secattr);
4098 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4100 skp = smack_from_secattr(&secattr, ssp);
4103 netlbl_secattr_destroy(&secattr);
4106 #ifdef SMACK_IPV6_SECMARK_LABELING
4118 * smack_sock_graft - Initialize a newly created socket with an existing sock
4120 * @parent: parent socket
4122 * Set the smk_{in,out} state of an existing sock based on the process that
4123 * is creating the new socket.
4125 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4127 struct socket_smack *ssp;
4128 struct smack_known *skp = smk_of_current();
4131 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4134 ssp = sk->sk_security;
4137 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4141 * smack_inet_conn_request - Smack access check on connect
4142 * @sk: socket involved
4146 * Returns 0 if a task with the packet label could write to
4147 * the socket, otherwise an error code
4149 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4150 struct request_sock *req)
4152 u16 family = sk->sk_family;
4153 struct smack_known *skp;
4154 struct socket_smack *ssp = sk->sk_security;
4155 struct netlbl_lsm_secattr secattr;
4156 struct sockaddr_in addr;
4158 struct smack_known *hskp;
4160 struct smk_audit_info ad;
4162 struct lsm_network_audit net;
4165 #if IS_ENABLED(CONFIG_IPV6)
4166 if (family == PF_INET6) {
4168 * Handle mapped IPv4 packets arriving
4169 * via IPv6 sockets. Don't set up netlabel
4170 * processing on IPv6.
4172 if (skb->protocol == htons(ETH_P_IP))
4177 #endif /* CONFIG_IPV6 */
4179 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4181 * If there is a secmark use it rather than the CIPSO label.
4182 * If there is no secmark fall back to CIPSO.
4183 * The secmark is assumed to reflect policy better.
4185 if (skb && skb->secmark != 0) {
4186 skp = smack_from_secid(skb->secmark);
4189 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4191 netlbl_secattr_init(&secattr);
4192 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4194 skp = smack_from_secattr(&secattr, ssp);
4196 skp = &smack_known_huh;
4197 netlbl_secattr_destroy(&secattr);
4199 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4204 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4205 ad.a.u.net->family = family;
4206 ad.a.u.net->netif = skb->skb_iif;
4207 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4210 * Receiving a packet requires that the other end be able to write
4211 * here. Read access is not required.
4213 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4214 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4219 * Save the peer's label in the request_sock so we can later setup
4220 * smk_packet in the child socket so that SO_PEERCRED can report it.
4222 req->peer_secid = skp->smk_secid;
4225 * We need to decide if we want to label the incoming connection here
4226 * if we do we only need to label the request_sock and the stack will
4227 * propagate the wire-label to the sock when it is created.
4230 addr.sin_addr.s_addr = hdr->saddr;
4232 hskp = smack_ipv4host_label(&addr);
4236 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4238 netlbl_req_delattr(req);
4244 * smack_inet_csk_clone - Copy the connection information to the new socket
4245 * @sk: the new socket
4246 * @req: the connection's request_sock
4248 * Transfer the connection's peer label to the newly created socket.
4250 static void smack_inet_csk_clone(struct sock *sk,
4251 const struct request_sock *req)
4253 struct socket_smack *ssp = sk->sk_security;
4254 struct smack_known *skp;
4256 if (req->peer_secid != 0) {
4257 skp = smack_from_secid(req->peer_secid);
4258 ssp->smk_packet = skp;
4260 ssp->smk_packet = NULL;
4264 * Key management security hooks
4266 * Casey has not tested key support very heavily.
4267 * The permission check is most likely too restrictive.
4268 * If you care about keys please have a look.
4273 * smack_key_alloc - Set the key security blob
4275 * @cred: the credentials to use
4278 * No allocation required
4282 static int smack_key_alloc(struct key *key, const struct cred *cred,
4283 unsigned long flags)
4285 struct smack_known *skp = smk_of_task(cred->security);
4287 key->security = skp;
4292 * smack_key_free - Clear the key security blob
4295 * Clear the blob pointer
4297 static void smack_key_free(struct key *key)
4299 key->security = NULL;
4303 * smack_key_permission - Smack access on a key
4304 * @key_ref: gets to the object
4305 * @cred: the credentials to use
4306 * @perm: requested key permissions
4308 * Return 0 if the task has read and write to the object,
4309 * an error code otherwise
4311 static int smack_key_permission(key_ref_t key_ref,
4312 const struct cred *cred, unsigned perm)
4315 struct smk_audit_info ad;
4316 struct smack_known *tkp = smk_of_task(cred->security);
4320 keyp = key_ref_to_ptr(key_ref);
4324 * If the key hasn't been initialized give it access so that
4327 if (keyp->security == NULL)
4330 * This should not occur
4335 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4339 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4340 ad.a.u.key_struct.key = keyp->serial;
4341 ad.a.u.key_struct.key_desc = keyp->description;
4343 if (perm & KEY_NEED_READ)
4345 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4346 request = MAY_WRITE;
4347 rc = smk_access(tkp, keyp->security, request, &ad);
4348 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4353 * smack_key_getsecurity - Smack label tagging the key
4354 * @key points to the key to be queried
4355 * @_buffer points to a pointer that should be set to point to the
4356 * resulting string (if no label or an error occurs).
4357 * Return the length of the string (including terminating NUL) or -ve if
4359 * May also return 0 (and a NULL buffer pointer) if there is no label.
4361 static int smack_key_getsecurity(struct key *key, char **_buffer)
4363 struct smack_known *skp = key->security;
4367 if (key->security == NULL) {
4372 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4375 length = strlen(copy) + 1;
4381 #endif /* CONFIG_KEYS */
4386 * Audit requires a unique representation of each Smack specific
4387 * rule. This unique representation is used to distinguish the
4388 * object to be audited from remaining kernel objects and also
4389 * works as a glue between the audit hooks.
4391 * Since repository entries are added but never deleted, we'll use
4392 * the smack_known label address related to the given audit rule as
4393 * the needed unique representation. This also better fits the smack
4394 * model where nearly everything is a label.
4399 * smack_audit_rule_init - Initialize a smack audit rule
4400 * @field: audit rule fields given from user-space (audit.h)
4401 * @op: required testing operator (=, !=, >, <, ...)
4402 * @rulestr: smack label to be audited
4403 * @vrule: pointer to save our own audit rule representation
4405 * Prepare to audit cases where (@field @op @rulestr) is true.
4406 * The label to be audited is created if necessay.
4408 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4410 struct smack_known *skp;
4411 char **rule = (char **)vrule;
4414 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4417 if (op != Audit_equal && op != Audit_not_equal)
4420 skp = smk_import_entry(rulestr, 0);
4422 return PTR_ERR(skp);
4424 *rule = skp->smk_known;
4430 * smack_audit_rule_known - Distinguish Smack audit rules
4431 * @krule: rule of interest, in Audit kernel representation format
4433 * This is used to filter Smack rules from remaining Audit ones.
4434 * If it's proved that this rule belongs to us, the
4435 * audit_rule_match hook will be called to do the final judgement.
4437 static int smack_audit_rule_known(struct audit_krule *krule)
4439 struct audit_field *f;
4442 for (i = 0; i < krule->field_count; i++) {
4443 f = &krule->fields[i];
4445 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4453 * smack_audit_rule_match - Audit given object ?
4454 * @secid: security id for identifying the object to test
4455 * @field: audit rule flags given from user-space
4456 * @op: required testing operator
4457 * @vrule: smack internal rule presentation
4458 * @actx: audit context associated with the check
4460 * The core Audit hook. It's used to take the decision of
4461 * whether to audit or not to audit a given object.
4463 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4464 struct audit_context *actx)
4466 struct smack_known *skp;
4469 if (unlikely(!rule)) {
4470 WARN_ONCE(1, "Smack: missing rule\n");
4474 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4477 skp = smack_from_secid(secid);
4480 * No need to do string comparisons. If a match occurs,
4481 * both pointers will point to the same smack_known
4484 if (op == Audit_equal)
4485 return (rule == skp->smk_known);
4486 if (op == Audit_not_equal)
4487 return (rule != skp->smk_known);
4493 * There is no need for a smack_audit_rule_free hook.
4494 * No memory was allocated.
4497 #endif /* CONFIG_AUDIT */
4500 * smack_ismaclabel - check if xattr @name references a smack MAC label
4501 * @name: Full xattr name to check.
4503 static int smack_ismaclabel(const char *name)
4505 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4510 * smack_secid_to_secctx - return the smack label for a secid
4511 * @secid: incoming integer
4512 * @secdata: destination
4513 * @seclen: how long it is
4515 * Exists for networking code.
4517 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4519 struct smack_known *skp = smack_from_secid(secid);
4522 *secdata = skp->smk_known;
4523 *seclen = strlen(skp->smk_known);
4528 * smack_secctx_to_secid - return the secid for a smack label
4529 * @secdata: smack label
4530 * @seclen: how long result is
4531 * @secid: outgoing integer
4533 * Exists for audit and networking code.
4535 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4537 struct smack_known *skp = smk_find_entry(secdata);
4540 *secid = skp->smk_secid;
4547 * There used to be a smack_release_secctx hook
4548 * that did nothing back when hooks were in a vector.
4549 * Now that there's a list such a hook adds cost.
4552 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4554 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4557 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4559 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4562 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4564 struct smack_known *skp = smk_of_inode(inode);
4566 *ctx = skp->smk_known;
4567 *ctxlen = strlen(skp->smk_known);
4571 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4574 struct task_smack *tsp;
4575 struct smack_known *skp;
4576 struct inode_smack *isp;
4577 struct cred *new_creds = *new;
4579 if (new_creds == NULL) {
4580 new_creds = prepare_creds();
4581 if (new_creds == NULL)
4585 tsp = new_creds->security;
4588 * Get label from overlay inode and set it in create_sid
4590 isp = d_inode(dentry->d_parent)->i_security;
4591 skp = isp->smk_inode;
4592 tsp->smk_task = skp;
4597 static int smack_inode_copy_up_xattr(const char *name)
4600 * Return 1 if this is the smack access Smack attribute.
4602 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4608 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4610 const struct cred *old,
4613 struct task_smack *otsp = old->security;
4614 struct task_smack *ntsp = new->security;
4615 struct inode_smack *isp;
4619 * Use the process credential unless all of
4620 * the transmuting criteria are met
4622 ntsp->smk_task = otsp->smk_task;
4625 * the attribute of the containing directory
4627 isp = d_inode(dentry->d_parent)->i_security;
4629 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4631 may = smk_access_entry(otsp->smk_task->smk_known,
4632 isp->smk_inode->smk_known,
4633 &otsp->smk_task->smk_rules);
4637 * If the directory is transmuting and the rule
4638 * providing access is transmuting use the containing
4639 * directory label instead of the process label.
4641 if (may > 0 && (may & MAY_TRANSMUTE))
4642 ntsp->smk_task = isp->smk_inode;
4647 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4648 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4649 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4650 LSM_HOOK_INIT(syslog, smack_syslog),
4652 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4653 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4654 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4655 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4656 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4657 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4658 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
4660 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4662 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4663 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4664 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4665 LSM_HOOK_INIT(inode_link, smack_inode_link),
4666 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4667 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4668 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4669 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4670 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4671 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4672 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4673 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4674 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4675 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4676 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4677 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4678 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4679 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4681 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4682 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4683 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4684 LSM_HOOK_INIT(file_lock, smack_file_lock),
4685 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4686 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4687 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4688 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4689 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4690 LSM_HOOK_INIT(file_receive, smack_file_receive),
4692 LSM_HOOK_INIT(file_open, smack_file_open),
4694 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4695 LSM_HOOK_INIT(cred_free, smack_cred_free),
4696 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4697 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4698 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4699 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4700 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4701 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4702 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4703 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4704 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4705 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4706 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4707 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4708 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4709 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4710 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4711 LSM_HOOK_INIT(task_kill, smack_task_kill),
4712 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4714 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4715 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4717 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4718 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4720 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4721 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
4722 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4723 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4724 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4725 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4727 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4728 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
4729 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4730 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4731 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4733 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4734 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
4735 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4736 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4737 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4739 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4741 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4742 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4744 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4745 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4747 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4748 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4749 #ifdef SMACK_IPV6_PORT_LABELING
4750 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4752 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4753 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4754 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4755 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4756 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4757 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4758 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4759 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4760 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4761 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4763 /* key management security hooks */
4765 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4766 LSM_HOOK_INIT(key_free, smack_key_free),
4767 LSM_HOOK_INIT(key_permission, smack_key_permission),
4768 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4769 #endif /* CONFIG_KEYS */
4773 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4774 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4775 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4776 #endif /* CONFIG_AUDIT */
4778 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4779 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4780 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4781 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4782 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4783 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4784 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4785 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4786 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4790 static __init void init_smack_known_list(void)
4793 * Initialize rule list locks
4795 mutex_init(&smack_known_huh.smk_rules_lock);
4796 mutex_init(&smack_known_hat.smk_rules_lock);
4797 mutex_init(&smack_known_floor.smk_rules_lock);
4798 mutex_init(&smack_known_star.smk_rules_lock);
4799 mutex_init(&smack_known_web.smk_rules_lock);
4801 * Initialize rule lists
4803 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4804 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4805 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4806 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4807 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4809 * Create the known labels list
4811 smk_insert_entry(&smack_known_huh);
4812 smk_insert_entry(&smack_known_hat);
4813 smk_insert_entry(&smack_known_star);
4814 smk_insert_entry(&smack_known_floor);
4815 smk_insert_entry(&smack_known_web);
4819 * smack_init - initialize the smack system
4823 static __init int smack_init(void)
4826 struct task_smack *tsp;
4828 if (!security_module_enable("smack"))
4831 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4832 if (!smack_inode_cache)
4835 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4838 kmem_cache_destroy(smack_inode_cache);
4844 pr_info("Smack: Initializing.\n");
4845 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4846 pr_info("Smack: Netfilter enabled.\n");
4848 #ifdef SMACK_IPV6_PORT_LABELING
4849 pr_info("Smack: IPv6 port labeling enabled.\n");
4851 #ifdef SMACK_IPV6_SECMARK_LABELING
4852 pr_info("Smack: IPv6 Netfilter enabled.\n");
4856 * Set the security state for the initial task.
4858 cred = (struct cred *) current->cred;
4859 cred->security = tsp;
4861 /* initialize the smack_known_list */
4862 init_smack_known_list();
4867 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4873 * Smack requires early initialization in order to label
4874 * all processes and objects when they are created.
4876 DEFINE_LSM(smack) = {