Re: [PATCH v2 6/9] s390/module: Use s390_kernel_write() for late relocations

From: Joe Lawrence
Date: Thu Apr 23 2020 - 22:35:33 EST


On Thu, Apr 23, 2020 at 06:26:57PM -0500, Josh Poimboeuf wrote:
> On Thu, Apr 23, 2020 at 01:10:30PM -0500, Josh Poimboeuf wrote:
> > Mystery solved:
> >
> > $ CROSS_COMPILE=s390x-linux-gnu- scripts/faddr2line vmlinux apply_rela+0x16a/0x520
> > apply_rela+0x16a/0x520:
> > apply_rela at arch/s390/kernel/module.c:336
> >
> > which corresponds to the following code in apply_rela():
> >
> >
> > case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
> > if (info->plt_initialized == 0) {
> > unsigned int *ip;
> > ip = me->core_layout.base + me->arch.plt_offset +
> > info->plt_offset;
> > ip[0] = 0x0d10e310; /* basr 1,0 */
> > ip[1] = 0x100a0004; /* lg 1,10(1) */
> >
> >
> > Notice how it's writing directly to text... oops.
>
> Here's a fix, using write() for the PLT and the GOT.
>
> diff --git a/arch/s390/kernel/module.c b/arch/s390/kernel/module.c
> index 2798329ebb74..fe446f42818f 100644
> --- a/arch/s390/kernel/module.c
> +++ b/arch/s390/kernel/module.c
> @@ -297,7 +297,7 @@ static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
>
> gotent = me->core_layout.base + me->arch.got_offset +
> info->got_offset;
> - *gotent = val;
> + write(gotent, &val, sizeof(*gotent));
> info->got_initialized = 1;
> }
> val = info->got_offset + rela->r_addend;
> @@ -330,25 +330,29 @@ static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
> case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
> case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
> if (info->plt_initialized == 0) {
> - unsigned int *ip;
> + unsigned int *ip, insn[5];
> +
> ip = me->core_layout.base + me->arch.plt_offset +
> info->plt_offset;

Would it be too paranoid to declare:

const unsigned int *ip = me->core_layout.base +
me->arch.plt_offset +
info->plt_offset;

That would trip an assignment to read-only error if someone were tempted
to write directly through the pointer in the future. Ditto for Elf_Addr
*gotent pointer in the R_390_GOTPLTENT case.

-- Joe