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=-6.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,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 D0EFDC433E0 for ; Tue, 22 Dec 2020 03:19:51 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 54D1C22B2D for ; Tue, 22 Dec 2020 03:19:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 54D1C22B2D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 818D68D0006; Mon, 21 Dec 2020 22:19:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C78D8D0001; Mon, 21 Dec 2020 22:19:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6DE978D0006; Mon, 21 Dec 2020 22:19:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0107.hostedemail.com [216.40.44.107]) by kanga.kvack.org (Postfix) with ESMTP id 57EF98D0001 for ; Mon, 21 Dec 2020 22:19:50 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 22A7D8249980 for ; Tue, 22 Dec 2020 03:19:50 +0000 (UTC) X-FDA: 77619463740.06.stamp96_291461c2745c Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id F3525100404C9 for ; Tue, 22 Dec 2020 03:19:49 +0000 (UTC) X-HE-Tag: stamp96_291461c2745c X-Filterd-Recvd-Size: 4813 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf36.hostedemail.com (Postfix) with ESMTP for ; Tue, 22 Dec 2020 03:19:49 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id 277E722CB2 for ; Tue, 22 Dec 2020 03:19:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1608607188; bh=FotjZisX966kuUXL0D8Gn9J6bdxuHo48V44mRV8iSi8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=XhUcubjnh2HyUFiUydLIFRTZ7/sGFH/GXPhv3g6TDB0sf+Aa2byWq7GCahRO7EMlQ cjfsgp9lH573lB+lF3d2jfe0ohjW5dntXU0xBQtzfxtB6p+gM2aoUDasS0d0CwAL7c 4iVjbydOweneVPK3C5y64Hz5gyBxBVRlf+c4w/aFpoNe07S6/W1g+NQijEfw2xy/4O tFivUdIkgWU/g4y/jmnJC4ECtj/zcYO3nMlNGbBSsK0D6b8kAcDMiiuL32BDKJS6o/ 14dlF461YQaPzd2yTkagSzG2BwM1vCWHBnuYNzstIzbE/wF2IZUJIDVaiWcnftpfQC UKf7jsgXOXnOQ== Received: by mail-wr1-f43.google.com with SMTP id q18so13120797wrn.1 for ; Mon, 21 Dec 2020 19:19:48 -0800 (PST) X-Gm-Message-State: AOAM530mJPuzOMp6MLdRU2Xwu6d9R0gjhrZB2eA5mNGnNRi1An8Z6Z6w XUfT0NfC3sBspDS2lMA9J+Cqb6R88nElvQCBjYma6g== X-Google-Smtp-Source: ABdhPJwTCgH3BmlX+GODdwcxPSZVy++AHA4Lx/hL1P4NF3Nxh69XC7OXOw9Si7NYZsjkZA/RhLZin44WT3xNAvcGE2Y= X-Received: by 2002:adf:db43:: with SMTP id f3mr22479424wrj.70.1608607186744; Mon, 21 Dec 2020 19:19:46 -0800 (PST) MIME-Version: 1.0 References: <20201221172711.GE6640@xz-x1> <76B4F49B-ED61-47EA-9BE4-7F17A26B610D@gmail.com> <9E301C7C-882A-4E0F-8D6D-1170E792065A@gmail.com> <1FCC8F93-FF29-44D3-A73A-DF943D056680@gmail.com> <20201221223041.GL6640@xz-x1> In-Reply-To: From: Andy Lutomirski Date: Mon, 21 Dec 2020 19:19:35 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect To: Linus Torvalds Cc: Peter Xu , Nadav Amit , Yu Zhao , Andrea Arcangeli , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable , Minchan Kim , Andy Lutomirski , Will Deacon , Peter Zijlstra Content-Type: text/plain; charset="UTF-8" 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 3:22 PM Linus Torvalds wrote: > > On Mon, Dec 21, 2020 at 2:30 PM Peter Xu wrote: > > > > AFAIU mprotect() is the only one who modifies the pte using the mmap write > > lock. NUMA balancing is also using read mmap lock when changing pte > > protections, while my understanding is mprotect() used write lock only because > > it manipulates the address space itself (aka. vma layout) rather than modifying > > the ptes, so it needs to. > > So it's ok to change the pte holding only the PTE lock, if it's a > *one*way* conversion. > > That doesn't break the "re-check the PTE contents" model (which > predates _all_ of the rest: NUMA, userfaultfd, everything - it's > pretty much the original model for our page table operations, and goes > back to the dark ages even before SMP and the existence of a page > table lock). > > So for example, a COW will always create a different pte (not just > because the page number itself changes - you could imagine a page > getting re-used and changing back - but because it's always a RO->RW > transition). > > So two COW operations cannot "undo" each other and fool us into > thinking nothing changed. > > Anything that changes RW->RO - like fork(), for example - needs to > take the mmap_lock. Ugh, this is unpleasantly complicated. I will admit that any API that takes an address and more-or-less-blindly marks it RO makes me quite nervous even assuming all the relevant locks are held. At least userfaultfd refuses to operate on VM_SHARED VMAs, but we have another instance of this (with mmap_sem held for write) in x86: mark_screen_rdonly(). Dare I ask how broken this is? We could likely get away with deleting it entirely. --Andy