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