Skip to content

Commit ae8b60a

Browse files
GregoryComerfacebook-github-bot
authored andcommitted
Prevent signed integer overflow in pixel_shuffle size calculation
Summary: The size calculation in `get_pixel_shuffle_out_target_size` can trigger signed integer overflow (UB) with very large upscale_factors. This manifested as an integer division by zero fault during fuzzing. Specifically, the calculation of `upscale_factor * upscale_factor` ([source](https://github.com/pytorch/executorch/blob/04f1e4d22383ffcbc770acf5002348e3f95082a2/kernels/portable/cpu/util/copy_ops_util.cpp#L364)) can overflow. In the motivating case, SizesType is a signed 32-bit integer and upscale_factor = 2^17. Since this is an impractically large upscale factor, I'm just adding a constraint that upscale_factor < 32768 (2^15). In theory, SizesType could be defined as less than 32 bits, but this seems unlikely in practice. I check this upper bound in `check_pixel_shuffle_args` and also assert in `get_pixel_shuffle_out_target_size` to ensure we don't hit UB. Differential Revision: D88693324
1 parent 04f1e4d commit ae8b60a

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

kernels/portable/cpu/util/copy_ops_util.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ bool check_pixel_shuffle_args(
330330
ET_LOG_AND_RETURN_IF_FALSE(upscale_factor > 0);
331331
ET_LOG_AND_RETURN_IF_FALSE(
332332
in.size(in.dim() - 3) % (upscale_factor * upscale_factor) == 0);
333+
ET_LOG_AND_RETURN_IF_FALSE(
334+
upscale_factor < 32768); // Prevent overflow when computing upscale_factor ^ 2.
333335
return true;
334336
}
335337

@@ -351,6 +353,10 @@ void get_pixel_shuffle_out_target_size(
351353
int64_t upscale_factor,
352354
executorch::aten::SizesType* out_sizes,
353355
size_t* out_ndim) {
356+
// Prevent signed integer overflow when computing upscale_factor ^ 2.
357+
// This constraint is checked in check_pixel_shuffle_args.
358+
ET_CHECK(upscale_factor < 32768);
359+
354360
*out_ndim = in.dim();
355361
const executorch::aten::SizesType casted_upscale_factor = upscale_factor;
356362

0 commit comments

Comments
 (0)