xref: /OK3568_Linux_fs/kernel/security/integrity/evm/evm_main.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2005-2010 IBM Corporation
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Author:
6*4882a593Smuzhiyun  * Mimi Zohar <zohar@us.ibm.com>
7*4882a593Smuzhiyun  * Kylene Hall <kjhall@us.ibm.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * File: evm_main.c
10*4882a593Smuzhiyun  *	implements evm_inode_setxattr, evm_inode_post_setxattr,
11*4882a593Smuzhiyun  *	evm_inode_removexattr, and evm_verifyxattr
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/init.h>
15*4882a593Smuzhiyun #include <linux/crypto.h>
16*4882a593Smuzhiyun #include <linux/audit.h>
17*4882a593Smuzhiyun #include <linux/xattr.h>
18*4882a593Smuzhiyun #include <linux/integrity.h>
19*4882a593Smuzhiyun #include <linux/evm.h>
20*4882a593Smuzhiyun #include <linux/magic.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <crypto/hash.h>
23*4882a593Smuzhiyun #include <crypto/hash_info.h>
24*4882a593Smuzhiyun #include <crypto/algapi.h>
25*4882a593Smuzhiyun #include "evm.h"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun int evm_initialized;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun static const char * const integrity_status_msg[] = {
30*4882a593Smuzhiyun 	"pass", "pass_immutable", "fail", "no_label", "no_xattrs", "unknown"
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun int evm_hmac_attrs;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun static struct xattr_list evm_config_default_xattrnames[] = {
35*4882a593Smuzhiyun #ifdef CONFIG_SECURITY_SELINUX
36*4882a593Smuzhiyun 	{.name = XATTR_NAME_SELINUX},
37*4882a593Smuzhiyun #endif
38*4882a593Smuzhiyun #ifdef CONFIG_SECURITY_SMACK
39*4882a593Smuzhiyun 	{.name = XATTR_NAME_SMACK},
40*4882a593Smuzhiyun #ifdef CONFIG_EVM_EXTRA_SMACK_XATTRS
41*4882a593Smuzhiyun 	{.name = XATTR_NAME_SMACKEXEC},
42*4882a593Smuzhiyun 	{.name = XATTR_NAME_SMACKTRANSMUTE},
43*4882a593Smuzhiyun 	{.name = XATTR_NAME_SMACKMMAP},
44*4882a593Smuzhiyun #endif
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun #ifdef CONFIG_SECURITY_APPARMOR
47*4882a593Smuzhiyun 	{.name = XATTR_NAME_APPARMOR},
48*4882a593Smuzhiyun #endif
49*4882a593Smuzhiyun #ifdef CONFIG_IMA_APPRAISE
50*4882a593Smuzhiyun 	{.name = XATTR_NAME_IMA},
51*4882a593Smuzhiyun #endif
52*4882a593Smuzhiyun 	{.name = XATTR_NAME_CAPS},
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun LIST_HEAD(evm_config_xattrnames);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun static int evm_fixmode __ro_after_init;
evm_set_fixmode(char * str)58*4882a593Smuzhiyun static int __init evm_set_fixmode(char *str)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	if (strncmp(str, "fix", 3) == 0)
61*4882a593Smuzhiyun 		evm_fixmode = 1;
62*4882a593Smuzhiyun 	else
63*4882a593Smuzhiyun 		pr_err("invalid \"%s\" mode", str);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	return 1;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun __setup("evm=", evm_set_fixmode);
68*4882a593Smuzhiyun 
evm_init_config(void)69*4882a593Smuzhiyun static void __init evm_init_config(void)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	int i, xattrs;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	xattrs = ARRAY_SIZE(evm_config_default_xattrnames);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	pr_info("Initialising EVM extended attributes:\n");
76*4882a593Smuzhiyun 	for (i = 0; i < xattrs; i++) {
77*4882a593Smuzhiyun 		pr_info("%s\n", evm_config_default_xattrnames[i].name);
78*4882a593Smuzhiyun 		list_add_tail(&evm_config_default_xattrnames[i].list,
79*4882a593Smuzhiyun 			      &evm_config_xattrnames);
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #ifdef CONFIG_EVM_ATTR_FSUUID
83*4882a593Smuzhiyun 	evm_hmac_attrs |= EVM_ATTR_FSUUID;
84*4882a593Smuzhiyun #endif
85*4882a593Smuzhiyun 	pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
evm_key_loaded(void)88*4882a593Smuzhiyun static bool evm_key_loaded(void)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	return (bool)(evm_initialized & EVM_KEY_MASK);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
evm_find_protected_xattrs(struct dentry * dentry)93*4882a593Smuzhiyun static int evm_find_protected_xattrs(struct dentry *dentry)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun 	struct inode *inode = d_backing_inode(dentry);
96*4882a593Smuzhiyun 	struct xattr_list *xattr;
97*4882a593Smuzhiyun 	int error;
98*4882a593Smuzhiyun 	int count = 0;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (!(inode->i_opflags & IOP_XATTR))
101*4882a593Smuzhiyun 		return -EOPNOTSUPP;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
104*4882a593Smuzhiyun 		error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0,
105*4882a593Smuzhiyun 				       XATTR_NOSECURITY);
106*4882a593Smuzhiyun 		if (error < 0) {
107*4882a593Smuzhiyun 			if (error == -ENODATA)
108*4882a593Smuzhiyun 				continue;
109*4882a593Smuzhiyun 			return error;
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 		count++;
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	return count;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun  * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * Compute the HMAC on the dentry's protected set of extended attributes
121*4882a593Smuzhiyun  * and compare it against the stored security.evm xattr.
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  * For performance:
124*4882a593Smuzhiyun  * - use the previoulsy retrieved xattr value and length to calculate the
125*4882a593Smuzhiyun  *   HMAC.)
126*4882a593Smuzhiyun  * - cache the verification result in the iint, when available.
127*4882a593Smuzhiyun  *
128*4882a593Smuzhiyun  * Returns integrity status
129*4882a593Smuzhiyun  */
evm_verify_hmac(struct dentry * dentry,const char * xattr_name,char * xattr_value,size_t xattr_value_len,struct integrity_iint_cache * iint)130*4882a593Smuzhiyun static enum integrity_status evm_verify_hmac(struct dentry *dentry,
131*4882a593Smuzhiyun 					     const char *xattr_name,
132*4882a593Smuzhiyun 					     char *xattr_value,
133*4882a593Smuzhiyun 					     size_t xattr_value_len,
134*4882a593Smuzhiyun 					     struct integrity_iint_cache *iint)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	struct evm_ima_xattr_data *xattr_data = NULL;
137*4882a593Smuzhiyun 	struct signature_v2_hdr *hdr;
138*4882a593Smuzhiyun 	enum integrity_status evm_status = INTEGRITY_PASS;
139*4882a593Smuzhiyun 	struct evm_digest digest;
140*4882a593Smuzhiyun 	struct inode *inode;
141*4882a593Smuzhiyun 	int rc, xattr_len;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	if (iint && (iint->evm_status == INTEGRITY_PASS ||
144*4882a593Smuzhiyun 		     iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
145*4882a593Smuzhiyun 		return iint->evm_status;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* if status is not PASS, try to check again - against -ENOMEM */
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* first need to know the sig type */
150*4882a593Smuzhiyun 	rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
151*4882a593Smuzhiyun 				GFP_NOFS);
152*4882a593Smuzhiyun 	if (rc <= 0) {
153*4882a593Smuzhiyun 		evm_status = INTEGRITY_FAIL;
154*4882a593Smuzhiyun 		if (rc == -ENODATA) {
155*4882a593Smuzhiyun 			rc = evm_find_protected_xattrs(dentry);
156*4882a593Smuzhiyun 			if (rc > 0)
157*4882a593Smuzhiyun 				evm_status = INTEGRITY_NOLABEL;
158*4882a593Smuzhiyun 			else if (rc == 0)
159*4882a593Smuzhiyun 				evm_status = INTEGRITY_NOXATTRS; /* new file */
160*4882a593Smuzhiyun 		} else if (rc == -EOPNOTSUPP) {
161*4882a593Smuzhiyun 			evm_status = INTEGRITY_UNKNOWN;
162*4882a593Smuzhiyun 		}
163*4882a593Smuzhiyun 		goto out;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	xattr_len = rc;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	/* check value type */
169*4882a593Smuzhiyun 	switch (xattr_data->type) {
170*4882a593Smuzhiyun 	case EVM_XATTR_HMAC:
171*4882a593Smuzhiyun 		if (xattr_len != sizeof(struct evm_xattr)) {
172*4882a593Smuzhiyun 			evm_status = INTEGRITY_FAIL;
173*4882a593Smuzhiyun 			goto out;
174*4882a593Smuzhiyun 		}
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 		digest.hdr.algo = HASH_ALGO_SHA1;
177*4882a593Smuzhiyun 		rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
178*4882a593Smuzhiyun 				   xattr_value_len, &digest);
179*4882a593Smuzhiyun 		if (rc)
180*4882a593Smuzhiyun 			break;
181*4882a593Smuzhiyun 		rc = crypto_memneq(xattr_data->data, digest.digest,
182*4882a593Smuzhiyun 				   SHA1_DIGEST_SIZE);
183*4882a593Smuzhiyun 		if (rc)
184*4882a593Smuzhiyun 			rc = -EINVAL;
185*4882a593Smuzhiyun 		break;
186*4882a593Smuzhiyun 	case EVM_IMA_XATTR_DIGSIG:
187*4882a593Smuzhiyun 	case EVM_XATTR_PORTABLE_DIGSIG:
188*4882a593Smuzhiyun 		/* accept xattr with non-empty signature field */
189*4882a593Smuzhiyun 		if (xattr_len <= sizeof(struct signature_v2_hdr)) {
190*4882a593Smuzhiyun 			evm_status = INTEGRITY_FAIL;
191*4882a593Smuzhiyun 			goto out;
192*4882a593Smuzhiyun 		}
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 		hdr = (struct signature_v2_hdr *)xattr_data;
195*4882a593Smuzhiyun 		digest.hdr.algo = hdr->hash_algo;
196*4882a593Smuzhiyun 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
197*4882a593Smuzhiyun 				   xattr_value_len, xattr_data->type, &digest);
198*4882a593Smuzhiyun 		if (rc)
199*4882a593Smuzhiyun 			break;
200*4882a593Smuzhiyun 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
201*4882a593Smuzhiyun 					(const char *)xattr_data, xattr_len,
202*4882a593Smuzhiyun 					digest.digest, digest.hdr.length);
203*4882a593Smuzhiyun 		if (!rc) {
204*4882a593Smuzhiyun 			inode = d_backing_inode(dentry);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 			if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
207*4882a593Smuzhiyun 				if (iint)
208*4882a593Smuzhiyun 					iint->flags |= EVM_IMMUTABLE_DIGSIG;
209*4882a593Smuzhiyun 				evm_status = INTEGRITY_PASS_IMMUTABLE;
210*4882a593Smuzhiyun 			} else if (!IS_RDONLY(inode) &&
211*4882a593Smuzhiyun 				   !(inode->i_sb->s_readonly_remount) &&
212*4882a593Smuzhiyun 				   !IS_IMMUTABLE(inode)) {
213*4882a593Smuzhiyun 				evm_update_evmxattr(dentry, xattr_name,
214*4882a593Smuzhiyun 						    xattr_value,
215*4882a593Smuzhiyun 						    xattr_value_len);
216*4882a593Smuzhiyun 			}
217*4882a593Smuzhiyun 		}
218*4882a593Smuzhiyun 		break;
219*4882a593Smuzhiyun 	default:
220*4882a593Smuzhiyun 		rc = -EINVAL;
221*4882a593Smuzhiyun 		break;
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	if (rc)
225*4882a593Smuzhiyun 		evm_status = (rc == -ENODATA) ?
226*4882a593Smuzhiyun 				INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
227*4882a593Smuzhiyun out:
228*4882a593Smuzhiyun 	if (iint)
229*4882a593Smuzhiyun 		iint->evm_status = evm_status;
230*4882a593Smuzhiyun 	kfree(xattr_data);
231*4882a593Smuzhiyun 	return evm_status;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
evm_protected_xattr(const char * req_xattr_name)234*4882a593Smuzhiyun static int evm_protected_xattr(const char *req_xattr_name)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	int namelen;
237*4882a593Smuzhiyun 	int found = 0;
238*4882a593Smuzhiyun 	struct xattr_list *xattr;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	namelen = strlen(req_xattr_name);
241*4882a593Smuzhiyun 	list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
242*4882a593Smuzhiyun 		if ((strlen(xattr->name) == namelen)
243*4882a593Smuzhiyun 		    && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
244*4882a593Smuzhiyun 			found = 1;
245*4882a593Smuzhiyun 			break;
246*4882a593Smuzhiyun 		}
247*4882a593Smuzhiyun 		if (strncmp(req_xattr_name,
248*4882a593Smuzhiyun 			    xattr->name + XATTR_SECURITY_PREFIX_LEN,
249*4882a593Smuzhiyun 			    strlen(req_xattr_name)) == 0) {
250*4882a593Smuzhiyun 			found = 1;
251*4882a593Smuzhiyun 			break;
252*4882a593Smuzhiyun 		}
253*4882a593Smuzhiyun 	}
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	return found;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun /**
259*4882a593Smuzhiyun  * evm_verifyxattr - verify the integrity of the requested xattr
260*4882a593Smuzhiyun  * @dentry: object of the verify xattr
261*4882a593Smuzhiyun  * @xattr_name: requested xattr
262*4882a593Smuzhiyun  * @xattr_value: requested xattr value
263*4882a593Smuzhiyun  * @xattr_value_len: requested xattr value length
264*4882a593Smuzhiyun  *
265*4882a593Smuzhiyun  * Calculate the HMAC for the given dentry and verify it against the stored
266*4882a593Smuzhiyun  * security.evm xattr. For performance, use the xattr value and length
267*4882a593Smuzhiyun  * previously retrieved to calculate the HMAC.
268*4882a593Smuzhiyun  *
269*4882a593Smuzhiyun  * Returns the xattr integrity status.
270*4882a593Smuzhiyun  *
271*4882a593Smuzhiyun  * This function requires the caller to lock the inode's i_mutex before it
272*4882a593Smuzhiyun  * is executed.
273*4882a593Smuzhiyun  */
evm_verifyxattr(struct dentry * dentry,const char * xattr_name,void * xattr_value,size_t xattr_value_len,struct integrity_iint_cache * iint)274*4882a593Smuzhiyun enum integrity_status evm_verifyxattr(struct dentry *dentry,
275*4882a593Smuzhiyun 				      const char *xattr_name,
276*4882a593Smuzhiyun 				      void *xattr_value, size_t xattr_value_len,
277*4882a593Smuzhiyun 				      struct integrity_iint_cache *iint)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
280*4882a593Smuzhiyun 		return INTEGRITY_UNKNOWN;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	if (!iint) {
283*4882a593Smuzhiyun 		iint = integrity_iint_find(d_backing_inode(dentry));
284*4882a593Smuzhiyun 		if (!iint)
285*4882a593Smuzhiyun 			return INTEGRITY_UNKNOWN;
286*4882a593Smuzhiyun 	}
287*4882a593Smuzhiyun 	return evm_verify_hmac(dentry, xattr_name, xattr_value,
288*4882a593Smuzhiyun 				 xattr_value_len, iint);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(evm_verifyxattr);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun /*
293*4882a593Smuzhiyun  * evm_verify_current_integrity - verify the dentry's metadata integrity
294*4882a593Smuzhiyun  * @dentry: pointer to the affected dentry
295*4882a593Smuzhiyun  *
296*4882a593Smuzhiyun  * Verify and return the dentry's metadata integrity. The exceptions are
297*4882a593Smuzhiyun  * before EVM is initialized or in 'fix' mode.
298*4882a593Smuzhiyun  */
evm_verify_current_integrity(struct dentry * dentry)299*4882a593Smuzhiyun static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	struct inode *inode = d_backing_inode(dentry);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
304*4882a593Smuzhiyun 		return 0;
305*4882a593Smuzhiyun 	return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun /*
309*4882a593Smuzhiyun  * evm_protect_xattr - protect the EVM extended attribute
310*4882a593Smuzhiyun  *
311*4882a593Smuzhiyun  * Prevent security.evm from being modified or removed without the
312*4882a593Smuzhiyun  * necessary permissions or when the existing value is invalid.
313*4882a593Smuzhiyun  *
314*4882a593Smuzhiyun  * The posix xattr acls are 'system' prefixed, which normally would not
315*4882a593Smuzhiyun  * affect security.evm.  An interesting side affect of writing posix xattr
316*4882a593Smuzhiyun  * acls is their modifying of the i_mode, which is included in security.evm.
317*4882a593Smuzhiyun  * For posix xattr acls only, permit security.evm, even if it currently
318*4882a593Smuzhiyun  * doesn't exist, to be updated unless the EVM signature is immutable.
319*4882a593Smuzhiyun  */
evm_protect_xattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)320*4882a593Smuzhiyun static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
321*4882a593Smuzhiyun 			     const void *xattr_value, size_t xattr_value_len)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun 	enum integrity_status evm_status;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
326*4882a593Smuzhiyun 		if (!capable(CAP_SYS_ADMIN))
327*4882a593Smuzhiyun 			return -EPERM;
328*4882a593Smuzhiyun 	} else if (!evm_protected_xattr(xattr_name)) {
329*4882a593Smuzhiyun 		if (!posix_xattr_acl(xattr_name))
330*4882a593Smuzhiyun 			return 0;
331*4882a593Smuzhiyun 		evm_status = evm_verify_current_integrity(dentry);
332*4882a593Smuzhiyun 		if ((evm_status == INTEGRITY_PASS) ||
333*4882a593Smuzhiyun 		    (evm_status == INTEGRITY_NOXATTRS))
334*4882a593Smuzhiyun 			return 0;
335*4882a593Smuzhiyun 		goto out;
336*4882a593Smuzhiyun 	}
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	evm_status = evm_verify_current_integrity(dentry);
339*4882a593Smuzhiyun 	if (evm_status == INTEGRITY_NOXATTRS) {
340*4882a593Smuzhiyun 		struct integrity_iint_cache *iint;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 		iint = integrity_iint_find(d_backing_inode(dentry));
343*4882a593Smuzhiyun 		if (iint && (iint->flags & IMA_NEW_FILE))
344*4882a593Smuzhiyun 			return 0;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 		/* exception for pseudo filesystems */
347*4882a593Smuzhiyun 		if (dentry->d_sb->s_magic == TMPFS_MAGIC
348*4882a593Smuzhiyun 		    || dentry->d_sb->s_magic == SYSFS_MAGIC)
349*4882a593Smuzhiyun 			return 0;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 		integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
352*4882a593Smuzhiyun 				    dentry->d_inode, dentry->d_name.name,
353*4882a593Smuzhiyun 				    "update_metadata",
354*4882a593Smuzhiyun 				    integrity_status_msg[evm_status],
355*4882a593Smuzhiyun 				    -EPERM, 0);
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun out:
358*4882a593Smuzhiyun 	if (evm_status != INTEGRITY_PASS)
359*4882a593Smuzhiyun 		integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
360*4882a593Smuzhiyun 				    dentry->d_name.name, "appraise_metadata",
361*4882a593Smuzhiyun 				    integrity_status_msg[evm_status],
362*4882a593Smuzhiyun 				    -EPERM, 0);
363*4882a593Smuzhiyun 	return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun /**
367*4882a593Smuzhiyun  * evm_inode_setxattr - protect the EVM extended attribute
368*4882a593Smuzhiyun  * @dentry: pointer to the affected dentry
369*4882a593Smuzhiyun  * @xattr_name: pointer to the affected extended attribute name
370*4882a593Smuzhiyun  * @xattr_value: pointer to the new extended attribute value
371*4882a593Smuzhiyun  * @xattr_value_len: pointer to the new extended attribute value length
372*4882a593Smuzhiyun  *
373*4882a593Smuzhiyun  * Before allowing the 'security.evm' protected xattr to be updated,
374*4882a593Smuzhiyun  * verify the existing value is valid.  As only the kernel should have
375*4882a593Smuzhiyun  * access to the EVM encrypted key needed to calculate the HMAC, prevent
376*4882a593Smuzhiyun  * userspace from writing HMAC value.  Writing 'security.evm' requires
377*4882a593Smuzhiyun  * requires CAP_SYS_ADMIN privileges.
378*4882a593Smuzhiyun  */
evm_inode_setxattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)379*4882a593Smuzhiyun int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
380*4882a593Smuzhiyun 		       const void *xattr_value, size_t xattr_value_len)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	const struct evm_ima_xattr_data *xattr_data = xattr_value;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	/* Policy permits modification of the protected xattrs even though
385*4882a593Smuzhiyun 	 * there's no HMAC key loaded
386*4882a593Smuzhiyun 	 */
387*4882a593Smuzhiyun 	if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
388*4882a593Smuzhiyun 		return 0;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
391*4882a593Smuzhiyun 		if (!xattr_value_len)
392*4882a593Smuzhiyun 			return -EINVAL;
393*4882a593Smuzhiyun 		if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
394*4882a593Smuzhiyun 		    xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
395*4882a593Smuzhiyun 			return -EPERM;
396*4882a593Smuzhiyun 	}
397*4882a593Smuzhiyun 	return evm_protect_xattr(dentry, xattr_name, xattr_value,
398*4882a593Smuzhiyun 				 xattr_value_len);
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun /**
402*4882a593Smuzhiyun  * evm_inode_removexattr - protect the EVM extended attribute
403*4882a593Smuzhiyun  * @dentry: pointer to the affected dentry
404*4882a593Smuzhiyun  * @xattr_name: pointer to the affected extended attribute name
405*4882a593Smuzhiyun  *
406*4882a593Smuzhiyun  * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
407*4882a593Smuzhiyun  * the current value is valid.
408*4882a593Smuzhiyun  */
evm_inode_removexattr(struct dentry * dentry,const char * xattr_name)409*4882a593Smuzhiyun int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	/* Policy permits modification of the protected xattrs even though
412*4882a593Smuzhiyun 	 * there's no HMAC key loaded
413*4882a593Smuzhiyun 	 */
414*4882a593Smuzhiyun 	if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
415*4882a593Smuzhiyun 		return 0;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	return evm_protect_xattr(dentry, xattr_name, NULL, 0);
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun 
evm_reset_status(struct inode * inode)420*4882a593Smuzhiyun static void evm_reset_status(struct inode *inode)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	struct integrity_iint_cache *iint;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	iint = integrity_iint_find(inode);
425*4882a593Smuzhiyun 	if (iint)
426*4882a593Smuzhiyun 		iint->evm_status = INTEGRITY_UNKNOWN;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun /**
430*4882a593Smuzhiyun  * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
431*4882a593Smuzhiyun  * @dentry: pointer to the affected dentry
432*4882a593Smuzhiyun  * @xattr_name: pointer to the affected extended attribute name
433*4882a593Smuzhiyun  * @xattr_value: pointer to the new extended attribute value
434*4882a593Smuzhiyun  * @xattr_value_len: pointer to the new extended attribute value length
435*4882a593Smuzhiyun  *
436*4882a593Smuzhiyun  * Update the HMAC stored in 'security.evm' to reflect the change.
437*4882a593Smuzhiyun  *
438*4882a593Smuzhiyun  * No need to take the i_mutex lock here, as this function is called from
439*4882a593Smuzhiyun  * __vfs_setxattr_noperm().  The caller of which has taken the inode's
440*4882a593Smuzhiyun  * i_mutex lock.
441*4882a593Smuzhiyun  */
evm_inode_post_setxattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)442*4882a593Smuzhiyun void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
443*4882a593Smuzhiyun 			     const void *xattr_value, size_t xattr_value_len)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name)
446*4882a593Smuzhiyun 				  && !posix_xattr_acl(xattr_name)))
447*4882a593Smuzhiyun 		return;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	evm_reset_status(dentry->d_inode);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun /**
455*4882a593Smuzhiyun  * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
456*4882a593Smuzhiyun  * @dentry: pointer to the affected dentry
457*4882a593Smuzhiyun  * @xattr_name: pointer to the affected extended attribute name
458*4882a593Smuzhiyun  *
459*4882a593Smuzhiyun  * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
460*4882a593Smuzhiyun  *
461*4882a593Smuzhiyun  * No need to take the i_mutex lock here, as this function is called from
462*4882a593Smuzhiyun  * vfs_removexattr() which takes the i_mutex.
463*4882a593Smuzhiyun  */
evm_inode_post_removexattr(struct dentry * dentry,const char * xattr_name)464*4882a593Smuzhiyun void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun 	if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
467*4882a593Smuzhiyun 		return;
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	evm_reset_status(dentry->d_inode);
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	evm_update_evmxattr(dentry, xattr_name, NULL, 0);
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun /**
475*4882a593Smuzhiyun  * evm_inode_setattr - prevent updating an invalid EVM extended attribute
476*4882a593Smuzhiyun  * @dentry: pointer to the affected dentry
477*4882a593Smuzhiyun  *
478*4882a593Smuzhiyun  * Permit update of file attributes when files have a valid EVM signature,
479*4882a593Smuzhiyun  * except in the case of them having an immutable portable signature.
480*4882a593Smuzhiyun  */
evm_inode_setattr(struct dentry * dentry,struct iattr * attr)481*4882a593Smuzhiyun int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	unsigned int ia_valid = attr->ia_valid;
484*4882a593Smuzhiyun 	enum integrity_status evm_status;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	/* Policy permits modification of the protected attrs even though
487*4882a593Smuzhiyun 	 * there's no HMAC key loaded
488*4882a593Smuzhiyun 	 */
489*4882a593Smuzhiyun 	if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
490*4882a593Smuzhiyun 		return 0;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
493*4882a593Smuzhiyun 		return 0;
494*4882a593Smuzhiyun 	evm_status = evm_verify_current_integrity(dentry);
495*4882a593Smuzhiyun 	if ((evm_status == INTEGRITY_PASS) ||
496*4882a593Smuzhiyun 	    (evm_status == INTEGRITY_NOXATTRS))
497*4882a593Smuzhiyun 		return 0;
498*4882a593Smuzhiyun 	integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
499*4882a593Smuzhiyun 			    dentry->d_name.name, "appraise_metadata",
500*4882a593Smuzhiyun 			    integrity_status_msg[evm_status], -EPERM, 0);
501*4882a593Smuzhiyun 	return -EPERM;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun /**
505*4882a593Smuzhiyun  * evm_inode_post_setattr - update 'security.evm' after modifying metadata
506*4882a593Smuzhiyun  * @dentry: pointer to the affected dentry
507*4882a593Smuzhiyun  * @ia_valid: for the UID and GID status
508*4882a593Smuzhiyun  *
509*4882a593Smuzhiyun  * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
510*4882a593Smuzhiyun  * changes.
511*4882a593Smuzhiyun  *
512*4882a593Smuzhiyun  * This function is called from notify_change(), which expects the caller
513*4882a593Smuzhiyun  * to lock the inode's i_mutex.
514*4882a593Smuzhiyun  */
evm_inode_post_setattr(struct dentry * dentry,int ia_valid)515*4882a593Smuzhiyun void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun 	if (!evm_key_loaded())
518*4882a593Smuzhiyun 		return;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
521*4882a593Smuzhiyun 		evm_update_evmxattr(dentry, NULL, NULL, 0);
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun /*
525*4882a593Smuzhiyun  * evm_inode_init_security - initializes security.evm HMAC value
526*4882a593Smuzhiyun  */
evm_inode_init_security(struct inode * inode,const struct xattr * lsm_xattr,struct xattr * evm_xattr)527*4882a593Smuzhiyun int evm_inode_init_security(struct inode *inode,
528*4882a593Smuzhiyun 				 const struct xattr *lsm_xattr,
529*4882a593Smuzhiyun 				 struct xattr *evm_xattr)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun 	struct evm_xattr *xattr_data;
532*4882a593Smuzhiyun 	int rc;
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	if (!(evm_initialized & EVM_INIT_HMAC) ||
535*4882a593Smuzhiyun 	    !evm_protected_xattr(lsm_xattr->name))
536*4882a593Smuzhiyun 		return 0;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
539*4882a593Smuzhiyun 	if (!xattr_data)
540*4882a593Smuzhiyun 		return -ENOMEM;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	xattr_data->data.type = EVM_XATTR_HMAC;
543*4882a593Smuzhiyun 	rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
544*4882a593Smuzhiyun 	if (rc < 0)
545*4882a593Smuzhiyun 		goto out;
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	evm_xattr->value = xattr_data;
548*4882a593Smuzhiyun 	evm_xattr->value_len = sizeof(*xattr_data);
549*4882a593Smuzhiyun 	evm_xattr->name = XATTR_EVM_SUFFIX;
550*4882a593Smuzhiyun 	return 0;
551*4882a593Smuzhiyun out:
552*4882a593Smuzhiyun 	kfree(xattr_data);
553*4882a593Smuzhiyun 	return rc;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(evm_inode_init_security);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun #ifdef CONFIG_EVM_LOAD_X509
evm_load_x509(void)558*4882a593Smuzhiyun void __init evm_load_x509(void)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	int rc;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH);
563*4882a593Smuzhiyun 	if (!rc)
564*4882a593Smuzhiyun 		evm_initialized |= EVM_INIT_X509;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun #endif
567*4882a593Smuzhiyun 
init_evm(void)568*4882a593Smuzhiyun static int __init init_evm(void)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun 	int error;
571*4882a593Smuzhiyun 	struct list_head *pos, *q;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	evm_init_config();
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	error = integrity_init_keyring(INTEGRITY_KEYRING_EVM);
576*4882a593Smuzhiyun 	if (error)
577*4882a593Smuzhiyun 		goto error;
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	error = evm_init_secfs();
580*4882a593Smuzhiyun 	if (error < 0) {
581*4882a593Smuzhiyun 		pr_info("Error registering secfs\n");
582*4882a593Smuzhiyun 		goto error;
583*4882a593Smuzhiyun 	}
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun error:
586*4882a593Smuzhiyun 	if (error != 0) {
587*4882a593Smuzhiyun 		if (!list_empty(&evm_config_xattrnames)) {
588*4882a593Smuzhiyun 			list_for_each_safe(pos, q, &evm_config_xattrnames)
589*4882a593Smuzhiyun 				list_del(pos);
590*4882a593Smuzhiyun 		}
591*4882a593Smuzhiyun 	}
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	return error;
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun late_initcall(init_evm);
597