Project

General

Profile

Actions

Bug #22531

closed

String#% with %s format string runs arbitrary code that can invalidate the argument array

Bug #22531: String#% with %s format string runs arbitrary code that can invalidate the argument array

Added by tekknolagi (Maxwell Bernstein) 23 days ago. Updated 13 days ago.

Status:
Closed
Assignee:
-
Target version:
[ruby-core:127405]

Description

For example,

$a = Array.new(20) { "x" * 100 }
o = Object.new
def o.to_s = ($a.clear; GC.start; "o")
$a[0] = o
puts "%s %s %s" % $a

This crashes for me with a segfault.

String#% calls rb_str_format_ary with the array ptr and length of $a but that changes out from underneath it when to_s gets called and invalidates the pointer.

Updated by jhawthorn (John Hawthorn) 13 days ago Actions #1 [ruby-core:127642]

  • Status changed from Open to Closed
  • Backport changed from 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.3: DONE, 3.4: DONE, 4.0: DONE

Looks like Peter fixed this in git|edce07a8c93895be8eeb2d27400aa138cc8a3cf9, which was backported to 3.3, 3.4 and 4.0 in yesterday's releases.

On master and 4.0.10 the example now raises instead of crashing:

/tmp/r.rb:5:in 'String#%': array modified during formatting (RuntimeError)
Actions

Also available in: PDF Atom