LKML Archive mirror
 help / color / mirror / Atom feed
From: Stefan Berger <stefanb@linux.ibm.com>
To: keyrings@vger.kernel.org, linux-crypto@vger.kernel.org,
	herbert@gondor.apana.org.au, davem@davemloft.net
Cc: linux-kernel@vger.kernel.org, lukas@wunner.de, jarkko@kernel.org,
	Stefan Berger <stefanb@linux.ibm.com>
Subject: [PATCH] crypto: ecc - Protect ecc_digits_from_bytes from reading too many bytes
Date: Fri, 26 Apr 2024 18:55:53 -0400	[thread overview]
Message-ID: <20240426225553.3038070-1-stefanb@linux.ibm.com> (raw)

Protect ecc_digits_from_bytes from reading too many bytes from the input
byte array in case an insufficient number of bytes is provided to fill the
output digit array of ndigits. Therefore, initialize the most significant
digits with 0 to avoid trying to read too many bytes later on.

If too many bytes are provided on the input byte array the extra bytes
are ignored since the input variable 'ndigits' limits the number of digits
that will be filled.

Fixes: d67c96fb97b5 ("crypto: ecdsa - Convert byte arrays with key coordinates to digits")
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 include/crypto/internal/ecc.h | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h
index 7ca1f463d1ec..56215f14ff96 100644
--- a/include/crypto/internal/ecc.h
+++ b/include/crypto/internal/ecc.h
@@ -67,9 +67,16 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigit
 static inline void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
 					 u64 *out, unsigned int ndigits)
 {
+	int diff = ndigits - DIV_ROUND_UP(nbytes, sizeof(u64));
 	unsigned int o = nbytes & 7;
 	__be64 msd = 0;
 
+	/* diff > 0: not enough input bytes: set most significant digits to 0 */
+	while (diff > 0) {
+		out[--ndigits] = 0;
+		diff--;
+	}
+
 	if (o) {
 		memcpy((u8 *)&msd + sizeof(msd) - o, in, o);
 		out[--ndigits] = be64_to_cpu(msd);
-- 
2.43.0


             reply	other threads:[~2024-04-26 22:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-26 22:55 Stefan Berger [this message]
2024-04-28 22:12 ` [PATCH] crypto: ecc - Protect ecc_digits_from_bytes from reading too many bytes Jarkko Sakkinen
2024-04-29  3:30   ` Lukas Wunner
2024-04-29 10:14     ` Jarkko Sakkinen
2024-04-29 11:11       ` Stefan Berger
2024-04-29 13:12         ` Jarkko Sakkinen
2024-05-03 10:30       ` Herbert Xu
2024-05-03 23:49         ` Jarkko Sakkinen
2024-05-03 23:51           ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240426225553.3038070-1-stefanb@linux.ibm.com \
    --to=stefanb@linux.ibm.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=jarkko@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas@wunner.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).