From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796182576; b=xnYnqNR37J4Rakx363D8JltVsY27xOW7RA3//vRCstGqx2icTl3tbIbVXL2Vj1voXVOy x766tzsvQBIewpZ7jDt4wk/P35aeMuPEPO38p0a3ZnxG9tMnfvaAv5C1YeKsZoSRPjHl0 klJtDDR8yAFiTCp9Ya+6e+HUObhldJB5CV5cNgZLz1gvAzEdRed6HGPkKNtfTX0vg46Dr BKwYVqAD2sDl98eCFsvLVVTZeCcHqUu1RqpvuKYtQmXs3aSewjsTXx1wX/ro8kak3XM2K 0ocw3fIvafhfO+GxhEuIy8C+IxMK3xBmNyXSWCBzMuCHgYnuTDrBzxDvqs9m8oC3ipA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796182576; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=4/OJ5OOpm4UuSpg1UTSX3WE/KkKgO8+z3X43lOaNnkU=; b=5UA1J8WvcFvlCNVdTfI1BHrXkEsWDyPTEarsQ/NAjOtT/JmU0pwWQge2EnxrU96lywKE eFjGoluEno/mqWL5XzM9wrBhHiNOMa1WXecHt10NLSIwyiK2EpUhz5Hg1mEcVhsGC+dIG x2Z4yDnfjYAHuoCBOQdasazhnp57OXWmZayCH3MW480z2bKj/aFdsK2Tj4GkUuiCIPE+r zyuJZe9SHHgKkpZ/bGr9yLDByMe2HQbm70QggO7jITXSLLISTZZMsEElxwULTMAIICvZI 5ZW6IuKb8LjIr5KxuDVNisnqRcJzl0jdqDlvmY3J1eRWPg1HhFHXS5BadShGpBEEHsw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--yuxiaozhang.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=GcTV1dag; arc=none smtp.remote-ip=209.85.210.70 Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) (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 7A1D9606A696 for ; Wed, 2 Dec 2026 03:36:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7A1D9606A696 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuxiaozhang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=GcTV1dag Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-85d630e451aso11055043a34.2 for ; Tue, 01 Dec 2026 19:36:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796182574; x=1796787374; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=4/OJ5OOpm4UuSpg1UTSX3WE/KkKgO8+z3X43lOaNnkU=; b=GcTV1dagrjwryy8TZBDe/n7Z68gJyPM6wJk5tkSl8NU7OsZ0VA0b+cwSfmzp5nc4Xr rLBSmuIb89uZh5LEO8Gt0XbkZYux4iIPpdJM3QlqAbRgJ3yhGt4mtsCTbnPXfSEC8VOh VgQSMf9Ri4BLFZ++N0gpKHcDvsWtC7C5qG7mJBfPAULHqiFtjAG7PAaDPJG0XbQTw1kL AfB9irUJ1kPp0s0JjLsL0PgblUGSXVWz5Omh77dDH4t4freAsdHPIr6BcckoGPG1ZTLG 7dqL2ZcMcS4vO76MEnKkwJE2l30eNY7vvJ4lQetBd8wczVt2nBStmW7DamdgydGmUVul kHGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796182574; x=1796787374; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=4/OJ5OOpm4UuSpg1UTSX3WE/KkKgO8+z3X43lOaNnkU=; b=ovrxeU7pAoylNy9nOw7rDchpGTnWUPHPLyYtJR5bZMJ4dwuHZxhxPlYUytqKM9zIME LVONdNgHhNeJ03P4bOt8pWtC5gH6nmW9LcqGkE9zlFnZonWkk3ETSkLiK2I0eVE0/4k2 fFqjlU7hyIUbWveHOHmndxEfwktYKE8U+7iw+Q2LLkE/xE1s1OKS0IP2zg1fNS/lR7S6 qTdzNc6jTgETAsdKWI+qHgpiXY8+aJlVQPG7cUWhnXFNS4W3AUrjHm0LmwHG6m1TgndY IXXbLU/G6dOXfIm8GsMvK/zYY/MP5N3Zpb7jrYXYQhvPb/9C/VkPmMLuJUbf+3r/Vik9 uQug== X-Forwarded-Encrypted: i=1; AL+hErFdjsTVXPe16B2qoEYFuraB6gIhjWiiNLB2mIjKe7xiytinsi/3+IaqMyKEExMTchC4SUWoe80=@vger.kernel.org X-Gm-Message-State: APuU4Z+yYIMq+/PNinOZxiyNWg5RffbV2d3sJ2xdJWYk9DMmMWLza9dP kTfstk2lk68pgLsarlMckqmD4RRFOg+lxSnBKDVKwtERgr8r334GAv9u647KT1gG/rmFE7sv0ZS bRJyXcM8ngi7FsyDbaMy942IkBQ== X-Received: from otci7.prod.google.com ([2002:a05:6830:67e7:b0:862:9bce:f1a3]) (user=yuxiaozhang job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6830:25d1:b0:862:9993:2d1a with SMTP id 46e09a7af769-86315a0bc62mr39266422a34.26.1796182574083; Tue, 01 Dec 2026 19:36:14 -0800 (PST) Date: Tue, 1 Dec 2026 19:36:08 -0800 In-Reply-To: <20261202033609.1934503-1-yuxiaozhang@google.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202033609.1934503-1-yuxiaozhang@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202033609.1934503-2-yuxiaozhang@google.com> Subject: [PATCH net v2 1/2] net/ncsi: validate response packet lengths against skb From: Yuxiao Zhang 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@vger.kernel.org, Yuxiao Zhang Content-Type: text/plain; charset="UTF-8" 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 --- 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