Same owner-only, protected, inheritable ACL as compat.c's private helper, * but deliberately no inherited-security fallback. TOKEN_USER storage is * arena-owned; Windows owns the ACL returned by SetEntriesInAclW. */
| 222 | * but deliberately no inherited-security fallback. TOKEN_USER storage is |
| 223 | * arena-owned; Windows owns the ACL returned by SetEntriesInAclW. */ |
| 224 | static bool gf_private_directory(cbm_git_facts_t *facts, char path[GF_PATH_CAP], |
| 225 | cbm_git_facts_error_t *error) { |
| 226 | CBMArena scratch; |
| 227 | cbm_arena_init_lazy(&scratch, 1024); |
| 228 | HANDLE token = NULL; |
| 229 | TOKEN_USER *user = NULL; |
| 230 | PACL acl = NULL; |
| 231 | DWORD needed = 0; |
| 232 | bool created = false; |
| 233 | const char *tmp = cbm_tmpdir(); |
| 234 | if (!gf_absolute(tmp)) { |
| 235 | gf_error(error, CBM_GIT_FACTS_IO, "An absolute private temp location is required"); |
| 236 | goto done; |
| 237 | } |
| 238 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token) || |
| 239 | GetTokenInformation(token, TokenUser, NULL, 0, &needed) || |
| 240 | GetLastError() != ERROR_INSUFFICIENT_BUFFER || needed == 0 || needed > 65536) { |
| 241 | gf_error(error, CBM_GIT_FACTS_IO, "Cannot establish private capture owner"); |
| 242 | goto done; |
| 243 | } |
| 244 | user = gf_allocate(facts, &scratch, needed, error); |
| 245 | if (!user) { |
| 246 | if (error->status == CBM_GIT_FACTS_OOM) |
| 247 | gf_error(error, CBM_GIT_FACTS_OOM, "Cannot retain private capture owner"); |
| 248 | goto done; |
| 249 | } |
| 250 | if (!GetTokenInformation(token, TokenUser, user, needed, &needed) || !user->User.Sid || |
| 251 | !IsValidSid(user->User.Sid)) { |
| 252 | gf_error(error, CBM_GIT_FACTS_IO, "Cannot establish private capture owner"); |
| 253 | goto done; |
| 254 | } |
| 255 | EXPLICIT_ACCESSW access; |
| 256 | memset(&access, 0, sizeof(access)); |
| 257 | access.grfAccessPermissions = GENERIC_ALL; |
| 258 | access.grfAccessMode = SET_ACCESS; |
| 259 | access.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT; |
| 260 | access.Trustee.TrusteeForm = TRUSTEE_IS_SID; |
| 261 | access.Trustee.TrusteeType = TRUSTEE_IS_USER; |
| 262 | access.Trustee.ptstrName = (LPWSTR)user->User.Sid; |
| 263 | SECURITY_DESCRIPTOR descriptor; |
| 264 | if (SetEntriesInAclW(1, &access, NULL, &acl) != ERROR_SUCCESS || |
| 265 | !InitializeSecurityDescriptor(&descriptor, SECURITY_DESCRIPTOR_REVISION) || |
| 266 | !SetSecurityDescriptorDacl(&descriptor, TRUE, acl, FALSE) || |
| 267 | !SetSecurityDescriptorOwner(&descriptor, user->User.Sid, FALSE) || |
| 268 | !SetSecurityDescriptorControl(&descriptor, SE_DACL_PROTECTED, SE_DACL_PROTECTED)) { |
| 269 | gf_error(error, CBM_GIT_FACTS_IO, "Cannot establish private capture permissions"); |
| 270 | goto done; |
| 271 | } |
| 272 | SECURITY_ATTRIBUTES attributes = {sizeof(attributes), &descriptor, FALSE}; |
| 273 | static const char hex[] = "0123456789abcdef"; |
| 274 | for (int attempt = 0; attempt < 16; attempt++) { |
| 275 | if (!gf_gate(facts, error)) |
| 276 | goto done; |
| 277 | unsigned char random[16]; |
| 278 | char suffix[33]; |
| 279 | if (!cbm_secure_random(random, sizeof(random))) |
| 280 | break; |
| 281 | for (size_t i = 0; i < sizeof(random); i++) { |
no test coverage detected