Fix heap OOB write in EmbedLayerNormalizationShapeInference#28176
Merged
Conversation
The condition `ctx.getNumOutputs() == 2 && mask_index_type == 0` tried to write to output index 2 when only outputs 0 and 1 existed, causing a heap out-of-bounds write during model loading. Fix by checking ctx.getNumOutputs() > 2 before accessing output index 2. Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/030beaf6-1876-4192-baf4-48058a0248c6 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix heap OOB write in EmbedLayerNormalization shape inference
Fix heap OOB write in EmbedLayerNormalizationShapeInference
Apr 22, 2026
Member
|
@copilot can you add a unit test? |
…eInference Test verifies that loading a model with EmbedLayerNormalization having mask_index_type=0 and exactly 2 outputs (no embedding_sum) no longer triggers a heap out-of-bounds write during shape inference. Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/1c68348d-6d40-4267-a7b7-5bbd0e2e76d4 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Contributor
Author
Added a C++ regression test |
Contributor
|
Should we also consider guarding output-1 accesses with ctx.getNumOutputs() > 1 to prevent the same class of bug |
vraspar
approved these changes
Apr 22, 2026
…copilot/fix-heap-oob-write-onnxmodel
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.
Description
EmbedLayerNormalizationShapeInferenceunconditionally wrote to output index 2 whengetNumOutputs() == 2 && mask_index_type == 0, causing a heap out-of-bounds write during model loading — nosession.run()required.Fix: Replace the flawed condition with a simple bounds check:
A regression test
EmbedLayerNormBatch1_NoMaskIndex_NoSumOutputhas been added toembed_layer_norm_op_test.ccto cover the previously vulnerable path:mask_index_type=0with exactly 2 outputs (noembedding_sum).Motivation and Context
A crafted ONNX model with an
EmbedLayerNormalizationnode declaring 2 outputs andmask_index_type=0triggers the vulnerable path.getOutputType(2)returns a pointer one past the end of the internalnode_output_types_vector; subsequent writes through that pointer corrupt adjacent heap memory. In release builds this is silent — no assertion, no crash, exploitable via heap shaping.The
embedding_sumoutput is always at index 2 by definition (confirmed in the CPU kernel). The old special-casing formask_index_type == 0was both incorrect and unnecessary.