Merge tag 'reset-for-v5.3' of git://git.pengutronix.de/git/pza/linux into arm/drivers
[sfrench/cifs-2.6.git] / security / integrity / ima / ima_template_lib.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Politecnico di Torino, Italy
4  *                    TORSEC group -- http://security.polito.it
5  *
6  * Author: Roberto Sassu <roberto.sassu@polito.it>
7  *
8  * File: ima_template_lib.c
9  *      Library of supported template fields.
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include "ima_template_lib.h"
15
16 static bool ima_template_hash_algo_allowed(u8 algo)
17 {
18         if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
19                 return true;
20
21         return false;
22 }
23
24 enum data_formats {
25         DATA_FMT_DIGEST = 0,
26         DATA_FMT_DIGEST_WITH_ALGO,
27         DATA_FMT_STRING,
28         DATA_FMT_HEX
29 };
30
31 static int ima_write_template_field_data(const void *data, const u32 datalen,
32                                          enum data_formats datafmt,
33                                          struct ima_field_data *field_data)
34 {
35         u8 *buf, *buf_ptr;
36         u32 buflen = datalen;
37
38         if (datafmt == DATA_FMT_STRING)
39                 buflen = datalen + 1;
40
41         buf = kzalloc(buflen, GFP_KERNEL);
42         if (!buf)
43                 return -ENOMEM;
44
45         memcpy(buf, data, datalen);
46
47         /*
48          * Replace all space characters with underscore for event names and
49          * strings. This avoid that, during the parsing of a measurements list,
50          * filenames with spaces or that end with the suffix ' (deleted)' are
51          * split into multiple template fields (the space is the delimitator
52          * character for measurements lists in ASCII format).
53          */
54         if (datafmt == DATA_FMT_STRING) {
55                 for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
56                         if (*buf_ptr == ' ')
57                                 *buf_ptr = '_';
58         }
59
60         field_data->data = buf;
61         field_data->len = buflen;
62         return 0;
63 }
64
65 static void ima_show_template_data_ascii(struct seq_file *m,
66                                          enum ima_show_type show,
67                                          enum data_formats datafmt,
68                                          struct ima_field_data *field_data)
69 {
70         u8 *buf_ptr = field_data->data;
71         u32 buflen = field_data->len;
72
73         switch (datafmt) {
74         case DATA_FMT_DIGEST_WITH_ALGO:
75                 buf_ptr = strnchr(field_data->data, buflen, ':');
76                 if (buf_ptr != field_data->data)
77                         seq_printf(m, "%s", field_data->data);
78
79                 /* skip ':' and '\0' */
80                 buf_ptr += 2;
81                 buflen -= buf_ptr - field_data->data;
82                 /* fall through */
83         case DATA_FMT_DIGEST:
84         case DATA_FMT_HEX:
85                 if (!buflen)
86                         break;
87                 ima_print_digest(m, buf_ptr, buflen);
88                 break;
89         case DATA_FMT_STRING:
90                 seq_printf(m, "%s", buf_ptr);
91                 break;
92         default:
93                 break;
94         }
95 }
96
97 static void ima_show_template_data_binary(struct seq_file *m,
98                                           enum ima_show_type show,
99                                           enum data_formats datafmt,
100                                           struct ima_field_data *field_data)
101 {
102         u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
103             strlen(field_data->data) : field_data->len;
104
105         if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
106                 u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len);
107
108                 ima_putc(m, &field_len, sizeof(field_len));
109         }
110
111         if (!len)
112                 return;
113
114         ima_putc(m, field_data->data, len);
115 }
116
117 static void ima_show_template_field_data(struct seq_file *m,
118                                          enum ima_show_type show,
119                                          enum data_formats datafmt,
120                                          struct ima_field_data *field_data)
121 {
122         switch (show) {
123         case IMA_SHOW_ASCII:
124                 ima_show_template_data_ascii(m, show, datafmt, field_data);
125                 break;
126         case IMA_SHOW_BINARY:
127         case IMA_SHOW_BINARY_NO_FIELD_LEN:
128         case IMA_SHOW_BINARY_OLD_STRING_FMT:
129                 ima_show_template_data_binary(m, show, datafmt, field_data);
130                 break;
131         default:
132                 break;
133         }
134 }
135
136 void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
137                               struct ima_field_data *field_data)
138 {
139         ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
140 }
141
142 void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
143                                  struct ima_field_data *field_data)
144 {
145         ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
146                                      field_data);
147 }
148
149 void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
150                               struct ima_field_data *field_data)
151 {
152         ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
153 }
154
155 void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
156                            struct ima_field_data *field_data)
157 {
158         ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
159 }
160
161 /**
162  * ima_parse_buf() - Parses lengths and data from an input buffer
163  * @bufstartp:       Buffer start address.
164  * @bufendp:         Buffer end address.
165  * @bufcurp:         Pointer to remaining (non-parsed) data.
166  * @maxfields:       Length of fields array.
167  * @fields:          Array containing lengths and pointers of parsed data.
168  * @curfields:       Number of array items containing parsed data.
169  * @len_mask:        Bitmap (if bit is set, data length should not be parsed).
170  * @enforce_mask:    Check if curfields == maxfields and/or bufcurp == bufendp.
171  * @bufname:         String identifier of the input buffer.
172  *
173  * Return: 0 on success, -EINVAL on error.
174  */
175 int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp,
176                   int maxfields, struct ima_field_data *fields, int *curfields,
177                   unsigned long *len_mask, int enforce_mask, char *bufname)
178 {
179         void *bufp = bufstartp;
180         int i;
181
182         for (i = 0; i < maxfields; i++) {
183                 if (len_mask == NULL || !test_bit(i, len_mask)) {
184                         if (bufp > (bufendp - sizeof(u32)))
185                                 break;
186
187                         fields[i].len = *(u32 *)bufp;
188                         if (ima_canonical_fmt)
189                                 fields[i].len = le32_to_cpu(fields[i].len);
190
191                         bufp += sizeof(u32);
192                 }
193
194                 if (bufp > (bufendp - fields[i].len))
195                         break;
196
197                 fields[i].data = bufp;
198                 bufp += fields[i].len;
199         }
200
201         if ((enforce_mask & ENFORCE_FIELDS) && i != maxfields) {
202                 pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n",
203                        bufname, maxfields, i);
204                 return -EINVAL;
205         }
206
207         if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {
208                 pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
209                        bufname, bufendp, bufp);
210                 return -EINVAL;
211         }
212
213         if (curfields)
214                 *curfields = i;
215
216         if (bufcurp)
217                 *bufcurp = bufp;
218
219         return 0;
220 }
221
222 static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
223                                        struct ima_field_data *field_data)
224 {
225         /*
226          * digest formats:
227          *  - DATA_FMT_DIGEST: digest
228          *  - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
229          *    where <hash algo> is provided if the hash algoritm is not
230          *    SHA1 or MD5
231          */
232         u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
233         enum data_formats fmt = DATA_FMT_DIGEST;
234         u32 offset = 0;
235
236         if (hash_algo < HASH_ALGO__LAST) {
237                 fmt = DATA_FMT_DIGEST_WITH_ALGO;
238                 offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s",
239                                    hash_algo_name[hash_algo]);
240                 buffer[offset] = ':';
241                 offset += 2;
242         }
243
244         if (digest)
245                 memcpy(buffer + offset, digest, digestsize);
246         else
247                 /*
248                  * If digest is NULL, the event being recorded is a violation.
249                  * Make room for the digest by increasing the offset of
250                  * IMA_DIGEST_SIZE.
251                  */
252                 offset += IMA_DIGEST_SIZE;
253
254         return ima_write_template_field_data(buffer, offset + digestsize,
255                                              fmt, field_data);
256 }
257
258 /*
259  * This function writes the digest of an event (with size limit).
260  */
261 int ima_eventdigest_init(struct ima_event_data *event_data,
262                          struct ima_field_data *field_data)
263 {
264         struct {
265                 struct ima_digest_data hdr;
266                 char digest[IMA_MAX_DIGEST_SIZE];
267         } hash;
268         u8 *cur_digest = NULL;
269         u32 cur_digestsize = 0;
270         struct inode *inode;
271         int result;
272
273         memset(&hash, 0, sizeof(hash));
274
275         if (event_data->violation)      /* recording a violation. */
276                 goto out;
277
278         if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
279                 cur_digest = event_data->iint->ima_hash->digest;
280                 cur_digestsize = event_data->iint->ima_hash->length;
281                 goto out;
282         }
283
284         if (!event_data->file)  /* missing info to re-calculate the digest */
285                 return -EINVAL;
286
287         inode = file_inode(event_data->file);
288         hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
289             ima_hash_algo : HASH_ALGO_SHA1;
290         result = ima_calc_file_hash(event_data->file, &hash.hdr);
291         if (result) {
292                 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
293                                     event_data->filename, "collect_data",
294                                     "failed", result, 0);
295                 return result;
296         }
297         cur_digest = hash.hdr.digest;
298         cur_digestsize = hash.hdr.length;
299 out:
300         return ima_eventdigest_init_common(cur_digest, cur_digestsize,
301                                            HASH_ALGO__LAST, field_data);
302 }
303
304 /*
305  * This function writes the digest of an event (without size limit).
306  */
307 int ima_eventdigest_ng_init(struct ima_event_data *event_data,
308                             struct ima_field_data *field_data)
309 {
310         u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
311         u32 cur_digestsize = 0;
312
313         if (event_data->violation)      /* recording a violation. */
314                 goto out;
315
316         cur_digest = event_data->iint->ima_hash->digest;
317         cur_digestsize = event_data->iint->ima_hash->length;
318
319         hash_algo = event_data->iint->ima_hash->algo;
320 out:
321         return ima_eventdigest_init_common(cur_digest, cur_digestsize,
322                                            hash_algo, field_data);
323 }
324
325 static int ima_eventname_init_common(struct ima_event_data *event_data,
326                                      struct ima_field_data *field_data,
327                                      bool size_limit)
328 {
329         const char *cur_filename = NULL;
330         u32 cur_filename_len = 0;
331
332         BUG_ON(event_data->filename == NULL && event_data->file == NULL);
333
334         if (event_data->filename) {
335                 cur_filename = event_data->filename;
336                 cur_filename_len = strlen(event_data->filename);
337
338                 if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
339                         goto out;
340         }
341
342         if (event_data->file) {
343                 cur_filename = event_data->file->f_path.dentry->d_name.name;
344                 cur_filename_len = strlen(cur_filename);
345         } else
346                 /*
347                  * Truncate filename if the latter is too long and
348                  * the file descriptor is not available.
349                  */
350                 cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
351 out:
352         return ima_write_template_field_data(cur_filename, cur_filename_len,
353                                              DATA_FMT_STRING, field_data);
354 }
355
356 /*
357  * This function writes the name of an event (with size limit).
358  */
359 int ima_eventname_init(struct ima_event_data *event_data,
360                        struct ima_field_data *field_data)
361 {
362         return ima_eventname_init_common(event_data, field_data, true);
363 }
364
365 /*
366  * This function writes the name of an event (without size limit).
367  */
368 int ima_eventname_ng_init(struct ima_event_data *event_data,
369                           struct ima_field_data *field_data)
370 {
371         return ima_eventname_init_common(event_data, field_data, false);
372 }
373
374 /*
375  *  ima_eventsig_init - include the file signature as part of the template data
376  */
377 int ima_eventsig_init(struct ima_event_data *event_data,
378                       struct ima_field_data *field_data)
379 {
380         struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
381
382         if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
383                 return 0;
384
385         return ima_write_template_field_data(xattr_value, event_data->xattr_len,
386                                              DATA_FMT_HEX, field_data);
387 }