ruby-changes:57048
From: Nobuyoshi <ko1@a...>
Date: Thu, 15 Aug 2019 23:50:15 +0900 (JST)
Subject: [ruby-changes:57048] Nobuyoshi Nakada: d5c33364e3 (master): Fixed heap-use-after-free
https://git.ruby-lang.org/ruby.git/commit/?id=d5c33364e3 From d5c33364e3c0efb15e11df417c925afee2cdb9c9 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Thu, 15 Aug 2019 23:25:37 +0900 Subject: Fixed heap-use-after-free * string.c (rb_str_sub_bang): retrieves a pointer to the replacement string buffer just before using it, for the case of replacement with the receiver string itself. [Bug #16105] diff --git a/string.c b/string.c index 9331532..6ec0514 100644 --- a/string.c +++ b/string.c @@ -5098,7 +5098,7 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L5098 cr = cr2; } plen = end0 - beg0; - rp = RSTRING_PTR(repl); rlen = RSTRING_LEN(repl); + rlen = RSTRING_LEN(repl); len = RSTRING_LEN(str); if (rlen > plen) { RESIZE_CAPA(str, len + rlen - plen); @@ -5107,6 +5107,7 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L5107 if (rlen != plen) { memmove(p + beg0 + rlen, p + beg0 + plen, len - beg0 - plen); } + rp = RSTRING_PTR(repl); memmove(p + beg0, rp, rlen); len += rlen - plen; STR_SET_LEN(str, len); diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 7ae3bf2..36d246f 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -2010,6 +2010,12 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L2010 r.taint a.sub!(/./, r) assert_predicate(a, :tainted?) + + bug16105 = '[Bug #16105] heap-use-after-free' + a = S("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345678") + b = a.dup + c = a.slice(1, 100) + assert_equal("AABCDEFGHIJKLMNOPQRSTUVWXYZ012345678", b.sub!(c, b), bug16105) end def test_succ -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/