Skip to content

Commit 5b85210

Browse files
committed
OSS-Fuzz: Add new fuzzer/corpus/dict targets project files handling
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
1 parent 68bf94a commit 5b85210

3 files changed

Lines changed: 160 additions & 1 deletion

File tree

‎oss-fuzz/Makefile‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ translate: translate.o type2.o
124124
${CXX} -std=c++11 -g ${CXXFLAGS} -o $@ type2.cpp translate.cpp
125125

126126
clean:
127-
rm -f *.o build/*.o oss-fuzz-client no-fuzz translate
127+
rm -f *.o build/*.o oss-fuzz-client oss-fuzz-client-project no-fuzz translate
128128

129129
preprare-samples:
130130
rm -rf samples
@@ -378,3 +378,9 @@ main.o: main.cpp type2.h
378378

379379
main_nofuzz.o: main.cpp type2.h
380380
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -DNO_FUZZ -c -o $@ main.cpp
381+
382+
project_fuzzer.o: project_fuzzer.cpp ../lib/config.h ../lib/importproject.h ../lib/filesettings.h ../lib/settings.h ../lib/suppressions.h ../lib/errortypes.h
383+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ project_fuzzer.cpp
384+
385+
oss-fuzz-client-project: $(EXTOBJ) $(LIBOBJ) project_fuzzer.o
386+
${CXX} ${CXXFLAGS} -o $@ $^ ${LIB_FUZZING_ENGINE}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Dictionary for oss-fuzz-client-project: tokens parsed by
2+
# lib/importproject.cpp (compile_commands JSON, VS sln/slnx/vcxproj,
3+
# Code::Blocks cbp, Borland bpr, cppcheck GUI project). All tokens are
4+
# multi-byte and traceable to source constructs.
5+
"directory"
6+
"arguments"
7+
"command"
8+
"file"
9+
"output"
10+
"compile_commands"
11+
"Microsoft Visual Studio Solution File"
12+
"Project("
13+
"EndProject"
14+
"GlobalSection"
15+
"ActiveCfg"
16+
"Build.0"
17+
"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"
18+
".vcxproj"
19+
"<Solution>"
20+
"<Project Path=\""
21+
"<PlatformList>"
22+
"<Platform Name="
23+
"<ProjectConfiguration Include=\"Debug|Win32\">"
24+
"<Configuration>"
25+
"<Platform>"
26+
"<ClCompile Include=\""
27+
"<ClInclude Include=\""
28+
"<PreprocessorDefinitions>"
29+
"<AdditionalIncludeDirectories>"
30+
"<ConfigurationType>"
31+
"<PlatformToolset>"
32+
"<LanguageStandard>"
33+
"<ProjectGuid>"
34+
"Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">"
35+
"<CodeBlocks_project_file>"
36+
"<Option title=\""
37+
"<Option output=\""
38+
"<Option compiler=\"gcc\" />"
39+
"<Add option=\"-D"
40+
"<Add directory=\""
41+
"<Unit filename=\""
42+
"<Build>"
43+
"<Target title=\""
44+
"<PROJECT>"
45+
"<MAINSOURCE>"
46+
"<FILELIST>"
47+
"<FILE FILENAME=\""
48+
"<DEFINES>"
49+
"<INCLUDEPATH>"
50+
"Cppcheck_GUI_project"
51+
"<project format_version=\"1\">"
52+
"<root name=\""
53+
"<builddir>"
54+
"<includedir>"
55+
"<dir name=\""
56+
"<defines>"
57+
"<define name=\""
58+
"<undefine>"
59+
"<paths>"
60+
"<exclude>"
61+
"<library>"
62+
"<suppressions>"
63+
"<addons>"
64+
"<addon>"

‎oss-fuzz/project_fuzzer.cpp‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2026 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
// Fuzzing rationale (oss-fuzz-client-project):
20+
//
21+
// ImportProject::import() parses project files that describe builds:
22+
// compile_commands.json (picojson), Visual Studio .sln/.slnx/.vcxproj,
23+
// Code::Blocks .cbp, Borland .bpr and cppcheck GUI .cppcheck projects.
24+
// These files are attacker-controllable in practice: cppcheck is pointed
25+
// at checkouts that carry them, and the XML/JSON parsers walk fully
26+
// fuzz-derived structure and attribute data. In the 2025-02-17 production
27+
// coverage report importproject.cpp is completely dark (0/1208 lines)
28+
// because the existing harness never imports any project.
29+
//
30+
// This harness writes the fuzz bytes to a temporary file whose extension
31+
// (selected from the first byte) picks the importer, drives the same
32+
// public entry point the CLI uses (--project=...), and probes the
33+
// imported file settings and error lists. A memory-safety or UB finding
34+
// here is a real bug reachable by anyone running cppcheck on a malicious
35+
// repository.
36+
37+
#include "importproject.h"
38+
#include "settings.h"
39+
#include "suppressions.h"
40+
41+
#include <cerrno>
42+
#include <cstdio>
43+
#include <cstdlib>
44+
#include <string>
45+
#include <unistd.h>
46+
47+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize);
48+
49+
namespace {
50+
constexpr size_t kMaxProjectSize = 1024 * 1024;
51+
const char* kExtensions[] = {".json", ".sln", ".vcxproj", ".cbp", ".cppcheck", ".bpr", ".slnx"};
52+
}
53+
54+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
55+
{
56+
if (dataSize < 8 || dataSize > kMaxProjectSize)
57+
return 0;
58+
59+
static unsigned counter = 0;
60+
char path[128];
61+
const int sel = data[0] % 7;
62+
snprintf(path, sizeof(path), "/tmp/cppcheck_projfuzz_%d_%u%s",
63+
static_cast<int>(getpid()), counter++, kExtensions[sel]);
64+
65+
FILE* f = fopen(path, "wb");
66+
if (!f)
67+
return 0;
68+
const size_t written = fwrite(data + 1, 1, dataSize - 1, f);
69+
fclose(f);
70+
if (written != dataSize - 1) {
71+
unlink(path);
72+
return 0;
73+
}
74+
75+
try {
76+
Settings settings;
77+
Suppressions supprs;
78+
ImportProject importer;
79+
(void)importer.import(path, &settings, &supprs);
80+
(void)importer.fileSettings.size();
81+
(void)importer.errors.size();
82+
} catch (const std::exception&) {
83+
// malformed project data must not kill the fuzzer
84+
} catch (...) {
85+
}
86+
87+
unlink(path);
88+
return 0;
89+
}

0 commit comments

Comments
 (0)