Skip to content

Commit 7e6ed7f

Browse files
committed
mingw: special-case administrators even more
The check for dubious ownership has one particular quirk on Windows: if running as an administrator, files owned by the Administrators _group_ are considered owned by the user. The rationale for that is: When running in elevated mode, Git creates files that aren't owned by the individual user but by the Administrators group. There is yet another quirk, though: The check I introduced to determine whether the current user is an administrator uses the `CheckTokenMembership()` function with the current process token. And that check only succeeds when running in elevated mode! Let's be a bit more lenient here and look harder whether the current user is an administrator. We do this by looking for a so-called "linked token". That token exists when administrators run in non-elevated mode, and can be used to create a new process in elevated mode. And feeding _that_ token to the `CheckTokenMembership()` function succeeds! Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 344af5c commit 7e6ed7f

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

compat/mingw.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,31 +3718,44 @@ static void setup_windows_environment(void)
37183718
has_symlinks = 0;
37193719
}
37203720

3721-
static PSID get_current_user_sid(void)
3721+
static void get_current_user_sid(PSID *sid, HANDLE *linked_token)
37223722
{
37233723
HANDLE token;
37243724
DWORD len = 0;
3725-
PSID result = NULL;
3725+
TOKEN_ELEVATION_TYPE elevationType;
3726+
DWORD size;
3727+
3728+
*sid = NULL;
3729+
*linked_token = NULL;
37263730

37273731
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
3728-
return NULL;
3732+
return;
37293733

37303734
if (!GetTokenInformation(token, TokenUser, NULL, 0, &len)) {
37313735
TOKEN_USER *info = xmalloc((size_t)len);
37323736
if (GetTokenInformation(token, TokenUser, info, len, &len)) {
37333737
len = GetLengthSid(info->User.Sid);
3734-
result = xmalloc(len);
3735-
if (!CopySid(len, result, info->User.Sid)) {
3738+
*sid = xmalloc(len);
3739+
if (!CopySid(len, *sid, info->User.Sid)) {
37363740
error(_("failed to copy SID (%ld)"),
37373741
GetLastError());
3738-
FREE_AND_NULL(result);
3742+
FREE_AND_NULL(*sid);
37393743
}
37403744
}
37413745
FREE_AND_NULL(info);
37423746
}
3743-
CloseHandle(token);
37443747

3745-
return result;
3748+
if (GetTokenInformation(token, TokenElevationType, &elevationType, sizeof(elevationType), &size) &&
3749+
elevationType == TokenElevationTypeLimited) {
3750+
/*
3751+
* The current process is run by a member of the Administrators
3752+
* group, but is not running elevated.
3753+
*/
3754+
if (!GetTokenInformation(token, TokenLinkedToken, linked_token, sizeof(*linked_token), &size))
3755+
linked_token = NULL; /* there is no linked token */
3756+
}
3757+
3758+
CloseHandle(token);
37463759
}
37473760

37483761
static BOOL user_sid_to_user_name(PSID sid, LPSTR *str)
@@ -3821,18 +3834,22 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
38213834
if (err == ERROR_SUCCESS && sid && IsValidSid(sid)) {
38223835
/* Now, verify that the SID matches the current user's */
38233836
static PSID current_user_sid;
3837+
static HANDLE linked_token;
38243838
BOOL is_member;
38253839

38263840
if (!current_user_sid)
3827-
current_user_sid = get_current_user_sid();
3841+
get_current_user_sid(&current_user_sid, &linked_token);
38283842

38293843
if (current_user_sid &&
38303844
IsValidSid(current_user_sid) &&
38313845
EqualSid(sid, current_user_sid))
38323846
result = 1;
38333847
else if (IsWellKnownSid(sid, WinBuiltinAdministratorsSid) &&
3834-
CheckTokenMembership(NULL, sid, &is_member) &&
3835-
is_member)
3848+
((CheckTokenMembership(NULL, sid, &is_member) &&
3849+
is_member) ||
3850+
(linked_token &&
3851+
CheckTokenMembership(linked_token, sid, &is_member) &&
3852+
is_member)))
38363853
/*
38373854
* If owned by the Administrators group, and the
38383855
* current user is an administrator, we consider that

0 commit comments

Comments
 (0)