From 0e2d7037db4048dbf1c194508c07384a818261d5 Mon Sep 17 00:00:00 2001 From: Yi Wu Date: Thu, 28 May 2026 15:10:14 +0100 Subject: [PATCH] Backport EC public key 1 byte overread Signed-off-by: Yi Wu --- ChangeLog.d/mbedtls_pk_ecc_set_pubkey.txt | 3 +++ library/pk_ecc.c | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 ChangeLog.d/mbedtls_pk_ecc_set_pubkey.txt diff --git a/ChangeLog.d/mbedtls_pk_ecc_set_pubkey.txt b/ChangeLog.d/mbedtls_pk_ecc_set_pubkey.txt new file mode 100644 index 0000000000..44906298da --- /dev/null +++ b/ChangeLog.d/mbedtls_pk_ecc_set_pubkey.txt @@ -0,0 +1,3 @@ +Security + * Fix a 1-byte buffer overread when parsing a malformed ECC public key + in the PK module. diff --git a/library/pk_ecc.c b/library/pk_ecc.c index 86218fffc8..20fe953d01 100644 --- a/library/pk_ecc.c +++ b/library/pk_ecc.c @@ -205,13 +205,19 @@ int mbedtls_pk_ecc_set_pubkey(mbedtls_pk_context *pk, const unsigned char *pub, { #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) + /* We need to read the first byte to determine the format (compressed, + * uncompressed or 0). */ + if (pub_len == 0) { + return MBEDTLS_ERR_PK_INVALID_PUBKEY; + } + /* Load the key */ if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(pk->ec_family) || *pub == 0x04) { /* Format directly supported by PSA: * - non-Weierstrass curves that only have one format; * - uncompressed format for Weierstrass curves. */ if (pub_len > sizeof(pk->pub_raw)) { - return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; + return MBEDTLS_ERR_PK_INVALID_PUBKEY; } memcpy(pk->pub_raw, pub, pub_len); pk->pub_raw_len = pub_len;