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