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;
342 struct smack_known *skp;
349 if (sp->smk_initialized)
352 sp->smk_initialized = 1;
354 for (op = data; op != NULL; op = commap) {
355 commap = strchr(op, ',');
359 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
360 op += strlen(SMK_FSHAT);
361 nsp = smk_import(op, 0);
366 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
367 op += strlen(SMK_FSFLOOR);
368 nsp = smk_import(op, 0);
373 } else if (strncmp(op, SMK_FSDEFAULT,
374 strlen(SMK_FSDEFAULT)) == 0) {
375 op += strlen(SMK_FSDEFAULT);
376 nsp = smk_import(op, 0);
378 sp->smk_default = nsp;
381 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
382 op += strlen(SMK_FSROOT);
383 nsp = smk_import(op, 0);
388 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
389 op += strlen(SMK_FSTRANS);
390 nsp = smk_import(op, 0);
399 if (!smack_privileged(CAP_MAC_ADMIN)) {
401 * Unprivileged mounts don't get to specify Smack values.
406 * Unprivileged mounts get root and default from the caller.
408 skp = smk_of_current();
409 sp->smk_root = skp->smk_known;
410 sp->smk_default = skp->smk_known;
413 * Initialize the root inode.
415 isp = inode->i_security;
417 isp = new_inode_smack(sp->smk_root);
420 inode->i_security = isp;
422 isp->smk_inode = sp->smk_root;
425 isp->smk_flags |= SMK_INODE_TRANSMUTE;
431 * smack_sb_statfs - Smack check on statfs
432 * @dentry: identifies the file system in question
434 * Returns 0 if current can read the floor of the filesystem,
435 * and error code otherwise
437 static int smack_sb_statfs(struct dentry *dentry)
439 struct superblock_smack *sbp = dentry->d_sb->s_security;
441 struct smk_audit_info ad;
443 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
444 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
446 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
455 * smack_bprm_set_creds - set creds for exec
456 * @bprm: the exec information
458 * Returns 0 if it gets a blob, -ENOMEM otherwise
460 static int smack_bprm_set_creds(struct linux_binprm *bprm)
462 struct inode *inode = file_inode(bprm->file);
463 struct task_smack *bsp = bprm->cred->security;
464 struct inode_smack *isp;
467 rc = cap_bprm_set_creds(bprm);
471 if (bprm->cred_prepared)
474 isp = inode->i_security;
475 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
481 bsp->smk_task = isp->smk_task;
482 bprm->per_clear |= PER_CLEAR_ON_SETID;
488 * smack_bprm_committing_creds - Prepare to install the new credentials
491 * @bprm: binprm for exec
493 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
495 struct task_smack *bsp = bprm->cred->security;
497 if (bsp->smk_task != bsp->smk_forked)
498 current->pdeath_signal = 0;
502 * smack_bprm_secureexec - Return the decision to use secureexec.
503 * @bprm: binprm for exec
505 * Returns 0 on success.
507 static int smack_bprm_secureexec(struct linux_binprm *bprm)
509 struct task_smack *tsp = current_security();
510 int ret = cap_bprm_secureexec(bprm);
512 if (!ret && (tsp->smk_task != tsp->smk_forked))
523 * smack_inode_alloc_security - allocate an inode blob
524 * @inode: the inode in need of a blob
526 * Returns 0 if it gets a blob, -ENOMEM otherwise
528 static int smack_inode_alloc_security(struct inode *inode)
530 struct smack_known *skp = smk_of_current();
532 inode->i_security = new_inode_smack(skp->smk_known);
533 if (inode->i_security == NULL)
539 * smack_inode_free_security - free an inode blob
540 * @inode: the inode with a blob
542 * Clears the blob pointer in inode
544 static void smack_inode_free_security(struct inode *inode)
546 kfree(inode->i_security);
547 inode->i_security = NULL;
551 * smack_inode_init_security - copy out the smack from an inode
555 * @name: where to put the attribute name
556 * @value: where to put the attribute value
557 * @len: where to put the length of the attribute
559 * Returns 0 if it all works out, -ENOMEM if there's no memory
561 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
562 const struct qstr *qstr, const char **name,
563 void **value, size_t *len)
565 struct inode_smack *issp = inode->i_security;
566 struct smack_known *skp = smk_of_current();
567 char *isp = smk_of_inode(inode);
568 char *dsp = smk_of_inode(dir);
572 *name = XATTR_SMACK_SUFFIX;
576 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
580 * If the access rule allows transmutation and
581 * the directory requests transmutation then
582 * by all means transmute.
583 * Mark the inode as changed.
585 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
586 smk_inode_transmutable(dir)) {
588 issp->smk_flags |= SMK_INODE_CHANGED;
591 *value = kstrdup(isp, GFP_NOFS);
597 *len = strlen(isp) + 1;
603 * smack_inode_link - Smack check on link
604 * @old_dentry: the existing object
606 * @new_dentry: the new object
608 * Returns 0 if access is permitted, an error code otherwise
610 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
611 struct dentry *new_dentry)
614 struct smk_audit_info ad;
617 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
618 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
620 isp = smk_of_inode(old_dentry->d_inode);
621 rc = smk_curacc(isp, MAY_WRITE, &ad);
623 if (rc == 0 && new_dentry->d_inode != NULL) {
624 isp = smk_of_inode(new_dentry->d_inode);
625 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
626 rc = smk_curacc(isp, MAY_WRITE, &ad);
633 * smack_inode_unlink - Smack check on inode deletion
634 * @dir: containing directory object
635 * @dentry: file to unlink
637 * Returns 0 if current can write the containing directory
638 * and the object, error code otherwise
640 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
642 struct inode *ip = dentry->d_inode;
643 struct smk_audit_info ad;
646 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
647 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
650 * You need write access to the thing you're unlinking
652 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
655 * You also need write access to the containing directory
657 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
658 smk_ad_setfield_u_fs_inode(&ad, dir);
659 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
665 * smack_inode_rmdir - Smack check on directory deletion
666 * @dir: containing directory object
667 * @dentry: directory to unlink
669 * Returns 0 if current can write the containing directory
670 * and the directory, error code otherwise
672 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
674 struct smk_audit_info ad;
677 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
678 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
681 * You need write access to the thing you're removing
683 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
686 * You also need write access to the containing directory
688 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
689 smk_ad_setfield_u_fs_inode(&ad, dir);
690 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
697 * smack_inode_rename - Smack check on rename
698 * @old_inode: the old directory
699 * @old_dentry: unused
700 * @new_inode: the new directory
701 * @new_dentry: unused
703 * Read and write access is required on both the old and
706 * Returns 0 if access is permitted, an error code otherwise
708 static int smack_inode_rename(struct inode *old_inode,
709 struct dentry *old_dentry,
710 struct inode *new_inode,
711 struct dentry *new_dentry)
715 struct smk_audit_info ad;
717 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
718 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
720 isp = smk_of_inode(old_dentry->d_inode);
721 rc = smk_curacc(isp, MAY_READWRITE, &ad);
723 if (rc == 0 && new_dentry->d_inode != NULL) {
724 isp = smk_of_inode(new_dentry->d_inode);
725 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
726 rc = smk_curacc(isp, MAY_READWRITE, &ad);
732 * smack_inode_permission - Smack version of permission()
733 * @inode: the inode in question
734 * @mask: the access requested
736 * This is the important Smack hook.
738 * Returns 0 if access is permitted, -EACCES otherwise
740 static int smack_inode_permission(struct inode *inode, int mask)
742 struct smk_audit_info ad;
743 int no_block = mask & MAY_NOT_BLOCK;
745 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
747 * No permission to check. Existence test. Yup, it's there.
752 /* May be droppable after audit */
755 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
756 smk_ad_setfield_u_fs_inode(&ad, inode);
757 return smk_curacc(smk_of_inode(inode), mask, &ad);
761 * smack_inode_setattr - Smack check for setting attributes
762 * @dentry: the object
763 * @iattr: for the force flag
765 * Returns 0 if access is permitted, an error code otherwise
767 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
769 struct smk_audit_info ad;
771 * Need to allow for clearing the setuid bit.
773 if (iattr->ia_valid & ATTR_FORCE)
775 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
776 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
778 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
782 * smack_inode_getattr - Smack check for getting attributes
784 * @dentry: the object
786 * Returns 0 if access is permitted, an error code otherwise
788 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
790 struct smk_audit_info ad;
793 path.dentry = dentry;
796 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
797 smk_ad_setfield_u_fs_path(&ad, path);
798 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
802 * smack_inode_setxattr - Smack check for setting xattrs
803 * @dentry: the object
804 * @name: name of the attribute
809 * This protects the Smack attribute explicitly.
811 * Returns 0 if access is permitted, an error code otherwise
813 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
814 const void *value, size_t size, int flags)
816 struct smk_audit_info ad;
817 struct smack_known *skp;
819 int check_import = 0;
824 * Check label validity here so import won't fail in post_setxattr
826 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
827 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
828 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
831 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
832 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
836 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
838 if (size != TRANS_TRUE_SIZE ||
839 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
842 rc = cap_inode_setxattr(dentry, name, value, size, flags);
844 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
847 if (rc == 0 && check_import) {
848 skp = smk_import_entry(value, size);
849 if (skp == NULL || (check_star &&
850 (skp == &smack_known_star || skp == &smack_known_web)))
854 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
855 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
858 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
864 * smack_inode_post_setxattr - Apply the Smack update approved above
866 * @name: attribute name
867 * @value: attribute value
868 * @size: attribute size
871 * Set the pointer in the inode blob to the entry found
872 * in the master label list.
874 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
875 const void *value, size_t size, int flags)
877 struct smack_known *skp;
878 struct inode_smack *isp = dentry->d_inode->i_security;
880 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
881 isp->smk_flags |= SMK_INODE_TRANSMUTE;
885 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
886 skp = smk_import_entry(value, size);
888 isp->smk_inode = skp->smk_known;
890 isp->smk_inode = smack_known_invalid.smk_known;
891 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
892 skp = smk_import_entry(value, size);
896 isp->smk_task = &smack_known_invalid;
897 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
898 skp = smk_import_entry(value, size);
902 isp->smk_mmap = &smack_known_invalid;
909 * smack_inode_getxattr - Smack check on getxattr
910 * @dentry: the object
913 * Returns 0 if access is permitted, an error code otherwise
915 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
917 struct smk_audit_info ad;
919 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
920 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
922 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
926 * smack_inode_removexattr - Smack check on removexattr
927 * @dentry: the object
928 * @name: name of the attribute
930 * Removing the Smack attribute requires CAP_MAC_ADMIN
932 * Returns 0 if access is permitted, an error code otherwise
934 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
936 struct inode_smack *isp;
937 struct smk_audit_info ad;
940 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
941 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
942 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
943 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
944 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
945 strcmp(name, XATTR_NAME_SMACKMMAP)) {
946 if (!smack_privileged(CAP_MAC_ADMIN))
949 rc = cap_inode_removexattr(dentry, name);
951 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
952 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
954 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
957 isp = dentry->d_inode->i_security;
958 isp->smk_task = NULL;
959 isp->smk_mmap = NULL;
966 * smack_inode_getsecurity - get smack xattrs
968 * @name: attribute name
969 * @buffer: where to put the result
972 * Returns the size of the attribute or an error code
974 static int smack_inode_getsecurity(const struct inode *inode,
975 const char *name, void **buffer,
978 struct socket_smack *ssp;
980 struct super_block *sbp;
981 struct inode *ip = (struct inode *)inode;
986 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
987 isp = smk_of_inode(inode);
988 ilen = strlen(isp) + 1;
994 * The rest of the Smack xattrs are only on sockets.
997 if (sbp->s_magic != SOCKFS_MAGIC)
1000 sock = SOCKET_I(ip);
1001 if (sock == NULL || sock->sk == NULL)
1004 ssp = sock->sk->sk_security;
1006 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1008 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1009 isp = ssp->smk_out->smk_known;
1013 ilen = strlen(isp) + 1;
1024 * smack_inode_listsecurity - list the Smack attributes
1025 * @inode: the object
1026 * @buffer: where they go
1027 * @buffer_size: size of buffer
1029 * Returns 0 on success, -EINVAL otherwise
1031 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1034 int len = strlen(XATTR_NAME_SMACK);
1036 if (buffer != NULL && len <= buffer_size) {
1037 memcpy(buffer, XATTR_NAME_SMACK, len);
1044 * smack_inode_getsecid - Extract inode's security id
1045 * @inode: inode to extract the info from
1046 * @secid: where result will be saved
1048 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1050 struct inode_smack *isp = inode->i_security;
1052 *secid = smack_to_secid(isp->smk_inode);
1060 * smack_file_permission - Smack check on file operations
1066 * Should access checks be done on each read or write?
1067 * UNICOS and SELinux say yes.
1068 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1070 * I'll say no for now. Smack does not do the frequent
1071 * label changing that SELinux does.
1073 static int smack_file_permission(struct file *file, int mask)
1079 * smack_file_alloc_security - assign a file security blob
1082 * The security blob for a file is a pointer to the master
1083 * label list, so no allocation is done.
1087 static int smack_file_alloc_security(struct file *file)
1089 struct smack_known *skp = smk_of_current();
1091 file->f_security = skp->smk_known;
1096 * smack_file_free_security - clear a file security blob
1099 * The security blob for a file is a pointer to the master
1100 * label list, so no memory is freed.
1102 static void smack_file_free_security(struct file *file)
1104 file->f_security = NULL;
1108 * smack_file_ioctl - Smack check on ioctls
1113 * Relies heavily on the correct use of the ioctl command conventions.
1115 * Returns 0 if allowed, error code otherwise
1117 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1121 struct smk_audit_info ad;
1123 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1124 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1126 if (_IOC_DIR(cmd) & _IOC_WRITE)
1127 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1129 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1130 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1136 * smack_file_lock - Smack check on file locking
1140 * Returns 0 if current has lock access, error code otherwise
1142 static int smack_file_lock(struct file *file, unsigned int cmd)
1144 struct smk_audit_info ad;
1146 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1147 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1148 return smk_curacc(file->f_security, MAY_LOCK, &ad);
1152 * smack_file_fcntl - Smack check on fcntl
1154 * @cmd: what action to check
1157 * Generally these operations are harmless.
1158 * File locking operations present an obvious mechanism
1159 * for passing information, so they require write access.
1161 * Returns 0 if current has access, error code otherwise
1163 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1166 struct smk_audit_info ad;
1175 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1176 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1177 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1181 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1182 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1183 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1194 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1195 * if mapping anonymous memory.
1196 * @file contains the file structure for file to map (may be NULL).
1197 * @reqprot contains the protection requested by the application.
1198 * @prot contains the protection that will be applied by the kernel.
1199 * @flags contains the operational flags.
1200 * Return 0 if permission is granted.
1202 static int smack_mmap_file(struct file *file,
1203 unsigned long reqprot, unsigned long prot,
1204 unsigned long flags)
1206 struct smack_known *skp;
1207 struct smack_known *mkp;
1208 struct smack_rule *srp;
1209 struct task_smack *tsp;
1211 struct inode_smack *isp;
1220 isp = file_inode(file)->i_security;
1221 if (isp->smk_mmap == NULL)
1223 mkp = isp->smk_mmap;
1225 tsp = current_security();
1226 skp = smk_of_current();
1231 * For each Smack rule associated with the subject
1232 * label verify that the SMACK64MMAP also has access
1233 * to that rule's object label.
1235 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1236 osmack = srp->smk_object;
1238 * Matching labels always allows access.
1240 if (mkp->smk_known == osmack)
1243 * If there is a matching local rule take
1244 * that into account as well.
1246 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1249 may = srp->smk_access;
1251 may &= srp->smk_access;
1253 * If may is zero the SMACK64MMAP subject can't
1254 * possibly have less access.
1260 * Fetch the global list entry.
1261 * If there isn't one a SMACK64MMAP subject
1262 * can't have as much access as current.
1264 mmay = smk_access_entry(mkp->smk_known, osmack,
1266 if (mmay == -ENOENT) {
1271 * If there is a local entry it modifies the
1272 * potential access, too.
1274 tmay = smk_access_entry(mkp->smk_known, osmack,
1276 if (tmay != -ENOENT)
1280 * If there is any access available to current that is
1281 * not available to a SMACK64MMAP subject
1284 if ((may | mmay) != mmay) {
1296 * smack_file_set_fowner - set the file security blob value
1297 * @file: object in question
1300 * Further research may be required on this one.
1302 static int smack_file_set_fowner(struct file *file)
1304 struct smack_known *skp = smk_of_current();
1306 file->f_security = skp->smk_known;
1311 * smack_file_send_sigiotask - Smack on sigio
1312 * @tsk: The target task
1313 * @fown: the object the signal come from
1316 * Allow a privileged task to get signals even if it shouldn't
1318 * Returns 0 if a subject with the object's smack could
1319 * write to the task, an error code otherwise.
1321 static int smack_file_send_sigiotask(struct task_struct *tsk,
1322 struct fown_struct *fown, int signum)
1324 struct smack_known *skp;
1325 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1328 struct smk_audit_info ad;
1331 * struct fown_struct is never outside the context of a struct file
1333 file = container_of(fown, struct file, f_owner);
1335 /* we don't log here as rc can be overriden */
1336 skp = smk_find_entry(file->f_security);
1337 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1338 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1341 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1342 smk_ad_setfield_u_tsk(&ad, tsk);
1343 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1348 * smack_file_receive - Smack file receive check
1351 * Returns 0 if current has access, error code otherwise
1353 static int smack_file_receive(struct file *file)
1356 struct smk_audit_info ad;
1358 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1359 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1361 * This code relies on bitmasks.
1363 if (file->f_mode & FMODE_READ)
1365 if (file->f_mode & FMODE_WRITE)
1368 return smk_curacc(file->f_security, may, &ad);
1372 * smack_file_open - Smack dentry open processing
1376 * Set the security blob in the file structure.
1380 static int smack_file_open(struct file *file, const struct cred *cred)
1382 struct inode_smack *isp = file_inode(file)->i_security;
1384 file->f_security = isp->smk_inode;
1394 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1395 * @new: the new credentials
1396 * @gfp: the atomicity of any memory allocations
1398 * Prepare a blank set of credentials for modification. This must allocate all
1399 * the memory the LSM module might require such that cred_transfer() can
1400 * complete without error.
1402 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1404 struct task_smack *tsp;
1406 tsp = new_task_smack(NULL, NULL, gfp);
1410 cred->security = tsp;
1417 * smack_cred_free - "free" task-level security credentials
1418 * @cred: the credentials in question
1421 static void smack_cred_free(struct cred *cred)
1423 struct task_smack *tsp = cred->security;
1424 struct smack_rule *rp;
1425 struct list_head *l;
1426 struct list_head *n;
1430 cred->security = NULL;
1432 list_for_each_safe(l, n, &tsp->smk_rules) {
1433 rp = list_entry(l, struct smack_rule, list);
1434 list_del(&rp->list);
1441 * smack_cred_prepare - prepare new set of credentials for modification
1442 * @new: the new credentials
1443 * @old: the original credentials
1444 * @gfp: the atomicity of any memory allocations
1446 * Prepare a new set of credentials for modification.
1448 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1451 struct task_smack *old_tsp = old->security;
1452 struct task_smack *new_tsp;
1455 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1456 if (new_tsp == NULL)
1459 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1463 new->security = new_tsp;
1468 * smack_cred_transfer - Transfer the old credentials to the new credentials
1469 * @new: the new credentials
1470 * @old: the original credentials
1472 * Fill in a set of blank credentials from another set of credentials.
1474 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1476 struct task_smack *old_tsp = old->security;
1477 struct task_smack *new_tsp = new->security;
1479 new_tsp->smk_task = old_tsp->smk_task;
1480 new_tsp->smk_forked = old_tsp->smk_task;
1481 mutex_init(&new_tsp->smk_rules_lock);
1482 INIT_LIST_HEAD(&new_tsp->smk_rules);
1485 /* cbs copy rule list */
1489 * smack_kernel_act_as - Set the subjective context in a set of credentials
1490 * @new: points to the set of credentials to be modified.
1491 * @secid: specifies the security ID to be set
1493 * Set the security data for a kernel service.
1495 static int smack_kernel_act_as(struct cred *new, u32 secid)
1497 struct task_smack *new_tsp = new->security;
1498 struct smack_known *skp = smack_from_secid(secid);
1503 new_tsp->smk_task = skp;
1508 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1509 * @new: points to the set of credentials to be modified
1510 * @inode: points to the inode to use as a reference
1512 * Set the file creation context in a set of credentials to the same
1513 * as the objective context of the specified inode
1515 static int smack_kernel_create_files_as(struct cred *new,
1516 struct inode *inode)
1518 struct inode_smack *isp = inode->i_security;
1519 struct task_smack *tsp = new->security;
1521 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1522 tsp->smk_task = tsp->smk_forked;
1527 * smk_curacc_on_task - helper to log task related access
1528 * @p: the task object
1529 * @access: the access requested
1530 * @caller: name of the calling function for audit
1532 * Return 0 if access is permitted
1534 static int smk_curacc_on_task(struct task_struct *p, int access,
1537 struct smk_audit_info ad;
1538 struct smack_known *skp = smk_of_task(task_security(p));
1540 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1541 smk_ad_setfield_u_tsk(&ad, p);
1542 return smk_curacc(skp->smk_known, access, &ad);
1546 * smack_task_setpgid - Smack check on setting pgid
1547 * @p: the task object
1550 * Return 0 if write access is permitted
1552 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1554 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1558 * smack_task_getpgid - Smack access check for getpgid
1559 * @p: the object task
1561 * Returns 0 if current can read the object task, error code otherwise
1563 static int smack_task_getpgid(struct task_struct *p)
1565 return smk_curacc_on_task(p, MAY_READ, __func__);
1569 * smack_task_getsid - Smack access check for getsid
1570 * @p: the object task
1572 * Returns 0 if current can read the object task, error code otherwise
1574 static int smack_task_getsid(struct task_struct *p)
1576 return smk_curacc_on_task(p, MAY_READ, __func__);
1580 * smack_task_getsecid - get the secid of the task
1581 * @p: the object task
1582 * @secid: where to put the result
1584 * Sets the secid to contain a u32 version of the smack label.
1586 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1588 struct smack_known *skp = smk_of_task(task_security(p));
1590 *secid = skp->smk_secid;
1594 * smack_task_setnice - Smack check on setting nice
1595 * @p: the task object
1598 * Return 0 if write access is permitted
1600 static int smack_task_setnice(struct task_struct *p, int nice)
1604 rc = cap_task_setnice(p, nice);
1606 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1611 * smack_task_setioprio - Smack check on setting ioprio
1612 * @p: the task object
1615 * Return 0 if write access is permitted
1617 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1621 rc = cap_task_setioprio(p, ioprio);
1623 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1628 * smack_task_getioprio - Smack check on reading ioprio
1629 * @p: the task object
1631 * Return 0 if read access is permitted
1633 static int smack_task_getioprio(struct task_struct *p)
1635 return smk_curacc_on_task(p, MAY_READ, __func__);
1639 * smack_task_setscheduler - Smack check on setting scheduler
1640 * @p: the task object
1644 * Return 0 if read access is permitted
1646 static int smack_task_setscheduler(struct task_struct *p)
1650 rc = cap_task_setscheduler(p);
1652 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1657 * smack_task_getscheduler - Smack check on reading scheduler
1658 * @p: the task object
1660 * Return 0 if read access is permitted
1662 static int smack_task_getscheduler(struct task_struct *p)
1664 return smk_curacc_on_task(p, MAY_READ, __func__);
1668 * smack_task_movememory - Smack check on moving memory
1669 * @p: the task object
1671 * Return 0 if write access is permitted
1673 static int smack_task_movememory(struct task_struct *p)
1675 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1679 * smack_task_kill - Smack check on signal delivery
1680 * @p: the task object
1683 * @secid: identifies the smack to use in lieu of current's
1685 * Return 0 if write access is permitted
1687 * The secid behavior is an artifact of an SELinux hack
1688 * in the USB code. Someday it may go away.
1690 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1693 struct smk_audit_info ad;
1694 struct smack_known *skp;
1695 struct smack_known *tkp = smk_of_task(task_security(p));
1697 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1698 smk_ad_setfield_u_tsk(&ad, p);
1700 * Sending a signal requires that the sender
1701 * can write the receiver.
1704 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
1706 * If the secid isn't 0 we're dealing with some USB IO
1707 * specific behavior. This is not clean. For one thing
1708 * we can't take privilege into account.
1710 skp = smack_from_secid(secid);
1711 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
1715 * smack_task_wait - Smack access check for waiting
1716 * @p: task to wait for
1720 static int smack_task_wait(struct task_struct *p)
1723 * Allow the operation to succeed.
1725 * In userless environments (e.g. phones) programs
1726 * get marked with SMACK64EXEC and even if the parent
1727 * and child shouldn't be talking the parent still
1728 * may expect to know when the child exits.
1734 * smack_task_to_inode - copy task smack into the inode blob
1735 * @p: task to copy from
1736 * @inode: inode to copy to
1738 * Sets the smack pointer in the inode security blob
1740 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1742 struct inode_smack *isp = inode->i_security;
1743 struct smack_known *skp = smk_of_task(task_security(p));
1745 isp->smk_inode = skp->smk_known;
1753 * smack_sk_alloc_security - Allocate a socket blob
1756 * @gfp_flags: memory allocation flags
1758 * Assign Smack pointers to current
1760 * Returns 0 on success, -ENOMEM is there's no memory
1762 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1764 struct smack_known *skp = smk_of_current();
1765 struct socket_smack *ssp;
1767 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1771 ssp->smk_in = skp->smk_known;
1773 ssp->smk_packet = NULL;
1775 sk->sk_security = ssp;
1781 * smack_sk_free_security - Free a socket blob
1784 * Clears the blob pointer
1786 static void smack_sk_free_security(struct sock *sk)
1788 kfree(sk->sk_security);
1792 * smack_host_label - check host based restrictions
1793 * @sip: the object end
1795 * looks for host based access restrictions
1797 * This version will only be appropriate for really small sets of single label
1798 * hosts. The caller is responsible for ensuring that the RCU read lock is
1799 * taken before calling this function.
1801 * Returns the label of the far end or NULL if it's not special.
1803 static char *smack_host_label(struct sockaddr_in *sip)
1805 struct smk_netlbladdr *snp;
1806 struct in_addr *siap = &sip->sin_addr;
1808 if (siap->s_addr == 0)
1811 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1813 * we break after finding the first match because
1814 * the list is sorted from longest to shortest mask
1815 * so we have found the most specific match
1817 if ((&snp->smk_host.sin_addr)->s_addr ==
1818 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1819 /* we have found the special CIPSO option */
1820 if (snp->smk_label == smack_cipso_option)
1822 return snp->smk_label;
1829 * smack_netlabel - Set the secattr on a socket
1831 * @labeled: socket label scheme
1833 * Convert the outbound smack value (smk_out) to a
1834 * secattr and attach it to the socket.
1836 * Returns 0 on success or an error code
1838 static int smack_netlabel(struct sock *sk, int labeled)
1840 struct smack_known *skp;
1841 struct socket_smack *ssp = sk->sk_security;
1845 * Usually the netlabel code will handle changing the
1846 * packet labeling based on the label.
1847 * The case of a single label host is different, because
1848 * a single label host should never get a labeled packet
1849 * even though the label is usually associated with a packet
1853 bh_lock_sock_nested(sk);
1855 if (ssp->smk_out == smack_net_ambient ||
1856 labeled == SMACK_UNLABELED_SOCKET)
1857 netlbl_sock_delattr(sk);
1860 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
1870 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1872 * @sap: the destination address
1874 * Set the correct secattr for the given socket based on the destination
1875 * address and perform any outbound access checks needed.
1877 * Returns 0 on success or an error code.
1880 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1882 struct smack_known *skp;
1886 struct socket_smack *ssp = sk->sk_security;
1887 struct smk_audit_info ad;
1890 hostsp = smack_host_label(sap);
1891 if (hostsp != NULL) {
1893 struct lsm_network_audit net;
1895 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1896 ad.a.u.net->family = sap->sin_family;
1897 ad.a.u.net->dport = sap->sin_port;
1898 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
1900 sk_lbl = SMACK_UNLABELED_SOCKET;
1902 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
1904 sk_lbl = SMACK_CIPSO_SOCKET;
1911 return smack_netlabel(sk, sk_lbl);
1915 * smk_ipv6_port_label - Smack port access table management
1919 * Create or update the port list entry
1921 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1923 struct sock *sk = sock->sk;
1924 struct sockaddr_in6 *addr6;
1925 struct socket_smack *ssp = sock->sk->sk_security;
1926 struct smk_port_label *spp;
1927 unsigned short port = 0;
1929 if (address == NULL) {
1931 * This operation is changing the Smack information
1932 * on the bound socket. Take the changes to the port
1935 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1936 if (sk != spp->smk_sock)
1938 spp->smk_in = ssp->smk_in;
1939 spp->smk_out = ssp->smk_out;
1943 * A NULL address is only used for updating existing
1944 * bound entries. If there isn't one, it's OK.
1949 addr6 = (struct sockaddr_in6 *)address;
1950 port = ntohs(addr6->sin6_port);
1952 * This is a special case that is safely ignored.
1958 * Look for an existing port list entry.
1959 * This is an indication that a port is getting reused.
1961 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1962 if (spp->smk_port != port)
1964 spp->smk_port = port;
1966 spp->smk_in = ssp->smk_in;
1967 spp->smk_out = ssp->smk_out;
1972 * A new port entry is required.
1974 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1978 spp->smk_port = port;
1980 spp->smk_in = ssp->smk_in;
1981 spp->smk_out = ssp->smk_out;
1983 list_add(&spp->list, &smk_ipv6_port_list);
1988 * smk_ipv6_port_check - check Smack port access
1992 * Create or update the port list entry
1994 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
1999 struct smk_port_label *spp;
2000 struct socket_smack *ssp = sk->sk_security;
2001 struct smack_known *skp;
2002 unsigned short port = 0;
2004 struct smk_audit_info ad;
2006 struct lsm_network_audit net;
2009 if (act == SMK_RECEIVING) {
2010 skp = smack_net_ambient;
2011 object = ssp->smk_in;
2014 object = smack_net_ambient->smk_known;
2018 * Get the IP address and port from the address.
2020 port = ntohs(address->sin6_port);
2021 bep = (__be16 *)(&address->sin6_addr);
2022 be32p = (__be32 *)(&address->sin6_addr);
2025 * It's remote, so port lookup does no good.
2027 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2031 * It's local so the send check has to have passed.
2033 if (act == SMK_RECEIVING) {
2034 skp = &smack_known_web;
2038 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2039 if (spp->smk_port != port)
2041 object = spp->smk_in;
2042 if (act == SMK_CONNECTING)
2043 ssp->smk_packet = spp->smk_out->smk_known;
2050 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2051 ad.a.u.net->family = sk->sk_family;
2052 ad.a.u.net->dport = port;
2053 if (act == SMK_RECEIVING)
2054 ad.a.u.net->v6info.saddr = address->sin6_addr;
2056 ad.a.u.net->v6info.daddr = address->sin6_addr;
2058 return smk_access(skp, object, MAY_WRITE, &ad);
2062 * smack_inode_setsecurity - set smack xattrs
2063 * @inode: the object
2064 * @name: attribute name
2065 * @value: attribute value
2066 * @size: size of the attribute
2069 * Sets the named attribute in the appropriate blob
2071 * Returns 0 on success, or an error code
2073 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2074 const void *value, size_t size, int flags)
2076 struct smack_known *skp;
2077 struct inode_smack *nsp = inode->i_security;
2078 struct socket_smack *ssp;
2079 struct socket *sock;
2082 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2085 skp = smk_import_entry(value, size);
2089 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2090 nsp->smk_inode = skp->smk_known;
2091 nsp->smk_flags |= SMK_INODE_INSTANT;
2095 * The rest of the Smack xattrs are only on sockets.
2097 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2100 sock = SOCKET_I(inode);
2101 if (sock == NULL || sock->sk == NULL)
2104 ssp = sock->sk->sk_security;
2106 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2107 ssp->smk_in = skp->smk_known;
2108 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2110 if (sock->sk->sk_family == PF_INET) {
2111 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2114 "Smack: \"%s\" netlbl error %d.\n",
2120 if (sock->sk->sk_family == PF_INET6)
2121 smk_ipv6_port_label(sock, NULL);
2127 * smack_socket_post_create - finish socket setup
2129 * @family: protocol family
2134 * Sets the netlabel information on the socket
2136 * Returns 0 on success, and error code otherwise
2138 static int smack_socket_post_create(struct socket *sock, int family,
2139 int type, int protocol, int kern)
2141 if (family != PF_INET || sock->sk == NULL)
2144 * Set the outbound netlbl.
2146 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2150 * smack_socket_bind - record port binding information.
2152 * @address: the port address
2153 * @addrlen: size of the address
2155 * Records the label bound to a port.
2159 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2162 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2163 smk_ipv6_port_label(sock, address);
2169 * smack_socket_connect - connect access check
2171 * @sap: the other end
2172 * @addrlen: size of sap
2174 * Verifies that a connection may be possible
2176 * Returns 0 on success, and error code otherwise
2178 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2183 if (sock->sk == NULL)
2186 switch (sock->sk->sk_family) {
2188 if (addrlen < sizeof(struct sockaddr_in))
2190 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2193 if (addrlen < sizeof(struct sockaddr_in6))
2195 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2203 * smack_flags_to_may - convert S_ to MAY_ values
2204 * @flags: the S_ value
2206 * Returns the equivalent MAY_ value
2208 static int smack_flags_to_may(int flags)
2212 if (flags & S_IRUGO)
2214 if (flags & S_IWUGO)
2216 if (flags & S_IXUGO)
2223 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2228 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2230 struct smack_known *skp = smk_of_current();
2232 msg->security = skp->smk_known;
2237 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2240 * Clears the blob pointer
2242 static void smack_msg_msg_free_security(struct msg_msg *msg)
2244 msg->security = NULL;
2248 * smack_of_shm - the smack pointer for the shm
2251 * Returns a pointer to the smack value
2253 static char *smack_of_shm(struct shmid_kernel *shp)
2255 return (char *)shp->shm_perm.security;
2259 * smack_shm_alloc_security - Set the security blob for shm
2264 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2266 struct kern_ipc_perm *isp = &shp->shm_perm;
2267 struct smack_known *skp = smk_of_current();
2269 isp->security = skp->smk_known;
2274 * smack_shm_free_security - Clear the security blob for shm
2277 * Clears the blob pointer
2279 static void smack_shm_free_security(struct shmid_kernel *shp)
2281 struct kern_ipc_perm *isp = &shp->shm_perm;
2283 isp->security = NULL;
2287 * smk_curacc_shm : check if current has access on shm
2289 * @access : access requested
2291 * Returns 0 if current has the requested access, error code otherwise
2293 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2295 char *ssp = smack_of_shm(shp);
2296 struct smk_audit_info ad;
2299 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2300 ad.a.u.ipc_id = shp->shm_perm.id;
2302 return smk_curacc(ssp, access, &ad);
2306 * smack_shm_associate - Smack access check for shm
2308 * @shmflg: access requested
2310 * Returns 0 if current has the requested access, error code otherwise
2312 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2316 may = smack_flags_to_may(shmflg);
2317 return smk_curacc_shm(shp, may);
2321 * smack_shm_shmctl - Smack access check for shm
2323 * @cmd: what it wants to do
2325 * Returns 0 if current has the requested access, error code otherwise
2327 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2340 may = MAY_READWRITE;
2345 * System level information.
2351 return smk_curacc_shm(shp, may);
2355 * smack_shm_shmat - Smack access for shmat
2358 * @shmflg: access requested
2360 * Returns 0 if current has the requested access, error code otherwise
2362 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2367 may = smack_flags_to_may(shmflg);
2368 return smk_curacc_shm(shp, may);
2372 * smack_of_sem - the smack pointer for the sem
2375 * Returns a pointer to the smack value
2377 static char *smack_of_sem(struct sem_array *sma)
2379 return (char *)sma->sem_perm.security;
2383 * smack_sem_alloc_security - Set the security blob for sem
2388 static int smack_sem_alloc_security(struct sem_array *sma)
2390 struct kern_ipc_perm *isp = &sma->sem_perm;
2391 struct smack_known *skp = smk_of_current();
2393 isp->security = skp->smk_known;
2398 * smack_sem_free_security - Clear the security blob for sem
2401 * Clears the blob pointer
2403 static void smack_sem_free_security(struct sem_array *sma)
2405 struct kern_ipc_perm *isp = &sma->sem_perm;
2407 isp->security = NULL;
2411 * smk_curacc_sem : check if current has access on sem
2413 * @access : access requested
2415 * Returns 0 if current has the requested access, error code otherwise
2417 static int smk_curacc_sem(struct sem_array *sma, int access)
2419 char *ssp = smack_of_sem(sma);
2420 struct smk_audit_info ad;
2423 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2424 ad.a.u.ipc_id = sma->sem_perm.id;
2426 return smk_curacc(ssp, access, &ad);
2430 * smack_sem_associate - Smack access check for sem
2432 * @semflg: access requested
2434 * Returns 0 if current has the requested access, error code otherwise
2436 static int smack_sem_associate(struct sem_array *sma, int semflg)
2440 may = smack_flags_to_may(semflg);
2441 return smk_curacc_sem(sma, may);
2445 * smack_sem_shmctl - Smack access check for sem
2447 * @cmd: what it wants to do
2449 * Returns 0 if current has the requested access, error code otherwise
2451 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2469 may = MAY_READWRITE;
2474 * System level information
2481 return smk_curacc_sem(sma, may);
2485 * smack_sem_semop - Smack checks of semaphore operations
2491 * Treated as read and write in all cases.
2493 * Returns 0 if access is allowed, error code otherwise
2495 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2496 unsigned nsops, int alter)
2498 return smk_curacc_sem(sma, MAY_READWRITE);
2502 * smack_msg_alloc_security - Set the security blob for msg
2507 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2509 struct kern_ipc_perm *kisp = &msq->q_perm;
2510 struct smack_known *skp = smk_of_current();
2512 kisp->security = skp->smk_known;
2517 * smack_msg_free_security - Clear the security blob for msg
2520 * Clears the blob pointer
2522 static void smack_msg_queue_free_security(struct msg_queue *msq)
2524 struct kern_ipc_perm *kisp = &msq->q_perm;
2526 kisp->security = NULL;
2530 * smack_of_msq - the smack pointer for the msq
2533 * Returns a pointer to the smack value
2535 static char *smack_of_msq(struct msg_queue *msq)
2537 return (char *)msq->q_perm.security;
2541 * smk_curacc_msq : helper to check if current has access on msq
2543 * @access : access requested
2545 * return 0 if current has access, error otherwise
2547 static int smk_curacc_msq(struct msg_queue *msq, int access)
2549 char *msp = smack_of_msq(msq);
2550 struct smk_audit_info ad;
2553 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2554 ad.a.u.ipc_id = msq->q_perm.id;
2556 return smk_curacc(msp, access, &ad);
2560 * smack_msg_queue_associate - Smack access check for msg_queue
2562 * @msqflg: access requested
2564 * Returns 0 if current has the requested access, error code otherwise
2566 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2570 may = smack_flags_to_may(msqflg);
2571 return smk_curacc_msq(msq, may);
2575 * smack_msg_queue_msgctl - Smack access check for msg_queue
2577 * @cmd: what it wants to do
2579 * Returns 0 if current has the requested access, error code otherwise
2581 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2592 may = MAY_READWRITE;
2597 * System level information
2604 return smk_curacc_msq(msq, may);
2608 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2611 * @msqflg: access requested
2613 * Returns 0 if current has the requested access, error code otherwise
2615 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2620 may = smack_flags_to_may(msqflg);
2621 return smk_curacc_msq(msq, may);
2625 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2632 * Returns 0 if current has read and write access, error code otherwise
2634 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2635 struct task_struct *target, long type, int mode)
2637 return smk_curacc_msq(msq, MAY_READWRITE);
2641 * smack_ipc_permission - Smack access for ipc_permission()
2642 * @ipp: the object permissions
2643 * @flag: access requested
2645 * Returns 0 if current has read and write access, error code otherwise
2647 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2649 char *isp = ipp->security;
2650 int may = smack_flags_to_may(flag);
2651 struct smk_audit_info ad;
2654 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2655 ad.a.u.ipc_id = ipp->id;
2657 return smk_curacc(isp, may, &ad);
2661 * smack_ipc_getsecid - Extract smack security id
2662 * @ipp: the object permissions
2663 * @secid: where result will be saved
2665 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2667 char *smack = ipp->security;
2669 *secid = smack_to_secid(smack);
2673 * smack_d_instantiate - Make sure the blob is correct on an inode
2674 * @opt_dentry: dentry where inode will be attached
2675 * @inode: the object
2677 * Set the inode's security blob if it hasn't been done already.
2679 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2681 struct super_block *sbp;
2682 struct superblock_smack *sbsp;
2683 struct inode_smack *isp;
2684 struct smack_known *skp;
2685 struct smack_known *ckp = smk_of_current();
2687 char trattr[TRANS_TRUE_SIZE];
2695 isp = inode->i_security;
2697 mutex_lock(&isp->smk_lock);
2699 * If the inode is already instantiated
2700 * take the quick way out
2702 if (isp->smk_flags & SMK_INODE_INSTANT)
2706 sbsp = sbp->s_security;
2708 * We're going to use the superblock default label
2709 * if there's no label on the file.
2711 final = sbsp->smk_default;
2714 * If this is the root inode the superblock
2715 * may be in the process of initialization.
2716 * If that is the case use the root value out
2717 * of the superblock.
2719 if (opt_dentry->d_parent == opt_dentry) {
2720 if (sbp->s_magic == CGROUP_SUPER_MAGIC) {
2722 * The cgroup filesystem is never mounted,
2723 * so there's no opportunity to set the mount
2726 sbsp->smk_root = smack_known_star.smk_known;
2727 sbsp->smk_default = smack_known_star.smk_known;
2729 isp->smk_inode = sbsp->smk_root;
2730 isp->smk_flags |= SMK_INODE_INSTANT;
2735 * This is pretty hackish.
2736 * Casey says that we shouldn't have to do
2737 * file system specific code, but it does help
2738 * with keeping it simple.
2740 switch (sbp->s_magic) {
2744 case CGROUP_SUPER_MAGIC:
2746 * Casey says that it's a little embarrassing
2747 * that the smack file system doesn't do
2748 * extended attributes.
2750 * Casey says pipes are easy (?)
2752 * Socket access is controlled by the socket
2753 * structures associated with the task involved.
2755 * Cgroupfs is special
2757 final = smack_known_star.smk_known;
2759 case DEVPTS_SUPER_MAGIC:
2761 * devpts seems content with the label of the task.
2762 * Programs that change smack have to treat the
2765 final = ckp->smk_known;
2767 case PROC_SUPER_MAGIC:
2769 * Casey says procfs appears not to care.
2770 * The superblock default suffices.
2775 * Device labels should come from the filesystem,
2776 * but watch out, because they're volitile,
2777 * getting recreated on every reboot.
2779 final = smack_known_star.smk_known;
2783 * If a smack value has been set we want to use it,
2784 * but since tmpfs isn't giving us the opportunity
2785 * to set mount options simulate setting the
2786 * superblock default.
2790 * This isn't an understood special case.
2791 * Get the value from the xattr.
2795 * UNIX domain sockets use lower level socket data.
2797 if (S_ISSOCK(inode->i_mode)) {
2798 final = smack_known_star.smk_known;
2802 * No xattr support means, alas, no SMACK label.
2803 * Use the aforeapplied default.
2804 * It would be curious if the label of the task
2805 * does not match that assigned.
2807 if (inode->i_op->getxattr == NULL)
2810 * Get the dentry for xattr.
2812 dp = dget(opt_dentry);
2813 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2815 final = skp->smk_known;
2818 * Transmuting directory
2820 if (S_ISDIR(inode->i_mode)) {
2822 * If this is a new directory and the label was
2823 * transmuted when the inode was initialized
2824 * set the transmute attribute on the directory
2825 * and mark the inode.
2827 * If there is a transmute attribute on the
2828 * directory mark the inode.
2830 if (isp->smk_flags & SMK_INODE_CHANGED) {
2831 isp->smk_flags &= ~SMK_INODE_CHANGED;
2832 rc = inode->i_op->setxattr(dp,
2833 XATTR_NAME_SMACKTRANSMUTE,
2834 TRANS_TRUE, TRANS_TRUE_SIZE,
2837 rc = inode->i_op->getxattr(dp,
2838 XATTR_NAME_SMACKTRANSMUTE, trattr,
2840 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2841 TRANS_TRUE_SIZE) != 0)
2845 transflag = SMK_INODE_TRANSMUTE;
2848 * Don't let the exec or mmap label be "*" or "@".
2850 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2851 if (skp == &smack_known_star || skp == &smack_known_web)
2853 isp->smk_task = skp;
2854 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2855 if (skp == &smack_known_star || skp == &smack_known_web)
2857 isp->smk_mmap = skp;
2864 isp->smk_inode = ckp->smk_known;
2866 isp->smk_inode = final;
2868 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2871 mutex_unlock(&isp->smk_lock);
2876 * smack_getprocattr - Smack process attribute access
2877 * @p: the object task
2878 * @name: the name of the attribute in /proc/.../attr
2879 * @value: where to put the result
2881 * Places a copy of the task Smack into value
2883 * Returns the length of the smack label or an error code
2885 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2887 struct smack_known *skp = smk_of_task(task_security(p));
2891 if (strcmp(name, "current") != 0)
2894 cp = kstrdup(skp->smk_known, GFP_KERNEL);
2904 * smack_setprocattr - Smack process attribute setting
2905 * @p: the object task
2906 * @name: the name of the attribute in /proc/.../attr
2907 * @value: the value to set
2908 * @size: the size of the value
2910 * Sets the Smack value of the task. Only setting self
2911 * is permitted and only with privilege
2913 * Returns the length of the smack label or an error code
2915 static int smack_setprocattr(struct task_struct *p, char *name,
2916 void *value, size_t size)
2918 struct task_smack *tsp;
2920 struct smack_known *skp;
2923 * Changing another process' Smack value is too dangerous
2924 * and supports no sane use case.
2929 if (!smack_privileged(CAP_MAC_ADMIN))
2932 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
2935 if (strcmp(name, "current") != 0)
2938 skp = smk_import_entry(value, size);
2943 * No process is ever allowed the web ("@") label.
2945 if (skp == &smack_known_web)
2948 new = prepare_creds();
2952 tsp = new->security;
2953 tsp->smk_task = skp;
2960 * smack_unix_stream_connect - Smack access on UDS
2962 * @other: the other sock
2965 * Return 0 if a subject with the smack of sock could access
2966 * an object with the smack of other, otherwise an error code
2968 static int smack_unix_stream_connect(struct sock *sock,
2969 struct sock *other, struct sock *newsk)
2971 struct smack_known *skp;
2972 struct socket_smack *ssp = sock->sk_security;
2973 struct socket_smack *osp = other->sk_security;
2974 struct socket_smack *nsp = newsk->sk_security;
2975 struct smk_audit_info ad;
2979 struct lsm_network_audit net;
2981 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2982 smk_ad_setfield_u_net_sk(&ad, other);
2985 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
2987 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
2991 * Cross reference the peer labels for SO_PEERSEC.
2994 nsp->smk_packet = ssp->smk_out->smk_known;
2995 ssp->smk_packet = osp->smk_out->smk_known;
3002 * smack_unix_may_send - Smack access on UDS
3004 * @other: the other socket
3006 * Return 0 if a subject with the smack of sock could access
3007 * an object with the smack of other, otherwise an error code
3009 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3011 struct socket_smack *ssp = sock->sk->sk_security;
3012 struct socket_smack *osp = other->sk->sk_security;
3013 struct smack_known *skp;
3014 struct smk_audit_info ad;
3017 struct lsm_network_audit net;
3019 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3020 smk_ad_setfield_u_net_sk(&ad, other->sk);
3023 if (smack_privileged(CAP_MAC_OVERRIDE))
3027 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3031 * smack_socket_sendmsg - Smack check based on destination host
3034 * @size: the size of the message
3036 * Return 0 if the current subject can write to the destination host.
3037 * For IPv4 this is only a question if the destination is a single label host.
3038 * For IPv6 this is a check against the label of the port.
3040 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3043 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3044 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3048 * Perfectly reasonable for this to be NULL
3053 switch (sip->sin_family) {
3055 rc = smack_netlabel_send(sock->sk, sip);
3058 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3065 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3066 * @sap: netlabel secattr
3067 * @ssp: socket security information
3069 * Returns a pointer to a Smack label entry found on the label list.
3071 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3072 struct socket_smack *ssp)
3074 struct smack_known *skp;
3079 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3081 * Looks like a CIPSO packet.
3082 * If there are flags but no level netlabel isn't
3083 * behaving the way we expect it to.
3085 * Look it up in the label table
3086 * Without guidance regarding the smack value
3087 * for the packet fall back on the network
3091 list_for_each_entry(skp, &smack_known_list, list) {
3092 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3095 * Compare the catsets. Use the netlbl APIs.
3097 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3098 if ((skp->smk_netlabel.flags &
3099 NETLBL_SECATTR_MLS_CAT) == 0)
3103 for (acat = -1, kcat = -1; acat == kcat; ) {
3104 acat = netlbl_secattr_catmap_walk(
3105 sap->attr.mls.cat, acat + 1);
3106 kcat = netlbl_secattr_catmap_walk(
3107 skp->smk_netlabel.attr.mls.cat,
3109 if (acat < 0 || kcat < 0)
3122 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
3123 return &smack_known_web;
3124 return &smack_known_star;
3126 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3128 * Looks like a fallback, which gives us a secid.
3130 skp = smack_from_secid(sap->attr.secid);
3132 * This has got to be a bug because it is
3133 * impossible to specify a fallback without
3134 * specifying the label, which will ensure
3135 * it has a secid, and the only way to get a
3136 * secid is from a fallback.
3138 BUG_ON(skp == NULL);
3142 * Without guidance regarding the smack value
3143 * for the packet fall back on the network
3146 return smack_net_ambient;
3149 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3153 int proto = -EINVAL;
3154 struct ipv6hdr _ipv6h;
3155 struct ipv6hdr *ip6;
3157 struct tcphdr _tcph, *th;
3158 struct udphdr _udph, *uh;
3159 struct dccp_hdr _dccph, *dh;
3163 offset = skb_network_offset(skb);
3164 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3167 sip->sin6_addr = ip6->saddr;
3169 nexthdr = ip6->nexthdr;
3170 offset += sizeof(_ipv6h);
3171 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3178 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3180 sip->sin6_port = th->source;
3183 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3185 sip->sin6_port = uh->source;
3188 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3190 sip->sin6_port = dh->dccph_sport;
3197 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3201 * Returns 0 if the packet should be delivered, an error code otherwise
3203 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3205 struct netlbl_lsm_secattr secattr;
3206 struct socket_smack *ssp = sk->sk_security;
3207 struct smack_known *skp;
3208 struct sockaddr_in6 sadd;
3210 struct smk_audit_info ad;
3212 struct lsm_network_audit net;
3214 switch (sk->sk_family) {
3217 * Translate what netlabel gave us.
3219 netlbl_secattr_init(&secattr);
3221 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3223 skp = smack_from_secattr(&secattr, ssp);
3225 skp = smack_net_ambient;
3227 netlbl_secattr_destroy(&secattr);
3230 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3231 ad.a.u.net->family = sk->sk_family;
3232 ad.a.u.net->netif = skb->skb_iif;
3233 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3236 * Receiving a packet requires that the other end
3237 * be able to write here. Read access is not required.
3238 * This is the simplist possible security model
3241 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3243 netlbl_skbuff_err(skb, rc, 0);
3246 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3247 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3248 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3257 * smack_socket_getpeersec_stream - pull in packet label
3259 * @optval: user's destination
3260 * @optlen: size thereof
3263 * returns zero on success, an error code otherwise
3265 static int smack_socket_getpeersec_stream(struct socket *sock,
3266 char __user *optval,
3267 int __user *optlen, unsigned len)
3269 struct socket_smack *ssp;
3274 ssp = sock->sk->sk_security;
3275 if (ssp->smk_packet != NULL) {
3276 rcp = ssp->smk_packet;
3277 slen = strlen(rcp) + 1;
3282 else if (copy_to_user(optval, rcp, slen) != 0)
3285 if (put_user(slen, optlen) != 0)
3293 * smack_socket_getpeersec_dgram - pull in packet label
3294 * @sock: the peer socket
3296 * @secid: pointer to where to put the secid of the packet
3298 * Sets the netlabel socket state on sk from parent
3300 static int smack_socket_getpeersec_dgram(struct socket *sock,
3301 struct sk_buff *skb, u32 *secid)
3304 struct netlbl_lsm_secattr secattr;
3305 struct socket_smack *ssp = NULL;
3306 struct smack_known *skp;
3307 int family = PF_UNSPEC;
3308 u32 s = 0; /* 0 is the invalid secid */
3312 if (skb->protocol == htons(ETH_P_IP))
3314 else if (skb->protocol == htons(ETH_P_IPV6))
3317 if (family == PF_UNSPEC && sock != NULL)
3318 family = sock->sk->sk_family;
3320 if (family == PF_UNIX) {
3321 ssp = sock->sk->sk_security;
3322 s = ssp->smk_out->smk_secid;
3323 } else if (family == PF_INET || family == PF_INET6) {
3325 * Translate what netlabel gave us.
3327 if (sock != NULL && sock->sk != NULL)
3328 ssp = sock->sk->sk_security;
3329 netlbl_secattr_init(&secattr);
3330 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3332 skp = smack_from_secattr(&secattr, ssp);
3335 netlbl_secattr_destroy(&secattr);
3344 * smack_sock_graft - Initialize a newly created socket with an existing sock
3346 * @parent: parent socket
3348 * Set the smk_{in,out} state of an existing sock based on the process that
3349 * is creating the new socket.
3351 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3353 struct socket_smack *ssp;
3354 struct smack_known *skp = smk_of_current();
3357 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3360 ssp = sk->sk_security;
3361 ssp->smk_in = skp->smk_known;
3363 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3367 * smack_inet_conn_request - Smack access check on connect
3368 * @sk: socket involved
3372 * Returns 0 if a task with the packet label could write to
3373 * the socket, otherwise an error code
3375 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3376 struct request_sock *req)
3378 u16 family = sk->sk_family;
3379 struct smack_known *skp;
3380 struct socket_smack *ssp = sk->sk_security;
3381 struct netlbl_lsm_secattr secattr;
3382 struct sockaddr_in addr;
3386 struct smk_audit_info ad;
3388 struct lsm_network_audit net;
3391 if (family == PF_INET6) {
3393 * Handle mapped IPv4 packets arriving
3394 * via IPv6 sockets. Don't set up netlabel
3395 * processing on IPv6.
3397 if (skb->protocol == htons(ETH_P_IP))
3403 netlbl_secattr_init(&secattr);
3404 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3406 skp = smack_from_secattr(&secattr, ssp);
3408 skp = &smack_known_huh;
3409 netlbl_secattr_destroy(&secattr);
3412 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3413 ad.a.u.net->family = family;
3414 ad.a.u.net->netif = skb->skb_iif;
3415 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3418 * Receiving a packet requires that the other end be able to write
3419 * here. Read access is not required.
3421 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3426 * Save the peer's label in the request_sock so we can later setup
3427 * smk_packet in the child socket so that SO_PEERCRED can report it.
3429 req->peer_secid = skp->smk_secid;
3432 * We need to decide if we want to label the incoming connection here
3433 * if we do we only need to label the request_sock and the stack will
3434 * propagate the wire-label to the sock when it is created.
3437 addr.sin_addr.s_addr = hdr->saddr;
3439 hsp = smack_host_label(&addr);
3443 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3445 netlbl_req_delattr(req);
3451 * smack_inet_csk_clone - Copy the connection information to the new socket
3452 * @sk: the new socket
3453 * @req: the connection's request_sock
3455 * Transfer the connection's peer label to the newly created socket.
3457 static void smack_inet_csk_clone(struct sock *sk,
3458 const struct request_sock *req)
3460 struct socket_smack *ssp = sk->sk_security;
3461 struct smack_known *skp;
3463 if (req->peer_secid != 0) {
3464 skp = smack_from_secid(req->peer_secid);
3465 ssp->smk_packet = skp->smk_known;
3467 ssp->smk_packet = NULL;
3471 * Key management security hooks
3473 * Casey has not tested key support very heavily.
3474 * The permission check is most likely too restrictive.
3475 * If you care about keys please have a look.
3480 * smack_key_alloc - Set the key security blob
3482 * @cred: the credentials to use
3485 * No allocation required
3489 static int smack_key_alloc(struct key *key, const struct cred *cred,
3490 unsigned long flags)
3492 struct smack_known *skp = smk_of_task(cred->security);
3494 key->security = skp->smk_known;
3499 * smack_key_free - Clear the key security blob
3502 * Clear the blob pointer
3504 static void smack_key_free(struct key *key)
3506 key->security = NULL;
3510 * smack_key_permission - Smack access on a key
3511 * @key_ref: gets to the object
3512 * @cred: the credentials to use
3515 * Return 0 if the task has read and write to the object,
3516 * an error code otherwise
3518 static int smack_key_permission(key_ref_t key_ref,
3519 const struct cred *cred, unsigned perm)
3522 struct smk_audit_info ad;
3523 struct smack_known *tkp = smk_of_task(cred->security);
3526 keyp = key_ref_to_ptr(key_ref);
3530 * If the key hasn't been initialized give it access so that
3533 if (keyp->security == NULL)
3536 * This should not occur
3541 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3542 ad.a.u.key_struct.key = keyp->serial;
3543 ad.a.u.key_struct.key_desc = keyp->description;
3545 if (perm & KEY_NEED_READ)
3547 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
3548 request = MAY_WRITE;
3549 return smk_access(tkp, keyp->security, request, &ad);
3551 #endif /* CONFIG_KEYS */
3556 * Audit requires a unique representation of each Smack specific
3557 * rule. This unique representation is used to distinguish the
3558 * object to be audited from remaining kernel objects and also
3559 * works as a glue between the audit hooks.
3561 * Since repository entries are added but never deleted, we'll use
3562 * the smack_known label address related to the given audit rule as
3563 * the needed unique representation. This also better fits the smack
3564 * model where nearly everything is a label.
3569 * smack_audit_rule_init - Initialize a smack audit rule
3570 * @field: audit rule fields given from user-space (audit.h)
3571 * @op: required testing operator (=, !=, >, <, ...)
3572 * @rulestr: smack label to be audited
3573 * @vrule: pointer to save our own audit rule representation
3575 * Prepare to audit cases where (@field @op @rulestr) is true.
3576 * The label to be audited is created if necessay.
3578 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3580 char **rule = (char **)vrule;
3583 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3586 if (op != Audit_equal && op != Audit_not_equal)
3589 *rule = smk_import(rulestr, 0);
3595 * smack_audit_rule_known - Distinguish Smack audit rules
3596 * @krule: rule of interest, in Audit kernel representation format
3598 * This is used to filter Smack rules from remaining Audit ones.
3599 * If it's proved that this rule belongs to us, the
3600 * audit_rule_match hook will be called to do the final judgement.
3602 static int smack_audit_rule_known(struct audit_krule *krule)
3604 struct audit_field *f;
3607 for (i = 0; i < krule->field_count; i++) {
3608 f = &krule->fields[i];
3610 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3618 * smack_audit_rule_match - Audit given object ?
3619 * @secid: security id for identifying the object to test
3620 * @field: audit rule flags given from user-space
3621 * @op: required testing operator
3622 * @vrule: smack internal rule presentation
3623 * @actx: audit context associated with the check
3625 * The core Audit hook. It's used to take the decision of
3626 * whether to audit or not to audit a given object.
3628 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3629 struct audit_context *actx)
3631 struct smack_known *skp;
3634 if (unlikely(!rule)) {
3635 WARN_ONCE(1, "Smack: missing rule\n");
3639 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3642 skp = smack_from_secid(secid);
3645 * No need to do string comparisons. If a match occurs,
3646 * both pointers will point to the same smack_known
3649 if (op == Audit_equal)
3650 return (rule == skp->smk_known);
3651 if (op == Audit_not_equal)
3652 return (rule != skp->smk_known);
3658 * smack_audit_rule_free - free smack rule representation
3659 * @vrule: rule to be freed.
3661 * No memory was allocated.
3663 static void smack_audit_rule_free(void *vrule)
3668 #endif /* CONFIG_AUDIT */
3671 * smack_ismaclabel - check if xattr @name references a smack MAC label
3672 * @name: Full xattr name to check.
3674 static int smack_ismaclabel(const char *name)
3676 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3681 * smack_secid_to_secctx - return the smack label for a secid
3682 * @secid: incoming integer
3683 * @secdata: destination
3684 * @seclen: how long it is
3686 * Exists for networking code.
3688 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3690 struct smack_known *skp = smack_from_secid(secid);
3693 *secdata = skp->smk_known;
3694 *seclen = strlen(skp->smk_known);
3699 * smack_secctx_to_secid - return the secid for a smack label
3700 * @secdata: smack label
3701 * @seclen: how long result is
3702 * @secid: outgoing integer
3704 * Exists for audit and networking code.
3706 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3708 *secid = smack_to_secid(secdata);
3713 * smack_release_secctx - don't do anything.
3717 * Exists to make sure nothing gets done, and properly
3719 static void smack_release_secctx(char *secdata, u32 seclen)
3723 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3725 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3728 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3730 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3733 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3736 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3744 struct security_operations smack_ops = {
3747 .ptrace_access_check = smack_ptrace_access_check,
3748 .ptrace_traceme = smack_ptrace_traceme,
3749 .syslog = smack_syslog,
3751 .sb_alloc_security = smack_sb_alloc_security,
3752 .sb_free_security = smack_sb_free_security,
3753 .sb_copy_data = smack_sb_copy_data,
3754 .sb_kern_mount = smack_sb_kern_mount,
3755 .sb_statfs = smack_sb_statfs,
3757 .bprm_set_creds = smack_bprm_set_creds,
3758 .bprm_committing_creds = smack_bprm_committing_creds,
3759 .bprm_secureexec = smack_bprm_secureexec,
3761 .inode_alloc_security = smack_inode_alloc_security,
3762 .inode_free_security = smack_inode_free_security,
3763 .inode_init_security = smack_inode_init_security,
3764 .inode_link = smack_inode_link,
3765 .inode_unlink = smack_inode_unlink,
3766 .inode_rmdir = smack_inode_rmdir,
3767 .inode_rename = smack_inode_rename,
3768 .inode_permission = smack_inode_permission,
3769 .inode_setattr = smack_inode_setattr,
3770 .inode_getattr = smack_inode_getattr,
3771 .inode_setxattr = smack_inode_setxattr,
3772 .inode_post_setxattr = smack_inode_post_setxattr,
3773 .inode_getxattr = smack_inode_getxattr,
3774 .inode_removexattr = smack_inode_removexattr,
3775 .inode_getsecurity = smack_inode_getsecurity,
3776 .inode_setsecurity = smack_inode_setsecurity,
3777 .inode_listsecurity = smack_inode_listsecurity,
3778 .inode_getsecid = smack_inode_getsecid,
3780 .file_permission = smack_file_permission,
3781 .file_alloc_security = smack_file_alloc_security,
3782 .file_free_security = smack_file_free_security,
3783 .file_ioctl = smack_file_ioctl,
3784 .file_lock = smack_file_lock,
3785 .file_fcntl = smack_file_fcntl,
3786 .mmap_file = smack_mmap_file,
3787 .mmap_addr = cap_mmap_addr,
3788 .file_set_fowner = smack_file_set_fowner,
3789 .file_send_sigiotask = smack_file_send_sigiotask,
3790 .file_receive = smack_file_receive,
3792 .file_open = smack_file_open,
3794 .cred_alloc_blank = smack_cred_alloc_blank,
3795 .cred_free = smack_cred_free,
3796 .cred_prepare = smack_cred_prepare,
3797 .cred_transfer = smack_cred_transfer,
3798 .kernel_act_as = smack_kernel_act_as,
3799 .kernel_create_files_as = smack_kernel_create_files_as,
3800 .task_setpgid = smack_task_setpgid,
3801 .task_getpgid = smack_task_getpgid,
3802 .task_getsid = smack_task_getsid,
3803 .task_getsecid = smack_task_getsecid,
3804 .task_setnice = smack_task_setnice,
3805 .task_setioprio = smack_task_setioprio,
3806 .task_getioprio = smack_task_getioprio,
3807 .task_setscheduler = smack_task_setscheduler,
3808 .task_getscheduler = smack_task_getscheduler,
3809 .task_movememory = smack_task_movememory,
3810 .task_kill = smack_task_kill,
3811 .task_wait = smack_task_wait,
3812 .task_to_inode = smack_task_to_inode,
3814 .ipc_permission = smack_ipc_permission,
3815 .ipc_getsecid = smack_ipc_getsecid,
3817 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3818 .msg_msg_free_security = smack_msg_msg_free_security,
3820 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3821 .msg_queue_free_security = smack_msg_queue_free_security,
3822 .msg_queue_associate = smack_msg_queue_associate,
3823 .msg_queue_msgctl = smack_msg_queue_msgctl,
3824 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3825 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3827 .shm_alloc_security = smack_shm_alloc_security,
3828 .shm_free_security = smack_shm_free_security,
3829 .shm_associate = smack_shm_associate,
3830 .shm_shmctl = smack_shm_shmctl,
3831 .shm_shmat = smack_shm_shmat,
3833 .sem_alloc_security = smack_sem_alloc_security,
3834 .sem_free_security = smack_sem_free_security,
3835 .sem_associate = smack_sem_associate,
3836 .sem_semctl = smack_sem_semctl,
3837 .sem_semop = smack_sem_semop,
3839 .d_instantiate = smack_d_instantiate,
3841 .getprocattr = smack_getprocattr,
3842 .setprocattr = smack_setprocattr,
3844 .unix_stream_connect = smack_unix_stream_connect,
3845 .unix_may_send = smack_unix_may_send,
3847 .socket_post_create = smack_socket_post_create,
3848 .socket_bind = smack_socket_bind,
3849 .socket_connect = smack_socket_connect,
3850 .socket_sendmsg = smack_socket_sendmsg,
3851 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3852 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3853 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3854 .sk_alloc_security = smack_sk_alloc_security,
3855 .sk_free_security = smack_sk_free_security,
3856 .sock_graft = smack_sock_graft,
3857 .inet_conn_request = smack_inet_conn_request,
3858 .inet_csk_clone = smack_inet_csk_clone,
3860 /* key management security hooks */
3862 .key_alloc = smack_key_alloc,
3863 .key_free = smack_key_free,
3864 .key_permission = smack_key_permission,
3865 #endif /* CONFIG_KEYS */
3869 .audit_rule_init = smack_audit_rule_init,
3870 .audit_rule_known = smack_audit_rule_known,
3871 .audit_rule_match = smack_audit_rule_match,
3872 .audit_rule_free = smack_audit_rule_free,
3873 #endif /* CONFIG_AUDIT */
3875 .ismaclabel = smack_ismaclabel,
3876 .secid_to_secctx = smack_secid_to_secctx,
3877 .secctx_to_secid = smack_secctx_to_secid,
3878 .release_secctx = smack_release_secctx,
3879 .inode_notifysecctx = smack_inode_notifysecctx,
3880 .inode_setsecctx = smack_inode_setsecctx,
3881 .inode_getsecctx = smack_inode_getsecctx,
3885 static __init void init_smack_known_list(void)
3888 * Initialize rule list locks
3890 mutex_init(&smack_known_huh.smk_rules_lock);
3891 mutex_init(&smack_known_hat.smk_rules_lock);
3892 mutex_init(&smack_known_floor.smk_rules_lock);
3893 mutex_init(&smack_known_star.smk_rules_lock);
3894 mutex_init(&smack_known_invalid.smk_rules_lock);
3895 mutex_init(&smack_known_web.smk_rules_lock);
3897 * Initialize rule lists
3899 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3900 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3901 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3902 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3903 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3904 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3906 * Create the known labels list
3908 smk_insert_entry(&smack_known_huh);
3909 smk_insert_entry(&smack_known_hat);
3910 smk_insert_entry(&smack_known_star);
3911 smk_insert_entry(&smack_known_floor);
3912 smk_insert_entry(&smack_known_invalid);
3913 smk_insert_entry(&smack_known_web);
3917 * smack_init - initialize the smack system
3921 static __init int smack_init(void)
3924 struct task_smack *tsp;
3926 if (!security_module_enable(&smack_ops))
3929 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3934 printk(KERN_INFO "Smack: Initializing.\n");
3937 * Set the security state for the initial task.
3939 cred = (struct cred *) current->cred;
3940 cred->security = tsp;
3942 /* initialize the smack_known_list */
3943 init_smack_known_list();
3948 if (register_security(&smack_ops))
3949 panic("smack: Unable to register with kernel.\n");
3955 * Smack requires early initialization in order to label
3956 * all processes and objects when they are created.
3958 security_initcall(smack_init);