Fix pm_compile_call for inline_new with keyword args

pm_setup_args mallocs one kw_arg buffer with references = 0. The inline_new branch then feeds that same pointer
to three callinfo constructions:

- 3789 — new_callinfo(... method_id="new" ..., kw_arg, 0) -> the opt_new ci
- 3795 — PUSH_SEND_R(... "initialize", ..., flags | VM_CALL_FCALL, kw_arg)
- 3800 — PUSH_SEND_R(... method_id="new", ..., flags, kw_arg) (fallback)

Tracing the refcount through rb_vm_ci_lookup()

1. It increments kwarg->references and allocates a fresh new_ci before the dedup st_update
2. The dedup (vm_ci_hash_cmp) compares kwarg contents, not the pointer. So if an earlier line already interned a new ci with the
identical keyword set, st_update returns that pre-existing ci (which holds a different buffer) and discards our new_ci.
3. Our kw_arg is now orphaned: references == 1, but the only holder is the discarded new_ci, which is a normal collectable imemo
4. An allocation like PUSH_INSN2 opt_new at or new_callinfo() can trigger a GC. References back to 0, kw_arg buffer freed.
5. new_callinfo() using the freed buffer, does argc += kw_arg->keyword_len (use-after-free)

The fix: Keep the buffer alive across the allocations in inline_new.

Fixes [Bug #22104] (Backport 4.0)
This commit is contained in:
Luke Gruber authored and Takashi Kokubun committed 2026-06-29 13:30:54 -07:00
1 parent 48ac72683a
commit 59d6fed324
3 files changed
+19 -6

No files matched your search

+1 -6
View File
@@ -577,12 +577,7 @@ rb_imemo_free(VALUE obj)
break;
case imemo_callinfo:{
const struct rb_callinfo *ci = ((const struct rb_callinfo *)obj);
if (ci->kwarg) {
if (RUBY_ATOMIC_FETCH_SUB(((struct rb_callinfo_kwarg *)ci->kwarg)->references, 1) == 1) {
xfree((void *)ci->kwarg);
}
}
rb_callinfo_kwarg_release((struct rb_callinfo_kwarg *)ci->kwarg);
RB_DEBUG_COUNTER_INC(obj_imemo_callinfo);
break;
+4
View File
@@ -3776,6 +3776,8 @@ pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *c
ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.line, location.node_id, BIN(putnil), 0)->link);
}
rb_callinfo_kwarg_retain(kw_arg);
// Jump unless the receiver uses the "basic" implementation of "new"
VALUE ci;
if (flags & VM_CALL_FORWARDING) {
@@ -3798,6 +3800,8 @@ pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *c
PUSH_LABEL(ret, not_basic_new_finish);
PUSH_INSN(ret, location, pop);
rb_callinfo_kwarg_release(kw_arg);
}
else {
PUSH_SEND_R(ret, location, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
+14
View File
@@ -61,6 +61,20 @@ rb_callinfo_kwarg_bytes(int keyword_len)
rb_eRuntimeError);
}
static inline void
rb_callinfo_kwarg_retain(struct rb_callinfo_kwarg *kwarg)
{
if (kwarg) RUBY_ATOMIC_INC(kwarg->references);
}
static inline void
rb_callinfo_kwarg_release(struct rb_callinfo_kwarg *kwarg)
{
if (kwarg && RUBY_ATOMIC_FETCH_SUB(kwarg->references, 1) == 1) {
ruby_sized_xfree(kwarg, rb_callinfo_kwarg_bytes(kwarg->keyword_len));
}
}
// imemo_callinfo
struct rb_callinfo {
VALUE flags;