Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ translate: translate.o type2.o
${CXX} -std=c++11 -g ${CXXFLAGS} -o $@ type2.cpp translate.cpp

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

preprare-samples:
rm -rf samples
Expand Down Expand Up @@ -378,3 +378,9 @@ main.o: main.cpp type2.h

main_nofuzz.o: main.cpp type2.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -DNO_FUZZ -c -o $@ main.cpp

project_fuzzer.o: project_fuzzer.cpp ../lib/config.h ../lib/importproject.h ../lib/filesettings.h ../lib/settings.h ../lib/suppressions.h ../lib/errortypes.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ project_fuzzer.cpp

oss-fuzz-client-project: $(EXTOBJ) $(LIBOBJ) project_fuzzer.o
${CXX} ${CXXFLAGS} -o $@ $^ ${LIB_FUZZING_ENGINE}
64 changes: 64 additions & 0 deletions oss-fuzz/oss-fuzz-client-project.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Dictionary for oss-fuzz-client-project: tokens parsed by
# lib/importproject.cpp (compile_commands JSON, VS sln/slnx/vcxproj,
# Code::Blocks cbp, Borland bpr, cppcheck GUI project). All tokens are
# multi-byte and traceable to source constructs.
"directory"
"arguments"
"command"
"file"
"output"
"compile_commands"
"Microsoft Visual Studio Solution File"
"Project("
"EndProject"
"GlobalSection"
"ActiveCfg"
"Build.0"
"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"
".vcxproj"
"<Solution>"
"<Project Path=\""
"<PlatformList>"
"<Platform Name="
"<ProjectConfiguration Include=\"Debug|Win32\">"
"<Configuration>"
"<Platform>"
"<ClCompile Include=\""
"<ClInclude Include=\""
"<PreprocessorDefinitions>"
"<AdditionalIncludeDirectories>"
"<ConfigurationType>"
"<PlatformToolset>"
"<LanguageStandard>"
"<ProjectGuid>"
"Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">"
"<CodeBlocks_project_file>"
"<Option title=\""
"<Option output=\""
"<Option compiler=\"gcc\" />"
"<Add option=\"-D"
"<Add directory=\""
"<Unit filename=\""
"<Build>"
"<Target title=\""
"<PROJECT>"
"<MAINSOURCE>"
"<FILELIST>"
"<FILE FILENAME=\""
"<DEFINES>"
"<INCLUDEPATH>"
"Cppcheck_GUI_project"
"<project format_version=\"1\">"
"<root name=\""
"<builddir>"
"<includedir>"
"<dir name=\""
"<defines>"
"<define name=\""
"<undefine>"
"<paths>"
"<exclude>"
"<library>"
"<suppressions>"
"<addons>"
"<addon>"
89 changes: 89 additions & 0 deletions oss-fuzz/project_fuzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// Fuzzing rationale (oss-fuzz-client-project):
//
// ImportProject::import() parses project files that describe builds:
// compile_commands.json (picojson), Visual Studio .sln/.slnx/.vcxproj,
// Code::Blocks .cbp, Borland .bpr and cppcheck GUI .cppcheck projects.
// These files are attacker-controllable in practice: cppcheck is pointed
// at checkouts that carry them, and the XML/JSON parsers walk fully
// fuzz-derived structure and attribute data. In the 2025-02-17 production
// coverage report importproject.cpp is completely dark (0/1208 lines)
// because the existing harness never imports any project.
//
// This harness writes the fuzz bytes to a temporary file whose extension
// (selected from the first byte) picks the importer, drives the same
// public entry point the CLI uses (--project=...), and probes the
// imported file settings and error lists. A memory-safety or UB finding
// here is a real bug reachable by anyone running cppcheck on a malicious
// repository.

#include "importproject.h"
#include "settings.h"
#include "suppressions.h"

#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <unistd.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize);

namespace {
constexpr size_t kMaxProjectSize = 1024 * 1024;
const char* kExtensions[] = {".json", ".sln", ".vcxproj", ".cbp", ".cppcheck", ".bpr", ".slnx"};
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
{
if (dataSize < 8 || dataSize > kMaxProjectSize)
return 0;

static unsigned counter = 0;
char path[128];
const int sel = data[0] % 7;
snprintf(path, sizeof(path), "/tmp/cppcheck_projfuzz_%d_%u%s",
static_cast<int>(getpid()), counter++, kExtensions[sel]);

FILE* f = fopen(path, "wb");
if (!f)
return 0;
const size_t written = fwrite(data + 1, 1, dataSize - 1, f);
fclose(f);
if (written != dataSize - 1) {
unlink(path);
return 0;
}

try {
Settings settings;
Suppressions supprs;
ImportProject importer;
(void)importer.import(path, &settings, &supprs);
(void)importer.fileSettings.size();
(void)importer.errors.size();
} catch (const std::exception&) {
// malformed project data must not kill the fuzzer
} catch (...) {
}

unlink(path);
return 0;
}
Loading