fix: bound tensor header length during deserialization#208
Open
dfgvaetyj3456356-hash wants to merge 1 commit into
Open
fix: bound tensor header length during deserialization#208dfgvaetyj3456356-hash wants to merge 1 commit into
dfgvaetyj3456356-hash wants to merge 1 commit into
Conversation
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.
Summary
Bounds each tensor header length against the metadata span that already describes where that tensor's data begins.
Before this change,
_TensorHeaderDeserializer.from_io()trusted the 8-byte header length stored in the file and allocated abytearray(header_len)before checking whether that length fit the metadata entry for the tensor. A malformed.tensorsfile could therefore make the lazy deserializer allocate based on an attacker-controlled header length, and small oversized headers could also be accepted because missing bytes were effectively padded in the header buffer.Fix
max_header_lenguard to_TensorHeaderDeserializer.from_io()._copy_thread(), derive the legal header span from the metadata entry at the current file offset:entry.data_offset - entry.offset.Tests
python -m py_compile tensorizer\serialization.py tests\test_serialization.pypython -m pytest tests\test_serialization.py::TestSerialization::test_oversized_tensor_header_is_rejected -qpython -m pytest tests\test_serialization.py::TestDeserialization::test_lazy_load -qpython -m pytest tests\test_serialization.py::TestSerialization::test_oversized_tensor_header_is_rejected tests\test_serialization.py::TestDeserialization::test_lazy_load -qgit diff --checktest_lazy_loademits the existing Hugging Face cache symlink warning on Windows, but passes.