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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBFA8C19F2D for ; Tue, 9 Aug 2022 18:53:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7EF968E0002; Tue, 9 Aug 2022 14:53:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 79DBE8E0001; Tue, 9 Aug 2022 14:53:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6661D8E0002; Tue, 9 Aug 2022 14:53:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 589128E0001 for ; Tue, 9 Aug 2022 14:53:06 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2E998121103 for ; Tue, 9 Aug 2022 18:53:06 +0000 (UTC) X-FDA: 79780951572.01.92D012D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf31.hostedemail.com (Postfix) with ESMTP id BB91520057 for ; Tue, 9 Aug 2022 18:53:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660071185; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5MxbEkPFfHnhv9lIG9SKrVYa+3yd5Ii2mgv7KMCMiKA=; b=MwVPwrnkbAFEUC9IAx+zOQqkmwQfCd6a7ltEHWCNbPk67eHZPR1jwf7uDDEwWZB/eJon8s 3qycWranGQzYZcaupPw1qLifh2lQ2Osl1GA2nNHQoAenDGtHeci32O0E2GVvoLxtpCRubP AHLKWXkzo5ngpzYg7O9qxK0agRfxhsk= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-61-E-HUstCQORqUE5ODcksq4A-1; Tue, 09 Aug 2022 14:53:03 -0400 X-MC-Unique: E-HUstCQORqUE5ODcksq4A-1 Received: by mail-wm1-f70.google.com with SMTP id x17-20020a05600c21d100b003a32dda6577so2522809wmj.7 for ; Tue, 09 Aug 2022 11:53:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc; bh=5MxbEkPFfHnhv9lIG9SKrVYa+3yd5Ii2mgv7KMCMiKA=; b=LmmYKz1uebTlAhmsnPGPk4MGOy4BWDnLyaW3aLaPZSzVtsDey9HvQouzsmyEdPn4Dq sZMeOrC3IxPEHiBd5QAwvVjIhTHaiT8YvBf2zmi29xkzcp5iljwyBSPgmYMV2jr38Fxr LXqtpU+HySucWVq4HuxKPdWJT08VVZl6s8JzMWXpOAkdIWdRdpnZYA58BUdMEoQg2bqK 4nkCigiw+HAtWTN5/TiRI0PD+7PphQQ2QEOcfsP7Um64GDGh3iyAMSCchSBCwmS9aea7 oUWnnIsrO3g4C/5BNhhpeQtyU6cive5rKdWMOFKWBLawnBNGYcIvB2apZ6ol7d91W6ik +w+g== X-Gm-Message-State: ACgBeo1X2jmLmwVa/wwnLZUWwcLI+0J3YWVWMLeMjxwhwt40zH0GLR4c cfnYgqa1HDaNE1YX1Rzs9X0vnwraYHWaTQPF4JRVqBHFKJIWTt7jFxjuZ2AvpKG5M/s8bc5wdiv oE2LPkv8MeS4= X-Received: by 2002:a05:6000:15c5:b0:220:727a:24bf with SMTP id y5-20020a05600015c500b00220727a24bfmr15682585wry.621.1660071182500; Tue, 09 Aug 2022 11:53:02 -0700 (PDT) X-Google-Smtp-Source: AA6agR7B1MwvtmEN6yaB1m5uxgVV9LrfTrVuas1WczTE6dxZ7xo83OW/ncC8YwQvF+UDacFqG6b94g== X-Received: by 2002:a05:6000:15c5:b0:220:727a:24bf with SMTP id y5-20020a05600015c500b00220727a24bfmr15682577wry.621.1660071182244; Tue, 09 Aug 2022 11:53:02 -0700 (PDT) Received: from ?IPV6:2003:cb:c705:3700:aed2:a0f8:c270:7f30? (p200300cbc7053700aed2a0f8c2707f30.dip0.t-ipconnect.de. [2003:cb:c705:3700:aed2:a0f8:c270:7f30]) by smtp.gmail.com with ESMTPSA id o4-20020a056000010400b0021e501519d3sm14070439wrx.67.2022.08.09.11.53.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 09 Aug 2022 11:53:01 -0700 (PDT) Message-ID: Date: Tue, 9 Aug 2022 20:53:01 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH v1] mm/gup: fix FOLL_FORCE COW security issue and remove FOLL_COW To: Jason Gunthorpe , Linus Torvalds Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Andrew Morton , Greg Kroah-Hartman , Axel Rasmussen , Peter Xu , Hugh Dickins , Andrea Arcangeli , Matthew Wilcox , Vlastimil Babka , John Hubbard References: <20220808073232.8808-1-david@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660071185; a=rsa-sha256; cv=none; b=FDFEv6rhVzdij2ilfFXtbUa+FDmghJQeA5lVAQeQlR7fVZYyS9qsl6y6Ie1HYGW0dGst5d mFR3x/r7w1ob2wUXSUaj0KWmXQi2+COQ/HXstYrWWqSZ7fImTpfc2FfSktXxGflHzqbXsd PGX4zsGptCO3q8WRsYEH+cw9udtol4Q= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MwVPwrnk; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf31.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660071185; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5MxbEkPFfHnhv9lIG9SKrVYa+3yd5Ii2mgv7KMCMiKA=; b=CR6Mvn+m+YyJvRz26cWaOoyikmgSEEACTxhbtXPtG+LwZJ3HSrTME6TX1pP4von9bDsCYd Yd9Ubdjor51RRzOf0fF64tbZk9nSwDdqkYR7iURqPfO19RgzXBBSDQq09xGSJEK9Yk/tFh b06isXIi5Z7ueVtbc689PgzM6dB9TD8= X-Rspamd-Server: rspam10 X-Stat-Signature: exo3nshhfn7t7aadohhfw8jrwgpus5iz Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MwVPwrnk; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf31.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com X-Rspam-User: X-Rspamd-Queue-Id: BB91520057 X-HE-Tag: 1660071185-950103 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 09.08.22 20:48, Jason Gunthorpe wrote: > On Tue, Aug 09, 2022 at 11:40:50AM -0700, Linus Torvalds wrote: >> On Mon, Aug 8, 2022 at 12:32 AM David Hildenbrand wrote: >>> >>> For example, a write() via /proc/self/mem to a uffd-wp-protected range has >>> to fail instead of silently granting write access and bypassing the >>> userspace fault handler. Note that FOLL_FORCE is not only used for debug >>> access, but also triggered by applications without debug intentions, for >>> example, when pinning pages via RDMA. >> >> So this made me go "Whaa?" >> >> I didn't even realize that the media drivers and rdma used FOLL_FORCE. >> >> That's just completely bogus. >> >> Why do they do that? >> >> It seems to be completely bogus, and seems to have no actual valid >> reason for it. Looking through the history, it goes back to the >> original code submission in 2006, and doesn't have a mention of why. > > It is because of all this madness with COW. > > Lets say an app does: > > buf = mmap(MAP_PRIVATE) > rdma_pin_for_read(buf) > buf[0] = 1 > > Then the store to buf[0] will COW the page and the pin will become > decoherent. > > The purpose of the FORCE is to force COW to happen early so this is > avoided. > > Sadly there are real apps that end up working this way, eg because > they are using buffer in .data or something. > > I've hoped David's new work on this provided some kind of path to a > proper solution, as the need is very similar to all the other places > where we need to ensure there is no possiblity of future COW. > > So, these usage can be interpreted as a FOLL flag we don't have - some > kind of (FOLL_EXCLUSIVE | FOLL_READ) to match the PG_anon_exclusive > sort of idea. Thanks Jason for the explanation. I have patches in the works to no longer use FOLL_FORCE|FOLL_WRITE for taking a reliable longerterm R/O pin in a MAP_PRIVATE mapping. The patches are mostly done (and comparably simple), I merely deferred sending them out because I stumbled over this issue first. Some ugly corner cases of MAP_SHARED remain, but for most prominent use cases, my upcoming patches should allow us to just have longterm R/O pins working as expected. -- Thanks, David / dhildenb