sudo open() ? (python newbee question)

S

slava

hello,

i am writing a python script that will be run by a non root user
the script needs to open a file in write mode that is owned by root

file = open('/etc/apt/sources.list', 'r+')

returns permission error

how can i call sudo on open()?

thanks alot
slava
 
D

Denis WERNERT

The script could be SUID Root, and you could use os.setuid immediately after
having performed the task to switch to a non-priviledged user. May be a big
security risk, if someone can alter the script, he gains root access to the
system...
 
D

Dan Sommers

The script could be SUID Root, and you could use os.setuid immediately
after having performed the task to switch to a non-priviledged
user. May be a big security risk, if someone can alter the script, he
gains root access to the system...

I am *not* advocating suid scripts, and *ESPECIALLY NOT* suid Python
programs, but if a user can modify an unwriteable suid script owned by
root in a an unwriteable directory, then they already have root access
to the system (unless there's' a kernel or filesystem bug, in which case
all bets are off anyway).

Regards,
Dan
 
C

Christos TZOTZIOY Georgiou

I am *not* advocating suid scripts, and *ESPECIALLY NOT* suid Python
programs, but if a user can modify an unwriteable suid script owned by
root in a an unwriteable directory, then they already have root access
to the system (unless there's' a kernel or filesystem bug, in which case
all bets are off anyway).

I believe that the suid bit on scripts (either *sh or python) is
completely ignored on most *nix systems.

Try this in a shell (bash or ksh) as a sudo-capable user:

echo hello >/tmp/tmp
sudo chown root /tmp/tmp
sudo chmod 600 /tmp/tmp
cat >/tmp/ax.py <<@
#!/usr/bin/env python
x = open("/tmp/tmp", "w")
x.write("there")
x.close()
@
sudo chown root /tmp/ax.py
sudo chmod a=rx,u+s /tmp/ax.py
ls -l /tmp/ax.py /tmp/tmp
/tmp/ax.py

I get:

-r-sr-xr-x 1 root users 75 2005-06-14 16:15 /tmp/ax.py
-rw------- 1 root users 6 2005-06-14 16:15 /tmp/tmp
Traceback (most recent call last):
File "/tmp/ax.py", line 2, in ?
x = open("/tmp/tmp", "w")
IOError: [Errno 13] Permission denied: '/tmp/tmp'
 
D

Dan Sommers

I believe that the suid bit on scripts (either *sh or python) is
completely ignored on most *nix systems.

Most *modern* systems, yes. ;-)

I must be getting old. :-(

Regards,
Dan
 
D

Dennis Lee Bieber

hello,

i am writing a python script that will be run by a non root user
the script needs to open a file in write mode that is owned by root

file = open('/etc/apt/sources.list', 'r+')

returns permission error

how can i call sudo on open()?

Don't think you can -- you'd have to invoke the /script/ using
sudo, I believe (haven't done this is quite some time; the only thing I
ran sudo on my Linux install was leafnode functions).

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top