Skip to content

Guard convolution/deconvolution packed weights size against integer overflow - #11184

Open
esoalba wants to merge 1 commit into
google:masterfrom
esoalba:fix/packed-weights-size-overflow
Open

esoalba wants to merge 1 commit into
google:masterfrom
esoalba:fix/packed-weights-size-overflow

Conversation

@esoalba

@esoalba esoalba commented Sep 10, 2026

Copy link
Copy Markdown

Summary

packed_group_weights_size is computed from the caller-supplied
group_input_channels / group_output_channels without any overflow checks,
while only kernel_height * kernel_width is guarded via xnn_safe_mul:

size_t packed_group_weights_size =
    ((kernel_size * k_stride << log2_filter_element_size) +
     bias_element_size + extra_weights_bytes) *
    n_stride;

When the product wraps size_t, aligned_total_weights_size is far smaller
than the amount of data the weight-packing microkernel writes into it, so
xnn_pack_f32_conv_goki_w writes past the end of the allocation — a heap
out-of-bounds write.

Two affected sites:

  • src/operators/convolution-nhwc.c — create_igemm, reached from
    xnn_create_convolution2d_nhwc_f32
  • src/operators/deconvolution-nhwc.c — create_deconvolution2d_nhwc,
    reached from xnn_create_deconvolution2d_nhwc_f32 (including the
    is_subconv2d branch)

The entry points only reject zero-valued channel counts, so oversized values
reach the unguarded arithmetic.

Reproduction

With group_input_channels = 2^30, group_output_channels = 2^28,
groups = 1 and a 4x4 kernel on a 64-bit build:

packed_group_weights_size =
  ((16 * 2^30 << 2) + 4) * round_up(2^28, 16)
  = (2^36 + 4) * 2^28
  = 2^64 + 2^30
  -> wraps to 2^30 = 1 GiB allocation

The pack kernel then writes the full logical kernel size (2^64 bytes).
Under AddressSanitizer the create call reports the 1 GiB allocation and then
crashes in xnn_pack_f32_conv_goki_w (src/reference/packing.cc:3995). On
32-bit targets (Android ARM32, WASM) group_input_channels = group_output_channels = 32768 is enough to wrap.

Fix

Guard every multiply and add in the size chain with
xnn_safe_mul / xnn_safe_add, and reject the operator with
xnn_status_unsupported_parameter when any step overflows. This mirrors the
guard already present for Fully Connected (src/operators/fully-connected-nc.c,
commit 3131f0d). The f16/pf16 variants already guard the full product; the
f32 paths did not.

In the convolution path the guard is evaluated before the weights-cache
look-up so that an overflow failure cannot leave
packed_weights.offset set while the packed_weights union's pointer
member is still unset.

Testing

  • convolution-nhwc-test: 797 passed
  • deconvolution-nhwc-test: 1079 passed
  • subgraph convolution-2d-test: 11 passed
  • subgraph deconvolution-2d-test: 7 passed

Both oversized inputs now fail cleanly with
packed weights size overflows size_t instead of corrupting the heap.

packed_group_weights_size is computed from the caller-supplied
group_input_channels / group_output_channels without overflow checks:

  ((kernel_size * k_stride << log2_filter_element_size) +
   bias_element_size + extra_weights_bytes) * n_stride

Only kernel_height * kernel_width is guarded via xnn_safe_mul. When the
product wraps size_t the resulting aligned_total_weights_size is far
smaller than the data the weight-packing microkernel writes into it,
producing a heap out-of-bounds write (e.g. group_input_channels = 2^30,
group_output_channels = 2^28, 4x4 kernel on 64-bit wraps to a 1 GiB
allocation while the pack kernel writes 2^64 bytes; on 32-bit 32768
channels per group is sufficient).

Guard every multiply/add in the size chain with xnn_safe_mul/xnn_safe_add
and reject the operator with xnn_status_unsupported_parameter, mirroring
the guard already present for Fully Connected (commit 3131f0d). The
f16/pf16 variants already guard the full product; the f32 paths did not.

The convolution guard is computed before the weights-cache look-up so a
failure does not leave packed_weights.offset set while the packed_weights
union's pointer member is still unset.
destro4evr-rgb added a commit to destro4evr-rgb/XNNPACK that referenced this pull request Sep 18, 2026
…lcaddc_path

Both paths in src/operators/convolution-nhwc.c computed packed_weights_size
using raw C multiplication of per_channel_bytes * c_stride with no overflow
check. On 32-bit targets (WebAssembly, ARM32) a model-supplied groups value
near 107M causes the product to wrap, resulting in an undersized allocation.
The subsequent pack_dwconv_hwg_w / pack_vmulcaddc_w call iterates over the
real groups count and writes past the end of the buffer.

Replace the raw multiplications with xnn_safe_mul / xnn_safe_add, consistent
with the fix applied to create_igemm and create_deconvolution2d_nhwc in
PR google#11184.
copybara-service Bot pushed a commit that referenced this pull request Sep 22, 2026
…-nchw.c

The sparse-weights path in xnn_create_convolution2d_nchw (the block-sparse
"spmm" packing branch, guarded by kernel_height == 1, kernel_width == 1,
groups == 1) computed packed_weights_size using raw C multiplication and
addition, with no overflow check:

  const size_t packed_weights_size =
    num_nonzero_blocks * 2 * sizeof(int32_t) +
    num_output_channel_blocks * sizeof(uint32_t) +
    ((context->group_output_channels + num_nonzero_values) << log2_filter_element_size) +
    XNN_EXTRA_BYTES;

num_nonzero_blocks, num_output_channel_blocks, and num_nonzero_values are
all derived from xnn_analyze_spmm's count of non-zero values in the
model's own weight tensor (spmm_packing_params.num_nonzeroes and
friends), combined with context->group_output_channels -- both
model-supplied. On a 32-bit target (WebAssembly, ARM32), a model whose
sparse weight tensor yields large enough counts for this expression
causes it to wrap size_t, producing an undersized allocation via
xnn_allocate_simd_memory. xnn_pack_spmm, called immediately after, then
writes based on the real, un-wrapped num_nonzero_blocks and
num_output_channel_blocks (it iterates over the same sparsity structure
that produced these counts), writing past the end of the undersized
buffer.

This is the same bug shape already fixed for the dense paths in this
codebase: create_igemm and create_deconvolution2d_nhwc (PR #11184), and
create_dwconv_path / create_vmulcaddc_path in convolution-nhwc.c (PR
#11240, "a model-supplied groups value near 107M causes the product to
wrap ... writes past the end of the buffer"). Found by sweeping this
same file and convolution-nhwc.c for other packed_weights_size
computations after reviewing #11240: this file already has a properly
guarded packed_weights_size calculation in its dense
(conv2d_hwc2chw) path (chained xnn_safe_mul calls on
context->group_input_channels / kernel_height / kernel_width /
context->groups / packed_group_output_channels), confirming the
guarding convention is established here -- it just was not extended to
this sparse path.

I also traced a second candidate this same sweep surfaced,
resize-bilinear-nhwc.c's xnn_setup_resize_bilinear2d_nhwc, and confirmed
it is not concretely exploitable: its packed_weights_size recomputation
reads output_height/output_width from
resize_op->context.resize_nhwc_indirection_init, which is populated in
exactly one place in that file -- inside
xnn_reshape_resize_bilinear2d_nhwc, immediately after that function's
own xnn_safe_mul-guarded computation of the same product already
succeeded on the same values. So unlike this one, that recomputation
can never see values its own file's overflow check has not already
validated. No fix needed there.

Fix: replace the raw expression with chained xnn_safe_mul / xnn_safe_add
calls (matching the style already used a few functions away in this
same file, and in the two prior PRs above), returning
xnn_status_invalid_parameter with a logged error on overflow instead of
proceeding with an undersized allocation.

Verified:
- Reproduced the overflow dynamically: extracted the exact pre-fix
  expression into a standalone harness using uint32_t to model size_t
  on a 32-bit target. With num_nonzero_blocks =
  num_output_channel_blocks = num_nonzero_values = 300000000 (a
  plausible crafted-sparse-model value) and group_output_channels = 4,
  the intended size is ~4.8 GB but the wrapped size_t computation
  produces 505032736 -- undersized by exactly 2^32 bytes (a full
  uint32_t wrap).
- Verified the fix closes it: transcribed the exact patch logic
  (identical chain of xnn_safe_mul/xnn_safe_add calls) into the same
  harness, using the real xnn_safe_mul/xnn_safe_add implementations
  from src/xnnpack/math.h (__builtin_mul_overflow /
  __builtin_add_overflow under the hood). The same malicious input is
  correctly rejected; a normal, legitimate sparse-weights input (e.g.
  1000 nonzero blocks, 64 output channel blocks) is correctly accepted
  and produces the expected byte count.
- Syntax-verified the inserted block in isolation against the real
  src/xnnpack/math.h header (correct xnn_safe_mul/xnn_safe_add
  signatures, no typos, balanced braces).
- Could not complete a full project build in this environment: the
  file transitively includes pthreadpool.h, an external dependency
  fetched by XNNPACK's Bazel/CMake build that was not available
  standalone in this sandbox. The verification above exercises the
  exact inserted logic against the project's real overflow-checking
  primitives rather than a reimplementation of them. Please confirm a
  full build + the existing convolution-nchw sparse-path tests in CI.
melkap01-Arm pushed a commit to melkap01-Arm/XNNPACK that referenced this pull request Oct 6, 2026
…lcaddc_path

Both paths in src/operators/convolution-nhwc.c computed packed_weights_size
using raw C multiplication of per_channel_bytes * c_stride with no overflow
check. On 32-bit targets (WebAssembly, ARM32) a model-supplied groups value
near 107M causes the product to wrap, resulting in an undersized allocation.
The subsequent pack_dwconv_hwg_w / pack_vmulcaddc_w call iterates over the
real groups count and writes past the end of the buffer.

Replace the raw multiplications with xnn_safe_mul / xnn_safe_add, consistent
with the fix applied to create_igemm and create_deconvolution2d_nhwc in
PR google#11184.
melkap01-Arm pushed a commit to melkap01-Arm/XNNPACK that referenced this pull request Oct 6, 2026
…-nchw.c

The sparse-weights path in xnn_create_convolution2d_nchw (the block-sparse
"spmm" packing branch, guarded by kernel_height == 1, kernel_width == 1,
groups == 1) computed packed_weights_size using raw C multiplication and
addition, with no overflow check:

  const size_t packed_weights_size =
    num_nonzero_blocks * 2 * sizeof(int32_t) +
    num_output_channel_blocks * sizeof(uint32_t) +
    ((context->group_output_channels + num_nonzero_values) << log2_filter_element_size) +
    XNN_EXTRA_BYTES;

num_nonzero_blocks, num_output_channel_blocks, and num_nonzero_values are
all derived from xnn_analyze_spmm's count of non-zero values in the
model's own weight tensor (spmm_packing_params.num_nonzeroes and
friends), combined with context->group_output_channels -- both
model-supplied. On a 32-bit target (WebAssembly, ARM32), a model whose
sparse weight tensor yields large enough counts for this expression
causes it to wrap size_t, producing an undersized allocation via
xnn_allocate_simd_memory. xnn_pack_spmm, called immediately after, then
writes based on the real, un-wrapped num_nonzero_blocks and
num_output_channel_blocks (it iterates over the same sparsity structure
that produced these counts), writing past the end of the undersized
buffer.

This is the same bug shape already fixed for the dense paths in this
codebase: create_igemm and create_deconvolution2d_nhwc (PR google#11184), and
create_dwconv_path / create_vmulcaddc_path in convolution-nhwc.c (PR
google#11240, "a model-supplied groups value near 107M causes the product to
wrap ... writes past the end of the buffer"). Found by sweeping this
same file and convolution-nhwc.c for other packed_weights_size
computations after reviewing google#11240: this file already has a properly
guarded packed_weights_size calculation in its dense
(conv2d_hwc2chw) path (chained xnn_safe_mul calls on
context->group_input_channels / kernel_height / kernel_width /
context->groups / packed_group_output_channels), confirming the
guarding convention is established here -- it just was not extended to
this sparse path.

I also traced a second candidate this same sweep surfaced,
resize-bilinear-nhwc.c's xnn_setup_resize_bilinear2d_nhwc, and confirmed
it is not concretely exploitable: its packed_weights_size recomputation
reads output_height/output_width from
resize_op->context.resize_nhwc_indirection_init, which is populated in
exactly one place in that file -- inside
xnn_reshape_resize_bilinear2d_nhwc, immediately after that function's
own xnn_safe_mul-guarded computation of the same product already
succeeded on the same values. So unlike this one, that recomputation
can never see values its own file's overflow check has not already
validated. No fix needed there.

Fix: replace the raw expression with chained xnn_safe_mul / xnn_safe_add
calls (matching the style already used a few functions away in this
same file, and in the two prior PRs above), returning
xnn_status_invalid_parameter with a logged error on overflow instead of
proceeding with an undersized allocation.

Verified:
- Reproduced the overflow dynamically: extracted the exact pre-fix
  expression into a standalone harness using uint32_t to model size_t
  on a 32-bit target. With num_nonzero_blocks =
  num_output_channel_blocks = num_nonzero_values = 300000000 (a
  plausible crafted-sparse-model value) and group_output_channels = 4,
  the intended size is ~4.8 GB but the wrapped size_t computation
  produces 505032736 -- undersized by exactly 2^32 bytes (a full
  uint32_t wrap).
- Verified the fix closes it: transcribed the exact patch logic
  (identical chain of xnn_safe_mul/xnn_safe_add calls) into the same
  harness, using the real xnn_safe_mul/xnn_safe_add implementations
  from src/xnnpack/math.h (__builtin_mul_overflow /
  __builtin_add_overflow under the hood). The same malicious input is
  correctly rejected; a normal, legitimate sparse-weights input (e.g.
  1000 nonzero blocks, 64 output channel blocks) is correctly accepted
  and produces the expected byte count.
- Syntax-verified the inserted block in isolation against the real
  src/xnnpack/math.h header (correct xnn_safe_mul/xnn_safe_add
  signatures, no typos, balanced braces).
- Could not complete a full project build in this environment: the
  file transitively includes pthreadpool.h, an external dependency
  fetched by XNNPACK's Bazel/CMake build that was not available
  standalone in this sandbox. The verification above exercises the
  exact inserted logic against the project's real overflow-checking
  primitives rather than a reimplementation of them. Please confirm a
  full build + the existing convolution-nchw sparse-path tests in CI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants