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 1aedf8607f83087547a2040cb09656348b21974c
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 6 10:06:40 2026 +0100

    syscall: Read a path from user memory once in a kernel build.
    
    A system call gives the kernel the pointer to a path in user memory, and
    the kernel reads it many times, also with the inode tree lock held.  A bad
    pointer, or a thread that changes or unmaps the path during the call, then
    faults with the lock held.  When a fault ends the caller, the lock stays
    held, and every later user of the inode tree blocks.
    
    In a kernel build, copy the path to the kernel stack in the system call
    stub, where no lock is held, and call the function with the copy.  A "|path"
    after the type in syscall.csv marks these parameters.  The kernel reads user
    memory one time, and the reads with the lock held read the copy.
    
    The loop ioctls get the name of the device, and the setup ioctls also the
    name of the file, in their argument, so they make the same copy.
    
    Flat and protected builds do not change: a fault there stops the system.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 Documentation/os/syscall.rst |  8 +++++
 drivers/loop/loop.c          | 42 ++++++++++++++++++++++++++-
 drivers/mtd/filemtd.c        | 45 +++++++++++++++++++++++++++--
 syscall/syscall.csv          | 62 +++++++++++++++++++--------------------
 tools/mksyscall.c            | 69 ++++++++++++++++++++++++++++++++++++++++++--
 5 files changed, 190 insertions(+), 36 deletions(-)

diff --git a/Documentation/os/syscall.rst b/Documentation/os/syscall.rst
index ad5652a99d8..c2f1631b14e 100644
--- a/Documentation/os/syscall.rst
+++ b/Documentation/os/syscall.rst
@@ -140,6 +140,14 @@ Each type field has a format as follows:
   cannot cast a union sigval to a uinptr_t either.  Rather, we need
   to cast a specific union member fieldname to ``uintptr_t``.
 
+* type name | ``path``:
+
+  For a path that the kernel reads.  In a kernel build the stub reads the
+  path from user memory once, into a copy on the kernel stack, and calls the
+  function with the copy.  Another thread can change or unmap the path during
+  the call, so kernel code must not read it from user memory when it holds a
+  lock.  In other builds this is the same as ``type name``.
+
 Variadic Functions
 ------------------
 
diff --git a/drivers/loop/loop.c b/drivers/loop/loop.c
index 6b5cace73ed..366f7e06c37 100644
--- a/drivers/loop/loop.c
+++ b/drivers/loop/loop.c
@@ -27,6 +27,7 @@
 #include <nuttx/config.h>
 
 #include <sys/types.h>
+#include <limits.h>
 #include <stdbool.h>
 #include <string.h>
 #include <errno.h>
@@ -71,6 +72,29 @@ static const struct file_operations g_loop_fops =
  * Private Functions
  ****************************************************************************/
 
+#ifdef CONFIG_BUILD_KERNEL
+/****************************************************************************
+ * Name: loop_copyname
+ *
+ * Description:
+ *   Read a name in user memory once, into copy (PATH_MAX + 1 bytes): the
+ *   inode tree lock is held when the name is used.  NULL stays NULL.
+ *
+ ****************************************************************************/
+
+static FAR const char *loop_copyname(FAR char *copy, FAR const char *name)
+{
+  if (name == NULL)
+    {
+      return NULL;
+    }
+
+  memccpy(copy, name, '\0', PATH_MAX);
+  copy[PATH_MAX] = '\0';
+  return copy;
+}
+#endif
+
 /****************************************************************************
  * Name: loop_read
  ****************************************************************************/
@@ -118,7 +142,17 @@ static int loop_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
             }
           else
             {
-              ret = losetup(setup->devname, setup->filename, setup->sectsize,
+              FAR const char *devname = setup->devname;
+              FAR const char *filename = setup->filename;
+#ifdef CONFIG_BUILD_KERNEL
+              char name[PATH_MAX + 1];
+              char file[PATH_MAX + 1];
+
+              devname = loop_copyname(name, devname);
+              filename = loop_copyname(file, filename);
+#endif
+
+              ret = losetup(devname, filename, setup->sectsize,
                             setup->offset, setup->readonly);
             }
         }
@@ -142,6 +176,12 @@ static int loop_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
             }
           else
             {
+#ifdef CONFIG_BUILD_KERNEL
+              char name[PATH_MAX + 1];
+
+              devname = loop_copyname(name, devname);
+#endif
+
               ret = loteardown(devname);
             }
         }
diff --git a/drivers/mtd/filemtd.c b/drivers/mtd/filemtd.c
index 92c0102770d..f0f9556716e 100644
--- a/drivers/mtd/filemtd.c
+++ b/drivers/mtd/filemtd.c
@@ -30,6 +30,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/mount.h>
+#include <limits.h>
 #include <stdint.h>
 #include <fcntl.h>
 #include <string.h>
@@ -689,6 +690,30 @@ static ssize_t mtd_loop_write(FAR struct file *filep,
 }
 #endif /* CONFIG_MTD_LOOP */
 
+/****************************************************************************
+ * Name: mtd_loop_copyname
+ *
+ * Description:
+ *   Read a name in user memory once, into copy (PATH_MAX + 1 bytes): the
+ *   inode tree lock is held when the name is used.  NULL stays NULL.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_MTD_LOOP) && defined(CONFIG_BUILD_KERNEL)
+static FAR const char *mtd_loop_copyname(FAR char *copy,
+                                         FAR const char *name)
+{
+  if (name == NULL)
+    {
+      return NULL;
+    }
+
+  memccpy(copy, name, '\0', PATH_MAX);
+  copy[PATH_MAX] = '\0';
+  return copy;
+}
+#endif
+
 /****************************************************************************
  * Name: mtd_loop_ioctl
  ****************************************************************************/
@@ -718,12 +743,22 @@ static int mtd_loop_ioctl(FAR struct file *filep, int cmd,
             }
           else
             {
+              FAR const char *devname = setup->devname;
+              FAR const char *filename = setup->filename;
+#ifdef CONFIG_BUILD_KERNEL
+              char name[PATH_MAX + 1];
+              char file[PATH_MAX + 1];
+
+              devname = mtd_loop_copyname(name, devname);
+              filename = mtd_loop_copyname(file, filename);
+#endif
+
 #  ifndef CONFIG_MTD_CONFIG_NONE
-              ret = mtd_loop_setup(setup->devname, setup->filename,
+              ret = mtd_loop_setup(devname, filename,
                                    setup->sectsize, setup->erasesize,
                                    setup->offset, setup->configdata);
 #  else
-              ret = mtd_loop_setup(setup->devname, setup->filename,
+              ret = mtd_loop_setup(devname, filename,
                                    setup->sectsize, setup->erasesize,
                                    setup->offset);
 #  endif
@@ -749,6 +784,12 @@ static int mtd_loop_ioctl(FAR struct file *filep, int cmd,
             }
           else
             {
+#ifdef CONFIG_BUILD_KERNEL
+              char name[PATH_MAX + 1];
+
+              devname = mtd_loop_copyname(name, devname);
+#endif
+
               ret = mtd_loop_teardown(devname);
             }
         }
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index e71c3d0560b..01cc6634dd8 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -8,9 +8,9 @@
 "aio_write","aio.h","defined(CONFIG_FS_AIO)","int","FAR struct aiocb *"
 "bind","sys/socket.h","defined(CONFIG_NET)","int","int","FAR const struct 
sockaddr *","socklen_t"
 "boardctl","sys/boardctl.h","defined(CONFIG_BOARDCTL)","int","unsigned 
int","uintptr_t"
-"chmod","sys/stat.h","","int","FAR const char *","mode_t"
-"chown","unistd.h","","int","FAR const char *","uid_t","gid_t"
-"chroot","unistd.h","defined(CONFIG_FS_CHROOT)","int","FAR const char *"
+"chmod","sys/stat.h","","int","FAR const char *|path","mode_t"
+"chown","unistd.h","","int","FAR const char *|path","uid_t","gid_t"
+"chroot","unistd.h","defined(CONFIG_FS_CHROOT)","int","FAR const char *|path"
 "clock","time.h","","clock_t"
 
"clock_adjtime","sys/timex.h","defined(CONFIG_CLOCK_ADJTIME)","int","clockid_t","struct
 timex *"
 "clock_getres","time.h","","int","clockid_t","FAR struct timespec *"
@@ -29,7 +29,7 @@
 "epoll_wait","sys/epoll.h","","int","int","FAR struct epoll_event 
*","int","int"
 "eventfd","sys/eventfd.h","defined(CONFIG_EVENT_FD)","int","unsigned int","int"
 "exec","nuttx/binfmt/binfmt.h","!defined(CONFIG_BINFMT_DISABLE) && 
!defined(CONFIG_BUILD_KERNEL)","int","FAR const char *","FAR char * const 
*","FAR char * const *","FAR const struct symtab_s *","int"
-"execve","unistd.h","!defined(CONFIG_BINFMT_DISABLE) && 
defined(CONFIG_LIBC_EXECFUNCS)","int","FAR const char *","FAR char * const 
[]|FAR char * const *","FAR char * const []|FAR char * const *"
+"execve","unistd.h","!defined(CONFIG_BINFMT_DISABLE) && 
defined(CONFIG_LIBC_EXECFUNCS)","int","FAR const char *|path","FAR char * const 
[]|FAR char * const *","FAR char * const []|FAR char * const *"
 "fchmod","sys/stat.h","","int","int","mode_t"
 "fchown","unistd.h","","int","int","uid_t","gid_t"
 "fcntl","fcntl.h","","int","int","int","...","uintptr_t"
@@ -55,38 +55,38 @@
 "gettimeofday","sys/time.h","","int","FAR struct timeval *","FAR struct 
timezone *"
 "getuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","uid_t"
 "getumask","sys/stat.h","","mode_t"
-"inotify_add_watch","sys/inotify.h","defined(CONFIG_FS_NOTIFY)","int","int","FAR
 const char *","uint32_t"
+"inotify_add_watch","sys/inotify.h","defined(CONFIG_FS_NOTIFY)","int","int","FAR
 const char *|path","uint32_t"
 "inotify_init","sys/inotify.h","defined(CONFIG_FS_NOTIFY)","int"
 "inotify_init1","sys/inotify.h","defined(CONFIG_FS_NOTIFY)","int","int"
 
"inotify_rm_watch","sys/inotify.h","defined(CONFIG_FS_NOTIFY)","int","int","int"
-"insmod","nuttx/module.h","defined(CONFIG_MODULE)","FAR void *","FAR const 
char *","FAR const char *"
+"insmod","nuttx/module.h","defined(CONFIG_MODULE)","FAR void *","FAR const 
char *|path","FAR const char *"
 "ioctl","sys/ioctl.h","","int","int","int","...","unsigned long"
 "issetugid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int"
 "kill","signal.h","","int","pid_t","int"
-"lchmod","sys/stat.h","","int","FAR const char *","mode_t"
-"lchown","unistd.h","","int","FAR const char *","uid_t","gid_t"
-"link","unistd.h","defined(CONFIG_FS_LINKS)","int","FAR const char *","FAR 
const char *"
+"lchmod","sys/stat.h","","int","FAR const char *|path","mode_t"
+"lchown","unistd.h","","int","FAR const char *|path","uid_t","gid_t"
+"link","unistd.h","defined(CONFIG_FS_LINKS)","int","FAR const char 
*|path","FAR const char *|path"
 "listen","sys/socket.h","defined(CONFIG_NET)","int","int","int"
 "lseek","unistd.h","","off_t","int","off_t","int"
-"lstat","sys/stat.h","","int","FAR const char *","FAR struct stat *"
-"lutimens","sys/stat.h","","int","FAR const char *","const struct timespec 
[2]|FAR const struct timespec *"
-"mkdir","sys/stat.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char *","mode_t"
+"lstat","sys/stat.h","","int","FAR const char *|path","FAR struct stat *"
+"lutimens","sys/stat.h","","int","FAR const char *|path","const struct 
timespec [2]|FAR const struct timespec *"
+"mkdir","sys/stat.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char *|path","mode_t"
 "mlock","sys/mman.h","","int","FAR const void *","size_t"
 "mlockall","sys/mman.h","","int","int"
 "mmap","sys/mman.h","","FAR void *","FAR void 
*","size_t","int","int","int","off_t"
 "mprotect","sys/mman.h","","int","FAR void *","size_t","int"
 "modhandle","nuttx/module.h","defined(CONFIG_MODULE)","FAR void *","FAR const 
char *"
-"mount","sys/mount.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char *","FAR const char *","FAR const char *","unsigned long","FAR const void *"
+"mount","sys/mount.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char *|path","FAR const char *|path","FAR const char *","unsigned long","FAR 
const void *"
 "mq_close","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","int","mqd_t"
 "mq_getattr","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","int","mqd_t","FAR 
struct mq_attr *"
 "mq_notify","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","int","mqd_t","FAR 
const struct sigevent *"
-"mq_open","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","mqd_t","FAR const char 
*","int","...","mode_t","FAR struct mq_attr *"
+"mq_open","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","mqd_t","FAR const char 
*|path","int","...","mode_t","FAR struct mq_attr *"
 
"mq_receive","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","ssize_t","mqd_t","FAR
 char *","size_t","FAR unsigned int *"
 "mq_send","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","int","mqd_t","FAR 
const char *","size_t","unsigned int"
 "mq_setattr","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","int","mqd_t","FAR 
const struct mq_attr *","FAR struct mq_attr *"
 
"mq_timedreceive","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","ssize_t","mqd_t","FAR
 char *","size_t","FAR unsigned int *","FAR const struct timespec *"
 "mq_timedsend","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","int","mqd_t","FAR 
const char *","size_t","unsigned int","FAR const struct timespec *"
-"mq_unlink","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","int","FAR const char 
*"
+"mq_unlink","mqueue.h","!defined(CONFIG_DISABLE_MQUEUE)","int","FAR const char 
*|path"
 
"msgctl","sys/msg.h","!defined(CONFIG_DISABLE_MQUEUE_SYSV)","int","int","int","FAR
 struct msqid_ds *"
 "msgget","sys/msg.h","!defined(CONFIG_DISABLE_MQUEUE_SYSV)","int","key_t","int"
 
"msgrcv","sys/msg.h","!defined(CONFIG_DISABLE_MQUEUE_SYSV)","ssize_t","int","FAR
 void *","size_t","long","int"
@@ -96,7 +96,7 @@
 "munlockall","sys/mman.h","","int"
 "munmap","sys/mman.h","","int","FAR void *","size_t"
 "nanosleep","time.h","","int","FAR const struct timespec *","FAR struct 
timespec *"
-"nx_mkfifo","nuttx/fs/fs.h","defined(CONFIG_PIPES) && CONFIG_DEV_FIFO_SIZE > 
0","int","FAR const char *","mode_t","size_t"
+"nx_mkfifo","nuttx/fs/fs.h","defined(CONFIG_PIPES) && CONFIG_DEV_FIFO_SIZE > 
0","int","FAR const char *|path","mode_t","size_t"
 
"nx_pthread_create","nuttx/pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_trampoline_t","FAR
 pthread_t *","FAR const pthread_attr_t 
*","pthread_startroutine_t","pthread_addr_t"
 
"nx_pthread_exit","nuttx/pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","noreturn","pthread_addr_t"
 
"nx_vsyslog","nuttx/syslog/syslog.h","!defined(CONFIG_SYSLOG_TO_SCHED_NOTE)","int","int","FAR
 const IPTR char *","FAR va_list *"
@@ -106,21 +106,21 @@
 
"nxsem_close","nuttx/semaphore.h","defined(CONFIG_FS_NAMED_SEMAPHORES)","int","FAR
 sem_t *"
 "nxsem_destroy","nuttx/semaphore.h","","int","FAR sem_t *"
 
"nxsem_getprioceiling","nuttx/semaphore.h","defined(CONFIG_PRIORITY_PROTECT)","int","FAR
 const sem_t *","FAR int *"
-"nxsem_open","nuttx/semaphore.h","defined(CONFIG_FS_NAMED_SEMAPHORES)","int","FAR
 sem_t **","FAR const char *","int","...","mode_t","unsigned int"
+"nxsem_open","nuttx/semaphore.h","defined(CONFIG_FS_NAMED_SEMAPHORES)","int","FAR
 sem_t **","FAR const char *|path","int","...","mode_t","unsigned int"
 "nxsem_post_slow","nuttx/semaphore.h","","int","FAR sem_t *"
 "nxsem_reset","nuttx/semaphore.h","","int","FAR sem_t *","int16_t"
 
"nxsem_set_protocol","nuttx/semaphore.h","defined(CONFIG_PRIORITY_INHERITANCE)","int","FAR
 sem_t *","int"
 
"nxsem_setprioceiling","nuttx/semaphore.h","defined(CONFIG_PRIORITY_PROTECT)","int","FAR
 sem_t *","int","FAR int *"
 "nxsem_timedwait","nuttx/semaphore.h","","int","FAR sem_t *","FAR const struct 
timespec *"
 "nxsem_trywait_slow","nuttx/semaphore.h","","int","FAR sem_t *"
-"nxsem_unlink","nuttx/semaphore.h","defined(CONFIG_FS_NAMED_SEMAPHORES)","int","FAR
 const char *"
+"nxsem_unlink","nuttx/semaphore.h","defined(CONFIG_FS_NAMED_SEMAPHORES)","int","FAR
 const char *|path"
 "nxsem_wait_slow","nuttx/semaphore.h","","int","FAR sem_t *"
-"open","fcntl.h","","int","FAR const char *","int","...","mode_t"
+"open","fcntl.h","","int","FAR const char *|path","int","...","mode_t"
 "pause","unistd.h","!defined(CONFIG_DISABLE_ALL_SIGNALS)","int"
 "pgalloc", "nuttx/arch.h", "defined(CONFIG_BUILD_KERNEL)", "uintptr_t", 
"uintptr_t", "unsigned int"
 "pipe2","unistd.h","defined(CONFIG_PIPES) && CONFIG_DEV_PIPE_SIZE > 
0","int","int [2]|FAR int *","int"
 "poll","poll.h","","int","FAR struct pollfd *","nfds_t","int"
-"posix_spawn","spawn.h","!defined(CONFIG_BINFMT_DISABLE) && 
defined(CONFIG_LIBC_EXECFUNCS)","int","FAR pid_t *","FAR const char *","FAR 
const posix_spawn_file_actions_t *","FAR const posix_spawnattr_t *","FAR char * 
const []|FAR char * const *","FAR char * const []|FAR char * const *"
+"posix_spawn","spawn.h","!defined(CONFIG_BINFMT_DISABLE) && 
defined(CONFIG_LIBC_EXECFUNCS)","int","FAR pid_t *","FAR const char 
*|path","FAR const posix_spawn_file_actions_t *","FAR const posix_spawnattr_t 
*","FAR char * const []|FAR char * const *","FAR char * const []|FAR char * 
const *"
 "ppoll","poll.h","!defined(CONFIG_DISABLE_ALL_SIGNALS)","int","FAR struct 
pollfd *","nfds_t","FAR const struct timespec *","FAR const sigset_t *"
 "prctl","sys/prctl.h","","int","int","...","uintptr_t","uintptr_t"
 "pread","unistd.h","","ssize_t","int","FAR void *","size_t","off_t"
@@ -138,12 +138,12 @@
 "pwrite","unistd.h","","ssize_t","int","FAR const void *","size_t","off_t"
 "read","unistd.h","","ssize_t","int","FAR void *","size_t"
 "readv","sys/uio.h","","ssize_t","int","FAR const struct iovec *","int"
-"readlink","unistd.h","defined(CONFIG_FS_LINKS)","ssize_t","FAR const char 
*","FAR char *","size_t"
+"readlink","unistd.h","defined(CONFIG_FS_LINKS)","ssize_t","FAR const char 
*|path","FAR char *","size_t"
 "recv","sys/socket.h","defined(CONFIG_NET)","ssize_t","int","FAR void 
*","size_t","int"
 "recvfrom","sys/socket.h","defined(CONFIG_NET)","ssize_t","int","FAR 
void*","size_t","int","FAR struct sockaddr*","FAR socklen_t*"
 "recvmsg","sys/socket.h","defined(CONFIG_NET)","ssize_t","int","FAR struct 
msghdr *","int"
-"rename","stdio.h","","int","FAR const char *","FAR const char *"
-"rmdir","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char*"
+"rename","stdio.h","","int","FAR const char *|path","FAR const char *|path"
+"rmdir","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char*|path"
 "rmmod","nuttx/module.h","defined(CONFIG_MODULE)","int","FAR void *"
 
"sched_backtrace","sched.h","defined(CONFIG_SCHED_BACKTRACE)","int","pid_t","FAR
 void **","int","int"
 
"sched_getaffinity","sched.h","defined(CONFIG_SMP)","int","pid_t","size_t","FAR 
cpu_set_t *"
@@ -180,8 +180,8 @@
 "setsockopt","sys/socket.h","defined(CONFIG_NET) && 
defined(CONFIG_NET_SOCKOPTS)","int","int","int","int","FAR const void 
*","socklen_t"
 "settimeofday","sys/time.h","","int","FAR const struct timeval *","FAR const 
struct timezone *"
 "setuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","uid_t"
-"shm_open","sys/mman.h","defined(CONFIG_FS_SHMFS)","int","FAR const char 
*","int","mode_t"
-"shm_unlink","sys/mman.h","defined(CONFIG_FS_SHMFS)","int","FAR const char *"
+"shm_open","sys/mman.h","defined(CONFIG_FS_SHMFS)","int","FAR const char 
*|path","int","mode_t"
+"shm_unlink","sys/mman.h","defined(CONFIG_FS_SHMFS)","int","FAR const char 
*|path"
 "shmat","sys/shm.h","defined(CONFIG_MM_SHM)","FAR void *","int","FAR const 
void *","int"
 "shmctl","sys/shm.h","defined(CONFIG_MM_SHM)","int","int","int","FAR struct 
shmid_ds *"
 "shmdt","sys/shm.h","defined(CONFIG_MM_SHM)","int","FAR const void *"
@@ -198,9 +198,9 @@
 "sigwaitinfo","signal.h","!defined(CONFIG_DISABLE_ALL_SIGNALS)","int","FAR 
const sigset_t *","FAR struct siginfo *"
 "socket","sys/socket.h","defined(CONFIG_NET)","int","int","int","int"
 "socketpair","sys/socket.h","defined(CONFIG_NET)","int","int","int","int","int 
[2]|FAR int *"
-"stat","sys/stat.h","","int","FAR const char *","FAR struct stat *"
-"statfs","sys/statfs.h","","int","FAR const char *","FAR struct statfs *"
-"symlink","unistd.h","defined(CONFIG_FS_LINKS)","int","FAR const char *","FAR 
const char *"
+"stat","sys/stat.h","","int","FAR const char *|path","FAR struct stat *"
+"statfs","sys/statfs.h","","int","FAR const char *|path","FAR struct statfs *"
+"symlink","unistd.h","defined(CONFIG_FS_LINKS)","int","FAR const char 
*|path","FAR const char *|path"
 "sync","unistd.h","","void"
 "sysinfo","sys/sysinfo.h","","int","FAR struct sysinfo *"
 "task_create","sched.h","!defined(CONFIG_BUILD_KERNEL)", "int","FAR const char 
*","int","int","main_t","FAR char * const []|FAR char * const *"
@@ -218,10 +218,10 @@
 "timerfd_gettime","sys/timerfd.h","defined(CONFIG_TIMER_FD)","int","int","FAR 
struct itimerspec *"
 
"timerfd_settime","sys/timerfd.h","defined(CONFIG_TIMER_FD)","int","int","int","FAR
 const struct itimerspec *","FAR struct itimerspec *"
 "umask","sys/stat.h","","mode_t","mode_t"
-"umount2","sys/mount.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char *","unsigned int"
-"unlink","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char *"
+"umount2","sys/mount.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char *|path","unsigned int"
+"unlink","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char *|path"
 "up_fork","nuttx/arch.h","defined(CONFIG_ARCH_HAVE_VFORK) || 
defined(CONFIG_ARCH_HAVE_FORK)","pid_t","bool"
-"utimens","sys/stat.h","","int","FAR const char *","const struct timespec 
[2]|FAR const struct timespec *"
+"utimens","sys/stat.h","","int","FAR const char *|path","const struct timespec 
[2]|FAR const struct timespec *"
 "wait","sys/wait.h","defined(CONFIG_SCHED_WAITPID) && 
defined(CONFIG_SCHED_HAVE_PARENT)","pid_t","FAR int *"
 "waitid","sys/wait.h","defined(CONFIG_SCHED_WAITPID) && 
defined(CONFIG_SCHED_HAVE_PARENT)","int","idtype_t","id_t"," FAR siginfo_t 
*","int"
 "waitpid","sys/wait.h","defined(CONFIG_SCHED_WAITPID)","pid_t","pid_t","FAR 
int *","int"
diff --git a/tools/mksyscall.c b/tools/mksyscall.c
index 6fcfc27be2a..c2ae9005c39 100644
--- a/tools/mksyscall.c
+++ b/tools/mksyscall.c
@@ -52,6 +52,13 @@ static bool is_union(const char *type)
   return strncmp(type, "union ", 6) == 0;
 }
 
+static bool is_path(const char *type)
+{
+  size_t len = strlen(type);
+
+  return len > 5 && strcmp(type + len - 5, "|path") == 0;
+}
+
 static const char *check_funcptr(const char *type)
 {
   const char *str = strstr(type, "(*)");
@@ -116,7 +123,7 @@ static void get_actualparmtype(const char *arg, char 
*actual)
 {
   const char *pstart = strchr(arg, '|');
 
-  if (pstart)
+  if (pstart && !is_path(arg))
     {
       /* The actual parameter type starts after the '|' */
 
@@ -125,7 +132,8 @@ static void get_actualparmtype(const char *arg, char 
*actual)
   else
     {
       /* The actual parameter is the same as the formal parameter
-       * at starts at the beginning of the type string.
+       * at starts at the beginning of the type string.  "type|path"
+       * has no actual type.
        */
 
       pstart = arg;
@@ -435,6 +443,18 @@ static void generate_stub(int nfixed, int nparms)
   fprintf(stream, "#include <string.h>\n");
   fprintf(stream, "#endif\n");
 
+  for (i = 0; i < nparms; i++)
+    {
+      if (is_path(g_parm[PARM1_INDEX + i]))
+        {
+          fprintf(stream, "#ifdef CONFIG_BUILD_KERNEL\n");
+          fprintf(stream, "#include <limits.h>\n");
+          fprintf(stream, "#include <string.h>\n");
+          fprintf(stream, "#endif\n");
+          break;
+        }
+    }
+
   if (strlen(g_parm[HEADER_INDEX]) > 0)
     {
       fprintf(stream, "#include <%s>\n", g_parm[HEADER_INDEX]);
@@ -467,6 +487,51 @@ static void generate_stub(int nfixed, int nparms)
 
   fprintf(stream, ")\n{\n");
 
+  /* In a kernel build another thread of the caller can change or unmap a
+   * path at any time.  Read it from user memory once, here, where no lock is
+   * held.  The function called below only sees the copy.
+   */
+
+  for (i = 0; i < nparms; i++)
+    {
+      if (is_path(g_parm[PARM1_INDEX + i]))
+        {
+          break;
+        }
+    }
+
+  if (i < nparms)
+    {
+      int j;
+
+      fprintf(stream, "#ifdef CONFIG_BUILD_KERNEL\n");
+
+      for (j = i; j < nparms; j++)
+        {
+          if (is_path(g_parm[PARM1_INDEX + j]))
+            {
+              fprintf(stream, "  char _path%d[PATH_MAX + 1];\n", j + 1);
+            }
+        }
+
+      for (j = i; j < nparms; j++)
+        {
+          if (is_path(g_parm[PARM1_INDEX + j]))
+            {
+              fprintf(stream, "\n  if (parm%d != 0)\n    {\n", j + 1);
+              fprintf(stream, "      memccpy(_path%d, (FAR const char *)"
+                              "parm%d, '\\0', PATH_MAX);\n",
+                              j + 1, j + 1);
+              fprintf(stream, "      _path%d[PATH_MAX] = '\\0';\n", j + 1);
+              fprintf(stream, "      parm%d = (uintptr_t)_path%d;\n",
+                              j + 1, j + 1);
+              fprintf(stream, "    }\n");
+            }
+        }
+
+      fprintf(stream, "#endif\n\n");
+    }
+
   /* Fixed union illegal type for cast */
 
   for (i = 0; i < nparms; i++)

Reply via email to