diff options
author | James Morris <jmorris@namei.org> | 2011-08-08 20:31:03 -0400 |
---|---|---|
committer | James Morris <jmorris@namei.org> | 2011-08-08 20:31:03 -0400 |
commit | 5a2f3a02aea164f4f59c0c3497772090a411b462 (patch) | |
tree | d3ebe03d4f97575290087843960baa01de3acd0a /security/integrity/evm | |
parent | 1d568ab068c021672d6cd7f50f92a3695a921ffb (diff) | |
parent | 817b54aa45db03437c6d09a7693fc6926eb8e822 (diff) |
Merge branch 'next-evm' of git://git.kernel.org/pub/scm/linux/kernel/git/zohar/ima-2.6 into next
Conflicts:
fs/attr.c
Resolve conflict manually.
Signed-off-by: James Morris <jmorris@namei.org>
Diffstat (limited to 'security/integrity/evm')
-rw-r--r-- | security/integrity/evm/Kconfig | 12 | ||||
-rw-r--r-- | security/integrity/evm/Makefile | 6 | ||||
-rw-r--r-- | security/integrity/evm/evm.h | 38 | ||||
-rw-r--r-- | security/integrity/evm/evm_crypto.c | 216 | ||||
-rw-r--r-- | security/integrity/evm/evm_main.c | 384 | ||||
-rw-r--r-- | security/integrity/evm/evm_secfs.c | 108 |
6 files changed, 764 insertions, 0 deletions
diff --git a/security/integrity/evm/Kconfig b/security/integrity/evm/Kconfig new file mode 100644 index 000000000000..73f654099a4b --- /dev/null +++ b/security/integrity/evm/Kconfig | |||
@@ -0,0 +1,12 @@ | |||
1 | config EVM | ||
2 | boolean "EVM support" | ||
3 | depends on SECURITY && KEYS && ENCRYPTED_KEYS | ||
4 | select CRYPTO_HMAC | ||
5 | select CRYPTO_MD5 | ||
6 | select CRYPTO_SHA1 | ||
7 | default n | ||
8 | help | ||
9 | EVM protects a file's security extended attributes against | ||
10 | integrity attacks. | ||
11 | |||
12 | If you are unsure how to answer this question, answer N. | ||
diff --git a/security/integrity/evm/Makefile b/security/integrity/evm/Makefile new file mode 100644 index 000000000000..0787d262b9e3 --- /dev/null +++ b/security/integrity/evm/Makefile | |||
@@ -0,0 +1,6 @@ | |||
1 | # | ||
2 | # Makefile for building the Extended Verification Module(EVM) | ||
3 | # | ||
4 | obj-$(CONFIG_EVM) += evm.o | ||
5 | |||
6 | evm-y := evm_main.o evm_crypto.o evm_secfs.o | ||
diff --git a/security/integrity/evm/evm.h b/security/integrity/evm/evm.h new file mode 100644 index 000000000000..d320f5197437 --- /dev/null +++ b/security/integrity/evm/evm.h | |||
@@ -0,0 +1,38 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2005-2010 IBM Corporation | ||
3 | * | ||
4 | * Authors: | ||
5 | * Mimi Zohar <zohar@us.ibm.com> | ||
6 | * Kylene Hall <kjhall@us.ibm.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation, version 2 of the License. | ||
11 | * | ||
12 | * File: evm.h | ||
13 | * | ||
14 | */ | ||
15 | #include <linux/xattr.h> | ||
16 | #include <linux/security.h> | ||
17 | #include "../integrity.h" | ||
18 | |||
19 | extern int evm_initialized; | ||
20 | extern char *evm_hmac; | ||
21 | |||
22 | extern struct crypto_shash *hmac_tfm; | ||
23 | |||
24 | /* List of EVM protected security xattrs */ | ||
25 | extern char *evm_config_xattrnames[]; | ||
26 | |||
27 | extern int evm_init_key(void); | ||
28 | extern int evm_update_evmxattr(struct dentry *dentry, | ||
29 | const char *req_xattr_name, | ||
30 | const char *req_xattr_value, | ||
31 | size_t req_xattr_value_len); | ||
32 | extern int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name, | ||
33 | const char *req_xattr_value, | ||
34 | size_t req_xattr_value_len, char *digest); | ||
35 | extern int evm_init_hmac(struct inode *inode, const struct xattr *xattr, | ||
36 | char *hmac_val); | ||
37 | extern int evm_init_secfs(void); | ||
38 | extern void evm_cleanup_secfs(void); | ||
diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c new file mode 100644 index 000000000000..5dd5b140242c --- /dev/null +++ b/security/integrity/evm/evm_crypto.c | |||
@@ -0,0 +1,216 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2005-2010 IBM Corporation | ||
3 | * | ||
4 | * Authors: | ||
5 | * Mimi Zohar <zohar@us.ibm.com> | ||
6 | * Kylene Hall <kjhall@us.ibm.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation, version 2 of the License. | ||
11 | * | ||
12 | * File: evm_crypto.c | ||
13 | * Using root's kernel master key (kmk), calculate the HMAC | ||
14 | */ | ||
15 | |||
16 | #include <linux/module.h> | ||
17 | #include <linux/crypto.h> | ||
18 | #include <linux/xattr.h> | ||
19 | #include <keys/encrypted-type.h> | ||
20 | #include <crypto/hash.h> | ||
21 | #include "evm.h" | ||
22 | |||
23 | #define EVMKEY "evm-key" | ||
24 | #define MAX_KEY_SIZE 128 | ||
25 | static unsigned char evmkey[MAX_KEY_SIZE]; | ||
26 | static int evmkey_len = MAX_KEY_SIZE; | ||
27 | |||
28 | struct crypto_shash *hmac_tfm; | ||
29 | |||
30 | static struct shash_desc *init_desc(void) | ||
31 | { | ||
32 | int rc; | ||
33 | struct shash_desc *desc; | ||
34 | |||
35 | if (hmac_tfm == NULL) { | ||
36 | hmac_tfm = crypto_alloc_shash(evm_hmac, 0, CRYPTO_ALG_ASYNC); | ||
37 | if (IS_ERR(hmac_tfm)) { | ||
38 | pr_err("Can not allocate %s (reason: %ld)\n", | ||
39 | evm_hmac, PTR_ERR(hmac_tfm)); | ||
40 | rc = PTR_ERR(hmac_tfm); | ||
41 | hmac_tfm = NULL; | ||
42 | return ERR_PTR(rc); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac_tfm), | ||
47 | GFP_KERNEL); | ||
48 | if (!desc) | ||
49 | return ERR_PTR(-ENOMEM); | ||
50 | |||
51 | desc->tfm = hmac_tfm; | ||
52 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; | ||
53 | |||
54 | rc = crypto_shash_setkey(hmac_tfm, evmkey, evmkey_len); | ||
55 | if (rc) | ||
56 | goto out; | ||
57 | rc = crypto_shash_init(desc); | ||
58 | out: | ||
59 | if (rc) { | ||
60 | kfree(desc); | ||
61 | return ERR_PTR(rc); | ||
62 | } | ||
63 | return desc; | ||
64 | } | ||
65 | |||
66 | /* Protect against 'cutting & pasting' security.evm xattr, include inode | ||
67 | * specific info. | ||
68 | * | ||
69 | * (Additional directory/file metadata needs to be added for more complete | ||
70 | * protection.) | ||
71 | */ | ||
72 | static void hmac_add_misc(struct shash_desc *desc, struct inode *inode, | ||
73 | char *digest) | ||
74 | { | ||
75 | struct h_misc { | ||
76 | unsigned long ino; | ||
77 | __u32 generation; | ||
78 | uid_t uid; | ||
79 | gid_t gid; | ||
80 | umode_t mode; | ||
81 | } hmac_misc; | ||
82 | |||
83 | memset(&hmac_misc, 0, sizeof hmac_misc); | ||
84 | hmac_misc.ino = inode->i_ino; | ||
85 | hmac_misc.generation = inode->i_generation; | ||
86 | hmac_misc.uid = inode->i_uid; | ||
87 | hmac_misc.gid = inode->i_gid; | ||
88 | hmac_misc.mode = inode->i_mode; | ||
89 | crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc); | ||
90 | crypto_shash_final(desc, digest); | ||
91 | } | ||
92 | |||
93 | /* | ||
94 | * Calculate the HMAC value across the set of protected security xattrs. | ||
95 | * | ||
96 | * Instead of retrieving the requested xattr, for performance, calculate | ||
97 | * the hmac using the requested xattr value. Don't alloc/free memory for | ||
98 | * each xattr, but attempt to re-use the previously allocated memory. | ||
99 | */ | ||
100 | int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name, | ||
101 | const char *req_xattr_value, size_t req_xattr_value_len, | ||
102 | char *digest) | ||
103 | { | ||
104 | struct inode *inode = dentry->d_inode; | ||
105 | struct shash_desc *desc; | ||
106 | char **xattrname; | ||
107 | size_t xattr_size = 0; | ||
108 | char *xattr_value = NULL; | ||
109 | int error; | ||
110 | int size; | ||
111 | |||
112 | if (!inode->i_op || !inode->i_op->getxattr) | ||
113 | return -EOPNOTSUPP; | ||
114 | desc = init_desc(); | ||
115 | if (IS_ERR(desc)) | ||
116 | return PTR_ERR(desc); | ||
117 | |||
118 | error = -ENODATA; | ||
119 | for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) { | ||
120 | if ((req_xattr_name && req_xattr_value) | ||
121 | && !strcmp(*xattrname, req_xattr_name)) { | ||
122 | error = 0; | ||
123 | crypto_shash_update(desc, (const u8 *)req_xattr_value, | ||
124 | req_xattr_value_len); | ||
125 | continue; | ||
126 | } | ||
127 | size = vfs_getxattr_alloc(dentry, *xattrname, | ||
128 | &xattr_value, xattr_size, GFP_NOFS); | ||
129 | if (size == -ENOMEM) { | ||
130 | error = -ENOMEM; | ||
131 | goto out; | ||
132 | } | ||
133 | if (size < 0) | ||
134 | continue; | ||
135 | |||
136 | error = 0; | ||
137 | xattr_size = size; | ||
138 | crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size); | ||
139 | } | ||
140 | hmac_add_misc(desc, inode, digest); | ||
141 | |||
142 | out: | ||
143 | kfree(xattr_value); | ||
144 | kfree(desc); | ||
145 | return error; | ||
146 | } | ||
147 | |||
148 | /* | ||
149 | * Calculate the hmac and update security.evm xattr | ||
150 | * | ||
151 | * Expects to be called with i_mutex locked. | ||
152 | */ | ||
153 | int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name, | ||
154 | const char *xattr_value, size_t xattr_value_len) | ||
155 | { | ||
156 | struct inode *inode = dentry->d_inode; | ||
157 | struct evm_ima_xattr_data xattr_data; | ||
158 | int rc = 0; | ||
159 | |||
160 | rc = evm_calc_hmac(dentry, xattr_name, xattr_value, | ||
161 | xattr_value_len, xattr_data.digest); | ||
162 | if (rc == 0) { | ||
163 | xattr_data.type = EVM_XATTR_HMAC; | ||
164 | rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM, | ||
165 | &xattr_data, | ||
166 | sizeof(xattr_data), 0); | ||
167 | } | ||
168 | else if (rc == -ENODATA) | ||
169 | rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM); | ||
170 | return rc; | ||
171 | } | ||
172 | |||
173 | int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr, | ||
174 | char *hmac_val) | ||
175 | { | ||
176 | struct shash_desc *desc; | ||
177 | |||
178 | desc = init_desc(); | ||
179 | if (IS_ERR(desc)) { | ||
180 | printk(KERN_INFO "init_desc failed\n"); | ||
181 | return PTR_ERR(desc); | ||
182 | } | ||
183 | |||
184 | crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len); | ||
185 | hmac_add_misc(desc, inode, hmac_val); | ||
186 | kfree(desc); | ||
187 | return 0; | ||
188 | } | ||
189 | |||
190 | /* | ||
191 | * Get the key from the TPM for the SHA1-HMAC | ||
192 | */ | ||
193 | int evm_init_key(void) | ||
194 | { | ||
195 | struct key *evm_key; | ||
196 | struct encrypted_key_payload *ekp; | ||
197 | int rc = 0; | ||
198 | |||
199 | evm_key = request_key(&key_type_encrypted, EVMKEY, NULL); | ||
200 | if (IS_ERR(evm_key)) | ||
201 | return -ENOENT; | ||
202 | |||
203 | down_read(&evm_key->sem); | ||
204 | ekp = evm_key->payload.data; | ||
205 | if (ekp->decrypted_datalen > MAX_KEY_SIZE) { | ||
206 | rc = -EINVAL; | ||
207 | goto out; | ||
208 | } | ||
209 | memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen); | ||
210 | out: | ||
211 | /* burn the original key contents */ | ||
212 | memset(ekp->decrypted_data, 0, ekp->decrypted_datalen); | ||
213 | up_read(&evm_key->sem); | ||
214 | key_put(evm_key); | ||
215 | return rc; | ||
216 | } | ||
diff --git a/security/integrity/evm/evm_main.c b/security/integrity/evm/evm_main.c new file mode 100644 index 000000000000..8fc5b5d7ceaa --- /dev/null +++ b/security/integrity/evm/evm_main.c | |||
@@ -0,0 +1,384 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2005-2010 IBM Corporation | ||
3 | * | ||
4 | * Author: | ||
5 | * Mimi Zohar <zohar@us.ibm.com> | ||
6 | * Kylene Hall <kjhall@us.ibm.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation, version 2 of the License. | ||
11 | * | ||
12 | * File: evm_main.c | ||
13 | * implements evm_inode_setxattr, evm_inode_post_setxattr, | ||
14 | * evm_inode_removexattr, and evm_verifyxattr | ||
15 | */ | ||
16 | |||
17 | #include <linux/module.h> | ||
18 | #include <linux/crypto.h> | ||
19 | #include <linux/xattr.h> | ||
20 | #include <linux/integrity.h> | ||
21 | #include <linux/evm.h> | ||
22 | #include <crypto/hash.h> | ||
23 | #include "evm.h" | ||
24 | |||
25 | int evm_initialized; | ||
26 | |||
27 | char *evm_hmac = "hmac(sha1)"; | ||
28 | |||
29 | char *evm_config_xattrnames[] = { | ||
30 | #ifdef CONFIG_SECURITY_SELINUX | ||
31 | XATTR_NAME_SELINUX, | ||
32 | #endif | ||
33 | #ifdef CONFIG_SECURITY_SMACK | ||
34 | XATTR_NAME_SMACK, | ||
35 | #endif | ||
36 | XATTR_NAME_CAPS, | ||
37 | NULL | ||
38 | }; | ||
39 | |||
40 | static int evm_fixmode; | ||
41 | static int __init evm_set_fixmode(char *str) | ||
42 | { | ||
43 | if (strncmp(str, "fix", 3) == 0) | ||
44 | evm_fixmode = 1; | ||
45 | return 0; | ||
46 | } | ||
47 | __setup("evm=", evm_set_fixmode); | ||
48 | |||
49 | /* | ||
50 | * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr | ||
51 | * | ||
52 | * Compute the HMAC on the dentry's protected set of extended attributes | ||
53 | * and compare it against the stored security.evm xattr. | ||
54 | * | ||
55 | * For performance: | ||
56 | * - use the previoulsy retrieved xattr value and length to calculate the | ||
57 | * HMAC.) | ||
58 | * - cache the verification result in the iint, when available. | ||
59 | * | ||
60 | * Returns integrity status | ||
61 | */ | ||
62 | static enum integrity_status evm_verify_hmac(struct dentry *dentry, | ||
63 | const char *xattr_name, | ||
64 | char *xattr_value, | ||
65 | size_t xattr_value_len, | ||
66 | struct integrity_iint_cache *iint) | ||
67 | { | ||
68 | struct evm_ima_xattr_data xattr_data; | ||
69 | enum integrity_status evm_status; | ||
70 | int rc; | ||
71 | |||
72 | if (iint && iint->evm_status == INTEGRITY_PASS) | ||
73 | return iint->evm_status; | ||
74 | |||
75 | /* if status is not PASS, try to check again - against -ENOMEM */ | ||
76 | |||
77 | rc = evm_calc_hmac(dentry, xattr_name, xattr_value, | ||
78 | xattr_value_len, xattr_data.digest); | ||
79 | if (rc < 0) | ||
80 | goto err_out; | ||
81 | |||
82 | xattr_data.type = EVM_XATTR_HMAC; | ||
83 | rc = vfs_xattr_cmp(dentry, XATTR_NAME_EVM, (u8 *)&xattr_data, | ||
84 | sizeof xattr_data, GFP_NOFS); | ||
85 | if (rc < 0) | ||
86 | goto err_out; | ||
87 | evm_status = INTEGRITY_PASS; | ||
88 | goto out; | ||
89 | |||
90 | err_out: | ||
91 | switch (rc) { | ||
92 | case -ENODATA: /* file not labelled */ | ||
93 | evm_status = INTEGRITY_NOLABEL; | ||
94 | break; | ||
95 | default: | ||
96 | evm_status = INTEGRITY_FAIL; | ||
97 | } | ||
98 | out: | ||
99 | if (iint) | ||
100 | iint->evm_status = evm_status; | ||
101 | return evm_status; | ||
102 | } | ||
103 | |||
104 | static int evm_protected_xattr(const char *req_xattr_name) | ||
105 | { | ||
106 | char **xattrname; | ||
107 | int namelen; | ||
108 | int found = 0; | ||
109 | |||
110 | namelen = strlen(req_xattr_name); | ||
111 | for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) { | ||
112 | if ((strlen(*xattrname) == namelen) | ||
113 | && (strncmp(req_xattr_name, *xattrname, namelen) == 0)) { | ||
114 | found = 1; | ||
115 | break; | ||
116 | } | ||
117 | if (strncmp(req_xattr_name, | ||
118 | *xattrname + XATTR_SECURITY_PREFIX_LEN, | ||
119 | strlen(req_xattr_name)) == 0) { | ||
120 | found = 1; | ||
121 | break; | ||
122 | } | ||
123 | } | ||
124 | return found; | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * evm_verifyxattr - verify the integrity of the requested xattr | ||
129 | * @dentry: object of the verify xattr | ||
130 | * @xattr_name: requested xattr | ||
131 | * @xattr_value: requested xattr value | ||
132 | * @xattr_value_len: requested xattr value length | ||
133 | * | ||
134 | * Calculate the HMAC for the given dentry and verify it against the stored | ||
135 | * security.evm xattr. For performance, use the xattr value and length | ||
136 | * previously retrieved to calculate the HMAC. | ||
137 | * | ||
138 | * Returns the xattr integrity status. | ||
139 | * | ||
140 | * This function requires the caller to lock the inode's i_mutex before it | ||
141 | * is executed. | ||
142 | */ | ||
143 | enum integrity_status evm_verifyxattr(struct dentry *dentry, | ||
144 | const char *xattr_name, | ||
145 | void *xattr_value, size_t xattr_value_len, | ||
146 | struct integrity_iint_cache *iint) | ||
147 | { | ||
148 | if (!evm_initialized || !evm_protected_xattr(xattr_name)) | ||
149 | return INTEGRITY_UNKNOWN; | ||
150 | |||
151 | if (!iint) { | ||
152 | iint = integrity_iint_find(dentry->d_inode); | ||
153 | if (!iint) | ||
154 | return INTEGRITY_UNKNOWN; | ||
155 | } | ||
156 | return evm_verify_hmac(dentry, xattr_name, xattr_value, | ||
157 | xattr_value_len, iint); | ||
158 | } | ||
159 | EXPORT_SYMBOL_GPL(evm_verifyxattr); | ||
160 | |||
161 | /* | ||
162 | * evm_protect_xattr - protect the EVM extended attribute | ||
163 | * | ||
164 | * Prevent security.evm from being modified or removed. | ||
165 | */ | ||
166 | static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name, | ||
167 | const void *xattr_value, size_t xattr_value_len) | ||
168 | { | ||
169 | if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) { | ||
170 | if (!capable(CAP_SYS_ADMIN)) | ||
171 | return -EPERM; | ||
172 | } | ||
173 | return 0; | ||
174 | } | ||
175 | |||
176 | /* | ||
177 | * evm_verify_current_integrity - verify the dentry's metadata integrity | ||
178 | * @dentry: pointer to the affected dentry | ||
179 | * | ||
180 | * Verify and return the dentry's metadata integrity. The exceptions are | ||
181 | * before EVM is initialized or in 'fix' mode. | ||
182 | */ | ||
183 | static enum integrity_status evm_verify_current_integrity(struct dentry *dentry) | ||
184 | { | ||
185 | struct inode *inode = dentry->d_inode; | ||
186 | |||
187 | if (!evm_initialized || !S_ISREG(inode->i_mode) || evm_fixmode) | ||
188 | return 0; | ||
189 | return evm_verify_hmac(dentry, NULL, NULL, 0, NULL); | ||
190 | } | ||
191 | |||
192 | /** | ||
193 | * evm_inode_setxattr - protect the EVM extended attribute | ||
194 | * @dentry: pointer to the affected dentry | ||
195 | * @xattr_name: pointer to the affected extended attribute name | ||
196 | * @xattr_value: pointer to the new extended attribute value | ||
197 | * @xattr_value_len: pointer to the new extended attribute value length | ||
198 | * | ||
199 | * Updating 'security.evm' requires CAP_SYS_ADMIN privileges and that | ||
200 | * the current value is valid. | ||
201 | */ | ||
202 | int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name, | ||
203 | const void *xattr_value, size_t xattr_value_len) | ||
204 | { | ||
205 | |||
206 | enum integrity_status evm_status; | ||
207 | int ret; | ||
208 | |||
209 | ret = evm_protect_xattr(dentry, xattr_name, xattr_value, | ||
210 | xattr_value_len); | ||
211 | if (ret) | ||
212 | return ret; | ||
213 | evm_status = evm_verify_current_integrity(dentry); | ||
214 | return evm_status == INTEGRITY_PASS ? 0 : -EPERM; | ||
215 | } | ||
216 | |||
217 | /** | ||
218 | * evm_inode_removexattr - protect the EVM extended attribute | ||
219 | * @dentry: pointer to the affected dentry | ||
220 | * @xattr_name: pointer to the affected extended attribute name | ||
221 | * | ||
222 | * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that | ||
223 | * the current value is valid. | ||
224 | */ | ||
225 | int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name) | ||
226 | { | ||
227 | enum integrity_status evm_status; | ||
228 | int ret; | ||
229 | |||
230 | ret = evm_protect_xattr(dentry, xattr_name, NULL, 0); | ||
231 | if (ret) | ||
232 | return ret; | ||
233 | evm_status = evm_verify_current_integrity(dentry); | ||
234 | return evm_status == INTEGRITY_PASS ? 0 : -EPERM; | ||
235 | } | ||
236 | |||
237 | /** | ||
238 | * evm_inode_post_setxattr - update 'security.evm' to reflect the changes | ||
239 | * @dentry: pointer to the affected dentry | ||
240 | * @xattr_name: pointer to the affected extended attribute name | ||
241 | * @xattr_value: pointer to the new extended attribute value | ||
242 | * @xattr_value_len: pointer to the new extended attribute value length | ||
243 | * | ||
244 | * Update the HMAC stored in 'security.evm' to reflect the change. | ||
245 | * | ||
246 | * No need to take the i_mutex lock here, as this function is called from | ||
247 | * __vfs_setxattr_noperm(). The caller of which has taken the inode's | ||
248 | * i_mutex lock. | ||
249 | */ | ||
250 | void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name, | ||
251 | const void *xattr_value, size_t xattr_value_len) | ||
252 | { | ||
253 | if (!evm_initialized || !evm_protected_xattr(xattr_name)) | ||
254 | return; | ||
255 | |||
256 | evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len); | ||
257 | return; | ||
258 | } | ||
259 | |||
260 | /** | ||
261 | * evm_inode_post_removexattr - update 'security.evm' after removing the xattr | ||
262 | * @dentry: pointer to the affected dentry | ||
263 | * @xattr_name: pointer to the affected extended attribute name | ||
264 | * | ||
265 | * Update the HMAC stored in 'security.evm' to reflect removal of the xattr. | ||
266 | */ | ||
267 | void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name) | ||
268 | { | ||
269 | struct inode *inode = dentry->d_inode; | ||
270 | |||
271 | if (!evm_initialized || !evm_protected_xattr(xattr_name)) | ||
272 | return; | ||
273 | |||
274 | mutex_lock(&inode->i_mutex); | ||
275 | evm_update_evmxattr(dentry, xattr_name, NULL, 0); | ||
276 | mutex_unlock(&inode->i_mutex); | ||
277 | return; | ||
278 | } | ||
279 | |||
280 | /** | ||
281 | * evm_inode_setattr - prevent updating an invalid EVM extended attribute | ||
282 | * @dentry: pointer to the affected dentry | ||
283 | */ | ||
284 | int evm_inode_setattr(struct dentry *dentry, struct iattr *attr) | ||
285 | { | ||
286 | unsigned int ia_valid = attr->ia_valid; | ||
287 | enum integrity_status evm_status; | ||
288 | |||
289 | if (ia_valid & ~(ATTR_MODE | ATTR_UID | ATTR_GID)) | ||
290 | return 0; | ||
291 | evm_status = evm_verify_current_integrity(dentry); | ||
292 | return evm_status == INTEGRITY_PASS ? 0 : -EPERM; | ||
293 | } | ||
294 | |||
295 | /** | ||
296 | * evm_inode_post_setattr - update 'security.evm' after modifying metadata | ||
297 | * @dentry: pointer to the affected dentry | ||
298 | * @ia_valid: for the UID and GID status | ||
299 | * | ||
300 | * For now, update the HMAC stored in 'security.evm' to reflect UID/GID | ||
301 | * changes. | ||
302 | * | ||
303 | * This function is called from notify_change(), which expects the caller | ||
304 | * to lock the inode's i_mutex. | ||
305 | */ | ||
306 | void evm_inode_post_setattr(struct dentry *dentry, int ia_valid) | ||
307 | { | ||
308 | if (!evm_initialized) | ||
309 | return; | ||
310 | |||
311 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) | ||
312 | evm_update_evmxattr(dentry, NULL, NULL, 0); | ||
313 | return; | ||
314 | } | ||
315 | |||
316 | /* | ||
317 | * evm_inode_init_security - initializes security.evm | ||
318 | */ | ||
319 | int evm_inode_init_security(struct inode *inode, | ||
320 | const struct xattr *lsm_xattr, | ||
321 | struct xattr *evm_xattr) | ||
322 | { | ||
323 | struct evm_ima_xattr_data *xattr_data; | ||
324 | int rc; | ||
325 | |||
326 | if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name)) | ||
327 | return -EOPNOTSUPP; | ||
328 | |||
329 | xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS); | ||
330 | if (!xattr_data) | ||
331 | return -ENOMEM; | ||
332 | |||
333 | xattr_data->type = EVM_XATTR_HMAC; | ||
334 | rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest); | ||
335 | if (rc < 0) | ||
336 | goto out; | ||
337 | |||
338 | evm_xattr->value = xattr_data; | ||
339 | evm_xattr->value_len = sizeof(*xattr_data); | ||
340 | evm_xattr->name = kstrdup(XATTR_EVM_SUFFIX, GFP_NOFS); | ||
341 | return 0; | ||
342 | out: | ||
343 | kfree(xattr_data); | ||
344 | return rc; | ||
345 | } | ||
346 | EXPORT_SYMBOL_GPL(evm_inode_init_security); | ||
347 | |||
348 | static int __init init_evm(void) | ||
349 | { | ||
350 | int error; | ||
351 | |||
352 | error = evm_init_secfs(); | ||
353 | if (error < 0) { | ||
354 | printk(KERN_INFO "EVM: Error registering secfs\n"); | ||
355 | goto err; | ||
356 | } | ||
357 | err: | ||
358 | return error; | ||
359 | } | ||
360 | |||
361 | static void __exit cleanup_evm(void) | ||
362 | { | ||
363 | evm_cleanup_secfs(); | ||
364 | if (hmac_tfm) | ||
365 | crypto_free_shash(hmac_tfm); | ||
366 | } | ||
367 | |||
368 | /* | ||
369 | * evm_display_config - list the EVM protected security extended attributes | ||
370 | */ | ||
371 | static int __init evm_display_config(void) | ||
372 | { | ||
373 | char **xattrname; | ||
374 | |||
375 | for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) | ||
376 | printk(KERN_INFO "EVM: %s\n", *xattrname); | ||
377 | return 0; | ||
378 | } | ||
379 | |||
380 | pure_initcall(evm_display_config); | ||
381 | late_initcall(init_evm); | ||
382 | |||
383 | MODULE_DESCRIPTION("Extended Verification Module"); | ||
384 | MODULE_LICENSE("GPL"); | ||
diff --git a/security/integrity/evm/evm_secfs.c b/security/integrity/evm/evm_secfs.c new file mode 100644 index 000000000000..ac7629950578 --- /dev/null +++ b/security/integrity/evm/evm_secfs.c | |||
@@ -0,0 +1,108 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2010 IBM Corporation | ||
3 | * | ||
4 | * Authors: | ||
5 | * Mimi Zohar <zohar@us.ibm.com> | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, version 2 of the License. | ||
10 | * | ||
11 | * File: evm_secfs.c | ||
12 | * - Used to signal when key is on keyring | ||
13 | * - Get the key and enable EVM | ||
14 | */ | ||
15 | |||
16 | #include <linux/uaccess.h> | ||
17 | #include <linux/module.h> | ||
18 | #include "evm.h" | ||
19 | |||
20 | static struct dentry *evm_init_tpm; | ||
21 | |||
22 | /** | ||
23 | * evm_read_key - read() for <securityfs>/evm | ||
24 | * | ||
25 | * @filp: file pointer, not actually used | ||
26 | * @buf: where to put the result | ||
27 | * @count: maximum to send along | ||
28 | * @ppos: where to start | ||
29 | * | ||
30 | * Returns number of bytes read or error code, as appropriate | ||
31 | */ | ||
32 | static ssize_t evm_read_key(struct file *filp, char __user *buf, | ||
33 | size_t count, loff_t *ppos) | ||
34 | { | ||
35 | char temp[80]; | ||
36 | ssize_t rc; | ||
37 | |||
38 | if (*ppos != 0) | ||
39 | return 0; | ||
40 | |||
41 | sprintf(temp, "%d", evm_initialized); | ||
42 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | ||
43 | |||
44 | return rc; | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * evm_write_key - write() for <securityfs>/evm | ||
49 | * @file: file pointer, not actually used | ||
50 | * @buf: where to get the data from | ||
51 | * @count: bytes sent | ||
52 | * @ppos: where to start | ||
53 | * | ||
54 | * Used to signal that key is on the kernel key ring. | ||
55 | * - get the integrity hmac key from the kernel key ring | ||
56 | * - create list of hmac protected extended attributes | ||
57 | * Returns number of bytes written or error code, as appropriate | ||
58 | */ | ||
59 | static ssize_t evm_write_key(struct file *file, const char __user *buf, | ||
60 | size_t count, loff_t *ppos) | ||
61 | { | ||
62 | char temp[80]; | ||
63 | int i, error; | ||
64 | |||
65 | if (!capable(CAP_SYS_ADMIN) || evm_initialized) | ||
66 | return -EPERM; | ||
67 | |||
68 | if (count >= sizeof(temp) || count == 0) | ||
69 | return -EINVAL; | ||
70 | |||
71 | if (copy_from_user(temp, buf, count) != 0) | ||
72 | return -EFAULT; | ||
73 | |||
74 | temp[count] = '\0'; | ||
75 | |||
76 | if ((sscanf(temp, "%d", &i) != 1) || (i != 1)) | ||
77 | return -EINVAL; | ||
78 | |||
79 | error = evm_init_key(); | ||
80 | if (!error) { | ||
81 | evm_initialized = 1; | ||
82 | pr_info("EVM: initialized\n"); | ||
83 | } else | ||
84 | pr_err("EVM: initialization failed\n"); | ||
85 | return count; | ||
86 | } | ||
87 | |||
88 | static const struct file_operations evm_key_ops = { | ||
89 | .read = evm_read_key, | ||
90 | .write = evm_write_key, | ||
91 | }; | ||
92 | |||
93 | int __init evm_init_secfs(void) | ||
94 | { | ||
95 | int error = 0; | ||
96 | |||
97 | evm_init_tpm = securityfs_create_file("evm", S_IRUSR | S_IRGRP, | ||
98 | NULL, NULL, &evm_key_ops); | ||
99 | if (!evm_init_tpm || IS_ERR(evm_init_tpm)) | ||
100 | error = -EFAULT; | ||
101 | return error; | ||
102 | } | ||
103 | |||
104 | void __exit evm_cleanup_secfs(void) | ||
105 | { | ||
106 | if (evm_init_tpm) | ||
107 | securityfs_remove(evm_init_tpm); | ||
108 | } | ||