All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] crypto: bcm - Reject requests with too many fragments
@ 2026-12-05  6:41 Praneeth Balasubramanian
  2026-12-05  7:02 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  6:41 UTC (permalink / raw)
  To: Herbert Xu, David S. Miller
  Cc: Praneeth Balasubramanian, stable, linux-crypto, linux-kernel

The skcipher, ahash and aead request handlers count the mailbox
scatterlist entries in u8 variables. They add the number of entries in the
request scatterlists to these counts. A request with 255 or more entries
makes a count wrap. The sg create helpers then allocate a short mailbox
scatterlist from the wrapped count. spu_msg_sg_add() still adds one entry
per request entry and writes past the end of the allocation.

Such requests are easy to make. A large AEAD request with one entry per
page, or an AF_ALG request built with splice from many small pieces, has
enough entries.

Count the fragments in u32 variables and fail requests with -EINVAL when a
count does not fit the u8 parameter of the sg create helpers. A request
with a wrapped count never worked, so no working request is rejected.

This was found by an LLM-based audit that checks code against the rules
stated in kernel comments.

Fixes: 9d12ba86f818 ("crypto: brcm - Add Broadcom SPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    This was found by an LLM-based audit, part of research at UCSB SecLab.
    An LLM turned a kernel comment into a rule, and three LLM votes checked
    the code against it. The fix and changelog were drafted with GLM-5.3,
    then checked against the source and partly rewritten with Claude Opus
    5.5. I reviewed the patch and take responsibility for it.
    
    Testing: builds without new warnings with W=1 (x86_64 allmodconfig),
    clang 23. checkpatch.pl is clean. Not runtime-tested, and there is no
    reproducer. I do not have this hardware.

 drivers/crypto/bcm/cipher.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/drivers/crypto/bcm/cipher.c b/drivers/crypto/bcm/cipher.c
index 8c3e9525c134..6a811a93a31f 100644
--- a/drivers/crypto/bcm/cipher.c
+++ b/drivers/crypto/bcm/cipher.c
@@ -315,8 +315,8 @@ static int handle_skcipher_req(struct iproc_reqctx_s *rctx)
 	struct brcm_message *mssg;	/* mailbox message */
 
 	/* number of entries in src and dst sg in mailbox message. */
-	u8 rx_frag_num = 2;	/* response header and STATUS */
-	u8 tx_frag_num = 1;	/* request header */
+	u32 rx_frag_num = 2;	/* response header and STATUS */
+	u32 tx_frag_num = 1;	/* request header */
 
 	flow_log("%s\n", __func__);
 
@@ -435,6 +435,10 @@ static int handle_skcipher_req(struct iproc_reqctx_s *rctx)
 	    spu->spu_xts_tweak_in_payload())
 		rx_frag_num++;	/* extra sg to insert tweak */
 
+	/* The sg create helpers take the fragment counts as a u8 */
+	if (rx_frag_num > U8_MAX)
+		return -EINVAL;
+
 	err = spu_skcipher_rx_sg_create(mssg, rctx, rx_frag_num, chunksize,
 					  stat_pad_len);
 	if (err)
@@ -449,6 +453,9 @@ static int handle_skcipher_req(struct iproc_reqctx_s *rctx)
 	    spu->spu_xts_tweak_in_payload())
 		tx_frag_num++;	/* extra sg to insert tweak */
 
+	if (tx_frag_num > U8_MAX)
+		return -EINVAL;
+
 	err = spu_skcipher_tx_sg_create(mssg, rctx, tx_frag_num, chunksize,
 					  pad_len);
 	if (err)
@@ -686,7 +693,7 @@ static int handle_ahash_req(struct iproc_reqctx_s *rctx)
 	 * rx always includes a buffer to catch digest and STATUS.
 	 */
 	u8 rx_frag_num = 3;
-	u8 tx_frag_num = 1;
+	u32 tx_frag_num = 1;
 
 	flow_log("total_todo %u, total_sent %u\n",
 		 rctx->total_todo, rctx->total_sent);
@@ -868,6 +875,9 @@ static int handle_ahash_req(struct iproc_reqctx_s *rctx)
 	tx_frag_num += rctx->src_nents;
 	if (spu->spu_tx_status_len())
 		tx_frag_num++;
+	/* The sg create helper takes the fragment count as a u8 */
+	if (tx_frag_num > U8_MAX)
+		return -EINVAL;
 	err = spu_ahash_tx_sg_create(mssg, rctx, tx_frag_num, spu_hdr_len,
 				     local_nbuf, new_data_len, pad_len);
 	if (err)
@@ -1275,8 +1285,8 @@ static int handle_aead_req(struct iproc_reqctx_s *rctx)
 
 	/* number of entries in src and dst sg. Always includes SPU msg header.
 	 */
-	u8 rx_frag_num = 2;	/* and STATUS */
-	u8 tx_frag_num = 1;
+	u32 rx_frag_num = 2;	/* and STATUS */
+	u32 tx_frag_num = 1;
 
 	/* doing the whole thing at once */
 	chunksize = rctx->total_todo;
@@ -1486,6 +1496,9 @@ static int handle_aead_req(struct iproc_reqctx_s *rctx)
 			rx_frag_num -= rctx->dst_nents;
 	}
 
+	/* The sg create helpers take the fragment counts as a u8 */
+	if (rx_frag_num > U8_MAX)
+		return -EINVAL;
 	err = spu_aead_rx_sg_create(mssg, req, rctx, rx_frag_num,
 				    aead_parms.assoc_size,
 				    aead_parms.ret_iv_len, resp_len, digestsize,
@@ -1502,6 +1515,8 @@ static int handle_aead_req(struct iproc_reqctx_s *rctx)
 		tx_frag_num++;
 	if (spu->spu_tx_status_len())
 		tx_frag_num++;
+	if (tx_frag_num > U8_MAX)
+		return -EINVAL;
 	err = spu_aead_tx_sg_create(mssg, rctx, tx_frag_num, spu_hdr_len,
 				    rctx->assoc, aead_parms.assoc_size,
 				    assoc_nents, aead_parms.iv_len, chunksize,

base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9
-- 
2.25.1


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

* Re: [PATCH] crypto: bcm - Reject requests with too many fragments
  2026-12-05  6:41 [PATCH] crypto: bcm - Reject requests with too many fragments Praneeth Balasubramanian
@ 2026-12-05  7:02 ` sashiko-bot
  2026-12-05  8:21   ` Praneeth Balasubramanian
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-12-05  7:02 UTC (permalink / raw)
  To: Praneeth Balasubramanian, Herbert Xu, David S. Miller; +Cc: linux-crypto

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Critical] crypto: bcm: off-by-one rx_frag_num check wraps u8 in
  spu_aead_rx_sg_create()
--

commit 18c5819a2054b0edb55dbef6d87807eed0c94639
Author: Praneeth Balasubramanian <praneethb2002@gmail.com>

crypto: bcm - Reject requests with too many fragments

This commit widens the fragment counters in the skcipher, ahash, and aead
request handlers from u8 to u32 and rejects requests with -EINVAL when a
count exceeds U8_MAX before calling the sg create helpers.

> diff --git a/drivers/crypto/bcm/cipher.c b/drivers/crypto/bcm/cipher.c
> index 8c3e9525c134e..6a811a93a31fb 100644
> --- a/drivers/crypto/bcm/cipher.c
> +++ b/drivers/crypto/bcm/cipher.c

[ ... ]

> @@ -1486,6 +1496,9 @@ static int handle_aead_req(struct iproc_reqctx_s *rctx)
>  			rx_frag_num -= rctx->dst_nents;
>  	}
>  
> +	/* The sg create helpers take the fragment counts as a u8 */
> +	if (rx_frag_num > U8_MAX)
> +		return -EINVAL;
>  	err = spu_aead_rx_sg_create(mssg, req, rctx, rx_frag_num,
>  				    aead_parms.assoc_size,
>  				    aead_parms.ret_iv_len, resp_len, digestsize,

[Severity: Critical]
Can rx_frag_num still wrap to 0 inside spu_aead_rx_sg_create()?

This check lets rx_frag_num == U8_MAX (255) through, but
spu_aead_rx_sg_create() takes rx_frag_num as a u8 and increments it again
when data_padlen is non-zero:

drivers/crypto/bcm/cipher.c:spu_aead_rx_sg_create() {
	...
	if (data_padlen)
		/* have to catch gcm pad in separate buffer */
		rx_frag_num++;

	mssg->spu.dst = kmalloc_objs(struct scatterlist, rx_frag_num, rctx->gfp);
	if (!mssg->spu.dst)
		return -ENOMEM;

	sg = mssg->spu.dst;
	sg_init_table(sg, rx_frag_num);
	...
}

On SPU-M hardware, an unprivileged AF_ALG user submitting a GCM request
with a payload not aligned to 16 bytes (e.g. 252 bytes across 252 dst sg
entries) gets a non-zero data_padlen from spum_gcm_ccm_pad_len() with
rx_frag_num == 255.

Incrementing u8 rx_frag_num wraps it to 0, so kmalloc_objs() returns
ZERO_SIZE_PTR (bypassing the NULL check) and sg_init_table(sg, 0) writes
out of bounds through ZERO_SIZE_PTR, causing a kernel oops or panic.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205064109.2526623-1-praneethb2002@gmail.com?part=1

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

* Re: [PATCH] crypto: bcm - Reject requests with too many fragments
  2026-12-05  7:02 ` sashiko-bot
@ 2026-12-05  8:21   ` Praneeth Balasubramanian
  0 siblings, 0 replies; 3+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  8:21 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel

On Sat, 05 Dec 2026 07:02:46 +0000, sashiko-bot@kernel.org wrote:
> [Severity: Critical]
> Can rx_frag_num still wrap to 0 inside spu_aead_rx_sg_create()?
>
> This check lets rx_frag_num == U8_MAX (255) through, but
> spu_aead_rx_sg_create() takes rx_frag_num as a u8 and increments it again
> when data_padlen is non-zero:

Right, thanks. The check in handle_aead_req() does not count the pad
entry, so the count can still wrap. Please do not apply this version.
v2 makes spu_aead_rx_sg_create() fail with -EINVAL when the pad entry
would not fit, before it allocates. The other sg create helpers do not
add entries on their own.

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

end of thread, other threads:[~2026-12-05  8:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  6:41 [PATCH] crypto: bcm - Reject requests with too many fragments Praneeth Balasubramanian
2026-12-05  7:02 ` sashiko-bot
2026-12-05  8:21   ` Praneeth Balasubramanian

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.