1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Politecnico di Torino, Italy
4 * TORSEC group -- http://security.polito.it
8 * File: ima_template_lib.c
9 * Library of supported template fields.
12 #include "ima_template_lib.h"
14 static bool ima_template_hash_algo_allowed(u8 algo)
16 if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
24 DATA_FMT_DIGEST_WITH_ALGO,
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)
36 if (datafmt == DATA_FMT_STRING)
39 buf = kzalloc(buflen, GFP_KERNEL);
43 memcpy(buf, data, datalen);
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).
52 if (datafmt == DATA_FMT_STRING) {
53 for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
58 field_data->data = buf;
59 field_data->len = buflen;
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)
68 u8 *buf_ptr = field_data->data;
69 u32 buflen = field_data->len;
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);
77 /* skip ':' and '\0' */
79 buflen -= buf_ptr - field_data->data;
85 ima_print_digest(m, buf_ptr, buflen);
88 seq_printf(m, "%s", buf_ptr);
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)
100 u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
101 strlen(field_data->data) : field_data->len;
103 if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
104 u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len);
106 ima_putc(m, &field_len, sizeof(field_len));
112 ima_putc(m, field_data->data, len);
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)
122 ima_show_template_data_ascii(m, show, datafmt, field_data);
124 case IMA_SHOW_BINARY:
125 case IMA_SHOW_BINARY_NO_FIELD_LEN:
126 case IMA_SHOW_BINARY_OLD_STRING_FMT:
127 ima_show_template_data_binary(m, show, datafmt, field_data);
134 void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
135 struct ima_field_data *field_data)
137 ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
140 void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
141 struct ima_field_data *field_data)
143 ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
147 void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
148 struct ima_field_data *field_data)
150 ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
153 void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
154 struct ima_field_data *field_data)
156 ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
159 void ima_show_template_buf(struct seq_file *m, enum ima_show_type show,
160 struct ima_field_data *field_data)
162 ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
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.
177 * Return: 0 on success, -EINVAL on error.
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)
183 void *bufp = bufstartp;
186 for (i = 0; i < maxfields; i++) {
187 if (len_mask == NULL || !test_bit(i, len_mask)) {
188 if (bufp > (bufendp - sizeof(u32)))
191 fields[i].len = *(u32 *)bufp;
192 if (ima_canonical_fmt)
193 fields[i].len = le32_to_cpu(fields[i].len);
198 if (bufp > (bufendp - fields[i].len))
201 fields[i].data = bufp;
202 bufp += fields[i].len;
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);
211 if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {
212 pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
213 bufname, bufendp, bufp);
226 static int ima_eventdigest_init_common(const u8 *digest, u32 digestsize,
228 struct ima_field_data *field_data)
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
237 u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
238 enum data_formats fmt = DATA_FMT_DIGEST;
241 if (hash_algo < HASH_ALGO__LAST) {
242 fmt = DATA_FMT_DIGEST_WITH_ALGO;
243 offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s",
244 hash_algo_name[hash_algo]);
245 buffer[offset] = ':';
250 memcpy(buffer + offset, digest, digestsize);
253 * If digest is NULL, the event being recorded is a violation.
254 * Make room for the digest by increasing the offset of
257 offset += IMA_DIGEST_SIZE;
259 return ima_write_template_field_data(buffer, offset + digestsize,
264 * This function writes the digest of an event (with size limit).
266 int ima_eventdigest_init(struct ima_event_data *event_data,
267 struct ima_field_data *field_data)
270 struct ima_digest_data hdr;
271 char digest[IMA_MAX_DIGEST_SIZE];
273 u8 *cur_digest = NULL;
274 u32 cur_digestsize = 0;
278 memset(&hash, 0, sizeof(hash));
280 if (event_data->violation) /* recording a violation. */
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;
289 if (!event_data->file) /* missing info to re-calculate the digest */
292 inode = file_inode(event_data->file);
293 hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
294 ima_hash_algo : HASH_ALGO_SHA1;
295 result = ima_calc_file_hash(event_data->file, &hash.hdr);
297 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
298 event_data->filename, "collect_data",
299 "failed", result, 0);
302 cur_digest = hash.hdr.digest;
303 cur_digestsize = hash.hdr.length;
305 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
306 HASH_ALGO__LAST, field_data);
310 * This function writes the digest of an event (without size limit).
312 int ima_eventdigest_ng_init(struct ima_event_data *event_data,
313 struct ima_field_data *field_data)
315 u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
316 u32 cur_digestsize = 0;
318 if (event_data->violation) /* recording a violation. */
321 cur_digest = event_data->iint->ima_hash->digest;
322 cur_digestsize = event_data->iint->ima_hash->length;
324 hash_algo = event_data->iint->ima_hash->algo;
326 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
327 hash_algo, field_data);
331 * This function writes the digest of the file which is expected to match the
332 * digest contained in the file's appended signature.
334 int ima_eventdigest_modsig_init(struct ima_event_data *event_data,
335 struct ima_field_data *field_data)
337 enum hash_algo hash_algo;
338 const u8 *cur_digest;
341 if (!event_data->modsig)
344 if (event_data->violation) {
345 /* Recording a violation. */
346 hash_algo = HASH_ALGO_SHA1;
352 rc = ima_get_modsig_digest(event_data->modsig, &hash_algo,
353 &cur_digest, &cur_digestsize);
356 else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0)
357 /* There was some error collecting the digest. */
361 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
362 hash_algo, field_data);
365 static int ima_eventname_init_common(struct ima_event_data *event_data,
366 struct ima_field_data *field_data,
369 const char *cur_filename = NULL;
370 u32 cur_filename_len = 0;
372 BUG_ON(event_data->filename == NULL && event_data->file == NULL);
374 if (event_data->filename) {
375 cur_filename = event_data->filename;
376 cur_filename_len = strlen(event_data->filename);
378 if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
382 if (event_data->file) {
383 cur_filename = event_data->file->f_path.dentry->d_name.name;
384 cur_filename_len = strlen(cur_filename);
387 * Truncate filename if the latter is too long and
388 * the file descriptor is not available.
390 cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
392 return ima_write_template_field_data(cur_filename, cur_filename_len,
393 DATA_FMT_STRING, field_data);
397 * This function writes the name of an event (with size limit).
399 int ima_eventname_init(struct ima_event_data *event_data,
400 struct ima_field_data *field_data)
402 return ima_eventname_init_common(event_data, field_data, true);
406 * This function writes the name of an event (without size limit).
408 int ima_eventname_ng_init(struct ima_event_data *event_data,
409 struct ima_field_data *field_data)
411 return ima_eventname_init_common(event_data, field_data, false);
415 * ima_eventsig_init - include the file signature as part of the template data
417 int ima_eventsig_init(struct ima_event_data *event_data,
418 struct ima_field_data *field_data)
420 struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
422 if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
425 return ima_write_template_field_data(xattr_value, event_data->xattr_len,
426 DATA_FMT_HEX, field_data);
430 * ima_eventbuf_init - include the buffer(kexec-cmldine) as part of the
433 int ima_eventbuf_init(struct ima_event_data *event_data,
434 struct ima_field_data *field_data)
436 if ((!event_data->buf) || (event_data->buf_len == 0))
439 return ima_write_template_field_data(event_data->buf,
440 event_data->buf_len, DATA_FMT_HEX,
445 * ima_eventmodsig_init - include the appended file signature as part of the
448 int ima_eventmodsig_init(struct ima_event_data *event_data,
449 struct ima_field_data *field_data)
455 if (!event_data->modsig)
459 * modsig is a runtime structure containing pointers. Get its raw data
462 rc = ima_get_raw_modsig(event_data->modsig, &data, &data_len);
466 return ima_write_template_field_data(data, data_len, DATA_FMT_HEX,