Skip to content

fix oob read on malformed length field in dba flatfile handler#22266

Open
alhudz wants to merge 1 commit into
php:masterfrom
alhudz:dba-flatfile-length-overflow
Open

fix oob read on malformed length field in dba flatfile handler#22266
alhudz wants to merge 1 commit into
php:masterfrom
alhudz:dba-flatfile-length-overflow

Conversation

@alhudz

@alhudz alhudz commented Jun 10, 2026

Copy link
Copy Markdown

A flatfile opened read-only parses an attacker-controlled record length, and a negative value overruns the read buffer.

  1. num is a size_t but is assigned from atoi(buf), so a length line of -1 becomes SIZE_MAX.
  2. the grow step then does buf_size = num + FLATFILE_BLOCK_SIZE, which wraps to a tiny value and erealloc shrinks buf.
  3. php_stream_read(dba->fp, buf, num) then reads the rest of the file (num is still SIZE_MAX) into the small buffer.

repro: file_put_contents('x', "-1\n" . str_repeat('A', 200000)); $db = dba_open('x', 'r', 'flatfile'); dba_firstkey($db);
expected: bool(false)
actual: zend_mm_heap corrupted (SIGABRT)

Reachable from dba_firstkey/dba_nextkey/dba_fetch/dba_exists/dba_delete. The same idiom is repeated in flatfile_delete, flatfile_findkey, flatfile_firstkey and flatfile_nextkey, so the guard goes in each. Added a regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant