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


The following commit(s) were added to refs/heads/master by this push:
     new b2d94ac897d drivers/loop: fix wrong-offset transfers from unprotected 
f_pos
b2d94ac897d is described below

commit b2d94ac897d607036cf668f37f3038266e32ccf6
Author: v-zhangxiaomeng5 <[email protected]>
AuthorDate: Thu Sep 3 15:59:55 2026 +0800

    drivers/loop: fix wrong-offset transfers from unprotected f_pos
    
    loop_read() and loop_write() seek dev->devfile and then transfer from the
    current position, but that position is shared by every user of the loop
    device and the seek/transfer pair was not serialized. A concurrent reader
    could move f_pos in between, so the transfer used the wrong offset and
    silently returned data from elsewhere in the backing file. Hold the
    existing dev->lock across the seek and the transfer. The lock is a plain
    mutex, so stacking a loop device on its own backing file is unsupported.
    Also let loop_write() bail out on a failed seek, as loop_read() already
    does, instead of writing at the current position.
    
    Signed-off-by: v-zhangxiaomeng5 <[email protected]>
---
 drivers/loop/losetup.c | 40 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 4 deletions(-)

diff --git a/drivers/loop/losetup.c b/drivers/loop/losetup.c
index 13dce1689f5..21c03823770 100644
--- a/drivers/loop/losetup.c
+++ b/drivers/loop/losetup.c
@@ -41,6 +41,7 @@
 #include <assert.h>
 #include <nuttx/debug.h>
 #include <errno.h>
+#include <inttypes.h>
 
 #include <nuttx/kmalloc.h>
 #include <nuttx/fs/fs.h>
@@ -200,11 +201,26 @@ static ssize_t loop_read(FAR struct inode *inode, FAR 
unsigned char *buffer,
   /* Calculate the offset to read the sectors and seek to the position */
 
   offset = start_sector * dev->sectsize + dev->offset;
+
+  /* dev->devfile's file position is shared by all users of this loop device,
+   * so the seek and read below must be atomic; a concurrent reader could
+   * otherwise make this call return data from the wrong offset. dev->lock
+   * is a plain mutex, so a loop device must not nest on its backing file.
+   */
+
+  ret = nxmutex_lock(&dev->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
   ret = file_seek(&dev->devfile, offset, SEEK_SET);
   if (ret < 0)
     {
-      ferr("ERROR: Seek failed for offset=%d: %d\n", (int)offset, (int)ret);
-      return -EIO;
+      ferr("ERROR: Seek failed for offset=%" PRIdOFF ": %" PRIdOFF "\n",
+           offset, ret);
+      nxmutex_unlock(&dev->lock);
+      return ret;
     }
 
   /* Then read the requested number of sectors from that position */
@@ -216,11 +232,14 @@ static ssize_t loop_read(FAR struct inode *inode, FAR 
unsigned char *buffer,
       if (nbytesread < 0 && nbytesread != -EINTR)
         {
           ferr("ERROR: Read failed: %zd\n", nbytesread);
-          return (int)nbytesread;
+          nxmutex_unlock(&dev->lock);
+          return nbytesread;
         }
     }
   while (nbytesread < 0);
 
+  nxmutex_unlock(&dev->lock);
+
   /* Return the number of sectors read */
 
   return nbytesread / dev->sectsize;
@@ -248,10 +267,20 @@ static ssize_t loop_write(FAR struct inode *inode,
   /* Calculate the offset to write the sectors and seek to the position */
 
   offset = start_sector * dev->sectsize + dev->offset;
+
+  ret = nxmutex_lock(&dev->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
   ret = file_seek(&dev->devfile, offset, SEEK_SET);
   if (ret < 0)
     {
-      ferr("ERROR: Seek failed for offset=%d: %d\n", (int)offset, (int)ret);
+      ferr("ERROR: Seek failed for offset=%" PRIdOFF ": %" PRIdOFF "\n",
+           offset, ret);
+      nxmutex_unlock(&dev->lock);
+      return ret;
     }
 
   /* Then write the requested number of sectors to that position */
@@ -263,11 +292,14 @@ static ssize_t loop_write(FAR struct inode *inode,
       if (nbyteswritten < 0 && nbyteswritten != -EINTR)
         {
           ferr("ERROR: file_write failed: %zd\n", nbyteswritten);
+          nxmutex_unlock(&dev->lock);
           return nbyteswritten;
         }
     }
   while (nbyteswritten < 0);
 
+  nxmutex_unlock(&dev->lock);
+
   /* Return the number of sectors written */
 
   return nbyteswritten / dev->sectsize;

Reply via email to