Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4652771pxu; Mon, 21 Dec 2020 19:22:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwC/Ko7iSuFTcZyjnfDxfB1ovC8pfoBDAjLhr8YpUAWc1ExgEPmwcZa/9b6tXxZ1whXH98 X-Received: by 2002:a17:906:710b:: with SMTP id x11mr18755228ejj.433.1608607354025; Mon, 21 Dec 2020 19:22:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608607354; cv=none; d=google.com; s=arc-20160816; b=UC9c4FlK5ctyQi+yR7TU/GolEBi350D7TMCpTUCHHpu2nxlCkHfn/NFmPaz3Q5Sem/ CNPQyCioTkb4jj7TOYCs5HfVkyzwrzPBpW0d2bpyTcAbASEI6cbt43e2lSQFBwR+QJoN mpdCgk0LV3IIoJp+KmkGU6Z+91HDHAKGu42tBvJYY1AJCEatBe3HtICwX8wF3FUEBeTE HSfVb4MhRCWlTswCCzlqJzhaUh5LzeTey/J+0Gtc+xUk7usH9xHfdmqpiOXq9SpM0TIx hdo56gaLr1GEmxDRs8Pl4+skSf4FYZu/ri6h62q+YNkOnmp4WN1QEB8HILxc0CxKjWDZ ytfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=vY1erApPMCpUl1zSlc9lDDu4+yVqt85f0KgHlSxi0p0=; b=E5OfgnWWZ7Fc6X/5W4GZqXFZrYOnGlJb2kCyfk8kviYf141Hd49ix5G5JIcmGv+fym PwN5WjFAz4Yhx16pJGklbHOg39Mwz2nVtXINNeo4Eo58L9onICbaUhW9y3b938QFFFua aqjR6vjsHoKSAEotkZdamd+2baBDG07rvpffyPyS9Tr+xS5o0Q+XIzY8Qq/Ob5oaJ+qw U4mL9ZVL/mGj3bRFYvn4oIjjlOdDihEUgO/wLmmwsFqr7/f5EwHR2fT1bRJAuWIhtgpn ceEO7vlS4ep46rV5UfLlUBpjkJcL4f1CbVyxq9hYId1aGk7YVAdVeNgX4NJVieRCI2bS WQHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XhUcubjn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h14si9758568ejt.198.2020.12.21.19.22.11; Mon, 21 Dec 2020 19:22:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XhUcubjn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725975AbgLVDU3 (ORCPT + 99 others); Mon, 21 Dec 2020 22:20:29 -0500 Received: from mail.kernel.org ([198.145.29.99]:47170 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725833AbgLVDU3 (ORCPT ); Mon, 21 Dec 2020 22:20:29 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4711423105 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-f46.google.com with SMTP id i9so13112178wrc.4 for ; Mon, 21 Dec 2020 19:19:48 -0800 (PST) X-Gm-Message-State: AOAM533AMqgAbkPrAgwNoxa25m/gCGOjdctrvi0lLz3CTEBBRZq2QzW7 RIMJsiKP0lQq+uZChCmxxzgaI2LIxPizpbj4TUUDfA== 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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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