public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Keith Seitz <keiths@redhat.com>
To: Felix Willgerodt <felix.willgerodt@intel.com>,
	gdb-patches@sourceware.org
Subject: Re: [PATCH 1/1] gdb: Fix segfault with a negative .dynamic section size
Date: Fri, 10 Nov 2023 11:00:20 -0800	[thread overview]
Message-ID: <b4fc0692-25c5-4fe5-a988-05af4f880870@redhat.com> (raw)
In-Reply-To: <20231110091627.2139626-1-felix.willgerodt@intel.com>

On 11/10/23 01:16, Felix Willgerodt wrote:
> Consider a binary with an erroneous size of the .dynamic section:
> 
> $ readelf a.out
> ...
>    [24] .dynamic          DYNAMIC          0000000000004c20  00003c20
>         000000fffffffa40  0000000000000010  WA       7     0     8
> ... >
> This binary causes a segfault in GDB, because we pass a negative
> value to alloca in gdb_bfd_scan_elf_dyntag.

Good catch. I especially welcome hardening patches like this,
so thank you!

> Alloca accepts size_t though, so it is really a big size that we
> pass.  I changed the code to heap allocation, as the size of the
> .dynamic section could be "too big" in a correct binary as well.
> That way GDB will assert on a negative size  value.

When you say "will assert", do you mean GDB will actually assert
and abort? If so, that doesn't seem particularly user friendly.

While I welcome the change to using the heap, I have to ask if
you've investigated pushing the fix down to BFD or otherwise attempted
to validate the section size?

Keith

> There should be no user visible change after this.
> ---
>   gdb/solib.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/gdb/solib.c b/gdb/solib.c
> index b9fb911a810..45a5309d199 100644
> --- a/gdb/solib.c
> +++ b/gdb/solib.c
> @@ -1546,7 +1546,8 @@ gdb_bfd_scan_elf_dyntag (const int desired_dyntag, bfd *abfd, CORE_ADDR *ptr,
>     /* Read in .dynamic from the BFD.  We will get the actual value
>        from memory later.  */
>     sect_size = bfd_section_size (sect);
> -  buf = bufstart = (gdb_byte *) alloca (sect_size);
> +  gdb::byte_vector buffer (sect_size);
> +  buf = bufstart = buffer.data ();
>     if (!bfd_get_section_contents (abfd, sect,
>   				 buf, 0, sect_size))
>       return 0;


  reply	other threads:[~2023-11-10 19:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-10  9:16 Felix Willgerodt
2023-11-10 19:00 ` Keith Seitz [this message]
2023-11-13  9:58   ` Willgerodt, Felix
2023-11-13 17:15     ` Tom Tromey
2023-11-14 15:41       ` Willgerodt, Felix
2023-11-14 16:29         ` Tom Tromey
2023-11-15  8:51           ` Willgerodt, Felix
2023-11-15 14:52             ` Tom Tromey
2023-11-13 15:59   ` Willgerodt, Felix
2023-11-13 17:16 ` 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=b4fc0692-25c5-4fe5-a988-05af4f880870@redhat.com \
    --to=keiths@redhat.com \
    --cc=felix.willgerodt@intel.com \
    --cc=gdb-patches@sourceware.org \
    /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).