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>
46 #define task_security(task) (task_cred_xxx((task), security))
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
56 LIST_HEAD(smk_ipv6_port_list);
57 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
58 static struct kmem_cache *smack_inode_cache;
61 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
64 static char *smk_bu_mess[] = {
65 "Bringup Error", /* Unused */
66 "Bringup", /* SMACK_BRINGUP_ALLOW */
67 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
68 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
72 static void smk_bu_mode(int mode, char *s)
82 if (mode & MAY_APPEND)
84 if (mode & MAY_TRANSMUTE)
94 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
95 static int smk_bu_note(char *note, struct smack_known *sskp, char *osp,
98 char acc[SMK_NUM_ACCESS_TYPE + 1];
102 if (rc > SMACK_UNCONFINED_OBJECT)
105 smk_bu_mode(mode, acc);
107 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
108 sskp->smk_known, osp, acc, note);
113 #define smk_bu_note(note, sskp, osp, mode, RC) (RC)
116 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
117 static int smk_bu_current(char *note, char *osp, int mode, int rc)
120 struct task_smack *tsp = current_security();
122 char acc[SMK_NUM_ACCESS_TYPE + 1];
126 if (rc > SMACK_UNCONFINED_OBJECT)
129 smk_bu_mode(mode, acc);
131 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
132 tsp->smk_task->smk_known, osp, acc, current->comm, note);
137 #define smk_bu_current(note, osp, mode, RC) (RC)
140 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
141 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
144 struct task_smack *tsp = current_security();
145 struct task_smack *otsp = task_security(otp);
147 char acc[SMK_NUM_ACCESS_TYPE + 1];
151 if (rc > SMACK_UNCONFINED_OBJECT)
154 smk_bu_mode(mode, acc);
156 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
157 tsp->smk_task->smk_known, otsp->smk_task->smk_known, acc,
158 current->comm, otp->comm);
163 #define smk_bu_task(otp, mode, RC) (RC)
166 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
167 static int smk_bu_inode(struct inode *inode, int mode, int rc)
170 struct task_smack *tsp = current_security();
172 struct inode_smack *isp = inode->i_security;
173 char acc[SMK_NUM_ACCESS_TYPE + 1];
176 if (isp->smk_flags & SMK_INODE_IMPURE)
177 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
178 inode->i_sb->s_id, inode->i_ino, current->comm);
183 if (rc > SMACK_UNCONFINED_OBJECT)
185 if (rc == SMACK_UNCONFINED_SUBJECT &&
186 (mode & (MAY_WRITE | MAY_APPEND)))
187 isp->smk_flags |= SMK_INODE_IMPURE;
189 smk_bu_mode(mode, acc);
191 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
192 tsp->smk_task->smk_known, smk_of_inode(inode), acc,
193 inode->i_sb->s_id, inode->i_ino, current->comm);
198 #define smk_bu_inode(inode, mode, RC) (RC)
201 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
202 static int smk_bu_file(struct file *file, int mode, int rc)
205 struct task_smack *tsp = current_security();
206 struct smack_known *sskp = tsp->smk_task;
208 struct inode *inode = file->f_mapping->host;
209 struct inode_smack *isp = inode->i_security;
210 char acc[SMK_NUM_ACCESS_TYPE + 1];
212 if (isp->smk_flags & SMK_INODE_IMPURE)
213 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
214 inode->i_sb->s_id, inode->i_ino, current->comm);
218 if (rc > SMACK_UNCONFINED_OBJECT)
221 smk_bu_mode(mode, acc);
223 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
224 sskp->smk_known, (char *)file->f_security, acc,
225 inode->i_sb->s_id, inode->i_ino, file->f_dentry->d_name.name,
231 #define smk_bu_file(file, mode, RC) (RC)
234 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
235 #define smk_bu_credfile(cred, file, mode, RC) (RC)
239 * smk_fetch - Fetch the smack label from a file.
240 * @ip: a pointer to the inode
241 * @dp: a pointer to the dentry
243 * Returns a pointer to the master list entry for the Smack label
244 * or NULL if there was no label to fetch.
246 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
251 struct smack_known *skp = NULL;
253 if (ip->i_op->getxattr == NULL)
256 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
260 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
262 skp = smk_import_entry(buffer, rc);
270 * new_inode_smack - allocate an inode security blob
271 * @smack: a pointer to the Smack label to use in the blob
273 * Returns the new blob or NULL if there's no memory available
275 struct inode_smack *new_inode_smack(char *smack)
277 struct inode_smack *isp;
279 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
283 isp->smk_inode = smack;
285 mutex_init(&isp->smk_lock);
291 * new_task_smack - allocate a task security blob
292 * @smack: a pointer to the Smack label to use in the blob
294 * Returns the new blob or NULL if there's no memory available
296 static struct task_smack *new_task_smack(struct smack_known *task,
297 struct smack_known *forked, gfp_t gfp)
299 struct task_smack *tsp;
301 tsp = kzalloc(sizeof(struct task_smack), gfp);
305 tsp->smk_task = task;
306 tsp->smk_forked = forked;
307 INIT_LIST_HEAD(&tsp->smk_rules);
308 INIT_LIST_HEAD(&tsp->smk_relabel);
309 mutex_init(&tsp->smk_rules_lock);
315 * smk_copy_rules - copy a rule set
316 * @nhead - new rules header pointer
317 * @ohead - old rules header pointer
319 * Returns 0 on success, -ENOMEM on error
321 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
324 struct smack_rule *nrp;
325 struct smack_rule *orp;
328 INIT_LIST_HEAD(nhead);
330 list_for_each_entry_rcu(orp, ohead, list) {
331 nrp = kzalloc(sizeof(struct smack_rule), gfp);
337 list_add_rcu(&nrp->list, nhead);
343 * smk_copy_relabel - copy smk_relabel labels list
344 * @nhead: new rules header pointer
345 * @ohead: old rules header pointer
346 * @gfp: type of the memory for the allocation
348 * Returns 0 on success, -ENOMEM on error
350 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
353 struct smack_known_list_elem *nklep;
354 struct smack_known_list_elem *oklep;
356 INIT_LIST_HEAD(nhead);
358 list_for_each_entry(oklep, ohead, list) {
359 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
361 smk_destroy_label_list(nhead);
364 nklep->smk_label = oklep->smk_label;
365 list_add(&nklep->list, nhead);
372 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
373 * @mode - input mode in form of PTRACE_MODE_*
375 * Returns a converted MAY_* mode usable by smack rules
377 static inline unsigned int smk_ptrace_mode(unsigned int mode)
380 case PTRACE_MODE_READ:
382 case PTRACE_MODE_ATTACH:
383 return MAY_READWRITE;
390 * smk_ptrace_rule_check - helper for ptrace access
391 * @tracer: tracer process
392 * @tracee_label: label of the process that's about to be traced,
393 * the pointer must originate from smack structures
394 * @mode: ptrace attachment mode (PTRACE_MODE_*)
395 * @func: name of the function that called us, used for audit
397 * Returns 0 on access granted, -error on error
399 static int smk_ptrace_rule_check(struct task_struct *tracer, char *tracee_label,
400 unsigned int mode, const char *func)
403 struct smk_audit_info ad, *saip = NULL;
404 struct task_smack *tsp;
405 struct smack_known *skp;
407 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
408 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
409 smk_ad_setfield_u_tsk(&ad, tracer);
413 tsp = task_security(tracer);
414 skp = smk_of_task(tsp);
416 if ((mode & PTRACE_MODE_ATTACH) &&
417 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
418 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
419 if (skp->smk_known == tracee_label)
421 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
423 else if (capable(CAP_SYS_PTRACE))
429 smack_log(skp->smk_known, tracee_label, 0, rc, saip);
434 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
435 rc = smk_tskacc(tsp, tracee_label, smk_ptrace_mode(mode), saip);
441 * We he, that is fun!
445 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
446 * @ctp: child task pointer
447 * @mode: ptrace attachment mode (PTRACE_MODE_*)
449 * Returns 0 if access is OK, an error code otherwise
451 * Do the capability checks.
453 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
456 struct smack_known *skp;
458 rc = cap_ptrace_access_check(ctp, mode);
462 skp = smk_of_task(task_security(ctp));
464 rc = smk_ptrace_rule_check(current, skp->smk_known, mode, __func__);
469 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
470 * @ptp: parent task pointer
472 * Returns 0 if access is OK, an error code otherwise
474 * Do the capability checks, and require PTRACE_MODE_ATTACH.
476 static int smack_ptrace_traceme(struct task_struct *ptp)
479 struct smack_known *skp;
481 rc = cap_ptrace_traceme(ptp);
485 skp = smk_of_task(current_security());
487 rc = smk_ptrace_rule_check(ptp, skp->smk_known,
488 PTRACE_MODE_ATTACH, __func__);
494 * smack_syslog - Smack approval on syslog
495 * @type: message type
497 * Require that the task has the floor label
499 * Returns 0 on success, error code otherwise.
501 static int smack_syslog(int typefrom_file)
504 struct smack_known *skp = smk_of_current();
506 if (smack_privileged(CAP_MAC_OVERRIDE))
509 if (skp != &smack_known_floor)
521 * smack_sb_alloc_security - allocate a superblock blob
522 * @sb: the superblock getting the blob
524 * Returns 0 on success or -ENOMEM on error.
526 static int smack_sb_alloc_security(struct super_block *sb)
528 struct superblock_smack *sbsp;
530 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
535 sbsp->smk_root = smack_known_floor.smk_known;
536 sbsp->smk_default = smack_known_floor.smk_known;
537 sbsp->smk_floor = smack_known_floor.smk_known;
538 sbsp->smk_hat = smack_known_hat.smk_known;
540 * smk_initialized will be zero from kzalloc.
542 sb->s_security = sbsp;
548 * smack_sb_free_security - free a superblock blob
549 * @sb: the superblock getting the blob
552 static void smack_sb_free_security(struct super_block *sb)
554 kfree(sb->s_security);
555 sb->s_security = NULL;
559 * smack_sb_copy_data - copy mount options data for processing
560 * @orig: where to start
561 * @smackopts: mount options string
563 * Returns 0 on success or -ENOMEM on error.
565 * Copy the Smack specific mount options out of the mount
568 static int smack_sb_copy_data(char *orig, char *smackopts)
570 char *cp, *commap, *otheropts, *dp;
572 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
573 if (otheropts == NULL)
576 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
577 if (strstr(cp, SMK_FSDEFAULT) == cp)
579 else if (strstr(cp, SMK_FSFLOOR) == cp)
581 else if (strstr(cp, SMK_FSHAT) == cp)
583 else if (strstr(cp, SMK_FSROOT) == cp)
585 else if (strstr(cp, SMK_FSTRANS) == cp)
590 commap = strchr(cp, ',');
599 strcpy(orig, otheropts);
600 free_page((unsigned long)otheropts);
606 * smack_sb_kern_mount - Smack specific mount processing
607 * @sb: the file system superblock
608 * @flags: the mount flags
609 * @data: the smack mount options
611 * Returns 0 on success, an error code on failure
613 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
615 struct dentry *root = sb->s_root;
616 struct inode *inode = root->d_inode;
617 struct superblock_smack *sp = sb->s_security;
618 struct inode_smack *isp;
619 struct smack_known *skp;
627 if (sp->smk_initialized)
630 sp->smk_initialized = 1;
632 for (op = data; op != NULL; op = commap) {
633 commap = strchr(op, ',');
637 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
638 op += strlen(SMK_FSHAT);
639 nsp = smk_import(op, 0);
644 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
645 op += strlen(SMK_FSFLOOR);
646 nsp = smk_import(op, 0);
651 } else if (strncmp(op, SMK_FSDEFAULT,
652 strlen(SMK_FSDEFAULT)) == 0) {
653 op += strlen(SMK_FSDEFAULT);
654 nsp = smk_import(op, 0);
656 sp->smk_default = nsp;
659 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
660 op += strlen(SMK_FSROOT);
661 nsp = smk_import(op, 0);
666 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
667 op += strlen(SMK_FSTRANS);
668 nsp = smk_import(op, 0);
677 if (!smack_privileged(CAP_MAC_ADMIN)) {
679 * Unprivileged mounts don't get to specify Smack values.
684 * Unprivileged mounts get root and default from the caller.
686 skp = smk_of_current();
687 sp->smk_root = skp->smk_known;
688 sp->smk_default = skp->smk_known;
691 * Initialize the root inode.
693 isp = inode->i_security;
695 isp = new_inode_smack(sp->smk_root);
698 inode->i_security = isp;
700 isp->smk_inode = sp->smk_root;
703 if (inode->i_op && inode->i_op->setxattr) {
704 rc = inode->i_op->setxattr(root,
705 XATTR_NAME_SMACKTRANSMUTE, TRANS_TRUE,
708 isp->smk_flags |= SMK_INODE_TRANSMUTE;
716 * smack_sb_statfs - Smack check on statfs
717 * @dentry: identifies the file system in question
719 * Returns 0 if current can read the floor of the filesystem,
720 * and error code otherwise
722 static int smack_sb_statfs(struct dentry *dentry)
724 struct superblock_smack *sbp = dentry->d_sb->s_security;
726 struct smk_audit_info ad;
728 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
729 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
731 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
732 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
741 * smack_bprm_set_creds - set creds for exec
742 * @bprm: the exec information
744 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
746 static int smack_bprm_set_creds(struct linux_binprm *bprm)
748 struct inode *inode = file_inode(bprm->file);
749 struct task_smack *bsp = bprm->cred->security;
750 struct inode_smack *isp;
753 rc = cap_bprm_set_creds(bprm);
757 if (bprm->cred_prepared)
760 isp = inode->i_security;
761 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
764 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
765 struct task_struct *tracer;
769 tracer = ptrace_parent(current);
770 if (likely(tracer != NULL))
771 rc = smk_ptrace_rule_check(tracer,
772 isp->smk_task->smk_known,
779 } else if (bprm->unsafe)
782 bsp->smk_task = isp->smk_task;
783 bprm->per_clear |= PER_CLEAR_ON_SETID;
789 * smack_bprm_committing_creds - Prepare to install the new credentials
792 * @bprm: binprm for exec
794 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
796 struct task_smack *bsp = bprm->cred->security;
798 if (bsp->smk_task != bsp->smk_forked)
799 current->pdeath_signal = 0;
803 * smack_bprm_secureexec - Return the decision to use secureexec.
804 * @bprm: binprm for exec
806 * Returns 0 on success.
808 static int smack_bprm_secureexec(struct linux_binprm *bprm)
810 struct task_smack *tsp = current_security();
811 int ret = cap_bprm_secureexec(bprm);
813 if (!ret && (tsp->smk_task != tsp->smk_forked))
824 * smack_inode_alloc_security - allocate an inode blob
825 * @inode: the inode in need of a blob
827 * Returns 0 if it gets a blob, -ENOMEM otherwise
829 static int smack_inode_alloc_security(struct inode *inode)
831 struct smack_known *skp = smk_of_current();
833 inode->i_security = new_inode_smack(skp->smk_known);
834 if (inode->i_security == NULL)
840 * smack_inode_free_security - free an inode blob
841 * @inode: the inode with a blob
843 * Clears the blob pointer in inode
845 static void smack_inode_free_security(struct inode *inode)
847 kmem_cache_free(smack_inode_cache, inode->i_security);
848 inode->i_security = NULL;
852 * smack_inode_init_security - copy out the smack from an inode
856 * @name: where to put the attribute name
857 * @value: where to put the attribute value
858 * @len: where to put the length of the attribute
860 * Returns 0 if it all works out, -ENOMEM if there's no memory
862 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
863 const struct qstr *qstr, char **name,
864 void **value, size_t *len)
866 struct inode_smack *issp = inode->i_security;
867 struct smack_known *skp = smk_of_current();
868 char *isp = smk_of_inode(inode);
869 char *dsp = smk_of_inode(dir);
873 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_NOFS);
880 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
884 * If the access rule allows transmutation and
885 * the directory requests transmutation then
886 * by all means transmute.
887 * Mark the inode as changed.
889 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
890 smk_inode_transmutable(dir)) {
892 issp->smk_flags |= SMK_INODE_CHANGED;
895 *value = kstrdup(isp, GFP_NOFS);
901 *len = strlen(isp) + 1;
907 * smack_inode_link - Smack check on link
908 * @old_dentry: the existing object
910 * @new_dentry: the new object
912 * Returns 0 if access is permitted, an error code otherwise
914 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
915 struct dentry *new_dentry)
918 struct smk_audit_info ad;
921 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
922 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
924 isp = smk_of_inode(old_dentry->d_inode);
925 rc = smk_curacc(isp, MAY_WRITE, &ad);
926 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
928 if (rc == 0 && new_dentry->d_inode != NULL) {
929 isp = smk_of_inode(new_dentry->d_inode);
930 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
931 rc = smk_curacc(isp, MAY_WRITE, &ad);
932 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
939 * smack_inode_unlink - Smack check on inode deletion
940 * @dir: containing directory object
941 * @dentry: file to unlink
943 * Returns 0 if current can write the containing directory
944 * and the object, error code otherwise
946 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
948 struct inode *ip = dentry->d_inode;
949 struct smk_audit_info ad;
952 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
953 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
956 * You need write access to the thing you're unlinking
958 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
959 rc = smk_bu_inode(ip, MAY_WRITE, rc);
962 * You also need write access to the containing directory
964 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
965 smk_ad_setfield_u_fs_inode(&ad, dir);
966 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
967 rc = smk_bu_inode(dir, MAY_WRITE, rc);
973 * smack_inode_rmdir - Smack check on directory deletion
974 * @dir: containing directory object
975 * @dentry: directory to unlink
977 * Returns 0 if current can write the containing directory
978 * and the directory, error code otherwise
980 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
982 struct smk_audit_info ad;
985 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
986 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
989 * You need write access to the thing you're removing
991 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
992 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
995 * You also need write access to the containing directory
997 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
998 smk_ad_setfield_u_fs_inode(&ad, dir);
999 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1000 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1007 * smack_inode_rename - Smack check on rename
1008 * @old_inode: the old directory
1009 * @old_dentry: unused
1010 * @new_inode: the new directory
1011 * @new_dentry: unused
1013 * Read and write access is required on both the old and
1016 * Returns 0 if access is permitted, an error code otherwise
1018 static int smack_inode_rename(struct inode *old_inode,
1019 struct dentry *old_dentry,
1020 struct inode *new_inode,
1021 struct dentry *new_dentry)
1025 struct smk_audit_info ad;
1027 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1028 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1030 isp = smk_of_inode(old_dentry->d_inode);
1031 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1032 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
1034 if (rc == 0 && new_dentry->d_inode != NULL) {
1035 isp = smk_of_inode(new_dentry->d_inode);
1036 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1037 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1038 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
1044 * smack_inode_permission - Smack version of permission()
1045 * @inode: the inode in question
1046 * @mask: the access requested
1048 * This is the important Smack hook.
1050 * Returns 0 if access is permitted, -EACCES otherwise
1052 static int smack_inode_permission(struct inode *inode, int mask)
1054 struct smk_audit_info ad;
1055 int no_block = mask & MAY_NOT_BLOCK;
1058 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1060 * No permission to check. Existence test. Yup, it's there.
1065 /* May be droppable after audit */
1068 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1069 smk_ad_setfield_u_fs_inode(&ad, inode);
1070 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1071 rc = smk_bu_inode(inode, mask, rc);
1076 * smack_inode_setattr - Smack check for setting attributes
1077 * @dentry: the object
1078 * @iattr: for the force flag
1080 * Returns 0 if access is permitted, an error code otherwise
1082 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1084 struct smk_audit_info ad;
1088 * Need to allow for clearing the setuid bit.
1090 if (iattr->ia_valid & ATTR_FORCE)
1092 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1093 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1095 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1096 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1101 * smack_inode_getattr - Smack check for getting attributes
1103 * @dentry: the object
1105 * Returns 0 if access is permitted, an error code otherwise
1107 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1109 struct smk_audit_info ad;
1113 path.dentry = dentry;
1116 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1117 smk_ad_setfield_u_fs_path(&ad, path);
1118 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1119 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1124 * smack_inode_setxattr - Smack check for setting xattrs
1125 * @dentry: the object
1126 * @name: name of the attribute
1131 * This protects the Smack attribute explicitly.
1133 * Returns 0 if access is permitted, an error code otherwise
1135 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1136 const void *value, size_t size, int flags)
1138 struct smk_audit_info ad;
1141 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1142 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1143 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1144 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1145 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1146 if (!smack_privileged(CAP_MAC_ADMIN))
1149 * check label validity here so import wont fail on
1152 if (size == 0 || size >= SMK_LONGLABEL ||
1153 smk_import(value, size) == NULL)
1155 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1156 if (!smack_privileged(CAP_MAC_ADMIN))
1158 if (size != TRANS_TRUE_SIZE ||
1159 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1162 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1164 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1165 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1168 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1169 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1176 * smack_inode_post_setxattr - Apply the Smack update approved above
1178 * @name: attribute name
1179 * @value: attribute value
1180 * @size: attribute size
1183 * Set the pointer in the inode blob to the entry found
1184 * in the master label list.
1186 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1187 const void *value, size_t size, int flags)
1189 struct smack_known *skp;
1190 struct inode_smack *isp = dentry->d_inode->i_security;
1192 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1193 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1197 skp = smk_import_entry(value, size);
1198 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1200 isp->smk_inode = skp->smk_known;
1202 isp->smk_inode = smack_known_invalid.smk_known;
1203 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1205 isp->smk_task = skp;
1207 isp->smk_task = &smack_known_invalid;
1208 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1210 isp->smk_mmap = skp;
1212 isp->smk_mmap = &smack_known_invalid;
1219 * smack_inode_getxattr - Smack check on getxattr
1220 * @dentry: the object
1223 * Returns 0 if access is permitted, an error code otherwise
1225 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1227 struct smk_audit_info ad;
1230 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1231 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1233 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1234 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1239 * smack_inode_removexattr - Smack check on removexattr
1240 * @dentry: the object
1241 * @name: name of the attribute
1243 * Removing the Smack attribute requires CAP_MAC_ADMIN
1245 * Returns 0 if access is permitted, an error code otherwise
1247 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1249 struct inode_smack *isp;
1250 struct smk_audit_info ad;
1253 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1254 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1255 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1256 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1257 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1258 strcmp(name, XATTR_NAME_SMACKMMAP)) {
1259 if (!smack_privileged(CAP_MAC_ADMIN))
1262 rc = cap_inode_removexattr(dentry, name);
1267 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1268 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1270 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1271 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1275 isp = dentry->d_inode->i_security;
1277 * Don't do anything special for these.
1278 * XATTR_NAME_SMACKIPIN
1279 * XATTR_NAME_SMACKIPOUT
1280 * XATTR_NAME_SMACKEXEC
1282 if (strcmp(name, XATTR_NAME_SMACK) == 0)
1283 isp->smk_task = NULL;
1284 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1285 isp->smk_mmap = NULL;
1286 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1287 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1293 * smack_inode_getsecurity - get smack xattrs
1294 * @inode: the object
1295 * @name: attribute name
1296 * @buffer: where to put the result
1299 * Returns the size of the attribute or an error code
1301 static int smack_inode_getsecurity(const struct inode *inode,
1302 const char *name, void **buffer,
1305 struct socket_smack *ssp;
1306 struct socket *sock;
1307 struct super_block *sbp;
1308 struct inode *ip = (struct inode *)inode;
1313 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1314 isp = smk_of_inode(inode);
1315 ilen = strlen(isp) + 1;
1321 * The rest of the Smack xattrs are only on sockets.
1324 if (sbp->s_magic != SOCKFS_MAGIC)
1327 sock = SOCKET_I(ip);
1328 if (sock == NULL || sock->sk == NULL)
1331 ssp = sock->sk->sk_security;
1333 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1334 isp = ssp->smk_in->smk_known;
1335 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1336 isp = ssp->smk_out->smk_known;
1340 ilen = strlen(isp) + 1;
1351 * smack_inode_listsecurity - list the Smack attributes
1352 * @inode: the object
1353 * @buffer: where they go
1354 * @buffer_size: size of buffer
1356 * Returns 0 on success, -EINVAL otherwise
1358 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1361 int len = strlen(XATTR_NAME_SMACK);
1363 if (buffer != NULL && len <= buffer_size) {
1364 memcpy(buffer, XATTR_NAME_SMACK, len);
1371 * smack_inode_getsecid - Extract inode's security id
1372 * @inode: inode to extract the info from
1373 * @secid: where result will be saved
1375 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1377 struct inode_smack *isp = inode->i_security;
1379 *secid = smack_to_secid(isp->smk_inode);
1387 * smack_file_permission - Smack check on file operations
1393 * Should access checks be done on each read or write?
1394 * UNICOS and SELinux say yes.
1395 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1397 * I'll say no for now. Smack does not do the frequent
1398 * label changing that SELinux does.
1400 static int smack_file_permission(struct file *file, int mask)
1406 * smack_file_alloc_security - assign a file security blob
1409 * The security blob for a file is a pointer to the master
1410 * label list, so no allocation is done.
1414 static int smack_file_alloc_security(struct file *file)
1416 struct smack_known *skp = smk_of_current();
1418 file->f_security = skp->smk_known;
1423 * smack_file_free_security - clear a file security blob
1426 * The security blob for a file is a pointer to the master
1427 * label list, so no memory is freed.
1429 static void smack_file_free_security(struct file *file)
1431 file->f_security = NULL;
1435 * smack_file_ioctl - Smack check on ioctls
1440 * Relies heavily on the correct use of the ioctl command conventions.
1442 * Returns 0 if allowed, error code otherwise
1444 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1448 struct smk_audit_info ad;
1450 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1451 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1453 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1454 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1455 rc = smk_bu_file(file, MAY_WRITE, rc);
1458 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1459 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1460 rc = smk_bu_file(file, MAY_READ, rc);
1467 * smack_file_lock - Smack check on file locking
1471 * Returns 0 if current has lock access, error code otherwise
1473 static int smack_file_lock(struct file *file, unsigned int cmd)
1475 struct smk_audit_info ad;
1478 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1479 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1480 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1481 rc = smk_bu_file(file, MAY_LOCK, rc);
1486 * smack_file_fcntl - Smack check on fcntl
1488 * @cmd: what action to check
1491 * Generally these operations are harmless.
1492 * File locking operations present an obvious mechanism
1493 * for passing information, so they require write access.
1495 * Returns 0 if current has access, error code otherwise
1497 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1500 struct smk_audit_info ad;
1509 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1510 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1511 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1512 rc = smk_bu_file(file, MAY_LOCK, rc);
1516 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1517 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1518 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1519 rc = smk_bu_file(file, MAY_WRITE, rc);
1530 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1531 * if mapping anonymous memory.
1532 * @file contains the file structure for file to map (may be NULL).
1533 * @reqprot contains the protection requested by the application.
1534 * @prot contains the protection that will be applied by the kernel.
1535 * @flags contains the operational flags.
1536 * Return 0 if permission is granted.
1538 static int smack_mmap_file(struct file *file,
1539 unsigned long reqprot, unsigned long prot,
1540 unsigned long flags)
1542 struct smack_known *skp;
1543 struct smack_known *mkp;
1544 struct smack_rule *srp;
1545 struct task_smack *tsp;
1547 struct inode_smack *isp;
1556 isp = file_inode(file)->i_security;
1557 if (isp->smk_mmap == NULL)
1559 mkp = isp->smk_mmap;
1561 tsp = current_security();
1562 skp = smk_of_current();
1567 * For each Smack rule associated with the subject
1568 * label verify that the SMACK64MMAP also has access
1569 * to that rule's object label.
1571 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1572 osmack = srp->smk_object;
1574 * Matching labels always allows access.
1576 if (mkp->smk_known == osmack)
1579 * If there is a matching local rule take
1580 * that into account as well.
1582 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1585 may = srp->smk_access;
1587 may &= srp->smk_access;
1589 * If may is zero the SMACK64MMAP subject can't
1590 * possibly have less access.
1596 * Fetch the global list entry.
1597 * If there isn't one a SMACK64MMAP subject
1598 * can't have as much access as current.
1600 mmay = smk_access_entry(mkp->smk_known, osmack,
1602 if (mmay == -ENOENT) {
1607 * If there is a local entry it modifies the
1608 * potential access, too.
1610 tmay = smk_access_entry(mkp->smk_known, osmack,
1612 if (tmay != -ENOENT)
1616 * If there is any access available to current that is
1617 * not available to a SMACK64MMAP subject
1620 if ((may | mmay) != mmay) {
1632 * smack_file_set_fowner - set the file security blob value
1633 * @file: object in question
1636 * Further research may be required on this one.
1638 static int smack_file_set_fowner(struct file *file)
1640 struct smack_known *skp = smk_of_current();
1642 file->f_security = skp->smk_known;
1647 * smack_file_send_sigiotask - Smack on sigio
1648 * @tsk: The target task
1649 * @fown: the object the signal come from
1652 * Allow a privileged task to get signals even if it shouldn't
1654 * Returns 0 if a subject with the object's smack could
1655 * write to the task, an error code otherwise.
1657 static int smack_file_send_sigiotask(struct task_struct *tsk,
1658 struct fown_struct *fown, int signum)
1660 struct smack_known *skp;
1661 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1664 struct smk_audit_info ad;
1667 * struct fown_struct is never outside the context of a struct file
1669 file = container_of(fown, struct file, f_owner);
1671 /* we don't log here as rc can be overriden */
1672 skp = smk_find_entry(file->f_security);
1673 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1674 rc = smk_bu_note("sigiotask", skp, tkp->smk_known, MAY_WRITE, rc);
1675 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1678 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1679 smk_ad_setfield_u_tsk(&ad, tsk);
1680 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1685 * smack_file_receive - Smack file receive check
1688 * Returns 0 if current has access, error code otherwise
1690 static int smack_file_receive(struct file *file)
1694 struct smk_audit_info ad;
1695 struct inode *inode = file_inode(file);
1696 struct socket *sock;
1697 struct task_smack *tsp;
1698 struct socket_smack *ssp;
1700 if (unlikely(IS_PRIVATE(file_inode(file))))
1703 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1704 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1706 if (S_ISSOCK(inode->i_mode)) {
1707 sock = SOCKET_I(inode);
1708 ssp = sock->sk->sk_security;
1709 tsp = current_security();
1711 * If the receiving process can't write to the
1712 * passed socket or if the passed socket can't
1713 * write to the receiving process don't accept
1714 * the passed socket.
1716 rc = smk_access(tsp->smk_task, ssp->smk_out->smk_known, MAY_WRITE, &ad);
1719 rc = smk_access(ssp->smk_in, tsp->smk_task->smk_known, MAY_WRITE, &ad);
1723 * This code relies on bitmasks.
1725 if (file->f_mode & FMODE_READ)
1727 if (file->f_mode & FMODE_WRITE)
1730 rc = smk_curacc(file->f_security, may, &ad);
1731 rc = smk_bu_file(file, may, rc);
1736 * smack_file_open - Smack dentry open processing
1740 * Set the security blob in the file structure.
1744 static int smack_file_open(struct file *file, const struct cred *cred)
1746 struct inode_smack *isp = file_inode(file)->i_security;
1748 file->f_security = isp->smk_inode;
1758 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1759 * @new: the new credentials
1760 * @gfp: the atomicity of any memory allocations
1762 * Prepare a blank set of credentials for modification. This must allocate all
1763 * the memory the LSM module might require such that cred_transfer() can
1764 * complete without error.
1766 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1768 struct task_smack *tsp;
1770 tsp = new_task_smack(NULL, NULL, gfp);
1774 cred->security = tsp;
1781 * smack_cred_free - "free" task-level security credentials
1782 * @cred: the credentials in question
1785 static void smack_cred_free(struct cred *cred)
1787 struct task_smack *tsp = cred->security;
1788 struct smack_rule *rp;
1789 struct list_head *l;
1790 struct list_head *n;
1794 cred->security = NULL;
1796 smk_destroy_label_list(&tsp->smk_relabel);
1798 list_for_each_safe(l, n, &tsp->smk_rules) {
1799 rp = list_entry(l, struct smack_rule, list);
1800 list_del(&rp->list);
1807 * smack_cred_prepare - prepare new set of credentials for modification
1808 * @new: the new credentials
1809 * @old: the original credentials
1810 * @gfp: the atomicity of any memory allocations
1812 * Prepare a new set of credentials for modification.
1814 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1817 struct task_smack *old_tsp = old->security;
1818 struct task_smack *new_tsp;
1821 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1822 if (new_tsp == NULL)
1825 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1829 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1834 new->security = new_tsp;
1839 * smack_cred_transfer - Transfer the old credentials to the new credentials
1840 * @new: the new credentials
1841 * @old: the original credentials
1843 * Fill in a set of blank credentials from another set of credentials.
1845 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1847 struct task_smack *old_tsp = old->security;
1848 struct task_smack *new_tsp = new->security;
1850 new_tsp->smk_task = old_tsp->smk_task;
1851 new_tsp->smk_forked = old_tsp->smk_task;
1852 mutex_init(&new_tsp->smk_rules_lock);
1853 INIT_LIST_HEAD(&new_tsp->smk_rules);
1856 /* cbs copy rule list */
1860 * smack_kernel_act_as - Set the subjective context in a set of credentials
1861 * @new: points to the set of credentials to be modified.
1862 * @secid: specifies the security ID to be set
1864 * Set the security data for a kernel service.
1866 static int smack_kernel_act_as(struct cred *new, u32 secid)
1868 struct task_smack *new_tsp = new->security;
1869 struct smack_known *skp = smack_from_secid(secid);
1874 new_tsp->smk_task = skp;
1879 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1880 * @new: points to the set of credentials to be modified
1881 * @inode: points to the inode to use as a reference
1883 * Set the file creation context in a set of credentials to the same
1884 * as the objective context of the specified inode
1886 static int smack_kernel_create_files_as(struct cred *new,
1887 struct inode *inode)
1889 struct inode_smack *isp = inode->i_security;
1890 struct task_smack *tsp = new->security;
1892 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1893 tsp->smk_task = tsp->smk_forked;
1898 * smk_curacc_on_task - helper to log task related access
1899 * @p: the task object
1900 * @access: the access requested
1901 * @caller: name of the calling function for audit
1903 * Return 0 if access is permitted
1905 static int smk_curacc_on_task(struct task_struct *p, int access,
1908 struct smk_audit_info ad;
1909 struct smack_known *skp = smk_of_task(task_security(p));
1912 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1913 smk_ad_setfield_u_tsk(&ad, p);
1914 rc = smk_curacc(skp->smk_known, access, &ad);
1915 rc = smk_bu_task(p, access, rc);
1920 * smack_task_setpgid - Smack check on setting pgid
1921 * @p: the task object
1924 * Return 0 if write access is permitted
1926 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1928 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1932 * smack_task_getpgid - Smack access check for getpgid
1933 * @p: the object task
1935 * Returns 0 if current can read the object task, error code otherwise
1937 static int smack_task_getpgid(struct task_struct *p)
1939 return smk_curacc_on_task(p, MAY_READ, __func__);
1943 * smack_task_getsid - Smack access check for getsid
1944 * @p: the object task
1946 * Returns 0 if current can read the object task, error code otherwise
1948 static int smack_task_getsid(struct task_struct *p)
1950 return smk_curacc_on_task(p, MAY_READ, __func__);
1954 * smack_task_getsecid - get the secid of the task
1955 * @p: the object task
1956 * @secid: where to put the result
1958 * Sets the secid to contain a u32 version of the smack label.
1960 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1962 struct smack_known *skp = smk_of_task(task_security(p));
1964 *secid = skp->smk_secid;
1968 * smack_task_setnice - Smack check on setting nice
1969 * @p: the task object
1972 * Return 0 if write access is permitted
1974 static int smack_task_setnice(struct task_struct *p, int nice)
1978 rc = cap_task_setnice(p, nice);
1980 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1985 * smack_task_setioprio - Smack check on setting ioprio
1986 * @p: the task object
1989 * Return 0 if write access is permitted
1991 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1995 rc = cap_task_setioprio(p, ioprio);
1997 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
2002 * smack_task_getioprio - Smack check on reading ioprio
2003 * @p: the task object
2005 * Return 0 if read access is permitted
2007 static int smack_task_getioprio(struct task_struct *p)
2009 return smk_curacc_on_task(p, MAY_READ, __func__);
2013 * smack_task_setscheduler - Smack check on setting scheduler
2014 * @p: the task object
2018 * Return 0 if read access is permitted
2020 static int smack_task_setscheduler(struct task_struct *p)
2024 rc = cap_task_setscheduler(p);
2026 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
2031 * smack_task_getscheduler - Smack check on reading scheduler
2032 * @p: the task object
2034 * Return 0 if read access is permitted
2036 static int smack_task_getscheduler(struct task_struct *p)
2038 return smk_curacc_on_task(p, MAY_READ, __func__);
2042 * smack_task_movememory - Smack check on moving memory
2043 * @p: the task object
2045 * Return 0 if write access is permitted
2047 static int smack_task_movememory(struct task_struct *p)
2049 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2053 * smack_task_kill - Smack check on signal delivery
2054 * @p: the task object
2057 * @secid: identifies the smack to use in lieu of current's
2059 * Return 0 if write access is permitted
2061 * The secid behavior is an artifact of an SELinux hack
2062 * in the USB code. Someday it may go away.
2064 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2067 struct smk_audit_info ad;
2068 struct smack_known *skp;
2069 struct smack_known *tkp = smk_of_task(task_security(p));
2072 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2073 smk_ad_setfield_u_tsk(&ad, p);
2075 * Sending a signal requires that the sender
2076 * can write the receiver.
2079 rc = smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
2080 rc = smk_bu_task(p, MAY_WRITE, rc);
2084 * If the secid isn't 0 we're dealing with some USB IO
2085 * specific behavior. This is not clean. For one thing
2086 * we can't take privilege into account.
2088 skp = smack_from_secid(secid);
2089 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
2090 rc = smk_bu_note("USB signal", skp, tkp->smk_known, MAY_WRITE, rc);
2095 * smack_task_wait - Smack access check for waiting
2096 * @p: task to wait for
2100 static int smack_task_wait(struct task_struct *p)
2103 * Allow the operation to succeed.
2105 * In userless environments (e.g. phones) programs
2106 * get marked with SMACK64EXEC and even if the parent
2107 * and child shouldn't be talking the parent still
2108 * may expect to know when the child exits.
2114 * smack_task_to_inode - copy task smack into the inode blob
2115 * @p: task to copy from
2116 * @inode: inode to copy to
2118 * Sets the smack pointer in the inode security blob
2120 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2122 struct inode_smack *isp = inode->i_security;
2123 struct smack_known *skp = smk_of_task(task_security(p));
2125 isp->smk_inode = skp->smk_known;
2133 * smack_sk_alloc_security - Allocate a socket blob
2136 * @gfp_flags: memory allocation flags
2138 * Assign Smack pointers to current
2140 * Returns 0 on success, -ENOMEM is there's no memory
2142 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2144 struct smack_known *skp = smk_of_current();
2145 struct socket_smack *ssp;
2147 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2152 * Sockets created by kernel threads receive web label.
2154 if (unlikely(current->flags & PF_KTHREAD)) {
2155 ssp->smk_in = &smack_known_web;
2156 ssp->smk_out = &smack_known_web;
2161 ssp->smk_packet = NULL;
2163 sk->sk_security = ssp;
2169 * smack_sk_free_security - Free a socket blob
2172 * Clears the blob pointer
2174 static void smack_sk_free_security(struct sock *sk)
2176 kfree(sk->sk_security);
2180 * smack_host_label - check host based restrictions
2181 * @sip: the object end
2183 * looks for host based access restrictions
2185 * This version will only be appropriate for really small sets of single label
2186 * hosts. The caller is responsible for ensuring that the RCU read lock is
2187 * taken before calling this function.
2189 * Returns the label of the far end or NULL if it's not special.
2191 static char *smack_host_label(struct sockaddr_in *sip)
2193 struct smk_netlbladdr *snp;
2194 struct in_addr *siap = &sip->sin_addr;
2196 if (siap->s_addr == 0)
2199 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2201 * we break after finding the first match because
2202 * the list is sorted from longest to shortest mask
2203 * so we have found the most specific match
2205 if ((&snp->smk_host.sin_addr)->s_addr ==
2206 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2207 /* we have found the special CIPSO option */
2208 if (snp->smk_label == smack_cipso_option)
2210 return snp->smk_label;
2217 * smack_netlabel - Set the secattr on a socket
2219 * @labeled: socket label scheme
2221 * Convert the outbound smack value (smk_out) to a
2222 * secattr and attach it to the socket.
2224 * Returns 0 on success or an error code
2226 static int smack_netlabel(struct sock *sk, int labeled)
2228 struct smack_known *skp;
2229 struct socket_smack *ssp = sk->sk_security;
2233 * Usually the netlabel code will handle changing the
2234 * packet labeling based on the label.
2235 * The case of a single label host is different, because
2236 * a single label host should never get a labeled packet
2237 * even though the label is usually associated with a packet
2241 bh_lock_sock_nested(sk);
2243 if (ssp->smk_out == smack_net_ambient ||
2244 labeled == SMACK_UNLABELED_SOCKET)
2245 netlbl_sock_delattr(sk);
2248 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2258 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2260 * @sap: the destination address
2262 * Set the correct secattr for the given socket based on the destination
2263 * address and perform any outbound access checks needed.
2265 * Returns 0 on success or an error code.
2268 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2270 struct smack_known *skp;
2274 struct socket_smack *ssp = sk->sk_security;
2275 struct smk_audit_info ad;
2278 hostsp = smack_host_label(sap);
2279 if (hostsp != NULL) {
2281 struct lsm_network_audit net;
2283 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2284 ad.a.u.net->family = sap->sin_family;
2285 ad.a.u.net->dport = sap->sin_port;
2286 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2288 sk_lbl = SMACK_UNLABELED_SOCKET;
2290 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
2291 rc = smk_bu_note("IPv4 host check", skp, hostsp, MAY_WRITE, rc);
2293 sk_lbl = SMACK_CIPSO_SOCKET;
2300 return smack_netlabel(sk, sk_lbl);
2303 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2305 * smk_ipv6_port_label - Smack port access table management
2309 * Create or update the port list entry
2311 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2313 struct sock *sk = sock->sk;
2314 struct sockaddr_in6 *addr6;
2315 struct socket_smack *ssp = sock->sk->sk_security;
2316 struct smk_port_label *spp;
2317 unsigned short port = 0;
2319 if (address == NULL) {
2321 * This operation is changing the Smack information
2322 * on the bound socket. Take the changes to the port
2325 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2326 if (sk != spp->smk_sock)
2328 spp->smk_in = ssp->smk_in;
2329 spp->smk_out = ssp->smk_out;
2333 * A NULL address is only used for updating existing
2334 * bound entries. If there isn't one, it's OK.
2339 addr6 = (struct sockaddr_in6 *)address;
2340 port = ntohs(addr6->sin6_port);
2342 * This is a special case that is safely ignored.
2348 * Look for an existing port list entry.
2349 * This is an indication that a port is getting reused.
2351 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2352 if (spp->smk_port != port)
2354 spp->smk_port = port;
2356 spp->smk_in = ssp->smk_in;
2357 spp->smk_out = ssp->smk_out;
2362 * A new port entry is required.
2364 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2368 spp->smk_port = port;
2370 spp->smk_in = ssp->smk_in;
2371 spp->smk_out = ssp->smk_out;
2373 list_add(&spp->list, &smk_ipv6_port_list);
2378 * smk_ipv6_port_check - check Smack port access
2382 * Create or update the port list entry
2384 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2389 struct smk_port_label *spp;
2390 struct socket_smack *ssp = sk->sk_security;
2391 struct smack_known *skp;
2392 unsigned short port = 0;
2394 struct smk_audit_info ad;
2397 struct lsm_network_audit net;
2400 if (act == SMK_RECEIVING) {
2401 skp = smack_net_ambient;
2402 object = ssp->smk_in->smk_known;
2405 object = smack_net_ambient->smk_known;
2409 * Get the IP address and port from the address.
2411 port = ntohs(address->sin6_port);
2412 bep = (__be16 *)(&address->sin6_addr);
2413 be32p = (__be32 *)(&address->sin6_addr);
2416 * It's remote, so port lookup does no good.
2418 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2422 * It's local so the send check has to have passed.
2424 if (act == SMK_RECEIVING) {
2425 skp = &smack_known_web;
2429 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2430 if (spp->smk_port != port)
2432 object = spp->smk_in->smk_known;
2433 if (act == SMK_CONNECTING)
2434 ssp->smk_packet = spp->smk_out;
2441 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2442 ad.a.u.net->family = sk->sk_family;
2443 ad.a.u.net->dport = port;
2444 if (act == SMK_RECEIVING)
2445 ad.a.u.net->v6info.saddr = address->sin6_addr;
2447 ad.a.u.net->v6info.daddr = address->sin6_addr;
2449 rc = smk_access(skp, object, MAY_WRITE, &ad);
2450 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2453 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2456 * smack_inode_setsecurity - set smack xattrs
2457 * @inode: the object
2458 * @name: attribute name
2459 * @value: attribute value
2460 * @size: size of the attribute
2463 * Sets the named attribute in the appropriate blob
2465 * Returns 0 on success, or an error code
2467 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2468 const void *value, size_t size, int flags)
2470 struct smack_known *skp;
2471 struct inode_smack *nsp = inode->i_security;
2472 struct socket_smack *ssp;
2473 struct socket *sock;
2476 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2479 skp = smk_import_entry(value, size);
2483 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2484 nsp->smk_inode = skp->smk_known;
2485 nsp->smk_flags |= SMK_INODE_INSTANT;
2489 * The rest of the Smack xattrs are only on sockets.
2491 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2494 sock = SOCKET_I(inode);
2495 if (sock == NULL || sock->sk == NULL)
2498 ssp = sock->sk->sk_security;
2500 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2502 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2504 if (sock->sk->sk_family == PF_INET) {
2505 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2508 "Smack: \"%s\" netlbl error %d.\n",
2514 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2515 if (sock->sk->sk_family == PF_INET6)
2516 smk_ipv6_port_label(sock, NULL);
2517 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2523 * smack_socket_post_create - finish socket setup
2525 * @family: protocol family
2530 * Sets the netlabel information on the socket
2532 * Returns 0 on success, and error code otherwise
2534 static int smack_socket_post_create(struct socket *sock, int family,
2535 int type, int protocol, int kern)
2537 struct socket_smack *ssp;
2539 if (sock->sk == NULL)
2543 * Sockets created by kernel threads receive web label.
2545 if (unlikely(current->flags & PF_KTHREAD)) {
2546 ssp = sock->sk->sk_security;
2547 ssp->smk_in = &smack_known_web;
2549 if (family != PF_INET)
2552 * Set the outbound netlbl.
2554 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2557 #ifndef CONFIG_SECURITY_SMACK_NETFILTER
2559 * smack_socket_bind - record port binding information.
2561 * @address: the port address
2562 * @addrlen: size of the address
2564 * Records the label bound to a port.
2568 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2571 #if IS_ENABLED(CONFIG_IPV6)
2572 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2573 smk_ipv6_port_label(sock, address);
2578 #endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
2581 * smack_socket_connect - connect access check
2583 * @sap: the other end
2584 * @addrlen: size of sap
2586 * Verifies that a connection may be possible
2588 * Returns 0 on success, and error code otherwise
2590 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2595 if (sock->sk == NULL)
2598 switch (sock->sk->sk_family) {
2600 if (addrlen < sizeof(struct sockaddr_in))
2602 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2605 if (addrlen < sizeof(struct sockaddr_in6))
2607 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2608 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2610 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2617 * smack_flags_to_may - convert S_ to MAY_ values
2618 * @flags: the S_ value
2620 * Returns the equivalent MAY_ value
2622 static int smack_flags_to_may(int flags)
2626 if (flags & S_IRUGO)
2628 if (flags & S_IWUGO)
2630 if (flags & S_IXUGO)
2637 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2642 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2644 struct smack_known *skp = smk_of_current();
2646 msg->security = skp->smk_known;
2651 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2654 * Clears the blob pointer
2656 static void smack_msg_msg_free_security(struct msg_msg *msg)
2658 msg->security = NULL;
2662 * smack_of_shm - the smack pointer for the shm
2665 * Returns a pointer to the smack value
2667 static char *smack_of_shm(struct shmid_kernel *shp)
2669 return (char *)shp->shm_perm.security;
2673 * smack_shm_alloc_security - Set the security blob for shm
2678 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2680 struct kern_ipc_perm *isp = &shp->shm_perm;
2681 struct smack_known *skp = smk_of_current();
2683 isp->security = skp->smk_known;
2688 * smack_shm_free_security - Clear the security blob for shm
2691 * Clears the blob pointer
2693 static void smack_shm_free_security(struct shmid_kernel *shp)
2695 struct kern_ipc_perm *isp = &shp->shm_perm;
2697 isp->security = NULL;
2701 * smk_curacc_shm : check if current has access on shm
2703 * @access : access requested
2705 * Returns 0 if current has the requested access, error code otherwise
2707 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2709 char *ssp = smack_of_shm(shp);
2710 struct smk_audit_info ad;
2714 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2715 ad.a.u.ipc_id = shp->shm_perm.id;
2717 rc = smk_curacc(ssp, access, &ad);
2718 rc = smk_bu_current("shm", ssp, access, rc);
2723 * smack_shm_associate - Smack access check for shm
2725 * @shmflg: access requested
2727 * Returns 0 if current has the requested access, error code otherwise
2729 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2733 may = smack_flags_to_may(shmflg);
2734 return smk_curacc_shm(shp, may);
2738 * smack_shm_shmctl - Smack access check for shm
2740 * @cmd: what it wants to do
2742 * Returns 0 if current has the requested access, error code otherwise
2744 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2757 may = MAY_READWRITE;
2762 * System level information.
2768 return smk_curacc_shm(shp, may);
2772 * smack_shm_shmat - Smack access for shmat
2775 * @shmflg: access requested
2777 * Returns 0 if current has the requested access, error code otherwise
2779 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2784 may = smack_flags_to_may(shmflg);
2785 return smk_curacc_shm(shp, may);
2789 * smack_of_sem - the smack pointer for the sem
2792 * Returns a pointer to the smack value
2794 static char *smack_of_sem(struct sem_array *sma)
2796 return (char *)sma->sem_perm.security;
2800 * smack_sem_alloc_security - Set the security blob for sem
2805 static int smack_sem_alloc_security(struct sem_array *sma)
2807 struct kern_ipc_perm *isp = &sma->sem_perm;
2808 struct smack_known *skp = smk_of_current();
2810 isp->security = skp->smk_known;
2815 * smack_sem_free_security - Clear the security blob for sem
2818 * Clears the blob pointer
2820 static void smack_sem_free_security(struct sem_array *sma)
2822 struct kern_ipc_perm *isp = &sma->sem_perm;
2824 isp->security = NULL;
2828 * smk_curacc_sem : check if current has access on sem
2830 * @access : access requested
2832 * Returns 0 if current has the requested access, error code otherwise
2834 static int smk_curacc_sem(struct sem_array *sma, int access)
2836 char *ssp = smack_of_sem(sma);
2837 struct smk_audit_info ad;
2841 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2842 ad.a.u.ipc_id = sma->sem_perm.id;
2844 rc = smk_curacc(ssp, access, &ad);
2845 rc = smk_bu_current("sem", ssp, access, rc);
2850 * smack_sem_associate - Smack access check for sem
2852 * @semflg: access requested
2854 * Returns 0 if current has the requested access, error code otherwise
2856 static int smack_sem_associate(struct sem_array *sma, int semflg)
2860 may = smack_flags_to_may(semflg);
2861 return smk_curacc_sem(sma, may);
2865 * smack_sem_shmctl - Smack access check for sem
2867 * @cmd: what it wants to do
2869 * Returns 0 if current has the requested access, error code otherwise
2871 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2889 may = MAY_READWRITE;
2894 * System level information
2901 return smk_curacc_sem(sma, may);
2905 * smack_sem_semop - Smack checks of semaphore operations
2911 * Treated as read and write in all cases.
2913 * Returns 0 if access is allowed, error code otherwise
2915 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2916 unsigned nsops, int alter)
2918 return smk_curacc_sem(sma, MAY_READWRITE);
2922 * smack_msg_alloc_security - Set the security blob for msg
2927 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2929 struct kern_ipc_perm *kisp = &msq->q_perm;
2930 struct smack_known *skp = smk_of_current();
2932 kisp->security = skp->smk_known;
2937 * smack_msg_free_security - Clear the security blob for msg
2940 * Clears the blob pointer
2942 static void smack_msg_queue_free_security(struct msg_queue *msq)
2944 struct kern_ipc_perm *kisp = &msq->q_perm;
2946 kisp->security = NULL;
2950 * smack_of_msq - the smack pointer for the msq
2953 * Returns a pointer to the smack value
2955 static char *smack_of_msq(struct msg_queue *msq)
2957 return (char *)msq->q_perm.security;
2961 * smk_curacc_msq : helper to check if current has access on msq
2963 * @access : access requested
2965 * return 0 if current has access, error otherwise
2967 static int smk_curacc_msq(struct msg_queue *msq, int access)
2969 char *msp = smack_of_msq(msq);
2970 struct smk_audit_info ad;
2974 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2975 ad.a.u.ipc_id = msq->q_perm.id;
2977 rc = smk_curacc(msp, access, &ad);
2978 rc = smk_bu_current("msq", msp, access, rc);
2983 * smack_msg_queue_associate - Smack access check for msg_queue
2985 * @msqflg: access requested
2987 * Returns 0 if current has the requested access, error code otherwise
2989 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2993 may = smack_flags_to_may(msqflg);
2994 return smk_curacc_msq(msq, may);
2998 * smack_msg_queue_msgctl - Smack access check for msg_queue
3000 * @cmd: what it wants to do
3002 * Returns 0 if current has the requested access, error code otherwise
3004 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
3015 may = MAY_READWRITE;
3020 * System level information
3027 return smk_curacc_msq(msq, may);
3031 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3034 * @msqflg: access requested
3036 * Returns 0 if current has the requested access, error code otherwise
3038 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
3043 may = smack_flags_to_may(msqflg);
3044 return smk_curacc_msq(msq, may);
3048 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3055 * Returns 0 if current has read and write access, error code otherwise
3057 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3058 struct task_struct *target, long type, int mode)
3060 return smk_curacc_msq(msq, MAY_READWRITE);
3064 * smack_ipc_permission - Smack access for ipc_permission()
3065 * @ipp: the object permissions
3066 * @flag: access requested
3068 * Returns 0 if current has read and write access, error code otherwise
3070 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3072 char *isp = ipp->security;
3073 int may = smack_flags_to_may(flag);
3074 struct smk_audit_info ad;
3078 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3079 ad.a.u.ipc_id = ipp->id;
3081 rc = smk_curacc(isp, may, &ad);
3082 rc = smk_bu_current("svipc", isp, may, rc);
3087 * smack_ipc_getsecid - Extract smack security id
3088 * @ipp: the object permissions
3089 * @secid: where result will be saved
3091 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3093 char *smack = ipp->security;
3095 *secid = smack_to_secid(smack);
3099 * smack_d_instantiate - Make sure the blob is correct on an inode
3100 * @opt_dentry: dentry where inode will be attached
3101 * @inode: the object
3103 * Set the inode's security blob if it hasn't been done already.
3105 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3107 struct super_block *sbp;
3108 struct superblock_smack *sbsp;
3109 struct inode_smack *isp;
3110 struct smack_known *skp;
3111 struct smack_known *ckp = smk_of_current();
3113 char trattr[TRANS_TRUE_SIZE];
3121 isp = inode->i_security;
3123 mutex_lock(&isp->smk_lock);
3125 * If the inode is already instantiated
3126 * take the quick way out
3128 if (isp->smk_flags & SMK_INODE_INSTANT)
3132 sbsp = sbp->s_security;
3134 * We're going to use the superblock default label
3135 * if there's no label on the file.
3137 final = sbsp->smk_default;
3140 * If this is the root inode the superblock
3141 * may be in the process of initialization.
3142 * If that is the case use the root value out
3143 * of the superblock.
3145 if (opt_dentry->d_parent == opt_dentry) {
3146 switch (sbp->s_magic) {
3147 case CGROUP_SUPER_MAGIC:
3149 * The cgroup filesystem is never mounted,
3150 * so there's no opportunity to set the mount
3153 sbsp->smk_root = smack_known_star.smk_known;
3154 sbsp->smk_default = smack_known_star.smk_known;
3155 isp->smk_inode = sbsp->smk_root;
3159 * What about shmem/tmpfs anonymous files with dentry
3160 * obtained from d_alloc_pseudo()?
3162 isp->smk_inode = smk_of_current()->smk_known;
3165 isp->smk_inode = smk_of_current()->smk_known;
3169 * Socket access is controlled by the socket
3170 * structures associated with the task involved.
3172 isp->smk_inode = smack_known_star.smk_known;
3175 isp->smk_inode = sbsp->smk_root;
3178 isp->smk_flags |= SMK_INODE_INSTANT;
3183 * This is pretty hackish.
3184 * Casey says that we shouldn't have to do
3185 * file system specific code, but it does help
3186 * with keeping it simple.
3188 switch (sbp->s_magic) {
3190 case CGROUP_SUPER_MAGIC:
3192 * Casey says that it's a little embarrassing
3193 * that the smack file system doesn't do
3194 * extended attributes.
3196 * Cgroupfs is special
3198 final = smack_known_star.smk_known;
3200 case DEVPTS_SUPER_MAGIC:
3202 * devpts seems content with the label of the task.
3203 * Programs that change smack have to treat the
3206 final = ckp->smk_known;
3208 case PROC_SUPER_MAGIC:
3210 * Casey says procfs appears not to care.
3211 * The superblock default suffices.
3216 * Device labels should come from the filesystem,
3217 * but watch out, because they're volitile,
3218 * getting recreated on every reboot.
3220 final = smack_known_star.smk_known;
3224 * If a smack value has been set we want to use it,
3225 * but since tmpfs isn't giving us the opportunity
3226 * to set mount options simulate setting the
3227 * superblock default.
3231 * This isn't an understood special case.
3232 * Get the value from the xattr.
3236 * UNIX domain sockets use lower level socket data.
3238 if (S_ISSOCK(inode->i_mode)) {
3239 final = smack_known_star.smk_known;
3243 * No xattr support means, alas, no SMACK label.
3244 * Use the aforeapplied default.
3245 * It would be curious if the label of the task
3246 * does not match that assigned.
3248 if (inode->i_op->getxattr == NULL)
3251 * Get the dentry for xattr.
3253 dp = dget(opt_dentry);
3254 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3256 final = skp->smk_known;
3259 * Transmuting directory
3261 if (S_ISDIR(inode->i_mode)) {
3263 * If this is a new directory and the label was
3264 * transmuted when the inode was initialized
3265 * set the transmute attribute on the directory
3266 * and mark the inode.
3268 * If there is a transmute attribute on the
3269 * directory mark the inode.
3271 if (isp->smk_flags & SMK_INODE_CHANGED) {
3272 isp->smk_flags &= ~SMK_INODE_CHANGED;
3273 rc = inode->i_op->setxattr(dp,
3274 XATTR_NAME_SMACKTRANSMUTE,
3275 TRANS_TRUE, TRANS_TRUE_SIZE,
3278 rc = inode->i_op->getxattr(dp,
3279 XATTR_NAME_SMACKTRANSMUTE, trattr,
3281 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3282 TRANS_TRUE_SIZE) != 0)
3286 transflag = SMK_INODE_TRANSMUTE;
3288 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3289 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3296 isp->smk_inode = ckp->smk_known;
3298 isp->smk_inode = final;
3300 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3303 mutex_unlock(&isp->smk_lock);
3308 * smack_getprocattr - Smack process attribute access
3309 * @p: the object task
3310 * @name: the name of the attribute in /proc/.../attr
3311 * @value: where to put the result
3313 * Places a copy of the task Smack into value
3315 * Returns the length of the smack label or an error code
3317 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3319 struct smack_known *skp = smk_of_task(task_security(p));
3323 if (strcmp(name, "current") != 0)
3326 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3336 * smack_setprocattr - Smack process attribute setting
3337 * @p: the object task
3338 * @name: the name of the attribute in /proc/.../attr
3339 * @value: the value to set
3340 * @size: the size of the value
3342 * Sets the Smack value of the task. Only setting self
3343 * is permitted and only with privilege
3345 * Returns the length of the smack label or an error code
3347 static int smack_setprocattr(struct task_struct *p, char *name,
3348 void *value, size_t size)
3350 struct task_smack *tsp = current_security();
3352 struct smack_known *skp;
3353 struct smack_known_list_elem *sklep;
3357 * Changing another process' Smack value is too dangerous
3358 * and supports no sane use case.
3363 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3366 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3369 if (strcmp(name, "current") != 0)
3372 skp = smk_import_entry(value, size);
3377 * No process is ever allowed the web ("@") label.
3379 if (skp == &smack_known_web)
3382 if (!smack_privileged(CAP_MAC_ADMIN)) {
3384 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3385 if (sklep->smk_label == skp) {
3393 new = prepare_creds();
3397 tsp = new->security;
3398 tsp->smk_task = skp;
3400 * process can change its label only once
3402 smk_destroy_label_list(&tsp->smk_relabel);
3409 * smack_unix_stream_connect - Smack access on UDS
3411 * @other: the other sock
3414 * Return 0 if a subject with the smack of sock could access
3415 * an object with the smack of other, otherwise an error code
3417 static int smack_unix_stream_connect(struct sock *sock,
3418 struct sock *other, struct sock *newsk)
3420 struct smack_known *skp;
3421 struct smack_known *okp;
3422 struct socket_smack *ssp = sock->sk_security;
3423 struct socket_smack *osp = other->sk_security;
3424 struct socket_smack *nsp = newsk->sk_security;
3425 struct smk_audit_info ad;
3428 struct lsm_network_audit net;
3431 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3435 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3436 smk_ad_setfield_u_net_sk(&ad, other);
3438 rc = smk_access(skp, okp->smk_known, MAY_WRITE, &ad);
3439 rc = smk_bu_note("UDS connect", skp, okp->smk_known,
3444 rc = smk_access(okp, skp->smk_known, MAY_WRITE, &ad);
3445 rc = smk_bu_note("UDS connect", okp, skp->smk_known,
3451 * Cross reference the peer labels for SO_PEERSEC.
3454 nsp->smk_packet = ssp->smk_out;
3455 ssp->smk_packet = osp->smk_out;
3462 * smack_unix_may_send - Smack access on UDS
3464 * @other: the other socket
3466 * Return 0 if a subject with the smack of sock could access
3467 * an object with the smack of other, otherwise an error code
3469 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3471 struct socket_smack *ssp = sock->sk->sk_security;
3472 struct socket_smack *osp = other->sk->sk_security;
3473 struct smack_known *skp;
3474 struct smk_audit_info ad;
3478 struct lsm_network_audit net;
3480 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3481 smk_ad_setfield_u_net_sk(&ad, other->sk);
3484 if (smack_privileged(CAP_MAC_OVERRIDE))
3489 rc = smk_access(skp, osp->smk_in->smk_known, MAY_WRITE, &ad);
3490 rc = smk_bu_note("UDS send", skp, osp->smk_in->smk_known,
3496 * smack_socket_sendmsg - Smack check based on destination host
3499 * @size: the size of the message
3501 * Return 0 if the current subject can write to the destination host.
3502 * For IPv4 this is only a question if the destination is a single label host.
3503 * For IPv6 this is a check against the label of the port.
3505 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3508 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3509 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3510 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3511 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
3515 * Perfectly reasonable for this to be NULL
3520 switch (sock->sk->sk_family) {
3522 rc = smack_netlabel_send(sock->sk, sip);
3525 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3526 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3527 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
3534 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3535 * @sap: netlabel secattr
3536 * @ssp: socket security information
3538 * Returns a pointer to a Smack label entry found on the label list.
3540 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3541 struct socket_smack *ssp)
3543 struct smack_known *skp;
3548 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3550 * Looks like a CIPSO packet.
3551 * If there are flags but no level netlabel isn't
3552 * behaving the way we expect it to.
3554 * Look it up in the label table
3555 * Without guidance regarding the smack value
3556 * for the packet fall back on the network
3560 list_for_each_entry(skp, &smack_known_list, list) {
3561 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3564 * Compare the catsets. Use the netlbl APIs.
3566 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3567 if ((skp->smk_netlabel.flags &
3568 NETLBL_SECATTR_MLS_CAT) == 0)
3572 for (acat = -1, kcat = -1; acat == kcat; ) {
3573 acat = netlbl_secattr_catmap_walk(
3574 sap->attr.mls.cat, acat + 1);
3575 kcat = netlbl_secattr_catmap_walk(
3576 skp->smk_netlabel.attr.mls.cat,
3578 if (acat < 0 || kcat < 0)
3591 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3592 return &smack_known_web;
3593 return &smack_known_star;
3595 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3597 * Looks like a fallback, which gives us a secid.
3599 skp = smack_from_secid(sap->attr.secid);
3601 * This has got to be a bug because it is
3602 * impossible to specify a fallback without
3603 * specifying the label, which will ensure
3604 * it has a secid, and the only way to get a
3605 * secid is from a fallback.
3607 BUG_ON(skp == NULL);
3611 * Without guidance regarding the smack value
3612 * for the packet fall back on the network
3615 return smack_net_ambient;
3618 #if IS_ENABLED(CONFIG_IPV6)
3619 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3623 int proto = -EINVAL;
3624 struct ipv6hdr _ipv6h;
3625 struct ipv6hdr *ip6;
3627 struct tcphdr _tcph, *th;
3628 struct udphdr _udph, *uh;
3629 struct dccp_hdr _dccph, *dh;
3633 offset = skb_network_offset(skb);
3634 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3637 sip->sin6_addr = ip6->saddr;
3639 nexthdr = ip6->nexthdr;
3640 offset += sizeof(_ipv6h);
3641 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3648 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3650 sip->sin6_port = th->source;
3653 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3655 sip->sin6_port = uh->source;
3658 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3660 sip->sin6_port = dh->dccph_sport;
3665 #endif /* CONFIG_IPV6 */
3668 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3672 * Returns 0 if the packet should be delivered, an error code otherwise
3674 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3676 struct netlbl_lsm_secattr secattr;
3677 struct socket_smack *ssp = sk->sk_security;
3678 struct smack_known *skp = NULL;
3680 struct smk_audit_info ad;
3682 struct lsm_network_audit net;
3684 #if IS_ENABLED(CONFIG_IPV6)
3685 struct sockaddr_in6 sadd;
3687 #endif /* CONFIG_IPV6 */
3689 switch (sk->sk_family) {
3691 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3693 * If there is a secmark use it rather than the CIPSO label.
3694 * If there is no secmark fall back to CIPSO.
3695 * The secmark is assumed to reflect policy better.
3697 if (skb && skb->secmark != 0) {
3698 skp = smack_from_secid(skb->secmark);
3701 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3703 * Translate what netlabel gave us.
3705 netlbl_secattr_init(&secattr);
3707 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3709 skp = smack_from_secattr(&secattr, ssp);
3711 skp = smack_net_ambient;
3713 netlbl_secattr_destroy(&secattr);
3715 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3719 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3720 ad.a.u.net->family = sk->sk_family;
3721 ad.a.u.net->netif = skb->skb_iif;
3722 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3725 * Receiving a packet requires that the other end
3726 * be able to write here. Read access is not required.
3727 * This is the simplist possible security model
3731 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3732 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in->smk_known,
3736 netlbl_skbuff_err(skb, rc, 0);
3738 #if IS_ENABLED(CONFIG_IPV6)
3740 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3741 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3743 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3744 if (skb && skb->secmark != 0)
3745 skp = smack_from_secid(skb->secmark);
3747 skp = smack_net_ambient;
3749 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3750 ad.a.u.net->family = sk->sk_family;
3751 ad.a.u.net->netif = skb->skb_iif;
3752 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3753 #endif /* CONFIG_AUDIT */
3754 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3755 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in->smk_known,
3757 #else /* CONFIG_SECURITY_SMACK_NETFILTER */
3758 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3759 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3761 #endif /* CONFIG_IPV6 */
3768 * smack_socket_getpeersec_stream - pull in packet label
3770 * @optval: user's destination
3771 * @optlen: size thereof
3774 * returns zero on success, an error code otherwise
3776 static int smack_socket_getpeersec_stream(struct socket *sock,
3777 char __user *optval,
3778 int __user *optlen, unsigned len)
3780 struct socket_smack *ssp;
3785 ssp = sock->sk->sk_security;
3786 if (ssp->smk_packet != NULL) {
3787 rcp = ssp->smk_packet->smk_known;
3788 slen = strlen(rcp) + 1;
3793 else if (copy_to_user(optval, rcp, slen) != 0)
3796 if (put_user(slen, optlen) != 0)
3804 * smack_socket_getpeersec_dgram - pull in packet label
3805 * @sock: the peer socket
3807 * @secid: pointer to where to put the secid of the packet
3809 * Sets the netlabel socket state on sk from parent
3811 static int smack_socket_getpeersec_dgram(struct socket *sock,
3812 struct sk_buff *skb, u32 *secid)
3815 struct netlbl_lsm_secattr secattr;
3816 struct socket_smack *ssp = NULL;
3817 struct smack_known *skp;
3818 int family = PF_UNSPEC;
3819 u32 s = 0; /* 0 is the invalid secid */
3823 if (skb->protocol == htons(ETH_P_IP))
3825 #if IS_ENABLED(CONFIG_IPV6)
3826 else if (skb->protocol == htons(ETH_P_IPV6))
3828 #endif /* CONFIG_IPV6 */
3830 if (family == PF_UNSPEC && sock != NULL)
3831 family = sock->sk->sk_family;
3835 ssp = sock->sk->sk_security;
3836 s = ssp->smk_out->smk_secid;
3839 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3845 * Translate what netlabel gave us.
3847 if (sock != NULL && sock->sk != NULL)
3848 ssp = sock->sk->sk_security;
3849 netlbl_secattr_init(&secattr);
3850 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3852 skp = smack_from_secattr(&secattr, ssp);
3855 netlbl_secattr_destroy(&secattr);
3857 #if IS_ENABLED(CONFIG_IPV6)
3859 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3861 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3863 #endif /* CONFIG_IPV6 */
3872 * smack_sock_graft - Initialize a newly created socket with an existing sock
3874 * @parent: parent socket
3876 * Set the smk_{in,out} state of an existing sock based on the process that
3877 * is creating the new socket.
3879 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3881 struct socket_smack *ssp;
3882 struct smack_known *skp = smk_of_current();
3885 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3888 ssp = sk->sk_security;
3891 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3895 * smack_inet_conn_request - Smack access check on connect
3896 * @sk: socket involved
3900 * Returns 0 if a task with the packet label could write to
3901 * the socket, otherwise an error code
3903 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3904 struct request_sock *req)
3906 u16 family = sk->sk_family;
3907 struct smack_known *skp;
3908 struct socket_smack *ssp = sk->sk_security;
3909 struct netlbl_lsm_secattr secattr;
3910 struct sockaddr_in addr;
3914 struct smk_audit_info ad;
3916 struct lsm_network_audit net;
3919 #if IS_ENABLED(CONFIG_IPV6)
3920 if (family == PF_INET6) {
3922 * Handle mapped IPv4 packets arriving
3923 * via IPv6 sockets. Don't set up netlabel
3924 * processing on IPv6.
3926 if (skb->protocol == htons(ETH_P_IP))
3931 #endif /* CONFIG_IPV6 */
3933 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3935 * If there is a secmark use it rather than the CIPSO label.
3936 * If there is no secmark fall back to CIPSO.
3937 * The secmark is assumed to reflect policy better.
3939 if (skb && skb->secmark != 0) {
3940 skp = smack_from_secid(skb->secmark);
3943 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3945 netlbl_secattr_init(&secattr);
3946 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3948 skp = smack_from_secattr(&secattr, ssp);
3950 skp = &smack_known_huh;
3951 netlbl_secattr_destroy(&secattr);
3953 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3958 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3959 ad.a.u.net->family = family;
3960 ad.a.u.net->netif = skb->skb_iif;
3961 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3964 * Receiving a packet requires that the other end be able to write
3965 * here. Read access is not required.
3968 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3969 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in->smk_known,
3975 * Save the peer's label in the request_sock so we can later setup
3976 * smk_packet in the child socket so that SO_PEERCRED can report it.
3978 req->peer_secid = skp->smk_secid;
3981 * We need to decide if we want to label the incoming connection here
3982 * if we do we only need to label the request_sock and the stack will
3983 * propagate the wire-label to the sock when it is created.
3986 addr.sin_addr.s_addr = hdr->saddr;
3988 hsp = smack_host_label(&addr);
3992 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3994 netlbl_req_delattr(req);
4000 * smack_inet_csk_clone - Copy the connection information to the new socket
4001 * @sk: the new socket
4002 * @req: the connection's request_sock
4004 * Transfer the connection's peer label to the newly created socket.
4006 static void smack_inet_csk_clone(struct sock *sk,
4007 const struct request_sock *req)
4009 struct socket_smack *ssp = sk->sk_security;
4010 struct smack_known *skp;
4012 if (req->peer_secid != 0) {
4013 skp = smack_from_secid(req->peer_secid);
4014 ssp->smk_packet = skp;
4016 ssp->smk_packet = NULL;
4020 * Key management security hooks
4022 * Casey has not tested key support very heavily.
4023 * The permission check is most likely too restrictive.
4024 * If you care about keys please have a look.
4029 * smack_key_alloc - Set the key security blob
4031 * @cred: the credentials to use
4034 * No allocation required
4038 static int smack_key_alloc(struct key *key, const struct cred *cred,
4039 unsigned long flags)
4041 struct smack_known *skp = smk_of_task(cred->security);
4043 key->security = skp->smk_known;
4048 * smack_key_free - Clear the key security blob
4051 * Clear the blob pointer
4053 static void smack_key_free(struct key *key)
4055 key->security = NULL;
4059 * smack_key_permission - Smack access on a key
4060 * @key_ref: gets to the object
4061 * @cred: the credentials to use
4064 * Return 0 if the task has read and write to the object,
4065 * an error code otherwise
4067 static int smack_key_permission(key_ref_t key_ref,
4068 const struct cred *cred, key_perm_t perm)
4071 struct smk_audit_info ad;
4072 struct smack_known *tkp = smk_of_task(cred->security);
4075 keyp = key_ref_to_ptr(key_ref);
4079 * If the key hasn't been initialized give it access so that
4082 if (keyp->security == NULL)
4085 * This should not occur
4090 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4091 ad.a.u.key_struct.key = keyp->serial;
4092 ad.a.u.key_struct.key_desc = keyp->description;
4094 rc = smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
4095 rc = smk_bu_note("key access", tkp, keyp->security, MAY_READWRITE, rc);
4098 #endif /* CONFIG_KEYS */
4103 * Audit requires a unique representation of each Smack specific
4104 * rule. This unique representation is used to distinguish the
4105 * object to be audited from remaining kernel objects and also
4106 * works as a glue between the audit hooks.
4108 * Since repository entries are added but never deleted, we'll use
4109 * the smack_known label address related to the given audit rule as
4110 * the needed unique representation. This also better fits the smack
4111 * model where nearly everything is a label.
4116 * smack_audit_rule_init - Initialize a smack audit rule
4117 * @field: audit rule fields given from user-space (audit.h)
4118 * @op: required testing operator (=, !=, >, <, ...)
4119 * @rulestr: smack label to be audited
4120 * @vrule: pointer to save our own audit rule representation
4122 * Prepare to audit cases where (@field @op @rulestr) is true.
4123 * The label to be audited is created if necessay.
4125 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4127 char **rule = (char **)vrule;
4130 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4133 if (op != Audit_equal && op != Audit_not_equal)
4136 *rule = smk_import(rulestr, 0);
4142 * smack_audit_rule_known - Distinguish Smack audit rules
4143 * @krule: rule of interest, in Audit kernel representation format
4145 * This is used to filter Smack rules from remaining Audit ones.
4146 * If it's proved that this rule belongs to us, the
4147 * audit_rule_match hook will be called to do the final judgement.
4149 static int smack_audit_rule_known(struct audit_krule *krule)
4151 struct audit_field *f;
4154 for (i = 0; i < krule->field_count; i++) {
4155 f = &krule->fields[i];
4157 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4165 * smack_audit_rule_match - Audit given object ?
4166 * @secid: security id for identifying the object to test
4167 * @field: audit rule flags given from user-space
4168 * @op: required testing operator
4169 * @vrule: smack internal rule presentation
4170 * @actx: audit context associated with the check
4172 * The core Audit hook. It's used to take the decision of
4173 * whether to audit or not to audit a given object.
4175 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4176 struct audit_context *actx)
4178 struct smack_known *skp;
4182 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
4183 "Smack: missing rule\n");
4187 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4190 skp = smack_from_secid(secid);
4193 * No need to do string comparisons. If a match occurs,
4194 * both pointers will point to the same smack_known
4197 if (op == Audit_equal)
4198 return (rule == skp->smk_known);
4199 if (op == Audit_not_equal)
4200 return (rule != skp->smk_known);
4206 * smack_audit_rule_free - free smack rule representation
4207 * @vrule: rule to be freed.
4209 * No memory was allocated.
4211 static void smack_audit_rule_free(void *vrule)
4216 #endif /* CONFIG_AUDIT */
4219 * smack_secid_to_secctx - return the smack label for a secid
4220 * @secid: incoming integer
4221 * @secdata: destination
4222 * @seclen: how long it is
4224 * Exists for networking code.
4226 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4228 struct smack_known *skp = smack_from_secid(secid);
4231 *secdata = skp->smk_known;
4232 *seclen = strlen(skp->smk_known);
4237 * smack_secctx_to_secid - return the secid for a smack label
4238 * @secdata: smack label
4239 * @seclen: how long result is
4240 * @secid: outgoing integer
4242 * Exists for audit and networking code.
4244 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4246 *secid = smack_to_secid(secdata);
4251 * smack_release_secctx - don't do anything.
4255 * Exists to make sure nothing gets done, and properly
4257 static void smack_release_secctx(char *secdata, u32 seclen)
4261 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4263 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4266 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4268 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4271 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4274 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4282 struct security_operations smack_ops = {
4285 .ptrace_access_check = smack_ptrace_access_check,
4286 .ptrace_traceme = smack_ptrace_traceme,
4287 .syslog = smack_syslog,
4289 .sb_alloc_security = smack_sb_alloc_security,
4290 .sb_free_security = smack_sb_free_security,
4291 .sb_copy_data = smack_sb_copy_data,
4292 .sb_kern_mount = smack_sb_kern_mount,
4293 .sb_statfs = smack_sb_statfs,
4295 .bprm_set_creds = smack_bprm_set_creds,
4296 .bprm_committing_creds = smack_bprm_committing_creds,
4297 .bprm_secureexec = smack_bprm_secureexec,
4299 .inode_alloc_security = smack_inode_alloc_security,
4300 .inode_free_security = smack_inode_free_security,
4301 .inode_init_security = smack_inode_init_security,
4302 .inode_link = smack_inode_link,
4303 .inode_unlink = smack_inode_unlink,
4304 .inode_rmdir = smack_inode_rmdir,
4305 .inode_rename = smack_inode_rename,
4306 .inode_permission = smack_inode_permission,
4307 .inode_setattr = smack_inode_setattr,
4308 .inode_getattr = smack_inode_getattr,
4309 .inode_setxattr = smack_inode_setxattr,
4310 .inode_post_setxattr = smack_inode_post_setxattr,
4311 .inode_getxattr = smack_inode_getxattr,
4312 .inode_removexattr = smack_inode_removexattr,
4313 .inode_getsecurity = smack_inode_getsecurity,
4314 .inode_setsecurity = smack_inode_setsecurity,
4315 .inode_listsecurity = smack_inode_listsecurity,
4316 .inode_getsecid = smack_inode_getsecid,
4318 .file_permission = smack_file_permission,
4319 .file_alloc_security = smack_file_alloc_security,
4320 .file_free_security = smack_file_free_security,
4321 .file_ioctl = smack_file_ioctl,
4322 .file_lock = smack_file_lock,
4323 .file_fcntl = smack_file_fcntl,
4324 .mmap_file = smack_mmap_file,
4325 .mmap_addr = cap_mmap_addr,
4326 .file_set_fowner = smack_file_set_fowner,
4327 .file_send_sigiotask = smack_file_send_sigiotask,
4328 .file_receive = smack_file_receive,
4330 .file_open = smack_file_open,
4332 .cred_alloc_blank = smack_cred_alloc_blank,
4333 .cred_free = smack_cred_free,
4334 .cred_prepare = smack_cred_prepare,
4335 .cred_transfer = smack_cred_transfer,
4336 .kernel_act_as = smack_kernel_act_as,
4337 .kernel_create_files_as = smack_kernel_create_files_as,
4338 .task_setpgid = smack_task_setpgid,
4339 .task_getpgid = smack_task_getpgid,
4340 .task_getsid = smack_task_getsid,
4341 .task_getsecid = smack_task_getsecid,
4342 .task_setnice = smack_task_setnice,
4343 .task_setioprio = smack_task_setioprio,
4344 .task_getioprio = smack_task_getioprio,
4345 .task_setscheduler = smack_task_setscheduler,
4346 .task_getscheduler = smack_task_getscheduler,
4347 .task_movememory = smack_task_movememory,
4348 .task_kill = smack_task_kill,
4349 .task_wait = smack_task_wait,
4350 .task_to_inode = smack_task_to_inode,
4352 .ipc_permission = smack_ipc_permission,
4353 .ipc_getsecid = smack_ipc_getsecid,
4355 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4356 .msg_msg_free_security = smack_msg_msg_free_security,
4358 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4359 .msg_queue_free_security = smack_msg_queue_free_security,
4360 .msg_queue_associate = smack_msg_queue_associate,
4361 .msg_queue_msgctl = smack_msg_queue_msgctl,
4362 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4363 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4365 .shm_alloc_security = smack_shm_alloc_security,
4366 .shm_free_security = smack_shm_free_security,
4367 .shm_associate = smack_shm_associate,
4368 .shm_shmctl = smack_shm_shmctl,
4369 .shm_shmat = smack_shm_shmat,
4371 .sem_alloc_security = smack_sem_alloc_security,
4372 .sem_free_security = smack_sem_free_security,
4373 .sem_associate = smack_sem_associate,
4374 .sem_semctl = smack_sem_semctl,
4375 .sem_semop = smack_sem_semop,
4377 .d_instantiate = smack_d_instantiate,
4379 .getprocattr = smack_getprocattr,
4380 .setprocattr = smack_setprocattr,
4382 .unix_stream_connect = smack_unix_stream_connect,
4383 .unix_may_send = smack_unix_may_send,
4385 .socket_post_create = smack_socket_post_create,
4386 #ifndef CONFIG_SECURITY_SMACK_NETFILTER
4387 .socket_bind = smack_socket_bind,
4388 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4389 .socket_connect = smack_socket_connect,
4390 .socket_sendmsg = smack_socket_sendmsg,
4391 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4392 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4393 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4394 .sk_alloc_security = smack_sk_alloc_security,
4395 .sk_free_security = smack_sk_free_security,
4396 .sock_graft = smack_sock_graft,
4397 .inet_conn_request = smack_inet_conn_request,
4398 .inet_csk_clone = smack_inet_csk_clone,
4400 /* key management security hooks */
4402 .key_alloc = smack_key_alloc,
4403 .key_free = smack_key_free,
4404 .key_permission = smack_key_permission,
4405 #endif /* CONFIG_KEYS */
4409 .audit_rule_init = smack_audit_rule_init,
4410 .audit_rule_known = smack_audit_rule_known,
4411 .audit_rule_match = smack_audit_rule_match,
4412 .audit_rule_free = smack_audit_rule_free,
4413 #endif /* CONFIG_AUDIT */
4415 .secid_to_secctx = smack_secid_to_secctx,
4416 .secctx_to_secid = smack_secctx_to_secid,
4417 .release_secctx = smack_release_secctx,
4418 .inode_notifysecctx = smack_inode_notifysecctx,
4419 .inode_setsecctx = smack_inode_setsecctx,
4420 .inode_getsecctx = smack_inode_getsecctx,
4424 static __init void init_smack_known_list(void)
4427 * Initialize rule list locks
4429 mutex_init(&smack_known_huh.smk_rules_lock);
4430 mutex_init(&smack_known_hat.smk_rules_lock);
4431 mutex_init(&smack_known_floor.smk_rules_lock);
4432 mutex_init(&smack_known_star.smk_rules_lock);
4433 mutex_init(&smack_known_invalid.smk_rules_lock);
4434 mutex_init(&smack_known_web.smk_rules_lock);
4436 * Initialize rule lists
4438 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4439 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4440 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4441 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4442 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4443 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4445 * Create the known labels list
4447 smk_insert_entry(&smack_known_huh);
4448 smk_insert_entry(&smack_known_hat);
4449 smk_insert_entry(&smack_known_star);
4450 smk_insert_entry(&smack_known_floor);
4451 smk_insert_entry(&smack_known_invalid);
4452 smk_insert_entry(&smack_known_web);
4455 /* KMEM caches for fast and thrifty allocations */
4456 struct kmem_cache *smack_rule_cache;
4457 struct kmem_cache *smack_master_list_cache;
4460 * smack_init - initialize the smack system
4464 static __init int smack_init(void)
4467 struct task_smack *tsp;
4469 if (!security_module_enable(&smack_ops))
4474 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4475 if (!smack_rule_cache)
4478 smack_master_list_cache = KMEM_CACHE(smack_master_list, 0);
4479 if (!smack_master_list_cache) {
4480 kmem_cache_destroy(smack_rule_cache);
4484 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4485 if (!smack_inode_cache) {
4486 kmem_cache_destroy(smack_master_list_cache);
4487 kmem_cache_destroy(smack_rule_cache);
4491 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4494 kmem_cache_destroy(smack_master_list_cache);
4495 kmem_cache_destroy(smack_rule_cache);
4496 kmem_cache_destroy(smack_inode_cache);
4500 printk(KERN_INFO "Smack: Initializing.\n");
4503 * Set the security state for the initial task.
4505 cred = (struct cred *) current->cred;
4506 cred->security = tsp;
4508 /* initialize the smack_known_list */
4509 init_smack_known_list();
4514 if (register_security(&smack_ops))
4515 panic("smack: Unable to register with kernel.\n");
4521 * Smack requires early initialization in order to label
4522 * all processes and objects when they are created.
4524 security_initcall(smack_init);