Commit 19317e2a
19317e2af8e8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #1196 from rubys/fix/view-render-gc-uaf
Fix two GC use-after-frees in generated view rendering
Details
- Commit
- 19317e2af8e8
- Parent
- 046055181245
- Merge parent
- 9f0cb3f03d68
spinel_codegen.rbmodifiedView file
@@ -8893,8 +8893,8 @@
88938893
# are merged in either direction, both result paths return a88948894
# fresh sym_poly_hash with the str entries boxed via sp_box_str.88958895
# Issue #515.8896-
emit_raw("static sp_SymPolyHash*sp_SymStrHash_to_sym_poly(sp_SymStrHash*h){sp_SymPolyHash*r=sp_SymPolyHash_new();for(mrb_int i=0;i<h->len;i++)sp_SymPolyHash_set(r,h->order[i],sp_box_str(sp_SymStrHash_get(h,h->order[i])));return r;}")8897-
emit_raw("static sp_SymPolyHash*sp_SymPolyHash_merge_str(sp_SymPolyHash*a,sp_SymStrHash*b){sp_SymPolyHash*r=sp_SymPolyHash_new();for(mrb_int i=0;i<a->len;i++)sp_SymPolyHash_set(r,a->order[i],sp_SymPolyHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_SymPolyHash_set(r,b->order[i],sp_box_str(sp_SymStrHash_get(b,b->order[i])));return r;}")8896+
emit_raw("static sp_SymPolyHash*sp_SymStrHash_to_sym_poly(sp_SymStrHash*h){SP_GC_SAVE();SP_GC_ROOT(h);sp_SymPolyHash*r=sp_SymPolyHash_new();SP_GC_ROOT(r);for(mrb_int i=0;i<h->len;i++)sp_SymPolyHash_set(r,h->order[i],sp_box_str(sp_SymStrHash_get(h,h->order[i])));return r;}")8897+
emit_raw("static sp_SymPolyHash*sp_SymPolyHash_merge_str(sp_SymPolyHash*a,sp_SymStrHash*b){SP_GC_SAVE();SP_GC_ROOT(a);SP_GC_ROOT(b);sp_SymPolyHash*r=sp_SymPolyHash_new();SP_GC_ROOT(r);for(mrb_int i=0;i<a->len;i++)sp_SymPolyHash_set(r,a->order[i],sp_SymPolyHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_SymPolyHash_set(r,b->order[i],sp_box_str(sp_SymStrHash_get(b,b->order[i])));return r;}")88988898
emit_raw("")88998899
end89008900
@@ -9476,13 +9476,13 @@
94769476
# Sym keys go through sp_sym_to_s; values are boxed to sp_RbVal.94779477
if @needs_str_poly_hash == 194789478
if @needs_sym_int_hash == 19479-
emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_int_hash(sp_SymIntHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();if(!h)return r;r->default_v=sp_box_int(h->default_v);for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_box_int(sp_SymIntHash_get(h,k)));}return r;}")9479+
emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_int_hash(sp_SymIntHash*h){SP_GC_SAVE();SP_GC_ROOT(h);sp_StrPolyHash*r=sp_StrPolyHash_new();SP_GC_ROOT(r);if(!h)return r;r->default_v=sp_box_int(h->default_v);for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_box_int(sp_SymIntHash_get(h,k)));}return r;}")94809480
end94819481
if @needs_sym_str_hash == 19482-
emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_str_hash(sp_SymStrHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();if(!h)return r;if(h->default_v)r->default_v=sp_box_str(h->default_v);for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_box_str(sp_SymStrHash_get(h,k)));}return r;}")9482+
emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_str_hash(sp_SymStrHash*h){SP_GC_SAVE();SP_GC_ROOT(h);sp_StrPolyHash*r=sp_StrPolyHash_new();SP_GC_ROOT(r);if(!h)return r;if(h->default_v)r->default_v=sp_box_str(h->default_v);for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_box_str(sp_SymStrHash_get(h,k)));}return r;}")94839483
end94849484
if @needs_sym_poly_hash == 19485-
emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_poly_hash(sp_SymPolyHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();if(!h)return r;r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_SymPolyHash_get(h,k));}return r;}")9485+
emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_poly_hash(sp_SymPolyHash*h){SP_GC_SAVE();SP_GC_ROOT(h);sp_StrPolyHash*r=sp_StrPolyHash_new();SP_GC_ROOT(r);if(!h)return r;r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_SymPolyHash_get(h,k));}return r;}")94869486
end94879487
end94889488
end@@ -19671,21 +19671,21 @@
1967119671
end1967219672
end1967319673
# Flatten chained string concat: a + b + c → sp_str_concat3(a,b,c)19674+
# Hoist every part into a rooted temp before the concat call. The19675+
# call-argument order (concat3 / concat4) and the compound-literal19676+
# initializer order (concat_arr) are both unspecified, so a part19677+
# that is a fresh GC string would otherwise sit unrooted on the C19678+
# stack while a sibling part evaluates and triggers sp_gc_collect,19679+
# leaving sp_str_concatN to read freed memory. Observed as a19680+
# use-after-free in the layout's19681+
# `stylesheet_link_tag(..) + "\n" + stylesheet_link_tag(..)` under19682+
# load: the first tag is built, the second tag's render allocates19683+
# and collects the first. The >=5 (concat_arr) path always hoisted;19684+
# the 3- and 4-part paths inlined the parts and were exposed.19685+
# @needs_gc is set in scan_features for any string `+`, so19686+
# SP_GC_SAVE() is already in the function header.1967419687
parts = collect_concat_chain(nid)19675-
if parts.length == 319676-
return "sp_str_concat3(" + parts[0] + ", " + parts[1] + ", " + parts[2] + ")"19677-
end19678-
if parts.length == 419679-
return "sp_str_concat4(" + parts[0] + ", " + parts[1] + ", " + parts[2] + ", " + parts[3] + ")"19680-
end19681-
if parts.length >= 519682-
# Variable-length: single malloc for N parts via sp_str_concat_arr.19683-
# Hoist each part into a rooted temp first — the compound-literal19684-
# initializer order is unspecified, and any part that is a fresh19685-
# GC string would otherwise sit unrooted on the C stack while19686-
# later parts evaluate (and may trigger sp_gc_collect).19687-
# @needs_gc is set in scan_features for any string `+`, ensuring19688-
# SP_GC_SAVE() is in the function header before we emit roots here.19688+
if parts.length >= 31968919689
tnames = "".split(",", -1)1969019690
k = 01969119691
while k < parts.length@@ -19695,6 +19695,13 @@
1969519695
tnames.push(t)1969619696
k = k + 11969719697
end19698+
if parts.length == 319699+
return "sp_str_concat3(" + tnames[0] + ", " + tnames[1] + ", " + tnames[2] + ")"19700+
end19701+
if parts.length == 419702+
return "sp_str_concat4(" + tnames[0] + ", " + tnames[1] + ", " + tnames[2] + ", " + tnames[3] + ")"19703+
end19704+
# Variable-length: single malloc for N parts via sp_str_concat_arr.1969819705
arr = "(const char *const[]){"1969919706
k = 01970019707
while k < tnames.lengthtest/concat_chain_operand_gc_root.rbaddedView file
Loading diff…
test/concat_chain_operand_gc_root.rb.expectedaddedView file
Loading diff…