This wiki page suggests using a chroot jail to sandbox Python, but
wouldn't running something like this in your sandboxed Python instance
still break you out of the chroot jail:
os.execle ('/usr/bin/python','-c','import os; os.execlp("/bin/sh")',
{})
or maybe:
del os.environ['LD_PRELOAD']
os.execl ('/usr/bin/python','-c','import os; os.execlp("/bin/sh")')
My ISP suggested these as counter-examples to my request for a chroot
jail. (I couldn't even get Python running in chroot to test this, nor
could I run these commands locally in Python on Ubuntu, though maybe
they opened sh?)
So is a chroot jail not adequate for sandboxing Python?
-Greg 12 7127
On Jun 25, 1:21 am, "gregpin...@gmail.com" <gregpin...@gmail.com>
wrote:
This wiki page suggests using a chroot jail to sandbox Python, but
wouldn't running something like this in your sandboxed Python instance
still break you out of the chroot jail:
os.execle ('/usr/bin/python','-c','import os; os.execlp("/bin/sh")',
{})
or maybe:
del os.environ['LD_PRELOAD']
os.execl ('/usr/bin/python','-c','import os; os.execlp("/bin/sh")')
My ISP suggested these as counter-examples to my request for a chroot
jail. (I couldn't even get Python running in chroot to test this, nor
could I run these commands locally in Python on Ubuntu, though maybe
they opened sh?)
So is a chroot jail not adequate for sandboxing Python?
-Greg
Edit: Google groups stripped out the URL. It's http://wiki.python.org/moin/How_can_..._(i.e._Sandbox)
(or the page titled this on the Python wiki if it strips out the url
above again)
"How can I run an untrusted Python script safely (i.e. Sandbox)"
-Greg gr********@gmail.com schrieb:
This wiki page suggests using a chroot jail to sandbox Python, but
wouldn't running something like this in your sandboxed Python instance
still break you out of the chroot jail:
os.execle ('/usr/bin/python','-c','import os; os.execlp("/bin/sh")',
{})
Depending on how the chroot jail is set up, this command might not
work - in the jail, /bin/sh might not exist.
or maybe:
del os.environ['LD_PRELOAD']
os.execl ('/usr/bin/python','-c','import os; os.execlp("/bin/sh")')
How could deleting LD_PRELOAD help? chroot is not a library trick.
It's a mechanism implemented in the operating system.
So is a chroot jail not adequate for sandboxing Python?
You have to define your threat model. If the threat to prevent is
a malicious user getting at your data, or spreading a virus
through your files, then chroot is perfectly adequate.
Regards,
Martin
On Jun 25, 1:43 am, "Martin v. Löwis" <mar...@v.loewis.dewrote:
gregpin...@gmail.com schrieb:
This wiki page suggests using a chroot jail to sandbox Python, but
wouldn't running something like this in your sandboxed Python instance
still break you out of the chroot jail:
os.execle ('/usr/bin/python','-c','import os; os.execlp("/bin/sh")',
{})
Depending on how the chroot jail is set up, this command might not
work - in the jail, /bin/sh might not exist.
This was my thought too. I just figured there was something special
about this command that brought one to the "real" Python intrepreter
and then to the real "/bin/sh". That's odd, my ISP seem adament that
this is a way to break out. I'll just have to put in the work to test
to locally I guess.
So is a chroot jail not adequate for sandboxing Python?
You have to define your threat model. If the threat to prevent is
a malicious user getting at your data, or spreading a virus
through your files, then chroot is perfectly adequate.
Yeah, sounds like my threat model. Maybe prevent someone sending
spam, or DOS from my server too.
-Greg gr********@gmail.com <gr********@gmail.comwrote:
On Jun 25, 1:43 am, "Martin v. Löwis" <mar...@v.loewis.dewrote:
You have to define your threat model. If the threat to prevent is
a malicious user getting at your data, or spreading a virus
through your files, then chroot is perfectly adequate.
Yeah, sounds like my threat model. Maybe prevent someone sending
spam, or DOS from my server too.
It all depends on how much stuff you put in your chroot!
If you don't put bin/sh in the chroot then you'll stop a lot of
exploits from working.
If you don't allow the chrooted user write permission to the chroot
then you will anyone uploading a shell or any other binaries, just
leaving you with the binaries in the chroot to worry about.
Nothing stops someone running os.fork() from python and spawning
processes to do bad stuff. However in order to do that they would
have to have compromised your .py and sent some code to exec(), or
found a buffer overflow within python. It is getting increasingly
unlikely but not impossible.
Note that root can break out of a chroot, so your users must
not be root in the chroot.
Chroots provide a reasonable level of security. If you are truly
paranoid about security then you want to check out selinux (as made
originally by the NSA).
From the selinux FAQ :-
The Security-enhanced Linux kernel enforces mandatory access control
policies that confine user programs and system servers to the
minimum amount of privilege they require to do their jobs. When
confined in this way, the ability of these user programs and system
daemons to cause harm when compromised (via buffer overflows or
misconfigurations, for example) is reduced or eliminated.
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
On Jun 25, 1:43 am, "Martin v. Löwis" <mar...@v.loewis.dewrote:
gregpin...@gmail.com schrieb:
This wiki page suggests using a chroot jail to sandbox Python, but
wouldn't running something like this in your sandboxed Python instance
still break you out of the chroot jail:
os.execle ('/usr/bin/python','-c','import os; os.execlp("/bin/sh")',
{})
Depending on how the chroot jail is set up, this command might not
work - in the jail, /bin/sh might not exist.
I followed up with my ISP. Here's the answer I got:
The os.exec call prepends the chroot directory to the absolute path,
but does NOT provide chroot for the child process. However, as long
as the environment is maintained, which contains an LD_PRELOAD, the
"chroot" will also be maintained. If LD_PRELOAD is removed or
ignored, then the chroot is ineffective.
Another way of saying it is that every process is responsible for
providing and maintaining the chroot through the LD_PRELOAD variable.
Those processes only maintain the chroot if that variable remains set.
The only solution that would bypass this problem altogether would be a
statically linked python. (is that possible?) It would have to be
statically linked to a custom-modified glibc to provide the virtual
chroot environment.
-Greg gr********@gmail.com wrote:
On Jun 25, 1:43 am, "Martin v. Löwis" <mar...@v.loewis.dewrote:
>gregpin...@gmail.com schrieb:
>>This wiki page suggests using a chroot jail to sandbox Python, but wouldn't running something like this in your sandboxed Python instance still break you out of the chroot jail: os.execle ('/usr/bin/python','-c','import os; os.execlp("/bin/sh")', {})
Depending on how the chroot jail is set up, this command might not work - in the jail, /bin/sh might not exist.
I followed up with my ISP. Here's the answer I got:
The os.exec call prepends the chroot directory to the absolute path,
but does NOT provide chroot for the child process. However, as long
as the environment is maintained, which contains an LD_PRELOAD, the
"chroot" will also be maintained. If LD_PRELOAD is removed or
ignored, then the chroot is ineffective.
Another way of saying it is that every process is responsible for
providing and maintaining the chroot through the LD_PRELOAD variable.
Those processes only maintain the chroot if that variable remains set.
The only solution that would bypass this problem altogether would be a
statically linked python. (is that possible?) It would have to be
statically linked to a custom-modified glibc to provide the virtual
chroot environment.
It seems to me that if a (potentially malicious) process needs to behave
itself in order for chroot to not allow the process access to the full
system, then chroot is broken. But perhaps I don't understand the
intent and scale of what chroot intends to do.
- Josiah
On 2007-06-25, gr********@gmail.com <gr********@gmail.comwrote:
On Jun 25, 1:43 am, "Martin v. Löwis" <mar...@v.loewis.dewrote:
>gregpin...@gmail.com schrieb:
This wiki page suggests using a chroot jail to sandbox Python, but
wouldn't running something like this in your sandboxed Python instance
still break you out of the chroot jail:
os.execle ('/usr/bin/python','-c','import os; os.execlp("/bin/sh")',
{})
Depending on how the chroot jail is set up, this command might not work - in the jail, /bin/sh might not exist.
I followed up with my ISP. Here's the answer I got:
The os.exec call prepends the chroot directory to the absolute path,
but does NOT provide chroot for the child process. However, as long
as the environment is maintained, which contains an LD_PRELOAD, the
"chroot" will also be maintained. If LD_PRELOAD is removed or
ignored, then the chroot is ineffective.
Another way of saying it is that every process is responsible for
providing and maintaining the chroot through the LD_PRELOAD variable.
Those processes only maintain the chroot if that variable remains set.
None of this makes any sense to me. Once an application is running
inside a chroot, there is no easy manipulation the application
can do "to break out". The example you gave above executes a shell that
is in the chroot dir. That's not really breaking out of the sandbox,
it's just accessing something inside the sandbox.
if your ISP is trying to enforce chroot through an LD_PRELOAD library,
they might be using 'fakechroot' which doesn't look very good to me.
Dave
On 25 Jun, 16:48, "gregpin...@gmail.com" <gregpin...@gmail.comwrote:
>
I followed up with my ISP. Here's the answer I got:
The os.exec call prepends the chroot directory to the absolute path,
but does NOT provide chroot for the child process. However, as long
as the environment is maintained, which contains an LD_PRELOAD, the
"chroot" will also be maintained. If LD_PRELOAD is removed or
ignored, then the chroot is ineffective.
So it appears that as long as LD_PRELOAD is set (possibly to link the
process to some other libraries than is usually the case), any
affected processes are effectively jailed. This doesn't really sound
like a traditional chroot environment, though.
Another way of saying it is that every process is responsible for
providing and maintaining the chroot through the LD_PRELOAD variable.
Those processes only maintain the chroot if that variable remains set.
Right.
The only solution that would bypass this problem altogether would be a
statically linked python. (is that possible?) It would have to be
statically linked to a custom-modified glibc to provide the virtual
chroot environment.
Some solutions depend on linking to restricted libraries, and the Wiki
page you referenced probably talks about them as well. I was thinking
that if I were to attempt to properly sandbox any current version of
CPython, I'd start off linking it to restricted libraries which
provide compatible interfaces for things like opening file handles;
then I'd put various policy controls in those libraries so that you
can have some control over what your programs do. Finally, you'd have
to stop arbitrary (extension) module loading in order to prevent
programs importing some nice modules and getting round the controls:
for example, importing socket or os to get access to file handles (or
to process creation which might get around the controls as suggested
above). Eventually, this arrives more or less at where Brett Cannon is
supposed to be right now with his sandboxed Python, perhaps by a
different route and with some different outcomes.
I notice that you've mailed me about a solution that I mentioned in
the past, but I'll respond here in order to air the ideas in public. I
looked into chroot "jails" and saw that some solutions exist for
populating directories with enough files for things like daemons or
services to be executed within the chroot environment. I also looked
at ways to break out of chroot environments, and there's a fairly well-
known trick involving open file handles which will do this effectively
for non-root users. What I then considered was the possibility of
avoiding population of a chroot filesystem by calling chroot and
setuid on a minimal "jailer" process which then loads a jailed program
after having imported a permitted set of modules.
I don't have the details with me now, but I'll probably upload the
code in the near future and post some kind of explanation of what it
does here. I'm tempted to say that the better solution involves the
restricted libraries solution mentioned above, and a nice side-effect
of that could be a more modular CPython that would benefit people
using the software in embedded environments: you'd have to insulate
the virtual machine from even the most central modules, meaning that
they could potentially be detached completely in places where memory
and storage are better used on other things.
Paul
This was my thought too. I just figured there was something special
about this command that brought one to the "real" Python intrepreter
and then to the real "/bin/sh". That's odd, my ISP seem adament that
this is a way to break out. I'll just have to put in the work to test
to locally I guess.
No. The jail is protected by the operating system - even if a
perpetrator would manage to run arbitrary native code (e.g.
through running a compiler) in the jail, they *still* could
not access any data outside the chroot.
There are very few way of getting out of the jail. One is
to manage creating a device node for, say /dev/hda, and then
mounting hard disk into the jail; others are listed at http://www.unixwiz.net/techtips/chroot-practices.html
In all cases, you need root privileges, so if the chrooted
process does not run as root, it is safe. Be careful
not to put any s-bit programs into the jail.
>>So is a chroot jail not adequate for sandboxing Python?
You have to define your threat model. If the threat to prevent is a malicious user getting at your data, or spreading a virus through your files, then chroot is perfectly adequate.
Yeah, sounds like my threat model. Maybe prevent someone sending
spam, or DOS from my server too.
It cannot help you prevent the sending of spam or DOS: it *only*
affects the local hard disk (that's why it's ch*root* - as in
"root directory"). The "jailed" process can perform all networking
that a similarly-privileged process outside the jail could.
To prevent network access, you would need additional system
features, as the ones provided by SELinux.
Regards,
Martin
The os.exec call prepends the chroot directory to the absolute path,
but does NOT provide chroot for the child process. However, as long
as the environment is maintained, which contains an LD_PRELOAD, the
"chroot" will also be maintained. If LD_PRELOAD is removed or
ignored, then the chroot is ineffective.
As others have mentioned (which I just repeat for additional
support): Your ISP is probably thinking of fakeroot, which
is entirely unlike chroot(2), with the latter being a proper
kernel mechanism, not dynamic library trickery (which would
indeed be easy to break out of).
Regards,
Martin
On Jun 25, 4:12 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
gregpin...@gmail.com wrote:
I followed up with my ISP. Here's the answer I got:
The os.exec call prepends the chroot directory to the absolute
path, but does NOT provide chroot for the child process.
That sounds like rubbish to me. If it worked like that, chrooting
servers would be virtually useless.
You're right. It turns out he was referring to fakechroot. Chroot
shouldn't have this problem.
-Greg
To launch a child process in a chroot you can easily just fork and
then make the chroot syscall in the child process immediately after
the fork.
It's not so easy. On Linux, you need to have the CAP_SYS_CHROOT
capability to invoke the syscall; on other systems, you may have
to be root.
Regards,
Martin This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andy |
last post by:
On my webserver the web components are seperated into a chroot jail so
it cannot access any of the mail (courier-mta) components. Does anyone
know...
|
by: SuicidalLabRat |
last post by:
Is it Possable to build a chroot() like function using
the abID and ITEMIDLIST structures, wherein the context
in which a process runs ( this...
|
by: alchimista |
last post by:
hi, I've succesfully installed mysql on linux 2.4.x (TRUSTIX), I've
tried to move it on my chroot jail but after 10s it crashes with the
following...
|
by: Bill Moran |
last post by:
I'm having some problems. Hopefully there are some FreeBSD folks here
that can help me out, if not, I'll try the FreeBSD lists next.
I'm running...
|
by: Vikram |
last post by:
Hi,
I am using postgresql 8.0beta in a freebsd jail environment. My inittdb
gives me a message like:
---
creating template1 database in...
|
by: goodnamesalltaken |
last post by:
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...
|
by: Fredrik Tolf |
last post by:
Hi List!
I was thinking about secure Python code execution, and I'd really
appreciate some comments from those who know Python better than I do....
|
by: Þ¾¯ |
last post by:
/************************************************** ***
*** chrexec.c ***
*This shit can be called from root or from any user (in that case...
|
by: support\.intranet |
last post by:
Hello! I'm writing a small script and I need to call the os.chroot function. The problem is, a few lines below I need to call a program in /usr/bin....
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
| |