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


The following commit(s) were added to refs/heads/master by this push:
     new fbc87ad6a7e fs/tmpfs: validate write offsets before computing the end
fbc87ad6a7e is described below

commit fbc87ad6a7e1dbc09fd1c8b000510764c44b30fb
Author: jingfei <[email protected]>
AuthorDate: Fri Nov 27 21:48:41 2026 +0800

    fs/tmpfs: validate write offsets before computing the end
    
    Check that the write position and requested end fit both off_t and the 
backing allocation size. Reject invalid positions with EINVAL and 
unrepresentable file ends with EFBIG before updating the file data, size, 
timestamps or position. Keep the existing allocation guard checks.
    
    Signed-off-by: jingfei <[email protected]>
---
 fs/tmpfs/fs_tmpfs.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index 2051eba2f1b..c3a5d082a55 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -2099,6 +2099,12 @@ static ssize_t tmpfs_writev(FAR struct file *filep, FAR 
struct uio *uio)
 
   if ((filep->f_oflags & O_APPEND) != 0)
     {
+      if (tfo->tfo_size > TMPFS_OFF_MAX)
+        {
+          ret = -EFBIG;
+          goto errout_with_lock;
+        }
+
       startpos = tfo->tfo_size;
     }
   else
@@ -2106,6 +2112,23 @@ static ssize_t tmpfs_writev(FAR struct file *filep, FAR 
struct uio *uio)
       startpos = filep->f_pos;
     }
 
+  if (startpos < 0)
+    {
+      ret = -EINVAL;
+      goto errout_with_lock;
+    }
+
+  /* Both the file offset and the backing allocation must represent the end.
+   */
+
+  if ((uintmax_t)startpos > SIZE_MAX ||
+      uio->uio_resid > SIZE_MAX - (size_t)startpos ||
+      uio->uio_resid > TMPFS_OFF_MAX - startpos)
+    {
+      ret = -EFBIG;
+      goto errout_with_lock;
+    }
+
   nwritten = uio->uio_resid;
   endpos   = startpos + nwritten;
 

Reply via email to