From patchwork Thu Aug 1 12:06:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13750398 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE279C3DA64 for ; Thu, 1 Aug 2024 12:59:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 39DA18D0006; Thu, 1 Aug 2024 08:59:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32B3D8D0003; Thu, 1 Aug 2024 08:59:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 178D78D0006; Thu, 1 Aug 2024 08:59:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E90398D0003 for ; Thu, 1 Aug 2024 08:59:25 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B134AA6C82 for ; Thu, 1 Aug 2024 12:59:25 +0000 (UTC) X-FDA: 82403682690.16.3E8CE5D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf13.hostedemail.com (Postfix) with ESMTP id D33FD20022 for ; Thu, 1 Aug 2024 12:59:23 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hd+OXBiY; spf=pass (imf13.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722517106; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4/myyOyuMfX6crVwVKxf4MM/Wn31RwnDs+hGA/iV1EA=; b=Z4j4qZOZQN0ON4k5iJtCREv9ISx2UbegU9T5bcJtcgGrQnqvAUdeFBWbjIl8kFDAbVZTML vAP4hR35M4J9WJKVozzmOQERPkFxVYJA97gP346c3+oHdnIHhLGrCEc30EfiAPC6c6vESS OqFf+glPV1BuT8aPtHdWKQ8eaj5kaNM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722517106; a=rsa-sha256; cv=none; b=eoqaI8uV6eGN9Lt/wn1lN0cwbrLF9+ggamacU8Nj6DJGKbiTzyOTBw1A/m9oQWOOxlrUij Rs4WquobOvNthCmUF7nzUotcijsK6Eyln2HNccDKLx7JRTQdWUFkmAJkCCzkpAcmHMHMwa J3aSuDAHXwE54Ntj9panWU1etIDAh00= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hd+OXBiY; spf=pass (imf13.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 16D7F6288A; Thu, 1 Aug 2024 12:59:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82677C4AF0A; Thu, 1 Aug 2024 12:59:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722517162; bh=mCYEqlOLyPN0qEO0joq8aJDaO3kSWJXCOekmmHiOO54=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hd+OXBiYmYQ7Y//fkA4aEYz+PfLaULR587Lip2lLru+MeE0zfjQPmmTmggZAOSb6W Qr/IsWI1qHO3AyRzzFrYBNju3NZhRMbESgLPxjvupiritZVxcIdSBeZl1MoIipOJ41 qX5O9TlfdXxTXaPIlXxjFiZTknTnqaLW2wAhpigDusoe1aoMYhecrtjrCVxQ47zTtR j930gjTJ2dbrJ0EeIjSPhq3YhBPyS1VyW4+YMIUeCMHzfzxuRcdu7Irekyq+UOT7Gk o3qmQIQ4qW+FdbvIS/arc20i2UI1n+WQCyuBoo8kBohmY7nlHK41NtvjYN2Q1E0mnW UJGRnJzPbvaKw== From: Mark Brown Date: Thu, 01 Aug 2024 13:06:45 +0100 Subject: [PATCH v10 18/40] arm64/mm: Handle GCS data aborts MIME-Version: 1.0 Message-Id: <20240801-arm64-gcs-v10-18-699e2bd2190b@kernel.org> References: <20240801-arm64-gcs-v10-0-699e2bd2190b@kernel.org> In-Reply-To: <20240801-arm64-gcs-v10-0-699e2bd2190b@kernel.org> To: Catalin Marinas , Will Deacon , Jonathan Corbet , Andrew Morton , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Arnd Bergmann , Oleg Nesterov , Eric Biederman , Shuah Khan , "Rick P. Edgecombe" , Deepak Gupta , Ard Biesheuvel , Szabolcs Nagy , Kees Cook Cc: "H.J. Lu" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Florian Weimer , Christian Brauner , Thiago Jung Bauermann , Ross Burton , linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, kvmarm@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Brown X-Mailer: b4 0.15-dev-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=3621; i=broonie@kernel.org; h=from:subject:message-id; bh=mCYEqlOLyPN0qEO0joq8aJDaO3kSWJXCOekmmHiOO54=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmq4YTtyLs+vnIQBWnGfIVH8o2DnS3kjKCvjmb12p9 SZmL+AaJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZquGEwAKCRAk1otyXVSH0AxZB/ wPhepfGMuxiO9PuNnOv6YrfgeBlsRwJvM/VUvi67zpKMpsBaAkgbb/2VWw/grqfKYNmhgW1gumwF6C tyVVQsT+s/EfexLl6BA26CVY7uXXlIp/0kXkFbMUqtlL1VQTL36JJqAqyXzSdCYCkeXmLh+wGD0f9e Kg5RUk1MX0Ch9escuRKahaaq7kI6zY1uH5zMeldSwh1bmAGX7N+kYxbINBnbp5LAqIY+DwUtEEN7C2 tsVyqoltrGCYqmb1W65kAyo5UKWnZ0QQ38AdwXRZiRNFImkRvBkwcUMZSX49aWKWsaAV98CGwC1Fwj 3QmFhr+QSiT5JnnL9eK6KF8bGEqgni X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Stat-Signature: 79eps5dh65zutedhczf3e5tjkewxiozc X-Rspamd-Queue-Id: D33FD20022 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1722517163-993081 X-HE-Meta: U2FsdGVkX19WfSCU1+Mu9d2KByXgUspvrxfwJmxgXKL4h3Kf7sM8891kS8fQqXDFMf9j3yOwo8W5gyROPya27doxNELPLQ12n8QZUZ7Vbz1eDKaysoc6EGpNWamPSmZxyyRW6E4XSu8i/TxBkBZHZBKU+Q++TyCsoVJK8P8Lo7ajGUjUzmC8JYmFeVTayQYLMj7O4psdgcsw/2T6HbUWymRFrbo1d8yp3LAr11SKjhgAWXox6Mo3BXBhOTWEZ3yoPFVOzFjz0h5aKwWr5nKcBiPUizvPlZYE6MBcAx06hLZG3E/gpIQiIzqRF6tZXCerPg+Y1mG3cICyCzYC44BsI3YBBMTJADDj+/Qb2ApezYBukScKVoeCSyb+x21McPcYkPzmgSxA4bXSHE8ckRUHOihE8V5HPml2VRuOvFzbV4RJsPFJLGd00Qf57r3ARHVE0+yJvqDY7UkN0M69NoBQno4SFH47MnVnRBCuDlAUL/g2x8khVKYTYB+nfGOesE4LlB4vL9IltWiT4D9mKFEl+qOF4PUQIaxD24U3+ofvz9AmKjHmTz/aUxXALSSzf/vj6Lh+Fj8xBWw23+sSPD/1tdTGu7a5SafYglXGKwHoVnN4OWGFJf/AtvqwQyNlyoP7+L80Bc9qllEr3EU8JMAiOfl/4RIlfjSQ+yNDTOjhIkUOtA5XTuV1yP+F5HMP+Nh1WLbLZNdOWPfvX90202mDyAnJwlv+lRPavu/klbboFkBbBmFHkOi6IXhEsA53WnFNegUOO/gMaRN6C28FYoNwGJEw1oEd7krg+KyTcedidrq8k2J54miz45SERvo990+7J+oPwZKPODBRqc7RZ1+Fc03IfI+OGd38bY2jz777L6tphcmlcJpaa53KIwB8jPr+5TH7LRnOHwEvC9jctZxoun/0Cc+Xf7BsLi61REEB1KrWuzg6/ABQhZiNsWQkyZ+vdmBJJvoCtlOlPPNe4ya Nl4Ts7U3 uiY2HHbM96TwNymFJ+mHGAmxPIaKmmbZQvejtO8vYBoHAf7+sSIfUGad7QZ1cLn+VzFhjsSWmJ9VcV0zc5FWZ4pOwj6TBjh8fG0KtWbjG/sR+MoiIHUYUhwGgyqx42C/I8s3VO1pOd5g1esB6qudyKxbT/ZOkA9rP7xXX1TgTHOwPSguS71Sr308L0ZPD2HGzfBZpxRNzgEtQ/o8dkmPewxpEpAMH1+fl9Gt9aZryeB2XFAMmWJTwk+QobuN0aHBh7xttlQ5zdn/26EIyKhsLFJ9Geqffp9D7maEN0dvlLc0vLKvCKZmn2H8F2nL/5tOKjQU0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: All GCS operations at EL0 must happen on a page which is marked as having UnprivGCS access, including read operations. If a GCS operation attempts to access a page without this then it will generate a data abort with the GCS bit set in ESR_EL1.ISS2. EL0 may validly generate such faults, for example due to copy on write which will cause the GCS data to be stored in a read only page with no GCS permissions until the actual copy happens. Since UnprivGCS allows both reads and writes to the GCS (though only through GCS operations) we need to ensure that the memory management subsystem handles GCS accesses as writes at all times. Do this by adding FAULT_FLAG_WRITE to any GCS page faults, adding handling to ensure that invalid cases are identfied as such early so the memory management core does not think they will succeed. The core cannot distinguish between VMAs which are generally writeable and VMAs which are only writeable through GCS operations. EL1 may validly write to EL0 GCS for management purposes (eg, while initialising with cap tokens). We also report any GCS faults in VMAs not marked as part of a GCS as access violations, causing a fault to be delivered to userspace if it attempts to do GCS operations outside a GCS. Signed-off-by: Mark Brown --- arch/arm64/mm/fault.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 451ba7cbd5ad..0973dd09f11a 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -486,6 +486,14 @@ static void do_bad_area(unsigned long far, unsigned long esr, } } +static bool is_gcs_fault(unsigned long esr) +{ + if (!esr_is_data_abort(esr)) + return false; + + return ESR_ELx_ISS2(esr) & ESR_ELx_GCS; +} + static bool is_el0_instruction_abort(unsigned long esr) { return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW; @@ -500,6 +508,25 @@ static bool is_write_abort(unsigned long esr) return (esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM); } +static bool is_invalid_gcs_access(struct vm_area_struct *vma, u64 esr) +{ + if (!system_supports_gcs()) + return false; + + if (unlikely(is_gcs_fault(esr))) { + /* GCS accesses must be performed on a GCS page */ + if (!(vma->vm_flags & VM_SHADOW_STACK)) + return true; + if (!(vma->vm_flags & VM_WRITE)) + return true; + } else if (unlikely(vma->vm_flags & VM_SHADOW_STACK)) { + /* Only GCS operations can write to a GCS page */ + return is_write_abort(esr); + } + + return false; +} + static int __kprobes do_page_fault(unsigned long far, unsigned long esr, struct pt_regs *regs) { @@ -535,6 +562,14 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, /* It was exec fault */ vm_flags = VM_EXEC; mm_flags |= FAULT_FLAG_INSTRUCTION; + } else if (is_gcs_fault(esr)) { + /* + * The GCS permission on a page implies both read and + * write so always handle any GCS fault as a write fault, + * we need to trigger CoW even for GCS reads. + */ + vm_flags = VM_WRITE; + mm_flags |= FAULT_FLAG_WRITE; } else if (is_write_abort(esr)) { /* It was write fault */ vm_flags = VM_WRITE; @@ -568,6 +603,13 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, if (!vma) goto lock_mmap; + if (is_invalid_gcs_access(vma, esr)) { + vma_end_read(vma); + fault = 0; + si_code = SEGV_ACCERR; + goto bad_area; + } + if (!(vma->vm_flags & vm_flags)) { vma_end_read(vma); fault = 0;