tags:

views:

43

answers:

2

I have the following Ruby code:

Testcase.rb:

filename = "/absolute/path/to/Untrusted.rb"
thread = Thread.new do
  $SAFE = 4
  Kernel::load(filename, true)
end
thread.join

Untrusted.rb

#Untrusted code
puts "Untrusted!"

However, I get an error when I try to run Testcase.rb:

/Volumes/Data/Users/mike/Desktop/Testcase.rb:4:in `write': Insecure operation `write' at level 4 (SecurityError)
    from /Volumes/Data/Users/mike/Desktop/Testcase.rb:7:in `join'
    from /Volumes/Data/Users/mike/Desktop/Testcase.rb:7

Removing $SAFE=4 solves the issue, but I want to be able to safely run untrusted code. Any ideas about why this isn't working?

A: 

If you are running in a sandbox, it doesn't allow unsafe code to be run, because a sandbox has its purpose, to keep you from doing something that's not allowed or unsafe.

alexy13
I understand this - but what is the unsafe code? I'm just doing a puts(and even commenting the puts out produces the same error - i.e. loading an empty file!)
Mike
+1  A: 

I tried your code and got the same result as you. Then I changed the $SAFE level to 3, and got this warning:

Insecure world writable dir /tmp in LOAD_PATH, mode 041777

I moved the file being loaded from /tmp to a directory that isn't world-writable and the warning went away. Changing the $SAFE level to 4 then worked.

So, try making sure that the loaded file's directory isn't world writable. Also try a lower safe level and see if you get a useful warning.

Wayne Conrad
Yes, that was the issue. Thanks!
Mike