This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 04ba8b5a9e69b1213b87fb06404c87cb1d176b9d
Author: Marco Casaroli <[email protected]>
AuthorDate: Sat Nov 21 15:45:27 2026 +0100

    net/tcp: Keep the list lock while a listener takes a new connection.
    
    tcp_accept_connection() found the listener with tcp_findlistener(),
    which takes and releases the TCP connection list lock, and then locked
    it and called its accept callback or added the connection to its
    backlog.  close() of the listener could run between the look-up and the
    lock: tcp_unlisten() removed the listener from the table and tcp_free()
    freed it, its lock and its backlog.  The input path then locked freed
    memory and added the connection to a freed backlog.
    
    Now tcp_accept_connection() keeps the list lock from the look-up to the
    end.  tcp_unlisten() removes the listener from the table under the same
    lock, so a listener that the input path found stays until the input
    path is done with it.  The order is the one of the input path: the
    device lock, then the list lock, then the lock of the listener.
    
    So the accept() handler of the listener and the TCP_BACKLOG event of its
    callbacks now run with the list lock held: they must not take a device
    lock.  tcp.h says so.  accept_eventhandler() only posts a semaphore.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp.h        | 10 +++++++++-
 net/tcp/tcp_listen.c | 10 +++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index ce5ed9587be..bf85333cf63 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -426,6 +426,10 @@ struct tcp_conn_s
    *     accept() handler when it is invoked with a point to this structure
    *     as an argument.
    *   accept: This is the pointer to the accept handler.
+   *
+   *   tcp_accept_connection() calls accept() with the device of the new
+   *   connection and the TCP connection list locked, so accept() must not
+   *   take a device lock.
    */
 
   FAR void *accept_private;
@@ -1265,8 +1269,12 @@ int tcp_listen(FAR struct tcp_conn_s *conn);
  * Description:
  *   Accept the new connection for the specified listening port.
  *
+ *   The listener's accept() handler, or the TCP_BACKLOG event of its
+ *   callbacks, runs with the TCP connection list locked: they must not
+ *   take a device lock, which comes before the list lock.
+ *
  * Assumptions:
- *   Called from network stack logic with the network stack locked
+ *   Called from the input path with the device locked.
  *
  ****************************************************************************/
 
diff --git a/net/tcp/tcp_listen.c b/net/tcp/tcp_listen.c
index 3cbd9ec5e2e..bfbaccf09fa 100644
--- a/net/tcp/tcp_listen.c
+++ b/net/tcp/tcp_listen.c
@@ -234,7 +234,7 @@ int tcp_listen(FAR struct tcp_conn_s *conn)
  *   Accept the new connection for the specified listening port.
  *
  * Assumptions:
- *   Called with the network locked.
+ *   Called from the input path with the device locked.
  *
  ****************************************************************************/
 
@@ -247,8 +247,15 @@ int tcp_accept_connection(FAR struct net_driver_s *dev,
   /* The event processing logic has already allocated and initialized a TCP
    * connection -- now check there if is an application in place to accept
    * the connection.
+   *
+   * Keep the connection list locked until the listener has the new
+   * connection: close() of the listener removes it from the table of
+   * listeners (tcp_unlisten()) and takes its backlog away (tcp_free())
+   * under this lock, and then frees it.
    */
 
+  tcp_conn_list_lock();
+
 #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
   listener = tcp_findlistener(&conn->u, portno, conn->domain);
 #else
@@ -291,6 +298,7 @@ int tcp_accept_connection(FAR struct net_driver_s *dev,
       conn_unlock(&listener->sconn);
     }
 
+  tcp_conn_list_unlock();
   return ret;
 }
 

Reply via email to