Exception methods can bypass $SAFE

Exception#to_s method can be used to trick $SAFE check, which makes a untrusted codes to modify arbitrary strings.

Detailed description

In Ruby's $SAFE semantics, safe level of 4 is used to run a untrusted code (such as plugin). So in upper safe levels, some sort of operations are prohibited to prevent untrusted codes from attacking outer (trusted) data.

Exception#to_s was found to be problematic around it. The method can trick safe level mechanism and destructively modifies an untaitned string to be tainted. With this an attacker can modify arbitrary untainted strings like this:

$secret_path = "foo"

proc do
    $SAFE = 4
    $secret_path.replace "/etc/passwd"

open($secret_path) do

Affected versions

Luckily this attack is ineffective for 1.9.x series of ruby. Affected versions are restricted to:

  • Ruby 1.8.6 patchlevel 420 and all prior versions
  • Ruby 1.8.7 patchlevel 330 and all prior versions
  • Development versions of Ruby 1.8 (1.8.8dev)


Please upgrade to a newer version.