Skip to content

Commit

Permalink
netfilter: nf_queue: handle socket prefetch
Browse files Browse the repository at this point in the history
commit 3b836da upstream.

In case someone combines bpf socket assign and nf_queue, then we will
queue an skb who references a struct sock that did not have its
reference count incremented.

As we leave rcu protection, there is no guarantee that skb->sk is still
valid.

For refcount-less skb->sk case, try to increment the reference count
and then override the destructor.

In case of failure we have two choices: orphan the skb and 'delete'
preselect or let nf_queue() drop the packet.

Do the latter, it should not happen during normal operation.

Fixes: cf7fbe6 ("bpf: Add socket assign support")
Acked-by: Joe Stringer <joe@cilium.io>
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Florian Westphal authored and gregkh committed Mar 8, 2022
1 parent 4d05239 commit 81f817f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions net/netfilter/nf_queue.c
Expand Up @@ -189,6 +189,18 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
break;
}

if (skb_sk_is_prefetched(skb)) {
struct sock *sk = skb->sk;

if (!sk_is_refcounted(sk)) {
if (!refcount_inc_not_zero(&sk->sk_refcnt))
return -ENOTCONN;

/* drop refcount on skb_orphan */
skb->destructor = sock_edemux;
}
}

entry = kmalloc(sizeof(*entry) + route_key_size, GFP_ATOMIC);
if (!entry)
return -ENOMEM;
Expand Down

0 comments on commit 81f817f

Please sign in to comment.