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);
58 * smk_fetch - Fetch the smack label from a file.
59 * @ip: a pointer to the inode
60 * @dp: a pointer to the dentry
62 * Returns a pointer to the master list entry for the Smack label
63 * or NULL if there was no label to fetch.
65 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
70 struct smack_known *skp = NULL;
72 if (ip->i_op->getxattr == NULL)
75 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
79 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
81 skp = smk_import_entry(buffer, rc);
89 * new_inode_smack - allocate an inode security blob
90 * @smack: a pointer to the Smack label to use in the blob
92 * Returns the new blob or NULL if there's no memory available
94 struct inode_smack *new_inode_smack(char *smack)
96 struct inode_smack *isp;
98 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
102 isp->smk_inode = smack;
104 mutex_init(&isp->smk_lock);
110 * new_task_smack - allocate a task security blob
111 * @smack: a pointer to the Smack label to use in the blob
113 * Returns the new blob or NULL if there's no memory available
115 static struct task_smack *new_task_smack(struct smack_known *task,
116 struct smack_known *forked, gfp_t gfp)
118 struct task_smack *tsp;
120 tsp = kzalloc(sizeof(struct task_smack), gfp);
124 tsp->smk_task = task;
125 tsp->smk_forked = forked;
126 INIT_LIST_HEAD(&tsp->smk_rules);
127 mutex_init(&tsp->smk_rules_lock);
133 * smk_copy_rules - copy a rule set
134 * @nhead - new rules header pointer
135 * @ohead - old rules header pointer
137 * Returns 0 on success, -ENOMEM on error
139 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
142 struct smack_rule *nrp;
143 struct smack_rule *orp;
146 INIT_LIST_HEAD(nhead);
148 list_for_each_entry_rcu(orp, ohead, list) {
149 nrp = kzalloc(sizeof(struct smack_rule), gfp);
155 list_add_rcu(&nrp->list, nhead);
162 * We he, that is fun!
166 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
167 * @ctp: child task pointer
168 * @mode: ptrace attachment mode
170 * Returns 0 if access is OK, an error code otherwise
172 * Do the capability checks, and require read and write.
174 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
177 struct smk_audit_info ad;
178 struct smack_known *skp;
180 rc = cap_ptrace_access_check(ctp, mode);
184 skp = smk_of_task(task_security(ctp));
185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
186 smk_ad_setfield_u_tsk(&ad, ctp);
188 rc = smk_curacc(skp->smk_known, mode, &ad);
193 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
194 * @ptp: parent task pointer
196 * Returns 0 if access is OK, an error code otherwise
198 * Do the capability checks, and require read and write.
200 static int smack_ptrace_traceme(struct task_struct *ptp)
203 struct smk_audit_info ad;
204 struct smack_known *skp;
206 rc = cap_ptrace_traceme(ptp);
210 skp = smk_of_task(task_security(ptp));
211 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
212 smk_ad_setfield_u_tsk(&ad, ptp);
214 rc = smk_curacc(skp->smk_known, MAY_READWRITE, &ad);
219 * smack_syslog - Smack approval on syslog
220 * @type: message type
222 * Returns 0 on success, error code otherwise.
224 static int smack_syslog(int typefrom_file)
227 struct smack_known *skp = smk_of_current();
229 if (smack_privileged(CAP_MAC_OVERRIDE))
232 if (smack_syslog_label != NULL && smack_syslog_label != skp)
244 * smack_sb_alloc_security - allocate a superblock blob
245 * @sb: the superblock getting the blob
247 * Returns 0 on success or -ENOMEM on error.
249 static int smack_sb_alloc_security(struct super_block *sb)
251 struct superblock_smack *sbsp;
253 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
258 sbsp->smk_root = smack_known_floor.smk_known;
259 sbsp->smk_default = smack_known_floor.smk_known;
260 sbsp->smk_floor = smack_known_floor.smk_known;
261 sbsp->smk_hat = smack_known_hat.smk_known;
263 * smk_initialized will be zero from kzalloc.
265 sb->s_security = sbsp;
271 * smack_sb_free_security - free a superblock blob
272 * @sb: the superblock getting the blob
275 static void smack_sb_free_security(struct super_block *sb)
277 kfree(sb->s_security);
278 sb->s_security = NULL;
282 * smack_sb_copy_data - copy mount options data for processing
283 * @orig: where to start
284 * @smackopts: mount options string
286 * Returns 0 on success or -ENOMEM on error.
288 * Copy the Smack specific mount options out of the mount
291 static int smack_sb_copy_data(char *orig, char *smackopts)
293 char *cp, *commap, *otheropts, *dp;
295 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
296 if (otheropts == NULL)
299 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
300 if (strstr(cp, SMK_FSDEFAULT) == cp)
302 else if (strstr(cp, SMK_FSFLOOR) == cp)
304 else if (strstr(cp, SMK_FSHAT) == cp)
306 else if (strstr(cp, SMK_FSROOT) == cp)
308 else if (strstr(cp, SMK_FSTRANS) == cp)
313 commap = strchr(cp, ',');
322 strcpy(orig, otheropts);
323 free_page((unsigned long)otheropts);
329 * smack_sb_kern_mount - Smack specific mount processing
330 * @sb: the file system superblock
331 * @flags: the mount flags
332 * @data: the smack mount options
334 * Returns 0 on success, an error code on failure
336 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
338 struct dentry *root = sb->s_root;
339 struct inode *inode = root->d_inode;
340 struct superblock_smack *sp = sb->s_security;
341 struct inode_smack *isp;
347 if (sp->smk_initialized)
350 sp->smk_initialized = 1;
352 for (op = data; op != NULL; op = commap) {
353 commap = strchr(op, ',');
357 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
358 op += strlen(SMK_FSHAT);
359 nsp = smk_import(op, 0);
362 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
363 op += strlen(SMK_FSFLOOR);
364 nsp = smk_import(op, 0);
367 } else if (strncmp(op, SMK_FSDEFAULT,
368 strlen(SMK_FSDEFAULT)) == 0) {
369 op += strlen(SMK_FSDEFAULT);
370 nsp = smk_import(op, 0);
372 sp->smk_default = nsp;
373 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
374 op += strlen(SMK_FSROOT);
375 nsp = smk_import(op, 0);
378 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
379 op += strlen(SMK_FSTRANS);
380 nsp = smk_import(op, 0);
389 * Initialize the root inode.
391 isp = inode->i_security;
392 if (inode->i_security == NULL) {
393 inode->i_security = new_inode_smack(sp->smk_root);
394 isp = inode->i_security;
396 isp->smk_inode = sp->smk_root;
399 isp->smk_flags |= SMK_INODE_TRANSMUTE;
405 * smack_sb_statfs - Smack check on statfs
406 * @dentry: identifies the file system in question
408 * Returns 0 if current can read the floor of the filesystem,
409 * and error code otherwise
411 static int smack_sb_statfs(struct dentry *dentry)
413 struct superblock_smack *sbp = dentry->d_sb->s_security;
415 struct smk_audit_info ad;
417 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
418 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
420 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
425 * smack_sb_mount - Smack check for mounting
432 * Returns 0 if current can write the floor of the filesystem
433 * being mounted on, an error code otherwise.
435 static int smack_sb_mount(const char *dev_name, struct path *path,
436 const char *type, unsigned long flags, void *data)
438 struct superblock_smack *sbp = path->dentry->d_sb->s_security;
439 struct smk_audit_info ad;
441 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
442 smk_ad_setfield_u_fs_path(&ad, *path);
444 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
448 * smack_sb_umount - Smack check for unmounting
449 * @mnt: file system to unmount
452 * Returns 0 if current can write the floor of the filesystem
453 * being unmounted, an error code otherwise.
455 static int smack_sb_umount(struct vfsmount *mnt, int flags)
457 struct superblock_smack *sbp;
458 struct smk_audit_info ad;
461 path.dentry = mnt->mnt_root;
464 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
465 smk_ad_setfield_u_fs_path(&ad, path);
467 sbp = path.dentry->d_sb->s_security;
468 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
476 * smack_bprm_set_creds - set creds for exec
477 * @bprm: the exec information
479 * Returns 0 if it gets a blob, -ENOMEM otherwise
481 static int smack_bprm_set_creds(struct linux_binprm *bprm)
483 struct inode *inode = file_inode(bprm->file);
484 struct task_smack *bsp = bprm->cred->security;
485 struct inode_smack *isp;
488 rc = cap_bprm_set_creds(bprm);
492 if (bprm->cred_prepared)
495 isp = inode->i_security;
496 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
502 bsp->smk_task = isp->smk_task;
503 bprm->per_clear |= PER_CLEAR_ON_SETID;
509 * smack_bprm_committing_creds - Prepare to install the new credentials
512 * @bprm: binprm for exec
514 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
516 struct task_smack *bsp = bprm->cred->security;
518 if (bsp->smk_task != bsp->smk_forked)
519 current->pdeath_signal = 0;
523 * smack_bprm_secureexec - Return the decision to use secureexec.
524 * @bprm: binprm for exec
526 * Returns 0 on success.
528 static int smack_bprm_secureexec(struct linux_binprm *bprm)
530 struct task_smack *tsp = current_security();
531 int ret = cap_bprm_secureexec(bprm);
533 if (!ret && (tsp->smk_task != tsp->smk_forked))
544 * smack_inode_alloc_security - allocate an inode blob
545 * @inode: the inode in need of a blob
547 * Returns 0 if it gets a blob, -ENOMEM otherwise
549 static int smack_inode_alloc_security(struct inode *inode)
551 struct smack_known *skp = smk_of_current();
553 inode->i_security = new_inode_smack(skp->smk_known);
554 if (inode->i_security == NULL)
560 * smack_inode_free_security - free an inode blob
561 * @inode: the inode with a blob
563 * Clears the blob pointer in inode
565 static void smack_inode_free_security(struct inode *inode)
567 kfree(inode->i_security);
568 inode->i_security = NULL;
572 * smack_inode_init_security - copy out the smack from an inode
576 * @name: where to put the attribute name
577 * @value: where to put the attribute value
578 * @len: where to put the length of the attribute
580 * Returns 0 if it all works out, -ENOMEM if there's no memory
582 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
583 const struct qstr *qstr, const char **name,
584 void **value, size_t *len)
586 struct inode_smack *issp = inode->i_security;
587 struct smack_known *skp = smk_of_current();
588 char *isp = smk_of_inode(inode);
589 char *dsp = smk_of_inode(dir);
593 *name = XATTR_SMACK_SUFFIX;
597 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
601 * If the access rule allows transmutation and
602 * the directory requests transmutation then
603 * by all means transmute.
604 * Mark the inode as changed.
606 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
607 smk_inode_transmutable(dir)) {
609 issp->smk_flags |= SMK_INODE_CHANGED;
612 *value = kstrdup(isp, GFP_NOFS);
618 *len = strlen(isp) + 1;
624 * smack_inode_link - Smack check on link
625 * @old_dentry: the existing object
627 * @new_dentry: the new object
629 * Returns 0 if access is permitted, an error code otherwise
631 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
632 struct dentry *new_dentry)
635 struct smk_audit_info ad;
638 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
639 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
641 isp = smk_of_inode(old_dentry->d_inode);
642 rc = smk_curacc(isp, MAY_WRITE, &ad);
644 if (rc == 0 && new_dentry->d_inode != NULL) {
645 isp = smk_of_inode(new_dentry->d_inode);
646 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
647 rc = smk_curacc(isp, MAY_WRITE, &ad);
654 * smack_inode_unlink - Smack check on inode deletion
655 * @dir: containing directory object
656 * @dentry: file to unlink
658 * Returns 0 if current can write the containing directory
659 * and the object, error code otherwise
661 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
663 struct inode *ip = dentry->d_inode;
664 struct smk_audit_info ad;
667 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
668 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
671 * You need write access to the thing you're unlinking
673 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
676 * You also need write access to the containing directory
678 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
679 smk_ad_setfield_u_fs_inode(&ad, dir);
680 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
686 * smack_inode_rmdir - Smack check on directory deletion
687 * @dir: containing directory object
688 * @dentry: directory to unlink
690 * Returns 0 if current can write the containing directory
691 * and the directory, error code otherwise
693 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
695 struct smk_audit_info ad;
698 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
699 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
702 * You need write access to the thing you're removing
704 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
707 * You also need write access to the containing directory
709 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
710 smk_ad_setfield_u_fs_inode(&ad, dir);
711 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
718 * smack_inode_rename - Smack check on rename
719 * @old_inode: the old directory
720 * @old_dentry: unused
721 * @new_inode: the new directory
722 * @new_dentry: unused
724 * Read and write access is required on both the old and
727 * Returns 0 if access is permitted, an error code otherwise
729 static int smack_inode_rename(struct inode *old_inode,
730 struct dentry *old_dentry,
731 struct inode *new_inode,
732 struct dentry *new_dentry)
736 struct smk_audit_info ad;
738 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
739 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
741 isp = smk_of_inode(old_dentry->d_inode);
742 rc = smk_curacc(isp, MAY_READWRITE, &ad);
744 if (rc == 0 && new_dentry->d_inode != NULL) {
745 isp = smk_of_inode(new_dentry->d_inode);
746 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
747 rc = smk_curacc(isp, MAY_READWRITE, &ad);
753 * smack_inode_permission - Smack version of permission()
754 * @inode: the inode in question
755 * @mask: the access requested
757 * This is the important Smack hook.
759 * Returns 0 if access is permitted, -EACCES otherwise
761 static int smack_inode_permission(struct inode *inode, int mask)
763 struct smk_audit_info ad;
764 int no_block = mask & MAY_NOT_BLOCK;
766 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
768 * No permission to check. Existence test. Yup, it's there.
773 /* May be droppable after audit */
776 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
777 smk_ad_setfield_u_fs_inode(&ad, inode);
778 return smk_curacc(smk_of_inode(inode), mask, &ad);
782 * smack_inode_setattr - Smack check for setting attributes
783 * @dentry: the object
784 * @iattr: for the force flag
786 * Returns 0 if access is permitted, an error code otherwise
788 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
790 struct smk_audit_info ad;
792 * Need to allow for clearing the setuid bit.
794 if (iattr->ia_valid & ATTR_FORCE)
796 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
797 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
799 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
803 * smack_inode_getattr - Smack check for getting attributes
805 * @dentry: the object
807 * Returns 0 if access is permitted, an error code otherwise
809 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
811 struct smk_audit_info ad;
814 path.dentry = dentry;
817 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
818 smk_ad_setfield_u_fs_path(&ad, path);
819 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
823 * smack_inode_setxattr - Smack check for setting xattrs
824 * @dentry: the object
825 * @name: name of the attribute
830 * This protects the Smack attribute explicitly.
832 * Returns 0 if access is permitted, an error code otherwise
834 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
835 const void *value, size_t size, int flags)
837 struct smk_audit_info ad;
838 struct smack_known *skp;
840 int check_import = 0;
845 * Check label validity here so import won't fail in post_setxattr
847 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
848 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
849 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
852 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
853 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
857 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
859 if (size != TRANS_TRUE_SIZE ||
860 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
863 rc = cap_inode_setxattr(dentry, name, value, size, flags);
865 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
868 if (rc == 0 && check_import) {
869 skp = smk_import_entry(value, size);
870 if (skp == NULL || (check_star &&
871 (skp == &smack_known_star || skp == &smack_known_web)))
875 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
876 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
879 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
885 * smack_inode_post_setxattr - Apply the Smack update approved above
887 * @name: attribute name
888 * @value: attribute value
889 * @size: attribute size
892 * Set the pointer in the inode blob to the entry found
893 * in the master label list.
895 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
896 const void *value, size_t size, int flags)
898 struct smack_known *skp;
899 struct inode_smack *isp = dentry->d_inode->i_security;
901 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
902 isp->smk_flags |= SMK_INODE_TRANSMUTE;
906 skp = smk_import_entry(value, size);
907 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
909 isp->smk_inode = skp->smk_known;
911 isp->smk_inode = smack_known_invalid.smk_known;
912 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
916 isp->smk_task = &smack_known_invalid;
917 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
921 isp->smk_mmap = &smack_known_invalid;
928 * smack_inode_getxattr - Smack check on getxattr
929 * @dentry: the object
932 * Returns 0 if access is permitted, an error code otherwise
934 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
936 struct smk_audit_info ad;
938 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
939 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
941 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
945 * smack_inode_removexattr - Smack check on removexattr
946 * @dentry: the object
947 * @name: name of the attribute
949 * Removing the Smack attribute requires CAP_MAC_ADMIN
951 * Returns 0 if access is permitted, an error code otherwise
953 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
955 struct inode_smack *isp;
956 struct smk_audit_info ad;
959 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
960 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
961 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
962 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
963 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
964 strcmp(name, XATTR_NAME_SMACKMMAP)) {
965 if (!smack_privileged(CAP_MAC_ADMIN))
968 rc = cap_inode_removexattr(dentry, name);
970 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
971 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
973 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
976 isp = dentry->d_inode->i_security;
977 isp->smk_task = NULL;
978 isp->smk_mmap = NULL;
985 * smack_inode_getsecurity - get smack xattrs
987 * @name: attribute name
988 * @buffer: where to put the result
991 * Returns the size of the attribute or an error code
993 static int smack_inode_getsecurity(const struct inode *inode,
994 const char *name, void **buffer,
997 struct socket_smack *ssp;
999 struct super_block *sbp;
1000 struct inode *ip = (struct inode *)inode;
1005 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1006 isp = smk_of_inode(inode);
1007 ilen = strlen(isp) + 1;
1013 * The rest of the Smack xattrs are only on sockets.
1016 if (sbp->s_magic != SOCKFS_MAGIC)
1019 sock = SOCKET_I(ip);
1020 if (sock == NULL || sock->sk == NULL)
1023 ssp = sock->sk->sk_security;
1025 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1027 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1028 isp = ssp->smk_out->smk_known;
1032 ilen = strlen(isp) + 1;
1043 * smack_inode_listsecurity - list the Smack attributes
1044 * @inode: the object
1045 * @buffer: where they go
1046 * @buffer_size: size of buffer
1048 * Returns 0 on success, -EINVAL otherwise
1050 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1053 int len = strlen(XATTR_NAME_SMACK);
1055 if (buffer != NULL && len <= buffer_size) {
1056 memcpy(buffer, XATTR_NAME_SMACK, len);
1063 * smack_inode_getsecid - Extract inode's security id
1064 * @inode: inode to extract the info from
1065 * @secid: where result will be saved
1067 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1069 struct inode_smack *isp = inode->i_security;
1071 *secid = smack_to_secid(isp->smk_inode);
1079 * smack_file_permission - Smack check on file operations
1085 * Should access checks be done on each read or write?
1086 * UNICOS and SELinux say yes.
1087 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1089 * I'll say no for now. Smack does not do the frequent
1090 * label changing that SELinux does.
1092 static int smack_file_permission(struct file *file, int mask)
1098 * smack_file_alloc_security - assign a file security blob
1101 * The security blob for a file is a pointer to the master
1102 * label list, so no allocation is done.
1106 static int smack_file_alloc_security(struct file *file)
1108 struct smack_known *skp = smk_of_current();
1110 file->f_security = skp->smk_known;
1115 * smack_file_free_security - clear a file security blob
1118 * The security blob for a file is a pointer to the master
1119 * label list, so no memory is freed.
1121 static void smack_file_free_security(struct file *file)
1123 file->f_security = NULL;
1127 * smack_file_ioctl - Smack check on ioctls
1132 * Relies heavily on the correct use of the ioctl command conventions.
1134 * Returns 0 if allowed, error code otherwise
1136 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1140 struct smk_audit_info ad;
1142 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1143 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1145 if (_IOC_DIR(cmd) & _IOC_WRITE)
1146 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1148 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1149 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1155 * smack_file_lock - Smack check on file locking
1159 * Returns 0 if current has lock access, error code otherwise
1161 static int smack_file_lock(struct file *file, unsigned int cmd)
1163 struct smk_audit_info ad;
1165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1166 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1167 return smk_curacc(file->f_security, MAY_LOCK, &ad);
1171 * smack_file_fcntl - Smack check on fcntl
1173 * @cmd: what action to check
1176 * Generally these operations are harmless.
1177 * File locking operations present an obvious mechanism
1178 * for passing information, so they require write access.
1180 * Returns 0 if current has access, error code otherwise
1182 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1185 struct smk_audit_info ad;
1194 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1195 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1196 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1200 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1201 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1202 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1213 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1214 * if mapping anonymous memory.
1215 * @file contains the file structure for file to map (may be NULL).
1216 * @reqprot contains the protection requested by the application.
1217 * @prot contains the protection that will be applied by the kernel.
1218 * @flags contains the operational flags.
1219 * Return 0 if permission is granted.
1221 static int smack_mmap_file(struct file *file,
1222 unsigned long reqprot, unsigned long prot,
1223 unsigned long flags)
1225 struct smack_known *skp;
1226 struct smack_known *mkp;
1227 struct smack_rule *srp;
1228 struct task_smack *tsp;
1230 struct inode_smack *isp;
1239 isp = file_inode(file)->i_security;
1240 if (isp->smk_mmap == NULL)
1242 mkp = isp->smk_mmap;
1244 tsp = current_security();
1245 skp = smk_of_current();
1250 * For each Smack rule associated with the subject
1251 * label verify that the SMACK64MMAP also has access
1252 * to that rule's object label.
1254 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1255 osmack = srp->smk_object;
1257 * Matching labels always allows access.
1259 if (mkp->smk_known == osmack)
1262 * If there is a matching local rule take
1263 * that into account as well.
1265 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1268 may = srp->smk_access;
1270 may &= srp->smk_access;
1272 * If may is zero the SMACK64MMAP subject can't
1273 * possibly have less access.
1279 * Fetch the global list entry.
1280 * If there isn't one a SMACK64MMAP subject
1281 * can't have as much access as current.
1283 mmay = smk_access_entry(mkp->smk_known, osmack,
1285 if (mmay == -ENOENT) {
1290 * If there is a local entry it modifies the
1291 * potential access, too.
1293 tmay = smk_access_entry(mkp->smk_known, osmack,
1295 if (tmay != -ENOENT)
1299 * If there is any access available to current that is
1300 * not available to a SMACK64MMAP subject
1303 if ((may | mmay) != mmay) {
1315 * smack_file_set_fowner - set the file security blob value
1316 * @file: object in question
1319 * Further research may be required on this one.
1321 static int smack_file_set_fowner(struct file *file)
1323 struct smack_known *skp = smk_of_current();
1325 file->f_security = skp->smk_known;
1330 * smack_file_send_sigiotask - Smack on sigio
1331 * @tsk: The target task
1332 * @fown: the object the signal come from
1335 * Allow a privileged task to get signals even if it shouldn't
1337 * Returns 0 if a subject with the object's smack could
1338 * write to the task, an error code otherwise.
1340 static int smack_file_send_sigiotask(struct task_struct *tsk,
1341 struct fown_struct *fown, int signum)
1343 struct smack_known *skp;
1344 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1347 struct smk_audit_info ad;
1350 * struct fown_struct is never outside the context of a struct file
1352 file = container_of(fown, struct file, f_owner);
1354 /* we don't log here as rc can be overriden */
1355 skp = smk_find_entry(file->f_security);
1356 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1357 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1360 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1361 smk_ad_setfield_u_tsk(&ad, tsk);
1362 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1367 * smack_file_receive - Smack file receive check
1370 * Returns 0 if current has access, error code otherwise
1372 static int smack_file_receive(struct file *file)
1375 struct smk_audit_info ad;
1377 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1378 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1380 * This code relies on bitmasks.
1382 if (file->f_mode & FMODE_READ)
1384 if (file->f_mode & FMODE_WRITE)
1387 return smk_curacc(file->f_security, may, &ad);
1391 * smack_file_open - Smack dentry open processing
1395 * Set the security blob in the file structure.
1399 static int smack_file_open(struct file *file, const struct cred *cred)
1401 struct inode_smack *isp = file_inode(file)->i_security;
1403 file->f_security = isp->smk_inode;
1413 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1414 * @new: the new credentials
1415 * @gfp: the atomicity of any memory allocations
1417 * Prepare a blank set of credentials for modification. This must allocate all
1418 * the memory the LSM module might require such that cred_transfer() can
1419 * complete without error.
1421 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1423 struct task_smack *tsp;
1425 tsp = new_task_smack(NULL, NULL, gfp);
1429 cred->security = tsp;
1436 * smack_cred_free - "free" task-level security credentials
1437 * @cred: the credentials in question
1440 static void smack_cred_free(struct cred *cred)
1442 struct task_smack *tsp = cred->security;
1443 struct smack_rule *rp;
1444 struct list_head *l;
1445 struct list_head *n;
1449 cred->security = NULL;
1451 list_for_each_safe(l, n, &tsp->smk_rules) {
1452 rp = list_entry(l, struct smack_rule, list);
1453 list_del(&rp->list);
1460 * smack_cred_prepare - prepare new set of credentials for modification
1461 * @new: the new credentials
1462 * @old: the original credentials
1463 * @gfp: the atomicity of any memory allocations
1465 * Prepare a new set of credentials for modification.
1467 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1470 struct task_smack *old_tsp = old->security;
1471 struct task_smack *new_tsp;
1474 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1475 if (new_tsp == NULL)
1478 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1482 new->security = new_tsp;
1487 * smack_cred_transfer - Transfer the old credentials to the new credentials
1488 * @new: the new credentials
1489 * @old: the original credentials
1491 * Fill in a set of blank credentials from another set of credentials.
1493 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1495 struct task_smack *old_tsp = old->security;
1496 struct task_smack *new_tsp = new->security;
1498 new_tsp->smk_task = old_tsp->smk_task;
1499 new_tsp->smk_forked = old_tsp->smk_task;
1500 mutex_init(&new_tsp->smk_rules_lock);
1501 INIT_LIST_HEAD(&new_tsp->smk_rules);
1504 /* cbs copy rule list */
1508 * smack_kernel_act_as - Set the subjective context in a set of credentials
1509 * @new: points to the set of credentials to be modified.
1510 * @secid: specifies the security ID to be set
1512 * Set the security data for a kernel service.
1514 static int smack_kernel_act_as(struct cred *new, u32 secid)
1516 struct task_smack *new_tsp = new->security;
1517 struct smack_known *skp = smack_from_secid(secid);
1522 new_tsp->smk_task = skp;
1527 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1528 * @new: points to the set of credentials to be modified
1529 * @inode: points to the inode to use as a reference
1531 * Set the file creation context in a set of credentials to the same
1532 * as the objective context of the specified inode
1534 static int smack_kernel_create_files_as(struct cred *new,
1535 struct inode *inode)
1537 struct inode_smack *isp = inode->i_security;
1538 struct task_smack *tsp = new->security;
1540 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1541 tsp->smk_task = tsp->smk_forked;
1546 * smk_curacc_on_task - helper to log task related access
1547 * @p: the task object
1548 * @access: the access requested
1549 * @caller: name of the calling function for audit
1551 * Return 0 if access is permitted
1553 static int smk_curacc_on_task(struct task_struct *p, int access,
1556 struct smk_audit_info ad;
1557 struct smack_known *skp = smk_of_task(task_security(p));
1559 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1560 smk_ad_setfield_u_tsk(&ad, p);
1561 return smk_curacc(skp->smk_known, access, &ad);
1565 * smack_task_setpgid - Smack check on setting pgid
1566 * @p: the task object
1569 * Return 0 if write access is permitted
1571 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1573 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1577 * smack_task_getpgid - Smack access check for getpgid
1578 * @p: the object task
1580 * Returns 0 if current can read the object task, error code otherwise
1582 static int smack_task_getpgid(struct task_struct *p)
1584 return smk_curacc_on_task(p, MAY_READ, __func__);
1588 * smack_task_getsid - Smack access check for getsid
1589 * @p: the object task
1591 * Returns 0 if current can read the object task, error code otherwise
1593 static int smack_task_getsid(struct task_struct *p)
1595 return smk_curacc_on_task(p, MAY_READ, __func__);
1599 * smack_task_getsecid - get the secid of the task
1600 * @p: the object task
1601 * @secid: where to put the result
1603 * Sets the secid to contain a u32 version of the smack label.
1605 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1607 struct smack_known *skp = smk_of_task(task_security(p));
1609 *secid = skp->smk_secid;
1613 * smack_task_setnice - Smack check on setting nice
1614 * @p: the task object
1617 * Return 0 if write access is permitted
1619 static int smack_task_setnice(struct task_struct *p, int nice)
1623 rc = cap_task_setnice(p, nice);
1625 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1630 * smack_task_setioprio - Smack check on setting ioprio
1631 * @p: the task object
1634 * Return 0 if write access is permitted
1636 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1640 rc = cap_task_setioprio(p, ioprio);
1642 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1647 * smack_task_getioprio - Smack check on reading ioprio
1648 * @p: the task object
1650 * Return 0 if read access is permitted
1652 static int smack_task_getioprio(struct task_struct *p)
1654 return smk_curacc_on_task(p, MAY_READ, __func__);
1658 * smack_task_setscheduler - Smack check on setting scheduler
1659 * @p: the task object
1663 * Return 0 if read access is permitted
1665 static int smack_task_setscheduler(struct task_struct *p)
1669 rc = cap_task_setscheduler(p);
1671 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1676 * smack_task_getscheduler - Smack check on reading scheduler
1677 * @p: the task object
1679 * Return 0 if read access is permitted
1681 static int smack_task_getscheduler(struct task_struct *p)
1683 return smk_curacc_on_task(p, MAY_READ, __func__);
1687 * smack_task_movememory - Smack check on moving memory
1688 * @p: the task object
1690 * Return 0 if write access is permitted
1692 static int smack_task_movememory(struct task_struct *p)
1694 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1698 * smack_task_kill - Smack check on signal delivery
1699 * @p: the task object
1702 * @secid: identifies the smack to use in lieu of current's
1704 * Return 0 if write access is permitted
1706 * The secid behavior is an artifact of an SELinux hack
1707 * in the USB code. Someday it may go away.
1709 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1712 struct smk_audit_info ad;
1713 struct smack_known *skp;
1714 struct smack_known *tkp = smk_of_task(task_security(p));
1716 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1717 smk_ad_setfield_u_tsk(&ad, p);
1719 * Sending a signal requires that the sender
1720 * can write the receiver.
1723 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
1725 * If the secid isn't 0 we're dealing with some USB IO
1726 * specific behavior. This is not clean. For one thing
1727 * we can't take privilege into account.
1729 skp = smack_from_secid(secid);
1730 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
1734 * smack_task_wait - Smack access check for waiting
1735 * @p: task to wait for
1739 static int smack_task_wait(struct task_struct *p)
1742 * Allow the operation to succeed.
1744 * In userless environments (e.g. phones) programs
1745 * get marked with SMACK64EXEC and even if the parent
1746 * and child shouldn't be talking the parent still
1747 * may expect to know when the child exits.
1753 * smack_task_to_inode - copy task smack into the inode blob
1754 * @p: task to copy from
1755 * @inode: inode to copy to
1757 * Sets the smack pointer in the inode security blob
1759 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1761 struct inode_smack *isp = inode->i_security;
1762 struct smack_known *skp = smk_of_task(task_security(p));
1764 isp->smk_inode = skp->smk_known;
1772 * smack_sk_alloc_security - Allocate a socket blob
1775 * @gfp_flags: memory allocation flags
1777 * Assign Smack pointers to current
1779 * Returns 0 on success, -ENOMEM is there's no memory
1781 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1783 struct smack_known *skp = smk_of_current();
1784 struct socket_smack *ssp;
1786 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1790 ssp->smk_in = skp->smk_known;
1792 ssp->smk_packet = NULL;
1794 sk->sk_security = ssp;
1800 * smack_sk_free_security - Free a socket blob
1803 * Clears the blob pointer
1805 static void smack_sk_free_security(struct sock *sk)
1807 kfree(sk->sk_security);
1811 * smack_host_label - check host based restrictions
1812 * @sip: the object end
1814 * looks for host based access restrictions
1816 * This version will only be appropriate for really small sets of single label
1817 * hosts. The caller is responsible for ensuring that the RCU read lock is
1818 * taken before calling this function.
1820 * Returns the label of the far end or NULL if it's not special.
1822 static char *smack_host_label(struct sockaddr_in *sip)
1824 struct smk_netlbladdr *snp;
1825 struct in_addr *siap = &sip->sin_addr;
1827 if (siap->s_addr == 0)
1830 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1832 * we break after finding the first match because
1833 * the list is sorted from longest to shortest mask
1834 * so we have found the most specific match
1836 if ((&snp->smk_host.sin_addr)->s_addr ==
1837 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1838 /* we have found the special CIPSO option */
1839 if (snp->smk_label == smack_cipso_option)
1841 return snp->smk_label;
1848 * smack_netlabel - Set the secattr on a socket
1850 * @labeled: socket label scheme
1852 * Convert the outbound smack value (smk_out) to a
1853 * secattr and attach it to the socket.
1855 * Returns 0 on success or an error code
1857 static int smack_netlabel(struct sock *sk, int labeled)
1859 struct smack_known *skp;
1860 struct socket_smack *ssp = sk->sk_security;
1864 * Usually the netlabel code will handle changing the
1865 * packet labeling based on the label.
1866 * The case of a single label host is different, because
1867 * a single label host should never get a labeled packet
1868 * even though the label is usually associated with a packet
1872 bh_lock_sock_nested(sk);
1874 if (ssp->smk_out == smack_net_ambient ||
1875 labeled == SMACK_UNLABELED_SOCKET)
1876 netlbl_sock_delattr(sk);
1879 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
1889 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1891 * @sap: the destination address
1893 * Set the correct secattr for the given socket based on the destination
1894 * address and perform any outbound access checks needed.
1896 * Returns 0 on success or an error code.
1899 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1901 struct smack_known *skp;
1905 struct socket_smack *ssp = sk->sk_security;
1906 struct smk_audit_info ad;
1909 hostsp = smack_host_label(sap);
1910 if (hostsp != NULL) {
1912 struct lsm_network_audit net;
1914 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1915 ad.a.u.net->family = sap->sin_family;
1916 ad.a.u.net->dport = sap->sin_port;
1917 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
1919 sk_lbl = SMACK_UNLABELED_SOCKET;
1921 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
1923 sk_lbl = SMACK_CIPSO_SOCKET;
1930 return smack_netlabel(sk, sk_lbl);
1934 * smk_ipv6_port_label - Smack port access table management
1938 * Create or update the port list entry
1940 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1942 struct sock *sk = sock->sk;
1943 struct sockaddr_in6 *addr6;
1944 struct socket_smack *ssp = sock->sk->sk_security;
1945 struct smk_port_label *spp;
1946 unsigned short port = 0;
1948 if (address == NULL) {
1950 * This operation is changing the Smack information
1951 * on the bound socket. Take the changes to the port
1954 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1955 if (sk != spp->smk_sock)
1957 spp->smk_in = ssp->smk_in;
1958 spp->smk_out = ssp->smk_out;
1962 * A NULL address is only used for updating existing
1963 * bound entries. If there isn't one, it's OK.
1968 addr6 = (struct sockaddr_in6 *)address;
1969 port = ntohs(addr6->sin6_port);
1971 * This is a special case that is safely ignored.
1977 * Look for an existing port list entry.
1978 * This is an indication that a port is getting reused.
1980 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1981 if (spp->smk_port != port)
1983 spp->smk_port = port;
1985 spp->smk_in = ssp->smk_in;
1986 spp->smk_out = ssp->smk_out;
1991 * A new port entry is required.
1993 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1997 spp->smk_port = port;
1999 spp->smk_in = ssp->smk_in;
2000 spp->smk_out = ssp->smk_out;
2002 list_add(&spp->list, &smk_ipv6_port_list);
2007 * smk_ipv6_port_check - check Smack port access
2011 * Create or update the port list entry
2013 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2018 struct smk_port_label *spp;
2019 struct socket_smack *ssp = sk->sk_security;
2020 struct smack_known *skp;
2021 unsigned short port = 0;
2023 struct smk_audit_info ad;
2025 struct lsm_network_audit net;
2028 if (act == SMK_RECEIVING) {
2029 skp = smack_net_ambient;
2030 object = ssp->smk_in;
2033 object = smack_net_ambient->smk_known;
2037 * Get the IP address and port from the address.
2039 port = ntohs(address->sin6_port);
2040 bep = (__be16 *)(&address->sin6_addr);
2041 be32p = (__be32 *)(&address->sin6_addr);
2044 * It's remote, so port lookup does no good.
2046 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2050 * It's local so the send check has to have passed.
2052 if (act == SMK_RECEIVING) {
2053 skp = &smack_known_web;
2057 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2058 if (spp->smk_port != port)
2060 object = spp->smk_in;
2061 if (act == SMK_CONNECTING)
2062 ssp->smk_packet = spp->smk_out->smk_known;
2069 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2070 ad.a.u.net->family = sk->sk_family;
2071 ad.a.u.net->dport = port;
2072 if (act == SMK_RECEIVING)
2073 ad.a.u.net->v6info.saddr = address->sin6_addr;
2075 ad.a.u.net->v6info.daddr = address->sin6_addr;
2077 return smk_access(skp, object, MAY_WRITE, &ad);
2081 * smack_inode_setsecurity - set smack xattrs
2082 * @inode: the object
2083 * @name: attribute name
2084 * @value: attribute value
2085 * @size: size of the attribute
2088 * Sets the named attribute in the appropriate blob
2090 * Returns 0 on success, or an error code
2092 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2093 const void *value, size_t size, int flags)
2095 struct smack_known *skp;
2096 struct inode_smack *nsp = inode->i_security;
2097 struct socket_smack *ssp;
2098 struct socket *sock;
2101 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2104 skp = smk_import_entry(value, size);
2108 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2109 nsp->smk_inode = skp->smk_known;
2110 nsp->smk_flags |= SMK_INODE_INSTANT;
2114 * The rest of the Smack xattrs are only on sockets.
2116 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2119 sock = SOCKET_I(inode);
2120 if (sock == NULL || sock->sk == NULL)
2123 ssp = sock->sk->sk_security;
2125 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2126 ssp->smk_in = skp->smk_known;
2127 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2129 if (sock->sk->sk_family == PF_INET) {
2130 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2133 "Smack: \"%s\" netlbl error %d.\n",
2139 if (sock->sk->sk_family == PF_INET6)
2140 smk_ipv6_port_label(sock, NULL);
2146 * smack_socket_post_create - finish socket setup
2148 * @family: protocol family
2153 * Sets the netlabel information on the socket
2155 * Returns 0 on success, and error code otherwise
2157 static int smack_socket_post_create(struct socket *sock, int family,
2158 int type, int protocol, int kern)
2160 if (family != PF_INET || sock->sk == NULL)
2163 * Set the outbound netlbl.
2165 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2169 * smack_socket_bind - record port binding information.
2171 * @address: the port address
2172 * @addrlen: size of the address
2174 * Records the label bound to a port.
2178 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2181 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2182 smk_ipv6_port_label(sock, address);
2188 * smack_socket_connect - connect access check
2190 * @sap: the other end
2191 * @addrlen: size of sap
2193 * Verifies that a connection may be possible
2195 * Returns 0 on success, and error code otherwise
2197 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2202 if (sock->sk == NULL)
2205 switch (sock->sk->sk_family) {
2207 if (addrlen < sizeof(struct sockaddr_in))
2209 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2212 if (addrlen < sizeof(struct sockaddr_in6))
2214 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2222 * smack_flags_to_may - convert S_ to MAY_ values
2223 * @flags: the S_ value
2225 * Returns the equivalent MAY_ value
2227 static int smack_flags_to_may(int flags)
2231 if (flags & S_IRUGO)
2233 if (flags & S_IWUGO)
2235 if (flags & S_IXUGO)
2242 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2247 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2249 struct smack_known *skp = smk_of_current();
2251 msg->security = skp->smk_known;
2256 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2259 * Clears the blob pointer
2261 static void smack_msg_msg_free_security(struct msg_msg *msg)
2263 msg->security = NULL;
2267 * smack_of_shm - the smack pointer for the shm
2270 * Returns a pointer to the smack value
2272 static char *smack_of_shm(struct shmid_kernel *shp)
2274 return (char *)shp->shm_perm.security;
2278 * smack_shm_alloc_security - Set the security blob for shm
2283 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2285 struct kern_ipc_perm *isp = &shp->shm_perm;
2286 struct smack_known *skp = smk_of_current();
2288 isp->security = skp->smk_known;
2293 * smack_shm_free_security - Clear the security blob for shm
2296 * Clears the blob pointer
2298 static void smack_shm_free_security(struct shmid_kernel *shp)
2300 struct kern_ipc_perm *isp = &shp->shm_perm;
2302 isp->security = NULL;
2306 * smk_curacc_shm : check if current has access on shm
2308 * @access : access requested
2310 * Returns 0 if current has the requested access, error code otherwise
2312 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2314 char *ssp = smack_of_shm(shp);
2315 struct smk_audit_info ad;
2318 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2319 ad.a.u.ipc_id = shp->shm_perm.id;
2321 return smk_curacc(ssp, access, &ad);
2325 * smack_shm_associate - Smack access check for shm
2327 * @shmflg: access requested
2329 * Returns 0 if current has the requested access, error code otherwise
2331 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2335 may = smack_flags_to_may(shmflg);
2336 return smk_curacc_shm(shp, may);
2340 * smack_shm_shmctl - Smack access check for shm
2342 * @cmd: what it wants to do
2344 * Returns 0 if current has the requested access, error code otherwise
2346 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2359 may = MAY_READWRITE;
2364 * System level information.
2370 return smk_curacc_shm(shp, may);
2374 * smack_shm_shmat - Smack access for shmat
2377 * @shmflg: access requested
2379 * Returns 0 if current has the requested access, error code otherwise
2381 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2386 may = smack_flags_to_may(shmflg);
2387 return smk_curacc_shm(shp, may);
2391 * smack_of_sem - the smack pointer for the sem
2394 * Returns a pointer to the smack value
2396 static char *smack_of_sem(struct sem_array *sma)
2398 return (char *)sma->sem_perm.security;
2402 * smack_sem_alloc_security - Set the security blob for sem
2407 static int smack_sem_alloc_security(struct sem_array *sma)
2409 struct kern_ipc_perm *isp = &sma->sem_perm;
2410 struct smack_known *skp = smk_of_current();
2412 isp->security = skp->smk_known;
2417 * smack_sem_free_security - Clear the security blob for sem
2420 * Clears the blob pointer
2422 static void smack_sem_free_security(struct sem_array *sma)
2424 struct kern_ipc_perm *isp = &sma->sem_perm;
2426 isp->security = NULL;
2430 * smk_curacc_sem : check if current has access on sem
2432 * @access : access requested
2434 * Returns 0 if current has the requested access, error code otherwise
2436 static int smk_curacc_sem(struct sem_array *sma, int access)
2438 char *ssp = smack_of_sem(sma);
2439 struct smk_audit_info ad;
2442 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2443 ad.a.u.ipc_id = sma->sem_perm.id;
2445 return smk_curacc(ssp, access, &ad);
2449 * smack_sem_associate - Smack access check for sem
2451 * @semflg: access requested
2453 * Returns 0 if current has the requested access, error code otherwise
2455 static int smack_sem_associate(struct sem_array *sma, int semflg)
2459 may = smack_flags_to_may(semflg);
2460 return smk_curacc_sem(sma, may);
2464 * smack_sem_shmctl - Smack access check for sem
2466 * @cmd: what it wants to do
2468 * Returns 0 if current has the requested access, error code otherwise
2470 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2488 may = MAY_READWRITE;
2493 * System level information
2500 return smk_curacc_sem(sma, may);
2504 * smack_sem_semop - Smack checks of semaphore operations
2510 * Treated as read and write in all cases.
2512 * Returns 0 if access is allowed, error code otherwise
2514 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2515 unsigned nsops, int alter)
2517 return smk_curacc_sem(sma, MAY_READWRITE);
2521 * smack_msg_alloc_security - Set the security blob for msg
2526 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2528 struct kern_ipc_perm *kisp = &msq->q_perm;
2529 struct smack_known *skp = smk_of_current();
2531 kisp->security = skp->smk_known;
2536 * smack_msg_free_security - Clear the security blob for msg
2539 * Clears the blob pointer
2541 static void smack_msg_queue_free_security(struct msg_queue *msq)
2543 struct kern_ipc_perm *kisp = &msq->q_perm;
2545 kisp->security = NULL;
2549 * smack_of_msq - the smack pointer for the msq
2552 * Returns a pointer to the smack value
2554 static char *smack_of_msq(struct msg_queue *msq)
2556 return (char *)msq->q_perm.security;
2560 * smk_curacc_msq : helper to check if current has access on msq
2562 * @access : access requested
2564 * return 0 if current has access, error otherwise
2566 static int smk_curacc_msq(struct msg_queue *msq, int access)
2568 char *msp = smack_of_msq(msq);
2569 struct smk_audit_info ad;
2572 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2573 ad.a.u.ipc_id = msq->q_perm.id;
2575 return smk_curacc(msp, access, &ad);
2579 * smack_msg_queue_associate - Smack access check for msg_queue
2581 * @msqflg: access requested
2583 * Returns 0 if current has the requested access, error code otherwise
2585 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2589 may = smack_flags_to_may(msqflg);
2590 return smk_curacc_msq(msq, may);
2594 * smack_msg_queue_msgctl - Smack access check for msg_queue
2596 * @cmd: what it wants to do
2598 * Returns 0 if current has the requested access, error code otherwise
2600 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2611 may = MAY_READWRITE;
2616 * System level information
2623 return smk_curacc_msq(msq, may);
2627 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2630 * @msqflg: access requested
2632 * Returns 0 if current has the requested access, error code otherwise
2634 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2639 may = smack_flags_to_may(msqflg);
2640 return smk_curacc_msq(msq, may);
2644 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2651 * Returns 0 if current has read and write access, error code otherwise
2653 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2654 struct task_struct *target, long type, int mode)
2656 return smk_curacc_msq(msq, MAY_READWRITE);
2660 * smack_ipc_permission - Smack access for ipc_permission()
2661 * @ipp: the object permissions
2662 * @flag: access requested
2664 * Returns 0 if current has read and write access, error code otherwise
2666 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2668 char *isp = ipp->security;
2669 int may = smack_flags_to_may(flag);
2670 struct smk_audit_info ad;
2673 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2674 ad.a.u.ipc_id = ipp->id;
2676 return smk_curacc(isp, may, &ad);
2680 * smack_ipc_getsecid - Extract smack security id
2681 * @ipp: the object permissions
2682 * @secid: where result will be saved
2684 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2686 char *smack = ipp->security;
2688 *secid = smack_to_secid(smack);
2692 * smack_d_instantiate - Make sure the blob is correct on an inode
2693 * @opt_dentry: dentry where inode will be attached
2694 * @inode: the object
2696 * Set the inode's security blob if it hasn't been done already.
2698 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2700 struct super_block *sbp;
2701 struct superblock_smack *sbsp;
2702 struct inode_smack *isp;
2703 struct smack_known *skp;
2704 struct smack_known *ckp = smk_of_current();
2706 char trattr[TRANS_TRUE_SIZE];
2714 isp = inode->i_security;
2716 mutex_lock(&isp->smk_lock);
2718 * If the inode is already instantiated
2719 * take the quick way out
2721 if (isp->smk_flags & SMK_INODE_INSTANT)
2725 sbsp = sbp->s_security;
2727 * We're going to use the superblock default label
2728 * if there's no label on the file.
2730 final = sbsp->smk_default;
2733 * If this is the root inode the superblock
2734 * may be in the process of initialization.
2735 * If that is the case use the root value out
2736 * of the superblock.
2738 if (opt_dentry->d_parent == opt_dentry) {
2739 isp->smk_inode = sbsp->smk_root;
2740 isp->smk_flags |= SMK_INODE_INSTANT;
2745 * This is pretty hackish.
2746 * Casey says that we shouldn't have to do
2747 * file system specific code, but it does help
2748 * with keeping it simple.
2750 switch (sbp->s_magic) {
2753 * Casey says that it's a little embarrassing
2754 * that the smack file system doesn't do
2755 * extended attributes.
2757 final = smack_known_star.smk_known;
2761 * Casey says pipes are easy (?)
2763 final = smack_known_star.smk_known;
2765 case DEVPTS_SUPER_MAGIC:
2767 * devpts seems content with the label of the task.
2768 * Programs that change smack have to treat the
2771 final = ckp->smk_known;
2775 * Socket access is controlled by the socket
2776 * structures associated with the task involved.
2778 final = smack_known_star.smk_known;
2780 case PROC_SUPER_MAGIC:
2782 * Casey says procfs appears not to care.
2783 * The superblock default suffices.
2788 * Device labels should come from the filesystem,
2789 * but watch out, because they're volitile,
2790 * getting recreated on every reboot.
2792 final = smack_known_star.smk_known;
2796 * If a smack value has been set we want to use it,
2797 * but since tmpfs isn't giving us the opportunity
2798 * to set mount options simulate setting the
2799 * superblock default.
2803 * This isn't an understood special case.
2804 * Get the value from the xattr.
2808 * UNIX domain sockets use lower level socket data.
2810 if (S_ISSOCK(inode->i_mode)) {
2811 final = smack_known_star.smk_known;
2815 * No xattr support means, alas, no SMACK label.
2816 * Use the aforeapplied default.
2817 * It would be curious if the label of the task
2818 * does not match that assigned.
2820 if (inode->i_op->getxattr == NULL)
2823 * Get the dentry for xattr.
2825 dp = dget(opt_dentry);
2826 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2828 final = skp->smk_known;
2831 * Transmuting directory
2833 if (S_ISDIR(inode->i_mode)) {
2835 * If this is a new directory and the label was
2836 * transmuted when the inode was initialized
2837 * set the transmute attribute on the directory
2838 * and mark the inode.
2840 * If there is a transmute attribute on the
2841 * directory mark the inode.
2843 if (isp->smk_flags & SMK_INODE_CHANGED) {
2844 isp->smk_flags &= ~SMK_INODE_CHANGED;
2845 rc = inode->i_op->setxattr(dp,
2846 XATTR_NAME_SMACKTRANSMUTE,
2847 TRANS_TRUE, TRANS_TRUE_SIZE,
2850 rc = inode->i_op->getxattr(dp,
2851 XATTR_NAME_SMACKTRANSMUTE, trattr,
2853 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2854 TRANS_TRUE_SIZE) != 0)
2858 transflag = SMK_INODE_TRANSMUTE;
2861 * Don't let the exec or mmap label be "*" or "@".
2863 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2864 if (skp == &smack_known_star || skp == &smack_known_web)
2866 isp->smk_task = skp;
2867 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2868 if (skp == &smack_known_star || skp == &smack_known_web)
2870 isp->smk_mmap = skp;
2877 isp->smk_inode = ckp->smk_known;
2879 isp->smk_inode = final;
2881 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2884 mutex_unlock(&isp->smk_lock);
2889 * smack_getprocattr - Smack process attribute access
2890 * @p: the object task
2891 * @name: the name of the attribute in /proc/.../attr
2892 * @value: where to put the result
2894 * Places a copy of the task Smack into value
2896 * Returns the length of the smack label or an error code
2898 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2900 struct smack_known *skp = smk_of_task(task_security(p));
2904 if (strcmp(name, "current") != 0)
2907 cp = kstrdup(skp->smk_known, GFP_KERNEL);
2917 * smack_setprocattr - Smack process attribute setting
2918 * @p: the object task
2919 * @name: the name of the attribute in /proc/.../attr
2920 * @value: the value to set
2921 * @size: the size of the value
2923 * Sets the Smack value of the task. Only setting self
2924 * is permitted and only with privilege
2926 * Returns the length of the smack label or an error code
2928 static int smack_setprocattr(struct task_struct *p, char *name,
2929 void *value, size_t size)
2931 struct task_smack *tsp;
2933 struct smack_known *skp;
2936 * Changing another process' Smack value is too dangerous
2937 * and supports no sane use case.
2942 if (!smack_privileged(CAP_MAC_ADMIN))
2945 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
2948 if (strcmp(name, "current") != 0)
2951 skp = smk_import_entry(value, size);
2956 * No process is ever allowed the web ("@") label.
2958 if (skp == &smack_known_web)
2961 new = prepare_creds();
2965 tsp = new->security;
2966 tsp->smk_task = skp;
2973 * smack_unix_stream_connect - Smack access on UDS
2975 * @other: the other sock
2978 * Return 0 if a subject with the smack of sock could access
2979 * an object with the smack of other, otherwise an error code
2981 static int smack_unix_stream_connect(struct sock *sock,
2982 struct sock *other, struct sock *newsk)
2984 struct smack_known *skp;
2985 struct socket_smack *ssp = sock->sk_security;
2986 struct socket_smack *osp = other->sk_security;
2987 struct socket_smack *nsp = newsk->sk_security;
2988 struct smk_audit_info ad;
2992 struct lsm_network_audit net;
2994 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2995 smk_ad_setfield_u_net_sk(&ad, other);
2998 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3000 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3004 * Cross reference the peer labels for SO_PEERSEC.
3007 nsp->smk_packet = ssp->smk_out->smk_known;
3008 ssp->smk_packet = osp->smk_out->smk_known;
3015 * smack_unix_may_send - Smack access on UDS
3017 * @other: the other socket
3019 * Return 0 if a subject with the smack of sock could access
3020 * an object with the smack of other, otherwise an error code
3022 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3024 struct socket_smack *ssp = sock->sk->sk_security;
3025 struct socket_smack *osp = other->sk->sk_security;
3026 struct smack_known *skp;
3027 struct smk_audit_info ad;
3030 struct lsm_network_audit net;
3032 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3033 smk_ad_setfield_u_net_sk(&ad, other->sk);
3036 if (smack_privileged(CAP_MAC_OVERRIDE))
3040 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3044 * smack_socket_sendmsg - Smack check based on destination host
3047 * @size: the size of the message
3049 * Return 0 if the current subject can write to the destination host.
3050 * For IPv4 this is only a question if the destination is a single label host.
3051 * For IPv6 this is a check against the label of the port.
3053 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3056 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3057 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3061 * Perfectly reasonable for this to be NULL
3066 switch (sip->sin_family) {
3068 rc = smack_netlabel_send(sock->sk, sip);
3071 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3078 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3079 * @sap: netlabel secattr
3080 * @ssp: socket security information
3082 * Returns a pointer to a Smack label entry found on the label list.
3084 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3085 struct socket_smack *ssp)
3087 struct smack_known *skp;
3092 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3094 * Looks like a CIPSO packet.
3095 * If there are flags but no level netlabel isn't
3096 * behaving the way we expect it to.
3098 * Look it up in the label table
3099 * Without guidance regarding the smack value
3100 * for the packet fall back on the network
3104 list_for_each_entry(skp, &smack_known_list, list) {
3105 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3108 * Compare the catsets. Use the netlbl APIs.
3110 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3111 if ((skp->smk_netlabel.flags &
3112 NETLBL_SECATTR_MLS_CAT) == 0)
3116 for (acat = -1, kcat = -1; acat == kcat; ) {
3117 acat = netlbl_secattr_catmap_walk(
3118 sap->attr.mls.cat, acat + 1);
3119 kcat = netlbl_secattr_catmap_walk(
3120 skp->smk_netlabel.attr.mls.cat,
3122 if (acat < 0 || kcat < 0)
3135 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
3136 return &smack_known_web;
3137 return &smack_known_star;
3139 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3141 * Looks like a fallback, which gives us a secid.
3143 skp = smack_from_secid(sap->attr.secid);
3145 * This has got to be a bug because it is
3146 * impossible to specify a fallback without
3147 * specifying the label, which will ensure
3148 * it has a secid, and the only way to get a
3149 * secid is from a fallback.
3151 BUG_ON(skp == NULL);
3155 * Without guidance regarding the smack value
3156 * for the packet fall back on the network
3159 return smack_net_ambient;
3162 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3166 int proto = -EINVAL;
3167 struct ipv6hdr _ipv6h;
3168 struct ipv6hdr *ip6;
3170 struct tcphdr _tcph, *th;
3171 struct udphdr _udph, *uh;
3172 struct dccp_hdr _dccph, *dh;
3176 offset = skb_network_offset(skb);
3177 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3180 sip->sin6_addr = ip6->saddr;
3182 nexthdr = ip6->nexthdr;
3183 offset += sizeof(_ipv6h);
3184 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3191 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3193 sip->sin6_port = th->source;
3196 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3198 sip->sin6_port = uh->source;
3201 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3203 sip->sin6_port = dh->dccph_sport;
3210 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3214 * Returns 0 if the packet should be delivered, an error code otherwise
3216 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3218 struct netlbl_lsm_secattr secattr;
3219 struct socket_smack *ssp = sk->sk_security;
3220 struct smack_known *skp;
3221 struct sockaddr_in6 sadd;
3223 struct smk_audit_info ad;
3225 struct lsm_network_audit net;
3227 switch (sk->sk_family) {
3230 * Translate what netlabel gave us.
3232 netlbl_secattr_init(&secattr);
3234 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3236 skp = smack_from_secattr(&secattr, ssp);
3238 skp = smack_net_ambient;
3240 netlbl_secattr_destroy(&secattr);
3243 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3244 ad.a.u.net->family = sk->sk_family;
3245 ad.a.u.net->netif = skb->skb_iif;
3246 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3249 * Receiving a packet requires that the other end
3250 * be able to write here. Read access is not required.
3251 * This is the simplist possible security model
3254 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3256 netlbl_skbuff_err(skb, rc, 0);
3259 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3260 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3261 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3270 * smack_socket_getpeersec_stream - pull in packet label
3272 * @optval: user's destination
3273 * @optlen: size thereof
3276 * returns zero on success, an error code otherwise
3278 static int smack_socket_getpeersec_stream(struct socket *sock,
3279 char __user *optval,
3280 int __user *optlen, unsigned len)
3282 struct socket_smack *ssp;
3287 ssp = sock->sk->sk_security;
3288 if (ssp->smk_packet != NULL) {
3289 rcp = ssp->smk_packet;
3290 slen = strlen(rcp) + 1;
3295 else if (copy_to_user(optval, rcp, slen) != 0)
3298 if (put_user(slen, optlen) != 0)
3306 * smack_socket_getpeersec_dgram - pull in packet label
3307 * @sock: the peer socket
3309 * @secid: pointer to where to put the secid of the packet
3311 * Sets the netlabel socket state on sk from parent
3313 static int smack_socket_getpeersec_dgram(struct socket *sock,
3314 struct sk_buff *skb, u32 *secid)
3317 struct netlbl_lsm_secattr secattr;
3318 struct socket_smack *ssp = NULL;
3319 struct smack_known *skp;
3320 int family = PF_UNSPEC;
3321 u32 s = 0; /* 0 is the invalid secid */
3325 if (skb->protocol == htons(ETH_P_IP))
3327 else if (skb->protocol == htons(ETH_P_IPV6))
3330 if (family == PF_UNSPEC && sock != NULL)
3331 family = sock->sk->sk_family;
3333 if (family == PF_UNIX) {
3334 ssp = sock->sk->sk_security;
3335 s = ssp->smk_out->smk_secid;
3336 } else if (family == PF_INET || family == PF_INET6) {
3338 * Translate what netlabel gave us.
3340 if (sock != NULL && sock->sk != NULL)
3341 ssp = sock->sk->sk_security;
3342 netlbl_secattr_init(&secattr);
3343 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3345 skp = smack_from_secattr(&secattr, ssp);
3348 netlbl_secattr_destroy(&secattr);
3357 * smack_sock_graft - Initialize a newly created socket with an existing sock
3359 * @parent: parent socket
3361 * Set the smk_{in,out} state of an existing sock based on the process that
3362 * is creating the new socket.
3364 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3366 struct socket_smack *ssp;
3367 struct smack_known *skp = smk_of_current();
3370 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3373 ssp = sk->sk_security;
3374 ssp->smk_in = skp->smk_known;
3376 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3380 * smack_inet_conn_request - Smack access check on connect
3381 * @sk: socket involved
3385 * Returns 0 if a task with the packet label could write to
3386 * the socket, otherwise an error code
3388 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3389 struct request_sock *req)
3391 u16 family = sk->sk_family;
3392 struct smack_known *skp;
3393 struct socket_smack *ssp = sk->sk_security;
3394 struct netlbl_lsm_secattr secattr;
3395 struct sockaddr_in addr;
3399 struct smk_audit_info ad;
3401 struct lsm_network_audit net;
3404 if (family == PF_INET6) {
3406 * Handle mapped IPv4 packets arriving
3407 * via IPv6 sockets. Don't set up netlabel
3408 * processing on IPv6.
3410 if (skb->protocol == htons(ETH_P_IP))
3416 netlbl_secattr_init(&secattr);
3417 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3419 skp = smack_from_secattr(&secattr, ssp);
3421 skp = &smack_known_huh;
3422 netlbl_secattr_destroy(&secattr);
3425 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3426 ad.a.u.net->family = family;
3427 ad.a.u.net->netif = skb->skb_iif;
3428 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3431 * Receiving a packet requires that the other end be able to write
3432 * here. Read access is not required.
3434 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3439 * Save the peer's label in the request_sock so we can later setup
3440 * smk_packet in the child socket so that SO_PEERCRED can report it.
3442 req->peer_secid = skp->smk_secid;
3445 * We need to decide if we want to label the incoming connection here
3446 * if we do we only need to label the request_sock and the stack will
3447 * propagate the wire-label to the sock when it is created.
3450 addr.sin_addr.s_addr = hdr->saddr;
3452 hsp = smack_host_label(&addr);
3456 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3458 netlbl_req_delattr(req);
3464 * smack_inet_csk_clone - Copy the connection information to the new socket
3465 * @sk: the new socket
3466 * @req: the connection's request_sock
3468 * Transfer the connection's peer label to the newly created socket.
3470 static void smack_inet_csk_clone(struct sock *sk,
3471 const struct request_sock *req)
3473 struct socket_smack *ssp = sk->sk_security;
3474 struct smack_known *skp;
3476 if (req->peer_secid != 0) {
3477 skp = smack_from_secid(req->peer_secid);
3478 ssp->smk_packet = skp->smk_known;
3480 ssp->smk_packet = NULL;
3484 * Key management security hooks
3486 * Casey has not tested key support very heavily.
3487 * The permission check is most likely too restrictive.
3488 * If you care about keys please have a look.
3493 * smack_key_alloc - Set the key security blob
3495 * @cred: the credentials to use
3498 * No allocation required
3502 static int smack_key_alloc(struct key *key, const struct cred *cred,
3503 unsigned long flags)
3505 struct smack_known *skp = smk_of_task(cred->security);
3507 key->security = skp->smk_known;
3512 * smack_key_free - Clear the key security blob
3515 * Clear the blob pointer
3517 static void smack_key_free(struct key *key)
3519 key->security = NULL;
3523 * smack_key_permission - Smack access on a key
3524 * @key_ref: gets to the object
3525 * @cred: the credentials to use
3528 * Return 0 if the task has read and write to the object,
3529 * an error code otherwise
3531 static int smack_key_permission(key_ref_t key_ref,
3532 const struct cred *cred, key_perm_t perm)
3535 struct smk_audit_info ad;
3536 struct smack_known *tkp = smk_of_task(cred->security);
3538 keyp = key_ref_to_ptr(key_ref);
3542 * If the key hasn't been initialized give it access so that
3545 if (keyp->security == NULL)
3548 * This should not occur
3553 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3554 ad.a.u.key_struct.key = keyp->serial;
3555 ad.a.u.key_struct.key_desc = keyp->description;
3557 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
3559 #endif /* CONFIG_KEYS */
3564 * Audit requires a unique representation of each Smack specific
3565 * rule. This unique representation is used to distinguish the
3566 * object to be audited from remaining kernel objects and also
3567 * works as a glue between the audit hooks.
3569 * Since repository entries are added but never deleted, we'll use
3570 * the smack_known label address related to the given audit rule as
3571 * the needed unique representation. This also better fits the smack
3572 * model where nearly everything is a label.
3577 * smack_audit_rule_init - Initialize a smack audit rule
3578 * @field: audit rule fields given from user-space (audit.h)
3579 * @op: required testing operator (=, !=, >, <, ...)
3580 * @rulestr: smack label to be audited
3581 * @vrule: pointer to save our own audit rule representation
3583 * Prepare to audit cases where (@field @op @rulestr) is true.
3584 * The label to be audited is created if necessay.
3586 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3588 char **rule = (char **)vrule;
3591 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3594 if (op != Audit_equal && op != Audit_not_equal)
3597 *rule = smk_import(rulestr, 0);
3603 * smack_audit_rule_known - Distinguish Smack audit rules
3604 * @krule: rule of interest, in Audit kernel representation format
3606 * This is used to filter Smack rules from remaining Audit ones.
3607 * If it's proved that this rule belongs to us, the
3608 * audit_rule_match hook will be called to do the final judgement.
3610 static int smack_audit_rule_known(struct audit_krule *krule)
3612 struct audit_field *f;
3615 for (i = 0; i < krule->field_count; i++) {
3616 f = &krule->fields[i];
3618 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3626 * smack_audit_rule_match - Audit given object ?
3627 * @secid: security id for identifying the object to test
3628 * @field: audit rule flags given from user-space
3629 * @op: required testing operator
3630 * @vrule: smack internal rule presentation
3631 * @actx: audit context associated with the check
3633 * The core Audit hook. It's used to take the decision of
3634 * whether to audit or not to audit a given object.
3636 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3637 struct audit_context *actx)
3639 struct smack_known *skp;
3643 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
3644 "Smack: missing rule\n");
3648 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3651 skp = smack_from_secid(secid);
3654 * No need to do string comparisons. If a match occurs,
3655 * both pointers will point to the same smack_known
3658 if (op == Audit_equal)
3659 return (rule == skp->smk_known);
3660 if (op == Audit_not_equal)
3661 return (rule != skp->smk_known);
3667 * smack_audit_rule_free - free smack rule representation
3668 * @vrule: rule to be freed.
3670 * No memory was allocated.
3672 static void smack_audit_rule_free(void *vrule)
3677 #endif /* CONFIG_AUDIT */
3680 * smack_ismaclabel - check if xattr @name references a smack MAC label
3681 * @name: Full xattr name to check.
3683 static int smack_ismaclabel(const char *name)
3685 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3690 * smack_secid_to_secctx - return the smack label for a secid
3691 * @secid: incoming integer
3692 * @secdata: destination
3693 * @seclen: how long it is
3695 * Exists for networking code.
3697 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3699 struct smack_known *skp = smack_from_secid(secid);
3702 *secdata = skp->smk_known;
3703 *seclen = strlen(skp->smk_known);
3708 * smack_secctx_to_secid - return the secid for a smack label
3709 * @secdata: smack label
3710 * @seclen: how long result is
3711 * @secid: outgoing integer
3713 * Exists for audit and networking code.
3715 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3717 *secid = smack_to_secid(secdata);
3722 * smack_release_secctx - don't do anything.
3726 * Exists to make sure nothing gets done, and properly
3728 static void smack_release_secctx(char *secdata, u32 seclen)
3732 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3734 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3737 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3739 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3742 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3745 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3753 struct security_operations smack_ops = {
3756 .ptrace_access_check = smack_ptrace_access_check,
3757 .ptrace_traceme = smack_ptrace_traceme,
3758 .syslog = smack_syslog,
3760 .sb_alloc_security = smack_sb_alloc_security,
3761 .sb_free_security = smack_sb_free_security,
3762 .sb_copy_data = smack_sb_copy_data,
3763 .sb_kern_mount = smack_sb_kern_mount,
3764 .sb_statfs = smack_sb_statfs,
3765 .sb_mount = smack_sb_mount,
3766 .sb_umount = smack_sb_umount,
3768 .bprm_set_creds = smack_bprm_set_creds,
3769 .bprm_committing_creds = smack_bprm_committing_creds,
3770 .bprm_secureexec = smack_bprm_secureexec,
3772 .inode_alloc_security = smack_inode_alloc_security,
3773 .inode_free_security = smack_inode_free_security,
3774 .inode_init_security = smack_inode_init_security,
3775 .inode_link = smack_inode_link,
3776 .inode_unlink = smack_inode_unlink,
3777 .inode_rmdir = smack_inode_rmdir,
3778 .inode_rename = smack_inode_rename,
3779 .inode_permission = smack_inode_permission,
3780 .inode_setattr = smack_inode_setattr,
3781 .inode_getattr = smack_inode_getattr,
3782 .inode_setxattr = smack_inode_setxattr,
3783 .inode_post_setxattr = smack_inode_post_setxattr,
3784 .inode_getxattr = smack_inode_getxattr,
3785 .inode_removexattr = smack_inode_removexattr,
3786 .inode_getsecurity = smack_inode_getsecurity,
3787 .inode_setsecurity = smack_inode_setsecurity,
3788 .inode_listsecurity = smack_inode_listsecurity,
3789 .inode_getsecid = smack_inode_getsecid,
3791 .file_permission = smack_file_permission,
3792 .file_alloc_security = smack_file_alloc_security,
3793 .file_free_security = smack_file_free_security,
3794 .file_ioctl = smack_file_ioctl,
3795 .file_lock = smack_file_lock,
3796 .file_fcntl = smack_file_fcntl,
3797 .mmap_file = smack_mmap_file,
3798 .mmap_addr = cap_mmap_addr,
3799 .file_set_fowner = smack_file_set_fowner,
3800 .file_send_sigiotask = smack_file_send_sigiotask,
3801 .file_receive = smack_file_receive,
3803 .file_open = smack_file_open,
3805 .cred_alloc_blank = smack_cred_alloc_blank,
3806 .cred_free = smack_cred_free,
3807 .cred_prepare = smack_cred_prepare,
3808 .cred_transfer = smack_cred_transfer,
3809 .kernel_act_as = smack_kernel_act_as,
3810 .kernel_create_files_as = smack_kernel_create_files_as,
3811 .task_setpgid = smack_task_setpgid,
3812 .task_getpgid = smack_task_getpgid,
3813 .task_getsid = smack_task_getsid,
3814 .task_getsecid = smack_task_getsecid,
3815 .task_setnice = smack_task_setnice,
3816 .task_setioprio = smack_task_setioprio,
3817 .task_getioprio = smack_task_getioprio,
3818 .task_setscheduler = smack_task_setscheduler,
3819 .task_getscheduler = smack_task_getscheduler,
3820 .task_movememory = smack_task_movememory,
3821 .task_kill = smack_task_kill,
3822 .task_wait = smack_task_wait,
3823 .task_to_inode = smack_task_to_inode,
3825 .ipc_permission = smack_ipc_permission,
3826 .ipc_getsecid = smack_ipc_getsecid,
3828 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3829 .msg_msg_free_security = smack_msg_msg_free_security,
3831 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3832 .msg_queue_free_security = smack_msg_queue_free_security,
3833 .msg_queue_associate = smack_msg_queue_associate,
3834 .msg_queue_msgctl = smack_msg_queue_msgctl,
3835 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3836 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3838 .shm_alloc_security = smack_shm_alloc_security,
3839 .shm_free_security = smack_shm_free_security,
3840 .shm_associate = smack_shm_associate,
3841 .shm_shmctl = smack_shm_shmctl,
3842 .shm_shmat = smack_shm_shmat,
3844 .sem_alloc_security = smack_sem_alloc_security,
3845 .sem_free_security = smack_sem_free_security,
3846 .sem_associate = smack_sem_associate,
3847 .sem_semctl = smack_sem_semctl,
3848 .sem_semop = smack_sem_semop,
3850 .d_instantiate = smack_d_instantiate,
3852 .getprocattr = smack_getprocattr,
3853 .setprocattr = smack_setprocattr,
3855 .unix_stream_connect = smack_unix_stream_connect,
3856 .unix_may_send = smack_unix_may_send,
3858 .socket_post_create = smack_socket_post_create,
3859 .socket_bind = smack_socket_bind,
3860 .socket_connect = smack_socket_connect,
3861 .socket_sendmsg = smack_socket_sendmsg,
3862 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3863 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3864 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3865 .sk_alloc_security = smack_sk_alloc_security,
3866 .sk_free_security = smack_sk_free_security,
3867 .sock_graft = smack_sock_graft,
3868 .inet_conn_request = smack_inet_conn_request,
3869 .inet_csk_clone = smack_inet_csk_clone,
3871 /* key management security hooks */
3873 .key_alloc = smack_key_alloc,
3874 .key_free = smack_key_free,
3875 .key_permission = smack_key_permission,
3876 #endif /* CONFIG_KEYS */
3880 .audit_rule_init = smack_audit_rule_init,
3881 .audit_rule_known = smack_audit_rule_known,
3882 .audit_rule_match = smack_audit_rule_match,
3883 .audit_rule_free = smack_audit_rule_free,
3884 #endif /* CONFIG_AUDIT */
3886 .ismaclabel = smack_ismaclabel,
3887 .secid_to_secctx = smack_secid_to_secctx,
3888 .secctx_to_secid = smack_secctx_to_secid,
3889 .release_secctx = smack_release_secctx,
3890 .inode_notifysecctx = smack_inode_notifysecctx,
3891 .inode_setsecctx = smack_inode_setsecctx,
3892 .inode_getsecctx = smack_inode_getsecctx,
3896 static __init void init_smack_known_list(void)
3899 * Initialize rule list locks
3901 mutex_init(&smack_known_huh.smk_rules_lock);
3902 mutex_init(&smack_known_hat.smk_rules_lock);
3903 mutex_init(&smack_known_floor.smk_rules_lock);
3904 mutex_init(&smack_known_star.smk_rules_lock);
3905 mutex_init(&smack_known_invalid.smk_rules_lock);
3906 mutex_init(&smack_known_web.smk_rules_lock);
3908 * Initialize rule lists
3910 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3911 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3912 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3913 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3914 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3915 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3917 * Create the known labels list
3919 smk_insert_entry(&smack_known_huh);
3920 smk_insert_entry(&smack_known_hat);
3921 smk_insert_entry(&smack_known_star);
3922 smk_insert_entry(&smack_known_floor);
3923 smk_insert_entry(&smack_known_invalid);
3924 smk_insert_entry(&smack_known_web);
3928 * smack_init - initialize the smack system
3932 static __init int smack_init(void)
3935 struct task_smack *tsp;
3937 if (!security_module_enable(&smack_ops))
3940 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3945 printk(KERN_INFO "Smack: Initializing.\n");
3948 * Set the security state for the initial task.
3950 cred = (struct cred *) current->cred;
3951 cred->security = tsp;
3953 /* initialize the smack_known_list */
3954 init_smack_known_list();
3959 if (register_security(&smack_ops))
3960 panic("smack: Unable to register with kernel.\n");
3966 * Smack requires early initialization in order to label
3967 * all processes and objects when they are created.
3969 security_initcall(smack_init);