fix(libkernel): recognize Windows anonymous shared memory descriptors #11403

Merged
MarcZX10 merged 2 commits from upstream-pr/11403 into main 2026-10-28 22:49:23 +00:00
MarcZX10 commented 2026-10-26 11:03:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T11:03:04Z.


What

On Windows, anonymous shm_open(SHM_ANON, ...) creates a temporary file but did not register the descriptor as shared memory. fchflags therefore treated it like an ordinary file instead of returning EINVAL, and descriptor duplication/close tracking did not preserve its shared-memory classification.

Create anonymous shared-memory files under the private Windows shared-memory root, track them as shared-memory descriptors, and add regression coverage for fchflags, duplication, close, and descriptor reuse.

Tested

  • x86_64-w64-mingw32-g++ -std=c++20 -D_WIN32 -D_fflush_nolock=fflush -fsyntax-only ... SharedMemory.cpp — passed.
  • The same MinGW syntax check for GuestNamedShmWindows.cpp — passed.
  • git diff --check upstream/main...HEAD — passed.
  • tools/check_conventions.py --base upstream/main --head HEAD — passed.
  • guest_named_shm_windows was not run locally. The automatic PR Windows product job is skipped by path policy, and the documented manual workflow dispatch requires repository admin rights; there is no hosted Windows runtime result for this head.

Checklist

  • Based on current main; no duplicate implementation found by the open-PR overlap scan
  • One topic per PR
  • No comments in code except technical debt
  • Generic behavior, not title-specific
  • Depends on: #11296 (Windows anonymous shared-memory descriptor recognition is tracked as the remaining case there)
  • Fixes: #11372
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5016331645 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11403). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T11:03:04Z. --- ### What On Windows, anonymous `shm_open(SHM_ANON, ...)` creates a temporary file but did not register the descriptor as shared memory. `fchflags` therefore treated it like an ordinary file instead of returning `EINVAL`, and descriptor duplication/close tracking did not preserve its shared-memory classification. Create anonymous shared-memory files under the private Windows shared-memory root, track them as shared-memory descriptors, and add regression coverage for `fchflags`, duplication, close, and descriptor reuse. ### Tested - `x86_64-w64-mingw32-g++ -std=c++20 -D_WIN32 -D_fflush_nolock=fflush -fsyntax-only ... SharedMemory.cpp` — passed. - The same MinGW syntax check for `GuestNamedShmWindows.cpp` — passed. - `git diff --check upstream/main...HEAD` — passed. - `tools/check_conventions.py --base upstream/main --head HEAD` — passed. - `guest_named_shm_windows` was not run locally. The automatic PR Windows product job is skipped by path policy, and the documented manual workflow dispatch requires repository admin rights; there is no hosted Windows runtime result for this head. ### Checklist - [x] Based on current `main`; no duplicate implementation found by the open-PR overlap scan - [x] One topic per PR - [x] No comments in code except technical debt - [x] Generic behavior, not title-specific - [x] Depends on: #11296 (Windows anonymous shared-memory descriptor recognition is tracked as the remaining case there) - [x] Fixes: #11372 - [x] GPUs tested: n/a - [x] AI-assisted: yes
rahul05ranjan commented 2026-10-26 12:53:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rahul05ranjan on 2026-10-26T12:53:53Z.

Review: COMMENTED


Traced the Windows anonymous path end to end; the fix is consistent on every side I checked.

  • Classification: IsHostSharedMemoryDescriptor accepts exactly <resolved temp>\AnyPS5\shm\<40 hex>.dat — no separator in the relative part, first 40 chars hex, .dat suffix (SharedMemory.cpp:110ff). CreateAnonymousFile now names files SHA1(random || pid || serial) in hex — 40 chars plus .dat, precisely the shape the validator accepts, so the tracked descriptor and the path check agree instead of the old anyps5-shm-... names that never validated.
  • The test's reinterpret_cast<const char*>(1) is the anonymous sentinel (anonymousObject, SharedMemory.cpp:161), so shm_open(1, ReadWrite, 0600) really exercises the anonymous route. And since shm_open_nid_postfix rejects O_RDONLY for the anonymous object before calling in, TrackSharedMemoryDescriptor(..., true)'s writable flag can't be wrong here.
  • Hardening vs. the old code: files no longer sit in the raw temp_directory_path() with a random_device-seeded, counter-suffixed name; they sit under AnyPS5\shm with an explicit private DACL (SYSTEM/administrators/owner full, no other ACEs), _O_CREAT | _O_EXCL against squatting, and the reparse-point check on the directories. That closes the predictable-name and inherited-ACL exposure the old placement had — with TMP redirected to a shared directory, other local accounts could previously read or pre-create the emulator's guest shared memory.

One gap worth a look: EnsurePrivateWindowsDirectory applies the private DACL only at creation and tolerates ERROR_ALREADY_EXISTS. A <temp>\AnyPS5 that already exists (older build, or anything under the same user) keeps whatever ACL it was created with — the code validates it's a directory and not a reparse point, but never its ACL. If the intent is "objects under this root are readable only by the owner", the existing-directory path would want a GetNamedSecurityInfo check or a repair step.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5652263270 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11403#pullrequestreview-5652263270). Originally posted by [rahul05ranjan](https://github.com/rahul05ranjan) on 2026-10-26T12:53:53Z. **Review:** COMMENTED --- Traced the Windows anonymous path end to end; the fix is consistent on every side I checked. - Classification: `IsHostSharedMemoryDescriptor` accepts exactly `<resolved temp>\AnyPS5\shm\<40 hex>.dat` — no separator in the relative part, first 40 chars hex, `.dat` suffix (SharedMemory.cpp:110ff). `CreateAnonymousFile` now names files SHA1(random || pid || serial) in hex — 40 chars plus `.dat`, precisely the shape the validator accepts, so the tracked descriptor and the path check agree instead of the old `anyps5-shm-...` names that never validated. - The test's `reinterpret_cast<const char*>(1)` is the anonymous sentinel (`anonymousObject`, SharedMemory.cpp:161), so `shm_open(1, ReadWrite, 0600)` really exercises the anonymous route. And since `shm_open_nid_postfix` rejects `O_RDONLY` for the anonymous object before calling in, `TrackSharedMemoryDescriptor(..., true)`'s writable flag can't be wrong here. - Hardening vs. the old code: files no longer sit in the raw `temp_directory_path()` with a `random_device`-seeded, counter-suffixed name; they sit under `AnyPS5\shm` with an explicit private DACL (SYSTEM/administrators/owner full, no other ACEs), `_O_CREAT | _O_EXCL` against squatting, and the reparse-point check on the directories. That closes the predictable-name and inherited-ACL exposure the old placement had — with TMP redirected to a shared directory, other local accounts could previously read or pre-create the emulator's guest shared memory. One gap worth a look: `EnsurePrivateWindowsDirectory` applies the private DACL only at creation and tolerates `ERROR_ALREADY_EXISTS`. A `<temp>\AnyPS5` that already exists (older build, or anything under the same user) keeps whatever ACL it was created with — the code validates it's a directory and not a reparse point, but never its ACL. If the intent is "objects under this root are readable only by the owner", the existing-directory path would want a `GetNamedSecurityInfo` check or a repair step.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!11403
No description provided.