Bug #80186 [NEW]: Segfault when iterating over FFI object

From: Date: Sun, 04 Oct 2020 20:40:56 +0000
Subject: Bug #80186 [NEW]: Segfault when iterating over FFI object
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229376@lists.php.net to get a copy of this message
From:             as
Operating system: 
PHP version:      8.0Git-2020-10-04 (Git)
Package:          Reproducible crash
Bug Type:         Bug
Bug description:Segfault when iterating over FFI object

Description:
------------
Currently zend_fake_get_properties in ext/ffi/ffi.c returns a
pointer to const HashTable zend_empty_array. Attempting to iterate
over this array in userland leads to a segfault because PHP tries to
increment zend_empty_array's nIteratorsCount which is const memory.

The proposed patch allows objects to return NULL for the
get_properties handler. In this case, the VM skips the foreach block.

If this approach is acceptable, there are other extensions and call
sites[1][2] to fix where zend_empty_array leaks into userland.

[1]
https://github.com/php/php-src/blob/107962208a19d8b6dc1a190cb25fc37614411e71/ext/com_dotnet/com_handlers.c#L221-L229
[2] https://github.com/protocolbuffers/protobuf/issues/7319


Test script:
---------------
<?php

$ffi = FFI::cdef('');

foreach ($ffi as $_) {
}


Expected result:
----------------
no segfault

Actual result:
--------------
segfault

-- 
Edit bug report at https://bugs.php.net/bug.php?id=80186&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=80186&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=80186&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=80186&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=80186&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=80186&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=80186&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=80186&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=80186&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=80186&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=80186&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80186&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=80186&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=80186&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=80186&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=80186&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=80186&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=80186&r=mysqlcfg


Thread (4 messages)

« previous php.bugs (#229376) next »