]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - security/integrity/ima/ima_fs.c
ima: Switch to dynamically allocated buffer for template digests
[mirror_ubuntu-hirsute-kernel.git] / security / integrity / ima / ima_fs.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
bab73937
MZ
2/*
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 *
bab73937
MZ
10 * File: ima_fs.c
11 * implemenents security file system for reporting
12 * current measurement list and IMA statistics
13 */
de636769 14
f850a7c0 15#include <linux/fcntl.h>
5a0e3ad6 16#include <linux/slab.h>
876979c9 17#include <linux/init.h>
bab73937
MZ
18#include <linux/seq_file.h>
19#include <linux/rculist.h>
20#include <linux/rcupdate.h>
4af4662f 21#include <linux/parser.h>
7429b092 22#include <linux/vmalloc.h>
bab73937
MZ
23
24#include "ima.h"
25
38d859f9
PM
26static DEFINE_MUTEX(ima_write_mutex);
27
d68a6fe9
MZ
28bool ima_canonical_fmt;
29static int __init default_canonical_fmt_setup(char *str)
30{
31#ifdef __BIG_ENDIAN
39adb925 32 ima_canonical_fmt = true;
d68a6fe9
MZ
33#endif
34 return 1;
35}
36__setup("ima_canonical_fmt", default_canonical_fmt_setup);
37
4af4662f 38static int valid_policy = 1;
1e4c8daf 39
bab73937
MZ
40static ssize_t ima_show_htable_value(char __user *buf, size_t count,
41 loff_t *ppos, atomic_long_t *val)
42{
1e4c8daf 43 char tmpbuf[32]; /* greater than largest 'long' string value */
bab73937
MZ
44 ssize_t len;
45
1e4c8daf 46 len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", atomic_long_read(val));
bab73937
MZ
47 return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
48}
49
50static ssize_t ima_show_htable_violations(struct file *filp,
51 char __user *buf,
52 size_t count, loff_t *ppos)
53{
54 return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
55}
56
828c0950 57static const struct file_operations ima_htable_violations_ops = {
cdcd90f9
AB
58 .read = ima_show_htable_violations,
59 .llseek = generic_file_llseek,
bab73937
MZ
60};
61
62static ssize_t ima_show_measurements_count(struct file *filp,
63 char __user *buf,
64 size_t count, loff_t *ppos)
65{
66 return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
67
68}
69
828c0950 70static const struct file_operations ima_measurements_count_ops = {
cdcd90f9
AB
71 .read = ima_show_measurements_count,
72 .llseek = generic_file_llseek,
bab73937
MZ
73};
74
75/* returns pointer to hlist_node */
76static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
77{
78 loff_t l = *pos;
79 struct ima_queue_entry *qe;
80
81 /* we need a lock since pos could point beyond last element */
82 rcu_read_lock();
83 list_for_each_entry_rcu(qe, &ima_measurements, later) {
84 if (!l--) {
85 rcu_read_unlock();
86 return qe;
87 }
88 }
89 rcu_read_unlock();
90 return NULL;
91}
92
93static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
94{
95 struct ima_queue_entry *qe = v;
96
97 /* lock protects when reading beyond last element
98 * against concurrent list-extension
99 */
100 rcu_read_lock();
089bc8e9 101 qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
bab73937
MZ
102 rcu_read_unlock();
103 (*pos)++;
104
105 return (&qe->later == &ima_measurements) ? NULL : qe;
106}
107
108static void ima_measurements_stop(struct seq_file *m, void *v)
109{
110}
111
3ce1217d 112void ima_putc(struct seq_file *m, void *data, int datalen)
bab73937
MZ
113{
114 while (datalen--)
115 seq_putc(m, *(char *)data++);
116}
117
118/* print format:
119 * 32bit-le=pcr#
120 * char[20]=template digest
121 * 32bit-le=template name size
122 * char[n]=template name
a71dc65d 123 * [eventdata length]
bab73937
MZ
124 * eventdata[n]=template specific data
125 */
7b8589cc 126int ima_measurements_show(struct seq_file *m, void *v)
bab73937
MZ
127{
128 /* the list never shrinks, so we don't need a lock here */
129 struct ima_queue_entry *qe = v;
130 struct ima_template_entry *e;
7dbdb420 131 char *template_name;
d68a6fe9 132 u32 pcr, namelen, template_data_len; /* temporary fields */
3e8e5503 133 bool is_ima_template = false;
a71dc65d 134 int i;
bab73937
MZ
135
136 /* get entry */
137 e = qe->entry;
138 if (e == NULL)
139 return -1;
140
7dbdb420
RS
141 template_name = (e->template_desc->name[0] != '\0') ?
142 e->template_desc->name : e->template_desc->fmt;
143
bab73937
MZ
144 /*
145 * 1st: PCRIndex
5f6f027b
ER
146 * PCR used defaults to the same (config option) in
147 * little-endian format, unless set in policy
bab73937 148 */
d68a6fe9
MZ
149 pcr = !ima_canonical_fmt ? e->pcr : cpu_to_le32(e->pcr);
150 ima_putc(m, &pcr, sizeof(e->pcr));
bab73937
MZ
151
152 /* 2nd: template digest */
aa724fe1 153 ima_putc(m, e->digests[ima_sha1_idx].digest, TPM_DIGEST_SIZE);
bab73937
MZ
154
155 /* 3rd: template name size */
d68a6fe9
MZ
156 namelen = !ima_canonical_fmt ? strlen(template_name) :
157 cpu_to_le32(strlen(template_name));
2bb930ab 158 ima_putc(m, &namelen, sizeof(namelen));
bab73937
MZ
159
160 /* 4th: template name */
d68a6fe9 161 ima_putc(m, template_name, strlen(template_name));
a71dc65d
RS
162
163 /* 5th: template length (except for 'ima' template) */
7dbdb420 164 if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
3e8e5503
RS
165 is_ima_template = true;
166
d68a6fe9
MZ
167 if (!is_ima_template) {
168 template_data_len = !ima_canonical_fmt ? e->template_data_len :
169 cpu_to_le32(e->template_data_len);
170 ima_putc(m, &template_data_len, sizeof(e->template_data_len));
171 }
bab73937 172
a71dc65d
RS
173 /* 6th: template specific data */
174 for (i = 0; i < e->template_desc->num_fields; i++) {
3e8e5503 175 enum ima_show_type show = IMA_SHOW_BINARY;
b2724d58
EB
176 const struct ima_template_field *field =
177 e->template_desc->fields[i];
3e8e5503
RS
178
179 if (is_ima_template && strcmp(field->field_id, "d") == 0)
180 show = IMA_SHOW_BINARY_NO_FIELD_LEN;
c019e307
RS
181 if (is_ima_template && strcmp(field->field_id, "n") == 0)
182 show = IMA_SHOW_BINARY_OLD_STRING_FMT;
3e8e5503 183 field->field_show(m, show, &e->template_data[i]);
a71dc65d 184 }
bab73937
MZ
185 return 0;
186}
187
88e9d34c 188static const struct seq_operations ima_measurments_seqops = {
bab73937
MZ
189 .start = ima_measurements_start,
190 .next = ima_measurements_next,
191 .stop = ima_measurements_stop,
192 .show = ima_measurements_show
193};
194
195static int ima_measurements_open(struct inode *inode, struct file *file)
196{
197 return seq_open(file, &ima_measurments_seqops);
198}
199
828c0950 200static const struct file_operations ima_measurements_ops = {
bab73937
MZ
201 .open = ima_measurements_open,
202 .read = seq_read,
203 .llseek = seq_lseek,
204 .release = seq_release,
205};
206
45b26133 207void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
bab73937 208{
45b26133 209 u32 i;
bab73937 210
140d8022 211 for (i = 0; i < size; i++)
bab73937
MZ
212 seq_printf(m, "%02x", *(digest + i));
213}
214
bab73937
MZ
215/* print in ascii */
216static int ima_ascii_measurements_show(struct seq_file *m, void *v)
217{
218 /* the list never shrinks, so we don't need a lock here */
219 struct ima_queue_entry *qe = v;
220 struct ima_template_entry *e;
7dbdb420 221 char *template_name;
a71dc65d 222 int i;
bab73937
MZ
223
224 /* get entry */
225 e = qe->entry;
226 if (e == NULL)
227 return -1;
228
7dbdb420
RS
229 template_name = (e->template_desc->name[0] != '\0') ?
230 e->template_desc->name : e->template_desc->fmt;
231
bab73937 232 /* 1st: PCR used (config option) */
5f6f027b 233 seq_printf(m, "%2d ", e->pcr);
bab73937
MZ
234
235 /* 2nd: SHA1 template hash */
aa724fe1 236 ima_print_digest(m, e->digests[ima_sha1_idx].digest, TPM_DIGEST_SIZE);
bab73937
MZ
237
238 /* 3th: template name */
7dbdb420 239 seq_printf(m, " %s", template_name);
bab73937
MZ
240
241 /* 4th: template specific data */
a71dc65d
RS
242 for (i = 0; i < e->template_desc->num_fields; i++) {
243 seq_puts(m, " ");
244 if (e->template_data[i].len == 0)
245 continue;
246
247 e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
248 &e->template_data[i]);
249 }
250 seq_puts(m, "\n");
bab73937
MZ
251 return 0;
252}
253
88e9d34c 254static const struct seq_operations ima_ascii_measurements_seqops = {
bab73937
MZ
255 .start = ima_measurements_start,
256 .next = ima_measurements_next,
257 .stop = ima_measurements_stop,
258 .show = ima_ascii_measurements_show
259};
260
261static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
262{
263 return seq_open(file, &ima_ascii_measurements_seqops);
264}
265
828c0950 266static const struct file_operations ima_ascii_measurements_ops = {
bab73937
MZ
267 .open = ima_ascii_measurements_open,
268 .read = seq_read,
269 .llseek = seq_lseek,
270 .release = seq_release,
271};
272
7429b092
DK
273static ssize_t ima_read_policy(char *path)
274{
275 void *data;
276 char *datap;
277 loff_t size;
278 int rc, pathlen = strlen(path);
279
280 char *p;
281
282 /* remove \n */
283 datap = path;
284 strsep(&datap, "\n");
285
286 rc = kernel_read_file_from_path(path, &data, &size, 0, READING_POLICY);
287 if (rc < 0) {
288 pr_err("Unable to open file: %s (%d)", path, rc);
289 return rc;
290 }
291
292 datap = data;
293 while (size > 0 && (p = strsep(&datap, "\n"))) {
294 pr_debug("rule: %s\n", p);
295 rc = ima_parse_add_rule(p);
296 if (rc < 0)
297 break;
298 size -= rc;
299 }
300
301 vfree(data);
302 if (rc < 0)
303 return rc;
304 else if (size)
305 return -EINVAL;
306 else
307 return pathlen;
308}
309
4af4662f
MZ
310static ssize_t ima_write_policy(struct file *file, const char __user *buf,
311 size_t datalen, loff_t *ppos)
312{
6427e6c7 313 char *data;
6ccd0456 314 ssize_t result;
4af4662f
MZ
315
316 if (datalen >= PAGE_SIZE)
6ccd0456
EP
317 datalen = PAGE_SIZE - 1;
318
319 /* No partial writes. */
320 result = -EINVAL;
321 if (*ppos != 0)
322 goto out;
323
b4e28030
GT
324 data = memdup_user_nul(buf, datalen);
325 if (IS_ERR(data)) {
326 result = PTR_ERR(data);
6ccd0456 327 goto out;
b4e28030 328 }
6ccd0456 329
6427e6c7
PM
330 result = mutex_lock_interruptible(&ima_write_mutex);
331 if (result < 0)
332 goto out_free;
6427e6c7 333
19f8a847 334 if (data[0] == '/') {
7429b092 335 result = ima_read_policy(data);
19f8a847 336 } else if (ima_appraise & IMA_APPRAISE_POLICY) {
de636769 337 pr_err("signed policy file (specified as an absolute pathname) required\n");
19f8a847
MZ
338 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
339 "policy_update", "signed policy required",
340 1, 0);
341 if (ima_appraise & IMA_APPRAISE_ENFORCE)
342 result = -EACCES;
343 } else {
7429b092 344 result = ima_parse_add_rule(data);
19f8a847 345 }
7429b092 346 mutex_unlock(&ima_write_mutex);
6427e6c7
PM
347out_free:
348 kfree(data);
6ccd0456
EP
349out:
350 if (result < 0)
351 valid_policy = 0;
38d859f9 352
6ccd0456 353 return result;
4af4662f
MZ
354}
355
bab73937 356static struct dentry *ima_dir;
0c343af8 357static struct dentry *ima_symlink;
bab73937
MZ
358static struct dentry *binary_runtime_measurements;
359static struct dentry *ascii_runtime_measurements;
360static struct dentry *runtime_measurements_count;
361static struct dentry *violations;
4af4662f
MZ
362static struct dentry *ima_policy;
363
0716abbb
DK
364enum ima_fs_flags {
365 IMA_FS_BUSY,
366};
367
368static unsigned long ima_fs_flags;
369
80eae209
PM
370#ifdef CONFIG_IMA_READ_POLICY
371static const struct seq_operations ima_policy_seqops = {
372 .start = ima_policy_start,
373 .next = ima_policy_next,
374 .stop = ima_policy_stop,
375 .show = ima_policy_show,
376};
377#endif
378
f4bd857b
MZ
379/*
380 * ima_open_policy: sequentialize access to the policy file
381 */
2bb930ab 382static int ima_open_policy(struct inode *inode, struct file *filp)
f4bd857b 383{
80eae209
PM
384 if (!(filp->f_flags & O_WRONLY)) {
385#ifndef CONFIG_IMA_READ_POLICY
f850a7c0 386 return -EACCES;
80eae209
PM
387#else
388 if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
389 return -EACCES;
390 if (!capable(CAP_SYS_ADMIN))
391 return -EPERM;
392 return seq_open(filp, &ima_policy_seqops);
393#endif
394 }
0716abbb
DK
395 if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
396 return -EBUSY;
397 return 0;
f4bd857b
MZ
398}
399
4af4662f
MZ
400/*
401 * ima_release_policy - start using the new measure policy rules.
402 *
403 * Initially, ima_measure points to the default policy rules, now
f4bd857b
MZ
404 * point to the new policy rules, and remove the securityfs policy file,
405 * assuming a valid policy.
4af4662f
MZ
406 */
407static int ima_release_policy(struct inode *inode, struct file *file)
408{
0716abbb
DK
409 const char *cause = valid_policy ? "completed" : "failed";
410
80eae209 411 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
9a11a189 412 return seq_release(inode, file);
80eae209 413
0112721d
SL
414 if (valid_policy && ima_check_policy() < 0) {
415 cause = "failed";
416 valid_policy = 0;
417 }
418
de636769 419 pr_info("policy update %s\n", cause);
0716abbb
DK
420 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
421 "policy_update", cause, !valid_policy, 0);
422
4af4662f
MZ
423 if (!valid_policy) {
424 ima_delete_rules();
f4bd857b 425 valid_policy = 1;
0716abbb 426 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
4af4662f
MZ
427 return 0;
428 }
80eae209 429
4af4662f 430 ima_update_policy();
2068626d 431#if !defined(CONFIG_IMA_WRITE_POLICY) && !defined(CONFIG_IMA_READ_POLICY)
4af4662f
MZ
432 securityfs_remove(ima_policy);
433 ima_policy = NULL;
2068626d 434#elif defined(CONFIG_IMA_WRITE_POLICY)
38d859f9 435 clear_bit(IMA_FS_BUSY, &ima_fs_flags);
ffb122de
PV
436#elif defined(CONFIG_IMA_READ_POLICY)
437 inode->i_mode &= ~S_IWUSR;
38d859f9 438#endif
4af4662f
MZ
439 return 0;
440}
441
828c0950 442static const struct file_operations ima_measure_policy_ops = {
f4bd857b 443 .open = ima_open_policy,
4af4662f 444 .write = ima_write_policy,
80eae209 445 .read = seq_read,
cdcd90f9
AB
446 .release = ima_release_policy,
447 .llseek = generic_file_llseek,
4af4662f 448};
bab73937 449
932995f0 450int __init ima_fs_init(void)
bab73937 451{
0c343af8 452 ima_dir = securityfs_create_dir("ima", integrity_dir);
bab73937
MZ
453 if (IS_ERR(ima_dir))
454 return -1;
455
0c343af8
MG
456 ima_symlink = securityfs_create_symlink("ima", NULL, "integrity/ima",
457 NULL);
458 if (IS_ERR(ima_symlink))
459 goto out;
460
bab73937
MZ
461 binary_runtime_measurements =
462 securityfs_create_file("binary_runtime_measurements",
463 S_IRUSR | S_IRGRP, ima_dir, NULL,
464 &ima_measurements_ops);
465 if (IS_ERR(binary_runtime_measurements))
466 goto out;
467
468 ascii_runtime_measurements =
469 securityfs_create_file("ascii_runtime_measurements",
470 S_IRUSR | S_IRGRP, ima_dir, NULL,
471 &ima_ascii_measurements_ops);
472 if (IS_ERR(ascii_runtime_measurements))
473 goto out;
474
475 runtime_measurements_count =
476 securityfs_create_file("runtime_measurements_count",
477 S_IRUSR | S_IRGRP, ima_dir, NULL,
478 &ima_measurements_count_ops);
479 if (IS_ERR(runtime_measurements_count))
480 goto out;
481
482 violations =
483 securityfs_create_file("violations", S_IRUSR | S_IRGRP,
484 ima_dir, NULL, &ima_htable_violations_ops);
485 if (IS_ERR(violations))
486 goto out;
487
80eae209 488 ima_policy = securityfs_create_file("policy", POLICY_FILE_FLAGS,
4af4662f
MZ
489 ima_dir, NULL,
490 &ima_measure_policy_ops);
491 if (IS_ERR(ima_policy))
492 goto out;
bab73937 493
4af4662f 494 return 0;
bab73937 495out:
0ea4f8ae 496 securityfs_remove(violations);
bab73937
MZ
497 securityfs_remove(runtime_measurements_count);
498 securityfs_remove(ascii_runtime_measurements);
499 securityfs_remove(binary_runtime_measurements);
0c343af8 500 securityfs_remove(ima_symlink);
bab73937 501 securityfs_remove(ima_dir);
4af4662f 502 securityfs_remove(ima_policy);
bab73937
MZ
503 return -1;
504}