Now that rexec is gone... | |
Now that rexec is gone, is there any code or information available on
executing Python in a restricted environment? And before I roll my own
solution, exactly where the security holes in rexec anyway?
(I know one way of getting a restricted environment: butcher the Python
interpreter by removing everything that's even remotely dangerous, use
Python only for restricted execution, and do everything else in a C++
program that embeds the butchered Python interpreter. I'd like to avoid
doing that, for obvious reasons.)
--
Rainer Deyke - rainerd@eldwood.com - http://eldwood.com | | | | re: Now that rexec is gone...
"Rainer Deyke" <rainerd@eldwood.com> wrote in message
news:XA8db.597231$o%2.276974@sccrnsc02...[color=blue]
> Now that rexec is gone, is there any code or information available[/color]
on[color=blue]
> executing Python in a restricted environment? And before I roll my[/color]
own[color=blue]
> solution, exactly where the security holes in rexec anyway?[/color]
Suggest you google last year of c.l.py for 'rexec'. Also check out
py-dev summaries of last fall for discussion of why removed.
TJR | | | | re: Now that rexec is gone...
"Rainer Deyke" <rainerd@eldwood.com> writes:
[color=blue]
> Now that rexec is gone, is there any code or information available on
> executing Python in a restricted environment?[/color]
There was a thread on python-dev about Zope's version of rexec
(RestrictedPython?) which looked promising on casual inspection.
Cheers,
mwh
--[color=blue]
> It might get my attention if you'd spin around in your chair,
> spoke in tongues, and puked jets of green goblin goo.[/color]
I can arrange for this. ;-) -- Barry Warsaw & Fred Drake | | | | re: Now that rexec is gone...
Rainer Deyke wrote:
[color=blue]
> Now that rexec is gone, is there any code or information available on
> executing Python in a restricted environment? And before I roll my own
> solution, exactly where the security holes in rexec anyway?
>
> (I know one way of getting a restricted environment: butcher the Python
> interpreter by removing everything that's even remotely dangerous, use
> Python only for restricted execution, and do everything else in a C++
> program that embeds the butchered Python interpreter. I'd like to avoid
> doing that, for obvious reasons.)[/color]
Actually, such a "butchered" Python interpreter might be a fun and
useful project indeed. You would have to add programmable limits on
resource consumptions -- e.g., memory allocatable by the script[s],
time (CPU or maybe elapsed) usable thereby, etc. And you should rename
everything, say to use Qy instead of Py, so that a normal and a
butchered interpreter could easily be embedded in the same program.
Once the hard work of "butchering" is done, you might in fact quite
easily expose "the butchered interpreter" via an extension module for
Python proper -- no need to do "everything in C++", you'd just have two
separate Pythons, a full-function one and a seriously-hobbled one.
Not *QUITE* as good as running untrusted code in a separate "jail"'d
process, perhaps, but probably the closest you can come to that on
such environments as Windows. Note that the need to add resource
limitations is crucial (and was never addressed by rexec, making it
pretty useless to ward against denial-of-service kinds of attacks!).
Alex | | | | re: Now that rexec is gone...
Alex Martelli wrote:[color=blue]
> Actually, such a "butchered" Python interpreter might be a fun and
> useful project indeed. You would have to add programmable limits on
> resource consumptions -- e.g., memory allocatable by the script[s],
> time (CPU or maybe elapsed) usable thereby, etc. And you should
> rename everything, say to use Qy instead of Py, so that a normal and a
> butchered interpreter could easily be embedded in the same program.[/color]
That might be a useful project, but it also sounds like a lot of work. I
don't think I'll be going that route.
As it turns out, I can solve my security problem in a different way
entirely: by confirming that any Python code I run is from a trusted source.
No need to run untrusted code at all.
--
Rainer Deyke - rainerd@eldwood.com - http://eldwood.com | | | | re: Now that rexec is gone...
Terry Reedy wrote:[color=blue]
> Suggest you google last year of c.l.py for 'rexec'. Also check out
> py-dev summaries of last fall for discussion of why removed.[/color]
I see... It turns out that, short of modifying the Python interpreter, there
is no way to get real security. '"".__class__' gives access to 'str', 'str'
gives access to 'object', and 'object' gives access to 'file'.
--
Rainer Deyke - rainerd@eldwood.com - http://eldwood.com | | | | re: Now that rexec is gone...
Rainer Deyke wrote:
[color=blue]
> As it turns out, I can solve my security problem in a different way
> entirely: by confirming that any Python code I run is from a trusted
> source.
> No need to run untrusted code at all.[/color]
Yes, that sounds like the most reasonable approach.
--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ In principle I am against principles.
\__/ Tristan Tzara | | | | re: Now that rexec is gone...
In article <Ihidb.130710$hE5.4447927@news1.tin.it>,
Alex Martelli <aleax@aleax.it> wrote: | | | | re: Now that rexec is gone...
"Rainer Deyke" <rainerd@eldwood.com> writes:
[color=blue]
> Terry Reedy wrote:[color=green]
> > Suggest you google last year of c.l.py for 'rexec'. Also check out
> > py-dev summaries of last fall for discussion of why removed.[/color]
>
> I see... It turns out that, short of modifying the Python interpreter, there
> is no way to get real security. '"".__class__' gives access to 'str', 'str'
> gives access to 'object', and 'object' gives access to 'file'.[/color]
A message-id, or similar, at this point, could save future googlers a
lot of time ... | | | | re: Now that rexec is gone...
Rob Hunter wrote:[color=blue]
> How do I check if a value is a number in Python?
>
> One way is (x == type(1)) and (x == type(1.2)) and (x ==
> type(2387482734274)) and ...
>
> but this seems kludgy. Any better way?[/color]
Why do you want to do so? Maybe, it is better in your
case to just run the piece of code using the number, and
if it fails, it fails. However, if you must, you need to
do type(x) is type(1) and ... etc., or isinstance(x, int)
and isinstance(x, float), etc.
Gerrit.
--
Asperger Syndroom - een persoonlijke benadering: http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien: http://www.sp.nl/ | | | | re: Now that rexec is gone...
First, whatever test you use, you should probably encapsulate it in a
function, so that if you need to update the definition you can do it at
one site instead of many:
def isnumeric(x):
return isinstance(x, (int, long, float))
You could have a registry of numeric types:
_numeric_types = ()
def register_numeric_type(t):
global _numeric_types
if t in _numeric_types: return
_numeric_types += (t,)
for value in (0, 0., 0l, 0j):
register_numeric_type(type(value))
def isnumeric(x):
return isinstance(x, _numeric_types)
Now, if someone wants to write a vector type, it merely needs to be
registered.
You could test that common numeric operations work:
def isnumeric(x):
try:
if x*1 == x and x+0 == x:
return 1
except TypeError:
pass
return 0
You could just run your code and let the eventual TypeError speak for
itself.. instead of
def f(x):
if not isnumeric(x): raise TypeError, "can't f() a %s" % type(x)
return x*x
just write
def f2(x):
return x*x
The difference in the quality of the error message is not large:[color=blue][color=green][color=darkred]
>>> f("")[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f
TypeError: can't f() a <type 'str'>[color=blue][color=green][color=darkred]
>>> f2("")[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f2
TypeError: unsupported operand type(s) for *: 'str' and 'str'
Jeff | | | | re: Now that rexec is gone...
> > How do I check if a value is a number in Python?[color=blue][color=green]
> >
> > One way is (x == type(1)) and (x == type(1.2)) and (x ==
> > type(2387482734274)) and ...[/color]
>
> Why do you want to do so? Maybe, it is better in your
> case to just run the piece of code using the number, and
> if it fails, it fails. However, if you must, you need to
> do type(x) is type(1) and ... etc., or isinstance(x, int)
> and isinstance(x, float), etc.[/color]
I used to use the latter approach suggested by Gerrit, but I recently
found on the web an alternative, elegant approach that might work
(sorry, I don't recall where I found it!):
hasattr(x, '__int__')
If the "__int__" method is defined for "x", it is a number. This will
work for integer, long, float and complex types, as well as for custom
classes that emulate numeric types.
Regards,
JSeb | | | | re: Now that rexec is gone...
Jean-S?bastien Bolduc wrote:
[color=blue]
> I used to use the latter approach suggested by Gerrit, but I recently
> found on the web an alternative, elegant approach that might work
> (sorry, I don't recall where I found it!):
>
> hasattr(x, '__int__')
>
> If the "__int__" method is defined for "x", it is a number. This will
> work for integer, long, float and complex types, as well as for custom
> classes that emulate numeric types.[/color]
This is an insidiously bad idea, in my opinion. All having an __int__
method means is there is some _conversion_ from an instance to an int
type. It does not at all mean the custom instance spends most of its
life behaving as an integer.
--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ We grow in time to trust the future for our answers.
\__/ Ruth Benedict | | | | re: Now that rexec is gone...
In article <3F79D3B4.DBE550CC@alcyone.com>,
Erik Max Francis <max@alcyone.com> wrote:[color=blue]
>Jean-S?bastien Bolduc wrote:[color=green]
>>
>> If the "__int__" method is defined for "x", it is a number. This will
>> work for integer, long, float and complex types, as well as for custom
>> classes that emulate numeric types.[/color]
>
>This is an insidiously bad idea, in my opinion. All having an __int__
>method means is there is some _conversion_ from an instance to an int
>type. It does not at all mean the custom instance spends most of its
>life behaving as an integer.[/color]
Yup. There's been some talk of adding an __index___() method or
something to deal with that.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|