blob: 461215e5fd31d11be9f68c97fac1de09e057b368 [file] [log] [blame]
Mimi Zoharbab73932009-02-04 09:06:59 -05001/*
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 */
Eric Parisf850a7c2009-05-12 15:13:55 -040018#include <linux/fcntl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Mimi Zoharbab73932009-02-04 09:06:59 -050020#include <linux/module.h>
21#include <linux/seq_file.h>
22#include <linux/rculist.h>
23#include <linux/rcupdate.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050024#include <linux/parser.h>
Mimi Zoharbab73932009-02-04 09:06:59 -050025
26#include "ima.h"
27
Mimi Zohar4af46622009-02-04 09:07:00 -050028static int valid_policy = 1;
Mimi Zoharbab73932009-02-04 09:06:59 -050029#define TMPBUFLEN 12
30static ssize_t ima_show_htable_value(char __user *buf, size_t count,
31 loff_t *ppos, atomic_long_t *val)
32{
33 char tmpbuf[TMPBUFLEN];
34 ssize_t len;
35
36 len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
37 return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
38}
39
40static ssize_t ima_show_htable_violations(struct file *filp,
41 char __user *buf,
42 size_t count, loff_t *ppos)
43{
44 return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
45}
46
Alexey Dobriyan828c0952009-10-01 15:43:56 -070047static const struct file_operations ima_htable_violations_ops = {
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +020048 .read = ima_show_htable_violations,
49 .llseek = generic_file_llseek,
Mimi Zoharbab73932009-02-04 09:06:59 -050050};
51
52static ssize_t ima_show_measurements_count(struct file *filp,
53 char __user *buf,
54 size_t count, loff_t *ppos)
55{
56 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
57
58}
59
Alexey Dobriyan828c0952009-10-01 15:43:56 -070060static const struct file_operations ima_measurements_count_ops = {
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +020061 .read = ima_show_measurements_count,
62 .llseek = generic_file_llseek,
Mimi Zoharbab73932009-02-04 09:06:59 -050063};
64
65/* returns pointer to hlist_node */
66static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
67{
68 loff_t l = *pos;
69 struct ima_queue_entry *qe;
70
71 /* we need a lock since pos could point beyond last element */
72 rcu_read_lock();
73 list_for_each_entry_rcu(qe, &ima_measurements, later) {
74 if (!l--) {
75 rcu_read_unlock();
76 return qe;
77 }
78 }
79 rcu_read_unlock();
80 return NULL;
81}
82
83static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
84{
85 struct ima_queue_entry *qe = v;
86
87 /* lock protects when reading beyond last element
88 * against concurrent list-extension
89 */
90 rcu_read_lock();
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090091 qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
Mimi Zoharbab73932009-02-04 09:06:59 -050092 rcu_read_unlock();
93 (*pos)++;
94
95 return (&qe->later == &ima_measurements) ? NULL : qe;
96}
97
98static void ima_measurements_stop(struct seq_file *m, void *v)
99{
100}
101
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200102void ima_putc(struct seq_file *m, void *data, int datalen)
Mimi Zoharbab73932009-02-04 09:06:59 -0500103{
104 while (datalen--)
105 seq_putc(m, *(char *)data++);
106}
107
108/* print format:
109 * 32bit-le=pcr#
110 * char[20]=template digest
111 * 32bit-le=template name size
112 * char[n]=template name
Roberto Sassua71dc652013-06-07 12:16:33 +0200113 * [eventdata length]
Mimi Zoharbab73932009-02-04 09:06:59 -0500114 * eventdata[n]=template specific data
115 */
116static int ima_measurements_show(struct seq_file *m, void *v)
117{
118 /* the list never shrinks, so we don't need a lock here */
119 struct ima_queue_entry *qe = v;
120 struct ima_template_entry *e;
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200121 char *template_name;
Mimi Zoharbab73932009-02-04 09:06:59 -0500122 int namelen;
123 u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100124 bool is_ima_template = false;
Roberto Sassua71dc652013-06-07 12:16:33 +0200125 int i;
Mimi Zoharbab73932009-02-04 09:06:59 -0500126
127 /* get entry */
128 e = qe->entry;
129 if (e == NULL)
130 return -1;
131
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200132 template_name = (e->template_desc->name[0] != '\0') ?
133 e->template_desc->name : e->template_desc->fmt;
134
Mimi Zoharbab73932009-02-04 09:06:59 -0500135 /*
136 * 1st: PCRIndex
137 * PCR used is always the same (config option) in
138 * little-endian format
139 */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200140 ima_putc(m, &pcr, sizeof(pcr));
Mimi Zoharbab73932009-02-04 09:06:59 -0500141
142 /* 2nd: template digest */
Mimi Zohar140d8022013-03-11 20:29:47 -0400143 ima_putc(m, e->digest, TPM_DIGEST_SIZE);
Mimi Zoharbab73932009-02-04 09:06:59 -0500144
145 /* 3rd: template name size */
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200146 namelen = strlen(template_name);
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200147 ima_putc(m, &namelen, sizeof(namelen));
Mimi Zoharbab73932009-02-04 09:06:59 -0500148
149 /* 4th: template name */
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200150 ima_putc(m, template_name, namelen);
Mimi Zoharbab73932009-02-04 09:06:59 -0500151
Roberto Sassua71dc652013-06-07 12:16:33 +0200152 /* 5th: template length (except for 'ima' template) */
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200153 if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100154 is_ima_template = true;
155
156 if (!is_ima_template)
Roberto Sassua71dc652013-06-07 12:16:33 +0200157 ima_putc(m, &e->template_data_len,
158 sizeof(e->template_data_len));
159
160 /* 6th: template specific data */
161 for (i = 0; i < e->template_desc->num_fields; i++) {
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100162 enum ima_show_type show = IMA_SHOW_BINARY;
163 struct ima_template_field *field = e->template_desc->fields[i];
164
165 if (is_ima_template && strcmp(field->field_id, "d") == 0)
166 show = IMA_SHOW_BINARY_NO_FIELD_LEN;
Roberto Sassuc019e302014-02-03 13:56:04 +0100167 if (is_ima_template && strcmp(field->field_id, "n") == 0)
168 show = IMA_SHOW_BINARY_OLD_STRING_FMT;
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100169 field->field_show(m, show, &e->template_data[i]);
Roberto Sassua71dc652013-06-07 12:16:33 +0200170 }
Mimi Zoharbab73932009-02-04 09:06:59 -0500171 return 0;
172}
173
James Morris88e9d342009-09-22 16:43:43 -0700174static const struct seq_operations ima_measurments_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500175 .start = ima_measurements_start,
176 .next = ima_measurements_next,
177 .stop = ima_measurements_stop,
178 .show = ima_measurements_show
179};
180
181static int ima_measurements_open(struct inode *inode, struct file *file)
182{
183 return seq_open(file, &ima_measurments_seqops);
184}
185
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700186static const struct file_operations ima_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500187 .open = ima_measurements_open,
188 .read = seq_read,
189 .llseek = seq_lseek,
190 .release = seq_release,
191};
192
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200193void ima_print_digest(struct seq_file *m, u8 *digest, int size)
Mimi Zoharbab73932009-02-04 09:06:59 -0500194{
195 int i;
196
Mimi Zohar140d8022013-03-11 20:29:47 -0400197 for (i = 0; i < size; i++)
Mimi Zoharbab73932009-02-04 09:06:59 -0500198 seq_printf(m, "%02x", *(digest + i));
199}
200
Mimi Zoharbab73932009-02-04 09:06:59 -0500201/* print in ascii */
202static int ima_ascii_measurements_show(struct seq_file *m, void *v)
203{
204 /* the list never shrinks, so we don't need a lock here */
205 struct ima_queue_entry *qe = v;
206 struct ima_template_entry *e;
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200207 char *template_name;
Roberto Sassua71dc652013-06-07 12:16:33 +0200208 int i;
Mimi Zoharbab73932009-02-04 09:06:59 -0500209
210 /* get entry */
211 e = qe->entry;
212 if (e == NULL)
213 return -1;
214
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200215 template_name = (e->template_desc->name[0] != '\0') ?
216 e->template_desc->name : e->template_desc->fmt;
217
Mimi Zoharbab73932009-02-04 09:06:59 -0500218 /* 1st: PCR used (config option) */
219 seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
220
221 /* 2nd: SHA1 template hash */
Mimi Zohar140d8022013-03-11 20:29:47 -0400222 ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
Mimi Zoharbab73932009-02-04 09:06:59 -0500223
224 /* 3th: template name */
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200225 seq_printf(m, " %s", template_name);
Mimi Zoharbab73932009-02-04 09:06:59 -0500226
227 /* 4th: template specific data */
Roberto Sassua71dc652013-06-07 12:16:33 +0200228 for (i = 0; i < e->template_desc->num_fields; i++) {
229 seq_puts(m, " ");
230 if (e->template_data[i].len == 0)
231 continue;
232
233 e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
234 &e->template_data[i]);
235 }
236 seq_puts(m, "\n");
Mimi Zoharbab73932009-02-04 09:06:59 -0500237 return 0;
238}
239
James Morris88e9d342009-09-22 16:43:43 -0700240static const struct seq_operations ima_ascii_measurements_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500241 .start = ima_measurements_start,
242 .next = ima_measurements_next,
243 .stop = ima_measurements_stop,
244 .show = ima_ascii_measurements_show
245};
246
247static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
248{
249 return seq_open(file, &ima_ascii_measurements_seqops);
250}
251
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700252static const struct file_operations ima_ascii_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500253 .open = ima_ascii_measurements_open,
254 .read = seq_read,
255 .llseek = seq_lseek,
256 .release = seq_release,
257};
258
Mimi Zohar4af46622009-02-04 09:07:00 -0500259static ssize_t ima_write_policy(struct file *file, const char __user *buf,
260 size_t datalen, loff_t *ppos)
261{
Eric Paris6ccd0452010-04-20 10:20:54 -0400262 char *data = NULL;
263 ssize_t result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500264
265 if (datalen >= PAGE_SIZE)
Eric Paris6ccd0452010-04-20 10:20:54 -0400266 datalen = PAGE_SIZE - 1;
267
268 /* No partial writes. */
269 result = -EINVAL;
270 if (*ppos != 0)
271 goto out;
272
273 result = -ENOMEM;
Mimi Zohar4af46622009-02-04 09:07:00 -0500274 data = kmalloc(datalen + 1, GFP_KERNEL);
275 if (!data)
Eric Paris6ccd0452010-04-20 10:20:54 -0400276 goto out;
Mimi Zohar4af46622009-02-04 09:07:00 -0500277
Mimi Zohar4af46622009-02-04 09:07:00 -0500278 *(data + datalen) = '\0';
Mimi Zohar4af46622009-02-04 09:07:00 -0500279
Eric Paris6ccd0452010-04-20 10:20:54 -0400280 result = -EFAULT;
281 if (copy_from_user(data, buf, datalen))
282 goto out;
283
284 result = ima_parse_add_rule(data);
285out:
286 if (result < 0)
287 valid_policy = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500288 kfree(data);
Eric Paris6ccd0452010-04-20 10:20:54 -0400289 return result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500290}
291
Mimi Zoharbab73932009-02-04 09:06:59 -0500292static struct dentry *ima_dir;
293static struct dentry *binary_runtime_measurements;
294static struct dentry *ascii_runtime_measurements;
295static struct dentry *runtime_measurements_count;
296static struct dentry *violations;
Mimi Zohar4af46622009-02-04 09:07:00 -0500297static struct dentry *ima_policy;
298
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300299enum ima_fs_flags {
300 IMA_FS_BUSY,
301};
302
303static unsigned long ima_fs_flags;
304
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500305/*
306 * ima_open_policy: sequentialize access to the policy file
307 */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200308static int ima_open_policy(struct inode *inode, struct file *filp)
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500309{
Eric Parisf850a7c2009-05-12 15:13:55 -0400310 /* No point in being allowed to open it if you aren't going to write */
311 if (!(filp->f_flags & O_WRONLY))
312 return -EACCES;
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300313 if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
314 return -EBUSY;
315 return 0;
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500316}
317
Mimi Zohar4af46622009-02-04 09:07:00 -0500318/*
319 * ima_release_policy - start using the new measure policy rules.
320 *
321 * Initially, ima_measure points to the default policy rules, now
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500322 * point to the new policy rules, and remove the securityfs policy file,
323 * assuming a valid policy.
Mimi Zohar4af46622009-02-04 09:07:00 -0500324 */
325static int ima_release_policy(struct inode *inode, struct file *file)
326{
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300327 const char *cause = valid_policy ? "completed" : "failed";
328
329 pr_info("IMA: policy update %s\n", cause);
330 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
331 "policy_update", cause, !valid_policy, 0);
332
Mimi Zohar4af46622009-02-04 09:07:00 -0500333 if (!valid_policy) {
334 ima_delete_rules();
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500335 valid_policy = 1;
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300336 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
Mimi Zohar4af46622009-02-04 09:07:00 -0500337 return 0;
338 }
339 ima_update_policy();
340 securityfs_remove(ima_policy);
341 ima_policy = NULL;
342 return 0;
343}
344
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700345static const struct file_operations ima_measure_policy_ops = {
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500346 .open = ima_open_policy,
Mimi Zohar4af46622009-02-04 09:07:00 -0500347 .write = ima_write_policy,
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +0200348 .release = ima_release_policy,
349 .llseek = generic_file_llseek,
Mimi Zohar4af46622009-02-04 09:07:00 -0500350};
Mimi Zoharbab73932009-02-04 09:06:59 -0500351
Eric Paris932995f2009-05-21 15:43:32 -0400352int __init ima_fs_init(void)
Mimi Zoharbab73932009-02-04 09:06:59 -0500353{
354 ima_dir = securityfs_create_dir("ima", NULL);
355 if (IS_ERR(ima_dir))
356 return -1;
357
358 binary_runtime_measurements =
359 securityfs_create_file("binary_runtime_measurements",
360 S_IRUSR | S_IRGRP, ima_dir, NULL,
361 &ima_measurements_ops);
362 if (IS_ERR(binary_runtime_measurements))
363 goto out;
364
365 ascii_runtime_measurements =
366 securityfs_create_file("ascii_runtime_measurements",
367 S_IRUSR | S_IRGRP, ima_dir, NULL,
368 &ima_ascii_measurements_ops);
369 if (IS_ERR(ascii_runtime_measurements))
370 goto out;
371
372 runtime_measurements_count =
373 securityfs_create_file("runtime_measurements_count",
374 S_IRUSR | S_IRGRP, ima_dir, NULL,
375 &ima_measurements_count_ops);
376 if (IS_ERR(runtime_measurements_count))
377 goto out;
378
379 violations =
380 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
381 ima_dir, NULL, &ima_htable_violations_ops);
382 if (IS_ERR(violations))
383 goto out;
384
Mimi Zohar4af46622009-02-04 09:07:00 -0500385 ima_policy = securityfs_create_file("policy",
Eric Parisf850a7c2009-05-12 15:13:55 -0400386 S_IWUSR,
Mimi Zohar4af46622009-02-04 09:07:00 -0500387 ima_dir, NULL,
388 &ima_measure_policy_ops);
389 if (IS_ERR(ima_policy))
390 goto out;
Mimi Zoharbab73932009-02-04 09:06:59 -0500391
Mimi Zohar4af46622009-02-04 09:07:00 -0500392 return 0;
Mimi Zoharbab73932009-02-04 09:06:59 -0500393out:
Dmitry Kasatkin0ea4f8a2012-01-29 19:19:08 -0500394 securityfs_remove(violations);
Mimi Zoharbab73932009-02-04 09:06:59 -0500395 securityfs_remove(runtime_measurements_count);
396 securityfs_remove(ascii_runtime_measurements);
397 securityfs_remove(binary_runtime_measurements);
398 securityfs_remove(ima_dir);
Mimi Zohar4af46622009-02-04 09:07:00 -0500399 securityfs_remove(ima_policy);
Mimi Zoharbab73932009-02-04 09:06:59 -0500400 return -1;
401}