Debian Codegraph @

src/fuzz/fuzz-bootspec.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <string.h>
4
5 #include "sd-json.h"
6
7 #include "alloc-util.h"
8 #include "bootspec.h"
9 #include "env-util.h"
10 #include "escape.h"
11 #include "fd-util.h"
12 #include "fuzz.h"
13 #include "json-util.h"
14 #include "strv.h"
15
16 static int json_dispatch_config(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
17 BootConfig *config = ASSERT_PTR(userdata);
18
19 const char *s = sd_json_variant_string(variant);
20 if (!s)
21 return -EINVAL;
22
23 _cleanup_fclose_ FILE *f = NULL;
24 assert_se(f = data_to_file((const uint8_t*) s, strlen(s)));
25
26 (void) boot_loader_read_conf(config, f, "memstream");
27 return 0;
28 }
29
30 static int json_dispatch_entries(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
31 BootConfig *config = ASSERT_PTR(userdata);
32 sd_json_variant *entry;
33
34 JSON_VARIANT_ARRAY_FOREACH(entry, variant) {
35 if (!sd_json_variant_is_array(entry) ||
36 sd_json_variant_elements(entry) < 1)
37 return -EINVAL;
38
39 sd_json_variant *v;
40 const char *id = NULL, *raw = NULL;
41 _cleanup_free_ char *data = NULL;
42 ssize_t len = -ENODATA;
43
44 v = sd_json_variant_by_index(entry, 0);
45 if (v)
46 id = sd_json_variant_string(v);
47 if (!id)
48 continue;
49
50 v = sd_json_variant_by_index(entry, 1);
51 if (v)
52 raw = sd_json_variant_string(v);
53 if (raw)
54 len = cunescape(raw, UNESCAPE_RELAX | UNESCAPE_ACCEPT_NUL, &data);
55 if (len >= 0) {
56 _cleanup_fclose_ FILE *f = NULL;
57 assert_se(f = data_to_file((const uint8_t*) data, len));
58
59 assert_se(boot_config_load_type1(config, f, "/", BOOT_ENTRY_ESP, "/entries", id) != -ENOMEM);
60 }
61 }
62
63 return 0;
64 }
65
66 static int json_dispatch_loader(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
67 BootConfig *config = ASSERT_PTR(userdata);
68 _cleanup_strv_free_ char **entries = NULL;
69 int r;
70
71 r = sd_json_dispatch_strv(name, variant, flags, &entries);
72 if (r < 0)
73 return r;
74
75 (void) boot_config_augment_from_loader(config, entries, false);
76 return 0;
77 }
78
79 static const sd_json_dispatch_field data_dispatch[] = {
80 { "config", SD_JSON_VARIANT_STRING, json_dispatch_config, 0, 0 },
81 { "entries", SD_JSON_VARIANT_ARRAY, json_dispatch_entries, 0, 0 },
82 { "loader", SD_JSON_VARIANT_ARRAY, json_dispatch_loader, 0, 0 },
83 {}
84 };
85
86 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
87 _cleanup_free_ const char *datadup = NULL;
88 _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
89 int r;
90
91 if (outside_size_range(size, 0, 65536))
92 return 0;
93
94 fuzz_setup_logging();
95
96 assert_se(datadup = memdup_suffix0(data, size));
97
98 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
99 r = sd_json_parse(datadup, 0, &v, NULL, NULL);
100 if (r < 0)
101 return 0;
102
103 r = sd_json_dispatch(v, data_dispatch, 0, &config);
104 if (r < 0)
105 return 0;
106
107 assert_se(boot_config_finalize(&config) >= 0);
108
109 (void) boot_config_select_special_entries(&config, /* skip_efivars= */ false);
110
111 _cleanup_close_ int orig_stdout_fd = -EBADF;
112 if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) {
113 orig_stdout_fd = fcntl(fileno(stdout), F_DUPFD_CLOEXEC, 3);
114 if (orig_stdout_fd < 0)
115 log_warning_errno(orig_stdout_fd, "Failed to duplicate fd 1: %m");
116 else
117 assert_se(freopen("/dev/null", "w", stdout));
118 }
119
120 (void) show_boot_entries(&config, SD_JSON_FORMAT_OFF);
121 (void) show_boot_entries(&config, SD_JSON_FORMAT_PRETTY);
122
123 if (orig_stdout_fd >= 0)
124 assert_se(freopen(FORMAT_PROC_FD_PATH(orig_stdout_fd), "w", stdout));
125
126 return 0;
127 }