xref: /OK3568_Linux_fs/kernel/security/integrity/evm/evm_secfs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2010 IBM Corporation
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Authors:
6*4882a593Smuzhiyun  * Mimi Zohar <zohar@us.ibm.com>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * File: evm_secfs.c
9*4882a593Smuzhiyun  *	- Used to signal when key is on keyring
10*4882a593Smuzhiyun  *	- Get the key and enable EVM
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/audit.h>
14*4882a593Smuzhiyun #include <linux/uaccess.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <linux/mutex.h>
17*4882a593Smuzhiyun #include "evm.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun static struct dentry *evm_dir;
20*4882a593Smuzhiyun static struct dentry *evm_init_tpm;
21*4882a593Smuzhiyun static struct dentry *evm_symlink;
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #ifdef CONFIG_EVM_ADD_XATTRS
24*4882a593Smuzhiyun static struct dentry *evm_xattrs;
25*4882a593Smuzhiyun static DEFINE_MUTEX(xattr_list_mutex);
26*4882a593Smuzhiyun static int evm_xattrs_locked;
27*4882a593Smuzhiyun #endif
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun  * evm_read_key - read() for <securityfs>/evm
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * @filp: file pointer, not actually used
33*4882a593Smuzhiyun  * @buf: where to put the result
34*4882a593Smuzhiyun  * @count: maximum to send along
35*4882a593Smuzhiyun  * @ppos: where to start
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * Returns number of bytes read or error code, as appropriate
38*4882a593Smuzhiyun  */
evm_read_key(struct file * filp,char __user * buf,size_t count,loff_t * ppos)39*4882a593Smuzhiyun static ssize_t evm_read_key(struct file *filp, char __user *buf,
40*4882a593Smuzhiyun 			    size_t count, loff_t *ppos)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	char temp[80];
43*4882a593Smuzhiyun 	ssize_t rc;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	if (*ppos != 0)
46*4882a593Smuzhiyun 		return 0;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	sprintf(temp, "%d", (evm_initialized & ~EVM_SETUP_COMPLETE));
49*4882a593Smuzhiyun 	rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	return rc;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun  * evm_write_key - write() for <securityfs>/evm
56*4882a593Smuzhiyun  * @file: file pointer, not actually used
57*4882a593Smuzhiyun  * @buf: where to get the data from
58*4882a593Smuzhiyun  * @count: bytes sent
59*4882a593Smuzhiyun  * @ppos: where to start
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * Used to signal that key is on the kernel key ring.
62*4882a593Smuzhiyun  * - get the integrity hmac key from the kernel key ring
63*4882a593Smuzhiyun  * - create list of hmac protected extended attributes
64*4882a593Smuzhiyun  * Returns number of bytes written or error code, as appropriate
65*4882a593Smuzhiyun  */
evm_write_key(struct file * file,const char __user * buf,size_t count,loff_t * ppos)66*4882a593Smuzhiyun static ssize_t evm_write_key(struct file *file, const char __user *buf,
67*4882a593Smuzhiyun 			     size_t count, loff_t *ppos)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	unsigned int i;
70*4882a593Smuzhiyun 	int ret;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP_COMPLETE))
73*4882a593Smuzhiyun 		return -EPERM;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	ret = kstrtouint_from_user(buf, count, 0, &i);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (ret)
78*4882a593Smuzhiyun 		return ret;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	/* Reject invalid values */
81*4882a593Smuzhiyun 	if (!i || (i & ~EVM_INIT_MASK) != 0)
82*4882a593Smuzhiyun 		return -EINVAL;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	/*
85*4882a593Smuzhiyun 	 * Don't allow a request to enable metadata writes if
86*4882a593Smuzhiyun 	 * an HMAC key is loaded.
87*4882a593Smuzhiyun 	 */
88*4882a593Smuzhiyun 	if ((i & EVM_ALLOW_METADATA_WRITES) &&
89*4882a593Smuzhiyun 	    (evm_initialized & EVM_INIT_HMAC) != 0)
90*4882a593Smuzhiyun 		return -EPERM;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	if (i & EVM_INIT_HMAC) {
93*4882a593Smuzhiyun 		ret = evm_init_key();
94*4882a593Smuzhiyun 		if (ret != 0)
95*4882a593Smuzhiyun 			return ret;
96*4882a593Smuzhiyun 		/* Forbid further writes after the symmetric key is loaded */
97*4882a593Smuzhiyun 		i |= EVM_SETUP_COMPLETE;
98*4882a593Smuzhiyun 	}
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	evm_initialized |= i;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* Don't allow protected metadata modification if a symmetric key
103*4882a593Smuzhiyun 	 * is loaded
104*4882a593Smuzhiyun 	 */
105*4882a593Smuzhiyun 	if (evm_initialized & EVM_INIT_HMAC)
106*4882a593Smuzhiyun 		evm_initialized &= ~(EVM_ALLOW_METADATA_WRITES);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	return count;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun static const struct file_operations evm_key_ops = {
112*4882a593Smuzhiyun 	.read		= evm_read_key,
113*4882a593Smuzhiyun 	.write		= evm_write_key,
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun #ifdef CONFIG_EVM_ADD_XATTRS
117*4882a593Smuzhiyun /**
118*4882a593Smuzhiyun  * evm_read_xattrs - read() for <securityfs>/evm_xattrs
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * @filp: file pointer, not actually used
121*4882a593Smuzhiyun  * @buf: where to put the result
122*4882a593Smuzhiyun  * @count: maximum to send along
123*4882a593Smuzhiyun  * @ppos: where to start
124*4882a593Smuzhiyun  *
125*4882a593Smuzhiyun  * Returns number of bytes read or error code, as appropriate
126*4882a593Smuzhiyun  */
evm_read_xattrs(struct file * filp,char __user * buf,size_t count,loff_t * ppos)127*4882a593Smuzhiyun static ssize_t evm_read_xattrs(struct file *filp, char __user *buf,
128*4882a593Smuzhiyun 			       size_t count, loff_t *ppos)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	char *temp;
131*4882a593Smuzhiyun 	int offset = 0;
132*4882a593Smuzhiyun 	ssize_t rc, size = 0;
133*4882a593Smuzhiyun 	struct xattr_list *xattr;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	if (*ppos != 0)
136*4882a593Smuzhiyun 		return 0;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	rc = mutex_lock_interruptible(&xattr_list_mutex);
139*4882a593Smuzhiyun 	if (rc)
140*4882a593Smuzhiyun 		return -ERESTARTSYS;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	list_for_each_entry(xattr, &evm_config_xattrnames, list)
143*4882a593Smuzhiyun 		size += strlen(xattr->name) + 1;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	temp = kmalloc(size + 1, GFP_KERNEL);
146*4882a593Smuzhiyun 	if (!temp) {
147*4882a593Smuzhiyun 		mutex_unlock(&xattr_list_mutex);
148*4882a593Smuzhiyun 		return -ENOMEM;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	list_for_each_entry(xattr, &evm_config_xattrnames, list) {
152*4882a593Smuzhiyun 		sprintf(temp + offset, "%s\n", xattr->name);
153*4882a593Smuzhiyun 		offset += strlen(xattr->name) + 1;
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	mutex_unlock(&xattr_list_mutex);
157*4882a593Smuzhiyun 	rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	kfree(temp);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	return rc;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /**
165*4882a593Smuzhiyun  * evm_write_xattrs - write() for <securityfs>/evm_xattrs
166*4882a593Smuzhiyun  * @file: file pointer, not actually used
167*4882a593Smuzhiyun  * @buf: where to get the data from
168*4882a593Smuzhiyun  * @count: bytes sent
169*4882a593Smuzhiyun  * @ppos: where to start
170*4882a593Smuzhiyun  *
171*4882a593Smuzhiyun  * Returns number of bytes written or error code, as appropriate
172*4882a593Smuzhiyun  */
evm_write_xattrs(struct file * file,const char __user * buf,size_t count,loff_t * ppos)173*4882a593Smuzhiyun static ssize_t evm_write_xattrs(struct file *file, const char __user *buf,
174*4882a593Smuzhiyun 				size_t count, loff_t *ppos)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	int len, err;
177*4882a593Smuzhiyun 	struct xattr_list *xattr, *tmp;
178*4882a593Smuzhiyun 	struct audit_buffer *ab;
179*4882a593Smuzhiyun 	struct iattr newattrs;
180*4882a593Smuzhiyun 	struct inode *inode;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN) || evm_xattrs_locked)
183*4882a593Smuzhiyun 		return -EPERM;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (*ppos != 0)
186*4882a593Smuzhiyun 		return -EINVAL;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	if (count > XATTR_NAME_MAX)
189*4882a593Smuzhiyun 		return -E2BIG;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	ab = audit_log_start(audit_context(), GFP_KERNEL,
192*4882a593Smuzhiyun 			     AUDIT_INTEGRITY_EVM_XATTR);
193*4882a593Smuzhiyun 	if (!ab)
194*4882a593Smuzhiyun 		return -ENOMEM;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	xattr = kmalloc(sizeof(struct xattr_list), GFP_KERNEL);
197*4882a593Smuzhiyun 	if (!xattr) {
198*4882a593Smuzhiyun 		err = -ENOMEM;
199*4882a593Smuzhiyun 		goto out;
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	xattr->name = memdup_user_nul(buf, count);
203*4882a593Smuzhiyun 	if (IS_ERR(xattr->name)) {
204*4882a593Smuzhiyun 		err = PTR_ERR(xattr->name);
205*4882a593Smuzhiyun 		xattr->name = NULL;
206*4882a593Smuzhiyun 		goto out;
207*4882a593Smuzhiyun 	}
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/* Remove any trailing newline */
210*4882a593Smuzhiyun 	len = strlen(xattr->name);
211*4882a593Smuzhiyun 	if (len && xattr->name[len-1] == '\n')
212*4882a593Smuzhiyun 		xattr->name[len-1] = '\0';
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	audit_log_format(ab, "xattr=");
215*4882a593Smuzhiyun 	audit_log_untrustedstring(ab, xattr->name);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	if (strcmp(xattr->name, ".") == 0) {
218*4882a593Smuzhiyun 		evm_xattrs_locked = 1;
219*4882a593Smuzhiyun 		newattrs.ia_mode = S_IFREG | 0440;
220*4882a593Smuzhiyun 		newattrs.ia_valid = ATTR_MODE;
221*4882a593Smuzhiyun 		inode = evm_xattrs->d_inode;
222*4882a593Smuzhiyun 		inode_lock(inode);
223*4882a593Smuzhiyun 		err = simple_setattr(evm_xattrs, &newattrs);
224*4882a593Smuzhiyun 		inode_unlock(inode);
225*4882a593Smuzhiyun 		if (!err)
226*4882a593Smuzhiyun 			err = count;
227*4882a593Smuzhiyun 		goto out;
228*4882a593Smuzhiyun 	}
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	if (strncmp(xattr->name, XATTR_SECURITY_PREFIX,
231*4882a593Smuzhiyun 		    XATTR_SECURITY_PREFIX_LEN) != 0) {
232*4882a593Smuzhiyun 		err = -EINVAL;
233*4882a593Smuzhiyun 		goto out;
234*4882a593Smuzhiyun 	}
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	/*
237*4882a593Smuzhiyun 	 * xattr_list_mutex guards against races in evm_read_xattrs().
238*4882a593Smuzhiyun 	 * Entries are only added to the evm_config_xattrnames list
239*4882a593Smuzhiyun 	 * and never deleted. Therefore, the list is traversed
240*4882a593Smuzhiyun 	 * using list_for_each_entry_lockless() without holding
241*4882a593Smuzhiyun 	 * the mutex in evm_calc_hmac_or_hash(), evm_find_protected_xattrs()
242*4882a593Smuzhiyun 	 * and evm_protected_xattr().
243*4882a593Smuzhiyun 	 */
244*4882a593Smuzhiyun 	mutex_lock(&xattr_list_mutex);
245*4882a593Smuzhiyun 	list_for_each_entry(tmp, &evm_config_xattrnames, list) {
246*4882a593Smuzhiyun 		if (strcmp(xattr->name, tmp->name) == 0) {
247*4882a593Smuzhiyun 			err = -EEXIST;
248*4882a593Smuzhiyun 			mutex_unlock(&xattr_list_mutex);
249*4882a593Smuzhiyun 			goto out;
250*4882a593Smuzhiyun 		}
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun 	list_add_tail_rcu(&xattr->list, &evm_config_xattrnames);
253*4882a593Smuzhiyun 	mutex_unlock(&xattr_list_mutex);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	audit_log_format(ab, " res=0");
256*4882a593Smuzhiyun 	audit_log_end(ab);
257*4882a593Smuzhiyun 	return count;
258*4882a593Smuzhiyun out:
259*4882a593Smuzhiyun 	audit_log_format(ab, " res=%d", err);
260*4882a593Smuzhiyun 	audit_log_end(ab);
261*4882a593Smuzhiyun 	if (xattr) {
262*4882a593Smuzhiyun 		kfree(xattr->name);
263*4882a593Smuzhiyun 		kfree(xattr);
264*4882a593Smuzhiyun 	}
265*4882a593Smuzhiyun 	return err;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun static const struct file_operations evm_xattr_ops = {
269*4882a593Smuzhiyun 	.read		= evm_read_xattrs,
270*4882a593Smuzhiyun 	.write		= evm_write_xattrs,
271*4882a593Smuzhiyun };
272*4882a593Smuzhiyun 
evm_init_xattrs(void)273*4882a593Smuzhiyun static int evm_init_xattrs(void)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun 	evm_xattrs = securityfs_create_file("evm_xattrs", 0660, evm_dir, NULL,
276*4882a593Smuzhiyun 					    &evm_xattr_ops);
277*4882a593Smuzhiyun 	if (!evm_xattrs || IS_ERR(evm_xattrs))
278*4882a593Smuzhiyun 		return -EFAULT;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	return 0;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun #else
evm_init_xattrs(void)283*4882a593Smuzhiyun static int evm_init_xattrs(void)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	return 0;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun #endif
288*4882a593Smuzhiyun 
evm_init_secfs(void)289*4882a593Smuzhiyun int __init evm_init_secfs(void)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	int error = 0;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	evm_dir = securityfs_create_dir("evm", integrity_dir);
294*4882a593Smuzhiyun 	if (!evm_dir || IS_ERR(evm_dir))
295*4882a593Smuzhiyun 		return -EFAULT;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	evm_init_tpm = securityfs_create_file("evm", 0660,
298*4882a593Smuzhiyun 					      evm_dir, NULL, &evm_key_ops);
299*4882a593Smuzhiyun 	if (!evm_init_tpm || IS_ERR(evm_init_tpm)) {
300*4882a593Smuzhiyun 		error = -EFAULT;
301*4882a593Smuzhiyun 		goto out;
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	evm_symlink = securityfs_create_symlink("evm", NULL,
305*4882a593Smuzhiyun 						"integrity/evm/evm", NULL);
306*4882a593Smuzhiyun 	if (!evm_symlink || IS_ERR(evm_symlink)) {
307*4882a593Smuzhiyun 		error = -EFAULT;
308*4882a593Smuzhiyun 		goto out;
309*4882a593Smuzhiyun 	}
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	if (evm_init_xattrs() != 0) {
312*4882a593Smuzhiyun 		error = -EFAULT;
313*4882a593Smuzhiyun 		goto out;
314*4882a593Smuzhiyun 	}
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	return 0;
317*4882a593Smuzhiyun out:
318*4882a593Smuzhiyun 	securityfs_remove(evm_symlink);
319*4882a593Smuzhiyun 	securityfs_remove(evm_init_tpm);
320*4882a593Smuzhiyun 	securityfs_remove(evm_dir);
321*4882a593Smuzhiyun 	return error;
322*4882a593Smuzhiyun }
323