Skip to content

Commit 638b189

Browse files
author
Github Executorch
committed
et check
1 parent cdabb14 commit 638b189

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

runtime/core/portable_type/tensor_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ ssize_t compute_numel(const TensorImpl::SizesType* sizes, ssize_t dim) {
4040
static_cast<ssize_t>(sizes[i]),
4141
i);
4242
ET_CHECK_MSG(
43-
sizes[i] == 0 || numel <= SSIZE_MAX / sizes[i],
43+
sizes[i] == 0 || numel <= (ssize_t)(SIZE_MAX / 2) / sizes[i],
4444
"Overflow computing numel: %zd * %zd would overflow ssize_t at dimension %zd",
4545
numel,
4646
static_cast<ssize_t>(sizes[i]),

runtime/executor/tensor_parser_portable.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <executorch/runtime/executor/tensor_parser.h>
1010

11+
#include <climits>
12+
1113
#include <executorch/runtime/core/exec_aten/exec_aten.h>
1214
#include <executorch/runtime/core/exec_aten/util/dim_order_util.h>
1315
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
@@ -118,17 +120,24 @@ Result<Tensor> parseTensor(
118120
dim_order =
119121
const_cast<executorch::aten::DimOrderType*>(serialized_dim_order);
120122
}
121-
// Validate sizes before using them in case the PTE data is bad. We can't
122-
// detect bad positive values, but we can reject negative values, which would
123-
// otherwise panic in the TensorImpl ctor. dim_order_to_stride() will validate
124-
// dim_order.
123+
// Validate sizes before using them in case the PTE data is bad. Reject
124+
// negative values and check that the product of all dimensions doesn't
125+
// overflow ssize_t, which would otherwise abort in the TensorImpl ctor.
126+
// dim_order_to_stride() will validate dim_order.
127+
ssize_t numel = 1;
125128
for (flatbuffers::uoffset_t i = 0; i < dim; i++) {
126129
ET_CHECK_OR_RETURN_ERROR(
127130
sizes[i] >= 0,
128131
InvalidProgram,
129132
"Negative size[%zu] %" PRId32,
130133
static_cast<size_t>(i),
131134
sizes[i]);
135+
ET_CHECK_OR_RETURN_ERROR(
136+
sizes[i] == 0 || numel <= (ssize_t)(SIZE_MAX / 2) / sizes[i],
137+
InvalidProgram,
138+
"Overflow computing numel at dim %zu",
139+
static_cast<size_t>(i));
140+
numel *= sizes[i];
132141
}
133142

134143
// We will remove strides from schema.

0 commit comments

Comments
 (0)