# Safe sandbox for running untrusted code

**URL:** <https://www.ruby-forum.com/t/safe-sandbox-for-running-untrusted-code/139289>\
**Category:** Ruby\
**Created:** [June 2, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/safe-sandbox-for-running-untrusted-code/139289 "2008-06-02T17:13:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubenfonseca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rubenfonseca](https://www.ruby-forum.com/u/rubenfonseca)\
**Post date:** [June 2, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/safe-sandbox-for-running-untrusted-code/139289/1 "2008-06-02T17:13:31Z")

</div>

Hello @all!

I’m sorry if this has been answered before, but I’m looking for an  
expert voice for this question.

I would need if Ruby have a safe mechanism of running unsafe code on a  
sandbox environment. Image this situation:

I lunch a contest “solve this problem in Ruby”. The users submit their  
code, and my system runs the code on an sandbox, with memory and time  
constraints, and verifies if the output meets the required result.

Basicly, I need a way to run Ruby code that reads from STDIN, writes to  
STDOUT, and I need to be sure it doesn’t run more than “x” seconds and  
don’t eat all my memory.

It this possible with the current VM (MRI 1.8)?

Thank you for your support.

---

<div class="post-metadata">

**Author:** ![rubenfonseca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rubenfonseca](https://www.ruby-forum.com/u/rubenfonseca)\
**Post date:** [June 2, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/safe-sandbox-for-running-untrusted-code/139289/2 "2008-06-02T17:20:30Z")

</div>

2008/6/2 Ruben F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> It this possible with the current VM (MRI 1.8)?

There is at least $SAFE:  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/taint.html#S1](http://www.ruby-doc.org/docs/ProgrammingRuby/html/taint.html#S1)

But AFAIK it does not prevent your CPU or memory going through the  
roof or just taking ages. Depending on your platform you might be  
able to do it using fork and having the parent kill the child if any  
of your constraints (time, memory) are violated. But then you are  
still not safe against system(“/bin/rm”, “-rf”, “/”). That’s where  
$SAFE helps.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubenfonseca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rubenfonseca](https://www.ruby-forum.com/u/rubenfonseca)\
**Post date:** [June 2, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/safe-sandbox-for-running-untrusted-code/139289/3 "2008-06-02T18:07:18Z")

</div>

On Tue, 3 Jun 2008 00:18:49 +0900  
“Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > don’t eat all my memory.  
> > still not safe against system(“/bin/rm”, “-rf”, “/”). That’s where  
> > $SAFE helps.
> 
> Kind regards
> 
> robert

I’d run the code within a virtualised environment. You’d be safe to rm  
-rf / as the “virtual server” is safely contained. vserver, xen, openvz,  
virtualbox, lguest… There’s also the capability of changing ram,  
diskspace and loads of other stuff from outside of the virtual thang.

/dev/jayeola

---

<div class="post-metadata">

**Author:** ![rubenfonseca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rubenfonseca](https://www.ruby-forum.com/u/rubenfonseca)\
**Post date:** [June 2, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/safe-sandbox-for-running-untrusted-code/139289/4 "2008-06-02T18:21:33Z")

</div>

ara.t.howard wrote:

> On Jun 2, 2008, at 9:12 AM, Ruben F. wrote:
> 
> > It this possible with the current VM (MRI 1.8)?
> 
> no.
> 
> you will need to combine using $SAFE=12 (ruby side) and ulimit/chroot  
> from the unix side (or similar).

hi! thanks for all your input!

so does anybody knows how [http://tryruby.hobix.com/](http://tryruby.hobix.com/) does it?

Ruben

---

<div class="post-metadata">

**Author:** ![rubenfonseca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rubenfonseca](https://www.ruby-forum.com/u/rubenfonseca)\
**Post date:** [June 2, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/safe-sandbox-for-running-untrusted-code/139289/5 "2008-06-02T18:10:15Z")

</div>

On Jun 2, 2008, at 9:12 AM, Ruben F. wrote:

> It this possible with the current VM (MRI 1.8)?

no.

you will need to combine using $SAFE=12 (ruby side) and ulimit/chroot  
from the unix side (or similar).

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![rubenfonseca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rubenfonseca](https://www.ruby-forum.com/u/rubenfonseca)\
**Post date:** [June 3, 2008, 8:59am UTC](https://www.ruby-forum.com/t/safe-sandbox-for-running-untrusted-code/139289/6 "2008-06-03T08:59:08Z")

</div>

On Mon, Jun 2, 2008 at 6:20 PM, Ruben F. wrote:

> so does anybody knows how [http://tryruby.hobix.com/](http://tryruby.hobix.com/) does it?

[http://code.whytheluckystiff.net/sandbox/](http://code.whytheluckystiff.net/sandbox/) and/or search for freaky  
freaky sandbox.

IIRC \_why got some patches in ruby regarding this – see archives for  
ruby-core.

J.

---

<div class="post-metadata">

**Author:** ![rubenfonseca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rubenfonseca](https://www.ruby-forum.com/u/rubenfonseca)\
**Post date:** [June 3, 2008, 10:01am UTC](https://www.ruby-forum.com/t/safe-sandbox-for-running-untrusted-code/139289/7 "2008-06-03T10:01:26Z")

</div>

Ruben F. wrote:

> ara.t.howard wrote:
> 
> > On Jun 2, 2008, at 9:12 AM, Ruben F. wrote:
> > 
> > > It this possible with the current VM (MRI 1.8)?
> > 
> > no.
> > 
> > you will need to combine using $SAFE=12 (ruby side) and ulimit/chroot  
> > from the unix side (or similar).
> 
> hi! thanks for all your input!
> 
> so does anybody knows how [http://tryruby.hobix.com/](http://tryruby.hobix.com/) does it?
> 
> Ruben

[http://www.spoj.pl/info/](http://www.spoj.pl/info/) does this with 30 programming languages,  
including Ruby. I don’t know how.

regards,

Siep
