Skip to content

Commit 1b067e3

Browse files
authored
Merge pull request DeusData#2139 from DeusData/fix/decision-b-win-sid-trust
fix(install): trust the local machine's built-in Administrator; name untrusted owners (DeusData#1705, DeusData#2023, DeusData#1686)
2 parents a0dc418 + 53700ee commit 1b067e3

4 files changed

Lines changed: 333 additions & 3 deletions

File tree

‎src/cli/activation_transaction.c‎

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define WIN32_LEAN_AND_MEAN
1919
#endif
2020
#include <aclapi.h>
21+
#include <ntsecapi.h>
2122
#include <sddl.h>
2223
#include <windows.h>
2324
#else
@@ -424,6 +425,102 @@ static void activation_windows_security_destroy(activation_windows_security_t *s
424425
memset(security, 0, sizeof(*security));
425426
}
426427

428+
/* #1705: THIS machine's built-in Administrator ACCOUNT (RID 500 under the local
429+
* machine account-domain SID, S-1-5-21-<machine>-500) is a trusted owner/grantee,
430+
* mirroring the daemon's win_sid_trusted (src/daemon/ipc.c). It is resolved via
431+
* LSA (the local machine account-domain SID) plus CreateWellKnownSid and compared
432+
* with EqualSid. It is deliberately NOT IsWellKnownSid(WinAccountAdministratorSid)
433+
* and NOT a trailing-RID-500 test: both accept a FOREIGN S-1-5-21-*-500 (a domain
434+
* admin, or another machine's built-in Administrator), opening a cross-machine
435+
* bypass. Resolved once and cached for the process; any LSA or synthesis failure
436+
* caches "none" and grants NO tolerance (fail closed). advapi32 is already loaded
437+
* (this file calls GetSecurityInfo etc.), so the functions are resolved from its
438+
* module handle with no new import. CLI activation is single-threaded by
439+
* contract, so the cache needs no lock. */
440+
typedef NTSTATUS(NTAPI *activation_lsa_open_policy_fn)(PLSA_UNICODE_STRING, PLSA_OBJECT_ATTRIBUTES,
441+
ACCESS_MASK, PLSA_HANDLE);
442+
typedef NTSTATUS(NTAPI *activation_lsa_query_information_policy_fn)(LSA_HANDLE,
443+
POLICY_INFORMATION_CLASS,
444+
PVOID *);
445+
typedef NTSTATUS(NTAPI *activation_lsa_free_memory_fn)(PVOID);
446+
typedef NTSTATUS(NTAPI *activation_lsa_close_fn)(LSA_HANDLE);
447+
typedef BOOL(WINAPI *activation_create_well_known_sid_fn)(WELL_KNOWN_SID_TYPE, PSID, PSID, DWORD *);
448+
449+
static PSID activation_windows_local_admin_sid(void) {
450+
static bool resolved = false;
451+
static PSID cached = NULL;
452+
if (resolved) {
453+
return cached;
454+
}
455+
resolved = true;
456+
HMODULE advapi = GetModuleHandleW(L"advapi32.dll");
457+
if (!advapi) {
458+
return NULL;
459+
}
460+
activation_lsa_open_policy_fn lsa_open =
461+
(activation_lsa_open_policy_fn)(void (*)(void))GetProcAddress(advapi, "LsaOpenPolicy");
462+
activation_lsa_query_information_policy_fn lsa_query =
463+
(activation_lsa_query_information_policy_fn)(void (*)(void))GetProcAddress(
464+
advapi, "LsaQueryInformationPolicy");
465+
activation_lsa_free_memory_fn lsa_free =
466+
(activation_lsa_free_memory_fn)(void (*)(void))GetProcAddress(advapi, "LsaFreeMemory");
467+
activation_lsa_close_fn lsa_close =
468+
(activation_lsa_close_fn)(void (*)(void))GetProcAddress(advapi, "LsaClose");
469+
activation_create_well_known_sid_fn create_sid =
470+
(activation_create_well_known_sid_fn)(void (*)(void))GetProcAddress(advapi,
471+
"CreateWellKnownSid");
472+
if (!lsa_open || !lsa_query || !lsa_free || !lsa_close || !create_sid) {
473+
return NULL;
474+
}
475+
LSA_OBJECT_ATTRIBUTES attributes;
476+
memset(&attributes, 0, sizeof(attributes));
477+
LSA_HANDLE policy = NULL;
478+
/* STATUS_SUCCESS is 0; any other status is treated as failure (fail closed). */
479+
if (lsa_open(NULL, &attributes, POLICY_VIEW_LOCAL_INFORMATION, &policy) != 0 || !policy) {
480+
return NULL;
481+
}
482+
POLICY_ACCOUNT_DOMAIN_INFO *domain = NULL;
483+
if (lsa_query(policy, PolicyAccountDomainInformation, (PVOID *)&domain) == 0 && domain &&
484+
domain->DomainSid && IsValidSid(domain->DomainSid)) {
485+
DWORD needed = 0;
486+
(void)create_sid(WinAccountAdministratorSid, domain->DomainSid, NULL, &needed);
487+
if (needed > 0U) {
488+
PSID admin = malloc(needed);
489+
if (admin &&
490+
create_sid(WinAccountAdministratorSid, domain->DomainSid, admin, &needed) &&
491+
IsValidSid(admin)) {
492+
cached = admin;
493+
} else {
494+
free(admin);
495+
}
496+
}
497+
}
498+
if (domain) {
499+
(void)lsa_free(domain);
500+
}
501+
(void)lsa_close(policy);
502+
return cached;
503+
}
504+
505+
/* #2023/#1686: an owner refusal must name WHICH owner, not just "status -3, os 0".
506+
* The path is already carried by g_activation_refusal_object; this appends the
507+
* offending owner's SID string, mirroring the daemon's ACL/owner diagnostics, so
508+
* the operator sees the exact identity to remove or the directory to move. */
509+
static void activation_windows_note_untrusted_owner(const char *predicate, PSID owner,
510+
DWORD os_error) {
511+
char label[192];
512+
LPSTR owner_text = NULL;
513+
(void)snprintf(
514+
label, sizeof(label), "%s; owner=%s", predicate,
515+
(owner && IsValidSid(owner) && ConvertSidToStringSidA(owner, &owner_text) && owner_text)
516+
? owner_text
517+
: "unresolved-sid");
518+
if (owner_text) {
519+
(void)LocalFree(owner_text);
520+
}
521+
activation_note_refusal(label, os_error);
522+
}
523+
427524
/* Trusted-owner acceptance for SOURCE-side objects: a downloaded release
428525
* bundle is owned by whatever the machine's default-owner policy dictates
429526
* (Administrators on GitHub-runner-class images). Its integrity is enforced
@@ -440,11 +537,13 @@ static bool activation_windows_owner_is_trusted(HANDLE handle) {
440537
PSECURITY_DESCRIPTOR descriptor = NULL;
441538
DWORD result = GetSecurityInfo(handle, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &owner, NULL,
442539
NULL, NULL, &descriptor);
540+
PSID local_admin = activation_windows_local_admin_sid();
443541
bool trusted = result == ERROR_SUCCESS && owner && IsValidSid(owner) &&
444542
(EqualSid(owner, user_sid) || IsWellKnownSid(owner, WinLocalSystemSid) ||
445-
IsWellKnownSid(owner, WinBuiltinAdministratorsSid));
543+
IsWellKnownSid(owner, WinBuiltinAdministratorsSid) ||
544+
(local_admin && EqualSid(owner, local_admin)));
446545
if (!trusted) {
447-
activation_note_refusal("owner-not-trusted", result);
546+
activation_windows_note_untrusted_owner("owner-not-trusted", owner, result);
448547
}
449548
if (descriptor) {
450549
(void)LocalFree(descriptor);
@@ -522,7 +621,7 @@ static bool activation_windows_owner_is_current(HANDLE handle) {
522621
}
523622
}
524623
if (!same) {
525-
activation_note_refusal("owner-not-current-user", result);
624+
activation_windows_note_untrusted_owner("owner-not-current-user", owner, result);
526625
}
527626
if (descriptor) {
528627
(void)LocalFree(descriptor);
@@ -596,10 +695,15 @@ static bool activation_windows_acl_check(HANDLE handle, DWORD tolerated_untruste
596695
PSID sid = (PSID)&ace->SidStart;
597696
size_t sid_capacity = (size_t)header->AceSize - sid_offset;
598697
DWORD sid_length = GetSidLengthRequired(((SID *)sid)->SubAuthorityCount);
698+
PSID local_admin = activation_windows_local_admin_sid();
599699
bool trusted = sid_length <= sid_capacity && IsValidSid(sid) &&
600700
GetLengthSid(sid) == sid_length &&
601701
(EqualSid(sid, user_sid) || IsWellKnownSid(sid, WinLocalSystemSid) ||
602702
IsWellKnownSid(sid, WinBuiltinAdministratorsSid) ||
703+
/* #1705: THIS machine's built-in Administrator (RID-500),
704+
* resolved via LSA — never a foreign S-1-5-21-*-500; same
705+
* tolerance as the daemon's win_sid_trusted. */
706+
(local_admin && EqualSid(sid, local_admin)) ||
603707
/* OWNER RIGHTS modulates whoever owns the object; the
604708
* owner is separately validated in every chain that
605709
* reaches here (same tolerance as the daemon IPC and

‎src/daemon/ipc.c‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3561,6 +3561,7 @@ bool cbm_daemon_ipc_local_transition_release(cbm_daemon_ipc_local_transition_t *
35613561
#include <windows.h>
35623562
#include <aclapi.h>
35633563
#include <sddl.h>
3564+
#include <ntsecapi.h>
35643565
#include <fcntl.h>
35653566
#include <io.h>
35663567
#include <shlobj.h>
@@ -4218,17 +4219,120 @@ static bool win_sid_is_trusted_installer(const uint8_t *sid, size_t sid_length)
42184219
return true;
42194220
}
42204221

4222+
/* #1705: the built-in Administrator ACCOUNT of THIS machine — RID 500 under the
4223+
* local machine's own account-domain SID (S-1-5-21-<machine>-500) — is a
4224+
* legitimate owner/grantee of directories an elevated install created, even when
4225+
* that account has been renamed or is disabled. It is resolved by asking LSA for
4226+
* the local machine account-domain SID and synthesizing its RID-500 SID with
4227+
* CreateWellKnownSid, then compared with EqualSid.
4228+
*
4229+
* It is deliberately NOT tested with IsWellKnownSid(sid, WinAccountAdministratorSid)
4230+
* and NOT by matching a trailing RID of 500: BOTH of those accept ANY domain's
4231+
* -500 — a domain administrator, or another machine's built-in Administrator —
4232+
* which is exactly the cross-machine trust escalation this must never open. Only
4233+
* THIS machine's -500 is trusted.
4234+
*
4235+
* Resolved once per process and cached; any LSA or synthesis failure leaves the
4236+
* cache NULL and therefore grants NO tolerance at all (fail closed). advapi32 is
4237+
* reached through the already-loaded module handle in win_security_t and the
4238+
* function pointers are resolved dynamically, matching this file's SID-API style
4239+
* and adding no static import. */
4240+
typedef NTSTATUS(NTAPI *lsa_open_policy_fn)(PLSA_UNICODE_STRING, PLSA_OBJECT_ATTRIBUTES,
4241+
ACCESS_MASK, PLSA_HANDLE);
4242+
typedef NTSTATUS(NTAPI *lsa_query_information_policy_fn)(LSA_HANDLE, POLICY_INFORMATION_CLASS,
4243+
PVOID *);
4244+
typedef NTSTATUS(NTAPI *lsa_free_memory_fn)(PVOID);
4245+
typedef NTSTATUS(NTAPI *lsa_close_fn)(LSA_HANDLE);
4246+
typedef BOOL(WINAPI *create_well_known_sid_fn)(WELL_KNOWN_SID_TYPE, PSID, PSID, DWORD *);
4247+
4248+
static INIT_ONCE g_local_admin_sid_once = INIT_ONCE_STATIC_INIT;
4249+
static PSID g_local_admin_sid = NULL; /* process-lifetime cache; NULL => no tolerance */
4250+
4251+
static BOOL CALLBACK win_resolve_local_admin_sid(PINIT_ONCE once, PVOID parameter, PVOID *context) {
4252+
(void)once;
4253+
(void)context;
4254+
win_security_t *security = (win_security_t *)parameter;
4255+
if (!security || !security->advapi) {
4256+
return TRUE; /* ran once; cache stays NULL (fail closed) */
4257+
}
4258+
HMODULE advapi = security->advapi;
4259+
lsa_open_policy_fn lsa_open =
4260+
(lsa_open_policy_fn)(void (*)(void))GetProcAddress(advapi, "LsaOpenPolicy");
4261+
lsa_query_information_policy_fn lsa_query = (lsa_query_information_policy_fn)(void (*)(
4262+
void))GetProcAddress(advapi, "LsaQueryInformationPolicy");
4263+
lsa_free_memory_fn lsa_free =
4264+
(lsa_free_memory_fn)(void (*)(void))GetProcAddress(advapi, "LsaFreeMemory");
4265+
lsa_close_fn lsa_close = (lsa_close_fn)(void (*)(void))GetProcAddress(advapi, "LsaClose");
4266+
create_well_known_sid_fn create_sid =
4267+
(create_well_known_sid_fn)(void (*)(void))GetProcAddress(advapi, "CreateWellKnownSid");
4268+
if (!lsa_open || !lsa_query || !lsa_free || !lsa_close || !create_sid) {
4269+
return TRUE;
4270+
}
4271+
LSA_OBJECT_ATTRIBUTES attributes;
4272+
memset(&attributes, 0, sizeof(attributes));
4273+
LSA_HANDLE policy = NULL;
4274+
/* STATUS_SUCCESS is 0; any other status (including informational positives) is
4275+
* treated as failure, keeping the outcome fail-closed. */
4276+
if (lsa_open(NULL, &attributes, POLICY_VIEW_LOCAL_INFORMATION, &policy) != 0 || !policy) {
4277+
return TRUE;
4278+
}
4279+
POLICY_ACCOUNT_DOMAIN_INFO *domain = NULL;
4280+
if (lsa_query(policy, PolicyAccountDomainInformation, (PVOID *)&domain) == 0 && domain &&
4281+
domain->DomainSid && security->is_valid_sid(domain->DomainSid)) {
4282+
DWORD needed = 0;
4283+
(void)create_sid(WinAccountAdministratorSid, domain->DomainSid, NULL, &needed);
4284+
if (needed > 0U) {
4285+
PSID resolved = malloc(needed);
4286+
if (resolved &&
4287+
create_sid(WinAccountAdministratorSid, domain->DomainSid, resolved, &needed) &&
4288+
security->is_valid_sid(resolved)) {
4289+
g_local_admin_sid = resolved;
4290+
} else {
4291+
free(resolved);
4292+
}
4293+
}
4294+
}
4295+
if (domain) {
4296+
(void)lsa_free(domain);
4297+
}
4298+
(void)lsa_close(policy);
4299+
return TRUE;
4300+
}
4301+
4302+
static PSID win_local_admin_sid(win_security_t *security) {
4303+
if (!security || !security->advapi) {
4304+
return NULL;
4305+
}
4306+
(void)InitOnceExecuteOnce(&g_local_admin_sid_once, win_resolve_local_admin_sid, (PVOID)security,
4307+
NULL);
4308+
return g_local_admin_sid;
4309+
}
4310+
42214311
static bool win_sid_trusted(win_security_t *security, PSID sid) {
42224312
if (!security || !sid || !security->is_valid_sid(sid)) {
42234313
return false;
42244314
}
42254315
DWORD sid_length = security->get_length_sid(sid);
4316+
PSID local_admin = win_local_admin_sid(security);
42264317
return (sid_length > 0U && security->equal_sid(sid, security->user_sid)) ||
42274318
security->is_well_known_sid(sid, WinLocalSystemSid) ||
42284319
security->is_well_known_sid(sid, WinBuiltinAdministratorsSid) ||
4320+
(local_admin && security->equal_sid(sid, local_admin)) ||
42294321
win_sid_is_trusted_installer((const uint8_t *)sid, (size_t)sid_length);
42304322
}
42314323

4324+
#ifdef CBM_ENABLE_TEST_SEAMS
4325+
bool cbm_daemon_ipc_win_sid_trusted_for_testing(void *sid) {
4326+
win_security_t security;
4327+
if (!win_security_init(&security)) {
4328+
return false;
4329+
}
4330+
bool trusted = win_sid_trusted(&security, (PSID)sid);
4331+
win_security_destroy(&security);
4332+
return trusted;
4333+
}
4334+
#endif
4335+
42324336
/* AppContainer identities: package SIDs (S-1-15-2-*) and capability SIDs
42334337
* (S-1-15-3-*), under the APP_PACKAGE identifier authority (15).
42344338
*

‎src/daemon/ipc.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ const char *cbm_daemon_ipc_validation_detail(void);
9494
#ifdef CBM_ENABLE_TEST_SEAMS
9595
/* #1537: seed the detail so a test can prove the CLI refusal surfaces it. */
9696
void cbm_daemon_ipc_set_validation_detail_for_testing(const char *detail);
97+
#ifdef _WIN32
98+
/* #1705: run the daemon's directory-owner/ACE trust predicate against an
99+
* arbitrary SID, so a test can assert THIS machine's built-in Administrator
100+
* (RID-500) is trusted while a foreign S-1-5-21-*-500 is not. Returns false on
101+
* any setup failure. Windows only. */
102+
bool cbm_daemon_ipc_win_sid_trusted_for_testing(void *sid);
103+
#endif
97104
#ifndef _WIN32
98105
/* #1830 seams (POSIX). Override the single-uid user-namespace overflow uid that
99106
* ancestors may be owned by (active=false restores the real /proc-derived

0 commit comments

Comments
 (0)