Exception methods can bypass $SAFE

Posted by Urabe Shyouhei on 18 Feb 2011

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
    Exception.new($secret_path).to_s
    $secret_path.replace "/etc/passwd"
end.call

open($secret_path) do
  ...
end

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)

Solutions

Please upgrade to a newer version.

Updates

Recent News

Ruby 3.2.11 Released

Ruby 3.2.11 has been released. This release includes an update to the zlib gem addressing CVE-2026-27820.

Posted by hsbt on 27 Mar 2026

Ruby 3.3.11 Released

Ruby 3.3.11 has been released. This release includes an update to the zlib gem addressing CVE-2026-27820, along with some bug fixes.

Posted by hsbt on 26 Mar 2026

More News...