Skip to content

Commit 03a4e46

Browse files
dschogitster
authored andcommitted
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 683c54c commit 03a4e46

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
@@ -2826,31 +2826,44 @@ static void setup_windows_environment(void)
28262826
}
28272827
}
28282828

2829-
static PSID get_current_user_sid(void)
2829+
static void get_current_user_sid(PSID *sid, HANDLE *linked_token)
28302830
{
28312831
HANDLE token;
28322832
DWORD len = 0;
2833-
PSID result = NULL;
2833+
TOKEN_ELEVATION_TYPE elevationType;
2834+
DWORD size;
2835+
2836+
*sid = NULL;
2837+
*linked_token = NULL;
28342838

28352839
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
2836-
return NULL;
2840+
return;
28372841

28382842
if (!GetTokenInformation(token, TokenUser, NULL, 0, &len)) {
28392843
TOKEN_USER *info = xmalloc((size_t)len);
28402844
if (GetTokenInformation(token, TokenUser, info, len, &len)) {
28412845
len = GetLengthSid(info->User.Sid);
2842-
result = xmalloc(len);
2843-
if (!CopySid(len, result, info->User.Sid)) {
2846+
*sid = xmalloc(len);
2847+
if (!CopySid(len, *sid, info->User.Sid)) {
28442848
error(_("failed to copy SID (%ld)"),
28452849
GetLastError());
2846-
FREE_AND_NULL(result);
2850+
FREE_AND_NULL(*sid);
28472851
}
28482852
}
28492853
FREE_AND_NULL(info);
28502854
}
2851-
CloseHandle(token);
28522855

2853-
return result;
2856+
if (GetTokenInformation(token, TokenElevationType, &elevationType, sizeof(elevationType), &size) &&
2857+
elevationType == TokenElevationTypeLimited) {
2858+
/*
2859+
* The current process is run by a member of the Administrators
2860+
* group, but is not running elevated.
2861+
*/
2862+
if (!GetTokenInformation(token, TokenLinkedToken, linked_token, sizeof(*linked_token), &size))
2863+
linked_token = NULL; /* there is no linked token */
2864+
}
2865+
2866+
CloseHandle(token);
28542867
}
28552868

28562869
static BOOL user_sid_to_user_name(PSID sid, LPSTR *str)
@@ -2931,18 +2944,22 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
29312944
else if (sid && IsValidSid(sid)) {
29322945
/* Now, verify that the SID matches the current user's */
29332946
static PSID current_user_sid;
2947+
static HANDLE linked_token;
29342948
BOOL is_member;
29352949

29362950
if (!current_user_sid)
2937-
current_user_sid = get_current_user_sid();
2951+
get_current_user_sid(&current_user_sid, &linked_token);
29382952

29392953
if (current_user_sid &&
29402954
IsValidSid(current_user_sid) &&
29412955
EqualSid(sid, current_user_sid))
29422956
result = 1;
29432957
else if (IsWellKnownSid(sid, WinBuiltinAdministratorsSid) &&
2944-
CheckTokenMembership(NULL, sid, &is_member) &&
2945-
is_member)
2958+
((CheckTokenMembership(NULL, sid, &is_member) &&
2959+
is_member) ||
2960+
(linked_token &&
2961+
CheckTokenMembership(linked_token, sid, &is_member) &&
2962+
is_member)))
29462963
/*
29472964
* If owned by the Administrators group, and the
29482965
* current user is an administrator, we consider that

0 commit comments

Comments
 (0)