Summary
A heap out-of-bounds write vulnerability in ncnn's parameter loading logic allows malicious .param model files to corrupt heap memory when parsed.
If an attacker can supply or replace the model parameter file, they may be able to trigger memory corruption, potentially leading to crashes or code execution during neural network initialization.
Affected context
This vulnerability occurs in applications that embed the ncnn inference framework and dynamically load model definitions from .param files.
It becomes relevant when the model parameters are not fully trusted—for example, when they are downloaded, side-loaded, or otherwise modifiable by an attacker or untrusted party.
The issue is specifically in ncnn::ParamDict::load_param() invoked by Net::load_param(), where parameter IDs derived from the file can cause incorrect indexing into an internal parameter array.
Exploit preconditions
- Attack surface: Parsing of
.parammodel files by ncnn at runtime - Required access: Ability to provide, replace, or tamper with the
.paramfile consumed byNet::load_param() - Trigger point: Loading a crafted model file that encodes a negative or otherwise invalid parameter ID
- Why exploitation is possible: The code validates only that
id >= NCNN_MAX_PARAM_COUNT, but does not reject negative IDs, allowing them to be used as indices and write before the start of theparams[NCNN_MAX_PARAM_COUNT]heap array
Why this happens
The underlying bug is improper validation of externally supplied indices prior to array access.
When parsing a .param file, the parameter ID is treated as an array index and only checked against a maximum boundary (id >= NCNN_MAX_PARAM_COUNT), without enforcing a non-negative lower bound.
This maps to CWE-129: Improper Validation of Array Index and CWE-787: Out-of-bounds Write, where untrusted input controls the index used for writing into a fixed-size array.
Additionally, the broader issue reflects CWE-20: Improper Input Validation, as the model file content is not comprehensively validated before being used in memory accesses.
Potential impact
- Application crashes due to heap corruption when loading a malicious model file
- Silent data corruption affecting model parameters or adjacent heap structures
- Potential execution of attacker-controlled code, depending on allocator behavior and surrounding mitigations
- Denial of service for services that perform inference on user-supplied or third-party models
General mitigation guidance
Mitigation generally focuses on tightening input validation around indices and constraining trust in model files.
Common defensive patterns include:
- Enforcing both upper and lower bounds on IDs and indices before accessing arrays (e.g., requiring
0 <= id && id < MAX) - Treating model files as untrusted input and performing robust structural and semantic validation before loading them into core data structures
- Using safer container abstractions or checked-access helpers instead of raw arrays where practical
- Applying defense-in-depth measures such as compiler hardening flags, heap hardening, and running inference components with least-privilege and sandboxing to limit the blast radius of memory corruption
How teams detect issues like this
Potential detection strategies include:
- Static analysis rules that flag array or pointer writes using indices derived from external or file-based input without full range checking (both lower and upper bounds)
- Code review checklists emphasizing validation of model and configuration parsers, with special attention to loop bounds and index arithmetic
- Fuzz testing of model loading routines using malformed and random
.paramfiles to expose memory safety issues - Integration of memory-safety tooling (e.g., AddressSanitizer or similar) in CI pipelines to catch heap out-of-bounds writes under test workloads

