Commit 7c56fa8
Add seqlens_k bounds validation in GroupQueryAttention to prevent GEMM OOB (#28031)
### Description
Validate seqlens_k tensor values in the CPU GroupQueryAttention operator
before they are used as GEMM dimensions. Without this check, a crafted
model can supply negative or oversized seqlens_k values that cause
out-of-bounds reads in the K/V present cache buffers.
Fixes
https://portal.microsofticm.com/imp/v5/incidents/details/31000000559235/summary
### Changes
- **group_query_attention.cc**: Add validation loop in `Compute()`
before any seqlens_k access:
- `seqlens_k[b] >= 0` (prevents unsigned wraparound in
`static_cast<size_t>`)
- `seqlens_k[b] + 1 <= present_kv_seqlen` (prevents GEMM reading past
K/V buffer)
- For non-first-prompt: `seqlens_k[b] + 1 >= sequence_length` (prevents
underflow in `past_seqlen = total_seqlen - sequence_length`)
- **group_query_attention_helper.h**: Fix seqlens_k shape validation
(`&&` to `||`) so wrong-length tensors are correctly rejected
- **Tests**: 4 regression tests covering negative, oversized,
multi-batch, and boundary-valid seqlens_k values
### Motivation and Context
MSRC case 108962: A crafted model can set seqlens_k values that, when
cast to `size_t` and used as GEMM N dimension, cause heap OOB reads from
the present K/V cache buffers.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent fb13eb3 commit 7c56fa8
5 files changed
Lines changed: 342 additions & 10 deletions
File tree
- onnxruntime
- contrib_ops/cpu/bert
- test/contrib_ops
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | | - | |
| 171 | + | |
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
175 | | - | |
| 175 | + | |
176 | 176 | | |
177 | 177 | | |
178 | 178 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
209 | | - | |
| 209 | + | |
210 | 210 | | |
211 | | - | |
| 211 | + | |
212 | 212 | | |
213 | 213 | | |
214 | 214 | | |
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
273 | | - | |
| 273 | + | |
274 | 274 | | |
275 | 275 | | |
276 | 276 | | |
| |||
291 | 291 | | |
292 | 292 | | |
293 | 293 | | |
294 | | - | |
| 294 | + | |
295 | 295 | | |
296 | 296 | | |
297 | 297 | | |
| |||
440 | 440 | | |
441 | 441 | | |
442 | 442 | | |
443 | | - | |
| 443 | + | |
444 | 444 | | |
445 | | - | |
| 445 | + | |
446 | 446 | | |
447 | 447 | | |
448 | 448 | | |
| |||
472 | 472 | | |
473 | 473 | | |
474 | 474 | | |
475 | | - | |
| 475 | + | |
476 | 476 | | |
477 | 477 | | |
478 | 478 | | |
| |||
Lines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
85 | 102 | | |
86 | 103 | | |
87 | 104 | | |
| |||
Lines changed: 5 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
262 | 262 | | |
263 | 263 | | |
264 | 264 | | |
265 | | - | |
| 265 | + | |
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
| |||
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
278 | 282 | | |
279 | 283 | | |
280 | 284 | | |
| |||
0 commit comments