Commit a21e718
Validate eos_token_id against vocab_size to prevent OOB write in ApplyMinLength (#2266)
## Summary
Fixes an out-of-bounds write (CWE-787 / CWE-129) reachable from a
malicious `genai_config.json`.
`Search_Cpu::ApplyMinLength` suppresses EOS tokens while the sequence is
shorter than `min_length` by writing
`std::numeric_limits<float>::lowest()` into the per-beam score row at
each configured `eos_token_id`:
```cpp
std::span<float> const beam_token_scores = GetScores(i); // vocab_size elements
for (auto token_id : params_->config.model.eos_token_id)
beam_token_scores[token_id] = std::numeric_limits<float>::lowest(); // unchecked index
```
`eos_token_id` comes straight from config (`src/config.cpp`) with only a
`static_cast<int>` and is never bounded against `vocab_size`. A model
whose `eos_token_id >= vocab_size` (or `< 0`) drives the store past the
end of the `vocab_size`-sized `GetScores()` subspan — a
heap-buffer-overflow write on the first decode step (reached via
`Generator::GenerateNextToken` -> `ApplyMinLength`). The same pattern
exists in the CUDA backend (`Search_Cuda::ApplyMinLength`).
## Changes
- **`src/generators.cpp`**: reject any `eos_token_id` outside `[0,
vocab_size)` in the `Generator::Generator` validator, mirroring the
existing `top_k <= vocab_size` check (added in #2224).
- **`src/search.cpp`** and **`src/cuda/search_cuda.cpp`**:
defense-in-depth — skip out-of-range ids at the sink in both backends.
- **`test/sampling_tests.cpp`**: add
`EosTokenIdExceedsVocabSizeThrowsCpu`, which overlays an out-of-range
`eos_token_id` and asserts `OgaGenerator::Create` throws instead of
proceeding to the OOB write.
## Testing
New regression test follows the existing `SamplingTests` overlay pattern
(`tiny-random-gpt2-fp32` + `vocab_size`/`eos_token_id` overlay). Local
build isn't available in my environment; relying on CI to build and run
the C++ unit tests.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent b58789f commit a21e718
2 files changed
Lines changed: 37 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
405 | 405 | | |
406 | 406 | | |
407 | 407 | | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
408 | 416 | | |
409 | 417 | | |
410 | 418 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
105 | 126 | | |
106 | 127 | | |
107 | 128 | | |
| |||
110 | 131 | | |
111 | 132 | | |
112 | 133 | | |
113 | | - | |
| 134 | + | |
114 | 135 | | |
115 | 136 | | |
116 | 137 | | |
| |||
244 | 265 | | |
245 | 266 | | |
246 | 267 | | |
247 | | - | |
| 268 | + | |
248 | 269 | | |
249 | 270 | | |
250 | 271 | | |
| |||
506 | 527 | | |
507 | 528 | | |
508 | 529 | | |
509 | | - | |
| 530 | + | |
510 | 531 | | |
511 | 532 | | |
512 | 533 | | |
| |||
536 | 557 | | |
537 | 558 | | |
538 | 559 | | |
539 | | - | |
| 560 | + | |
540 | 561 | | |
541 | 562 | | |
542 | 563 | | |
| |||
569 | 590 | | |
570 | 591 | | |
571 | 592 | | |
572 | | - | |
| 593 | + | |
573 | 594 | | |
574 | 595 | | |
575 | 596 | | |
| |||
728 | 749 | | |
729 | 750 | | |
730 | 751 | | |
731 | | - | |
| 752 | + | |
732 | 753 | | |
733 | 754 | | |
734 | 755 | | |
| |||
0 commit comments