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