Skip to content

Bound file-controlled sizes in view(): levels region and dense vectors span (OOB read on crafted index) - #776

Merged
ashvardanian merged 2 commits into
unum-cloud:main-devfrom
professor-moody:bound-view-levels-and-dense-span
Aug 22, 2026
Merged

ashvardanian merged 2 commits into
unum-cloud:main-devfrom
professor-moody:bound-view-levels-and-dense-span

Conversation

@professor-moody

Copy link
Copy Markdown
Contributor

Summary

index_gt::view (and index_dense::view) read file-controlled sizes and dereference the memory mapping before bounding them, so a crafted .usearch index over-reads the mapping. Two sinks:

  1. Base view() levels region (index.hpp): view sizes a memory-mapped levels[] array from the file's header.size and walks it in the offset-precompute loop before the if (file.size() < total_bytes) check runs. A crafted header declaring more nodes than the file contains reads levels[] past the mapping. Fix: reject when the file does not extend to first_offset (the end of the levels array) before the loop.

  2. Dense view() vectors span (index_dense.hpp): vectors_buffer = {file.data() + offset, matrix_rows * matrix_cols} forms a span with no file-size check (and the product can overflow size_t), and the following if (file.size() - offset < sizeof(buffer)) guard is an unsigned subtraction that underflows once offset passes file.size(). Fix: bound the span with a checked multiply and make the header guard underflow-safe.

Impact

Memory-mapping a crafted or untrusted .usearch index via Index.view / Index.load(..., view=True) yields an out-of-bounds read (crash / adjacent-memory disclosure). CWE-125.

Validation

AddressSanitizer, control vs crafted, driving the real view() over a buffer-backed mapping: a well-formed index loads cleanly; a crafted one (header.size larger than the file / oversized matrix dims) is now rejected with a clear error and no OOB. Found with a structure-aware model/index parser fuzzer.

…s span

index_gt::view walked the memory-mapped levels[] array (sized from the
file's header.size) before the only file-size check, and index_dense::view
formed a matrix_rows*matrix_cols span into the mapping with no bound plus
an unsigned-underflow header guard. A crafted .usearch index over-read the
mapping. Bound the levels region before the offset-precompute loop, bound
the vectors span with a checked multiply, and make the header guard
underflow-safe. Validated with AddressSanitizer (crafted rejected cleanly,
valid indexes still load).
@professor-moody

Copy link
Copy Markdown
Contributor Author

Small correction to the Impact section of this PR: I named a Python entry point that does not exist.

I wrote Index.load(..., view=True). Index.load takes (self, path_or_buffer, progress) and has no view parameter and no **kwargs, so that call raises TypeError. Anyone reproducing from my text would hit that and reasonably wonder whether I had exercised it at all.

The entry points that actually memory-map, and therefore reach view(), are:

Index.view(path_or_buffer)
Index.restore(path_or_buffer, view=True)

Index.view alone is sufficient and is the one I should have written.

Nothing else changes. The two sinks and the fix are unaffected.

`view` takes `offset` publicly, so every `file.size() - offset` in it underflows into a huge span for an offset past the end rather than failing. Guard it once on entry, which makes the added disjunct in the header check redundant.

Add a regression test that truncates a saved index and expects `view` to refuse it, in place and at an offset past the end. Without the bounds checks it segfaults in the `memcpy` that reads the dense header.
@ashvardanian
ashvardanian changed the base branch from main to main-dev August 22, 2026 14:28
@ashvardanian
ashvardanian merged commit 64d10d8 into unum-cloud:main-dev Aug 22, 2026
ashvardanian pushed a commit that referenced this pull request Aug 22, 2026
### Patch

- Fix: Respect Cargo-selected Windows CRT #778 (6bf5f1b)
- Fix: Reserve the requested member capacity #777 (88d9a5e)
- Fix: Bound file-controlled sizes in `view()` (#776) (64d10d8)
- Make: Pin citations, deps, and CI versions (810c172)
xet7 pushed a commit to xet7/usearch that referenced this pull request Nov 12, 2026
The 2.x line released v2.25.2 through v2.26.6 after 3.0 branched at
v2.25.1, and its public main ends at 33d3ab6. Installs place headers
under GNUInstallDirs' include directory (14f9807, unum-cloud#800), the README
lists the integrations 2.x added (0fd2f32, unum-cloud#795), and CONTRIBUTING
states the commit conventions in place of stale links (06ad060, unum-cloud#748).

Its correctness fixes already hold on 3.0's code:

- Keys move with their entries when `compact` permutes slots
  (0a4baa6, unum-cloud#795), and the insert-only key table never rehashes
  (1633e25, unum-cloud#822; 9d77be5, unum-cloud#759)
- File loads bound every column slice from the header (9c25176, unum-cloud#824;
  64d10d8, unum-cloud#776), and allocation sizes are checked (89da7c5, unum-cloud#763)
- JavaScript keys are range-checked (b65a420, unum-cloud#812), Go filters ride
  `cgo.Handle`s (ad77fe7, unum-cloud#825), `splitmix64_t` survives `windows.h`
  (0f1c5f2, unum-cloud#806), and integer key hashes mix with SplitMix64
  (9fc3500, unum-cloud#773)
- C install targets and relocatable pkg-config (4160e47, 679e190,
  unum-cloud#800), the GIL released in long calls (d8be67d, unum-cloud#764), and graph
  cursors in C and Rust (55c43f7, unum-cloud#748)

Thread-context reserves, tombstones, renames, `change_metric`, OpenMP
metrics and hand-written BF16 casts are absent from 3.0, so their
fixes have nothing to land on. The Rust `compact()` binding (749d03b,
unum-cloud#771) waits for all bindings to wrap `usearch_dense_compact`.

The README's performance lead now quotes the SIFT 100M rows at 99%
recall on a B300 and on 192 Graviton5 cores beside FAISS HNSW, and
the verification notes link ForkUnion's `main`.

The sibling pins move to the libraries' own merges with their released
lines, so this line builds against them.

Co-Authored-By: Jonathan Ringer <[email protected]>
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.

2 participants