Fix GQA out-of-bounds read in past KV buffer (CPU)#29447
Open
tianleiwu wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a security/correctness issue in the CPU GroupQueryAttention contrib op where seqlens_k validation did not fully bound reads from the past KV cache during non-prompt execution, potentially leading to an out-of-bounds read. It adds an additional bounds check for the number of past rows copied and introduces a regression test covering the reported scenario.
Changes:
- Add a decode-path validation to ensure
(seqlens_k + 1 - sequence_length)does not exceed the past KV buffer sequence length. - Add a regression test (
SeqlensKExceedsPastBuffer_OOBRead) to exercise the large-seqlens_k/ small-past-buffer case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
onnxruntime/contrib_ops/cpu/bert/group_query_attention.cc |
Adds past-buffer bounds validation for decode to prevent past KV OOB reads. |
onnxruntime/test/contrib_ops/group_query_attention_op_test.cc |
Adds a regression test to ensure invalid seqlens_k is rejected when it would over-read past KV. |
| // Shared KV (kv_sequence_length == 0) appends no new KV and its past read is already | ||
| // bounded by the present-buffer check together with the total_sequence_length <= | ||
| // seqlen_past_kv_cache enforcement in the apply-attention paths, so it needs no check here. | ||
| if (past_kv_seqlen > 0 && parameters.kv_sequence_length != 0 && !parameters.is_first_prompt) { |
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
Fixes an out-of-bounds (OOB) read in the CPU
GroupQueryAttention(GQA) operator. During token generation (decode),ConcatStateChunkGQAcopiesseqlens_k + 1 - sequence_lengthrows out of the past key/value buffers. The existing validation only bounded the present buffer write (seqlens_k < present_kv_seqlen), but the present buffer can be larger than the past buffer whentotal_sequence_lengthexceeds the past sequence length. A largeseqlens_kcombined with a small past buffer therefore read past the end of the past key/value tensors.Motivation and Context
present_kv_seqlen = max(total_sequence_length, past_sequence_length), so the pre-existingseqlens_k < present_kv_seqlencheck does not bound the past-side read whentotal_sequence_length > past_sequence_length. With a craftedseqlens_k, the decode path inConcatStateChunkGQAreadsseqlens_k + 1 - sequence_lengthrows from the smaller past buffer, causing an OOB read.Key Changes
onnxruntime/contrib_ops/cpu/bert/group_query_attention.ccseqlens_kvalidation block: for the decode case (past_kv_seqlen > 0 && kv_sequence_length != 0 && !is_first_prompt), reject whenseqlens_k + 1 - sequence_length > past_kv_seqlen.onnxruntime/test/contrib_ops/group_query_attention_op_test.ccSeqlensKExceedsPastBuffer_OOBReadexercising a largeseqlens_kagainst a small past buffer.Shared KV (
kv_sequence_length == 0) appends no new KV and its past read is already bounded by the present-buffer check together with thetotal_sequence_length <= seqlen_past_kv_cacheenforcement in the apply-attention paths, so it needs no additional check.Testing Notes
SeqlensKExceedsPastBuffer_OOBReadregression and the existing shared-KV CPU cases.lintrunnerreports no issues on the changed files.