Fix missing bounds checks in subgraph API functions#9845
Open
mohammadmseet-hue wants to merge 1 commit intogoogle:masterfrom
Open
Fix missing bounds checks in subgraph API functions#9845mohammadmseet-hue wants to merge 1 commit intogoogle:masterfrom
mohammadmseet-hue wants to merge 1 commit intogoogle:masterfrom
Conversation
Add bounds validation for user-controlled parameters that were used as loop bounds or array indices without checking: - xnn_define_static_expand_dims: num_new_axes was used to write into int32_t ynn_axes[XNN_MAX_TENSOR_DIMS] without bounds check. Add check that num_new_axes <= XNN_MAX_TENSOR_DIMS. - xnn_define_static_reduce: num_reduction_axes was used to write into int64_t signed_reduction_axes[XNN_MAX_TENSOR_DIMS] without bounds check. Add check that num_reduction_axes <= XNN_MAX_TENSOR_DIMS. - ynn_define_broadcast: axes were passed to axis_to_slinky_dim without validate_axis, producing out-of-range bitset indices. Add validate_axis call. - ynn_define_broadcast_like: same missing validate_axis. Add it. - ynn_define_reduce: same missing validate_axis. Add it. - ynn_define_fuse_dims: axes[i]+1 was passed to axis_to_slinky_dim but only axes[i] was validated. When axes[i]=rank-1, axes[i]+1=rank produces an invalid index. Add validate_axis for axes[i]+1.
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
Multiple public API functions in ynnpack accept user-controlled count/axis parameters
that are used as loop bounds or array indices without bounds checking, causing stack
buffer overflows:
xnn_define_static_expand_dims(subgraph.cc):num_new_axesused as loop bound to write intoint32_t ynn_axes[XNN_MAX_TENSOR_DIMS](size 6). ASAN-confirmed stack-buffer-overflow withnum_new_axes > 6.xnn_define_static_reduce(subgraph.cc):num_reduction_axesused as loop bound to write intoint64_t signed_reduction_axes[XNN_MAX_TENSOR_DIMS](size 6). Same pattern.ynn_define_broadcast(broadcast.cc): Missingvalidate_axiscall. Out-of-range axes passed directly toaxis_to_slinky_dim, producing negative results used asbitset<10>indices.ynn_define_broadcast_like(broadcast_like.cc): Same missing validation.ynn_define_reduce(reduce.cc): Same missing validation.ynn_define_fuse_dims(copy.cc): Off-by-one —axes[i]is validated butaxes[i]+1is passed toaxis_to_slinky_dim. Whenaxes[i] = rank-1,axes[i]+1 = rankproduces index -1.Fixes
num_new_axes > XNN_MAX_TENSOR_DIMS/num_reduction_axes > XNN_MAX_TENSOR_DIMSbounds checks before stack array writes.validate_axiscalls beforeaxis_to_slinky_dimin broadcast, broadcast_like, and reduce.validate_axisforaxes[i]+1in fuse_dims.ASAN trace (xnn_define_static_expand_dims, before fix)
Test plan
bazel test //ynnpack/subgraph/test:oob_write_poc --copt=-fsanitize=address --linkopt=-fsanitize=addresspasses