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/slab.h>
31 #include <linux/mutex.h>
32 #include <linux/pipe_fs_i.h>
33 #include <net/netlabel.h>
34 #include <net/cipso_ipv4.h>
35 #include <linux/audit.h>
36 #include <linux/magic.h>
37 #include <linux/dcache.h>
38 #include <linux/personality.h>
41 #define task_security(task) (task_cred_xxx((task), security))
43 #define TRANS_TRUE "TRUE"
44 #define TRANS_TRUE_SIZE 4
47 * smk_fetch - Fetch the smack label from a file.
48 * @ip: a pointer to the inode
49 * @dp: a pointer to the dentry
51 * Returns a pointer to the master list entry for the Smack label
52 * or NULL if there was no label to fetch.
54 static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
57 char in[SMK_LABELLEN];
59 if (ip->i_op->getxattr == NULL)
62 rc = ip->i_op->getxattr(dp, name, in, SMK_LABELLEN);
66 return smk_import(in, rc);
70 * new_inode_smack - allocate an inode security blob
71 * @smack: a pointer to the Smack label to use in the blob
73 * Returns the new blob or NULL if there's no memory available
75 struct inode_smack *new_inode_smack(char *smack)
77 struct inode_smack *isp;
79 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
83 isp->smk_inode = smack;
85 mutex_init(&isp->smk_lock);
91 * new_task_smack - allocate a task security blob
92 * @smack: a pointer to the Smack label to use in the blob
94 * Returns the new blob or NULL if there's no memory available
96 static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
98 struct task_smack *tsp;
100 tsp = kzalloc(sizeof(struct task_smack), gfp);
104 tsp->smk_task = task;
105 tsp->smk_forked = forked;
106 INIT_LIST_HEAD(&tsp->smk_rules);
107 mutex_init(&tsp->smk_rules_lock);
113 * smk_copy_rules - copy a rule set
114 * @nhead - new rules header pointer
115 * @ohead - old rules header pointer
117 * Returns 0 on success, -ENOMEM on error
119 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
122 struct smack_rule *nrp;
123 struct smack_rule *orp;
126 INIT_LIST_HEAD(nhead);
128 list_for_each_entry_rcu(orp, ohead, list) {
129 nrp = kzalloc(sizeof(struct smack_rule), gfp);
135 list_add_rcu(&nrp->list, nhead);
142 * We he, that is fun!
146 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
147 * @ctp: child task pointer
148 * @mode: ptrace attachment mode
150 * Returns 0 if access is OK, an error code otherwise
152 * Do the capability checks, and require read and write.
154 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
157 struct smk_audit_info ad;
160 rc = cap_ptrace_access_check(ctp, mode);
164 tsp = smk_of_task(task_security(ctp));
165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
166 smk_ad_setfield_u_tsk(&ad, ctp);
168 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
173 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
174 * @ptp: parent task pointer
176 * Returns 0 if access is OK, an error code otherwise
178 * Do the capability checks, and require read and write.
180 static int smack_ptrace_traceme(struct task_struct *ptp)
183 struct smk_audit_info ad;
186 rc = cap_ptrace_traceme(ptp);
190 tsp = smk_of_task(task_security(ptp));
191 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
192 smk_ad_setfield_u_tsk(&ad, ptp);
194 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
199 * smack_syslog - Smack approval on syslog
200 * @type: message type
202 * Require that the task has the floor label
204 * Returns 0 on success, error code otherwise.
206 static int smack_syslog(int type, bool from_file)
211 rc = cap_syslog(type, from_file);
215 if (capable(CAP_MAC_OVERRIDE))
218 sp = smk_of_current();
220 if (sp != smack_known_floor.smk_known)
232 * smack_sb_alloc_security - allocate a superblock blob
233 * @sb: the superblock getting the blob
235 * Returns 0 on success or -ENOMEM on error.
237 static int smack_sb_alloc_security(struct super_block *sb)
239 struct superblock_smack *sbsp;
241 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
246 sbsp->smk_root = smack_known_floor.smk_known;
247 sbsp->smk_default = smack_known_floor.smk_known;
248 sbsp->smk_floor = smack_known_floor.smk_known;
249 sbsp->smk_hat = smack_known_hat.smk_known;
250 sbsp->smk_initialized = 0;
251 spin_lock_init(&sbsp->smk_sblock);
253 sb->s_security = sbsp;
259 * smack_sb_free_security - free a superblock blob
260 * @sb: the superblock getting the blob
263 static void smack_sb_free_security(struct super_block *sb)
265 kfree(sb->s_security);
266 sb->s_security = NULL;
270 * smack_sb_copy_data - copy mount options data for processing
271 * @orig: where to start
272 * @smackopts: mount options string
274 * Returns 0 on success or -ENOMEM on error.
276 * Copy the Smack specific mount options out of the mount
279 static int smack_sb_copy_data(char *orig, char *smackopts)
281 char *cp, *commap, *otheropts, *dp;
283 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
284 if (otheropts == NULL)
287 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
288 if (strstr(cp, SMK_FSDEFAULT) == cp)
290 else if (strstr(cp, SMK_FSFLOOR) == cp)
292 else if (strstr(cp, SMK_FSHAT) == cp)
294 else if (strstr(cp, SMK_FSROOT) == cp)
299 commap = strchr(cp, ',');
308 strcpy(orig, otheropts);
309 free_page((unsigned long)otheropts);
315 * smack_sb_kern_mount - Smack specific mount processing
316 * @sb: the file system superblock
317 * @flags: the mount flags
318 * @data: the smack mount options
320 * Returns 0 on success, an error code on failure
322 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
324 struct dentry *root = sb->s_root;
325 struct inode *inode = root->d_inode;
326 struct superblock_smack *sp = sb->s_security;
327 struct inode_smack *isp;
332 spin_lock(&sp->smk_sblock);
333 if (sp->smk_initialized != 0) {
334 spin_unlock(&sp->smk_sblock);
337 sp->smk_initialized = 1;
338 spin_unlock(&sp->smk_sblock);
340 for (op = data; op != NULL; op = commap) {
341 commap = strchr(op, ',');
345 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
346 op += strlen(SMK_FSHAT);
347 nsp = smk_import(op, 0);
350 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
351 op += strlen(SMK_FSFLOOR);
352 nsp = smk_import(op, 0);
355 } else if (strncmp(op, SMK_FSDEFAULT,
356 strlen(SMK_FSDEFAULT)) == 0) {
357 op += strlen(SMK_FSDEFAULT);
358 nsp = smk_import(op, 0);
360 sp->smk_default = nsp;
361 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
362 op += strlen(SMK_FSROOT);
363 nsp = smk_import(op, 0);
370 * Initialize the root inode.
372 isp = inode->i_security;
374 inode->i_security = new_inode_smack(sp->smk_root);
376 isp->smk_inode = sp->smk_root;
382 * smack_sb_statfs - Smack check on statfs
383 * @dentry: identifies the file system in question
385 * Returns 0 if current can read the floor of the filesystem,
386 * and error code otherwise
388 static int smack_sb_statfs(struct dentry *dentry)
390 struct superblock_smack *sbp = dentry->d_sb->s_security;
392 struct smk_audit_info ad;
394 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
395 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
397 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
402 * smack_sb_mount - Smack check for mounting
409 * Returns 0 if current can write the floor of the filesystem
410 * being mounted on, an error code otherwise.
412 static int smack_sb_mount(char *dev_name, struct path *path,
413 char *type, unsigned long flags, void *data)
415 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
416 struct smk_audit_info ad;
418 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
419 smk_ad_setfield_u_fs_path(&ad, *path);
421 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
425 * smack_sb_umount - Smack check for unmounting
426 * @mnt: file system to unmount
429 * Returns 0 if current can write the floor of the filesystem
430 * being unmounted, an error code otherwise.
432 static int smack_sb_umount(struct vfsmount *mnt, int flags)
434 struct superblock_smack *sbp;
435 struct smk_audit_info ad;
440 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
441 smk_ad_setfield_u_fs_path_dentry(&ad, mnt->mnt_root);
442 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
444 sbp = mnt->mnt_sb->s_security;
445 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
453 * smack_bprm_set_creds - set creds for exec
454 * @bprm: the exec information
456 * Returns 0 if it gets a blob, -ENOMEM otherwise
458 static int smack_bprm_set_creds(struct linux_binprm *bprm)
460 struct inode *inode = bprm->file->f_path.dentry->d_inode;
461 struct task_smack *bsp = bprm->cred->security;
462 struct inode_smack *isp;
465 rc = cap_bprm_set_creds(bprm);
469 if (bprm->cred_prepared)
472 isp = inode->i_security;
473 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
479 bsp->smk_task = isp->smk_task;
480 bprm->per_clear |= PER_CLEAR_ON_SETID;
486 * smack_bprm_committing_creds - Prepare to install the new credentials
489 * @bprm: binprm for exec
491 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
493 struct task_smack *bsp = bprm->cred->security;
495 if (bsp->smk_task != bsp->smk_forked)
496 current->pdeath_signal = 0;
500 * smack_bprm_secureexec - Return the decision to use secureexec.
501 * @bprm: binprm for exec
503 * Returns 0 on success.
505 static int smack_bprm_secureexec(struct linux_binprm *bprm)
507 struct task_smack *tsp = current_security();
508 int ret = cap_bprm_secureexec(bprm);
510 if (!ret && (tsp->smk_task != tsp->smk_forked))
521 * smack_inode_alloc_security - allocate an inode blob
522 * @inode: the inode in need of a blob
524 * Returns 0 if it gets a blob, -ENOMEM otherwise
526 static int smack_inode_alloc_security(struct inode *inode)
528 inode->i_security = new_inode_smack(smk_of_current());
529 if (inode->i_security == NULL)
535 * smack_inode_free_security - free an inode blob
536 * @inode: the inode with a blob
538 * Clears the blob pointer in inode
540 static void smack_inode_free_security(struct inode *inode)
542 kfree(inode->i_security);
543 inode->i_security = NULL;
547 * smack_inode_init_security - copy out the smack from an inode
551 * @name: where to put the attribute name
552 * @value: where to put the attribute value
553 * @len: where to put the length of the attribute
555 * Returns 0 if it all works out, -ENOMEM if there's no memory
557 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
558 char **name, void **value, size_t *len)
561 struct smack_known *skp;
562 struct inode_smack *issp = inode->i_security;
563 char *csp = smk_of_current();
564 char *isp = smk_of_inode(inode);
565 char *dsp = smk_of_inode(dir);
570 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
576 skp = smk_find_entry(csp);
578 may = smk_access_entry(csp, dsp, &skp->smk_rules);
582 * If the access rule allows transmutation and
583 * the directory requests transmutation then
584 * by all means transmute.
585 * Mark the inode as changed.
587 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
588 smk_inode_transmutable(dir)) {
590 issp->smk_flags |= SMK_INODE_CHANGED;
593 *value = kstrdup(isp, GFP_KERNEL);
599 *len = strlen(isp) + 1;
605 * smack_inode_link - Smack check on link
606 * @old_dentry: the existing object
608 * @new_dentry: the new object
610 * Returns 0 if access is permitted, an error code otherwise
612 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
613 struct dentry *new_dentry)
616 struct smk_audit_info ad;
619 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
620 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
622 isp = smk_of_inode(old_dentry->d_inode);
623 rc = smk_curacc(isp, MAY_WRITE, &ad);
625 if (rc == 0 && new_dentry->d_inode != NULL) {
626 isp = smk_of_inode(new_dentry->d_inode);
627 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
628 rc = smk_curacc(isp, MAY_WRITE, &ad);
635 * smack_inode_unlink - Smack check on inode deletion
636 * @dir: containing directory object
637 * @dentry: file to unlink
639 * Returns 0 if current can write the containing directory
640 * and the object, error code otherwise
642 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
644 struct inode *ip = dentry->d_inode;
645 struct smk_audit_info ad;
648 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
649 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
652 * You need write access to the thing you're unlinking
654 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
657 * You also need write access to the containing directory
659 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
660 smk_ad_setfield_u_fs_inode(&ad, dir);
661 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
667 * smack_inode_rmdir - Smack check on directory deletion
668 * @dir: containing directory object
669 * @dentry: directory to unlink
671 * Returns 0 if current can write the containing directory
672 * and the directory, error code otherwise
674 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
676 struct smk_audit_info ad;
679 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
680 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
683 * You need write access to the thing you're removing
685 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
688 * You also need write access to the containing directory
690 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
691 smk_ad_setfield_u_fs_inode(&ad, dir);
692 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
699 * smack_inode_rename - Smack check on rename
700 * @old_inode: the old directory
701 * @old_dentry: unused
702 * @new_inode: the new directory
703 * @new_dentry: unused
705 * Read and write access is required on both the old and
708 * Returns 0 if access is permitted, an error code otherwise
710 static int smack_inode_rename(struct inode *old_inode,
711 struct dentry *old_dentry,
712 struct inode *new_inode,
713 struct dentry *new_dentry)
717 struct smk_audit_info ad;
719 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
720 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
722 isp = smk_of_inode(old_dentry->d_inode);
723 rc = smk_curacc(isp, MAY_READWRITE, &ad);
725 if (rc == 0 && new_dentry->d_inode != NULL) {
726 isp = smk_of_inode(new_dentry->d_inode);
727 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
728 rc = smk_curacc(isp, MAY_READWRITE, &ad);
734 * smack_inode_permission - Smack version of permission()
735 * @inode: the inode in question
736 * @mask: the access requested
738 * This is the important Smack hook.
740 * Returns 0 if access is permitted, -EACCES otherwise
742 static int smack_inode_permission(struct inode *inode, int mask)
744 struct smk_audit_info ad;
746 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
748 * No permission to check. Existence test. Yup, it's there.
753 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
754 smk_ad_setfield_u_fs_inode(&ad, inode);
755 return smk_curacc(smk_of_inode(inode), mask, &ad);
759 * smack_inode_setattr - Smack check for setting attributes
760 * @dentry: the object
761 * @iattr: for the force flag
763 * Returns 0 if access is permitted, an error code otherwise
765 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
767 struct smk_audit_info ad;
769 * Need to allow for clearing the setuid bit.
771 if (iattr->ia_valid & ATTR_FORCE)
773 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
774 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
776 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
780 * smack_inode_getattr - Smack check for getting attributes
782 * @dentry: the object
784 * Returns 0 if access is permitted, an error code otherwise
786 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
788 struct smk_audit_info ad;
790 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
791 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
792 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
793 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
797 * smack_inode_setxattr - Smack check for setting xattrs
798 * @dentry: the object
799 * @name: name of the attribute
804 * This protects the Smack attribute explicitly.
806 * Returns 0 if access is permitted, an error code otherwise
808 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
809 const void *value, size_t size, int flags)
811 struct smk_audit_info ad;
814 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
815 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
816 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
817 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
818 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
819 if (!capable(CAP_MAC_ADMIN))
822 * check label validity here so import wont fail on
825 if (size == 0 || size >= SMK_LABELLEN ||
826 smk_import(value, size) == NULL)
828 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
829 if (!capable(CAP_MAC_ADMIN))
831 if (size != TRANS_TRUE_SIZE ||
832 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
835 rc = cap_inode_setxattr(dentry, name, value, size, flags);
837 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
838 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
841 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
847 * smack_inode_post_setxattr - Apply the Smack update approved above
849 * @name: attribute name
850 * @value: attribute value
851 * @size: attribute size
854 * Set the pointer in the inode blob to the entry found
855 * in the master label list.
857 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
858 const void *value, size_t size, int flags)
861 struct inode_smack *isp = dentry->d_inode->i_security;
863 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
864 nsp = smk_import(value, size);
866 isp->smk_inode = nsp;
868 isp->smk_inode = smack_known_invalid.smk_known;
869 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
870 nsp = smk_import(value, size);
874 isp->smk_task = smack_known_invalid.smk_known;
875 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
876 nsp = smk_import(value, size);
880 isp->smk_mmap = smack_known_invalid.smk_known;
881 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
882 isp->smk_flags |= SMK_INODE_TRANSMUTE;
888 * smack_inode_getxattr - Smack check on getxattr
889 * @dentry: the object
892 * Returns 0 if access is permitted, an error code otherwise
894 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
896 struct smk_audit_info ad;
898 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
899 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
901 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
905 * smack_inode_removexattr - Smack check on removexattr
906 * @dentry: the object
907 * @name: name of the attribute
909 * Removing the Smack attribute requires CAP_MAC_ADMIN
911 * Returns 0 if access is permitted, an error code otherwise
913 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
915 struct inode_smack *isp;
916 struct smk_audit_info ad;
919 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
920 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
921 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
922 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
923 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
924 strcmp(name, XATTR_NAME_SMACKMMAP)) {
925 if (!capable(CAP_MAC_ADMIN))
928 rc = cap_inode_removexattr(dentry, name);
930 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
931 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
933 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
936 isp = dentry->d_inode->i_security;
937 isp->smk_task = NULL;
938 isp->smk_mmap = NULL;
945 * smack_inode_getsecurity - get smack xattrs
947 * @name: attribute name
948 * @buffer: where to put the result
951 * Returns the size of the attribute or an error code
953 static int smack_inode_getsecurity(const struct inode *inode,
954 const char *name, void **buffer,
957 struct socket_smack *ssp;
959 struct super_block *sbp;
960 struct inode *ip = (struct inode *)inode;
965 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
966 isp = smk_of_inode(inode);
967 ilen = strlen(isp) + 1;
973 * The rest of the Smack xattrs are only on sockets.
976 if (sbp->s_magic != SOCKFS_MAGIC)
980 if (sock == NULL || sock->sk == NULL)
983 ssp = sock->sk->sk_security;
985 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
987 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
992 ilen = strlen(isp) + 1;
1003 * smack_inode_listsecurity - list the Smack attributes
1004 * @inode: the object
1005 * @buffer: where they go
1006 * @buffer_size: size of buffer
1008 * Returns 0 on success, -EINVAL otherwise
1010 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1013 int len = strlen(XATTR_NAME_SMACK);
1015 if (buffer != NULL && len <= buffer_size) {
1016 memcpy(buffer, XATTR_NAME_SMACK, len);
1023 * smack_inode_getsecid - Extract inode's security id
1024 * @inode: inode to extract the info from
1025 * @secid: where result will be saved
1027 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1029 struct inode_smack *isp = inode->i_security;
1031 *secid = smack_to_secid(isp->smk_inode);
1039 * smack_file_permission - Smack check on file operations
1045 * Should access checks be done on each read or write?
1046 * UNICOS and SELinux say yes.
1047 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1049 * I'll say no for now. Smack does not do the frequent
1050 * label changing that SELinux does.
1052 static int smack_file_permission(struct file *file, int mask)
1058 * smack_file_alloc_security - assign a file security blob
1061 * The security blob for a file is a pointer to the master
1062 * label list, so no allocation is done.
1066 static int smack_file_alloc_security(struct file *file)
1068 file->f_security = smk_of_current();
1073 * smack_file_free_security - clear a file security blob
1076 * The security blob for a file is a pointer to the master
1077 * label list, so no memory is freed.
1079 static void smack_file_free_security(struct file *file)
1081 file->f_security = NULL;
1085 * smack_file_ioctl - Smack check on ioctls
1090 * Relies heavily on the correct use of the ioctl command conventions.
1092 * Returns 0 if allowed, error code otherwise
1094 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1098 struct smk_audit_info ad;
1100 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1101 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1103 if (_IOC_DIR(cmd) & _IOC_WRITE)
1104 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1106 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1107 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1113 * smack_file_lock - Smack check on file locking
1117 * Returns 0 if current has write access, error code otherwise
1119 static int smack_file_lock(struct file *file, unsigned int cmd)
1121 struct smk_audit_info ad;
1123 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1124 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
1125 return smk_curacc(file->f_security, MAY_WRITE, &ad);
1129 * smack_file_fcntl - Smack check on fcntl
1131 * @cmd: what action to check
1134 * Generally these operations are harmless.
1135 * File locking operations present an obvious mechanism
1136 * for passing information, so they require write access.
1138 * Returns 0 if current has access, error code otherwise
1140 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1143 struct smk_audit_info ad;
1153 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1154 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1155 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1166 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1167 * if mapping anonymous memory.
1168 * @file contains the file structure for file to map (may be NULL).
1169 * @reqprot contains the protection requested by the application.
1170 * @prot contains the protection that will be applied by the kernel.
1171 * @flags contains the operational flags.
1172 * Return 0 if permission is granted.
1174 static int smack_file_mmap(struct file *file,
1175 unsigned long reqprot, unsigned long prot,
1176 unsigned long flags, unsigned long addr,
1177 unsigned long addr_only)
1179 struct smack_known *skp;
1180 struct smack_rule *srp;
1181 struct task_smack *tsp;
1185 struct inode_smack *isp;
1192 /* do DAC check on address space usage */
1193 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1194 if (rc || addr_only)
1197 if (file == NULL || file->f_dentry == NULL)
1200 dp = file->f_dentry;
1202 if (dp->d_inode == NULL)
1205 isp = dp->d_inode->i_security;
1206 if (isp->smk_mmap == NULL)
1208 msmack = isp->smk_mmap;
1210 tsp = current_security();
1211 sp = smk_of_current();
1212 skp = smk_find_entry(sp);
1217 * For each Smack rule associated with the subject
1218 * label verify that the SMACK64MMAP also has access
1219 * to that rule's object label.
1221 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1222 osmack = srp->smk_object;
1224 * Matching labels always allows access.
1226 if (msmack == osmack)
1229 * If there is a matching local rule take
1230 * that into account as well.
1232 may = smk_access_entry(srp->smk_subject, osmack,
1235 may = srp->smk_access;
1237 may &= srp->smk_access;
1239 * If may is zero the SMACK64MMAP subject can't
1240 * possibly have less access.
1246 * Fetch the global list entry.
1247 * If there isn't one a SMACK64MMAP subject
1248 * can't have as much access as current.
1250 skp = smk_find_entry(msmack);
1251 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
1252 if (mmay == -ENOENT) {
1257 * If there is a local entry it modifies the
1258 * potential access, too.
1260 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1261 if (tmay != -ENOENT)
1265 * If there is any access available to current that is
1266 * not available to a SMACK64MMAP subject
1269 if ((may | mmay) != mmay) {
1281 * smack_file_set_fowner - set the file security blob value
1282 * @file: object in question
1285 * Further research may be required on this one.
1287 static int smack_file_set_fowner(struct file *file)
1289 file->f_security = smk_of_current();
1294 * smack_file_send_sigiotask - Smack on sigio
1295 * @tsk: The target task
1296 * @fown: the object the signal come from
1299 * Allow a privileged task to get signals even if it shouldn't
1301 * Returns 0 if a subject with the object's smack could
1302 * write to the task, an error code otherwise.
1304 static int smack_file_send_sigiotask(struct task_struct *tsk,
1305 struct fown_struct *fown, int signum)
1309 char *tsp = smk_of_task(tsk->cred->security);
1310 struct smk_audit_info ad;
1313 * struct fown_struct is never outside the context of a struct file
1315 file = container_of(fown, struct file, f_owner);
1317 /* we don't log here as rc can be overriden */
1318 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
1319 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1322 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1323 smk_ad_setfield_u_tsk(&ad, tsk);
1324 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
1329 * smack_file_receive - Smack file receive check
1332 * Returns 0 if current has access, error code otherwise
1334 static int smack_file_receive(struct file *file)
1337 struct smk_audit_info ad;
1339 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1340 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1342 * This code relies on bitmasks.
1344 if (file->f_mode & FMODE_READ)
1346 if (file->f_mode & FMODE_WRITE)
1349 return smk_curacc(file->f_security, may, &ad);
1353 * smack_dentry_open - Smack dentry open processing
1357 * Set the security blob in the file structure.
1361 static int smack_dentry_open(struct file *file, const struct cred *cred)
1363 struct inode_smack *isp = file->f_path.dentry->d_inode->i_security;
1365 file->f_security = isp->smk_inode;
1375 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1376 * @new: the new credentials
1377 * @gfp: the atomicity of any memory allocations
1379 * Prepare a blank set of credentials for modification. This must allocate all
1380 * the memory the LSM module might require such that cred_transfer() can
1381 * complete without error.
1383 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1385 struct task_smack *tsp;
1387 tsp = new_task_smack(NULL, NULL, gfp);
1391 cred->security = tsp;
1398 * smack_cred_free - "free" task-level security credentials
1399 * @cred: the credentials in question
1402 static void smack_cred_free(struct cred *cred)
1404 struct task_smack *tsp = cred->security;
1405 struct smack_rule *rp;
1406 struct list_head *l;
1407 struct list_head *n;
1411 cred->security = NULL;
1413 list_for_each_safe(l, n, &tsp->smk_rules) {
1414 rp = list_entry(l, struct smack_rule, list);
1415 list_del(&rp->list);
1422 * smack_cred_prepare - prepare new set of credentials for modification
1423 * @new: the new credentials
1424 * @old: the original credentials
1425 * @gfp: the atomicity of any memory allocations
1427 * Prepare a new set of credentials for modification.
1429 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1432 struct task_smack *old_tsp = old->security;
1433 struct task_smack *new_tsp;
1436 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1437 if (new_tsp == NULL)
1440 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1444 new->security = new_tsp;
1449 * smack_cred_transfer - Transfer the old credentials to the new credentials
1450 * @new: the new credentials
1451 * @old: the original credentials
1453 * Fill in a set of blank credentials from another set of credentials.
1455 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1457 struct task_smack *old_tsp = old->security;
1458 struct task_smack *new_tsp = new->security;
1460 new_tsp->smk_task = old_tsp->smk_task;
1461 new_tsp->smk_forked = old_tsp->smk_task;
1462 mutex_init(&new_tsp->smk_rules_lock);
1463 INIT_LIST_HEAD(&new_tsp->smk_rules);
1465 /* cbs copy rule list */
1469 * smack_kernel_act_as - Set the subjective context in a set of credentials
1470 * @new: points to the set of credentials to be modified.
1471 * @secid: specifies the security ID to be set
1473 * Set the security data for a kernel service.
1475 static int smack_kernel_act_as(struct cred *new, u32 secid)
1477 struct task_smack *new_tsp = new->security;
1478 char *smack = smack_from_secid(secid);
1483 new_tsp->smk_task = smack;
1488 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1489 * @new: points to the set of credentials to be modified
1490 * @inode: points to the inode to use as a reference
1492 * Set the file creation context in a set of credentials to the same
1493 * as the objective context of the specified inode
1495 static int smack_kernel_create_files_as(struct cred *new,
1496 struct inode *inode)
1498 struct inode_smack *isp = inode->i_security;
1499 struct task_smack *tsp = new->security;
1501 tsp->smk_forked = isp->smk_inode;
1502 tsp->smk_task = isp->smk_inode;
1507 * smk_curacc_on_task - helper to log task related access
1508 * @p: the task object
1509 * @access: the access requested
1510 * @caller: name of the calling function for audit
1512 * Return 0 if access is permitted
1514 static int smk_curacc_on_task(struct task_struct *p, int access,
1517 struct smk_audit_info ad;
1519 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1520 smk_ad_setfield_u_tsk(&ad, p);
1521 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
1525 * smack_task_setpgid - Smack check on setting pgid
1526 * @p: the task object
1529 * Return 0 if write access is permitted
1531 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1533 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1537 * smack_task_getpgid - Smack access check for getpgid
1538 * @p: the object task
1540 * Returns 0 if current can read the object task, error code otherwise
1542 static int smack_task_getpgid(struct task_struct *p)
1544 return smk_curacc_on_task(p, MAY_READ, __func__);
1548 * smack_task_getsid - Smack access check for getsid
1549 * @p: the object task
1551 * Returns 0 if current can read the object task, error code otherwise
1553 static int smack_task_getsid(struct task_struct *p)
1555 return smk_curacc_on_task(p, MAY_READ, __func__);
1559 * smack_task_getsecid - get the secid of the task
1560 * @p: the object task
1561 * @secid: where to put the result
1563 * Sets the secid to contain a u32 version of the smack label.
1565 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1567 *secid = smack_to_secid(smk_of_task(task_security(p)));
1571 * smack_task_setnice - Smack check on setting nice
1572 * @p: the task object
1575 * Return 0 if write access is permitted
1577 static int smack_task_setnice(struct task_struct *p, int nice)
1581 rc = cap_task_setnice(p, nice);
1583 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1588 * smack_task_setioprio - Smack check on setting ioprio
1589 * @p: the task object
1592 * Return 0 if write access is permitted
1594 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1598 rc = cap_task_setioprio(p, ioprio);
1600 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1605 * smack_task_getioprio - Smack check on reading ioprio
1606 * @p: the task object
1608 * Return 0 if read access is permitted
1610 static int smack_task_getioprio(struct task_struct *p)
1612 return smk_curacc_on_task(p, MAY_READ, __func__);
1616 * smack_task_setscheduler - Smack check on setting scheduler
1617 * @p: the task object
1621 * Return 0 if read access is permitted
1623 static int smack_task_setscheduler(struct task_struct *p, int policy,
1624 struct sched_param *lp)
1628 rc = cap_task_setscheduler(p, policy, lp);
1630 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1635 * smack_task_getscheduler - Smack check on reading scheduler
1636 * @p: the task object
1638 * Return 0 if read access is permitted
1640 static int smack_task_getscheduler(struct task_struct *p)
1642 return smk_curacc_on_task(p, MAY_READ, __func__);
1646 * smack_task_movememory - Smack check on moving memory
1647 * @p: the task object
1649 * Return 0 if write access is permitted
1651 static int smack_task_movememory(struct task_struct *p)
1653 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1657 * smack_task_kill - Smack check on signal delivery
1658 * @p: the task object
1661 * @secid: identifies the smack to use in lieu of current's
1663 * Return 0 if write access is permitted
1665 * The secid behavior is an artifact of an SELinux hack
1666 * in the USB code. Someday it may go away.
1668 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1671 struct smk_audit_info ad;
1673 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1674 smk_ad_setfield_u_tsk(&ad, p);
1676 * Sending a signal requires that the sender
1677 * can write the receiver.
1680 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1683 * If the secid isn't 0 we're dealing with some USB IO
1684 * specific behavior. This is not clean. For one thing
1685 * we can't take privilege into account.
1687 return smk_access(smack_from_secid(secid),
1688 smk_of_task(task_security(p)), MAY_WRITE, &ad);
1692 * smack_task_wait - Smack access check for waiting
1693 * @p: task to wait for
1695 * Returns 0 if current can wait for p, error code otherwise
1697 static int smack_task_wait(struct task_struct *p)
1699 struct smk_audit_info ad;
1700 char *sp = smk_of_current();
1701 char *tsp = smk_of_forked(task_security(p));
1704 /* we don't log here, we can be overriden */
1705 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
1710 * Allow the operation to succeed if either task
1711 * has privilege to perform operations that might
1712 * account for the smack labels having gotten to
1713 * be different in the first place.
1715 * This breaks the strict subject/object access
1716 * control ideal, taking the object's privilege
1717 * state into account in the decision as well as
1720 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1722 /* we log only if we didn't get overriden */
1724 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1725 smk_ad_setfield_u_tsk(&ad, p);
1726 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
1731 * smack_task_to_inode - copy task smack into the inode blob
1732 * @p: task to copy from
1733 * @inode: inode to copy to
1735 * Sets the smack pointer in the inode security blob
1737 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1739 struct inode_smack *isp = inode->i_security;
1740 isp->smk_inode = smk_of_task(task_security(p));
1748 * smack_sk_alloc_security - Allocate a socket blob
1751 * @gfp_flags: memory allocation flags
1753 * Assign Smack pointers to current
1755 * Returns 0 on success, -ENOMEM is there's no memory
1757 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1759 char *csp = smk_of_current();
1760 struct socket_smack *ssp;
1762 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1768 ssp->smk_packet = NULL;
1770 sk->sk_security = ssp;
1776 * smack_sk_free_security - Free a socket blob
1779 * Clears the blob pointer
1781 static void smack_sk_free_security(struct sock *sk)
1783 kfree(sk->sk_security);
1787 * smack_host_label - check host based restrictions
1788 * @sip: the object end
1790 * looks for host based access restrictions
1792 * This version will only be appropriate for really small sets of single label
1793 * hosts. The caller is responsible for ensuring that the RCU read lock is
1794 * taken before calling this function.
1796 * Returns the label of the far end or NULL if it's not special.
1798 static char *smack_host_label(struct sockaddr_in *sip)
1800 struct smk_netlbladdr *snp;
1801 struct in_addr *siap = &sip->sin_addr;
1803 if (siap->s_addr == 0)
1806 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1808 * we break after finding the first match because
1809 * the list is sorted from longest to shortest mask
1810 * so we have found the most specific match
1812 if ((&snp->smk_host.sin_addr)->s_addr ==
1813 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1814 /* we have found the special CIPSO option */
1815 if (snp->smk_label == smack_cipso_option)
1817 return snp->smk_label;
1824 * smack_set_catset - convert a capset to netlabel mls categories
1825 * @catset: the Smack categories
1826 * @sap: where to put the netlabel categories
1828 * Allocates and fills attr.mls.cat
1830 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1841 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1842 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1843 sap->attr.mls.cat->startbit = 0;
1845 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1846 for (m = 0x80; m != 0; m >>= 1, cat++) {
1849 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1855 * smack_to_secattr - fill a secattr from a smack value
1856 * @smack: the smack value
1857 * @nlsp: where the result goes
1859 * Casey says that CIPSO is good enough for now.
1860 * It can be used to effect.
1861 * It can also be abused to effect when necessary.
1862 * Apologies to the TSIG group in general and GW in particular.
1864 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1866 struct smack_cipso cipso;
1869 nlsp->domain = smack;
1870 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1872 rc = smack_to_cipso(smack, &cipso);
1874 nlsp->attr.mls.lvl = cipso.smk_level;
1875 smack_set_catset(cipso.smk_catset, nlsp);
1877 nlsp->attr.mls.lvl = smack_cipso_direct;
1878 smack_set_catset(smack, nlsp);
1883 * smack_netlabel - Set the secattr on a socket
1885 * @labeled: socket label scheme
1887 * Convert the outbound smack value (smk_out) to a
1888 * secattr and attach it to the socket.
1890 * Returns 0 on success or an error code
1892 static int smack_netlabel(struct sock *sk, int labeled)
1894 struct socket_smack *ssp = sk->sk_security;
1895 struct netlbl_lsm_secattr secattr;
1899 * Usually the netlabel code will handle changing the
1900 * packet labeling based on the label.
1901 * The case of a single label host is different, because
1902 * a single label host should never get a labeled packet
1903 * even though the label is usually associated with a packet
1907 bh_lock_sock_nested(sk);
1909 if (ssp->smk_out == smack_net_ambient ||
1910 labeled == SMACK_UNLABELED_SOCKET)
1911 netlbl_sock_delattr(sk);
1913 netlbl_secattr_init(&secattr);
1914 smack_to_secattr(ssp->smk_out, &secattr);
1915 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
1916 netlbl_secattr_destroy(&secattr);
1926 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1928 * @sap: the destination address
1930 * Set the correct secattr for the given socket based on the destination
1931 * address and perform any outbound access checks needed.
1933 * Returns 0 on success or an error code.
1936 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1941 struct socket_smack *ssp = sk->sk_security;
1942 struct smk_audit_info ad;
1945 hostsp = smack_host_label(sap);
1946 if (hostsp != NULL) {
1947 sk_lbl = SMACK_UNLABELED_SOCKET;
1949 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1950 ad.a.u.net.family = sap->sin_family;
1951 ad.a.u.net.dport = sap->sin_port;
1952 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1954 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
1956 sk_lbl = SMACK_CIPSO_SOCKET;
1963 return smack_netlabel(sk, sk_lbl);
1967 * smack_inode_setsecurity - set smack xattrs
1968 * @inode: the object
1969 * @name: attribute name
1970 * @value: attribute value
1971 * @size: size of the attribute
1974 * Sets the named attribute in the appropriate blob
1976 * Returns 0 on success, or an error code
1978 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1979 const void *value, size_t size, int flags)
1982 struct inode_smack *nsp = inode->i_security;
1983 struct socket_smack *ssp;
1984 struct socket *sock;
1987 if (value == NULL || size > SMK_LABELLEN || size == 0)
1990 sp = smk_import(value, size);
1994 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1995 nsp->smk_inode = sp;
1996 nsp->smk_flags |= SMK_INODE_INSTANT;
2000 * The rest of the Smack xattrs are only on sockets.
2002 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2005 sock = SOCKET_I(inode);
2006 if (sock == NULL || sock->sk == NULL)
2009 ssp = sock->sk->sk_security;
2011 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2013 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2015 if (sock->sk->sk_family != PF_UNIX) {
2016 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2019 "Smack: \"%s\" netlbl error %d.\n",
2029 * smack_socket_post_create - finish socket setup
2031 * @family: protocol family
2036 * Sets the netlabel information on the socket
2038 * Returns 0 on success, and error code otherwise
2040 static int smack_socket_post_create(struct socket *sock, int family,
2041 int type, int protocol, int kern)
2043 if (family != PF_INET || sock->sk == NULL)
2046 * Set the outbound netlbl.
2048 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2052 * smack_socket_connect - connect access check
2054 * @sap: the other end
2055 * @addrlen: size of sap
2057 * Verifies that a connection may be possible
2059 * Returns 0 on success, and error code otherwise
2061 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2064 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
2066 if (addrlen < sizeof(struct sockaddr_in))
2069 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2073 * smack_flags_to_may - convert S_ to MAY_ values
2074 * @flags: the S_ value
2076 * Returns the equivalent MAY_ value
2078 static int smack_flags_to_may(int flags)
2082 if (flags & S_IRUGO)
2084 if (flags & S_IWUGO)
2086 if (flags & S_IXUGO)
2093 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2098 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2100 msg->security = smk_of_current();
2105 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2108 * Clears the blob pointer
2110 static void smack_msg_msg_free_security(struct msg_msg *msg)
2112 msg->security = NULL;
2116 * smack_of_shm - the smack pointer for the shm
2119 * Returns a pointer to the smack value
2121 static char *smack_of_shm(struct shmid_kernel *shp)
2123 return (char *)shp->shm_perm.security;
2127 * smack_shm_alloc_security - Set the security blob for shm
2132 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2134 struct kern_ipc_perm *isp = &shp->shm_perm;
2136 isp->security = smk_of_current();
2141 * smack_shm_free_security - Clear the security blob for shm
2144 * Clears the blob pointer
2146 static void smack_shm_free_security(struct shmid_kernel *shp)
2148 struct kern_ipc_perm *isp = &shp->shm_perm;
2150 isp->security = NULL;
2154 * smk_curacc_shm : check if current has access on shm
2156 * @access : access requested
2158 * Returns 0 if current has the requested access, error code otherwise
2160 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2162 char *ssp = smack_of_shm(shp);
2163 struct smk_audit_info ad;
2166 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2167 ad.a.u.ipc_id = shp->shm_perm.id;
2169 return smk_curacc(ssp, access, &ad);
2173 * smack_shm_associate - Smack access check for shm
2175 * @shmflg: access requested
2177 * Returns 0 if current has the requested access, error code otherwise
2179 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2183 may = smack_flags_to_may(shmflg);
2184 return smk_curacc_shm(shp, may);
2188 * smack_shm_shmctl - Smack access check for shm
2190 * @cmd: what it wants to do
2192 * Returns 0 if current has the requested access, error code otherwise
2194 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2207 may = MAY_READWRITE;
2212 * System level information.
2218 return smk_curacc_shm(shp, may);
2222 * smack_shm_shmat - Smack access for shmat
2225 * @shmflg: access requested
2227 * Returns 0 if current has the requested access, error code otherwise
2229 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2234 may = smack_flags_to_may(shmflg);
2235 return smk_curacc_shm(shp, may);
2239 * smack_of_sem - the smack pointer for the sem
2242 * Returns a pointer to the smack value
2244 static char *smack_of_sem(struct sem_array *sma)
2246 return (char *)sma->sem_perm.security;
2250 * smack_sem_alloc_security - Set the security blob for sem
2255 static int smack_sem_alloc_security(struct sem_array *sma)
2257 struct kern_ipc_perm *isp = &sma->sem_perm;
2259 isp->security = smk_of_current();
2264 * smack_sem_free_security - Clear the security blob for sem
2267 * Clears the blob pointer
2269 static void smack_sem_free_security(struct sem_array *sma)
2271 struct kern_ipc_perm *isp = &sma->sem_perm;
2273 isp->security = NULL;
2277 * smk_curacc_sem : check if current has access on sem
2279 * @access : access requested
2281 * Returns 0 if current has the requested access, error code otherwise
2283 static int smk_curacc_sem(struct sem_array *sma, int access)
2285 char *ssp = smack_of_sem(sma);
2286 struct smk_audit_info ad;
2289 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2290 ad.a.u.ipc_id = sma->sem_perm.id;
2292 return smk_curacc(ssp, access, &ad);
2296 * smack_sem_associate - Smack access check for sem
2298 * @semflg: access requested
2300 * Returns 0 if current has the requested access, error code otherwise
2302 static int smack_sem_associate(struct sem_array *sma, int semflg)
2306 may = smack_flags_to_may(semflg);
2307 return smk_curacc_sem(sma, may);
2311 * smack_sem_shmctl - Smack access check for sem
2313 * @cmd: what it wants to do
2315 * Returns 0 if current has the requested access, error code otherwise
2317 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2335 may = MAY_READWRITE;
2340 * System level information
2347 return smk_curacc_sem(sma, may);
2351 * smack_sem_semop - Smack checks of semaphore operations
2357 * Treated as read and write in all cases.
2359 * Returns 0 if access is allowed, error code otherwise
2361 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2362 unsigned nsops, int alter)
2364 return smk_curacc_sem(sma, MAY_READWRITE);
2368 * smack_msg_alloc_security - Set the security blob for msg
2373 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2375 struct kern_ipc_perm *kisp = &msq->q_perm;
2377 kisp->security = smk_of_current();
2382 * smack_msg_free_security - Clear the security blob for msg
2385 * Clears the blob pointer
2387 static void smack_msg_queue_free_security(struct msg_queue *msq)
2389 struct kern_ipc_perm *kisp = &msq->q_perm;
2391 kisp->security = NULL;
2395 * smack_of_msq - the smack pointer for the msq
2398 * Returns a pointer to the smack value
2400 static char *smack_of_msq(struct msg_queue *msq)
2402 return (char *)msq->q_perm.security;
2406 * smk_curacc_msq : helper to check if current has access on msq
2408 * @access : access requested
2410 * return 0 if current has access, error otherwise
2412 static int smk_curacc_msq(struct msg_queue *msq, int access)
2414 char *msp = smack_of_msq(msq);
2415 struct smk_audit_info ad;
2418 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2419 ad.a.u.ipc_id = msq->q_perm.id;
2421 return smk_curacc(msp, access, &ad);
2425 * smack_msg_queue_associate - Smack access check for msg_queue
2427 * @msqflg: access requested
2429 * Returns 0 if current has the requested access, error code otherwise
2431 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2435 may = smack_flags_to_may(msqflg);
2436 return smk_curacc_msq(msq, may);
2440 * smack_msg_queue_msgctl - Smack access check for msg_queue
2442 * @cmd: what it wants to do
2444 * Returns 0 if current has the requested access, error code otherwise
2446 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2457 may = MAY_READWRITE;
2462 * System level information
2469 return smk_curacc_msq(msq, may);
2473 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2476 * @msqflg: access requested
2478 * Returns 0 if current has the requested access, error code otherwise
2480 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2485 may = smack_flags_to_may(msqflg);
2486 return smk_curacc_msq(msq, may);
2490 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2497 * Returns 0 if current has read and write access, error code otherwise
2499 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2500 struct task_struct *target, long type, int mode)
2502 return smk_curacc_msq(msq, MAY_READWRITE);
2506 * smack_ipc_permission - Smack access for ipc_permission()
2507 * @ipp: the object permissions
2508 * @flag: access requested
2510 * Returns 0 if current has read and write access, error code otherwise
2512 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2514 char *isp = ipp->security;
2515 int may = smack_flags_to_may(flag);
2516 struct smk_audit_info ad;
2519 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2520 ad.a.u.ipc_id = ipp->id;
2522 return smk_curacc(isp, may, &ad);
2526 * smack_ipc_getsecid - Extract smack security id
2527 * @ipp: the object permissions
2528 * @secid: where result will be saved
2530 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2532 char *smack = ipp->security;
2534 *secid = smack_to_secid(smack);
2538 * smack_d_instantiate - Make sure the blob is correct on an inode
2539 * @opt_dentry: dentry where inode will be attached
2540 * @inode: the object
2542 * Set the inode's security blob if it hasn't been done already.
2544 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2546 struct super_block *sbp;
2547 struct superblock_smack *sbsp;
2548 struct inode_smack *isp;
2549 char *csp = smk_of_current();
2552 char trattr[TRANS_TRUE_SIZE];
2560 isp = inode->i_security;
2562 mutex_lock(&isp->smk_lock);
2564 * If the inode is already instantiated
2565 * take the quick way out
2567 if (isp->smk_flags & SMK_INODE_INSTANT)
2571 sbsp = sbp->s_security;
2573 * We're going to use the superblock default label
2574 * if there's no label on the file.
2576 final = sbsp->smk_default;
2579 * If this is the root inode the superblock
2580 * may be in the process of initialization.
2581 * If that is the case use the root value out
2582 * of the superblock.
2584 if (opt_dentry->d_parent == opt_dentry) {
2585 isp->smk_inode = sbsp->smk_root;
2586 isp->smk_flags |= SMK_INODE_INSTANT;
2591 * This is pretty hackish.
2592 * Casey says that we shouldn't have to do
2593 * file system specific code, but it does help
2594 * with keeping it simple.
2596 switch (sbp->s_magic) {
2599 * Casey says that it's a little embarrassing
2600 * that the smack file system doesn't do
2601 * extended attributes.
2603 final = smack_known_star.smk_known;
2607 * Casey says pipes are easy (?)
2609 final = smack_known_star.smk_known;
2611 case DEVPTS_SUPER_MAGIC:
2613 * devpts seems content with the label of the task.
2614 * Programs that change smack have to treat the
2621 * Socket access is controlled by the socket
2622 * structures associated with the task involved.
2624 final = smack_known_star.smk_known;
2626 case PROC_SUPER_MAGIC:
2628 * Casey says procfs appears not to care.
2629 * The superblock default suffices.
2634 * Device labels should come from the filesystem,
2635 * but watch out, because they're volitile,
2636 * getting recreated on every reboot.
2638 final = smack_known_star.smk_known;
2642 * If a smack value has been set we want to use it,
2643 * but since tmpfs isn't giving us the opportunity
2644 * to set mount options simulate setting the
2645 * superblock default.
2649 * This isn't an understood special case.
2650 * Get the value from the xattr.
2654 * UNIX domain sockets use lower level socket data.
2656 if (S_ISSOCK(inode->i_mode)) {
2657 final = smack_known_star.smk_known;
2661 * No xattr support means, alas, no SMACK label.
2662 * Use the aforeapplied default.
2663 * It would be curious if the label of the task
2664 * does not match that assigned.
2666 if (inode->i_op->getxattr == NULL)
2669 * Get the dentry for xattr.
2671 dp = dget(opt_dentry);
2672 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2673 if (fetched != NULL)
2677 * Transmuting directory
2679 if (S_ISDIR(inode->i_mode)) {
2681 * If there is a transmute attribute on the
2682 * directory mark the inode.
2684 rc = inode->i_op->getxattr(dp,
2685 XATTR_NAME_SMACKTRANSMUTE, trattr,
2688 strncmp(trattr, TRANS_TRUE, TRANS_TRUE_SIZE) == 0)
2689 transflag = SMK_INODE_TRANSMUTE;
2691 * If this is a new directory and the label was
2692 * transmuted when the inode was initialized
2693 * set the transmute attribute on the directory
2694 * and mark the inode.
2696 if (isp->smk_flags & SMK_INODE_CHANGED) {
2697 isp->smk_flags &= ~SMK_INODE_CHANGED;
2698 rc = inode->i_op->setxattr(dp,
2699 XATTR_NAME_SMACKTRANSMUTE,
2700 TRANS_TRUE, TRANS_TRUE_SIZE,
2703 transflag = SMK_INODE_TRANSMUTE;
2706 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2707 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2714 isp->smk_inode = csp;
2716 isp->smk_inode = final;
2718 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2721 mutex_unlock(&isp->smk_lock);
2726 * smack_getprocattr - Smack process attribute access
2727 * @p: the object task
2728 * @name: the name of the attribute in /proc/.../attr
2729 * @value: where to put the result
2731 * Places a copy of the task Smack into value
2733 * Returns the length of the smack label or an error code
2735 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2740 if (strcmp(name, "current") != 0)
2743 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
2753 * smack_setprocattr - Smack process attribute setting
2754 * @p: the object task
2755 * @name: the name of the attribute in /proc/.../attr
2756 * @value: the value to set
2757 * @size: the size of the value
2759 * Sets the Smack value of the task. Only setting self
2760 * is permitted and only with privilege
2762 * Returns the length of the smack label or an error code
2764 static int smack_setprocattr(struct task_struct *p, char *name,
2765 void *value, size_t size)
2768 struct task_smack *tsp;
2769 struct task_smack *oldtsp;
2774 * Changing another process' Smack value is too dangerous
2775 * and supports no sane use case.
2780 if (!capable(CAP_MAC_ADMIN))
2783 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2786 if (strcmp(name, "current") != 0)
2789 newsmack = smk_import(value, size);
2790 if (newsmack == NULL)
2794 * No process is ever allowed the web ("@") label.
2796 if (newsmack == smack_known_web.smk_known)
2799 oldtsp = p->cred->security;
2800 new = prepare_creds();
2804 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
2809 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2813 new->security = tsp;
2819 * smack_unix_stream_connect - Smack access on UDS
2821 * @other: the other sock
2824 * Return 0 if a subject with the smack of sock could access
2825 * an object with the smack of other, otherwise an error code
2827 static int smack_unix_stream_connect(struct socket *sock,
2828 struct socket *other, struct sock *newsk)
2830 struct socket_smack *ssp = sock->sk->sk_security;
2831 struct socket_smack *osp = other->sk->sk_security;
2832 struct socket_smack *nsp = newsk->sk_security;
2833 struct smk_audit_info ad;
2836 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2837 smk_ad_setfield_u_net_sk(&ad, other->sk);
2839 if (!capable(CAP_MAC_OVERRIDE))
2840 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2843 * Cross reference the peer labels for SO_PEERSEC.
2846 nsp->smk_packet = ssp->smk_out;
2847 ssp->smk_packet = osp->smk_out;
2854 * smack_unix_may_send - Smack access on UDS
2856 * @other: the other socket
2858 * Return 0 if a subject with the smack of sock could access
2859 * an object with the smack of other, otherwise an error code
2861 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2863 struct socket_smack *ssp = sock->sk->sk_security;
2864 struct socket_smack *osp = other->sk->sk_security;
2865 struct smk_audit_info ad;
2868 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2869 smk_ad_setfield_u_net_sk(&ad, other->sk);
2871 if (!capable(CAP_MAC_OVERRIDE))
2872 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2878 * smack_socket_sendmsg - Smack check based on destination host
2881 * @size: the size of the message
2883 * Return 0 if the current subject can write to the destination
2884 * host. This is only a question if the destination is a single
2887 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2890 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2893 * Perfectly reasonable for this to be NULL
2895 if (sip == NULL || sip->sin_family != AF_INET)
2898 return smack_netlabel_send(sock->sk, sip);
2902 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2903 * @sap: netlabel secattr
2904 * @ssp: socket security information
2906 * Returns a pointer to a Smack label found on the label list.
2908 static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
2909 struct socket_smack *ssp)
2911 struct smack_known *skp;
2912 char smack[SMK_LABELLEN];
2916 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2918 * Looks like a CIPSO packet.
2919 * If there are flags but no level netlabel isn't
2920 * behaving the way we expect it to.
2922 * Get the categories, if any
2923 * Without guidance regarding the smack value
2924 * for the packet fall back on the network
2927 memset(smack, '\0', SMK_LABELLEN);
2928 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2930 pcat = netlbl_secattr_catmap_walk(
2931 sap->attr.mls.cat, pcat + 1);
2934 smack_catset_bit(pcat, smack);
2937 * If it is CIPSO using smack direct mapping
2938 * we are already done. WeeHee.
2940 if (sap->attr.mls.lvl == smack_cipso_direct) {
2942 * The label sent is usually on the label list.
2944 * If it is not we may still want to allow the
2947 * If the recipient is accepting all packets
2948 * because it is using the star ("*") label
2949 * for SMACK64IPIN provide the web ("@") label
2950 * so that a directed response will succeed.
2951 * This is not very correct from a MAC point
2952 * of view, but gets around the problem that
2953 * locking prevents adding the newly discovered
2954 * label to the list.
2955 * The case where the recipient is not using
2956 * the star label should obviously fail.
2957 * The easy way to do this is to provide the
2958 * star label as the subject label.
2960 skp = smk_find_entry(smack);
2962 return skp->smk_known;
2964 ssp->smk_in == smack_known_star.smk_known)
2965 return smack_known_web.smk_known;
2966 return smack_known_star.smk_known;
2969 * Look it up in the supplied table if it is not
2972 sp = smack_from_cipso(sap->attr.mls.lvl, smack);
2975 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
2976 return smack_known_web.smk_known;
2977 return smack_known_star.smk_known;
2979 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2981 * Looks like a fallback, which gives us a secid.
2983 sp = smack_from_secid(sap->attr.secid);
2985 * This has got to be a bug because it is
2986 * impossible to specify a fallback without
2987 * specifying the label, which will ensure
2988 * it has a secid, and the only way to get a
2989 * secid is from a fallback.
2995 * Without guidance regarding the smack value
2996 * for the packet fall back on the network
2999 return smack_net_ambient;
3003 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3007 * Returns 0 if the packet should be delivered, an error code otherwise
3009 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3011 struct netlbl_lsm_secattr secattr;
3012 struct socket_smack *ssp = sk->sk_security;
3015 struct smk_audit_info ad;
3016 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
3020 * Translate what netlabel gave us.
3022 netlbl_secattr_init(&secattr);
3024 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3026 csp = smack_from_secattr(&secattr, ssp);
3028 csp = smack_net_ambient;
3030 netlbl_secattr_destroy(&secattr);
3033 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3034 ad.a.u.net.family = sk->sk_family;
3035 ad.a.u.net.netif = skb->skb_iif;
3036 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3039 * Receiving a packet requires that the other end
3040 * be able to write here. Read access is not required.
3041 * This is the simplist possible security model
3044 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
3046 netlbl_skbuff_err(skb, rc, 0);
3051 * smack_socket_getpeersec_stream - pull in packet label
3053 * @optval: user's destination
3054 * @optlen: size thereof
3057 * returns zero on success, an error code otherwise
3059 static int smack_socket_getpeersec_stream(struct socket *sock,
3060 char __user *optval,
3061 int __user *optlen, unsigned len)
3063 struct socket_smack *ssp;
3068 ssp = sock->sk->sk_security;
3069 if (ssp->smk_packet != NULL) {
3070 rcp = ssp->smk_packet;
3071 slen = strlen(rcp) + 1;
3076 else if (copy_to_user(optval, rcp, slen) != 0)
3079 if (put_user(slen, optlen) != 0)
3087 * smack_socket_getpeersec_dgram - pull in packet label
3088 * @sock: the peer socket
3090 * @secid: pointer to where to put the secid of the packet
3092 * Sets the netlabel socket state on sk from parent
3094 static int smack_socket_getpeersec_dgram(struct socket *sock,
3095 struct sk_buff *skb, u32 *secid)
3098 struct netlbl_lsm_secattr secattr;
3099 struct socket_smack *ssp = NULL;
3101 int family = PF_UNSPEC;
3102 u32 s = 0; /* 0 is the invalid secid */
3106 if (skb->protocol == htons(ETH_P_IP))
3108 else if (skb->protocol == htons(ETH_P_IPV6))
3111 if (family == PF_UNSPEC && sock != NULL)
3112 family = sock->sk->sk_family;
3114 if (family == PF_UNIX) {
3115 ssp = sock->sk->sk_security;
3116 s = smack_to_secid(ssp->smk_out);
3117 } else if (family == PF_INET || family == PF_INET6) {
3119 * Translate what netlabel gave us.
3121 if (sock != NULL && sock->sk != NULL)
3122 ssp = sock->sk->sk_security;
3123 netlbl_secattr_init(&secattr);
3124 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3126 sp = smack_from_secattr(&secattr, ssp);
3127 s = smack_to_secid(sp);
3129 netlbl_secattr_destroy(&secattr);
3138 * smack_sock_graft - Initialize a newly created socket with an existing sock
3140 * @parent: parent socket
3142 * Set the smk_{in,out} state of an existing sock based on the process that
3143 * is creating the new socket.
3145 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3147 struct socket_smack *ssp;
3150 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3153 ssp = sk->sk_security;
3154 ssp->smk_in = ssp->smk_out = smk_of_current();
3155 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3159 * smack_inet_conn_request - Smack access check on connect
3160 * @sk: socket involved
3164 * Returns 0 if a task with the packet label could write to
3165 * the socket, otherwise an error code
3167 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3168 struct request_sock *req)
3170 u16 family = sk->sk_family;
3171 struct socket_smack *ssp = sk->sk_security;
3172 struct netlbl_lsm_secattr secattr;
3173 struct sockaddr_in addr;
3177 struct smk_audit_info ad;
3179 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3180 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3183 netlbl_secattr_init(&secattr);
3184 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3186 sp = smack_from_secattr(&secattr, ssp);
3188 sp = smack_known_huh.smk_known;
3189 netlbl_secattr_destroy(&secattr);
3192 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3193 ad.a.u.net.family = family;
3194 ad.a.u.net.netif = skb->skb_iif;
3195 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3198 * Receiving a packet requires that the other end be able to write
3199 * here. Read access is not required.
3201 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
3206 * Save the peer's label in the request_sock so we can later setup
3207 * smk_packet in the child socket so that SO_PEERCRED can report it.
3209 req->peer_secid = smack_to_secid(sp);
3212 * We need to decide if we want to label the incoming connection here
3213 * if we do we only need to label the request_sock and the stack will
3214 * propagate the wire-label to the sock when it is created.
3217 addr.sin_addr.s_addr = hdr->saddr;
3219 if (smack_host_label(&addr) == NULL) {
3221 netlbl_secattr_init(&secattr);
3222 smack_to_secattr(sp, &secattr);
3223 rc = netlbl_req_setattr(req, &secattr);
3224 netlbl_secattr_destroy(&secattr);
3227 netlbl_req_delattr(req);
3234 * smack_inet_csk_clone - Copy the connection information to the new socket
3235 * @sk: the new socket
3236 * @req: the connection's request_sock
3238 * Transfer the connection's peer label to the newly created socket.
3240 static void smack_inet_csk_clone(struct sock *sk,
3241 const struct request_sock *req)
3243 struct socket_smack *ssp = sk->sk_security;
3245 if (req->peer_secid != 0)
3246 ssp->smk_packet = smack_from_secid(req->peer_secid);
3248 ssp->smk_packet = NULL;
3252 * Key management security hooks
3254 * Casey has not tested key support very heavily.
3255 * The permission check is most likely too restrictive.
3256 * If you care about keys please have a look.
3261 * smack_key_alloc - Set the key security blob
3263 * @cred: the credentials to use
3266 * No allocation required
3270 static int smack_key_alloc(struct key *key, const struct cred *cred,
3271 unsigned long flags)
3273 key->security = smk_of_task(cred->security);
3278 * smack_key_free - Clear the key security blob
3281 * Clear the blob pointer
3283 static void smack_key_free(struct key *key)
3285 key->security = NULL;
3289 * smack_key_permission - Smack access on a key
3290 * @key_ref: gets to the object
3291 * @cred: the credentials to use
3294 * Return 0 if the task has read and write to the object,
3295 * an error code otherwise
3297 static int smack_key_permission(key_ref_t key_ref,
3298 const struct cred *cred, key_perm_t perm)
3301 struct smk_audit_info ad;
3302 char *tsp = smk_of_task(cred->security);
3304 keyp = key_ref_to_ptr(key_ref);
3308 * If the key hasn't been initialized give it access so that
3311 if (keyp->security == NULL)
3314 * This should not occur
3319 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3320 ad.a.u.key_struct.key = keyp->serial;
3321 ad.a.u.key_struct.key_desc = keyp->description;
3323 return smk_access(tsp, keyp->security,
3324 MAY_READWRITE, &ad);
3326 #endif /* CONFIG_KEYS */
3331 * Audit requires a unique representation of each Smack specific
3332 * rule. This unique representation is used to distinguish the
3333 * object to be audited from remaining kernel objects and also
3334 * works as a glue between the audit hooks.
3336 * Since repository entries are added but never deleted, we'll use
3337 * the smack_known label address related to the given audit rule as
3338 * the needed unique representation. This also better fits the smack
3339 * model where nearly everything is a label.
3344 * smack_audit_rule_init - Initialize a smack audit rule
3345 * @field: audit rule fields given from user-space (audit.h)
3346 * @op: required testing operator (=, !=, >, <, ...)
3347 * @rulestr: smack label to be audited
3348 * @vrule: pointer to save our own audit rule representation
3350 * Prepare to audit cases where (@field @op @rulestr) is true.
3351 * The label to be audited is created if necessay.
3353 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3355 char **rule = (char **)vrule;
3358 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3361 if (op != Audit_equal && op != Audit_not_equal)
3364 *rule = smk_import(rulestr, 0);
3370 * smack_audit_rule_known - Distinguish Smack audit rules
3371 * @krule: rule of interest, in Audit kernel representation format
3373 * This is used to filter Smack rules from remaining Audit ones.
3374 * If it's proved that this rule belongs to us, the
3375 * audit_rule_match hook will be called to do the final judgement.
3377 static int smack_audit_rule_known(struct audit_krule *krule)
3379 struct audit_field *f;
3382 for (i = 0; i < krule->field_count; i++) {
3383 f = &krule->fields[i];
3385 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3393 * smack_audit_rule_match - Audit given object ?
3394 * @secid: security id for identifying the object to test
3395 * @field: audit rule flags given from user-space
3396 * @op: required testing operator
3397 * @vrule: smack internal rule presentation
3398 * @actx: audit context associated with the check
3400 * The core Audit hook. It's used to take the decision of
3401 * whether to audit or not to audit a given object.
3403 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3404 struct audit_context *actx)
3410 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3411 "Smack: missing rule\n");
3415 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3418 smack = smack_from_secid(secid);
3421 * No need to do string comparisons. If a match occurs,
3422 * both pointers will point to the same smack_known
3425 if (op == Audit_equal)
3426 return (rule == smack);
3427 if (op == Audit_not_equal)
3428 return (rule != smack);
3434 * smack_audit_rule_free - free smack rule representation
3435 * @vrule: rule to be freed.
3437 * No memory was allocated.
3439 static void smack_audit_rule_free(void *vrule)
3444 #endif /* CONFIG_AUDIT */
3447 * smack_secid_to_secctx - return the smack label for a secid
3448 * @secid: incoming integer
3449 * @secdata: destination
3450 * @seclen: how long it is
3452 * Exists for networking code.
3454 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3456 char *sp = smack_from_secid(secid);
3460 *seclen = strlen(sp);
3465 * smack_secctx_to_secid - return the secid for a smack label
3466 * @secdata: smack label
3467 * @seclen: how long result is
3468 * @secid: outgoing integer
3470 * Exists for audit and networking code.
3472 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3474 *secid = smack_to_secid(secdata);
3479 * smack_release_secctx - don't do anything.
3483 * Exists to make sure nothing gets done, and properly
3485 static void smack_release_secctx(char *secdata, u32 seclen)
3489 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3491 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3494 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3496 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3499 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3502 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3510 struct security_operations smack_ops = {
3513 .ptrace_access_check = smack_ptrace_access_check,
3514 .ptrace_traceme = smack_ptrace_traceme,
3515 .syslog = smack_syslog,
3517 .sb_alloc_security = smack_sb_alloc_security,
3518 .sb_free_security = smack_sb_free_security,
3519 .sb_copy_data = smack_sb_copy_data,
3520 .sb_kern_mount = smack_sb_kern_mount,
3521 .sb_statfs = smack_sb_statfs,
3522 .sb_mount = smack_sb_mount,
3523 .sb_umount = smack_sb_umount,
3525 .bprm_set_creds = smack_bprm_set_creds,
3526 .bprm_committing_creds = smack_bprm_committing_creds,
3527 .bprm_secureexec = smack_bprm_secureexec,
3529 .inode_alloc_security = smack_inode_alloc_security,
3530 .inode_free_security = smack_inode_free_security,
3531 .inode_init_security = smack_inode_init_security,
3532 .inode_link = smack_inode_link,
3533 .inode_unlink = smack_inode_unlink,
3534 .inode_rmdir = smack_inode_rmdir,
3535 .inode_rename = smack_inode_rename,
3536 .inode_permission = smack_inode_permission,
3537 .inode_setattr = smack_inode_setattr,
3538 .inode_getattr = smack_inode_getattr,
3539 .inode_setxattr = smack_inode_setxattr,
3540 .inode_post_setxattr = smack_inode_post_setxattr,
3541 .inode_getxattr = smack_inode_getxattr,
3542 .inode_removexattr = smack_inode_removexattr,
3543 .inode_getsecurity = smack_inode_getsecurity,
3544 .inode_setsecurity = smack_inode_setsecurity,
3545 .inode_listsecurity = smack_inode_listsecurity,
3546 .inode_getsecid = smack_inode_getsecid,
3548 .file_permission = smack_file_permission,
3549 .file_alloc_security = smack_file_alloc_security,
3550 .file_free_security = smack_file_free_security,
3551 .file_ioctl = smack_file_ioctl,
3552 .file_lock = smack_file_lock,
3553 .file_fcntl = smack_file_fcntl,
3554 .file_mmap = smack_file_mmap,
3555 .file_set_fowner = smack_file_set_fowner,
3556 .file_send_sigiotask = smack_file_send_sigiotask,
3557 .file_receive = smack_file_receive,
3559 .dentry_open = smack_dentry_open,
3561 .cred_alloc_blank = smack_cred_alloc_blank,
3562 .cred_free = smack_cred_free,
3563 .cred_prepare = smack_cred_prepare,
3564 .cred_transfer = smack_cred_transfer,
3565 .kernel_act_as = smack_kernel_act_as,
3566 .kernel_create_files_as = smack_kernel_create_files_as,
3567 .task_setpgid = smack_task_setpgid,
3568 .task_getpgid = smack_task_getpgid,
3569 .task_getsid = smack_task_getsid,
3570 .task_getsecid = smack_task_getsecid,
3571 .task_setnice = smack_task_setnice,
3572 .task_setioprio = smack_task_setioprio,
3573 .task_getioprio = smack_task_getioprio,
3574 .task_setscheduler = smack_task_setscheduler,
3575 .task_getscheduler = smack_task_getscheduler,
3576 .task_movememory = smack_task_movememory,
3577 .task_kill = smack_task_kill,
3578 .task_wait = smack_task_wait,
3579 .task_to_inode = smack_task_to_inode,
3581 .ipc_permission = smack_ipc_permission,
3582 .ipc_getsecid = smack_ipc_getsecid,
3584 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3585 .msg_msg_free_security = smack_msg_msg_free_security,
3587 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3588 .msg_queue_free_security = smack_msg_queue_free_security,
3589 .msg_queue_associate = smack_msg_queue_associate,
3590 .msg_queue_msgctl = smack_msg_queue_msgctl,
3591 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3592 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3594 .shm_alloc_security = smack_shm_alloc_security,
3595 .shm_free_security = smack_shm_free_security,
3596 .shm_associate = smack_shm_associate,
3597 .shm_shmctl = smack_shm_shmctl,
3598 .shm_shmat = smack_shm_shmat,
3600 .sem_alloc_security = smack_sem_alloc_security,
3601 .sem_free_security = smack_sem_free_security,
3602 .sem_associate = smack_sem_associate,
3603 .sem_semctl = smack_sem_semctl,
3604 .sem_semop = smack_sem_semop,
3606 .d_instantiate = smack_d_instantiate,
3608 .getprocattr = smack_getprocattr,
3609 .setprocattr = smack_setprocattr,
3611 .unix_stream_connect = smack_unix_stream_connect,
3612 .unix_may_send = smack_unix_may_send,
3614 .socket_post_create = smack_socket_post_create,
3615 .socket_connect = smack_socket_connect,
3616 .socket_sendmsg = smack_socket_sendmsg,
3617 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3618 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3619 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3620 .sk_alloc_security = smack_sk_alloc_security,
3621 .sk_free_security = smack_sk_free_security,
3622 .sock_graft = smack_sock_graft,
3623 .inet_conn_request = smack_inet_conn_request,
3624 .inet_csk_clone = smack_inet_csk_clone,
3626 /* key management security hooks */
3628 .key_alloc = smack_key_alloc,
3629 .key_free = smack_key_free,
3630 .key_permission = smack_key_permission,
3631 #endif /* CONFIG_KEYS */
3635 .audit_rule_init = smack_audit_rule_init,
3636 .audit_rule_known = smack_audit_rule_known,
3637 .audit_rule_match = smack_audit_rule_match,
3638 .audit_rule_free = smack_audit_rule_free,
3639 #endif /* CONFIG_AUDIT */
3641 .secid_to_secctx = smack_secid_to_secctx,
3642 .secctx_to_secid = smack_secctx_to_secid,
3643 .release_secctx = smack_release_secctx,
3644 .inode_notifysecctx = smack_inode_notifysecctx,
3645 .inode_setsecctx = smack_inode_setsecctx,
3646 .inode_getsecctx = smack_inode_getsecctx,
3650 static __init void init_smack_know_list(void)
3652 list_add(&smack_known_huh.list, &smack_known_list);
3653 list_add(&smack_known_hat.list, &smack_known_list);
3654 list_add(&smack_known_star.list, &smack_known_list);
3655 list_add(&smack_known_floor.list, &smack_known_list);
3656 list_add(&smack_known_invalid.list, &smack_known_list);
3657 list_add(&smack_known_web.list, &smack_known_list);
3661 * smack_init - initialize the smack system
3665 static __init int smack_init(void)
3668 struct task_smack *tsp;
3670 if (!security_module_enable(&smack_ops))
3673 tsp = new_task_smack(smack_known_floor.smk_known,
3674 smack_known_floor.smk_known, GFP_KERNEL);
3678 printk(KERN_INFO "Smack: Initializing.\n");
3681 * Set the security state for the initial task.
3683 cred = (struct cred *) current->cred;
3684 cred->security = tsp;
3686 /* initialize the smack_know_list */
3687 init_smack_know_list();
3691 spin_lock_init(&smack_known_huh.smk_cipsolock);
3692 spin_lock_init(&smack_known_hat.smk_cipsolock);
3693 spin_lock_init(&smack_known_star.smk_cipsolock);
3694 spin_lock_init(&smack_known_floor.smk_cipsolock);
3695 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3700 if (register_security(&smack_ops))
3701 panic("smack: Unable to register with kernel.\n");
3707 * Smack requires early initialization in order to label
3708 * all processes and objects when they are created.
3710 security_initcall(smack_init);