From 86a16b534ab867a03450c72244556e68388424f0 Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Tue, 24 Nov 2026 04:58:52 +0000 Subject: [PATCH] OSS-Fuzz: Add new fuzzer/corpus/dict targets cli parser Signed-off-by: Arthur Chan --- oss-fuzz/Makefile | 19 +++++- oss-fuzz/cli_fuzzer.cpp | 103 ++++++++++++++++++++++++++++++ oss-fuzz/oss-fuzz-client-cli.dict | 79 +++++++++++++++++++++++ 3 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 oss-fuzz/cli_fuzzer.cpp create mode 100644 oss-fuzz/oss-fuzz-client-cli.dict diff --git a/oss-fuzz/Makefile b/oss-fuzz/Makefile index a71b026618b..ea3daaf4386 100644 --- a/oss-fuzz/Makefile +++ b/oss-fuzz/Makefile @@ -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) @@ -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 @@ -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} diff --git a/oss-fuzz/cli_fuzzer.cpp b/oss-fuzz/cli_fuzzer.cpp new file mode 100644 index 00000000000..62725213706 --- /dev/null +++ b/oss-fuzz/cli_fuzzer.cpp @@ -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 . + */ + +// 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 +#include +#include +#include + +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 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(data) + start, len)); + } + start = i + 1; + } + } + } + + std::vector 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(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; +} diff --git a/oss-fuzz/oss-fuzz-client-cli.dict b/oss-fuzz/oss-fuzz-client-cli.dict new file mode 100644 index 00000000000..631c2c34889 --- /dev/null +++ b/oss-fuzz/oss-fuzz-client-cli.dict @@ -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"