From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
patches@lists.linux.dev,
Tianjia Zhang <tianjia.zhang@linux.alibaba.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
Nathan Chancellor <nathan@kernel.org>
Subject: [PATCH 6.2 02/16] crypto: arm64/sm4-gcm - Fix possible crash in GCM cryption
Date: Wed, 1 Mar 2023 19:07:38 +0100 [thread overview]
Message-ID: <20230301180653.358560430@linuxfoundation.org> (raw)
In-Reply-To: <20230301180653.263532453@linuxfoundation.org>
From: Herbert Xu <herbert@gondor.apana.org.au>
commit 4e4a08868f15897ca236528771c3733fded42c62 upstream.
An often overlooked aspect of the skcipher walker API is that an
error is not just indicated by a non-zero return value, but by the
fact that walk->nbytes is zero.
Thus it is an error to call skcipher_walk_done after getting back
walk->nbytes == 0 from the previous interaction with the walker.
This is because when walk->nbytes is zero the walker is left in
an undefined state and any further calls to it may try to free
uninitialised stack memory.
The sm4 arm64 ccm code gets this wrong and ends up calling
skcipher_walk_done even when walk->nbytes is zero.
This patch rewrites the loop in a form that resembles other callers.
Reported-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Fixes: ae1b83c7d572 ("crypto: arm64/sm4 - add CE implementation for GCM mode")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Tested-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
arch/arm64/crypto/sm4-ce-gcm-glue.c | 51 +++++++++++++++++-------------------
1 file changed, 25 insertions(+), 26 deletions(-)
--- a/arch/arm64/crypto/sm4-ce-gcm-glue.c
+++ b/arch/arm64/crypto/sm4-ce-gcm-glue.c
@@ -135,22 +135,23 @@ static void gcm_calculate_auth_mac(struc
}
static int gcm_crypt(struct aead_request *req, struct skcipher_walk *walk,
- struct sm4_gcm_ctx *ctx, u8 ghash[],
+ u8 ghash[], int err,
void (*sm4_ce_pmull_gcm_crypt)(const u32 *rkey_enc,
u8 *dst, const u8 *src, u8 *iv,
unsigned int nbytes, u8 *ghash,
const u8 *ghash_table, const u8 *lengths))
{
+ struct crypto_aead *aead = crypto_aead_reqtfm(req);
+ struct sm4_gcm_ctx *ctx = crypto_aead_ctx(aead);
u8 __aligned(8) iv[SM4_BLOCK_SIZE];
be128 __aligned(8) lengths;
- int err;
memset(ghash, 0, SM4_BLOCK_SIZE);
lengths.a = cpu_to_be64(req->assoclen * 8);
lengths.b = cpu_to_be64(walk->total * 8);
- memcpy(iv, walk->iv, GCM_IV_SIZE);
+ memcpy(iv, req->iv, GCM_IV_SIZE);
put_unaligned_be32(2, iv + GCM_IV_SIZE);
kernel_neon_begin();
@@ -158,49 +159,51 @@ static int gcm_crypt(struct aead_request
if (req->assoclen)
gcm_calculate_auth_mac(req, ghash);
- do {
+ while (walk->nbytes) {
unsigned int tail = walk->nbytes % SM4_BLOCK_SIZE;
const u8 *src = walk->src.virt.addr;
u8 *dst = walk->dst.virt.addr;
if (walk->nbytes == walk->total) {
- tail = 0;
-
sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, dst, src, iv,
walk->nbytes, ghash,
ctx->ghash_table,
(const u8 *)&lengths);
- } else if (walk->nbytes - tail) {
- sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, dst, src, iv,
- walk->nbytes - tail, ghash,
- ctx->ghash_table, NULL);
+
+ kernel_neon_end();
+
+ return skcipher_walk_done(walk, 0);
}
+ sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, dst, src, iv,
+ walk->nbytes - tail, ghash,
+ ctx->ghash_table, NULL);
+
kernel_neon_end();
err = skcipher_walk_done(walk, tail);
- if (err)
- return err;
- if (walk->nbytes)
- kernel_neon_begin();
- } while (walk->nbytes > 0);
- return 0;
+ kernel_neon_begin();
+ }
+
+ sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, NULL, NULL, iv,
+ walk->nbytes, ghash, ctx->ghash_table,
+ (const u8 *)&lengths);
+
+ kernel_neon_end();
+
+ return err;
}
static int gcm_encrypt(struct aead_request *req)
{
struct crypto_aead *aead = crypto_aead_reqtfm(req);
- struct sm4_gcm_ctx *ctx = crypto_aead_ctx(aead);
u8 __aligned(8) ghash[SM4_BLOCK_SIZE];
struct skcipher_walk walk;
int err;
err = skcipher_walk_aead_encrypt(&walk, req, false);
- if (err)
- return err;
-
- err = gcm_crypt(req, &walk, ctx, ghash, sm4_ce_pmull_gcm_enc);
+ err = gcm_crypt(req, &walk, ghash, err, sm4_ce_pmull_gcm_enc);
if (err)
return err;
@@ -215,17 +218,13 @@ static int gcm_decrypt(struct aead_reque
{
struct crypto_aead *aead = crypto_aead_reqtfm(req);
unsigned int authsize = crypto_aead_authsize(aead);
- struct sm4_gcm_ctx *ctx = crypto_aead_ctx(aead);
u8 __aligned(8) ghash[SM4_BLOCK_SIZE];
u8 authtag[SM4_BLOCK_SIZE];
struct skcipher_walk walk;
int err;
err = skcipher_walk_aead_decrypt(&walk, req, false);
- if (err)
- return err;
-
- err = gcm_crypt(req, &walk, ctx, ghash, sm4_ce_pmull_gcm_dec);
+ err = gcm_crypt(req, &walk, ghash, err, sm4_ce_pmull_gcm_dec);
if (err)
return err;
next prev parent reply other threads:[~2023-03-01 18:09 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-01 18:07 [PATCH 6.2 00/16] 6.2.2-rc1 review Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 01/16] ALSA: hda: cs35l41: Correct error condition handling Greg Kroah-Hartman
2023-03-01 18:07 ` Greg Kroah-Hartman [this message]
2023-03-01 18:07 ` [PATCH 6.2 03/16] bpf: bpf_fib_lookup should not return neigh in NUD_FAILED state Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 04/16] vc_screen: dont clobber return value in vcs_read Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 05/16] drm/amd/display: Move DCN314 DOMAIN power control to DMCUB Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 06/16] drm/amd/display: Properly reuse completion structure Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 07/16] scripts/tags.sh: fix incompatibility with PCRE2 Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 08/16] wifi: rtw88: usb: Set qsel correctly Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 09/16] wifi: rtw88: usb: send Zero length packets if necessary Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 10/16] wifi: rtw88: usb: drop now unnecessary URB size check Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 11/16] usb: dwc3: pci: add support for the Intel Meteor Lake-M Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 12/16] USB: serial: option: add support for VW/Skoda "Carstick LTE" Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 13/16] usb: gadget: u_serial: Add null pointer check in gserial_resume Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 14/16] arm64: dts: uniphier: Fix property name in PXs3 USB node Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 15/16] usb: typec: pd: Remove usb_suspend_supported sysfs from sink PDO Greg Kroah-Hartman
2023-03-01 18:07 ` [PATCH 6.2 16/16] USB: core: Dont hold device lock while reading the "descriptors" sysfs file Greg Kroah-Hartman
2023-03-01 22:50 ` [PATCH 6.2 00/16] 6.2.2-rc1 review Florian Fainelli
2023-03-01 23:24 ` Justin Forbes
2023-03-02 1:42 ` Shuah Khan
2023-03-02 2:16 ` Bagas Sanjaya
2023-03-02 7:27 ` Jon Hunter
2023-03-02 10:21 ` Conor Dooley
2023-03-02 11:40 ` Sudip Mukherjee (Codethink)
2023-03-02 17:23 ` Ron Economos
2023-03-02 19:25 ` Naresh Kamboju
2023-03-02 23:16 ` Slade Watkins
2023-03-03 1:31 ` Guenter Roeck
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=20230301180653.358560430@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=herbert@gondor.apana.org.au \
--cc=nathan@kernel.org \
--cc=patches@lists.linux.dev \
--cc=stable@vger.kernel.org \
--cc=tianjia.zhang@linux.alibaba.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.