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);
2150 ssp->smk_packet = NULL;
2152 sk->sk_security = ssp;
2158 * smack_sk_free_security - Free a socket blob
2161 * Clears the blob pointer
2163 static void smack_sk_free_security(struct sock *sk)
2165 kfree(sk->sk_security);
2169 * smack_host_label - check host based restrictions
2170 * @sip: the object end
2172 * looks for host based access restrictions
2174 * This version will only be appropriate for really small sets of single label
2175 * hosts. The caller is responsible for ensuring that the RCU read lock is
2176 * taken before calling this function.
2178 * Returns the label of the far end or NULL if it's not special.
2180 static char *smack_host_label(struct sockaddr_in *sip)
2182 struct smk_netlbladdr *snp;
2183 struct in_addr *siap = &sip->sin_addr;
2185 if (siap->s_addr == 0)
2188 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2190 * we break after finding the first match because
2191 * the list is sorted from longest to shortest mask
2192 * so we have found the most specific match
2194 if ((&snp->smk_host.sin_addr)->s_addr ==
2195 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2196 /* we have found the special CIPSO option */
2197 if (snp->smk_label == smack_cipso_option)
2199 return snp->smk_label;
2206 * smack_netlabel - Set the secattr on a socket
2208 * @labeled: socket label scheme
2210 * Convert the outbound smack value (smk_out) to a
2211 * secattr and attach it to the socket.
2213 * Returns 0 on success or an error code
2215 static int smack_netlabel(struct sock *sk, int labeled)
2217 struct smack_known *skp;
2218 struct socket_smack *ssp = sk->sk_security;
2222 * Usually the netlabel code will handle changing the
2223 * packet labeling based on the label.
2224 * The case of a single label host is different, because
2225 * a single label host should never get a labeled packet
2226 * even though the label is usually associated with a packet
2230 bh_lock_sock_nested(sk);
2232 if (ssp->smk_out == smack_net_ambient ||
2233 labeled == SMACK_UNLABELED_SOCKET)
2234 netlbl_sock_delattr(sk);
2237 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2247 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2249 * @sap: the destination address
2251 * Set the correct secattr for the given socket based on the destination
2252 * address and perform any outbound access checks needed.
2254 * Returns 0 on success or an error code.
2257 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2259 struct smack_known *skp;
2263 struct socket_smack *ssp = sk->sk_security;
2264 struct smk_audit_info ad;
2267 hostsp = smack_host_label(sap);
2268 if (hostsp != NULL) {
2270 struct lsm_network_audit net;
2272 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2273 ad.a.u.net->family = sap->sin_family;
2274 ad.a.u.net->dport = sap->sin_port;
2275 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2277 sk_lbl = SMACK_UNLABELED_SOCKET;
2279 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
2280 rc = smk_bu_note("IPv4 host check", skp, hostsp, MAY_WRITE, rc);
2282 sk_lbl = SMACK_CIPSO_SOCKET;
2289 return smack_netlabel(sk, sk_lbl);
2293 * smk_ipv6_port_label - Smack port access table management
2297 * Create or update the port list entry
2299 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2301 struct sock *sk = sock->sk;
2302 struct sockaddr_in6 *addr6;
2303 struct socket_smack *ssp = sock->sk->sk_security;
2304 struct smk_port_label *spp;
2305 unsigned short port = 0;
2307 if (address == NULL) {
2309 * This operation is changing the Smack information
2310 * on the bound socket. Take the changes to the port
2313 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2314 if (sk != spp->smk_sock)
2316 spp->smk_in = ssp->smk_in;
2317 spp->smk_out = ssp->smk_out;
2321 * A NULL address is only used for updating existing
2322 * bound entries. If there isn't one, it's OK.
2327 addr6 = (struct sockaddr_in6 *)address;
2328 port = ntohs(addr6->sin6_port);
2330 * This is a special case that is safely ignored.
2336 * Look for an existing port list entry.
2337 * This is an indication that a port is getting reused.
2339 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2340 if (spp->smk_port != port)
2342 spp->smk_port = port;
2344 spp->smk_in = ssp->smk_in;
2345 spp->smk_out = ssp->smk_out;
2350 * A new port entry is required.
2352 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2356 spp->smk_port = port;
2358 spp->smk_in = ssp->smk_in;
2359 spp->smk_out = ssp->smk_out;
2361 list_add(&spp->list, &smk_ipv6_port_list);
2366 * smk_ipv6_port_check - check Smack port access
2370 * Create or update the port list entry
2372 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2377 struct smk_port_label *spp;
2378 struct socket_smack *ssp = sk->sk_security;
2379 struct smack_known *skp;
2380 unsigned short port = 0;
2382 struct smk_audit_info ad;
2385 struct lsm_network_audit net;
2388 if (act == SMK_RECEIVING) {
2389 skp = smack_net_ambient;
2390 object = ssp->smk_in->smk_known;
2393 object = smack_net_ambient->smk_known;
2397 * Get the IP address and port from the address.
2399 port = ntohs(address->sin6_port);
2400 bep = (__be16 *)(&address->sin6_addr);
2401 be32p = (__be32 *)(&address->sin6_addr);
2404 * It's remote, so port lookup does no good.
2406 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2410 * It's local so the send check has to have passed.
2412 if (act == SMK_RECEIVING) {
2413 skp = &smack_known_web;
2417 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2418 if (spp->smk_port != port)
2420 object = spp->smk_in->smk_known;
2421 if (act == SMK_CONNECTING)
2422 ssp->smk_packet = spp->smk_out;
2429 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2430 ad.a.u.net->family = sk->sk_family;
2431 ad.a.u.net->dport = port;
2432 if (act == SMK_RECEIVING)
2433 ad.a.u.net->v6info.saddr = address->sin6_addr;
2435 ad.a.u.net->v6info.daddr = address->sin6_addr;
2437 rc = smk_access(skp, object, MAY_WRITE, &ad);
2438 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2443 * smack_inode_setsecurity - set smack xattrs
2444 * @inode: the object
2445 * @name: attribute name
2446 * @value: attribute value
2447 * @size: size of the attribute
2450 * Sets the named attribute in the appropriate blob
2452 * Returns 0 on success, or an error code
2454 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2455 const void *value, size_t size, int flags)
2457 struct smack_known *skp;
2458 struct inode_smack *nsp = inode->i_security;
2459 struct socket_smack *ssp;
2460 struct socket *sock;
2463 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2466 skp = smk_import_entry(value, size);
2470 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2471 nsp->smk_inode = skp->smk_known;
2472 nsp->smk_flags |= SMK_INODE_INSTANT;
2476 * The rest of the Smack xattrs are only on sockets.
2478 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2481 sock = SOCKET_I(inode);
2482 if (sock == NULL || sock->sk == NULL)
2485 ssp = sock->sk->sk_security;
2487 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2489 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2491 if (sock->sk->sk_family == PF_INET) {
2492 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2495 "Smack: \"%s\" netlbl error %d.\n",
2501 if (sock->sk->sk_family == PF_INET6)
2502 smk_ipv6_port_label(sock, NULL);
2508 * smack_socket_post_create - finish socket setup
2510 * @family: protocol family
2515 * Sets the netlabel information on the socket
2517 * Returns 0 on success, and error code otherwise
2519 static int smack_socket_post_create(struct socket *sock, int family,
2520 int type, int protocol, int kern)
2522 struct socket_smack *ssp;
2524 if (sock->sk == NULL)
2528 * Sockets created by kernel threads receive web label.
2530 if (unlikely(current->flags & PF_KTHREAD)) {
2531 ssp = sock->sk->sk_security;
2532 ssp->smk_in = &smack_known_web;
2534 if (family != PF_INET)
2537 * Set the outbound netlbl.
2539 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2543 * smack_socket_bind - record port binding information.
2545 * @address: the port address
2546 * @addrlen: size of the address
2548 * Records the label bound to a port.
2552 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2555 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2556 smk_ipv6_port_label(sock, address);
2562 * smack_socket_connect - connect access check
2564 * @sap: the other end
2565 * @addrlen: size of sap
2567 * Verifies that a connection may be possible
2569 * Returns 0 on success, and error code otherwise
2571 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2576 if (sock->sk == NULL)
2579 switch (sock->sk->sk_family) {
2581 if (addrlen < sizeof(struct sockaddr_in))
2583 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2586 if (addrlen < sizeof(struct sockaddr_in6))
2588 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2596 * smack_flags_to_may - convert S_ to MAY_ values
2597 * @flags: the S_ value
2599 * Returns the equivalent MAY_ value
2601 static int smack_flags_to_may(int flags)
2605 if (flags & S_IRUGO)
2607 if (flags & S_IWUGO)
2609 if (flags & S_IXUGO)
2616 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2621 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2623 struct smack_known *skp = smk_of_current();
2625 msg->security = skp->smk_known;
2630 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2633 * Clears the blob pointer
2635 static void smack_msg_msg_free_security(struct msg_msg *msg)
2637 msg->security = NULL;
2641 * smack_of_shm - the smack pointer for the shm
2644 * Returns a pointer to the smack value
2646 static char *smack_of_shm(struct shmid_kernel *shp)
2648 return (char *)shp->shm_perm.security;
2652 * smack_shm_alloc_security - Set the security blob for shm
2657 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2659 struct kern_ipc_perm *isp = &shp->shm_perm;
2660 struct smack_known *skp = smk_of_current();
2662 isp->security = skp->smk_known;
2667 * smack_shm_free_security - Clear the security blob for shm
2670 * Clears the blob pointer
2672 static void smack_shm_free_security(struct shmid_kernel *shp)
2674 struct kern_ipc_perm *isp = &shp->shm_perm;
2676 isp->security = NULL;
2680 * smk_curacc_shm : check if current has access on shm
2682 * @access : access requested
2684 * Returns 0 if current has the requested access, error code otherwise
2686 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2688 char *ssp = smack_of_shm(shp);
2689 struct smk_audit_info ad;
2693 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2694 ad.a.u.ipc_id = shp->shm_perm.id;
2696 rc = smk_curacc(ssp, access, &ad);
2697 rc = smk_bu_current("shm", ssp, access, rc);
2702 * smack_shm_associate - Smack access check for shm
2704 * @shmflg: access requested
2706 * Returns 0 if current has the requested access, error code otherwise
2708 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2712 may = smack_flags_to_may(shmflg);
2713 return smk_curacc_shm(shp, may);
2717 * smack_shm_shmctl - Smack access check for shm
2719 * @cmd: what it wants to do
2721 * Returns 0 if current has the requested access, error code otherwise
2723 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2736 may = MAY_READWRITE;
2741 * System level information.
2747 return smk_curacc_shm(shp, may);
2751 * smack_shm_shmat - Smack access for shmat
2754 * @shmflg: access requested
2756 * Returns 0 if current has the requested access, error code otherwise
2758 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2763 may = smack_flags_to_may(shmflg);
2764 return smk_curacc_shm(shp, may);
2768 * smack_of_sem - the smack pointer for the sem
2771 * Returns a pointer to the smack value
2773 static char *smack_of_sem(struct sem_array *sma)
2775 return (char *)sma->sem_perm.security;
2779 * smack_sem_alloc_security - Set the security blob for sem
2784 static int smack_sem_alloc_security(struct sem_array *sma)
2786 struct kern_ipc_perm *isp = &sma->sem_perm;
2787 struct smack_known *skp = smk_of_current();
2789 isp->security = skp->smk_known;
2794 * smack_sem_free_security - Clear the security blob for sem
2797 * Clears the blob pointer
2799 static void smack_sem_free_security(struct sem_array *sma)
2801 struct kern_ipc_perm *isp = &sma->sem_perm;
2803 isp->security = NULL;
2807 * smk_curacc_sem : check if current has access on sem
2809 * @access : access requested
2811 * Returns 0 if current has the requested access, error code otherwise
2813 static int smk_curacc_sem(struct sem_array *sma, int access)
2815 char *ssp = smack_of_sem(sma);
2816 struct smk_audit_info ad;
2820 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2821 ad.a.u.ipc_id = sma->sem_perm.id;
2823 rc = smk_curacc(ssp, access, &ad);
2824 rc = smk_bu_current("sem", ssp, access, rc);
2829 * smack_sem_associate - Smack access check for sem
2831 * @semflg: access requested
2833 * Returns 0 if current has the requested access, error code otherwise
2835 static int smack_sem_associate(struct sem_array *sma, int semflg)
2839 may = smack_flags_to_may(semflg);
2840 return smk_curacc_sem(sma, may);
2844 * smack_sem_shmctl - Smack access check for sem
2846 * @cmd: what it wants to do
2848 * Returns 0 if current has the requested access, error code otherwise
2850 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2868 may = MAY_READWRITE;
2873 * System level information
2880 return smk_curacc_sem(sma, may);
2884 * smack_sem_semop - Smack checks of semaphore operations
2890 * Treated as read and write in all cases.
2892 * Returns 0 if access is allowed, error code otherwise
2894 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2895 unsigned nsops, int alter)
2897 return smk_curacc_sem(sma, MAY_READWRITE);
2901 * smack_msg_alloc_security - Set the security blob for msg
2906 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2908 struct kern_ipc_perm *kisp = &msq->q_perm;
2909 struct smack_known *skp = smk_of_current();
2911 kisp->security = skp->smk_known;
2916 * smack_msg_free_security - Clear the security blob for msg
2919 * Clears the blob pointer
2921 static void smack_msg_queue_free_security(struct msg_queue *msq)
2923 struct kern_ipc_perm *kisp = &msq->q_perm;
2925 kisp->security = NULL;
2929 * smack_of_msq - the smack pointer for the msq
2932 * Returns a pointer to the smack value
2934 static char *smack_of_msq(struct msg_queue *msq)
2936 return (char *)msq->q_perm.security;
2940 * smk_curacc_msq : helper to check if current has access on msq
2942 * @access : access requested
2944 * return 0 if current has access, error otherwise
2946 static int smk_curacc_msq(struct msg_queue *msq, int access)
2948 char *msp = smack_of_msq(msq);
2949 struct smk_audit_info ad;
2953 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2954 ad.a.u.ipc_id = msq->q_perm.id;
2956 rc = smk_curacc(msp, access, &ad);
2957 rc = smk_bu_current("msq", msp, access, rc);
2962 * smack_msg_queue_associate - Smack access check for msg_queue
2964 * @msqflg: access requested
2966 * Returns 0 if current has the requested access, error code otherwise
2968 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2972 may = smack_flags_to_may(msqflg);
2973 return smk_curacc_msq(msq, may);
2977 * smack_msg_queue_msgctl - Smack access check for msg_queue
2979 * @cmd: what it wants to do
2981 * Returns 0 if current has the requested access, error code otherwise
2983 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2994 may = MAY_READWRITE;
2999 * System level information
3006 return smk_curacc_msq(msq, may);
3010 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3013 * @msqflg: access requested
3015 * Returns 0 if current has the requested access, error code otherwise
3017 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
3022 may = smack_flags_to_may(msqflg);
3023 return smk_curacc_msq(msq, may);
3027 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3034 * Returns 0 if current has read and write access, error code otherwise
3036 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3037 struct task_struct *target, long type, int mode)
3039 return smk_curacc_msq(msq, MAY_READWRITE);
3043 * smack_ipc_permission - Smack access for ipc_permission()
3044 * @ipp: the object permissions
3045 * @flag: access requested
3047 * Returns 0 if current has read and write access, error code otherwise
3049 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3051 char *isp = ipp->security;
3052 int may = smack_flags_to_may(flag);
3053 struct smk_audit_info ad;
3057 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3058 ad.a.u.ipc_id = ipp->id;
3060 rc = smk_curacc(isp, may, &ad);
3061 rc = smk_bu_current("svipc", isp, may, rc);
3066 * smack_ipc_getsecid - Extract smack security id
3067 * @ipp: the object permissions
3068 * @secid: where result will be saved
3070 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3072 char *smack = ipp->security;
3074 *secid = smack_to_secid(smack);
3078 * smack_d_instantiate - Make sure the blob is correct on an inode
3079 * @opt_dentry: dentry where inode will be attached
3080 * @inode: the object
3082 * Set the inode's security blob if it hasn't been done already.
3084 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3086 struct super_block *sbp;
3087 struct superblock_smack *sbsp;
3088 struct inode_smack *isp;
3089 struct smack_known *skp;
3090 struct smack_known *ckp = smk_of_current();
3092 char trattr[TRANS_TRUE_SIZE];
3100 isp = inode->i_security;
3102 mutex_lock(&isp->smk_lock);
3104 * If the inode is already instantiated
3105 * take the quick way out
3107 if (isp->smk_flags & SMK_INODE_INSTANT)
3111 sbsp = sbp->s_security;
3113 * We're going to use the superblock default label
3114 * if there's no label on the file.
3116 final = sbsp->smk_default;
3119 * If this is the root inode the superblock
3120 * may be in the process of initialization.
3121 * If that is the case use the root value out
3122 * of the superblock.
3124 if (opt_dentry->d_parent == opt_dentry) {
3125 switch (sbp->s_magic) {
3126 case CGROUP_SUPER_MAGIC:
3128 * The cgroup filesystem is never mounted,
3129 * so there's no opportunity to set the mount
3132 sbsp->smk_root = smack_known_star.smk_known;
3133 sbsp->smk_default = smack_known_star.smk_known;
3134 isp->smk_inode = sbsp->smk_root;
3138 * What about shmem/tmpfs anonymous files with dentry
3139 * obtained from d_alloc_pseudo()?
3141 isp->smk_inode = smk_of_current()->smk_known;
3144 isp->smk_inode = smk_of_current()->smk_known;
3147 isp->smk_inode = sbsp->smk_root;
3150 isp->smk_flags |= SMK_INODE_INSTANT;
3155 * This is pretty hackish.
3156 * Casey says that we shouldn't have to do
3157 * file system specific code, but it does help
3158 * with keeping it simple.
3160 switch (sbp->s_magic) {
3164 case CGROUP_SUPER_MAGIC:
3166 * Casey says that it's a little embarrassing
3167 * that the smack file system doesn't do
3168 * extended attributes.
3170 * Casey says pipes are easy (?)
3172 * Socket access is controlled by the socket
3173 * structures associated with the task involved.
3175 * Cgroupfs is special
3177 final = smack_known_star.smk_known;
3179 case DEVPTS_SUPER_MAGIC:
3181 * devpts seems content with the label of the task.
3182 * Programs that change smack have to treat the
3185 final = ckp->smk_known;
3187 case PROC_SUPER_MAGIC:
3189 * Casey says procfs appears not to care.
3190 * The superblock default suffices.
3195 * Device labels should come from the filesystem,
3196 * but watch out, because they're volitile,
3197 * getting recreated on every reboot.
3199 final = smack_known_star.smk_known;
3203 * If a smack value has been set we want to use it,
3204 * but since tmpfs isn't giving us the opportunity
3205 * to set mount options simulate setting the
3206 * superblock default.
3210 * This isn't an understood special case.
3211 * Get the value from the xattr.
3215 * UNIX domain sockets use lower level socket data.
3217 if (S_ISSOCK(inode->i_mode)) {
3218 final = smack_known_star.smk_known;
3222 * No xattr support means, alas, no SMACK label.
3223 * Use the aforeapplied default.
3224 * It would be curious if the label of the task
3225 * does not match that assigned.
3227 if (inode->i_op->getxattr == NULL)
3230 * Get the dentry for xattr.
3232 dp = dget(opt_dentry);
3233 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3235 final = skp->smk_known;
3238 * Transmuting directory
3240 if (S_ISDIR(inode->i_mode)) {
3242 * If this is a new directory and the label was
3243 * transmuted when the inode was initialized
3244 * set the transmute attribute on the directory
3245 * and mark the inode.
3247 * If there is a transmute attribute on the
3248 * directory mark the inode.
3250 if (isp->smk_flags & SMK_INODE_CHANGED) {
3251 isp->smk_flags &= ~SMK_INODE_CHANGED;
3252 rc = inode->i_op->setxattr(dp,
3253 XATTR_NAME_SMACKTRANSMUTE,
3254 TRANS_TRUE, TRANS_TRUE_SIZE,
3257 rc = inode->i_op->getxattr(dp,
3258 XATTR_NAME_SMACKTRANSMUTE, trattr,
3260 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3261 TRANS_TRUE_SIZE) != 0)
3265 transflag = SMK_INODE_TRANSMUTE;
3267 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3268 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3275 isp->smk_inode = ckp->smk_known;
3277 isp->smk_inode = final;
3279 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3282 mutex_unlock(&isp->smk_lock);
3287 * smack_getprocattr - Smack process attribute access
3288 * @p: the object task
3289 * @name: the name of the attribute in /proc/.../attr
3290 * @value: where to put the result
3292 * Places a copy of the task Smack into value
3294 * Returns the length of the smack label or an error code
3296 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3298 struct smack_known *skp = smk_of_task(task_security(p));
3302 if (strcmp(name, "current") != 0)
3305 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3315 * smack_setprocattr - Smack process attribute setting
3316 * @p: the object task
3317 * @name: the name of the attribute in /proc/.../attr
3318 * @value: the value to set
3319 * @size: the size of the value
3321 * Sets the Smack value of the task. Only setting self
3322 * is permitted and only with privilege
3324 * Returns the length of the smack label or an error code
3326 static int smack_setprocattr(struct task_struct *p, char *name,
3327 void *value, size_t size)
3329 struct task_smack *tsp = current_security();
3331 struct smack_known *skp;
3332 struct smack_known_list_elem *sklep;
3336 * Changing another process' Smack value is too dangerous
3337 * and supports no sane use case.
3342 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3345 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3348 if (strcmp(name, "current") != 0)
3351 skp = smk_import_entry(value, size);
3356 * No process is ever allowed the web ("@") label.
3358 if (skp == &smack_known_web)
3361 if (!smack_privileged(CAP_MAC_ADMIN)) {
3363 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3364 if (sklep->smk_label == skp) {
3372 new = prepare_creds();
3376 tsp = new->security;
3377 tsp->smk_task = skp;
3379 * process can change its label only once
3381 smk_destroy_label_list(&tsp->smk_relabel);
3388 * smack_unix_stream_connect - Smack access on UDS
3390 * @other: the other sock
3393 * Return 0 if a subject with the smack of sock could access
3394 * an object with the smack of other, otherwise an error code
3396 static int smack_unix_stream_connect(struct sock *sock,
3397 struct sock *other, struct sock *newsk)
3399 struct smack_known *skp;
3400 struct smack_known *okp;
3401 struct socket_smack *ssp = sock->sk_security;
3402 struct socket_smack *osp = other->sk_security;
3403 struct socket_smack *nsp = newsk->sk_security;
3404 struct smk_audit_info ad;
3407 struct lsm_network_audit net;
3410 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3414 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3415 smk_ad_setfield_u_net_sk(&ad, other);
3417 rc = smk_access(skp, okp->smk_known, MAY_WRITE, &ad);
3418 rc = smk_bu_note("UDS connect", skp, okp->smk_known,
3423 rc = smk_access(okp, skp->smk_known, MAY_WRITE, &ad);
3424 rc = smk_bu_note("UDS connect", okp, skp->smk_known,
3430 * Cross reference the peer labels for SO_PEERSEC.
3433 nsp->smk_packet = ssp->smk_out;
3434 ssp->smk_packet = osp->smk_out;
3441 * smack_unix_may_send - Smack access on UDS
3443 * @other: the other socket
3445 * Return 0 if a subject with the smack of sock could access
3446 * an object with the smack of other, otherwise an error code
3448 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3450 struct socket_smack *ssp = sock->sk->sk_security;
3451 struct socket_smack *osp = other->sk->sk_security;
3452 struct smack_known *skp;
3453 struct smk_audit_info ad;
3457 struct lsm_network_audit net;
3459 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3460 smk_ad_setfield_u_net_sk(&ad, other->sk);
3463 if (smack_privileged(CAP_MAC_OVERRIDE))
3468 rc = smk_access(skp, osp->smk_in->smk_known, MAY_WRITE, &ad);
3469 rc = smk_bu_note("UDS send", skp, osp->smk_in->smk_known,
3475 * smack_socket_sendmsg - Smack check based on destination host
3478 * @size: the size of the message
3480 * Return 0 if the current subject can write to the destination host.
3481 * For IPv4 this is only a question if the destination is a single label host.
3482 * For IPv6 this is a check against the label of the port.
3484 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3487 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3488 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3492 * Perfectly reasonable for this to be NULL
3497 switch (sock->sk->sk_family) {
3499 rc = smack_netlabel_send(sock->sk, sip);
3502 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3509 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3510 * @sap: netlabel secattr
3511 * @ssp: socket security information
3513 * Returns a pointer to a Smack label entry found on the label list.
3515 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3516 struct socket_smack *ssp)
3518 struct smack_known *skp;
3523 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3525 * Looks like a CIPSO packet.
3526 * If there are flags but no level netlabel isn't
3527 * behaving the way we expect it to.
3529 * Look it up in the label table
3530 * Without guidance regarding the smack value
3531 * for the packet fall back on the network
3535 list_for_each_entry(skp, &smack_known_list, list) {
3536 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3539 * Compare the catsets. Use the netlbl APIs.
3541 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3542 if ((skp->smk_netlabel.flags &
3543 NETLBL_SECATTR_MLS_CAT) == 0)
3547 for (acat = -1, kcat = -1; acat == kcat; ) {
3548 acat = netlbl_secattr_catmap_walk(
3549 sap->attr.mls.cat, acat + 1);
3550 kcat = netlbl_secattr_catmap_walk(
3551 skp->smk_netlabel.attr.mls.cat,
3553 if (acat < 0 || kcat < 0)
3566 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3567 return &smack_known_web;
3568 return &smack_known_star;
3570 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3572 * Looks like a fallback, which gives us a secid.
3574 skp = smack_from_secid(sap->attr.secid);
3576 * This has got to be a bug because it is
3577 * impossible to specify a fallback without
3578 * specifying the label, which will ensure
3579 * it has a secid, and the only way to get a
3580 * secid is from a fallback.
3582 BUG_ON(skp == NULL);
3586 * Without guidance regarding the smack value
3587 * for the packet fall back on the network
3590 return smack_net_ambient;
3593 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3597 int proto = -EINVAL;
3598 struct ipv6hdr _ipv6h;
3599 struct ipv6hdr *ip6;
3601 struct tcphdr _tcph, *th;
3602 struct udphdr _udph, *uh;
3603 struct dccp_hdr _dccph, *dh;
3607 offset = skb_network_offset(skb);
3608 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3611 sip->sin6_addr = ip6->saddr;
3613 nexthdr = ip6->nexthdr;
3614 offset += sizeof(_ipv6h);
3615 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3622 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3624 sip->sin6_port = th->source;
3627 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3629 sip->sin6_port = uh->source;
3632 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3634 sip->sin6_port = dh->dccph_sport;
3641 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3645 * Returns 0 if the packet should be delivered, an error code otherwise
3647 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3649 struct netlbl_lsm_secattr secattr;
3650 struct socket_smack *ssp = sk->sk_security;
3651 struct smack_known *skp;
3652 struct sockaddr_in6 sadd;
3654 struct smk_audit_info ad;
3656 struct lsm_network_audit net;
3658 switch (sk->sk_family) {
3661 * Translate what netlabel gave us.
3663 netlbl_secattr_init(&secattr);
3665 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3667 skp = smack_from_secattr(&secattr, ssp);
3669 skp = smack_net_ambient;
3671 netlbl_secattr_destroy(&secattr);
3674 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3675 ad.a.u.net->family = sk->sk_family;
3676 ad.a.u.net->netif = skb->skb_iif;
3677 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3680 * Receiving a packet requires that the other end
3681 * be able to write here. Read access is not required.
3682 * This is the simplist possible security model
3686 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3687 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in->smk_known,
3691 netlbl_skbuff_err(skb, rc, 0);
3694 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3695 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3696 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3705 * smack_socket_getpeersec_stream - pull in packet label
3707 * @optval: user's destination
3708 * @optlen: size thereof
3711 * returns zero on success, an error code otherwise
3713 static int smack_socket_getpeersec_stream(struct socket *sock,
3714 char __user *optval,
3715 int __user *optlen, unsigned len)
3717 struct socket_smack *ssp;
3722 ssp = sock->sk->sk_security;
3723 if (ssp->smk_packet != NULL) {
3724 rcp = ssp->smk_packet->smk_known;
3725 slen = strlen(rcp) + 1;
3730 else if (copy_to_user(optval, rcp, slen) != 0)
3733 if (put_user(slen, optlen) != 0)
3741 * smack_socket_getpeersec_dgram - pull in packet label
3742 * @sock: the peer socket
3744 * @secid: pointer to where to put the secid of the packet
3746 * Sets the netlabel socket state on sk from parent
3748 static int smack_socket_getpeersec_dgram(struct socket *sock,
3749 struct sk_buff *skb, u32 *secid)
3752 struct netlbl_lsm_secattr secattr;
3753 struct socket_smack *ssp = NULL;
3754 struct smack_known *skp;
3755 int family = PF_UNSPEC;
3756 u32 s = 0; /* 0 is the invalid secid */
3760 if (skb->protocol == htons(ETH_P_IP))
3762 else if (skb->protocol == htons(ETH_P_IPV6))
3765 if (family == PF_UNSPEC && sock != NULL)
3766 family = sock->sk->sk_family;
3768 if (family == PF_UNIX) {
3769 ssp = sock->sk->sk_security;
3770 s = ssp->smk_out->smk_secid;
3771 } else if (family == PF_INET || family == PF_INET6) {
3773 * Translate what netlabel gave us.
3775 if (sock != NULL && sock->sk != NULL)
3776 ssp = sock->sk->sk_security;
3777 netlbl_secattr_init(&secattr);
3778 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3780 skp = smack_from_secattr(&secattr, ssp);
3783 netlbl_secattr_destroy(&secattr);
3792 * smack_sock_graft - Initialize a newly created socket with an existing sock
3794 * @parent: parent socket
3796 * Set the smk_{in,out} state of an existing sock based on the process that
3797 * is creating the new socket.
3799 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3801 struct socket_smack *ssp;
3802 struct smack_known *skp = smk_of_current();
3805 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3808 ssp = sk->sk_security;
3811 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3815 * smack_inet_conn_request - Smack access check on connect
3816 * @sk: socket involved
3820 * Returns 0 if a task with the packet label could write to
3821 * the socket, otherwise an error code
3823 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3824 struct request_sock *req)
3826 u16 family = sk->sk_family;
3827 struct smack_known *skp;
3828 struct socket_smack *ssp = sk->sk_security;
3829 struct netlbl_lsm_secattr secattr;
3830 struct sockaddr_in addr;
3834 struct smk_audit_info ad;
3836 struct lsm_network_audit net;
3839 if (family == PF_INET6) {
3841 * Handle mapped IPv4 packets arriving
3842 * via IPv6 sockets. Don't set up netlabel
3843 * processing on IPv6.
3845 if (skb->protocol == htons(ETH_P_IP))
3851 netlbl_secattr_init(&secattr);
3852 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3854 skp = smack_from_secattr(&secattr, ssp);
3856 skp = &smack_known_huh;
3857 netlbl_secattr_destroy(&secattr);
3860 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3861 ad.a.u.net->family = family;
3862 ad.a.u.net->netif = skb->skb_iif;
3863 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3866 * Receiving a packet requires that the other end be able to write
3867 * here. Read access is not required.
3870 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3871 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in->smk_known,
3877 * Save the peer's label in the request_sock so we can later setup
3878 * smk_packet in the child socket so that SO_PEERCRED can report it.
3880 req->peer_secid = skp->smk_secid;
3883 * We need to decide if we want to label the incoming connection here
3884 * if we do we only need to label the request_sock and the stack will
3885 * propagate the wire-label to the sock when it is created.
3888 addr.sin_addr.s_addr = hdr->saddr;
3890 hsp = smack_host_label(&addr);
3894 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3896 netlbl_req_delattr(req);
3902 * smack_inet_csk_clone - Copy the connection information to the new socket
3903 * @sk: the new socket
3904 * @req: the connection's request_sock
3906 * Transfer the connection's peer label to the newly created socket.
3908 static void smack_inet_csk_clone(struct sock *sk,
3909 const struct request_sock *req)
3911 struct socket_smack *ssp = sk->sk_security;
3912 struct smack_known *skp;
3914 if (req->peer_secid != 0) {
3915 skp = smack_from_secid(req->peer_secid);
3916 ssp->smk_packet = skp;
3918 ssp->smk_packet = NULL;
3922 * Key management security hooks
3924 * Casey has not tested key support very heavily.
3925 * The permission check is most likely too restrictive.
3926 * If you care about keys please have a look.
3931 * smack_key_alloc - Set the key security blob
3933 * @cred: the credentials to use
3936 * No allocation required
3940 static int smack_key_alloc(struct key *key, const struct cred *cred,
3941 unsigned long flags)
3943 struct smack_known *skp = smk_of_task(cred->security);
3945 key->security = skp->smk_known;
3950 * smack_key_free - Clear the key security blob
3953 * Clear the blob pointer
3955 static void smack_key_free(struct key *key)
3957 key->security = NULL;
3961 * smack_key_permission - Smack access on a key
3962 * @key_ref: gets to the object
3963 * @cred: the credentials to use
3966 * Return 0 if the task has read and write to the object,
3967 * an error code otherwise
3969 static int smack_key_permission(key_ref_t key_ref,
3970 const struct cred *cred, key_perm_t perm)
3973 struct smk_audit_info ad;
3974 struct smack_known *tkp = smk_of_task(cred->security);
3977 keyp = key_ref_to_ptr(key_ref);
3981 * If the key hasn't been initialized give it access so that
3984 if (keyp->security == NULL)
3987 * This should not occur
3992 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3993 ad.a.u.key_struct.key = keyp->serial;
3994 ad.a.u.key_struct.key_desc = keyp->description;
3996 rc = smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
3997 rc = smk_bu_note("key access", tkp, keyp->security, MAY_READWRITE, rc);
4000 #endif /* CONFIG_KEYS */
4005 * Audit requires a unique representation of each Smack specific
4006 * rule. This unique representation is used to distinguish the
4007 * object to be audited from remaining kernel objects and also
4008 * works as a glue between the audit hooks.
4010 * Since repository entries are added but never deleted, we'll use
4011 * the smack_known label address related to the given audit rule as
4012 * the needed unique representation. This also better fits the smack
4013 * model where nearly everything is a label.
4018 * smack_audit_rule_init - Initialize a smack audit rule
4019 * @field: audit rule fields given from user-space (audit.h)
4020 * @op: required testing operator (=, !=, >, <, ...)
4021 * @rulestr: smack label to be audited
4022 * @vrule: pointer to save our own audit rule representation
4024 * Prepare to audit cases where (@field @op @rulestr) is true.
4025 * The label to be audited is created if necessay.
4027 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4029 char **rule = (char **)vrule;
4032 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4035 if (op != Audit_equal && op != Audit_not_equal)
4038 *rule = smk_import(rulestr, 0);
4044 * smack_audit_rule_known - Distinguish Smack audit rules
4045 * @krule: rule of interest, in Audit kernel representation format
4047 * This is used to filter Smack rules from remaining Audit ones.
4048 * If it's proved that this rule belongs to us, the
4049 * audit_rule_match hook will be called to do the final judgement.
4051 static int smack_audit_rule_known(struct audit_krule *krule)
4053 struct audit_field *f;
4056 for (i = 0; i < krule->field_count; i++) {
4057 f = &krule->fields[i];
4059 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4067 * smack_audit_rule_match - Audit given object ?
4068 * @secid: security id for identifying the object to test
4069 * @field: audit rule flags given from user-space
4070 * @op: required testing operator
4071 * @vrule: smack internal rule presentation
4072 * @actx: audit context associated with the check
4074 * The core Audit hook. It's used to take the decision of
4075 * whether to audit or not to audit a given object.
4077 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4078 struct audit_context *actx)
4080 struct smack_known *skp;
4084 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
4085 "Smack: missing rule\n");
4089 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4092 skp = smack_from_secid(secid);
4095 * No need to do string comparisons. If a match occurs,
4096 * both pointers will point to the same smack_known
4099 if (op == Audit_equal)
4100 return (rule == skp->smk_known);
4101 if (op == Audit_not_equal)
4102 return (rule != skp->smk_known);
4108 * smack_audit_rule_free - free smack rule representation
4109 * @vrule: rule to be freed.
4111 * No memory was allocated.
4113 static void smack_audit_rule_free(void *vrule)
4118 #endif /* CONFIG_AUDIT */
4121 * smack_secid_to_secctx - return the smack label for a secid
4122 * @secid: incoming integer
4123 * @secdata: destination
4124 * @seclen: how long it is
4126 * Exists for networking code.
4128 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4130 struct smack_known *skp = smack_from_secid(secid);
4133 *secdata = skp->smk_known;
4134 *seclen = strlen(skp->smk_known);
4139 * smack_secctx_to_secid - return the secid for a smack label
4140 * @secdata: smack label
4141 * @seclen: how long result is
4142 * @secid: outgoing integer
4144 * Exists for audit and networking code.
4146 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4148 *secid = smack_to_secid(secdata);
4153 * smack_release_secctx - don't do anything.
4157 * Exists to make sure nothing gets done, and properly
4159 static void smack_release_secctx(char *secdata, u32 seclen)
4163 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4165 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4168 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4170 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4173 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4176 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4184 struct security_operations smack_ops = {
4187 .ptrace_access_check = smack_ptrace_access_check,
4188 .ptrace_traceme = smack_ptrace_traceme,
4189 .syslog = smack_syslog,
4191 .sb_alloc_security = smack_sb_alloc_security,
4192 .sb_free_security = smack_sb_free_security,
4193 .sb_copy_data = smack_sb_copy_data,
4194 .sb_kern_mount = smack_sb_kern_mount,
4195 .sb_statfs = smack_sb_statfs,
4197 .bprm_set_creds = smack_bprm_set_creds,
4198 .bprm_committing_creds = smack_bprm_committing_creds,
4199 .bprm_secureexec = smack_bprm_secureexec,
4201 .inode_alloc_security = smack_inode_alloc_security,
4202 .inode_free_security = smack_inode_free_security,
4203 .inode_init_security = smack_inode_init_security,
4204 .inode_link = smack_inode_link,
4205 .inode_unlink = smack_inode_unlink,
4206 .inode_rmdir = smack_inode_rmdir,
4207 .inode_rename = smack_inode_rename,
4208 .inode_permission = smack_inode_permission,
4209 .inode_setattr = smack_inode_setattr,
4210 .inode_getattr = smack_inode_getattr,
4211 .inode_setxattr = smack_inode_setxattr,
4212 .inode_post_setxattr = smack_inode_post_setxattr,
4213 .inode_getxattr = smack_inode_getxattr,
4214 .inode_removexattr = smack_inode_removexattr,
4215 .inode_getsecurity = smack_inode_getsecurity,
4216 .inode_setsecurity = smack_inode_setsecurity,
4217 .inode_listsecurity = smack_inode_listsecurity,
4218 .inode_getsecid = smack_inode_getsecid,
4220 .file_permission = smack_file_permission,
4221 .file_alloc_security = smack_file_alloc_security,
4222 .file_free_security = smack_file_free_security,
4223 .file_ioctl = smack_file_ioctl,
4224 .file_lock = smack_file_lock,
4225 .file_fcntl = smack_file_fcntl,
4226 .mmap_file = smack_mmap_file,
4227 .mmap_addr = cap_mmap_addr,
4228 .file_set_fowner = smack_file_set_fowner,
4229 .file_send_sigiotask = smack_file_send_sigiotask,
4230 .file_receive = smack_file_receive,
4232 .file_open = smack_file_open,
4234 .cred_alloc_blank = smack_cred_alloc_blank,
4235 .cred_free = smack_cred_free,
4236 .cred_prepare = smack_cred_prepare,
4237 .cred_transfer = smack_cred_transfer,
4238 .kernel_act_as = smack_kernel_act_as,
4239 .kernel_create_files_as = smack_kernel_create_files_as,
4240 .task_setpgid = smack_task_setpgid,
4241 .task_getpgid = smack_task_getpgid,
4242 .task_getsid = smack_task_getsid,
4243 .task_getsecid = smack_task_getsecid,
4244 .task_setnice = smack_task_setnice,
4245 .task_setioprio = smack_task_setioprio,
4246 .task_getioprio = smack_task_getioprio,
4247 .task_setscheduler = smack_task_setscheduler,
4248 .task_getscheduler = smack_task_getscheduler,
4249 .task_movememory = smack_task_movememory,
4250 .task_kill = smack_task_kill,
4251 .task_wait = smack_task_wait,
4252 .task_to_inode = smack_task_to_inode,
4254 .ipc_permission = smack_ipc_permission,
4255 .ipc_getsecid = smack_ipc_getsecid,
4257 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4258 .msg_msg_free_security = smack_msg_msg_free_security,
4260 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4261 .msg_queue_free_security = smack_msg_queue_free_security,
4262 .msg_queue_associate = smack_msg_queue_associate,
4263 .msg_queue_msgctl = smack_msg_queue_msgctl,
4264 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4265 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4267 .shm_alloc_security = smack_shm_alloc_security,
4268 .shm_free_security = smack_shm_free_security,
4269 .shm_associate = smack_shm_associate,
4270 .shm_shmctl = smack_shm_shmctl,
4271 .shm_shmat = smack_shm_shmat,
4273 .sem_alloc_security = smack_sem_alloc_security,
4274 .sem_free_security = smack_sem_free_security,
4275 .sem_associate = smack_sem_associate,
4276 .sem_semctl = smack_sem_semctl,
4277 .sem_semop = smack_sem_semop,
4279 .d_instantiate = smack_d_instantiate,
4281 .getprocattr = smack_getprocattr,
4282 .setprocattr = smack_setprocattr,
4284 .unix_stream_connect = smack_unix_stream_connect,
4285 .unix_may_send = smack_unix_may_send,
4287 .socket_post_create = smack_socket_post_create,
4288 .socket_bind = smack_socket_bind,
4289 .socket_connect = smack_socket_connect,
4290 .socket_sendmsg = smack_socket_sendmsg,
4291 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4292 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4293 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4294 .sk_alloc_security = smack_sk_alloc_security,
4295 .sk_free_security = smack_sk_free_security,
4296 .sock_graft = smack_sock_graft,
4297 .inet_conn_request = smack_inet_conn_request,
4298 .inet_csk_clone = smack_inet_csk_clone,
4300 /* key management security hooks */
4302 .key_alloc = smack_key_alloc,
4303 .key_free = smack_key_free,
4304 .key_permission = smack_key_permission,
4305 #endif /* CONFIG_KEYS */
4309 .audit_rule_init = smack_audit_rule_init,
4310 .audit_rule_known = smack_audit_rule_known,
4311 .audit_rule_match = smack_audit_rule_match,
4312 .audit_rule_free = smack_audit_rule_free,
4313 #endif /* CONFIG_AUDIT */
4315 .secid_to_secctx = smack_secid_to_secctx,
4316 .secctx_to_secid = smack_secctx_to_secid,
4317 .release_secctx = smack_release_secctx,
4318 .inode_notifysecctx = smack_inode_notifysecctx,
4319 .inode_setsecctx = smack_inode_setsecctx,
4320 .inode_getsecctx = smack_inode_getsecctx,
4324 static __init void init_smack_known_list(void)
4327 * Initialize rule list locks
4329 mutex_init(&smack_known_huh.smk_rules_lock);
4330 mutex_init(&smack_known_hat.smk_rules_lock);
4331 mutex_init(&smack_known_floor.smk_rules_lock);
4332 mutex_init(&smack_known_star.smk_rules_lock);
4333 mutex_init(&smack_known_invalid.smk_rules_lock);
4334 mutex_init(&smack_known_web.smk_rules_lock);
4336 * Initialize rule lists
4338 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4339 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4340 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4341 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4342 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4343 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4345 * Create the known labels list
4347 smk_insert_entry(&smack_known_huh);
4348 smk_insert_entry(&smack_known_hat);
4349 smk_insert_entry(&smack_known_star);
4350 smk_insert_entry(&smack_known_floor);
4351 smk_insert_entry(&smack_known_invalid);
4352 smk_insert_entry(&smack_known_web);
4355 /* KMEM caches for fast and thrifty allocations */
4356 struct kmem_cache *smack_rule_cache;
4357 struct kmem_cache *smack_master_list_cache;
4360 * smack_init - initialize the smack system
4364 static __init int smack_init(void)
4367 struct task_smack *tsp;
4369 if (!security_module_enable(&smack_ops))
4372 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4373 if (!smack_rule_cache)
4376 smack_master_list_cache = KMEM_CACHE(smack_master_list, 0);
4377 if (!smack_master_list_cache) {
4378 kmem_cache_destroy(smack_rule_cache);
4382 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4383 if (!smack_inode_cache) {
4384 kmem_cache_destroy(smack_master_list_cache);
4385 kmem_cache_destroy(smack_rule_cache);
4389 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4392 kmem_cache_destroy(smack_master_list_cache);
4393 kmem_cache_destroy(smack_rule_cache);
4394 kmem_cache_destroy(smack_inode_cache);
4398 printk(KERN_INFO "Smack: Initializing.\n");
4401 * Set the security state for the initial task.
4403 cred = (struct cred *) current->cred;
4404 cred->security = tsp;
4406 /* initialize the smack_known_list */
4407 init_smack_known_list();
4412 if (register_security(&smack_ops))
4413 panic("smack: Unable to register with kernel.\n");
4419 * Smack requires early initialization in order to label
4420 * all processes and objects when they are created.
4422 security_initcall(smack_init);