qwertyboy qwertyboy - 2 months ago 52
Python Question

Python CGIHTTPServer crashes with "OSError: [Errno 13] Permission denied"

I am running the following command from my home directory:

python -m CGIHTTPServer


This runs the server, but when I try to access a script in the cgi-bin directory I get:

Traceback (most recent call last):
File "/usr/lib/python2.7/CGIHTTPServer.py", line 251, in run_cgi
os.execve(scriptfile, args, env)
OSError: [Errno 13] Permission denied


Running as root does not make a difference. The files seem to have all the right permissions:

student@bandersnatch:~$ ls -lhR
.:
total 12K
drwxr-xr-x 2 student student 4.0K Jun 13 18:38 cgi-bin
drwxr--r-- 2 student student 4.0K Jun 10 2004 kalpy
-rwxrwxrwx 1 student student 2.0K Jun 13 12:37 test.html

./cgi-bin:
total 8.0K
-rwxr-xr-x 1 student student 31 Jun 13 18:38 test.py


Edit: The content of test.py is:

#!/usr/bin/python
print "test"


The shebang is valid:

~$ which python
/usr/bin/python

Answer

Are you, by any chance, running the process as root?

If you use the source, you will see in CGIHTTPServer.py, just before calling execve:

try:
    os.setuid(nobody)
except os.error:
    pass

That is, it will run the CGI script as nobody, if it is able to change the UID, that is if it is root. If it is not root, this call will most likely fail, and pass on.

So my guess is that you are running the server as root, so the script is run as nobody, but this user doesn't have access to the script. Which is expected, as you say that it is in your home dir.

Two solutions that I can think of:

  • The recommended: do not run the server as root!
  • The workaround: copy the script to a directory where nobody can read it (/tmp for example).
Comments