Repository navigation
libs/libc: validate iovcnt bounds and iov_len overflow in preadv/pwritev - #21548
Merged
xiaoxiang781216 merged 1 commit intoNov 8, 2026
Conversation
Add POSIX-mandated validation to preadv() and pwritev(): 1. Reject iovcnt <= 0 or iovcnt > IOV_MAX with ERROR (errno = EINVAL). 2. Reject iov == NULL with ERROR (errno = EFAULT). 3. Reject offset < 0 with ERROR (errno = EINVAL). 4. Validate that total iov_len does not overflow SSIZE_MAX with ERROR (errno = EINVAL). 5. Validate non-NULL iov_base for non-zero iov_len entries with ERROR (errno = EFAULT). 6. In preadv(), fix partial read error handling to preserve the count of bytes already transferred (return ntotal > 0 ? ntotal : ERROR), aligning with pwritev(). 7. Include <errno.h> and <limits.h> in lib_preadv.c and lib_pwritev.c. Reference: POSIX.1-2024 (IEEE Std 1003.1-2024) preadv/pwritev [EINVAL]. Assisted-by: gemini-3.8-flash Signed-off-by: Swatantra Yadav <[email protected]>
|
acassis
approved these changes
Nov 8, 2026
xiaoxiang781216
approved these changes
Nov 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add POSIX-mandated
[EINVAL]validation topreadv()andpwritev()foriovcntbounds,offsetsign, andiov_lenoverflow, as required by POSIX.1-2024 (IEEE Std 1003.1-2024). In addition, fix a bug inpreadv()where partial read progress was lost on a subsequent read error.Problem
preadv()andpwritev()inlibs/libc/uio/lib_preadv.candlibs/libc/uio/lib_pwritev.comitted standard POSIX boundary and overflow validation:iovcnt <= 0 || iovcnt > IOV_MAX: Wheniovcnt <= 0, both functions returned0without modifyingerrnoinstead of returning-1witherrno = EINVAL.iov == NULL: Dereferenced without validation.offset < 0: Not validated upfront before invokingpread()/pwrite().sum(iov_len) > SSIZE_MAX: Neither function checked for accumulation overflow intossize_t ntotal.preadv(): Inlib_preadv.c, if a subsequentpread()failed after reading earlier buffers (ntotal > 0), it returnednread(-1) directly. The caller had no way of knowing how many bytes were already read from the file. In contrast,lib_pwritev.ccorrectly preserved partial writes viantotal ? ntotal : ERROR.lib_preadv.cdid not include<errno.h>.Changes
libs/libc/uio/lib_preadv.c:<errno.h>and<limits.h>.iovcnt <= 0 || iovcnt > IOV_MAXreturningERRORwitherrno = EINVAL.iov == NULLreturningERRORwitherrno = EFAULT.offset < 0returningERRORwitherrno = EINVAL.SSIZE_MAX - len < iov[i].iov_lenreturningERRORwitherrno = EINVAL.iov_basefor non-zeroiov_lenentries returningERRORwitherrno = EFAULT.ntotal > 0 ? ntotal : ERRORon read failure to preserve partial read progress, matchingpwritev().libs/libc/uio/lib_pwritev.c:<limits.h>.ntotal > 0 ? ntotal : ERRORpartial write error handling.Behavior Comparison
iovcnt == 00-1,errno = EINVAL-1,[EINVAL]iovcnt < 00-1,errno = EINVAL-1,[EINVAL]iovcnt > IOV_MAX-1,errno = EINVAL-1,[EINVAL]iov == NULL-1,errno = EFAULT-1,[EFAULT]offset < 0-1,errno = EINVAL-1,[EINVAL]iov_len>SSIZE_MAXntotal-1,errno = EINVAL-1,[EINVAL]preadv()-1(losesntotal)ntotal)Impact
preadv()andpwritev()into full compliance with POSIX.1-2024.Testing
I confirm that changes are verified on local setup and works as intended:
Style Verification (nxstyle in Docker)
Build Log
Runtime Verification Log (Docker ubuntu:24.04)