Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Security] Validate CPU cache_indirection beam indices - #28486

#28486
Merged
kunal-vaishnavi merged 5 commits into
mainmicrosoft/onnxruntime:mainfrom
tlwu/20260512/icm_2microsoft/onnxruntime:tlwu/20260512/icm_2Copy head branch name to clipboard
May 19, 2026
Merged

[Security] Validate CPU cache_indirection beam indices#28486
kunal-vaishnavi merged 5 commits into
mainmicrosoft/onnxruntime:mainfrom
tlwu/20260512/icm_2microsoft/onnxruntime:tlwu/20260512/icm_2Copy head branch name to clipboard

Conversation

@tianleiwu

Copy link
Copy Markdown
Contributor

Summary

  • reject out-of-range cache_indirection beam indices in the CPU beam-attention path before they are converted into past KV offsets
  • keep DecoderMaskedMultiHeadAttention beam-width handling consistent with the cache_indirection shape
  • add CPU regression tests for MultiHeadAttention and DecoderMaskedMultiHeadAttention

Motivation

MultiHeadAttention and DecoderMaskedMultiHeadAttention on the CPU provider could consume attacker-controlled cache_indirection values as beam indices without validating that each element stayed within [0, beam_width). That let malformed models compute offsets past the past key/value buffers. This change rejects invalid indices up front and adds focused tests for the failure path.

Key Changes

  • add shared CPU validation in AttentionCPUBase::ApplyAttentionWithBeams so the beam path fails before any past-key or past-value reads occur
  • report an INVALID_ARGUMENT error that identifies the offending beam index and its position
  • validate that an explicit decoder beam_width input matches cache_indirection dimension 1 when both are present
  • add contrib-op tests that exercise invalid cache indirection values on the CPU execution provider

Testing

  • lintrunner -a
  • cd build/Linux/Debug && make -j4 CMakeFiles/onnxruntime_providers.dir/home/tlwu/onnxruntime/onnxruntime/contrib_ops/cpu/bert/multihead_attention.cc.o CMakeFiles/onnxruntime_providers.dir/home/tlwu/onnxruntime/onnxruntime/contrib_ops/cpu/bert/decoder_masked_multihead_attention.cc.o CMakeFiles/onnxruntime_provider_test.dir/home/tlwu/onnxruntime/onnxruntime/test/contrib_ops/multihead_attention_op_test.cc.o CMakeFiles/onnxruntime_provider_test.dir/home/tlwu/onnxruntime/onnxruntime/test/contrib_ops/decoder_masked_multihead_attention_op_test.cc.o
  • full onnxruntime_provider_test relink/run was not completed locally

tianleiwu added 4 commits May 13, 2026 01:37
…ests

Both tests crashed with null pointer dereferences because present_key
and present_value were marked as optional output edges. The DMMHA and
MHA compute functions unconditionally dereference present_key/present_value
when past_present_share_buffer is enabled, causing SEGV/SEH exceptions
before the cache indirection validation could run.

Provide actual output tensors so the code reaches ValidateCacheIndirectionValues.
…ember

DecoderMaskedMultiHeadAttentionParameters shadows AttentionParameters::beam_width
with a default of 1. CheckInputs sets the base class member via reinterpret_cast,
but the Compute function reads the derived class's shadowed member which stays at
1. Read the beam width from cache_indirection dim 1 directly instead.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the CPU-provider beam-attention path by validating cache_indirection beam indices before they are used to compute past-K/V offsets, and adds regression coverage for invalid indices.

Changes:

  • Add CPU-side validation for cache_indirection indices in the beam attention path (AttentionCPUBase::ApplyAttentionWithBeams).
  • Keep DecoderMaskedMultiHeadAttention beam width handling consistent with cache_indirection’s shape (and error out on mismatch).
  • Add CPU regression tests for invalid cache_indirection values in MultiHeadAttention and DecoderMaskedMultiHeadAttention.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
onnxruntime/contrib_ops/cpu/bert/attention_cpu_base.h Adds shared validation for cache_indirection indices in the CPU beam path.
onnxruntime/contrib_ops/cpu/bert/decoder_masked_multihead_attention.cc Aligns decoder beam width handling with cache_indirection shape and validates mismatch.
onnxruntime/test/contrib_ops/multihead_attention_op_test.cc Adds a CPU regression test expecting failure on out-of-range cache_indirection values.
onnxruntime/test/contrib_ops/decoder_masked_multihead_attention_op_test.cc Adds a CPU regression test for invalid cache_indirection indices in decoder MHA.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/contrib_ops/cpu/bert/attention_cpu_base.h Outdated
Comment thread onnxruntime/test/contrib_ops/multihead_attention_op_test.cc
@tianleiwu

Copy link
Copy Markdown
Contributor Author

@copilot apply changes based on the comments in this thread

Copilot AI commented May 15, 2026

Copy link
Copy Markdown
Contributor

@copilot apply changes based on the comments in this thread

Implemented in 18c947b. I updated CPU cache_indirection validation to also check beam_width==1 (only index 0 allowed), and added a new MultiHeadAttention CPU regression test for cache_indirection shape with beam_width==1 containing an invalid index. I also rebuilt and ran the targeted regression tests, which now pass.

@kunal-vaishnavi

kunal-vaishnavi commented May 16, 2026

Copy link
Copy Markdown
Contributor

There is a known parity issue between the CPU and CUDA implementations for DecoderMaskedMultiHeadAttention.

If I recall correctly, the CPU unit tests are not correct as well and the Whisper CPU model with DecoderMaskedMultiHeadAttention does not produce good results. The Whisper CUDA model works well with this kernela and the CUDA unit tests are correct.

Does this PR fix the issue?

@tianleiwu

Copy link
Copy Markdown
Contributor Author

There is a known parity issue between the CPU and CUDA implementations for DecoderMaskedMultiHeadAttention.

If I recall correctly, the CPU unit tests are not correct as well and the Whisper CPU model with DecoderMaskedMultiHeadAttention does not produce good results. The Whisper CUDA model works well with this kernela and the CUDA unit tests are correct.

Does this PR fix the issue?

No, this PR is for security. That parity issue can be addressed in another PR.

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.

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.