Skip to content

Commit 66110f7

Browse files
kouclaude
andauthored
Fix a use-after-free of the string of StringIO that shares its data (#235)
`dup`, `clone` and `reopen(other)` make two or more `StringIO` objects share one `struct StringIO`. `StringIO` is write barrier protected, but `RB_OBJ_WRITE(self, &ptr->string, string)` records the write barrier only for `self`. If another object that shares the data is in the old generation, the generational GC doesn't know that it refers to the new string. The string is collected when `self` is collected, and the other object keeps a dangling reference: g = StringIO.new("old".dup) 4.times {GC.start} # Promote g to the old generation g.dup.string = "new" * 100 GC.start(full_mark: false) g.string # refers to a collected object Unprotect objects from the write barrier when they start sharing the data. `StringIO` objects that don't share their data are still write barrier protected. TruffleRuby doesn't have write barriers and doesn't define `RB_OBJ_WB_UNPROTECT()`, so define it as a no-op there. This has existed since 009896b "Implement write barrier on StringIO". Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent f434104 commit 66110f7

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎ext/stringio/stringio.c‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ typedef unsigned long rb_ulen_t;
4343
# define RB_INTEGER_TYPE_P(c) (FIXNUM_P(c) || RB_TYPE_P(c, T_BIGNUM))
4444
#endif
4545

46+
/* TruffleRuby doesn't have write barriers */
47+
#ifndef RB_OBJ_WB_UNPROTECT
48+
# define RB_OBJ_WB_UNPROTECT(obj) ((void)(obj))
49+
#endif
50+
4651
/* strio_free may run on any thread, so the refcount shared by #initialize_copy must be atomic */
4752
#ifdef RUBY_TYPED_THREAD_SAFE_FREE
4853
# include "ruby/atomic.h"
@@ -785,18 +790,20 @@ static VALUE
785790
strio_copy(VALUE copy, VALUE orig)
786791
{
787792
struct StringIO *ptr, *old_ptr;
788-
VALUE old_string = Qundef;
789793

790794
orig = rb_convert_type(orig, T_DATA, "StringIO", "to_strio");
791795
if (copy == orig) return copy;
792796
ptr = StringIOForRead(orig);
793797
old_ptr = check_strio(copy);
794798
if (old_ptr) {
795-
old_string = old_ptr->string;
796799
strio_free(old_ptr);
797800
}
801+
/* copy and orig share ptr now. A write barrier through one of them
802+
* can't notify the other, so the generational GC can miss an
803+
* old-to-young reference in ptr->string. Unprotect both. */
804+
RB_OBJ_WB_UNPROTECT(copy);
805+
RB_OBJ_WB_UNPROTECT(orig);
798806
DATA_PTR(copy) = ptr;
799-
RB_OBJ_WRITTEN(copy, old_string, ptr->string);
800807
RB_FL_UNSET_RAW(copy, STRIO_READWRITE);
801808
RB_FL_SET_RAW(copy, RB_FL_TEST_RAW(orig, STRIO_READWRITE));
802809
STRIO_REFCNT_INC(ptr);

‎test/stringio/test_stringio.rb‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,17 @@ def test_initialize_copy
12061206
assert_not_predicate(f2, :frozen?)
12071207
end
12081208

1209+
def test_set_string_via_dup_with_gc
1210+
omit "This is for the write barrier of CRuby" unless RUBY_ENGINE == "ruby"
1211+
assert_separately(%w[-rstringio], "#{<<-"begin;"}\n#{<<-"end;"}")
1212+
begin;
1213+
io = StringIO.new("old".dup)
1214+
4.times {GC.start} # Promote io to the old generation
1215+
io.dup.string = "new" * 100
1216+
GC.verify_internal_consistency
1217+
end;
1218+
end
1219+
12091220
private
12101221

12111222
def assert_string(content, encoding, str, mesg = nil)

0 commit comments

Comments
 (0)