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=-11.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FSL_HELO_FAKE, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=no 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 64EE4C4361B for ; Sun, 20 Dec 2020 06:06:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2FCCA233CE for ; Sun, 20 Dec 2020 06:06:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727004AbgLTGGO (ORCPT ); Sun, 20 Dec 2020 01:06:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726113AbgLTGGN (ORCPT ); Sun, 20 Dec 2020 01:06:13 -0500 Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A0AFC0613CF for ; Sat, 19 Dec 2020 22:05:32 -0800 (PST) Received: by mail-il1-x12c.google.com with SMTP id r17so6001360ilo.11 for ; Sat, 19 Dec 2020 22:05:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=3PPLg+PyRHcRYpZXYU0/asVVIWhX7CVrUOdST+JsHR0=; b=e9wxcQKAFmPoEefwuZbY4XDYd4tBIR1mNJrmj/KVWvI/UZUztU7+L4iHz+Ve9enOI+ E2lrFuoODyJXy71rDvSN4jQw1/jWVkaHsoHX8wOHb2M0vig7rxn0n3iT9Jr5ij4N+5Op FUID7+AbzTYOVJ1KMLZsFkggc51AVCic7CrJd31xeO34IKxcmQQD7SjEAOT2tSv3WueV jrWSVojFo//oZQnc8LOGMxJxPVxHA/Sp9paANaQEYBlp2RZkxJ0I38bCB8jI1OG+VxtS Zwddmly0CxNmChMl8rVCN5c2w1PqfAOk35/xeloRR92oDb42Hcy1egNuuW3yeIWjVB6G qFvQ== 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:content-transfer-encoding :in-reply-to; bh=3PPLg+PyRHcRYpZXYU0/asVVIWhX7CVrUOdST+JsHR0=; b=lWWyazch5j4RcrDDJ8wbp/i3npPmusG8Y8Lpi1iWMMaEexbco1Br8fZ1OF5+WoTyvI F5QMV03E80VQTvNKAg+rpwzUXqVIboCn9pgol7cBZl9EmGUhNuSjzTPApWxKhzp/sCeX jkntk54SypW7RcGv/R3236oiLxI8ndNPf64vQcPQwDKJ489xtXXskUeunKxuXw9Aknfm DcRQHFucrsMAbmeKisoBtooCwjT0XImt732t5/djmSe7mTmw3Aion/ozNGjzqhMTBwiY RVQk9AkdSyfwEMDf58/kdTsezpN7bimcNcMgeW9H5wx5IjW3gqz57fncrDoWeTCuYpsn AwSA== X-Gm-Message-State: AOAM532ZbkR5NysCbwbOTTwgPaaPZNAuk3PQfvTMsVqXVY0j4S5YGCNi kHgZmboqQOkly176ik16/Clvfg== X-Google-Smtp-Source: ABdhPJyy9yVMnwfNic7VftM2KcUqOrVxMDy8+CIcb0HITMOOPzzKatb6Pr6CA9cJOtnPTOc4U8HISA== X-Received: by 2002:a92:d7d2:: with SMTP id g18mr11631723ilq.2.1608444331660; Sat, 19 Dec 2020 22:05:31 -0800 (PST) Received: from google.com ([2620:15c:183:200:7220:84ff:fe09:2d90]) by smtp.gmail.com with ESMTPSA id f13sm12714253iog.18.2020.12.19.22.05.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Dec 2020 22:05:30 -0800 (PST) Date: Sat, 19 Dec 2020 23:05:26 -0700 From: Yu Zhao To: Nadav Amit Cc: Andrea Arcangeli , linux-mm , Peter Xu , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable@vger.kernel.org, minchan@kernel.org, Andy Lutomirski , Will Deacon , Peter Zijlstra Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect Message-ID: References: <20201219043006.2206347-1-namit@vmware.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Dec 19, 2020 at 01:34:29PM -0800, Nadav Amit wrote: > [ cc’ing some more people who have experience with similar problems ] > > > On Dec 19, 2020, at 11:15 AM, Andrea Arcangeli wrote: > > > > Hello, > > > > On Fri, Dec 18, 2020 at 08:30:06PM -0800, Nadav Amit wrote: > >> Analyzing this problem indicates that there is a real bug since > >> mmap_lock is only taken for read in mwriteprotect_range(). This might > > > > Never having to take the mmap_sem for writing, and in turn never > > blocking, in order to modify the pagetables is quite an important > > feature in uffd that justifies uffd instead of mprotect. It's not the > > most important reason to use uffd, but it'd be nice if that guarantee > > would remain also for the UFFDIO_WRITEPROTECT API, not only for the > > other pgtable manipulations. > > > >> Consider the following scenario with 3 CPUs (cpu2 is not shown): > >> > >> cpu0 cpu1 > >> ---- ---- > >> userfaultfd_writeprotect() > >> [ write-protecting ] > >> mwriteprotect_range() > >> mmap_read_lock() > >> change_protection() > >> change_protection_range() > >> ... > >> change_pte_range() > >> [ defer TLB flushes] > >> userfaultfd_writeprotect() > >> mmap_read_lock() > >> change_protection() > >> [ write-unprotect ] > >> ... > >> [ unprotect PTE logically ] > >> ... > >> [ page-fault] > >> ... > >> wp_page_copy() > >> [ set new writable page in PTE] I don't see any problem in this example -- wp_page_copy() calls ptep_clear_flush_notify(), which should take care of the stale entry left by cpu0. That being said, I suspect the memory corruption you observed is related this example, with cpu1 running something else that flushes conditionally depending on pte_write(). Do you know which type of pages were corrupted? file, anon, etc. > > Can't we check mm_tlb_flush_pending(vma->vm_mm) if MM_CP_UFFD_WP_ALL > > is set and do an explicit (potentially spurious) tlb flush before > > write-unprotect? > > There is a concrete scenario that I actually encountered and then there is a > general problem. > > In general, the kernel code assumes that PTEs that are read from the > page-tables are coherent across all the TLBs, excluding permission promotion > (i.e., the PTE may have higher permissions in the page-tables than those > that are cached in the TLBs). > > We therefore need to both: (a) protect change_protection_range() from the > changes of others who might defer TLB flushes without taking mmap_sem for > write (e.g., try_to_unmap_one()); and (b) to protect others (e.g., > page-fault handlers) from concurrent changes of change_protection(). > > We have already encountered several similar bugs, and debugging such issues > s time consuming and these bugs impact is substantial (memory corruption, > security). So I think we should only stick to general solutions. > > So perhaps your the approach of your proposed solution is feasible, but it > would have to be applied all over the place: we will need to add a check for > mm_tlb_flush_pending() and conditionally flush the TLB in every case in > which PTEs are read and there might be an assumption that the > access-permission reflect what the TLBs hold. This includes page-fault > handlers, but also NUMA migration code in change_protection(), softdirty > cleanup in clear_refs_write() and maybe others. > > [ I have in mind another solution, such as keeping in each page-table a > “table-generation” which is the mm-generation at the time of the change, > and only flush if “table-generation”==“mm-generation”, but it requires > some thought on how to avoid adding new memory barriers. ] > > IOW: I think the change that you suggest is insufficient, and a proper > solution is too intrusive for “stable". > > As for performance, I can add another patch later to remove the TLB flush > that is unnecessarily performed during change_protection_range() that does > permission promotion. I know that your concern is about the “protect” case > but I cannot think of a good immediate solution that avoids taking mmap_lock > for write. > > Thoughts? >