fix(ml): use SafeInt checked arithmetic in ML operator coefficient size validation

Merged
TI
7mo ago
+156-15

Replaces unchecked static_cast<size_t> multiplications in coefficient/support-vector size guards with SafeInt<size_t> arithmetic. Without this, a crafted model with adversarial dimension values could cause the product to silently wrap around, bypassing the validation entirely before downstream GEMM/dot-product reads.

Also improves the SVMClassifier feature-count mismatch error to include expected feature_count, actual num_features, input_rank, and num_batches for easier diagnosis.

Changes

  • linearclassifier.cc — SafeInt<size_t>(class_count_) * SafeInt<size_t>(num_features) with try/catch returning INVALID_ARGUMENT on overflow
  • linearregressor.cc — Same pattern for num_targets_ * num_features
  • svmclassifier.cc — SafeInt overflow protection for both the linear (class_count_ * num_features) and non-linear (vector_count_ * num_features) branches; descriptive error message on feature-count mismatch
  • svmclassifier_test.cc — Update assertion substring to match the new error message

Motivation and Context

Unchecked integer multiplication in size-validation guards can be bypassed by a crafted model whose dimension values wrap around size_t, effectively disabling the bounds check before out-of-bounds memory reads occur downstream. This hardens the existing validation added for LinearClassifier, LinearRegressor, SVMClassifier, and SVMRegressor.