Skip to content

Reject overlapping safetensors data offsets - #48

Open
glasses666 wants to merge 1 commit into
antirez:mainfrom
glasses666:fix/safetensors-offset-layout
Open

Reject overlapping safetensors data offsets#48
glasses666 wants to merge 1 commit into
antirez:mainfrom
glasses666:fix/safetensors-offset-layout

Conversation

@glasses666

Copy link
Copy Markdown

Summary

  • Validate the global safetensors payload layout after parsing the header.
  • Sort tensors by payload offset before checking contiguity, so unordered JSON headers still load.
  • Reject overlapping ranges and payload bytes that are not covered by tensor metadata.

Why

The safetensors format defines data_offsets relative to the byte buffer and requires the byte buffer to be entirely indexed, with no holes, to prevent polyglot files: https://github.com/huggingface/safetensors#format

h3 already checked each tensor shape against its own byte range, but a header could still make two tensors overlap or leave payload bytes unindexed. This keeps that validation at the header parser boundary so all safetensors consumers share it.

Tests

  • make test
  • clang -std=c11 -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wno-sign-conversion -D_DARWIN_C_SOURCE -I. -fsyntax-only h3_safetensors.c tests/test_h3.c
  • ASan/UBSan h3_tests

The safetensors format relies on tensor data offsets fully describing the byte buffer: offsets are relative to the payload, must be ordered without gaps or overlaps after sorting by offset, and the final end must match the payload size. h3 already checked each tensor shape against its own byte range, but it accepted layouts where two tensor ranges overlapped or left bytes unindexed.

This validates the global offset layout once the header has been parsed and adds a regression with an out-of-order valid header plus overlapping tensor ranges.

Constraint: Safetensors README requires the byte buffer to be entirely indexed and hole-free to prevent polyglot payloads.

Rejected: Pairwise overlap-only scan | would still accept trailing unindexed bytes.

Confidence: high

Scope-risk: narrow

Directive: Keep this check at header parse time so all safetensors consumers share the same trust-boundary validation.

Tested: make test

Tested: ASan/UBSan h3_tests

Not-tested: Full model-weight parity tests requiring released fixtures not present locally
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