Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Politecnico di Torino, Italy |
Alexander A. Klimov | c9fecf5 | 2020-07-05 23:45:12 +0200 | [diff] [blame] | 4 | * TORSEC group -- https://security.polito.it |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 5 | * |
| 6 | * Author: Roberto Sassu <roberto.sassu@polito.it> |
| 7 | * |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 8 | * File: ima_template_lib.c |
| 9 | * Library of supported template fields. |
| 10 | */ |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 11 | |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 12 | #include "ima_template_lib.h" |
| 13 | |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 14 | static bool ima_template_hash_algo_allowed(u8 algo) |
| 15 | { |
| 16 | if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5) |
| 17 | return true; |
| 18 | |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | enum data_formats { |
| 23 | DATA_FMT_DIGEST = 0, |
| 24 | DATA_FMT_DIGEST_WITH_ALGO, |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 25 | DATA_FMT_STRING, |
| 26 | DATA_FMT_HEX |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 29 | static int ima_write_template_field_data(const void *data, const u32 datalen, |
| 30 | enum data_formats datafmt, |
| 31 | struct ima_field_data *field_data) |
| 32 | { |
| 33 | u8 *buf, *buf_ptr; |
Roberto Sassu | e3b64c2 | 2014-02-03 13:56:05 +0100 | [diff] [blame] | 34 | u32 buflen = datalen; |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 35 | |
Roberto Sassu | e3b64c2 | 2014-02-03 13:56:05 +0100 | [diff] [blame] | 36 | if (datafmt == DATA_FMT_STRING) |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 37 | buflen = datalen + 1; |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 38 | |
| 39 | buf = kzalloc(buflen, GFP_KERNEL); |
| 40 | if (!buf) |
| 41 | return -ENOMEM; |
| 42 | |
| 43 | memcpy(buf, data, datalen); |
| 44 | |
| 45 | /* |
| 46 | * Replace all space characters with underscore for event names and |
| 47 | * strings. This avoid that, during the parsing of a measurements list, |
| 48 | * filenames with spaces or that end with the suffix ' (deleted)' are |
| 49 | * split into multiple template fields (the space is the delimitator |
| 50 | * character for measurements lists in ASCII format). |
| 51 | */ |
Roberto Sassu | e3b64c2 | 2014-02-03 13:56:05 +0100 | [diff] [blame] | 52 | if (datafmt == DATA_FMT_STRING) { |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 53 | for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++) |
| 54 | if (*buf_ptr == ' ') |
| 55 | *buf_ptr = '_'; |
| 56 | } |
| 57 | |
| 58 | field_data->data = buf; |
| 59 | field_data->len = buflen; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static void ima_show_template_data_ascii(struct seq_file *m, |
| 64 | enum ima_show_type show, |
| 65 | enum data_formats datafmt, |
| 66 | struct ima_field_data *field_data) |
| 67 | { |
Mimi Zohar | 45b2613 | 2015-06-11 11:54:42 -0400 | [diff] [blame] | 68 | u8 *buf_ptr = field_data->data; |
| 69 | u32 buflen = field_data->len; |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 70 | |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 71 | switch (datafmt) { |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 72 | case DATA_FMT_DIGEST_WITH_ALGO: |
| 73 | buf_ptr = strnchr(field_data->data, buflen, ':'); |
| 74 | if (buf_ptr != field_data->data) |
| 75 | seq_printf(m, "%s", field_data->data); |
| 76 | |
| 77 | /* skip ':' and '\0' */ |
| 78 | buf_ptr += 2; |
| 79 | buflen -= buf_ptr - field_data->data; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 80 | fallthrough; |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 81 | case DATA_FMT_DIGEST: |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 82 | case DATA_FMT_HEX: |
| 83 | if (!buflen) |
| 84 | break; |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 85 | ima_print_digest(m, buf_ptr, buflen); |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 86 | break; |
| 87 | case DATA_FMT_STRING: |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 88 | seq_printf(m, "%s", buf_ptr); |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 89 | break; |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static void ima_show_template_data_binary(struct seq_file *m, |
| 96 | enum ima_show_type show, |
| 97 | enum data_formats datafmt, |
| 98 | struct ima_field_data *field_data) |
| 99 | { |
Roberto Sassu | c019e30 | 2014-02-03 13:56:04 +0100 | [diff] [blame] | 100 | u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ? |
| 101 | strlen(field_data->data) : field_data->len; |
Roberto Sassu | 3e8e550 | 2013-11-08 19:21:40 +0100 | [diff] [blame] | 102 | |
Mimi Zohar | d68a6fe | 2016-12-19 16:22:57 -0800 | [diff] [blame] | 103 | if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) { |
| 104 | u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len); |
| 105 | |
| 106 | ima_putc(m, &field_len, sizeof(field_len)); |
| 107 | } |
Roberto Sassu | c019e30 | 2014-02-03 13:56:04 +0100 | [diff] [blame] | 108 | |
| 109 | if (!len) |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 110 | return; |
Roberto Sassu | 3e8e550 | 2013-11-08 19:21:40 +0100 | [diff] [blame] | 111 | |
Roberto Sassu | c019e30 | 2014-02-03 13:56:04 +0100 | [diff] [blame] | 112 | ima_putc(m, field_data->data, len); |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void ima_show_template_field_data(struct seq_file *m, |
| 116 | enum ima_show_type show, |
| 117 | enum data_formats datafmt, |
| 118 | struct ima_field_data *field_data) |
| 119 | { |
| 120 | switch (show) { |
| 121 | case IMA_SHOW_ASCII: |
| 122 | ima_show_template_data_ascii(m, show, datafmt, field_data); |
| 123 | break; |
| 124 | case IMA_SHOW_BINARY: |
Roberto Sassu | 3e8e550 | 2013-11-08 19:21:40 +0100 | [diff] [blame] | 125 | case IMA_SHOW_BINARY_NO_FIELD_LEN: |
Roberto Sassu | c019e30 | 2014-02-03 13:56:04 +0100 | [diff] [blame] | 126 | case IMA_SHOW_BINARY_OLD_STRING_FMT: |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 127 | ima_show_template_data_binary(m, show, datafmt, field_data); |
| 128 | break; |
| 129 | default: |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void ima_show_template_digest(struct seq_file *m, enum ima_show_type show, |
| 135 | struct ima_field_data *field_data) |
| 136 | { |
| 137 | ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data); |
| 138 | } |
| 139 | |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 140 | void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show, |
| 141 | struct ima_field_data *field_data) |
| 142 | { |
| 143 | ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO, |
| 144 | field_data); |
| 145 | } |
| 146 | |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 147 | void ima_show_template_string(struct seq_file *m, enum ima_show_type show, |
| 148 | struct ima_field_data *field_data) |
| 149 | { |
| 150 | ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data); |
| 151 | } |
| 152 | |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 153 | void ima_show_template_sig(struct seq_file *m, enum ima_show_type show, |
| 154 | struct ima_field_data *field_data) |
| 155 | { |
| 156 | ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data); |
| 157 | } |
| 158 | |
Prakhar Srivastava | 86b4da8 | 2019-06-23 23:23:30 -0700 | [diff] [blame] | 159 | void ima_show_template_buf(struct seq_file *m, enum ima_show_type show, |
| 160 | struct ima_field_data *field_data) |
| 161 | { |
| 162 | ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data); |
| 163 | } |
| 164 | |
Roberto Sassu | b17fd9e | 2017-05-16 14:53:41 +0200 | [diff] [blame] | 165 | /** |
| 166 | * ima_parse_buf() - Parses lengths and data from an input buffer |
| 167 | * @bufstartp: Buffer start address. |
| 168 | * @bufendp: Buffer end address. |
| 169 | * @bufcurp: Pointer to remaining (non-parsed) data. |
| 170 | * @maxfields: Length of fields array. |
| 171 | * @fields: Array containing lengths and pointers of parsed data. |
| 172 | * @curfields: Number of array items containing parsed data. |
| 173 | * @len_mask: Bitmap (if bit is set, data length should not be parsed). |
| 174 | * @enforce_mask: Check if curfields == maxfields and/or bufcurp == bufendp. |
| 175 | * @bufname: String identifier of the input buffer. |
| 176 | * |
| 177 | * Return: 0 on success, -EINVAL on error. |
| 178 | */ |
| 179 | int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp, |
| 180 | int maxfields, struct ima_field_data *fields, int *curfields, |
| 181 | unsigned long *len_mask, int enforce_mask, char *bufname) |
| 182 | { |
| 183 | void *bufp = bufstartp; |
| 184 | int i; |
| 185 | |
| 186 | for (i = 0; i < maxfields; i++) { |
| 187 | if (len_mask == NULL || !test_bit(i, len_mask)) { |
| 188 | if (bufp > (bufendp - sizeof(u32))) |
| 189 | break; |
| 190 | |
| 191 | fields[i].len = *(u32 *)bufp; |
| 192 | if (ima_canonical_fmt) |
| 193 | fields[i].len = le32_to_cpu(fields[i].len); |
| 194 | |
| 195 | bufp += sizeof(u32); |
| 196 | } |
| 197 | |
| 198 | if (bufp > (bufendp - fields[i].len)) |
| 199 | break; |
| 200 | |
| 201 | fields[i].data = bufp; |
| 202 | bufp += fields[i].len; |
| 203 | } |
| 204 | |
| 205 | if ((enforce_mask & ENFORCE_FIELDS) && i != maxfields) { |
| 206 | pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n", |
| 207 | bufname, maxfields, i); |
| 208 | return -EINVAL; |
| 209 | } |
| 210 | |
| 211 | if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) { |
| 212 | pr_err("%s: buf end mismatch: expected: %p, current: %p\n", |
| 213 | bufname, bufendp, bufp); |
| 214 | return -EINVAL; |
| 215 | } |
| 216 | |
| 217 | if (curfields) |
| 218 | *curfields = i; |
| 219 | |
| 220 | if (bufcurp) |
| 221 | *bufcurp = bufp; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Thiago Jung Bauermann | 3878d50 | 2019-06-27 23:19:32 -0300 | [diff] [blame] | 226 | static int ima_eventdigest_init_common(const u8 *digest, u32 digestsize, |
| 227 | u8 hash_algo, |
Roberto Sassu | dcf4e39 | 2013-11-08 19:21:37 +0100 | [diff] [blame] | 228 | struct ima_field_data *field_data) |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 229 | { |
| 230 | /* |
| 231 | * digest formats: |
| 232 | * - DATA_FMT_DIGEST: digest |
| 233 | * - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest, |
| 234 | * where <hash algo> is provided if the hash algoritm is not |
| 235 | * SHA1 or MD5 |
| 236 | */ |
| 237 | u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 }; |
| 238 | enum data_formats fmt = DATA_FMT_DIGEST; |
| 239 | u32 offset = 0; |
| 240 | |
Roberto Sassu | dcf4e39 | 2013-11-08 19:21:37 +0100 | [diff] [blame] | 241 | if (hash_algo < HASH_ALGO__LAST) { |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 242 | fmt = DATA_FMT_DIGEST_WITH_ALGO; |
Roberto Sassu | dcf4e39 | 2013-11-08 19:21:37 +0100 | [diff] [blame] | 243 | offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s", |
| 244 | hash_algo_name[hash_algo]); |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 245 | buffer[offset] = ':'; |
| 246 | offset += 2; |
| 247 | } |
| 248 | |
| 249 | if (digest) |
| 250 | memcpy(buffer + offset, digest, digestsize); |
| 251 | else |
| 252 | /* |
| 253 | * If digest is NULL, the event being recorded is a violation. |
| 254 | * Make room for the digest by increasing the offset of |
| 255 | * IMA_DIGEST_SIZE. |
| 256 | */ |
| 257 | offset += IMA_DIGEST_SIZE; |
| 258 | |
| 259 | return ima_write_template_field_data(buffer, offset + digestsize, |
| 260 | fmt, field_data); |
| 261 | } |
| 262 | |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 263 | /* |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 264 | * This function writes the digest of an event (with size limit). |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 265 | */ |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 266 | int ima_eventdigest_init(struct ima_event_data *event_data, |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 267 | struct ima_field_data *field_data) |
| 268 | { |
| 269 | struct { |
| 270 | struct ima_digest_data hdr; |
| 271 | char digest[IMA_MAX_DIGEST_SIZE]; |
| 272 | } hash; |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 273 | u8 *cur_digest = NULL; |
| 274 | u32 cur_digestsize = 0; |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 275 | struct inode *inode; |
| 276 | int result; |
| 277 | |
| 278 | memset(&hash, 0, sizeof(hash)); |
| 279 | |
Roberto Sassu | 8d94eb9 | 2015-04-11 17:12:39 +0200 | [diff] [blame] | 280 | if (event_data->violation) /* recording a violation. */ |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 281 | goto out; |
| 282 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 283 | if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) { |
| 284 | cur_digest = event_data->iint->ima_hash->digest; |
| 285 | cur_digestsize = event_data->iint->ima_hash->length; |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 286 | goto out; |
| 287 | } |
| 288 | |
Roberto Sassu | 6cc7c26 | 2020-06-03 17:08:21 +0200 | [diff] [blame] | 289 | if ((const char *)event_data->filename == boot_aggregate_name) { |
| 290 | if (ima_tpm_chip) { |
| 291 | hash.hdr.algo = HASH_ALGO_SHA1; |
| 292 | result = ima_calc_boot_aggregate(&hash.hdr); |
| 293 | |
| 294 | /* algo can change depending on available PCR banks */ |
| 295 | if (!result && hash.hdr.algo != HASH_ALGO_SHA1) |
| 296 | result = -EINVAL; |
| 297 | |
| 298 | if (result < 0) |
| 299 | memset(&hash, 0, sizeof(hash)); |
| 300 | } |
| 301 | |
| 302 | cur_digest = hash.hdr.digest; |
| 303 | cur_digestsize = hash_digest_size[HASH_ALGO_SHA1]; |
| 304 | goto out; |
| 305 | } |
| 306 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 307 | if (!event_data->file) /* missing info to re-calculate the digest */ |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 308 | return -EINVAL; |
| 309 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 310 | inode = file_inode(event_data->file); |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 311 | hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ? |
| 312 | ima_hash_algo : HASH_ALGO_SHA1; |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 313 | result = ima_calc_file_hash(event_data->file, &hash.hdr); |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 314 | if (result) { |
| 315 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 316 | event_data->filename, "collect_data", |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 317 | "failed", result, 0); |
| 318 | return result; |
| 319 | } |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 320 | cur_digest = hash.hdr.digest; |
| 321 | cur_digestsize = hash.hdr.length; |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 322 | out: |
Roberto Sassu | 712a49b | 2013-11-08 19:21:36 +0100 | [diff] [blame] | 323 | return ima_eventdigest_init_common(cur_digest, cur_digestsize, |
Roberto Sassu | dcf4e39 | 2013-11-08 19:21:37 +0100 | [diff] [blame] | 324 | HASH_ALGO__LAST, field_data); |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 328 | * This function writes the digest of an event (without size limit). |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 329 | */ |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 330 | int ima_eventdigest_ng_init(struct ima_event_data *event_data, |
| 331 | struct ima_field_data *field_data) |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 332 | { |
Roberto Sassu | c502c78 | 2013-11-08 19:21:35 +0100 | [diff] [blame] | 333 | u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1; |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 334 | u32 cur_digestsize = 0; |
| 335 | |
Roberto Sassu | 8d94eb9 | 2015-04-11 17:12:39 +0200 | [diff] [blame] | 336 | if (event_data->violation) /* recording a violation. */ |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 337 | goto out; |
| 338 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 339 | cur_digest = event_data->iint->ima_hash->digest; |
| 340 | cur_digestsize = event_data->iint->ima_hash->length; |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 341 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 342 | hash_algo = event_data->iint->ima_hash->algo; |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 343 | out: |
| 344 | return ima_eventdigest_init_common(cur_digest, cur_digestsize, |
Roberto Sassu | dcf4e39 | 2013-11-08 19:21:37 +0100 | [diff] [blame] | 345 | hash_algo, field_data); |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Thiago Jung Bauermann | 3878d50 | 2019-06-27 23:19:32 -0300 | [diff] [blame] | 348 | /* |
| 349 | * This function writes the digest of the file which is expected to match the |
| 350 | * digest contained in the file's appended signature. |
| 351 | */ |
| 352 | int ima_eventdigest_modsig_init(struct ima_event_data *event_data, |
| 353 | struct ima_field_data *field_data) |
| 354 | { |
| 355 | enum hash_algo hash_algo; |
| 356 | const u8 *cur_digest; |
| 357 | u32 cur_digestsize; |
| 358 | |
| 359 | if (!event_data->modsig) |
| 360 | return 0; |
| 361 | |
| 362 | if (event_data->violation) { |
| 363 | /* Recording a violation. */ |
| 364 | hash_algo = HASH_ALGO_SHA1; |
| 365 | cur_digest = NULL; |
| 366 | cur_digestsize = 0; |
| 367 | } else { |
| 368 | int rc; |
| 369 | |
| 370 | rc = ima_get_modsig_digest(event_data->modsig, &hash_algo, |
| 371 | &cur_digest, &cur_digestsize); |
| 372 | if (rc) |
| 373 | return rc; |
| 374 | else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0) |
| 375 | /* There was some error collecting the digest. */ |
| 376 | return -EINVAL; |
| 377 | } |
| 378 | |
| 379 | return ima_eventdigest_init_common(cur_digest, cur_digestsize, |
| 380 | hash_algo, field_data); |
| 381 | } |
| 382 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 383 | static int ima_eventname_init_common(struct ima_event_data *event_data, |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 384 | struct ima_field_data *field_data, |
| 385 | bool size_limit) |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 386 | { |
| 387 | const char *cur_filename = NULL; |
| 388 | u32 cur_filename_len = 0; |
| 389 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 390 | BUG_ON(event_data->filename == NULL && event_data->file == NULL); |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 391 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 392 | if (event_data->filename) { |
| 393 | cur_filename = event_data->filename; |
| 394 | cur_filename_len = strlen(event_data->filename); |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 395 | |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 396 | if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX) |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 397 | goto out; |
| 398 | } |
| 399 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 400 | if (event_data->file) { |
| 401 | cur_filename = event_data->file->f_path.dentry->d_name.name; |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 402 | cur_filename_len = strlen(cur_filename); |
| 403 | } else |
| 404 | /* |
| 405 | * Truncate filename if the latter is too long and |
| 406 | * the file descriptor is not available. |
| 407 | */ |
| 408 | cur_filename_len = IMA_EVENT_NAME_LEN_MAX; |
| 409 | out: |
| 410 | return ima_write_template_field_data(cur_filename, cur_filename_len, |
Roberto Sassu | e3b64c2 | 2014-02-03 13:56:05 +0100 | [diff] [blame] | 411 | DATA_FMT_STRING, field_data); |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* |
| 415 | * This function writes the name of an event (with size limit). |
| 416 | */ |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 417 | int ima_eventname_init(struct ima_event_data *event_data, |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 418 | struct ima_field_data *field_data) |
| 419 | { |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 420 | return ima_eventname_init_common(event_data, field_data, true); |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* |
| 424 | * This function writes the name of an event (without size limit). |
| 425 | */ |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 426 | int ima_eventname_ng_init(struct ima_event_data *event_data, |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 427 | struct ima_field_data *field_data) |
| 428 | { |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 429 | return ima_eventname_init_common(event_data, field_data, false); |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 430 | } |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 431 | |
| 432 | /* |
| 433 | * ima_eventsig_init - include the file signature as part of the template data |
| 434 | */ |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 435 | int ima_eventsig_init(struct ima_event_data *event_data, |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 436 | struct ima_field_data *field_data) |
| 437 | { |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 438 | struct evm_ima_xattr_data *xattr_value = event_data->xattr_value; |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 439 | |
| 440 | if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG)) |
Thiago Jung Bauermann | 1775cb8 | 2018-03-14 17:20:18 -0300 | [diff] [blame] | 441 | return 0; |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 442 | |
Thiago Jung Bauermann | 1775cb8 | 2018-03-14 17:20:18 -0300 | [diff] [blame] | 443 | return ima_write_template_field_data(xattr_value, event_data->xattr_len, |
| 444 | DATA_FMT_HEX, field_data); |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 445 | } |
Prakhar Srivastava | 86b4da8 | 2019-06-23 23:23:30 -0700 | [diff] [blame] | 446 | |
| 447 | /* |
| 448 | * ima_eventbuf_init - include the buffer(kexec-cmldine) as part of the |
| 449 | * template data. |
| 450 | */ |
| 451 | int ima_eventbuf_init(struct ima_event_data *event_data, |
| 452 | struct ima_field_data *field_data) |
| 453 | { |
| 454 | if ((!event_data->buf) || (event_data->buf_len == 0)) |
| 455 | return 0; |
| 456 | |
| 457 | return ima_write_template_field_data(event_data->buf, |
| 458 | event_data->buf_len, DATA_FMT_HEX, |
| 459 | field_data); |
| 460 | } |
Thiago Jung Bauermann | 3878d50 | 2019-06-27 23:19:32 -0300 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * ima_eventmodsig_init - include the appended file signature as part of the |
| 464 | * template data |
| 465 | */ |
| 466 | int ima_eventmodsig_init(struct ima_event_data *event_data, |
| 467 | struct ima_field_data *field_data) |
| 468 | { |
| 469 | const void *data; |
| 470 | u32 data_len; |
| 471 | int rc; |
| 472 | |
| 473 | if (!event_data->modsig) |
| 474 | return 0; |
| 475 | |
| 476 | /* |
| 477 | * modsig is a runtime structure containing pointers. Get its raw data |
| 478 | * instead. |
| 479 | */ |
| 480 | rc = ima_get_raw_modsig(event_data->modsig, &data, &data_len); |
| 481 | if (rc) |
| 482 | return rc; |
| 483 | |
| 484 | return ima_write_template_field_data(data, data_len, DATA_FMT_HEX, |
| 485 | field_data); |
| 486 | } |