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
19 changes: 17 additions & 2 deletions oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ else
$(error invalid MATCHCOMPILER value '$(MATCHCOMPILER)')
endif

INCS=-I../lib -isystem../externals/simplecpp -isystem../externals/tinyxml2 -isystem../externals/picojson
INCS=-I../lib -I../cli -I../frontend -isystem../externals/simplecpp -isystem../externals/tinyxml2 -isystem../externals/picojson

override CXXFLAGS+=-std=c++11 -g -w
override CPPFLAGS+=$(INCS)
Expand Down 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-cli no-fuzz translate

preprare-samples:
rm -rf samples
Expand Down Expand Up @@ -378,3 +378,18 @@ 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

cmdlineparser.o: ../cli/cmdlineparser.cpp ../cli/cmdlineparser.h ../cli/cmdlinelogger.h ../cli/filelister.h ../frontend/frontend.h ../lib/settings.h ../lib/suppressions.h ../lib/library.h ../lib/importproject.h ../lib/filesettings.h ../lib/addoninfo.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ ../cli/cmdlineparser.cpp

frontend.o: ../frontend/frontend.cpp ../frontend/frontend.h ../lib/filesettings.h ../lib/settings.h ../lib/standards.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ ../frontend/frontend.cpp

filelister.o: ../cli/filelister.cpp ../cli/filelister.h ../lib/filesettings.h ../lib/path.h ../lib/pathmatch.h ../lib/standards.h ../lib/utils.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ ../cli/filelister.cpp

cli_fuzzer.o: cli_fuzzer.cpp ../cli/cmdlineparser.h ../cli/cmdlinelogger.h ../lib/settings.h ../lib/suppressions.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli_fuzzer.cpp

oss-fuzz-client-cli: $(EXTOBJ) $(LIBOBJ) cmdlineparser.o frontend.o filelister.o cli_fuzzer.o
${CXX} ${CXXFLAGS} -o $@ $^ ${LIB_FUZZING_ENGINE}
103 changes: 103 additions & 0 deletions oss-fuzz/cli_fuzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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-cli):
//
// The entire cli/ front end (cli/cmdlineparser.cpp alone is ~2.3k lines) is
// not part of the OSS-Fuzz build at all: the existing harness links only
// lib/ + the grammar generator, so the command-line parser - which parses
// fully attacker-controlled text (users routinely run cppcheck on untrusted
// repositories whose build scripts embed crafted arguments, and CI systems
// pass user-controlled option strings) - has zero coverage by construction.
//
// This harness feeds NUL-separated argument strings through the same public
// entry point the executor uses (CmdLineParser::fillSettingsFromArgs),
// which returns a Result instead of exiting, and probes the parsed file
// lists. Options such as --library/--project/--suppressions-list/--rule
// additionally drive the library/project/suppression loading code paths
// with attacker-controlled names. Findings here are real bugs reachable
// from the command line.
//
// Input format: NUL-separated argument strings (argv[0] "cppcheck" is
// synthesized by the harness; the input holds only the arguments).

#include "cmdlineparser.h"
#include "cmdlinelogger.h"
#include "settings.h"
#include "suppressions.h"

#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>

namespace {
// argument strings are short; keep runtime bounded
constexpr size_t kMaxInputSize = 4096;
constexpr int kMaxArgs = 64;

class NullLogger : public CmdLineLogger {
public:
void printMessage(const std::string&) override {}
void printError(const std::string&) override {}
void printRaw(const std::string&) override {}
};
}

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

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

std::vector<std::string> args;
args.push_back("cppcheck");
{
size_t start = 0;
for (size_t i = 0; i <= dataSize && args.size() < kMaxArgs; ++i) {
if (i == dataSize || data[i] == 0) {
if (i > start) {
const size_t len = (i - start) > 1024 ? 1024 : (i - start);
args.push_back(std::string(reinterpret_cast<const char*>(data) + start, len));
}
start = i + 1;
}
}
}

std::vector<const char*> argv;
argv.reserve(args.size());
for (const std::string& a : args)
argv.push_back(a.c_str());

try {
Settings settings;
Suppressions supprs;
NullLogger logger;
CmdLineParser parser(logger, settings, supprs);
(void)parser.fillSettingsFromArgs(static_cast<int>(argv.size()), argv.data());
// probe the parse results cheaply
(void)parser.getFiles().size();
(void)parser.getFileSettings().size();
} catch (const std::exception&) {
// options such as --rule=... may throw on bad regexes; a fuzzer must not die
} catch (...) {
}
return 0;
}
79 changes: 79 additions & 0 deletions oss-fuzz/oss-fuzz-client-cli.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Dictionary for oss-fuzz-client-cli: option tokens parsed by
# cli/cmdlineparser.cpp. All tokens are multi-byte and traceable to source
# constructs (the option dispatch code in cmdlineparser.cpp).
"--enable="
"--disable="
"--library="
"--platform="
"--std="
"--language="
"--template="
"--template-location="
"--project="
"--project-configuration="
"--suppressions-list="
"--suppress="
"--suppress-xml="
"--exitcode-suppressions="
"--error-exitcode="
"--addon="
"--addon-python="
"--addons-dir="
"--cppcheck-build-dir="
"--check-level="
"--check-headers"
"--check-config"
"--check-library"
"--checks-max-time="
"--checkers-report="
"--config-exclude="
"--config-excludes-file="
"--file-filter="
"--file-list="
"--filesdir="
"--include="
"--includes-file="
"-I"
"-D"
"-U"
"--jobs="
"--executor="
"--max-configs="
"--max-ctu-depth="
"--max-template-recursion="
"--valueflow-max-iterations="
"--output-file="
"--output-format="
"--plist-output="
"--report-progress="
"--rule="
"--rule-file="
"--showtime="
"--typedef-max-time="
"--template-max-time="
"--xml-version="
"--relative-paths="
"--debug"
"--debug-ast"
"--dump"
"--errorlist"
"--help"
"--version"
"--doc"
"--inconclusive"
"--inline-suppr"
"--force"
"--quiet"
"--verbose"
"--xml"
"all"
"missingInclude"
"unusedFunction"
"exhaustive"
"posix"
"unix64"
"win32W"
"c89"
"c++17"
"sarif"
"thread"
Loading