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