Skip to content

Fix use-after-free in Marshal.dump - #18974

Merged
peterzhu2118 merged 1 commit into
ruby:masterfrom
peterzhu2118:marshal-dump-uaf
Sep 24, 2026
Merged

peterzhu2118 merged 1 commit into
ruby:masterfrom
peterzhu2118:marshal-dump-uaf

Conversation

@peterzhu2118

Copy link
Copy Markdown
Member

When the string buffer reaches BUFSIZ (8192 bytes), Marshal.dump will call the write method on the IO object. This IO object can run arbitrary Ruby code. If the object is a string, it writes the length of the string followed by the bytes of the string. While writing the length, the IO object could modify the string which could cause an use-after-free when writing the bytes.

For example, the following script triggers an use-after-free in ASAN and outputs corrupted bytes when not using ASAN:

class W
  attr_reader :buf

  def write(s)
    @buf ||= ""
    @buf << s
    $str.clear
  end
end
$str = "B" * 1_000_000
obj = W.new
Marshal.dump(["A" * 8172, $str], obj)
loaded = Marshal.load(obj.buf)
p loaded[1]

When the string buffer reaches BUFSIZ (8192 bytes), Marshal.dump will call
the write method on the IO object. This IO object can run arbitrary Ruby
code. If the object is a string, it writes the length of the string followed
by the bytes of the string. While writing the length, the IO object could
modify the string which could cause an use-after-free when writing the
bytes.

For example, the following script triggers an use-after-free in ASAN and
outputs corrupted bytes when not using ASAN:

    class W
      attr_reader :buf

      def write(s)
        @buf ||= ""
        @buf << s
        $str.clear
      end
    end
    $str = "B" * 1_000_000
    obj = W.new
    Marshal.dump(["A" * 8172, $str], obj)
    loaded = Marshal.load(obj.buf)
    p loaded[1]
@peterzhu2118
peterzhu2118 merged commit 7d4b5ed into ruby:master Sep 24, 2026
132 checks passed
@peterzhu2118
peterzhu2118 deleted the marshal-dump-uaf branch September 24, 2026 08:46
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