Skip to content

net/tcp: fix use-after-free race and nullptr dereference in tcp_timer - #20641

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/tcp-timer-dereference-race
Oct 15, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/tcp-timer-dereference-race

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Fixes #19849.

Under network disruption (rapid reconnects, socket resets, peer resets), a race condition between tcp_timer_expiry() and tcp_stop_timer() could cause a kernel crash (Load Access Fault on offsetof(struct net_driver_s, d_lock), mcause 5, mtval 0x24) or leave the network stack permanently wedged.

Root Causes

  1. Use-after-free race condition:
    tcp_stop_timer() used asynchronous work_cancel(LPWORK, &conn->work). When tcp_timer_expiry() had already been dequeued and was executing on the LPWORK worker thread, work_cancel() returned without cancelling the active callback. As a result, tcp_free() proceeded to clear and zero out the connection memory via net_bufpool_free() (memset(net_bufnode, 0, ...)), leaving the running tcp_timer_expiry() callback to access zeroed/freed connection memory.
  2. Missing NULL check on conn->dev:
    tcp_timer_expiry() read conn->dev without checking for NULL before calling netdev_lock(conn->dev), netdev_txnotify_dev(conn->dev, TCP_POLL), and netdev_unlock(conn->dev). When conn->dev was NULL (or cleared by tcp_free), dereferencing dev->d_lock triggered a Load Access Fault.

Changes Made

  1. Synchronous timer cancellation: Replaced work_cancel(LPWORK, &conn->work) with work_cancel_sync(LPWORK, &conn->work) in tcp_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).
  2. Defensive NULL validation: In tcp_timer_expiry(), captured dev = conn->dev under tcp_conn_list_lock() and guarded the lock and notify operations with if (dev != NULL).

Impact

  • Is new feature added? No
  • Is existing feature changed? No (bugfix only)
  • Impact on user: Resolves crashes and hangs during rapid socket closes, cable disconnects, and network reconnects.
  • Impact on build: None
  • Impact on hardware: All architectures using TCP over NuttX work queues.

Testing

  • Verified tcp_timer_expiry() safely handles conn->dev == NULL.
  • Confirmed work_cancel_sync(LPWORK, &conn->work) safely synchronizes work cancellation and excludes worker self-deadlock via kwork_cancel.c.
  • Follows NuttX C coding style standards.

@github-actions github-actions Bot added Area: Networking Effects networking subsystem Size: S The size of the change in this PR is small labels 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]>
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@xiaoxiang781216
xiaoxiang781216 merged commit bc862ac into apache:master Oct 15, 2026
24 of 53 checks passed
@casaroli

Copy link
Copy Markdown
Contributor

Can this be backported to releases/13.0 and releases/13.1? Both branches still use work_cancel() in tcp_stop_timer() (net/tcp/tcp_timer.c:345), so the crash of #19849 is still in the 13.x releases. bc862ac applies to both branches without a conflict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Networking Effects networking subsystem Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Possible nullptr dereference in /net/tcp/tcp_timer.c

5 participants