Skip to contentSkip to conversation

net: make skb_dst_force return false when dst was cleared

PATCH In review Florian Westphal 2 participants 4 messages Jun 25, 2019 – Jun 26, 2019

Subsystems (4)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 net: make skb_dst_force return false when dst was cleared

XFRM and netfilter don't expect that skb_dst_force() can cause skb to lose its dst entry.

I got a bug report with a skb->dst NULL dereference in netfilter output path. The backtrace contains nf_reinject(), so the dst might have been cleared when skb got queued to userspace.

The xfrm part of this change was done after code inspection, it looks like similar crash could happen here too.

One way to fix this is to add a skb_dst() check right after skb_dst_force() call, but I think its preferable to make the 'dst might get cleared' part of the function explicit.

---
 include/net/dst.h        | 6 +++++-
 net/netfilter/nf_queue.c | 6 +++++-
 net/xfrm/xfrm_policy.c   | 5 ++++-
 3 files changed, 14 insertions(+), 3 deletions(-)
include/net/dst.h +5 -1
index 12b31c602cb0..42cd53d51364 100644
@@ -302,8 +302,9 @@ static inline bool dst_hold_safe(struct dst_entry *dst)
  * @skb: buffer
  *
  * If dst is not yet refcounted and not destroyed, grab a ref on it.
+ * Returns false if skb had a destroyed dst.
  */
-static inline void skb_dst_force(struct sk_buff *skb)
+static inline bool skb_dst_force(struct sk_buff *skb)
 {
 	if (skb_dst_is_noref(skb)) {
 		struct dst_entry *dst = skb_dst(skb);
@@ -313,7 +314,10 @@ static inline void skb_dst_force(struct sk_buff *skb)
 			dst = NULL;
 
 		skb->_skb_refdst = (unsigned long)dst;
+		return dst != NULL;
 	}
+
+	return true;
 }
 
 
net/netfilter/nf_queue.c +5 -1
index b5b2be55ca82..dc8628a919a5 100644
@@ -190,6 +190,11 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
 		goto err;
 	}
 
+	if (!skb_dst_force(skb)) {
+		status = -ENETDOWN;
+		goto err;
+	}
+
 	*entry = (struct nf_queue_entry) {
 		.skb	= skb,
 		.state	= *state,
@@ -198,7 +203,6 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
 	};
 
 	nf_queue_entry_get_refs(entry);
-	skb_dst_force(skb);
 
 	switch (entry->state.pf) {
 	case AF_INET:
net/xfrm/xfrm_policy.c +4 -1
index b1694d5d15d3..5c66c18d5ff5 100644
@@ -2842,7 +2842,10 @@ static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *s
 		return -EAGAIN;
 	}
 
-	skb_dst_force(skb);
+	if (!skb_dst_force(skb)) {
+		kfree_skb(skb);
+		return -ENETDOWN;
+	}
 
 	spin_lock_bh(&pq->hold_queue.lock);
 
Signature · 1 line
2.21.0
#1 lore raw
Quoted 38 lines · Florian Westphal

XFRM and netfilter don't expect that skb_dst_force() can cause skb to lose its dst entry.

I got a bug report with a skb->dst NULL dereference in netfilter output path. The backtrace contains nf_reinject(), so the dst might have been cleared when skb got queued to userspace.

The xfrm part of this change was done after code inspection, it looks like similar crash could happen here too.

One way to fix this is to add a skb_dst() check right after skb_dst_force() call, but I think its preferable to make the 'dst might get cleared' part of the function explicit.

---
 include/net/dst.h        | 6 +++++-
 net/netfilter/nf_queue.c | 6 +++++-
 net/xfrm/xfrm_policy.c   | 5 ++++-
 3 files changed, 14 insertions(+), 3 deletions(-)
include/net/dst.h +2 -1
index 12b31c602cb0..42cd53d51364 100644
@@ -302,8 +302,9 @@ static inline bool dst_hold_safe(struct dst_entry *dst)
  * @skb: buffer
  *
  * If dst is not yet refcounted and not destroyed, grab a ref on it.
+ * Returns false if skb had a destroyed dst.
 
  */
-static inline void skb_dst_force(struct sk_buff *skb)
+static inline bool skb_dst_force(struct sk_buff *skb)
 {
 	if (skb_dst_is_noref(skb)) {
include/net/dst.h +0 -0
 		struct dst_entry *dst = skb_dst(skb);

include/net/dst.h static inline void skb_dst_force(struct sk_buff *skb) · line 320

Quoted 7 lines
include/net/dst.h +3 -0
@@ -313,7 +314,10 @@ static inline void skb_dst_force(struct sk_buff *skb)
 			dst = NULL;
 
 		skb->_skb_refdst = (unsigned long)dst;
+		return dst != NULL;
 	}
+
+	return true;

This will return true, even if skb has a NULL dst.

Say if we have two skb_dst_force() calls for some reason on the same skb, only the first one will return false.

author maintainer
#2 lore raw
Quoted 8 lines · Eric Dumazet
Quoted 8 lines
include/net/dst.h +1 -1
-static inline void skb_dst_force(struct sk_buff *skb)
+static inline bool skb_dst_force(struct sk_buff *skb)
 {
 	if (skb_dst_is_noref(skb)) {
 		struct dst_entry *dst = skb_dst(skb);
@@ -313,7 +314,10 @@ static inline void skb_dst_force(struct sk_buff *skb)
 			dst = NULL;
 
Quoted 7 lines

include/net/dst.h static inline void skb_dst_force(struct sk_buff *skb) · line 320

Quoted 5 lines
include/net/dst.h +3 -0
 		skb->_skb_refdst = (unsigned long)dst;
+		return dst != NULL;
 	}
+
+	return true;

This will return true, even if skb has a NULL dst.

Yes, that was intentional -- it should return false to let caller know that no reference could be obtained and that the dst was invalidated as a result.

Quoted 2 lines

Say if we have two skb_dst_force() calls for some reason on the same skb, only the first one will return false.

What would you suggest instead?

Alternative is something like

if (skb_dst(skb)) {
	skb_dst_force(skb);
	if (!skb_dst(skb)) {
		kfree_skb(skb);
		goto err;
	}
}

... i find this a bit ugly.

#3 lore raw
Quoted 17 lines · Florian Westphal
Quoted 8 lines · Eric Dumazet
Quoted 8 lines
include/net/dst.h +1 -1
-static inline void skb_dst_force(struct sk_buff *skb)
+static inline bool skb_dst_force(struct sk_buff *skb)
 {
 	if (skb_dst_is_noref(skb)) {
 		struct dst_entry *dst = skb_dst(skb);
@@ -313,7 +314,10 @@ static inline void skb_dst_force(struct sk_buff *skb)
 			dst = NULL;
 
Quoted 7 lines

include/net/dst.h static inline void skb_dst_force(struct sk_buff *skb) · line 320

Quoted 5 lines
include/net/dst.h +3 -0
 		skb->_skb_refdst = (unsigned long)dst;
+		return dst != NULL;
 	}
+
+	return true;

This will return true, even if skb has a NULL dst.

Quoted 3 lines

Yes, that was intentional -- it should return false to let caller know that no reference could be obtained and that the dst was invalidated as a result.

Problem is that some callers ignore skb_dst_force() return value.

Quoted 8 lines
Quoted 3 lines

Say if we have two skb_dst_force() calls for some reason on the same skb, only the first one will return false.

What would you suggest instead?

Alternative is something like

Quoted 7 lines
if (skb_dst(skb)) {
	skb_dst_force(skb);
	if (!skb_dst(skb)) {
		kfree_skb(skb);
		goto err;
	}
}

Simply change

return true;

by

return skb->_skb_refdst != 0UL;