loongarch.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: WANG Rui <r@hev.cc>
To: Huacai Chen <chenhuacai@kernel.org>
Cc: Ard Biesheuvel <ardb@kernel.org>, WANG Xuerui <kernel@xen0n.name>,
	 Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Lisa Robinson <lisa@bytefly.space>,
	 loongarch@lists.linux.dev, linux-efi@vger.kernel.org,
	 linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/2] efi/loongarch: Randomize kernel preferred address for KASLR
Date: Tue, 28 Apr 2026 22:34:41 +0800	[thread overview]
Message-ID: <CAHirt9hvsGLsDEo3qdWtC5BqiJsrCSv8UJYTZfqte8op7c8etw@mail.gmail.com> (raw)
In-Reply-To: <CAAhV-H4dTKEwb6u5X08dX2UFxophvr_GsfB-FFt_UHbVqJJi8Q@mail.gmail.com>

Hi Huacai,

On Tue, Apr 28, 2026 at 1:01 PM Huacai Chen <chenhuacai@kernel.org> wrote:
>
> Hi, Rui,
>
> On Tue, Apr 28, 2026 at 12:02 PM WANG Rui <r@hev.cc> wrote:
> >
> > Introduce efi_get_kimg_kaslr_address() to compute the preferred
> > kernel image address dynamically when CONFIG_RANDOMIZE_BASE is
> > enabled. The function derives a random offset using EFI-provided
> > randomness combined with the timer value, and constrains it within
> > CONFIG_RANDOMIZE_BASE_MAX_OFFSET.
> >
> > Update EFI_KIMG_PREFERRED_ADDRESS to call this helper so that the
> > EFI stub can select a randomized load address when KASLR is active,
> > while preserving the original base address behavior when KASLR is
> > disabled or nokaslr is specified.
> >
> > Signed-off-by: WANG Rui <r@hev.cc>
> > ---
> >  arch/loongarch/Kconfig                   |  2 +-
> >  arch/loongarch/include/asm/efi.h         |  4 +++-
> >  drivers/firmware/efi/libstub/loongarch.c | 16 ++++++++++++++++
> >  3 files changed, 20 insertions(+), 2 deletions(-)
> >
> > diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
> > index 3b042dbb2c41..a5afb70f73d9 100644
> > --- a/arch/loongarch/Kconfig
> > +++ b/arch/loongarch/Kconfig
> > @@ -730,7 +730,7 @@ config RANDOMIZE_BASE
> >  config RANDOMIZE_BASE_MAX_OFFSET
> >         hex "Maximum KASLR offset" if EXPERT
> >         depends on RANDOMIZE_BASE
> > -       range 0x0 0x10000000
> > +       range 0x20000 0x10000000
> >         default "0x01000000"
> >         help
> >           When KASLR is active, this provides the maximum offset that will
> > diff --git a/arch/loongarch/include/asm/efi.h b/arch/loongarch/include/asm/efi.h
> > index eddc8e79b3fa..f831320efd41 100644
> > --- a/arch/loongarch/include/asm/efi.h
> > +++ b/arch/loongarch/include/asm/efi.h
> > @@ -30,6 +30,8 @@ static inline unsigned long efi_get_kimg_min_align(void)
> >         return SZ_2M;
> >  }
> >
> > -#define EFI_KIMG_PREFERRED_ADDRESS     PHYSADDR(VMLINUX_LOAD_ADDRESS)
> > +unsigned long efi_get_kimg_kaslr_address(void);
> > +
> > +#define EFI_KIMG_PREFERRED_ADDRESS     efi_get_kimg_kaslr_address()
> >
> >  #endif /* _ASM_LOONGARCH_EFI_H */
> > diff --git a/drivers/firmware/efi/libstub/loongarch.c b/drivers/firmware/efi/libstub/loongarch.c
> > index 9825f5218137..c44be5d3dc04 100644
> > --- a/drivers/firmware/efi/libstub/loongarch.c
> > +++ b/drivers/firmware/efi/libstub/loongarch.c
> > @@ -38,6 +38,22 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
> >         return EFI_SUCCESS;
> >  }
> >
> > +unsigned long efi_get_kimg_kaslr_address(void)
> Move it into arch/loongarch/include/asm/efi.h?

In that case, we'd need to declare efi_nokaslr and
efi_get_random_bytes() in efi.h. Since including efistub.h in efi.h
doesn't work, putting them here keeps things pretty clean.

Thanks,
Rui

>
> > +{
> > +       unsigned int random_offset = 0;
> > +
> > +#ifdef CONFIG_RANDOMIZE_BASE
> > +       if (!efi_nokaslr) {
> > +               efi_get_random_bytes(sizeof(random_offset), (u8 *)&random_offset);
> > +               random_offset ^= (random_get_entropy() << 16);
> > +               random_offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - SZ_64K - 1);
> I still don't want to modify the range of RANDOMIZE_BASE_MAX_OFFSET, so use
>      random_offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1)
> here?
>
> > +               random_offset = ALIGN(random_offset, SZ_64K) + SZ_64K;
> It seems "random_offset = ALIGN(random_offset + SZ64K, SZ_64K)" is better.
>
> Huacai
>
> > +       }
> > +#endif
> > +
> > +       return PHYSADDR(VMLINUX_LOAD_ADDRESS) + random_offset;
> > +}
> > +
> >  unsigned long __weak kernel_entry_address(unsigned long kernel_addr,
> >                 efi_loaded_image_t *image)
> >  {
> > --
> > 2.54.0
> >
>

  reply	other threads:[~2026-04-28 14:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-28  4:01 [PATCH v2 0/2] LoongArch: Move KASLR to EFI stub to avoid initrd overlap WANG Rui
2026-04-28  4:01 ` [PATCH v2 1/2] efi/loongarch: Randomize kernel preferred address for KASLR WANG Rui
2026-04-28  5:01   ` Huacai Chen
2026-04-28 14:34     ` WANG Rui [this message]
2026-04-28  4:01 ` [PATCH v2 2/2] LoongArch: Skip relocation-time KASLR if it has already been applied WANG Rui

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=CAHirt9hvsGLsDEo3qdWtC5BqiJsrCSv8UJYTZfqte8op7c8etw@mail.gmail.com \
    --to=r@hev.cc \
    --cc=ardb@kernel.org \
    --cc=chenhuacai@kernel.org \
    --cc=ilias.apalodimas@linaro.org \
    --cc=kernel@xen0n.name \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lisa@bytefly.space \
    --cc=loongarch@lists.linux.dev \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).