1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2011 IBM Corporation
4 *
5 * Author:
6 * Mimi Zohar <zohar@us.ibm.com>
7 */
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/file.h>
11 #include <linux/binfmts.h>
12 #include <linux/fs.h>
13 #include <linux/xattr.h>
14 #include <linux/magic.h>
15 #include <linux/ima.h>
16 #include <linux/evm.h>
17 #include <linux/fsverity.h>
18 #include <keys/system_keyring.h>
19 #include <uapi/linux/fsverity.h>
20
21 #include "ima.h"
22
23 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
24 static char *ima_appraise_cmdline_default __initdata;
25 core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0);
26
ima_appraise_parse_cmdline(void)27 void __init ima_appraise_parse_cmdline(void)
28 {
29 const char *str = ima_appraise_cmdline_default;
30 bool sb_state = arch_get_secureboot();
31 int appraisal_state = ima_appraise;
32
33 if (!str)
34 return;
35
36 if (strncmp(str, "off", 3) == 0)
37 appraisal_state = 0;
38 else if (strncmp(str, "log", 3) == 0)
39 appraisal_state = IMA_APPRAISE_LOG;
40 else if (strncmp(str, "fix", 3) == 0)
41 appraisal_state = IMA_APPRAISE_FIX;
42 else if (strncmp(str, "enforce", 7) == 0)
43 appraisal_state = IMA_APPRAISE_ENFORCE;
44 else
45 pr_err("invalid \"%s\" appraise option", str);
46
47 /* If appraisal state was changed, but secure boot is enabled,
48 * keep its default */
49 if (sb_state) {
50 if (!(appraisal_state & IMA_APPRAISE_ENFORCE))
51 pr_info("Secure boot enabled: ignoring ima_appraise=%s option",
52 str);
53 } else {
54 ima_appraise = appraisal_state;
55 }
56 }
57 #endif
58
59 /*
60 * is_ima_appraise_enabled - return appraise status
61 *
62 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
63 */
is_ima_appraise_enabled(void)64 bool is_ima_appraise_enabled(void)
65 {
66 return ima_appraise & IMA_APPRAISE_ENFORCE;
67 }
68
69 /*
70 * ima_must_appraise - set appraise flag
71 *
72 * Return 1 to appraise or hash
73 */
ima_must_appraise(struct mnt_idmap * idmap,struct inode * inode,int mask,enum ima_hooks func)74 int ima_must_appraise(struct mnt_idmap *idmap, struct inode *inode,
75 int mask, enum ima_hooks func)
76 {
77 struct lsm_prop prop;
78
79 if (!ima_appraise)
80 return 0;
81
82 security_current_getlsmprop_subj(&prop);
83 return ima_match_policy(idmap, inode, current_cred(), &prop,
84 func, mask, IMA_APPRAISE | IMA_HASH, NULL,
85 NULL, NULL, NULL);
86 }
87
ima_fix_xattr(struct dentry * dentry,struct ima_iint_cache * iint)88 static int ima_fix_xattr(struct dentry *dentry, struct ima_iint_cache *iint)
89 {
90 int rc, offset;
91 u8 algo = iint->ima_hash->algo;
92
93 if (IS_RDONLY(d_inode(dentry)))
94 return -EROFS;
95 if (IS_IMMUTABLE(d_inode(dentry)))
96 return -EPERM;
97
98 if (algo <= HASH_ALGO_SHA1) {
99 offset = 1;
100 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
101 } else {
102 offset = 0;
103 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
104 iint->ima_hash->xattr.ng.algo = algo;
105 }
106 rc = __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_IMA,
107 &iint->ima_hash->xattr.data[offset],
108 (sizeof(iint->ima_hash->xattr) - offset) +
109 iint->ima_hash->length, 0);
110 return rc;
111 }
112
113 /* Return specific func appraised cached result */
ima_get_cache_status(struct ima_iint_cache * iint,enum ima_hooks func)114 enum integrity_status ima_get_cache_status(struct ima_iint_cache *iint,
115 enum ima_hooks func)
116 {
117 switch (func) {
118 case MMAP_CHECK:
119 case MMAP_CHECK_REQPROT:
120 return iint->ima_mmap_status;
121 case BPRM_CHECK:
122 return iint->ima_bprm_status;
123 case CREDS_CHECK:
124 return iint->ima_creds_status;
125 case FILE_CHECK:
126 case POST_SETATTR:
127 return iint->ima_file_status;
128 case MODULE_CHECK ... MAX_CHECK - 1:
129 default:
130 return iint->ima_read_status;
131 }
132 }
133
ima_set_cache_status(struct ima_iint_cache * iint,enum ima_hooks func,enum integrity_status status)134 static void ima_set_cache_status(struct ima_iint_cache *iint,
135 enum ima_hooks func,
136 enum integrity_status status)
137 {
138 switch (func) {
139 case MMAP_CHECK:
140 case MMAP_CHECK_REQPROT:
141 iint->ima_mmap_status = status;
142 break;
143 case BPRM_CHECK:
144 iint->ima_bprm_status = status;
145 break;
146 case CREDS_CHECK:
147 iint->ima_creds_status = status;
148 break;
149 case FILE_CHECK:
150 case POST_SETATTR:
151 iint->ima_file_status = status;
152 break;
153 case MODULE_CHECK ... MAX_CHECK - 1:
154 default:
155 iint->ima_read_status = status;
156 break;
157 }
158 }
159
ima_cache_flags(struct ima_iint_cache * iint,enum ima_hooks func)160 static void ima_cache_flags(struct ima_iint_cache *iint, enum ima_hooks func)
161 {
162 switch (func) {
163 case MMAP_CHECK:
164 case MMAP_CHECK_REQPROT:
165 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
166 break;
167 case BPRM_CHECK:
168 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
169 break;
170 case CREDS_CHECK:
171 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
172 break;
173 case FILE_CHECK:
174 case POST_SETATTR:
175 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
176 break;
177 case MODULE_CHECK ... MAX_CHECK - 1:
178 default:
179 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
180 break;
181 }
182 }
183
ima_get_hash_algo(const struct evm_ima_xattr_data * xattr_value,int xattr_len)184 enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
185 int xattr_len)
186 {
187 struct signature_v2_hdr *sig;
188 enum hash_algo ret;
189
190 if (!xattr_value || xattr_len < 2)
191 /* return default hash algo */
192 return ima_hash_algo;
193
194 switch (xattr_value->type) {
195 case IMA_VERITY_DIGSIG:
196 sig = (typeof(sig))xattr_value;
197 if (sig->version != 3 || xattr_len <= sizeof(*sig) ||
198 sig->hash_algo >= HASH_ALGO__LAST)
199 return ima_hash_algo;
200 return sig->hash_algo;
201 case EVM_IMA_XATTR_DIGSIG:
202 sig = (typeof(sig))xattr_value;
203 if ((sig->version != 2 && sig->version != 3) ||
204 xattr_len <= sizeof(*sig) ||
205 sig->hash_algo >= HASH_ALGO__LAST)
206 return ima_hash_algo;
207 return sig->hash_algo;
208 case IMA_XATTR_DIGEST_NG:
209 /* first byte contains algorithm id */
210 ret = xattr_value->data[0];
211 if (ret < HASH_ALGO__LAST)
212 return ret;
213 break;
214 case IMA_XATTR_DIGEST:
215 /* this is for backward compatibility */
216 if (xattr_len == 21) {
217 unsigned int zero = 0;
218 if (!memcmp(&xattr_value->data[16], &zero, 4))
219 return HASH_ALGO_MD5;
220 else
221 return HASH_ALGO_SHA1;
222 } else if (xattr_len == 17)
223 return HASH_ALGO_MD5;
224 break;
225 }
226
227 /* return default hash algo */
228 return ima_hash_algo;
229 }
230
ima_read_xattr(struct dentry * dentry,struct evm_ima_xattr_data ** xattr_value,int xattr_len)231 int ima_read_xattr(struct dentry *dentry,
232 struct evm_ima_xattr_data **xattr_value, int xattr_len)
233 {
234 int ret;
235
236 ret = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, XATTR_NAME_IMA,
237 (char **)xattr_value, xattr_len, GFP_NOFS);
238 if (ret == -EOPNOTSUPP)
239 ret = 0;
240 return ret;
241 }
242
243 /*
244 * xattr_verify - verify xattr digest or signature
245 *
246 * Verify whether the hash or signature matches the file contents.
247 *
248 * Return 0 on success, error code otherwise.
249 */
xattr_verify(enum ima_hooks func,struct ima_iint_cache * iint,struct evm_ima_xattr_data * xattr_value,int xattr_len,enum integrity_status * status,const char ** cause)250 static int xattr_verify(enum ima_hooks func, struct ima_iint_cache *iint,
251 struct evm_ima_xattr_data *xattr_value, int xattr_len,
252 enum integrity_status *status, const char **cause)
253 {
254 struct signature_v2_hdr *sig;
255 int rc = -EINVAL, hash_start = 0;
256 int mask;
257
258 switch (xattr_value->type) {
259 case IMA_XATTR_DIGEST_NG:
260 /* first byte contains algorithm id */
261 hash_start = 1;
262 fallthrough;
263 case IMA_XATTR_DIGEST:
264 if (*status != INTEGRITY_PASS_IMMUTABLE) {
265 if (iint->flags & IMA_DIGSIG_REQUIRED) {
266 if (iint->flags & IMA_VERITY_REQUIRED)
267 *cause = "verity-signature-required";
268 else
269 *cause = "IMA-signature-required";
270 *status = INTEGRITY_FAIL;
271 break;
272 }
273 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
274 } else {
275 set_bit(IMA_DIGSIG, &iint->atomic_flags);
276 }
277 /*
278 * Use addition, not subtraction: sizeof() forces unsigned
279 * math and a short xattr_len would wrap around, bypassing
280 * this bounds check.
281 */
282 if (xattr_len >= (int)sizeof(xattr_value->type) + hash_start +
283 (int)iint->ima_hash->length)
284 /*
285 * xattr length may be longer. md5 hash in previous
286 * version occupied 20 bytes in xattr, instead of 16
287 */
288 rc = memcmp(&xattr_value->data[hash_start],
289 iint->ima_hash->digest,
290 iint->ima_hash->length);
291 else
292 rc = -EINVAL;
293 if (rc) {
294 *cause = "invalid-hash";
295 *status = INTEGRITY_FAIL;
296 break;
297 }
298 *status = INTEGRITY_PASS;
299 break;
300 case EVM_IMA_XATTR_DIGSIG:
301 set_bit(IMA_DIGSIG, &iint->atomic_flags);
302
303 mask = IMA_DIGSIG_REQUIRED | IMA_VERITY_REQUIRED;
304 if ((iint->flags & mask) == mask) {
305 *cause = "verity-signature-required";
306 *status = INTEGRITY_FAIL;
307 break;
308 }
309
310 sig = (typeof(sig))xattr_value;
311 if (sig->version > 3) {
312 *cause = "invalid-signature-version";
313 *status = INTEGRITY_FAIL;
314 break;
315 }
316
317 if ((iint->flags & IMA_SIGV3_REQUIRED) && sig->version != 3) {
318 *cause = "IMA-sigv3-required";
319 *status = INTEGRITY_FAIL;
320 break;
321 }
322
323 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
324 (const char *)xattr_value,
325 xattr_len,
326 iint->ima_hash->digest,
327 iint->ima_hash->length,
328 iint->ima_hash->algo);
329 if (rc == -EOPNOTSUPP) {
330 *status = INTEGRITY_UNKNOWN;
331 break;
332 }
333 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
334 func == KEXEC_KERNEL_CHECK)
335 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
336 (const char *)xattr_value,
337 xattr_len,
338 iint->ima_hash->digest,
339 iint->ima_hash->length,
340 iint->ima_hash->algo);
341
342 if (rc) {
343 *cause = "invalid-signature";
344 *status = INTEGRITY_FAIL;
345 } else {
346 *status = INTEGRITY_PASS;
347 }
348 break;
349 case IMA_VERITY_DIGSIG:
350 set_bit(IMA_DIGSIG, &iint->atomic_flags);
351
352 if (iint->flags & IMA_DIGSIG_REQUIRED) {
353 if (!(iint->flags & IMA_VERITY_REQUIRED)) {
354 *cause = "IMA-signature-required";
355 *status = INTEGRITY_FAIL;
356 break;
357 }
358 }
359
360 sig = (typeof(sig))xattr_value;
361 if (sig->version != 3) {
362 *cause = "invalid-signature-version";
363 *status = INTEGRITY_FAIL;
364 break;
365 }
366
367 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
368 (const char *)xattr_value,
369 xattr_len,
370 iint->ima_hash->digest,
371 iint->ima_hash->length,
372 iint->ima_hash->algo);
373 if (rc == -EOPNOTSUPP) {
374 *status = INTEGRITY_UNKNOWN;
375 break;
376 } else if (rc) {
377 *cause = "invalid-verity-signature";
378 *status = INTEGRITY_FAIL;
379 } else {
380 *status = INTEGRITY_PASS;
381 }
382
383 break;
384 default:
385 *status = INTEGRITY_UNKNOWN;
386 *cause = "unknown-ima-data";
387 break;
388 }
389
390 return rc;
391 }
392
393 /*
394 * modsig_verify - verify modsig signature
395 *
396 * Verify whether the signature matches the file contents.
397 *
398 * Return 0 on success, error code otherwise.
399 */
modsig_verify(enum ima_hooks func,const struct modsig * modsig,enum integrity_status * status,const char ** cause)400 static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
401 enum integrity_status *status, const char **cause)
402 {
403 int rc;
404
405 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
406 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
407 func == KEXEC_KERNEL_CHECK)
408 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
409 modsig);
410 if (rc) {
411 *cause = "invalid-signature";
412 *status = INTEGRITY_FAIL;
413 } else {
414 *status = INTEGRITY_PASS;
415 }
416
417 return rc;
418 }
419
420 /*
421 * ima_check_blacklist - determine if the binary is blacklisted.
422 *
423 * Add the hash of the blacklisted binary to the measurement list, based
424 * on policy.
425 *
426 * Returns -EPERM if the hash is blacklisted.
427 */
ima_check_blacklist(struct ima_iint_cache * iint,const struct modsig * modsig,int pcr)428 int ima_check_blacklist(struct ima_iint_cache *iint,
429 const struct modsig *modsig, int pcr)
430 {
431 enum hash_algo hash_algo;
432 const u8 *digest = NULL;
433 u32 digestsize = 0;
434 int rc = 0;
435
436 if (!(iint->flags & IMA_CHECK_BLACKLIST))
437 return 0;
438
439 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
440 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
441
442 rc = is_binary_blacklisted(digest, digestsize);
443 } else if (iint->flags & IMA_DIGSIG_REQUIRED && iint->ima_hash)
444 rc = is_binary_blacklisted(iint->ima_hash->digest, iint->ima_hash->length);
445
446 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
447 process_buffer_measurement(&nop_mnt_idmap, NULL, digest, digestsize,
448 "blacklisted-hash", NONE,
449 pcr, NULL, false, NULL, 0);
450
451 return rc;
452 }
453
454 /*
455 * ima_appraise_measurement - appraise file measurement
456 *
457 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
458 * Assuming success, compare the xattr hash with the collected measurement.
459 *
460 * Return 0 on success, error code otherwise
461 */
ima_appraise_measurement(enum ima_hooks func,struct ima_iint_cache * iint,struct file * file,const unsigned char * filename,struct evm_ima_xattr_data * xattr_value,int xattr_len,const struct modsig * modsig,bool bprm_is_check)462 int ima_appraise_measurement(enum ima_hooks func, struct ima_iint_cache *iint,
463 struct file *file, const unsigned char *filename,
464 struct evm_ima_xattr_data *xattr_value,
465 int xattr_len, const struct modsig *modsig,
466 bool bprm_is_check)
467 {
468 static const char op[] = "appraise_data";
469 int audit_msgno = AUDIT_INTEGRITY_DATA;
470 const char *cause = "unknown";
471 struct dentry *dentry = file_dentry(file);
472 struct inode *inode = d_backing_inode(dentry);
473 enum integrity_status status = INTEGRITY_UNKNOWN;
474 int rc = xattr_len;
475 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
476
477 /* If not appraising a modsig, we need an xattr. */
478 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
479 return INTEGRITY_UNKNOWN;
480
481 /*
482 * Unlike any of the other LSM hooks where the kernel enforces file
483 * integrity, enforcing file integrity for the bprm_creds_for_exec()
484 * LSM hook with the AT_EXECVE_CHECK flag is left up to the discretion
485 * of the script interpreter(userspace). Differentiate kernel and
486 * userspace enforced integrity audit messages.
487 */
488 if (bprm_is_check)
489 audit_msgno = AUDIT_INTEGRITY_USERSPACE;
490
491 /* If reading the xattr failed and there's no modsig, error out. */
492 if (rc <= 0 && !try_modsig) {
493 if (rc && rc != -ENODATA)
494 goto out;
495
496 if (iint->flags & IMA_DIGSIG_REQUIRED) {
497 if (iint->flags & IMA_VERITY_REQUIRED)
498 cause = "verity-signature-required";
499 else
500 cause = "IMA-signature-required";
501 } else {
502 cause = "missing-hash";
503 }
504
505 status = INTEGRITY_NOLABEL;
506 if (file->f_mode & FMODE_CREATED)
507 iint->flags |= IMA_NEW_FILE;
508 if ((iint->flags & IMA_NEW_FILE) &&
509 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
510 (inode->i_size == 0)))
511 status = INTEGRITY_PASS;
512 goto out;
513 }
514
515 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value,
516 rc < 0 ? 0 : rc);
517 switch (status) {
518 case INTEGRITY_PASS:
519 case INTEGRITY_PASS_IMMUTABLE:
520 case INTEGRITY_UNKNOWN:
521 break;
522 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
523 /* It's fine not to have xattrs when using a modsig. */
524 if (try_modsig)
525 break;
526 fallthrough;
527 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
528 cause = "missing-HMAC";
529 goto out;
530 case INTEGRITY_FAIL_IMMUTABLE:
531 set_bit(IMA_DIGSIG, &iint->atomic_flags);
532 cause = "invalid-fail-immutable";
533 goto out;
534 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
535 cause = "invalid-HMAC";
536 goto out;
537 default:
538 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
539 }
540
541 if (xattr_value)
542 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
543 &cause);
544
545 /*
546 * If we have a modsig and either no imasig or the imasig's key isn't
547 * known, then try verifying the modsig.
548 */
549 if (try_modsig &&
550 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
551 rc == -ENOKEY))
552 rc = modsig_verify(func, modsig, &status, &cause);
553
554 out:
555 /*
556 * File signatures on some filesystems can not be properly verified.
557 * When such filesystems are mounted by an untrusted mounter or on a
558 * system not willing to accept such a risk, fail the file signature
559 * verification.
560 */
561 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
562 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
563 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
564 status = INTEGRITY_FAIL;
565 cause = "unverifiable-signature";
566 integrity_audit_msg(audit_msgno, inode, filename,
567 op, cause, rc, 0);
568 } else if (status != INTEGRITY_PASS) {
569 /* Fix mode, but don't replace file signatures. */
570 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
571 (!xattr_value ||
572 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
573 if (!ima_fix_xattr(dentry, iint))
574 status = INTEGRITY_PASS;
575 } else if (status == INTEGRITY_NOLABEL) {
576 if (!evm_fix_hmac(dentry, XATTR_NAME_IMA,
577 (const char *)xattr_value,
578 xattr_len))
579 status = INTEGRITY_PASS;
580 }
581
582 /*
583 * Permit new files with file/EVM portable signatures, but
584 * without data.
585 */
586 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
587 test_bit(IMA_DIGSIG, &iint->atomic_flags)) {
588 status = INTEGRITY_PASS;
589 }
590
591 integrity_audit_msg(audit_msgno, inode, filename,
592 op, cause, rc, 0);
593 } else {
594 ima_cache_flags(iint, func);
595 }
596
597 ima_set_cache_status(iint, func, status);
598 return status;
599 }
600
601 /*
602 * ima_update_xattr - update 'security.ima' hash value
603 */
ima_update_xattr(struct ima_iint_cache * iint,struct file * file)604 void ima_update_xattr(struct ima_iint_cache *iint, struct file *file)
605 {
606 struct dentry *dentry = file_dentry(file);
607 int rc = 0;
608
609 /* do not collect and update hash for digital signatures */
610 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
611 return;
612
613 if ((iint->ima_file_status != INTEGRITY_PASS) &&
614 !(iint->flags & IMA_HASH))
615 return;
616
617 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
618 if (rc < 0)
619 return;
620
621 inode_lock(file_inode(file));
622 ima_fix_xattr(dentry, iint);
623 inode_unlock(file_inode(file));
624 }
625
626 /**
627 * ima_inode_post_setattr - reflect file metadata changes
628 * @idmap: idmap of the mount the inode was found from
629 * @dentry: pointer to the affected dentry
630 * @ia_valid: for the UID and GID status
631 *
632 * Changes to a dentry's metadata might result in needing to appraise.
633 *
634 * This function is called from notify_change(), which expects the caller
635 * to lock the inode's i_mutex.
636 */
ima_inode_post_setattr(struct mnt_idmap * idmap,struct dentry * dentry,int ia_valid)637 static void ima_inode_post_setattr(struct mnt_idmap *idmap,
638 struct dentry *dentry, int ia_valid)
639 {
640 struct inode *inode = d_backing_inode(dentry);
641 struct ima_iint_cache *iint;
642 int action;
643
644 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
645 || !(inode->i_opflags & IOP_XATTR))
646 return;
647
648 action = ima_must_appraise(idmap, inode, MAY_ACCESS, POST_SETATTR);
649 iint = ima_iint_find(inode);
650 if (iint) {
651 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
652 if (!action)
653 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
654 }
655 }
656
657 /*
658 * ima_protect_xattr - protect 'security.ima'
659 *
660 * Ensure that not just anyone can modify or remove 'security.ima'.
661 */
ima_protect_xattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)662 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
663 const void *xattr_value, size_t xattr_value_len)
664 {
665 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
666 if (!capable(CAP_SYS_ADMIN))
667 return -EPERM;
668 return 1;
669 }
670 return 0;
671 }
672
673 /*
674 * ima_reset_appraise_flags - reset ima_iint_cache flags
675 *
676 * @digsig: whether to clear/set IMA_DIGSIG flag, tristate values
677 * 0: clear IMA_DIGSIG
678 * 1: set IMA_DIGSIG
679 * -1: don't change IMA_DIGSIG
680 *
681 */
ima_reset_appraise_flags(struct inode * inode,int digsig)682 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
683 {
684 struct ima_iint_cache *iint;
685
686 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
687 return;
688
689 iint = ima_iint_find(inode);
690 if (!iint)
691 return;
692 iint->measured_pcrs = 0;
693 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
694 if (digsig == 1)
695 set_bit(IMA_DIGSIG, &iint->atomic_flags);
696 else if (digsig == 0)
697 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
698 }
699
700 /**
701 * validate_hash_algo() - Block setxattr with unsupported hash algorithms
702 * @dentry: object of the setxattr()
703 * @xattr_value: userland supplied xattr value
704 * @xattr_value_len: length of xattr_value
705 *
706 * The xattr value is mapped to its hash algorithm, and this algorithm
707 * must be built in the kernel for the setxattr to be allowed.
708 *
709 * Emit an audit message when the algorithm is invalid.
710 *
711 * Return: 0 on success, else an error.
712 */
validate_hash_algo(struct dentry * dentry,const struct evm_ima_xattr_data * xattr_value,size_t xattr_value_len)713 static int validate_hash_algo(struct dentry *dentry,
714 const struct evm_ima_xattr_data *xattr_value,
715 size_t xattr_value_len)
716 {
717 char *path = NULL, *pathbuf = NULL;
718 enum hash_algo xattr_hash_algo;
719 const char *errmsg = "unavailable-hash-algorithm";
720 unsigned int allowed_hashes;
721
722 xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len);
723
724 allowed_hashes = atomic_read(&ima_setxattr_allowed_hash_algorithms);
725
726 if (allowed_hashes) {
727 /* success if the algorithm is allowed in the ima policy */
728 if (allowed_hashes & (1U << xattr_hash_algo))
729 return 0;
730
731 /*
732 * We use a different audit message when the hash algorithm
733 * is denied by a policy rule, instead of not being built
734 * in the kernel image
735 */
736 errmsg = "denied-hash-algorithm";
737 } else {
738 if (likely(xattr_hash_algo == ima_hash_algo))
739 return 0;
740
741 /* allow any xattr using an algorithm built in the kernel */
742 if (crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0))
743 return 0;
744 }
745
746 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
747 if (!pathbuf)
748 return -EACCES;
749
750 path = dentry_path(dentry, pathbuf, PATH_MAX);
751
752 integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path,
753 "set_data", errmsg, -EACCES, 0);
754
755 kfree(pathbuf);
756
757 return -EACCES;
758 }
759
ima_inode_setxattr(struct mnt_idmap * idmap,struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len,int flags)760 static int ima_inode_setxattr(struct mnt_idmap *idmap, struct dentry *dentry,
761 const char *xattr_name, const void *xattr_value,
762 size_t xattr_value_len, int flags)
763 {
764 const struct evm_ima_xattr_data *xvalue = xattr_value;
765 int digsig = 0;
766 int result;
767 int err;
768
769 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
770 xattr_value_len);
771 if (result == 1) {
772 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
773 return -EINVAL;
774
775 err = validate_hash_algo(dentry, xvalue, xattr_value_len);
776 if (err)
777 return err;
778
779 digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG);
780 } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) {
781 digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG);
782 } else {
783 digsig = -1;
784 }
785 if (result == 1 || evm_revalidate_status(xattr_name)) {
786 ima_reset_appraise_flags(d_backing_inode(dentry), digsig);
787 if (result == 1)
788 result = 0;
789 }
790 return result;
791 }
792
ima_inode_set_acl(struct mnt_idmap * idmap,struct dentry * dentry,const char * acl_name,struct posix_acl * kacl)793 static int ima_inode_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
794 const char *acl_name, struct posix_acl *kacl)
795 {
796 if (evm_revalidate_status(acl_name))
797 ima_reset_appraise_flags(d_backing_inode(dentry), -1);
798
799 return 0;
800 }
801
ima_inode_removexattr(struct mnt_idmap * idmap,struct dentry * dentry,const char * xattr_name)802 static int ima_inode_removexattr(struct mnt_idmap *idmap, struct dentry *dentry,
803 const char *xattr_name)
804 {
805 int result, digsig = -1;
806
807 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
808 if (result == 1 || evm_revalidate_status(xattr_name)) {
809 if (!strcmp(xattr_name, XATTR_NAME_IMA))
810 digsig = 0;
811 ima_reset_appraise_flags(d_backing_inode(dentry), digsig);
812 if (result == 1)
813 result = 0;
814 }
815 return result;
816 }
817
ima_inode_remove_acl(struct mnt_idmap * idmap,struct dentry * dentry,const char * acl_name)818 static int ima_inode_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry,
819 const char *acl_name)
820 {
821 return ima_inode_set_acl(idmap, dentry, acl_name, NULL);
822 }
823
824 static struct security_hook_list ima_appraise_hooks[] __ro_after_init = {
825 LSM_HOOK_INIT(inode_post_setattr, ima_inode_post_setattr),
826 LSM_HOOK_INIT(inode_setxattr, ima_inode_setxattr),
827 LSM_HOOK_INIT(inode_set_acl, ima_inode_set_acl),
828 LSM_HOOK_INIT(inode_removexattr, ima_inode_removexattr),
829 LSM_HOOK_INIT(inode_remove_acl, ima_inode_remove_acl),
830 };
831
init_ima_appraise_lsm(const struct lsm_id * lsmid)832 void __init init_ima_appraise_lsm(const struct lsm_id *lsmid)
833 {
834 security_add_hooks(ima_appraise_hooks, ARRAY_SIZE(ima_appraise_hooks),
835 lsmid);
836 }
837