fix(security): prevent OOB write and integer overflows during model load#45
Open
bytecodesky wants to merge 1 commit intomaderix:mainfrom
Open
fix(security): prevent OOB write and integer overflows during model load#45bytecodesky wants to merge 1 commit intomaderix:mainfrom
bytecodesky wants to merge 1 commit intomaderix:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug: Out-of-Bounds Write (CWE-787) and Unchecked Allocation / NULL Pointer Dereference (CWE-190/CWE-476) via Untrusted Model Configuration
Impact:
The
model_load_weightsfunction reads theConfigstructure directly from an untrusted file without validating its fields. This introduces two critical memory corruption vectors:n_layers(nl) parameter dictates the number of iterations in the weight-loading loop. However, the destination arrays in theModelstruct (e.g.,m->wo[N_LAYERS]) have a statically defined size ofN_LAYERS(12). If an attacker crafts a model file withn_layers > 12, the loop will write heap pointers beyond the bounds of these arrays. This sequentially corrupts adjacent pointer arrays in theModelstruct (including theANEKernelpointers, which encapsulate executable Objective-C/Metal objects), leading to highly exploitable memory corruption and Arbitrary Code Execution (ACE).dim(d) andhidden_dim(hd) parameters are used directly in size calculations for heap allocations (e.g.,d * d * sizeof(float)). Maliciously large values will cause integer overflows or excessive memory requests, causingmallocto fail and returnNULL. Because the code lacksNULLchecks, the subsequentmemcpyoperations immediately dereference theNULLpointer, resulting in a deterministic segmentation fault (Denial of Service).Vulnerable Code:
Proposed Fix:
Implement strict bounds checking on the configuration parameters immediately after reading them from disk. Additionally, enforce
NULLchecks on allocations to ensure graceful failure under memory pressure.