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=-2.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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 02BEFC433DB for ; Mon, 21 Dec 2020 20:23:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 87D56224D4 for ; Mon, 21 Dec 2020 20:23:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 87D56224D4 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 1AF2A6B0036; Mon, 21 Dec 2020 15:23:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 185DE6B0068; Mon, 21 Dec 2020 15:23:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C30C6B006C; Mon, 21 Dec 2020 15:23:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0050.hostedemail.com [216.40.44.50]) by kanga.kvack.org (Postfix) with ESMTP id EB8606B0036 for ; Mon, 21 Dec 2020 15:23:12 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id A9F5A1F0A for ; Mon, 21 Dec 2020 20:23:12 +0000 (UTC) X-FDA: 77618413824.11.guide37_0801ce52745a Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin11.hostedemail.com (Postfix) with ESMTP id 8A30A180FA17D for ; Mon, 21 Dec 2020 20:23:12 +0000 (UTC) X-HE-Tag: guide37_0801ce52745a X-Filterd-Recvd-Size: 6734 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf20.hostedemail.com (Postfix) with ESMTP for ; Mon, 21 Dec 2020 20:23:11 +0000 (UTC) Received: by mail-pl1-f182.google.com with SMTP id j1so6201957pld.3 for ; Mon, 21 Dec 2020 12:23:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=IhDqxXtaSRuZsIjaLCj/NmcYBTl37UcNZwF/uRakx8k=; b=jUNhpSVIQNM4f3fXEAeUMtmUJ1gSJB8MISFd63Iq7QAUKXXonTSA/gbsmBJbtdncAb HyRXprr1VgIiB+ypCp0/MkhlFNwZaR7bHbw9sbxVUK4kQzWt3krAM2WzFQ3jOVkrJu27 ADcOVcW0ImaLLra1uJ83kx5HdO62gOp0kk5RzNaQf2/TSfOx3Vb4SEQP7u7B0g+ZB8hJ 283d8tU5RipqWYfCTliyWzoq3EuNViPy1sPn9EkYrXUCaF4Yes9Xfewouv4qqCUTx6Y0 mxwKIbaMY98nvf72SQGzZuJ5kHjyr/tQa+thlusILZ/JKC4d8WLb/yfmXkSlZdu4KAKt lnQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=IhDqxXtaSRuZsIjaLCj/NmcYBTl37UcNZwF/uRakx8k=; b=QHUv4JSHMv7Miq51OlCjTLGrg7wRRyhLjS0Etpc+YS+5mbyBSBM/i6DNuZ2J+LGPE+ muCs8AxfBoK+81uuuB+N8oXEuIfxJjVFcPqLjGQXKCkr+Kxw/m/uh40h/WgIfMYFJEKc KN+7N/vR//C/N5fgtp88WSDFkW+6It3njDiZ4j8oqhhfT+8VfXermoHA3vXjtwS/jEaR y+4GLVDqgXAKaLA2ZiJ9XUBu0jHCDGltIBIJQOgTZZCv/TKzYmK11D/H2nPAAywfEU2F 8uXk+2RYxK2Zm6r0ZXRGNAVAsZkzsbNlwWwmxpX1hkk+V3WQuK46owgQ4nk5DXEss9ZY nJkQ== X-Gm-Message-State: AOAM532dul16LuAs1r4Tu6JeHO+RFLsItwJFwAIX+blykfUQfj+WZtKH mVR3JYVn75L0qrnBiixIp3U= X-Google-Smtp-Source: ABdhPJzejpo1qNQLXklic1ZtlJOOw2VX+WgbbKLGqczDFXLubIFnNa3AddddFXTAil1PfzsgjS1uVw== X-Received: by 2002:a17:902:a708:b029:da:ec42:a3d4 with SMTP id w8-20020a170902a708b02900daec42a3d4mr17639493plq.40.1608582190897; Mon, 21 Dec 2020 12:23:10 -0800 (PST) Received: from ?IPv6:2601:647:4700:9b2:104c:8d35:de28:b8dc? ([2601:647:4700:9b2:104c:8d35:de28:b8dc]) by smtp.gmail.com with ESMTPSA id s10sm17598459pgg.76.2020.12.21.12.23.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Dec 2020 12:23:09 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect From: Nadav Amit In-Reply-To: Date: Mon, 21 Dec 2020 12:23:06 -0800 Cc: Yu Zhao , Peter Xu , Andrea Arcangeli , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable , Minchan Kim , Andy Lutomirski , Will Deacon , Peter Zijlstra Content-Transfer-Encoding: quoted-printable Message-Id: <9E301C7C-882A-4E0F-8D6D-1170E792065A@gmail.com> References: <20201219043006.2206347-1-namit@vmware.com> <20201221172711.GE6640@xz-x1> <76B4F49B-ED61-47EA-9BE4-7F17A26B610D@gmail.com> To: Linus Torvalds X-Mailer: Apple Mail (2.3608.120.23.2.4) 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 Dec 21, 2020, at 11:55 AM, Linus Torvalds = wrote: >=20 > On Mon, Dec 21, 2020 at 11:16 AM Yu Zhao wrote: >> Nadav Amit found memory corruptions when running userfaultfd test = above. >> It seems to me the problem is related to commit 09854ba94c6a ("mm: >> do_wp_page() simplification"). Can you please take a look? Thanks. >>=20 >> TL;DR: it may not safe to make copies of singly mapped (non-COW) = pages >> when it's locked or has additional ref count because concurrent >> clear_soft_dirty or change_pte_range may have removed pte_write but = yet >> to flush tlb. >=20 > Hmm. The TLB flush shouldn't actually matter, because anything that > changes the writable bit had better be serialized by the page table > lock. >=20 > Yes, we often load the page table value without holding the page table > lock (in order to know what we are going to do), but then before we > finalize the operation, we then re-check - undet the page table lock - > that the value we loaded still matches. >=20 > But I think I see what *MAY* be going on. The userfaultfd > mwriteprotect_range() code takes the mm lock for _reading_. Which > means that you can have >=20 > Thread A Thread B >=20 > - fault starts. Sees write-protected pte, allocates memory, copies = data >=20 > - userfaultfd makes the regions writable >=20 > - usefaultfd case writes to the region >=20 > - userfaultfd makes region non-writable >=20 > - fault continues, gets the page table lock, sees that the pte is the > same, uses old copied data >=20 > But if this is what's happening, I think it's a userfaultfd bug. I > think the mmap_read_lock(dst_mm) in mwriteprotect_range() needs to be > a mmap_write_lock(). >=20 > mprotect() does this right, it looks like userfaultfd does not. You > cannot just change the writability of a page willy-nilly without the > correct locking. >=20 > Maybe there are other causes, but this one stands out to me as one > possible cause. >=20 > Comments? Using mmap_write_lock() was my initial fix and there was a strong = pushback on this approach due to its potential impact on performance. So I think there are 4 possible directions for solutions that I thought = of or others have raised/hinted: 1. mmap_write_lock() 2. Copy the page in cow_user_page() while holding the PTL and after = flushing has been done. I am not sure if there are potential problems with special-pages (2 flushes might be necessary for special pages). 3. Always reuse the page and never COW on userfaultfd/soft-dirty. I do not know if it is feasible. 4. Retry the page-fault if mm->tlb_flush_pending is set. Hopefully I did not miss any other suggestion.