Skip to content

Stop raising ERR_R_MALLOC_FAILURE in most places#19301

Closed
levitte wants to merge 10 commits intoopenssl:masterfrom
levitte:alt-19072
Closed

Stop raising ERR_R_MALLOC_FAILURE in most places#19301
levitte wants to merge 10 commits intoopenssl:masterfrom
levitte:alt-19072

Conversation

@levitte
Copy link
Member

@levitte levitte commented Sep 29, 2022

Since OPENSSL_malloc() and friends report ERR_R_MALLOC_FAILURE, and at least handle the file name and line number they are called from, there's no need to report ERR_R_MALLOC_FAILURE where they are called directly, or when SSLfatal() and RLAYERfatal() is used, the reason ERR_R_MALLOC_FAILURE is changed to ERR_R_CRYPTO_LIB.

There were a number of places where ERR_R_MALLOC_FAILURE was reported even though it was a function from a different sub-system that was called. Those places are changed to report ERR_R_{lib}_LIB, where {lib} is the name of that sub-system.
Some of them are tricky to get right, as we have a lot of functions that belong in the ASN1 sub-system, and all the sk_ calls or from the CRYPTO sub-system.

Some extra adaptation was necessary where there were custom OPENSSL_malloc() wrappers, and some bugs are fixed alongside these changes.
Most notably, CRYPTO_secure_malloc() has been changed to also raise ERR_R_MALLOC_FAILURE when appropriate.

This is an alternative to #19072

In other words, make it raise ERR_R_MALLOC_FAILURE appropriately.
Since OPENSSL_malloc() and friends report ERR_R_MALLOC_FAILURE, and
at least handle the file name and line number they are called from,
there's no need to report ERR_R_MALLOC_FAILURE where they are called
directly, or when SSLfatal() and RLAYERfatal() is used, the reason
`ERR_R_MALLOC_FAILURE` is changed to `ERR_R_CRYPTO_LIB`.

There were a number of places where `ERR_R_MALLOC_FAILURE` was reported
even though it was a function from a different sub-system that was
called.  Those places are changed to report ERR_R_{lib}_LIB, where
{lib} is the name of that sub-system.
Some of them are tricky to get right, as we have a lot of functions
that belong in the ASN1 sub-system, and all the `sk_` calls or from
the CRYPTO sub-system.

Some extra adaptation was necessary where there were custom OPENSSL_malloc()
wrappers, and some bugs are fixed alongside these changes.
@levitte
Copy link
Member Author

levitte commented Sep 29, 2022

This is in draft for a bit, I need to re-read what I've done after a pause.
Feel free to look, comment, or even review.

@github-actions github-actions bot added the severity: fips change The pull request changes FIPS provider sources label Sep 29, 2022
@DDvO
Copy link
Contributor

DDvO commented Sep 29, 2022

This is an alternative to #19072

This is clearly more advanced than #19072.
Yet note that that PR also has commit 6a6e27b which replaces all remnant ECerr() and EVPerr() calls in crypto/ by ERR_raise().
This is not (yet) covered here - you might simply cherry-pick that commit.

@levitte
Copy link
Member Author

levitte commented Sep 29, 2022

Yet note that that PR also has commit 6a6e27b which replaces all remnant ECerr() and EVPerr() calls in crypto/ by ERR_raise().

That could actually be made into a completely separate fix commit. It's quite independent from all the rest

@DDvO
Copy link
Contributor

DDvO commented Sep 29, 2022

That could actually be made into a completely separate fix commit. It's quite independent from all the rest

I just did this in #19302, and closed #19072 in favor of the two new PRs.

Engines lacked the possibility to refer to themselves in this form:

    WHATEVERerr(WHATEVER_F_SOMETHING, WHATEVER_R_WHATEVER_LIB);

This little change makes that possible, and gets used in e_capi.
@t8m t8m added the triaged: bug The issue/pr is/fixes a bug label Sep 29, 2022
@levitte levitte marked this pull request as ready for review September 29, 2022 13:53
@levitte levitte marked this pull request as draft September 29, 2022 13:53
Copy link
Member

@t8m t8m left a comment

Choose a reason for hiding this comment

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

I've found just two mistakes in this gargantuan PR. How did you do it, even with some scripting, I cannot imagine. Great work!

@levitte
Copy link
Member Author

levitte commented Sep 29, 2022

How did you do it, even with some scripting, I cannot imagine.

Grep, and eyeballing

@levitte
Copy link
Member Author

levitte commented Sep 29, 2022

I'm re-reading my changes, and clearly got blurry-eyed in some sections. Corrections coming up!

@beldmit
Copy link
Member

beldmit commented Sep 29, 2022

@levitte it looks impossible to review all the affected files.
I'd suggest splitting them at least on commit per-directory basis if you want to fixup it.

@levitte
Copy link
Member Author

levitte commented Sep 29, 2022

@levitte it looks impossible to review all the affected files. I'd suggest splitting them at least on commit per-directory basis if you want to fixup it.

... I think that'll be something for tomorrow, although I'm hoping that someone has the stamina 😉

@t8m
Copy link
Member

t8m commented Oct 1, 2022

Should the draft status be removed? IMO this is good to go.

@levitte levitte marked this pull request as ready for review October 1, 2022 19:38
@levitte
Copy link
Member Author

levitte commented Oct 3, 2022

Second review, @openssl/committers ?

@hlandau hlandau added the approval: done This pull request has the required number of approvals label Oct 4, 2022
@openssl-machine openssl-machine removed the approval: done This pull request has the required number of approvals label Oct 5, 2022
@openssl-machine
Copy link
Collaborator

This pull request is ready to merge

@openssl-machine openssl-machine added the approval: ready to merge The 24 hour grace period has passed, ready to merge label Oct 5, 2022
@levitte
Copy link
Member Author

levitte commented Oct 5, 2022

Merged

9167a47 Adapt CRYPTO_secure_malloc() like CRYPTO_malloc()
e077455 Stop raising ERR_R_MALLOC_FAILURE in most places
79c8dcf Add {lib}R{lib}_LIB, for our engines and other "external" modules

@levitte levitte closed this Oct 5, 2022
@levitte levitte deleted the alt-19072 branch October 5, 2022 12:05
openssl-machine pushed a commit that referenced this pull request Oct 5, 2022
In other words, make it raise ERR_R_MALLOC_FAILURE appropriately.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from #19301)
openssl-machine pushed a commit that referenced this pull request Oct 5, 2022
Since OPENSSL_malloc() and friends report ERR_R_MALLOC_FAILURE, and
at least handle the file name and line number they are called from,
there's no need to report ERR_R_MALLOC_FAILURE where they are called
directly, or when SSLfatal() and RLAYERfatal() is used, the reason
`ERR_R_MALLOC_FAILURE` is changed to `ERR_R_CRYPTO_LIB`.

There were a number of places where `ERR_R_MALLOC_FAILURE` was reported
even though it was a function from a different sub-system that was
called.  Those places are changed to report ERR_R_{lib}_LIB, where
{lib} is the name of that sub-system.
Some of them are tricky to get right, as we have a lot of functions
that belong in the ASN1 sub-system, and all the `sk_` calls or from
the CRYPTO sub-system.

Some extra adaptation was necessary where there were custom OPENSSL_malloc()
wrappers, and some bugs are fixed alongside these changes.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from #19301)
openssl-machine pushed a commit that referenced this pull request Oct 5, 2022
Engines lacked the possibility to refer to themselves in this form:

    WHATEVERerr(WHATEVER_F_SOMETHING, WHATEVER_R_WHATEVER_LIB);

This little change makes that possible, and gets used in e_capi.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from #19301)
@openssl openssl deleted a comment Oct 8, 2022
beldmit pushed a commit to beldmit/openssl that referenced this pull request Dec 26, 2022
In other words, make it raise ERR_R_MALLOC_FAILURE appropriately.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from openssl#19301)
beldmit pushed a commit to beldmit/openssl that referenced this pull request Dec 26, 2022
Since OPENSSL_malloc() and friends report ERR_R_MALLOC_FAILURE, and
at least handle the file name and line number they are called from,
there's no need to report ERR_R_MALLOC_FAILURE where they are called
directly, or when SSLfatal() and RLAYERfatal() is used, the reason
`ERR_R_MALLOC_FAILURE` is changed to `ERR_R_CRYPTO_LIB`.

There were a number of places where `ERR_R_MALLOC_FAILURE` was reported
even though it was a function from a different sub-system that was
called.  Those places are changed to report ERR_R_{lib}_LIB, where
{lib} is the name of that sub-system.
Some of them are tricky to get right, as we have a lot of functions
that belong in the ASN1 sub-system, and all the `sk_` calls or from
the CRYPTO sub-system.

Some extra adaptation was necessary where there were custom OPENSSL_malloc()
wrappers, and some bugs are fixed alongside these changes.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from openssl#19301)
beldmit pushed a commit to beldmit/openssl that referenced this pull request Dec 26, 2022
Engines lacked the possibility to refer to themselves in this form:

    WHATEVERerr(WHATEVER_F_SOMETHING, WHATEVER_R_WHATEVER_LIB);

This little change makes that possible, and gets used in e_capi.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from openssl#19301)
@bubble932
Copy link

@levitte I am curious about the differences in error handling between EVP_KDF_CTX_new() and EVP_MAC_CTX_new(). When OPENSSL_zalloc() fails, EVP_KDF_CTX_new() will raises ERR_R_EVP_LIB, whereas EVP_MAC_CTX_new() does not. Instead, EVP_MAC_CTX_new() only raises ERR_R_EVP_LIB when newctx() fails. This makes EVP_MAC_CTX_new() seem more reasonable in its error handling. Are there any other concerns I haven't noticed? Please enlighten me on this. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Applies to master branch severity: fips change The pull request changes FIPS provider sources triaged: bug The issue/pr is/fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants