xref: /openbmc/linux/security/integrity/ima/ima_appraise.c (revision a2d2329e30e224ea68d575d2525b866df9805ea0)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22fe5d6deSMimi Zohar /*
32fe5d6deSMimi Zohar  * Copyright (C) 2011 IBM Corporation
42fe5d6deSMimi Zohar  *
52fe5d6deSMimi Zohar  * Author:
62fe5d6deSMimi Zohar  * Mimi Zohar <zohar@us.ibm.com>
72fe5d6deSMimi Zohar  */
8b000d5cbSArd Biesheuvel #include <linux/module.h>
9876979c9SPaul Gortmaker #include <linux/init.h>
102fe5d6deSMimi Zohar #include <linux/file.h>
112fe5d6deSMimi Zohar #include <linux/fs.h>
122fe5d6deSMimi Zohar #include <linux/xattr.h>
132fe5d6deSMimi Zohar #include <linux/magic.h>
142fe5d6deSMimi Zohar #include <linux/ima.h>
152fe5d6deSMimi Zohar #include <linux/evm.h>
16273df864SNayna Jain #include <keys/system_keyring.h>
172fe5d6deSMimi Zohar 
182fe5d6deSMimi Zohar #include "ima.h"
192fe5d6deSMimi Zohar 
20e1f5e01fSMimi Zohar #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
21b000d5cbSArd Biesheuvel static char *ima_appraise_cmdline_default __initdata;
22b000d5cbSArd Biesheuvel core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0);
23b000d5cbSArd Biesheuvel 
24b000d5cbSArd Biesheuvel void __init ima_appraise_parse_cmdline(void)
25b000d5cbSArd Biesheuvel {
26b000d5cbSArd Biesheuvel 	const char *str = ima_appraise_cmdline_default;
27e4d7e2dfSBruno Meneguele 	bool sb_state = arch_ima_get_secureboot();
28e4d7e2dfSBruno Meneguele 	int appraisal_state = ima_appraise;
29311aa6aaSBruno Meneguele 
30b000d5cbSArd Biesheuvel 	if (!str)
31b000d5cbSArd Biesheuvel 		return;
32b000d5cbSArd Biesheuvel 
332fe5d6deSMimi Zohar 	if (strncmp(str, "off", 3) == 0)
34e4d7e2dfSBruno Meneguele 		appraisal_state = 0;
352faa6ef3SDmitry Kasatkin 	else if (strncmp(str, "log", 3) == 0)
36e4d7e2dfSBruno Meneguele 		appraisal_state = IMA_APPRAISE_LOG;
372fe5d6deSMimi Zohar 	else if (strncmp(str, "fix", 3) == 0)
38e4d7e2dfSBruno Meneguele 		appraisal_state = IMA_APPRAISE_FIX;
394afb28abSBruno Meneguele 	else if (strncmp(str, "enforce", 7) == 0)
40e4d7e2dfSBruno Meneguele 		appraisal_state = IMA_APPRAISE_ENFORCE;
417fe2bb7eSBruno Meneguele 	else
427fe2bb7eSBruno Meneguele 		pr_err("invalid \"%s\" appraise option", str);
43e4d7e2dfSBruno Meneguele 
44e4d7e2dfSBruno Meneguele 	/* If appraisal state was changed, but secure boot is enabled,
45e4d7e2dfSBruno Meneguele 	 * keep its default */
46e4d7e2dfSBruno Meneguele 	if (sb_state) {
47e4d7e2dfSBruno Meneguele 		if (!(appraisal_state & IMA_APPRAISE_ENFORCE))
48e4d7e2dfSBruno Meneguele 			pr_info("Secure boot enabled: ignoring ima_appraise=%s option",
49e4d7e2dfSBruno Meneguele 				str);
50e4d7e2dfSBruno Meneguele 	} else {
51e4d7e2dfSBruno Meneguele 		ima_appraise = appraisal_state;
52e4d7e2dfSBruno Meneguele 	}
532fe5d6deSMimi Zohar }
54b000d5cbSArd Biesheuvel #endif
552fe5d6deSMimi Zohar 
562fe5d6deSMimi Zohar /*
576f6723e2SMimi Zohar  * is_ima_appraise_enabled - return appraise status
586f6723e2SMimi Zohar  *
596f6723e2SMimi Zohar  * Only return enabled, if not in ima_appraise="fix" or "log" modes.
606f6723e2SMimi Zohar  */
616f6723e2SMimi Zohar bool is_ima_appraise_enabled(void)
626f6723e2SMimi Zohar {
63e5729f86SThiago Jung Bauermann 	return ima_appraise & IMA_APPRAISE_ENFORCE;
646f6723e2SMimi Zohar }
656f6723e2SMimi Zohar 
666f6723e2SMimi Zohar /*
672fe5d6deSMimi Zohar  * ima_must_appraise - set appraise flag
682fe5d6deSMimi Zohar  *
69da1b0029SMimi Zohar  * Return 1 to appraise or hash
702fe5d6deSMimi Zohar  */
71*a2d2329eSChristian Brauner int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode,
72*a2d2329eSChristian Brauner 		      int mask, enum ima_hooks func)
732fe5d6deSMimi Zohar {
74d906c10dSMatthew Garrett 	u32 secid;
75d906c10dSMatthew Garrett 
7607f6a794SMimi Zohar 	if (!ima_appraise)
772fe5d6deSMimi Zohar 		return 0;
7807f6a794SMimi Zohar 
79d906c10dSMatthew Garrett 	security_task_getsecid(current, &secid);
80*a2d2329eSChristian Brauner 	return ima_match_policy(mnt_userns, inode, current_cred(), secid, func,
81*a2d2329eSChristian Brauner 				mask, IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL);
822fe5d6deSMimi Zohar }
832fe5d6deSMimi Zohar 
84def3e8b9SDmitry Kasatkin static int ima_fix_xattr(struct dentry *dentry,
852fe5d6deSMimi Zohar 			 struct integrity_iint_cache *iint)
862fe5d6deSMimi Zohar {
873ea7a560SDmitry Kasatkin 	int rc, offset;
883ea7a560SDmitry Kasatkin 	u8 algo = iint->ima_hash->algo;
893ea7a560SDmitry Kasatkin 
903ea7a560SDmitry Kasatkin 	if (algo <= HASH_ALGO_SHA1) {
913ea7a560SDmitry Kasatkin 		offset = 1;
923ea7a560SDmitry Kasatkin 		iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
933ea7a560SDmitry Kasatkin 	} else {
943ea7a560SDmitry Kasatkin 		offset = 0;
953ea7a560SDmitry Kasatkin 		iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
963ea7a560SDmitry Kasatkin 		iint->ima_hash->xattr.ng.algo = algo;
973ea7a560SDmitry Kasatkin 	}
98c7c7a1a1STycho Andersen 	rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA,
993ea7a560SDmitry Kasatkin 				   &iint->ima_hash->xattr.data[offset],
1003ea7a560SDmitry Kasatkin 				   (sizeof(iint->ima_hash->xattr) - offset) +
1013ea7a560SDmitry Kasatkin 				   iint->ima_hash->length, 0);
1023ea7a560SDmitry Kasatkin 	return rc;
1032fe5d6deSMimi Zohar }
1042fe5d6deSMimi Zohar 
105d79d72e0SMimi Zohar /* Return specific func appraised cached result */
106d79d72e0SMimi Zohar enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
1074ad87a3dSMimi Zohar 					   enum ima_hooks func)
108d79d72e0SMimi Zohar {
109d79d72e0SMimi Zohar 	switch (func) {
110d79d72e0SMimi Zohar 	case MMAP_CHECK:
111d79d72e0SMimi Zohar 		return iint->ima_mmap_status;
112d79d72e0SMimi Zohar 	case BPRM_CHECK:
113d79d72e0SMimi Zohar 		return iint->ima_bprm_status;
114d906c10dSMatthew Garrett 	case CREDS_CHECK:
115d906c10dSMatthew Garrett 		return iint->ima_creds_status;
116d79d72e0SMimi Zohar 	case FILE_CHECK:
117c6af8efeSMimi Zohar 	case POST_SETATTR:
118d79d72e0SMimi Zohar 		return iint->ima_file_status;
119c6af8efeSMimi Zohar 	case MODULE_CHECK ... MAX_CHECK - 1:
120c6af8efeSMimi Zohar 	default:
121c6af8efeSMimi Zohar 		return iint->ima_read_status;
122d79d72e0SMimi Zohar 	}
123d79d72e0SMimi Zohar }
124d79d72e0SMimi Zohar 
125d79d72e0SMimi Zohar static void ima_set_cache_status(struct integrity_iint_cache *iint,
1264ad87a3dSMimi Zohar 				 enum ima_hooks func,
1274ad87a3dSMimi Zohar 				 enum integrity_status status)
128d79d72e0SMimi Zohar {
129d79d72e0SMimi Zohar 	switch (func) {
130d79d72e0SMimi Zohar 	case MMAP_CHECK:
131d79d72e0SMimi Zohar 		iint->ima_mmap_status = status;
132d79d72e0SMimi Zohar 		break;
133d79d72e0SMimi Zohar 	case BPRM_CHECK:
134d79d72e0SMimi Zohar 		iint->ima_bprm_status = status;
135d79d72e0SMimi Zohar 		break;
136d906c10dSMatthew Garrett 	case CREDS_CHECK:
137d906c10dSMatthew Garrett 		iint->ima_creds_status = status;
13809186e50SGustavo A. R. Silva 		break;
139d79d72e0SMimi Zohar 	case FILE_CHECK:
140c6af8efeSMimi Zohar 	case POST_SETATTR:
141d79d72e0SMimi Zohar 		iint->ima_file_status = status;
142c6af8efeSMimi Zohar 		break;
143c6af8efeSMimi Zohar 	case MODULE_CHECK ... MAX_CHECK - 1:
144c6af8efeSMimi Zohar 	default:
145c6af8efeSMimi Zohar 		iint->ima_read_status = status;
146c6af8efeSMimi Zohar 		break;
147d79d72e0SMimi Zohar 	}
148d79d72e0SMimi Zohar }
149d79d72e0SMimi Zohar 
1504ad87a3dSMimi Zohar static void ima_cache_flags(struct integrity_iint_cache *iint,
1514ad87a3dSMimi Zohar 			     enum ima_hooks func)
152d79d72e0SMimi Zohar {
153d79d72e0SMimi Zohar 	switch (func) {
154d79d72e0SMimi Zohar 	case MMAP_CHECK:
155d79d72e0SMimi Zohar 		iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
156d79d72e0SMimi Zohar 		break;
157d79d72e0SMimi Zohar 	case BPRM_CHECK:
158d79d72e0SMimi Zohar 		iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
159d79d72e0SMimi Zohar 		break;
160d906c10dSMatthew Garrett 	case CREDS_CHECK:
161d906c10dSMatthew Garrett 		iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
162d906c10dSMatthew Garrett 		break;
163d79d72e0SMimi Zohar 	case FILE_CHECK:
164c6af8efeSMimi Zohar 	case POST_SETATTR:
165d79d72e0SMimi Zohar 		iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
166c6af8efeSMimi Zohar 		break;
167c6af8efeSMimi Zohar 	case MODULE_CHECK ... MAX_CHECK - 1:
168c6af8efeSMimi Zohar 	default:
169c6af8efeSMimi Zohar 		iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
170c6af8efeSMimi Zohar 		break;
171d79d72e0SMimi Zohar 	}
172d79d72e0SMimi Zohar }
173d79d72e0SMimi Zohar 
1741525b06dSDmitry Kasatkin enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
1751525b06dSDmitry Kasatkin 				 int xattr_len)
176d3634d0fSDmitry Kasatkin {
177d3634d0fSDmitry Kasatkin 	struct signature_v2_hdr *sig;
178b4bfec7fSSeth Forshee 	enum hash_algo ret;
179d3634d0fSDmitry Kasatkin 
1803ea7a560SDmitry Kasatkin 	if (!xattr_value || xattr_len < 2)
1811525b06dSDmitry Kasatkin 		/* return default hash algo */
1821525b06dSDmitry Kasatkin 		return ima_hash_algo;
183d3634d0fSDmitry Kasatkin 
1843ea7a560SDmitry Kasatkin 	switch (xattr_value->type) {
1853ea7a560SDmitry Kasatkin 	case EVM_IMA_XATTR_DIGSIG:
1863ea7a560SDmitry Kasatkin 		sig = (typeof(sig))xattr_value;
1873ea7a560SDmitry Kasatkin 		if (sig->version != 2 || xattr_len <= sizeof(*sig))
1881525b06dSDmitry Kasatkin 			return ima_hash_algo;
1891525b06dSDmitry Kasatkin 		return sig->hash_algo;
1903ea7a560SDmitry Kasatkin 		break;
1913ea7a560SDmitry Kasatkin 	case IMA_XATTR_DIGEST_NG:
192650b29dbSThiago Jung Bauermann 		/* first byte contains algorithm id */
193650b29dbSThiago Jung Bauermann 		ret = xattr_value->data[0];
194b4bfec7fSSeth Forshee 		if (ret < HASH_ALGO__LAST)
195b4bfec7fSSeth Forshee 			return ret;
1963ea7a560SDmitry Kasatkin 		break;
1973ea7a560SDmitry Kasatkin 	case IMA_XATTR_DIGEST:
1983ea7a560SDmitry Kasatkin 		/* this is for backward compatibility */
1993ea7a560SDmitry Kasatkin 		if (xattr_len == 21) {
2003ea7a560SDmitry Kasatkin 			unsigned int zero = 0;
201650b29dbSThiago Jung Bauermann 			if (!memcmp(&xattr_value->data[16], &zero, 4))
2021525b06dSDmitry Kasatkin 				return HASH_ALGO_MD5;
2033ea7a560SDmitry Kasatkin 			else
2041525b06dSDmitry Kasatkin 				return HASH_ALGO_SHA1;
2053ea7a560SDmitry Kasatkin 		} else if (xattr_len == 17)
2061525b06dSDmitry Kasatkin 			return HASH_ALGO_MD5;
2073ea7a560SDmitry Kasatkin 		break;
2083ea7a560SDmitry Kasatkin 	}
2091525b06dSDmitry Kasatkin 
2101525b06dSDmitry Kasatkin 	/* return default hash algo */
2111525b06dSDmitry Kasatkin 	return ima_hash_algo;
212d3634d0fSDmitry Kasatkin }
213d3634d0fSDmitry Kasatkin 
214d3634d0fSDmitry Kasatkin int ima_read_xattr(struct dentry *dentry,
215d3634d0fSDmitry Kasatkin 		   struct evm_ima_xattr_data **xattr_value)
216d3634d0fSDmitry Kasatkin {
2175d6c3191SAndreas Gruenbacher 	ssize_t ret;
218d3634d0fSDmitry Kasatkin 
219c7c7a1a1STycho Andersen 	ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA,
220c7c7a1a1STycho Andersen 				 (char **)xattr_value, 0, GFP_NOFS);
2215d6c3191SAndreas Gruenbacher 	if (ret == -EOPNOTSUPP)
2225d6c3191SAndreas Gruenbacher 		ret = 0;
2235d6c3191SAndreas Gruenbacher 	return ret;
224d3634d0fSDmitry Kasatkin }
225d3634d0fSDmitry Kasatkin 
2262fe5d6deSMimi Zohar /*
227a5fbeb61SThiago Jung Bauermann  * xattr_verify - verify xattr digest or signature
228a5fbeb61SThiago Jung Bauermann  *
229a5fbeb61SThiago Jung Bauermann  * Verify whether the hash or signature matches the file contents.
230a5fbeb61SThiago Jung Bauermann  *
231a5fbeb61SThiago Jung Bauermann  * Return 0 on success, error code otherwise.
232a5fbeb61SThiago Jung Bauermann  */
233a5fbeb61SThiago Jung Bauermann static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
234a5fbeb61SThiago Jung Bauermann 			struct evm_ima_xattr_data *xattr_value, int xattr_len,
235a5fbeb61SThiago Jung Bauermann 			enum integrity_status *status, const char **cause)
236a5fbeb61SThiago Jung Bauermann {
237a5fbeb61SThiago Jung Bauermann 	int rc = -EINVAL, hash_start = 0;
238a5fbeb61SThiago Jung Bauermann 
239a5fbeb61SThiago Jung Bauermann 	switch (xattr_value->type) {
240a5fbeb61SThiago Jung Bauermann 	case IMA_XATTR_DIGEST_NG:
241a5fbeb61SThiago Jung Bauermann 		/* first byte contains algorithm id */
242a5fbeb61SThiago Jung Bauermann 		hash_start = 1;
243df561f66SGustavo A. R. Silva 		fallthrough;
244a5fbeb61SThiago Jung Bauermann 	case IMA_XATTR_DIGEST:
245a5fbeb61SThiago Jung Bauermann 		if (iint->flags & IMA_DIGSIG_REQUIRED) {
246a5fbeb61SThiago Jung Bauermann 			*cause = "IMA-signature-required";
247a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_FAIL;
248a5fbeb61SThiago Jung Bauermann 			break;
249a5fbeb61SThiago Jung Bauermann 		}
250a5fbeb61SThiago Jung Bauermann 		clear_bit(IMA_DIGSIG, &iint->atomic_flags);
251a5fbeb61SThiago Jung Bauermann 		if (xattr_len - sizeof(xattr_value->type) - hash_start >=
252a5fbeb61SThiago Jung Bauermann 				iint->ima_hash->length)
253a5fbeb61SThiago Jung Bauermann 			/*
254a5fbeb61SThiago Jung Bauermann 			 * xattr length may be longer. md5 hash in previous
255a5fbeb61SThiago Jung Bauermann 			 * version occupied 20 bytes in xattr, instead of 16
256a5fbeb61SThiago Jung Bauermann 			 */
257a5fbeb61SThiago Jung Bauermann 			rc = memcmp(&xattr_value->data[hash_start],
258a5fbeb61SThiago Jung Bauermann 				    iint->ima_hash->digest,
259a5fbeb61SThiago Jung Bauermann 				    iint->ima_hash->length);
260a5fbeb61SThiago Jung Bauermann 		else
261a5fbeb61SThiago Jung Bauermann 			rc = -EINVAL;
262a5fbeb61SThiago Jung Bauermann 		if (rc) {
263a5fbeb61SThiago Jung Bauermann 			*cause = "invalid-hash";
264a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_FAIL;
265a5fbeb61SThiago Jung Bauermann 			break;
266a5fbeb61SThiago Jung Bauermann 		}
267a5fbeb61SThiago Jung Bauermann 		*status = INTEGRITY_PASS;
268a5fbeb61SThiago Jung Bauermann 		break;
269a5fbeb61SThiago Jung Bauermann 	case EVM_IMA_XATTR_DIGSIG:
270a5fbeb61SThiago Jung Bauermann 		set_bit(IMA_DIGSIG, &iint->atomic_flags);
271a5fbeb61SThiago Jung Bauermann 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
272a5fbeb61SThiago Jung Bauermann 					     (const char *)xattr_value,
273a5fbeb61SThiago Jung Bauermann 					     xattr_len,
274a5fbeb61SThiago Jung Bauermann 					     iint->ima_hash->digest,
275a5fbeb61SThiago Jung Bauermann 					     iint->ima_hash->length);
276a5fbeb61SThiago Jung Bauermann 		if (rc == -EOPNOTSUPP) {
277a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_UNKNOWN;
278a5fbeb61SThiago Jung Bauermann 			break;
279a5fbeb61SThiago Jung Bauermann 		}
280a5fbeb61SThiago Jung Bauermann 		if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
281a5fbeb61SThiago Jung Bauermann 		    func == KEXEC_KERNEL_CHECK)
282a5fbeb61SThiago Jung Bauermann 			rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
283a5fbeb61SThiago Jung Bauermann 						     (const char *)xattr_value,
284a5fbeb61SThiago Jung Bauermann 						     xattr_len,
285a5fbeb61SThiago Jung Bauermann 						     iint->ima_hash->digest,
286a5fbeb61SThiago Jung Bauermann 						     iint->ima_hash->length);
287a5fbeb61SThiago Jung Bauermann 		if (rc) {
288a5fbeb61SThiago Jung Bauermann 			*cause = "invalid-signature";
289a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_FAIL;
290a5fbeb61SThiago Jung Bauermann 		} else {
291a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_PASS;
292a5fbeb61SThiago Jung Bauermann 		}
293a5fbeb61SThiago Jung Bauermann 		break;
294a5fbeb61SThiago Jung Bauermann 	default:
295a5fbeb61SThiago Jung Bauermann 		*status = INTEGRITY_UNKNOWN;
296a5fbeb61SThiago Jung Bauermann 		*cause = "unknown-ima-data";
297a5fbeb61SThiago Jung Bauermann 		break;
298a5fbeb61SThiago Jung Bauermann 	}
299a5fbeb61SThiago Jung Bauermann 
300a5fbeb61SThiago Jung Bauermann 	return rc;
301a5fbeb61SThiago Jung Bauermann }
302a5fbeb61SThiago Jung Bauermann 
303a5fbeb61SThiago Jung Bauermann /*
30439b07096SThiago Jung Bauermann  * modsig_verify - verify modsig signature
30539b07096SThiago Jung Bauermann  *
30639b07096SThiago Jung Bauermann  * Verify whether the signature matches the file contents.
30739b07096SThiago Jung Bauermann  *
30839b07096SThiago Jung Bauermann  * Return 0 on success, error code otherwise.
30939b07096SThiago Jung Bauermann  */
31039b07096SThiago Jung Bauermann static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
31139b07096SThiago Jung Bauermann 			 enum integrity_status *status, const char **cause)
31239b07096SThiago Jung Bauermann {
31339b07096SThiago Jung Bauermann 	int rc;
31439b07096SThiago Jung Bauermann 
31539b07096SThiago Jung Bauermann 	rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
31639b07096SThiago Jung Bauermann 	if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
31739b07096SThiago Jung Bauermann 	    func == KEXEC_KERNEL_CHECK)
31839b07096SThiago Jung Bauermann 		rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
31939b07096SThiago Jung Bauermann 					     modsig);
32039b07096SThiago Jung Bauermann 	if (rc) {
32139b07096SThiago Jung Bauermann 		*cause = "invalid-signature";
32239b07096SThiago Jung Bauermann 		*status = INTEGRITY_FAIL;
32339b07096SThiago Jung Bauermann 	} else {
32439b07096SThiago Jung Bauermann 		*status = INTEGRITY_PASS;
32539b07096SThiago Jung Bauermann 	}
32639b07096SThiago Jung Bauermann 
32739b07096SThiago Jung Bauermann 	return rc;
32839b07096SThiago Jung Bauermann }
32939b07096SThiago Jung Bauermann 
33039b07096SThiago Jung Bauermann /*
331273df864SNayna Jain  * ima_check_blacklist - determine if the binary is blacklisted.
332273df864SNayna Jain  *
333273df864SNayna Jain  * Add the hash of the blacklisted binary to the measurement list, based
334273df864SNayna Jain  * on policy.
335273df864SNayna Jain  *
336273df864SNayna Jain  * Returns -EPERM if the hash is blacklisted.
337273df864SNayna Jain  */
338273df864SNayna Jain int ima_check_blacklist(struct integrity_iint_cache *iint,
339273df864SNayna Jain 			const struct modsig *modsig, int pcr)
340273df864SNayna Jain {
341273df864SNayna Jain 	enum hash_algo hash_algo;
342273df864SNayna Jain 	const u8 *digest = NULL;
343273df864SNayna Jain 	u32 digestsize = 0;
344273df864SNayna Jain 	int rc = 0;
345273df864SNayna Jain 
346273df864SNayna Jain 	if (!(iint->flags & IMA_CHECK_BLACKLIST))
347273df864SNayna Jain 		return 0;
348273df864SNayna Jain 
349273df864SNayna Jain 	if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
350273df864SNayna Jain 		ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
351273df864SNayna Jain 
352273df864SNayna Jain 		rc = is_binary_blacklisted(digest, digestsize);
353273df864SNayna Jain 		if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
354*a2d2329eSChristian Brauner 			process_buffer_measurement(&init_user_ns, NULL, digest, digestsize,
355273df864SNayna Jain 						   "blacklisted-hash", NONE,
356e9085e0aSLakshmi Ramasubramanian 						   pcr, NULL);
357273df864SNayna Jain 	}
358273df864SNayna Jain 
359273df864SNayna Jain 	return rc;
360273df864SNayna Jain }
361273df864SNayna Jain 
362273df864SNayna Jain /*
3632fe5d6deSMimi Zohar  * ima_appraise_measurement - appraise file measurement
3642fe5d6deSMimi Zohar  *
3652fe5d6deSMimi Zohar  * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
3662fe5d6deSMimi Zohar  * Assuming success, compare the xattr hash with the collected measurement.
3672fe5d6deSMimi Zohar  *
3682fe5d6deSMimi Zohar  * Return 0 on success, error code otherwise
3692fe5d6deSMimi Zohar  */
3704ad87a3dSMimi Zohar int ima_appraise_measurement(enum ima_hooks func,
3714ad87a3dSMimi Zohar 			     struct integrity_iint_cache *iint,
372d3634d0fSDmitry Kasatkin 			     struct file *file, const unsigned char *filename,
373d3634d0fSDmitry Kasatkin 			     struct evm_ima_xattr_data *xattr_value,
37439b07096SThiago Jung Bauermann 			     int xattr_len, const struct modsig *modsig)
3752fe5d6deSMimi Zohar {
37652a13284SMimi Zohar 	static const char op[] = "appraise_data";
377f5e51fa3SThiago Jung Bauermann 	const char *cause = "unknown";
378e71b9dffSMiklos Szeredi 	struct dentry *dentry = file_dentry(file);
379c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
3802fe5d6deSMimi Zohar 	enum integrity_status status = INTEGRITY_UNKNOWN;
381a5fbeb61SThiago Jung Bauermann 	int rc = xattr_len;
38239b07096SThiago Jung Bauermann 	bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
3832fe5d6deSMimi Zohar 
38439b07096SThiago Jung Bauermann 	/* If not appraising a modsig, we need an xattr. */
38539b07096SThiago Jung Bauermann 	if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
3862fe5d6deSMimi Zohar 		return INTEGRITY_UNKNOWN;
3872fe5d6deSMimi Zohar 
38839b07096SThiago Jung Bauermann 	/* If reading the xattr failed and there's no modsig, error out. */
38939b07096SThiago Jung Bauermann 	if (rc <= 0 && !try_modsig) {
3902fe5d6deSMimi Zohar 		if (rc && rc != -ENODATA)
3912fe5d6deSMimi Zohar 			goto out;
3922fe5d6deSMimi Zohar 
393915d9d25SThiago Jung Bauermann 		cause = iint->flags & IMA_DIGSIG_REQUIRED ?
394915d9d25SThiago Jung Bauermann 				"IMA-signature-required" : "missing-hash";
395b151d6b0SDmitry Kasatkin 		status = INTEGRITY_NOLABEL;
3966035a27bSAl Viro 		if (file->f_mode & FMODE_CREATED)
397b151d6b0SDmitry Kasatkin 			iint->flags |= IMA_NEW_FILE;
3981ac202e9SDaniel Glöckner 		if ((iint->flags & IMA_NEW_FILE) &&
399b7e27bc1SMimi Zohar 		    (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
400b7e27bc1SMimi Zohar 		     (inode->i_size == 0)))
401b151d6b0SDmitry Kasatkin 			status = INTEGRITY_PASS;
4022fe5d6deSMimi Zohar 		goto out;
4032fe5d6deSMimi Zohar 	}
4042fe5d6deSMimi Zohar 
4058606404fSDmitry Kasatkin 	status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
406f5e51fa3SThiago Jung Bauermann 	switch (status) {
407f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_PASS:
408f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_PASS_IMMUTABLE:
409f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_UNKNOWN:
410f5e51fa3SThiago Jung Bauermann 		break;
411f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_NOXATTRS:	/* No EVM protected xattrs. */
41239b07096SThiago Jung Bauermann 		/* It's fine not to have xattrs when using a modsig. */
41339b07096SThiago Jung Bauermann 		if (try_modsig)
41439b07096SThiago Jung Bauermann 			break;
415df561f66SGustavo A. R. Silva 		fallthrough;
416f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_NOLABEL:		/* No security.evm xattr. */
4172fe5d6deSMimi Zohar 		cause = "missing-HMAC";
418f5e51fa3SThiago Jung Bauermann 		goto out;
419f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_FAIL:		/* Invalid HMAC/signature. */
4202fe5d6deSMimi Zohar 		cause = "invalid-HMAC";
4212fe5d6deSMimi Zohar 		goto out;
422f5e51fa3SThiago Jung Bauermann 	default:
423f5e51fa3SThiago Jung Bauermann 		WARN_ONCE(true, "Unexpected integrity status %d\n", status);
4242fe5d6deSMimi Zohar 	}
425f5e51fa3SThiago Jung Bauermann 
426a5fbeb61SThiago Jung Bauermann 	if (xattr_value)
427a5fbeb61SThiago Jung Bauermann 		rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
428a5fbeb61SThiago Jung Bauermann 				  &cause);
4298606404fSDmitry Kasatkin 
43039b07096SThiago Jung Bauermann 	/*
43139b07096SThiago Jung Bauermann 	 * If we have a modsig and either no imasig or the imasig's key isn't
43239b07096SThiago Jung Bauermann 	 * known, then try verifying the modsig.
43339b07096SThiago Jung Bauermann 	 */
43439b07096SThiago Jung Bauermann 	if (try_modsig &&
43539b07096SThiago Jung Bauermann 	    (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
43639b07096SThiago Jung Bauermann 	     rc == -ENOKEY))
43739b07096SThiago Jung Bauermann 		rc = modsig_verify(func, modsig, &status, &cause);
43839b07096SThiago Jung Bauermann 
4392fe5d6deSMimi Zohar out:
44057b56ac6SMimi Zohar 	/*
44157b56ac6SMimi Zohar 	 * File signatures on some filesystems can not be properly verified.
4429e67028eSMimi Zohar 	 * When such filesystems are mounted by an untrusted mounter or on a
4439e67028eSMimi Zohar 	 * system not willing to accept such a risk, fail the file signature
4449e67028eSMimi Zohar 	 * verification.
44557b56ac6SMimi Zohar 	 */
4469e67028eSMimi Zohar 	if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
4479e67028eSMimi Zohar 	    ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
4489e67028eSMimi Zohar 	     (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
44957b56ac6SMimi Zohar 		status = INTEGRITY_FAIL;
45057b56ac6SMimi Zohar 		cause = "unverifiable-signature";
45157b56ac6SMimi Zohar 		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
45257b56ac6SMimi Zohar 				    op, cause, rc, 0);
45357b56ac6SMimi Zohar 	} else if (status != INTEGRITY_PASS) {
454f5e51fa3SThiago Jung Bauermann 		/* Fix mode, but don't replace file signatures. */
45539b07096SThiago Jung Bauermann 		if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
4568606404fSDmitry Kasatkin 		    (!xattr_value ||
4578606404fSDmitry Kasatkin 		     xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
458def3e8b9SDmitry Kasatkin 			if (!ima_fix_xattr(dentry, iint))
4592fe5d6deSMimi Zohar 				status = INTEGRITY_PASS;
460f5e51fa3SThiago Jung Bauermann 		}
461f5e51fa3SThiago Jung Bauermann 
462f5e51fa3SThiago Jung Bauermann 		/* Permit new files with file signatures, but without data. */
463f5e51fa3SThiago Jung Bauermann 		if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
464f5e51fa3SThiago Jung Bauermann 		    xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
46505d1a717SMimi Zohar 			status = INTEGRITY_PASS;
4662fe5d6deSMimi Zohar 		}
467f5e51fa3SThiago Jung Bauermann 
4682fe5d6deSMimi Zohar 		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
4692fe5d6deSMimi Zohar 				    op, cause, rc, 0);
4708606404fSDmitry Kasatkin 	} else {
471d79d72e0SMimi Zohar 		ima_cache_flags(iint, func);
4722fe5d6deSMimi Zohar 	}
47357b56ac6SMimi Zohar 
474d79d72e0SMimi Zohar 	ima_set_cache_status(iint, func, status);
4752fe5d6deSMimi Zohar 	return status;
4762fe5d6deSMimi Zohar }
4772fe5d6deSMimi Zohar 
4782fe5d6deSMimi Zohar /*
4792fe5d6deSMimi Zohar  * ima_update_xattr - update 'security.ima' hash value
4802fe5d6deSMimi Zohar  */
4812fe5d6deSMimi Zohar void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
4822fe5d6deSMimi Zohar {
483e71b9dffSMiklos Szeredi 	struct dentry *dentry = file_dentry(file);
4842fe5d6deSMimi Zohar 	int rc = 0;
4852fe5d6deSMimi Zohar 
4868606404fSDmitry Kasatkin 	/* do not collect and update hash for digital signatures */
4870d73a552SDmitry Kasatkin 	if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
4888606404fSDmitry Kasatkin 		return;
4898606404fSDmitry Kasatkin 
490da1b0029SMimi Zohar 	if ((iint->ima_file_status != INTEGRITY_PASS) &&
491da1b0029SMimi Zohar 	    !(iint->flags & IMA_HASH))
492020aae3eSRoberto Sassu 		return;
493020aae3eSRoberto Sassu 
49415588227SThiago Jung Bauermann 	rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
4952fe5d6deSMimi Zohar 	if (rc < 0)
4962fe5d6deSMimi Zohar 		return;
4978606404fSDmitry Kasatkin 
4980d73a552SDmitry Kasatkin 	inode_lock(file_inode(file));
4992fe5d6deSMimi Zohar 	ima_fix_xattr(dentry, iint);
5000d73a552SDmitry Kasatkin 	inode_unlock(file_inode(file));
5012fe5d6deSMimi Zohar }
5022fe5d6deSMimi Zohar 
5032fe5d6deSMimi Zohar /**
5042fe5d6deSMimi Zohar  * ima_inode_post_setattr - reflect file metadata changes
505*a2d2329eSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
5062fe5d6deSMimi Zohar  * @dentry: pointer to the affected dentry
5072fe5d6deSMimi Zohar  *
5082fe5d6deSMimi Zohar  * Changes to a dentry's metadata might result in needing to appraise.
5092fe5d6deSMimi Zohar  *
5102fe5d6deSMimi Zohar  * This function is called from notify_change(), which expects the caller
5112fe5d6deSMimi Zohar  * to lock the inode's i_mutex.
5122fe5d6deSMimi Zohar  */
513*a2d2329eSChristian Brauner void ima_inode_post_setattr(struct user_namespace *mnt_userns,
514*a2d2329eSChristian Brauner 			    struct dentry *dentry)
5152fe5d6deSMimi Zohar {
516c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
5172fe5d6deSMimi Zohar 	struct integrity_iint_cache *iint;
518da1b0029SMimi Zohar 	int action;
5192fe5d6deSMimi Zohar 
520a756024eSRoberto Sassu 	if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
5215d6c3191SAndreas Gruenbacher 	    || !(inode->i_opflags & IOP_XATTR))
5222fe5d6deSMimi Zohar 		return;
5232fe5d6deSMimi Zohar 
524*a2d2329eSChristian Brauner 	action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR);
525da1b0029SMimi Zohar 	if (!action)
526c7c7a1a1STycho Andersen 		__vfs_removexattr(&init_user_ns, dentry, XATTR_NAME_IMA);
5270d73a552SDmitry Kasatkin 	iint = integrity_iint_find(inode);
5280d73a552SDmitry Kasatkin 	if (iint) {
5290d73a552SDmitry Kasatkin 		set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
530da1b0029SMimi Zohar 		if (!action)
5310d73a552SDmitry Kasatkin 			clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
5320d73a552SDmitry Kasatkin 	}
5332fe5d6deSMimi Zohar }
53442c63330SMimi Zohar 
53542c63330SMimi Zohar /*
53642c63330SMimi Zohar  * ima_protect_xattr - protect 'security.ima'
53742c63330SMimi Zohar  *
53842c63330SMimi Zohar  * Ensure that not just anyone can modify or remove 'security.ima'.
53942c63330SMimi Zohar  */
54042c63330SMimi Zohar static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
54142c63330SMimi Zohar 			     const void *xattr_value, size_t xattr_value_len)
54242c63330SMimi Zohar {
54342c63330SMimi Zohar 	if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
54442c63330SMimi Zohar 		if (!capable(CAP_SYS_ADMIN))
54542c63330SMimi Zohar 			return -EPERM;
54642c63330SMimi Zohar 		return 1;
54742c63330SMimi Zohar 	}
54842c63330SMimi Zohar 	return 0;
54942c63330SMimi Zohar }
55042c63330SMimi Zohar 
551060bdebfSMimi Zohar static void ima_reset_appraise_flags(struct inode *inode, int digsig)
55242c63330SMimi Zohar {
55342c63330SMimi Zohar 	struct integrity_iint_cache *iint;
55442c63330SMimi Zohar 
555a756024eSRoberto Sassu 	if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
55642c63330SMimi Zohar 		return;
55742c63330SMimi Zohar 
55842c63330SMimi Zohar 	iint = integrity_iint_find(inode);
55942c63330SMimi Zohar 	if (!iint)
56042c63330SMimi Zohar 		return;
561a422638dSEric Richter 	iint->measured_pcrs = 0;
5620d73a552SDmitry Kasatkin 	set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
563060bdebfSMimi Zohar 	if (digsig)
5640d73a552SDmitry Kasatkin 		set_bit(IMA_DIGSIG, &iint->atomic_flags);
5650d73a552SDmitry Kasatkin 	else
5660d73a552SDmitry Kasatkin 		clear_bit(IMA_DIGSIG, &iint->atomic_flags);
56742c63330SMimi Zohar }
56842c63330SMimi Zohar 
56942c63330SMimi Zohar int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
57042c63330SMimi Zohar 		       const void *xattr_value, size_t xattr_value_len)
57142c63330SMimi Zohar {
572060bdebfSMimi Zohar 	const struct evm_ima_xattr_data *xvalue = xattr_value;
57342c63330SMimi Zohar 	int result;
57442c63330SMimi Zohar 
57542c63330SMimi Zohar 	result = ima_protect_xattr(dentry, xattr_name, xattr_value,
57642c63330SMimi Zohar 				   xattr_value_len);
57742c63330SMimi Zohar 	if (result == 1) {
578a48fda9dSDmitry Kasatkin 		if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
579a48fda9dSDmitry Kasatkin 			return -EINVAL;
580f5acb3dcSMimi Zohar 		ima_reset_appraise_flags(d_backing_inode(dentry),
581e5729f86SThiago Jung Bauermann 			xvalue->type == EVM_IMA_XATTR_DIGSIG);
58242c63330SMimi Zohar 		result = 0;
58342c63330SMimi Zohar 	}
58442c63330SMimi Zohar 	return result;
58542c63330SMimi Zohar }
58642c63330SMimi Zohar 
58742c63330SMimi Zohar int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
58842c63330SMimi Zohar {
58942c63330SMimi Zohar 	int result;
59042c63330SMimi Zohar 
59142c63330SMimi Zohar 	result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
59242c63330SMimi Zohar 	if (result == 1) {
593c6f493d6SDavid Howells 		ima_reset_appraise_flags(d_backing_inode(dentry), 0);
59442c63330SMimi Zohar 		result = 0;
59542c63330SMimi Zohar 	}
59642c63330SMimi Zohar 	return result;
59742c63330SMimi Zohar }
598