Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH] crypto: pkcs7_verify: use constant-time comparison for digest and signature verification
@ 2026-07-10 17:30 David C.C.M. Gall
  2026-07-10 17:56 ` James Bottomley
  0 siblings, 1 reply; 3+ messages in thread
From: David C.C.M. Gall @ 2026-07-10 17:30 UTC (permalink / raw)
  To: David Howells, Lukas Wunner, Ignat Korchagin, Herbert Xu,
	David S. Miller, keyrings, linux-crypto, linux-kernel
  Cc: gregkh

Replace memcmp() with crypto_memneq() for cryptographic digest and
signature comparisons to prevent timing side-channel attacks.

crypto/asymmetric_keys/pkcs7_verify.c: PKCS#7 message digest comparison
during signature verification passes argument pkcs7 and attached
signatures to pkcs7_digest via pkcs7_verify_one. pkcs7_digest utilized
memcmp which could leak valid prefix length for attached signatures via
timing side-channel.

Assisted-by: gregkh_clanker_t1000
Signed-off-by: David C.C.M. Gall <david.ccm.gall@googlemail.com>
---
 crypto/asymmetric_keys/pkcs7_verify.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/crypto/asymmetric_keys/pkcs7_verify.c b/crypto/asymmetric_keys/pkcs7_verify.c
index 474e2c1ae21b..28953e53177b 100644
--- a/crypto/asymmetric_keys/pkcs7_verify.c
+++ b/crypto/asymmetric_keys/pkcs7_verify.c
@@ -13,6 +13,7 @@
 #include <linux/asn1.h>
 #include <crypto/hash.h>
 #include <crypto/hash_info.h>
+#include <crypto/utils.h>
 #include <crypto/public_key.h>
 #include "pkcs7_parser.h"
 
@@ -93,8 +94,8 @@ static int pkcs7_digest(struct pkcs7_message *pkcs7,
 			goto error;
 		}
 
-		if (memcmp(sig->m, sinfo->msgdigest,
-			   sinfo->msgdigest_len) != 0) {
+		if (crypto_memneq(sig->m, sinfo->msgdigest,
+				  sinfo->msgdigest_len)) {
 			pr_warn("Sig %u: Message digest doesn't match\n",
 				sinfo->index);
 			ret = -EKEYREJECTED;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] crypto: pkcs7_verify: use constant-time comparison for digest and signature verification
  2026-07-10 17:30 [PATCH] crypto: pkcs7_verify: use constant-time comparison for digest and signature verification David C.C.M. Gall
@ 2026-07-10 17:56 ` James Bottomley
  2026-07-13 21:23   ` David Gall
  0 siblings, 1 reply; 3+ messages in thread
From: James Bottomley @ 2026-07-10 17:56 UTC (permalink / raw)
  To: David C.C.M. Gall, David Howells, Lukas Wunner, Ignat Korchagin,
	Herbert Xu, David S. Miller, keyrings, linux-crypto, linux-kernel
  Cc: gregkh

On Fri, 2026-07-10 at 19:30 +0200, David C.C.M. Gall wrote:
> Replace memcmp() with crypto_memneq() for cryptographic digest and
> signature comparisons to prevent timing side-channel attacks.
> 
> crypto/asymmetric_keys/pkcs7_verify.c: PKCS#7 message digest
> comparison during signature verification passes argument pkcs7 and
> attached signatures to pkcs7_digest via pkcs7_verify_one.
> pkcs7_digest utilized memcmp which could leak valid prefix length for
> attached signatures via timing side-channel.

Please explain how this information is usable by an attacker?  The
assumption is the attacker sees the module (or whatever is signed) so
the pkcs7 digest is inside the signature in plain text and the digest
of the entity being compared should be computable by any attacker.

Regards,

James


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] crypto: pkcs7_verify: use constant-time comparison for digest and signature verification
  2026-07-10 17:56 ` James Bottomley
@ 2026-07-13 21:23   ` David Gall
  0 siblings, 0 replies; 3+ messages in thread
From: David Gall @ 2026-07-13 21:23 UTC (permalink / raw)
  To: James Bottomley
  Cc: David Howells, Lukas Wunner, Ignat Korchagin, Herbert Xu,
	David S. Miller, keyrings, linux-crypto, linux-kernel, gregkh

On Fri, Jul 10, 2026 at 01:56:51PM -0400, James Bottomley wrote:
> On Fri, 2026-07-10 at 19:30 +0200, David C.C.M. Gall wrote:
> > Replace memcmp() with crypto_memneq() for cryptographic digest and
> > signature comparisons to prevent timing side-channel attacks.
> > 
> > crypto/asymmetric_keys/pkcs7_verify.c: PKCS#7 message digest
> > comparison during signature verification passes argument pkcs7 and
> > attached signatures to pkcs7_digest via pkcs7_verify_one.
> > pkcs7_digest utilized memcmp which could leak valid prefix length for
> > attached signatures via timing side-channel.
> 
> Please explain how this information is usable by an attacker?  The
> assumption is the attacker sees the module (or whatever is signed) so
> the pkcs7 digest is inside the signature in plain text and the digest
> of the entity being compared should be computable by any attacker.
> 
> Regards,
> 
> James
> 
Looking into the usage of these methods a bit deeper, I agree with you
that an attacker does not gain any useful information. I double checked
and the method in question is also used as part of IMA modsig
collection during the measurement collection process, but there too the
method is not used to verify a signature, just to generate a hash, so
the comparison itself is never reached in that path.
                                                                                                                                                                                              
In that case, I'd actually drop this patch. The only affected paths
don't disclose anything useful to an attacker that can't already be
computed by just examining the content.

David

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-07-13 21:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-10 17:30 [PATCH] crypto: pkcs7_verify: use constant-time comparison for digest and signature verification David C.C.M. Gall
2026-07-10 17:56 ` James Bottomley
2026-07-13 21:23   ` David Gall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox