Validate safetensors header fields before using them as sizes - #713
Open
professor-moody wants to merge 1 commit into
Open
professor-moody wants to merge 1 commit into
professor-moody wants to merge 1 commit into
Conversation
The safetensors header is untrusted input, and three values from it were used as allocation sizes or read lengths without being checked against anything else. - SafeTensors: the 8-byte header length was passed straight to new char[configBytes + 5]. That addition wraps for values near UINT64_MAX, giving a 0-4 byte allocation while the following fread still asks for configBytes. Bound it against the actual file size, and check the fopen and fread results, which were also unchecked. - SafeTensorItem: data_offsets was indexed at [1] without checking that two elements were parsed, and there was no check that the end offset is not before the beginning. - SafeTensorItem: the tensor buffer is allocated from the shape product but filled by fread with the data_offsets span. Those are independent header fields, so they can disagree. Reconcile them once, at parse time, against the size implied by shape and dtype. The shape product and the shape-times-dtype product are also checked for overflow. Valid models are unaffected: the new checks only reject headers whose own fields contradict each other. Fixes ztxz16#712
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.
Fixes #712.
Three values from the
.safetensorsheader were used as allocation sizes or read lengths without being checked against anything else. Loading a crafted file writes past the end of heap allocations.SafeTensors(src/model.cpp:1430-1435). The 8-byte header length went straight intonew char[configBytes + 5]. That addition wraps for values nearUINT64_MAX, so the allocation becomes 0-4 bytes while the followingfreadstill asks forconfigBytes. Now bounded against the real file size. Thefopenresult and the firstfreadreturn were also unchecked, so a short or unreadable file leftconfigBytesuninitialised.SafeTensorItem(src/model.cpp:900-912).data_offsets[1]was indexed without checking that two elements were parsed, and there was no check that the end offset is not before the beginning.SafeTensorItem::CreateBuffer(src/model.cpp:1383-1385). The tensor buffer is allocated from the shape product and then filled byfreadwith thedata_offsetsspan. Those are independent header fields and can disagree. Theelsebranch has the mirror problem, whereConvertDataTypecopieslen * unitSizeout of a buffer allocated withbytes.All three are now checked at parse time, where the fields are first read, rather than at each point of use.
Testing
Against the crafted files from #712, on a build with
-fsanitize=addressand_FORTIFY_SOURCEdisabled so the writes are visible rather than caught by glibc:[32,8],data_offsetsspan 65536data_offsets: [0]data_offsets: []2^64-1, 4 KB body2^64-1, no bodySeven crafted inputs in total are rejected, each by the specific check intended for it. A model directory whose header fields agree still loads to inference.
I have the crafted files if they would be useful for a regression test.