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);
57 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
58 static void smk_bu_mode(int mode, char *s)
68 if (mode & MAY_APPEND)
70 if (mode & MAY_TRANSMUTE)
80 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
81 static int smk_bu_note(char *note, struct smack_known *sskp, char *osp,
84 char acc[SMK_NUM_ACCESS_TYPE + 1];
89 smk_bu_mode(mode, acc);
90 pr_info("Smack Bringup: (%s %s %s) %s\n",
91 sskp->smk_known, osp, acc, note);
95 #define smk_bu_note(note, sskp, osp, mode, RC) (RC)
98 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
99 static int smk_bu_current(char *note, char *osp, int mode, int rc)
101 struct task_smack *tsp = current_security();
102 char acc[SMK_NUM_ACCESS_TYPE + 1];
107 smk_bu_mode(mode, acc);
108 pr_info("Smack Bringup: (%s %s %s) %s %s\n",
109 tsp->smk_task->smk_known, osp, acc, current->comm, note);
113 #define smk_bu_current(note, osp, mode, RC) (RC)
116 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
117 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
119 struct task_smack *tsp = current_security();
120 struct task_smack *otsp = task_security(otp);
121 char acc[SMK_NUM_ACCESS_TYPE + 1];
126 smk_bu_mode(mode, acc);
127 pr_info("Smack Bringup: (%s %s %s) %s to %s\n",
128 tsp->smk_task->smk_known, otsp->smk_task->smk_known, acc,
129 current->comm, otp->comm);
133 #define smk_bu_task(otp, mode, RC) (RC)
136 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
137 static int smk_bu_inode(struct inode *inode, int mode, int rc)
139 struct task_smack *tsp = current_security();
140 char acc[SMK_NUM_ACCESS_TYPE + 1];
145 smk_bu_mode(mode, acc);
146 pr_info("Smack Bringup: (%s %s %s) inode=(%s %ld) %s\n",
147 tsp->smk_task->smk_known, smk_of_inode(inode), acc,
148 inode->i_sb->s_id, inode->i_ino, current->comm);
152 #define smk_bu_inode(inode, mode, RC) (RC)
155 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
156 static int smk_bu_file(struct file *file, int mode, int rc)
158 struct task_smack *tsp = current_security();
159 struct smack_known *sskp = tsp->smk_task;
160 struct inode *inode = file->f_inode;
161 char acc[SMK_NUM_ACCESS_TYPE + 1];
166 smk_bu_mode(mode, acc);
167 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %s) %s\n",
168 sskp->smk_known, (char *)file->f_security, acc,
169 inode->i_sb->s_id, inode->i_ino, file->f_dentry->d_name.name,
174 #define smk_bu_file(file, mode, RC) (RC)
177 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
178 static int smk_bu_credfile(const struct cred *cred, struct file *file,
181 struct task_smack *tsp = cred->security;
182 struct smack_known *sskp = tsp->smk_task;
183 struct inode *inode = file->f_inode;
184 char acc[SMK_NUM_ACCESS_TYPE + 1];
189 smk_bu_mode(mode, acc);
190 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %s) %s\n",
191 sskp->smk_known, smk_of_inode(inode), acc,
192 inode->i_sb->s_id, inode->i_ino, file->f_dentry->d_name.name,
197 #define smk_bu_credfile(cred, file, mode, RC) (RC)
201 * smk_fetch - Fetch the smack label from a file.
202 * @ip: a pointer to the inode
203 * @dp: a pointer to the dentry
205 * Returns a pointer to the master list entry for the Smack label
206 * or NULL if there was no label to fetch.
208 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
213 struct smack_known *skp = NULL;
215 if (ip->i_op->getxattr == NULL)
218 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
222 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
224 skp = smk_import_entry(buffer, rc);
232 * new_inode_smack - allocate an inode security blob
233 * @smack: a pointer to the Smack label to use in the blob
235 * Returns the new blob or NULL if there's no memory available
237 struct inode_smack *new_inode_smack(char *smack)
239 struct inode_smack *isp;
241 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
245 isp->smk_inode = smack;
247 mutex_init(&isp->smk_lock);
253 * new_task_smack - allocate a task security blob
254 * @smack: a pointer to the Smack label to use in the blob
256 * Returns the new blob or NULL if there's no memory available
258 static struct task_smack *new_task_smack(struct smack_known *task,
259 struct smack_known *forked, gfp_t gfp)
261 struct task_smack *tsp;
263 tsp = kzalloc(sizeof(struct task_smack), gfp);
267 tsp->smk_task = task;
268 tsp->smk_forked = forked;
269 INIT_LIST_HEAD(&tsp->smk_rules);
270 mutex_init(&tsp->smk_rules_lock);
276 * smk_copy_rules - copy a rule set
277 * @nhead - new rules header pointer
278 * @ohead - old rules header pointer
280 * Returns 0 on success, -ENOMEM on error
282 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
285 struct smack_rule *nrp;
286 struct smack_rule *orp;
289 INIT_LIST_HEAD(nhead);
291 list_for_each_entry_rcu(orp, ohead, list) {
292 nrp = kzalloc(sizeof(struct smack_rule), gfp);
298 list_add_rcu(&nrp->list, nhead);
304 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
305 * @mode - input mode in form of PTRACE_MODE_*
307 * Returns a converted MAY_* mode usable by smack rules
309 static inline unsigned int smk_ptrace_mode(unsigned int mode)
312 case PTRACE_MODE_READ:
314 case PTRACE_MODE_ATTACH:
315 return MAY_READWRITE;
322 * smk_ptrace_rule_check - helper for ptrace access
323 * @tracer: tracer process
324 * @tracee_label: label of the process that's about to be traced,
325 * the pointer must originate from smack structures
326 * @mode: ptrace attachment mode (PTRACE_MODE_*)
327 * @func: name of the function that called us, used for audit
329 * Returns 0 on access granted, -error on error
331 static int smk_ptrace_rule_check(struct task_struct *tracer, char *tracee_label,
332 unsigned int mode, const char *func)
335 struct smk_audit_info ad, *saip = NULL;
336 struct task_smack *tsp;
337 struct smack_known *skp;
339 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
340 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
341 smk_ad_setfield_u_tsk(&ad, tracer);
345 tsp = task_security(tracer);
346 skp = smk_of_task(tsp);
348 if ((mode & PTRACE_MODE_ATTACH) &&
349 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
350 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
351 if (skp->smk_known == tracee_label)
353 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
355 else if (capable(CAP_SYS_PTRACE))
361 smack_log(skp->smk_known, tracee_label, 0, rc, saip);
366 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
367 rc = smk_tskacc(tsp, tracee_label, smk_ptrace_mode(mode), saip);
373 * We he, that is fun!
377 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
378 * @ctp: child task pointer
379 * @mode: ptrace attachment mode (PTRACE_MODE_*)
381 * Returns 0 if access is OK, an error code otherwise
383 * Do the capability checks.
385 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
388 struct smack_known *skp;
390 rc = cap_ptrace_access_check(ctp, mode);
394 skp = smk_of_task(task_security(ctp));
396 rc = smk_ptrace_rule_check(current, skp->smk_known, mode, __func__);
401 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
402 * @ptp: parent task pointer
404 * Returns 0 if access is OK, an error code otherwise
406 * Do the capability checks, and require PTRACE_MODE_ATTACH.
408 static int smack_ptrace_traceme(struct task_struct *ptp)
411 struct smack_known *skp;
413 rc = cap_ptrace_traceme(ptp);
417 skp = smk_of_task(current_security());
419 rc = smk_ptrace_rule_check(ptp, skp->smk_known,
420 PTRACE_MODE_ATTACH, __func__);
425 * smack_syslog - Smack approval on syslog
426 * @type: message type
428 * Returns 0 on success, error code otherwise.
430 static int smack_syslog(int typefrom_file)
433 struct smack_known *skp = smk_of_current();
435 if (smack_privileged(CAP_MAC_OVERRIDE))
438 if (smack_syslog_label != NULL && smack_syslog_label != skp)
450 * smack_sb_alloc_security - allocate a superblock blob
451 * @sb: the superblock getting the blob
453 * Returns 0 on success or -ENOMEM on error.
455 static int smack_sb_alloc_security(struct super_block *sb)
457 struct superblock_smack *sbsp;
459 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
464 sbsp->smk_root = smack_known_floor.smk_known;
465 sbsp->smk_default = smack_known_floor.smk_known;
466 sbsp->smk_floor = smack_known_floor.smk_known;
467 sbsp->smk_hat = smack_known_hat.smk_known;
469 * smk_initialized will be zero from kzalloc.
471 sb->s_security = sbsp;
477 * smack_sb_free_security - free a superblock blob
478 * @sb: the superblock getting the blob
481 static void smack_sb_free_security(struct super_block *sb)
483 kfree(sb->s_security);
484 sb->s_security = NULL;
488 * smack_sb_copy_data - copy mount options data for processing
489 * @orig: where to start
490 * @smackopts: mount options string
492 * Returns 0 on success or -ENOMEM on error.
494 * Copy the Smack specific mount options out of the mount
497 static int smack_sb_copy_data(char *orig, char *smackopts)
499 char *cp, *commap, *otheropts, *dp;
501 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
502 if (otheropts == NULL)
505 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
506 if (strstr(cp, SMK_FSDEFAULT) == cp)
508 else if (strstr(cp, SMK_FSFLOOR) == cp)
510 else if (strstr(cp, SMK_FSHAT) == cp)
512 else if (strstr(cp, SMK_FSROOT) == cp)
514 else if (strstr(cp, SMK_FSTRANS) == cp)
519 commap = strchr(cp, ',');
528 strcpy(orig, otheropts);
529 free_page((unsigned long)otheropts);
535 * smack_sb_kern_mount - Smack specific mount processing
536 * @sb: the file system superblock
537 * @flags: the mount flags
538 * @data: the smack mount options
540 * Returns 0 on success, an error code on failure
542 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
544 struct dentry *root = sb->s_root;
545 struct inode *inode = root->d_inode;
546 struct superblock_smack *sp = sb->s_security;
547 struct inode_smack *isp;
548 struct smack_known *skp;
555 if (sp->smk_initialized)
558 sp->smk_initialized = 1;
560 for (op = data; op != NULL; op = commap) {
561 commap = strchr(op, ',');
565 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
566 op += strlen(SMK_FSHAT);
567 nsp = smk_import(op, 0);
572 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
573 op += strlen(SMK_FSFLOOR);
574 nsp = smk_import(op, 0);
579 } else if (strncmp(op, SMK_FSDEFAULT,
580 strlen(SMK_FSDEFAULT)) == 0) {
581 op += strlen(SMK_FSDEFAULT);
582 nsp = smk_import(op, 0);
584 sp->smk_default = nsp;
587 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
588 op += strlen(SMK_FSROOT);
589 nsp = smk_import(op, 0);
594 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
595 op += strlen(SMK_FSTRANS);
596 nsp = smk_import(op, 0);
605 if (!smack_privileged(CAP_MAC_ADMIN)) {
607 * Unprivileged mounts don't get to specify Smack values.
612 * Unprivileged mounts get root and default from the caller.
614 skp = smk_of_current();
615 sp->smk_root = skp->smk_known;
616 sp->smk_default = skp->smk_known;
619 * Initialize the root inode.
621 isp = inode->i_security;
623 isp = new_inode_smack(sp->smk_root);
626 inode->i_security = isp;
628 isp->smk_inode = sp->smk_root;
631 isp->smk_flags |= SMK_INODE_TRANSMUTE;
637 * smack_sb_statfs - Smack check on statfs
638 * @dentry: identifies the file system in question
640 * Returns 0 if current can read the floor of the filesystem,
641 * and error code otherwise
643 static int smack_sb_statfs(struct dentry *dentry)
645 struct superblock_smack *sbp = dentry->d_sb->s_security;
647 struct smk_audit_info ad;
649 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
650 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
652 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
653 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
662 * smack_bprm_set_creds - set creds for exec
663 * @bprm: the exec information
665 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
667 static int smack_bprm_set_creds(struct linux_binprm *bprm)
669 struct inode *inode = file_inode(bprm->file);
670 struct task_smack *bsp = bprm->cred->security;
671 struct inode_smack *isp;
674 rc = cap_bprm_set_creds(bprm);
678 if (bprm->cred_prepared)
681 isp = inode->i_security;
682 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
685 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
686 struct task_struct *tracer;
690 tracer = ptrace_parent(current);
691 if (likely(tracer != NULL))
692 rc = smk_ptrace_rule_check(tracer,
693 isp->smk_task->smk_known,
700 } else if (bprm->unsafe)
703 bsp->smk_task = isp->smk_task;
704 bprm->per_clear |= PER_CLEAR_ON_SETID;
710 * smack_bprm_committing_creds - Prepare to install the new credentials
713 * @bprm: binprm for exec
715 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
717 struct task_smack *bsp = bprm->cred->security;
719 if (bsp->smk_task != bsp->smk_forked)
720 current->pdeath_signal = 0;
724 * smack_bprm_secureexec - Return the decision to use secureexec.
725 * @bprm: binprm for exec
727 * Returns 0 on success.
729 static int smack_bprm_secureexec(struct linux_binprm *bprm)
731 struct task_smack *tsp = current_security();
732 int ret = cap_bprm_secureexec(bprm);
734 if (!ret && (tsp->smk_task != tsp->smk_forked))
745 * smack_inode_alloc_security - allocate an inode blob
746 * @inode: the inode in need of a blob
748 * Returns 0 if it gets a blob, -ENOMEM otherwise
750 static int smack_inode_alloc_security(struct inode *inode)
752 struct smack_known *skp = smk_of_current();
754 inode->i_security = new_inode_smack(skp->smk_known);
755 if (inode->i_security == NULL)
761 * smack_inode_free_security - free an inode blob
762 * @inode: the inode with a blob
764 * Clears the blob pointer in inode
766 static void smack_inode_free_security(struct inode *inode)
768 kfree(inode->i_security);
769 inode->i_security = NULL;
773 * smack_inode_init_security - copy out the smack from an inode
777 * @name: where to put the attribute name
778 * @value: where to put the attribute value
779 * @len: where to put the length of the attribute
781 * Returns 0 if it all works out, -ENOMEM if there's no memory
783 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
784 const struct qstr *qstr, const char **name,
785 void **value, size_t *len)
787 struct inode_smack *issp = inode->i_security;
788 struct smack_known *skp = smk_of_current();
789 char *isp = smk_of_inode(inode);
790 char *dsp = smk_of_inode(dir);
794 *name = XATTR_SMACK_SUFFIX;
798 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
802 * If the access rule allows transmutation and
803 * the directory requests transmutation then
804 * by all means transmute.
805 * Mark the inode as changed.
807 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
808 smk_inode_transmutable(dir)) {
810 issp->smk_flags |= SMK_INODE_CHANGED;
813 *value = kstrdup(isp, GFP_NOFS);
825 * smack_inode_link - Smack check on link
826 * @old_dentry: the existing object
828 * @new_dentry: the new object
830 * Returns 0 if access is permitted, an error code otherwise
832 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
833 struct dentry *new_dentry)
836 struct smk_audit_info ad;
839 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
840 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
842 isp = smk_of_inode(old_dentry->d_inode);
843 rc = smk_curacc(isp, MAY_WRITE, &ad);
844 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
846 if (rc == 0 && new_dentry->d_inode != NULL) {
847 isp = smk_of_inode(new_dentry->d_inode);
848 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
849 rc = smk_curacc(isp, MAY_WRITE, &ad);
850 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
857 * smack_inode_unlink - Smack check on inode deletion
858 * @dir: containing directory object
859 * @dentry: file to unlink
861 * Returns 0 if current can write the containing directory
862 * and the object, error code otherwise
864 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
866 struct inode *ip = dentry->d_inode;
867 struct smk_audit_info ad;
870 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
871 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
874 * You need write access to the thing you're unlinking
876 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
877 rc = smk_bu_inode(ip, MAY_WRITE, rc);
880 * You also need write access to the containing directory
882 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
883 smk_ad_setfield_u_fs_inode(&ad, dir);
884 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
885 rc = smk_bu_inode(dir, MAY_WRITE, rc);
891 * smack_inode_rmdir - Smack check on directory deletion
892 * @dir: containing directory object
893 * @dentry: directory to unlink
895 * Returns 0 if current can write the containing directory
896 * and the directory, error code otherwise
898 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
900 struct smk_audit_info ad;
903 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
904 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
907 * You need write access to the thing you're removing
909 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
910 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
913 * You also need write access to the containing directory
915 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
916 smk_ad_setfield_u_fs_inode(&ad, dir);
917 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
918 rc = smk_bu_inode(dir, MAY_WRITE, rc);
925 * smack_inode_rename - Smack check on rename
926 * @old_inode: the old directory
927 * @old_dentry: unused
928 * @new_inode: the new directory
929 * @new_dentry: unused
931 * Read and write access is required on both the old and
934 * Returns 0 if access is permitted, an error code otherwise
936 static int smack_inode_rename(struct inode *old_inode,
937 struct dentry *old_dentry,
938 struct inode *new_inode,
939 struct dentry *new_dentry)
943 struct smk_audit_info ad;
945 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
946 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
948 isp = smk_of_inode(old_dentry->d_inode);
949 rc = smk_curacc(isp, MAY_READWRITE, &ad);
950 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
952 if (rc == 0 && new_dentry->d_inode != NULL) {
953 isp = smk_of_inode(new_dentry->d_inode);
954 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
955 rc = smk_curacc(isp, MAY_READWRITE, &ad);
956 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
962 * smack_inode_permission - Smack version of permission()
963 * @inode: the inode in question
964 * @mask: the access requested
966 * This is the important Smack hook.
968 * Returns 0 if access is permitted, -EACCES otherwise
970 static int smack_inode_permission(struct inode *inode, int mask)
972 struct smk_audit_info ad;
973 int no_block = mask & MAY_NOT_BLOCK;
976 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
978 * No permission to check. Existence test. Yup, it's there.
983 /* May be droppable after audit */
986 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
987 smk_ad_setfield_u_fs_inode(&ad, inode);
988 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
989 rc = smk_bu_inode(inode, mask, rc);
994 * smack_inode_setattr - Smack check for setting attributes
995 * @dentry: the object
996 * @iattr: for the force flag
998 * Returns 0 if access is permitted, an error code otherwise
1000 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1002 struct smk_audit_info ad;
1006 * Need to allow for clearing the setuid bit.
1008 if (iattr->ia_valid & ATTR_FORCE)
1010 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1011 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1013 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1014 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1019 * smack_inode_getattr - Smack check for getting attributes
1021 * @dentry: the object
1023 * Returns 0 if access is permitted, an error code otherwise
1025 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1027 struct smk_audit_info ad;
1031 path.dentry = dentry;
1034 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1035 smk_ad_setfield_u_fs_path(&ad, path);
1036 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1037 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1042 * smack_inode_setxattr - Smack check for setting xattrs
1043 * @dentry: the object
1044 * @name: name of the attribute
1049 * This protects the Smack attribute explicitly.
1051 * Returns 0 if access is permitted, an error code otherwise
1053 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1054 const void *value, size_t size, int flags)
1056 struct smk_audit_info ad;
1057 struct smack_known *skp;
1059 int check_import = 0;
1064 * Check label validity here so import won't fail in post_setxattr
1066 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1067 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1068 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1071 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1072 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1076 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1078 if (size != TRANS_TRUE_SIZE ||
1079 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1082 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1084 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1087 if (rc == 0 && check_import) {
1088 skp = size ? smk_import_entry(value, size) : NULL;
1089 if (skp == NULL || (check_star &&
1090 (skp == &smack_known_star || skp == &smack_known_web)))
1094 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1095 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1098 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1099 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1106 * smack_inode_post_setxattr - Apply the Smack update approved above
1108 * @name: attribute name
1109 * @value: attribute value
1110 * @size: attribute size
1113 * Set the pointer in the inode blob to the entry found
1114 * in the master label list.
1116 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1117 const void *value, size_t size, int flags)
1119 struct smack_known *skp;
1120 struct inode_smack *isp = dentry->d_inode->i_security;
1122 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1123 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1127 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1128 skp = smk_import_entry(value, size);
1130 isp->smk_inode = skp->smk_known;
1132 isp->smk_inode = smack_known_invalid.smk_known;
1133 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1134 skp = smk_import_entry(value, size);
1136 isp->smk_task = skp;
1138 isp->smk_task = &smack_known_invalid;
1139 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1140 skp = smk_import_entry(value, size);
1142 isp->smk_mmap = skp;
1144 isp->smk_mmap = &smack_known_invalid;
1151 * smack_inode_getxattr - Smack check on getxattr
1152 * @dentry: the object
1155 * Returns 0 if access is permitted, an error code otherwise
1157 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1159 struct smk_audit_info ad;
1162 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1163 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1165 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1166 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1171 * smack_inode_removexattr - Smack check on removexattr
1172 * @dentry: the object
1173 * @name: name of the attribute
1175 * Removing the Smack attribute requires CAP_MAC_ADMIN
1177 * Returns 0 if access is permitted, an error code otherwise
1179 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1181 struct inode_smack *isp;
1182 struct smk_audit_info ad;
1185 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1186 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1187 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1188 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1189 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1190 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1191 if (!smack_privileged(CAP_MAC_ADMIN))
1194 rc = cap_inode_removexattr(dentry, name);
1199 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1200 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1202 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1203 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1207 isp = dentry->d_inode->i_security;
1209 * Don't do anything special for these.
1210 * XATTR_NAME_SMACKIPIN
1211 * XATTR_NAME_SMACKIPOUT
1212 * XATTR_NAME_SMACKEXEC
1214 if (strcmp(name, XATTR_NAME_SMACK) == 0)
1215 isp->smk_task = NULL;
1216 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1217 isp->smk_mmap = NULL;
1218 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1219 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1225 * smack_inode_getsecurity - get smack xattrs
1226 * @inode: the object
1227 * @name: attribute name
1228 * @buffer: where to put the result
1231 * Returns the size of the attribute or an error code
1233 static int smack_inode_getsecurity(const struct inode *inode,
1234 const char *name, void **buffer,
1237 struct socket_smack *ssp;
1238 struct socket *sock;
1239 struct super_block *sbp;
1240 struct inode *ip = (struct inode *)inode;
1245 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1246 isp = smk_of_inode(inode);
1253 * The rest of the Smack xattrs are only on sockets.
1256 if (sbp->s_magic != SOCKFS_MAGIC)
1259 sock = SOCKET_I(ip);
1260 if (sock == NULL || sock->sk == NULL)
1263 ssp = sock->sk->sk_security;
1265 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1266 isp = ssp->smk_in->smk_known;
1267 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1268 isp = ssp->smk_out->smk_known;
1283 * smack_inode_listsecurity - list the Smack attributes
1284 * @inode: the object
1285 * @buffer: where they go
1286 * @buffer_size: size of buffer
1288 * Returns 0 on success, -EINVAL otherwise
1290 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1293 int len = sizeof(XATTR_NAME_SMACK);
1295 if (buffer != NULL && len <= buffer_size)
1296 memcpy(buffer, XATTR_NAME_SMACK, len);
1302 * smack_inode_getsecid - Extract inode's security id
1303 * @inode: inode to extract the info from
1304 * @secid: where result will be saved
1306 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1308 struct inode_smack *isp = inode->i_security;
1310 *secid = smack_to_secid(isp->smk_inode);
1318 * smack_file_permission - Smack check on file operations
1324 * Should access checks be done on each read or write?
1325 * UNICOS and SELinux say yes.
1326 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1328 * I'll say no for now. Smack does not do the frequent
1329 * label changing that SELinux does.
1331 static int smack_file_permission(struct file *file, int mask)
1337 * smack_file_alloc_security - assign a file security blob
1340 * The security blob for a file is a pointer to the master
1341 * label list, so no allocation is done.
1345 static int smack_file_alloc_security(struct file *file)
1347 struct smack_known *skp = smk_of_current();
1349 file->f_security = skp->smk_known;
1354 * smack_file_free_security - clear a file security blob
1357 * The security blob for a file is a pointer to the master
1358 * label list, so no memory is freed.
1360 static void smack_file_free_security(struct file *file)
1362 file->f_security = NULL;
1366 * smack_file_ioctl - Smack check on ioctls
1371 * Relies heavily on the correct use of the ioctl command conventions.
1373 * Returns 0 if allowed, error code otherwise
1375 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1379 struct smk_audit_info ad;
1381 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1382 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1384 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1385 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1386 rc = smk_bu_file(file, MAY_WRITE, rc);
1389 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1390 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1391 rc = smk_bu_file(file, MAY_READ, rc);
1398 * smack_file_lock - Smack check on file locking
1402 * Returns 0 if current has lock access, error code otherwise
1404 static int smack_file_lock(struct file *file, unsigned int cmd)
1406 struct smk_audit_info ad;
1409 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1410 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1411 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1412 rc = smk_bu_file(file, MAY_LOCK, rc);
1417 * smack_file_fcntl - Smack check on fcntl
1419 * @cmd: what action to check
1422 * Generally these operations are harmless.
1423 * File locking operations present an obvious mechanism
1424 * for passing information, so they require write access.
1426 * Returns 0 if current has access, error code otherwise
1428 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1431 struct smk_audit_info ad;
1440 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1441 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1442 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1443 rc = smk_bu_file(file, MAY_LOCK, rc);
1447 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1448 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1449 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1450 rc = smk_bu_file(file, MAY_WRITE, rc);
1461 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1462 * if mapping anonymous memory.
1463 * @file contains the file structure for file to map (may be NULL).
1464 * @reqprot contains the protection requested by the application.
1465 * @prot contains the protection that will be applied by the kernel.
1466 * @flags contains the operational flags.
1467 * Return 0 if permission is granted.
1469 static int smack_mmap_file(struct file *file,
1470 unsigned long reqprot, unsigned long prot,
1471 unsigned long flags)
1473 struct smack_known *skp;
1474 struct smack_known *mkp;
1475 struct smack_rule *srp;
1476 struct task_smack *tsp;
1478 struct inode_smack *isp;
1487 isp = file_inode(file)->i_security;
1488 if (isp->smk_mmap == NULL)
1490 mkp = isp->smk_mmap;
1492 tsp = current_security();
1493 skp = smk_of_current();
1498 * For each Smack rule associated with the subject
1499 * label verify that the SMACK64MMAP also has access
1500 * to that rule's object label.
1502 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1503 osmack = srp->smk_object;
1505 * Matching labels always allows access.
1507 if (mkp->smk_known == osmack)
1510 * If there is a matching local rule take
1511 * that into account as well.
1513 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1516 may = srp->smk_access;
1518 may &= srp->smk_access;
1520 * If may is zero the SMACK64MMAP subject can't
1521 * possibly have less access.
1527 * Fetch the global list entry.
1528 * If there isn't one a SMACK64MMAP subject
1529 * can't have as much access as current.
1531 mmay = smk_access_entry(mkp->smk_known, osmack,
1533 if (mmay == -ENOENT) {
1538 * If there is a local entry it modifies the
1539 * potential access, too.
1541 tmay = smk_access_entry(mkp->smk_known, osmack,
1543 if (tmay != -ENOENT)
1547 * If there is any access available to current that is
1548 * not available to a SMACK64MMAP subject
1551 if ((may | mmay) != mmay) {
1563 * smack_file_set_fowner - set the file security blob value
1564 * @file: object in question
1567 * Further research may be required on this one.
1569 static int smack_file_set_fowner(struct file *file)
1571 struct smack_known *skp = smk_of_current();
1573 file->f_security = skp->smk_known;
1578 * smack_file_send_sigiotask - Smack on sigio
1579 * @tsk: The target task
1580 * @fown: the object the signal come from
1583 * Allow a privileged task to get signals even if it shouldn't
1585 * Returns 0 if a subject with the object's smack could
1586 * write to the task, an error code otherwise.
1588 static int smack_file_send_sigiotask(struct task_struct *tsk,
1589 struct fown_struct *fown, int signum)
1591 struct smack_known *skp;
1592 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1595 struct smk_audit_info ad;
1598 * struct fown_struct is never outside the context of a struct file
1600 file = container_of(fown, struct file, f_owner);
1602 /* we don't log here as rc can be overriden */
1603 skp = smk_find_entry(file->f_security);
1604 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1605 rc = smk_bu_note("sigiotask", skp, tkp->smk_known, MAY_WRITE, rc);
1606 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1609 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1610 smk_ad_setfield_u_tsk(&ad, tsk);
1611 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1616 * smack_file_receive - Smack file receive check
1619 * Returns 0 if current has access, error code otherwise
1621 static int smack_file_receive(struct file *file)
1625 struct smk_audit_info ad;
1627 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1628 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1630 * This code relies on bitmasks.
1632 if (file->f_mode & FMODE_READ)
1634 if (file->f_mode & FMODE_WRITE)
1637 rc = smk_curacc(file->f_security, may, &ad);
1638 rc = smk_bu_file(file, may, rc);
1643 * smack_file_open - Smack dentry open processing
1645 * @cred: task credential
1647 * Set the security blob in the file structure.
1648 * Allow the open only if the task has read access. There are
1649 * many read operations (e.g. fstat) that you can do with an
1650 * fd even if you have the file open write-only.
1654 static int smack_file_open(struct file *file, const struct cred *cred)
1656 struct task_smack *tsp = cred->security;
1657 struct inode_smack *isp = file_inode(file)->i_security;
1658 struct smk_audit_info ad;
1661 if (smack_privileged(CAP_MAC_OVERRIDE)) {
1662 file->f_security = isp->smk_inode;
1666 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1667 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1668 rc = smk_access(tsp->smk_task, isp->smk_inode, MAY_READ, &ad);
1669 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
1671 file->f_security = isp->smk_inode;
1681 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1682 * @new: the new credentials
1683 * @gfp: the atomicity of any memory allocations
1685 * Prepare a blank set of credentials for modification. This must allocate all
1686 * the memory the LSM module might require such that cred_transfer() can
1687 * complete without error.
1689 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1691 struct task_smack *tsp;
1693 tsp = new_task_smack(NULL, NULL, gfp);
1697 cred->security = tsp;
1704 * smack_cred_free - "free" task-level security credentials
1705 * @cred: the credentials in question
1708 static void smack_cred_free(struct cred *cred)
1710 struct task_smack *tsp = cred->security;
1711 struct smack_rule *rp;
1712 struct list_head *l;
1713 struct list_head *n;
1717 cred->security = NULL;
1719 list_for_each_safe(l, n, &tsp->smk_rules) {
1720 rp = list_entry(l, struct smack_rule, list);
1721 list_del(&rp->list);
1728 * smack_cred_prepare - prepare new set of credentials for modification
1729 * @new: the new credentials
1730 * @old: the original credentials
1731 * @gfp: the atomicity of any memory allocations
1733 * Prepare a new set of credentials for modification.
1735 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1738 struct task_smack *old_tsp = old->security;
1739 struct task_smack *new_tsp;
1742 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1743 if (new_tsp == NULL)
1746 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1750 new->security = new_tsp;
1755 * smack_cred_transfer - Transfer the old credentials to the new credentials
1756 * @new: the new credentials
1757 * @old: the original credentials
1759 * Fill in a set of blank credentials from another set of credentials.
1761 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1763 struct task_smack *old_tsp = old->security;
1764 struct task_smack *new_tsp = new->security;
1766 new_tsp->smk_task = old_tsp->smk_task;
1767 new_tsp->smk_forked = old_tsp->smk_task;
1768 mutex_init(&new_tsp->smk_rules_lock);
1769 INIT_LIST_HEAD(&new_tsp->smk_rules);
1772 /* cbs copy rule list */
1776 * smack_kernel_act_as - Set the subjective context in a set of credentials
1777 * @new: points to the set of credentials to be modified.
1778 * @secid: specifies the security ID to be set
1780 * Set the security data for a kernel service.
1782 static int smack_kernel_act_as(struct cred *new, u32 secid)
1784 struct task_smack *new_tsp = new->security;
1785 struct smack_known *skp = smack_from_secid(secid);
1790 new_tsp->smk_task = skp;
1795 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1796 * @new: points to the set of credentials to be modified
1797 * @inode: points to the inode to use as a reference
1799 * Set the file creation context in a set of credentials to the same
1800 * as the objective context of the specified inode
1802 static int smack_kernel_create_files_as(struct cred *new,
1803 struct inode *inode)
1805 struct inode_smack *isp = inode->i_security;
1806 struct task_smack *tsp = new->security;
1808 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1809 tsp->smk_task = tsp->smk_forked;
1814 * smk_curacc_on_task - helper to log task related access
1815 * @p: the task object
1816 * @access: the access requested
1817 * @caller: name of the calling function for audit
1819 * Return 0 if access is permitted
1821 static int smk_curacc_on_task(struct task_struct *p, int access,
1824 struct smk_audit_info ad;
1825 struct smack_known *skp = smk_of_task(task_security(p));
1828 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1829 smk_ad_setfield_u_tsk(&ad, p);
1830 rc = smk_curacc(skp->smk_known, access, &ad);
1831 rc = smk_bu_task(p, access, rc);
1836 * smack_task_setpgid - Smack check on setting pgid
1837 * @p: the task object
1840 * Return 0 if write access is permitted
1842 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1844 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1848 * smack_task_getpgid - Smack access check for getpgid
1849 * @p: the object task
1851 * Returns 0 if current can read the object task, error code otherwise
1853 static int smack_task_getpgid(struct task_struct *p)
1855 return smk_curacc_on_task(p, MAY_READ, __func__);
1859 * smack_task_getsid - Smack access check for getsid
1860 * @p: the object task
1862 * Returns 0 if current can read the object task, error code otherwise
1864 static int smack_task_getsid(struct task_struct *p)
1866 return smk_curacc_on_task(p, MAY_READ, __func__);
1870 * smack_task_getsecid - get the secid of the task
1871 * @p: the object task
1872 * @secid: where to put the result
1874 * Sets the secid to contain a u32 version of the smack label.
1876 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1878 struct smack_known *skp = smk_of_task(task_security(p));
1880 *secid = skp->smk_secid;
1884 * smack_task_setnice - Smack check on setting nice
1885 * @p: the task object
1888 * Return 0 if write access is permitted
1890 static int smack_task_setnice(struct task_struct *p, int nice)
1894 rc = cap_task_setnice(p, nice);
1896 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1901 * smack_task_setioprio - Smack check on setting ioprio
1902 * @p: the task object
1905 * Return 0 if write access is permitted
1907 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1911 rc = cap_task_setioprio(p, ioprio);
1913 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1918 * smack_task_getioprio - Smack check on reading ioprio
1919 * @p: the task object
1921 * Return 0 if read access is permitted
1923 static int smack_task_getioprio(struct task_struct *p)
1925 return smk_curacc_on_task(p, MAY_READ, __func__);
1929 * smack_task_setscheduler - Smack check on setting scheduler
1930 * @p: the task object
1934 * Return 0 if read access is permitted
1936 static int smack_task_setscheduler(struct task_struct *p)
1940 rc = cap_task_setscheduler(p);
1942 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1947 * smack_task_getscheduler - Smack check on reading scheduler
1948 * @p: the task object
1950 * Return 0 if read access is permitted
1952 static int smack_task_getscheduler(struct task_struct *p)
1954 return smk_curacc_on_task(p, MAY_READ, __func__);
1958 * smack_task_movememory - Smack check on moving memory
1959 * @p: the task object
1961 * Return 0 if write access is permitted
1963 static int smack_task_movememory(struct task_struct *p)
1965 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1969 * smack_task_kill - Smack check on signal delivery
1970 * @p: the task object
1973 * @secid: identifies the smack to use in lieu of current's
1975 * Return 0 if write access is permitted
1977 * The secid behavior is an artifact of an SELinux hack
1978 * in the USB code. Someday it may go away.
1980 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1983 struct smk_audit_info ad;
1984 struct smack_known *skp;
1985 struct smack_known *tkp = smk_of_task(task_security(p));
1988 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1989 smk_ad_setfield_u_tsk(&ad, p);
1991 * Sending a signal requires that the sender
1992 * can write the receiver.
1995 rc = smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
1996 rc = smk_bu_task(p, MAY_WRITE, rc);
2000 * If the secid isn't 0 we're dealing with some USB IO
2001 * specific behavior. This is not clean. For one thing
2002 * we can't take privilege into account.
2004 skp = smack_from_secid(secid);
2005 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
2006 rc = smk_bu_note("USB signal", skp, tkp->smk_known, MAY_WRITE, rc);
2011 * smack_task_wait - Smack access check for waiting
2012 * @p: task to wait for
2016 static int smack_task_wait(struct task_struct *p)
2019 * Allow the operation to succeed.
2021 * In userless environments (e.g. phones) programs
2022 * get marked with SMACK64EXEC and even if the parent
2023 * and child shouldn't be talking the parent still
2024 * may expect to know when the child exits.
2030 * smack_task_to_inode - copy task smack into the inode blob
2031 * @p: task to copy from
2032 * @inode: inode to copy to
2034 * Sets the smack pointer in the inode security blob
2036 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2038 struct inode_smack *isp = inode->i_security;
2039 struct smack_known *skp = smk_of_task(task_security(p));
2041 isp->smk_inode = skp->smk_known;
2049 * smack_sk_alloc_security - Allocate a socket blob
2052 * @gfp_flags: memory allocation flags
2054 * Assign Smack pointers to current
2056 * Returns 0 on success, -ENOMEM is there's no memory
2058 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2060 struct smack_known *skp = smk_of_current();
2061 struct socket_smack *ssp;
2063 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2069 ssp->smk_packet = NULL;
2071 sk->sk_security = ssp;
2077 * smack_sk_free_security - Free a socket blob
2080 * Clears the blob pointer
2082 static void smack_sk_free_security(struct sock *sk)
2084 kfree(sk->sk_security);
2088 * smack_host_label - check host based restrictions
2089 * @sip: the object end
2091 * looks for host based access restrictions
2093 * This version will only be appropriate for really small sets of single label
2094 * hosts. The caller is responsible for ensuring that the RCU read lock is
2095 * taken before calling this function.
2097 * Returns the label of the far end or NULL if it's not special.
2099 static char *smack_host_label(struct sockaddr_in *sip)
2101 struct smk_netlbladdr *snp;
2102 struct in_addr *siap = &sip->sin_addr;
2104 if (siap->s_addr == 0)
2107 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2109 * we break after finding the first match because
2110 * the list is sorted from longest to shortest mask
2111 * so we have found the most specific match
2113 if ((&snp->smk_host.sin_addr)->s_addr ==
2114 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2115 /* we have found the special CIPSO option */
2116 if (snp->smk_label == smack_cipso_option)
2118 return snp->smk_label;
2125 * smack_netlabel - Set the secattr on a socket
2127 * @labeled: socket label scheme
2129 * Convert the outbound smack value (smk_out) to a
2130 * secattr and attach it to the socket.
2132 * Returns 0 on success or an error code
2134 static int smack_netlabel(struct sock *sk, int labeled)
2136 struct smack_known *skp;
2137 struct socket_smack *ssp = sk->sk_security;
2141 * Usually the netlabel code will handle changing the
2142 * packet labeling based on the label.
2143 * The case of a single label host is different, because
2144 * a single label host should never get a labeled packet
2145 * even though the label is usually associated with a packet
2149 bh_lock_sock_nested(sk);
2151 if (ssp->smk_out == smack_net_ambient ||
2152 labeled == SMACK_UNLABELED_SOCKET)
2153 netlbl_sock_delattr(sk);
2156 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2166 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2168 * @sap: the destination address
2170 * Set the correct secattr for the given socket based on the destination
2171 * address and perform any outbound access checks needed.
2173 * Returns 0 on success or an error code.
2176 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2178 struct smack_known *skp;
2182 struct socket_smack *ssp = sk->sk_security;
2183 struct smk_audit_info ad;
2186 hostsp = smack_host_label(sap);
2187 if (hostsp != NULL) {
2189 struct lsm_network_audit net;
2191 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2192 ad.a.u.net->family = sap->sin_family;
2193 ad.a.u.net->dport = sap->sin_port;
2194 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2196 sk_lbl = SMACK_UNLABELED_SOCKET;
2198 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
2199 rc = smk_bu_note("IPv4 host check", skp, hostsp, MAY_WRITE, rc);
2201 sk_lbl = SMACK_CIPSO_SOCKET;
2208 return smack_netlabel(sk, sk_lbl);
2212 * smk_ipv6_port_label - Smack port access table management
2216 * Create or update the port list entry
2218 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2220 struct sock *sk = sock->sk;
2221 struct sockaddr_in6 *addr6;
2222 struct socket_smack *ssp = sock->sk->sk_security;
2223 struct smk_port_label *spp;
2224 unsigned short port = 0;
2226 if (address == NULL) {
2228 * This operation is changing the Smack information
2229 * on the bound socket. Take the changes to the port
2232 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2233 if (sk != spp->smk_sock)
2235 spp->smk_in = ssp->smk_in;
2236 spp->smk_out = ssp->smk_out;
2240 * A NULL address is only used for updating existing
2241 * bound entries. If there isn't one, it's OK.
2246 addr6 = (struct sockaddr_in6 *)address;
2247 port = ntohs(addr6->sin6_port);
2249 * This is a special case that is safely ignored.
2255 * Look for an existing port list entry.
2256 * This is an indication that a port is getting reused.
2258 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2259 if (spp->smk_port != port)
2261 spp->smk_port = port;
2263 spp->smk_in = ssp->smk_in;
2264 spp->smk_out = ssp->smk_out;
2269 * A new port entry is required.
2271 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2275 spp->smk_port = port;
2277 spp->smk_in = ssp->smk_in;
2278 spp->smk_out = ssp->smk_out;
2280 list_add(&spp->list, &smk_ipv6_port_list);
2285 * smk_ipv6_port_check - check Smack port access
2289 * Create or update the port list entry
2291 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2296 struct smk_port_label *spp;
2297 struct socket_smack *ssp = sk->sk_security;
2298 struct smack_known *skp;
2299 unsigned short port = 0;
2301 struct smk_audit_info ad;
2304 struct lsm_network_audit net;
2307 if (act == SMK_RECEIVING) {
2308 skp = smack_net_ambient;
2309 object = ssp->smk_in->smk_known;
2312 object = smack_net_ambient->smk_known;
2316 * Get the IP address and port from the address.
2318 port = ntohs(address->sin6_port);
2319 bep = (__be16 *)(&address->sin6_addr);
2320 be32p = (__be32 *)(&address->sin6_addr);
2323 * It's remote, so port lookup does no good.
2325 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2329 * It's local so the send check has to have passed.
2331 if (act == SMK_RECEIVING) {
2332 skp = &smack_known_web;
2336 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2337 if (spp->smk_port != port)
2339 object = spp->smk_in->smk_known;
2340 if (act == SMK_CONNECTING)
2341 ssp->smk_packet = spp->smk_out;
2348 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2349 ad.a.u.net->family = sk->sk_family;
2350 ad.a.u.net->dport = port;
2351 if (act == SMK_RECEIVING)
2352 ad.a.u.net->v6info.saddr = address->sin6_addr;
2354 ad.a.u.net->v6info.daddr = address->sin6_addr;
2356 rc = smk_access(skp, object, MAY_WRITE, &ad);
2357 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2362 * smack_inode_setsecurity - set smack xattrs
2363 * @inode: the object
2364 * @name: attribute name
2365 * @value: attribute value
2366 * @size: size of the attribute
2369 * Sets the named attribute in the appropriate blob
2371 * Returns 0 on success, or an error code
2373 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2374 const void *value, size_t size, int flags)
2376 struct smack_known *skp;
2377 struct inode_smack *nsp = inode->i_security;
2378 struct socket_smack *ssp;
2379 struct socket *sock;
2382 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2385 skp = smk_import_entry(value, size);
2389 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2390 nsp->smk_inode = skp->smk_known;
2391 nsp->smk_flags |= SMK_INODE_INSTANT;
2395 * The rest of the Smack xattrs are only on sockets.
2397 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2400 sock = SOCKET_I(inode);
2401 if (sock == NULL || sock->sk == NULL)
2404 ssp = sock->sk->sk_security;
2406 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2408 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2410 if (sock->sk->sk_family == PF_INET) {
2411 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2414 "Smack: \"%s\" netlbl error %d.\n",
2420 if (sock->sk->sk_family == PF_INET6)
2421 smk_ipv6_port_label(sock, NULL);
2427 * smack_socket_post_create - finish socket setup
2429 * @family: protocol family
2434 * Sets the netlabel information on the socket
2436 * Returns 0 on success, and error code otherwise
2438 static int smack_socket_post_create(struct socket *sock, int family,
2439 int type, int protocol, int kern)
2441 if (family != PF_INET || sock->sk == NULL)
2444 * Set the outbound netlbl.
2446 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2450 * smack_socket_bind - record port binding information.
2452 * @address: the port address
2453 * @addrlen: size of the address
2455 * Records the label bound to a port.
2459 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2462 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2463 smk_ipv6_port_label(sock, address);
2469 * smack_socket_connect - connect access check
2471 * @sap: the other end
2472 * @addrlen: size of sap
2474 * Verifies that a connection may be possible
2476 * Returns 0 on success, and error code otherwise
2478 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2483 if (sock->sk == NULL)
2486 switch (sock->sk->sk_family) {
2488 if (addrlen < sizeof(struct sockaddr_in))
2490 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2493 if (addrlen < sizeof(struct sockaddr_in6))
2495 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2503 * smack_flags_to_may - convert S_ to MAY_ values
2504 * @flags: the S_ value
2506 * Returns the equivalent MAY_ value
2508 static int smack_flags_to_may(int flags)
2512 if (flags & S_IRUGO)
2514 if (flags & S_IWUGO)
2516 if (flags & S_IXUGO)
2523 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2528 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2530 struct smack_known *skp = smk_of_current();
2532 msg->security = skp->smk_known;
2537 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2540 * Clears the blob pointer
2542 static void smack_msg_msg_free_security(struct msg_msg *msg)
2544 msg->security = NULL;
2548 * smack_of_shm - the smack pointer for the shm
2551 * Returns a pointer to the smack value
2553 static char *smack_of_shm(struct shmid_kernel *shp)
2555 return (char *)shp->shm_perm.security;
2559 * smack_shm_alloc_security - Set the security blob for shm
2564 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2566 struct kern_ipc_perm *isp = &shp->shm_perm;
2567 struct smack_known *skp = smk_of_current();
2569 isp->security = skp->smk_known;
2574 * smack_shm_free_security - Clear the security blob for shm
2577 * Clears the blob pointer
2579 static void smack_shm_free_security(struct shmid_kernel *shp)
2581 struct kern_ipc_perm *isp = &shp->shm_perm;
2583 isp->security = NULL;
2587 * smk_curacc_shm : check if current has access on shm
2589 * @access : access requested
2591 * Returns 0 if current has the requested access, error code otherwise
2593 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2595 char *ssp = smack_of_shm(shp);
2596 struct smk_audit_info ad;
2600 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2601 ad.a.u.ipc_id = shp->shm_perm.id;
2603 rc = smk_curacc(ssp, access, &ad);
2604 rc = smk_bu_current("shm", ssp, access, rc);
2609 * smack_shm_associate - Smack access check for shm
2611 * @shmflg: access requested
2613 * Returns 0 if current has the requested access, error code otherwise
2615 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2619 may = smack_flags_to_may(shmflg);
2620 return smk_curacc_shm(shp, may);
2624 * smack_shm_shmctl - Smack access check for shm
2626 * @cmd: what it wants to do
2628 * Returns 0 if current has the requested access, error code otherwise
2630 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2643 may = MAY_READWRITE;
2648 * System level information.
2654 return smk_curacc_shm(shp, may);
2658 * smack_shm_shmat - Smack access for shmat
2661 * @shmflg: access requested
2663 * Returns 0 if current has the requested access, error code otherwise
2665 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2670 may = smack_flags_to_may(shmflg);
2671 return smk_curacc_shm(shp, may);
2675 * smack_of_sem - the smack pointer for the sem
2678 * Returns a pointer to the smack value
2680 static char *smack_of_sem(struct sem_array *sma)
2682 return (char *)sma->sem_perm.security;
2686 * smack_sem_alloc_security - Set the security blob for sem
2691 static int smack_sem_alloc_security(struct sem_array *sma)
2693 struct kern_ipc_perm *isp = &sma->sem_perm;
2694 struct smack_known *skp = smk_of_current();
2696 isp->security = skp->smk_known;
2701 * smack_sem_free_security - Clear the security blob for sem
2704 * Clears the blob pointer
2706 static void smack_sem_free_security(struct sem_array *sma)
2708 struct kern_ipc_perm *isp = &sma->sem_perm;
2710 isp->security = NULL;
2714 * smk_curacc_sem : check if current has access on sem
2716 * @access : access requested
2718 * Returns 0 if current has the requested access, error code otherwise
2720 static int smk_curacc_sem(struct sem_array *sma, int access)
2722 char *ssp = smack_of_sem(sma);
2723 struct smk_audit_info ad;
2727 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2728 ad.a.u.ipc_id = sma->sem_perm.id;
2730 rc = smk_curacc(ssp, access, &ad);
2731 rc = smk_bu_current("sem", ssp, access, rc);
2736 * smack_sem_associate - Smack access check for sem
2738 * @semflg: access requested
2740 * Returns 0 if current has the requested access, error code otherwise
2742 static int smack_sem_associate(struct sem_array *sma, int semflg)
2746 may = smack_flags_to_may(semflg);
2747 return smk_curacc_sem(sma, may);
2751 * smack_sem_shmctl - Smack access check for sem
2753 * @cmd: what it wants to do
2755 * Returns 0 if current has the requested access, error code otherwise
2757 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2775 may = MAY_READWRITE;
2780 * System level information
2787 return smk_curacc_sem(sma, may);
2791 * smack_sem_semop - Smack checks of semaphore operations
2797 * Treated as read and write in all cases.
2799 * Returns 0 if access is allowed, error code otherwise
2801 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2802 unsigned nsops, int alter)
2804 return smk_curacc_sem(sma, MAY_READWRITE);
2808 * smack_msg_alloc_security - Set the security blob for msg
2813 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2815 struct kern_ipc_perm *kisp = &msq->q_perm;
2816 struct smack_known *skp = smk_of_current();
2818 kisp->security = skp->smk_known;
2823 * smack_msg_free_security - Clear the security blob for msg
2826 * Clears the blob pointer
2828 static void smack_msg_queue_free_security(struct msg_queue *msq)
2830 struct kern_ipc_perm *kisp = &msq->q_perm;
2832 kisp->security = NULL;
2836 * smack_of_msq - the smack pointer for the msq
2839 * Returns a pointer to the smack value
2841 static char *smack_of_msq(struct msg_queue *msq)
2843 return (char *)msq->q_perm.security;
2847 * smk_curacc_msq : helper to check if current has access on msq
2849 * @access : access requested
2851 * return 0 if current has access, error otherwise
2853 static int smk_curacc_msq(struct msg_queue *msq, int access)
2855 char *msp = smack_of_msq(msq);
2856 struct smk_audit_info ad;
2860 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2861 ad.a.u.ipc_id = msq->q_perm.id;
2863 rc = smk_curacc(msp, access, &ad);
2864 rc = smk_bu_current("msq", msp, access, rc);
2869 * smack_msg_queue_associate - Smack access check for msg_queue
2871 * @msqflg: access requested
2873 * Returns 0 if current has the requested access, error code otherwise
2875 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2879 may = smack_flags_to_may(msqflg);
2880 return smk_curacc_msq(msq, may);
2884 * smack_msg_queue_msgctl - Smack access check for msg_queue
2886 * @cmd: what it wants to do
2888 * Returns 0 if current has the requested access, error code otherwise
2890 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2901 may = MAY_READWRITE;
2906 * System level information
2913 return smk_curacc_msq(msq, may);
2917 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2920 * @msqflg: access requested
2922 * Returns 0 if current has the requested access, error code otherwise
2924 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2929 may = smack_flags_to_may(msqflg);
2930 return smk_curacc_msq(msq, may);
2934 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2941 * Returns 0 if current has read and write access, error code otherwise
2943 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2944 struct task_struct *target, long type, int mode)
2946 return smk_curacc_msq(msq, MAY_READWRITE);
2950 * smack_ipc_permission - Smack access for ipc_permission()
2951 * @ipp: the object permissions
2952 * @flag: access requested
2954 * Returns 0 if current has read and write access, error code otherwise
2956 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2958 char *isp = ipp->security;
2959 int may = smack_flags_to_may(flag);
2960 struct smk_audit_info ad;
2964 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2965 ad.a.u.ipc_id = ipp->id;
2967 rc = smk_curacc(isp, may, &ad);
2968 rc = smk_bu_current("svipc", isp, may, rc);
2973 * smack_ipc_getsecid - Extract smack security id
2974 * @ipp: the object permissions
2975 * @secid: where result will be saved
2977 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2979 char *smack = ipp->security;
2981 *secid = smack_to_secid(smack);
2985 * smack_d_instantiate - Make sure the blob is correct on an inode
2986 * @opt_dentry: dentry where inode will be attached
2987 * @inode: the object
2989 * Set the inode's security blob if it hasn't been done already.
2991 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2993 struct super_block *sbp;
2994 struct superblock_smack *sbsp;
2995 struct inode_smack *isp;
2996 struct smack_known *skp;
2997 struct smack_known *ckp = smk_of_current();
2999 char trattr[TRANS_TRUE_SIZE];
3007 isp = inode->i_security;
3009 mutex_lock(&isp->smk_lock);
3011 * If the inode is already instantiated
3012 * take the quick way out
3014 if (isp->smk_flags & SMK_INODE_INSTANT)
3018 sbsp = sbp->s_security;
3020 * We're going to use the superblock default label
3021 * if there's no label on the file.
3023 final = sbsp->smk_default;
3026 * If this is the root inode the superblock
3027 * may be in the process of initialization.
3028 * If that is the case use the root value out
3029 * of the superblock.
3031 if (opt_dentry->d_parent == opt_dentry) {
3032 if (sbp->s_magic == CGROUP_SUPER_MAGIC) {
3034 * The cgroup filesystem is never mounted,
3035 * so there's no opportunity to set the mount
3038 sbsp->smk_root = smack_known_star.smk_known;
3039 sbsp->smk_default = smack_known_star.smk_known;
3041 isp->smk_inode = sbsp->smk_root;
3042 isp->smk_flags |= SMK_INODE_INSTANT;
3047 * This is pretty hackish.
3048 * Casey says that we shouldn't have to do
3049 * file system specific code, but it does help
3050 * with keeping it simple.
3052 switch (sbp->s_magic) {
3056 case CGROUP_SUPER_MAGIC:
3058 * Casey says that it's a little embarrassing
3059 * that the smack file system doesn't do
3060 * extended attributes.
3062 * Casey says pipes are easy (?)
3064 * Socket access is controlled by the socket
3065 * structures associated with the task involved.
3067 * Cgroupfs is special
3069 final = smack_known_star.smk_known;
3071 case DEVPTS_SUPER_MAGIC:
3073 * devpts seems content with the label of the task.
3074 * Programs that change smack have to treat the
3077 final = ckp->smk_known;
3079 case PROC_SUPER_MAGIC:
3081 * Casey says procfs appears not to care.
3082 * The superblock default suffices.
3087 * Device labels should come from the filesystem,
3088 * but watch out, because they're volitile,
3089 * getting recreated on every reboot.
3091 final = smack_known_star.smk_known;
3095 * If a smack value has been set we want to use it,
3096 * but since tmpfs isn't giving us the opportunity
3097 * to set mount options simulate setting the
3098 * superblock default.
3102 * This isn't an understood special case.
3103 * Get the value from the xattr.
3107 * UNIX domain sockets use lower level socket data.
3109 if (S_ISSOCK(inode->i_mode)) {
3110 final = smack_known_star.smk_known;
3114 * No xattr support means, alas, no SMACK label.
3115 * Use the aforeapplied default.
3116 * It would be curious if the label of the task
3117 * does not match that assigned.
3119 if (inode->i_op->getxattr == NULL)
3122 * Get the dentry for xattr.
3124 dp = dget(opt_dentry);
3125 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3127 final = skp->smk_known;
3130 * Transmuting directory
3132 if (S_ISDIR(inode->i_mode)) {
3134 * If this is a new directory and the label was
3135 * transmuted when the inode was initialized
3136 * set the transmute attribute on the directory
3137 * and mark the inode.
3139 * If there is a transmute attribute on the
3140 * directory mark the inode.
3142 if (isp->smk_flags & SMK_INODE_CHANGED) {
3143 isp->smk_flags &= ~SMK_INODE_CHANGED;
3144 rc = inode->i_op->setxattr(dp,
3145 XATTR_NAME_SMACKTRANSMUTE,
3146 TRANS_TRUE, TRANS_TRUE_SIZE,
3149 rc = inode->i_op->getxattr(dp,
3150 XATTR_NAME_SMACKTRANSMUTE, trattr,
3152 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3153 TRANS_TRUE_SIZE) != 0)
3157 transflag = SMK_INODE_TRANSMUTE;
3160 * Don't let the exec or mmap label be "*" or "@".
3162 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3163 if (skp == &smack_known_star || skp == &smack_known_web)
3165 isp->smk_task = skp;
3166 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3167 if (skp == &smack_known_star || skp == &smack_known_web)
3169 isp->smk_mmap = skp;
3176 isp->smk_inode = ckp->smk_known;
3178 isp->smk_inode = final;
3180 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3183 mutex_unlock(&isp->smk_lock);
3188 * smack_getprocattr - Smack process attribute access
3189 * @p: the object task
3190 * @name: the name of the attribute in /proc/.../attr
3191 * @value: where to put the result
3193 * Places a copy of the task Smack into value
3195 * Returns the length of the smack label or an error code
3197 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3199 struct smack_known *skp = smk_of_task(task_security(p));
3203 if (strcmp(name, "current") != 0)
3206 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3216 * smack_setprocattr - Smack process attribute setting
3217 * @p: the object task
3218 * @name: the name of the attribute in /proc/.../attr
3219 * @value: the value to set
3220 * @size: the size of the value
3222 * Sets the Smack value of the task. Only setting self
3223 * is permitted and only with privilege
3225 * Returns the length of the smack label or an error code
3227 static int smack_setprocattr(struct task_struct *p, char *name,
3228 void *value, size_t size)
3230 struct task_smack *tsp;
3232 struct smack_known *skp;
3235 * Changing another process' Smack value is too dangerous
3236 * and supports no sane use case.
3241 if (!smack_privileged(CAP_MAC_ADMIN))
3244 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3247 if (strcmp(name, "current") != 0)
3250 skp = smk_import_entry(value, size);
3255 * No process is ever allowed the web ("@") label.
3257 if (skp == &smack_known_web)
3260 new = prepare_creds();
3264 tsp = new->security;
3265 tsp->smk_task = skp;
3272 * smack_unix_stream_connect - Smack access on UDS
3274 * @other: the other sock
3277 * Return 0 if a subject with the smack of sock could access
3278 * an object with the smack of other, otherwise an error code
3280 static int smack_unix_stream_connect(struct sock *sock,
3281 struct sock *other, struct sock *newsk)
3283 struct smack_known *skp;
3284 struct smack_known *okp;
3285 struct socket_smack *ssp = sock->sk_security;
3286 struct socket_smack *osp = other->sk_security;
3287 struct socket_smack *nsp = newsk->sk_security;
3288 struct smk_audit_info ad;
3291 struct lsm_network_audit net;
3294 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3298 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3299 smk_ad_setfield_u_net_sk(&ad, other);
3301 rc = smk_access(skp, okp->smk_known, MAY_WRITE, &ad);
3302 rc = smk_bu_note("UDS connect", skp, okp->smk_known,
3305 rc = smk_access(okp, okp->smk_known, MAY_WRITE, NULL);
3306 rc = smk_bu_note("UDS connect", okp, okp->smk_known,
3312 * Cross reference the peer labels for SO_PEERSEC.
3315 nsp->smk_packet = ssp->smk_out;
3316 ssp->smk_packet = osp->smk_out;
3323 * smack_unix_may_send - Smack access on UDS
3325 * @other: the other socket
3327 * Return 0 if a subject with the smack of sock could access
3328 * an object with the smack of other, otherwise an error code
3330 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3332 struct socket_smack *ssp = sock->sk->sk_security;
3333 struct socket_smack *osp = other->sk->sk_security;
3334 struct smack_known *skp;
3335 struct smk_audit_info ad;
3339 struct lsm_network_audit net;
3341 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3342 smk_ad_setfield_u_net_sk(&ad, other->sk);
3345 if (smack_privileged(CAP_MAC_OVERRIDE))
3349 rc = smk_access(skp, osp->smk_in->smk_known, MAY_WRITE, &ad);
3350 rc = smk_bu_note("UDS send", skp, osp->smk_in->smk_known,
3356 * smack_socket_sendmsg - Smack check based on destination host
3359 * @size: the size of the message
3361 * Return 0 if the current subject can write to the destination host.
3362 * For IPv4 this is only a question if the destination is a single label host.
3363 * For IPv6 this is a check against the label of the port.
3365 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3368 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3369 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3373 * Perfectly reasonable for this to be NULL
3378 switch (sip->sin_family) {
3380 rc = smack_netlabel_send(sock->sk, sip);
3383 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3390 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3391 * @sap: netlabel secattr
3392 * @ssp: socket security information
3394 * Returns a pointer to a Smack label entry found on the label list.
3396 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3397 struct socket_smack *ssp)
3399 struct smack_known *skp;
3404 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3406 * Looks like a CIPSO packet.
3407 * If there are flags but no level netlabel isn't
3408 * behaving the way we expect it to.
3410 * Look it up in the label table
3411 * Without guidance regarding the smack value
3412 * for the packet fall back on the network
3416 list_for_each_entry(skp, &smack_known_list, list) {
3417 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3420 * Compare the catsets. Use the netlbl APIs.
3422 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3423 if ((skp->smk_netlabel.flags &
3424 NETLBL_SECATTR_MLS_CAT) == 0)
3428 for (acat = -1, kcat = -1; acat == kcat; ) {
3429 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3431 kcat = netlbl_catmap_walk(
3432 skp->smk_netlabel.attr.mls.cat,
3434 if (acat < 0 || kcat < 0)
3447 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3448 return &smack_known_web;
3449 return &smack_known_star;
3451 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3453 * Looks like a fallback, which gives us a secid.
3455 skp = smack_from_secid(sap->attr.secid);
3457 * This has got to be a bug because it is
3458 * impossible to specify a fallback without
3459 * specifying the label, which will ensure
3460 * it has a secid, and the only way to get a
3461 * secid is from a fallback.
3463 BUG_ON(skp == NULL);
3467 * Without guidance regarding the smack value
3468 * for the packet fall back on the network
3471 return smack_net_ambient;
3474 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3478 int proto = -EINVAL;
3479 struct ipv6hdr _ipv6h;
3480 struct ipv6hdr *ip6;
3482 struct tcphdr _tcph, *th;
3483 struct udphdr _udph, *uh;
3484 struct dccp_hdr _dccph, *dh;
3488 offset = skb_network_offset(skb);
3489 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3492 sip->sin6_addr = ip6->saddr;
3494 nexthdr = ip6->nexthdr;
3495 offset += sizeof(_ipv6h);
3496 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3503 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3505 sip->sin6_port = th->source;
3508 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3510 sip->sin6_port = uh->source;
3513 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3515 sip->sin6_port = dh->dccph_sport;
3522 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3526 * Returns 0 if the packet should be delivered, an error code otherwise
3528 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3530 struct netlbl_lsm_secattr secattr;
3531 struct socket_smack *ssp = sk->sk_security;
3532 struct smack_known *skp;
3533 struct sockaddr_in6 sadd;
3535 struct smk_audit_info ad;
3537 struct lsm_network_audit net;
3539 switch (sk->sk_family) {
3542 * Translate what netlabel gave us.
3544 netlbl_secattr_init(&secattr);
3546 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3548 skp = smack_from_secattr(&secattr, ssp);
3550 skp = smack_net_ambient;
3552 netlbl_secattr_destroy(&secattr);
3555 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3556 ad.a.u.net->family = sk->sk_family;
3557 ad.a.u.net->netif = skb->skb_iif;
3558 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3561 * Receiving a packet requires that the other end
3562 * be able to write here. Read access is not required.
3563 * This is the simplist possible security model
3566 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3567 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in->smk_known,
3570 netlbl_skbuff_err(skb, rc, 0);
3573 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3574 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3575 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3584 * smack_socket_getpeersec_stream - pull in packet label
3586 * @optval: user's destination
3587 * @optlen: size thereof
3590 * returns zero on success, an error code otherwise
3592 static int smack_socket_getpeersec_stream(struct socket *sock,
3593 char __user *optval,
3594 int __user *optlen, unsigned len)
3596 struct socket_smack *ssp;
3601 ssp = sock->sk->sk_security;
3602 if (ssp->smk_packet != NULL) {
3603 rcp = ssp->smk_packet->smk_known;
3604 slen = strlen(rcp) + 1;
3609 else if (copy_to_user(optval, rcp, slen) != 0)
3612 if (put_user(slen, optlen) != 0)
3620 * smack_socket_getpeersec_dgram - pull in packet label
3621 * @sock: the peer socket
3623 * @secid: pointer to where to put the secid of the packet
3625 * Sets the netlabel socket state on sk from parent
3627 static int smack_socket_getpeersec_dgram(struct socket *sock,
3628 struct sk_buff *skb, u32 *secid)
3631 struct netlbl_lsm_secattr secattr;
3632 struct socket_smack *ssp = NULL;
3633 struct smack_known *skp;
3634 int family = PF_UNSPEC;
3635 u32 s = 0; /* 0 is the invalid secid */
3639 if (skb->protocol == htons(ETH_P_IP))
3641 else if (skb->protocol == htons(ETH_P_IPV6))
3644 if (family == PF_UNSPEC && sock != NULL)
3645 family = sock->sk->sk_family;
3647 if (family == PF_UNIX) {
3648 ssp = sock->sk->sk_security;
3649 s = ssp->smk_out->smk_secid;
3650 } else if (family == PF_INET || family == PF_INET6) {
3652 * Translate what netlabel gave us.
3654 if (sock != NULL && sock->sk != NULL)
3655 ssp = sock->sk->sk_security;
3656 netlbl_secattr_init(&secattr);
3657 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3659 skp = smack_from_secattr(&secattr, ssp);
3662 netlbl_secattr_destroy(&secattr);
3671 * smack_sock_graft - Initialize a newly created socket with an existing sock
3673 * @parent: parent socket
3675 * Set the smk_{in,out} state of an existing sock based on the process that
3676 * is creating the new socket.
3678 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3680 struct socket_smack *ssp;
3681 struct smack_known *skp = smk_of_current();
3684 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3687 ssp = sk->sk_security;
3690 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3694 * smack_inet_conn_request - Smack access check on connect
3695 * @sk: socket involved
3699 * Returns 0 if a task with the packet label could write to
3700 * the socket, otherwise an error code
3702 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3703 struct request_sock *req)
3705 u16 family = sk->sk_family;
3706 struct smack_known *skp;
3707 struct socket_smack *ssp = sk->sk_security;
3708 struct netlbl_lsm_secattr secattr;
3709 struct sockaddr_in addr;
3713 struct smk_audit_info ad;
3715 struct lsm_network_audit net;
3718 if (family == PF_INET6) {
3720 * Handle mapped IPv4 packets arriving
3721 * via IPv6 sockets. Don't set up netlabel
3722 * processing on IPv6.
3724 if (skb->protocol == htons(ETH_P_IP))
3730 netlbl_secattr_init(&secattr);
3731 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3733 skp = smack_from_secattr(&secattr, ssp);
3735 skp = &smack_known_huh;
3736 netlbl_secattr_destroy(&secattr);
3739 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3740 ad.a.u.net->family = family;
3741 ad.a.u.net->netif = skb->skb_iif;
3742 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3745 * Receiving a packet requires that the other end be able to write
3746 * here. Read access is not required.
3748 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3749 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in->smk_known,
3755 * Save the peer's label in the request_sock so we can later setup
3756 * smk_packet in the child socket so that SO_PEERCRED can report it.
3758 req->peer_secid = skp->smk_secid;
3761 * We need to decide if we want to label the incoming connection here
3762 * if we do we only need to label the request_sock and the stack will
3763 * propagate the wire-label to the sock when it is created.
3766 addr.sin_addr.s_addr = hdr->saddr;
3768 hsp = smack_host_label(&addr);
3772 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3774 netlbl_req_delattr(req);
3780 * smack_inet_csk_clone - Copy the connection information to the new socket
3781 * @sk: the new socket
3782 * @req: the connection's request_sock
3784 * Transfer the connection's peer label to the newly created socket.
3786 static void smack_inet_csk_clone(struct sock *sk,
3787 const struct request_sock *req)
3789 struct socket_smack *ssp = sk->sk_security;
3790 struct smack_known *skp;
3792 if (req->peer_secid != 0) {
3793 skp = smack_from_secid(req->peer_secid);
3794 ssp->smk_packet = skp;
3796 ssp->smk_packet = NULL;
3800 * Key management security hooks
3802 * Casey has not tested key support very heavily.
3803 * The permission check is most likely too restrictive.
3804 * If you care about keys please have a look.
3809 * smack_key_alloc - Set the key security blob
3811 * @cred: the credentials to use
3814 * No allocation required
3818 static int smack_key_alloc(struct key *key, const struct cred *cred,
3819 unsigned long flags)
3821 struct smack_known *skp = smk_of_task(cred->security);
3823 key->security = skp->smk_known;
3828 * smack_key_free - Clear the key security blob
3831 * Clear the blob pointer
3833 static void smack_key_free(struct key *key)
3835 key->security = NULL;
3839 * smack_key_permission - Smack access on a key
3840 * @key_ref: gets to the object
3841 * @cred: the credentials to use
3844 * Return 0 if the task has read and write to the object,
3845 * an error code otherwise
3847 static int smack_key_permission(key_ref_t key_ref,
3848 const struct cred *cred, unsigned perm)
3851 struct smk_audit_info ad;
3852 struct smack_known *tkp = smk_of_task(cred->security);
3856 keyp = key_ref_to_ptr(key_ref);
3860 * If the key hasn't been initialized give it access so that
3863 if (keyp->security == NULL)
3866 * This should not occur
3871 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3872 ad.a.u.key_struct.key = keyp->serial;
3873 ad.a.u.key_struct.key_desc = keyp->description;
3875 if (perm & KEY_NEED_READ)
3877 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
3878 request = MAY_WRITE;
3879 rc = smk_access(tkp, keyp->security, request, &ad);
3880 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
3883 #endif /* CONFIG_KEYS */
3888 * Audit requires a unique representation of each Smack specific
3889 * rule. This unique representation is used to distinguish the
3890 * object to be audited from remaining kernel objects and also
3891 * works as a glue between the audit hooks.
3893 * Since repository entries are added but never deleted, we'll use
3894 * the smack_known label address related to the given audit rule as
3895 * the needed unique representation. This also better fits the smack
3896 * model where nearly everything is a label.
3901 * smack_audit_rule_init - Initialize a smack audit rule
3902 * @field: audit rule fields given from user-space (audit.h)
3903 * @op: required testing operator (=, !=, >, <, ...)
3904 * @rulestr: smack label to be audited
3905 * @vrule: pointer to save our own audit rule representation
3907 * Prepare to audit cases where (@field @op @rulestr) is true.
3908 * The label to be audited is created if necessay.
3910 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3912 char **rule = (char **)vrule;
3915 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3918 if (op != Audit_equal && op != Audit_not_equal)
3921 *rule = smk_import(rulestr, 0);
3927 * smack_audit_rule_known - Distinguish Smack audit rules
3928 * @krule: rule of interest, in Audit kernel representation format
3930 * This is used to filter Smack rules from remaining Audit ones.
3931 * If it's proved that this rule belongs to us, the
3932 * audit_rule_match hook will be called to do the final judgement.
3934 static int smack_audit_rule_known(struct audit_krule *krule)
3936 struct audit_field *f;
3939 for (i = 0; i < krule->field_count; i++) {
3940 f = &krule->fields[i];
3942 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3950 * smack_audit_rule_match - Audit given object ?
3951 * @secid: security id for identifying the object to test
3952 * @field: audit rule flags given from user-space
3953 * @op: required testing operator
3954 * @vrule: smack internal rule presentation
3955 * @actx: audit context associated with the check
3957 * The core Audit hook. It's used to take the decision of
3958 * whether to audit or not to audit a given object.
3960 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3961 struct audit_context *actx)
3963 struct smack_known *skp;
3966 if (unlikely(!rule)) {
3967 WARN_ONCE(1, "Smack: missing rule\n");
3971 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3974 skp = smack_from_secid(secid);
3977 * No need to do string comparisons. If a match occurs,
3978 * both pointers will point to the same smack_known
3981 if (op == Audit_equal)
3982 return (rule == skp->smk_known);
3983 if (op == Audit_not_equal)
3984 return (rule != skp->smk_known);
3990 * smack_audit_rule_free - free smack rule representation
3991 * @vrule: rule to be freed.
3993 * No memory was allocated.
3995 static void smack_audit_rule_free(void *vrule)
4000 #endif /* CONFIG_AUDIT */
4003 * smack_ismaclabel - check if xattr @name references a smack MAC label
4004 * @name: Full xattr name to check.
4006 static int smack_ismaclabel(const char *name)
4008 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4013 * smack_secid_to_secctx - return the smack label for a secid
4014 * @secid: incoming integer
4015 * @secdata: destination
4016 * @seclen: how long it is
4018 * Exists for networking code.
4020 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4022 struct smack_known *skp = smack_from_secid(secid);
4025 *secdata = skp->smk_known;
4026 *seclen = strlen(skp->smk_known);
4031 * smack_secctx_to_secid - return the secid for a smack label
4032 * @secdata: smack label
4033 * @seclen: how long result is
4034 * @secid: outgoing integer
4036 * Exists for audit and networking code.
4038 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4040 *secid = smack_to_secid(secdata);
4045 * smack_release_secctx - don't do anything.
4049 * Exists to make sure nothing gets done, and properly
4051 static void smack_release_secctx(char *secdata, u32 seclen)
4055 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4057 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4060 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4062 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4065 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4068 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4076 struct security_operations smack_ops = {
4079 .ptrace_access_check = smack_ptrace_access_check,
4080 .ptrace_traceme = smack_ptrace_traceme,
4081 .syslog = smack_syslog,
4083 .sb_alloc_security = smack_sb_alloc_security,
4084 .sb_free_security = smack_sb_free_security,
4085 .sb_copy_data = smack_sb_copy_data,
4086 .sb_kern_mount = smack_sb_kern_mount,
4087 .sb_statfs = smack_sb_statfs,
4089 .bprm_set_creds = smack_bprm_set_creds,
4090 .bprm_committing_creds = smack_bprm_committing_creds,
4091 .bprm_secureexec = smack_bprm_secureexec,
4093 .inode_alloc_security = smack_inode_alloc_security,
4094 .inode_free_security = smack_inode_free_security,
4095 .inode_init_security = smack_inode_init_security,
4096 .inode_link = smack_inode_link,
4097 .inode_unlink = smack_inode_unlink,
4098 .inode_rmdir = smack_inode_rmdir,
4099 .inode_rename = smack_inode_rename,
4100 .inode_permission = smack_inode_permission,
4101 .inode_setattr = smack_inode_setattr,
4102 .inode_getattr = smack_inode_getattr,
4103 .inode_setxattr = smack_inode_setxattr,
4104 .inode_post_setxattr = smack_inode_post_setxattr,
4105 .inode_getxattr = smack_inode_getxattr,
4106 .inode_removexattr = smack_inode_removexattr,
4107 .inode_getsecurity = smack_inode_getsecurity,
4108 .inode_setsecurity = smack_inode_setsecurity,
4109 .inode_listsecurity = smack_inode_listsecurity,
4110 .inode_getsecid = smack_inode_getsecid,
4112 .file_permission = smack_file_permission,
4113 .file_alloc_security = smack_file_alloc_security,
4114 .file_free_security = smack_file_free_security,
4115 .file_ioctl = smack_file_ioctl,
4116 .file_lock = smack_file_lock,
4117 .file_fcntl = smack_file_fcntl,
4118 .mmap_file = smack_mmap_file,
4119 .mmap_addr = cap_mmap_addr,
4120 .file_set_fowner = smack_file_set_fowner,
4121 .file_send_sigiotask = smack_file_send_sigiotask,
4122 .file_receive = smack_file_receive,
4124 .file_open = smack_file_open,
4126 .cred_alloc_blank = smack_cred_alloc_blank,
4127 .cred_free = smack_cred_free,
4128 .cred_prepare = smack_cred_prepare,
4129 .cred_transfer = smack_cred_transfer,
4130 .kernel_act_as = smack_kernel_act_as,
4131 .kernel_create_files_as = smack_kernel_create_files_as,
4132 .task_setpgid = smack_task_setpgid,
4133 .task_getpgid = smack_task_getpgid,
4134 .task_getsid = smack_task_getsid,
4135 .task_getsecid = smack_task_getsecid,
4136 .task_setnice = smack_task_setnice,
4137 .task_setioprio = smack_task_setioprio,
4138 .task_getioprio = smack_task_getioprio,
4139 .task_setscheduler = smack_task_setscheduler,
4140 .task_getscheduler = smack_task_getscheduler,
4141 .task_movememory = smack_task_movememory,
4142 .task_kill = smack_task_kill,
4143 .task_wait = smack_task_wait,
4144 .task_to_inode = smack_task_to_inode,
4146 .ipc_permission = smack_ipc_permission,
4147 .ipc_getsecid = smack_ipc_getsecid,
4149 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4150 .msg_msg_free_security = smack_msg_msg_free_security,
4152 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4153 .msg_queue_free_security = smack_msg_queue_free_security,
4154 .msg_queue_associate = smack_msg_queue_associate,
4155 .msg_queue_msgctl = smack_msg_queue_msgctl,
4156 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4157 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4159 .shm_alloc_security = smack_shm_alloc_security,
4160 .shm_free_security = smack_shm_free_security,
4161 .shm_associate = smack_shm_associate,
4162 .shm_shmctl = smack_shm_shmctl,
4163 .shm_shmat = smack_shm_shmat,
4165 .sem_alloc_security = smack_sem_alloc_security,
4166 .sem_free_security = smack_sem_free_security,
4167 .sem_associate = smack_sem_associate,
4168 .sem_semctl = smack_sem_semctl,
4169 .sem_semop = smack_sem_semop,
4171 .d_instantiate = smack_d_instantiate,
4173 .getprocattr = smack_getprocattr,
4174 .setprocattr = smack_setprocattr,
4176 .unix_stream_connect = smack_unix_stream_connect,
4177 .unix_may_send = smack_unix_may_send,
4179 .socket_post_create = smack_socket_post_create,
4180 .socket_bind = smack_socket_bind,
4181 .socket_connect = smack_socket_connect,
4182 .socket_sendmsg = smack_socket_sendmsg,
4183 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4184 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4185 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4186 .sk_alloc_security = smack_sk_alloc_security,
4187 .sk_free_security = smack_sk_free_security,
4188 .sock_graft = smack_sock_graft,
4189 .inet_conn_request = smack_inet_conn_request,
4190 .inet_csk_clone = smack_inet_csk_clone,
4192 /* key management security hooks */
4194 .key_alloc = smack_key_alloc,
4195 .key_free = smack_key_free,
4196 .key_permission = smack_key_permission,
4197 #endif /* CONFIG_KEYS */
4201 .audit_rule_init = smack_audit_rule_init,
4202 .audit_rule_known = smack_audit_rule_known,
4203 .audit_rule_match = smack_audit_rule_match,
4204 .audit_rule_free = smack_audit_rule_free,
4205 #endif /* CONFIG_AUDIT */
4207 .ismaclabel = smack_ismaclabel,
4208 .secid_to_secctx = smack_secid_to_secctx,
4209 .secctx_to_secid = smack_secctx_to_secid,
4210 .release_secctx = smack_release_secctx,
4211 .inode_notifysecctx = smack_inode_notifysecctx,
4212 .inode_setsecctx = smack_inode_setsecctx,
4213 .inode_getsecctx = smack_inode_getsecctx,
4217 static __init void init_smack_known_list(void)
4220 * Initialize rule list locks
4222 mutex_init(&smack_known_huh.smk_rules_lock);
4223 mutex_init(&smack_known_hat.smk_rules_lock);
4224 mutex_init(&smack_known_floor.smk_rules_lock);
4225 mutex_init(&smack_known_star.smk_rules_lock);
4226 mutex_init(&smack_known_invalid.smk_rules_lock);
4227 mutex_init(&smack_known_web.smk_rules_lock);
4229 * Initialize rule lists
4231 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4232 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4233 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4234 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4235 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4236 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4238 * Create the known labels list
4240 smk_insert_entry(&smack_known_huh);
4241 smk_insert_entry(&smack_known_hat);
4242 smk_insert_entry(&smack_known_star);
4243 smk_insert_entry(&smack_known_floor);
4244 smk_insert_entry(&smack_known_invalid);
4245 smk_insert_entry(&smack_known_web);
4249 * smack_init - initialize the smack system
4253 static __init int smack_init(void)
4256 struct task_smack *tsp;
4258 if (!security_module_enable(&smack_ops))
4261 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4266 printk(KERN_INFO "Smack: Initializing.\n");
4269 * Set the security state for the initial task.
4271 cred = (struct cred *) current->cred;
4272 cred->security = tsp;
4274 /* initialize the smack_known_list */
4275 init_smack_known_list();
4280 if (register_security(&smack_ops))
4281 panic("smack: Unable to register with kernel.\n");
4287 * Smack requires early initialization in order to label
4288 * all processes and objects when they are created.
4290 security_initcall(smack_init);