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.
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
)
30 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
)
42 if (datafmt
== DATA_FMT_STRING
)
45 buf
= kzalloc(buflen
, GFP_KERNEL
);
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
++)
64 field_data
->data
= buf
;
65 field_data
->len
= buflen
;
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
;
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' */
85 buflen
-= buf_ptr
- field_data
->data
;
90 ima_print_digest(m
, buf_ptr
, buflen
);
93 seq_printf(m
, "%s", buf_ptr
);
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
));
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
)
127 ima_show_template_data_ascii(m
, show
, datafmt
, field_data
);
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
);
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
);
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
;
185 for (i
= 0; i
< maxfields
; i
++) {
186 if (len_mask
== NULL
|| !test_bit(i
, len_mask
)) {
187 if (bufp
> (bufendp
- sizeof(u32
)))
190 fields
[i
].len
= *(u32
*)bufp
;
191 if (ima_canonical_fmt
)
192 fields
[i
].len
= le32_to_cpu(fields
[i
].len
);
197 if (bufp
> (bufendp
- fields
[i
].len
))
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
);
210 if ((enforce_mask
& ENFORCE_BUFEND
) && bufp
!= bufendp
) {
211 pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
212 bufname
, bufendp
, bufp
);
225 static int ima_eventdigest_init_common(u8
*digest
, u32 digestsize
, u8 hash_algo
,
226 struct ima_field_data
*field_data
)
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
235 u8 buffer
[CRYPTO_MAX_ALG_NAME
+ 2 + IMA_MAX_DIGEST_SIZE
] = { 0 };
236 enum data_formats fmt
= DATA_FMT_DIGEST
;
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
] = ':';
248 memcpy(buffer
+ offset
, digest
, digestsize
);
251 * If digest is NULL, the event being recorded is a violation.
252 * Make room for the digest by increasing the offset of
255 offset
+= IMA_DIGEST_SIZE
;
257 return ima_write_template_field_data(buffer
, offset
+ digestsize
,
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
)
268 struct ima_digest_data hdr
;
269 char digest
[IMA_MAX_DIGEST_SIZE
];
271 u8
*cur_digest
= NULL
;
272 u32 cur_digestsize
= 0;
276 memset(&hash
, 0, sizeof(hash
));
278 if (event_data
->violation
) /* recording a violation. */
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
;
287 if (!event_data
->file
) /* missing info to re-calculate the digest */
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
);
295 integrity_audit_msg(AUDIT_INTEGRITY_DATA
, inode
,
296 event_data
->filename
, "collect_data",
297 "failed", result
, 0);
300 cur_digest
= hash
.hdr
.digest
;
301 cur_digestsize
= hash
.hdr
.length
;
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. */
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
;
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
,
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
)
345 if (event_data
->file
) {
346 cur_filename
= event_data
->file
->f_path
.dentry
->d_name
.name
;
347 cur_filename_len
= strlen(cur_filename
);
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
;
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
))
388 return ima_write_template_field_data(xattr_value
, event_data
->xattr_len
,
389 DATA_FMT_HEX
, field_data
);