Swatantra-66 opened a new pull request, #22519:
URL: https://github.com/apache/nuttx/pull/22519
## Summary
This change fixes a POSIX compliance violation in the NuttX Virtual File
System (VFS) layer where attempting to truncate a file opened in read-only mode
(O_RDONLY) incorrectly resulted in -EINVAL rather than the standard-mandated
-EBADF.
### Problem Analysis
1. POSIX Specification Violation:
According to POSIX.1-2017 (IEEE Std 1003.1-2017) specification for
ftruncate():
"The ftruncate() function shall fail if:
[EBADF] The fildes argument is not a file descriptor open for writing."
Under Linux and standard POSIX platforms (man 2 ftruncate):
"EBADF: The file descriptor fd is not a valid file descriptor or is not open
for writing."
In fs/vfs/fs_truncate.c, file_truncate() previously evaluated:
```c
if ((filep->f_oflags & O_ACCMODE) == O_RDONLY)
{
fwarn("WARNING: Cannot truncate a file opened read-only\n");
return -EINVAL;
}
```
Returning -EINVAL caused ftruncate() to set errno to EINVAL instead of
EBADF, breaking POSIX test suites and portable applications expecting standard
POSIX error semantics.
2. Inconsistency Across VFS Layer:
Other VFS operations in NuttX that reject read-only file descriptors already
return -EBADF:
- fs/vfs/fs_write.c (lines 144, 275): returns -EBADF if (filep->f_oflags &
O_ACCMODE) == O_RDONLY.
- fs/vfs/fs_sendfile.c (line 275): returns -EBADF if (outfile->f_oflags &
O_ACCMODE) == O_RDONLY.
Aligning fs/vfs/fs_truncate.c ensures consistent errno reporting across all
file-modifying operations.
3. Missing Validation in file_truncate():
Previously, the check for negative length (length < 0) was located solely
inside ftruncate() (lines 166-169). However, file_truncate() is exported via
include/nuttx/fs/fs.h and called directly by internal kernel subsystems,
including:
- fs/rpmsgfs/rpmsgfs_server.c: file_truncate(filep, msg->length)
- net/route/net_del_fileroute.c: file_truncate(&fshandle, filesize)
Because file_truncate() did not validate the length parameter, a negative
length passed by kernel callers would propagate directly to the underlying
filesystem truncate vtable (inode->u.i_ops->truncate(filep, length)), leading
to potential integer underflow, allocation faults, or filesystem corruption.
4. Pointer Safety:
file_truncate() dereferenced filep->f_oflags without verifying that filep
was non-NULL, risking an immediate NULL pointer dereference.
### Solution
- Return -EBADF instead of -EINVAL in file_truncate() when the file
descriptor was opened read-only (O_RDONLY).
- Add defensive validation if (length < 0) returning -EINVAL directly in
file_truncate(), ensuring all callers (kernel and user space) are protected.
- Add DEBUGASSERT(filep != NULL) in file_truncate() consistent with other
file_* functions in the VFS subsystem (fs_close, fs_write, fs_fstat).
- Update the ftruncate() function header documentation in
fs/vfs/fs_truncate.c to match POSIX.1-2017 definitions and fix a minor logging
typo.
### Behavior Comparison
| Condition | Previous Behavior | New Behavior | Standard Reference |
| :--- | :--- | :--- | :--- |
| File opened O_RDONLY | returns -EINVAL (errno = EINVAL) | returns -EBADF
(errno = EBADF) | POSIX.1-2017 ftruncate() [EBADF] |
| Direct call with length < 0 | Passed to filesystem vtable | returns
-EINVAL (errno = EINVAL) | POSIX.1-2017 ftruncate() [EINVAL] |
| Direct call with filep == NULL | NULL pointer dereference | Assertion
triggered in debug | Defensive kernel API contract |
## Impact
- Is new feature added? No
- Impact on user? Yes, user applications calling ftruncate() on read-only
descriptors will now receive the standard POSIX errno EBADF instead of EINVAL.
- Impact on build? No
- Impact on hardware? None. Architecture-independent VFS logic.
- Impact on documentation? Updated API docstring in fs/vfs/fs_truncate.c to
accurately reflect POSIX returned values.
- Impact on security? Prevents kernel callers from passing unchecked
negative length values to filesystem drivers.
- Impact on compatibility? Significantly improves POSIX IEEE Std 1003.1
conformance.
## Testing
Build verification:
Configuration: same70-xplained:nsh (ARM Cortex-M7, arm-none-eabi-gcc 14.2.1)
```
$ ./tools/configure.sh -l same70-xplained:nsh
$ make -j4
...
CC: fs/vfs/fs_truncate.c
...
LD: nuttx
Memory region Used Size Region Size %age Used
flash: 145684 B 2 MB 6.95%
sram: 10784 B 384 KB 2.74%
CP: nuttx.bin
```
Verification Details:
1. Compiled cleanly with zero warnings or errors.
2. Verified with tools/nxstyle adhering strictly to the NuttX C Coding
Standard.
--
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]