Commit a208df8
Use SafeInt for size arithmetic in CPU tensor operators to prevent overflow (#28060)
### Description
Replace unchecked `int64_t` size/offset arithmetic with
`SafeInt<size_t>` across several CPU operator implementations to prevent
silent integer overflow when computing buffer offsets and allocation
sizes.
All changed expressions compute non-negative element counts or byte
offsets used in pointer arithmetic, `memset`, `std::copy_n`,
`std::fill_n`, or allocator calls. On models with large tensor
dimensions the intermediate products (e.g., `N * C * H * W`) can
overflow `int64_t` before the result is used. Wrapping the leading
factor in `SafeInt<size_t>()` ensures every intermediate multiplication
is overflow-checked and produces a `size_t` result.
### Motivation and Context
Integer overflow in size calculations can lead to undersized
allocations, out-of-bounds memory access, or incorrect pointer offsets —
all of which are security-sensitive. This change hardens the affected
code paths against such overflow.
### Key Changes
| File | Change |
|---|---|
| `core/providers/cpu/tensor/grid_sample.cc` | Wrap grid/input/output
offset computations with `SafeInt<size_t>`, chain all factors through
SafeInt instead of parenthesized sub-expressions |
| `core/providers/cpu/tensor/affine_grid.cc` | Wrap batch offset and
Eigen map size computations with `SafeInt<size_t>` |
| `core/providers/cpu/tensor/upsample_antialias.h` | Replace
`narrow<size_t>(a * b)` and `static_cast<size_t>(a * b)` with
`SafeInt<size_t>(a) * b` for temp buffer sizes, span extents, and copy
counts |
| `core/providers/cpu/nn/tfidfvectorizer.cc` | Wrap `memset` byte-count
computation with `SafeInt` |
| `core/providers/cpu/quantization/qlinearconv.cc` | Wrap `Alloc()` /
`MakeUniquePtr` size computation with `SafeInt` |
| `core/providers/cpu/quantization/quantize_linear.cc` | Wrap sub-byte
quantization total-size computation with `SafeInt` |
| `core/providers/cpu/sequence/sequence_ops.cc` | Wrap `SplitToSequence`
offset and copy-count computations with `SafeInt` |
### Testing
Existing unit tests cover the functional behavior of all affected
operators. The change is purely defensive — it makes previously
unchecked arithmetic throw on overflow instead of silently wrapping,
with no change to behavior for in-range inputs.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>1 parent 3f74b3c commit a208df8
File tree
7 files changed
+101
-63
lines changed- onnxruntime/core/providers/cpu
- nn
- quantization
- sequence
- tensor
7 files changed
+101
-63
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
382 | 382 | | |
383 | 383 | | |
384 | 384 | | |
385 | | - | |
| 385 | + | |
386 | 386 | | |
387 | 387 | | |
388 | 388 | | |
| |||
Lines changed: 5 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
254 | | - | |
| 254 | + | |
| 255 | + | |
255 | 256 | | |
256 | 257 | | |
257 | 258 | | |
| |||
439 | 440 | | |
440 | 441 | | |
441 | 442 | | |
442 | | - | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
443 | 446 | | |
444 | 447 | | |
445 | 448 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
872 | 872 | | |
873 | 873 | | |
874 | 874 | | |
875 | | - | |
| 875 | + | |
876 | 876 | | |
877 | 877 | | |
878 | 878 | | |
| |||
890 | 890 | | |
891 | 891 | | |
892 | 892 | | |
893 | | - | |
| 893 | + | |
894 | 894 | | |
895 | 895 | | |
896 | 896 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
517 | 518 | | |
518 | 519 | | |
519 | 520 | | |
520 | | - | |
| 521 | + | |
| 522 | + | |
521 | 523 | | |
522 | 524 | | |
523 | 525 | | |
| |||
528 | 530 | | |
529 | 531 | | |
530 | 532 | | |
531 | | - | |
| 533 | + | |
532 | 534 | | |
533 | 535 | | |
534 | 536 | | |
| |||
540 | 542 | | |
541 | 543 | | |
542 | 544 | | |
543 | | - | |
| 545 | + | |
544 | 546 | | |
545 | 547 | | |
546 | 548 | | |
547 | | - | |
548 | | - | |
549 | | - | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
550 | 552 | | |
551 | 553 | | |
552 | 554 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
81 | | - | |
| 82 | + | |
82 | 83 | | |
83 | | - | |
| 84 | + | |
| 85 | + | |
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
| |||
97 | 99 | | |
98 | 100 | | |
99 | 101 | | |
100 | | - | |
| 102 | + | |
101 | 103 | | |
102 | | - | |
| 104 | + | |
| 105 | + | |
103 | 106 | | |
104 | 107 | | |
105 | 108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
379 | 380 | | |
380 | 381 | | |
381 | 382 | | |
382 | | - | |
| 383 | + | |
383 | 384 | | |
384 | 385 | | |
385 | 386 | | |
386 | | - | |
387 | | - | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
388 | 391 | | |
389 | 392 | | |
390 | 393 | | |
| |||
469 | 472 | | |
470 | 473 | | |
471 | 474 | | |
472 | | - | |
| 475 | + | |
473 | 476 | | |
474 | 477 | | |
475 | 478 | | |
476 | | - | |
477 | | - | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
478 | 486 | | |
479 | 487 | | |
480 | 488 | | |
| |||
0 commit comments