Fix invalid memory access and leaks in verify and decryptverify

This commit is contained in:
Jakub Zelenka 2026-11-22 14:04:45 +01:00
commit dd7b83b02d
No known key found for this signature in database
GPG key ID: 1C0779DC5C0A9DE4

20
gnupg.c
View file

@ -1530,7 +1530,8 @@ PHP_FUNCTION(gnupg_verify)
gpgme_data_t gpgme_text, gpgme_sig; gpgme_data_t gpgme_text, gpgme_sig;
gpgme_verify_result_t gpgme_result; gpgme_verify_result_t gpgme_result;
/* text without the signature, if its a detached one, or the text incl the sig */ /* text without the signature, if its a detached one, or the text incl the sig */
zval *signed_text = NULL; char *signed_text = NULL;
size_t signed_text_len;
/* signature, if its a detached one */ /* signature, if its a detached one */
zval *signature = NULL; zval *signature = NULL;
/* signed_text without the signature if its not a detached sig */ /* signed_text without the signature if its not a detached sig */
@ -1541,13 +1542,13 @@ PHP_FUNCTION(gnupg_verify)
GNUPG_GETOBJ(); GNUPG_GETOBJ();
if (this) { if (this) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz|z", if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz|z",
&signed_text, &signature, &plain_text) == FAILURE) { &signed_text, &signed_text_len, &signature, &plain_text) == FAILURE) {
return; return;
} }
} else { } else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ozz|z", if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osz|z",
&object, gnupg_class_entry, &signed_text, &signature, &plain_text) == FAILURE) { &object, gnupg_class_entry, &signed_text, &signed_text_len, &signature, &plain_text) == FAILURE) {
return; return;
} }
intern = Z_GNUPG_P(object); intern = Z_GNUPG_P(object);
@ -1561,10 +1562,9 @@ PHP_FUNCTION(gnupg_verify)
} }
/* and the text */ /* and the text */
if (!PHP_GNUPG_DO(gpgme_data_new_from_mem( if (!PHP_GNUPG_DO(gpgme_data_new_from_mem(
&gpgme_text, Z_STRVAL_P(signed_text), Z_STRLEN_P(signed_text), 0))) { &gpgme_text, signed_text, signed_text_len, 0))) {
GNUPG_ERR("could not create text-databuffer"); GNUPG_ERR("could not create text-databuffer");
gpgme_data_release(gpgme_sig); gpgme_data_release(gpgme_sig);
gpgme_data_release(gpgme_text);
return; return;
} }
/* now verify sig + text */ /* now verify sig + text */
@ -1576,7 +1576,7 @@ PHP_FUNCTION(gnupg_verify)
} }
} else { /* clearsign or normal signature */ } else { /* clearsign or normal signature */
if (!PHP_GNUPG_DO(gpgme_data_new_from_mem( if (!PHP_GNUPG_DO(gpgme_data_new_from_mem(
&gpgme_sig, Z_STRVAL_P(signed_text), Z_STRLEN_P(signed_text), 0))) { &gpgme_sig, signed_text, signed_text_len, 0))) {
GNUPG_ERR("could not create signature-databuffer"); GNUPG_ERR("could not create signature-databuffer");
return; return;
} }
@ -1584,7 +1584,6 @@ PHP_FUNCTION(gnupg_verify)
if (!PHP_GNUPG_DO(gpgme_data_new_from_mem(&gpgme_text, NULL, 0, 0))) { if (!PHP_GNUPG_DO(gpgme_data_new_from_mem(&gpgme_text, NULL, 0, 0))) {
GNUPG_ERR("could not create text-databuffer"); GNUPG_ERR("could not create text-databuffer");
gpgme_data_release(gpgme_sig); gpgme_data_release(gpgme_sig);
gpgme_data_release(gpgme_text);
return; return;
} }
/* and verify the 'signature' */ /* and verify the 'signature' */
@ -1599,6 +1598,7 @@ PHP_FUNCTION(gnupg_verify)
gpgme_result = gpgme_op_verify_result(intern->ctx); gpgme_result = gpgme_op_verify_result(intern->ctx);
if (!gpgme_result->signatures) { if (!gpgme_result->signatures) {
GNUPG_ERR("no signature found"); GNUPG_ERR("no signature found");
gpgme_data_release(gpgme_text);
} else { } else {
/* fetch all signatures in an array */ /* fetch all signatures in an array */
gnupg_fetchsignatures(gpgme_result->signatures, return_value); gnupg_fetchsignatures(gpgme_result->signatures, return_value);
@ -1606,6 +1606,7 @@ PHP_FUNCTION(gnupg_verify)
gpg_plain = gpgme_data_release_and_get_mem(gpgme_text, &gpg_plain_len); gpg_plain = gpgme_data_release_and_get_mem(gpgme_text, &gpg_plain_len);
if (gpg_plain && gpg_plain_len > 0 && plain_text) { if (gpg_plain && gpg_plain_len > 0 && plain_text) {
ZVAL_DEREF(plain_text); ZVAL_DEREF(plain_text);
zval_ptr_dtor(plain_text);
ZVAL_STRINGL(plain_text, gpg_plain, gpg_plain_len); ZVAL_STRINGL(plain_text, gpg_plain, gpg_plain_len);
} }
free(gpg_plain); free(gpg_plain);
@ -1731,6 +1732,7 @@ PHP_FUNCTION(gnupg_decryptverify)
return; return;
} }
userret = gpgme_data_release_and_get_mem(out, &ret_size); userret = gpgme_data_release_and_get_mem(out, &ret_size);
zval_ptr_dtor(plaintext);
ZVAL_STRINGL(plaintext, userret, ret_size); ZVAL_STRINGL(plaintext, userret, ret_size);
free(userret); free(userret);
decrypt_result = gpgme_op_decrypt_result(intern->ctx); decrypt_result = gpgme_op_decrypt_result(intern->ctx);