From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
patches@lists.linux.dev,
Anderson Nascimento <anderson@allelesecurity.com>,
Eric Dumazet <edumazet@google.com>,
Kuniyuki Iwashima <kuniyu@amazon.com>,
Jakub Kicinski <kuba@kernel.org>, Sasha Levin <sashal@kernel.org>
Subject: [PATCH 5.4 64/84] tcp: Use refcount_inc_not_zero() in tcp_twsk_unique().
Date: Tue, 14 May 2024 12:20:15 +0200 [thread overview]
Message-ID: <20240514100954.091778234@linuxfoundation.org> (raw)
In-Reply-To: <20240514100951.686412426@linuxfoundation.org>
5.4-stable review patch. If anyone has any objections, please let me know.
------------------
From: Kuniyuki Iwashima <kuniyu@amazon.com>
[ Upstream commit f2db7230f73a80dbb179deab78f88a7947f0ab7e ]
Anderson Nascimento reported a use-after-free splat in tcp_twsk_unique()
with nice analysis.
Since commit ec94c2696f0b ("tcp/dccp: avoid one atomic operation for
timewait hashdance"), inet_twsk_hashdance() sets TIME-WAIT socket's
sk_refcnt after putting it into ehash and releasing the bucket lock.
Thus, there is a small race window where other threads could try to
reuse the port during connect() and call sock_hold() in tcp_twsk_unique()
for the TIME-WAIT socket with zero refcnt.
If that happens, the refcnt taken by tcp_twsk_unique() is overwritten
and sock_put() will cause underflow, triggering a real use-after-free
somewhere else.
To avoid the use-after-free, we need to use refcount_inc_not_zero() in
tcp_twsk_unique() and give up on reusing the port if it returns false.
[0]:
refcount_t: addition on 0; use-after-free.
WARNING: CPU: 0 PID: 1039313 at lib/refcount.c:25 refcount_warn_saturate+0xe5/0x110
CPU: 0 PID: 1039313 Comm: trigger Not tainted 6.8.6-200.fc39.x86_64 #1
Hardware name: VMware, Inc. VMware20,1/440BX Desktop Reference Platform, BIOS VMW201.00V.21805430.B64.2305221830 05/22/2023
RIP: 0010:refcount_warn_saturate+0xe5/0x110
Code: 42 8e ff 0f 0b c3 cc cc cc cc 80 3d aa 13 ea 01 00 0f 85 5e ff ff ff 48 c7 c7 f8 8e b7 82 c6 05 96 13 ea 01 01 e8 7b 42 8e ff <0f> 0b c3 cc cc cc cc 48 c7 c7 50 8f b7 82 c6 05 7a 13 ea 01 01 e8
RSP: 0018:ffffc90006b43b60 EFLAGS: 00010282
RAX: 0000000000000000 RBX: ffff888009bb3ef0 RCX: 0000000000000027
RDX: ffff88807be218c8 RSI: 0000000000000001 RDI: ffff88807be218c0
RBP: 0000000000069d70 R08: 0000000000000000 R09: ffffc90006b439f0
R10: ffffc90006b439e8 R11: 0000000000000003 R12: ffff8880029ede84
R13: 0000000000004e20 R14: ffffffff84356dc0 R15: ffff888009bb3ef0
FS: 00007f62c10926c0(0000) GS:ffff88807be00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000020ccb000 CR3: 000000004628c005 CR4: 0000000000f70ef0
PKRU: 55555554
Call Trace:
<TASK>
? refcount_warn_saturate+0xe5/0x110
? __warn+0x81/0x130
? refcount_warn_saturate+0xe5/0x110
? report_bug+0x171/0x1a0
? refcount_warn_saturate+0xe5/0x110
? handle_bug+0x3c/0x80
? exc_invalid_op+0x17/0x70
? asm_exc_invalid_op+0x1a/0x20
? refcount_warn_saturate+0xe5/0x110
tcp_twsk_unique+0x186/0x190
__inet_check_established+0x176/0x2d0
__inet_hash_connect+0x74/0x7d0
? __pfx___inet_check_established+0x10/0x10
tcp_v4_connect+0x278/0x530
__inet_stream_connect+0x10f/0x3d0
inet_stream_connect+0x3a/0x60
__sys_connect+0xa8/0xd0
__x64_sys_connect+0x18/0x20
do_syscall_64+0x83/0x170
entry_SYSCALL_64_after_hwframe+0x78/0x80
RIP: 0033:0x7f62c11a885d
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d a3 45 0c 00 f7 d8 64 89 01 48
RSP: 002b:00007f62c1091e58 EFLAGS: 00000296 ORIG_RAX: 000000000000002a
RAX: ffffffffffffffda RBX: 0000000020ccb004 RCX: 00007f62c11a885d
RDX: 0000000000000010 RSI: 0000000020ccb000 RDI: 0000000000000003
RBP: 00007f62c1091e90 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000296 R12: 00007f62c10926c0
R13: ffffffffffffff88 R14: 0000000000000000 R15: 00007ffe237885b0
</TASK>
Fixes: ec94c2696f0b ("tcp/dccp: avoid one atomic operation for timewait hashdance")
Reported-by: Anderson Nascimento <anderson@allelesecurity.com>
Closes: https://lore.kernel.org/netdev/37a477a6-d39e-486b-9577-3463f655a6b7@allelesecurity.com/
Suggested-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Link: https://lore.kernel.org/r/20240501213145.62261-1-kuniyu@amazon.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
net/ipv4/tcp_ipv4.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index a54505c29a5c2..0dd917c5a7da6 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -152,6 +152,12 @@ int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
if (tcptw->tw_ts_recent_stamp &&
(!twp || (reuse && time_after32(ktime_get_seconds(),
tcptw->tw_ts_recent_stamp)))) {
+ /* inet_twsk_hashdance() sets sk_refcnt after putting twsk
+ * and releasing the bucket lock.
+ */
+ if (unlikely(!refcount_inc_not_zero(&sktw->sk_refcnt)))
+ return 0;
+
/* In case of repair and re-using TIME-WAIT sockets we still
* want to be sure that it is safe as above but honor the
* sequence numbers and time stamps set as part of the repair
@@ -172,7 +178,7 @@ int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
tp->rx_opt.ts_recent = tcptw->tw_ts_recent;
tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
}
- sock_hold(sktw);
+
return 1;
}
--
2.43.0
next prev parent reply other threads:[~2024-05-14 11:45 UTC|newest]
Thread overview: 91+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-14 10:19 [PATCH 5.4 00/84] 5.4.276-rc1 review Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 01/84] dmaengine: pl330: issue_pending waits until WFP state Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 02/84] dmaengine: Revert "dmaengine: pl330: issue_pending waits until WFP state" Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 03/84] wifi: nl80211: dont free NULL coalescing rule Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 04/84] pinctrl: core: delete incorrect free in pinctrl_enable() Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 05/84] pinctrl: mediatek: Check gpio pin number and use binary search in mtk_hw_pin_field_lookup() Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 06/84] pinctrl: mediatek: Supporting driving setting without mapping current to register value Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 07/84] pinctrl: mediatek: Refine mtk_pinconf_get() and mtk_pinconf_set() Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 08/84] pinctrl: mediatek: Refine mtk_pinconf_get() Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 09/84] pinctrl: mediatek: Backward compatible to previous Mediateks bias-pull usage Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 10/84] pinctrl: mediatek: remove shadow variable declaration Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 11/84] pinctrl: mediatek: paris: Fix PIN_CONFIG_BIAS_* readback Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 12/84] pinctrl: mediatek: paris: Rework mtk_pinconf_{get,set} switch/case logic Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 13/84] pinctrl: mediatek: paris: Rework support for PIN_CONFIG_{INPUT,OUTPUT}_ENABLE Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 14/84] sunrpc: add a struct rpc_stats arg to rpc_create_args Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 15/84] nfs: expose /proc/net/sunrpc/nfs in net namespaces Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 16/84] nfs: make the rpc_stat per net namespace Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 17/84] nfs: Handle error of rpc_proc_register() in nfs_net_init() Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 18/84] power: rt9455: hide unused rt9455_boost_voltage_values Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 19/84] pinctrl: devicetree: fix refcount leak in pinctrl_dt_to_map() Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 20/84] s390/mm: Fix storage key clearing for guest huge pages Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 21/84] s390/mm: Fix clearing storage keys for " Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 22/84] bna: ensure the copied buf is NUL terminated Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 23/84] nsh: Restore skb->{protocol,data,mac_header} for outer header in nsh_gso_segment() Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 24/84] net l2tp: drop flow hash on forward Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 25/84] net: qede: use return from qede_parse_flow_attr() for flow_spec Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 26/84] ASoC: meson: axg-card: make links nonatomic Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 27/84] ASoC: meson: axg-tdm-interface: manage formatters in trigger Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 28/84] net: dsa: mv88e6xxx: Add number of MACs in the ATU Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 29/84] net: dsa: mv88e6xxx: Fix number of databases for 88E6141 / 88E6341 Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 30/84] net: bridge: fix multicast-to-unicast with fraglist GSO Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 31/84] tipc: fix a possible memleak in tipc_buf_append Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 32/84] clk: sunxi-ng: h6: Reparent CPUX during PLL CPUX rate change Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 33/84] scsi: lpfc: Update lpfc_ramp_down_queue_handler() logic Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 34/84] gfs2: Fix invalid metadata access in punch_hole Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 35/84] wifi: mac80211: fix ieee80211_bss_*_flags kernel-doc Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 36/84] wifi: cfg80211: fix rdev_dump_mpp() arguments order Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 37/84] net: mark racy access on sk->sk_rcvbuf Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 38/84] scsi: bnx2fc: Remove spin_lock_bh while releasing resources after upload Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 39/84] ALSA: line6: Zero-initialize message buffers Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 40/84] net: bcmgenet: Reset RBUF on first open Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 41/84] ata: sata_gemini: Check clk_enable() result Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 42/84] firewire: ohci: mask bus reset interrupts between ISR and bottom half Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 43/84] tools/power turbostat: Fix added raw MSR output Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 44/84] tools/power turbostat: Fix Bzy_MHz documentation typo Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 45/84] btrfs: make btrfs_clear_delalloc_extent() free delalloc reserve Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 46/84] btrfs: always clear PERTRANS metadata during commit Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 47/84] scsi: target: Fix SELinux error when systemd-modules loads the target module Greg Kroah-Hartman
2024-05-14 10:19 ` [PATCH 5.4 48/84] gpu: host1x: Do not setup DMA for virtual devices Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 49/84] MIPS: scall: Save thread_info.syscall unconditionally on entry Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 50/84] selftests: timers: Fix valid-adjtimex signed left-shift undefined behavior Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 51/84] fs/9p: only translate RWX permissions for plain 9P2000 Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 52/84] fs/9p: translate O_TRUNC into OTRUNC Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 53/84] 9p: explicitly deny setlease attempts Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 54/84] gpio: wcove: Use -ENOTSUPP consistently Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 55/84] gpio: crystalcove: " Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 56/84] clk: Dont hold prepare_lock when calling kref_put() Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 57/84] fs/9p: drop inodes immediately on non-.L too Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 58/84] net:usb:qmi_wwan: support Rolling modules Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 59/84] pinctrl: mediatek: Fix fallback call path Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 60/84] ASoC: meson: axg-card: Fix nonatomic links Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 61/84] ASoC: meson: axg-tdm-interface: Fix formatters in trigger" Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 62/84] xfrm: Preserve vlan tags for transport mode software GRO Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 63/84] tcp: defer shutdown(SEND_SHUTDOWN) for TCP_SYN_RECV sockets Greg Kroah-Hartman
2024-05-14 10:20 ` Greg Kroah-Hartman [this message]
2024-05-14 10:20 ` [PATCH 5.4 65/84] Bluetooth: Fix use-after-free bugs caused by sco_sock_timeout Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 66/84] Bluetooth: l2cap: fix null-ptr-deref in l2cap_chan_timeout Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 67/84] rtnetlink: Correct nested IFLA_VF_VLAN_LIST attribute validation Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 68/84] phonet: fix rtm_phonet_notify() skb allocation Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 69/84] net: bridge: fix corrupted ethernet header on multicast-to-unicast Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 70/84] ipv6: fib6_rules: avoid possible NULL dereference in fib6_rule_action() Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 71/84] net: qede: sanitize rc in qede_add_tc_flower_fltr() Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 72/84] net: qede: use return from qede_parse_flow_attr() for flower Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 73/84] firewire: nosy: ensure user_length is taken into account when fetching packet contents Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 74/84] usb: gadget: composite: fix OS descriptors w_value logic Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 75/84] usb: gadget: f_fs: Fix a race condition when processing setup packets Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 76/84] tipc: fix UAF in error path Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 77/84] dyndbg: fix old BUG_ON in >control parser Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 78/84] drm/vmwgfx: Fix invalid reads in fence signaled events Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 79/84] net: fix out-of-bounds access in ops_init Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 80/84] regulator: core: fix debugfs creation regression Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 81/84] pinctrl: mediatek: Fix fallback behavior for bias_set_combo Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 82/84] pinctrl: mediatek: Fix some off by one bugs Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 83/84] pinctrl: mediatek: remove set but not used variable e Greg Kroah-Hartman
2024-05-14 10:20 ` [PATCH 5.4 84/84] pinctrl: mediatek: paris: Fix PIN_CONFIG_INPUT_SCHMITT_ENABLE readback Greg Kroah-Hartman
2024-05-14 16:29 ` [PATCH 5.4 00/84] 5.4.276-rc1 review Harshit Mogalapalli
2024-05-14 22:32 ` Florian Fainelli
2024-05-15 15:08 ` Shuah Khan
2024-05-16 7:41 ` Naresh Kamboju
2024-05-16 12:30 ` Jon Hunter
2024-05-16 19:57 ` Shreeya Patel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240514100954.091778234@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=anderson@allelesecurity.com \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=kuniyu@amazon.com \
--cc=patches@lists.linux.dev \
--cc=sashal@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox