|
| 1 | +From 58abdf887821a5da09ba184c6e400a3bc5cccd5a Mon Sep 17 00:00:00 2001 |
| 2 | +From: Keith Seitz < [email protected]> |
| 3 | +Date: Wed, 2 Aug 2023 08:35:11 -0700 |
| 4 | +Subject: [PATCH] Verify COFF symbol stringtab offset |
| 5 | + |
| 6 | +This patch addresses an issue with malformed/fuzzed debug information that |
| 7 | +was recently reported in gdb/30639. That bug specifically deals with |
| 8 | +an ASAN issue, but the reproducer provided by the reporter causes a |
| 9 | +another failure outside of ASAN: |
| 10 | + |
| 11 | +$ ./gdb --data-directory data-directory -nx -q UAF_2 |
| 12 | +Reading symbols from /home/keiths/UAF_2... |
| 13 | + |
| 14 | + |
| 15 | +Fatal signal: Segmentation fault |
| 16 | +----- Backtrace ----- |
| 17 | +0x59a53a gdb_internal_backtrace_1 |
| 18 | + ../../src/gdb/bt-utils.c:122 |
| 19 | +0x59a5dd _Z22gdb_internal_backtracev |
| 20 | + ../../src/gdb/bt-utils.c:168 |
| 21 | +0x786380 handle_fatal_signal |
| 22 | + ../../src/gdb/event-top.c:889 |
| 23 | +0x7864ec handle_sigsegv |
| 24 | + ../../src/gdb/event-top.c:962 |
| 25 | +0x7ff354c5fb6f ??? |
| 26 | +0x611f9a process_coff_symbol |
| 27 | + ../../src/gdb/coffread.c:1556 |
| 28 | +0x611025 coff_symtab_read |
| 29 | + ../../src/gdb/coffread.c:1172 |
| 30 | +0x60f8ff coff_read_minsyms |
| 31 | + ../../src/gdb/coffread.c:549 |
| 32 | +0x60fe4b coff_symfile_read |
| 33 | + ../../src/gdb/coffread.c:698 |
| 34 | +0xbde0f6 read_symbols |
| 35 | + ../../src/gdb/symfile.c:772 |
| 36 | +0xbde7a3 syms_from_objfile_1 |
| 37 | + ../../src/gdb/symfile.c:966 |
| 38 | +0xbde867 syms_from_objfile |
| 39 | + ../../src/gdb/symfile.c:983 |
| 40 | +0xbded42 symbol_file_add_with_addrs |
| 41 | + ../../src/gdb/symfile.c:1086 |
| 42 | +0xbdf083 _Z24symbol_file_add_from_bfdRKN3gdb7ref_ptrI3bfd18gdb_bfd_ref_policyEEPKc10enum_flagsI16symfile_add_flagEPSt6vectorI14other_sectionsSaISC_EES8_I12objfile_flagEP7objfile |
| 43 | + ../../src/gdb/symfile.c:1166 |
| 44 | +0xbdf0d2 _Z15symbol_file_addPKc10enum_flagsI16symfile_add_flagEPSt6vectorI14other_sectionsSaIS5_EES1_I12objfile_flagE |
| 45 | + ../../src/gdb/symfile.c:1179 |
| 46 | +0xbdf197 symbol_file_add_main_1 |
| 47 | + ../../src/gdb/symfile.c:1203 |
| 48 | +0xbdf13e _Z20symbol_file_add_mainPKc10enum_flagsI16symfile_add_flagE |
| 49 | + ../../src/gdb/symfile.c:1194 |
| 50 | +0x90f97f symbol_file_add_main_adapter |
| 51 | + ../../src/gdb/main.c:549 |
| 52 | +0x90f895 catch_command_errors |
| 53 | + ../../src/gdb/main.c:518 |
| 54 | +0x9109b6 captured_main_1 |
| 55 | + ../../src/gdb/main.c:1203 |
| 56 | +0x910fc8 captured_main |
| 57 | + ../../src/gdb/main.c:1310 |
| 58 | +0x911067 _Z8gdb_mainP18captured_main_args |
| 59 | + ../../src/gdb/main.c:1339 |
| 60 | +0x418c71 main |
| 61 | + ../../src/gdb/gdb.c:39 |
| 62 | +--------------------- |
| 63 | +A fatal error internal to GDB has been detected, further |
| 64 | +debugging is not possible. GDB will now terminate. |
| 65 | + |
| 66 | +This is a bug, please report it. For instructions, see: |
| 67 | +<https://www.gnu.org/software/gdb/bugs/>. |
| 68 | + |
| 69 | +Segmentation fault (core dumped) |
| 70 | + |
| 71 | +The issue here is that the COFF offset for the fuzzed symbol's |
| 72 | +name is outside the string table. That is, the offset is greater |
| 73 | +than the actual string table size. |
| 74 | + |
| 75 | +coffread.c:getsymname actually contains a FIXME about this, and that's |
| 76 | +what I've chosen to address to fix this issue, following what is done |
| 77 | +in the DWARF reader: |
| 78 | + |
| 79 | +$ ./gdb --data-directory data-directory -nx -q UAF_2 |
| 80 | +Reading symbols from /home/keiths/UAF_2... |
| 81 | +COFF Error: string table offset (256) outside string table (length 0) |
| 82 | +(gdb) |
| 83 | + |
| 84 | +Unfortunately, I haven't any idea how else to test this patch since |
| 85 | +COFF is not very common anymore. GCC removed support for it five |
| 86 | +years ago with GCC 8. |
| 87 | +--- |
| 88 | + gdb/coffread.c | 7 +++++-- |
| 89 | + 1 file changed, 5 insertions(+), 2 deletions(-) |
| 90 | + |
| 91 | +diff --git a/gdb/coffread.c b/gdb/coffread.c |
| 92 | +index f8e14d8ad93..ae7632d49cb 100644 |
| 93 | +--- a/gdb/coffread.c |
| 94 | ++++ b/gdb/coffread.c |
| 95 | +@@ -159,6 +159,7 @@ static file_ptr linetab_offset; |
| 96 | + static file_ptr linetab_size; |
| 97 | + |
| 98 | + static char *stringtab = NULL; |
| 99 | ++static long stringtab_length = 0; |
| 100 | + |
| 101 | + extern void stabsread_clear_cache (void); |
| 102 | + |
| 103 | +@@ -1303,6 +1304,7 @@ init_stringtab (bfd *abfd, file_ptr offset, gdb::unique_xmalloc_ptr<char> *stora |
| 104 | + /* This is in target format (probably not very useful, and not |
| 105 | + currently used), not host format. */ |
| 106 | + memcpy (stringtab, lengthbuf, sizeof lengthbuf); |
| 107 | ++ stringtab_length = length; |
| 108 | + if (length == sizeof length) /* Empty table -- just the count. */ |
| 109 | + return 0; |
| 110 | + |
| 111 | +@@ -1322,8 +1324,9 @@ getsymname (struct internal_syment *symbol_entry) |
| 112 | + |
| 113 | + if (symbol_entry->_n._n_n._n_zeroes == 0) |
| 114 | + { |
| 115 | +- /* FIXME: Probably should be detecting corrupt symbol files by |
| 116 | +- seeing whether offset points to within the stringtab. */ |
| 117 | ++ if (symbol_entry->_n._n_n._n_offset > stringtab_length) |
| 118 | ++ error (_("COFF Error: string table offset (%ld) outside string table (length %ld)"), |
| 119 | ++ symbol_entry->_n._n_n._n_offset, stringtab_length); |
| 120 | + result = stringtab + symbol_entry->_n._n_n._n_offset; |
| 121 | + } |
| 122 | + else |
| 123 | +-- |
| 124 | +2.43.5 |
0 commit comments