From ba70cc499eddfbf874afe9a656dc265703ffc5f1 Mon Sep 17 00:00:00 2001 From: liuwei1031 <46661762+liuwei1031@users.noreply.github.com> Date: Tue, 21 May 2019 11:46:59 +0800 Subject: [PATCH] fix security bugs : (#17464) http://newicafe.baidu.com:80/issue/PaddleSec-33/show?from=page http://newicafe.baidu.com:80/issue/PaddleSec-28/show?from=page http://newicafe.baidu.com:80/issue/PaddleSec-25/show?from=page http://newicafe.baidu.com:80/issue/PaddleSec-24/show?from=page http://newicafe.baidu.com:80/issue/PaddleSec-21/show?from=page http://newicafe.baidu.com:80/issue/PaddleSec-20/show?from=page test=develop --- paddle/fluid/inference/api/helper.h | 44 +++++++++++++++++-- paddle/fluid/operators/im2sequence_op.cc | 2 +- paddle/fluid/operators/im2sequence_op.h | 7 +-- paddle/fluid/operators/math/context_project.h | 12 +++-- paddle/fluid/operators/reader/ctr_reader.cc | 16 +++---- paddle/fluid/operators/unpool_op.h | 8 ++-- 6 files changed, 66 insertions(+), 23 deletions(-) diff --git a/paddle/fluid/inference/api/helper.h b/paddle/fluid/inference/api/helper.h index ab7f5533748..38f754b92d4 100644 --- a/paddle/fluid/inference/api/helper.h +++ b/paddle/fluid/inference/api/helper.h @@ -21,6 +21,7 @@ #endif #include #include // NOLINT +#include #include #include #include @@ -79,26 +80,63 @@ static void split(const std::string &str, char sep, pieces->push_back(str.substr(pos)); } } + +template +static T convert(const std::string &item, + std::function func) { + T res; + try { + res = func(item); + } catch (std::invalid_argument &e) { + std::string message = + "invalid_argument exception when try to convert : " + item; + LOG(ERROR) << message; + PADDLE_THROW(message); + } catch (std::out_of_range &e) { + std::string message = + "out_of_range exception when try to convert : " + item; + LOG(ERROR) << message; + PADDLE_THROW(message); + } catch (...) { + std::string message = "unexpected exception when try to convert " + item; + LOG(ERROR) << message; + PADDLE_THROW(message); + } + return res; +} + static void split_to_float(const std::string &str, char sep, std::vector *fs) { std::vector pieces; split(str, sep, &pieces); std::transform(pieces.begin(), pieces.end(), std::back_inserter(*fs), - [](const std::string &v) { return std::stof(v); }); + [](const std::string &v) { + return convert(v, [](const std::string &item) { + return std::stof(item); + }); + }); } static void split_to_int64(const std::string &str, char sep, std::vector *is) { std::vector pieces; split(str, sep, &pieces); std::transform(pieces.begin(), pieces.end(), std::back_inserter(*is), - [](const std::string &v) { return std::stoi(v); }); + [](const std::string &v) { + return convert(v, [](const std::string &item) { + return std::stoll(item); + }); + }); } static void split_to_int(const std::string &str, char sep, std::vector *is) { std::vector pieces; split(str, sep, &pieces); std::transform(pieces.begin(), pieces.end(), std::back_inserter(*is), - [](const std::string &v) { return std::stoi(v); }); + [](const std::string &v) { + return convert(v, [](const std::string &item) { + return std::stoi(item); + }); + }); } template std::string to_string(const std::vector &vec) { diff --git a/paddle/fluid/operators/im2sequence_op.cc b/paddle/fluid/operators/im2sequence_op.cc index 44fd95edef2..0fa7322fbd6 100644 --- a/paddle/fluid/operators/im2sequence_op.cc +++ b/paddle/fluid/operators/im2sequence_op.cc @@ -34,7 +34,7 @@ class Im2SequenceOp : public framework::OperatorWithKernel { PADDLE_ENFORCE_EQ(in_dim.size(), 4, "Input(X) format must be 4D tensor, eg., NCHW."); - int img_channels = in_dim[1]; + auto img_channels = in_dim[1]; auto kernels = ctx->Attrs().Get>("kernels"); auto strides = ctx->Attrs().Get>("strides"); diff --git a/paddle/fluid/operators/im2sequence_op.h b/paddle/fluid/operators/im2sequence_op.h index 4a994281941..9c9069b7227 100644 --- a/paddle/fluid/operators/im2sequence_op.h +++ b/paddle/fluid/operators/im2sequence_op.h @@ -113,9 +113,10 @@ class Im2SequenceKernel : public framework::OpKernel { paddings[2], strides[0]); int output_width = Im2SeqOutputSize(img_width, kernels[1], paddings[1], paddings[3], strides[1]); - out->mutable_data({batch_size * output_height * output_width, - img_channels * kernels[0] * kernels[1]}, - ctx.GetPlace()); + out->mutable_data( + {static_cast(batch_size) * output_height * output_width, + static_cast(img_channels) * kernels[0] * kernels[1]}, + ctx.GetPlace()); const std::vector dilations({1, 1}); auto out_dims = out->dims(); out->Resize({batch_size, out->numel() / batch_size}); diff --git a/paddle/fluid/operators/math/context_project.h b/paddle/fluid/operators/math/context_project.h index f6094369567..e9019c6d2fe 100644 --- a/paddle/fluid/operators/math/context_project.h +++ b/paddle/fluid/operators/math/context_project.h @@ -144,7 +144,8 @@ class ContextProjectFunctor { sequence_height = static_cast(out_t.dims()[0]); // add up trainable data - out_t.Resize({sequence_height * context_length, sequence_width}); + out_t.Resize({static_cast(sequence_height) * context_length, + sequence_width}); if (up_pad > 0) { // add up pad int padding_rows = std::min( @@ -191,7 +192,8 @@ class ContextProjectFunctor { &out_t_sub); } } - out_t.Resize({sequence_height, context_length * sequence_width}); + out_t.Resize({sequence_height, + static_cast(context_length) * sequence_width}); } } } @@ -260,7 +262,8 @@ class ContextProjectGradFunctor { static_cast(lod_level_0[i + 1])); sequence_height = static_cast(out_t.dims()[0]); - out_t.Resize({sequence_height * context_length, sequence_width}); + out_t.Resize({static_cast(sequence_height) * context_length, + sequence_width}); if (up_pad > 0) { int padding_rows = std::min( @@ -308,7 +311,8 @@ class ContextProjectGradFunctor { w_sub.data()); } } - out_t.Resize({sequence_height, context_length * sequence_width}); + out_t.Resize({sequence_height, + static_cast(context_length) * sequence_width}); } } } diff --git a/paddle/fluid/operators/reader/ctr_reader.cc b/paddle/fluid/operators/reader/ctr_reader.cc index 43a49de5224..10ad67ac877 100644 --- a/paddle/fluid/operators/reader/ctr_reader.cc +++ b/paddle/fluid/operators/reader/ctr_reader.cc @@ -32,17 +32,17 @@ namespace reader { static inline void string_split(const std::string& s, const char delimiter, std::vector* output) { - size_t start = 0; - size_t end = s.find_first_of(delimiter); + if (s.empty()) return; - while (end <= std::string::npos) { - output->emplace_back(s.substr(start, end - start)); - if (end == std::string::npos) { - break; - } + size_t start = 0; + size_t end = s.find(delimiter); + while (end != std::string::npos) { + if (end > start) output->emplace_back(s.substr(start, end - start)); start = end + 1; - end = s.find_first_of(delimiter, start); + end = s.find(delimiter, start); } + auto term = s.substr(start); + if (!term.empty()) output->emplace_back(term); } static inline void parse_line( diff --git a/paddle/fluid/operators/unpool_op.h b/paddle/fluid/operators/unpool_op.h index 96abad3de9b..e388ec5ae39 100644 --- a/paddle/fluid/operators/unpool_op.h +++ b/paddle/fluid/operators/unpool_op.h @@ -61,10 +61,10 @@ class UnpoolGradKernel : public framework::OpKernel { auto& device_ctx = context.template device_context(); math::SetConstant zero; - if (in_x_grad) { - in_x_grad->mutable_data(context.GetPlace()); - zero(device_ctx, in_x_grad, static_cast(0)); - } + + in_x_grad->mutable_data(context.GetPlace()); + zero(device_ctx, in_x_grad, static_cast(0)); + math::Unpool2dMaxGradFunctor unpool2d_max_backward; unpool2d_max_backward(device_ctx, *in_x, *in_y, *out, *out_grad, in_x_grad); } -- GitLab