Skip to content

Commit

Permalink
Fix tf.raw_ops.ResizeNearestNeighborGrad vulnerability with large dim…
Browse files Browse the repository at this point in the history
…ensions.

Note: This fix will have to be cherry picked in r2.10, r2.9, and r2.8.
PiperOrigin-RevId: 479141644
  • Loading branch information
poulsbo authored and tensorflower-gardener committed Oct 5, 2022
1 parent 4017ac5 commit 00c821a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 5 additions & 5 deletions tensorflow/core/kernels/image/resize_nearest_neighbor_op.cc
Expand Up @@ -257,11 +257,11 @@ class ResizeNearestNeighborOpGrad : public OpKernel {
const int64_t out_width = sizes(1);

Tensor* output = nullptr;
OP_REQUIRES_OK(
context,
context->allocate_output(
0, TensorShape({batch_size, out_height, out_width, channels}),
&output));
TensorShape shape;
OP_REQUIRES_OK(context,
TensorShape::BuildTensorShape(
{batch_size, out_height, out_width, channels}, &shape));
OP_REQUIRES_OK(context, context->allocate_output(0, shape, &output));

// Return if the output is empty.
if (output->NumElements() == 0) return;
Expand Down
19 changes: 19 additions & 0 deletions tensorflow/python/ops/image_ops_test.py
Expand Up @@ -4175,6 +4175,25 @@ def testPad(self):
self._assertReturns(x, x_shape, y, y_shape)


class ResizeNearestNeighborGrad(test_util.TensorFlowTestCase):

def testSizeTooLarge(self):
align_corners = True
half_pixel_centers = False
grads = constant_op.constant(1, shape=[1, 8, 16, 3], dtype=dtypes.float16)
size = constant_op.constant([1879048192, 1879048192],
shape=[2],
dtype=dtypes.int32)
with self.assertRaisesRegex(errors.InvalidArgumentError,
r"Encountered overflow when multiplying"):
self.evaluate(
gen_image_ops.ResizeNearestNeighborGrad(
grads=grads,
size=size,
align_corners=align_corners,
half_pixel_centers=half_pixel_centers))


class ResizeImageWithCropOrPadTest(test_util.TensorFlowTestCase):

def _ResizeImageWithCropOrPad(self, x, target_height, target_width,
Expand Down

0 comments on commit 00c821a

Please sign in to comment.