From 0a7df9e03059c8a0b4a480c3d4cc3911b15f4350 Mon Sep 17 00:00:00 2001 From: Alexander Egorenkov Date: Tue, 19 Jan 2021 08:42:59 +0100 Subject: [PATCH] zdump: improve error handling in pt_load_add() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Verify the given ELF header before adding any memory chunks otherwise a memory chunk might be added even if the given ELF header is invalid. Signed-off-by: Alexander Egorenkov Reviewed-by: Philipp Rudo Signed-off-by: Jan Höppner --- zdump/dfi_elf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zdump/dfi_elf.c b/zdump/dfi_elf.c index f1534e66..fb2fc08a 100644 --- a/zdump/dfi_elf.c +++ b/zdump/dfi_elf.c @@ -42,6 +42,8 @@ static int pt_load_add(Elf64_Phdr *phdr) STDERR("Dump file \"%s\" is a user space core dump\n", g.opts.device); } + if (phdr->p_offset + phdr->p_filesz > zg_size(g.fh)) + return -EINVAL; if (phdr->p_filesz == 0) { /* Add zero memory chunk */ dfi_mem_chunk_add(phdr->p_paddr, phdr->p_memsz, NULL, @@ -52,8 +54,6 @@ static int pt_load_add(Elf64_Phdr *phdr) dfi_mem_chunk_add(phdr->p_paddr, phdr->p_memsz, off_ptr, dfi_elf_mem_chunk_read_fn, zg_free); } - if (phdr->p_offset + phdr->p_filesz > zg_size(g.fh)) - return -EINVAL; return 0; }