Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CherryPick:2.5] TF Security #49038

Merged
merged 12 commits into from
May 10, 2021
Merged
3 changes: 3 additions & 0 deletions tensorflow/core/kernels/fused_batch_norm_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ struct FusedBatchNorm<CPUDevice, T, U, /* is_training= */ false> {
const CPUDevice& d = context->eigen_device<CPUDevice>();

const int depth = x.dimension(3);
OP_REQUIRES(
context, depth != 0,
errors::Internal("The 4th element in the input shape cannot be 0."));
const int size = x.size();
const int rest_size = size / depth;
Eigen::DSizes<Eigen::Index, 2> rest_by_depth(rest_size, depth);
Expand Down
8 changes: 8 additions & 0 deletions tensorflow/core/kernels/quantize_and_dequantize_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/framework/op_requires.h"
#define EIGEN_USE_THREADS

#if (defined(GOOGLE_CUDA) && GOOGLE_CUDA) || \
Expand Down Expand Up @@ -71,6 +72,9 @@ class QuantizeAndDequantizeV2Op : public OpKernel {

void Compute(OpKernelContext* ctx) override {
const Tensor& input = ctx->input(0);
OP_REQUIRES(
ctx, axis_ >= -1,
errors::InvalidArgument("Axis must be at least -1. Found ", axis_));
OP_REQUIRES(
ctx, (axis_ == -1 || axis_ < input.shape().dims()),
errors::InvalidArgument("Shape must be at least rank ", axis_ + 1,
Expand Down Expand Up @@ -234,6 +238,10 @@ class QuantizeAndDequantizeV3Op : public OpKernel {

void Compute(OpKernelContext* ctx) override {
const Tensor& input = ctx->input(0);
OP_REQUIRES(ctx, axis_ < input.dims(),
errors::InvalidArgument(
"Axis requested is larger than input dimensions. Axis: ",
axis_, " Input Dimensions: ", input.dims()));
const int depth = (axis_ == -1) ? 1 : input.dim_size(axis_);
Tensor* output = nullptr;
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, input.shape(), &output));
Expand Down
6 changes: 6 additions & 0 deletions tensorflow/core/kernels/ragged_tensor_to_tensor_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ class RaggedTensorToTensorBaseOp : public OpKernel {
output_index_multiplier, output_size, result);
return tensorflow::Status::OK();
case RowPartitionType::ROW_SPLITS:
if (row_partition_tensor.size() - 1 > parent_output_index.size()) {
return errors::InvalidArgument(
"Row partition size is greater than output size: ",
row_partition_tensor.size() - 1, " > ",
parent_output_index.size());
}
CalculateOutputIndexRowSplit(
context, row_partition_tensor, parent_output_index,
output_index_multiplier, output_size, result);
Expand Down
6 changes: 6 additions & 0 deletions tensorflow/core/kernels/reverse_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ class ReverseOp : public OpKernel {

void Compute(OpKernelContext* context) override {
const Tensor& input = context->input(0);
// If input is provided, check to make sure the first dimension is valid.
if (input.dims() > 0) {
OP_REQUIRES(
context, input.dim_size(0) != 0,
errors::InvalidArgument("Invalid input first dimension. Found 0."));
}
const Tensor& dims = context->input(1);

if (TensorShapeUtils::IsScalar(input.shape())) {
Expand Down
5 changes: 5 additions & 0 deletions tensorflow/core/kernels/sparse_add_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
==============================================================================*/

#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/op_requires.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_util.h"
Expand Down Expand Up @@ -101,6 +102,10 @@ class SparseAddOp : public OpKernel {
std::vector<T> out_values;
const int num_dims = a_shape->dim_size(0);

OP_REQUIRES(ctx, num_dims > 0,
errors::InvalidArgument("Invalid input_a shape. Received: ",
a_shape->DebugString()));

// The input and output sparse tensors are assumed to be ordered along
// increasing dimension number.
int64 i = 0, j = 0;
Expand Down
4 changes: 4 additions & 0 deletions tensorflow/core/kernels/sparse_matmul_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,10 @@ class SparseMatMulOp : public OpKernel {
if (transpose_b) {
// TODO(agarwal): avoid transposing the matrix here and directly handle
// transpose in CreateDenseSlices.
OP_REQUIRES(ctx, right->dim_size(0) != 0,
errors::InvalidArgument("b has an entry 0 in it's shape."));
OP_REQUIRES(ctx, right->dim_size(1) != 0,
errors::InvalidArgument("b has an entry 0 in it's shape."));
right_tr.reset(
new Tensor(right->dtype(),
TensorShape({right->dim_size(1), right->dim_size(0)})));
Expand Down
12 changes: 12 additions & 0 deletions tensorflow/core/kernels/sparse_reshape_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ limitations under the License.
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/kernels/reshape_util.h"
#include "tensorflow/core/lib/gtl/inlined_vector.h"
#include "tensorflow/core/platform/errors.h"

namespace tensorflow {

Expand All @@ -38,6 +39,17 @@ class SparseReshapeOp : public OpKernel {
explicit SparseReshapeOp(OpKernelConstruction* context) : OpKernel(context) {}

void Compute(OpKernelContext* context) override {
const Tensor& input_indices_in = context->input(0);
const Tensor& input_shape_in = context->input(1);

OP_REQUIRES(context, TensorShapeUtils::IsMatrix(input_indices_in.shape()),
errors::InvalidArgument("Input must be a matrix."));
OP_REQUIRES(context, TensorShapeUtils::IsVector(input_shape_in.shape()),
errors::InvalidArgument("Input shape must be a vector."));
OP_REQUIRES(context,
input_indices_in.dim_size(1) == input_shape_in.dim_size(0),
errors::InvalidArgument(
"Input tensor rank must match input shape length."));
ReshapeSparseTensor<Device>(context, context->input(0), context->input(1),
context->input(2), 0 /* output indices index */,
1 /* output shape index */);
Expand Down
5 changes: 5 additions & 0 deletions tensorflow/core/kernels/sparse_sparse_binary_op_shared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ class SparseSparseBinaryOpShared : public OpKernel {
" for dimension ", i));
}

OP_REQUIRES(
ctx, a_indices_t->dim_size(1) == b_indices_t->dim_size(1),
errors::InvalidArgument(
"Indices' dimensions do not match: got ", a_indices_t->dim_size(1),
" and ", b_indices_t->dim_size(1), " for the second dimension."));
const int num_dims = a_indices_t->dim_size(1);
const auto a_indices_mat = a_indices_t->matrix<int64>();
const auto b_indices_mat = b_indices_t->matrix<int64>();
Expand Down
19 changes: 19 additions & 0 deletions tensorflow/core/kernels/unicode_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,17 @@ class UnicodeEncodeOp : public OpKernel {
const Tensor& input_splits = context->input(1);
const auto input_splits_flat = input_splits.flat<SPLITS_TYPE>();

// Operation will treat first argument in input_splits as if it were zero
// regardless of its actual value since splits should begin with zero and
// end with the length of the input values vector.
OP_REQUIRES(
context, input_splits_flat(0) == 0,
errors::InvalidArgument("First value in input_splits must be zero."));
OP_REQUIRES(context,
input_splits_flat(input_splits_flat.size() - 1) ==
input_tensor_flat.size(),
errors::InvalidArgument("Last value in input_splits must be "
"equal to length of input_tensor."));
// Since we limit to a 2-D input (flat_values of rank 1 and a single splits
// tensor), our output dimension will be 1 with it's size equal to the
// number of splits (outer dimension or ragged tensor).
Expand All @@ -548,6 +559,14 @@ class UnicodeEncodeOp : public OpKernel {
for (int i = 1; i < input_splits_flat.size(); ++i) {
icu::UnicodeString unicode_string;
icu::UnicodeStringAppendable appendable_unicode_string(unicode_string);
OP_REQUIRES(
context, input_splits_flat(i - 1) <= input_splits_flat(i),
errors::InvalidArgument(
"Values in input_splits must be equal or in ascending order."));
OP_REQUIRES(
context, input_splits_flat(i) <= input_tensor_flat.size(),
errors::InvalidArgument("Values in input_splits must be less than or "
"equal to input_tensor length."));
for (; idx < input_splits_flat(i); ++idx) {
int32 code_point = input_tensor_flat(idx);
// Check for invalid code point
Expand Down
2 changes: 2 additions & 0 deletions tensorflow/core/kernels/unsorted_segment_join_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class UnsortedSegmentJoinOp : public OpKernel {
const int32 segment_dims = segment_id_shape.dims();

const Tensor& num_segments_tensor = context->input(2);
OP_REQUIRES(context, num_segments_tensor.NumElements() != 0,
errors::InvalidArgument("Number of segments cannot be empty."));
auto num_segments = num_segments_tensor.scalar<NUM_SEGMENTS_TYPE>()();

OP_REQUIRES(context, segment_dims != 0,
Expand Down
4 changes: 4 additions & 0 deletions tensorflow/core/util/sparse/sparse_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,10 @@ inline Status SparseTensor::Split(const SparseTensor& input_tensor,
for (int i = 0; i < input_tensor.indices().dim_size(0); ++i) {
const int dim = input_tensor.indices().matrix<int64>()(i, split_dim);
int slice_index = GetSliceIndex(dim, split_size, residual);
if (slice_index >= num_values.size()) {
return errors::InvalidArgument("Slice index ", slice_index,
" is larger than num_split.");
}
num_values[slice_index]++;
}

Expand Down