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/module.h>
20 #include <linux/seq_file.h>
21 #include <linux/rculist.h>
22 #include <linux/rcupdate.h>
23 #include <linux/parser.h>
27 static int valid_policy
= 1;
29 static ssize_t
ima_show_htable_value(char __user
*buf
, size_t count
,
30 loff_t
*ppos
, atomic_long_t
*val
)
32 char tmpbuf
[TMPBUFLEN
];
35 len
= scnprintf(tmpbuf
, TMPBUFLEN
, "%li\n", atomic_long_read(val
));
36 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, len
);
39 static ssize_t
ima_show_htable_violations(struct file
*filp
,
41 size_t count
, loff_t
*ppos
)
43 return ima_show_htable_value(buf
, count
, ppos
, &ima_htable
.violations
);
46 static const struct file_operations ima_htable_violations_ops
= {
47 .read
= ima_show_htable_violations
50 static ssize_t
ima_show_measurements_count(struct file
*filp
,
52 size_t count
, loff_t
*ppos
)
54 return ima_show_htable_value(buf
, count
, ppos
, &ima_htable
.len
);
58 static const struct file_operations ima_measurements_count_ops
= {
59 .read
= ima_show_measurements_count
62 /* returns pointer to hlist_node */
63 static void *ima_measurements_start(struct seq_file
*m
, loff_t
*pos
)
66 struct ima_queue_entry
*qe
;
68 /* we need a lock since pos could point beyond last element */
70 list_for_each_entry_rcu(qe
, &ima_measurements
, later
) {
80 static void *ima_measurements_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
82 struct ima_queue_entry
*qe
= v
;
84 /* lock protects when reading beyond last element
85 * against concurrent list-extension
88 qe
= list_entry_rcu(qe
->later
.next
,
89 struct ima_queue_entry
, later
);
93 return (&qe
->later
== &ima_measurements
) ? NULL
: qe
;
96 static void ima_measurements_stop(struct seq_file
*m
, void *v
)
100 static void ima_putc(struct seq_file
*m
, void *data
, int datalen
)
103 seq_putc(m
, *(char *)data
++);
108 * char[20]=template digest
109 * 32bit-le=template name size
110 * char[n]=template name
111 * eventdata[n]=template specific data
113 static int ima_measurements_show(struct seq_file
*m
, void *v
)
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
;
119 u32 pcr
= CONFIG_IMA_MEASURE_PCR_IDX
;
128 * PCR used is always the same (config option) in
129 * little-endian format
131 ima_putc(m
, &pcr
, sizeof pcr
);
133 /* 2nd: template digest */
134 ima_putc(m
, e
->digest
, IMA_DIGEST_SIZE
);
136 /* 3rd: template name size */
137 namelen
= strlen(e
->template_name
);
138 ima_putc(m
, &namelen
, sizeof namelen
);
140 /* 4th: template name */
141 ima_putc(m
, (void *)e
->template_name
, namelen
);
143 /* 5th: template specific data */
144 ima_template_show(m
, (struct ima_template_data
*)&e
->template,
149 static const struct seq_operations ima_measurments_seqops
= {
150 .start
= ima_measurements_start
,
151 .next
= ima_measurements_next
,
152 .stop
= ima_measurements_stop
,
153 .show
= ima_measurements_show
156 static int ima_measurements_open(struct inode
*inode
, struct file
*file
)
158 return seq_open(file
, &ima_measurments_seqops
);
161 static const struct file_operations ima_measurements_ops
= {
162 .open
= ima_measurements_open
,
165 .release
= seq_release
,
168 static void ima_print_digest(struct seq_file
*m
, u8
*digest
)
172 for (i
= 0; i
< IMA_DIGEST_SIZE
; i
++)
173 seq_printf(m
, "%02x", *(digest
+ i
));
176 void ima_template_show(struct seq_file
*m
, void *e
, enum ima_show_type show
)
178 struct ima_template_data
*entry
= e
;
183 ima_print_digest(m
, entry
->digest
);
184 seq_printf(m
, " %s\n", entry
->file_name
);
186 case IMA_SHOW_BINARY
:
187 ima_putc(m
, entry
->digest
, IMA_DIGEST_SIZE
);
189 namelen
= strlen(entry
->file_name
);
190 ima_putc(m
, &namelen
, sizeof namelen
);
191 ima_putc(m
, entry
->file_name
, namelen
);
198 static int ima_ascii_measurements_show(struct seq_file
*m
, void *v
)
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
;
209 /* 1st: PCR used (config option) */
210 seq_printf(m
, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX
);
212 /* 2nd: SHA1 template hash */
213 ima_print_digest(m
, e
->digest
);
215 /* 3th: template name */
216 seq_printf(m
, " %s ", e
->template_name
);
218 /* 4th: template specific data */
219 ima_template_show(m
, (struct ima_template_data
*)&e
->template,
224 static const struct seq_operations ima_ascii_measurements_seqops
= {
225 .start
= ima_measurements_start
,
226 .next
= ima_measurements_next
,
227 .stop
= ima_measurements_stop
,
228 .show
= ima_ascii_measurements_show
231 static int ima_ascii_measurements_open(struct inode
*inode
, struct file
*file
)
233 return seq_open(file
, &ima_ascii_measurements_seqops
);
236 static const struct file_operations ima_ascii_measurements_ops
= {
237 .open
= ima_ascii_measurements_open
,
240 .release
= seq_release
,
243 static ssize_t
ima_write_policy(struct file
*file
, const char __user
*buf
,
244 size_t datalen
, loff_t
*ppos
)
249 if (datalen
>= PAGE_SIZE
)
252 /* No partial writes. */
255 data
= kmalloc(datalen
+ 1, GFP_KERNEL
);
259 if (copy_from_user(data
, buf
, datalen
)) {
263 *(data
+ datalen
) = '\0';
264 rc
= ima_parse_add_rule(data
);
274 static struct dentry
*ima_dir
;
275 static struct dentry
*binary_runtime_measurements
;
276 static struct dentry
*ascii_runtime_measurements
;
277 static struct dentry
*runtime_measurements_count
;
278 static struct dentry
*violations
;
279 static struct dentry
*ima_policy
;
281 static atomic_t policy_opencount
= ATOMIC_INIT(1);
283 * ima_open_policy: sequentialize access to the policy file
285 int ima_open_policy(struct inode
* inode
, struct file
* filp
)
287 /* No point in being allowed to open it if you aren't going to write */
288 if (!(filp
->f_flags
& O_WRONLY
))
290 if (atomic_dec_and_test(&policy_opencount
))
296 * ima_release_policy - start using the new measure policy rules.
298 * Initially, ima_measure points to the default policy rules, now
299 * point to the new policy rules, and remove the securityfs policy file,
300 * assuming a valid policy.
302 static int ima_release_policy(struct inode
*inode
, struct file
*file
)
307 atomic_set(&policy_opencount
, 1);
311 securityfs_remove(ima_policy
);
316 static const struct file_operations ima_measure_policy_ops
= {
317 .open
= ima_open_policy
,
318 .write
= ima_write_policy
,
319 .release
= ima_release_policy
322 int __init
ima_fs_init(void)
324 ima_dir
= securityfs_create_dir("ima", NULL
);
328 binary_runtime_measurements
=
329 securityfs_create_file("binary_runtime_measurements",
330 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
331 &ima_measurements_ops
);
332 if (IS_ERR(binary_runtime_measurements
))
335 ascii_runtime_measurements
=
336 securityfs_create_file("ascii_runtime_measurements",
337 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
338 &ima_ascii_measurements_ops
);
339 if (IS_ERR(ascii_runtime_measurements
))
342 runtime_measurements_count
=
343 securityfs_create_file("runtime_measurements_count",
344 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
345 &ima_measurements_count_ops
);
346 if (IS_ERR(runtime_measurements_count
))
350 securityfs_create_file("violations", S_IRUSR
| S_IRGRP
,
351 ima_dir
, NULL
, &ima_htable_violations_ops
);
352 if (IS_ERR(violations
))
355 ima_policy
= securityfs_create_file("policy",
358 &ima_measure_policy_ops
);
359 if (IS_ERR(ima_policy
))
364 securityfs_remove(runtime_measurements_count
);
365 securityfs_remove(ascii_runtime_measurements
);
366 securityfs_remove(binary_runtime_measurements
);
367 securityfs_remove(ima_dir
);
368 securityfs_remove(ima_policy
);
372 void __exit
ima_fs_cleanup(void)
374 securityfs_remove(violations
);
375 securityfs_remove(runtime_measurements_count
);
376 securityfs_remove(ascii_runtime_measurements
);
377 securityfs_remove(binary_runtime_measurements
);
378 securityfs_remove(ima_dir
);
379 securityfs_remove(ima_policy
);