Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2444570pxm; Fri, 25 Feb 2022 01:59:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJy7sAVs9TFFjoLU9y9SQuH0DkUZ4GWAj8lX4FN3tC7Ju9l1yhCQV8FAA6zLu2vpnnhVSwTF X-Received: by 2002:a17:906:a145:b0:6ae:e45d:15b6 with SMTP id bu5-20020a170906a14500b006aee45d15b6mr5559354ejb.714.1645783187945; Fri, 25 Feb 2022 01:59:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645783187; cv=none; d=google.com; s=arc-20160816; b=U/YT5+zhz43Y4nUHXvvmZ22IeyFaBDjuUW93lkAvqlicDG8K8P/aPeiB4sRRn+bIEY eSt8o+s8Fz9iW3Vejjdus1FyOvAa6TpNLrnLXCiCK1P/9KWNytf3Kkw53DfdT50vMfLQ AYpv7DDebTbKZmJMomoO/GhTXuVDTohjryTWZx0Q6b+K/nwy+H6kAG6AGU3vShohxyz5 I5wJuJQISTyvvXSC6HbnJ4Vp0za0mblMwgvBEd5q1K8pk6IQg00rHz0NfsTiMM3q+t6l t4OSgqs/2rGrmp9fY2cX1BEWG9LCcPwG3iy5xK530zv+oXC9ZJ8LcfKbvNJyot9nIGcW QpKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:cc:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=N53fsYbjdomZEqC08ZX2aLHLeu/DgUziJYRqEwUtI1A=; b=h2lmhW6LkBvQTtnnIBXBfcLvdRfDmx49FRu49b5DTZzclU4Jsref+7GWDs/npqPVe0 Zh+gBoZU1x068Ug+RFwEafrm3YNflDCdvARELMXhoxl18nAq1eEBCH/R9CWiXBhxRSdY uXMAewdok5wpNklsfgPiwwKnKW9pe1tQ3RPL1k/94rHPDt2zNxz/x47XKVdP4QcmQL0d 9HXnfOh6A3awrDvZpTzMqkSVR4/wxxe9BLEfn9nZpgZLdOIVsXspnsC4tBswTFWQ9JAl Vm0JJb/Zp8DLCDoGTgoBM0oPdQxR8W7whLsj1y7OtMa25aYzUHFA9hlqw6CfCoelJAcS C4YA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="RDsCf9B/"; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v9-20020a1709063bc900b006cfd3c7fd67si1200569ejf.636.2022.02.25.01.59.22; Fri, 25 Feb 2022 01:59:47 -0800 (PST) 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=@google.com header.s=20210112 header.b="RDsCf9B/"; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237129AbiBYDo0 (ORCPT + 99 others); Thu, 24 Feb 2022 22:44:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237095AbiBYDoZ (ORCPT ); Thu, 24 Feb 2022 22:44:25 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2342D21CD29 for ; Thu, 24 Feb 2022 19:43:54 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id b64-20020a256743000000b0061e169a5f19so2014407ybc.11 for ; Thu, 24 Feb 2022 19:43:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:cc; bh=N53fsYbjdomZEqC08ZX2aLHLeu/DgUziJYRqEwUtI1A=; b=RDsCf9B/tbqpBWDVVLA0Uni/cUxBssEL82brdpQDflFscrjWeDp/IrFbmB2lXkwnOT +aBRWNfkxrYjNC06MXJX+XbipoXeQdIouEFph61JxnLy+dAT8wBgGgTRmpGb6ZeE08OW 7pwzwrlXeD3rOPlWuIroN5SUiUVJPqFsleOthEEoxub28NOvTT+YyUVHv2PlkCalpcgo qgwxVpdwC96gSO2gBGYZYG9DZwu2zh03MCxlgz5iq6nL9h/OBROE4CheyUhRwlryYluZ Pe15eTYZFhbKKqb8Vi5+4TLV3djTewGaBaZDmTc1qVLOTEnaHckwbPlSu9/2sm80a6Zj uLlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:cc; bh=N53fsYbjdomZEqC08ZX2aLHLeu/DgUziJYRqEwUtI1A=; b=0WepXWXAGGYV9bUMtzotkJ/79xNILyu8Z7EEv6j7Y0tYaCFXM35WVruwPHb9i0f93G 8KxzUPe8kWfr2J+CcvImTL545A6h+SYYHbVfeKE/nU/mKEgLOIch9hIoj6IUVUcy3jbp pAAICBSQgcyUL2/NOD2CoDCFOP3ZOiQPpD6EHRQVUwr+Vim711jhy2DioJ2XByX7GaNW LE4RhdKn7uW1mSqVaxvzkq7bMH/dSa6/JrkG3Vs2Uw1eK+Z1iMQdMKW0hv/Sw/L0HpPC Synok3EkrfVjZMh3pGNGin+SUCvTHEEannvpwBb68PUIuOwtPGzBF9GAY/iDKUHPhtzM xWuA== X-Gm-Message-State: AOAM530Nq1le9lr3c9FJiJI5Vmp02fGrVNfX5odydS9iinmeumKYVQu9 oFLVrL2TSeMsZSpixQUXtjfowXRp29hboc4zQg== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:3a50:90b2:e6a2:9060]) (user=kaleshsingh job=sendgmr) by 2002:a25:df51:0:b0:624:5c91:8e76 with SMTP id w78-20020a25df51000000b006245c918e76mr5460126ybg.337.1645760633363; Thu, 24 Feb 2022 19:43:53 -0800 (PST) Date: Thu, 24 Feb 2022 19:34:50 -0800 In-Reply-To: <20220225033548.1912117-1-kaleshsingh@google.com> Message-Id: <20220225033548.1912117-6-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220225033548.1912117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH v4 5/8] KVM: arm64: Detect and handle hypervisor stack overflows From: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Kalesh Singh , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Mark Rutland , Mark Brown , Masami Hiramatsu , Peter Collingbourne , "Madhavan T. Venkataraman" , Andrew Scull , Paolo Bonzini , Andrew Jones , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MISSING_HEADERS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The hypervisor stacks (for both nVHE Hyp mode and nVHE protected mode) are aligned to twice their size (PAGE_SIZE), meaning that any valid stack address has PAGE_SHIFT bit as 0. This allows us to conveniently check for overflow in the exception entry without corrupting any GPRs. We won't recover from a stack overflow so panic the hypervisor. Signed-off-by: Kalesh Singh --- Changes in v3: - Remove test_sp_overflow macro, per Mark - Add asmlinkage attribute for hyp_panic, hyp_panic_bad_stack, per Ard arch/arm64/kvm/hyp/nvhe/host.S | 24 ++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/switch.c | 7 ++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/host.S b/arch/arm64/kvm/hyp/nvhe/host.S index 3d613e721a75..749961bfa5ba 100644 --- a/arch/arm64/kvm/hyp/nvhe/host.S +++ b/arch/arm64/kvm/hyp/nvhe/host.S @@ -153,6 +153,18 @@ SYM_FUNC_END(__host_hvc) .macro invalid_host_el2_vect .align 7 + + /* + * Test whether the SP has overflowed, without corrupting a GPR. + * nVHE hypervisor stacks are aligned so that SP & (1 << PAGE_SHIFT) + * should always be zero. + */ + add sp, sp, x0 // sp' = sp + x0 + sub x0, sp, x0 // x0' = sp' - x0 = (sp + x0) - x0 = sp + tbnz x0, #PAGE_SHIFT, .L__hyp_sp_overflow\@ + sub x0, sp, x0 // x0'' = sp' - x0' = (sp + x0) - sp = x0 + sub sp, sp, x0 // sp'' = sp' - x0 = (sp + x0) - x0 = sp + /* If a guest is loaded, panic out of it. */ stp x0, x1, [sp, #-16]! get_loaded_vcpu x0, x1 @@ -165,6 +177,18 @@ SYM_FUNC_END(__host_hvc) * been partially clobbered by __host_enter. */ b hyp_panic + +.L__hyp_sp_overflow\@: + /* + * Reset SP to the top of the stack, to allow handling the hyp_panic. + * This corrupts the stack but is ok, since we won't be attempting + * any unwinding here. + */ + ldr_this_cpu x0, kvm_init_params + NVHE_INIT_STACK_HYP_VA, x1 + mov sp, x0 + + bl hyp_panic_bad_stack + ASM_BUG() .endm .macro invalid_host_el1_vect diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c index 6410d21d8695..703a5d3f611b 100644 --- a/arch/arm64/kvm/hyp/nvhe/switch.c +++ b/arch/arm64/kvm/hyp/nvhe/switch.c @@ -347,7 +347,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu) return exit_code; } -void __noreturn hyp_panic(void) +asmlinkage void __noreturn hyp_panic(void) { u64 spsr = read_sysreg_el2(SYS_SPSR); u64 elr = read_sysreg_el2(SYS_ELR); @@ -369,6 +369,11 @@ void __noreturn hyp_panic(void) unreachable(); } +asmlinkage void __noreturn hyp_panic_bad_stack(void) +{ + hyp_panic(); +} + asmlinkage void kvm_unexpected_el2_exception(void) { return __kvm_unexpected_el2_exception(); -- 2.35.1.574.g5d30c73bfb-goog