xref: /linux/security/integrity/ima/ima_appraise.c (revision a1c3be890440a1769ed6f822376a3e3ab0d42994)
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/fs.h>
12 #include <linux/xattr.h>
13 #include <linux/magic.h>
14 #include <linux/ima.h>
15 #include <linux/evm.h>
16 #include <keys/system_keyring.h>
17 
18 #include "ima.h"
19 
20 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
21 static char *ima_appraise_cmdline_default __initdata;
22 core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0);
23 
24 void __init ima_appraise_parse_cmdline(void)
25 {
26 	const char *str = ima_appraise_cmdline_default;
27 	bool sb_state = arch_ima_get_secureboot();
28 	int appraisal_state = ima_appraise;
29 
30 	if (!str)
31 		return;
32 
33 	if (strncmp(str, "off", 3) == 0)
34 		appraisal_state = 0;
35 	else if (strncmp(str, "log", 3) == 0)
36 		appraisal_state = IMA_APPRAISE_LOG;
37 	else if (strncmp(str, "fix", 3) == 0)
38 		appraisal_state = IMA_APPRAISE_FIX;
39 	else if (strncmp(str, "enforce", 7) == 0)
40 		appraisal_state = IMA_APPRAISE_ENFORCE;
41 	else
42 		pr_err("invalid \"%s\" appraise option", str);
43 
44 	/* If appraisal state was changed, but secure boot is enabled,
45 	 * keep its default */
46 	if (sb_state) {
47 		if (!(appraisal_state & IMA_APPRAISE_ENFORCE))
48 			pr_info("Secure boot enabled: ignoring ima_appraise=%s option",
49 				str);
50 	} else {
51 		ima_appraise = appraisal_state;
52 	}
53 }
54 #endif
55 
56 /*
57  * is_ima_appraise_enabled - return appraise status
58  *
59  * Only return enabled, if not in ima_appraise="fix" or "log" modes.
60  */
61 bool is_ima_appraise_enabled(void)
62 {
63 	return ima_appraise & IMA_APPRAISE_ENFORCE;
64 }
65 
66 /*
67  * ima_must_appraise - set appraise flag
68  *
69  * Return 1 to appraise or hash
70  */
71 int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode,
72 		      int mask, enum ima_hooks func)
73 {
74 	u32 secid;
75 
76 	if (!ima_appraise)
77 		return 0;
78 
79 	security_task_getsecid(current, &secid);
80 	return ima_match_policy(mnt_userns, inode, current_cred(), secid, func,
81 				mask, IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL);
82 }
83 
84 static int ima_fix_xattr(struct dentry *dentry,
85 			 struct integrity_iint_cache *iint)
86 {
87 	int rc, offset;
88 	u8 algo = iint->ima_hash->algo;
89 
90 	if (algo <= HASH_ALGO_SHA1) {
91 		offset = 1;
92 		iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
93 	} else {
94 		offset = 0;
95 		iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
96 		iint->ima_hash->xattr.ng.algo = algo;
97 	}
98 	rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA,
99 				   &iint->ima_hash->xattr.data[offset],
100 				   (sizeof(iint->ima_hash->xattr) - offset) +
101 				   iint->ima_hash->length, 0);
102 	return rc;
103 }
104 
105 /* Return specific func appraised cached result */
106 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
107 					   enum ima_hooks func)
108 {
109 	switch (func) {
110 	case MMAP_CHECK:
111 		return iint->ima_mmap_status;
112 	case BPRM_CHECK:
113 		return iint->ima_bprm_status;
114 	case CREDS_CHECK:
115 		return iint->ima_creds_status;
116 	case FILE_CHECK:
117 	case POST_SETATTR:
118 		return iint->ima_file_status;
119 	case MODULE_CHECK ... MAX_CHECK - 1:
120 	default:
121 		return iint->ima_read_status;
122 	}
123 }
124 
125 static void ima_set_cache_status(struct integrity_iint_cache *iint,
126 				 enum ima_hooks func,
127 				 enum integrity_status status)
128 {
129 	switch (func) {
130 	case MMAP_CHECK:
131 		iint->ima_mmap_status = status;
132 		break;
133 	case BPRM_CHECK:
134 		iint->ima_bprm_status = status;
135 		break;
136 	case CREDS_CHECK:
137 		iint->ima_creds_status = status;
138 		break;
139 	case FILE_CHECK:
140 	case POST_SETATTR:
141 		iint->ima_file_status = status;
142 		break;
143 	case MODULE_CHECK ... MAX_CHECK - 1:
144 	default:
145 		iint->ima_read_status = status;
146 		break;
147 	}
148 }
149 
150 static void ima_cache_flags(struct integrity_iint_cache *iint,
151 			     enum ima_hooks func)
152 {
153 	switch (func) {
154 	case MMAP_CHECK:
155 		iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
156 		break;
157 	case BPRM_CHECK:
158 		iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
159 		break;
160 	case CREDS_CHECK:
161 		iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
162 		break;
163 	case FILE_CHECK:
164 	case POST_SETATTR:
165 		iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
166 		break;
167 	case MODULE_CHECK ... MAX_CHECK - 1:
168 	default:
169 		iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
170 		break;
171 	}
172 }
173 
174 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
175 				 int xattr_len)
176 {
177 	struct signature_v2_hdr *sig;
178 	enum hash_algo ret;
179 
180 	if (!xattr_value || xattr_len < 2)
181 		/* return default hash algo */
182 		return ima_hash_algo;
183 
184 	switch (xattr_value->type) {
185 	case EVM_IMA_XATTR_DIGSIG:
186 		sig = (typeof(sig))xattr_value;
187 		if (sig->version != 2 || xattr_len <= sizeof(*sig))
188 			return ima_hash_algo;
189 		return sig->hash_algo;
190 		break;
191 	case IMA_XATTR_DIGEST_NG:
192 		/* first byte contains algorithm id */
193 		ret = xattr_value->data[0];
194 		if (ret < HASH_ALGO__LAST)
195 			return ret;
196 		break;
197 	case IMA_XATTR_DIGEST:
198 		/* this is for backward compatibility */
199 		if (xattr_len == 21) {
200 			unsigned int zero = 0;
201 			if (!memcmp(&xattr_value->data[16], &zero, 4))
202 				return HASH_ALGO_MD5;
203 			else
204 				return HASH_ALGO_SHA1;
205 		} else if (xattr_len == 17)
206 			return HASH_ALGO_MD5;
207 		break;
208 	}
209 
210 	/* return default hash algo */
211 	return ima_hash_algo;
212 }
213 
214 int ima_read_xattr(struct dentry *dentry,
215 		   struct evm_ima_xattr_data **xattr_value)
216 {
217 	ssize_t ret;
218 
219 	ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA,
220 				 (char **)xattr_value, 0, GFP_NOFS);
221 	if (ret == -EOPNOTSUPP)
222 		ret = 0;
223 	return ret;
224 }
225 
226 /*
227  * xattr_verify - verify xattr digest or signature
228  *
229  * Verify whether the hash or signature matches the file contents.
230  *
231  * Return 0 on success, error code otherwise.
232  */
233 static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
234 			struct evm_ima_xattr_data *xattr_value, int xattr_len,
235 			enum integrity_status *status, const char **cause)
236 {
237 	int rc = -EINVAL, hash_start = 0;
238 
239 	switch (xattr_value->type) {
240 	case IMA_XATTR_DIGEST_NG:
241 		/* first byte contains algorithm id */
242 		hash_start = 1;
243 		fallthrough;
244 	case IMA_XATTR_DIGEST:
245 		if (iint->flags & IMA_DIGSIG_REQUIRED) {
246 			*cause = "IMA-signature-required";
247 			*status = INTEGRITY_FAIL;
248 			break;
249 		}
250 		clear_bit(IMA_DIGSIG, &iint->atomic_flags);
251 		if (xattr_len - sizeof(xattr_value->type) - hash_start >=
252 				iint->ima_hash->length)
253 			/*
254 			 * xattr length may be longer. md5 hash in previous
255 			 * version occupied 20 bytes in xattr, instead of 16
256 			 */
257 			rc = memcmp(&xattr_value->data[hash_start],
258 				    iint->ima_hash->digest,
259 				    iint->ima_hash->length);
260 		else
261 			rc = -EINVAL;
262 		if (rc) {
263 			*cause = "invalid-hash";
264 			*status = INTEGRITY_FAIL;
265 			break;
266 		}
267 		*status = INTEGRITY_PASS;
268 		break;
269 	case EVM_IMA_XATTR_DIGSIG:
270 		set_bit(IMA_DIGSIG, &iint->atomic_flags);
271 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
272 					     (const char *)xattr_value,
273 					     xattr_len,
274 					     iint->ima_hash->digest,
275 					     iint->ima_hash->length);
276 		if (rc == -EOPNOTSUPP) {
277 			*status = INTEGRITY_UNKNOWN;
278 			break;
279 		}
280 		if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
281 		    func == KEXEC_KERNEL_CHECK)
282 			rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
283 						     (const char *)xattr_value,
284 						     xattr_len,
285 						     iint->ima_hash->digest,
286 						     iint->ima_hash->length);
287 		if (rc) {
288 			*cause = "invalid-signature";
289 			*status = INTEGRITY_FAIL;
290 		} else {
291 			*status = INTEGRITY_PASS;
292 		}
293 		break;
294 	default:
295 		*status = INTEGRITY_UNKNOWN;
296 		*cause = "unknown-ima-data";
297 		break;
298 	}
299 
300 	return rc;
301 }
302 
303 /*
304  * modsig_verify - verify modsig signature
305  *
306  * Verify whether the signature matches the file contents.
307  *
308  * Return 0 on success, error code otherwise.
309  */
310 static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
311 			 enum integrity_status *status, const char **cause)
312 {
313 	int rc;
314 
315 	rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
316 	if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
317 	    func == KEXEC_KERNEL_CHECK)
318 		rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
319 					     modsig);
320 	if (rc) {
321 		*cause = "invalid-signature";
322 		*status = INTEGRITY_FAIL;
323 	} else {
324 		*status = INTEGRITY_PASS;
325 	}
326 
327 	return rc;
328 }
329 
330 /*
331  * ima_check_blacklist - determine if the binary is blacklisted.
332  *
333  * Add the hash of the blacklisted binary to the measurement list, based
334  * on policy.
335  *
336  * Returns -EPERM if the hash is blacklisted.
337  */
338 int ima_check_blacklist(struct integrity_iint_cache *iint,
339 			const struct modsig *modsig, int pcr)
340 {
341 	enum hash_algo hash_algo;
342 	const u8 *digest = NULL;
343 	u32 digestsize = 0;
344 	int rc = 0;
345 
346 	if (!(iint->flags & IMA_CHECK_BLACKLIST))
347 		return 0;
348 
349 	if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
350 		ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
351 
352 		rc = is_binary_blacklisted(digest, digestsize);
353 		if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
354 			process_buffer_measurement(&init_user_ns, NULL, digest, digestsize,
355 						   "blacklisted-hash", NONE,
356 						   pcr, NULL, false);
357 	}
358 
359 	return rc;
360 }
361 
362 /*
363  * ima_appraise_measurement - appraise file measurement
364  *
365  * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
366  * Assuming success, compare the xattr hash with the collected measurement.
367  *
368  * Return 0 on success, error code otherwise
369  */
370 int ima_appraise_measurement(enum ima_hooks func,
371 			     struct integrity_iint_cache *iint,
372 			     struct file *file, const unsigned char *filename,
373 			     struct evm_ima_xattr_data *xattr_value,
374 			     int xattr_len, const struct modsig *modsig)
375 {
376 	static const char op[] = "appraise_data";
377 	const char *cause = "unknown";
378 	struct dentry *dentry = file_dentry(file);
379 	struct inode *inode = d_backing_inode(dentry);
380 	enum integrity_status status = INTEGRITY_UNKNOWN;
381 	int rc = xattr_len;
382 	bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
383 
384 	/* If not appraising a modsig, we need an xattr. */
385 	if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
386 		return INTEGRITY_UNKNOWN;
387 
388 	/* If reading the xattr failed and there's no modsig, error out. */
389 	if (rc <= 0 && !try_modsig) {
390 		if (rc && rc != -ENODATA)
391 			goto out;
392 
393 		cause = iint->flags & IMA_DIGSIG_REQUIRED ?
394 				"IMA-signature-required" : "missing-hash";
395 		status = INTEGRITY_NOLABEL;
396 		if (file->f_mode & FMODE_CREATED)
397 			iint->flags |= IMA_NEW_FILE;
398 		if ((iint->flags & IMA_NEW_FILE) &&
399 		    (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
400 		     (inode->i_size == 0)))
401 			status = INTEGRITY_PASS;
402 		goto out;
403 	}
404 
405 	status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
406 	switch (status) {
407 	case INTEGRITY_PASS:
408 	case INTEGRITY_PASS_IMMUTABLE:
409 	case INTEGRITY_UNKNOWN:
410 		break;
411 	case INTEGRITY_NOXATTRS:	/* No EVM protected xattrs. */
412 		/* It's fine not to have xattrs when using a modsig. */
413 		if (try_modsig)
414 			break;
415 		fallthrough;
416 	case INTEGRITY_NOLABEL:		/* No security.evm xattr. */
417 		cause = "missing-HMAC";
418 		goto out;
419 	case INTEGRITY_FAIL:		/* Invalid HMAC/signature. */
420 		cause = "invalid-HMAC";
421 		goto out;
422 	default:
423 		WARN_ONCE(true, "Unexpected integrity status %d\n", status);
424 	}
425 
426 	if (xattr_value)
427 		rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
428 				  &cause);
429 
430 	/*
431 	 * If we have a modsig and either no imasig or the imasig's key isn't
432 	 * known, then try verifying the modsig.
433 	 */
434 	if (try_modsig &&
435 	    (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
436 	     rc == -ENOKEY))
437 		rc = modsig_verify(func, modsig, &status, &cause);
438 
439 out:
440 	/*
441 	 * File signatures on some filesystems can not be properly verified.
442 	 * When such filesystems are mounted by an untrusted mounter or on a
443 	 * system not willing to accept such a risk, fail the file signature
444 	 * verification.
445 	 */
446 	if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
447 	    ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
448 	     (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
449 		status = INTEGRITY_FAIL;
450 		cause = "unverifiable-signature";
451 		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
452 				    op, cause, rc, 0);
453 	} else if (status != INTEGRITY_PASS) {
454 		/* Fix mode, but don't replace file signatures. */
455 		if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
456 		    (!xattr_value ||
457 		     xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
458 			if (!ima_fix_xattr(dentry, iint))
459 				status = INTEGRITY_PASS;
460 		}
461 
462 		/* Permit new files with file signatures, but without data. */
463 		if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
464 		    xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
465 			status = INTEGRITY_PASS;
466 		}
467 
468 		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
469 				    op, cause, rc, 0);
470 	} else {
471 		ima_cache_flags(iint, func);
472 	}
473 
474 	ima_set_cache_status(iint, func, status);
475 	return status;
476 }
477 
478 /*
479  * ima_update_xattr - update 'security.ima' hash value
480  */
481 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
482 {
483 	struct dentry *dentry = file_dentry(file);
484 	int rc = 0;
485 
486 	/* do not collect and update hash for digital signatures */
487 	if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
488 		return;
489 
490 	if ((iint->ima_file_status != INTEGRITY_PASS) &&
491 	    !(iint->flags & IMA_HASH))
492 		return;
493 
494 	rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
495 	if (rc < 0)
496 		return;
497 
498 	inode_lock(file_inode(file));
499 	ima_fix_xattr(dentry, iint);
500 	inode_unlock(file_inode(file));
501 }
502 
503 /**
504  * ima_inode_post_setattr - reflect file metadata changes
505  * @mnt_userns:	user namespace of the mount the inode was found from
506  * @dentry: pointer to the affected dentry
507  *
508  * Changes to a dentry's metadata might result in needing to appraise.
509  *
510  * This function is called from notify_change(), which expects the caller
511  * to lock the inode's i_mutex.
512  */
513 void ima_inode_post_setattr(struct user_namespace *mnt_userns,
514 			    struct dentry *dentry)
515 {
516 	struct inode *inode = d_backing_inode(dentry);
517 	struct integrity_iint_cache *iint;
518 	int action;
519 
520 	if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
521 	    || !(inode->i_opflags & IOP_XATTR))
522 		return;
523 
524 	action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR);
525 	if (!action)
526 		__vfs_removexattr(&init_user_ns, dentry, XATTR_NAME_IMA);
527 	iint = integrity_iint_find(inode);
528 	if (iint) {
529 		set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
530 		if (!action)
531 			clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
532 	}
533 }
534 
535 /*
536  * ima_protect_xattr - protect 'security.ima'
537  *
538  * Ensure that not just anyone can modify or remove 'security.ima'.
539  */
540 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
541 			     const void *xattr_value, size_t xattr_value_len)
542 {
543 	if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
544 		if (!capable(CAP_SYS_ADMIN))
545 			return -EPERM;
546 		return 1;
547 	}
548 	return 0;
549 }
550 
551 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
552 {
553 	struct integrity_iint_cache *iint;
554 
555 	if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
556 		return;
557 
558 	iint = integrity_iint_find(inode);
559 	if (!iint)
560 		return;
561 	iint->measured_pcrs = 0;
562 	set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
563 	if (digsig)
564 		set_bit(IMA_DIGSIG, &iint->atomic_flags);
565 	else
566 		clear_bit(IMA_DIGSIG, &iint->atomic_flags);
567 }
568 
569 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
570 		       const void *xattr_value, size_t xattr_value_len)
571 {
572 	const struct evm_ima_xattr_data *xvalue = xattr_value;
573 	int result;
574 
575 	result = ima_protect_xattr(dentry, xattr_name, xattr_value,
576 				   xattr_value_len);
577 	if (result == 1) {
578 		if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
579 			return -EINVAL;
580 		ima_reset_appraise_flags(d_backing_inode(dentry),
581 			xvalue->type == EVM_IMA_XATTR_DIGSIG);
582 		result = 0;
583 	}
584 	return result;
585 }
586 
587 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
588 {
589 	int result;
590 
591 	result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
592 	if (result == 1) {
593 		ima_reset_appraise_flags(d_backing_inode(dentry), 0);
594 		result = 0;
595 	}
596 	return result;
597 }
598