8000 gh-100163: allow re-assuming root privileges on subprocess invocations by duaneg · Pull Request #134400 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-100163: allow re-assuming root privileges on subprocess invocations #134400

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

duaneg
Copy link
Contributor
@duaneg duaneg commented May 21, 2025

The subprocess.Popen class takes user/group/extra groups parameters which allow invocations to run as a different user. When dropping privileges (i.e. the main program is running as root but wants to run a subprocess as an unprivileged user) this works fine. However, it fails if a program wants to drop privileges and re-assume them to run a subprocess.

To allow this there are a couple of changes required:

  • Use setresuid/setresgid instead of setreuid/setregid so the saved user/group identity can be re-assumed after it has been dropped.
  • Setting the user identity first so the group identity and extra groups calls can be made with root privilege.

Naturally we need to continue to set the user identity last when going from root to an unprivileged user, so the order of calls needs to be determined dynamically based on whether we are switching to or from root.

duaneg added 3 commits May 21, 2025 12:19
This is a preparatory refactoring which should have no semantic effect in
itself but will hopefully make the real changes coming in subsequent patches
easier to review.
Setting the saved user/group-ID allows the code to re-assume privileges after
dropping them.
When dropping privileges we need to set the new effective UID last, so we have
permission to change the gid/groups. When re-assuming root privileges we need
to do the opposite: set the UID first so we then have permission to change
gid/groups.
@gpshead
Copy link
Member
gpshead commented May 21, 2025

status: I'm marking this as a Draft as we're deciding (see issue) on if this is the right approach. manual testing likely required due to privs. required.

@gpshead gpshead marked this pull request as draft May 21, 2025 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0