Skip to content

Reject tensors with ndim > GGUF_TENSOR_MAX_DIM (fix OOB write in #25)#26

Open
ValheruEldarr wants to merge 1 commit into
antirez:mainfrom
ValheruEldarr:fix/tensor-ndim-oob-write
Open

Reject tensors with ndim > GGUF_TENSOR_MAX_DIM (fix OOB write in #25)#26
ValheruEldarr wants to merge 1 commit into
antirez:mainfrom
ValheruEldarr:fix/tensor-ndim-oob-write

Conversation

@ValheruEldarr

Copy link
Copy Markdown

Fixes the out-of-bounds write reported in #25 (CWE-787).

gguf_get_tensor read the tensor dimension count ndim straight from the file and wrote that many 64-bit values into the fixed uint64_t dim[GGUF_TENSOR_MAX_DIM] array (== 8). The only guard was an assert(tensor->ndim <= GGUF_TENSOR_MAX_DIM), which is compiled out under -DNDEBUG (the usual release build), so a .gguf declaring ndim > 8 wrote past dim[].

This returns 0 (stops the tensor iteration, exactly like the existing *type >= GGUF_TYPE_COUNT rejection a few lines below) when ndim > GGUF_TENSOR_MAX_DIM, instead of writing out of bounds.

Verified: built with -DNDEBUG -fsanitize=address,undefined, the PoC from #25 (gguf-tools show poison.gguf) no longer reports a stack-buffer-overflow WRITE or the index 8 out of bounds for type 'uint64_t[8]' UBSan error. For valid tensors (ndim <= 8; the GGUF spec maximum is 4) the new branch is not taken, so behavior is unchanged.

Note: #25 also reports out-of-bounds reads in the KV / metadata parsing (for example gguf_get_key indexing ctx->data by an unchecked in-file str->len). Those require bounding each ctx->data + offset access against ctx->size and are a larger, separate change; this PR addresses the memory-corruption write only.

…x OOB write, antirez#25)

gguf_get_tensor read the tensor dimension count (ndim) from the file and wrote that
many entries into the fixed uint64_t dim[GGUF_TENSOR_MAX_DIM] array, guarded only by
an assert() that is compiled out under -DNDEBUG (the usual release build). A .gguf
declaring ndim > 8 therefore caused an out-of-bounds write. Return 0 (stop iteration)
instead. For valid tensors (ndim <= 8; the GGUF maximum is 4) behavior is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant