Debian Codegraph @

src/fuzz/fuzz-pe-binary.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /* Fuzz target for userspace UKI / PE inspection.
4 *
5 * pe_load_headers / pe_load_sections / pe_read_section_data_by_name / uki_hash
6 * are exercised by bootctl, systemd-measure, pcrlock, kernel-install and
7 * reboot-util against UKIs that an unprivileged actor may have produced.
8 *
9 * Expected input: bytes that look like a PE/COFF file (DOS "MZ" header,
10 * `e_lfanew` pointing to a "PE\0\0" signature, followed by IMAGE_FILE_HEADER,
11 * IMAGE_OPTIONAL_HEADER and a section table). The harness wraps the bytes in
12 * a memfd, walks the headers, then attempts to read the known UKI sections
13 * and finally hashes them via uki_hash() when OpenSSL is available.
14 */
15
16 #include <unistd.h>
17
18 #include "alloc-util.h"
19 #include "crypto-util.h"
20 #include "fd-util.h"
21 #include "fuzz.h"
22 #include "memfd-util.h"
23 #include "pe-binary.h"
24 #include "tests.h"
25 #include "uki.h"
26
27 /* Cap section reads so a crafted VirtualSize cannot drive a multi-GiB malloc
28 * and OOM-kill the fuzzer. Well under the 16 MiB input limit; real code uses
29 * PE_SECTION_READ_MAX (16 KiB) or smaller. */
30 #define FUZZ_SECTION_READ_MAX (1U*1024U*1024U)
31
32 static void fuzz_read_section(
33 int fd,
34 const PeHeader *pe_header,
35 const IMAGE_SECTION_HEADER *sections,
36 const char *name) {
37
38 _cleanup_free_ void *buf = NULL;
39 size_t buf_size = 0;
40
41 (void) pe_read_section_data_by_name(fd, pe_header, sections, name, FUZZ_SECTION_READ_MAX, &buf, &buf_size);
42 DO_NOT_OPTIMIZE(buf);
43 DO_NOT_OPTIMIZE(buf_size);
44 }
45
46 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
47 _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL;
48 _cleanup_free_ PeHeader *pe_header = NULL;
49 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
50
51 if (outside_size_range(size, 0, 16 * 1024 * 1024))
52 return 0;
53
54 fuzz_setup_logging();
55
56 _cleanup_close_ int fd = ASSERT_OK(memfd_new("fuzz-pe-binary"));
57 if (size > 0)
58 ASSERT_OK_EQ_ERRNO(write(fd, data, size), (ssize_t) size);
59 ASSERT_OK_ERRNO(lseek(fd, 0, SEEK_SET));
60
61 if (pe_load_headers(fd, &dos_header, &pe_header) < 0)
62 return 0;
63
64 if (pe_load_sections(fd, dos_header, pe_header, &sections) < 0)
65 return 0;
66
67 /* Exercise the section-read path for every UKI section. unified_sections[]
68 * (uki.h) is the canonical list; .sdmagic is written by sd-boot/sd-stub but
69 * is not part of that table, so read it as an extra step. */
70 FOREACH_ARRAY(s, unified_sections, _UNIFIED_SECTION_MAX)
71 fuzz_read_section(fd, pe_header, sections, *s);
72 fuzz_read_section(fd, pe_header, sections, ".sdmagic");
73
74 (void) pe_is_uki(pe_header, sections);
75 (void) pe_is_addon(pe_header, sections);
76 (void) pe_is_native(pe_header);
77
78 #if HAVE_OPENSSL
79 if (dlopen_libcrypto(LOG_DEBUG) >= 0) {
80 void *hashes[_UNIFIED_SECTION_MAX] = {};
81 size_t hash_size = 0;
82
83 /* uki_hash() can return partway through with some hashes already
84 * allocated; free unconditionally. */
85 (void) uki_hash(fd, sym_EVP_sha256(), hashes, &hash_size);
86 free_many(hashes, _UNIFIED_SECTION_MAX);
87 }
88 #endif
89
90 return 0;
91 }