Prevent out-of-bounds memory access caused by corrupt tensor.ndim in gguf file#3359
Prevent out-of-bounds memory access caused by corrupt tensor.ndim in gguf file#3359MillaFleurs wants to merge 5 commits intoml-explore:mainfrom
Conversation
…r gguf specs where dim = 4
zcbenz
left a comment
There was a problem hiding this comment.
I don't think this can prevent a bad/malicious model file.
For example the file could provide ndim: 4 with a tensor of 0 size, which would still crash the reading program.
|
You're right that this doesn't prevent all malformed files — but it's not intended to. This specifically fixes an out-of-bounds memory read where ndim > 8 causes get_shape() to read past the end of the fixed-size dim[8] array into adjacent stack memory. A zero-dimension tensor with ndim=4 stays within array bounds and doesn't trigger the same class of bug. Additional validation for semantic issues like zero-size tensors could be a good follow-up, but is orthogonal to this memory safety fix. |
zcbenz
left a comment
There was a problem hiding this comment.
Thanks for the clarification, to be honest I think it should be checked in gguflib, can you send a PR there?
Removed note about GGUF file loading validation.
|
I made the requested changes @zcbenz . Thank you for the feedback it's extremely helpful and insightful. |
I will file a PR on github.com/antirez/gguf-tools as well that's a great idea. We can do both as well. By keeping the |
|
The test does not work since the assertion works under debug build, I think we can |
Proposed changes
Fix for #3358 get_shape() lacks bounds checking.
The GGUF tensor loader in Apple MLX trusts the
ndimfield from a crafted GGUF file without bounds-checking it against the fixed-sizedim[]array in thegguf_tensorstruct. In release builds, the upstreamgguflib.cassert()guard is compiled out (NDEBUG), leaving no enforcement. MLX'sget_shape()function iteratesndimtimes over the stack-allocateddim[]array (maximum 8 elements), reading beyond its bounds whenndim > 8.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes