Skip to content

Commit

Permalink
Fix security vulnerability with FractionalAvgPoolGrad
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 462292194
  • Loading branch information
tensorflower-gardener committed Jul 21, 2022
1 parent d631c03 commit 03a659d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
30 changes: 28 additions & 2 deletions tensorflow/core/kernels/fractional_avg_pool_op.cc
Expand Up @@ -12,22 +12,23 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#define EIGEN_USE_THREADS

#include <algorithm>
#include <cmath>
#include <random>
#include <vector>

#include "tensorflow/core/kernels/fractional_pool_common.h"

#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/core/framework/numeric_op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/kernels/fractional_pool_common.h"
#include "tensorflow/core/lib/random/random.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/core/util/guarded_philox_random.h"
#include "tensorflow/core/util/overflow.h"

namespace tensorflow {
typedef Eigen::ThreadPoolDevice CPUDevice;
Expand Down Expand Up @@ -241,7 +242,32 @@ class FractionalAvgPoolGradOp : public OpKernel {
orig_input_tensor_shape.NumElements() == 4,
errors::InvalidArgument("original input tensor shape must be"
"1-dimensional and 4 elements"));
int64_t num_elements = 1;
for (int i = 0; i < orig_input_tensor_shape.dims(); i++) {
OP_REQUIRES(context, orig_input_tensor_shape.dim_size(i) > 0,
errors::InvalidArgument(
"orig_input_tensor_shape must be positive, got: ",
orig_input_tensor_shape.dim_size(i)));
num_elements = MultiplyWithoutOverflow(
num_elements, orig_input_tensor_shape.dim_size(i));
OP_REQUIRES(
context, num_elements > 0,
errors::InvalidArgument(
"The total elements specified by orig_input_tensor_shape",
" is too large. Encountered overflow after multiplying ",
orig_input_tensor_shape.dim_size(i), ", result: ", num_elements));
}

const Tensor& out_backprop = context->input(1);
OP_REQUIRES(context, out_backprop.dims() == 4,
errors::InvalidArgument("out_backprop must be 4-dimensional"));
for (int i = 0; i < out_backprop.dims(); i++) {
OP_REQUIRES(context, out_backprop.dim_size(i) > 0,
errors::InvalidArgument(
"out_backprop must be positive for all dimension, got:",
out_backprop.dim_size(i)));
}

const Tensor& row_seq_tensor = context->input(2);
const Tensor& col_seq_tensor = context->input(3);

Expand Down
Expand Up @@ -541,6 +541,27 @@ def testLargePoolingRatioThroughGradientError(self):
delta=1e-2)
self.assertLess(gradient_error, error_margin)

def testInvalidSeqRaiseErrorForFractionalAvgPoolGrad(self):
with self.assertRaises((errors.InvalidArgumentError, ValueError)):
with self.cached_session() as _:
overlapping = True
orig_input_tensor_shape = constant_op.constant(
-1879048192, shape=[4], dtype=dtypes.int64)
out_backprop = constant_op.constant([],
shape=[0, 0, 0, 0],
dtype=dtypes.float64)
row_pooling_sequence = constant_op.constant(
1, shape=[4], dtype=dtypes.int64)
col_pooling_sequence = constant_op.constant(
1, shape=[4], dtype=dtypes.int64)
t = gen_nn_ops.fractional_avg_pool_grad(
orig_input_tensor_shape=orig_input_tensor_shape,
out_backprop=out_backprop,
row_pooling_sequence=row_pooling_sequence,
col_pooling_sequence=col_pooling_sequence,
overlapping=overlapping)
self.evaluate(t)


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

0 comments on commit 03a659d

Please sign in to comment.