Skip to contentSkip to conversation

netfilter: use RCU safe kfree for conntrack extensions

PATCH In review Michal Kubecek 4 participants 7 messages Sep 11, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: use RCU safe kfree for conntrack extensions

Commit 68b80f11 (netfilter: nf_nat: fix RCU races) introduced RCU protection for freeing extension data when reallocation moves them to a new location. We need the same protection when freeing them in nf_ct_ext_free() in order to prevent a use-after-free by other threads referencing a NAT extension data via bysource list.

---
 include/net/netfilter/nf_conntrack_extend.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/net/netfilter/nf_conntrack_extend.h +1 -1
index 977bc8a..3313108 100644
@@ -80,7 +80,7 @@ static inline void nf_ct_ext_destroy(struct nf_conn *ct)
 static inline void nf_ct_ext_free(struct nf_conn *ct)
 {
 	if (ct->ext)
-		kfree(ct->ext);
+		kfree_rcu(ct->ext, rcu);
 }
 
 /* Add this type, returns pointer to data or NULL. */
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 6 lines · Michal Kubecek

Commit 68b80f11 (netfilter: nf_nat: fix RCU races) introduced RCU protection for freeing extension data when reallocation moves them to a new location. We need the same protection when freeing them in nf_ct_ext_free() in order to prevent a use-after-free by other threads referencing a NAT extension data via bysource list.

Hi Michal -

coincidentally I've been looking into this area this week due to another bug report (https://bugzilla.kernel.org/show_bug.cgi?id=60853). Looking at your proposed fix, the NAT extension data should have been cleaned from the bysource list in nf_nat_cleanup_conntrack (via __nf_ct_ext_destroy) before reaching the kfree. Would you agree?

The reporter of #60853 suggested adding a synchronize_rcu to the end of the nf_nat_cleanup_conntrack function, which seems sane. I have been trying to reproduce the crash to test that theory.

Are you able to reproduce an OOPS in your testing? Or is there a bug report you are working from?

Phil

#2 lore raw
Quoted 8 lines · Phil Oester
Quoted 6 lines · Michal Kubecek

Commit 68b80f11 (netfilter: nf_nat: fix RCU races) introduced RCU protection for freeing extension data when reallocation moves them to a new location. We need the same protection when freeing them in nf_ct_ext_free() in order to prevent a use-after-free by other threads referencing a NAT extension data via bysource list.

Quoted 4 lines

Hi Michal -

coincidentally I've been looking into this area this week due to another bug report (https://bugzilla.kernel.org/show_bug.cgi?id=60853).

Looking at the initial command, I would say this bug report is actually of the same origin as ours.

Quoted 4 lines

Looking at
your proposed fix, the NAT extension data should have been cleaned from the bysource list in nf_nat_cleanup_conntrack (via __nf_ct_ext_destroy) before reaching the kfree. Would you agree?

It is cleaned from the list but as it is an RCU list, other readers can still be holding pointers to it. We have to wait for the RCU grace period before we can reuse it.

Quoted 2 lines

The reporter of #60853 suggested adding a synchronize_rcu to the end of the nf_nat_cleanup_conntrack function, which seems sane.

That was also my first idea. However, nf_nat_cleanup_conntrack() is called from __nf_ct_ext_destroy() inside a rcu_read_lock() / rcu_read_unlock() block. Even if this block is for a different RCU list, we still cannot call synchronize_rcu() while inside it.

We could call synchronize_rcu() in __nf_ct_ext_destroy() after rcu_read_unlock() but this would IMHO add an unnecessary delay so it is more efficient and more appropriate to wait before the actual kfree() which is the operation that needs to wait for RCU grace period.

Quoted 3 lines

I have been trying to reproduce the crash to test that theory. Are you able to reproduce an OOPS in your testing? Or is there a bug report you are working from?

No, it is a bugreport from our customer. And even that customer encountered it only once so far. Which is not very surprising as to reproduce it, you have to be (un)lucky twice: first to have someone overwrite the area soon enough and second to have someone access the area after it is overwritten. This is IMHO the reason why the reporter cleared the block with memset() for testing purposes.

Michal Kubecek

#3 lore raw
Quoted 8 lines · Michal Kubecek
Quoted 4 lines

Looking at
your proposed fix, the NAT extension data should have been cleaned from the bysource list in nf_nat_cleanup_conntrack (via __nf_ct_ext_destroy) before reaching the kfree. Would you agree?

It is cleaned from the list but as it is an RCU list, other readers can still be holding pointers to it. We have to wait for the RCU grace period before we can reuse it.

Agreed - looks like your fix should work. However, two nits:

1) normally RCU functions have _rcu suffixes. So nf_ct_ext_free should become nf_ct_ext_free_rcu.

2) kfree_rcu was not added to the kernel until 3.0. All of the bug reports I've been looking into (including the original in netfilter bugzilla at http://bugzilla.netfilter.org/show_bug.cgi?id=714) have been reported in 2.6.32 or earlier kernels. So a different fix would need to be backported for -stable. For that, we would probably export __nf_ct_ext_free_rcu from nf_conntrack_extend.c and change the kfree call in nf_ct_ext_free_rcu to call_rcu(&ct->ext->rcu, __nf_ct_ext_free_rcu). Of course the alternative is just to use this fix for both old and new kernels for simplicity.

Quoted 5 lines

No, it is a bugreport from our customer. And even that customer encountered it only once so far. Which is not very surprising as to reproduce it, you have to be (un)lucky twice: first to have someone overwrite the area soon enough and second to have someone access the area after it is overwritten.

Yes, hitting this seems dependent upon phase of the moon.

Phil

#4 lore raw
Quoted 3 lines · Phil Oester

1) normally RCU functions have _rcu suffixes. So nf_ct_ext_free should become nf_ct_ext_free_rcu.

Right. I'll post updated version with the rename tomorrow.

Quoted 7 lines

2) kfree_rcu was not added to the kernel until 3.0. All of the bug reports I've been looking into (including the original in netfilter bugzilla at http://bugzilla.netfilter.org/show_bug.cgi?id=714) have been reported in 2.6.32 or earlier kernels. So a different fix would need to be backported for -stable. For that, we would probably export __nf_ct_ext_free_rcu from nf_conntrack_extend.c and change the kfree call in nf_ct_ext_free_rcu to call_rcu(&ct->ext->rcu, __nf_ct_ext_free_rcu).

Yes, the patch submitted here is against current nf branch. For SLES 11 SP1 (with 2.6.32 kernel), I'm going to use call_rcu() the way original commit 68b80f11 does. IIRC the only pre-3.0 stable branch still maintained is 2.6.32, all others are 3.0 or newer so they have kfree_rcu() and also use it in __nf_ct_ext_add() since commit 1f8d36a1.

Michal Kubecek

#5 lore raw
Quoted 3 lines · Michal Kubecek

Yes, the patch submitted here is against current nf branch. For SLES 11 SP1 (with 2.6.32 kernel), I'm going to use call_rcu() the way original commit 68b80f11 does.

Well, please just submit a patch for current tree, using kfree_rcu()

When doing backport to stable branches, needed adaptation shall be done.

#6 lore raw
In reply to Phil Oester
Quoted 6 lines · Phil Oester
Quoted 5 lines · Michal Kubecek
Quoted 4 lines

Looking at
your proposed fix, the NAT extension data should have been cleaned from the bysource list in nf_nat_cleanup_conntrack (via __nf_ct_ext_destroy) before reaching the kfree. Would you agree?

Quoted 8 lines
Quoted 3 lines

It is cleaned from the list but as it is an RCU list, other readers can still be holding pointers to it. We have to wait for the RCU grace period before we can reuse it.

Agreed - looks like your fix should work. However, two nits:

1) normally RCU functions have _rcu suffixes. So nf_ct_ext_free should become nf_ct_ext_free_rcu.

That postfix is there if the function requires to be called holding rcu read lock, not this case. I'll take this patch.

Quoted 8 lines

2) kfree_rcu was not added to the kernel until 3.0. All of the bug reports I've been looking into (including the original in netfilter bugzilla at http://bugzilla.netfilter.org/show_bug.cgi?id=714) have been reported in 2.6.32 or earlier kernels. So a different fix would need to be backported for -stable. For that, we would probably export __nf_ct_ext_free_rcu from nf_conntrack_extend.c and change the kfree call in nf_ct_ext_free_rcu to call_rcu(&ct->ext->rcu, __nf_ct_ext_free_rcu). Of course the alternative is just to use this fix for both old and new kernels for simplicity.

Either way, we need a specific backport for 2.6.x indeed.

Thanks for tracking up this issue.