]> git.baikalelectronics.ru Git - kernel.git/commitdiff
powerpc/module_64: Fix livepatching for RO modules
authorRussell Currey <ruscur@russell.cc>
Tue, 23 Nov 2021 08:15:20 +0000 (18:15 +1000)
committerMichael Ellerman <mpe@ellerman.id.au>
Tue, 14 Dec 2021 12:13:03 +0000 (23:13 +1100)
Livepatching a loaded module involves applying relocations through
apply_relocate_add(), which attempts to write to read-only memory when
CONFIG_STRICT_MODULE_RWX=y.  Work around this by performing these
writes through the text poke area by using patch_instruction().

R_PPC_REL24 is the only relocation type generated by the kpatch-build
userspace tool or klp-convert kernel tree that I observed applying a
relocation to a post-init module.

A more comprehensive solution is planned, but using patch_instruction()
for R_PPC_REL24 on should serve as a sufficient fix.

This does have a performance impact, I observed ~15% overhead in
module_load() on POWER8 bare metal with checksum verification off.

Fixes: 0a4858327e11 ("powerpc: Set ARCH_HAS_STRICT_MODULE_RWX")
Cc: stable@vger.kernel.org # v5.14+
Reported-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Russell Currey <ruscur@russell.cc>
Tested-by: Joe Lawrence <joe.lawrence@redhat.com>
[mpe: Check return codes from patch_instruction()]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20211214121248.777249-1-mpe@ellerman.id.au
arch/powerpc/kernel/module_64.c

index 6baa676e7cb60219795d6b40a99b27a674357723..5d77d3f5fbb561192cb8892158d5ec6297e17699 100644 (file)
@@ -422,11 +422,17 @@ static inline int create_stub(const Elf64_Shdr *sechdrs,
                              const char *name)
 {
        long reladdr;
+       func_desc_t desc;
+       int i;
 
        if (is_mprofile_ftrace_call(name))
                return create_ftrace_stub(entry, addr, me);
 
-       memcpy(entry->jump, ppc64_stub_insns, sizeof(ppc64_stub_insns));
+       for (i = 0; i < sizeof(ppc64_stub_insns) / sizeof(u32); i++) {
+               if (patch_instruction(&entry->jump[i],
+                                     ppc_inst(ppc64_stub_insns[i])))
+                       return 0;
+       }
 
        /* Stub uses address relative to r2. */
        reladdr = (unsigned long)entry - my_r2(sechdrs, me);
@@ -437,10 +443,24 @@ static inline int create_stub(const Elf64_Shdr *sechdrs,
        }
        pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr);
 
-       entry->jump[0] |= PPC_HA(reladdr);
-       entry->jump[1] |= PPC_LO(reladdr);
-       entry->funcdata = func_desc(addr);
-       entry->magic = STUB_MAGIC;
+       if (patch_instruction(&entry->jump[0],
+                             ppc_inst(entry->jump[0] | PPC_HA(reladdr))))
+               return 0;
+
+       if (patch_instruction(&entry->jump[1],
+                         ppc_inst(entry->jump[1] | PPC_LO(reladdr))))
+               return 0;
+
+       // func_desc_t is 8 bytes if ABIv2, else 16 bytes
+       desc = func_desc(addr);
+       for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) {
+               if (patch_instruction(((u32 *)&entry->funcdata) + i,
+                                     ppc_inst(((u32 *)(&desc))[i])))
+                       return 0;
+       }
+
+       if (patch_instruction(&entry->magic, ppc_inst(STUB_MAGIC)))
+               return 0;
 
        return 1;
 }
@@ -495,8 +515,11 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me)
                        me->name, *instruction, instruction);
                return 0;
        }
+
        /* ld r2,R2_STACK_OFFSET(r1) */
-       *instruction = PPC_INST_LD_TOC;
+       if (patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC)))
+               return 0;
+
        return 1;
 }
 
@@ -636,9 +659,12 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
                        }
 
                        /* Only replace bits 2 through 26 */
-                       *(uint32_t *)location
-                               = (*(uint32_t *)location & ~0x03fffffc)
+                       value = (*(uint32_t *)location & ~0x03fffffc)
                                | (value & 0x03fffffc);
+
+                       if (patch_instruction((u32 *)location, ppc_inst(value)))
+                               return -EFAULT;
+
                        break;
 
                case R_PPC64_REL64: