blob: db01125926bdb1e696165389bcdd26117e9d25fc [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;
121 int namelen;
122 u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100123 bool is_ima_template = false;
Roberto Sassua71dc652013-06-07 12:16:33 +0200124 int i;
Mimi Zoharbab73932009-02-04 09:06:59 -0500125
126 /* get entry */
127 e = qe->entry;
128 if (e == NULL)
129 return -1;
130
131 /*
132 * 1st: PCRIndex
133 * PCR used is always the same (config option) in
134 * little-endian format
135 */
136 ima_putc(m, &pcr, sizeof pcr);
137
138 /* 2nd: template digest */
Mimi Zohar140d8022013-03-11 20:29:47 -0400139 ima_putc(m, e->digest, TPM_DIGEST_SIZE);
Mimi Zoharbab73932009-02-04 09:06:59 -0500140
141 /* 3rd: template name size */
Roberto Sassua71dc652013-06-07 12:16:33 +0200142 namelen = strlen(e->template_desc->name);
Mimi Zoharbab73932009-02-04 09:06:59 -0500143 ima_putc(m, &namelen, sizeof namelen);
144
145 /* 4th: template name */
Roberto Sassua71dc652013-06-07 12:16:33 +0200146 ima_putc(m, e->template_desc->name, namelen);
Mimi Zoharbab73932009-02-04 09:06:59 -0500147
Roberto Sassua71dc652013-06-07 12:16:33 +0200148 /* 5th: template length (except for 'ima' template) */
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100149 if (strcmp(e->template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0)
150 is_ima_template = true;
151
152 if (!is_ima_template)
Roberto Sassua71dc652013-06-07 12:16:33 +0200153 ima_putc(m, &e->template_data_len,
154 sizeof(e->template_data_len));
155
156 /* 6th: template specific data */
157 for (i = 0; i < e->template_desc->num_fields; i++) {
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100158 enum ima_show_type show = IMA_SHOW_BINARY;
159 struct ima_template_field *field = e->template_desc->fields[i];
160
161 if (is_ima_template && strcmp(field->field_id, "d") == 0)
162 show = IMA_SHOW_BINARY_NO_FIELD_LEN;
163 field->field_show(m, show, &e->template_data[i]);
Roberto Sassua71dc652013-06-07 12:16:33 +0200164 }
Mimi Zoharbab73932009-02-04 09:06:59 -0500165 return 0;
166}
167
James Morris88e9d342009-09-22 16:43:43 -0700168static const struct seq_operations ima_measurments_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500169 .start = ima_measurements_start,
170 .next = ima_measurements_next,
171 .stop = ima_measurements_stop,
172 .show = ima_measurements_show
173};
174
175static int ima_measurements_open(struct inode *inode, struct file *file)
176{
177 return seq_open(file, &ima_measurments_seqops);
178}
179
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700180static const struct file_operations ima_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500181 .open = ima_measurements_open,
182 .read = seq_read,
183 .llseek = seq_lseek,
184 .release = seq_release,
185};
186
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200187void ima_print_digest(struct seq_file *m, u8 *digest, int size)
Mimi Zoharbab73932009-02-04 09:06:59 -0500188{
189 int i;
190
Mimi Zohar140d8022013-03-11 20:29:47 -0400191 for (i = 0; i < size; i++)
Mimi Zoharbab73932009-02-04 09:06:59 -0500192 seq_printf(m, "%02x", *(digest + i));
193}
194
Mimi Zoharbab73932009-02-04 09:06:59 -0500195/* print in ascii */
196static int ima_ascii_measurements_show(struct seq_file *m, void *v)
197{
198 /* the list never shrinks, so we don't need a lock here */
199 struct ima_queue_entry *qe = v;
200 struct ima_template_entry *e;
Roberto Sassua71dc652013-06-07 12:16:33 +0200201 int i;
Mimi Zoharbab73932009-02-04 09:06:59 -0500202
203 /* get entry */
204 e = qe->entry;
205 if (e == NULL)
206 return -1;
207
208 /* 1st: PCR used (config option) */
209 seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
210
211 /* 2nd: SHA1 template hash */
Mimi Zohar140d8022013-03-11 20:29:47 -0400212 ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
Mimi Zoharbab73932009-02-04 09:06:59 -0500213
214 /* 3th: template name */
Roberto Sassua71dc652013-06-07 12:16:33 +0200215 seq_printf(m, " %s", e->template_desc->name);
Mimi Zoharbab73932009-02-04 09:06:59 -0500216
217 /* 4th: template specific data */
Roberto Sassua71dc652013-06-07 12:16:33 +0200218 for (i = 0; i < e->template_desc->num_fields; i++) {
219 seq_puts(m, " ");
220 if (e->template_data[i].len == 0)
221 continue;
222
223 e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
224 &e->template_data[i]);
225 }
226 seq_puts(m, "\n");
Mimi Zoharbab73932009-02-04 09:06:59 -0500227 return 0;
228}
229
James Morris88e9d342009-09-22 16:43:43 -0700230static const struct seq_operations ima_ascii_measurements_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500231 .start = ima_measurements_start,
232 .next = ima_measurements_next,
233 .stop = ima_measurements_stop,
234 .show = ima_ascii_measurements_show
235};
236
237static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
238{
239 return seq_open(file, &ima_ascii_measurements_seqops);
240}
241
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700242static const struct file_operations ima_ascii_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500243 .open = ima_ascii_measurements_open,
244 .read = seq_read,
245 .llseek = seq_lseek,
246 .release = seq_release,
247};
248
Mimi Zohar4af46622009-02-04 09:07:00 -0500249static ssize_t ima_write_policy(struct file *file, const char __user *buf,
250 size_t datalen, loff_t *ppos)
251{
Eric Paris6ccd0452010-04-20 10:20:54 -0400252 char *data = NULL;
253 ssize_t result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500254
255 if (datalen >= PAGE_SIZE)
Eric Paris6ccd0452010-04-20 10:20:54 -0400256 datalen = PAGE_SIZE - 1;
257
258 /* No partial writes. */
259 result = -EINVAL;
260 if (*ppos != 0)
261 goto out;
262
263 result = -ENOMEM;
Mimi Zohar4af46622009-02-04 09:07:00 -0500264 data = kmalloc(datalen + 1, GFP_KERNEL);
265 if (!data)
Eric Paris6ccd0452010-04-20 10:20:54 -0400266 goto out;
Mimi Zohar4af46622009-02-04 09:07:00 -0500267
Mimi Zohar4af46622009-02-04 09:07:00 -0500268 *(data + datalen) = '\0';
Mimi Zohar4af46622009-02-04 09:07:00 -0500269
Eric Paris6ccd0452010-04-20 10:20:54 -0400270 result = -EFAULT;
271 if (copy_from_user(data, buf, datalen))
272 goto out;
273
274 result = ima_parse_add_rule(data);
275out:
276 if (result < 0)
277 valid_policy = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500278 kfree(data);
Eric Paris6ccd0452010-04-20 10:20:54 -0400279 return result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500280}
281
Mimi Zoharbab73932009-02-04 09:06:59 -0500282static struct dentry *ima_dir;
283static struct dentry *binary_runtime_measurements;
284static struct dentry *ascii_runtime_measurements;
285static struct dentry *runtime_measurements_count;
286static struct dentry *violations;
Mimi Zohar4af46622009-02-04 09:07:00 -0500287static struct dentry *ima_policy;
288
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500289static atomic_t policy_opencount = ATOMIC_INIT(1);
290/*
291 * ima_open_policy: sequentialize access to the policy file
292 */
James Morrisb97e1452011-08-30 10:18:30 +1000293static int ima_open_policy(struct inode * inode, struct file * filp)
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500294{
Eric Parisf850a7c2009-05-12 15:13:55 -0400295 /* No point in being allowed to open it if you aren't going to write */
296 if (!(filp->f_flags & O_WRONLY))
297 return -EACCES;
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500298 if (atomic_dec_and_test(&policy_opencount))
299 return 0;
300 return -EBUSY;
301}
302
Mimi Zohar4af46622009-02-04 09:07:00 -0500303/*
304 * ima_release_policy - start using the new measure policy rules.
305 *
306 * Initially, ima_measure points to the default policy rules, now
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500307 * point to the new policy rules, and remove the securityfs policy file,
308 * assuming a valid policy.
Mimi Zohar4af46622009-02-04 09:07:00 -0500309 */
310static int ima_release_policy(struct inode *inode, struct file *file)
311{
312 if (!valid_policy) {
313 ima_delete_rules();
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500314 valid_policy = 1;
315 atomic_set(&policy_opencount, 1);
Mimi Zohar4af46622009-02-04 09:07:00 -0500316 return 0;
317 }
318 ima_update_policy();
319 securityfs_remove(ima_policy);
320 ima_policy = NULL;
321 return 0;
322}
323
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700324static const struct file_operations ima_measure_policy_ops = {
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500325 .open = ima_open_policy,
Mimi Zohar4af46622009-02-04 09:07:00 -0500326 .write = ima_write_policy,
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +0200327 .release = ima_release_policy,
328 .llseek = generic_file_llseek,
Mimi Zohar4af46622009-02-04 09:07:00 -0500329};
Mimi Zoharbab73932009-02-04 09:06:59 -0500330
Eric Paris932995f2009-05-21 15:43:32 -0400331int __init ima_fs_init(void)
Mimi Zoharbab73932009-02-04 09:06:59 -0500332{
333 ima_dir = securityfs_create_dir("ima", NULL);
334 if (IS_ERR(ima_dir))
335 return -1;
336
337 binary_runtime_measurements =
338 securityfs_create_file("binary_runtime_measurements",
339 S_IRUSR | S_IRGRP, ima_dir, NULL,
340 &ima_measurements_ops);
341 if (IS_ERR(binary_runtime_measurements))
342 goto out;
343
344 ascii_runtime_measurements =
345 securityfs_create_file("ascii_runtime_measurements",
346 S_IRUSR | S_IRGRP, ima_dir, NULL,
347 &ima_ascii_measurements_ops);
348 if (IS_ERR(ascii_runtime_measurements))
349 goto out;
350
351 runtime_measurements_count =
352 securityfs_create_file("runtime_measurements_count",
353 S_IRUSR | S_IRGRP, ima_dir, NULL,
354 &ima_measurements_count_ops);
355 if (IS_ERR(runtime_measurements_count))
356 goto out;
357
358 violations =
359 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
360 ima_dir, NULL, &ima_htable_violations_ops);
361 if (IS_ERR(violations))
362 goto out;
363
Mimi Zohar4af46622009-02-04 09:07:00 -0500364 ima_policy = securityfs_create_file("policy",
Eric Parisf850a7c2009-05-12 15:13:55 -0400365 S_IWUSR,
Mimi Zohar4af46622009-02-04 09:07:00 -0500366 ima_dir, NULL,
367 &ima_measure_policy_ops);
368 if (IS_ERR(ima_policy))
369 goto out;
Mimi Zoharbab73932009-02-04 09:06:59 -0500370
Mimi Zohar4af46622009-02-04 09:07:00 -0500371 return 0;
Mimi Zoharbab73932009-02-04 09:06:59 -0500372out:
Dmitry Kasatkin0ea4f8a2012-01-29 19:19:08 -0500373 securityfs_remove(violations);
Mimi Zoharbab73932009-02-04 09:06:59 -0500374 securityfs_remove(runtime_measurements_count);
375 securityfs_remove(ascii_runtime_measurements);
376 securityfs_remove(binary_runtime_measurements);
377 securityfs_remove(ima_dir);
Mimi Zohar4af46622009-02-04 09:07:00 -0500378 securityfs_remove(ima_policy);
Mimi Zoharbab73932009-02-04 09:06:59 -0500379 return -1;
380}