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 LIST_HEAD(smk_ipv6_port_list);
56 static struct kmem_cache *smack_inode_cache;
58 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
61 static char *smk_bu_mess[] = {
62 "Bringup Error", /* Unused */
63 "Bringup", /* SMACK_BRINGUP_ALLOW */
64 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
65 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
69 static void smk_bu_mode(int mode, char *s)
79 if (mode & MAY_APPEND)
81 if (mode & MAY_TRANSMUTE)
91 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
92 static int smk_bu_note(char *note, struct smack_known *sskp, char *osp,
95 char acc[SMK_NUM_ACCESS_TYPE + 1];
99 if (rc > SMACK_UNCONFINED_OBJECT)
102 smk_bu_mode(mode, acc);
104 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
105 sskp->smk_known, osp, acc, note);
110 #define smk_bu_note(note, sskp, osp, mode, RC) (RC)
113 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
114 static int smk_bu_current(char *note, char *osp, int mode, int rc)
117 struct task_smack *tsp = current_security();
119 char acc[SMK_NUM_ACCESS_TYPE + 1];
123 if (rc > SMACK_UNCONFINED_OBJECT)
126 smk_bu_mode(mode, acc);
128 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
129 tsp->smk_task->smk_known, osp, acc, current->comm, note);
134 #define smk_bu_current(note, osp, mode, RC) (RC)
137 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
138 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
141 struct task_smack *tsp = current_security();
142 struct task_smack *otsp = task_security(otp);
144 char acc[SMK_NUM_ACCESS_TYPE + 1];
148 if (rc > SMACK_UNCONFINED_OBJECT)
151 smk_bu_mode(mode, acc);
153 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
154 tsp->smk_task->smk_known, otsp->smk_task->smk_known, acc,
155 current->comm, otp->comm);
160 #define smk_bu_task(otp, mode, RC) (RC)
163 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
164 static int smk_bu_inode(struct inode *inode, int mode, int rc)
167 struct task_smack *tsp = current_security();
169 struct inode_smack *isp = inode->i_security;
170 char acc[SMK_NUM_ACCESS_TYPE + 1];
173 if (isp->smk_flags & SMK_INODE_IMPURE)
174 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
175 inode->i_sb->s_id, inode->i_ino, current->comm);
180 if (rc > SMACK_UNCONFINED_OBJECT)
182 if (rc == SMACK_UNCONFINED_SUBJECT &&
183 (mode & (MAY_WRITE | MAY_APPEND)))
184 isp->smk_flags |= SMK_INODE_IMPURE;
186 smk_bu_mode(mode, acc);
188 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
189 tsp->smk_task->smk_known, smk_of_inode(inode), acc,
190 inode->i_sb->s_id, inode->i_ino, current->comm);
195 #define smk_bu_inode(inode, mode, RC) (RC)
198 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
199 static int smk_bu_file(struct file *file, int mode, int rc)
202 struct task_smack *tsp = current_security();
203 struct smack_known *sskp = tsp->smk_task;
205 struct inode *inode = file->f_mapping->host;
206 struct inode_smack *isp = inode->i_security;
207 char acc[SMK_NUM_ACCESS_TYPE + 1];
209 if (isp->smk_flags & SMK_INODE_IMPURE)
210 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
211 inode->i_sb->s_id, inode->i_ino, current->comm);
215 if (rc > SMACK_UNCONFINED_OBJECT)
218 smk_bu_mode(mode, acc);
220 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
221 sskp->smk_known, (char *)file->f_security, acc,
222 inode->i_sb->s_id, inode->i_ino, file->f_dentry->d_name.name,
228 #define smk_bu_file(file, mode, RC) (RC)
231 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
232 #define smk_bu_credfile(cred, file, mode, RC) (RC)
236 * smk_fetch - Fetch the smack label from a file.
237 * @ip: a pointer to the inode
238 * @dp: a pointer to the dentry
240 * Returns a pointer to the master list entry for the Smack label
241 * or NULL if there was no label to fetch.
243 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
248 struct smack_known *skp = NULL;
250 if (ip->i_op->getxattr == NULL)
253 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
257 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
259 skp = smk_import_entry(buffer, rc);
267 * new_inode_smack - allocate an inode security blob
268 * @smack: a pointer to the Smack label to use in the blob
270 * Returns the new blob or NULL if there's no memory available
272 struct inode_smack *new_inode_smack(char *smack)
274 struct inode_smack *isp;
276 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
280 isp->smk_inode = smack;
282 mutex_init(&isp->smk_lock);
288 * new_task_smack - allocate a task security blob
289 * @smack: a pointer to the Smack label to use in the blob
291 * Returns the new blob or NULL if there's no memory available
293 static struct task_smack *new_task_smack(struct smack_known *task,
294 struct smack_known *forked, gfp_t gfp)
296 struct task_smack *tsp;
298 tsp = kzalloc(sizeof(struct task_smack), gfp);
302 tsp->smk_task = task;
303 tsp->smk_forked = forked;
304 INIT_LIST_HEAD(&tsp->smk_rules);
305 INIT_LIST_HEAD(&tsp->smk_relabel);
306 mutex_init(&tsp->smk_rules_lock);
312 * smk_copy_rules - copy a rule set
313 * @nhead - new rules header pointer
314 * @ohead - old rules header pointer
316 * Returns 0 on success, -ENOMEM on error
318 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
321 struct smack_rule *nrp;
322 struct smack_rule *orp;
325 INIT_LIST_HEAD(nhead);
327 list_for_each_entry_rcu(orp, ohead, list) {
328 nrp = kzalloc(sizeof(struct smack_rule), gfp);
334 list_add_rcu(&nrp->list, nhead);
340 * smk_copy_relabel - copy smk_relabel labels list
341 * @nhead: new rules header pointer
342 * @ohead: old rules header pointer
343 * @gfp: type of the memory for the allocation
345 * Returns 0 on success, -ENOMEM on error
347 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
350 struct smack_known_list_elem *nklep;
351 struct smack_known_list_elem *oklep;
353 INIT_LIST_HEAD(nhead);
355 list_for_each_entry(oklep, ohead, list) {
356 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
358 smk_destroy_label_list(nhead);
361 nklep->smk_label = oklep->smk_label;
362 list_add(&nklep->list, nhead);
369 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
370 * @mode - input mode in form of PTRACE_MODE_*
372 * Returns a converted MAY_* mode usable by smack rules
374 static inline unsigned int smk_ptrace_mode(unsigned int mode)
377 case PTRACE_MODE_READ:
379 case PTRACE_MODE_ATTACH:
380 return MAY_READWRITE;
387 * smk_ptrace_rule_check - helper for ptrace access
388 * @tracer: tracer process
389 * @tracee_label: label of the process that's about to be traced,
390 * the pointer must originate from smack structures
391 * @mode: ptrace attachment mode (PTRACE_MODE_*)
392 * @func: name of the function that called us, used for audit
394 * Returns 0 on access granted, -error on error
396 static int smk_ptrace_rule_check(struct task_struct *tracer, char *tracee_label,
397 unsigned int mode, const char *func)
400 struct smk_audit_info ad, *saip = NULL;
401 struct task_smack *tsp;
402 struct smack_known *skp;
404 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
405 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
406 smk_ad_setfield_u_tsk(&ad, tracer);
410 tsp = task_security(tracer);
411 skp = smk_of_task(tsp);
413 if ((mode & PTRACE_MODE_ATTACH) &&
414 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
415 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
416 if (skp->smk_known == tracee_label)
418 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
420 else if (capable(CAP_SYS_PTRACE))
426 smack_log(skp->smk_known, tracee_label, 0, rc, saip);
431 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
432 rc = smk_tskacc(tsp, tracee_label, smk_ptrace_mode(mode), saip);
438 * We he, that is fun!
442 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
443 * @ctp: child task pointer
444 * @mode: ptrace attachment mode (PTRACE_MODE_*)
446 * Returns 0 if access is OK, an error code otherwise
448 * Do the capability checks.
450 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
453 struct smack_known *skp;
455 rc = cap_ptrace_access_check(ctp, mode);
459 skp = smk_of_task(task_security(ctp));
461 rc = smk_ptrace_rule_check(current, skp->smk_known, mode, __func__);
466 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
467 * @ptp: parent task pointer
469 * Returns 0 if access is OK, an error code otherwise
471 * Do the capability checks, and require PTRACE_MODE_ATTACH.
473 static int smack_ptrace_traceme(struct task_struct *ptp)
476 struct smack_known *skp;
478 rc = cap_ptrace_traceme(ptp);
482 skp = smk_of_task(current_security());
484 rc = smk_ptrace_rule_check(ptp, skp->smk_known,
485 PTRACE_MODE_ATTACH, __func__);
491 * smack_syslog - Smack approval on syslog
492 * @type: message type
494 * Require that the task has the floor label
496 * Returns 0 on success, error code otherwise.
498 static int smack_syslog(int typefrom_file)
501 struct smack_known *skp = smk_of_current();
503 if (smack_privileged(CAP_MAC_OVERRIDE))
506 if (skp != &smack_known_floor)
518 * smack_sb_alloc_security - allocate a superblock blob
519 * @sb: the superblock getting the blob
521 * Returns 0 on success or -ENOMEM on error.
523 static int smack_sb_alloc_security(struct super_block *sb)
525 struct superblock_smack *sbsp;
527 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
532 sbsp->smk_root = smack_known_floor.smk_known;
533 sbsp->smk_default = smack_known_floor.smk_known;
534 sbsp->smk_floor = smack_known_floor.smk_known;
535 sbsp->smk_hat = smack_known_hat.smk_known;
537 * smk_initialized will be zero from kzalloc.
539 sb->s_security = sbsp;
545 * smack_sb_free_security - free a superblock blob
546 * @sb: the superblock getting the blob
549 static void smack_sb_free_security(struct super_block *sb)
551 kfree(sb->s_security);
552 sb->s_security = NULL;
556 * smack_sb_copy_data - copy mount options data for processing
557 * @orig: where to start
558 * @smackopts: mount options string
560 * Returns 0 on success or -ENOMEM on error.
562 * Copy the Smack specific mount options out of the mount
565 static int smack_sb_copy_data(char *orig, char *smackopts)
567 char *cp, *commap, *otheropts, *dp;
569 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
570 if (otheropts == NULL)
573 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
574 if (strstr(cp, SMK_FSDEFAULT) == cp)
576 else if (strstr(cp, SMK_FSFLOOR) == cp)
578 else if (strstr(cp, SMK_FSHAT) == cp)
580 else if (strstr(cp, SMK_FSROOT) == cp)
582 else if (strstr(cp, SMK_FSTRANS) == cp)
587 commap = strchr(cp, ',');
596 strcpy(orig, otheropts);
597 free_page((unsigned long)otheropts);
603 * smack_sb_kern_mount - Smack specific mount processing
604 * @sb: the file system superblock
605 * @flags: the mount flags
606 * @data: the smack mount options
608 * Returns 0 on success, an error code on failure
610 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
612 struct dentry *root = sb->s_root;
613 struct inode *inode = root->d_inode;
614 struct superblock_smack *sp = sb->s_security;
615 struct inode_smack *isp;
616 struct smack_known *skp;
624 if (sp->smk_initialized)
627 sp->smk_initialized = 1;
629 for (op = data; op != NULL; op = commap) {
630 commap = strchr(op, ',');
634 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
635 op += strlen(SMK_FSHAT);
636 nsp = smk_import(op, 0);
641 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
642 op += strlen(SMK_FSFLOOR);
643 nsp = smk_import(op, 0);
648 } else if (strncmp(op, SMK_FSDEFAULT,
649 strlen(SMK_FSDEFAULT)) == 0) {
650 op += strlen(SMK_FSDEFAULT);
651 nsp = smk_import(op, 0);
653 sp->smk_default = nsp;
656 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
657 op += strlen(SMK_FSROOT);
658 nsp = smk_import(op, 0);
663 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
664 op += strlen(SMK_FSTRANS);
665 nsp = smk_import(op, 0);
674 if (!smack_privileged(CAP_MAC_ADMIN)) {
676 * Unprivileged mounts don't get to specify Smack values.
681 * Unprivileged mounts get root and default from the caller.
683 skp = smk_of_current();
684 sp->smk_root = skp->smk_known;
685 sp->smk_default = skp->smk_known;
688 * Initialize the root inode.
690 isp = inode->i_security;
692 isp = new_inode_smack(sp->smk_root);
695 inode->i_security = isp;
697 isp->smk_inode = sp->smk_root;
700 if (inode->i_op && inode->i_op->setxattr) {
701 rc = inode->i_op->setxattr(root,
702 XATTR_NAME_SMACKTRANSMUTE, TRANS_TRUE,
705 isp->smk_flags |= SMK_INODE_TRANSMUTE;
713 * smack_sb_statfs - Smack check on statfs
714 * @dentry: identifies the file system in question
716 * Returns 0 if current can read the floor of the filesystem,
717 * and error code otherwise
719 static int smack_sb_statfs(struct dentry *dentry)
721 struct superblock_smack *sbp = dentry->d_sb->s_security;
723 struct smk_audit_info ad;
725 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
726 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
728 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
729 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
738 * smack_bprm_set_creds - set creds for exec
739 * @bprm: the exec information
741 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
743 static int smack_bprm_set_creds(struct linux_binprm *bprm)
745 struct inode *inode = file_inode(bprm->file);
746 struct task_smack *bsp = bprm->cred->security;
747 struct inode_smack *isp;
750 rc = cap_bprm_set_creds(bprm);
754 if (bprm->cred_prepared)
757 isp = inode->i_security;
758 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
761 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
762 struct task_struct *tracer;
766 tracer = ptrace_parent(current);
767 if (likely(tracer != NULL))
768 rc = smk_ptrace_rule_check(tracer,
769 isp->smk_task->smk_known,
776 } else if (bprm->unsafe)
779 bsp->smk_task = isp->smk_task;
780 bprm->per_clear |= PER_CLEAR_ON_SETID;
786 * smack_bprm_committing_creds - Prepare to install the new credentials
789 * @bprm: binprm for exec
791 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
793 struct task_smack *bsp = bprm->cred->security;
795 if (bsp->smk_task != bsp->smk_forked)
796 current->pdeath_signal = 0;
800 * smack_bprm_secureexec - Return the decision to use secureexec.
801 * @bprm: binprm for exec
803 * Returns 0 on success.
805 static int smack_bprm_secureexec(struct linux_binprm *bprm)
807 struct task_smack *tsp = current_security();
808 int ret = cap_bprm_secureexec(bprm);
810 if (!ret && (tsp->smk_task != tsp->smk_forked))
821 * smack_inode_alloc_security - allocate an inode blob
822 * @inode: the inode in need of a blob
824 * Returns 0 if it gets a blob, -ENOMEM otherwise
826 static int smack_inode_alloc_security(struct inode *inode)
828 struct smack_known *skp = smk_of_current();
830 inode->i_security = new_inode_smack(skp->smk_known);
831 if (inode->i_security == NULL)
837 * smack_inode_free_security - free an inode blob
838 * @inode: the inode with a blob
840 * Clears the blob pointer in inode
842 static void smack_inode_free_security(struct inode *inode)
844 kmem_cache_free(smack_inode_cache, inode->i_security);
845 inode->i_security = NULL;
849 * smack_inode_init_security - copy out the smack from an inode
853 * @name: where to put the attribute name
854 * @value: where to put the attribute value
855 * @len: where to put the length of the attribute
857 * Returns 0 if it all works out, -ENOMEM if there's no memory
859 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
860 const struct qstr *qstr, char **name,
861 void **value, size_t *len)
863 struct inode_smack *issp = inode->i_security;
864 struct smack_known *skp = smk_of_current();
865 char *isp = smk_of_inode(inode);
866 char *dsp = smk_of_inode(dir);
870 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_NOFS);
877 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
881 * If the access rule allows transmutation and
882 * the directory requests transmutation then
883 * by all means transmute.
884 * Mark the inode as changed.
886 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
887 smk_inode_transmutable(dir)) {
889 issp->smk_flags |= SMK_INODE_CHANGED;
892 *value = kstrdup(isp, GFP_NOFS);
898 *len = strlen(isp) + 1;
904 * smack_inode_link - Smack check on link
905 * @old_dentry: the existing object
907 * @new_dentry: the new object
909 * Returns 0 if access is permitted, an error code otherwise
911 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
912 struct dentry *new_dentry)
915 struct smk_audit_info ad;
918 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
919 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
921 isp = smk_of_inode(old_dentry->d_inode);
922 rc = smk_curacc(isp, MAY_WRITE, &ad);
923 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
925 if (rc == 0 && new_dentry->d_inode != NULL) {
926 isp = smk_of_inode(new_dentry->d_inode);
927 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
928 rc = smk_curacc(isp, MAY_WRITE, &ad);
929 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
936 * smack_inode_unlink - Smack check on inode deletion
937 * @dir: containing directory object
938 * @dentry: file to unlink
940 * Returns 0 if current can write the containing directory
941 * and the object, error code otherwise
943 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
945 struct inode *ip = dentry->d_inode;
946 struct smk_audit_info ad;
949 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
950 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
953 * You need write access to the thing you're unlinking
955 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
956 rc = smk_bu_inode(ip, MAY_WRITE, rc);
959 * You also need write access to the containing directory
961 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
962 smk_ad_setfield_u_fs_inode(&ad, dir);
963 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
964 rc = smk_bu_inode(dir, MAY_WRITE, rc);
970 * smack_inode_rmdir - Smack check on directory deletion
971 * @dir: containing directory object
972 * @dentry: directory to unlink
974 * Returns 0 if current can write the containing directory
975 * and the directory, error code otherwise
977 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
979 struct smk_audit_info ad;
982 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
983 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
986 * You need write access to the thing you're removing
988 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
989 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
992 * You also need write access to the containing directory
994 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
995 smk_ad_setfield_u_fs_inode(&ad, dir);
996 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
997 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1004 * smack_inode_rename - Smack check on rename
1005 * @old_inode: the old directory
1006 * @old_dentry: unused
1007 * @new_inode: the new directory
1008 * @new_dentry: unused
1010 * Read and write access is required on both the old and
1013 * Returns 0 if access is permitted, an error code otherwise
1015 static int smack_inode_rename(struct inode *old_inode,
1016 struct dentry *old_dentry,
1017 struct inode *new_inode,
1018 struct dentry *new_dentry)
1022 struct smk_audit_info ad;
1024 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1025 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1027 isp = smk_of_inode(old_dentry->d_inode);
1028 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1029 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
1031 if (rc == 0 && new_dentry->d_inode != NULL) {
1032 isp = smk_of_inode(new_dentry->d_inode);
1033 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1034 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1035 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
1041 * smack_inode_permission - Smack version of permission()
1042 * @inode: the inode in question
1043 * @mask: the access requested
1045 * This is the important Smack hook.
1047 * Returns 0 if access is permitted, -EACCES otherwise
1049 static int smack_inode_permission(struct inode *inode, int mask)
1051 struct smk_audit_info ad;
1052 int no_block = mask & MAY_NOT_BLOCK;
1055 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1057 * No permission to check. Existence test. Yup, it's there.
1062 /* May be droppable after audit */
1065 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1066 smk_ad_setfield_u_fs_inode(&ad, inode);
1067 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1068 rc = smk_bu_inode(inode, mask, rc);
1073 * smack_inode_setattr - Smack check for setting attributes
1074 * @dentry: the object
1075 * @iattr: for the force flag
1077 * Returns 0 if access is permitted, an error code otherwise
1079 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1081 struct smk_audit_info ad;
1085 * Need to allow for clearing the setuid bit.
1087 if (iattr->ia_valid & ATTR_FORCE)
1089 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1090 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1092 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1093 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1098 * smack_inode_getattr - Smack check for getting attributes
1100 * @dentry: the object
1102 * Returns 0 if access is permitted, an error code otherwise
1104 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1106 struct smk_audit_info ad;
1110 path.dentry = dentry;
1113 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1114 smk_ad_setfield_u_fs_path(&ad, path);
1115 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1116 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1121 * smack_inode_setxattr - Smack check for setting xattrs
1122 * @dentry: the object
1123 * @name: name of the attribute
1128 * This protects the Smack attribute explicitly.
1130 * Returns 0 if access is permitted, an error code otherwise
1132 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1133 const void *value, size_t size, int flags)
1135 struct smk_audit_info ad;
1138 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1139 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1140 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1141 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1142 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1143 if (!smack_privileged(CAP_MAC_ADMIN))
1146 * check label validity here so import wont fail on
1149 if (size == 0 || size >= SMK_LONGLABEL ||
1150 smk_import(value, size) == NULL)
1152 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1153 if (!smack_privileged(CAP_MAC_ADMIN))
1155 if (size != TRANS_TRUE_SIZE ||
1156 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1159 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1161 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1162 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1165 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1166 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1173 * smack_inode_post_setxattr - Apply the Smack update approved above
1175 * @name: attribute name
1176 * @value: attribute value
1177 * @size: attribute size
1180 * Set the pointer in the inode blob to the entry found
1181 * in the master label list.
1183 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1184 const void *value, size_t size, int flags)
1186 struct smack_known *skp;
1187 struct inode_smack *isp = dentry->d_inode->i_security;
1189 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1190 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1194 skp = smk_import_entry(value, size);
1195 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1197 isp->smk_inode = skp->smk_known;
1199 isp->smk_inode = smack_known_invalid.smk_known;
1200 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1202 isp->smk_task = skp;
1204 isp->smk_task = &smack_known_invalid;
1205 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1207 isp->smk_mmap = skp;
1209 isp->smk_mmap = &smack_known_invalid;
1216 * smack_inode_getxattr - Smack check on getxattr
1217 * @dentry: the object
1220 * Returns 0 if access is permitted, an error code otherwise
1222 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1224 struct smk_audit_info ad;
1227 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1228 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1230 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1231 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1236 * smack_inode_removexattr - Smack check on removexattr
1237 * @dentry: the object
1238 * @name: name of the attribute
1240 * Removing the Smack attribute requires CAP_MAC_ADMIN
1242 * Returns 0 if access is permitted, an error code otherwise
1244 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1246 struct inode_smack *isp;
1247 struct smk_audit_info ad;
1250 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1251 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1252 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1253 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1254 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1255 strcmp(name, XATTR_NAME_SMACKMMAP)) {
1256 if (!smack_privileged(CAP_MAC_ADMIN))
1259 rc = cap_inode_removexattr(dentry, name);
1264 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1265 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1267 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1268 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1272 isp = dentry->d_inode->i_security;
1274 * Don't do anything special for these.
1275 * XATTR_NAME_SMACKIPIN
1276 * XATTR_NAME_SMACKIPOUT
1277 * XATTR_NAME_SMACKEXEC
1279 if (strcmp(name, XATTR_NAME_SMACK) == 0)
1280 isp->smk_task = NULL;
1281 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1282 isp->smk_mmap = NULL;
1283 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1284 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1290 * smack_inode_getsecurity - get smack xattrs
1291 * @inode: the object
1292 * @name: attribute name
1293 * @buffer: where to put the result
1296 * Returns the size of the attribute or an error code
1298 static int smack_inode_getsecurity(const struct inode *inode,
1299 const char *name, void **buffer,
1302 struct socket_smack *ssp;
1303 struct socket *sock;
1304 struct super_block *sbp;
1305 struct inode *ip = (struct inode *)inode;
1310 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1311 isp = smk_of_inode(inode);
1312 ilen = strlen(isp) + 1;
1318 * The rest of the Smack xattrs are only on sockets.
1321 if (sbp->s_magic != SOCKFS_MAGIC)
1324 sock = SOCKET_I(ip);
1325 if (sock == NULL || sock->sk == NULL)
1328 ssp = sock->sk->sk_security;
1330 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1331 isp = ssp->smk_in->smk_known;
1332 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1333 isp = ssp->smk_out->smk_known;
1337 ilen = strlen(isp) + 1;
1348 * smack_inode_listsecurity - list the Smack attributes
1349 * @inode: the object
1350 * @buffer: where they go
1351 * @buffer_size: size of buffer
1353 * Returns 0 on success, -EINVAL otherwise
1355 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1358 int len = strlen(XATTR_NAME_SMACK);
1360 if (buffer != NULL && len <= buffer_size) {
1361 memcpy(buffer, XATTR_NAME_SMACK, len);
1368 * smack_inode_getsecid - Extract inode's security id
1369 * @inode: inode to extract the info from
1370 * @secid: where result will be saved
1372 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1374 struct inode_smack *isp = inode->i_security;
1376 *secid = smack_to_secid(isp->smk_inode);
1384 * smack_file_permission - Smack check on file operations
1390 * Should access checks be done on each read or write?
1391 * UNICOS and SELinux say yes.
1392 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1394 * I'll say no for now. Smack does not do the frequent
1395 * label changing that SELinux does.
1397 static int smack_file_permission(struct file *file, int mask)
1403 * smack_file_alloc_security - assign a file security blob
1406 * The security blob for a file is a pointer to the master
1407 * label list, so no allocation is done.
1411 static int smack_file_alloc_security(struct file *file)
1413 struct smack_known *skp = smk_of_current();
1415 file->f_security = skp->smk_known;
1420 * smack_file_free_security - clear a file security blob
1423 * The security blob for a file is a pointer to the master
1424 * label list, so no memory is freed.
1426 static void smack_file_free_security(struct file *file)
1428 file->f_security = NULL;
1432 * smack_file_ioctl - Smack check on ioctls
1437 * Relies heavily on the correct use of the ioctl command conventions.
1439 * Returns 0 if allowed, error code otherwise
1441 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1445 struct smk_audit_info ad;
1447 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1448 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1450 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1451 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1452 rc = smk_bu_file(file, MAY_WRITE, rc);
1455 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1456 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1457 rc = smk_bu_file(file, MAY_READ, rc);
1464 * smack_file_lock - Smack check on file locking
1468 * Returns 0 if current has lock access, error code otherwise
1470 static int smack_file_lock(struct file *file, unsigned int cmd)
1472 struct smk_audit_info ad;
1475 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1476 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1477 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1478 rc = smk_bu_file(file, MAY_LOCK, rc);
1483 * smack_file_fcntl - Smack check on fcntl
1485 * @cmd: what action to check
1488 * Generally these operations are harmless.
1489 * File locking operations present an obvious mechanism
1490 * for passing information, so they require write access.
1492 * Returns 0 if current has access, error code otherwise
1494 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1497 struct smk_audit_info ad;
1506 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1507 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1508 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1509 rc = smk_bu_file(file, MAY_LOCK, rc);
1513 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1514 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1515 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1516 rc = smk_bu_file(file, MAY_WRITE, rc);
1527 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1528 * if mapping anonymous memory.
1529 * @file contains the file structure for file to map (may be NULL).
1530 * @reqprot contains the protection requested by the application.
1531 * @prot contains the protection that will be applied by the kernel.
1532 * @flags contains the operational flags.
1533 * Return 0 if permission is granted.
1535 static int smack_mmap_file(struct file *file,
1536 unsigned long reqprot, unsigned long prot,
1537 unsigned long flags)
1539 struct smack_known *skp;
1540 struct smack_known *mkp;
1541 struct smack_rule *srp;
1542 struct task_smack *tsp;
1544 struct inode_smack *isp;
1553 isp = file_inode(file)->i_security;
1554 if (isp->smk_mmap == NULL)
1556 mkp = isp->smk_mmap;
1558 tsp = current_security();
1559 skp = smk_of_current();
1564 * For each Smack rule associated with the subject
1565 * label verify that the SMACK64MMAP also has access
1566 * to that rule's object label.
1568 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1569 osmack = srp->smk_object;
1571 * Matching labels always allows access.
1573 if (mkp->smk_known == osmack)
1576 * If there is a matching local rule take
1577 * that into account as well.
1579 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1582 may = srp->smk_access;
1584 may &= srp->smk_access;
1586 * If may is zero the SMACK64MMAP subject can't
1587 * possibly have less access.
1593 * Fetch the global list entry.
1594 * If there isn't one a SMACK64MMAP subject
1595 * can't have as much access as current.
1597 mmay = smk_access_entry(mkp->smk_known, osmack,
1599 if (mmay == -ENOENT) {
1604 * If there is a local entry it modifies the
1605 * potential access, too.
1607 tmay = smk_access_entry(mkp->smk_known, osmack,
1609 if (tmay != -ENOENT)
1613 * If there is any access available to current that is
1614 * not available to a SMACK64MMAP subject
1617 if ((may | mmay) != mmay) {
1629 * smack_file_set_fowner - set the file security blob value
1630 * @file: object in question
1633 * Further research may be required on this one.
1635 static int smack_file_set_fowner(struct file *file)
1637 struct smack_known *skp = smk_of_current();
1639 file->f_security = skp->smk_known;
1644 * smack_file_send_sigiotask - Smack on sigio
1645 * @tsk: The target task
1646 * @fown: the object the signal come from
1649 * Allow a privileged task to get signals even if it shouldn't
1651 * Returns 0 if a subject with the object's smack could
1652 * write to the task, an error code otherwise.
1654 static int smack_file_send_sigiotask(struct task_struct *tsk,
1655 struct fown_struct *fown, int signum)
1657 struct smack_known *skp;
1658 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1661 struct smk_audit_info ad;
1664 * struct fown_struct is never outside the context of a struct file
1666 file = container_of(fown, struct file, f_owner);
1668 /* we don't log here as rc can be overriden */
1669 skp = smk_find_entry(file->f_security);
1670 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1671 rc = smk_bu_note("sigiotask", skp, tkp->smk_known, MAY_WRITE, rc);
1672 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1675 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1676 smk_ad_setfield_u_tsk(&ad, tsk);
1677 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1682 * smack_file_receive - Smack file receive check
1685 * Returns 0 if current has access, error code otherwise
1687 static int smack_file_receive(struct file *file)
1691 struct smk_audit_info ad;
1692 struct inode *inode = file_inode(file);
1693 struct socket *sock;
1694 struct task_smack *tsp;
1695 struct socket_smack *ssp;
1697 if (unlikely(IS_PRIVATE(file_inode(file))))
1700 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1701 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1703 if (S_ISSOCK(inode->i_mode)) {
1704 sock = SOCKET_I(inode);
1705 ssp = sock->sk->sk_security;
1706 tsp = current_security();
1708 * If the receiving process can't write to the
1709 * passed socket or if the passed socket can't
1710 * write to the receiving process don't accept
1711 * the passed socket.
1713 rc = smk_access(tsp->smk_task, ssp->smk_out->smk_known, MAY_WRITE, &ad);
1716 rc = smk_access(ssp->smk_in, tsp->smk_task->smk_known, MAY_WRITE, &ad);
1720 * This code relies on bitmasks.
1722 if (file->f_mode & FMODE_READ)
1724 if (file->f_mode & FMODE_WRITE)
1727 rc = smk_curacc(file->f_security, may, &ad);
1728 rc = smk_bu_file(file, may, rc);
1733 * smack_file_open - Smack dentry open processing
1737 * Set the security blob in the file structure.
1741 static int smack_file_open(struct file *file, const struct cred *cred)
1743 struct inode_smack *isp = file_inode(file)->i_security;
1745 file->f_security = isp->smk_inode;
1755 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1756 * @new: the new credentials
1757 * @gfp: the atomicity of any memory allocations
1759 * Prepare a blank set of credentials for modification. This must allocate all
1760 * the memory the LSM module might require such that cred_transfer() can
1761 * complete without error.
1763 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1765 struct task_smack *tsp;
1767 tsp = new_task_smack(NULL, NULL, gfp);
1771 cred->security = tsp;
1778 * smack_cred_free - "free" task-level security credentials
1779 * @cred: the credentials in question
1782 static void smack_cred_free(struct cred *cred)
1784 struct task_smack *tsp = cred->security;
1785 struct smack_rule *rp;
1786 struct list_head *l;
1787 struct list_head *n;
1791 cred->security = NULL;
1793 smk_destroy_label_list(&tsp->smk_relabel);
1795 list_for_each_safe(l, n, &tsp->smk_rules) {
1796 rp = list_entry(l, struct smack_rule, list);
1797 list_del(&rp->list);
1804 * smack_cred_prepare - prepare new set of credentials for modification
1805 * @new: the new credentials
1806 * @old: the original credentials
1807 * @gfp: the atomicity of any memory allocations
1809 * Prepare a new set of credentials for modification.
1811 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1814 struct task_smack *old_tsp = old->security;
1815 struct task_smack *new_tsp;
1818 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1819 if (new_tsp == NULL)
1822 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1826 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1831 new->security = new_tsp;
1836 * smack_cred_transfer - Transfer the old credentials to the new credentials
1837 * @new: the new credentials
1838 * @old: the original credentials
1840 * Fill in a set of blank credentials from another set of credentials.
1842 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1844 struct task_smack *old_tsp = old->security;
1845 struct task_smack *new_tsp = new->security;
1847 new_tsp->smk_task = old_tsp->smk_task;
1848 new_tsp->smk_forked = old_tsp->smk_task;
1849 mutex_init(&new_tsp->smk_rules_lock);
1850 INIT_LIST_HEAD(&new_tsp->smk_rules);
1853 /* cbs copy rule list */
1857 * smack_kernel_act_as - Set the subjective context in a set of credentials
1858 * @new: points to the set of credentials to be modified.
1859 * @secid: specifies the security ID to be set
1861 * Set the security data for a kernel service.
1863 static int smack_kernel_act_as(struct cred *new, u32 secid)
1865 struct task_smack *new_tsp = new->security;
1866 struct smack_known *skp = smack_from_secid(secid);
1871 new_tsp->smk_task = skp;
1876 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1877 * @new: points to the set of credentials to be modified
1878 * @inode: points to the inode to use as a reference
1880 * Set the file creation context in a set of credentials to the same
1881 * as the objective context of the specified inode
1883 static int smack_kernel_create_files_as(struct cred *new,
1884 struct inode *inode)
1886 struct inode_smack *isp = inode->i_security;
1887 struct task_smack *tsp = new->security;
1889 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1890 tsp->smk_task = tsp->smk_forked;
1895 * smk_curacc_on_task - helper to log task related access
1896 * @p: the task object
1897 * @access: the access requested
1898 * @caller: name of the calling function for audit
1900 * Return 0 if access is permitted
1902 static int smk_curacc_on_task(struct task_struct *p, int access,
1905 struct smk_audit_info ad;
1906 struct smack_known *skp = smk_of_task(task_security(p));
1909 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1910 smk_ad_setfield_u_tsk(&ad, p);
1911 rc = smk_curacc(skp->smk_known, access, &ad);
1912 rc = smk_bu_task(p, access, rc);
1917 * smack_task_setpgid - Smack check on setting pgid
1918 * @p: the task object
1921 * Return 0 if write access is permitted
1923 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1925 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1929 * smack_task_getpgid - Smack access check for getpgid
1930 * @p: the object task
1932 * Returns 0 if current can read the object task, error code otherwise
1934 static int smack_task_getpgid(struct task_struct *p)
1936 return smk_curacc_on_task(p, MAY_READ, __func__);
1940 * smack_task_getsid - Smack access check for getsid
1941 * @p: the object task
1943 * Returns 0 if current can read the object task, error code otherwise
1945 static int smack_task_getsid(struct task_struct *p)
1947 return smk_curacc_on_task(p, MAY_READ, __func__);
1951 * smack_task_getsecid - get the secid of the task
1952 * @p: the object task
1953 * @secid: where to put the result
1955 * Sets the secid to contain a u32 version of the smack label.
1957 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1959 struct smack_known *skp = smk_of_task(task_security(p));
1961 *secid = skp->smk_secid;
1965 * smack_task_setnice - Smack check on setting nice
1966 * @p: the task object
1969 * Return 0 if write access is permitted
1971 static int smack_task_setnice(struct task_struct *p, int nice)
1975 rc = cap_task_setnice(p, nice);
1977 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1982 * smack_task_setioprio - Smack check on setting ioprio
1983 * @p: the task object
1986 * Return 0 if write access is permitted
1988 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1992 rc = cap_task_setioprio(p, ioprio);
1994 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1999 * smack_task_getioprio - Smack check on reading ioprio
2000 * @p: the task object
2002 * Return 0 if read access is permitted
2004 static int smack_task_getioprio(struct task_struct *p)
2006 return smk_curacc_on_task(p, MAY_READ, __func__);
2010 * smack_task_setscheduler - Smack check on setting scheduler
2011 * @p: the task object
2015 * Return 0 if read access is permitted
2017 static int smack_task_setscheduler(struct task_struct *p)
2021 rc = cap_task_setscheduler(p);
2023 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
2028 * smack_task_getscheduler - Smack check on reading scheduler
2029 * @p: the task object
2031 * Return 0 if read access is permitted
2033 static int smack_task_getscheduler(struct task_struct *p)
2035 return smk_curacc_on_task(p, MAY_READ, __func__);
2039 * smack_task_movememory - Smack check on moving memory
2040 * @p: the task object
2042 * Return 0 if write access is permitted
2044 static int smack_task_movememory(struct task_struct *p)
2046 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2050 * smack_task_kill - Smack check on signal delivery
2051 * @p: the task object
2054 * @secid: identifies the smack to use in lieu of current's
2056 * Return 0 if write access is permitted
2058 * The secid behavior is an artifact of an SELinux hack
2059 * in the USB code. Someday it may go away.
2061 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2064 struct smk_audit_info ad;
2065 struct smack_known *skp;
2066 struct smack_known *tkp = smk_of_task(task_security(p));
2069 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2070 smk_ad_setfield_u_tsk(&ad, p);
2072 * Sending a signal requires that the sender
2073 * can write the receiver.
2076 rc = smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
2077 rc = smk_bu_task(p, MAY_WRITE, rc);
2081 * If the secid isn't 0 we're dealing with some USB IO
2082 * specific behavior. This is not clean. For one thing
2083 * we can't take privilege into account.
2085 skp = smack_from_secid(secid);
2086 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
2087 rc = smk_bu_note("USB signal", skp, tkp->smk_known, MAY_WRITE, rc);
2092 * smack_task_wait - Smack access check for waiting
2093 * @p: task to wait for
2097 static int smack_task_wait(struct task_struct *p)
2100 * Allow the operation to succeed.
2102 * In userless environments (e.g. phones) programs
2103 * get marked with SMACK64EXEC and even if the parent
2104 * and child shouldn't be talking the parent still
2105 * may expect to know when the child exits.
2111 * smack_task_to_inode - copy task smack into the inode blob
2112 * @p: task to copy from
2113 * @inode: inode to copy to
2115 * Sets the smack pointer in the inode security blob
2117 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2119 struct inode_smack *isp = inode->i_security;
2120 struct smack_known *skp = smk_of_task(task_security(p));
2122 isp->smk_inode = skp->smk_known;
2130 * smack_sk_alloc_security - Allocate a socket blob
2133 * @gfp_flags: memory allocation flags
2135 * Assign Smack pointers to current
2137 * Returns 0 on success, -ENOMEM is there's no memory
2139 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2141 struct smack_known *skp = smk_of_current();
2142 struct socket_smack *ssp;
2144 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2149 * Sockets created by kernel threads receive web label.
2151 if (unlikely(current->flags & PF_KTHREAD)) {
2152 ssp->smk_in = &smack_known_web;
2153 ssp->smk_out = &smack_known_web;
2158 ssp->smk_packet = NULL;
2160 sk->sk_security = ssp;
2166 * smack_sk_free_security - Free a socket blob
2169 * Clears the blob pointer
2171 static void smack_sk_free_security(struct sock *sk)
2173 kfree(sk->sk_security);
2177 * smack_host_label - check host based restrictions
2178 * @sip: the object end
2180 * looks for host based access restrictions
2182 * This version will only be appropriate for really small sets of single label
2183 * hosts. The caller is responsible for ensuring that the RCU read lock is
2184 * taken before calling this function.
2186 * Returns the label of the far end or NULL if it's not special.
2188 static char *smack_host_label(struct sockaddr_in *sip)
2190 struct smk_netlbladdr *snp;
2191 struct in_addr *siap = &sip->sin_addr;
2193 if (siap->s_addr == 0)
2196 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2198 * we break after finding the first match because
2199 * the list is sorted from longest to shortest mask
2200 * so we have found the most specific match
2202 if ((&snp->smk_host.sin_addr)->s_addr ==
2203 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2204 /* we have found the special CIPSO option */
2205 if (snp->smk_label == smack_cipso_option)
2207 return snp->smk_label;
2214 * smack_netlabel - Set the secattr on a socket
2216 * @labeled: socket label scheme
2218 * Convert the outbound smack value (smk_out) to a
2219 * secattr and attach it to the socket.
2221 * Returns 0 on success or an error code
2223 static int smack_netlabel(struct sock *sk, int labeled)
2225 struct smack_known *skp;
2226 struct socket_smack *ssp = sk->sk_security;
2230 * Usually the netlabel code will handle changing the
2231 * packet labeling based on the label.
2232 * The case of a single label host is different, because
2233 * a single label host should never get a labeled packet
2234 * even though the label is usually associated with a packet
2238 bh_lock_sock_nested(sk);
2240 if (ssp->smk_out == smack_net_ambient ||
2241 labeled == SMACK_UNLABELED_SOCKET)
2242 netlbl_sock_delattr(sk);
2245 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2255 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2257 * @sap: the destination address
2259 * Set the correct secattr for the given socket based on the destination
2260 * address and perform any outbound access checks needed.
2262 * Returns 0 on success or an error code.
2265 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2267 struct smack_known *skp;
2271 struct socket_smack *ssp = sk->sk_security;
2272 struct smk_audit_info ad;
2275 hostsp = smack_host_label(sap);
2276 if (hostsp != NULL) {
2278 struct lsm_network_audit net;
2280 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2281 ad.a.u.net->family = sap->sin_family;
2282 ad.a.u.net->dport = sap->sin_port;
2283 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2285 sk_lbl = SMACK_UNLABELED_SOCKET;
2287 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
2288 rc = smk_bu_note("IPv4 host check", skp, hostsp, MAY_WRITE, rc);
2290 sk_lbl = SMACK_CIPSO_SOCKET;
2297 return smack_netlabel(sk, sk_lbl);
2301 * smk_ipv6_port_label - Smack port access table management
2305 * Create or update the port list entry
2307 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2309 struct sock *sk = sock->sk;
2310 struct sockaddr_in6 *addr6;
2311 struct socket_smack *ssp = sock->sk->sk_security;
2312 struct smk_port_label *spp;
2313 unsigned short port = 0;
2315 if (address == NULL) {
2317 * This operation is changing the Smack information
2318 * on the bound socket. Take the changes to the port
2321 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2322 if (sk != spp->smk_sock)
2324 spp->smk_in = ssp->smk_in;
2325 spp->smk_out = ssp->smk_out;
2329 * A NULL address is only used for updating existing
2330 * bound entries. If there isn't one, it's OK.
2335 addr6 = (struct sockaddr_in6 *)address;
2336 port = ntohs(addr6->sin6_port);
2338 * This is a special case that is safely ignored.
2344 * Look for an existing port list entry.
2345 * This is an indication that a port is getting reused.
2347 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2348 if (spp->smk_port != port)
2350 spp->smk_port = port;
2352 spp->smk_in = ssp->smk_in;
2353 spp->smk_out = ssp->smk_out;
2358 * A new port entry is required.
2360 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2364 spp->smk_port = port;
2366 spp->smk_in = ssp->smk_in;
2367 spp->smk_out = ssp->smk_out;
2369 list_add(&spp->list, &smk_ipv6_port_list);
2374 * smk_ipv6_port_check - check Smack port access
2378 * Create or update the port list entry
2380 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2385 struct smk_port_label *spp;
2386 struct socket_smack *ssp = sk->sk_security;
2387 struct smack_known *skp;
2388 unsigned short port = 0;
2390 struct smk_audit_info ad;
2393 struct lsm_network_audit net;
2396 if (act == SMK_RECEIVING) {
2397 skp = smack_net_ambient;
2398 object = ssp->smk_in->smk_known;
2401 object = smack_net_ambient->smk_known;
2405 * Get the IP address and port from the address.
2407 port = ntohs(address->sin6_port);
2408 bep = (__be16 *)(&address->sin6_addr);
2409 be32p = (__be32 *)(&address->sin6_addr);
2412 * It's remote, so port lookup does no good.
2414 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2418 * It's local so the send check has to have passed.
2420 if (act == SMK_RECEIVING) {
2421 skp = &smack_known_web;
2425 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2426 if (spp->smk_port != port)
2428 object = spp->smk_in->smk_known;
2429 if (act == SMK_CONNECTING)
2430 ssp->smk_packet = spp->smk_out;
2437 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2438 ad.a.u.net->family = sk->sk_family;
2439 ad.a.u.net->dport = port;
2440 if (act == SMK_RECEIVING)
2441 ad.a.u.net->v6info.saddr = address->sin6_addr;
2443 ad.a.u.net->v6info.daddr = address->sin6_addr;
2445 rc = smk_access(skp, object, MAY_WRITE, &ad);
2446 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2451 * smack_inode_setsecurity - set smack xattrs
2452 * @inode: the object
2453 * @name: attribute name
2454 * @value: attribute value
2455 * @size: size of the attribute
2458 * Sets the named attribute in the appropriate blob
2460 * Returns 0 on success, or an error code
2462 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2463 const void *value, size_t size, int flags)
2465 struct smack_known *skp;
2466 struct inode_smack *nsp = inode->i_security;
2467 struct socket_smack *ssp;
2468 struct socket *sock;
2471 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2474 skp = smk_import_entry(value, size);
2478 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2479 nsp->smk_inode = skp->smk_known;
2480 nsp->smk_flags |= SMK_INODE_INSTANT;
2484 * The rest of the Smack xattrs are only on sockets.
2486 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2489 sock = SOCKET_I(inode);
2490 if (sock == NULL || sock->sk == NULL)
2493 ssp = sock->sk->sk_security;
2495 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2497 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2499 if (sock->sk->sk_family == PF_INET) {
2500 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2503 "Smack: \"%s\" netlbl error %d.\n",
2509 if (sock->sk->sk_family == PF_INET6)
2510 smk_ipv6_port_label(sock, NULL);
2516 * smack_socket_post_create - finish socket setup
2518 * @family: protocol family
2523 * Sets the netlabel information on the socket
2525 * Returns 0 on success, and error code otherwise
2527 static int smack_socket_post_create(struct socket *sock, int family,
2528 int type, int protocol, int kern)
2530 struct socket_smack *ssp;
2532 if (sock->sk == NULL)
2536 * Sockets created by kernel threads receive web label.
2538 if (unlikely(current->flags & PF_KTHREAD)) {
2539 ssp = sock->sk->sk_security;
2540 ssp->smk_in = &smack_known_web;
2542 if (family != PF_INET)
2545 * Set the outbound netlbl.
2547 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2551 * smack_socket_bind - record port binding information.
2553 * @address: the port address
2554 * @addrlen: size of the address
2556 * Records the label bound to a port.
2560 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2563 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2564 smk_ipv6_port_label(sock, address);
2570 * smack_socket_connect - connect access check
2572 * @sap: the other end
2573 * @addrlen: size of sap
2575 * Verifies that a connection may be possible
2577 * Returns 0 on success, and error code otherwise
2579 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2584 if (sock->sk == NULL)
2587 switch (sock->sk->sk_family) {
2589 if (addrlen < sizeof(struct sockaddr_in))
2591 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2594 if (addrlen < sizeof(struct sockaddr_in6))
2596 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2604 * smack_flags_to_may - convert S_ to MAY_ values
2605 * @flags: the S_ value
2607 * Returns the equivalent MAY_ value
2609 static int smack_flags_to_may(int flags)
2613 if (flags & S_IRUGO)
2615 if (flags & S_IWUGO)
2617 if (flags & S_IXUGO)
2624 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2629 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2631 struct smack_known *skp = smk_of_current();
2633 msg->security = skp->smk_known;
2638 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2641 * Clears the blob pointer
2643 static void smack_msg_msg_free_security(struct msg_msg *msg)
2645 msg->security = NULL;
2649 * smack_of_shm - the smack pointer for the shm
2652 * Returns a pointer to the smack value
2654 static char *smack_of_shm(struct shmid_kernel *shp)
2656 return (char *)shp->shm_perm.security;
2660 * smack_shm_alloc_security - Set the security blob for shm
2665 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2667 struct kern_ipc_perm *isp = &shp->shm_perm;
2668 struct smack_known *skp = smk_of_current();
2670 isp->security = skp->smk_known;
2675 * smack_shm_free_security - Clear the security blob for shm
2678 * Clears the blob pointer
2680 static void smack_shm_free_security(struct shmid_kernel *shp)
2682 struct kern_ipc_perm *isp = &shp->shm_perm;
2684 isp->security = NULL;
2688 * smk_curacc_shm : check if current has access on shm
2690 * @access : access requested
2692 * Returns 0 if current has the requested access, error code otherwise
2694 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2696 char *ssp = smack_of_shm(shp);
2697 struct smk_audit_info ad;
2701 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2702 ad.a.u.ipc_id = shp->shm_perm.id;
2704 rc = smk_curacc(ssp, access, &ad);
2705 rc = smk_bu_current("shm", ssp, access, rc);
2710 * smack_shm_associate - Smack access check for shm
2712 * @shmflg: access requested
2714 * Returns 0 if current has the requested access, error code otherwise
2716 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2720 may = smack_flags_to_may(shmflg);
2721 return smk_curacc_shm(shp, may);
2725 * smack_shm_shmctl - Smack access check for shm
2727 * @cmd: what it wants to do
2729 * Returns 0 if current has the requested access, error code otherwise
2731 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2744 may = MAY_READWRITE;
2749 * System level information.
2755 return smk_curacc_shm(shp, may);
2759 * smack_shm_shmat - Smack access for shmat
2762 * @shmflg: access requested
2764 * Returns 0 if current has the requested access, error code otherwise
2766 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2771 may = smack_flags_to_may(shmflg);
2772 return smk_curacc_shm(shp, may);
2776 * smack_of_sem - the smack pointer for the sem
2779 * Returns a pointer to the smack value
2781 static char *smack_of_sem(struct sem_array *sma)
2783 return (char *)sma->sem_perm.security;
2787 * smack_sem_alloc_security - Set the security blob for sem
2792 static int smack_sem_alloc_security(struct sem_array *sma)
2794 struct kern_ipc_perm *isp = &sma->sem_perm;
2795 struct smack_known *skp = smk_of_current();
2797 isp->security = skp->smk_known;
2802 * smack_sem_free_security - Clear the security blob for sem
2805 * Clears the blob pointer
2807 static void smack_sem_free_security(struct sem_array *sma)
2809 struct kern_ipc_perm *isp = &sma->sem_perm;
2811 isp->security = NULL;
2815 * smk_curacc_sem : check if current has access on sem
2817 * @access : access requested
2819 * Returns 0 if current has the requested access, error code otherwise
2821 static int smk_curacc_sem(struct sem_array *sma, int access)
2823 char *ssp = smack_of_sem(sma);
2824 struct smk_audit_info ad;
2828 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2829 ad.a.u.ipc_id = sma->sem_perm.id;
2831 rc = smk_curacc(ssp, access, &ad);
2832 rc = smk_bu_current("sem", ssp, access, rc);
2837 * smack_sem_associate - Smack access check for sem
2839 * @semflg: access requested
2841 * Returns 0 if current has the requested access, error code otherwise
2843 static int smack_sem_associate(struct sem_array *sma, int semflg)
2847 may = smack_flags_to_may(semflg);
2848 return smk_curacc_sem(sma, may);
2852 * smack_sem_shmctl - Smack access check for sem
2854 * @cmd: what it wants to do
2856 * Returns 0 if current has the requested access, error code otherwise
2858 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2876 may = MAY_READWRITE;
2881 * System level information
2888 return smk_curacc_sem(sma, may);
2892 * smack_sem_semop - Smack checks of semaphore operations
2898 * Treated as read and write in all cases.
2900 * Returns 0 if access is allowed, error code otherwise
2902 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2903 unsigned nsops, int alter)
2905 return smk_curacc_sem(sma, MAY_READWRITE);
2909 * smack_msg_alloc_security - Set the security blob for msg
2914 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2916 struct kern_ipc_perm *kisp = &msq->q_perm;
2917 struct smack_known *skp = smk_of_current();
2919 kisp->security = skp->smk_known;
2924 * smack_msg_free_security - Clear the security blob for msg
2927 * Clears the blob pointer
2929 static void smack_msg_queue_free_security(struct msg_queue *msq)
2931 struct kern_ipc_perm *kisp = &msq->q_perm;
2933 kisp->security = NULL;
2937 * smack_of_msq - the smack pointer for the msq
2940 * Returns a pointer to the smack value
2942 static char *smack_of_msq(struct msg_queue *msq)
2944 return (char *)msq->q_perm.security;
2948 * smk_curacc_msq : helper to check if current has access on msq
2950 * @access : access requested
2952 * return 0 if current has access, error otherwise
2954 static int smk_curacc_msq(struct msg_queue *msq, int access)
2956 char *msp = smack_of_msq(msq);
2957 struct smk_audit_info ad;
2961 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2962 ad.a.u.ipc_id = msq->q_perm.id;
2964 rc = smk_curacc(msp, access, &ad);
2965 rc = smk_bu_current("msq", msp, access, rc);
2970 * smack_msg_queue_associate - Smack access check for msg_queue
2972 * @msqflg: access requested
2974 * Returns 0 if current has the requested access, error code otherwise
2976 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2980 may = smack_flags_to_may(msqflg);
2981 return smk_curacc_msq(msq, may);
2985 * smack_msg_queue_msgctl - Smack access check for msg_queue
2987 * @cmd: what it wants to do
2989 * Returns 0 if current has the requested access, error code otherwise
2991 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
3002 may = MAY_READWRITE;
3007 * System level information
3014 return smk_curacc_msq(msq, may);
3018 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3021 * @msqflg: access requested
3023 * Returns 0 if current has the requested access, error code otherwise
3025 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
3030 may = smack_flags_to_may(msqflg);
3031 return smk_curacc_msq(msq, may);
3035 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3042 * Returns 0 if current has read and write access, error code otherwise
3044 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3045 struct task_struct *target, long type, int mode)
3047 return smk_curacc_msq(msq, MAY_READWRITE);
3051 * smack_ipc_permission - Smack access for ipc_permission()
3052 * @ipp: the object permissions
3053 * @flag: access requested
3055 * Returns 0 if current has read and write access, error code otherwise
3057 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3059 char *isp = ipp->security;
3060 int may = smack_flags_to_may(flag);
3061 struct smk_audit_info ad;
3065 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3066 ad.a.u.ipc_id = ipp->id;
3068 rc = smk_curacc(isp, may, &ad);
3069 rc = smk_bu_current("svipc", isp, may, rc);
3074 * smack_ipc_getsecid - Extract smack security id
3075 * @ipp: the object permissions
3076 * @secid: where result will be saved
3078 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3080 char *smack = ipp->security;
3082 *secid = smack_to_secid(smack);
3086 * smack_d_instantiate - Make sure the blob is correct on an inode
3087 * @opt_dentry: dentry where inode will be attached
3088 * @inode: the object
3090 * Set the inode's security blob if it hasn't been done already.
3092 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3094 struct super_block *sbp;
3095 struct superblock_smack *sbsp;
3096 struct inode_smack *isp;
3097 struct smack_known *skp;
3098 struct smack_known *ckp = smk_of_current();
3100 char trattr[TRANS_TRUE_SIZE];
3108 isp = inode->i_security;
3110 mutex_lock(&isp->smk_lock);
3112 * If the inode is already instantiated
3113 * take the quick way out
3115 if (isp->smk_flags & SMK_INODE_INSTANT)
3119 sbsp = sbp->s_security;
3121 * We're going to use the superblock default label
3122 * if there's no label on the file.
3124 final = sbsp->smk_default;
3127 * If this is the root inode the superblock
3128 * may be in the process of initialization.
3129 * If that is the case use the root value out
3130 * of the superblock.
3132 if (opt_dentry->d_parent == opt_dentry) {
3133 switch (sbp->s_magic) {
3134 case CGROUP_SUPER_MAGIC:
3136 * The cgroup filesystem is never mounted,
3137 * so there's no opportunity to set the mount
3140 sbsp->smk_root = smack_known_star.smk_known;
3141 sbsp->smk_default = smack_known_star.smk_known;
3142 isp->smk_inode = sbsp->smk_root;
3146 * What about shmem/tmpfs anonymous files with dentry
3147 * obtained from d_alloc_pseudo()?
3149 isp->smk_inode = smk_of_current()->smk_known;
3152 isp->smk_inode = smk_of_current()->smk_known;
3155 isp->smk_inode = sbsp->smk_root;
3158 isp->smk_flags |= SMK_INODE_INSTANT;
3163 * This is pretty hackish.
3164 * Casey says that we shouldn't have to do
3165 * file system specific code, but it does help
3166 * with keeping it simple.
3168 switch (sbp->s_magic) {
3172 case CGROUP_SUPER_MAGIC:
3174 * Casey says that it's a little embarrassing
3175 * that the smack file system doesn't do
3176 * extended attributes.
3178 * Casey says pipes are easy (?)
3180 * Socket access is controlled by the socket
3181 * structures associated with the task involved.
3183 * Cgroupfs is special
3185 final = smack_known_star.smk_known;
3187 case DEVPTS_SUPER_MAGIC:
3189 * devpts seems content with the label of the task.
3190 * Programs that change smack have to treat the
3193 final = ckp->smk_known;
3195 case PROC_SUPER_MAGIC:
3197 * Casey says procfs appears not to care.
3198 * The superblock default suffices.
3203 * Device labels should come from the filesystem,
3204 * but watch out, because they're volitile,
3205 * getting recreated on every reboot.
3207 final = smack_known_star.smk_known;
3211 * If a smack value has been set we want to use it,
3212 * but since tmpfs isn't giving us the opportunity
3213 * to set mount options simulate setting the
3214 * superblock default.
3218 * This isn't an understood special case.
3219 * Get the value from the xattr.
3223 * UNIX domain sockets use lower level socket data.
3225 if (S_ISSOCK(inode->i_mode)) {
3226 final = smack_known_star.smk_known;
3230 * No xattr support means, alas, no SMACK label.
3231 * Use the aforeapplied default.
3232 * It would be curious if the label of the task
3233 * does not match that assigned.
3235 if (inode->i_op->getxattr == NULL)
3238 * Get the dentry for xattr.
3240 dp = dget(opt_dentry);
3241 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3243 final = skp->smk_known;
3246 * Transmuting directory
3248 if (S_ISDIR(inode->i_mode)) {
3250 * If this is a new directory and the label was
3251 * transmuted when the inode was initialized
3252 * set the transmute attribute on the directory
3253 * and mark the inode.
3255 * If there is a transmute attribute on the
3256 * directory mark the inode.
3258 if (isp->smk_flags & SMK_INODE_CHANGED) {
3259 isp->smk_flags &= ~SMK_INODE_CHANGED;
3260 rc = inode->i_op->setxattr(dp,
3261 XATTR_NAME_SMACKTRANSMUTE,
3262 TRANS_TRUE, TRANS_TRUE_SIZE,
3265 rc = inode->i_op->getxattr(dp,
3266 XATTR_NAME_SMACKTRANSMUTE, trattr,
3268 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3269 TRANS_TRUE_SIZE) != 0)
3273 transflag = SMK_INODE_TRANSMUTE;
3275 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3276 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3283 isp->smk_inode = ckp->smk_known;
3285 isp->smk_inode = final;
3287 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3290 mutex_unlock(&isp->smk_lock);
3295 * smack_getprocattr - Smack process attribute access
3296 * @p: the object task
3297 * @name: the name of the attribute in /proc/.../attr
3298 * @value: where to put the result
3300 * Places a copy of the task Smack into value
3302 * Returns the length of the smack label or an error code
3304 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3306 struct smack_known *skp = smk_of_task(task_security(p));
3310 if (strcmp(name, "current") != 0)
3313 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3323 * smack_setprocattr - Smack process attribute setting
3324 * @p: the object task
3325 * @name: the name of the attribute in /proc/.../attr
3326 * @value: the value to set
3327 * @size: the size of the value
3329 * Sets the Smack value of the task. Only setting self
3330 * is permitted and only with privilege
3332 * Returns the length of the smack label or an error code
3334 static int smack_setprocattr(struct task_struct *p, char *name,
3335 void *value, size_t size)
3337 struct task_smack *tsp = current_security();
3339 struct smack_known *skp;
3340 struct smack_known_list_elem *sklep;
3344 * Changing another process' Smack value is too dangerous
3345 * and supports no sane use case.
3350 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3353 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3356 if (strcmp(name, "current") != 0)
3359 skp = smk_import_entry(value, size);
3364 * No process is ever allowed the web ("@") label.
3366 if (skp == &smack_known_web)
3369 if (!smack_privileged(CAP_MAC_ADMIN)) {
3371 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3372 if (sklep->smk_label == skp) {
3380 new = prepare_creds();
3384 tsp = new->security;
3385 tsp->smk_task = skp;
3387 * process can change its label only once
3389 smk_destroy_label_list(&tsp->smk_relabel);
3396 * smack_unix_stream_connect - Smack access on UDS
3398 * @other: the other sock
3401 * Return 0 if a subject with the smack of sock could access
3402 * an object with the smack of other, otherwise an error code
3404 static int smack_unix_stream_connect(struct sock *sock,
3405 struct sock *other, struct sock *newsk)
3407 struct smack_known *skp;
3408 struct smack_known *okp;
3409 struct socket_smack *ssp = sock->sk_security;
3410 struct socket_smack *osp = other->sk_security;
3411 struct socket_smack *nsp = newsk->sk_security;
3412 struct smk_audit_info ad;
3415 struct lsm_network_audit net;
3418 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3422 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3423 smk_ad_setfield_u_net_sk(&ad, other);
3425 rc = smk_access(skp, okp->smk_known, MAY_WRITE, &ad);
3426 rc = smk_bu_note("UDS connect", skp, okp->smk_known,
3431 rc = smk_access(okp, skp->smk_known, MAY_WRITE, &ad);
3432 rc = smk_bu_note("UDS connect", okp, skp->smk_known,
3438 * Cross reference the peer labels for SO_PEERSEC.
3441 nsp->smk_packet = ssp->smk_out;
3442 ssp->smk_packet = osp->smk_out;
3449 * smack_unix_may_send - Smack access on UDS
3451 * @other: the other socket
3453 * Return 0 if a subject with the smack of sock could access
3454 * an object with the smack of other, otherwise an error code
3456 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3458 struct socket_smack *ssp = sock->sk->sk_security;
3459 struct socket_smack *osp = other->sk->sk_security;
3460 struct smack_known *skp;
3461 struct smk_audit_info ad;
3465 struct lsm_network_audit net;
3467 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3468 smk_ad_setfield_u_net_sk(&ad, other->sk);
3471 if (smack_privileged(CAP_MAC_OVERRIDE))
3476 rc = smk_access(skp, osp->smk_in->smk_known, MAY_WRITE, &ad);
3477 rc = smk_bu_note("UDS send", skp, osp->smk_in->smk_known,
3483 * smack_socket_sendmsg - Smack check based on destination host
3486 * @size: the size of the message
3488 * Return 0 if the current subject can write to the destination host.
3489 * For IPv4 this is only a question if the destination is a single label host.
3490 * For IPv6 this is a check against the label of the port.
3492 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3495 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3496 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3500 * Perfectly reasonable for this to be NULL
3505 switch (sock->sk->sk_family) {
3507 rc = smack_netlabel_send(sock->sk, sip);
3510 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3517 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3518 * @sap: netlabel secattr
3519 * @ssp: socket security information
3521 * Returns a pointer to a Smack label entry found on the label list.
3523 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3524 struct socket_smack *ssp)
3526 struct smack_known *skp;
3531 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3533 * Looks like a CIPSO packet.
3534 * If there are flags but no level netlabel isn't
3535 * behaving the way we expect it to.
3537 * Look it up in the label table
3538 * Without guidance regarding the smack value
3539 * for the packet fall back on the network
3543 list_for_each_entry(skp, &smack_known_list, list) {
3544 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3547 * Compare the catsets. Use the netlbl APIs.
3549 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3550 if ((skp->smk_netlabel.flags &
3551 NETLBL_SECATTR_MLS_CAT) == 0)
3555 for (acat = -1, kcat = -1; acat == kcat; ) {
3556 acat = netlbl_secattr_catmap_walk(
3557 sap->attr.mls.cat, acat + 1);
3558 kcat = netlbl_secattr_catmap_walk(
3559 skp->smk_netlabel.attr.mls.cat,
3561 if (acat < 0 || kcat < 0)
3574 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3575 return &smack_known_web;
3576 return &smack_known_star;
3578 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3580 * Looks like a fallback, which gives us a secid.
3582 skp = smack_from_secid(sap->attr.secid);
3584 * This has got to be a bug because it is
3585 * impossible to specify a fallback without
3586 * specifying the label, which will ensure
3587 * it has a secid, and the only way to get a
3588 * secid is from a fallback.
3590 BUG_ON(skp == NULL);
3594 * Without guidance regarding the smack value
3595 * for the packet fall back on the network
3598 return smack_net_ambient;
3601 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3605 int proto = -EINVAL;
3606 struct ipv6hdr _ipv6h;
3607 struct ipv6hdr *ip6;
3609 struct tcphdr _tcph, *th;
3610 struct udphdr _udph, *uh;
3611 struct dccp_hdr _dccph, *dh;
3615 offset = skb_network_offset(skb);
3616 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3619 sip->sin6_addr = ip6->saddr;
3621 nexthdr = ip6->nexthdr;
3622 offset += sizeof(_ipv6h);
3623 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3630 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3632 sip->sin6_port = th->source;
3635 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3637 sip->sin6_port = uh->source;
3640 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3642 sip->sin6_port = dh->dccph_sport;
3649 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3653 * Returns 0 if the packet should be delivered, an error code otherwise
3655 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3657 struct netlbl_lsm_secattr secattr;
3658 struct socket_smack *ssp = sk->sk_security;
3659 struct smack_known *skp;
3660 struct sockaddr_in6 sadd;
3662 struct smk_audit_info ad;
3664 struct lsm_network_audit net;
3666 switch (sk->sk_family) {
3669 * Translate what netlabel gave us.
3671 netlbl_secattr_init(&secattr);
3673 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3675 skp = smack_from_secattr(&secattr, ssp);
3677 skp = smack_net_ambient;
3679 netlbl_secattr_destroy(&secattr);
3682 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3683 ad.a.u.net->family = sk->sk_family;
3684 ad.a.u.net->netif = skb->skb_iif;
3685 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3688 * Receiving a packet requires that the other end
3689 * be able to write here. Read access is not required.
3690 * This is the simplist possible security model
3694 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3695 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in->smk_known,
3699 netlbl_skbuff_err(skb, rc, 0);
3702 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3703 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3704 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3713 * smack_socket_getpeersec_stream - pull in packet label
3715 * @optval: user's destination
3716 * @optlen: size thereof
3719 * returns zero on success, an error code otherwise
3721 static int smack_socket_getpeersec_stream(struct socket *sock,
3722 char __user *optval,
3723 int __user *optlen, unsigned len)
3725 struct socket_smack *ssp;
3730 ssp = sock->sk->sk_security;
3731 if (ssp->smk_packet != NULL) {
3732 rcp = ssp->smk_packet->smk_known;
3733 slen = strlen(rcp) + 1;
3738 else if (copy_to_user(optval, rcp, slen) != 0)
3741 if (put_user(slen, optlen) != 0)
3749 * smack_socket_getpeersec_dgram - pull in packet label
3750 * @sock: the peer socket
3752 * @secid: pointer to where to put the secid of the packet
3754 * Sets the netlabel socket state on sk from parent
3756 static int smack_socket_getpeersec_dgram(struct socket *sock,
3757 struct sk_buff *skb, u32 *secid)
3760 struct netlbl_lsm_secattr secattr;
3761 struct socket_smack *ssp = NULL;
3762 struct smack_known *skp;
3763 int family = PF_UNSPEC;
3764 u32 s = 0; /* 0 is the invalid secid */
3768 if (skb->protocol == htons(ETH_P_IP))
3770 else if (skb->protocol == htons(ETH_P_IPV6))
3773 if (family == PF_UNSPEC && sock != NULL)
3774 family = sock->sk->sk_family;
3776 if (family == PF_UNIX) {
3777 ssp = sock->sk->sk_security;
3778 s = ssp->smk_out->smk_secid;
3779 } else if (family == PF_INET || family == PF_INET6) {
3781 * Translate what netlabel gave us.
3783 if (sock != NULL && sock->sk != NULL)
3784 ssp = sock->sk->sk_security;
3785 netlbl_secattr_init(&secattr);
3786 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3788 skp = smack_from_secattr(&secattr, ssp);
3791 netlbl_secattr_destroy(&secattr);
3800 * smack_sock_graft - Initialize a newly created socket with an existing sock
3802 * @parent: parent socket
3804 * Set the smk_{in,out} state of an existing sock based on the process that
3805 * is creating the new socket.
3807 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3809 struct socket_smack *ssp;
3810 struct smack_known *skp = smk_of_current();
3813 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3816 ssp = sk->sk_security;
3819 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3823 * smack_inet_conn_request - Smack access check on connect
3824 * @sk: socket involved
3828 * Returns 0 if a task with the packet label could write to
3829 * the socket, otherwise an error code
3831 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3832 struct request_sock *req)
3834 u16 family = sk->sk_family;
3835 struct smack_known *skp;
3836 struct socket_smack *ssp = sk->sk_security;
3837 struct netlbl_lsm_secattr secattr;
3838 struct sockaddr_in addr;
3842 struct smk_audit_info ad;
3844 struct lsm_network_audit net;
3847 if (family == PF_INET6) {
3849 * Handle mapped IPv4 packets arriving
3850 * via IPv6 sockets. Don't set up netlabel
3851 * processing on IPv6.
3853 if (skb->protocol == htons(ETH_P_IP))
3859 netlbl_secattr_init(&secattr);
3860 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3862 skp = smack_from_secattr(&secattr, ssp);
3864 skp = &smack_known_huh;
3865 netlbl_secattr_destroy(&secattr);
3868 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3869 ad.a.u.net->family = family;
3870 ad.a.u.net->netif = skb->skb_iif;
3871 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3874 * Receiving a packet requires that the other end be able to write
3875 * here. Read access is not required.
3878 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3879 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in->smk_known,
3885 * Save the peer's label in the request_sock so we can later setup
3886 * smk_packet in the child socket so that SO_PEERCRED can report it.
3888 req->peer_secid = skp->smk_secid;
3891 * We need to decide if we want to label the incoming connection here
3892 * if we do we only need to label the request_sock and the stack will
3893 * propagate the wire-label to the sock when it is created.
3896 addr.sin_addr.s_addr = hdr->saddr;
3898 hsp = smack_host_label(&addr);
3902 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3904 netlbl_req_delattr(req);
3910 * smack_inet_csk_clone - Copy the connection information to the new socket
3911 * @sk: the new socket
3912 * @req: the connection's request_sock
3914 * Transfer the connection's peer label to the newly created socket.
3916 static void smack_inet_csk_clone(struct sock *sk,
3917 const struct request_sock *req)
3919 struct socket_smack *ssp = sk->sk_security;
3920 struct smack_known *skp;
3922 if (req->peer_secid != 0) {
3923 skp = smack_from_secid(req->peer_secid);
3924 ssp->smk_packet = skp;
3926 ssp->smk_packet = NULL;
3930 * Key management security hooks
3932 * Casey has not tested key support very heavily.
3933 * The permission check is most likely too restrictive.
3934 * If you care about keys please have a look.
3939 * smack_key_alloc - Set the key security blob
3941 * @cred: the credentials to use
3944 * No allocation required
3948 static int smack_key_alloc(struct key *key, const struct cred *cred,
3949 unsigned long flags)
3951 struct smack_known *skp = smk_of_task(cred->security);
3953 key->security = skp->smk_known;
3958 * smack_key_free - Clear the key security blob
3961 * Clear the blob pointer
3963 static void smack_key_free(struct key *key)
3965 key->security = NULL;
3969 * smack_key_permission - Smack access on a key
3970 * @key_ref: gets to the object
3971 * @cred: the credentials to use
3974 * Return 0 if the task has read and write to the object,
3975 * an error code otherwise
3977 static int smack_key_permission(key_ref_t key_ref,
3978 const struct cred *cred, key_perm_t perm)
3981 struct smk_audit_info ad;
3982 struct smack_known *tkp = smk_of_task(cred->security);
3985 keyp = key_ref_to_ptr(key_ref);
3989 * If the key hasn't been initialized give it access so that
3992 if (keyp->security == NULL)
3995 * This should not occur
4000 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4001 ad.a.u.key_struct.key = keyp->serial;
4002 ad.a.u.key_struct.key_desc = keyp->description;
4004 rc = smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
4005 rc = smk_bu_note("key access", tkp, keyp->security, MAY_READWRITE, rc);
4008 #endif /* CONFIG_KEYS */
4013 * Audit requires a unique representation of each Smack specific
4014 * rule. This unique representation is used to distinguish the
4015 * object to be audited from remaining kernel objects and also
4016 * works as a glue between the audit hooks.
4018 * Since repository entries are added but never deleted, we'll use
4019 * the smack_known label address related to the given audit rule as
4020 * the needed unique representation. This also better fits the smack
4021 * model where nearly everything is a label.
4026 * smack_audit_rule_init - Initialize a smack audit rule
4027 * @field: audit rule fields given from user-space (audit.h)
4028 * @op: required testing operator (=, !=, >, <, ...)
4029 * @rulestr: smack label to be audited
4030 * @vrule: pointer to save our own audit rule representation
4032 * Prepare to audit cases where (@field @op @rulestr) is true.
4033 * The label to be audited is created if necessay.
4035 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4037 char **rule = (char **)vrule;
4040 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4043 if (op != Audit_equal && op != Audit_not_equal)
4046 *rule = smk_import(rulestr, 0);
4052 * smack_audit_rule_known - Distinguish Smack audit rules
4053 * @krule: rule of interest, in Audit kernel representation format
4055 * This is used to filter Smack rules from remaining Audit ones.
4056 * If it's proved that this rule belongs to us, the
4057 * audit_rule_match hook will be called to do the final judgement.
4059 static int smack_audit_rule_known(struct audit_krule *krule)
4061 struct audit_field *f;
4064 for (i = 0; i < krule->field_count; i++) {
4065 f = &krule->fields[i];
4067 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4075 * smack_audit_rule_match - Audit given object ?
4076 * @secid: security id for identifying the object to test
4077 * @field: audit rule flags given from user-space
4078 * @op: required testing operator
4079 * @vrule: smack internal rule presentation
4080 * @actx: audit context associated with the check
4082 * The core Audit hook. It's used to take the decision of
4083 * whether to audit or not to audit a given object.
4085 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4086 struct audit_context *actx)
4088 struct smack_known *skp;
4092 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
4093 "Smack: missing rule\n");
4097 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4100 skp = smack_from_secid(secid);
4103 * No need to do string comparisons. If a match occurs,
4104 * both pointers will point to the same smack_known
4107 if (op == Audit_equal)
4108 return (rule == skp->smk_known);
4109 if (op == Audit_not_equal)
4110 return (rule != skp->smk_known);
4116 * smack_audit_rule_free - free smack rule representation
4117 * @vrule: rule to be freed.
4119 * No memory was allocated.
4121 static void smack_audit_rule_free(void *vrule)
4126 #endif /* CONFIG_AUDIT */
4129 * smack_secid_to_secctx - return the smack label for a secid
4130 * @secid: incoming integer
4131 * @secdata: destination
4132 * @seclen: how long it is
4134 * Exists for networking code.
4136 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4138 struct smack_known *skp = smack_from_secid(secid);
4141 *secdata = skp->smk_known;
4142 *seclen = strlen(skp->smk_known);
4147 * smack_secctx_to_secid - return the secid for a smack label
4148 * @secdata: smack label
4149 * @seclen: how long result is
4150 * @secid: outgoing integer
4152 * Exists for audit and networking code.
4154 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4156 *secid = smack_to_secid(secdata);
4161 * smack_release_secctx - don't do anything.
4165 * Exists to make sure nothing gets done, and properly
4167 static void smack_release_secctx(char *secdata, u32 seclen)
4171 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4173 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4176 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4178 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4181 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4184 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4192 struct security_operations smack_ops = {
4195 .ptrace_access_check = smack_ptrace_access_check,
4196 .ptrace_traceme = smack_ptrace_traceme,
4197 .syslog = smack_syslog,
4199 .sb_alloc_security = smack_sb_alloc_security,
4200 .sb_free_security = smack_sb_free_security,
4201 .sb_copy_data = smack_sb_copy_data,
4202 .sb_kern_mount = smack_sb_kern_mount,
4203 .sb_statfs = smack_sb_statfs,
4205 .bprm_set_creds = smack_bprm_set_creds,
4206 .bprm_committing_creds = smack_bprm_committing_creds,
4207 .bprm_secureexec = smack_bprm_secureexec,
4209 .inode_alloc_security = smack_inode_alloc_security,
4210 .inode_free_security = smack_inode_free_security,
4211 .inode_init_security = smack_inode_init_security,
4212 .inode_link = smack_inode_link,
4213 .inode_unlink = smack_inode_unlink,
4214 .inode_rmdir = smack_inode_rmdir,
4215 .inode_rename = smack_inode_rename,
4216 .inode_permission = smack_inode_permission,
4217 .inode_setattr = smack_inode_setattr,
4218 .inode_getattr = smack_inode_getattr,
4219 .inode_setxattr = smack_inode_setxattr,
4220 .inode_post_setxattr = smack_inode_post_setxattr,
4221 .inode_getxattr = smack_inode_getxattr,
4222 .inode_removexattr = smack_inode_removexattr,
4223 .inode_getsecurity = smack_inode_getsecurity,
4224 .inode_setsecurity = smack_inode_setsecurity,
4225 .inode_listsecurity = smack_inode_listsecurity,
4226 .inode_getsecid = smack_inode_getsecid,
4228 .file_permission = smack_file_permission,
4229 .file_alloc_security = smack_file_alloc_security,
4230 .file_free_security = smack_file_free_security,
4231 .file_ioctl = smack_file_ioctl,
4232 .file_lock = smack_file_lock,
4233 .file_fcntl = smack_file_fcntl,
4234 .mmap_file = smack_mmap_file,
4235 .mmap_addr = cap_mmap_addr,
4236 .file_set_fowner = smack_file_set_fowner,
4237 .file_send_sigiotask = smack_file_send_sigiotask,
4238 .file_receive = smack_file_receive,
4240 .file_open = smack_file_open,
4242 .cred_alloc_blank = smack_cred_alloc_blank,
4243 .cred_free = smack_cred_free,
4244 .cred_prepare = smack_cred_prepare,
4245 .cred_transfer = smack_cred_transfer,
4246 .kernel_act_as = smack_kernel_act_as,
4247 .kernel_create_files_as = smack_kernel_create_files_as,
4248 .task_setpgid = smack_task_setpgid,
4249 .task_getpgid = smack_task_getpgid,
4250 .task_getsid = smack_task_getsid,
4251 .task_getsecid = smack_task_getsecid,
4252 .task_setnice = smack_task_setnice,
4253 .task_setioprio = smack_task_setioprio,
4254 .task_getioprio = smack_task_getioprio,
4255 .task_setscheduler = smack_task_setscheduler,
4256 .task_getscheduler = smack_task_getscheduler,
4257 .task_movememory = smack_task_movememory,
4258 .task_kill = smack_task_kill,
4259 .task_wait = smack_task_wait,
4260 .task_to_inode = smack_task_to_inode,
4262 .ipc_permission = smack_ipc_permission,
4263 .ipc_getsecid = smack_ipc_getsecid,
4265 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4266 .msg_msg_free_security = smack_msg_msg_free_security,
4268 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4269 .msg_queue_free_security = smack_msg_queue_free_security,
4270 .msg_queue_associate = smack_msg_queue_associate,
4271 .msg_queue_msgctl = smack_msg_queue_msgctl,
4272 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4273 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4275 .shm_alloc_security = smack_shm_alloc_security,
4276 .shm_free_security = smack_shm_free_security,
4277 .shm_associate = smack_shm_associate,
4278 .shm_shmctl = smack_shm_shmctl,
4279 .shm_shmat = smack_shm_shmat,
4281 .sem_alloc_security = smack_sem_alloc_security,
4282 .sem_free_security = smack_sem_free_security,
4283 .sem_associate = smack_sem_associate,
4284 .sem_semctl = smack_sem_semctl,
4285 .sem_semop = smack_sem_semop,
4287 .d_instantiate = smack_d_instantiate,
4289 .getprocattr = smack_getprocattr,
4290 .setprocattr = smack_setprocattr,
4292 .unix_stream_connect = smack_unix_stream_connect,
4293 .unix_may_send = smack_unix_may_send,
4295 .socket_post_create = smack_socket_post_create,
4296 .socket_bind = smack_socket_bind,
4297 .socket_connect = smack_socket_connect,
4298 .socket_sendmsg = smack_socket_sendmsg,
4299 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4300 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4301 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4302 .sk_alloc_security = smack_sk_alloc_security,
4303 .sk_free_security = smack_sk_free_security,
4304 .sock_graft = smack_sock_graft,
4305 .inet_conn_request = smack_inet_conn_request,
4306 .inet_csk_clone = smack_inet_csk_clone,
4308 /* key management security hooks */
4310 .key_alloc = smack_key_alloc,
4311 .key_free = smack_key_free,
4312 .key_permission = smack_key_permission,
4313 #endif /* CONFIG_KEYS */
4317 .audit_rule_init = smack_audit_rule_init,
4318 .audit_rule_known = smack_audit_rule_known,
4319 .audit_rule_match = smack_audit_rule_match,
4320 .audit_rule_free = smack_audit_rule_free,
4321 #endif /* CONFIG_AUDIT */
4323 .secid_to_secctx = smack_secid_to_secctx,
4324 .secctx_to_secid = smack_secctx_to_secid,
4325 .release_secctx = smack_release_secctx,
4326 .inode_notifysecctx = smack_inode_notifysecctx,
4327 .inode_setsecctx = smack_inode_setsecctx,
4328 .inode_getsecctx = smack_inode_getsecctx,
4332 static __init void init_smack_known_list(void)
4335 * Initialize rule list locks
4337 mutex_init(&smack_known_huh.smk_rules_lock);
4338 mutex_init(&smack_known_hat.smk_rules_lock);
4339 mutex_init(&smack_known_floor.smk_rules_lock);
4340 mutex_init(&smack_known_star.smk_rules_lock);
4341 mutex_init(&smack_known_invalid.smk_rules_lock);
4342 mutex_init(&smack_known_web.smk_rules_lock);
4344 * Initialize rule lists
4346 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4347 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4348 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4349 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4350 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4351 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4353 * Create the known labels list
4355 smk_insert_entry(&smack_known_huh);
4356 smk_insert_entry(&smack_known_hat);
4357 smk_insert_entry(&smack_known_star);
4358 smk_insert_entry(&smack_known_floor);
4359 smk_insert_entry(&smack_known_invalid);
4360 smk_insert_entry(&smack_known_web);
4363 /* KMEM caches for fast and thrifty allocations */
4364 struct kmem_cache *smack_rule_cache;
4365 struct kmem_cache *smack_master_list_cache;
4368 * smack_init - initialize the smack system
4372 static __init int smack_init(void)
4375 struct task_smack *tsp;
4377 if (!security_module_enable(&smack_ops))
4380 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4381 if (!smack_rule_cache)
4384 smack_master_list_cache = KMEM_CACHE(smack_master_list, 0);
4385 if (!smack_master_list_cache) {
4386 kmem_cache_destroy(smack_rule_cache);
4390 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4391 if (!smack_inode_cache) {
4392 kmem_cache_destroy(smack_master_list_cache);
4393 kmem_cache_destroy(smack_rule_cache);
4397 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4400 kmem_cache_destroy(smack_master_list_cache);
4401 kmem_cache_destroy(smack_rule_cache);
4402 kmem_cache_destroy(smack_inode_cache);
4406 printk(KERN_INFO "Smack: Initializing.\n");
4409 * Set the security state for the initial task.
4411 cred = (struct cred *) current->cred;
4412 cred->security = tsp;
4414 /* initialize the smack_known_list */
4415 init_smack_known_list();
4420 if (register_security(&smack_ops))
4421 panic("smack: Unable to register with kernel.\n");
4427 * Smack requires early initialization in order to label
4428 * all processes and objects when they are created.
4430 security_initcall(smack_init);