This source file includes following definitions.
- evm_read_key
- evm_write_key
- evm_read_xattrs
- evm_write_xattrs
- evm_init_xattrs
- evm_init_xattrs
- evm_init_secfs
1
2
3
4
5
6
7
8
9
10
11
12
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/audit.h>
16 #include <linux/uaccess.h>
17 #include <linux/init.h>
18 #include <linux/mutex.h>
19 #include "evm.h"
20
21 static struct dentry *evm_dir;
22 static struct dentry *evm_init_tpm;
23 static struct dentry *evm_symlink;
24
25 #ifdef CONFIG_EVM_ADD_XATTRS
26 static struct dentry *evm_xattrs;
27 static DEFINE_MUTEX(xattr_list_mutex);
28 static int evm_xattrs_locked;
29 #endif
30
31
32
33
34
35
36
37
38
39
40
41 static ssize_t evm_read_key(struct file *filp, char __user *buf,
42 size_t count, loff_t *ppos)
43 {
44 char temp[80];
45 ssize_t rc;
46
47 if (*ppos != 0)
48 return 0;
49
50 sprintf(temp, "%d", (evm_initialized & ~EVM_SETUP_COMPLETE));
51 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
52
53 return rc;
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68 static ssize_t evm_write_key(struct file *file, const char __user *buf,
69 size_t count, loff_t *ppos)
70 {
71 int i, ret;
72
73 if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP_COMPLETE))
74 return -EPERM;
75
76 ret = kstrtoint_from_user(buf, count, 0, &i);
77
78 if (ret)
79 return ret;
80
81
82 if (!i || (i & ~EVM_INIT_MASK) != 0)
83 return -EINVAL;
84
85
86
87
88 if ((i & EVM_ALLOW_METADATA_WRITES) &&
89 ((evm_initialized & EVM_KEY_MASK) != 0) &&
90 !(evm_initialized & EVM_ALLOW_METADATA_WRITES))
91 return -EPERM;
92
93 if (i & EVM_INIT_HMAC) {
94 ret = evm_init_key();
95 if (ret != 0)
96 return ret;
97
98 i |= EVM_SETUP_COMPLETE;
99 }
100
101 evm_initialized |= i;
102
103
104
105
106 if (evm_initialized & EVM_INIT_HMAC)
107 evm_initialized &= ~(EVM_ALLOW_METADATA_WRITES);
108
109 return count;
110 }
111
112 static const struct file_operations evm_key_ops = {
113 .read = evm_read_key,
114 .write = evm_write_key,
115 };
116
117 #ifdef CONFIG_EVM_ADD_XATTRS
118
119
120
121
122
123
124
125
126
127
128 static ssize_t evm_read_xattrs(struct file *filp, char __user *buf,
129 size_t count, loff_t *ppos)
130 {
131 char *temp;
132 int offset = 0;
133 ssize_t rc, size = 0;
134 struct xattr_list *xattr;
135
136 if (*ppos != 0)
137 return 0;
138
139 rc = mutex_lock_interruptible(&xattr_list_mutex);
140 if (rc)
141 return -ERESTARTSYS;
142
143 list_for_each_entry(xattr, &evm_config_xattrnames, list)
144 size += strlen(xattr->name) + 1;
145
146 temp = kmalloc(size + 1, GFP_KERNEL);
147 if (!temp) {
148 mutex_unlock(&xattr_list_mutex);
149 return -ENOMEM;
150 }
151
152 list_for_each_entry(xattr, &evm_config_xattrnames, list) {
153 sprintf(temp + offset, "%s\n", xattr->name);
154 offset += strlen(xattr->name) + 1;
155 }
156
157 mutex_unlock(&xattr_list_mutex);
158 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
159
160 kfree(temp);
161
162 return rc;
163 }
164
165
166
167
168
169
170
171
172
173
174 static ssize_t evm_write_xattrs(struct file *file, const char __user *buf,
175 size_t count, loff_t *ppos)
176 {
177 int len, err;
178 struct xattr_list *xattr, *tmp;
179 struct audit_buffer *ab;
180 struct iattr newattrs;
181 struct inode *inode;
182
183 if (!capable(CAP_SYS_ADMIN) || evm_xattrs_locked)
184 return -EPERM;
185
186 if (*ppos != 0)
187 return -EINVAL;
188
189 if (count > XATTR_NAME_MAX)
190 return -E2BIG;
191
192 ab = audit_log_start(audit_context(), GFP_KERNEL,
193 AUDIT_INTEGRITY_EVM_XATTR);
194 if (!ab)
195 return -ENOMEM;
196
197 xattr = kmalloc(sizeof(struct xattr_list), GFP_KERNEL);
198 if (!xattr) {
199 err = -ENOMEM;
200 goto out;
201 }
202
203 xattr->name = memdup_user_nul(buf, count);
204 if (IS_ERR(xattr->name)) {
205 err = PTR_ERR(xattr->name);
206 xattr->name = NULL;
207 goto out;
208 }
209
210
211 len = strlen(xattr->name);
212 if (len && xattr->name[len-1] == '\n')
213 xattr->name[len-1] = '\0';
214
215 audit_log_format(ab, "xattr=");
216 audit_log_untrustedstring(ab, xattr->name);
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 if (!err)
227 err = count;
228 goto out;
229 }
230
231 if (strncmp(xattr->name, XATTR_SECURITY_PREFIX,
232 XATTR_SECURITY_PREFIX_LEN) != 0) {
233 err = -EINVAL;
234 goto out;
235 }
236
237
238
239
240
241
242
243
244
245 mutex_lock(&xattr_list_mutex);
246 list_for_each_entry(tmp, &evm_config_xattrnames, list) {
247 if (strcmp(xattr->name, tmp->name) == 0) {
248 err = -EEXIST;
249 mutex_unlock(&xattr_list_mutex);
250 goto out;
251 }
252 }
253 list_add_tail_rcu(&xattr->list, &evm_config_xattrnames);
254 mutex_unlock(&xattr_list_mutex);
255
256 audit_log_format(ab, " res=0");
257 audit_log_end(ab);
258 return count;
259 out:
260 audit_log_format(ab, " res=%d", err);
261 audit_log_end(ab);
262 if (xattr) {
263 kfree(xattr->name);
264 kfree(xattr);
265 }
266 return err;
267 }
268
269 static const struct file_operations evm_xattr_ops = {
270 .read = evm_read_xattrs,
271 .write = evm_write_xattrs,
272 };
273
274 static int evm_init_xattrs(void)
275 {
276 evm_xattrs = securityfs_create_file("evm_xattrs", 0660, evm_dir, NULL,
277 &evm_xattr_ops);
278 if (!evm_xattrs || IS_ERR(evm_xattrs))
279 return -EFAULT;
280
281 return 0;
282 }
283 #else
284 static int evm_init_xattrs(void)
285 {
286 return 0;
287 }
288 #endif
289
290 int __init evm_init_secfs(void)
291 {
292 int error = 0;
293
294 evm_dir = securityfs_create_dir("evm", integrity_dir);
295 if (!evm_dir || IS_ERR(evm_dir))
296 return -EFAULT;
297
298 evm_init_tpm = securityfs_create_file("evm", 0660,
299 evm_dir, NULL, &evm_key_ops);
300 if (!evm_init_tpm || IS_ERR(evm_init_tpm)) {
301 error = -EFAULT;
302 goto out;
303 }
304
305 evm_symlink = securityfs_create_symlink("evm", NULL,
306 "integrity/evm/evm", NULL);
307 if (!evm_symlink || IS_ERR(evm_symlink)) {
308 error = -EFAULT;
309 goto out;
310 }
311
312 if (evm_init_xattrs() != 0) {
313 error = -EFAULT;
314 goto out;
315 }
316
317 return 0;
318 out:
319 securityfs_remove(evm_symlink);
320 securityfs_remove(evm_init_tpm);
321 securityfs_remove(evm_dir);
322 return error;
323 }