Skip to content

Commit 8919ef8

Browse files
lucylqGithub Executorch
andauthored
Fix integer overflow in data loader bounds checks (#18676)
Replace `offset + size <= file_size_` with overflow-safe `c10::add_overflows` Affected files: MmapDataLoader, FileDataLoader,FileDescriptorDataLoader, SharedPtrDataLoader. This PR was authored with the assistance of Claude. -- also update the error message --------- Co-authored-by: Github Executorch <github_executorch@arm.com>
1 parent 0ee0f67 commit 8919ef8

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

extension/data_loader/buffer_data_loader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class BufferDataLoader final : public executorch::runtime::DataLoader {
4040
ET_CHECK_OR_RETURN_ERROR(
4141
!overflow && total_size <= size_,
4242
InvalidArgument,
43-
"offset %zu + size %zu > size_ %zu",
43+
"offset %zu + size %zu > size_ %zu, or overflow detected",
4444
offset,
4545
size,
4646
size_);

extension/data_loader/file_data_loader.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <sys/stat.h>
2020
#include <sys/types.h>
2121

22+
#include <c10/util/safe_numerics.h>
2223
#include <executorch/runtime/core/error.h>
2324
#include <executorch/runtime/core/result.h>
2425
#include <executorch/runtime/platform/log.h>
@@ -143,10 +144,12 @@ Result<FreeableBuffer> FileDataLoader::load(
143144
fd_ >= 0,
144145
InvalidState,
145146
"Uninitialized");
147+
size_t total_size;
148+
bool overflow = c10::add_overflows(offset, size, &total_size);
146149
ET_CHECK_OR_RETURN_ERROR(
147-
offset + size <= file_size_,
150+
!overflow && total_size <= file_size_,
148151
InvalidArgument,
149-
"File %s: offset %zu + size %zu > file_size_ %zu",
152+
"File %s: offset %zu + size %zu > file_size_ %zu, or overflow detected",
150153
file_name_,
151154
offset,
152155
size,
@@ -204,10 +207,12 @@ ET_NODISCARD Error FileDataLoader::load_into(
204207
fd_ >= 0,
205208
InvalidState,
206209
"Uninitialized");
210+
size_t total_size;
211+
bool overflow = c10::add_overflows(offset, size, &total_size);
207212
ET_CHECK_OR_RETURN_ERROR(
208-
offset + size <= file_size_,
213+
!overflow && total_size <= file_size_,
209214
InvalidArgument,
210-
"File %s: offset %zu + size %zu > file_size_ %zu",
215+
"File %s: offset %zu + size %zu > file_size_ %zu, or overflow detected",
211216
file_name_,
212217
offset,
213218
size,

extension/data_loader/file_descriptor_data_loader.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <sys/types.h>
2020
#include <unistd.h>
2121

22+
#include <c10/util/safe_numerics.h>
2223
#include <executorch/runtime/core/error.h>
2324
#include <executorch/runtime/core/result.h>
2425
#include <executorch/runtime/platform/log.h>
@@ -157,10 +158,12 @@ Result<FreeableBuffer> FileDescriptorDataLoader::load(
157158
fd_ >= 0,
158159
InvalidState,
159160
"Uninitialized");
161+
size_t total_size;
162+
bool overflow = c10::add_overflows(offset, size, &total_size);
160163
ET_CHECK_OR_RETURN_ERROR(
161-
offset + size <= file_size_,
164+
!overflow && total_size <= file_size_,
162165
InvalidArgument,
163-
"File %s: offset %zu + size %zu > file_size_ %zu",
166+
"File %s: offset %zu + size %zu > file_size_ %zu, or overflow detected",
164167
file_descriptor_uri_,
165168
offset,
166169
size,
@@ -218,10 +221,12 @@ ET_NODISCARD Error FileDescriptorDataLoader::load_into(
218221
fd_ >= 0,
219222
InvalidState,
220223
"Uninitialized");
224+
size_t total_size;
225+
bool overflow = c10::add_overflows(offset, size, &total_size);
221226
ET_CHECK_OR_RETURN_ERROR(
222-
offset + size <= file_size_,
227+
!overflow && total_size <= file_size_,
223228
InvalidArgument,
224-
"File %s: offset %zu + size %zu > file_size_ %zu",
229+
"File %s: offset %zu + size %zu > file_size_ %zu, or overflow detected",
225230
file_descriptor_uri_,
226231
offset,
227232
size,

extension/data_loader/mmap_data_loader.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <sys/stat.h>
1818
#include <sys/types.h>
1919

20+
#include <c10/util/safe_numerics.h>
2021
#include <executorch/extension/data_loader/mman.h>
2122
#include <executorch/runtime/core/error.h>
2223
#include <executorch/runtime/core/result.h>
@@ -159,10 +160,12 @@ Error MmapDataLoader::validate_input(size_t offset, size_t size) const {
159160
fd_ >= 0,
160161
InvalidState,
161162
"Uninitialized");
163+
size_t total_size;
164+
bool overflow = c10::add_overflows(offset, size, &total_size);
162165
ET_CHECK_OR_RETURN_ERROR(
163-
offset + size <= file_size_,
166+
!overflow && total_size <= file_size_,
164167
InvalidArgument,
165-
"File %s: offset %zu + size %zu > file_size_ %zu",
168+
"File %s: offset %zu + size %zu > file_size_ %zu, or overflow detected",
166169
file_name_,
167170
offset,
168171
size,

extension/data_loader/shared_ptr_data_loader.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#pragma once
1010

11+
#include <c10/util/safe_numerics.h>
1112
#include <executorch/runtime/core/data_loader.h>
1213
#include <executorch/runtime/core/error.h>
1314
#include <executorch/runtime/core/result.h>
@@ -33,10 +34,12 @@ class SharedPtrDataLoader final : public executorch::runtime::DataLoader {
3334
size_t offset,
3435
size_t size,
3536
ET_UNUSED const DataLoader::SegmentInfo& segment_info) const override {
37+
size_t total_size;
38+
bool overflow = c10::add_overflows(offset, size, &total_size);
3639
ET_CHECK_OR_RETURN_ERROR(
37-
offset + size <= size_,
40+
!overflow && total_size <= size_,
3841
InvalidArgument,
39-
"offset %zu + size %zu > size_ %zu",
42+
"offset %zu + size %zu > size_ %zu, or overflow detected",
4043
offset,
4144
size,
4245
size_);

0 commit comments

Comments
 (0)