From patchwork Thu Feb 10 22:41:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12742498 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 97E1AC433F5 for ; Thu, 10 Feb 2022 22:46:56 +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=SkD1TTx6x3fuUPM8RbEx0YoamUf45LRUkzaHXUrN4vM=; b=sRc2/wFYjXL2Jw qhpPfX5uZUB0cj2k1OieSwiyTq7uYcmUDz/YeGrm2Qw+/h8VHvLYhemauFiGa3+2pUaDKd9HFUpKO fZTE7pJ7z9Yxvn7EddKJaLelBrRlf3S+oQvwbssf/Z2vSKJQibV1JnoKozAnrIFJRs2d/7L0yB3C9 PofGP9u7vrwx0OYE1yU1Q1zmlBNI9TGk/nzmQbAjbE83/vNd9vTnj0dupQKy8arUTfURh5a0j+BLN 0bK78EoXKh7IVyfB0bATGUEcthOeRMOPAKU9sLZUb0BSsuykRgAy2DjuGYQMLr20Is63VEdInX2mX UYoAuiNyt36DtB8f3c6Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nIIBv-0054IH-3L; Thu, 10 Feb 2022 22:45:31 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nIIBr-0054GJ-2k for linux-arm-kernel@lists.infradead.org; Thu, 10 Feb 2022 22:45:28 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id c5-20020a25f305000000b0061dd6123f18so15046250ybs.17 for ; Thu, 10 Feb 2022 14:45:25 -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=OagLRmMmzyQmGqXHU8QCfKrH7+tgm0iC+teioaMlxY8=; b=KZUDFgua6kTwrdY0rgWjsvVP3R/Era680jCgYQwjuK0iUjxXTEGhr1k5AU4ecCZI8w lGlhfM50n/PyOfFuEnRyrza/dCfR09l2qnRTramuth4IQ5kSJWx8IHEXrR6WGw+YWDLS Fcw37pMtSTu7lmTbNbpjWo/86asLiZ0qeY3n5xM9TuAmWeZiYQybxooNBeFcYXyFXw0+ nw3MMrt2SnLYQkB9BXZaWEk/9+7BpUqlQ9/jkoMabBFdTAqgmPJRbDiXXuHwXpiKMGkR ABy9ETz+4Cv/q3YuBrAzVEL6xo4Z9nyp3TJ9LkgLV7VtKKpMguCHsIaLgm9iv9xO8mkw WU8Q== 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=OagLRmMmzyQmGqXHU8QCfKrH7+tgm0iC+teioaMlxY8=; b=mFYXgfhbfNAoCP5WKTzjnMxDDhUPfoDfMnFGpJSstiAoylAUeRm3FHK/DbjDioo8sc CGMlEeKt3wMoWgre5dqzSK1+vMPi+w7o5z69DITR1erFsgFCVVmx3wF9lyitK4/hha7z InwzmhUdS9Ny2R7B7pUW18gjSvNRD3julLCljme33DGWGaYXNVw+Pju6TN1l+XTMpIxp CkRg/61DOWMbVy2KkXItdlVjnSF/VbvJCq0ili3etZUylRrjMTKw5aI4B4YsIuYQuPZI 1thzpS3MUo9suTyC4mg5SB5YLbY2rJ80Ix7s8EK7MxqVZRBmdEgr0CTLLUbr46VYjbXK +nYw== X-Gm-Message-State: AOAM531lFKfqy8/ZQyf1FpOIHcBpGkujxY7cqDe5Bhuk5gcqdppx/8X4 tm9KMPhPSMf7BG/PGbWPQvqWQRbhMWX1ZEZ07Q== X-Google-Smtp-Source: ABdhPJxzsMWs7cPxHcJVEK0MMmzcLZ3BX26xgEqJ+cjkAM67DycOE9xaVXyG4ooDAIXoCBwYiczs1AVvH+kAit3i8g== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:8f02:232:ad86:2ab2]) (user=kaleshsingh job=sendgmr) by 2002:a5b:804:: with SMTP id x4mr9503248ybp.673.1644533125008; Thu, 10 Feb 2022 14:45:25 -0800 (PST) Date: Thu, 10 Feb 2022 14:41:45 -0800 In-Reply-To: <20220210224220.4076151-1-kaleshsingh@google.com> Message-Id: <20220210224220.4076151-5-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220210224220.4076151-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.35.1.265.g69c8d7142f-goog Subject: [PATCH 4/7] KVM: arm64: Allocate guard pages near hyp stacks 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 , 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-20220210_144527_155162_D2B0D259 X-CRM114-Status: GOOD ( 15.51 ) 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 From: Quentin Perret Allocate unbacked VA space underneath each stack page to ensure stack overflows get trapped and don't corrupt memory silently. The stack is aligned to twice its size (PAGE_SIZE), meaning that any valid stack address has PAGE_SHIFT bit as 0. This allows us to easily check for overflow in the exception entry without corrupting any GPRs. Signed-off-by: Quentin Perret [ Kalesh - Update commit text and comments, refactor, add overflow handling ] Signed-off-by: Kalesh Singh --- arch/arm64/kvm/hyp/nvhe/host.S | 16 ++++++++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 19 ++++++++++++++++++- arch/arm64/kvm/hyp/nvhe/switch.c | 5 +++++ 3 files changed, 39 insertions(+), 1 deletion(-) 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/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c index 99e178cf4249..114053dff228 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -105,7 +105,24 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, if (ret) return ret; - /* Map stack pages in the 'private' VA range */ + /* + * Allocate 'private' VA range for stack guard pages. + * + * The 'private' VA range grows upward and stacks downwards, so + * allocate the guard page first. But make sure to align the + * stack itself with PAGE_SIZE * 2 granularity to ease overflow + * detection in the entry assembly code. + */ + do { + start = (void *)hyp_alloc_private_va_range(PAGE_SIZE); + if (IS_ERR(start)) + return PTR_ERR(start); + } while (IS_ALIGNED((u64) start, PAGE_SIZE * 2)); + + /* + * Map stack pages in the 'private' VA range above the allocated + * guard pages. + */ end = (void *)__hyp_pa(per_cpu_ptr(&kvm_init_params, i)->stack_hyp_va); start = end - PAGE_SIZE; start = (void *)__pkvm_create_private_mapping((phys_addr_t)start, 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();