llvm.org GIT mirror llvm / 10b2fe5
[llvm-objcopy] - Emit error and don't crash if program header reaches past end of file. This is https://bugs.llvm.org/show_bug.cgi?id=42122. If an object file has a size less than program header's file [offset + size] (i.e. if we have overflow), llvm-objcopy crashes instead of reporting a error. The patch fixes this issue. Differential revision: https://reviews.llvm.org/D62898 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362778 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar 4 months ago
2 changed file(s) with 50 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 ## In this case, we have a program header with a file size that
1 ## overflows the binary size. Check llvm-objcopy doesn't crash
2 ## and report this error properly.
3
4 # RUN: yaml2obj --docnum=1 %s -o %t1.o
5 # RUN: not llvm-objcopy %t1.o 2>&1 | FileCheck %s --check-prefix=ERR1
6 # ERR1: error: program header with offset 0x1b8 and file size 0x100000 goes past the end of the file
7
8 --- !ELF
9 FileHeader:
10 Class: ELFCLASS64
11 Data: ELFDATA2LSB
12 Type: ET_EXEC
13 Machine: EM_X86_64
14 Sections:
15 - Name: .foo
16 Type: SHT_PROGBITS
17 ProgramHeaders:
18 - Type: PT_LOAD
19 FileSize: 0x100000
20 Sections:
21 - Section: .foo
22
23 ## A similar case, but now the p_offset property of the program header is too large.
24
25 # RUN: yaml2obj --docnum=2 %s -o %t2.o
26 # RUN: not llvm-objcopy %t2.o 2>&1 | FileCheck %s --check-prefix=ERR2
27 # ERR2: error: program header with offset 0x100000 and file size 0x1 goes past the end of the file
28
29 --- !ELF
30 FileHeader:
31 Class: ELFCLASS64
32 Data: ELFDATA2LSB
33 Type: ET_EXEC
34 Machine: EM_X86_64
35 Sections:
36 - Name: .foo
37 Type: SHT_PROGBITS
38 Size: 1
39 ProgramHeaders:
40 - Type: PT_LOAD
41 Offset: 0x100000
42 FileSize: 1
43 Sections:
44 - Section: .foo
11031103 template void ELFBuilder::readProgramHeaders() {
11041104 uint32_t Index = 0;
11051105 for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
1106 if (Phdr.p_offset + Phdr.p_filesz > ElfFile.getBufSize())
1107 error("program header with offset 0x" + Twine::utohexstr(Phdr.p_offset) +
1108 " and file size 0x" + Twine::utohexstr(Phdr.p_filesz) +
1109 " goes past the end of the file");
1110
11061111 ArrayRef Data{ElfFile.base() + Phdr.p_offset,
11071112 (size_t)Phdr.p_filesz};
11081113 Segment &Seg = Obj.addSegment(Data);