2 * Copyright (C) 2013 Politecnico di Torino, Italy
3 * TORSEC group -- http://security.polito.it
5 * Author: Roberto Sassu <roberto.sassu@polito.it>
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
12 * File: ima_template_lib.c
13 * Library of supported template fields.
15 #include <crypto/hash_info.h>
17 #include "ima_template_lib.h"
19 static bool ima_template_hash_algo_allowed(u8 algo)
21 if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
29 DATA_FMT_DIGEST_WITH_ALGO,
35 static int ima_write_template_field_data(const void *data, const u32 datalen,
36 enum data_formats datafmt,
37 struct ima_field_data *field_data)
43 case DATA_FMT_EVENT_NAME:
44 buflen = IMA_EVENT_NAME_LEN_MAX + 1;
53 buf = kzalloc(buflen, GFP_KERNEL);
57 memcpy(buf, data, datalen);
60 * Replace all space characters with underscore for event names and
61 * strings. This avoid that, during the parsing of a measurements list,
62 * filenames with spaces or that end with the suffix ' (deleted)' are
63 * split into multiple template fields (the space is the delimitator
64 * character for measurements lists in ASCII format).
66 if (datafmt == DATA_FMT_EVENT_NAME || datafmt == DATA_FMT_STRING) {
67 for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
72 field_data->data = buf;
73 field_data->len = buflen;
77 static void ima_show_template_data_ascii(struct seq_file *m,
78 enum ima_show_type show,
79 enum data_formats datafmt,
80 struct ima_field_data *field_data)
82 u8 *buf_ptr = field_data->data, buflen = field_data->len;
85 case DATA_FMT_DIGEST_WITH_ALGO:
86 buf_ptr = strnchr(field_data->data, buflen, ':');
87 if (buf_ptr != field_data->data)
88 seq_printf(m, "%s", field_data->data);
90 /* skip ':' and '\0' */
92 buflen -= buf_ptr - field_data->data;
97 ima_print_digest(m, buf_ptr, buflen);
100 seq_printf(m, "%s", buf_ptr);
107 static void ima_show_template_data_binary(struct seq_file *m,
108 enum ima_show_type show,
109 enum data_formats datafmt,
110 struct ima_field_data *field_data)
112 if (show != IMA_SHOW_BINARY_NO_FIELD_LEN)
113 ima_putc(m, &field_data->len, sizeof(u32));
115 if (!field_data->len)
118 ima_putc(m, field_data->data, field_data->len);
121 static void ima_show_template_field_data(struct seq_file *m,
122 enum ima_show_type show,
123 enum data_formats datafmt,
124 struct ima_field_data *field_data)
128 ima_show_template_data_ascii(m, show, datafmt, field_data);
130 case IMA_SHOW_BINARY:
131 case IMA_SHOW_BINARY_NO_FIELD_LEN:
132 ima_show_template_data_binary(m, show, datafmt, field_data);
139 void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
140 struct ima_field_data *field_data)
142 ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
145 void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
146 struct ima_field_data *field_data)
148 ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
152 void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
153 struct ima_field_data *field_data)
155 ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
158 void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
159 struct ima_field_data *field_data)
161 ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
164 static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
165 struct ima_field_data *field_data,
170 * - DATA_FMT_DIGEST: digest
171 * - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
172 * where <hash algo> is provided if the hash algoritm is not
175 u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
176 enum data_formats fmt = DATA_FMT_DIGEST;
180 fmt = DATA_FMT_DIGEST_WITH_ALGO;
181 if (hash_algo < HASH_ALGO__LAST)
182 offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1,
183 "%s", hash_algo_name[hash_algo]);
184 buffer[offset] = ':';
189 memcpy(buffer + offset, digest, digestsize);
192 * If digest is NULL, the event being recorded is a violation.
193 * Make room for the digest by increasing the offset of
196 offset += IMA_DIGEST_SIZE;
198 return ima_write_template_field_data(buffer, offset + digestsize,
203 * This function writes the digest of an event (with size limit).
205 int ima_eventdigest_init(struct integrity_iint_cache *iint, struct file *file,
206 const unsigned char *filename,
207 struct evm_ima_xattr_data *xattr_value, int xattr_len,
208 struct ima_field_data *field_data)
211 struct ima_digest_data hdr;
212 char digest[IMA_MAX_DIGEST_SIZE];
214 u8 *cur_digest = NULL;
215 u32 cur_digestsize = 0;
219 memset(&hash, 0, sizeof(hash));
221 if (!iint) /* recording a violation. */
224 if (ima_template_hash_algo_allowed(iint->ima_hash->algo)) {
225 cur_digest = iint->ima_hash->digest;
226 cur_digestsize = iint->ima_hash->length;
230 if (!file) /* missing info to re-calculate the digest */
233 inode = file_inode(file);
234 hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
235 ima_hash_algo : HASH_ALGO_SHA1;
236 result = ima_calc_file_hash(file, &hash.hdr);
238 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
239 filename, "collect_data",
240 "failed", result, 0);
243 cur_digest = hash.hdr.digest;
244 cur_digestsize = hash.hdr.length;
246 return ima_eventdigest_init_common(cur_digest, cur_digestsize, -1,
251 * This function writes the digest of an event (without size limit).
253 int ima_eventdigest_ng_init(struct integrity_iint_cache *iint,
254 struct file *file, const unsigned char *filename,
255 struct evm_ima_xattr_data *xattr_value,
256 int xattr_len, struct ima_field_data *field_data)
258 u8 *cur_digest = NULL, hash_algo = HASH_ALGO__LAST;
259 u32 cur_digestsize = 0;
261 /* If iint is NULL, we are recording a violation. */
265 cur_digest = iint->ima_hash->digest;
266 cur_digestsize = iint->ima_hash->length;
268 hash_algo = iint->ima_hash->algo;
270 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
271 hash_algo, field_data, false);
274 static int ima_eventname_init_common(struct integrity_iint_cache *iint,
276 const unsigned char *filename,
277 struct ima_field_data *field_data,
280 const char *cur_filename = NULL;
281 u32 cur_filename_len = 0;
282 enum data_formats fmt = size_limit ?
283 DATA_FMT_EVENT_NAME : DATA_FMT_STRING;
285 BUG_ON(filename == NULL && file == NULL);
288 cur_filename = filename;
289 cur_filename_len = strlen(filename);
291 if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
296 cur_filename = file->f_dentry->d_name.name;
297 cur_filename_len = strlen(cur_filename);
300 * Truncate filename if the latter is too long and
301 * the file descriptor is not available.
303 cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
305 return ima_write_template_field_data(cur_filename, cur_filename_len,
310 * This function writes the name of an event (with size limit).
312 int ima_eventname_init(struct integrity_iint_cache *iint, struct file *file,
313 const unsigned char *filename,
314 struct evm_ima_xattr_data *xattr_value, int xattr_len,
315 struct ima_field_data *field_data)
317 return ima_eventname_init_common(iint, file, filename,
322 * This function writes the name of an event (without size limit).
324 int ima_eventname_ng_init(struct integrity_iint_cache *iint, struct file *file,
325 const unsigned char *filename,
326 struct evm_ima_xattr_data *xattr_value, int xattr_len,
327 struct ima_field_data *field_data)
329 return ima_eventname_init_common(iint, file, filename,
334 * ima_eventsig_init - include the file signature as part of the template data
336 int ima_eventsig_init(struct integrity_iint_cache *iint, struct file *file,
337 const unsigned char *filename,
338 struct evm_ima_xattr_data *xattr_value, int xattr_len,
339 struct ima_field_data *field_data)
341 enum data_formats fmt = DATA_FMT_HEX;
344 if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
347 rc = ima_write_template_field_data(xattr_value, xattr_len, fmt,