[Crash-utility PATCH] RISCV64: Add KASLR support

Guo Ren guoren at kernel.org
Thu Aug 17 17:45:44 PDT 2023


On Tue, Aug 15, 2023 at 6:50 PM Song Shuai <songshuaishuai at tinylab.org> wrote:
>
> From: Song Shuai <suagrfillet at gmail.com>
>
> This patch adds KASLR support for Crash to analyze KASLR-ed vmcore
> since RISC-V Linux is already sufficiently prepared for KASLR [1].
>
> With this patch, even if the Crash '--kaslr' option is not set or Linux
> CONFIG_RANDOMIZE_BASE is not configured, the 'derive_kaslr_offset()'
> function will always work to calculate 'kt->relocate' which serves to
> update the kernel virtual address.
>
> [1]: https://lore.kernel.org/linux-riscv/20230722123850.634544-1-alexghiti@rivosinc.com/
>
> Signed-off-by: Song Shuai <suagrfillet at gmail.com>
> ---
>  main.c    |  2 +-
>  riscv64.c | 11 +++++++++++
>  symbols.c |  4 ++--
>  3 files changed, 14 insertions(+), 3 deletions(-)
>
> diff --git a/main.c b/main.c
> index b278c22..0c6e595 100644
> --- a/main.c
> +++ b/main.c
> @@ -228,7 +228,7 @@ main(int argc, char **argv)
>                         } else if (STREQ(long_options[option_index].name, "kaslr")) {
>                                 if (!machine_type("X86_64") &&
>                                     !machine_type("ARM64") && !machine_type("X86") &&
> -                                   !machine_type("S390X"))
> +                                   !machine_type("S390X") && !machine_type("RISCV64"))
>                                         error(INFO, "--kaslr not valid "
>                                                 "with this machine type.\n");
>                                 else if (STREQ(optarg, "auto"))
> diff --git a/riscv64.c b/riscv64.c
> index a02f75a..288c7ae 100644
> --- a/riscv64.c
> +++ b/riscv64.c
> @@ -378,6 +378,9 @@ static void riscv64_get_va_range(struct machine_specific *ms)
>         } else
>                 goto error;
>
> +       if ((kt->flags2 & KASLR) && (kt->flags & RELOC_SET))
> +               ms->kernel_link_addr += (kt->relocate * -1);
> +
>         /*
>          * From Linux 5.13, the kernel mapping is moved to the last 2GB
>          * of the address space, modules use the 2GB memory range right
> @@ -1360,6 +1363,14 @@ riscv64_init(int when)
>
>                 machdep->verify_paddr = generic_verify_paddr;
>                 machdep->ptrs_per_pgd = PTRS_PER_PGD;
> +
> +               /*
> +                * Even if CONFIG_RANDOMIZE_BASE is not configured,
> +                * derive_kaslr_offset() should work and set
> +                * kt->relocate to 0
> +                */
> +               if (!kt->relocate && !(kt->flags2 & (RELOC_AUTO|KASLR)))
> +                       kt->flags2 |= (RELOC_AUTO|KASLR);
>                 break;
>
>         case PRE_GDB:
> diff --git a/symbols.c b/symbols.c
> index 876be7a..8e8b4c3 100644
> --- a/symbols.c
> +++ b/symbols.c
> @@ -629,7 +629,7 @@ kaslr_init(void)
>         char *string;
>
>         if ((!machine_type("X86_64") && !machine_type("ARM64") && !machine_type("X86") &&
> -           !machine_type("S390X")) || (kt->flags & RELOC_SET))
> +           !machine_type("S390X") && !machine_type("RISCV64")) || (kt->flags & RELOC_SET))
>                 return;
>
>         if (!kt->vmcoreinfo._stext_SYMBOL &&
> @@ -795,7 +795,7 @@ store_symbols(bfd *abfd, int dynamic, void *minisyms, long symcount,
>                 } else if (!(kt->flags & RELOC_SET))
>                         kt->flags |= RELOC_FORCE;
>         } else if (machine_type("X86_64") || machine_type("ARM64") ||
> -                  machine_type("S390X")) {
> +                  machine_type("S390X") || machine_type("RISCV64")) {
>                 if ((kt->flags2 & RELOC_AUTO) && !(kt->flags & RELOC_SET))
>                         derive_kaslr_offset(abfd, dynamic, from,
>                                 fromend, size, store);
> --
> 2.20.1
>
Reviewed-by: Guo Ren <guoren at kernel.org>

Could you put some output in the commit log, to let other turst this
patch could work.

-- 
Best Regards
 Guo Ren



More information about the linux-riscv mailing list