Bound tensor data offset/size against the mapping in gguf_get_tensor (follow-up to #28)#33
Open
gigioneggiando wants to merge 1 commit into
Open
Conversation
Follow-up to antirez#28. That PR bounds the metadata / tensor-info walk, but the tensor DATA region is still unchecked: gguf_get_tensor computes tensor->offset = ctx->data_off + *offset and tensor->weights_data = ctx->data + tensor->offset from a raw file-supplied 64-bit offset, with no check that the range stays inside the mapping. Any later read of the weights (gguf_tensor_to_float, inspect-tensor, compare) is then an out-of-bounds read. Compute bsize first, then verify (overflow-safe) that *offset and the resulting absolute offset are within ctx->size and that the bsize-byte extent fits, before forming weights_data. Reject with return 0 otherwise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #28, as suggested there.
#28 bounds the metadata / tensor-info walk, but the tensor data region is still unchecked. In
gguf_get_tensor():*offsetand the resultingtensor->bsizeare never checked againstctx->size, so a crafted tensor offset/size placesweights_data(or itsbsize-byte extent) outside the mapping. Any later read of the weights (gguf_tensor_to_float,inspect-tensor,compare) is then an out-of-bounds read.Reproduction
A one-tensor GGUF whose data offset is 1 MiB (well past the file), driven through the real
gguf_get_tensor:Fix
Compute
bsizefirst, then verify (overflow-safe) that*offsetand the resulting absolute offset are withinctx->sizeand that thebsize-byte extent fits, beforeweights_datais formed; return 0 otherwise.This touches only the offset/
bsizetail ofgguf_get_tensor, not the dimension/ndimarea #28 changes, so it should apply cleanly alongside #28 (happy to rebase on top of it if you merge #28 first).