Skip to content

Commit 0389f98

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 <johannes.schindelin@gmx.de>
1 parent 2dc56ed commit 0389f98

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
@@ -3575,31 +3575,44 @@ static void setup_windows_environment(void)
35753575
has_symlinks = 0;
35763576
}
35773577

3578-
static PSID get_current_user_sid(void)
3578+
static void get_current_user_sid(PSID *sid, HANDLE *linked_token)
35793579
{
35803580
HANDLE token;
35813581
DWORD len = 0;
3582-
PSID result = NULL;
3582+
TOKEN_ELEVATION_TYPE elevationType;
3583+
DWORD size;
3584+
3585+
*sid = NULL;
3586+
*linked_token = NULL;
35833587

35843588
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
3585-
return NULL;
3589+
return;
35863590

35873591
if (!GetTokenInformation(token, TokenUser, NULL, 0, &len)) {
35883592
TOKEN_USER *info = xmalloc((size_t)len);
35893593
if (GetTokenInformation(token, TokenUser, info, len, &len)) {
35903594
len = GetLengthSid(info->User.Sid);
3591-
result = xmalloc(len);
3592-
if (!CopySid(len, result, info->User.Sid)) {
3595+
*sid = xmalloc(len);
3596+
if (!CopySid(len, *sid, info->User.Sid)) {
35933597
error(_("failed to copy SID (%ld)"),
35943598
GetLastError());
3595-
FREE_AND_NULL(result);
3599+
FREE_AND_NULL(*sid);
35963600
}
35973601
}
35983602
FREE_AND_NULL(info);
35993603
}
3600-
CloseHandle(token);
36013604

3602-
return result;
3605+
if (GetTokenInformation(token, TokenElevationType, &elevationType, sizeof(elevationType), &size) &&
3606+
elevationType == TokenElevationTypeLimited) {
3607+
/*
3608+
* The current process is run by a member of the Administrators
3609+
* group, but is not running elevated.
3610+
*/
3611+
if (!GetTokenInformation(token, TokenLinkedToken, linked_token, sizeof(*linked_token), &size))
3612+
linked_token = NULL; /* there is no linked token */
3613+
}
3614+
3615+
CloseHandle(token);
36033616
}
36043617

36053618
static BOOL user_sid_to_user_name(PSID sid, LPSTR *str)
@@ -3678,18 +3691,22 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
36783691
if (err == ERROR_SUCCESS && sid && IsValidSid(sid)) {
36793692
/* Now, verify that the SID matches the current user's */
36803693
static PSID current_user_sid;
3694+
static HANDLE linked_token;
36813695
BOOL is_member;
36823696

36833697
if (!current_user_sid)
3684-
current_user_sid = get_current_user_sid();
3698+
get_current_user_sid(&current_user_sid, &linked_token);
36853699

36863700
if (current_user_sid &&
36873701
IsValidSid(current_user_sid) &&
36883702
EqualSid(sid, current_user_sid))
36893703
result = 1;
36903704
else if (IsWellKnownSid(sid, WinBuiltinAdministratorsSid) &&
3691-
CheckTokenMembership(NULL, sid, &is_member) &&
3692-
is_member)
3705+
((CheckTokenMembership(NULL, sid, &is_member) &&
3706+
is_member) ||
3707+
(linked_token &&
3708+
CheckTokenMembership(linked_token, sid, &is_member) &&
3709+
is_member)))
36933710
/*
36943711
* If owned by the Administrators group, and the
36953712
* current user is an administrator, we consider that

0 commit comments

Comments
 (0)