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 F2FFEC433EF for ; Tue, 12 Jul 2022 17:40:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E0999400BF; Tue, 12 Jul 2022 13:40:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8906C940063; Tue, 12 Jul 2022 13:40:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77F519400BF; Tue, 12 Jul 2022 13:40:25 -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 6903A940063 for ; Tue, 12 Jul 2022 13:40:25 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 32186120957 for ; Tue, 12 Jul 2022 17:40:25 +0000 (UTC) X-FDA: 79679162010.12.46BB3A1 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf31.hostedemail.com (Postfix) with ESMTP id C783620068 for ; Tue, 12 Jul 2022 17:40:24 +0000 (UTC) Received: by mail-pf1-f177.google.com with SMTP id g126so8085339pfb.3 for ; Tue, 12 Jul 2022 10:40:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Sp14BDiJ3o3yBys1mNzUPG3x8p1qawMTa3Q7bZ9V7ZQ=; b=JY1A1qPuUfcobXSu0qwLPgQ3ReI/jpVuV88VYHV8/If49pMd0RDr4gujE+Epi0V+pt MR1Md8Mt4od2yWXymnRQIs2rnbJCKyK8h2ZBriJa/VlBXDJskI7u6X5UZPA9jrykbCAc S0fdmGAzVueQoSYSAFvtwMEc7tq9oRFG7kS+9LFRFipZO2kcbd/AYV7P0YezpS0uLFUr VMV68lkSOkDnLV/SF7xX3hNXmfuO+65NJTWUvybohd95W6/Z0wBiFIDZyA6dj+R3hlL5 7vLbMup2inmleDBdFQt/jvIcXjpUtwFUUcWiKeLXMAbA/oa+MFaRtuZJydb7wkkap/w+ 5D9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Sp14BDiJ3o3yBys1mNzUPG3x8p1qawMTa3Q7bZ9V7ZQ=; b=CiQqvY6qVVoPXNmU/nOU9WdiSzWXuFyL7OssL95z75o4YBca6gqXRZEtWcUgD3gwCR AXmL+kpY3pUfamXpXc2GlbB40KnwN1El4YCb1FMoZqQ+fY7FIoGOx+inzmTziPZp8SOr 0gZxbhx+w8hZD+S07kIyWPpBXAUTpYB6qRA8VC0pcjcVX6pzKnxSBiibj5lFZmr8XxAi rmnkN8bx6yC4mAFehc+VV/69C6s5LNfb051ofqE2fGoBzGYN5hPNMnsOGDpBPaihrtX1 s8JVrRWEomPXgDg9heKjfJ7QL1nIuW5GLpd/mLH6k06t9buBrzEzwrGx24QPFX4G4sim nI+g== X-Gm-Message-State: AJIora9Y10CC44eEEUFyaCE03gEi2uUgVrFVzSkZOtQD31lhM7Z1kKHp mngVE93U9qdcxe8k2qIKgaRNFT1ZHcWIBiXuNkU= X-Google-Smtp-Source: AGRyM1t5I4ckAblDJVCQOsZeDEea9rEVIbFiFh5Nju1djPkIcFOozw7FE7mOLqeUdQulEtdeUh/OnxcxdoBGurVJuqY= X-Received: by 2002:a63:3cd:0:b0:415:f76d:fb4 with SMTP id 196-20020a6303cd000000b00415f76d0fb4mr10978219pgd.587.1657647623722; Tue, 12 Jul 2022 10:40:23 -0700 (PDT) MIME-Version: 1.0 References: <20220707165650.248088-1-rppt@kernel.org> In-Reply-To: From: Yang Shi Date: Tue, 12 Jul 2022 10:40:11 -0700 Message-ID: Subject: Re: [PATCH v2] secretmem: fix unhandled fault in truncate To: Mike Rapoport , Jan Kara Cc: "Darrick J. Wong" , Andrew Morton , Axel Rasmussen , Eric Biggers , Hillf Danton , Matthew Wilcox , Mike Rapoport , Linux FS-devel Mailing List , Linux MM , syzkaller-bugs , syzbot+9bd2b7adbd34b30b87e4@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=JY1A1qPu; spf=pass (imf31.hostedemail.com: domain of shy828301@gmail.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657647624; a=rsa-sha256; cv=none; b=cNN1U7fj+lHfdffW5xmeDZDSMeyeSpwzkitXIYvj0EJuReb/wYWeQ9TyF0mKlxMZTQ8pUY RWbIhaJ5TpiRHx+W2DXIjf+dHnwRACaz0y4xpMVYo6VshdMV/pq8kY5gMzSRQA2AozqF0a mTUCtDtFlJf5rZVFXT2qcMyTf3Yc9vI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657647624; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Sp14BDiJ3o3yBys1mNzUPG3x8p1qawMTa3Q7bZ9V7ZQ=; b=A4zV94QQtYlqA4L7dpEUja4wf/IX2g3p5j8fhmJ4rnVvhYDGDad+1HOKvnhk/b1vGm9dDl FtUVKOAiQ2qVwfruUK4U8eSYoMNni6y+oWwVgl55fWUYBEKbOrkGDaGKlgiMXAIqhFRVwx Pvwb9EcAvl1tHrbqzf83BfznXPeGSYY= Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=JY1A1qPu; spf=pass (imf31.hostedemail.com: domain of shy828301@gmail.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam09 X-Stat-Signature: ux7ir38bsprzo5d9wonyf93gizm8ypp5 X-Rspamd-Queue-Id: C783620068 X-HE-Tag: 1657647624-744183 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 Fri, Jul 8, 2022 at 1:29 AM Mike Rapoport wrote: > > On Thu, Jul 07, 2022 at 03:09:32PM -0700, Yang Shi wrote: > > On Thu, Jul 7, 2022 at 1:55 PM Darrick J. Wong wrote: > > > > > > On Thu, Jul 07, 2022 at 10:48:00AM -0700, Yang Shi wrote: > > > > On Thu, Jul 7, 2022 at 9:57 AM Mike Rapoport wrote: > > > > > > > > > > Eric Biggers suggested that this happens when > > > > > secretmem_setattr()->simple_setattr() races with secretmem_fault() so > > > > > that a page that is faulted in by secretmem_fault() (and thus removed > > > > > from the direct map) is zeroed by inode truncation right afterwards. > > > > > > > > > > Since do_truncate() takes inode_lock(), adding inode_lock_shared() to > > > > > secretmem_fault() prevents the race. > > > > > > > > Should invalidate_lock be used to serialize between page fault and truncate? > > > > > > I would have thought so, given Documentation/filesystems/locking.rst: > > > > > > "->fault() is called when a previously not present pte is about to be > > > faulted in. The filesystem must find and return the page associated with > > > the passed in "pgoff" in the vm_fault structure. If it is possible that > > > the page may be truncated and/or invalidated, then the filesystem must > > > lock invalidate_lock, then ensure the page is not already truncated > > > (invalidate_lock will block subsequent truncate), and then return with > > > VM_FAULT_LOCKED, and the page locked. The VM will unlock the page." > > > > > > IIRC page faults aren't supposed to take i_rwsem because the fault could > > > be in response to someone mmaping a file into memory and then write()ing > > > to the same file using the mmapped region. The write() takes > > > inode_lock and faults on the buffer, so the fault cannot take inode_lock > > > again. > > > > Do you mean writing from one part of the file to the other part of the > > file so the "from" buffer used by copy_from_user() is part of the > > mmaped region? > > > > Another possible deadlock issue by using inode_lock in page faults is > > mmap_lock is acquired before inode_lock, but write may acquire > > inode_lock before mmap_lock, it is a AB-BA lock pattern, but it should > > not cause real deadlock since mmap_lock is not exclusive for page > > faults. But such pattern should be avoided IMHO. > > > > > That said... I don't think memfd_secret files /can/ be written to? > > memfd_secret files cannot be written to, they can only be mmap()ed. > Synchronization is only required between > do_truncate()->...->simple_setatt() and secretmem->fault() and I don't see > how that can deadlock. Sure, there is no deadlock. > > I'm not an fs expert though, so if you think that invalidate_lock() is > safer, I don't mind s/inode_lock/invalidate_lock/ in the patch. IIUC invalidate_lock should be preferred per the filesystem's locking document. And I found Jan Kara's email of the invalidate_lock patchset, please refer to https://lore.kernel.org/linux-mm/20210715133202.5975-1-jack@suse.cz/. > > > > Hard to say, since I can't find a manpage describing what that syscall > > > does. > > Right, I don't see it's published :-/ > > There is a groff version: > https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/man2/memfd_secret.2 > > > > --D > > > > > > > > > > > > > > > > > Reported-by: syzbot+9bd2b7adbd34b30b87e4@syzkaller.appspotmail.com > > > > > Suggested-by: Eric Biggers > > > > > Signed-off-by: Mike Rapoport > > > > > --- > > > > > > > > > > v2: use inode_lock_shared() rather than add a new rw_sem to secretmem > > > > > > > > > > Axel, I didn't add your Reviewed-by because v2 is quite different. > > > > > > > > > > mm/secretmem.c | 21 ++++++++++++++++----- > > > > > 1 file changed, 16 insertions(+), 5 deletions(-) > > > > > > > > > > diff --git a/mm/secretmem.c b/mm/secretmem.c > > > > > index 206ed6b40c1d..a4fabf705e4f 100644 > > > > > --- a/mm/secretmem.c > > > > > +++ b/mm/secretmem.c > > > > > @@ -55,22 +55,28 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) > > > > > gfp_t gfp = vmf->gfp_mask; > > > > > unsigned long addr; > > > > > struct page *page; > > > > > + vm_fault_t ret; > > > > > int err; > > > > > > > > > > if (((loff_t)vmf->pgoff << PAGE_SHIFT) >= i_size_read(inode)) > > > > > return vmf_error(-EINVAL); > > > > > > > > > > + inode_lock_shared(inode); > > > > > + > > > > > retry: > > > > > page = find_lock_page(mapping, offset); > > > > > if (!page) { > > > > > page = alloc_page(gfp | __GFP_ZERO); > > > > > - if (!page) > > > > > - return VM_FAULT_OOM; > > > > > + if (!page) { > > > > > + ret = VM_FAULT_OOM; > > > > > + goto out; > > > > > + } > > > > > > > > > > err = set_direct_map_invalid_noflush(page); > > > > > if (err) { > > > > > put_page(page); > > > > > - return vmf_error(err); > > > > > + ret = vmf_error(err); > > > > > + goto out; > > > > > } > > > > > > > > > > __SetPageUptodate(page); > > > > > @@ -86,7 +92,8 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) > > > > > if (err == -EEXIST) > > > > > goto retry; > > > > > > > > > > - return vmf_error(err); > > > > > + ret = vmf_error(err); > > > > > + goto out; > > > > > } > > > > > > > > > > addr = (unsigned long)page_address(page); > > > > > @@ -94,7 +101,11 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) > > > > > } > > > > > > > > > > vmf->page = page; > > > > > - return VM_FAULT_LOCKED; > > > > > + ret = VM_FAULT_LOCKED; > > > > > + > > > > > +out: > > > > > + inode_unlock_shared(inode); > > > > > + return ret; > > > > > } > > > > > > > > > > static const struct vm_operations_struct secretmem_vm_ops = { > > > > > > > > > > base-commit: 03c765b0e3b4cb5063276b086c76f7a612856a9a > > > > > -- > > > > > 2.34.1 > > > > > > > > > > > > -- > Sincerely yours, > Mike.