From patchwork Tue Feb 22 16:51:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12755727 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 DF54AC433F5 for ; Tue, 22 Feb 2022 17:14:38 +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:From:Subject:References:Mime-Version :Message-Id:In-Reply-To:Date:Reply-To:To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=xB6h7oY2EjMVKY3m8SKr/aM+ipmPicLZJ1qy0I4CghY=; b=cRp4cMvWVS3HhL pvvmbFi9z21Qylz3fMM9s7O12PE6FH6aq12fzFeQfVKgrXQ3eJjKGYJ4Zhr5CYKLYxivbDLvjfM2/ xyNXsM1QakJumUciVhrxPEPpJw2VnVMtmggj0svy6bUFLgo3RDJndZaF6Z9tH5FA+fQZfiOCsRU0n NtObBEw6R/2vCVpwVrf52DxvZCE9LIfBbKXh3XseofZBdNa37EuHoFtoQZpdpnLx5bdOF3itPQ8Dq g78c/OcF/g7tAhVEkvJqmtlHI3J/NqSYLAcKJj/TO+MxlDrw1h4MVLSKUi9e0h4M/fKJiIlyoCnij Zw6ahuDvbsmlZeYLXO9g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nMYif-00AteK-PI; Tue, 22 Feb 2022 17:12:58 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nMYVh-00Aoad-8n for linux-arm-kernel@lists.infradead.org; Tue, 22 Feb 2022 16:59:34 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2d61b4ef6cdso146069417b3.11 for ; Tue, 22 Feb 2022 08:59:32 -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=kA1uHTXTZgvCDEwUv0Venj02VbjFVl5MyBDxltrhOyc=; b=NP6fnietNid07WODom2crvgVApuYNwyBc+vaQeT9ELb0Wby0y027V1WoutortBDdER 6VKH2m5g/hhqE8jP+GV8Z52SXKndZFpGpqKGnvqTJ6saOgO9yiXq/Sl8KOE48ShXSFrf Yd/GQs7iTARZWcbcAbs2DZcziHvlIf4ws8th2T86PRoxGLRCAo79SHT5a0UzBseydVww c/kdTBUTDmKW4WhBmbRdCfs4Sm1ZyK65IyLMvKPqQFMBa2uiSRCXXe8ja7vMgkYdHVOd FDQ7AFIgQCuxZCe7VWKGtZS0vLPpQ4mLhJFEH4IE0gr/JO65ZVTSGIb2gzhuMTjBEe1w 9t+A== 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=kA1uHTXTZgvCDEwUv0Venj02VbjFVl5MyBDxltrhOyc=; b=r+zmFUJ4w2JAf/P26F8cMb7YnkHQhIdmGHi9LgRPnoJF6x7tVFPeItYKDAmOBdFJn4 Di8X0V3PfyxtzdNDbSJs7DnPacTccYDyut6lbQ91Z0nxMJaEOyfjgdIhj/HKii0i8/Lu Pe19Zcxjqulj59e/TYNySphWWDtOMVnVqz7VMkzB2w8p066xUukB4kiuEu9e0d2vL187 7SiFopenyVcadycUmkeEVy+T/HZxGoneFtEEyh7OG6rdGO1i5dP0mbTHEb5CpD0Mymjg ffpUSvj80mG5Th1QLQp72XD6p3Uua/R/IQY1fbwUQMCtymLBsfvYbRS/x6JRCd9Gac+L j8rA== X-Gm-Message-State: AOAM530hl/gI8abbMBRWNOGek6GTMXUNppZKZ11AA/7sOGuNGzchYALa Q/lzzNYSlL3y5UEuaDbx4fdpsh/205AP7vQjyA== X-Google-Smtp-Source: ABdhPJw2THd1z2iAbfih7pFjkHmBG5AlQKbmEqgBiWlGmwrM/dGUOKWBoivdDJ4o6t47SOdmT8K0alB2LRMYcbGgbA== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:5db7:1235:b3dd:cfcb]) (user=kaleshsingh job=sendgmr) by 2002:a81:138a:0:b0:2d6:b391:90cd with SMTP id 132-20020a81138a000000b002d6b39190cdmr23404107ywt.480.1645549171601; Tue, 22 Feb 2022 08:59:31 -0800 (PST) Date: Tue, 22 Feb 2022 08:51:07 -0800 In-Reply-To: <20220222165212.2005066-1-kaleshsingh@google.com> Message-Id: <20220222165212.2005066-7-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220222165212.2005066-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.35.1.473.g83b2b277ed-goog Subject: [PATCH v2 6/9] 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 , Catalin Marinas , James Morse , Alexandru Elisei , Suzuki K Poulose , Ard Biesheuvel , Mark Rutland , Pasha Tatashin , Joey Gouly , Peter Collingbourne , Andrew Scull , Paolo Bonzini , Zenghui Yu , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.cs.columbia.edu X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220222_085933_356564_71ABC080 X-CRM114-Status: GOOD ( 11.91 ) 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 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 Reported-by: kernel test robot Reported-by: kernel test robot --- arch/arm64/kvm/hyp/nvhe/host.S | 16 ++++++++++++++++ arch/arm64/kvm/hyp/nvhe/switch.c | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/arch/arm64/kvm/hyp/nvhe/host.S b/arch/arm64/kvm/hyp/nvhe/host.S index 3d613e721a75..78e4b612ac06 100644 --- a/arch/arm64/kvm/hyp/nvhe/host.S +++ b/arch/arm64/kvm/hyp/nvhe/host.S @@ -153,6 +153,10 @@ SYM_FUNC_END(__host_hvc) .macro invalid_host_el2_vect .align 7 + + /* Test stack overflow without corrupting GPRs */ + test_sp_overflow PAGE_SHIFT, .L__hyp_sp_overflow\@ + /* If a guest is loaded, panic out of it. */ stp x0, x1, [sp, #-16]! get_loaded_vcpu x0, x1 @@ -165,6 +169,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..5a2e1ab79913 100644 --- a/arch/arm64/kvm/hyp/nvhe/switch.c +++ b/arch/arm64/kvm/hyp/nvhe/switch.c @@ -369,6 +369,11 @@ void __noreturn hyp_panic(void) unreachable(); } +void __noreturn hyp_panic_bad_stack(void) +{ + hyp_panic(); +} + asmlinkage void kvm_unexpected_el2_exception(void) { return __kvm_unexpected_el2_exception();