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 7D8C8C77B60 for ; Fri, 28 Apr 2023 17:29:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E0A336B0078; Fri, 28 Apr 2023 13:29:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DBA9A6B007B; Fri, 28 Apr 2023 13:29:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C5BC26B007D; Fri, 28 Apr 2023 13:29:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B788F6B0078 for ; Fri, 28 Apr 2023 13:29:44 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6EF3A14027E for ; Fri, 28 Apr 2023 17:29:44 +0000 (UTC) X-FDA: 80731487088.04.9EA69E9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 419AB80011 for ; Fri, 28 Apr 2023 17:29:41 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aCY2SKw3; spf=pass (imf02.hostedemail.com: domain of david@redhat.com designates 170.10.133.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=1682702981; 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=EIIwH7T02AjnTsmSMt7UxStS64AfVcp0/yWRRkLLOC0=; b=r3favLByzL5RExSJUhQzgak3QpwM0xJ7Ai6BfDLNhZJCx1NJAk60NMK74plKsY7YMbS7cP 6E5h5x5bceno4zq3P6K8ET65/7FYr1Cahmy/fKSG0HQiCwxm7n+QKEUjTB6ji3GQnECZcS 4qJcHUE43R9G9gvBkZoaBxrDmcXz2uM= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aCY2SKw3; spf=pass (imf02.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682702981; a=rsa-sha256; cv=none; b=PO+ABpYzDeJaQoeNtlJOHUglPcXLLq0ZYWRtXPmo5KFA/CTlrADpOvf8HwDlpWPNf0YFM+ xzvBfs7KYRRAaIoBjE9S7cxLE00y3nMH5R0GEX7CvYLAOGOkEwGAI/tGCoJgGJcJ/Y5jF4 oFAUAhRpGcPr63wME5ZYH5RJ8k7XwFY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682702980; 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=EIIwH7T02AjnTsmSMt7UxStS64AfVcp0/yWRRkLLOC0=; b=aCY2SKw31EMEilRsPZAXUqCUJizuAIExHYRqDwdp73WupGOKFqT058O60RoVBUo7CyvZyi 1fHs0SMSpMdYZNnA10YoAXjmn8XaD3dCANgw4tBNdaTD3s0YKkC/kL8KsMM48ZqjIbOqyo /etQjhIzCNdzKGVKI0mACkbIpTsY/Dk= 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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-630-q0gyTFmiOhuNDlkaoWbl2w-1; Fri, 28 Apr 2023 13:29:39 -0400 X-MC-Unique: q0gyTFmiOhuNDlkaoWbl2w-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-3f1793d6363so36683825e9.1 for ; Fri, 28 Apr 2023 10:29:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682702978; x=1685294978; 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=EIIwH7T02AjnTsmSMt7UxStS64AfVcp0/yWRRkLLOC0=; b=lBO0LQKJZ0a0fZgpcRjLe3hTNQzgMJi1QQU/H4Gp7PCmb56f4VFdKaOibaksWOHnGS kAeFuD95JRMvqB3pT1yxepd1yVoHBkekDfIDDkYUgNCuppiA9wxIV+rkqy8O1tF+GEYY 54wypvwwXzVj49H7pftoMEMnIQ6xaIB9kzDTJK5SOTIId0MWHRwgznp5tshIgJxwD/cm /fe+IG/wozXXJjRBLGQkyJmVihMb4BMEpFajxb0lhDEYyTMpLBp/XhGNFJVtp9pwt1xu 5pg4JqUYmPBH+2oDwIKblVQLoAjiJh8pXmFTok9TZDW8AC5cgJZTrc+KmZffTnxzR20O deRg== X-Gm-Message-State: AC+VfDzyVu15MsYhVXKUPtupFDeDi/DU1BE0fa4LLV5NNzxe6iXgA9WX 2ADfPScL4RZ4HWyXP3xJ73R65AHMt8MKNsKQpz+nUuZ2ZEHt4hxv0BDIComk3YnNTKY0K5iBAIC cUKvf/oAegC4= X-Received: by 2002:a7b:c015:0:b0:3f1:662a:93d0 with SMTP id c21-20020a7bc015000000b003f1662a93d0mr4869020wmb.15.1682702978226; Fri, 28 Apr 2023 10:29:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4h9gBJBIx2ZQJ+gm3oOoE/Rgj9KykEapquC53KsgU6XwysYEqKUCcp5L2ysNH2brGQpGxgmQ== X-Received: by 2002:a7b:c015:0:b0:3f1:662a:93d0 with SMTP id c21-20020a7bc015000000b003f1662a93d0mr4869002wmb.15.1682702977813; Fri, 28 Apr 2023 10:29:37 -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 q16-20020a7bce90000000b003f09d7b6e20sm25059003wmj.2.2023.04.28.10.29.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Apr 2023 10:29:37 -0700 (PDT) Message-ID: <7e096879-4578-36df-4809-3b04f4c20587@redhat.com> Date: Fri, 28 Apr 2023 19:29:35 +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 To: Lorenzo Stoakes Cc: Peter Xu , "Kirill A . Shutemov" , 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: <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> <40fc128f-1978-42db-b9c1-77ac3c2cebfe@lucifer.local> <3d7fcfab-e445-1dc7-f000-9fbe7bea04c0@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; format=flowed Content-Transfer-Encoding: 7bit X-Stat-Signature: h7868cai89jrrrhf44ung1p4e6pdxy81 X-Rspam-User: X-Rspamd-Queue-Id: 419AB80011 X-Rspamd-Server: rspam06 X-HE-Tag: 1682702981-853455 X-HE-Meta: U2FsdGVkX1+97WY5sH3WqjRdpsW2I0DMpyyplN1hB9yok45wGFiGs7dw2pzAnOy2xkIh6PB8jz2zkSHnBzr2sdZZXsgLvIiEG5mX9Qvdk1xL7FWUPWAkhSANzFUloJk4oc+oX85rN6HGF4y2DXdTKRqLty03F+OH2voGXLhzpSP6XGFUHnC9K7ULiUh8DKZihBnyHa1FW8roF5xETusUJ358BoXG+1r8jRdMxB9QN3MZJAei1NCBnpJdHMhRW7pj+X2DrSz4FttTaccp/c8ktSQqS2SA8WFkSNtiqPSi3HoBCUJo2ZD/dmCWHGdj9QHGZtidNpF6mx/BKdR72cLgoeMfQ4P6SKGsEAOuh+VAS4iq6odBktfr4F5As4ebrMmC1Ob1Eolr2JEOhgChzfnTv5iBCR3Zx/EI3w57tsUVW61qZXwn6IhZjEc/+Q1ziNTmhwoXD8rnCZqO0PI0tOKDE04nBUoCz9ucJrDniDYES1wtDQlkXNzTZmjcUvszzb2nn9RqcPONjZDvVwukcvsexVOIalzKtpzTiGNtzCWEWhpk3wzPf6xqorwzBaG6tiNpO9QqPyO92aUFdQIA5AwDMs9df/OsmGwh9UQbLKaK7nviIgQ4VubmQ47KgtGg+mR6j7MMQq08CJw8zBD7WFMtqnkriVxxoYDs3MqDYN2T2pc2n0Vv6pimWOaa17PSijC2/m8bPPJW52LvBZU8Uzhm97sBUGo+JRJu7LeUVGzg3PhkcqxSrM9JEOjIEhaCGkIp84Fg02Wfp8xV9cj9Ei7PUHZIlFxCHw6/A9OwcratdF80RtFDBOEzzXNkwiDvNEy4idjZFdMNRkQKBp0gbeHxnaA8piQsQPCX+XwSiglMw7H9kMnpd3qdZ4FCNlnvwT5+FfjkLqSg7MY7jiypXqK4ydTKfcHQRvt2rGAz/lLWmXCaa+3teHypnWjMG7+oiVdQOcX93Of8g0EuUoyaFSZ 3hwcWIuB YXyVYJIu/SspSJUpNIAbMWONH9VvW1vzYvPMbsPpgFetO0uTciqGjEsqRXiLfW4PIWClsHD599NE+66k4mqmkZMrzdh6mlKEFS5XROIRdsR00mYOdl7MfZVQCVyHiR4theOEpN21iVv8hwKH9GIFr8WoohvhdvGUw4PO/5SByDsF+tIoDDquxXt+d4oMJpIg7as49ZszYN2qT3rnqboW4GTtB/Y2dpUKeZs7nvJtKUQL6FbJ3PndBE3OKJdp7apcEAMUSejKEh+wApeh78BvByfjD/cXDSxKewJUy1ficcr+gpKfp/rnpJ6uO51xmVHeVzzavahoQKcuU0w7AXa/Npqc/s4yqqGyXTDeWuuDhMThcF5ljUskNL8NG2y63ZA8DTWcy2im4knnBAnv6jGitRTEltrTcZd2J4+Qj3HLVYv1RfPzHEJHJG/HSr//xnzKPXP1wZ2mNxgGci6I= 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 28.04.23 19:13, Lorenzo Stoakes wrote: > On Fri, Apr 28, 2023 at 07:05:38PM +0200, David Hildenbrand wrote: >> On 28.04.23 19:01, Lorenzo Stoakes 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. >>>> >>> >>> This shouldn't be an issue though right? Because after a pup call unlocks the >>> mmap_lock we're in the same situation anyway. GUP doesn't generally guarantee >>> the mapping remains valid, only pinning the underlying folio. >> >> Yes. But the issue here is rather dereferencing something that has already >> been freed, eventually leading to undefined behavior. >> > > Is that an issue with interrupts disabled though? Will block page tables being > removed and as Kirill says (sorry I maybe misinterpreted you) we should be ok. Let's rule out page table freeing. If our VMA only spans a single page and falls into the same PMD as another VMA, an munmap() would not even free a single page table. However, if unmapping a page (flushing the TLB) would imply an IPI as Kirill said, we'd be fine. I recall that that's not the case for all architectures, but I might be just wrong. ... and now I'll stop reading mails until Tuesday :) -- Thanks, David / dhildenb