Debian Codegraph @

fuzz/wget_ftpls_fuzzer.c

raw
1 /*
2 * Copyright (c) 2017-2024 Free Software Foundation, Inc.
3 *
4 * This file is part of GNU Wget.
5 *
6 * GNU Wget is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GNU Wget is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Wget. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21
22 #include <sys/types.h>
23 #include <dirent.h> // opendir, readdir
24 #include <stdint.h> // uint8_t
25 #include <stdio.h> // fmemopen
26 #include <string.h> // strncmp
27 #include <stdlib.h> // free
28 #include <fcntl.h> // open flags
29 #include <unistd.h> // close
30 #include <setjmp.h> // longjmp, setjmp
31
32 #include "wget.h"
33 #undef fopen_wgetrc
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 #include "ftp.h"
39
40 // declarations for wget internal functions
41 int main_wget(int argc, const char **argv);
42 void cleanup(void);
43 FILE *fopen_wget(const char *pathname, const char *mode);
44 FILE *fopen_wgetrc(const char *pathname, const char *mode);
45 void exit_wget(int status);
46 #ifdef __cplusplus
47 }
48 #endif
49
50 #include "fuzzer.h"
51
52 FILE *fopen_wget(const char *pathname, const char *mode)
53 {
54 return fopen("/dev/null", mode);
55 }
56
57 FILE *fopen_wgetrc(const char *pathname, const char *mode)
58 {
59 return NULL;
60 }
61
62 static int do_jump;
63 static jmp_buf jmpbuf;
64 #ifdef FUZZING
65 void exit_wget(int status)
66 {
67 longjmp(jmpbuf, 1);
68 }
69 #elif defined HAVE_DLFCN_H
70 #include <dlfcn.h> // dlsym
71 #ifndef RTLD_NEXT
72 #define RTLD_NEXT RTLD_GLOBAL
73 #endif
74 void exit(int status)
75 {
76 if (do_jump) {
77 longjmp(jmpbuf, 1);
78 } else {
79 void (*libc_exit)(int) = (void(*)(int)) dlsym (RTLD_NEXT, "exit");
80 libc_exit(status);
81 }
82 }
83 #endif
84
85 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
86 {
87 #ifdef HAVE_FMEMOPEN
88 FILE *fp;
89 struct fileinfo *fi;
90
91 if (size > 4096) // same as max_len = ... in .options file
92 return 0;
93
94 fp = fmemopen((void *) data, size, "r");
95 if (!fp) return 0;
96
97 CLOSE_STDERR
98
99 do_jump = 1;
100
101 if (setjmp(jmpbuf))
102 goto done;
103
104 fi = ftp_parse_ls_fp(fp, ST_UNIX);
105 freefileinfo(fi);
106 rewind(fp);
107
108 fi = ftp_parse_ls_fp(fp, ST_VMS);
109 freefileinfo(fi);
110 rewind(fp);
111
112 fi = ftp_parse_ls_fp(fp, ST_WINNT);
113 freefileinfo(fi);
114 rewind(fp);
115
116 fi = ftp_parse_ls_fp(fp, ST_MACOS);
117
118 done:
119 freefileinfo(fi);
120 fclose(fp);
121
122 do_jump = 0;
123
124 RESTORE_STDERR
125 #endif
126 return 0;
127 }