Skip to content

Commit 428cc06

Browse files
lucylqfacebook-github-bot
authored andcommitted
Fix write-heap-buffer-overflow in et_copy_index (pytorch#15605)
Summary: 1. Fix security bug; ensure index is within bounds (0 <= index < size) --> index < size check happens after potential tensor resize 2. Convert ET_CHECK_MSG --> ET_KERNEL_CHECK_MSG for error reporting --- The crash is a write-heap-buffer-overflow that occurs in the `et_copy_index` function. The root cause is the lack of proper validation of the `index` argument, which can lead to an out-of-bounds write when `index` is negative or exceeds the bounds of the `copy_to` tensor. The patch fixes the crash by adding two checks: `ET_CHECK_MSG(index >= 0, "Index must be non-negative");` and `ET_CHECK_MSG(index < copy_to.sizes()[0], "Index out of bounds");`. These checks ensure that `index` is within the valid range for the `copy_to` tensor, preventing the out-of-bounds write. Other considerations that reviewers should take into account when validating the patch include verifying that the added checks do not introduce any performance regressions and that they correctly handle edge cases, such as when `index` is equal to `copy_to.sizes()[0] - 1`. Reviewers should also check that the patch does not alter the existing functionality of the `et_copy_index` function and that it is consistent with the surrounding code. Additionally, reviewers may want to consider testing the patch with various inputs, including negative `index` values, `index` values that exceed the bounds of `copy_to`, and valid `index` values, to ensure that the patch correctly prevents the write-heap-buffer-overflow crash. Here is the commit message: ``` Fix write-heap-buffer-overflow crash in et_copy_index The crash is a write-heap-buffer-overflow that occurs in the `et_copy_index` function. The root cause is the lack of proper validation of the `index` argument, which can lead to an out-of-bounds write when `index` is negative or exceeds the bounds of the `copy_to` tensor. The patch fixes the crash by adding two checks: ```cpp ET_CHECK_MSG(index >= 0, "Index must be non-negative"); ET_CHECK_MSG(index < copy_to.sizes()[0], "Index out of bounds"); ``` These checks ensure that `index` is within the valid range for the `copy_to` tensor, preventing the out-of-bounds write. Other considerations that reviewers should take into account when validating the patch include verifying that the added checks do not introduce any performance regressions and that they correctly handle edge cases, such as when `index` is equal to `copy_to.sizes()[0] - 1`. Reviewers should also check that the patch does not alter the existing functionality of the `et_copy_index` function and that it is consistent with the surrounding code. ``` NOTE: This diff is entirely auto-generated by LLM-based patch generator. Reviewer should carefully examine this diff as Lionhead does not guarrantee the correctnesss of the patch beyond fixing the crash and passing existing tests. Please commandeer this diff and revise as needed. Our bot does not respond to comments or revision requests (yet). Differential Revision: D80399111
1 parent 6b545a2 commit 428cc06

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

kernels/prim_ops/et_copy_index.cpp

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ constexpr size_t kTensorDimensionLimit = 16;
5959
// torch.ops.executorch.prim.add.int(iteration_index, 1, iteration_index)
6060
// done_bool = torch.ops.executorch.prim.eq.int(iteration_index,
6161
// sym_size, done_bool) # Emitter inserts a instruction here, if
62-
// done_bool == False jump to selcect_copy op # if not continue. return
62+
// done_bool == False jump to select_copy op # if not continue. return
6363
// add_tensor
6464
//
6565
// The output of each iteration (copy_from) is copied into the copy_to tensor at
@@ -79,12 +79,25 @@ void et_copy_index(KernelRuntimeContext& context, Span<EValue*> stack) {
7979
auto copy_from = (*stack[1]).toTensor();
8080
auto index = (*stack[2]).toInt();
8181

82+
ET_KERNEL_CHECK_MSG(
83+
context,
84+
index >= 0,
85+
InvalidArgument,
86+
/* void */,
87+
"Expected index to be non-negative, got %" ET_PRI_TENSOR_SIZE,
88+
static_cast<size_t>(index));
89+
8290
// Number of bytes we need to copy over from copy_from tensor.
8391
size_t size_copy_from = (copy_from.element_size()) * (copy_from.numel());
8492

85-
ET_CHECK_MSG(
93+
ET_KERNEL_CHECK_MSG(
94+
context,
8695
(copy_to.sizes().size() - copy_from.sizes().size()) == 1,
87-
"Ranks of copy_to and copy_from tensor should only differ by 1.");
96+
InvalidArgument,
97+
/* void */,
98+
"Ranks of copy_to %zu and copy_from tensor %zu should only differ by 1.",
99+
copy_to.sizes().size(),
100+
copy_from.sizes().size());
88101

89102
// Here we calculate the size of the out_tensor after copy_from has
90103
// been copied to it. This will be passed onto the resize call.
@@ -93,9 +106,15 @@ void et_copy_index(KernelRuntimeContext& context, Span<EValue*> stack) {
93106
// If we're copying past the first index then the shape of
94107
// copy_from and copy_to without the leading dimension should be
95108
// the same. i.e. copy_to.size[1:] == copy_from.size[:].
96-
ET_CHECK_MSG(
109+
ET_KERNEL_CHECK_MSG(
110+
context,
97111
copy_to.sizes()[i + 1] == copy_from.sizes()[i],
98-
"Mismatch in shape between copy_to and copy_from tensors");
112+
InvalidArgument,
113+
/* void */,
114+
"Mismatch in shape between copy_to %" ET_PRI_TENSOR_SIZE
115+
" and copy_from %" ET_PRI_TENSOR_SIZE " tensors",
116+
static_cast<size_t>(copy_to.sizes()[i + 1]),
117+
static_cast<size_t>(copy_from.sizes()[i]));
99118
expected_output_size[i + 1] = copy_from.sizes()[i];
100119
}
101120

@@ -105,11 +124,25 @@ void et_copy_index(KernelRuntimeContext& context, Span<EValue*> stack) {
105124
Error err =
106125
resize_tensor(copy_to, {expected_output_size, copy_to.sizes().size()});
107126
ET_CHECK(err == Error::Ok);
108-
ET_CHECK_MSG(
127+
ET_KERNEL_CHECK_MSG(
128+
context,
109129
data_ptr == copy_to.const_data_ptr(),
130+
InvalidState,
131+
/* void */,
110132
"Data ptr of copy_to tensor changed after resize which isn't allowed for static/upper-bounded tensors");
111133
}
112134

135+
// After potential resize, verify that index is within bounds.
136+
ET_KERNEL_CHECK_MSG(
137+
context,
138+
index < copy_to.sizes()[0],
139+
InvalidArgument,
140+
/* void */,
141+
"Index %" ET_PRI_TENSOR_SIZE
142+
" out of bounds for tensor size %" ET_PRI_TENSOR_SIZE,
143+
static_cast<size_t>(index),
144+
static_cast<size_t>(copy_to.sizes()[0]));
145+
113146
auto copy_to_ptr = copy_to.const_data_ptr();
114147
auto copy_from_ptr = copy_from.const_data_ptr();
115148

@@ -118,12 +151,23 @@ void et_copy_index(KernelRuntimeContext& context, Span<EValue*> stack) {
118151
// copy_from into the copy_to tensor.
119152

120153
// Check that the destination has enough space for the copy.
154+
ET_KERNEL_CHECK_MSG(
155+
context,
156+
size_copy_from == 0 || index <= SIZE_MAX / size_copy_from,
157+
InvalidArgument,
158+
/* void */,
159+
"Offset multiplication overflow. size_copy_from: %zu, index: %" ET_PRI_TENSOR_SIZE,
160+
size_copy_from,
161+
static_cast<size_t>(index));
121162
size_t offset = index * size_copy_from;
122163
size_t copy_to_size = copy_to.element_size() * copy_to.numel();
123-
ET_CHECK_MSG(
124-
offset + size_copy_from <= copy_to_size,
125-
"Buffer overflow: copy_to tensor is smaller than copy_from tensor.");
126-
164+
ET_KERNEL_CHECK_MSG(
165+
context,
166+
(offset <= SIZE_MAX - size_copy_from) &&
167+
(offset + size_copy_from <= copy_to_size),
168+
InvalidArgument,
169+
/* void */,
170+
"Buffer overflow; offset overflow or copy_to tensor is smaller than copy_from tensor.");
127171
memcpy(
128172
// NOLINTNEXTLINE(performance-no-int-to-ptr)
129173
(void*)((uintptr_t)copy_to_ptr + offset),

kernels/prim_ops/test/prim_ops_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,9 @@ TEST_F(RegisterPrimOpsTest, TestETCopyIndexMismatchShape) {
276276
// Try to copy and replace at index 1. This will fail because
277277
// copy_to.sizes[1:] and to_copy.sizes[:] don't match each other
278278
// which is a pre-requisite for this operator.
279-
ET_EXPECT_DEATH(
280-
getOpsFn("executorch_prim::et_copy_index.tensor")(context_, stack), "");
279+
ET_EXPECT_KERNEL_FAILURE(
280+
context_,
281+
getOpsFn("executorch_prim::et_copy_index.tensor")(context_, stack));
281282
}
282283

283284
TEST_F(RegisterPrimOpsTest, TestETCopyIndexStaticShape) {

0 commit comments

Comments
 (0)