Commit | Line | Data |
---|---|---|
b886d83c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
66dbc325 MZ |
2 | /* |
3 | * Copyright (C) 2005-2010 IBM Corporation | |
4 | * | |
5 | * Authors: | |
6 | * Mimi Zohar <zohar@us.ibm.com> | |
7 | * Kylene Hall <kjhall@us.ibm.com> | |
8 | * | |
66dbc325 MZ |
9 | * File: evm_crypto.c |
10 | * Using root's kernel master key (kmk), calculate the HMAC | |
11 | */ | |
12 | ||
87ac3d00 MZ |
13 | #define pr_fmt(fmt) "EVM: "fmt |
14 | ||
876979c9 | 15 | #include <linux/export.h> |
66dbc325 MZ |
16 | #include <linux/crypto.h> |
17 | #include <linux/xattr.h> | |
76266763 | 18 | #include <linux/evm.h> |
66dbc325 | 19 | #include <keys/encrypted-type.h> |
d46eb369 | 20 | #include <crypto/hash.h> |
5feeb611 | 21 | #include <crypto/hash_info.h> |
66dbc325 MZ |
22 | #include "evm.h" |
23 | ||
24 | #define EVMKEY "evm-key" | |
25 | #define MAX_KEY_SIZE 128 | |
26 | static unsigned char evmkey[MAX_KEY_SIZE]; | |
b2724d58 | 27 | static const int evmkey_len = MAX_KEY_SIZE; |
66dbc325 | 28 | |
4c41186f | 29 | static struct crypto_shash *hmac_tfm; |
5feeb611 | 30 | static struct crypto_shash *evm_tfm[HASH_ALGO__LAST]; |
d46eb369 | 31 | |
97426f98 DK |
32 | static DEFINE_MUTEX(mutex); |
33 | ||
76266763 DK |
34 | #define EVM_SET_KEY_BUSY 0 |
35 | ||
36 | static unsigned long evm_set_key_flags; | |
37 | ||
b2724d58 | 38 | static const char evm_hmac[] = "hmac(sha1)"; |
1a82cee3 | 39 | |
76266763 DK |
40 | /** |
41 | * evm_set_key() - set EVM HMAC key from the kernel | |
42 | * @key: pointer to a buffer with the key data | |
996e0a97 | 43 | * @keylen: length of the key data |
76266763 DK |
44 | * |
45 | * This function allows setting the EVM HMAC key from the kernel | |
46 | * without using the "encrypted" key subsystem keys. It can be used | |
47 | * by the crypto HW kernel module which has its own way of managing | |
48 | * keys. | |
49 | * | |
50 | * key length should be between 32 and 128 bytes long | |
51 | */ | |
52 | int evm_set_key(void *key, size_t keylen) | |
53 | { | |
54 | int rc; | |
55 | ||
56 | rc = -EBUSY; | |
57 | if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags)) | |
58 | goto busy; | |
59 | rc = -EINVAL; | |
60 | if (keylen > MAX_KEY_SIZE) | |
61 | goto inval; | |
62 | memcpy(evmkey, key, keylen); | |
63 | evm_initialized |= EVM_INIT_HMAC; | |
64 | pr_info("key initialized\n"); | |
65 | return 0; | |
66 | inval: | |
67 | clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags); | |
68 | busy: | |
69 | pr_err("key initialization failed\n"); | |
70 | return rc; | |
71 | } | |
72 | EXPORT_SYMBOL_GPL(evm_set_key); | |
73 | ||
5feeb611 | 74 | static struct shash_desc *init_desc(char type, uint8_t hash_algo) |
66dbc325 | 75 | { |
143b01d3 | 76 | long rc; |
5feeb611 | 77 | const char *algo; |
51dd64bb | 78 | struct crypto_shash **tfm, *tmp_tfm; |
d46eb369 DK |
79 | struct shash_desc *desc; |
80 | ||
15647eb3 | 81 | if (type == EVM_XATTR_HMAC) { |
26ddabfe | 82 | if (!(evm_initialized & EVM_INIT_HMAC)) { |
0485d066 | 83 | pr_err_once("HMAC key is not set\n"); |
26ddabfe DK |
84 | return ERR_PTR(-ENOKEY); |
85 | } | |
15647eb3 DK |
86 | tfm = &hmac_tfm; |
87 | algo = evm_hmac; | |
88 | } else { | |
221be106 RS |
89 | if (hash_algo >= HASH_ALGO__LAST) |
90 | return ERR_PTR(-EINVAL); | |
91 | ||
5feeb611 MG |
92 | tfm = &evm_tfm[hash_algo]; |
93 | algo = hash_algo_name[hash_algo]; | |
15647eb3 DK |
94 | } |
95 | ||
84338569 DC |
96 | if (*tfm) |
97 | goto alloc; | |
98 | mutex_lock(&mutex); | |
99 | if (*tfm) | |
100 | goto unlock; | |
101 | ||
102 | tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD); | |
103 | if (IS_ERR(tmp_tfm)) { | |
104 | pr_err("Can not allocate %s (reason: %ld)\n", algo, | |
105 | PTR_ERR(tmp_tfm)); | |
106 | mutex_unlock(&mutex); | |
107 | return ERR_CAST(tmp_tfm); | |
108 | } | |
109 | if (type == EVM_XATTR_HMAC) { | |
110 | rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len); | |
111 | if (rc) { | |
112 | crypto_free_shash(tmp_tfm); | |
97426f98 | 113 | mutex_unlock(&mutex); |
d46eb369 DK |
114 | return ERR_PTR(rc); |
115 | } | |
66dbc325 | 116 | } |
84338569 DC |
117 | *tfm = tmp_tfm; |
118 | unlock: | |
119 | mutex_unlock(&mutex); | |
120 | alloc: | |
15647eb3 | 121 | desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm), |
d46eb369 | 122 | GFP_KERNEL); |
51dd64bb | 123 | if (!desc) |
d46eb369 DK |
124 | return ERR_PTR(-ENOMEM); |
125 | ||
15647eb3 | 126 | desc->tfm = *tfm; |
d46eb369 | 127 | |
d46eb369 | 128 | rc = crypto_shash_init(desc); |
d46eb369 DK |
129 | if (rc) { |
130 | kfree(desc); | |
131 | return ERR_PTR(rc); | |
132 | } | |
133 | return desc; | |
66dbc325 MZ |
134 | } |
135 | ||
136 | /* Protect against 'cutting & pasting' security.evm xattr, include inode | |
137 | * specific info. | |
138 | * | |
139 | * (Additional directory/file metadata needs to be added for more complete | |
140 | * protection.) | |
141 | */ | |
d46eb369 | 142 | static void hmac_add_misc(struct shash_desc *desc, struct inode *inode, |
50b97748 | 143 | char type, char *digest) |
66dbc325 MZ |
144 | { |
145 | struct h_misc { | |
146 | unsigned long ino; | |
147 | __u32 generation; | |
148 | uid_t uid; | |
149 | gid_t gid; | |
150 | umode_t mode; | |
151 | } hmac_misc; | |
66dbc325 | 152 | |
2bb930ab | 153 | memset(&hmac_misc, 0, sizeof(hmac_misc)); |
50b97748 MG |
154 | /* Don't include the inode or generation number in portable |
155 | * signatures | |
156 | */ | |
157 | if (type != EVM_XATTR_PORTABLE_DIGSIG) { | |
158 | hmac_misc.ino = inode->i_ino; | |
159 | hmac_misc.generation = inode->i_generation; | |
160 | } | |
19339c25 EB |
161 | /* The hmac uid and gid must be encoded in the initial user |
162 | * namespace (not the filesystems user namespace) as encoding | |
163 | * them in the filesystems user namespace allows an attack | |
164 | * where first they are written in an unprivileged fuse mount | |
165 | * of a filesystem and then the system is tricked to mount the | |
166 | * filesystem for real on next boot and trust it because | |
167 | * everything is signed. | |
168 | */ | |
169 | hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid); | |
170 | hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid); | |
66dbc325 | 171 | hmac_misc.mode = inode->i_mode; |
2bb930ab | 172 | crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc)); |
50b97748 MG |
173 | if ((evm_hmac_attrs & EVM_ATTR_FSUUID) && |
174 | type != EVM_XATTR_PORTABLE_DIGSIG) | |
e7fde070 | 175 | crypto_shash_update(desc, (u8 *)&inode->i_sb->s_uuid, UUID_SIZE); |
d46eb369 | 176 | crypto_shash_final(desc, digest); |
87ac3d00 MZ |
177 | |
178 | pr_debug("hmac_misc: (%zu) [%*phN]\n", sizeof(struct h_misc), | |
179 | (int)sizeof(struct h_misc), &hmac_misc); | |
180 | } | |
181 | ||
182 | /* | |
ceb5faef | 183 | * Dump large security xattr values as a continuous ascii hexadecimal string. |
87ac3d00 MZ |
184 | * (pr_debug is limited to 64 bytes.) |
185 | */ | |
8250865c XJ |
186 | static void dump_security_xattr_l(const char *prefix, const void *src, |
187 | size_t count) | |
87ac3d00 MZ |
188 | { |
189 | #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) | |
190 | char *asciihex, *p; | |
191 | ||
192 | p = asciihex = kmalloc(count * 2 + 1, GFP_KERNEL); | |
193 | if (!asciihex) | |
194 | return; | |
195 | ||
196 | p = bin2hex(p, src, count); | |
197 | *p = 0; | |
198 | pr_debug("%s: (%zu) %.*s\n", prefix, count, (int)count * 2, asciihex); | |
199 | kfree(asciihex); | |
200 | #endif | |
66dbc325 MZ |
201 | } |
202 | ||
8250865c XJ |
203 | static void dump_security_xattr(const char *name, const char *value, |
204 | size_t value_len) | |
205 | { | |
206 | if (value_len < 64) | |
207 | pr_debug("%s: (%zu) [%*phN]\n", name, value_len, | |
208 | (int)value_len, value); | |
209 | else | |
210 | dump_security_xattr_l(name, value, value_len); | |
211 | } | |
212 | ||
66dbc325 MZ |
213 | /* |
214 | * Calculate the HMAC value across the set of protected security xattrs. | |
215 | * | |
216 | * Instead of retrieving the requested xattr, for performance, calculate | |
217 | * the hmac using the requested xattr value. Don't alloc/free memory for | |
218 | * each xattr, but attempt to re-use the previously allocated memory. | |
219 | */ | |
15647eb3 | 220 | static int evm_calc_hmac_or_hash(struct dentry *dentry, |
5feeb611 MG |
221 | const char *req_xattr_name, |
222 | const char *req_xattr_value, | |
223 | size_t req_xattr_value_len, | |
a652aa59 SB |
224 | uint8_t type, struct evm_digest *data, |
225 | struct evm_iint_cache *iint) | |
66dbc325 | 226 | { |
faf99481 | 227 | struct inode *inode = d_inode(d_real(dentry, D_REAL_METADATA)); |
21af7663 | 228 | struct xattr_list *xattr; |
d46eb369 | 229 | struct shash_desc *desc; |
66dbc325 MZ |
230 | size_t xattr_size = 0; |
231 | char *xattr_value = NULL; | |
232 | int error; | |
907a399d | 233 | int size, user_space_size; |
50b97748 | 234 | bool ima_present = false; |
a652aa59 | 235 | u64 i_version = 0; |
66dbc325 | 236 | |
a3a5c966 SF |
237 | if (!(inode->i_opflags & IOP_XATTR) || |
238 | inode->i_sb->s_user_ns != &init_user_ns) | |
66dbc325 | 239 | return -EOPNOTSUPP; |
5d6c3191 | 240 | |
5feeb611 | 241 | desc = init_desc(type, data->hdr.algo); |
d46eb369 DK |
242 | if (IS_ERR(desc)) |
243 | return PTR_ERR(desc); | |
66dbc325 | 244 | |
5feeb611 MG |
245 | data->hdr.length = crypto_shash_digestsize(desc->tfm); |
246 | ||
66dbc325 | 247 | error = -ENODATA; |
770f6058 | 248 | list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) { |
50b97748 MG |
249 | bool is_ima = false; |
250 | ||
21af7663 | 251 | if (strcmp(xattr->name, XATTR_NAME_IMA) == 0) |
50b97748 MG |
252 | is_ima = true; |
253 | ||
8c7a703e RS |
254 | /* |
255 | * Skip non-enabled xattrs for locally calculated | |
256 | * signatures/HMACs. | |
257 | */ | |
258 | if (type != EVM_XATTR_PORTABLE_DIGSIG && !xattr->enabled) | |
259 | continue; | |
260 | ||
66dbc325 | 261 | if ((req_xattr_name && req_xattr_value) |
21af7663 | 262 | && !strcmp(xattr->name, req_xattr_name)) { |
66dbc325 | 263 | error = 0; |
d46eb369 DK |
264 | crypto_shash_update(desc, (const u8 *)req_xattr_value, |
265 | req_xattr_value_len); | |
50b97748 MG |
266 | if (is_ima) |
267 | ima_present = true; | |
87ac3d00 | 268 | |
8250865c XJ |
269 | dump_security_xattr(req_xattr_name, |
270 | req_xattr_value, | |
271 | req_xattr_value_len); | |
66dbc325 MZ |
272 | continue; |
273 | } | |
4609e1f1 | 274 | size = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, xattr->name, |
66dbc325 MZ |
275 | &xattr_value, xattr_size, GFP_NOFS); |
276 | if (size == -ENOMEM) { | |
277 | error = -ENOMEM; | |
278 | goto out; | |
279 | } | |
280 | if (size < 0) | |
281 | continue; | |
282 | ||
4609e1f1 | 283 | user_space_size = vfs_getxattr(&nop_mnt_idmap, dentry, |
907a399d RS |
284 | xattr->name, NULL, 0); |
285 | if (user_space_size != size) | |
286 | pr_debug("file %s: xattr %s size mismatch (kernel: %d, user: %d)\n", | |
287 | dentry->d_name.name, xattr->name, size, | |
288 | user_space_size); | |
66dbc325 MZ |
289 | error = 0; |
290 | xattr_size = size; | |
d46eb369 | 291 | crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size); |
50b97748 MG |
292 | if (is_ima) |
293 | ima_present = true; | |
87ac3d00 | 294 | |
8250865c | 295 | dump_security_xattr(xattr->name, xattr_value, xattr_size); |
66dbc325 | 296 | } |
5feeb611 | 297 | hmac_add_misc(desc, inode, type, data->digest); |
d46eb369 | 298 | |
a652aa59 SB |
299 | if (inode != d_backing_inode(dentry) && iint) { |
300 | if (IS_I_VERSION(inode)) | |
301 | i_version = inode_query_iversion(inode); | |
302 | integrity_inode_attrs_store(&iint->metadata_inode, i_version, | |
303 | inode); | |
304 | } | |
305 | ||
50b97748 MG |
306 | /* Portable EVM signatures must include an IMA hash */ |
307 | if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present) | |
0c4395fb | 308 | error = -EPERM; |
66dbc325 | 309 | out: |
d46eb369 DK |
310 | kfree(xattr_value); |
311 | kfree(desc); | |
66dbc325 MZ |
312 | return error; |
313 | } | |
314 | ||
15647eb3 DK |
315 | int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name, |
316 | const char *req_xattr_value, size_t req_xattr_value_len, | |
a652aa59 | 317 | struct evm_digest *data, struct evm_iint_cache *iint) |
15647eb3 DK |
318 | { |
319 | return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value, | |
a652aa59 SB |
320 | req_xattr_value_len, EVM_XATTR_HMAC, data, |
321 | iint); | |
15647eb3 DK |
322 | } |
323 | ||
324 | int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name, | |
325 | const char *req_xattr_value, size_t req_xattr_value_len, | |
a652aa59 | 326 | char type, struct evm_digest *data, struct evm_iint_cache *iint) |
15647eb3 DK |
327 | { |
328 | return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value, | |
a652aa59 | 329 | req_xattr_value_len, type, data, iint); |
50b97748 MG |
330 | } |
331 | ||
332 | static int evm_is_immutable(struct dentry *dentry, struct inode *inode) | |
333 | { | |
334 | const struct evm_ima_xattr_data *xattr_data = NULL; | |
75a323e6 | 335 | struct evm_iint_cache *iint; |
50b97748 MG |
336 | int rc = 0; |
337 | ||
75a323e6 | 338 | iint = evm_iint_inode(inode); |
50b97748 MG |
339 | if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG)) |
340 | return 1; | |
341 | ||
342 | /* Do this the hard way */ | |
4609e1f1 | 343 | rc = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, XATTR_NAME_EVM, |
c7c7a1a1 | 344 | (char **)&xattr_data, 0, GFP_NOFS); |
50b97748 MG |
345 | if (rc <= 0) { |
346 | if (rc == -ENODATA) | |
f6fbd8cb PM |
347 | rc = 0; |
348 | goto out; | |
50b97748 MG |
349 | } |
350 | if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) | |
351 | rc = 1; | |
352 | else | |
353 | rc = 0; | |
354 | ||
f6fbd8cb | 355 | out: |
50b97748 MG |
356 | kfree(xattr_data); |
357 | return rc; | |
15647eb3 DK |
358 | } |
359 | ||
50b97748 | 360 | |
66dbc325 MZ |
361 | /* |
362 | * Calculate the hmac and update security.evm xattr | |
363 | * | |
364 | * Expects to be called with i_mutex locked. | |
365 | */ | |
366 | int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name, | |
367 | const char *xattr_value, size_t xattr_value_len) | |
368 | { | |
c6f493d6 | 369 | struct inode *inode = d_backing_inode(dentry); |
a652aa59 | 370 | struct evm_iint_cache *iint = evm_iint_inode(inode); |
5feeb611 | 371 | struct evm_digest data; |
66dbc325 MZ |
372 | int rc = 0; |
373 | ||
50b97748 MG |
374 | /* |
375 | * Don't permit any transformation of the EVM xattr if the signature | |
376 | * is of an immutable type | |
377 | */ | |
378 | rc = evm_is_immutable(dentry, inode); | |
379 | if (rc < 0) | |
380 | return rc; | |
381 | if (rc) | |
382 | return -EPERM; | |
383 | ||
5feeb611 | 384 | data.hdr.algo = HASH_ALGO_SHA1; |
66dbc325 | 385 | rc = evm_calc_hmac(dentry, xattr_name, xattr_value, |
a652aa59 | 386 | xattr_value_len, &data, iint); |
6be5cc52 | 387 | if (rc == 0) { |
5feeb611 | 388 | data.hdr.xattr.sha1.type = EVM_XATTR_HMAC; |
39f60c1c | 389 | rc = __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, |
c7c7a1a1 | 390 | XATTR_NAME_EVM, |
5feeb611 MG |
391 | &data.hdr.xattr.data[1], |
392 | SHA1_DIGEST_SIZE + 1, 0); | |
5d6c3191 | 393 | } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) { |
39f60c1c | 394 | rc = __vfs_removexattr(&nop_mnt_idmap, dentry, XATTR_NAME_EVM); |
a67adb99 | 395 | } |
66dbc325 MZ |
396 | return rc; |
397 | } | |
398 | ||
c31288e5 | 399 | int evm_init_hmac(struct inode *inode, const struct xattr *xattrs, |
cb723180 MZ |
400 | char *hmac_val) |
401 | { | |
d46eb369 | 402 | struct shash_desc *desc; |
c31288e5 | 403 | const struct xattr *xattr; |
cb723180 | 404 | |
5feeb611 | 405 | desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1); |
d46eb369 | 406 | if (IS_ERR(desc)) { |
20ee451f | 407 | pr_info("init_desc failed\n"); |
d46eb369 | 408 | return PTR_ERR(desc); |
cb723180 MZ |
409 | } |
410 | ||
c31288e5 RS |
411 | for (xattr = xattrs; xattr->name; xattr++) { |
412 | if (!evm_protected_xattr(xattr->name)) | |
413 | continue; | |
414 | ||
415 | crypto_shash_update(desc, xattr->value, xattr->value_len); | |
416 | } | |
417 | ||
50b97748 | 418 | hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val); |
d46eb369 | 419 | kfree(desc); |
cb723180 MZ |
420 | return 0; |
421 | } | |
422 | ||
66dbc325 MZ |
423 | /* |
424 | * Get the key from the TPM for the SHA1-HMAC | |
425 | */ | |
426 | int evm_init_key(void) | |
427 | { | |
428 | struct key *evm_key; | |
429 | struct encrypted_key_payload *ekp; | |
76266763 | 430 | int rc; |
66dbc325 | 431 | |
028db3e2 | 432 | evm_key = request_key(&key_type_encrypted, EVMKEY, NULL); |
66dbc325 MZ |
433 | if (IS_ERR(evm_key)) |
434 | return -ENOENT; | |
435 | ||
436 | down_read(&evm_key->sem); | |
146aa8b1 | 437 | ekp = evm_key->payload.data[0]; |
76266763 DK |
438 | |
439 | rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen); | |
440 | ||
66dbc325 MZ |
441 | /* burn the original key contents */ |
442 | memset(ekp->decrypted_data, 0, ekp->decrypted_datalen); | |
443 | up_read(&evm_key->sem); | |
444 | key_put(evm_key); | |
445 | return rc; | |
446 | } |