Skip to content

Commit a0eea5f

Browse files
Paolo Abenidavem330
authored andcommitted
mptcp: fix memory leak on address flush
The endpoint cleanup path is prone to a memory leak, as reported by syzkaller: BUG: memory leak unreferenced object 0xffff88810680ea00 (size 64): comm "syz-executor.6", pid 6191, jiffies 4295756280 (age 24.138s) hex dump (first 32 bytes): 58 75 7d 3c 80 88 ff ff 22 01 00 00 00 00 ad de Xu}<...."....... 01 00 02 00 00 00 00 00 ac 1e 00 07 00 00 00 00 ................ backtrace: [<0000000072a9f72a>] kmalloc include/linux/slab.h:591 [inline] [<0000000072a9f72a>] mptcp_nl_cmd_add_addr+0x287/0x9f0 net/mptcp/pm_netlink.c:1170 [<00000000f6e931bf>] genl_family_rcv_msg_doit.isra.0+0x225/0x340 net/netlink/genetlink.c:731 [<00000000f1504a2c>] genl_family_rcv_msg net/netlink/genetlink.c:775 [inline] [<00000000f1504a2c>] genl_rcv_msg+0x341/0x5b0 net/netlink/genetlink.c:792 [<0000000097e76f6a>] netlink_rcv_skb+0x148/0x430 net/netlink/af_netlink.c:2504 [<00000000ceefa2b8>] genl_rcv+0x24/0x40 net/netlink/genetlink.c:803 [<000000008ff91aec>] netlink_unicast_kernel net/netlink/af_netlink.c:1314 [inline] [<000000008ff91aec>] netlink_unicast+0x537/0x750 net/netlink/af_netlink.c:1340 [<0000000041682c35>] netlink_sendmsg+0x846/0xd80 net/netlink/af_netlink.c:1929 [<00000000df3aa8e7>] sock_sendmsg_nosec net/socket.c:704 [inline] [<00000000df3aa8e7>] sock_sendmsg+0x14e/0x190 net/socket.c:724 [<000000002154c54c>] ____sys_sendmsg+0x709/0x870 net/socket.c:2403 [<000000001aab01d7>] ___sys_sendmsg+0xff/0x170 net/socket.c:2457 [<00000000fa3b1446>] __sys_sendmsg+0xe5/0x1b0 net/socket.c:2486 [<00000000db2ee9c7>] do_syscall_x64 arch/x86/entry/common.c:50 [inline] [<00000000db2ee9c7>] do_syscall_64+0x38/0x90 arch/x86/entry/common.c:80 [<000000005873517d>] entry_SYSCALL_64_after_hwframe+0x44/0xae We should not require an allocation to cleanup stuff. Rework the code a bit so that the additional RCU work is no more needed. Fixes: 1729cf1 ("mptcp: create the listening socket for new port") Signed-off-by: Paolo Abeni <[email protected]> Signed-off-by: Mat Martineau <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent fb4b137 commit a0eea5f

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

net/mptcp/pm_netlink.c

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,36 +1135,12 @@ static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
11351135
return 0;
11361136
}
11371137

1138-
struct addr_entry_release_work {
1139-
struct rcu_work rwork;
1140-
struct mptcp_pm_addr_entry *entry;
1141-
};
1142-
1143-
static void mptcp_pm_release_addr_entry(struct work_struct *work)
1138+
/* caller must ensure the RCU grace period is already elapsed */
1139+
static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry)
11441140
{
1145-
struct addr_entry_release_work *w;
1146-
struct mptcp_pm_addr_entry *entry;
1147-
1148-
w = container_of(to_rcu_work(work), struct addr_entry_release_work, rwork);
1149-
entry = w->entry;
1150-
if (entry) {
1151-
if (entry->lsk)
1152-
sock_release(entry->lsk);
1153-
kfree(entry);
1154-
}
1155-
kfree(w);
1156-
}
1157-
1158-
static void mptcp_pm_free_addr_entry(struct mptcp_pm_addr_entry *entry)
1159-
{
1160-
struct addr_entry_release_work *w;
1161-
1162-
w = kmalloc(sizeof(*w), GFP_ATOMIC);
1163-
if (w) {
1164-
INIT_RCU_WORK(&w->rwork, mptcp_pm_release_addr_entry);
1165-
w->entry = entry;
1166-
queue_rcu_work(system_wq, &w->rwork);
1167-
}
1141+
if (entry->lsk)
1142+
sock_release(entry->lsk);
1143+
kfree(entry);
11681144
}
11691145

11701146
static int mptcp_nl_remove_id_zero_address(struct net *net,
@@ -1244,7 +1220,8 @@ static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
12441220
spin_unlock_bh(&pernet->lock);
12451221

12461222
mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
1247-
mptcp_pm_free_addr_entry(entry);
1223+
synchronize_rcu();
1224+
__mptcp_pm_release_addr_entry(entry);
12481225

12491226
return ret;
12501227
}
@@ -1297,6 +1274,7 @@ static void mptcp_nl_remove_addrs_list(struct net *net,
12971274
}
12981275
}
12991276

1277+
/* caller must ensure the RCU grace period is already elapsed */
13001278
static void __flush_addrs(struct list_head *list)
13011279
{
13021280
while (!list_empty(list)) {
@@ -1305,7 +1283,7 @@ static void __flush_addrs(struct list_head *list)
13051283
cur = list_entry(list->next,
13061284
struct mptcp_pm_addr_entry, list);
13071285
list_del_rcu(&cur->list);
1308-
mptcp_pm_free_addr_entry(cur);
1286+
__mptcp_pm_release_addr_entry(cur);
13091287
}
13101288
}
13111289

@@ -1329,6 +1307,7 @@ static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
13291307
bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1);
13301308
spin_unlock_bh(&pernet->lock);
13311309
mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
1310+
synchronize_rcu();
13321311
__flush_addrs(&free_list);
13331312
return 0;
13341313
}
@@ -1939,7 +1918,8 @@ static void __net_exit pm_nl_exit_net(struct list_head *net_list)
19391918
struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
19401919

19411920
/* net is removed from namespace list, can't race with
1942-
* other modifiers
1921+
* other modifiers, also netns core already waited for a
1922+
* RCU grace period.
19431923
*/
19441924
__flush_addrs(&pernet->local_addr_list);
19451925
}

0 commit comments

Comments
 (0)