blob: 3caed6de610c19c21d0f597ce2ccfff3357562ba [file] [log] [blame]
Mimi Zoharbab73932009-02-04 09:06:59 -05001/*
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 */
Eric Parisf850a7c2009-05-12 15:13:55 -040018#include <linux/fcntl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Mimi Zoharbab73932009-02-04 09:06:59 -050020#include <linux/module.h>
21#include <linux/seq_file.h>
22#include <linux/rculist.h>
23#include <linux/rcupdate.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050024#include <linux/parser.h>
Mimi Zoharbab73932009-02-04 09:06:59 -050025
26#include "ima.h"
27
Petko Manolov38d859f2015-12-02 17:47:54 +020028static DEFINE_MUTEX(ima_write_mutex);
29
Mimi Zohar4af46622009-02-04 09:07:00 -050030static int valid_policy = 1;
Mimi Zoharbab73932009-02-04 09:06:59 -050031#define TMPBUFLEN 12
32static ssize_t ima_show_htable_value(char __user *buf, size_t count,
33 loff_t *ppos, atomic_long_t *val)
34{
35 char tmpbuf[TMPBUFLEN];
36 ssize_t len;
37
38 len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
39 return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
40}
41
42static ssize_t ima_show_htable_violations(struct file *filp,
43 char __user *buf,
44 size_t count, loff_t *ppos)
45{
46 return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
47}
48
Alexey Dobriyan828c0952009-10-01 15:43:56 -070049static const struct file_operations ima_htable_violations_ops = {
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +020050 .read = ima_show_htable_violations,
51 .llseek = generic_file_llseek,
Mimi Zoharbab73932009-02-04 09:06:59 -050052};
53
54static ssize_t ima_show_measurements_count(struct file *filp,
55 char __user *buf,
56 size_t count, loff_t *ppos)
57{
58 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
59
60}
61
Alexey Dobriyan828c0952009-10-01 15:43:56 -070062static const struct file_operations ima_measurements_count_ops = {
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +020063 .read = ima_show_measurements_count,
64 .llseek = generic_file_llseek,
Mimi Zoharbab73932009-02-04 09:06:59 -050065};
66
67/* returns pointer to hlist_node */
68static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
69{
70 loff_t l = *pos;
71 struct ima_queue_entry *qe;
72
73 /* we need a lock since pos could point beyond last element */
74 rcu_read_lock();
75 list_for_each_entry_rcu(qe, &ima_measurements, later) {
76 if (!l--) {
77 rcu_read_unlock();
78 return qe;
79 }
80 }
81 rcu_read_unlock();
82 return NULL;
83}
84
85static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
86{
87 struct ima_queue_entry *qe = v;
88
89 /* lock protects when reading beyond last element
90 * against concurrent list-extension
91 */
92 rcu_read_lock();
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090093 qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
Mimi Zoharbab73932009-02-04 09:06:59 -050094 rcu_read_unlock();
95 (*pos)++;
96
97 return (&qe->later == &ima_measurements) ? NULL : qe;
98}
99
100static void ima_measurements_stop(struct seq_file *m, void *v)
101{
102}
103
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200104void ima_putc(struct seq_file *m, void *data, int datalen)
Mimi Zoharbab73932009-02-04 09:06:59 -0500105{
106 while (datalen--)
107 seq_putc(m, *(char *)data++);
108}
109
110/* print format:
111 * 32bit-le=pcr#
112 * char[20]=template digest
113 * 32bit-le=template name size
114 * char[n]=template name
Roberto Sassua71dc652013-06-07 12:16:33 +0200115 * [eventdata length]
Mimi Zoharbab73932009-02-04 09:06:59 -0500116 * eventdata[n]=template specific data
117 */
118static int ima_measurements_show(struct seq_file *m, void *v)
119{
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;
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200123 char *template_name;
Mimi Zoharbab73932009-02-04 09:06:59 -0500124 int namelen;
125 u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100126 bool is_ima_template = false;
Roberto Sassua71dc652013-06-07 12:16:33 +0200127 int i;
Mimi Zoharbab73932009-02-04 09:06:59 -0500128
129 /* get entry */
130 e = qe->entry;
131 if (e == NULL)
132 return -1;
133
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200134 template_name = (e->template_desc->name[0] != '\0') ?
135 e->template_desc->name : e->template_desc->fmt;
136
Mimi Zoharbab73932009-02-04 09:06:59 -0500137 /*
138 * 1st: PCRIndex
139 * PCR used is always the same (config option) in
140 * little-endian format
141 */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200142 ima_putc(m, &pcr, sizeof(pcr));
Mimi Zoharbab73932009-02-04 09:06:59 -0500143
144 /* 2nd: template digest */
Mimi Zohar140d8022013-03-11 20:29:47 -0400145 ima_putc(m, e->digest, TPM_DIGEST_SIZE);
Mimi Zoharbab73932009-02-04 09:06:59 -0500146
147 /* 3rd: template name size */
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200148 namelen = strlen(template_name);
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200149 ima_putc(m, &namelen, sizeof(namelen));
Mimi Zoharbab73932009-02-04 09:06:59 -0500150
151 /* 4th: template name */
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200152 ima_putc(m, template_name, namelen);
Mimi Zoharbab73932009-02-04 09:06:59 -0500153
Roberto Sassua71dc652013-06-07 12:16:33 +0200154 /* 5th: template length (except for 'ima' template) */
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200155 if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100156 is_ima_template = true;
157
158 if (!is_ima_template)
Roberto Sassua71dc652013-06-07 12:16:33 +0200159 ima_putc(m, &e->template_data_len,
160 sizeof(e->template_data_len));
161
162 /* 6th: template specific data */
163 for (i = 0; i < e->template_desc->num_fields; i++) {
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100164 enum ima_show_type show = IMA_SHOW_BINARY;
165 struct ima_template_field *field = e->template_desc->fields[i];
166
167 if (is_ima_template && strcmp(field->field_id, "d") == 0)
168 show = IMA_SHOW_BINARY_NO_FIELD_LEN;
Roberto Sassuc019e302014-02-03 13:56:04 +0100169 if (is_ima_template && strcmp(field->field_id, "n") == 0)
170 show = IMA_SHOW_BINARY_OLD_STRING_FMT;
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100171 field->field_show(m, show, &e->template_data[i]);
Roberto Sassua71dc652013-06-07 12:16:33 +0200172 }
Mimi Zoharbab73932009-02-04 09:06:59 -0500173 return 0;
174}
175
James Morris88e9d342009-09-22 16:43:43 -0700176static const struct seq_operations ima_measurments_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500177 .start = ima_measurements_start,
178 .next = ima_measurements_next,
179 .stop = ima_measurements_stop,
180 .show = ima_measurements_show
181};
182
183static int ima_measurements_open(struct inode *inode, struct file *file)
184{
185 return seq_open(file, &ima_measurments_seqops);
186}
187
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700188static const struct file_operations ima_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500189 .open = ima_measurements_open,
190 .read = seq_read,
191 .llseek = seq_lseek,
192 .release = seq_release,
193};
194
Mimi Zohar45b26132015-06-11 11:54:42 -0400195void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
Mimi Zoharbab73932009-02-04 09:06:59 -0500196{
Mimi Zohar45b26132015-06-11 11:54:42 -0400197 u32 i;
Mimi Zoharbab73932009-02-04 09:06:59 -0500198
Mimi Zohar140d8022013-03-11 20:29:47 -0400199 for (i = 0; i < size; i++)
Mimi Zoharbab73932009-02-04 09:06:59 -0500200 seq_printf(m, "%02x", *(digest + i));
201}
202
Mimi Zoharbab73932009-02-04 09:06:59 -0500203/* print in ascii */
204static int ima_ascii_measurements_show(struct seq_file *m, void *v)
205{
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;
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200209 char *template_name;
Roberto Sassua71dc652013-06-07 12:16:33 +0200210 int i;
Mimi Zoharbab73932009-02-04 09:06:59 -0500211
212 /* get entry */
213 e = qe->entry;
214 if (e == NULL)
215 return -1;
216
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200217 template_name = (e->template_desc->name[0] != '\0') ?
218 e->template_desc->name : e->template_desc->fmt;
219
Mimi Zoharbab73932009-02-04 09:06:59 -0500220 /* 1st: PCR used (config option) */
221 seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
222
223 /* 2nd: SHA1 template hash */
Mimi Zohar140d8022013-03-11 20:29:47 -0400224 ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
Mimi Zoharbab73932009-02-04 09:06:59 -0500225
226 /* 3th: template name */
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200227 seq_printf(m, " %s", template_name);
Mimi Zoharbab73932009-02-04 09:06:59 -0500228
229 /* 4th: template specific data */
Roberto Sassua71dc652013-06-07 12:16:33 +0200230 for (i = 0; i < e->template_desc->num_fields; i++) {
231 seq_puts(m, " ");
232 if (e->template_data[i].len == 0)
233 continue;
234
235 e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
236 &e->template_data[i]);
237 }
238 seq_puts(m, "\n");
Mimi Zoharbab73932009-02-04 09:06:59 -0500239 return 0;
240}
241
James Morris88e9d342009-09-22 16:43:43 -0700242static const struct seq_operations ima_ascii_measurements_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500243 .start = ima_measurements_start,
244 .next = ima_measurements_next,
245 .stop = ima_measurements_stop,
246 .show = ima_ascii_measurements_show
247};
248
249static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
250{
251 return seq_open(file, &ima_ascii_measurements_seqops);
252}
253
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700254static const struct file_operations ima_ascii_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500255 .open = ima_ascii_measurements_open,
256 .read = seq_read,
257 .llseek = seq_lseek,
258 .release = seq_release,
259};
260
Mimi Zohar4af46622009-02-04 09:07:00 -0500261static ssize_t ima_write_policy(struct file *file, const char __user *buf,
262 size_t datalen, loff_t *ppos)
263{
Eric Paris6ccd0452010-04-20 10:20:54 -0400264 char *data = NULL;
265 ssize_t result;
Petko Manolov38d859f2015-12-02 17:47:54 +0200266 int res;
267
268 res = mutex_lock_interruptible(&ima_write_mutex);
269 if (res)
270 return res;
Mimi Zohar4af46622009-02-04 09:07:00 -0500271
272 if (datalen >= PAGE_SIZE)
Eric Paris6ccd0452010-04-20 10:20:54 -0400273 datalen = PAGE_SIZE - 1;
274
275 /* No partial writes. */
276 result = -EINVAL;
277 if (*ppos != 0)
278 goto out;
279
280 result = -ENOMEM;
Mimi Zohar4af46622009-02-04 09:07:00 -0500281 data = kmalloc(datalen + 1, GFP_KERNEL);
282 if (!data)
Eric Paris6ccd0452010-04-20 10:20:54 -0400283 goto out;
Mimi Zohar4af46622009-02-04 09:07:00 -0500284
Mimi Zohar4af46622009-02-04 09:07:00 -0500285 *(data + datalen) = '\0';
Mimi Zohar4af46622009-02-04 09:07:00 -0500286
Eric Paris6ccd0452010-04-20 10:20:54 -0400287 result = -EFAULT;
288 if (copy_from_user(data, buf, datalen))
289 goto out;
290
291 result = ima_parse_add_rule(data);
292out:
293 if (result < 0)
294 valid_policy = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500295 kfree(data);
Petko Manolov38d859f2015-12-02 17:47:54 +0200296 mutex_unlock(&ima_write_mutex);
297
Eric Paris6ccd0452010-04-20 10:20:54 -0400298 return result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500299}
300
Mimi Zoharbab73932009-02-04 09:06:59 -0500301static struct dentry *ima_dir;
302static struct dentry *binary_runtime_measurements;
303static struct dentry *ascii_runtime_measurements;
304static struct dentry *runtime_measurements_count;
305static struct dentry *violations;
Mimi Zohar4af46622009-02-04 09:07:00 -0500306static struct dentry *ima_policy;
307
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300308enum ima_fs_flags {
309 IMA_FS_BUSY,
310};
311
312static unsigned long ima_fs_flags;
313
Petko Manolov80eae202015-12-02 17:47:56 +0200314#ifdef CONFIG_IMA_READ_POLICY
315static 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,
320};
321#endif
322
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500323/*
324 * ima_open_policy: sequentialize access to the policy file
325 */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200326static int ima_open_policy(struct inode *inode, struct file *filp)
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500327{
Petko Manolov80eae202015-12-02 17:47:56 +0200328 if (!(filp->f_flags & O_WRONLY)) {
329#ifndef CONFIG_IMA_READ_POLICY
Eric Parisf850a7c2009-05-12 15:13:55 -0400330 return -EACCES;
Petko Manolov80eae202015-12-02 17:47:56 +0200331#else
332 if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
333 return -EACCES;
334 if (!capable(CAP_SYS_ADMIN))
335 return -EPERM;
336 return seq_open(filp, &ima_policy_seqops);
337#endif
338 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300339 if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
340 return -EBUSY;
341 return 0;
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500342}
343
Mimi Zohar4af46622009-02-04 09:07:00 -0500344/*
345 * ima_release_policy - start using the new measure policy rules.
346 *
347 * Initially, ima_measure points to the default policy rules, now
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500348 * point to the new policy rules, and remove the securityfs policy file,
349 * assuming a valid policy.
Mimi Zohar4af46622009-02-04 09:07:00 -0500350 */
351static int ima_release_policy(struct inode *inode, struct file *file)
352{
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300353 const char *cause = valid_policy ? "completed" : "failed";
354
Petko Manolov80eae202015-12-02 17:47:56 +0200355 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
356 return 0;
357
Sasha Levin01127212015-12-22 08:51:23 -0500358 if (valid_policy && ima_check_policy() < 0) {
359 cause = "failed";
360 valid_policy = 0;
361 }
362
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300363 pr_info("IMA: policy update %s\n", cause);
364 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
365 "policy_update", cause, !valid_policy, 0);
366
Mimi Zohar4af46622009-02-04 09:07:00 -0500367 if (!valid_policy) {
368 ima_delete_rules();
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500369 valid_policy = 1;
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300370 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
Mimi Zohar4af46622009-02-04 09:07:00 -0500371 return 0;
372 }
Petko Manolov80eae202015-12-02 17:47:56 +0200373
Mimi Zohar4af46622009-02-04 09:07:00 -0500374 ima_update_policy();
Petko Manolov38d859f2015-12-02 17:47:54 +0200375#ifndef CONFIG_IMA_WRITE_POLICY
Mimi Zohar4af46622009-02-04 09:07:00 -0500376 securityfs_remove(ima_policy);
377 ima_policy = NULL;
Petko Manolov38d859f2015-12-02 17:47:54 +0200378#else
379 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
380#endif
Mimi Zohar4af46622009-02-04 09:07:00 -0500381 return 0;
382}
383
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700384static const struct file_operations ima_measure_policy_ops = {
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500385 .open = ima_open_policy,
Mimi Zohar4af46622009-02-04 09:07:00 -0500386 .write = ima_write_policy,
Petko Manolov80eae202015-12-02 17:47:56 +0200387 .read = seq_read,
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +0200388 .release = ima_release_policy,
389 .llseek = generic_file_llseek,
Mimi Zohar4af46622009-02-04 09:07:00 -0500390};
Mimi Zoharbab73932009-02-04 09:06:59 -0500391
Eric Paris932995f2009-05-21 15:43:32 -0400392int __init ima_fs_init(void)
Mimi Zoharbab73932009-02-04 09:06:59 -0500393{
394 ima_dir = securityfs_create_dir("ima", NULL);
395 if (IS_ERR(ima_dir))
396 return -1;
397
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))
403 goto out;
404
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))
410 goto out;
411
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))
417 goto out;
418
419 violations =
420 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
421 ima_dir, NULL, &ima_htable_violations_ops);
422 if (IS_ERR(violations))
423 goto out;
424
Petko Manolov80eae202015-12-02 17:47:56 +0200425 ima_policy = securityfs_create_file("policy", POLICY_FILE_FLAGS,
Mimi Zohar4af46622009-02-04 09:07:00 -0500426 ima_dir, NULL,
427 &ima_measure_policy_ops);
428 if (IS_ERR(ima_policy))
429 goto out;
Mimi Zoharbab73932009-02-04 09:06:59 -0500430
Mimi Zohar4af46622009-02-04 09:07:00 -0500431 return 0;
Mimi Zoharbab73932009-02-04 09:06:59 -0500432out:
Dmitry Kasatkin0ea4f8a2012-01-29 19:19:08 -0500433 securityfs_remove(violations);
Mimi Zoharbab73932009-02-04 09:06:59 -0500434 securityfs_remove(runtime_measurements_count);
435 securityfs_remove(ascii_runtime_measurements);
436 securityfs_remove(binary_runtime_measurements);
437 securityfs_remove(ima_dir);
Mimi Zohar4af46622009-02-04 09:07:00 -0500438 securityfs_remove(ima_policy);
Mimi Zoharbab73932009-02-04 09:06:59 -0500439 return -1;
440}