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/module.h>
19 #include <linux/seq_file.h>
20 #include <linux/rculist.h>
21 #include <linux/rcupdate.h>
22 #include <linux/parser.h>
26 static int valid_policy
= 1;
28 static ssize_t
ima_show_htable_value(char __user
*buf
, size_t count
,
29 loff_t
*ppos
, atomic_long_t
*val
)
31 char tmpbuf
[TMPBUFLEN
];
34 len
= scnprintf(tmpbuf
, TMPBUFLEN
, "%li\n", atomic_long_read(val
));
35 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, len
);
38 static ssize_t
ima_show_htable_violations(struct file
*filp
,
40 size_t count
, loff_t
*ppos
)
42 return ima_show_htable_value(buf
, count
, ppos
, &ima_htable
.violations
);
45 static struct file_operations ima_htable_violations_ops
= {
46 .read
= ima_show_htable_violations
49 static ssize_t
ima_show_measurements_count(struct file
*filp
,
51 size_t count
, loff_t
*ppos
)
53 return ima_show_htable_value(buf
, count
, ppos
, &ima_htable
.len
);
57 static struct file_operations ima_measurements_count_ops
= {
58 .read
= ima_show_measurements_count
61 /* returns pointer to hlist_node */
62 static void *ima_measurements_start(struct seq_file
*m
, loff_t
*pos
)
65 struct ima_queue_entry
*qe
;
67 /* we need a lock since pos could point beyond last element */
69 list_for_each_entry_rcu(qe
, &ima_measurements
, later
) {
79 static void *ima_measurements_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
81 struct ima_queue_entry
*qe
= v
;
83 /* lock protects when reading beyond last element
84 * against concurrent list-extension
87 qe
= list_entry(rcu_dereference(qe
->later
.next
),
88 struct ima_queue_entry
, later
);
92 return (&qe
->later
== &ima_measurements
) ? NULL
: qe
;
95 static void ima_measurements_stop(struct seq_file
*m
, void *v
)
99 static void ima_putc(struct seq_file
*m
, void *data
, int datalen
)
102 seq_putc(m
, *(char *)data
++);
107 * char[20]=template digest
108 * 32bit-le=template name size
109 * char[n]=template name
110 * eventdata[n]=template specific data
112 static int ima_measurements_show(struct seq_file
*m
, void *v
)
114 /* the list never shrinks, so we don't need a lock here */
115 struct ima_queue_entry
*qe
= v
;
116 struct ima_template_entry
*e
;
118 u32 pcr
= CONFIG_IMA_MEASURE_PCR_IDX
;
127 * PCR used is always the same (config option) in
128 * little-endian format
130 ima_putc(m
, &pcr
, sizeof pcr
);
132 /* 2nd: template digest */
133 ima_putc(m
, e
->digest
, IMA_DIGEST_SIZE
);
135 /* 3rd: template name size */
136 namelen
= strlen(e
->template_name
);
137 ima_putc(m
, &namelen
, sizeof namelen
);
139 /* 4th: template name */
140 ima_putc(m
, (void *)e
->template_name
, namelen
);
142 /* 5th: template specific data */
143 ima_template_show(m
, (struct ima_template_data
*)&e
->template,
148 static struct seq_operations ima_measurments_seqops
= {
149 .start
= ima_measurements_start
,
150 .next
= ima_measurements_next
,
151 .stop
= ima_measurements_stop
,
152 .show
= ima_measurements_show
155 static int ima_measurements_open(struct inode
*inode
, struct file
*file
)
157 return seq_open(file
, &ima_measurments_seqops
);
160 static struct file_operations ima_measurements_ops
= {
161 .open
= ima_measurements_open
,
164 .release
= seq_release
,
167 static void ima_print_digest(struct seq_file
*m
, u8
*digest
)
171 for (i
= 0; i
< IMA_DIGEST_SIZE
; i
++)
172 seq_printf(m
, "%02x", *(digest
+ i
));
175 void ima_template_show(struct seq_file
*m
, void *e
, enum ima_show_type show
)
177 struct ima_template_data
*entry
= e
;
182 ima_print_digest(m
, entry
->digest
);
183 seq_printf(m
, " %s\n", entry
->file_name
);
185 case IMA_SHOW_BINARY
:
186 ima_putc(m
, entry
->digest
, IMA_DIGEST_SIZE
);
188 namelen
= strlen(entry
->file_name
);
189 ima_putc(m
, &namelen
, sizeof namelen
);
190 ima_putc(m
, entry
->file_name
, namelen
);
197 static int ima_ascii_measurements_show(struct seq_file
*m
, void *v
)
199 /* the list never shrinks, so we don't need a lock here */
200 struct ima_queue_entry
*qe
= v
;
201 struct ima_template_entry
*e
;
208 /* 1st: PCR used (config option) */
209 seq_printf(m
, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX
);
211 /* 2nd: SHA1 template hash */
212 ima_print_digest(m
, e
->digest
);
214 /* 3th: template name */
215 seq_printf(m
, " %s ", e
->template_name
);
217 /* 4th: template specific data */
218 ima_template_show(m
, (struct ima_template_data
*)&e
->template,
223 static struct seq_operations ima_ascii_measurements_seqops
= {
224 .start
= ima_measurements_start
,
225 .next
= ima_measurements_next
,
226 .stop
= ima_measurements_stop
,
227 .show
= ima_ascii_measurements_show
230 static int ima_ascii_measurements_open(struct inode
*inode
, struct file
*file
)
232 return seq_open(file
, &ima_ascii_measurements_seqops
);
235 static struct file_operations ima_ascii_measurements_ops
= {
236 .open
= ima_ascii_measurements_open
,
239 .release
= seq_release
,
242 static ssize_t
ima_write_policy(struct file
*file
, const char __user
*buf
,
243 size_t datalen
, loff_t
*ppos
)
248 if (datalen
>= PAGE_SIZE
)
251 /* No partial writes. */
254 data
= kmalloc(datalen
+ 1, GFP_KERNEL
);
258 if (copy_from_user(data
, buf
, datalen
)) {
262 *(data
+ datalen
) = '\0';
263 rc
= ima_parse_add_rule(data
);
273 static struct dentry
*ima_dir
;
274 static struct dentry
*binary_runtime_measurements
;
275 static struct dentry
*ascii_runtime_measurements
;
276 static struct dentry
*runtime_measurements_count
;
277 static struct dentry
*violations
;
278 static struct dentry
*ima_policy
;
280 static atomic_t policy_opencount
= ATOMIC_INIT(1);
282 * ima_open_policy: sequentialize access to the policy file
284 int ima_open_policy(struct inode
* inode
, struct file
* filp
)
286 if (atomic_dec_and_test(&policy_opencount
))
292 * ima_release_policy - start using the new measure policy rules.
294 * Initially, ima_measure points to the default policy rules, now
295 * point to the new policy rules, and remove the securityfs policy file,
296 * assuming a valid policy.
298 static int ima_release_policy(struct inode
*inode
, struct file
*file
)
303 atomic_set(&policy_opencount
, 1);
307 securityfs_remove(ima_policy
);
312 static struct file_operations ima_measure_policy_ops
= {
313 .open
= ima_open_policy
,
314 .write
= ima_write_policy
,
315 .release
= ima_release_policy
318 int ima_fs_init(void)
320 ima_dir
= securityfs_create_dir("ima", NULL
);
324 binary_runtime_measurements
=
325 securityfs_create_file("binary_runtime_measurements",
326 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
327 &ima_measurements_ops
);
328 if (IS_ERR(binary_runtime_measurements
))
331 ascii_runtime_measurements
=
332 securityfs_create_file("ascii_runtime_measurements",
333 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
334 &ima_ascii_measurements_ops
);
335 if (IS_ERR(ascii_runtime_measurements
))
338 runtime_measurements_count
=
339 securityfs_create_file("runtime_measurements_count",
340 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
341 &ima_measurements_count_ops
);
342 if (IS_ERR(runtime_measurements_count
))
346 securityfs_create_file("violations", S_IRUSR
| S_IRGRP
,
347 ima_dir
, NULL
, &ima_htable_violations_ops
);
348 if (IS_ERR(violations
))
351 ima_policy
= securityfs_create_file("policy",
352 S_IRUSR
| S_IRGRP
| S_IWUSR
,
354 &ima_measure_policy_ops
);
355 if (IS_ERR(ima_policy
))
360 securityfs_remove(runtime_measurements_count
);
361 securityfs_remove(ascii_runtime_measurements
);
362 securityfs_remove(binary_runtime_measurements
);
363 securityfs_remove(ima_dir
);
364 securityfs_remove(ima_policy
);
368 void __exit
ima_fs_cleanup(void)
370 securityfs_remove(violations
);
371 securityfs_remove(runtime_measurements_count
);
372 securityfs_remove(ascii_runtime_measurements
);
373 securityfs_remove(binary_runtime_measurements
);
374 securityfs_remove(ima_dir
);
375 securityfs_remove(ima_policy
);