473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

os.execve(pth,args,env) and os.chroot(pth) = problems

Hello fellow python users,

I've been working on a basic implementation of a privilege separated
web server, and I've goto the point of running a basic cgi script.
Basically when the execCGI function in my Unpriv.py program is called a
few things should happen, it should fork (which it does), the stdout of
the child should be redirected to a given pipe (which it does), and the
script should execute using execve(which is has problems with). Also
before the execCGI() fucntion is executed, a few important things
happen in __init__: os.chroot('/var/www/webroot/') the path is web
server root and os.setuid(33) where 33 is the uid of a user on my
machine. It seems me that the os.chroot() call is messing up the
os.execve() (which executes the cgi script). os.execve(pth, args,
envVariables) is only executed if os.path.exists(pth) returns True.
But when I run a CGI script that os.path.exists(pth) returns True for,
then os.execve(pth) python throws back the error:

File "/***/***/***/Unpriv.py", line 201, in execCGI
OSError: [Errno 2] No such file or directory

However I don't see how this is possible if os.path.exists(pth) returns
True, why is it os.execve() has problems finding it.

I thought it might be a user permission problem, but I've run it
without the chroot and with the setuid and it works fine. So I'm
pretty sure its something to do with the chroot conflicting with the
os.execve(). Also execution privileges for the script file are also
checked before execution.
Any insight would be greatly appreciated
Here are the relevant functions of Unpriv.py
class Unpriv(object):

def __init__(self):
pipes = sys.argv
self.fromParent, self.toParent = os.pipe()
# this is just how http requested are recieved from a privilege
part and sent back
os.dup2(int(pipes[2]), self. toParent) ##write
os.dup2(int(pipes[1]), self.fromParent) ##read

os.chroot('/var/www/webroot/') ##chrooting
os.setuid(33) ##setuid
def checkFileExists(self, pth): ## just calls os.path.exists()
return os.path.exists(pth)
def execCGI(self, pth):
##
x = pth.rfind('?')
if x > -1:
path = pth[:x]
query = pth[x+1:]
query = query.replace('+', ' ')
else:
path = pth
query = ''

##populate enviroment variables, not yet implemented
envVariables = {}

readEnd, writeEnd = os.pipe()
parentStout = sys.stdout.fileno()
args = ['']
args.append(query)

pid = os.fork()

if pid == 0:
##child

os.dup2(writeEnd, sys.stdout.fileno()) ##this works

## this is the problem!
if self.checkFileExists(pth):
os.execve(pth , args, envVariables)
else:
## just some debug info for me.
## in fact part of pth is there
##however for some reason execve doesn't see
print 'the directory listing is:',
os.listdir(os.getcwd())

os._exit(0)

else: #parent
#read the output from the script
scriptOutPut = os.read(readEnd, 100000)
#just some debug info for me
print 'OUTPUT FROM SCRIPT EXECUTION: ' , scriptOutPut
return scriptOutPut

Please excuse any problems with the formatting.
Thanks in advance.

Mar 7 '06 #1
2 5356
In article <11*********************@j52g2000cwj.googlegroups. com>,
"go***************@gmail.com" <go***************@gmail.com> wrote:
... It seems me that the os.chroot() call is messing up the
os.execve() (which executes the cgi script). os.execve(pth, args,
envVariables) is only executed if os.path.exists(pth) returns True.
But when I run a CGI script that os.path.exists(pth) returns True for,
then os.execve(pth) python throws back the error:

File "/***/***/***/Unpriv.py", line 201, in execCGI
OSError: [Errno 2] No such file or directory

However I don't see how this is possible if os.path.exists(pth) returns
True, why is it os.execve() has problems finding it.


I haven't used chroot enough to know all the pitfalls, but
here's one guess: suppose the CGI script file `pth' might
actually be a script, with a `#!' top line that points to
an interpreter that isn't there, in your chroot space?

Donn Cave, do**@u.washington.edu
Mar 7 '06 #2
Thanks for the reply Donn,
It seems logical enough to me that finding #!/usr/bin/env python in the
script file with the chroot I have used, is the problem. Once again
thank you for the help.

Regards,
Gavin

Mar 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: glmlima | last post by:
I need to implement a prompt of commands (mere academic project). The function presents errors in the call execve, as parameter. It follows the code below. I´m using gcc... thanks. #include...
1
by: yogamatt1970 | last post by:
I have some code which runs on a number of different machines, however I see problems on one particular Solaris box. When I call Popen.wait(), the following exception is raised: .. . . File...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.