shared-module/os: fix size_t underflow in abspath(".." at root) - #11113
Merged
Conversation
common_hal_os_path_abspath() removes ".." components by decrementing
slash_count and then indexing slashes[slash_count - 1]. When the input
path resolves to ".." applied directly at the filesystem root ("/"),
only the root boundary itself has been recorded (slash_count == 1).
The unconditional slash_count-- makes it 0, and slashes[(size_t)0 - 1]
reads slashes[SIZE_MAX] -- an out-of-bounds read of a single-element
stack VLA whose garbage value is then used as output_len to write
full_path[output_len] = '\0', an out-of-bounds/wild write.
This is reachable directly from Python whenever cwd is "/" (the default
at boot, before any chdir), e.g.:
os.chdir("..")
os.listdir("..")
os.mkdir("../x")
and likewise for os.remove/os.rmdir/os.rename/os.utime, since they all
normalize their path argument through this same helper first.
Fix: only decrement slash_count when there is more than the root
boundary recorded (slash_count > 1). This matches POSIX "cd .. from /
stays at /" semantics and leaves every non-root case byte-for-byte
identical to the previous behavior.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
dhalbert
approved these changes
Jul 13, 2026
dhalbert
left a comment
Collaborator
There was a problem hiding this comment.
This is a good fix, thanks. It would be nice to add a test for this case to the test suite. If you have an idea for that, go ahead. There is no direct way to call abspath, unfortunately.
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
common_hal_os_path_abspath()(shared-module/os/__init__.c) normalizes..components by decrementing
slash_countand then indexingslashes[slash_count - 1]. When the path resolves to..applied directly atthe filesystem root (
/), only the root boundary itself has been recorded, soslash_count == 1at that point. The unconditionalslash_count--makes it0, andslashes[(size_t)0 - 1]readsslashes[SIZE_MAX]— an out-of-boundsread of a 1-element stack VLA. The garbage value read back is then used as
output_lento dofull_path[output_len] = '\0', an out-of-bounds/wild write.This helper is shared by every path-taking
osfunction, so it's reachabledirectly from user code whenever
cwdis"/"(the default at boot, beforeany
chdir), e.g.:Fix
Only decrement
slash_countwhen more than the root boundary is recorded(
slash_count > 1). This matches POSIX "cd ..from/stays at/"semantics, and leaves every non-root case byte-for-byte identical to the
current behavior (verified below).
Verification
Building/running the full CircuitPython firmware/unix-port test suite wasn't
practical in this environment (submodules aren't checked out and this is a
3-line change deep in path normalization, not something that needs a target
board to exercise). Instead I extracted the exact normalization algorithm
verbatim (only the allocator call and the
cwdplumbing were adapted to runas a standalone host program — every line of the loop under test is unchanged)
into a small C program and ran it under gcc + AddressSanitizer:
abspath(cwd="/", path="..")reliably triggers"/",abspath("/", "../foo")returns
"/foo", andabspath(NULL, "..")(thecwd == NULL/fresh-bootdefault-init path) also returns
"/"— zero ASan reports.abspath("/a/b", ".."),abspath("/a/b/c", "../.."),abspath("/", "/foo/../bar"),abspath("/a", "./foo"), etc.) produces byte-identical output betweenthe unpatched and patched algorithm, i.e. this change has no effect outside
the root-boundary case.
I also ran the repository's own formatter (
tools/codeformat.py -c/uncrustify) over the changed file; it made no further changes beyond what'sin this diff.
Disclosure
Generative AI (Claude, Anthropic) was used to help investigate this issue
(reading the current source, deriving the failure trace, writing and running
the AddressSanitizer reproduction) and to implement this fix. All changes
were reviewed by me before submission.