From: mame@... Date: 2019-09-08T00:45:56+00:00 Subject: [ruby-core:94833] [Ruby master Bug#16151] [PATCH] Fix a class of fstring related use-after-free Issue #16151 has been updated by mame (Yusuke Endoh). Hi @alanwu , thank you for taking time to this issue. I may be wrong, but I think that the code path you pointed is benign itself. It calls `str_replace_shared_without_enc` which may free the string buffer. However, it does not free the buffer if it is STR_NOFREE. I guess that we need to set STR_NOFREE flag somewhere, though I cannot understand at all what is STR_NOFREE. Looks like `str_new_frozen` creates a broken shared pair. https://github.com/ruby/ruby/blob/e9bc8b35c6c860e227627e3b0aab86b4f51c59af/string.c#L1303-L1312 It allocates `str`, copies the contents from `orig` to `str`, make the buffer shared, but does not set the STR_NOFREE flag to `str`. The following patch fixes the two examples you showed. But it would produce a new memory-leak issue, I guess. @nobu, what do you think? ``` diff --git a/string.c b/string.c index 05ce0ed8d6..d810f252e7 100644 --- a/string.c +++ b/string.c @@ -1305,7 +1305,7 @@ str_new_frozen(VALUE klass, VALUE orig) RSTRING(str)->as.heap.len = RSTRING_LEN(orig); RSTRING(str)->as.heap.ptr = RSTRING_PTR(orig); RSTRING(str)->as.heap.aux.capa = RSTRING(orig)->as.heap.aux.capa; - RBASIC(str)->flags |= RBASIC(orig)->flags & STR_NOFREE; + RBASIC(str)->flags |= STR_NOFREE; RBASIC(orig)->flags &= ~STR_NOFREE; STR_SET_SHARED(orig, str); if (klass == 0) ``` Honestly, fstring looks ill-designed to me. ---------------------------------------- Bug #16151: [PATCH] Fix a class of fstring related use-after-free https://bugs.ruby-lang.org/issues/16151#change-81453 * Author: alanwu (Alan Wu) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.7.0dev (2019-09-07T18:26:35Z master e9bc8b35c6) [x86_64-linux] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- Pull request: https://github.com/ruby/ruby/pull/2435 ## The bug Run the following against master(e9bc8b3) to observe use-after-free: ```ruby -('a' * 30).force_encoding(Encoding::ASCII) a = ('a' * 30).force_encoding(Encoding::ASCII).taint t = Thread.new{} t.name = a eval('', binding, t.name) p a ``` ```ruby -('a' * 30).force_encoding(Encoding::ASCII) a = ('a' * 30).force_encoding(Encoding::ASCII).taint require 'ripper' ripper = Ripper.new("", a) eval('', binding, ripper.filename) p a ``` There may be other cases in the standard library or in the wild. ## Background When a string has both `STR_NOEMBED` and `STR_SHARED` set, it relies on a different string for its buffer. I will refer to strings that are depended upon as "shared roots". Shared roots are frozen and have the `STR_SHARED` unset. This is a bit unintuitive to me. A name for `STR_SHARED` that makes more sense to me would be `STR_BUFFER_ELSEWHERE`. ## What went wrong It is not safe to free the buffer of a shared root while it has dependants. The root and its dependants use the same buffer. As such, it is only safe to free the shared buffer when all users are unreachable on the heap. ## The Fix `rb_fstring` has a code path that frees and replaces the buffer of its input. Using this code path on the shared root of dependant strings sets up use-after-free. This patch removes the problematic code path as no tests require said buffer replacement functionality. Additionally, there has been three other issues that steam from this particular code path. See #15926, #15916 and #16136 --- I used @mame's commit in #16136 as the starting point for this investigation. Thank you! -- https://bugs.ruby-lang.org/ Unsubscribe: