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-cfg 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

cfg_fuzzer.o: cfg_fuzzer.cpp ../lib/config.h ../lib/library.h ../lib/errortypes.h ../lib/path.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cfg_fuzzer.cpp

oss-fuzz-client-cfg: $(EXTOBJ) $(LIBOBJ) cfg_fuzzer.o
${CXX} ${CXXFLAGS} -o $@ $^ ${LIB_FUZZING_ENGINE}
103 changes: 103 additions & 0 deletions oss-fuzz/cfg_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-cfg):
//
// cppcheck loads library configuration files (.cfg XML) from user-controlled
// locations (--library=..., project files, addons) and parses them with its
// own dispatch code (lib/library.cpp, ~2.1k lines) on top of the bundled XML
// parser. The existing OSS-Fuzz harness never loads any library config (its
// own create_settings() says "TODO: load std.cfg"), so library.cpp is almost
// completely dark (276/1809 covered in the 2025-02-17 production report) and
// the suppressions/platform/standards config layers are similarly dark.
//
// This harness treats the fuzz input as an attacker-supplied .cfg file and
// drives it through the same public entry point the CLI uses
// (Library::load), then probes the loaded tables. A memory-safety or UB
// finding here is a real bug reachable by anyone running cppcheck with a
// malicious or corrupted library config file.

#include "library.h"

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

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

namespace {
// keeps runtime bounded; real-world .cfg files are up to ~1 MB
constexpr size_t kMaxCfgSize = 1024 * 1024;

const char* kProbeFunctions[] = {
"malloc", "calloc", "realloc", "free", "fopen", "fclose",
"printf", "memcpy", "strlen", "strdup", "read", "write"
};
}

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

char path[] = "/tmp/cppcheck_cfgfuzz_XXXXXX";
const int fd = mkstemp(path);
if (fd < 0)
return 0;
FILE* const f = fdopen(fd, "wb");
if (!f) {
close(fd);
unlink(path);
return 0;
}
const size_t written = fwrite(data, 1, dataSize, f);
fclose(f);
if (written != dataSize) {
unlink(path);
return 0;
}

try {
Library lib;
const Library::Error err = lib.load(nullptr, path);
if (err.errorcode == Library::ErrorCode::OK) {
// probe the parsed tables with fixed, well-known names
for (const char* fn : kProbeFunctions) {
(void)lib.getAllocFuncInfo(fn);
(void)lib.getDeallocFuncInfo(fn);
(void)lib.allocId(fn);
(void)lib.deallocId(fn);
}
(void)lib.hasAnyTypeCheck("int");
(void)lib.getTypeCheck("use", "int");
(void)lib.getTypeCheck("suppress", "char*");
(void)lib.functionwarn().size();
(void)lib.platform_type("size_t", "unix64");
(void)lib.platform_type("wchar_t", "win32W");
}
} catch (const std::exception&) {
// Library::load throws on some malformed inputs; a fuzzer must not die
} catch (...) {
}

unlink(path);
return 0;
}
75 changes: 75 additions & 0 deletions oss-fuzz/oss-fuzz-client-cfg.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Dictionary for oss-fuzz-client-cfg: XML section/attribute tokens dispatched
# by lib/library.cpp. All tokens are multi-byte and traceable to source
# constructs (the nodename == "..." dispatch table in library.cpp).
"<def format=\"1\">"
"</def>"
"<?xml version=\"1.0\"?>"
"<function name=\""
"</function>"
"<memory>"
"</memory>"
"<resource>"
"</resource>"
"<alloc init=\"true\">"
"<alloc init=\"false\" buffer-size=\"malloc\">"
"<realloc init=\"false\">"
"<dealloc>"
"<podtype name=\""
"sign=\"u\""
"sign=\"s\""
"size=\"1\""
"<platformtype name=\""
"<platform name=\"win32A\"/>"
"<define name=\"X\" value=\"1\"/>"
"<markup extension=\".html\">"
"<block name=\""
"endblock=\""
"<import>"
"<container id=\"std::vector\">"
"startTag=\"<\""
"endTag=\">\""
"iterator=\""
"<size>"
"<access index=\""
"arrayLike=\"true\""
"<push_back>"
"<pop_back>"
"<erase>"
"<clear>"
"<other>"
"<smart-pointer class=\""
"data=\"get()\""
"<type-checks>"
"<suppress>"
"<check>"
"<noreturn>true</noreturn>"
"<leak-ignore/>"
"<pure/>"
"<const/>"
"<arg nr=\"1\">"
"<arg nr=\"any\">"
"direction=\"in\""
"direction=\"inout\""
"<not-null/>"
"<not-uninit/>"
"<not-bool/>"
"<not-overlapping-data ptr1-arg=\"1\" ptr2-arg=\"2\"/>"
"<valid min=\"0\" max=\"100\"/>"
"<minsize takes=\"arg\" nr=\"2\"/>"
"<formatstr/>"
"<strz/>"
"<entrypoint nr=\"main\"/>"
"<warn severity=\"style\" reason=\"x\" alternatives=\"y\">msg</warn>"
"<ignorefunction>"
"<keywords>"
"<reflection>"
"<iterator>"
"<minsize"
"<unsigned/>"
"<signed/>"
"<long/>"
"<pointer/>"
"<structure>"
"<exported>"
"<imported>"
"<codeblocks>"