blob: 3674a52e1cfb768dc6d4390fafa2fb3ac574064d [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>
Mimi Zoharbab73932009-02-04 09:06:59 -050019#include <linux/module.h>
20#include <linux/seq_file.h>
21#include <linux/rculist.h>
22#include <linux/rcupdate.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050023#include <linux/parser.h>
Mimi Zoharbab73932009-02-04 09:06:59 -050024
25#include "ima.h"
26
Mimi Zohar4af46622009-02-04 09:07:00 -050027static int valid_policy = 1;
Mimi Zoharbab73932009-02-04 09:06:59 -050028#define TMPBUFLEN 12
29static ssize_t ima_show_htable_value(char __user *buf, size_t count,
30 loff_t *ppos, atomic_long_t *val)
31{
32 char tmpbuf[TMPBUFLEN];
33 ssize_t len;
34
35 len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
36 return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
37}
38
39static ssize_t ima_show_htable_violations(struct file *filp,
40 char __user *buf,
41 size_t count, loff_t *ppos)
42{
43 return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
44}
45
Alexey Dobriyan828c0952009-10-01 15:43:56 -070046static const struct file_operations ima_htable_violations_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -050047 .read = ima_show_htable_violations
48};
49
50static ssize_t ima_show_measurements_count(struct file *filp,
51 char __user *buf,
52 size_t count, loff_t *ppos)
53{
54 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
55
56}
57
Alexey Dobriyan828c0952009-10-01 15:43:56 -070058static const struct file_operations ima_measurements_count_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -050059 .read = ima_show_measurements_count
60};
61
62/* returns pointer to hlist_node */
63static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
64{
65 loff_t l = *pos;
66 struct ima_queue_entry *qe;
67
68 /* we need a lock since pos could point beyond last element */
69 rcu_read_lock();
70 list_for_each_entry_rcu(qe, &ima_measurements, later) {
71 if (!l--) {
72 rcu_read_unlock();
73 return qe;
74 }
75 }
76 rcu_read_unlock();
77 return NULL;
78}
79
80static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
81{
82 struct ima_queue_entry *qe = v;
83
84 /* lock protects when reading beyond last element
85 * against concurrent list-extension
86 */
87 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +020088 qe = list_entry_rcu(qe->later.next,
89 struct ima_queue_entry, later);
Mimi Zoharbab73932009-02-04 09:06:59 -050090 rcu_read_unlock();
91 (*pos)++;
92
93 return (&qe->later == &ima_measurements) ? NULL : qe;
94}
95
96static void ima_measurements_stop(struct seq_file *m, void *v)
97{
98}
99
100static void ima_putc(struct seq_file *m, void *data, int datalen)
101{
102 while (datalen--)
103 seq_putc(m, *(char *)data++);
104}
105
106/* print format:
107 * 32bit-le=pcr#
108 * char[20]=template digest
109 * 32bit-le=template name size
110 * char[n]=template name
111 * eventdata[n]=template specific data
112 */
113static int ima_measurements_show(struct seq_file *m, void *v)
114{
115 /* the list never shrinks, so we don't need a lock here */
116 struct ima_queue_entry *qe = v;
117 struct ima_template_entry *e;
118 int namelen;
119 u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
120
121 /* get entry */
122 e = qe->entry;
123 if (e == NULL)
124 return -1;
125
126 /*
127 * 1st: PCRIndex
128 * PCR used is always the same (config option) in
129 * little-endian format
130 */
131 ima_putc(m, &pcr, sizeof pcr);
132
133 /* 2nd: template digest */
134 ima_putc(m, e->digest, IMA_DIGEST_SIZE);
135
136 /* 3rd: template name size */
137 namelen = strlen(e->template_name);
138 ima_putc(m, &namelen, sizeof namelen);
139
140 /* 4th: template name */
Mimi Zohar523979a2009-02-11 11:12:28 -0500141 ima_putc(m, (void *)e->template_name, namelen);
Mimi Zoharbab73932009-02-04 09:06:59 -0500142
143 /* 5th: template specific data */
144 ima_template_show(m, (struct ima_template_data *)&e->template,
145 IMA_SHOW_BINARY);
146 return 0;
147}
148
James Morris88e9d342009-09-22 16:43:43 -0700149static const struct seq_operations ima_measurments_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500150 .start = ima_measurements_start,
151 .next = ima_measurements_next,
152 .stop = ima_measurements_stop,
153 .show = ima_measurements_show
154};
155
156static int ima_measurements_open(struct inode *inode, struct file *file)
157{
158 return seq_open(file, &ima_measurments_seqops);
159}
160
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700161static const struct file_operations ima_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500162 .open = ima_measurements_open,
163 .read = seq_read,
164 .llseek = seq_lseek,
165 .release = seq_release,
166};
167
168static void ima_print_digest(struct seq_file *m, u8 *digest)
169{
170 int i;
171
172 for (i = 0; i < IMA_DIGEST_SIZE; i++)
173 seq_printf(m, "%02x", *(digest + i));
174}
175
176void ima_template_show(struct seq_file *m, void *e, enum ima_show_type show)
177{
178 struct ima_template_data *entry = e;
179 int namelen;
180
181 switch (show) {
182 case IMA_SHOW_ASCII:
183 ima_print_digest(m, entry->digest);
184 seq_printf(m, " %s\n", entry->file_name);
185 break;
186 case IMA_SHOW_BINARY:
187 ima_putc(m, entry->digest, IMA_DIGEST_SIZE);
188
189 namelen = strlen(entry->file_name);
190 ima_putc(m, &namelen, sizeof namelen);
191 ima_putc(m, entry->file_name, namelen);
192 default:
193 break;
194 }
195}
196
197/* print in ascii */
198static int ima_ascii_measurements_show(struct seq_file *m, void *v)
199{
200 /* the list never shrinks, so we don't need a lock here */
201 struct ima_queue_entry *qe = v;
202 struct ima_template_entry *e;
203
204 /* get entry */
205 e = qe->entry;
206 if (e == NULL)
207 return -1;
208
209 /* 1st: PCR used (config option) */
210 seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
211
212 /* 2nd: SHA1 template hash */
213 ima_print_digest(m, e->digest);
214
215 /* 3th: template name */
216 seq_printf(m, " %s ", e->template_name);
217
218 /* 4th: template specific data */
219 ima_template_show(m, (struct ima_template_data *)&e->template,
220 IMA_SHOW_ASCII);
221 return 0;
222}
223
James Morris88e9d342009-09-22 16:43:43 -0700224static const struct seq_operations ima_ascii_measurements_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500225 .start = ima_measurements_start,
226 .next = ima_measurements_next,
227 .stop = ima_measurements_stop,
228 .show = ima_ascii_measurements_show
229};
230
231static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
232{
233 return seq_open(file, &ima_ascii_measurements_seqops);
234}
235
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700236static const struct file_operations ima_ascii_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500237 .open = ima_ascii_measurements_open,
238 .read = seq_read,
239 .llseek = seq_lseek,
240 .release = seq_release,
241};
242
Mimi Zohar4af46622009-02-04 09:07:00 -0500243static ssize_t ima_write_policy(struct file *file, const char __user *buf,
244 size_t datalen, loff_t *ppos)
245{
Eric Paris6ccd0452010-04-20 10:20:54 -0400246 char *data = NULL;
247 ssize_t result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500248
249 if (datalen >= PAGE_SIZE)
Eric Paris6ccd0452010-04-20 10:20:54 -0400250 datalen = PAGE_SIZE - 1;
251
252 /* No partial writes. */
253 result = -EINVAL;
254 if (*ppos != 0)
255 goto out;
256
257 result = -ENOMEM;
Mimi Zohar4af46622009-02-04 09:07:00 -0500258 data = kmalloc(datalen + 1, GFP_KERNEL);
259 if (!data)
Eric Paris6ccd0452010-04-20 10:20:54 -0400260 goto out;
Mimi Zohar4af46622009-02-04 09:07:00 -0500261
Mimi Zohar4af46622009-02-04 09:07:00 -0500262 *(data + datalen) = '\0';
Mimi Zohar4af46622009-02-04 09:07:00 -0500263
Eric Paris6ccd0452010-04-20 10:20:54 -0400264 result = -EFAULT;
265 if (copy_from_user(data, buf, datalen))
266 goto out;
267
268 result = ima_parse_add_rule(data);
269out:
270 if (result < 0)
271 valid_policy = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500272 kfree(data);
Eric Paris6ccd0452010-04-20 10:20:54 -0400273 return result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500274}
275
Mimi Zoharbab73932009-02-04 09:06:59 -0500276static struct dentry *ima_dir;
277static struct dentry *binary_runtime_measurements;
278static struct dentry *ascii_runtime_measurements;
279static struct dentry *runtime_measurements_count;
280static struct dentry *violations;
Mimi Zohar4af46622009-02-04 09:07:00 -0500281static struct dentry *ima_policy;
282
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500283static atomic_t policy_opencount = ATOMIC_INIT(1);
284/*
285 * ima_open_policy: sequentialize access to the policy file
286 */
287int ima_open_policy(struct inode * inode, struct file * filp)
288{
Eric Parisf850a7c2009-05-12 15:13:55 -0400289 /* No point in being allowed to open it if you aren't going to write */
290 if (!(filp->f_flags & O_WRONLY))
291 return -EACCES;
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500292 if (atomic_dec_and_test(&policy_opencount))
293 return 0;
294 return -EBUSY;
295}
296
Mimi Zohar4af46622009-02-04 09:07:00 -0500297/*
298 * ima_release_policy - start using the new measure policy rules.
299 *
300 * Initially, ima_measure points to the default policy rules, now
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500301 * point to the new policy rules, and remove the securityfs policy file,
302 * assuming a valid policy.
Mimi Zohar4af46622009-02-04 09:07:00 -0500303 */
304static int ima_release_policy(struct inode *inode, struct file *file)
305{
306 if (!valid_policy) {
307 ima_delete_rules();
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500308 valid_policy = 1;
309 atomic_set(&policy_opencount, 1);
Mimi Zohar4af46622009-02-04 09:07:00 -0500310 return 0;
311 }
312 ima_update_policy();
313 securityfs_remove(ima_policy);
314 ima_policy = NULL;
315 return 0;
316}
317
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700318static const struct file_operations ima_measure_policy_ops = {
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500319 .open = ima_open_policy,
Mimi Zohar4af46622009-02-04 09:07:00 -0500320 .write = ima_write_policy,
321 .release = ima_release_policy
322};
Mimi Zoharbab73932009-02-04 09:06:59 -0500323
Eric Paris932995f2009-05-21 15:43:32 -0400324int __init ima_fs_init(void)
Mimi Zoharbab73932009-02-04 09:06:59 -0500325{
326 ima_dir = securityfs_create_dir("ima", NULL);
327 if (IS_ERR(ima_dir))
328 return -1;
329
330 binary_runtime_measurements =
331 securityfs_create_file("binary_runtime_measurements",
332 S_IRUSR | S_IRGRP, ima_dir, NULL,
333 &ima_measurements_ops);
334 if (IS_ERR(binary_runtime_measurements))
335 goto out;
336
337 ascii_runtime_measurements =
338 securityfs_create_file("ascii_runtime_measurements",
339 S_IRUSR | S_IRGRP, ima_dir, NULL,
340 &ima_ascii_measurements_ops);
341 if (IS_ERR(ascii_runtime_measurements))
342 goto out;
343
344 runtime_measurements_count =
345 securityfs_create_file("runtime_measurements_count",
346 S_IRUSR | S_IRGRP, ima_dir, NULL,
347 &ima_measurements_count_ops);
348 if (IS_ERR(runtime_measurements_count))
349 goto out;
350
351 violations =
352 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
353 ima_dir, NULL, &ima_htable_violations_ops);
354 if (IS_ERR(violations))
355 goto out;
356
Mimi Zohar4af46622009-02-04 09:07:00 -0500357 ima_policy = securityfs_create_file("policy",
Eric Parisf850a7c2009-05-12 15:13:55 -0400358 S_IWUSR,
Mimi Zohar4af46622009-02-04 09:07:00 -0500359 ima_dir, NULL,
360 &ima_measure_policy_ops);
361 if (IS_ERR(ima_policy))
362 goto out;
Mimi Zoharbab73932009-02-04 09:06:59 -0500363
Mimi Zohar4af46622009-02-04 09:07:00 -0500364 return 0;
Mimi Zoharbab73932009-02-04 09:06:59 -0500365out:
366 securityfs_remove(runtime_measurements_count);
367 securityfs_remove(ascii_runtime_measurements);
368 securityfs_remove(binary_runtime_measurements);
369 securityfs_remove(ima_dir);
Mimi Zohar4af46622009-02-04 09:07:00 -0500370 securityfs_remove(ima_policy);
Mimi Zoharbab73932009-02-04 09:06:59 -0500371 return -1;
372}
373
374void __exit ima_fs_cleanup(void)
375{
376 securityfs_remove(violations);
377 securityfs_remove(runtime_measurements_count);
378 securityfs_remove(ascii_runtime_measurements);
379 securityfs_remove(binary_runtime_measurements);
380 securityfs_remove(ima_dir);
Mimi Zohar4af46622009-02-04 09:07:00 -0500381 securityfs_remove(ima_policy);
Mimi Zoharbab73932009-02-04 09:06:59 -0500382}