Commit | Line | Data |
---|---|---|
bab73937 MZ |
1 | /* |
2 | * Copyright (C) 2005,2006,2007,2008 IBM Corporation | |
3 | * | |
4 | * Authors: | |
5 | * Kylene Hall <kjhall@us.ibm.com> | |
6 | * Reiner Sailer <sailer@us.ibm.com> | |
7 | * Mimi Zohar <zohar@us.ibm.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License as | |
11 | * published by the Free Software Foundation, version 2 of the | |
12 | * License. | |
13 | * | |
14 | * File: ima_fs.c | |
15 | * implemenents security file system for reporting | |
16 | * current measurement list and IMA statistics | |
17 | */ | |
f850a7c0 | 18 | #include <linux/fcntl.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
bab73937 MZ |
20 | #include <linux/module.h> |
21 | #include <linux/seq_file.h> | |
22 | #include <linux/rculist.h> | |
23 | #include <linux/rcupdate.h> | |
4af4662f | 24 | #include <linux/parser.h> |
7429b092 | 25 | #include <linux/vmalloc.h> |
bab73937 MZ |
26 | |
27 | #include "ima.h" | |
28 | ||
38d859f9 PM |
29 | static DEFINE_MUTEX(ima_write_mutex); |
30 | ||
4af4662f | 31 | static int valid_policy = 1; |
bab73937 MZ |
32 | #define TMPBUFLEN 12 |
33 | static ssize_t ima_show_htable_value(char __user *buf, size_t count, | |
34 | loff_t *ppos, atomic_long_t *val) | |
35 | { | |
36 | char tmpbuf[TMPBUFLEN]; | |
37 | ssize_t len; | |
38 | ||
39 | len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val)); | |
40 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, len); | |
41 | } | |
42 | ||
43 | static ssize_t ima_show_htable_violations(struct file *filp, | |
44 | char __user *buf, | |
45 | size_t count, loff_t *ppos) | |
46 | { | |
47 | return ima_show_htable_value(buf, count, ppos, &ima_htable.violations); | |
48 | } | |
49 | ||
828c0950 | 50 | static const struct file_operations ima_htable_violations_ops = { |
cdcd90f9 AB |
51 | .read = ima_show_htable_violations, |
52 | .llseek = generic_file_llseek, | |
bab73937 MZ |
53 | }; |
54 | ||
55 | static ssize_t ima_show_measurements_count(struct file *filp, | |
56 | char __user *buf, | |
57 | size_t count, loff_t *ppos) | |
58 | { | |
59 | return ima_show_htable_value(buf, count, ppos, &ima_htable.len); | |
60 | ||
61 | } | |
62 | ||
828c0950 | 63 | static const struct file_operations ima_measurements_count_ops = { |
cdcd90f9 AB |
64 | .read = ima_show_measurements_count, |
65 | .llseek = generic_file_llseek, | |
bab73937 MZ |
66 | }; |
67 | ||
68 | /* returns pointer to hlist_node */ | |
69 | static void *ima_measurements_start(struct seq_file *m, loff_t *pos) | |
70 | { | |
71 | loff_t l = *pos; | |
72 | struct ima_queue_entry *qe; | |
73 | ||
74 | /* we need a lock since pos could point beyond last element */ | |
75 | rcu_read_lock(); | |
76 | list_for_each_entry_rcu(qe, &ima_measurements, later) { | |
77 | if (!l--) { | |
78 | rcu_read_unlock(); | |
79 | return qe; | |
80 | } | |
81 | } | |
82 | rcu_read_unlock(); | |
83 | return NULL; | |
84 | } | |
85 | ||
86 | static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos) | |
87 | { | |
88 | struct ima_queue_entry *qe = v; | |
89 | ||
90 | /* lock protects when reading beyond last element | |
91 | * against concurrent list-extension | |
92 | */ | |
93 | rcu_read_lock(); | |
089bc8e9 | 94 | qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later); |
bab73937 MZ |
95 | rcu_read_unlock(); |
96 | (*pos)++; | |
97 | ||
98 | return (&qe->later == &ima_measurements) ? NULL : qe; | |
99 | } | |
100 | ||
101 | static void ima_measurements_stop(struct seq_file *m, void *v) | |
102 | { | |
103 | } | |
104 | ||
3ce1217d | 105 | void ima_putc(struct seq_file *m, void *data, int datalen) |
bab73937 MZ |
106 | { |
107 | while (datalen--) | |
108 | seq_putc(m, *(char *)data++); | |
109 | } | |
110 | ||
111 | /* print format: | |
112 | * 32bit-le=pcr# | |
113 | * char[20]=template digest | |
114 | * 32bit-le=template name size | |
115 | * char[n]=template name | |
a71dc65d | 116 | * [eventdata length] |
bab73937 MZ |
117 | * eventdata[n]=template specific data |
118 | */ | |
119 | static int ima_measurements_show(struct seq_file *m, void *v) | |
120 | { | |
121 | /* the list never shrinks, so we don't need a lock here */ | |
122 | struct ima_queue_entry *qe = v; | |
123 | struct ima_template_entry *e; | |
7dbdb420 | 124 | char *template_name; |
bab73937 MZ |
125 | int namelen; |
126 | u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX; | |
3e8e5503 | 127 | bool is_ima_template = false; |
a71dc65d | 128 | int i; |
bab73937 MZ |
129 | |
130 | /* get entry */ | |
131 | e = qe->entry; | |
132 | if (e == NULL) | |
133 | return -1; | |
134 | ||
7dbdb420 RS |
135 | template_name = (e->template_desc->name[0] != '\0') ? |
136 | e->template_desc->name : e->template_desc->fmt; | |
137 | ||
bab73937 MZ |
138 | /* |
139 | * 1st: PCRIndex | |
140 | * PCR used is always the same (config option) in | |
141 | * little-endian format | |
142 | */ | |
2bb930ab | 143 | ima_putc(m, &pcr, sizeof(pcr)); |
bab73937 MZ |
144 | |
145 | /* 2nd: template digest */ | |
140d8022 | 146 | ima_putc(m, e->digest, TPM_DIGEST_SIZE); |
bab73937 MZ |
147 | |
148 | /* 3rd: template name size */ | |
7dbdb420 | 149 | namelen = strlen(template_name); |
2bb930ab | 150 | ima_putc(m, &namelen, sizeof(namelen)); |
bab73937 MZ |
151 | |
152 | /* 4th: template name */ | |
7dbdb420 | 153 | ima_putc(m, template_name, namelen); |
a71dc65d RS |
154 | |
155 | /* 5th: template length (except for 'ima' template) */ | |
7dbdb420 | 156 | if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0) |
3e8e5503 RS |
157 | is_ima_template = true; |
158 | ||
159 | if (!is_ima_template) | |
a71dc65d RS |
160 | ima_putc(m, &e->template_data_len, |
161 | sizeof(e->template_data_len)); | |
bab73937 | 162 | |
a71dc65d RS |
163 | /* 6th: template specific data */ |
164 | for (i = 0; i < e->template_desc->num_fields; i++) { | |
3e8e5503 RS |
165 | enum ima_show_type show = IMA_SHOW_BINARY; |
166 | struct ima_template_field *field = e->template_desc->fields[i]; | |
167 | ||
168 | if (is_ima_template && strcmp(field->field_id, "d") == 0) | |
169 | show = IMA_SHOW_BINARY_NO_FIELD_LEN; | |
c019e307 RS |
170 | if (is_ima_template && strcmp(field->field_id, "n") == 0) |
171 | show = IMA_SHOW_BINARY_OLD_STRING_FMT; | |
3e8e5503 | 172 | field->field_show(m, show, &e->template_data[i]); |
a71dc65d | 173 | } |
bab73937 MZ |
174 | return 0; |
175 | } | |
176 | ||
88e9d34c | 177 | static const struct seq_operations ima_measurments_seqops = { |
bab73937 MZ |
178 | .start = ima_measurements_start, |
179 | .next = ima_measurements_next, | |
180 | .stop = ima_measurements_stop, | |
181 | .show = ima_measurements_show | |
182 | }; | |
183 | ||
184 | static int ima_measurements_open(struct inode *inode, struct file *file) | |
185 | { | |
186 | return seq_open(file, &ima_measurments_seqops); | |
187 | } | |
188 | ||
828c0950 | 189 | static const struct file_operations ima_measurements_ops = { |
bab73937 MZ |
190 | .open = ima_measurements_open, |
191 | .read = seq_read, | |
192 | .llseek = seq_lseek, | |
193 | .release = seq_release, | |
194 | }; | |
195 | ||
45b26133 | 196 | void ima_print_digest(struct seq_file *m, u8 *digest, u32 size) |
bab73937 | 197 | { |
45b26133 | 198 | u32 i; |
bab73937 | 199 | |
140d8022 | 200 | for (i = 0; i < size; i++) |
bab73937 MZ |
201 | seq_printf(m, "%02x", *(digest + i)); |
202 | } | |
203 | ||
bab73937 MZ |
204 | /* print in ascii */ |
205 | static int ima_ascii_measurements_show(struct seq_file *m, void *v) | |
206 | { | |
207 | /* the list never shrinks, so we don't need a lock here */ | |
208 | struct ima_queue_entry *qe = v; | |
209 | struct ima_template_entry *e; | |
7dbdb420 | 210 | char *template_name; |
a71dc65d | 211 | int i; |
bab73937 MZ |
212 | |
213 | /* get entry */ | |
214 | e = qe->entry; | |
215 | if (e == NULL) | |
216 | return -1; | |
217 | ||
7dbdb420 RS |
218 | template_name = (e->template_desc->name[0] != '\0') ? |
219 | e->template_desc->name : e->template_desc->fmt; | |
220 | ||
bab73937 MZ |
221 | /* 1st: PCR used (config option) */ |
222 | seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX); | |
223 | ||
224 | /* 2nd: SHA1 template hash */ | |
140d8022 | 225 | ima_print_digest(m, e->digest, TPM_DIGEST_SIZE); |
bab73937 MZ |
226 | |
227 | /* 3th: template name */ | |
7dbdb420 | 228 | seq_printf(m, " %s", template_name); |
bab73937 MZ |
229 | |
230 | /* 4th: template specific data */ | |
a71dc65d RS |
231 | for (i = 0; i < e->template_desc->num_fields; i++) { |
232 | seq_puts(m, " "); | |
233 | if (e->template_data[i].len == 0) | |
234 | continue; | |
235 | ||
236 | e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII, | |
237 | &e->template_data[i]); | |
238 | } | |
239 | seq_puts(m, "\n"); | |
bab73937 MZ |
240 | return 0; |
241 | } | |
242 | ||
88e9d34c | 243 | static const struct seq_operations ima_ascii_measurements_seqops = { |
bab73937 MZ |
244 | .start = ima_measurements_start, |
245 | .next = ima_measurements_next, | |
246 | .stop = ima_measurements_stop, | |
247 | .show = ima_ascii_measurements_show | |
248 | }; | |
249 | ||
250 | static int ima_ascii_measurements_open(struct inode *inode, struct file *file) | |
251 | { | |
252 | return seq_open(file, &ima_ascii_measurements_seqops); | |
253 | } | |
254 | ||
828c0950 | 255 | static const struct file_operations ima_ascii_measurements_ops = { |
bab73937 MZ |
256 | .open = ima_ascii_measurements_open, |
257 | .read = seq_read, | |
258 | .llseek = seq_lseek, | |
259 | .release = seq_release, | |
260 | }; | |
261 | ||
7429b092 DK |
262 | static ssize_t ima_read_policy(char *path) |
263 | { | |
264 | void *data; | |
265 | char *datap; | |
266 | loff_t size; | |
267 | int rc, pathlen = strlen(path); | |
268 | ||
269 | char *p; | |
270 | ||
271 | /* remove \n */ | |
272 | datap = path; | |
273 | strsep(&datap, "\n"); | |
274 | ||
275 | rc = kernel_read_file_from_path(path, &data, &size, 0, READING_POLICY); | |
276 | if (rc < 0) { | |
277 | pr_err("Unable to open file: %s (%d)", path, rc); | |
278 | return rc; | |
279 | } | |
280 | ||
281 | datap = data; | |
282 | while (size > 0 && (p = strsep(&datap, "\n"))) { | |
283 | pr_debug("rule: %s\n", p); | |
284 | rc = ima_parse_add_rule(p); | |
285 | if (rc < 0) | |
286 | break; | |
287 | size -= rc; | |
288 | } | |
289 | ||
290 | vfree(data); | |
291 | if (rc < 0) | |
292 | return rc; | |
293 | else if (size) | |
294 | return -EINVAL; | |
295 | else | |
296 | return pathlen; | |
297 | } | |
298 | ||
4af4662f MZ |
299 | static ssize_t ima_write_policy(struct file *file, const char __user *buf, |
300 | size_t datalen, loff_t *ppos) | |
301 | { | |
6427e6c7 | 302 | char *data; |
6ccd0456 | 303 | ssize_t result; |
4af4662f MZ |
304 | |
305 | if (datalen >= PAGE_SIZE) | |
6ccd0456 EP |
306 | datalen = PAGE_SIZE - 1; |
307 | ||
308 | /* No partial writes. */ | |
309 | result = -EINVAL; | |
310 | if (*ppos != 0) | |
311 | goto out; | |
312 | ||
313 | result = -ENOMEM; | |
4af4662f MZ |
314 | data = kmalloc(datalen + 1, GFP_KERNEL); |
315 | if (!data) | |
6ccd0456 | 316 | goto out; |
4af4662f | 317 | |
4af4662f | 318 | *(data + datalen) = '\0'; |
4af4662f | 319 | |
6ccd0456 EP |
320 | result = -EFAULT; |
321 | if (copy_from_user(data, buf, datalen)) | |
6427e6c7 | 322 | goto out_free; |
6ccd0456 | 323 | |
6427e6c7 PM |
324 | result = mutex_lock_interruptible(&ima_write_mutex); |
325 | if (result < 0) | |
326 | goto out_free; | |
6427e6c7 | 327 | |
7429b092 DK |
328 | if (data[0] == '/') |
329 | result = ima_read_policy(data); | |
330 | else | |
331 | result = ima_parse_add_rule(data); | |
332 | mutex_unlock(&ima_write_mutex); | |
6427e6c7 PM |
333 | out_free: |
334 | kfree(data); | |
6ccd0456 EP |
335 | out: |
336 | if (result < 0) | |
337 | valid_policy = 0; | |
38d859f9 | 338 | |
6ccd0456 | 339 | return result; |
4af4662f MZ |
340 | } |
341 | ||
bab73937 MZ |
342 | static struct dentry *ima_dir; |
343 | static struct dentry *binary_runtime_measurements; | |
344 | static struct dentry *ascii_runtime_measurements; | |
345 | static struct dentry *runtime_measurements_count; | |
346 | static struct dentry *violations; | |
4af4662f MZ |
347 | static struct dentry *ima_policy; |
348 | ||
0716abbb DK |
349 | enum ima_fs_flags { |
350 | IMA_FS_BUSY, | |
351 | }; | |
352 | ||
353 | static unsigned long ima_fs_flags; | |
354 | ||
80eae209 PM |
355 | #ifdef CONFIG_IMA_READ_POLICY |
356 | static const struct seq_operations ima_policy_seqops = { | |
357 | .start = ima_policy_start, | |
358 | .next = ima_policy_next, | |
359 | .stop = ima_policy_stop, | |
360 | .show = ima_policy_show, | |
361 | }; | |
362 | #endif | |
363 | ||
f4bd857b MZ |
364 | /* |
365 | * ima_open_policy: sequentialize access to the policy file | |
366 | */ | |
2bb930ab | 367 | static int ima_open_policy(struct inode *inode, struct file *filp) |
f4bd857b | 368 | { |
80eae209 PM |
369 | if (!(filp->f_flags & O_WRONLY)) { |
370 | #ifndef CONFIG_IMA_READ_POLICY | |
f850a7c0 | 371 | return -EACCES; |
80eae209 PM |
372 | #else |
373 | if ((filp->f_flags & O_ACCMODE) != O_RDONLY) | |
374 | return -EACCES; | |
375 | if (!capable(CAP_SYS_ADMIN)) | |
376 | return -EPERM; | |
377 | return seq_open(filp, &ima_policy_seqops); | |
378 | #endif | |
379 | } | |
0716abbb DK |
380 | if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags)) |
381 | return -EBUSY; | |
382 | return 0; | |
f4bd857b MZ |
383 | } |
384 | ||
4af4662f MZ |
385 | /* |
386 | * ima_release_policy - start using the new measure policy rules. | |
387 | * | |
388 | * Initially, ima_measure points to the default policy rules, now | |
f4bd857b MZ |
389 | * point to the new policy rules, and remove the securityfs policy file, |
390 | * assuming a valid policy. | |
4af4662f MZ |
391 | */ |
392 | static int ima_release_policy(struct inode *inode, struct file *file) | |
393 | { | |
0716abbb DK |
394 | const char *cause = valid_policy ? "completed" : "failed"; |
395 | ||
80eae209 PM |
396 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) |
397 | return 0; | |
398 | ||
0112721d SL |
399 | if (valid_policy && ima_check_policy() < 0) { |
400 | cause = "failed"; | |
401 | valid_policy = 0; | |
402 | } | |
403 | ||
0716abbb DK |
404 | pr_info("IMA: policy update %s\n", cause); |
405 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL, | |
406 | "policy_update", cause, !valid_policy, 0); | |
407 | ||
4af4662f MZ |
408 | if (!valid_policy) { |
409 | ima_delete_rules(); | |
f4bd857b | 410 | valid_policy = 1; |
0716abbb | 411 | clear_bit(IMA_FS_BUSY, &ima_fs_flags); |
4af4662f MZ |
412 | return 0; |
413 | } | |
80eae209 | 414 | |
4af4662f | 415 | ima_update_policy(); |
38d859f9 | 416 | #ifndef CONFIG_IMA_WRITE_POLICY |
4af4662f MZ |
417 | securityfs_remove(ima_policy); |
418 | ima_policy = NULL; | |
38d859f9 PM |
419 | #else |
420 | clear_bit(IMA_FS_BUSY, &ima_fs_flags); | |
421 | #endif | |
4af4662f MZ |
422 | return 0; |
423 | } | |
424 | ||
828c0950 | 425 | static const struct file_operations ima_measure_policy_ops = { |
f4bd857b | 426 | .open = ima_open_policy, |
4af4662f | 427 | .write = ima_write_policy, |
80eae209 | 428 | .read = seq_read, |
cdcd90f9 AB |
429 | .release = ima_release_policy, |
430 | .llseek = generic_file_llseek, | |
4af4662f | 431 | }; |
bab73937 | 432 | |
932995f0 | 433 | int __init ima_fs_init(void) |
bab73937 MZ |
434 | { |
435 | ima_dir = securityfs_create_dir("ima", NULL); | |
436 | if (IS_ERR(ima_dir)) | |
437 | return -1; | |
438 | ||
439 | binary_runtime_measurements = | |
440 | securityfs_create_file("binary_runtime_measurements", | |
441 | S_IRUSR | S_IRGRP, ima_dir, NULL, | |
442 | &ima_measurements_ops); | |
443 | if (IS_ERR(binary_runtime_measurements)) | |
444 | goto out; | |
445 | ||
446 | ascii_runtime_measurements = | |
447 | securityfs_create_file("ascii_runtime_measurements", | |
448 | S_IRUSR | S_IRGRP, ima_dir, NULL, | |
449 | &ima_ascii_measurements_ops); | |
450 | if (IS_ERR(ascii_runtime_measurements)) | |
451 | goto out; | |
452 | ||
453 | runtime_measurements_count = | |
454 | securityfs_create_file("runtime_measurements_count", | |
455 | S_IRUSR | S_IRGRP, ima_dir, NULL, | |
456 | &ima_measurements_count_ops); | |
457 | if (IS_ERR(runtime_measurements_count)) | |
458 | goto out; | |
459 | ||
460 | violations = | |
461 | securityfs_create_file("violations", S_IRUSR | S_IRGRP, | |
462 | ima_dir, NULL, &ima_htable_violations_ops); | |
463 | if (IS_ERR(violations)) | |
464 | goto out; | |
465 | ||
80eae209 | 466 | ima_policy = securityfs_create_file("policy", POLICY_FILE_FLAGS, |
4af4662f MZ |
467 | ima_dir, NULL, |
468 | &ima_measure_policy_ops); | |
469 | if (IS_ERR(ima_policy)) | |
470 | goto out; | |
bab73937 | 471 | |
4af4662f | 472 | return 0; |
bab73937 | 473 | out: |
0ea4f8ae | 474 | securityfs_remove(violations); |
bab73937 MZ |
475 | securityfs_remove(runtime_measurements_count); |
476 | securityfs_remove(ascii_runtime_measurements); | |
477 | securityfs_remove(binary_runtime_measurements); | |
478 | securityfs_remove(ima_dir); | |
4af4662f | 479 | securityfs_remove(ima_policy); |
bab73937 MZ |
480 | return -1; |
481 | } |