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