|
| 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 | +} |
0 commit comments