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=-1.4 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FSL_HELO_FAKE,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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 7BA29C433DB for ; Mon, 21 Dec 2020 19:16:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 14F8C229C5 for ; Mon, 21 Dec 2020 19:16:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 14F8C229C5 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 50F5F6B005C; Mon, 21 Dec 2020 14:16:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C1EB6B005D; Mon, 21 Dec 2020 14:16:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AFCB6B0068; Mon, 21 Dec 2020 14:16:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0018.hostedemail.com [216.40.44.18]) by kanga.kvack.org (Postfix) with ESMTP id 257B56B005C for ; Mon, 21 Dec 2020 14:16:28 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id DFB628249980 for ; Mon, 21 Dec 2020 19:16:27 +0000 (UTC) X-FDA: 77618245614.04.fan90_0f07c3d27459 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin04.hostedemail.com (Postfix) with ESMTP id C14168004332 for ; Mon, 21 Dec 2020 19:16:27 +0000 (UTC) X-HE-Tag: fan90_0f07c3d27459 X-Filterd-Recvd-Size: 7007 Received: from mail-io1-f44.google.com (mail-io1-f44.google.com [209.85.166.44]) by imf23.hostedemail.com (Postfix) with ESMTP for ; Mon, 21 Dec 2020 19:16:27 +0000 (UTC) Received: by mail-io1-f44.google.com with SMTP id y5so9833881iow.5 for ; Mon, 21 Dec 2020 11:16:27 -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=L3Gs6WqmH8kL8ZhxKYgFmuIwY0FVCfgjiIRrG/rLCoQ=; b=XR1oBnHY+FNDw8Pofg87UATPJwTQibIHCH0Wtd8KJvWHmSPCZRxXSfyyWDthuOtjpz eUEwuy4LEK+L7MORftyvnd3nSvXZM4VTd15C+RXRfz2XUWlMdOaGDfdHfR1QXaUseVO6 gIBis0u7qXKe8TzrNEGpL0BxU2h7AAGZihNGcDMktPLtjZdjlYJNnvs3klod/1dOfcFu wvZd7rZZOO926AziaTBTKvMeVO+b7cG1etMxlCrwvNLpR7qsCUovMewcZtn+4rKqWvOW 0Q+LAbozXG3rhB6WbprSCV/ddPOXiHpgoQKrJt+vGt6bjMu0c0Im9Ncl8ob9qQXnFcnq UYwQ== 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=L3Gs6WqmH8kL8ZhxKYgFmuIwY0FVCfgjiIRrG/rLCoQ=; b=pZBvxP1AL99bAm/JZEaGB14gJhPhu3QUYZcwkZCOzUTmbWVNXQuCGiyYqTfLeK7/TY Cps8IgmtmY7Iwj3xdqVQKuTCz9Rty3A1tfg8hXiyUlW5UI3caolD5tM+12p+ZzwnKxVN ft87aujPoW75gIfrYw0ino2EinuwHa7XoOkcYY7N9kJ3I+OUk/QOqCqcHiQZuHg6kRVE WD6FdbrYc6XPkZHFk8qqapaRwrgtYs42rwzDlzuDP9ZAHZGNlptYPAsiHGhGyKNcbCAW y+dZQq6Mo4/p/gnIwByqcCz1I0oGvFcapt3C06mCPD69y6G5r7YpgcdUwJCC4nG1hVVm uFeQ== X-Gm-Message-State: AOAM533ivjrR69UAIhR1koIkrL1Vh+jTnZz/ur5rvjCG1h6Ll5X3devs WCbk0wSEV3sAplBeQipWtW5o/w== X-Google-Smtp-Source: ABdhPJwOsREO3Cv01bkG3byoUcuKvOsFEAK5JGQ1eGqIHRODacDApA3vawIoWmAz77JL8ZnCmtxJhA== X-Received: by 2002:a5e:9b06:: with SMTP id j6mr15203388iok.171.1608578186432; Mon, 21 Dec 2020 11:16:26 -0800 (PST) Received: from google.com ([2620:15c:183:200:7220:84ff:fe09:2d90]) by smtp.gmail.com with ESMTPSA id o10sm14812859ili.82.2020.12.21.11.16.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Dec 2020 11:16:25 -0800 (PST) Date: Mon, 21 Dec 2020 12:16:21 -0700 From: Yu Zhao To: Linus Torvalds Cc: Peter Xu , Andrea Arcangeli , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable@vger.kernel.org, minchan@kernel.org, Andy Lutomirski , Will Deacon , Peter Zijlstra , Nadav Amit Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect Message-ID: References: <20201219043006.2206347-1-namit@vmware.com> <20201221172711.GE6640@xz-x1> <76B4F49B-ED61-47EA-9BE4-7F17A26B610D@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <76B4F49B-ED61-47EA-9BE4-7F17A26B610D@gmail.com> Content-Transfer-Encoding: quoted-printable 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 Mon, Dec 21, 2020 at 10:31:57AM -0800, Nadav Amit wrote: > > On Dec 21, 2020, at 9:27 AM, Peter Xu wrote: > >=20 > > Hi, Nadav, > >=20 > > On Sun, Dec 20, 2020 at 12:06:38AM -0800, Nadav Amit wrote: > >=20 > > [...] > >=20 > >> So to correct myself, I think that what I really encountered was act= ually > >> during MM_CP_UFFD_WP_RESOLVE (i.e., when the protection is removed).= The > >> problem was that in this case the =E2=80=9Cwrite=E2=80=9D-bit was re= moved during unprotect. > >> Sorry for the strange formatting to fit within 80 columns: > >=20 > > I assume I can ignore the race mentioned in the commit message but on= ly refer > > to this one below. However I'm still confused. Please see below. > >=20 > >> [ Start: PTE is writable ] > >>=20 > >> cpu0 cpu1 cpu2 > >> ---- ---- ---- > >> [ Writable PTE=20 > >> cached in TLB ] > >=20 > > Here cpu2 got writable pte in tlb. But why? > >=20 > > If below is an unprotect, it means it must have been protected once b= y > > userfaultfd, right? If so, the previous change_protection_range() wh= ich did > > the wr-protect should have done a tlb flush already before it returns= (since > > pages>0 - we protected one pte at least). Then I can't see why cpu2 = tlb has > > stall data. >=20 > Thanks, Peter. Just as you can munprotect() a region which was not prot= ected > before, you can ufff-unprotect a region that was not protected before. = It > might be that the user tried to unprotect a large region, which was > partially protected and partially unprotected. >=20 > The selftest obviously blindly unprotect some regions to check for bugs= . >=20 > So to your question - it was not write-protected (think about initial c= opy > without write-protecting). >=20 > > If I assume cpu2 doesn't have that cached tlb, then "write to old pag= e" won't > > happen either, because cpu1/cpu2 will all go through the cow path and= pgtable > > lock should serialize them. > >=20 > >> userfaultfd_writeprotect() =09 > >> [ write-*unprotect* ] > >> mwriteprotect_range() > >> mmap_read_lock() > >> change_protection() > >>=20 > >> change_protection_range() > >> ... > >> change_pte_range() > >> [ *clear* =E2=80=9Cwrite=E2=80=9D-bit ] > >> [ defer TLB flushes] > >> [ page-fault ] > >> =E2=80=A6 > >> wp_page_copy() > >> cow_user_page() > >> [ copy page ] > >> [ write to old > >> page ] > >> =E2=80=A6 > >> set_pte_at_notify() > >>=20 > >> [ End: cpu2 write not copied form old to new page. ] > >=20 > > Could you share how to reproduce the problem? I would be glad to giv= e it a > > shot as well. >=20 > You can run the selftests/userfaultfd with my small patch [1]. I ran it= with > the following parameters: =E2=80=9C ./userfaultfd anon 100 100 =E2=80=9C= . I think that it is > more easily reproducible with =E2=80=9Cmitigations=3Doff idle=3Dpoll=E2= =80=9D as kernel > parameters. >=20 > [1] https://lore.kernel.org/patchwork/patch/1346386/ Hi Linus, 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. 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.