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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
#include <nxt_sha1.h>
#include <nxt_websocket.h>
#include <nxt_websocket_header.h>
/* DO NOT TRY THIS AT HOME! */
#include <nxt_websocket_accept.c>
#define KMININPUTLENGTH 4
#define KMAXINPUTLENGTH 128
extern int LLVMFuzzerInitialize(int *argc, char ***argv);
extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
void nxt_base64_fuzz(const u_char *data, size_t size);
void nxt_djb_hash_fuzz(const u_char *data, size_t size);
void nxt_murmur_hash2_fuzz(const u_char *data, size_t size);
void nxt_parse_fuzz(const u_char *data, size_t size);
void nxt_sha1_fuzz(const u_char *data, size_t size);
void nxt_sha1_update_fuzz(const u_char *data, size_t size);
void nxt_term_fuzz(const u_char *data, size_t size);
void nxt_time_fuzz(const u_char *data, size_t size);
void nxt_uri_fuzz(const u_char *data, size_t size);
void nxt_utf8_fuzz(const u_char *data, size_t size);
void nxt_websocket_base64_fuzz(const u_char *data, size_t size);
void nxt_websocket_frame_fuzz(const u_char *data, size_t size);
extern char **environ;
int
LLVMFuzzerInitialize(int *argc, char ***argv)
{
if (nxt_lib_start("fuzzing", NULL, &environ) != NXT_OK) {
return NXT_ERROR;
}
return 0;
}
int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
if (size < KMININPUTLENGTH || size > KMAXINPUTLENGTH) {
return 0;
}
nxt_base64_fuzz(data, size);
nxt_djb_hash_fuzz(data, size);
nxt_murmur_hash2_fuzz(data, size);
nxt_parse_fuzz(data, size);
nxt_sha1_fuzz(data, size);
nxt_sha1_update_fuzz(data, size);
nxt_term_fuzz(data, size);
nxt_time_fuzz(data, size);
nxt_uri_fuzz(data, size);
nxt_utf8_fuzz(data, size);
nxt_websocket_base64_fuzz(data, size);
nxt_websocket_frame_fuzz(data, size);
return 0;
}
void
nxt_base64_fuzz(const u_char *data, size_t size)
{
u_char buf[256];
ssize_t ret;
/*
* Validate base64 data before decoding.
*/
ret = nxt_base64_decode(NULL, (u_char *)data, size);
if (ret == NXT_ERROR) {
return;
}
nxt_base64_decode(buf, (u_char *)data, size);
}
void
nxt_djb_hash_fuzz(const u_char *data, size_t size)
{
nxt_djb_hash(data, size);
nxt_djb_hash_lowcase(data, size);
}
void
nxt_murmur_hash2_fuzz(const u_char *data, size_t size)
{
nxt_murmur_hash2(data, size);
nxt_murmur_hash2_uint32(data);
}
void
nxt_parse_fuzz(const u_char *data, size_t size)
{
nxt_str_t input;
input.start = (u_char *)data;
input.length = size;
nxt_int_parse(data, size);
nxt_size_t_parse(data, size);
nxt_size_parse(data, size);
nxt_off_t_parse(data, size);
nxt_str_int_parse(&input);
nxt_number_parse(&data, data + size);
}
void
nxt_sha1_fuzz(const u_char *data, size_t size)
{
u_char bin_accept[20];
nxt_sha1_t ctx;
nxt_sha1_init(&ctx);
nxt_sha1_update(&ctx, data, size);
nxt_sha1_final(bin_accept, &ctx);
}
void
nxt_sha1_update_fuzz(const u_char *data, size_t size)
{
u_char bin_accept[20];
nxt_sha1_t ctx;
nxt_sha1_init(&ctx);
nxt_sha1_update(&ctx, data, size);
nxt_sha1_update(&ctx, data, size);
nxt_sha1_final(bin_accept, &ctx);
}
void
nxt_term_fuzz(const u_char *data, size_t size)
{
nxt_term_parse(data, size, 0);
nxt_term_parse(data, size, 1);
}
void
nxt_time_fuzz(const u_char *data, size_t size)
{
nxt_time_parse(data, size);
}
void
nxt_uri_fuzz(const u_char *data, size_t size)
{
u_char *dst;
dst = nxt_zalloc(size * 3);
if (dst == NULL) {
return;
}
nxt_decode_uri(dst, (u_char *)data, size);
nxt_decode_uri_plus(dst, (u_char *)data, size);
nxt_memzero(dst, size * 3);
nxt_encode_uri(NULL, (u_char *)data, size);
nxt_encode_uri(dst, (u_char *)data, size);
nxt_free(dst);
}
void
nxt_utf8_fuzz(const u_char *data, size_t size)
{
const u_char *in;
in = data;
nxt_utf8_decode(&in, data + size);
nxt_utf8_casecmp((const u_char *)"ABC АБВ ΑΒΓ",
data,
nxt_length("ABC АБВ ΑΒΓ"),
size);
}
void
nxt_websocket_base64_fuzz(const u_char *data, size_t size)
{
u_char *out;
out = nxt_zalloc(size * 2);
if (out == NULL) {
return;
}
nxt_websocket_base64_encode(out, data, size);
nxt_free(out);
}
void
nxt_websocket_frame_fuzz(const u_char *data, size_t size)
{
u_char *input;
/*
* Resolve overwrites-const-input by using a copy of the data.
*/
input = nxt_malloc(size);
if (input == NULL) {
return;
}
nxt_memcpy(input, data, size);
nxt_websocket_frame_init(input, 0);
nxt_websocket_frame_header_size(input);
nxt_websocket_frame_payload_len(input);
nxt_free(input);
}
|