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 E5331C00140 for ; Mon, 8 Aug 2022 16:02:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 321F08E0001; Mon, 8 Aug 2022 12:02:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D0816B0072; Mon, 8 Aug 2022 12:02:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 199328E0001; Mon, 8 Aug 2022 12:02:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0D5B36B0071 for ; Mon, 8 Aug 2022 12:02:12 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D5C4A1A1047 for ; Mon, 8 Aug 2022 16:02:11 +0000 (UTC) X-FDA: 79776892062.31.777E9A7 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 40FB62019B for ; Mon, 8 Aug 2022 16:02:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659974526; 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=v+VLxN4BnSUnRtGOHcjHhnP7NJbMaGRF/+oLZoagv+Q=; b=MvVXQpXkjQr7EOvqvif6r7MArWmAqFlBq0u4hSJiL2GKkFt9sh0gzwVxzzUsT6oaXk94ou n2qyFTE485V1wEYfaHfC+RFQEb0QTNbUuG8KBGAaBHntB0W7j4F4y+zoN8NXikekWk4HF2 arPiNzJq1JMLSFBv5QpzuYIhdc71izI= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-315-YEi_N5DxMqWxGtr4CQuZvw-1; Mon, 08 Aug 2022 12:02:05 -0400 X-MC-Unique: YEi_N5DxMqWxGtr4CQuZvw-1 Received: by mail-wm1-f72.google.com with SMTP id ay31-20020a05600c1e1f00b003a53bda5b0eso2307149wmb.0 for ; Mon, 08 Aug 2022 09:02:05 -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=v+VLxN4BnSUnRtGOHcjHhnP7NJbMaGRF/+oLZoagv+Q=; b=eke7Eu+Pv89zTKKrHUgZZVzi0Cdg44sN1DKADVbvj3r4R4gnN1F7ESdh6H/f/VMHAk 391rYRyH+Avff/tFCmgAZ6TM/e4T9SHlokgJOqrfAyZfF/KGFz94IUEM9sJPwH5PDiLX 3Py4efd7jFyP9c6LCHf0LV2YarFlb9nwE3cXFbn6WEK07YhZ9Ime438ldBqrlhX7WnYr vLqqeX8liY/RNe9+cMFXf6khJ3St+tfE/eehxkFH7ftC6iXkCYRkx0qe9rwAvo3+XSnA DeXNwID8TcTYe0YRT8TgSJdGcmLZg2+akBsaYlTwD5tYaQ+BdZKJuXRy5kV3cgOIY/6X IvVA== X-Gm-Message-State: ACgBeo25B76kVOf0KCsQUaDbWlFzYF5515vNZbQmrc/PxbI8oTjxwv5i MMRAxuG0+Lp1tqtMW5/a3AVv8oov6IVtzcmLGiHBkbgfRTN4X39bThPBxaoMaVJkHF1+PGEhP5v nHK5mmAlJLFg= X-Received: by 2002:a05:6000:1a41:b0:20e:687f:1c3 with SMTP id t1-20020a0560001a4100b0020e687f01c3mr11547713wry.415.1659974524212; Mon, 08 Aug 2022 09:02:04 -0700 (PDT) X-Google-Smtp-Source: AA6agR67Pq4plyoq9gtFCwaC8JrukP/DJqd25ev+P8l5w9C9NNDAqkHXuQ2j70Mv0jTTVryoR8cIsg== X-Received: by 2002:a05:6000:1a41:b0:20e:687f:1c3 with SMTP id t1-20020a0560001a4100b0020e687f01c3mr11547684wry.415.1659974523955; Mon, 08 Aug 2022 09:02:03 -0700 (PDT) Received: from ?IPV6:2003:d8:2f15:c300:d2ce:1fb5:2460:179a? (p200300d82f15c300d2ce1fb52460179a.dip0.t-ipconnect.de. [2003:d8:2f15:c300:d2ce:1fb5:2460:179a]) by smtp.gmail.com with ESMTPSA id e27-20020adfa45b000000b0021e519eba9bsm11746517wra.42.2022.08.08.09.02.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 08 Aug 2022 09:02:03 -0700 (PDT) Message-ID: Date: Mon, 8 Aug 2022 18:02:02 +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: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, stable@vger.kernel.org, Linus Torvalds , Andrew Morton , Greg Kroah-Hartman , Axel Rasmussen , Peter Xu , Hugh Dickins , Andrea Arcangeli , Matthew Wilcox , Vlastimil Babka , John Hubbard , Jason Gunthorpe , Nadav Amit References: <20220808073232.8808-1-david@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20220808073232.8808-1-david@redhat.com> 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=1659974528; a=rsa-sha256; cv=none; b=Rp4a7NborVrHZkxfDFXFSTZTSlm8Cne7ZfYvqdxJ1vvORR4g7T/2u4qplLAlC6t0bAds46 3QLgZnj7rm4mdxUla02sInckusz36uTN4d8fdNGurzVAp5iA51Oh73baRE2hSXk3LwaVjm kd/4jQPSmQGhVyAMwKVWnbDovUpjoeE= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MvVXQpXk; spf=pass (imf31.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659974528; 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=v+VLxN4BnSUnRtGOHcjHhnP7NJbMaGRF/+oLZoagv+Q=; b=CkCGb4TXuSpwdDXA/qKi+AIHz0zVHWQW+LCa17dVbeQp+idkj6CBLt6APZ9Sw1eELsJ9cF PXMWCUiSDrL1fVPWQRbW59yrcEMxM6Agks3F5elmIiqzwO7g74KLfXZTdoBs1g5BdoaSoh fKVjKj5OzqmuZMRabq/KcyKNPjw3GHo= X-Rspamd-Queue-Id: 40FB62019B X-Rspam-User: X-Rspamd-Server: rspam11 Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MvVXQpXk; spf=pass (imf31.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: tqre498jyiwdaieqjutxniepw8ygwnu5 X-HE-Tag: 1659974527-137487 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 08.08.22 09:32, David Hildenbrand wrote: > Ever since the Dirty COW (CVE-2016-5195) security issue happened, we know > that FOLL_FORCE can be possibly dangerous, especially if there are races > that can be exploited by user space. > > Right now, it would be sufficient to have some code that sets a PTE of > a R/O-mapped shared page dirty, in order for it to erroneously become > writable by FOLL_FORCE. The implications of setting a write-protected PTE > dirty might not be immediately obvious to everyone. > > And in fact ever since commit 9ae0f87d009c ("mm/shmem: unconditionally set > pte dirty in mfill_atomic_install_pte"), we can use UFFDIO_CONTINUE to map > a shmem page R/O while marking the pte dirty. This can be used by > unprivileged user space to modify tmpfs/shmem file content even if the user > does not have write permissions to the file -- Dirty COW restricted to > tmpfs/shmem (CVE-2022-2590). > > To fix such security issues for good, the insight is that we really only > need that fancy retry logic (FOLL_COW) for COW mappings that are not > writable (!VM_WRITE). And in a COW mapping, we really only broke COW if > we have an exclusive anonymous page mapped. If we have something else > mapped, or the mapped anonymous page might be shared (!PageAnonExclusive), > we have to trigger a write fault to break COW. If we don't find an > exclusive anonymous page when we retry, we have to trigger COW breaking > once again because something intervened. > > Let's move away from this mandatory-retry + dirty handling and rely on > our PageAnonExclusive() flag for making a similar decision, to use the > same COW logic as in other kernel parts here as well. In case we stumble > over a PTE in a COW mapping that does not map an exclusive anonymous page, > COW was not properly broken and we have to trigger a fake write-fault to > break COW. > > Just like we do in can_change_pte_writable() added via > commit 64fe24a3e05e ("mm/mprotect: try avoiding write faults for exclusive > anonymous pages when changing protection") and commit 76aefad628aa > ("mm/mprotect: fix soft-dirty check in can_change_pte_writable()"), take > care of softdirty and uffd-wp manually. > > 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. > > This fixes CVE-2022-2590. Note that only x86_64 and aarch64 are > affected, because only those support CONFIG_HAVE_ARCH_USERFAULTFD_MINOR. > > Fortunately, FOLL_COW is no longer required to handle FOLL_FORCE. So > let's just get rid of it. I have to add here: "Thanks to Nadav Amit for pointing out that the pte_dirty() check in FOLL_FORCE code is problematic and might be exploitable." -- Thanks, David / dhildenb