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 bool ima_canonical_fmt
;
32 static int __init
default_canonical_fmt_setup(char *str
)
35 ima_canonical_fmt
= true;
39 __setup("ima_canonical_fmt", default_canonical_fmt_setup
);
41 static int valid_policy
= 1;
43 static ssize_t
ima_show_htable_value(char __user
*buf
, size_t count
,
44 loff_t
*ppos
, atomic_long_t
*val
)
46 char tmpbuf
[TMPBUFLEN
];
49 len
= scnprintf(tmpbuf
, TMPBUFLEN
, "%li\n", atomic_long_read(val
));
50 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, len
);
53 static ssize_t
ima_show_htable_violations(struct file
*filp
,
55 size_t count
, loff_t
*ppos
)
57 return ima_show_htable_value(buf
, count
, ppos
, &ima_htable
.violations
);
60 static const struct file_operations ima_htable_violations_ops
= {
61 .read
= ima_show_htable_violations
,
62 .llseek
= generic_file_llseek
,
65 static ssize_t
ima_show_measurements_count(struct file
*filp
,
67 size_t count
, loff_t
*ppos
)
69 return ima_show_htable_value(buf
, count
, ppos
, &ima_htable
.len
);
73 static const struct file_operations ima_measurements_count_ops
= {
74 .read
= ima_show_measurements_count
,
75 .llseek
= generic_file_llseek
,
78 /* returns pointer to hlist_node */
79 static void *ima_measurements_start(struct seq_file
*m
, loff_t
*pos
)
82 struct ima_queue_entry
*qe
;
84 /* we need a lock since pos could point beyond last element */
86 list_for_each_entry_rcu(qe
, &ima_measurements
, later
) {
96 static void *ima_measurements_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
98 struct ima_queue_entry
*qe
= v
;
100 /* lock protects when reading beyond last element
101 * against concurrent list-extension
104 qe
= list_entry_rcu(qe
->later
.next
, struct ima_queue_entry
, later
);
108 return (&qe
->later
== &ima_measurements
) ? NULL
: qe
;
111 static void ima_measurements_stop(struct seq_file
*m
, void *v
)
115 void ima_putc(struct seq_file
*m
, void *data
, int datalen
)
118 seq_putc(m
, *(char *)data
++);
123 * char[20]=template digest
124 * 32bit-le=template name size
125 * char[n]=template name
127 * eventdata[n]=template specific data
129 int ima_measurements_show(struct seq_file
*m
, void *v
)
131 /* the list never shrinks, so we don't need a lock here */
132 struct ima_queue_entry
*qe
= v
;
133 struct ima_template_entry
*e
;
135 u32 pcr
, namelen
, template_data_len
; /* temporary fields */
136 bool is_ima_template
= false;
144 template_name
= (e
->template_desc
->name
[0] != '\0') ?
145 e
->template_desc
->name
: e
->template_desc
->fmt
;
149 * PCR used defaults to the same (config option) in
150 * little-endian format, unless set in policy
152 pcr
= !ima_canonical_fmt
? e
->pcr
: cpu_to_le32(e
->pcr
);
153 ima_putc(m
, &pcr
, sizeof(e
->pcr
));
155 /* 2nd: template digest */
156 ima_putc(m
, e
->digest
, TPM_DIGEST_SIZE
);
158 /* 3rd: template name size */
159 namelen
= !ima_canonical_fmt
? strlen(template_name
) :
160 cpu_to_le32(strlen(template_name
));
161 ima_putc(m
, &namelen
, sizeof(namelen
));
163 /* 4th: template name */
164 ima_putc(m
, template_name
, strlen(template_name
));
166 /* 5th: template length (except for 'ima' template) */
167 if (strcmp(template_name
, IMA_TEMPLATE_IMA_NAME
) == 0)
168 is_ima_template
= true;
170 if (!is_ima_template
) {
171 template_data_len
= !ima_canonical_fmt
? e
->template_data_len
:
172 cpu_to_le32(e
->template_data_len
);
173 ima_putc(m
, &template_data_len
, sizeof(e
->template_data_len
));
176 /* 6th: template specific data */
177 for (i
= 0; i
< e
->template_desc
->num_fields
; i
++) {
178 enum ima_show_type show
= IMA_SHOW_BINARY
;
179 struct ima_template_field
*field
= e
->template_desc
->fields
[i
];
181 if (is_ima_template
&& strcmp(field
->field_id
, "d") == 0)
182 show
= IMA_SHOW_BINARY_NO_FIELD_LEN
;
183 if (is_ima_template
&& strcmp(field
->field_id
, "n") == 0)
184 show
= IMA_SHOW_BINARY_OLD_STRING_FMT
;
185 field
->field_show(m
, show
, &e
->template_data
[i
]);
190 static const struct seq_operations ima_measurments_seqops
= {
191 .start
= ima_measurements_start
,
192 .next
= ima_measurements_next
,
193 .stop
= ima_measurements_stop
,
194 .show
= ima_measurements_show
197 static int ima_measurements_open(struct inode
*inode
, struct file
*file
)
199 return seq_open(file
, &ima_measurments_seqops
);
202 static const struct file_operations ima_measurements_ops
= {
203 .open
= ima_measurements_open
,
206 .release
= seq_release
,
209 void ima_print_digest(struct seq_file
*m
, u8
*digest
, u32 size
)
213 for (i
= 0; i
< size
; i
++)
214 seq_printf(m
, "%02x", *(digest
+ i
));
218 static int ima_ascii_measurements_show(struct seq_file
*m
, void *v
)
220 /* the list never shrinks, so we don't need a lock here */
221 struct ima_queue_entry
*qe
= v
;
222 struct ima_template_entry
*e
;
231 template_name
= (e
->template_desc
->name
[0] != '\0') ?
232 e
->template_desc
->name
: e
->template_desc
->fmt
;
234 /* 1st: PCR used (config option) */
235 seq_printf(m
, "%2d ", e
->pcr
);
237 /* 2nd: SHA1 template hash */
238 ima_print_digest(m
, e
->digest
, TPM_DIGEST_SIZE
);
240 /* 3th: template name */
241 seq_printf(m
, " %s", template_name
);
243 /* 4th: template specific data */
244 for (i
= 0; i
< e
->template_desc
->num_fields
; i
++) {
246 if (e
->template_data
[i
].len
== 0)
249 e
->template_desc
->fields
[i
]->field_show(m
, IMA_SHOW_ASCII
,
250 &e
->template_data
[i
]);
256 static const struct seq_operations ima_ascii_measurements_seqops
= {
257 .start
= ima_measurements_start
,
258 .next
= ima_measurements_next
,
259 .stop
= ima_measurements_stop
,
260 .show
= ima_ascii_measurements_show
263 static int ima_ascii_measurements_open(struct inode
*inode
, struct file
*file
)
265 return seq_open(file
, &ima_ascii_measurements_seqops
);
268 static const struct file_operations ima_ascii_measurements_ops
= {
269 .open
= ima_ascii_measurements_open
,
272 .release
= seq_release
,
275 static ssize_t
ima_read_policy(char *path
)
280 int rc
, pathlen
= strlen(path
);
286 strsep(&datap
, "\n");
288 rc
= kernel_read_file_from_path(path
, &data
, &size
, 0, READING_POLICY
);
290 pr_err("Unable to open file: %s (%d)", path
, rc
);
295 while (size
> 0 && (p
= strsep(&datap
, "\n"))) {
296 pr_debug("rule: %s\n", p
);
297 rc
= ima_parse_add_rule(p
);
312 static ssize_t
ima_write_policy(struct file
*file
, const char __user
*buf
,
313 size_t datalen
, loff_t
*ppos
)
318 if (datalen
>= PAGE_SIZE
)
319 datalen
= PAGE_SIZE
- 1;
321 /* No partial writes. */
326 data
= memdup_user_nul(buf
, datalen
);
328 result
= PTR_ERR(data
);
332 result
= mutex_lock_interruptible(&ima_write_mutex
);
336 if (data
[0] == '/') {
337 result
= ima_read_policy(data
);
338 } else if (ima_appraise
& IMA_APPRAISE_POLICY
) {
339 pr_err("IMA: signed policy file (specified as an absolute pathname) required\n");
340 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
, NULL
,
341 "policy_update", "signed policy required",
343 if (ima_appraise
& IMA_APPRAISE_ENFORCE
)
346 result
= ima_parse_add_rule(data
);
348 mutex_unlock(&ima_write_mutex
);
358 static struct dentry
*ima_dir
;
359 static struct dentry
*binary_runtime_measurements
;
360 static struct dentry
*ascii_runtime_measurements
;
361 static struct dentry
*runtime_measurements_count
;
362 static struct dentry
*violations
;
363 static struct dentry
*ima_policy
;
369 static unsigned long ima_fs_flags
;
371 #ifdef CONFIG_IMA_READ_POLICY
372 static const struct seq_operations ima_policy_seqops
= {
373 .start
= ima_policy_start
,
374 .next
= ima_policy_next
,
375 .stop
= ima_policy_stop
,
376 .show
= ima_policy_show
,
381 * ima_open_policy: sequentialize access to the policy file
383 static int ima_open_policy(struct inode
*inode
, struct file
*filp
)
385 if (!(filp
->f_flags
& O_WRONLY
)) {
386 #ifndef CONFIG_IMA_READ_POLICY
389 if ((filp
->f_flags
& O_ACCMODE
) != O_RDONLY
)
391 if (!capable(CAP_SYS_ADMIN
))
393 return seq_open(filp
, &ima_policy_seqops
);
396 if (test_and_set_bit(IMA_FS_BUSY
, &ima_fs_flags
))
402 * ima_release_policy - start using the new measure policy rules.
404 * Initially, ima_measure points to the default policy rules, now
405 * point to the new policy rules, and remove the securityfs policy file,
406 * assuming a valid policy.
408 static int ima_release_policy(struct inode
*inode
, struct file
*file
)
410 const char *cause
= valid_policy
? "completed" : "failed";
412 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
413 return seq_release(inode
, file
);
415 if (valid_policy
&& ima_check_policy() < 0) {
420 pr_info("IMA: policy update %s\n", cause
);
421 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
, NULL
,
422 "policy_update", cause
, !valid_policy
, 0);
427 clear_bit(IMA_FS_BUSY
, &ima_fs_flags
);
432 #if !defined(CONFIG_IMA_WRITE_POLICY) && !defined(CONFIG_IMA_READ_POLICY)
433 securityfs_remove(ima_policy
);
435 #elif defined(CONFIG_IMA_WRITE_POLICY)
436 clear_bit(IMA_FS_BUSY
, &ima_fs_flags
);
441 static const struct file_operations ima_measure_policy_ops
= {
442 .open
= ima_open_policy
,
443 .write
= ima_write_policy
,
445 .release
= ima_release_policy
,
446 .llseek
= generic_file_llseek
,
449 int __init
ima_fs_init(void)
451 ima_dir
= securityfs_create_dir("ima", NULL
);
455 binary_runtime_measurements
=
456 securityfs_create_file("binary_runtime_measurements",
457 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
458 &ima_measurements_ops
);
459 if (IS_ERR(binary_runtime_measurements
))
462 ascii_runtime_measurements
=
463 securityfs_create_file("ascii_runtime_measurements",
464 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
465 &ima_ascii_measurements_ops
);
466 if (IS_ERR(ascii_runtime_measurements
))
469 runtime_measurements_count
=
470 securityfs_create_file("runtime_measurements_count",
471 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
472 &ima_measurements_count_ops
);
473 if (IS_ERR(runtime_measurements_count
))
477 securityfs_create_file("violations", S_IRUSR
| S_IRGRP
,
478 ima_dir
, NULL
, &ima_htable_violations_ops
);
479 if (IS_ERR(violations
))
482 ima_policy
= securityfs_create_file("policy", POLICY_FILE_FLAGS
,
484 &ima_measure_policy_ops
);
485 if (IS_ERR(ima_policy
))
490 securityfs_remove(violations
);
491 securityfs_remove(runtime_measurements_count
);
492 securityfs_remove(ascii_runtime_measurements
);
493 securityfs_remove(binary_runtime_measurements
);
494 securityfs_remove(ima_dir
);
495 securityfs_remove(ima_policy
);