[PATCH net-next v1 3/6] ipv4: raw: do not use skb_header_pointer() in icmp_filter()

From
Xuanqiang Luo <hidden>
Date
2026-11-24 13:08:00
Also in
bpf, lkml, mptcp, netfilter-devel
Reply to
Xuanqiang Luo, 2026-11-24
From: Xuanqiang Luo <redacted>

raw_local_deliver() is called for every IPv4 packet delivered locally.

icmp_filter() passes the address of a local struct icmphdr to
skb_header_pointer(). Once inlined, this forces a stack canary
(CONFIG_STACKPROTECTOR_STRONG=y) into raw_local_deliver(), although
only ICMP packets matching a raw socket need the filter.

Prefer pskb_may_pull(), as commit a14d93179045 ("ipv6: do not use
skb_header_pointer() in icmpv6_filter()") did for IPv6.

skb->data already points to the transport header here.

skb->head can change, reload iph pointer in raw_v4_input().

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-95 (-95)
Function                                     old     new   delta
raw_local_deliver                            648     553     -95
Total: Before=24068073, After=24067978, chg -0.00%

Signed-off-by: Xuanqiang Luo <redacted>
---
 net/ipv4/raw.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c
index f33a9dfc5309..f72163699040 100644
--- a/net/ipv4/raw.c
+++ b/net/ipv4/raw.c
@@ -140,16 +140,14 @@ EXPORT_SYMBOL_GPL(raw_v4_match);
  *	0 - deliver
  *	1 - block
  */
-static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
+static int icmp_filter(const struct sock *sk, struct sk_buff *skb)
 {
-	struct icmphdr _hdr;
 	const struct icmphdr *hdr;
 
-	hdr = skb_header_pointer(skb, skb_transport_offset(skb),
-				 sizeof(_hdr), &_hdr);
-	if (!hdr)
+	if (!pskb_may_pull(skb, sizeof(*hdr)))
 		return 1;
 
+	hdr = (const struct icmphdr *)skb->data;
 	if (hdr->type < 32) {
 		__u32 data = raw_sk(sk)->filter.data;
 
@@ -166,10 +164,10 @@ static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
  * RFC 1122: SHOULD pass TOS value up to the transport layer.
  * -> It does. And not only TOS, but all IP header.
  */
-static int raw_v4_input(struct net *net, struct sk_buff *skb,
-			const struct iphdr *iph, int hash)
+static int raw_v4_input(struct net *net, struct sk_buff *skb, int hash)
 {
 	int sdif = inet_sdif(skb);
+	const struct iphdr *iph;
 	struct hlist_head *hlist;
 	int dif = inet_iif(skb);
 	int delivered = 0;
@@ -178,13 +176,19 @@ static int raw_v4_input(struct net *net, struct sk_buff *skb,
 	hlist = &raw_v4_hashinfo.ht[hash];
 	rcu_read_lock();
 	sk_for_each_rcu(sk, hlist) {
+		iph = ip_hdr(skb);
 		if (!raw_v4_match(net, sk, iph->protocol,
 				  iph->saddr, iph->daddr, dif, sdif))
 			continue;
 
 		delivered = 1;
-		if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) &&
-		    ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
+		if (iph->protocol == IPPROTO_ICMP) {
+			if (icmp_filter(sk, skb))
+				continue;
+			/* icmp_filter() might have changed skb->head. */
+			iph = ip_hdr(skb);
+		}
+		if (ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
 				   skb->dev->ifindex, sdif)) {
 			struct sk_buff *clone;
 
@@ -208,8 +212,7 @@ int raw_local_deliver(struct sk_buff *skb, int protocol)
 {
 	struct net *net = dev_net(skb->dev);
 
-	return raw_v4_input(net, skb, ip_hdr(skb),
-			    raw_hashfunc(net, protocol));
+	return raw_v4_input(net, skb, raw_hashfunc(net, protocol));
 }
 
 static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
-- 
2.43.0
Thread (7 messages) 7 messages, 1 author, last reply 15h ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help