mbox series

[v3,0/5] riscv: Introduce KASLR

Message ID 20230606123242.20804-1-alexghiti@rivosinc.com
Headers show
Series riscv: Introduce KASLR | expand

Message

Alexandre Ghiti June 6, 2023, 12:32 p.m. UTC
The following KASLR implementation allows to randomize the kernel mapping:

- virtually: we expect the bootloader to provide a seed in the device-tree
- physically: only implemented in the EFI stub, it relies on the firmware to
  provide a seed using EFI_RNG_PROTOCOL. arm64 has a similar implementation
  hence the patch 3 factorizes KASLR related functions for riscv to take
  advantage.

The new virtual kernel location is limited by the early page table that only
has one PUD and with the PMD alignment constraint, the kernel can only take
< 512 positions.

base-commit-tag: v6.4-rc2

Changes in v3:
  * Rebase on top of 6.4-rc2
  * Make RANDOMIZE_BASE depend on 64bit
  * Fix efi_icache_sync and efi_get_kimg_min_align which were undefined
    in x86 (and certainly other archs)
  * Add patch 4 to fix warning on rv32

Changes in v2:
  * Rebase on top of 6.3-rc1
  * Add a riscv cache sync after memcpying the kernel
  * Add kaslr_offset implementation for KCOV
  * Add forward declaration to quiet LLVM

Alexandre Ghiti (5):
  riscv: Introduce virtual kernel mapping KASLR
  riscv: Dump out kernel offset information on panic
  arm64: libstub: Move KASLR handling functions to efi-stub-helper.c
  libstub: Fix compilation warning for rv32
  riscv: libstub: Implement KASLR by using generic functions

 arch/arm64/include/asm/efi.h                  |   4 +
 arch/riscv/Kconfig                            |  19 +++
 arch/riscv/include/asm/efi.h                  |   4 +
 arch/riscv/include/asm/page.h                 |   3 +
 arch/riscv/kernel/image-vars.h                |   1 +
 arch/riscv/kernel/pi/Makefile                 |   2 +-
 arch/riscv/kernel/pi/cmdline_early.c          |  13 ++
 arch/riscv/kernel/pi/fdt_early.c              |  30 ++++
 arch/riscv/kernel/setup.c                     |  25 +++
 arch/riscv/mm/init.c                          |  36 ++++-
 drivers/firmware/efi/libstub/arm64-stub.c     | 112 +------------
 drivers/firmware/efi/libstub/arm64.c          |   5 +
 .../firmware/efi/libstub/efi-stub-helper.c    | 151 ++++++++++++++++++
 drivers/firmware/efi/libstub/efistub.h        |  18 +++
 drivers/firmware/efi/libstub/riscv-stub.c     |  28 ++--
 drivers/firmware/efi/libstub/riscv.c          |   5 +
 16 files changed, 332 insertions(+), 124 deletions(-)
 create mode 100644 arch/riscv/kernel/pi/fdt_early.c

Comments

Palmer Dabbelt June 22, 2023, 12:09 a.m. UTC | #1
On Tue, 06 Jun 2023 05:32:37 PDT (-0700), alexghiti@rivosinc.com wrote:
> The following KASLR implementation allows to randomize the kernel mapping:
>
> - virtually: we expect the bootloader to provide a seed in the device-tree
> - physically: only implemented in the EFI stub, it relies on the firmware to
>   provide a seed using EFI_RNG_PROTOCOL. arm64 has a similar implementation
>   hence the patch 3 factorizes KASLR related functions for riscv to take
>   advantage.
>
> The new virtual kernel location is limited by the early page table that only
> has one PUD and with the PMD alignment constraint, the kernel can only take
> < 512 positions.
>
> base-commit-tag: v6.4-rc2
>
> Changes in v3:
>   * Rebase on top of 6.4-rc2
>   * Make RANDOMIZE_BASE depend on 64bit
>   * Fix efi_icache_sync and efi_get_kimg_min_align which were undefined
>     in x86 (and certainly other archs)
>   * Add patch 4 to fix warning on rv32
>
> Changes in v2:
>   * Rebase on top of 6.3-rc1
>   * Add a riscv cache sync after memcpying the kernel
>   * Add kaslr_offset implementation for KCOV
>   * Add forward declaration to quiet LLVM
>
> Alexandre Ghiti (5):
>   riscv: Introduce virtual kernel mapping KASLR
>   riscv: Dump out kernel offset information on panic
>   arm64: libstub: Move KASLR handling functions to efi-stub-helper.c
>   libstub: Fix compilation warning for rv32
>   riscv: libstub: Implement KASLR by using generic functions
>
>  arch/arm64/include/asm/efi.h                  |   4 +
>  arch/riscv/Kconfig                            |  19 +++
>  arch/riscv/include/asm/efi.h                  |   4 +
>  arch/riscv/include/asm/page.h                 |   3 +
>  arch/riscv/kernel/image-vars.h                |   1 +
>  arch/riscv/kernel/pi/Makefile                 |   2 +-
>  arch/riscv/kernel/pi/cmdline_early.c          |  13 ++
>  arch/riscv/kernel/pi/fdt_early.c              |  30 ++++
>  arch/riscv/kernel/setup.c                     |  25 +++
>  arch/riscv/mm/init.c                          |  36 ++++-
>  drivers/firmware/efi/libstub/arm64-stub.c     | 112 +------------
>  drivers/firmware/efi/libstub/arm64.c          |   5 +
>  .../firmware/efi/libstub/efi-stub-helper.c    | 151 ++++++++++++++++++
>  drivers/firmware/efi/libstub/efistub.h        |  18 +++
>  drivers/firmware/efi/libstub/riscv-stub.c     |  28 ++--
>  drivers/firmware/efi/libstub/riscv.c          |   5 +
>  16 files changed, 332 insertions(+), 124 deletions(-)
>  create mode 100644 arch/riscv/kernel/pi/fdt_early.c

I'm marking this one as changes requested as well as LKP is pointing at 
some build failures.  It also touches libstub so I'd really prefer an 
Ack, but folks might not be looking with the build issues.