Coverage Report

Created: 2025-03-06 07:58

/src/wget/fuzz/wget_ftpls_fuzzer.c
Line
Count
Source (jump to first uncovered line)
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
0
{
54
0
  return fopen("/dev/null", mode);
55
0
}
56
57
FILE *fopen_wgetrc(const char *pathname, const char *mode)
58
0
{
59
0
  return NULL;
60
0
}
61
62
static int do_jump;
63
static jmp_buf jmpbuf;
64
#ifdef FUZZING
65
void exit_wget(int status)
66
0
{
67
0
  longjmp(jmpbuf, 1);
68
0
}
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
3.35k
{
87
3.35k
#ifdef HAVE_FMEMOPEN
88
3.35k
  FILE *fp;
89
3.35k
  struct fileinfo *fi;
90
91
3.35k
  if (size > 4096) // same as max_len = ... in .options file
92
19
    return 0;
93
94
3.33k
  fp = fmemopen((void *) data, size, "r");
95
3.33k
  if (!fp) return 0;
96
97
3.33k
  CLOSE_STDERR
98
99
3.33k
  do_jump = 1;
100
101
3.33k
  if (setjmp(jmpbuf))
102
0
    goto done;
103
104
3.33k
  fi = ftp_parse_ls_fp(fp, ST_UNIX);
105
3.33k
  freefileinfo(fi);
106
3.33k
  rewind(fp);
107
108
3.33k
  fi = ftp_parse_ls_fp(fp, ST_VMS);
109
3.33k
  freefileinfo(fi);
110
3.33k
  rewind(fp);
111
112
3.33k
  fi = ftp_parse_ls_fp(fp, ST_WINNT);
113
3.33k
  freefileinfo(fi);
114
3.33k
  rewind(fp);
115
116
3.33k
  fi = ftp_parse_ls_fp(fp, ST_MACOS);
117
118
3.33k
done:
119
3.33k
  freefileinfo(fi);
120
3.33k
  fclose(fp);
121
122
3.33k
  do_jump = 0;
123
124
3.33k
  RESTORE_STDERR
125
3.33k
#endif
126
3.33k
  return 0;
127
3.33k
}