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 01829d2136b fs/psedofs: add a readonly defconfig for psedo file
01829d2136b is described below

commit 01829d2136b96ad612b61a2cf32b447d257b0f35
Author: jingfei <[email protected]>
AuthorDate: Tue Apr 28 14:08:28 2026 +0800

    fs/psedofs: add a readonly defconfig for psedo file
    
    Make pseudo files read-only. When enabled, opening pseudo files
    with write access, writing data, or truncating the file is denied.
    
    Signed-off-by: jingfei <[email protected]>
---
 Documentation/os/filesystem/pseudofs.rst |  8 ++++++
 fs/Kconfig                               |  8 ++++++
 fs/vfs/fs_pseudofile.c                   | 47 +++++++++++++++++++++++++++-----
 3 files changed, 56 insertions(+), 7 deletions(-)

diff --git a/Documentation/os/filesystem/pseudofs.rst 
b/Documentation/os/filesystem/pseudofs.rst
index e8397094fac..cdc120783dd 100644
--- a/Documentation/os/filesystem/pseudofs.rst
+++ b/Documentation/os/filesystem/pseudofs.rst
@@ -215,3 +215,11 @@ See also NxFileSystem in
 When ``CONFIG_FS_PERMISSION`` is enabled, pseudoFS directory modes also gate
 access into mounted volumes beneath them.  See
 :ref:`file-permission`.
+
+Pseudo-files can be made read-only with
+``CONFIG_PSEUDOFS_FILE_READONLY``.  This option depends on
+``CONFIG_PSEUDOFS_FILE`` and defaults to ``CONFIG_DEFAULT_SMALL``.  In
+read-only mode, opens requesting ``O_WRONLY`` or ``O_RDWR`` fail with
+``EACCES``.  Write and truncate operations are unavailable, and mappings
+requesting ``PROT_WRITE`` fail with ``EACCES``, including ``MAP_PRIVATE``
+mappings.  Read-only opens and mappings are unaffected.
diff --git a/fs/Kconfig b/fs/Kconfig
index 5d88785ddad..321303c736c 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -234,6 +234,14 @@ config PSEUDOFS_FILE
        ---help---
                Support to create a file on pseudo filesystem.
 
+config PSEUDOFS_FILE_READONLY
+       bool "Pseudo files are read-only"
+       default DEFAULT_SMALL
+       depends on PSEUDOFS_FILE
+       ---help---
+               Reject write access to pseudo files and disable write, truncate,
+               and writable memory mapping operations.
+
 config SENDFILE_BUFSIZE
        int "sendfile() buffer size"
        default 512
diff --git a/fs/vfs/fs_pseudofile.c b/fs/vfs/fs_pseudofile.c
index db2bba68123..1dea72565c3 100644
--- a/fs/vfs/fs_pseudofile.c
+++ b/fs/vfs/fs_pseudofile.c
@@ -33,6 +33,7 @@
 #include <limits.h>
 #include <fcntl.h>
 #include <sys/param.h>
+#include <sys/mman.h>
 
 #include <nuttx/kmalloc.h>
 #include <nuttx/fs/fs.h>
@@ -62,8 +63,10 @@ struct fs_pseudofile_s
 
 static int     pseudofile_open(FAR struct file *filep);
 static int     pseudofile_close(FAR struct file *filep);
+#ifndef CONFIG_PSEUDOFS_FILE_READONLY
 static ssize_t pseudofile_write(FAR struct file *filep,
                                 FAR const char *buffer, size_t buflen);
+#endif
 static ssize_t pseudofile_read(FAR struct file *filep, FAR char *buffer,
                                size_t buflen);
 static off_t   pseudofile_seek(FAR struct file *filep, off_t offset,
@@ -74,7 +77,9 @@ static int     pseudofile_munmap(FAR struct task_group_s 
*group,
                                  FAR struct mm_map_entry_s *map,
                                  FAR void *start,
                                  size_t length);
+#ifndef CONFIG_PSEUDOFS_FILE_READONLY
 static int     pseudofile_truncate(FAR struct file *filep, off_t length);
+#endif
 #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
 static int     pseudofile_unlink(FAR struct inode *inode);
 #endif
@@ -88,11 +93,19 @@ static const struct file_operations g_pseudofile_ops =
   pseudofile_open,     /* open */
   pseudofile_close,    /* close */
   pseudofile_read,     /* read */
+#ifdef CONFIG_PSEUDOFS_FILE_READONLY
+  NULL,                /* write */
+#else
   pseudofile_write,    /* write */
+#endif
   pseudofile_seek,     /* seek */
   NULL,                /* ioctl */
   pseudofile_mmap,     /* mmap */
+#ifdef CONFIG_PSEUDOFS_FILE_READONLY
+  NULL,                /* truncate */
+#else
   pseudofile_truncate, /* truncate */
+#endif
   NULL,                /* poll */
   NULL,                /* readv */
   NULL,                /* writev */
@@ -111,6 +124,13 @@ static int pseudofile_open(FAR struct file *filep)
   FAR struct fs_pseudofile_s *pf = node->i_private;
   int ret;
 
+#ifdef CONFIG_PSEUDOFS_FILE_READONLY
+  if ((filep->f_oflags & O_ACCMODE) != O_RDONLY)
+    {
+      return -EACCES;
+    }
+#endif
+
   ret = nxmutex_lock(&pf->lock);
   if (ret < 0)
     {
@@ -171,6 +191,7 @@ static int pseudofile_close(FAR struct file *filep)
 
 /* Grow allocation by doubling until it covers the requested size. */
 
+#ifndef CONFIG_PSEUDOFS_FILE_READONLY
 static size_t pseudofile_buffersize(FAR struct fs_pseudofile_s *pf,
                                     size_t size)
 {
@@ -233,7 +254,9 @@ static int pseudofile_expand(FAR struct inode *node,
   node->i_size = size;
   return 0;
 }
+#endif
 
+#ifndef CONFIG_PSEUDOFS_FILE_READONLY
 static ssize_t pseudofile_write(FAR struct file *filep,
                                 FAR const char *buffer, size_t buflen)
 {
@@ -289,6 +312,7 @@ static ssize_t pseudofile_write(FAR struct file *filep,
   nxmutex_unlock(&pf->lock);
   return buflen;
 }
+#endif
 
 static ssize_t pseudofile_read(FAR struct file *filep, FAR char *buffer,
                                size_t buflen)
@@ -338,19 +362,19 @@ static off_t pseudofile_seek(FAR struct file *filep, 
off_t offset,
   switch (whence)
     {
       case SEEK_SET:
-          break;
+        break;
 
       case SEEK_CUR:
-          offset += filep->f_pos;
-          break;
+        offset += filep->f_pos;
+        break;
 
       case SEEK_END:
-          offset += node->i_size;
-          break;
+        offset += node->i_size;
+        break;
 
       default:
-          nxmutex_unlock(&pf->lock);
-          return -EINVAL;
+        nxmutex_unlock(&pf->lock);
+        return -EINVAL;
     }
 
   if (offset < 0)
@@ -374,6 +398,13 @@ static int pseudofile_mmap(FAR struct file *filep,
   FAR struct fs_pseudofile_s *pf = node->i_private;
   int ret = -EINVAL;
 
+#ifdef CONFIG_PSEUDOFS_FILE_READONLY
+  if ((map->prot & PROT_WRITE) != 0)
+    {
+      return -EACCES;
+    }
+#endif
+
   /* Keep the inode when mmapped, increase refcount */
 
   inode_addref(node);
@@ -434,6 +465,7 @@ static int pseudofile_munmap(FAR struct task_group_s *group,
   return ret;
 }
 
+#ifndef CONFIG_PSEUDOFS_FILE_READONLY
 static int pseudofile_truncate(FAR struct file *filep, off_t length)
 {
   FAR struct inode *node = filep->f_inode;
@@ -483,6 +515,7 @@ out:
   nxmutex_unlock(&pf->lock);
   return ret;
 }
+#endif
 
 #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
 static int pseudofile_unlink(FAR struct inode *node)

Reply via email to