[PATCH v2] arm64: module: Use text-poke for late PLT/veneer emission

Rui Qi qirui.001 at bytedance.com
Tue Nov 17 03:22:03 PST 2026


On 11/14/26 2:23 AM, Catalin Marinas wrote:
> Thanks Rui for respinning.
> 
> On Wed, Nov 11, 2026 at 03:39:06PM +0800, Rui Qi wrote:
>> diff --git a/arch/arm64/include/asm/module.h b/arch/arm64/include/asm/module.h
>> index fb9b88eebeb1..4a1a47f8ba1a 100644
>> --- a/arch/arm64/include/asm/module.h
>> +++ b/arch/arm64/include/asm/module.h
>> @@ -6,6 +6,7 @@
>>  #define __ASM_MODULE_H
>>  
>>  #include <asm-generic/module.h>
>> +#include <asm/text-patching.h>
>>  
>>  struct mod_plt_sec {
>>  	int			plt_shndx;
>> @@ -29,6 +30,21 @@ u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs,
>>  u64 module_emit_veneer_for_adrp(struct module *mod, Elf64_Shdr *sechdrs,
>>  				void *loc, u64 val);
>>  
>> +/*
>> + * Write a relocation value to @place within a module.  While the module
>> + * is MODULE_STATE_UNFORMED the destination is writable and a direct store
>> + * is used; once the module is fully formed the region is read-only and
>> + * executable, so the write is routed through the text-poke API.
>> + */
>> +#define module_write_place(place, val, mod) do {			\
>> +	__typeof__(*place) __val = (val);				\
>> +									\
>> +	if (mod->state == MODULE_STATE_UNFORMED)			\
>> +		*(place) = __val;					\
>> +	else								\
>> +		aarch64_insn_copy(place, &(__val), sizeof(*place));	\
>> +} while (0)
> 
> A nit here, I'd move this macro to asm/text-patching.h since
> asm/module.h is pulled in by linux/module.h. Even better if you could
> make this a function (either static inline or out of line).
> 

I'll replace the macro with an out-of-line function:

    void module_write_place(void *place, const void *val,
                            size_t size, struct module *mod);

I'll declare it in asm/text-patching.h with a forward declaration of
struct module, and define it in arch/arm64/kernel/module.c.

Keeping the implementation out of line avoids adding a dependency on
linux/module.h to text-patching.h. The header only needs the module
pointer type, while access to mod->state and MODULE_STATE_UNFORMED
stays in the .c file.


>> --- a/arch/arm64/kernel/module-plts.c
>> +++ b/arch/arm64/kernel/module-plts.c
>> @@ -76,11 +76,13 @@ u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs,
>>  	int i = pltsec->plt_num_entries;
>>  	int j = i - 1;
>>  	u64 val = sym->st_value + rela->r_addend;
>> +	struct plt_entry entry;
>>  
>>  	if (is_forbidden_offset_for_adrp(&plt[i].adrp))
>>  		i++;
>>  
>> -	plt[i] = get_plt_entry(val, &plt[i]);
>> +	entry = get_plt_entry(val, &plt[i]);
>> +	module_write_place(&plt[i], entry, mod);
> 
> Sashiko reported PLT exhaustion/overrun and ADRP corruption when
> relocations are reapplied after unloading and reloading a target module:
> 
> https://sashiko.dev/#/patchset/20261111073906.652400-1-qirui.001@bytedance.com
> 
> These look like existing problems, only exposed now because we no longer
> get the ROX fault. I think they can be addressed in separate follow-up
> patches. This patch still fixes the fault on the first application.
> 

Agreed, pre-existing.  I checked: plt_num_entries is only reset to 0 in
module_frob_arch_sections(), i.e. once at load.  livepatch reapplication
(klp_apply_object_relocs() -> apply_relocate_add(), e.g. on target-module
unload/reload) doesn't reset it, so module_emit_plt_entry() keeps emitting
past the original entries and overruns .plt; before this patch that path
died on the ROX fault first.  I'll send a separate follow-up.  The .plt
section is shared across target objects, so the fix is more than a count
reset.


> However, could we include a bounds check before writing the PLT entry,
> maybe with a WARN_ON_ONCE? Once late writes use text-poke, retrying
> after slot exhaustion can overwrite memory beyond .plt.
> 
> Thanks.
> 

Yes.  The hole is in module_emit_plt_entry(): the write runs before the
existing WARN_ON, so on exhaustion text-poke corrupts past .plt before
the check fires.  module_emit_veneer_for_adrp() already checks before
its write (and returns 0 on exhaustion), so it isn't affected.

I'll add, after the is_forbidden_offset_for_adrp() bump and before the
write:

    if (WARN_ON_ONCE(i >= pltsec->plt_max_entries))
        return 0;

This is the same condition as the post-write check -- num_entries becomes
i + 1, so (num_entries > plt_max_entries) <=> (i >= plt_max_entries) --
just evaluated before the write.  The caller maps a 0 return to -ENOEXEC,
so exhaustion becomes a clean load failure instead of corruption.  The
post-write WARN_ON is then redundant and I'll drop it.

v3 to follow.



More information about the linux-arm-kernel mailing list