Skip to content

gh-118486: Switch mkdir(mode=0o700) on Windows to use OWNER RIGHTS instead of CURRENT_USER - #118515

Merged
zooba merged 1 commit into
python:mainfrom
zooba:gh-118486
May 2, 2024
Merged

gh-118486: Switch mkdir(mode=0o700) on Windows to use OWNER RIGHTS instead of CURRENT_USER#118515
zooba merged 1 commit into
python:mainfrom
zooba:gh-118486

Conversation

@zooba

@zoobazooba commented May 2, 2024

Copy link
Copy Markdown
Member

This ensures directories created while elevated are not accessible by the same user when non-elevated.

Comment threadModules/posixmodule.c

int use_alias = 0;
DWORD cbSid = sizeof(data->sid);
if (!CreateWellKnownSid(WinCreatorOwnerRightsSid, NULL, (PSID)data->sid, &cbSid)) {

@eryksuneryksunMay 3, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you prefer to use "CURRENT_USER", you could just move it to the end of the array. Then the SetEntriesInAclW() call would use a count of 2 or 3 depending on whether the current user is an elevated administrator. The latter is determined by querying TokenElevationType. For example:

ULONGentry_count=3;
TOKEN_ELEVATION_TYPEelevation_type;
DWORDreturn_length;
if (GetTokenInformation(GetCurrentProcessToken(), TokenElevationType,
&elevation_type, sizeof(elevation_type), &return_length) &&elevation_type==TokenElevationTypeFull)
{
entry_count=2;
}

GetCurrentProcessToken() returns the pseudohandle (HANDLE)-4, which has been supported since Windows 8.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but I definitely don't want to try to detect anything about the user's current state. That feels brittle (e.g. if more things are added that ought to be detected).

OWNER RIGHTS is good, and it's actually what I tried to find first. The fallback is just for completeness - I don't expect CreateWellKnownSid to ever fail here, but at least if it does, we're probably not doing anything too bad.

SonicField pushed a commit to SonicField/cpython that referenced this pull request May 8, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@zooba@eryksun