jingfei195887 opened a new pull request, #22296:
URL: https://github.com/apache/nuttx/pull/22296
## Summary
Add `CONFIG_PSEUDOFS_FILE_READONLY` to omit pseudo-file write/truncate
support when it is not needed. It depends on `PSEUDOFS_FILE` and defaults to
`DEFAULT_SMALL`.
Read-only mode rejects write-access opens and `PROT_WRITE` mappings,
including `MAP_PRIVATE`: pseudo-files use the driver mmap path, which directly
exposes their contents rather than making a private copy.
## Impact
- Feature/user/compatibility: **YES**. Configurations using
`DEFAULT_SMALL=y` that need writable pseudo-files must explicitly set
`CONFIG_PSEUDOFS_FILE_READONLY=n`. Disabling the option preserves existing
behavior; other filesystem types are unchanged.
- Build process/hardware: **NO**. The option conditionally omits the
write/truncate implementations and callbacks.
- Documentation: **YES**. `pseudofs.rst` documents the option and access
restrictions.
- Security: this restricts filesystem API operations; it does not add
hardware memory protection.
## Testing
Tested commit `fb20dee5f451f71d89f911f5dd49ba24ff03f0de`, based on
`1325a419756929a6fc6f7b25c4c835b1e8ff2cdd`, with nuttx-apps `9cd18a2d63`.
- `sim:nsh`, Linux x86_64, GCC 13.1.0: Make and CMake builds with read-only
support enabled; `ostest` exits with status 0.
- A separate `sim:nsh` CMake build with `PSEUDOFS_FILE_READONLY=n` also
passes.
- `sim:nsh` with `DEFAULT_SMALL=y`: the new option defaults to `y` when left
unspecified; all 8 read-only assertions pass. Explicitly setting it to `n`
passes all 5 writable assertions.
- `qemu-armv7r:pnsh` (protected, Arm GCC 13.2.1) and `qemu-armv8a:knsh`
(kernel, GCC 13.4.0): Make builds with the option enabled.
- Protected/kernel runtime, real hardware, other architectures, and
successful nonempty read-only mappings were **not** tested. The mmap assertions
below check writable-map rejection.
The same local test helper was used before and after the change. The base
log deliberately uses the new read-only expectations, so its four FAIL lines
demonstrate behavior that the new option disables. The helper remains local to
keep this PR limited to the OS option; its tested functions are included below
rather than adding a standalone example to nuttx-apps.
<details>
<summary>Build and ostest log excerpts</summary>
Read-only `sim:nsh` gate:
```text
head : fb20dee5f451f71d89f911f5dd49ba24ff03f0de
base : 1325a41975 (merge-base with upstream/master)
apps : 9cd18a2d63
options : CONFIG_PSEUDOFS_FILE CONFIG_PSEUDOFS_FILE_READONLY
host : Linux x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
make build: rc=0
warnings in changed files: 0
ostest: Exiting with status 0
cmake reconfigure: rc=0
cmake build: rc=0
RESULT: PASS
```
protected-readonly:
```text
CC: vfs/fs_pseudofile.c
LD: nuttx_user
LD: nuttx
Memory region Used Size Region Size %age Used
kflash: 160680 B 512 KB 30.65%
ksram: 12 KB 8 MB 0.15%
```
kernel-readonly:
```text
CC: vfs/fs_pseudofile.c
LD: nuttx
Memory region Used Size Region Size %age Used
dram: 304 KB 2 MB 14.84%
pgram: 0 B 4 MB 0.00%
```
</details>
<details>
<summary>Before/after functional logs</summary>
base:
```text
nsh> fsuptest pseudofs
pseudofs read-only open: PASS
pseudofs write-mode open: FAIL
pseudofs read-write open: FAIL
pseudofs write: PASS
pseudofs truncate: PASS
pseudofs path truncate: FAIL
pseudofs shared writable mmap: PASS
pseudofs private writable mmap: FAIL
FSUPTEST_DONE
nsh> poweroff
```
readonly-default:
```text
nsh> fsuptest pseudofs
pseudofs read-only open: PASS
pseudofs write-mode open: PASS
pseudofs read-write open: PASS
pseudofs write: PASS
pseudofs truncate: PASS
pseudofs path truncate: PASS
pseudofs shared writable mmap: PASS
pseudofs private writable mmap: PASS
FSUPTEST_DONE
nsh> poweroff
```
writable-override:
```text
nsh> fsuptest pseudofs
pseudofs write-mode open: PASS
pseudofs write: PASS
pseudofs truncate: PASS
pseudofs path truncate: PASS
pseudofs shared writable mmap: PASS
FSUPTEST_DONE
nsh> poweroff
```
</details>
<details>
<summary>Size comparison: sim:nsh with DEFAULT_SMALL=y</summary>
The configurations and local test helper are identical except for the added
`CONFIG_PSEUDOFS_FILE_READONLY=y`. GNU `size` output:
Base:
```text
text data bss dec hex filename
1895315 301376 229536 2426227 250573 nuttx
```
Read-only:
```text
text data bss dec hex filename
1892987 300880 229280 2423147 24f96b nuttx
```
Delta: text **-2328 bytes**, data **-496 bytes**, bss **-256 bytes**; total
**-3080 bytes** in this configuration.
</details>
<details>
<summary>Local regression helper</summary>
Build these functions into a local NuttX command and call `pseudofs_test()`
once per boot. Define `CONFIG_EXAMPLES_FSUPTEST_READONLY_EXPECTED` for the
base/read-only comparison; leave it undefined for the writable test. Required
headers are `<nuttx/config.h>`, `<sys/mman.h>`, `<stdbool.h>`, `<stdio.h>`,
`<fcntl.h>`, `<errno.h>` and `<unistd.h>`.
With `DEFAULT_SMALL=y`, remove any existing explicit setting of
`PSEUDOFS_FILE_READONLY` before `make olddefconfig` to test the default. For
the writable case, explicitly disable it before `olddefconfig`.
```c
static void pseudofs_result(FAR const char *name, bool passed)
{
printf("pseudofs %s: %s\n", name, passed ? "PASS" : "FAIL");
}
static void pseudofs_test(void)
{
static const char path[] = "/fsuptest_pseudo";
FAR void *mapping;
#ifndef CONFIG_EXAMPLES_FSUPTEST_READONLY_EXPECTED
char value;
#endif
int fd;
int ret;
#ifdef CONFIG_EXAMPLES_FSUPTEST_READONLY_EXPECTED
fd = open(path, O_CREAT | O_RDONLY, 0666);
pseudofs_result("read-only open", fd >= 0);
if (fd < 0)
{
return;
}
errno = 0;
ret = open(path, O_WRONLY);
pseudofs_result("write-mode open", ret < 0 && errno == EACCES);
if (ret >= 0)
{
close(ret);
}
errno = 0;
ret = open(path, O_RDWR);
pseudofs_result("read-write open", ret < 0 && errno == EACCES);
if (ret >= 0)
{
close(ret);
}
errno = 0;
ret = write(fd, "X", 1);
pseudofs_result("write", ret < 0 && errno == EBADF);
errno = 0;
ret = ftruncate(fd, 1);
pseudofs_result("truncate", ret < 0 && errno == EINVAL);
errno = 0;
ret = truncate(path, 1);
pseudofs_result("path truncate", ret < 0 && errno == EACCES);
errno = 0;
mapping = mmap(NULL, 1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
pseudofs_result("shared writable mmap",
mapping == MAP_FAILED && errno == EACCES);
if (mapping != MAP_FAILED)
{
munmap(mapping, 1);
}
errno = 0;
mapping = mmap(NULL, 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
pseudofs_result("private writable mmap",
mapping == MAP_FAILED && errno == EACCES);
if (mapping != MAP_FAILED)
{
munmap(mapping, 1);
}
#else
fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0666);
pseudofs_result("write-mode open", fd >= 0);
if (fd < 0)
{
return;
}
ret = write(fd, "A", 1);
pseudofs_result("write", ret == 1);
ret = ftruncate(fd, 2);
pseudofs_result("truncate", ret == 0);
ret = truncate(path, 1);
pseudofs_result("path truncate", ret == 0);
mapping = mmap(NULL, 1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
ret = mapping == MAP_FAILED ? -1 : 0;
if (ret == 0)
{
((FAR char *)mapping)[0] = 'B';
ret = msync(mapping, 1, MS_SYNC);
munmap(mapping, 1);
}
lseek(fd, 0, SEEK_SET);
ret = ret == 0 ? read(fd, &value, 1) : ret;
pseudofs_result("shared writable mmap", ret == 1 && value == 'B');
#endif
close(fd);
}
```
</details>
## PR verification Self-Check
- [x] This PR introduces only one functional change.
- [x] Configuration behavior and migration are documented.
- [x] Local build/runtime results and untested cases are described above.
- [ ] My PR is still work in progress.
- [x] My PR is ready for review.
--
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]