2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/dccp.h>
31 #include <linux/slab.h>
32 #include <linux/mutex.h>
33 #include <linux/pipe_fs_i.h>
34 #include <net/cipso_ipv4.h>
37 #include <linux/audit.h>
38 #include <linux/magic.h>
39 #include <linux/dcache.h>
40 #include <linux/personality.h>
41 #include <linux/msg.h>
42 #include <linux/shm.h>
43 #include <linux/binfmts.h>
44 #include <linux/parser.h>
47 #define TRANS_TRUE "TRUE"
48 #define TRANS_TRUE_SIZE 4
50 #define SMK_CONNECTING 0
51 #define SMK_RECEIVING 1
54 #ifdef SMACK_IPV6_PORT_LABELING
55 LIST_HEAD(smk_ipv6_port_list);
57 static struct kmem_cache *smack_inode_cache;
60 static const match_table_t smk_mount_tokens = {
61 {Opt_fsdefault, SMK_FSDEFAULT "%s"},
62 {Opt_fsfloor, SMK_FSFLOOR "%s"},
63 {Opt_fshat, SMK_FSHAT "%s"},
64 {Opt_fsroot, SMK_FSROOT "%s"},
65 {Opt_fstransmute, SMK_FSTRANS "%s"},
69 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
70 static char *smk_bu_mess[] = {
71 "Bringup Error", /* Unused */
72 "Bringup", /* SMACK_BRINGUP_ALLOW */
73 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
74 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
77 static void smk_bu_mode(int mode, char *s)
87 if (mode & MAY_APPEND)
89 if (mode & MAY_TRANSMUTE)
99 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
100 static int smk_bu_note(char *note, struct smack_known *sskp,
101 struct smack_known *oskp, int mode, int rc)
103 char acc[SMK_NUM_ACCESS_TYPE + 1];
107 if (rc > SMACK_UNCONFINED_OBJECT)
110 smk_bu_mode(mode, acc);
111 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
112 sskp->smk_known, oskp->smk_known, acc, note);
116 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
119 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
120 static int smk_bu_current(char *note, struct smack_known *oskp,
123 struct task_smack *tsp = current_security();
124 char acc[SMK_NUM_ACCESS_TYPE + 1];
128 if (rc > SMACK_UNCONFINED_OBJECT)
131 smk_bu_mode(mode, acc);
132 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
133 tsp->smk_task->smk_known, oskp->smk_known,
134 acc, current->comm, note);
138 #define smk_bu_current(note, oskp, mode, RC) (RC)
141 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
142 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
144 struct task_smack *tsp = current_security();
145 struct smack_known *smk_task = smk_of_task_struct(otp);
146 char acc[SMK_NUM_ACCESS_TYPE + 1];
150 if (rc > SMACK_UNCONFINED_OBJECT)
153 smk_bu_mode(mode, acc);
154 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
155 tsp->smk_task->smk_known, smk_task->smk_known, acc,
156 current->comm, otp->comm);
160 #define smk_bu_task(otp, mode, RC) (RC)
163 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
164 static int smk_bu_inode(struct inode *inode, int mode, int rc)
166 struct task_smack *tsp = current_security();
167 struct inode_smack *isp = inode->i_security;
168 char acc[SMK_NUM_ACCESS_TYPE + 1];
170 if (isp->smk_flags & SMK_INODE_IMPURE)
171 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
172 inode->i_sb->s_id, inode->i_ino, current->comm);
176 if (rc > SMACK_UNCONFINED_OBJECT)
178 if (rc == SMACK_UNCONFINED_SUBJECT &&
179 (mode & (MAY_WRITE | MAY_APPEND)))
180 isp->smk_flags |= SMK_INODE_IMPURE;
182 smk_bu_mode(mode, acc);
184 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
185 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
186 inode->i_sb->s_id, inode->i_ino, current->comm);
190 #define smk_bu_inode(inode, mode, RC) (RC)
193 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
194 static int smk_bu_file(struct file *file, int mode, int rc)
196 struct task_smack *tsp = current_security();
197 struct smack_known *sskp = tsp->smk_task;
198 struct inode *inode = file_inode(file);
199 struct inode_smack *isp = inode->i_security;
200 char acc[SMK_NUM_ACCESS_TYPE + 1];
202 if (isp->smk_flags & SMK_INODE_IMPURE)
203 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
204 inode->i_sb->s_id, inode->i_ino, current->comm);
208 if (rc > SMACK_UNCONFINED_OBJECT)
211 smk_bu_mode(mode, acc);
212 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
213 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
214 inode->i_sb->s_id, inode->i_ino, file,
219 #define smk_bu_file(file, mode, RC) (RC)
222 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
223 static int smk_bu_credfile(const struct cred *cred, struct file *file,
226 struct task_smack *tsp = cred->security;
227 struct smack_known *sskp = tsp->smk_task;
228 struct inode *inode = file->f_inode;
229 struct inode_smack *isp = inode->i_security;
230 char acc[SMK_NUM_ACCESS_TYPE + 1];
232 if (isp->smk_flags & SMK_INODE_IMPURE)
233 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
234 inode->i_sb->s_id, inode->i_ino, current->comm);
238 if (rc > SMACK_UNCONFINED_OBJECT)
241 smk_bu_mode(mode, acc);
242 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
243 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
244 inode->i_sb->s_id, inode->i_ino, file,
249 #define smk_bu_credfile(cred, file, mode, RC) (RC)
253 * smk_fetch - Fetch the smack label from a file.
254 * @name: type of the label (attribute)
255 * @ip: a pointer to the inode
256 * @dp: a pointer to the dentry
258 * Returns a pointer to the master list entry for the Smack label,
259 * NULL if there was no label to fetch, or an error code.
261 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
266 struct smack_known *skp = NULL;
268 if (ip->i_op->getxattr == NULL)
269 return ERR_PTR(-EOPNOTSUPP);
271 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
273 return ERR_PTR(-ENOMEM);
275 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
281 skp = smk_import_entry(buffer, rc);
289 * new_inode_smack - allocate an inode security blob
290 * @skp: a pointer to the Smack label entry to use in the blob
292 * Returns the new blob or NULL if there's no memory available
294 static struct inode_smack *new_inode_smack(struct smack_known *skp)
296 struct inode_smack *isp;
298 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
302 isp->smk_inode = skp;
304 mutex_init(&isp->smk_lock);
310 * new_task_smack - allocate a task security blob
311 * @task: a pointer to the Smack label for the running task
312 * @forked: a pointer to the Smack label for the forked task
313 * @gfp: type of the memory for the allocation
315 * Returns the new blob or NULL if there's no memory available
317 static struct task_smack *new_task_smack(struct smack_known *task,
318 struct smack_known *forked, gfp_t gfp)
320 struct task_smack *tsp;
322 tsp = kzalloc(sizeof(struct task_smack), gfp);
326 tsp->smk_task = task;
327 tsp->smk_forked = forked;
328 INIT_LIST_HEAD(&tsp->smk_rules);
329 mutex_init(&tsp->smk_rules_lock);
335 * smk_copy_rules - copy a rule set
336 * @nhead: new rules header pointer
337 * @ohead: old rules header pointer
338 * @gfp: type of the memory for the allocation
340 * Returns 0 on success, -ENOMEM on error
342 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
345 struct smack_rule *nrp;
346 struct smack_rule *orp;
349 INIT_LIST_HEAD(nhead);
351 list_for_each_entry_rcu(orp, ohead, list) {
352 nrp = kzalloc(sizeof(struct smack_rule), gfp);
358 list_add_rcu(&nrp->list, nhead);
364 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
365 * @mode - input mode in form of PTRACE_MODE_*
367 * Returns a converted MAY_* mode usable by smack rules
369 static inline unsigned int smk_ptrace_mode(unsigned int mode)
372 case PTRACE_MODE_READ:
374 case PTRACE_MODE_ATTACH:
375 return MAY_READWRITE;
382 * smk_ptrace_rule_check - helper for ptrace access
383 * @tracer: tracer process
384 * @tracee_known: label entry of the process that's about to be traced
385 * @mode: ptrace attachment mode (PTRACE_MODE_*)
386 * @func: name of the function that called us, used for audit
388 * Returns 0 on access granted, -error on error
390 static int smk_ptrace_rule_check(struct task_struct *tracer,
391 struct smack_known *tracee_known,
392 unsigned int mode, const char *func)
395 struct smk_audit_info ad, *saip = NULL;
396 struct task_smack *tsp;
397 struct smack_known *tracer_known;
399 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
400 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
401 smk_ad_setfield_u_tsk(&ad, tracer);
406 tsp = __task_cred(tracer)->security;
407 tracer_known = smk_of_task(tsp);
409 if ((mode & PTRACE_MODE_ATTACH) &&
410 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
411 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
412 if (tracer_known->smk_known == tracee_known->smk_known)
414 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
416 else if (capable(CAP_SYS_PTRACE))
422 smack_log(tracer_known->smk_known,
423 tracee_known->smk_known,
430 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
431 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
439 * We he, that is fun!
443 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
444 * @ctp: child task pointer
445 * @mode: ptrace attachment mode (PTRACE_MODE_*)
447 * Returns 0 if access is OK, an error code otherwise
449 * Do the capability checks.
451 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
453 struct smack_known *skp;
455 skp = smk_of_task_struct(ctp);
457 return smk_ptrace_rule_check(current, skp, mode, __func__);
461 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
462 * @ptp: parent task pointer
464 * Returns 0 if access is OK, an error code otherwise
466 * Do the capability checks, and require PTRACE_MODE_ATTACH.
468 static int smack_ptrace_traceme(struct task_struct *ptp)
471 struct smack_known *skp;
473 skp = smk_of_task(current_security());
475 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
480 * smack_syslog - Smack approval on syslog
481 * @type: message type
483 * Returns 0 on success, error code otherwise.
485 static int smack_syslog(int typefrom_file)
488 struct smack_known *skp = smk_of_current();
490 if (smack_privileged(CAP_MAC_OVERRIDE))
493 if (smack_syslog_label != NULL && smack_syslog_label != skp)
505 * smack_sb_alloc_security - allocate a superblock blob
506 * @sb: the superblock getting the blob
508 * Returns 0 on success or -ENOMEM on error.
510 static int smack_sb_alloc_security(struct super_block *sb)
512 struct superblock_smack *sbsp;
514 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
519 sbsp->smk_root = &smack_known_floor;
520 sbsp->smk_default = &smack_known_floor;
521 sbsp->smk_floor = &smack_known_floor;
522 sbsp->smk_hat = &smack_known_hat;
524 * smk_initialized will be zero from kzalloc.
526 sb->s_security = sbsp;
532 * smack_sb_free_security - free a superblock blob
533 * @sb: the superblock getting the blob
536 static void smack_sb_free_security(struct super_block *sb)
538 kfree(sb->s_security);
539 sb->s_security = NULL;
543 * smack_sb_copy_data - copy mount options data for processing
544 * @orig: where to start
545 * @smackopts: mount options string
547 * Returns 0 on success or -ENOMEM on error.
549 * Copy the Smack specific mount options out of the mount
552 static int smack_sb_copy_data(char *orig, char *smackopts)
554 char *cp, *commap, *otheropts, *dp;
556 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
557 if (otheropts == NULL)
560 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
561 if (strstr(cp, SMK_FSDEFAULT) == cp)
563 else if (strstr(cp, SMK_FSFLOOR) == cp)
565 else if (strstr(cp, SMK_FSHAT) == cp)
567 else if (strstr(cp, SMK_FSROOT) == cp)
569 else if (strstr(cp, SMK_FSTRANS) == cp)
574 commap = strchr(cp, ',');
583 strcpy(orig, otheropts);
584 free_page((unsigned long)otheropts);
590 * smack_parse_opts_str - parse Smack specific mount options
591 * @options: mount options string
592 * @opts: where to store converted mount opts
594 * Returns 0 on success or -ENOMEM on error.
596 * converts Smack specific mount options to generic security option format
598 static int smack_parse_opts_str(char *options,
599 struct security_mnt_opts *opts)
602 char *fsdefault = NULL;
603 char *fsfloor = NULL;
606 char *fstransmute = NULL;
608 int num_mnt_opts = 0;
611 opts->num_mnt_opts = 0;
616 while ((p = strsep(&options, ",")) != NULL) {
617 substring_t args[MAX_OPT_ARGS];
622 token = match_token(p, smk_mount_tokens, args);
628 fsdefault = match_strdup(&args[0]);
635 fsfloor = match_strdup(&args[0]);
642 fshat = match_strdup(&args[0]);
649 fsroot = match_strdup(&args[0]);
653 case Opt_fstransmute:
656 fstransmute = match_strdup(&args[0]);
662 pr_warn("Smack: unknown mount option\n");
667 opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
671 opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
673 if (!opts->mnt_opts_flags) {
674 kfree(opts->mnt_opts);
679 opts->mnt_opts[num_mnt_opts] = fsdefault;
680 opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
683 opts->mnt_opts[num_mnt_opts] = fsfloor;
684 opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
687 opts->mnt_opts[num_mnt_opts] = fshat;
688 opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
691 opts->mnt_opts[num_mnt_opts] = fsroot;
692 opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
695 opts->mnt_opts[num_mnt_opts] = fstransmute;
696 opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
699 opts->num_mnt_opts = num_mnt_opts;
704 pr_warn("Smack: duplicate mount options\n");
716 * smack_set_mnt_opts - set Smack specific mount options
717 * @sb: the file system superblock
718 * @opts: Smack mount options
719 * @kern_flags: mount option from kernel space or user space
720 * @set_kern_flags: where to store converted mount opts
722 * Returns 0 on success, an error code on failure
724 * Allow filesystems with binary mount data to explicitly set Smack mount
727 static int smack_set_mnt_opts(struct super_block *sb,
728 struct security_mnt_opts *opts,
729 unsigned long kern_flags,
730 unsigned long *set_kern_flags)
732 struct dentry *root = sb->s_root;
733 struct inode *inode = d_backing_inode(root);
734 struct superblock_smack *sp = sb->s_security;
735 struct inode_smack *isp;
736 struct smack_known *skp;
738 int num_opts = opts->num_mnt_opts;
741 if (sp->smk_initialized)
744 sp->smk_initialized = 1;
746 for (i = 0; i < num_opts; i++) {
747 switch (opts->mnt_opts_flags[i]) {
749 skp = smk_import_entry(opts->mnt_opts[i], 0);
752 sp->smk_default = skp;
755 skp = smk_import_entry(opts->mnt_opts[i], 0);
761 skp = smk_import_entry(opts->mnt_opts[i], 0);
767 skp = smk_import_entry(opts->mnt_opts[i], 0);
773 skp = smk_import_entry(opts->mnt_opts[i], 0);
784 if (!smack_privileged(CAP_MAC_ADMIN)) {
786 * Unprivileged mounts don't get to specify Smack values.
791 * Unprivileged mounts get root and default from the caller.
793 skp = smk_of_current();
795 sp->smk_default = skp;
799 * Initialize the root inode.
801 isp = inode->i_security;
803 isp = new_inode_smack(sp->smk_root);
806 inode->i_security = isp;
808 isp->smk_inode = sp->smk_root;
811 isp->smk_flags |= SMK_INODE_TRANSMUTE;
817 * smack_sb_kern_mount - Smack specific mount processing
818 * @sb: the file system superblock
819 * @flags: the mount flags
820 * @data: the smack mount options
822 * Returns 0 on success, an error code on failure
824 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
827 char *options = data;
828 struct security_mnt_opts opts;
830 security_init_mnt_opts(&opts);
835 rc = smack_parse_opts_str(options, &opts);
840 rc = smack_set_mnt_opts(sb, &opts, 0, NULL);
843 security_free_mnt_opts(&opts);
848 * smack_sb_statfs - Smack check on statfs
849 * @dentry: identifies the file system in question
851 * Returns 0 if current can read the floor of the filesystem,
852 * and error code otherwise
854 static int smack_sb_statfs(struct dentry *dentry)
856 struct superblock_smack *sbp = dentry->d_sb->s_security;
858 struct smk_audit_info ad;
860 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
861 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
863 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
864 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
873 * smack_bprm_set_creds - set creds for exec
874 * @bprm: the exec information
876 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
878 static int smack_bprm_set_creds(struct linux_binprm *bprm)
880 struct inode *inode = file_inode(bprm->file);
881 struct task_smack *bsp = bprm->cred->security;
882 struct inode_smack *isp;
885 if (bprm->cred_prepared)
888 isp = inode->i_security;
889 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
892 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
893 struct task_struct *tracer;
897 tracer = ptrace_parent(current);
898 if (likely(tracer != NULL))
899 rc = smk_ptrace_rule_check(tracer,
907 } else if (bprm->unsafe)
910 bsp->smk_task = isp->smk_task;
911 bprm->per_clear |= PER_CLEAR_ON_SETID;
917 * smack_bprm_committing_creds - Prepare to install the new credentials
920 * @bprm: binprm for exec
922 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
924 struct task_smack *bsp = bprm->cred->security;
926 if (bsp->smk_task != bsp->smk_forked)
927 current->pdeath_signal = 0;
931 * smack_bprm_secureexec - Return the decision to use secureexec.
932 * @bprm: binprm for exec
934 * Returns 0 on success.
936 static int smack_bprm_secureexec(struct linux_binprm *bprm)
938 struct task_smack *tsp = current_security();
940 if (tsp->smk_task != tsp->smk_forked)
951 * smack_inode_alloc_security - allocate an inode blob
952 * @inode: the inode in need of a blob
954 * Returns 0 if it gets a blob, -ENOMEM otherwise
956 static int smack_inode_alloc_security(struct inode *inode)
958 struct smack_known *skp = smk_of_current();
960 inode->i_security = new_inode_smack(skp);
961 if (inode->i_security == NULL)
967 * smack_inode_free_security - free an inode blob
968 * @inode: the inode with a blob
970 * Clears the blob pointer in inode
972 static void smack_inode_free_security(struct inode *inode)
974 kmem_cache_free(smack_inode_cache, inode->i_security);
975 inode->i_security = NULL;
979 * smack_inode_init_security - copy out the smack from an inode
980 * @inode: the newly created inode
981 * @dir: containing directory object
983 * @name: where to put the attribute name
984 * @value: where to put the attribute value
985 * @len: where to put the length of the attribute
987 * Returns 0 if it all works out, -ENOMEM if there's no memory
989 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
990 const struct qstr *qstr, const char **name,
991 void **value, size_t *len)
993 struct inode_smack *issp = inode->i_security;
994 struct smack_known *skp = smk_of_current();
995 struct smack_known *isp = smk_of_inode(inode);
996 struct smack_known *dsp = smk_of_inode(dir);
1000 *name = XATTR_SMACK_SUFFIX;
1004 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1009 * If the access rule allows transmutation and
1010 * the directory requests transmutation then
1011 * by all means transmute.
1012 * Mark the inode as changed.
1014 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
1015 smk_inode_transmutable(dir)) {
1017 issp->smk_flags |= SMK_INODE_CHANGED;
1020 *value = kstrdup(isp->smk_known, GFP_NOFS);
1024 *len = strlen(isp->smk_known);
1031 * smack_inode_link - Smack check on link
1032 * @old_dentry: the existing object
1034 * @new_dentry: the new object
1036 * Returns 0 if access is permitted, an error code otherwise
1038 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1039 struct dentry *new_dentry)
1041 struct smack_known *isp;
1042 struct smk_audit_info ad;
1045 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1046 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1048 isp = smk_of_inode(d_backing_inode(old_dentry));
1049 rc = smk_curacc(isp, MAY_WRITE, &ad);
1050 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1052 if (rc == 0 && d_is_positive(new_dentry)) {
1053 isp = smk_of_inode(d_backing_inode(new_dentry));
1054 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1055 rc = smk_curacc(isp, MAY_WRITE, &ad);
1056 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1063 * smack_inode_unlink - Smack check on inode deletion
1064 * @dir: containing directory object
1065 * @dentry: file to unlink
1067 * Returns 0 if current can write the containing directory
1068 * and the object, error code otherwise
1070 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1072 struct inode *ip = d_backing_inode(dentry);
1073 struct smk_audit_info ad;
1076 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1077 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1080 * You need write access to the thing you're unlinking
1082 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1083 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1086 * You also need write access to the containing directory
1088 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1089 smk_ad_setfield_u_fs_inode(&ad, dir);
1090 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1091 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1097 * smack_inode_rmdir - Smack check on directory deletion
1098 * @dir: containing directory object
1099 * @dentry: directory to unlink
1101 * Returns 0 if current can write the containing directory
1102 * and the directory, error code otherwise
1104 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1106 struct smk_audit_info ad;
1109 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1110 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1113 * You need write access to the thing you're removing
1115 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1116 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1119 * You also need write access to the containing directory
1121 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1122 smk_ad_setfield_u_fs_inode(&ad, dir);
1123 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1124 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1131 * smack_inode_rename - Smack check on rename
1132 * @old_inode: unused
1133 * @old_dentry: the old object
1134 * @new_inode: unused
1135 * @new_dentry: the new object
1137 * Read and write access is required on both the old and
1140 * Returns 0 if access is permitted, an error code otherwise
1142 static int smack_inode_rename(struct inode *old_inode,
1143 struct dentry *old_dentry,
1144 struct inode *new_inode,
1145 struct dentry *new_dentry)
1148 struct smack_known *isp;
1149 struct smk_audit_info ad;
1151 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1152 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1154 isp = smk_of_inode(d_backing_inode(old_dentry));
1155 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1156 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1158 if (rc == 0 && d_is_positive(new_dentry)) {
1159 isp = smk_of_inode(d_backing_inode(new_dentry));
1160 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1161 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1162 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1168 * smack_inode_permission - Smack version of permission()
1169 * @inode: the inode in question
1170 * @mask: the access requested
1172 * This is the important Smack hook.
1174 * Returns 0 if access is permitted, -EACCES otherwise
1176 static int smack_inode_permission(struct inode *inode, int mask)
1178 struct smk_audit_info ad;
1179 int no_block = mask & MAY_NOT_BLOCK;
1182 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1184 * No permission to check. Existence test. Yup, it's there.
1189 /* May be droppable after audit */
1192 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1193 smk_ad_setfield_u_fs_inode(&ad, inode);
1194 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1195 rc = smk_bu_inode(inode, mask, rc);
1200 * smack_inode_setattr - Smack check for setting attributes
1201 * @dentry: the object
1202 * @iattr: for the force flag
1204 * Returns 0 if access is permitted, an error code otherwise
1206 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1208 struct smk_audit_info ad;
1212 * Need to allow for clearing the setuid bit.
1214 if (iattr->ia_valid & ATTR_FORCE)
1216 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1217 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1219 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1220 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1225 * smack_inode_getattr - Smack check for getting attributes
1226 * @mnt: vfsmount of the object
1227 * @dentry: the object
1229 * Returns 0 if access is permitted, an error code otherwise
1231 static int smack_inode_getattr(const struct path *path)
1233 struct smk_audit_info ad;
1234 struct inode *inode = d_backing_inode(path->dentry);
1237 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1238 smk_ad_setfield_u_fs_path(&ad, *path);
1239 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1240 rc = smk_bu_inode(inode, MAY_READ, rc);
1245 * smack_inode_setxattr - Smack check for setting xattrs
1246 * @dentry: the object
1247 * @name: name of the attribute
1248 * @value: value of the attribute
1249 * @size: size of the value
1252 * This protects the Smack attribute explicitly.
1254 * Returns 0 if access is permitted, an error code otherwise
1256 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1257 const void *value, size_t size, int flags)
1259 struct smk_audit_info ad;
1260 struct smack_known *skp;
1262 int check_import = 0;
1267 * Check label validity here so import won't fail in post_setxattr
1269 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1270 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1271 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1274 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1275 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1279 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1281 if (size != TRANS_TRUE_SIZE ||
1282 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1285 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1287 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1290 if (rc == 0 && check_import) {
1291 skp = size ? smk_import_entry(value, size) : NULL;
1294 else if (skp == NULL || (check_star &&
1295 (skp == &smack_known_star || skp == &smack_known_web)))
1299 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1300 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1303 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1304 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1311 * smack_inode_post_setxattr - Apply the Smack update approved above
1313 * @name: attribute name
1314 * @value: attribute value
1315 * @size: attribute size
1318 * Set the pointer in the inode blob to the entry found
1319 * in the master label list.
1321 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1322 const void *value, size_t size, int flags)
1324 struct smack_known *skp;
1325 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
1327 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1328 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1332 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1333 skp = smk_import_entry(value, size);
1335 isp->smk_inode = skp;
1337 isp->smk_inode = &smack_known_invalid;
1338 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1339 skp = smk_import_entry(value, size);
1341 isp->smk_task = skp;
1343 isp->smk_task = &smack_known_invalid;
1344 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1345 skp = smk_import_entry(value, size);
1347 isp->smk_mmap = skp;
1349 isp->smk_mmap = &smack_known_invalid;
1356 * smack_inode_getxattr - Smack check on getxattr
1357 * @dentry: the object
1360 * Returns 0 if access is permitted, an error code otherwise
1362 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1364 struct smk_audit_info ad;
1367 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1368 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1370 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1371 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1376 * smack_inode_removexattr - Smack check on removexattr
1377 * @dentry: the object
1378 * @name: name of the attribute
1380 * Removing the Smack attribute requires CAP_MAC_ADMIN
1382 * Returns 0 if access is permitted, an error code otherwise
1384 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1386 struct inode_smack *isp;
1387 struct smk_audit_info ad;
1390 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1391 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1392 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1393 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1394 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1395 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1396 if (!smack_privileged(CAP_MAC_ADMIN))
1399 rc = cap_inode_removexattr(dentry, name);
1404 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1405 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1407 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1408 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1412 isp = d_backing_inode(dentry)->i_security;
1414 * Don't do anything special for these.
1415 * XATTR_NAME_SMACKIPIN
1416 * XATTR_NAME_SMACKIPOUT
1417 * XATTR_NAME_SMACKEXEC
1419 if (strcmp(name, XATTR_NAME_SMACK) == 0)
1420 isp->smk_task = NULL;
1421 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1422 isp->smk_mmap = NULL;
1423 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1424 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1430 * smack_inode_getsecurity - get smack xattrs
1431 * @inode: the object
1432 * @name: attribute name
1433 * @buffer: where to put the result
1436 * Returns the size of the attribute or an error code
1438 static int smack_inode_getsecurity(const struct inode *inode,
1439 const char *name, void **buffer,
1442 struct socket_smack *ssp;
1443 struct socket *sock;
1444 struct super_block *sbp;
1445 struct inode *ip = (struct inode *)inode;
1446 struct smack_known *isp;
1450 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1451 isp = smk_of_inode(inode);
1452 ilen = strlen(isp->smk_known);
1453 *buffer = isp->smk_known;
1458 * The rest of the Smack xattrs are only on sockets.
1461 if (sbp->s_magic != SOCKFS_MAGIC)
1464 sock = SOCKET_I(ip);
1465 if (sock == NULL || sock->sk == NULL)
1468 ssp = sock->sk->sk_security;
1470 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1472 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1477 ilen = strlen(isp->smk_known);
1479 *buffer = isp->smk_known;
1488 * smack_inode_listsecurity - list the Smack attributes
1489 * @inode: the object
1490 * @buffer: where they go
1491 * @buffer_size: size of buffer
1493 * Returns 0 on success, -EINVAL otherwise
1495 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1498 int len = sizeof(XATTR_NAME_SMACK);
1500 if (buffer != NULL && len <= buffer_size)
1501 memcpy(buffer, XATTR_NAME_SMACK, len);
1507 * smack_inode_getsecid - Extract inode's security id
1508 * @inode: inode to extract the info from
1509 * @secid: where result will be saved
1511 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1513 struct inode_smack *isp = inode->i_security;
1515 *secid = isp->smk_inode->smk_secid;
1523 * smack_file_permission - Smack check on file operations
1529 * Should access checks be done on each read or write?
1530 * UNICOS and SELinux say yes.
1531 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1533 * I'll say no for now. Smack does not do the frequent
1534 * label changing that SELinux does.
1536 static int smack_file_permission(struct file *file, int mask)
1542 * smack_file_alloc_security - assign a file security blob
1545 * The security blob for a file is a pointer to the master
1546 * label list, so no allocation is done.
1548 * f_security is the owner security information. It
1549 * isn't used on file access checks, it's for send_sigio.
1553 static int smack_file_alloc_security(struct file *file)
1555 struct smack_known *skp = smk_of_current();
1557 file->f_security = skp;
1562 * smack_file_free_security - clear a file security blob
1565 * The security blob for a file is a pointer to the master
1566 * label list, so no memory is freed.
1568 static void smack_file_free_security(struct file *file)
1570 file->f_security = NULL;
1574 * smack_file_ioctl - Smack check on ioctls
1579 * Relies heavily on the correct use of the ioctl command conventions.
1581 * Returns 0 if allowed, error code otherwise
1583 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1587 struct smk_audit_info ad;
1588 struct inode *inode = file_inode(file);
1590 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1591 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1593 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1594 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1595 rc = smk_bu_file(file, MAY_WRITE, rc);
1598 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1599 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1600 rc = smk_bu_file(file, MAY_READ, rc);
1607 * smack_file_lock - Smack check on file locking
1611 * Returns 0 if current has lock access, error code otherwise
1613 static int smack_file_lock(struct file *file, unsigned int cmd)
1615 struct smk_audit_info ad;
1617 struct inode *inode = file_inode(file);
1619 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1620 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1621 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1622 rc = smk_bu_file(file, MAY_LOCK, rc);
1627 * smack_file_fcntl - Smack check on fcntl
1629 * @cmd: what action to check
1632 * Generally these operations are harmless.
1633 * File locking operations present an obvious mechanism
1634 * for passing information, so they require write access.
1636 * Returns 0 if current has access, error code otherwise
1638 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1641 struct smk_audit_info ad;
1643 struct inode *inode = file_inode(file);
1650 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1651 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1652 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1653 rc = smk_bu_file(file, MAY_LOCK, rc);
1657 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1658 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1659 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1660 rc = smk_bu_file(file, MAY_WRITE, rc);
1671 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1672 * if mapping anonymous memory.
1673 * @file contains the file structure for file to map (may be NULL).
1674 * @reqprot contains the protection requested by the application.
1675 * @prot contains the protection that will be applied by the kernel.
1676 * @flags contains the operational flags.
1677 * Return 0 if permission is granted.
1679 static int smack_mmap_file(struct file *file,
1680 unsigned long reqprot, unsigned long prot,
1681 unsigned long flags)
1683 struct smack_known *skp;
1684 struct smack_known *mkp;
1685 struct smack_rule *srp;
1686 struct task_smack *tsp;
1687 struct smack_known *okp;
1688 struct inode_smack *isp;
1697 isp = file_inode(file)->i_security;
1698 if (isp->smk_mmap == NULL)
1700 mkp = isp->smk_mmap;
1702 tsp = current_security();
1703 skp = smk_of_current();
1708 * For each Smack rule associated with the subject
1709 * label verify that the SMACK64MMAP also has access
1710 * to that rule's object label.
1712 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1713 okp = srp->smk_object;
1715 * Matching labels always allows access.
1717 if (mkp->smk_known == okp->smk_known)
1720 * If there is a matching local rule take
1721 * that into account as well.
1723 may = smk_access_entry(srp->smk_subject->smk_known,
1727 may = srp->smk_access;
1729 may &= srp->smk_access;
1731 * If may is zero the SMACK64MMAP subject can't
1732 * possibly have less access.
1738 * Fetch the global list entry.
1739 * If there isn't one a SMACK64MMAP subject
1740 * can't have as much access as current.
1742 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1744 if (mmay == -ENOENT) {
1749 * If there is a local entry it modifies the
1750 * potential access, too.
1752 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1754 if (tmay != -ENOENT)
1758 * If there is any access available to current that is
1759 * not available to a SMACK64MMAP subject
1762 if ((may | mmay) != mmay) {
1774 * smack_file_set_fowner - set the file security blob value
1775 * @file: object in question
1778 static void smack_file_set_fowner(struct file *file)
1780 file->f_security = smk_of_current();
1784 * smack_file_send_sigiotask - Smack on sigio
1785 * @tsk: The target task
1786 * @fown: the object the signal come from
1789 * Allow a privileged task to get signals even if it shouldn't
1791 * Returns 0 if a subject with the object's smack could
1792 * write to the task, an error code otherwise.
1794 static int smack_file_send_sigiotask(struct task_struct *tsk,
1795 struct fown_struct *fown, int signum)
1797 struct smack_known *skp;
1798 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1801 struct smk_audit_info ad;
1804 * struct fown_struct is never outside the context of a struct file
1806 file = container_of(fown, struct file, f_owner);
1808 /* we don't log here as rc can be overriden */
1809 skp = file->f_security;
1810 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1811 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
1812 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1815 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1816 smk_ad_setfield_u_tsk(&ad, tsk);
1817 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
1822 * smack_file_receive - Smack file receive check
1825 * Returns 0 if current has access, error code otherwise
1827 static int smack_file_receive(struct file *file)
1831 struct smk_audit_info ad;
1832 struct inode *inode = file_inode(file);
1834 if (unlikely(IS_PRIVATE(inode)))
1837 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1838 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1840 * This code relies on bitmasks.
1842 if (file->f_mode & FMODE_READ)
1844 if (file->f_mode & FMODE_WRITE)
1847 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1848 rc = smk_bu_file(file, may, rc);
1853 * smack_file_open - Smack dentry open processing
1855 * @cred: task credential
1857 * Set the security blob in the file structure.
1858 * Allow the open only if the task has read access. There are
1859 * many read operations (e.g. fstat) that you can do with an
1860 * fd even if you have the file open write-only.
1864 static int smack_file_open(struct file *file, const struct cred *cred)
1866 struct task_smack *tsp = cred->security;
1867 struct inode *inode = file_inode(file);
1868 struct smk_audit_info ad;
1871 if (smack_privileged(CAP_MAC_OVERRIDE))
1874 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1875 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1876 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
1877 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
1887 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1888 * @new: the new credentials
1889 * @gfp: the atomicity of any memory allocations
1891 * Prepare a blank set of credentials for modification. This must allocate all
1892 * the memory the LSM module might require such that cred_transfer() can
1893 * complete without error.
1895 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1897 struct task_smack *tsp;
1899 tsp = new_task_smack(NULL, NULL, gfp);
1903 cred->security = tsp;
1910 * smack_cred_free - "free" task-level security credentials
1911 * @cred: the credentials in question
1914 static void smack_cred_free(struct cred *cred)
1916 struct task_smack *tsp = cred->security;
1917 struct smack_rule *rp;
1918 struct list_head *l;
1919 struct list_head *n;
1923 cred->security = NULL;
1925 list_for_each_safe(l, n, &tsp->smk_rules) {
1926 rp = list_entry(l, struct smack_rule, list);
1927 list_del(&rp->list);
1934 * smack_cred_prepare - prepare new set of credentials for modification
1935 * @new: the new credentials
1936 * @old: the original credentials
1937 * @gfp: the atomicity of any memory allocations
1939 * Prepare a new set of credentials for modification.
1941 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1944 struct task_smack *old_tsp = old->security;
1945 struct task_smack *new_tsp;
1948 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1949 if (new_tsp == NULL)
1952 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1956 new->security = new_tsp;
1961 * smack_cred_transfer - Transfer the old credentials to the new credentials
1962 * @new: the new credentials
1963 * @old: the original credentials
1965 * Fill in a set of blank credentials from another set of credentials.
1967 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1969 struct task_smack *old_tsp = old->security;
1970 struct task_smack *new_tsp = new->security;
1972 new_tsp->smk_task = old_tsp->smk_task;
1973 new_tsp->smk_forked = old_tsp->smk_task;
1974 mutex_init(&new_tsp->smk_rules_lock);
1975 INIT_LIST_HEAD(&new_tsp->smk_rules);
1978 /* cbs copy rule list */
1982 * smack_kernel_act_as - Set the subjective context in a set of credentials
1983 * @new: points to the set of credentials to be modified.
1984 * @secid: specifies the security ID to be set
1986 * Set the security data for a kernel service.
1988 static int smack_kernel_act_as(struct cred *new, u32 secid)
1990 struct task_smack *new_tsp = new->security;
1991 struct smack_known *skp = smack_from_secid(secid);
1996 new_tsp->smk_task = skp;
2001 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2002 * @new: points to the set of credentials to be modified
2003 * @inode: points to the inode to use as a reference
2005 * Set the file creation context in a set of credentials to the same
2006 * as the objective context of the specified inode
2008 static int smack_kernel_create_files_as(struct cred *new,
2009 struct inode *inode)
2011 struct inode_smack *isp = inode->i_security;
2012 struct task_smack *tsp = new->security;
2014 tsp->smk_forked = isp->smk_inode;
2015 tsp->smk_task = tsp->smk_forked;
2020 * smk_curacc_on_task - helper to log task related access
2021 * @p: the task object
2022 * @access: the access requested
2023 * @caller: name of the calling function for audit
2025 * Return 0 if access is permitted
2027 static int smk_curacc_on_task(struct task_struct *p, int access,
2030 struct smk_audit_info ad;
2031 struct smack_known *skp = smk_of_task_struct(p);
2034 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2035 smk_ad_setfield_u_tsk(&ad, p);
2036 rc = smk_curacc(skp, access, &ad);
2037 rc = smk_bu_task(p, access, rc);
2042 * smack_task_setpgid - Smack check on setting pgid
2043 * @p: the task object
2046 * Return 0 if write access is permitted
2048 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2050 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2054 * smack_task_getpgid - Smack access check for getpgid
2055 * @p: the object task
2057 * Returns 0 if current can read the object task, error code otherwise
2059 static int smack_task_getpgid(struct task_struct *p)
2061 return smk_curacc_on_task(p, MAY_READ, __func__);
2065 * smack_task_getsid - Smack access check for getsid
2066 * @p: the object task
2068 * Returns 0 if current can read the object task, error code otherwise
2070 static int smack_task_getsid(struct task_struct *p)
2072 return smk_curacc_on_task(p, MAY_READ, __func__);
2076 * smack_task_getsecid - get the secid of the task
2077 * @p: the object task
2078 * @secid: where to put the result
2080 * Sets the secid to contain a u32 version of the smack label.
2082 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2084 struct smack_known *skp = smk_of_task_struct(p);
2086 *secid = skp->smk_secid;
2090 * smack_task_setnice - Smack check on setting nice
2091 * @p: the task object
2094 * Return 0 if write access is permitted
2096 static int smack_task_setnice(struct task_struct *p, int nice)
2098 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2102 * smack_task_setioprio - Smack check on setting ioprio
2103 * @p: the task object
2106 * Return 0 if write access is permitted
2108 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2110 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2114 * smack_task_getioprio - Smack check on reading ioprio
2115 * @p: the task object
2117 * Return 0 if read access is permitted
2119 static int smack_task_getioprio(struct task_struct *p)
2121 return smk_curacc_on_task(p, MAY_READ, __func__);
2125 * smack_task_setscheduler - Smack check on setting scheduler
2126 * @p: the task object
2130 * Return 0 if read access is permitted
2132 static int smack_task_setscheduler(struct task_struct *p)
2134 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2138 * smack_task_getscheduler - Smack check on reading scheduler
2139 * @p: the task object
2141 * Return 0 if read access is permitted
2143 static int smack_task_getscheduler(struct task_struct *p)
2145 return smk_curacc_on_task(p, MAY_READ, __func__);
2149 * smack_task_movememory - Smack check on moving memory
2150 * @p: the task object
2152 * Return 0 if write access is permitted
2154 static int smack_task_movememory(struct task_struct *p)
2156 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2160 * smack_task_kill - Smack check on signal delivery
2161 * @p: the task object
2164 * @secid: identifies the smack to use in lieu of current's
2166 * Return 0 if write access is permitted
2168 * The secid behavior is an artifact of an SELinux hack
2169 * in the USB code. Someday it may go away.
2171 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2174 struct smk_audit_info ad;
2175 struct smack_known *skp;
2176 struct smack_known *tkp = smk_of_task_struct(p);
2179 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2180 smk_ad_setfield_u_tsk(&ad, p);
2182 * Sending a signal requires that the sender
2183 * can write the receiver.
2186 rc = smk_curacc(tkp, MAY_WRITE, &ad);
2187 rc = smk_bu_task(p, MAY_WRITE, rc);
2191 * If the secid isn't 0 we're dealing with some USB IO
2192 * specific behavior. This is not clean. For one thing
2193 * we can't take privilege into account.
2195 skp = smack_from_secid(secid);
2196 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2197 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
2202 * smack_task_wait - Smack access check for waiting
2203 * @p: task to wait for
2207 static int smack_task_wait(struct task_struct *p)
2210 * Allow the operation to succeed.
2212 * In userless environments (e.g. phones) programs
2213 * get marked with SMACK64EXEC and even if the parent
2214 * and child shouldn't be talking the parent still
2215 * may expect to know when the child exits.
2221 * smack_task_to_inode - copy task smack into the inode blob
2222 * @p: task to copy from
2223 * @inode: inode to copy to
2225 * Sets the smack pointer in the inode security blob
2227 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2229 struct inode_smack *isp = inode->i_security;
2230 struct smack_known *skp = smk_of_task_struct(p);
2232 isp->smk_inode = skp;
2240 * smack_sk_alloc_security - Allocate a socket blob
2243 * @gfp_flags: memory allocation flags
2245 * Assign Smack pointers to current
2247 * Returns 0 on success, -ENOMEM is there's no memory
2249 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2251 struct smack_known *skp = smk_of_current();
2252 struct socket_smack *ssp;
2254 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2260 ssp->smk_packet = NULL;
2262 sk->sk_security = ssp;
2268 * smack_sk_free_security - Free a socket blob
2271 * Clears the blob pointer
2273 static void smack_sk_free_security(struct sock *sk)
2275 kfree(sk->sk_security);
2279 * smack_ipv4host_label - check host based restrictions
2280 * @sip: the object end
2282 * looks for host based access restrictions
2284 * This version will only be appropriate for really small sets of single label
2285 * hosts. The caller is responsible for ensuring that the RCU read lock is
2286 * taken before calling this function.
2288 * Returns the label of the far end or NULL if it's not special.
2290 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2292 struct smk_net4addr *snp;
2293 struct in_addr *siap = &sip->sin_addr;
2295 if (siap->s_addr == 0)
2298 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2300 * we break after finding the first match because
2301 * the list is sorted from longest to shortest mask
2302 * so we have found the most specific match
2304 if (snp->smk_host.s_addr ==
2305 (siap->s_addr & snp->smk_mask.s_addr))
2306 return snp->smk_label;
2311 #if IS_ENABLED(CONFIG_IPV6)
2313 * smk_ipv6_localhost - Check for local ipv6 host address
2316 * Returns boolean true if this is the localhost address
2318 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2320 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2321 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2323 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2324 ntohs(be16p[7]) == 1)
2330 * smack_ipv6host_label - check host based restrictions
2331 * @sip: the object end
2333 * looks for host based access restrictions
2335 * This version will only be appropriate for really small sets of single label
2336 * hosts. The caller is responsible for ensuring that the RCU read lock is
2337 * taken before calling this function.
2339 * Returns the label of the far end or NULL if it's not special.
2341 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2343 struct smk_net6addr *snp;
2344 struct in6_addr *sap = &sip->sin6_addr;
2349 * It's local. Don't look for a host label.
2351 if (smk_ipv6_localhost(sip))
2354 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2356 * we break after finding the first match because
2357 * the list is sorted from longest to shortest mask
2358 * so we have found the most specific match
2360 for (found = 1, i = 0; i < 8; i++) {
2362 * If the label is NULL the entry has
2363 * been renounced. Ignore it.
2365 if (snp->smk_label == NULL)
2367 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2368 snp->smk_host.s6_addr16[i]) {
2374 return snp->smk_label;
2379 #endif /* CONFIG_IPV6 */
2382 * smack_netlabel - Set the secattr on a socket
2384 * @labeled: socket label scheme
2386 * Convert the outbound smack value (smk_out) to a
2387 * secattr and attach it to the socket.
2389 * Returns 0 on success or an error code
2391 static int smack_netlabel(struct sock *sk, int labeled)
2393 struct smack_known *skp;
2394 struct socket_smack *ssp = sk->sk_security;
2398 * Usually the netlabel code will handle changing the
2399 * packet labeling based on the label.
2400 * The case of a single label host is different, because
2401 * a single label host should never get a labeled packet
2402 * even though the label is usually associated with a packet
2406 bh_lock_sock_nested(sk);
2408 if (ssp->smk_out == smack_net_ambient ||
2409 labeled == SMACK_UNLABELED_SOCKET)
2410 netlbl_sock_delattr(sk);
2413 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2423 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2425 * @sap: the destination address
2427 * Set the correct secattr for the given socket based on the destination
2428 * address and perform any outbound access checks needed.
2430 * Returns 0 on success or an error code.
2433 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2435 struct smack_known *skp;
2438 struct smack_known *hkp;
2439 struct socket_smack *ssp = sk->sk_security;
2440 struct smk_audit_info ad;
2443 hkp = smack_ipv4host_label(sap);
2446 struct lsm_network_audit net;
2448 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2449 ad.a.u.net->family = sap->sin_family;
2450 ad.a.u.net->dport = sap->sin_port;
2451 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2453 sk_lbl = SMACK_UNLABELED_SOCKET;
2455 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2456 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2458 sk_lbl = SMACK_CIPSO_SOCKET;
2465 return smack_netlabel(sk, sk_lbl);
2468 #if IS_ENABLED(CONFIG_IPV6)
2470 * smk_ipv6_check - check Smack access
2471 * @subject: subject Smack label
2472 * @object: object Smack label
2474 * @act: the action being taken
2476 * Check an IPv6 access
2478 static int smk_ipv6_check(struct smack_known *subject,
2479 struct smack_known *object,
2480 struct sockaddr_in6 *address, int act)
2483 struct lsm_network_audit net;
2485 struct smk_audit_info ad;
2489 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2490 ad.a.u.net->family = PF_INET6;
2491 ad.a.u.net->dport = ntohs(address->sin6_port);
2492 if (act == SMK_RECEIVING)
2493 ad.a.u.net->v6info.saddr = address->sin6_addr;
2495 ad.a.u.net->v6info.daddr = address->sin6_addr;
2497 rc = smk_access(subject, object, MAY_WRITE, &ad);
2498 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2501 #endif /* CONFIG_IPV6 */
2503 #ifdef SMACK_IPV6_PORT_LABELING
2505 * smk_ipv6_port_label - Smack port access table management
2509 * Create or update the port list entry
2511 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2513 struct sock *sk = sock->sk;
2514 struct sockaddr_in6 *addr6;
2515 struct socket_smack *ssp = sock->sk->sk_security;
2516 struct smk_port_label *spp;
2517 unsigned short port = 0;
2519 if (address == NULL) {
2521 * This operation is changing the Smack information
2522 * on the bound socket. Take the changes to the port
2525 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2526 if (sk != spp->smk_sock)
2528 spp->smk_in = ssp->smk_in;
2529 spp->smk_out = ssp->smk_out;
2533 * A NULL address is only used for updating existing
2534 * bound entries. If there isn't one, it's OK.
2539 addr6 = (struct sockaddr_in6 *)address;
2540 port = ntohs(addr6->sin6_port);
2542 * This is a special case that is safely ignored.
2548 * Look for an existing port list entry.
2549 * This is an indication that a port is getting reused.
2551 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2552 if (spp->smk_port != port)
2554 spp->smk_port = port;
2556 spp->smk_in = ssp->smk_in;
2557 spp->smk_out = ssp->smk_out;
2562 * A new port entry is required.
2564 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2568 spp->smk_port = port;
2570 spp->smk_in = ssp->smk_in;
2571 spp->smk_out = ssp->smk_out;
2573 list_add(&spp->list, &smk_ipv6_port_list);
2578 * smk_ipv6_port_check - check Smack port access
2582 * Create or update the port list entry
2584 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2587 struct smk_port_label *spp;
2588 struct socket_smack *ssp = sk->sk_security;
2589 struct smack_known *skp = NULL;
2590 unsigned short port;
2591 struct smack_known *object;
2593 if (act == SMK_RECEIVING) {
2594 skp = smack_ipv6host_label(address);
2595 object = ssp->smk_in;
2598 object = smack_ipv6host_label(address);
2602 * The other end is a single label host.
2604 if (skp != NULL && object != NULL)
2605 return smk_ipv6_check(skp, object, address, act);
2607 skp = smack_net_ambient;
2609 object = smack_net_ambient;
2612 * It's remote, so port lookup does no good.
2614 if (!smk_ipv6_localhost(address))
2615 return smk_ipv6_check(skp, object, address, act);
2618 * It's local so the send check has to have passed.
2620 if (act == SMK_RECEIVING)
2623 port = ntohs(address->sin6_port);
2624 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2625 if (spp->smk_port != port)
2627 object = spp->smk_in;
2628 if (act == SMK_CONNECTING)
2629 ssp->smk_packet = spp->smk_out;
2633 return smk_ipv6_check(skp, object, address, act);
2635 #endif /* SMACK_IPV6_PORT_LABELING */
2638 * smack_inode_setsecurity - set smack xattrs
2639 * @inode: the object
2640 * @name: attribute name
2641 * @value: attribute value
2642 * @size: size of the attribute
2645 * Sets the named attribute in the appropriate blob
2647 * Returns 0 on success, or an error code
2649 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2650 const void *value, size_t size, int flags)
2652 struct smack_known *skp;
2653 struct inode_smack *nsp = inode->i_security;
2654 struct socket_smack *ssp;
2655 struct socket *sock;
2658 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2661 skp = smk_import_entry(value, size);
2663 return PTR_ERR(skp);
2665 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2666 nsp->smk_inode = skp;
2667 nsp->smk_flags |= SMK_INODE_INSTANT;
2671 * The rest of the Smack xattrs are only on sockets.
2673 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2676 sock = SOCKET_I(inode);
2677 if (sock == NULL || sock->sk == NULL)
2680 ssp = sock->sk->sk_security;
2682 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2684 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2686 if (sock->sk->sk_family == PF_INET) {
2687 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2690 "Smack: \"%s\" netlbl error %d.\n",
2696 #ifdef SMACK_IPV6_PORT_LABELING
2697 if (sock->sk->sk_family == PF_INET6)
2698 smk_ipv6_port_label(sock, NULL);
2705 * smack_socket_post_create - finish socket setup
2707 * @family: protocol family
2712 * Sets the netlabel information on the socket
2714 * Returns 0 on success, and error code otherwise
2716 static int smack_socket_post_create(struct socket *sock, int family,
2717 int type, int protocol, int kern)
2719 struct socket_smack *ssp;
2721 if (sock->sk == NULL)
2725 * Sockets created by kernel threads receive web label.
2727 if (unlikely(current->flags & PF_KTHREAD)) {
2728 ssp = sock->sk->sk_security;
2729 ssp->smk_in = &smack_known_web;
2730 ssp->smk_out = &smack_known_web;
2733 if (family != PF_INET)
2736 * Set the outbound netlbl.
2738 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2741 #ifdef SMACK_IPV6_PORT_LABELING
2743 * smack_socket_bind - record port binding information.
2745 * @address: the port address
2746 * @addrlen: size of the address
2748 * Records the label bound to a port.
2752 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2755 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2756 smk_ipv6_port_label(sock, address);
2759 #endif /* SMACK_IPV6_PORT_LABELING */
2762 * smack_socket_connect - connect access check
2764 * @sap: the other end
2765 * @addrlen: size of sap
2767 * Verifies that a connection may be possible
2769 * Returns 0 on success, and error code otherwise
2771 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2775 #if IS_ENABLED(CONFIG_IPV6)
2776 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2778 #ifdef SMACK_IPV6_SECMARK_LABELING
2779 struct smack_known *rsp;
2780 struct socket_smack *ssp = sock->sk->sk_security;
2783 if (sock->sk == NULL)
2786 switch (sock->sk->sk_family) {
2788 if (addrlen < sizeof(struct sockaddr_in))
2790 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2793 if (addrlen < sizeof(struct sockaddr_in6))
2795 #ifdef SMACK_IPV6_SECMARK_LABELING
2796 rsp = smack_ipv6host_label(sip);
2798 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2801 #ifdef SMACK_IPV6_PORT_LABELING
2802 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2810 * smack_flags_to_may - convert S_ to MAY_ values
2811 * @flags: the S_ value
2813 * Returns the equivalent MAY_ value
2815 static int smack_flags_to_may(int flags)
2819 if (flags & S_IRUGO)
2821 if (flags & S_IWUGO)
2823 if (flags & S_IXUGO)
2830 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2835 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2837 struct smack_known *skp = smk_of_current();
2839 msg->security = skp;
2844 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2847 * Clears the blob pointer
2849 static void smack_msg_msg_free_security(struct msg_msg *msg)
2851 msg->security = NULL;
2855 * smack_of_shm - the smack pointer for the shm
2858 * Returns a pointer to the smack value
2860 static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
2862 return (struct smack_known *)shp->shm_perm.security;
2866 * smack_shm_alloc_security - Set the security blob for shm
2871 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2873 struct kern_ipc_perm *isp = &shp->shm_perm;
2874 struct smack_known *skp = smk_of_current();
2876 isp->security = skp;
2881 * smack_shm_free_security - Clear the security blob for shm
2884 * Clears the blob pointer
2886 static void smack_shm_free_security(struct shmid_kernel *shp)
2888 struct kern_ipc_perm *isp = &shp->shm_perm;
2890 isp->security = NULL;
2894 * smk_curacc_shm : check if current has access on shm
2896 * @access : access requested
2898 * Returns 0 if current has the requested access, error code otherwise
2900 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2902 struct smack_known *ssp = smack_of_shm(shp);
2903 struct smk_audit_info ad;
2907 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2908 ad.a.u.ipc_id = shp->shm_perm.id;
2910 rc = smk_curacc(ssp, access, &ad);
2911 rc = smk_bu_current("shm", ssp, access, rc);
2916 * smack_shm_associate - Smack access check for shm
2918 * @shmflg: access requested
2920 * Returns 0 if current has the requested access, error code otherwise
2922 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2926 may = smack_flags_to_may(shmflg);
2927 return smk_curacc_shm(shp, may);
2931 * smack_shm_shmctl - Smack access check for shm
2933 * @cmd: what it wants to do
2935 * Returns 0 if current has the requested access, error code otherwise
2937 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2950 may = MAY_READWRITE;
2955 * System level information.
2961 return smk_curacc_shm(shp, may);
2965 * smack_shm_shmat - Smack access for shmat
2968 * @shmflg: access requested
2970 * Returns 0 if current has the requested access, error code otherwise
2972 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2977 may = smack_flags_to_may(shmflg);
2978 return smk_curacc_shm(shp, may);
2982 * smack_of_sem - the smack pointer for the sem
2985 * Returns a pointer to the smack value
2987 static struct smack_known *smack_of_sem(struct sem_array *sma)
2989 return (struct smack_known *)sma->sem_perm.security;
2993 * smack_sem_alloc_security - Set the security blob for sem
2998 static int smack_sem_alloc_security(struct sem_array *sma)
3000 struct kern_ipc_perm *isp = &sma->sem_perm;
3001 struct smack_known *skp = smk_of_current();
3003 isp->security = skp;
3008 * smack_sem_free_security - Clear the security blob for sem
3011 * Clears the blob pointer
3013 static void smack_sem_free_security(struct sem_array *sma)
3015 struct kern_ipc_perm *isp = &sma->sem_perm;
3017 isp->security = NULL;
3021 * smk_curacc_sem : check if current has access on sem
3023 * @access : access requested
3025 * Returns 0 if current has the requested access, error code otherwise
3027 static int smk_curacc_sem(struct sem_array *sma, int access)
3029 struct smack_known *ssp = smack_of_sem(sma);
3030 struct smk_audit_info ad;
3034 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3035 ad.a.u.ipc_id = sma->sem_perm.id;
3037 rc = smk_curacc(ssp, access, &ad);
3038 rc = smk_bu_current("sem", ssp, access, rc);
3043 * smack_sem_associate - Smack access check for sem
3045 * @semflg: access requested
3047 * Returns 0 if current has the requested access, error code otherwise
3049 static int smack_sem_associate(struct sem_array *sma, int semflg)
3053 may = smack_flags_to_may(semflg);
3054 return smk_curacc_sem(sma, may);
3058 * smack_sem_shmctl - Smack access check for sem
3060 * @cmd: what it wants to do
3062 * Returns 0 if current has the requested access, error code otherwise
3064 static int smack_sem_semctl(struct sem_array *sma, int cmd)
3082 may = MAY_READWRITE;
3087 * System level information
3094 return smk_curacc_sem(sma, may);
3098 * smack_sem_semop - Smack checks of semaphore operations
3104 * Treated as read and write in all cases.
3106 * Returns 0 if access is allowed, error code otherwise
3108 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
3109 unsigned nsops, int alter)
3111 return smk_curacc_sem(sma, MAY_READWRITE);
3115 * smack_msg_alloc_security - Set the security blob for msg
3120 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
3122 struct kern_ipc_perm *kisp = &msq->q_perm;
3123 struct smack_known *skp = smk_of_current();
3125 kisp->security = skp;
3130 * smack_msg_free_security - Clear the security blob for msg
3133 * Clears the blob pointer
3135 static void smack_msg_queue_free_security(struct msg_queue *msq)
3137 struct kern_ipc_perm *kisp = &msq->q_perm;
3139 kisp->security = NULL;
3143 * smack_of_msq - the smack pointer for the msq
3146 * Returns a pointer to the smack label entry
3148 static struct smack_known *smack_of_msq(struct msg_queue *msq)
3150 return (struct smack_known *)msq->q_perm.security;
3154 * smk_curacc_msq : helper to check if current has access on msq
3156 * @access : access requested
3158 * return 0 if current has access, error otherwise
3160 static int smk_curacc_msq(struct msg_queue *msq, int access)
3162 struct smack_known *msp = smack_of_msq(msq);
3163 struct smk_audit_info ad;
3167 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3168 ad.a.u.ipc_id = msq->q_perm.id;
3170 rc = smk_curacc(msp, access, &ad);
3171 rc = smk_bu_current("msq", msp, access, rc);
3176 * smack_msg_queue_associate - Smack access check for msg_queue
3178 * @msqflg: access requested
3180 * Returns 0 if current has the requested access, error code otherwise
3182 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
3186 may = smack_flags_to_may(msqflg);
3187 return smk_curacc_msq(msq, may);
3191 * smack_msg_queue_msgctl - Smack access check for msg_queue
3193 * @cmd: what it wants to do
3195 * Returns 0 if current has the requested access, error code otherwise
3197 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
3208 may = MAY_READWRITE;
3213 * System level information
3220 return smk_curacc_msq(msq, may);
3224 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3227 * @msqflg: access requested
3229 * Returns 0 if current has the requested access, error code otherwise
3231 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
3236 may = smack_flags_to_may(msqflg);
3237 return smk_curacc_msq(msq, may);
3241 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3248 * Returns 0 if current has read and write access, error code otherwise
3250 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3251 struct task_struct *target, long type, int mode)
3253 return smk_curacc_msq(msq, MAY_READWRITE);
3257 * smack_ipc_permission - Smack access for ipc_permission()
3258 * @ipp: the object permissions
3259 * @flag: access requested
3261 * Returns 0 if current has read and write access, error code otherwise
3263 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3265 struct smack_known *iskp = ipp->security;
3266 int may = smack_flags_to_may(flag);
3267 struct smk_audit_info ad;
3271 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3272 ad.a.u.ipc_id = ipp->id;
3274 rc = smk_curacc(iskp, may, &ad);
3275 rc = smk_bu_current("svipc", iskp, may, rc);
3280 * smack_ipc_getsecid - Extract smack security id
3281 * @ipp: the object permissions
3282 * @secid: where result will be saved
3284 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3286 struct smack_known *iskp = ipp->security;
3288 *secid = iskp->smk_secid;
3292 * smack_d_instantiate - Make sure the blob is correct on an inode
3293 * @opt_dentry: dentry where inode will be attached
3294 * @inode: the object
3296 * Set the inode's security blob if it hasn't been done already.
3298 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3300 struct super_block *sbp;
3301 struct superblock_smack *sbsp;
3302 struct inode_smack *isp;
3303 struct smack_known *skp;
3304 struct smack_known *ckp = smk_of_current();
3305 struct smack_known *final;
3306 char trattr[TRANS_TRUE_SIZE];
3314 isp = inode->i_security;
3316 mutex_lock(&isp->smk_lock);
3318 * If the inode is already instantiated
3319 * take the quick way out
3321 if (isp->smk_flags & SMK_INODE_INSTANT)
3325 sbsp = sbp->s_security;
3327 * We're going to use the superblock default label
3328 * if there's no label on the file.
3330 final = sbsp->smk_default;
3333 * If this is the root inode the superblock
3334 * may be in the process of initialization.
3335 * If that is the case use the root value out
3336 * of the superblock.
3338 if (opt_dentry->d_parent == opt_dentry) {
3339 switch (sbp->s_magic) {
3340 case CGROUP_SUPER_MAGIC:
3342 * The cgroup filesystem is never mounted,
3343 * so there's no opportunity to set the mount
3346 sbsp->smk_root = &smack_known_star;
3347 sbsp->smk_default = &smack_known_star;
3348 isp->smk_inode = sbsp->smk_root;
3352 * What about shmem/tmpfs anonymous files with dentry
3353 * obtained from d_alloc_pseudo()?
3355 isp->smk_inode = smk_of_current();
3358 isp->smk_inode = sbsp->smk_root;
3361 isp->smk_flags |= SMK_INODE_INSTANT;
3366 * This is pretty hackish.
3367 * Casey says that we shouldn't have to do
3368 * file system specific code, but it does help
3369 * with keeping it simple.
3371 switch (sbp->s_magic) {
3375 case CGROUP_SUPER_MAGIC:
3377 * Casey says that it's a little embarrassing
3378 * that the smack file system doesn't do
3379 * extended attributes.
3381 * Casey says pipes are easy (?)
3383 * Socket access is controlled by the socket
3384 * structures associated with the task involved.
3386 * Cgroupfs is special
3388 final = &smack_known_star;
3390 case DEVPTS_SUPER_MAGIC:
3392 * devpts seems content with the label of the task.
3393 * Programs that change smack have to treat the
3398 case PROC_SUPER_MAGIC:
3400 * Casey says procfs appears not to care.
3401 * The superblock default suffices.
3406 * Device labels should come from the filesystem,
3407 * but watch out, because they're volitile,
3408 * getting recreated on every reboot.
3410 final = &smack_known_star;
3414 * If a smack value has been set we want to use it,
3415 * but since tmpfs isn't giving us the opportunity
3416 * to set mount options simulate setting the
3417 * superblock default.
3421 * This isn't an understood special case.
3422 * Get the value from the xattr.
3426 * UNIX domain sockets use lower level socket data.
3428 if (S_ISSOCK(inode->i_mode)) {
3429 final = &smack_known_star;
3433 * No xattr support means, alas, no SMACK label.
3434 * Use the aforeapplied default.
3435 * It would be curious if the label of the task
3436 * does not match that assigned.
3438 if (inode->i_op->getxattr == NULL)
3441 * Get the dentry for xattr.
3443 dp = dget(opt_dentry);
3444 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3445 if (!IS_ERR_OR_NULL(skp))
3449 * Transmuting directory
3451 if (S_ISDIR(inode->i_mode)) {
3453 * If this is a new directory and the label was
3454 * transmuted when the inode was initialized
3455 * set the transmute attribute on the directory
3456 * and mark the inode.
3458 * If there is a transmute attribute on the
3459 * directory mark the inode.
3461 if (isp->smk_flags & SMK_INODE_CHANGED) {
3462 isp->smk_flags &= ~SMK_INODE_CHANGED;
3463 rc = inode->i_op->setxattr(dp,
3464 XATTR_NAME_SMACKTRANSMUTE,
3465 TRANS_TRUE, TRANS_TRUE_SIZE,
3468 rc = inode->i_op->getxattr(dp,
3469 XATTR_NAME_SMACKTRANSMUTE, trattr,
3471 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3472 TRANS_TRUE_SIZE) != 0)
3476 transflag = SMK_INODE_TRANSMUTE;
3479 * Don't let the exec or mmap label be "*" or "@".
3481 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3482 if (IS_ERR(skp) || skp == &smack_known_star ||
3483 skp == &smack_known_web)
3485 isp->smk_task = skp;
3487 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3488 if (IS_ERR(skp) || skp == &smack_known_star ||
3489 skp == &smack_known_web)
3491 isp->smk_mmap = skp;
3498 isp->smk_inode = ckp;
3500 isp->smk_inode = final;
3502 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3505 mutex_unlock(&isp->smk_lock);
3510 * smack_getprocattr - Smack process attribute access
3511 * @p: the object task
3512 * @name: the name of the attribute in /proc/.../attr
3513 * @value: where to put the result
3515 * Places a copy of the task Smack into value
3517 * Returns the length of the smack label or an error code
3519 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3521 struct smack_known *skp = smk_of_task_struct(p);
3525 if (strcmp(name, "current") != 0)
3528 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3538 * smack_setprocattr - Smack process attribute setting
3539 * @p: the object task
3540 * @name: the name of the attribute in /proc/.../attr
3541 * @value: the value to set
3542 * @size: the size of the value
3544 * Sets the Smack value of the task. Only setting self
3545 * is permitted and only with privilege
3547 * Returns the length of the smack label or an error code
3549 static int smack_setprocattr(struct task_struct *p, char *name,
3550 void *value, size_t size)
3552 struct task_smack *tsp;
3554 struct smack_known *skp;
3557 * Changing another process' Smack value is too dangerous
3558 * and supports no sane use case.
3563 if (!smack_privileged(CAP_MAC_ADMIN))
3566 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3569 if (strcmp(name, "current") != 0)
3572 skp = smk_import_entry(value, size);
3574 return PTR_ERR(skp);
3577 * No process is ever allowed the web ("@") label.
3579 if (skp == &smack_known_web)
3582 new = prepare_creds();
3586 tsp = new->security;
3587 tsp->smk_task = skp;
3594 * smack_unix_stream_connect - Smack access on UDS
3596 * @other: the other sock
3599 * Return 0 if a subject with the smack of sock could access
3600 * an object with the smack of other, otherwise an error code
3602 static int smack_unix_stream_connect(struct sock *sock,
3603 struct sock *other, struct sock *newsk)
3605 struct smack_known *skp;
3606 struct smack_known *okp;
3607 struct socket_smack *ssp = sock->sk_security;
3608 struct socket_smack *osp = other->sk_security;
3609 struct socket_smack *nsp = newsk->sk_security;
3610 struct smk_audit_info ad;
3613 struct lsm_network_audit net;
3616 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3620 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3621 smk_ad_setfield_u_net_sk(&ad, other);
3623 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3624 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3628 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3629 rc = smk_bu_note("UDS connect", okp, skp,
3635 * Cross reference the peer labels for SO_PEERSEC.
3638 nsp->smk_packet = ssp->smk_out;
3639 ssp->smk_packet = osp->smk_out;
3646 * smack_unix_may_send - Smack access on UDS
3648 * @other: the other socket
3650 * Return 0 if a subject with the smack of sock could access
3651 * an object with the smack of other, otherwise an error code
3653 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3655 struct socket_smack *ssp = sock->sk->sk_security;
3656 struct socket_smack *osp = other->sk->sk_security;
3657 struct smk_audit_info ad;
3661 struct lsm_network_audit net;
3663 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3664 smk_ad_setfield_u_net_sk(&ad, other->sk);
3667 if (smack_privileged(CAP_MAC_OVERRIDE))
3670 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3671 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3676 * smack_socket_sendmsg - Smack check based on destination host
3679 * @size: the size of the message
3681 * Return 0 if the current subject can write to the destination host.
3682 * For IPv4 this is only a question if the destination is a single label host.
3683 * For IPv6 this is a check against the label of the port.
3685 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3688 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3689 #if IS_ENABLED(CONFIG_IPV6)
3690 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3692 #ifdef SMACK_IPV6_SECMARK_LABELING
3693 struct socket_smack *ssp = sock->sk->sk_security;
3694 struct smack_known *rsp;
3699 * Perfectly reasonable for this to be NULL
3704 switch (sip->sin_family) {
3706 rc = smack_netlabel_send(sock->sk, sip);
3709 #ifdef SMACK_IPV6_SECMARK_LABELING
3710 rsp = smack_ipv6host_label(sap);
3712 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3715 #ifdef SMACK_IPV6_PORT_LABELING
3716 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3724 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3725 * @sap: netlabel secattr
3726 * @ssp: socket security information
3728 * Returns a pointer to a Smack label entry found on the label list.
3730 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3731 struct socket_smack *ssp)
3733 struct smack_known *skp;
3738 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3740 * Looks like a CIPSO packet.
3741 * If there are flags but no level netlabel isn't
3742 * behaving the way we expect it to.
3744 * Look it up in the label table
3745 * Without guidance regarding the smack value
3746 * for the packet fall back on the network
3750 list_for_each_entry(skp, &smack_known_list, list) {
3751 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3754 * Compare the catsets. Use the netlbl APIs.
3756 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3757 if ((skp->smk_netlabel.flags &
3758 NETLBL_SECATTR_MLS_CAT) == 0)
3762 for (acat = -1, kcat = -1; acat == kcat; ) {
3763 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3765 kcat = netlbl_catmap_walk(
3766 skp->smk_netlabel.attr.mls.cat,
3768 if (acat < 0 || kcat < 0)
3781 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3782 return &smack_known_web;
3783 return &smack_known_star;
3785 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3787 * Looks like a fallback, which gives us a secid.
3789 skp = smack_from_secid(sap->attr.secid);
3791 * This has got to be a bug because it is
3792 * impossible to specify a fallback without
3793 * specifying the label, which will ensure
3794 * it has a secid, and the only way to get a
3795 * secid is from a fallback.
3797 BUG_ON(skp == NULL);
3801 * Without guidance regarding the smack value
3802 * for the packet fall back on the network
3805 return smack_net_ambient;
3808 #if IS_ENABLED(CONFIG_IPV6)
3809 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3813 int proto = -EINVAL;
3814 struct ipv6hdr _ipv6h;
3815 struct ipv6hdr *ip6;
3817 struct tcphdr _tcph, *th;
3818 struct udphdr _udph, *uh;
3819 struct dccp_hdr _dccph, *dh;
3823 offset = skb_network_offset(skb);
3824 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3827 sip->sin6_addr = ip6->saddr;
3829 nexthdr = ip6->nexthdr;
3830 offset += sizeof(_ipv6h);
3831 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3838 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3840 sip->sin6_port = th->source;
3843 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3845 sip->sin6_port = uh->source;
3848 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3850 sip->sin6_port = dh->dccph_sport;
3855 #endif /* CONFIG_IPV6 */
3858 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3862 * Returns 0 if the packet should be delivered, an error code otherwise
3864 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3866 struct netlbl_lsm_secattr secattr;
3867 struct socket_smack *ssp = sk->sk_security;
3868 struct smack_known *skp = NULL;
3870 struct smk_audit_info ad;
3872 struct lsm_network_audit net;
3874 #if IS_ENABLED(CONFIG_IPV6)
3875 struct sockaddr_in6 sadd;
3877 #endif /* CONFIG_IPV6 */
3879 switch (sk->sk_family) {
3881 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3883 * If there is a secmark use it rather than the CIPSO label.
3884 * If there is no secmark fall back to CIPSO.
3885 * The secmark is assumed to reflect policy better.
3887 if (skb && skb->secmark != 0) {
3888 skp = smack_from_secid(skb->secmark);
3891 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3893 * Translate what netlabel gave us.
3895 netlbl_secattr_init(&secattr);
3897 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3899 skp = smack_from_secattr(&secattr, ssp);
3901 skp = smack_net_ambient;
3903 netlbl_secattr_destroy(&secattr);
3905 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3909 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3910 ad.a.u.net->family = sk->sk_family;
3911 ad.a.u.net->netif = skb->skb_iif;
3912 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3915 * Receiving a packet requires that the other end
3916 * be able to write here. Read access is not required.
3917 * This is the simplist possible security model
3920 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3921 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3924 netlbl_skbuff_err(skb, rc, 0);
3926 #if IS_ENABLED(CONFIG_IPV6)
3928 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3929 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3931 #ifdef SMACK_IPV6_SECMARK_LABELING
3932 if (skb && skb->secmark != 0)
3933 skp = smack_from_secid(skb->secmark);
3935 skp = smack_ipv6host_label(&sadd);
3937 skp = smack_net_ambient;
3939 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3940 ad.a.u.net->family = sk->sk_family;
3941 ad.a.u.net->netif = skb->skb_iif;
3942 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3943 #endif /* CONFIG_AUDIT */
3944 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3945 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3947 #endif /* SMACK_IPV6_SECMARK_LABELING */
3948 #ifdef SMACK_IPV6_PORT_LABELING
3949 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3950 #endif /* SMACK_IPV6_PORT_LABELING */
3952 #endif /* CONFIG_IPV6 */
3959 * smack_socket_getpeersec_stream - pull in packet label
3961 * @optval: user's destination
3962 * @optlen: size thereof
3965 * returns zero on success, an error code otherwise
3967 static int smack_socket_getpeersec_stream(struct socket *sock,
3968 char __user *optval,
3969 int __user *optlen, unsigned len)
3971 struct socket_smack *ssp;
3976 ssp = sock->sk->sk_security;
3977 if (ssp->smk_packet != NULL) {
3978 rcp = ssp->smk_packet->smk_known;
3979 slen = strlen(rcp) + 1;
3984 else if (copy_to_user(optval, rcp, slen) != 0)
3987 if (put_user(slen, optlen) != 0)
3995 * smack_socket_getpeersec_dgram - pull in packet label
3996 * @sock: the peer socket
3998 * @secid: pointer to where to put the secid of the packet
4000 * Sets the netlabel socket state on sk from parent
4002 static int smack_socket_getpeersec_dgram(struct socket *sock,
4003 struct sk_buff *skb, u32 *secid)
4006 struct netlbl_lsm_secattr secattr;
4007 struct socket_smack *ssp = NULL;
4008 struct smack_known *skp;
4009 int family = PF_UNSPEC;
4010 u32 s = 0; /* 0 is the invalid secid */
4014 if (skb->protocol == htons(ETH_P_IP))
4016 #if IS_ENABLED(CONFIG_IPV6)
4017 else if (skb->protocol == htons(ETH_P_IPV6))
4019 #endif /* CONFIG_IPV6 */
4021 if (family == PF_UNSPEC && sock != NULL)
4022 family = sock->sk->sk_family;
4026 ssp = sock->sk->sk_security;
4027 s = ssp->smk_out->smk_secid;
4030 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4036 * Translate what netlabel gave us.
4038 if (sock != NULL && sock->sk != NULL)
4039 ssp = sock->sk->sk_security;
4040 netlbl_secattr_init(&secattr);
4041 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4043 skp = smack_from_secattr(&secattr, ssp);
4046 netlbl_secattr_destroy(&secattr);
4049 #ifdef SMACK_IPV6_SECMARK_LABELING
4061 * smack_sock_graft - Initialize a newly created socket with an existing sock
4063 * @parent: parent socket
4065 * Set the smk_{in,out} state of an existing sock based on the process that
4066 * is creating the new socket.
4068 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4070 struct socket_smack *ssp;
4071 struct smack_known *skp = smk_of_current();
4074 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4077 ssp = sk->sk_security;
4080 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4084 * smack_inet_conn_request - Smack access check on connect
4085 * @sk: socket involved
4089 * Returns 0 if a task with the packet label could write to
4090 * the socket, otherwise an error code
4092 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4093 struct request_sock *req)
4095 u16 family = sk->sk_family;
4096 struct smack_known *skp;
4097 struct socket_smack *ssp = sk->sk_security;
4098 struct netlbl_lsm_secattr secattr;
4099 struct sockaddr_in addr;
4101 struct smack_known *hskp;
4103 struct smk_audit_info ad;
4105 struct lsm_network_audit net;
4108 #if IS_ENABLED(CONFIG_IPV6)
4109 if (family == PF_INET6) {
4111 * Handle mapped IPv4 packets arriving
4112 * via IPv6 sockets. Don't set up netlabel
4113 * processing on IPv6.
4115 if (skb->protocol == htons(ETH_P_IP))
4120 #endif /* CONFIG_IPV6 */
4122 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4124 * If there is a secmark use it rather than the CIPSO label.
4125 * If there is no secmark fall back to CIPSO.
4126 * The secmark is assumed to reflect policy better.
4128 if (skb && skb->secmark != 0) {
4129 skp = smack_from_secid(skb->secmark);
4132 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4134 netlbl_secattr_init(&secattr);
4135 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4137 skp = smack_from_secattr(&secattr, ssp);
4139 skp = &smack_known_huh;
4140 netlbl_secattr_destroy(&secattr);
4142 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4147 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4148 ad.a.u.net->family = family;
4149 ad.a.u.net->netif = skb->skb_iif;
4150 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4153 * Receiving a packet requires that the other end be able to write
4154 * here. Read access is not required.
4156 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4157 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4162 * Save the peer's label in the request_sock so we can later setup
4163 * smk_packet in the child socket so that SO_PEERCRED can report it.
4165 req->peer_secid = skp->smk_secid;
4168 * We need to decide if we want to label the incoming connection here
4169 * if we do we only need to label the request_sock and the stack will
4170 * propagate the wire-label to the sock when it is created.
4173 addr.sin_addr.s_addr = hdr->saddr;
4175 hskp = smack_ipv4host_label(&addr);
4179 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4181 netlbl_req_delattr(req);
4187 * smack_inet_csk_clone - Copy the connection information to the new socket
4188 * @sk: the new socket
4189 * @req: the connection's request_sock
4191 * Transfer the connection's peer label to the newly created socket.
4193 static void smack_inet_csk_clone(struct sock *sk,
4194 const struct request_sock *req)
4196 struct socket_smack *ssp = sk->sk_security;
4197 struct smack_known *skp;
4199 if (req->peer_secid != 0) {
4200 skp = smack_from_secid(req->peer_secid);
4201 ssp->smk_packet = skp;
4203 ssp->smk_packet = NULL;
4207 * Key management security hooks
4209 * Casey has not tested key support very heavily.
4210 * The permission check is most likely too restrictive.
4211 * If you care about keys please have a look.
4216 * smack_key_alloc - Set the key security blob
4218 * @cred: the credentials to use
4221 * No allocation required
4225 static int smack_key_alloc(struct key *key, const struct cred *cred,
4226 unsigned long flags)
4228 struct smack_known *skp = smk_of_task(cred->security);
4230 key->security = skp;
4235 * smack_key_free - Clear the key security blob
4238 * Clear the blob pointer
4240 static void smack_key_free(struct key *key)
4242 key->security = NULL;
4246 * smack_key_permission - Smack access on a key
4247 * @key_ref: gets to the object
4248 * @cred: the credentials to use
4249 * @perm: requested key permissions
4251 * Return 0 if the task has read and write to the object,
4252 * an error code otherwise
4254 static int smack_key_permission(key_ref_t key_ref,
4255 const struct cred *cred, unsigned perm)
4258 struct smk_audit_info ad;
4259 struct smack_known *tkp = smk_of_task(cred->security);
4263 keyp = key_ref_to_ptr(key_ref);
4267 * If the key hasn't been initialized give it access so that
4270 if (keyp->security == NULL)
4273 * This should not occur
4278 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4279 ad.a.u.key_struct.key = keyp->serial;
4280 ad.a.u.key_struct.key_desc = keyp->description;
4282 if (perm & KEY_NEED_READ)
4284 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4285 request = MAY_WRITE;
4286 rc = smk_access(tkp, keyp->security, request, &ad);
4287 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4292 * smack_key_getsecurity - Smack label tagging the key
4293 * @key points to the key to be queried
4294 * @_buffer points to a pointer that should be set to point to the
4295 * resulting string (if no label or an error occurs).
4296 * Return the length of the string (including terminating NUL) or -ve if
4298 * May also return 0 (and a NULL buffer pointer) if there is no label.
4300 static int smack_key_getsecurity(struct key *key, char **_buffer)
4302 struct smack_known *skp = key->security;
4306 if (key->security == NULL) {
4311 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4314 length = strlen(copy) + 1;
4320 #endif /* CONFIG_KEYS */
4325 * Audit requires a unique representation of each Smack specific
4326 * rule. This unique representation is used to distinguish the
4327 * object to be audited from remaining kernel objects and also
4328 * works as a glue between the audit hooks.
4330 * Since repository entries are added but never deleted, we'll use
4331 * the smack_known label address related to the given audit rule as
4332 * the needed unique representation. This also better fits the smack
4333 * model where nearly everything is a label.
4338 * smack_audit_rule_init - Initialize a smack audit rule
4339 * @field: audit rule fields given from user-space (audit.h)
4340 * @op: required testing operator (=, !=, >, <, ...)
4341 * @rulestr: smack label to be audited
4342 * @vrule: pointer to save our own audit rule representation
4344 * Prepare to audit cases where (@field @op @rulestr) is true.
4345 * The label to be audited is created if necessay.
4347 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4349 struct smack_known *skp;
4350 char **rule = (char **)vrule;
4353 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4356 if (op != Audit_equal && op != Audit_not_equal)
4359 skp = smk_import_entry(rulestr, 0);
4361 return PTR_ERR(skp);
4363 *rule = skp->smk_known;
4369 * smack_audit_rule_known - Distinguish Smack audit rules
4370 * @krule: rule of interest, in Audit kernel representation format
4372 * This is used to filter Smack rules from remaining Audit ones.
4373 * If it's proved that this rule belongs to us, the
4374 * audit_rule_match hook will be called to do the final judgement.
4376 static int smack_audit_rule_known(struct audit_krule *krule)
4378 struct audit_field *f;
4381 for (i = 0; i < krule->field_count; i++) {
4382 f = &krule->fields[i];
4384 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4392 * smack_audit_rule_match - Audit given object ?
4393 * @secid: security id for identifying the object to test
4394 * @field: audit rule flags given from user-space
4395 * @op: required testing operator
4396 * @vrule: smack internal rule presentation
4397 * @actx: audit context associated with the check
4399 * The core Audit hook. It's used to take the decision of
4400 * whether to audit or not to audit a given object.
4402 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4403 struct audit_context *actx)
4405 struct smack_known *skp;
4408 if (unlikely(!rule)) {
4409 WARN_ONCE(1, "Smack: missing rule\n");
4413 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4416 skp = smack_from_secid(secid);
4419 * No need to do string comparisons. If a match occurs,
4420 * both pointers will point to the same smack_known
4423 if (op == Audit_equal)
4424 return (rule == skp->smk_known);
4425 if (op == Audit_not_equal)
4426 return (rule != skp->smk_known);
4432 * smack_audit_rule_free - free smack rule representation
4433 * @vrule: rule to be freed.
4435 * No memory was allocated.
4437 static void smack_audit_rule_free(void *vrule)
4442 #endif /* CONFIG_AUDIT */
4445 * smack_ismaclabel - check if xattr @name references a smack MAC label
4446 * @name: Full xattr name to check.
4448 static int smack_ismaclabel(const char *name)
4450 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4455 * smack_secid_to_secctx - return the smack label for a secid
4456 * @secid: incoming integer
4457 * @secdata: destination
4458 * @seclen: how long it is
4460 * Exists for networking code.
4462 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4464 struct smack_known *skp = smack_from_secid(secid);
4467 *secdata = skp->smk_known;
4468 *seclen = strlen(skp->smk_known);
4473 * smack_secctx_to_secid - return the secid for a smack label
4474 * @secdata: smack label
4475 * @seclen: how long result is
4476 * @secid: outgoing integer
4478 * Exists for audit and networking code.
4480 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4482 struct smack_known *skp = smk_find_entry(secdata);
4485 *secid = skp->smk_secid;
4492 * smack_release_secctx - don't do anything.
4496 * Exists to make sure nothing gets done, and properly
4498 static void smack_release_secctx(char *secdata, u32 seclen)
4502 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4504 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4507 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4509 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4512 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4515 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4523 static struct security_hook_list smack_hooks[] = {
4524 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4525 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4526 LSM_HOOK_INIT(syslog, smack_syslog),
4528 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4529 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4530 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4531 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4532 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4533 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4534 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
4536 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4537 LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
4538 LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
4540 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4541 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4542 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4543 LSM_HOOK_INIT(inode_link, smack_inode_link),
4544 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4545 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4546 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4547 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4548 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4549 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4550 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4551 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4552 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4553 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4554 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4555 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4556 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4557 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4559 LSM_HOOK_INIT(file_permission, smack_file_permission),
4560 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4561 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4562 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4563 LSM_HOOK_INIT(file_lock, smack_file_lock),
4564 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4565 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4566 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4567 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4568 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4569 LSM_HOOK_INIT(file_receive, smack_file_receive),
4571 LSM_HOOK_INIT(file_open, smack_file_open),
4573 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4574 LSM_HOOK_INIT(cred_free, smack_cred_free),
4575 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4576 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4577 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4578 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4579 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4580 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4581 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4582 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4583 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4584 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4585 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4586 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4587 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4588 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4589 LSM_HOOK_INIT(task_kill, smack_task_kill),
4590 LSM_HOOK_INIT(task_wait, smack_task_wait),
4591 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4593 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4594 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4596 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4597 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4599 LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security),
4600 LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security),
4601 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4602 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4603 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4604 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4606 LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security),
4607 LSM_HOOK_INIT(shm_free_security, smack_shm_free_security),
4608 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4609 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4610 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4612 LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security),
4613 LSM_HOOK_INIT(sem_free_security, smack_sem_free_security),
4614 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4615 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4616 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4618 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4620 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4621 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4623 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4624 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4626 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4627 #ifdef SMACK_IPV6_PORT_LABELING
4628 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4630 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4631 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4632 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4633 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4634 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4635 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4636 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4637 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4638 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4639 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4641 /* key management security hooks */
4643 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4644 LSM_HOOK_INIT(key_free, smack_key_free),
4645 LSM_HOOK_INIT(key_permission, smack_key_permission),
4646 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4647 #endif /* CONFIG_KEYS */
4651 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4652 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4653 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4654 LSM_HOOK_INIT(audit_rule_free, smack_audit_rule_free),
4655 #endif /* CONFIG_AUDIT */
4657 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4658 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4659 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4660 LSM_HOOK_INIT(release_secctx, smack_release_secctx),
4661 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4662 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4663 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4667 static __init void init_smack_known_list(void)
4670 * Initialize rule list locks
4672 mutex_init(&smack_known_huh.smk_rules_lock);
4673 mutex_init(&smack_known_hat.smk_rules_lock);
4674 mutex_init(&smack_known_floor.smk_rules_lock);
4675 mutex_init(&smack_known_star.smk_rules_lock);
4676 mutex_init(&smack_known_invalid.smk_rules_lock);
4677 mutex_init(&smack_known_web.smk_rules_lock);
4679 * Initialize rule lists
4681 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4682 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4683 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4684 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4685 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4686 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4688 * Create the known labels list
4690 smk_insert_entry(&smack_known_huh);
4691 smk_insert_entry(&smack_known_hat);
4692 smk_insert_entry(&smack_known_star);
4693 smk_insert_entry(&smack_known_floor);
4694 smk_insert_entry(&smack_known_invalid);
4695 smk_insert_entry(&smack_known_web);
4699 * smack_init - initialize the smack system
4703 static __init int smack_init(void)
4706 struct task_smack *tsp;
4708 if (!security_module_enable("smack"))
4713 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4714 if (!smack_inode_cache)
4717 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4720 kmem_cache_destroy(smack_inode_cache);
4724 pr_info("Smack: Initializing.\n");
4725 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4726 pr_info("Smack: Netfilter enabled.\n");
4728 #ifdef SMACK_IPV6_PORT_LABELING
4729 pr_info("Smack: IPv6 port labeling enabled.\n");
4731 #ifdef SMACK_IPV6_SECMARK_LABELING
4732 pr_info("Smack: IPv6 Netfilter enabled.\n");
4736 * Set the security state for the initial task.
4738 cred = (struct cred *) current->cred;
4739 cred->security = tsp;
4741 /* initialize the smack_known_list */
4742 init_smack_known_list();
4747 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks));
4753 * Smack requires early initialization in order to label
4754 * all processes and objects when they are created.
4756 security_initcall(smack_init);