7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Eric Biggers [ Upstream commit efb621a57d7e4ce28e12082ba5692231858c4a3a ] C doesn't guarantee at the language level that an enum actually has one of the declared values. For robustness, make mldsa_verify() handle invalid values of 'alg'. No effect on current callers, who always pass a valid value for 'alg'. (Except for the KUnit test case that this commit adds.) Fixes: 64edccea594c ("lib/crypto: Add ML-DSA verification support") Cc: stable@vger.kernel.org Reviewed-by: Ard Biesheuvel Link: https://patch.msgid.link/20261029054534.68481-3-ebiggers@kernel.org Signed-off-by: Eric Biggers Signed-off-by: Sasha Levin --- include/crypto/mldsa.h | 1 + lib/crypto/mldsa.c | 10 ++++++++-- lib/crypto/tests/mldsa_kunit.c | 9 +++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/crypto/mldsa.h b/include/crypto/mldsa.h index 3ef2676787c9e..e0754879e7452 100644 --- a/include/crypto/mldsa.h +++ b/include/crypto/mldsa.h @@ -48,6 +48,7 @@ enum mldsa_alg { * Return: * * 0 if the signature is valid * * -EBADMSG if the signature and/or public key is malformed + * * -EINVAL if @alg is not a valid value * * -EKEYREJECTED if the signature is invalid but otherwise well-formed * * -ENOMEM if out of memory so the validity of the signature is unknown */ diff --git a/lib/crypto/mldsa.c b/lib/crypto/mldsa.c index 68a1b432315b1..80e0e63b418a7 100644 --- a/lib/crypto/mldsa.c +++ b/lib/crypto/mldsa.c @@ -528,8 +528,8 @@ static size_t encode_w1(u8 out[MAX_W1_ENCODED_LEN], int mldsa_verify(enum mldsa_alg alg, const u8 *sig, size_t sig_len, const u8 *msg, size_t msg_len, const u8 *pk, size_t pk_len) { - const struct mldsa_parameter_set *params = &mldsa_parameter_sets[alg]; - const int k = params->k, l = params->l; + const struct mldsa_parameter_set *params; + int k, l; /* For now this just does pure ML-DSA with an empty context string. */ static const u8 msg_prefix[2] = { /* dom_sep= */ 0, /* ctx_len= */ 0 }; const u8 *ctilde; /* The signer's commitment hash */ @@ -537,6 +537,12 @@ int mldsa_verify(enum mldsa_alg alg, const u8 *sig, size_t sig_len, u8 *h; /* The signer's hint vector, length k * N */ size_t w1_enc_len; + if (alg != MLDSA44 && alg != MLDSA65 && alg != MLDSA87) + return -EINVAL; + params = &mldsa_parameter_sets[alg]; + k = params->k; + l = params->l; + /* Validate the public key and signature lengths. */ if (pk_len != params->pk_len || sig_len != params->sig_len) return -EBADMSG; diff --git a/lib/crypto/tests/mldsa_kunit.c b/lib/crypto/tests/mldsa_kunit.c index ec64e10c97f25..d0ee704a36944 100644 --- a/lib/crypto/tests/mldsa_kunit.c +++ b/lib/crypto/tests/mldsa_kunit.c @@ -372,6 +372,14 @@ static void test_mldsa_use_hint(struct kunit *test) } } +static void test_mldsa_invalid_alg(struct kunit *test) +{ + KUNIT_ASSERT_EQ(test, -EINVAL, + mldsa_verify(MLDSA44 - 1, NULL, 0, NULL, 0, NULL, 0)); + KUNIT_ASSERT_EQ(test, -EINVAL, + mldsa_verify(MLDSA87 + 1, NULL, 0, NULL, 0, NULL, 0)); +} + static void benchmark_mldsa(struct kunit *test, const struct mldsa_testvector *tv) { @@ -414,6 +422,7 @@ static struct kunit_case mldsa_kunit_cases[] = { KUNIT_CASE(test_mldsa65), KUNIT_CASE(test_mldsa87), KUNIT_CASE(test_mldsa_use_hint), + KUNIT_CASE(test_mldsa_invalid_alg), KUNIT_CASE(benchmark_mldsa44), KUNIT_CASE(benchmark_mldsa65), KUNIT_CASE(benchmark_mldsa87), -- 2.53.0