Commit 50d04b9
Fix out-of-bounds read in CropBase: validate scale attribute length (microsoft#28399)
### Description
`CropBase::ValidateInput` guarded `scale_[0]`/`scale_[1]` access with
`!scale_.empty()` but never enforced `scale_.size() == 2`. A malformed
model with a wrong-length `scale` attribute (e.g. 1 element) causes an
out-of-bounds read in both validation and compute paths.
**Changes:**
- **`contrib_ops/cpu/crop.h`** — inside the `!scale_.empty()` branch,
return `INVALID_ARGUMENT` early if `scale_.size() != 2` with a
descriptive message before any indexing occurs. `Compute` is protected
transitively since it calls `ValidateInput` first.
- **`test/contrib_ops/crop_op_test.cc`** — added
`Crop_Invalid_Scale_Size` test: passes a 1-element `scale` attribute and
asserts the expected error is returned.
### Motivation and Context
`scale_` is a user-supplied attribute read directly from the model.
Without length validation a malformed model can trigger undefined
behavior (out-of-bounds vector read) in both the CPU and CUDA `Crop`
kernels (CUDA inherits `CropBase`).
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Co-authored-by: Tianlei Wu <tlwu@microsoft.com>1 parent dcf8775 commit 50d04b9
2 files changed
Lines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
56 | 61 | | |
57 | 62 | | |
58 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
33 | 53 | | |
34 | 54 | | |
0 commit comments