Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mptcp: fix race condition in mptcp_schedule_work()

syzbot reported use-after-free in mptcp_schedule_work() [1]

Issue here is that mptcp_schedule_work() schedules a work,
then gets a refcount on sk->sk_refcnt if the work was scheduled.
This refcount will be released by mptcp_worker().

[A] if (schedule_work(...)) {
[B] sock_hold(sk);
return true;
}

Problem is that mptcp_worker() can run immediately and complete before [B]

We need instead :

sock_hold(sk);
if (schedule_work(...))
return true;
sock_put(sk);

[1]
refcount_t: addition on 0; use-after-free.
WARNING: CPU: 1 PID: 29 at lib/refcount.c:25 refcount_warn_saturate+0xfa/0x1d0 lib/refcount.c:25
Call Trace:
<TASK>
__refcount_add include/linux/refcount.h:-1 [inline]
__refcount_inc include/linux/refcount.h:366 [inline]
refcount_inc include/linux/refcount.h:383 [inline]
sock_hold include/net/sock.h:816 [inline]
mptcp_schedule_work+0x164/0x1a0 net/mptcp/protocol.c:943
mptcp_tout_timer+0x21/0xa0 net/mptcp/protocol.c:2316
call_timer_fn+0x17e/0x5f0 kernel/time/timer.c:1747
expire_timers kernel/time/timer.c:1798 [inline]
__run_timers kernel/time/timer.c:2372 [inline]
__run_timer_base+0x648/0x970 kernel/time/timer.c:2384
run_timer_base kernel/time/timer.c:2393 [inline]
run_timer_softirq+0xb7/0x180 kernel/time/timer.c:2403
handle_softirqs+0x22f/0x710 kernel/softirq.c:622
__do_softirq kernel/softirq.c:656 [inline]
run_ktimerd+0xcf/0x190 kernel/softirq.c:1138
smpboot_thread_fn+0x542/0xa60 kernel/smpboot.c:160
kthread+0x711/0x8a0 kernel/kthread.c:463
ret_from_fork+0x4bc/0x870 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245

Cc: stable@vger.kernel.org
Fixes: 3b1d6210a957 ("mptcp: implement and use MPTCP-level retransmission")
Reported-by: syzbot+355158e7e301548a1424@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/netdev/6915b46f.050a0220.3565dc.0028.GAE@google.com/T/#u
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Link: https://patch.msgid.link/20251113103924.3737425-1-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Eric Dumazet and committed by
Jakub Kicinski
035bca3f b0c959fe

+12 -7
+12 -7
net/mptcp/protocol.c
··· 935 935 936 936 bool mptcp_schedule_work(struct sock *sk) 937 937 { 938 - if (inet_sk_state_load(sk) != TCP_CLOSE && 939 - schedule_work(&mptcp_sk(sk)->work)) { 940 - /* each subflow already holds a reference to the sk, and the 941 - * workqueue is invoked by a subflow, so sk can't go away here. 942 - */ 943 - sock_hold(sk); 938 + if (inet_sk_state_load(sk) == TCP_CLOSE) 939 + return false; 940 + 941 + /* Get a reference on this socket, mptcp_worker() will release it. 942 + * As mptcp_worker() might complete before us, we can not avoid 943 + * a sock_hold()/sock_put() if schedule_work() returns false. 944 + */ 945 + sock_hold(sk); 946 + 947 + if (schedule_work(&mptcp_sk(sk)->work)) 944 948 return true; 945 - } 949 + 950 + sock_put(sk); 946 951 return false; 947 952 } 948 953