Skip to content

Commit

Permalink
RISCV64: Add KASLR support
Browse files Browse the repository at this point in the history
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.

Testing in Qemu rv64 virt, kernel log outputed the kernel offset:

  [  121.214447] SMP: stopping secondary CPUs
  [  121.215445] Kernel Offset: 0x37c00000 from 0xffffffff80000000
  [  121.216312] Starting crashdump kernel...
  [  121.216585] Will call new kernel at 94800000 from hart id 0
  [  121.216834] FDT image at 9c7fd000
  [  121.216982] Bye...

Running crash with '-d 1' option and without '--kaslr' option,
we get the right 'kt->relocate' and kernel link addr:

  $ ../crash/crash -d 1 vmlinux vmcore_kaslr_0815
  ...
  KASLR:
    _stext from vmlinux: ffffffff80002000
    _stext from vmcoreinfo: ffffffffb7c02000
    relocate: 37c00000 (892MB)
  vmemmap : 0xff1c000000000000 - 0xff20000000000000
  vmalloc : 0xff20000000000000 - 0xff60000000000000
  mudules : 0xffffffff3952f000 - 0xffffffffb7c00000
  lowmem  : 0xff60000000000000 -
  kernel link addr        : 0xffffffffb7c00000
  ...
        KERNEL: /home/song/9_linux/linux/00_rv_kaslr/vmlinux
      DUMPFILE: /tmp/hello/vmcore_kaslr_0815
          CPUS: 2
          DATE: Tue Aug 15 16:36:15 CST 2023
        UPTIME: 00:02:01
  LOAD AVERAGE: 0.40, 0.23, 0.09
         TASKS: 63
      NODENAME: stage4.fedoraproject.org
       RELEASE: 6.5.0-rc3-00008-gad18dee423ac
       VERSION: #17 SMP Tue Aug 15 14:41:12 CST 2023
       MACHINE: riscv64  (unknown Mhz)
        MEMORY: 511.8 MB
         PANIC: "Kernel panic - not syncing: sysrq triggered crash"
           PID: 160
       COMMAND: "bash"
          TASK: ff6000000152bac0  [THREAD_INFO: ff6000000152bac0]
           CPU: 1
         STATE: TASK_RUNNING (PANIC)
  crash>

[1]: https://lore.kernel.org/linux-riscv/[email protected]/

Signed-off-by: Song Shuai <[email protected]>
Reviewed-by: Guo Ren <[email protected]>
  • Loading branch information
Song Shuai authored and k-hagio committed Aug 21, 2023
1 parent f774fe0 commit 1aa93cd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
11 changes: 11 additions & 0 deletions riscv64.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,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
Expand Down Expand Up @@ -1340,6 +1343,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:
Expand Down
4 changes: 2 additions & 2 deletions symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1aa93cd

Please sign in to comment.