Skip to content

Commit 80df642

Browse files
committed
[BinaryFormat] Fix out of bounds read.
Found by OSS-FUZZ! https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=3220 llvm-svn: 312238
1 parent bfcac0b commit 80df642

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

llvm/lib/BinaryFormat/Magic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ file_magic llvm::identify_magic(StringRef Magic) {
182182
break;
183183

184184
case 'M': // Possible MS-DOS stub on Windows PE file
185-
if (startswith(Magic, "MZ")) {
185+
if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
186186
uint32_t off = read32le(Magic.data() + 0x3c);
187187
// PE/COFF file, either EXE or DLL.
188188
if (off < Magic.size() &&

llvm/unittests/BinaryFormat/TestFileMagic.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const char windows_resource[] =
8080
"\x00\x00\x00\x00\x020\x00\x00\x00\xff\xff\x00\x00\xff\xff\x00\x00";
8181
const char macho_dynamically_linked_shared_lib_stub[] =
8282
"\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
83+
const char ms_dos_stub_broken[] = "\x4d\x5a\x20\x20";
8384

8485
TEST_F(MagicTest, Magic) {
8586
struct type {
@@ -108,7 +109,9 @@ TEST_F(MagicTest, Magic) {
108109
DEFINE(macho_dynamically_linked_shared_lib_stub),
109110
DEFINE(macho_dsym_companion),
110111
DEFINE(macho_kext_bundle),
111-
DEFINE(windows_resource)
112+
DEFINE(windows_resource),
113+
{"ms_dos_stub_broken", ms_dos_stub_broken, sizeof(ms_dos_stub_broken),
114+
file_magic::unknown},
112115
#undef DEFINE
113116
};
114117

0 commit comments

Comments
 (0)