fix: memory-safety bugs reachable from ordinary PHP - #12
Open
somethingwithproof wants to merge 14 commits into
Open
fix: memory-safety bugs reachable from ordinary PHP#12somethingwithproof wants to merge 14 commits into
somethingwithproof wants to merge 14 commits into
Conversation
Without tests/data the SKIPIF blocks skip 16 of the 22 tests, so CI reported green while most of the suite never ran. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Both were written against an older librrd and fail against 1.7.2 and 1.11.0 alike; they were never noticed because the fixtures were missing. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Called with one argument, zend_parse_parameters never writes start_time, so the following `if (start_time)` dereferenced whatever the stack happened to hold and copied it into the --start option. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
An integer key gave RRDUpdater::update() a NULL zend_string, save() on a bare RRDCreator merged undefined zvals, and every method assumed __construct had run; all three reached librrd through a NULL pointer. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
ZEND_LTOA writes its terminator at the return value of snprintf, so an 11-byte buffer overflowed once a timestamp needed more than ten digits, and rrd_fetch's 32-bit loop counter wrapped past the end of librrd's data. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
A second __construct kept the previous step, data sources and archives, so the new file was written from the old object's definition. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
somethingwithproof
force-pushed
the
fix/memory-safety
branch
from
August 21, 2026 04:27
90093b3 to
d3c14d3
Compare
The new !ds_data / !data / !step tests declare those locals untrusted, and the hoisted free() reaches them on the same paths, so leaving them uninitialised traded a leak for a free of whatever the stack held. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Both write a file the caller names, and both skipped the check that rrd_create() and RRDGraph::save() already perform. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
convert_to_string() edited the array in place, so a non-string option changed the caller's variable and every by-value copy of it. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
zval_get_string() hands back an empty string with an exception already pending, so the original Error was masked by whatever librrd then reported. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Converting a value runs __toString, which can call __construct to repoint the object past the open_basedir check, or setOptions to free the array being walked. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
…t list Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The "was not constructed" guards test for NULL, and rrd_fetch keyed its per-source arrays by name, so duplicate names left the walk short of ds_cnt entries. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
… objects The duplicate-name case is a crash on master: renaming one ds_nam over another in the header leaves rrd_fetch walking past the end of the array it built. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
This was referenced Aug 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Five ways to reach librrd through a bad pointer, all from ordinary PHP. Each fix has a test; every one of the reproducers below segfaults on master and is clean after.
RRDCreator::__construct($path)(rrd_create.c:107) declareszend_string *start_time;and never initialises it. With one argumentzend_parse_parametersdoes not write it, soif (start_time) ... estrdup(ZSTR_VAL(start_time))dereferences whatever the stack held and copies it into--start. valgrind on master:With the stack slot primed by an earlier three-argument call, the bytes it reads come back out through the exception:
Exception: start time: unparsable time: p!.RRDUpdater::update(array(1.0))—ZEND_HASH_FOREACH_STR_KEY_VALyields a NULLzend_stringfor an integer key, andZSTR_VALdereferences it. A list array is an easy mistake to make; it should be an exception, not a crash.(new RRDCreator($p))->save()with noaddDataSource/addArchive—php_array_mergegetsZ_ARRVALof anIS_UNDEFzval.Any of the three classes via
newInstanceWithoutConstructor()—file_pathis NULL and goes straight tostrlen().Timestamps wider than ten digits.
rrd_fetchandrrd_xportformat intochar str_timestamp[11]withZEND_LTOA, whose non-Windows definition issnprintfreturns the length it wanted, so once a timestamp needs more than ten digits the terminator lands past the array.rrd_xport'stime_indexis atime_ttaken from the caller's--end, which puts the offset under the caller's control. Under gdb on master,str_timestampis at0xffffffffbc10and thetime_datazval at0xffffffffbc20; a 17-digit timestamp writes at+17and rewrites the zval's pointer from0xfffff5e572a0to0xfffff5e500a0, and the nextadd_assoc_doublesegfaults. (PHP removedZEND_LTOAin master "as it was unsafe" —UPGRADING.INTERNALS.)rrd_fetchhas the same buffer plus a 32-bitunsignedloop counter against 64-bitstart/end. An--endpast 2^32 wraps it, the loop stops terminating, anddatapwalks off librrd's allocation — measured at the fault,ds_datais 8016 bytes anddatapis 23752 bytes in, with the leaked doubles landing in the returned array. A zerostepwould spin the same loop forever.The buffers become
MAX_LENGTH_OF_LONG + 1with a plainsnprintf, the counter becomestime_t, and a zero step reports no data.Repeated
__constructcalls also leaked the previous strings (42 bytes per pair under valgrind), and kept the previous step, data sources and archives so the new file was written from the old object's definition. The constructors now release and reset what they replace.The new
!stepguards initially skipped librrd's ownfree()calls, which sat inside the branch being bypassed. That is fixed here: the frees are hoisted so they run on every path. Forcingstep == 0over 200 calls, before and after:Validation
php:8.4-cli with librrd 1.7.2 and macOS with librrd 1.11.0. 28/28 tests pass on both, 0 leaked under
TEST_PHP_ARGS=-m, andvalgrind -qon the constructor reproducer is silent. Stacked on #11 — without it most of these tests would skip.Scope note
RRDCreator::save()andRRDGraph::saveVerbose()still lack theopen_basedircheck their procedural siblings perform. That gap is pre-existing and is closed in the PR stacked directly on this one, which is where it belongs; this branch is scoped to memory safety.Stack order. This is part of a series, each building on the one before. Merge order:
#11 (test suite) → #12 (memory safety) → #13 (open_basedir, value semantics) → #14 (php-src master) → #15 (librrd argv) → #16 (argument builder) → #17 (autoconf). Commits below this PR's own belong to the ones underneath it.