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 DEFINE_MUTEX(ima_write_mutex
);
30 static int valid_policy
= 1;
32 static ssize_t
ima_show_htable_value(char __user
*buf
, size_t count
,
33 loff_t
*ppos
, atomic_long_t
*val
)
35 char tmpbuf
[TMPBUFLEN
];
38 len
= scnprintf(tmpbuf
, TMPBUFLEN
, "%li\n", atomic_long_read(val
));
39 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, len
);
42 static ssize_t
ima_show_htable_violations(struct file
*filp
,
44 size_t count
, loff_t
*ppos
)
46 return ima_show_htable_value(buf
, count
, ppos
, &ima_htable
.violations
);
49 static const struct file_operations ima_htable_violations_ops
= {
50 .read
= ima_show_htable_violations
,
51 .llseek
= generic_file_llseek
,
54 static ssize_t
ima_show_measurements_count(struct file
*filp
,
56 size_t count
, loff_t
*ppos
)
58 return ima_show_htable_value(buf
, count
, ppos
, &ima_htable
.len
);
62 static const struct file_operations ima_measurements_count_ops
= {
63 .read
= ima_show_measurements_count
,
64 .llseek
= generic_file_llseek
,
67 /* returns pointer to hlist_node */
68 static void *ima_measurements_start(struct seq_file
*m
, loff_t
*pos
)
71 struct ima_queue_entry
*qe
;
73 /* we need a lock since pos could point beyond last element */
75 list_for_each_entry_rcu(qe
, &ima_measurements
, later
) {
85 static void *ima_measurements_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
87 struct ima_queue_entry
*qe
= v
;
89 /* lock protects when reading beyond last element
90 * against concurrent list-extension
93 qe
= list_entry_rcu(qe
->later
.next
, struct ima_queue_entry
, later
);
97 return (&qe
->later
== &ima_measurements
) ? NULL
: qe
;
100 static void ima_measurements_stop(struct seq_file
*m
, void *v
)
104 void ima_putc(struct seq_file
*m
, void *data
, int datalen
)
107 seq_putc(m
, *(char *)data
++);
112 * char[20]=template digest
113 * 32bit-le=template name size
114 * char[n]=template name
116 * eventdata[n]=template specific data
118 static int ima_measurements_show(struct seq_file
*m
, void *v
)
120 /* the list never shrinks, so we don't need a lock here */
121 struct ima_queue_entry
*qe
= v
;
122 struct ima_template_entry
*e
;
125 u32 pcr
= CONFIG_IMA_MEASURE_PCR_IDX
;
126 bool is_ima_template
= false;
134 template_name
= (e
->template_desc
->name
[0] != '\0') ?
135 e
->template_desc
->name
: e
->template_desc
->fmt
;
139 * PCR used is always the same (config option) in
140 * little-endian format
142 ima_putc(m
, &pcr
, sizeof(pcr
));
144 /* 2nd: template digest */
145 ima_putc(m
, e
->digest
, TPM_DIGEST_SIZE
);
147 /* 3rd: template name size */
148 namelen
= strlen(template_name
);
149 ima_putc(m
, &namelen
, sizeof(namelen
));
151 /* 4th: template name */
152 ima_putc(m
, template_name
, namelen
);
154 /* 5th: template length (except for 'ima' template) */
155 if (strcmp(template_name
, IMA_TEMPLATE_IMA_NAME
) == 0)
156 is_ima_template
= true;
158 if (!is_ima_template
)
159 ima_putc(m
, &e
->template_data_len
,
160 sizeof(e
->template_data_len
));
162 /* 6th: template specific data */
163 for (i
= 0; i
< e
->template_desc
->num_fields
; i
++) {
164 enum ima_show_type show
= IMA_SHOW_BINARY
;
165 struct ima_template_field
*field
= e
->template_desc
->fields
[i
];
167 if (is_ima_template
&& strcmp(field
->field_id
, "d") == 0)
168 show
= IMA_SHOW_BINARY_NO_FIELD_LEN
;
169 if (is_ima_template
&& strcmp(field
->field_id
, "n") == 0)
170 show
= IMA_SHOW_BINARY_OLD_STRING_FMT
;
171 field
->field_show(m
, show
, &e
->template_data
[i
]);
176 static const struct seq_operations ima_measurments_seqops
= {
177 .start
= ima_measurements_start
,
178 .next
= ima_measurements_next
,
179 .stop
= ima_measurements_stop
,
180 .show
= ima_measurements_show
183 static int ima_measurements_open(struct inode
*inode
, struct file
*file
)
185 return seq_open(file
, &ima_measurments_seqops
);
188 static const struct file_operations ima_measurements_ops
= {
189 .open
= ima_measurements_open
,
192 .release
= seq_release
,
195 void ima_print_digest(struct seq_file
*m
, u8
*digest
, u32 size
)
199 for (i
= 0; i
< size
; i
++)
200 seq_printf(m
, "%02x", *(digest
+ i
));
204 static int ima_ascii_measurements_show(struct seq_file
*m
, void *v
)
206 /* the list never shrinks, so we don't need a lock here */
207 struct ima_queue_entry
*qe
= v
;
208 struct ima_template_entry
*e
;
217 template_name
= (e
->template_desc
->name
[0] != '\0') ?
218 e
->template_desc
->name
: e
->template_desc
->fmt
;
220 /* 1st: PCR used (config option) */
221 seq_printf(m
, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX
);
223 /* 2nd: SHA1 template hash */
224 ima_print_digest(m
, e
->digest
, TPM_DIGEST_SIZE
);
226 /* 3th: template name */
227 seq_printf(m
, " %s", template_name
);
229 /* 4th: template specific data */
230 for (i
= 0; i
< e
->template_desc
->num_fields
; i
++) {
232 if (e
->template_data
[i
].len
== 0)
235 e
->template_desc
->fields
[i
]->field_show(m
, IMA_SHOW_ASCII
,
236 &e
->template_data
[i
]);
242 static const struct seq_operations ima_ascii_measurements_seqops
= {
243 .start
= ima_measurements_start
,
244 .next
= ima_measurements_next
,
245 .stop
= ima_measurements_stop
,
246 .show
= ima_ascii_measurements_show
249 static int ima_ascii_measurements_open(struct inode
*inode
, struct file
*file
)
251 return seq_open(file
, &ima_ascii_measurements_seqops
);
254 static const struct file_operations ima_ascii_measurements_ops
= {
255 .open
= ima_ascii_measurements_open
,
258 .release
= seq_release
,
261 static ssize_t
ima_write_policy(struct file
*file
, const char __user
*buf
,
262 size_t datalen
, loff_t
*ppos
)
267 if (datalen
>= PAGE_SIZE
)
268 datalen
= PAGE_SIZE
- 1;
270 /* No partial writes. */
276 data
= kmalloc(datalen
+ 1, GFP_KERNEL
);
280 *(data
+ datalen
) = '\0';
283 if (copy_from_user(data
, buf
, datalen
))
286 result
= mutex_lock_interruptible(&ima_write_mutex
);
289 result
= ima_parse_add_rule(data
);
290 mutex_unlock(&ima_write_mutex
);
301 static struct dentry
*ima_dir
;
302 static struct dentry
*binary_runtime_measurements
;
303 static struct dentry
*ascii_runtime_measurements
;
304 static struct dentry
*runtime_measurements_count
;
305 static struct dentry
*violations
;
306 static struct dentry
*ima_policy
;
312 static unsigned long ima_fs_flags
;
314 #ifdef CONFIG_IMA_READ_POLICY
315 static const struct seq_operations ima_policy_seqops
= {
316 .start
= ima_policy_start
,
317 .next
= ima_policy_next
,
318 .stop
= ima_policy_stop
,
319 .show
= ima_policy_show
,
324 * ima_open_policy: sequentialize access to the policy file
326 static int ima_open_policy(struct inode
*inode
, struct file
*filp
)
328 if (!(filp
->f_flags
& O_WRONLY
)) {
329 #ifndef CONFIG_IMA_READ_POLICY
332 if ((filp
->f_flags
& O_ACCMODE
) != O_RDONLY
)
334 if (!capable(CAP_SYS_ADMIN
))
336 return seq_open(filp
, &ima_policy_seqops
);
339 if (test_and_set_bit(IMA_FS_BUSY
, &ima_fs_flags
))
345 * ima_release_policy - start using the new measure policy rules.
347 * Initially, ima_measure points to the default policy rules, now
348 * point to the new policy rules, and remove the securityfs policy file,
349 * assuming a valid policy.
351 static int ima_release_policy(struct inode
*inode
, struct file
*file
)
353 const char *cause
= valid_policy
? "completed" : "failed";
355 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
358 if (valid_policy
&& ima_check_policy() < 0) {
363 pr_info("IMA: policy update %s\n", cause
);
364 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
, NULL
,
365 "policy_update", cause
, !valid_policy
, 0);
370 clear_bit(IMA_FS_BUSY
, &ima_fs_flags
);
375 #ifndef CONFIG_IMA_WRITE_POLICY
376 securityfs_remove(ima_policy
);
379 clear_bit(IMA_FS_BUSY
, &ima_fs_flags
);
384 static const struct file_operations ima_measure_policy_ops
= {
385 .open
= ima_open_policy
,
386 .write
= ima_write_policy
,
388 .release
= ima_release_policy
,
389 .llseek
= generic_file_llseek
,
392 int __init
ima_fs_init(void)
394 ima_dir
= securityfs_create_dir("ima", NULL
);
398 binary_runtime_measurements
=
399 securityfs_create_file("binary_runtime_measurements",
400 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
401 &ima_measurements_ops
);
402 if (IS_ERR(binary_runtime_measurements
))
405 ascii_runtime_measurements
=
406 securityfs_create_file("ascii_runtime_measurements",
407 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
408 &ima_ascii_measurements_ops
);
409 if (IS_ERR(ascii_runtime_measurements
))
412 runtime_measurements_count
=
413 securityfs_create_file("runtime_measurements_count",
414 S_IRUSR
| S_IRGRP
, ima_dir
, NULL
,
415 &ima_measurements_count_ops
);
416 if (IS_ERR(runtime_measurements_count
))
420 securityfs_create_file("violations", S_IRUSR
| S_IRGRP
,
421 ima_dir
, NULL
, &ima_htable_violations_ops
);
422 if (IS_ERR(violations
))
425 ima_policy
= securityfs_create_file("policy", POLICY_FILE_FLAGS
,
427 &ima_measure_policy_ops
);
428 if (IS_ERR(ima_policy
))
433 securityfs_remove(violations
);
434 securityfs_remove(runtime_measurements_count
);
435 securityfs_remove(ascii_runtime_measurements
);
436 securityfs_remove(binary_runtime_measurements
);
437 securityfs_remove(ima_dir
);
438 securityfs_remove(ima_policy
);