From c5f84db7af0b4625a0ba0ba726489cd2e64bc421 Mon Sep 17 00:00:00 2001 From: pedrobtz Date: Wed, 21 Oct 2026 23:58:20 +0200 Subject: [PATCH] fix: Reject lengths whose buffer sizes overflow int64 in minimal validation The minimum buffer sizes and the fixed-size list child length were computed by multiplying offset + length without an overflow check, so a huge length could wrap around to a small size and pass validation. Co-Authored-By: Claude Opus 5.5 --- src/nanoarrow/common/array.c | 19 +++++++++++++++ src/nanoarrow/common/array_test.cc | 38 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/nanoarrow/common/array.c b/src/nanoarrow/common/array.c index e4538955c..ec0ca464a 100644 --- a/src/nanoarrow/common/array.c +++ b/src/nanoarrow/common/array.c @@ -1619,6 +1619,17 @@ static int ArrowArrayViewValidateMinimal(struct ArrowArrayView* array_view, // Only loop over the first two buffers because the size of the third buffer // is always data dependent for all current Arrow types. for (int i = 0; i < 2; i++) { + // Ensure that the buffer sizes calculated below fit within an int64 (the largest + // is the element size in bits times offset + length + 1, rounded up to bytes) + int64_t element_size_bits = array_view->layout.element_size_bits[i]; + if (element_size_bits > 0 && + offset_plus_length > (INT64_MAX - 8) / element_size_bits - 1) { + ArrowErrorSet(error, + "Offset + length of %" PRId64 " is too large for %s array buffer %d", + offset_plus_length, ArrowTypeString(array_view->storage_type), i); + return EINVAL; + } + int64_t element_size_bytes = array_view->layout.element_size_bits[i] / 8; // Initialize with a value that will cause an error if accidentally used uninitialized // Need to suppress the clang-tidy warning because gcc warns for possible use @@ -1724,6 +1735,14 @@ static int ArrowArrayViewValidateMinimal(struct ArrowArrayView* array_view, break; case NANOARROW_TYPE_FIXED_SIZE_LIST: + if (array_view->layout.child_size_elements > 0 && + offset_plus_length > INT64_MAX / array_view->layout.child_size_elements) { + ArrowErrorSet(error, + "Offset + length of %" PRId64 + " is too large for fixed_size_list array with list size %" PRId64, + offset_plus_length, array_view->layout.child_size_elements); + return EINVAL; + } child_min_length = (array_view->offset + array_view->length) * array_view->layout.child_size_elements; if (array_view->children[0]->length < child_min_length) { diff --git a/src/nanoarrow/common/array_test.cc b/src/nanoarrow/common/array_test.cc index 9324cdde6..6fd3f0b92 100644 --- a/src/nanoarrow/common/array_test.cc +++ b/src/nanoarrow/common/array_test.cc @@ -2839,6 +2839,44 @@ TEST(ArrayTest, ArrayViewTestMove) { ArrowArrayViewReset(&array_view2); } +TEST(ArrayTest, ArrayViewTestBufferSizeOverflow) { + struct ArrowError error; + + // 4 * (2^62 + 1) wraps around to 4, so without a check a 16-byte data buffer + // would pass as large enough for 2^62 + 1 int32 values + struct ArrowArrayView array_view; + ArrowArrayViewInitFromType(&array_view, NANOARROW_TYPE_INT32); + uint8_t data[16] = {0}; + array_view.length = (int64_t{1} << 62) + 1; + array_view.buffer_views[1].data.as_uint8 = data; + array_view.buffer_views[1].size_bytes = sizeof(data); + EXPECT_EQ( + ArrowArrayViewValidate(&array_view, NANOARROW_VALIDATION_LEVEL_MINIMAL, &error), + EINVAL); + EXPECT_STREQ(error.message, + "Offset + length of 4611686018427387905 is too large for int32 array " + "buffer 1"); + ArrowArrayViewReset(&array_view); + + // The same for a fixed-size list's minimum child length + nanoarrow::UniqueSchema schema; + ArrowSchemaInit(schema.get()); + ASSERT_EQ(ArrowSchemaSetTypeFixedSize(schema.get(), NANOARROW_TYPE_FIXED_SIZE_LIST, 4), + NANOARROW_OK); + ASSERT_EQ(ArrowSchemaSetType(schema->children[0], NANOARROW_TYPE_NA), NANOARROW_OK); + nanoarrow::UniqueArrayView list_view; + ASSERT_EQ(ArrowArrayViewInitFromSchema(list_view.get(), schema.get(), &error), + NANOARROW_OK); + list_view->length = (int64_t{1} << 62) + 1; + list_view->children[0]->length = 4; + EXPECT_EQ( + ArrowArrayViewValidate(list_view.get(), NANOARROW_VALIDATION_LEVEL_MINIMAL, &error), + EINVAL); + EXPECT_STREQ(error.message, + "Offset + length of 4611686018427387905 is too large for fixed_size_list " + "array with list size 4"); +} + TEST(ArrayTest, ArrayViewTestString) { struct ArrowArrayView array_view; struct ArrowError error;