Author: David Ripton <drip...@ripton.net>
Branch: fix_find_executable_bug
Changeset: r73243:8ff323403724
Date: 2014-08-31 11:20 -0400
http://bitbucket.org/pypy/pypy/changeset/8ff323403724/

Log:    issue #1856, find_executable should only find executable files.

        We use os.access to check whether the file is executable by the
        current user. os.access compares against the user's real uid and
        gid, not the effective uid and gid. I think that's okay because
        nobody should be running pypy setuid/setgid.

diff --git a/pypy/module/sys/initpath.py b/pypy/module/sys/initpath.py
--- a/pypy/module/sys/initpath.py
+++ b/pypy/module/sys/initpath.py
@@ -35,8 +35,12 @@
             for dir in path.split(os.pathsep):
                 fn = os.path.join(dir, executable)
                 if os.path.isfile(fn):
-                    executable = fn
-                    break
+                    # os.access checks using the user's real uid and gid.
+                    # Since pypy should not be run setuid/setgid, this
+                    # should be sufficient.
+                    if os.access(fn, os.X_OK):
+                        executable = fn
+                        break
     executable = rpath.rabspath(executable)
 
     # 'sys.executable' should not end up being an non-existing file;
diff --git a/pypy/module/sys/test/test_initpath.py 
b/pypy/module/sys/test/test_initpath.py
--- a/pypy/module/sys/test/test_initpath.py
+++ b/pypy/module/sys/test/test_initpath.py
@@ -57,6 +57,7 @@
     a.join('pypy').ensure(file=True)
     b.join('pypy').ensure(file=True)
     #
+    monkeypatch.setattr(os, 'access', lambda x, y: True)
     # if there is already a slash, don't do anything
     monkeypatch.chdir(tmpdir)
     assert find_executable('a/pypy') == a.join('pypy')
@@ -82,7 +83,11 @@
     # if pypy is found but it's not a file, ignore it
     c.join('pypy').ensure(dir=True)
     assert find_executable('pypy') == a.join('pypy')
+    # if pypy is found but it's not executable, ignore it
+    monkeypatch.setattr(os, 'access', lambda x, y: False)
+    assert find_executable('pypy') == ''
     #
+    monkeypatch.setattr(os, 'access', lambda x, y: True)
     monkeypatch.setattr(initpath, 'we_are_translated', lambda: True)
     monkeypatch.setattr(initpath, '_WIN32', True)
     monkeypatch.setenv('PATH', str(a))
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to