Skip to content

netfilter: nf_queue: rework refcount handling

Florian Westphal netfilter-devel 1 version Applied

Status from this message: Applied.

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest4 patches 1 replyApplied

Patches in v1 Applied

#Subject and filesSizeRepliesApplied
1/4netfilter: nf_queue: make nf_queue_entry_release_refs staticinclude/net/netfilter/nf_queue.h net/netfilter/nf_queue.c net/netfilter/nfnetlink_queue.c+11 -110 replies
2/4netfilter: nf_queue: place bridge physports into queue_entry structinclude/net/netfilter/nf_queue.h net/netfilter/nf_queue.c+27 -310 replies
3/4netfilter: nf_queue: do not release refcouts until nf_reinject is donenet/netfilter/nf_queue.c+2 -40 replies
4/4netfilter: nf_queue: prefer nf_queue_entry_freenet/netfilter/nf_queue.c+9 -180 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH nf-next 0/4] netfilter: nf_queue: rework refcount handling

running nft_queue.sh selftest with refcount debugging enabled triggers following splat:

------------[ cut here ]------------
refcount_t: addition on 0; use-after-free.
WARNING: CPU: 0 PID: 2441 at lib/refcount.c:25 refcount_warn_saturate+0xbc/0x110
RIP: 0010:refcount_warn_saturate+0xbc/0x110
[..]
Call Trace:
 nf_queue_entry_get_refs+0x194/0x1b0
 nf_queue+0x38b/0x640
 nf_reinject+0x264/0x280
 nfqnl_recv_verdict+0x5d5/0x920
 nfnetlink_rcv_msg+0x27a/0x460

This is because nf_queue uses following pattern:
nf_queue_entry_get_refs()
nf_queue() // leave rcu protection
// nfnetlink, wait for verdict
// sk might be closed now
nf_reinject // reenter rcu protection
nf_queue_entry_release_refs // refcount can drop to 0 // iterate/call remaining hooks and okfn

If the hook iteration results in another nf_queue() call, above splat might be triggered.

This series fixes this by deferring the call to nf_queue_entry_release_refs() until after the hook iteration/okfn returns; i.e. another nf_queue invocation from nf_reinject path will not observe a zero refcount.

This series also applies to nf, but given we're a bit further along in release cycle nf-next might be better; this fix isn't simple, unfortunately.