Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp131840rwl; Tue, 28 Mar 2023 22:32:34 -0700 (PDT) X-Google-Smtp-Source: AKy350Y1jY2t9T+MgcTWR+GZMgImg1mR0iryO/T8u8Sp+JWLcMOIYPo1MOcFxvVogcQ63W5IY2/K X-Received: by 2002:a17:906:5646:b0:930:7d8f:15a4 with SMTP id v6-20020a170906564600b009307d8f15a4mr18110076ejr.53.1680067954053; Tue, 28 Mar 2023 22:32:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680067954; cv=none; d=google.com; s=arc-20160816; b=rAvCYTBYMTxUCZn9Q+Vt2LR6mJbPIfd/Rax7YT8bP4BRlSoNZ/99rWoCUyhqjDzr+E bdZE056+exsg/zQ1igovnK61ncCMJTWR0MXmY4m4f0yscGt27zSLFl06s5pIEegDPql1 uc+yCwM+pz6am12epLBV/qWCgko5NlK+mjVupoI4jhbhwkGdtr9ud8fErGxcUE+OwPjj yoBbZfhkuAhXmEnXNlyWei7RpT1+MDPc7yNjQrDAUBkURKTX2vgp6GOP6XCUw6cVn+jR CbNpJEZ/nXbfPIaJ+571FqiSzeRipfa8hWOnVMOBbHi2LMx/3hh5kzBcSSadkWAFgIP3 ucfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Qh1V0LIX32Q2iQ7BrWToezKcW+WTS8mmuAS/fUlTBqM=; b=MEGsrwFVx6MAHwAntcE0HamyTd63jJg4xkd0RAcG6pEIGGijm4rjMiucGxVlmB8jOP YQ1NCAPLafgICnuECAfydwEhsz1xLGTmVgO+yFHXpqknCZ5kRjJLMNe4JX1j66pN5WPT uKhBandyh0NQVPZ2/jeJ8m+MdyK6xRe2wvYUAarT1I59uKnOQzrXm4YuBHK38LsVHyYz D3GX1RCeW5ngXziJHCdfk+XfOPQkKQHFdAG61JPXuolyRgdUOs96mdpaeD2JE7hwy0bj 5DrqvJnHjG4o20zVjSxs/MDbXw3LE8jlGjgjIQN67vBrg7wm3Qb/p3PBZPPjkrCIeLDy QeXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=UVvpeJ9M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 15-20020a170906328f00b008d02052ed2asi34549459ejw.637.2023.03.28.22.32.09; Tue, 28 Mar 2023 22:32:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=UVvpeJ9M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229706AbjC2Fah (ORCPT + 99 others); Wed, 29 Mar 2023 01:30:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229695AbjC2Fag (ORCPT ); Wed, 29 Mar 2023 01:30:36 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D34135A0 for ; Tue, 28 Mar 2023 22:30:34 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id bg13-20020a05600c3c8d00b003ef90adc168so1027467wmb.5 for ; Tue, 28 Mar 2023 22:30:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680067832; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Qh1V0LIX32Q2iQ7BrWToezKcW+WTS8mmuAS/fUlTBqM=; b=UVvpeJ9M5UrnRVLYVaA+ohxbH+4i23vDML6dK9MhzSiuBxg/T2ouoUrKCwyhwrp8+L EQWSjvLfj1As7OK1wfPciLl1w94+YjgnzKAcVIJZ8Qt/vZ+JlNZ05rKvkLQCVCExLUiz E3JIUH0hNpj6saw60WamkBGPPDCCZrqm/exwM4vqVNBr9YCFSiiXXBDAz+aPww54y4GU wZIjc1rAYPH4W55NSVdumNZQWkz88Hks5mZ2dq73XHtMYZXRw35QHJwIWFJSVBr1oz6O MyE3ZqCl6mE3hMQjIP8TEOaMxp43pC8EgL3Ng4og3AH8Tfhcw7zOoQaCkEmr846rwAoc 4O8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680067832; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Qh1V0LIX32Q2iQ7BrWToezKcW+WTS8mmuAS/fUlTBqM=; b=k9ZM1nYTX0Z4YggobnBqXczrD0J8JUIUC7SnLLyus3se+3ab4liZqh/VI1a7rd4ftj 4HLOfmHwLCUDLZP3p4pQsmA/FQoJor+4opzgKUxHgy9EFO8ZBwMu9Qtp8Wwx3tHcyR96 AY8fhN2b0BGIidq3cM+y+ozUDcwXbvowNIn6WvSut0Vcm5Z7alCWkzeVpGgn+QcU5Lwu A47C9Rak+9d7ZcvLnRktWp4RmS6EJ19H8OgoHnpI1+NNHL5fUU4e6dIB+4yvcF76i+4q 229XwrZ7YS46G62gftDkhzrcq5Rsy+EvCr1pMlPFZ+7LekaNqU5mlezTIYR31sCTJHFu 3RQA== X-Gm-Message-State: AO0yUKXUn4aSiVZS9QQegElcc2AVj9Sz/HWj31aJUafm2nGYDY9197XE P1TPJz4lntHnc9csT5oQ4Soih33skySs9WudkDg= X-Received: by 2002:a05:600c:214d:b0:3eb:39e7:3607 with SMTP id v13-20020a05600c214d00b003eb39e73607mr13689444wml.4.1680067832558; Tue, 28 Mar 2023 22:30:32 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id f8-20020a7bcc08000000b003ef64affec7sm863719wmh.22.2023.03.28.22.30.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 22:30:32 -0700 (PDT) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Ard Biesheuvel , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Alexandre Ghiti Subject: [PATCH v2 1/4] riscv: Introduce virtual kernel mapping KASLR Date: Wed, 29 Mar 2023 07:29:23 +0200 Message-Id: <20230329052926.69632-2-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230329052926.69632-1-alexghiti@rivosinc.com> References: <20230329052926.69632-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org KASLR implementation relies on a relocatable kernel so that we can move the kernel mapping. The seed needed to virtually move the kernel is taken from the device tree, so we rely on the bootloader to provide a correct seed. Zkr could be used unconditionnally instead if implemented, but that's for another patch. Signed-off-by: Alexandre Ghiti --- arch/riscv/Kconfig | 18 ++++++++++++++ arch/riscv/include/asm/page.h | 3 +++ arch/riscv/kernel/pi/Makefile | 2 +- arch/riscv/kernel/pi/cmdline_early.c | 13 ++++++++++ arch/riscv/kernel/pi/fdt_early.c | 30 +++++++++++++++++++++++ arch/riscv/mm/init.c | 36 +++++++++++++++++++++++++++- 6 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/kernel/pi/fdt_early.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 6ff9f574195d..af23750543c7 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -576,6 +576,24 @@ config RELOCATABLE If unsure, say N. +config RANDOMIZE_BASE + bool "Randomize the address of the kernel image" + select RELOCATABLE + help + Randomizes the virtual address at which the kernel image is + loaded, as a security feature that deters exploit attempts + relying on knowledge of the location of kernel internals. + + It is the bootloader's job to provide entropy, by passing a + random u64 value in /chosen/kaslr-seed at kernel entry. + + When booting via the UEFI stub, it will invoke the firmware's + EFI_RNG_PROTOCOL implementation (if available) to supply entropy + to the kernel proper. In addition, it will randomise the physical + location of the kernel Image as well. + + If unsure, say N. + endmenu # "Kernel features" menu "Boot options" diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h index ea1a0e237211..e0c8a1c8b22a 100644 --- a/arch/riscv/include/asm/page.h +++ b/arch/riscv/include/asm/page.h @@ -107,6 +107,7 @@ typedef struct page *pgtable_t; struct kernel_mapping { unsigned long page_offset; unsigned long virt_addr; + unsigned long virt_offset; uintptr_t phys_addr; uintptr_t size; /* Offset between linear mapping virtual address and kernel load address */ @@ -186,6 +187,8 @@ extern phys_addr_t __phys_addr_symbol(unsigned long x); #define sym_to_pfn(x) __phys_to_pfn(__pa_symbol(x)) +unsigned long kaslr_offset(void); + #endif /* __ASSEMBLY__ */ #define virt_addr_valid(vaddr) ({ \ diff --git a/arch/riscv/kernel/pi/Makefile b/arch/riscv/kernel/pi/Makefile index 42c58f4ab53b..edb3218bfb36 100644 --- a/arch/riscv/kernel/pi/Makefile +++ b/arch/riscv/kernel/pi/Makefile @@ -35,5 +35,5 @@ $(obj)/string.o: $(srctree)/lib/string.c FORCE $(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE $(call if_changed_rule,cc_o_c) -obj-y := cmdline_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o +obj-y := cmdline_early.pi.o fdt_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o extra-y := $(patsubst %.pi.o,%.o,$(obj-y)) diff --git a/arch/riscv/kernel/pi/cmdline_early.c b/arch/riscv/kernel/pi/cmdline_early.c index 05652d13c746..68e786c84c94 100644 --- a/arch/riscv/kernel/pi/cmdline_early.c +++ b/arch/riscv/kernel/pi/cmdline_early.c @@ -14,6 +14,7 @@ static char early_cmdline[COMMAND_LINE_SIZE]; * LLVM complain because the function is actually unused in this file). */ u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa); +bool set_nokaslr_from_cmdline(uintptr_t dtb_pa); static char *get_early_cmdline(uintptr_t dtb_pa) { @@ -60,3 +61,15 @@ u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa) return match_noXlvl(cmdline); } + +static bool match_nokaslr(char *cmdline) +{ + return strstr(cmdline, "nokaslr"); +} + +bool set_nokaslr_from_cmdline(uintptr_t dtb_pa) +{ + char *cmdline = get_early_cmdline(dtb_pa); + + return match_nokaslr(cmdline); +} diff --git a/arch/riscv/kernel/pi/fdt_early.c b/arch/riscv/kernel/pi/fdt_early.c new file mode 100644 index 000000000000..899610e042ab --- /dev/null +++ b/arch/riscv/kernel/pi/fdt_early.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include + +/* + * Declare the functions that are exported (but prefixed) here so that LLVM + * does not complain it lacks the 'static' keyword (which, if added, makes + * LLVM complain because the function is actually unused in this file). + */ +u64 get_kaslr_seed(uintptr_t dtb_pa); + +u64 get_kaslr_seed(uintptr_t dtb_pa) +{ + int node, len; + fdt64_t *prop; + u64 ret; + + node = fdt_path_offset((void *)dtb_pa, "/chosen"); + if (node < 0) + return 0; + + prop = fdt_getprop_w((void *)dtb_pa, node, "kaslr-seed", &len); + if (!prop || len != sizeof(u64)) + return 0; + + ret = fdt64_to_cpu(*prop); + *prop = 0; + return ret; +} diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 3ad771571c2d..3e535a88a065 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -1033,11 +1033,45 @@ static void __init pt_ops_set_late(void) #endif } +#ifdef CONFIG_RANDOMIZE_BASE +extern bool __init __pi_set_nokaslr_from_cmdline(uintptr_t dtb_pa); +extern u64 __init __pi_get_kaslr_seed(uintptr_t dtb_pa); + +static int __init print_nokaslr(char *p) +{ + pr_info("Disabled KASLR"); + return 0; +} +early_param("nokaslr", print_nokaslr); + +unsigned long kaslr_offset(void) +{ + return kernel_map.virt_offset; +} +#endif + asmlinkage void __init setup_vm(uintptr_t dtb_pa) { pmd_t __maybe_unused fix_bmap_spmd, fix_bmap_epmd; - kernel_map.virt_addr = KERNEL_LINK_ADDR; +#ifdef CONFIG_RANDOMIZE_BASE + if (!__pi_set_nokaslr_from_cmdline(dtb_pa)) { + u64 kaslr_seed = __pi_get_kaslr_seed(dtb_pa); + u32 kernel_size = (uintptr_t)(&_end) - (uintptr_t)(&_start); + u32 nr_pos; + + /* + * Compute the number of positions available: we are limited + * by the early page table that only has one PUD and we must + * be aligned on PMD_SIZE. + */ + nr_pos = (PUD_SIZE - kernel_size) / PMD_SIZE; + + kernel_map.virt_offset = (kaslr_seed % nr_pos) * PMD_SIZE; + } +#endif + + kernel_map.virt_addr = KERNEL_LINK_ADDR + kernel_map.virt_offset; kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL); #ifdef CONFIG_XIP_KERNEL -- 2.37.2