Skip to content

Fix freeing uninitialized memory in LDAP sort control parsing#88

Closed
iliaal wants to merge 2 commits into
PHP-8.4from
fix-ldap-sort-control
Closed

Fix freeing uninitialized memory in LDAP sort control parsing#88
iliaal wants to merge 2 commits into
PHP-8.4from
fix-ldap-sort-control

Conversation

@iliaal

@iliaal iliaal commented Jun 16, 2026

Copy link
Copy Markdown
Owner

ldap_search() and the other control-taking LDAP functions free uninitialized memory when a sort control is built from a malformed key list. php_ldap_control_from_array() allocates sort_keys with safe_emalloc() and writes the NULL terminator only after the per-key loop completes, but a sort key missing the "attr" entry makes the loop bail out early via a ValueError, leaving the tail of the array uninitialized. The failure cleanup then walks sort_keys as a NULL-terminated list and calls efree() on an uninitialized slot, freeing a garbage pointer. Allocating with ecalloc() makes the unwritten slots NULL so the cleanup stops at the first one.

The crash is reachable before any server connection: ldap_search($ld, $base, $filter, controls: [["oid" => LDAP_CONTROL_SORTREQUEST, "value" => [["attr" => "cn"], ["reverse" => true]]]]) aborts under ASAN.

@iliaal iliaal force-pushed the fix-ldap-sort-control branch from a699ff3 to 1ac9a73 Compare June 16, 2026 20:00
@iliaal iliaal force-pushed the fix-ldap-sort-control branch from 1ac9a73 to d2ad57b Compare June 16, 2026 21:28
_php_ldap_control_from_array() allocated the sort_keys array with
safe_emalloc() and only wrote its NULL terminator after the per-key loop
finished. A sort key missing the "attr" entry makes the loop bail out
early, leaving the array partially uninitialized; the failure cleanup
then walks it as a NULL-terminated list and calls efree() on the
uninitialized slots. Allocate the array zeroed with ecalloc() so the
unwritten slots are NULL. Reachable from userland via the $controls
argument of ldap_search() and the other control-taking LDAP functions.

Closes phpGH-22342
@iliaal iliaal force-pushed the fix-ldap-sort-control branch from d2ad57b to f0450fa Compare June 16, 2026 21:28
@iliaal

iliaal commented Jun 16, 2026

Copy link
Copy Markdown
Owner Author

Submitted upstream as php#22342.

@iliaal iliaal closed this Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants