1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Politecnico di Torino, Italy
4 * TORSEC group -- https://security.polito.it
6 * Author: Roberto Sassu <roberto.sassu@polito.it>
8 * File: ima_template_lib.c
9 * Library of supported template fields.
12 #include "ima_template_lib.h"
13 #include <linux/xattr.h>
14 #include <linux/evm.h>
16 static bool ima_template_hash_algo_allowed(u8 algo)
18 if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
26 DATA_FMT_DIGEST_WITH_ALGO,
27 DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO,
39 #define DIGEST_TYPE_NAME_LEN_MAX 7 /* including NUL */
40 static const char * const digest_type_name[DIGEST_TYPE__LAST] = {
41 [DIGEST_TYPE_IMA] = "ima",
42 [DIGEST_TYPE_VERITY] = "verity"
45 static int ima_write_template_field_data(const void *data, const u32 datalen,
46 enum data_formats datafmt,
47 struct ima_field_data *field_data)
52 if (datafmt == DATA_FMT_STRING)
55 buf = kzalloc(buflen, GFP_KERNEL);
59 memcpy(buf, data, datalen);
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).
68 if (datafmt == DATA_FMT_STRING) {
69 for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
74 field_data->data = buf;
75 field_data->len = buflen;
79 static 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)
84 u8 *buf_ptr = field_data->data;
85 u32 buflen = field_data->len;
88 case DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:
89 case DATA_FMT_DIGEST_WITH_ALGO:
90 buf_ptr = strrchr(field_data->data, ':');
91 if (buf_ptr != field_data->data)
92 seq_printf(m, "%s", field_data->data);
94 /* skip ':' and '\0' */
96 buflen -= buf_ptr - field_data->data;
102 ima_print_digest(m, buf_ptr, buflen);
104 case DATA_FMT_STRING:
105 seq_printf(m, "%s", buf_ptr);
108 switch (field_data->len) {
110 seq_printf(m, "%u", *(u8 *)buf_ptr);
113 if (ima_canonical_fmt)
115 le16_to_cpu(*(__le16 *)buf_ptr));
117 seq_printf(m, "%u", *(u16 *)buf_ptr);
120 if (ima_canonical_fmt)
122 le32_to_cpu(*(__le32 *)buf_ptr));
124 seq_printf(m, "%u", *(u32 *)buf_ptr);
127 if (ima_canonical_fmt)
128 seq_printf(m, "%llu",
129 le64_to_cpu(*(__le64 *)buf_ptr));
131 seq_printf(m, "%llu", *(u64 *)buf_ptr);
142 static 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)
147 u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
148 strlen(field_data->data) : field_data->len;
150 if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
151 u32 field_len = !ima_canonical_fmt ?
152 len : (__force u32)cpu_to_le32(len);
154 ima_putc(m, &field_len, sizeof(field_len));
160 ima_putc(m, field_data->data, len);
163 static 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)
170 ima_show_template_data_ascii(m, show, datafmt, field_data);
172 case IMA_SHOW_BINARY:
173 case IMA_SHOW_BINARY_NO_FIELD_LEN:
174 case IMA_SHOW_BINARY_OLD_STRING_FMT:
175 ima_show_template_data_binary(m, show, datafmt, field_data);
182 void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
183 struct ima_field_data *field_data)
185 ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
188 void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
189 struct ima_field_data *field_data)
191 ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
195 void ima_show_template_digest_ngv2(struct seq_file *m, enum ima_show_type show,
196 struct ima_field_data *field_data)
198 ima_show_template_field_data(m, show,
199 DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO,
203 void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
204 struct ima_field_data *field_data)
206 ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
209 void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
210 struct ima_field_data *field_data)
212 ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
215 void ima_show_template_buf(struct seq_file *m, enum ima_show_type show,
216 struct ima_field_data *field_data)
218 ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
221 void ima_show_template_uint(struct seq_file *m, enum ima_show_type show,
222 struct ima_field_data *field_data)
224 ima_show_template_field_data(m, show, DATA_FMT_UINT, field_data);
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.
239 * Return: 0 on success, -EINVAL on error.
241 int 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)
245 void *bufp = bufstartp;
248 for (i = 0; i < maxfields; i++) {
249 if (len_mask == NULL || !test_bit(i, len_mask)) {
250 if (bufp > (bufendp - sizeof(u32)))
253 if (ima_canonical_fmt)
254 fields[i].len = le32_to_cpu(*(__le32 *)bufp);
256 fields[i].len = *(u32 *)bufp;
261 if (bufp > (bufendp - fields[i].len))
264 fields[i].data = bufp;
265 bufp += fields[i].len;
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);
274 if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {
275 pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
276 bufname, bufendp, bufp);
289 static int ima_eventdigest_init_common(const u8 *digest, u32 digestsize,
290 u8 digest_type, u8 hash_algo,
291 struct ima_field_data *field_data)
295 * - DATA_FMT_DIGEST: digest
296 * - DATA_FMT_DIGEST_WITH_ALGO: <hash algo> + ':' + '\0' + digest,
297 * - DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:
298 * <digest type> + ':' + <hash algo> + ':' + '\0' + digest,
300 * where 'DATA_FMT_DIGEST' is the original digest format ('d')
301 * with a hash size limitation of 20 bytes,
302 * where <digest type> is either "ima" or "verity",
303 * where <hash algo> is the hash_algo_name[] string.
305 u8 buffer[DIGEST_TYPE_NAME_LEN_MAX + CRYPTO_MAX_ALG_NAME + 2 +
306 IMA_MAX_DIGEST_SIZE] = { 0 };
307 enum data_formats fmt = DATA_FMT_DIGEST;
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) {
316 fmt = DATA_FMT_DIGEST_WITH_ALGO;
317 offset += 1 + sprintf(buffer, "%s:",
318 hash_algo_name[hash_algo]);
322 memcpy(buffer + offset, digest, digestsize);
325 * If digest is NULL, the event being recorded is a violation.
326 * Make room for the digest by increasing the offset by the
327 * hash algorithm digest size.
329 offset += hash_digest_size[hash_algo];
331 return ima_write_template_field_data(buffer, offset + digestsize,
336 * This function writes the digest of an event (with size limit).
338 int ima_eventdigest_init(struct ima_event_data *event_data,
339 struct ima_field_data *field_data)
341 struct ima_max_digest_data hash;
342 u8 *cur_digest = NULL;
343 u32 cur_digestsize = 0;
347 memset(&hash, 0, sizeof(hash));
349 if (event_data->violation) /* recording a violation. */
352 if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
353 cur_digest = event_data->iint->ima_hash->digest;
354 cur_digestsize = event_data->iint->ima_hash->length;
358 if ((const char *)event_data->filename == boot_aggregate_name) {
360 hash.hdr.algo = HASH_ALGO_SHA1;
361 result = ima_calc_boot_aggregate(&hash.hdr);
363 /* algo can change depending on available PCR banks */
364 if (!result && hash.hdr.algo != HASH_ALGO_SHA1)
368 memset(&hash, 0, sizeof(hash));
371 cur_digest = hash.hdr.digest;
372 cur_digestsize = hash_digest_size[HASH_ALGO_SHA1];
376 if (!event_data->file) /* missing info to re-calculate the digest */
379 inode = file_inode(event_data->file);
380 hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
381 ima_hash_algo : HASH_ALGO_SHA1;
382 result = ima_calc_file_hash(event_data->file, &hash.hdr);
384 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
385 event_data->filename, "collect_data",
386 "failed", result, 0);
389 cur_digest = hash.hdr.digest;
390 cur_digestsize = hash.hdr.length;
392 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
393 DIGEST_TYPE__LAST, HASH_ALGO__LAST,
398 * This function writes the digest of an event (without size limit).
400 int ima_eventdigest_ng_init(struct ima_event_data *event_data,
401 struct ima_field_data *field_data)
403 u8 *cur_digest = NULL, hash_algo = ima_hash_algo;
404 u32 cur_digestsize = 0;
406 if (event_data->violation) /* recording a violation. */
409 cur_digest = event_data->iint->ima_hash->digest;
410 cur_digestsize = event_data->iint->ima_hash->length;
412 hash_algo = event_data->iint->ima_hash->algo;
414 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
415 DIGEST_TYPE__LAST, hash_algo,
420 * This function writes the digest of an event (without size limit),
421 * prefixed with both the digest type and hash algorithm.
423 int ima_eventdigest_ngv2_init(struct ima_event_data *event_data,
424 struct ima_field_data *field_data)
426 u8 *cur_digest = NULL, hash_algo = ima_hash_algo;
427 u32 cur_digestsize = 0;
428 u8 digest_type = DIGEST_TYPE_IMA;
430 if (event_data->violation) /* recording a violation. */
433 cur_digest = event_data->iint->ima_hash->digest;
434 cur_digestsize = event_data->iint->ima_hash->length;
436 hash_algo = event_data->iint->ima_hash->algo;
437 if (event_data->iint->flags & IMA_VERITY_REQUIRED)
438 digest_type = DIGEST_TYPE_VERITY;
440 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
441 digest_type, hash_algo,
446 * This function writes the digest of the file which is expected to match the
447 * digest contained in the file's appended signature.
449 int ima_eventdigest_modsig_init(struct ima_event_data *event_data,
450 struct ima_field_data *field_data)
452 enum hash_algo hash_algo;
453 const u8 *cur_digest;
456 if (!event_data->modsig)
459 if (event_data->violation) {
460 /* Recording a violation. */
461 hash_algo = HASH_ALGO_SHA1;
467 rc = ima_get_modsig_digest(event_data->modsig, &hash_algo,
468 &cur_digest, &cur_digestsize);
471 else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0)
472 /* There was some error collecting the digest. */
476 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
477 DIGEST_TYPE__LAST, hash_algo,
481 static int ima_eventname_init_common(struct ima_event_data *event_data,
482 struct ima_field_data *field_data,
485 const char *cur_filename = NULL;
486 u32 cur_filename_len = 0;
488 BUG_ON(event_data->filename == NULL && event_data->file == NULL);
490 if (event_data->filename) {
491 cur_filename = event_data->filename;
492 cur_filename_len = strlen(event_data->filename);
494 if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
498 if (event_data->file) {
499 cur_filename = event_data->file->f_path.dentry->d_name.name;
500 cur_filename_len = strlen(cur_filename);
503 * Truncate filename if the latter is too long and
504 * the file descriptor is not available.
506 cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
508 return ima_write_template_field_data(cur_filename, cur_filename_len,
509 DATA_FMT_STRING, field_data);
513 * This function writes the name of an event (with size limit).
515 int ima_eventname_init(struct ima_event_data *event_data,
516 struct ima_field_data *field_data)
518 return ima_eventname_init_common(event_data, field_data, true);
522 * This function writes the name of an event (without size limit).
524 int ima_eventname_ng_init(struct ima_event_data *event_data,
525 struct ima_field_data *field_data)
527 return ima_eventname_init_common(event_data, field_data, false);
531 * ima_eventsig_init - include the file signature as part of the template data
533 int ima_eventsig_init(struct ima_event_data *event_data,
534 struct ima_field_data *field_data)
536 struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
539 (xattr_value->type != EVM_IMA_XATTR_DIGSIG &&
540 xattr_value->type != IMA_VERITY_DIGSIG))
541 return ima_eventevmsig_init(event_data, field_data);
543 return ima_write_template_field_data(xattr_value, event_data->xattr_len,
544 DATA_FMT_HEX, field_data);
548 * ima_eventbuf_init - include the buffer(kexec-cmldine) as part of the
551 int ima_eventbuf_init(struct ima_event_data *event_data,
552 struct ima_field_data *field_data)
554 if ((!event_data->buf) || (event_data->buf_len == 0))
557 return ima_write_template_field_data(event_data->buf,
558 event_data->buf_len, DATA_FMT_HEX,
563 * ima_eventmodsig_init - include the appended file signature as part of the
566 int ima_eventmodsig_init(struct ima_event_data *event_data,
567 struct ima_field_data *field_data)
573 if (!event_data->modsig)
577 * modsig is a runtime structure containing pointers. Get its raw data
580 rc = ima_get_raw_modsig(event_data->modsig, &data, &data_len);
584 return ima_write_template_field_data(data, data_len, DATA_FMT_HEX,
589 * ima_eventevmsig_init - include the EVM portable signature as part of the
592 int ima_eventevmsig_init(struct ima_event_data *event_data,
593 struct ima_field_data *field_data)
595 struct evm_ima_xattr_data *xattr_data = NULL;
598 if (!event_data->file)
601 rc = vfs_getxattr_alloc(&nop_mnt_idmap, file_dentry(event_data->file),
602 XATTR_NAME_EVM, (char **)&xattr_data, 0,
604 if (rc <= 0 || xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG) {
609 rc = ima_write_template_field_data((char *)xattr_data, rc, DATA_FMT_HEX,
617 static int ima_eventinodedac_init_common(struct ima_event_data *event_data,
618 struct ima_field_data *field_data,
623 if (!event_data->file)
627 id = i_uid_read(file_inode(event_data->file));
629 id = i_gid_read(file_inode(event_data->file));
631 if (ima_canonical_fmt) {
632 if (sizeof(id) == sizeof(u16))
633 id = (__force u16)cpu_to_le16(id);
635 id = (__force u32)cpu_to_le32(id);
638 return ima_write_template_field_data((void *)&id, sizeof(id),
639 DATA_FMT_UINT, field_data);
643 * ima_eventinodeuid_init - include the inode UID as part of the template
646 int ima_eventinodeuid_init(struct ima_event_data *event_data,
647 struct ima_field_data *field_data)
649 return ima_eventinodedac_init_common(event_data, field_data, true);
653 * ima_eventinodegid_init - include the inode GID as part of the template
656 int ima_eventinodegid_init(struct ima_event_data *event_data,
657 struct ima_field_data *field_data)
659 return ima_eventinodedac_init_common(event_data, field_data, false);
663 * ima_eventinodemode_init - include the inode mode as part of the template
666 int ima_eventinodemode_init(struct ima_event_data *event_data,
667 struct ima_field_data *field_data)
672 if (!event_data->file)
675 inode = file_inode(event_data->file);
676 mode = inode->i_mode;
677 if (ima_canonical_fmt)
678 mode = (__force u16)cpu_to_le16(mode);
680 return ima_write_template_field_data((char *)&mode, sizeof(mode),
681 DATA_FMT_UINT, field_data);
684 static int ima_eventinodexattrs_init_common(struct ima_event_data *event_data,
685 struct ima_field_data *field_data,
691 if (!event_data->file)
694 rc = evm_read_protected_xattrs(file_dentry(event_data->file), NULL, 0,
695 type, ima_canonical_fmt);
699 buffer = kmalloc(rc, GFP_KERNEL);
703 rc = evm_read_protected_xattrs(file_dentry(event_data->file), buffer,
704 rc, type, ima_canonical_fmt);
710 rc = ima_write_template_field_data((char *)buffer, rc, DATA_FMT_HEX,
718 * ima_eventinodexattrnames_init - include a list of xattr names as part of the
721 int ima_eventinodexattrnames_init(struct ima_event_data *event_data,
722 struct ima_field_data *field_data)
724 return ima_eventinodexattrs_init_common(event_data, field_data, 'n');
728 * ima_eventinodexattrlengths_init - include a list of xattr lengths as part of
731 int ima_eventinodexattrlengths_init(struct ima_event_data *event_data,
732 struct ima_field_data *field_data)
734 return ima_eventinodexattrs_init_common(event_data, field_data, 'l');
738 * ima_eventinodexattrvalues_init - include a list of xattr values as part of
741 int ima_eventinodexattrvalues_init(struct ima_event_data *event_data,
742 struct ima_field_data *field_data)
744 return ima_eventinodexattrs_init_common(event_data, field_data, 'v');