Skip to content

Commit 86a16b5

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

3 files changed

Lines changed: 199 additions & 2 deletions

File tree

‎oss-fuzz/Makefile‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ else
3030
$(error invalid MATCHCOMPILER value '$(MATCHCOMPILER)')
3131
endif
3232

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

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

129129
preprare-samples:
130130
rm -rf samples
@@ -378,3 +378,18 @@ 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+
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
383+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ ../cli/cmdlineparser.cpp
384+
385+
frontend.o: ../frontend/frontend.cpp ../frontend/frontend.h ../lib/filesettings.h ../lib/settings.h ../lib/standards.h
386+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ ../frontend/frontend.cpp
387+
388+
filelister.o: ../cli/filelister.cpp ../cli/filelister.h ../lib/filesettings.h ../lib/path.h ../lib/pathmatch.h ../lib/standards.h ../lib/utils.h
389+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ ../cli/filelister.cpp
390+
391+
cli_fuzzer.o: cli_fuzzer.cpp ../cli/cmdlineparser.h ../cli/cmdlinelogger.h ../lib/settings.h ../lib/suppressions.h
392+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli_fuzzer.cpp
393+
394+
oss-fuzz-client-cli: $(EXTOBJ) $(LIBOBJ) cmdlineparser.o frontend.o filelister.o cli_fuzzer.o
395+
${CXX} ${CXXFLAGS} -o $@ $^ ${LIB_FUZZING_ENGINE}

‎oss-fuzz/cli_fuzzer.cpp‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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-cli):
20+
//
21+
// The entire cli/ front end (cli/cmdlineparser.cpp alone is ~2.3k lines) is
22+
// not part of the OSS-Fuzz build at all: the existing harness links only
23+
// lib/ + the grammar generator, so the command-line parser - which parses
24+
// fully attacker-controlled text (users routinely run cppcheck on untrusted
25+
// repositories whose build scripts embed crafted arguments, and CI systems
26+
// pass user-controlled option strings) - has zero coverage by construction.
27+
//
28+
// This harness feeds NUL-separated argument strings through the same public
29+
// entry point the executor uses (CmdLineParser::fillSettingsFromArgs),
30+
// which returns a Result instead of exiting, and probes the parsed file
31+
// lists. Options such as --library/--project/--suppressions-list/--rule
32+
// additionally drive the library/project/suppression loading code paths
33+
// with attacker-controlled names. Findings here are real bugs reachable
34+
// from the command line.
35+
//
36+
// Input format: NUL-separated argument strings (argv[0] "cppcheck" is
37+
// synthesized by the harness; the input holds only the arguments).
38+
39+
#include "cmdlineparser.h"
40+
#include "cmdlinelogger.h"
41+
#include "settings.h"
42+
#include "suppressions.h"
43+
44+
#include <cstddef>
45+
#include <cstdint>
46+
#include <string>
47+
#include <vector>
48+
49+
namespace {
50+
// argument strings are short; keep runtime bounded
51+
constexpr size_t kMaxInputSize = 4096;
52+
constexpr int kMaxArgs = 64;
53+
54+
class NullLogger : public CmdLineLogger {
55+
public:
56+
void printMessage(const std::string&) override {}
57+
void printError(const std::string&) override {}
58+
void printRaw(const std::string&) override {}
59+
};
60+
}
61+
62+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize);
63+
64+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
65+
{
66+
if (dataSize < 2 || dataSize > kMaxInputSize)
67+
return 0;
68+
69+
std::vector<std::string> args;
70+
args.push_back("cppcheck");
71+
{
72+
size_t start = 0;
73+
for (size_t i = 0; i <= dataSize && args.size() < kMaxArgs; ++i) {
74+
if (i == dataSize || data[i] == 0) {
75+
if (i > start) {
76+
const size_t len = (i - start) > 1024 ? 1024 : (i - start);
77+
args.push_back(std::string(reinterpret_cast<const char*>(data) + start, len));
78+
}
79+
start = i + 1;
80+
}
81+
}
82+
}
83+
84+
std::vector<const char*> argv;
85+
argv.reserve(args.size());
86+
for (const std::string& a : args)
87+
argv.push_back(a.c_str());
88+
89+
try {
90+
Settings settings;
91+
Suppressions supprs;
92+
NullLogger logger;
93+
CmdLineParser parser(logger, settings, supprs);
94+
(void)parser.fillSettingsFromArgs(static_cast<int>(argv.size()), argv.data());
95+
// probe the parse results cheaply
96+
(void)parser.getFiles().size();
97+
(void)parser.getFileSettings().size();
98+
} catch (const std::exception&) {
99+
// options such as --rule=... may throw on bad regexes; a fuzzer must not die
100+
} catch (...) {
101+
}
102+
return 0;
103+
}

‎oss-fuzz/oss-fuzz-client-cli.dict‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Dictionary for oss-fuzz-client-cli: option tokens parsed by
2+
# cli/cmdlineparser.cpp. All tokens are multi-byte and traceable to source
3+
# constructs (the option dispatch code in cmdlineparser.cpp).
4+
"--enable="
5+
"--disable="
6+
"--library="
7+
"--platform="
8+
"--std="
9+
"--language="
10+
"--template="
11+
"--template-location="
12+
"--project="
13+
"--project-configuration="
14+
"--suppressions-list="
15+
"--suppress="
16+
"--suppress-xml="
17+
"--exitcode-suppressions="
18+
"--error-exitcode="
19+
"--addon="
20+
"--addon-python="
21+
"--addons-dir="
22+
"--cppcheck-build-dir="
23+
"--check-level="
24+
"--check-headers"
25+
"--check-config"
26+
"--check-library"
27+
"--checks-max-time="
28+
"--checkers-report="
29+
"--config-exclude="
30+
"--config-excludes-file="
31+
"--file-filter="
32+
"--file-list="
33+
"--filesdir="
34+
"--include="
35+
"--includes-file="
36+
"-I"
37+
"-D"
38+
"-U"
39+
"--jobs="
40+
"--executor="
41+
"--max-configs="
42+
"--max-ctu-depth="
43+
"--max-template-recursion="
44+
"--valueflow-max-iterations="
45+
"--output-file="
46+
"--output-format="
47+
"--plist-output="
48+
"--report-progress="
49+
"--rule="
50+
"--rule-file="
51+
"--showtime="
52+
"--typedef-max-time="
53+
"--template-max-time="
54+
"--xml-version="
55+
"--relative-paths="
56+
"--debug"
57+
"--debug-ast"
58+
"--dump"
59+
"--errorlist"
60+
"--help"
61+
"--version"
62+
"--doc"
63+
"--inconclusive"
64+
"--inline-suppr"
65+
"--force"
66+
"--quiet"
67+
"--verbose"
68+
"--xml"
69+
"all"
70+
"missingInclude"
71+
"unusedFunction"
72+
"exhaustive"
73+
"posix"
74+
"unix64"
75+
"win32W"
76+
"c89"
77+
"c++17"
78+
"sarif"
79+
"thread"

0 commit comments

Comments
 (0)