From 3bfc85732dd12d82f1f6092e6010132ff065a697 Mon Sep 17 00:00:00 2001 From: chenzefeng Date: Sun, 12 May 2019 09:58:22 +0800 Subject: [PATCH] kpatch-elf: fix the unexpected elf classes kpatch-elf::kpatch_write_output_elf will call the gelf_getclass() to acquire the output elf's class. But the input parameter kelf->elf is NULL, the gelf_getclass(kelf->elf) will return ELFCLASSNONE, not the value we expect ELFCLASS32 or ELFCLASS64. the gelf_getclass function code: int gelf_getclass (Elf *elf) { return elf == NULL || elf->kind != ELF_K_ELF ? ELFCLASSNONE : elf->class; } the gelf_newehdr fuction code: void * gelf_newehdr (Elf *elf, int class) { return (class == ELFCLASS32 ? (void *) INTUSE(elf32_newehdr) (elf) : (void *) INTUSE(elf64_newehdr) (elf)); } Luckily, when we create a patch for x86_64 or powerpc64, if we pass the ELFCLASSNONE for the function gelf_newehdr, it will return elf64_newehdr, so don't cause the fault. But it's better to use the gelf_getclass(elf) instead of gelf_getclass(kelf->elf). Signed-off-by: chenzefeng --- kpatch-build/kpatch-elf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kpatch-build/kpatch-elf.c b/kpatch-build/kpatch-elf.c index 55a1351..5802c46 100644 --- a/kpatch-build/kpatch-elf.c +++ b/kpatch-build/kpatch-elf.c @@ -765,7 +765,7 @@ void kpatch_write_output_elf(struct kpatch_elf *kelf, Elf *elf, char *outfile) if (!elfout) ERROR("elf_begin"); - if (!gelf_newehdr(elfout, gelf_getclass(kelf->elf))) + if (!gelf_newehdr(elfout, gelf_getclass(elf))) ERROR("gelf_newehdr"); if (!gelf_getehdr(elfout, &ehout))