Skip to content

Commit 42c0dd5

Browse files
committed
fixing ldap memory leaks php#2
1 parent bb4e66c commit 42c0dd5

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

ext/ldap/ldap.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,17 @@ static void _php_ldap_control_to_array(LDAP *ld, LDAPControl* ctrl, zval* array,
166166
}
167167
} else if (strcmp(ctrl->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0) {
168168
int lestimated, rc;
169-
struct berval lcookie;
169+
struct berval lcookie = { 0, NULL };
170170
zval value;
171171

172172
if (ctrl->ldctl_value.bv_len) {
173-
rc = ldap_parse_pageresponse_control(ld, ctrl, &lestimated, &lcookie);
173+
/* ldap_parse_pageresponse_control() allocates lcookie.bv_val */
174+
rc = ldap_parse_pageresponse_control(ld, ctrl, &lestimated, &lcookie); /* memleak: ??? */
174175
} else {
175176
/* ldap_parse_pageresponse_control will crash if value is empty */
176177
rc = -1;
177178
}
179+
178180
if ( rc == LDAP_SUCCESS ) {
179181
array_init(&value);
180182
add_assoc_long(&value, "size", lestimated);
@@ -183,6 +185,10 @@ static void _php_ldap_control_to_array(LDAP *ld, LDAPControl* ctrl, zval* array,
183185
} else {
184186
add_assoc_null(array, "value");
185187
}
188+
189+
if (lcookie.bv_val) {
190+
ldap_memfree(lcookie.bv_val);
191+
}
186192
} else if ((strcmp(ctrl->ldctl_oid, LDAP_CONTROL_PRE_READ) == 0) || (strcmp(ctrl->ldctl_oid, LDAP_CONTROL_POST_READ) == 0)) {
187193
BerElement *ber;
188194
struct berval bv;
@@ -1630,7 +1636,7 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
16301636
php_set_opts(ld->link, ldap_sizelimit, ldap_timelimit, ldap_deref, &old_ldap_sizelimit, &old_ldap_timelimit, &old_ldap_deref);
16311637

16321638
/* Run the actual search */
1633-
errno = ldap_search_ext_s(ld->link, ZSTR_VAL(ldap_base_dn), scope, ZSTR_VAL(ldap_filter), ldap_attrs, ldap_attrsonly, lserverctrls, NULL, NULL, ldap_sizelimit, &ldap_res);
1639+
errno = ldap_search_ext_s(ld->link, ZSTR_VAL(ldap_base_dn), scope, ZSTR_VAL(ldap_filter), ldap_attrs, ldap_attrsonly, lserverctrls, NULL, NULL, ldap_sizelimit, &ldap_res); /* memleak: !!! */
16341640

16351641
if (errno != LDAP_SUCCESS
16361642
&& errno != LDAP_SIZELIMIT_EXCEEDED
@@ -4254,8 +4260,7 @@ PHP_FUNCTION(ldap_exop_passwd)
42544260
{
42554261
zval *link, *serverctrls;
42564262
struct berval luser, loldpw, lnewpw, lgenpasswd;
4257-
LDAPControl **lserverctrls = NULL, **requestctrls = NULL;
4258-
LDAPControl *ctrl, **ctrlp;
4263+
LDAPControl *ctrl, **lserverctrls = NULL, *requestctrls[2] = { NULL, NULL };
42594264
LDAPMessage* ldap_res;
42604265
ldap_linkdata *ld;
42614266
int rc, myargcount = ZEND_NUM_ARGS(), msgid, err;
@@ -4275,16 +4280,10 @@ PHP_FUNCTION(ldap_exop_passwd)
42754280

42764281
switch (myargcount) {
42774282
case 5:
4278-
requestctrls = safe_emalloc(2, sizeof(*requestctrls), 0);
4279-
*requestctrls = NULL;
4280-
ctrlp = requestctrls;
4281-
4282-
if (ldap_create_passwordpolicy_control(ld->link, &ctrl) == LDAP_SUCCESS) {
4283-
*ctrlp = ctrl;
4284-
++ctrlp;
4283+
/* ldap_create_passwordpolicy_control() allocates ctrl */
4284+
if (ldap_create_passwordpolicy_control(ld->link, &ctrl) == LDAP_SUCCESS) { /* memleak: ??? */
4285+
requestctrls[0] = ctrl;
42854286
}
4286-
4287-
*ctrlp = NULL;
42884287
}
42894288

42904289
/* asynchronous call to get result and controls */
@@ -4294,8 +4293,8 @@ PHP_FUNCTION(ldap_exop_passwd)
42944293
requestctrls,
42954294
NULL, &msgid);
42964295

4297-
if (requestctrls != NULL) {
4298-
efree(requestctrls);
4296+
if (requestctrls[0] != NULL) {
4297+
ldap_control_free(requestctrls[0]);
42994298
}
43004299

43014300
if (rc != LDAP_SUCCESS ) {
@@ -4317,9 +4316,12 @@ PHP_FUNCTION(ldap_exop_passwd)
43174316
RETURN_FALSE;
43184317
}
43194318

4320-
rc = ldap_parse_result(ld->link, ldap_res, &err, NULL, &errmsg, NULL, (myargcount > 4 ? &lserverctrls : NULL), 1);
4319+
rc = ldap_parse_result(ld->link, ldap_res, &err, NULL, &errmsg, NULL, (myargcount > 4 ? &lserverctrls : NULL), 1); /* memleak: ??? */
43214320
if( rc != LDAP_SUCCESS ) {
43224321
php_error_docref(NULL, E_WARNING, "Passwd modify extended operation failed: %s (%d)", ldap_err2string(rc), rc);
4322+
if (lserverctrls) {
4323+
ldap_controls_free(lserverctrls);
4324+
}
43234325
RETURN_FALSE;
43244326
}
43254327

0 commit comments

Comments
 (0)