Repository navigation
Conversation
Reviewer's GuideAPIAdd acquire/release refcount operations and document the ordering contract required when objects can be reused under SLAB_TYPESAFE_BY_RCU.
Rename the TIME_WAIT hashdance interface and extend it to accept the timer delay so publication, timer setup, and descheduling can be coordinated.
Uses of the APIUse acquire refcount increments and reject unhashed sockets after acquiring a reference in the IPv4 and IPv6 established lookups.
Update BPF socket assignment to acquire its socket reference with the ordering required for potentially reused objects.
Route TCP and DCCP TIME_WAIT transitions through the updated hashdance-and-schedule interface.
Use release refcount initialization and balance request-socket references across migration, SYN-cookie, Fast Open, and failure paths.
ImplementationPublish a fully initialized TIME_WAIT socket in place of the established socket, schedule its timer under the ehash lock, and synchronize timer shutdown during descheduling.
Take an acquire-ordered reference and recheck unhashed state before purging a socket discovered through the shared ehash.
Update the TIME_WAIT reuse comment to match the new refcount-before-publication ordering.
DocumentationUpdate the RCU documentation to explain the relevant RCU object-reuse rules.
Document how refcounting differs from atomic operations, including the added ordering guarantees.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="net/ipv4/inet_connection_sock.c" line_range="1118" />
<code_context>
if (nreq) {
__NET_INC_STATS(net, LINUX_MIB_TCPMIGRATEREQFAILURE);
+ /* The timer was not armed, drop its reference here. */
+ reqsk_put(nreq);
no_ownership:
reqsk_migrate_reset(nreq);
</code_context>
<issue_to_address>
**Timer callback accesses freed request**
When the cloned request's timer is armed or rearmed and `inet_ehash_insert()` fails, `reqsk_put()` and the subsequent cleanup release the request references while the timer remains pending; `__inet_csk_reqsk_queue_drop(..., true)` skips timer deletion, so the callback accesses freed memory and can crash the kernel.
Delete the pending request timer before dropping its timer-held and other request references on the insertion-failure path.
</issue_to_address>There was a problem hiding this comment.
🟡 Changes recommended
Weakly ordered lookups can return a recycled request with a NULL listener, while failed migration can leave a cloned timer armed, allowing stray retransmissions and listener retention.
2 open issues
What changed in this PR
This backport hardens TCP socket lookups and request-socket lifetime management against reuse races that can crash the kernel.
Changes:
- Reject unhashed sockets after reference acquisition in IPv4 and IPv6 established lookups.
- Use reference-counted cleanup for SYN-cookie, Fast Open, and request-migration failure paths.
| File | Description |
|---|---|
net/ipv6/inet6_hashtables.c |
Adds unhashed-socket validation to IPv6 lookups. |
net/ipv4/tcp_input.c |
Updates Fast Open failure cleanup. |
net/ipv4/syncookies.c |
Releases failed cookie requests through reference counting. |
net/ipv4/inet_hashtables.c |
Adds unhashed-socket validation to IPv4 lookups. |
net/ipv4/inet_connection_sock.c |
Updates migration and timer-path reference cleanup. |
Review effort: Balanced
| goto out; | ||
| if (unlikely(!inet_match(net, sk, acookie, | ||
| ports, dif, sdif))) { | ||
| if (unlikely(hlist_nulls_unhashed_lockless(&sk->sk_nulls_node) || |
975200b to
1f4655a
Compare
1f4655a to
915d722
Compare
| * so we are not allowed to use tw anymore. | ||
| */ | ||
| refcount_set(&tw->tw_refcnt, 3); | ||
| refcount_set_release(&tw->tw_refcnt, 3); |
mainline inclusion from mainline-v6.15-rc1 commit 7f8ceea category: bugfix bugzilla: https://atomgit.com/src-openeuler/kernel/issues/21180 CVE: CVE-2026-99288 Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7f8ceea0c58039dcea3d31b8d5da58aa5f6e12bf -------------------------------- For speculative lookups where a successful inc_not_zero() pins the object, but where we still need to double check if the object acquired is indeed the one we set out to acquire (identity check), needs this validation to happen *after* the increment. Similarly, when a new object is initialized and its memory might have been previously occupied by another object, all stores to initialize the object should happen *before* refcount initialization. Notably SLAB_TYPESAFE_BY_RCU is one such an example when this ordering is required for reference counting. Add refcount_{add|inc}_not_zero_acquire() to guarantee the proper ordering between acquiring a reference count on an object and performing the identity check for that object. Add refcount_set_release() to guarantee proper ordering between stores initializing object attributes and the store initializing the refcount. refcount_set_release() should be done after all other object attributes are initialized. Once refcount_set_release() is called, the object should be considered visible to other tasks even if it was not yet added into an object collection normally used to discover it. This is because other tasks might have discovered the object previously occupying the same memory and after memory reuse they can succeed in taking refcount for the new object and start using it. Object reuse example to consider: consumer: obj = lookup(collection, key); if (!refcount_inc_not_zero_acquire(&obj->ref)) return; if (READ_ONCE(obj->key) != key) { /* identity check */ put_ref(obj); return; } use(obj->value); producer: remove(collection, obj->key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(obj); obj = malloc(); /* obj is reused */ obj->key = new_key; obj->value = new_value; refcount_set_release(obj->ref, 1); add(collection, new_key, obj); refcount_{add|inc}_not_zero_acquire() is required to prevent the following reordering when refcount_inc_not_zero() is used instead: consumer: obj = lookup(collection, key); if (READ_ONCE(obj->key) != key) { /* reordered identity check */ put_ref(obj); return; } producer: remove(collection, obj->key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(obj); obj = malloc(); /* obj is reused */ obj->key = new_key; obj->value = new_value; refcount_set_release(obj->ref, 1); add(collection, new_key, obj); if (!refcount_inc_not_zero(&obj->ref)) return; use(obj->value); /* USING WRONG OBJECT */ refcount_set_release() is required to prevent the following reordering when refcount_set() is used instead: consumer: obj = lookup(collection, key); producer: remove(collection, obj->key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(obj); obj = malloc(); /* obj is reused */ obj->key = new_key; /* new_key == old_key */ refcount_set(obj->ref, 1); if (!refcount_inc_not_zero_acquire(&obj->ref)) return; if (READ_ONCE(obj->key) != key) { /* pass since new_key == old_key */ put_ref(obj); return; } use(obj->value); /* USING STALE obj->value */ obj->value = new_value; /* reordered store */ add(collection, key, obj); [surenb@google.com: fix title underlines in refcount-vs-atomic.rst] Link: https://lkml.kernel.org/r/20250217161645.3137927-1-surenb@google.com Link: https://lkml.kernel.org/r/20250213224655.1680278-11-surenb@google.com Signed-off-by: Suren Baghdasaryan <surenb@google.com> Acked-by: Vlastimil Babka <vbabka@suse.cz> [slab] Tested-by: Shivank Garg <shivankg@amd.com> Link: https://lkml.kernel.org/r/5e19ec93-8307-47c2-bb13-3ddf7150624e@amd.com Cc: Peter Zijlstra <peterz@infradead.org> Cc: Will Deacon <will@kernel.org> Cc: Paul E. McKenney <paulmck@kernel.org> Cc: Christian Brauner <brauner@kernel.org> Cc: David Hildenbrand <david@redhat.com> Cc: David Howells <dhowells@redhat.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Hugh Dickins <hughd@google.com> Cc: Jann Horn <jannh@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Klara Modin <klarasmodin@gmail.com> Cc: Liam R. Howlett <Liam.Howlett@Oracle.com> Cc: Lokesh Gidra <lokeshgidra@google.com> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Mateusz Guzik <mjguzik@gmail.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Mel Gorman <mgorman@techsingularity.net> Cc: Michal Hocko <mhocko@suse.com> Cc: Minchan Kim <minchan@google.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Pasha Tatashin <pasha.tatashin@soleen.com> Cc: Peter Xu <peterx@redhat.com> Cc: Shakeel Butt <shakeel.butt@linux.dev> Cc: Sourav Panda <souravpanda@google.com> Cc: Wei Yang <richard.weiyang@gmail.com> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: Stephen Rothwell <sfr@canb.auug.org.au> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Conflicts: Documentation/RCU/whatisRCU.rst include/linux/refcount.h [Keep the 6.6 documentation wording and drop __signed_wrap, which is not available in this tree.] Stable-dep-of: c934130 ("RDMA/mlx4: Fix mis-use of RCU in mlx4_srq_event()") Signed-off-by: Sasha Levin <sashal@kernel.org> (cherry picked from commit 82290418744d1368d02c5f082350d80c07870808) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v7.1-rc4 commit 97c8a3c category: bugfix bugzilla: https://atomgit.com/src-openeuler/kernel/issues/21112 CVE: CVE-2026-99270 Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=97c8a3c1f73d828de43a5a88e8a9a143efb2b661 -------------------------------- When TCP socket migration fails at inet_ehash_insert() in reqsk_timer_handler(), we jump to the no_ownership: label and free the new reqsk immediately with __reqsk_free(). Thus, we must stop the new reqsk's timer before jumping to the label, but the timer might be missed since the cited commit, resulting in UAF. As we are in the original reqsk's timer context, we can safely call timer_delete_sync() for the new reqsk. Let's pass false to __inet_csk_reqsk_queue_drop() to stop the new reqsk's timer. Fixes: 83fccfc ("inet: fix potential deadlock in reqsk_queue_unlink()") Reported-by: Damiano Melotti <melotti@google.com> Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com> Link: https://patch.msgid.link/20260506035954.1563147-2-kuniyu@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> (cherry picked from commit 97c8a3c) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v7.4-rc2 commit 4e2b3ef19e0e04adca38442f07dd6bc93c712bf1 category: bugfix bugzilla: https://atomgit.com/src-openeuler/kernel/issues/21180 CVE: CVE-2026-99288 Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=4e2b3ef19e0e04adca38442f07dd6bc93c712bf1 -------------------------------- TCP established ehash lookups are lockless. The socket caches involved use SLAB_TYPESAFE_BY_RCU, so memory can be reused before an RCU reader that retained its old address has finished. After a tuple match, __inet_lookup_established() and __inet6_lookup_established() acquire a reference and repeat the tuple check. They do not check whether the new object is hashed. If the address is reused for a socket with the same tuple, the lookup can acquire a reference to the new object and return it while its nulls node is unhashed. A PoC that drives reuse with a SYN cookie request socket reproduced the crash. The new request socket is unhashed and has a NULL rsk_listener. A lookup that had loaded the address of the old request socket acquired the reference published by tcp_get_cookie_sock() and returned the new object. tcp_v4_rcv() then read sk_state through the NULL listener. The resulting crash was: BUG: kernel NULL pointer dereference, address: 0000000000000012 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page ... Oops: Oops: 0000 [#1] SMP NOPTI CPU: 4 UID: 0 PID: 0 Comm: swapper/4 Not tainted 7.3.0-00591-g0e135910deec #1 PREEMPT(lazy) ... RIP: 0010:tcp_v4_rcv+0x44e/0x1280 ... R10: 0000000000000001 R11: 00000000024ca8c0 R12: 0000000000000000 ... CR2: 0000000000000012 CR3: 0000000103d50004 CR4: 0000000000770ef0 ... Call Trace: <IRQ> ? raw_local_deliver+0xae/0x240 ip_protocol_deliver_rcu+0x2d/0x180 ip_local_deliver_finish+0x84/0x100 ip_sublist_rcv_finish+0x5f/0x70 ip_sublist_rcv+0x12a/0x1d0 ip_list_rcv+0xfd/0x130 __netif_receive_skb_list_core+0x223/0x250 netif_receive_skb_list_internal+0x1d2/0x2c0 ? try_fill_recv+0x39/0x7b0 napi_complete_done+0x74/0x170 virtnet_poll+0x445/0x600 __napi_poll+0x27/0x1e0 net_rx_action+0x2fe/0x3b0 handle_softirqs+0xc2/0x2c0 __irq_exit_rcu+0x74/0xd0 common_interrupt+0x7f/0x90 </IRQ> ... Kernel panic - not syncing: Fatal exception in interrupt After taking a reference, check whether the nulls node is unhashed in both IPv4 and IPv6 established lookups. If it is unhashed or the tuple no longer matches, drop the reference and restart the lookup. Lookups racing with the TIME_WAIT hashdance, or with a request being replaced by its child, now restart and find the new socket instead of returning the old one. Request sockets that were never inserted into ehash can have a nonzero refcount, and a stale lookup can hold a transient reference. Therefore, when SYN cookie, Fast Open, and request migration paths fail after setting a nonzero refcount, release the references held by those paths with reqsk_put() instead of freeing the request directly. Fixes: 079096f ("tcp/dccp: install syn_recv requests into ehash table") Cc: stable@vger.kernel.org Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com> Reviewed-by: Eric Dumazet <edumazet@kernel.org> Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com> Link: https://patch.msgid.link/20261029214219.1035176-2-imv4bel@gmail.com Signed-off-by: Paolo Abeni <pabeni@redhat.com> Co-authored-by: BackportAgent@deepseek-v4.1-flash Signed-off-by: Hulk Robot <hulkrobot@huawei.com> Signed-off-by: openeuler-ci-bot <george@openeuler.sh> Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v7.4-rc2 commit 5b5d8f351dd96f56a58bb516a80e2d4d742fe4e2 category: bugfix bugzilla: https://atomgit.com/src-openeuler/kernel/issues/21180 CVE: CVE-2026-99288 Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=5b5d8f351dd96f56a58bb516a80e2d4d742fe4e2 -------------------------------- Before commit fb041bb ("locking/refcount: Consolidate implementations of refcount_t"), default builds implemented refcount_inc_not_zero() using atomic_add_unless(), which provided full memory ordering on success. The commit removed that ordering on weakly ordered architectures, allowing accesses after a successful reference acquisition to be reordered before it. Use refcount_inc_not_zero_acquire() in the IPv4 and IPv6 established lookups and in inet_twsk_purge(), where the socket is validated again after taking a reference. bpf_sk_assign() can receive a socket pointer obtained through BTF without a reference. Use acquire ordering when it takes its own reference for a refcounted socket. Use refcount_set_release() when setting refcounts in the SYN cookie, TCP Fast Open, request migration, and TIME_WAIT paths. Keep the existing TIME_WAIT smp_wmb(), which separately orders the refcount store before ehash publication. Conflicts: net/ipv4/inet_timewait_sock.c net/ipv4/syncookies.c [Keep the 6.6 inet_twsk_hashdance() structure and the existing tsoffset assignment. Also keep timer_setup() before refcount_set_release() in the request migration path.] Fixes: fb041bb ("locking/refcount: Consolidate implementations of refcount_t") Fixes: 27e5203 ("bpf: Change bpf_sk_assign to accept ARG_PTR_TO_BTF_ID_SOCK_COMMON") Cc: stable@vger.kernel.org Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com> Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com> Link: https://patch.msgid.link/20261029214219.1035176-3-imv4bel@gmail.com Signed-off-by: Paolo Abeni <pabeni@redhat.com> (cherry picked from commit 5b5d8f351dd96f56a58bb516a80e2d4d742fe4e2) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
3d6bd81 to
1b680a1
Compare
maillist inclusion
category: bugfix
When a cloned request is migrated again, its timer has the same lockdep
class as the running request callback. Using timer_delete_sync() through
__inet_csk_reqsk_queue_drop(..., false) can therefore trigger a
recursive-locking splat and disable lockdep.
The clone is unhashed when inet_ehash_insert() fails, so
reqsk_queue_unlink() is a no-op. Its timer is pinned to the current CPU,
allowing it to be cancelled without waiting for its callback. Use
timer_delete() and drop the timer reference directly instead of
synchronously deleting it through the queue helper.
This change depends on the request lifetime changes in
commit 4e2b3ef19e0e ("inet: reject unhashed sockets during established lookups")
Without them, __reqsk_free() frees nreq immediately and the pending
timer can still trigger a UAF.
Link: https://lore.kernel.org/all/72918C6B4A6E0CC7+20261120040954.57832-2-guanwentao@uniontech.com/
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
maillist inclusion category: bugfix A shared ehash can retain a stale pointer that is recycled as an unhashed SYN-cookie request from another dead namespace. The namespace recheck alone accepts that request, and inet_csk_reqsk_queue_drop_and_put() then operates on an uninitialized timer and a NULL listener. Recheck the nulls node after taking the reference. If it is unhashed, drop the reference and restart the purge instead of operating on the recycled request. Link: https://lore.kernel.org/all/1595ACC3A3D11BB8+20261119041113.3514099-2-guanwentao@uniontech.com/ Fixes: 4e2b3ef19e0e ("inet: reject unhashed sockets during established lookups") Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1b680a1 to
739545a
Compare
| * so we are not allowed to use tw anymore. | ||
| */ | ||
| refcount_set(&tw->tw_refcnt, 3); | ||
| refcount_set_release(&tw->tw_refcnt, 3); |
7e078a9 to
afb34cd
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
mainline inclusion from mainline-v6.11-rc1 commit b334b92 category: bugfix Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b334b924c9b709bc969644fb5c406f5c9d01dceb -------------------------------- The TCP timewait timer is proving to be problematic for setups where scheduler CPU isolation is achieved at runtime via cpusets (as opposed to statically via isolcpus=domains). What happens there is a CPU goes through tcp_time_wait(), arming the time_wait timer, then gets isolated. TCP_TIMEWAIT_LEN later, the timer fires, causing interference for the now-isolated CPU. This is conceptually similar to the issue described in commit e02b931 ("workqueue: Unbind kworkers before sending them to exit()") Move inet_twsk_schedule() to within inet_twsk_hashdance(), with the ehash lock held. Expand the lock's critical section from inet_twsk_kill() to inet_twsk_deschedule_put(), serializing the scheduling vs descheduling of the timer. IOW, this prevents the following race: tcp_time_wait() inet_twsk_hashdance() inet_twsk_deschedule_put() del_timer_sync() inet_twsk_schedule() Thanks to Paolo Abeni for suggesting to leverage the ehash lock. This also restores a comment from commit ec94c26 ("tcp/dccp: avoid one atomic operation for timewait hashdance") as inet_twsk_hashdance() had a "Step 1" and "Step 3" comment, but the "Step 2" had gone missing. inet_twsk_deschedule_put() now acquires the ehash spinlock to synchronize with inet_twsk_hashdance_schedule(). To ease possible regression search, actual un-pin is done in next patch. Conflicts: net/ipv4/inet_timewait_sock.c net/ipv4/tcp_ipv4.c [Retain refcount_set_release() and the existing tcptw timestamp field.] Link: https://lore.kernel.org/all/ZPhpfMjSiHVjQkTk@localhost.localdomain/ Reviewed-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Valentin Schneider <vschneid@redhat.com> Co-developed-by: Florian Westphal <fw@strlen.de> Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: David S. Miller <davem@davemloft.net> (cherry picked from commit b334b92) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v6.19-rc1 commit b8ec80b category: bugfix Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b8ec80b130211e7bf076ef72365952979d5f7a72 -------------------------------- Since ehash lookups are lockless, if another CPU is converting sk to tw concurrently, fetching the newly inserted tw with tw->tw_refcnt == 0 cause lookup failure. The call trace map is drawn as follows: CPU 0 CPU 1 ----- ----- inet_twsk_hashdance_schedule() spin_lock() inet_twsk_add_node_rcu(tw, ...) __inet_lookup_established() (find tw, failure due to tw_refcnt = 0) __sk_nulls_del_node_init_rcu(sk) refcount_set(&tw->tw_refcnt, 3) spin_unlock() By replacing sk with tw atomically via hlist_nulls_replace_init_rcu() after setting tw_refcnt, we ensure that tw is either fully initialized or not visible to other CPUs, eliminating the race. It's worth noting that we held lock_sock() before the replacement, so there's no need to check if sk is hashed. Thanks to Kuniyuki Iwashima! Conflicts: net/ipv4/inet_timewait_sock.c [Retain refcount_set_release() for the existing memory-ordering fix.] Fixes: 3ab5aee ("net: Convert TCP & DCCP hash tables to use RCU / hlist_nulls") Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com> Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev> Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn> Reviewed-by: Eric Dumazet <edumazet@google.com> Link: https://patch.msgid.link/20251015020236.431822-4-xuanqiang.luo@linux.dev Signed-off-by: Jakub Kicinski <kuba@kernel.org> (cherry picked from commit b8ec80b) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
deepin inclusion category: bugfix The hashdance now sets tw_refcnt before publishing tw into ehash. Update the tcp_twsk_unique() comment accordingly. No functional change. Fixes: b8ec80b ("inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule()") Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
afb34cd to
5199f79
Compare



mainline inclusion
from mainline-v7.4-rc2
commit 4e2b3ef19e0e04adca38442f07dd6bc93c712bf1
category: bugfix
bugzilla: https://atomgit.com/src-openeuler/kernel/issues/21180 CVE: CVE-2026-99288
Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=4e2b3ef19e0e04adca38442f07dd6bc93c712bf1
TCP established ehash lookups are lockless. The socket caches involved use SLAB_TYPESAFE_BY_RCU, so memory can be reused before an RCU reader that retained its old address has finished.
After a tuple match, __inet_lookup_established() and __inet6_lookup_established() acquire a reference and repeat the tuple check. They do not check whether the new object is hashed. If the address is reused for a socket with the same tuple, the lookup can acquire a reference to the new object and return it while its nulls node is unhashed.
A PoC that drives reuse with a SYN cookie request socket reproduced the crash. The new request socket is unhashed and has a NULL rsk_listener. A lookup that had loaded the address of the old request socket acquired the reference published by tcp_get_cookie_sock() and returned the new object. tcp_v4_rcv() then read sk_state through the NULL listener.
The resulting crash was:
BUG: kernel NULL pointer dereference, address: 0000000000000012
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
...
Oops: Oops: 0000 [#1] SMP NOPTI
CPU: 4 UID: 0 PID: 0 Comm: swapper/4 Not tainted 7.3.0-00591-g0e135910deec #1 PREEMPT(lazy)
...
RIP: 0010:tcp_v4_rcv+0x44e/0x1280
...
R10: 0000000000000001 R11: 00000000024ca8c0 R12: 0000000000000000
...
CR2: 0000000000000012 CR3: 0000000103d50004 CR4: 0000000000770ef0
...
Call Trace:
? raw_local_deliver+0xae/0x240
ip_protocol_deliver_rcu+0x2d/0x180
ip_local_deliver_finish+0x84/0x100
ip_sublist_rcv_finish+0x5f/0x70
ip_sublist_rcv+0x12a/0x1d0
ip_list_rcv+0xfd/0x130
__netif_receive_skb_list_core+0x223/0x250
netif_receive_skb_list_internal+0x1d2/0x2c0
? try_fill_recv+0x39/0x7b0
napi_complete_done+0x74/0x170
virtnet_poll+0x445/0x600
__napi_poll+0x27/0x1e0
net_rx_action+0x2fe/0x3b0
handle_softirqs+0xc2/0x2c0
__irq_exit_rcu+0x74/0xd0
common_interrupt+0x7f/0x90
...
Kernel panic - not syncing: Fatal exception in interrupt
After taking a reference, check whether the nulls node is unhashed in both IPv4 and IPv6 established lookups. If it is unhashed or the tuple no longer matches, drop the reference and restart the lookup.
Lookups racing with the TIME_WAIT hashdance, or with a request being replaced by its child, now restart and find the new socket instead of returning the old one.
Request sockets that were never inserted into ehash can have a nonzero refcount, and a stale lookup can hold a transient reference. Therefore, when SYN cookie, Fast Open, and request migration paths fail after setting a nonzero refcount, release the references held by those paths with reqsk_put() instead of freeing the request directly.
Fixes: 079096f ("tcp/dccp: install syn_recv requests into ehash table")
Cc: stable@vger.kernel.org
Reviewed-by: Eric Dumazet edumazet@kernel.org
Reviewed-by: Kuniyuki Iwashima kuniyu@google.com
Link: https://patch.msgid.link/20261029214219.1035176-2-imv4bel@gmail.com
Co-authored-by: BackportAgent@deepseek-v4.1-flash
Summary by Sourcery
Prevent lockless established-socket lookups from returning unhashed reused sockets and fix the associated reference-counting races.
Bug Fixes:
Enhancements:
Documentation: