drm/i915: fix port checks for MST support on gen >= 11
[linux/fpc-iii.git] / security / integrity / ima / ima_template_lib.c
blob43752002c2223ca0315237b3dc2bf5df9ac5c4c0
1 /*
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
10 * License.
12 * File: ima_template_lib.c
13 * Library of supported template fields.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include "ima_template_lib.h"
20 static bool ima_template_hash_algo_allowed(u8 algo)
22 if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
23 return true;
25 return false;
28 enum data_formats {
29 DATA_FMT_DIGEST = 0,
30 DATA_FMT_DIGEST_WITH_ALGO,
31 DATA_FMT_STRING,
32 DATA_FMT_HEX
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)
39 u8 *buf, *buf_ptr;
40 u32 buflen = datalen;
42 if (datafmt == DATA_FMT_STRING)
43 buflen = datalen + 1;
45 buf = kzalloc(buflen, GFP_KERNEL);
46 if (!buf)
47 return -ENOMEM;
49 memcpy(buf, data, datalen);
52 * Replace all space characters with underscore for event names and
53 * strings. This avoid that, during the parsing of a measurements list,
54 * filenames with spaces or that end with the suffix ' (deleted)' are
55 * split into multiple template fields (the space is the delimitator
56 * character for measurements lists in ASCII format).
58 if (datafmt == DATA_FMT_STRING) {
59 for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
60 if (*buf_ptr == ' ')
61 *buf_ptr = '_';
64 field_data->data = buf;
65 field_data->len = buflen;
66 return 0;
69 static void ima_show_template_data_ascii(struct seq_file *m,
70 enum ima_show_type show,
71 enum data_formats datafmt,
72 struct ima_field_data *field_data)
74 u8 *buf_ptr = field_data->data;
75 u32 buflen = field_data->len;
77 switch (datafmt) {
78 case DATA_FMT_DIGEST_WITH_ALGO:
79 buf_ptr = strnchr(field_data->data, buflen, ':');
80 if (buf_ptr != field_data->data)
81 seq_printf(m, "%s", field_data->data);
83 /* skip ':' and '\0' */
84 buf_ptr += 2;
85 buflen -= buf_ptr - field_data->data;
86 case DATA_FMT_DIGEST:
87 case DATA_FMT_HEX:
88 if (!buflen)
89 break;
90 ima_print_digest(m, buf_ptr, buflen);
91 break;
92 case DATA_FMT_STRING:
93 seq_printf(m, "%s", buf_ptr);
94 break;
95 default:
96 break;
100 static void ima_show_template_data_binary(struct seq_file *m,
101 enum ima_show_type show,
102 enum data_formats datafmt,
103 struct ima_field_data *field_data)
105 u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
106 strlen(field_data->data) : field_data->len;
108 if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
109 u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len);
111 ima_putc(m, &field_len, sizeof(field_len));
114 if (!len)
115 return;
117 ima_putc(m, field_data->data, len);
120 static void ima_show_template_field_data(struct seq_file *m,
121 enum ima_show_type show,
122 enum data_formats datafmt,
123 struct ima_field_data *field_data)
125 switch (show) {
126 case IMA_SHOW_ASCII:
127 ima_show_template_data_ascii(m, show, datafmt, field_data);
128 break;
129 case IMA_SHOW_BINARY:
130 case IMA_SHOW_BINARY_NO_FIELD_LEN:
131 case IMA_SHOW_BINARY_OLD_STRING_FMT:
132 ima_show_template_data_binary(m, show, datafmt, field_data);
133 break;
134 default:
135 break;
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,
149 field_data);
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);
165 * ima_parse_buf() - Parses lengths and data from an input buffer
166 * @bufstartp: Buffer start address.
167 * @bufendp: Buffer end address.
168 * @bufcurp: Pointer to remaining (non-parsed) data.
169 * @maxfields: Length of fields array.
170 * @fields: Array containing lengths and pointers of parsed data.
171 * @curfields: Number of array items containing parsed data.
172 * @len_mask: Bitmap (if bit is set, data length should not be parsed).
173 * @enforce_mask: Check if curfields == maxfields and/or bufcurp == bufendp.
174 * @bufname: String identifier of the input buffer.
176 * Return: 0 on success, -EINVAL on error.
178 int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp,
179 int maxfields, struct ima_field_data *fields, int *curfields,
180 unsigned long *len_mask, int enforce_mask, char *bufname)
182 void *bufp = bufstartp;
183 int i;
185 for (i = 0; i < maxfields; i++) {
186 if (len_mask == NULL || !test_bit(i, len_mask)) {
187 if (bufp > (bufendp - sizeof(u32)))
188 break;
190 fields[i].len = *(u32 *)bufp;
191 if (ima_canonical_fmt)
192 fields[i].len = le32_to_cpu(fields[i].len);
194 bufp += sizeof(u32);
197 if (bufp > (bufendp - fields[i].len))
198 break;
200 fields[i].data = bufp;
201 bufp += fields[i].len;
204 if ((enforce_mask & ENFORCE_FIELDS) && i != maxfields) {
205 pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n",
206 bufname, maxfields, i);
207 return -EINVAL;
210 if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {
211 pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
212 bufname, bufendp, bufp);
213 return -EINVAL;
216 if (curfields)
217 *curfields = i;
219 if (bufcurp)
220 *bufcurp = bufp;
222 return 0;
225 static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
226 struct ima_field_data *field_data)
229 * digest formats:
230 * - DATA_FMT_DIGEST: digest
231 * - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
232 * where <hash algo> is provided if the hash algoritm is not
233 * SHA1 or MD5
235 u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
236 enum data_formats fmt = DATA_FMT_DIGEST;
237 u32 offset = 0;
239 if (hash_algo < HASH_ALGO__LAST) {
240 fmt = DATA_FMT_DIGEST_WITH_ALGO;
241 offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s",
242 hash_algo_name[hash_algo]);
243 buffer[offset] = ':';
244 offset += 2;
247 if (digest)
248 memcpy(buffer + offset, digest, digestsize);
249 else
251 * If digest is NULL, the event being recorded is a violation.
252 * Make room for the digest by increasing the offset of
253 * IMA_DIGEST_SIZE.
255 offset += IMA_DIGEST_SIZE;
257 return ima_write_template_field_data(buffer, offset + digestsize,
258 fmt, field_data);
262 * This function writes the digest of an event (with size limit).
264 int ima_eventdigest_init(struct ima_event_data *event_data,
265 struct ima_field_data *field_data)
267 struct {
268 struct ima_digest_data hdr;
269 char digest[IMA_MAX_DIGEST_SIZE];
270 } hash;
271 u8 *cur_digest = NULL;
272 u32 cur_digestsize = 0;
273 struct inode *inode;
274 int result;
276 memset(&hash, 0, sizeof(hash));
278 if (event_data->violation) /* recording a violation. */
279 goto out;
281 if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
282 cur_digest = event_data->iint->ima_hash->digest;
283 cur_digestsize = event_data->iint->ima_hash->length;
284 goto out;
287 if (!event_data->file) /* missing info to re-calculate the digest */
288 return -EINVAL;
290 inode = file_inode(event_data->file);
291 hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
292 ima_hash_algo : HASH_ALGO_SHA1;
293 result = ima_calc_file_hash(event_data->file, &hash.hdr);
294 if (result) {
295 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
296 event_data->filename, "collect_data",
297 "failed", result, 0);
298 return result;
300 cur_digest = hash.hdr.digest;
301 cur_digestsize = hash.hdr.length;
302 out:
303 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
304 HASH_ALGO__LAST, field_data);
308 * This function writes the digest of an event (without size limit).
310 int ima_eventdigest_ng_init(struct ima_event_data *event_data,
311 struct ima_field_data *field_data)
313 u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
314 u32 cur_digestsize = 0;
316 if (event_data->violation) /* recording a violation. */
317 goto out;
319 cur_digest = event_data->iint->ima_hash->digest;
320 cur_digestsize = event_data->iint->ima_hash->length;
322 hash_algo = event_data->iint->ima_hash->algo;
323 out:
324 return ima_eventdigest_init_common(cur_digest, cur_digestsize,
325 hash_algo, field_data);
328 static int ima_eventname_init_common(struct ima_event_data *event_data,
329 struct ima_field_data *field_data,
330 bool size_limit)
332 const char *cur_filename = NULL;
333 u32 cur_filename_len = 0;
335 BUG_ON(event_data->filename == NULL && event_data->file == NULL);
337 if (event_data->filename) {
338 cur_filename = event_data->filename;
339 cur_filename_len = strlen(event_data->filename);
341 if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
342 goto out;
345 if (event_data->file) {
346 cur_filename = event_data->file->f_path.dentry->d_name.name;
347 cur_filename_len = strlen(cur_filename);
348 } else
350 * Truncate filename if the latter is too long and
351 * the file descriptor is not available.
353 cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
354 out:
355 return ima_write_template_field_data(cur_filename, cur_filename_len,
356 DATA_FMT_STRING, field_data);
360 * This function writes the name of an event (with size limit).
362 int ima_eventname_init(struct ima_event_data *event_data,
363 struct ima_field_data *field_data)
365 return ima_eventname_init_common(event_data, field_data, true);
369 * This function writes the name of an event (without size limit).
371 int ima_eventname_ng_init(struct ima_event_data *event_data,
372 struct ima_field_data *field_data)
374 return ima_eventname_init_common(event_data, field_data, false);
378 * ima_eventsig_init - include the file signature as part of the template data
380 int ima_eventsig_init(struct ima_event_data *event_data,
381 struct ima_field_data *field_data)
383 struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
385 if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
386 return 0;
388 return ima_write_template_field_data(xattr_value, event_data->xattr_len,
389 DATA_FMT_HEX, field_data);