blob: 5f0fd113433a27ff9fa43c92e1492212f8509bf2 [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
102static void ima_putc(struct seq_file *m, void *data, int datalen)
103{
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
113 * eventdata[n]=template specific data
114 */
115static int ima_measurements_show(struct seq_file *m, void *v)
116{
117 /* the list never shrinks, so we don't need a lock here */
118 struct ima_queue_entry *qe = v;
119 struct ima_template_entry *e;
120 int namelen;
121 u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
122
123 /* get entry */
124 e = qe->entry;
125 if (e == NULL)
126 return -1;
127
128 /*
129 * 1st: PCRIndex
130 * PCR used is always the same (config option) in
131 * little-endian format
132 */
133 ima_putc(m, &pcr, sizeof pcr);
134
135 /* 2nd: template digest */
136 ima_putc(m, e->digest, IMA_DIGEST_SIZE);
137
138 /* 3rd: template name size */
139 namelen = strlen(e->template_name);
140 ima_putc(m, &namelen, sizeof namelen);
141
142 /* 4th: template name */
Mimi Zohar523979a2009-02-11 11:12:28 -0500143 ima_putc(m, (void *)e->template_name, namelen);
Mimi Zoharbab73932009-02-04 09:06:59 -0500144
145 /* 5th: template specific data */
146 ima_template_show(m, (struct ima_template_data *)&e->template,
147 IMA_SHOW_BINARY);
148 return 0;
149}
150
James Morris88e9d342009-09-22 16:43:43 -0700151static const struct seq_operations ima_measurments_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500152 .start = ima_measurements_start,
153 .next = ima_measurements_next,
154 .stop = ima_measurements_stop,
155 .show = ima_measurements_show
156};
157
158static int ima_measurements_open(struct inode *inode, struct file *file)
159{
160 return seq_open(file, &ima_measurments_seqops);
161}
162
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700163static const struct file_operations ima_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500164 .open = ima_measurements_open,
165 .read = seq_read,
166 .llseek = seq_lseek,
167 .release = seq_release,
168};
169
170static void ima_print_digest(struct seq_file *m, u8 *digest)
171{
172 int i;
173
174 for (i = 0; i < IMA_DIGEST_SIZE; i++)
175 seq_printf(m, "%02x", *(digest + i));
176}
177
178void ima_template_show(struct seq_file *m, void *e, enum ima_show_type show)
179{
180 struct ima_template_data *entry = e;
181 int namelen;
182
183 switch (show) {
184 case IMA_SHOW_ASCII:
185 ima_print_digest(m, entry->digest);
186 seq_printf(m, " %s\n", entry->file_name);
187 break;
188 case IMA_SHOW_BINARY:
189 ima_putc(m, entry->digest, IMA_DIGEST_SIZE);
190
191 namelen = strlen(entry->file_name);
192 ima_putc(m, &namelen, sizeof namelen);
193 ima_putc(m, entry->file_name, namelen);
194 default:
195 break;
196 }
197}
198
199/* print in ascii */
200static int ima_ascii_measurements_show(struct seq_file *m, void *v)
201{
202 /* the list never shrinks, so we don't need a lock here */
203 struct ima_queue_entry *qe = v;
204 struct ima_template_entry *e;
205
206 /* get entry */
207 e = qe->entry;
208 if (e == NULL)
209 return -1;
210
211 /* 1st: PCR used (config option) */
212 seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
213
214 /* 2nd: SHA1 template hash */
215 ima_print_digest(m, e->digest);
216
217 /* 3th: template name */
218 seq_printf(m, " %s ", e->template_name);
219
220 /* 4th: template specific data */
221 ima_template_show(m, (struct ima_template_data *)&e->template,
222 IMA_SHOW_ASCII);
223 return 0;
224}
225
James Morris88e9d342009-09-22 16:43:43 -0700226static const struct seq_operations ima_ascii_measurements_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500227 .start = ima_measurements_start,
228 .next = ima_measurements_next,
229 .stop = ima_measurements_stop,
230 .show = ima_ascii_measurements_show
231};
232
233static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
234{
235 return seq_open(file, &ima_ascii_measurements_seqops);
236}
237
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700238static const struct file_operations ima_ascii_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500239 .open = ima_ascii_measurements_open,
240 .read = seq_read,
241 .llseek = seq_lseek,
242 .release = seq_release,
243};
244
Mimi Zohar4af46622009-02-04 09:07:00 -0500245static ssize_t ima_write_policy(struct file *file, const char __user *buf,
246 size_t datalen, loff_t *ppos)
247{
Eric Paris6ccd0452010-04-20 10:20:54 -0400248 char *data = NULL;
249 ssize_t result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500250
251 if (datalen >= PAGE_SIZE)
Eric Paris6ccd0452010-04-20 10:20:54 -0400252 datalen = PAGE_SIZE - 1;
253
254 /* No partial writes. */
255 result = -EINVAL;
256 if (*ppos != 0)
257 goto out;
258
259 result = -ENOMEM;
Mimi Zohar4af46622009-02-04 09:07:00 -0500260 data = kmalloc(datalen + 1, GFP_KERNEL);
261 if (!data)
Eric Paris6ccd0452010-04-20 10:20:54 -0400262 goto out;
Mimi Zohar4af46622009-02-04 09:07:00 -0500263
Mimi Zohar4af46622009-02-04 09:07:00 -0500264 *(data + datalen) = '\0';
Mimi Zohar4af46622009-02-04 09:07:00 -0500265
Eric Paris6ccd0452010-04-20 10:20:54 -0400266 result = -EFAULT;
267 if (copy_from_user(data, buf, datalen))
268 goto out;
269
270 result = ima_parse_add_rule(data);
271out:
272 if (result < 0)
273 valid_policy = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500274 kfree(data);
Eric Paris6ccd0452010-04-20 10:20:54 -0400275 return result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500276}
277
Mimi Zoharbab73932009-02-04 09:06:59 -0500278static struct dentry *ima_dir;
279static struct dentry *binary_runtime_measurements;
280static struct dentry *ascii_runtime_measurements;
281static struct dentry *runtime_measurements_count;
282static struct dentry *violations;
Mimi Zohar4af46622009-02-04 09:07:00 -0500283static struct dentry *ima_policy;
284
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500285static atomic_t policy_opencount = ATOMIC_INIT(1);
286/*
287 * ima_open_policy: sequentialize access to the policy file
288 */
James Morrisb97e1452011-08-30 10:18:30 +1000289static int ima_open_policy(struct inode * inode, struct file * filp)
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500290{
Eric Parisf850a7c2009-05-12 15:13:55 -0400291 /* No point in being allowed to open it if you aren't going to write */
292 if (!(filp->f_flags & O_WRONLY))
293 return -EACCES;
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500294 if (atomic_dec_and_test(&policy_opencount))
295 return 0;
296 return -EBUSY;
297}
298
Mimi Zohar4af46622009-02-04 09:07:00 -0500299/*
300 * ima_release_policy - start using the new measure policy rules.
301 *
302 * Initially, ima_measure points to the default policy rules, now
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500303 * point to the new policy rules, and remove the securityfs policy file,
304 * assuming a valid policy.
Mimi Zohar4af46622009-02-04 09:07:00 -0500305 */
306static int ima_release_policy(struct inode *inode, struct file *file)
307{
308 if (!valid_policy) {
309 ima_delete_rules();
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500310 valid_policy = 1;
311 atomic_set(&policy_opencount, 1);
Mimi Zohar4af46622009-02-04 09:07:00 -0500312 return 0;
313 }
314 ima_update_policy();
315 securityfs_remove(ima_policy);
316 ima_policy = NULL;
317 return 0;
318}
319
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700320static const struct file_operations ima_measure_policy_ops = {
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500321 .open = ima_open_policy,
Mimi Zohar4af46622009-02-04 09:07:00 -0500322 .write = ima_write_policy,
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +0200323 .release = ima_release_policy,
324 .llseek = generic_file_llseek,
Mimi Zohar4af46622009-02-04 09:07:00 -0500325};
Mimi Zoharbab73932009-02-04 09:06:59 -0500326
Eric Paris932995f2009-05-21 15:43:32 -0400327int __init ima_fs_init(void)
Mimi Zoharbab73932009-02-04 09:06:59 -0500328{
329 ima_dir = securityfs_create_dir("ima", NULL);
330 if (IS_ERR(ima_dir))
331 return -1;
332
333 binary_runtime_measurements =
334 securityfs_create_file("binary_runtime_measurements",
335 S_IRUSR | S_IRGRP, ima_dir, NULL,
336 &ima_measurements_ops);
337 if (IS_ERR(binary_runtime_measurements))
338 goto out;
339
340 ascii_runtime_measurements =
341 securityfs_create_file("ascii_runtime_measurements",
342 S_IRUSR | S_IRGRP, ima_dir, NULL,
343 &ima_ascii_measurements_ops);
344 if (IS_ERR(ascii_runtime_measurements))
345 goto out;
346
347 runtime_measurements_count =
348 securityfs_create_file("runtime_measurements_count",
349 S_IRUSR | S_IRGRP, ima_dir, NULL,
350 &ima_measurements_count_ops);
351 if (IS_ERR(runtime_measurements_count))
352 goto out;
353
354 violations =
355 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
356 ima_dir, NULL, &ima_htable_violations_ops);
357 if (IS_ERR(violations))
358 goto out;
359
Mimi Zohar4af46622009-02-04 09:07:00 -0500360 ima_policy = securityfs_create_file("policy",
Eric Parisf850a7c2009-05-12 15:13:55 -0400361 S_IWUSR,
Mimi Zohar4af46622009-02-04 09:07:00 -0500362 ima_dir, NULL,
363 &ima_measure_policy_ops);
364 if (IS_ERR(ima_policy))
365 goto out;
Mimi Zoharbab73932009-02-04 09:06:59 -0500366
Mimi Zohar4af46622009-02-04 09:07:00 -0500367 return 0;
Mimi Zoharbab73932009-02-04 09:06:59 -0500368out:
Dmitry Kasatkin0ea4f8a2012-01-29 19:19:08 -0500369 securityfs_remove(violations);
Mimi Zoharbab73932009-02-04 09:06:59 -0500370 securityfs_remove(runtime_measurements_count);
371 securityfs_remove(ascii_runtime_measurements);
372 securityfs_remove(binary_runtime_measurements);
373 securityfs_remove(ima_dir);
Mimi Zohar4af46622009-02-04 09:07:00 -0500374 securityfs_remove(ima_policy);
Mimi Zoharbab73932009-02-04 09:06:59 -0500375 return -1;
376}