Skip to content

Commit 45e31f0

Browse files
committed
Merge branch 'js/mingw-admins-are-special'
"Dubious ownership" checks on Windows has been tightened up. * js/mingw-admins-are-special: test-tool path-utils: support debugging "dubious ownership" issues mingw: special-case administrators even more
2 parents 97b747c + 5bb88e8 commit 45e31f0

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-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

t/helper/test-path-utils.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,25 @@ int cmd__path_utils(int argc, const char **argv)
504504
return !!res;
505505
}
506506

507+
if (argc > 1 && !strcmp(argv[1], "is_path_owned_by_current_user")) {
508+
int res = 0;
509+
510+
for (int i = 2; i < argc; i++) {
511+
struct strbuf buf = STRBUF_INIT;
512+
513+
if (is_path_owned_by_current_user(argv[i], &buf))
514+
printf("'%s' is owned by current SID\n", argv[i]);
515+
else {
516+
printf("'%s' is not owned by current SID: %s\n", argv[i], buf.buf);
517+
res = 1;
518+
}
519+
520+
strbuf_release(&buf);
521+
}
522+
523+
return res;
524+
}
525+
507526
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
508527
argv[1] ? argv[1] : "(there was none)");
509528
return 1;

0 commit comments

Comments
 (0)