Skip to content

Commit ee56af2

Browse files
committed
Fix out-of-bounds read in objdump reloc section handling
BinaryReaderObjdump::OnRelocCount ignored the error returned by BinaryReaderObjdumpBase::OnRelocCount when the section_index was invalid. This caused the function to proceed to GetSectionName which called GetSectionStart with BinarySection::Invalid (~0), resulting in an out-of-bounds read on the stack-allocated section_starts_ array of size kBinarySectionCount (14). Propagate the error via CHECK_RESULT so that the out-of-bounds access is never reached. Add a regression test with a crafted wasm binary containing a reloc custom section that references a non-existent section index.
1 parent d09cffc commit ee56af2

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ if (BUILD_TESTS)
712712

713713
# wabt-unittests
714714
set(UNITTESTS_SRCS
715+
src/binary-reader-objdump.cc
715716
src/test-binary-reader.cc
716717
src/test-interp.cc
717718
src/test-intrusive-list.cc

src/binary-reader-objdump.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,7 @@ Result BinaryReaderObjdump::OnDylinkNeeded(std::string_view so_name) {
21612161
}
21622162

21632163
Result BinaryReaderObjdump::OnRelocCount(Index count, Index section_index) {
2164-
BinaryReaderObjdumpBase::OnRelocCount(count, section_index);
2164+
CHECK_RESULT(BinaryReaderObjdumpBase::OnRelocCount(count, section_index));
21652165
PrintDetails(" - relocations for section: %d (" PRIstringview ") [%d]\n",
21662166
section_index,
21672167
WABT_PRINTF_STRING_VIEW_ARG(GetSectionName(section_index)),

src/test-binary-reader.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "gtest/gtest.h"
1818

1919
#include "wabt/binary-reader-nop.h"
20+
#include "wabt/binary-reader-objdump.h"
2021
#include "wabt/binary-reader.h"
2122
#include "wabt/leb128.h"
2223
#include "wabt/opcode.h"
@@ -73,3 +74,39 @@ TEST(BinaryReader, DisabledOpcodes) {
7374
<< "Got error message: " << message;
7475
}
7576
}
77+
78+
TEST(BinaryReaderObjdump, RelocInvalidSectionIndex) {
79+
// Minimal wasm with a reloc section referencing a section_index that exceeds
80+
// the actual number of sections. Before the fix, the derived-class
81+
// OnRelocCount ignored the error returned by the base class and proceeded
82+
// to call GetSectionName with BinarySection::Invalid, causing an
83+
// out-of-bounds read on the section_starts_ array.
84+
//
85+
// The fix propagates the base class error via CHECK_RESULT so that the
86+
// out-of-bounds GetSectionName call is never reached. The overall result
87+
// is still Ok because custom section errors are not fatal by default.
88+
89+
uint8_t data[] = {
90+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, // magic + version
91+
92+
// Custom section pretending to be "reloc." (section id 0)
93+
0x00, // section code: custom
94+
0x0a, // section size: 10 bytes
95+
// section name "reloc." (length-prefixed)
96+
0x06, // name length
97+
'r', 'e', 'l', 'o', 'c', '.', 0xff,
98+
0x01, // section_index = 255 (invalid, LEB128)
99+
0x00, // relocation count = 0
100+
};
101+
102+
ObjdumpOptions options;
103+
memset(&options, 0, sizeof(options));
104+
options.mode = ObjdumpMode::Details;
105+
options.details = true;
106+
ObjdumpState state;
107+
108+
// Should not crash. Custom section errors are suppressed, so the overall
109+
// result is Ok even though the reloc section itself fails.
110+
Result result = ReadBinaryObjdump(data, sizeof(data), &options, &state);
111+
EXPECT_EQ(Result::Ok, result);
112+
}

test/binary/bad-relocs.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Section Details:
2020

2121
Custom:
2222
- name: "reloc.BAD"
23-
- relocations for section: 99 () [0]
2423

2524
Code Disassembly:
2625

0 commit comments

Comments
 (0)