From 2a863d4babed062dd91d2fe519d5018651c6378e Mon Sep 17 00:00:00 2001 From: "Urabe, Shyouhei" Date: Thu, 25 Apr 2019 15:03:18 +0900 Subject: avoid buffer overflow in vm_check_canary ec->cfp->iseq might not exist at the very beginning of a thread. ================================================================= ==82954==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7fc86f334810 at pc 0x55ceaf013125 bp 0x7ffe2eddbbf0 sp 0x7ffe2eddbbe8 READ of size 8 at 0x7fc86f334810 thread T0 #0 0x55ceaf013124 in vm_check_canary vm_insnhelper.c:217:24 #1 0x55ceaefb4796 in vm_push_frame vm_insnhelper.c:276:5 #2 0x55ceaf0124bd in th_init vm.c:2661:5 #3 0x55ceaf00d5eb in ruby_thread_init vm.c:2690:5 #4 0x55ceaf00d4b1 in rb_thread_alloc vm.c:2703:5 #5 0x55ceaef0038b in thread_s_new thread.c:872:20 #6 0x55ceaf04d8c1 in call_cfunc_m1 vm_insnhelper.c:2041:12 #7 0x55ceaf03118d in vm_call_cfunc_with_frame vm_insnhelper.c:2207:11 #8 0x55ceaf017985 in vm_call_cfunc vm_insnhelper.c:2225:12 #9 0x55ceaf01548b in vm_call_method_each_type vm_insnhelper.c:2560:9 #10 0x55ceaf014c96 in vm_call_method vm_insnhelper.c:2686:13 #11 0x55ceaefb5de4 in vm_call_general vm_insnhelper.c:2730:12 #12 0x55ceaf03c868 in vm_sendish vm_insnhelper.c:3623:11 #13 0x55ceaefc95bb in vm_exec_core insns.def:771:11 #14 0x55ceaf006700 in rb_vm_exec vm.c:1892:22 #15 0x55ceaf00acbf in rb_iseq_eval_main vm.c:2151:11 #16 0x55ceaea250ca in ruby_exec_internal eval.c:262:2 #17 0x55ceaea2498b in ruby_exec_node eval.c:326:12 #18 0x55ceaea247d0 in ruby_run_node eval.c:318:25 #19 0x55ceae88c486 in main main.c:42:9 #20 0x7fc874330b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310 #21 0x55ceae7e5289 in _start (miniruby+0x15f289) 0x7fc86f334810 is located 16 bytes to the right of 1048576-byte region [0x7fc86f234800,0x7fc86f334800) allocated by thread T0 here: #0 0x55ceae85d56d in malloc (miniruby+0x1d756d) #1 0x55ceaea71d12 in objspace_xmalloc0 gc.c:9416:5 #2 0x55ceaea71cd2 in ruby_xmalloc2_body gc.c:9623:12 #3 0x55ceaea7d09c in ruby_xmalloc2 gc.c:11479:12 #4 0x55ceaf00c3b7 in rb_thread_recycle_stack vm.c:2462:12 #5 0x55ceaf012256 in th_init vm.c:2656:29 #6 0x55ceaf00d5eb in ruby_thread_init vm.c:2690:5 #7 0x55ceaf00d4b1 in rb_thread_alloc vm.c:2703:5 #8 0x55ceaef0038b in thread_s_new thread.c:872:20 #9 0x55ceaf04d8c1 in call_cfunc_m1 vm_insnhelper.c:2041:12 #10 0x55ceaf03118d in vm_call_cfunc_with_frame vm_insnhelper.c:2207:11 #11 0x55ceaf017985 in vm_call_cfunc vm_insnhelper.c:2225:12 #12 0x55ceaf01548b in vm_call_method_each_type vm_insnhelper.c:2560:9 #13 0x55ceaf014c96 in vm_call_method vm_insnhelper.c:2686:13 #14 0x55ceaefb5de4 in vm_call_general vm_insnhelper.c:2730:12 #15 0x55ceaf03c868 in vm_sendish vm_insnhelper.c:3623:11 #16 0x55ceaefc95bb in vm_exec_core insns.def:771:11 #17 0x55ceaf006700 in rb_vm_exec vm.c:1892:22 #18 0x55ceaf00acbf in rb_iseq_eval_main vm.c:2151:11 #19 0x55ceaea250ca in ruby_exec_internal eval.c:262:2 #20 0x55ceaea2498b in ruby_exec_node eval.c:326:12 #21 0x55ceaea247d0 in ruby_run_node eval.c:318:25 #22 0x55ceae88c486 in main main.c:42:9 #23 0x7fc874330b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310 SUMMARY: AddressSanitizer: heap-buffer-overflow vm_insnhelper.c:217:24 in vm_check_canary Shadow bytes around the buggy address: 0x0ff98de5e8b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ff98de5e8c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ff98de5e8d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ff98de5e8e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ff98de5e8f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0ff98de5e900: fa fa[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0ff98de5e910: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0ff98de5e920: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0ff98de5e930: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0ff98de5e940: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0ff98de5e950: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==82954==ABORTING --- vm_insnhelper.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'vm_insnhelper.c') diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 2739e2bedf..b69eaf168b 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -214,6 +214,10 @@ vm_check_canary(const rb_execution_context_t *ec, VALUE *sp) if (! LIKELY(vm_stack_canary_was_born)) { return; /* :FIXME: isn't it rather fatal to enter this branch? */ } + else if ((VALUE *)reg_cfp == ec->vm_stack + ec->vm_stack_size) { + /* This is at the very beginning of a thread. cfp does not exist. */ + return; + } else if (! (iseq = GET_ISEQ())) { return; } -- cgit v1.2.3