Repository navigation
net/tcp: fix use-after-free race and nullptr dereference in tcp_timer - #20641
Merged
xiaoxiang781216 merged 1 commit intoOct 15, 2026
Merged
Conversation
Swatantra-66
requested review from
jerpelea and
xiaoxiang781216
as code owners
October 13, 2026 17:21
xiaoxiang781216
previously approved these changes
Oct 13, 2026
In tcp_timer_expiry(), conn->dev was accessed without a NULL check, leading to a crash (Load Access Fault on offsetof(struct net_driver_s, d_lock)) if conn->dev was unbound or zeroed out during connection teardown. Furthermore, tcp_stop_timer() used asynchronous work_cancel(), which does not wait for a worker callback that is already running. If tcp_timer_expiry() was dequeued on LPWORK while tcp_free() was called on another thread, tcp_free() would clear the connection structure via net_bufpool_free() (zeroing out conn->dev and other fields), causing a race and crash in tcp_timer_expiry(). This commit: 1. Replaces work_cancel() with work_cancel_sync() in tcp_stop_timer(), ensuring any running timer expiry callback finishes before the connection is deallocated. 2. Captures conn->dev under the active connection list lock and guards netdev_lock()/netdev_txnotify_dev() with a NULL check. Fixes: apache#19849 Signed-off-by: Swatantra Yadav <[email protected]>
Swatantra-66
force-pushed
the
fix/tcp-timer-dereference-race
branch
from
October 13, 2026 22:07
c4682ae to
a2608f7
Compare
xiaoxiang781216
approved these changes
Oct 14, 2026
acassis
approved these changes
Oct 15, 2026
jerpelea
approved these changes
Oct 15, 2026
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #19849.
Under network disruption (rapid reconnects, socket resets, peer resets), a race condition between
tcp_timer_expiry()andtcp_stop_timer()could cause a kernel crash (Load Access Faultonoffsetof(struct net_driver_s, d_lock), mcause 5, mtval 0x24) or leave the network stack permanently wedged.Root Causes
tcp_stop_timer()used asynchronouswork_cancel(LPWORK, &conn->work). Whentcp_timer_expiry()had already been dequeued and was executing on theLPWORKworker thread,work_cancel()returned without cancelling the active callback. As a result,tcp_free()proceeded to clear and zero out the connection memory vianet_bufpool_free()(memset(net_bufnode, 0, ...)), leaving the runningtcp_timer_expiry()callback to access zeroed/freed connection memory.conn->dev:tcp_timer_expiry()readconn->devwithout checking forNULLbefore callingnetdev_lock(conn->dev),netdev_txnotify_dev(conn->dev, TCP_POLL), andnetdev_unlock(conn->dev). Whenconn->devwasNULL(or cleared bytcp_free), dereferencingdev->d_locktriggered aLoad Access Fault.Changes Made
work_cancel(LPWORK, &conn->work)withwork_cancel_sync(LPWORK, &conn->work)intcp_stop_timer(). This guarantees that if a timer expiry callback is currently executing,tcp_free()will wait for it to finish before releasing or zeroing the connection structure. (work_cancel_sync()safely excludes the calling worker thread to prevent self-deadlock if ever called from the same worker).tcp_timer_expiry(), captureddev = conn->devundertcp_conn_list_lock()and guarded the lock and notify operations withif (dev != NULL).Impact
Testing
tcp_timer_expiry()safely handlesconn->dev == NULL.work_cancel_sync(LPWORK, &conn->work)safely synchronizes work cancellation and excludes worker self-deadlock viakwork_cancel.c.