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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DB1D4C77B60 for ; Fri, 28 Apr 2023 17:03:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346299AbjD1RD2 (ORCPT ); Fri, 28 Apr 2023 13:03:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229697AbjD1RDZ (ORCPT ); Fri, 28 Apr 2023 13:03:25 -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 68960659F for ; Fri, 28 Apr 2023 10:02:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682701348; 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=zN+hlinWDo7HPckU96nAsQkShApM93gzKFjoIDnOxkE=; b=NojjS2VEjlGzwAbO565WIMKXXrJfXYhvW1BFZQHF3rqYbbxaEB+10Gucls1s1gwn2gorPz RY5j7RKcwKDR3nojuoNBCq6dTvOdpWOF0TdlIRsouRRy7WR8YNs4neXYIbXCHoFlHVq6Qp RM36GJiHU6DHn0CrfWn0MT8KYDbDkOI= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-635-OV4uSdz8PeeFz7Ac3G8Vnw-1; Fri, 28 Apr 2023 13:02:27 -0400 X-MC-Unique: OV4uSdz8PeeFz7Ac3G8Vnw-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-2f2981b8364so5739398f8f.1 for ; Fri, 28 Apr 2023 10:02:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682701346; x=1685293346; 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:subject:date:message-id :reply-to; bh=zN+hlinWDo7HPckU96nAsQkShApM93gzKFjoIDnOxkE=; b=UfCs8qXGorkalqCFKU4tBNXpwC3BEQSWEt0nQIXlbE0ogXZ7SSIeFq91eDfrs3Bla5 l9zG9N800iH565uhIouS9rBZxEmtAwrC7x7Iow1vzHJDqkllWx/+/ueZ2QhZEncbTpeV kcPhaG1DGDdQvp/sQMNPBw/3bbM2C6XtXRkqjBs2cItXkLUzz1mrrbGkyCZBfpAXpOF0 sos2fr9EY1AeunjBkoJn6WjNn6hFYEf93zwXk+mYDnnPCQY1KxtM1Te1UbzryQt9NJd3 lqruWS2C1Y+dX0kjt/qujSG9/9CD0KWsif7VNHiAyVlSFobd6fxA23eTtmZ3Gn2LhIuu oB6g== X-Gm-Message-State: AC+VfDxQP+RpNy7TdC1PP7c+S1RQ7aYYSfC9UuCGJH3CXE2IWDz5INAJ E+gSB7rQ6J2FzVfv+BCR/51LfHq4MPAMmVfDoiZKeng4MqlrlzAJm6CYxJyr0+OXpfGsTZbvfNv 3QchoJoBIBUZr9tB1M1sjXZygMab1rw== X-Received: by 2002:a5d:40cd:0:b0:2ff:3a63:ed61 with SMTP id b13-20020a5d40cd000000b002ff3a63ed61mr4380616wrq.13.1682701345893; Fri, 28 Apr 2023 10:02:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5IGpmPcW+sW5hDbaIE0RZbDfpW/KW7f3SgvaJbedb3u6P7PhLZRwvYbDxXsxQPtSCsSBa1uw== X-Received: by 2002:a5d:40cd:0:b0:2ff:3a63:ed61 with SMTP id b13-20020a5d40cd000000b002ff3a63ed61mr4380577wrq.13.1682701345525; Fri, 28 Apr 2023 10:02:25 -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 d13-20020adfe88d000000b002e55cc69169sm21510251wrm.38.2023.04.28.10.02.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Apr 2023 10:02:24 -0700 (PDT) Message-ID: <1039c830-acec-d99b-b315-c2a6e26c34ca@redhat.com> Date: Fri, 28 Apr 2023 19:02:22 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v5] mm/gup: disallow GUP writing to file-backed mappings by default Content-Language: en-US To: "Kirill A . Shutemov" Cc: Peter Xu , 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> <173337c0-14f4-3246-15ff-7fbf03861c94@redhat.com> <20230428165623.pqchgi5gtfhxd5b5@box.shutemov.name> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20230428165623.pqchgi5gtfhxd5b5@box.shutemov.name> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-perf-users@vger.kernel.org On 28.04.23 18:56, Kirill A . Shutemov wrote: > On Fri, Apr 28, 2023 at 06:51:46PM +0200, David Hildenbrand wrote: >> 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. > > No. VMA cannot get away before PTEs are unmapped and TLB is flushed. And > TLB flushing is serialized against GUP_fast(). The whole CONFIG_MMU_GATHER_RCU_TABLE_FREE handling makes the situation more complicated. -- Thanks, David / dhildenb