Bug #22517
closedSome String methods return a UTF-16/32 string without room for its terminator
Description
Case mapping, String#reverse, String#succ, String#scrub, String#slice!, string interpolation and String#-@ can return a UTF-16 or UTF-32 string allocated with room for a 1-byte terminator only, so filling the 2- or 4-byte terminator later, such as in Process.setproctitle, writes beyond the allocation.
Each of the following lines makes AddressSanitizer report a heap-buffer-overflow in str_fill_term (no visible error on a release build).
s = ("a" * 1000).encode("UTF-32LE")
Process.setproctitle(s.downcase) # ASAN: heap-buffer-overflow, WRITE of size 4
Process.setproctitle(s.reverse) # ASAN: heap-buffer-overflow, WRITE of size 4
Process.setproctitle(s.succ) # ASAN: heap-buffer-overflow, WRITE of size 4
Process.setproctitle(s.dup.slice!(0, 900)) # ASAN: heap-buffer-overflow, WRITE of size 4
Process.setproctitle("#{s}#{s}") # ASAN: heap-buffer-overflow, WRITE of size 4
Process.setproctitle("#{s.b.force_encoding("US-ASCII")}#{s[0, 0]}") # ASAN: heap-buffer-overflow, WRITE of size 4
Process.setproctitle(s.b.tr("\0", "\xDC".b).force_encoding(s.encoding).scrub) # ASAN: heap-buffer-overflow, WRITE of size 4
Process.setproctitle(-Ractor.make_shareable(Class.new(String).new(s))) # ASAN: heap-buffer-overflow, READ of size 1
Updated by mame (Yusuke Endoh) 10 days ago
Updated by mame (Yusuke Endoh) 10 days ago
- Status changed from Open to Closed
Applied in changeset git|73db2bd4e98cab1e28f04f14cefd660bb5b75b84.
Allocate the result of String#reverse with the terminator of its encoding
rb_str_reverse allocated the result with room for a 1-byte terminator
and then set a UTF-16 or UTF-32 encoding with str_enc_copy_direct, which
does not adjust the room for the terminator.
[Bug #22517]
Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Updated by nagachika (Tomoyuki Chikanaga) 5 days ago
- Backport changed from 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: REQUIRED, 3.4: DONE, 4.0: REQUIRED
ruby_3_4 4133095cdbde46be196f27cd27b00c5b7a6541a2 merged revision(s) 73db2bd4e98cab1e28f04f14cefd660bb5b75b84, 00492f24408f7125e27fb7e4208b5dc5a57b3475, 6b35fcfea2ccf51187ef2ee86f4eb5918e07936b, 6b6d75fd8c014e88e07e0f4ea3de0abd0799227d, fc9d1cd671d4fbc1927bdb49f2f3b51264532ce1, a60de9cfe17be2fbc76b504c965b13593c8d2113, c2f4312b6b88f645a876f5c77932edf0cf7c41ff, 8d2bec3b97c3198e49387180b83ae34b60b364c9, c6cf749faabee026337a841b62b127dbf3feef11.
Updated by k0kubun (Takashi Kokubun) 3 days ago
- Backport changed from 3.3: REQUIRED, 3.4: DONE, 4.0: REQUIRED to 3.3: REQUIRED, 3.4: DONE, 4.0: DONE
ruby_4_0 a7c45148d00b598576c7cd4435a323e369f184ad, ec5b3f018a2b5b7477915727c925432403c116f9, 59dbb22e53016d45f2f56474c04378b05049809a, 1c564521af023823652ef37226123823eaf0ef50, 077d161551cb6c9e500810633c2287949620093d, aeb3d4cbc44af10efc10ca81e40aa84a65141c0a, 799fd1f5a7f34520cfca81114a7e001cd2e2afa1, b5e415dbae3fce9fd102ede2f9104fadcea2897d, 1b9731824189d114cb0e067eb9470c78b76b4564 merged revision(s) 73db2bd4e98cab1e28f04f14cefd660bb5b75b84, 00492f24408f7125e27fb7e4208b5dc5a57b3475, 6b35fcfea2ccf51187ef2ee86f4eb5918e07936b, 6b6d75fd8c014e88e07e0f4ea3de0abd0799227d, fc9d1cd671d4fbc1927bdb49f2f3b51264532ce1, a60de9cfe17be2fbc76b504c965b13593c8d2113, c2f4312b6b88f645a876f5c77932edf0cf7c41ff, 8d2bec3b97c3198e49387180b83ae34b60b364c9, c6cf749faabee026337a841b62b127dbf3feef11.