Netdev List
 help / color / mirror / Atom feed
From: Yuxiao Zhang <yuxiaozhang@google.com>
To: Samuel Mendoza-Jonas <sam@mendozajonas.com>,
	"David S. Miller" <davem@davemloft.net>,
	 Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	 Willem de Bruijn <willemb@google.com>
Cc: Paul Fertser <fercerpav@gmail.com>,
	Simon Horman <horms@kernel.org>, Joel Stanley <joel@jms.id.au>,
	 Peter Delevoryas <peter@pjd.dev>,
	Patrick Williams <patrick@stwcx.xyz>,
	netdev@vger.kernel.org,  Yuxiao Zhang <yuxiaozhang@google.com>
Subject: [PATCH net v2 1/2] net/ncsi: validate response packet lengths against skb
Date: Tue,  1 Dec 2026 19:36:08 -0800	[thread overview]
Message-ID: <20261202033609.1934503-2-yuxiaozhang@google.com> (raw)
In-Reply-To: <20261202033609.1934503-1-yuxiaozhang@google.com>

ncsi_rcv_rsp() dereferences struct ncsi_pkt_hdr from skb_network_header()
without ensuring the header is present in the linear portion of the skb,
and ncsi_validate_rsp_pkt() reads response code, reason, and checksum
fields without verifying that payload is at least the 4-byte response
header extension or that the skb contains the full common header,
4-byte-aligned payload, and trailing checksum.

For variable-length response handlers (payload == -1 in
ncsi_rsp_handlers[], such as GMCMA, GP, OEM, and PLDM), ncsi_rcv_rsp()
passes ntohs(hdr->length) directly to ncsi_validate_rsp_pkt(). A short
skb with a large hdr->length (for example, a 46-byte frame with
length = 1538) therefore reads out of bounds when dereferencing
*pchecksum and computing the checksum, before any per-command handler
runs. Additionally, ALIGN(payload, 4) on an unsigned short wraps to 0
for payload values 65533..65535, and dereferencing hdr->type after
ncsi_validate_rsp_pkt() would use a stale pointer once pskb_may_pull()
can reallocate skb->head.

Unshare the skb with skb_share_check() before pulling, verify the common
header with pskb_may_pull() in ncsi_rcv_rsp(), check the minimum payload
size and pull the full common header + 4-byte-aligned payload + checksum
in ncsi_validate_rsp_pkt() using size_t arithmetic for ALIGN(), and use
nrh->type for logging after validation. Because ALIGN((size_t)payload, 4)
rounds odd payload lengths up to a multiple of 4, ncsi_calculate_checksum()
also stays within the pulled linear buffer.

Discovered during review of the GMCMA bounds fix (originally found via
automated LLM-assisted code scanning and verified by manual code
inspection). Not observed in production, but reproduced at runtime on
netdev/net with ARCH=um + KASAN by delivering a 46-byte
NCSI_PKT_RSP_GMCMA skb with length=1538 to ncsi_rcv_rsp():

  BUG: KASAN: slab-out-of-bounds in ncsi_validate_rsp_pkt.isra.0+0x2cd/0x330
  Read of size 4 at addr 00000000624d69d4 by task kunit_try_catch/30

With this patch applied, the same KUnit + KASAN run rejects truncated
<16B headers, payload < 4, length=1538, and length=65533..65535 with
-EINVAL without triggering KASAN, while passing odd-length checksums and
shared non-linear skbs.

Fixes: 138635cc27c9 ("net/ncsi: NCSI response packet handler")
Signed-off-by: Yuxiao Zhang <yuxiaozhang@google.com>
---
 net/ncsi/ncsi-rsp.c | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/net/ncsi/ncsi-rsp.c b/net/ncsi/ncsi-rsp.c
index 84cc8ac0ca2f..e917cefd408b 100644
--- a/net/ncsi/ncsi-rsp.c
+++ b/net/ncsi/ncsi-rsp.c
@@ -35,9 +35,25 @@ static u8 decode_bcd_u8(u8 x)
 static int ncsi_validate_rsp_pkt(struct ncsi_request *nr,
 				 unsigned short payload)
 {
+	size_t payload_len, pkt_len;
 	struct ncsi_rsp_pkt_hdr *h;
-	u32 checksum;
 	__be32 *pchecksum;
+	u32 checksum;
+
+	if (payload < sizeof(*h) - sizeof(h->common)) {
+		netdev_dbg(nr->ndp->ndev.dev,
+			   "NCSI: response payload too short (%u)\n", payload);
+		return -EINVAL;
+	}
+
+	payload_len = ALIGN((size_t)payload, 4);
+	pkt_len = sizeof(h->common) + payload_len + sizeof(*pchecksum);
+	if (!pskb_may_pull(nr->rsp, pkt_len)) {
+		netdev_dbg(nr->ndp->ndev.dev,
+			   "NCSI: response packet truncated (len %u, need %zu)\n",
+			   nr->rsp->len, pkt_len);
+		return -EINVAL;
+	}
 
 	/* Check NCSI packet header. We don't need validate
 	 * the packet type, which should have been checked
@@ -69,7 +85,7 @@ static int ncsi_validate_rsp_pkt(struct ncsi_request *nr,
 	 * sender doesn't support checksum according to NCSI
 	 * specification.
 	 */
-	pchecksum = (__be32 *)((void *)(h + 1) + ALIGN(payload, 4) - 4);
+	pchecksum = (__be32 *)((void *)h + sizeof(h->common) + payload_len);
 	if (ntohl(*pchecksum) == 0)
 		return 0;
 
@@ -1184,6 +1200,15 @@ int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
 		goto err_free_skb;
 	}
 
+	skb = skb_share_check(skb, GFP_ATOMIC);
+	if (!skb)
+		return -ENOMEM;
+
+	if (!pskb_may_pull(skb, sizeof(*hdr))) {
+		ret = -EINVAL;
+		goto err_free_skb;
+	}
+
 	/* Check if it is AEN packet */
 	hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
 	if (hdr->type == NCSI_PKT_AEN)
@@ -1233,7 +1258,7 @@ int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
 	if (ret) {
 		netdev_warn(ndp->ndev.dev,
 			    "NCSI: 'bad' packet ignored for type 0x%x\n",
-			    hdr->type);
+			    nrh->type);
 
 		if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
 			if (ret == -EPERM)
@@ -1253,7 +1278,7 @@ int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
 	if (ret)
 		netdev_err(ndp->ndev.dev,
 			   "NCSI: Handler for packet type 0x%x returned %d\n",
-			   hdr->type, ret);
+			   nrh->type, ret);
 
 out_netlink:
 	if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
@@ -1261,7 +1286,7 @@ int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
 		if (ret) {
 			netdev_err(ndp->ndev.dev,
 				   "NCSI: Netlink handler for packet type 0x%x returned %d\n",
-				   hdr->type, ret);
+				   nrh->type, ret);
 		}
 	}
 
-- 
2.56.1.839.g22a8290db9-goog


  reply	other threads:[~2026-12-02  3:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  3:36 [PATCH net v2 0/2] net/ncsi: validate response packet and GMCMA lengths Yuxiao Zhang
2026-12-02  3:36 ` Yuxiao Zhang [this message]
2026-12-02  3:36 ` [PATCH net v2 2/2] net/ncsi: validate GMCMA address_count against packet length Yuxiao Zhang

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=20261202033609.1934503-2-yuxiaozhang@google.com \
    --to=yuxiaozhang@google.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fercerpav@gmail.com \
    --cc=horms@kernel.org \
    --cc=joel@jms.id.au \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=patrick@stwcx.xyz \
    --cc=peter@pjd.dev \
    --cc=sam@mendozajonas.com \
    --cc=willemb@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox