From a3bf8f0852d0f66911dcf879c5a1fcff3cb4cb46 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Mon, 27 Mar 2017 17:01:57 +0200 Subject: libelf: Always update last_offset in updatefile and updatemmap. When ELF section data was used, but not updated or marked as dirty and there also existed non-dirty sections and some padding was needed between the sections (possibly because of alignment) then elf_update might write "fill" over some of the existing data. This happened because in that case the last_position was not updated correctly. Includes a new testcase fillfile that fails before this patch by showing fill instead of the expected data in some section data. It succeeds with this patch. https://sourceware.org/bugzilla/show_bug.cgi?id=21199 Signed-off-by: Mark Wielaard --- libelf/elf32_updatefile.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'libelf/elf32_updatefile.c') diff --git a/libelf/elf32_updatefile.c b/libelf/elf32_updatefile.c index 8dd85d1a..7ac99510 100644 --- a/libelf/elf32_updatefile.c +++ b/libelf/elf32_updatefile.c @@ -343,9 +343,10 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum) { fill_mmap (dl->data.d.d_off, last_position, scn_start, shdr_start, shdr_end); - last_position = scn_start + dl->data.d.d_off; } + last_position = scn_start + dl->data.d.d_off; + if ((scn->flags | dl->flags | elf->flags) & ELF_F_DIRTY) { /* Let it go backward if the sections use a bogus @@ -353,8 +354,6 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum) user's section data with the latest one, rather than crashing. */ - last_position = scn_start + dl->data.d.d_off; - if (unlikely (change_bo)) { #if EV_NUM != 2 @@ -728,6 +727,8 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf, int change_bo, size_t shnum) } } + last_offset = scn_start + dl->data.d.d_off; + if ((scn->flags | dl->flags | elf->flags) & ELF_F_DIRTY) { char tmpbuf[MAX_TMPBUF]; @@ -738,8 +739,6 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf, int change_bo, size_t shnum) user's section data with the latest one, rather than crashing. */ - last_offset = scn_start + dl->data.d.d_off; - if (unlikely (change_bo)) { #if EV_NUM != 2 -- cgit v1.2.3