Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp5744403iob; Tue, 10 May 2022 02:38:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvXnLAF3bfbzZyshzzpjkoUEU5MX8PtmOjeuTrn1I90cahzH52ZfUfAWxgpjOz5sP4kW91 X-Received: by 2002:a17:90a:343:b0:1cb:234a:a975 with SMTP id 3-20020a17090a034300b001cb234aa975mr30713835pjf.83.1652175539601; Tue, 10 May 2022 02:38:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652175539; cv=none; d=google.com; s=arc-20160816; b=Al97fUxbzTZ419quL2GPOhkH96S3jQj5/eYLhKcL5jpgyybGJKQ0GqTgoOnb67g4oG n/nSpfGu1lEiOSkRZGjGwowoWJO3AWhGEkJiLqIDUt8jhc4YajprP22e5LKURxGKv0HW P1joRtvZc4tzw6lfOVCngILlDZQXt/iKCT8glij74duBIFCjjBOVniwhyKR2MKNj39TA vTVCx8mvF73fityveCYKiaIf6liVovEmUVRRUbB6ETU6CdA+SsaoAoilXP8tvBVv/mMC gtnaWnnWSl9Iv8Dpvb0q+ewk5rXXBwJ8I4xNJ6+tvs87zPHjJzDvmTMpXh4H+KhYps4k m8AQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=WrMoJrxrZGg5PVkbTdEvYJDSvLwXAN41Hn/TLknZuGE=; b=gmDm61QsYYnUHJ+NDbdf9GdmmKE8XL+cHCRlblMqMWOoiXrw3DLZN9tzVRpr038tNh PcdmiLK4DC+0nbATdnQ70KAkkjRuI4KMzDlCIC/P/wrDCfJQwy03fESG/9smpZBlJtyq fHc9S1guwn9A9U1t9b29860CwCCvW4+u9THQPnXqNksYWOWWg5ms/dS5ZrRHgN9LivFJ nZtI+VOboIoII2bhJ5qw8CEHDP/36oKkrivWEhs+SianI/J+PIwP1vTcRCHFTaW4T+zg CaZtzZ+u3yjZpSj+IVkwbrSsWskJJQh2BDgH7yrVIQMbCIAuo70zAqZDntJxpj4hum10 qbjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=J2x+ncNk; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t70-20020a638149000000b003c5a1baf6acsi17088314pgd.503.2022.05.10.02.38.44; Tue, 10 May 2022 02:38:59 -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=@gmail.com header.s=20210112 header.b=J2x+ncNk; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238157AbiEJIrZ (ORCPT + 99 others); Tue, 10 May 2022 04:47:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232706AbiEJIrW (ORCPT ); Tue, 10 May 2022 04:47:22 -0400 Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78E482A2F65; Tue, 10 May 2022 01:43:25 -0700 (PDT) Received: by mail-qv1-xf31.google.com with SMTP id js14so12163205qvb.12; Tue, 10 May 2022 01:43:25 -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=WrMoJrxrZGg5PVkbTdEvYJDSvLwXAN41Hn/TLknZuGE=; b=J2x+ncNkddklUrmeq5tRp8U99jTNUPrG3A/KrP2Pz6lNbj24D0eaDOUqpYMIrBAJYF KJjr/85bxa7w4f6dYtnPmHGCXjVmevEtrn174z28RYmA0kD9byvcirj72UcdXA2+2f5A VIYXnsyklWx6D/43iYqGf78T0L7IGOo/2tcXvkCNkBoYylIqv6xIPnLXB/TcwyuHfe/8 0++nI8eUIWLgy00E7bxlz93Tm8kaceu162c1Kf/0kwuX+0Du4vK13kQns6+Hcc3Ppx3i SJDstXgFYedwOB5TW6FCsZLaYePcn5qWdE8YR0f68dhafqVU8aTPRflNCQcifoX7KyXu VnOg== 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=WrMoJrxrZGg5PVkbTdEvYJDSvLwXAN41Hn/TLknZuGE=; b=Gc0g26z+aboW58atXWm3gwSOZ+8/Vo5Q4rOXC5LH+Es1onfSsuz1nc+8MBNVxJp18Z LflMTO3/GB50YgumCQoM+4+rxMjRRmH4PJk8YPweX7oa3IS4k27DIA2NBAJ47hcvXjvt 7OJRxs+UD+CmeDbIwj0C96x7nlVkb0Epi+EVYGg2VwQgdJu7g8AHp5gSDO3V0BC13+eH drIbJpvRNWA18fVOpi5po9Ljmwx2jX0sqyG3Y430jC18UmCvScasXsr7oXNxTeOdfu2B spd/HkcLfEnirNN4KajoxuFaaygqQ4O9RXBUBs8ph1PA3fV3ktF6nLGtvB59T6uH8BQr HxyQ== X-Gm-Message-State: AOAM530NhCrMWQ+1evVhzcTwdjXlbwzIC1f+DNPEgDKiPdTIHirt6cRN FUFoTTeOGH7sTh2+01GhO1Di8MRE4L09MDw5f94= X-Received: by 2002:a05:6214:2409:b0:432:bf34:362f with SMTP id fv9-20020a056214240900b00432bf34362fmr16976378qvb.66.1652172204571; Tue, 10 May 2022 01:43:24 -0700 (PDT) MIME-Version: 1.0 References: <20220420140633.753772-1-stefanb@linux.ibm.com> <20220420140633.753772-2-stefanb@linux.ibm.com> <20220509195414.GA30894@mail.hallyn.com> <20220509203618.GA31408@mail.hallyn.com> In-Reply-To: <20220509203618.GA31408@mail.hallyn.com> From: Amir Goldstein Date: Tue, 10 May 2022 11:43:13 +0300 Message-ID: Subject: Re: [PATCH v12 01/26] securityfs: rework dentry creation To: "Serge E. Hallyn" Cc: Stefan Berger , linux-integrity , Mimi Zohar , Christian Brauner , containers@lists.linux.dev, Dmitry Kasatkin , "Eric W. Biederman" , krzysztof.struczynski@huawei.com, roberto.sassu@huawei.com, mpeters@redhat.com, lhinds@redhat.com, lsturman@redhat.com, puiterwi@redhat.com, jejb@linux.ibm.com, jamjoom@us.ibm.com, linux-kernel , Paul Moore , Richard Guy Briggs , LSM List , James Morris , jpenumak@redhat.com, Christian Brauner , John Johansen , Matthew Garrett , Micah Morton , Kentaro Takeda , Jarkko Sakkinen Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Mon, May 9, 2022 at 11:36 PM Serge E. Hallyn wrote: > > On Mon, May 09, 2022 at 02:54:14PM -0500, Serge E. Hallyn wrote: > > On Wed, Apr 20, 2022 at 10:06:08AM -0400, Stefan Berger wrote: > > > From: Christian Brauner > > > > > > When securityfs creates a new file or directory via > > > securityfs_create_dentry() it will take an additional reference on the > > > newly created dentry after it has attached the new inode to the new > > > dentry and added it to the hashqueues. > > > If we contrast this with debugfs which has the same underlying logic as > > > securityfs. It uses a similar pairing as securityfs. Where securityfs > > > has the securityfs_create_dentry() and securityfs_remove() pairing, > > > debugfs has the __debugfs_create_file() and debugfs_remove() pairing. > > > > > > In contrast to securityfs, debugfs doesn't take an additional reference > > > on the newly created dentry in __debugfs_create_file() which would need > > > to be put in debugfs_remove(). > > > > > > The additional dget() isn't a problem per se. In the current > > > implementation of securityfs each created dentry pins the filesystem via > > > > Is 'via' an extra word here or is there a missing word? > > > > I'll delay the rest of my response as the missing word may answer my > > remaining question :) > > > > > until it is removed. Since it is virtually guaranteed that there is at > > > least one user of securityfs that has created dentries the initial > > > securityfs mount cannot go away until all dentries have been removed. > > > > > > Since most of the users of the initial securityfs mount don't go away > > > until the system is shutdown the initial securityfs won't go away when > > > unmounted. Instead a mount will usually surface the same superblock as > > > before. The additional dget() doesn't matter in this scenario since it > > > is required that all dentries have been cleaned up by the respective > > > users before the superblock can be destroyed, i.e. superblock shutdown > > > is tied to the lifetime of the associated dentries. > > > > > > However, in order to support ima namespaces we need to extend securityfs > > > to support being mounted outside of the initial user namespace. For > > > namespaced users the pinning logic doesn't make sense. Whereas in the > > > initial namespace the securityfs instance and the associated data > > > structures of its users can't go away for reason explained earlier users > > > of non-initial securityfs instances do go away when the last users of > > > the namespace are gone. > > > > > > So for those users we neither want to duplicate the pinning logic nor > > > make the global securityfs instance display different information based > > > on the namespace. Both options would be really messy and hacky. > > > > > > Instead we will simply give each namespace its own securityfs instance > > > similar to how each ipc namespace has its own mqueue instance and all > > > entries in there are cleaned up on umount or when the last user of the > > > associated namespace is gone. > > > > > > This means that the superblock's lifetime isn't tied to the dentries. > > > Instead the last umount, without any fds kept open, will trigger a clean > > > shutdown. But now the additional dget() gets in the way. Instead of > > > being able to rely on the generic superblock shutdown logic we would > > > need to drop the additional dentry reference during superblock shutdown > > > for all associated users. That would force the use of a generic > > > coordination mechanism for current and future users of securityfs which > > > is unnecessary. Simply remove the additional dget() in > > > securityfs_dentry_create(). > > > > > > In securityfs_remove() we will call dget() to take an additional > > > reference on the dentry about to be removed. After simple_unlink() or > > > simple_rmdir() have dropped the dentry refcount we can call d_delete() > > > which will either turn the dentry into negative dentry if our earlier > > > dget() is the only reference to the dentry, i.e. it has no other users, > > > or remove it from the hashqueues in case there are additional users. > > > The first case (turn negative) cannot happen because the function is entered with at least 1 refcount and increments it by 1. So you can follow commit 46c46f8df9aa ("devpts_pty_kill(): don't bother with d_delete()") and use d_drop() instead. > > > All of these changes should not have any effect on the userspace > > > semantics of the initial securityfs mount. > > > > > > Signed-off-by: Christian Brauner > > > Cc: John Johansen > > > Cc: Matthew Garrett > > > Cc: Micah Morton > > > Cc: Kentaro Takeda > > > Cc: James Morris > > > Cc: Jarkko Sakkinen > > > Signed-off-by: Stefan Berger > > > Reviewed-by: Mimi Zohar > > > --- > > > security/inode.c | 3 ++- > > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > > > diff --git a/security/inode.c b/security/inode.c > > > index 6c326939750d..13e6780c4444 100644 > > > --- a/security/inode.c > > > +++ b/security/inode.c > > > @@ -159,7 +159,6 @@ static struct dentry *securityfs_create_dentry(const char *name, umode_t mode, > > > inode->i_fop = fops; > > > } > > > d_instantiate(dentry, inode); > > > - dget(dentry); > > > inode_unlock(dir); > > > return dentry; > > > > > > @@ -302,10 +301,12 @@ void securityfs_remove(struct dentry *dentry) > > > dir = d_inode(dentry->d_parent); > > > inode_lock(dir); > > > if (simple_positive(dentry)) { > > > + dget(dentry); > > > if (d_is_dir(dentry)) > > > simple_rmdir(dir, dentry); > > Hm, so I realize your patch isn't introducing this, but is the > fact that we ignore the possible -ENOTEMPTY return value of > simple_rmdir() not a problem? As long as we are using debugfs as a reference code, wouldn't securityfs need to use simple_recursive_removal()? Can we guaranty that modules always cleanup all entries in correct order? > > > > else > > > simple_unlink(dir, dentry); > > > + d_delete(dentry); > d_drop() (see comment above) > I'm mostly trying to convince myself that the fact that there is not > a matching dput being removed (to match the dget being removed above) > is ok. I do think it is, but that belief seems to dictate that right > now dentries must never be being released. > > Otherwise, it seems like there must be cases where the next dput could > be called on a dentry that has been freed. > > > > dput(dentry); Huh? There must be a ref to dentry when entering this function and there is dget() added above so balance is not lost. Thanks, Amir.