0xiviel commited on
Commit
14fa8c5
·
verified ·
1 Parent(s): 17d048e

Upload test_oob_read.cpp with huggingface_hub

Browse files
Files changed (1) hide show
  1. test_oob_read.cpp +84 -0
test_oob_read.cpp ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * PoC: MemoryReadAdapter heap out-of-bounds read
3
+ *
4
+ * This test directly demonstrates that caffe2::serialize::MemoryReadAdapter::read()
5
+ * performs NO bounds checking. The read() method blindly memcpy's from data_+pos
6
+ * for n bytes, without checking that pos+n <= size_.
7
+ *
8
+ * Compile with ASAN:
9
+ * g++ -fsanitize=address -g -I$(python3 -c "import torch; print(torch.utils.cmake_prefix_path)")/../../include \
10
+ * test_oob_read.cpp -o test_oob_read
11
+ *
12
+ * Expected output: ASAN reports heap-buffer-overflow
13
+ */
14
+
15
+ #include <cstdio>
16
+ #include <cstdlib>
17
+ #include <cstring>
18
+ #include <cstdint>
19
+
20
+ // Inline the vulnerable class (exact copy from caffe2/serialize/in_memory_adapter.h)
21
+ // to avoid needing the full PyTorch build
22
+ class MemoryReadAdapter {
23
+ public:
24
+ explicit MemoryReadAdapter(const void* data, int64_t size)
25
+ : data_(data), size_(size) {}
26
+
27
+ size_t size() const {
28
+ return size_;
29
+ }
30
+
31
+ // THIS IS THE VULNERABILITY: no bounds checking on pos or n vs size_
32
+ size_t read(uint64_t pos, void* buf, size_t n, const char* what = "") const {
33
+ (void)what;
34
+ memcpy(buf, (int8_t*)(data_) + pos, n); // NO CHECK: pos+n vs size_
35
+ return n;
36
+ }
37
+
38
+ private:
39
+ const void* data_;
40
+ int64_t size_;
41
+ };
42
+
43
+ // For comparison: miniz's own memory reader HAS bounds checking
44
+ // (from third_party/miniz-3.0.2/miniz.c, mz_zip_mem_read_func)
45
+ size_t safe_read(const void* data, size_t data_size, uint64_t pos, void* buf, size_t n) {
46
+ size_t s = (pos >= data_size) ? 0 : (size_t)((data_size - pos < n) ? data_size - pos : n);
47
+ memcpy(buf, (const uint8_t*)data + pos, s);
48
+ return s;
49
+ }
50
+
51
+ int main() {
52
+ // Allocate a small buffer (32 bytes)
53
+ const size_t BUF_SIZE = 32;
54
+ char* data = (char*)malloc(BUF_SIZE);
55
+ if (!data) return 1;
56
+ memset(data, 'A', BUF_SIZE);
57
+
58
+ // Create MemoryReadAdapter with correct size
59
+ MemoryReadAdapter adapter(data, BUF_SIZE);
60
+ printf("Buffer size: %zu bytes at %p\n", adapter.size(), data);
61
+
62
+ char output[256];
63
+ memset(output, 0, sizeof(output));
64
+
65
+ // Test 1: Normal read within bounds (should work fine)
66
+ printf("\n[Test 1] Reading 16 bytes at offset 0 (within bounds)...\n");
67
+ adapter.read(0, output, 16);
68
+ printf(" OK: read 16 bytes\n");
69
+
70
+ // Test 2: Read past the end of the buffer (OOB!)
71
+ printf("\n[Test 2] Reading 64 bytes at offset 0 (32 bytes past buffer end)...\n");
72
+ printf(" Buffer is %zu bytes, but requesting 64 bytes\n", BUF_SIZE);
73
+ printf(" MemoryReadAdapter::read() will memcpy 64 bytes - reading 32 bytes of HEAP DATA\n");
74
+ adapter.read(0, output, 64); // ASAN: heap-buffer-overflow
75
+ printf(" Leaked %zu bytes past buffer end!\n", (size_t)64 - BUF_SIZE);
76
+
77
+ // Test 3: Read at offset past buffer end
78
+ printf("\n[Test 3] Reading 16 bytes at offset 128 (entirely past buffer)...\n");
79
+ adapter.read(128, output, 16); // ASAN: heap-buffer-overflow
80
+ printf(" Read from offset 128, buffer is only %zu bytes!\n", BUF_SIZE);
81
+
82
+ free(data);
83
+ return 0;
84
+ }