Skip to content
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

combined patch for the more or less obvious issues: #2127

Conversation

bernd-edlinger
Copy link
Member

@levitte levitte added the hold: cla required The contributor needs to submit a license agreement label Dec 21, 2016
@@ -186,7 +186,7 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
pseqval = asn1_get_field_ptr(pval, tt);
if (!ASN1_template_new(pseqval, tt))
goto memerr;
goto auxerr;
Copy link
Member

Choose a reason for hiding this comment

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

I understand why you made that choice. Unfortunately, that will give off the wrong error code, so what I'd do if I was you is to change this back and add a ASN1_item_ex_free(pval, it); in the memerr: code.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, and it's possible you need to check if pval != NULL in that case...

Copy link
Member Author

Choose a reason for hiding this comment

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

ok?

@bernd-edlinger
Copy link
Member Author

updated patch should keep return code as before,
and only clean up *pval if it is actually initialized.
Note that ASN1_OP_NEW_PRE does not always initialize
*pval if the return code is zero. see sig_cb in dsa/dsa_asn1.c

@bernd-edlinger
Copy link
Member Author

PS: CCLA was signed and sent to legal@opensslfoundation.org today.

@bernd-edlinger
Copy link
Member Author

I noticed there is a further memory leak in ex_data.c if lh_insert fails.
The issue is similar to what can happen in the SSL_SESSION cache.

@bernd-edlinger
Copy link
Member Author

oops, I doubt that the error in the AppVeyor build is due to my patch, so please ignore:

'ml64' is not recognized as an internal or external command,
operable program or batch file.
nmake /f ms%MAK%
'nmake' is not recognized as an internal or external command,
operable program or batch file.
Command exited with code 1

@bernd-edlinger
Copy link
Member Author

Thinking at it again,
I noticed that not calling the free_func may result in memory leaks,
so there needs to be a fallback strategy, if allocating "storage" in
int_free_ex_data fails.

In my testing I did not have any free_func != NULL,
probably because I was not using any engine.

But I see that dynamic_data_ctx_free_func is one example of
what might happen if the free_func is not called.

Furthermore, but I have not seen any examples of these
function pointers, the return codes of new_func and dup_func
seem to be ignored as well.

@bernd-edlinger
Copy link
Member Author

Oops, and the data flow in the new_func is reversed.
I really doubt that ever worked.

@kroeckx kroeckx added CLA OK and removed hold: cla required The contributor needs to submit a license agreement labels Dec 31, 2016
@richsalz richsalz removed the CLA OK label Jan 28, 2017
crypto/ex_data.c Outdated
for (i = 0; i < mx; i++) {
if (storage[i] && storage[i]->free_func) {
if (storage)
Copy link
Member

Choose a reason for hiding this comment

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

if (storage != NULL)

crypto/ex_data.c Outdated
f = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
}
if (f && f->free_func) {
Copy link
Member

Choose a reason for hiding this comment

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

if (f != NULL && f->free_func != NULL) {

crypto/ex_data.c Outdated
ptr = CRYPTO_get_ex_data(ad, i);
storage[i]->free_func(obj, ptr, ad, i,
storage[i]->argl, storage[i]->argp);
f->free_func(obj, ptr, ad, i, f->argl, f->argp);
}
}
if (storage)
OPENSSL_free(storage);
Copy link
Member

Choose a reason for hiding this comment

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

Remove the condition, as NULL is valid input for OPENSSL_free()

crypto/ex_data.c Outdated
}
}
if (storage)
OPENSSL_free(storage);
err:
if (ad->sk) {
sk_void_free(ad->sk);
Copy link
Member

Choose a reason for hiding this comment

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

Remove condition, as NULL is valid input for sk_void_free()

if (!HMAC_CTX_copy(&dctx->ctx, &sctx->ctx))
return 0;
goto err;
if (sctx->ktmp.data) {
Copy link
Member

Choose a reason for hiding this comment

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

While you're at it, you could change this to if (sctx->ktmp.data != NULL) {

MD_Update(&m, local_md, MD_DIGEST_LENGTH);
if (!MD_Init(&m) ||
!MD_Update(&m, local_md, MD_DIGEST_LENGTH))
goto err;
Copy link
Member

Choose a reason for hiding this comment

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

Indent by one more space

MD_Update(&m, &(state[st_idx]), MD_DIGEST_LENGTH / 2);
MD_Final(&m, local_md);
if (!MD_Update(&m, &(state[st_idx]), MD_DIGEST_LENGTH / 2))
goto err;
Copy link
Member

Choose a reason for hiding this comment

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

Since you're making changes here anyway, it would be nice if you put curlies around the else part

ssl/s3_pkt.c Outdated
@@ -699,6 +699,7 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
len >= 4 * (int)(max_send_fragment = s->max_send_fragment) &&
s->compress == NULL && s->msg_callback == NULL &&
SSL_USE_EXPLICIT_IV(s) &&
s->enc_write_ctx &&
Copy link
Member

Choose a reason for hiding this comment

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

s->enc_write_ctx != NULL &&

@levitte
Copy link
Member

levitte commented Feb 1, 2017

I'm a little confused by this PR. Is it only for 1.0.2?

Fixed a memory leak in ASN1_digest and ASN1_item_digest.

asn1_template_noexp_d2i call ASN1_item_ex_free(&skfield,...) on error.

Reworked error handling in asn1_item_ex_combine_new:
- call ASN1_item_ex_free and return the correct error code if ASN1_template_new failed.
- dont call ASN1_item_ex_free if ASN1_OP_NEW_PRE failed.

Reworked error handing in x509_name_ex_d2i and x509_name_encode.

Fixed error handling in int_ctx_new and EVP_PKEY_CTX_dup.

Fixed a memory leak in def_get_class if lh_EX_CLASS_ITEM_insert fails due to OOM:
- to figure out if the insertion succeeded, use lh_EX_CLASS_ITEM_retrieve again.
- on error, p will be NULL, and gen needs to be cleaned up again.

int_free_ex_data needs to have a fallback solution if unable to allocate "storage":
- if free_func is non-zero this must be called to clean up all memory.

Fixed error handling in pkey_hmac_copy.

Fixed error handling in ssleay_rand_add and ssleay_rand_bytes.

Fixed error handling in X509_STORE_new.

Fixed a memory leak in ssl3_get_key_exchange.

Check for null pointer in ssl3_write_bytes.

Check for null pointer in ssl3_get_cert_verify.

Fixed a memory leak in ssl_cert_dup.

Fixes openssl#2087 openssl#2094 openssl#2103 openssl#2104 openssl#2105 openssl#2106 openssl#2107 openssl#2108 openssl#2110 openssl#2111 openssl#2112 openssl#2115
@bernd-edlinger
Copy link
Member Author

Yes, this PR is for 1.0.2 only,
I have another pull request for 1.1.0.

@levitte levitte added the branch: 1.0.2 Merge to OpenSSL_1_0_2-stable branch label Feb 1, 2017
@levitte
Copy link
Member

levitte commented Feb 1, 2017

Okie. This looks good to me, I just want to see the build results (Travis seems to have ground to a halt for the moment)

@richsalz
Copy link
Contributor

richsalz commented Feb 4, 2017

ping @levitte to reconfirm.

@richsalz richsalz closed this Feb 4, 2017
@bernd-edlinger
Copy link
Member Author

@richsalz I think you accidentally closed this PR, I cannot re-open this PR, is that a problem,
can you still do the merge, or should I open a new PR for 1.0.2?

@richsalz richsalz reopened this Feb 4, 2017
@levitte levitte added the approval: done This pull request has the required number of approvals label Feb 6, 2017
levitte pushed a commit that referenced this pull request Feb 6, 2017
Fixed a memory leak in ASN1_digest and ASN1_item_digest.

asn1_template_noexp_d2i call ASN1_item_ex_free(&skfield,...) on error.

Reworked error handling in asn1_item_ex_combine_new:
- call ASN1_item_ex_free and return the correct error code if ASN1_template_new failed.
- dont call ASN1_item_ex_free if ASN1_OP_NEW_PRE failed.

Reworked error handing in x509_name_ex_d2i and x509_name_encode.

Fixed error handling in int_ctx_new and EVP_PKEY_CTX_dup.

Fixed a memory leak in def_get_class if lh_EX_CLASS_ITEM_insert fails due to OOM:
- to figure out if the insertion succeeded, use lh_EX_CLASS_ITEM_retrieve again.
- on error, p will be NULL, and gen needs to be cleaned up again.

int_free_ex_data needs to have a fallback solution if unable to allocate "storage":
- if free_func is non-zero this must be called to clean up all memory.

Fixed error handling in pkey_hmac_copy.

Fixed error handling in ssleay_rand_add and ssleay_rand_bytes.

Fixed error handling in X509_STORE_new.

Fixed a memory leak in ssl3_get_key_exchange.

Check for null pointer in ssl3_write_bytes.

Check for null pointer in ssl3_get_cert_verify.

Fixed a memory leak in ssl_cert_dup.

Fixes #2087 #2094 #2103 #2104 #2105 #2106 #2107 #2108 #2110 #2111 #2112 #2115

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from #2127)
@richsalz
Copy link
Contributor

richsalz commented Feb 6, 2017

748cb9a on 1.0.2 Is there a version for 1.1.0/master still pending? Or should I just close all those issues?
Thanks!

@richsalz richsalz closed this Feb 6, 2017
@bernd-edlinger
Copy link
Member Author

All issues are fixed on all branches. Please go ahead and close all issues. Thanks!

@richsalz
Copy link
Contributor

richsalz commented Feb 7, 2017

@bernd-edlinger thank you very much for all your work on improving OpenSSL!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approval: done This pull request has the required number of approvals branch: 1.0.2 Merge to OpenSSL_1_0_2-stable branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants