From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796459504; b=p7F++VRqn6yAnbZhGIwK840/RF2zDGkrJs+cglmI2F/oTS33myPfmpEPeTVU1klog6Gd mQgg8IRQe5LsS+Z+E2rCPY8LZLVYuvw3ctCtU/KRYQMDwiEwg1n+1+SAh5cuBrI3MxY0z bDinL7WQuUdq7MfHClvHDBT5Htl/yN00XlvyfXxUhhMRnLXO5GhO78XLPFPq02rK7p+N8 mH5w2AFsYtpYMHcU3u3Eqsj0m9Os4CPPgO6gcERhL68bIpebRdQKrUw8HYpiqjVtZCDB4 DsF0WG+3jYgqopd54/Lnxd9EyRqM/h5e5WRX3GnB3kTC6GkBAHXxmsmre2fGl08Pnaw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796459504; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=q/N9OrmiVMedrHAL2Kckfqb+/mmgQoE7JTEV90AwDJE=; b=Z+yhkkjKHmVt1vi+0XYIxTQhJTqKDi9Hfaqj1gsbBXDRU+0324+dowzlKx3UlaHRqdIW kI4l3aETaqmqmpkDihu/8f2WfaQP8z0lzk54N7G4i2LbRlQv2Du/9pzvs80yI8ry/joqG PZXdCCLp+tVt+6Rml7NM7MN+ACFLTz1XxZSl4wm7XcKnm58CkZxFy0gF2vNZwuCTSUPXZ ot3d+b+yZA2hGlWGfilTKrmAdZxK2hrW6uQYyLGP/s4abWXX0TCYyHdJjAG3o9/6lR934 LkiFxM8lGPdMeGo1LugMXqzoZfI/Lkukpb9eFJWSA5sMWtSGSq24GL5DjH5+C+GxK6g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZAAE+x6b; arc=none smtp.remote-ip=74.125.82.45 Received: from mail-dl1-f45.google.com (mail-dl1-f45.google.com [74.125.82.45]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5619A606A330 for ; Sat, 5 Dec 2026 08:31:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5619A606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZAAE+x6b Received: by mail-dl1-f45.google.com with SMTP id a92af1059eb24-1b5d5f0ba39so1967770c88.0 for ; Sat, 05 Dec 2026 00:31:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796459502; x=1797064302; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=q/N9OrmiVMedrHAL2Kckfqb+/mmgQoE7JTEV90AwDJE=; b=ZAAE+x6bgaaMK/zNuZS/zio8vAvDmfxIb8U6kuKwaI9us8/4o+dFlHJYJhiCo41g4b 3YMiYwpRcblHZ4zFLW/BoMbbaPZG/rPRHx7DE7XyGBMQ8sFGivPRtm2Vfcq29xGaRxpJ O/N/mRyzFE1YvvZSdjwosjfoUIJCB9R5q8AxZBoitVGSLA5HRNXIlkJdlUq+N0nssgix 8ITAPhRsxWlihILmMHXm0y8GCAEk9W+8TEE4v3ZeAlpCWJZnt3cflcOgI2hjzcM/mFSr BPRn/fQgy0yi6iiQUt6ubKalZnd0pdwMZSGZhCV7pZpuXJchq4zJFfqp1uyaqIHRVKT4 rdmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796459502; x=1797064302; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=q/N9OrmiVMedrHAL2Kckfqb+/mmgQoE7JTEV90AwDJE=; b=dE3kFuuYq6MpDlOWwxH80D/nzgsBTg/csbL8jAaHeRWL6IJiPdq2rmBFkYd4W//P0i gV7+E067hA4vFSfwxi7TpYzSg9ya25G4zoHV5tcrvi1IL7oNMJcz3XYMYantasWNNWWO 1X4/qM7l2OqJU3whqNgCjoSNoL/McIqrCJob1yC+8reEwdPA9ig/Oiyr6K5vvtjuEG8d rGes1r3jSz8EBYj3accURsWNPWVXZiEkB92z9+i9KkML94IF/D9ORm+SC74a8g2QZWmi 5Qe53lKQg0YP3UAvcFkgq988bVR9tsLMUuh7bET21gld0BcBAvVyi5KZ4dUBsVzkLktZ noyQ== X-Forwarded-Encrypted: i=1; AL+hErEiW95/DCtSUDe87lQkRWLGforZ9uhl1fnDU+LEpiNiVl0m3vViHL1s5Lrdo9alJjc/9K+gZJo=@vger.kernel.org X-Gm-Message-State: APuU4Z/5S5+aLMygbtr8kRaNmzcwhsSXA8ncgxe1uQM/8J28uGzly6lM gGEjO9XgVFdX1dd+tADJj8S1D5doOxEtYyCan4DNbfna9O5YAf7e/wot X-Gm-Gg: AZFdTZduHB1nJ2IHWclONIX+kmuxwTlkbOcWiS39wRCRdMJH+XXSgZWo9P8FHIKAixU OowpEBik96YcSruih0yOfWjBsTQNMt6SKLeVHl0vbI/xn/7V1TYemoYPLWrNQ7BAwJ4nqntjXef bG4rDo3oEM2ZirSF+3vgYbOh99RAJSBB/Rji0Q8jlYMTYGnU1CJCNHbXBhz8ss3G4rd9MHlpqiE JqyAskQotd/0qFQnKJX/ekkrY5zwpR5h88wmuxIDhW+e97Qby7pnvJrsuu+QjB24Ytp+/HSQsZ6 j2NcgusIzkFbW8KVwnCzG/iWtRt4NWwLcOjQMSzhGa7mLSXV4DqLjQqQQwALD9fR4w4uU5on3YV +G0ftkQAjIX6ZIx0CJsF/KxN7KmwdkjxuX2+ju8CWyY4hBifmd/bZmgH8O04A5XWpWBma9uhnRD /f8AeQodtXuV+DWRNHVr14Y5t4A5Qh4zKAl5kmw6IS06yA18nhNeaj/HQ+0V9im1AYhr/lfvN+v 8Op6h9YF/WkZl+S9F4GvAmHWdAlaFE0LO/J0oEm/NLtkp2vQB8lWgk9NnMGMgeArEvqVwzKsDXE Mut9KxzWtMxDsSylZlPsLp1/rSmdXQw9dsJQ+JKKvrRF7Fh2RjcZPHQ5zgdskvt0i50= X-Received: by 2002:a05:701a:c9e2:20b0:1b5:bcb7:2176 with SMTP id a92af1059eb24-1b5d70de060mr4015327c88.16.1796459502433; Sat, 05 Dec 2026 00:31:42 -0800 (PST) Received: from localhost.localdomain (wood.seclab.cs.ucsb.edu. [128.111.49.37]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-38752204591sm18912369eec.21.2026.12.05.00.31.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 00:31:42 -0800 (PST) From: Praneeth Balasubramanian To: Herbert Xu , "David S. Miller" Cc: Praneeth Balasubramanian , stable@vger.kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] crypto: bcm - Reject requests with too many fragments Date: Sat, 5 Dec 2026 00:31:38 -0800 Message-Id: <20261205083138.2603704-1-praneethb2002@gmail.com> X-Mailer: git-send-email 2.25.1 X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. spu_aead_rx_sg_create() adds one more entry itself when the GCM or CCM response needs a pad buffer, so it also fails when that entry would wrap its count. 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 --- Notes: Changes in v2: - Fixed an off-by-one that Sashiko found. spu_aead_rx_sg_create() adds one entry for the GCM/CCM pad after the caller's check, so a count of 255 still wrapped to 0. The helper now checks before it adds the entry. v1: https://lore.kernel.org/all/20261205064109.2526623-1-praneethb2002@gmail.com/ 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. The v2 changes were made with Claude Opus 5.5 after it checked the Sashiko review against the source. 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 | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/drivers/crypto/bcm/cipher.c b/drivers/crypto/bcm/cipher.c index 8c3e9525c134..35a95682a6a5 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) @@ -1069,9 +1079,12 @@ static int spu_aead_rx_sg_create(struct brcm_message *mssg, resp_len + data_padlen); - if (data_padlen) + if (data_padlen) { /* have to catch gcm pad in separate buffer */ + if (rx_frag_num == U8_MAX) + return -EINVAL; rx_frag_num++; + } mssg->spu.dst = kmalloc_objs(struct scatterlist, rx_frag_num, rctx->gfp); if (!mssg->spu.dst) @@ -1275,8 +1288,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 +1499,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 +1518,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