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,
82 struct smack_known *oskp, int mode, int rc)
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, oskp->smk_known, acc, note);
95 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
98 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
99 static int smk_bu_current(char *note, struct smack_known *oskp,
102 struct task_smack *tsp = current_security();
103 char acc[SMK_NUM_ACCESS_TYPE + 1];
108 smk_bu_mode(mode, acc);
109 pr_info("Smack Bringup: (%s %s %s) %s %s\n",
110 tsp->smk_task->smk_known, oskp->smk_known,
111 acc, current->comm, note);
115 #define smk_bu_current(note, oskp, mode, RC) (RC)
118 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
119 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
121 struct task_smack *tsp = current_security();
122 struct task_smack *otsp = task_security(otp);
123 char acc[SMK_NUM_ACCESS_TYPE + 1];
128 smk_bu_mode(mode, acc);
129 pr_info("Smack Bringup: (%s %s %s) %s to %s\n",
130 tsp->smk_task->smk_known, otsp->smk_task->smk_known, acc,
131 current->comm, otp->comm);
135 #define smk_bu_task(otp, mode, RC) (RC)
138 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
139 static int smk_bu_inode(struct inode *inode, int mode, int rc)
141 struct task_smack *tsp = current_security();
142 char acc[SMK_NUM_ACCESS_TYPE + 1];
147 smk_bu_mode(mode, acc);
148 pr_info("Smack Bringup: (%s %s %s) inode=(%s %ld) %s\n",
149 tsp->smk_task->smk_known, smk_of_inode(inode)->smk_known, acc,
150 inode->i_sb->s_id, inode->i_ino, current->comm);
154 #define smk_bu_inode(inode, mode, RC) (RC)
157 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
158 static int smk_bu_file(struct file *file, int mode, int rc)
160 struct task_smack *tsp = current_security();
161 struct smack_known *sskp = tsp->smk_task;
162 struct inode *inode = file->f_inode;
163 char acc[SMK_NUM_ACCESS_TYPE + 1];
168 smk_bu_mode(mode, acc);
169 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %s) %s\n",
170 sskp->smk_known, (char *)file->f_security, acc,
171 inode->i_sb->s_id, inode->i_ino, file->f_dentry->d_name.name,
176 #define smk_bu_file(file, mode, RC) (RC)
179 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
180 static int smk_bu_credfile(const struct cred *cred, struct file *file,
183 struct task_smack *tsp = cred->security;
184 struct smack_known *sskp = tsp->smk_task;
185 struct inode *inode = file->f_inode;
186 char acc[SMK_NUM_ACCESS_TYPE + 1];
191 smk_bu_mode(mode, acc);
192 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %s) %s\n",
193 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
194 inode->i_sb->s_id, inode->i_ino, file->f_dentry->d_name.name,
199 #define smk_bu_credfile(cred, file, mode, RC) (RC)
203 * smk_fetch - Fetch the smack label from a file.
204 * @ip: a pointer to the inode
205 * @dp: a pointer to the dentry
207 * Returns a pointer to the master list entry for the Smack label
208 * or NULL if there was no label to fetch.
210 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
215 struct smack_known *skp = NULL;
217 if (ip->i_op->getxattr == NULL)
220 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
224 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
226 skp = smk_import_entry(buffer, rc);
234 * new_inode_smack - allocate an inode security blob
235 * @skp: a pointer to the Smack label entry to use in the blob
237 * Returns the new blob or NULL if there's no memory available
239 struct inode_smack *new_inode_smack(struct smack_known *skp)
241 struct inode_smack *isp;
243 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
247 isp->smk_inode = skp;
249 mutex_init(&isp->smk_lock);
255 * new_task_smack - allocate a task security blob
256 * @smack: a pointer to the Smack label to use in the blob
258 * Returns the new blob or NULL if there's no memory available
260 static struct task_smack *new_task_smack(struct smack_known *task,
261 struct smack_known *forked, gfp_t gfp)
263 struct task_smack *tsp;
265 tsp = kzalloc(sizeof(struct task_smack), gfp);
269 tsp->smk_task = task;
270 tsp->smk_forked = forked;
271 INIT_LIST_HEAD(&tsp->smk_rules);
272 mutex_init(&tsp->smk_rules_lock);
278 * smk_copy_rules - copy a rule set
279 * @nhead - new rules header pointer
280 * @ohead - old rules header pointer
282 * Returns 0 on success, -ENOMEM on error
284 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
287 struct smack_rule *nrp;
288 struct smack_rule *orp;
291 INIT_LIST_HEAD(nhead);
293 list_for_each_entry_rcu(orp, ohead, list) {
294 nrp = kzalloc(sizeof(struct smack_rule), gfp);
300 list_add_rcu(&nrp->list, nhead);
306 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
307 * @mode - input mode in form of PTRACE_MODE_*
309 * Returns a converted MAY_* mode usable by smack rules
311 static inline unsigned int smk_ptrace_mode(unsigned int mode)
314 case PTRACE_MODE_READ:
316 case PTRACE_MODE_ATTACH:
317 return MAY_READWRITE;
324 * smk_ptrace_rule_check - helper for ptrace access
325 * @tracer: tracer process
326 * @tracee_known: label entry of the process that's about to be traced
327 * @mode: ptrace attachment mode (PTRACE_MODE_*)
328 * @func: name of the function that called us, used for audit
330 * Returns 0 on access granted, -error on error
332 static int smk_ptrace_rule_check(struct task_struct *tracer,
333 struct smack_known *tracee_known,
334 unsigned int mode, const char *func)
337 struct smk_audit_info ad, *saip = NULL;
338 struct task_smack *tsp;
339 struct smack_known *tracer_known;
341 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
342 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
343 smk_ad_setfield_u_tsk(&ad, tracer);
347 tsp = task_security(tracer);
348 tracer_known = smk_of_task(tsp);
350 if ((mode & PTRACE_MODE_ATTACH) &&
351 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
352 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
353 if (tracer_known->smk_known == tracee_known->smk_known)
355 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
357 else if (capable(CAP_SYS_PTRACE))
363 smack_log(tracer_known->smk_known,
364 tracee_known->smk_known,
370 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
371 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
377 * We he, that is fun!
381 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
382 * @ctp: child task pointer
383 * @mode: ptrace attachment mode (PTRACE_MODE_*)
385 * Returns 0 if access is OK, an error code otherwise
387 * Do the capability checks.
389 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
392 struct smack_known *skp;
394 rc = cap_ptrace_access_check(ctp, mode);
398 skp = smk_of_task(task_security(ctp));
400 rc = smk_ptrace_rule_check(current, skp, mode, __func__);
405 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
406 * @ptp: parent task pointer
408 * Returns 0 if access is OK, an error code otherwise
410 * Do the capability checks, and require PTRACE_MODE_ATTACH.
412 static int smack_ptrace_traceme(struct task_struct *ptp)
415 struct smack_known *skp;
417 rc = cap_ptrace_traceme(ptp);
421 skp = smk_of_task(current_security());
423 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
428 * smack_syslog - Smack approval on syslog
429 * @type: message type
431 * Returns 0 on success, error code otherwise.
433 static int smack_syslog(int typefrom_file)
436 struct smack_known *skp = smk_of_current();
438 if (smack_privileged(CAP_MAC_OVERRIDE))
441 if (smack_syslog_label != NULL && smack_syslog_label != skp)
453 * smack_sb_alloc_security - allocate a superblock blob
454 * @sb: the superblock getting the blob
456 * Returns 0 on success or -ENOMEM on error.
458 static int smack_sb_alloc_security(struct super_block *sb)
460 struct superblock_smack *sbsp;
462 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
467 sbsp->smk_root = &smack_known_floor;
468 sbsp->smk_default = &smack_known_floor;
469 sbsp->smk_floor = &smack_known_floor;
470 sbsp->smk_hat = &smack_known_hat;
472 * smk_initialized will be zero from kzalloc.
474 sb->s_security = sbsp;
480 * smack_sb_free_security - free a superblock blob
481 * @sb: the superblock getting the blob
484 static void smack_sb_free_security(struct super_block *sb)
486 kfree(sb->s_security);
487 sb->s_security = NULL;
491 * smack_sb_copy_data - copy mount options data for processing
492 * @orig: where to start
493 * @smackopts: mount options string
495 * Returns 0 on success or -ENOMEM on error.
497 * Copy the Smack specific mount options out of the mount
500 static int smack_sb_copy_data(char *orig, char *smackopts)
502 char *cp, *commap, *otheropts, *dp;
504 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
505 if (otheropts == NULL)
508 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
509 if (strstr(cp, SMK_FSDEFAULT) == cp)
511 else if (strstr(cp, SMK_FSFLOOR) == cp)
513 else if (strstr(cp, SMK_FSHAT) == cp)
515 else if (strstr(cp, SMK_FSROOT) == cp)
517 else if (strstr(cp, SMK_FSTRANS) == cp)
522 commap = strchr(cp, ',');
531 strcpy(orig, otheropts);
532 free_page((unsigned long)otheropts);
538 * smack_sb_kern_mount - Smack specific mount processing
539 * @sb: the file system superblock
540 * @flags: the mount flags
541 * @data: the smack mount options
543 * Returns 0 on success, an error code on failure
545 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
547 struct dentry *root = sb->s_root;
548 struct inode *inode = root->d_inode;
549 struct superblock_smack *sp = sb->s_security;
550 struct inode_smack *isp;
551 struct smack_known *skp;
557 if (sp->smk_initialized)
560 sp->smk_initialized = 1;
562 for (op = data; op != NULL; op = commap) {
563 commap = strchr(op, ',');
567 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
568 op += strlen(SMK_FSHAT);
569 skp = smk_import_entry(op, 0);
574 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
575 op += strlen(SMK_FSFLOOR);
576 skp = smk_import_entry(op, 0);
581 } else if (strncmp(op, SMK_FSDEFAULT,
582 strlen(SMK_FSDEFAULT)) == 0) {
583 op += strlen(SMK_FSDEFAULT);
584 skp = smk_import_entry(op, 0);
586 sp->smk_default = skp;
589 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
590 op += strlen(SMK_FSROOT);
591 skp = smk_import_entry(op, 0);
596 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
597 op += strlen(SMK_FSTRANS);
598 skp = smk_import_entry(op, 0);
607 if (!smack_privileged(CAP_MAC_ADMIN)) {
609 * Unprivileged mounts don't get to specify Smack values.
614 * Unprivileged mounts get root and default from the caller.
616 skp = smk_of_current();
618 sp->smk_default = skp;
621 * Initialize the root inode.
623 isp = inode->i_security;
625 isp = new_inode_smack(sp->smk_root);
628 inode->i_security = isp;
630 isp->smk_inode = sp->smk_root;
633 isp->smk_flags |= SMK_INODE_TRANSMUTE;
639 * smack_sb_statfs - Smack check on statfs
640 * @dentry: identifies the file system in question
642 * Returns 0 if current can read the floor of the filesystem,
643 * and error code otherwise
645 static int smack_sb_statfs(struct dentry *dentry)
647 struct superblock_smack *sbp = dentry->d_sb->s_security;
649 struct smk_audit_info ad;
651 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
652 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
654 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
655 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
664 * smack_bprm_set_creds - set creds for exec
665 * @bprm: the exec information
667 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
669 static int smack_bprm_set_creds(struct linux_binprm *bprm)
671 struct inode *inode = file_inode(bprm->file);
672 struct task_smack *bsp = bprm->cred->security;
673 struct inode_smack *isp;
676 rc = cap_bprm_set_creds(bprm);
680 if (bprm->cred_prepared)
683 isp = inode->i_security;
684 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
687 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
688 struct task_struct *tracer;
692 tracer = ptrace_parent(current);
693 if (likely(tracer != NULL))
694 rc = smk_ptrace_rule_check(tracer,
702 } else if (bprm->unsafe)
705 bsp->smk_task = isp->smk_task;
706 bprm->per_clear |= PER_CLEAR_ON_SETID;
712 * smack_bprm_committing_creds - Prepare to install the new credentials
715 * @bprm: binprm for exec
717 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
719 struct task_smack *bsp = bprm->cred->security;
721 if (bsp->smk_task != bsp->smk_forked)
722 current->pdeath_signal = 0;
726 * smack_bprm_secureexec - Return the decision to use secureexec.
727 * @bprm: binprm for exec
729 * Returns 0 on success.
731 static int smack_bprm_secureexec(struct linux_binprm *bprm)
733 struct task_smack *tsp = current_security();
734 int ret = cap_bprm_secureexec(bprm);
736 if (!ret && (tsp->smk_task != tsp->smk_forked))
747 * smack_inode_alloc_security - allocate an inode blob
748 * @inode: the inode in need of a blob
750 * Returns 0 if it gets a blob, -ENOMEM otherwise
752 static int smack_inode_alloc_security(struct inode *inode)
754 struct smack_known *skp = smk_of_current();
756 inode->i_security = new_inode_smack(skp);
757 if (inode->i_security == NULL)
763 * smack_inode_free_security - free an inode blob
764 * @inode: the inode with a blob
766 * Clears the blob pointer in inode
768 static void smack_inode_free_security(struct inode *inode)
770 kfree(inode->i_security);
771 inode->i_security = NULL;
775 * smack_inode_init_security - copy out the smack from an inode
776 * @inode: the newly created inode
777 * @dir: containing directory object
779 * @name: where to put the attribute name
780 * @value: where to put the attribute value
781 * @len: where to put the length of the attribute
783 * Returns 0 if it all works out, -ENOMEM if there's no memory
785 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
786 const struct qstr *qstr, const char **name,
787 void **value, size_t *len)
789 struct inode_smack *issp = inode->i_security;
790 struct smack_known *skp = smk_of_current();
791 struct smack_known *isp = smk_of_inode(inode);
792 struct smack_known *dsp = smk_of_inode(dir);
796 *name = XATTR_SMACK_SUFFIX;
800 may = smk_access_entry(skp->smk_known, dsp->smk_known,
805 * If the access rule allows transmutation and
806 * the directory requests transmutation then
807 * by all means transmute.
808 * Mark the inode as changed.
810 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
811 smk_inode_transmutable(dir)) {
813 issp->smk_flags |= SMK_INODE_CHANGED;
816 *value = kstrdup(isp->smk_known, GFP_NOFS);
822 *len = strlen(isp->smk_known);
828 * smack_inode_link - Smack check on link
829 * @old_dentry: the existing object
831 * @new_dentry: the new object
833 * Returns 0 if access is permitted, an error code otherwise
835 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
836 struct dentry *new_dentry)
838 struct smack_known *isp;
839 struct smk_audit_info ad;
842 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
843 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
845 isp = smk_of_inode(old_dentry->d_inode);
846 rc = smk_curacc(isp, MAY_WRITE, &ad);
847 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
849 if (rc == 0 && new_dentry->d_inode != NULL) {
850 isp = smk_of_inode(new_dentry->d_inode);
851 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
852 rc = smk_curacc(isp, MAY_WRITE, &ad);
853 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
860 * smack_inode_unlink - Smack check on inode deletion
861 * @dir: containing directory object
862 * @dentry: file to unlink
864 * Returns 0 if current can write the containing directory
865 * and the object, error code otherwise
867 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
869 struct inode *ip = dentry->d_inode;
870 struct smk_audit_info ad;
873 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
874 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
877 * You need write access to the thing you're unlinking
879 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
880 rc = smk_bu_inode(ip, MAY_WRITE, rc);
883 * You also need write access to the containing directory
885 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
886 smk_ad_setfield_u_fs_inode(&ad, dir);
887 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
888 rc = smk_bu_inode(dir, MAY_WRITE, rc);
894 * smack_inode_rmdir - Smack check on directory deletion
895 * @dir: containing directory object
896 * @dentry: directory to unlink
898 * Returns 0 if current can write the containing directory
899 * and the directory, error code otherwise
901 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
903 struct smk_audit_info ad;
906 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
907 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
910 * You need write access to the thing you're removing
912 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
913 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
916 * You also need write access to the containing directory
918 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
919 smk_ad_setfield_u_fs_inode(&ad, dir);
920 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
921 rc = smk_bu_inode(dir, MAY_WRITE, rc);
928 * smack_inode_rename - Smack check on rename
930 * @old_dentry: the old object
932 * @new_dentry: the new object
934 * Read and write access is required on both the old and
937 * Returns 0 if access is permitted, an error code otherwise
939 static int smack_inode_rename(struct inode *old_inode,
940 struct dentry *old_dentry,
941 struct inode *new_inode,
942 struct dentry *new_dentry)
945 struct smack_known *isp;
946 struct smk_audit_info ad;
948 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
949 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
951 isp = smk_of_inode(old_dentry->d_inode);
952 rc = smk_curacc(isp, MAY_READWRITE, &ad);
953 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
955 if (rc == 0 && new_dentry->d_inode != NULL) {
956 isp = smk_of_inode(new_dentry->d_inode);
957 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
958 rc = smk_curacc(isp, MAY_READWRITE, &ad);
959 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
965 * smack_inode_permission - Smack version of permission()
966 * @inode: the inode in question
967 * @mask: the access requested
969 * This is the important Smack hook.
971 * Returns 0 if access is permitted, -EACCES otherwise
973 static int smack_inode_permission(struct inode *inode, int mask)
975 struct smk_audit_info ad;
976 int no_block = mask & MAY_NOT_BLOCK;
979 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
981 * No permission to check. Existence test. Yup, it's there.
986 /* May be droppable after audit */
989 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
990 smk_ad_setfield_u_fs_inode(&ad, inode);
991 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
992 rc = smk_bu_inode(inode, mask, rc);
997 * smack_inode_setattr - Smack check for setting attributes
998 * @dentry: the object
999 * @iattr: for the force flag
1001 * Returns 0 if access is permitted, an error code otherwise
1003 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1005 struct smk_audit_info ad;
1009 * Need to allow for clearing the setuid bit.
1011 if (iattr->ia_valid & ATTR_FORCE)
1013 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1014 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1016 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1017 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1022 * smack_inode_getattr - Smack check for getting attributes
1023 * @mnt: vfsmount of the object
1024 * @dentry: the object
1026 * Returns 0 if access is permitted, an error code otherwise
1028 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1030 struct smk_audit_info ad;
1034 path.dentry = dentry;
1037 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1038 smk_ad_setfield_u_fs_path(&ad, path);
1039 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1040 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1045 * smack_inode_setxattr - Smack check for setting xattrs
1046 * @dentry: the object
1047 * @name: name of the attribute
1048 * @value: value of the attribute
1049 * @size: size of the value
1052 * This protects the Smack attribute explicitly.
1054 * Returns 0 if access is permitted, an error code otherwise
1056 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1057 const void *value, size_t size, int flags)
1059 struct smk_audit_info ad;
1060 struct smack_known *skp;
1062 int check_import = 0;
1067 * Check label validity here so import won't fail in post_setxattr
1069 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1070 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1071 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1074 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1075 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1079 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1081 if (size != TRANS_TRUE_SIZE ||
1082 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1085 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1087 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1090 if (rc == 0 && check_import) {
1091 skp = size ? smk_import_entry(value, size) : NULL;
1092 if (skp == NULL || (check_star &&
1093 (skp == &smack_known_star || skp == &smack_known_web)))
1097 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1098 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1101 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1102 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1109 * smack_inode_post_setxattr - Apply the Smack update approved above
1111 * @name: attribute name
1112 * @value: attribute value
1113 * @size: attribute size
1116 * Set the pointer in the inode blob to the entry found
1117 * in the master label list.
1119 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1120 const void *value, size_t size, int flags)
1122 struct smack_known *skp;
1123 struct inode_smack *isp = dentry->d_inode->i_security;
1125 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1126 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1130 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1131 skp = smk_import_entry(value, size);
1133 isp->smk_inode = skp;
1135 isp->smk_inode = &smack_known_invalid;
1136 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1137 skp = smk_import_entry(value, size);
1139 isp->smk_task = skp;
1141 isp->smk_task = &smack_known_invalid;
1142 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1143 skp = smk_import_entry(value, size);
1145 isp->smk_mmap = skp;
1147 isp->smk_mmap = &smack_known_invalid;
1154 * smack_inode_getxattr - Smack check on getxattr
1155 * @dentry: the object
1158 * Returns 0 if access is permitted, an error code otherwise
1160 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1162 struct smk_audit_info ad;
1165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1166 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1168 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1169 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1174 * smack_inode_removexattr - Smack check on removexattr
1175 * @dentry: the object
1176 * @name: name of the attribute
1178 * Removing the Smack attribute requires CAP_MAC_ADMIN
1180 * Returns 0 if access is permitted, an error code otherwise
1182 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1184 struct inode_smack *isp;
1185 struct smk_audit_info ad;
1188 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1189 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1190 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1191 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1192 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1193 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1194 if (!smack_privileged(CAP_MAC_ADMIN))
1197 rc = cap_inode_removexattr(dentry, name);
1202 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1203 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1205 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1206 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1210 isp = dentry->d_inode->i_security;
1212 * Don't do anything special for these.
1213 * XATTR_NAME_SMACKIPIN
1214 * XATTR_NAME_SMACKIPOUT
1215 * XATTR_NAME_SMACKEXEC
1217 if (strcmp(name, XATTR_NAME_SMACK) == 0)
1218 isp->smk_task = NULL;
1219 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1220 isp->smk_mmap = NULL;
1221 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1222 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1228 * smack_inode_getsecurity - get smack xattrs
1229 * @inode: the object
1230 * @name: attribute name
1231 * @buffer: where to put the result
1234 * Returns the size of the attribute or an error code
1236 static int smack_inode_getsecurity(const struct inode *inode,
1237 const char *name, void **buffer,
1240 struct socket_smack *ssp;
1241 struct socket *sock;
1242 struct super_block *sbp;
1243 struct inode *ip = (struct inode *)inode;
1244 struct smack_known *isp;
1248 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1249 isp = smk_of_inode(inode);
1250 ilen = strlen(isp->smk_known);
1251 *buffer = isp->smk_known;
1256 * The rest of the Smack xattrs are only on sockets.
1259 if (sbp->s_magic != SOCKFS_MAGIC)
1262 sock = SOCKET_I(ip);
1263 if (sock == NULL || sock->sk == NULL)
1266 ssp = sock->sk->sk_security;
1268 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1270 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1275 ilen = strlen(isp->smk_known);
1277 *buffer = isp->smk_known;
1286 * smack_inode_listsecurity - list the Smack attributes
1287 * @inode: the object
1288 * @buffer: where they go
1289 * @buffer_size: size of buffer
1291 * Returns 0 on success, -EINVAL otherwise
1293 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1296 int len = sizeof(XATTR_NAME_SMACK);
1298 if (buffer != NULL && len <= buffer_size)
1299 memcpy(buffer, XATTR_NAME_SMACK, len);
1305 * smack_inode_getsecid - Extract inode's security id
1306 * @inode: inode to extract the info from
1307 * @secid: where result will be saved
1309 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1311 struct inode_smack *isp = inode->i_security;
1313 *secid = isp->smk_inode->smk_secid;
1321 * smack_file_permission - Smack check on file operations
1327 * Should access checks be done on each read or write?
1328 * UNICOS and SELinux say yes.
1329 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1331 * I'll say no for now. Smack does not do the frequent
1332 * label changing that SELinux does.
1334 static int smack_file_permission(struct file *file, int mask)
1340 * smack_file_alloc_security - assign a file security blob
1343 * The security blob for a file is a pointer to the master
1344 * label list, so no allocation is done.
1348 static int smack_file_alloc_security(struct file *file)
1350 struct smack_known *skp = smk_of_current();
1352 file->f_security = skp;
1357 * smack_file_free_security - clear a file security blob
1360 * The security blob for a file is a pointer to the master
1361 * label list, so no memory is freed.
1363 static void smack_file_free_security(struct file *file)
1365 file->f_security = NULL;
1369 * smack_file_ioctl - Smack check on ioctls
1374 * Relies heavily on the correct use of the ioctl command conventions.
1376 * Returns 0 if allowed, error code otherwise
1378 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1382 struct smk_audit_info ad;
1384 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1385 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1387 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1388 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1389 rc = smk_bu_file(file, MAY_WRITE, rc);
1392 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1393 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1394 rc = smk_bu_file(file, MAY_READ, rc);
1401 * smack_file_lock - Smack check on file locking
1405 * Returns 0 if current has lock access, error code otherwise
1407 static int smack_file_lock(struct file *file, unsigned int cmd)
1409 struct smk_audit_info ad;
1412 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1413 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1414 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1415 rc = smk_bu_file(file, MAY_LOCK, rc);
1420 * smack_file_fcntl - Smack check on fcntl
1422 * @cmd: what action to check
1425 * Generally these operations are harmless.
1426 * File locking operations present an obvious mechanism
1427 * for passing information, so they require write access.
1429 * Returns 0 if current has access, error code otherwise
1431 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1434 struct smk_audit_info ad;
1443 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1444 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1445 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1446 rc = smk_bu_file(file, MAY_LOCK, rc);
1450 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1451 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1452 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1453 rc = smk_bu_file(file, MAY_WRITE, rc);
1464 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1465 * if mapping anonymous memory.
1466 * @file contains the file structure for file to map (may be NULL).
1467 * @reqprot contains the protection requested by the application.
1468 * @prot contains the protection that will be applied by the kernel.
1469 * @flags contains the operational flags.
1470 * Return 0 if permission is granted.
1472 static int smack_mmap_file(struct file *file,
1473 unsigned long reqprot, unsigned long prot,
1474 unsigned long flags)
1476 struct smack_known *skp;
1477 struct smack_known *mkp;
1478 struct smack_rule *srp;
1479 struct task_smack *tsp;
1480 struct smack_known *okp;
1481 struct inode_smack *isp;
1490 isp = file_inode(file)->i_security;
1491 if (isp->smk_mmap == NULL)
1493 mkp = isp->smk_mmap;
1495 tsp = current_security();
1496 skp = smk_of_current();
1501 * For each Smack rule associated with the subject
1502 * label verify that the SMACK64MMAP also has access
1503 * to that rule's object label.
1505 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1506 okp = srp->smk_object;
1508 * Matching labels always allows access.
1510 if (mkp->smk_known == okp->smk_known)
1513 * If there is a matching local rule take
1514 * that into account as well.
1516 may = smk_access_entry(srp->smk_subject->smk_known,
1520 may = srp->smk_access;
1522 may &= srp->smk_access;
1524 * If may is zero the SMACK64MMAP subject can't
1525 * possibly have less access.
1531 * Fetch the global list entry.
1532 * If there isn't one a SMACK64MMAP subject
1533 * can't have as much access as current.
1535 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1537 if (mmay == -ENOENT) {
1542 * If there is a local entry it modifies the
1543 * potential access, too.
1545 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1547 if (tmay != -ENOENT)
1551 * If there is any access available to current that is
1552 * not available to a SMACK64MMAP subject
1555 if ((may | mmay) != mmay) {
1567 * smack_file_set_fowner - set the file security blob value
1568 * @file: object in question
1571 * Further research may be required on this one.
1573 static int smack_file_set_fowner(struct file *file)
1575 struct smack_known *skp = smk_of_current();
1577 file->f_security = skp;
1582 * smack_file_send_sigiotask - Smack on sigio
1583 * @tsk: The target task
1584 * @fown: the object the signal come from
1587 * Allow a privileged task to get signals even if it shouldn't
1589 * Returns 0 if a subject with the object's smack could
1590 * write to the task, an error code otherwise.
1592 static int smack_file_send_sigiotask(struct task_struct *tsk,
1593 struct fown_struct *fown, int signum)
1595 struct smack_known *skp;
1596 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1599 struct smk_audit_info ad;
1602 * struct fown_struct is never outside the context of a struct file
1604 file = container_of(fown, struct file, f_owner);
1606 /* we don't log here as rc can be overriden */
1607 skp = file->f_security;
1608 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1609 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
1610 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1613 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1614 smk_ad_setfield_u_tsk(&ad, tsk);
1615 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
1620 * smack_file_receive - Smack file receive check
1623 * Returns 0 if current has access, error code otherwise
1625 static int smack_file_receive(struct file *file)
1629 struct smk_audit_info ad;
1631 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1632 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1634 * This code relies on bitmasks.
1636 if (file->f_mode & FMODE_READ)
1638 if (file->f_mode & FMODE_WRITE)
1641 rc = smk_curacc(file->f_security, may, &ad);
1642 rc = smk_bu_file(file, may, rc);
1647 * smack_file_open - Smack dentry open processing
1649 * @cred: task credential
1651 * Set the security blob in the file structure.
1652 * Allow the open only if the task has read access. There are
1653 * many read operations (e.g. fstat) that you can do with an
1654 * fd even if you have the file open write-only.
1658 static int smack_file_open(struct file *file, const struct cred *cred)
1660 struct task_smack *tsp = cred->security;
1661 struct inode_smack *isp = file_inode(file)->i_security;
1662 struct smk_audit_info ad;
1665 if (smack_privileged(CAP_MAC_OVERRIDE)) {
1666 file->f_security = isp->smk_inode;
1670 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1671 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1672 rc = smk_access(tsp->smk_task, isp->smk_inode, MAY_READ, &ad);
1673 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
1675 file->f_security = isp->smk_inode;
1685 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1686 * @new: the new credentials
1687 * @gfp: the atomicity of any memory allocations
1689 * Prepare a blank set of credentials for modification. This must allocate all
1690 * the memory the LSM module might require such that cred_transfer() can
1691 * complete without error.
1693 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1695 struct task_smack *tsp;
1697 tsp = new_task_smack(NULL, NULL, gfp);
1701 cred->security = tsp;
1708 * smack_cred_free - "free" task-level security credentials
1709 * @cred: the credentials in question
1712 static void smack_cred_free(struct cred *cred)
1714 struct task_smack *tsp = cred->security;
1715 struct smack_rule *rp;
1716 struct list_head *l;
1717 struct list_head *n;
1721 cred->security = NULL;
1723 list_for_each_safe(l, n, &tsp->smk_rules) {
1724 rp = list_entry(l, struct smack_rule, list);
1725 list_del(&rp->list);
1732 * smack_cred_prepare - prepare new set of credentials for modification
1733 * @new: the new credentials
1734 * @old: the original credentials
1735 * @gfp: the atomicity of any memory allocations
1737 * Prepare a new set of credentials for modification.
1739 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1742 struct task_smack *old_tsp = old->security;
1743 struct task_smack *new_tsp;
1746 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1747 if (new_tsp == NULL)
1750 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1754 new->security = new_tsp;
1759 * smack_cred_transfer - Transfer the old credentials to the new credentials
1760 * @new: the new credentials
1761 * @old: the original credentials
1763 * Fill in a set of blank credentials from another set of credentials.
1765 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1767 struct task_smack *old_tsp = old->security;
1768 struct task_smack *new_tsp = new->security;
1770 new_tsp->smk_task = old_tsp->smk_task;
1771 new_tsp->smk_forked = old_tsp->smk_task;
1772 mutex_init(&new_tsp->smk_rules_lock);
1773 INIT_LIST_HEAD(&new_tsp->smk_rules);
1776 /* cbs copy rule list */
1780 * smack_kernel_act_as - Set the subjective context in a set of credentials
1781 * @new: points to the set of credentials to be modified.
1782 * @secid: specifies the security ID to be set
1784 * Set the security data for a kernel service.
1786 static int smack_kernel_act_as(struct cred *new, u32 secid)
1788 struct task_smack *new_tsp = new->security;
1789 struct smack_known *skp = smack_from_secid(secid);
1794 new_tsp->smk_task = skp;
1799 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1800 * @new: points to the set of credentials to be modified
1801 * @inode: points to the inode to use as a reference
1803 * Set the file creation context in a set of credentials to the same
1804 * as the objective context of the specified inode
1806 static int smack_kernel_create_files_as(struct cred *new,
1807 struct inode *inode)
1809 struct inode_smack *isp = inode->i_security;
1810 struct task_smack *tsp = new->security;
1812 tsp->smk_forked = isp->smk_inode;
1813 tsp->smk_task = tsp->smk_forked;
1818 * smk_curacc_on_task - helper to log task related access
1819 * @p: the task object
1820 * @access: the access requested
1821 * @caller: name of the calling function for audit
1823 * Return 0 if access is permitted
1825 static int smk_curacc_on_task(struct task_struct *p, int access,
1828 struct smk_audit_info ad;
1829 struct smack_known *skp = smk_of_task(task_security(p));
1832 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1833 smk_ad_setfield_u_tsk(&ad, p);
1834 rc = smk_curacc(skp, access, &ad);
1835 rc = smk_bu_task(p, access, rc);
1840 * smack_task_setpgid - Smack check on setting pgid
1841 * @p: the task object
1844 * Return 0 if write access is permitted
1846 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1848 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1852 * smack_task_getpgid - Smack access check for getpgid
1853 * @p: the object task
1855 * Returns 0 if current can read the object task, error code otherwise
1857 static int smack_task_getpgid(struct task_struct *p)
1859 return smk_curacc_on_task(p, MAY_READ, __func__);
1863 * smack_task_getsid - Smack access check for getsid
1864 * @p: the object task
1866 * Returns 0 if current can read the object task, error code otherwise
1868 static int smack_task_getsid(struct task_struct *p)
1870 return smk_curacc_on_task(p, MAY_READ, __func__);
1874 * smack_task_getsecid - get the secid of the task
1875 * @p: the object task
1876 * @secid: where to put the result
1878 * Sets the secid to contain a u32 version of the smack label.
1880 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1882 struct smack_known *skp = smk_of_task(task_security(p));
1884 *secid = skp->smk_secid;
1888 * smack_task_setnice - Smack check on setting nice
1889 * @p: the task object
1892 * Return 0 if write access is permitted
1894 static int smack_task_setnice(struct task_struct *p, int nice)
1898 rc = cap_task_setnice(p, nice);
1900 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1905 * smack_task_setioprio - Smack check on setting ioprio
1906 * @p: the task object
1909 * Return 0 if write access is permitted
1911 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1915 rc = cap_task_setioprio(p, ioprio);
1917 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1922 * smack_task_getioprio - Smack check on reading ioprio
1923 * @p: the task object
1925 * Return 0 if read access is permitted
1927 static int smack_task_getioprio(struct task_struct *p)
1929 return smk_curacc_on_task(p, MAY_READ, __func__);
1933 * smack_task_setscheduler - Smack check on setting scheduler
1934 * @p: the task object
1938 * Return 0 if read access is permitted
1940 static int smack_task_setscheduler(struct task_struct *p)
1944 rc = cap_task_setscheduler(p);
1946 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1951 * smack_task_getscheduler - Smack check on reading scheduler
1952 * @p: the task object
1954 * Return 0 if read access is permitted
1956 static int smack_task_getscheduler(struct task_struct *p)
1958 return smk_curacc_on_task(p, MAY_READ, __func__);
1962 * smack_task_movememory - Smack check on moving memory
1963 * @p: the task object
1965 * Return 0 if write access is permitted
1967 static int smack_task_movememory(struct task_struct *p)
1969 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1973 * smack_task_kill - Smack check on signal delivery
1974 * @p: the task object
1977 * @secid: identifies the smack to use in lieu of current's
1979 * Return 0 if write access is permitted
1981 * The secid behavior is an artifact of an SELinux hack
1982 * in the USB code. Someday it may go away.
1984 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1987 struct smk_audit_info ad;
1988 struct smack_known *skp;
1989 struct smack_known *tkp = smk_of_task(task_security(p));
1992 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1993 smk_ad_setfield_u_tsk(&ad, p);
1995 * Sending a signal requires that the sender
1996 * can write the receiver.
1999 rc = smk_curacc(tkp, MAY_WRITE, &ad);
2000 rc = smk_bu_task(p, MAY_WRITE, rc);
2004 * If the secid isn't 0 we're dealing with some USB IO
2005 * specific behavior. This is not clean. For one thing
2006 * we can't take privilege into account.
2008 skp = smack_from_secid(secid);
2009 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2010 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
2015 * smack_task_wait - Smack access check for waiting
2016 * @p: task to wait for
2020 static int smack_task_wait(struct task_struct *p)
2023 * Allow the operation to succeed.
2025 * In userless environments (e.g. phones) programs
2026 * get marked with SMACK64EXEC and even if the parent
2027 * and child shouldn't be talking the parent still
2028 * may expect to know when the child exits.
2034 * smack_task_to_inode - copy task smack into the inode blob
2035 * @p: task to copy from
2036 * @inode: inode to copy to
2038 * Sets the smack pointer in the inode security blob
2040 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2042 struct inode_smack *isp = inode->i_security;
2043 struct smack_known *skp = smk_of_task(task_security(p));
2045 isp->smk_inode = skp;
2053 * smack_sk_alloc_security - Allocate a socket blob
2056 * @gfp_flags: memory allocation flags
2058 * Assign Smack pointers to current
2060 * Returns 0 on success, -ENOMEM is there's no memory
2062 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2064 struct smack_known *skp = smk_of_current();
2065 struct socket_smack *ssp;
2067 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2073 ssp->smk_packet = NULL;
2075 sk->sk_security = ssp;
2081 * smack_sk_free_security - Free a socket blob
2084 * Clears the blob pointer
2086 static void smack_sk_free_security(struct sock *sk)
2088 kfree(sk->sk_security);
2092 * smack_host_label - check host based restrictions
2093 * @sip: the object end
2095 * looks for host based access restrictions
2097 * This version will only be appropriate for really small sets of single label
2098 * hosts. The caller is responsible for ensuring that the RCU read lock is
2099 * taken before calling this function.
2101 * Returns the label of the far end or NULL if it's not special.
2103 static struct smack_known *smack_host_label(struct sockaddr_in *sip)
2105 struct smk_netlbladdr *snp;
2106 struct in_addr *siap = &sip->sin_addr;
2108 if (siap->s_addr == 0)
2111 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2113 * we break after finding the first match because
2114 * the list is sorted from longest to shortest mask
2115 * so we have found the most specific match
2117 if ((&snp->smk_host.sin_addr)->s_addr ==
2118 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2119 /* we have found the special CIPSO option */
2120 if (snp->smk_label == &smack_cipso_option)
2122 return snp->smk_label;
2129 * smack_netlabel - Set the secattr on a socket
2131 * @labeled: socket label scheme
2133 * Convert the outbound smack value (smk_out) to a
2134 * secattr and attach it to the socket.
2136 * Returns 0 on success or an error code
2138 static int smack_netlabel(struct sock *sk, int labeled)
2140 struct smack_known *skp;
2141 struct socket_smack *ssp = sk->sk_security;
2145 * Usually the netlabel code will handle changing the
2146 * packet labeling based on the label.
2147 * The case of a single label host is different, because
2148 * a single label host should never get a labeled packet
2149 * even though the label is usually associated with a packet
2153 bh_lock_sock_nested(sk);
2155 if (ssp->smk_out == smack_net_ambient ||
2156 labeled == SMACK_UNLABELED_SOCKET)
2157 netlbl_sock_delattr(sk);
2160 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2170 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2172 * @sap: the destination address
2174 * Set the correct secattr for the given socket based on the destination
2175 * address and perform any outbound access checks needed.
2177 * Returns 0 on success or an error code.
2180 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2182 struct smack_known *skp;
2185 struct smack_known *hkp;
2186 struct socket_smack *ssp = sk->sk_security;
2187 struct smk_audit_info ad;
2190 hkp = smack_host_label(sap);
2193 struct lsm_network_audit net;
2195 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2196 ad.a.u.net->family = sap->sin_family;
2197 ad.a.u.net->dport = sap->sin_port;
2198 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2200 sk_lbl = SMACK_UNLABELED_SOCKET;
2202 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2203 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2205 sk_lbl = SMACK_CIPSO_SOCKET;
2212 return smack_netlabel(sk, sk_lbl);
2216 * smk_ipv6_port_label - Smack port access table management
2220 * Create or update the port list entry
2222 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2224 struct sock *sk = sock->sk;
2225 struct sockaddr_in6 *addr6;
2226 struct socket_smack *ssp = sock->sk->sk_security;
2227 struct smk_port_label *spp;
2228 unsigned short port = 0;
2230 if (address == NULL) {
2232 * This operation is changing the Smack information
2233 * on the bound socket. Take the changes to the port
2236 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2237 if (sk != spp->smk_sock)
2239 spp->smk_in = ssp->smk_in;
2240 spp->smk_out = ssp->smk_out;
2244 * A NULL address is only used for updating existing
2245 * bound entries. If there isn't one, it's OK.
2250 addr6 = (struct sockaddr_in6 *)address;
2251 port = ntohs(addr6->sin6_port);
2253 * This is a special case that is safely ignored.
2259 * Look for an existing port list entry.
2260 * This is an indication that a port is getting reused.
2262 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2263 if (spp->smk_port != port)
2265 spp->smk_port = port;
2267 spp->smk_in = ssp->smk_in;
2268 spp->smk_out = ssp->smk_out;
2273 * A new port entry is required.
2275 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2279 spp->smk_port = port;
2281 spp->smk_in = ssp->smk_in;
2282 spp->smk_out = ssp->smk_out;
2284 list_add(&spp->list, &smk_ipv6_port_list);
2289 * smk_ipv6_port_check - check Smack port access
2293 * Create or update the port list entry
2295 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2300 struct smk_port_label *spp;
2301 struct socket_smack *ssp = sk->sk_security;
2302 struct smack_known *skp;
2303 unsigned short port = 0;
2304 struct smack_known *object;
2305 struct smk_audit_info ad;
2308 struct lsm_network_audit net;
2311 if (act == SMK_RECEIVING) {
2312 skp = smack_net_ambient;
2313 object = ssp->smk_in;
2316 object = smack_net_ambient;
2320 * Get the IP address and port from the address.
2322 port = ntohs(address->sin6_port);
2323 bep = (__be16 *)(&address->sin6_addr);
2324 be32p = (__be32 *)(&address->sin6_addr);
2327 * It's remote, so port lookup does no good.
2329 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2333 * It's local so the send check has to have passed.
2335 if (act == SMK_RECEIVING) {
2336 skp = &smack_known_web;
2340 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2341 if (spp->smk_port != port)
2343 object = spp->smk_in;
2344 if (act == SMK_CONNECTING)
2345 ssp->smk_packet = spp->smk_out;
2352 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2353 ad.a.u.net->family = sk->sk_family;
2354 ad.a.u.net->dport = port;
2355 if (act == SMK_RECEIVING)
2356 ad.a.u.net->v6info.saddr = address->sin6_addr;
2358 ad.a.u.net->v6info.daddr = address->sin6_addr;
2360 rc = smk_access(skp, object, MAY_WRITE, &ad);
2361 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2366 * smack_inode_setsecurity - set smack xattrs
2367 * @inode: the object
2368 * @name: attribute name
2369 * @value: attribute value
2370 * @size: size of the attribute
2373 * Sets the named attribute in the appropriate blob
2375 * Returns 0 on success, or an error code
2377 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2378 const void *value, size_t size, int flags)
2380 struct smack_known *skp;
2381 struct inode_smack *nsp = inode->i_security;
2382 struct socket_smack *ssp;
2383 struct socket *sock;
2386 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2389 skp = smk_import_entry(value, size);
2393 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2394 nsp->smk_inode = skp;
2395 nsp->smk_flags |= SMK_INODE_INSTANT;
2399 * The rest of the Smack xattrs are only on sockets.
2401 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2404 sock = SOCKET_I(inode);
2405 if (sock == NULL || sock->sk == NULL)
2408 ssp = sock->sk->sk_security;
2410 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2412 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2414 if (sock->sk->sk_family == PF_INET) {
2415 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2418 "Smack: \"%s\" netlbl error %d.\n",
2424 if (sock->sk->sk_family == PF_INET6)
2425 smk_ipv6_port_label(sock, NULL);
2431 * smack_socket_post_create - finish socket setup
2433 * @family: protocol family
2438 * Sets the netlabel information on the socket
2440 * Returns 0 on success, and error code otherwise
2442 static int smack_socket_post_create(struct socket *sock, int family,
2443 int type, int protocol, int kern)
2445 if (family != PF_INET || sock->sk == NULL)
2448 * Set the outbound netlbl.
2450 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2454 * smack_socket_bind - record port binding information.
2456 * @address: the port address
2457 * @addrlen: size of the address
2459 * Records the label bound to a port.
2463 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2466 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2467 smk_ipv6_port_label(sock, address);
2473 * smack_socket_connect - connect access check
2475 * @sap: the other end
2476 * @addrlen: size of sap
2478 * Verifies that a connection may be possible
2480 * Returns 0 on success, and error code otherwise
2482 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2487 if (sock->sk == NULL)
2490 switch (sock->sk->sk_family) {
2492 if (addrlen < sizeof(struct sockaddr_in))
2494 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2497 if (addrlen < sizeof(struct sockaddr_in6))
2499 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2507 * smack_flags_to_may - convert S_ to MAY_ values
2508 * @flags: the S_ value
2510 * Returns the equivalent MAY_ value
2512 static int smack_flags_to_may(int flags)
2516 if (flags & S_IRUGO)
2518 if (flags & S_IWUGO)
2520 if (flags & S_IXUGO)
2527 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2532 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2534 struct smack_known *skp = smk_of_current();
2536 msg->security = skp;
2541 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2544 * Clears the blob pointer
2546 static void smack_msg_msg_free_security(struct msg_msg *msg)
2548 msg->security = NULL;
2552 * smack_of_shm - the smack pointer for the shm
2555 * Returns a pointer to the smack value
2557 static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
2559 return (struct smack_known *)shp->shm_perm.security;
2563 * smack_shm_alloc_security - Set the security blob for shm
2568 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2570 struct kern_ipc_perm *isp = &shp->shm_perm;
2571 struct smack_known *skp = smk_of_current();
2573 isp->security = skp;
2578 * smack_shm_free_security - Clear the security blob for shm
2581 * Clears the blob pointer
2583 static void smack_shm_free_security(struct shmid_kernel *shp)
2585 struct kern_ipc_perm *isp = &shp->shm_perm;
2587 isp->security = NULL;
2591 * smk_curacc_shm : check if current has access on shm
2593 * @access : access requested
2595 * Returns 0 if current has the requested access, error code otherwise
2597 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2599 struct smack_known *ssp = smack_of_shm(shp);
2600 struct smk_audit_info ad;
2604 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2605 ad.a.u.ipc_id = shp->shm_perm.id;
2607 rc = smk_curacc(ssp, access, &ad);
2608 rc = smk_bu_current("shm", ssp, access, rc);
2613 * smack_shm_associate - Smack access check for shm
2615 * @shmflg: access requested
2617 * Returns 0 if current has the requested access, error code otherwise
2619 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2623 may = smack_flags_to_may(shmflg);
2624 return smk_curacc_shm(shp, may);
2628 * smack_shm_shmctl - Smack access check for shm
2630 * @cmd: what it wants to do
2632 * Returns 0 if current has the requested access, error code otherwise
2634 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2647 may = MAY_READWRITE;
2652 * System level information.
2658 return smk_curacc_shm(shp, may);
2662 * smack_shm_shmat - Smack access for shmat
2665 * @shmflg: access requested
2667 * Returns 0 if current has the requested access, error code otherwise
2669 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2674 may = smack_flags_to_may(shmflg);
2675 return smk_curacc_shm(shp, may);
2679 * smack_of_sem - the smack pointer for the sem
2682 * Returns a pointer to the smack value
2684 static struct smack_known *smack_of_sem(struct sem_array *sma)
2686 return (struct smack_known *)sma->sem_perm.security;
2690 * smack_sem_alloc_security - Set the security blob for sem
2695 static int smack_sem_alloc_security(struct sem_array *sma)
2697 struct kern_ipc_perm *isp = &sma->sem_perm;
2698 struct smack_known *skp = smk_of_current();
2700 isp->security = skp;
2705 * smack_sem_free_security - Clear the security blob for sem
2708 * Clears the blob pointer
2710 static void smack_sem_free_security(struct sem_array *sma)
2712 struct kern_ipc_perm *isp = &sma->sem_perm;
2714 isp->security = NULL;
2718 * smk_curacc_sem : check if current has access on sem
2720 * @access : access requested
2722 * Returns 0 if current has the requested access, error code otherwise
2724 static int smk_curacc_sem(struct sem_array *sma, int access)
2726 struct smack_known *ssp = smack_of_sem(sma);
2727 struct smk_audit_info ad;
2731 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2732 ad.a.u.ipc_id = sma->sem_perm.id;
2734 rc = smk_curacc(ssp, access, &ad);
2735 rc = smk_bu_current("sem", ssp, access, rc);
2740 * smack_sem_associate - Smack access check for sem
2742 * @semflg: access requested
2744 * Returns 0 if current has the requested access, error code otherwise
2746 static int smack_sem_associate(struct sem_array *sma, int semflg)
2750 may = smack_flags_to_may(semflg);
2751 return smk_curacc_sem(sma, may);
2755 * smack_sem_shmctl - Smack access check for sem
2757 * @cmd: what it wants to do
2759 * Returns 0 if current has the requested access, error code otherwise
2761 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2779 may = MAY_READWRITE;
2784 * System level information
2791 return smk_curacc_sem(sma, may);
2795 * smack_sem_semop - Smack checks of semaphore operations
2801 * Treated as read and write in all cases.
2803 * Returns 0 if access is allowed, error code otherwise
2805 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2806 unsigned nsops, int alter)
2808 return smk_curacc_sem(sma, MAY_READWRITE);
2812 * smack_msg_alloc_security - Set the security blob for msg
2817 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2819 struct kern_ipc_perm *kisp = &msq->q_perm;
2820 struct smack_known *skp = smk_of_current();
2822 kisp->security = skp;
2827 * smack_msg_free_security - Clear the security blob for msg
2830 * Clears the blob pointer
2832 static void smack_msg_queue_free_security(struct msg_queue *msq)
2834 struct kern_ipc_perm *kisp = &msq->q_perm;
2836 kisp->security = NULL;
2840 * smack_of_msq - the smack pointer for the msq
2843 * Returns a pointer to the smack label entry
2845 static struct smack_known *smack_of_msq(struct msg_queue *msq)
2847 return (struct smack_known *)msq->q_perm.security;
2851 * smk_curacc_msq : helper to check if current has access on msq
2853 * @access : access requested
2855 * return 0 if current has access, error otherwise
2857 static int smk_curacc_msq(struct msg_queue *msq, int access)
2859 struct smack_known *msp = smack_of_msq(msq);
2860 struct smk_audit_info ad;
2864 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2865 ad.a.u.ipc_id = msq->q_perm.id;
2867 rc = smk_curacc(msp, access, &ad);
2868 rc = smk_bu_current("msq", msp, access, rc);
2873 * smack_msg_queue_associate - Smack access check for msg_queue
2875 * @msqflg: access requested
2877 * Returns 0 if current has the requested access, error code otherwise
2879 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2883 may = smack_flags_to_may(msqflg);
2884 return smk_curacc_msq(msq, may);
2888 * smack_msg_queue_msgctl - Smack access check for msg_queue
2890 * @cmd: what it wants to do
2892 * Returns 0 if current has the requested access, error code otherwise
2894 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2905 may = MAY_READWRITE;
2910 * System level information
2917 return smk_curacc_msq(msq, may);
2921 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2924 * @msqflg: access requested
2926 * Returns 0 if current has the requested access, error code otherwise
2928 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2933 may = smack_flags_to_may(msqflg);
2934 return smk_curacc_msq(msq, may);
2938 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2945 * Returns 0 if current has read and write access, error code otherwise
2947 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2948 struct task_struct *target, long type, int mode)
2950 return smk_curacc_msq(msq, MAY_READWRITE);
2954 * smack_ipc_permission - Smack access for ipc_permission()
2955 * @ipp: the object permissions
2956 * @flag: access requested
2958 * Returns 0 if current has read and write access, error code otherwise
2960 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2962 struct smack_known *iskp = ipp->security;
2963 int may = smack_flags_to_may(flag);
2964 struct smk_audit_info ad;
2968 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2969 ad.a.u.ipc_id = ipp->id;
2971 rc = smk_curacc(iskp, may, &ad);
2972 rc = smk_bu_current("svipc", iskp, may, rc);
2977 * smack_ipc_getsecid - Extract smack security id
2978 * @ipp: the object permissions
2979 * @secid: where result will be saved
2981 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2983 struct smack_known *iskp = ipp->security;
2985 *secid = iskp->smk_secid;
2989 * smack_d_instantiate - Make sure the blob is correct on an inode
2990 * @opt_dentry: dentry where inode will be attached
2991 * @inode: the object
2993 * Set the inode's security blob if it hasn't been done already.
2995 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2997 struct super_block *sbp;
2998 struct superblock_smack *sbsp;
2999 struct inode_smack *isp;
3000 struct smack_known *skp;
3001 struct smack_known *ckp = smk_of_current();
3002 struct smack_known *final;
3003 char trattr[TRANS_TRUE_SIZE];
3011 isp = inode->i_security;
3013 mutex_lock(&isp->smk_lock);
3015 * If the inode is already instantiated
3016 * take the quick way out
3018 if (isp->smk_flags & SMK_INODE_INSTANT)
3022 sbsp = sbp->s_security;
3024 * We're going to use the superblock default label
3025 * if there's no label on the file.
3027 final = sbsp->smk_default;
3030 * If this is the root inode the superblock
3031 * may be in the process of initialization.
3032 * If that is the case use the root value out
3033 * of the superblock.
3035 if (opt_dentry->d_parent == opt_dentry) {
3036 if (sbp->s_magic == CGROUP_SUPER_MAGIC) {
3038 * The cgroup filesystem is never mounted,
3039 * so there's no opportunity to set the mount
3042 sbsp->smk_root = &smack_known_star;
3043 sbsp->smk_default = &smack_known_star;
3045 isp->smk_inode = sbsp->smk_root;
3046 isp->smk_flags |= SMK_INODE_INSTANT;
3051 * This is pretty hackish.
3052 * Casey says that we shouldn't have to do
3053 * file system specific code, but it does help
3054 * with keeping it simple.
3056 switch (sbp->s_magic) {
3060 case CGROUP_SUPER_MAGIC:
3062 * Casey says that it's a little embarrassing
3063 * that the smack file system doesn't do
3064 * extended attributes.
3066 * Casey says pipes are easy (?)
3068 * Socket access is controlled by the socket
3069 * structures associated with the task involved.
3071 * Cgroupfs is special
3073 final = &smack_known_star;
3075 case DEVPTS_SUPER_MAGIC:
3077 * devpts seems content with the label of the task.
3078 * Programs that change smack have to treat the
3083 case PROC_SUPER_MAGIC:
3085 * Casey says procfs appears not to care.
3086 * The superblock default suffices.
3091 * Device labels should come from the filesystem,
3092 * but watch out, because they're volitile,
3093 * getting recreated on every reboot.
3095 final = &smack_known_star;
3099 * If a smack value has been set we want to use it,
3100 * but since tmpfs isn't giving us the opportunity
3101 * to set mount options simulate setting the
3102 * superblock default.
3106 * This isn't an understood special case.
3107 * Get the value from the xattr.
3111 * UNIX domain sockets use lower level socket data.
3113 if (S_ISSOCK(inode->i_mode)) {
3114 final = &smack_known_star;
3118 * No xattr support means, alas, no SMACK label.
3119 * Use the aforeapplied default.
3120 * It would be curious if the label of the task
3121 * does not match that assigned.
3123 if (inode->i_op->getxattr == NULL)
3126 * Get the dentry for xattr.
3128 dp = dget(opt_dentry);
3129 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3134 * Transmuting directory
3136 if (S_ISDIR(inode->i_mode)) {
3138 * If this is a new directory and the label was
3139 * transmuted when the inode was initialized
3140 * set the transmute attribute on the directory
3141 * and mark the inode.
3143 * If there is a transmute attribute on the
3144 * directory mark the inode.
3146 if (isp->smk_flags & SMK_INODE_CHANGED) {
3147 isp->smk_flags &= ~SMK_INODE_CHANGED;
3148 rc = inode->i_op->setxattr(dp,
3149 XATTR_NAME_SMACKTRANSMUTE,
3150 TRANS_TRUE, TRANS_TRUE_SIZE,
3153 rc = inode->i_op->getxattr(dp,
3154 XATTR_NAME_SMACKTRANSMUTE, trattr,
3156 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3157 TRANS_TRUE_SIZE) != 0)
3161 transflag = SMK_INODE_TRANSMUTE;
3164 * Don't let the exec or mmap label be "*" or "@".
3166 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3167 if (skp == &smack_known_star || skp == &smack_known_web)
3169 isp->smk_task = skp;
3170 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3171 if (skp == &smack_known_star || skp == &smack_known_web)
3173 isp->smk_mmap = skp;
3180 isp->smk_inode = ckp;
3182 isp->smk_inode = final;
3184 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3187 mutex_unlock(&isp->smk_lock);
3192 * smack_getprocattr - Smack process attribute access
3193 * @p: the object task
3194 * @name: the name of the attribute in /proc/.../attr
3195 * @value: where to put the result
3197 * Places a copy of the task Smack into value
3199 * Returns the length of the smack label or an error code
3201 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3203 struct smack_known *skp = smk_of_task(task_security(p));
3207 if (strcmp(name, "current") != 0)
3210 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3220 * smack_setprocattr - Smack process attribute setting
3221 * @p: the object task
3222 * @name: the name of the attribute in /proc/.../attr
3223 * @value: the value to set
3224 * @size: the size of the value
3226 * Sets the Smack value of the task. Only setting self
3227 * is permitted and only with privilege
3229 * Returns the length of the smack label or an error code
3231 static int smack_setprocattr(struct task_struct *p, char *name,
3232 void *value, size_t size)
3234 struct task_smack *tsp;
3236 struct smack_known *skp;
3239 * Changing another process' Smack value is too dangerous
3240 * and supports no sane use case.
3245 if (!smack_privileged(CAP_MAC_ADMIN))
3248 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3251 if (strcmp(name, "current") != 0)
3254 skp = smk_import_entry(value, size);
3259 * No process is ever allowed the web ("@") label.
3261 if (skp == &smack_known_web)
3264 new = prepare_creds();
3268 tsp = new->security;
3269 tsp->smk_task = skp;
3276 * smack_unix_stream_connect - Smack access on UDS
3278 * @other: the other sock
3281 * Return 0 if a subject with the smack of sock could access
3282 * an object with the smack of other, otherwise an error code
3284 static int smack_unix_stream_connect(struct sock *sock,
3285 struct sock *other, struct sock *newsk)
3287 struct smack_known *skp;
3288 struct smack_known *okp;
3289 struct socket_smack *ssp = sock->sk_security;
3290 struct socket_smack *osp = other->sk_security;
3291 struct socket_smack *nsp = newsk->sk_security;
3292 struct smk_audit_info ad;
3295 struct lsm_network_audit net;
3298 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3302 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3303 smk_ad_setfield_u_net_sk(&ad, other);
3305 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3306 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3308 rc = smk_access(okp, skp, MAY_WRITE, NULL);
3309 rc = smk_bu_note("UDS connect", okp, skp,
3315 * Cross reference the peer labels for SO_PEERSEC.
3318 nsp->smk_packet = ssp->smk_out;
3319 ssp->smk_packet = osp->smk_out;
3326 * smack_unix_may_send - Smack access on UDS
3328 * @other: the other socket
3330 * Return 0 if a subject with the smack of sock could access
3331 * an object with the smack of other, otherwise an error code
3333 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3335 struct socket_smack *ssp = sock->sk->sk_security;
3336 struct socket_smack *osp = other->sk->sk_security;
3337 struct smk_audit_info ad;
3341 struct lsm_network_audit net;
3343 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3344 smk_ad_setfield_u_net_sk(&ad, other->sk);
3347 if (smack_privileged(CAP_MAC_OVERRIDE))
3350 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3351 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
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, MAY_WRITE, &ad);
3567 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
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;
3711 struct smack_known *hskp;
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, MAY_WRITE, &ad);
3749 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
3754 * Save the peer's label in the request_sock so we can later setup
3755 * smk_packet in the child socket so that SO_PEERCRED can report it.
3757 req->peer_secid = skp->smk_secid;
3760 * We need to decide if we want to label the incoming connection here
3761 * if we do we only need to label the request_sock and the stack will
3762 * propagate the wire-label to the sock when it is created.
3765 addr.sin_addr.s_addr = hdr->saddr;
3767 hskp = smack_host_label(&addr);
3771 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3773 netlbl_req_delattr(req);
3779 * smack_inet_csk_clone - Copy the connection information to the new socket
3780 * @sk: the new socket
3781 * @req: the connection's request_sock
3783 * Transfer the connection's peer label to the newly created socket.
3785 static void smack_inet_csk_clone(struct sock *sk,
3786 const struct request_sock *req)
3788 struct socket_smack *ssp = sk->sk_security;
3789 struct smack_known *skp;
3791 if (req->peer_secid != 0) {
3792 skp = smack_from_secid(req->peer_secid);
3793 ssp->smk_packet = skp;
3795 ssp->smk_packet = NULL;
3799 * Key management security hooks
3801 * Casey has not tested key support very heavily.
3802 * The permission check is most likely too restrictive.
3803 * If you care about keys please have a look.
3808 * smack_key_alloc - Set the key security blob
3810 * @cred: the credentials to use
3813 * No allocation required
3817 static int smack_key_alloc(struct key *key, const struct cred *cred,
3818 unsigned long flags)
3820 struct smack_known *skp = smk_of_task(cred->security);
3822 key->security = skp;
3827 * smack_key_free - Clear the key security blob
3830 * Clear the blob pointer
3832 static void smack_key_free(struct key *key)
3834 key->security = NULL;
3838 * smack_key_permission - Smack access on a key
3839 * @key_ref: gets to the object
3840 * @cred: the credentials to use
3843 * Return 0 if the task has read and write to the object,
3844 * an error code otherwise
3846 static int smack_key_permission(key_ref_t key_ref,
3847 const struct cred *cred, unsigned perm)
3850 struct smk_audit_info ad;
3851 struct smack_known *tkp = smk_of_task(cred->security);
3855 keyp = key_ref_to_ptr(key_ref);
3859 * If the key hasn't been initialized give it access so that
3862 if (keyp->security == NULL)
3865 * This should not occur
3870 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3871 ad.a.u.key_struct.key = keyp->serial;
3872 ad.a.u.key_struct.key_desc = keyp->description;
3874 if (perm & KEY_NEED_READ)
3876 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
3877 request = MAY_WRITE;
3878 rc = smk_access(tkp, keyp->security, request, &ad);
3879 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
3882 #endif /* CONFIG_KEYS */
3887 * Audit requires a unique representation of each Smack specific
3888 * rule. This unique representation is used to distinguish the
3889 * object to be audited from remaining kernel objects and also
3890 * works as a glue between the audit hooks.
3892 * Since repository entries are added but never deleted, we'll use
3893 * the smack_known label address related to the given audit rule as
3894 * the needed unique representation. This also better fits the smack
3895 * model where nearly everything is a label.
3900 * smack_audit_rule_init - Initialize a smack audit rule
3901 * @field: audit rule fields given from user-space (audit.h)
3902 * @op: required testing operator (=, !=, >, <, ...)
3903 * @rulestr: smack label to be audited
3904 * @vrule: pointer to save our own audit rule representation
3906 * Prepare to audit cases where (@field @op @rulestr) is true.
3907 * The label to be audited is created if necessay.
3909 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3911 struct smack_known *skp;
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 skp = smk_import_entry(rulestr, 0);
3923 *rule = skp->smk_known;
3929 * smack_audit_rule_known - Distinguish Smack audit rules
3930 * @krule: rule of interest, in Audit kernel representation format
3932 * This is used to filter Smack rules from remaining Audit ones.
3933 * If it's proved that this rule belongs to us, the
3934 * audit_rule_match hook will be called to do the final judgement.
3936 static int smack_audit_rule_known(struct audit_krule *krule)
3938 struct audit_field *f;
3941 for (i = 0; i < krule->field_count; i++) {
3942 f = &krule->fields[i];
3944 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3952 * smack_audit_rule_match - Audit given object ?
3953 * @secid: security id for identifying the object to test
3954 * @field: audit rule flags given from user-space
3955 * @op: required testing operator
3956 * @vrule: smack internal rule presentation
3957 * @actx: audit context associated with the check
3959 * The core Audit hook. It's used to take the decision of
3960 * whether to audit or not to audit a given object.
3962 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3963 struct audit_context *actx)
3965 struct smack_known *skp;
3968 if (unlikely(!rule)) {
3969 WARN_ONCE(1, "Smack: missing rule\n");
3973 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3976 skp = smack_from_secid(secid);
3979 * No need to do string comparisons. If a match occurs,
3980 * both pointers will point to the same smack_known
3983 if (op == Audit_equal)
3984 return (rule == skp->smk_known);
3985 if (op == Audit_not_equal)
3986 return (rule != skp->smk_known);
3992 * smack_audit_rule_free - free smack rule representation
3993 * @vrule: rule to be freed.
3995 * No memory was allocated.
3997 static void smack_audit_rule_free(void *vrule)
4002 #endif /* CONFIG_AUDIT */
4005 * smack_ismaclabel - check if xattr @name references a smack MAC label
4006 * @name: Full xattr name to check.
4008 static int smack_ismaclabel(const char *name)
4010 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4015 * smack_secid_to_secctx - return the smack label for a secid
4016 * @secid: incoming integer
4017 * @secdata: destination
4018 * @seclen: how long it is
4020 * Exists for networking code.
4022 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4024 struct smack_known *skp = smack_from_secid(secid);
4027 *secdata = skp->smk_known;
4028 *seclen = strlen(skp->smk_known);
4033 * smack_secctx_to_secid - return the secid for a smack label
4034 * @secdata: smack label
4035 * @seclen: how long result is
4036 * @secid: outgoing integer
4038 * Exists for audit and networking code.
4040 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4042 struct smack_known *skp = smk_find_entry(secdata);
4045 *secid = skp->smk_secid;
4052 * smack_release_secctx - don't do anything.
4056 * Exists to make sure nothing gets done, and properly
4058 static void smack_release_secctx(char *secdata, u32 seclen)
4062 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4064 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4067 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4069 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4072 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4075 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4083 struct security_operations smack_ops = {
4086 .ptrace_access_check = smack_ptrace_access_check,
4087 .ptrace_traceme = smack_ptrace_traceme,
4088 .syslog = smack_syslog,
4090 .sb_alloc_security = smack_sb_alloc_security,
4091 .sb_free_security = smack_sb_free_security,
4092 .sb_copy_data = smack_sb_copy_data,
4093 .sb_kern_mount = smack_sb_kern_mount,
4094 .sb_statfs = smack_sb_statfs,
4096 .bprm_set_creds = smack_bprm_set_creds,
4097 .bprm_committing_creds = smack_bprm_committing_creds,
4098 .bprm_secureexec = smack_bprm_secureexec,
4100 .inode_alloc_security = smack_inode_alloc_security,
4101 .inode_free_security = smack_inode_free_security,
4102 .inode_init_security = smack_inode_init_security,
4103 .inode_link = smack_inode_link,
4104 .inode_unlink = smack_inode_unlink,
4105 .inode_rmdir = smack_inode_rmdir,
4106 .inode_rename = smack_inode_rename,
4107 .inode_permission = smack_inode_permission,
4108 .inode_setattr = smack_inode_setattr,
4109 .inode_getattr = smack_inode_getattr,
4110 .inode_setxattr = smack_inode_setxattr,
4111 .inode_post_setxattr = smack_inode_post_setxattr,
4112 .inode_getxattr = smack_inode_getxattr,
4113 .inode_removexattr = smack_inode_removexattr,
4114 .inode_getsecurity = smack_inode_getsecurity,
4115 .inode_setsecurity = smack_inode_setsecurity,
4116 .inode_listsecurity = smack_inode_listsecurity,
4117 .inode_getsecid = smack_inode_getsecid,
4119 .file_permission = smack_file_permission,
4120 .file_alloc_security = smack_file_alloc_security,
4121 .file_free_security = smack_file_free_security,
4122 .file_ioctl = smack_file_ioctl,
4123 .file_lock = smack_file_lock,
4124 .file_fcntl = smack_file_fcntl,
4125 .mmap_file = smack_mmap_file,
4126 .mmap_addr = cap_mmap_addr,
4127 .file_set_fowner = smack_file_set_fowner,
4128 .file_send_sigiotask = smack_file_send_sigiotask,
4129 .file_receive = smack_file_receive,
4131 .file_open = smack_file_open,
4133 .cred_alloc_blank = smack_cred_alloc_blank,
4134 .cred_free = smack_cred_free,
4135 .cred_prepare = smack_cred_prepare,
4136 .cred_transfer = smack_cred_transfer,
4137 .kernel_act_as = smack_kernel_act_as,
4138 .kernel_create_files_as = smack_kernel_create_files_as,
4139 .task_setpgid = smack_task_setpgid,
4140 .task_getpgid = smack_task_getpgid,
4141 .task_getsid = smack_task_getsid,
4142 .task_getsecid = smack_task_getsecid,
4143 .task_setnice = smack_task_setnice,
4144 .task_setioprio = smack_task_setioprio,
4145 .task_getioprio = smack_task_getioprio,
4146 .task_setscheduler = smack_task_setscheduler,
4147 .task_getscheduler = smack_task_getscheduler,
4148 .task_movememory = smack_task_movememory,
4149 .task_kill = smack_task_kill,
4150 .task_wait = smack_task_wait,
4151 .task_to_inode = smack_task_to_inode,
4153 .ipc_permission = smack_ipc_permission,
4154 .ipc_getsecid = smack_ipc_getsecid,
4156 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4157 .msg_msg_free_security = smack_msg_msg_free_security,
4159 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4160 .msg_queue_free_security = smack_msg_queue_free_security,
4161 .msg_queue_associate = smack_msg_queue_associate,
4162 .msg_queue_msgctl = smack_msg_queue_msgctl,
4163 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4164 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4166 .shm_alloc_security = smack_shm_alloc_security,
4167 .shm_free_security = smack_shm_free_security,
4168 .shm_associate = smack_shm_associate,
4169 .shm_shmctl = smack_shm_shmctl,
4170 .shm_shmat = smack_shm_shmat,
4172 .sem_alloc_security = smack_sem_alloc_security,
4173 .sem_free_security = smack_sem_free_security,
4174 .sem_associate = smack_sem_associate,
4175 .sem_semctl = smack_sem_semctl,
4176 .sem_semop = smack_sem_semop,
4178 .d_instantiate = smack_d_instantiate,
4180 .getprocattr = smack_getprocattr,
4181 .setprocattr = smack_setprocattr,
4183 .unix_stream_connect = smack_unix_stream_connect,
4184 .unix_may_send = smack_unix_may_send,
4186 .socket_post_create = smack_socket_post_create,
4187 .socket_bind = smack_socket_bind,
4188 .socket_connect = smack_socket_connect,
4189 .socket_sendmsg = smack_socket_sendmsg,
4190 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4191 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4192 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4193 .sk_alloc_security = smack_sk_alloc_security,
4194 .sk_free_security = smack_sk_free_security,
4195 .sock_graft = smack_sock_graft,
4196 .inet_conn_request = smack_inet_conn_request,
4197 .inet_csk_clone = smack_inet_csk_clone,
4199 /* key management security hooks */
4201 .key_alloc = smack_key_alloc,
4202 .key_free = smack_key_free,
4203 .key_permission = smack_key_permission,
4204 #endif /* CONFIG_KEYS */
4208 .audit_rule_init = smack_audit_rule_init,
4209 .audit_rule_known = smack_audit_rule_known,
4210 .audit_rule_match = smack_audit_rule_match,
4211 .audit_rule_free = smack_audit_rule_free,
4212 #endif /* CONFIG_AUDIT */
4214 .ismaclabel = smack_ismaclabel,
4215 .secid_to_secctx = smack_secid_to_secctx,
4216 .secctx_to_secid = smack_secctx_to_secid,
4217 .release_secctx = smack_release_secctx,
4218 .inode_notifysecctx = smack_inode_notifysecctx,
4219 .inode_setsecctx = smack_inode_setsecctx,
4220 .inode_getsecctx = smack_inode_getsecctx,
4224 static __init void init_smack_known_list(void)
4227 * Initialize rule list locks
4229 mutex_init(&smack_known_huh.smk_rules_lock);
4230 mutex_init(&smack_known_hat.smk_rules_lock);
4231 mutex_init(&smack_known_floor.smk_rules_lock);
4232 mutex_init(&smack_known_star.smk_rules_lock);
4233 mutex_init(&smack_known_invalid.smk_rules_lock);
4234 mutex_init(&smack_known_web.smk_rules_lock);
4236 * Initialize rule lists
4238 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4239 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4240 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4241 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4242 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4243 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4245 * Create the known labels list
4247 smk_insert_entry(&smack_known_huh);
4248 smk_insert_entry(&smack_known_hat);
4249 smk_insert_entry(&smack_known_star);
4250 smk_insert_entry(&smack_known_floor);
4251 smk_insert_entry(&smack_known_invalid);
4252 smk_insert_entry(&smack_known_web);
4256 * smack_init - initialize the smack system
4260 static __init int smack_init(void)
4263 struct task_smack *tsp;
4265 if (!security_module_enable(&smack_ops))
4268 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4273 printk(KERN_INFO "Smack: Initializing.\n");
4276 * Set the security state for the initial task.
4278 cred = (struct cred *) current->cred;
4279 cred->security = tsp;
4281 /* initialize the smack_known_list */
4282 init_smack_known_list();
4287 if (register_security(&smack_ops))
4288 panic("smack: Unable to register with kernel.\n");
4294 * Smack requires early initialization in order to label
4295 * all processes and objects when they are created.
4297 security_initcall(smack_init);