Skip to content

fs/vfs: validate iovcnt bounds and iov_len overflow in readv/writev - #21544

Merged
michallenc merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-readv-writev-posix-validation
Nov 9, 2026
Merged

michallenc merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-readv-writev-posix-validation

Conversation

@Swatantra-66

@Swatantra-66 Swatantra-66 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add POSIX-mandated [EINVAL] validation to file_readv() and file_writev() for iovcnt bounds and iov_len overflow, as required by POSIX.1-2017 (IEEE Std 1003.1-2017).

Closes #21543

Problem

readv() and writev() in fs/vfs/fs_read.c and fs/vfs/fs_write.c did not validate:

  • iovcnt <= 0 or iovcnt > IOV_MAX (should return -EINVAL)
  • Sum of iov_len overflowing SSIZE_MAX (should return -EINVAL)
  • NULL iov_base with non-zero iov_len (should return -EFAULT)

The iovcnt == 0 case silently returned 0, and the overflow check was only done mid-loop in the compat path.

Changes

File Change
fs/vfs/fs_read.c Add upfront iovcnt bounds check, SSIZE_MAX overflow loop, and NULL iov_base validation in file_readv(). Remove redundant mid-loop checks from file_readv_compat().
fs/vfs/fs_write.c Same pattern for file_writev() and file_writev_compat().
include/sys/uio.h Remove (Not implemented) annotations from EINVAL docs. Fix readv() → writev() copy-paste typo. Remove stale TODO comment.

Behavior Comparison

Condition Before After POSIX Requirement
iovcnt == 0 Returns 0 Returns -EINVAL -EINVAL
iovcnt < 0 Undefined Returns -EINVAL -EINVAL
iovcnt > IOV_MAX No check Returns -EINVAL -EINVAL
Sum of iov_len > SSIZE_MAX Checked only in compat loop (read) Upfront check in both paths -EINVAL
NULL iov_base with iov_len > 0 Checked only in write compat loop Upfront check in both read and write -EFAULT

Impact

  • file_read() and file_write() (single-buffer) call file_readv()/file_writev() with iovcnt = 1, which passes all new validation.
  • The read-path NULL check preserves the existing kernel build exception (CONFIG_ARCH_TEXT_VBASE == 0) for ELF loading at address 0.
  • No behavioral change for valid inputs.

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): Linux (Ubuntu 24.04 on WSL2, x86_64), gcc 15.2.0
  • Target(s): sim:nsh (x86_64)

Style Verification

$ ./tools/nxstyle -v 1 fs/vfs/fs_read.c
/mnt/c/Users/HP/Desktop/gsoc/repos/nuttx/fs/vfs/fs_read.c: PASSED nxstyle check

$ ./tools/nxstyle -v 1 fs/vfs/fs_write.c
/mnt/c/Users/HP/Desktop/gsoc/repos/nuttx/fs/vfs/fs_write.c: PASSED nxstyle check

$ ./tools/nxstyle -v 1 include/sys/uio.h
/mnt/c/Users/HP/Desktop/gsoc/repos/nuttx/include/sys/uio.h: PASSED nxstyle check

Build Log

$ ./tools/configure.sh -l sim:nsh
  Copy files
  Select CONFIG_HOST_LINUX=y
  Refreshing...
Loaded configuration '.config'
Configuration saved to '.config'

$ make -j$(nproc)
...
CC:  vfs/fs_read.c
CC:  vfs/fs_write.c
...
LD:  nuttx
Pac SIM with dynamic libs..
SIM elf with dynamic libs archive in nuttx.tgz

Runtime Verification Log

$ ./nuttx
nsh> help
help usage:  help [-v] [<cmd>]

    .           dirname     free        mount       set         umount      
    [           date        help        mv          kill        unset       
    ?           df          hexdump     pidof       pkill       uptime      
    alias       dmesg       losetup     poweroff    sleep       watch       
    unalias     echo        ln          quit        usleep      xd          
    basename    env         ls          printf      source      wait        
    break       exec        du          ps          test        
    cat         exit        mkdir       pwd         time        
    cd          expr        mkfatfs     readlink    true        
    cp          false       mkfifo      rm          truncate    
    cmp         fdinfo      mkrd        rmdir       uname       

Builtin Apps:
    dd           gcov         hello        nsh          sh           
    dumpstack    gpio         init         ostest       
nsh> exit

@github-actions github-actions Bot added Area: File System File System issues Size: S The size of the change in this PR is small labels Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text -60 B (-0.1%, 66,503 B / 262,144 B, total: 25% used)

esp32-devkitc

  • ROM: .flash.text +36 B (+0.0%, 129,448 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +36 B (+0.0%, 95,296 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +40 B (+0.0%, 86,120 B / 4,194,304 B, total: 2% used)

qemu-armv8a

  • Code: .text.file_readv -4 B, .text.file_writev -36 B (-0.0%, 360,900 B)

qemu-intel64

  • Code: .text +22 B (+0.0%, 8,694,106 B)

s698pm-dkit

  • Code: .text +16 B (+0.0%, 389,952 B)

stm32-nucleo-f103rb

  • flash: .text +4 B (+0.0%, 34,896 B / 131,072 B, total: 27% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Comment thread fs/vfs/fs_read.c Resolved
Comment thread fs/vfs/fs_write.c Resolved
Add POSIX-mandated validation to file_readv() and file_writev():

1. Reject iovcnt <= 0 or iovcnt > IOV_MAX with -EINVAL.
2. Validate total iov_len does not overflow SSIZE_MAX with -EINVAL.
3. Validate non-NULL iov_base for non-zero iov_len entries with
   -EFAULT (read path preserves kernel build exception for
   CONFIG_ARCH_TEXT_VBASE == 0).
4. Move per-entry overflow and NULL checks from compat loops to
   upfront validation in the main functions.

Update include/sys/uio.h doc comments to remove "(Not implemented)"
annotations and fix readv/writev copy-paste typo.

Reference: POSIX.1-2017 (IEEE Std 1003.1-2017) readv/writev [EINVAL].

Assisted-by: Google Gemini:gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/vfs-readv-writev-posix-validation branch from 7b7ba58 to fa50eff Compare November 8, 2026 05:59
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216 Could you please re-run the failed 'arm-other' job?
It failed on a transient HTTP 504 timeout downloading LLVM libcxx

@michallenc
michallenc merged commit f7bdfab into apache:master Nov 9, 2026
89 of 90 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: File System File System issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] fs/vfs: missing POSIX iovcnt bounds and iov_len overflow validation in readv/writev

3 participants