473,396 Members | 1,942 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,396 software developers and data experts.

minimum install & pickling

Sometimes questions come up on here about unpickling safely and
executing foreign code. I was thinking a minimum install that didn't
even have access to modules like 'os' could be safe. (Potentially.)
I have time to entertain this a little, though all the devs are busy.
I can bring it up again in a few months if it's a better time.

I browsed for info on 'rexec'. Two c-l-py threads:
http://mail.python.org/pipermail/pyt...er/031160.html
http://mail.python.org/pipermail/pyt...ry/031848.html

A lot of modules would have to go. <Long list IPC modules:
subprocess, socket, signal, popen2, asyncore, asynchat. ctypes, mmap,
platform.popen, glob, shutil, dircache, and many more</Long>.

I tested it out. I renamed the 'Lib' directory and ran.

'import site' failed; use -v for traceback
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>import os
ImportError: No module named os
>>import socket
ImportError: No module named socket
>>del __builtins__.__import__
__import__
NameError: name '__import__' is not defined
>>del __builtins__.open, __builtins__.file
open
NameError: name 'open' is not defined
>>file
NameError: name 'file' is not defined

Even a function created from raw bytecode string can't do anything
without __import__ or 'open'. And you can't get a second instance
running without subprocess or os.system.

'rexec' may be full of swiss cheese and irreparable, but maybe it
would work to start from bare-bones and add pieces known to be safe.
This sort of thing wouldn't need and standard library support either,
I don't think.
Sep 17 '08 #1
4 1316
Aaron "Castironpi" Brady wrote:
Even a function created from raw bytecode string can't do anything
without __import__ or 'open'.
Not true:

for cls in (1).__class__.__bases__[0].__subclasses__():
if cls.__name__ == "file":
F = cls

F(my_naughty_path, "w").write(my_naughty_data)

--
Greg
Sep 17 '08 #2
On Sep 17, 6:06*pm, greg <g...@cosc.canterbury.ac.nzwrote:
Aaron "Castironpi" Brady wrote:
Even a function created from raw bytecode string can't do anything
without __import__ or 'open'.

Not true:

* *for cls in (1).__class__.__bases__[0].__subclasses__():
* * *if cls.__name__ == "file":
* * * *F = cls

* *F(my_naughty_path, "w").write(my_naughty_data)

--
Greg
You're right, the list is a little longer. See above, where I renamed
the Lib/ folder.

'import site' failed; use -v for traceback
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>for cls in (1).__class__.__bases__[0].__subclasses__():
.... if cls.__name__ == "file":
.... F = cls
....
>>F
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'F' is not defined
>>>
'file' here is still defined.
>>file
<type 'file'>
>>del __builtins__.file
file
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined

This one stands a chance.
Sep 18 '08 #3
On 17 Sep, 22:18, "Aaron \"Castironpi\" Brady" <castiro...@gmail.com>
wrote:
On Sep 17, 4:43*am, Paul Boddie <p...@boddie.org.ukwrote:
http://wiki.python.org/moin/How_can_...Python_script_...)

These solutions have at least the same bugs that the bare bones
solution in the corresponding framework has. *Malicious code has fewer
options, but constructive code does too. *If you're running foreign
code, what do you want it to do? *What does it want to do? *The more
options it needs, the more code you have to trust.
As I noted, instead of just forbidding access to external resources,
what you'd want to do is to control access instead. This idea is not
exactly new: although Brett Cannon was working on a sandbox capability
for CPython, the underlying concepts involving different privilege
domains have been around since Safe-Tcl, if not longer. The advantage
of using various operating system features, potentially together with
tools like fakechroot or, I believe, Plash, is that they should work
for non-Python programs. Certainly, the chances of successfully
introducing people to such capabilities are increased if you don't
have to persuade the CPython core developers to incorporate your
changes into their code.
The only way a Python script can return a value is with sys.exit, and
only an integer at that. *It is going to have output; maybe there's a
way to place a maximum limit on its consumption. *It's going to have
input, so that the output is relative to something. *You just make
copies to prevent it from destroying data. *Maybe command-line
parameters are enough. *IIRC if I recall correctly, Win32 has a way to
examine how much time a process has owned so far, and a way to
terminate it, which could be in Python's future.
There is support for imposing limits on processes in the Python
standard library:

http://docs.python.org/lib/node521.html

My experimental package, jailtools, relies on each process's sandbox
being set up explicitly before the process is run, so you'd definitely
want to copy data into the sandbox. Setting limits on the amount of
data produced would probably require support from the operating
system. Generally, when looking into these kinds of systems, most of
the solutions ultimately come from the operating system: process
control, resource utilisation, access control, and so on. (This is the
amusing thing about Java: that Sun attempted to reproduce lots of
things that a decent operating system would provide *and* insist on
their use when deploying Java code in a controlled server environment,
despite actually having a decent operating system to offer already.)
PyPy sandbox says: *"The C code generated by PyPy is not
segfaultable." *I find that to be a bold claim (whether it's true or
not).

I'm imagining in the general case, you want the foreign code to make
changes to objects in your particular context, such as exec x in
vars. *In that case, x can still be productive without any libraries,
just less productive.
Defining an interface between trusted and untrusted code can be
awkward. When I looked into this kind of thing for my undergraduate
project, I ended up using something similar to CORBA, and my
conclusion was that trusted code would need to expose an interface
that untrusted "agents" would rely on to request operations outside
the sandbox. That seems restrictive, but as the situation with rexec
has shown, if you expose a broad interface to untrusted programs, it
becomes increasingly difficult to verify whether or not the solution
is actually secure.

Paul
Sep 18 '08 #4
On Sep 18, 5:20*am, Paul Boddie <p...@boddie.org.ukwrote:
On 17 Sep, 22:18, "Aaron \"Castironpi\" Brady" <castiro...@gmail.com>
wrote:
On Sep 17, 4:43*am, Paul Boddie <p...@boddie.org.ukwrote:
>http://wiki.python.org/moin/How_can_...Python_script_....)
These solutions have at least the same bugs that the bare bones
solution in the corresponding framework has. *Malicious code has fewer
options, but constructive code does too. *If you're running foreign
code, what do you want it to do? *What does it want to do? *The more
options it needs, the more code you have to trust.

As I noted, instead of just forbidding access to external resources,
what you'd want to do is to control access instead. This idea is not
exactly new: although Brett Cannon was working on a sandbox capability
for CPython, the underlying concepts involving different privilege
domains have been around since Safe-Tcl, if not longer. The advantage
of using various operating system features, potentially together with
tools like fakechroot or, I believe, Plash, is that they should work
for non-Python programs. Certainly, the chances of successfully
introducing people to such capabilities are increased if you don't
have to persuade the CPython core developers to incorporate your
changes into their code.
The only way a Python script can return a value is with sys.exit, and
only an integer at that. *It is going to have output; maybe there's a
way to place a maximum limit on its consumption. *It's going to have
input, so that the output is relative to something. *You just make
copies to prevent it from destroying data. *Maybe command-line
parameters are enough. *IIRC if I recall correctly, Win32 has a way to
examine how much time a process has owned so far, and a way to
terminate it, which could be in Python's future.

There is support for imposing limits on processes in the Python
standard library:

http://docs.python.org/lib/node521.html

My experimental package, jailtools, relies on each process's sandbox
being set up explicitly before the process is run, so you'd definitely
want to copy data into the sandbox. Setting limits on the amount of
data produced would probably require support from the operating
system. Generally, when looking into these kinds of systems, most of
the solutions ultimately come from the operating system: process
control, resource utilisation, access control, and so on. (This is the
amusing thing about Java: that Sun attempted to reproduce lots of
things that a decent operating system would provide *and* insist on
their use when deploying Java code in a controlled server environment,
despite actually having a decent operating system to offer already.)
PyPy sandbox says: *"The C code generated by PyPy is not
segfaultable." *I find that to be a bold claim (whether it's true or
not).
I'm imagining in the general case, you want the foreign code to make
changes to objects in your particular context, such as exec x in
vars. *In that case, x can still be productive without any libraries,
just less productive.

Defining an interface between trusted and untrusted code can be
awkward. When I looked into this kind of thing for my undergraduate
project, I ended up using something similar to CORBA, and my
conclusion was that trusted code would need to expose an interface
that untrusted "agents" would rely on to request operations outside
the sandbox. That seems restrictive, but as the situation with rexec
has shown, if you expose a broad interface to untrusted programs, it
becomes increasingly difficult to verify whether or not the solution
is actually secure.

Paul
I think you could autogenerate a file with a copy of the data, then
run a bare bones Python installation with the foreign code that
imports the copy, or just concatenate the foreign code and copy. At
least for input. For output, you'd need a file that had an upper
bound on its size.

The problem with Python is that if an agent has access to a part of an
object, it has the whole thing. Such as the trusted agents-- if they
can perform an operation, then anything with access to the agent can.
If they're just policy makers, then however an authorized agent
performs the action, is available to an unauthorized one. You'd still
need a 'struct' instance to write your output, since memory is upper-
bounded, and you can't permit foreign code to store any form of Python
objects.
Sep 18 '08 #5

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

Similar topics

1
by: Marc | last post by:
Hi all, After some research I've decided that my previous question (Confusing problem between Tkinter.Intvar...) was headed in the wrong direction. Partly because I think I have a greater...
1
by: Edward Loper | last post by:
I'm having trouble pickling subclasses of dict when they contain cycles. In particular: >>> import pickle >>> class D(dict): pass >>> d = D() >>> d = d # add a cycle. >>> print d {1: {...}}...
8
by: Hans Georg Krauthaeuser | last post by:
Dear all, I have a long running application (electromagnetic compatibility measurements in mode-stirred chambers over GPIB) that use pickle (cPickle) to autosave a class instance with all the...
1
by: A.B., Khalid | last post by:
I wonder if someone can explain what is wrong here. I am pickling a list of dictionaries (see code attached) and unpickling it back using the HIGHEST_PROTOCOL of pickle and cPickle. I am getting an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.