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