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
,
49 .llseek
= generic_file_llseek
,
52 static ssize_t
ima_show_measurements_count(struct file
*filp
,
54 size_t count
, loff_t
*ppos
)
56 return ima_show_htable_value(buf
, count
, ppos
, &ima_htable
.len
);
60 static const struct file_operations ima_measurements_count_ops
= {
61 .read
= ima_show_measurements_count
,
62 .llseek
= generic_file_llseek
,
65 /* returns pointer to hlist_node */
66 static void *ima_measurements_start(struct seq_file
*m
, loff_t
*pos
)
69 struct ima_queue_entry
*qe
;
71 /* we need a lock since pos could point beyond last element */
73 list_for_each_entry_rcu(qe
, &ima_measurements
, later
) {
83 static void *ima_measurements_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
85 struct ima_queue_entry
*qe
= v
;
87 /* lock protects when reading beyond last element
88 * against concurrent list-extension
91 qe
= list_entry_rcu(qe
->later
.next
, struct ima_queue_entry
, later
);
95 return (&qe
->later
== &ima_measurements
) ? NULL
: qe
;
98 static void ima_measurements_stop(struct seq_file
*m
, void *v
)
102 void ima_putc(struct seq_file
*m
, void *data
, int datalen
)
105 seq_putc(m
, *(char *)data
++);
110 * char[20]=template digest
111 * 32bit-le=template name size
112 * char[n]=template name
114 * eventdata[n]=template specific data
116 static int ima_measurements_show(struct seq_file
*m
, void *v
)
118 /* the list never shrinks, so we don't need a lock here */
119 struct ima_queue_entry
*qe
= v
;
120 struct ima_template_entry
*e
;
123 u32 pcr
= CONFIG_IMA_MEASURE_PCR_IDX
;
124 bool is_ima_template
= false;
132 template_name
= (e
->template_desc
->name
[0] != '\0') ?
133 e
->template_desc
->name
: e
->template_desc
->fmt
;
137 * PCR used is always the same (config option) in
138 * little-endian format
140 ima_putc(m
, &pcr
, sizeof(pcr
));
142 /* 2nd: template digest */
143 ima_putc(m
, e
->digest
, TPM_DIGEST_SIZE
);
145 /* 3rd: template name size */
146 namelen
= strlen(template_name
);
147 ima_putc(m
, &namelen
, sizeof(namelen
));
149 /* 4th: template name */
150 ima_putc(m
, template_name
, namelen
);
152 /* 5th: template length (except for 'ima' template) */
153 if (strcmp(template_name
, IMA_TEMPLATE_IMA_NAME
) == 0)
154 is_ima_template
= true;
156 if (!is_ima_template
)
157 ima_putc(m
, &e
->template_data_len
,
158 sizeof(e
->template_data_len
));
160 /* 6th: template specific data */
161 for (i
= 0; i
< e
->template_desc
->num_fields
; i
++) {
162 enum ima_show_type show
= IMA_SHOW_BINARY
;
163 struct ima_template_field
*field
= e
->template_desc
->fields
[i
];
165 if (is_ima_template
&& strcmp(field
->field_id
, "d") == 0)
166 show
= IMA_SHOW_BINARY_NO_FIELD_LEN
;
167 if (is_ima_template
&& strcmp(field
->field_id
, "n") == 0)
168 show
= IMA_SHOW_BINARY_OLD_STRING_FMT
;
169 field
->field_show(m
, show
, &e
->template_data
[i
]);
174 static const struct seq_operations ima_measurments_seqops
= {
175 .start
= ima_measurements_start
,
176 .next
= ima_measurements_next
,
177 .stop
= ima_measurements_stop
,
178 .show
= ima_measurements_show
181 static int ima_measurements_open(struct inode
*inode
, struct file
*file
)
183 return seq_open(file
, &ima_measurments_seqops
);
186 static const struct file_operations ima_measurements_ops
= {
187 .open
= ima_measurements_open
,
190 .release
= seq_release
,
193 void ima_print_digest(struct seq_file
*m
, u8
*digest
, u32 size
)
197 for (i
= 0; i
< size
; i
++)
198 seq_printf(m
, "%02x", *(digest
+ i
));
202 static int ima_ascii_measurements_show(struct seq_file
*m
, void *v
)
204 /* the list never shrinks, so we don't need a lock here */
205 struct ima_queue_entry
*qe
= v
;
206 struct ima_template_entry
*e
;
215 template_name
= (e
->template_desc
->name
[0] != '\0') ?
216 e
->template_desc
->name
: e
->template_desc
->fmt
;
218 /* 1st: PCR used (config option) */
219 seq_printf(m
, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX
);
221 /* 2nd: SHA1 template hash */
222 ima_print_digest(m
, e
->digest
, TPM_DIGEST_SIZE
);
224 /* 3th: template name */
225 seq_printf(m
, " %s", template_name
);
227 /* 4th: template specific data */
228 for (i
= 0; i
< e
->template_desc
->num_fields
; i
++) {
230 if (e
->template_data
[i
].len
== 0)
233 e
->template_desc
->fields
[i
]->field_show(m
, IMA_SHOW_ASCII
,
234 &e
->template_data
[i
]);
240 static const struct seq_operations ima_ascii_measurements_seqops
= {
241 .start
= ima_measurements_start
,
242 .next
= ima_measurements_next
,
243 .stop
= ima_measurements_stop
,
244 .show
= ima_ascii_measurements_show
247 static int ima_ascii_measurements_open(struct inode
*inode
, struct file
*file
)
249 return seq_open(file
, &ima_ascii_measurements_seqops
);
252 static const struct file_operations ima_ascii_measurements_ops
= {
253 .open
= ima_ascii_measurements_open
,
256 .release
= seq_release
,
259 static ssize_t
ima_write_policy(struct file
*file
, const char __user
*buf
,
260 size_t datalen
, loff_t
*ppos
)
265 if (datalen
>= PAGE_SIZE
)
266 datalen
= PAGE_SIZE
- 1;
268 /* No partial writes. */
274 data
= kmalloc(datalen
+ 1, GFP_KERNEL
);
278 *(data
+ datalen
) = '\0';
281 if (copy_from_user(data
, buf
, datalen
))
284 result
= ima_parse_add_rule(data
);
292 static struct dentry
*ima_dir
;
293 static struct dentry
*binary_runtime_measurements
;
294 static struct dentry
*ascii_runtime_measurements
;
295 static struct dentry
*runtime_measurements_count
;
296 static struct dentry
*violations
;
297 static struct dentry
*ima_policy
;
303 static unsigned long ima_fs_flags
;
306 * ima_open_policy: sequentialize access to the policy file
308 static int ima_open_policy(struct inode
*inode
, struct file
*filp
)
310 /* No point in being allowed to open it if you aren't going to write */
311 if (!(filp
->f_flags
& O_WRONLY
))
313 if (test_and_set_bit(IMA_FS_BUSY
, &ima_fs_flags
))
319 * ima_release_policy - start using the new measure policy rules.
321 * Initially, ima_measure points to the default policy rules, now
322 * point to the new policy rules, and remove the securityfs policy file,
323 * assuming a valid policy.
325 static int ima_release_policy(struct inode
*inode
, struct file
*file
)
327 const char *cause
= valid_policy
? "completed" : "failed";
329 pr_info("IMA: policy update %s\n", cause
);
330 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
, NULL
,
331 "policy_update", cause
, !valid_policy
, 0);
336 clear_bit(IMA_FS_BUSY
, &ima_fs_flags
);
340 securityfs_remove(ima_policy
);
345 static const struct file_operations ima_measure_policy_ops
= {
346 .open
= ima_open_policy
,
347 .write
= ima_write_policy
,
348 .release
= ima_release_policy
,
349 .llseek
= generic_file_llseek
,
352 int __init
ima_fs_init(void)
354 ima_dir
= securityfs_create_dir("ima", NULL
);
358 binary_runtime_measurements
=
359 securityfs_create_file("binary_runtime_measurements",
360 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
361 &ima_measurements_ops
);
362 if (IS_ERR(binary_runtime_measurements
))
365 ascii_runtime_measurements
=
366 securityfs_create_file("ascii_runtime_measurements",
367 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
368 &ima_ascii_measurements_ops
);
369 if (IS_ERR(ascii_runtime_measurements
))
372 runtime_measurements_count
=
373 securityfs_create_file("runtime_measurements_count",
374 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
375 &ima_measurements_count_ops
);
376 if (IS_ERR(runtime_measurements_count
))
380 securityfs_create_file("violations", S_IRUSR
| S_IRGRP
,
381 ima_dir
, NULL
, &ima_htable_violations_ops
);
382 if (IS_ERR(violations
))
385 ima_policy
= securityfs_create_file("policy",
388 &ima_measure_policy_ops
);
389 if (IS_ERR(ima_policy
))
394 securityfs_remove(violations
);
395 securityfs_remove(runtime_measurements_count
);
396 securityfs_remove(ascii_runtime_measurements
);
397 securityfs_remove(binary_runtime_measurements
);
398 securityfs_remove(ima_dir
);
399 securityfs_remove(ima_policy
);