use of exec()

L

lars van gemerden

I am trying to implement a way to let users give a limited possibility to define functions in text, that wille be stored and executed at a later time. I use exec() to transform the text to a function. The code is as follows:

class code(str):
def __call__(self, *args):
try:
return self._func_(*args)
except AttributeError:
self._func_ = self._creat_func_()
return self._func_(*args)
def __getstate__(self):
return {}

class _lambdacode(code):
def _creat_func_(self):
return eval("lambda %s: %s" % (", ".join(type(self).args), self),{},{})

class _functioncode(code):
def _creat_func_(self):
exec("def function(%s):\n\t%s" % (", ".join(type(self).args),
"\n\t".join(self.split('\n'))))
return function

def lambdatype(*argnames):
return type('lambdacode', (_lambdacode,),{'args': argnames})

def functiontype(*argnames):
return type('functioncode', (_functioncode,),{'args': argnames})

if __name__ == '__main__':
f = lambdatype('a', 'b')('a ** b')
print f
print f(3, 4)
print f(4, 3)

g = functiontype('a', 'b')('a, b = a/b, a*b\nreturn a ** b')
print g
print g(3.0, 4.0)
print g(4.0, 3.0)

This code works. But if I replace _functioncode with:

class _functioncode(code):
def _creat_func_(self):
exec("def function(%s):\n\t%s" % (", ".join(type(self).args),
"\n\t".join(self.split('\n'))),{})
return function

or

class _functioncode(code):
def _creat_func_(self):
exec("def function(%s):\n\t%s" % (", ".join(type(self).args),
"\n\t".join(self.split('\n'))),{},{})
return function

to restrict access to global variables, similar to the lambda version, i get the error:

Traceback (most recent call last):
File "D:\Documents\Code\Eclipse\workspace\FlowWare_1\toolshed\tests\mini_test.py", line 41, in <module>
print g(3.0, 4.0)
File "D:\Documents\Code\Eclipse\workspace\FlowWare_1\toolshed\tests\mini_test.py", line 13, in __call__
self._func_ = self._creat_func_()
File "D:\Documents\Code\Eclipse\workspace\FlowWare_1\toolshed\tests\mini_test.py", line 25, in _creat_func_
return function
NameError: name 'function' is not defined

which seems an odd error, but i think some global variable is necessary for this to work (if i put in globals() instead of {}, it works).

My question is which variable or if that is not the problem, what is and how can i restrict access the user code has.

Cheers, Lars
 
C

Chris Angelico

NameError: name 'function' is not defined

which seems an odd error, but i think some global variable is necessary for this to work (if i put in globals() instead of {}, it works).

The def statement simply adds a name to the current namespace. This
should work (untested):

class _functioncode(code):
def _creat_func_(self):
ns={}
exec("def function(%s):\n\t%s" % (", ".join(type(self).args),
"\n\t".join(self.split('\n'))),ns,ns)
return ns.function

But it's going to be eternally plagued by security issues. You may
want, instead, to look at literal_eval from the ast module; but that
won't work if you need anything other than, as the name suggests,
literals.

ChrisA
 
L

lars van gemerden

The def statement simply adds a name to the current namespace. This

should work (untested):



class _functioncode(code):

def _creat_func_(self):

ns={}

exec("def function(%s):\n\t%s" % (", ".join(type(self).args),

"\n\t".join(self.split('\n'))),ns,ns)

return ns.function



But it's going to be eternally plagued by security issues. You may

want, instead, to look at literal_eval from the ast module; but that

won't work if you need anything other than, as the name suggests,

literals.



ChrisA

Thanks, Chris,

That works like a charm (after replacig "return ns.function" with "return ns['function']" ;-) ).

About the security, i noticed you can still import and use modules within the exec'ed code. Is there a way to prevent this or otherwise make this approach more secure.

I should say that the users that will be able to make custom functions, are not end-users, but authenticated designers, however i would like to close a backdoor to the whole framework.

Cheers, Lars
 
L

lars van gemerden

The def statement simply adds a name to the current namespace. This

should work (untested):



class _functioncode(code):

def _creat_func_(self):

ns={}

exec("def function(%s):\n\t%s" % (", ".join(type(self).args),

"\n\t".join(self.split('\n'))),ns,ns)

return ns.function



But it's going to be eternally plagued by security issues. You may

want, instead, to look at literal_eval from the ast module; but that

won't work if you need anything other than, as the name suggests,

literals.



ChrisA

Thanks, Chris,

That works like a charm (after replacig "return ns.function" with "return ns['function']" ;-) ).

About the security, i noticed you can still import and use modules within the exec'ed code. Is there a way to prevent this or otherwise make this approach more secure.

I should say that the users that will be able to make custom functions, are not end-users, but authenticated designers, however i would like to close a backdoor to the whole framework.

Cheers, Lars
 
C

Chris Angelico

Thanks, Chris,

That works like a charm (after replacig "return ns.function" with "return ns['function']" ;-) ).

Err, yes, I forget sometimes that Python doesn't do that. JavaScript
and Pike both let you (though Pike uses -> instead of . for that
operator). Yes, Python has real methods on dictionary objects :)
About the security, i noticed you can still import and use modules within the exec'ed code. Is there a way to prevent this or otherwise make this approach more secure.

Basically no, there's no real way to make it secure. Without
eliminating exec/eval, destroying insecurity is the hopeless work of a
wasted life, as the oracle said to Alice.
I should say that the users that will be able to make custom functions, are not end-users, but authenticated designers, however i would like to close a backdoor to the whole framework.

You have to decide one thing: Will you permit them to execute
untrusted code on your system? If so, go ahead (and just warn them
that things like import shouldn't be done, as they can cause other
messes). I run a server that I build with the help of another guy (I
do the code, he does the bulk of the content - descriptions and
stuff), and I'm happy to trust him to not be malicious, so the purpose
of "embedded code in loci" is to make it easier to write tiny bits of
code, without any security requirement. But if you need security,
don't use eval. AT ALL.

There may be a brand new service coming along, though. The ast module
I think is getting a new evaluator that allows a little more
functionality than literal_eval, while still not permitting most
things. But you then have the question of performance, since you
effectively interpret the code at a high level.

ChrisA
 
L

lars van gemerden

Thanks, Chris,
That works like a charm (after replacig "return ns.function" with "return ns['function']" ;-) ).



Err, yes, I forget sometimes that Python doesn't do that. JavaScript

and Pike both let you (though Pike uses -> instead of . for that

operator). Yes, Python has real methods on dictionary objects :)


About the security, i noticed you can still import and use modules within the exec'ed code. Is there a way to prevent this or otherwise make this approach more secure.



Basically no, there's no real way to make it secure. Without

eliminating exec/eval, destroying insecurity is the hopeless work of a

wasted life, as the oracle said to Alice.


I should say that the users that will be able to make custom functions,are not end-users, but authenticated designers, however i would like to close a backdoor to the whole framework.



You have to decide one thing: Will you permit them to execute

untrusted code on your system? If so, go ahead (and just warn them

that things like import shouldn't be done, as they can cause other

messes). I run a server that I build with the help of another guy (I

do the code, he does the bulk of the content - descriptions and

stuff), and I'm happy to trust him to not be malicious, so the purpose

of "embedded code in loci" is to make it easier to write tiny bits of

code, without any security requirement. But if you need security,

don't use eval. AT ALL.



There may be a brand new service coming along, though. The ast module

I think is getting a new evaluator that allows a little more

functionality than literal_eval, while still not permitting most

things. But you then have the question of performance, since you

effectively interpret the code at a high level.



ChrisA

I get your point, since in this case having the custom code option makes the system a whole lot less complex and flexible, i will leave the option in.The future customer will be informed that they should handle the security around the designers as if they were programmers. Aditionally i will probably add some screening for unwanted keywords (like 'import') and securely log any new/changed custom code including the designer account (must do that for other actions anyway).

Thanks again, Lars
 
L

lars van gemerden

Thanks, Chris,
That works like a charm (after replacig "return ns.function" with "return ns['function']" ;-) ).



Err, yes, I forget sometimes that Python doesn't do that. JavaScript

and Pike both let you (though Pike uses -> instead of . for that

operator). Yes, Python has real methods on dictionary objects :)


About the security, i noticed you can still import and use modules within the exec'ed code. Is there a way to prevent this or otherwise make this approach more secure.



Basically no, there's no real way to make it secure. Without

eliminating exec/eval, destroying insecurity is the hopeless work of a

wasted life, as the oracle said to Alice.


I should say that the users that will be able to make custom functions,are not end-users, but authenticated designers, however i would like to close a backdoor to the whole framework.



You have to decide one thing: Will you permit them to execute

untrusted code on your system? If so, go ahead (and just warn them

that things like import shouldn't be done, as they can cause other

messes). I run a server that I build with the help of another guy (I

do the code, he does the bulk of the content - descriptions and

stuff), and I'm happy to trust him to not be malicious, so the purpose

of "embedded code in loci" is to make it easier to write tiny bits of

code, without any security requirement. But if you need security,

don't use eval. AT ALL.



There may be a brand new service coming along, though. The ast module

I think is getting a new evaluator that allows a little more

functionality than literal_eval, while still not permitting most

things. But you then have the question of performance, since you

effectively interpret the code at a high level.



ChrisA

I get your point, since in this case having the custom code option makes the system a whole lot less complex and flexible, i will leave the option in.The future customer will be informed that they should handle the security around the designers as if they were programmers. Aditionally i will probably add some screening for unwanted keywords (like 'import') and securely log any new/changed custom code including the designer account (must do that for other actions anyway).

Thanks again, Lars
 
C

Chris Angelico

I get your point, since in this case having the custom code option makes the system a whole lot less complex and flexible, i will leave the option in. The future customer will be informed that they should handle the security around the designers as if they were programmers. Aditionally i will probably add some screening for unwanted keywords (like 'import') and securely log any new/changed custom code including the designer account (must do that for other actions anyway).

That sounds like a reasonable implementation of Layer Eight security.
As long as everyone understands that this code can do ANYTHING, you'll
be fine.

You may want to add some other programmatic checks, though; for
instance, a watchdog timer in case the code gets stuck in an infinite
loop, or a memory usage limit, or somesuch. Since you're no longer
worrying about security, this sort of thing will be fairly easy, and
will be just to help catch common errors.

ChrisA
 
L

lars van gemerden

That sounds like a reasonable implementation of Layer Eight security.

As long as everyone understands that this code can do ANYTHING, you'll

be fine.



You may want to add some other programmatic checks, though; for

instance, a watchdog timer in case the code gets stuck in an infinite

loop, or a memory usage limit, or somesuch. Since you're no longer

worrying about security, this sort of thing will be fairly easy, and

will be just to help catch common errors.



ChrisA

Do you have any ideas about to what extend the "lambda" version of the code(custom code is only the 'body' of the lambda function) has the same issues?

Cheers, Lars
 
L

lars van gemerden

That sounds like a reasonable implementation of Layer Eight security.

As long as everyone understands that this code can do ANYTHING, you'll

be fine.



You may want to add some other programmatic checks, though; for

instance, a watchdog timer in case the code gets stuck in an infinite

loop, or a memory usage limit, or somesuch. Since you're no longer

worrying about security, this sort of thing will be fairly easy, and

will be just to help catch common errors.



ChrisA

Do you have any ideas about to what extend the "lambda" version of the code(custom code is only the 'body' of the lambda function) has the same issues?

Cheers, Lars
 
C

Chris Angelico

Do you have any ideas about to what extend the "lambda" version of the code (custom code is only the 'body' of the lambda function) has the same issues?

The lambda version definitely has the same issues. You can do pretty
much anything with a single expression.

ChrisA
 
L

lars van gemerden

The lambda version definitely has the same issues. You can do pretty

much anything with a single expression.



ChrisA

Thanks a lot Chris, I will return later to the watchdog/memory limit ideas (no idea how to do that yet).
 
L

lars van gemerden

The lambda version definitely has the same issues. You can do pretty

much anything with a single expression.



ChrisA

Thanks a lot Chris, I will return later to the watchdog/memory limit ideas (no idea how to do that yet).
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top