blob: 3d8e9d5db5aa5125b1136974f47f3a9062d3ddb9 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Mimi Zoharbab73932009-02-04 09:06:59 -05002/*
3 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
4 *
5 * Authors:
6 * Kylene Hall <kjhall@us.ibm.com>
7 * Reiner Sailer <sailer@us.ibm.com>
8 * Mimi Zohar <zohar@us.ibm.com>
9 *
Mimi Zoharbab73932009-02-04 09:06:59 -050010 * File: ima_fs.c
11 * implemenents security file system for reporting
12 * current measurement list and IMA statistics
13 */
Petr Vorelde636762018-04-24 16:30:38 +020014
Eric Parisf850a7c2009-05-12 15:13:55 -040015#include <linux/fcntl.h>
Scott Brandenb89999d02020-10-02 10:38:15 -070016#include <linux/kernel_read_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmaker876979c2018-12-09 15:36:29 -050018#include <linux/init.h>
Mimi Zoharbab73932009-02-04 09:06:59 -050019#include <linux/seq_file.h>
20#include <linux/rculist.h>
21#include <linux/rcupdate.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050022#include <linux/parser.h>
Dmitry Kasatkin7429b0922014-04-11 17:47:01 +030023#include <linux/vmalloc.h>
Mimi Zoharbab73932009-02-04 09:06:59 -050024
25#include "ima.h"
26
Petko Manolov38d859f2015-12-02 17:47:54 +020027static DEFINE_MUTEX(ima_write_mutex);
28
Mimi Zohard68a6fe2016-12-19 16:22:57 -080029bool ima_canonical_fmt;
30static int __init default_canonical_fmt_setup(char *str)
31{
32#ifdef __BIG_ENDIAN
Thomas Meyer39adb922017-10-07 16:02:21 +020033 ima_canonical_fmt = true;
Mimi Zohard68a6fe2016-12-19 16:22:57 -080034#endif
35 return 1;
36}
37__setup("ima_canonical_fmt", default_canonical_fmt_setup);
38
Mimi Zohar4af46622009-02-04 09:07:00 -050039static int valid_policy = 1;
Eric Biggers1e4c8da2018-09-07 14:33:24 -070040
Mimi Zoharbab73932009-02-04 09:06:59 -050041static ssize_t ima_show_htable_value(char __user *buf, size_t count,
42 loff_t *ppos, atomic_long_t *val)
43{
Eric Biggers1e4c8da2018-09-07 14:33:24 -070044 char tmpbuf[32]; /* greater than largest 'long' string value */
Mimi Zoharbab73932009-02-04 09:06:59 -050045 ssize_t len;
46
Eric Biggers1e4c8da2018-09-07 14:33:24 -070047 len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", atomic_long_read(val));
Mimi Zoharbab73932009-02-04 09:06:59 -050048 return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
49}
50
51static ssize_t ima_show_htable_violations(struct file *filp,
52 char __user *buf,
53 size_t count, loff_t *ppos)
54{
55 return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
56}
57
Alexey Dobriyan828c0952009-10-01 15:43:56 -070058static const struct file_operations ima_htable_violations_ops = {
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +020059 .read = ima_show_htable_violations,
60 .llseek = generic_file_llseek,
Mimi Zoharbab73932009-02-04 09:06:59 -050061};
62
63static ssize_t ima_show_measurements_count(struct file *filp,
64 char __user *buf,
65 size_t count, loff_t *ppos)
66{
67 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
68
69}
70
Alexey Dobriyan828c0952009-10-01 15:43:56 -070071static const struct file_operations ima_measurements_count_ops = {
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +020072 .read = ima_show_measurements_count,
73 .llseek = generic_file_llseek,
Mimi Zoharbab73932009-02-04 09:06:59 -050074};
75
76/* returns pointer to hlist_node */
77static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
78{
79 loff_t l = *pos;
80 struct ima_queue_entry *qe;
81
82 /* we need a lock since pos could point beyond last element */
83 rcu_read_lock();
84 list_for_each_entry_rcu(qe, &ima_measurements, later) {
85 if (!l--) {
86 rcu_read_unlock();
87 return qe;
88 }
89 }
90 rcu_read_unlock();
91 return NULL;
92}
93
94static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
95{
96 struct ima_queue_entry *qe = v;
97
98 /* lock protects when reading beyond last element
99 * against concurrent list-extension
100 */
101 rcu_read_lock();
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900102 qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
Mimi Zoharbab73932009-02-04 09:06:59 -0500103 rcu_read_unlock();
104 (*pos)++;
105
106 return (&qe->later == &ima_measurements) ? NULL : qe;
107}
108
109static void ima_measurements_stop(struct seq_file *m, void *v)
110{
111}
112
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200113void ima_putc(struct seq_file *m, void *data, int datalen)
Mimi Zoharbab73932009-02-04 09:06:59 -0500114{
115 while (datalen--)
116 seq_putc(m, *(char *)data++);
117}
118
119/* print format:
120 * 32bit-le=pcr#
121 * char[20]=template digest
122 * 32bit-le=template name size
123 * char[n]=template name
Roberto Sassua71dc652013-06-07 12:16:33 +0200124 * [eventdata length]
Mimi Zoharbab73932009-02-04 09:06:59 -0500125 * eventdata[n]=template specific data
126 */
Mimi Zohar7b8589c2016-12-19 16:22:48 -0800127int ima_measurements_show(struct seq_file *m, void *v)
Mimi Zoharbab73932009-02-04 09:06:59 -0500128{
129 /* the list never shrinks, so we don't need a lock here */
130 struct ima_queue_entry *qe = v;
131 struct ima_template_entry *e;
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200132 char *template_name;
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800133 u32 pcr, namelen, template_data_len; /* temporary fields */
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100134 bool is_ima_template = false;
Roberto Sassua71dc652013-06-07 12:16:33 +0200135 int i;
Mimi Zoharbab73932009-02-04 09:06:59 -0500136
137 /* get entry */
138 e = qe->entry;
139 if (e == NULL)
140 return -1;
141
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200142 template_name = (e->template_desc->name[0] != '\0') ?
143 e->template_desc->name : e->template_desc->fmt;
144
Mimi Zoharbab73932009-02-04 09:06:59 -0500145 /*
146 * 1st: PCRIndex
Eric Richter5f6f0272016-06-01 13:14:04 -0500147 * PCR used defaults to the same (config option) in
148 * little-endian format, unless set in policy
Mimi Zoharbab73932009-02-04 09:06:59 -0500149 */
Roberto Sassu6b262852021-06-08 14:31:22 +0200150 pcr = !ima_canonical_fmt ? e->pcr : (__force u32)cpu_to_le32(e->pcr);
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800151 ima_putc(m, &pcr, sizeof(e->pcr));
Mimi Zoharbab73932009-02-04 09:06:59 -0500152
153 /* 2nd: template digest */
Roberto Sassuaa724fe2020-03-25 11:47:09 +0100154 ima_putc(m, e->digests[ima_sha1_idx].digest, TPM_DIGEST_SIZE);
Mimi Zoharbab73932009-02-04 09:06:59 -0500155
156 /* 3rd: template name size */
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800157 namelen = !ima_canonical_fmt ? strlen(template_name) :
Roberto Sassu6b262852021-06-08 14:31:22 +0200158 (__force u32)cpu_to_le32(strlen(template_name));
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200159 ima_putc(m, &namelen, sizeof(namelen));
Mimi Zoharbab73932009-02-04 09:06:59 -0500160
161 /* 4th: template name */
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800162 ima_putc(m, template_name, strlen(template_name));
Mimi Zoharbab73932009-02-04 09:06:59 -0500163
Roberto Sassua71dc652013-06-07 12:16:33 +0200164 /* 5th: template length (except for 'ima' template) */
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200165 if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100166 is_ima_template = true;
167
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800168 if (!is_ima_template) {
169 template_data_len = !ima_canonical_fmt ? e->template_data_len :
Roberto Sassu6b262852021-06-08 14:31:22 +0200170 (__force u32)cpu_to_le32(e->template_data_len);
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800171 ima_putc(m, &template_data_len, sizeof(e->template_data_len));
172 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200173
174 /* 6th: template specific data */
175 for (i = 0; i < e->template_desc->num_fields; i++) {
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100176 enum ima_show_type show = IMA_SHOW_BINARY;
Eric Biggersb2724d52018-09-07 13:22:23 -0700177 const struct ima_template_field *field =
178 e->template_desc->fields[i];
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100179
180 if (is_ima_template && strcmp(field->field_id, "d") == 0)
181 show = IMA_SHOW_BINARY_NO_FIELD_LEN;
Roberto Sassuc019e302014-02-03 13:56:04 +0100182 if (is_ima_template && strcmp(field->field_id, "n") == 0)
183 show = IMA_SHOW_BINARY_OLD_STRING_FMT;
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100184 field->field_show(m, show, &e->template_data[i]);
Roberto Sassua71dc652013-06-07 12:16:33 +0200185 }
Mimi Zoharbab73932009-02-04 09:06:59 -0500186 return 0;
187}
188
James Morris88e9d342009-09-22 16:43:43 -0700189static const struct seq_operations ima_measurments_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500190 .start = ima_measurements_start,
191 .next = ima_measurements_next,
192 .stop = ima_measurements_stop,
193 .show = ima_measurements_show
194};
195
196static int ima_measurements_open(struct inode *inode, struct file *file)
197{
198 return seq_open(file, &ima_measurments_seqops);
199}
200
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700201static const struct file_operations ima_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500202 .open = ima_measurements_open,
203 .read = seq_read,
204 .llseek = seq_lseek,
205 .release = seq_release,
206};
207
Mimi Zohar45b26132015-06-11 11:54:42 -0400208void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
Mimi Zoharbab73932009-02-04 09:06:59 -0500209{
Mimi Zohar45b26132015-06-11 11:54:42 -0400210 u32 i;
Mimi Zoharbab73932009-02-04 09:06:59 -0500211
Mimi Zohar140d8022013-03-11 20:29:47 -0400212 for (i = 0; i < size; i++)
Mimi Zoharbab73932009-02-04 09:06:59 -0500213 seq_printf(m, "%02x", *(digest + i));
214}
215
Mimi Zoharbab73932009-02-04 09:06:59 -0500216/* print in ascii */
217static int ima_ascii_measurements_show(struct seq_file *m, void *v)
218{
219 /* the list never shrinks, so we don't need a lock here */
220 struct ima_queue_entry *qe = v;
221 struct ima_template_entry *e;
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200222 char *template_name;
Roberto Sassua71dc652013-06-07 12:16:33 +0200223 int i;
Mimi Zoharbab73932009-02-04 09:06:59 -0500224
225 /* get entry */
226 e = qe->entry;
227 if (e == NULL)
228 return -1;
229
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200230 template_name = (e->template_desc->name[0] != '\0') ?
231 e->template_desc->name : e->template_desc->fmt;
232
Mimi Zoharbab73932009-02-04 09:06:59 -0500233 /* 1st: PCR used (config option) */
Eric Richter5f6f0272016-06-01 13:14:04 -0500234 seq_printf(m, "%2d ", e->pcr);
Mimi Zoharbab73932009-02-04 09:06:59 -0500235
236 /* 2nd: SHA1 template hash */
Roberto Sassuaa724fe2020-03-25 11:47:09 +0100237 ima_print_digest(m, e->digests[ima_sha1_idx].digest, TPM_DIGEST_SIZE);
Mimi Zoharbab73932009-02-04 09:06:59 -0500238
239 /* 3th: template name */
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200240 seq_printf(m, " %s", template_name);
Mimi Zoharbab73932009-02-04 09:06:59 -0500241
242 /* 4th: template specific data */
Roberto Sassua71dc652013-06-07 12:16:33 +0200243 for (i = 0; i < e->template_desc->num_fields; i++) {
244 seq_puts(m, " ");
245 if (e->template_data[i].len == 0)
246 continue;
247
248 e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
249 &e->template_data[i]);
250 }
251 seq_puts(m, "\n");
Mimi Zoharbab73932009-02-04 09:06:59 -0500252 return 0;
253}
254
James Morris88e9d342009-09-22 16:43:43 -0700255static const struct seq_operations ima_ascii_measurements_seqops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500256 .start = ima_measurements_start,
257 .next = ima_measurements_next,
258 .stop = ima_measurements_stop,
259 .show = ima_ascii_measurements_show
260};
261
262static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
263{
264 return seq_open(file, &ima_ascii_measurements_seqops);
265}
266
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700267static const struct file_operations ima_ascii_measurements_ops = {
Mimi Zoharbab73932009-02-04 09:06:59 -0500268 .open = ima_ascii_measurements_open,
269 .read = seq_read,
270 .llseek = seq_lseek,
271 .release = seq_release,
272};
273
Dmitry Kasatkin7429b0922014-04-11 17:47:01 +0300274static ssize_t ima_read_policy(char *path)
275{
Kees Cookc3074592020-10-02 10:38:13 -0700276 void *data = NULL;
Dmitry Kasatkin7429b0922014-04-11 17:47:01 +0300277 char *datap;
Kees Cookf7a4f682020-10-02 10:38:17 -0700278 size_t size;
Dmitry Kasatkin7429b0922014-04-11 17:47:01 +0300279 int rc, pathlen = strlen(path);
280
281 char *p;
282
283 /* remove \n */
284 datap = path;
285 strsep(&datap, "\n");
286
Kees Cook0fa8e082020-10-02 10:38:25 -0700287 rc = kernel_read_file_from_path(path, 0, &data, INT_MAX, NULL,
288 READING_POLICY);
Dmitry Kasatkin7429b0922014-04-11 17:47:01 +0300289 if (rc < 0) {
290 pr_err("Unable to open file: %s (%d)", path, rc);
291 return rc;
292 }
Kees Cookf7a4f682020-10-02 10:38:17 -0700293 size = rc;
294 rc = 0;
Dmitry Kasatkin7429b0922014-04-11 17:47:01 +0300295
296 datap = data;
297 while (size > 0 && (p = strsep(&datap, "\n"))) {
298 pr_debug("rule: %s\n", p);
299 rc = ima_parse_add_rule(p);
300 if (rc < 0)
301 break;
302 size -= rc;
303 }
304
305 vfree(data);
306 if (rc < 0)
307 return rc;
308 else if (size)
309 return -EINVAL;
310 else
311 return pathlen;
312}
313
Mimi Zohar4af46622009-02-04 09:07:00 -0500314static ssize_t ima_write_policy(struct file *file, const char __user *buf,
315 size_t datalen, loff_t *ppos)
316{
Petko Manolov6427e6c2016-01-03 17:36:38 +0200317 char *data;
Eric Paris6ccd0452010-04-20 10:20:54 -0400318 ssize_t result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500319
320 if (datalen >= PAGE_SIZE)
Eric Paris6ccd0452010-04-20 10:20:54 -0400321 datalen = PAGE_SIZE - 1;
322
323 /* No partial writes. */
324 result = -EINVAL;
325 if (*ppos != 0)
326 goto out;
327
Geliang Tangb4e28032017-05-06 23:40:18 +0800328 data = memdup_user_nul(buf, datalen);
329 if (IS_ERR(data)) {
330 result = PTR_ERR(data);
Eric Paris6ccd0452010-04-20 10:20:54 -0400331 goto out;
Geliang Tangb4e28032017-05-06 23:40:18 +0800332 }
Eric Paris6ccd0452010-04-20 10:20:54 -0400333
Petko Manolov6427e6c2016-01-03 17:36:38 +0200334 result = mutex_lock_interruptible(&ima_write_mutex);
335 if (result < 0)
336 goto out_free;
Petko Manolov6427e6c2016-01-03 17:36:38 +0200337
Mimi Zohar19f8a842016-01-15 10:17:12 -0500338 if (data[0] == '/') {
Dmitry Kasatkin7429b0922014-04-11 17:47:01 +0300339 result = ima_read_policy(data);
Mimi Zohar19f8a842016-01-15 10:17:12 -0500340 } else if (ima_appraise & IMA_APPRAISE_POLICY) {
Petr Vorelde636762018-04-24 16:30:38 +0200341 pr_err("signed policy file (specified as an absolute pathname) required\n");
Mimi Zohar19f8a842016-01-15 10:17:12 -0500342 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
343 "policy_update", "signed policy required",
344 1, 0);
Roberto Sassu2e3a34e2020-04-27 12:31:28 +0200345 result = -EACCES;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500346 } else {
Dmitry Kasatkin7429b0922014-04-11 17:47:01 +0300347 result = ima_parse_add_rule(data);
Mimi Zohar19f8a842016-01-15 10:17:12 -0500348 }
Dmitry Kasatkin7429b0922014-04-11 17:47:01 +0300349 mutex_unlock(&ima_write_mutex);
Petko Manolov6427e6c2016-01-03 17:36:38 +0200350out_free:
351 kfree(data);
Eric Paris6ccd0452010-04-20 10:20:54 -0400352out:
353 if (result < 0)
354 valid_policy = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +0200355
Eric Paris6ccd0452010-04-20 10:20:54 -0400356 return result;
Mimi Zohar4af46622009-02-04 09:07:00 -0500357}
358
Mimi Zoharbab73932009-02-04 09:06:59 -0500359static struct dentry *ima_dir;
Matthew Garrett0c343af2018-05-11 16:12:34 -0700360static struct dentry *ima_symlink;
Mimi Zoharbab73932009-02-04 09:06:59 -0500361static struct dentry *binary_runtime_measurements;
362static struct dentry *ascii_runtime_measurements;
363static struct dentry *runtime_measurements_count;
364static struct dentry *violations;
Mimi Zohar4af46622009-02-04 09:07:00 -0500365static struct dentry *ima_policy;
366
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300367enum ima_fs_flags {
368 IMA_FS_BUSY,
369};
370
371static unsigned long ima_fs_flags;
372
Petko Manolov80eae202015-12-02 17:47:56 +0200373#ifdef CONFIG_IMA_READ_POLICY
374static const struct seq_operations ima_policy_seqops = {
375 .start = ima_policy_start,
376 .next = ima_policy_next,
377 .stop = ima_policy_stop,
378 .show = ima_policy_show,
379};
380#endif
381
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500382/*
383 * ima_open_policy: sequentialize access to the policy file
384 */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200385static int ima_open_policy(struct inode *inode, struct file *filp)
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500386{
Petko Manolov80eae202015-12-02 17:47:56 +0200387 if (!(filp->f_flags & O_WRONLY)) {
388#ifndef CONFIG_IMA_READ_POLICY
Eric Parisf850a7c2009-05-12 15:13:55 -0400389 return -EACCES;
Petko Manolov80eae202015-12-02 17:47:56 +0200390#else
391 if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
392 return -EACCES;
393 if (!capable(CAP_SYS_ADMIN))
394 return -EPERM;
395 return seq_open(filp, &ima_policy_seqops);
396#endif
397 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300398 if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
399 return -EBUSY;
400 return 0;
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500401}
402
Mimi Zohar4af46622009-02-04 09:07:00 -0500403/*
404 * ima_release_policy - start using the new measure policy rules.
405 *
406 * Initially, ima_measure points to the default policy rules, now
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500407 * point to the new policy rules, and remove the securityfs policy file,
408 * assuming a valid policy.
Mimi Zohar4af46622009-02-04 09:07:00 -0500409 */
410static int ima_release_policy(struct inode *inode, struct file *file)
411{
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300412 const char *cause = valid_policy ? "completed" : "failed";
413
Petko Manolov80eae202015-12-02 17:47:56 +0200414 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
Eric Richter9a11a182016-10-13 17:47:36 -0500415 return seq_release(inode, file);
Petko Manolov80eae202015-12-02 17:47:56 +0200416
Sasha Levin01127212015-12-22 08:51:23 -0500417 if (valid_policy && ima_check_policy() < 0) {
418 cause = "failed";
419 valid_policy = 0;
420 }
421
Petr Vorelde636762018-04-24 16:30:38 +0200422 pr_info("policy update %s\n", cause);
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300423 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
424 "policy_update", cause, !valid_policy, 0);
425
Mimi Zohar4af46622009-02-04 09:07:00 -0500426 if (!valid_policy) {
427 ima_delete_rules();
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500428 valid_policy = 1;
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300429 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
Mimi Zohar4af46622009-02-04 09:07:00 -0500430 return 0;
431 }
Petko Manolov80eae202015-12-02 17:47:56 +0200432
Mimi Zohar4af46622009-02-04 09:07:00 -0500433 ima_update_policy();
Mimi Zohar20686262017-06-27 16:10:39 -0400434#if !defined(CONFIG_IMA_WRITE_POLICY) && !defined(CONFIG_IMA_READ_POLICY)
Mimi Zohar4af46622009-02-04 09:07:00 -0500435 securityfs_remove(ima_policy);
436 ima_policy = NULL;
Mimi Zohar20686262017-06-27 16:10:39 -0400437#elif defined(CONFIG_IMA_WRITE_POLICY)
Petko Manolov38d859f2015-12-02 17:47:54 +0200438 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
Petr Vorelffb122d2018-04-20 15:28:57 +0200439#elif defined(CONFIG_IMA_READ_POLICY)
440 inode->i_mode &= ~S_IWUSR;
Petko Manolov38d859f2015-12-02 17:47:54 +0200441#endif
Mimi Zohar4af46622009-02-04 09:07:00 -0500442 return 0;
443}
444
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700445static const struct file_operations ima_measure_policy_ops = {
Mimi Zoharf4bd8572009-02-04 09:07:01 -0500446 .open = ima_open_policy,
Mimi Zohar4af46622009-02-04 09:07:00 -0500447 .write = ima_write_policy,
Petko Manolov80eae202015-12-02 17:47:56 +0200448 .read = seq_read,
Arnd Bergmanncdcd90f2010-07-07 23:40:15 +0200449 .release = ima_release_policy,
450 .llseek = generic_file_llseek,
Mimi Zohar4af46622009-02-04 09:07:00 -0500451};
Mimi Zoharbab73932009-02-04 09:06:59 -0500452
Eric Paris932995f2009-05-21 15:43:32 -0400453int __init ima_fs_init(void)
Mimi Zoharbab73932009-02-04 09:06:59 -0500454{
Matthew Garrett0c343af2018-05-11 16:12:34 -0700455 ima_dir = securityfs_create_dir("ima", integrity_dir);
Mimi Zoharbab73932009-02-04 09:06:59 -0500456 if (IS_ERR(ima_dir))
457 return -1;
458
Matthew Garrett0c343af2018-05-11 16:12:34 -0700459 ima_symlink = securityfs_create_symlink("ima", NULL, "integrity/ima",
460 NULL);
461 if (IS_ERR(ima_symlink))
462 goto out;
463
Mimi Zoharbab73932009-02-04 09:06:59 -0500464 binary_runtime_measurements =
465 securityfs_create_file("binary_runtime_measurements",
466 S_IRUSR | S_IRGRP, ima_dir, NULL,
467 &ima_measurements_ops);
468 if (IS_ERR(binary_runtime_measurements))
469 goto out;
470
471 ascii_runtime_measurements =
472 securityfs_create_file("ascii_runtime_measurements",
473 S_IRUSR | S_IRGRP, ima_dir, NULL,
474 &ima_ascii_measurements_ops);
475 if (IS_ERR(ascii_runtime_measurements))
476 goto out;
477
478 runtime_measurements_count =
479 securityfs_create_file("runtime_measurements_count",
480 S_IRUSR | S_IRGRP, ima_dir, NULL,
481 &ima_measurements_count_ops);
482 if (IS_ERR(runtime_measurements_count))
483 goto out;
484
485 violations =
486 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
487 ima_dir, NULL, &ima_htable_violations_ops);
488 if (IS_ERR(violations))
489 goto out;
490
Petko Manolov80eae202015-12-02 17:47:56 +0200491 ima_policy = securityfs_create_file("policy", POLICY_FILE_FLAGS,
Mimi Zohar4af46622009-02-04 09:07:00 -0500492 ima_dir, NULL,
493 &ima_measure_policy_ops);
494 if (IS_ERR(ima_policy))
495 goto out;
Mimi Zoharbab73932009-02-04 09:06:59 -0500496
Mimi Zohar4af46622009-02-04 09:07:00 -0500497 return 0;
Mimi Zoharbab73932009-02-04 09:06:59 -0500498out:
Dmitry Kasatkin0ea4f8a2012-01-29 19:19:08 -0500499 securityfs_remove(violations);
Mimi Zoharbab73932009-02-04 09:06:59 -0500500 securityfs_remove(runtime_measurements_count);
501 securityfs_remove(ascii_runtime_measurements);
502 securityfs_remove(binary_runtime_measurements);
Matthew Garrett0c343af2018-05-11 16:12:34 -0700503 securityfs_remove(ima_symlink);
Mimi Zoharbab73932009-02-04 09:06:59 -0500504 securityfs_remove(ima_dir);
Mimi Zohar4af46622009-02-04 09:07:00 -0500505 securityfs_remove(ima_policy);
Mimi Zoharbab73932009-02-04 09:06:59 -0500506 return -1;
507}