mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Rui Qi" <qirui.001@bytedance.com>
To: "Catalin Marinas" <catalin.marinas@arm.com>
Cc: <will@kernel.org>, <mark.rutland@arm.com>, <ardb@kernel.org>,
	 <song@kernel.org>, <dylanbhatch@google.com>,
	 <linux-arm-kernel@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>,  <stable@vger.kernel.org>
Subject: Re: [PATCH v2] arm64: module: Use text-poke for late PLT/veneer emission
Date: Tue, 17 Nov 2026 19:22:03 +0800	[thread overview]
Message-ID: <c6cc9216-147d-41dc-8139-690fac6f6015@bytedance.com> (raw)
In-Reply-To: <avdVsAOC_fpoDNDr@arm.com>

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.

      reply	other threads:[~2026-11-17 11:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11  7:39 Rui Qi
2026-11-13 18:23 ` Catalin Marinas
2026-11-17 11:22   ` Rui Qi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c6cc9216-147d-41dc-8139-690fac6f6015@bytedance.com \
    --to=qirui.001@bytedance.com \
    --cc=ardb@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=dylanbhatch@google.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=song@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®