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