Restricted Access

I

iapain

I'm developing a webIDE for python and I've 2 questions regarding it.

1. How can i disable some of the modules without deleting. e.g I wish
to disable "os" module.
2. How can i force user code to access only his particular folder, I
dont want to create uses in unix, e.g

fp = open(PATH, 'w') # If this PATH is defined then use can access
files else he cant .. is there is any way?

Regards!
iapain
 
T

Tim Chase

1. How can i disable some of the modules without deleting. e.g I wish
to disable "os" module.

If you're prepared for the massive breakage that will ensue, you can

chmod go-rwx /usr/lib/python2.3/os.*

(assuming *nix as you later detail).
2. How can i force user code to access only his particular folder, I
dont want to create uses in unix, e.g

Well, you can create a chroot jail for each user that contains a
clone of your /usr/{lib/python2.3/,bin/,usr/bin/} directories.
You'd have to include any other executables that the user would
need (important stuff like ls, cp, mv, mkdir, rmdir,
cvs/ci/co/rcs/svn, etc) This would ensure that each user doesn't
access anything that you haven't explicitly copied in to their
jail. Another alternative might just be to copy the python
libraries to some place in the user's homedir (whatever their
original library path was), revoke execute non-user execute privs
from the python executable ("chmod go-x `which python`), and then
change python to be a script that runs something like "chroot
$HOME/ python $@". Allow per-user access to this script via sudo.

Just a couple ideas you might try.

-tkc
 
I

iapain

Tim said:
If you're prepared for the massive breakage that will ensue, you can

chmod go-rwx /usr/lib/python2.3/os.*

No, I cant change permission or delete the module, the best would be
something to detect 'import os' in user code .. but If i go with chroot
jail approch then everything will be like what i want. But chroot jail
approch would take much space on webserver, what would happen if number
of users are large.
Another alternative might just be to copy the python
libraries to some place in the user's homedir (whatever their
original library path was), revoke execute non-user execute privs
from the python executable ("chmod go-x `which python`), and then
change python to be a script that runs something like "chroot
$HOME/ python $@". Allow per-user access to this script via sudo.

Its having the same problem. The idea on which i am working is a
webide(which i already created) and a user file system(on which i am
working now) so that each user can access python globally and files
from his own folder, without adding them in unix user list.

Best!
 
F

Fredrik Lundh

iapain said:
No, I cant change permission or delete the module, the best would be
something to detect 'import os' in user code ..

trust me, implementing a restricted execution model for Python that actually
works is a *lot* harder than that.

googling for "python restricted execution" might give you some clues.

</F>
 
I

iapain

googling for "python restricted execution" might give you some clues.

I've already assumed that there is no rexec for me as i am using python
2.4. Yeah its much more difficult that my imagination. Should I go for
alternatives like
1. Assume every one who is using this webide, wont corrupt system
2. Use some tricks to encrypt the user path and do lots of replacement
on user code and o/p.

or something else?

Best!
iapain
 
F

Fredrik Lundh

iapain said:
I've already assumed that there is no rexec for me as i am using python
2.4. Yeah its much more difficult that my imagination. Should I go for
alternatives like
1. Assume every one who is using this webide, wont corrupt system
2. Use some tricks to encrypt the user path and do lots of replacement
on user code and o/p.

or something else?

unless you're willing to build a restricted runtime that runs on top of the core inter-
preter, you should assume that anyone writing a Python script that's executed by
your program has access to everything that your Python process has access to...

</F>
 
I

iapain

unless you're willing to build a restricted runtime that runs on top of the core inter-
preter, you should assume that anyone writing a Python script that's executed by
your program has access to everything that your Python process has access to...

I think using replacements I can ban atleast OS module and about files,
either i should ban file open or write my own module something like
rexec, truefully i dont know if I can write that one or not. I was
thinking that this gonna take few days but looking much more difficult.
Thanks Fred! for nice tutorials on www.
 
S

Steven D'Aprano

I think using replacements I can ban atleast OS module and about files,

How are you planning on banning the module? Are you thinking about using
source code scanning to detect risky code?

What about modules which export os? It's one thing to "ban" os, but
did you remember to ban glob.os? How about site.os? And netrc.os? And and
and and...

What about this line of code?

my_innocent_object = __import__(''.join([chr(110+x) for x in [1, 5]]))


Creating a restricted execution environment is *hard*. As far as I know,
even Microsoft has never attempted it. And for all of Sun's resources and
talent, security holes are sometimes found even in Java.
 
I

iapain

my_innocent_object = __import__(''.join([chr(110+x) for x in [1, 5]]))

Thats really smart way, yeah i had plan to scan and detect but I think
its not gonna work.
Creating a restricted execution environment is *hard*. As far as I know,
even Microsoft has never attempted it. And for all of Sun's resources and
talent, security holes are sometimes found even in Java.

Does that mean there is no way to implement restricted enviorment?

Best!
iapain
 
C

Cameron Laird

.
.
.
Does that mean there is no way to implement restricted enviorment?
.
.
.
The most knowledgeable people have effectively given up, in
regard to Python.

As it happens, though, Tcl *does* admit quite an interesting
restricted-execution model. Tcl was widely used in the '90s
for "agent" experiments, and its "safe interpreters" are
arguably more reliable than Java's restricted environment.
Tcl is also roughly comparable to Python in its power and
convenience for the individual developer. Should you want to
pursue this subject, <URL: http://wiki.tcl.tk/safe > and <URL:
http://wiki.tcl.tk/interp > might interest you.
 
D

Diez B. Roggisch

iapain said:
my_innocent_object = __import__(''.join([chr(110+x) for x in [1, 5]]))

Thats really smart way, yeah i had plan to scan and detect but I think
its not gonna work.
Creating a restricted execution environment is *hard*. As far as I know,
even Microsoft has never attempted it. And for all of Sun's resources and
talent, security holes are sometimes found even in Java.

Does that mean there is no way to implement restricted enviorment?

In a nutshell: yes, especially if not designed from ground up that way. If
you need it, the best thing to do is to put some distance between your code
and the possibly malicious one, using some RPC.

Diez
 
K

K.S.Sreeram

Steven said:
Creating a restricted execution environment is *hard*. As far as I know,
even Microsoft has never attempted it. And for all of Sun's resources and
talent, security holes are sometimes found even in Java.

Java is not the only restricted execution environment around.
Javascript, as implemented by most browsers, is an excellent lightweight
restricted execution environment, and there are many browsers which have
good implementations.

Regards
Sreeram


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEs9RGrgn0plK5qqURAnjvAKC/0kaWmWFI8un4016RGsNgm+3bggCgnhgh
P3NgiQD1zTVcqazwkr/qNEc=
=FBpd
-----END PGP SIGNATURE-----
 
I

iapain

The most knowledgeable people have effectively given up, in
regard to Python.

I guess now I am up with only one option, i.e hope that user input code
wont be evil to the system. **which is rarely possible**
 
P

Paul Rubin

K.S.Sreeram said:
Java is not the only restricted execution environment around.
Javascript, as implemented by most browsers, is an excellent lightweight
restricted execution environment, and there are many browsers which have
good implementations.

And we hear about browser security bugs all the time, for which the
workaround is "shut off javascript".
 
G

Georg Brandl

Cameron said:
.
.
.
.
.
.
The most knowledgeable people have effectively given up, in
regard to Python.

Brett Cannon is currently trying to come up with a comprehensive spec
and implementation of a sandboxed Python interpreter, for use in
Mozilla as a JavaScript replacement. (look in the python-dev archives
for more)

Georg
 
I

iapain

Brett Cannon is currently trying to come up with a comprehensive spec
and implementation of a sandboxed Python interpreter, for use in
Mozilla as a JavaScript replacement. (look in the python-dev archives
for more)

I'm not sure he is working or not, latest i read was he purposed new
restricted enviornment for python.

Best!
 
K

K.S.Sreeram

Paul said:
And we hear about browser security bugs all the time, for which the
workaround is "shut off javascript".

They all have bugs (including java), but atleast the architecture itself
isnt flawed (unlike say, ActiveX).

Anyways, the point I was trying to make is that, Sun is not the only one
to have implemented a restricted execution environment. Opera, Mozilla,
Safari etc,have all done it.

Regards
Sreeram




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEtDp/rgn0plK5qqURAo6gAJ496tWGJmmKTga2MtscI41uiL6+WACaA7C5
DxcvH4TIwQEQBCEZuPXA95Y=
=X1ju
-----END PGP SIGNATURE-----
 
D

Dennis Lee Bieber

Does that mean there is no way to implement restricted enviorment?
Do you have an IBM s/370 running VM/CMS? VM was sort of an OS for
running multiple OSs, so it would be the "restricted environment" <G>

Of course, getting Python to build is another matter (native
scripting language since the mid-80s is REXX).
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top