-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix buffer overrun in NT_GNU_PROPERTY_TYPE_0 parser (#538)
* Fix buffer overrun in NT_GNU_PROPERTY_TYPE_0 parser The `iter_notes` method has code to parse `NT_GNU_PROPERTY_TYPE_0` type notes. The contents of the note are interpreted as an array of `elffile.structs.Elf_Prop`s. There was a bug where it would keep on parsing from the stream until the end of the *segment or section*. This is only correct if the note would be the last in the segment/section. In general, it should stop parsing until it reaches the end of the note's data buffer. This PR fixes this bug. Fixes: #534 * Add comment explaining n_descsz
- Loading branch information
1 parent
c04e8fa
commit c359508
Showing
6 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
test/testfiles_for_unittests/note_after_gnu_property/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
main.elf: main.c link.ld | ||
gcc -O0 main.c -T link.ld -Wl,--build-id=none -o main.elf |
34 changes: 34 additions & 0 deletions
34
test/testfiles_for_unittests/note_after_gnu_property/link.ld
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
PHDRS | ||
{ | ||
elf_headers PT_LOAD FILEHDR PHDRS FLAGS(PF_R) ; | ||
text PT_LOAD FLAGS(PF_R | PF_X) ; | ||
data PT_LOAD FLAGS(PF_R | PF_W) ; | ||
bss PT_LOAD FLAGS(PF_R | PF_W) ; | ||
note PT_NOTE FLAGS(PF_R) ; | ||
} | ||
|
||
SECTIONS | ||
{ | ||
.text : { | ||
*(.text .text.* .gnu.linkonce.t.*) | ||
} :text | ||
|
||
.rela.dyn : { | ||
*(.rela.dyn) | ||
} :text | ||
|
||
.bss . (NOLOAD): { | ||
*(.bss) | ||
*(COMMON) | ||
} :bss | ||
|
||
.note : ALIGN(8) { | ||
KEEP(*(.note.gnu*)) | ||
KEEP(*(.note.custom)) | ||
} :text :note | ||
|
||
/DISCARD/ : { | ||
*(.note.ABI-tag) | ||
} | ||
} | ||
|
28 changes: 28 additions & 0 deletions
28
test/testfiles_for_unittests/note_after_gnu_property/main.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <link.h> | ||
|
||
// NOTE: This is a minimal test fixture that creates an ELF file with a note | ||
// segment that has a single NOTE segment with a NT_GNU_PROPERTY_TYPE_0, | ||
// followed by a custom note. It's used in a regression test for a buffer | ||
// overrun bug in the parsing of the NT_GNU_PROPERTY_TYPE_0. | ||
|
||
struct elf_note { | ||
ElfW(Nhdr) nhdr; // header: 12 bytes | ||
char name[4]; // name buffer: 2 bytes + 2 bytes padding | ||
uint8_t data[8]; // data buffer: 8 bytes | ||
}; | ||
|
||
__attribute__((section(".note.custom"), aligned(8))) | ||
__attribute__((used)) | ||
const struct elf_note note = { | ||
.nhdr = { | ||
.n_namesz = 4, | ||
.n_descsz = 8, | ||
.n_type = 0, | ||
}, | ||
.name = {'H', 'i', '\0'}, | ||
.data = {}, | ||
}; | ||
|
||
int main() { | ||
return 0; | ||
} |
Binary file not shown.