blob: 2934e3d377f1e0c1c20e6d97e8651b19154569da [file] [log] [blame]
Roberto Sassu3ce1217d2013-06-07 12:16:30 +02001/*
2 * Copyright (C) 2013 Politecnico di Torino, Italy
3 * TORSEC group -- http://security.polito.it
4 *
5 * Author: Roberto Sassu <roberto.sassu@polito.it>
6 *
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
10 * License.
11 *
12 * File: ima_template_lib.c
13 * Library of supported template fields.
14 */
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020015#include <crypto/hash_info.h>
16
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020017#include "ima_template_lib.h"
18
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020019static bool ima_template_hash_algo_allowed(u8 algo)
20{
21 if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
22 return true;
23
24 return false;
25}
26
27enum data_formats {
28 DATA_FMT_DIGEST = 0,
29 DATA_FMT_DIGEST_WITH_ALGO,
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -040030 DATA_FMT_STRING,
31 DATA_FMT_HEX
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020032};
33
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020034static int ima_write_template_field_data(const void *data, const u32 datalen,
35 enum data_formats datafmt,
36 struct ima_field_data *field_data)
37{
38 u8 *buf, *buf_ptr;
Roberto Sassue3b64c22014-02-03 13:56:05 +010039 u32 buflen = datalen;
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020040
Roberto Sassue3b64c22014-02-03 13:56:05 +010041 if (datafmt == DATA_FMT_STRING)
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020042 buflen = datalen + 1;
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020043
44 buf = kzalloc(buflen, GFP_KERNEL);
45 if (!buf)
46 return -ENOMEM;
47
48 memcpy(buf, data, datalen);
49
50 /*
51 * Replace all space characters with underscore for event names and
52 * strings. This avoid that, during the parsing of a measurements list,
53 * filenames with spaces or that end with the suffix ' (deleted)' are
54 * split into multiple template fields (the space is the delimitator
55 * character for measurements lists in ASCII format).
56 */
Roberto Sassue3b64c22014-02-03 13:56:05 +010057 if (datafmt == DATA_FMT_STRING) {
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020058 for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
59 if (*buf_ptr == ' ')
60 *buf_ptr = '_';
61 }
62
63 field_data->data = buf;
64 field_data->len = buflen;
65 return 0;
66}
67
68static void ima_show_template_data_ascii(struct seq_file *m,
69 enum ima_show_type show,
70 enum data_formats datafmt,
71 struct ima_field_data *field_data)
72{
Mimi Zohar45b26132015-06-11 11:54:42 -040073 u8 *buf_ptr = field_data->data;
74 u32 buflen = field_data->len;
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020075
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020076 switch (datafmt) {
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020077 case DATA_FMT_DIGEST_WITH_ALGO:
78 buf_ptr = strnchr(field_data->data, buflen, ':');
79 if (buf_ptr != field_data->data)
80 seq_printf(m, "%s", field_data->data);
81
82 /* skip ':' and '\0' */
83 buf_ptr += 2;
84 buflen -= buf_ptr - field_data->data;
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020085 case DATA_FMT_DIGEST:
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -040086 case DATA_FMT_HEX:
87 if (!buflen)
88 break;
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020089 ima_print_digest(m, buf_ptr, buflen);
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020090 break;
91 case DATA_FMT_STRING:
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020092 seq_printf(m, "%s", buf_ptr);
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020093 break;
94 default:
95 break;
96 }
97}
98
99static void ima_show_template_data_binary(struct seq_file *m,
100 enum ima_show_type show,
101 enum data_formats datafmt,
102 struct ima_field_data *field_data)
103{
Roberto Sassuc019e302014-02-03 13:56:04 +0100104 u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
105 strlen(field_data->data) : field_data->len;
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100106
Roberto Sassuc019e302014-02-03 13:56:04 +0100107 if (show != IMA_SHOW_BINARY_NO_FIELD_LEN)
108 ima_putc(m, &len, sizeof(len));
109
110 if (!len)
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200111 return;
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100112
Roberto Sassuc019e302014-02-03 13:56:04 +0100113 ima_putc(m, field_data->data, len);
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200114}
115
116static void ima_show_template_field_data(struct seq_file *m,
117 enum ima_show_type show,
118 enum data_formats datafmt,
119 struct ima_field_data *field_data)
120{
121 switch (show) {
122 case IMA_SHOW_ASCII:
123 ima_show_template_data_ascii(m, show, datafmt, field_data);
124 break;
125 case IMA_SHOW_BINARY:
Roberto Sassu3e8e5502013-11-08 19:21:40 +0100126 case IMA_SHOW_BINARY_NO_FIELD_LEN:
Roberto Sassuc019e302014-02-03 13:56:04 +0100127 case IMA_SHOW_BINARY_OLD_STRING_FMT:
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200128 ima_show_template_data_binary(m, show, datafmt, field_data);
129 break;
130 default:
131 break;
132 }
133}
134
135void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
136 struct ima_field_data *field_data)
137{
138 ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
139}
140
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200141void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
142 struct ima_field_data *field_data)
143{
144 ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
145 field_data);
146}
147
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200148void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
149 struct ima_field_data *field_data)
150{
151 ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
152}
153
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -0400154void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
155 struct ima_field_data *field_data)
156{
157 ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
158}
159
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200160static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
Roberto Sassudcf4e392013-11-08 19:21:37 +0100161 struct ima_field_data *field_data)
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200162{
163 /*
164 * digest formats:
165 * - DATA_FMT_DIGEST: digest
166 * - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
167 * where <hash algo> is provided if the hash algoritm is not
168 * SHA1 or MD5
169 */
170 u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
171 enum data_formats fmt = DATA_FMT_DIGEST;
172 u32 offset = 0;
173
Roberto Sassudcf4e392013-11-08 19:21:37 +0100174 if (hash_algo < HASH_ALGO__LAST) {
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200175 fmt = DATA_FMT_DIGEST_WITH_ALGO;
Roberto Sassudcf4e392013-11-08 19:21:37 +0100176 offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s",
177 hash_algo_name[hash_algo]);
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200178 buffer[offset] = ':';
179 offset += 2;
180 }
181
182 if (digest)
183 memcpy(buffer + offset, digest, digestsize);
184 else
185 /*
186 * If digest is NULL, the event being recorded is a violation.
187 * Make room for the digest by increasing the offset of
188 * IMA_DIGEST_SIZE.
189 */
190 offset += IMA_DIGEST_SIZE;
191
192 return ima_write_template_field_data(buffer, offset + digestsize,
193 fmt, field_data);
194}
195
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200196/*
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200197 * This function writes the digest of an event (with size limit).
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200198 */
Roberto Sassu23b57412015-04-11 17:09:50 +0200199int ima_eventdigest_init(struct ima_event_data *event_data,
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200200 struct ima_field_data *field_data)
201{
202 struct {
203 struct ima_digest_data hdr;
204 char digest[IMA_MAX_DIGEST_SIZE];
205 } hash;
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200206 u8 *cur_digest = NULL;
207 u32 cur_digestsize = 0;
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200208 struct inode *inode;
209 int result;
210
211 memset(&hash, 0, sizeof(hash));
212
Roberto Sassu8d94eb92015-04-11 17:12:39 +0200213 if (event_data->violation) /* recording a violation. */
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200214 goto out;
215
Roberto Sassu23b57412015-04-11 17:09:50 +0200216 if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
217 cur_digest = event_data->iint->ima_hash->digest;
218 cur_digestsize = event_data->iint->ima_hash->length;
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200219 goto out;
220 }
221
Roberto Sassu23b57412015-04-11 17:09:50 +0200222 if (!event_data->file) /* missing info to re-calculate the digest */
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200223 return -EINVAL;
224
Roberto Sassu23b57412015-04-11 17:09:50 +0200225 inode = file_inode(event_data->file);
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200226 hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
227 ima_hash_algo : HASH_ALGO_SHA1;
Roberto Sassu23b57412015-04-11 17:09:50 +0200228 result = ima_calc_file_hash(event_data->file, &hash.hdr);
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200229 if (result) {
230 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
Roberto Sassu23b57412015-04-11 17:09:50 +0200231 event_data->filename, "collect_data",
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200232 "failed", result, 0);
233 return result;
234 }
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200235 cur_digest = hash.hdr.digest;
236 cur_digestsize = hash.hdr.length;
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200237out:
Roberto Sassu712a49b2013-11-08 19:21:36 +0100238 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
Roberto Sassudcf4e392013-11-08 19:21:37 +0100239 HASH_ALGO__LAST, field_data);
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200240}
241
242/*
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200243 * This function writes the digest of an event (without size limit).
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200244 */
Roberto Sassu23b57412015-04-11 17:09:50 +0200245int ima_eventdigest_ng_init(struct ima_event_data *event_data,
246 struct ima_field_data *field_data)
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200247{
Roberto Sassuc502c782013-11-08 19:21:35 +0100248 u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200249 u32 cur_digestsize = 0;
250
Roberto Sassu8d94eb92015-04-11 17:12:39 +0200251 if (event_data->violation) /* recording a violation. */
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200252 goto out;
253
Roberto Sassu23b57412015-04-11 17:09:50 +0200254 cur_digest = event_data->iint->ima_hash->digest;
255 cur_digestsize = event_data->iint->ima_hash->length;
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200256
Roberto Sassu23b57412015-04-11 17:09:50 +0200257 hash_algo = event_data->iint->ima_hash->algo;
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200258out:
259 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
Roberto Sassudcf4e392013-11-08 19:21:37 +0100260 hash_algo, field_data);
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200261}
262
Roberto Sassu23b57412015-04-11 17:09:50 +0200263static int ima_eventname_init_common(struct ima_event_data *event_data,
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200264 struct ima_field_data *field_data,
265 bool size_limit)
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200266{
267 const char *cur_filename = NULL;
268 u32 cur_filename_len = 0;
269
Roberto Sassu23b57412015-04-11 17:09:50 +0200270 BUG_ON(event_data->filename == NULL && event_data->file == NULL);
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200271
Roberto Sassu23b57412015-04-11 17:09:50 +0200272 if (event_data->filename) {
273 cur_filename = event_data->filename;
274 cur_filename_len = strlen(event_data->filename);
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200275
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200276 if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200277 goto out;
278 }
279
Roberto Sassu23b57412015-04-11 17:09:50 +0200280 if (event_data->file) {
281 cur_filename = event_data->file->f_path.dentry->d_name.name;
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200282 cur_filename_len = strlen(cur_filename);
283 } else
284 /*
285 * Truncate filename if the latter is too long and
286 * the file descriptor is not available.
287 */
288 cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
289out:
290 return ima_write_template_field_data(cur_filename, cur_filename_len,
Roberto Sassue3b64c22014-02-03 13:56:05 +0100291 DATA_FMT_STRING, field_data);
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200292}
293
294/*
295 * This function writes the name of an event (with size limit).
296 */
Roberto Sassu23b57412015-04-11 17:09:50 +0200297int ima_eventname_init(struct ima_event_data *event_data,
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200298 struct ima_field_data *field_data)
299{
Roberto Sassu23b57412015-04-11 17:09:50 +0200300 return ima_eventname_init_common(event_data, field_data, true);
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200301}
302
303/*
304 * This function writes the name of an event (without size limit).
305 */
Roberto Sassu23b57412015-04-11 17:09:50 +0200306int ima_eventname_ng_init(struct ima_event_data *event_data,
Roberto Sassu4d7aeee72013-06-07 12:16:32 +0200307 struct ima_field_data *field_data)
308{
Roberto Sassu23b57412015-04-11 17:09:50 +0200309 return ima_eventname_init_common(event_data, field_data, false);
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200310}
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -0400311
312/*
313 * ima_eventsig_init - include the file signature as part of the template data
314 */
Roberto Sassu23b57412015-04-11 17:09:50 +0200315int ima_eventsig_init(struct ima_event_data *event_data,
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -0400316 struct ima_field_data *field_data)
317{
318 enum data_formats fmt = DATA_FMT_HEX;
Roberto Sassu23b57412015-04-11 17:09:50 +0200319 struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
320 int xattr_len = event_data->xattr_len;
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -0400321 int rc = 0;
322
323 if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
324 goto out;
325
326 rc = ima_write_template_field_data(xattr_value, xattr_len, fmt,
327 field_data);
328out:
329 return rc;
330}