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 299ba8264dfdaeb91b5fdcf7afc48e172d3bba36
Author: zhanghongyu <[email protected]>
AuthorDate: Thu Dec 25 20:51:19 2025 +0800

    tcp_close.c: delayed release of TCP resources in TCP_SYN state
    
    Closing a TCP connection while it is still in TCP_SYN_SENT/TCP_SYN_RCVD
    can free the connection while the SYN handshake work is still pending,
    leading to a use-after-free.  Defer the release of the TCP resources in
    the SYN states until the pending work has completed.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 net/tcp/tcp_close.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/net/tcp/tcp_close.c b/net/tcp/tcp_close.c
index 3ba69207ce4..6acc8fdb9e0 100644
--- a/net/tcp/tcp_close.c
+++ b/net/tcp/tcp_close.c
@@ -303,6 +303,17 @@ static inline int tcp_close_disconnect(FAR struct socket 
*psock)
       tcp_send_txnotify(psock, conn);
       conn_dev_unlock(&conn->sconn, conn->dev);
     }
+  else if (conn->tcpstateflags == TCP_SYN_SENT ||
+           conn->tcpstateflags == TCP_SYN_RCVD)
+    {
+      /* Free network resources */
+
+      conn->flags |= TCP_CLOSE_ARRANGED;
+      conn->tcpstateflags = TCP_CLOSED;
+      work_queue(LPWORK, &conn->clswork, tcp_close_work, conn,
+                 HSEC2TICK(conn->timer));
+      conn_dev_unlock(&conn->sconn, conn->dev);
+    }
   else
     {
       /* Stop the network monitor for all sockets */

Reply via email to