blob: 8fe736aabe71264d3018f35a1a3c7d64806411bb [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 = {
Mimi Zoharbab73932009-02-04 09:06:59 -050048 .read = ima_show_htable_violations
49};
50
51static ssize_t ima_show_measurements_count(struct file *filp,
52 char __user *buf,
53 size_t count, loff_t *ppos)
54{
55 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
56
57}
58
Alexey Dobriyan828c0952009-10-01 15:43:56 -070059static const struct file_operations ima_measurements_count_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -050060 .read = ima_show_measurements_count
61};
62
63/* returns pointer to hlist_node */
64static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
65{
66 loff_t l = *pos;
67 struct ima_queue_entry *qe;
68
69 /* we need a lock since pos could point beyond last element */
70 rcu_read_lock();
71 list_for_each_entry_rcu(qe, &ima_measurements, later) {
72 if (!l--) {
73 rcu_read_unlock();
74 return qe;
75 }
76 }
77 rcu_read_unlock();
78 return NULL;
79}
80
81static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
82{
83 struct ima_queue_entry *qe = v;
84
85 /* lock protects when reading beyond last element
86 * against concurrent list-extension
87 */
88 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +020089 qe = list_entry_rcu(qe->later.next,
90 struct ima_queue_entry, later);
Mimi Zoharbab73932009-02-04 09:06:59 -050091 rcu_read_unlock();
92 (*pos)++;
93
94 return (&qe->later == &ima_measurements) ? NULL : qe;
95}
96
97static void ima_measurements_stop(struct seq_file *m, void *v)
98{
99}
100
101static void ima_putc(struct seq_file *m, void *data, int datalen)
102{
103 while (datalen--)
104 seq_putc(m, *(char *)data++);
105}
106
107/* print format:
108 * 32bit-le=pcr#
109 * char[20]=template digest
110 * 32bit-le=template name size
111 * char[n]=template name
112 * eventdata[n]=template specific data
113 */
114static int ima_measurements_show(struct seq_file *m, void *v)
115{
116 /* the list never shrinks, so we don't need a lock here */
117 struct ima_queue_entry *qe = v;
118 struct ima_template_entry *e;
119 int namelen;
120 u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
121
122 /* get entry */
123 e = qe->entry;
124 if (e == NULL)
125 return -1;
126
127 /*
128 * 1st: PCRIndex
129 * PCR used is always the same (config option) in
130 * little-endian format
131 */
132 ima_putc(m, &pcr, sizeof pcr);
133
134 /* 2nd: template digest */
135 ima_putc(m, e->digest, IMA_DIGEST_SIZE);
136
137 /* 3rd: template name size */
138 namelen = strlen(e->template_name);
139 ima_putc(m, &namelen, sizeof namelen);
140
141 /* 4th: template name */
Mimi Zohar523979a2009-02-11 11:12:28 -0500142 ima_putc(m, (void *)e->template_name, namelen);
Mimi Zoharbab73932009-02-04 09:06:59 -0500143
144 /* 5th: template specific data */
145 ima_template_show(m, (struct ima_template_data *)&e->template,
146 IMA_SHOW_BINARY);
147 return 0;
148}
149
James Morris88e9d342009-09-22 16:43:43 -0700150static const struct seq_operations ima_measurments_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500151 .start = ima_measurements_start,
152 .next = ima_measurements_next,
153 .stop = ima_measurements_stop,
154 .show = ima_measurements_show
155};
156
157static int ima_measurements_open(struct inode *inode, struct file *file)
158{
159 return seq_open(file, &ima_measurments_seqops);
160}
161
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700162static const struct file_operations ima_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500163 .open = ima_measurements_open,
164 .read = seq_read,
165 .llseek = seq_lseek,
166 .release = seq_release,
167};
168
169static void ima_print_digest(struct seq_file *m, u8 *digest)
170{
171 int i;
172
173 for (i = 0; i < IMA_DIGEST_SIZE; i++)
174 seq_printf(m, "%02x", *(digest + i));
175}
176
177void ima_template_show(struct seq_file *m, void *e, enum ima_show_type show)
178{
179 struct ima_template_data *entry = e;
180 int namelen;
181
182 switch (show) {
183 case IMA_SHOW_ASCII:
184 ima_print_digest(m, entry->digest);
185 seq_printf(m, " %s\n", entry->file_name);
186 break;
187 case IMA_SHOW_BINARY:
188 ima_putc(m, entry->digest, IMA_DIGEST_SIZE);
189
190 namelen = strlen(entry->file_name);
191 ima_putc(m, &namelen, sizeof namelen);
192 ima_putc(m, entry->file_name, namelen);
193 default:
194 break;
195 }
196}
197
198/* print in ascii */
199static int ima_ascii_measurements_show(struct seq_file *m, void *v)
200{
201 /* the list never shrinks, so we don't need a lock here */
202 struct ima_queue_entry *qe = v;
203 struct ima_template_entry *e;
204
205 /* get entry */
206 e = qe->entry;
207 if (e == NULL)
208 return -1;
209
210 /* 1st: PCR used (config option) */
211 seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
212
213 /* 2nd: SHA1 template hash */
214 ima_print_digest(m, e->digest);
215
216 /* 3th: template name */
217 seq_printf(m, " %s ", e->template_name);
218
219 /* 4th: template specific data */
220 ima_template_show(m, (struct ima_template_data *)&e->template,
221 IMA_SHOW_ASCII);
222 return 0;
223}
224
James Morris88e9d342009-09-22 16:43:43 -0700225static const struct seq_operations ima_ascii_measurements_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500226 .start = ima_measurements_start,
227 .next = ima_measurements_next,
228 .stop = ima_measurements_stop,
229 .show = ima_ascii_measurements_show
230};
231
232static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
233{
234 return seq_open(file, &ima_ascii_measurements_seqops);
235}
236
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700237static const struct file_operations ima_ascii_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500238 .open = ima_ascii_measurements_open,
239 .read = seq_read,
240 .llseek = seq_lseek,
241 .release = seq_release,
242};
243
Mimi Zohar4af46622009-02-04 09:07:00 -0500244static ssize_t ima_write_policy(struct file *file, const char __user *buf,
245 size_t datalen, loff_t *ppos)
246{
Eric Paris6ccd0452010-04-20 10:20:54 -0400247 char *data = NULL;
248 ssize_t result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500249
250 if (datalen >= PAGE_SIZE)
Eric Paris6ccd0452010-04-20 10:20:54 -0400251 datalen = PAGE_SIZE - 1;
252
253 /* No partial writes. */
254 result = -EINVAL;
255 if (*ppos != 0)
256 goto out;
257
258 result = -ENOMEM;
Mimi Zohar4af46622009-02-04 09:07:00 -0500259 data = kmalloc(datalen + 1, GFP_KERNEL);
260 if (!data)
Eric Paris6ccd0452010-04-20 10:20:54 -0400261 goto out;
Mimi Zohar4af46622009-02-04 09:07:00 -0500262
Mimi Zohar4af46622009-02-04 09:07:00 -0500263 *(data + datalen) = '\0';
Mimi Zohar4af46622009-02-04 09:07:00 -0500264
Eric Paris6ccd0452010-04-20 10:20:54 -0400265 result = -EFAULT;
266 if (copy_from_user(data, buf, datalen))
267 goto out;
268
269 result = ima_parse_add_rule(data);
270out:
271 if (result < 0)
272 valid_policy = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500273 kfree(data);
Eric Paris6ccd0452010-04-20 10:20:54 -0400274 return result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500275}
276
Mimi Zoharbab73932009-02-04 09:06:59 -0500277static struct dentry *ima_dir;
278static struct dentry *binary_runtime_measurements;
279static struct dentry *ascii_runtime_measurements;
280static struct dentry *runtime_measurements_count;
281static struct dentry *violations;
Mimi Zohar4af46622009-02-04 09:07:00 -0500282static struct dentry *ima_policy;
283
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500284static atomic_t policy_opencount = ATOMIC_INIT(1);
285/*
286 * ima_open_policy: sequentialize access to the policy file
287 */
288int ima_open_policy(struct inode * inode, struct file * filp)
289{
Eric Parisf850a7c2009-05-12 15:13:55 -0400290 /* No point in being allowed to open it if you aren't going to write */
291 if (!(filp->f_flags & O_WRONLY))
292 return -EACCES;
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500293 if (atomic_dec_and_test(&policy_opencount))
294 return 0;
295 return -EBUSY;
296}
297
Mimi Zohar4af46622009-02-04 09:07:00 -0500298/*
299 * ima_release_policy - start using the new measure policy rules.
300 *
301 * Initially, ima_measure points to the default policy rules, now
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500302 * point to the new policy rules, and remove the securityfs policy file,
303 * assuming a valid policy.
Mimi Zohar4af46622009-02-04 09:07:00 -0500304 */
305static int ima_release_policy(struct inode *inode, struct file *file)
306{
307 if (!valid_policy) {
308 ima_delete_rules();
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500309 valid_policy = 1;
310 atomic_set(&policy_opencount, 1);
Mimi Zohar4af46622009-02-04 09:07:00 -0500311 return 0;
312 }
313 ima_update_policy();
314 securityfs_remove(ima_policy);
315 ima_policy = NULL;
316 return 0;
317}
318
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700319static const struct file_operations ima_measure_policy_ops = {
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500320 .open = ima_open_policy,
Mimi Zohar4af46622009-02-04 09:07:00 -0500321 .write = ima_write_policy,
322 .release = ima_release_policy
323};
Mimi Zoharbab73932009-02-04 09:06:59 -0500324
Eric Paris932995f2009-05-21 15:43:32 -0400325int __init ima_fs_init(void)
Mimi Zoharbab73932009-02-04 09:06:59 -0500326{
327 ima_dir = securityfs_create_dir("ima", NULL);
328 if (IS_ERR(ima_dir))
329 return -1;
330
331 binary_runtime_measurements =
332 securityfs_create_file("binary_runtime_measurements",
333 S_IRUSR | S_IRGRP, ima_dir, NULL,
334 &ima_measurements_ops);
335 if (IS_ERR(binary_runtime_measurements))
336 goto out;
337
338 ascii_runtime_measurements =
339 securityfs_create_file("ascii_runtime_measurements",
340 S_IRUSR | S_IRGRP, ima_dir, NULL,
341 &ima_ascii_measurements_ops);
342 if (IS_ERR(ascii_runtime_measurements))
343 goto out;
344
345 runtime_measurements_count =
346 securityfs_create_file("runtime_measurements_count",
347 S_IRUSR | S_IRGRP, ima_dir, NULL,
348 &ima_measurements_count_ops);
349 if (IS_ERR(runtime_measurements_count))
350 goto out;
351
352 violations =
353 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
354 ima_dir, NULL, &ima_htable_violations_ops);
355 if (IS_ERR(violations))
356 goto out;
357
Mimi Zohar4af46622009-02-04 09:07:00 -0500358 ima_policy = securityfs_create_file("policy",
Eric Parisf850a7c2009-05-12 15:13:55 -0400359 S_IWUSR,
Mimi Zohar4af46622009-02-04 09:07:00 -0500360 ima_dir, NULL,
361 &ima_measure_policy_ops);
362 if (IS_ERR(ima_policy))
363 goto out;
Mimi Zoharbab73932009-02-04 09:06:59 -0500364
Mimi Zohar4af46622009-02-04 09:07:00 -0500365 return 0;
Mimi Zoharbab73932009-02-04 09:06:59 -0500366out:
367 securityfs_remove(runtime_measurements_count);
368 securityfs_remove(ascii_runtime_measurements);
369 securityfs_remove(binary_runtime_measurements);
370 securityfs_remove(ima_dir);
Mimi Zohar4af46622009-02-04 09:07:00 -0500371 securityfs_remove(ima_policy);
Mimi Zoharbab73932009-02-04 09:06:59 -0500372 return -1;
373}
374
375void __exit ima_fs_cleanup(void)
376{
377 securityfs_remove(violations);
378 securityfs_remove(runtime_measurements_count);
379 securityfs_remove(ascii_runtime_measurements);
380 securityfs_remove(binary_runtime_measurements);
381 securityfs_remove(ima_dir);
Mimi Zohar4af46622009-02-04 09:07:00 -0500382 securityfs_remove(ima_policy);
Mimi Zoharbab73932009-02-04 09:06:59 -0500383}