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_NOFS);
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");
742 * smack_set_mnt_opts - set Smack specific mount options
743 * @sb: the file system superblock
744 * @opts: Smack mount options
745 * @kern_flags: mount option from kernel space or user space
746 * @set_kern_flags: where to store converted mount opts
748 * Returns 0 on success, an error code on failure
750 * Allow filesystems with binary mount data to explicitly set Smack mount
753 static int smack_set_mnt_opts(struct super_block *sb,
754 struct security_mnt_opts *opts,
755 unsigned long kern_flags,
756 unsigned long *set_kern_flags)
758 struct dentry *root = sb->s_root;
759 struct inode *inode = d_backing_inode(root);
760 struct superblock_smack *sp = sb->s_security;
761 struct inode_smack *isp;
762 struct smack_known *skp;
764 int num_opts = opts->num_mnt_opts;
767 if (sp->smk_flags & SMK_SB_INITIALIZED)
770 if (!smack_privileged(CAP_MAC_ADMIN)) {
772 * Unprivileged mounts don't get to specify Smack values.
777 * Unprivileged mounts get root and default from the caller.
779 skp = smk_of_current();
781 sp->smk_default = skp;
783 * For a handful of fs types with no user-controlled
784 * backing store it's okay to trust security labels
785 * in the filesystem. The rest are untrusted.
787 if (sb->s_user_ns != &init_user_ns &&
788 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
789 sb->s_magic != RAMFS_MAGIC) {
791 sp->smk_flags |= SMK_SB_UNTRUSTED;
795 sp->smk_flags |= SMK_SB_INITIALIZED;
797 for (i = 0; i < num_opts; i++) {
798 switch (opts->mnt_opts_flags[i]) {
800 skp = smk_import_entry(opts->mnt_opts[i], 0);
803 sp->smk_default = skp;
806 skp = smk_import_entry(opts->mnt_opts[i], 0);
812 skp = smk_import_entry(opts->mnt_opts[i], 0);
818 skp = smk_import_entry(opts->mnt_opts[i], 0);
824 skp = smk_import_entry(opts->mnt_opts[i], 0);
836 * Initialize the root inode.
838 isp = inode->i_security;
840 isp = new_inode_smack(sp->smk_root);
843 inode->i_security = isp;
845 isp->smk_inode = sp->smk_root;
848 isp->smk_flags |= SMK_INODE_TRANSMUTE;
854 * smack_sb_kern_mount - Smack specific mount processing
855 * @sb: the file system superblock
856 * @flags: the mount flags
857 * @data: the smack mount options
859 * Returns 0 on success, an error code on failure
861 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
864 char *options = data;
865 struct security_mnt_opts opts;
867 security_init_mnt_opts(&opts);
872 rc = smack_parse_opts_str(options, &opts);
877 rc = smack_set_mnt_opts(sb, &opts, 0, NULL);
880 security_free_mnt_opts(&opts);
885 * smack_sb_statfs - Smack check on statfs
886 * @dentry: identifies the file system in question
888 * Returns 0 if current can read the floor of the filesystem,
889 * and error code otherwise
891 static int smack_sb_statfs(struct dentry *dentry)
893 struct superblock_smack *sbp = dentry->d_sb->s_security;
895 struct smk_audit_info ad;
897 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
898 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
900 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
901 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
910 * smack_bprm_set_creds - set creds for exec
911 * @bprm: the exec information
913 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
915 static int smack_bprm_set_creds(struct linux_binprm *bprm)
917 struct inode *inode = file_inode(bprm->file);
918 struct task_smack *bsp = bprm->cred->security;
919 struct inode_smack *isp;
920 struct superblock_smack *sbsp;
923 if (bprm->called_set_creds)
926 isp = inode->i_security;
927 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
930 sbsp = inode->i_sb->s_security;
931 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
932 isp->smk_task != sbsp->smk_root)
935 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
936 struct task_struct *tracer;
940 tracer = ptrace_parent(current);
941 if (likely(tracer != NULL))
942 rc = smk_ptrace_rule_check(tracer,
951 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
954 bsp->smk_task = isp->smk_task;
955 bprm->per_clear |= PER_CLEAR_ON_SETID;
957 /* Decide if this is a secure exec. */
958 if (bsp->smk_task != bsp->smk_forked)
959 bprm->secureexec = 1;
969 * smack_inode_alloc_security - allocate an inode blob
970 * @inode: the inode in need of a blob
972 * Returns 0 if it gets a blob, -ENOMEM otherwise
974 static int smack_inode_alloc_security(struct inode *inode)
976 struct smack_known *skp = smk_of_current();
978 inode->i_security = new_inode_smack(skp);
979 if (inode->i_security == NULL)
985 * smack_inode_free_rcu - Free inode_smack blob from cache
986 * @head: the rcu_head for getting inode_smack pointer
988 * Call back function called from call_rcu() to free
989 * the i_security blob pointer in inode
991 static void smack_inode_free_rcu(struct rcu_head *head)
993 struct inode_smack *issp;
995 issp = container_of(head, struct inode_smack, smk_rcu);
996 kmem_cache_free(smack_inode_cache, issp);
1000 * smack_inode_free_security - free an inode blob using call_rcu()
1001 * @inode: the inode with a blob
1003 * Clears the blob pointer in inode using RCU
1005 static void smack_inode_free_security(struct inode *inode)
1007 struct inode_smack *issp = inode->i_security;
1010 * The inode may still be referenced in a path walk and
1011 * a call to smack_inode_permission() can be made
1012 * after smack_inode_free_security() is called.
1013 * To avoid race condition free the i_security via RCU
1014 * and leave the current inode->i_security pointer intact.
1015 * The inode will be freed after the RCU grace period too.
1017 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
1021 * smack_inode_init_security - copy out the smack from an inode
1022 * @inode: the newly created inode
1023 * @dir: containing directory object
1025 * @name: where to put the attribute name
1026 * @value: where to put the attribute value
1027 * @len: where to put the length of the attribute
1029 * Returns 0 if it all works out, -ENOMEM if there's no memory
1031 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
1032 const struct qstr *qstr, const char **name,
1033 void **value, size_t *len)
1035 struct task_smack *tsp = current_security();
1036 struct inode_smack *issp = inode->i_security;
1037 struct smack_known *skp = smk_of_task(tsp);
1038 struct smack_known *isp = smk_of_inode(inode);
1039 struct smack_known *dsp = smk_of_inode(dir);
1043 *name = XATTR_SMACK_SUFFIX;
1047 * If equal, transmuting already occurred in
1048 * smack_dentry_create_files_as(). No need to check again.
1050 if (tsp->smk_task != tsp->smk_transmuted) {
1052 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1058 * In addition to having smk_task equal to smk_transmuted,
1059 * if the access rule allows transmutation and the directory
1060 * requests transmutation then by all means transmute.
1061 * Mark the inode as changed.
1063 if ((tsp->smk_task == tsp->smk_transmuted) ||
1064 (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1065 smk_inode_transmutable(dir))) {
1067 * The caller of smack_dentry_create_files_as()
1068 * should have overridden the current cred, so the
1069 * inode label was already set correctly in
1070 * smack_inode_alloc_security().
1072 if (tsp->smk_task != tsp->smk_transmuted)
1074 issp->smk_flags |= SMK_INODE_CHANGED;
1077 *value = kstrdup(isp->smk_known, GFP_NOFS);
1081 *len = strlen(isp->smk_known);
1088 * smack_inode_link - Smack check on link
1089 * @old_dentry: the existing object
1091 * @new_dentry: the new object
1093 * Returns 0 if access is permitted, an error code otherwise
1095 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1096 struct dentry *new_dentry)
1098 struct smack_known *isp;
1099 struct smk_audit_info ad;
1102 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1103 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1105 isp = smk_of_inode(d_backing_inode(old_dentry));
1106 rc = smk_curacc(isp, MAY_WRITE, &ad);
1107 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1109 if (rc == 0 && d_is_positive(new_dentry)) {
1110 isp = smk_of_inode(d_backing_inode(new_dentry));
1111 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1112 rc = smk_curacc(isp, MAY_WRITE, &ad);
1113 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1120 * smack_inode_unlink - Smack check on inode deletion
1121 * @dir: containing directory object
1122 * @dentry: file to unlink
1124 * Returns 0 if current can write the containing directory
1125 * and the object, error code otherwise
1127 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1129 struct inode *ip = d_backing_inode(dentry);
1130 struct smk_audit_info ad;
1133 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1134 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1137 * You need write access to the thing you're unlinking
1139 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1140 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1143 * You also need write access to the containing directory
1145 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1146 smk_ad_setfield_u_fs_inode(&ad, dir);
1147 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1148 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1154 * smack_inode_rmdir - Smack check on directory deletion
1155 * @dir: containing directory object
1156 * @dentry: directory to unlink
1158 * Returns 0 if current can write the containing directory
1159 * and the directory, error code otherwise
1161 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1163 struct smk_audit_info ad;
1166 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1167 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1170 * You need write access to the thing you're removing
1172 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1173 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1176 * You also need write access to the containing directory
1178 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1179 smk_ad_setfield_u_fs_inode(&ad, dir);
1180 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1181 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1188 * smack_inode_rename - Smack check on rename
1189 * @old_inode: unused
1190 * @old_dentry: the old object
1191 * @new_inode: unused
1192 * @new_dentry: the new object
1194 * Read and write access is required on both the old and
1197 * Returns 0 if access is permitted, an error code otherwise
1199 static int smack_inode_rename(struct inode *old_inode,
1200 struct dentry *old_dentry,
1201 struct inode *new_inode,
1202 struct dentry *new_dentry)
1205 struct smack_known *isp;
1206 struct smk_audit_info ad;
1208 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1209 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1211 isp = smk_of_inode(d_backing_inode(old_dentry));
1212 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1213 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1215 if (rc == 0 && d_is_positive(new_dentry)) {
1216 isp = smk_of_inode(d_backing_inode(new_dentry));
1217 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1218 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1219 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1225 * smack_inode_permission - Smack version of permission()
1226 * @inode: the inode in question
1227 * @mask: the access requested
1229 * This is the important Smack hook.
1231 * Returns 0 if access is permitted, -EACCES otherwise
1233 static int smack_inode_permission(struct inode *inode, int mask)
1235 struct superblock_smack *sbsp = inode->i_sb->s_security;
1236 struct smk_audit_info ad;
1237 int no_block = mask & MAY_NOT_BLOCK;
1240 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1242 * No permission to check. Existence test. Yup, it's there.
1247 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1248 if (smk_of_inode(inode) != sbsp->smk_root)
1252 /* May be droppable after audit */
1255 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1256 smk_ad_setfield_u_fs_inode(&ad, inode);
1257 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1258 rc = smk_bu_inode(inode, mask, rc);
1263 * smack_inode_setattr - Smack check for setting attributes
1264 * @dentry: the object
1265 * @iattr: for the force flag
1267 * Returns 0 if access is permitted, an error code otherwise
1269 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1271 struct smk_audit_info ad;
1275 * Need to allow for clearing the setuid bit.
1277 if (iattr->ia_valid & ATTR_FORCE)
1279 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1280 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1282 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1283 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1288 * smack_inode_getattr - Smack check for getting attributes
1289 * @mnt: vfsmount of the object
1290 * @dentry: the object
1292 * Returns 0 if access is permitted, an error code otherwise
1294 static int smack_inode_getattr(const struct path *path)
1296 struct smk_audit_info ad;
1297 struct inode *inode = d_backing_inode(path->dentry);
1300 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1301 smk_ad_setfield_u_fs_path(&ad, *path);
1302 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1303 rc = smk_bu_inode(inode, MAY_READ, rc);
1308 * smack_inode_setxattr - Smack check for setting xattrs
1309 * @dentry: the object
1310 * @name: name of the attribute
1311 * @value: value of the attribute
1312 * @size: size of the value
1315 * This protects the Smack attribute explicitly.
1317 * Returns 0 if access is permitted, an error code otherwise
1319 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1320 const void *value, size_t size, int flags)
1322 struct smk_audit_info ad;
1323 struct smack_known *skp;
1325 int check_import = 0;
1330 * Check label validity here so import won't fail in post_setxattr
1332 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1333 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1334 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1337 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1338 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1342 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1344 if (size != TRANS_TRUE_SIZE ||
1345 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1348 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1350 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1353 if (rc == 0 && check_import) {
1354 skp = size ? smk_import_entry(value, size) : NULL;
1357 else if (skp == NULL || (check_star &&
1358 (skp == &smack_known_star || skp == &smack_known_web)))
1362 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1363 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1366 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1367 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1374 * smack_inode_post_setxattr - Apply the Smack update approved above
1376 * @name: attribute name
1377 * @value: attribute value
1378 * @size: attribute size
1381 * Set the pointer in the inode blob to the entry found
1382 * in the master label list.
1384 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1385 const void *value, size_t size, int flags)
1387 struct smack_known *skp;
1388 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
1390 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1391 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1395 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1396 skp = smk_import_entry(value, size);
1398 isp->smk_inode = skp;
1399 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1400 skp = smk_import_entry(value, size);
1402 isp->smk_task = skp;
1403 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1404 skp = smk_import_entry(value, size);
1406 isp->smk_mmap = skp;
1413 * smack_inode_getxattr - Smack check on getxattr
1414 * @dentry: the object
1417 * Returns 0 if access is permitted, an error code otherwise
1419 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1421 struct smk_audit_info ad;
1424 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1425 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1427 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1428 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1433 * smack_inode_removexattr - Smack check on removexattr
1434 * @dentry: the object
1435 * @name: name of the attribute
1437 * Removing the Smack attribute requires CAP_MAC_ADMIN
1439 * Returns 0 if access is permitted, an error code otherwise
1441 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1443 struct inode_smack *isp;
1444 struct smk_audit_info ad;
1447 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1448 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1449 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1450 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1451 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1452 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1453 if (!smack_privileged(CAP_MAC_ADMIN))
1456 rc = cap_inode_removexattr(dentry, name);
1461 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1462 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1464 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1465 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1469 isp = d_backing_inode(dentry)->i_security;
1471 * Don't do anything special for these.
1472 * XATTR_NAME_SMACKIPIN
1473 * XATTR_NAME_SMACKIPOUT
1475 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1476 struct super_block *sbp = dentry->d_sb;
1477 struct superblock_smack *sbsp = sbp->s_security;
1479 isp->smk_inode = sbsp->smk_default;
1480 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1481 isp->smk_task = NULL;
1482 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1483 isp->smk_mmap = NULL;
1484 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1485 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1491 * smack_inode_getsecurity - get smack xattrs
1492 * @inode: the object
1493 * @name: attribute name
1494 * @buffer: where to put the result
1495 * @alloc: duplicate memory
1497 * Returns the size of the attribute or an error code
1499 static int smack_inode_getsecurity(struct inode *inode,
1500 const char *name, void **buffer,
1503 struct socket_smack *ssp;
1504 struct socket *sock;
1505 struct super_block *sbp;
1506 struct inode *ip = (struct inode *)inode;
1507 struct smack_known *isp;
1508 struct inode_smack *ispp;
1512 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1513 isp = smk_of_inode(inode);
1514 } else if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) {
1515 ispp = inode->i_security;
1516 if (ispp->smk_flags & SMK_INODE_TRANSMUTE)
1522 * The rest of the Smack xattrs are only on sockets.
1525 if (sbp->s_magic != SOCKFS_MAGIC)
1528 sock = SOCKET_I(ip);
1529 if (sock == NULL || sock->sk == NULL)
1532 ssp = sock->sk->sk_security;
1534 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1536 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1543 label = isp->smk_known;
1545 label_len = strlen(label);
1548 *buffer = kstrdup(label, GFP_KERNEL);
1549 if (*buffer == NULL)
1558 * smack_inode_listsecurity - list the Smack attributes
1559 * @inode: the object
1560 * @buffer: where they go
1561 * @buffer_size: size of buffer
1563 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1566 int len = sizeof(XATTR_NAME_SMACK);
1568 if (buffer != NULL && len <= buffer_size)
1569 memcpy(buffer, XATTR_NAME_SMACK, len);
1575 * smack_inode_getsecid - Extract inode's security id
1576 * @inode: inode to extract the info from
1577 * @secid: where result will be saved
1579 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1581 struct smack_known *skp = smk_of_inode(inode);
1583 *secid = skp->smk_secid;
1591 * There is no smack_file_permission hook
1593 * Should access checks be done on each read or write?
1594 * UNICOS and SELinux say yes.
1595 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1597 * I'll say no for now. Smack does not do the frequent
1598 * label changing that SELinux does.
1602 * smack_file_alloc_security - assign a file security blob
1605 * The security blob for a file is a pointer to the master
1606 * label list, so no allocation is done.
1608 * f_security is the owner security information. It
1609 * isn't used on file access checks, it's for send_sigio.
1613 static int smack_file_alloc_security(struct file *file)
1615 struct smack_known *skp = smk_of_current();
1617 file->f_security = skp;
1622 * smack_file_free_security - clear a file security blob
1625 * The security blob for a file is a pointer to the master
1626 * label list, so no memory is freed.
1628 static void smack_file_free_security(struct file *file)
1630 file->f_security = NULL;
1634 * smack_file_ioctl - Smack check on ioctls
1639 * Relies heavily on the correct use of the ioctl command conventions.
1641 * Returns 0 if allowed, error code otherwise
1643 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1647 struct smk_audit_info ad;
1648 struct inode *inode = file_inode(file);
1650 if (unlikely(IS_PRIVATE(inode)))
1653 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1654 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1656 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1657 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1658 rc = smk_bu_file(file, MAY_WRITE, rc);
1661 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1662 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1663 rc = smk_bu_file(file, MAY_READ, rc);
1670 * smack_file_lock - Smack check on file locking
1674 * Returns 0 if current has lock access, error code otherwise
1676 static int smack_file_lock(struct file *file, unsigned int cmd)
1678 struct smk_audit_info ad;
1680 struct inode *inode = file_inode(file);
1682 if (unlikely(IS_PRIVATE(inode)))
1685 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1686 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1687 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1688 rc = smk_bu_file(file, MAY_LOCK, rc);
1693 * smack_file_fcntl - Smack check on fcntl
1695 * @cmd: what action to check
1698 * Generally these operations are harmless.
1699 * File locking operations present an obvious mechanism
1700 * for passing information, so they require write access.
1702 * Returns 0 if current has access, error code otherwise
1704 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1707 struct smk_audit_info ad;
1709 struct inode *inode = file_inode(file);
1711 if (unlikely(IS_PRIVATE(inode)))
1719 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1720 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1721 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1722 rc = smk_bu_file(file, MAY_LOCK, rc);
1726 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1727 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1728 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1729 rc = smk_bu_file(file, MAY_WRITE, rc);
1740 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1741 * if mapping anonymous memory.
1742 * @file contains the file structure for file to map (may be NULL).
1743 * @reqprot contains the protection requested by the application.
1744 * @prot contains the protection that will be applied by the kernel.
1745 * @flags contains the operational flags.
1746 * Return 0 if permission is granted.
1748 static int smack_mmap_file(struct file *file,
1749 unsigned long reqprot, unsigned long prot,
1750 unsigned long flags)
1752 struct smack_known *skp;
1753 struct smack_known *mkp;
1754 struct smack_rule *srp;
1755 struct task_smack *tsp;
1756 struct smack_known *okp;
1757 struct inode_smack *isp;
1758 struct superblock_smack *sbsp;
1767 if (unlikely(IS_PRIVATE(file_inode(file))))
1770 isp = file_inode(file)->i_security;
1771 if (isp->smk_mmap == NULL)
1773 sbsp = file_inode(file)->i_sb->s_security;
1774 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1775 isp->smk_mmap != sbsp->smk_root)
1777 mkp = isp->smk_mmap;
1779 tsp = current_security();
1780 skp = smk_of_current();
1785 * For each Smack rule associated with the subject
1786 * label verify that the SMACK64MMAP also has access
1787 * to that rule's object label.
1789 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1790 okp = srp->smk_object;
1792 * Matching labels always allows access.
1794 if (mkp->smk_known == okp->smk_known)
1797 * If there is a matching local rule take
1798 * that into account as well.
1800 may = smk_access_entry(srp->smk_subject->smk_known,
1804 may = srp->smk_access;
1806 may &= srp->smk_access;
1808 * If may is zero the SMACK64MMAP subject can't
1809 * possibly have less access.
1815 * Fetch the global list entry.
1816 * If there isn't one a SMACK64MMAP subject
1817 * can't have as much access as current.
1819 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1821 if (mmay == -ENOENT) {
1826 * If there is a local entry it modifies the
1827 * potential access, too.
1829 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1831 if (tmay != -ENOENT)
1835 * If there is any access available to current that is
1836 * not available to a SMACK64MMAP subject
1839 if ((may | mmay) != mmay) {
1851 * smack_file_set_fowner - set the file security blob value
1852 * @file: object in question
1855 static void smack_file_set_fowner(struct file *file)
1857 file->f_security = smk_of_current();
1861 * smack_file_send_sigiotask - Smack on sigio
1862 * @tsk: The target task
1863 * @fown: the object the signal come from
1866 * Allow a privileged task to get signals even if it shouldn't
1868 * Returns 0 if a subject with the object's smack could
1869 * write to the task, an error code otherwise.
1871 static int smack_file_send_sigiotask(struct task_struct *tsk,
1872 struct fown_struct *fown, int signum)
1874 struct smack_known *skp;
1875 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1876 const struct cred *tcred;
1879 struct smk_audit_info ad;
1882 * struct fown_struct is never outside the context of a struct file
1884 file = container_of(fown, struct file, f_owner);
1886 /* we don't log here as rc can be overriden */
1887 skp = file->f_security;
1888 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1889 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1892 tcred = __task_cred(tsk);
1893 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1897 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1898 smk_ad_setfield_u_tsk(&ad, tsk);
1899 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1904 * smack_file_receive - Smack file receive check
1907 * Returns 0 if current has access, error code otherwise
1909 static int smack_file_receive(struct file *file)
1913 struct smk_audit_info ad;
1914 struct inode *inode = file_inode(file);
1915 struct socket *sock;
1916 struct task_smack *tsp;
1917 struct socket_smack *ssp;
1919 if (unlikely(IS_PRIVATE(inode)))
1922 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1923 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1925 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1926 sock = SOCKET_I(inode);
1927 ssp = sock->sk->sk_security;
1928 tsp = current_security();
1930 * If the receiving process can't write to the
1931 * passed socket or if the passed socket can't
1932 * write to the receiving process don't accept
1933 * the passed socket.
1935 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1936 rc = smk_bu_file(file, may, rc);
1939 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1940 rc = smk_bu_file(file, may, rc);
1944 * This code relies on bitmasks.
1946 if (file->f_mode & FMODE_READ)
1948 if (file->f_mode & FMODE_WRITE)
1951 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1952 rc = smk_bu_file(file, may, rc);
1957 * smack_file_open - Smack dentry open processing
1959 * @cred: task credential
1961 * Set the security blob in the file structure.
1962 * Allow the open only if the task has read access. There are
1963 * many read operations (e.g. fstat) that you can do with an
1964 * fd even if you have the file open write-only.
1968 static int smack_file_open(struct file *file)
1970 struct task_smack *tsp = file->f_cred->security;
1971 struct inode *inode = file_inode(file);
1972 struct smk_audit_info ad;
1975 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1976 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1977 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1978 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1988 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1989 * @new: the new credentials
1990 * @gfp: the atomicity of any memory allocations
1992 * Prepare a blank set of credentials for modification. This must allocate all
1993 * the memory the LSM module might require such that cred_transfer() can
1994 * complete without error.
1996 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1998 struct task_smack *tsp;
2000 tsp = new_task_smack(NULL, NULL, gfp);
2004 cred->security = tsp;
2011 * smack_cred_free - "free" task-level security credentials
2012 * @cred: the credentials in question
2015 static void smack_cred_free(struct cred *cred)
2017 struct task_smack *tsp = cred->security;
2018 struct smack_rule *rp;
2019 struct list_head *l;
2020 struct list_head *n;
2024 cred->security = NULL;
2026 smk_destroy_label_list(&tsp->smk_relabel);
2028 list_for_each_safe(l, n, &tsp->smk_rules) {
2029 rp = list_entry(l, struct smack_rule, list);
2030 list_del(&rp->list);
2037 * smack_cred_prepare - prepare new set of credentials for modification
2038 * @new: the new credentials
2039 * @old: the original credentials
2040 * @gfp: the atomicity of any memory allocations
2042 * Prepare a new set of credentials for modification.
2044 static int smack_cred_prepare(struct cred *new, const struct cred *old,
2047 struct task_smack *old_tsp = old->security;
2048 struct task_smack *new_tsp;
2051 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
2052 if (new_tsp == NULL)
2055 new->security = new_tsp;
2057 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2061 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2070 * smack_cred_transfer - Transfer the old credentials to the new credentials
2071 * @new: the new credentials
2072 * @old: the original credentials
2074 * Fill in a set of blank credentials from another set of credentials.
2076 static void smack_cred_transfer(struct cred *new, const struct cred *old)
2078 struct task_smack *old_tsp = old->security;
2079 struct task_smack *new_tsp = new->security;
2081 new_tsp->smk_task = old_tsp->smk_task;
2082 new_tsp->smk_forked = old_tsp->smk_task;
2083 mutex_init(&new_tsp->smk_rules_lock);
2084 INIT_LIST_HEAD(&new_tsp->smk_rules);
2087 /* cbs copy rule list */
2091 * smack_cred_getsecid - get the secid corresponding to a creds structure
2092 * @c: the object creds
2093 * @secid: where to put the result
2095 * Sets the secid to contain a u32 version of the smack label.
2097 static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2099 struct smack_known *skp;
2102 skp = smk_of_task(c->security);
2103 *secid = skp->smk_secid;
2108 * smack_kernel_act_as - Set the subjective context in a set of credentials
2109 * @new: points to the set of credentials to be modified.
2110 * @secid: specifies the security ID to be set
2112 * Set the security data for a kernel service.
2114 static int smack_kernel_act_as(struct cred *new, u32 secid)
2116 struct task_smack *new_tsp = new->security;
2118 new_tsp->smk_task = smack_from_secid(secid);
2123 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2124 * @new: points to the set of credentials to be modified
2125 * @inode: points to the inode to use as a reference
2127 * Set the file creation context in a set of credentials to the same
2128 * as the objective context of the specified inode
2130 static int smack_kernel_create_files_as(struct cred *new,
2131 struct inode *inode)
2133 struct inode_smack *isp = inode->i_security;
2134 struct task_smack *tsp = new->security;
2136 tsp->smk_forked = isp->smk_inode;
2137 tsp->smk_task = tsp->smk_forked;
2142 * smk_curacc_on_task - helper to log task related access
2143 * @p: the task object
2144 * @access: the access requested
2145 * @caller: name of the calling function for audit
2147 * Return 0 if access is permitted
2149 static int smk_curacc_on_task(struct task_struct *p, int access,
2152 struct smk_audit_info ad;
2153 struct smack_known *skp = smk_of_task_struct(p);
2156 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2157 smk_ad_setfield_u_tsk(&ad, p);
2158 rc = smk_curacc(skp, access, &ad);
2159 rc = smk_bu_task(p, access, rc);
2164 * smack_task_setpgid - Smack check on setting pgid
2165 * @p: the task object
2168 * Return 0 if write access is permitted
2170 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2172 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2176 * smack_task_getpgid - Smack access check for getpgid
2177 * @p: the object task
2179 * Returns 0 if current can read the object task, error code otherwise
2181 static int smack_task_getpgid(struct task_struct *p)
2183 return smk_curacc_on_task(p, MAY_READ, __func__);
2187 * smack_task_getsid - Smack access check for getsid
2188 * @p: the object task
2190 * Returns 0 if current can read the object task, error code otherwise
2192 static int smack_task_getsid(struct task_struct *p)
2194 return smk_curacc_on_task(p, MAY_READ, __func__);
2198 * smack_task_getsecid - get the secid of the task
2199 * @p: the object task
2200 * @secid: where to put the result
2202 * Sets the secid to contain a u32 version of the smack label.
2204 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2206 struct smack_known *skp = smk_of_task_struct(p);
2208 *secid = skp->smk_secid;
2212 * smack_task_setnice - Smack check on setting nice
2213 * @p: the task object
2216 * Return 0 if write access is permitted
2218 static int smack_task_setnice(struct task_struct *p, int nice)
2220 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2224 * smack_task_setioprio - Smack check on setting ioprio
2225 * @p: the task object
2228 * Return 0 if write access is permitted
2230 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2232 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2236 * smack_task_getioprio - Smack check on reading ioprio
2237 * @p: the task object
2239 * Return 0 if read access is permitted
2241 static int smack_task_getioprio(struct task_struct *p)
2243 return smk_curacc_on_task(p, MAY_READ, __func__);
2247 * smack_task_setscheduler - Smack check on setting scheduler
2248 * @p: the task object
2252 * Return 0 if read access is permitted
2254 static int smack_task_setscheduler(struct task_struct *p)
2256 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2260 * smack_task_getscheduler - Smack check on reading scheduler
2261 * @p: the task object
2263 * Return 0 if read access is permitted
2265 static int smack_task_getscheduler(struct task_struct *p)
2267 return smk_curacc_on_task(p, MAY_READ, __func__);
2271 * smack_task_movememory - Smack check on moving memory
2272 * @p: the task object
2274 * Return 0 if write access is permitted
2276 static int smack_task_movememory(struct task_struct *p)
2278 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2282 * smack_task_kill - Smack check on signal delivery
2283 * @p: the task object
2286 * @cred: identifies the cred to use in lieu of current's
2288 * Return 0 if write access is permitted
2291 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2292 int sig, const struct cred *cred)
2294 struct smk_audit_info ad;
2295 struct smack_known *skp;
2296 struct smack_known *tkp = smk_of_task_struct(p);
2300 return 0; /* null signal; existence test */
2302 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2303 smk_ad_setfield_u_tsk(&ad, p);
2305 * Sending a signal requires that the sender
2306 * can write the receiver.
2309 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2310 rc = smk_bu_task(p, MAY_DELIVER, rc);
2314 * If the cred isn't NULL we're dealing with some USB IO
2315 * specific behavior. This is not clean. For one thing
2316 * we can't take privilege into account.
2318 skp = smk_of_task(cred->security);
2319 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2320 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2325 * smack_task_to_inode - copy task smack into the inode blob
2326 * @p: task to copy from
2327 * @inode: inode to copy to
2329 * Sets the smack pointer in the inode security blob
2331 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2333 struct inode_smack *isp = inode->i_security;
2334 struct smack_known *skp = smk_of_task_struct(p);
2336 isp->smk_inode = skp;
2337 isp->smk_flags |= SMK_INODE_INSTANT;
2345 * smack_sk_alloc_security - Allocate a socket blob
2348 * @gfp_flags: memory allocation flags
2350 * Assign Smack pointers to current
2352 * Returns 0 on success, -ENOMEM is there's no memory
2354 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2356 struct smack_known *skp = smk_of_current();
2357 struct socket_smack *ssp;
2359 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2364 * Sockets created by kernel threads receive web label.
2366 if (unlikely(current->flags & PF_KTHREAD)) {
2367 ssp->smk_in = &smack_known_web;
2368 ssp->smk_out = &smack_known_web;
2373 ssp->smk_packet = NULL;
2375 sk->sk_security = ssp;
2381 * smack_sk_free_security - Free a socket blob
2384 * Clears the blob pointer
2386 static void smack_sk_free_security(struct sock *sk)
2388 #ifdef SMACK_IPV6_PORT_LABELING
2389 struct smk_port_label *spp;
2391 if (sk->sk_family == PF_INET6) {
2393 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2394 if (spp->smk_sock != sk)
2396 spp->smk_can_reuse = 1;
2402 kfree(sk->sk_security);
2406 * smack_ipv4host_label - check host based restrictions
2407 * @sip: the object end
2409 * looks for host based access restrictions
2411 * This version will only be appropriate for really small sets of single label
2412 * hosts. The caller is responsible for ensuring that the RCU read lock is
2413 * taken before calling this function.
2415 * Returns the label of the far end or NULL if it's not special.
2417 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2419 struct smk_net4addr *snp;
2420 struct in_addr *siap = &sip->sin_addr;
2422 if (siap->s_addr == 0)
2425 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2427 * we break after finding the first match because
2428 * the list is sorted from longest to shortest mask
2429 * so we have found the most specific match
2431 if (snp->smk_host.s_addr ==
2432 (siap->s_addr & snp->smk_mask.s_addr))
2433 return snp->smk_label;
2438 #if IS_ENABLED(CONFIG_IPV6)
2440 * smk_ipv6_localhost - Check for local ipv6 host address
2443 * Returns boolean true if this is the localhost address
2445 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2447 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2448 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2450 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2451 ntohs(be16p[7]) == 1)
2457 * smack_ipv6host_label - check host based restrictions
2458 * @sip: the object end
2460 * looks for host based access restrictions
2462 * This version will only be appropriate for really small sets of single label
2463 * hosts. The caller is responsible for ensuring that the RCU read lock is
2464 * taken before calling this function.
2466 * Returns the label of the far end or NULL if it's not special.
2468 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2470 struct smk_net6addr *snp;
2471 struct in6_addr *sap = &sip->sin6_addr;
2476 * It's local. Don't look for a host label.
2478 if (smk_ipv6_localhost(sip))
2481 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2483 * If the label is NULL the entry has
2484 * been renounced. Ignore it.
2486 if (snp->smk_label == NULL)
2489 * we break after finding the first match because
2490 * the list is sorted from longest to shortest mask
2491 * so we have found the most specific match
2493 for (found = 1, i = 0; i < 8; i++) {
2494 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2495 snp->smk_host.s6_addr16[i]) {
2501 return snp->smk_label;
2506 #endif /* CONFIG_IPV6 */
2509 * smack_netlabel - Set the secattr on a socket
2511 * @labeled: socket label scheme
2513 * Convert the outbound smack value (smk_out) to a
2514 * secattr and attach it to the socket.
2516 * Returns 0 on success or an error code
2518 static int smack_netlabel(struct sock *sk, int labeled)
2520 struct smack_known *skp;
2521 struct socket_smack *ssp = sk->sk_security;
2525 * Usually the netlabel code will handle changing the
2526 * packet labeling based on the label.
2527 * The case of a single label host is different, because
2528 * a single label host should never get a labeled packet
2529 * even though the label is usually associated with a packet
2533 bh_lock_sock_nested(sk);
2535 if (ssp->smk_out == smack_net_ambient ||
2536 labeled == SMACK_UNLABELED_SOCKET)
2537 netlbl_sock_delattr(sk);
2540 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2550 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2552 * @sap: the destination address
2554 * Set the correct secattr for the given socket based on the destination
2555 * address and perform any outbound access checks needed.
2557 * Returns 0 on success or an error code.
2560 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2562 struct smack_known *skp;
2565 struct smack_known *hkp;
2566 struct socket_smack *ssp = sk->sk_security;
2567 struct smk_audit_info ad;
2570 hkp = smack_ipv4host_label(sap);
2573 struct lsm_network_audit net;
2575 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2576 ad.a.u.net->family = sap->sin_family;
2577 ad.a.u.net->dport = sap->sin_port;
2578 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2580 sk_lbl = SMACK_UNLABELED_SOCKET;
2582 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2583 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2585 sk_lbl = SMACK_CIPSO_SOCKET;
2592 return smack_netlabel(sk, sk_lbl);
2595 #if IS_ENABLED(CONFIG_IPV6)
2597 * smk_ipv6_check - check Smack access
2598 * @subject: subject Smack label
2599 * @object: object Smack label
2601 * @act: the action being taken
2603 * Check an IPv6 access
2605 static int smk_ipv6_check(struct smack_known *subject,
2606 struct smack_known *object,
2607 struct sockaddr_in6 *address, int act)
2610 struct lsm_network_audit net;
2612 struct smk_audit_info ad;
2616 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2617 ad.a.u.net->family = PF_INET6;
2618 ad.a.u.net->dport = address->sin6_port;
2619 if (act == SMK_RECEIVING)
2620 ad.a.u.net->v6info.saddr = address->sin6_addr;
2622 ad.a.u.net->v6info.daddr = address->sin6_addr;
2624 rc = smk_access(subject, object, MAY_WRITE, &ad);
2625 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2628 #endif /* CONFIG_IPV6 */
2630 #ifdef SMACK_IPV6_PORT_LABELING
2632 * smk_ipv6_port_label - Smack port access table management
2636 * Create or update the port list entry
2638 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2640 struct sock *sk = sock->sk;
2641 struct sockaddr_in6 *addr6;
2642 struct socket_smack *ssp = sock->sk->sk_security;
2643 struct smk_port_label *spp;
2644 unsigned short port = 0;
2646 if (address == NULL) {
2648 * This operation is changing the Smack information
2649 * on the bound socket. Take the changes to the port
2653 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2654 if (sk != spp->smk_sock)
2656 spp->smk_in = ssp->smk_in;
2657 spp->smk_out = ssp->smk_out;
2662 * A NULL address is only used for updating existing
2663 * bound entries. If there isn't one, it's OK.
2669 addr6 = (struct sockaddr_in6 *)address;
2670 port = ntohs(addr6->sin6_port);
2672 * This is a special case that is safely ignored.
2678 * Look for an existing port list entry.
2679 * This is an indication that a port is getting reused.
2682 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2683 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2685 if (spp->smk_can_reuse != 1) {
2689 spp->smk_port = port;
2691 spp->smk_in = ssp->smk_in;
2692 spp->smk_out = ssp->smk_out;
2693 spp->smk_can_reuse = 0;
2699 * A new port entry is required.
2701 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2705 spp->smk_port = port;
2707 spp->smk_in = ssp->smk_in;
2708 spp->smk_out = ssp->smk_out;
2709 spp->smk_sock_type = sock->type;
2710 spp->smk_can_reuse = 0;
2712 mutex_lock(&smack_ipv6_lock);
2713 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2714 mutex_unlock(&smack_ipv6_lock);
2719 * smk_ipv6_port_check - check Smack port access
2723 * Create or update the port list entry
2725 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2728 struct smk_port_label *spp;
2729 struct socket_smack *ssp = sk->sk_security;
2730 struct smack_known *skp = NULL;
2731 unsigned short port;
2732 struct smack_known *object;
2734 if (act == SMK_RECEIVING) {
2735 skp = smack_ipv6host_label(address);
2736 object = ssp->smk_in;
2739 object = smack_ipv6host_label(address);
2743 * The other end is a single label host.
2745 if (skp != NULL && object != NULL)
2746 return smk_ipv6_check(skp, object, address, act);
2748 skp = smack_net_ambient;
2750 object = smack_net_ambient;
2753 * It's remote, so port lookup does no good.
2755 if (!smk_ipv6_localhost(address))
2756 return smk_ipv6_check(skp, object, address, act);
2759 * It's local so the send check has to have passed.
2761 if (act == SMK_RECEIVING)
2764 port = ntohs(address->sin6_port);
2766 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2767 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2769 object = spp->smk_in;
2770 if (act == SMK_CONNECTING)
2771 ssp->smk_packet = spp->smk_out;
2776 return smk_ipv6_check(skp, object, address, act);
2778 #endif /* SMACK_IPV6_PORT_LABELING */
2781 * smack_inode_setsecurity - set smack xattrs
2782 * @inode: the object
2783 * @name: attribute name
2784 * @value: attribute value
2785 * @size: size of the attribute
2788 * Sets the named attribute in the appropriate blob
2790 * Returns 0 on success, or an error code
2792 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2793 const void *value, size_t size, int flags)
2795 struct smack_known *skp;
2796 struct inode_smack *nsp = inode->i_security;
2797 struct socket_smack *ssp;
2798 struct socket *sock;
2801 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2804 skp = smk_import_entry(value, size);
2806 return PTR_ERR(skp);
2808 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2809 nsp->smk_inode = skp;
2810 nsp->smk_flags |= SMK_INODE_INSTANT;
2814 * The rest of the Smack xattrs are only on sockets.
2816 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2819 sock = SOCKET_I(inode);
2820 if (sock == NULL || sock->sk == NULL)
2823 ssp = sock->sk->sk_security;
2825 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2827 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2829 if (sock->sk->sk_family == PF_INET) {
2830 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2833 "Smack: \"%s\" netlbl error %d.\n",
2839 #ifdef SMACK_IPV6_PORT_LABELING
2840 if (sock->sk->sk_family == PF_INET6)
2841 smk_ipv6_port_label(sock, NULL);
2848 * smack_socket_post_create - finish socket setup
2850 * @family: protocol family
2855 * Sets the netlabel information on the socket
2857 * Returns 0 on success, and error code otherwise
2859 static int smack_socket_post_create(struct socket *sock, int family,
2860 int type, int protocol, int kern)
2862 struct socket_smack *ssp;
2864 if (sock->sk == NULL)
2868 * Sockets created by kernel threads receive web label.
2870 if (unlikely(current->flags & PF_KTHREAD)) {
2871 ssp = sock->sk->sk_security;
2872 ssp->smk_in = &smack_known_web;
2873 ssp->smk_out = &smack_known_web;
2876 if (family != PF_INET)
2879 * Set the outbound netlbl.
2881 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2885 * smack_socket_socketpair - create socket pair
2886 * @socka: one socket
2887 * @sockb: another socket
2889 * Cross reference the peer labels for SO_PEERSEC
2891 * Returns 0 on success, and error code otherwise
2893 static int smack_socket_socketpair(struct socket *socka,
2894 struct socket *sockb)
2896 struct socket_smack *asp = socka->sk->sk_security;
2897 struct socket_smack *bsp = sockb->sk->sk_security;
2899 asp->smk_packet = bsp->smk_out;
2900 bsp->smk_packet = asp->smk_out;
2905 #ifdef SMACK_IPV6_PORT_LABELING
2907 * smack_socket_bind - record port binding information.
2909 * @address: the port address
2910 * @addrlen: size of the address
2912 * Records the label bound to a port.
2916 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2919 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2920 smk_ipv6_port_label(sock, address);
2923 #endif /* SMACK_IPV6_PORT_LABELING */
2926 * smack_socket_connect - connect access check
2928 * @sap: the other end
2929 * @addrlen: size of sap
2931 * Verifies that a connection may be possible
2933 * Returns 0 on success, and error code otherwise
2935 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2939 #if IS_ENABLED(CONFIG_IPV6)
2940 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2942 #ifdef SMACK_IPV6_SECMARK_LABELING
2943 struct smack_known *rsp;
2944 struct socket_smack *ssp;
2947 if (sock->sk == NULL)
2950 #ifdef SMACK_IPV6_SECMARK_LABELING
2951 ssp = sock->sk->sk_security;
2954 switch (sock->sk->sk_family) {
2956 if (addrlen < sizeof(struct sockaddr_in))
2958 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2961 if (addrlen < sizeof(struct sockaddr_in6))
2963 #ifdef SMACK_IPV6_SECMARK_LABELING
2964 rsp = smack_ipv6host_label(sip);
2966 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2969 #ifdef SMACK_IPV6_PORT_LABELING
2970 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2978 * smack_flags_to_may - convert S_ to MAY_ values
2979 * @flags: the S_ value
2981 * Returns the equivalent MAY_ value
2983 static int smack_flags_to_may(int flags)
2987 if (flags & S_IRUGO)
2989 if (flags & S_IWUGO)
2991 if (flags & S_IXUGO)
2998 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
3003 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
3005 struct smack_known *skp = smk_of_current();
3007 msg->security = skp;
3012 * smack_msg_msg_free_security - Clear the security blob for msg_msg
3015 * Clears the blob pointer
3017 static void smack_msg_msg_free_security(struct msg_msg *msg)
3019 msg->security = NULL;
3023 * smack_of_ipc - the smack pointer for the ipc
3026 * Returns a pointer to the smack value
3028 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
3030 return (struct smack_known *)isp->security;
3034 * smack_ipc_alloc_security - Set the security blob for ipc
3039 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
3041 struct smack_known *skp = smk_of_current();
3043 isp->security = skp;
3048 * smack_ipc_free_security - Clear the security blob for ipc
3051 * Clears the blob pointer
3053 static void smack_ipc_free_security(struct kern_ipc_perm *isp)
3055 isp->security = NULL;
3059 * smk_curacc_shm : check if current has access on shm
3061 * @access : access requested
3063 * Returns 0 if current has the requested access, error code otherwise
3065 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
3067 struct smack_known *ssp = smack_of_ipc(isp);
3068 struct smk_audit_info ad;
3072 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3073 ad.a.u.ipc_id = isp->id;
3075 rc = smk_curacc(ssp, access, &ad);
3076 rc = smk_bu_current("shm", ssp, access, rc);
3081 * smack_shm_associate - Smack access check for shm
3083 * @shmflg: access requested
3085 * Returns 0 if current has the requested access, error code otherwise
3087 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3091 may = smack_flags_to_may(shmflg);
3092 return smk_curacc_shm(isp, may);
3096 * smack_shm_shmctl - Smack access check for shm
3098 * @cmd: what it wants to do
3100 * Returns 0 if current has the requested access, error code otherwise
3102 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3116 may = MAY_READWRITE;
3121 * System level information.
3127 return smk_curacc_shm(isp, may);
3131 * smack_shm_shmat - Smack access for shmat
3134 * @shmflg: access requested
3136 * Returns 0 if current has the requested access, error code otherwise
3138 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3143 may = smack_flags_to_may(shmflg);
3144 return smk_curacc_shm(ipc, may);
3148 * smk_curacc_sem : check if current has access on sem
3150 * @access : access requested
3152 * Returns 0 if current has the requested access, error code otherwise
3154 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3156 struct smack_known *ssp = smack_of_ipc(isp);
3157 struct smk_audit_info ad;
3161 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3162 ad.a.u.ipc_id = isp->id;
3164 rc = smk_curacc(ssp, access, &ad);
3165 rc = smk_bu_current("sem", ssp, access, rc);
3170 * smack_sem_associate - Smack access check for sem
3172 * @semflg: access requested
3174 * Returns 0 if current has the requested access, error code otherwise
3176 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3180 may = smack_flags_to_may(semflg);
3181 return smk_curacc_sem(isp, may);
3185 * smack_sem_shmctl - Smack access check for sem
3187 * @cmd: what it wants to do
3189 * Returns 0 if current has the requested access, error code otherwise
3191 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3210 may = MAY_READWRITE;
3215 * System level information
3222 return smk_curacc_sem(isp, may);
3226 * smack_sem_semop - Smack checks of semaphore operations
3232 * Treated as read and write in all cases.
3234 * Returns 0 if access is allowed, error code otherwise
3236 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3237 unsigned nsops, int alter)
3239 return smk_curacc_sem(isp, MAY_READWRITE);
3243 * smk_curacc_msq : helper to check if current has access on msq
3245 * @access : access requested
3247 * return 0 if current has access, error otherwise
3249 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3251 struct smack_known *msp = smack_of_ipc(isp);
3252 struct smk_audit_info ad;
3256 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3257 ad.a.u.ipc_id = isp->id;
3259 rc = smk_curacc(msp, access, &ad);
3260 rc = smk_bu_current("msq", msp, access, rc);
3265 * smack_msg_queue_associate - Smack access check for msg_queue
3267 * @msqflg: access requested
3269 * Returns 0 if current has the requested access, error code otherwise
3271 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3275 may = smack_flags_to_may(msqflg);
3276 return smk_curacc_msq(isp, may);
3280 * smack_msg_queue_msgctl - Smack access check for msg_queue
3282 * @cmd: what it wants to do
3284 * Returns 0 if current has the requested access, error code otherwise
3286 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3298 may = MAY_READWRITE;
3303 * System level information
3310 return smk_curacc_msq(isp, may);
3314 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3317 * @msqflg: access requested
3319 * Returns 0 if current has the requested access, error code otherwise
3321 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3326 may = smack_flags_to_may(msqflg);
3327 return smk_curacc_msq(isp, may);
3331 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3338 * Returns 0 if current has read and write access, error code otherwise
3340 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3341 struct task_struct *target, long type, int mode)
3343 return smk_curacc_msq(isp, MAY_READWRITE);
3347 * smack_ipc_permission - Smack access for ipc_permission()
3348 * @ipp: the object permissions
3349 * @flag: access requested
3351 * Returns 0 if current has read and write access, error code otherwise
3353 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3355 struct smack_known *iskp = ipp->security;
3356 int may = smack_flags_to_may(flag);
3357 struct smk_audit_info ad;
3361 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3362 ad.a.u.ipc_id = ipp->id;
3364 rc = smk_curacc(iskp, may, &ad);
3365 rc = smk_bu_current("svipc", iskp, may, rc);
3370 * smack_ipc_getsecid - Extract smack security id
3371 * @ipp: the object permissions
3372 * @secid: where result will be saved
3374 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3376 struct smack_known *iskp = ipp->security;
3378 *secid = iskp->smk_secid;
3382 * smack_d_instantiate - Make sure the blob is correct on an inode
3383 * @opt_dentry: dentry where inode will be attached
3384 * @inode: the object
3386 * Set the inode's security blob if it hasn't been done already.
3388 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3390 struct super_block *sbp;
3391 struct superblock_smack *sbsp;
3392 struct inode_smack *isp;
3393 struct smack_known *skp;
3394 struct smack_known *ckp = smk_of_current();
3395 struct smack_known *final;
3396 char trattr[TRANS_TRUE_SIZE];
3404 isp = inode->i_security;
3406 mutex_lock(&isp->smk_lock);
3408 * If the inode is already instantiated
3409 * take the quick way out
3411 if (isp->smk_flags & SMK_INODE_INSTANT)
3415 sbsp = sbp->s_security;
3417 * We're going to use the superblock default label
3418 * if there's no label on the file.
3420 final = sbsp->smk_default;
3423 * If this is the root inode the superblock
3424 * may be in the process of initialization.
3425 * If that is the case use the root value out
3426 * of the superblock.
3428 if (opt_dentry->d_parent == opt_dentry) {
3429 switch (sbp->s_magic) {
3430 case CGROUP_SUPER_MAGIC:
3431 case CGROUP2_SUPER_MAGIC:
3433 * The cgroup filesystem is never mounted,
3434 * so there's no opportunity to set the mount
3437 sbsp->smk_root = &smack_known_star;
3438 sbsp->smk_default = &smack_known_star;
3439 isp->smk_inode = sbsp->smk_root;
3443 * What about shmem/tmpfs anonymous files with dentry
3444 * obtained from d_alloc_pseudo()?
3446 isp->smk_inode = smk_of_current();
3449 isp->smk_inode = smk_of_current();
3453 * Socket access is controlled by the socket
3454 * structures associated with the task involved.
3456 isp->smk_inode = &smack_known_star;
3459 isp->smk_inode = sbsp->smk_root;
3462 isp->smk_flags |= SMK_INODE_INSTANT;
3467 * This is pretty hackish.
3468 * Casey says that we shouldn't have to do
3469 * file system specific code, but it does help
3470 * with keeping it simple.
3472 switch (sbp->s_magic) {
3474 case CGROUP_SUPER_MAGIC:
3475 case CGROUP2_SUPER_MAGIC:
3477 * Casey says that it's a little embarrassing
3478 * that the smack file system doesn't do
3479 * extended attributes.
3481 * Cgroupfs is special
3483 final = &smack_known_star;
3485 case DEVPTS_SUPER_MAGIC:
3487 * devpts seems content with the label of the task.
3488 * Programs that change smack have to treat the
3493 case PROC_SUPER_MAGIC:
3495 * Casey says procfs appears not to care.
3496 * The superblock default suffices.
3501 * Device labels should come from the filesystem,
3502 * but watch out, because they're volitile,
3503 * getting recreated on every reboot.
3505 final = &smack_known_star;
3509 * If a smack value has been set we want to use it,
3510 * but since tmpfs isn't giving us the opportunity
3511 * to set mount options simulate setting the
3512 * superblock default.
3516 * This isn't an understood special case.
3517 * Get the value from the xattr.
3521 * UNIX domain sockets use lower level socket data.
3523 if (S_ISSOCK(inode->i_mode)) {
3524 final = &smack_known_star;
3528 * No xattr support means, alas, no SMACK label.
3529 * Use the aforeapplied default.
3530 * It would be curious if the label of the task
3531 * does not match that assigned.
3533 if (!(inode->i_opflags & IOP_XATTR))
3536 * Get the dentry for xattr.
3538 dp = dget(opt_dentry);
3539 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3540 if (!IS_ERR_OR_NULL(skp))
3544 * Transmuting directory
3546 if (S_ISDIR(inode->i_mode)) {
3548 * If this is a new directory and the label was
3549 * transmuted when the inode was initialized
3550 * set the transmute attribute on the directory
3551 * and mark the inode.
3553 * If there is a transmute attribute on the
3554 * directory mark the inode.
3556 if (isp->smk_flags & SMK_INODE_CHANGED) {
3557 isp->smk_flags &= ~SMK_INODE_CHANGED;
3558 rc = __vfs_setxattr(dp, inode,
3559 XATTR_NAME_SMACKTRANSMUTE,
3560 TRANS_TRUE, TRANS_TRUE_SIZE,
3563 rc = __vfs_getxattr(dp, inode,
3564 XATTR_NAME_SMACKTRANSMUTE, trattr,
3566 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3567 TRANS_TRUE_SIZE) != 0)
3571 transflag = SMK_INODE_TRANSMUTE;
3574 * Don't let the exec or mmap label be "*" or "@".
3576 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3577 if (IS_ERR(skp) || skp == &smack_known_star ||
3578 skp == &smack_known_web)
3580 isp->smk_task = skp;
3582 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3583 if (IS_ERR(skp) || skp == &smack_known_star ||
3584 skp == &smack_known_web)
3586 isp->smk_mmap = skp;
3593 isp->smk_inode = ckp;
3595 isp->smk_inode = final;
3597 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3600 mutex_unlock(&isp->smk_lock);
3605 * smack_getprocattr - Smack process attribute access
3606 * @p: the object task
3607 * @name: the name of the attribute in /proc/.../attr
3608 * @value: where to put the result
3610 * Places a copy of the task Smack into value
3612 * Returns the length of the smack label or an error code
3614 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3616 struct smack_known *skp = smk_of_task_struct(p);
3620 if (strcmp(name, "current") != 0)
3623 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3633 * smack_setprocattr - Smack process attribute setting
3634 * @name: the name of the attribute in /proc/.../attr
3635 * @value: the value to set
3636 * @size: the size of the value
3638 * Sets the Smack value of the task. Only setting self
3639 * is permitted and only with privilege
3641 * Returns the length of the smack label or an error code
3643 static int smack_setprocattr(const char *name, void *value, size_t size)
3645 struct task_smack *tsp = current_security();
3647 struct smack_known *skp;
3648 struct smack_known_list_elem *sklep;
3651 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3654 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3657 if (strcmp(name, "current") != 0)
3660 skp = smk_import_entry(value, size);
3662 return PTR_ERR(skp);
3665 * No process is ever allowed the web ("@") label
3666 * and the star ("*") label.
3668 if (skp == &smack_known_web || skp == &smack_known_star)
3671 if (!smack_privileged(CAP_MAC_ADMIN)) {
3673 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3674 if (sklep->smk_label == skp) {
3682 new = prepare_creds();
3686 tsp = new->security;
3687 tsp->smk_task = skp;
3689 * process can change its label only once
3691 smk_destroy_label_list(&tsp->smk_relabel);
3698 * smack_unix_stream_connect - Smack access on UDS
3700 * @other: the other sock
3703 * Return 0 if a subject with the smack of sock could access
3704 * an object with the smack of other, otherwise an error code
3706 static int smack_unix_stream_connect(struct sock *sock,
3707 struct sock *other, struct sock *newsk)
3709 struct smack_known *skp;
3710 struct smack_known *okp;
3711 struct socket_smack *ssp = sock->sk_security;
3712 struct socket_smack *osp = other->sk_security;
3713 struct socket_smack *nsp = newsk->sk_security;
3714 struct smk_audit_info ad;
3717 struct lsm_network_audit net;
3720 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3724 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3725 smk_ad_setfield_u_net_sk(&ad, other);
3727 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3728 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3732 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3733 rc = smk_bu_note("UDS connect", okp, skp,
3739 * Cross reference the peer labels for SO_PEERSEC.
3742 nsp->smk_packet = ssp->smk_out;
3743 ssp->smk_packet = osp->smk_out;
3750 * smack_unix_may_send - Smack access on UDS
3752 * @other: the other socket
3754 * Return 0 if a subject with the smack of sock could access
3755 * an object with the smack of other, otherwise an error code
3757 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3759 struct socket_smack *ssp = sock->sk->sk_security;
3760 struct socket_smack *osp = other->sk->sk_security;
3761 struct smk_audit_info ad;
3765 struct lsm_network_audit net;
3767 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3768 smk_ad_setfield_u_net_sk(&ad, other->sk);
3771 if (smack_privileged(CAP_MAC_OVERRIDE))
3774 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3775 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3780 * smack_socket_sendmsg - Smack check based on destination host
3783 * @size: the size of the message
3785 * Return 0 if the current subject can write to the destination host.
3786 * For IPv4 this is only a question if the destination is a single label host.
3787 * For IPv6 this is a check against the label of the port.
3789 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3792 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3793 #if IS_ENABLED(CONFIG_IPV6)
3794 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3796 #ifdef SMACK_IPV6_SECMARK_LABELING
3797 struct socket_smack *ssp = sock->sk->sk_security;
3798 struct smack_known *rsp;
3803 * Perfectly reasonable for this to be NULL
3808 switch (sock->sk->sk_family) {
3810 rc = smack_netlabel_send(sock->sk, sip);
3813 #ifdef SMACK_IPV6_SECMARK_LABELING
3814 rsp = smack_ipv6host_label(sap);
3816 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3819 #ifdef SMACK_IPV6_PORT_LABELING
3820 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3828 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3829 * @sap: netlabel secattr
3830 * @ssp: socket security information
3832 * Returns a pointer to a Smack label entry found on the label list.
3834 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3835 struct socket_smack *ssp)
3837 struct smack_known *skp;
3842 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3844 * Looks like a CIPSO packet.
3845 * If there are flags but no level netlabel isn't
3846 * behaving the way we expect it to.
3848 * Look it up in the label table
3849 * Without guidance regarding the smack value
3850 * for the packet fall back on the network
3854 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3855 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3858 * Compare the catsets. Use the netlbl APIs.
3860 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3861 if ((skp->smk_netlabel.flags &
3862 NETLBL_SECATTR_MLS_CAT) == 0)
3866 for (acat = -1, kcat = -1; acat == kcat; ) {
3867 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3869 kcat = netlbl_catmap_walk(
3870 skp->smk_netlabel.attr.mls.cat,
3872 if (acat < 0 || kcat < 0)
3885 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3886 return &smack_known_web;
3887 return &smack_known_star;
3889 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3891 * Looks like a fallback, which gives us a secid.
3893 return smack_from_secid(sap->attr.secid);
3895 * Without guidance regarding the smack value
3896 * for the packet fall back on the network
3899 return smack_net_ambient;
3902 #if IS_ENABLED(CONFIG_IPV6)
3903 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3907 int proto = -EINVAL;
3908 struct ipv6hdr _ipv6h;
3909 struct ipv6hdr *ip6;
3911 struct tcphdr _tcph, *th;
3912 struct udphdr _udph, *uh;
3913 struct dccp_hdr _dccph, *dh;
3917 offset = skb_network_offset(skb);
3918 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3921 sip->sin6_addr = ip6->saddr;
3923 nexthdr = ip6->nexthdr;
3924 offset += sizeof(_ipv6h);
3925 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3932 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3934 sip->sin6_port = th->source;
3937 case IPPROTO_UDPLITE:
3938 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3940 sip->sin6_port = uh->source;
3943 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3945 sip->sin6_port = dh->dccph_sport;
3950 #endif /* CONFIG_IPV6 */
3953 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3957 * Returns 0 if the packet should be delivered, an error code otherwise
3959 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3961 struct netlbl_lsm_secattr secattr;
3962 struct socket_smack *ssp = sk->sk_security;
3963 struct smack_known *skp = NULL;
3965 struct smk_audit_info ad;
3966 u16 family = sk->sk_family;
3968 struct lsm_network_audit net;
3970 #if IS_ENABLED(CONFIG_IPV6)
3971 struct sockaddr_in6 sadd;
3974 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3976 #endif /* CONFIG_IPV6 */
3980 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3982 * If there is a secmark use it rather than the CIPSO label.
3983 * If there is no secmark fall back to CIPSO.
3984 * The secmark is assumed to reflect policy better.
3986 if (skb && skb->secmark != 0) {
3987 skp = smack_from_secid(skb->secmark);
3990 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3992 * Translate what netlabel gave us.
3994 netlbl_secattr_init(&secattr);
3996 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3998 skp = smack_from_secattr(&secattr, ssp);
4000 skp = smack_net_ambient;
4002 netlbl_secattr_destroy(&secattr);
4004 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4008 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4009 ad.a.u.net->family = family;
4010 ad.a.u.net->netif = skb->skb_iif;
4011 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4014 * Receiving a packet requires that the other end
4015 * be able to write here. Read access is not required.
4016 * This is the simplist possible security model
4019 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4020 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
4023 netlbl_skbuff_err(skb, family, rc, 0);
4025 #if IS_ENABLED(CONFIG_IPV6)
4027 proto = smk_skb_to_addr_ipv6(skb, &sadd);
4028 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
4029 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
4031 #ifdef SMACK_IPV6_SECMARK_LABELING
4032 if (skb && skb->secmark != 0)
4033 skp = smack_from_secid(skb->secmark);
4035 skp = smack_ipv6host_label(&sadd);
4037 skp = smack_net_ambient;
4041 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4042 ad.a.u.net->family = family;
4043 ad.a.u.net->netif = skb->skb_iif;
4044 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4045 #endif /* CONFIG_AUDIT */
4046 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4047 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4049 #endif /* SMACK_IPV6_SECMARK_LABELING */
4050 #ifdef SMACK_IPV6_PORT_LABELING
4051 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
4052 #endif /* SMACK_IPV6_PORT_LABELING */
4054 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4055 ICMPV6_ADM_PROHIBITED, 0);
4057 #endif /* CONFIG_IPV6 */
4064 * smack_socket_getpeersec_stream - pull in packet label
4066 * @optval: user's destination
4067 * @optlen: size thereof
4070 * returns zero on success, an error code otherwise
4072 static int smack_socket_getpeersec_stream(struct socket *sock,
4073 char __user *optval,
4074 int __user *optlen, unsigned len)
4076 struct socket_smack *ssp;
4081 ssp = sock->sk->sk_security;
4082 if (ssp->smk_packet != NULL) {
4083 rcp = ssp->smk_packet->smk_known;
4084 slen = strlen(rcp) + 1;
4089 else if (copy_to_user(optval, rcp, slen) != 0)
4092 if (put_user(slen, optlen) != 0)
4100 * smack_socket_getpeersec_dgram - pull in packet label
4101 * @sock: the peer socket
4103 * @secid: pointer to where to put the secid of the packet
4105 * Sets the netlabel socket state on sk from parent
4107 static int smack_socket_getpeersec_dgram(struct socket *sock,
4108 struct sk_buff *skb, u32 *secid)
4111 struct netlbl_lsm_secattr secattr;
4112 struct socket_smack *ssp = NULL;
4113 struct smack_known *skp;
4114 int family = PF_UNSPEC;
4115 u32 s = 0; /* 0 is the invalid secid */
4119 if (skb->protocol == htons(ETH_P_IP))
4121 #if IS_ENABLED(CONFIG_IPV6)
4122 else if (skb->protocol == htons(ETH_P_IPV6))
4124 #endif /* CONFIG_IPV6 */
4126 if (family == PF_UNSPEC && sock != NULL)
4127 family = sock->sk->sk_family;
4131 ssp = sock->sk->sk_security;
4132 s = ssp->smk_out->smk_secid;
4135 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4141 * Translate what netlabel gave us.
4143 if (sock != NULL && sock->sk != NULL)
4144 ssp = sock->sk->sk_security;
4145 netlbl_secattr_init(&secattr);
4146 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4148 skp = smack_from_secattr(&secattr, ssp);
4151 netlbl_secattr_destroy(&secattr);
4154 #ifdef SMACK_IPV6_SECMARK_LABELING
4166 * smack_sock_graft - Initialize a newly created socket with an existing sock
4168 * @parent: parent socket
4170 * Set the smk_{in,out} state of an existing sock based on the process that
4171 * is creating the new socket.
4173 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4175 struct socket_smack *ssp;
4176 struct smack_known *skp = smk_of_current();
4179 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4182 ssp = sk->sk_security;
4185 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4189 * smack_inet_conn_request - Smack access check on connect
4190 * @sk: socket involved
4194 * Returns 0 if a task with the packet label could write to
4195 * the socket, otherwise an error code
4197 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4198 struct request_sock *req)
4200 u16 family = sk->sk_family;
4201 struct smack_known *skp;
4202 struct socket_smack *ssp = sk->sk_security;
4203 struct netlbl_lsm_secattr secattr;
4204 struct sockaddr_in addr;
4206 struct smack_known *hskp;
4208 struct smk_audit_info ad;
4210 struct lsm_network_audit net;
4213 #if IS_ENABLED(CONFIG_IPV6)
4214 if (family == PF_INET6) {
4216 * Handle mapped IPv4 packets arriving
4217 * via IPv6 sockets. Don't set up netlabel
4218 * processing on IPv6.
4220 if (skb->protocol == htons(ETH_P_IP))
4225 #endif /* CONFIG_IPV6 */
4227 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4229 * If there is a secmark use it rather than the CIPSO label.
4230 * If there is no secmark fall back to CIPSO.
4231 * The secmark is assumed to reflect policy better.
4233 if (skb && skb->secmark != 0) {
4234 skp = smack_from_secid(skb->secmark);
4237 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4239 netlbl_secattr_init(&secattr);
4240 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4242 skp = smack_from_secattr(&secattr, ssp);
4244 skp = &smack_known_huh;
4245 netlbl_secattr_destroy(&secattr);
4247 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4252 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4253 ad.a.u.net->family = family;
4254 ad.a.u.net->netif = skb->skb_iif;
4255 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4258 * Receiving a packet requires that the other end be able to write
4259 * here. Read access is not required.
4261 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4262 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4267 * Save the peer's label in the request_sock so we can later setup
4268 * smk_packet in the child socket so that SO_PEERCRED can report it.
4270 req->peer_secid = skp->smk_secid;
4273 * We need to decide if we want to label the incoming connection here
4274 * if we do we only need to label the request_sock and the stack will
4275 * propagate the wire-label to the sock when it is created.
4278 addr.sin_addr.s_addr = hdr->saddr;
4280 hskp = smack_ipv4host_label(&addr);
4284 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4286 netlbl_req_delattr(req);
4292 * smack_inet_csk_clone - Copy the connection information to the new socket
4293 * @sk: the new socket
4294 * @req: the connection's request_sock
4296 * Transfer the connection's peer label to the newly created socket.
4298 static void smack_inet_csk_clone(struct sock *sk,
4299 const struct request_sock *req)
4301 struct socket_smack *ssp = sk->sk_security;
4302 struct smack_known *skp;
4304 if (req->peer_secid != 0) {
4305 skp = smack_from_secid(req->peer_secid);
4306 ssp->smk_packet = skp;
4308 ssp->smk_packet = NULL;
4312 * Key management security hooks
4314 * Casey has not tested key support very heavily.
4315 * The permission check is most likely too restrictive.
4316 * If you care about keys please have a look.
4321 * smack_key_alloc - Set the key security blob
4323 * @cred: the credentials to use
4326 * No allocation required
4330 static int smack_key_alloc(struct key *key, const struct cred *cred,
4331 unsigned long flags)
4333 struct smack_known *skp = smk_of_task(cred->security);
4335 key->security = skp;
4340 * smack_key_free - Clear the key security blob
4343 * Clear the blob pointer
4345 static void smack_key_free(struct key *key)
4347 key->security = NULL;
4351 * smack_key_permission - Smack access on a key
4352 * @key_ref: gets to the object
4353 * @cred: the credentials to use
4354 * @perm: requested key permissions
4356 * Return 0 if the task has read and write to the object,
4357 * an error code otherwise
4359 static int smack_key_permission(key_ref_t key_ref,
4360 const struct cred *cred, unsigned perm)
4363 struct smk_audit_info ad;
4364 struct smack_known *tkp = smk_of_task(cred->security);
4369 * Validate requested permissions
4371 if (perm & ~KEY_NEED_ALL)
4374 keyp = key_ref_to_ptr(key_ref);
4378 * If the key hasn't been initialized give it access so that
4381 if (keyp->security == NULL)
4384 * This should not occur
4389 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4393 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4394 ad.a.u.key_struct.key = keyp->serial;
4395 ad.a.u.key_struct.key_desc = keyp->description;
4397 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4398 request |= MAY_READ;
4399 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4400 request |= MAY_WRITE;
4401 rc = smk_access(tkp, keyp->security, request, &ad);
4402 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4407 * smack_key_getsecurity - Smack label tagging the key
4408 * @key points to the key to be queried
4409 * @_buffer points to a pointer that should be set to point to the
4410 * resulting string (if no label or an error occurs).
4411 * Return the length of the string (including terminating NUL) or -ve if
4413 * May also return 0 (and a NULL buffer pointer) if there is no label.
4415 static int smack_key_getsecurity(struct key *key, char **_buffer)
4417 struct smack_known *skp = key->security;
4421 if (key->security == NULL) {
4426 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4429 length = strlen(copy) + 1;
4435 #endif /* CONFIG_KEYS */
4440 * Audit requires a unique representation of each Smack specific
4441 * rule. This unique representation is used to distinguish the
4442 * object to be audited from remaining kernel objects and also
4443 * works as a glue between the audit hooks.
4445 * Since repository entries are added but never deleted, we'll use
4446 * the smack_known label address related to the given audit rule as
4447 * the needed unique representation. This also better fits the smack
4448 * model where nearly everything is a label.
4453 * smack_audit_rule_init - Initialize a smack audit rule
4454 * @field: audit rule fields given from user-space (audit.h)
4455 * @op: required testing operator (=, !=, >, <, ...)
4456 * @rulestr: smack label to be audited
4457 * @vrule: pointer to save our own audit rule representation
4459 * Prepare to audit cases where (@field @op @rulestr) is true.
4460 * The label to be audited is created if necessay.
4462 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4464 struct smack_known *skp;
4465 char **rule = (char **)vrule;
4468 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4471 if (op != Audit_equal && op != Audit_not_equal)
4474 skp = smk_import_entry(rulestr, 0);
4476 return PTR_ERR(skp);
4478 *rule = skp->smk_known;
4484 * smack_audit_rule_known - Distinguish Smack audit rules
4485 * @krule: rule of interest, in Audit kernel representation format
4487 * This is used to filter Smack rules from remaining Audit ones.
4488 * If it's proved that this rule belongs to us, the
4489 * audit_rule_match hook will be called to do the final judgement.
4491 static int smack_audit_rule_known(struct audit_krule *krule)
4493 struct audit_field *f;
4496 for (i = 0; i < krule->field_count; i++) {
4497 f = &krule->fields[i];
4499 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4507 * smack_audit_rule_match - Audit given object ?
4508 * @secid: security id for identifying the object to test
4509 * @field: audit rule flags given from user-space
4510 * @op: required testing operator
4511 * @vrule: smack internal rule presentation
4512 * @actx: audit context associated with the check
4514 * The core Audit hook. It's used to take the decision of
4515 * whether to audit or not to audit a given object.
4517 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4518 struct audit_context *actx)
4520 struct smack_known *skp;
4523 if (unlikely(!rule)) {
4524 WARN_ONCE(1, "Smack: missing rule\n");
4528 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4531 skp = smack_from_secid(secid);
4534 * No need to do string comparisons. If a match occurs,
4535 * both pointers will point to the same smack_known
4538 if (op == Audit_equal)
4539 return (rule == skp->smk_known);
4540 if (op == Audit_not_equal)
4541 return (rule != skp->smk_known);
4547 * There is no need for a smack_audit_rule_free hook.
4548 * No memory was allocated.
4551 #endif /* CONFIG_AUDIT */
4554 * smack_ismaclabel - check if xattr @name references a smack MAC label
4555 * @name: Full xattr name to check.
4557 static int smack_ismaclabel(const char *name)
4559 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4564 * smack_secid_to_secctx - return the smack label for a secid
4565 * @secid: incoming integer
4566 * @secdata: destination
4567 * @seclen: how long it is
4569 * Exists for networking code.
4571 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4573 struct smack_known *skp = smack_from_secid(secid);
4576 *secdata = skp->smk_known;
4577 *seclen = strlen(skp->smk_known);
4582 * smack_secctx_to_secid - return the secid for a smack label
4583 * @secdata: smack label
4584 * @seclen: how long result is
4585 * @secid: outgoing integer
4587 * Exists for audit and networking code.
4589 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4591 struct smack_known *skp = smk_find_entry(secdata);
4594 *secid = skp->smk_secid;
4601 * There used to be a smack_release_secctx hook
4602 * that did nothing back when hooks were in a vector.
4603 * Now that there's a list such a hook adds cost.
4606 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4608 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4611 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4613 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4616 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4618 struct smack_known *skp = smk_of_inode(inode);
4620 *ctx = skp->smk_known;
4621 *ctxlen = strlen(skp->smk_known);
4625 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4628 struct task_smack *tsp;
4629 struct smack_known *skp;
4630 struct inode_smack *isp;
4631 struct cred *new_creds = *new;
4633 if (new_creds == NULL) {
4634 new_creds = prepare_creds();
4635 if (new_creds == NULL)
4639 tsp = new_creds->security;
4642 * Get label from overlay inode and set it in create_sid
4644 isp = d_inode(dentry)->i_security;
4645 skp = isp->smk_inode;
4646 tsp->smk_task = skp;
4651 static int smack_inode_copy_up_xattr(const char *name)
4654 * Return 1 if this is the smack access Smack attribute.
4656 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4662 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4664 const struct cred *old,
4667 struct task_smack *otsp = old->security;
4668 struct task_smack *ntsp = new->security;
4669 struct inode_smack *isp;
4673 * Use the process credential unless all of
4674 * the transmuting criteria are met
4676 ntsp->smk_task = otsp->smk_task;
4679 * the attribute of the containing directory
4681 isp = d_inode(dentry->d_parent)->i_security;
4683 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4685 may = smk_access_entry(otsp->smk_task->smk_known,
4686 isp->smk_inode->smk_known,
4687 &otsp->smk_task->smk_rules);
4691 * If the directory is transmuting and the rule
4692 * providing access is transmuting use the containing
4693 * directory label instead of the process label.
4695 if (may > 0 && (may & MAY_TRANSMUTE)) {
4696 ntsp->smk_task = isp->smk_inode;
4697 ntsp->smk_transmuted = ntsp->smk_task;
4703 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4704 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4705 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4706 LSM_HOOK_INIT(syslog, smack_syslog),
4708 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4709 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4710 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4711 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4712 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4713 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4714 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
4716 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4718 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4719 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4720 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4721 LSM_HOOK_INIT(inode_link, smack_inode_link),
4722 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4723 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4724 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4725 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4726 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4727 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4728 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4729 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4730 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4731 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4732 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4733 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4734 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4735 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4737 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4738 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4739 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4740 LSM_HOOK_INIT(file_lock, smack_file_lock),
4741 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4742 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4743 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4744 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4745 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4746 LSM_HOOK_INIT(file_receive, smack_file_receive),
4748 LSM_HOOK_INIT(file_open, smack_file_open),
4750 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4751 LSM_HOOK_INIT(cred_free, smack_cred_free),
4752 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4753 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4754 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4755 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4756 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4757 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4758 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4759 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4760 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4761 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4762 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4763 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4764 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4765 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4766 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4767 LSM_HOOK_INIT(task_kill, smack_task_kill),
4768 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4770 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4771 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4773 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4774 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4776 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4777 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
4778 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4779 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4780 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4781 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4783 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4784 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
4785 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4786 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4787 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4789 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4790 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
4791 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4792 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4793 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4795 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4797 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4798 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4800 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4801 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4803 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4804 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4805 #ifdef SMACK_IPV6_PORT_LABELING
4806 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4808 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4809 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4810 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4811 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4812 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4813 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4814 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4815 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4816 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4817 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4819 /* key management security hooks */
4821 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4822 LSM_HOOK_INIT(key_free, smack_key_free),
4823 LSM_HOOK_INIT(key_permission, smack_key_permission),
4824 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4825 #endif /* CONFIG_KEYS */
4829 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4830 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4831 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4832 #endif /* CONFIG_AUDIT */
4834 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4835 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4836 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4837 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4838 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4839 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4840 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4841 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4842 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4846 static __init void init_smack_known_list(void)
4849 * Initialize rule list locks
4851 mutex_init(&smack_known_huh.smk_rules_lock);
4852 mutex_init(&smack_known_hat.smk_rules_lock);
4853 mutex_init(&smack_known_floor.smk_rules_lock);
4854 mutex_init(&smack_known_star.smk_rules_lock);
4855 mutex_init(&smack_known_web.smk_rules_lock);
4857 * Initialize rule lists
4859 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4860 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4861 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4862 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4863 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4865 * Create the known labels list
4867 smk_insert_entry(&smack_known_huh);
4868 smk_insert_entry(&smack_known_hat);
4869 smk_insert_entry(&smack_known_star);
4870 smk_insert_entry(&smack_known_floor);
4871 smk_insert_entry(&smack_known_web);
4875 * smack_init - initialize the smack system
4879 static __init int smack_init(void)
4882 struct task_smack *tsp;
4884 if (!security_module_enable("smack"))
4887 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4888 if (!smack_inode_cache)
4891 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4894 kmem_cache_destroy(smack_inode_cache);
4900 pr_info("Smack: Initializing.\n");
4901 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4902 pr_info("Smack: Netfilter enabled.\n");
4904 #ifdef SMACK_IPV6_PORT_LABELING
4905 pr_info("Smack: IPv6 port labeling enabled.\n");
4907 #ifdef SMACK_IPV6_SECMARK_LABELING
4908 pr_info("Smack: IPv6 Netfilter enabled.\n");
4912 * Set the security state for the initial task.
4914 cred = (struct cred *) current->cred;
4915 cred->security = tsp;
4917 /* initialize the smack_known_list */
4918 init_smack_known_list();
4923 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4929 * Smack requires early initialization in order to label
4930 * all processes and objects when they are created.
4932 security_initcall(smack_init);