ima: ignore empty and with whitespaces policy lines
[deliverable/linux.git] / security / integrity / ima / ima_fs.c
CommitLineData
bab73937
MZ
1/*
2 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
3 *
4 * Authors:
5 * Kylene Hall <kjhall@us.ibm.com>
6 * Reiner Sailer <sailer@us.ibm.com>
7 * Mimi Zohar <zohar@us.ibm.com>
8 *
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
12 * License.
13 *
14 * File: ima_fs.c
15 * implemenents security file system for reporting
16 * current measurement list and IMA statistics
17 */
f850a7c0 18#include <linux/fcntl.h>
5a0e3ad6 19#include <linux/slab.h>
bab73937
MZ
20#include <linux/module.h>
21#include <linux/seq_file.h>
22#include <linux/rculist.h>
23#include <linux/rcupdate.h>
4af4662f 24#include <linux/parser.h>
bab73937
MZ
25
26#include "ima.h"
27
4af4662f 28static int valid_policy = 1;
bab73937
MZ
29#define TMPBUFLEN 12
30static ssize_t ima_show_htable_value(char __user *buf, size_t count,
31 loff_t *ppos, atomic_long_t *val)
32{
33 char tmpbuf[TMPBUFLEN];
34 ssize_t len;
35
36 len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
37 return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
38}
39
40static ssize_t ima_show_htable_violations(struct file *filp,
41 char __user *buf,
42 size_t count, loff_t *ppos)
43{
44 return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
45}
46
828c0950 47static const struct file_operations ima_htable_violations_ops = {
cdcd90f9
AB
48 .read = ima_show_htable_violations,
49 .llseek = generic_file_llseek,
bab73937
MZ
50};
51
52static ssize_t ima_show_measurements_count(struct file *filp,
53 char __user *buf,
54 size_t count, loff_t *ppos)
55{
56 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
57
58}
59
828c0950 60static const struct file_operations ima_measurements_count_ops = {
cdcd90f9
AB
61 .read = ima_show_measurements_count,
62 .llseek = generic_file_llseek,
bab73937
MZ
63};
64
65/* returns pointer to hlist_node */
66static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
67{
68 loff_t l = *pos;
69 struct ima_queue_entry *qe;
70
71 /* we need a lock since pos could point beyond last element */
72 rcu_read_lock();
73 list_for_each_entry_rcu(qe, &ima_measurements, later) {
74 if (!l--) {
75 rcu_read_unlock();
76 return qe;
77 }
78 }
79 rcu_read_unlock();
80 return NULL;
81}
82
83static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
84{
85 struct ima_queue_entry *qe = v;
86
87 /* lock protects when reading beyond last element
88 * against concurrent list-extension
89 */
90 rcu_read_lock();
089bc8e9 91 qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
bab73937
MZ
92 rcu_read_unlock();
93 (*pos)++;
94
95 return (&qe->later == &ima_measurements) ? NULL : qe;
96}
97
98static void ima_measurements_stop(struct seq_file *m, void *v)
99{
100}
101
3ce1217d 102void ima_putc(struct seq_file *m, void *data, int datalen)
bab73937
MZ
103{
104 while (datalen--)
105 seq_putc(m, *(char *)data++);
106}
107
108/* print format:
109 * 32bit-le=pcr#
110 * char[20]=template digest
111 * 32bit-le=template name size
112 * char[n]=template name
a71dc65d 113 * [eventdata length]
bab73937
MZ
114 * eventdata[n]=template specific data
115 */
116static int ima_measurements_show(struct seq_file *m, void *v)
117{
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;
121 int namelen;
122 u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
3e8e5503 123 bool is_ima_template = false;
a71dc65d 124 int i;
bab73937
MZ
125
126 /* get entry */
127 e = qe->entry;
128 if (e == NULL)
129 return -1;
130
131 /*
132 * 1st: PCRIndex
133 * PCR used is always the same (config option) in
134 * little-endian format
135 */
2bb930ab 136 ima_putc(m, &pcr, sizeof(pcr));
bab73937
MZ
137
138 /* 2nd: template digest */
140d8022 139 ima_putc(m, e->digest, TPM_DIGEST_SIZE);
bab73937
MZ
140
141 /* 3rd: template name size */
a71dc65d 142 namelen = strlen(e->template_desc->name);
2bb930ab 143 ima_putc(m, &namelen, sizeof(namelen));
bab73937
MZ
144
145 /* 4th: template name */
a71dc65d
RS
146 ima_putc(m, e->template_desc->name, namelen);
147
148 /* 5th: template length (except for 'ima' template) */
3e8e5503
RS
149 if (strcmp(e->template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0)
150 is_ima_template = true;
151
152 if (!is_ima_template)
a71dc65d
RS
153 ima_putc(m, &e->template_data_len,
154 sizeof(e->template_data_len));
bab73937 155
a71dc65d
RS
156 /* 6th: template specific data */
157 for (i = 0; i < e->template_desc->num_fields; i++) {
3e8e5503
RS
158 enum ima_show_type show = IMA_SHOW_BINARY;
159 struct ima_template_field *field = e->template_desc->fields[i];
160
161 if (is_ima_template && strcmp(field->field_id, "d") == 0)
162 show = IMA_SHOW_BINARY_NO_FIELD_LEN;
c019e307
RS
163 if (is_ima_template && strcmp(field->field_id, "n") == 0)
164 show = IMA_SHOW_BINARY_OLD_STRING_FMT;
3e8e5503 165 field->field_show(m, show, &e->template_data[i]);
a71dc65d 166 }
bab73937
MZ
167 return 0;
168}
169
88e9d34c 170static const struct seq_operations ima_measurments_seqops = {
bab73937
MZ
171 .start = ima_measurements_start,
172 .next = ima_measurements_next,
173 .stop = ima_measurements_stop,
174 .show = ima_measurements_show
175};
176
177static int ima_measurements_open(struct inode *inode, struct file *file)
178{
179 return seq_open(file, &ima_measurments_seqops);
180}
181
828c0950 182static const struct file_operations ima_measurements_ops = {
bab73937
MZ
183 .open = ima_measurements_open,
184 .read = seq_read,
185 .llseek = seq_lseek,
186 .release = seq_release,
187};
188
3ce1217d 189void ima_print_digest(struct seq_file *m, u8 *digest, int size)
bab73937
MZ
190{
191 int i;
192
140d8022 193 for (i = 0; i < size; i++)
bab73937
MZ
194 seq_printf(m, "%02x", *(digest + i));
195}
196
bab73937
MZ
197/* print in ascii */
198static int ima_ascii_measurements_show(struct seq_file *m, void *v)
199{
200 /* the list never shrinks, so we don't need a lock here */
201 struct ima_queue_entry *qe = v;
202 struct ima_template_entry *e;
a71dc65d 203 int i;
bab73937
MZ
204
205 /* get entry */
206 e = qe->entry;
207 if (e == NULL)
208 return -1;
209
210 /* 1st: PCR used (config option) */
211 seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
212
213 /* 2nd: SHA1 template hash */
140d8022 214 ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
bab73937
MZ
215
216 /* 3th: template name */
a71dc65d 217 seq_printf(m, " %s", e->template_desc->name);
bab73937
MZ
218
219 /* 4th: template specific data */
a71dc65d
RS
220 for (i = 0; i < e->template_desc->num_fields; i++) {
221 seq_puts(m, " ");
222 if (e->template_data[i].len == 0)
223 continue;
224
225 e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
226 &e->template_data[i]);
227 }
228 seq_puts(m, "\n");
bab73937
MZ
229 return 0;
230}
231
88e9d34c 232static const struct seq_operations ima_ascii_measurements_seqops = {
bab73937
MZ
233 .start = ima_measurements_start,
234 .next = ima_measurements_next,
235 .stop = ima_measurements_stop,
236 .show = ima_ascii_measurements_show
237};
238
239static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
240{
241 return seq_open(file, &ima_ascii_measurements_seqops);
242}
243
828c0950 244static const struct file_operations ima_ascii_measurements_ops = {
bab73937
MZ
245 .open = ima_ascii_measurements_open,
246 .read = seq_read,
247 .llseek = seq_lseek,
248 .release = seq_release,
249};
250
4af4662f
MZ
251static ssize_t ima_write_policy(struct file *file, const char __user *buf,
252 size_t datalen, loff_t *ppos)
253{
6ccd0456
EP
254 char *data = NULL;
255 ssize_t result;
4af4662f
MZ
256
257 if (datalen >= PAGE_SIZE)
6ccd0456
EP
258 datalen = PAGE_SIZE - 1;
259
260 /* No partial writes. */
261 result = -EINVAL;
262 if (*ppos != 0)
263 goto out;
264
265 result = -ENOMEM;
4af4662f
MZ
266 data = kmalloc(datalen + 1, GFP_KERNEL);
267 if (!data)
6ccd0456 268 goto out;
4af4662f 269
4af4662f 270 *(data + datalen) = '\0';
4af4662f 271
6ccd0456
EP
272 result = -EFAULT;
273 if (copy_from_user(data, buf, datalen))
274 goto out;
275
276 result = ima_parse_add_rule(data);
277out:
278 if (result < 0)
279 valid_policy = 0;
4af4662f 280 kfree(data);
6ccd0456 281 return result;
4af4662f
MZ
282}
283
bab73937
MZ
284static struct dentry *ima_dir;
285static struct dentry *binary_runtime_measurements;
286static struct dentry *ascii_runtime_measurements;
287static struct dentry *runtime_measurements_count;
288static struct dentry *violations;
4af4662f
MZ
289static struct dentry *ima_policy;
290
f4bd857b
MZ
291static atomic_t policy_opencount = ATOMIC_INIT(1);
292/*
293 * ima_open_policy: sequentialize access to the policy file
294 */
2bb930ab 295static int ima_open_policy(struct inode *inode, struct file *filp)
f4bd857b 296{
f850a7c0
EP
297 /* No point in being allowed to open it if you aren't going to write */
298 if (!(filp->f_flags & O_WRONLY))
299 return -EACCES;
f4bd857b
MZ
300 if (atomic_dec_and_test(&policy_opencount))
301 return 0;
302 return -EBUSY;
303}
304
4af4662f
MZ
305/*
306 * ima_release_policy - start using the new measure policy rules.
307 *
308 * Initially, ima_measure points to the default policy rules, now
f4bd857b
MZ
309 * point to the new policy rules, and remove the securityfs policy file,
310 * assuming a valid policy.
4af4662f
MZ
311 */
312static int ima_release_policy(struct inode *inode, struct file *file)
313{
78bb5d0b
DK
314 pr_info("IMA: policy update %s\n",
315 valid_policy ? "completed" : "failed");
4af4662f
MZ
316 if (!valid_policy) {
317 ima_delete_rules();
f4bd857b
MZ
318 valid_policy = 1;
319 atomic_set(&policy_opencount, 1);
4af4662f
MZ
320 return 0;
321 }
322 ima_update_policy();
323 securityfs_remove(ima_policy);
324 ima_policy = NULL;
325 return 0;
326}
327
828c0950 328static const struct file_operations ima_measure_policy_ops = {
f4bd857b 329 .open = ima_open_policy,
4af4662f 330 .write = ima_write_policy,
cdcd90f9
AB
331 .release = ima_release_policy,
332 .llseek = generic_file_llseek,
4af4662f 333};
bab73937 334
932995f0 335int __init ima_fs_init(void)
bab73937
MZ
336{
337 ima_dir = securityfs_create_dir("ima", NULL);
338 if (IS_ERR(ima_dir))
339 return -1;
340
341 binary_runtime_measurements =
342 securityfs_create_file("binary_runtime_measurements",
343 S_IRUSR | S_IRGRP, ima_dir, NULL,
344 &ima_measurements_ops);
345 if (IS_ERR(binary_runtime_measurements))
346 goto out;
347
348 ascii_runtime_measurements =
349 securityfs_create_file("ascii_runtime_measurements",
350 S_IRUSR | S_IRGRP, ima_dir, NULL,
351 &ima_ascii_measurements_ops);
352 if (IS_ERR(ascii_runtime_measurements))
353 goto out;
354
355 runtime_measurements_count =
356 securityfs_create_file("runtime_measurements_count",
357 S_IRUSR | S_IRGRP, ima_dir, NULL,
358 &ima_measurements_count_ops);
359 if (IS_ERR(runtime_measurements_count))
360 goto out;
361
362 violations =
363 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
364 ima_dir, NULL, &ima_htable_violations_ops);
365 if (IS_ERR(violations))
366 goto out;
367
4af4662f 368 ima_policy = securityfs_create_file("policy",
f850a7c0 369 S_IWUSR,
4af4662f
MZ
370 ima_dir, NULL,
371 &ima_measure_policy_ops);
372 if (IS_ERR(ima_policy))
373 goto out;
bab73937 374
4af4662f 375 return 0;
bab73937 376out:
0ea4f8ae 377 securityfs_remove(violations);
bab73937
MZ
378 securityfs_remove(runtime_measurements_count);
379 securityfs_remove(ascii_runtime_measurements);
380 securityfs_remove(binary_runtime_measurements);
381 securityfs_remove(ima_dir);
4af4662f 382 securityfs_remove(ima_policy);
bab73937
MZ
383 return -1;
384}
This page took 1.984264 seconds and 5 git commands to generate.