Skip to content

Commit b60a773

Browse files
Florian Westphaldavem330
authored andcommitted
net: make skb_dst_force return true when dst is refcounted
netfilter did not 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. Other users were fixed via if (skb_dst(skb)) { skb_dst_force(skb); if (!skb_dst(skb)) goto handle_err; } But I think its preferable to make the 'dst might be cleared' part of the function explicit. In netfilter case, skb with a null dst is expected when queueing in prerouting hook, so drop skb for the other hooks. v2: v1 of this patch returned true in case skb had no dst entry. Eric said: Say if we have two skb_dst_force() calls for some reason on the same skb, only the first one will return false. This now returns false even when skb had no dst, as per Erics suggestion, so callers might need to check skb_dst() first before skb_dst_force(). Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 9b6c088 commit b60a773

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎include/net/dst.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ static inline bool dst_hold_safe(struct dst_entry *dst)
302302
* @skb: buffer
303303
*
304304
* If dst is not yet refcounted and not destroyed, grab a ref on it.
305+
* Returns true if dst is refcounted.
305306
*/
306-
static inline void skb_dst_force(struct sk_buff *skb)
307+
static inline bool skb_dst_force(struct sk_buff *skb)
307308
{
308309
if (skb_dst_is_noref(skb)) {
309310
struct dst_entry *dst = skb_dst(skb);
@@ -314,6 +315,8 @@ static inline void skb_dst_force(struct sk_buff *skb)
314315

315316
skb->_skb_refdst = (unsigned long)dst;
316317
}
318+
319+
return skb->_skb_refdst != 0UL;
317320
}
318321

319322

‎net/netfilter/nf_queue.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
190190
goto err;
191191
}
192192

193+
if (!skb_dst_force(skb) && state->hook != NF_INET_PRE_ROUTING) {
194+
status = -ENETDOWN;
195+
goto err;
196+
}
197+
193198
*entry = (struct nf_queue_entry) {
194199
.skb = skb,
195200
.state = *state,
@@ -198,7 +203,6 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
198203
};
199204

200205
nf_queue_entry_get_refs(entry);
201-
skb_dst_force(skb);
202206

203207
switch (entry->state.pf) {
204208
case AF_INET:

0 commit comments

Comments
 (0)