|
| 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-project): |
| 20 | +// |
| 21 | +// ImportProject::import() parses project files that describe builds: |
| 22 | +// compile_commands.json (picojson), Visual Studio .sln/.slnx/.vcxproj, |
| 23 | +// Code::Blocks .cbp, Borland .bpr and cppcheck GUI .cppcheck projects. |
| 24 | +// These files are attacker-controllable in practice: cppcheck is pointed |
| 25 | +// at checkouts that carry them, and the XML/JSON parsers walk fully |
| 26 | +// fuzz-derived structure and attribute data. In the 2025-02-17 production |
| 27 | +// coverage report importproject.cpp is completely dark (0/1208 lines) |
| 28 | +// because the existing harness never imports any project. |
| 29 | +// |
| 30 | +// This harness writes the fuzz bytes to a temporary file whose extension |
| 31 | +// (selected from the first byte) picks the importer, drives the same |
| 32 | +// public entry point the CLI uses (--project=...), and probes the |
| 33 | +// imported file settings and error lists. A memory-safety or UB finding |
| 34 | +// here is a real bug reachable by anyone running cppcheck on a malicious |
| 35 | +// repository. |
| 36 | + |
| 37 | +#include "importproject.h" |
| 38 | +#include "settings.h" |
| 39 | +#include "suppressions.h" |
| 40 | + |
| 41 | +#include <cerrno> |
| 42 | +#include <cstdio> |
| 43 | +#include <cstdlib> |
| 44 | +#include <string> |
| 45 | +#include <unistd.h> |
| 46 | + |
| 47 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize); |
| 48 | + |
| 49 | +namespace { |
| 50 | + constexpr size_t kMaxProjectSize = 1024 * 1024; |
| 51 | + const char* kExtensions[] = {".json", ".sln", ".vcxproj", ".cbp", ".cppcheck", ".bpr", ".slnx"}; |
| 52 | +} |
| 53 | + |
| 54 | +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize) |
| 55 | +{ |
| 56 | + if (dataSize < 8 || dataSize > kMaxProjectSize) |
| 57 | + return 0; |
| 58 | + |
| 59 | + static unsigned counter = 0; |
| 60 | + char path[128]; |
| 61 | + const int sel = data[0] % 7; |
| 62 | + snprintf(path, sizeof(path), "/tmp/cppcheck_projfuzz_%d_%u%s", |
| 63 | + static_cast<int>(getpid()), counter++, kExtensions[sel]); |
| 64 | + |
| 65 | + FILE* f = fopen(path, "wb"); |
| 66 | + if (!f) |
| 67 | + return 0; |
| 68 | + const size_t written = fwrite(data + 1, 1, dataSize - 1, f); |
| 69 | + fclose(f); |
| 70 | + if (written != dataSize - 1) { |
| 71 | + unlink(path); |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + Settings settings; |
| 77 | + Suppressions supprs; |
| 78 | + ImportProject importer; |
| 79 | + (void)importer.import(path, &settings, &supprs); |
| 80 | + (void)importer.fileSettings.size(); |
| 81 | + (void)importer.errors.size(); |
| 82 | + } catch (const std::exception&) { |
| 83 | + // malformed project data must not kill the fuzzer |
| 84 | + } catch (...) { |
| 85 | + } |
| 86 | + |
| 87 | + unlink(path); |
| 88 | + return 0; |
| 89 | +} |
0 commit comments