public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Alexandra Petlanova Hajkova <ahajkova@redhat.com>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH] Fix value chain use-after-free
Date: Tue, 14 Feb 2023 18:05:01 +0100	[thread overview]
Message-ID: <CAJVr-EOwRS06i_YOP2oDchj046-z4onxyj+4rCRJsx41uk4MKg@mail.gmail.com> (raw)
In-Reply-To: <20230211010805.3700057-1-tom@tromey.com>

[-- Attachment #1: Type: text/plain, Size: 2575 bytes --]

On Sat, Feb 11, 2023 at 2:08 AM Tom Tromey <tom@tromey.com> wrote:

> Hannes filed a bug showing a crash, where a pretty-printer written in
> Python could cause a use-after-free.  He sent a patch, but I thought a
> different approach was needed.
>
> In a much earlier patch (see bug #12533), we changed the Python code
> to release new values from the value chain when constructing a
> gdb.Value.  The rationale for this is that if you write a command that
> does a lot of computations in a loop, all the values will be kept live
> by the value chain, resulting in gdb using a large amount of memory.
>
> However, suppose a value is passed to Python from some code in gdb
> that needs to use the value after the call into Python.  In this
> scenario, value_to_value_object will still release the value -- and
> because gdb code doesn't generally keep strong references to values (a
> consequence of the ancient decision to use the value chain to avoid
> memory management), this will result in a use-after-free.
>
> This scenario can happen, as it turns out, when a value is passed to
> Python for pretty-printing.  Now, normally this route boxes the value
> via value_to_value_object_no_release, avoiding the problematic release
> from the value chain.  However, if you then call Value.cast, the
> underlying value API might return the same value, when is then
> released from the chain.
>
> This patch fixes the problem by changing how value boxing is done.
> value_to_value_object no longer removes a value from the chain.
> Instead, every spot in gdb that might construct new values uses a
> scoped_value_mark to ensure that the requirements of bug #12533 are
> met.  And, because incoming values aren't ever released from the chain
> (the Value.cast one comes earlier on the chain than the
> scoped_value_mark), the bug can no longer occur.  (Note that many
> spots in the Python layer already take this approach, so not many
> places needed to be touched.)
>
> In the future I think we should replace the use of raw "value *" with
> value_ref_ptr pretty much everywhere.  This will ensure lifetime
> safety throughout gdb.
>
> The test case in this patch comes from Hannes' original patch.  I only
> made a trivial ("require") change to it.  However, while this fails
> for him, I can't make it fail on this machine; nevertheless, he tried
> my patch and reported the bug as being fixed.
>
> 2.39.1
>
>
I think it's a good solution and I can confirm gdb.python/py-pp-cast.exp
passes for me. But I wasn't able to apply the patch cleanly, I think it
needs to be rebased.

  reply	other threads:[~2023-02-14 17:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-11  1:08 Tom Tromey
2023-02-14 17:05 ` Alexandra Petlanova Hajkova [this message]
2023-02-14 22:16   ` Tom Tromey
2023-02-27 22:45 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJVr-EOwRS06i_YOP2oDchj046-z4onxyj+4rCRJsx41uk4MKg@mail.gmail.com \
    --to=ahajkova@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).