Skip to content

Commit 0cf7016

Browse files
author
Github Executorch
committed
Fix TOB-EXECUTORCH-41: validate buffer_idx bounds in getConstantDataPtr
Add bounds checking on buffer_idx in both constant_buffer and constant_data code paths of getConstantDataPtr to prevent out-of-bounds vector access from malicious flatbuffer inputs. Authored-with: Claude
1 parent 21d9c64 commit 0cf7016

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

backends/xnnpack/runtime/XNNCompiler.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,28 @@ const uint8_t* getConstantDataPtr(
184184
if (!constant_data_ptr) {
185185
// TODO(T172265611): Remove constant_buffer in flatbuffer path after BC
186186
// window
187-
const auto& constant_buffer = *flatbuffer_graph->constant_buffer();
187+
auto* cb = flatbuffer_graph->constant_buffer();
188+
if (cb == nullptr || buffer_idx >= cb->size()) {
189+
ET_LOG(
190+
Error,
191+
"Invalid buffer_idx %u for constant_buffer of size %u",
192+
buffer_idx,
193+
cb ? cb->size() : 0);
194+
return nullptr;
195+
}
196+
const auto& constant_buffer = *cb;
188197
return constant_buffer[buffer_idx]->storage()->data();
189198
} else {
190-
ConstantDataOffsetPtr constant_data_offset =
191-
flatbuffer_graph->constant_data()->Get(buffer_idx);
199+
auto* cd = flatbuffer_graph->constant_data();
200+
if (cd == nullptr || buffer_idx >= cd->size()) {
201+
ET_LOG(
202+
Error,
203+
"Invalid buffer_idx %u for constant_data of size %u",
204+
buffer_idx,
205+
cd ? cd->size() : 0);
206+
return nullptr;
207+
}
208+
ConstantDataOffsetPtr constant_data_offset = cd->Get(buffer_idx);
192209
uint64_t offset = constant_data_offset->offset();
193210

194211
bool has_named_key = flatbuffers::IsFieldPresent(

0 commit comments

Comments
 (0)