[PATCH 2/3] lib/digsig: Reject a signature larger than the modulus

From: Dan Carpenter

Date: Wed Dec 02 2026 - 07:20:58 EST


This is a correctness fix based on the RFC. Currently,
digsig_verify_rsa() accepts a legacy IMA or EVM signature even if the
RSA modulus has been added to it. That's a bug: it means one signature
could have several different encodings that verify.

RSA verification (RSAVP1 in RFC 8017 section 5.2.2) starts by checking
that the signature s is less than the modulus n. digsig_verify_rsa()
skips that check and computes s^e mod n directly, so s, s + n, s + 2n
and so on all give the same result. No signature is forged, but a
different value is accepted for the same file. The kernel's other RSA
code rejects s >= n in rsa_check_payload().

Reject a signature that is greater than or equal to the modulus.

In real life signatures made by evmctl are always less than n because
RSA_private_encrypt() returns m^d mod n. So it means no one is affected
by this bug in practice but also fixing it has no risk of breaking
anything.

Fixes: 051dbb918c7f ("crypto: digital signature verification support")
Signed-off-by: Dan Carpenter <dan@xxxxxxxx>
---
lib/digsig.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/lib/digsig.c b/lib/digsig.c
index 8ca6af0a8fd4..4940ce755750 100644
--- a/lib/digsig.c
+++ b/lib/digsig.c
@@ -135,6 +135,11 @@ static int digsig_verify_rsa(struct key *key,
goto err;
}

+ if (mpi_cmp(in, pkey[0]) >= 0) {
+ err = -EINVAL;
+ goto err;
+ }
+
res = mpi_alloc(mpi_get_nlimbs(in) * 2);
if (!res)
goto err;

--
2.53.0