Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mingw: special-case administrators even more #712

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -3575,31 +3575,44 @@ static void setup_windows_environment(void)
has_symlinks = 0;
}

static PSID get_current_user_sid(void)
static void get_current_user_sid(PSID *sid, HANDLE *linked_token)
{
HANDLE token;
DWORD len = 0;
PSID result = NULL;
TOKEN_ELEVATION_TYPE elevationType;
DWORD size;

*sid = NULL;
*linked_token = NULL;

if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
return NULL;
return;

if (!GetTokenInformation(token, TokenUser, NULL, 0, &len)) {
TOKEN_USER *info = xmalloc((size_t)len);
if (GetTokenInformation(token, TokenUser, info, len, &len)) {
len = GetLengthSid(info->User.Sid);
result = xmalloc(len);
if (!CopySid(len, result, info->User.Sid)) {
*sid = xmalloc(len);
if (!CopySid(len, *sid, info->User.Sid)) {
error(_("failed to copy SID (%ld)"),
GetLastError());
FREE_AND_NULL(result);
FREE_AND_NULL(*sid);
}
}
FREE_AND_NULL(info);
}
CloseHandle(token);

return result;
if (GetTokenInformation(token, TokenElevationType, &elevationType, sizeof(elevationType), &size) &&
elevationType == TokenElevationTypeLimited) {
/*
* The current process is run by a member of the Administrators
* group, but is not running elevated.
*/
if (!GetTokenInformation(token, TokenLinkedToken, linked_token, sizeof(*linked_token), &size))
linked_token = NULL; /* there is no linked token */
}

CloseHandle(token);
}

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

if (!current_user_sid)
current_user_sid = get_current_user_sid();
get_current_user_sid(&current_user_sid, &linked_token);

if (current_user_sid &&
IsValidSid(current_user_sid) &&
EqualSid(sid, current_user_sid))
result = 1;
else if (IsWellKnownSid(sid, WinBuiltinAdministratorsSid) &&
CheckTokenMembership(NULL, sid, &is_member) &&
is_member)
((CheckTokenMembership(NULL, sid, &is_member) &&
is_member) ||
(linked_token &&
CheckTokenMembership(linked_token, sid, &is_member) &&
is_member)))
/*
* If owned by the Administrators group, and the
* current user is an administrator, we consider that
Expand Down
19 changes: 19 additions & 0 deletions t/helper/test-path-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,25 @@ int cmd__path_utils(int argc, const char **argv)
return !!res;
}

if (argc > 1 && !strcmp(argv[1], "is_path_owned_by_current_user")) {
int res = 0;

for (int i = 2; i < argc; i++) {
struct strbuf buf = STRBUF_INIT;

if (is_path_owned_by_current_user(argv[i], &buf))
printf("'%s' is owned by current SID\n", argv[i]);
else {
printf("'%s' is not owned by current SID: %s\n", argv[i], buf.buf);
res = 1;
}

strbuf_release(&buf);
}

return res;
}

fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
argv[1] ? argv[1] : "(there was none)");
return 1;
Expand Down
Loading