Fix stack buffer overflows in ynnpack channelwise quantized tensor and reduce#9843
Open
mohammadmseet-hue wants to merge 2 commits intogoogle:masterfrom
Open
Conversation
dsharlet
reviewed
Apr 1, 2026
…d reduce Bug 1: xnn_define_channelwise_quantized_tensor_value_v3 (tensor.cc:146) std::copy_n(dims, channel_dim + 1, quantization_dims) copies channel_dim + 1 elements into quantization_dims[YNN_MAX_TENSOR_RANK] (size 8) without checking channel_dim < num_dims or channel_dim < YNN_MAX_TENSOR_RANK. With channel_dim >= 8, this writes past the stack buffer. ASAN trace: ==ERROR: AddressSanitizer: stack-buffer-overflow WRITE of size 88 google#8 xnn_define_channelwise_quantized_tensor_value_v3 [96, 160) 'quantization_dims' (line 145) <== overflows this variable Fix: Add channel_dim >= num_dims and num_dims > YNN_MAX_TENSOR_RANK checks. Bug 2: get_reduce_identity_value (reduce.cc:243) For ynn_reduce_min_max with keep_dims=true on a rank-8 tensor, output.extents.push_back(2) increases rank to 9. Then dims[rank - 1] = dims[8] writes one element past the size-8 stack array. Fix: Add rank bounds check before array access.
- tensor.cc: Split combined check into separate num_dims and channel_dim validations with YNN_LOG_ERROR messages. Replace asserts with proper error returns for channelwise_zero_point. Remove assert(data) per reviewer (XNNPACK limitation, not YNNPACK). - reduce.cc: Change define_reduce to return ynn_status. Add output rank validation after min_max dimension push. Keep rank >= 1 as assert (internal invariant). Propagate error via YNN_RETURN_IF_ERROR at call site.
be986be to
f781ad1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two stack buffer overflows in ynnpack:
Bug 1: Stack buffer overflow in
xnn_define_channelwise_quantized_tensor_value_v3File:
ynnpack/xnnpack/tensor.cc:146std::copy_n(dims, channel_dim + 1, quantization_dims)copieschannel_dim + 1elements intoquantization_dims[YNN_MAX_TENSOR_RANK](size 8) without checkingchannel_dim < num_dimsorchannel_dim < YNN_MAX_TENSOR_RANK. Additionally,xnn_validate_channelwise_quantized_tensor(line 112-116) is empty (return xnn_status_success).With
channel_dim >= 8, this writes past the end of the stack buffer — 88 bytes of controlled overflow confirmed.ASAN output:
Reachable from public API:
xnn_define_channelwise_quantized_tensor_value(),xnn_define_channelwise_quantized_tensor_value_v2(),xnn_define_channelwise_quantized_tensor_value_v3().Fix: Add
channel_dim >= num_dims || num_dims > YNN_MAX_TENSOR_RANKcheck before thestd::copy_n.Bug 2: Stack buffer overflow in
get_reduce_identity_valueFile:
ynnpack/subgraph/reduce.cc:243For
ynn_reduce_min_maxwithkeep_dims=trueon a rank-8 tensor,output.extents.push_back(2)(line 345) increases the output rank to 9. Thenget_reduce_identity_valuecomputesrank = output.rank()= 9 and accessesdims[rank - 1]=dims[8], writing one element past thesize_t dims[YNN_MAX_TENSOR_RANK](size 8) stack array.Fix: Add rank bounds check in the
ynn_reduce_min_maxcase before accessingdims[rank - 1].