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