Re: [PATCH v4 15/16] module: Move where we mark modules RO,X

From: Josh Poimboeuf
Date: Thu Oct 24 2019 - 14:31:33 EST


On Thu, Oct 24, 2019 at 12:59:04PM +0200, Peter Zijlstra wrote:
> On Wed, Oct 23, 2019 at 12:15:14PM -0500, Josh Poimboeuf wrote:
> > On Wed, Oct 23, 2019 at 05:16:54PM +0200, Peter Zijlstra wrote:
> > > @@ -157,6 +158,14 @@ static int __apply_relocate_add(Elf64_Sh
> > >
> > > val = sym->st_value + rel[i].r_addend;
> > >
> > > + /*
> > > + * .klp.rela.* sections should only contain module
> > > + * related RELAs. All core-kernel RELAs should be in
> > > + * normal .rela.* sections and be applied when loading
> > > + * the patch module itself.
> > > + */
> > > + WARN_ON_ONCE(klp && core_kernel_text(val));
> > > +
> >
> > This isn't quite true, we also use .klp.rela sections to access
> > unexported vmlinux symbols.
>
> Yes, you said in that earlier email. That all makes it really hard to
> validate this. But unless we validate it, it will stay buggy :/
>
> Hmmm.... /me ponders
>
> The alternative would be to apply the .klp.rela.* sections twice, once
> at patch-module load time and then apply those core_kernel_text()
> entries, and then once later and skip over them.
>
> How's this?

How about something like this? Completely untested, but if you agree
with this approach I could hack up kpatch-build to test it properly.

diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index ab4a4606d19b..597bf32bc591 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -239,6 +239,17 @@ static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod)
if (ret)
return ret;

+ /*
+ * Prevent module patches from using livepatch relas for
+ * vmlinux symbols. Presumably such symbols are exported and
+ * normal relas can instead be used at patch module loading
+ * time.
+ */
+ if (!vmlinux && core_kernel_text(addr)) {
+ pr_err("unsupported livepatch symbol\n");
+ return -EINVAL;
+ }
+
sym->st_value = addr;
}