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 b632d89dda0 fs/tmpfs: validate seek offsets before changing the file
position
b632d89dda0 is described below
commit b632d89dda03cc62fb3993e9d625df6707f1c6db
Author: jingfei <[email protected]>
AuthorDate: Fri Nov 27 11:35:07 2026 +0800
fs/tmpfs: validate seek offsets before changing the file position
Reject negative results with EINVAL and additions beyond off_t with
EOVERFLOW. Check the operands before adding and leave f_pos unchanged
on failure, including failed pread and pwrite seeks. Hold the file lock
while reading the current position or size and committing the new offset.
Signed-off-by: jingfei <[email protected]>
---
fs/tmpfs/fs_tmpfs.c | 56 +++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 46 insertions(+), 10 deletions(-)
diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index 49f1be74947..67da959a79f 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -60,6 +60,12 @@
# warning CONFIG_FS_TMPFS_FILE_FREEGUARD needs to be > ALLOCGUARD
#endif
+#ifdef CONFIG_FS_LARGEFILE
+# define TMPFS_OFF_MAX INT64_MAX
+#else
+# define TMPFS_OFF_MAX INT32_MAX
+#endif
+
#define tmpfs_lock(fs) \
nxrmutex_lock(&fs->tfs_lock)
#define tmpfs_lock_object(to) \
@@ -2141,6 +2147,7 @@ static off_t tmpfs_seek(FAR struct file *filep, off_t
offset, int whence)
{
FAR struct tmpfs_file_s *tfo;
off_t position;
+ int ret;
finfo("filep: %p\n", filep);
DEBUGASSERT(filep->f_priv != NULL);
@@ -2149,31 +2156,60 @@ static off_t tmpfs_seek(FAR struct file *filep, off_t
offset, int whence)
tfo = filep->f_priv;
+ ret = tmpfs_lock_file(tfo);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
/* Map the offset according to the whence option */
switch (whence)
{
- case SEEK_SET: /* The offset is set to offset bytes. */
- position = offset;
+ case SEEK_SET:
+ position = 0;
break;
- case SEEK_CUR: /* The offset is set to its current location plus
- * offset bytes. */
- position = offset + filep->f_pos;
+ case SEEK_CUR:
+ position = filep->f_pos;
break;
- case SEEK_END: /* The offset is set to the size of the file plus
- * offset bytes. */
- position = offset + tfo->tfo_size;
+ case SEEK_END:
+ if (tfo->tfo_size > TMPFS_OFF_MAX)
+ {
+ position = -EOVERFLOW;
+ goto out;
+ }
+
+ position = tfo->tfo_size;
break;
default:
- return -EINVAL;
+ position = -EINVAL;
+ goto out;
+ }
+
+ /* Check before adding so failure preserves the file position and the
+ * addition cannot overflow off_t.
+ */
+
+ if (offset < -position)
+ {
+ position = -EINVAL;
+ goto out;
}
- /* Save the new file position */
+ if (offset > TMPFS_OFF_MAX - position)
+ {
+ position = -EOVERFLOW;
+ goto out;
+ }
+ position += offset;
filep->f_pos = position;
+
+out:
+ tmpfs_unlock_file(tfo);
return position;
}