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