Commit 155ec59a
155ec59a2269Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Copy a mutable String's buffer when converting it to an immutable string
A mutable String (String.new + <<) is an sp_String object that owns its data buffer; the object's finalizer frees it, and the buffer carries a 0xfd marker byte that sp_gc_mark deliberately ignores. Converting that buffer to a const char* by aliasing ->data therefore produces a string whose lifetime the GC cannot track independently: once the unreferenced sp_String is collected the alias dangles. This is the use-after-free that crashes a roundhouse Rails app under load, where every render boxes the view builder's buffer (sp_box_str(view_result->data)) into iv_body and a GC between render and the layout sprintf frees it.
Copy into a sweep-owned (0xfe) buffer via sp_str_dup_external at every site where the alias escapes the builder's rooted scope, centralized in mutable_str_to_cstr:
- the poly box (mutable_str -> SP_TAG_STR), the reported crash;
- compile_expr_for_expected_type's mutable_str -> string coercion;
- Kernel#String(builder);
- builder.to_s;
- the general mutable-string-method fallthrough, whose receiver- returning methods (itself, encode, each_char/each_byte, and gsub/sub/chomp/tr_s on a no-op) would otherwise hand back the alias. The fallthrough copy is GC-rooted so it also survives a GC inside an allocating method (split, chars, scan, ...) that reads the receiver.
Transient reads where the builder stays rooted across the expression (method receivers passed to allocating helpers, sprintf args, iteration) are left aliasing; they were never the bug.
This makes mutable_str -> string a snapshot, which diverges from CRuby's shared-mutable String(s) / s.to_s-returns-self, but Spinel already types those results as immutable strings, so it is consistent with the subset.
Fixes #1071
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 155ec59a2269
- Parent
- b55b1483270b
spinel_codegen.rbmodifiedView file
# return type was inferred too narrowly upstream, but the call site # can recover at the boundary. if at == "mutable_str" return "(" + val + ")->data" return mutable_str_to_cstr(val) end end if is_obj_type(expected_base) == 1 && at == "poly" tmp end # A mutable_str (sp_String *) owns its `->data` buffer, freed by the # object's finalizer. That buffer carries a 0xfd marker byte which # sp_gc_mark deliberately ignores, so a `const char *` aliasing it # cannot be kept alive independently: once the unreferenced sp_String # is collected the alias dangles. Whenever the alias would escape the # object's rooted scope -- boxed into a poly slot, returned, stored -- # copy into a sweep-owned (0xfe) buffer via sp_str_dup_external so the # resulting immutable string carries its own lifetime. `expr` is # emitted exactly once. def mutable_str_to_cstr(expr) "sp_str_dup_external((" + expr + ")->data)" end def compile_arg0(nid) args_id = @nd_arguments[nid] if args_id >= 0 return "sp_String_dup(" + rc + ")" end if mname == "to_s" return rc + "->data" return mutable_str_to_cstr(rc) end # Issues #740, #741: mutating methods on mutable_str. if mname == "prepend" || mname == "<<" || mname == "concat" if mname == "frozen?" return "sp_String_is_frozen(" + rc + ")" end # For all other string methods, convert via ->data r = compile_string_method_expr(nid, mname, rc + "->data") # For all other string methods, operate on a sweep-owned copy of the # buffer instead of the object-owned alias. Methods that return the # receiver unchanged (itself, encode, each_char/each_byte, and # gsub/sub/chomp/tr_s on a no-op) would otherwise hand back a # `const char *` aliasing `->data`, which dangles once the sp_String # is collected. The copy is rooted so it also survives a GC inside an # allocating method (split, chars, scan, ...) that reads the receiver. cptmp = new_temp @needs_gc = 1 emit(" const char *" + cptmp + " = " + mutable_str_to_cstr(rc) + ";") emit_gc_root_for_expr(cptmp, "string") r = compile_string_method_expr(nid, mname, cptmp) if r != "" return r end return "sp_bigint_to_s((sp_Bigint *)" + a_e_S + ")" end if at_S == "mutable_str" return "(" + a_e_S + ")->data" return mutable_str_to_cstr(a_e_S) end if at_S == "poly" @needs_rb_value = 1 # a statement-expression so side effects fire exactly once. if at == "mutable_str" mtmp = new_temp return "({ sp_String *" + mtmp + " = " + val + "; " + mtmp + " ? sp_box_str(" + mtmp + "->data) : sp_box_nil(); })" return "({ sp_String *" + mtmp + " = " + val + "; " + mtmp + " ? sp_box_str(" + mutable_str_to_cstr(mtmp) + ") : sp_box_nil(); })" end # Other pointer types — box with a neutral cls_id of 0 rather # than truncating the pointer to int.test/mutable_str_alias_uaf.rbaddedView file
Loading diff…
test/mutable_str_alias_uaf.rb.expectedaddedView file
Loading diff…