From a3108de96aa68d930c9e2a1a4a37acbd47ff43bb Mon Sep 17 00:00:00 2001 From: Jessica Yu Date: Mon, 23 Jan 2017 12:42:56 -0800 Subject: [PATCH] kpatch-elf: fix null dereference when sym->sec is NULL Make sure sym->sec is not NULL before checking for its rela section (sym->sec->rela). This fixes a case where an object may have STT_FUNC symbols whose the sections (sym->sec) were not selected for inclusion (or are located in another object) and hence these symbols do not have sym->sec set. This corner case only recently popped up after reusing kpatch_elf_open() on objects that have been outputted by create-diff-object (and these objects only contain the necessary sections needed for the patch module). This will also automatically exclude livepatch symbols from the check, because they do not have sections associated with them (i.e., sym->sec is NULL). We do not have to check for fentry calls for klp (SHN_LIVEPATCH) symbols, because [1] they do not have sections associated with them, [2] they are not the target functions to be patched, and [3] they are technically just placeholder symbols for symbol resolution in livepatch. --- 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 aa658bc..45a5a3e 100644 --- a/kpatch-build/kpatch-elf.c +++ b/kpatch-build/kpatch-elf.c @@ -341,7 +341,7 @@ static void kpatch_find_fentry_calls(struct kpatch_elf *kelf) struct symbol *sym; struct rela *rela; list_for_each_entry(sym, &kelf->symbols, list) { - if (sym->type != STT_FUNC || !sym->sec->rela) + if (sym->type != STT_FUNC || !sym->sec || !sym->sec->rela) continue; rela = list_first_entry(&sym->sec->rela->relas, struct rela,