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
0 commit comments