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 05b4a2842b2bbd1e3d999cc3e3f253e15d22a5b6
Author: Bowen Wang <[email protected]>
AuthorDate: Thu Jul 30 22:14:27 2026 +0800

    net/rpmsg: detach the endpoint when the connection outlives its device
    
    rpmsg_socket_ept_release() is a release_cb that may decline to free its
    object:  while a file descriptor is still open (conn->crefs != 0) it
    keeps the connection.  rpmsg_ept_decref() however only clears ept->rdev
    for endpoints without a release_cb, assuming that release_cb frees the
    endpoint.  Such a connection is therefore neither freed nor detached
    from its rpmsg device.
    
    Once the device is gone, conn->ept.rdev is a dangling pointer that is
    not NULL, every "if (conn->ept.rdev)" guard still passes and
    rpmsg_socket_close() touches freed memory.  It happens whenever an rpmsg
    device is destroyed while a socket to it is open, e.g. a router edge
    device torn down by RPMSG_ROUTER_DESTROY, which only calls
    rpmsg_socket_ns_unbind() for the socket.  After the freed device memory
    was reused, close() jumped into it:
    
      EXCCAUSE=12 (InstrFetchError) PC=0xfffa0001 EXCVADDR=0xfffa0000
    
      rpmsg_send_offchannel_raw+94    callx8 a3, a3=0xfffa0001
      rpmsg_send_ns_message+39
      rpmsg_destroy_ept+37
      rpmsg_socket_close+431
      psock_close+36
      sock_file_close+9
      file_close+26
      file_put+41
      fdlist_close+50
      nx_close+24
      close+9
    
    Clear ept->rdev in rpmsg_socket_ept_release(), so that close() simply
    frees the connection.  This also covers a virtio device removed by
    rpmsg_deinit_vdev() while a socket is open.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 net/rpmsg/rpmsg_sockif.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/net/rpmsg/rpmsg_sockif.c b/net/rpmsg/rpmsg_sockif.c
index 3bfa45a6fcc..7647231565b 100644
--- a/net/rpmsg/rpmsg_sockif.c
+++ b/net/rpmsg/rpmsg_sockif.c
@@ -690,6 +690,13 @@ static void rpmsg_socket_ept_release(FAR struct 
rpmsg_endpoint *ept)
 {
   FAR struct rpmsg_socket_conn_s *conn = ept->priv;
 
+  /* Detach the ept from its rpmsg device: the conn may outlive the device
+   * when a file descriptor is still open, and a stale rdev would keep all
+   * the "if (conn->ept.rdev)" guards passing on freed memory.
+   */
+
+  ept->rdev = NULL;
+
   /* Release conn only when close is called */
 
   if (!conn->crefs)

Reply via email to