Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 19 additions & 3 deletions ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
78 changes: 78 additions & 0 deletions ext/ffi/tests/array_size_overflow.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
--TEST--
FFI: array types whose size overflows size_t are rejected
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
/* The size of an array type was computed as length * element size without
* an overflow check. A length that is valid on its own wrapped the size around
* to a few bytes, so FFI::new() allocated a tiny buffer while element access,
* which is only checked against the length, reached far beyond it. */
$bits = PHP_INT_SIZE * 8;
$max = (1 << ($bits - 2)) - 1; // int32_t[$max] is the largest int32_t array that fits
$wrap = $max + 2; // int32_t[$wrap] wraps around to a size of 4 bytes
$half = 1 << ($bits - 3); // int32_t[$half] fits, int32_t[4][$half] doesn't
$quarter = $half >> 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
Loading