-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix integer overflow in compute_numel() #18598
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,11 @@ | |
| #include <executorch/runtime/core/portable_type/tensor_impl.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <climits> | ||
| #include <cstdint> | ||
|
|
||
| #include <c10/util/irange.h> | ||
| #include <c10/util/safe_numerics.h> | ||
|
Comment on lines
11
to
+16
|
||
|
|
||
| #include <executorch/runtime/core/exec_aten/util/dim_order_util.h> | ||
| #include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> | ||
|
|
@@ -38,7 +40,14 @@ ssize_t compute_numel(const TensorImpl::SizesType* sizes, ssize_t dim) { | |
| "Size must be non-negative, got %zd at dimension %zd", | ||
| static_cast<ssize_t>(sizes[i]), | ||
| i); | ||
| numel *= sizes[i]; | ||
| ssize_t next_numel; | ||
| ET_CHECK_MSG( | ||
| !c10::mul_overflows(numel, static_cast<ssize_t>(sizes[i]), &next_numel), | ||
| "Overflow computing numel: %zd * %zd would overflow ssize_t at dimension %zd", | ||
|
lucylq marked this conversation as resolved.
|
||
| numel, | ||
| static_cast<ssize_t>(sizes[i]), | ||
| i); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should remove ET_CHECK_MSG, this has a large blast radius here though (see PR summary).
Comment on lines
+44
to
+49
|
||
| numel = next_numel; | ||
| } | ||
| return numel; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| #include <executorch/runtime/executor/tensor_parser.h> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #include <c10/util/safe_numerics.h> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #include <executorch/runtime/core/exec_aten/exec_aten.h> | ||||||||||||||||||||||
| #include <executorch/runtime/core/exec_aten/util/dim_order_util.h> | ||||||||||||||||||||||
| #include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> | ||||||||||||||||||||||
|
|
@@ -118,17 +120,25 @@ Result<Tensor> parseTensor( | |||||||||||||||||||||
| dim_order = | ||||||||||||||||||||||
| const_cast<executorch::aten::DimOrderType*>(serialized_dim_order); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| // Validate sizes before using them in case the PTE data is bad. We can't | ||||||||||||||||||||||
| // detect bad positive values, but we can reject negative values, which would | ||||||||||||||||||||||
| // otherwise panic in the TensorImpl ctor. dim_order_to_stride() will validate | ||||||||||||||||||||||
| // dim_order. | ||||||||||||||||||||||
| // Validate sizes before using them in case the PTE data is bad. Reject | ||||||||||||||||||||||
| // negative values and check that the product of all dimensions doesn't | ||||||||||||||||||||||
| // overflow ssize_t, which would otherwise abort in the TensorImpl ctor. | ||||||||||||||||||||||
| // dim_order_to_stride() will validate dim_order. | ||||||||||||||||||||||
| ssize_t numel = 1; | ||||||||||||||||||||||
| for (flatbuffers::uoffset_t i = 0; i < dim; i++) { | ||||||||||||||||||||||
| ET_CHECK_OR_RETURN_ERROR( | ||||||||||||||||||||||
| sizes[i] >= 0, | ||||||||||||||||||||||
| InvalidProgram, | ||||||||||||||||||||||
| "Negative size[%zu] %" PRId32, | ||||||||||||||||||||||
| static_cast<size_t>(i), | ||||||||||||||||||||||
| sizes[i]); | ||||||||||||||||||||||
| ssize_t next_numel; | ||||||||||||||||||||||
| ET_CHECK_OR_RETURN_ERROR( | ||||||||||||||||||||||
| !c10::mul_overflows(numel, static_cast<ssize_t>(sizes[i]), &next_numel), | ||||||||||||||||||||||
| InvalidProgram, | ||||||||||||||||||||||
|
lucylq marked this conversation as resolved.
|
||||||||||||||||||||||
| "Overflow computing numel at dim %zu", | ||||||||||||||||||||||
| static_cast<size_t>(i)); | ||||||||||||||||||||||
| numel = next_numel; | ||||||||||||||||||||||
|
Comment on lines
+135
to
+141
|
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
||||||||||||||||||||||
| } | |
| } | |
| // Also ensure that the total number of bytes fits in size_t when | |
| // multiplied by the element size for this scalar type. | |
| const size_t element_size = elementSize(scalar_type); | |
| ET_CHECK_OR_RETURN_ERROR( | |
| numel == 0 || | |
| static_cast<size_t>(numel) <= SIZE_MAX / element_size, | |
| InvalidProgram, | |
| "Overflow computing tensor nbytes"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <climits>was added but doesn't appear to be used in this file. Consider removing it to avoid unnecessary dependencies.