2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
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;
425 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
426 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
427 smk_ad_setfield_u_tsk(&ad, tracer);
432 tsp = __task_cred(tracer)->security;
433 tracer_known = smk_of_task(tsp);
435 if ((mode & PTRACE_MODE_ATTACH) &&
436 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
437 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
438 if (tracer_known->smk_known == tracee_known->smk_known)
440 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
442 else if (capable(CAP_SYS_PTRACE))
448 smack_log(tracer_known->smk_known,
449 tracee_known->smk_known,
456 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
457 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
465 * We he, that is fun!
469 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
470 * @ctp: child task pointer
471 * @mode: ptrace attachment mode (PTRACE_MODE_*)
473 * Returns 0 if access is OK, an error code otherwise
475 * Do the capability checks.
477 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
479 struct smack_known *skp;
481 skp = smk_of_task_struct(ctp);
483 return smk_ptrace_rule_check(current, skp, mode, __func__);
487 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
488 * @ptp: parent task pointer
490 * Returns 0 if access is OK, an error code otherwise
492 * Do the capability checks, and require PTRACE_MODE_ATTACH.
494 static int smack_ptrace_traceme(struct task_struct *ptp)
497 struct smack_known *skp;
499 skp = smk_of_task(current_security());
501 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
506 * smack_syslog - Smack approval on syslog
507 * @type: message type
509 * Returns 0 on success, error code otherwise.
511 static int smack_syslog(int typefrom_file)
514 struct smack_known *skp = smk_of_current();
516 if (smack_privileged(CAP_MAC_OVERRIDE))
519 if (smack_syslog_label != NULL && smack_syslog_label != skp)
531 * smack_sb_alloc_security - allocate a superblock blob
532 * @sb: the superblock getting the blob
534 * Returns 0 on success or -ENOMEM on error.
536 static int smack_sb_alloc_security(struct super_block *sb)
538 struct superblock_smack *sbsp;
540 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
545 sbsp->smk_root = &smack_known_floor;
546 sbsp->smk_default = &smack_known_floor;
547 sbsp->smk_floor = &smack_known_floor;
548 sbsp->smk_hat = &smack_known_hat;
550 * SMK_SB_INITIALIZED will be zero from kzalloc.
552 sb->s_security = sbsp;
558 * smack_sb_free_security - free a superblock blob
559 * @sb: the superblock getting the blob
562 static void smack_sb_free_security(struct super_block *sb)
564 kfree(sb->s_security);
565 sb->s_security = NULL;
569 * smack_sb_copy_data - copy mount options data for processing
570 * @orig: where to start
571 * @smackopts: mount options string
573 * Returns 0 on success or -ENOMEM on error.
575 * Copy the Smack specific mount options out of the mount
578 static int smack_sb_copy_data(char *orig, char *smackopts)
580 char *cp, *commap, *otheropts, *dp;
582 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
583 if (otheropts == NULL)
586 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
587 if (strstr(cp, SMK_FSDEFAULT) == cp)
589 else if (strstr(cp, SMK_FSFLOOR) == cp)
591 else if (strstr(cp, SMK_FSHAT) == cp)
593 else if (strstr(cp, SMK_FSROOT) == cp)
595 else if (strstr(cp, SMK_FSTRANS) == cp)
600 commap = strchr(cp, ',');
609 strcpy(orig, otheropts);
610 free_page((unsigned long)otheropts);
616 * smack_parse_opts_str - parse Smack specific mount options
617 * @options: mount options string
618 * @opts: where to store converted mount opts
620 * Returns 0 on success or -ENOMEM on error.
622 * converts Smack specific mount options to generic security option format
624 static int smack_parse_opts_str(char *options,
625 struct security_mnt_opts *opts)
628 char *fsdefault = NULL;
629 char *fsfloor = NULL;
632 char *fstransmute = NULL;
634 int num_mnt_opts = 0;
637 opts->num_mnt_opts = 0;
642 while ((p = strsep(&options, ",")) != NULL) {
643 substring_t args[MAX_OPT_ARGS];
648 token = match_token(p, smk_mount_tokens, args);
654 fsdefault = match_strdup(&args[0]);
661 fsfloor = match_strdup(&args[0]);
668 fshat = match_strdup(&args[0]);
675 fsroot = match_strdup(&args[0]);
679 case Opt_fstransmute:
682 fstransmute = match_strdup(&args[0]);
688 pr_warn("Smack: unknown mount option\n");
693 opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_KERNEL);
697 opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
699 if (!opts->mnt_opts_flags)
703 opts->mnt_opts[num_mnt_opts] = fsdefault;
704 opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
707 opts->mnt_opts[num_mnt_opts] = fsfloor;
708 opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
711 opts->mnt_opts[num_mnt_opts] = fshat;
712 opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
715 opts->mnt_opts[num_mnt_opts] = fsroot;
716 opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
719 opts->mnt_opts[num_mnt_opts] = fstransmute;
720 opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
723 opts->num_mnt_opts = num_mnt_opts;
728 pr_warn("Smack: duplicate mount options\n");
740 * smack_set_mnt_opts - set Smack specific mount options
741 * @sb: the file system superblock
742 * @opts: Smack mount options
743 * @kern_flags: mount option from kernel space or user space
744 * @set_kern_flags: where to store converted mount opts
746 * Returns 0 on success, an error code on failure
748 * Allow filesystems with binary mount data to explicitly set Smack mount
751 static int smack_set_mnt_opts(struct super_block *sb,
752 struct security_mnt_opts *opts,
753 unsigned long kern_flags,
754 unsigned long *set_kern_flags)
756 struct dentry *root = sb->s_root;
757 struct inode *inode = d_backing_inode(root);
758 struct superblock_smack *sp = sb->s_security;
759 struct inode_smack *isp;
760 struct smack_known *skp;
762 int num_opts = opts->num_mnt_opts;
765 if (sp->smk_flags & SMK_SB_INITIALIZED)
768 if (!smack_privileged(CAP_MAC_ADMIN)) {
770 * Unprivileged mounts don't get to specify Smack values.
775 * Unprivileged mounts get root and default from the caller.
777 skp = smk_of_current();
779 sp->smk_default = skp;
781 * For a handful of fs types with no user-controlled
782 * backing store it's okay to trust security labels
783 * in the filesystem. The rest are untrusted.
785 if (sb->s_user_ns != &init_user_ns &&
786 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
787 sb->s_magic != RAMFS_MAGIC) {
789 sp->smk_flags |= SMK_SB_UNTRUSTED;
793 sp->smk_flags |= SMK_SB_INITIALIZED;
795 for (i = 0; i < num_opts; i++) {
796 switch (opts->mnt_opts_flags[i]) {
798 skp = smk_import_entry(opts->mnt_opts[i], 0);
801 sp->smk_default = skp;
804 skp = smk_import_entry(opts->mnt_opts[i], 0);
810 skp = smk_import_entry(opts->mnt_opts[i], 0);
816 skp = smk_import_entry(opts->mnt_opts[i], 0);
822 skp = smk_import_entry(opts->mnt_opts[i], 0);
834 * Initialize the root inode.
836 isp = inode->i_security;
838 isp = new_inode_smack(sp->smk_root);
841 inode->i_security = isp;
843 isp->smk_inode = sp->smk_root;
846 isp->smk_flags |= SMK_INODE_TRANSMUTE;
852 * smack_sb_kern_mount - Smack specific mount processing
853 * @sb: the file system superblock
854 * @flags: the mount flags
855 * @data: the smack mount options
857 * Returns 0 on success, an error code on failure
859 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
862 char *options = data;
863 struct security_mnt_opts opts;
865 security_init_mnt_opts(&opts);
870 rc = smack_parse_opts_str(options, &opts);
875 rc = smack_set_mnt_opts(sb, &opts, 0, NULL);
878 security_free_mnt_opts(&opts);
883 * smack_sb_statfs - Smack check on statfs
884 * @dentry: identifies the file system in question
886 * Returns 0 if current can read the floor of the filesystem,
887 * and error code otherwise
889 static int smack_sb_statfs(struct dentry *dentry)
891 struct superblock_smack *sbp = dentry->d_sb->s_security;
893 struct smk_audit_info ad;
895 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
896 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
898 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
899 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
908 * smack_bprm_set_creds - set creds for exec
909 * @bprm: the exec information
911 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
913 static int smack_bprm_set_creds(struct linux_binprm *bprm)
915 struct inode *inode = file_inode(bprm->file);
916 struct task_smack *bsp = bprm->cred->security;
917 struct inode_smack *isp;
918 struct superblock_smack *sbsp;
921 if (bprm->called_set_creds)
924 isp = inode->i_security;
925 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
928 sbsp = inode->i_sb->s_security;
929 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
930 isp->smk_task != sbsp->smk_root)
933 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
934 struct task_struct *tracer;
938 tracer = ptrace_parent(current);
939 if (likely(tracer != NULL))
940 rc = smk_ptrace_rule_check(tracer,
948 } else if (bprm->unsafe)
951 bsp->smk_task = isp->smk_task;
952 bprm->per_clear |= PER_CLEAR_ON_SETID;
954 /* Decide if this is a secure exec. */
955 if (bsp->smk_task != bsp->smk_forked)
956 bprm->secureexec = 1;
966 * smack_inode_alloc_security - allocate an inode blob
967 * @inode: the inode in need of a blob
969 * Returns 0 if it gets a blob, -ENOMEM otherwise
971 static int smack_inode_alloc_security(struct inode *inode)
973 struct smack_known *skp = smk_of_current();
975 inode->i_security = new_inode_smack(skp);
976 if (inode->i_security == NULL)
982 * smack_inode_free_rcu - Free inode_smack blob from cache
983 * @head: the rcu_head for getting inode_smack pointer
985 * Call back function called from call_rcu() to free
986 * the i_security blob pointer in inode
988 static void smack_inode_free_rcu(struct rcu_head *head)
990 struct inode_smack *issp;
992 issp = container_of(head, struct inode_smack, smk_rcu);
993 kmem_cache_free(smack_inode_cache, issp);
997 * smack_inode_free_security - free an inode blob using call_rcu()
998 * @inode: the inode with a blob
1000 * Clears the blob pointer in inode using RCU
1002 static void smack_inode_free_security(struct inode *inode)
1004 struct inode_smack *issp = inode->i_security;
1007 * The inode may still be referenced in a path walk and
1008 * a call to smack_inode_permission() can be made
1009 * after smack_inode_free_security() is called.
1010 * To avoid race condition free the i_security via RCU
1011 * and leave the current inode->i_security pointer intact.
1012 * The inode will be freed after the RCU grace period too.
1014 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
1018 * smack_inode_init_security - copy out the smack from an inode
1019 * @inode: the newly created inode
1020 * @dir: containing directory object
1022 * @name: where to put the attribute name
1023 * @value: where to put the attribute value
1024 * @len: where to put the length of the attribute
1026 * Returns 0 if it all works out, -ENOMEM if there's no memory
1028 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
1029 const struct qstr *qstr, const char **name,
1030 void **value, size_t *len)
1032 struct inode_smack *issp = inode->i_security;
1033 struct smack_known *skp = smk_of_current();
1034 struct smack_known *isp = smk_of_inode(inode);
1035 struct smack_known *dsp = smk_of_inode(dir);
1039 *name = XATTR_SMACK_SUFFIX;
1043 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1048 * If the access rule allows transmutation and
1049 * the directory requests transmutation then
1050 * by all means transmute.
1051 * Mark the inode as changed.
1053 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1054 smk_inode_transmutable(dir)) {
1056 issp->smk_flags |= SMK_INODE_CHANGED;
1059 *value = kstrdup(isp->smk_known, GFP_NOFS);
1063 *len = strlen(isp->smk_known);
1070 * smack_inode_link - Smack check on link
1071 * @old_dentry: the existing object
1073 * @new_dentry: the new object
1075 * Returns 0 if access is permitted, an error code otherwise
1077 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1078 struct dentry *new_dentry)
1080 struct smack_known *isp;
1081 struct smk_audit_info ad;
1084 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1085 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1087 isp = smk_of_inode(d_backing_inode(old_dentry));
1088 rc = smk_curacc(isp, MAY_WRITE, &ad);
1089 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1091 if (rc == 0 && d_is_positive(new_dentry)) {
1092 isp = smk_of_inode(d_backing_inode(new_dentry));
1093 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1094 rc = smk_curacc(isp, MAY_WRITE, &ad);
1095 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1102 * smack_inode_unlink - Smack check on inode deletion
1103 * @dir: containing directory object
1104 * @dentry: file to unlink
1106 * Returns 0 if current can write the containing directory
1107 * and the object, error code otherwise
1109 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1111 struct inode *ip = d_backing_inode(dentry);
1112 struct smk_audit_info ad;
1115 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1116 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1119 * You need write access to the thing you're unlinking
1121 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1122 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1125 * You also need write access to the containing directory
1127 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1128 smk_ad_setfield_u_fs_inode(&ad, dir);
1129 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1130 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1136 * smack_inode_rmdir - Smack check on directory deletion
1137 * @dir: containing directory object
1138 * @dentry: directory to unlink
1140 * Returns 0 if current can write the containing directory
1141 * and the directory, error code otherwise
1143 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1145 struct smk_audit_info ad;
1148 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1149 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1152 * You need write access to the thing you're removing
1154 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1155 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1158 * You also need write access to the containing directory
1160 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1161 smk_ad_setfield_u_fs_inode(&ad, dir);
1162 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1163 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1170 * smack_inode_rename - Smack check on rename
1171 * @old_inode: unused
1172 * @old_dentry: the old object
1173 * @new_inode: unused
1174 * @new_dentry: the new object
1176 * Read and write access is required on both the old and
1179 * Returns 0 if access is permitted, an error code otherwise
1181 static int smack_inode_rename(struct inode *old_inode,
1182 struct dentry *old_dentry,
1183 struct inode *new_inode,
1184 struct dentry *new_dentry)
1187 struct smack_known *isp;
1188 struct smk_audit_info ad;
1190 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1191 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1193 isp = smk_of_inode(d_backing_inode(old_dentry));
1194 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1195 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1197 if (rc == 0 && d_is_positive(new_dentry)) {
1198 isp = smk_of_inode(d_backing_inode(new_dentry));
1199 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1200 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1201 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1207 * smack_inode_permission - Smack version of permission()
1208 * @inode: the inode in question
1209 * @mask: the access requested
1211 * This is the important Smack hook.
1213 * Returns 0 if access is permitted, -EACCES otherwise
1215 static int smack_inode_permission(struct inode *inode, int mask)
1217 struct superblock_smack *sbsp = inode->i_sb->s_security;
1218 struct smk_audit_info ad;
1219 int no_block = mask & MAY_NOT_BLOCK;
1222 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1224 * No permission to check. Existence test. Yup, it's there.
1229 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1230 if (smk_of_inode(inode) != sbsp->smk_root)
1234 /* May be droppable after audit */
1237 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1238 smk_ad_setfield_u_fs_inode(&ad, inode);
1239 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1240 rc = smk_bu_inode(inode, mask, rc);
1245 * smack_inode_setattr - Smack check for setting attributes
1246 * @dentry: the object
1247 * @iattr: for the force flag
1249 * Returns 0 if access is permitted, an error code otherwise
1251 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1253 struct smk_audit_info ad;
1257 * Need to allow for clearing the setuid bit.
1259 if (iattr->ia_valid & ATTR_FORCE)
1261 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1262 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1264 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1265 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1270 * smack_inode_getattr - Smack check for getting attributes
1271 * @mnt: vfsmount of the object
1272 * @dentry: the object
1274 * Returns 0 if access is permitted, an error code otherwise
1276 static int smack_inode_getattr(const struct path *path)
1278 struct smk_audit_info ad;
1279 struct inode *inode = d_backing_inode(path->dentry);
1282 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1283 smk_ad_setfield_u_fs_path(&ad, *path);
1284 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1285 rc = smk_bu_inode(inode, MAY_READ, rc);
1290 * smack_inode_setxattr - Smack check for setting xattrs
1291 * @dentry: the object
1292 * @name: name of the attribute
1293 * @value: value of the attribute
1294 * @size: size of the value
1297 * This protects the Smack attribute explicitly.
1299 * Returns 0 if access is permitted, an error code otherwise
1301 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1302 const void *value, size_t size, int flags)
1304 struct smk_audit_info ad;
1305 struct smack_known *skp;
1307 int check_import = 0;
1312 * Check label validity here so import won't fail in post_setxattr
1314 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1315 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1316 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1319 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1320 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1324 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1326 if (size != TRANS_TRUE_SIZE ||
1327 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1330 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1332 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1335 if (rc == 0 && check_import) {
1336 skp = size ? smk_import_entry(value, size) : NULL;
1339 else if (skp == NULL || (check_star &&
1340 (skp == &smack_known_star || skp == &smack_known_web)))
1344 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1345 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1348 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1349 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1356 * smack_inode_post_setxattr - Apply the Smack update approved above
1358 * @name: attribute name
1359 * @value: attribute value
1360 * @size: attribute size
1363 * Set the pointer in the inode blob to the entry found
1364 * in the master label list.
1366 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1367 const void *value, size_t size, int flags)
1369 struct smack_known *skp;
1370 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
1372 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1373 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1377 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1378 skp = smk_import_entry(value, size);
1380 isp->smk_inode = skp;
1381 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1382 skp = smk_import_entry(value, size);
1384 isp->smk_task = skp;
1385 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1386 skp = smk_import_entry(value, size);
1388 isp->smk_mmap = skp;
1395 * smack_inode_getxattr - Smack check on getxattr
1396 * @dentry: the object
1399 * Returns 0 if access is permitted, an error code otherwise
1401 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1403 struct smk_audit_info ad;
1406 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1407 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1409 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1410 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1415 * smack_inode_removexattr - Smack check on removexattr
1416 * @dentry: the object
1417 * @name: name of the attribute
1419 * Removing the Smack attribute requires CAP_MAC_ADMIN
1421 * Returns 0 if access is permitted, an error code otherwise
1423 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1425 struct inode_smack *isp;
1426 struct smk_audit_info ad;
1429 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1430 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1431 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1432 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1433 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1434 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1435 if (!smack_privileged(CAP_MAC_ADMIN))
1438 rc = cap_inode_removexattr(dentry, name);
1443 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1444 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1446 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1447 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1451 isp = d_backing_inode(dentry)->i_security;
1453 * Don't do anything special for these.
1454 * XATTR_NAME_SMACKIPIN
1455 * XATTR_NAME_SMACKIPOUT
1457 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1458 struct super_block *sbp = dentry->d_sb;
1459 struct superblock_smack *sbsp = sbp->s_security;
1461 isp->smk_inode = sbsp->smk_default;
1462 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1463 isp->smk_task = NULL;
1464 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1465 isp->smk_mmap = NULL;
1466 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1467 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1473 * smack_inode_getsecurity - get smack xattrs
1474 * @inode: the object
1475 * @name: attribute name
1476 * @buffer: where to put the result
1477 * @alloc: duplicate memory
1479 * Returns the size of the attribute or an error code
1481 static int smack_inode_getsecurity(struct inode *inode,
1482 const char *name, void **buffer,
1485 struct socket_smack *ssp;
1486 struct socket *sock;
1487 struct super_block *sbp;
1488 struct inode *ip = (struct inode *)inode;
1489 struct smack_known *isp;
1491 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1492 isp = smk_of_inode(inode);
1495 * The rest of the Smack xattrs are only on sockets.
1498 if (sbp->s_magic != SOCKFS_MAGIC)
1501 sock = SOCKET_I(ip);
1502 if (sock == NULL || sock->sk == NULL)
1505 ssp = sock->sk->sk_security;
1507 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1509 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1516 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1517 if (*buffer == NULL)
1521 return strlen(isp->smk_known);
1526 * smack_inode_listsecurity - list the Smack attributes
1527 * @inode: the object
1528 * @buffer: where they go
1529 * @buffer_size: size of buffer
1531 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1534 int len = sizeof(XATTR_NAME_SMACK);
1536 if (buffer != NULL && len <= buffer_size)
1537 memcpy(buffer, XATTR_NAME_SMACK, len);
1543 * smack_inode_getsecid - Extract inode's security id
1544 * @inode: inode to extract the info from
1545 * @secid: where result will be saved
1547 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1549 struct smack_known *skp = smk_of_inode(inode);
1551 *secid = skp->smk_secid;
1559 * There is no smack_file_permission hook
1561 * Should access checks be done on each read or write?
1562 * UNICOS and SELinux say yes.
1563 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1565 * I'll say no for now. Smack does not do the frequent
1566 * label changing that SELinux does.
1570 * smack_file_alloc_security - assign a file security blob
1573 * The security blob for a file is a pointer to the master
1574 * label list, so no allocation is done.
1576 * f_security is the owner security information. It
1577 * isn't used on file access checks, it's for send_sigio.
1581 static int smack_file_alloc_security(struct file *file)
1583 struct smack_known *skp = smk_of_current();
1585 file->f_security = skp;
1590 * smack_file_free_security - clear a file security blob
1593 * The security blob for a file is a pointer to the master
1594 * label list, so no memory is freed.
1596 static void smack_file_free_security(struct file *file)
1598 file->f_security = NULL;
1602 * smack_file_ioctl - Smack check on ioctls
1607 * Relies heavily on the correct use of the ioctl command conventions.
1609 * Returns 0 if allowed, error code otherwise
1611 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1615 struct smk_audit_info ad;
1616 struct inode *inode = file_inode(file);
1618 if (unlikely(IS_PRIVATE(inode)))
1621 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1622 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1624 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1625 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1626 rc = smk_bu_file(file, MAY_WRITE, rc);
1629 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1630 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1631 rc = smk_bu_file(file, MAY_READ, rc);
1638 * smack_file_lock - Smack check on file locking
1642 * Returns 0 if current has lock access, error code otherwise
1644 static int smack_file_lock(struct file *file, unsigned int cmd)
1646 struct smk_audit_info ad;
1648 struct inode *inode = file_inode(file);
1650 if (unlikely(IS_PRIVATE(inode)))
1653 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1654 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1655 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1656 rc = smk_bu_file(file, MAY_LOCK, rc);
1661 * smack_file_fcntl - Smack check on fcntl
1663 * @cmd: what action to check
1666 * Generally these operations are harmless.
1667 * File locking operations present an obvious mechanism
1668 * for passing information, so they require write access.
1670 * Returns 0 if current has access, error code otherwise
1672 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1675 struct smk_audit_info ad;
1677 struct inode *inode = file_inode(file);
1679 if (unlikely(IS_PRIVATE(inode)))
1687 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1688 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1689 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1690 rc = smk_bu_file(file, MAY_LOCK, rc);
1694 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1695 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1696 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1697 rc = smk_bu_file(file, MAY_WRITE, rc);
1708 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1709 * if mapping anonymous memory.
1710 * @file contains the file structure for file to map (may be NULL).
1711 * @reqprot contains the protection requested by the application.
1712 * @prot contains the protection that will be applied by the kernel.
1713 * @flags contains the operational flags.
1714 * Return 0 if permission is granted.
1716 static int smack_mmap_file(struct file *file,
1717 unsigned long reqprot, unsigned long prot,
1718 unsigned long flags)
1720 struct smack_known *skp;
1721 struct smack_known *mkp;
1722 struct smack_rule *srp;
1723 struct task_smack *tsp;
1724 struct smack_known *okp;
1725 struct inode_smack *isp;
1726 struct superblock_smack *sbsp;
1735 if (unlikely(IS_PRIVATE(file_inode(file))))
1738 isp = file_inode(file)->i_security;
1739 if (isp->smk_mmap == NULL)
1741 sbsp = file_inode(file)->i_sb->s_security;
1742 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1743 isp->smk_mmap != sbsp->smk_root)
1745 mkp = isp->smk_mmap;
1747 tsp = current_security();
1748 skp = smk_of_current();
1753 * For each Smack rule associated with the subject
1754 * label verify that the SMACK64MMAP also has access
1755 * to that rule's object label.
1757 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1758 okp = srp->smk_object;
1760 * Matching labels always allows access.
1762 if (mkp->smk_known == okp->smk_known)
1765 * If there is a matching local rule take
1766 * that into account as well.
1768 may = smk_access_entry(srp->smk_subject->smk_known,
1772 may = srp->smk_access;
1774 may &= srp->smk_access;
1776 * If may is zero the SMACK64MMAP subject can't
1777 * possibly have less access.
1783 * Fetch the global list entry.
1784 * If there isn't one a SMACK64MMAP subject
1785 * can't have as much access as current.
1787 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1789 if (mmay == -ENOENT) {
1794 * If there is a local entry it modifies the
1795 * potential access, too.
1797 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1799 if (tmay != -ENOENT)
1803 * If there is any access available to current that is
1804 * not available to a SMACK64MMAP subject
1807 if ((may | mmay) != mmay) {
1819 * smack_file_set_fowner - set the file security blob value
1820 * @file: object in question
1823 static void smack_file_set_fowner(struct file *file)
1825 file->f_security = smk_of_current();
1829 * smack_file_send_sigiotask - Smack on sigio
1830 * @tsk: The target task
1831 * @fown: the object the signal come from
1834 * Allow a privileged task to get signals even if it shouldn't
1836 * Returns 0 if a subject with the object's smack could
1837 * write to the task, an error code otherwise.
1839 static int smack_file_send_sigiotask(struct task_struct *tsk,
1840 struct fown_struct *fown, int signum)
1842 struct smack_known *skp;
1843 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1846 struct smk_audit_info ad;
1849 * struct fown_struct is never outside the context of a struct file
1851 file = container_of(fown, struct file, f_owner);
1853 /* we don't log here as rc can be overriden */
1854 skp = file->f_security;
1855 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1856 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1857 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1860 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1861 smk_ad_setfield_u_tsk(&ad, tsk);
1862 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1867 * smack_file_receive - Smack file receive check
1870 * Returns 0 if current has access, error code otherwise
1872 static int smack_file_receive(struct file *file)
1876 struct smk_audit_info ad;
1877 struct inode *inode = file_inode(file);
1878 struct socket *sock;
1879 struct task_smack *tsp;
1880 struct socket_smack *ssp;
1882 if (unlikely(IS_PRIVATE(inode)))
1885 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1886 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1888 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1889 sock = SOCKET_I(inode);
1890 ssp = sock->sk->sk_security;
1891 tsp = current_security();
1893 * If the receiving process can't write to the
1894 * passed socket or if the passed socket can't
1895 * write to the receiving process don't accept
1896 * the passed socket.
1898 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1899 rc = smk_bu_file(file, may, rc);
1902 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1903 rc = smk_bu_file(file, may, rc);
1907 * This code relies on bitmasks.
1909 if (file->f_mode & FMODE_READ)
1911 if (file->f_mode & FMODE_WRITE)
1914 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1915 rc = smk_bu_file(file, may, rc);
1920 * smack_file_open - Smack dentry open processing
1922 * @cred: task credential
1924 * Set the security blob in the file structure.
1925 * Allow the open only if the task has read access. There are
1926 * many read operations (e.g. fstat) that you can do with an
1927 * fd even if you have the file open write-only.
1931 static int smack_file_open(struct file *file)
1933 struct task_smack *tsp = file->f_cred->security;
1934 struct inode *inode = file_inode(file);
1935 struct smk_audit_info ad;
1938 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1939 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1940 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1941 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1951 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1952 * @new: the new credentials
1953 * @gfp: the atomicity of any memory allocations
1955 * Prepare a blank set of credentials for modification. This must allocate all
1956 * the memory the LSM module might require such that cred_transfer() can
1957 * complete without error.
1959 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1961 struct task_smack *tsp;
1963 tsp = new_task_smack(NULL, NULL, gfp);
1967 cred->security = tsp;
1974 * smack_cred_free - "free" task-level security credentials
1975 * @cred: the credentials in question
1978 static void smack_cred_free(struct cred *cred)
1980 struct task_smack *tsp = cred->security;
1981 struct smack_rule *rp;
1982 struct list_head *l;
1983 struct list_head *n;
1987 cred->security = NULL;
1989 smk_destroy_label_list(&tsp->smk_relabel);
1991 list_for_each_safe(l, n, &tsp->smk_rules) {
1992 rp = list_entry(l, struct smack_rule, list);
1993 list_del(&rp->list);
2000 * smack_cred_prepare - prepare new set of credentials for modification
2001 * @new: the new credentials
2002 * @old: the original credentials
2003 * @gfp: the atomicity of any memory allocations
2005 * Prepare a new set of credentials for modification.
2007 static int smack_cred_prepare(struct cred *new, const struct cred *old,
2010 struct task_smack *old_tsp = old->security;
2011 struct task_smack *new_tsp;
2014 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
2015 if (new_tsp == NULL)
2018 new->security = new_tsp;
2020 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2024 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2033 * smack_cred_transfer - Transfer the old credentials to the new credentials
2034 * @new: the new credentials
2035 * @old: the original credentials
2037 * Fill in a set of blank credentials from another set of credentials.
2039 static void smack_cred_transfer(struct cred *new, const struct cred *old)
2041 struct task_smack *old_tsp = old->security;
2042 struct task_smack *new_tsp = new->security;
2044 new_tsp->smk_task = old_tsp->smk_task;
2045 new_tsp->smk_forked = old_tsp->smk_task;
2046 mutex_init(&new_tsp->smk_rules_lock);
2047 INIT_LIST_HEAD(&new_tsp->smk_rules);
2050 /* cbs copy rule list */
2054 * smack_cred_getsecid - get the secid corresponding to a creds structure
2055 * @c: the object creds
2056 * @secid: where to put the result
2058 * Sets the secid to contain a u32 version of the smack label.
2060 static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2062 struct smack_known *skp;
2065 skp = smk_of_task(c->security);
2066 *secid = skp->smk_secid;
2071 * smack_kernel_act_as - Set the subjective context in a set of credentials
2072 * @new: points to the set of credentials to be modified.
2073 * @secid: specifies the security ID to be set
2075 * Set the security data for a kernel service.
2077 static int smack_kernel_act_as(struct cred *new, u32 secid)
2079 struct task_smack *new_tsp = new->security;
2081 new_tsp->smk_task = smack_from_secid(secid);
2086 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2087 * @new: points to the set of credentials to be modified
2088 * @inode: points to the inode to use as a reference
2090 * Set the file creation context in a set of credentials to the same
2091 * as the objective context of the specified inode
2093 static int smack_kernel_create_files_as(struct cred *new,
2094 struct inode *inode)
2096 struct inode_smack *isp = inode->i_security;
2097 struct task_smack *tsp = new->security;
2099 tsp->smk_forked = isp->smk_inode;
2100 tsp->smk_task = tsp->smk_forked;
2105 * smk_curacc_on_task - helper to log task related access
2106 * @p: the task object
2107 * @access: the access requested
2108 * @caller: name of the calling function for audit
2110 * Return 0 if access is permitted
2112 static int smk_curacc_on_task(struct task_struct *p, int access,
2115 struct smk_audit_info ad;
2116 struct smack_known *skp = smk_of_task_struct(p);
2119 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2120 smk_ad_setfield_u_tsk(&ad, p);
2121 rc = smk_curacc(skp, access, &ad);
2122 rc = smk_bu_task(p, access, rc);
2127 * smack_task_setpgid - Smack check on setting pgid
2128 * @p: the task object
2131 * Return 0 if write access is permitted
2133 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2135 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2139 * smack_task_getpgid - Smack access check for getpgid
2140 * @p: the object task
2142 * Returns 0 if current can read the object task, error code otherwise
2144 static int smack_task_getpgid(struct task_struct *p)
2146 return smk_curacc_on_task(p, MAY_READ, __func__);
2150 * smack_task_getsid - Smack access check for getsid
2151 * @p: the object task
2153 * Returns 0 if current can read the object task, error code otherwise
2155 static int smack_task_getsid(struct task_struct *p)
2157 return smk_curacc_on_task(p, MAY_READ, __func__);
2161 * smack_task_getsecid - get the secid of the task
2162 * @p: the object task
2163 * @secid: where to put the result
2165 * Sets the secid to contain a u32 version of the smack label.
2167 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2169 struct smack_known *skp = smk_of_task_struct(p);
2171 *secid = skp->smk_secid;
2175 * smack_task_setnice - Smack check on setting nice
2176 * @p: the task object
2179 * Return 0 if write access is permitted
2181 static int smack_task_setnice(struct task_struct *p, int nice)
2183 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2187 * smack_task_setioprio - Smack check on setting ioprio
2188 * @p: the task object
2191 * Return 0 if write access is permitted
2193 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2195 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2199 * smack_task_getioprio - Smack check on reading ioprio
2200 * @p: the task object
2202 * Return 0 if read access is permitted
2204 static int smack_task_getioprio(struct task_struct *p)
2206 return smk_curacc_on_task(p, MAY_READ, __func__);
2210 * smack_task_setscheduler - Smack check on setting scheduler
2211 * @p: the task object
2215 * Return 0 if read access is permitted
2217 static int smack_task_setscheduler(struct task_struct *p)
2219 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2223 * smack_task_getscheduler - Smack check on reading scheduler
2224 * @p: the task object
2226 * Return 0 if read access is permitted
2228 static int smack_task_getscheduler(struct task_struct *p)
2230 return smk_curacc_on_task(p, MAY_READ, __func__);
2234 * smack_task_movememory - Smack check on moving memory
2235 * @p: the task object
2237 * Return 0 if write access is permitted
2239 static int smack_task_movememory(struct task_struct *p)
2241 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2245 * smack_task_kill - Smack check on signal delivery
2246 * @p: the task object
2249 * @cred: identifies the cred to use in lieu of current's
2251 * Return 0 if write access is permitted
2254 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2255 int sig, const struct cred *cred)
2257 struct smk_audit_info ad;
2258 struct smack_known *skp;
2259 struct smack_known *tkp = smk_of_task_struct(p);
2263 return 0; /* null signal; existence test */
2265 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2266 smk_ad_setfield_u_tsk(&ad, p);
2268 * Sending a signal requires that the sender
2269 * can write the receiver.
2272 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2273 rc = smk_bu_task(p, MAY_DELIVER, rc);
2277 * If the cred isn't NULL we're dealing with some USB IO
2278 * specific behavior. This is not clean. For one thing
2279 * we can't take privilege into account.
2281 skp = smk_of_task(cred->security);
2282 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2283 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2288 * smack_task_to_inode - copy task smack into the inode blob
2289 * @p: task to copy from
2290 * @inode: inode to copy to
2292 * Sets the smack pointer in the inode security blob
2294 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2296 struct inode_smack *isp = inode->i_security;
2297 struct smack_known *skp = smk_of_task_struct(p);
2299 isp->smk_inode = skp;
2300 isp->smk_flags |= SMK_INODE_INSTANT;
2308 * smack_sk_alloc_security - Allocate a socket blob
2311 * @gfp_flags: memory allocation flags
2313 * Assign Smack pointers to current
2315 * Returns 0 on success, -ENOMEM is there's no memory
2317 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2319 struct smack_known *skp = smk_of_current();
2320 struct socket_smack *ssp;
2322 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2327 * Sockets created by kernel threads receive web label.
2329 if (unlikely(current->flags & PF_KTHREAD)) {
2330 ssp->smk_in = &smack_known_web;
2331 ssp->smk_out = &smack_known_web;
2336 ssp->smk_packet = NULL;
2338 sk->sk_security = ssp;
2344 * smack_sk_free_security - Free a socket blob
2347 * Clears the blob pointer
2349 static void smack_sk_free_security(struct sock *sk)
2351 #ifdef SMACK_IPV6_PORT_LABELING
2352 struct smk_port_label *spp;
2354 if (sk->sk_family == PF_INET6) {
2356 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2357 if (spp->smk_sock != sk)
2359 spp->smk_can_reuse = 1;
2365 kfree(sk->sk_security);
2369 * smack_ipv4host_label - check host based restrictions
2370 * @sip: the object end
2372 * looks for host based access restrictions
2374 * This version will only be appropriate for really small sets of single label
2375 * hosts. The caller is responsible for ensuring that the RCU read lock is
2376 * taken before calling this function.
2378 * Returns the label of the far end or NULL if it's not special.
2380 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2382 struct smk_net4addr *snp;
2383 struct in_addr *siap = &sip->sin_addr;
2385 if (siap->s_addr == 0)
2388 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2390 * we break after finding the first match because
2391 * the list is sorted from longest to shortest mask
2392 * so we have found the most specific match
2394 if (snp->smk_host.s_addr ==
2395 (siap->s_addr & snp->smk_mask.s_addr))
2396 return snp->smk_label;
2401 #if IS_ENABLED(CONFIG_IPV6)
2403 * smk_ipv6_localhost - Check for local ipv6 host address
2406 * Returns boolean true if this is the localhost address
2408 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2410 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2411 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2413 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2414 ntohs(be16p[7]) == 1)
2420 * smack_ipv6host_label - check host based restrictions
2421 * @sip: the object end
2423 * looks for host based access restrictions
2425 * This version will only be appropriate for really small sets of single label
2426 * hosts. The caller is responsible for ensuring that the RCU read lock is
2427 * taken before calling this function.
2429 * Returns the label of the far end or NULL if it's not special.
2431 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2433 struct smk_net6addr *snp;
2434 struct in6_addr *sap = &sip->sin6_addr;
2439 * It's local. Don't look for a host label.
2441 if (smk_ipv6_localhost(sip))
2444 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2446 * If the label is NULL the entry has
2447 * been renounced. Ignore it.
2449 if (snp->smk_label == NULL)
2452 * we break after finding the first match because
2453 * the list is sorted from longest to shortest mask
2454 * so we have found the most specific match
2456 for (found = 1, i = 0; i < 8; i++) {
2457 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2458 snp->smk_host.s6_addr16[i]) {
2464 return snp->smk_label;
2469 #endif /* CONFIG_IPV6 */
2472 * smack_netlabel - Set the secattr on a socket
2474 * @labeled: socket label scheme
2476 * Convert the outbound smack value (smk_out) to a
2477 * secattr and attach it to the socket.
2479 * Returns 0 on success or an error code
2481 static int smack_netlabel(struct sock *sk, int labeled)
2483 struct smack_known *skp;
2484 struct socket_smack *ssp = sk->sk_security;
2488 * Usually the netlabel code will handle changing the
2489 * packet labeling based on the label.
2490 * The case of a single label host is different, because
2491 * a single label host should never get a labeled packet
2492 * even though the label is usually associated with a packet
2496 bh_lock_sock_nested(sk);
2498 if (ssp->smk_out == smack_net_ambient ||
2499 labeled == SMACK_UNLABELED_SOCKET)
2500 netlbl_sock_delattr(sk);
2503 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2513 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2515 * @sap: the destination address
2517 * Set the correct secattr for the given socket based on the destination
2518 * address and perform any outbound access checks needed.
2520 * Returns 0 on success or an error code.
2523 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2525 struct smack_known *skp;
2528 struct smack_known *hkp;
2529 struct socket_smack *ssp = sk->sk_security;
2530 struct smk_audit_info ad;
2533 hkp = smack_ipv4host_label(sap);
2536 struct lsm_network_audit net;
2538 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2539 ad.a.u.net->family = sap->sin_family;
2540 ad.a.u.net->dport = sap->sin_port;
2541 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2543 sk_lbl = SMACK_UNLABELED_SOCKET;
2545 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2546 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2548 sk_lbl = SMACK_CIPSO_SOCKET;
2555 return smack_netlabel(sk, sk_lbl);
2558 #if IS_ENABLED(CONFIG_IPV6)
2560 * smk_ipv6_check - check Smack access
2561 * @subject: subject Smack label
2562 * @object: object Smack label
2564 * @act: the action being taken
2566 * Check an IPv6 access
2568 static int smk_ipv6_check(struct smack_known *subject,
2569 struct smack_known *object,
2570 struct sockaddr_in6 *address, int act)
2573 struct lsm_network_audit net;
2575 struct smk_audit_info ad;
2579 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2580 ad.a.u.net->family = PF_INET6;
2581 ad.a.u.net->dport = ntohs(address->sin6_port);
2582 if (act == SMK_RECEIVING)
2583 ad.a.u.net->v6info.saddr = address->sin6_addr;
2585 ad.a.u.net->v6info.daddr = address->sin6_addr;
2587 rc = smk_access(subject, object, MAY_WRITE, &ad);
2588 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2591 #endif /* CONFIG_IPV6 */
2593 #ifdef SMACK_IPV6_PORT_LABELING
2595 * smk_ipv6_port_label - Smack port access table management
2599 * Create or update the port list entry
2601 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2603 struct sock *sk = sock->sk;
2604 struct sockaddr_in6 *addr6;
2605 struct socket_smack *ssp = sock->sk->sk_security;
2606 struct smk_port_label *spp;
2607 unsigned short port = 0;
2609 if (address == NULL) {
2611 * This operation is changing the Smack information
2612 * on the bound socket. Take the changes to the port
2616 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2617 if (sk != spp->smk_sock)
2619 spp->smk_in = ssp->smk_in;
2620 spp->smk_out = ssp->smk_out;
2625 * A NULL address is only used for updating existing
2626 * bound entries. If there isn't one, it's OK.
2632 addr6 = (struct sockaddr_in6 *)address;
2633 port = ntohs(addr6->sin6_port);
2635 * This is a special case that is safely ignored.
2641 * Look for an existing port list entry.
2642 * This is an indication that a port is getting reused.
2645 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2646 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2648 if (spp->smk_can_reuse != 1) {
2652 spp->smk_port = port;
2654 spp->smk_in = ssp->smk_in;
2655 spp->smk_out = ssp->smk_out;
2656 spp->smk_can_reuse = 0;
2662 * A new port entry is required.
2664 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2668 spp->smk_port = port;
2670 spp->smk_in = ssp->smk_in;
2671 spp->smk_out = ssp->smk_out;
2672 spp->smk_sock_type = sock->type;
2673 spp->smk_can_reuse = 0;
2675 mutex_lock(&smack_ipv6_lock);
2676 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2677 mutex_unlock(&smack_ipv6_lock);
2682 * smk_ipv6_port_check - check Smack port access
2686 * Create or update the port list entry
2688 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2691 struct smk_port_label *spp;
2692 struct socket_smack *ssp = sk->sk_security;
2693 struct smack_known *skp = NULL;
2694 unsigned short port;
2695 struct smack_known *object;
2697 if (act == SMK_RECEIVING) {
2698 skp = smack_ipv6host_label(address);
2699 object = ssp->smk_in;
2702 object = smack_ipv6host_label(address);
2706 * The other end is a single label host.
2708 if (skp != NULL && object != NULL)
2709 return smk_ipv6_check(skp, object, address, act);
2711 skp = smack_net_ambient;
2713 object = smack_net_ambient;
2716 * It's remote, so port lookup does no good.
2718 if (!smk_ipv6_localhost(address))
2719 return smk_ipv6_check(skp, object, address, act);
2722 * It's local so the send check has to have passed.
2724 if (act == SMK_RECEIVING)
2727 port = ntohs(address->sin6_port);
2729 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2730 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2732 object = spp->smk_in;
2733 if (act == SMK_CONNECTING)
2734 ssp->smk_packet = spp->smk_out;
2739 return smk_ipv6_check(skp, object, address, act);
2741 #endif /* SMACK_IPV6_PORT_LABELING */
2744 * smack_inode_setsecurity - set smack xattrs
2745 * @inode: the object
2746 * @name: attribute name
2747 * @value: attribute value
2748 * @size: size of the attribute
2751 * Sets the named attribute in the appropriate blob
2753 * Returns 0 on success, or an error code
2755 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2756 const void *value, size_t size, int flags)
2758 struct smack_known *skp;
2759 struct inode_smack *nsp = inode->i_security;
2760 struct socket_smack *ssp;
2761 struct socket *sock;
2764 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2767 skp = smk_import_entry(value, size);
2769 return PTR_ERR(skp);
2771 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2772 nsp->smk_inode = skp;
2773 nsp->smk_flags |= SMK_INODE_INSTANT;
2777 * The rest of the Smack xattrs are only on sockets.
2779 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2782 sock = SOCKET_I(inode);
2783 if (sock == NULL || sock->sk == NULL)
2786 ssp = sock->sk->sk_security;
2788 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2790 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2792 if (sock->sk->sk_family == PF_INET) {
2793 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2796 "Smack: \"%s\" netlbl error %d.\n",
2802 #ifdef SMACK_IPV6_PORT_LABELING
2803 if (sock->sk->sk_family == PF_INET6)
2804 smk_ipv6_port_label(sock, NULL);
2811 * smack_socket_post_create - finish socket setup
2813 * @family: protocol family
2818 * Sets the netlabel information on the socket
2820 * Returns 0 on success, and error code otherwise
2822 static int smack_socket_post_create(struct socket *sock, int family,
2823 int type, int protocol, int kern)
2825 struct socket_smack *ssp;
2827 if (sock->sk == NULL)
2831 * Sockets created by kernel threads receive web label.
2833 if (unlikely(current->flags & PF_KTHREAD)) {
2834 ssp = sock->sk->sk_security;
2835 ssp->smk_in = &smack_known_web;
2836 ssp->smk_out = &smack_known_web;
2839 if (family != PF_INET)
2842 * Set the outbound netlbl.
2844 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2848 * smack_socket_socketpair - create socket pair
2849 * @socka: one socket
2850 * @sockb: another socket
2852 * Cross reference the peer labels for SO_PEERSEC
2854 * Returns 0 on success, and error code otherwise
2856 static int smack_socket_socketpair(struct socket *socka,
2857 struct socket *sockb)
2859 struct socket_smack *asp = socka->sk->sk_security;
2860 struct socket_smack *bsp = sockb->sk->sk_security;
2862 asp->smk_packet = bsp->smk_out;
2863 bsp->smk_packet = asp->smk_out;
2868 #ifdef SMACK_IPV6_PORT_LABELING
2870 * smack_socket_bind - record port binding information.
2872 * @address: the port address
2873 * @addrlen: size of the address
2875 * Records the label bound to a port.
2879 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2882 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2883 smk_ipv6_port_label(sock, address);
2886 #endif /* SMACK_IPV6_PORT_LABELING */
2889 * smack_socket_connect - connect access check
2891 * @sap: the other end
2892 * @addrlen: size of sap
2894 * Verifies that a connection may be possible
2896 * Returns 0 on success, and error code otherwise
2898 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2902 #if IS_ENABLED(CONFIG_IPV6)
2903 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2905 #ifdef SMACK_IPV6_SECMARK_LABELING
2906 struct smack_known *rsp;
2907 struct socket_smack *ssp;
2910 if (sock->sk == NULL)
2913 #ifdef SMACK_IPV6_SECMARK_LABELING
2914 ssp = sock->sk->sk_security;
2917 switch (sock->sk->sk_family) {
2919 if (addrlen < sizeof(struct sockaddr_in))
2921 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2924 if (addrlen < sizeof(struct sockaddr_in6))
2926 #ifdef SMACK_IPV6_SECMARK_LABELING
2927 rsp = smack_ipv6host_label(sip);
2929 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2932 #ifdef SMACK_IPV6_PORT_LABELING
2933 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2941 * smack_flags_to_may - convert S_ to MAY_ values
2942 * @flags: the S_ value
2944 * Returns the equivalent MAY_ value
2946 static int smack_flags_to_may(int flags)
2950 if (flags & S_IRUGO)
2952 if (flags & S_IWUGO)
2954 if (flags & S_IXUGO)
2961 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2966 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2968 struct smack_known *skp = smk_of_current();
2970 msg->security = skp;
2975 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2978 * Clears the blob pointer
2980 static void smack_msg_msg_free_security(struct msg_msg *msg)
2982 msg->security = NULL;
2986 * smack_of_ipc - the smack pointer for the ipc
2989 * Returns a pointer to the smack value
2991 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2993 return (struct smack_known *)isp->security;
2997 * smack_ipc_alloc_security - Set the security blob for ipc
3002 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
3004 struct smack_known *skp = smk_of_current();
3006 isp->security = skp;
3011 * smack_ipc_free_security - Clear the security blob for ipc
3014 * Clears the blob pointer
3016 static void smack_ipc_free_security(struct kern_ipc_perm *isp)
3018 isp->security = NULL;
3022 * smk_curacc_shm : check if current has access on shm
3024 * @access : access requested
3026 * Returns 0 if current has the requested access, error code otherwise
3028 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
3030 struct smack_known *ssp = smack_of_ipc(isp);
3031 struct smk_audit_info ad;
3035 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3036 ad.a.u.ipc_id = isp->id;
3038 rc = smk_curacc(ssp, access, &ad);
3039 rc = smk_bu_current("shm", ssp, access, rc);
3044 * smack_shm_associate - Smack access check for shm
3046 * @shmflg: access requested
3048 * Returns 0 if current has the requested access, error code otherwise
3050 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
3054 may = smack_flags_to_may(shmflg);
3055 return smk_curacc_shm(isp, may);
3059 * smack_shm_shmctl - Smack access check for shm
3061 * @cmd: what it wants to do
3063 * Returns 0 if current has the requested access, error code otherwise
3065 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
3079 may = MAY_READWRITE;
3084 * System level information.
3090 return smk_curacc_shm(isp, may);
3094 * smack_shm_shmat - Smack access for shmat
3097 * @shmflg: access requested
3099 * Returns 0 if current has the requested access, error code otherwise
3101 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3106 may = smack_flags_to_may(shmflg);
3107 return smk_curacc_shm(ipc, may);
3111 * smk_curacc_sem : check if current has access on sem
3113 * @access : access requested
3115 * Returns 0 if current has the requested access, error code otherwise
3117 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3119 struct smack_known *ssp = smack_of_ipc(isp);
3120 struct smk_audit_info ad;
3124 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3125 ad.a.u.ipc_id = isp->id;
3127 rc = smk_curacc(ssp, access, &ad);
3128 rc = smk_bu_current("sem", ssp, access, rc);
3133 * smack_sem_associate - Smack access check for sem
3135 * @semflg: access requested
3137 * Returns 0 if current has the requested access, error code otherwise
3139 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3143 may = smack_flags_to_may(semflg);
3144 return smk_curacc_sem(isp, may);
3148 * smack_sem_shmctl - Smack access check for sem
3150 * @cmd: what it wants to do
3152 * Returns 0 if current has the requested access, error code otherwise
3154 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3173 may = MAY_READWRITE;
3178 * System level information
3185 return smk_curacc_sem(isp, may);
3189 * smack_sem_semop - Smack checks of semaphore operations
3195 * Treated as read and write in all cases.
3197 * Returns 0 if access is allowed, error code otherwise
3199 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3200 unsigned nsops, int alter)
3202 return smk_curacc_sem(isp, MAY_READWRITE);
3206 * smk_curacc_msq : helper to check if current has access on msq
3208 * @access : access requested
3210 * return 0 if current has access, error otherwise
3212 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3214 struct smack_known *msp = smack_of_ipc(isp);
3215 struct smk_audit_info ad;
3219 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3220 ad.a.u.ipc_id = isp->id;
3222 rc = smk_curacc(msp, access, &ad);
3223 rc = smk_bu_current("msq", msp, access, rc);
3228 * smack_msg_queue_associate - Smack access check for msg_queue
3230 * @msqflg: access requested
3232 * Returns 0 if current has the requested access, error code otherwise
3234 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3238 may = smack_flags_to_may(msqflg);
3239 return smk_curacc_msq(isp, may);
3243 * smack_msg_queue_msgctl - Smack access check for msg_queue
3245 * @cmd: what it wants to do
3247 * Returns 0 if current has the requested access, error code otherwise
3249 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3261 may = MAY_READWRITE;
3266 * System level information
3273 return smk_curacc_msq(isp, may);
3277 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3280 * @msqflg: access requested
3282 * Returns 0 if current has the requested access, error code otherwise
3284 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3289 may = smack_flags_to_may(msqflg);
3290 return smk_curacc_msq(isp, may);
3294 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3301 * Returns 0 if current has read and write access, error code otherwise
3303 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3304 struct task_struct *target, long type, int mode)
3306 return smk_curacc_msq(isp, MAY_READWRITE);
3310 * smack_ipc_permission - Smack access for ipc_permission()
3311 * @ipp: the object permissions
3312 * @flag: access requested
3314 * Returns 0 if current has read and write access, error code otherwise
3316 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3318 struct smack_known *iskp = ipp->security;
3319 int may = smack_flags_to_may(flag);
3320 struct smk_audit_info ad;
3324 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3325 ad.a.u.ipc_id = ipp->id;
3327 rc = smk_curacc(iskp, may, &ad);
3328 rc = smk_bu_current("svipc", iskp, may, rc);
3333 * smack_ipc_getsecid - Extract smack security id
3334 * @ipp: the object permissions
3335 * @secid: where result will be saved
3337 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3339 struct smack_known *iskp = ipp->security;
3341 *secid = iskp->smk_secid;
3345 * smack_d_instantiate - Make sure the blob is correct on an inode
3346 * @opt_dentry: dentry where inode will be attached
3347 * @inode: the object
3349 * Set the inode's security blob if it hasn't been done already.
3351 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3353 struct super_block *sbp;
3354 struct superblock_smack *sbsp;
3355 struct inode_smack *isp;
3356 struct smack_known *skp;
3357 struct smack_known *ckp = smk_of_current();
3358 struct smack_known *final;
3359 char trattr[TRANS_TRUE_SIZE];
3367 isp = inode->i_security;
3369 mutex_lock(&isp->smk_lock);
3371 * If the inode is already instantiated
3372 * take the quick way out
3374 if (isp->smk_flags & SMK_INODE_INSTANT)
3378 sbsp = sbp->s_security;
3380 * We're going to use the superblock default label
3381 * if there's no label on the file.
3383 final = sbsp->smk_default;
3386 * If this is the root inode the superblock
3387 * may be in the process of initialization.
3388 * If that is the case use the root value out
3389 * of the superblock.
3391 if (opt_dentry->d_parent == opt_dentry) {
3392 switch (sbp->s_magic) {
3393 case CGROUP_SUPER_MAGIC:
3394 case CGROUP2_SUPER_MAGIC:
3396 * The cgroup filesystem is never mounted,
3397 * so there's no opportunity to set the mount
3400 sbsp->smk_root = &smack_known_star;
3401 sbsp->smk_default = &smack_known_star;
3402 isp->smk_inode = sbsp->smk_root;
3406 * What about shmem/tmpfs anonymous files with dentry
3407 * obtained from d_alloc_pseudo()?
3409 isp->smk_inode = smk_of_current();
3412 isp->smk_inode = smk_of_current();
3416 * Socket access is controlled by the socket
3417 * structures associated with the task involved.
3419 isp->smk_inode = &smack_known_star;
3422 isp->smk_inode = sbsp->smk_root;
3425 isp->smk_flags |= SMK_INODE_INSTANT;
3430 * This is pretty hackish.
3431 * Casey says that we shouldn't have to do
3432 * file system specific code, but it does help
3433 * with keeping it simple.
3435 switch (sbp->s_magic) {
3437 case CGROUP_SUPER_MAGIC:
3438 case CGROUP2_SUPER_MAGIC:
3440 * Casey says that it's a little embarrassing
3441 * that the smack file system doesn't do
3442 * extended attributes.
3444 * Cgroupfs is special
3446 final = &smack_known_star;
3448 case DEVPTS_SUPER_MAGIC:
3450 * devpts seems content with the label of the task.
3451 * Programs that change smack have to treat the
3456 case PROC_SUPER_MAGIC:
3458 * Casey says procfs appears not to care.
3459 * The superblock default suffices.
3464 * Device labels should come from the filesystem,
3465 * but watch out, because they're volitile,
3466 * getting recreated on every reboot.
3468 final = &smack_known_star;
3472 * If a smack value has been set we want to use it,
3473 * but since tmpfs isn't giving us the opportunity
3474 * to set mount options simulate setting the
3475 * superblock default.
3479 * This isn't an understood special case.
3480 * Get the value from the xattr.
3484 * UNIX domain sockets use lower level socket data.
3486 if (S_ISSOCK(inode->i_mode)) {
3487 final = &smack_known_star;
3491 * No xattr support means, alas, no SMACK label.
3492 * Use the aforeapplied default.
3493 * It would be curious if the label of the task
3494 * does not match that assigned.
3496 if (!(inode->i_opflags & IOP_XATTR))
3499 * Get the dentry for xattr.
3501 dp = dget(opt_dentry);
3502 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3503 if (!IS_ERR_OR_NULL(skp))
3507 * Transmuting directory
3509 if (S_ISDIR(inode->i_mode)) {
3511 * If this is a new directory and the label was
3512 * transmuted when the inode was initialized
3513 * set the transmute attribute on the directory
3514 * and mark the inode.
3516 * If there is a transmute attribute on the
3517 * directory mark the inode.
3519 if (isp->smk_flags & SMK_INODE_CHANGED) {
3520 isp->smk_flags &= ~SMK_INODE_CHANGED;
3521 rc = __vfs_setxattr(dp, inode,
3522 XATTR_NAME_SMACKTRANSMUTE,
3523 TRANS_TRUE, TRANS_TRUE_SIZE,
3526 rc = __vfs_getxattr(dp, inode,
3527 XATTR_NAME_SMACKTRANSMUTE, trattr,
3529 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3530 TRANS_TRUE_SIZE) != 0)
3534 transflag = SMK_INODE_TRANSMUTE;
3537 * Don't let the exec or mmap label be "*" or "@".
3539 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3540 if (IS_ERR(skp) || skp == &smack_known_star ||
3541 skp == &smack_known_web)
3543 isp->smk_task = skp;
3545 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3546 if (IS_ERR(skp) || skp == &smack_known_star ||
3547 skp == &smack_known_web)
3549 isp->smk_mmap = skp;
3556 isp->smk_inode = ckp;
3558 isp->smk_inode = final;
3560 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3563 mutex_unlock(&isp->smk_lock);
3568 * smack_getprocattr - Smack process attribute access
3569 * @p: the object task
3570 * @name: the name of the attribute in /proc/.../attr
3571 * @value: where to put the result
3573 * Places a copy of the task Smack into value
3575 * Returns the length of the smack label or an error code
3577 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3579 struct smack_known *skp = smk_of_task_struct(p);
3583 if (strcmp(name, "current") != 0)
3586 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3596 * smack_setprocattr - Smack process attribute setting
3597 * @name: the name of the attribute in /proc/.../attr
3598 * @value: the value to set
3599 * @size: the size of the value
3601 * Sets the Smack value of the task. Only setting self
3602 * is permitted and only with privilege
3604 * Returns the length of the smack label or an error code
3606 static int smack_setprocattr(const char *name, void *value, size_t size)
3608 struct task_smack *tsp = current_security();
3610 struct smack_known *skp;
3611 struct smack_known_list_elem *sklep;
3614 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3617 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3620 if (strcmp(name, "current") != 0)
3623 skp = smk_import_entry(value, size);
3625 return PTR_ERR(skp);
3628 * No process is ever allowed the web ("@") label
3629 * and the star ("*") label.
3631 if (skp == &smack_known_web || skp == &smack_known_star)
3634 if (!smack_privileged(CAP_MAC_ADMIN)) {
3636 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3637 if (sklep->smk_label == skp) {
3645 new = prepare_creds();
3649 tsp = new->security;
3650 tsp->smk_task = skp;
3652 * process can change its label only once
3654 smk_destroy_label_list(&tsp->smk_relabel);
3661 * smack_unix_stream_connect - Smack access on UDS
3663 * @other: the other sock
3666 * Return 0 if a subject with the smack of sock could access
3667 * an object with the smack of other, otherwise an error code
3669 static int smack_unix_stream_connect(struct sock *sock,
3670 struct sock *other, struct sock *newsk)
3672 struct smack_known *skp;
3673 struct smack_known *okp;
3674 struct socket_smack *ssp = sock->sk_security;
3675 struct socket_smack *osp = other->sk_security;
3676 struct socket_smack *nsp = newsk->sk_security;
3677 struct smk_audit_info ad;
3680 struct lsm_network_audit net;
3683 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3687 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3688 smk_ad_setfield_u_net_sk(&ad, other);
3690 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3691 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3695 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3696 rc = smk_bu_note("UDS connect", okp, skp,
3702 * Cross reference the peer labels for SO_PEERSEC.
3705 nsp->smk_packet = ssp->smk_out;
3706 ssp->smk_packet = osp->smk_out;
3713 * smack_unix_may_send - Smack access on UDS
3715 * @other: the other socket
3717 * Return 0 if a subject with the smack of sock could access
3718 * an object with the smack of other, otherwise an error code
3720 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3722 struct socket_smack *ssp = sock->sk->sk_security;
3723 struct socket_smack *osp = other->sk->sk_security;
3724 struct smk_audit_info ad;
3728 struct lsm_network_audit net;
3730 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3731 smk_ad_setfield_u_net_sk(&ad, other->sk);
3734 if (smack_privileged(CAP_MAC_OVERRIDE))
3737 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3738 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3743 * smack_socket_sendmsg - Smack check based on destination host
3746 * @size: the size of the message
3748 * Return 0 if the current subject can write to the destination host.
3749 * For IPv4 this is only a question if the destination is a single label host.
3750 * For IPv6 this is a check against the label of the port.
3752 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3755 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3756 #if IS_ENABLED(CONFIG_IPV6)
3757 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3759 #ifdef SMACK_IPV6_SECMARK_LABELING
3760 struct socket_smack *ssp = sock->sk->sk_security;
3761 struct smack_known *rsp;
3766 * Perfectly reasonable for this to be NULL
3771 switch (sock->sk->sk_family) {
3773 rc = smack_netlabel_send(sock->sk, sip);
3776 #ifdef SMACK_IPV6_SECMARK_LABELING
3777 rsp = smack_ipv6host_label(sap);
3779 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3782 #ifdef SMACK_IPV6_PORT_LABELING
3783 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3791 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3792 * @sap: netlabel secattr
3793 * @ssp: socket security information
3795 * Returns a pointer to a Smack label entry found on the label list.
3797 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3798 struct socket_smack *ssp)
3800 struct smack_known *skp;
3805 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3807 * Looks like a CIPSO packet.
3808 * If there are flags but no level netlabel isn't
3809 * behaving the way we expect it to.
3811 * Look it up in the label table
3812 * Without guidance regarding the smack value
3813 * for the packet fall back on the network
3817 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3818 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3821 * Compare the catsets. Use the netlbl APIs.
3823 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3824 if ((skp->smk_netlabel.flags &
3825 NETLBL_SECATTR_MLS_CAT) == 0)
3829 for (acat = -1, kcat = -1; acat == kcat; ) {
3830 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3832 kcat = netlbl_catmap_walk(
3833 skp->smk_netlabel.attr.mls.cat,
3835 if (acat < 0 || kcat < 0)
3848 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3849 return &smack_known_web;
3850 return &smack_known_star;
3852 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3854 * Looks like a fallback, which gives us a secid.
3856 return smack_from_secid(sap->attr.secid);
3858 * Without guidance regarding the smack value
3859 * for the packet fall back on the network
3862 return smack_net_ambient;
3865 #if IS_ENABLED(CONFIG_IPV6)
3866 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3870 int proto = -EINVAL;
3871 struct ipv6hdr _ipv6h;
3872 struct ipv6hdr *ip6;
3874 struct tcphdr _tcph, *th;
3875 struct udphdr _udph, *uh;
3876 struct dccp_hdr _dccph, *dh;
3880 offset = skb_network_offset(skb);
3881 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3884 sip->sin6_addr = ip6->saddr;
3886 nexthdr = ip6->nexthdr;
3887 offset += sizeof(_ipv6h);
3888 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3895 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3897 sip->sin6_port = th->source;
3900 case IPPROTO_UDPLITE:
3901 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3903 sip->sin6_port = uh->source;
3906 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3908 sip->sin6_port = dh->dccph_sport;
3913 #endif /* CONFIG_IPV6 */
3916 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3920 * Returns 0 if the packet should be delivered, an error code otherwise
3922 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3924 struct netlbl_lsm_secattr secattr;
3925 struct socket_smack *ssp = sk->sk_security;
3926 struct smack_known *skp = NULL;
3928 struct smk_audit_info ad;
3929 u16 family = sk->sk_family;
3931 struct lsm_network_audit net;
3933 #if IS_ENABLED(CONFIG_IPV6)
3934 struct sockaddr_in6 sadd;
3937 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3939 #endif /* CONFIG_IPV6 */
3943 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3945 * If there is a secmark use it rather than the CIPSO label.
3946 * If there is no secmark fall back to CIPSO.
3947 * The secmark is assumed to reflect policy better.
3949 if (skb && skb->secmark != 0) {
3950 skp = smack_from_secid(skb->secmark);
3953 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3955 * Translate what netlabel gave us.
3957 netlbl_secattr_init(&secattr);
3959 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3961 skp = smack_from_secattr(&secattr, ssp);
3963 skp = smack_net_ambient;
3965 netlbl_secattr_destroy(&secattr);
3967 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3971 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3972 ad.a.u.net->family = family;
3973 ad.a.u.net->netif = skb->skb_iif;
3974 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3977 * Receiving a packet requires that the other end
3978 * be able to write here. Read access is not required.
3979 * This is the simplist possible security model
3982 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3983 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3986 netlbl_skbuff_err(skb, family, rc, 0);
3988 #if IS_ENABLED(CONFIG_IPV6)
3990 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3991 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3992 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3994 #ifdef SMACK_IPV6_SECMARK_LABELING
3995 if (skb && skb->secmark != 0)
3996 skp = smack_from_secid(skb->secmark);
3998 skp = smack_ipv6host_label(&sadd);
4000 skp = smack_net_ambient;
4002 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4003 ad.a.u.net->family = family;
4004 ad.a.u.net->netif = skb->skb_iif;
4005 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4006 #endif /* CONFIG_AUDIT */
4007 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4008 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4010 #endif /* SMACK_IPV6_SECMARK_LABELING */
4011 #ifdef SMACK_IPV6_PORT_LABELING
4012 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
4013 #endif /* SMACK_IPV6_PORT_LABELING */
4015 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4016 ICMPV6_ADM_PROHIBITED, 0);
4018 #endif /* CONFIG_IPV6 */
4025 * smack_socket_getpeersec_stream - pull in packet label
4027 * @optval: user's destination
4028 * @optlen: size thereof
4031 * returns zero on success, an error code otherwise
4033 static int smack_socket_getpeersec_stream(struct socket *sock,
4034 char __user *optval,
4035 int __user *optlen, unsigned len)
4037 struct socket_smack *ssp;
4042 ssp = sock->sk->sk_security;
4043 if (ssp->smk_packet != NULL) {
4044 rcp = ssp->smk_packet->smk_known;
4045 slen = strlen(rcp) + 1;
4050 else if (copy_to_user(optval, rcp, slen) != 0)
4053 if (put_user(slen, optlen) != 0)
4061 * smack_socket_getpeersec_dgram - pull in packet label
4062 * @sock: the peer socket
4064 * @secid: pointer to where to put the secid of the packet
4066 * Sets the netlabel socket state on sk from parent
4068 static int smack_socket_getpeersec_dgram(struct socket *sock,
4069 struct sk_buff *skb, u32 *secid)
4072 struct netlbl_lsm_secattr secattr;
4073 struct socket_smack *ssp = NULL;
4074 struct smack_known *skp;
4075 int family = PF_UNSPEC;
4076 u32 s = 0; /* 0 is the invalid secid */
4080 if (skb->protocol == htons(ETH_P_IP))
4082 #if IS_ENABLED(CONFIG_IPV6)
4083 else if (skb->protocol == htons(ETH_P_IPV6))
4085 #endif /* CONFIG_IPV6 */
4087 if (family == PF_UNSPEC && sock != NULL)
4088 family = sock->sk->sk_family;
4092 ssp = sock->sk->sk_security;
4093 s = ssp->smk_out->smk_secid;
4096 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4102 * Translate what netlabel gave us.
4104 if (sock != NULL && sock->sk != NULL)
4105 ssp = sock->sk->sk_security;
4106 netlbl_secattr_init(&secattr);
4107 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4109 skp = smack_from_secattr(&secattr, ssp);
4112 netlbl_secattr_destroy(&secattr);
4115 #ifdef SMACK_IPV6_SECMARK_LABELING
4127 * smack_sock_graft - Initialize a newly created socket with an existing sock
4129 * @parent: parent socket
4131 * Set the smk_{in,out} state of an existing sock based on the process that
4132 * is creating the new socket.
4134 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4136 struct socket_smack *ssp;
4137 struct smack_known *skp = smk_of_current();
4140 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4143 ssp = sk->sk_security;
4146 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4150 * smack_inet_conn_request - Smack access check on connect
4151 * @sk: socket involved
4155 * Returns 0 if a task with the packet label could write to
4156 * the socket, otherwise an error code
4158 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4159 struct request_sock *req)
4161 u16 family = sk->sk_family;
4162 struct smack_known *skp;
4163 struct socket_smack *ssp = sk->sk_security;
4164 struct netlbl_lsm_secattr secattr;
4165 struct sockaddr_in addr;
4167 struct smack_known *hskp;
4169 struct smk_audit_info ad;
4171 struct lsm_network_audit net;
4174 #if IS_ENABLED(CONFIG_IPV6)
4175 if (family == PF_INET6) {
4177 * Handle mapped IPv4 packets arriving
4178 * via IPv6 sockets. Don't set up netlabel
4179 * processing on IPv6.
4181 if (skb->protocol == htons(ETH_P_IP))
4186 #endif /* CONFIG_IPV6 */
4188 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4190 * If there is a secmark use it rather than the CIPSO label.
4191 * If there is no secmark fall back to CIPSO.
4192 * The secmark is assumed to reflect policy better.
4194 if (skb && skb->secmark != 0) {
4195 skp = smack_from_secid(skb->secmark);
4198 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4200 netlbl_secattr_init(&secattr);
4201 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4203 skp = smack_from_secattr(&secattr, ssp);
4205 skp = &smack_known_huh;
4206 netlbl_secattr_destroy(&secattr);
4208 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4213 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4214 ad.a.u.net->family = family;
4215 ad.a.u.net->netif = skb->skb_iif;
4216 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4219 * Receiving a packet requires that the other end be able to write
4220 * here. Read access is not required.
4222 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4223 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4228 * Save the peer's label in the request_sock so we can later setup
4229 * smk_packet in the child socket so that SO_PEERCRED can report it.
4231 req->peer_secid = skp->smk_secid;
4234 * We need to decide if we want to label the incoming connection here
4235 * if we do we only need to label the request_sock and the stack will
4236 * propagate the wire-label to the sock when it is created.
4239 addr.sin_addr.s_addr = hdr->saddr;
4241 hskp = smack_ipv4host_label(&addr);
4245 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4247 netlbl_req_delattr(req);
4253 * smack_inet_csk_clone - Copy the connection information to the new socket
4254 * @sk: the new socket
4255 * @req: the connection's request_sock
4257 * Transfer the connection's peer label to the newly created socket.
4259 static void smack_inet_csk_clone(struct sock *sk,
4260 const struct request_sock *req)
4262 struct socket_smack *ssp = sk->sk_security;
4263 struct smack_known *skp;
4265 if (req->peer_secid != 0) {
4266 skp = smack_from_secid(req->peer_secid);
4267 ssp->smk_packet = skp;
4269 ssp->smk_packet = NULL;
4273 * Key management security hooks
4275 * Casey has not tested key support very heavily.
4276 * The permission check is most likely too restrictive.
4277 * If you care about keys please have a look.
4282 * smack_key_alloc - Set the key security blob
4284 * @cred: the credentials to use
4287 * No allocation required
4291 static int smack_key_alloc(struct key *key, const struct cred *cred,
4292 unsigned long flags)
4294 struct smack_known *skp = smk_of_task(cred->security);
4296 key->security = skp;
4301 * smack_key_free - Clear the key security blob
4304 * Clear the blob pointer
4306 static void smack_key_free(struct key *key)
4308 key->security = NULL;
4312 * smack_key_permission - Smack access on a key
4313 * @key_ref: gets to the object
4314 * @cred: the credentials to use
4315 * @perm: requested key permissions
4317 * Return 0 if the task has read and write to the object,
4318 * an error code otherwise
4320 static int smack_key_permission(key_ref_t key_ref,
4321 const struct cred *cred, unsigned perm)
4324 struct smk_audit_info ad;
4325 struct smack_known *tkp = smk_of_task(cred->security);
4329 keyp = key_ref_to_ptr(key_ref);
4333 * If the key hasn't been initialized give it access so that
4336 if (keyp->security == NULL)
4339 * This should not occur
4344 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4348 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4349 ad.a.u.key_struct.key = keyp->serial;
4350 ad.a.u.key_struct.key_desc = keyp->description;
4352 if (perm & KEY_NEED_READ)
4354 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4355 request = MAY_WRITE;
4356 rc = smk_access(tkp, keyp->security, request, &ad);
4357 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4362 * smack_key_getsecurity - Smack label tagging the key
4363 * @key points to the key to be queried
4364 * @_buffer points to a pointer that should be set to point to the
4365 * resulting string (if no label or an error occurs).
4366 * Return the length of the string (including terminating NUL) or -ve if
4368 * May also return 0 (and a NULL buffer pointer) if there is no label.
4370 static int smack_key_getsecurity(struct key *key, char **_buffer)
4372 struct smack_known *skp = key->security;
4376 if (key->security == NULL) {
4381 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4384 length = strlen(copy) + 1;
4390 #endif /* CONFIG_KEYS */
4395 * Audit requires a unique representation of each Smack specific
4396 * rule. This unique representation is used to distinguish the
4397 * object to be audited from remaining kernel objects and also
4398 * works as a glue between the audit hooks.
4400 * Since repository entries are added but never deleted, we'll use
4401 * the smack_known label address related to the given audit rule as
4402 * the needed unique representation. This also better fits the smack
4403 * model where nearly everything is a label.
4408 * smack_audit_rule_init - Initialize a smack audit rule
4409 * @field: audit rule fields given from user-space (audit.h)
4410 * @op: required testing operator (=, !=, >, <, ...)
4411 * @rulestr: smack label to be audited
4412 * @vrule: pointer to save our own audit rule representation
4414 * Prepare to audit cases where (@field @op @rulestr) is true.
4415 * The label to be audited is created if necessay.
4417 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4419 struct smack_known *skp;
4420 char **rule = (char **)vrule;
4423 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4426 if (op != Audit_equal && op != Audit_not_equal)
4429 skp = smk_import_entry(rulestr, 0);
4431 return PTR_ERR(skp);
4433 *rule = skp->smk_known;
4439 * smack_audit_rule_known - Distinguish Smack audit rules
4440 * @krule: rule of interest, in Audit kernel representation format
4442 * This is used to filter Smack rules from remaining Audit ones.
4443 * If it's proved that this rule belongs to us, the
4444 * audit_rule_match hook will be called to do the final judgement.
4446 static int smack_audit_rule_known(struct audit_krule *krule)
4448 struct audit_field *f;
4451 for (i = 0; i < krule->field_count; i++) {
4452 f = &krule->fields[i];
4454 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4462 * smack_audit_rule_match - Audit given object ?
4463 * @secid: security id for identifying the object to test
4464 * @field: audit rule flags given from user-space
4465 * @op: required testing operator
4466 * @vrule: smack internal rule presentation
4467 * @actx: audit context associated with the check
4469 * The core Audit hook. It's used to take the decision of
4470 * whether to audit or not to audit a given object.
4472 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4473 struct audit_context *actx)
4475 struct smack_known *skp;
4478 if (unlikely(!rule)) {
4479 WARN_ONCE(1, "Smack: missing rule\n");
4483 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4486 skp = smack_from_secid(secid);
4489 * No need to do string comparisons. If a match occurs,
4490 * both pointers will point to the same smack_known
4493 if (op == Audit_equal)
4494 return (rule == skp->smk_known);
4495 if (op == Audit_not_equal)
4496 return (rule != skp->smk_known);
4502 * There is no need for a smack_audit_rule_free hook.
4503 * No memory was allocated.
4506 #endif /* CONFIG_AUDIT */
4509 * smack_ismaclabel - check if xattr @name references a smack MAC label
4510 * @name: Full xattr name to check.
4512 static int smack_ismaclabel(const char *name)
4514 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4519 * smack_secid_to_secctx - return the smack label for a secid
4520 * @secid: incoming integer
4521 * @secdata: destination
4522 * @seclen: how long it is
4524 * Exists for networking code.
4526 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4528 struct smack_known *skp = smack_from_secid(secid);
4531 *secdata = skp->smk_known;
4532 *seclen = strlen(skp->smk_known);
4537 * smack_secctx_to_secid - return the secid for a smack label
4538 * @secdata: smack label
4539 * @seclen: how long result is
4540 * @secid: outgoing integer
4542 * Exists for audit and networking code.
4544 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4546 struct smack_known *skp = smk_find_entry(secdata);
4549 *secid = skp->smk_secid;
4556 * There used to be a smack_release_secctx hook
4557 * that did nothing back when hooks were in a vector.
4558 * Now that there's a list such a hook adds cost.
4561 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4563 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4566 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4568 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4571 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4573 struct smack_known *skp = smk_of_inode(inode);
4575 *ctx = skp->smk_known;
4576 *ctxlen = strlen(skp->smk_known);
4580 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4583 struct task_smack *tsp;
4584 struct smack_known *skp;
4585 struct inode_smack *isp;
4586 struct cred *new_creds = *new;
4588 if (new_creds == NULL) {
4589 new_creds = prepare_creds();
4590 if (new_creds == NULL)
4594 tsp = new_creds->security;
4597 * Get label from overlay inode and set it in create_sid
4599 isp = d_inode(dentry->d_parent)->i_security;
4600 skp = isp->smk_inode;
4601 tsp->smk_task = skp;
4606 static int smack_inode_copy_up_xattr(const char *name)
4609 * Return 1 if this is the smack access Smack attribute.
4611 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4617 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4619 const struct cred *old,
4622 struct task_smack *otsp = old->security;
4623 struct task_smack *ntsp = new->security;
4624 struct inode_smack *isp;
4628 * Use the process credential unless all of
4629 * the transmuting criteria are met
4631 ntsp->smk_task = otsp->smk_task;
4634 * the attribute of the containing directory
4636 isp = d_inode(dentry->d_parent)->i_security;
4638 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4640 may = smk_access_entry(otsp->smk_task->smk_known,
4641 isp->smk_inode->smk_known,
4642 &otsp->smk_task->smk_rules);
4646 * If the directory is transmuting and the rule
4647 * providing access is transmuting use the containing
4648 * directory label instead of the process label.
4650 if (may > 0 && (may & MAY_TRANSMUTE))
4651 ntsp->smk_task = isp->smk_inode;
4656 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4657 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4658 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4659 LSM_HOOK_INIT(syslog, smack_syslog),
4661 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4662 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4663 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4664 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4665 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4666 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4667 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
4669 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4671 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4672 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4673 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4674 LSM_HOOK_INIT(inode_link, smack_inode_link),
4675 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4676 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4677 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4678 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4679 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4680 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4681 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4682 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4683 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4684 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4685 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4686 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4687 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4688 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4690 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4691 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4692 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4693 LSM_HOOK_INIT(file_lock, smack_file_lock),
4694 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4695 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4696 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4697 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4698 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4699 LSM_HOOK_INIT(file_receive, smack_file_receive),
4701 LSM_HOOK_INIT(file_open, smack_file_open),
4703 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4704 LSM_HOOK_INIT(cred_free, smack_cred_free),
4705 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4706 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4707 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4708 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4709 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4710 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4711 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4712 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4713 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4714 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4715 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4716 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4717 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4718 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4719 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4720 LSM_HOOK_INIT(task_kill, smack_task_kill),
4721 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4723 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4724 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4726 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4727 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4729 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4730 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
4731 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4732 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4733 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4734 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4736 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4737 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
4738 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4739 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4740 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4742 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4743 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
4744 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4745 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4746 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4748 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4750 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4751 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4753 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4754 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4756 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4757 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4758 #ifdef SMACK_IPV6_PORT_LABELING
4759 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4761 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4762 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4763 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4764 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4765 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4766 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4767 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4768 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4769 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4770 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4772 /* key management security hooks */
4774 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4775 LSM_HOOK_INIT(key_free, smack_key_free),
4776 LSM_HOOK_INIT(key_permission, smack_key_permission),
4777 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4778 #endif /* CONFIG_KEYS */
4782 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4783 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4784 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4785 #endif /* CONFIG_AUDIT */
4787 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4788 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4789 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4790 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4791 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4792 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4793 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4794 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4795 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4799 static __init void init_smack_known_list(void)
4802 * Initialize rule list locks
4804 mutex_init(&smack_known_huh.smk_rules_lock);
4805 mutex_init(&smack_known_hat.smk_rules_lock);
4806 mutex_init(&smack_known_floor.smk_rules_lock);
4807 mutex_init(&smack_known_star.smk_rules_lock);
4808 mutex_init(&smack_known_web.smk_rules_lock);
4810 * Initialize rule lists
4812 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4813 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4814 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4815 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4816 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4818 * Create the known labels list
4820 smk_insert_entry(&smack_known_huh);
4821 smk_insert_entry(&smack_known_hat);
4822 smk_insert_entry(&smack_known_star);
4823 smk_insert_entry(&smack_known_floor);
4824 smk_insert_entry(&smack_known_web);
4828 * smack_init - initialize the smack system
4832 static __init int smack_init(void)
4835 struct task_smack *tsp;
4837 if (!security_module_enable("smack"))
4840 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4841 if (!smack_inode_cache)
4844 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4847 kmem_cache_destroy(smack_inode_cache);
4853 pr_info("Smack: Initializing.\n");
4854 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4855 pr_info("Smack: Netfilter enabled.\n");
4857 #ifdef SMACK_IPV6_PORT_LABELING
4858 pr_info("Smack: IPv6 port labeling enabled.\n");
4860 #ifdef SMACK_IPV6_SECMARK_LABELING
4861 pr_info("Smack: IPv6 Netfilter enabled.\n");
4865 * Set the security state for the initial task.
4867 cred = (struct cred *) current->cred;
4868 cred->security = tsp;
4870 /* initialize the smack_known_list */
4871 init_smack_known_list();
4876 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4882 * Smack requires early initialization in order to label
4883 * all processes and objects when they are created.
4885 security_initcall(smack_init);