From mboxrd@z Thu Jan 1 00:00:00 1970 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 13A07C77B75 for ; Fri, 21 Apr 2023 12:24:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6127F6B0071; Fri, 21 Apr 2023 08:24:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C2BA6B0072; Fri, 21 Apr 2023 08:24:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B1346B0074; Fri, 21 Apr 2023 08:24:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3DDF96B0071 for ; Fri, 21 Apr 2023 08:24:14 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0A76380320 for ; Fri, 21 Apr 2023 12:24:14 +0000 (UTC) X-FDA: 80705315628.22.D26C660 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf24.hostedemail.com (Postfix) with ESMTP id 65680180010 for ; Fri, 21 Apr 2023 12:24:12 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; spf=pass (imf24.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682079852; 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: in-reply-to:in-reply-to:references:references; bh=fB6TT068dSAv1YZvlFK/YP2ZzpZfRelP40a4NOIfQHc=; b=dt/kvQTTHW7Wgs8+PzjW6+vPSIAKvqrktIeMO6GZ4iJZpM6E4p+5IL+7PydwXNG0cbXLbS JRHJ0HpLq7Tw1ib4UErfP5j+hYdmn4/kjkqpn48CbIhWKH8bMJqeMbfEgQ1zGr1sVgWKAR U6Xv5SgfdU8Co2R6ugoFwzo5NVO43nY= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; spf=pass (imf24.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none) ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682079852; a=rsa-sha256; cv=none; b=Sd53YL1Y2+HqxE6uzxWHA2W6wjsgsTyX25trDcK0iZhu+mwgG5R11ErsmYtjKWfOszWai6 cdD3Abm3DnpMbzsUoQUIio6Z5brWDtBJwrdoMU6dcClAnCBzwjIy1R6Sovz2xzPBWvLWp5 Sk3E6UOJWzTITNolA6W+C6Z2z3OMMPw= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 264F865043; Fri, 21 Apr 2023 12:24:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E292C433EF; Fri, 21 Apr 2023 12:24:08 +0000 (UTC) Date: Fri, 21 Apr 2023 13:24:05 +0100 From: Catalin Marinas To: Peter Collingbourne Cc: andreyknvl@gmail.com, Qun-wei Lin =?utf-8?B?KOael+e+pOW0tCk=?= , Guangye Yang =?utf-8?B?KOadqOWFieS4mik=?= , linux-mm@kvack.org, Chinwen Chang =?utf-8?B?KOW8temMpuaWhyk=?= , kasan-dev@googlegroups.com, ryabinin.a.a@gmail.com, linux-arm-kernel@lists.infradead.org, vincenzo.frascino@arm.com, will@kernel.org, eugenis@google.com, stable@vger.kernel.org Subject: Re: [PATCH] arm64: Also reset KASAN tag if page is not PG_mte_tagged Message-ID: References: <20230420210945.2313627-1-pcc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230420210945.2313627-1-pcc@google.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: y68ocmubsmeejkdrb8r1yrqa1n85tpc4 X-Rspamd-Queue-Id: 65680180010 X-HE-Tag: 1682079852-891838 X-HE-Meta: U2FsdGVkX19uVEtr63Mr8tr6LeX/IK8y9o8nrmXH8LNsh1v8vzQ0LMJPoA8VG7+JT9y5MEYJQoZ/j11hnLt4fkndIbOFkBeyW0rWSxKqyfqO8Somfo/oIUgw/wJXZD/99HlepECVCad/RzisZsfzZ4A8usf9bU6d8+wZW1G4b5ZqAXFNAnMp6TKD2XhQh9Vr2MVg2mUcUJ0pxtkiGaPqrzBiTksQUu3lp3CJnth07K4hPRzYkdg4jugNn0d1Ova7GbRIM/hue+tXjG/4ajLvXMSJeV7eiLGgihcJCt+g695M5QJphLkTsycl3UEv/Hg2Q/gh8hqXwPYwycmMkVRfHeFT0WGZsQkeBv9YfYJ2F+fKCmhaqQ5Ejb4RG9kMtSL+eMvWCTjSrL/6dtVRJax0h4I6pk6xNW+4mMNuHcxuhzdA3EN12g1NzN7XrM0hQEZsVGpJXsgmeaaj/VK1hwSIcdbgNY7+x08Foi0QEEIrgA4DrAh02bbKlwTGWVsPFbxeg9LEhW11N9bnWGMuCYLGedKi3SVf9HfNY3W0VuN6s4rF0VqpVUcpguTYlxaQxh1llUbXjBYeDo88XQg0hVqYw7iyrFnN2eNTAtJFqnTYsPwD45IZg2l/E8qLoZ5z/lIZrhAyrUMCyyKB2gsD4UnMiyziYUvgAK5ZvTO9jFhSvMY4rM17dsM/z5ek0tz0xVo7XLpU5xMZ1270xpuo+WhirjjDhmWtxUM4X6Ba+ct2Aw3mr6oXTMPOOUPJxXdT07Bx43A8UIbM+dDqjmEfYBbuZuZrDaN9aOrsEWtdq5y0t8OPOk9Ti7h6wMi5/f1QUSe0W9VlGSUFml6VBP4CDPHI3x/B9BXm0TzsaehB8IUpNfvTNa3b7jhdKylODauGBPaRIl0fCUnNpoJGXGWj73G992vf+XehupuwlKGjFetjwzgNNgkKJG53UiKBNyr85AAenj3/c69Zgu16E4yucCM QRbuoUgV QUqce3/SnLb1mXWUYHJjSSfZvflv4yYJsC5dkvNVCrNFHuCTTHfj8C4teESOzOoY+efAhcU+lpnVFxwQCYqohS39kiLLG2iLhlSJZe4aZkBYu7cIxXeomnaFfsCgR/cuX+IE3G5b4LJQaRZfsmWCRiRpSnGBA3Yzo21TjkqqvDdpdMhC7Zuj7xZt8Ric/2ARibNCKJWPOlLhiY55bNvN+KhDIc5XWG9f29KhS0NAcWF+kWU5K2f2E84lrh+YRM4IxBgcQQ8GZXjBE4sTr0KidAdHtutIxpwLGaORlplQclPhIUwKiTXDiYdQwq/QhW399MXrI6IMvgsKt3iWvyM+vCQuYDeBxn1hegd8pxO6g5oIFg1rN6UbLMQ8EI5IATxZzRhaNH0MjHaKuCfddPn7QhHjgcCYCBbFDeOLO1rXC3B4ah1ZLAu9LgFmQRVNmwPxN0TTUPY4PchyUlaywvgAESPKslPB3kG2O1Ziw9YEazd7KTCInHaYzC9ps3kPp08Q6NLC0eMyvE7azQ/m4BSxeuz+qLXXjKlbnXC0U 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: On Thu, Apr 20, 2023 at 02:09:45PM -0700, Peter Collingbourne wrote: > Consider the following sequence of events: > > 1) A page in a PROT_READ|PROT_WRITE VMA is faulted. > 2) Page migration allocates a page with the KASAN allocator, > causing it to receive a non-match-all tag, and uses it > to replace the page faulted in 1. > 3) The program uses mprotect() to enable PROT_MTE on the page faulted in 1. Ah, so there is no race here, it's simply because the page allocation for migration has a non-match-all kasan tag in page->flags. How do we handle the non-migration case with mprotect()? IIRC post_alloc_hook() always resets the page->flags since GFP_HIGHUSER_MOVABLE has the __GFP_SKIP_KASAN_UNPOISON flag. > As a result of step 3, we are left with a non-match-all tag for a page > with tags accessible to userspace, which can lead to the same kind of > tag check faults that commit e74a68468062 ("arm64: Reset KASAN tag in > copy_highpage with HW tags only") intended to fix. > > The general invariant that we have for pages in a VMA with VM_MTE_ALLOWED > is that they cannot have a non-match-all tag. As a result of step 2, the > invariant is broken. This means that the fix in the referenced commit > was incomplete and we also need to reset the tag for pages without > PG_mte_tagged. > > Fixes: e5b8d9218951 ("arm64: mte: reset the page tag in page->flags") This commit was reverted in 20794545c146 (arm64: kasan: Revert "arm64: mte: reset the page tag in page->flags"). It looks a bit strange to fix it up. > diff --git a/arch/arm64/mm/copypage.c b/arch/arm64/mm/copypage.c > index 4aadcfb01754..a7bb20055ce0 100644 > --- a/arch/arm64/mm/copypage.c > +++ b/arch/arm64/mm/copypage.c > @@ -21,9 +21,10 @@ void copy_highpage(struct page *to, struct page *from) > > copy_page(kto, kfrom); > > + if (kasan_hw_tags_enabled()) > + page_kasan_tag_reset(to); > + > if (system_supports_mte() && page_mte_tagged(from)) { > - if (kasan_hw_tags_enabled()) > - page_kasan_tag_reset(to); This should work but can we not do this at allocation time like we do for the source page and remove any page_kasan_tag_reset() here altogether? -- Catalin