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>
25 #include <linux/vmalloc.h>
29 static DEFINE_MUTEX(ima_write_mutex);
31 static int valid_policy = 1;
33 static ssize_t ima_show_htable_value(char __user *buf, size_t count,
34 loff_t *ppos, atomic_long_t *val)
36 char tmpbuf[TMPBUFLEN];
39 len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
40 return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
43 static ssize_t ima_show_htable_violations(struct file *filp,
45 size_t count, loff_t *ppos)
47 return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
50 static const struct file_operations ima_htable_violations_ops = {
51 .read = ima_show_htable_violations,
52 .llseek = generic_file_llseek,
55 static ssize_t ima_show_measurements_count(struct file *filp,
57 size_t count, loff_t *ppos)
59 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
63 static const struct file_operations ima_measurements_count_ops = {
64 .read = ima_show_measurements_count,
65 .llseek = generic_file_llseek,
68 /* returns pointer to hlist_node */
69 static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
72 struct ima_queue_entry *qe;
74 /* we need a lock since pos could point beyond last element */
76 list_for_each_entry_rcu(qe, &ima_measurements, later) {
86 static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
88 struct ima_queue_entry *qe = v;
90 /* lock protects when reading beyond last element
91 * against concurrent list-extension
94 qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
98 return (&qe->later == &ima_measurements) ? NULL : qe;
101 static void ima_measurements_stop(struct seq_file *m, void *v)
105 void ima_putc(struct seq_file *m, void *data, int datalen)
108 seq_putc(m, *(char *)data++);
113 * char[20]=template digest
114 * 32bit-le=template name size
115 * char[n]=template name
117 * eventdata[n]=template specific data
119 static int ima_measurements_show(struct seq_file *m, void *v)
121 /* the list never shrinks, so we don't need a lock here */
122 struct ima_queue_entry *qe = v;
123 struct ima_template_entry *e;
126 u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
127 bool is_ima_template = false;
135 template_name = (e->template_desc->name[0] != '\0') ?
136 e->template_desc->name : e->template_desc->fmt;
140 * PCR used is always the same (config option) in
141 * little-endian format
143 ima_putc(m, &pcr, sizeof(pcr));
145 /* 2nd: template digest */
146 ima_putc(m, e->digest, TPM_DIGEST_SIZE);
148 /* 3rd: template name size */
149 namelen = strlen(template_name);
150 ima_putc(m, &namelen, sizeof(namelen));
152 /* 4th: template name */
153 ima_putc(m, template_name, namelen);
155 /* 5th: template length (except for 'ima' template) */
156 if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
157 is_ima_template = true;
159 if (!is_ima_template)
160 ima_putc(m, &e->template_data_len,
161 sizeof(e->template_data_len));
163 /* 6th: template specific data */
164 for (i = 0; i < e->template_desc->num_fields; i++) {
165 enum ima_show_type show = IMA_SHOW_BINARY;
166 struct ima_template_field *field = e->template_desc->fields[i];
168 if (is_ima_template && strcmp(field->field_id, "d") == 0)
169 show = IMA_SHOW_BINARY_NO_FIELD_LEN;
170 if (is_ima_template && strcmp(field->field_id, "n") == 0)
171 show = IMA_SHOW_BINARY_OLD_STRING_FMT;
172 field->field_show(m, show, &e->template_data[i]);
177 static const struct seq_operations ima_measurments_seqops = {
178 .start = ima_measurements_start,
179 .next = ima_measurements_next,
180 .stop = ima_measurements_stop,
181 .show = ima_measurements_show
184 static int ima_measurements_open(struct inode *inode, struct file *file)
186 return seq_open(file, &ima_measurments_seqops);
189 static const struct file_operations ima_measurements_ops = {
190 .open = ima_measurements_open,
193 .release = seq_release,
196 void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
200 for (i = 0; i < size; i++)
201 seq_printf(m, "%02x", *(digest + i));
205 static int ima_ascii_measurements_show(struct seq_file *m, void *v)
207 /* the list never shrinks, so we don't need a lock here */
208 struct ima_queue_entry *qe = v;
209 struct ima_template_entry *e;
218 template_name = (e->template_desc->name[0] != '\0') ?
219 e->template_desc->name : e->template_desc->fmt;
221 /* 1st: PCR used (config option) */
222 seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
224 /* 2nd: SHA1 template hash */
225 ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
227 /* 3th: template name */
228 seq_printf(m, " %s", template_name);
230 /* 4th: template specific data */
231 for (i = 0; i < e->template_desc->num_fields; i++) {
233 if (e->template_data[i].len == 0)
236 e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
237 &e->template_data[i]);
243 static const struct seq_operations ima_ascii_measurements_seqops = {
244 .start = ima_measurements_start,
245 .next = ima_measurements_next,
246 .stop = ima_measurements_stop,
247 .show = ima_ascii_measurements_show
250 static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
252 return seq_open(file, &ima_ascii_measurements_seqops);
255 static const struct file_operations ima_ascii_measurements_ops = {
256 .open = ima_ascii_measurements_open,
259 .release = seq_release,
262 static ssize_t ima_read_policy(char *path)
267 int rc, pathlen = strlen(path);
273 strsep(&datap, "\n");
275 rc = kernel_read_file_from_path(path, &data, &size, 0, READING_POLICY);
277 pr_err("Unable to open file: %s (%d)", path, rc);
282 while (size > 0 && (p = strsep(&datap, "\n"))) {
283 pr_debug("rule: %s\n", p);
284 rc = ima_parse_add_rule(p);
299 static ssize_t ima_write_policy(struct file *file, const char __user *buf,
300 size_t datalen, loff_t *ppos)
305 if (datalen >= PAGE_SIZE)
306 datalen = PAGE_SIZE - 1;
308 /* No partial writes. */
314 data = kmalloc(datalen + 1, GFP_KERNEL);
318 *(data + datalen) = '\0';
321 if (copy_from_user(data, buf, datalen))
324 result = mutex_lock_interruptible(&ima_write_mutex);
328 if (data[0] == '/') {
329 result = ima_read_policy(data);
330 } else if (ima_appraise & IMA_APPRAISE_POLICY) {
331 pr_err("IMA: signed policy file (specified as an absolute pathname) required\n");
332 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
333 "policy_update", "signed policy required",
335 if (ima_appraise & IMA_APPRAISE_ENFORCE)
338 result = ima_parse_add_rule(data);
340 mutex_unlock(&ima_write_mutex);
350 static struct dentry *ima_dir;
351 static struct dentry *binary_runtime_measurements;
352 static struct dentry *ascii_runtime_measurements;
353 static struct dentry *runtime_measurements_count;
354 static struct dentry *violations;
355 static struct dentry *ima_policy;
361 static unsigned long ima_fs_flags;
363 #ifdef CONFIG_IMA_READ_POLICY
364 static const struct seq_operations ima_policy_seqops = {
365 .start = ima_policy_start,
366 .next = ima_policy_next,
367 .stop = ima_policy_stop,
368 .show = ima_policy_show,
373 * ima_open_policy: sequentialize access to the policy file
375 static int ima_open_policy(struct inode *inode, struct file *filp)
377 if (!(filp->f_flags & O_WRONLY)) {
378 #ifndef CONFIG_IMA_READ_POLICY
381 if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
383 if (!capable(CAP_SYS_ADMIN))
385 return seq_open(filp, &ima_policy_seqops);
388 if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
394 * ima_release_policy - start using the new measure policy rules.
396 * Initially, ima_measure points to the default policy rules, now
397 * point to the new policy rules, and remove the securityfs policy file,
398 * assuming a valid policy.
400 static int ima_release_policy(struct inode *inode, struct file *file)
402 const char *cause = valid_policy ? "completed" : "failed";
404 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
407 if (valid_policy && ima_check_policy() < 0) {
412 pr_info("IMA: policy update %s\n", cause);
413 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
414 "policy_update", cause, !valid_policy, 0);
419 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
424 #ifndef CONFIG_IMA_WRITE_POLICY
425 securityfs_remove(ima_policy);
428 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
433 static const struct file_operations ima_measure_policy_ops = {
434 .open = ima_open_policy,
435 .write = ima_write_policy,
437 .release = ima_release_policy,
438 .llseek = generic_file_llseek,
441 int __init ima_fs_init(void)
443 ima_dir = securityfs_create_dir("ima", NULL);
447 binary_runtime_measurements =
448 securityfs_create_file("binary_runtime_measurements",
449 S_IRUSR | S_IRGRP, ima_dir, NULL,
450 &ima_measurements_ops);
451 if (IS_ERR(binary_runtime_measurements))
454 ascii_runtime_measurements =
455 securityfs_create_file("ascii_runtime_measurements",
456 S_IRUSR | S_IRGRP, ima_dir, NULL,
457 &ima_ascii_measurements_ops);
458 if (IS_ERR(ascii_runtime_measurements))
461 runtime_measurements_count =
462 securityfs_create_file("runtime_measurements_count",
463 S_IRUSR | S_IRGRP, ima_dir, NULL,
464 &ima_measurements_count_ops);
465 if (IS_ERR(runtime_measurements_count))
469 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
470 ima_dir, NULL, &ima_htable_violations_ops);
471 if (IS_ERR(violations))
474 ima_policy = securityfs_create_file("policy", POLICY_FILE_FLAGS,
476 &ima_measure_policy_ops);
477 if (IS_ERR(ima_policy))
482 securityfs_remove(violations);
483 securityfs_remove(runtime_measurements_count);
484 securityfs_remove(ascii_runtime_measurements);
485 securityfs_remove(binary_runtime_measurements);
486 securityfs_remove(ima_dir);
487 securityfs_remove(ima_policy);