1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Politecnico di Torino, Italy
4 * TORSEC group -- http://security.polito.it
6 * Author: Roberto Sassu <roberto.sassu@polito.it>
8 * File: ima_template_lib.c
9 * Library of supported template fields.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include "ima_template_lib.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,
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)
38 if (datafmt == DATA_FMT_STRING)
41 buf = kzalloc(buflen, GFP_KERNEL);
45 memcpy(buf, data, datalen);
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).
54 if (datafmt == DATA_FMT_STRING) {
55 for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
60 field_data->data = buf;
61 field_data->len = buflen;
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)
70 u8 *buf_ptr = field_data->data;
71 u32 buflen = field_data->len;
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);
79 /* skip ':' and '\0' */
81 buflen -= buf_ptr - field_data->data;
87 ima_print_digest(m, buf_ptr, buflen);
90 seq_printf(m, "%s", buf_ptr);
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)
102 u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
103 strlen(field_data->data) : field_data->len;
105 if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
106 u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len);
108 ima_putc(m, &field_len, sizeof(field_len));
114 ima_putc(m, field_data->data, len);
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)
124 ima_show_template_data_ascii(m, show, datafmt, field_data);
126 case IMA_SHOW_BINARY:
127 case IMA_SHOW_BINARY_NO_FIELD_LEN:
128 case IMA_SHOW_BINARY_OLD_STRING_FMT:
129 ima_show_template_data_binary(m, show, datafmt, field_data);
136 void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
137 struct ima_field_data *field_data)
139 ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
142 void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
143 struct ima_field_data *field_data)
145 ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
149 void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
150 struct ima_field_data *field_data)
152 ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
155 void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
156 struct ima_field_data *field_data)
158 ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
162 * ima_parse_buf() - Parses lengths and data from an input buffer
163 * @bufstartp: Buffer start address.
164 * @bufendp: Buffer end address.
165 * @bufcurp: Pointer to remaining (non-parsed) data.
166 * @maxfields: Length of fields array.
167 * @fields: Array containing lengths and pointers of parsed data.
168 * @curfields: Number of array items containing parsed data.
169 * @len_mask: Bitmap (if bit is set, data length should not be parsed).
170 * @enforce_mask: Check if curfields == maxfields and/or bufcurp == bufendp.
171 * @bufname: String identifier of the input buffer.
173 * Return: 0 on success, -EINVAL on error.
175 int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp,
176 int maxfields, struct ima_field_data *fields, int *curfields,
177 unsigned long *len_mask, int enforce_mask, char *bufname)
179 void *bufp = bufstartp;
182 for (i = 0; i < maxfields; i++) {
183 if (len_mask == NULL || !test_bit(i, len_mask)) {
184 if (bufp > (bufendp - sizeof(u32)))
187 fields[i].len = *(u32 *)bufp;
188 if (ima_canonical_fmt)
189 fields[i].len = le32_to_cpu(fields[i].len);
194 if (bufp > (bufendp - fields[i].len))
197 fields[i].data = bufp;
198 bufp += fields[i].len;
201 if ((enforce_mask & ENFORCE_FIELDS) && i != maxfields) {
202 pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n",
203 bufname, maxfields, i);
207 if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {
208 pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
209 bufname, bufendp, bufp);
222 static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
223 struct ima_field_data *field_data)
227 * - DATA_FMT_DIGEST: digest
228 * - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
229 * where <hash algo> is provided if the hash algoritm is not
232 u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
233 enum data_formats fmt = DATA_FMT_DIGEST;
236 if (hash_algo < HASH_ALGO__LAST) {
237 fmt = DATA_FMT_DIGEST_WITH_ALGO;
238 offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s",
239 hash_algo_name[hash_algo]);
240 buffer[offset] = ':';
245 memcpy(buffer + offset, digest, digestsize);
248 * If digest is NULL, the event being recorded is a violation.
249 * Make room for the digest by increasing the offset of
252 offset += IMA_DIGEST_SIZE;
254 return ima_write_template_field_data(buffer, offset + digestsize,
259 * This function writes the digest of an event (with size limit).
261 int ima_eventdigest_init(struct ima_event_data *event_data,
262 struct ima_field_data *field_data)
265 struct ima_digest_data hdr;
266 char digest[IMA_MAX_DIGEST_SIZE];
268 u8 *cur_digest = NULL;
269 u32 cur_digestsize = 0;
273 memset(&hash, 0, sizeof(hash));
275 if (event_data->violation) /* recording a violation. */
278 if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
279 cur_digest = event_data->iint->ima_hash->digest;
280 cur_digestsize = event_data->iint->ima_hash->length;
284 if (!event_data->file) /* missing info to re-calculate the digest */
287 inode = file_inode(event_data->file);
288 hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
289 ima_hash_algo : HASH_ALGO_SHA1;
290 result = ima_calc_file_hash(event_data->file, &hash.hdr);
292 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
293 event_data->filename, "collect_data",
294 "failed", result, 0);
297 cur_digest = hash.hdr.digest;
298 cur_digestsize = hash.hdr.length;
300 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
301 HASH_ALGO__LAST, field_data);
305 * This function writes the digest of an event (without size limit).
307 int ima_eventdigest_ng_init(struct ima_event_data *event_data,
308 struct ima_field_data *field_data)
310 u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
311 u32 cur_digestsize = 0;
313 if (event_data->violation) /* recording a violation. */
316 cur_digest = event_data->iint->ima_hash->digest;
317 cur_digestsize = event_data->iint->ima_hash->length;
319 hash_algo = event_data->iint->ima_hash->algo;
321 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
322 hash_algo, field_data);
325 static int ima_eventname_init_common(struct ima_event_data *event_data,
326 struct ima_field_data *field_data,
329 const char *cur_filename = NULL;
330 u32 cur_filename_len = 0;
332 BUG_ON(event_data->filename == NULL && event_data->file == NULL);
334 if (event_data->filename) {
335 cur_filename = event_data->filename;
336 cur_filename_len = strlen(event_data->filename);
338 if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
342 if (event_data->file) {
343 cur_filename = event_data->file->f_path.dentry->d_name.name;
344 cur_filename_len = strlen(cur_filename);
347 * Truncate filename if the latter is too long and
348 * the file descriptor is not available.
350 cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
352 return ima_write_template_field_data(cur_filename, cur_filename_len,
353 DATA_FMT_STRING, field_data);
357 * This function writes the name of an event (with size limit).
359 int ima_eventname_init(struct ima_event_data *event_data,
360 struct ima_field_data *field_data)
362 return ima_eventname_init_common(event_data, field_data, true);
366 * This function writes the name of an event (without size limit).
368 int ima_eventname_ng_init(struct ima_event_data *event_data,
369 struct ima_field_data *field_data)
371 return ima_eventname_init_common(event_data, field_data, false);
375 * ima_eventsig_init - include the file signature as part of the template data
377 int ima_eventsig_init(struct ima_event_data *event_data,
378 struct ima_field_data *field_data)
380 struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
382 if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
385 return ima_write_template_field_data(xattr_value, event_data->xattr_len,
386 DATA_FMT_HEX, field_data);