Skip to content

Commit b4681c2

Browse files
idoschdavem330
authored andcommitted
ipv4: Fix use-after-free when flushing FIB tables
Since commit 0ddcf43 ("ipv4: FIB Local/MAIN table collapse") the local table uses the same trie allocated for the main table when custom rules are not in use. When a net namespace is dismantled, the main table is flushed and freed (via an RCU callback) before the local table. In case the callback is invoked before the local table is iterated, a use-after-free can occur. Fix this by iterating over the FIB tables in reverse order, so that the main table is always freed after the local table. v3: Reworded comment according to Alex's suggestion. v2: Add a comment to make the fix more explicit per Dave's and Alex's feedback. Fixes: 0ddcf43 ("ipv4: FIB Local/MAIN table collapse") Signed-off-by: Ido Schimmel <[email protected]> Reported-by: Fengguang Wu <[email protected]> Acked-by: Alexander Duyck <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ad3cbf6 commit b4681c2

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

net/ipv4/fib_frontend.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -1298,14 +1298,19 @@ static int __net_init ip_fib_net_init(struct net *net)
12981298

12991299
static void ip_fib_net_exit(struct net *net)
13001300
{
1301-
unsigned int i;
1301+
int i;
13021302

13031303
rtnl_lock();
13041304
#ifdef CONFIG_IP_MULTIPLE_TABLES
13051305
RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
13061306
RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
13071307
#endif
1308-
for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
1308+
/* Destroy the tables in reverse order to guarantee that the
1309+
* local table, ID 255, is destroyed before the main table, ID
1310+
* 254. This is necessary as the local table may contain
1311+
* references to data contained in the main table.
1312+
*/
1313+
for (i = FIB_TABLE_HASHSZ - 1; i >= 0; i--) {
13091314
struct hlist_head *head = &net->ipv4.fib_table_hash[i];
13101315
struct hlist_node *tmp;
13111316
struct fib_table *tb;

0 commit comments

Comments
 (0)