xref: /openbmc/linux/security/integrity/ima/ima_appraise.c (revision a5fbeb615ca42f913ace3291d636e96feabcc545)
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>
152fe5d6deSMimi Zohar 
162fe5d6deSMimi Zohar #include "ima.h"
172fe5d6deSMimi Zohar 
182fe5d6deSMimi Zohar static int __init default_appraise_setup(char *str)
192fe5d6deSMimi Zohar {
20e1f5e01fSMimi Zohar #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
212fe5d6deSMimi Zohar 	if (strncmp(str, "off", 3) == 0)
222fe5d6deSMimi Zohar 		ima_appraise = 0;
232faa6ef3SDmitry Kasatkin 	else if (strncmp(str, "log", 3) == 0)
242faa6ef3SDmitry Kasatkin 		ima_appraise = IMA_APPRAISE_LOG;
252fe5d6deSMimi Zohar 	else if (strncmp(str, "fix", 3) == 0)
262fe5d6deSMimi Zohar 		ima_appraise = IMA_APPRAISE_FIX;
27e1f5e01fSMimi Zohar #endif
282fe5d6deSMimi Zohar 	return 1;
292fe5d6deSMimi Zohar }
302fe5d6deSMimi Zohar 
312fe5d6deSMimi Zohar __setup("ima_appraise=", default_appraise_setup);
322fe5d6deSMimi Zohar 
332fe5d6deSMimi Zohar /*
346f6723e2SMimi Zohar  * is_ima_appraise_enabled - return appraise status
356f6723e2SMimi Zohar  *
366f6723e2SMimi Zohar  * Only return enabled, if not in ima_appraise="fix" or "log" modes.
376f6723e2SMimi Zohar  */
386f6723e2SMimi Zohar bool is_ima_appraise_enabled(void)
396f6723e2SMimi Zohar {
40e5729f86SThiago Jung Bauermann 	return ima_appraise & IMA_APPRAISE_ENFORCE;
416f6723e2SMimi Zohar }
426f6723e2SMimi Zohar 
436f6723e2SMimi Zohar /*
442fe5d6deSMimi Zohar  * ima_must_appraise - set appraise flag
452fe5d6deSMimi Zohar  *
46da1b0029SMimi Zohar  * Return 1 to appraise or hash
472fe5d6deSMimi Zohar  */
48d26e1936SDmitry Kasatkin int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
492fe5d6deSMimi Zohar {
50d906c10dSMatthew Garrett 	u32 secid;
51d906c10dSMatthew Garrett 
5207f6a794SMimi Zohar 	if (!ima_appraise)
532fe5d6deSMimi Zohar 		return 0;
5407f6a794SMimi Zohar 
55d906c10dSMatthew Garrett 	security_task_getsecid(current, &secid);
56d906c10dSMatthew Garrett 	return ima_match_policy(inode, current_cred(), secid, func, mask,
5719453ce0SMatthew Garrett 				IMA_APPRAISE | IMA_HASH, NULL, NULL);
582fe5d6deSMimi Zohar }
592fe5d6deSMimi Zohar 
60def3e8b9SDmitry Kasatkin static int ima_fix_xattr(struct dentry *dentry,
612fe5d6deSMimi Zohar 			 struct integrity_iint_cache *iint)
622fe5d6deSMimi Zohar {
633ea7a560SDmitry Kasatkin 	int rc, offset;
643ea7a560SDmitry Kasatkin 	u8 algo = iint->ima_hash->algo;
653ea7a560SDmitry Kasatkin 
663ea7a560SDmitry Kasatkin 	if (algo <= HASH_ALGO_SHA1) {
673ea7a560SDmitry Kasatkin 		offset = 1;
683ea7a560SDmitry Kasatkin 		iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
693ea7a560SDmitry Kasatkin 	} else {
703ea7a560SDmitry Kasatkin 		offset = 0;
713ea7a560SDmitry Kasatkin 		iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
723ea7a560SDmitry Kasatkin 		iint->ima_hash->xattr.ng.algo = algo;
733ea7a560SDmitry Kasatkin 	}
743ea7a560SDmitry Kasatkin 	rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
753ea7a560SDmitry Kasatkin 				   &iint->ima_hash->xattr.data[offset],
763ea7a560SDmitry Kasatkin 				   (sizeof(iint->ima_hash->xattr) - offset) +
773ea7a560SDmitry Kasatkin 				   iint->ima_hash->length, 0);
783ea7a560SDmitry Kasatkin 	return rc;
792fe5d6deSMimi Zohar }
802fe5d6deSMimi Zohar 
81d79d72e0SMimi Zohar /* Return specific func appraised cached result */
82d79d72e0SMimi Zohar enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
834ad87a3dSMimi Zohar 					   enum ima_hooks func)
84d79d72e0SMimi Zohar {
85d79d72e0SMimi Zohar 	switch (func) {
86d79d72e0SMimi Zohar 	case MMAP_CHECK:
87d79d72e0SMimi Zohar 		return iint->ima_mmap_status;
88d79d72e0SMimi Zohar 	case BPRM_CHECK:
89d79d72e0SMimi Zohar 		return iint->ima_bprm_status;
90d906c10dSMatthew Garrett 	case CREDS_CHECK:
91d906c10dSMatthew Garrett 		return iint->ima_creds_status;
92d79d72e0SMimi Zohar 	case FILE_CHECK:
93c6af8efeSMimi Zohar 	case POST_SETATTR:
94d79d72e0SMimi Zohar 		return iint->ima_file_status;
95c6af8efeSMimi Zohar 	case MODULE_CHECK ... MAX_CHECK - 1:
96c6af8efeSMimi Zohar 	default:
97c6af8efeSMimi Zohar 		return iint->ima_read_status;
98d79d72e0SMimi Zohar 	}
99d79d72e0SMimi Zohar }
100d79d72e0SMimi Zohar 
101d79d72e0SMimi Zohar static void ima_set_cache_status(struct integrity_iint_cache *iint,
1024ad87a3dSMimi Zohar 				 enum ima_hooks func,
1034ad87a3dSMimi Zohar 				 enum integrity_status status)
104d79d72e0SMimi Zohar {
105d79d72e0SMimi Zohar 	switch (func) {
106d79d72e0SMimi Zohar 	case MMAP_CHECK:
107d79d72e0SMimi Zohar 		iint->ima_mmap_status = status;
108d79d72e0SMimi Zohar 		break;
109d79d72e0SMimi Zohar 	case BPRM_CHECK:
110d79d72e0SMimi Zohar 		iint->ima_bprm_status = status;
111d79d72e0SMimi Zohar 		break;
112d906c10dSMatthew Garrett 	case CREDS_CHECK:
113d906c10dSMatthew Garrett 		iint->ima_creds_status = status;
11409186e50SGustavo A. R. Silva 		break;
115d79d72e0SMimi Zohar 	case FILE_CHECK:
116c6af8efeSMimi Zohar 	case POST_SETATTR:
117d79d72e0SMimi Zohar 		iint->ima_file_status = status;
118c6af8efeSMimi Zohar 		break;
119c6af8efeSMimi Zohar 	case MODULE_CHECK ... MAX_CHECK - 1:
120c6af8efeSMimi Zohar 	default:
121c6af8efeSMimi Zohar 		iint->ima_read_status = status;
122c6af8efeSMimi Zohar 		break;
123d79d72e0SMimi Zohar 	}
124d79d72e0SMimi Zohar }
125d79d72e0SMimi Zohar 
1264ad87a3dSMimi Zohar static void ima_cache_flags(struct integrity_iint_cache *iint,
1274ad87a3dSMimi Zohar 			     enum ima_hooks func)
128d79d72e0SMimi Zohar {
129d79d72e0SMimi Zohar 	switch (func) {
130d79d72e0SMimi Zohar 	case MMAP_CHECK:
131d79d72e0SMimi Zohar 		iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
132d79d72e0SMimi Zohar 		break;
133d79d72e0SMimi Zohar 	case BPRM_CHECK:
134d79d72e0SMimi Zohar 		iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
135d79d72e0SMimi Zohar 		break;
136d906c10dSMatthew Garrett 	case CREDS_CHECK:
137d906c10dSMatthew Garrett 		iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
138d906c10dSMatthew Garrett 		break;
139d79d72e0SMimi Zohar 	case FILE_CHECK:
140c6af8efeSMimi Zohar 	case POST_SETATTR:
141d79d72e0SMimi Zohar 		iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
142c6af8efeSMimi Zohar 		break;
143c6af8efeSMimi Zohar 	case MODULE_CHECK ... MAX_CHECK - 1:
144c6af8efeSMimi Zohar 	default:
145c6af8efeSMimi Zohar 		iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
146c6af8efeSMimi Zohar 		break;
147d79d72e0SMimi Zohar 	}
148d79d72e0SMimi Zohar }
149d79d72e0SMimi Zohar 
1501525b06dSDmitry Kasatkin enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
1511525b06dSDmitry Kasatkin 				 int xattr_len)
152d3634d0fSDmitry Kasatkin {
153d3634d0fSDmitry Kasatkin 	struct signature_v2_hdr *sig;
154b4bfec7fSSeth Forshee 	enum hash_algo ret;
155d3634d0fSDmitry Kasatkin 
1563ea7a560SDmitry Kasatkin 	if (!xattr_value || xattr_len < 2)
1571525b06dSDmitry Kasatkin 		/* return default hash algo */
1581525b06dSDmitry Kasatkin 		return ima_hash_algo;
159d3634d0fSDmitry Kasatkin 
1603ea7a560SDmitry Kasatkin 	switch (xattr_value->type) {
1613ea7a560SDmitry Kasatkin 	case EVM_IMA_XATTR_DIGSIG:
1623ea7a560SDmitry Kasatkin 		sig = (typeof(sig))xattr_value;
1633ea7a560SDmitry Kasatkin 		if (sig->version != 2 || xattr_len <= sizeof(*sig))
1641525b06dSDmitry Kasatkin 			return ima_hash_algo;
1651525b06dSDmitry Kasatkin 		return sig->hash_algo;
1663ea7a560SDmitry Kasatkin 		break;
1673ea7a560SDmitry Kasatkin 	case IMA_XATTR_DIGEST_NG:
168650b29dbSThiago Jung Bauermann 		/* first byte contains algorithm id */
169650b29dbSThiago Jung Bauermann 		ret = xattr_value->data[0];
170b4bfec7fSSeth Forshee 		if (ret < HASH_ALGO__LAST)
171b4bfec7fSSeth Forshee 			return ret;
1723ea7a560SDmitry Kasatkin 		break;
1733ea7a560SDmitry Kasatkin 	case IMA_XATTR_DIGEST:
1743ea7a560SDmitry Kasatkin 		/* this is for backward compatibility */
1753ea7a560SDmitry Kasatkin 		if (xattr_len == 21) {
1763ea7a560SDmitry Kasatkin 			unsigned int zero = 0;
177650b29dbSThiago Jung Bauermann 			if (!memcmp(&xattr_value->data[16], &zero, 4))
1781525b06dSDmitry Kasatkin 				return HASH_ALGO_MD5;
1793ea7a560SDmitry Kasatkin 			else
1801525b06dSDmitry Kasatkin 				return HASH_ALGO_SHA1;
1813ea7a560SDmitry Kasatkin 		} else if (xattr_len == 17)
1821525b06dSDmitry Kasatkin 			return HASH_ALGO_MD5;
1833ea7a560SDmitry Kasatkin 		break;
1843ea7a560SDmitry Kasatkin 	}
1851525b06dSDmitry Kasatkin 
1861525b06dSDmitry Kasatkin 	/* return default hash algo */
1871525b06dSDmitry Kasatkin 	return ima_hash_algo;
188d3634d0fSDmitry Kasatkin }
189d3634d0fSDmitry Kasatkin 
190d3634d0fSDmitry Kasatkin int ima_read_xattr(struct dentry *dentry,
191d3634d0fSDmitry Kasatkin 		   struct evm_ima_xattr_data **xattr_value)
192d3634d0fSDmitry Kasatkin {
1935d6c3191SAndreas Gruenbacher 	ssize_t ret;
194d3634d0fSDmitry Kasatkin 
1955d6c3191SAndreas Gruenbacher 	ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
196d3634d0fSDmitry Kasatkin 				 0, GFP_NOFS);
1975d6c3191SAndreas Gruenbacher 	if (ret == -EOPNOTSUPP)
1985d6c3191SAndreas Gruenbacher 		ret = 0;
1995d6c3191SAndreas Gruenbacher 	return ret;
200d3634d0fSDmitry Kasatkin }
201d3634d0fSDmitry Kasatkin 
2022fe5d6deSMimi Zohar /*
203*a5fbeb61SThiago Jung Bauermann  * xattr_verify - verify xattr digest or signature
204*a5fbeb61SThiago Jung Bauermann  *
205*a5fbeb61SThiago Jung Bauermann  * Verify whether the hash or signature matches the file contents.
206*a5fbeb61SThiago Jung Bauermann  *
207*a5fbeb61SThiago Jung Bauermann  * Return 0 on success, error code otherwise.
208*a5fbeb61SThiago Jung Bauermann  */
209*a5fbeb61SThiago Jung Bauermann static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
210*a5fbeb61SThiago Jung Bauermann 			struct evm_ima_xattr_data *xattr_value, int xattr_len,
211*a5fbeb61SThiago Jung Bauermann 			enum integrity_status *status, const char **cause)
212*a5fbeb61SThiago Jung Bauermann {
213*a5fbeb61SThiago Jung Bauermann 	int rc = -EINVAL, hash_start = 0;
214*a5fbeb61SThiago Jung Bauermann 
215*a5fbeb61SThiago Jung Bauermann 	switch (xattr_value->type) {
216*a5fbeb61SThiago Jung Bauermann 	case IMA_XATTR_DIGEST_NG:
217*a5fbeb61SThiago Jung Bauermann 		/* first byte contains algorithm id */
218*a5fbeb61SThiago Jung Bauermann 		hash_start = 1;
219*a5fbeb61SThiago Jung Bauermann 		/* fall through */
220*a5fbeb61SThiago Jung Bauermann 	case IMA_XATTR_DIGEST:
221*a5fbeb61SThiago Jung Bauermann 		if (iint->flags & IMA_DIGSIG_REQUIRED) {
222*a5fbeb61SThiago Jung Bauermann 			*cause = "IMA-signature-required";
223*a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_FAIL;
224*a5fbeb61SThiago Jung Bauermann 			break;
225*a5fbeb61SThiago Jung Bauermann 		}
226*a5fbeb61SThiago Jung Bauermann 		clear_bit(IMA_DIGSIG, &iint->atomic_flags);
227*a5fbeb61SThiago Jung Bauermann 		if (xattr_len - sizeof(xattr_value->type) - hash_start >=
228*a5fbeb61SThiago Jung Bauermann 				iint->ima_hash->length)
229*a5fbeb61SThiago Jung Bauermann 			/*
230*a5fbeb61SThiago Jung Bauermann 			 * xattr length may be longer. md5 hash in previous
231*a5fbeb61SThiago Jung Bauermann 			 * version occupied 20 bytes in xattr, instead of 16
232*a5fbeb61SThiago Jung Bauermann 			 */
233*a5fbeb61SThiago Jung Bauermann 			rc = memcmp(&xattr_value->data[hash_start],
234*a5fbeb61SThiago Jung Bauermann 				    iint->ima_hash->digest,
235*a5fbeb61SThiago Jung Bauermann 				    iint->ima_hash->length);
236*a5fbeb61SThiago Jung Bauermann 		else
237*a5fbeb61SThiago Jung Bauermann 			rc = -EINVAL;
238*a5fbeb61SThiago Jung Bauermann 		if (rc) {
239*a5fbeb61SThiago Jung Bauermann 			*cause = "invalid-hash";
240*a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_FAIL;
241*a5fbeb61SThiago Jung Bauermann 			break;
242*a5fbeb61SThiago Jung Bauermann 		}
243*a5fbeb61SThiago Jung Bauermann 		*status = INTEGRITY_PASS;
244*a5fbeb61SThiago Jung Bauermann 		break;
245*a5fbeb61SThiago Jung Bauermann 	case EVM_IMA_XATTR_DIGSIG:
246*a5fbeb61SThiago Jung Bauermann 		set_bit(IMA_DIGSIG, &iint->atomic_flags);
247*a5fbeb61SThiago Jung Bauermann 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
248*a5fbeb61SThiago Jung Bauermann 					     (const char *)xattr_value,
249*a5fbeb61SThiago Jung Bauermann 					     xattr_len,
250*a5fbeb61SThiago Jung Bauermann 					     iint->ima_hash->digest,
251*a5fbeb61SThiago Jung Bauermann 					     iint->ima_hash->length);
252*a5fbeb61SThiago Jung Bauermann 		if (rc == -EOPNOTSUPP) {
253*a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_UNKNOWN;
254*a5fbeb61SThiago Jung Bauermann 			break;
255*a5fbeb61SThiago Jung Bauermann 		}
256*a5fbeb61SThiago Jung Bauermann 		if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
257*a5fbeb61SThiago Jung Bauermann 		    func == KEXEC_KERNEL_CHECK)
258*a5fbeb61SThiago Jung Bauermann 			rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
259*a5fbeb61SThiago Jung Bauermann 						     (const char *)xattr_value,
260*a5fbeb61SThiago Jung Bauermann 						     xattr_len,
261*a5fbeb61SThiago Jung Bauermann 						     iint->ima_hash->digest,
262*a5fbeb61SThiago Jung Bauermann 						     iint->ima_hash->length);
263*a5fbeb61SThiago Jung Bauermann 		if (rc) {
264*a5fbeb61SThiago Jung Bauermann 			*cause = "invalid-signature";
265*a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_FAIL;
266*a5fbeb61SThiago Jung Bauermann 		} else {
267*a5fbeb61SThiago Jung Bauermann 			*status = INTEGRITY_PASS;
268*a5fbeb61SThiago Jung Bauermann 		}
269*a5fbeb61SThiago Jung Bauermann 		break;
270*a5fbeb61SThiago Jung Bauermann 	default:
271*a5fbeb61SThiago Jung Bauermann 		*status = INTEGRITY_UNKNOWN;
272*a5fbeb61SThiago Jung Bauermann 		*cause = "unknown-ima-data";
273*a5fbeb61SThiago Jung Bauermann 		break;
274*a5fbeb61SThiago Jung Bauermann 	}
275*a5fbeb61SThiago Jung Bauermann 
276*a5fbeb61SThiago Jung Bauermann 	return rc;
277*a5fbeb61SThiago Jung Bauermann }
278*a5fbeb61SThiago Jung Bauermann 
279*a5fbeb61SThiago Jung Bauermann /*
2802fe5d6deSMimi Zohar  * ima_appraise_measurement - appraise file measurement
2812fe5d6deSMimi Zohar  *
2822fe5d6deSMimi Zohar  * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
2832fe5d6deSMimi Zohar  * Assuming success, compare the xattr hash with the collected measurement.
2842fe5d6deSMimi Zohar  *
2852fe5d6deSMimi Zohar  * Return 0 on success, error code otherwise
2862fe5d6deSMimi Zohar  */
2874ad87a3dSMimi Zohar int ima_appraise_measurement(enum ima_hooks func,
2884ad87a3dSMimi Zohar 			     struct integrity_iint_cache *iint,
289d3634d0fSDmitry Kasatkin 			     struct file *file, const unsigned char *filename,
290d3634d0fSDmitry Kasatkin 			     struct evm_ima_xattr_data *xattr_value,
2916035a27bSAl Viro 			     int xattr_len)
2922fe5d6deSMimi Zohar {
29352a13284SMimi Zohar 	static const char op[] = "appraise_data";
294f5e51fa3SThiago Jung Bauermann 	const char *cause = "unknown";
295e71b9dffSMiklos Szeredi 	struct dentry *dentry = file_dentry(file);
296c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
2972fe5d6deSMimi Zohar 	enum integrity_status status = INTEGRITY_UNKNOWN;
298*a5fbeb61SThiago Jung Bauermann 	int rc = xattr_len;
2992fe5d6deSMimi Zohar 
3005d6c3191SAndreas Gruenbacher 	if (!(inode->i_opflags & IOP_XATTR))
3012fe5d6deSMimi Zohar 		return INTEGRITY_UNKNOWN;
3022fe5d6deSMimi Zohar 
3032fe5d6deSMimi Zohar 	if (rc <= 0) {
3042fe5d6deSMimi Zohar 		if (rc && rc != -ENODATA)
3052fe5d6deSMimi Zohar 			goto out;
3062fe5d6deSMimi Zohar 
307915d9d25SThiago Jung Bauermann 		cause = iint->flags & IMA_DIGSIG_REQUIRED ?
308915d9d25SThiago Jung Bauermann 				"IMA-signature-required" : "missing-hash";
309b151d6b0SDmitry Kasatkin 		status = INTEGRITY_NOLABEL;
3106035a27bSAl Viro 		if (file->f_mode & FMODE_CREATED)
311b151d6b0SDmitry Kasatkin 			iint->flags |= IMA_NEW_FILE;
3121ac202e9SDaniel Glöckner 		if ((iint->flags & IMA_NEW_FILE) &&
313b7e27bc1SMimi Zohar 		    (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
314b7e27bc1SMimi Zohar 		     (inode->i_size == 0)))
315b151d6b0SDmitry Kasatkin 			status = INTEGRITY_PASS;
3162fe5d6deSMimi Zohar 		goto out;
3172fe5d6deSMimi Zohar 	}
3182fe5d6deSMimi Zohar 
3198606404fSDmitry Kasatkin 	status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
320f5e51fa3SThiago Jung Bauermann 	switch (status) {
321f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_PASS:
322f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_PASS_IMMUTABLE:
323f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_UNKNOWN:
324f5e51fa3SThiago Jung Bauermann 		break;
325f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_NOXATTRS:	/* No EVM protected xattrs. */
326f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_NOLABEL:		/* No security.evm xattr. */
3272fe5d6deSMimi Zohar 		cause = "missing-HMAC";
328f5e51fa3SThiago Jung Bauermann 		goto out;
329f5e51fa3SThiago Jung Bauermann 	case INTEGRITY_FAIL:		/* Invalid HMAC/signature. */
3302fe5d6deSMimi Zohar 		cause = "invalid-HMAC";
3312fe5d6deSMimi Zohar 		goto out;
332f5e51fa3SThiago Jung Bauermann 	default:
333f5e51fa3SThiago Jung Bauermann 		WARN_ONCE(true, "Unexpected integrity status %d\n", status);
3342fe5d6deSMimi Zohar 	}
335f5e51fa3SThiago Jung Bauermann 
336*a5fbeb61SThiago Jung Bauermann 	if (xattr_value)
337*a5fbeb61SThiago Jung Bauermann 		rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
338*a5fbeb61SThiago Jung Bauermann 				  &cause);
3398606404fSDmitry Kasatkin 
3402fe5d6deSMimi Zohar out:
34157b56ac6SMimi Zohar 	/*
34257b56ac6SMimi Zohar 	 * File signatures on some filesystems can not be properly verified.
3439e67028eSMimi Zohar 	 * When such filesystems are mounted by an untrusted mounter or on a
3449e67028eSMimi Zohar 	 * system not willing to accept such a risk, fail the file signature
3459e67028eSMimi Zohar 	 * verification.
34657b56ac6SMimi Zohar 	 */
3479e67028eSMimi Zohar 	if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
3489e67028eSMimi Zohar 	    ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
3499e67028eSMimi Zohar 	     (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
35057b56ac6SMimi Zohar 		status = INTEGRITY_FAIL;
35157b56ac6SMimi Zohar 		cause = "unverifiable-signature";
35257b56ac6SMimi Zohar 		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
35357b56ac6SMimi Zohar 				    op, cause, rc, 0);
35457b56ac6SMimi Zohar 	} else if (status != INTEGRITY_PASS) {
355f5e51fa3SThiago Jung Bauermann 		/* Fix mode, but don't replace file signatures. */
3568606404fSDmitry Kasatkin 		if ((ima_appraise & IMA_APPRAISE_FIX) &&
3578606404fSDmitry Kasatkin 		    (!xattr_value ||
3588606404fSDmitry Kasatkin 		     xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
359def3e8b9SDmitry Kasatkin 			if (!ima_fix_xattr(dentry, iint))
3602fe5d6deSMimi Zohar 				status = INTEGRITY_PASS;
361f5e51fa3SThiago Jung Bauermann 		}
362f5e51fa3SThiago Jung Bauermann 
363f5e51fa3SThiago Jung Bauermann 		/* Permit new files with file signatures, but without data. */
364f5e51fa3SThiago Jung Bauermann 		if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
365f5e51fa3SThiago Jung Bauermann 		    xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
36605d1a717SMimi Zohar 			status = INTEGRITY_PASS;
3672fe5d6deSMimi Zohar 		}
368f5e51fa3SThiago Jung Bauermann 
3692fe5d6deSMimi Zohar 		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
3702fe5d6deSMimi Zohar 				    op, cause, rc, 0);
3718606404fSDmitry Kasatkin 	} else {
372d79d72e0SMimi Zohar 		ima_cache_flags(iint, func);
3732fe5d6deSMimi Zohar 	}
37457b56ac6SMimi Zohar 
375d79d72e0SMimi Zohar 	ima_set_cache_status(iint, func, status);
3762fe5d6deSMimi Zohar 	return status;
3772fe5d6deSMimi Zohar }
3782fe5d6deSMimi Zohar 
3792fe5d6deSMimi Zohar /*
3802fe5d6deSMimi Zohar  * ima_update_xattr - update 'security.ima' hash value
3812fe5d6deSMimi Zohar  */
3822fe5d6deSMimi Zohar void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
3832fe5d6deSMimi Zohar {
384e71b9dffSMiklos Szeredi 	struct dentry *dentry = file_dentry(file);
3852fe5d6deSMimi Zohar 	int rc = 0;
3862fe5d6deSMimi Zohar 
3878606404fSDmitry Kasatkin 	/* do not collect and update hash for digital signatures */
3880d73a552SDmitry Kasatkin 	if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
3898606404fSDmitry Kasatkin 		return;
3908606404fSDmitry Kasatkin 
391da1b0029SMimi Zohar 	if ((iint->ima_file_status != INTEGRITY_PASS) &&
392da1b0029SMimi Zohar 	    !(iint->flags & IMA_HASH))
393020aae3eSRoberto Sassu 		return;
394020aae3eSRoberto Sassu 
395cf222217SMimi Zohar 	rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
3962fe5d6deSMimi Zohar 	if (rc < 0)
3972fe5d6deSMimi Zohar 		return;
3988606404fSDmitry Kasatkin 
3990d73a552SDmitry Kasatkin 	inode_lock(file_inode(file));
4002fe5d6deSMimi Zohar 	ima_fix_xattr(dentry, iint);
4010d73a552SDmitry Kasatkin 	inode_unlock(file_inode(file));
4022fe5d6deSMimi Zohar }
4032fe5d6deSMimi Zohar 
4042fe5d6deSMimi Zohar /**
4052fe5d6deSMimi Zohar  * ima_inode_post_setattr - reflect file metadata changes
4062fe5d6deSMimi Zohar  * @dentry: pointer to the affected dentry
4072fe5d6deSMimi Zohar  *
4082fe5d6deSMimi Zohar  * Changes to a dentry's metadata might result in needing to appraise.
4092fe5d6deSMimi Zohar  *
4102fe5d6deSMimi Zohar  * This function is called from notify_change(), which expects the caller
4112fe5d6deSMimi Zohar  * to lock the inode's i_mutex.
4122fe5d6deSMimi Zohar  */
4132fe5d6deSMimi Zohar void ima_inode_post_setattr(struct dentry *dentry)
4142fe5d6deSMimi Zohar {
415c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
4162fe5d6deSMimi Zohar 	struct integrity_iint_cache *iint;
417da1b0029SMimi Zohar 	int action;
4182fe5d6deSMimi Zohar 
419a756024eSRoberto Sassu 	if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
4205d6c3191SAndreas Gruenbacher 	    || !(inode->i_opflags & IOP_XATTR))
4212fe5d6deSMimi Zohar 		return;
4222fe5d6deSMimi Zohar 
423da1b0029SMimi Zohar 	action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
424da1b0029SMimi Zohar 	if (!action)
4255d6c3191SAndreas Gruenbacher 		__vfs_removexattr(dentry, XATTR_NAME_IMA);
4260d73a552SDmitry Kasatkin 	iint = integrity_iint_find(inode);
4270d73a552SDmitry Kasatkin 	if (iint) {
4280d73a552SDmitry Kasatkin 		set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
429da1b0029SMimi Zohar 		if (!action)
4300d73a552SDmitry Kasatkin 			clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
4310d73a552SDmitry Kasatkin 	}
4322fe5d6deSMimi Zohar }
43342c63330SMimi Zohar 
43442c63330SMimi Zohar /*
43542c63330SMimi Zohar  * ima_protect_xattr - protect 'security.ima'
43642c63330SMimi Zohar  *
43742c63330SMimi Zohar  * Ensure that not just anyone can modify or remove 'security.ima'.
43842c63330SMimi Zohar  */
43942c63330SMimi Zohar static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
44042c63330SMimi Zohar 			     const void *xattr_value, size_t xattr_value_len)
44142c63330SMimi Zohar {
44242c63330SMimi Zohar 	if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
44342c63330SMimi Zohar 		if (!capable(CAP_SYS_ADMIN))
44442c63330SMimi Zohar 			return -EPERM;
44542c63330SMimi Zohar 		return 1;
44642c63330SMimi Zohar 	}
44742c63330SMimi Zohar 	return 0;
44842c63330SMimi Zohar }
44942c63330SMimi Zohar 
450060bdebfSMimi Zohar static void ima_reset_appraise_flags(struct inode *inode, int digsig)
45142c63330SMimi Zohar {
45242c63330SMimi Zohar 	struct integrity_iint_cache *iint;
45342c63330SMimi Zohar 
454a756024eSRoberto Sassu 	if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
45542c63330SMimi Zohar 		return;
45642c63330SMimi Zohar 
45742c63330SMimi Zohar 	iint = integrity_iint_find(inode);
45842c63330SMimi Zohar 	if (!iint)
45942c63330SMimi Zohar 		return;
460a422638dSEric Richter 	iint->measured_pcrs = 0;
4610d73a552SDmitry Kasatkin 	set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
462060bdebfSMimi Zohar 	if (digsig)
4630d73a552SDmitry Kasatkin 		set_bit(IMA_DIGSIG, &iint->atomic_flags);
4640d73a552SDmitry Kasatkin 	else
4650d73a552SDmitry Kasatkin 		clear_bit(IMA_DIGSIG, &iint->atomic_flags);
46642c63330SMimi Zohar }
46742c63330SMimi Zohar 
46842c63330SMimi Zohar int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
46942c63330SMimi Zohar 		       const void *xattr_value, size_t xattr_value_len)
47042c63330SMimi Zohar {
471060bdebfSMimi Zohar 	const struct evm_ima_xattr_data *xvalue = xattr_value;
47242c63330SMimi Zohar 	int result;
47342c63330SMimi Zohar 
47442c63330SMimi Zohar 	result = ima_protect_xattr(dentry, xattr_name, xattr_value,
47542c63330SMimi Zohar 				   xattr_value_len);
47642c63330SMimi Zohar 	if (result == 1) {
477a48fda9dSDmitry Kasatkin 		if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
478a48fda9dSDmitry Kasatkin 			return -EINVAL;
479f5acb3dcSMimi Zohar 		ima_reset_appraise_flags(d_backing_inode(dentry),
480e5729f86SThiago Jung Bauermann 			xvalue->type == EVM_IMA_XATTR_DIGSIG);
48142c63330SMimi Zohar 		result = 0;
48242c63330SMimi Zohar 	}
48342c63330SMimi Zohar 	return result;
48442c63330SMimi Zohar }
48542c63330SMimi Zohar 
48642c63330SMimi Zohar int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
48742c63330SMimi Zohar {
48842c63330SMimi Zohar 	int result;
48942c63330SMimi Zohar 
49042c63330SMimi Zohar 	result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
49142c63330SMimi Zohar 	if (result == 1) {
492c6f493d6SDavid Howells 		ima_reset_appraise_flags(d_backing_inode(dentry), 0);
49342c63330SMimi Zohar 		result = 0;
49442c63330SMimi Zohar 	}
49542c63330SMimi Zohar 	return result;
49642c63330SMimi Zohar }
497