integrity: Avoid -Wflex-array-member-not-at-end warnings
[linux-2.6-block.git] / security / integrity / ima / ima_template_lib.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
3ce1217d
RS
2/*
3 * Copyright (C) 2013 Politecnico di Torino, Italy
c9fecf50 4 * TORSEC group -- https://security.polito.it
3ce1217d
RS
5 *
6 * Author: Roberto Sassu <roberto.sassu@polito.it>
7 *
3ce1217d
RS
8 * File: ima_template_lib.c
9 * Library of supported template fields.
10 */
4d7aeee7 11
3ce1217d 12#include "ima_template_lib.h"
026d7fc9 13#include <linux/xattr.h>
8314b673 14#include <linux/evm.h>
3ce1217d 15
4d7aeee7
RS
16static 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
24enum data_formats {
25 DATA_FMT_DIGEST = 0,
26 DATA_FMT_DIGEST_WITH_ALGO,
989dc725 27 DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO,
bcbc9b0c 28 DATA_FMT_STRING,
cde1391a
RS
29 DATA_FMT_HEX,
30 DATA_FMT_UINT
4d7aeee7
RS
31};
32
989dc725
MZ
33enum digest_type {
34 DIGEST_TYPE_IMA,
54f03916 35 DIGEST_TYPE_VERITY,
989dc725
MZ
36 DIGEST_TYPE__LAST
37};
38
54f03916 39#define DIGEST_TYPE_NAME_LEN_MAX 7 /* including NUL */
989dc725 40static const char * const digest_type_name[DIGEST_TYPE__LAST] = {
54f03916
MZ
41 [DIGEST_TYPE_IMA] = "ima",
42 [DIGEST_TYPE_VERITY] = "verity"
989dc725
MZ
43};
44
3ce1217d
RS
45static int ima_write_template_field_data(const void *data, const u32 datalen,
46 enum data_formats datafmt,
47 struct ima_field_data *field_data)
48{
49 u8 *buf, *buf_ptr;
e3b64c26 50 u32 buflen = datalen;
3ce1217d 51
e3b64c26 52 if (datafmt == DATA_FMT_STRING)
3ce1217d 53 buflen = datalen + 1;
3ce1217d
RS
54
55 buf = kzalloc(buflen, GFP_KERNEL);
56 if (!buf)
57 return -ENOMEM;
58
59 memcpy(buf, data, datalen);
60
61 /*
62 * Replace all space characters with underscore for event names and
63 * strings. This avoid that, during the parsing of a measurements list,
64 * filenames with spaces or that end with the suffix ' (deleted)' are
65 * split into multiple template fields (the space is the delimitator
66 * character for measurements lists in ASCII format).
67 */
e3b64c26 68 if (datafmt == DATA_FMT_STRING) {
3ce1217d
RS
69 for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
70 if (*buf_ptr == ' ')
71 *buf_ptr = '_';
72 }
73
74 field_data->data = buf;
75 field_data->len = buflen;
76 return 0;
77}
78
79static void ima_show_template_data_ascii(struct seq_file *m,
80 enum ima_show_type show,
81 enum data_formats datafmt,
82 struct ima_field_data *field_data)
83{
45b26133
MZ
84 u8 *buf_ptr = field_data->data;
85 u32 buflen = field_data->len;
4d7aeee7 86
3ce1217d 87 switch (datafmt) {
989dc725 88 case DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:
4d7aeee7 89 case DATA_FMT_DIGEST_WITH_ALGO:
989dc725 90 buf_ptr = strrchr(field_data->data, ':');
4d7aeee7
RS
91 if (buf_ptr != field_data->data)
92 seq_printf(m, "%s", field_data->data);
93
94 /* skip ':' and '\0' */
95 buf_ptr += 2;
96 buflen -= buf_ptr - field_data->data;
df561f66 97 fallthrough;
3ce1217d 98 case DATA_FMT_DIGEST:
bcbc9b0c
MZ
99 case DATA_FMT_HEX:
100 if (!buflen)
101 break;
4d7aeee7 102 ima_print_digest(m, buf_ptr, buflen);
3ce1217d
RS
103 break;
104 case DATA_FMT_STRING:
4d7aeee7 105 seq_printf(m, "%s", buf_ptr);
3ce1217d 106 break;
cde1391a
RS
107 case DATA_FMT_UINT:
108 switch (field_data->len) {
109 case sizeof(u8):
110 seq_printf(m, "%u", *(u8 *)buf_ptr);
111 break;
112 case sizeof(u16):
113 if (ima_canonical_fmt)
114 seq_printf(m, "%u",
24c9ae23 115 le16_to_cpu(*(__le16 *)buf_ptr));
cde1391a
RS
116 else
117 seq_printf(m, "%u", *(u16 *)buf_ptr);
118 break;
119 case sizeof(u32):
120 if (ima_canonical_fmt)
121 seq_printf(m, "%u",
24c9ae23 122 le32_to_cpu(*(__le32 *)buf_ptr));
cde1391a
RS
123 else
124 seq_printf(m, "%u", *(u32 *)buf_ptr);
125 break;
126 case sizeof(u64):
127 if (ima_canonical_fmt)
128 seq_printf(m, "%llu",
24c9ae23 129 le64_to_cpu(*(__le64 *)buf_ptr));
cde1391a
RS
130 else
131 seq_printf(m, "%llu", *(u64 *)buf_ptr);
132 break;
133 default:
134 break;
135 }
7d2201d4 136 break;
3ce1217d
RS
137 default:
138 break;
139 }
140}
141
142static void ima_show_template_data_binary(struct seq_file *m,
143 enum ima_show_type show,
144 enum data_formats datafmt,
145 struct ima_field_data *field_data)
146{
c019e307
RS
147 u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
148 strlen(field_data->data) : field_data->len;
149
d68a6fe9 150 if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
6b26285f
RS
151 u32 field_len = !ima_canonical_fmt ?
152 len : (__force u32)cpu_to_le32(len);
d68a6fe9
MZ
153
154 ima_putc(m, &field_len, sizeof(field_len));
155 }
3e8e5503 156
c019e307 157 if (!len)
3ce1217d 158 return;
3e8e5503 159
c019e307 160 ima_putc(m, field_data->data, len);
3ce1217d
RS
161}
162
163static void ima_show_template_field_data(struct seq_file *m,
164 enum ima_show_type show,
165 enum data_formats datafmt,
166 struct ima_field_data *field_data)
167{
168 switch (show) {
169 case IMA_SHOW_ASCII:
170 ima_show_template_data_ascii(m, show, datafmt, field_data);
171 break;
172 case IMA_SHOW_BINARY:
3e8e5503 173 case IMA_SHOW_BINARY_NO_FIELD_LEN:
c019e307 174 case IMA_SHOW_BINARY_OLD_STRING_FMT:
3ce1217d
RS
175 ima_show_template_data_binary(m, show, datafmt, field_data);
176 break;
177 default:
178 break;
179 }
180}
181
182void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
183 struct ima_field_data *field_data)
184{
185 ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
186}
187
4d7aeee7
RS
188void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
189 struct ima_field_data *field_data)
190{
191 ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
192 field_data);
193}
194
989dc725
MZ
195void ima_show_template_digest_ngv2(struct seq_file *m, enum ima_show_type show,
196 struct ima_field_data *field_data)
197{
198 ima_show_template_field_data(m, show,
199 DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO,
200 field_data);
201}
202
3ce1217d
RS
203void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
204 struct ima_field_data *field_data)
205{
206 ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
207}
208
bcbc9b0c
MZ
209void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
210 struct ima_field_data *field_data)
211{
212 ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
213}
214
86b4da8c
PS
215void ima_show_template_buf(struct seq_file *m, enum ima_show_type show,
216 struct ima_field_data *field_data)
217{
218 ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
219}
220
cde1391a
RS
221void ima_show_template_uint(struct seq_file *m, enum ima_show_type show,
222 struct ima_field_data *field_data)
223{
224 ima_show_template_field_data(m, show, DATA_FMT_UINT, field_data);
225}
226
b17fd9ec
RS
227/**
228 * ima_parse_buf() - Parses lengths and data from an input buffer
229 * @bufstartp: Buffer start address.
230 * @bufendp: Buffer end address.
231 * @bufcurp: Pointer to remaining (non-parsed) data.
232 * @maxfields: Length of fields array.
233 * @fields: Array containing lengths and pointers of parsed data.
234 * @curfields: Number of array items containing parsed data.
235 * @len_mask: Bitmap (if bit is set, data length should not be parsed).
236 * @enforce_mask: Check if curfields == maxfields and/or bufcurp == bufendp.
237 * @bufname: String identifier of the input buffer.
238 *
239 * Return: 0 on success, -EINVAL on error.
240 */
241int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp,
242 int maxfields, struct ima_field_data *fields, int *curfields,
243 unsigned long *len_mask, int enforce_mask, char *bufname)
244{
245 void *bufp = bufstartp;
246 int i;
247
248 for (i = 0; i < maxfields; i++) {
249 if (len_mask == NULL || !test_bit(i, len_mask)) {
250 if (bufp > (bufendp - sizeof(u32)))
251 break;
252
b17fd9ec 253 if (ima_canonical_fmt)
24c9ae23
RS
254 fields[i].len = le32_to_cpu(*(__le32 *)bufp);
255 else
256 fields[i].len = *(u32 *)bufp;
b17fd9ec
RS
257
258 bufp += sizeof(u32);
259 }
260
261 if (bufp > (bufendp - fields[i].len))
262 break;
263
264 fields[i].data = bufp;
265 bufp += fields[i].len;
266 }
267
268 if ((enforce_mask & ENFORCE_FIELDS) && i != maxfields) {
269 pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n",
270 bufname, maxfields, i);
271 return -EINVAL;
272 }
273
274 if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {
275 pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
276 bufname, bufendp, bufp);
277 return -EINVAL;
278 }
279
280 if (curfields)
281 *curfields = i;
282
283 if (bufcurp)
284 *bufcurp = bufp;
285
286 return 0;
287}
288
3878d505 289static int ima_eventdigest_init_common(const u8 *digest, u32 digestsize,
989dc725 290 u8 digest_type, u8 hash_algo,
dcf4e392 291 struct ima_field_data *field_data)
4d7aeee7
RS
292{
293 /*
294 * digest formats:
295 * - DATA_FMT_DIGEST: digest
64466462 296 * - DATA_FMT_DIGEST_WITH_ALGO: <hash algo> + ':' + '\0' + digest,
989dc725
MZ
297 * - DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:
298 * <digest type> + ':' + <hash algo> + ':' + '\0' + digest,
64466462
MZ
299 *
300 * where 'DATA_FMT_DIGEST' is the original digest format ('d')
301 * with a hash size limitation of 20 bytes,
54f03916 302 * where <digest type> is either "ima" or "verity",
64466462 303 * where <hash algo> is the hash_algo_name[] string.
4d7aeee7 304 */
989dc725
MZ
305 u8 buffer[DIGEST_TYPE_NAME_LEN_MAX + CRYPTO_MAX_ALG_NAME + 2 +
306 IMA_MAX_DIGEST_SIZE] = { 0 };
4d7aeee7
RS
307 enum data_formats fmt = DATA_FMT_DIGEST;
308 u32 offset = 0;
309
989dc725
MZ
310 if (digest_type < DIGEST_TYPE__LAST && hash_algo < HASH_ALGO__LAST) {
311 fmt = DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO;
312 offset += 1 + sprintf(buffer, "%s:%s:",
313 digest_type_name[digest_type],
314 hash_algo_name[hash_algo]);
315 } else if (hash_algo < HASH_ALGO__LAST) {
4d7aeee7 316 fmt = DATA_FMT_DIGEST_WITH_ALGO;
989dc725
MZ
317 offset += 1 + sprintf(buffer, "%s:",
318 hash_algo_name[hash_algo]);
4d7aeee7
RS
319 }
320
321 if (digest)
322 memcpy(buffer + offset, digest, digestsize);
323 else
324 /*
325 * If digest is NULL, the event being recorded is a violation.
9fab303a
MZ
326 * Make room for the digest by increasing the offset by the
327 * hash algorithm digest size.
4d7aeee7 328 */
9fab303a 329 offset += hash_digest_size[hash_algo];
4d7aeee7
RS
330
331 return ima_write_template_field_data(buffer, offset + digestsize,
332 fmt, field_data);
333}
334
3ce1217d 335/*
4d7aeee7 336 * This function writes the digest of an event (with size limit).
3ce1217d 337 */
23b57419 338int ima_eventdigest_init(struct ima_event_data *event_data,
3ce1217d
RS
339 struct ima_field_data *field_data)
340{
8c54135e 341 struct ima_max_digest_data hash;
38aa3f5a
GS
342 struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
343 struct ima_digest_data, hdr);
4d7aeee7
RS
344 u8 *cur_digest = NULL;
345 u32 cur_digestsize = 0;
3ce1217d
RS
346 struct inode *inode;
347 int result;
348
349 memset(&hash, 0, sizeof(hash));
350
8d94eb9b 351 if (event_data->violation) /* recording a violation. */
3ce1217d
RS
352 goto out;
353
23b57419
RS
354 if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
355 cur_digest = event_data->iint->ima_hash->digest;
356 cur_digestsize = event_data->iint->ima_hash->length;
3ce1217d
RS
357 goto out;
358 }
359
6cc7c266
RS
360 if ((const char *)event_data->filename == boot_aggregate_name) {
361 if (ima_tpm_chip) {
362 hash.hdr.algo = HASH_ALGO_SHA1;
38aa3f5a 363 result = ima_calc_boot_aggregate(hash_hdr);
6cc7c266
RS
364
365 /* algo can change depending on available PCR banks */
366 if (!result && hash.hdr.algo != HASH_ALGO_SHA1)
367 result = -EINVAL;
368
369 if (result < 0)
370 memset(&hash, 0, sizeof(hash));
371 }
372
38aa3f5a 373 cur_digest = hash_hdr->digest;
6cc7c266
RS
374 cur_digestsize = hash_digest_size[HASH_ALGO_SHA1];
375 goto out;
376 }
377
23b57419 378 if (!event_data->file) /* missing info to re-calculate the digest */
3ce1217d
RS
379 return -EINVAL;
380
23b57419 381 inode = file_inode(event_data->file);
4d7aeee7
RS
382 hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
383 ima_hash_algo : HASH_ALGO_SHA1;
38aa3f5a 384 result = ima_calc_file_hash(event_data->file, hash_hdr);
3ce1217d
RS
385 if (result) {
386 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
23b57419 387 event_data->filename, "collect_data",
3ce1217d
RS
388 "failed", result, 0);
389 return result;
390 }
38aa3f5a 391 cur_digest = hash_hdr->digest;
4d7aeee7 392 cur_digestsize = hash.hdr.length;
3ce1217d 393out:
712a49bd 394 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
989dc725
MZ
395 DIGEST_TYPE__LAST, HASH_ALGO__LAST,
396 field_data);
3ce1217d
RS
397}
398
399/*
4d7aeee7 400 * This function writes the digest of an event (without size limit).
3ce1217d 401 */
23b57419
RS
402int ima_eventdigest_ng_init(struct ima_event_data *event_data,
403 struct ima_field_data *field_data)
4d7aeee7 404{
09091c44 405 u8 *cur_digest = NULL, hash_algo = ima_hash_algo;
4d7aeee7
RS
406 u32 cur_digestsize = 0;
407
8d94eb9b 408 if (event_data->violation) /* recording a violation. */
4d7aeee7
RS
409 goto out;
410
23b57419
RS
411 cur_digest = event_data->iint->ima_hash->digest;
412 cur_digestsize = event_data->iint->ima_hash->length;
4d7aeee7 413
23b57419 414 hash_algo = event_data->iint->ima_hash->algo;
4d7aeee7
RS
415out:
416 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
989dc725
MZ
417 DIGEST_TYPE__LAST, hash_algo,
418 field_data);
419}
420
421/*
422 * This function writes the digest of an event (without size limit),
423 * prefixed with both the digest type and hash algorithm.
424 */
425int ima_eventdigest_ngv2_init(struct ima_event_data *event_data,
426 struct ima_field_data *field_data)
427{
428 u8 *cur_digest = NULL, hash_algo = ima_hash_algo;
429 u32 cur_digestsize = 0;
430 u8 digest_type = DIGEST_TYPE_IMA;
431
432 if (event_data->violation) /* recording a violation. */
433 goto out;
434
435 cur_digest = event_data->iint->ima_hash->digest;
436 cur_digestsize = event_data->iint->ima_hash->length;
437
438 hash_algo = event_data->iint->ima_hash->algo;
54f03916
MZ
439 if (event_data->iint->flags & IMA_VERITY_REQUIRED)
440 digest_type = DIGEST_TYPE_VERITY;
989dc725
MZ
441out:
442 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
443 digest_type, hash_algo,
444 field_data);
4d7aeee7
RS
445}
446
3878d505
TJB
447/*
448 * This function writes the digest of the file which is expected to match the
449 * digest contained in the file's appended signature.
450 */
451int ima_eventdigest_modsig_init(struct ima_event_data *event_data,
452 struct ima_field_data *field_data)
453{
454 enum hash_algo hash_algo;
455 const u8 *cur_digest;
456 u32 cur_digestsize;
457
458 if (!event_data->modsig)
459 return 0;
460
461 if (event_data->violation) {
462 /* Recording a violation. */
463 hash_algo = HASH_ALGO_SHA1;
464 cur_digest = NULL;
465 cur_digestsize = 0;
466 } else {
467 int rc;
468
469 rc = ima_get_modsig_digest(event_data->modsig, &hash_algo,
470 &cur_digest, &cur_digestsize);
471 if (rc)
472 return rc;
473 else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0)
474 /* There was some error collecting the digest. */
475 return -EINVAL;
476 }
477
478 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
989dc725
MZ
479 DIGEST_TYPE__LAST, hash_algo,
480 field_data);
3878d505
TJB
481}
482
23b57419 483static int ima_eventname_init_common(struct ima_event_data *event_data,
4d7aeee7
RS
484 struct ima_field_data *field_data,
485 bool size_limit)
3ce1217d
RS
486{
487 const char *cur_filename = NULL;
be84f32b 488 struct name_snapshot filename;
3ce1217d 489 u32 cur_filename_len = 0;
be84f32b
SB
490 bool snapshot = false;
491 int ret;
3ce1217d 492
23b57419 493 BUG_ON(event_data->filename == NULL && event_data->file == NULL);
3ce1217d 494
23b57419
RS
495 if (event_data->filename) {
496 cur_filename = event_data->filename;
497 cur_filename_len = strlen(event_data->filename);
3ce1217d 498
4d7aeee7 499 if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
3ce1217d
RS
500 goto out;
501 }
502
23b57419 503 if (event_data->file) {
be84f32b
SB
504 take_dentry_name_snapshot(&filename,
505 event_data->file->f_path.dentry);
506 snapshot = true;
507 cur_filename = filename.name.name;
3ce1217d
RS
508 cur_filename_len = strlen(cur_filename);
509 } else
510 /*
511 * Truncate filename if the latter is too long and
512 * the file descriptor is not available.
513 */
514 cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
515out:
be84f32b
SB
516 ret = ima_write_template_field_data(cur_filename, cur_filename_len,
517 DATA_FMT_STRING, field_data);
518
519 if (snapshot)
520 release_dentry_name_snapshot(&filename);
521
522 return ret;
4d7aeee7
RS
523}
524
525/*
526 * This function writes the name of an event (with size limit).
527 */
23b57419 528int ima_eventname_init(struct ima_event_data *event_data,
4d7aeee7
RS
529 struct ima_field_data *field_data)
530{
23b57419 531 return ima_eventname_init_common(event_data, field_data, true);
4d7aeee7
RS
532}
533
534/*
535 * This function writes the name of an event (without size limit).
536 */
23b57419 537int ima_eventname_ng_init(struct ima_event_data *event_data,
4d7aeee7
RS
538 struct ima_field_data *field_data)
539{
23b57419 540 return ima_eventname_init_common(event_data, field_data, false);
3ce1217d 541}
bcbc9b0c
MZ
542
543/*
544 * ima_eventsig_init - include the file signature as part of the template data
545 */
23b57419 546int ima_eventsig_init(struct ima_event_data *event_data,
bcbc9b0c
MZ
547 struct ima_field_data *field_data)
548{
23b57419 549 struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
bcbc9b0c 550
398c42e2
MZ
551 if (!xattr_value ||
552 (xattr_value->type != EVM_IMA_XATTR_DIGSIG &&
553 xattr_value->type != IMA_VERITY_DIGSIG))
026d7fc9 554 return ima_eventevmsig_init(event_data, field_data);
bcbc9b0c 555
1775cb87
TJB
556 return ima_write_template_field_data(xattr_value, event_data->xattr_len,
557 DATA_FMT_HEX, field_data);
bcbc9b0c 558}
86b4da8c
PS
559
560/*
561 * ima_eventbuf_init - include the buffer(kexec-cmldine) as part of the
562 * template data.
563 */
564int ima_eventbuf_init(struct ima_event_data *event_data,
565 struct ima_field_data *field_data)
566{
567 if ((!event_data->buf) || (event_data->buf_len == 0))
568 return 0;
569
570 return ima_write_template_field_data(event_data->buf,
571 event_data->buf_len, DATA_FMT_HEX,
572 field_data);
573}
3878d505
TJB
574
575/*
576 * ima_eventmodsig_init - include the appended file signature as part of the
577 * template data
578 */
579int ima_eventmodsig_init(struct ima_event_data *event_data,
580 struct ima_field_data *field_data)
581{
582 const void *data;
583 u32 data_len;
584 int rc;
585
586 if (!event_data->modsig)
587 return 0;
588
589 /*
590 * modsig is a runtime structure containing pointers. Get its raw data
591 * instead.
592 */
593 rc = ima_get_raw_modsig(event_data->modsig, &data, &data_len);
594 if (rc)
595 return rc;
596
597 return ima_write_template_field_data(data, data_len, DATA_FMT_HEX,
598 field_data);
599}
026d7fc9
RS
600
601/*
602 * ima_eventevmsig_init - include the EVM portable signature as part of the
603 * template data
604 */
605int ima_eventevmsig_init(struct ima_event_data *event_data,
606 struct ima_field_data *field_data)
607{
608 struct evm_ima_xattr_data *xattr_data = NULL;
609 int rc = 0;
610
611 if (!event_data->file)
612 return 0;
613
4609e1f1 614 rc = vfs_getxattr_alloc(&nop_mnt_idmap, file_dentry(event_data->file),
026d7fc9
RS
615 XATTR_NAME_EVM, (char **)&xattr_data, 0,
616 GFP_NOFS);
f6fbd8cb
PM
617 if (rc <= 0 || xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG) {
618 rc = 0;
619 goto out;
026d7fc9
RS
620 }
621
622 rc = ima_write_template_field_data((char *)xattr_data, rc, DATA_FMT_HEX,
623 field_data);
f6fbd8cb
PM
624
625out:
026d7fc9
RS
626 kfree(xattr_data);
627 return rc;
628}
7dcfeacc
RS
629
630static int ima_eventinodedac_init_common(struct ima_event_data *event_data,
631 struct ima_field_data *field_data,
632 bool get_uid)
633{
634 unsigned int id;
635
636 if (!event_data->file)
637 return 0;
638
639 if (get_uid)
640 id = i_uid_read(file_inode(event_data->file));
641 else
642 id = i_gid_read(file_inode(event_data->file));
643
644 if (ima_canonical_fmt) {
645 if (sizeof(id) == sizeof(u16))
6b26285f 646 id = (__force u16)cpu_to_le16(id);
7dcfeacc 647 else
6b26285f 648 id = (__force u32)cpu_to_le32(id);
7dcfeacc
RS
649 }
650
651 return ima_write_template_field_data((void *)&id, sizeof(id),
652 DATA_FMT_UINT, field_data);
653}
654
655/*
656 * ima_eventinodeuid_init - include the inode UID as part of the template
657 * data
658 */
659int ima_eventinodeuid_init(struct ima_event_data *event_data,
660 struct ima_field_data *field_data)
661{
662 return ima_eventinodedac_init_common(event_data, field_data, true);
663}
664
665/*
666 * ima_eventinodegid_init - include the inode GID as part of the template
667 * data
668 */
669int ima_eventinodegid_init(struct ima_event_data *event_data,
670 struct ima_field_data *field_data)
671{
672 return ima_eventinodedac_init_common(event_data, field_data, false);
673}
f8216f6b
RS
674
675/*
676 * ima_eventinodemode_init - include the inode mode as part of the template
677 * data
678 */
679int ima_eventinodemode_init(struct ima_event_data *event_data,
680 struct ima_field_data *field_data)
681{
682 struct inode *inode;
6b26285f 683 u16 mode;
f8216f6b
RS
684
685 if (!event_data->file)
686 return 0;
687
688 inode = file_inode(event_data->file);
689 mode = inode->i_mode;
690 if (ima_canonical_fmt)
6b26285f 691 mode = (__force u16)cpu_to_le16(mode);
f8216f6b
RS
692
693 return ima_write_template_field_data((char *)&mode, sizeof(mode),
694 DATA_FMT_UINT, field_data);
695}
8314b673
RS
696
697static int ima_eventinodexattrs_init_common(struct ima_event_data *event_data,
698 struct ima_field_data *field_data,
699 char type)
700{
701 u8 *buffer = NULL;
702 int rc;
703
704 if (!event_data->file)
705 return 0;
706
707 rc = evm_read_protected_xattrs(file_dentry(event_data->file), NULL, 0,
708 type, ima_canonical_fmt);
709 if (rc < 0)
710 return 0;
711
712 buffer = kmalloc(rc, GFP_KERNEL);
713 if (!buffer)
714 return 0;
715
716 rc = evm_read_protected_xattrs(file_dentry(event_data->file), buffer,
717 rc, type, ima_canonical_fmt);
718 if (rc < 0) {
719 rc = 0;
720 goto out;
721 }
722
723 rc = ima_write_template_field_data((char *)buffer, rc, DATA_FMT_HEX,
724 field_data);
725out:
726 kfree(buffer);
727 return rc;
728}
729
730/*
731 * ima_eventinodexattrnames_init - include a list of xattr names as part of the
732 * template data
733 */
734int ima_eventinodexattrnames_init(struct ima_event_data *event_data,
735 struct ima_field_data *field_data)
736{
737 return ima_eventinodexattrs_init_common(event_data, field_data, 'n');
738}
739
740/*
741 * ima_eventinodexattrlengths_init - include a list of xattr lengths as part of
742 * the template data
743 */
744int ima_eventinodexattrlengths_init(struct ima_event_data *event_data,
745 struct ima_field_data *field_data)
746{
747 return ima_eventinodexattrs_init_common(event_data, field_data, 'l');
748}
749
750/*
751 * ima_eventinodexattrvalues_init - include a list of xattr values as part of
752 * the template data
753 */
754int ima_eventinodexattrvalues_init(struct ima_event_data *event_data,
755 struct ima_field_data *field_data)
756{
757 return ima_eventinodexattrs_init_common(event_data, field_data, 'v');
758}