From patchwork Fri May 10 11:26:29 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: 13661431 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 69C62C25B5F for ; Fri, 10 May 2024 11:27: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=ILIPFb10eVAviogA/9ISrCdFh+0fKB/xKAJUq28uiOk=; b=ngW +5h1/FKKlzlEhUA0VT9uhcrPMoUi4rxiv+QvqYqzkf04baGVTm/nylQ+EHzYd2RX5C6Kk+W8G5cPr hMRFbZCUQGiXttEk76OwtElj6cIYSx39d/YXy9KDadxU3XDbnTzH5iMvTszrHncnbSDs+j/g/Kzlh nixLIT86Np7yJOeSzNe2CAbmO661brSrSPZz12w8Abnc6y+f6xmUFHulzVyeKNC/rbezZ4tHP7ftZ eHiqEtO0v4hotk5DnRRy1yfhX6NBGURPciSFujUnPBxCW3PWKFsoDtrcMOZLGK8OkfWdkIZJSIH7Z aTwwZmT6Q45c8W9TauMuqg7HlSP/mAw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s5OPN-000000052zb-3USs; Fri, 10 May 2024 11:27:25 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s5OPL-000000052xm-0S0X for linux-arm-kernel@lists.infradead.org; Fri, 10 May 2024 11:27:24 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-de54ccab44aso3504756276.3 for ; Fri, 10 May 2024 04:27:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715340437; x=1715945237; 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=GeTpsY//IUL3F2M4pGNF9LnkrFBIxs/L6CWJHc93UMI=; b=uUheVho+Uv5m0RXs9ykKUPp6X85aiQT9fRNz4+6X2hKGK786FX3Vgj6k7Sh+QqCF9u hhrrWlr7423BFAQNF4VlYAEhw2edoPFRkEM2CtOFAwj2sGkUqpF2sG5h39ZvB/gGKLSA 120UFogIix5Z5R7celKzUf3FSuh+Mars2MTVlXhwlljOrPsFpbiXX0kbVol3oVUoaEKf lSBCXsPiE1gtv3iDxT/wTb7rOT/bY3/twXOHbjNSxjj4Nn/N7kRQug/BED7+0nYtf0zZ n3M6R7m+1p5YCuj2fJqP9Mjax7VRjNWYQnhAdPaLiTU+n2m1sunjGjbroTXhCesdXJIs oSmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715340437; x=1715945237; 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=GeTpsY//IUL3F2M4pGNF9LnkrFBIxs/L6CWJHc93UMI=; b=fM5b7RpKqArtpaRKNb+MaTdC6BbK5oOOlZgSo/sYBx9tfhzOZ9nxuB569ZVtXe8uwE WAdfZ50Ykikes5GJmAhMLUuzOEHc0i5BIcPrH4+kva9JOW+hee5mz+C+kvYiK+Go7WNC 4woVNN/jHAq0/geoawxocyWOmHszbz1ptlYPWgtsZZ5pVIyJhTuJ2kfuV4Vza/MSwhtK gBFYYnJnpGzxo1KNNUXBeJNGb2ItPXYYbvG97SZWC+FezPiT5y4aMOEG7kEod6EPY/Ag Eko0s8zk+1Nnqe24eVGE/6fM2wahmafQOdlOqepCIzEO7u6YM8Xri/3KoieKF29hzaG3 MY1A== X-Forwarded-Encrypted: i=1; AJvYcCV6eDlLsg3YMxE43HvNaUJtHhbyfUT0o9asui5Y22YMopcIpJU5Ty1oGRLm1MKlfRmntTFf+kgiiN+lqeJIqagMDRCI6FfEWVqY+7V7QoK+Lo1s880= X-Gm-Message-State: AOJu0YykEivVPRAzWmW8xiQO2cjg9YMBG2Fp788CpJWqcTJLb4pG+I9H QhIPn2XtWL2tYRLLTrxxBRod0rfs6zwuqbjZ9a0o0WrV1iVlP9a2QIqKX6hpzmGplk+MMxzaDA= = X-Google-Smtp-Source: AGHT+IH8LcT7Mlvj6sPM9cJjFdpOx3dU6FNulJiXugTsKM/5ei+quZrDmLQDkQIioDTzRKYsUovHynihLw== X-Received: from ptosi.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:11ec]) (user=ptosi job=sendgmr) by 2002:a05:6902:100f:b0:dee:5d43:a0f3 with SMTP id 3f1490d57ef6-dee5d43c91fmr54501276.6.1715340437656; Fri, 10 May 2024 04:27:17 -0700 (PDT) Date: Fri, 10 May 2024 12:26:29 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.45.0.118.g7fe29c98d7-goog Message-ID: <20240510112645.3625702-1-ptosi@google.com> Subject: [PATCH v3 00/12] 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-20240510_042723_195167_F6686EB4 X-CRM114-Status: GOOD ( 17.20 ) 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/12] 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/12] & [03/12] (resp.) fix and improve __pkvm_init_switch_pgd for kCFI - [04/12] to [06/12] prepare nVHE for CONFIG_CFI_CLANG and [10/12] enables it - [12/12] improves kCFI error messages by saving then parsing the CPU context - [09/12] adds a kCFI test module for VHE and [11/12] 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. Change in v3: - Reworked the commit message of [04/12] - (no code changes since v2, questions on v1 remain open) Changes in v2: - Added 2 commits implementing a test module for hyp kCFI; - For __kvm_hyp_host_vector, dropped changes to the sync EL2t handler and kept the SP overflow checks; - Used the names __guest_exit_restore_elr_and_panic, esr_brk_comment; - Documented the use of SYM_TYPED_FUNC_START for __pkvm_init_switch_pgd; - Fixed or clarified commit messages. Pierre-Clément Tosi (12): KVM: arm64: Fix clobbered ELR in sync abort/SError KVM: arm64: Fix __pkvm_init_switch_pgd C signature KVM: arm64: Pass pointer to __pkvm_init_switch_pgd KVM: arm64: nVHE: Remove __guest_exit_panic path KVM: arm64: nVHE: Add EL2h sync exception handler KVM: arm64: nVHE: gen-hyprel: Skip R_AARCH64_ABS32 KVM: arm64: VHE: Mark __hyp_call_panic __noreturn arm64: Move esr_comment() to KVM: arm64: VHE: Add test module for hyp kCFI KVM: arm64: nVHE: Support CONFIG_CFI_CLANG at EL2 KVM: arm64: nVHE: Support test module for hyp kCFI KVM: arm64: Improve CONFIG_CFI_CLANG error message 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 | 4 +- 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 | 39 ++++++- arch/arm64/kvm/hyp/cfi.c | 37 +++++++ arch/arm64/kvm/hyp/entry.S | 43 +++++++- 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 | 20 ++-- arch/arm64/kvm/hyp/nvhe/hyp-init.S | 18 +++- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 19 ++++ arch/arm64/kvm/hyp/nvhe/setup.c | 6 +- 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(+), 42 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