@@ -453,6 +453,52 @@ static bool activation_windows_owner_is_trusted(HANDLE handle) {
453453 return trusted ;
454454}
455455
456+ /* The SID Windows will stamp as OWNER on objects this process creates.
457+ *
458+ * That is TokenOwner, NOT TokenUser, and the two differ exactly when it matters:
459+ * for a member of the Administrators group the default owner is
460+ * BUILTIN\Administrators (the "System objects: Default owner for objects
461+ * created by members of the Administrators group" policy, default on Server and
462+ * common on hardened clients). So an elevated install created its staging file,
463+ * then refused it as "owner-not-current-user" — we rejected a file we had just
464+ * written ourselves (#1580), and the daemon path failed the same way, exiting
465+ * before it could say anything (#1582).
466+ *
467+ * Reading TokenOwner is STRICTER-or-equal, never looser: it is the one SID this
468+ * process stamps. On a non-elevated account TokenOwner == TokenUser and nothing
469+ * changes. It does not accept "anything an administrator owns" — only the exact
470+ * SID our own creations carry. */
471+ static bool activation_windows_token_owner (void * * information_out , PSID * sid_out ) {
472+ * information_out = NULL ;
473+ * sid_out = NULL ;
474+ HANDLE token = NULL ;
475+ if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY , & token )) {
476+ return false;
477+ }
478+ DWORD needed = 0 ;
479+ (void )GetTokenInformation (token , TokenOwner , NULL , 0 , & needed );
480+ if (needed == 0 || GetLastError () != ERROR_INSUFFICIENT_BUFFER ) {
481+ (void )CloseHandle (token );
482+ return false;
483+ }
484+ void * information = calloc (1 , needed );
485+ bool ok =
486+ information && GetTokenInformation (token , TokenOwner , information , needed , & needed ) != 0 ;
487+ (void )CloseHandle (token );
488+ if (!ok ) {
489+ free (information );
490+ return false;
491+ }
492+ PSID sid = ((TOKEN_OWNER * )information )-> Owner ;
493+ if (!sid || !IsValidSid (sid )) {
494+ free (information );
495+ return false;
496+ }
497+ * information_out = information ;
498+ * sid_out = sid ;
499+ return true;
500+ }
501+
456502static bool activation_windows_owner_is_current (HANDLE handle ) {
457503 void * information = NULL ;
458504 PSID user_sid = NULL ;
@@ -464,6 +510,17 @@ static bool activation_windows_owner_is_current(HANDLE handle) {
464510 DWORD result = GetSecurityInfo (handle , SE_FILE_OBJECT , OWNER_SECURITY_INFORMATION , & owner , NULL ,
465511 NULL , NULL , & descriptor );
466512 bool same = result == ERROR_SUCCESS && owner && IsValidSid (owner ) && EqualSid (owner , user_sid );
513+ /* Not our user SID — but Windows may legitimately have stamped our token's
514+ * OWNER instead (Administrators, for an elevated account). Accept that and
515+ * only that; see activation_windows_token_owner. */
516+ if (!same && result == ERROR_SUCCESS && owner && IsValidSid (owner )) {
517+ void * owner_information = NULL ;
518+ PSID token_owner = NULL ;
519+ if (activation_windows_token_owner (& owner_information , & token_owner )) {
520+ same = EqualSid (owner , token_owner ) != 0 ;
521+ free (owner_information );
522+ }
523+ }
467524 if (!same ) {
468525 activation_note_refusal ("owner-not-current-user" , result );
469526 }
0 commit comments