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