Skip to content

Commit 747860c

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: 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 bf1021c + 10b5d4b commit 747860c

File tree

5 files changed

+99
-11
lines changed

5 files changed

+99
-11
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ PHP NEWS
3535
ldap_modify_batch()). (Girgias)
3636
. Fixed bug GH-16101 (Segfault in ldap_list(), ldap_read(), and ldap_search()
3737
when LDAPs array is not a list). (Girgias)
38+
. Fix GH-16132 (php_ldap_do_modify() attempts to free pointer not allocated
39+
by ZMM.). (Girgias)
40+
. Fix GH-16136 (Memory leak in php_ldap_do_modify() when entry is not a
41+
proper dictionary). (Girgias)
3842

3943
- Opcache:
4044
. Fixed bug GH-16009 (Segmentation fault with frameless functions and

ext/ldap/ldap.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,17 +2226,11 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22262226
ldap_mods[i]->mod_type = estrndup(ZSTR_VAL(attribute), ZSTR_LEN(attribute));
22272227
} else {
22282228
php_error_docref(NULL, E_WARNING, "Unknown attribute in the data");
2229-
/* Free allocated memory */
2230-
while (i >= 0) {
2231-
if (ldap_mods[i]->mod_type) {
2232-
efree(ldap_mods[i]->mod_type);
2233-
}
2234-
efree(ldap_mods[i]);
2235-
i--;
2236-
}
2237-
efree(num_berval);
2238-
efree(ldap_mods);
2239-
RETURN_FALSE;
2229+
RETVAL_FALSE;
2230+
num_berval[i] = 0;
2231+
num_attribs = i + 1;
2232+
ldap_mods[i]->mod_bvalues = NULL;
2233+
goto cleanup;
22402234
}
22412235

22422236
value = zend_hash_get_current_data(Z_ARRVAL_P(entry));
@@ -2257,6 +2251,8 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22572251
convert_to_string(value);
22582252
if (EG(exception)) {
22592253
RETVAL_FALSE;
2254+
num_berval[i] = 0;
2255+
num_attribs = i + 1;
22602256
goto cleanup;
22612257
}
22622258
ldap_mods[i]->mod_bvalues[0] = (struct berval *) emalloc (sizeof(struct berval));
@@ -2273,6 +2269,8 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22732269
}
22742270
convert_to_string(ivalue);
22752271
if (EG(exception)) {
2272+
num_berval[i] = j;
2273+
num_attribs = i + 1;
22762274
RETVAL_FALSE;
22772275
goto cleanup;
22782276
}

ext/ldap/tests/gh16132-1.phpt

Lines changed: 28 additions & 0 deletions
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

Lines changed: 28 additions & 0 deletions
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

Lines changed: 30 additions & 0 deletions
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)