guest_kqueue: DeniedParentWatch aborts in an elevated process on Windows since #5493; drop the backup privileges in the test, diff included #6597

Closed
opened 2026-10-15 22:11:35 +00:00 by frankea · 4 comments
frankea commented 2026-10-15 22:11:35 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-10-15T22:11:35Z.


Since #5493, guest_kqueue's DeniedParentWatch aborts when the test process is elevated (Windows 11 26200, MinGW-w64 GCC 15.2.0 r7, Release; main a077a88df through 31cbb55d7, 4 of 4 and then 1 of 1 runs):

the vnode watch error lacks "parent":
FATAL: abort() (SIGABRT) on thread 20364

Cause. The test denies FILE_LIST_DIRECTORY on the parent directory to the current user and expects the vnode watch's directory open to fail with the parent named in the error. An elevated token holds SeBackupPrivilege and SeRestorePrivilege; a directory opened with backup semantics (which a directory handle needs) is granted regardless of its DACL when the caller holds them, so the watch succeeds, ThrownMessage returns an empty string and the parent check aborts. Non-elevated runs and CI are unaffected, which is why it passes there.

Fix. Remove both privileges from the process token at the start of DeniedParentWatch with AdjustTokenPrivileges(SE_PRIVILEGE_REMOVED). On a token that does not hold them the call succeeds with ERROR_NOT_ALL_ASSIGNED and changes nothing, so the non-elevated path is the same as today. Removal is permanent for the process; Timeout is the only case that runs after it and does not touch files.

--- a/core/libs/tests/GuestKqueue.cpp
+++ b/core/libs/tests/GuestKqueue.cpp
@@ -540,7 +540,24 @@ static void RequireContextInsensitive(std::string message, std::string context)
     }
 }
 
+static void DropPrivilege(HANDLE token, const wchar_t* name) {
+    TOKEN_PRIVILEGES privileges{};
+    privileges.PrivilegeCount = 1;
+    privileges.Privileges[0].Attributes = SE_PRIVILEGE_REMOVED;
+    Require(::LookupPrivilegeValueW(nullptr, name, &privileges.Privileges[0].Luid) != FALSE);
+    Require(::AdjustTokenPrivileges(token, FALSE, &privileges, 0, nullptr, nullptr) != FALSE);
+}
+
+static void DropBackupPrivileges() {
+    HANDLE token = nullptr;
+    Require(::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token) != FALSE);
+    DropPrivilege(token, L"SeBackupPrivilege");
+    DropPrivilege(token, L"SeRestorePrivilege");
+    Require(::CloseHandle(token) != FALSE);
+}
+
 static void DeniedParentWatch() {
+    DropBackupPrivileges();
     const auto path = TemporaryName("denied");
     Require(mkdir_nid_postfix(path.c_str(), 0755) == 0);
     const int file = open_nid_postfix((path + "/watched.bin").c_str(), ReadWrite | Create, 0644);

Tested. main 31cbb55d7 on the same elevated box: without the change guest_kqueue aborts as above (1 of 1); with it, passes 3 of 3 (--repeat until-fail:3). python tools/check_conventions.py --base origin/main passes. Not run: a non-elevated Windows session (the ERROR_NOT_ALL_ASSIGNED path), Linux (the function is inside the existing #ifdef _WIN32).

Branch: https://github.com/frankea/AnyPS5/tree/fix/kqueue-denied-parent-watch-privileges (one commit, 8f1610a22, on main 31cbb55d7). I have no merged PRs, so this is an issue with the change rather than a PR; happy for anyone to carry it. Related: #5493.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5874728542 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6597). Originally posted by [frankea](https://github.com/frankea) on 2026-10-15T22:11:35Z. --- Since #5493, `guest_kqueue`'s `DeniedParentWatch` aborts when the test process is elevated (Windows 11 26200, MinGW-w64 GCC 15.2.0 r7, Release; `main` a077a88df through 31cbb55d7, 4 of 4 and then 1 of 1 runs): ``` the vnode watch error lacks "parent": FATAL: abort() (SIGABRT) on thread 20364 ``` **Cause.** The test denies `FILE_LIST_DIRECTORY` on the parent directory to the current user and expects the vnode watch's directory open to fail with the parent named in the error. An elevated token holds `SeBackupPrivilege` and `SeRestorePrivilege`; a directory opened with backup semantics (which a directory handle needs) is granted regardless of its DACL when the caller holds them, so the watch succeeds, `ThrownMessage` returns an empty string and the `parent` check aborts. Non-elevated runs and CI are unaffected, which is why it passes there. **Fix.** Remove both privileges from the process token at the start of `DeniedParentWatch` with `AdjustTokenPrivileges(SE_PRIVILEGE_REMOVED)`. On a token that does not hold them the call succeeds with `ERROR_NOT_ALL_ASSIGNED` and changes nothing, so the non-elevated path is the same as today. Removal is permanent for the process; `Timeout` is the only case that runs after it and does not touch files. ```diff --- a/core/libs/tests/GuestKqueue.cpp +++ b/core/libs/tests/GuestKqueue.cpp @@ -540,7 +540,24 @@ static void RequireContextInsensitive(std::string message, std::string context) } } +static void DropPrivilege(HANDLE token, const wchar_t* name) { + TOKEN_PRIVILEGES privileges{}; + privileges.PrivilegeCount = 1; + privileges.Privileges[0].Attributes = SE_PRIVILEGE_REMOVED; + Require(::LookupPrivilegeValueW(nullptr, name, &privileges.Privileges[0].Luid) != FALSE); + Require(::AdjustTokenPrivileges(token, FALSE, &privileges, 0, nullptr, nullptr) != FALSE); +} + +static void DropBackupPrivileges() { + HANDLE token = nullptr; + Require(::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token) != FALSE); + DropPrivilege(token, L"SeBackupPrivilege"); + DropPrivilege(token, L"SeRestorePrivilege"); + Require(::CloseHandle(token) != FALSE); +} + static void DeniedParentWatch() { + DropBackupPrivileges(); const auto path = TemporaryName("denied"); Require(mkdir_nid_postfix(path.c_str(), 0755) == 0); const int file = open_nid_postfix((path + "/watched.bin").c_str(), ReadWrite | Create, 0644); ``` **Tested.** `main` 31cbb55d7 on the same elevated box: without the change `guest_kqueue` aborts as above (1 of 1); with it, passes 3 of 3 (`--repeat until-fail:3`). `python tools/check_conventions.py --base origin/main` passes. Not run: a non-elevated Windows session (the `ERROR_NOT_ALL_ASSIGNED` path), Linux (the function is inside the existing `#ifdef _WIN32`). Branch: https://github.com/frankea/AnyPS5/tree/fix/kqueue-denied-parent-watch-privileges (one commit, 8f1610a22, on `main` 31cbb55d7). I have no merged PRs, so this is an issue with the change rather than a PR; happy for anyone to carry it. Related: #5493. AI-assisted: yes (Claude Code).
MarcZX10 commented 2026-10-15 22:26:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T22:26:09Z.


I opened PR #6599 with the focused privilege removal before DeniedParentWatch. The Windows-only translation unit compiles with MinGW GCC 12 under -Wall -Wextra -Werror; conventions, eligibility and intake pass. I could not run the elevated Windows test locally; the PR records the 3/3 Windows result from this issue.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6203548506 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6597#issuecomment-6203548506). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-15T22:26:09Z. --- I opened [PR #6599](https://github.com/boykopovar/AnyPS5/pull/6599) with the focused privilege removal before `DeniedParentWatch`. The Windows-only translation unit compiles with MinGW GCC 12 under `-Wall -Wextra -Werror`; conventions, eligibility and intake pass. I could not run the elevated Windows test locally; the PR records the 3/3 Windows result from this issue.
frankea commented 2026-10-15 22:27:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-10-15T22:27:09Z.


The change is now on a branch as well: https://github.com/frankea/AnyPS5/tree/fix/kqueue-denied-parent-watch-privileges (one commit, 8f1610a22, on main 31cbb55d7; the same diff as above).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6203562406 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6597#issuecomment-6203562406). Originally posted by [frankea](https://github.com/frankea) on 2026-10-15T22:27:09Z. --- The change is now on a branch as well: https://github.com/frankea/AnyPS5/tree/fix/kqueue-denied-parent-watch-privileges (one commit, 8f1610a22, on `main` 31cbb55d7; the same diff as above).
MarcZX10 commented 2026-10-15 23:13:24 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T23:13:24Z.


Thanks for publishing the branch. PR #6599 already carries the same 17-line change and keeps Adam Franke as the human co-author on MarcZX100-authored commit 083f1cc9; the manual overlap review is also posted there. I do not see a separate implementation to submit.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6204224458 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6597#issuecomment-6204224458). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-15T23:13:24Z. --- Thanks for publishing the branch. PR #6599 already carries the same 17-line change and keeps Adam Franke as the human co-author on MarcZX100-authored commit `083f1cc9`; the manual overlap review is also posted there. I do not see a separate implementation to submit.
oneandonlydean commented 2026-10-17 03:21:00 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T03:21:00Z.


@johnmaia, since guest_kqueue aborts with 0xc0000409 on main on your machine: two open PRs look like they fix two separate causes of this abort, and the evidence suggests both are needed. #6599 handles an elevated token where backup privileges bypass the deny entry; #6173 handles a user name that equals the computer name. Could you run ctest -R "^guest_kqueue$" --repeat until-fail:5 --output-on-failure on main with both merged, and note whether the shell was elevated and whether your user name matches the computer name? One run on a machine that fails on main is the proof we need before merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6228975916 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6597#issuecomment-6228975916). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-17T03:21:00Z. --- @johnmaia, since `guest_kqueue` aborts with 0xc0000409 on main on your machine: two open PRs look like they fix two separate causes of this abort, and the evidence suggests both are needed. #6599 handles an elevated token where backup privileges bypass the deny entry; #6173 handles a user name that equals the computer name. Could you run `ctest -R "^guest_kqueue$" --repeat until-fail:5 --output-on-failure` on main with both merged, and note whether the shell was elevated and whether your user name matches the computer name? One run on a machine that fails on main is the proof we need before merging.
Sign in to join this conversation.
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#6597
No description provided.