Skip to content

Fix buffer overflow in chilled string warning in String#setbyte - #19913

Merged
peterzhu2118 merged 1 commit into
ruby:masterfrom
peterzhu2118:str-setbyte-chilled-string-in-warning
Nov 16, 2026
Merged

peterzhu2118 merged 1 commit into
ruby:masterfrom
peterzhu2118:str-setbyte-chilled-string-in-warning

Conversation

@peterzhu2118

Copy link
Copy Markdown
Member

If the source string is modified in the chilled string warning in String#setbyte, it will cause a buffer overflow because the string length is read before the warning.

For example, the following code will output an error in ASAN:

Warning[:deprecated] = true
module Warning
  def self.warn(msg, **kw) = $s.clear
end
# 1000-byte chilled literal (heap allocated)
eval("$s = '#{"a" * 1000}'")
$s.setbyte(999, 0x42)
p $s

Outputs:

ERROR: AddressSanitizer: use-after-poison on address 0x0001235e44d7 at pc 0x0001027170d4 bp 0x00016dc48f50 sp 0x00016dc48f48
WRITE of size 1 at 0x0001235e44d7 thread T0
    #0 0x0001027170d0 in rb_str_setbyte string.c:7405
    #1 0x0001028756a4 in vm_call_cfunc_with_frame_ vm_insnhelper.c:3936
    #2 0x000102860918 in vm_call_method_each_type vm_insnhelper.c:5041
    #3 0x00010285ffd4 in vm_call_method vm_insnhelper.c
    #4 0x00010281de04 in vm_sendish vm_insnhelper.c:6429
    #5 0x00010281de04 in vm_exec_core insns.def:912
    #6 0x000102809388 in rb_vm_exec vm.c:2917
    #7 0x0001023a0400 in rb_ec_exec_node eval.c:300
    #8 0x0001023a0228 in ruby_run_node eval.c:338
    #9 0x0001021b4c20 in rb_main main.c:42
    #10 0x0001021b4c20 in main main.c:62
    #11 0x0001916bcd78 in start+0x19f8 (dyld:arm64e+0x31d78)

If the source string is modified in the chilled string warning in String#setbyte,
it will cause a buffer overflow because the string length is read before
the warning.

For example, the following code will output an error in ASAN:

    Warning[:deprecated] = true
    module Warning
      def self.warn(msg, **kw) = $s.clear
    end
    # 1000-byte chilled literal (heap allocated)
    eval("$s = '#{"a" * 1000}'")
    $s.setbyte(999, 0x42)
    p $s

Outputs:

    ERROR: AddressSanitizer: use-after-poison on address 0x0001235e44d7 at pc 0x0001027170d4 bp 0x00016dc48f50 sp 0x00016dc48f48
    WRITE of size 1 at 0x0001235e44d7 thread T0
        #0 0x0001027170d0 in rb_str_setbyte string.c:7405
        #1 0x0001028756a4 in vm_call_cfunc_with_frame_ vm_insnhelper.c:3936
        ruby#2 0x000102860918 in vm_call_method_each_type vm_insnhelper.c:5041
        ruby#3 0x00010285ffd4 in vm_call_method vm_insnhelper.c
        ruby#4 0x00010281de04 in vm_sendish vm_insnhelper.c:6429
        ruby#5 0x00010281de04 in vm_exec_core insns.def:912
        ruby#6 0x000102809388 in rb_vm_exec vm.c:2917
        ruby#7 0x0001023a0400 in rb_ec_exec_node eval.c:300
        ruby#8 0x0001023a0228 in ruby_run_node eval.c:338
        ruby#9 0x0001021b4c20 in rb_main main.c:42
        ruby#10 0x0001021b4c20 in main main.c:62
        ruby#11 0x0001916bcd78 in start+0x19f8 (dyld:arm64e+0x31d78)
@peterzhu2118
peterzhu2118 merged commit 0b60b0e into ruby:master Nov 16, 2026
136 of 138 checks passed
@peterzhu2118
peterzhu2118 deleted the str-setbyte-chilled-string-in-warning branch November 16, 2026 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant