Skip to content

ext/ldap: Fix GH-16132 (Freeing pointer not allocated by ZMM) #16134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Sep 30, 2024

No description provided.

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for both memleak and crash seem good. I have one suggestion though to avoid code duplication.

ext/ldap/ldap.c Outdated
@@ -2154,6 +2154,11 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
while (i >= 0) {
if (ldap_mods[i]->mod_type) {
efree(ldap_mods[i]->mod_type);
/* Free attribute values */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would replace the entire "Free allocated memory" block with just:

			RETVAL_FALSE;
			num_berval[i] = 0;
			num_attribs = i + 1;
			ldap_mods[i]->mod_bvalues = NULL;
			goto cleanup;

Sadly I can't mark it as a suggestion

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried doing that but I think my issues was not setting mod_bvalues to NULL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for me:

diff --git a/ext/ldap/ldap.c b/ext/ldap/ldap.c
index d40bf550260..82d4975a769 100644
--- a/ext/ldap/ldap.c
+++ b/ext/ldap/ldap.c
@@ -2236,22 +2236,11 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
 			ldap_mods[i]->mod_type = estrndup(ZSTR_VAL(attribute), ZSTR_LEN(attribute));
 		} else {
 			php_error_docref(NULL, E_WARNING, "Unknown attribute in the data");
-			/* Free allocated memory */
-			while (i >= 0) {
-				if (ldap_mods[i]->mod_type) {
-					efree(ldap_mods[i]->mod_type);
-					/* Free attribute values */
-					for (j = 0; j < num_berval[i]; j++) {
-						efree(ldap_mods[i]->mod_bvalues[j]);
-					}
-					efree(ldap_mods[i]->mod_bvalues);
-				}
-				efree(ldap_mods[i]);
-				i--;
-			}
-			efree(num_berval);
-			efree(ldap_mods);
-			RETURN_FALSE;
+			RETVAL_FALSE;
+			num_berval[i] = 0;
+			num_attribs = i + 1;
+			ldap_mods[i]->mod_bvalues = NULL;
+			goto cleanup;
 		}
 
 		value = zend_hash_get_current_data(Z_ARRVAL_P(entry));

@Girgias Girgias force-pushed the ldap-8.2-ldap_do_modify branch from 3435d5a to 79939fe Compare September 30, 2024 19:56
@Girgias Girgias force-pushed the ldap-8.2-ldap_do_modify branch from 79939fe to 61d5a76 Compare September 30, 2024 20:08
Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine
ASAN job failure is because of the JSON stack limit test I added, which works for some reason on 8.3 but not on higher versions...

@Girgias Girgias closed this in c910e78 Sep 30, 2024
@Girgias Girgias deleted the ldap-8.2-ldap_do_modify branch September 30, 2024 21:25
jorgsowa pushed a commit to jorgsowa/php-src that referenced this pull request Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants