/src/gdal/fuzzers/filename_fuzzer.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL |
4 | | * Purpose: Fuzzer |
5 | | * Author: Even Rouault, even.rouault at spatialys.com |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2022, Even Rouault <even.rouault at spatialys.com> |
9 | | * |
10 | | * Permission is hereby granted, free of charge, to any person obtaining a |
11 | | * copy of this software and associated documentation files (the "Software"), |
12 | | * to deal in the Software without restriction, including without limitation |
13 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
14 | | * and/or sell copies of the Software, and to permit persons to whom the |
15 | | * Software is furnished to do so, subject to the following conditions: |
16 | | * |
17 | | * The above copyright notice and this permission notice shall be included |
18 | | * in all copies or substantial portions of the Software. |
19 | | * |
20 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
21 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
23 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
25 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
26 | | * DEALINGS IN THE SOFTWARE. |
27 | | ****************************************************************************/ |
28 | | |
29 | | #include <stddef.h> |
30 | | #include <stdint.h> |
31 | | |
32 | | #include "gdal.h" |
33 | | #include "cpl_conv.h" |
34 | | #include "cpl_string.h" |
35 | | #include "cpl_vsi.h" |
36 | | #include "gdal_priv.h" |
37 | | |
38 | | extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv); |
39 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len); |
40 | | |
41 | | int LLVMFuzzerInitialize(int * /*argc*/, char ***argv) |
42 | 78 | { |
43 | 78 | const char *exe_path = (*argv)[0]; |
44 | 78 | if (CPLGetConfigOption("GDAL_DATA", nullptr) == nullptr) |
45 | 78 | { |
46 | 78 | CPLSetConfigOption("GDAL_DATA", CPLGetPathSafe(exe_path).c_str()); |
47 | 78 | } |
48 | 78 | CPLSetConfigOption("CPL_TMPDIR", "/tmp"); |
49 | 78 | CPLSetConfigOption("DISABLE_OPEN_REAL_NETCDF_FILES", "YES"); |
50 | | // Disable PDF text rendering as fontconfig cannot access its config files |
51 | 78 | CPLSetConfigOption("GDAL_PDF_RENDERING_OPTIONS", "RASTER,VECTOR"); |
52 | | // to avoid timeout in WMS driver |
53 | 78 | CPLSetConfigOption("GDAL_WMS_ABORT_CURL_REQUEST", "YES"); |
54 | 78 | CPLSetConfigOption("GDAL_HTTP_TIMEOUT", "1"); |
55 | 78 | CPLSetConfigOption("GDAL_HTTP_CONNECTTIMEOUT", "1"); |
56 | 78 | CPLSetConfigOption("GDAL_CACHEMAX", "1000"); // Limit to 1 GB |
57 | | #ifdef GTIFF_USE_MMAP |
58 | | CPLSetConfigOption("GTIFF_USE_MMAP", "YES"); |
59 | | #endif |
60 | | |
61 | | #ifdef GDAL_SKIP |
62 | | CPLSetConfigOption("GDAL_SKIP", GDAL_SKIP); |
63 | | #endif |
64 | 78 | GDALAllRegister(); |
65 | | |
66 | 78 | return 0; |
67 | 78 | } |
68 | | |
69 | | int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) |
70 | 18.0k | { |
71 | 18.0k | VSIFCloseL(VSIFileFromMemBuffer( |
72 | 18.0k | "/vsimem/input.tar", |
73 | 18.0k | reinterpret_cast<GByte *>(const_cast<uint8_t *>(buf)), len, FALSE)); |
74 | | |
75 | 18.0k | CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); |
76 | | |
77 | 18.0k | GByte *paby = nullptr; |
78 | 18.0k | vsi_l_offset nSize = 0; |
79 | 18.0k | if (!VSIIngestFile(nullptr, "/vsitar//vsimem/input.tar/filename", &paby, |
80 | 18.0k | &nSize, -1)) |
81 | 425 | { |
82 | 425 | VSIUnlink("/vsimem/input.tar"); |
83 | 425 | return 0; |
84 | 425 | } |
85 | 17.5k | const std::string osFilename(reinterpret_cast<const char *>(paby)); |
86 | 17.5k | VSIFree(paby); |
87 | | |
88 | 17.5k | paby = nullptr; |
89 | 17.5k | nSize = 0; |
90 | 17.5k | int ret = VSIIngestFile(nullptr, "/vsitar//vsimem/input.tar/content", &paby, |
91 | 17.5k | &nSize, -1); |
92 | 17.5k | VSIUnlink("/vsimem/input.tar"); |
93 | 17.5k | if (!ret) |
94 | 46 | { |
95 | 46 | return 0; |
96 | 46 | } |
97 | | |
98 | 17.5k | const std::string osRealFilename("/vsimem/" + osFilename); |
99 | 17.5k | VSILFILE *fp = VSIFileFromMemBuffer(osRealFilename.c_str(), paby, |
100 | 17.5k | static_cast<size_t>(nSize), TRUE); |
101 | 17.5k | if (fp) |
102 | 17.5k | { |
103 | 17.5k | VSIFCloseL(fp); |
104 | 17.5k | delete GDALDataset::Open(osRealFilename.c_str()); |
105 | 17.5k | } |
106 | 32 | else |
107 | 32 | { |
108 | 32 | VSIFree(paby); |
109 | 32 | } |
110 | | |
111 | 17.5k | VSIRmdirRecursive("/vsimem/"); |
112 | | |
113 | 17.5k | return 0; |
114 | 17.5k | } |