From patchwork Wed Oct 4 22:09:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fan Wu X-Patchwork-Id: 148600 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:254a:b0:403:3b70:6f57 with SMTP id hf10csp432051vqb; Wed, 4 Oct 2023 15:12:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG25OEtxdUbh1IG1+DL9p6ysDEQrAsrbZogvbKUVKBW0RvRioEotdo2u0p1ru8vWegqLHXn X-Received: by 2002:a05:6a20:7485:b0:15e:e0fd:98e7 with SMTP id p5-20020a056a20748500b0015ee0fd98e7mr3940441pzd.20.1696457533553; Wed, 04 Oct 2023 15:12:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696457533; cv=none; d=google.com; s=arc-20160816; b=sJ2JAWWA/rqIvJz4aWexCbw6AgrdpXBp5cU4J3t1NRLXoWeMV7ZnwNDUo5haW41BS5 FH2EfYnJDwHJx462YK2Lzw8+fNsC1XoP2fkOx5tqmeRz3UCh4gMJdhJdJDtKB7uQp05X QW0br+8mRXGzp4UUqRbaRmOX5JXNt+XogMJBBZtTUj3tYnalz4OCk5npd2e1XZVdflIt OLBfqnQ7gE+PNliEEQw0Fdb9Fl42DzVVp2eaJVKztjzqIocqORCqHtI5gBLoDvbUD6c9 3uTNFM+yTZCL+gNIMWsocb3AoKqPPz7CF8iTofxUWWU8o3QItDZ3nbHGgwrDwWUcKJ8E O8sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature:dkim-filter; bh=ly0VW0W3rTiQjAGTfCHxetMYPAQVrGT5zbQKAgFSTXI=; fh=PhRfb9k/VCaD3nhvPFELAdaSNFsaoc654VPOTTQHJlA=; b=izoJCZmV2psu60WkgXeXdwU7B/kWT4upSSdpOgdPvoVUfhstw+67wEXK/InAiINtlL LkREKyF8mNLb6wIrPJFijnnxLjZxIKJnhMJCQ74XjjwkYiCCjzHwR4rpBdb7fDr7jWkF jXhqkavwMO9Z3RgWMhGN10zVExjXZKPqaKGGvqzJdV0dU33Tn6uO4r4+whTllUkLdvr1 EUc3zq/EVFDo9E9dlobnP1F5cQmSIbDmNgQJpt8KT02XlDBGLSX8I0DyH9EghbKG4D+R QVzs3hKXBJW1Z3K7Gih6YdsimUyZfcksCBGE+g2CzbQ/xK5lfgyvrY20/BpXWUCXpdbj JMxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=cBYiOaUI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id y18-20020a17090ad71200b00271a1895130si222029pju.119.2023.10.04.15.12.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 15:12:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=cBYiOaUI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id AAD5E8098191; Wed, 4 Oct 2023 15:11:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239644AbjJDWLV (ORCPT + 19 others); Wed, 4 Oct 2023 18:11:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232622AbjJDWJ4 (ORCPT ); Wed, 4 Oct 2023 18:09:56 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 01DFBF4; Wed, 4 Oct 2023 15:09:52 -0700 (PDT) Received: by linux.microsoft.com (Postfix, from userid 1052) id 7C6A520B74CC; Wed, 4 Oct 2023 15:09:50 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 7C6A520B74CC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1696457390; bh=ly0VW0W3rTiQjAGTfCHxetMYPAQVrGT5zbQKAgFSTXI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cBYiOaUICog7Az0HJsLRD8/Yq/9aOQ7hn0Afs19hI3GhUYJMHH9a7piDe1sstNx9e aJRZepSsSwcuCBNEi3vg5j7bYMLa81TmDFgfzWqFId2NPkG6eL4qqGlipaLKXBqj+u iy4kDzBYlK6CH35gdf8s/5nGwShk2fBHuYK2y1oE= From: Fan Wu To: corbet@lwn.net, zohar@linux.ibm.com, jmorris@namei.org, serge@hallyn.com, tytso@mit.edu, ebiggers@kernel.org, axboe@kernel.dk, agk@redhat.com, snitzer@kernel.org, eparis@redhat.com, paul@paul-moore.com Cc: linux-doc@vger.kernel.org, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fscrypt@vger.kernel.org, linux-block@vger.kernel.org, dm-devel@redhat.com, audit@vger.kernel.org, roberto.sassu@huawei.com, linux-kernel@vger.kernel.org, Fan Wu Subject: [RFC PATCH v11 06/19] security: add new securityfs delete function Date: Wed, 4 Oct 2023 15:09:33 -0700 Message-Id: <1696457386-3010-7-git-send-email-wufan@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1696457386-3010-1-git-send-email-wufan@linux.microsoft.com> References: <1696457386-3010-1-git-send-email-wufan@linux.microsoft.com> X-Spam-Status: No, score=-17.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH,SPF_HELO_PASS,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 04 Oct 2023 15:11:41 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778864654959663055 X-GMAIL-MSGID: 1778864654959663055 When deleting a directory in the security file system, the existing securityfs_remove requires the directory to be empty, otherwise it will do nothing. This leads to a potential risk that the security file system might be in an unclean state when the intentded deletion did not happen. This commit introduces a new function securityfs_recursive_remove to recursively delete a directory without leaving an unclean state. Co-developed-by: Christian Brauner (Microsoft) Signed-off-by: Fan Wu --- v1-v8: + Not present v9: + Introduced v10: + No changes v11: + Fix code style issues --- include/linux/security.h | 1 + security/inode.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/linux/security.h b/include/linux/security.h index 5f16eecde00b..8fa3bab9fa10 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -1985,6 +1985,7 @@ struct dentry *securityfs_create_symlink(const char *name, const char *target, const struct inode_operations *iops); extern void securityfs_remove(struct dentry *dentry); +extern void securityfs_recursive_remove(struct dentry *dentry); #else /* CONFIG_SECURITYFS */ diff --git a/security/inode.c b/security/inode.c index 3aa75fffa8c9..e0585c2946c5 100644 --- a/security/inode.c +++ b/security/inode.c @@ -313,6 +313,31 @@ void securityfs_remove(struct dentry *dentry) } EXPORT_SYMBOL_GPL(securityfs_remove); +static void remove_one(struct dentry *victim) +{ + simple_release_fs(&mount, &mount_count); +} + +/** + * securityfs_recursive_remove - recursively removes a file or directory + * + * @dentry: a pointer to a the dentry of the file or directory to be removed. + * + * This function recursively removes a file or directory in securityfs that was + * previously created with a call to another securityfs function (like + * securityfs_create_file() or variants thereof.) + */ +void securityfs_recursive_remove(struct dentry *dentry) +{ + if (IS_ERR_OR_NULL(dentry)) + return; + + simple_pin_fs(&fs_type, &mount, &mount_count); + simple_recursive_removal(dentry, remove_one); + simple_release_fs(&mount, &mount_count); +} +EXPORT_SYMBOL_GPL(securityfs_recursive_remove); + #ifdef CONFIG_SECURITY static struct dentry *lsm_dentry; static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count,