2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/dccp.h>
31 #include <linux/icmpv6.h>
32 #include <linux/slab.h>
33 #include <linux/mutex.h>
34 #include <linux/pipe_fs_i.h>
35 #include <net/cipso_ipv4.h>
38 #include <linux/audit.h>
39 #include <linux/magic.h>
40 #include <linux/dcache.h>
41 #include <linux/personality.h>
42 #include <linux/msg.h>
43 #include <linux/shm.h>
44 #include <linux/binfmts.h>
45 #include <linux/parser.h>
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 #ifdef SMACK_IPV6_PORT_LABELING
56 DEFINE_MUTEX(smack_ipv6_lock);
57 static LIST_HEAD(smk_ipv6_port_list);
59 static struct kmem_cache *smack_inode_cache;
62 static const match_table_t smk_mount_tokens = {
63 {Opt_fsdefault, SMK_FSDEFAULT "%s"},
64 {Opt_fsfloor, SMK_FSFLOOR "%s"},
65 {Opt_fshat, SMK_FSHAT "%s"},
66 {Opt_fsroot, SMK_FSROOT "%s"},
67 {Opt_fstransmute, SMK_FSTRANS "%s"},
71 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
72 static char *smk_bu_mess[] = {
73 "Bringup Error", /* Unused */
74 "Bringup", /* SMACK_BRINGUP_ALLOW */
75 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
76 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
79 static void smk_bu_mode(int mode, char *s)
89 if (mode & MAY_APPEND)
91 if (mode & MAY_TRANSMUTE)
101 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
102 static int smk_bu_note(char *note, struct smack_known *sskp,
103 struct smack_known *oskp, int mode, int rc)
105 char acc[SMK_NUM_ACCESS_TYPE + 1];
109 if (rc > SMACK_UNCONFINED_OBJECT)
112 smk_bu_mode(mode, acc);
113 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
114 sskp->smk_known, oskp->smk_known, acc, note);
118 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
121 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
122 static int smk_bu_current(char *note, struct smack_known *oskp,
125 struct task_smack *tsp = current_security();
126 char acc[SMK_NUM_ACCESS_TYPE + 1];
130 if (rc > SMACK_UNCONFINED_OBJECT)
133 smk_bu_mode(mode, acc);
134 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
135 tsp->smk_task->smk_known, oskp->smk_known,
136 acc, current->comm, note);
140 #define smk_bu_current(note, oskp, mode, RC) (RC)
143 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
144 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
146 struct task_smack *tsp = current_security();
147 struct smack_known *smk_task = smk_of_task_struct(otp);
148 char acc[SMK_NUM_ACCESS_TYPE + 1];
152 if (rc > SMACK_UNCONFINED_OBJECT)
155 smk_bu_mode(mode, acc);
156 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
157 tsp->smk_task->smk_known, smk_task->smk_known, acc,
158 current->comm, otp->comm);
162 #define smk_bu_task(otp, mode, RC) (RC)
165 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
166 static int smk_bu_inode(struct inode *inode, int mode, int rc)
168 struct task_smack *tsp = current_security();
169 struct inode_smack *isp = inode->i_security;
170 char acc[SMK_NUM_ACCESS_TYPE + 1];
172 if (isp->smk_flags & SMK_INODE_IMPURE)
173 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
174 inode->i_sb->s_id, inode->i_ino, current->comm);
178 if (rc > SMACK_UNCONFINED_OBJECT)
180 if (rc == SMACK_UNCONFINED_SUBJECT &&
181 (mode & (MAY_WRITE | MAY_APPEND)))
182 isp->smk_flags |= SMK_INODE_IMPURE;
184 smk_bu_mode(mode, acc);
186 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
187 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
188 inode->i_sb->s_id, inode->i_ino, current->comm);
192 #define smk_bu_inode(inode, mode, RC) (RC)
195 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
196 static int smk_bu_file(struct file *file, int mode, int rc)
198 struct task_smack *tsp = current_security();
199 struct smack_known *sskp = tsp->smk_task;
200 struct inode *inode = file_inode(file);
201 struct inode_smack *isp = inode->i_security;
202 char acc[SMK_NUM_ACCESS_TYPE + 1];
204 if (isp->smk_flags & SMK_INODE_IMPURE)
205 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
206 inode->i_sb->s_id, inode->i_ino, current->comm);
210 if (rc > SMACK_UNCONFINED_OBJECT)
213 smk_bu_mode(mode, acc);
214 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
215 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
216 inode->i_sb->s_id, inode->i_ino, file,
221 #define smk_bu_file(file, mode, RC) (RC)
224 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
225 static int smk_bu_credfile(const struct cred *cred, struct file *file,
228 struct task_smack *tsp = cred->security;
229 struct smack_known *sskp = tsp->smk_task;
230 struct inode *inode = file_inode(file);
231 struct inode_smack *isp = inode->i_security;
232 char acc[SMK_NUM_ACCESS_TYPE + 1];
234 if (isp->smk_flags & SMK_INODE_IMPURE)
235 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
236 inode->i_sb->s_id, inode->i_ino, current->comm);
240 if (rc > SMACK_UNCONFINED_OBJECT)
243 smk_bu_mode(mode, acc);
244 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
245 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
246 inode->i_sb->s_id, inode->i_ino, file,
251 #define smk_bu_credfile(cred, file, mode, RC) (RC)
255 * smk_fetch - Fetch the smack label from a file.
256 * @name: type of the label (attribute)
257 * @ip: a pointer to the inode
258 * @dp: a pointer to the dentry
260 * Returns a pointer to the master list entry for the Smack label,
261 * NULL if there was no label to fetch, or an error code.
263 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
268 struct smack_known *skp = NULL;
270 if (!(ip->i_opflags & IOP_XATTR))
271 return ERR_PTR(-EOPNOTSUPP);
273 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
275 return ERR_PTR(-ENOMEM);
277 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
283 skp = smk_import_entry(buffer, rc);
291 * new_inode_smack - allocate an inode security blob
292 * @skp: a pointer to the Smack label entry to use in the blob
294 * Returns the new blob or NULL if there's no memory available
296 static struct inode_smack *new_inode_smack(struct smack_known *skp)
298 struct inode_smack *isp;
300 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
304 isp->smk_inode = skp;
306 mutex_init(&isp->smk_lock);
312 * new_task_smack - allocate a task security blob
313 * @task: a pointer to the Smack label for the running task
314 * @forked: a pointer to the Smack label for the forked task
315 * @gfp: type of the memory for the allocation
317 * Returns the new blob or NULL if there's no memory available
319 static struct task_smack *new_task_smack(struct smack_known *task,
320 struct smack_known *forked, gfp_t gfp)
322 struct task_smack *tsp;
324 tsp = kzalloc(sizeof(struct task_smack), gfp);
328 tsp->smk_task = task;
329 tsp->smk_forked = forked;
330 INIT_LIST_HEAD(&tsp->smk_rules);
331 INIT_LIST_HEAD(&tsp->smk_relabel);
332 mutex_init(&tsp->smk_rules_lock);
338 * smk_copy_rules - copy a rule set
339 * @nhead: new rules header pointer
340 * @ohead: old rules header pointer
341 * @gfp: type of the memory for the allocation
343 * Returns 0 on success, -ENOMEM on error
345 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
348 struct smack_rule *nrp;
349 struct smack_rule *orp;
352 list_for_each_entry_rcu(orp, ohead, list) {
353 nrp = kzalloc(sizeof(struct smack_rule), gfp);
359 list_add_rcu(&nrp->list, nhead);
365 * smk_copy_relabel - copy smk_relabel labels list
366 * @nhead: new rules header pointer
367 * @ohead: old rules header pointer
368 * @gfp: type of the memory for the allocation
370 * Returns 0 on success, -ENOMEM on error
372 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
375 struct smack_known_list_elem *nklep;
376 struct smack_known_list_elem *oklep;
378 list_for_each_entry(oklep, ohead, list) {
379 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
381 smk_destroy_label_list(nhead);
384 nklep->smk_label = oklep->smk_label;
385 list_add(&nklep->list, nhead);
392 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
393 * @mode - input mode in form of PTRACE_MODE_*
395 * Returns a converted MAY_* mode usable by smack rules
397 static inline unsigned int smk_ptrace_mode(unsigned int mode)
399 if (mode & PTRACE_MODE_ATTACH)
400 return MAY_READWRITE;
401 if (mode & PTRACE_MODE_READ)
408 * smk_ptrace_rule_check - helper for ptrace access
409 * @tracer: tracer process
410 * @tracee_known: label entry of the process that's about to be traced
411 * @mode: ptrace attachment mode (PTRACE_MODE_*)
412 * @func: name of the function that called us, used for audit
414 * Returns 0 on access granted, -error on error
416 static int smk_ptrace_rule_check(struct task_struct *tracer,
417 struct smack_known *tracee_known,
418 unsigned int mode, const char *func)
421 struct smk_audit_info ad, *saip = NULL;
422 struct task_smack *tsp;
423 struct smack_known *tracer_known;
424 const struct cred *tracercred;
426 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
427 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
428 smk_ad_setfield_u_tsk(&ad, tracer);
433 tracercred = __task_cred(tracer);
434 tsp = tracercred->security;
435 tracer_known = smk_of_task(tsp);
437 if ((mode & PTRACE_MODE_ATTACH) &&
438 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
439 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
440 if (tracer_known->smk_known == tracee_known->smk_known)
442 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
444 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
450 smack_log(tracer_known->smk_known,
451 tracee_known->smk_known,
458 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
459 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
467 * We he, that is fun!
471 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
472 * @ctp: child task pointer
473 * @mode: ptrace attachment mode (PTRACE_MODE_*)
475 * Returns 0 if access is OK, an error code otherwise
477 * Do the capability checks.
479 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
481 struct smack_known *skp;
483 skp = smk_of_task_struct(ctp);
485 return smk_ptrace_rule_check(current, skp, mode, __func__);
489 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
490 * @ptp: parent task pointer
492 * Returns 0 if access is OK, an error code otherwise
494 * Do the capability checks, and require PTRACE_MODE_ATTACH.
496 static int smack_ptrace_traceme(struct task_struct *ptp)
499 struct smack_known *skp;
501 skp = smk_of_task(current_security());
503 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
508 * smack_syslog - Smack approval on syslog
509 * @type: message type
511 * Returns 0 on success, error code otherwise.
513 static int smack_syslog(int typefrom_file)
516 struct smack_known *skp = smk_of_current();
518 if (smack_privileged(CAP_MAC_OVERRIDE))
521 if (smack_syslog_label != NULL && smack_syslog_label != skp)
533 * smack_sb_alloc_security - allocate a superblock blob
534 * @sb: the superblock getting the blob
536 * Returns 0 on success or -ENOMEM on error.
538 static int smack_sb_alloc_security(struct super_block *sb)
540 struct superblock_smack *sbsp;
542 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
547 sbsp->smk_root = &smack_known_floor;
548 sbsp->smk_default = &smack_known_floor;
549 sbsp->smk_floor = &smack_known_floor;
550 sbsp->smk_hat = &smack_known_hat;
552 * SMK_SB_INITIALIZED will be zero from kzalloc.
554 sb->s_security = sbsp;
560 * smack_sb_free_security - free a superblock blob
561 * @sb: the superblock getting the blob
564 static void smack_sb_free_security(struct super_block *sb)
566 kfree(sb->s_security);
567 sb->s_security = NULL;
570 struct smack_mnt_opts {
571 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
574 static void smack_free_mnt_opts(void *mnt_opts)
576 struct smack_mnt_opts *opts = mnt_opts;
577 kfree(opts->fsdefault);
578 kfree(opts->fsfloor);
581 kfree(opts->fstransmute);
586 * smack_sb_copy_data - copy mount options data for processing
587 * @orig: where to start
588 * @smackopts: mount options string
590 * Returns 0 on success or -ENOMEM on error.
592 * Copy the Smack specific mount options out of the mount
595 static int smack_sb_copy_data(char *orig, char *smackopts)
597 char *cp, *commap, *otheropts, *dp;
599 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
600 if (otheropts == NULL)
603 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
604 if (strstr(cp, SMK_FSDEFAULT) == cp)
606 else if (strstr(cp, SMK_FSFLOOR) == cp)
608 else if (strstr(cp, SMK_FSHAT) == cp)
610 else if (strstr(cp, SMK_FSROOT) == cp)
612 else if (strstr(cp, SMK_FSTRANS) == cp)
617 commap = strchr(cp, ',');
626 strcpy(orig, otheropts);
627 free_page((unsigned long)otheropts);
633 * smack_parse_opts_str - parse Smack specific mount options
634 * @options: mount options string
635 * @opts: where to store converted mount opts
637 * Returns 0 on success or -ENOMEM on error.
639 * converts Smack specific mount options to generic security option format
641 static int smack_parse_opts_str(char *options,
644 struct smack_mnt_opts *opts = *mnt_opts;
652 while ((p = strsep(&options, ",")) != NULL) {
653 substring_t args[MAX_OPT_ARGS];
658 token = match_token(p, smk_mount_tokens, args);
661 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
670 opts->fsdefault = match_strdup(&args[0]);
671 if (!opts->fsdefault)
677 opts->fsfloor = match_strdup(&args[0]);
684 opts->fshat = match_strdup(&args[0]);
691 opts->fsroot = match_strdup(&args[0]);
695 case Opt_fstransmute:
696 if (opts->fstransmute)
698 opts->fstransmute = match_strdup(&args[0]);
699 if (!opts->fstransmute)
704 pr_warn("Smack: unknown mount option\n");
713 pr_warn("Smack: duplicate mount options\n");
717 smack_free_mnt_opts(opts);
721 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
723 char *s = (char *)get_zeroed_page(GFP_KERNEL);
728 err = smack_sb_copy_data(options, s);
730 err = smack_parse_opts_str(s, mnt_opts);
731 free_page((unsigned long)s);
736 * smack_set_mnt_opts - set Smack specific mount options
737 * @sb: the file system superblock
738 * @opts: Smack mount options
739 * @kern_flags: mount option from kernel space or user space
740 * @set_kern_flags: where to store converted mount opts
742 * Returns 0 on success, an error code on failure
744 * Allow filesystems with binary mount data to explicitly set Smack mount
747 static int smack_set_mnt_opts(struct super_block *sb,
749 unsigned long kern_flags,
750 unsigned long *set_kern_flags)
752 struct dentry *root = sb->s_root;
753 struct inode *inode = d_backing_inode(root);
754 struct superblock_smack *sp = sb->s_security;
755 struct inode_smack *isp;
756 struct smack_known *skp;
757 struct smack_mnt_opts *opts = mnt_opts;
758 bool transmute = false;
760 if (sp->smk_flags & SMK_SB_INITIALIZED)
763 if (!smack_privileged(CAP_MAC_ADMIN)) {
765 * Unprivileged mounts don't get to specify Smack values.
770 * Unprivileged mounts get root and default from the caller.
772 skp = smk_of_current();
774 sp->smk_default = skp;
776 * For a handful of fs types with no user-controlled
777 * backing store it's okay to trust security labels
778 * in the filesystem. The rest are untrusted.
780 if (sb->s_user_ns != &init_user_ns &&
781 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
782 sb->s_magic != RAMFS_MAGIC) {
784 sp->smk_flags |= SMK_SB_UNTRUSTED;
788 sp->smk_flags |= SMK_SB_INITIALIZED;
791 if (opts->fsdefault) {
792 skp = smk_import_entry(opts->fsdefault, 0);
795 sp->smk_default = skp;
798 skp = smk_import_entry(opts->fsfloor, 0);
804 skp = smk_import_entry(opts->fshat, 0);
810 skp = smk_import_entry(opts->fsroot, 0);
815 if (opts->fstransmute) {
816 skp = smk_import_entry(opts->fstransmute, 0);
825 * Initialize the root inode.
827 isp = inode->i_security;
829 isp = new_inode_smack(sp->smk_root);
832 inode->i_security = isp;
834 isp->smk_inode = sp->smk_root;
837 isp->smk_flags |= SMK_INODE_TRANSMUTE;
843 * smack_sb_statfs - Smack check on statfs
844 * @dentry: identifies the file system in question
846 * Returns 0 if current can read the floor of the filesystem,
847 * and error code otherwise
849 static int smack_sb_statfs(struct dentry *dentry)
851 struct superblock_smack *sbp = dentry->d_sb->s_security;
853 struct smk_audit_info ad;
855 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
856 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
858 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
859 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
868 * smack_bprm_set_creds - set creds for exec
869 * @bprm: the exec information
871 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
873 static int smack_bprm_set_creds(struct linux_binprm *bprm)
875 struct inode *inode = file_inode(bprm->file);
876 struct task_smack *bsp = bprm->cred->security;
877 struct inode_smack *isp;
878 struct superblock_smack *sbsp;
881 if (bprm->called_set_creds)
884 isp = inode->i_security;
885 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
888 sbsp = inode->i_sb->s_security;
889 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
890 isp->smk_task != sbsp->smk_root)
893 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
894 struct task_struct *tracer;
898 tracer = ptrace_parent(current);
899 if (likely(tracer != NULL))
900 rc = smk_ptrace_rule_check(tracer,
908 } else if (bprm->unsafe)
911 bsp->smk_task = isp->smk_task;
912 bprm->per_clear |= PER_CLEAR_ON_SETID;
914 /* Decide if this is a secure exec. */
915 if (bsp->smk_task != bsp->smk_forked)
916 bprm->secureexec = 1;
926 * smack_inode_alloc_security - allocate an inode blob
927 * @inode: the inode in need of a blob
929 * Returns 0 if it gets a blob, -ENOMEM otherwise
931 static int smack_inode_alloc_security(struct inode *inode)
933 struct smack_known *skp = smk_of_current();
935 inode->i_security = new_inode_smack(skp);
936 if (inode->i_security == NULL)
942 * smack_inode_free_rcu - Free inode_smack blob from cache
943 * @head: the rcu_head for getting inode_smack pointer
945 * Call back function called from call_rcu() to free
946 * the i_security blob pointer in inode
948 static void smack_inode_free_rcu(struct rcu_head *head)
950 struct inode_smack *issp;
952 issp = container_of(head, struct inode_smack, smk_rcu);
953 kmem_cache_free(smack_inode_cache, issp);
957 * smack_inode_free_security - free an inode blob using call_rcu()
958 * @inode: the inode with a blob
960 * Clears the blob pointer in inode using RCU
962 static void smack_inode_free_security(struct inode *inode)
964 struct inode_smack *issp = inode->i_security;
967 * The inode may still be referenced in a path walk and
968 * a call to smack_inode_permission() can be made
969 * after smack_inode_free_security() is called.
970 * To avoid race condition free the i_security via RCU
971 * and leave the current inode->i_security pointer intact.
972 * The inode will be freed after the RCU grace period too.
974 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
978 * smack_inode_init_security - copy out the smack from an inode
979 * @inode: the newly created inode
980 * @dir: containing directory object
982 * @name: where to put the attribute name
983 * @value: where to put the attribute value
984 * @len: where to put the length of the attribute
986 * Returns 0 if it all works out, -ENOMEM if there's no memory
988 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
989 const struct qstr *qstr, const char **name,
990 void **value, size_t *len)
992 struct inode_smack *issp = inode->i_security;
993 struct smack_known *skp = smk_of_current();
994 struct smack_known *isp = smk_of_inode(inode);
995 struct smack_known *dsp = smk_of_inode(dir);
999 *name = XATTR_SMACK_SUFFIX;
1003 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1008 * If the access rule allows transmutation and
1009 * the directory requests transmutation then
1010 * by all means transmute.
1011 * Mark the inode as changed.
1013 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1014 smk_inode_transmutable(dir)) {
1016 issp->smk_flags |= SMK_INODE_CHANGED;
1019 *value = kstrdup(isp->smk_known, GFP_NOFS);
1023 *len = strlen(isp->smk_known);
1030 * smack_inode_link - Smack check on link
1031 * @old_dentry: the existing object
1033 * @new_dentry: the new object
1035 * Returns 0 if access is permitted, an error code otherwise
1037 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1038 struct dentry *new_dentry)
1040 struct smack_known *isp;
1041 struct smk_audit_info ad;
1044 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1045 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1047 isp = smk_of_inode(d_backing_inode(old_dentry));
1048 rc = smk_curacc(isp, MAY_WRITE, &ad);
1049 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1051 if (rc == 0 && d_is_positive(new_dentry)) {
1052 isp = smk_of_inode(d_backing_inode(new_dentry));
1053 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1054 rc = smk_curacc(isp, MAY_WRITE, &ad);
1055 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1062 * smack_inode_unlink - Smack check on inode deletion
1063 * @dir: containing directory object
1064 * @dentry: file to unlink
1066 * Returns 0 if current can write the containing directory
1067 * and the object, error code otherwise
1069 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1071 struct inode *ip = d_backing_inode(dentry);
1072 struct smk_audit_info ad;
1075 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1076 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1079 * You need write access to the thing you're unlinking
1081 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1082 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1085 * You also need write access to the containing directory
1087 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1088 smk_ad_setfield_u_fs_inode(&ad, dir);
1089 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1090 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1096 * smack_inode_rmdir - Smack check on directory deletion
1097 * @dir: containing directory object
1098 * @dentry: directory to unlink
1100 * Returns 0 if current can write the containing directory
1101 * and the directory, error code otherwise
1103 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1105 struct smk_audit_info ad;
1108 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1109 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1112 * You need write access to the thing you're removing
1114 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1115 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1118 * You also need write access to the containing directory
1120 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1121 smk_ad_setfield_u_fs_inode(&ad, dir);
1122 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1123 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1130 * smack_inode_rename - Smack check on rename
1131 * @old_inode: unused
1132 * @old_dentry: the old object
1133 * @new_inode: unused
1134 * @new_dentry: the new object
1136 * Read and write access is required on both the old and
1139 * Returns 0 if access is permitted, an error code otherwise
1141 static int smack_inode_rename(struct inode *old_inode,
1142 struct dentry *old_dentry,
1143 struct inode *new_inode,
1144 struct dentry *new_dentry)
1147 struct smack_known *isp;
1148 struct smk_audit_info ad;
1150 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1151 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1153 isp = smk_of_inode(d_backing_inode(old_dentry));
1154 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1155 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1157 if (rc == 0 && d_is_positive(new_dentry)) {
1158 isp = smk_of_inode(d_backing_inode(new_dentry));
1159 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1160 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1161 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1167 * smack_inode_permission - Smack version of permission()
1168 * @inode: the inode in question
1169 * @mask: the access requested
1171 * This is the important Smack hook.
1173 * Returns 0 if access is permitted, -EACCES otherwise
1175 static int smack_inode_permission(struct inode *inode, int mask)
1177 struct superblock_smack *sbsp = inode->i_sb->s_security;
1178 struct smk_audit_info ad;
1179 int no_block = mask & MAY_NOT_BLOCK;
1182 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1184 * No permission to check. Existence test. Yup, it's there.
1189 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1190 if (smk_of_inode(inode) != sbsp->smk_root)
1194 /* May be droppable after audit */
1197 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1198 smk_ad_setfield_u_fs_inode(&ad, inode);
1199 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1200 rc = smk_bu_inode(inode, mask, rc);
1205 * smack_inode_setattr - Smack check for setting attributes
1206 * @dentry: the object
1207 * @iattr: for the force flag
1209 * Returns 0 if access is permitted, an error code otherwise
1211 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1213 struct smk_audit_info ad;
1217 * Need to allow for clearing the setuid bit.
1219 if (iattr->ia_valid & ATTR_FORCE)
1221 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1222 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1224 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1225 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1230 * smack_inode_getattr - Smack check for getting attributes
1231 * @mnt: vfsmount of the object
1232 * @dentry: the object
1234 * Returns 0 if access is permitted, an error code otherwise
1236 static int smack_inode_getattr(const struct path *path)
1238 struct smk_audit_info ad;
1239 struct inode *inode = d_backing_inode(path->dentry);
1242 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1243 smk_ad_setfield_u_fs_path(&ad, *path);
1244 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1245 rc = smk_bu_inode(inode, MAY_READ, rc);
1250 * smack_inode_setxattr - Smack check for setting xattrs
1251 * @dentry: the object
1252 * @name: name of the attribute
1253 * @value: value of the attribute
1254 * @size: size of the value
1257 * This protects the Smack attribute explicitly.
1259 * Returns 0 if access is permitted, an error code otherwise
1261 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1262 const void *value, size_t size, int flags)
1264 struct smk_audit_info ad;
1265 struct smack_known *skp;
1267 int check_import = 0;
1272 * Check label validity here so import won't fail in post_setxattr
1274 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1275 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1276 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1279 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1280 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1284 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1286 if (size != TRANS_TRUE_SIZE ||
1287 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1290 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1292 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1295 if (rc == 0 && check_import) {
1296 skp = size ? smk_import_entry(value, size) : NULL;
1299 else if (skp == NULL || (check_star &&
1300 (skp == &smack_known_star || skp == &smack_known_web)))
1304 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1305 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1308 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1309 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1316 * smack_inode_post_setxattr - Apply the Smack update approved above
1318 * @name: attribute name
1319 * @value: attribute value
1320 * @size: attribute size
1323 * Set the pointer in the inode blob to the entry found
1324 * in the master label list.
1326 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1327 const void *value, size_t size, int flags)
1329 struct smack_known *skp;
1330 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
1332 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1333 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1337 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1338 skp = smk_import_entry(value, size);
1340 isp->smk_inode = skp;
1341 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1342 skp = smk_import_entry(value, size);
1344 isp->smk_task = skp;
1345 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1346 skp = smk_import_entry(value, size);
1348 isp->smk_mmap = skp;
1355 * smack_inode_getxattr - Smack check on getxattr
1356 * @dentry: the object
1359 * Returns 0 if access is permitted, an error code otherwise
1361 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1363 struct smk_audit_info ad;
1366 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1367 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1369 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1370 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1375 * smack_inode_removexattr - Smack check on removexattr
1376 * @dentry: the object
1377 * @name: name of the attribute
1379 * Removing the Smack attribute requires CAP_MAC_ADMIN
1381 * Returns 0 if access is permitted, an error code otherwise
1383 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1385 struct inode_smack *isp;
1386 struct smk_audit_info ad;
1389 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1390 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1391 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1392 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1393 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1394 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1395 if (!smack_privileged(CAP_MAC_ADMIN))
1398 rc = cap_inode_removexattr(dentry, name);
1403 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1404 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1406 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1407 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1411 isp = d_backing_inode(dentry)->i_security;
1413 * Don't do anything special for these.
1414 * XATTR_NAME_SMACKIPIN
1415 * XATTR_NAME_SMACKIPOUT
1417 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1418 struct super_block *sbp = dentry->d_sb;
1419 struct superblock_smack *sbsp = sbp->s_security;
1421 isp->smk_inode = sbsp->smk_default;
1422 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1423 isp->smk_task = NULL;
1424 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1425 isp->smk_mmap = NULL;
1426 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1427 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1433 * smack_inode_getsecurity - get smack xattrs
1434 * @inode: the object
1435 * @name: attribute name
1436 * @buffer: where to put the result
1437 * @alloc: duplicate memory
1439 * Returns the size of the attribute or an error code
1441 static int smack_inode_getsecurity(struct inode *inode,
1442 const char *name, void **buffer,
1445 struct socket_smack *ssp;
1446 struct socket *sock;
1447 struct super_block *sbp;
1448 struct inode *ip = (struct inode *)inode;
1449 struct smack_known *isp;
1451 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1452 isp = smk_of_inode(inode);
1455 * The rest of the Smack xattrs are only on sockets.
1458 if (sbp->s_magic != SOCKFS_MAGIC)
1461 sock = SOCKET_I(ip);
1462 if (sock == NULL || sock->sk == NULL)
1465 ssp = sock->sk->sk_security;
1467 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1469 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1476 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1477 if (*buffer == NULL)
1481 return strlen(isp->smk_known);
1486 * smack_inode_listsecurity - list the Smack attributes
1487 * @inode: the object
1488 * @buffer: where they go
1489 * @buffer_size: size of buffer
1491 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1494 int len = sizeof(XATTR_NAME_SMACK);
1496 if (buffer != NULL && len <= buffer_size)
1497 memcpy(buffer, XATTR_NAME_SMACK, len);
1503 * smack_inode_getsecid - Extract inode's security id
1504 * @inode: inode to extract the info from
1505 * @secid: where result will be saved
1507 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1509 struct smack_known *skp = smk_of_inode(inode);
1511 *secid = skp->smk_secid;
1519 * There is no smack_file_permission hook
1521 * Should access checks be done on each read or write?
1522 * UNICOS and SELinux say yes.
1523 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1525 * I'll say no for now. Smack does not do the frequent
1526 * label changing that SELinux does.
1530 * smack_file_alloc_security - assign a file security blob
1533 * The security blob for a file is a pointer to the master
1534 * label list, so no allocation is done.
1536 * f_security is the owner security information. It
1537 * isn't used on file access checks, it's for send_sigio.
1541 static int smack_file_alloc_security(struct file *file)
1543 struct smack_known *skp = smk_of_current();
1545 file->f_security = skp;
1550 * smack_file_free_security - clear a file security blob
1553 * The security blob for a file is a pointer to the master
1554 * label list, so no memory is freed.
1556 static void smack_file_free_security(struct file *file)
1558 file->f_security = NULL;
1562 * smack_file_ioctl - Smack check on ioctls
1567 * Relies heavily on the correct use of the ioctl command conventions.
1569 * Returns 0 if allowed, error code otherwise
1571 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1575 struct smk_audit_info ad;
1576 struct inode *inode = file_inode(file);
1578 if (unlikely(IS_PRIVATE(inode)))
1581 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1582 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1584 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1585 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1586 rc = smk_bu_file(file, MAY_WRITE, rc);
1589 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1590 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1591 rc = smk_bu_file(file, MAY_READ, rc);
1598 * smack_file_lock - Smack check on file locking
1602 * Returns 0 if current has lock access, error code otherwise
1604 static int smack_file_lock(struct file *file, unsigned int cmd)
1606 struct smk_audit_info ad;
1608 struct inode *inode = file_inode(file);
1610 if (unlikely(IS_PRIVATE(inode)))
1613 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1614 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1615 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1616 rc = smk_bu_file(file, MAY_LOCK, rc);
1621 * smack_file_fcntl - Smack check on fcntl
1623 * @cmd: what action to check
1626 * Generally these operations are harmless.
1627 * File locking operations present an obvious mechanism
1628 * for passing information, so they require write access.
1630 * Returns 0 if current has access, error code otherwise
1632 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1635 struct smk_audit_info ad;
1637 struct inode *inode = file_inode(file);
1639 if (unlikely(IS_PRIVATE(inode)))
1647 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1648 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1649 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1650 rc = smk_bu_file(file, MAY_LOCK, rc);
1654 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1655 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1656 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1657 rc = smk_bu_file(file, MAY_WRITE, rc);
1668 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1669 * if mapping anonymous memory.
1670 * @file contains the file structure for file to map (may be NULL).
1671 * @reqprot contains the protection requested by the application.
1672 * @prot contains the protection that will be applied by the kernel.
1673 * @flags contains the operational flags.
1674 * Return 0 if permission is granted.
1676 static int smack_mmap_file(struct file *file,
1677 unsigned long reqprot, unsigned long prot,
1678 unsigned long flags)
1680 struct smack_known *skp;
1681 struct smack_known *mkp;
1682 struct smack_rule *srp;
1683 struct task_smack *tsp;
1684 struct smack_known *okp;
1685 struct inode_smack *isp;
1686 struct superblock_smack *sbsp;
1695 if (unlikely(IS_PRIVATE(file_inode(file))))
1698 isp = file_inode(file)->i_security;
1699 if (isp->smk_mmap == NULL)
1701 sbsp = file_inode(file)->i_sb->s_security;
1702 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1703 isp->smk_mmap != sbsp->smk_root)
1705 mkp = isp->smk_mmap;
1707 tsp = current_security();
1708 skp = smk_of_current();
1713 * For each Smack rule associated with the subject
1714 * label verify that the SMACK64MMAP also has access
1715 * to that rule's object label.
1717 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1718 okp = srp->smk_object;
1720 * Matching labels always allows access.
1722 if (mkp->smk_known == okp->smk_known)
1725 * If there is a matching local rule take
1726 * that into account as well.
1728 may = smk_access_entry(srp->smk_subject->smk_known,
1732 may = srp->smk_access;
1734 may &= srp->smk_access;
1736 * If may is zero the SMACK64MMAP subject can't
1737 * possibly have less access.
1743 * Fetch the global list entry.
1744 * If there isn't one a SMACK64MMAP subject
1745 * can't have as much access as current.
1747 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1749 if (mmay == -ENOENT) {
1754 * If there is a local entry it modifies the
1755 * potential access, too.
1757 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1759 if (tmay != -ENOENT)
1763 * If there is any access available to current that is
1764 * not available to a SMACK64MMAP subject
1767 if ((may | mmay) != mmay) {
1779 * smack_file_set_fowner - set the file security blob value
1780 * @file: object in question
1783 static void smack_file_set_fowner(struct file *file)
1785 file->f_security = smk_of_current();
1789 * smack_file_send_sigiotask - Smack on sigio
1790 * @tsk: The target task
1791 * @fown: the object the signal come from
1794 * Allow a privileged task to get signals even if it shouldn't
1796 * Returns 0 if a subject with the object's smack could
1797 * write to the task, an error code otherwise.
1799 static int smack_file_send_sigiotask(struct task_struct *tsk,
1800 struct fown_struct *fown, int signum)
1802 struct smack_known *skp;
1803 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1804 const struct cred *tcred;
1807 struct smk_audit_info ad;
1810 * struct fown_struct is never outside the context of a struct file
1812 file = container_of(fown, struct file, f_owner);
1814 /* we don't log here as rc can be overriden */
1815 skp = file->f_security;
1816 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1817 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1820 tcred = __task_cred(tsk);
1821 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1825 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1826 smk_ad_setfield_u_tsk(&ad, tsk);
1827 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1832 * smack_file_receive - Smack file receive check
1835 * Returns 0 if current has access, error code otherwise
1837 static int smack_file_receive(struct file *file)
1841 struct smk_audit_info ad;
1842 struct inode *inode = file_inode(file);
1843 struct socket *sock;
1844 struct task_smack *tsp;
1845 struct socket_smack *ssp;
1847 if (unlikely(IS_PRIVATE(inode)))
1850 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1851 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1853 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1854 sock = SOCKET_I(inode);
1855 ssp = sock->sk->sk_security;
1856 tsp = current_security();
1858 * If the receiving process can't write to the
1859 * passed socket or if the passed socket can't
1860 * write to the receiving process don't accept
1861 * the passed socket.
1863 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1864 rc = smk_bu_file(file, may, rc);
1867 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1868 rc = smk_bu_file(file, may, rc);
1872 * This code relies on bitmasks.
1874 if (file->f_mode & FMODE_READ)
1876 if (file->f_mode & FMODE_WRITE)
1879 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1880 rc = smk_bu_file(file, may, rc);
1885 * smack_file_open - Smack dentry open processing
1887 * @cred: task credential
1889 * Set the security blob in the file structure.
1890 * Allow the open only if the task has read access. There are
1891 * many read operations (e.g. fstat) that you can do with an
1892 * fd even if you have the file open write-only.
1896 static int smack_file_open(struct file *file)
1898 struct task_smack *tsp = file->f_cred->security;
1899 struct inode *inode = file_inode(file);
1900 struct smk_audit_info ad;
1903 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1904 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1905 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1906 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1916 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1917 * @new: the new credentials
1918 * @gfp: the atomicity of any memory allocations
1920 * Prepare a blank set of credentials for modification. This must allocate all
1921 * the memory the LSM module might require such that cred_transfer() can
1922 * complete without error.
1924 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1926 struct task_smack *tsp;
1928 tsp = new_task_smack(NULL, NULL, gfp);
1932 cred->security = tsp;
1939 * smack_cred_free - "free" task-level security credentials
1940 * @cred: the credentials in question
1943 static void smack_cred_free(struct cred *cred)
1945 struct task_smack *tsp = cred->security;
1946 struct smack_rule *rp;
1947 struct list_head *l;
1948 struct list_head *n;
1952 cred->security = NULL;
1954 smk_destroy_label_list(&tsp->smk_relabel);
1956 list_for_each_safe(l, n, &tsp->smk_rules) {
1957 rp = list_entry(l, struct smack_rule, list);
1958 list_del(&rp->list);
1965 * smack_cred_prepare - prepare new set of credentials for modification
1966 * @new: the new credentials
1967 * @old: the original credentials
1968 * @gfp: the atomicity of any memory allocations
1970 * Prepare a new set of credentials for modification.
1972 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1975 struct task_smack *old_tsp = old->security;
1976 struct task_smack *new_tsp;
1979 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1980 if (new_tsp == NULL)
1983 new->security = new_tsp;
1985 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1989 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1998 * smack_cred_transfer - Transfer the old credentials to the new credentials
1999 * @new: the new credentials
2000 * @old: the original credentials
2002 * Fill in a set of blank credentials from another set of credentials.
2004 static void smack_cred_transfer(struct cred *new, const struct cred *old)
2006 struct task_smack *old_tsp = old->security;
2007 struct task_smack *new_tsp = new->security;
2009 new_tsp->smk_task = old_tsp->smk_task;
2010 new_tsp->smk_forked = old_tsp->smk_task;
2011 mutex_init(&new_tsp->smk_rules_lock);
2012 INIT_LIST_HEAD(&new_tsp->smk_rules);
2015 /* cbs copy rule list */
2019 * smack_cred_getsecid - get the secid corresponding to a creds structure
2020 * @c: the object creds
2021 * @secid: where to put the result
2023 * Sets the secid to contain a u32 version of the smack label.
2025 static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2027 struct smack_known *skp;
2030 skp = smk_of_task(c->security);
2031 *secid = skp->smk_secid;
2036 * smack_kernel_act_as - Set the subjective context in a set of credentials
2037 * @new: points to the set of credentials to be modified.
2038 * @secid: specifies the security ID to be set
2040 * Set the security data for a kernel service.
2042 static int smack_kernel_act_as(struct cred *new, u32 secid)
2044 struct task_smack *new_tsp = new->security;
2046 new_tsp->smk_task = smack_from_secid(secid);
2051 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2052 * @new: points to the set of credentials to be modified
2053 * @inode: points to the inode to use as a reference
2055 * Set the file creation context in a set of credentials to the same
2056 * as the objective context of the specified inode
2058 static int smack_kernel_create_files_as(struct cred *new,
2059 struct inode *inode)
2061 struct inode_smack *isp = inode->i_security;
2062 struct task_smack *tsp = new->security;
2064 tsp->smk_forked = isp->smk_inode;
2065 tsp->smk_task = tsp->smk_forked;
2070 * smk_curacc_on_task - helper to log task related access
2071 * @p: the task object
2072 * @access: the access requested
2073 * @caller: name of the calling function for audit
2075 * Return 0 if access is permitted
2077 static int smk_curacc_on_task(struct task_struct *p, int access,
2080 struct smk_audit_info ad;
2081 struct smack_known *skp = smk_of_task_struct(p);
2084 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2085 smk_ad_setfield_u_tsk(&ad, p);
2086 rc = smk_curacc(skp, access, &ad);
2087 rc = smk_bu_task(p, access, rc);
2092 * smack_task_setpgid - Smack check on setting pgid
2093 * @p: the task object
2096 * Return 0 if write access is permitted
2098 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2100 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2104 * smack_task_getpgid - Smack access check for getpgid
2105 * @p: the object task
2107 * Returns 0 if current can read the object task, error code otherwise
2109 static int smack_task_getpgid(struct task_struct *p)
2111 return smk_curacc_on_task(p, MAY_READ, __func__);
2115 * smack_task_getsid - Smack access check for getsid
2116 * @p: the object task
2118 * Returns 0 if current can read the object task, error code otherwise
2120 static int smack_task_getsid(struct task_struct *p)
2122 return smk_curacc_on_task(p, MAY_READ, __func__);
2126 * smack_task_getsecid - get the secid of the task
2127 * @p: the object task
2128 * @secid: where to put the result
2130 * Sets the secid to contain a u32 version of the smack label.
2132 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2134 struct smack_known *skp = smk_of_task_struct(p);
2136 *secid = skp->smk_secid;
2140 * smack_task_setnice - Smack check on setting nice
2141 * @p: the task object
2144 * Return 0 if write access is permitted
2146 static int smack_task_setnice(struct task_struct *p, int nice)
2148 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2152 * smack_task_setioprio - Smack check on setting ioprio
2153 * @p: the task object
2156 * Return 0 if write access is permitted
2158 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2160 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2164 * smack_task_getioprio - Smack check on reading ioprio
2165 * @p: the task object
2167 * Return 0 if read access is permitted
2169 static int smack_task_getioprio(struct task_struct *p)
2171 return smk_curacc_on_task(p, MAY_READ, __func__);
2175 * smack_task_setscheduler - Smack check on setting scheduler
2176 * @p: the task object
2180 * Return 0 if read access is permitted
2182 static int smack_task_setscheduler(struct task_struct *p)
2184 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2188 * smack_task_getscheduler - Smack check on reading scheduler
2189 * @p: the task object
2191 * Return 0 if read access is permitted
2193 static int smack_task_getscheduler(struct task_struct *p)
2195 return smk_curacc_on_task(p, MAY_READ, __func__);
2199 * smack_task_movememory - Smack check on moving memory
2200 * @p: the task object
2202 * Return 0 if write access is permitted
2204 static int smack_task_movememory(struct task_struct *p)
2206 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2210 * smack_task_kill - Smack check on signal delivery
2211 * @p: the task object
2214 * @cred: identifies the cred to use in lieu of current's
2216 * Return 0 if write access is permitted
2219 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2220 int sig, const struct cred *cred)
2222 struct smk_audit_info ad;
2223 struct smack_known *skp;
2224 struct smack_known *tkp = smk_of_task_struct(p);
2228 return 0; /* null signal; existence test */
2230 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2231 smk_ad_setfield_u_tsk(&ad, p);
2233 * Sending a signal requires that the sender
2234 * can write the receiver.
2237 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2238 rc = smk_bu_task(p, MAY_DELIVER, rc);
2242 * If the cred isn't NULL we're dealing with some USB IO
2243 * specific behavior. This is not clean. For one thing
2244 * we can't take privilege into account.
2246 skp = smk_of_task(cred->security);
2247 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2248 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2253 * smack_task_to_inode - copy task smack into the inode blob
2254 * @p: task to copy from
2255 * @inode: inode to copy to
2257 * Sets the smack pointer in the inode security blob
2259 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2261 struct inode_smack *isp = inode->i_security;
2262 struct smack_known *skp = smk_of_task_struct(p);
2264 isp->smk_inode = skp;
2265 isp->smk_flags |= SMK_INODE_INSTANT;
2273 * smack_sk_alloc_security - Allocate a socket blob
2276 * @gfp_flags: memory allocation flags
2278 * Assign Smack pointers to current
2280 * Returns 0 on success, -ENOMEM is there's no memory
2282 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2284 struct smack_known *skp = smk_of_current();
2285 struct socket_smack *ssp;
2287 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2292 * Sockets created by kernel threads receive web label.
2294 if (unlikely(current->flags & PF_KTHREAD)) {
2295 ssp->smk_in = &smack_known_web;
2296 ssp->smk_out = &smack_known_web;
2301 ssp->smk_packet = NULL;
2303 sk->sk_security = ssp;
2309 * smack_sk_free_security - Free a socket blob
2312 * Clears the blob pointer
2314 static void smack_sk_free_security(struct sock *sk)
2316 #ifdef SMACK_IPV6_PORT_LABELING
2317 struct smk_port_label *spp;
2319 if (sk->sk_family == PF_INET6) {
2321 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2322 if (spp->smk_sock != sk)
2324 spp->smk_can_reuse = 1;
2330 kfree(sk->sk_security);
2334 * smack_ipv4host_label - check host based restrictions
2335 * @sip: the object end
2337 * looks for host based access restrictions
2339 * This version will only be appropriate for really small sets of single label
2340 * hosts. The caller is responsible for ensuring that the RCU read lock is
2341 * taken before calling this function.
2343 * Returns the label of the far end or NULL if it's not special.
2345 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2347 struct smk_net4addr *snp;
2348 struct in_addr *siap = &sip->sin_addr;
2350 if (siap->s_addr == 0)
2353 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2355 * we break after finding the first match because
2356 * the list is sorted from longest to shortest mask
2357 * so we have found the most specific match
2359 if (snp->smk_host.s_addr ==
2360 (siap->s_addr & snp->smk_mask.s_addr))
2361 return snp->smk_label;
2366 #if IS_ENABLED(CONFIG_IPV6)
2368 * smk_ipv6_localhost - Check for local ipv6 host address
2371 * Returns boolean true if this is the localhost address
2373 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2375 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2376 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2378 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2379 ntohs(be16p[7]) == 1)
2385 * smack_ipv6host_label - check host based restrictions
2386 * @sip: the object end
2388 * looks for host based access restrictions
2390 * This version will only be appropriate for really small sets of single label
2391 * hosts. The caller is responsible for ensuring that the RCU read lock is
2392 * taken before calling this function.
2394 * Returns the label of the far end or NULL if it's not special.
2396 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2398 struct smk_net6addr *snp;
2399 struct in6_addr *sap = &sip->sin6_addr;
2404 * It's local. Don't look for a host label.
2406 if (smk_ipv6_localhost(sip))
2409 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2411 * If the label is NULL the entry has
2412 * been renounced. Ignore it.
2414 if (snp->smk_label == NULL)
2417 * we break after finding the first match because
2418 * the list is sorted from longest to shortest mask
2419 * so we have found the most specific match
2421 for (found = 1, i = 0; i < 8; i++) {
2422 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2423 snp->smk_host.s6_addr16[i]) {
2429 return snp->smk_label;
2434 #endif /* CONFIG_IPV6 */
2437 * smack_netlabel - Set the secattr on a socket
2439 * @labeled: socket label scheme
2441 * Convert the outbound smack value (smk_out) to a
2442 * secattr and attach it to the socket.
2444 * Returns 0 on success or an error code
2446 static int smack_netlabel(struct sock *sk, int labeled)
2448 struct smack_known *skp;
2449 struct socket_smack *ssp = sk->sk_security;
2453 * Usually the netlabel code will handle changing the
2454 * packet labeling based on the label.
2455 * The case of a single label host is different, because
2456 * a single label host should never get a labeled packet
2457 * even though the label is usually associated with a packet
2461 bh_lock_sock_nested(sk);
2463 if (ssp->smk_out == smack_net_ambient ||
2464 labeled == SMACK_UNLABELED_SOCKET)
2465 netlbl_sock_delattr(sk);
2468 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2478 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2480 * @sap: the destination address
2482 * Set the correct secattr for the given socket based on the destination
2483 * address and perform any outbound access checks needed.
2485 * Returns 0 on success or an error code.
2488 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2490 struct smack_known *skp;
2493 struct smack_known *hkp;
2494 struct socket_smack *ssp = sk->sk_security;
2495 struct smk_audit_info ad;
2498 hkp = smack_ipv4host_label(sap);
2501 struct lsm_network_audit net;
2503 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2504 ad.a.u.net->family = sap->sin_family;
2505 ad.a.u.net->dport = sap->sin_port;
2506 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2508 sk_lbl = SMACK_UNLABELED_SOCKET;
2510 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2511 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2513 sk_lbl = SMACK_CIPSO_SOCKET;
2520 return smack_netlabel(sk, sk_lbl);
2523 #if IS_ENABLED(CONFIG_IPV6)
2525 * smk_ipv6_check - check Smack access
2526 * @subject: subject Smack label
2527 * @object: object Smack label
2529 * @act: the action being taken
2531 * Check an IPv6 access
2533 static int smk_ipv6_check(struct smack_known *subject,
2534 struct smack_known *object,
2535 struct sockaddr_in6 *address, int act)
2538 struct lsm_network_audit net;
2540 struct smk_audit_info ad;
2544 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2545 ad.a.u.net->family = PF_INET6;
2546 ad.a.u.net->dport = ntohs(address->sin6_port);
2547 if (act == SMK_RECEIVING)
2548 ad.a.u.net->v6info.saddr = address->sin6_addr;
2550 ad.a.u.net->v6info.daddr = address->sin6_addr;
2552 rc = smk_access(subject, object, MAY_WRITE, &ad);
2553 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2556 #endif /* CONFIG_IPV6 */
2558 #ifdef SMACK_IPV6_PORT_LABELING
2560 * smk_ipv6_port_label - Smack port access table management
2564 * Create or update the port list entry
2566 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2568 struct sock *sk = sock->sk;
2569 struct sockaddr_in6 *addr6;
2570 struct socket_smack *ssp = sock->sk->sk_security;
2571 struct smk_port_label *spp;
2572 unsigned short port = 0;
2574 if (address == NULL) {
2576 * This operation is changing the Smack information
2577 * on the bound socket. Take the changes to the port
2581 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2582 if (sk != spp->smk_sock)
2584 spp->smk_in = ssp->smk_in;
2585 spp->smk_out = ssp->smk_out;
2590 * A NULL address is only used for updating existing
2591 * bound entries. If there isn't one, it's OK.
2597 addr6 = (struct sockaddr_in6 *)address;
2598 port = ntohs(addr6->sin6_port);
2600 * This is a special case that is safely ignored.
2606 * Look for an existing port list entry.
2607 * This is an indication that a port is getting reused.
2610 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2611 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2613 if (spp->smk_can_reuse != 1) {
2617 spp->smk_port = port;
2619 spp->smk_in = ssp->smk_in;
2620 spp->smk_out = ssp->smk_out;
2621 spp->smk_can_reuse = 0;
2627 * A new port entry is required.
2629 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2633 spp->smk_port = port;
2635 spp->smk_in = ssp->smk_in;
2636 spp->smk_out = ssp->smk_out;
2637 spp->smk_sock_type = sock->type;
2638 spp->smk_can_reuse = 0;
2640 mutex_lock(&smack_ipv6_lock);
2641 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2642 mutex_unlock(&smack_ipv6_lock);
2647 * smk_ipv6_port_check - check Smack port access
2651 * Create or update the port list entry
2653 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2656 struct smk_port_label *spp;
2657 struct socket_smack *ssp = sk->sk_security;
2658 struct smack_known *skp = NULL;
2659 unsigned short port;
2660 struct smack_known *object;
2662 if (act == SMK_RECEIVING) {
2663 skp = smack_ipv6host_label(address);
2664 object = ssp->smk_in;
2667 object = smack_ipv6host_label(address);
2671 * The other end is a single label host.
2673 if (skp != NULL && object != NULL)
2674 return smk_ipv6_check(skp, object, address, act);
2676 skp = smack_net_ambient;
2678 object = smack_net_ambient;
2681 * It's remote, so port lookup does no good.
2683 if (!smk_ipv6_localhost(address))
2684 return smk_ipv6_check(skp, object, address, act);
2687 * It's local so the send check has to have passed.
2689 if (act == SMK_RECEIVING)
2692 port = ntohs(address->sin6_port);
2694 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2695 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2697 object = spp->smk_in;
2698 if (act == SMK_CONNECTING)
2699 ssp->smk_packet = spp->smk_out;
2704 return smk_ipv6_check(skp, object, address, act);
2706 #endif /* SMACK_IPV6_PORT_LABELING */
2709 * smack_inode_setsecurity - set smack xattrs
2710 * @inode: the object
2711 * @name: attribute name
2712 * @value: attribute value
2713 * @size: size of the attribute
2716 * Sets the named attribute in the appropriate blob
2718 * Returns 0 on success, or an error code
2720 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2721 const void *value, size_t size, int flags)
2723 struct smack_known *skp;
2724 struct inode_smack *nsp = inode->i_security;
2725 struct socket_smack *ssp;
2726 struct socket *sock;
2729 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2732 skp = smk_import_entry(value, size);
2734 return PTR_ERR(skp);
2736 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2737 nsp->smk_inode = skp;
2738 nsp->smk_flags |= SMK_INODE_INSTANT;
2742 * The rest of the Smack xattrs are only on sockets.
2744 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2747 sock = SOCKET_I(inode);
2748 if (sock == NULL || sock->sk == NULL)
2751 ssp = sock->sk->sk_security;
2753 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2755 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2757 if (sock->sk->sk_family == PF_INET) {
2758 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2761 "Smack: \"%s\" netlbl error %d.\n",
2767 #ifdef SMACK_IPV6_PORT_LABELING
2768 if (sock->sk->sk_family == PF_INET6)
2769 smk_ipv6_port_label(sock, NULL);
2776 * smack_socket_post_create - finish socket setup
2778 * @family: protocol family
2783 * Sets the netlabel information on the socket
2785 * Returns 0 on success, and error code otherwise
2787 static int smack_socket_post_create(struct socket *sock, int family,
2788 int type, int protocol, int kern)
2790 struct socket_smack *ssp;
2792 if (sock->sk == NULL)
2796 * Sockets created by kernel threads receive web label.
2798 if (unlikely(current->flags & PF_KTHREAD)) {
2799 ssp = sock->sk->sk_security;
2800 ssp->smk_in = &smack_known_web;
2801 ssp->smk_out = &smack_known_web;
2804 if (family != PF_INET)
2807 * Set the outbound netlbl.
2809 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2813 * smack_socket_socketpair - create socket pair
2814 * @socka: one socket
2815 * @sockb: another socket
2817 * Cross reference the peer labels for SO_PEERSEC
2819 * Returns 0 on success, and error code otherwise
2821 static int smack_socket_socketpair(struct socket *socka,
2822 struct socket *sockb)
2824 struct socket_smack *asp = socka->sk->sk_security;
2825 struct socket_smack *bsp = sockb->sk->sk_security;
2827 asp->smk_packet = bsp->smk_out;
2828 bsp->smk_packet = asp->smk_out;
2833 #ifdef SMACK_IPV6_PORT_LABELING
2835 * smack_socket_bind - record port binding information.
2837 * @address: the port address
2838 * @addrlen: size of the address
2840 * Records the label bound to a port.
2844 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2847 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2848 smk_ipv6_port_label(sock, address);
2851 #endif /* SMACK_IPV6_PORT_LABELING */
2854 * smack_socket_connect - connect access check
2856 * @sap: the other end
2857 * @addrlen: size of sap
2859 * Verifies that a connection may be possible
2861 * Returns 0 on success, and error code otherwise
2863 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2867 #if IS_ENABLED(CONFIG_IPV6)
2868 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2870 #ifdef SMACK_IPV6_SECMARK_LABELING
2871 struct smack_known *rsp;
2872 struct socket_smack *ssp;
2875 if (sock->sk == NULL)
2878 #ifdef SMACK_IPV6_SECMARK_LABELING
2879 ssp = sock->sk->sk_security;
2882 switch (sock->sk->sk_family) {
2884 if (addrlen < sizeof(struct sockaddr_in))
2886 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2889 if (addrlen < sizeof(struct sockaddr_in6))
2891 #ifdef SMACK_IPV6_SECMARK_LABELING
2892 rsp = smack_ipv6host_label(sip);
2894 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2897 #ifdef SMACK_IPV6_PORT_LABELING
2898 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2906 * smack_flags_to_may - convert S_ to MAY_ values
2907 * @flags: the S_ value
2909 * Returns the equivalent MAY_ value
2911 static int smack_flags_to_may(int flags)
2915 if (flags & S_IRUGO)
2917 if (flags & S_IWUGO)
2919 if (flags & S_IXUGO)
2926 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2931 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2933 struct smack_known *skp = smk_of_current();
2935 msg->security = skp;
2940 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2943 * Clears the blob pointer
2945 static void smack_msg_msg_free_security(struct msg_msg *msg)
2947 msg->security = NULL;
2951 * smack_of_ipc - the smack pointer for the ipc
2954 * Returns a pointer to the smack value
2956 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2958 return (struct smack_known *)isp->security;
2962 * smack_ipc_alloc_security - Set the security blob for ipc
2967 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2969 struct smack_known *skp = smk_of_current();
2971 isp->security = skp;
2976 * smack_ipc_free_security - Clear the security blob for ipc
2979 * Clears the blob pointer
2981 static void smack_ipc_free_security(struct kern_ipc_perm *isp)
2983 isp->security = NULL;
2987 * smk_curacc_shm : check if current has access on shm
2989 * @access : access requested
2991 * Returns 0 if current has the requested access, error code otherwise
2993 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2995 struct smack_known *ssp = smack_of_ipc(isp);
2996 struct smk_audit_info ad;
3000 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3001 ad.a.u.ipc_id = isp->id;
3003 rc = smk_curacc(ssp, access, &ad);
3004 rc = smk_bu_current("shm", ssp, access, rc);
3009 * smack_shm_associate - Smack access check for shm
3011 * @shmflg: access requested
3013 * Returns 0 if current has the requested access, error code otherwise
3015 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3019 may = smack_flags_to_may(shmflg);
3020 return smk_curacc_shm(isp, may);
3024 * smack_shm_shmctl - Smack access check for shm
3026 * @cmd: what it wants to do
3028 * Returns 0 if current has the requested access, error code otherwise
3030 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3044 may = MAY_READWRITE;
3049 * System level information.
3055 return smk_curacc_shm(isp, may);
3059 * smack_shm_shmat - Smack access for shmat
3062 * @shmflg: access requested
3064 * Returns 0 if current has the requested access, error code otherwise
3066 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3071 may = smack_flags_to_may(shmflg);
3072 return smk_curacc_shm(ipc, may);
3076 * smk_curacc_sem : check if current has access on sem
3078 * @access : access requested
3080 * Returns 0 if current has the requested access, error code otherwise
3082 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3084 struct smack_known *ssp = smack_of_ipc(isp);
3085 struct smk_audit_info ad;
3089 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3090 ad.a.u.ipc_id = isp->id;
3092 rc = smk_curacc(ssp, access, &ad);
3093 rc = smk_bu_current("sem", ssp, access, rc);
3098 * smack_sem_associate - Smack access check for sem
3100 * @semflg: access requested
3102 * Returns 0 if current has the requested access, error code otherwise
3104 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3108 may = smack_flags_to_may(semflg);
3109 return smk_curacc_sem(isp, may);
3113 * smack_sem_shmctl - Smack access check for sem
3115 * @cmd: what it wants to do
3117 * Returns 0 if current has the requested access, error code otherwise
3119 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3138 may = MAY_READWRITE;
3143 * System level information
3150 return smk_curacc_sem(isp, may);
3154 * smack_sem_semop - Smack checks of semaphore operations
3160 * Treated as read and write in all cases.
3162 * Returns 0 if access is allowed, error code otherwise
3164 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3165 unsigned nsops, int alter)
3167 return smk_curacc_sem(isp, MAY_READWRITE);
3171 * smk_curacc_msq : helper to check if current has access on msq
3173 * @access : access requested
3175 * return 0 if current has access, error otherwise
3177 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3179 struct smack_known *msp = smack_of_ipc(isp);
3180 struct smk_audit_info ad;
3184 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3185 ad.a.u.ipc_id = isp->id;
3187 rc = smk_curacc(msp, access, &ad);
3188 rc = smk_bu_current("msq", msp, access, rc);
3193 * smack_msg_queue_associate - Smack access check for msg_queue
3195 * @msqflg: access requested
3197 * Returns 0 if current has the requested access, error code otherwise
3199 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3203 may = smack_flags_to_may(msqflg);
3204 return smk_curacc_msq(isp, may);
3208 * smack_msg_queue_msgctl - Smack access check for msg_queue
3210 * @cmd: what it wants to do
3212 * Returns 0 if current has the requested access, error code otherwise
3214 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3226 may = MAY_READWRITE;
3231 * System level information
3238 return smk_curacc_msq(isp, may);
3242 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3245 * @msqflg: access requested
3247 * Returns 0 if current has the requested access, error code otherwise
3249 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3254 may = smack_flags_to_may(msqflg);
3255 return smk_curacc_msq(isp, may);
3259 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3266 * Returns 0 if current has read and write access, error code otherwise
3268 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3269 struct task_struct *target, long type, int mode)
3271 return smk_curacc_msq(isp, MAY_READWRITE);
3275 * smack_ipc_permission - Smack access for ipc_permission()
3276 * @ipp: the object permissions
3277 * @flag: access requested
3279 * Returns 0 if current has read and write access, error code otherwise
3281 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3283 struct smack_known *iskp = ipp->security;
3284 int may = smack_flags_to_may(flag);
3285 struct smk_audit_info ad;
3289 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3290 ad.a.u.ipc_id = ipp->id;
3292 rc = smk_curacc(iskp, may, &ad);
3293 rc = smk_bu_current("svipc", iskp, may, rc);
3298 * smack_ipc_getsecid - Extract smack security id
3299 * @ipp: the object permissions
3300 * @secid: where result will be saved
3302 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3304 struct smack_known *iskp = ipp->security;
3306 *secid = iskp->smk_secid;
3310 * smack_d_instantiate - Make sure the blob is correct on an inode
3311 * @opt_dentry: dentry where inode will be attached
3312 * @inode: the object
3314 * Set the inode's security blob if it hasn't been done already.
3316 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3318 struct super_block *sbp;
3319 struct superblock_smack *sbsp;
3320 struct inode_smack *isp;
3321 struct smack_known *skp;
3322 struct smack_known *ckp = smk_of_current();
3323 struct smack_known *final;
3324 char trattr[TRANS_TRUE_SIZE];
3332 isp = inode->i_security;
3334 mutex_lock(&isp->smk_lock);
3336 * If the inode is already instantiated
3337 * take the quick way out
3339 if (isp->smk_flags & SMK_INODE_INSTANT)
3343 sbsp = sbp->s_security;
3345 * We're going to use the superblock default label
3346 * if there's no label on the file.
3348 final = sbsp->smk_default;
3351 * If this is the root inode the superblock
3352 * may be in the process of initialization.
3353 * If that is the case use the root value out
3354 * of the superblock.
3356 if (opt_dentry->d_parent == opt_dentry) {
3357 switch (sbp->s_magic) {
3358 case CGROUP_SUPER_MAGIC:
3359 case CGROUP2_SUPER_MAGIC:
3361 * The cgroup filesystem is never mounted,
3362 * so there's no opportunity to set the mount
3365 sbsp->smk_root = &smack_known_star;
3366 sbsp->smk_default = &smack_known_star;
3367 isp->smk_inode = sbsp->smk_root;
3371 * What about shmem/tmpfs anonymous files with dentry
3372 * obtained from d_alloc_pseudo()?
3374 isp->smk_inode = smk_of_current();
3377 isp->smk_inode = smk_of_current();
3381 * Socket access is controlled by the socket
3382 * structures associated with the task involved.
3384 isp->smk_inode = &smack_known_star;
3387 isp->smk_inode = sbsp->smk_root;
3390 isp->smk_flags |= SMK_INODE_INSTANT;
3395 * This is pretty hackish.
3396 * Casey says that we shouldn't have to do
3397 * file system specific code, but it does help
3398 * with keeping it simple.
3400 switch (sbp->s_magic) {
3402 case CGROUP_SUPER_MAGIC:
3403 case CGROUP2_SUPER_MAGIC:
3405 * Casey says that it's a little embarrassing
3406 * that the smack file system doesn't do
3407 * extended attributes.
3409 * Cgroupfs is special
3411 final = &smack_known_star;
3413 case DEVPTS_SUPER_MAGIC:
3415 * devpts seems content with the label of the task.
3416 * Programs that change smack have to treat the
3421 case PROC_SUPER_MAGIC:
3423 * Casey says procfs appears not to care.
3424 * The superblock default suffices.
3429 * Device labels should come from the filesystem,
3430 * but watch out, because they're volitile,
3431 * getting recreated on every reboot.
3433 final = &smack_known_star;
3437 * If a smack value has been set we want to use it,
3438 * but since tmpfs isn't giving us the opportunity
3439 * to set mount options simulate setting the
3440 * superblock default.
3444 * This isn't an understood special case.
3445 * Get the value from the xattr.
3449 * UNIX domain sockets use lower level socket data.
3451 if (S_ISSOCK(inode->i_mode)) {
3452 final = &smack_known_star;
3456 * No xattr support means, alas, no SMACK label.
3457 * Use the aforeapplied default.
3458 * It would be curious if the label of the task
3459 * does not match that assigned.
3461 if (!(inode->i_opflags & IOP_XATTR))
3464 * Get the dentry for xattr.
3466 dp = dget(opt_dentry);
3467 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3468 if (!IS_ERR_OR_NULL(skp))
3472 * Transmuting directory
3474 if (S_ISDIR(inode->i_mode)) {
3476 * If this is a new directory and the label was
3477 * transmuted when the inode was initialized
3478 * set the transmute attribute on the directory
3479 * and mark the inode.
3481 * If there is a transmute attribute on the
3482 * directory mark the inode.
3484 if (isp->smk_flags & SMK_INODE_CHANGED) {
3485 isp->smk_flags &= ~SMK_INODE_CHANGED;
3486 rc = __vfs_setxattr(dp, inode,
3487 XATTR_NAME_SMACKTRANSMUTE,
3488 TRANS_TRUE, TRANS_TRUE_SIZE,
3491 rc = __vfs_getxattr(dp, inode,
3492 XATTR_NAME_SMACKTRANSMUTE, trattr,
3494 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3495 TRANS_TRUE_SIZE) != 0)
3499 transflag = SMK_INODE_TRANSMUTE;
3502 * Don't let the exec or mmap label be "*" or "@".
3504 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3505 if (IS_ERR(skp) || skp == &smack_known_star ||
3506 skp == &smack_known_web)
3508 isp->smk_task = skp;
3510 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3511 if (IS_ERR(skp) || skp == &smack_known_star ||
3512 skp == &smack_known_web)
3514 isp->smk_mmap = skp;
3521 isp->smk_inode = ckp;
3523 isp->smk_inode = final;
3525 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3528 mutex_unlock(&isp->smk_lock);
3533 * smack_getprocattr - Smack process attribute access
3534 * @p: the object task
3535 * @name: the name of the attribute in /proc/.../attr
3536 * @value: where to put the result
3538 * Places a copy of the task Smack into value
3540 * Returns the length of the smack label or an error code
3542 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3544 struct smack_known *skp = smk_of_task_struct(p);
3548 if (strcmp(name, "current") != 0)
3551 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3561 * smack_setprocattr - Smack process attribute setting
3562 * @name: the name of the attribute in /proc/.../attr
3563 * @value: the value to set
3564 * @size: the size of the value
3566 * Sets the Smack value of the task. Only setting self
3567 * is permitted and only with privilege
3569 * Returns the length of the smack label or an error code
3571 static int smack_setprocattr(const char *name, void *value, size_t size)
3573 struct task_smack *tsp = current_security();
3575 struct smack_known *skp;
3576 struct smack_known_list_elem *sklep;
3579 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3582 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3585 if (strcmp(name, "current") != 0)
3588 skp = smk_import_entry(value, size);
3590 return PTR_ERR(skp);
3593 * No process is ever allowed the web ("@") label
3594 * and the star ("*") label.
3596 if (skp == &smack_known_web || skp == &smack_known_star)
3599 if (!smack_privileged(CAP_MAC_ADMIN)) {
3601 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3602 if (sklep->smk_label == skp) {
3610 new = prepare_creds();
3614 tsp = new->security;
3615 tsp->smk_task = skp;
3617 * process can change its label only once
3619 smk_destroy_label_list(&tsp->smk_relabel);
3626 * smack_unix_stream_connect - Smack access on UDS
3628 * @other: the other sock
3631 * Return 0 if a subject with the smack of sock could access
3632 * an object with the smack of other, otherwise an error code
3634 static int smack_unix_stream_connect(struct sock *sock,
3635 struct sock *other, struct sock *newsk)
3637 struct smack_known *skp;
3638 struct smack_known *okp;
3639 struct socket_smack *ssp = sock->sk_security;
3640 struct socket_smack *osp = other->sk_security;
3641 struct socket_smack *nsp = newsk->sk_security;
3642 struct smk_audit_info ad;
3645 struct lsm_network_audit net;
3648 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3652 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3653 smk_ad_setfield_u_net_sk(&ad, other);
3655 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3656 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3660 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3661 rc = smk_bu_note("UDS connect", okp, skp,
3667 * Cross reference the peer labels for SO_PEERSEC.
3670 nsp->smk_packet = ssp->smk_out;
3671 ssp->smk_packet = osp->smk_out;
3678 * smack_unix_may_send - Smack access on UDS
3680 * @other: the other socket
3682 * Return 0 if a subject with the smack of sock could access
3683 * an object with the smack of other, otherwise an error code
3685 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3687 struct socket_smack *ssp = sock->sk->sk_security;
3688 struct socket_smack *osp = other->sk->sk_security;
3689 struct smk_audit_info ad;
3693 struct lsm_network_audit net;
3695 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3696 smk_ad_setfield_u_net_sk(&ad, other->sk);
3699 if (smack_privileged(CAP_MAC_OVERRIDE))
3702 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3703 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3708 * smack_socket_sendmsg - Smack check based on destination host
3711 * @size: the size of the message
3713 * Return 0 if the current subject can write to the destination host.
3714 * For IPv4 this is only a question if the destination is a single label host.
3715 * For IPv6 this is a check against the label of the port.
3717 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3720 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3721 #if IS_ENABLED(CONFIG_IPV6)
3722 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3724 #ifdef SMACK_IPV6_SECMARK_LABELING
3725 struct socket_smack *ssp = sock->sk->sk_security;
3726 struct smack_known *rsp;
3731 * Perfectly reasonable for this to be NULL
3736 switch (sock->sk->sk_family) {
3738 rc = smack_netlabel_send(sock->sk, sip);
3741 #ifdef SMACK_IPV6_SECMARK_LABELING
3742 rsp = smack_ipv6host_label(sap);
3744 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3747 #ifdef SMACK_IPV6_PORT_LABELING
3748 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3756 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3757 * @sap: netlabel secattr
3758 * @ssp: socket security information
3760 * Returns a pointer to a Smack label entry found on the label list.
3762 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3763 struct socket_smack *ssp)
3765 struct smack_known *skp;
3770 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3772 * Looks like a CIPSO packet.
3773 * If there are flags but no level netlabel isn't
3774 * behaving the way we expect it to.
3776 * Look it up in the label table
3777 * Without guidance regarding the smack value
3778 * for the packet fall back on the network
3782 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3783 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3786 * Compare the catsets. Use the netlbl APIs.
3788 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3789 if ((skp->smk_netlabel.flags &
3790 NETLBL_SECATTR_MLS_CAT) == 0)
3794 for (acat = -1, kcat = -1; acat == kcat; ) {
3795 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3797 kcat = netlbl_catmap_walk(
3798 skp->smk_netlabel.attr.mls.cat,
3800 if (acat < 0 || kcat < 0)
3813 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3814 return &smack_known_web;
3815 return &smack_known_star;
3817 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3819 * Looks like a fallback, which gives us a secid.
3821 return smack_from_secid(sap->attr.secid);
3823 * Without guidance regarding the smack value
3824 * for the packet fall back on the network
3827 return smack_net_ambient;
3830 #if IS_ENABLED(CONFIG_IPV6)
3831 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3835 int proto = -EINVAL;
3836 struct ipv6hdr _ipv6h;
3837 struct ipv6hdr *ip6;
3839 struct tcphdr _tcph, *th;
3840 struct udphdr _udph, *uh;
3841 struct dccp_hdr _dccph, *dh;
3845 offset = skb_network_offset(skb);
3846 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3849 sip->sin6_addr = ip6->saddr;
3851 nexthdr = ip6->nexthdr;
3852 offset += sizeof(_ipv6h);
3853 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3860 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3862 sip->sin6_port = th->source;
3865 case IPPROTO_UDPLITE:
3866 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3868 sip->sin6_port = uh->source;
3871 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3873 sip->sin6_port = dh->dccph_sport;
3878 #endif /* CONFIG_IPV6 */
3881 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3885 * Returns 0 if the packet should be delivered, an error code otherwise
3887 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3889 struct netlbl_lsm_secattr secattr;
3890 struct socket_smack *ssp = sk->sk_security;
3891 struct smack_known *skp = NULL;
3893 struct smk_audit_info ad;
3894 u16 family = sk->sk_family;
3896 struct lsm_network_audit net;
3898 #if IS_ENABLED(CONFIG_IPV6)
3899 struct sockaddr_in6 sadd;
3902 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3904 #endif /* CONFIG_IPV6 */
3908 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3910 * If there is a secmark use it rather than the CIPSO label.
3911 * If there is no secmark fall back to CIPSO.
3912 * The secmark is assumed to reflect policy better.
3914 if (skb && skb->secmark != 0) {
3915 skp = smack_from_secid(skb->secmark);
3918 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3920 * Translate what netlabel gave us.
3922 netlbl_secattr_init(&secattr);
3924 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3926 skp = smack_from_secattr(&secattr, ssp);
3928 skp = smack_net_ambient;
3930 netlbl_secattr_destroy(&secattr);
3932 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3936 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3937 ad.a.u.net->family = family;
3938 ad.a.u.net->netif = skb->skb_iif;
3939 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3942 * Receiving a packet requires that the other end
3943 * be able to write here. Read access is not required.
3944 * This is the simplist possible security model
3947 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3948 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3951 netlbl_skbuff_err(skb, family, rc, 0);
3953 #if IS_ENABLED(CONFIG_IPV6)
3955 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3956 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3957 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3959 #ifdef SMACK_IPV6_SECMARK_LABELING
3960 if (skb && skb->secmark != 0)
3961 skp = smack_from_secid(skb->secmark);
3963 skp = smack_ipv6host_label(&sadd);
3965 skp = smack_net_ambient;
3967 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3968 ad.a.u.net->family = family;
3969 ad.a.u.net->netif = skb->skb_iif;
3970 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3971 #endif /* CONFIG_AUDIT */
3972 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3973 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3975 #endif /* SMACK_IPV6_SECMARK_LABELING */
3976 #ifdef SMACK_IPV6_PORT_LABELING
3977 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3978 #endif /* SMACK_IPV6_PORT_LABELING */
3980 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3981 ICMPV6_ADM_PROHIBITED, 0);
3983 #endif /* CONFIG_IPV6 */
3990 * smack_socket_getpeersec_stream - pull in packet label
3992 * @optval: user's destination
3993 * @optlen: size thereof
3996 * returns zero on success, an error code otherwise
3998 static int smack_socket_getpeersec_stream(struct socket *sock,
3999 char __user *optval,
4000 int __user *optlen, unsigned len)
4002 struct socket_smack *ssp;
4007 ssp = sock->sk->sk_security;
4008 if (ssp->smk_packet != NULL) {
4009 rcp = ssp->smk_packet->smk_known;
4010 slen = strlen(rcp) + 1;
4015 else if (copy_to_user(optval, rcp, slen) != 0)
4018 if (put_user(slen, optlen) != 0)
4026 * smack_socket_getpeersec_dgram - pull in packet label
4027 * @sock: the peer socket
4029 * @secid: pointer to where to put the secid of the packet
4031 * Sets the netlabel socket state on sk from parent
4033 static int smack_socket_getpeersec_dgram(struct socket *sock,
4034 struct sk_buff *skb, u32 *secid)
4037 struct netlbl_lsm_secattr secattr;
4038 struct socket_smack *ssp = NULL;
4039 struct smack_known *skp;
4040 int family = PF_UNSPEC;
4041 u32 s = 0; /* 0 is the invalid secid */
4045 if (skb->protocol == htons(ETH_P_IP))
4047 #if IS_ENABLED(CONFIG_IPV6)
4048 else if (skb->protocol == htons(ETH_P_IPV6))
4050 #endif /* CONFIG_IPV6 */
4052 if (family == PF_UNSPEC && sock != NULL)
4053 family = sock->sk->sk_family;
4057 ssp = sock->sk->sk_security;
4058 s = ssp->smk_out->smk_secid;
4061 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4067 * Translate what netlabel gave us.
4069 if (sock != NULL && sock->sk != NULL)
4070 ssp = sock->sk->sk_security;
4071 netlbl_secattr_init(&secattr);
4072 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4074 skp = smack_from_secattr(&secattr, ssp);
4077 netlbl_secattr_destroy(&secattr);
4080 #ifdef SMACK_IPV6_SECMARK_LABELING
4092 * smack_sock_graft - Initialize a newly created socket with an existing sock
4094 * @parent: parent socket
4096 * Set the smk_{in,out} state of an existing sock based on the process that
4097 * is creating the new socket.
4099 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4101 struct socket_smack *ssp;
4102 struct smack_known *skp = smk_of_current();
4105 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4108 ssp = sk->sk_security;
4111 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4115 * smack_inet_conn_request - Smack access check on connect
4116 * @sk: socket involved
4120 * Returns 0 if a task with the packet label could write to
4121 * the socket, otherwise an error code
4123 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4124 struct request_sock *req)
4126 u16 family = sk->sk_family;
4127 struct smack_known *skp;
4128 struct socket_smack *ssp = sk->sk_security;
4129 struct netlbl_lsm_secattr secattr;
4130 struct sockaddr_in addr;
4132 struct smack_known *hskp;
4134 struct smk_audit_info ad;
4136 struct lsm_network_audit net;
4139 #if IS_ENABLED(CONFIG_IPV6)
4140 if (family == PF_INET6) {
4142 * Handle mapped IPv4 packets arriving
4143 * via IPv6 sockets. Don't set up netlabel
4144 * processing on IPv6.
4146 if (skb->protocol == htons(ETH_P_IP))
4151 #endif /* CONFIG_IPV6 */
4153 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4155 * If there is a secmark use it rather than the CIPSO label.
4156 * If there is no secmark fall back to CIPSO.
4157 * The secmark is assumed to reflect policy better.
4159 if (skb && skb->secmark != 0) {
4160 skp = smack_from_secid(skb->secmark);
4163 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4165 netlbl_secattr_init(&secattr);
4166 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4168 skp = smack_from_secattr(&secattr, ssp);
4170 skp = &smack_known_huh;
4171 netlbl_secattr_destroy(&secattr);
4173 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4178 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4179 ad.a.u.net->family = family;
4180 ad.a.u.net->netif = skb->skb_iif;
4181 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4184 * Receiving a packet requires that the other end be able to write
4185 * here. Read access is not required.
4187 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4188 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4193 * Save the peer's label in the request_sock so we can later setup
4194 * smk_packet in the child socket so that SO_PEERCRED can report it.
4196 req->peer_secid = skp->smk_secid;
4199 * We need to decide if we want to label the incoming connection here
4200 * if we do we only need to label the request_sock and the stack will
4201 * propagate the wire-label to the sock when it is created.
4204 addr.sin_addr.s_addr = hdr->saddr;
4206 hskp = smack_ipv4host_label(&addr);
4210 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4212 netlbl_req_delattr(req);
4218 * smack_inet_csk_clone - Copy the connection information to the new socket
4219 * @sk: the new socket
4220 * @req: the connection's request_sock
4222 * Transfer the connection's peer label to the newly created socket.
4224 static void smack_inet_csk_clone(struct sock *sk,
4225 const struct request_sock *req)
4227 struct socket_smack *ssp = sk->sk_security;
4228 struct smack_known *skp;
4230 if (req->peer_secid != 0) {
4231 skp = smack_from_secid(req->peer_secid);
4232 ssp->smk_packet = skp;
4234 ssp->smk_packet = NULL;
4238 * Key management security hooks
4240 * Casey has not tested key support very heavily.
4241 * The permission check is most likely too restrictive.
4242 * If you care about keys please have a look.
4247 * smack_key_alloc - Set the key security blob
4249 * @cred: the credentials to use
4252 * No allocation required
4256 static int smack_key_alloc(struct key *key, const struct cred *cred,
4257 unsigned long flags)
4259 struct smack_known *skp = smk_of_task(cred->security);
4261 key->security = skp;
4266 * smack_key_free - Clear the key security blob
4269 * Clear the blob pointer
4271 static void smack_key_free(struct key *key)
4273 key->security = NULL;
4277 * smack_key_permission - Smack access on a key
4278 * @key_ref: gets to the object
4279 * @cred: the credentials to use
4280 * @perm: requested key permissions
4282 * Return 0 if the task has read and write to the object,
4283 * an error code otherwise
4285 static int smack_key_permission(key_ref_t key_ref,
4286 const struct cred *cred, unsigned perm)
4289 struct smk_audit_info ad;
4290 struct smack_known *tkp = smk_of_task(cred->security);
4294 keyp = key_ref_to_ptr(key_ref);
4298 * If the key hasn't been initialized give it access so that
4301 if (keyp->security == NULL)
4304 * This should not occur
4309 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4313 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4314 ad.a.u.key_struct.key = keyp->serial;
4315 ad.a.u.key_struct.key_desc = keyp->description;
4317 if (perm & KEY_NEED_READ)
4319 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4320 request = MAY_WRITE;
4321 rc = smk_access(tkp, keyp->security, request, &ad);
4322 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4327 * smack_key_getsecurity - Smack label tagging the key
4328 * @key points to the key to be queried
4329 * @_buffer points to a pointer that should be set to point to the
4330 * resulting string (if no label or an error occurs).
4331 * Return the length of the string (including terminating NUL) or -ve if
4333 * May also return 0 (and a NULL buffer pointer) if there is no label.
4335 static int smack_key_getsecurity(struct key *key, char **_buffer)
4337 struct smack_known *skp = key->security;
4341 if (key->security == NULL) {
4346 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4349 length = strlen(copy) + 1;
4355 #endif /* CONFIG_KEYS */
4360 * Audit requires a unique representation of each Smack specific
4361 * rule. This unique representation is used to distinguish the
4362 * object to be audited from remaining kernel objects and also
4363 * works as a glue between the audit hooks.
4365 * Since repository entries are added but never deleted, we'll use
4366 * the smack_known label address related to the given audit rule as
4367 * the needed unique representation. This also better fits the smack
4368 * model where nearly everything is a label.
4373 * smack_audit_rule_init - Initialize a smack audit rule
4374 * @field: audit rule fields given from user-space (audit.h)
4375 * @op: required testing operator (=, !=, >, <, ...)
4376 * @rulestr: smack label to be audited
4377 * @vrule: pointer to save our own audit rule representation
4379 * Prepare to audit cases where (@field @op @rulestr) is true.
4380 * The label to be audited is created if necessay.
4382 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4384 struct smack_known *skp;
4385 char **rule = (char **)vrule;
4388 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4391 if (op != Audit_equal && op != Audit_not_equal)
4394 skp = smk_import_entry(rulestr, 0);
4396 return PTR_ERR(skp);
4398 *rule = skp->smk_known;
4404 * smack_audit_rule_known - Distinguish Smack audit rules
4405 * @krule: rule of interest, in Audit kernel representation format
4407 * This is used to filter Smack rules from remaining Audit ones.
4408 * If it's proved that this rule belongs to us, the
4409 * audit_rule_match hook will be called to do the final judgement.
4411 static int smack_audit_rule_known(struct audit_krule *krule)
4413 struct audit_field *f;
4416 for (i = 0; i < krule->field_count; i++) {
4417 f = &krule->fields[i];
4419 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4427 * smack_audit_rule_match - Audit given object ?
4428 * @secid: security id for identifying the object to test
4429 * @field: audit rule flags given from user-space
4430 * @op: required testing operator
4431 * @vrule: smack internal rule presentation
4432 * @actx: audit context associated with the check
4434 * The core Audit hook. It's used to take the decision of
4435 * whether to audit or not to audit a given object.
4437 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4438 struct audit_context *actx)
4440 struct smack_known *skp;
4443 if (unlikely(!rule)) {
4444 WARN_ONCE(1, "Smack: missing rule\n");
4448 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4451 skp = smack_from_secid(secid);
4454 * No need to do string comparisons. If a match occurs,
4455 * both pointers will point to the same smack_known
4458 if (op == Audit_equal)
4459 return (rule == skp->smk_known);
4460 if (op == Audit_not_equal)
4461 return (rule != skp->smk_known);
4467 * There is no need for a smack_audit_rule_free hook.
4468 * No memory was allocated.
4471 #endif /* CONFIG_AUDIT */
4474 * smack_ismaclabel - check if xattr @name references a smack MAC label
4475 * @name: Full xattr name to check.
4477 static int smack_ismaclabel(const char *name)
4479 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4484 * smack_secid_to_secctx - return the smack label for a secid
4485 * @secid: incoming integer
4486 * @secdata: destination
4487 * @seclen: how long it is
4489 * Exists for networking code.
4491 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4493 struct smack_known *skp = smack_from_secid(secid);
4496 *secdata = skp->smk_known;
4497 *seclen = strlen(skp->smk_known);
4502 * smack_secctx_to_secid - return the secid for a smack label
4503 * @secdata: smack label
4504 * @seclen: how long result is
4505 * @secid: outgoing integer
4507 * Exists for audit and networking code.
4509 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4511 struct smack_known *skp = smk_find_entry(secdata);
4514 *secid = skp->smk_secid;
4521 * There used to be a smack_release_secctx hook
4522 * that did nothing back when hooks were in a vector.
4523 * Now that there's a list such a hook adds cost.
4526 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4528 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4531 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4533 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4536 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4538 struct smack_known *skp = smk_of_inode(inode);
4540 *ctx = skp->smk_known;
4541 *ctxlen = strlen(skp->smk_known);
4545 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4548 struct task_smack *tsp;
4549 struct smack_known *skp;
4550 struct inode_smack *isp;
4551 struct cred *new_creds = *new;
4553 if (new_creds == NULL) {
4554 new_creds = prepare_creds();
4555 if (new_creds == NULL)
4559 tsp = new_creds->security;
4562 * Get label from overlay inode and set it in create_sid
4564 isp = d_inode(dentry->d_parent)->i_security;
4565 skp = isp->smk_inode;
4566 tsp->smk_task = skp;
4571 static int smack_inode_copy_up_xattr(const char *name)
4574 * Return 1 if this is the smack access Smack attribute.
4576 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4582 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4584 const struct cred *old,
4587 struct task_smack *otsp = old->security;
4588 struct task_smack *ntsp = new->security;
4589 struct inode_smack *isp;
4593 * Use the process credential unless all of
4594 * the transmuting criteria are met
4596 ntsp->smk_task = otsp->smk_task;
4599 * the attribute of the containing directory
4601 isp = d_inode(dentry->d_parent)->i_security;
4603 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4605 may = smk_access_entry(otsp->smk_task->smk_known,
4606 isp->smk_inode->smk_known,
4607 &otsp->smk_task->smk_rules);
4611 * If the directory is transmuting and the rule
4612 * providing access is transmuting use the containing
4613 * directory label instead of the process label.
4615 if (may > 0 && (may & MAY_TRANSMUTE))
4616 ntsp->smk_task = isp->smk_inode;
4621 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4622 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4623 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4624 LSM_HOOK_INIT(syslog, smack_syslog),
4626 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4627 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4628 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4629 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4630 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4631 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4633 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4635 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4636 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4637 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4638 LSM_HOOK_INIT(inode_link, smack_inode_link),
4639 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4640 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4641 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4642 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4643 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4644 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4645 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4646 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4647 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4648 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4649 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4650 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4651 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4652 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4654 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4655 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4656 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4657 LSM_HOOK_INIT(file_lock, smack_file_lock),
4658 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4659 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4660 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4661 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4662 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4663 LSM_HOOK_INIT(file_receive, smack_file_receive),
4665 LSM_HOOK_INIT(file_open, smack_file_open),
4667 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4668 LSM_HOOK_INIT(cred_free, smack_cred_free),
4669 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4670 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4671 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4672 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4673 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4674 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4675 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4676 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4677 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4678 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4679 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4680 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4681 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4682 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4683 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4684 LSM_HOOK_INIT(task_kill, smack_task_kill),
4685 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4687 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4688 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4690 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4691 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4693 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4694 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
4695 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4696 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4697 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4698 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4700 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4701 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
4702 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4703 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4704 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4706 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4707 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
4708 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4709 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4710 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4712 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4714 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4715 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4717 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4718 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4720 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4721 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4722 #ifdef SMACK_IPV6_PORT_LABELING
4723 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4725 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4726 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4727 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4728 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4729 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4730 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4731 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4732 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4733 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4734 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4736 /* key management security hooks */
4738 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4739 LSM_HOOK_INIT(key_free, smack_key_free),
4740 LSM_HOOK_INIT(key_permission, smack_key_permission),
4741 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4742 #endif /* CONFIG_KEYS */
4746 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4747 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4748 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4749 #endif /* CONFIG_AUDIT */
4751 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4752 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4753 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4754 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4755 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4756 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4757 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4758 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4759 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4763 static __init void init_smack_known_list(void)
4766 * Initialize rule list locks
4768 mutex_init(&smack_known_huh.smk_rules_lock);
4769 mutex_init(&smack_known_hat.smk_rules_lock);
4770 mutex_init(&smack_known_floor.smk_rules_lock);
4771 mutex_init(&smack_known_star.smk_rules_lock);
4772 mutex_init(&smack_known_web.smk_rules_lock);
4774 * Initialize rule lists
4776 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4777 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4778 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4779 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4780 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4782 * Create the known labels list
4784 smk_insert_entry(&smack_known_huh);
4785 smk_insert_entry(&smack_known_hat);
4786 smk_insert_entry(&smack_known_star);
4787 smk_insert_entry(&smack_known_floor);
4788 smk_insert_entry(&smack_known_web);
4792 * smack_init - initialize the smack system
4796 static __init int smack_init(void)
4799 struct task_smack *tsp;
4801 if (!security_module_enable("smack"))
4804 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4805 if (!smack_inode_cache)
4808 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4811 kmem_cache_destroy(smack_inode_cache);
4817 pr_info("Smack: Initializing.\n");
4818 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4819 pr_info("Smack: Netfilter enabled.\n");
4821 #ifdef SMACK_IPV6_PORT_LABELING
4822 pr_info("Smack: IPv6 port labeling enabled.\n");
4824 #ifdef SMACK_IPV6_SECMARK_LABELING
4825 pr_info("Smack: IPv6 Netfilter enabled.\n");
4829 * Set the security state for the initial task.
4831 cred = (struct cred *) current->cred;
4832 cred->security = tsp;
4834 /* initialize the smack_known_list */
4835 init_smack_known_list();
4840 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4846 * Smack requires early initialization in order to label
4847 * all processes and objects when they are created.
4849 DEFINE_LSM(smack) = {