From e73e14bbf46785f5533dce87ff40cbc7f0b5128d Mon Sep 17 00:00:00 2001 From: ShenYage Date: Tue, 3 Sep 2024 19:49:55 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20CVE-2021-3712=E3=80=81CVE-2022-0778?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ShenYage --- ...NAME-to-not-assume-NUL-terminated-st.patch | 148 ++++++++++++++ ...rinting-to-not-assume-NUL-terminated.patch | 53 +++++ ...PROXY_CERT_INFO_EXTENSION-to-not-ass.patch | 33 +++ ...straints-code-to-not-assume-NUL-term.patch | 190 ++++++++++++++++++ ...to-not-assume-NUL-terminated-strings.patch | 39 ++++ ...unction-to-not-assume-NUL-terminated.patch | 55 +++++ ...I_print-function-to-not-assume-NUL-t.patch | 32 +++ ..._from_ecparameters-to-check-the-base.patch | 36 ++++ ...ossible-infinite-loop-in-BN_mod_sqrt.patch | 69 +++++++ edk2.spec | 15 +- 10 files changed, 669 insertions(+), 1 deletion(-) create mode 100644 0089-Fix-i2v_GENERAL_NAME-to-not-assume-NUL-terminated-st.patch create mode 100644 0090-Fix-POLICYINFO-printing-to-not-assume-NUL-terminated.patch create mode 100644 0091-Fix-printing-of-PROXY_CERT_INFO_EXTENSION-to-not-ass.patch create mode 100644 0092-Fix-the-name-constraints-code-to-not-assume-NUL-term.patch create mode 100644 0093-Fix-test-code-to-not-assume-NUL-terminated-strings.patch create mode 100644 0094-Fix-append_ia5-function-to-not-assume-NUL-terminated.patch create mode 100644 0095-Fix-NETSCAPE_SPKI_print-function-to-not-assume-NUL-t.patch create mode 100644 0096-Fix-EC_GROUP_new_from_ecparameters-to-check-the-base.patch create mode 100644 0097-Fix-possible-infinite-loop-in-BN_mod_sqrt.patch diff --git a/0089-Fix-i2v_GENERAL_NAME-to-not-assume-NUL-terminated-st.patch b/0089-Fix-i2v_GENERAL_NAME-to-not-assume-NUL-terminated-st.patch new file mode 100644 index 0000000..8fa9dc7 --- /dev/null +++ b/0089-Fix-i2v_GENERAL_NAME-to-not-assume-NUL-terminated-st.patch @@ -0,0 +1,148 @@ +From 0d8b0b8b00094b3b58bd37fce9f260affe9f6a16 Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Wed, 18 Aug 2021 12:24:22 +0100 +Subject: [PATCH 1/9] Fix i2v_GENERAL_NAME to not assume NUL terminated strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + .../OpensslLib/openssl/crypto/x509v3/v3_alt.c | 10 +++-- + .../OpensslLib/openssl/crypto/x509v3/v3_utl.c | 38 ++++++++++++++++--- + .../OpensslLib/openssl/include/crypto/x509.h | 5 +++ + 3 files changed, 44 insertions(+), 9 deletions(-) + +diff --git a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_alt.c b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_alt.c +index 7ac2911..79f0f14 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_alt.c ++++ b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_alt.c +@@ -9,6 +9,7 @@ + + #include + #include "internal/cryptlib.h" ++#include "crypto/x509.h" + #include + #include + #include "ext_dat.h" +@@ -99,17 +100,20 @@ STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, + break; + + case GEN_EMAIL: +- if (!X509V3_add_value_uchar("email", gen->d.ia5->data, &ret)) ++ if (!x509v3_add_len_value_uchar("email", gen->d.ia5->data, ++ gen->d.ia5->length, &ret)) + return NULL; + break; + + case GEN_DNS: +- if (!X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret)) ++ if (!x509v3_add_len_value_uchar("DNS", gen->d.ia5->data, ++ gen->d.ia5->length, &ret)) + return NULL; + break; + + case GEN_URI: +- if (!X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret)) ++ if (!x509v3_add_len_value_uchar("URI", gen->d.ia5->data, ++ gen->d.ia5->length, &ret)) + return NULL; + break; + +diff --git a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_utl.c b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_utl.c +index 7281a7b..004ef55 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_utl.c ++++ b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_utl.c +@@ -12,6 +12,7 @@ + #include "e_os.h" + #include "internal/cryptlib.h" + #include ++#include + #include "crypto/ctype.h" + #include + #include +@@ -34,17 +35,26 @@ static int ipv6_hex(unsigned char *out, const char *in, int inlen); + + /* Add a CONF_VALUE name value pair to stack */ + +-int X509V3_add_value(const char *name, const char *value, +- STACK_OF(CONF_VALUE) **extlist) ++static int x509v3_add_len_value(const char *name, const char *value, ++ size_t vallen, STACK_OF(CONF_VALUE) **extlist) + { + CONF_VALUE *vtmp = NULL; + char *tname = NULL, *tvalue = NULL; + int sk_allocated = (*extlist == NULL); + +- if (name && (tname = OPENSSL_strdup(name)) == NULL) +- goto err; +- if (value && (tvalue = OPENSSL_strdup(value)) == NULL) ++ if (name != NULL && (tname = OPENSSL_strdup(name)) == NULL) + goto err; ++ if (value != NULL && vallen > 0) { ++ /* ++ * We tolerate a single trailing NUL character, but otherwise no ++ * embedded NULs ++ */ ++ if (memchr(value, 0, vallen - 1) != NULL) ++ goto err; ++ tvalue = OPENSSL_strndup(value, vallen); ++ if (tvalue == NULL) ++ goto err; ++ } + if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL) + goto err; + if (sk_allocated && (*extlist = sk_CONF_VALUE_new_null()) == NULL) +@@ -67,10 +77,26 @@ int X509V3_add_value(const char *name, const char *value, + return 0; + } + ++int X509V3_add_value(const char *name, const char *value, ++ STACK_OF(CONF_VALUE) **extlist) ++{ ++ return x509v3_add_len_value(name, value, ++ value != NULL ? strlen((const char *)value) : 0, ++ extlist); ++} ++ + int X509V3_add_value_uchar(const char *name, const unsigned char *value, + STACK_OF(CONF_VALUE) **extlist) + { +- return X509V3_add_value(name, (const char *)value, extlist); ++ return x509v3_add_len_value(name, (const char *)value, ++ value != NULL ? strlen((const char *)value) : 0, ++ extlist); ++} ++ ++int x509v3_add_len_value_uchar(const char *name, const unsigned char *value, ++ size_t vallen, STACK_OF(CONF_VALUE) **extlist) ++{ ++ return x509v3_add_len_value(name, (const char *)value, vallen, extlist); + } + + /* Free function for STACK_OF(CONF_VALUE) */ +diff --git a/CryptoPkg/Library/OpensslLib/openssl/include/crypto/x509.h b/CryptoPkg/Library/OpensslLib/openssl/include/crypto/x509.h +index b53c2b0..7ffb8ab 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/include/crypto/x509.h ++++ b/CryptoPkg/Library/OpensslLib/openssl/include/crypto/x509.h +@@ -8,6 +8,8 @@ + */ + + #include "internal/refcount.h" ++#include ++#include + + /* Internal X509 structures and functions: not for application use */ + +@@ -284,3 +286,6 @@ int a2i_ipadd(unsigned char *ipout, const char *ipasc); + int x509_set1_time(ASN1_TIME **ptm, const ASN1_TIME *tm); + + void x509_init_sig_info(X509 *x); ++ ++int x509v3_add_len_value_uchar(const char *name, const unsigned char *value, ++ size_t vallen, STACK_OF(CONF_VALUE) **extlist); +-- +2.33.0 + diff --git a/0090-Fix-POLICYINFO-printing-to-not-assume-NUL-terminated.patch b/0090-Fix-POLICYINFO-printing-to-not-assume-NUL-terminated.patch new file mode 100644 index 0000000..03b34d3 --- /dev/null +++ b/0090-Fix-POLICYINFO-printing-to-not-assume-NUL-terminated.patch @@ -0,0 +1,53 @@ +From 3667dd5072369630c0977833987a4fc6d0cd6c0f Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Wed, 18 Aug 2021 12:31:38 +0100 +Subject: [PATCH 2/9] Fix POLICYINFO printing to not assume NUL terminated + strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + .../Library/OpensslLib/openssl/crypto/x509v3/v3_cpols.c | 9 ++++++--- + 1 file changed, 6 insertions(+), 3 deletions(-) + +diff --git a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_cpols.c b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_cpols.c +index 1d12c89..861e845 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_cpols.c ++++ b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_cpols.c +@@ -422,7 +422,8 @@ static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals, + qualinfo = sk_POLICYQUALINFO_value(quals, i); + switch (OBJ_obj2nid(qualinfo->pqualid)) { + case NID_id_qt_cps: +- BIO_printf(out, "%*sCPS: %s\n", indent, "", ++ BIO_printf(out, "%*sCPS: %.*s\n", indent, "", ++ qualinfo->d.cpsuri->length, + qualinfo->d.cpsuri->data); + break; + +@@ -447,7 +448,8 @@ static void print_notice(BIO *out, USERNOTICE *notice, int indent) + if (notice->noticeref) { + NOTICEREF *ref; + ref = notice->noticeref; +- BIO_printf(out, "%*sOrganization: %s\n", indent, "", ++ BIO_printf(out, "%*sOrganization: %.*s\n", indent, "", ++ ref->organization->length, + ref->organization->data); + BIO_printf(out, "%*sNumber%s: ", indent, "", + sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : ""); +@@ -470,7 +472,8 @@ static void print_notice(BIO *out, USERNOTICE *notice, int indent) + BIO_puts(out, "\n"); + } + if (notice->exptext) +- BIO_printf(out, "%*sExplicit Text: %s\n", indent, "", ++ BIO_printf(out, "%*sExplicit Text: %.*s\n", indent, "", ++ notice->exptext->length, + notice->exptext->data); + } + +-- +2.33.0 + diff --git a/0091-Fix-printing-of-PROXY_CERT_INFO_EXTENSION-to-not-ass.patch b/0091-Fix-printing-of-PROXY_CERT_INFO_EXTENSION-to-not-ass.patch new file mode 100644 index 0000000..dd43b1b --- /dev/null +++ b/0091-Fix-printing-of-PROXY_CERT_INFO_EXTENSION-to-not-ass.patch @@ -0,0 +1,33 @@ +From a38c0d6a477fca7df10bc8d57c7428f42f875fd9 Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Wed, 18 Aug 2021 14:02:40 +0100 +Subject: [PATCH 3/9] Fix printing of PROXY_CERT_INFO_EXTENSION to not assume + NUL terminated strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_pci.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_pci.c b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_pci.c +index 3d124fa..98b6ef2 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_pci.c ++++ b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_pci.c +@@ -77,7 +77,8 @@ static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci, + i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage); + BIO_puts(out, "\n"); + if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data) +- BIO_printf(out, "%*sPolicy Text: %s\n", indent, "", ++ BIO_printf(out, "%*sPolicy Text: %.*s\n", indent, "", ++ pci->proxyPolicy->policy->length, + pci->proxyPolicy->policy->data); + return 1; + } +-- +2.33.0 + diff --git a/0092-Fix-the-name-constraints-code-to-not-assume-NUL-term.patch b/0092-Fix-the-name-constraints-code-to-not-assume-NUL-term.patch new file mode 100644 index 0000000..5852b11 --- /dev/null +++ b/0092-Fix-the-name-constraints-code-to-not-assume-NUL-term.patch @@ -0,0 +1,190 @@ +From 75797fee3d7ec2e69f6edaed8b317cc53782eccf Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Wed, 18 Aug 2021 17:08:58 +0100 +Subject: [PATCH 4/9] Fix the name constraints code to not assume NUL + terminated strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + .../openssl/crypto/x509v3/v3_ncons.c | 77 +++++++++++++------ + 1 file changed, 52 insertions(+), 25 deletions(-) + +diff --git a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_ncons.c b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_ncons.c +index 2a7b4f0..cb701c4 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_ncons.c ++++ b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_ncons.c +@@ -63,8 +63,31 @@ ASN1_SEQUENCE(NAME_CONSTRAINTS) = { + IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE) + IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS) + ++ ++#define IA5_OFFSET_LEN(ia5base, offset) \ ++ ((ia5base)->length - ((unsigned char *)(offset) - (ia5base)->data)) ++ ++/* Like memchr but for ASN1_IA5STRING. Additionally you can specify the ++ * starting point to search from ++ */ ++# define ia5memchr(str, start, c) memchr(start, c, IA5_OFFSET_LEN(str, start)) ++ ++/* Like memrrchr but for ASN1_IA5STRING */ ++static char *ia5memrchr(ASN1_IA5STRING *str, int c) ++{ ++ int i; ++ ++ for (i = str->length; i > 0 && str->data[i - 1] != c; i--); ++ ++ if (i == 0) ++ return NULL; ++ ++ return (char *)&str->data[i - 1]; ++} ++ + /* +- * We cannot use strncasecmp here because that applies locale specific rules. ++ * We cannot use strncasecmp here because that applies locale specific rules. It ++ * also doesn't work with ASN1_STRINGs that may have embedded NUL characters. + * For example in Turkish 'I' is not the uppercase character for 'i'. We need to + * do a simple ASCII case comparison ignoring the locale (that is why we use + * numeric constants below). +@@ -89,20 +112,12 @@ static int ia5ncasecmp(const char *s1, const char *s2, size_t n) + + /* c1 > c2 */ + return 1; +- } else if (*s1 == 0) { +- /* If we get here we know that *s2 == 0 too */ +- return 0; + } + } + + return 0; + } + +-static int ia5casecmp(const char *s1, const char *s2) +-{ +- return ia5ncasecmp(s1, s2, SIZE_MAX); +-} +- + static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, + X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval) + { +@@ -337,7 +352,7 @@ static int cn2dnsid(ASN1_STRING *cn, unsigned char **dnsid, size_t *idlen) + --utf8_length; + + /* Reject *embedded* NULs */ +- if ((size_t)utf8_length != strlen((char *)utf8_value)) { ++ if (memchr(utf8_value, 0, utf8_length) != NULL) { + OPENSSL_free(utf8_value); + return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; + } +@@ -536,9 +551,14 @@ static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base) + { + char *baseptr = (char *)base->data; + char *dnsptr = (char *)dns->data; ++ + /* Empty matches everything */ +- if (!*baseptr) ++ if (base->length == 0) + return X509_V_OK; ++ ++ if (dns->length < base->length) ++ return X509_V_ERR_PERMITTED_VIOLATION; ++ + /* + * Otherwise can add zero or more components on the left so compare RHS + * and if dns is longer and expect '.' as preceding character. +@@ -549,7 +569,7 @@ static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base) + return X509_V_ERR_PERMITTED_VIOLATION; + } + +- if (ia5casecmp(baseptr, dnsptr)) ++ if (ia5ncasecmp(baseptr, dnsptr, base->length)) + return X509_V_ERR_PERMITTED_VIOLATION; + + return X509_V_OK; +@@ -560,16 +580,17 @@ static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base) + { + const char *baseptr = (char *)base->data; + const char *emlptr = (char *)eml->data; ++ const char *baseat = ia5memrchr(base, '@'); ++ const char *emlat = ia5memrchr(eml, '@'); ++ size_t basehostlen, emlhostlen; + +- const char *baseat = strchr(baseptr, '@'); +- const char *emlat = strchr(emlptr, '@'); + if (!emlat) + return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; + /* Special case: initial '.' is RHS match */ +- if (!baseat && (*baseptr == '.')) { ++ if (!baseat && base->length > 0 && (*baseptr == '.')) { + if (eml->length > base->length) { + emlptr += eml->length - base->length; +- if (ia5casecmp(baseptr, emlptr) == 0) ++ if (ia5ncasecmp(baseptr, emlptr, base->length) == 0) + return X509_V_OK; + } + return X509_V_ERR_PERMITTED_VIOLATION; +@@ -589,8 +610,10 @@ static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base) + baseptr = baseat + 1; + } + emlptr = emlat + 1; ++ basehostlen = IA5_OFFSET_LEN(base, baseptr); ++ emlhostlen = IA5_OFFSET_LEN(eml, emlptr); + /* Just have hostname left to match: case insensitive */ +- if (ia5casecmp(baseptr, emlptr)) ++ if (basehostlen != emlhostlen || ia5ncasecmp(baseptr, emlptr, emlhostlen)) + return X509_V_ERR_PERMITTED_VIOLATION; + + return X509_V_OK; +@@ -601,10 +624,14 @@ static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base) + { + const char *baseptr = (char *)base->data; + const char *hostptr = (char *)uri->data; +- const char *p = strchr(hostptr, ':'); ++ const char *p = ia5memchr(uri, (char *)uri->data, ':'); + int hostlen; ++ + /* Check for foo:// and skip past it */ +- if (!p || (p[1] != '/') || (p[2] != '/')) ++ if (p == NULL ++ || IA5_OFFSET_LEN(uri, p) < 3 ++ || p[1] != '/' ++ || p[2] != '/') + return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; + hostptr = p + 3; + +@@ -612,13 +639,13 @@ static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base) + + /* Look for a port indicator as end of hostname first */ + +- p = strchr(hostptr, ':'); ++ p = ia5memchr(uri, hostptr, ':'); + /* Otherwise look for trailing slash */ +- if (!p) +- p = strchr(hostptr, '/'); ++ if (p == NULL) ++ p = ia5memchr(uri, hostptr, '/'); + +- if (!p) +- hostlen = strlen(hostptr); ++ if (p == NULL) ++ hostlen = IA5_OFFSET_LEN(uri, hostptr); + else + hostlen = p - hostptr; + +@@ -626,7 +653,7 @@ static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base) + return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; + + /* Special case: initial '.' is RHS match */ +- if (*baseptr == '.') { ++ if (base->length > 0 && *baseptr == '.') { + if (hostlen > base->length) { + p = hostptr + hostlen - base->length; + if (ia5ncasecmp(p, baseptr, base->length) == 0) +-- +2.33.0 + diff --git a/0093-Fix-test-code-to-not-assume-NUL-terminated-strings.patch b/0093-Fix-test-code-to-not-assume-NUL-terminated-strings.patch new file mode 100644 index 0000000..0c234ec --- /dev/null +++ b/0093-Fix-test-code-to-not-assume-NUL-terminated-strings.patch @@ -0,0 +1,39 @@ +From 60f5589c783f24b5b3b3d7257b422a27e1975631 Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Wed, 18 Aug 2021 17:37:41 +0100 +Subject: [PATCH 5/9] Fix test code to not assume NUL terminated strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + .../Library/OpensslLib/openssl/test/x509_time_test.c | 10 ++++++---- + 1 file changed, 6 insertions(+), 4 deletions(-) + +diff --git a/CryptoPkg/Library/OpensslLib/openssl/test/x509_time_test.c b/CryptoPkg/Library/OpensslLib/openssl/test/x509_time_test.c +index b6fd38a..d0993d9 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/test/x509_time_test.c ++++ b/CryptoPkg/Library/OpensslLib/openssl/test/x509_time_test.c +@@ -330,10 +330,12 @@ static int test_x509_time(int idx) + + /* if t is not NULL but expected_string is NULL, it is an 'OK' case too */ + if (t != NULL && x509_format_tests[idx].expected_string) { +- if (!TEST_str_eq((const char *)t->data, +- x509_format_tests[idx].expected_string)) { +- TEST_info("test_x509_time(%d) failed: expected_string %s, got %s\n", +- idx, x509_format_tests[idx].expected_string, t->data); ++ if (!TEST_mem_eq((const char *)t->data, t->length, ++ x509_format_tests[idx].expected_string, ++ strlen(x509_format_tests[idx].expected_string))) { ++ TEST_info("test_x509_time(%d) failed: expected_string %s, got %.*s\n", ++ idx, x509_format_tests[idx].expected_string, t->length, ++ t->data); + goto out; + } + } +-- +2.33.0 + diff --git a/0094-Fix-append_ia5-function-to-not-assume-NUL-terminated.patch b/0094-Fix-append_ia5-function-to-not-assume-NUL-terminated.patch new file mode 100644 index 0000000..dc1254e --- /dev/null +++ b/0094-Fix-append_ia5-function-to-not-assume-NUL-terminated.patch @@ -0,0 +1,55 @@ +From bdb229da3af51e369c97095a445d0d2139ce1b07 Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Wed, 18 Aug 2021 17:58:23 +0100 +Subject: [PATCH 6/9] Fix append_ia5 function to not assume NUL terminated + strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + .../OpensslLib/openssl/crypto/x509v3/v3_utl.c | 18 +++++++++++++----- + 1 file changed, 13 insertions(+), 5 deletions(-) + +diff --git a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_utl.c b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_utl.c +index 004ef55..513dc68 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_utl.c ++++ b/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3/v3_utl.c +@@ -528,18 +528,26 @@ static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, const ASN1_IA5STRING *email + /* First some sanity checks */ + if (email->type != V_ASN1_IA5STRING) + return 1; +- if (!email->data || !email->length) ++ if (email->data == NULL || email->length == 0) ++ return 1; ++ if (memchr(email->data, 0, email->length) != NULL) + return 1; + if (*sk == NULL) + *sk = sk_OPENSSL_STRING_new(sk_strcmp); + if (*sk == NULL) + return 0; ++ ++ emtmp = OPENSSL_strndup((char *)email->data, email->length); ++ if (emtmp == NULL) ++ return 0; ++ + /* Don't add duplicates */ +- if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1) ++ if (sk_OPENSSL_STRING_find(*sk, emtmp) != -1) { ++ OPENSSL_free(emtmp); + return 1; +- emtmp = OPENSSL_strdup((char *)email->data); +- if (emtmp == NULL || !sk_OPENSSL_STRING_push(*sk, emtmp)) { +- OPENSSL_free(emtmp); /* free on push failure */ ++ } ++ if (!sk_OPENSSL_STRING_push(*sk, emtmp)) { ++ OPENSSL_free(emtmp); /* free on push failure */ + X509_email_free(*sk); + *sk = NULL; + return 0; +-- +2.33.0 + diff --git a/0095-Fix-NETSCAPE_SPKI_print-function-to-not-assume-NUL-t.patch b/0095-Fix-NETSCAPE_SPKI_print-function-to-not-assume-NUL-t.patch new file mode 100644 index 0000000..c8febf6 --- /dev/null +++ b/0095-Fix-NETSCAPE_SPKI_print-function-to-not-assume-NUL-t.patch @@ -0,0 +1,32 @@ +From 4e29f3192f39492f96c5e917659e15b5d62f66ff Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Thu, 19 Aug 2021 12:23:38 +0100 +Subject: [PATCH 7/9] Fix NETSCAPE_SPKI_print function to not assume NUL + terminated strings + +ASN.1 strings may not be NUL terminated. Don't assume they are. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + CryptoPkg/Library/OpensslLib/openssl/crypto/asn1/t_spki.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/CryptoPkg/Library/OpensslLib/openssl/crypto/asn1/t_spki.c b/CryptoPkg/Library/OpensslLib/openssl/crypto/asn1/t_spki.c +index 51b56d0..64ee77e 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/crypto/asn1/t_spki.c ++++ b/CryptoPkg/Library/OpensslLib/openssl/crypto/asn1/t_spki.c +@@ -38,7 +38,7 @@ int NETSCAPE_SPKI_print(BIO *out, NETSCAPE_SPKI *spki) + } + chal = spki->spkac->challenge; + if (chal->length) +- BIO_printf(out, " Challenge String: %s\n", chal->data); ++ BIO_printf(out, " Challenge String: %.*s\n", chal->length, chal->data); + i = OBJ_obj2nid(spki->sig_algor.algorithm); + BIO_printf(out, " Signature Algorithm: %s", + (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i)); +-- +2.33.0 + diff --git a/0096-Fix-EC_GROUP_new_from_ecparameters-to-check-the-base.patch b/0096-Fix-EC_GROUP_new_from_ecparameters-to-check-the-base.patch new file mode 100644 index 0000000..0591784 --- /dev/null +++ b/0096-Fix-EC_GROUP_new_from_ecparameters-to-check-the-base.patch @@ -0,0 +1,36 @@ +From d229e59d3007ba8b2620a3014d96f4942651ff0d Mon Sep 17 00:00:00 2001 +From: Matt Caswell +Date: Thu, 19 Aug 2021 12:24:17 +0100 +Subject: [PATCH 8/9] Fix EC_GROUP_new_from_ecparameters to check the base + length + +Check that there's at least one byte in params->base before trying to +read it. + +CVE-2021-3712 + +Reviewed-by: Viktor Dukhovni +Reviewed-by: Paul Dale +--- + CryptoPkg/Library/OpensslLib/openssl/crypto/ec/ec_asn1.c | 5 ++++- + 1 file changed, 4 insertions(+), 1 deletion(-) + +diff --git a/CryptoPkg/Library/OpensslLib/openssl/crypto/ec/ec_asn1.c b/CryptoPkg/Library/OpensslLib/openssl/crypto/ec/ec_asn1.c +index 336afc9..98a742d 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/crypto/ec/ec_asn1.c ++++ b/CryptoPkg/Library/OpensslLib/openssl/crypto/ec/ec_asn1.c +@@ -747,7 +747,10 @@ EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params) + ret->seed_len = params->curve->seed->length; + } + +- if (!params->order || !params->base || !params->base->data) { ++ if (params->order == NULL ++ || params->base == NULL ++ || params->base->data == NULL ++ || params->base->length == 0) { + ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR); + goto err; + } +-- +2.33.0 + diff --git a/0097-Fix-possible-infinite-loop-in-BN_mod_sqrt.patch b/0097-Fix-possible-infinite-loop-in-BN_mod_sqrt.patch new file mode 100644 index 0000000..6a9b9e1 --- /dev/null +++ b/0097-Fix-possible-infinite-loop-in-BN_mod_sqrt.patch @@ -0,0 +1,69 @@ +From 3b1f22885bdda25e26d41dee35554fe8fae41629 Mon Sep 17 00:00:00 2001 +From: Tomas Mraz +Date: Mon, 28 Feb 2022 18:26:21 +0100 +Subject: [PATCH 9/9] Fix possible infinite loop in BN_mod_sqrt() + +The calculation in some cases does not finish for non-prime p. + +This fixes CVE-2022-0778. + +Based on patch by David Benjamin . + +Reviewed-by: Paul Dale +Reviewed-by: Matt Caswell +--- + .../OpensslLib/openssl/crypto/bn/bn_sqrt.c | 30 +++++++++++-------- + 1 file changed, 18 insertions(+), 12 deletions(-) + +diff --git a/CryptoPkg/Library/OpensslLib/openssl/crypto/bn/bn_sqrt.c b/CryptoPkg/Library/OpensslLib/openssl/crypto/bn/bn_sqrt.c +index 1723d5d..53b0f55 100644 +--- a/CryptoPkg/Library/OpensslLib/openssl/crypto/bn/bn_sqrt.c ++++ b/CryptoPkg/Library/OpensslLib/openssl/crypto/bn/bn_sqrt.c +@@ -14,7 +14,8 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) + /* + * Returns 'ret' such that ret^2 == a (mod p), using the Tonelli/Shanks + * algorithm (cf. Henri Cohen, "A Course in Algebraic Computational Number +- * Theory", algorithm 1.5.1). 'p' must be prime! ++ * Theory", algorithm 1.5.1). 'p' must be prime, otherwise an error or ++ * an incorrect "result" will be returned. + */ + { + BIGNUM *ret = in; +@@ -301,18 +302,23 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) + goto vrfy; + } + +- /* find smallest i such that b^(2^i) = 1 */ +- i = 1; +- if (!BN_mod_sqr(t, b, p, ctx)) +- goto end; +- while (!BN_is_one(t)) { +- i++; +- if (i == e) { +- BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE); +- goto end; ++ /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */ ++ for (i = 1; i < e; i++) { ++ if (i == 1) { ++ if (!BN_mod_sqr(t, b, p, ctx)) ++ goto end; ++ ++ } else { ++ if (!BN_mod_mul(t, t, t, p, ctx)) ++ goto end; + } +- if (!BN_mod_mul(t, t, t, p, ctx)) +- goto end; ++ if (BN_is_one(t)) ++ break; ++ } ++ /* If not found, a is not a square or p is not prime. */ ++ if (i >= e) { ++ BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE); ++ goto end; + } + + /* t := y^2^(e - i - 1) */ +-- +2.33.0 + diff --git a/edk2.spec b/edk2.spec index 52c680f..1396678 100644 --- a/edk2.spec +++ b/edk2.spec @@ -5,7 +5,7 @@ Name: edk2 Version: %{stable_date} -Release: 22 +Release: 23 Summary: EFI Development Kit II License: BSD-2-Clause-Patent URL: https://github.com/tianocore/edk2 @@ -105,6 +105,16 @@ Patch0088: 0086-Hardening-around-not_resumable-sessions.patch Patch0089: 0087-Add-a-test-for-session-cache-overflow.patch Patch0090: 0088-MdeModulePkg-Potential-UINT32-overflow-in-S3-ResumeC.patch + +Patch0091: 0089-Fix-i2v_GENERAL_NAME-to-not-assume-NUL-terminated-st.patch +Patch0092: 0090-Fix-POLICYINFO-printing-to-not-assume-NUL-terminated.patch +Patch0093: 0091-Fix-printing-of-PROXY_CERT_INFO_EXTENSION-to-not-ass.patch +Patch0094: 0092-Fix-the-name-constraints-code-to-not-assume-NUL-term.patch +Patch0095: 0093-Fix-test-code-to-not-assume-NUL-terminated-strings.patch +Patch0096: 0094-Fix-append_ia5-function-to-not-assume-NUL-terminated.patch +Patch0097: 0095-Fix-NETSCAPE_SPKI_print-function-to-not-assume-NUL-t.patch +Patch0098: 0096-Fix-EC_GROUP_new_from_ecparameters-to-check-the-base.patch +Patch0099: 0097-Fix-possible-infinite-loop-in-BN_mod_sqrt.patch BuildRequires: acpica-tools gcc gcc-c++ libuuid-devel python3 bc nasm python2 %description @@ -299,6 +309,9 @@ chmod +x %{buildroot}%{_bindir}/Rsa2048Sha256GenerateKeys %endif %changelog +* Tue Sep 3 2024 shenyage - 202002-23 +- fix CVE-2021-3712、CVE-2022-0778 + * Tue Jun 11 2024 shenyage - 202002-22 - fix CVE-2024-1298 -- Gitee