1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2005-2010 IBM Corporation
4 *
5 * Author:
6 * Mimi Zohar <zohar@us.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
8 *
9 * File: evm_main.c
10 * implements evm_inode_setxattr, evm_inode_post_setxattr,
11 * evm_inode_removexattr, and evm_verifyxattr
12 */
13
14 #include <linux/init.h>
15 #include <linux/crypto.h>
16 #include <linux/audit.h>
17 #include <linux/xattr.h>
18 #include <linux/integrity.h>
19 #include <linux/evm.h>
20 #include <linux/magic.h>
21
22 #include <crypto/hash.h>
23 #include <crypto/hash_info.h>
24 #include <crypto/algapi.h>
25 #include "evm.h"
26
27 int evm_initialized;
28
29 static const char * const integrity_status_msg[] = {
30 "pass", "pass_immutable", "fail", "no_label", "no_xattrs", "unknown"
31 };
32 int evm_hmac_attrs;
33
34 static struct xattr_list evm_config_default_xattrnames[] = {
35 #ifdef CONFIG_SECURITY_SELINUX
36 {.name = XATTR_NAME_SELINUX},
37 #endif
38 #ifdef CONFIG_SECURITY_SMACK
39 {.name = XATTR_NAME_SMACK},
40 #ifdef CONFIG_EVM_EXTRA_SMACK_XATTRS
41 {.name = XATTR_NAME_SMACKEXEC},
42 {.name = XATTR_NAME_SMACKTRANSMUTE},
43 {.name = XATTR_NAME_SMACKMMAP},
44 #endif
45 #endif
46 #ifdef CONFIG_SECURITY_APPARMOR
47 {.name = XATTR_NAME_APPARMOR},
48 #endif
49 #ifdef CONFIG_IMA_APPRAISE
50 {.name = XATTR_NAME_IMA},
51 #endif
52 {.name = XATTR_NAME_CAPS},
53 };
54
55 LIST_HEAD(evm_config_xattrnames);
56
57 static int evm_fixmode __ro_after_init;
evm_set_fixmode(char * str)58 static int __init evm_set_fixmode(char *str)
59 {
60 if (strncmp(str, "fix", 3) == 0)
61 evm_fixmode = 1;
62 else
63 pr_err("invalid \"%s\" mode", str);
64
65 return 1;
66 }
67 __setup("evm=", evm_set_fixmode);
68
evm_init_config(void)69 static void __init evm_init_config(void)
70 {
71 int i, xattrs;
72
73 xattrs = ARRAY_SIZE(evm_config_default_xattrnames);
74
75 pr_info("Initialising EVM extended attributes:\n");
76 for (i = 0; i < xattrs; i++) {
77 pr_info("%s\n", evm_config_default_xattrnames[i].name);
78 list_add_tail(&evm_config_default_xattrnames[i].list,
79 &evm_config_xattrnames);
80 }
81
82 #ifdef CONFIG_EVM_ATTR_FSUUID
83 evm_hmac_attrs |= EVM_ATTR_FSUUID;
84 #endif
85 pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs);
86 }
87
evm_key_loaded(void)88 static bool evm_key_loaded(void)
89 {
90 return (bool)(evm_initialized & EVM_KEY_MASK);
91 }
92
evm_find_protected_xattrs(struct dentry * dentry)93 static int evm_find_protected_xattrs(struct dentry *dentry)
94 {
95 struct inode *inode = d_backing_inode(dentry);
96 struct xattr_list *xattr;
97 int error;
98 int count = 0;
99
100 if (!(inode->i_opflags & IOP_XATTR))
101 return -EOPNOTSUPP;
102
103 list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
104 error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0,
105 XATTR_NOSECURITY);
106 if (error < 0) {
107 if (error == -ENODATA)
108 continue;
109 return error;
110 }
111 count++;
112 }
113
114 return count;
115 }
116
117 /*
118 * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
119 *
120 * Compute the HMAC on the dentry's protected set of extended attributes
121 * and compare it against the stored security.evm xattr.
122 *
123 * For performance:
124 * - use the previoulsy retrieved xattr value and length to calculate the
125 * HMAC.)
126 * - cache the verification result in the iint, when available.
127 *
128 * Returns integrity status
129 */
evm_verify_hmac(struct dentry * dentry,const char * xattr_name,char * xattr_value,size_t xattr_value_len,struct integrity_iint_cache * iint)130 static enum integrity_status evm_verify_hmac(struct dentry *dentry,
131 const char *xattr_name,
132 char *xattr_value,
133 size_t xattr_value_len,
134 struct integrity_iint_cache *iint)
135 {
136 struct evm_ima_xattr_data *xattr_data = NULL;
137 struct signature_v2_hdr *hdr;
138 enum integrity_status evm_status = INTEGRITY_PASS;
139 struct evm_digest digest;
140 struct inode *inode;
141 int rc, xattr_len;
142
143 if (iint && (iint->evm_status == INTEGRITY_PASS ||
144 iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
145 return iint->evm_status;
146
147 /* if status is not PASS, try to check again - against -ENOMEM */
148
149 /* first need to know the sig type */
150 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
151 GFP_NOFS);
152 if (rc <= 0) {
153 evm_status = INTEGRITY_FAIL;
154 if (rc == -ENODATA) {
155 rc = evm_find_protected_xattrs(dentry);
156 if (rc > 0)
157 evm_status = INTEGRITY_NOLABEL;
158 else if (rc == 0)
159 evm_status = INTEGRITY_NOXATTRS; /* new file */
160 } else if (rc == -EOPNOTSUPP) {
161 evm_status = INTEGRITY_UNKNOWN;
162 }
163 goto out;
164 }
165
166 xattr_len = rc;
167
168 /* check value type */
169 switch (xattr_data->type) {
170 case EVM_XATTR_HMAC:
171 if (xattr_len != sizeof(struct evm_xattr)) {
172 evm_status = INTEGRITY_FAIL;
173 goto out;
174 }
175
176 digest.hdr.algo = HASH_ALGO_SHA1;
177 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
178 xattr_value_len, &digest);
179 if (rc)
180 break;
181 rc = crypto_memneq(xattr_data->data, digest.digest,
182 SHA1_DIGEST_SIZE);
183 if (rc)
184 rc = -EINVAL;
185 break;
186 case EVM_IMA_XATTR_DIGSIG:
187 case EVM_XATTR_PORTABLE_DIGSIG:
188 /* accept xattr with non-empty signature field */
189 if (xattr_len <= sizeof(struct signature_v2_hdr)) {
190 evm_status = INTEGRITY_FAIL;
191 goto out;
192 }
193
194 hdr = (struct signature_v2_hdr *)xattr_data;
195 digest.hdr.algo = hdr->hash_algo;
196 rc = evm_calc_hash(dentry, xattr_name, xattr_value,
197 xattr_value_len, xattr_data->type, &digest);
198 if (rc)
199 break;
200 rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
201 (const char *)xattr_data, xattr_len,
202 digest.digest, digest.hdr.length);
203 if (!rc) {
204 inode = d_backing_inode(dentry);
205
206 if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
207 if (iint)
208 iint->flags |= EVM_IMMUTABLE_DIGSIG;
209 evm_status = INTEGRITY_PASS_IMMUTABLE;
210 } else if (!IS_RDONLY(inode) &&
211 !(inode->i_sb->s_readonly_remount) &&
212 !IS_IMMUTABLE(inode)) {
213 evm_update_evmxattr(dentry, xattr_name,
214 xattr_value,
215 xattr_value_len);
216 }
217 }
218 break;
219 default:
220 rc = -EINVAL;
221 break;
222 }
223
224 if (rc)
225 evm_status = (rc == -ENODATA) ?
226 INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
227 out:
228 if (iint)
229 iint->evm_status = evm_status;
230 kfree(xattr_data);
231 return evm_status;
232 }
233
evm_protected_xattr(const char * req_xattr_name)234 static int evm_protected_xattr(const char *req_xattr_name)
235 {
236 int namelen;
237 int found = 0;
238 struct xattr_list *xattr;
239
240 namelen = strlen(req_xattr_name);
241 list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
242 if ((strlen(xattr->name) == namelen)
243 && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
244 found = 1;
245 break;
246 }
247 if (strncmp(req_xattr_name,
248 xattr->name + XATTR_SECURITY_PREFIX_LEN,
249 strlen(req_xattr_name)) == 0) {
250 found = 1;
251 break;
252 }
253 }
254
255 return found;
256 }
257
258 /**
259 * evm_verifyxattr - verify the integrity of the requested xattr
260 * @dentry: object of the verify xattr
261 * @xattr_name: requested xattr
262 * @xattr_value: requested xattr value
263 * @xattr_value_len: requested xattr value length
264 *
265 * Calculate the HMAC for the given dentry and verify it against the stored
266 * security.evm xattr. For performance, use the xattr value and length
267 * previously retrieved to calculate the HMAC.
268 *
269 * Returns the xattr integrity status.
270 *
271 * This function requires the caller to lock the inode's i_mutex before it
272 * is executed.
273 */
evm_verifyxattr(struct dentry * dentry,const char * xattr_name,void * xattr_value,size_t xattr_value_len,struct integrity_iint_cache * iint)274 enum integrity_status evm_verifyxattr(struct dentry *dentry,
275 const char *xattr_name,
276 void *xattr_value, size_t xattr_value_len,
277 struct integrity_iint_cache *iint)
278 {
279 if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
280 return INTEGRITY_UNKNOWN;
281
282 if (!iint) {
283 iint = integrity_iint_find(d_backing_inode(dentry));
284 if (!iint)
285 return INTEGRITY_UNKNOWN;
286 }
287 return evm_verify_hmac(dentry, xattr_name, xattr_value,
288 xattr_value_len, iint);
289 }
290 EXPORT_SYMBOL_GPL(evm_verifyxattr);
291
292 /*
293 * evm_verify_current_integrity - verify the dentry's metadata integrity
294 * @dentry: pointer to the affected dentry
295 *
296 * Verify and return the dentry's metadata integrity. The exceptions are
297 * before EVM is initialized or in 'fix' mode.
298 */
evm_verify_current_integrity(struct dentry * dentry)299 static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
300 {
301 struct inode *inode = d_backing_inode(dentry);
302
303 if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
304 return 0;
305 return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
306 }
307
308 /*
309 * evm_protect_xattr - protect the EVM extended attribute
310 *
311 * Prevent security.evm from being modified or removed without the
312 * necessary permissions or when the existing value is invalid.
313 *
314 * The posix xattr acls are 'system' prefixed, which normally would not
315 * affect security.evm. An interesting side affect of writing posix xattr
316 * acls is their modifying of the i_mode, which is included in security.evm.
317 * For posix xattr acls only, permit security.evm, even if it currently
318 * doesn't exist, to be updated unless the EVM signature is immutable.
319 */
evm_protect_xattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)320 static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
321 const void *xattr_value, size_t xattr_value_len)
322 {
323 enum integrity_status evm_status;
324
325 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
326 if (!capable(CAP_SYS_ADMIN))
327 return -EPERM;
328 } else if (!evm_protected_xattr(xattr_name)) {
329 if (!posix_xattr_acl(xattr_name))
330 return 0;
331 evm_status = evm_verify_current_integrity(dentry);
332 if ((evm_status == INTEGRITY_PASS) ||
333 (evm_status == INTEGRITY_NOXATTRS))
334 return 0;
335 goto out;
336 }
337
338 evm_status = evm_verify_current_integrity(dentry);
339 if (evm_status == INTEGRITY_NOXATTRS) {
340 struct integrity_iint_cache *iint;
341
342 iint = integrity_iint_find(d_backing_inode(dentry));
343 if (iint && (iint->flags & IMA_NEW_FILE))
344 return 0;
345
346 /* exception for pseudo filesystems */
347 if (dentry->d_sb->s_magic == TMPFS_MAGIC
348 || dentry->d_sb->s_magic == SYSFS_MAGIC)
349 return 0;
350
351 integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
352 dentry->d_inode, dentry->d_name.name,
353 "update_metadata",
354 integrity_status_msg[evm_status],
355 -EPERM, 0);
356 }
357 out:
358 if (evm_status != INTEGRITY_PASS)
359 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
360 dentry->d_name.name, "appraise_metadata",
361 integrity_status_msg[evm_status],
362 -EPERM, 0);
363 return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
364 }
365
366 /**
367 * evm_inode_setxattr - protect the EVM extended attribute
368 * @dentry: pointer to the affected dentry
369 * @xattr_name: pointer to the affected extended attribute name
370 * @xattr_value: pointer to the new extended attribute value
371 * @xattr_value_len: pointer to the new extended attribute value length
372 *
373 * Before allowing the 'security.evm' protected xattr to be updated,
374 * verify the existing value is valid. As only the kernel should have
375 * access to the EVM encrypted key needed to calculate the HMAC, prevent
376 * userspace from writing HMAC value. Writing 'security.evm' requires
377 * requires CAP_SYS_ADMIN privileges.
378 */
evm_inode_setxattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)379 int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
380 const void *xattr_value, size_t xattr_value_len)
381 {
382 const struct evm_ima_xattr_data *xattr_data = xattr_value;
383
384 /* Policy permits modification of the protected xattrs even though
385 * there's no HMAC key loaded
386 */
387 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
388 return 0;
389
390 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
391 if (!xattr_value_len)
392 return -EINVAL;
393 if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
394 xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
395 return -EPERM;
396 }
397 return evm_protect_xattr(dentry, xattr_name, xattr_value,
398 xattr_value_len);
399 }
400
401 /**
402 * evm_inode_removexattr - protect the EVM extended attribute
403 * @dentry: pointer to the affected dentry
404 * @xattr_name: pointer to the affected extended attribute name
405 *
406 * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
407 * the current value is valid.
408 */
evm_inode_removexattr(struct dentry * dentry,const char * xattr_name)409 int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
410 {
411 /* Policy permits modification of the protected xattrs even though
412 * there's no HMAC key loaded
413 */
414 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
415 return 0;
416
417 return evm_protect_xattr(dentry, xattr_name, NULL, 0);
418 }
419
evm_reset_status(struct inode * inode)420 static void evm_reset_status(struct inode *inode)
421 {
422 struct integrity_iint_cache *iint;
423
424 iint = integrity_iint_find(inode);
425 if (iint)
426 iint->evm_status = INTEGRITY_UNKNOWN;
427 }
428
429 /**
430 * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
431 * @dentry: pointer to the affected dentry
432 * @xattr_name: pointer to the affected extended attribute name
433 * @xattr_value: pointer to the new extended attribute value
434 * @xattr_value_len: pointer to the new extended attribute value length
435 *
436 * Update the HMAC stored in 'security.evm' to reflect the change.
437 *
438 * No need to take the i_mutex lock here, as this function is called from
439 * __vfs_setxattr_noperm(). The caller of which has taken the inode's
440 * i_mutex lock.
441 */
evm_inode_post_setxattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)442 void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
443 const void *xattr_value, size_t xattr_value_len)
444 {
445 if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name)
446 && !posix_xattr_acl(xattr_name)))
447 return;
448
449 evm_reset_status(dentry->d_inode);
450
451 evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
452 }
453
454 /**
455 * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
456 * @dentry: pointer to the affected dentry
457 * @xattr_name: pointer to the affected extended attribute name
458 *
459 * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
460 *
461 * No need to take the i_mutex lock here, as this function is called from
462 * vfs_removexattr() which takes the i_mutex.
463 */
evm_inode_post_removexattr(struct dentry * dentry,const char * xattr_name)464 void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
465 {
466 if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
467 return;
468
469 evm_reset_status(dentry->d_inode);
470
471 evm_update_evmxattr(dentry, xattr_name, NULL, 0);
472 }
473
474 /**
475 * evm_inode_setattr - prevent updating an invalid EVM extended attribute
476 * @idmap: idmap of the mount
477 * @dentry: pointer to the affected dentry
478 * @attr: iattr structure containing the new file attributes
479 *
480 * Permit update of file attributes when files have a valid EVM signature,
481 * except in the case of them having an immutable portable signature.
482 */
evm_inode_setattr(struct dentry * dentry,struct iattr * attr)483 int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
484 {
485 unsigned int ia_valid = attr->ia_valid;
486 enum integrity_status evm_status;
487
488 /* Policy permits modification of the protected attrs even though
489 * there's no HMAC key loaded
490 */
491 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
492 return 0;
493
494 if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
495 return 0;
496 evm_status = evm_verify_current_integrity(dentry);
497 if ((evm_status == INTEGRITY_PASS) ||
498 (evm_status == INTEGRITY_NOXATTRS))
499 return 0;
500 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
501 dentry->d_name.name, "appraise_metadata",
502 integrity_status_msg[evm_status], -EPERM, 0);
503 return -EPERM;
504 }
505
506 /**
507 * evm_inode_post_setattr - update 'security.evm' after modifying metadata
508 * @dentry: pointer to the affected dentry
509 * @ia_valid: for the UID and GID status
510 *
511 * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
512 * changes.
513 *
514 * This function is called from notify_change(), which expects the caller
515 * to lock the inode's i_mutex.
516 */
evm_inode_post_setattr(struct dentry * dentry,int ia_valid)517 void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
518 {
519 if (!evm_key_loaded())
520 return;
521
522 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
523 evm_update_evmxattr(dentry, NULL, NULL, 0);
524 }
525
526 /*
527 * evm_inode_init_security - initializes security.evm HMAC value
528 */
evm_inode_init_security(struct inode * inode,const struct xattr * lsm_xattr,struct xattr * evm_xattr)529 int evm_inode_init_security(struct inode *inode,
530 const struct xattr *lsm_xattr,
531 struct xattr *evm_xattr)
532 {
533 struct evm_xattr *xattr_data;
534 int rc;
535
536 if (!(evm_initialized & EVM_INIT_HMAC) ||
537 !evm_protected_xattr(lsm_xattr->name))
538 return 0;
539
540 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
541 if (!xattr_data)
542 return -ENOMEM;
543
544 xattr_data->data.type = EVM_XATTR_HMAC;
545 rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
546 if (rc < 0)
547 goto out;
548
549 evm_xattr->value = xattr_data;
550 evm_xattr->value_len = sizeof(*xattr_data);
551 evm_xattr->name = XATTR_EVM_SUFFIX;
552 return 0;
553 out:
554 kfree(xattr_data);
555 return rc;
556 }
557 EXPORT_SYMBOL_GPL(evm_inode_init_security);
558
559 #ifdef CONFIG_EVM_LOAD_X509
evm_load_x509(void)560 void __init evm_load_x509(void)
561 {
562 int rc;
563
564 rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH);
565 if (!rc)
566 evm_initialized |= EVM_INIT_X509;
567 }
568 #endif
569
init_evm(void)570 static int __init init_evm(void)
571 {
572 int error;
573 struct list_head *pos, *q;
574
575 evm_init_config();
576
577 error = integrity_init_keyring(INTEGRITY_KEYRING_EVM);
578 if (error)
579 goto error;
580
581 error = evm_init_secfs();
582 if (error < 0) {
583 pr_info("Error registering secfs\n");
584 goto error;
585 }
586
587 error:
588 if (error != 0) {
589 if (!list_empty(&evm_config_xattrnames)) {
590 list_for_each_safe(pos, q, &evm_config_xattrnames)
591 list_del(pos);
592 }
593 }
594
595 return error;
596 }
597
598 late_initcall(init_evm);
599