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
;
91 ima_print_digest(m
, buf_ptr
, buflen
);
94 seq_printf(m
, "%s", buf_ptr
);
101 static void ima_show_template_data_binary(struct seq_file
*m
,
102 enum ima_show_type show
,
103 enum data_formats datafmt
,
104 struct ima_field_data
*field_data
)
106 u32 len
= (show
== IMA_SHOW_BINARY_OLD_STRING_FMT
) ?
107 strlen(field_data
->data
) : field_data
->len
;
109 if (show
!= IMA_SHOW_BINARY_NO_FIELD_LEN
) {
110 u32 field_len
= !ima_canonical_fmt
? len
: cpu_to_le32(len
);
112 ima_putc(m
, &field_len
, sizeof(field_len
));
118 ima_putc(m
, field_data
->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 case IMA_SHOW_BINARY_OLD_STRING_FMT
:
133 ima_show_template_data_binary(m
, show
, datafmt
, field_data
);
140 void ima_show_template_digest(struct seq_file
*m
, enum ima_show_type show
,
141 struct ima_field_data
*field_data
)
143 ima_show_template_field_data(m
, show
, DATA_FMT_DIGEST
, field_data
);
146 void ima_show_template_digest_ng(struct seq_file
*m
, enum ima_show_type show
,
147 struct ima_field_data
*field_data
)
149 ima_show_template_field_data(m
, show
, DATA_FMT_DIGEST_WITH_ALGO
,
153 void ima_show_template_string(struct seq_file
*m
, enum ima_show_type show
,
154 struct ima_field_data
*field_data
)
156 ima_show_template_field_data(m
, show
, DATA_FMT_STRING
, field_data
);
159 void ima_show_template_sig(struct seq_file
*m
, enum ima_show_type show
,
160 struct ima_field_data
*field_data
)
162 ima_show_template_field_data(m
, show
, DATA_FMT_HEX
, field_data
);
166 * ima_parse_buf() - Parses lengths and data from an input buffer
167 * @bufstartp: Buffer start address.
168 * @bufendp: Buffer end address.
169 * @bufcurp: Pointer to remaining (non-parsed) data.
170 * @maxfields: Length of fields array.
171 * @fields: Array containing lengths and pointers of parsed data.
172 * @curfields: Number of array items containing parsed data.
173 * @len_mask: Bitmap (if bit is set, data length should not be parsed).
174 * @enforce_mask: Check if curfields == maxfields and/or bufcurp == bufendp.
175 * @bufname: String identifier of the input buffer.
177 * Return: 0 on success, -EINVAL on error.
179 int ima_parse_buf(void *bufstartp
, void *bufendp
, void **bufcurp
,
180 int maxfields
, struct ima_field_data
*fields
, int *curfields
,
181 unsigned long *len_mask
, int enforce_mask
, char *bufname
)
183 void *bufp
= bufstartp
;
186 for (i
= 0; i
< maxfields
; i
++) {
187 if (len_mask
== NULL
|| !test_bit(i
, len_mask
)) {
188 if (bufp
> (bufendp
- sizeof(u32
)))
191 fields
[i
].len
= *(u32
*)bufp
;
192 if (ima_canonical_fmt
)
193 fields
[i
].len
= le32_to_cpu(fields
[i
].len
);
198 if (bufp
> (bufendp
- fields
[i
].len
))
201 fields
[i
].data
= bufp
;
202 bufp
+= fields
[i
].len
;
205 if ((enforce_mask
& ENFORCE_FIELDS
) && i
!= maxfields
) {
206 pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n",
207 bufname
, maxfields
, i
);
211 if ((enforce_mask
& ENFORCE_BUFEND
) && bufp
!= bufendp
) {
212 pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
213 bufname
, bufendp
, bufp
);
226 static int ima_eventdigest_init_common(u8
*digest
, u32 digestsize
, u8 hash_algo
,
227 struct ima_field_data
*field_data
)
231 * - DATA_FMT_DIGEST: digest
232 * - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
233 * where <hash algo> is provided if the hash algoritm is not
236 u8 buffer
[CRYPTO_MAX_ALG_NAME
+ 2 + IMA_MAX_DIGEST_SIZE
] = { 0 };
237 enum data_formats fmt
= DATA_FMT_DIGEST
;
240 if (hash_algo
< HASH_ALGO__LAST
) {
241 fmt
= DATA_FMT_DIGEST_WITH_ALGO
;
242 offset
+= snprintf(buffer
, CRYPTO_MAX_ALG_NAME
+ 1, "%s",
243 hash_algo_name
[hash_algo
]);
244 buffer
[offset
] = ':';
249 memcpy(buffer
+ offset
, digest
, digestsize
);
252 * If digest is NULL, the event being recorded is a violation.
253 * Make room for the digest by increasing the offset of
256 offset
+= IMA_DIGEST_SIZE
;
258 return ima_write_template_field_data(buffer
, offset
+ digestsize
,
263 * This function writes the digest of an event (with size limit).
265 int ima_eventdigest_init(struct ima_event_data
*event_data
,
266 struct ima_field_data
*field_data
)
269 struct ima_digest_data hdr
;
270 char digest
[IMA_MAX_DIGEST_SIZE
];
272 u8
*cur_digest
= NULL
;
273 u32 cur_digestsize
= 0;
277 memset(&hash
, 0, sizeof(hash
));
279 if (event_data
->violation
) /* recording a violation. */
282 if (ima_template_hash_algo_allowed(event_data
->iint
->ima_hash
->algo
)) {
283 cur_digest
= event_data
->iint
->ima_hash
->digest
;
284 cur_digestsize
= event_data
->iint
->ima_hash
->length
;
288 if (!event_data
->file
) /* missing info to re-calculate the digest */
291 inode
= file_inode(event_data
->file
);
292 hash
.hdr
.algo
= ima_template_hash_algo_allowed(ima_hash_algo
) ?
293 ima_hash_algo
: HASH_ALGO_SHA1
;
294 result
= ima_calc_file_hash(event_data
->file
, &hash
.hdr
);
296 integrity_audit_msg(AUDIT_INTEGRITY_DATA
, inode
,
297 event_data
->filename
, "collect_data",
298 "failed", result
, 0);
301 cur_digest
= hash
.hdr
.digest
;
302 cur_digestsize
= hash
.hdr
.length
;
304 return ima_eventdigest_init_common(cur_digest
, cur_digestsize
,
305 HASH_ALGO__LAST
, field_data
);
309 * This function writes the digest of an event (without size limit).
311 int ima_eventdigest_ng_init(struct ima_event_data
*event_data
,
312 struct ima_field_data
*field_data
)
314 u8
*cur_digest
= NULL
, hash_algo
= HASH_ALGO_SHA1
;
315 u32 cur_digestsize
= 0;
317 if (event_data
->violation
) /* recording a violation. */
320 cur_digest
= event_data
->iint
->ima_hash
->digest
;
321 cur_digestsize
= event_data
->iint
->ima_hash
->length
;
323 hash_algo
= event_data
->iint
->ima_hash
->algo
;
325 return ima_eventdigest_init_common(cur_digest
, cur_digestsize
,
326 hash_algo
, field_data
);
329 static int ima_eventname_init_common(struct ima_event_data
*event_data
,
330 struct ima_field_data
*field_data
,
333 const char *cur_filename
= NULL
;
334 u32 cur_filename_len
= 0;
336 BUG_ON(event_data
->filename
== NULL
&& event_data
->file
== NULL
);
338 if (event_data
->filename
) {
339 cur_filename
= event_data
->filename
;
340 cur_filename_len
= strlen(event_data
->filename
);
342 if (!size_limit
|| cur_filename_len
<= IMA_EVENT_NAME_LEN_MAX
)
346 if (event_data
->file
) {
347 cur_filename
= event_data
->file
->f_path
.dentry
->d_name
.name
;
348 cur_filename_len
= strlen(cur_filename
);
351 * Truncate filename if the latter is too long and
352 * the file descriptor is not available.
354 cur_filename_len
= IMA_EVENT_NAME_LEN_MAX
;
356 return ima_write_template_field_data(cur_filename
, cur_filename_len
,
357 DATA_FMT_STRING
, field_data
);
361 * This function writes the name of an event (with size limit).
363 int ima_eventname_init(struct ima_event_data
*event_data
,
364 struct ima_field_data
*field_data
)
366 return ima_eventname_init_common(event_data
, field_data
, true);
370 * This function writes the name of an event (without size limit).
372 int ima_eventname_ng_init(struct ima_event_data
*event_data
,
373 struct ima_field_data
*field_data
)
375 return ima_eventname_init_common(event_data
, field_data
, false);
379 * ima_eventsig_init - include the file signature as part of the template data
381 int ima_eventsig_init(struct ima_event_data
*event_data
,
382 struct ima_field_data
*field_data
)
384 struct evm_ima_xattr_data
*xattr_value
= event_data
->xattr_value
;
386 if ((!xattr_value
) || (xattr_value
->type
!= EVM_IMA_XATTR_DIGSIG
))
389 return ima_write_template_field_data(xattr_value
, event_data
->xattr_len
,
390 DATA_FMT_HEX
, field_data
);