Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(compat): create mkdtemp directories with a private security descriptor

Martin Vogel5498af09db75
diff --git a/src/foundation/compat.c b/src/foundation/compat.c
index e1bcff2f5..32f40ef24 100644
--- a/src/foundation/compat.c
+++ b/src/foundation/compat.c
@@ -55,6 +55,64 @@ char *cbm_strcasestr(const char *haystack, const char *needle) {
#ifdef _WIN32
#include <direct.h>
+#include <aclapi.h>
+#include "foundation/win_utf8.h"
+
+/* Create `path` with an explicit private security descriptor: owner stamped
+ * to the token user and a protected, inheritable, user-only DACL. A plain
+ * _mkdir takes 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 directory is then born owned by
+ * BUILTIN\Administrators with foreign inherited grants, and every private-
+ * namespace validation (activation-transaction staging, launcher directory
+ * policy) rejects the temp directory this function just made. Returns false
+ * if the descriptor cannot be built or creation fails; the caller falls
+ * back to plain _mkdir so degraded environments (Wine) keep working —
+ * downstream validation still gates security there. */
+static bool win_mkdtemp_private_create(const char *path) {
+ bool created = false;
+ HANDLE token = NULL;
+ TOKEN_USER *user = NULL;
+ PACL acl = NULL;
+ DWORD needed = 0;
+ wchar_t *wide = cbm_utf8_to_wide(path);
+ if (wide && OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token) &&
+ !GetTokenInformation(token, TokenUser, NULL, 0, &needed) &&
+ GetLastError() == ERROR_INSUFFICIENT_BUFFER && (user = malloc(needed)) != NULL &&
+ GetTokenInformation(token, TokenUser, user, needed, &needed) && user->User.Sid &&
+ IsValidSid(user->User.Sid)) {
+ EXPLICIT_ACCESSW access;
+ memset(&access, 0, sizeof(access));
+ access.grfAccessPermissions = GENERIC_ALL;
+ access.grfAccessMode = SET_ACCESS;
+ access.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
+ access.Trustee.TrusteeForm = TRUSTEE_IS_SID;
+ access.Trustee.TrusteeType = TRUSTEE_IS_USER;
+ access.Trustee.ptstrName = (LPWSTR)user->User.Sid;
+ SECURITY_DESCRIPTOR descriptor;
+ if (SetEntriesInAclW(1, &access, NULL, &acl) == ERROR_SUCCESS &&
+ InitializeSecurityDescriptor(&descriptor, SECURITY_DESCRIPTOR_REVISION) &&
+ SetSecurityDescriptorDacl(&descriptor, TRUE, acl, FALSE) &&
+ SetSecurityDescriptorOwner(&descriptor, user->User.Sid, FALSE) &&
+ SetSecurityDescriptorControl(&descriptor, SE_DACL_PROTECTED, SE_DACL_PROTECTED)) {
+ SECURITY_ATTRIBUTES attributes;
+ attributes.nLength = sizeof(attributes);
+ attributes.lpSecurityDescriptor = &descriptor;
+ attributes.bInheritHandle = FALSE;
+ created = CreateDirectoryW(wide, &attributes) != 0;
+ }
+ }
+ if (acl) {
+ (void)LocalFree(acl);
+ }
+ free(user);
+ if (token) {
+ (void)CloseHandle(token);
+ }
+ free(wide);
+ return created;
+}
+
char *cbm_mkdtemp(char *tmpl) {
/* Build path in static buffer, then copy back to caller.
* Callers must provide buffers >= CBM_SZ_256 bytes (all test code does). */
@@ -71,7 +129,7 @@ char *cbm_mkdtemp(char *tmpl) {
}
if (!_mktemp(buf))
return NULL;
- if (_mkdir(buf) != 0)
+ if (!win_mkdtemp_private_create(buf) && _mkdir(buf) != 0)
return NULL;
/* Normalize to forward slashes. Callers embed this path in JSON repo_path
* (where "\t"/"\a" are invalid escapes → index fails) and pass it to git -C.