From patchwork Wed May 29 12:12:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Pierre-Cl=C3=A9ment_Tosi?= X-Patchwork-Id: 13678763 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DBAABC25B75 for ; Wed, 29 May 2024 12:13:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: Mime-Version:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=isPlNebLwncN7CxI+diF7utDioXkbj9X3ItabzLnNaQ=; b=V21 s7bSMOYIiNoKozjJBuvrWEiSjhjqjcCAb5ggKEc8RNG0YU5b6spWZtO5RVni55T1juCUMXHiS4NiX gkkJyTiYMewBCD/4ZgBTuiDtRZB+7DMwuQRwKfx8dDYyBnH9//pC+WXM84F8P4EtzlemSuNLgrwR/ 8OjMnMvqgTuDOwEbJp4w2T+qiFwe+M5LB+r/yNzfbsc7i+sYQXxjcEVkF5gKNv1pM2VTjtUNHCdYP Z+l5dyVg0v1X4Qu7ZgLxdXoNNEDAZ8Q8ea8pRotEBXjxcb7zla/ctGPWwPoD1t1OPvV60TQt4f/PE mqmMf/HA9DgM9yJdjzzwf34za5vJMNA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sCIBN-000000042YA-3Tq0; Wed, 29 May 2024 12:13:30 +0000 Received: from mail-ed1-x549.google.com ([2a00:1450:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sCIBK-000000042XB-1BoG for linux-arm-kernel@lists.infradead.org; Wed, 29 May 2024 12:13:27 +0000 Received: by mail-ed1-x549.google.com with SMTP id 4fb4d7f45d1cf-5785f7b847cso1177371a12.0 for ; Wed, 29 May 2024 05:13:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716984802; x=1717589602; darn=lists.infradead.org; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=KHM4hvPISZ5Fqfl4GlcTR57oUno+i9soKGwTyex86bs=; b=4KKpppSjtmaROJ2drhRoLfDaYmYWT01dq/HT/D33Y0Gw6IdHfU11AisRdWJRnhDVHR hMuiJTXYg6DqssQZoBYrOWK1tbgxKGk3mJuePDU0kZ7+9K6NPUfn1JGaTfedNLEvD0rV sSq7ieNxmQJVkhAWicKL7SKf98aGnVNj7QmmdcCiRH+zpK/RpXLwA8CLKpKUEkJTJl+m al3rNRfyMT7SERcGhE7QQKB8V4Nptwsedz6gS6wkMPdcX5elnNYDGLwuvDA1XPY6ly5z IAGxxgZ4EQlpooy6m7VqXuDs5zdnX1CfN/vDtAtiFDrrPifvNkbf8WcF7OCiVwE/HTKt 10iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716984802; x=1717589602; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=KHM4hvPISZ5Fqfl4GlcTR57oUno+i9soKGwTyex86bs=; b=tGlh7NI6NuxTozpmGQlJVnOCeuQwkM8j0iw5zakesr1V/tXLhb4X5ZC+KMYc5fxMDZ 7Eg1fA40r2Ql+CEHrT/Ybqddw/X32F4dcgexyKjgGPwALLV1b5S/5lRxBSyNK446L3Rr ef9sVLcN5MLjzuCaZNMeMK//MOoCR0CCeaFJ/NkXojrUwc3A48mNVlq2tt+sHGYScvci q9I3xsx/UoDsgjdeQv1lzS7rwMTKZO38VfjD3FPbtVKES47p1UEizD/ncdqqjpq3WDV9 qkLv274qfjkwN7s2reDDXf/g4Dz+B9nh8rlR/RG9WNVaPa3Pt7AOAuvCIYT8JBn0N5I8 245g== X-Forwarded-Encrypted: i=1; AJvYcCUkxGRNIYztCefpUBtAY/ytr39qLAxT4PEoec/30V0uyWugf1/rhXLdywd8gX43GbpiqvKqWUuSfg4BgnkaNemchk4ET/Ri0IpRaqfxPPNeOBOly7w= X-Gm-Message-State: AOJu0Yx/zO1NVZasqI/pDOfiJaT8HmxE4lb07Iu0+ZihSCXjBag2GUt4 PL2prf+NfEIIR8cZuY6zY38hsNlKyoL5aBZs6SH/EFelVmp4HLkTfeVoayxgnEh5VoszZQF/CA= = X-Google-Smtp-Source: AGHT+IGy4WL2CP5PklvEqxujQTD6yc2fhlZRBTpxJ6g4wgBAaDOJBrprVoBIpT2DiBPtsVpg198JthWjYw== X-Received: from ptosi.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:11ec]) (user=ptosi job=sendgmr) by 2002:a05:6402:3884:b0:578:242b:aac4 with SMTP id 4fb4d7f45d1cf-57a03f586cdmr2540a12.2.1716984801909; Wed, 29 May 2024 05:13:21 -0700 (PDT) Date: Wed, 29 May 2024 13:12:06 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240529121251.1993135-1-ptosi@google.com> Subject: [PATCH v4 00/13] KVM: arm64: Add support for hypervisor kCFI From: " =?utf-8?q?Pierre-Cl=C3=A9ment_Tosi?= " To: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org Cc: " =?utf-8?q?Pierre-Cl=C3=A9ment_Tosi?= " , Marc Zyngier , Oliver Upton , Suzuki K Poulose , Vincent Donnefort X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240529_051326_445135_CA59CD7E X-CRM114-Status: GOOD ( 18.11 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org CONFIG_CFI_CLANG ("kernel Control Flow Integrity") makes the compiler inject runtime type checks before any indirect function call. On AArch64, it generates a BRK instruction to be executed on type mismatch and encodes the indices of the registers holding the branch target and expected type in the immediate of the instruction. As a result, a synchronous exception gets triggered on kCFI failure and the fault handler can retrieve the immediate (and indices) from ESR_ELx. This feature has been supported at EL1 ("host") since it was introduced by b26e484b8bb3 ("arm64: Add CFI error handling"), where cfi_handler() decodes ESR_EL1, giving informative panic messages such as [ 21.885179] CFI failure at lkdtm_indirect_call+0x2c/0x44 [lkdtm] (target: lkdtm_increment_int+0x0/0x1c [lkdtm]; expected type: 0x7e0c52a) [ 21.886593] Internal error: Oops - CFI: 0 [#1] PREEMPT SMP However, it is not or only partially supported at EL2: in nVHE (or pKVM), CONFIG_CFI_CLANG gets filtered out at build time, preventing the compiler from injecting the checks. In VHE, EL2 code gets compiled with the checks but the handlers in VBAR_EL2 are not aware of kCFI and will produce a generic and not-so-helpful panic message such as [ 36.456088][ T200] Kernel panic - not syncing: HYP panic: [ 36.456088][ T200] PS:204003c9 PC:ffffffc080092310 ESR:f2008228 [ 36.456088][ T200] FAR:0000000081a50000 HPFAR:000000000081a500 PAR:1de7ec7edbadc0de [ 36.456088][ T200] VCPU:00000000e189c7cf To address this, - [01/13] fixes an existing bug where the ELR_EL2 was getting clobbered on synchronous exceptions, causing the wrong "PC" to be reported by nvhe_hyp_panic_handler() or __hyp_call_panic(). This is particularly limiting for kCFI, as it would mask the location of the failed type check. - [02/13] fixes a minor C/asm ABI mismatch which would trigger a kCFI failure - [03/13] to [09/13] prepare nVHE for CONFIG_CFI_CLANG and [10/13] enables it - [11/13] improves kCFI error messages by saving then parsing the CPU context - [12/13] adds a kCFI test module for VHE and [13/13] extends it to nVHE & pKVM As a result, an informative kCFI panic message is printed by or on behalf of EL2 giving the expected type and target address (possibly resolved to a symbol) for VHE, nVHE, and pKVM (iff CONFIG_NVHE_EL2_DEBUG=y). Note that kCFI errors remain fatal at EL2, even when CONFIG_CFI_PERMISSIVE=y. Changes in v4: - Addressed Will's comments on v3: - Removed save/restore of x0-x1 & used __guest_exit_panic ABI for new routines - Reworked __pkvm_init_switch_pgd new API with separate args - Moved cosmetic changes (renaming to __hyp_panic) into dedicated commit - Further clarified the commit message regarding R_AARCH64_ABS32 - early_brk64() uses esr_is_cfi_brk() (now introduced along esr_brk_comment()) - Added helper to display nvHE panic banner - Moved the test module to the end of the series Pierre-Clément Tosi (13): KVM: arm64: Fix clobbered ELR in sync abort/SError KVM: arm64: Fix __pkvm_init_switch_pgd call ABI KVM: arm64: nVHE: Simplify __guest_exit_panic path KVM: arm64: nVHE: Add EL2h sync exception handler KVM: arm64: Rename __guest_exit_panic __hyp_panic KVM: arm64: nVHE: gen-hyprel: Skip R_AARCH64_ABS32 KVM: arm64: VHE: Mark __hyp_call_panic __noreturn arm64: Introduce esr_comment() & esr_is_cfi_brk() KVM: arm64: Introduce print_nvhe_hyp_panic helper KVM: arm64: nVHE: Support CONFIG_CFI_CLANG at EL2 KVM: arm64: Improve CONFIG_CFI_CLANG error message KVM: arm64: VHE: Add test module for hyp kCFI KVM: arm64: nVHE: Support test module for hyp kCFI arch/arm64/include/asm/esr.h | 11 ++ arch/arm64/include/asm/kvm_asm.h | 3 + arch/arm64/include/asm/kvm_cfi.h | 38 +++++++ arch/arm64/include/asm/kvm_hyp.h | 3 +- arch/arm64/kernel/asm-offsets.c | 1 + arch/arm64/kernel/debug-monitors.c | 4 +- arch/arm64/kernel/traps.c | 8 +- arch/arm64/kvm/Kconfig | 20 ++++ arch/arm64/kvm/Makefile | 3 + arch/arm64/kvm/handle_exit.c | 50 ++++++++- arch/arm64/kvm/hyp/cfi.c | 37 +++++++ arch/arm64/kvm/hyp/entry.S | 34 +++++- arch/arm64/kvm/hyp/hyp-entry.S | 4 +- arch/arm64/kvm/hyp/include/hyp/cfi.h | 47 +++++++++ arch/arm64/kvm/hyp/include/hyp/switch.h | 5 +- arch/arm64/kvm/hyp/nvhe/Makefile | 7 +- arch/arm64/kvm/hyp/nvhe/gen-hyprel.c | 6 ++ arch/arm64/kvm/hyp/nvhe/host.S | 21 ++-- arch/arm64/kvm/hyp/nvhe/hyp-init.S | 23 ++-- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 19 ++++ arch/arm64/kvm/hyp/nvhe/setup.c | 4 +- arch/arm64/kvm/hyp/nvhe/switch.c | 7 ++ arch/arm64/kvm/hyp/vhe/Makefile | 1 + arch/arm64/kvm/hyp/vhe/switch.c | 34 +++++- arch/arm64/kvm/hyp_cfi_test.c | 75 +++++++++++++ arch/arm64/kvm/hyp_cfi_test_module.c | 135 ++++++++++++++++++++++++ 26 files changed, 553 insertions(+), 47 deletions(-) create mode 100644 arch/arm64/include/asm/kvm_cfi.h create mode 100644 arch/arm64/kvm/hyp/cfi.c create mode 100644 arch/arm64/kvm/hyp/include/hyp/cfi.h create mode 100644 arch/arm64/kvm/hyp_cfi_test.c create mode 100644 arch/arm64/kvm/hyp_cfi_test_module.c