Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F6B8C636D4 for ; Wed, 15 Feb 2023 14:51:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229898AbjBOOvZ (ORCPT ); Wed, 15 Feb 2023 09:51:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229811AbjBOOvX (ORCPT ); Wed, 15 Feb 2023 09:51:23 -0500 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88C7D39B91 for ; Wed, 15 Feb 2023 06:51:17 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id co8so15666036wrb.1 for ; Wed, 15 Feb 2023 06:51:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ctWJSElYx/FpQ+Il/jr6fw9xbpiBuK2KMIX0fff726o=; b=GLyffEPMYlT/cmqtzo2hfct/e9+r4hBtyDtRC0QOsgxl5uG2zZYS2aCeBLUuFLsLqo 8+EWhE3A1okDf4bDVnsCqQh0zxTcTgI3IgDy3ipTGvN5RnxzX5bgBRFJ0Vj5GFgyKzQ+ wt8YZ2u0Imx2utyxu9hadRnNSm0/IAzax1OHpC5qcF7/RoD3qznLt+K6ps/tHRTi2Jnb piFePO8J/H/acKIh43ArzCYPygQ9tUbRj7rz3nDujjJEhM2YvC5y3L4VDSAtKs6HERy6 X2lnmKW9ak0eHD5b5b+67yNd4M4yCK7/yIO6dLFdEuI00JPlYwg/UgxhH7hOpj+2NrM6 aYQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ctWJSElYx/FpQ+Il/jr6fw9xbpiBuK2KMIX0fff726o=; b=Q05KJl2660ZoiYn24iCTegbhZYe+hv9Hr/nz1CNQLydS40kYPgOpIl1e4+HGRUEZt/ GOq9yxwaPg1LIR/AlR75DZyBdBGXmMmd+41/D039/9sBGeYicO5ntzkwklA46cLS4qR6 8x156n8e2qJYycaBe/XrzJhDFHECH55ELpd6k/cosy0KTZAakgPzmNkkYJyfdtSYZbpk VtnrayqhdPf17CE/ZNU5lCTWAaBVZZlYLK8NH32nTYdINnaJBTmWVgBbTfhAOQnuj27l TGO0VDM3BVpTfAqzRwOOY+uMOGCIud1BByod62wJdVYu4yNxcTbLf/2VlSBlpOnZGjN8 5YNA== X-Gm-Message-State: AO0yUKWwntT9OPtBiwjR+k7Rr9xUsP9brSqcO7EERgNsho14RSwHEmJl Q2ERvZdBEbZuCeJq11IA/c6A0A== X-Google-Smtp-Source: AK7set+Nq5t/4Q7aVf6eQyb5vxlI9IEku3M0RuLvSxT47U9GMbWRMLO9/X1s/whi16YDzCCafZULdA== X-Received: by 2002:adf:ecca:0:b0:2c5:5943:27e5 with SMTP id s10-20020adfecca000000b002c5594327e5mr1877958wro.15.1676472676157; Wed, 15 Feb 2023 06:51:16 -0800 (PST) Received: from alex-rivos.home (lfbn-lyo-1-450-160.w2-7.abo.wanadoo.fr. [2.7.42.160]) by smtp.gmail.com with ESMTPSA id m20-20020a056000181400b002c54e89fcadsm11022524wrh.55.2023.02.15.06.51.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Feb 2023 06:51:15 -0800 (PST) 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 Cc: Alexandre Ghiti Subject: [PATCH 0/4] riscv: Introduce KASLR Date: Wed, 15 Feb 2023 15:51:09 +0100 Message-Id: <20230215145113.465558-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Note that the patch "riscv: Use PUD/P4D/PGD pages for the linear mapping" is necessary to retrieve the memory below the physical kernel address, so that the EFI stub does not have to try to relocate the kernel as close as possible to the start of dram. This patchset is rebased on top of: Introduce 64b relocatable kernel (https://lore.kernel.org/lkml/20230215143626.453491-1-alexghiti@rivosinc.com/) RISC-V kasan rework (https://lore.kernel.org/lkml/Y6TTvku%2FyuSjm42j@spud/T/) riscv: Use PUD/P4D/PGD pages for the linear mapping (https://lore.kernel.org/lkml/20230125114229.hrhsyw4aegrnmoau@orel/T/) riscv: Allow to downgrade paging mode from the command line (https://lore.kernel.org/lkml/CAHVXubjeSMvfTPnvrnYRupOGx6+vUvUGfRS3piTeo=TH2cHKNg@mail.gmail.com/) base-commit-tag: v6.2-rc7 This patchset was tested with u-boot boottime service for the seed on: - ubuntu defconfig + kasan outline (sv39, sv48, sv57): OK - ubuntu defconfig + kasan inline (sv39, sv48, sv57): OK - ubuntu defconfg (sv39, sv48, sv57): OK Alexandre Ghiti (4): 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 riscv: libstub: Implement KASLR by using generic functions arch/riscv/Kconfig | 18 +++ arch/riscv/include/asm/page.h | 1 + arch/riscv/kernel/pi/Makefile | 2 +- arch/riscv/kernel/pi/cmdline_early.c | 12 ++ arch/riscv/kernel/pi/fdt_early.c | 23 +++ arch/riscv/kernel/setup.c | 25 +++ arch/riscv/mm/init.c | 31 +++- drivers/firmware/efi/libstub/arm64-stub.c | 110 +------------ .../firmware/efi/libstub/efi-stub-helper.c | 147 ++++++++++++++++++ drivers/firmware/efi/libstub/efistub.h | 8 + drivers/firmware/efi/libstub/riscv-stub.c | 27 ++-- 11 files changed, 281 insertions(+), 123 deletions(-) create mode 100644 arch/riscv/kernel/pi/fdt_early.c -- 2.37.2