xref: /openbmc/linux/security/integrity/ima/ima_appraise.c (revision 4afb28ab03d5d811b369af4472f3941557928569)
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  */
8876979c9SPaul Gortmaker #include <linux/init.h>
92fe5d6deSMimi Zohar #include <linux/file.h>
102fe5d6deSMimi Zohar #include <linux/fs.h>
112fe5d6deSMimi Zohar #include <linux/xattr.h>
122fe5d6deSMimi Zohar #include <linux/magic.h>
132fe5d6deSMimi Zohar #include <linux/ima.h>
142fe5d6deSMimi Zohar #include <linux/evm.h>
15273df864SNayna Jain #include <keys/system_keyring.h>
162fe5d6deSMimi Zohar 
172fe5d6deSMimi Zohar #include "ima.h"
182fe5d6deSMimi Zohar 
192fe5d6deSMimi Zohar static int __init default_appraise_setup(char *str)
202fe5d6deSMimi Zohar {
21e1f5e01fSMimi Zohar #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
22311aa6aaSBruno Meneguele 	if (arch_ima_get_secureboot()) {
23311aa6aaSBruno Meneguele 		pr_info("Secure boot enabled: ignoring ima_appraise=%s boot parameter option",
24311aa6aaSBruno Meneguele 			str);
25311aa6aaSBruno Meneguele 		return 1;
26311aa6aaSBruno Meneguele 	}
27311aa6aaSBruno Meneguele 
282fe5d6deSMimi Zohar 	if (strncmp(str, "off", 3) == 0)
292fe5d6deSMimi Zohar 		ima_appraise = 0;
302faa6ef3SDmitry Kasatkin 	else if (strncmp(str, "log", 3) == 0)
312faa6ef3SDmitry Kasatkin 		ima_appraise = IMA_APPRAISE_LOG;
322fe5d6deSMimi Zohar 	else if (strncmp(str, "fix", 3) == 0)
332fe5d6deSMimi Zohar 		ima_appraise = IMA_APPRAISE_FIX;
34*4afb28abSBruno Meneguele 	else if (strncmp(str, "enforce", 7) == 0)
35*4afb28abSBruno Meneguele 		ima_appraise = IMA_APPRAISE_ENFORCE;
36e1f5e01fSMimi Zohar #endif
372fe5d6deSMimi Zohar 	return 1;
382fe5d6deSMimi Zohar }
392fe5d6deSMimi Zohar 
402fe5d6deSMimi Zohar __setup("ima_appraise=", default_appraise_setup);
412fe5d6deSMimi Zohar 
422fe5d6deSMimi Zohar /*
436f6723e2SMimi Zohar  * is_ima_appraise_enabled - return appraise status
446f6723e2SMimi Zohar  *
456f6723e2SMimi Zohar  * Only return enabled, if not in ima_appraise="fix" or "log" modes.
466f6723e2SMimi Zohar  */
476f6723e2SMimi Zohar bool is_ima_appraise_enabled(void)
486f6723e2SMimi Zohar {
49e5729f86SThiago Jung Bauermann 	return ima_appraise & IMA_APPRAISE_ENFORCE;
506f6723e2SMimi Zohar }
516f6723e2SMimi Zohar 
526f6723e2SMimi Zohar /*
532fe5d6deSMimi Zohar  * ima_must_appraise - set appraise flag
542fe5d6deSMimi Zohar  *
55da1b0029SMimi Zohar  * Return 1 to appraise or hash
562fe5d6deSMimi Zohar  */
57d26e1936SDmitry Kasatkin int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
582fe5d6deSMimi Zohar {
59d906c10dSMatthew Garrett 	u32 secid;
60d906c10dSMatthew Garrett 
6107f6a794SMimi Zohar 	if (!ima_appraise)
622fe5d6deSMimi Zohar 		return 0;
6307f6a794SMimi Zohar 
64d906c10dSMatthew Garrett 	security_task_getsecid(current, &secid);
65d906c10dSMatthew Garrett 	return ima_match_policy(inode, current_cred(), secid, func, mask,
66e9085e0aSLakshmi Ramasubramanian 				IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL);
672fe5d6deSMimi Zohar }
682fe5d6deSMimi Zohar 
69def3e8b9SDmitry Kasatkin static int ima_fix_xattr(struct dentry *dentry,
702fe5d6deSMimi Zohar 			 struct integrity_iint_cache *iint)
712fe5d6deSMimi Zohar {
723ea7a560SDmitry Kasatkin 	int rc, offset;
733ea7a560SDmitry Kasatkin 	u8 algo = iint->ima_hash->algo;
743ea7a560SDmitry Kasatkin 
753ea7a560SDmitry Kasatkin 	if (algo <= HASH_ALGO_SHA1) {
763ea7a560SDmitry Kasatkin 		offset = 1;
773ea7a560SDmitry Kasatkin 		iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
783ea7a560SDmitry Kasatkin 	} else {
793ea7a560SDmitry Kasatkin 		offset = 0;
803ea7a560SDmitry Kasatkin 		iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
813ea7a560SDmitry Kasatkin 		iint->ima_hash->xattr.ng.algo = algo;
823ea7a560SDmitry Kasatkin 	}
833ea7a560SDmitry Kasatkin 	rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
843ea7a560SDmitry Kasatkin 				   &iint->ima_hash->xattr.data[offset],
853ea7a560SDmitry Kasatkin 				   (sizeof(iint->ima_hash->xattr) - offset) +
863ea7a560SDmitry Kasatkin 				   iint->ima_hash->length, 0);
873ea7a560SDmitry Kasatkin 	return rc;
882fe5d6deSMimi Zohar }
892fe5d6deSMimi Zohar 
90d79d72e0SMimi Zohar /* Return specific func appraised cached result */
91d79d72e0SMimi Zohar enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
924ad87a3dSMimi Zohar 					   enum ima_hooks func)
93d79d72e0SMimi Zohar {
94d79d72e0SMimi Zohar 	switch (func) {
95d79d72e0SMimi Zohar 	case MMAP_CHECK:
96d79d72e0SMimi Zohar 		return iint->ima_mmap_status;
97d79d72e0SMimi Zohar 	case BPRM_CHECK:
98d79d72e0SMimi Zohar 		return iint->ima_bprm_status;
99d906c10dSMatthew Garrett 	case CREDS_CHECK:
100d906c10dSMatthew Garrett 		return iint->ima_creds_status;
101d79d72e0SMimi Zohar 	case FILE_CHECK:
102c6af8efeSMimi Zohar 	case POST_SETATTR:
103d79d72e0SMimi Zohar 		return iint->ima_file_status;
104c6af8efeSMimi Zohar 	case MODULE_CHECK ... MAX_CHECK - 1:
105c6af8efeSMimi Zohar 	default:
106c6af8efeSMimi Zohar 		return iint->ima_read_status;
107d79d72e0SMimi Zohar 	}
108d79d72e0SMimi Zohar }
109d79d72e0SMimi Zohar 
110d79d72e0SMimi Zohar static void ima_set_cache_status(struct integrity_iint_cache *iint,
1114ad87a3dSMimi Zohar 				 enum ima_hooks func,
1124ad87a3dSMimi Zohar 				 enum integrity_status status)
113d79d72e0SMimi Zohar {
114d79d72e0SMimi Zohar 	switch (func) {
115d79d72e0SMimi Zohar 	case MMAP_CHECK:
116d79d72e0SMimi Zohar 		iint->ima_mmap_status = status;
117d79d72e0SMimi Zohar 		break;
118d79d72e0SMimi Zohar 	case BPRM_CHECK:
119d79d72e0SMimi Zohar 		iint->ima_bprm_status = status;
120d79d72e0SMimi Zohar 		break;
121d906c10dSMatthew Garrett 	case CREDS_CHECK:
122d906c10dSMatthew Garrett 		iint->ima_creds_status = status;
12309186e50SGustavo A. R. Silva 		break;
124d79d72e0SMimi Zohar 	case FILE_CHECK:
125c6af8efeSMimi Zohar 	case POST_SETATTR:
126d79d72e0SMimi Zohar 		iint->ima_file_status = status;
127c6af8efeSMimi Zohar 		break;
128c6af8efeSMimi Zohar 	case MODULE_CHECK ... MAX_CHECK - 1:
129c6af8efeSMimi Zohar 	default:
130c6af8efeSMimi Zohar 		iint->ima_read_status = status;
131c6af8efeSMimi Zohar 		break;
132d79d72e0SMimi Zohar 	}
133d79d72e0SMimi Zohar }
134d79d72e0SMimi Zohar 
1354ad87a3dSMimi Zohar static void ima_cache_flags(struct integrity_iint_cache *iint,
1364ad87a3dSMimi Zohar 			     enum ima_hooks func)
137d79d72e0SMimi Zohar {
138d79d72e0SMimi Zohar 	switch (func) {
139d79d72e0SMimi Zohar 	case MMAP_CHECK:
140d79d72e0SMimi Zohar 		iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
141d79d72e0SMimi Zohar 		break;
142d79d72e0SMimi Zohar 	case BPRM_CHECK:
143d79d72e0SMimi Zohar 		iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
144d79d72e0SMimi Zohar 		break;
145d906c10dSMatthew Garrett 	case CREDS_CHECK:
146d906c10dSMatthew Garrett 		iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
147d906c10dSMatthew Garrett 		break;
148d79d72e0SMimi Zohar 	case FILE_CHECK:
149c6af8efeSMimi Zohar 	case POST_SETATTR:
150d79d72e0SMimi Zohar 		iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
151c6af8efeSMimi Zohar 		break;
152c6af8efeSMimi Zohar 	case MODULE_CHECK ... MAX_CHECK - 1:
153c6af8efeSMimi Zohar 	default:
154c6af8efeSMimi Zohar 		iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
155c6af8efeSMimi Zohar 		break;
156d79d72e0SMimi Zohar 	}
157d79d72e0SMimi Zohar }
158d79d72e0SMimi Zohar 
1591525b06dSDmitry Kasatkin enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
1601525b06dSDmitry Kasatkin 				 int xattr_len)
161d3634d0fSDmitry Kasatkin {
162d3634d0fSDmitry Kasatkin 	struct signature_v2_hdr *sig;
163b4bfec7fSSeth Forshee 	enum hash_algo ret;
164d3634d0fSDmitry Kasatkin 
1653ea7a560SDmitry Kasatkin 	if (!xattr_value || xattr_len < 2)
1661525b06dSDmitry Kasatkin 		/* return default hash algo */
1671525b06dSDmitry Kasatkin 		return ima_hash_algo;
168d3634d0fSDmitry Kasatkin 
1693ea7a560SDmitry Kasatkin 	switch (xattr_value->type) {
1703ea7a560SDmitry Kasatkin 	case EVM_IMA_XATTR_DIGSIG:
1713ea7a560SDmitry Kasatkin 		sig = (typeof(sig))xattr_value;
1723ea7a560SDmitry Kasatkin 		if (sig->version != 2 || xattr_len <= sizeof(*sig))
1731525b06dSDmitry Kasatkin 			return ima_hash_algo;
1741525b06dSDmitry Kasatkin 		return sig->hash_algo;
1753ea7a560SDmitry Kasatkin 		break;
1763ea7a560SDmitry Kasatkin 	case IMA_XATTR_DIGEST_NG:
177650b29dbSThiago Jung Bauermann 		/* first byte contains algorithm id */
178650b29dbSThiago Jung Bauermann 		ret = xattr_value->data[0];
179b4bfec7fSSeth Forshee 		if (ret < HASH_ALGO__LAST)
180b4bfec7fSSeth Forshee 			return ret;
1813ea7a560SDmitry Kasatkin 		break;
1823ea7a560SDmitry Kasatkin 	case IMA_XATTR_DIGEST:
1833ea7a560SDmitry Kasatkin 		/* this is for backward compatibility */
1843ea7a560SDmitry Kasatkin 		if (xattr_len == 21) {
1853ea7a560SDmitry Kasatkin 			unsigned int zero = 0;
186650b29dbSThiago Jung Bauermann 			if (!memcmp(&xattr_value->data[16], &zero, 4))
1871525b06dSDmitry Kasatkin 				return HASH_ALGO_MD5;
1883ea7a560SDmitry Kasatkin 			else
1891525b06dSDmitry Kasatkin 				return HASH_ALGO_SHA1;
1903ea7a560SDmitry Kasatkin 		} else if (xattr_len == 17)
1911525b06dSDmitry Kasatkin 			return HASH_ALGO_MD5;
1923ea7a560SDmitry Kasatkin 		break;
1933ea7a560SDmitry Kasatkin 	}
1941525b06dSDmitry Kasatkin 
1951525b06dSDmitry Kasatkin 	/* return default hash algo */
1961525b06dSDmitry Kasatkin 	return ima_hash_algo;
197d3634d0fSDmitry Kasatkin }
198d3634d0fSDmitry Kasatkin 
199d3634d0fSDmitry Kasatkin int ima_read_xattr(struct dentry *dentry,
200d3634d0fSDmitry Kasatkin 		   struct evm_ima_xattr_data **xattr_value)
201d3634d0fSDmitry Kasatkin {
2025d6c3191SAndreas Gruenbacher 	ssize_t ret;
203d3634d0fSDmitry Kasatkin 
2045d6c3191SAndreas Gruenbacher 	ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
205d3634d0fSDmitry Kasatkin 				 0, GFP_NOFS);
2065d6c3191SAndreas Gruenbacher 	if (ret == -EOPNOTSUPP)
2075d6c3191SAndreas Gruenbacher 		ret = 0;
2085d6c3191SAndreas Gruenbacher 	return ret;
209d3634d0fSDmitry Kasatkin }
210d3634d0fSDmitry Kasatkin 
2112fe5d6deSMimi Zohar /*
212a5fbeb61SThiago Jung Bauermann  * xattr_verify - verify xattr digest or signature
213a5fbeb61SThiago Jung Bauermann  *
214a5fbeb61SThiago Jung Bauermann  * Verify whether the hash or signature matches the file contents.
215a5fbeb61SThiago Jung Bauermann  *
216a5fbeb61SThiago Jung Bauermann  * Return 0 on success, error code otherwise.
217a5fbeb61SThiago Jung Bauermann  */
218a5fbeb61SThiago Jung Bauermann static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
219a5fbeb61SThiago Jung Bauermann 			struct evm_ima_xattr_data *xattr_value, int xattr_len,
220a5fbeb61SThiago Jung Bauermann 			enum integrity_status *status, const char **cause)
221a5fbeb61SThiago Jung Bauermann {
222a5fbeb61SThiago Jung Bauermann 	int rc = -EINVAL, hash_start = 0;
223a5fbeb61SThiago Jung Bauermann 
224a5fbeb61SThiago Jung Bauermann 	switch (xattr_value->type) {
225a5fbeb61SThiago Jung Bauermann 	case IMA_XATTR_DIGEST_NG:
226a5fbeb61SThiago Jung Bauermann 		/* first byte contains algorithm id */
227a5fbeb61SThiago Jung Bauermann 		hash_start = 1;
228a5fbeb61SThiago Jung Bauermann 		/* fall through */
229a5fbeb61SThiago Jung Bauermann 	case IMA_XATTR_DIGEST:
230a5fbeb61SThiago Jung Bauermann 		if (iint->flags & IMA_DIGSIG_REQUIRED) {
231a5fbeb61SThiago Jung Bauermann 			*cause = "IMA-signature-required";
232a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_FAIL;
233a5fbeb61SThiago Jung Bauermann 			break;
234a5fbeb61SThiago Jung Bauermann 		}
235a5fbeb61SThiago Jung Bauermann 		clear_bit(IMA_DIGSIG, &iint->atomic_flags);
236a5fbeb61SThiago Jung Bauermann 		if (xattr_len - sizeof(xattr_value->type) - hash_start >=
237a5fbeb61SThiago Jung Bauermann 				iint->ima_hash->length)
238a5fbeb61SThiago Jung Bauermann 			/*
239a5fbeb61SThiago Jung Bauermann 			 * xattr length may be longer. md5 hash in previous
240a5fbeb61SThiago Jung Bauermann 			 * version occupied 20 bytes in xattr, instead of 16
241a5fbeb61SThiago Jung Bauermann 			 */
242a5fbeb61SThiago Jung Bauermann 			rc = memcmp(&xattr_value->data[hash_start],
243a5fbeb61SThiago Jung Bauermann 				    iint->ima_hash->digest,
244a5fbeb61SThiago Jung Bauermann 				    iint->ima_hash->length);
245a5fbeb61SThiago Jung Bauermann 		else
246a5fbeb61SThiago Jung Bauermann 			rc = -EINVAL;
247a5fbeb61SThiago Jung Bauermann 		if (rc) {
248a5fbeb61SThiago Jung Bauermann 			*cause = "invalid-hash";
249a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_FAIL;
250a5fbeb61SThiago Jung Bauermann 			break;
251a5fbeb61SThiago Jung Bauermann 		}
252a5fbeb61SThiago Jung Bauermann 		*status = INTEGRITY_PASS;
253a5fbeb61SThiago Jung Bauermann 		break;
254a5fbeb61SThiago Jung Bauermann 	case EVM_IMA_XATTR_DIGSIG:
255a5fbeb61SThiago Jung Bauermann 		set_bit(IMA_DIGSIG, &iint->atomic_flags);
256a5fbeb61SThiago Jung Bauermann 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
257a5fbeb61SThiago Jung Bauermann 					     (const char *)xattr_value,
258a5fbeb61SThiago Jung Bauermann 					     xattr_len,
259a5fbeb61SThiago Jung Bauermann 					     iint->ima_hash->digest,
260a5fbeb61SThiago Jung Bauermann 					     iint->ima_hash->length);
261a5fbeb61SThiago Jung Bauermann 		if (rc == -EOPNOTSUPP) {
262a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_UNKNOWN;
263a5fbeb61SThiago Jung Bauermann 			break;
264a5fbeb61SThiago Jung Bauermann 		}
265a5fbeb61SThiago Jung Bauermann 		if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
266a5fbeb61SThiago Jung Bauermann 		    func == KEXEC_KERNEL_CHECK)
267a5fbeb61SThiago Jung Bauermann 			rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
268a5fbeb61SThiago Jung Bauermann 						     (const char *)xattr_value,
269a5fbeb61SThiago Jung Bauermann 						     xattr_len,
270a5fbeb61SThiago Jung Bauermann 						     iint->ima_hash->digest,
271a5fbeb61SThiago Jung Bauermann 						     iint->ima_hash->length);
272a5fbeb61SThiago Jung Bauermann 		if (rc) {
273a5fbeb61SThiago Jung Bauermann 			*cause = "invalid-signature";
274a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_FAIL;
275a5fbeb61SThiago Jung Bauermann 		} else {
276a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_PASS;
277a5fbeb61SThiago Jung Bauermann 		}
278a5fbeb61SThiago Jung Bauermann 		break;
279a5fbeb61SThiago Jung Bauermann 	default:
280a5fbeb61SThiago Jung Bauermann 		*status = INTEGRITY_UNKNOWN;
281a5fbeb61SThiago Jung Bauermann 		*cause = "unknown-ima-data";
282a5fbeb61SThiago Jung Bauermann 		break;
283a5fbeb61SThiago Jung Bauermann 	}
284a5fbeb61SThiago Jung Bauermann 
285a5fbeb61SThiago Jung Bauermann 	return rc;
286a5fbeb61SThiago Jung Bauermann }
287a5fbeb61SThiago Jung Bauermann 
288a5fbeb61SThiago Jung Bauermann /*
28939b07096SThiago Jung Bauermann  * modsig_verify - verify modsig signature
29039b07096SThiago Jung Bauermann  *
29139b07096SThiago Jung Bauermann  * Verify whether the signature matches the file contents.
29239b07096SThiago Jung Bauermann  *
29339b07096SThiago Jung Bauermann  * Return 0 on success, error code otherwise.
29439b07096SThiago Jung Bauermann  */
29539b07096SThiago Jung Bauermann static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
29639b07096SThiago Jung Bauermann 			 enum integrity_status *status, const char **cause)
29739b07096SThiago Jung Bauermann {
29839b07096SThiago Jung Bauermann 	int rc;
29939b07096SThiago Jung Bauermann 
30039b07096SThiago Jung Bauermann 	rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
30139b07096SThiago Jung Bauermann 	if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
30239b07096SThiago Jung Bauermann 	    func == KEXEC_KERNEL_CHECK)
30339b07096SThiago Jung Bauermann 		rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
30439b07096SThiago Jung Bauermann 					     modsig);
30539b07096SThiago Jung Bauermann 	if (rc) {
30639b07096SThiago Jung Bauermann 		*cause = "invalid-signature";
30739b07096SThiago Jung Bauermann 		*status = INTEGRITY_FAIL;
30839b07096SThiago Jung Bauermann 	} else {
30939b07096SThiago Jung Bauermann 		*status = INTEGRITY_PASS;
31039b07096SThiago Jung Bauermann 	}
31139b07096SThiago Jung Bauermann 
31239b07096SThiago Jung Bauermann 	return rc;
31339b07096SThiago Jung Bauermann }
31439b07096SThiago Jung Bauermann 
31539b07096SThiago Jung Bauermann /*
316273df864SNayna Jain  * ima_check_blacklist - determine if the binary is blacklisted.
317273df864SNayna Jain  *
318273df864SNayna Jain  * Add the hash of the blacklisted binary to the measurement list, based
319273df864SNayna Jain  * on policy.
320273df864SNayna Jain  *
321273df864SNayna Jain  * Returns -EPERM if the hash is blacklisted.
322273df864SNayna Jain  */
323273df864SNayna Jain int ima_check_blacklist(struct integrity_iint_cache *iint,
324273df864SNayna Jain 			const struct modsig *modsig, int pcr)
325273df864SNayna Jain {
326273df864SNayna Jain 	enum hash_algo hash_algo;
327273df864SNayna Jain 	const u8 *digest = NULL;
328273df864SNayna Jain 	u32 digestsize = 0;
329273df864SNayna Jain 	int rc = 0;
330273df864SNayna Jain 
331273df864SNayna Jain 	if (!(iint->flags & IMA_CHECK_BLACKLIST))
332273df864SNayna Jain 		return 0;
333273df864SNayna Jain 
334273df864SNayna Jain 	if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
335273df864SNayna Jain 		ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
336273df864SNayna Jain 
337273df864SNayna Jain 		rc = is_binary_blacklisted(digest, digestsize);
338273df864SNayna Jain 		if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
3394834177eSTyler Hicks 			process_buffer_measurement(NULL, digest, digestsize,
340273df864SNayna Jain 						   "blacklisted-hash", NONE,
341e9085e0aSLakshmi Ramasubramanian 						   pcr, NULL);
342273df864SNayna Jain 	}
343273df864SNayna Jain 
344273df864SNayna Jain 	return rc;
345273df864SNayna Jain }
346273df864SNayna Jain 
347273df864SNayna Jain /*
3482fe5d6deSMimi Zohar  * ima_appraise_measurement - appraise file measurement
3492fe5d6deSMimi Zohar  *
3502fe5d6deSMimi Zohar  * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
3512fe5d6deSMimi Zohar  * Assuming success, compare the xattr hash with the collected measurement.
3522fe5d6deSMimi Zohar  *
3532fe5d6deSMimi Zohar  * Return 0 on success, error code otherwise
3542fe5d6deSMimi Zohar  */
3554ad87a3dSMimi Zohar int ima_appraise_measurement(enum ima_hooks func,
3564ad87a3dSMimi Zohar 			     struct integrity_iint_cache *iint,
357d3634d0fSDmitry Kasatkin 			     struct file *file, const unsigned char *filename,
358d3634d0fSDmitry Kasatkin 			     struct evm_ima_xattr_data *xattr_value,
35939b07096SThiago Jung Bauermann 			     int xattr_len, const struct modsig *modsig)
3602fe5d6deSMimi Zohar {
36152a13284SMimi Zohar 	static const char op[] = "appraise_data";
362f5e51fa3SThiago Jung Bauermann 	const char *cause = "unknown";
363e71b9dffSMiklos Szeredi 	struct dentry *dentry = file_dentry(file);
364c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
3652fe5d6deSMimi Zohar 	enum integrity_status status = INTEGRITY_UNKNOWN;
366a5fbeb61SThiago Jung Bauermann 	int rc = xattr_len;
36739b07096SThiago Jung Bauermann 	bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
3682fe5d6deSMimi Zohar 
36939b07096SThiago Jung Bauermann 	/* If not appraising a modsig, we need an xattr. */
37039b07096SThiago Jung Bauermann 	if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
3712fe5d6deSMimi Zohar 		return INTEGRITY_UNKNOWN;
3722fe5d6deSMimi Zohar 
37339b07096SThiago Jung Bauermann 	/* If reading the xattr failed and there's no modsig, error out. */
37439b07096SThiago Jung Bauermann 	if (rc <= 0 && !try_modsig) {
3752fe5d6deSMimi Zohar 		if (rc && rc != -ENODATA)
3762fe5d6deSMimi Zohar 			goto out;
3772fe5d6deSMimi Zohar 
378915d9d25SThiago Jung Bauermann 		cause = iint->flags & IMA_DIGSIG_REQUIRED ?
379915d9d25SThiago Jung Bauermann 				"IMA-signature-required" : "missing-hash";
380b151d6b0SDmitry Kasatkin 		status = INTEGRITY_NOLABEL;
3816035a27bSAl Viro 		if (file->f_mode & FMODE_CREATED)
382b151d6b0SDmitry Kasatkin 			iint->flags |= IMA_NEW_FILE;
3831ac202e9SDaniel Glöckner 		if ((iint->flags & IMA_NEW_FILE) &&
384b7e27bc1SMimi Zohar 		    (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
385b7e27bc1SMimi Zohar 		     (inode->i_size == 0)))
386b151d6b0SDmitry Kasatkin 			status = INTEGRITY_PASS;
3872fe5d6deSMimi Zohar 		goto out;
3882fe5d6deSMimi Zohar 	}
3892fe5d6deSMimi Zohar 
3908606404fSDmitry Kasatkin 	status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
391f5e51fa3SThiago Jung Bauermann 	switch (status) {
392f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_PASS:
393f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_PASS_IMMUTABLE:
394f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_UNKNOWN:
395f5e51fa3SThiago Jung Bauermann 		break;
396f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_NOXATTRS:	/* No EVM protected xattrs. */
39739b07096SThiago Jung Bauermann 		/* It's fine not to have xattrs when using a modsig. */
39839b07096SThiago Jung Bauermann 		if (try_modsig)
39939b07096SThiago Jung Bauermann 			break;
40039b07096SThiago Jung Bauermann 		/* fall through */
401f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_NOLABEL:		/* No security.evm xattr. */
4022fe5d6deSMimi Zohar 		cause = "missing-HMAC";
403f5e51fa3SThiago Jung Bauermann 		goto out;
404f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_FAIL:		/* Invalid HMAC/signature. */
4052fe5d6deSMimi Zohar 		cause = "invalid-HMAC";
4062fe5d6deSMimi Zohar 		goto out;
407f5e51fa3SThiago Jung Bauermann 	default:
408f5e51fa3SThiago Jung Bauermann 		WARN_ONCE(true, "Unexpected integrity status %d\n", status);
4092fe5d6deSMimi Zohar 	}
410f5e51fa3SThiago Jung Bauermann 
411a5fbeb61SThiago Jung Bauermann 	if (xattr_value)
412a5fbeb61SThiago Jung Bauermann 		rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
413a5fbeb61SThiago Jung Bauermann 				  &cause);
4148606404fSDmitry Kasatkin 
41539b07096SThiago Jung Bauermann 	/*
41639b07096SThiago Jung Bauermann 	 * If we have a modsig and either no imasig or the imasig's key isn't
41739b07096SThiago Jung Bauermann 	 * known, then try verifying the modsig.
41839b07096SThiago Jung Bauermann 	 */
41939b07096SThiago Jung Bauermann 	if (try_modsig &&
42039b07096SThiago Jung Bauermann 	    (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
42139b07096SThiago Jung Bauermann 	     rc == -ENOKEY))
42239b07096SThiago Jung Bauermann 		rc = modsig_verify(func, modsig, &status, &cause);
42339b07096SThiago Jung Bauermann 
4242fe5d6deSMimi Zohar out:
42557b56ac6SMimi Zohar 	/*
42657b56ac6SMimi Zohar 	 * File signatures on some filesystems can not be properly verified.
4279e67028eSMimi Zohar 	 * When such filesystems are mounted by an untrusted mounter or on a
4289e67028eSMimi Zohar 	 * system not willing to accept such a risk, fail the file signature
4299e67028eSMimi Zohar 	 * verification.
43057b56ac6SMimi Zohar 	 */
4319e67028eSMimi Zohar 	if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
4329e67028eSMimi Zohar 	    ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
4339e67028eSMimi Zohar 	     (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
43457b56ac6SMimi Zohar 		status = INTEGRITY_FAIL;
43557b56ac6SMimi Zohar 		cause = "unverifiable-signature";
43657b56ac6SMimi Zohar 		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
43757b56ac6SMimi Zohar 				    op, cause, rc, 0);
43857b56ac6SMimi Zohar 	} else if (status != INTEGRITY_PASS) {
439f5e51fa3SThiago Jung Bauermann 		/* Fix mode, but don't replace file signatures. */
44039b07096SThiago Jung Bauermann 		if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
4418606404fSDmitry Kasatkin 		    (!xattr_value ||
4428606404fSDmitry Kasatkin 		     xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
443def3e8b9SDmitry Kasatkin 			if (!ima_fix_xattr(dentry, iint))
4442fe5d6deSMimi Zohar 				status = INTEGRITY_PASS;
445f5e51fa3SThiago Jung Bauermann 		}
446f5e51fa3SThiago Jung Bauermann 
447f5e51fa3SThiago Jung Bauermann 		/* Permit new files with file signatures, but without data. */
448f5e51fa3SThiago Jung Bauermann 		if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
449f5e51fa3SThiago Jung Bauermann 		    xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
45005d1a717SMimi Zohar 			status = INTEGRITY_PASS;
4512fe5d6deSMimi Zohar 		}
452f5e51fa3SThiago Jung Bauermann 
4532fe5d6deSMimi Zohar 		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
4542fe5d6deSMimi Zohar 				    op, cause, rc, 0);
4558606404fSDmitry Kasatkin 	} else {
456d79d72e0SMimi Zohar 		ima_cache_flags(iint, func);
4572fe5d6deSMimi Zohar 	}
45857b56ac6SMimi Zohar 
459d79d72e0SMimi Zohar 	ima_set_cache_status(iint, func, status);
4602fe5d6deSMimi Zohar 	return status;
4612fe5d6deSMimi Zohar }
4622fe5d6deSMimi Zohar 
4632fe5d6deSMimi Zohar /*
4642fe5d6deSMimi Zohar  * ima_update_xattr - update 'security.ima' hash value
4652fe5d6deSMimi Zohar  */
4662fe5d6deSMimi Zohar void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
4672fe5d6deSMimi Zohar {
468e71b9dffSMiklos Szeredi 	struct dentry *dentry = file_dentry(file);
4692fe5d6deSMimi Zohar 	int rc = 0;
4702fe5d6deSMimi Zohar 
4718606404fSDmitry Kasatkin 	/* do not collect and update hash for digital signatures */
4720d73a552SDmitry Kasatkin 	if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
4738606404fSDmitry Kasatkin 		return;
4748606404fSDmitry Kasatkin 
475da1b0029SMimi Zohar 	if ((iint->ima_file_status != INTEGRITY_PASS) &&
476da1b0029SMimi Zohar 	    !(iint->flags & IMA_HASH))
477020aae3eSRoberto Sassu 		return;
478020aae3eSRoberto Sassu 
47915588227SThiago Jung Bauermann 	rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
4802fe5d6deSMimi Zohar 	if (rc < 0)
4812fe5d6deSMimi Zohar 		return;
4828606404fSDmitry Kasatkin 
4830d73a552SDmitry Kasatkin 	inode_lock(file_inode(file));
4842fe5d6deSMimi Zohar 	ima_fix_xattr(dentry, iint);
4850d73a552SDmitry Kasatkin 	inode_unlock(file_inode(file));
4862fe5d6deSMimi Zohar }
4872fe5d6deSMimi Zohar 
4882fe5d6deSMimi Zohar /**
4892fe5d6deSMimi Zohar  * ima_inode_post_setattr - reflect file metadata changes
4902fe5d6deSMimi Zohar  * @dentry: pointer to the affected dentry
4912fe5d6deSMimi Zohar  *
4922fe5d6deSMimi Zohar  * Changes to a dentry's metadata might result in needing to appraise.
4932fe5d6deSMimi Zohar  *
4942fe5d6deSMimi Zohar  * This function is called from notify_change(), which expects the caller
4952fe5d6deSMimi Zohar  * to lock the inode's i_mutex.
4962fe5d6deSMimi Zohar  */
4972fe5d6deSMimi Zohar void ima_inode_post_setattr(struct dentry *dentry)
4982fe5d6deSMimi Zohar {
499c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
5002fe5d6deSMimi Zohar 	struct integrity_iint_cache *iint;
501da1b0029SMimi Zohar 	int action;
5022fe5d6deSMimi Zohar 
503a756024eSRoberto Sassu 	if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
5045d6c3191SAndreas Gruenbacher 	    || !(inode->i_opflags & IOP_XATTR))
5052fe5d6deSMimi Zohar 		return;
5062fe5d6deSMimi Zohar 
507da1b0029SMimi Zohar 	action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
508da1b0029SMimi Zohar 	if (!action)
5095d6c3191SAndreas Gruenbacher 		__vfs_removexattr(dentry, XATTR_NAME_IMA);
5100d73a552SDmitry Kasatkin 	iint = integrity_iint_find(inode);
5110d73a552SDmitry Kasatkin 	if (iint) {
5120d73a552SDmitry Kasatkin 		set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
513da1b0029SMimi Zohar 		if (!action)
5140d73a552SDmitry Kasatkin 			clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
5150d73a552SDmitry Kasatkin 	}
5162fe5d6deSMimi Zohar }
51742c63330SMimi Zohar 
51842c63330SMimi Zohar /*
51942c63330SMimi Zohar  * ima_protect_xattr - protect 'security.ima'
52042c63330SMimi Zohar  *
52142c63330SMimi Zohar  * Ensure that not just anyone can modify or remove 'security.ima'.
52242c63330SMimi Zohar  */
52342c63330SMimi Zohar static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
52442c63330SMimi Zohar 			     const void *xattr_value, size_t xattr_value_len)
52542c63330SMimi Zohar {
52642c63330SMimi Zohar 	if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
52742c63330SMimi Zohar 		if (!capable(CAP_SYS_ADMIN))
52842c63330SMimi Zohar 			return -EPERM;
52942c63330SMimi Zohar 		return 1;
53042c63330SMimi Zohar 	}
53142c63330SMimi Zohar 	return 0;
53242c63330SMimi Zohar }
53342c63330SMimi Zohar 
534060bdebfSMimi Zohar static void ima_reset_appraise_flags(struct inode *inode, int digsig)
53542c63330SMimi Zohar {
53642c63330SMimi Zohar 	struct integrity_iint_cache *iint;
53742c63330SMimi Zohar 
538a756024eSRoberto Sassu 	if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
53942c63330SMimi Zohar 		return;
54042c63330SMimi Zohar 
54142c63330SMimi Zohar 	iint = integrity_iint_find(inode);
54242c63330SMimi Zohar 	if (!iint)
54342c63330SMimi Zohar 		return;
544a422638dSEric Richter 	iint->measured_pcrs = 0;
5450d73a552SDmitry Kasatkin 	set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
546060bdebfSMimi Zohar 	if (digsig)
5470d73a552SDmitry Kasatkin 		set_bit(IMA_DIGSIG, &iint->atomic_flags);
5480d73a552SDmitry Kasatkin 	else
5490d73a552SDmitry Kasatkin 		clear_bit(IMA_DIGSIG, &iint->atomic_flags);
55042c63330SMimi Zohar }
55142c63330SMimi Zohar 
55242c63330SMimi Zohar int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
55342c63330SMimi Zohar 		       const void *xattr_value, size_t xattr_value_len)
55442c63330SMimi Zohar {
555060bdebfSMimi Zohar 	const struct evm_ima_xattr_data *xvalue = xattr_value;
55642c63330SMimi Zohar 	int result;
55742c63330SMimi Zohar 
55842c63330SMimi Zohar 	result = ima_protect_xattr(dentry, xattr_name, xattr_value,
55942c63330SMimi Zohar 				   xattr_value_len);
56042c63330SMimi Zohar 	if (result == 1) {
561a48fda9dSDmitry Kasatkin 		if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
562a48fda9dSDmitry Kasatkin 			return -EINVAL;
563f5acb3dcSMimi Zohar 		ima_reset_appraise_flags(d_backing_inode(dentry),
564e5729f86SThiago Jung Bauermann 			xvalue->type == EVM_IMA_XATTR_DIGSIG);
56542c63330SMimi Zohar 		result = 0;
56642c63330SMimi Zohar 	}
56742c63330SMimi Zohar 	return result;
56842c63330SMimi Zohar }
56942c63330SMimi Zohar 
57042c63330SMimi Zohar int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
57142c63330SMimi Zohar {
57242c63330SMimi Zohar 	int result;
57342c63330SMimi Zohar 
57442c63330SMimi Zohar 	result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
57542c63330SMimi Zohar 	if (result == 1) {
576c6f493d6SDavid Howells 		ima_reset_appraise_flags(d_backing_inode(dentry), 0);
57742c63330SMimi Zohar 		result = 0;
57842c63330SMimi Zohar 	}
57942c63330SMimi Zohar 	return result;
58042c63330SMimi Zohar }
581