2 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
5 * Kylene Hall <kjhall@us.ibm.com>
6 * Reiner Sailer <sailer@us.ibm.com>
7 * Mimi Zohar <zohar@us.ibm.com>
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
15 * implemenents security file system for reporting
16 * current measurement list and IMA statistics
18 #include <linux/fcntl.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/seq_file.h>
22 #include <linux/rculist.h>
23 #include <linux/rcupdate.h>
24 #include <linux/parser.h>
28 static int valid_policy = 1;
30 static ssize_t ima_show_htable_value(char __user *buf, size_t count,
31 loff_t *ppos, atomic_long_t *val)
33 char tmpbuf[TMPBUFLEN];
36 len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
37 return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
40 static ssize_t ima_show_htable_violations(struct file *filp,
42 size_t count, loff_t *ppos)
44 return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
47 static const struct file_operations ima_htable_violations_ops = {
48 .read = ima_show_htable_violations
51 static ssize_t ima_show_measurements_count(struct file *filp,
53 size_t count, loff_t *ppos)
55 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
59 static const struct file_operations ima_measurements_count_ops = {
60 .read = ima_show_measurements_count
63 /* returns pointer to hlist_node */
64 static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
67 struct ima_queue_entry *qe;
69 /* we need a lock since pos could point beyond last element */
71 list_for_each_entry_rcu(qe, &ima_measurements, later) {
81 static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
83 struct ima_queue_entry *qe = v;
85 /* lock protects when reading beyond last element
86 * against concurrent list-extension
89 qe = list_entry_rcu(qe->later.next,
90 struct ima_queue_entry, later);
94 return (&qe->later == &ima_measurements) ? NULL : qe;
97 static void ima_measurements_stop(struct seq_file *m, void *v)
101 static void ima_putc(struct seq_file *m, void *data, int datalen)
104 seq_putc(m, *(char *)data++);
109 * char[20]=template digest
110 * 32bit-le=template name size
111 * char[n]=template name
112 * eventdata[n]=template specific data
114 static int ima_measurements_show(struct seq_file *m, void *v)
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;
120 u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
129 * PCR used is always the same (config option) in
130 * little-endian format
132 ima_putc(m, &pcr, sizeof pcr);
134 /* 2nd: template digest */
135 ima_putc(m, e->digest, IMA_DIGEST_SIZE);
137 /* 3rd: template name size */
138 namelen = strlen(e->template_name);
139 ima_putc(m, &namelen, sizeof namelen);
141 /* 4th: template name */
142 ima_putc(m, (void *)e->template_name, namelen);
144 /* 5th: template specific data */
145 ima_template_show(m, (struct ima_template_data *)&e->template,
150 static const struct seq_operations ima_measurments_seqops = {
151 .start = ima_measurements_start,
152 .next = ima_measurements_next,
153 .stop = ima_measurements_stop,
154 .show = ima_measurements_show
157 static int ima_measurements_open(struct inode *inode, struct file *file)
159 return seq_open(file, &ima_measurments_seqops);
162 static const struct file_operations ima_measurements_ops = {
163 .open = ima_measurements_open,
166 .release = seq_release,
169 static void ima_print_digest(struct seq_file *m, u8 *digest)
173 for (i = 0; i < IMA_DIGEST_SIZE; i++)
174 seq_printf(m, "%02x", *(digest + i));
177 void ima_template_show(struct seq_file *m, void *e, enum ima_show_type show)
179 struct ima_template_data *entry = e;
184 ima_print_digest(m, entry->digest);
185 seq_printf(m, " %s\n", entry->file_name);
187 case IMA_SHOW_BINARY:
188 ima_putc(m, entry->digest, IMA_DIGEST_SIZE);
190 namelen = strlen(entry->file_name);
191 ima_putc(m, &namelen, sizeof namelen);
192 ima_putc(m, entry->file_name, namelen);
199 static int ima_ascii_measurements_show(struct seq_file *m, void *v)
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;
210 /* 1st: PCR used (config option) */
211 seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
213 /* 2nd: SHA1 template hash */
214 ima_print_digest(m, e->digest);
216 /* 3th: template name */
217 seq_printf(m, " %s ", e->template_name);
219 /* 4th: template specific data */
220 ima_template_show(m, (struct ima_template_data *)&e->template,
225 static const struct seq_operations ima_ascii_measurements_seqops = {
226 .start = ima_measurements_start,
227 .next = ima_measurements_next,
228 .stop = ima_measurements_stop,
229 .show = ima_ascii_measurements_show
232 static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
234 return seq_open(file, &ima_ascii_measurements_seqops);
237 static const struct file_operations ima_ascii_measurements_ops = {
238 .open = ima_ascii_measurements_open,
241 .release = seq_release,
244 static ssize_t ima_write_policy(struct file *file, const char __user *buf,
245 size_t datalen, loff_t *ppos)
250 if (datalen >= PAGE_SIZE)
253 /* No partial writes. */
256 data = kmalloc(datalen + 1, GFP_KERNEL);
260 if (copy_from_user(data, buf, datalen)) {
264 *(data + datalen) = '\0';
265 rc = ima_parse_add_rule(data);
275 static struct dentry *ima_dir;
276 static struct dentry *binary_runtime_measurements;
277 static struct dentry *ascii_runtime_measurements;
278 static struct dentry *runtime_measurements_count;
279 static struct dentry *violations;
280 static struct dentry *ima_policy;
282 static atomic_t policy_opencount = ATOMIC_INIT(1);
284 * ima_open_policy: sequentialize access to the policy file
286 int ima_open_policy(struct inode * inode, struct file * filp)
288 /* No point in being allowed to open it if you aren't going to write */
289 if (!(filp->f_flags & O_WRONLY))
291 if (atomic_dec_and_test(&policy_opencount))
297 * ima_release_policy - start using the new measure policy rules.
299 * Initially, ima_measure points to the default policy rules, now
300 * point to the new policy rules, and remove the securityfs policy file,
301 * assuming a valid policy.
303 static int ima_release_policy(struct inode *inode, struct file *file)
308 atomic_set(&policy_opencount, 1);
312 securityfs_remove(ima_policy);
317 static const struct file_operations ima_measure_policy_ops = {
318 .open = ima_open_policy,
319 .write = ima_write_policy,
320 .release = ima_release_policy
323 int __init ima_fs_init(void)
325 ima_dir = securityfs_create_dir("ima", NULL);
329 binary_runtime_measurements =
330 securityfs_create_file("binary_runtime_measurements",
331 S_IRUSR | S_IRGRP, ima_dir, NULL,
332 &ima_measurements_ops);
333 if (IS_ERR(binary_runtime_measurements))
336 ascii_runtime_measurements =
337 securityfs_create_file("ascii_runtime_measurements",
338 S_IRUSR | S_IRGRP, ima_dir, NULL,
339 &ima_ascii_measurements_ops);
340 if (IS_ERR(ascii_runtime_measurements))
343 runtime_measurements_count =
344 securityfs_create_file("runtime_measurements_count",
345 S_IRUSR | S_IRGRP, ima_dir, NULL,
346 &ima_measurements_count_ops);
347 if (IS_ERR(runtime_measurements_count))
351 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
352 ima_dir, NULL, &ima_htable_violations_ops);
353 if (IS_ERR(violations))
356 ima_policy = securityfs_create_file("policy",
359 &ima_measure_policy_ops);
360 if (IS_ERR(ima_policy))
365 securityfs_remove(runtime_measurements_count);
366 securityfs_remove(ascii_runtime_measurements);
367 securityfs_remove(binary_runtime_measurements);
368 securityfs_remove(ima_dir);
369 securityfs_remove(ima_policy);
373 void __exit ima_fs_cleanup(void)
375 securityfs_remove(violations);
376 securityfs_remove(runtime_measurements_count);
377 securityfs_remove(ascii_runtime_measurements);
378 securityfs_remove(binary_runtime_measurements);
379 securityfs_remove(ima_dir);
380 securityfs_remove(ima_policy);