Re: [PATCH v2 2/9] livepatch: Apply vmlinux-specific KLP relocations early

From: Joe Lawrence
Date: Wed Apr 22 2020 - 21:10:18 EST


On Mon, Apr 20, 2020 at 02:11:17PM -0500, Josh Poimboeuf wrote:
> On Mon, Apr 20, 2020 at 03:01:41PM -0400, Joe Lawrence wrote:
> > > > ... apply_relocations() is also iterating over the section headers (the
> > > > diff context doesn't show it here, but i is an incrementing index over
> > > > sechdrs[]).
> > > >
> > > > So if there is more than one KLP relocation section, we'll process them
> > > > multiple times. At least the x86 relocation code will detect this and
> > > > fail the module load with an invalid relocation (existing value not
> > > > zero).
> > >
> > > Ah, yes, good catch!
> > >
> >
> > The same test case passed with a small modification to push the foreach
> > KLP section part to a kernel/livepatch/core.c local function and
> > exposing the klp_resolve_symbols() + apply_relocate_add() for a given
> > section to kernel/module.c. Something like following...
>
> I came up with something very similar, though I named them
> klp_apply_object_relocs() and klp_apply_section_relocs() and changed the
> argument order a bit (module first). Since it sounds like you have a
> test, could you try this one?
>
> diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
> index 533359e48c39..fb1a3de39726 100644
> --- a/include/linux/livepatch.h
> +++ b/include/linux/livepatch.h
>
> [ ... snip ... ]
>
> @@ -245,10 +245,10 @@ static inline void klp_update_patch_state(struct task_struct *task) {}
> static inline void klp_copy_process(struct task_struct *child) {}
>
> static inline
> -int klp_write_relocations(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
> - const char *shstrtab, const char *strtab,
> - unsigned int symindex, struct module *pmod,
> - const char *objname)
> +int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
> + const char *shstrtab, const char *strtab,
> + unsigned int symindex, unsigned int secindex,
> + const char *objname);
^^
Whoops, stray semicolon in !CONFIG_LIVEPATCH case. I found it by
botching my cross-compiling .config, but the build-bot might find it
when you push your branch.

> {
> return 0;
> }

-- Joe