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

Fix engine cleanup error handling #21971

Conversation

bernd-edlinger
Copy link
Member

Error handling in engine_cleanup_add_first/last was broken and caused memory leaks.

Error handling in engine_cleanup_add_first/last was
broken and caused memory leaks.
@bernd-edlinger bernd-edlinger added branch: master Merge to master branch branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 labels Sep 5, 2023
@t8m t8m added approval: review pending This pull request needs review by a committer approval: otc review pending This pull request needs review by an OTC member triaged: bug The issue/pr is/fixes a bug labels Sep 5, 2023
@t8m
Copy link
Member

t8m commented Sep 5, 2023

Would it be possible to make test for this?

@bernd-edlinger
Copy link
Member Author

Would it be possible to make test for this?

The test that found it, is already there.

I did it using ./config enable-crypto-mdebug enable-crypto-mdebug-backtrace
together with OPENSSL_MALLOC_FAILURES=1@1 make test V=1
and spotted it, by looking at the keyword "bytes leaked",
ignoring lots of irrelevant findings outside the crypto tree.

Unfortunately the feature crypto/mem_dbg.c was removed in 3.0,
but address sanitizer cannot replace that functionality completely,
so enable-crypto-mdebug is crippled and enable-crypto-mdebug-backtrace
is currently useless.
Therefore my testing had to be done against the 1.1.1 branch.

@t8m t8m added tests: exempted The PR is exempt from requirements for testing and removed approval: otc review pending This pull request needs review by an OTC member labels Sep 6, 2023
@bernd-edlinger
Copy link
Member Author

@openssl/committers ping...

@paulidale paulidale added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Sep 14, 2023
@openssl-machine openssl-machine added approval: ready to merge The 24 hour grace period has passed, ready to merge and removed approval: done This pull request has the required number of approvals labels Sep 15, 2023
@openssl-machine
Copy link
Collaborator

This pull request is ready to merge

openssl-machine pushed a commit that referenced this pull request Sep 15, 2023
Error handling in engine_cleanup_add_first/last was
broken and caused memory leaks.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #21971)
openssl-machine pushed a commit that referenced this pull request Sep 15, 2023
Error handling in engine_cleanup_add_first/last was
broken and caused memory leaks.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #21971)

(cherry picked from commit 00f2efc)
openssl-machine pushed a commit that referenced this pull request Sep 15, 2023
Error handling in engine_cleanup_add_first/last was
broken and caused memory leaks.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #21971)

(cherry picked from commit 00f2efc)
@bernd-edlinger
Copy link
Member Author

Merged to all branches. Thanks!

bernd-edlinger added a commit to bernd-edlinger/openssl that referenced this pull request Sep 15, 2023
Error handling in engine_cleanup_add_first/last was
broken and caused memory leaks.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from openssl#21971)

(cherry picked from commit 00f2efc)
@tom-cosgrove-arm
Copy link
Contributor

The merge to 3.0 has broken the 3.0 branch

crypto/engine/eng_list.c:85:49: error: too few arguments provided to function-like macro invocation
            CRYPTO_DOWN_REF(&e->struct_ref, &ref);
                                                ^
include/internal/refcount.h:162:10: note: macro 'CRYPTO_DOWN_REF' defined here
# define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock)
         ^

as the declaration of ref was not brought in

diff --git a/crypto/engine/eng_list.c b/crypto/engine/eng_list.c
index 04c73c7628..d3bd0c0dc2 100644
--- a/crypto/engine/eng_list.c
+++ b/crypto/engine/eng_list.c
@@ -78,12 +78,16 @@ static int engine_list_add(ENGINE *e)
             ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
             return 0;
         }
-        engine_list_head = e;
-        e->prev = NULL;
         /*
          * The first time the list allocates, we should register the cleanup.
          */
-        engine_cleanup_add_last(engine_list_cleanup);
+        if (!engine_cleanup_add_last(engine_list_cleanup)) {
+            CRYPTO_DOWN_REF(&e->struct_ref, &ref);
+            ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
+            return 0;
+        }
+        engine_list_head = e;
+        e->prev = NULL;
     } else {
         /* We are adding to the tail of an existing list. */
         if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {

@bernd-edlinger
Copy link
Member Author

ah sorry, now I see 3.1 is actually also broken, since the struct ref is incremented below, while master incremended it before.

@tom-cosgrove-arm
Copy link
Contributor

(I didn't check 3.1; I saw the 3.0 failure on the fuzz results on another PR)

@bernd-edlinger
Copy link
Member Author

quite possible that 3.1 will compile, but the ref-counting is weird on master, that confused me a bit...

wanghao75 pushed a commit to openeuler-mirror/openssl that referenced this pull request Sep 23, 2023
Error handling in engine_cleanup_add_first/last was
broken and caused memory leaks.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from openssl/openssl#21971)

Signed-off-by: fly2x <fly2x@hitls.org>
wanghao75 pushed a commit to openeuler-mirror/openssl that referenced this pull request Sep 23, 2023
Error handling in engine_cleanup_add_first/last was
broken and caused memory leaks.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from openssl/openssl#21971)

(cherry picked from commit 00f2efc)
Signed-off-by: fly2x <fly2x@hitls.org>
wanghao75 pushed a commit to openeuler-mirror/openssl that referenced this pull request Sep 23, 2023
Error handling in engine_cleanup_add_first/last was
broken and caused memory leaks.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from openssl/openssl#21971)

(cherry picked from commit 00f2efc)
Signed-off-by: fly2x <fly2x@hitls.org>
xl32 pushed a commit to xl32/openssl that referenced this pull request Sep 29, 2023
Error handling in engine_cleanup_add_first/last was
broken and caused memory leaks.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from openssl#21971)

(cherry picked from commit 00f2efc)
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 Merge to master branch branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 tests: exempted The PR is exempt from requirements for testing triaged: bug The issue/pr is/fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants