Linux-libre 5.0.14-gnu
[librecmc/linux-libre.git] / security / integrity / ima / ima_template_lib.c
1 /*
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  */
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18 #include "ima_template_lib.h"
19
20 static bool ima_template_hash_algo_allowed(u8 algo)
21 {
22         if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
23                 return true;
24
25         return false;
26 }
27
28 enum data_formats {
29         DATA_FMT_DIGEST = 0,
30         DATA_FMT_DIGEST_WITH_ALGO,
31         DATA_FMT_STRING,
32         DATA_FMT_HEX
33 };
34
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)
38 {
39         u8 *buf, *buf_ptr;
40         u32 buflen = datalen;
41
42         if (datafmt == DATA_FMT_STRING)
43                 buflen = datalen + 1;
44
45         buf = kzalloc(buflen, GFP_KERNEL);
46         if (!buf)
47                 return -ENOMEM;
48
49         memcpy(buf, data, datalen);
50
51         /*
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).
57          */
58         if (datafmt == DATA_FMT_STRING) {
59                 for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
60                         if (*buf_ptr == ' ')
61                                 *buf_ptr = '_';
62         }
63
64         field_data->data = buf;
65         field_data->len = buflen;
66         return 0;
67 }
68
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)
73 {
74         u8 *buf_ptr = field_data->data;
75         u32 buflen = field_data->len;
76
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);
82
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;
97         }
98 }
99
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)
104 {
105         u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
106             strlen(field_data->data) : field_data->len;
107
108         if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
109                 u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len);
110
111                 ima_putc(m, &field_len, sizeof(field_len));
112         }
113
114         if (!len)
115                 return;
116
117         ima_putc(m, field_data->data, len);
118 }
119
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)
124 {
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;
136         }
137 }
138
139 void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
140                               struct ima_field_data *field_data)
141 {
142         ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
143 }
144
145 void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
146                                  struct ima_field_data *field_data)
147 {
148         ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
149                                      field_data);
150 }
151
152 void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
153                               struct ima_field_data *field_data)
154 {
155         ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
156 }
157
158 void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
159                            struct ima_field_data *field_data)
160 {
161         ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
162 }
163
164 /**
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.
175  *
176  * Return: 0 on success, -EINVAL on error.
177  */
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)
181 {
182         void *bufp = bufstartp;
183         int i;
184
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;
189
190                         fields[i].len = *(u32 *)bufp;
191                         if (ima_canonical_fmt)
192                                 fields[i].len = le32_to_cpu(fields[i].len);
193
194                         bufp += sizeof(u32);
195                 }
196
197                 if (bufp > (bufendp - fields[i].len))
198                         break;
199
200                 fields[i].data = bufp;
201                 bufp += fields[i].len;
202         }
203
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;
208         }
209
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;
214         }
215
216         if (curfields)
217                 *curfields = i;
218
219         if (bufcurp)
220                 *bufcurp = bufp;
221
222         return 0;
223 }
224
225 static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
226                                        struct ima_field_data *field_data)
227 {
228         /*
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
234          */
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;
238
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;
245         }
246
247         if (digest)
248                 memcpy(buffer + offset, digest, digestsize);
249         else
250                 /*
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.
254                  */
255                 offset += IMA_DIGEST_SIZE;
256
257         return ima_write_template_field_data(buffer, offset + digestsize,
258                                              fmt, field_data);
259 }
260
261 /*
262  * This function writes the digest of an event (with size limit).
263  */
264 int ima_eventdigest_init(struct ima_event_data *event_data,
265                          struct ima_field_data *field_data)
266 {
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;
275
276         memset(&hash, 0, sizeof(hash));
277
278         if (event_data->violation)      /* recording a violation. */
279                 goto out;
280
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;
285         }
286
287         if (!event_data->file)  /* missing info to re-calculate the digest */
288                 return -EINVAL;
289
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;
299         }
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);
305 }
306
307 /*
308  * This function writes the digest of an event (without size limit).
309  */
310 int ima_eventdigest_ng_init(struct ima_event_data *event_data,
311                             struct ima_field_data *field_data)
312 {
313         u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
314         u32 cur_digestsize = 0;
315
316         if (event_data->violation)      /* recording a violation. */
317                 goto out;
318
319         cur_digest = event_data->iint->ima_hash->digest;
320         cur_digestsize = event_data->iint->ima_hash->length;
321
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);
326 }
327
328 static int ima_eventname_init_common(struct ima_event_data *event_data,
329                                      struct ima_field_data *field_data,
330                                      bool size_limit)
331 {
332         const char *cur_filename = NULL;
333         u32 cur_filename_len = 0;
334
335         BUG_ON(event_data->filename == NULL && event_data->file == NULL);
336
337         if (event_data->filename) {
338                 cur_filename = event_data->filename;
339                 cur_filename_len = strlen(event_data->filename);
340
341                 if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
342                         goto out;
343         }
344
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
349                 /*
350                  * Truncate filename if the latter is too long and
351                  * the file descriptor is not available.
352                  */
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);
357 }
358
359 /*
360  * This function writes the name of an event (with size limit).
361  */
362 int ima_eventname_init(struct ima_event_data *event_data,
363                        struct ima_field_data *field_data)
364 {
365         return ima_eventname_init_common(event_data, field_data, true);
366 }
367
368 /*
369  * This function writes the name of an event (without size limit).
370  */
371 int ima_eventname_ng_init(struct ima_event_data *event_data,
372                           struct ima_field_data *field_data)
373 {
374         return ima_eventname_init_common(event_data, field_data, false);
375 }
376
377 /*
378  *  ima_eventsig_init - include the file signature as part of the template data
379  */
380 int ima_eventsig_init(struct ima_event_data *event_data,
381                       struct ima_field_data *field_data)
382 {
383         struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
384
385         if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
386                 return 0;
387
388         return ima_write_template_field_data(xattr_value, event_data->xattr_len,
389                                              DATA_FMT_HEX, field_data);
390 }