Fix out-of-bounds read in CropBase: validate scale attribute length

Merged
CO
6mo ago
+25-0

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).