Skip to contentSkip to conversation

netfilter: xt_hashlimit: unregister proc file before releasing mutex

PATCH In review Cong Wang 2 participants 7 messages Feb 13, 2020 – Feb 26, 2020

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 netfilter: xt_hashlimit: unregister proc file before releasing mutex

Before releasing the global mutex, we only unlink the hashtable from the hash list, its proc file is still not unregistered at this point. So syzbot could trigger a race condition where a parallel htable_create() could register the same file immediately after the mutex is released.

Move htable_remove_proc_entry() back to mutex protection to fix this. And, fold htable_destroy() into htable_put() to make the code slightly easier to understand.

Reported-and-tested-by [email protected]
Fixes c4a3922d2d20 ("netfilter: xt_hashlimit: reduce hashlimit_mutex scope for htable_put()")
Cc Florian Westphal <[email protected]>
Cc Pablo Neira Ayuso <[email protected]>
---
 net/netfilter/xt_hashlimit.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
net/netfilter/xt_hashlimit.c +6 -10
index 7a2c4b8408c4..8c835ad63729 100644
@@ -402,15 +402,6 @@ static void htable_remove_proc_entry(struct xt_hashlimit_htable *hinfo)
 		remove_proc_entry(hinfo->name, parent);
 }
 
-static void htable_destroy(struct xt_hashlimit_htable *hinfo)
-{
-	cancel_delayed_work_sync(&hinfo->gc_work);
-	htable_remove_proc_entry(hinfo);
-	htable_selective_cleanup(hinfo, true);
-	kfree(hinfo->name);
-	vfree(hinfo);
-}
-
 static struct xt_hashlimit_htable *htable_find_get(struct net *net,
 						   const char *name,
 						   u_int8_t family)
@@ -432,8 +423,13 @@ static void htable_put(struct xt_hashlimit_htable *hinfo)
 {
 	if (refcount_dec_and_mutex_lock(&hinfo->use, &hashlimit_mutex)) {
 		hlist_del(&hinfo->node);
+		htable_remove_proc_entry(hinfo);
 		mutex_unlock(&hashlimit_mutex);
-		htable_destroy(hinfo);
+
+		cancel_delayed_work_sync(&hinfo->gc_work);
+		htable_selective_cleanup(hinfo, true);
+		kfree(hinfo->name);
+		vfree(hinfo);
 	}
 }
 
Signature · 1 line
2.21.1
#1 lore raw
Quoted 9 lines · Cong Wang

Before releasing the global mutex, we only unlink the hashtable from the hash list, its proc file is still not unregistered at this point. So syzbot could trigger a race condition where a parallel htable_create() could register the same file immediately after the mutex is released.

Move htable_remove_proc_entry() back to mutex protection to fix this. And, fold htable_destroy() into htable_put() to make the code slightly easier to understand.

Probably revert previous one?

author
#2 lore raw
Quoted 8 lines · Pablo Neira Ayuso
Quoted 6 lines · Cong Wang

Before releasing the global mutex, we only unlink the hashtable from the hash list, its proc file is still not unregistered at this point. So syzbot could trigger a race condition where a parallel htable_create() could register the same file immediately after the mutex is released.

Quoted 5 lines
Quoted 3 lines

Move htable_remove_proc_entry() back to mutex protection to fix this. And, fold htable_destroy() into htable_put() to make the code slightly easier to understand.

Probably revert previous one?

The hung task could appear again if we move the cleanup back under mutex.

#3 lore raw
Quoted 9 lines · Cong Wang
Quoted 8 lines · Pablo Neira Ayuso
Quoted 6 lines · Cong Wang

Before releasing the global mutex, we only unlink the hashtable from the hash list, its proc file is still not unregistered at this point. So syzbot could trigger a race condition where a parallel htable_create() could register the same file immediately after the mutex is released.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Move htable_remove_proc_entry() back to mutex protection to fix this. And, fold htable_destroy() into htable_put() to make the code slightly easier to understand.

Probably revert previous one?

The hung task could appear again if we move the cleanup back under mutex.

How could the hung task appear again by reverting
c4a3922d2d20c710f827? Please elaborate.

Thanks.

author
#4 lore raw
Quoted 15 lines · Pablo Neira Ayuso
Quoted 13 lines · Cong Wang
Quoted 12 lines · Pablo Neira Ayuso
Quoted 9 lines · Cong Wang

Before releasing the global mutex, we only unlink the hashtable from the hash list, its proc file is still not unregistered at this point. So syzbot could trigger a race condition where a parallel htable_create() could register the same file immediately after the mutex is released.

Move htable_remove_proc_entry() back to mutex protection to fix this. And, fold htable_destroy() into htable_put() to make the code slightly easier to understand.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Probably revert previous one?

The hung task could appear again if we move the cleanup back under mutex.

How could the hung task appear again by reverting
c4a3922d2d20c710f827? Please elaborate.

Because the cfg.max could be as large as 8*HASHLIMIT_MAX_SIZE:

311 if (hinfo->cfg.max == 0)
312 hinfo->cfg.max = 8 * hinfo->cfg.size;
313 else if (hinfo->cfg.max < hinfo->cfg.size)
314 hinfo->cfg.max = hinfo->cfg.size;

Not sure whether we can finish cleaning up 8*HASHLIMIT_MAX_SIZE entries within the time a hung task tolerates. This largely depends on how much contention the spinlock has, at least I don't want to bet on it.

Thanks.

#5 lore raw
Quoted 31 lines · Cong Wang
Quoted 15 lines · Pablo Neira Ayuso
Quoted 13 lines · Cong Wang
Quoted 12 lines · Pablo Neira Ayuso
Quoted 9 lines · Cong Wang

Before releasing the global mutex, we only unlink the hashtable from the hash list, its proc file is still not unregistered at this point. So syzbot could trigger a race condition where a parallel htable_create() could register the same file immediately after the mutex is released.

Move htable_remove_proc_entry() back to mutex protection to fix this. And, fold htable_destroy() into htable_put() to make the code slightly easier to understand.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Probably revert previous one?

The hung task could appear again if we move the cleanup back under mutex.

How could the hung task appear again by reverting
c4a3922d2d20c710f827? Please elaborate.

Because the cfg.max could be as large as 8*HASHLIMIT_MAX_SIZE:

311 if (hinfo->cfg.max == 0)
312 hinfo->cfg.max = 8 * hinfo->cfg.size;
313 else if (hinfo->cfg.max < hinfo->cfg.size)
314 hinfo->cfg.max = hinfo->cfg.size;

Quoted 4 lines

Not sure whether we can finish cleaning up 8*HASHLIMIT_MAX_SIZE entries within the time a hung task tolerates. This largely depends on how much contention the spinlock has, at least I don't want to bet on it.

Please, resend. Thanks.

#6 lore raw
Quoted 32 lines · Pablo Neira Ayuso
Quoted 31 lines · Cong Wang
Quoted 15 lines · Pablo Neira Ayuso
Quoted 13 lines · Cong Wang
Quoted 12 lines · Pablo Neira Ayuso
Quoted 9 lines · Cong Wang

Before releasing the global mutex, we only unlink the hashtable from the hash list, its proc file is still not unregistered at this point. So syzbot could trigger a race condition where a parallel htable_create() could register the same file immediately after the mutex is released.

Move htable_remove_proc_entry() back to mutex protection to fix this. And, fold htable_destroy() into htable_put() to make the code slightly easier to understand.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Probably revert previous one?

The hung task could appear again if we move the cleanup back under mutex.

How could the hung task appear again by reverting
c4a3922d2d20c710f827? Please elaborate.

Because the cfg.max could be as large as 8*HASHLIMIT_MAX_SIZE:

311 if (hinfo->cfg.max == 0)
312 hinfo->cfg.max = 8 * hinfo->cfg.size;
313 else if (hinfo->cfg.max < hinfo->cfg.size)
314 hinfo->cfg.max = hinfo->cfg.size;

Quoted 6 lines
Quoted 4 lines

Not sure whether we can finish cleaning up 8*HASHLIMIT_MAX_SIZE entries within the time a hung task tolerates. This largely depends on how much contention the spinlock has, at least I don't want to bet on it.

Please, resend. Thanks.

Sorry, I meant, applied, thanks.