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 52a84634e4a4237a092226175034bf57b8e318db
Author: Yongrong Wang <[email protected]>
AuthorDate: Thu Jan 16 14:03:44 2025 +0800

    fs/rpmsgfs/rpmsgfs_server: check the buffer size in readdir
    
    The readdir handler stored MIN(rpmsg_get_tx_buffer_size(),
    rpmsg_get_rx_buffer_size()) in a size_t, so a negative error from
    either turned into a huge size and the name was copied past the end of
    the message. Keep it signed, reply with the error when it is negative,
    and skip the entry when the buffer is smaller than the request.
    
    Signed-off-by: Yongrong Wang <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 fs/rpmsgfs/rpmsgfs_server.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/fs/rpmsgfs/rpmsgfs_server.c b/fs/rpmsgfs/rpmsgfs_server.c
index 26a68e974ed..40b1501d835 100644
--- a/fs/rpmsgfs/rpmsgfs_server.c
+++ b/fs/rpmsgfs/rpmsgfs_server.c
@@ -680,7 +680,7 @@ static int rpmsgfs_readdir_handler(FAR struct 
rpmsg_endpoint *ept,
   FAR struct dirent *entry;
   int ret = -ENOENT;
   FAR void *dir;
-  size_t size;
+  ssize_t size;
 
   dir = rpmsgfs_get_dir(priv, msg->fd);
   if (dir)
@@ -690,15 +690,25 @@ static int rpmsgfs_readdir_handler(FAR struct 
rpmsg_endpoint *ept,
         {
           size = MIN(rpmsg_get_tx_buffer_size(ept),
                      rpmsg_get_rx_buffer_size(ept));
-          size = MIN(size - len, strlen(entry->d_name) + 1);
-          msg->type = entry->d_type;
-          strlcpy(msg->name, entry->d_name, size);
-          msg->ino = entry->d_ino;
-          len += size;
-          ret = 0;
+          if (size < 0)
+            {
+              ret = size;
+              goto out;
+            }
+
+          if (size >= len)
+            {
+              size = MIN(size - len, strlen(entry->d_name) + 1);
+              msg->type = entry->d_type;
+              strlcpy(msg->name, entry->d_name, size);
+              msg->ino = entry->d_ino;
+              len += size;
+              ret = 0;
+            }
         }
     }
 
+out:
   msg->header.result = ret;
   return rpmsg_send(ept, msg, len);
 }

Reply via email to