Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp2406912rwr; Fri, 28 Apr 2023 09:54:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5vGE/b7VT8c5FlcsGuKAHDV9NcMDiEZDJwvCT4NUO0Fa3gQQkkRzewd9CskTdH24I5Q7e2 X-Received: by 2002:a05:6a20:918a:b0:f0:2893:8a34 with SMTP id v10-20020a056a20918a00b000f028938a34mr7801506pzd.52.1682700885880; Fri, 28 Apr 2023 09:54:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682700885; cv=none; d=google.com; s=arc-20160816; b=PU0Fx1Xp9epCYem+jF1L4PZynwJP+OZ5Zm23A9KLB1xClSrq2E5nBGSfZplqLj5ZAn eoeID2XUHiNQCmpVECg8Nz29tKkQ0+k9oZk6dlUECHE8GOT3TFZYGMU1/tWsgdUPuz/5 s5VAMHH4QySEHYWW7pPrjGpd7FwQq58v/7AMWyLRx0yTMIt2q7Q7MSED+jLP/O5WYdkU +t6wu0/o14LMnjyE/5i+KNQZ4p44tu938SVDujLn8OVOqVmadYxfXuylvrxSakV3vTpw aAPoOL8/bbP3FFwNbsJOSQjkqwvrvgWNK3DetfkH5GE25YwBRALiJCpfuM3UIMDuMYqu iElA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :organization:from:references:cc:to:content-language:user-agent :mime-version:date:message-id:dkim-signature; bh=9UJtOJNRjK11X6Ax3/YkWZBfom69VzTQrdhUY1o0X2A=; b=m6buhmwfS1UqgeuZws7UWAlhVpidbNqy/s4clBz5vDUiMyEs9JqUdZgioItwOfhWvi amzzLO8D75P0EzGTHgj825Ct/7wSMmQjv/FcRCM9mT3qpEpPYrtEiMPXMOVOD0SnewIi dDyQGY1rXVQkNhqfEmfLryoTGlbwFqCYsUPXdsTe7mJN0Vj0JIalwzlnxPHH1h2JkltI Md/XI4yxM4inUp5qMIccy+C3MedAhYmpjsMrzUtFnAPa+0zan4RU7+VKZrU+emPUEuHt 0WYo4+2rVC0b2VZJ+SoPVbgfjQbVV3bCB8IXhJHcs36/avtYsnWFk5qGWvGvCYH74jQv cMsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SIJnKvEm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s191-20020a632cc8000000b0052868eb9742si10211404pgs.77.2023.04.28.09.54.24; Fri, 28 Apr 2023 09:54:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SIJnKvEm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346346AbjD1Qwl (ORCPT + 99 others); Fri, 28 Apr 2023 12:52:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbjD1Qwj (ORCPT ); Fri, 28 Apr 2023 12:52:39 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBB835BA9 for ; Fri, 28 Apr 2023 09:51:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682700713; 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=9UJtOJNRjK11X6Ax3/YkWZBfom69VzTQrdhUY1o0X2A=; b=SIJnKvEmM/1ccfhjUXdSa1J2BnIQm88ZD2VMmKMGfJNp8WiK5YubpsnCGcCfBaJQh09KF1 DLqvAveD6merFb12ueLfkVER+yuHc5bE8yYx4Nygib8KlpxiIqKYoBVM9EjR4sJMAc+/mJ zv7wWumQUSLkI/HjuOqpeBGLXHKGM8w= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-189-AoNx9u--PSyhSEakCy4vbg-1; Fri, 28 Apr 2023 12:51:50 -0400 X-MC-Unique: AoNx9u--PSyhSEakCy4vbg-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-3f315735edeso44570565e9.1 for ; Fri, 28 Apr 2023 09:51:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682700710; x=1685292710; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9UJtOJNRjK11X6Ax3/YkWZBfom69VzTQrdhUY1o0X2A=; b=EfhqJ1n/a7bXaXHvM0u9ZlWIgj/ZccIW/PVUI3AUnSKS7axtOzPYm85vVSGC1N2L20 3GzThm3hhdiKjyjMH4FXn0xpf6EjqHLfWcuMRVyOPsZFuyYmxRLje0ALHp6Vp0v/GSd5 h3QPPN4DzsX1SeKrDAqi5X1gV2sdDyR6wc3TcahQ1DqzBd06oHjQmYgX28gxI+3TjrcQ 6Oo/5dVtgQEMnIDbP+I4/sISCRpuQGkZR2ThW5UPe3inAiLdPfMfbYONXYnpK5TsFixX +X6KdOtRvB0zOP1L5dqcMVVgzJ8SYErJGnNG+6Y6c8tdXg8hYN5e3ox6nOhdaONd/5ci l2Lg== X-Gm-Message-State: AC+VfDzx3aFU5nuvCGNICNWdVXECMtgaqfx+VEOHARdUxCL8y5u8v8pw z4GKT7b8/pKgyjUkkNHuoi/JQxp+OZzwkqCs4cxBjxUOzWl1ogOY7b+gnMg2eluB4G6ZHzpM0dY 0Btv6LFeqfXfjaqonXcXkgDB9 X-Received: by 2002:a05:600c:350c:b0:3ee:93d2:c915 with SMTP id h12-20020a05600c350c00b003ee93d2c915mr7076523wmq.6.1682700709872; Fri, 28 Apr 2023 09:51:49 -0700 (PDT) X-Received: by 2002:a05:600c:350c:b0:3ee:93d2:c915 with SMTP id h12-20020a05600c350c00b003ee93d2c915mr7076499wmq.6.1682700709524; Fri, 28 Apr 2023 09:51:49 -0700 (PDT) Received: from ?IPV6:2003:cb:c726:9300:1711:356:6550:7502? (p200300cbc72693001711035665507502.dip0.t-ipconnect.de. [2003:cb:c726:9300:1711:356:6550:7502]) by smtp.gmail.com with ESMTPSA id k18-20020a05600c0b5200b003edf2dc7ca3sm24690362wmr.34.2023.04.28.09.51.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Apr 2023 09:51:48 -0700 (PDT) Message-ID: <173337c0-14f4-3246-15ff-7fbf03861c94@redhat.com> Date: Fri, 28 Apr 2023 18:51:46 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Content-Language: en-US To: Peter Xu , "Kirill A . Shutemov" Cc: Lorenzo Stoakes , Jason Gunthorpe , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Jens Axboe , Matthew Wilcox , Dennis Dalessandro , Leon Romanovsky , Christian Benvenuti , Nelson Escobar , Bernard Metzler , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Bjorn Topel , Magnus Karlsson , Maciej Fijalkowski , Jonathan Lemon , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Christian Brauner , Richard Cochran , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , linux-fsdevel@vger.kernel.org, linux-perf-users@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Oleg Nesterov , John Hubbard , Jan Kara , Pavel Begunkov , Mika Penttila , David Howells , Christoph Hellwig References: <094d2074-5b69-5d61-07f7-9f962014fa68@redhat.com> <400da248-a14e-46a4-420a-a3e075291085@redhat.com> <077c4b21-8806-455f-be98-d7052a584259@lucifer.local> <62ec50da-5f73-559c-c4b3-bde4eb215e08@redhat.com> <6ddc7ac4-4091-632a-7b2c-df2005438ec4@redhat.com> <20230428160925.5medjfxkyvmzfyhq@box.shutemov.name> <39cc0f26-8fc2-79dd-2e84-62238d27fd98@redhat.com> <20230428162207.o3ejmcz7rzezpt6n@box.shutemov.name> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v5] mm/gup: disallow GUP writing to file-backed mappings by default In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 28.04.23 18:39, Peter Xu wrote: > On Fri, Apr 28, 2023 at 07:22:07PM +0300, Kirill A . Shutemov wrote: >> On Fri, Apr 28, 2023 at 06:13:03PM +0200, David Hildenbrand wrote: >>> On 28.04.23 18:09, Kirill A . Shutemov wrote: >>>> On Fri, Apr 28, 2023 at 05:43:52PM +0200, David Hildenbrand wrote: >>>>> On 28.04.23 17:34, David Hildenbrand wrote: >>>>>> On 28.04.23 17:33, Lorenzo Stoakes wrote: >>>>>>> On Fri, Apr 28, 2023 at 05:23:29PM +0200, David Hildenbrand wrote: >>>>>>>>>> >>>>>>>>>> Security is the primary case where we have historically closed uAPI >>>>>>>>>> items. >>>>>>>>> >>>>>>>>> As this patch >>>>>>>>> >>>>>>>>> 1) Does not tackle GUP-fast >>>>>>>>> 2) Does not take care of !FOLL_LONGTERM >>>>>>>>> >>>>>>>>> I am not convinced by the security argument in regard to this patch. >>>>>>>>> >>>>>>>>> >>>>>>>>> If we want to sells this as a security thing, we have to block it >>>>>>>>> *completely* and then CC stable. >>>>>>>> >>>>>>>> Regarding GUP-fast, to fix the issue there as well, I guess we could do >>>>>>>> something similar as I did in gup_must_unshare(): >>>>>>>> >>>>>>>> If we're in GUP-fast (no VMA), and want to pin a !anon page writable, >>>>>>>> fallback to ordinary GUP. IOW, if we don't know, better be safe. >>>>>>> >>>>>>> How do we determine it's non-anon in the first place? The check is on the >>>>>>> VMA. We could do it by following page tables down to folio and checking >>>>>>> folio->mapping for PAGE_MAPPING_ANON I suppose? >>>>>> >>>>>> PageAnon(page) can be called from GUP-fast after grabbing a reference. >>>>>> See gup_must_unshare(). >>>>> >>>>> IIRC, PageHuge() can also be called from GUP-fast and could special-case >>>>> hugetlb eventually, as it's table while we hold a (temporary) reference. >>>>> Shmem might be not so easy ... >>>> >>>> page->mapping->a_ops should be enough to whitelist whatever fs you want. >>>> >>> >>> The issue is how to stabilize that from GUP-fast, such that we can safely >>> dereference the mapping. Any idea? >>> >>> At least for anon page I know that page->mapping only gets cleared when >>> freeing the page, and we don't dereference the mapping but only check a >>> single flag stored alongside the mapping. Therefore, PageAnon() is fine in >>> GUP-fast context. >> >> What codepath you are worry about that clears ->mapping on pages with >> non-zero refcount? >> >> I can only think of truncate (and punch hole). READ_ONCE(page->mapping) >> and fail GUP_fast if it is NULL should be fine, no? >> >> I guess we should consider if the inode can be freed from under us and the >> mapping pointer becomes dangling. But I think we should be fine here too: >> VMA pins inode and VMA cannot go away from under GUP. > > Can vma still go away if during a fast-gup? > So, after we grabbed the page and made sure the the PTE didn't change (IOW, the PTE was stable while we processed it), the page can get unmapped (but not freed, because we hold a reference) and the VMA can theoretically go away (and as far as I understand, nothing stops the file from getting deleted, truncated etc). So we might be looking at folio->mapping and the VMA is no longer there. Maybe even the file is no longer there. -- Thanks, David / dhildenb