1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010 IBM Corporation
6 * Mimi Zohar <zohar@us.ibm.com>
9 * - Used to signal when key is on keyring
10 * - Get the key and enable EVM
13 #include <linux/audit.h>
14 #include <linux/uaccess.h>
15 #include <linux/init.h>
16 #include <linux/mutex.h>
19 static struct dentry *evm_dir;
20 static struct dentry *evm_init_tpm;
21 static struct dentry *evm_symlink;
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;
30 * evm_read_key - read() for <securityfs>/evm
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
37 * Returns number of bytes read or error code, as appropriate
39 static ssize_t evm_read_key(struct file *filp, char __user *buf,
40 size_t count, loff_t *ppos)
48 sprintf(temp, "%d", (evm_initialized & ~EVM_SETUP_COMPLETE));
49 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
55 * evm_write_key - write() for <securityfs>/evm
56 * @file: file pointer, not actually used
57 * @buf: where to get the data from
59 * @ppos: where to start
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
66 static ssize_t evm_write_key(struct file *file, const char __user *buf,
67 size_t count, loff_t *ppos)
72 if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP_COMPLETE))
75 ret = kstrtouint_from_user(buf, count, 0, &i);
80 /* Reject invalid values */
81 if (!i || (i & ~EVM_INIT_MASK) != 0)
85 * Don't allow a request to enable metadata writes if
86 * an HMAC key is loaded.
88 if ((i & EVM_ALLOW_METADATA_WRITES) &&
89 (evm_initialized & EVM_INIT_HMAC) != 0)
92 if (i & EVM_INIT_HMAC) {
96 /* Forbid further writes after the symmetric key is loaded */
97 i |= EVM_SETUP_COMPLETE;
100 evm_initialized |= i;
102 /* Don't allow protected metadata modification if a symmetric key
105 if (evm_initialized & EVM_INIT_HMAC)
106 evm_initialized &= ~(EVM_ALLOW_METADATA_WRITES);
111 static const struct file_operations evm_key_ops = {
112 .read = evm_read_key,
113 .write = evm_write_key,
116 #ifdef CONFIG_EVM_ADD_XATTRS
118 * evm_read_xattrs - read() for <securityfs>/evm_xattrs
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
125 * Returns number of bytes read or error code, as appropriate
127 static ssize_t evm_read_xattrs(struct file *filp, char __user *buf,
128 size_t count, loff_t *ppos)
132 ssize_t rc, size = 0;
133 struct xattr_list *xattr;
138 rc = mutex_lock_interruptible(&xattr_list_mutex);
142 list_for_each_entry(xattr, &evm_config_xattrnames, list)
143 size += strlen(xattr->name) + 1;
145 temp = kmalloc(size + 1, GFP_KERNEL);
147 mutex_unlock(&xattr_list_mutex);
151 list_for_each_entry(xattr, &evm_config_xattrnames, list) {
152 sprintf(temp + offset, "%s\n", xattr->name);
153 offset += strlen(xattr->name) + 1;
156 mutex_unlock(&xattr_list_mutex);
157 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
165 * evm_write_xattrs - write() for <securityfs>/evm_xattrs
166 * @file: file pointer, not actually used
167 * @buf: where to get the data from
169 * @ppos: where to start
171 * Returns number of bytes written or error code, as appropriate
173 static ssize_t evm_write_xattrs(struct file *file, const char __user *buf,
174 size_t count, loff_t *ppos)
177 struct xattr_list *xattr, *tmp;
178 struct audit_buffer *ab;
179 struct iattr newattrs;
182 if (!capable(CAP_SYS_ADMIN) || evm_xattrs_locked)
188 if (count > XATTR_NAME_MAX)
191 ab = audit_log_start(audit_context(), GFP_KERNEL,
192 AUDIT_INTEGRITY_EVM_XATTR);
196 xattr = kmalloc(sizeof(struct xattr_list), GFP_KERNEL);
202 xattr->name = memdup_user_nul(buf, count);
203 if (IS_ERR(xattr->name)) {
204 err = PTR_ERR(xattr->name);
209 /* Remove any trailing newline */
210 len = strlen(xattr->name);
211 if (len && xattr->name[len-1] == '\n')
212 xattr->name[len-1] = '\0';
214 audit_log_format(ab, "xattr=");
215 audit_log_untrustedstring(ab, xattr->name);
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;
223 err = simple_setattr(evm_xattrs, &newattrs);
230 if (strncmp(xattr->name, XATTR_SECURITY_PREFIX,
231 XATTR_SECURITY_PREFIX_LEN) != 0) {
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().
244 mutex_lock(&xattr_list_mutex);
245 list_for_each_entry(tmp, &evm_config_xattrnames, list) {
246 if (strcmp(xattr->name, tmp->name) == 0) {
248 mutex_unlock(&xattr_list_mutex);
252 list_add_tail_rcu(&xattr->list, &evm_config_xattrnames);
253 mutex_unlock(&xattr_list_mutex);
255 audit_log_format(ab, " res=0");
259 audit_log_format(ab, " res=%d", err);
268 static const struct file_operations evm_xattr_ops = {
269 .read = evm_read_xattrs,
270 .write = evm_write_xattrs,
273 static int evm_init_xattrs(void)
275 evm_xattrs = securityfs_create_file("evm_xattrs", 0660, evm_dir, NULL,
277 if (!evm_xattrs || IS_ERR(evm_xattrs))
283 static int evm_init_xattrs(void)
289 int __init evm_init_secfs(void)
293 evm_dir = securityfs_create_dir("evm", integrity_dir);
294 if (!evm_dir || IS_ERR(evm_dir))
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)) {
304 evm_symlink = securityfs_create_symlink("evm", NULL,
305 "integrity/evm/evm", NULL);
306 if (!evm_symlink || IS_ERR(evm_symlink)) {
311 if (evm_init_xattrs() != 0) {
318 securityfs_remove(evm_symlink);
319 securityfs_remove(evm_init_tpm);
320 securityfs_remove(evm_dir);