Repository navigation
fs/inode: validate parameters and minfd bounds in fdlist_dupfile - #21979
Merged
xiaoxiang781216 merged 1 commit intoNov 19, 2026
Merged
xiaoxiang781216 merged 1 commit into
xiaoxiang781216 merged 1 commit into
Conversation
Swatantra-66
requested review from
Donny9,
jerpelea,
pkarashchenko,
pussuw and
xiaoxiang781216
as code owners
November 18, 2026 05:19
|
xiaoxiang781216
approved these changes
Nov 18, 2026
POSIX IEEE Std 1003.1 (fcntl(2) F_DUPFD) and file descriptor allocation rules specify parameter validation and bounds handling for fdlist_dupfile: 1. Validate minfd >= 0 and minfd < limit: Previously, fdlist_dupfile() only checked minfd >= limit without verifying minfd >= 0. Passing a negative minfd (e.g., -1) bypassed the limit check, resulting in negative array indexing (minfd % 8 = -1) and out-of-bounds heap access (&list->fl_fds[0][-1]) or heap corruption (CWE-125/CWE-129/CWE-787). Checking minfd < 0 upfront safely returns -EINVAL. 2. Defensively validate list != NULL and filep != NULL: Returns -EINVAL if list or filep is NULL, eliminating potential CWE-476 NULL pointer dereferences in fdlist_limit(list) and atomic_add(&filep->f_refs, 1). 3. Avoid false descriptor decoding in fdcheck_restore(): Standard streams (<= 2) are never protected by fdcheck_protect(), and assigned tags are never 0. In lib_fdcheck.c, fdcheck_restore() previously checked only (fd == 0 || val < 0), causing bare values >= 256 (such as minfd = 256 with tag 0) to be decoded as descriptor 1 (matching open stdout with tag 0) or panicking if descriptor 1 was closed. Checking (fd <= 2 || tag_expect == 0 || val < 0) ensures bare descriptors are returned directly. Assisted-by: Claude:claude-opus-4.6 Signed-off-by: Swatantra Yadav <[email protected]>
Swatantra-66
force-pushed
the
fix/vfs-dupfile-bounds-validation
branch
from
November 19, 2026 07:31
099273b to
df9a8fa
Compare
jerpelea
approved these changes
Nov 19, 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
POSIX IEEE Std 1003.1 (
fcntl(2)F_DUPFD), Linux specifications, and file descriptor allocation rules specify parameter validation, bounds constraints, and descriptor decoding forfdlist_dupfile()andfdcheck_restore():Validate
minfd >= 0andminfd < limitinfdlist_dupfile():Previously,
fdlist_dupfile()only checkedminfd >= limitwithout verifyingminfd >= 0. Passing a negativeminfd(e.g.,-1or-9) bypassed the limit check because negative values are strictly< limit. This caused:j = minfd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK = -1.&list->fl_fds[0][-1]or&list->fl_fds[-1][-1](CWE-125 / CWE-129).fdp->f_file == NULL, it wrote into out-of-bounds heap memory before the table (atomic_add(&filep->f_refs, 1)andfdp->f_file = filep) (CWE-787).Checking
minfd < 0upfront before and after restoration safely returns-EINVAL.Defensive NULL List and File Pointer Validation:
Defensively validates
list == NULL || filep == NULLreturning-EINVAL. This eliminates:fdlist_limit(list)whenlist == NULL.atomic_add(&filep->f_refs, 1)whenfilep == NULL(previously only guarded by release-strippedDEBUGASSERT(filep)).Avoid False Descriptor Decoding in
fdcheck_restore():Under
CONFIG_FDCHECK,fdcheck_protect()never protects standard descriptors (fd <= 2), and assigned tags are never 0 (g_fdcheck_tagskips 0). Previously,fdcheck_restore()only checked(fd == 0 || val < 0). Consequently:minfd = 256,0x0100, whereioctl(1, FIOC_GETTAG_FDCHECK, &tag_store)succeeded withtag_store = 0, matchingtag_expect = 0and returning1. This silently bypassedminfd >= limitchecks on tasks withlimit = 16.ioctl(1)failed with-EBADFand invokedPANIC().Updating
fdcheck_restore()to check(fd <= 2 || tag_expect == 0 || val < 0)ensures bare integers and untagged descriptors are returned directly without false mutation or panics.Before vs. After Behavior
fdlist_dupfile(list, flags, -1, filep)j = -1); out-of-bounds heap read/write atfl_fds[0][-1]-EINVALfdlist_dupfile(NULL, flags, 0, filep)NULL->fl_limit(Kernel Panic / Crash)-EINVALfdlist_dupfile(list, flags, 0, NULL)NULL->f_refs(Crash)-EINVALCONFIG_FDCHECKwithminfd = 256,limit = 16, fd 1 open-EINVAL(CONFIG_FDCHECKwithminfd = 256, fd 1 closedioctl(1)failed with-EBADFand triggered kernelPANIC()-EINVALImpact
minfdvalues with-EINVAL.PANIC()calls underCONFIG_FDCHECK.Testing
1. Build Verification
Cleanly built with
arm-none-eabi-gcc 10.3.1onstm32f103-minimum:nsh:2. Code Style & Commit Verification
Verified with
nxstyleinside Docker:Verified with
checkpatch.sh:3. What Was Not Tested