merge revision(s) 92dd9734a9: [Backport #20950]

Fix use-after-free in ep in Proc#dup for ifunc procs

	[Bug #20950]

	ifunc proc has the ep allocated in the cfunc_proc_t which is the data of
	the TypedData object. If an ifunc proc is duplicated, the ep points to
	the ep of the source object. If the source object is freed, then the ep
	of the duplicated object now points to a freed memory region. If we try
	to use the ep we could crash.

	For example, the following script crashes:

	    p = { a: 1 }.to_proc
	    100.times do
	      p = p.dup
	      GC.start
	      p.call
	    rescue ArgumentError
	    end

	This commit changes ifunc proc to also duplicate the ep when it is duplicated.
This commit is contained in:
Takashi Kokubun 2025-01-14 17:54:19 -08:00
parent 00147cbab5
commit 299455be99
5 changed files with 64 additions and 6 deletions

View file

@ -23,6 +23,7 @@ VALUE rb_block_to_s(VALUE self, const struct rb_block *block, const char *additi
VALUE rb_callable_receiver(VALUE); VALUE rb_callable_receiver(VALUE);
VALUE rb_func_proc_new(rb_block_call_func_t func, VALUE val); VALUE rb_func_proc_new(rb_block_call_func_t func, VALUE val);
VALUE rb_func_proc_dup(VALUE src_obj);
VALUE rb_func_lambda_new(rb_block_call_func_t func, VALUE val, int min_argc, int max_argc); VALUE rb_func_lambda_new(rb_block_call_func_t func, VALUE val, int min_argc, int max_argc);
VALUE rb_iseq_location(const struct rb_iseq_struct *iseq); VALUE rb_iseq_location(const struct rb_iseq_struct *iseq);
VALUE rb_sym_to_proc(VALUE sym); VALUE rb_sym_to_proc(VALUE sym);

44
proc.c
View file

@ -680,6 +680,29 @@ cfunc_proc_new(VALUE klass, VALUE ifunc)
return procval; return procval;
} }
VALUE
rb_func_proc_dup(VALUE src_obj)
{
RUBY_ASSERT(rb_typeddata_is_instance_of(src_obj, &proc_data_type));
rb_proc_t *src_proc;
GetProcPtr(src_obj, src_proc);
RUBY_ASSERT(vm_block_type(&src_proc->block) == block_type_ifunc);
cfunc_proc_t *proc;
VALUE proc_obj = TypedData_Make_Struct(rb_obj_class(src_obj), cfunc_proc_t, &proc_data_type, proc);
memcpy(&proc->basic, src_proc, sizeof(rb_proc_t));
VALUE *ep = *(VALUE **)&proc->basic.block.as.captured.ep = proc->env + VM_ENV_DATA_SIZE - 1;
ep[VM_ENV_DATA_INDEX_FLAGS] = src_proc->block.as.captured.ep[VM_ENV_DATA_INDEX_FLAGS];
ep[VM_ENV_DATA_INDEX_ME_CREF] = src_proc->block.as.captured.ep[VM_ENV_DATA_INDEX_ME_CREF];
ep[VM_ENV_DATA_INDEX_SPECVAL] = src_proc->block.as.captured.ep[VM_ENV_DATA_INDEX_SPECVAL];
ep[VM_ENV_DATA_INDEX_ENV] = src_proc->block.as.captured.ep[VM_ENV_DATA_INDEX_ENV];
return proc_obj;
}
static VALUE static VALUE
sym_proc_new(VALUE klass, VALUE sym) sym_proc_new(VALUE klass, VALUE sym)
{ {
@ -1300,12 +1323,17 @@ proc_eq(VALUE self, VALUE other)
} }
break; break;
case block_type_ifunc: case block_type_ifunc:
if (self_block->as.captured.ep != \ if (self_block->as.captured.code.ifunc != \
other_block->as.captured.ep ||
self_block->as.captured.code.ifunc != \
other_block->as.captured.code.ifunc) { other_block->as.captured.code.ifunc) {
return Qfalse; return Qfalse;
} }
if (memcmp(
((cfunc_proc_t *)self_proc)->env,
((cfunc_proc_t *)other_proc)->env,
sizeof(((cfunc_proc_t *)self_proc)->env))) {
return Qfalse;
}
break; break;
case block_type_proc: case block_type_proc:
if (self_block->as.proc != other_block->as.proc) { if (self_block->as.proc != other_block->as.proc) {
@ -1434,6 +1462,7 @@ rb_hash_proc(st_index_t hash, VALUE prc)
break; break;
case block_type_ifunc: case block_type_ifunc:
hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.ifunc->func); hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.ifunc->func);
hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.ifunc->data);
break; break;
case block_type_symbol: case block_type_symbol:
hash = rb_st_hash_uint(hash, rb_any_hash(proc->block.as.symbol)); hash = rb_st_hash_uint(hash, rb_any_hash(proc->block.as.symbol));
@ -1445,7 +1474,14 @@ rb_hash_proc(st_index_t hash, VALUE prc)
rb_bug("rb_hash_proc: unknown block type %d", vm_block_type(&proc->block)); rb_bug("rb_hash_proc: unknown block type %d", vm_block_type(&proc->block));
} }
return rb_hash_uint(hash, (st_index_t)proc->block.as.captured.ep); /* ifunc procs have their own allocated ep. If an ifunc is duplicated, they
* will point to different ep but they should return the same hash code, so
* we cannot include the ep in the hash. */
if (vm_block_type(&proc->block) != block_type_ifunc) {
hash = rb_hash_uint(hash, (st_index_t)proc->block.as.captured.ep);
}
return hash;
} }

View file

@ -410,6 +410,18 @@ class TestProc < Test::Unit::TestCase
assert_throw(:initialize_dup) {c1.new{}.dup} assert_throw(:initialize_dup) {c1.new{}.dup}
end end
def test_dup_ifunc_proc_bug_20950
assert_normal_exit(<<~RUBY, "[Bug #20950]")
p = { a: 1 }.to_proc
100.times do
p = p.dup
GC.start
p.call
rescue ArgumentError
end
RUBY
end
def test_clone_subclass def test_clone_subclass
c1 = Class.new(Proc) c1 = Class.new(Proc)
assert_equal c1, c1.new{}.clone.class, '[Bug #17545]' assert_equal c1, c1.new{}.clone.class, '[Bug #17545]'

View file

@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 6 #define RUBY_VERSION_TEENY 6
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 117 #define RUBY_PATCHLEVEL 118
#include "ruby/version.h" #include "ruby/version.h"
#include "ruby/internal/abi.h" #include "ruby/internal/abi.h"

11
vm.c
View file

@ -1158,7 +1158,16 @@ rb_proc_dup(VALUE self)
rb_proc_t *src; rb_proc_t *src;
GetProcPtr(self, src); GetProcPtr(self, src);
procval = proc_create(rb_obj_class(self), &src->block, src->is_from_method, src->is_lambda);
switch (vm_block_type(&src->block)) {
case block_type_ifunc:
procval = rb_func_proc_dup(self);
break;
default:
procval = proc_create(rb_obj_class(self), &src->block, src->is_from_method, src->is_lambda);
break;
}
if (RB_OBJ_SHAREABLE_P(self)) FL_SET_RAW(procval, RUBY_FL_SHAREABLE); if (RB_OBJ_SHAREABLE_P(self)) FL_SET_RAW(procval, RUBY_FL_SHAREABLE);
RB_GC_GUARD(self); /* for: body = rb_proc_dup(body) */ RB_GC_GUARD(self); /* for: body = rb_proc_dup(body) */
return procval; return procval;