Coverage Report

Created: 2026-11-13 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-oss-fuzz/harnesses/zip.c
Line
Count
Source
1
/*
2
 * libFuzzer harness for R's bundled zip reader.
3
 *
4
 * src/main/dounzip.c carries a copy of minizip (unzOpen, the central
5
 * directory search, local-header coherency checks, unzReadCurrentFile)
6
 * plus R's own listing and extraction loops on top of it.  It runs on
7
 * every utils::unzip() and unz() call, which is how binary packages
8
 * and download.file() payloads get unpacked -- the archive is untrusted
9
 * by construction.  The copy is private to R, so nobody else fuzzes it.
10
 *
11
 * The slots cover listing, extraction into a per-process scratch
12
 * directory, and reading the first entry through an unz() connection in
13
 * both binary and text mode.  unzip is pinned to "internal" so the
14
 * harness can never shell out to a system unzip.  The input is staged in
15
 * a per-process scratch file since the entry points only take paths
16
 * (see common.h).
17
 */
18
19
#include <stdint.h>
20
#include <string.h>
21
22
#include "common.h"
23
24
23.2k
#define FUZZ_MAX_INPUT (1024 * 64)
25
23.1k
#define N_CALLS 4
26
27
/* Largest uncompressed size the extraction and text-read slots accept.
28
 * minizip inflates an entry only up to the size its central-directory
29
 * record states, which is what the listing reports as Length; bzip2
30
 * entries let a few hundred bytes state, and produce, tens of megabytes,
31
 * which takes longer than the timeout to write or read under ASAN. */
32
2
#define FUZZ_MAX_INFLATED (1024 * 1024)
33
34
/* Both paths stay reachable from here for the life of the process, so
35
 * LeakSanitizer does not report them at exit. */
36
static char *scratch_path;
37
static char *scratch_dir;
38
static SEXP calls[N_CALLS];
39
40
/* Extraction needs a directory of its own.  junkpaths = TRUE keeps
41
 * every entry inside it regardless of the path stored in the archive,
42
 * and the wrapper empties it again on exit (error or not) so one
43
 * iteration's files never collide with the next.  Output volume is
44
 * bounded by FUZZ_MAX_INFLATED. */
45
static char *make_scratch_dir(void)
46
2
{
47
2
    const char *tmpdir = getenv("TMPDIR");
48
2
    if (tmpdir == NULL || *tmpdir == '\0')
49
2
        tmpdir = "/tmp";
50
51
2
    char *path = malloc(PATH_MAX);
52
2
    if (path == NULL)
53
0
        return NULL;
54
2
    snprintf(path, PATH_MAX, "%s/fuzz-zip-exdir-XXXXXX", tmpdir);
55
2
    if (mkdtemp(path) == NULL) {
56
0
        free(path);
57
0
        return NULL;
58
0
    }
59
2
    return path;
60
2
}
61
62
int fuzz_target_init(int *argc, char ***argv)
63
2
{
64
2
    (void)argc;
65
2
    (void)argv;
66
2
    fuzz_init_r();
67
68
2
    scratch_path = fuzz_scratch_file("zip");
69
2
    scratch_dir = make_scratch_dir();
70
2
    if (scratch_path == NULL || scratch_dir == NULL)
71
0
        abort();
72
73
    /* [0] listing only: central directory walk (ziplist).
74
     * [1] extraction: local headers and inflate (zipunzip/extract_one).
75
     * [2] unz() connection, binary read of the first entry.
76
     * [3] unz() connection, text read of the first entry.
77
     * readBin's count bounds slot 2; readLines(n =) bounds lines, not
78
     * their length, so slots 1 and 3 check the listed sizes against cap
79
     * first.  An archive whose listing fails is not extracted: extraction
80
     * walks the same central directory, which slot 0 covers. */
81
2
    static const char *const sources[N_CALLS] = {
82
2
        "function(path, exdir, cap) "
83
2
        "utils::unzip(path, list = TRUE, unzip = \"internal\")",
84
85
2
        "function(path, exdir, cap) { "
86
2
        "l <- utils::unzip(path, list = TRUE, unzip = \"internal\"); "
87
2
        "if (sum(l$Length) > cap) return(NULL); "
88
2
        "on.exit(unlink(list.files(exdir, all.files = TRUE, "
89
2
        "full.names = TRUE, no.. = TRUE), recursive = TRUE)); "
90
2
        "utils::unzip(path, exdir = exdir, junkpaths = TRUE, "
91
2
        "unzip = \"internal\") }",
92
93
2
        "function(path, exdir, cap) { "
94
2
        "l <- utils::unzip(path, list = TRUE, unzip = \"internal\"); "
95
2
        "if (nrow(l)) { con <- unz(path, l$Name[1], open = \"rb\"); "
96
2
        "on.exit(close(con)); readBin(con, \"raw\", 65536L) } }",
97
98
2
        "function(path, exdir, cap) { "
99
2
        "l <- utils::unzip(path, list = TRUE, unzip = \"internal\"); "
100
2
        "if (nrow(l) && l$Length[1] <= cap) { con <- unz(path, l$Name[1]); "
101
2
        "on.exit(close(con)); readLines(con, n = 1024L, warn = FALSE) } }",
102
2
    };
103
2
    SEXP path, exdir_string, cap;
104
2
    Rf_protect(path = Rf_mkString(scratch_path));
105
2
    Rf_protect(exdir_string = Rf_mkString(scratch_dir));
106
2
    Rf_protect(cap = Rf_ScalarInteger(FUZZ_MAX_INFLATED));
107
10
    for (int i = 0; i < N_CALLS; i++) {
108
8
        SEXP wrapper;
109
8
        Rf_protect(wrapper = fuzz_make_wrapper(sources[i]));
110
8
        Rf_protect(calls[i] = Rf_lang4(wrapper, path, exdir_string, cap));
111
8
    }
112
2
    fuzz_label_calls(calls, N_CALLS);
113
2
    return 0;
114
2
}
115
116
int fuzz_target_run(const uint8_t *data, size_t size)
117
23.2k
{
118
23.2k
    if (size < 2 || size > FUZZ_MAX_INPUT)
119
39
        return 0;
120
121
23.1k
    if (!fuzz_write_scratch(scratch_path, data + 1, size - 1))
122
0
        return 0;
123
124
23.1k
    fuzz_eval_silent(calls[data[0] % N_CALLS], R_GlobalEnv);
125
23.1k
    return 0;
126
23.1k
}