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

Potential use after free in callers of X509v3_asid_add_id_or_range() #22700

Closed
botovq opened this issue Nov 11, 2023 · 4 comments
Closed

Potential use after free in callers of X509v3_asid_add_id_or_range() #22700

botovq opened this issue Nov 11, 2023 · 4 comments
Labels
branch: master Merge to master branch branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 branch: 3.2 Merge to openssl-3.2 triaged: bug The issue/pr is/fixes a bug

Comments

@botovq
Copy link
Contributor

botovq commented Nov 11, 2023

The RFC 3779 code is undocumented, not very well designed and thus rather dangerous public OpenSSL API. It's full of traps and bugs. It should at least be warned against if not documented. Fortunately, almost no one uses it, but there is now an attempt at exposing bindings to Rust: sfackler/rust-openssl#2053 .

The only internal caller v2i_ASIdentifiers() of X509v3_asid_ad_id_or_range() allocates and passes one or two ASN1_INTEGERs and frees them on failure.

Towards the end of X509v3_asid_ad_id_or_range(), ownership of min and max is transferred to aor:

ASN1_INTEGER_free(aor->u.range->min);
aor->u.range->min = min;
ASN1_INTEGER_free(aor->u.range->max);
aor->u.range->max = max;

Subsequently, aor is pushed onto a part of asid. If the push fails, aor is freed and min and max with it.

if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor)))
goto err;
return 1;
err:
ASIdOrRange_free(aor);
return 0;

Here's the fix I landed earlier today: openbsd/src@9b790dc .

@botovq botovq added the issue: bug report The issue was opened to report a bug label Nov 11, 2023
@botovq botovq changed the title Potential use after free in callers of X509v3_addr_add_id_or_range() Potential use after free in callers of X509v3_asid_add_id_or_range() Nov 11, 2023
@t8m t8m added branch: master Merge to master branch triaged: bug The issue/pr is/fixes a bug branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 branch: 3.2 Merge to openssl-3.2 and removed issue: bug report The issue was opened to report a bug labels Nov 13, 2023
@bernd-edlinger
Copy link
Member

bernd-edlinger commented Nov 14, 2023

how about this for a simple fix?

diff --git a/crypto/x509/v3_asid.c b/crypto/x509/v3_asid.c
index d1c3dd5d9f..30a62399bf 100644
--- a/crypto/x509/v3_asid.c
+++ b/crypto/x509/v3_asid.c
@@ -208,6 +208,8 @@ int X509v3_asid_add_id_or_range(ASIdentifiers *asid,
     }
     if ((aor = ASIdOrRange_new()) == NULL)
         return 0;
+    if (!sk_ASIdOrRange_reserve((*choice)->u.asIdsOrRanges, 1))
+        goto err;
     if (max == NULL) {
         aor->type = ASIdOrRange_id;
         aor->u.id = min;

maybe with a comment at the sk_ASIdOrRange_push below, that it cannot fail
due to the reservation?

@botovq
Copy link
Contributor Author

botovq commented Nov 14, 2023 via email

@bernd-edlinger
Copy link
Member

I will certainly back port that to my 1.1.1 feature branch:
https://github.com/bernd-edlinger/openssl/tree/openssl-111-features
I have lots of similar fixes in the queue.

@botovq
Copy link
Contributor Author

botovq commented Nov 14, 2023 via email

bernd-edlinger added a commit to bernd-edlinger/openssl that referenced this issue Nov 15, 2023
And clean up partially created choice objects, which have
still the default type = -1 from ASIdentifierChoice_new().

Fixes openssl#22700
openssl-machine pushed a commit that referenced this issue Dec 1, 2023
And clean up partially created choice objects, which have
still the default type = -1 from ASIdentifierChoice_new().

Fixes #22700

Reviewed-by: Todd Short <todd.short@me.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22745)

(cherry picked from commit 49e9436)
openssl-machine pushed a commit that referenced this issue Dec 1, 2023
And clean up partially created choice objects, which have
still the default type = -1 from ASIdentifierChoice_new().

Fixes #22700

Reviewed-by: Todd Short <todd.short@me.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22745)

(cherry picked from commit 49e9436)
openssl-machine pushed a commit that referenced this issue Dec 1, 2023
And clean up partially created choice objects, which have
still the default type = -1 from ASIdentifierChoice_new().

Fixes #22700

Reviewed-by: Todd Short <todd.short@me.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22745)

(cherry picked from commit 49e9436)
bernd-edlinger added a commit to bernd-edlinger/openssl that referenced this issue Dec 1, 2023
And clean up partially created choice objects, which have
still the default type = -1 from ASIdentifierChoice_new().

Fixes openssl#22700

Reviewed-by: Todd Short <todd.short@me.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl#22745)

(cherry picked from commit 49e9436)
wbeck10 pushed a commit to wbeck10/openssl that referenced this issue Jan 8, 2024
And clean up partially created choice objects, which have
still the default type = -1 from ASIdentifierChoice_new().

Fixes openssl#22700

Reviewed-by: Todd Short <todd.short@me.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl#22745)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch: master Merge to master branch branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 branch: 3.2 Merge to openssl-3.2 triaged: bug The issue/pr is/fixes a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants