Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/common/image-fit.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * Copyright (C) Jan Lübbe, 2014
4
 *
5
 * This code is inspired by the U-Boot FIT image code.
6
 */
7
8
#define pr_fmt(fmt) "FIT: " fmt
9
#include <common.h>
10
#include <init.h>
11
#include <bootm.h>
12
#include <libfile.h>
13
#include <fdt.h>
14
#include <digest.h>
15
#include <of.h>
16
#include <fs.h>
17
#include <malloc.h>
18
#include <linux/ctype.h>
19
#include <linux/refcount.h>
20
#include <asm/byteorder.h>
21
#include <errno.h>
22
#include <linux/err.h>
23
#include <stringlist.h>
24
#include <crypto/public_key.h>
25
#include <uncompress.h>
26
#include <image-fit.h>
27
#include <fuzz.h>
28
29
4.76k
#define FDT_MAX_DEPTH 32
30
4.75k
#define FDT_MAX_PATH_LEN 200
31
32
#define CHECK_LEVEL_NONE 0
33
#define CHECK_LEVEL_HASH 1
34
#define CHECK_LEVEL_SIG 2
35
#define CHECK_LEVEL_MAX 3
36
37
static LIST_HEAD(open_fits);
38
39
static struct device_node *fit_get_child_by_name_exact(const struct device_node *node,
40
                   const char *name)
41
133k
{
42
133k
  struct device_node *child;
43
44
133k
  for_each_child_of_node(node, child)
45
174k
    if (child->name && (strcmp(child->name, name) == 0))
46
47.4k
      return child;
47
48
86.2k
  return NULL;
49
133k
}
50
51
static uint32_t dt_struct_advance(struct fdt_header *f, uint32_t dt, int size)
52
23.7k
{
53
23.7k
  dt += size;
54
23.7k
  dt = ALIGN(dt, 4);
55
56
23.7k
  if (dt > f->off_dt_struct + f->size_dt_struct)
57
21
    return 0;
58
59
23.6k
  return dt;
60
23.7k
}
61
62
static char *dt_string(struct fdt_header *f, char *strstart, uint32_t ofs)
63
15.0k
{
64
15.0k
  if (ofs > f->size_dt_strings)
65
0
    return NULL;
66
15.0k
  else
67
15.0k
    return strstart + ofs;
68
15.0k
}
69
70
static int fit_digest(struct fit_handle *handle, struct digest *digest,
71
          struct string_list *inc_nodes, struct string_list *exc_props,
72
          uint32_t hashed_strings_size)
73
442
{
74
442
  const struct fdt_header *fdt = handle->fit;
75
442
  const void *fit = handle->fit;
76
442
  uint32_t dt_struct;
77
442
  void *dt_strings;
78
442
  struct fdt_header f = {};
79
442
  int stack[FDT_MAX_DEPTH];
80
442
  char path[FDT_MAX_PATH_LEN];
81
442
  char *end;
82
442
  uint32_t tag;
83
442
  int start = -1;
84
442
  int depth = -1;
85
442
  int want = 0;
86
87
442
  f.totalsize = fdt32_to_cpu(fdt->totalsize);
88
442
  f.off_dt_struct = fdt32_to_cpu(fdt->off_dt_struct);
89
442
  f.size_dt_struct = fdt32_to_cpu(fdt->size_dt_struct);
90
442
  f.off_dt_strings = fdt32_to_cpu(fdt->off_dt_strings);
91
442
  f.size_dt_strings = fdt32_to_cpu(fdt->size_dt_strings);
92
93
442
  if (hashed_strings_size > f.size_dt_strings) {
94
48
    pr_err("%s: hashed-strings too large\n", __func__);
95
48
    return -EINVAL;
96
48
  }
97
98
394
  dt_struct = f.off_dt_struct;
99
394
  dt_strings = (void *)fdt + f.off_dt_strings;
100
101
394
  end = path;
102
394
  *end = '\0';
103
104
23.7k
  do {
105
23.7k
    const struct fdt_property *fdt_prop;
106
23.7k
    const struct fdt_node_header *fnh;
107
23.7k
    const char *name;
108
23.7k
    int include = 0;
109
23.7k
    int offset = dt_struct;
110
23.7k
    int maxlen, len;
111
112
23.7k
    tag = be32_to_cpu(*(uint32_t *)(fit + dt_struct));
113
114
23.7k
    switch (tag) {
115
4.76k
    case FDT_BEGIN_NODE:
116
4.76k
      fnh = fit + dt_struct;
117
4.76k
      name = fnh->name;
118
4.76k
      maxlen = (unsigned long)fdt + f.off_dt_struct +
119
4.76k
        f.size_dt_struct - (unsigned long)name;
120
121
4.76k
      len = strnlen(name, maxlen + 1);
122
4.76k
      if (len > maxlen)
123
0
        return -ESPIPE;
124
125
4.76k
      dt_struct = dt_struct_advance(&f, dt_struct,
126
4.76k
                  sizeof(struct fdt_node_header) + len + 1);
127
128
4.76k
      depth++;
129
4.76k
      if (depth == FDT_MAX_DEPTH)
130
3
        return -ESPIPE;
131
4.75k
      if (end - path + 2 + len >= FDT_MAX_PATH_LEN)
132
9
        return -ESPIPE;
133
4.74k
      if (end != path + 1)
134
4.35k
        *end++ = '/';
135
4.74k
      strcpy(end, name);
136
4.74k
      end += len;
137
4.74k
      stack[depth] = want;
138
4.74k
      if (string_list_contains(inc_nodes, path))
139
1.44k
        want = 2;
140
3.30k
      else if (want)
141
2.86k
        want--;
142
4.74k
      include = want;
143
144
4.74k
      break;
145
146
3.31k
    case FDT_END_NODE:
147
3.31k
      dt_struct = dt_struct_advance(&f, dt_struct, FDT_TAGSIZE);
148
149
3.31k
      if (depth < 0)
150
0
        return -ESPIPE;
151
3.31k
      include = want;
152
3.31k
      want = stack[depth--];
153
56.4k
      while (end > path && *--end != '/')
154
53.1k
        ;
155
3.31k
      *end = '\0';
156
157
3.31k
      break;
158
159
15.0k
    case FDT_PROP:
160
15.0k
      fdt_prop = fit + dt_struct;
161
15.0k
      len = fdt32_to_cpu(fdt_prop->len);
162
163
15.0k
      name = dt_string(&f, dt_strings, fdt32_to_cpu(fdt_prop->nameoff));
164
15.0k
      if (!name)
165
0
        return -ESPIPE;
166
167
15.0k
      dt_struct = dt_struct_advance(&f, dt_struct,
168
15.0k
                  sizeof(struct fdt_property) + len);
169
170
15.0k
      include = want >= 2;
171
15.0k
      if (string_list_contains(exc_props, name))
172
283
        include = 0;
173
174
15.0k
      break;
175
176
162
    case FDT_NOP:
177
162
      dt_struct = dt_struct_advance(&f, dt_struct, FDT_TAGSIZE);
178
179
162
      include = want >= 2;
180
181
162
      break;
182
183
382
    case FDT_END:
184
382
      dt_struct = dt_struct_advance(&f, dt_struct, FDT_TAGSIZE);
185
186
382
      include = 1;
187
188
382
      break;
189
190
0
    default:
191
0
      pr_err("%s: Unknown tag 0x%08X\n", __func__, tag);
192
0
      return -EINVAL;
193
23.7k
    }
194
195
23.6k
    if (!dt_struct)
196
21
      return -ESPIPE;
197
198
23.6k
    pr_debug("%s: include %d, want %d, offset 0x%x, len 0x%x\n",
199
23.6k
       path, include, want, offset, dt_struct-offset);
200
201
23.6k
    if (include && start == -1)
202
2.64k
      start = offset;
203
204
23.6k
    if (!include && start != -1) {
205
2.27k
      pr_debug("region: 0x%p+0x%x\n", fit + start, offset - start);
206
2.27k
      digest_update(digest, fit + start, offset - start);
207
2.27k
      start = -1;
208
2.27k
    }
209
23.6k
  } while (tag != FDT_END);
210
211
361
  pr_debug("region: 0x%p+0x%x\n", fit + start, dt_struct - start);
212
361
  digest_update(digest, fit + start, dt_struct - start);
213
214
361
  pr_debug("strings: 0x%p+0x%x\n", dt_strings, hashed_strings_size);
215
361
  digest_update(digest, dt_strings, hashed_strings_size);
216
217
361
  return 0;
218
394
}
219
220
static struct digest *fit_alloc_digest(struct device_node *sig_node,
221
               enum hash_algo *algo_out)
222
556
{
223
556
  struct digest *digest;
224
556
  enum hash_algo algo;
225
556
  const char *algo_name;
226
227
556
  if (of_property_read_string(sig_node, "algo", &algo_name)) {
228
22
    pr_err("algo property not found\n");
229
22
    return ERR_PTR(-EINVAL);
230
22
  }
231
232
534
  if (strncmp(algo_name, "sha1,", 5) == 0) {
233
318
    algo = HASH_ALGO_SHA1;
234
318
  } else if (strncmp(algo_name, "sha256,", 7) == 0) {
235
165
    algo = HASH_ALGO_SHA256;
236
165
  } else {
237
51
    pr_err("unknown algo %s\n", algo_name);
238
51
    return ERR_PTR(-EINVAL);
239
51
  }
240
241
483
  digest = digest_alloc_by_algo(algo);
242
483
  if (!digest) {
243
0
    pr_err("unsupported algo %s\n", algo_name);
244
0
    return ERR_PTR(-EINVAL);
245
0
  }
246
247
483
  digest_init(digest);
248
249
483
  *algo_out = algo;
250
251
483
  return digest;
252
483
}
253
254
static int fit_check_signature(struct fit_handle *handle, struct device_node *sig_node,
255
             enum hash_algo algo, void *hash)
256
402
{
257
402
  const char *fail_reason;
258
402
  const struct public_key *key;
259
402
  const struct keyring *fit_kr;
260
402
  const char *key_name = NULL;
261
402
  int sig_len;
262
402
  const char *sig_value;
263
402
  int ret;
264
265
402
  sig_value = of_get_property(sig_node, "value", &sig_len);
266
402
  if (!sig_value) {
267
116
    pr_err("signature value not found in %pOF\n", sig_node);
268
116
    return -EINVAL;
269
116
  }
270
271
286
  fail_reason = "no matching keys";
272
273
286
  fit_kr = keyring_find("fit");
274
286
  if (!fit_kr) {
275
0
    pr_err("fit keyring not registered\n");
276
0
    return -ENOKEY;
277
0
  }
278
279
286
  of_property_read_string(sig_node, "key-name-hint", &key_name);
280
286
  if (key_name) {
281
139
    key = keyring_find_key(fit_kr, key_name);
282
139
    if (!IS_ERR(key)) {
283
56
      fail_reason = "verification failed";
284
56
      ret = public_key_verify(key, sig_value, sig_len, hash, algo);
285
56
      if (handle->verbose)
286
56
        pr_info("Key %*phN (%s) -> signature %s\n", key->hashlen,
287
56
          key->hash, key_name, ret ? "BAD" : "OK");
288
56
      if (!ret)
289
37
        goto ok;
290
56
    }
291
139
  }
292
293
249
  for_each_key_in_keyring(key, fit_kr) {
294
295
    /* Don't recheck with same key as before */
296
249
    if (key_name && streq_ptr(key->key_name_hint, key_name))
297
19
      continue;
298
299
230
    fail_reason = "verification failed";
300
230
    ret = public_key_verify(key, sig_value, sig_len, hash, algo);
301
302
230
    if (handle->verbose)
303
230
      pr_info("Key %*phN -> signature %s\n", key->hashlen, key->hash,
304
230
        ret ? "BAD" : "OK");
305
306
230
    if (!ret)
307
169
      goto ok;
308
230
  }
309
310
80
  pr_err("image signature BAD: %s\n", fail_reason);
311
312
80
  return -EBADMSG;
313
206
ok:
314
206
  return 0;
315
249
}
316
317
static int fit_config_build_hash_nodes(struct fit_handle *handle,
318
               struct device_node *conf_node,
319
               struct string_list *node_list)
320
539
{
321
539
  struct device_node *image_node, *child;
322
539
  struct property *prop;
323
539
  const char *unit;
324
539
  int i, ret, count;
325
326
539
  ret = string_list_add(node_list, "/");
327
539
  if (ret)
328
0
    return ret;
329
539
  ret = string_list_add_asprintf(node_list, "%s", conf_node->full_name);
330
539
  if (ret)
331
0
    return ret;
332
333
2.80k
  for_each_property_of_node(conf_node, prop) {
334
2.80k
    if (!strcmp(prop->name, "description") ||
335
1.89k
        !strcmp(prop->name, "compatible") ||
336
1.71k
        !strcmp(prop->name, "cmdline") ||
337
1.71k
        !strcmp(prop->name, "default"))
338
1.21k
      continue;
339
340
    /* permit neither empty properties, nor unterminated strings.
341
     * Should we choose to support e.g. the boolean load-only
342
     * property in future, we should handle it specially and
343
     * allow that only it can be empty and not all properties.
344
     */
345
1.59k
    count = of_property_count_strings(conf_node, prop->name);
346
1.59k
    if (count < 0)
347
8
      return count;
348
349
3.21k
    for (i = 0; i < count; i++) {
350
1.67k
      if (of_property_read_string_index(conf_node, prop->name,
351
1.67k
                i, &unit))
352
0
        return -EINVAL;
353
354
1.67k
      if (strchr(unit, '/'))
355
6
        return -EINVAL;
356
357
1.66k
      ret = string_list_add_asprintf(node_list, "/images/%s", unit);
358
1.66k
      if (ret)
359
0
        return ret;
360
361
1.66k
      image_node = fit_get_child_by_name_exact(handle->images, unit);
362
1.66k
      if (!image_node)
363
41
        return -EINVAL;
364
365
1.62k
      for_each_child_of_node(image_node, child) {
366
1.35k
        if (!of_node_has_prefix(child, "hash"))
367
958
          continue;
368
395
        ret = string_list_add_asprintf(node_list, "/images/%s/%s",
369
395
                     unit, child->name);
370
395
        if (ret)
371
0
          return ret;
372
395
      }
373
1.62k
    }
374
1.58k
  }
375
376
484
  return 0;
377
539
}
378
379
/**
380
 * fit_config_check_hash_nodes - Sanity check hashed-nodes
381
 * @sig_node: Signature node of a FIT configuration
382
 * @inc_nodes: String list of nodes included in the hash
383
 *
384
 * Check if the informational hashed-nodes property is cosistent with
385
 * the list of nodes to hash that we calculated.
386
 *
387
 * We only do this if hash verification failed, so we can present a better
388
 * error messages in some circumstances.
389
 */
390
static void fit_config_check_hash_nodes(struct device_node *sig_node,
391
          struct string_list *inc_nodes)
392
163
{
393
163
  struct string_list *entry;
394
163
  int ret, i = 0;
395
396
874
  string_list_for_each_entry(entry, inc_nodes) {
397
874
    ret = of_property_match_string(sig_node, "hashed-nodes", entry->str);
398
874
    if (ret < 0)
399
874
      pr_err("%pOF/hashed-nodes: '%s' is missing\n", sig_node,
400
874
             entry->str);
401
402
874
    i++;
403
874
  }
404
405
163
  ret = of_property_count_strings(sig_node, "hashed-nodes");
406
163
  if (ret != i)
407
163
    pr_err("hashed-nodes property has more entries than calculated: %d != %d\n",
408
163
           ret, i);
409
163
}
410
411
/*
412
 * Hash algorithms in order of precedence. Only the hash with the strongest
413
 * algorithm is verified. The insecure algorithms are only good for detecting
414
 * accidental corruption and are never used for verified boot.
415
 */
416
static const struct fit_hash_algo {
417
  const char *name;
418
  bool secure;
419
} fit_hash_algos[] = {
420
  { .name = "sha512", .secure = true },
421
  { .name = "sha384", .secure = true },
422
  { .name = "sha256", .secure = true },
423
  { .name = "sha1", .secure = false },
424
  { .name = "md5", .secure = false },
425
  { .name = "crc32", .secure = false },
426
};
427
428
static struct device_node *fit_find_hash_node(struct device_node *image,
429
                const char *name)
430
2.60k
{
431
2.60k
  struct device_node *hash;
432
2.60k
  const char *algo;
433
434
2.60k
  for_each_child_of_node(image, hash) {
435
869
    if (!of_node_has_prefix(hash, "hash"))
436
355
      continue;
437
438
514
    if (!of_property_read_string(hash, "algo", &algo) &&
439
352
        !strcmp(algo, name))
440
81
      return hash;
441
514
  }
442
443
2.52k
  return NULL;
444
2.60k
}
445
446
/*
447
 * The consistency of the FTD structure was already checked by of_unflatten_dtb()
448
 */
449
static int fit_verify_signature(struct fit_handle *handle,
450
        struct device_node *sig_node,
451
        struct device_node *conf_node)
452
621
{
453
621
  uint32_t hashed_strings_start, hashed_strings_size;
454
621
  struct string_list inc_nodes, exc_props;
455
621
  struct digest *digest;
456
621
  void *hash;
457
621
  enum hash_algo algo = 0;
458
621
  int ret;
459
460
621
  if (of_property_read_u32_index(sig_node, "hashed-strings", 0,
461
621
      &hashed_strings_start)) {
462
35
    pr_err("hashed-strings start not found in %pOF\n", sig_node);
463
35
    return -EINVAL;
464
35
  }
465
466
586
  if (hashed_strings_start != 0) {
467
44
    pr_err("%pOF: hashed-strings offset must be 0\n", sig_node);
468
44
    return -EINVAL;
469
44
  }
470
471
542
  if (of_property_read_u32_index(sig_node, "hashed-strings", 1,
472
542
      &hashed_strings_size)) {
473
3
    pr_err("hashed-strings size not found in %pOF\n", sig_node);
474
3
    return -EINVAL;
475
3
  }
476
477
539
  string_list_init(&inc_nodes);
478
539
  string_list_init(&exc_props);
479
480
539
  ret = fit_config_build_hash_nodes(handle, conf_node, &inc_nodes);
481
539
  if (ret) {
482
55
    pr_err("Failed to build hash node list for %pOF\n", conf_node);
483
55
    goto out_sl;
484
55
  }
485
486
484
  string_list_add(&exc_props, "data");
487
488
484
  digest = fit_alloc_digest(sig_node, &algo);
489
484
  if (IS_ERR(digest)) {
490
42
    ret = PTR_ERR(digest);
491
42
    goto out_sl;
492
42
  }
493
494
442
  ret = fit_digest(handle, digest, &inc_nodes, &exc_props, hashed_strings_size);
495
442
  if (ret)
496
81
    goto out_digest;
497
498
361
  hash = xzalloc(digest_length(digest));
499
361
  digest_final(digest, hash);
500
501
361
  ret = fit_check_signature(handle, sig_node, algo, hash);
502
361
  if (ret)
503
163
    fit_config_check_hash_nodes(sig_node, &inc_nodes);
504
505
361
  free(hash);
506
442
 out_digest:
507
442
  digest_free(digest);
508
539
 out_sl:
509
539
  string_list_free(&inc_nodes);
510
539
  string_list_free(&exc_props);
511
512
539
  return ret;
513
442
}
514
515
static int fit_verify_hash_node(struct fit_handle *handle,
516
        struct device_node *hash, struct digest *d,
517
        const void *data, int data_len)
518
81
{
519
81
  const char *value_read;
520
81
  int hash_len;
521
522
81
  value_read = of_get_property(hash, "value", &hash_len);
523
81
  if (!value_read) {
524
20
    pr_err("%pOF: \"value\" property not found\n", hash);
525
20
    return -EINVAL;
526
20
  }
527
528
61
  if (hash_len != digest_length(d)) {
529
14
    pr_err("%pOF: invalid hash length %d\n", hash, hash_len);
530
14
    return -EINVAL;
531
14
  }
532
533
47
  digest_init(d);
534
47
  digest_update(d, data, data_len);
535
536
47
  if (digest_verify(d, value_read)) {
537
19
    pr_err("%pOF: hash BAD\n", hash);
538
19
    return -EBADMSG;
539
19
  }
540
541
28
  if (handle->verbose)
542
28
    pr_info("%pOF: hash OK\n", hash);
543
544
28
  return 0;
545
47
}
546
547
static int fit_verify_hash(struct fit_handle *handle, struct device_node *image,
548
         const void *data, int data_len)
549
7.90k
{
550
7.90k
  const struct fit_hash_algo *algo;
551
7.90k
  struct device_node *hash;
552
7.90k
  struct digest *d;
553
7.90k
  int i, ret;
554
555
7.90k
  if (handle->verify == BOOTM_VERIFY_NONE)
556
7.42k
    return 0;
557
558
3.00k
  for (i = 0; i < ARRAY_SIZE(fit_hash_algos); i++) {
559
2.61k
    algo = &fit_hash_algos[i];
560
561
2.61k
    if (handle->verify == BOOTM_VERIFY_SIGNATURE && !algo->secure)
562
14
      break;
563
564
2.60k
    hash = fit_find_hash_node(image, algo->name);
565
2.60k
    if (!hash)
566
2.52k
      continue;
567
568
81
    d = digest_alloc(algo->name);
569
81
    if (!d) {
570
0
      pr_debug("%pOF: unsupported algo %s, skipping\n",
571
0
         hash, algo->name);
572
0
      continue;
573
0
    }
574
575
81
    ret = fit_verify_hash_node(handle, hash, d, data, data_len);
576
81
    digest_free(d);
577
578
81
    return ret;
579
81
  }
580
581
401
  if (handle->verify == BOOTM_VERIFY_AVAILABLE)
582
339
    return 0;
583
584
62
  if (handle->verify == BOOTM_VERIFY_SIGNATURE)
585
62
    pr_err("image %pOF has no supported hash allowed for verified boot\n",
586
48
           image);
587
48
  else
588
62
    pr_err("image %pOF has no supported hash\n", image);
589
590
62
  return -EINVAL;
591
401
}
592
593
static int fit_image_verify_signature(struct fit_handle *handle,
594
              struct device_node *image,
595
              const void *data, int data_len)
596
5.20k
{
597
5.20k
  struct digest *digest;
598
5.20k
  struct device_node *sig_node;
599
5.20k
  enum hash_algo algo = 0;
600
5.20k
  void *hash;
601
5.20k
  int ret;
602
603
5.20k
  if (!IS_ENABLED(CONFIG_FITIMAGE_SIGNATURE)) {
604
0
    if (handle->verify != BOOTM_VERIFY_SIGNATURE)
605
0
      return 0;
606
607
0
    pr_err("signature verification required but support is disabled\n");
608
0
    return -ENOSYS;
609
0
  }
610
611
5.20k
  switch (handle->verify) {
612
4.84k
  case BOOTM_VERIFY_NONE:
613
4.84k
    return 0;
614
237
  case BOOTM_VERIFY_AVAILABLE:
615
237
    ret = 0;
616
237
    break;
617
128
  default:
618
128
    ret = -EINVAL;
619
5.20k
  }
620
621
365
  sig_node = fit_get_child_by_name_exact(image, "signature-1");
622
365
  if (!sig_node)
623
299
    sig_node = fit_get_child_by_name_exact(image, "signature@1");
624
365
  if (!sig_node) {
625
293
    pr_err("Image %pOF has no signature\n", image);
626
293
    return ret;
627
293
  }
628
629
72
  digest = fit_alloc_digest(sig_node, &algo);
630
72
  if (IS_ERR(digest))
631
31
    return PTR_ERR(digest);
632
633
41
  digest_update(digest, data, data_len);
634
41
  hash = xzalloc(digest_length(digest));
635
41
  digest_final(digest, hash);
636
637
41
  ret = fit_check_signature(handle, sig_node, algo, hash);
638
639
41
  free(hash);
640
641
41
  digest_free(digest);
642
643
41
  return ret;
644
72
}
645
646
int fit_count_images(struct fit_handle *handle, void *configuration,
647
         const char *name)
648
25.3k
{
649
25.3k
  struct device_node *conf_node = configuration;
650
651
25.3k
  if (!conf_node)
652
0
    return -EINVAL;
653
654
25.3k
  return of_property_count_strings(conf_node, name);
655
25.3k
}
656
657
static int fit_get_address(struct device_node *image, const char *property,
658
         unsigned long *addr)
659
15.9k
{
660
15.9k
  const __be32 *cell;
661
15.9k
  int len = 0;
662
663
15.9k
  cell = of_get_property(image, property, &len);
664
15.9k
  if (!cell)
665
14.6k
    return -EINVAL;
666
1.31k
  if (len > sizeof(*addr))
667
39
    return -ENOTSUPP;
668
669
1.28k
  *addr = (unsigned long)of_read_number(cell, len / sizeof(*cell));
670
1.28k
  return 0;
671
1.31k
}
672
673
static struct device_node *
674
fit_get_image(struct fit_handle *handle, void *configuration,
675
        const char **unit, int idx)
676
146k
{
677
146k
  struct device_node *conf_node = configuration;
678
679
146k
  if (conf_node) {
680
59.9k
    if (of_property_read_string_index(conf_node, *unit, idx, unit)) {
681
32.0k
      pr_err("No image named '%s'\n", *unit);
682
32.0k
      return NULL;
683
32.0k
    }
684
59.9k
  }
685
686
114k
  return fit_get_child_by_name_exact(handle->images, *unit);
687
146k
}
688
689
/**
690
 * fit_get_image_address - Get an address from an image in a FIT image
691
 * @handle: The FIT image handle
692
 * @name: The name of the image to open
693
 * @property: The name of the address to get (for example "load" or "entry")
694
 * @address: The address given by the image
695
 *
696
 * Try to parse the @property in the image @name as an address. @configuration
697
 * holds the cookie returned from fit_open_configuration() if the image is
698
 * opened as part of a configuration, or NULL if the image is opened without a
699
 * configuration. If it exists the value will be returned in @address. Otherwise
700
 * @address won't be changed.
701
 *
702
 * Return: 0 for success, negative error code otherwise
703
 */
704
int fit_get_image_address(struct fit_handle *handle, void *configuration,
705
        const char *name, const char *property,
706
        unsigned long *address)
707
97.3k
{
708
97.3k
  struct device_node *image;
709
97.3k
  const char *unit = name;
710
97.3k
  const char *type;
711
97.3k
  int ret;
712
713
97.3k
  if (!address || !property || !name)
714
0
    return -EINVAL;
715
716
97.3k
  image = fit_get_image(handle, configuration, &unit, 0);
717
97.3k
  if (!image)
718
81.1k
    return -ENOENT;
719
720
  /* Treat type = "kernel_noload" as if entry/load address is missing */
721
16.2k
  ret = of_property_read_string(image, "type", &type);
722
16.2k
  if (!ret && !strcmp(type, "kernel_noload"))
723
302
      return -ENOENT;
724
725
15.9k
  ret = fit_get_address(image, property, address);
726
727
15.9k
  return ret;
728
16.2k
}
729
730
static void fit_uncompress_error_fn(char *x)
731
11.6k
{
732
11.6k
  pr_err("%s\n", x);
733
11.6k
}
734
735
static const char *get_compression_type(struct device_node *image)
736
13.5k
{
737
13.5k
  const char *compression = NULL;
738
739
13.5k
  of_property_read_string(image, "compression", &compression);
740
13.5k
  if (!compression || !strcmp(compression, "none"))
741
1.23k
    return NULL;
742
743
12.3k
  return compression;
744
13.5k
}
745
746
static int fit_handle_decompression(struct device_node *image,
747
            const char *type,
748
            const void **data,
749
            int *data_len)
750
12.8k
{
751
12.8k
  const char *compression;
752
12.8k
  void *uc_data;
753
12.8k
  int ret;
754
755
12.8k
  compression = get_compression_type(image);
756
12.8k
  if (!compression)
757
577
    return 0;
758
759
12.2k
  if (!strcmp(type, "ramdisk")) {
760
15
    pr_warn("compression != \"none\" for ramdisks is deprecated,"
761
15
      " please fix your .its file!\n");
762
15
    return 0;
763
15
  }
764
765
12.2k
  if (!IS_ENABLED(CONFIG_UNCOMPRESS)) {
766
0
    pr_err("image has compression = \"%s\", but support not compiled in\n",
767
0
           compression);
768
0
    return -ENOSYS;
769
0
  }
770
771
12.2k
  ret = uncompress_buf_to_buf(*data, *data_len, &uc_data,
772
12.2k
            fit_uncompress_error_fn);
773
12.2k
  if (ret < 0) {
774
11.7k
    pr_err("%s data couldn't be decompressed\n", compression);
775
11.7k
    return ret;
776
11.7k
  }
777
778
526
  *data = uc_data;
779
526
  *data_len = ret;
780
781
  /* associate buffer with FIT, so it's not leaked */
782
526
  __of_new_property(image, "$uncompressed-data", uc_data, *data_len);
783
784
526
  return 0;
785
12.2k
}
786
787
/**
788
 * fit_open_image - Open an image in a FIT image
789
 * @handle: The FIT image handle
790
 * @configuration: configuration cookie from fit_open_configuration(), or NULL
791
 * @name: The name of the image to open
792
 * @idx: The index of image to open (for multi-image properties like overlays)
793
 * @outdata: The returned image
794
 * @outsize: Size of the returned image
795
 *
796
 * Open an image in a FIT image. The returned image is freed during fit_close().
797
 *
798
 * If @configuration is NULL then the RSA signature of the image is checked
799
 * if desired; otherwise only the hash is checked (because opening the
800
 * configuration already checks the RSA signature of all involved nodes).
801
 *
802
 * Return: 0 for success, negative error code otherwise
803
 */
804
int fit_open_image(struct fit_handle *handle, void *configuration,
805
       const char *name, int idx, const void **outdata,
806
       unsigned long *outsize)
807
48.5k
{
808
48.5k
  struct device_node *image;
809
48.5k
  const char *unit = name, *type = NULL, *desc= "(no description)";
810
48.5k
  const void *data;
811
48.5k
  int data_len;
812
48.5k
  int ret = 0;
813
814
48.5k
  image = fit_get_image(handle, configuration, &unit, idx);
815
48.5k
  if (!image)
816
34.5k
    return -ENOENT;
817
818
13.9k
  of_property_read_string(image, "description", &desc);
819
13.9k
  if (handle->verbose)
820
13.9k
    pr_info("image '%s': '%s'\n", unit, desc);
821
822
13.9k
  of_property_read_string(image, "type", &type);
823
13.9k
  if (!type) {
824
687
    pr_err("No \"type\" property found in %pOF\n", image);
825
687
    return -EINVAL;
826
687
  }
827
828
13.2k
  data = of_get_property(image, "data", &data_len);
829
13.2k
  if (!data) {
830
169
    pr_err("data not found\n");
831
169
    return -EINVAL;
832
169
  }
833
834
13.1k
  if (configuration)
835
7.90k
    ret = fit_verify_hash(handle, image, data, data_len);
836
5.20k
  else
837
5.20k
    ret = fit_image_verify_signature(handle, image, data, data_len);
838
839
13.1k
  if (ret < 0)
840
284
    return ret;
841
842
12.8k
  ret = fit_handle_decompression(image, type, &data, &data_len);
843
12.8k
  if (ret)
844
11.7k
    return ret;
845
846
1.11k
  *outdata = data;
847
1.11k
  *outsize = data_len;
848
849
1.11k
  return 0;
850
12.8k
}
851
852
int fit_config_verify_signature(struct fit_handle *handle, struct device_node *conf_node)
853
4.40k
{
854
4.40k
  struct device_node *sig_node;
855
4.40k
  int ret = -EINVAL;
856
857
4.40k
  if (!IS_ENABLED(CONFIG_FITIMAGE_SIGNATURE)) {
858
0
    if (handle->verify != BOOTM_VERIFY_SIGNATURE)
859
0
      return 0;
860
861
0
    pr_err("signature verification required but support is disabled\n");
862
0
    return -ENOSYS;
863
0
  }
864
865
4.40k
  switch (handle->verify) {
866
3.72k
  case BOOTM_VERIFY_NONE:
867
3.77k
  case BOOTM_VERIFY_HASH:
868
3.77k
    return 0;
869
162
  case BOOTM_VERIFY_SIGNATURE:
870
162
    ret = -EINVAL;
871
162
    break;
872
466
  case BOOTM_VERIFY_AVAILABLE:
873
466
    ret = 0;
874
466
    break;
875
4.40k
  }
876
877
905
  for_each_child_of_node(conf_node, sig_node) {
878
905
    if (!of_node_has_prefix(sig_node, "signature"))
879
284
      continue;
880
881
621
    if (handle->verbose)
882
0
      of_print_nodes(sig_node, 0, ~0);
883
884
621
    ret = fit_verify_signature(handle, sig_node, conf_node);
885
621
    if (ret < 0)
886
423
      return ret;
887
621
  }
888
889
205
  if (ret < 0) {
890
42
    pr_err("configuration '%pOF' does not have a signature\n", conf_node);
891
42
    return ret;
892
42
  }
893
894
163
  return ret;
895
205
}
896
897
static int fit_fdt_is_compatible(struct fit_handle *handle,
898
         struct device_node *child,
899
         const char *machine)
900
4.05k
{
901
4.05k
  const char *reason = "malformed";
902
4.05k
  struct device_node *image;
903
4.05k
  const char *unit = "fdt";
904
4.05k
  int data_len;
905
4.05k
  const void *data;
906
907
4.05k
  if (of_property_present(child, "compatible"))
908
72
    return 0;
909
3.97k
  if (!of_property_present(child, "fdt"))
910
2.93k
    return 0;
911
912
1.04k
  image = fit_get_image(handle, child, &unit, 0);
913
1.04k
  if (!image)
914
275
    goto err;
915
916
767
  data = of_get_property(image, "data", &data_len);
917
767
  if (!data)
918
41
    goto err;
919
920
  /* We have three options here:
921
   *
922
   * 1) Increase our attack surface by all supported compression algos
923
   * 2) Verify all configurations in the image as we search for best
924
   *    OF match score
925
   * 3) Blame the user and expect them to supply a compatible property
926
   *    in the configuration node if they want to compress their FDTs
927
   *
928
   * We go for option 3.
929
   */
930
726
  if (get_compression_type(image)) {
931
72
    reason = "compressed";
932
72
    goto err;
933
72
  }
934
935
654
  return fdt_machine_is_compatible(data, data_len, machine);
936
388
err:
937
388
  pr_warn("skipping %s configuration \"%pOF\"\n",
938
388
    reason, child);
939
388
  return 0;
940
726
}
941
942
static int fit_find_compatible_unit(struct fit_handle *handle,
943
            struct device_node *conf_node,
944
            const char **unit,
945
            bool (*config_node_valid)(struct fit_handle *handle,
946
                    struct device_node *config))
947
4.33k
{
948
4.33k
  struct device_node *child = NULL, *dflt = NULL, *best = NULL;
949
4.33k
  struct device_node *barebox_root;
950
4.33k
  int best_score = 0;
951
4.33k
  const char *machine, *dfltname = NULL;
952
4.33k
  bool dflt_pending = false;
953
4.33k
  int ret;
954
955
4.33k
  if (!of_property_read_string(conf_node, "default", &dfltname)) {
956
314
    dflt = fit_get_child_by_name_exact(conf_node, dfltname);
957
314
    if (dflt)
958
151
      dflt_pending = true;
959
314
  }
960
961
4.33k
  barebox_root = of_get_root_node();
962
4.33k
  if (!barebox_root)
963
0
    goto default_unit;
964
965
4.33k
  ret = of_property_read_string(barebox_root, "compatible", &machine);
966
4.33k
  if (ret)
967
0
    goto default_unit;
968
969
5.71k
  for_each_child_of_node(conf_node, child) {
970
5.71k
    int score;
971
972
5.71k
    if (child == dflt)
973
151
      dflt_pending = false;
974
975
5.71k
    if (config_node_valid && !config_node_valid(handle, child))
976
1.63k
      continue;
977
978
4.08k
    score = of_device_is_compatible(child, machine);
979
980
4.08k
    if (!score)
981
4.05k
      score = fit_fdt_is_compatible(handle, child, machine);
982
983
4.08k
    if (!score)
984
4.04k
      continue;
985
986
    /*
987
     * A FIT may carry one base devicetree plus a number of
988
     * overlay combinations with multiple configurations matching the
989
     * board equally well. Allow the image author to influences who
990
     * wins ties by means of the default property.
991
     */
992
38
    if (score > best_score || (score == best_score && child == dflt)) {
993
38
      best_score = score;
994
38
      best = child;
995
38
    }
996
997
    /* Nothing left to walk into that could do better */
998
38
    if (best_score == OF_DEVICE_COMPATIBLE_MAX_SCORE && !dflt_pending)
999
24
      break;
1000
38
  }
1001
1002
4.33k
  if (best) {
1003
38
    *unit = best->name;
1004
38
    pr_info("matching unit '%s' found\n", *unit);
1005
38
    return 0;
1006
38
  }
1007
1008
4.30k
default_unit:
1009
4.30k
  pr_info("No match found. Trying default.\n");
1010
4.30k
  if (!dflt) {
1011
4.17k
    if (dfltname)
1012
4.17k
      pr_err("default configuration '%s' not found\n", dfltname);
1013
4.17k
    return -ENOENT;
1014
4.17k
  }
1015
1016
128
  *unit = dflt->name;
1017
1018
128
  return 0;
1019
4.30k
}
1020
1021
static int fit_find_last_unit(struct fit_handle *handle,
1022
            const char **out_unit)
1023
5.73k
{
1024
5.73k
  struct device_node *conf_node = handle->configurations;
1025
5.73k
  struct device_node *child;
1026
5.73k
  const char *unit = NULL;
1027
1028
5.73k
  if (!conf_node)
1029
1.46k
    return -ENOENT;
1030
1031
4.26k
  for_each_child_of_node(conf_node, child)
1032
5.64k
    unit = child->name;
1033
1034
4.26k
  if (!unit)
1035
34
    return -ENOENT;
1036
1037
4.23k
  *out_unit = unit;
1038
4.23k
  return 0;
1039
4.26k
}
1040
1041
/**
1042
 * fit_open_configuration - open a FIT configuration
1043
 * @handle: The FIT image handle
1044
 * @name: The name of the configuration
1045
 *
1046
 * This opens a FIT configuration and eventually checks the signature
1047
 * depending on the verify mode the FIT image is opened with.
1048
 *
1049
 * Return: If successful a pointer to a valid configuration node,
1050
 *         otherwise a ERR_PTR()
1051
 */
1052
void *fit_open_configuration(struct fit_handle *handle, const char *name,
1053
           bool (*match_valid)(struct fit_handle *handle,
1054
             struct device_node *config))
1055
10.0k
{
1056
10.0k
  struct device_node *conf_node = handle->configurations;
1057
10.0k
  const char *unit, *desc = "(no description)";
1058
10.0k
  int ret;
1059
1060
10.0k
  if (!conf_node)
1061
1.46k
    return ERR_PTR(-ENOENT);
1062
1063
8.57k
  if (name) {
1064
4.23k
    unit = name;
1065
4.33k
  } else {
1066
4.33k
    ret = fit_find_compatible_unit(handle, conf_node, &unit,
1067
4.33k
                 match_valid);
1068
4.33k
    if (ret) {
1069
4.17k
      pr_info("Couldn't get a valid configuration. Aborting.\n");
1070
4.17k
      return ERR_PTR(ret);
1071
4.17k
    }
1072
4.33k
  }
1073
1074
4.40k
  conf_node = fit_get_child_by_name_exact(conf_node, unit);
1075
4.40k
  if (!conf_node) {
1076
0
    pr_err("configuration '%s' not found\n", unit);
1077
0
    return ERR_PTR(-ENOENT);
1078
0
  }
1079
1080
4.40k
  of_property_read_string(conf_node, "description", &desc);
1081
4.40k
  pr_info("configuration '%s': %s\n", unit, desc);
1082
1083
4.40k
  ret = fit_config_verify_signature(handle, conf_node);
1084
4.40k
  if (ret)
1085
465
    return ERR_PTR(ret);
1086
1087
3.93k
  return conf_node;
1088
4.40k
}
1089
1090
const char *fit_config_get_name(struct fit_handle *handle, void *config)
1091
3.93k
{
1092
3.93k
  struct device_node *node = config;
1093
3.93k
  return node->name;
1094
3.93k
}
1095
1096
static struct fit_handle *fit_get_handle(const char *filename)
1097
729
{
1098
729
  struct fit_handle *handle;
1099
1100
729
  list_for_each_entry(handle, &open_fits, entry) {
1101
288
    if (!strcmp(filename, handle->filename))
1102
288
      return handle;
1103
288
  }
1104
1105
441
  return NULL;
1106
729
}
1107
1108
static int fit_do_open(struct fit_handle *handle)
1109
6.54k
{
1110
6.54k
  const char *desc = "(no description)";
1111
6.54k
  struct device_node *root;
1112
1113
6.54k
  root = of_unflatten_dtb_const(handle->fit, handle->size);
1114
6.54k
  if (IS_ERR(root))
1115
713
    return PTR_ERR(root);
1116
1117
5.83k
  handle->root = root;
1118
1119
5.83k
  handle->images = fit_get_child_by_name_exact(handle->root, "images");
1120
5.83k
  if (!handle->images)
1121
30
    return -ENOENT;
1122
1123
5.80k
  handle->configurations = fit_get_child_by_name_exact(handle->root, "configurations");
1124
1125
5.80k
  of_property_read_string(handle->root, "description", &desc);
1126
5.80k
  pr_info("Opened FIT image: %s\n", desc);
1127
1128
5.80k
  return 0;
1129
5.83k
}
1130
1131
/**
1132
 * fit_open_buf - open a FIT image from a buffer
1133
 * @buf:  The buffer containing the FIT image
1134
 * @size: Size of the FIT image
1135
 * @verbose:  If true, be more verbose
1136
 * @verify: The verify mode
1137
 *
1138
 * This opens a FIT image found in buf. The returned handle is used as
1139
 * context for the other FIT functions.
1140
 *
1141
 * Return: A handle to a FIT image or a ERR_PTR
1142
 */
1143
struct fit_handle *fit_open_buf(const void *buf, size_t size, bool verbose,
1144
        enum bootm_verify verify)
1145
6.14k
{
1146
6.14k
  struct fit_handle *handle;
1147
6.14k
  int ret;
1148
1149
6.14k
  handle = xzalloc(sizeof(struct fit_handle));
1150
1151
6.14k
  handle->verbose = verbose;
1152
6.14k
  handle->fit = buf;
1153
6.14k
  handle->size = size;
1154
6.14k
  handle->verify = verify;
1155
1156
6.14k
  refcount_set(&handle->users, 1);
1157
1158
6.14k
  ret = fit_do_open(handle);
1159
6.14k
  if (ret) {
1160
631
    fit_close(handle);
1161
631
    return ERR_PTR(ret);
1162
631
  }
1163
1164
5.51k
  return handle;
1165
6.14k
}
1166
1167
/**
1168
 * fit_open - open a FIT image
1169
 * @filename: The filename of the FIT image
1170
 * @verbose:  If true, be more verbose
1171
 * @verify: The verify mode
1172
 *
1173
 * This opens a FIT image found in @filename. The returned handle is used as
1174
 * context for the other FIT functions.
1175
 *
1176
 * Return: A handle to a FIT image or a ERR_PTR
1177
 */
1178
struct fit_handle *fit_open(const char *_filename, bool verbose,
1179
          enum bootm_verify verify)
1180
729
{
1181
729
  struct fit_handle *handle;
1182
729
  ssize_t nbytes;
1183
729
  char *filename;
1184
729
  int fd, ret;
1185
1186
729
  filename = canonicalize_path(AT_FDCWD, _filename);
1187
729
  if (!filename) {
1188
0
    pr_err("Cannot open %s: %m\n", _filename);
1189
0
    return ERR_PTR(-errno);
1190
0
  }
1191
1192
729
  handle = fit_get_handle(filename);
1193
729
  if (handle) {
1194
288
    free(filename);
1195
288
    refcount_inc(&handle->users);
1196
288
    return handle;
1197
288
  }
1198
1199
441
  handle = xzalloc(sizeof(struct fit_handle));
1200
1201
441
  handle->verbose = verbose;
1202
441
  handle->verify = verify;
1203
1204
441
  fd = open_fdt(filename, &handle->size);
1205
441
  if (fd < 0) {
1206
41
    ret = fd;
1207
41
    goto free_handle;
1208
41
  }
1209
1210
400
  handle->fit_alloc = malloc(handle->size);
1211
400
  if (!handle->fit_alloc) {
1212
0
    ret = -ENOMEM;
1213
0
    goto close_fd;
1214
0
  }
1215
1216
400
  nbytes = read_full(fd, handle->fit_alloc, handle->size);
1217
400
  if (nbytes < 0) {
1218
0
    ret = nbytes;
1219
0
    goto free_fit_alloc;
1220
0
  }
1221
400
  if (handle->size != nbytes) {
1222
0
    ret = -ENODATA;
1223
0
    goto free_fit_alloc;
1224
0
  }
1225
1226
400
  close(fd);
1227
1228
400
  handle->fit = handle->fit_alloc;
1229
400
  handle->filename = filename;
1230
1231
400
  refcount_set(&handle->users, 1);
1232
400
  list_add(&handle->entry, &open_fits);
1233
1234
400
  ret = fit_do_open(handle);
1235
400
  if (ret) {
1236
112
    fit_close(handle);
1237
112
    return ERR_PTR(ret);
1238
112
  }
1239
1240
288
  return handle;
1241
1242
0
free_fit_alloc:
1243
0
  free(handle->fit_alloc);
1244
0
close_fd:
1245
0
  close(fd);
1246
41
free_handle:
1247
41
  pr_err("unable to read %s: %pe\n", filename, ERR_PTR(ret));
1248
41
  free(filename);
1249
41
  free(handle);
1250
41
  return ERR_PTR(ret);
1251
1252
0
}
1253
1254
static bool __fit_close(struct fit_handle *handle)
1255
6.83k
{
1256
6.83k
  if (!refcount_dec_and_test(&handle->users))
1257
288
    return false;
1258
1259
6.54k
  if (handle->root)
1260
5.83k
    of_delete_node(handle->root);
1261
1262
6.54k
  if (handle->filename)
1263
400
    list_del(&handle->entry);
1264
1265
6.54k
  free(handle->filename);
1266
6.54k
  free(handle->fit_alloc);
1267
6.54k
  return true;
1268
6.83k
}
1269
1270
void fit_close(struct fit_handle *handle)
1271
6.83k
{
1272
6.83k
  if (__fit_close(handle))
1273
6.54k
    free(handle);
1274
6.83k
}
1275
1276
static int do_bootm_fit(struct image_data *data)
1277
0
{
1278
0
  pr_err("Cannot boot device tree binary blob\n");
1279
1280
0
  return -EINVAL;
1281
0
}
1282
1283
static bool fitimage_check(struct image_handler *handler,
1284
         struct image_data *data,
1285
         enum filetype detected_filetype)
1286
0
{
1287
0
  return detected_filetype == filetype_oftree ||
1288
0
    detected_filetype == filetype_fit;
1289
0
}
1290
1291
static struct image_handler fit_handler = {
1292
  .name = "FIT image",
1293
  .bootm = do_bootm_fit,
1294
  .filetype = filetype_fit,
1295
  .check_image = fitimage_check,
1296
};
1297
1298
static int bootm_fit_register(void)
1299
30
{
1300
30
  return register_image_handler(&fit_handler);
1301
30
}
1302
late_initcall(bootm_fit_register);
1303
1304
/*
1305
 * Verification modes selected by the control byte. All of them are
1306
 * reachable in practice, and which one is in effect decides whether
1307
 * hashes and signatures are merely checked when present or required.
1308
 */
1309
static const enum bootm_verify fuzz_verify_modes[] = {
1310
  BOOTM_VERIFY_NONE,
1311
  BOOTM_VERIFY_HASH,
1312
  BOOTM_VERIFY_SIGNATURE,
1313
  BOOTM_VERIFY_AVAILABLE,
1314
};
1315
1316
/*
1317
 * The image types a configuration can name. Each of these properties may
1318
 * list any number of images, so cap how many of them are opened.
1319
 */
1320
static const char * const fuzz_image_names[] = {
1321
  "kernel", "fdt", "ramdisk", "tee", "loadables",
1322
};
1323
1324
#define FUZZ_FIT_MAX_IMAGES 8
1325
1.61k
#define FUZZ_FIT_FILE   "/fuzz.fit"
1326
1327
/* The candidate filter bootm applies while a configuration is picked */
1328
static bool fuzz_fit_has_kernel(struct fit_handle *handle,
1329
        struct device_node *config)
1330
5.71k
{
1331
5.71k
  return fit_has_image(handle, config, "kernel");
1332
5.71k
}
1333
1334
/*
1335
 * bootm opens a FIT by name: open_fdt() checks the header against the file
1336
 * size and a second open of the same name is served from the handle cache.
1337
 */
1338
static struct fit_handle *fuzz_fit_open_file(const u8 *data, size_t size,
1339
               enum bootm_verify verify)
1340
441
{
1341
441
  struct fit_handle *handle, *cached;
1342
441
  int ret;
1343
1344
441
  ret = write_file(FUZZ_FIT_FILE, data, size);
1345
441
  if (ret)
1346
0
    return ERR_PTR(ret);
1347
1348
441
  handle = fit_open(FUZZ_FIT_FILE, false, verify);
1349
441
  if (!IS_ERR(handle)) {
1350
288
    cached = fit_open(FUZZ_FIT_FILE, false, verify);
1351
288
    if (!IS_ERR(cached))
1352
288
      fit_close(cached);
1353
288
  }
1354
1355
441
  unlink(FUZZ_FIT_FILE);
1356
1357
441
  return handle;
1358
441
}
1359
1360
static void fuzz_fit_open_images(struct fit_handle *handle, void *config)
1361
9.73k
{
1362
9.73k
  unsigned int i;
1363
1364
58.4k
  for (i = 0; i < ARRAY_SIZE(fuzz_image_names); i++) {
1365
48.6k
    const char *name = fuzz_image_names[i];
1366
48.6k
    const void *outdata;
1367
48.6k
    unsigned long outsize, addr;
1368
48.6k
    int idx, count;
1369
1370
    /*
1371
     * Without a configuration the image is looked up by its
1372
     * own name, so there is exactly one to open.
1373
     */
1374
48.6k
    count = config ? clamp(fit_count_images(handle, config, name),
1375
48.6k
               0, FUZZ_FIT_MAX_IMAGES) : 1;
1376
1377
97.2k
    for (idx = 0; idx < count; idx++)
1378
48.5k
      fit_open_image(handle, config, name, idx,
1379
48.5k
               &outdata, &outsize);
1380
1381
48.6k
    fit_get_image_address(handle, config, name, "load", &addr);
1382
48.6k
    fit_get_image_address(handle, config, name, "entry", &addr);
1383
48.6k
  }
1384
9.73k
}
1385
1386
/*
1387
 * A FIT that verifies goes on to run a kernel of its choosing, so what it
1388
 * does afterwards is not attack surface: this stops at fit_open_image(),
1389
 * where the hash and the image signature are checked. The hash comparison
1390
 * including its success path does matter, as a hash is trivially recomputed
1391
 * for modified data wherever signatures are not enforced.
1392
 */
1393
static int fuzz_fit(const u8 *data, size_t size)
1394
6.60k
{
1395
6.60k
  struct fit_handle *handle;
1396
6.60k
  enum bootm_verify verify;
1397
6.60k
  const char *unit;
1398
6.60k
  void *config;
1399
6.60k
  u8 ctrl = 0;
1400
1401
6.60k
  if (size < sizeof(struct fdt_header))
1402
14
    return 0;
1403
1404
  /*
1405
   * If the buffer is larger than the FDT claims to be, take the
1406
   * first trailing byte as a control byte to vary what the fuzzer
1407
   * exercises. Corpus entries that are exactly one FDT get 0.
1408
   * Input that is no FDT at all has no structure worth preserving,
1409
   * so there the last byte selects.
1410
   */
1411
6.58k
  if (fdt_magic(data) == FDT_MAGIC) {
1412
6.41k
    uint32_t fdt_size = fdt_totalsize(data);
1413
1414
6.41k
    if (fdt_size >= sizeof(struct fdt_header) && fdt_size < size) {
1415
875
      ctrl = data[fdt_size];
1416
875
      size = fdt_size;
1417
875
    }
1418
6.41k
  } else {
1419
174
    ctrl = data[size - 1];
1420
174
  }
1421
1422
6.58k
  verify = fuzz_verify_modes[ctrl % ARRAY_SIZE(fuzz_verify_modes)];
1423
1424
6.58k
  if (ctrl & BIT(2))
1425
441
    handle = fuzz_fit_open_file(data, size, verify);
1426
6.14k
  else
1427
6.14k
    handle = fit_open_buf(data, size, false, verify);
1428
6.58k
  if (IS_ERR(handle))
1429
784
    return 0;
1430
1431
  /*
1432
   * Opening without a name picks the configuration matching barebox'
1433
   * own compatible. Fall back to the last one, so images that describe
1434
   * a different machine are explored as well.
1435
   */
1436
5.80k
  config = fit_open_configuration(handle, NULL, fuzz_fit_has_kernel);
1437
5.80k
  if (IS_ERR(config) && !fit_find_last_unit(handle, &unit))
1438
4.23k
    config = fit_open_configuration(handle, unit, NULL);
1439
1440
5.80k
  if (!IS_ERR(config)) {
1441
3.93k
    fit_config_get_name(handle, config);
1442
3.93k
    fuzz_fit_open_images(handle, config);
1443
3.93k
  }
1444
1445
  /* Without a configuration an image's own signature is checked */
1446
5.80k
  fuzz_fit_open_images(handle, NULL);
1447
1448
5.80k
  fit_close(handle);
1449
1450
5.80k
  return 0;
1451
6.58k
}
1452
fuzz_test("fit", fuzz_fit);