Skip to content

Commit a6d111f

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: NEWS entries for LDAP bug fixes ext/ldap: Fix GH-16136 (Memory leak in php_ldap_do_modify()) ext/ldap: Fix GH-16132 (Freeing pointer not allocated by ZMM)
2 parents 98a4c53 + 747860c commit a6d111f

File tree

4 files changed

+95
-11
lines changed

4 files changed

+95
-11
lines changed

ext/ldap/ldap.c

+9-11
Original file line numberDiff line numberDiff line change
@@ -2236,17 +2236,11 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22362236
ldap_mods[i]->mod_type = estrndup(ZSTR_VAL(attribute), ZSTR_LEN(attribute));
22372237
} else {
22382238
php_error_docref(NULL, E_WARNING, "Unknown attribute in the data");
2239-
/* Free allocated memory */
2240-
while (i >= 0) {
2241-
if (ldap_mods[i]->mod_type) {
2242-
efree(ldap_mods[i]->mod_type);
2243-
}
2244-
efree(ldap_mods[i]);
2245-
i--;
2246-
}
2247-
efree(num_berval);
2248-
efree(ldap_mods);
2249-
RETURN_FALSE;
2239+
RETVAL_FALSE;
2240+
num_berval[i] = 0;
2241+
num_attribs = i + 1;
2242+
ldap_mods[i]->mod_bvalues = NULL;
2243+
goto cleanup;
22502244
}
22512245

22522246
value = zend_hash_get_current_data(Z_ARRVAL_P(entry));
@@ -2267,6 +2261,8 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22672261
convert_to_string(value);
22682262
if (EG(exception)) {
22692263
RETVAL_FALSE;
2264+
num_berval[i] = 0;
2265+
num_attribs = i + 1;
22702266
goto cleanup;
22712267
}
22722268
ldap_mods[i]->mod_bvalues[0] = (struct berval *) emalloc (sizeof(struct berval));
@@ -2283,6 +2279,8 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22832279
}
22842280
convert_to_string(ivalue);
22852281
if (EG(exception)) {
2282+
num_berval[i] = j;
2283+
num_attribs = i + 1;
22862284
RETVAL_FALSE;
22872285
goto cleanup;
22882286
}

ext/ldap/tests/gh16132-1.phpt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Bug GH-16132: Attempting to free pointer not allocated by ZMM
3+
--EXTENSIONS--
4+
ldap
5+
--FILE--
6+
<?php
7+
8+
/* ldap_add(_ext)(), ldap_mod_replace(_ext)(), ldap_mod_add(_ext)(), and ldap_mod_del(_ext)() share an underlying C function */
9+
/* We are assuming 3333 is not connectable */
10+
$ldap = ldap_connect('ldap://127.0.0.1:3333');
11+
$valid_dn = "cn=userA,something";
12+
13+
$dict_key_value_not_string = [
14+
'attribute1' => new stdClass(),
15+
'attribute2' => [
16+
'value1',
17+
'value2',
18+
],
19+
];
20+
try {
21+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_value_not_string));
22+
} catch (Throwable $e) {
23+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
24+
}
25+
26+
?>
27+
--EXPECT--
28+
Error: Object of class stdClass could not be converted to string

ext/ldap/tests/gh16132-2.phpt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Bug GH-16132: Attempting to free pointer not allocated by ZMM
3+
--EXTENSIONS--
4+
ldap
5+
--FILE--
6+
<?php
7+
8+
/* ldap_add(_ext)(), ldap_mod_replace(_ext)(), ldap_mod_add(_ext)(), and ldap_mod_del(_ext)() share an underlying C function */
9+
/* We are assuming 3333 is not connectable */
10+
$ldap = ldap_connect('ldap://127.0.0.1:3333');
11+
$valid_dn = "cn=userA,something";
12+
13+
$dict_key_multi_value_not_list_of_strings2 = [
14+
'attribute1' => 'value',
15+
'attribute2' => [
16+
'value1',
17+
new stdClass(),
18+
],
19+
];
20+
try {
21+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_multi_value_not_list_of_strings2));
22+
} catch (Throwable $e) {
23+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
24+
}
25+
26+
?>
27+
--EXPECT--
28+
Error: Object of class stdClass could not be converted to string

ext/ldap/tests/gh16136.phpt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug GH-16136: Memory leak in php_ldap_do_modify() when entry is not a proper dictionary
3+
--EXTENSIONS--
4+
ldap
5+
--FILE--
6+
<?php
7+
8+
/* ldap_add(_ext)(), ldap_mod_replace(_ext)(), ldap_mod_add(_ext)(), and ldap_mod_del(_ext)() share an underlying C function */
9+
/* We are assuming 3333 is not connectable */
10+
$ldap = ldap_connect('ldap://127.0.0.1:3333');
11+
$valid_dn = "cn=userA,something";
12+
13+
$not_dict_of_attributes = [
14+
'attribute1' => 'value',
15+
'not_key_entry',
16+
'attribute3' => [
17+
'value1',
18+
'value2',
19+
],
20+
];
21+
try {
22+
var_dump(ldap_add($ldap, $valid_dn, $not_dict_of_attributes));
23+
} catch (Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
25+
}
26+
27+
?>
28+
--EXPECTF--
29+
Warning: ldap_add(): Unknown attribute in the data in %s on line %d
30+
bool(false)

0 commit comments

Comments
 (0)