public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PUSHED] gdb/python: Avoid use after free in py-tui.c
@ 2020-06-05 18:23 Andrew Burgess
  2020-06-05 18:26 ` Christian Biesinger
  2020-06-05 18:37 ` Tom Tromey
  0 siblings, 2 replies; 4+ messages in thread
From: Andrew Burgess @ 2020-06-05 18:23 UTC (permalink / raw)
  To: gdb-patches

When setting the window title of a tui frame we do this:

  gdb::unique_xmalloc_ptr<char> value
    = python_string_to_host_string (<python-object>);
  ...
  win->window->title = value.get ();

The problem here is that 'get ()' only borrows the pointer from value,
when value goes out of scope the pointer will be freed.  As a result,
the tui frame will be left with a pointer to undefined memory
contents.

Instead we should be using 'value.release ()' to take ownership of the
pointer from value.

gdb/ChangeLog:

	* python/py-tui.c (gdbpy_tui_set_title): Use release, not get, to
	avoid use after free.
---
 gdb/ChangeLog       | 5 +++++
 gdb/python/py-tui.c | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c
index ca88f85eb9f..f2c03395a0b 100644
--- a/gdb/python/py-tui.c
+++ b/gdb/python/py-tui.c
@@ -433,7 +433,7 @@ gdbpy_tui_set_title (PyObject *self, PyObject *newvalue, void *closure)
   if (value == nullptr)
     return -1;
 
-  win->window->title = value.get ();
+  win->window->title = value.release ();
   return 0;
 }
 
-- 
2.25.4


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PUSHED] gdb/python: Avoid use after free in py-tui.c
  2020-06-05 18:23 [PUSHED] gdb/python: Avoid use after free in py-tui.c Andrew Burgess
@ 2020-06-05 18:26 ` Christian Biesinger
  2020-06-05 18:37 ` Tom Tromey
  1 sibling, 0 replies; 4+ messages in thread
From: Christian Biesinger @ 2020-06-05 18:26 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

On Fri, Jun 5, 2020 at 1:23 PM Andrew Burgess
<andrew.burgess@embecosm.com> wrote:

> -  win->window->title = value.get ();
> +  win->window->title = value.release ();

Does the old title need to be freed here?

Christian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PUSHED] gdb/python: Avoid use after free in py-tui.c
  2020-06-05 18:23 [PUSHED] gdb/python: Avoid use after free in py-tui.c Andrew Burgess
  2020-06-05 18:26 ` Christian Biesinger
@ 2020-06-05 18:37 ` Tom Tromey
  2020-06-05 20:14   ` Andrew Burgess
  1 sibling, 1 reply; 4+ messages in thread
From: Tom Tromey @ 2020-06-05 18:37 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

>>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:

Andrew> When setting the window title of a tui frame we do this:
Andrew>   gdb::unique_xmalloc_ptr<char> value
Andrew>     = python_string_to_host_string (<python-object>);
Andrew>   ...
Andrew> win-> window->title = value.get ();

Andrew> The problem here is that 'get ()' only borrows the pointer from value,
Andrew> when value goes out of scope the pointer will be freed.  As a result,
Andrew> the tui frame will be left with a pointer to undefined memory
Andrew> contents.

This does not make sense to me, because tui_win_info::title is a
std::string.

Andrew> Instead we should be using 'value.release ()' to take ownership of the
Andrew> pointer from value.

I suspect this introduces a memory leak instead.

Tom

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PUSHED] gdb/python: Avoid use after free in py-tui.c
  2020-06-05 18:37 ` Tom Tromey
@ 2020-06-05 20:14   ` Andrew Burgess
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Burgess @ 2020-06-05 20:14 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

* Tom Tromey <tom@tromey.com> [2020-06-05 12:37:12 -0600]:

> >>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:
> 
> Andrew> When setting the window title of a tui frame we do this:
> Andrew>   gdb::unique_xmalloc_ptr<char> value
> Andrew>     = python_string_to_host_string (<python-object>);
> Andrew>   ...
> Andrew> win-> window->title = value.get ();
> 
> Andrew> The problem here is that 'get ()' only borrows the pointer from value,
> Andrew> when value goes out of scope the pointer will be freed.  As a result,
> Andrew> the tui frame will be left with a pointer to undefined memory
> Andrew> contents.
> 
> This does not make sense to me, because tui_win_info::title is a
> std::string.
> 
> Andrew> Instead we should be using 'value.release ()' to take ownership of the
> Andrew> pointer from value.
> 
> I suspect this introduces a memory leak instead.

My apologies.  I have reverted the commit.

Sorry for the noise.

Thanks,
Andrew

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-06-05 20:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-05 18:23 [PUSHED] gdb/python: Avoid use after free in py-tui.c Andrew Burgess
2020-06-05 18:26 ` Christian Biesinger
2020-06-05 18:37 ` Tom Tromey
2020-06-05 20:14   ` Andrew Burgess

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).