mm/slub.c: fix corrupted freechain in deactivate_slab()
[linux/fpc-iii.git] / security / integrity / ima / ima_fs.c
blob853a7d2333b3e898076e88005167adb706c6cc72
1 /*
2 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
4 * Authors:
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
12 * License.
14 * File: ima_fs.c
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>
27 #include "ima.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[32]; /* greater than largest 'long' string value */
37 ssize_t len;
39 len = scnprintf(tmpbuf, sizeof(tmpbuf), "%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,
44 char __user *buf,
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,
56 char __user *buf,
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)
71 loff_t l = *pos;
72 struct ima_queue_entry *qe;
74 /* we need a lock since pos could point beyond last element */
75 rcu_read_lock();
76 list_for_each_entry_rcu(qe, &ima_measurements, later) {
77 if (!l--) {
78 rcu_read_unlock();
79 return qe;
82 rcu_read_unlock();
83 return NULL;
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
93 rcu_read_lock();
94 qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
95 rcu_read_unlock();
96 (*pos)++;
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)
107 while (datalen--)
108 seq_putc(m, *(char *)data++);
111 /* print format:
112 * 32bit-le=pcr#
113 * char[20]=template digest
114 * 32bit-le=template name size
115 * char[n]=template name
116 * [eventdata length]
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;
124 char *template_name;
125 int namelen;
126 bool is_ima_template = false;
127 int i;
129 /* get entry */
130 e = qe->entry;
131 if (e == NULL)
132 return -1;
134 template_name = (e->template_desc->name[0] != '\0') ?
135 e->template_desc->name : e->template_desc->fmt;
138 * 1st: PCRIndex
139 * PCR used defaults to the same (config option) in
140 * little-endian format, unless set in policy
142 ima_putc(m, &e->pcr, sizeof(e->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]);
173 return 0;
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,
190 .read = seq_read,
191 .llseek = seq_lseek,
192 .release = seq_release,
195 void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
197 u32 i;
199 for (i = 0; i < size; i++)
200 seq_printf(m, "%02x", *(digest + i));
203 /* print in ascii */
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;
209 char *template_name;
210 int i;
212 /* get entry */
213 e = qe->entry;
214 if (e == NULL)
215 return -1;
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 ", e->pcr);
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++) {
231 seq_puts(m, " ");
232 if (e->template_data[i].len == 0)
233 continue;
235 e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
236 &e->template_data[i]);
238 seq_puts(m, "\n");
239 return 0;
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,
256 .read = seq_read,
257 .llseek = seq_lseek,
258 .release = seq_release,
261 static ssize_t ima_read_policy(char *path)
263 void *data;
264 char *datap;
265 loff_t size;
266 int rc, pathlen = strlen(path);
268 char *p;
270 /* remove \n */
271 datap = path;
272 strsep(&datap, "\n");
274 rc = kernel_read_file_from_path(path, &data, &size, 0, READING_POLICY);
275 if (rc < 0) {
276 pr_err("Unable to open file: %s (%d)", path, rc);
277 return rc;
280 datap = data;
281 while (size > 0 && (p = strsep(&datap, "\n"))) {
282 pr_debug("rule: %s\n", p);
283 rc = ima_parse_add_rule(p);
284 if (rc < 0)
285 break;
286 size -= rc;
289 vfree(data);
290 if (rc < 0)
291 return rc;
292 else if (size)
293 return -EINVAL;
294 else
295 return pathlen;
298 static ssize_t ima_write_policy(struct file *file, const char __user *buf,
299 size_t datalen, loff_t *ppos)
301 char *data;
302 ssize_t result;
304 if (datalen >= PAGE_SIZE)
305 datalen = PAGE_SIZE - 1;
307 /* No partial writes. */
308 result = -EINVAL;
309 if (*ppos != 0)
310 goto out;
312 result = -ENOMEM;
313 data = kmalloc(datalen + 1, GFP_KERNEL);
314 if (!data)
315 goto out;
317 *(data + datalen) = '\0';
319 result = -EFAULT;
320 if (copy_from_user(data, buf, datalen))
321 goto out_free;
323 result = mutex_lock_interruptible(&ima_write_mutex);
324 if (result < 0)
325 goto out_free;
327 if (data[0] == '/') {
328 result = ima_read_policy(data);
329 } else if (ima_appraise & IMA_APPRAISE_POLICY) {
330 pr_err("IMA: signed policy file (specified as an absolute pathname) required\n");
331 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
332 "policy_update", "signed policy required",
333 1, 0);
334 result = -EACCES;
335 } else {
336 result = ima_parse_add_rule(data);
338 mutex_unlock(&ima_write_mutex);
339 out_free:
340 kfree(data);
341 out:
342 if (result < 0)
343 valid_policy = 0;
345 return result;
348 static struct dentry *ima_dir;
349 static struct dentry *binary_runtime_measurements;
350 static struct dentry *ascii_runtime_measurements;
351 static struct dentry *runtime_measurements_count;
352 static struct dentry *violations;
353 static struct dentry *ima_policy;
355 enum ima_fs_flags {
356 IMA_FS_BUSY,
359 static unsigned long ima_fs_flags;
361 #ifdef CONFIG_IMA_READ_POLICY
362 static const struct seq_operations ima_policy_seqops = {
363 .start = ima_policy_start,
364 .next = ima_policy_next,
365 .stop = ima_policy_stop,
366 .show = ima_policy_show,
368 #endif
371 * ima_open_policy: sequentialize access to the policy file
373 static int ima_open_policy(struct inode *inode, struct file *filp)
375 if (!(filp->f_flags & O_WRONLY)) {
376 #ifndef CONFIG_IMA_READ_POLICY
377 return -EACCES;
378 #else
379 if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
380 return -EACCES;
381 if (!capable(CAP_SYS_ADMIN))
382 return -EPERM;
383 return seq_open(filp, &ima_policy_seqops);
384 #endif
386 if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
387 return -EBUSY;
388 return 0;
392 * ima_release_policy - start using the new measure policy rules.
394 * Initially, ima_measure points to the default policy rules, now
395 * point to the new policy rules, and remove the securityfs policy file,
396 * assuming a valid policy.
398 static int ima_release_policy(struct inode *inode, struct file *file)
400 const char *cause = valid_policy ? "completed" : "failed";
402 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
403 return seq_release(inode, file);
405 if (valid_policy && ima_check_policy() < 0) {
406 cause = "failed";
407 valid_policy = 0;
410 pr_info("IMA: policy update %s\n", cause);
411 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
412 "policy_update", cause, !valid_policy, 0);
414 if (!valid_policy) {
415 ima_delete_rules();
416 valid_policy = 1;
417 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
418 return 0;
421 ima_update_policy();
422 #ifndef CONFIG_IMA_WRITE_POLICY
423 securityfs_remove(ima_policy);
424 ima_policy = NULL;
425 #else
426 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
427 #endif
428 return 0;
431 static const struct file_operations ima_measure_policy_ops = {
432 .open = ima_open_policy,
433 .write = ima_write_policy,
434 .read = seq_read,
435 .release = ima_release_policy,
436 .llseek = generic_file_llseek,
439 int __init ima_fs_init(void)
441 ima_dir = securityfs_create_dir("ima", NULL);
442 if (IS_ERR(ima_dir))
443 return -1;
445 binary_runtime_measurements =
446 securityfs_create_file("binary_runtime_measurements",
447 S_IRUSR | S_IRGRP, ima_dir, NULL,
448 &ima_measurements_ops);
449 if (IS_ERR(binary_runtime_measurements))
450 goto out;
452 ascii_runtime_measurements =
453 securityfs_create_file("ascii_runtime_measurements",
454 S_IRUSR | S_IRGRP, ima_dir, NULL,
455 &ima_ascii_measurements_ops);
456 if (IS_ERR(ascii_runtime_measurements))
457 goto out;
459 runtime_measurements_count =
460 securityfs_create_file("runtime_measurements_count",
461 S_IRUSR | S_IRGRP, ima_dir, NULL,
462 &ima_measurements_count_ops);
463 if (IS_ERR(runtime_measurements_count))
464 goto out;
466 violations =
467 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
468 ima_dir, NULL, &ima_htable_violations_ops);
469 if (IS_ERR(violations))
470 goto out;
472 ima_policy = securityfs_create_file("policy", POLICY_FILE_FLAGS,
473 ima_dir, NULL,
474 &ima_measure_policy_ops);
475 if (IS_ERR(ima_policy))
476 goto out;
478 return 0;
479 out:
480 securityfs_remove(violations);
481 securityfs_remove(runtime_measurements_count);
482 securityfs_remove(ascii_runtime_measurements);
483 securityfs_remove(binary_runtime_measurements);
484 securityfs_remove(ima_dir);
485 securityfs_remove(ima_policy);
486 return -1;