Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 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 | * |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 10 | * File: ima_fs.c |
| 11 | * implemenents security file system for reporting |
| 12 | * current measurement list and IMA statistics |
| 13 | */ |
Petr Vorel | de63676 | 2018-04-24 16:30:38 +0200 | [diff] [blame] | 14 | |
Eric Paris | f850a7c | 2009-05-12 15:13:55 -0400 | [diff] [blame] | 15 | #include <linux/fcntl.h> |
Scott Branden | b89999d0 | 2020-10-02 10:38:15 -0700 | [diff] [blame] | 16 | #include <linux/kernel_read_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Paul Gortmaker | 876979c | 2018-12-09 15:36:29 -0500 | [diff] [blame] | 18 | #include <linux/init.h> |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 19 | #include <linux/seq_file.h> |
| 20 | #include <linux/rculist.h> |
| 21 | #include <linux/rcupdate.h> |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 22 | #include <linux/parser.h> |
Dmitry Kasatkin | 7429b092 | 2014-04-11 17:47:01 +0300 | [diff] [blame] | 23 | #include <linux/vmalloc.h> |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 24 | |
| 25 | #include "ima.h" |
| 26 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 27 | static DEFINE_MUTEX(ima_write_mutex); |
| 28 | |
Mimi Zohar | d68a6fe | 2016-12-19 16:22:57 -0800 | [diff] [blame] | 29 | bool ima_canonical_fmt; |
| 30 | static int __init default_canonical_fmt_setup(char *str) |
| 31 | { |
| 32 | #ifdef __BIG_ENDIAN |
Thomas Meyer | 39adb92 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 33 | ima_canonical_fmt = true; |
Mimi Zohar | d68a6fe | 2016-12-19 16:22:57 -0800 | [diff] [blame] | 34 | #endif |
| 35 | return 1; |
| 36 | } |
| 37 | __setup("ima_canonical_fmt", default_canonical_fmt_setup); |
| 38 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 39 | static int valid_policy = 1; |
Eric Biggers | 1e4c8da | 2018-09-07 14:33:24 -0700 | [diff] [blame] | 40 | |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 41 | static ssize_t ima_show_htable_value(char __user *buf, size_t count, |
| 42 | loff_t *ppos, atomic_long_t *val) |
| 43 | { |
Eric Biggers | 1e4c8da | 2018-09-07 14:33:24 -0700 | [diff] [blame] | 44 | char tmpbuf[32]; /* greater than largest 'long' string value */ |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 45 | ssize_t len; |
| 46 | |
Eric Biggers | 1e4c8da | 2018-09-07 14:33:24 -0700 | [diff] [blame] | 47 | len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", atomic_long_read(val)); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 48 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, len); |
| 49 | } |
| 50 | |
| 51 | static 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 Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 58 | static const struct file_operations ima_htable_violations_ops = { |
Arnd Bergmann | cdcd90f | 2010-07-07 23:40:15 +0200 | [diff] [blame] | 59 | .read = ima_show_htable_violations, |
| 60 | .llseek = generic_file_llseek, |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static 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 Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 71 | static const struct file_operations ima_measurements_count_ops = { |
Arnd Bergmann | cdcd90f | 2010-07-07 23:40:15 +0200 | [diff] [blame] | 72 | .read = ima_show_measurements_count, |
| 73 | .llseek = generic_file_llseek, |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* returns pointer to hlist_node */ |
| 77 | static 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 | |
| 94 | static 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 Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 102 | qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 103 | rcu_read_unlock(); |
| 104 | (*pos)++; |
| 105 | |
| 106 | return (&qe->later == &ima_measurements) ? NULL : qe; |
| 107 | } |
| 108 | |
| 109 | static void ima_measurements_stop(struct seq_file *m, void *v) |
| 110 | { |
| 111 | } |
| 112 | |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 113 | void ima_putc(struct seq_file *m, void *data, int datalen) |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 114 | { |
| 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 Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 124 | * [eventdata length] |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 125 | * eventdata[n]=template specific data |
| 126 | */ |
Mimi Zohar | 7b8589c | 2016-12-19 16:22:48 -0800 | [diff] [blame] | 127 | int ima_measurements_show(struct seq_file *m, void *v) |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 128 | { |
| 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 Sassu | 7dbdb42 | 2014-10-13 14:08:39 +0200 | [diff] [blame] | 132 | char *template_name; |
Mimi Zohar | d68a6fe | 2016-12-19 16:22:57 -0800 | [diff] [blame] | 133 | u32 pcr, namelen, template_data_len; /* temporary fields */ |
Roberto Sassu | 3e8e550 | 2013-11-08 19:21:40 +0100 | [diff] [blame] | 134 | bool is_ima_template = false; |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 135 | int i; |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 136 | |
| 137 | /* get entry */ |
| 138 | e = qe->entry; |
| 139 | if (e == NULL) |
| 140 | return -1; |
| 141 | |
Roberto Sassu | 7dbdb42 | 2014-10-13 14:08:39 +0200 | [diff] [blame] | 142 | template_name = (e->template_desc->name[0] != '\0') ? |
| 143 | e->template_desc->name : e->template_desc->fmt; |
| 144 | |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 145 | /* |
| 146 | * 1st: PCRIndex |
Eric Richter | 5f6f027 | 2016-06-01 13:14:04 -0500 | [diff] [blame] | 147 | * PCR used defaults to the same (config option) in |
| 148 | * little-endian format, unless set in policy |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 149 | */ |
Roberto Sassu | 6b26285 | 2021-06-08 14:31:22 +0200 | [diff] [blame] | 150 | pcr = !ima_canonical_fmt ? e->pcr : (__force u32)cpu_to_le32(e->pcr); |
Mimi Zohar | d68a6fe | 2016-12-19 16:22:57 -0800 | [diff] [blame] | 151 | ima_putc(m, &pcr, sizeof(e->pcr)); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 152 | |
| 153 | /* 2nd: template digest */ |
Roberto Sassu | aa724fe | 2020-03-25 11:47:09 +0100 | [diff] [blame] | 154 | ima_putc(m, e->digests[ima_sha1_idx].digest, TPM_DIGEST_SIZE); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 155 | |
| 156 | /* 3rd: template name size */ |
Mimi Zohar | d68a6fe | 2016-12-19 16:22:57 -0800 | [diff] [blame] | 157 | namelen = !ima_canonical_fmt ? strlen(template_name) : |
Roberto Sassu | 6b26285 | 2021-06-08 14:31:22 +0200 | [diff] [blame] | 158 | (__force u32)cpu_to_le32(strlen(template_name)); |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 159 | ima_putc(m, &namelen, sizeof(namelen)); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 160 | |
| 161 | /* 4th: template name */ |
Mimi Zohar | d68a6fe | 2016-12-19 16:22:57 -0800 | [diff] [blame] | 162 | ima_putc(m, template_name, strlen(template_name)); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 163 | |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 164 | /* 5th: template length (except for 'ima' template) */ |
Roberto Sassu | 7dbdb42 | 2014-10-13 14:08:39 +0200 | [diff] [blame] | 165 | if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0) |
Roberto Sassu | 3e8e550 | 2013-11-08 19:21:40 +0100 | [diff] [blame] | 166 | is_ima_template = true; |
| 167 | |
Mimi Zohar | d68a6fe | 2016-12-19 16:22:57 -0800 | [diff] [blame] | 168 | if (!is_ima_template) { |
| 169 | template_data_len = !ima_canonical_fmt ? e->template_data_len : |
Roberto Sassu | 6b26285 | 2021-06-08 14:31:22 +0200 | [diff] [blame] | 170 | (__force u32)cpu_to_le32(e->template_data_len); |
Mimi Zohar | d68a6fe | 2016-12-19 16:22:57 -0800 | [diff] [blame] | 171 | ima_putc(m, &template_data_len, sizeof(e->template_data_len)); |
| 172 | } |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 173 | |
| 174 | /* 6th: template specific data */ |
| 175 | for (i = 0; i < e->template_desc->num_fields; i++) { |
Roberto Sassu | 3e8e550 | 2013-11-08 19:21:40 +0100 | [diff] [blame] | 176 | enum ima_show_type show = IMA_SHOW_BINARY; |
Eric Biggers | b2724d5 | 2018-09-07 13:22:23 -0700 | [diff] [blame] | 177 | const struct ima_template_field *field = |
| 178 | e->template_desc->fields[i]; |
Roberto Sassu | 3e8e550 | 2013-11-08 19:21:40 +0100 | [diff] [blame] | 179 | |
| 180 | if (is_ima_template && strcmp(field->field_id, "d") == 0) |
| 181 | show = IMA_SHOW_BINARY_NO_FIELD_LEN; |
Roberto Sassu | c019e30 | 2014-02-03 13:56:04 +0100 | [diff] [blame] | 182 | if (is_ima_template && strcmp(field->field_id, "n") == 0) |
| 183 | show = IMA_SHOW_BINARY_OLD_STRING_FMT; |
Roberto Sassu | 3e8e550 | 2013-11-08 19:21:40 +0100 | [diff] [blame] | 184 | field->field_show(m, show, &e->template_data[i]); |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 185 | } |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 189 | static const struct seq_operations ima_measurments_seqops = { |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 190 | .start = ima_measurements_start, |
| 191 | .next = ima_measurements_next, |
| 192 | .stop = ima_measurements_stop, |
| 193 | .show = ima_measurements_show |
| 194 | }; |
| 195 | |
| 196 | static int ima_measurements_open(struct inode *inode, struct file *file) |
| 197 | { |
| 198 | return seq_open(file, &ima_measurments_seqops); |
| 199 | } |
| 200 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 201 | static const struct file_operations ima_measurements_ops = { |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 202 | .open = ima_measurements_open, |
| 203 | .read = seq_read, |
| 204 | .llseek = seq_lseek, |
| 205 | .release = seq_release, |
| 206 | }; |
| 207 | |
Mimi Zohar | 45b2613 | 2015-06-11 11:54:42 -0400 | [diff] [blame] | 208 | void ima_print_digest(struct seq_file *m, u8 *digest, u32 size) |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 209 | { |
Mimi Zohar | 45b2613 | 2015-06-11 11:54:42 -0400 | [diff] [blame] | 210 | u32 i; |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 211 | |
Mimi Zohar | 140d802 | 2013-03-11 20:29:47 -0400 | [diff] [blame] | 212 | for (i = 0; i < size; i++) |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 213 | seq_printf(m, "%02x", *(digest + i)); |
| 214 | } |
| 215 | |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 216 | /* print in ascii */ |
| 217 | static 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 Sassu | 7dbdb42 | 2014-10-13 14:08:39 +0200 | [diff] [blame] | 222 | char *template_name; |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 223 | int i; |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 224 | |
| 225 | /* get entry */ |
| 226 | e = qe->entry; |
| 227 | if (e == NULL) |
| 228 | return -1; |
| 229 | |
Roberto Sassu | 7dbdb42 | 2014-10-13 14:08:39 +0200 | [diff] [blame] | 230 | template_name = (e->template_desc->name[0] != '\0') ? |
| 231 | e->template_desc->name : e->template_desc->fmt; |
| 232 | |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 233 | /* 1st: PCR used (config option) */ |
Eric Richter | 5f6f027 | 2016-06-01 13:14:04 -0500 | [diff] [blame] | 234 | seq_printf(m, "%2d ", e->pcr); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 235 | |
| 236 | /* 2nd: SHA1 template hash */ |
Roberto Sassu | aa724fe | 2020-03-25 11:47:09 +0100 | [diff] [blame] | 237 | ima_print_digest(m, e->digests[ima_sha1_idx].digest, TPM_DIGEST_SIZE); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 238 | |
| 239 | /* 3th: template name */ |
Roberto Sassu | 7dbdb42 | 2014-10-13 14:08:39 +0200 | [diff] [blame] | 240 | seq_printf(m, " %s", template_name); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 241 | |
| 242 | /* 4th: template specific data */ |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 243 | 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 Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 255 | static const struct seq_operations ima_ascii_measurements_seqops = { |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 256 | .start = ima_measurements_start, |
| 257 | .next = ima_measurements_next, |
| 258 | .stop = ima_measurements_stop, |
| 259 | .show = ima_ascii_measurements_show |
| 260 | }; |
| 261 | |
| 262 | static int ima_ascii_measurements_open(struct inode *inode, struct file *file) |
| 263 | { |
| 264 | return seq_open(file, &ima_ascii_measurements_seqops); |
| 265 | } |
| 266 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 267 | static const struct file_operations ima_ascii_measurements_ops = { |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 268 | .open = ima_ascii_measurements_open, |
| 269 | .read = seq_read, |
| 270 | .llseek = seq_lseek, |
| 271 | .release = seq_release, |
| 272 | }; |
| 273 | |
Dmitry Kasatkin | 7429b092 | 2014-04-11 17:47:01 +0300 | [diff] [blame] | 274 | static ssize_t ima_read_policy(char *path) |
| 275 | { |
Kees Cook | c307459 | 2020-10-02 10:38:13 -0700 | [diff] [blame] | 276 | void *data = NULL; |
Dmitry Kasatkin | 7429b092 | 2014-04-11 17:47:01 +0300 | [diff] [blame] | 277 | char *datap; |
Kees Cook | f7a4f68 | 2020-10-02 10:38:17 -0700 | [diff] [blame] | 278 | size_t size; |
Dmitry Kasatkin | 7429b092 | 2014-04-11 17:47:01 +0300 | [diff] [blame] | 279 | int rc, pathlen = strlen(path); |
| 280 | |
| 281 | char *p; |
| 282 | |
| 283 | /* remove \n */ |
| 284 | datap = path; |
| 285 | strsep(&datap, "\n"); |
| 286 | |
Kees Cook | 0fa8e08 | 2020-10-02 10:38:25 -0700 | [diff] [blame] | 287 | rc = kernel_read_file_from_path(path, 0, &data, INT_MAX, NULL, |
| 288 | READING_POLICY); |
Dmitry Kasatkin | 7429b092 | 2014-04-11 17:47:01 +0300 | [diff] [blame] | 289 | if (rc < 0) { |
| 290 | pr_err("Unable to open file: %s (%d)", path, rc); |
| 291 | return rc; |
| 292 | } |
Kees Cook | f7a4f68 | 2020-10-02 10:38:17 -0700 | [diff] [blame] | 293 | size = rc; |
| 294 | rc = 0; |
Dmitry Kasatkin | 7429b092 | 2014-04-11 17:47:01 +0300 | [diff] [blame] | 295 | |
| 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 Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 314 | static ssize_t ima_write_policy(struct file *file, const char __user *buf, |
| 315 | size_t datalen, loff_t *ppos) |
| 316 | { |
Petko Manolov | 6427e6c | 2016-01-03 17:36:38 +0200 | [diff] [blame] | 317 | char *data; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 318 | ssize_t result; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 319 | |
| 320 | if (datalen >= PAGE_SIZE) |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 321 | datalen = PAGE_SIZE - 1; |
| 322 | |
| 323 | /* No partial writes. */ |
| 324 | result = -EINVAL; |
| 325 | if (*ppos != 0) |
| 326 | goto out; |
| 327 | |
Geliang Tang | b4e2803 | 2017-05-06 23:40:18 +0800 | [diff] [blame] | 328 | data = memdup_user_nul(buf, datalen); |
| 329 | if (IS_ERR(data)) { |
| 330 | result = PTR_ERR(data); |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 331 | goto out; |
Geliang Tang | b4e2803 | 2017-05-06 23:40:18 +0800 | [diff] [blame] | 332 | } |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 333 | |
Petko Manolov | 6427e6c | 2016-01-03 17:36:38 +0200 | [diff] [blame] | 334 | result = mutex_lock_interruptible(&ima_write_mutex); |
| 335 | if (result < 0) |
| 336 | goto out_free; |
Petko Manolov | 6427e6c | 2016-01-03 17:36:38 +0200 | [diff] [blame] | 337 | |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 338 | if (data[0] == '/') { |
Dmitry Kasatkin | 7429b092 | 2014-04-11 17:47:01 +0300 | [diff] [blame] | 339 | result = ima_read_policy(data); |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 340 | } else if (ima_appraise & IMA_APPRAISE_POLICY) { |
Petr Vorel | de63676 | 2018-04-24 16:30:38 +0200 | [diff] [blame] | 341 | pr_err("signed policy file (specified as an absolute pathname) required\n"); |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 342 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL, |
| 343 | "policy_update", "signed policy required", |
| 344 | 1, 0); |
Roberto Sassu | 2e3a34e | 2020-04-27 12:31:28 +0200 | [diff] [blame] | 345 | result = -EACCES; |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 346 | } else { |
Dmitry Kasatkin | 7429b092 | 2014-04-11 17:47:01 +0300 | [diff] [blame] | 347 | result = ima_parse_add_rule(data); |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 348 | } |
Dmitry Kasatkin | 7429b092 | 2014-04-11 17:47:01 +0300 | [diff] [blame] | 349 | mutex_unlock(&ima_write_mutex); |
Petko Manolov | 6427e6c | 2016-01-03 17:36:38 +0200 | [diff] [blame] | 350 | out_free: |
| 351 | kfree(data); |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 352 | out: |
| 353 | if (result < 0) |
| 354 | valid_policy = 0; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 355 | |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 356 | return result; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 357 | } |
| 358 | |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 359 | static struct dentry *ima_dir; |
Matthew Garrett | 0c343af | 2018-05-11 16:12:34 -0700 | [diff] [blame] | 360 | static struct dentry *ima_symlink; |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 361 | static struct dentry *binary_runtime_measurements; |
| 362 | static struct dentry *ascii_runtime_measurements; |
| 363 | static struct dentry *runtime_measurements_count; |
| 364 | static struct dentry *violations; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 365 | static struct dentry *ima_policy; |
| 366 | |
Dmitry Kasatkin | 0716abb | 2014-10-03 14:40:21 +0300 | [diff] [blame] | 367 | enum ima_fs_flags { |
| 368 | IMA_FS_BUSY, |
| 369 | }; |
| 370 | |
| 371 | static unsigned long ima_fs_flags; |
| 372 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 373 | #ifdef CONFIG_IMA_READ_POLICY |
| 374 | static 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 Zohar | f4bd857 | 2009-02-04 09:07:01 -0500 | [diff] [blame] | 382 | /* |
| 383 | * ima_open_policy: sequentialize access to the policy file |
| 384 | */ |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 385 | static int ima_open_policy(struct inode *inode, struct file *filp) |
Mimi Zohar | f4bd857 | 2009-02-04 09:07:01 -0500 | [diff] [blame] | 386 | { |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 387 | if (!(filp->f_flags & O_WRONLY)) { |
| 388 | #ifndef CONFIG_IMA_READ_POLICY |
Eric Paris | f850a7c | 2009-05-12 15:13:55 -0400 | [diff] [blame] | 389 | return -EACCES; |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 390 | #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 Kasatkin | 0716abb | 2014-10-03 14:40:21 +0300 | [diff] [blame] | 398 | if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags)) |
| 399 | return -EBUSY; |
| 400 | return 0; |
Mimi Zohar | f4bd857 | 2009-02-04 09:07:01 -0500 | [diff] [blame] | 401 | } |
| 402 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 403 | /* |
| 404 | * ima_release_policy - start using the new measure policy rules. |
| 405 | * |
| 406 | * Initially, ima_measure points to the default policy rules, now |
Mimi Zohar | f4bd857 | 2009-02-04 09:07:01 -0500 | [diff] [blame] | 407 | * point to the new policy rules, and remove the securityfs policy file, |
| 408 | * assuming a valid policy. |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 409 | */ |
| 410 | static int ima_release_policy(struct inode *inode, struct file *file) |
| 411 | { |
Dmitry Kasatkin | 0716abb | 2014-10-03 14:40:21 +0300 | [diff] [blame] | 412 | const char *cause = valid_policy ? "completed" : "failed"; |
| 413 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 414 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) |
Eric Richter | 9a11a18 | 2016-10-13 17:47:36 -0500 | [diff] [blame] | 415 | return seq_release(inode, file); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 416 | |
Sasha Levin | 0112721 | 2015-12-22 08:51:23 -0500 | [diff] [blame] | 417 | if (valid_policy && ima_check_policy() < 0) { |
| 418 | cause = "failed"; |
| 419 | valid_policy = 0; |
| 420 | } |
| 421 | |
Petr Vorel | de63676 | 2018-04-24 16:30:38 +0200 | [diff] [blame] | 422 | pr_info("policy update %s\n", cause); |
Dmitry Kasatkin | 0716abb | 2014-10-03 14:40:21 +0300 | [diff] [blame] | 423 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL, |
| 424 | "policy_update", cause, !valid_policy, 0); |
| 425 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 426 | if (!valid_policy) { |
| 427 | ima_delete_rules(); |
Mimi Zohar | f4bd857 | 2009-02-04 09:07:01 -0500 | [diff] [blame] | 428 | valid_policy = 1; |
Dmitry Kasatkin | 0716abb | 2014-10-03 14:40:21 +0300 | [diff] [blame] | 429 | clear_bit(IMA_FS_BUSY, &ima_fs_flags); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 430 | return 0; |
| 431 | } |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 432 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 433 | ima_update_policy(); |
Mimi Zohar | 2068626 | 2017-06-27 16:10:39 -0400 | [diff] [blame] | 434 | #if !defined(CONFIG_IMA_WRITE_POLICY) && !defined(CONFIG_IMA_READ_POLICY) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 435 | securityfs_remove(ima_policy); |
| 436 | ima_policy = NULL; |
Mimi Zohar | 2068626 | 2017-06-27 16:10:39 -0400 | [diff] [blame] | 437 | #elif defined(CONFIG_IMA_WRITE_POLICY) |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 438 | clear_bit(IMA_FS_BUSY, &ima_fs_flags); |
Petr Vorel | ffb122d | 2018-04-20 15:28:57 +0200 | [diff] [blame] | 439 | #elif defined(CONFIG_IMA_READ_POLICY) |
| 440 | inode->i_mode &= ~S_IWUSR; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 441 | #endif |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 445 | static const struct file_operations ima_measure_policy_ops = { |
Mimi Zohar | f4bd857 | 2009-02-04 09:07:01 -0500 | [diff] [blame] | 446 | .open = ima_open_policy, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 447 | .write = ima_write_policy, |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 448 | .read = seq_read, |
Arnd Bergmann | cdcd90f | 2010-07-07 23:40:15 +0200 | [diff] [blame] | 449 | .release = ima_release_policy, |
| 450 | .llseek = generic_file_llseek, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 451 | }; |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 452 | |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 453 | int __init ima_fs_init(void) |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 454 | { |
Matthew Garrett | 0c343af | 2018-05-11 16:12:34 -0700 | [diff] [blame] | 455 | ima_dir = securityfs_create_dir("ima", integrity_dir); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 456 | if (IS_ERR(ima_dir)) |
| 457 | return -1; |
| 458 | |
Matthew Garrett | 0c343af | 2018-05-11 16:12:34 -0700 | [diff] [blame] | 459 | ima_symlink = securityfs_create_symlink("ima", NULL, "integrity/ima", |
| 460 | NULL); |
| 461 | if (IS_ERR(ima_symlink)) |
| 462 | goto out; |
| 463 | |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 464 | 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 Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 491 | ima_policy = securityfs_create_file("policy", POLICY_FILE_FLAGS, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 492 | ima_dir, NULL, |
| 493 | &ima_measure_policy_ops); |
| 494 | if (IS_ERR(ima_policy)) |
| 495 | goto out; |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 496 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 497 | return 0; |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 498 | out: |
Stefan Berger | f7333b9 | 2022-01-25 17:46:23 -0500 | [diff] [blame] | 499 | securityfs_remove(ima_policy); |
Dmitry Kasatkin | 0ea4f8a | 2012-01-29 19:19:08 -0500 | [diff] [blame] | 500 | securityfs_remove(violations); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 501 | securityfs_remove(runtime_measurements_count); |
| 502 | securityfs_remove(ascii_runtime_measurements); |
| 503 | securityfs_remove(binary_runtime_measurements); |
Matthew Garrett | 0c343af | 2018-05-11 16:12:34 -0700 | [diff] [blame] | 504 | securityfs_remove(ima_symlink); |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 505 | securityfs_remove(ima_dir); |
| 506 | return -1; |
| 507 | } |