Repository navigation
🐛 fix(unix): check XDG_RUNTIME_DIR parents - #668
Merged
Merged
Conversation
user_runtime_dir checked only the last component of XDG_RUNTIME_DIR and, under ensure_exists, created every missing parent before that check. A symlink or directory another user put at a missing parent in /tmp decided where platformdirs created the runtime directory, and the path came back without a warning. Check the parents before creating anything and again before returning the path: an entry of a world-writable directory must belong to root or the user, and no parent may be world-writable without the sticky bit. Otherwise warn and fall back, as for any rejected XDG_RUNTIME_DIR. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-UJJM5OMU.
…ent-symlink # Conflicts: # .github/THREAT_MODEL.md
…ent-symlink # Conflicts: # src/platformdirs/unix.py
…ent-symlink # Conflicts: # docs/explanation.rst
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.
Unix.user_runtime_dircreatedXDG_RUNTIME_DIRand its missing parents, then checked only the last component. WhenXDG_RUNTIME_DIRsat two or more levels below/tmp, another local user who created the first missing level first, as a symlink or as their own directory, chose where the victim's runtime directory went, and platformdirs returned it without aRuntimeDirWarning. After this PR, platformdirs rejects such a path with a warning and falls back as for any other rejectedXDG_RUNTIME_DIR. This addresses draft advisory ANT-2026-UJJM5OMU.The check only saw the last component
_runtime_dir_problemcallslstat(2), which leaves only a symlink in the last position unresolved. It resolves a symlink at a parent, so the owner and mode checks describe whatever directory sits at its target. Underensure_exists,_make_private_directorieshad already created that directory through the link as the victim, with mode0700, so the check passed.fs.protected_symlinksdid not stop it: in the reproduction the kernel refused to follow the link as the last component but let root create directories through it as a parent._runtime_dir_parent_problemwalks the existing parents from/down. A parent that is an entry of a world-writable directory must belong to root or the user, and no parent may be world-writable without the sticky bit. It reads the owner withlstatbefore following a symlink, so it still rejects a link thatfs.protected_symlinksrefuses to follow.user_runtime_dirruns it before creating anything and again, after the leaf check, before returning the path.flowchart TD E["XDG_RUNTIME_DIR=/tmp/run/user/0, /tmp/run planted by another user"] --> B{version} B -- before --> C["mkdir through /tmp/run, lstat the leaf: passes"] C --> R["returns a path the other user can redirect"] B -- after --> P{"parent in a world-writable dir owned by someone else?"} P -- yes --> W["RuntimeDirWarning, fall back"] P -- no --> K["create, check leaf and parents, return"] classDef bad fill:#ffd6d6,stroke:#c1121f,color:#5c0000; classDef ok fill:#d8f3dc,stroke:#2d6a4f,color:#1b4332; class C,R bad; class W,K ok;Behavior changes
XDG_RUNTIME_DIRbelow another user's entry in a world-writable directory, or below a world-writable directory without the sticky bit, now raises aRuntimeDirWarningsuch asXDG_RUNTIME_DIR /tmp/run/user/0 is below /tmp/run, owned by uid 65534, falling back to /tmp/runtime-0.place_runtime_filefalls back the same way, since its lookup goes throughuser_runtime_dir./run/user/<uid>, a path inside the user's home and an entry the user owns in/tmpkeep working.THREAT_MODEL.mdnow lists the parent check under the$XDG_RUNTIME_DIRguarantee, and the race and group-writable parents from the next item as accepted risks.site_runtime_dirare unchanged.Reported by Anthropic's OSS Scanner: ANT-2026-UJJM5OMU.