Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Tests/Fuzzing/cmArchiveExtractFuzzer.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
4
/*
5
 * Fuzzer for CMake's archive extraction (tar/zip)
6
 *
7
 * CMake extracts archives via cmSystemTools::ExtractTar. This is a critical
8
 * attack surface as malicious archives could contain path traversal sequences
9
 * (Zip Slip) or other exploits.
10
 *
11
 * Coverage targets:
12
 * - Archive format detection (tar, gzip, bzip2, xz, zip)
13
 * - Path handling during extraction
14
 * - Symlink handling
15
 * - Large file handling
16
 * - Malformed archive recovery
17
 *
18
 * Security focus:
19
 * - Path traversal (../) detection
20
 * - Absolute path handling
21
 * - Symlink escape attempts
22
 */
23
24
#include <cstddef>
25
#include <cstdint>
26
#include <cstdio>
27
#include <cstdlib>
28
#include <string>
29
#include <vector>
30
31
#include "cmSystemTools.h"
32
33
// Archives can be large but limit for fuzzing
34
static constexpr size_t kMinInputSize = 4;          // Minimum magic bytes
35
static constexpr size_t kMaxInputSize = 256 * 1024; // 256KB
36
37
// Sandbox directory for extraction
38
static std::string g_extractDir;
39
40
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
41
6
{
42
6
  (void)argc;
43
6
  (void)argv;
44
45
  // Create a unique extraction directory in /tmp
46
6
  char tmpl[] = "/tmp/cmake_fuzz_extract_XXXXXX";
47
6
  char* dir = mkdtemp(tmpl);
48
6
  if (dir) {
49
6
    g_extractDir = dir;
50
6
  } else {
51
0
    g_extractDir = "/tmp/cmake_fuzz_extract";
52
0
    cmSystemTools::MakeDirectory(g_extractDir);
53
0
  }
54
55
6
  return 0;
56
6
}
57
58
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
59
14.3k
{
60
14.3k
  if (size < kMinInputSize || size > kMaxInputSize) {
61
2
    return 0;
62
2
  }
63
64
  // Write archive to temp file
65
14.3k
  std::string archiveFile = g_extractDir + "/test_archive";
66
14.3k
  {
67
14.3k
    FILE* fp = fopen(archiveFile.c_str(), "wb");
68
14.3k
    if (!fp) {
69
0
      return 0;
70
0
    }
71
14.3k
    fwrite(data, 1, size, fp);
72
14.3k
    fclose(fp);
73
14.3k
  }
74
75
  // Create a fresh extraction subdirectory each time
76
0
  std::string extractSubDir = g_extractDir + "/out";
77
14.3k
  cmSystemTools::RemoveADirectory(extractSubDir);
78
14.3k
  cmSystemTools::MakeDirectory(extractSubDir);
79
80
  // Save current directory
81
14.3k
  std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
82
83
  // Change to extraction directory (cmSystemTools extracts to cwd)
84
14.3k
  if (cmSystemTools::ChangeDirectory(extractSubDir)) {
85
    // Try extraction with different options
86
14.3k
    std::vector<std::string> files;
87
88
    // Extract without verbose, with timestamps
89
14.3k
    std::vector<std::string> excludeFiles;
90
14.3k
    bool result1 = cmSystemTools::ExtractTar(
91
14.3k
      archiveFile, files, excludeFiles,
92
14.3k
      cmSystemTools::cmTarExtractTimestamps::Yes, "UTF-8", false);
93
14.3k
    (void)result1;
94
95
    // Restore directory BEFORE removing (can't remove cwd)
96
14.3k
    cmSystemTools::ChangeDirectory(cwd);
97
98
    // Clean up extracted files
99
14.3k
    cmSystemTools::RemoveADirectory(extractSubDir);
100
14.3k
    cmSystemTools::MakeDirectory(extractSubDir);
101
102
    // Change back for second extraction
103
14.3k
    if (cmSystemTools::ChangeDirectory(extractSubDir)) {
104
      // Extract with verbose, without timestamps
105
14.3k
      files.clear();
106
14.3k
      bool result2 = cmSystemTools::ExtractTar(
107
14.3k
        archiveFile, files, excludeFiles,
108
14.3k
        cmSystemTools::cmTarExtractTimestamps::No, "UTF-8", true);
109
14.3k
      (void)result2;
110
111
      // Restore directory
112
14.3k
      cmSystemTools::ChangeDirectory(cwd);
113
14.3k
    }
114
14.3k
  }
115
116
  // Note: A more thorough security check would verify nothing escaped the
117
  // sandbox, but for fuzzing we rely on sanitizers to catch path traversal
118
119
  // Cleanup
120
14.3k
  cmSystemTools::RemoveADirectory(extractSubDir);
121
14.3k
  cmSystemTools::RemoveFile(archiveFile);
122
123
14.3k
  return 0;
124
14.3k
}