Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 553762 - dev-python/pip: Disable running as root
Summary: dev-python/pip: Disable running as root
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Unspecified (show other bugs)
Hardware: All Linux
: Normal critical (vote)
Assignee: Python Gentoo Team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-07-02 05:05 UTC by Patrick Lauer
Modified: 2019-11-01 08:05 UTC (History)
7 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
Disable systemwide package install (0001-install-Raise-an-error-to-avoid-breaking-python-exec.patch,1.04 KB, patch)
2015-10-27 16:26 UTC, Mike Gilbert
Details | Diff
debian patch (prevent-pip-from-removing-system-packages.patch,2.87 KB, patch)
2016-02-04 16:59 UTC, Oleh
Details | Diff
better patch from debian (prevent-pip-from-removing-system-packages.patch,2.65 KB, patch)
2016-02-04 17:01 UTC, Oleh
Details | Diff
Make Portage itself pip-installable (0001-Patch-setup.py-to-enable-bdist-wheel-support.patch,2.47 KB, patch)
2019-11-01 05:51 UTC, Kenneth G. Strawn
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Patrick Lauer gentoo-dev 2015-07-02 05:05:58 UTC
Ahoi,

when run as root pip tends to nuke python-exec, which then breaks pretty much all python apps installed through portage.

This is a known stupidity and should be patched out of pip, I'd suggest exit(-1) when run as root.

(It's such a furball of indirections that I have no idea what is called how, so I can't figure out what to patch. Yeargh!)
Comment 1 Mike Gilbert gentoo-dev 2015-07-02 15:20:15 UTC
Do we have a reproducible test case for this?

I have seen many related bugs, but I can recall anybody telling us exactly what they installed with pip that overwrote python-exec.

I want to make sure this is a really problem with pip, rather than distutils or setuptools.
Comment 2 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2015-07-02 17:17:11 UTC
Well, I think the core issue here is that pip installs into /usr. I think it might be a better solution to just make it install into /root (like it does homedir installs for regular users, and like CPAN works).
Comment 3 Mike Gilbert gentoo-dev 2015-07-02 18:27:17 UTC
pip only does a homedir install if you pass --user.

pip install --user setuptools

Otherwise, it defaults to a system-wide install, regardless of the current UID.
Comment 4 Mike Gilbert gentoo-dev 2015-07-02 18:30:43 UTC
Of course, we can patch that behavior; I'm just clarifying how it currently works.
Comment 5 Brian Evans (RETIRED) gentoo-dev 2015-10-27 15:35:49 UTC
(In reply to Mike Gilbert from comment #1)
> Do we have a reproducible test case for this?
> 
> I have seen many related bugs, but I can recall anybody telling us exactly
> what they installed with pip that overwrote python-exec.
> 
> I want to make sure this is a really problem with pip, rather than distutils
> or setuptools.

A user in #gentoo said he executed "sudo pip install -U mopidy" and it broke python-exec.

I started to investigate and the same command did not break on ~amd64.

grknight@akame ~/mysql-extras $ sudo pip install -U mopidy
Collecting mopidy
  Downloading Mopidy-1.1.1-py2.py3-none-any.whl (202kB)
    100% | | 204kB 138kB/s
Collecting Pykka>=1.1 (from mopidy)
  Downloading Pykka-1.2.1-py2.py3-none-any.whl
Collecting tornado>=2.3 (from mopidy)
  Downloading tornado-4.2.1.tar.gz (434kB)
    100% | | 438kB 483kB/s
Requirement already up-to-date: requests>=2.0 in /usr/lib64/python3.4/site-packages (from mopidy)
Requirement already up-to-date: setuptools in /usr/lib64/python3.4/site-packages (from mopidy)
Installing collected packages: Pykka, tornado, mopidy
  Running setup.py install for tornado
Successfully installed Pykka-1.2.1 mopidy-1.1.1 tornado-4.2.1

So i started from a blank, stable VM and python-exec broke.   The same command installed a new version of setuptools and now `pip --version` reports: "setuptools 18.4 from /usr/lib/python2.7/site-packages"
Comment 6 Mike Gilbert gentoo-dev 2015-10-27 16:26:42 UTC
Created attachment 415590 [details, diff]
Disable systemwide package install

Please give this patch a try. It should prevent pip install from being run without the --target, --root, or --user options.
Comment 7 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2015-10-27 16:30:04 UTC
Doesn't this break virtualenv?
Comment 8 Mike Gilbert gentoo-dev 2015-10-27 16:33:19 UTC
(In reply to Michał Górny from comment #7)
> Doesn't this break virtualenv?

I don't know. Please test it and let me know if it does.
Comment 9 Mike Gilbert gentoo-dev 2015-10-27 16:53:04 UTC
virtualenv seems to install its own fresh, unpatched copy of pip unless you pas --no-pip to it.
Comment 10 David Flogeras 2015-11-03 20:34:02 UTC
https://bugs.gentoo.org/show_bug.cgi?id=564836
Comment 11 Oleh 2016-01-10 16:57:23 UTC
alternatively, it could be used as root, but by default pip could install everything into /usr/local to not mess things. i believe Debian have their pip working that way.
Comment 15 Oleh 2016-02-04 16:59:54 UTC
Created attachment 424606 [details, diff]
debian patch
Comment 16 Oleh 2016-02-04 17:01:39 UTC
Created attachment 424608 [details, diff]
better patch from debian

sorry, previous attachment broken
Comment 17 Mike Gilbert gentoo-dev 2017-01-04 04:29:07 UTC
commit 698a23d83720989123978eb212b387bc5fe96e4a
Author: Mike Gilbert <floppym@gentoo.org>
Date:   Tue Jan 3 23:28:30 2017 -0500

    dev-python/pip: disable systemwide installs
    
    Bug: https://bugs.gentoo.org/553762
    
    Package-Manager: Portage-2.3.3_p13, Repoman-2.3.1_p6

 .../pip/files/pip-disable-system-install.patch     | 29 ++++++++++++++++++++++
 .../pip/{pip-9.0.1.ebuild => pip-9.0.1-r1.ebuild}  | 12 ++++-----
 2 files changed, 34 insertions(+), 7 deletions(-)
Comment 18 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2017-01-04 09:04:53 UTC
I guess that will also prevent people from cleaning packages already installed by pip out. But then, that was unsafe anyway.
Comment 19 SergeiMinaev 2017-01-13 01:57:07 UTC
Latest PIP version did not work with virtualenvs.

I have several virtual envs for my projects. Today I tried to update Django which is installed in virtual env and got this error:

$ pip install -U django
ERROR: (Gentoo) Please run pip with the --user option to avoid breaking python-exec

Is there possibility to ignore it?
Comment 20 SergeiMinaev 2017-01-13 02:13:53 UTC
Also I don't understand why I getting that error when I run PIP as non-root user. It can not damage system anyway.
Comment 21 Mike Gilbert gentoo-dev 2017-01-13 02:37:54 UTC
(In reply to Brainsburn from comment #19)

Please give exact steps to reproduce the problem.
Comment 22 SergeiMinaev 2017-01-13 14:05:12 UTC
(In reply to Mike Gilbert from comment #21)
> Please give exact steps to reproduce the problem.

There is virtualenv and Django-1.10.2 installed in virtual enviroment named "testing". I'm trying to update Django:

# emerge =pip-9.0.1-r1
$ workon testing  # <- virtualenvwrapper's command to activate virtual environment
$ pip show Django
Name: Django
Version: 1.10.2
Location: /myworkdir/testing/lib/python2.7/site-packages

$ pip install -U Django
ERROR: (Gentoo) Please run pip with the --user option to avoid breaking python-exec
$ pip install -U Django --user
Successfully installed Django-1.10.5
$ pip show Django
Name: Django
Version: 1.10.2
Location: /myworkdir/testing/lib/python2.7/site-packages

The problem is that I need to install Django to "myworkdir/testing/" but with --user option it installs somewhere in ~/.local/
Comment 23 Mike Gilbert gentoo-dev 2017-01-13 14:45:39 UTC
I cannot reproduce the problem. The virtualenv uses its own, unaltered copy of pip, and does not display the (Gentoo) message.

floppym@naomi ~ % equery l pip
 * Searching for pip ...
[IP-] [  ] dev-python/pip-9.0.1-r1:0
floppym@naomi ~ % mkdir .venv
floppym@naomi ~ % export WORKON_HOME=$HOME/.venv
floppym@naomi ~ % . /usr/bin/virtualenvwrapper.sh 
virtualenvwrapper.user_scripts creating /home/floppym/.venv/initialize
virtualenvwrapper.user_scripts creating /home/floppym/.venv/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/floppym/.venv/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/floppym/.venv/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/floppym/.venv/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/floppym/.venv/predeactivate
virtualenvwrapper.user_scripts creating /home/floppym/.venv/postdeactivate
virtualenvwrapper.user_scripts creating /home/floppym/.venv/preactivate
virtualenvwrapper.user_scripts creating /home/floppym/.venv/postactivate
virtualenvwrapper.user_scripts creating /home/floppym/.venv/get_env_details
virtualenvwrapper.user_scripts creating /home/floppym/.venv/premkproject
virtualenvwrapper.user_scripts creating /home/floppym/.venv/postmkproject
floppym@naomi ~ % workon
floppym@naomi ~ % mkvirtualenv testing
Using base prefix '/usr'
New python executable in /home/floppym/.venv/testing/bin/python3.5
Also creating executable in /home/floppym/.venv/testing/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/floppym/.venv/testing/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/floppym/.venv/testing/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/floppym/.venv/testing/bin/preactivate
virtualenvwrapper.user_scripts creating /home/floppym/.venv/testing/bin/postactivate
virtualenvwrapper.user_scripts creating /home/floppym/.venv/testing/bin/get_env_details
(testing) floppym@naomi ~ % pip install "Django == 1.10.2"
Collecting Django==1.10.2
  Downloading Django-1.10.2-py2.py3-none-any.whl (6.8MB)
Installing collected packages: Django
Successfully installed Django-1.10.2
(testing) floppym@naomi ~ % pip show Django
Name: Django
Version: 1.10.2
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: http://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: /home/floppym/.venv/testing/lib/python3.5/site-packages
Requires: 
(testing) floppym@naomi ~ % pip install -U Django
Collecting Django
  Using cached Django-1.10.5-py2.py3-none-any.whl
Installing collected packages: Django
  Found existing installation: Django 1.10.2
    Uninstalling Django-1.10.2:
      Successfully uninstalled Django-1.10.2
Successfully installed Django-1.10.5
Comment 24 SergeiMinaev 2017-01-13 15:07:07 UTC
Oh I think I got it. Probably my virtualenv was created with "--no-pip" option. Without this option pip works fine. Sorry that I bothered you.
Comment 25 Mike Gilbert gentoo-dev 2017-01-13 15:54:19 UTC
(In reply to Brainsburn from comment #24)

Ah, that would make sense. No worries, I'm glad to get the feedback.
Comment 26 Matthew Thode ( prometheanfire ) archtester Gentoo Infrastructure gentoo-dev Security 2019-06-19 04:22:21 UTC
Is there a reason this is still open?
Comment 27 Kenneth G. Strawn 2019-11-01 05:51:08 UTC
Created attachment 594716 [details, diff]
Make Portage itself pip-installable

Apply my patch and you'll be able to pack Portage as 'portage-2.3.78-py3-none-any.whl' and upload it to PyPI. Much cleaner solution than having to throw an error whenever pip is run as root, one would imagine.
Comment 28 Kenneth G. Strawn 2019-11-01 06:24:02 UTC
https://pypi.org/project/portage/2.3.78/

In case there was any doubt about my solution working ― hear, hear.
Comment 29 Kenneth G. Strawn 2019-11-01 07:58:43 UTC
Removed the test build from PyPI due to failure to bundle some important scripts (need to modify setup.py some more), but the fact that this does still produce a working installable package should be noted. It kind of renders the errors moot.
Comment 30 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2019-11-01 08:05:32 UTC
(In reply to Matthew Thode ( prometheanfire ) from comment #26)
> Is there a reason this is still open?

I'd guess we were waiting for old versions to disappear.