Skip to contentSkip to conversation

netfilter: nf_tables: call skb_valid_dst() before skb_dst()

PATCH Superseded Pablo Neira Ayuso 2 participants 3 messages Jul 28 – Jul 29

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nf_tables: call skb_valid_dst() before skb_dst()

When fetching the dst_entry from the skb, check if it valid, ie. this is not a template dst.

---
 net/netfilter/nft_meta.c | 6 ++++--
 net/netfilter/nft_rt.c   | 6 ++++--
 net/netfilter/nft_xfrm.c | 9 ++++++++-
 3 files changed, 16 insertions(+), 5 deletions(-)
net/netfilter/nft_meta.c +4 -2
index 0a43e0787a68..01cfbaa36525 100644
@@ -20,6 +20,7 @@
 #include <net/dst.h>
 #include <net/ip.h>
 #include <net/sock.h>
+#include <net/dst_metadata.h>
 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
 #include <net/netfilter/nf_tables.h>
 #include <net/netfilter/nf_tables_core.h>
@@ -279,11 +280,12 @@ static bool nft_meta_get_eval_ifname(enum nft_meta_keys key, u32 *dest,
 static noinline bool
 nft_meta_get_eval_rtclassid(const struct sk_buff *skb, u32 *dest)
 {
-	const struct dst_entry *dst = skb_dst(skb);
+	const struct dst_entry *dst;
 
-	if (!dst)
+	if (!skb_valid_dst(skb))
 		return false;
 
+	dst = skb_dst(skb);
 	*dest = dst->tclassid;
 	return true;
 }
net/netfilter/nft_rt.c +4 -2
index aeb0094eafd8..841c863a08db 100644
@@ -8,6 +8,7 @@
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>
 #include <net/dst.h>
+#include <net/dst_metadata.h>
 #include <net/ip6_route.h>
 #include <net/route.h>
 #include <net/netfilter/nf_tables.h>
@@ -59,10 +60,11 @@ void nft_rt_get_eval(const struct nft_expr *expr,
 	u32 *dest = &regs->data[priv->dreg];
 	const struct dst_entry *dst;
 
-	dst = skb_dst(skb);
-	if (!dst)
+	if (!skb_valid_dst(skb))
 		goto err;
 
+	dst = skb_dst(skb);
+
 	switch (priv->key) {
 #ifdef CONFIG_IP_ROUTE_CLASSID
 	case NFT_RT_CLASSID:
net/netfilter/nft_xfrm.c +8 -1
index 8cec43064319..c8bba697f993 100644
@@ -12,6 +12,7 @@
 #include <linux/netfilter/nf_tables.h>
 #include <net/netfilter/nf_tables_core.h>
 #include <net/netfilter/nf_tables.h>
+#include <net/dst_metadata.h>
 #include <linux/in.h>
 #include <net/xfrm.h>
 
@@ -177,9 +178,15 @@ static void nft_xfrm_get_eval_out(const struct nft_xfrm *priv,
 				  struct nft_regs *regs,
 				  const struct nft_pktinfo *pkt)
 {
-	const struct dst_entry *dst = skb_dst(pkt->skb);
+	const struct dst_entry *dst;
 	int i;
 
+	if (!skb_valid_dst(pkt->skb)) {
+		regs->verdict.code = NFT_BREAK;
+		return;
+	}
+
+	dst = skb_dst(pkt->skb);
 	for (i = 0; dst && dst->xfrm;
 	     dst = ((const struct xfrm_dst *)dst)->child, i++) {
 		if (i < priv->spnum)
Signature · 1 line
2.47.3
#1 lore raw
Quoted 4 lines · Pablo Neira Ayuso

When fetching the dst_entry from the skb, check if it valid, ie. this is not a template dst.

Hi Pablo,

I used a coccinelle script to see if this pattern is present in other places. So far in Netfilter I have found this in net/ipv6/netfilter/nf_reject_ipv6.c:nf_send_reset6()

        if (!skb_dst(oldskb)) {
                nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false);
                if (!dst)
                        return;
                skb_dst_set(oldskb, dst);
        }

If I am not wrong the same problem happens here too.

Quoted 88 lines
---
  net/netfilter/nft_meta.c | 6 ++++--
  net/netfilter/nft_rt.c   | 6 ++++--
  net/netfilter/nft_xfrm.c | 9 ++++++++-
  3 files changed, 16 insertions(+), 5 deletions(-)
net/netfilter/nft_meta.c +4 -2
index 0a43e0787a68..01cfbaa36525 100644
@@ -20,6 +20,7 @@
  #include <net/dst.h>
  #include <net/ip.h>
  #include <net/sock.h>
+#include <net/dst_metadata.h>
  #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
  #include <net/netfilter/nf_tables.h>
  #include <net/netfilter/nf_tables_core.h>
@@ -279,11 +280,12 @@ static bool nft_meta_get_eval_ifname(enum nft_meta_keys key, u32 *dest,
  static noinline bool
  nft_meta_get_eval_rtclassid(const struct sk_buff *skb, u32 *dest)
  {
-	const struct dst_entry *dst = skb_dst(skb);
+	const struct dst_entry *dst;
 
-	if (!dst)
+	if (!skb_valid_dst(skb))
  		return false;
 
+	dst = skb_dst(skb);
  	*dest = dst->tclassid;
  	return true;
  }
net/netfilter/nft_rt.c +4 -2
index aeb0094eafd8..841c863a08db 100644
@@ -8,6 +8,7 @@
  #include <linux/netfilter.h>
  #include <linux/netfilter/nf_tables.h>
  #include <net/dst.h>
+#include <net/dst_metadata.h>
  #include <net/ip6_route.h>
  #include <net/route.h>
  #include <net/netfilter/nf_tables.h>
@@ -59,10 +60,11 @@ void nft_rt_get_eval(const struct nft_expr *expr,
  	u32 *dest = &regs->data[priv->dreg];
  	const struct dst_entry *dst;
 
-	dst = skb_dst(skb);
-	if (!dst)
+	if (!skb_valid_dst(skb))
  		goto err;
 
+	dst = skb_dst(skb);
+
  	switch (priv->key) {
  #ifdef CONFIG_IP_ROUTE_CLASSID
  	case NFT_RT_CLASSID:
net/netfilter/nft_xfrm.c +8 -1
index 8cec43064319..c8bba697f993 100644
@@ -12,6 +12,7 @@
  #include <linux/netfilter/nf_tables.h>
  #include <net/netfilter/nf_tables_core.h>
  #include <net/netfilter/nf_tables.h>
+#include <net/dst_metadata.h>
  #include <linux/in.h>
  #include <net/xfrm.h>
 
@@ -177,9 +178,15 @@ static void nft_xfrm_get_eval_out(const struct nft_xfrm *priv,
  				  struct nft_regs *regs,
  				  const struct nft_pktinfo *pkt)
  {
-	const struct dst_entry *dst = skb_dst(pkt->skb);
+	const struct dst_entry *dst;
  	int i;
 
+	if (!skb_valid_dst(pkt->skb)) {
+		regs->verdict.code = NFT_BREAK;
+		return;
+	}
+
+	dst = skb_dst(pkt->skb);
  	for (i = 0; dst && dst->xfrm;
  	     dst = ((const struct xfrm_dst *)dst)->child, i++) {
  		if (i < priv->spnum)
author maintainer
#2 lore raw

Hi Fernando,

Quoted 12 lines · Fernando Fernandez Mancera
Quoted 4 lines · Pablo Neira Ayuso

When fetching the dst_entry from the skb, check if it valid, ie. this is not a template dst.

Hi Pablo,

I used a coccinelle script to see if this pattern is present in other places. So far in Netfilter I have found this in net/ipv6/netfilter/nf_reject_ipv6.c:nf_send_reset6()

Quoted 8 lines
        if (!skb_dst(oldskb)) {
                nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false);
                if (!dst)
                        return;
                skb_dst_set(oldskb, dst);
        }

If I am not wrong the same problem happens here too.

Good point.

It was intentional, I thought this is never called from netdev ingress/egress.

I can expand the commit message to explicitly mention that the intention is to check for valid dst in the netdev/ingress,egrees paths where the template dst can be seen.

But, after second glance, this can be indeed called from NF_INET_INGRESS.

So this chunk is really needed, I will post v2, thanks!