_psCreateFolder relies on glibc realpath() side effects for nonexistent dirs (UB) #24

Closed
opened 2026-11-29 18:23:05 +00:00 by opencode · 1 comment
Owner

Found during the issue-#11 path audit (2026-11-29).

Problem: _psCreateFolder (src/skel/glfw/glfw.cpp, master ~107–131; same on miami) calls realpath() on a directory that may not exist yet. realpath returns NULL for nonexistent paths and writes to the output buffer only as a glibc side effect — the code works by luck on glibc and would break user-files directory creation on other libcs (musl etc.). Confirmed behavior on the reference glibc: it writes a usable string even for a missing dir, but that is not contractual.

Task: make the directory creation explicit and libc-independent — e.g. mkdir (recursively for the parent chain) before resolving, or construct the path without realpath for the not-yet-existing case — matching the tree's existing POSIX patterns. Both branches.

Acceptance

  • User-files dir creation works without relying on glibc realpath side effects (demonstrate with a harness against the patched code, as in #11).
  • No behavior change on Windows.
Found during the issue-#11 path audit (2026-11-29). **Problem:** `_psCreateFolder` (`src/skel/glfw/glfw.cpp`, master ~107–131; same on miami) calls `realpath()` on a directory that may not exist yet. `realpath` returns NULL for nonexistent paths and writes to the output buffer only as a glibc side effect — the code works **by luck on glibc** and would break user-files directory creation on other libcs (musl etc.). Confirmed behavior on the reference glibc: it writes a usable string even for a missing dir, but that is not contractual. **Task:** make the directory creation explicit and libc-independent — e.g. `mkdir` (recursively for the parent chain) before resolving, or construct the path without `realpath` for the not-yet-existing case — matching the tree's existing POSIX patterns. Both branches. ## Acceptance - User-files dir creation works without relying on glibc `realpath` side effects (demonstrate with a harness against the patched code, as in #11). - No behavior change on Windows.
opencode added this to the M2: Linux platform hardening milestone 2026-11-29 18:23:06 +00:00
opencode added the savegamelinuxbug labels 2026-11-29 18:23:07 +00:00
Author
Owner

RESOLVED 2026-11-29. _psCreateFolder's POSIX branch no longer calls realpath() at all: lstat(path) + mkdir(path, 0755) on the path as given, errno == ENOENT guarded. The reviewer independently confirmed: the old !S_ISDIR(info.st_mode) clause read indeterminate info after a failed lstat (never meaningful — its removal is strictly better); symlink semantics re-derived as no-regression (existing dir / symlink-to-dir: identical behavior; dangling symlink moved from undefined to defined-but-silent); the only live POSIX caller is _psGetUserFilesFolder() → "userfiles" (single-level, cwd-relative — MemoryCard's callers are PS2_MENU-dead).

Published on both branches (fix: _psCreateFolder creates the dir without relying on realpath side effects, glfw.cpp only). Harnesses 12/12 against the real patched body: creates as dir mode 0755, idempotent, cwd-relative, no invented recursion (pre-existing no-nested-creation limitation documented as out of scope — its only live caller is single-level). Windows branch untouched. Reviewer verdict: ship.

**RESOLVED 2026-11-29.** `_psCreateFolder`'s POSIX branch no longer calls `realpath()` at all: `lstat(path)` + `mkdir(path, 0755)` on the path as given, `errno == ENOENT` guarded. The reviewer independently confirmed: the old `!S_ISDIR(info.st_mode)` clause read indeterminate `info` after a failed `lstat` (never meaningful — its removal is strictly better); symlink semantics re-derived as no-regression (existing dir / symlink-to-dir: identical behavior; dangling symlink moved from undefined to defined-but-silent); the only live POSIX caller is `_psGetUserFilesFolder()` → `"userfiles"` (single-level, cwd-relative — MemoryCard's callers are PS2_MENU-dead). Published on both branches (`fix: _psCreateFolder creates the dir without relying on realpath side effects`, glfw.cpp only). Harnesses 12/12 against the real patched body: creates as dir mode 0755, idempotent, cwd-relative, no invented recursion (pre-existing no-nested-creation limitation documented as out of scope — its only live caller is single-level). Windows branch untouched. Reviewer verdict: **ship**.
Sign in to join this conversation.