Skip to content

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

Draft
duaneg wants to merge 4 commits intopython:mainfrom
duaneg:gh-100163
Draft

gh-100163: allow re-assuming root privileges on subprocess invocations#134400
duaneg wants to merge 4 commits intopython:mainfrom
duaneg:gh-100163

Conversation

@duaneg
Copy link
Copy Markdown
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
Copy Markdown
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
@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants