From bef1655754fc4d5f75fdeb63a68d3632c5cc6ce0 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 23 Nov 2026 23:41:12 +0100 Subject: [PATCH] Fix FFI array types whose size overflows size_t The size of an array type was computed as length * element size without an overflow check, in FFI::arrayType() as well as for array declarators and nested declarators. A length that is valid on its own could wrap the size around, leaving the array type with a size far smaller than its length requires. --- NEWS | 4 ++ ext/ffi/ffi.c | 22 +++++++- ext/ffi/tests/array_size_overflow.phpt | 78 ++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 ext/ffi/tests/array_size_overflow.phpt diff --git a/NEWS b/NEWS index dfec4033c9af..4ccf941036df 100644 --- a/NEWS +++ b/NEWS @@ -59,6 +59,10 @@ PHP NEWS . Fixed use-after-free in getElementById() after a removed ID attribute is freed. (Ilia Alshanetsky) +- FFI: + . Fixed array types whose size overflows size_t being accepted with a + wrapped-around size. (Marc Bennewitz) + - Filter: . Fixed stack overflow in filter_var() and filter_var_array() with deeply nested arrays. (Eyüp Can Akman) diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index 97d32e5b99cc..83a05d86aae5 100644 --- a/ext/ffi/ffi.c +++ b/ext/ffi/ffi.c @@ -4327,6 +4327,7 @@ ZEND_METHOD(FFI, arrayType) /* {{{ */ ZEND_HASH_REVERSE_FOREACH_VAL(dims, val) { zend_long n = zval_get_long(val); + size_t element_size = ZEND_FFI_TYPE(type)->size; zend_ffi_type *new_type; if (n < 0) { @@ -4337,12 +4338,16 @@ ZEND_METHOD(FFI, arrayType) /* {{{ */ zend_throw_error(zend_ffi_exception_ce, "only the leftmost array can be undimensioned"); zend_ffi_type_dtor(type); RETURN_THROWS(); + } else if (element_size && n > SIZE_MAX / element_size) { + zend_throw_error(zend_ffi_exception_ce, "array size is too large"); + zend_ffi_type_dtor(type); + RETURN_THROWS(); } new_type = emalloc(sizeof(zend_ffi_type)); new_type->kind = ZEND_FFI_TYPE_ARRAY; new_type->attr = 0; - new_type->size = n * ZEND_FFI_TYPE(type)->size; + new_type->size = (size_t) n * element_size; new_type->align = ZEND_FFI_TYPE(type)->align; new_type->array.type = type; new_type->array.length = n; @@ -6456,10 +6461,16 @@ void zend_ffi_make_array_type(zend_ffi_dcl *dcl, const zend_ffi_val *len) /* {{{ LONGJMP(FFI_G(bailout), FAILURE); } + if (element_type->size && length > SIZE_MAX / element_type->size) { + zend_ffi_cleanup_dcl(dcl); + zend_ffi_parser_error("Array size is too large at line %d", FFI_G(line)); + return; + } + type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent)); type->kind = ZEND_FFI_TYPE_ARRAY; type->attr = FFI_G(default_type_attr) | (dcl->attr & ZEND_FFI_ARRAY_ATTRS); - type->size = length * element_type->size; + type->size = (size_t) length * element_type->size; type->align = element_type->align; type->array.type = dcl->type; type->array.length = length; @@ -7104,7 +7115,12 @@ static zend_result zend_ffi_nested_type(zend_ffi_type *type, zend_ffi_type *nest return FAILURE; } } - nested_type->size = nested_type->array.length * ZEND_FFI_TYPE(nested_type->array.type)->size; + size_t element_size = ZEND_FFI_TYPE(nested_type->array.type)->size; + if (element_size && nested_type->array.length > SIZE_MAX / element_size) { + zend_ffi_throw_parser_error("Array size is too large at line %d", FFI_G(line)); + return FAILURE; + } + nested_type->size = (size_t) nested_type->array.length * element_size; nested_type->align = ZEND_FFI_TYPE(nested_type->array.type)->align; return SUCCESS; break; diff --git a/ext/ffi/tests/array_size_overflow.phpt b/ext/ffi/tests/array_size_overflow.phpt new file mode 100644 index 000000000000..b1c940964994 --- /dev/null +++ b/ext/ffi/tests/array_size_overflow.phpt @@ -0,0 +1,78 @@ +--TEST-- +FFI: array types whose size overflows size_t are rejected +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +> 1; // int32_t[3][$quarter] still fits + +$cases = [ + 'FFI::arrayType() just too large' => + fn() => FFI::arrayType(FFI::cdef()->type('int32_t'), [$max + 1]), + 'FFI::arrayType()' => + fn() => FFI::arrayType(FFI::cdef()->type('int32_t'), [$wrap]), + 'FFI::arrayType() of PHP_INT_MAX' => + fn() => FFI::arrayType(FFI::cdef()->type('int32_t'), [PHP_INT_MAX]), + 'FFI::arrayType() outer dimension' => + fn() => FFI::arrayType(FFI::cdef()->type('int32_t'), [4, $half]), + 'cdef array just too large' => + fn() => FFI::cdef("typedef int32_t t[" . ($max + 1) . "];"), + 'cdef array' => + fn() => FFI::cdef("typedef int32_t t[$wrap];"), + 'cdef outer dimension' => + fn() => FFI::cdef("typedef int32_t t[4][$half];"), + 'cdef nested declarator' => + fn() => FFI::cdef("typedef int32_t (t[$wrap]);"), + 'cdef nested declarator outer dimension' => + fn() => FFI::cdef("typedef int32_t (t[4][$half]);"), +]; + +foreach ($cases as $label => $fn) { + try { + $fn(); + echo "$label: unexpected success\n"; + } catch (FFI\Exception $e) { + echo "$label: ", $e::class, ": ", $e->getMessage(), "\n"; + } +} + +/* The largest arrays that still fit are accepted. */ +FFI::arrayType(FFI::cdef()->type('int32_t'), [$max]); +FFI::arrayType(FFI::cdef()->type('int32_t'), [3, $quarter]); +FFI::cdef("typedef int32_t t1[$max]; typedef int32_t t2[3][$quarter];"); +FFI::cdef("typedef int32_t (t1[$max]); typedef int32_t (t2[3][$quarter]);"); + +/* Arrays of zero-size elements (GNU C) stay zero-size whatever the length. */ +$len = PHP_INT_MAX; +$ffi = FFI::cdef("struct e {}; struct z { int32_t a[0]; };"); +var_dump(FFI::sizeof(FFI::arrayType($ffi->type('struct e'), [$len, $len]))); +var_dump(FFI::sizeof(FFI::arrayType($ffi->type('struct z'), [$len]))); +$ffi = FFI::cdef("struct e {}; typedef struct e t1[$len][$len]; typedef struct e (t2[$len]);"); +var_dump(FFI::sizeof($ffi->type('t1')), FFI::sizeof($ffi->type('t2'))); +echo "done\n"; +?> +--EXPECT-- +FFI::arrayType() just too large: FFI\Exception: array size is too large +FFI::arrayType(): FFI\Exception: array size is too large +FFI::arrayType() of PHP_INT_MAX: FFI\Exception: array size is too large +FFI::arrayType() outer dimension: FFI\Exception: array size is too large +cdef array just too large: FFI\ParserException: Array size is too large at line 1 +cdef array: FFI\ParserException: Array size is too large at line 1 +cdef outer dimension: FFI\ParserException: Array size is too large at line 1 +cdef nested declarator: FFI\ParserException: Array size is too large at line 1 +cdef nested declarator outer dimension: FFI\ParserException: Array size is too large at line 1 +int(0) +int(0) +int(0) +int(0) +done