jingfei195887 opened a new pull request, #22536:
URL: https://github.com/apache/nuttx/pull/22536
## Summary
Allow a new pseudo-file to open with `CONFIG_FS_PERMISSION` enabled. After
creation succeeds, initialize the lookup suffix to the empty path used for a
resolved pseudoFS node before running the existing permission checks.
## Impact
- Fixes successful pseudo-file creation reaching the permission check with
incomplete lookup state. No API, configuration, allocation or structure-layout
change.
- Parent search/write checks, existing-file access checks and mounted
filesystem dispatch retain their current behavior.
- Required user migration, build-system changes and hardware/driver changes:
NO.
- Documentation changes: NO; the fix restores the documented pseudo-file
permission behavior.
- Permission enforcement: existing checks remain in place, including parent
search/write denial and existing-file access denial.
## Testing
Tested on Linux x86_64 with GCC 13.1.0. Baseline
`f9d3eb13c3f412426a2658227f2b332e21e3a526`; candidate
`917230a99ab6dd52b9d7dfd850450e0efb4ca0e9`. Functional apps revision
`0a37af9866b7e558fe186da4de7d99cc3dda51d7`. The final gate records its apps
revision below.
| Simulator configuration | Before | After |
|---|---|---|
| 64-bit, permissions enabled | 21 controls pass; creation stops with
SIGSEGV | 78 checks, 0 failures |
| 32-bit, permissions enabled | 21 controls pass; creation stops with
SIGSEGV | 78 checks, 0 failures |
| 64-bit, permissions disabled | 84 checks, 0 failures | 84 checks, 0
failures |
Each pair uses identical enabled configuration values. The fixture covers
missing files without `O_CREAT`, parent search/write denial, absolute and
relative creation, `.`/`..` normalization, creation modes, read/write/close,
existing-file permissions and a mounted TMPFS control. The baseline debugger
exits with status 1 after recording the fault; those runs are failures, not
completed tests. The branch completes 240 checks with no failures. Functional
builds have no compiler warnings.
All runs emit the same automatic NSH `/tmp` mount error at startup. The
fixture independently mounts `/pcreate-volume` and checks the mount and
file-operation results.
The small simulator configuration enables `CONFIG_DEFAULT_SMALL`,
pseudo-files, attributes and permission checks. Its text/data/bss is `(555877,
2800, 13120)` before and `(555893, 2800, 13120)` after: delta `(16, 0, 0)`
bytes. Both size builds emit the same existing apps `readline_common.c`
savednch warning.
Protected `qemu-armv7r:pnsh` and kernel `qemu-armv8a:knsh` compile with
pseudo-files and permission checks enabled. Make, CMake, OSTest and the
exact-HEAD gate pass. No physical hardware, SMP/concurrent stress,
protected/kernel runtime or chroot runtime was tested.
<details>
<summary>Actual before/after runtime logs</summary>
`baseline-on`
```text
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Error mount tmpfs '' on '/tmp' failed: 20
nsh> pseudocreatecheck
PCREATE bits=64
PCREATE controls checks=21 failures=0
CREATE /pcreate-absolute flags=2 mode=0600
Program received signal SIGSEGV, Segmentation fault.
0x0000000040046968 in file_vopen (filep=0x7ffff3d23b20, path=0x40119720
"/pcreate-absolute", oflags=66, umask=0, ap=0x7ffff3d667f0) at vfs/fs_open.c:248
248 ret = inode_checkpathperm(inode, INODE_PATH_AMODE(&desc), 0);
#0 0x0000000040046968 in file_vopen (filep=0x7ffff3d23b20, path=0x40119720
"/pcreate-absolute", oflags=66, umask=0, ap=0x7ffff3d667f0) at vfs/fs_open.c:248
#1 0x0000000040047004 in nx_vopen (list=0x7ffff3d239c8, path=0x40119720
"/pcreate-absolute", oflags=66, ap=0x7ffff3d667f0) at vfs/fs_open.c:426
#2 0x0000000040047553 in open (path=0x40119720 "/pcreate-absolute",
oflags=66) at vfs/fs_open.c:585
#3 0x000000004009af92 in create_file (path=0x40119720 "/pcreate-absolute",
flags=2, mode=384) at pseudocreatecheck_main.c:66
#4 0x000000004009c195 in pseudocreatecheck_main (argc=1,
argv=0x7ffff3d54b60) at pseudocreatecheck_main.c:222
#5 0x0000000040027eba in nxtask_startup (entrypt=0x4009be10
<pseudocreatecheck_main>, argc=1, argv=0x7ffff3d54b60) at
sched/task_startup.c:72
#6 0x0000000040010cef in nxtask_start () at task/task_start.c:104
#7 0x0000000040034796 in pre_start () at sim/sim_initialstate.c:54
#8 0x0000000000000000 in ?? ()
#0 0x0000000040046968 in file_vopen (filep=0x7ffff3d23b20, path=0x40119720
"/pcreate-absolute", oflags=66, umask=0, ap=0x7ffff3d667f0) at vfs/fs_open.c:248
248 ret = inode_checkpathperm(inode, INODE_PATH_AMODE(&desc), 0);
$1 = 0x0
A debugging session is active.
Inferior 1 [process 3894248] will be killed.
Quit anyway? (y or n) [answered Y; input not from terminal]
```
`final-on`
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> pseudocreatecheck
PCREATE bits=64
PCREATE controls checks=21 failures=0
CREATE /pcreate-absolute flags=2 mode=0600
CREATE /pcreate-readable flags=0 mode=0400
CREATE /pcreate-writable flags=1 mode=0200
CREATE relative flags=2 mode=0600
CREATE ./dot flags=2 mode=0600
CREATE /pcreate-directory/../pcreate-normalized flags=2 mode=0600
PCREATE RESULT checks=78 failures=0
nsh> poweroff
```
`baseline32-on`
```text
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Error mount tmpfs '' on '/tmp' failed: 20
nsh> pseudocreatecheck
PCREATE bits=32
PCREATE controls checks=21 failures=0
CREATE /pcreate-absolute flags=2 mode=0600
Program received signal SIGSEGV, Segmentation fault.
0x40071f59 in file_vopen (filep=0xf3e73c80, path=0x4017306c
"/pcreate-absolute", oflags=66, umask=0, ap=0xf3eb6b8c "\200\001") at
vfs/fs_open.c:248
248 ret = inode_checkpathperm(inode, INODE_PATH_AMODE(&desc), 0);
#0 0x40071f59 in file_vopen (filep=0xf3e73c80, path=0x4017306c
"/pcreate-absolute", oflags=66, umask=0, ap=0xf3eb6b8c "\200\001") at
vfs/fs_open.c:248
#1 0x400727e4 in nx_vopen (list=0xf3e73b9c, path=0x4017306c
"/pcreate-absolute", oflags=66, ap=0xf3eb6b88 "\200\001") at vfs/fs_open.c:426
#2 0x40072c21 in open (path=0x4017306c "/pcreate-absolute", oflags=66) at
vfs/fs_open.c:585
#3 0x400d8b56 in create_file (path=0x4017306c "/pcreate-absolute", flags=2,
mode=384) at pseudocreatecheck_main.c:66
#4 0x400da39b in pseudocreatecheck_main (argc=1, argv=0xf3ea4df0) at
pseudocreatecheck_main.c:222
#5 0x4004d226 in nxtask_startup (entrypt=0x400da073
<pseudocreatecheck_main>, argc=1, argv=0xf3ea4df0) at sched/task_startup.c:72
#6 0x4002f892 in nxtask_start () at task/task_start.c:104
#7 0x4005c025 in pre_start () at sim/sim_initialstate.c:54
#8 0x00000000 in ?? ()
#0 0x40071f59 in file_vopen (filep=0xf3e73c80, path=0x4017306c
"/pcreate-absolute", oflags=66, umask=0, ap=0xf3eb6b8c "\200\001") at
vfs/fs_open.c:248
248 ret = inode_checkpathperm(inode, INODE_PATH_AMODE(&desc), 0);
$1 = 0x0
A debugging session is active.
Inferior 1 [process 3911132] will be killed.
Quit anyway? (y or n) [answered Y; input not from terminal]
```
`final32-on`
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> pseudocreatecheck
PCREATE bits=32
PCREATE controls checks=21 failures=0
CREATE /pcreate-absolute flags=2 mode=0600
CREATE /pcreate-readable flags=0 mode=0400
CREATE /pcreate-writable flags=1 mode=0200
CREATE relative flags=2 mode=0600
CREATE ./dot flags=2 mode=0600
CREATE /pcreate-directory/../pcreate-normalized flags=2 mode=0600
PCREATE RESULT checks=78 failures=0
nsh> poweroff
```
`baseline-off`
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> pseudocreatecheck
PCREATE bits=64
PCREATE controls checks=25 failures=0
CREATE /pcreate-absolute flags=2 mode=0600
CREATE /pcreate-readable flags=0 mode=0400
CREATE /pcreate-writable flags=1 mode=0200
CREATE relative flags=2 mode=0600
CREATE ./dot flags=2 mode=0600
CREATE /pcreate-directory/../pcreate-normalized flags=2 mode=0600
PCREATE RESULT checks=84 failures=0
nsh> poweroff
```
`final-off`
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> pseudocreatecheck
PCREATE bits=64
PCREATE controls checks=25 failures=0
CREATE /pcreate-absolute flags=2 mode=0600
CREATE /pcreate-readable flags=0 mode=0400
CREATE /pcreate-writable flags=1 mode=0200
CREATE relative flags=2 mode=0600
CREATE ./dot flags=2 mode=0600
CREATE /pcreate-directory/../pcreate-normalized flags=2 mode=0600
PCREATE RESULT checks=84 failures=0
nsh> poweroff
```
</details>
<details>
<summary>Build evidence</summary>
`protected` compiler and build-log tail:
```text
arm-none-eabi-gcc (Arm GNU Toolchain 13.2.rel1 (Build arm-13.7)) 13.2.1
20231009
AS: armv7-r/arm_vectortab.S
CC: qemu_boardinit.c
CC: qemu_bringup.c
CPP:
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm/qemu/qemu-armv7r/scripts/nuttx.ld->
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm/qemu/qemu-armv7r/scripts/nuttx.ld.tmp
LD: nuttx
arm-none-eabi-ld: warning:
/home/mi/test/nuttx_apache/fs-upstream/nuttx/nuttx has a LOAD segment with RWX
permissions
Memory region Used Size Region Size %age Used
kflash: 185584 B 512 KB 35.40%
uflash: 0 GB 512 KB 0.00%
ksram: 12 KB 8 MB 0.15%
usram: 0 GB 8 MB 0.00%
CP: nuttx.bin
qemu-armv7r:pnsh build PASS
```
Warnings in this build:
```text
CC: stdio/lib_perror.c fpu.c:57:8: warning: #warning "FPU test not built;
Only available in the flat build (CONFIG_BUILD_FLAT)" [-Wcpp]
arm-none-eabi-ld: warning:
/home/mi/test/nuttx_apache/fs-upstream/nuttx/nuttx has a LOAD segment with RWX
permissions
```
`kernel` compiler and build-log tail:
```text
aarch64-none-elf-gcc (GCC) 13.4.0
IN: libs/libc/libc.a -> staging/libc.a
CPP:
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm64/qemu/qemu-armv8a/scripts/ld-kernel.script->
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm64/qemu/qemu-armv8a/scripts/ld-kernel.script.tmp
CC: qemu_boardinit.c
CC: qemu_bringup.c
CC: qemu_power.c
LD: nuttx
Memory region Used Size Region Size %age Used
dram: 328 KB 2 MB 16.02%
pgram: 0 B 4 MB 0.00%
CP: nuttx.hex
CP: nuttx.bin
qemu-armv8a:knsh build PASS
```
Final local gate:
```text
target : nuttx
branch : fs/pseudofile-create-relpath
head : 917230a99ab6dd52b9d7dfd850450e0efb4ca0e9
subject : fs/vfs: initialize the path suffix after pseudo-file creation
commits : 1
base : f9d3eb13c3 (merge-base with upstream/master)
behind : 0 commits of upstream/master
nuttx : 917230a99a
apps : 0a37af9866
options : CONFIG_PSEUDOFS_FILE CONFIG_PSEUDOFS_ATTRIBUTES
CONFIG_SCHED_USER_IDENTITY CONFIG_FS_PERMISSION CONFIG_FS_TMPFS
CONFIG_FS_NOTIFY CONFIG_DEBUG_ASSERTIONS
host : Linux x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
checkpatch: rc=0
signer : jingfei <[email protected]>
commits missing your Signed-off-by: 0
make build: rc=0
warnings in changed files: 0
ostest: Exiting with status 0
cmake reconfigure: rc=0
cmake build: rc=0
worktree tracked changes after: 0
RESULT: PASS
```
</details>
<details>
<summary>Local regression fixture and reproduction commands</summary>
The fixture is supplied here as local validation input; this NuttX PR does
not add an application to nuttx-apps. Place these files under
`apps/examples/pseudocreatecheck/`.
`pseudocreatecheck_main.c`
```c
/****************************************************************************
* apps/examples/pseudocreatecheck/pseudocreatecheck_main.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/****************************************************************************
* Private Data
****************************************************************************/
static int g_checks;
static int g_failures;
/****************************************************************************
* Private Functions
****************************************************************************/
static void check(int condition, FAR const char *label)
{
g_checks++;
if (!condition)
{
g_failures++;
printf("FAIL %s errno=%d\n", label, errno);
}
}
static void create_file(FAR const char *path, int flags, mode_t mode)
{
struct stat st;
char data[4];
int fd;
printf("CREATE %s flags=%d mode=%04o\n", path, flags, mode);
fflush(stdout);
fd = open(path, O_CREAT | flags, mode);
check(fd >= 0, "create pseudo-file");
if (fd >= 0)
{
check(fstat(fd, &st) == 0 && S_ISREG(st.st_mode), "regular file");
#ifdef CONFIG_PSEUDOFS_ATTRIBUTES
check((st.st_mode & 0777) == mode, "creation mode");
#endif
if (flags == O_RDWR)
{
check(write(fd, "data", 4) == 4, "write new file");
check(lseek(fd, 0, SEEK_SET) == 0, "rewind new file");
check(read(fd, data, sizeof(data)) == 4 &&
memcmp(data, "data", 4) == 0, "read new file");
}
check(close(fd) == 0, "close new file");
}
check(unlink(path) == 0, "remove new file");
}
static void existing_file(void)
{
int fd;
fd = open("/pcreate-existing", O_CREAT | O_RDWR, 0600);
check(fd >= 0, "create existing-file control");
if (fd < 0)
{
return;
}
check(close(fd) == 0, "close existing-file control");
check(chmod("/pcreate-existing", 0400) == 0, "set read-only mode");
fd = open("/pcreate-existing", O_RDONLY);
check(fd >= 0, "read existing read-only file");
if (fd >= 0)
{
check(close(fd) == 0, "close existing read-only file");
}
errno = 0;
fd = open("/pcreate-existing", O_WRONLY);
#ifdef CONFIG_FS_PERMISSION
check(fd == -1 && errno == EACCES, "reject existing-file write");
#else
check(fd >= 0, "permission-disabled existing-file write");
#endif
if (fd >= 0)
{
check(close(fd) == 0, "close existing-file write");
}
check(chmod("/pcreate-existing", 0200) == 0, "set write-only mode");
errno = 0;
fd = open("/pcreate-existing", O_RDONLY);
#ifdef CONFIG_FS_PERMISSION
check(fd == -1 && errno == EACCES, "reject existing-file read");
#else
check(fd >= 0, "permission-disabled existing-file read");
#endif
if (fd >= 0)
{
check(close(fd) == 0, "close existing-file read");
}
fd = open("/pcreate-existing", O_WRONLY);
check(fd >= 0, "write existing write-only file");
if (fd >= 0)
{
check(close(fd) == 0, "close existing write-only file");
}
check(unlink("/pcreate-existing") == 0, "remove existing-file control");
}
static void parent_mode(mode_t mode)
{
struct stat st;
int fd;
check(mkdir("/pcreate-parent", mode) == 0, "create restricted parent");
errno = 0;
fd = open("/pcreate-parent/child", O_CREAT | O_RDWR, 0600);
#ifdef CONFIG_FS_PERMISSION
check(fd == -1 && errno == EACCES, "reject restricted parent");
#else
check(fd >= 0, "permission-disabled parent control");
#endif
if (fd >= 0)
{
check(close(fd) == 0, "close parent control");
}
check(chmod("/pcreate-parent", 0700) == 0, "restore parent mode");
#ifdef CONFIG_FS_PERMISSION
check(stat("/pcreate-parent/child", &st) == -1 && errno == ENOENT,
"denied creation left no child");
#else
check(stat("/pcreate-parent/child", &st) == 0, "control child exists");
check(unlink("/pcreate-parent/child") == 0, "remove control child");
#endif
check(rmdir("/pcreate-parent") == 0, "remove restricted parent");
}
static void mounted_file(void)
{
struct stat st;
int fd;
check(mount(NULL, "/pcreate-volume", "tmpfs", 0, NULL) == 0,
"mount tmpfs control");
fd = open("/pcreate-volume/file", O_CREAT | O_RDWR, 0600);
check(fd >= 0, "create mounted file");
if (fd >= 0)
{
check(fstat(fd, &st) == 0 && S_ISREG(st.st_mode),
"mounted regular file");
check(write(fd, "m", 1) == 1, "write mounted file");
check(close(fd) == 0, "close mounted file");
}
fd = open("/pcreate-volume/file", O_RDONLY);
check(fd >= 0, "reopen mounted file");
if (fd >= 0)
{
check(close(fd) == 0, "close reopened mounted file");
}
check(unlink("/pcreate-volume/file") == 0, "remove mounted file");
check(umount("/pcreate-volume") == 0, "unmount tmpfs control");
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, FAR char *argv[])
{
char saved_path[256] = "/";
mode_t saved_mask;
int fd;
g_checks = 0;
g_failures = 0;
saved_mask = umask(0);
check(getcwd(saved_path, sizeof(saved_path)) != NULL, "save cwd");
printf("PCREATE bits=%zu\n", sizeof(void *) * 8);
errno = 0;
fd = open("/pcreate-missing", O_RDONLY);
check(fd == -1 && errno == ENOENT, "missing without O_CREAT");
parent_mode(0500);
parent_mode(0600);
mounted_file();
printf("PCREATE controls checks=%d failures=%d\n", g_checks, g_failures);
create_file("/pcreate-absolute", O_RDWR, 0600);
create_file("/pcreate-readable", O_RDONLY, 0400);
create_file("/pcreate-writable", O_WRONLY, 0200);
check(mkdir("/pcreate-directory", 0700) == 0, "create relative parent");
check(chdir("/pcreate-directory") == 0, "enter relative parent");
create_file("relative", O_RDWR, 0600);
create_file("./dot", O_RDWR, 0600);
check(chdir(saved_path) == 0, "restore cwd");
create_file("/pcreate-directory/../pcreate-normalized", O_RDWR, 0600);
check(rmdir("/pcreate-directory") == 0, "remove relative parent");
existing_file();
umask(saved_mask);
printf("PCREATE RESULT checks=%d failures=%d\n", g_checks, g_failures);
return g_failures != 0;
}
```
`Kconfig`
```kconfig
config EXAMPLES_PSEUDOCREATECHECK
tristate "Pseudo-file creation permission regression"
default n
depends on PSEUDOFS_FILE && FS_TMPFS
```
`Make.defs`
```makefile
ifneq ($(CONFIG_EXAMPLES_PSEUDOCREATECHECK),)
CONFIGURED_APPS += $(APPDIR)/examples/pseudocreatecheck
endif
```
`Makefile`
```makefile
include $(APPDIR)/Make.defs
PROGNAME = pseudocreatecheck
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 8192
MODULE = $(CONFIG_EXAMPLES_PSEUDOCREATECHECK)
MAINSRC = pseudocreatecheck_main.c
include $(APPDIR)/Application.mk
```
`CMakeLists.txt`
```cmake
if(CONFIG_EXAMPLES_PSEUDOCREATECHECK)
nuttx_add_application(NAME pseudocreatecheck SRCS pseudocreatecheck_main.c
STACKSIZE 8192)
endif()
```
From the NuttX directory, on either revision:
```sh
make distclean
./tools/configure.sh -l -a ../apps sim:nsh
for option in CONFIG_EXAMPLES_PSEUDOCREATECHECK CONFIG_PSEUDOFS_FILE \
CONFIG_PSEUDOFS_ATTRIBUTES CONFIG_SCHED_USER_IDENTITY \
CONFIG_FS_PERMISSION CONFIG_FS_TMPFS CONFIG_FS_NOTIFY \
CONFIG_DEBUG_ASSERTIONS; do
kconfig-tweak --enable "$option"
done
kconfig-tweak --disable CONFIG_PSEUDOFS_FILE_READONLY
# For the 32-bit run, also enable CONFIG_SIM_M32 before olddefconfig.
# For the permission-disabled control, disable FS_PERMISSION and FS_NOTIFY.
make olddefconfig
make -j2
printf 'pseudocreatecheck\npoweroff\n' | ./nuttx
```
</details>
## PR verification Self-Check
- [x] This PR introduces only one functional change.
- [x] I have updated all required description fields above.
- [x] Commit message, author/sign-off and code style checks pass.
- [ ] My PR is still work in progress (not ready for review).
- [x] My PR is ready for review and can be safely merged into a codebase.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]