Skip to content

Commit 044ea46

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

3 files changed

Lines changed: 185 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-cfg 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+
cfg_fuzzer.o: cfg_fuzzer.cpp ../lib/config.h ../lib/library.h ../lib/errortypes.h ../lib/path.h
383+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cfg_fuzzer.cpp
384+
385+
oss-fuzz-client-cfg: $(EXTOBJ) $(LIBOBJ) cfg_fuzzer.o
386+
${CXX} ${CXXFLAGS} -o $@ $^ ${LIB_FUZZING_ENGINE}

‎oss-fuzz/cfg_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-cfg):
20+
//
21+
// cppcheck loads library configuration files (.cfg XML) from user-controlled
22+
// locations (--library=..., project files, addons) and parses them with its
23+
// own dispatch code (lib/library.cpp, ~2.1k lines) on top of the bundled XML
24+
// parser. The existing OSS-Fuzz harness never loads any library config (its
25+
// own create_settings() says "TODO: load std.cfg"), so library.cpp is almost
26+
// completely dark (276/1809 covered in the 2025-02-17 production report) and
27+
// the suppressions/platform/standards config layers are similarly dark.
28+
//
29+
// This harness treats the fuzz input as an attacker-supplied .cfg file and
30+
// drives it through the same public entry point the CLI uses
31+
// (Library::load), then probes the loaded tables. A memory-safety or UB
32+
// finding here is a real bug reachable by anyone running cppcheck with a
33+
// malicious or corrupted library config file.
34+
35+
#include "library.h"
36+
37+
#include <cerrno>
38+
#include <cstdio>
39+
#include <cstdlib>
40+
#include <cstring>
41+
#include <string>
42+
#include <unistd.h>
43+
44+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize);
45+
46+
namespace {
47+
// keeps runtime bounded; real-world .cfg files are up to ~1 MB
48+
constexpr size_t kMaxCfgSize = 1024 * 1024;
49+
50+
const char* kProbeFunctions[] = {
51+
"malloc", "calloc", "realloc", "free", "fopen", "fclose",
52+
"printf", "memcpy", "strlen", "strdup", "read", "write"
53+
};
54+
}
55+
56+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
57+
{
58+
if (dataSize < 8 || dataSize > kMaxCfgSize)
59+
return 0;
60+
61+
char path[] = "/tmp/cppcheck_cfgfuzz_XXXXXX";
62+
const int fd = mkstemp(path);
63+
if (fd < 0)
64+
return 0;
65+
FILE* const f = fdopen(fd, "wb");
66+
if (!f) {
67+
close(fd);
68+
unlink(path);
69+
return 0;
70+
}
71+
const size_t written = fwrite(data, 1, dataSize, f);
72+
fclose(f);
73+
if (written != dataSize) {
74+
unlink(path);
75+
return 0;
76+
}
77+
78+
try {
79+
Library lib;
80+
const Library::Error err = lib.load(nullptr, path);
81+
if (err.errorcode == Library::ErrorCode::OK) {
82+
// probe the parsed tables with fixed, well-known names
83+
for (const char* fn : kProbeFunctions) {
84+
(void)lib.getAllocFuncInfo(fn);
85+
(void)lib.getDeallocFuncInfo(fn);
86+
(void)lib.allocId(fn);
87+
(void)lib.deallocId(fn);
88+
}
89+
(void)lib.hasAnyTypeCheck("int");
90+
(void)lib.getTypeCheck("use", "int");
91+
(void)lib.getTypeCheck("suppress", "char*");
92+
(void)lib.functionwarn().size();
93+
(void)lib.platform_type("size_t", "unix64");
94+
(void)lib.platform_type("wchar_t", "win32W");
95+
}
96+
} catch (const std::exception&) {
97+
// Library::load throws on some malformed inputs; a fuzzer must not die
98+
} catch (...) {
99+
}
100+
101+
unlink(path);
102+
return 0;
103+
}

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Dictionary for oss-fuzz-client-cfg: XML section/attribute tokens dispatched
2+
# by lib/library.cpp. All tokens are multi-byte and traceable to source
3+
# constructs (the nodename == "..." dispatch table in library.cpp).
4+
"<def format=\"1\">"
5+
"</def>"
6+
"<?xml version=\"1.0\"?>"
7+
"<function name=\""
8+
"</function>"
9+
"<memory>"
10+
"</memory>"
11+
"<resource>"
12+
"</resource>"
13+
"<alloc init=\"true\">"
14+
"<alloc init=\"false\" buffer-size=\"malloc\">"
15+
"<realloc init=\"false\">"
16+
"<dealloc>"
17+
"<podtype name=\""
18+
"sign=\"u\""
19+
"sign=\"s\""
20+
"size=\"1\""
21+
"<platformtype name=\""
22+
"<platform name=\"win32A\"/>"
23+
"<define name=\"X\" value=\"1\"/>"
24+
"<markup extension=\".html\">"
25+
"<block name=\""
26+
"endblock=\""
27+
"<import>"
28+
"<container id=\"std::vector\">"
29+
"startTag=\"<\""
30+
"endTag=\">\""
31+
"iterator=\""
32+
"<size>"
33+
"<access index=\""
34+
"arrayLike=\"true\""
35+
"<push_back>"
36+
"<pop_back>"
37+
"<erase>"
38+
"<clear>"
39+
"<other>"
40+
"<smart-pointer class=\""
41+
"data=\"get()\""
42+
"<type-checks>"
43+
"<suppress>"
44+
"<check>"
45+
"<noreturn>true</noreturn>"
46+
"<leak-ignore/>"
47+
"<pure/>"
48+
"<const/>"
49+
"<arg nr=\"1\">"
50+
"<arg nr=\"any\">"
51+
"direction=\"in\""
52+
"direction=\"inout\""
53+
"<not-null/>"
54+
"<not-uninit/>"
55+
"<not-bool/>"
56+
"<not-overlapping-data ptr1-arg=\"1\" ptr2-arg=\"2\"/>"
57+
"<valid min=\"0\" max=\"100\"/>"
58+
"<minsize takes=\"arg\" nr=\"2\"/>"
59+
"<formatstr/>"
60+
"<strz/>"
61+
"<entrypoint nr=\"main\"/>"
62+
"<warn severity=\"style\" reason=\"x\" alternatives=\"y\">msg</warn>"
63+
"<ignorefunction>"
64+
"<keywords>"
65+
"<reflection>"
66+
"<iterator>"
67+
"<minsize"
68+
"<unsigned/>"
69+
"<signed/>"
70+
"<long/>"
71+
"<pointer/>"
72+
"<structure>"
73+
"<exported>"
74+
"<imported>"
75+
"<codeblocks>"

0 commit comments

Comments
 (0)