Skip to content

Commit ac3d934

Browse files
committed
Add checks to the MachOObjectFile() constructor to make sure load commands sizes
are the correct multiple. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@274798 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d0de2d9 commit ac3d934

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/Object/MachOObjectFile.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,25 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
297297
}
298298

299299
for (unsigned I = 0; I < LoadCommandCount; ++I) {
300+
if (is64Bit()) {
301+
if (Load.C.cmdsize % 8 != 0) {
302+
// We have a hack here to allow 64-bit Mach-O core files to have
303+
// LC_THREAD commands that are only a multiple of 4 and not 8 to be
304+
// allowed since the macOS kernel produces them.
305+
if (getHeader().filetype != MachO::MH_CORE ||
306+
Load.C.cmd != MachO::LC_THREAD || Load.C.cmdsize % 4) {
307+
Err = malformedError("load command " + Twine(I) + " cmdsize not a "
308+
"multiple of 8");
309+
return;
310+
}
311+
}
312+
} else {
313+
if (Load.C.cmdsize % 4 != 0) {
314+
Err = malformedError("load command " + Twine(I) + " cmdsize not a "
315+
"multiple of 4");
316+
return;
317+
}
318+
}
300319
LoadCommands.push_back(Load);
301320
if (Load.C.cmd == MachO::LC_SYMTAB) {
302321
// Multiple symbol tables

test/Object/macho-invalid.test

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ RUN: | FileCheck -check-prefix SMALL-LOADC-SIZE-1 %s
2424
SMALL-LOADC-SIZE-1: truncated or malformed object (load command 1 with size less than 8 bytes)
2525

2626
RUN: not llvm-objdump -private-headers %p/Inputs/macho-invalid-too-small-segment-load-command 2>&1 \
27-
RUN: | FileCheck -check-prefix SMALL-SEGLOADC-SIZE %s
27+
RUN: | FileCheck -check-prefix MULTIPLE-NOT-4 %s
28+
MULTIPLE-NOT-4: truncated or malformed object (load command 0 cmdsize not a multiple of 4)
29+
2830
RUN: not llvm-objdump -private-headers %p/Inputs/macho-invalid-too-small-segment-load-command.1 2>&1 \
2931
RUN: | FileCheck -check-prefix SMALL-SEGLOADC-SIZE %s
3032
SMALL-SEGLOADC-SIZE: truncated or malformed object (load command 0 LC_SEGMENT cmdsize too small)
33+
3134
RUN: not llvm-objdump -private-headers %p/Inputs/macho64-invalid-too-small-segment-load-command 2>&1 \
32-
RUN: | FileCheck -check-prefix SMALL-SEGLOADC-SIZE-64 %s
33-
SMALL-SEGLOADC-SIZE-64: truncated or malformed object (load command 0 LC_SEGMENT_64 cmdsize too small)
35+
RUN: | FileCheck -check-prefix MULTIPLE-NOT-8 %s
36+
MULTIPLE-NOT-8: truncated or malformed object (load command 0 cmdsize not a multiple of 8)
3437

3538
RUN: not llvm-objdump -private-headers %p/Inputs/macho-invalid-no-size-for-sections 2>&1 \
3639
RUN: | FileCheck -check-prefix TOO-MANY-SECTS %s

0 commit comments

Comments
 (0)