-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathfuzz-netboot.c
More file actions
214 lines (174 loc) · 4.62 KB
/
fuzz-netboot.c
File metadata and controls
214 lines (174 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// SPDX-License-Identifier: BSD-2-Clause-Patent
/*
* fuzz-netboot.c - fuzz TFTP netboot code.
*/
#include <stdint.h>
#include <stdio.h>
#ifndef SHIM_UNIT_TEST
#define SHIM_UNIT_TEST
#endif
#include "shim.h"
extern EFI_PXE_BASE_CODE *pxe;
extern CHAR8 *full_path;
UINT8 mok_policy = 0;
UINTN hsi_status = 0;
/* A struct to track fuzzing input bytes */
typedef struct {
const uint8_t *data;
size_t len;
} state_t;
/* Consumes `len` fuzzing input bytes into `dst` */
static int
fuzzer_consume_bytes(state_t *state, void *dst, size_t len)
{
if (state->len < len)
return 1;
memcpy(dst, state->data, len);
state->data += len;
state->len -= len;
return 0;
}
/* Returns a random length of bytes that `state` is guaranteed to
* be able to satisfy */
static int
fuzzer_consume_len(state_t *state, size_t *len)
{
if (state->len <= sizeof(*len))
return 1;
fuzzer_consume_bytes(state, len, sizeof(*len));
*len %= state->len;
return 0;
}
/* Consumes a `BOOLEAN` from the fuzzing input bytes */
static int
fuzzer_consume_bool(state_t *state, BOOLEAN *b)
{
int ret, val = 0;
ret = fuzzer_consume_bytes(state, &val, 1);
if (!ret)
*b = val & 1;
return ret;
}
/* Global fuzzing state, set from LLVMFuzzerTestOneInput() so that
* mtftp_xfer() can access input bytes */
static state_t *gstate = NULL;
static EFI_STATUS EFIAPI
mtftp_xfer(struct _EFI_PXE_BASE_CODE_PROTOCOL *pxe,
EFI_PXE_BASE_CODE_TFTP_OPCODE op, VOID *buf,
BOOLEAN overwrite UNUSED, UINT64 *bufsize, UINT64 *blocksize UNUSED,
EFI_IP_ADDRESS *addr UNUSED, UINT8 *filename UNUSED,
EFI_PXE_BASE_CODE_MTFTP_INFO *info UNUSED, BOOLEAN dontusebuf UNUSED)
{
EFI_STATUS status;
size_t size;
unsigned int i;
EFI_PXE_BASE_CODE_TFTP_ERROR *error;
uint8_t c;
if (op != EFI_PXE_BASE_CODE_TFTP_READ_FILE) {
status = EFI_UNSUPPORTED;
goto out_err;
}
if (fuzzer_consume_len(gstate, &size)) {
status = EFI_TFTP_ERROR;
goto out_err;
}
if (*bufsize < size) {
status = EFI_BUFFER_TOO_SMALL;
goto out_err;
}
fuzzer_consume_bytes(gstate, buf, size);
*bufsize = size;
return EFI_SUCCESS;
out_err:
pxe->Mode->TftpErrorReceived = 1;
error = &pxe->Mode->TftpError;
for (i = 0;
i < sizeof(error->ErrorString) / sizeof(error->ErrorString[0]);
++i) {
if (fuzzer_consume_bytes(gstate, &c, sizeof(c)))
error->ErrorString[i] = c;
}
return status;
}
static int
fuzzer_init_mode(state_t *state, EFI_PXE_BASE_CODE_MODE *mode)
{
#define FUZZ_GET_BOOL(_state, _dst) \
do { \
if (fuzzer_consume_bool(_state, _dst) != 0) \
goto out; \
} while (0)
#define FUZZ_GET_BYTES(_state, _dst) \
do { \
if (fuzzer_consume_bytes(_state, _dst, sizeof(*_dst)) != 0) \
goto out; \
} while (0)
memset(mode, 0, sizeof(*mode));
FUZZ_GET_BOOL(state, &mode->DhcpAckReceived);
FUZZ_GET_BOOL(state, &mode->ProxyOfferReceived);
FUZZ_GET_BOOL(state, &mode->PxeReplyReceived);
FUZZ_GET_BOOL(state, &mode->UsingIpv6);
if (mode->UsingIpv6) {
FUZZ_GET_BYTES(state, &mode->DhcpAck.Dhcpv6);
FUZZ_GET_BYTES(state, &mode->PxeReply.Dhcpv6);
FUZZ_GET_BYTES(state, &mode->ProxyOffer.Dhcpv6);
} else {
FUZZ_GET_BYTES(state, &mode->DhcpAck.Dhcpv4);
FUZZ_GET_BYTES(state, &mode->PxeReply.Dhcpv4);
FUZZ_GET_BYTES(state, &mode->ProxyOffer.Dhcpv4);
}
return 0;
out:
return -1;
}
static char *
fuzzer_create_name(state_t *state)
{
char *name;
size_t name_len = 0;
if (fuzzer_consume_len(state, &name_len) || !name_len)
return NULL;
name = calloc(1, name_len);
if (name)
fuzzer_consume_bytes(state, name, name_len - 1);
return name;
}
static int
fuzzer_main(state_t *state)
{
EFI_STATUS status;
char *name = NULL, *netbootname;
void *sourcebuffer = NULL;
UINT64 sourcesize;
if (fuzzer_init_mode(state, pxe->Mode))
return -1;
name = fuzzer_create_name(state);
netbootname = name ? name : "boot64.efi";
status = parseNetbootinfo(NULL, netbootname);
if (EFI_ERROR(status))
goto out;
status = FetchNetbootimage(NULL, &sourcebuffer, &sourcesize, 0);
if (!EFI_ERROR(status))
FreePool(sourcebuffer);
out:
if (full_path) {
FreePool(full_path);
full_path = NULL;
}
if (name)
free(name);
return 0;
}
static EFI_PXE_BASE_CODE_MODE fuzz_mode = { 0 };
static EFI_PXE_BASE_CODE fuzz_pxe = {
.Mtftp = mtftp_xfer,
.Mode = &fuzz_mode,
};
int
LLVMFuzzerTestOneInput(const UINT8 *data, size_t size)
{
state_t state = { .data = data, .len = size };
pxe = &fuzz_pxe;
gstate = &state;
return fuzzer_main(&state);
}