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

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

commit d0c19a47ff5fbbf113f4ce0defac801a4e24a2bf
Author: Bowen Wang <[email protected]>
AuthorDate: Thu Nov 12 12:49:03 2026 +0800

    fs/rpmsgfs: free client priv from the endpoint release callback
    
    rpmsgfs_client_unbind() frees priv as soon as
    rpmsg_unregister_callback() returns, but that only drops one endpoint
    reference.  An endpoint still held by an in-flight message survives,
    and its callback then runs on the freed priv:
    
      umount                          rx thread
        |                                |
        |                          rpmsg_virtio_rx_callback()
        |                            incref: ept refcnt 1 -> 2
      rpmsgfs_client_unbind()            |
        rpmsg_unregister_callback()      |
          rpmsgfs_device_destroy()       |
            rpmsg_destroy_ept()          |
              decref: refcnt 2 -> 1    (ept stays alive)
        fs_heap_free(priv)               |
                                    rpmsgfs_ept_cb()
                                      ept->priv         <- use after free
    
    Free priv from the endpoint's release callback instead, so that it lives
    for as long as the endpoint may still be used.
    
    Signed-off-by: Bowen Wang <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 fs/rpmsgfs/rpmsgfs_client.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/fs/rpmsgfs/rpmsgfs_client.c b/fs/rpmsgfs/rpmsgfs_client.c
index 204de1ce572..9e7f8f1eafc 100644
--- a/fs/rpmsgfs/rpmsgfs_client.c
+++ b/fs/rpmsgfs/rpmsgfs_client.c
@@ -35,6 +35,7 @@
 #include <unistd.h>
 
 #include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
 #include <nuttx/fs/ioctl.h>
 #include <nuttx/rpmsg/rpmsg.h>
 #include <nuttx/semaphore.h>
@@ -94,6 +95,7 @@ static void rpmsgfs_device_created(struct rpmsg_device *rdev,
                                    FAR void *priv_);
 static void rpmsgfs_device_destroy(struct rpmsg_device *rdev,
                                    FAR void *priv_);
+static void rpmsgfs_ept_release(FAR struct rpmsg_endpoint *ept);
 static int  rpmsgfs_ept_cb(FAR struct rpmsg_endpoint *ept,
                            FAR void *data, size_t len, uint32_t src,
                            FAR void *priv);
@@ -348,6 +350,14 @@ static void rpmsgfs_ns_bound(struct rpmsg_endpoint *ept)
   rpmsg_post(&priv->ept, &priv->wait);
 }
 
+static void rpmsgfs_ept_release(FAR struct rpmsg_endpoint *ept)
+{
+  FAR struct rpmsgfs_s *priv = container_of(ept, struct rpmsgfs_s, ept);
+
+  nxsem_destroy(&priv->wait);
+  fs_heap_free(priv);
+}
+
 static void rpmsgfs_device_created(FAR struct rpmsg_device *rdev,
                                    FAR void *priv_)
 {
@@ -358,6 +368,7 @@ static void rpmsgfs_device_created(FAR struct rpmsg_device 
*rdev,
     {
       priv->ept.priv = priv;
       priv->ept.ns_bound_cb = rpmsgfs_ns_bound;
+      priv->ept.release_cb = rpmsgfs_ept_release;
       snprintf(buf, sizeof(buf), "%s%p", RPMSGFS_NAME_PREFIX, priv);
       rpmsg_create_ept(&priv->ept, rdev, buf,
                        RPMSG_ADDR_ANY, RPMSG_ADDR_ANY,
@@ -824,16 +835,12 @@ int rpmsgfs_client_bind(FAR void **handle, FAR const char 
*cpuname)
 
 int rpmsgfs_client_unbind(FAR void *handle)
 {
-  struct rpmsgfs_s *priv = handle;
-
-  rpmsg_unregister_callback(priv,
+  rpmsg_unregister_callback(handle,
                             rpmsgfs_device_created,
                             rpmsgfs_device_destroy,
                             NULL,
                             NULL);
 
-  nxsem_destroy(&priv->wait);
-  fs_heap_free(priv);
   return 0;
 }
 

Reply via email to