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 X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7FA8C48BE6 for ; Mon, 14 Jun 2021 13:10:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8351861283 for ; Mon, 14 Jun 2021 13:10:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8351861283 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 10DEC6B006C; Mon, 14 Jun 2021 09:10:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BE0E6B006E; Mon, 14 Jun 2021 09:10:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E51306B0070; Mon, 14 Jun 2021 09:10:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0142.hostedemail.com [216.40.44.142]) by kanga.kvack.org (Postfix) with ESMTP id ADBDF6B006C for ; Mon, 14 Jun 2021 09:10:41 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 4A300640E for ; Mon, 14 Jun 2021 13:10:41 +0000 (UTC) X-FDA: 78252363882.25.FBA7609 Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) by imf07.hostedemail.com (Postfix) with ESMTP id 10D10A000278 for ; Mon, 14 Jun 2021 13:10:31 +0000 (UTC) Received: by mail-lf1-f46.google.com with SMTP id k40so21048596lfv.7 for ; Mon, 14 Jun 2021 06:10:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=/AteiqldFMFOlpZ79iKSrJdb9WM23atrdo76u3CtXI0=; b=FnIic0hbgy06y+4HvKKGSOvyWaPQrYD35pFM/GZos/rN/Lcvxwixs/PuimxKwX06/S l6O4pihxZsTOmjP310zNezN9EfU7dnv95KvZX6kDvNz9Myr4OixJwc/s2+fU6BSYqxSo OD0MqV6BUR3mKwKSaXy9O3Z9YDamvekqCLaxgXDvu3GaSHyxIThLOAsJam8pmw3mQkOZ Etj9FKo8CWi8fnzaaUKYfFCpnj0uFzz/WxGKHp/qA/2ZUzuaoouVr4ws63PADIWze9FF KgiSHIP3uiTSPIKZMgAY9ZmLCLGxOmKAX/S/tRsjMThQJomfcXKcvSQwjChbGSN6diTw 1UoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=/AteiqldFMFOlpZ79iKSrJdb9WM23atrdo76u3CtXI0=; b=Cm7iYD0WOsTIpGSnHmcky5l5yu1Kyi0XKqpLyVz2J4xyiDlyX7VUiHBJsGStbbmtKh OrlGAsTT2N6lI/ikwE3RoT+G7D62hg8De419YMN+UZpfhrK4+k4cs3K7qPfx43BvRbe9 2l76LeRPlrjMKzzUYGkzqjTkIuO5I31OR+Jj/IsQDpBUX2/8MBd4oOA8chqX6pMKfby4 PlOC6alAcPwNIpQO+Nbiq6jGPpGNGGODrY+ZvhtDOd5sL7IvR/vw9UWxgf7A2bh9RxQA myN06y5D/f+MzmIXWhuRdtsv7zZ/Otag6rX7ST+BWm8UvkcD+itK/NHJVQLgDudeE56n LMzg== X-Gm-Message-State: AOAM532tytBn6C3s8RcHuu7a5Yu4IPqdLPRBxTj4mV8N5r1wxEq4AnFS 6hwsmFOpv95Uiuh7q5O67Ck5YA== X-Google-Smtp-Source: ABdhPJw/sDHQHR4Uy1E7hq8O/pknucYcEdwsugy0iyc2N7DnWfH9PwZY0WvwzTbxFxv9m3RQiSz30g== X-Received: by 2002:a05:6512:1290:: with SMTP id u16mr4641610lfs.144.1623676239063; Mon, 14 Jun 2021 06:10:39 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id d15sm1086495lfl.199.2021.06.14.06.10.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 06:10:38 -0700 (PDT) Received: by box.localdomain (Postfix, from userid 1000) id C702E101ABE; Mon, 14 Jun 2021 16:10:56 +0300 (+03) Date: Mon, 14 Jun 2021 16:10:56 +0300 From: "Kirill A. Shutemov" To: Jann Horn Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Matthew Wilcox , John Hubbard , Jan Kara , stable@vger.kernel.org Subject: Re: [PATCH resend] mm/gup: fix try_grab_compound_head() race with split_huge_page() Message-ID: <20210614131056.lo4ojw7lvfecwits@box.shutemov.name> References: <20210611161545.998858-1-jannh@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210611161545.998858-1-jannh@google.com> Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=FnIic0hb; spf=none (imf07.hostedemail.com: domain of kirill@shutemov.name has no SPF policy when checking 209.85.167.46) smtp.mailfrom=kirill@shutemov.name; dmarc=none X-Stat-Signature: ip3yx6uauo4pxpk5zto73xphjne5gbtj X-Rspamd-Queue-Id: 10D10A000278 X-Rspamd-Server: rspam06 X-HE-Tag: 1623676231-957978 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 Fri, Jun 11, 2021 at 06:15:45PM +0200, Jann Horn wrote: > try_grab_compound_head() is used to grab a reference to a page from > get_user_pages_fast(), which is only protected against concurrent > freeing of page tables (via local_irq_save()), but not against > concurrent TLB flushes, freeing of data pages, or splitting of compound > pages. > > Because no reference is held to the page when try_grab_compound_head() > is called, the page may have been freed and reallocated by the time its > refcount has been elevated; therefore, once we're holding a stable > reference to the page, the caller re-checks whether the PTE still points > to the same page (with the same access rights). > > The problem is that try_grab_compound_head() has to grab a reference on > the head page; but between the time we look up what the head page is and > the time we actually grab a reference on the head page, the compound > page may have been split up (either explicitly through split_huge_page() > or by freeing the compound page to the buddy allocator and then > allocating its individual order-0 pages). > If that happens, get_user_pages_fast() may end up returning the right > page but lifting the refcount on a now-unrelated page, leading to > use-after-free of pages. > > To fix it: > Re-check whether the pages still belong together after lifting the > refcount on the head page. > Move anything else that checks compound_head(page) below the refcount > increment. > > This can't actually happen on bare-metal x86 (because there, disabling > IRQs locks out remote TLB flushes), but it can happen on virtualized x86 > (e.g. under KVM) and probably also on arm64. The race window is pretty > narrow, and constantly allocating and shattering hugepages isn't exactly > fast; for now I've only managed to reproduce this in an x86 KVM guest with > an artificially widened timing window (by adding a loop that repeatedly > calls `inl(0x3f8 + 5)` in `try_get_compound_head()` to force VM exits, > so that PV TLB flushes are used instead of IPIs). > > Cc: Matthew Wilcox > Cc: Kirill A. Shutemov > Cc: John Hubbard > Cc: Jan Kara > Cc: stable@vger.kernel.org > Fixes: 7aef4172c795 ("mm: handle PTE-mapped tail pages in gerneric fast gup implementaiton") > Signed-off-by: Jann Horn Looks good to me: Acked-by: Kirill A. Shutemov -- Kirill A. Shutemov