diff --git a/ext/ldap/ldap.c b/ext/ldap/ldap.c index ca9e5fc2202e..ee877b39a67f 100644 --- a/ext/ldap/ldap.c +++ b/ext/ldap/ldap.c @@ -234,6 +234,12 @@ static void ldap_result_entry_free_obj(zend_object *obj) } \ } +#define VERIFY_LDAP_RESULT_ENTRY_OPEN(re) \ +{ \ + ldap_resultdata *ldap_entry_result = Z_LDAP_RESULT_P(&re->res); \ + VERIFY_LDAP_RESULT_OPEN(ldap_entry_result); \ +} + static bool php_ldap_is_numerically_indexed_array(zend_array *arr) { if (zend_hash_num_elements(arr) == 0 || HT_IS_PACKED(arr)) { @@ -1874,6 +1880,7 @@ PHP_FUNCTION(ldap_next_entry) VERIFY_LDAP_LINK_CONNECTED(ld); resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); + VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry); if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) { RETVAL_FALSE; @@ -2004,6 +2011,7 @@ PHP_FUNCTION(ldap_first_attribute) VERIFY_LDAP_LINK_CONNECTED(ld); resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); + VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry); if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) { RETURN_FALSE; @@ -2032,6 +2040,7 @@ PHP_FUNCTION(ldap_next_attribute) VERIFY_LDAP_LINK_CONNECTED(ld); resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); + VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry); if (resultentry->ber == NULL) { php_error_docref(NULL, E_WARNING, "Called before calling ldap_first_attribute() or no attributes found in result entry"); @@ -2075,6 +2084,7 @@ PHP_FUNCTION(ldap_get_attributes) VERIFY_LDAP_LINK_CONNECTED(ld); resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); + VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry); array_init(return_value); num_attrib = 0; @@ -2129,6 +2139,7 @@ PHP_FUNCTION(ldap_get_values_len) VERIFY_LDAP_LINK_CONNECTED(ld); resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); + VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry); if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr)) == NULL) { php_error_docref(NULL, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link))); @@ -2164,6 +2175,7 @@ PHP_FUNCTION(ldap_get_dn) VERIFY_LDAP_LINK_CONNECTED(ld); resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); + VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry); text = ldap_get_dn(ld->link, resultentry->data); if (text != NULL) { @@ -3590,6 +3602,7 @@ PHP_FUNCTION(ldap_next_reference) VERIFY_LDAP_LINK_CONNECTED(ld); resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); + VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry); if ((entry_next = ldap_next_reference(ld->link, resultentry->data)) == NULL) { RETVAL_FALSE; @@ -3620,6 +3633,7 @@ PHP_FUNCTION(ldap_parse_reference) VERIFY_LDAP_LINK_CONNECTED(ld); resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); + VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry); if (ldap_parse_reference(ld->link, resultentry->data, &lreferrals, NULL /* &serverctrls */, 0) != LDAP_SUCCESS) { RETURN_FALSE; diff --git a/ext/ldap/tests/ldap_free_result_entry_after_free.phpt b/ext/ldap/tests/ldap_free_result_entry_after_free.phpt new file mode 100644 index 000000000000..95e2e53dceb8 --- /dev/null +++ b/ext/ldap/tests/ldap_free_result_entry_after_free.phpt @@ -0,0 +1,48 @@ +--TEST-- +ldap_free_result() - entries of a freed result can no longer be used +--EXTENSIONS-- +ldap +--SKIPIF-- + +--FILE-- + ldap_get_dn($link, $entry), + fn() => ldap_get_attributes($link, $entry), + fn() => ldap_first_attribute($link, $entry), + fn() => ldap_next_attribute($link, $entry), + fn() => ldap_next_entry($link, $entry), + fn() => ldap_get_values_len($link, $entry, "cn"), +] as $fn) { + try { + $fn(); + } catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), "\n"; + } +} +?> +--CLEAN-- + +--EXPECTF-- +string(%d) "cn=userA,%s" +Error: LDAP result has already been closed +Error: LDAP result has already been closed +Error: LDAP result has already been closed +Error: LDAP result has already been closed +Error: LDAP result has already been closed +Error: LDAP result has already been closed