Skip to content

Commit 5498af0

Browse files
committed
fix(compat): create mkdtemp directories with a private security descriptor
cbm_mkdtemp on Windows created the directory with plain _mkdir, so it took the token's default owner and the parent's inheritable DACL. Under an Administrators-default-owner policy -- standard on Windows Server and GitHub's elevated runners -- the temp directory was born owned by BUILTIN\Administrators with foreign inherited grants. The activation transaction validates its staging directory with a strict owner-is-current- user check plus a trusted-only DACL scan (activation_directory_secure), so install staging failed at the directory gate before any file was staged: "private staging failed for the launcher" in the Phase 8 smoke, previously masked as "did not remain runnable after private staging". Create the directory with CreateDirectoryW and an explicit descriptor: owner stamped to the token user and a protected, inheritable, user-only DACL -- private by construction, the same policy the daemon applies to its runtime directory. If the descriptor cannot be built the code falls back to plain _mkdir so degraded environments (Wine) keep working; downstream validation still gates security there. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent dc72a5b commit 5498af0

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

‎src/foundation/compat.c‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,64 @@ char *cbm_strcasestr(const char *haystack, const char *needle) {
5555

5656
#ifdef _WIN32
5757
#include <direct.h>
58+
#include <aclapi.h>
59+
#include "foundation/win_utf8.h"
60+
61+
/* Create `path` with an explicit private security descriptor: owner stamped
62+
* to the token user and a protected, inheritable, user-only DACL. A plain
63+
* _mkdir takes the token's DEFAULT owner and the parent's inheritable DACL;
64+
* under an Administrators-default-owner policy (standard on Windows Server
65+
* and GitHub's elevated runners) the directory is then born owned by
66+
* BUILTIN\Administrators with foreign inherited grants, and every private-
67+
* namespace validation (activation-transaction staging, launcher directory
68+
* policy) rejects the temp directory this function just made. Returns false
69+
* if the descriptor cannot be built or creation fails; the caller falls
70+
* back to plain _mkdir so degraded environments (Wine) keep working —
71+
* downstream validation still gates security there. */
72+
static bool win_mkdtemp_private_create(const char *path) {
73+
bool created = false;
74+
HANDLE token = NULL;
75+
TOKEN_USER *user = NULL;
76+
PACL acl = NULL;
77+
DWORD needed = 0;
78+
wchar_t *wide = cbm_utf8_to_wide(path);
79+
if (wide && OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token) &&
80+
!GetTokenInformation(token, TokenUser, NULL, 0, &needed) &&
81+
GetLastError() == ERROR_INSUFFICIENT_BUFFER && (user = malloc(needed)) != NULL &&
82+
GetTokenInformation(token, TokenUser, user, needed, &needed) && user->User.Sid &&
83+
IsValidSid(user->User.Sid)) {
84+
EXPLICIT_ACCESSW access;
85+
memset(&access, 0, sizeof(access));
86+
access.grfAccessPermissions = GENERIC_ALL;
87+
access.grfAccessMode = SET_ACCESS;
88+
access.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
89+
access.Trustee.TrusteeForm = TRUSTEE_IS_SID;
90+
access.Trustee.TrusteeType = TRUSTEE_IS_USER;
91+
access.Trustee.ptstrName = (LPWSTR)user->User.Sid;
92+
SECURITY_DESCRIPTOR descriptor;
93+
if (SetEntriesInAclW(1, &access, NULL, &acl) == ERROR_SUCCESS &&
94+
InitializeSecurityDescriptor(&descriptor, SECURITY_DESCRIPTOR_REVISION) &&
95+
SetSecurityDescriptorDacl(&descriptor, TRUE, acl, FALSE) &&
96+
SetSecurityDescriptorOwner(&descriptor, user->User.Sid, FALSE) &&
97+
SetSecurityDescriptorControl(&descriptor, SE_DACL_PROTECTED, SE_DACL_PROTECTED)) {
98+
SECURITY_ATTRIBUTES attributes;
99+
attributes.nLength = sizeof(attributes);
100+
attributes.lpSecurityDescriptor = &descriptor;
101+
attributes.bInheritHandle = FALSE;
102+
created = CreateDirectoryW(wide, &attributes) != 0;
103+
}
104+
}
105+
if (acl) {
106+
(void)LocalFree(acl);
107+
}
108+
free(user);
109+
if (token) {
110+
(void)CloseHandle(token);
111+
}
112+
free(wide);
113+
return created;
114+
}
115+
58116
char *cbm_mkdtemp(char *tmpl) {
59117
/* Build path in static buffer, then copy back to caller.
60118
* Callers must provide buffers >= CBM_SZ_256 bytes (all test code does). */
@@ -71,7 +129,7 @@ char *cbm_mkdtemp(char *tmpl) {
71129
}
72130
if (!_mktemp(buf))
73131
return NULL;
74-
if (_mkdir(buf) != 0)
132+
if (!win_mkdtemp_private_create(buf) && _mkdir(buf) != 0)
75133
return NULL;
76134
/* Normalize to forward slashes. Callers embed this path in JSON repo_path
77135
* (where "\t"/"\a" are invalid escapes → index fails) and pass it to git -C.

0 commit comments

Comments
 (0)