● Zion Boggan
repos/Cyclops V2/targets/patch_gguf_fuzzdepth.py
zionboggan.com ↗
32 lines · python
History for this file →
1
"""Guard the KNOWN-shallow div-by-zero sites in gguf.cpp so the fuzzer gets PAST
2
tensor-shape validation and explores deeper tensor-data/offset handling (where a
3
novel memory-corruption bug would live). These are fuzzing-depth guards, NOT a
4
security fix - they neutralize the disclosed V-05/V-06-class and our line-681 DoS
5
so distinct, higher-severity crashes can surface.
6
"""
7
import sys
8
p = "/opt/cyclops-v2/targets/src/llama.cpp/ggml/src/gguf.cpp"
9
src = open(p).read()
10
PATCHES = [
11
    ("INT64_MAX/info.t.ne[1] <= info.t.ne[0]",
12
     "info.t.ne[1]!=0 && INT64_MAX/info.t.ne[1] <= info.t.ne[0]"),
13
    ("INT64_MAX/info.t.ne[2] <= info.t.ne[0]*info.t.ne[1]",
14
     "info.t.ne[2]!=0 && INT64_MAX/info.t.ne[2] <= info.t.ne[0]*info.t.ne[1]"),
15
    ("INT64_MAX/info.t.ne[3] <= info.t.ne[0]*info.t.ne[1]*info.t.ne[2]",
16
     "info.t.ne[3]!=0 && INT64_MAX/info.t.ne[3] <= info.t.ne[0]*info.t.ne[1]*info.t.ne[2]"),
17
    ("uint64_t(ggml_nelements(&info.t)/ggml_blck_size(info.t.type)) > SIZE_MAX/ggml_type_size(info.t.type)",
18
     "ggml_blck_size(info.t.type)!=0 && ggml_type_size(info.t.type)!=0 && uint64_t(ggml_nelements(&info.t)/ggml_blck_size(info.t.type)) > SIZE_MAX/ggml_type_size(info.t.type)"),
19
    ("info.t.nb[1] = info.t.nb[0]*(info.t.ne[0]/blck_size);",
20
     "info.t.nb[1] = info.t.nb[0]*(blck_size?info.t.ne[0]/blck_size:0);"),
21
]
22
n = 0
23
for a, b in PATCHES:
24
    if a in src and b not in src:
25
        src = src.replace(a, b); n += 1
26
        print("patched:", a[:50])
27
    elif b in src:
28
        print("already:", a[:50])
29
    else:
30
        print("NOT FOUND:", a[:50])
31
open(p, "w").write(src)
32
print(f"applied {n} fuzz-depth guards")