Visitar URL original
bpo-36046: Add user and group parameters to subprocess by patrick-mclean · Pull Request #11950 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20220506202120/https://github.com/python/cpython/pull/11950
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

bpo-36046: Add user and group parameters to subprocess #11950

Merged

Conversation

Copy link
Contributor

@patrick-mclean patrick-mclean commented Feb 20, 2019 •

This adds user and groups parameters to the subprocess.Popen constructor, which allows dropping privileges when running a subprocess.

https://bugs.python.org/issue36046

@the-knights-who-say-ni
Copy link

@the-knights-who-say-ni the-knights-who-say-ni commented Feb 20, 2019

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

Doc/library/subprocess.rst Outdated Show resolved Hide resolved
Doc/library/subprocess.rst Outdated Show resolved Hide resolved
Doc/library/subprocess.rst Outdated Show resolved Hide resolved
Doc/library/subprocess.rst Outdated Show resolved Hide resolved
Doc/library/subprocess.rst Outdated Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
Modules/_posixsubprocess.c Outdated Show resolved Hide resolved
Modules/_posixsubprocess.c Outdated Show resolved Hide resolved
@gpshead gpshead self-assigned this Feb 20, 2019
@gpshead
Copy link

@gpshead gpshead commented Feb 20, 2019

fyi - we will need the CLA to be signed to accept this PR (see the early CLA bot comment)

@patrick-mclean
Copy link
Author

@patrick-mclean patrick-mclean commented Feb 20, 2019 •

fyi - we will need the CLA to be signed to accept this PR (see the early CLA bot comment)

I signed the CLA right before submitting this PR, it just hasn't picked up on that yet. Hopefully it will pick it up soon.

@patrick-mclean patrick-mclean force-pushed the subprocess-user-group-parameters branch 3 times, most recently from 1781568 to 4121a81 Compare Feb 20, 2019
@patrick-mclean
Copy link
Author

@patrick-mclean patrick-mclean commented Feb 20, 2019

Updated with all recommended changes, and fixed the documentation issue that was causing Travis and Azure to fail.

@patrick-mclean patrick-mclean force-pushed the subprocess-user-group-parameters branch 4 times, most recently from 463ed0d to 8e9ac28 Compare Feb 20, 2019
Doc/library/subprocess.rst Outdated Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
Lib/test/test_subprocess.py Outdated Show resolved Hide resolved
Lib/test/test_subprocess.py Outdated Show resolved Hide resolved
Lib/test/test_subprocess.py Outdated Show resolved Hide resolved
Modules/_posixsubprocess.c Outdated Show resolved Hide resolved
Modules/_posixsubprocess.c Outdated Show resolved Hide resolved
Lib/test/test_subprocess.py Outdated Show resolved Hide resolved
@patrick-mclean
Copy link
Author

@patrick-mclean patrick-mclean commented Feb 21, 2019

Just pushed more changes based on feedback. This push does add PyMem_RawMalloc() and PyMem_RawFree() calls in subprocess_fork_exec to avoid allocating 256KB on the stack as it was doing before.

@patrick-mclean patrick-mclean force-pushed the subprocess-user-group-parameters branch 5 times, most recently from e70bf5b to 2b90745 Compare Feb 21, 2019
Doc/library/subprocess.rst Outdated Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
Lib/test/test_subprocess.py Outdated Show resolved Hide resolved
Lib/subprocess.py Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
@patrick-mclean patrick-mclean force-pushed the subprocess-user-group-parameters branch 2 times, most recently from e094812 to d0dad2d Compare Feb 22, 2019
@patrick-mclean
Copy link
Author

@patrick-mclean patrick-mclean commented Feb 22, 2019

Pushed another update based on the latest round of feedback.

@patrick-mclean patrick-mclean force-pushed the subprocess-user-group-parameters branch 7 times, most recently from f9f8fce to de8a836 Compare Feb 28, 2019
@patrick-mclean
Copy link
Author

@patrick-mclean patrick-mclean commented Feb 28, 2019

Updated again, rebased against the current master.

I addressed all the feedback so far. It now uses the feature check macros, and will raise ValueError if the features are missing. The groups parameter has been split to group and extra_groups, setgroups() will not be called if extra_groups is None.

I also added negative tests to make sure that ValueError is raised on systems where these features are missing.

I ran the tests locally with superuser privileges to make sure they actually work when setgroups() actually succeeds rather than returning EPERM.

As a side note, if we want to test the setgroups() in CI, It could potentially be possible to test privileged operations in CI by running the tests in a user namespace.

@patrick-mclean patrick-mclean force-pushed the subprocess-user-group-parameters branch from de8a836 to 55393eb Compare Feb 28, 2019
Copy link
Contributor

@izbyshev izbyshev left a comment

Thank you for your work, Patrick. Looks good to me except for several small issues (mostly cosmetic) and the issue with uid_t/gid_t types.

}
#else /* HAVE_SETGROUPS */
PyErr_SetString(PyExc_ValueError,
"setgroups() is not supported on the current platform");
Copy link
Contributor

@izbyshev izbyshev Mar 2, 2019

Choose a reason for hiding this comment

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

I think you don't need to go into trouble of raising an exception here. The availability of setgroups is checked on Python side, and AFAIK there is no public API to call into the C helper directly (@gpshead: is this right?), so you can remove #else clause and move the if statement under #ifdef (i.e. make it the same as when you call setgroups).

This applies to setregid and setreuid as well.

Copy link
Member

@gpshead gpshead Mar 2, 2019

Choose a reason for hiding this comment

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

It is still useful to raise in this situation as it helps debug internal API use problems.

Copy link
Contributor

@izbyshev izbyshev Mar 2, 2019

Choose a reason for hiding this comment

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

Maybe raise something more serious than a ValueError then? It's effectively an assertion failure if we got here.

Copy link
Contributor Author

@patrick-mclean patrick-mclean Mar 4, 2019

Choose a reason for hiding this comment

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

From the description in the docs, it looks like SystemError is probably appropriate here, since it's basically a bug in the library if it reaches this (or someone is doing something they shouldn't).

I could also use RuntimeError here as well, if SystemError is not appropriate.

Copy link
Contributor

@izbyshev izbyshev Mar 4, 2019

Choose a reason for hiding this comment

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

IMHO SystemError is appropriate. You may raise it via PyErr_BadInternalCall.

Lib/test/test_subprocess.py Outdated Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
Modules/_posixsubprocess.c Outdated Show resolved Hide resolved
Modules/_posixsubprocess.c Outdated Show resolved Hide resolved
Modules/_posixsubprocess.c Outdated Show resolved Hide resolved
@patrick-mclean patrick-mclean force-pushed the subprocess-user-group-parameters branch 2 times, most recently from 9751206 to 2e68e49 Compare Mar 4, 2019
@patrick-mclean
Copy link
Author

@patrick-mclean patrick-mclean commented Mar 4, 2019

Updated again, rebased against current master.

  • Made the minor changes suggested above
  • Moved to using _Py_Uid_Converter/_Py_Gid_Converter as discussed above.
  • Made the C code use PyErr_BadInternalCall() to raise SystemError if an attempt to use setreuid()/setregid()/setgroups() on a platform that does not support it makes it to C code.
  • Moved the negative tests to skipUnless

Lib/test/test_subprocess.py Outdated Show resolved Hide resolved
Lib/subprocess.py Outdated Show resolved Hide resolved
….Popen

This adds a `user` parameter to the Popen constructor that will call
setreuid() in the child before calling exec(). This allows processes
running as root to safely drop privileges before running the subprocess
without having to use a preexec_fn.

This also adds a `group` parameter that will call setregid() in
the child process before calling exec().

Finally an `extra_groups` parameter was added that will call
setgroups() to set the supplimental groups.
@patrick-mclean patrick-mclean force-pushed the subprocess-user-group-parameters branch from 2e68e49 to 2528076 Compare Mar 16, 2019
@gpshead gpshead added sprint type-feature labels Sep 12, 2019
@gpshead gpshead merged commit 2b2ead7 into python:master Sep 12, 2019
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA signed sprint type-feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants