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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/fcntl.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/seq_file.h>
25 #include <linux/rculist.h>
26 #include <linux/rcupdate.h>
27 #include <linux/parser.h>
28 #include <linux/vmalloc.h>
32 static DEFINE_MUTEX(ima_write_mutex);
34 bool ima_canonical_fmt;
35 static int __init default_canonical_fmt_setup(char *str)
38 ima_canonical_fmt = true;
42 __setup("ima_canonical_fmt", default_canonical_fmt_setup);
44 static int valid_policy = 1;
46 static ssize_t ima_show_htable_value(char __user *buf, size_t count,
47 loff_t *ppos, atomic_long_t *val)
49 char tmpbuf[32]; /* greater than largest 'long' string value */
52 len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", atomic_long_read(val));
53 return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
56 static ssize_t ima_show_htable_violations(struct file *filp,
58 size_t count, loff_t *ppos)
60 return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
63 static const struct file_operations ima_htable_violations_ops = {
64 .read = ima_show_htable_violations,
65 .llseek = generic_file_llseek,
68 static ssize_t ima_show_measurements_count(struct file *filp,
70 size_t count, loff_t *ppos)
72 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
76 static const struct file_operations ima_measurements_count_ops = {
77 .read = ima_show_measurements_count,
78 .llseek = generic_file_llseek,
81 /* returns pointer to hlist_node */
82 static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
85 struct ima_queue_entry *qe;
87 /* we need a lock since pos could point beyond last element */
89 list_for_each_entry_rcu(qe, &ima_measurements, later) {
99 static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
101 struct ima_queue_entry *qe = v;
103 /* lock protects when reading beyond last element
104 * against concurrent list-extension
107 qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
111 return (&qe->later == &ima_measurements) ? NULL : qe;
114 static void ima_measurements_stop(struct seq_file *m, void *v)
118 void ima_putc(struct seq_file *m, void *data, int datalen)
121 seq_putc(m, *(char *)data++);
126 * char[20]=template digest
127 * 32bit-le=template name size
128 * char[n]=template name
130 * eventdata[n]=template specific data
132 int ima_measurements_show(struct seq_file *m, void *v)
134 /* the list never shrinks, so we don't need a lock here */
135 struct ima_queue_entry *qe = v;
136 struct ima_template_entry *e;
138 u32 pcr, namelen, template_data_len; /* temporary fields */
139 bool is_ima_template = false;
147 template_name = (e->template_desc->name[0] != '\0') ?
148 e->template_desc->name : e->template_desc->fmt;
152 * PCR used defaults to the same (config option) in
153 * little-endian format, unless set in policy
155 pcr = !ima_canonical_fmt ? e->pcr : cpu_to_le32(e->pcr);
156 ima_putc(m, &pcr, sizeof(e->pcr));
158 /* 2nd: template digest */
159 ima_putc(m, e->digest, TPM_DIGEST_SIZE);
161 /* 3rd: template name size */
162 namelen = !ima_canonical_fmt ? strlen(template_name) :
163 cpu_to_le32(strlen(template_name));
164 ima_putc(m, &namelen, sizeof(namelen));
166 /* 4th: template name */
167 ima_putc(m, template_name, strlen(template_name));
169 /* 5th: template length (except for 'ima' template) */
170 if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
171 is_ima_template = true;
173 if (!is_ima_template) {
174 template_data_len = !ima_canonical_fmt ? e->template_data_len :
175 cpu_to_le32(e->template_data_len);
176 ima_putc(m, &template_data_len, sizeof(e->template_data_len));
179 /* 6th: template specific data */
180 for (i = 0; i < e->template_desc->num_fields; i++) {
181 enum ima_show_type show = IMA_SHOW_BINARY;
182 const struct ima_template_field *field =
183 e->template_desc->fields[i];
185 if (is_ima_template && strcmp(field->field_id, "d") == 0)
186 show = IMA_SHOW_BINARY_NO_FIELD_LEN;
187 if (is_ima_template && strcmp(field->field_id, "n") == 0)
188 show = IMA_SHOW_BINARY_OLD_STRING_FMT;
189 field->field_show(m, show, &e->template_data[i]);
194 static const struct seq_operations ima_measurments_seqops = {
195 .start = ima_measurements_start,
196 .next = ima_measurements_next,
197 .stop = ima_measurements_stop,
198 .show = ima_measurements_show
201 static int ima_measurements_open(struct inode *inode, struct file *file)
203 return seq_open(file, &ima_measurments_seqops);
206 static const struct file_operations ima_measurements_ops = {
207 .open = ima_measurements_open,
210 .release = seq_release,
213 void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
217 for (i = 0; i < size; i++)
218 seq_printf(m, "%02x", *(digest + i));
222 static int ima_ascii_measurements_show(struct seq_file *m, void *v)
224 /* the list never shrinks, so we don't need a lock here */
225 struct ima_queue_entry *qe = v;
226 struct ima_template_entry *e;
235 template_name = (e->template_desc->name[0] != '\0') ?
236 e->template_desc->name : e->template_desc->fmt;
238 /* 1st: PCR used (config option) */
239 seq_printf(m, "%2d ", e->pcr);
241 /* 2nd: SHA1 template hash */
242 ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
244 /* 3th: template name */
245 seq_printf(m, " %s", template_name);
247 /* 4th: template specific data */
248 for (i = 0; i < e->template_desc->num_fields; i++) {
250 if (e->template_data[i].len == 0)
253 e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
254 &e->template_data[i]);
260 static const struct seq_operations ima_ascii_measurements_seqops = {
261 .start = ima_measurements_start,
262 .next = ima_measurements_next,
263 .stop = ima_measurements_stop,
264 .show = ima_ascii_measurements_show
267 static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
269 return seq_open(file, &ima_ascii_measurements_seqops);
272 static const struct file_operations ima_ascii_measurements_ops = {
273 .open = ima_ascii_measurements_open,
276 .release = seq_release,
279 static ssize_t ima_read_policy(char *path)
284 int rc, pathlen = strlen(path);
290 strsep(&datap, "\n");
292 rc = kernel_read_file_from_path(path, &data, &size, 0, READING_POLICY);
294 pr_err("Unable to open file: %s (%d)", path, rc);
299 while (size > 0 && (p = strsep(&datap, "\n"))) {
300 pr_debug("rule: %s\n", p);
301 rc = ima_parse_add_rule(p);
316 static ssize_t ima_write_policy(struct file *file, const char __user *buf,
317 size_t datalen, loff_t *ppos)
322 if (datalen >= PAGE_SIZE)
323 datalen = PAGE_SIZE - 1;
325 /* No partial writes. */
330 data = memdup_user_nul(buf, datalen);
332 result = PTR_ERR(data);
336 result = mutex_lock_interruptible(&ima_write_mutex);
340 if (data[0] == '/') {
341 result = ima_read_policy(data);
342 } else if (ima_appraise & IMA_APPRAISE_POLICY) {
343 pr_err("signed policy file (specified as an absolute pathname) required\n");
344 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
345 "policy_update", "signed policy required",
347 if (ima_appraise & IMA_APPRAISE_ENFORCE)
350 result = ima_parse_add_rule(data);
352 mutex_unlock(&ima_write_mutex);
362 static struct dentry *ima_dir;
363 static struct dentry *ima_symlink;
364 static struct dentry *binary_runtime_measurements;
365 static struct dentry *ascii_runtime_measurements;
366 static struct dentry *runtime_measurements_count;
367 static struct dentry *violations;
368 static struct dentry *ima_policy;
374 static unsigned long ima_fs_flags;
376 #ifdef CONFIG_IMA_READ_POLICY
377 static const struct seq_operations ima_policy_seqops = {
378 .start = ima_policy_start,
379 .next = ima_policy_next,
380 .stop = ima_policy_stop,
381 .show = ima_policy_show,
386 * ima_open_policy: sequentialize access to the policy file
388 static int ima_open_policy(struct inode *inode, struct file *filp)
390 if (!(filp->f_flags & O_WRONLY)) {
391 #ifndef CONFIG_IMA_READ_POLICY
394 if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
396 if (!capable(CAP_SYS_ADMIN))
398 return seq_open(filp, &ima_policy_seqops);
401 if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
407 * ima_release_policy - start using the new measure policy rules.
409 * Initially, ima_measure points to the default policy rules, now
410 * point to the new policy rules, and remove the securityfs policy file,
411 * assuming a valid policy.
413 static int ima_release_policy(struct inode *inode, struct file *file)
415 const char *cause = valid_policy ? "completed" : "failed";
417 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
418 return seq_release(inode, file);
420 if (valid_policy && ima_check_policy() < 0) {
425 pr_info("policy update %s\n", cause);
426 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
427 "policy_update", cause, !valid_policy, 0);
432 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
437 #if !defined(CONFIG_IMA_WRITE_POLICY) && !defined(CONFIG_IMA_READ_POLICY)
438 securityfs_remove(ima_policy);
440 #elif defined(CONFIG_IMA_WRITE_POLICY)
441 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
442 #elif defined(CONFIG_IMA_READ_POLICY)
443 inode->i_mode &= ~S_IWUSR;
448 static const struct file_operations ima_measure_policy_ops = {
449 .open = ima_open_policy,
450 .write = ima_write_policy,
452 .release = ima_release_policy,
453 .llseek = generic_file_llseek,
456 int __init ima_fs_init(void)
458 ima_dir = securityfs_create_dir("ima", integrity_dir);
462 ima_symlink = securityfs_create_symlink("ima", NULL, "integrity/ima",
464 if (IS_ERR(ima_symlink))
467 binary_runtime_measurements =
468 securityfs_create_file("binary_runtime_measurements",
469 S_IRUSR | S_IRGRP, ima_dir, NULL,
470 &ima_measurements_ops);
471 if (IS_ERR(binary_runtime_measurements))
474 ascii_runtime_measurements =
475 securityfs_create_file("ascii_runtime_measurements",
476 S_IRUSR | S_IRGRP, ima_dir, NULL,
477 &ima_ascii_measurements_ops);
478 if (IS_ERR(ascii_runtime_measurements))
481 runtime_measurements_count =
482 securityfs_create_file("runtime_measurements_count",
483 S_IRUSR | S_IRGRP, ima_dir, NULL,
484 &ima_measurements_count_ops);
485 if (IS_ERR(runtime_measurements_count))
489 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
490 ima_dir, NULL, &ima_htable_violations_ops);
491 if (IS_ERR(violations))
494 ima_policy = securityfs_create_file("policy", POLICY_FILE_FLAGS,
496 &ima_measure_policy_ops);
497 if (IS_ERR(ima_policy))
502 securityfs_remove(violations);
503 securityfs_remove(runtime_measurements_count);
504 securityfs_remove(ascii_runtime_measurements);
505 securityfs_remove(binary_runtime_measurements);
506 securityfs_remove(ima_symlink);
507 securityfs_remove(ima_dir);
508 securityfs_remove(ima_policy);