Skip to content

fix: bound tensor header length during deserialization#208

Open
dfgvaetyj3456356-hash wants to merge 1 commit into
coreweave:mainfrom
dfgvaetyj3456356-hash:security/bound-tensor-header-length
Open

fix: bound tensor header length during deserialization#208
dfgvaetyj3456356-hash wants to merge 1 commit into
coreweave:mainfrom
dfgvaetyj3456356-hash:security/bound-tensor-header-length

Conversation

@dfgvaetyj3456356-hash
Copy link
Copy Markdown

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 a bytearray(header_len) before checking whether that length fit the metadata entry for the tensor. A malformed .tensors file 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

  • Add an optional max_header_len guard to _TensorHeaderDeserializer.from_io().
  • In _copy_thread(), derive the legal header span from the metadata entry at the current file offset: entry.data_offset - entry.offset.
  • Reject unexpected header offsets and oversized header lengths before allocating the header buffer.

Tests

  • python -m py_compile tensorizer\serialization.py tests\test_serialization.py
  • python -m pytest tests\test_serialization.py::TestSerialization::test_oversized_tensor_header_is_rejected -q
  • python -m pytest tests\test_serialization.py::TestDeserialization::test_lazy_load -q
  • python -m pytest tests\test_serialization.py::TestSerialization::test_oversized_tensor_header_is_rejected tests\test_serialization.py::TestDeserialization::test_lazy_load -q
  • git diff --check

test_lazy_load emits the existing Hugging Face cache symlink warning on Windows, but passes.

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