Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp5661494pxu; Wed, 23 Dec 2020 02:08:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJx6IBlJlUj+E3a7y2+f4Rj3SyIbXYXpJI7ycj0tNM7mdWwcVKq2JuZ1n7ePI6K1WnfT0P9g X-Received: by 2002:a50:e882:: with SMTP id f2mr23446128edn.76.1608718126566; Wed, 23 Dec 2020 02:08:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608718126; cv=none; d=google.com; s=arc-20160816; b=uSEjafTDlVR6MuJ0paLnnxgegul/hFwvT6xPSPtk2gSrsVM1+Tb5mpBKN6TIi+Ht8S Lghl593fzan1B4XAhfGHIce2Zt7Jgh8/kXliaqkmiWOoVqujVaTPE0spI+KLDZskyBRq gH+PD3ekDc3ERjL+lxuxq136zfq+cwqcq2lnixiqQSPZiVsWu0jB7UDEPZvoyjFO6hnM /D6WI/XpmAWBRCXveqW1s6ylKGHEnl7/iHlCfI8MF9UAv4Rp2oGB/VhS5ucnFRMrF61C PoA3b391WZupgYGfZyj6P7lZsDz9M83W3WygLLbTue5hDJrE2hg2ywwXTUi5CIgytJaJ Lx9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=TsZfeXRM1ReVd7eBjQcIMfaYnF6JpJXqu5pz2p0wGEQ=; b=C2221mDPqHK0b4GD+RgqS2rQL5LZpjhezlYkeFdOPcPokhAOC0P3ZFsFyVX7Y9v4Mm D/p6XtJp2t2SIgaeTe0q0L+58UVLweeXNyCdzOcYXaSgK/2LLFB4RzHyxjZ/AvI6juVe VDdcKGdT/phGqJYaWUDmQYNFGKgxiXAwAwt5RXTyZsB7QYoyIfdLrhCdyWMJva3MMMup qNYZckmmB6LLoPYoUdeMFJB4JQIqW8NiMrwlM7Ge2G/0F9llmS04NZ6EDauETQvufpMn R67FRWIL3CSrd3KScpffZwettELWk+/pAkBmCkTHXcx/6AWVehLHgpSzNL8OSlODofnW F52A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=CNmoLodA; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b2si6357733ejc.551.2020.12.23.02.08.24; Wed, 23 Dec 2020 02:08:46 -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=@google.com header.s=20161025 header.b=CNmoLodA; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728553AbgLWKHR (ORCPT + 99 others); Wed, 23 Dec 2020 05:07:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728214AbgLWKHQ (ORCPT ); Wed, 23 Dec 2020 05:07:16 -0500 Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6C67C0613D6 for ; Wed, 23 Dec 2020 02:06:35 -0800 (PST) Received: by mail-io1-xd2c.google.com with SMTP id i18so14651981ioa.1 for ; Wed, 23 Dec 2020 02:06:35 -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:in-reply-to; bh=TsZfeXRM1ReVd7eBjQcIMfaYnF6JpJXqu5pz2p0wGEQ=; b=CNmoLodAEpv7gOwb8VpGyiCO30xoo2vCseCl7Eji3i9ZVItKzd5J+ZHmP4Q90yW9SF v9/P4uSVz7jVdIkY/Q7aJORA3LOHdltQkbnFMryoxugSt3lIs3VvpBheK+VoEHaroXxq GrILcIEPhED0FYieTZbeY6px2c/7Y+7nHdeWGGqdoSpqu0F+ojsemeGhHiIY8iO5KauO vYeFCrHb+nlpRHSDYs+V3QZIxHwep2HohggzXgELErQKjWM2g8p/N9RjYIY+sOMCzknf W/tq16osSog5ItcqmtldMo0KTddd4OEAdZzG2SgrPWKfdkqAfUfij21WlwmTwR4h8ro5 yNXA== 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:in-reply-to; bh=TsZfeXRM1ReVd7eBjQcIMfaYnF6JpJXqu5pz2p0wGEQ=; b=dnSYf+dfktJ7g/eVurdqqgZbAlCXL/+USBHy0kPn/E+t6On8HqjpOabmdS2WdXJmYQ jteo+1Lscs8inVEkB8TCOSnkrFVEdFDs6CHlz0fFhaRqvoDbjxjEzHGRsFq+hGHy/zqT yD9o3x/lxAGbew98d9vvFOUPVOi6Xey/9WYlQN9hjeKHCZxcNJ3ZvDbJLWBGkXY5+KO/ LjL0+nfRGdn8cyJoPFtlNpPg2fKR2X2CMj8f8QROFGWXwBCaX/UBoKcBA6jejeTzxvSo tbP/fWp6XF6TbDZePZa5f2hPc9AOOCNQOd8Rguc9UNDOBUKVQ18xAckfRAjB1ylUepSd l6LQ== X-Gm-Message-State: AOAM531gMU0EvNQuQAHjvDjpfS71umHDBKgCzemh9DT0UTmIhIJ4Qetb vv0ebTSHTz6VbH6RBE7t0V5u+w== X-Received: by 2002:a05:6602:387:: with SMTP id f7mr21085021iov.209.1608717994800; Wed, 23 Dec 2020 02:06:34 -0800 (PST) Received: from google.com ([2620:15c:183:200:7220:84ff:fe09:2d90]) by smtp.gmail.com with ESMTPSA id 12sm17304825ily.42.2020.12.23.02.06.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Dec 2020 02:06:34 -0800 (PST) Date: Wed, 23 Dec 2020 03:06:30 -0700 From: Yu Zhao To: Linus Torvalds Cc: Andrea Arcangeli , Andy Lutomirski , Peter Xu , Nadav Amit , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable , Minchan Kim , Will Deacon , Peter Zijlstra Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect Message-ID: References: <20201221223041.GL6640@xz-x1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 23, 2020 at 01:44:42AM -0800, Linus Torvalds wrote: > On Tue, Dec 22, 2020 at 4:01 PM Linus Torvalds > wrote: > > > > The more I look at the mprotect code, the less I like it. We seem to > > be much better about the TLB flushes in other places (looking at > > mremap, for example). The mprotect code seems to be very laissez-faire > > about the TLB flushing. > > No, this doesn't help. > > > Does adding a TLB flush to before that > > > > pte_unmap_unlock(pte - 1, ptl); > > > > fix things for you? > > It really doesn't fix it. Exactly because - as pointed out earlier - > the actual page *copy* happens outside the pte lock. I appreciate all the pointers. It seems to me it does. > So what can happen is: > > - CPU 1 holds the page table lock, while doing the write protect. It > has cleared the writable bit, but hasn't flushed the TLB's yet > > - CPU 2 did *not* have the TLB entry, sees the new read-only state, > takes a COW page fault, and reads the PTE from memory (into > vmf->orig_pte) In handle_pte_fault(), we lock page table and check pte_write(), so we either see a RW pte before CPU 1 runs or a RO one with no stale tlb entries after CPU 1 runs, assume CPU 1 flushes tlb while holding the same page table lock (not mmap_lock). > - CPU 2 correctly decides it needs to be a COW, and copies the page contents > > - CPU 3 *does* have a stale TLB (because TLB invalidation hasn't > happened yet), and writes to that page in users apce > > - CPU 1 now does the TLB invalidate, and releases the page table lock > > - CPU 2 gets the page table lock, sees that its PTE matches > vmf->orig_pte, and switches it to be that writable copy of the page. > > where the copy happened before CPU 3 had stopped writing to the page. > > So the pte lock doesn't actually matter, unless we actually do the > page copy inside of it (on CPU2), in addition to doing the TLB flush > inside of it (on CPU1). > > mprotect() is actually safe for two independent reasons: (a) it does > the mmap_sem for writing (so mprotect can't race with the COW logic at > all), and (b) it changes the vma permissions so turning something > read-only actually disables COW anyway, since it won't be a COW, it > will be a SIGSEGV. > > So mprotect() is irrelevant, other than the fact that it shares some > code with that "turn it read-only in the page tables". > > fork() is a much closer operation, in that it actually triggers that > COW behavior, but fork() takes the mmap_sem for writing, so it avoids > this too. > > So it's really just userfaultfd and that kind of ilk that is relevant > here, I think. But that "you need to flush the TLB before releasing > the page table lock" was not true (well, it's true in other > circumstances - just not *here*), and is not part of the solution. > > Or rather, if it's part of the solution here, it would have to be > matched with that "page copy needs to be done under the page table > lock too". > > Linus >