]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - security/integrity/ima/ima_fs.c
IMA: create machine owner and blacklist keyrings
[mirror_ubuntu-artful-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>
bab73937
MZ
25
26#include "ima.h"
27
38d859f9
PM
28static DEFINE_MUTEX(ima_write_mutex);
29
4af4662f 30static int valid_policy = 1;
bab73937
MZ
31#define TMPBUFLEN 12
32static ssize_t ima_show_htable_value(char __user *buf, size_t count,
33 loff_t *ppos, atomic_long_t *val)
34{
35 char tmpbuf[TMPBUFLEN];
36 ssize_t len;
37
38 len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
39 return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
40}
41
42static ssize_t ima_show_htable_violations(struct file *filp,
43 char __user *buf,
44 size_t count, loff_t *ppos)
45{
46 return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
47}
48
828c0950 49static const struct file_operations ima_htable_violations_ops = {
cdcd90f9
AB
50 .read = ima_show_htable_violations,
51 .llseek = generic_file_llseek,
bab73937
MZ
52};
53
54static ssize_t ima_show_measurements_count(struct file *filp,
55 char __user *buf,
56 size_t count, loff_t *ppos)
57{
58 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
59
60}
61
828c0950 62static const struct file_operations ima_measurements_count_ops = {
cdcd90f9
AB
63 .read = ima_show_measurements_count,
64 .llseek = generic_file_llseek,
bab73937
MZ
65};
66
67/* returns pointer to hlist_node */
68static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
69{
70 loff_t l = *pos;
71 struct ima_queue_entry *qe;
72
73 /* we need a lock since pos could point beyond last element */
74 rcu_read_lock();
75 list_for_each_entry_rcu(qe, &ima_measurements, later) {
76 if (!l--) {
77 rcu_read_unlock();
78 return qe;
79 }
80 }
81 rcu_read_unlock();
82 return NULL;
83}
84
85static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
86{
87 struct ima_queue_entry *qe = v;
88
89 /* lock protects when reading beyond last element
90 * against concurrent list-extension
91 */
92 rcu_read_lock();
089bc8e9 93 qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
bab73937
MZ
94 rcu_read_unlock();
95 (*pos)++;
96
97 return (&qe->later == &ima_measurements) ? NULL : qe;
98}
99
100static void ima_measurements_stop(struct seq_file *m, void *v)
101{
102}
103
3ce1217d 104void ima_putc(struct seq_file *m, void *data, int datalen)
bab73937
MZ
105{
106 while (datalen--)
107 seq_putc(m, *(char *)data++);
108}
109
110/* print format:
111 * 32bit-le=pcr#
112 * char[20]=template digest
113 * 32bit-le=template name size
114 * char[n]=template name
a71dc65d 115 * [eventdata length]
bab73937
MZ
116 * eventdata[n]=template specific data
117 */
118static int ima_measurements_show(struct seq_file *m, void *v)
119{
120 /* the list never shrinks, so we don't need a lock here */
121 struct ima_queue_entry *qe = v;
122 struct ima_template_entry *e;
7dbdb420 123 char *template_name;
bab73937
MZ
124 int namelen;
125 u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
3e8e5503 126 bool is_ima_template = false;
a71dc65d 127 int i;
bab73937
MZ
128
129 /* get entry */
130 e = qe->entry;
131 if (e == NULL)
132 return -1;
133
7dbdb420
RS
134 template_name = (e->template_desc->name[0] != '\0') ?
135 e->template_desc->name : e->template_desc->fmt;
136
bab73937
MZ
137 /*
138 * 1st: PCRIndex
139 * PCR used is always the same (config option) in
140 * little-endian format
141 */
2bb930ab 142 ima_putc(m, &pcr, sizeof(pcr));
bab73937
MZ
143
144 /* 2nd: template digest */
140d8022 145 ima_putc(m, e->digest, TPM_DIGEST_SIZE);
bab73937
MZ
146
147 /* 3rd: template name size */
7dbdb420 148 namelen = strlen(template_name);
2bb930ab 149 ima_putc(m, &namelen, sizeof(namelen));
bab73937
MZ
150
151 /* 4th: template name */
7dbdb420 152 ima_putc(m, template_name, namelen);
a71dc65d
RS
153
154 /* 5th: template length (except for 'ima' template) */
7dbdb420 155 if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
3e8e5503
RS
156 is_ima_template = true;
157
158 if (!is_ima_template)
a71dc65d
RS
159 ima_putc(m, &e->template_data_len,
160 sizeof(e->template_data_len));
bab73937 161
a71dc65d
RS
162 /* 6th: template specific data */
163 for (i = 0; i < e->template_desc->num_fields; i++) {
3e8e5503
RS
164 enum ima_show_type show = IMA_SHOW_BINARY;
165 struct ima_template_field *field = e->template_desc->fields[i];
166
167 if (is_ima_template && strcmp(field->field_id, "d") == 0)
168 show = IMA_SHOW_BINARY_NO_FIELD_LEN;
c019e307
RS
169 if (is_ima_template && strcmp(field->field_id, "n") == 0)
170 show = IMA_SHOW_BINARY_OLD_STRING_FMT;
3e8e5503 171 field->field_show(m, show, &e->template_data[i]);
a71dc65d 172 }
bab73937
MZ
173 return 0;
174}
175
88e9d34c 176static const struct seq_operations ima_measurments_seqops = {
bab73937
MZ
177 .start = ima_measurements_start,
178 .next = ima_measurements_next,
179 .stop = ima_measurements_stop,
180 .show = ima_measurements_show
181};
182
183static int ima_measurements_open(struct inode *inode, struct file *file)
184{
185 return seq_open(file, &ima_measurments_seqops);
186}
187
828c0950 188static const struct file_operations ima_measurements_ops = {
bab73937
MZ
189 .open = ima_measurements_open,
190 .read = seq_read,
191 .llseek = seq_lseek,
192 .release = seq_release,
193};
194
45b26133 195void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
bab73937 196{
45b26133 197 u32 i;
bab73937 198
140d8022 199 for (i = 0; i < size; i++)
bab73937
MZ
200 seq_printf(m, "%02x", *(digest + i));
201}
202
bab73937
MZ
203/* print in ascii */
204static int ima_ascii_measurements_show(struct seq_file *m, void *v)
205{
206 /* the list never shrinks, so we don't need a lock here */
207 struct ima_queue_entry *qe = v;
208 struct ima_template_entry *e;
7dbdb420 209 char *template_name;
a71dc65d 210 int i;
bab73937
MZ
211
212 /* get entry */
213 e = qe->entry;
214 if (e == NULL)
215 return -1;
216
7dbdb420
RS
217 template_name = (e->template_desc->name[0] != '\0') ?
218 e->template_desc->name : e->template_desc->fmt;
219
bab73937
MZ
220 /* 1st: PCR used (config option) */
221 seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
222
223 /* 2nd: SHA1 template hash */
140d8022 224 ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
bab73937
MZ
225
226 /* 3th: template name */
7dbdb420 227 seq_printf(m, " %s", template_name);
bab73937
MZ
228
229 /* 4th: template specific data */
a71dc65d
RS
230 for (i = 0; i < e->template_desc->num_fields; i++) {
231 seq_puts(m, " ");
232 if (e->template_data[i].len == 0)
233 continue;
234
235 e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
236 &e->template_data[i]);
237 }
238 seq_puts(m, "\n");
bab73937
MZ
239 return 0;
240}
241
88e9d34c 242static const struct seq_operations ima_ascii_measurements_seqops = {
bab73937
MZ
243 .start = ima_measurements_start,
244 .next = ima_measurements_next,
245 .stop = ima_measurements_stop,
246 .show = ima_ascii_measurements_show
247};
248
249static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
250{
251 return seq_open(file, &ima_ascii_measurements_seqops);
252}
253
828c0950 254static const struct file_operations ima_ascii_measurements_ops = {
bab73937
MZ
255 .open = ima_ascii_measurements_open,
256 .read = seq_read,
257 .llseek = seq_lseek,
258 .release = seq_release,
259};
260
4af4662f
MZ
261static ssize_t ima_write_policy(struct file *file, const char __user *buf,
262 size_t datalen, loff_t *ppos)
263{
6ccd0456
EP
264 char *data = NULL;
265 ssize_t result;
38d859f9
PM
266 int res;
267
268 res = mutex_lock_interruptible(&ima_write_mutex);
269 if (res)
270 return res;
4af4662f
MZ
271
272 if (datalen >= PAGE_SIZE)
6ccd0456
EP
273 datalen = PAGE_SIZE - 1;
274
275 /* No partial writes. */
276 result = -EINVAL;
277 if (*ppos != 0)
278 goto out;
279
280 result = -ENOMEM;
4af4662f
MZ
281 data = kmalloc(datalen + 1, GFP_KERNEL);
282 if (!data)
6ccd0456 283 goto out;
4af4662f 284
4af4662f 285 *(data + datalen) = '\0';
4af4662f 286
6ccd0456
EP
287 result = -EFAULT;
288 if (copy_from_user(data, buf, datalen))
289 goto out;
290
291 result = ima_parse_add_rule(data);
292out:
293 if (result < 0)
294 valid_policy = 0;
4af4662f 295 kfree(data);
38d859f9
PM
296 mutex_unlock(&ima_write_mutex);
297
6ccd0456 298 return result;
4af4662f
MZ
299}
300
bab73937
MZ
301static struct dentry *ima_dir;
302static struct dentry *binary_runtime_measurements;
303static struct dentry *ascii_runtime_measurements;
304static struct dentry *runtime_measurements_count;
305static struct dentry *violations;
4af4662f
MZ
306static struct dentry *ima_policy;
307
0716abbb
DK
308enum ima_fs_flags {
309 IMA_FS_BUSY,
310};
311
312static unsigned long ima_fs_flags;
313
f4bd857b
MZ
314/*
315 * ima_open_policy: sequentialize access to the policy file
316 */
2bb930ab 317static int ima_open_policy(struct inode *inode, struct file *filp)
f4bd857b 318{
f850a7c0
EP
319 /* No point in being allowed to open it if you aren't going to write */
320 if (!(filp->f_flags & O_WRONLY))
321 return -EACCES;
0716abbb
DK
322 if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
323 return -EBUSY;
324 return 0;
f4bd857b
MZ
325}
326
4af4662f
MZ
327/*
328 * ima_release_policy - start using the new measure policy rules.
329 *
330 * Initially, ima_measure points to the default policy rules, now
f4bd857b
MZ
331 * point to the new policy rules, and remove the securityfs policy file,
332 * assuming a valid policy.
4af4662f
MZ
333 */
334static int ima_release_policy(struct inode *inode, struct file *file)
335{
0716abbb
DK
336 const char *cause = valid_policy ? "completed" : "failed";
337
338 pr_info("IMA: policy update %s\n", cause);
339 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
340 "policy_update", cause, !valid_policy, 0);
341
4af4662f
MZ
342 if (!valid_policy) {
343 ima_delete_rules();
f4bd857b 344 valid_policy = 1;
0716abbb 345 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
4af4662f
MZ
346 return 0;
347 }
348 ima_update_policy();
38d859f9 349#ifndef CONFIG_IMA_WRITE_POLICY
4af4662f
MZ
350 securityfs_remove(ima_policy);
351 ima_policy = NULL;
38d859f9
PM
352#else
353 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
354#endif
4af4662f
MZ
355 return 0;
356}
357
828c0950 358static const struct file_operations ima_measure_policy_ops = {
f4bd857b 359 .open = ima_open_policy,
4af4662f 360 .write = ima_write_policy,
cdcd90f9
AB
361 .release = ima_release_policy,
362 .llseek = generic_file_llseek,
4af4662f 363};
bab73937 364
932995f0 365int __init ima_fs_init(void)
bab73937
MZ
366{
367 ima_dir = securityfs_create_dir("ima", NULL);
368 if (IS_ERR(ima_dir))
369 return -1;
370
371 binary_runtime_measurements =
372 securityfs_create_file("binary_runtime_measurements",
373 S_IRUSR | S_IRGRP, ima_dir, NULL,
374 &ima_measurements_ops);
375 if (IS_ERR(binary_runtime_measurements))
376 goto out;
377
378 ascii_runtime_measurements =
379 securityfs_create_file("ascii_runtime_measurements",
380 S_IRUSR | S_IRGRP, ima_dir, NULL,
381 &ima_ascii_measurements_ops);
382 if (IS_ERR(ascii_runtime_measurements))
383 goto out;
384
385 runtime_measurements_count =
386 securityfs_create_file("runtime_measurements_count",
387 S_IRUSR | S_IRGRP, ima_dir, NULL,
388 &ima_measurements_count_ops);
389 if (IS_ERR(runtime_measurements_count))
390 goto out;
391
392 violations =
393 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
394 ima_dir, NULL, &ima_htable_violations_ops);
395 if (IS_ERR(violations))
396 goto out;
397
4af4662f 398 ima_policy = securityfs_create_file("policy",
f850a7c0 399 S_IWUSR,
4af4662f
MZ
400 ima_dir, NULL,
401 &ima_measure_policy_ops);
402 if (IS_ERR(ima_policy))
403 goto out;
bab73937 404
4af4662f 405 return 0;
bab73937 406out:
0ea4f8ae 407 securityfs_remove(violations);
bab73937
MZ
408 securityfs_remove(runtime_measurements_count);
409 securityfs_remove(ascii_runtime_measurements);
410 securityfs_remove(binary_runtime_measurements);
411 securityfs_remove(ima_dir);
4af4662f 412 securityfs_remove(ima_policy);
bab73937
MZ
413 return -1;
414}