From patchwork Mon Jun 10 06:32:37 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: 13691565 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 93310C27C6E for ; Mon, 10 Jun 2024 06:34:00 +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: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=T2kPueQnz56VDjk+FDbpE8QnRlqddlwz1fJsgTXHP1g=; b=oVJMFhwUaG8WS3NZbAkXgk5VmQ JrFEVBzzv57004WgNVF2+QwXgp7knvPzkG+Ya2qOUe5QTopKA++GhT7OqJ+PuTr4gY64GcVq0hJh1 bR47+cfMaIvQvu8KWTMlmOCo+1nY0+KUXf/tmklbY79E6edsZwSlUMruUbW9xcN6KhSOemJJDR8k+ A6nFCpPedfOzoGiGPU6HqA6SYdjduanV57X5MHIvtMk8lDWqmNPi5k0rKH0GZrlfaoy2IU2HiPzer 7XCOPfGro4pNv1XHlWk+hnY6spkJnTmNF9ELADZnBxQBgMEY7eqGTyZEJjSn8udgqMjISyngN58CM gzK8O70Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sGYb9-00000003zZn-2dzd; Mon, 10 Jun 2024 06:33:43 +0000 Received: from mail-ed1-x54a.google.com ([2a00:1450:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sGYaq-00000003zGy-40bc for linux-arm-kernel@lists.infradead.org; Mon, 10 Jun 2024 06:33:27 +0000 Received: by mail-ed1-x54a.google.com with SMTP id 4fb4d7f45d1cf-57c748dd112so677874a12.0 for ; Sun, 09 Jun 2024 23:33:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718001200; x=1718606000; darn=lists.infradead.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=/3Fqo6uZ6WACSXr1SiKgfaQUsW+tD2BveY2g61YybgU=; b=uJpv5cQqJ3qtdJmX60z2hDV2SkUQTwyO0pPhtoOnwer+bX+fapQnPsbN4s9raFhgYT mpfc7AV1lPRyOv3TPBekD92UhyLW76vvPd9k7lO/93UqQst8IsLdc0ZrPUqS5u56XIr8 CV9p9vVg+6hpgYJt/yfECw6xNxnuFmAqS9u3M7hISpL49ipi7w0OEFcoQtYSfUALScpM zixSlYwAxtBEtMiJDmIx5Gd4l6ta/PdCzB0YrBIL7qBk+xEX0BsiAUKCYjVPwuOb0Dks xEsA+XUMqaV5iPXaAEuydFwF0iKMtlAYW60tR19GESUGqdzowtNfBxYjdNTqItKj1nZ2 G/jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718001200; x=1718606000; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=/3Fqo6uZ6WACSXr1SiKgfaQUsW+tD2BveY2g61YybgU=; b=Yom+OFyfcGY+UCl7ACCfPjJDwWd4BSsyZHtAbGoDeSHOd7YQWazsSFTsIT17Srxxno 3rToO2zkdiCUu3Oumo5jaP2xpDPEkYjWyJ2pbGQEh/hReFGDOLlcDXGA4HYHw10WHKa1 yzFBDT0P05aJspVgNyJSp7+sgzNhe8fIboOVV53eWa9kqLuVVbhbz22wPc6zwhJtvL30 yOD+4T6Bn1VDEVq5KmVGxvYEO3YwYYPf8aQJtl3MptC1I4zF6SqeuP1j85wLfFv1MGa6 aLv3yeFwVAT3v3C1nm6gevj4fb+HhB0ZkoPeM3ahSdz84m/J8UMLTlSjBvUQ8/GRQf+b LFFA== X-Forwarded-Encrypted: i=1; AJvYcCU/e11MLCoPZAUMe9kLhFH4InYYOOAxuC/BE0ruWFAFHswpAr4To2G6RjFMJRGbcO0E11oKjLjnm4pxXyVE5Vfsi6hrqkYNUdvyVU4PIC9W/ZDBmsI= X-Gm-Message-State: AOJu0Yzlr4wvRRaH7B+AWi128dz1sTGRvFrsUzIJaN2xdDilK/BsEW7u 0wwOVrNoeyCOG+Cnlfe/CDe1+0ra2RC24xHBKKejdKK4UjVXMU8/GN7czRdjWEETGXDWoPY+sQ= = X-Google-Smtp-Source: AGHT+IGl14UbfzJFzP5FVcOdHdoo6VR8VgccIp2YuEQvM8uJdIjgJCFM5cwo5OdJoioDTQ1NLN2OfjF80w== X-Received: from ptosi.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:11ec]) (user=ptosi job=sendgmr) by 2002:a05:6402:381b:b0:57c:803e:3a57 with SMTP id 4fb4d7f45d1cf-57c803e3c99mr3092a12.2.1718001200147; Sun, 09 Jun 2024 23:33:20 -0700 (PDT) Date: Mon, 10 Jun 2024 07:32:37 +0100 In-Reply-To: <20240610063244.2828978-1-ptosi@google.com> Mime-Version: 1.0 References: <20240610063244.2828978-1-ptosi@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240610063244.2828978-9-ptosi@google.com> Subject: [PATCH v5 8/8] KVM: arm64: nVHE: Support CONFIG_CFI_CLANG at EL2 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?= " , Will Deacon , 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-20240609_233325_091759_ED82A261 X-CRM114-Status: GOOD ( 17.29 ) 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 The compiler implements kCFI by adding type information (u32) above every function that might be indirectly called and, whenever a function pointer is called, injects a read-and-compare of that u32 against the value corresponding to the expected type. In case of a mismatch, a BRK instruction gets executed. When the hypervisor triggers such an exception in nVHE, it panics and triggers and exception return to EL1. Therefore, teach nvhe_hyp_panic_handler() to detect kCFI errors from the ESR and report them. If necessary, remind the user that EL2 kCFI is not affected by CONFIG_CFI_PERMISSIVE. Pass $(CC_FLAGS_CFI) to the compiler when building the nVHE hyp code. Use SYM_TYPED_FUNC_START() for __pkvm_init_switch_pgd, as nVHE can't call it directly and must use a PA function pointer from C (because it is part of the idmap page), which would trigger a kCFI failure if the type ID wasn't present. Signed-off-by: Pierre-Clément Tosi Acked-by: Will Deacon --- arch/arm64/kvm/handle_exit.c | 10 ++++++++++ arch/arm64/kvm/hyp/nvhe/Makefile | 6 +++--- arch/arm64/kvm/hyp/nvhe/hyp-init.S | 6 +++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index b3d6657a259d..69b08ac7322d 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -417,6 +417,14 @@ static void print_nvhe_hyp_panic(const char *name, u64 panic_addr) (void *)(panic_addr + kaslr_offset())); } +static void kvm_nvhe_report_cfi_failure(u64 panic_addr) +{ + print_nvhe_hyp_panic("CFI failure", panic_addr); + + if (IS_ENABLED(CONFIG_CFI_PERMISSIVE)) + kvm_err(" (CONFIG_CFI_PERMISSIVE ignored for hyp failures)\n"); +} + void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, u64 elr_virt, u64 elr_phys, u64 par, uintptr_t vcpu, @@ -446,6 +454,8 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, kvm_err("nVHE hyp BUG at: %s:%u!\n", file, line); else print_nvhe_hyp_panic("BUG", panic_addr); + } else if (IS_ENABLED(CONFIG_CFI_CLANG) && esr_is_cfi_brk(esr)) { + kvm_nvhe_report_cfi_failure(panic_addr); } else { print_nvhe_hyp_panic("panic", panic_addr); } diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile index 50fa0ffb6b7e..782b34b004be 100644 --- a/arch/arm64/kvm/hyp/nvhe/Makefile +++ b/arch/arm64/kvm/hyp/nvhe/Makefile @@ -89,9 +89,9 @@ quiet_cmd_hyprel = HYPREL $@ quiet_cmd_hypcopy = HYPCOPY $@ cmd_hypcopy = $(OBJCOPY) --prefix-symbols=__kvm_nvhe_ $< $@ -# Remove ftrace, Shadow Call Stack, and CFI CFLAGS. -# This is equivalent to the 'notrace', '__noscs', and '__nocfi' annotations. -KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS) $(CC_FLAGS_CFI), $(KBUILD_CFLAGS)) +# Remove ftrace and Shadow Call Stack CFLAGS. +# This is equivalent to the 'notrace' and '__noscs' annotations. +KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) # Starting from 13.0.0 llvm emits SHT_REL section '.llvm.call-graph-profile' # when profile optimization is applied. gen-hyprel does not support SHT_REL and # causes a build failure. Remove profile optimization flags. diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-init.S b/arch/arm64/kvm/hyp/nvhe/hyp-init.S index 3a2836a52e85..07120b37da35 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-init.S +++ b/arch/arm64/kvm/hyp/nvhe/hyp-init.S @@ -5,6 +5,7 @@ */ #include +#include #include #include @@ -268,8 +269,11 @@ SYM_CODE_END(__kvm_handle_stub_hvc) /* * void __pkvm_init_switch_pgd(phys_addr_t pgd, unsigned long sp, * void (*fn)(void)); + * + * SYM_TYPED_FUNC_START() allows C to call this ID-mapped function indirectly + * using a physical pointer without triggering a kCFI failure. */ -SYM_FUNC_START(__pkvm_init_switch_pgd) +SYM_TYPED_FUNC_START(__pkvm_init_switch_pgd) /* Turn the MMU off */ pre_disable_mmu_workaround mrs x3, sctlr_el2