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) {
146 size += strlen(xattr->name) + 1;
149 temp = kmalloc(size + 1, GFP_KERNEL);
151 mutex_unlock(&xattr_list_mutex);
155 list_for_each_entry(xattr, &evm_config_xattrnames, list) {
159 sprintf(temp + offset, "%s\n", xattr->name);
160 offset += strlen(xattr->name) + 1;
163 mutex_unlock(&xattr_list_mutex);
164 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
172 * evm_write_xattrs - write() for <securityfs>/evm_xattrs
173 * @file: file pointer, not actually used
174 * @buf: where to get the data from
176 * @ppos: where to start
178 * Returns number of bytes written or error code, as appropriate
180 static ssize_t evm_write_xattrs(struct file *file, const char __user *buf,
181 size_t count, loff_t *ppos)
184 struct xattr_list *xattr, *tmp;
185 struct audit_buffer *ab;
186 struct iattr newattrs;
189 if (!capable(CAP_SYS_ADMIN) || evm_xattrs_locked)
195 if (count > XATTR_NAME_MAX)
198 ab = audit_log_start(audit_context(), GFP_KERNEL,
199 AUDIT_INTEGRITY_EVM_XATTR);
200 if (!ab && IS_ENABLED(CONFIG_AUDIT))
203 xattr = kmalloc(sizeof(struct xattr_list), GFP_KERNEL);
209 xattr->enabled = true;
210 xattr->name = memdup_user_nul(buf, count);
211 if (IS_ERR(xattr->name)) {
212 err = PTR_ERR(xattr->name);
217 /* Remove any trailing newline */
218 len = strlen(xattr->name);
219 if (len && xattr->name[len-1] == '\n')
220 xattr->name[len-1] = '\0';
222 audit_log_format(ab, "xattr=");
223 audit_log_untrustedstring(ab, xattr->name);
225 if (strcmp(xattr->name, ".") == 0) {
226 evm_xattrs_locked = 1;
227 newattrs.ia_mode = S_IFREG | 0440;
228 newattrs.ia_valid = ATTR_MODE;
229 inode = evm_xattrs->d_inode;
231 err = simple_setattr(&nop_mnt_idmap, evm_xattrs, &newattrs);
238 if (strncmp(xattr->name, XATTR_SECURITY_PREFIX,
239 XATTR_SECURITY_PREFIX_LEN) != 0) {
245 * xattr_list_mutex guards against races in evm_read_xattrs().
246 * Entries are only added to the evm_config_xattrnames list
247 * and never deleted. Therefore, the list is traversed
248 * using list_for_each_entry_lockless() without holding
249 * the mutex in evm_calc_hmac_or_hash(), evm_find_protected_xattrs()
250 * and evm_protected_xattr().
252 mutex_lock(&xattr_list_mutex);
253 list_for_each_entry(tmp, &evm_config_xattrnames, list) {
254 if (strcmp(xattr->name, tmp->name) == 0) {
260 mutex_unlock(&xattr_list_mutex);
264 list_add_tail_rcu(&xattr->list, &evm_config_xattrnames);
265 mutex_unlock(&xattr_list_mutex);
267 audit_log_format(ab, " res=0");
271 audit_log_format(ab, " res=%d", (err < 0) ? err : 0);
280 static const struct file_operations evm_xattr_ops = {
281 .read = evm_read_xattrs,
282 .write = evm_write_xattrs,
285 static int evm_init_xattrs(void)
287 evm_xattrs = securityfs_create_file("evm_xattrs", 0660, evm_dir, NULL,
289 if (!evm_xattrs || IS_ERR(evm_xattrs))
295 static int evm_init_xattrs(void)
301 int __init evm_init_secfs(void)
305 evm_dir = securityfs_create_dir("evm", integrity_dir);
306 if (!evm_dir || IS_ERR(evm_dir))
309 evm_init_tpm = securityfs_create_file("evm", 0660,
310 evm_dir, NULL, &evm_key_ops);
311 if (!evm_init_tpm || IS_ERR(evm_init_tpm)) {
316 evm_symlink = securityfs_create_symlink("evm", NULL,
317 "integrity/evm/evm", NULL);
318 if (!evm_symlink || IS_ERR(evm_symlink)) {
323 if (evm_init_xattrs() != 0) {
330 securityfs_remove(evm_symlink);
331 securityfs_remove(evm_init_tpm);
332 securityfs_remove(evm_dir);