public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/1] gdb: Fix segfault in "start" with fuzzed binary
@ 2024-02-19 15:19 Felix Willgerodt
  2024-02-20 11:57 ` Andrew Burgess
  0 siblings, 1 reply; 5+ messages in thread
From: Felix Willgerodt @ 2024-02-19 15:19 UTC (permalink / raw)
  To: gdb-patches

I found this while playing around with fuzzing.  Though I did fuzz with
"-ex start", so this isn't a security issue.  But any comments welcome.

What I observed is this segfault:

~~~
Thread 1 "gdb-up" received signal SIGSEGV, Segmentation fault.
0x0000555555d8883a in objfile::arch (this=0x0) at /user/sources/gdb/gdb/objfiles.h:509
509         return per_bfd->gdbarch;
(gdb) bt 5
    filter_=..., cond_string_=..., extra_string_=..., disposition_=disp_del, thread_=-1, simd_lane_num_=-1, task_=-1, inferior_=1, ignore_count_=0,
    from_tty=0, enabled_=1, flags=0, display_canonical_=0) at /user/sources/gdb/gdb/breakpoint.c:8960
(More stack frames follow...)
(gdb) frame 1
7645        return sal.section->objfile->arch ();
(gdb) p sal.section
$1 = (obj_section *) 0x555558828bf8
(gdb) p *sal.section
$2 = {the_bfd_section = 0x0, objfile = 0x0, ovly_mapped = 0}
~~~

The parsed binary has a weird .text section header:

  [14] .text             LOUSER+0x6c0000  0000000000001040  00001040
       00000000000000f9  0000000000000000  WX       0     0     16

It is marked as writeable (I think) and the type is also different.  For
reference here is the one from the normal binary that I started fuzzing with:

  [14] .text             PROGBITS         0000000000001040  00001040
       00000000000000f9  0000000000000000  AX       0     0     16

I couldn't find where GDB actually parses this.  Nor could I figure out why
the section has a nullptr as objfile.  But after this patch, the segfault
is gone and the output seems reasonable for a broken binary:

Temporary breakpoint 1 at 0x1040: file main.c, line 1.
Starting program: a.out
/bin/bash: line 1: a.out: Permission denied
/bin/bash: line 1: exec: a.out: cannot execute: Permission denied
During startup program exited with code 126.
(gdb)

In addition this changes the function to use explicit comparisons.
---
 gdb/breakpoint.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 5f05657a8b3..0313b920ca6 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -7641,9 +7641,9 @@ set_breakpoint_location_function (struct bp_location *loc)
 struct gdbarch *
 get_sal_arch (struct symtab_and_line sal)
 {
-  if (sal.section)
+  if (sal.section != nullptr && sal.section->objfile != nullptr)
     return sal.section->objfile->arch ();
-  if (sal.symtab)
+  if (sal.symtab != nullptr)
     return sal.symtab->compunit ()->objfile ()->arch ();
 
   return NULL;
-- 
2.34.1

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


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

end of thread, other threads:[~2024-04-08  9:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-19 15:19 [PATCH 1/1] gdb: Fix segfault in "start" with fuzzed binary Felix Willgerodt
2024-02-20 11:57 ` Andrew Burgess
2024-02-20 14:13   ` Willgerodt, Felix
2024-04-07 13:57     ` Andrew Burgess
2024-04-08  9:20       ` Willgerodt, Felix

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