From: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
To: netdev@vger.kernel.org
Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
elder@kernel.org, Willem de Bruijn <willemb@google.com>
Subject: [PATCH net-next 3/6] packet: validate TX header length before skb allocation
Date: Tue, 24 Nov 2026 12:05:41 -0500 [thread overview]
Message-ID: <20261124170551.1874487-4-willemdebruijn.kernel@gmail.com> (raw)
In-Reply-To: <20261124170551.1874487-1-willemdebruijn.kernel@gmail.com>
From: Willem de Bruijn <willemb@google.com>
Now that dev_validate_header() is gone, TX header validation only
compares length against hard_header_len and no longer needs the copied
link layer header.
Simplify packet_sendmsg_spkt(), tpacket_snd(), and packet_snd() by
validating against the snapshotted hard_header_len before skb
allocation and copy. This removes the now unreachable len < hhlen
network header reset in packet_sendmsg_spkt() and, since
__packet_snd_vnet_parse() already enforces vnet_hdr.hdr_len <= tp_len,
the redundant hdrlen clamp in tpacket_fill_skb().
A too short packet now fails with -EINVAL before, rather than after,
allocation and copy errors (e.g., -ENOBUFS, -EAGAIN, -EFAULT).
Signed-off-by: Willem de Bruijn <willemb@google.com>
---
net/packet/af_packet.c | 34 +++++++++++++++++++---------------
1 file changed, 19 insertions(+), 15 deletions(-)
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 230b16742346..e8326cade94a 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -2024,8 +2024,13 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
}
/* Keep the allocation-time header length across retry. */
- if (!skb)
+ if (!skb) {
hard_header_len = READ_ONCE(dev->hard_header_len);
+ if (len < hard_header_len || !len) {
+ err = -EINVAL;
+ goto out_unlock;
+ }
+ }
err = -EMSGSIZE;
if (len > dev->mtu + hard_header_len + VLAN_HLEN + extra_len)
@@ -2051,8 +2056,6 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
if (hhlen) {
skb->data -= hhlen;
skb->tail -= hhlen;
- if (len < hhlen)
- skb_reset_network_header(skb);
}
err = memcpy_from_msg(skb_put(skb, len), msg, len);
if (err)
@@ -2060,10 +2063,6 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
goto retry;
}
- if (len < hard_header_len || !skb->len) {
- err = -EINVAL;
- goto out_unlock;
- }
if (len > (dev->mtu + hard_header_len + extra_len) &&
!packet_extra_vlan_len_allowed(dev, skb)) {
err = -EMSGSIZE;
@@ -2637,18 +2636,14 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
if (unlikely(err < 0))
return -EINVAL;
} else if (copylen) {
- int hdrlen = min_t(int, copylen, tp_len);
-
skb_push(skb, hard_header_len);
skb_put(skb, copylen - hard_header_len);
- err = skb_store_bits(skb, 0, data, hdrlen);
+ err = skb_store_bits(skb, 0, data, copylen);
if (unlikely(err))
return err;
- if (hdrlen < hard_header_len)
- return -EINVAL;
- data += hdrlen;
- to_write -= hdrlen;
+ data += copylen;
+ to_write -= copylen;
}
offset = offset_in_page(data);
@@ -2878,6 +2873,10 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
vnet_hdr.hdr_len);
has_vnet_hdr = true;
}
+ if (unlikely(tp_len < reserve)) {
+ tp_len = -EINVAL;
+ goto tpacket_error;
+ }
/* Prevent the byte count from wrapping into a negative errno */
if (unlikely(tp_len > INT_MAX - len_sum))
break;
@@ -3086,6 +3085,11 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
(len > dev->mtu + reserve + VLAN_HLEN + extra_len))
goto out_unlock;
+ if (unlikely(len < reserve)) {
+ err = -EINVAL;
+ goto out_unlock;
+ }
+
err = -ENOBUFS;
hlen = LL_RESERVED_SPACE_EX(dev, hard_header_len);
tlen = dev->needed_tailroom;
@@ -3115,7 +3119,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
if (err)
goto out_free;
- if ((sock->type == SOCK_RAW && len < hard_header_len) || !skb->len) {
+ if (unlikely(!skb->len)) {
err = -EINVAL;
goto out_free;
}
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-11-24 17:05 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 17:05 [PATCH net-next 0/6] net: remove dev_validate_header() and net_device::min_header_len Willem de Bruijn
2026-11-24 17:05 ` [PATCH net-next 1/6] net: ipa: remove ethernet-specific artifacts from modem netdev setup Willem de Bruijn
2026-11-24 17:05 ` [PATCH net-next 2/6] net: remove header_ops->validate and dev_validate_header() Willem de Bruijn
2026-11-24 17:05 ` Willem de Bruijn [this message]
2026-11-24 17:05 ` [PATCH net-next 4/6] packet: reset network header on all headerless devices Willem de Bruijn
2026-11-24 17:05 ` [PATCH net-next 5/6] packet: use Ethernet header length in vlan_get_tci() Willem de Bruijn
2026-11-24 17:05 ` [PATCH net-next 6/6] net: remove unused net_device::min_header_len Willem de Bruijn
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=20261124170551.1874487-4-willemdebruijn.kernel@gmail.com \
--to=willemdebruijn.kernel@gmail.com \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=elder@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.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