Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2580860pxj; Mon, 14 Jun 2021 02:07:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzq7O2jS0DsLArW5Jql7dFJpsOLqeszIZ0+udi8zNXoM/urehQ4/AUK54Wd/ndwhMEBjW8R X-Received: by 2002:aa7:c515:: with SMTP id o21mr15593025edq.195.1623661625621; Mon, 14 Jun 2021 02:07:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623661625; cv=none; d=google.com; s=arc-20160816; b=mrbOldcixwv77FgNLNUqmnEzCnMNtrBIJ+bzy1StiOWDU+2urclPKUCtytXAmxpXhK QC7aMfDBGSt8xisRAEbce0XL530d0/WXxrneK8gatLV1r40I6GJlc+U7YCpUhmipCPDm 7ynVY7EZQVe7yS69mmH9Jllk5hmH7eXPT3cIXWo2OOb+66Y7qDfzOJivgQQnXVctZtw2 AzHdAv+t5lHc+RVcPapWCiR+SD90klbx1X16FG7xjsnEMcpvSwn35kn82nEBH3Fhd9yv TppVm7Ao7idUfazJS5ElVyzulQ0bXIFiPBM4ZN4ypWvTuyMn9o7xFjXLqlpHTsq07Rpu lufw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=8snUiDeEvxDRf4ac5iUBOoyV0yF9v4ciI9EcHsbcJgU=; b=LrVDGa4M2dDyAJHTmTjVbccTNaII+y8oK0KmfichHk2r9eW4H/htgWzH1JoWauJZEy Mmav0igSAcW+esdGSBbWNvobFHGQVpx0TYpQTH1wZOPJYDPaNkbr0c15cKFqkV5Yq06g pL1ysVAzkXpcFt9DW+bMoEDLgjo6jGfhIWxNRTojmgmjdHjBiL2Db85fmjz5oLu9GK2N yP5aV53v/sdW9j5ougDvTu622K0DdiqMTUyj0mNP+SGywmIAjsTCzSjnW1/aWDX6rx7a 7UgKqy0hTiVR+/4xA1Hx7gH8RlFxNqLw5m/Ia9J+0U1j5B6hDpYEsEk5IFT6jcUEmY5F 9Aug== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id yh25si10018925ejb.182.2021.06.14.02.06.42; Mon, 14 Jun 2021 02:07:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232670AbhFNJHr (ORCPT + 99 others); Mon, 14 Jun 2021 05:07:47 -0400 Received: from foss.arm.com ([217.140.110.172]:58226 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232666AbhFNJHq (ORCPT ); Mon, 14 Jun 2021 05:07:46 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CA644113E; Mon, 14 Jun 2021 02:05:43 -0700 (PDT) Received: from e112269-lin.arm.com (autoplooker.cambridge.arm.com [10.1.194.57]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0A8A33F694; Mon, 14 Jun 2021 02:05:40 -0700 (PDT) From: Steven Price To: Catalin Marinas , Marc Zyngier , Will Deacon Cc: Steven Price , James Morse , Julien Thierry , Suzuki K Poulose , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Dave Martin , Mark Rutland , Thomas Gleixner , qemu-devel@nongnu.org, Juan Quintela , "Dr. David Alan Gilbert" , Richard Henderson , Peter Maydell , Haibo Xu , Andrew Jones Subject: [PATCH v15 1/7] arm64: mte: Handle race when synchronising tags Date: Mon, 14 Jun 2021 10:05:19 +0100 Message-Id: <20210614090525.4338-2-steven.price@arm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210614090525.4338-1-steven.price@arm.com> References: <20210614090525.4338-1-steven.price@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org mte_sync_tags() used test_and_set_bit() to set the PG_mte_tagged flag before restoring/zeroing the MTE tags. However if another thread were to race and attempt to sync the tags on the same page before the first thread had completed restoring/zeroing then it would see the flag is already set and continue without waiting. This would potentially expose the previous contents of the tags to user space, and cause any updates that user space makes before the restoring/zeroing has completed to potentially be lost. Since this code is run from atomic contexts we can't just lock the page during the process. Instead implement a new (global) spinlock to protect the mte_sync_page_tags() function. Fixes: 34bfeea4a9e9 ("arm64: mte: Clear the tags when a page is mapped in user-space with PROT_MTE") Reviewed-by: Catalin Marinas Signed-off-by: Steven Price --- arch/arm64/kernel/mte.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index 125a10e413e9..a3583a7fd400 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -25,6 +25,7 @@ u64 gcr_kernel_excl __ro_after_init; static bool report_fault_once = true; +static DEFINE_SPINLOCK(tag_sync_lock); #ifdef CONFIG_KASAN_HW_TAGS /* Whether the MTE asynchronous mode is enabled. */ @@ -34,13 +35,22 @@ EXPORT_SYMBOL_GPL(mte_async_mode); static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap) { + unsigned long flags; pte_t old_pte = READ_ONCE(*ptep); + spin_lock_irqsave(&tag_sync_lock, flags); + + /* Recheck with the lock held */ + if (test_bit(PG_mte_tagged, &page->flags)) + goto out; + if (check_swap && is_swap_pte(old_pte)) { swp_entry_t entry = pte_to_swp_entry(old_pte); - if (!non_swap_entry(entry) && mte_restore_tags(entry, page)) - return; + if (!non_swap_entry(entry) && mte_restore_tags(entry, page)) { + set_bit(PG_mte_tagged, &page->flags); + goto out; + } } page_kasan_tag_reset(page); @@ -53,6 +63,10 @@ static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap) */ smp_wmb(); mte_clear_page_tags(page_address(page)); + set_bit(PG_mte_tagged, &page->flags); + +out: + spin_unlock_irqrestore(&tag_sync_lock, flags); } void mte_sync_tags(pte_t *ptep, pte_t pte) @@ -63,7 +77,7 @@ void mte_sync_tags(pte_t *ptep, pte_t pte) /* if PG_mte_tagged is set, tags have already been initialised */ for (i = 0; i < nr_pages; i++, page++) { - if (!test_and_set_bit(PG_mte_tagged, &page->flags)) + if (!test_bit(PG_mte_tagged, &page->flags)) mte_sync_page_tags(page, ptep, check_swap); } } -- 2.20.1