Skip to content
Permalink
Browse files Browse the repository at this point in the history
Fix tf.raw_ops.SetSize vulnerability with invalid input arg specifyin…
…g shape.

Check that given input is a 1D tensor, as required.

PiperOrigin-RevId: 460740463
  • Loading branch information
poulsbo authored and tensorflower-gardener committed Jul 13, 2022
1 parent 5a7be37 commit cf70b79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions tensorflow/core/kernels/set_kernels.cc
Expand Up @@ -70,8 +70,12 @@ Status SparseTensorFromContext(OpKernelContext* ctx, const int32_t base_index,
sparse::SparseTensor* tensor) {
// Assume row-major order.
TensorShape shape;
TF_RETURN_IF_ERROR(TensorShape::BuildTensorShape(
ctx->input(base_index + 2).vec<int64_t>(), &shape));
const Tensor& shape_tensor = ctx->input(base_index + 2);
if (shape_tensor.dims() != 1) {
return errors::InvalidArgument("Shape must be a 1D tensor.");
}
TF_RETURN_IF_ERROR(
TensorShape::BuildTensorShape(shape_tensor.vec<int64_t>(), &shape));
CheckRankAtLeast2(ctx, shape);
std::vector<int64_t> order(shape.dims());
std::iota(order.begin(), order.end(), 0);
Expand Down
13 changes: 13 additions & 0 deletions tensorflow/python/kernel_tests/math_ops/sets_test.py
Expand Up @@ -23,6 +23,7 @@
from tensorflow.python.framework import sparse_tensor as sparse_tensor_lib
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_set_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import sets
from tensorflow.python.ops import sparse_ops
Expand Down Expand Up @@ -1303,6 +1304,18 @@ def test_set_union_output_is_sorted(self, dtype):
result.values,
_constant([1, 3, 5, 7, 9, 0, 2, 4, 5, 6, 6, 8, 9], dtype))

def test_raw_ops_setsize_invalid_shape(self):
with self.assertRaisesRegex(errors_impl.InvalidArgumentError,
"Shape must be a 1D tensor"):
invalid_shape = 1
self.evaluate(
gen_set_ops.set_size(
set_indices=1,
set_values=[1, 1],
set_shape=invalid_shape,
validate_indices=True,
name=""))


if __name__ == "__main__":
googletest.main()

0 comments on commit cf70b79

Please sign in to comment.