* [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; 4+ 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] 4+ 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-03 3:38 ` netdev-bot+sashiko 2026-12-02 3:36 ` [PATCH net v2 2/2] net/ncsi: validate GMCMA address_count against packet length Yuxiao Zhang 1 sibling, 1 reply; 4+ 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] 4+ messages in thread
* Re: [PATCH net v2 1/2] net/ncsi: validate response packet lengths against skb 2026-12-02 3:36 ` [PATCH net v2 1/2] net/ncsi: validate response packet lengths against skb Yuxiao Zhang @ 2026-12-03 3:38 ` netdev-bot+sashiko 0 siblings, 0 replies; 4+ messages in thread From: netdev-bot+sashiko @ 2026-12-03 3:38 UTC (permalink / raw) To: yuxiaozhang Cc: sam, davem, edumazet, kuba, pabeni, willemb, fercerpav, horms, joel, peter, patrick, netdev Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider. Critical: 0 · High: 0 (2) · Medium: 1 · Low: 1 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [Medium] The fix is incomplete on the AEN branch of ncsi_rcv_rsp(). - [Low] The commit message says 'ALIGN(payload, 4) on an unsigned short wraps to 0 for payload values 65533..65535' and gives that as the… Pre-existing issues: - [High] This problem wasn't introduced by this patch, but the variable- length response handlers the commit message names (GP, OEM) still read… - [High] This problem wasn't introduced by this patch, but ncsi_rsp_handler_gp(), one of the variable-length handlers named in… -- net/ncsi: validate response packet lengths against skb This patch unshares the NCSI response skb and pulls the common header in ncsi_rcv_rsp(). It also makes ncsi_validate_rsp_pkt() require a minimum payload and pull the full header, the aligned payload and the checksum before reading them. Log messages after validation now use nrh->type instead of hdr->type. > Additionally, ALIGN(payload, 4) on an unsigned short wraps to 0 > for payload values 65533..65535, and dereferencing hdr->type after [Severity: Low] Is this part of the commit message accurate? ALIGN(x, a) expands to __ALIGN_KERNEL_MASK(x, (__typeof__(x))(a) - 1), which is ((x) + (mask)) & ~(mask). When x is an unsigned short, both operands are promoted to int. For 65533..65535, x + 3 gives 65536..65538, and & ~3 gives 65536, not 0. The old code used the result directly in pointer arithmetic. The new code stores it in a size_t. So there was no wrap with or without the size_t cast. The 65533..65535 KUnit cases appear to be rejected only because the skb is shorter than pkt_len. Could the commit message drop or reword the wraparound claim? Since the patch carries a Fixes: tag, it would help backporters to know exactly what is being fixed. > diff --git a/net/ncsi/ncsi-rsp.c b/net/ncsi/ncsi-rsp.c > index 84cc8ac0ca2f8..e917cefd408b1 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; > + } [Severity: High] This is a pre-existing issue and was not introduced by this patch. The GP and OEM variable-length handlers named in the commit message still read past the region this validation guarantees. For payload == -1 types, the payload comes from hdr->length. A response with hdr->length = 4 passes this check, and only 24 bytes are pulled. After that, ncsi_rsp_handler_gp() reads link_mode, bc_mode, valid_modes and aen_mode up to offset 48. It then walks the filter tables: ncsi_rsp_handler_gp() { ... pdata = (unsigned char *)rsp + 48; enable = rsp->mac_enable; ... for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) { ... for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) { ... } None of these reads are compared with hdr->length or skb->len. With mac_cnt = vlan_cnt = 255, the handler reads about 2088 bytes past the network header. That is beyond the 1536-byte RX buffer used by ftgmac100. ncsi_rsp_handler_oem_gma() has the same pattern: memcpy(saddr->__data, &rsp->data[mac_addr_off], ETH_ALEN); For Broadcom, rsp->data starts at offset 24 and mac_addr_off is 28, so this reads bytes 52..57 without checking hdr->length. The bytes it reads end up in ndp->pending_mac and are applied as the interface MAC. Because ncsi_rcv_rsp() does not check the response type against nr->cmd, any outstanding request id is enough to reach these handlers. The second patch in this series ("net/ncsi: validate GMCMA address_count against packet length") adds a check based on hdr->length to ncsi_rsp_handler_gmcma() only. Should GP and OEM get the same treatment? [Severity: High] This is also a pre-existing issue, not something this patch introduced. ncsi_rsp_handler_gp() can write past heap allocations, because its loops are bounded only by the u8 counts in the packet: for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) { assign_bit(i, bitmap, enable & (0x1 << i)); memcpy(&ncmf->addrs[i * ETH_ALEN], pdata, ETH_ALEN); } ... for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) { assign_bit(i, bitmap, enable & (0x1 << i)); ncvf->vids[i] = ntohs(*(__be16 *)pdata); } ncmf->addrs and ncvf->vids were allocated in ncsi_rsp_handler_gc() using counts from an earlier, independent GC response: size = (rsp->uc_cnt + rsp->mc_cnt + rsp->mixed_cnt) * ETH_ALEN; nc->mac_filter.addrs = kzalloc(size, GFP_ATOMIC); GP never compares mac_cnt with n_uc + n_mc + n_mixed. It never compares vlan_cnt with n_vids, or either count with the width of the u64 bitmap. In struct ncsi_channel_mac_filter, the u64 bitmap is followed directly by the addrs pointer: struct ncsi_channel_mac_filter { u8 n_uc; u8 n_mc; u8 n_mixed; u64 bitmap; unsigned char *addrs; }; Can assign_bit() with i >= 64 overwrite ncmf->addrs, so the next memcpy() writes through a corrupted pointer? The vlan filter struct has the same layout. If GC reported zero filters, addrs is ZERO_SIZE_PTR. If no GC was handled for the channel, addrs and vids are NULL. GP is unchanged at the end of this series. [ ... ] > @@ -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) [Severity: Medium] Is the AEN branch still exposed to the same short or non-linear skb problem? This pull guarantees only sizeof(struct ncsi_pkt_hdr), which is 16 bytes. The skb is then passed to ncsi_aen_handler(), which reads further: ncsi_aen_handler() { ... h = (struct ncsi_aen_pkt_hdr *)skb_network_header(skb); for (i = 0; i < ARRAY_SIZE(ncsi_aen_handlers); i++) { if (ncsi_aen_handlers[i].type == h->type) { ... } struct ncsi_aen_pkt_hdr is 20 bytes, and h->type sits at offset 19. ncsi_validate_aen_pkt() only compares h->common.length with the fixed payload. After that comparison it reads the checksum and computes the checksum over the packet: pchecksum = (__be32 *)((void *)(h + 1) + payload - 4); if (ntohl(*pchecksum) == 0) return 0; checksum = ncsi_calculate_checksum((unsigned char *)h, sizeof(*h) + payload - 4); None of this is checked against skb->len or skb_headlen(). The patch removes this same dereference and computation from ncsi_validate_rsp_pkt(). With ftgmac100's linear 1536-byte buffers, these reads return stale data from inside the allocation. A driver that produces short or non-linear heads, which is the case this patch says it handles, would read out of bounds. Would it make sense to add pskb_may_pull(skb, sizeof(struct ncsi_aen_pkt_hdr)) before the AEN type lookup, and to pull sizeof(*h) + payload in ncsi_validate_aen_pkt()? -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202033609.1934503-1-yuxiaozhang%40google.com ^ permalink raw reply [flat|nested] 4+ 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; 4+ 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] 4+ messages in thread
end of thread, other threads:[~2026-12-03 3:38 UTC | newest] Thread overview: 4+ 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-03 3:38 ` netdev-bot+sashiko 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