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
|
/*
* Copyright (c) 2019-2022 Free Software Foundation, Inc.
*
* This file is part of GNU Wget.
*
* GNU Wget is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GNU Wget is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wget. If not, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <sys/types.h>
#include <stdint.h> // uint8_t
#include <stdio.h> // fmemopen
#include <string.h> // strncmp
#include <stdlib.h> // free
#include <unistd.h> // close
#include <fcntl.h> // open flags
#include <unistd.h> // close
#include <unistd.h> // close
#include "wget.h"
#include "connect.h"
#undef fopen_wgetrc
#ifdef __cplusplus
extern "C" {
#endif
#include "retr.h"
// declarations for wget internal functions
int main_wget(int argc, const char **argv);
void cleanup(void);
// FILE *fopen_wget(const char *pathname, const char *mode);
// FILE *fopen_wgetrc(const char *pathname, const char *mode);
void exit_wget(int status);
#ifdef __cplusplus
}
#endif
#include "fuzzer.h"
FILE *fopen_wget(const char *pathname, const char *mode)
{
return fopen("/dev/null", mode);
}
FILE *fopen_wgetrc(const char *pathname, const char *mode)
{
return NULL;
}
#ifdef FUZZING
void exit_wget(int status)
{
}
#endif
static const uint8_t *g_data;
static size_t g_size, g_read;
struct my_context {
int peeklen;
char peekbuf[512];
};
static int my_peek (int fd _GL_UNUSED, char *buf, int bufsize, void *arg, double d)
{
(void) d;
if (g_read < g_size) {
struct my_context *ctx = (struct my_context *) arg;
int n = rand() % (g_size - g_read);
if (n > bufsize)
n = bufsize;
if (n > (int) sizeof(ctx->peekbuf))
n = sizeof(ctx->peekbuf);
memcpy(buf, g_data + g_read, n);
memcpy(ctx->peekbuf, g_data + g_read, n);
g_read += n;
ctx->peeklen=n;
return n;
}
return 0;
}
static int my_read (int fd _GL_UNUSED, char *buf, int bufsize, void *arg, double d)
{
(void) d;
struct my_context *ctx = (struct my_context *) arg;
if (ctx->peeklen) {
/* If we have any peek data, simply return that. */
int copysize = MIN (bufsize, ctx->peeklen);
memcpy (buf, ctx->peekbuf, copysize);
ctx->peeklen -= copysize;
if (ctx->peeklen)
memmove (ctx->peekbuf, ctx->peekbuf + copysize, ctx->peeklen);
return copysize;
}
if (g_read < g_size) {
int n = rand() % (g_size - g_read);
if (n > bufsize)
n = bufsize;
memcpy(buf, g_data + g_read, n);
g_read += n;
return n;
}
return 0;
}
static int my_write (int fd _GL_UNUSED, char *buf _GL_UNUSED, int bufsize, void *arg _GL_UNUSED)
{
return bufsize;
}
static int my_poll (int fd _GL_UNUSED, double timeout _GL_UNUSED, int wait_for _GL_UNUSED, void *arg)
{
struct my_context *ctx = (struct my_context *) arg;
return ctx->peeklen || g_read < g_size;
}
static const char *my_errstr (int fd _GL_UNUSED, void *arg _GL_UNUSED)
{
return "Success";
}
static void my_close (int fd _GL_UNUSED, void *arg _GL_UNUSED)
{
}
static struct transport_implementation my_transport =
{
my_read, my_write, my_poll,
my_peek, my_errstr, my_close
};
/* copied from wget's http.c */
static const char *
response_head_terminator (const char *start, const char *peeked, int peeklen)
{
const char *p, *end;
/* If at first peek, verify whether HUNK starts with "HTTP". If
not, this is a HTTP/0.9 request and we must bail out without
reading anything. */
if (start == peeked && 0 != memcmp (start, "HTTP", MIN (peeklen, 4)))
return start;
/* Look for "\n[\r]\n", and return the following position if found.
Start two chars before the current to cover the possibility that
part of the terminator (e.g. "\n\r") arrived in the previous
batch. */
p = peeked - start < 2 ? start : peeked - 2;
end = peeked + peeklen;
/* Check for \n\r\n or \n\n anywhere in [p, end-2). */
for (; p < end - 2; p++)
if (*p == '\n')
{
if (p[1] == '\r' && p[2] == '\n')
return p + 3;
else if (p[1] == '\n')
return p + 2;
}
/* p==end-2: check for \n\n directly preceding END. */
if (peeklen >= 2 && p[0] == '\n' && p[1] == '\n')
return p + 2;
return NULL;
}
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
char *hunk;
if (size > 4096) // same as max_len = ... in .options file
return 0;
// CLOSE_STDERR
g_data = data;
g_size = size;
g_read = 0;
struct my_context *ctx = (struct my_context *) calloc(1, sizeof(struct my_context));
fd_register_transport(99, &my_transport, ctx);
while ((hunk = fd_read_hunk(99, response_head_terminator, 512, 65536)))
free(hunk);
connect_cleanup();
free(ctx);
// RESTORE_STDERR
return 0;
}
|