๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #3173

Re: [PATCH net-next 1/4] net: add ip4_parse()

#3173
From: netdev-bot+sashiko
Date: Wed, 14 Oct 2026 10:59
104 lines
9368 bytes
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The doc comment on ip4_parse() (include/linux/ip.h) promises more
  than the code checks.
- [Low] API hazard: nhoff is unsigned int and `nhoff + sizeof(struct
  iphdr)` is not checked for overflow.

--

net: add ip4_parse()

This commit adds an inline helper, ip4_parse(), to include/linux/ip.h.
It pulls the fixed IPv4 header at a given offset, checks for version 4
and IHL >= 5, and returns the header together with the transport header
offset. It is meant to replace several open-coded copies of the same
minimal validation in the net stack.

> diff --git a/include/linux/ip.h b/include/linux/ip.h
> index d11c25f5030a0..98012f6d0efb5 100644
> --- a/include/linux/ip.h
> +++ b/include/linux/ip.h
> @@ -56,4 +56,28 @@ static inline void iph_set_totlen(struct iphdr *iph, unsigned int len)
>  {
>  	iph->tot_len = len <= IP_MAX_MTU ? htons(len) : 0;
>  }
> +
> +/*
> + * Pull and minimally validate the IPv4 header at @nhoff.
> + * Returns the header and stores the transport header offset in
> + * @thoff, or NULL if truncated or malformed.  Only the fixed header
> + * is guaranteed linear; callers that need the options or the
> + * transport header must pull further.
> + */

[Severity: Low]
Is "NULL if truncated or malformed" accurate here? ip4_parse() only does
three checks: a pskb_may_pull() of the 20-byte fixed header, version == 4,
and ihl >= 5.

Two kinds of bad header still get a non-NULL iph back:

  - A header cut off inside its options, for example ihl = 15 with only
    20 bytes in the skb.
  - A header whose tot_len is smaller than ihl * 4.

In both cases thoff is stored without being checked against skb->len or
ntohs(iph->tot_len):

	*thoff = nhoff + iph->ihl * sizeof(u32);

The next sentence of the comment says the options may not be linear. It
does not say that thoff can point past the end of the packet or the
datagram.

The callers added later in this series do their own checks on thoff, so
none of them overruns today. Those callers are ip_rcv_core(),
br_validate_ipv4(), nf_reject_iphdr_validate() and
virtio_net_csum_partial_ok_at_offset().

Could the comment say that only the fixed header is validated, and that
callers must check thoff against what they can pull and against tot_len?

> +static inline struct iphdr *ip4_parse(struct sk_buff *skb,
> +				      unsigned int nhoff,
> +				      unsigned int *thoff)
> +{
> +	struct iphdr *iph;
> +
> +	if (!pskb_may_pull(skb, nhoff + sizeof(struct iphdr)))
> +		return NULL;
> +
> +	iph = (struct iphdr *)(skb->data + nhoff);

[Severity: Low]
This isn't a bug in any current caller, but nhoff is an unsigned int and
the sum nhoff + sizeof(struct iphdr) is not checked for overflow.

Suppose a caller passed a negative offset, such as a negative
skb_network_offset(), or a very large one. Could the length passed to
pskb_may_pull() then wrap to a small value, while skb->data + nhoff still
uses the original offset?

No caller in the series passes such an offset:

  - ip_rcv_core(), br_validate_ipv4() and nf_reject_iphdr_validate()
    pass 0.
  - virtio_net_csum_partial_ok() passes ETH_HLEN, a VLAN depth, or a
    non-negative skb_network_offset().

The IPv6 branch next to the ip4_parse() call open-codes the same
pskb_may_pull(skb, nhoff + sizeof(...)) pattern.

Would it be worth documenting that nhoff must be in range, or rejecting
an nhoff that wraps?

[ ... ]

-- 
Sashiko AI review ยท https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1791887415.git.mst%40redhat.com
Technical Details
Message-ID: <179197556754.434549.10151843409625099884@kernel.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-346173-linux-kernel-netdev=dewy.ru
References: <bfd6257f8fc8a5c6cd876b8ebc498b8a4ee0be8e.1791887415.git.mst@redhat.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-346173-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net-next 1/4] net: add ip4_parse()
Date: Wed, 14 Oct 2026 10:59:27 +0000
Organization: Usenet Network
Message-ID: <179197556754.434549.10151843409625099884@kernel.org>
References: <bfd6257f8fc8a5c6cd876b8ebc498b8a4ee0be8e.1791887415.git.mst@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2275340"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:iC4T2MZiKde1O2i9YR/OodYSBsY= sha256:0o/iORl1PNGcVWpvLf68flksY4gdg761pllhKTn58o4=
	sha1:w1yAf02kBI5A/eu5nHD7S5lRMvc= sha256:JnUUoUe1kSDe8L4j7Q1sAGD3NYuMU+xb98YDE9n00b8=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id E01CC1100B8; Wed, 14 Oct 2026 14:00:06 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -1.34
X-Spam-Status: No, score=-1.34 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.24, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001]
	autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id aVb5nYR0wtDt;
	Wed, 14 Oct 2026 13:59:59 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-346173-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 2CDED1100AD; Wed, 14 Oct 2026 13:59:50 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
	by tor.lore.kernel.org (Postfix) with ESMTP id 309EE3211A; Wed, 14 Oct 2026 10:59:33 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 78B724F4743;
	Wed, 14 Oct 2026 10:59:30 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bB8OGdGb"
X-Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A22F412276;
	Wed, 14 Oct 2026 10:59:29 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1791975570; cv=none; b=MMw5PcPkGTLvHWV4GpU07lf6YWdD6NVhcSUY0HriAvuY+EkGUruXxUvTS78d9YQlbdIltcZlffgvT+ReN/RP+4NDZX0hcZ5+gq/yxqevt0TW5M6EbC2TRa/QiXA83B6eG8wn/TmHHTuN/ILe+psVLX5oUe4eV7oaojxAq5nmJSM=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1791975570; c=relaxed/simple;
	bh=6uvmCIdeVyQZ6DXKXSWtOio1ENcHU/d1CYuniVEwkBw=;
	h=Subject:From:To:Cc:Date:Message-ID:In-Reply-To:References:
	 Content-Type:MIME-Version; b=u31i2O3AxjJ8OzV29TOP4ue/M4eSDEIsZpTu3Rpjw0wKTeLagSBrJu5LnFODtbFtavqkBXi4pFqz2P0a42WpR+/a/kvmiIVuPyxTI9FA8Dd9y66u4eif8P92dshURldtRo8/Fk2qcVzSeq3tPVid05xxnIl58OgZdO5kwJSgaB8=
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bB8OGdGb; arc=none smtp.client-ip=100.103.45.18
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0872A1F00893;
	Wed, 14 Oct 2026 10:59:27 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1791975568;
	bh=udGiZ2QiUocnB/gausSU85SeHeMymbjJpcvE7RG+rjQ=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=bB8OGdGbHPyC24LDrAtXrQquFPQkOcX8xD6Fj4g83z4Y5pNoqIa1q/6zyWGnbhMUs
	 7Byy657gBydCr3BV8+IBPLb+gZphdRhjqixKaiXeVGgfk1nkYIfnw6Rgmz+6K4NGb1
	 FAzJGiAO3Cx+qsC6u84iKUV1IuTicl7vLNnGS8uVQO2bNy786fkzzic5Ynh/oaFsVe
	 pOXEK6XFm7t2XWUrx+mXjkwKXl3HiqTVJoztPjBz2M6LmYZZjkn2DuvHljLSXd73Nt
	 +2f6HjEtyXxjnOkTbMjXvizMVGcgynY2/boNgF/UYUcSk5nbkgjDWqsvKwvuf/qNwu
	 5EvzCjQ8bqEdQ==
X-Cc: linux-kernel@vger.kernel.org, willemdebruijn.kernel@gmail.com,
 jasowangio@gmail.com, eperezma@redhat.com, xuanzhuo@linux.alibaba.com,
 andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
 kuba@kernel.org, pabeni@redhat.com, netdev@vger.kernel.org,
 virtualization@lists.linux.dev, maxk@qualcomm.com, rusty@rustcorp.com.au,
 horms@kernel.org
X-In-Reply-To:  <bfd6257f8fc8a5c6cd876b8ebc498b8a4ee0be8e.1791887415.git.mst@redhat.com>
X-sashiko-severity: Low
X-Precedence: bulk
X-Mailing-List: netdev@vger.kernel.org
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>