* [PATCH net v2 0/2] net/ncsi: validate response packet and GMCMA lengths
@ 2026-12-02 3:36 Yuxiao Zhang
2026-12-02 3:36 ` [PATCH net v2 1/2] net/ncsi: validate response packet lengths against skb Yuxiao Zhang
2026-12-02 3:36 ` [PATCH net v2 2/2] net/ncsi: validate GMCMA address_count against packet length Yuxiao Zhang
0 siblings, 2 replies; 3+ messages in thread
From: Yuxiao Zhang @ 2026-12-02 3:36 UTC (permalink / raw)
To: Samuel Mendoza-Jonas, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Willem de Bruijn
Cc: Paul Fertser, Simon Horman, Joel Stanley, Peter Delevoryas,
Patrick Williams, netdev, Yuxiao Zhang
This 2-patch series fixes out-of-bounds reads in the NC-SI response RX
path:
1. Validate minimum response payload length and pull the common header,
4-byte-aligned payload, and trailing checksum in ncsi_rcv_rsp() /
ncsi_validate_rsp_pkt() (using size_t for ALIGN() to avoid 16-bit
wraparound on 65533..65535), after unsharing the skb with
skb_share_check() and replacing post-validation hdr->type uses with
nrh->type.
2. Validate that the NC-SI payload length covers the fixed GMCMA header
and all address_count * ETH_ALEN entries in ncsi_rsp_handler_gmcma().
Discovery & testing:
- Originally found via automated LLM-assisted code scanning and verified
by manual code inspection. Not observed in production, but both
out-of-bounds reads were reproduced at runtime on netdev/net with
ARCH=um KUnit + KASAN (length=1538 in ncsi_validate_rsp_pkt() and
length=8 / address_count=0xff in ncsi_rsp_handler_gmcma()).
- With both patches applied, all 9 KUnit + KASAN test cases pass (0
KASAN reports), covering <16B headers, payload < 4, length=1538,
length=65533..65535, GMCMA address_count=0xff, Ethernet-padded
length=8 / address_count=3, GMCMA payload=4, odd-length checksums, and
shared non-linear skbs. Also compile-tested on arm64 with W=1.
Changes in v2:
- Split into 2 patches in response to Sashiko AI review: move skb length
validation and pskb_may_pull() up into ncsi_rcv_rsp() and
ncsi_validate_rsp_pkt() (Patch 1/2) so short skbs with large
hdr->length (e.g. 1538 or 0xffff) are rejected before *pchecksum and
ncsi_calculate_checksum() read out of bounds.
- Simplify ncsi_rsp_handler_gmcma() (Patch 2/2) to check min_payload and
address_count * ETH_ALEN against the already-validated payload length.
- Include discovery method and runtime ARCH=um KASAN reproduction
details in the cover letter and commit messages per pw-bot feedback.
- Note: the pre-existing ncsi_rsp_handler_gp() bounds issue noted by
Sashiko is separate and left for a follow-up patch.
Yuxiao Zhang (2):
net/ncsi: validate response packet lengths against skb
net/ncsi: validate GMCMA address_count against packet length
net/ncsi/ncsi-rsp.c | 43 ++++++++++++++++++++++++++++++++++++++-----
1 file changed, 38 insertions(+), 5 deletions(-)
base-commit: 76fca30e8d34eb151fcf4f363b51c21b7e8c4709
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH net v2 1/2] net/ncsi: validate response packet lengths against skb
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
2026-12-02 3:36 ` [PATCH net v2 2/2] net/ncsi: validate GMCMA address_count against packet length Yuxiao Zhang
1 sibling, 0 replies; 3+ messages in thread
From: Yuxiao Zhang @ 2026-12-02 3:36 UTC (permalink / raw)
To: Samuel Mendoza-Jonas, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Willem de Bruijn
Cc: Paul Fertser, Simon Horman, Joel Stanley, Peter Delevoryas,
Patrick Williams, netdev, Yuxiao Zhang
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH net v2 2/2] net/ncsi: validate GMCMA address_count against packet length
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 ` [PATCH net v2 1/2] net/ncsi: validate response packet lengths against skb Yuxiao Zhang
@ 2026-12-02 3:36 ` Yuxiao Zhang
1 sibling, 0 replies; 3+ messages in thread
From: Yuxiao Zhang @ 2026-12-02 3:36 UTC (permalink / raw)
To: Samuel Mendoza-Jonas, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Willem de Bruijn
Cc: Paul Fertser, Simon Horman, Joel Stanley, Peter Delevoryas,
Patrick Williams, netdev, Yuxiao Zhang
ncsi_rsp_handler_gmcma() iterates up to rsp->address_count (a u8 field
from the network packet, up to 255) over rsp->addresses[] and logs each
6-byte entry without verifying that the validated NC-SI payload actually
holds the fixed GMCMA response header and all advertised MAC addresses.
A malformed or malicious NC-SI Get MC MAC Address response with a small
payload (e.g. length = 8 in a 46-byte Ethernet-padded frame) and a large
address_count (up to 0xff, indexing up to 1530 bytes past addresses[])
triggers a slab out-of-bounds read and leaks adjacent kernel heap memory
into dmesg via netdev_info(), or reads trailing Ethernet padding as
provisioned MAC addresses.
Now that ncsi_validate_rsp_pkt() guarantees the skb linear head covers
the common header, 4-byte-aligned payload, and checksum, validate that
the NC-SI payload length covers both the fixed GMCMA header fields and
all address_count * ETH_ALEN bytes before indexing rsp->addresses[].
Discovered 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 (length=8, checksum=0, address_count=0xff) to
ncsi_rcv_rsp():
BUG: KASAN: slab-out-of-bounds in ncsi_rsp_handler_gmcma+0x212/0x573
Read of size 1 at addr 00000000624d6683 by task kunit_try_catch/30
The buggy address belongs to the object at 00000000624d63c0
which belongs to the cache skbuff_small_head of size 704
The buggy address is located 3 bytes to the right of
allocated 704-byte region [00000000624d63c0, 00000000624d6680)
With this series applied, the same KUnit + KASAN run rejects malformed
GMCMA frames with -EINVAL without triggering KASAN and continues to
parse valid linear and shared non-linear GMCMA responses.
Fixes: b8291cf3d118 ("net/ncsi: Add NC-SI 1.2 Get MC MAC Address command")
Signed-off-by: Yuxiao Zhang <yuxiaozhang@google.com>
---
net/ncsi/ncsi-rsp.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/net/ncsi/ncsi-rsp.c b/net/ncsi/ncsi-rsp.c
index e917cefd408b..23f2715baa8e 100644
--- a/net/ncsi/ncsi-rsp.c
+++ b/net/ncsi/ncsi-rsp.c
@@ -1111,9 +1111,17 @@ static int ncsi_rsp_handler_gmcma(struct ncsi_request *nr)
struct sockaddr_storage *saddr = &ndp->pending_mac;
struct net_device *ndev = ndp->ndev.dev;
struct ncsi_rsp_gmcma_pkt *rsp;
+ size_t min_payload, payload;
int i;
+ min_payload = offsetof(struct ncsi_rsp_gmcma_pkt, addresses) -
+ sizeof(struct ncsi_pkt_hdr);
rsp = (struct ncsi_rsp_gmcma_pkt *)skb_network_header(nr->rsp);
+ payload = ntohs(rsp->rsp.common.length);
+ if (payload < min_payload ||
+ payload - min_payload < (size_t)rsp->address_count * ETH_ALEN)
+ return -EINVAL;
+
ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
netdev_info(ndev, "NCSI: Received %d provisioned MAC addresses\n",
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-02 3:36 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH net v2 1/2] net/ncsi: validate response packet lengths against skb Yuxiao Zhang
2026-12-02 3:36 ` [PATCH net v2 2/2] net/ncsi: validate GMCMA address_count against packet length Yuxiao Zhang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox