Repository navigation
fs/vfs: validate parameters and buffer alignment in eventfd - #21855
Merged
acassis merged 1 commit intoNov 14, 2026
Merged
Conversation
Swatantra-66
requested review from
Donny9 and
xiaoxiang781216
as code owners
November 13, 2026 16:51
|
xiaoxiang781216
approved these changes
Nov 13, 2026
Contributor
|
fixed by #21771. |
1. eventfd_do_read():
- Separate buffer nullity and size validation: return -EFAULT when
buffer == NULL per POSIX read(2), and -EINVAL when
len < sizeof(eventfd_t) per Linux eventfd(2) specification.
- Eliminate unaligned memory access: replace direct pointer casting
and 64-bit store on (FAR eventfd_t *)buffer with a local variable
and memcpy(), preventing alignment traps on strict architectures.
2. eventfd_do_write():
- Separate buffer nullity and size validation: return -EFAULT when
buffer == NULL per POSIX write(2), and -EINVAL when
len < sizeof(eventfd_t) per Linux eventfd(2) specification.
- Read input counter once into a local variable via memcpy() to
prevent unaligned 64-bit loads and eliminate repeated buffer
dereferencing.
- Bound counter overflow: enforce Linux eventfd maximum counter
ceiling (new_counter < dev->counter || new_counter == (eventfd_t)-1)
to prevent counter from reaching reserved (eventfd_t)-1.
Assisted-by: Google Gemini <gemini-3.8-flash>
Signed-off-by: Swatantra Yadav <[email protected]>
Swatantra-66
force-pushed
the
fix/vfs-eventfd-posix-validation
branch
from
November 14, 2026 07:26
37f0ee8 to
8bb8609
Compare
acassis
approved these changes
Nov 14, 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
This PR validates parameters and fixes error handling, buffer alignment, and counter overflow bounds in
fs/vfs/fs_eventfd.c:eventfd_do_read():buffer == NULLreturns-EFAULTper POSIXread(2)([EFAULT] buf is outside your accessible address space). A buffer size less thansizeof(eventfd_t)returns-EINVALper Linuxeventfd(2).*(FAR eventfd_t *)bufferand direct 64-bit store with a localeventfd_t value;written viamemcpy(buffer, &value, sizeof(eventfd_t));. Becauseeventfd_tis a 64-bit unsigned integer, writing directly to an unaligned user pointer (char *buffer) causes unaligned memory access hardware traps on strict architectures (e.g. ARM Cortex-M0/M4) and violates C strict aliasing.eventfd_do_write():-EFAULTwhenbuffer == NULLper POSIXwrite(2), and-EINVALwhenlen < sizeof(eventfd_t)per Linuxeventfd(2).*(FAR eventfd_t *)bufferwith a single local load viamemcpy(&value, buffer, sizeof(eventfd_t));, eliminating unaligned memory access traps and eliminating repeated buffer dereferencing throughout the function and its wait loop.eventfd(2)specification ceiling where the maximum counter value is0xfffffffffffffffe((eventfd_t)-2). Previously, NuttX only checkednew_counter < dev->counter, which allowed the counter to reach0xffffffffffffffff((eventfd_t)-1). Because(eventfd_t)-1is reserved and stops triggeringPOLLOUTineventfd_do_poll(), checkingnew_counter < dev->counter || new_counter == (eventfd_t)-1ensures the counter is bounded correctly.Comparison Table
read(efd, NULL, len)-1witherrno = EINVAL-1witherrno = EFAULTread(2)read(efd, buf, < 8)-1witherrno = EINVAL-1witherrno = EINVALeventfd(2)read(efd, unaligned_buf, 8)memcpywrite(efd, NULL, len)-1witherrno = EINVAL-1witherrno = EFAULTwrite(2)write(efd, unaligned_buf, 8)memcpywrite(efd, ...)overflowing to(eventfd_t)-1(eventfd_t)-1EAGAINeventfd(2)Impact
Testing
nuttx-builderLinux x86_64)sim:nshwithCONFIG_EVENT_FD=y1. Code Style Check (
nxstyle)$ ./tools/nxstyle fs/vfs/fs_eventfd.c # Exit code: 0 (0 errors, 0 warnings)2. Build Verification
3. Binary Size
4. Simulator Boot and Runtime Test