Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2893498rdb; Wed, 4 Oct 2023 15:12:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEdi4KGFwDJuCnkh0Dg7QIqjyZ19X2G4YxbrO6yDQKJ+a+4v3n3yms6xLGspvNpVRs/BOso X-Received: by 2002:a05:6a21:7983:b0:15d:7767:6553 with SMTP id bh3-20020a056a21798300b0015d77676553mr3385515pzc.35.1696457532494; Wed, 04 Oct 2023 15:12:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696457532; cv=none; d=google.com; s=arc-20160816; b=akDJrxMjZxOH8G6tJuGQLW6kbofXPBv9Z6dY6P17qW4BCbURW+IROXNGdaqdhDBsaA tMl+NOnsoEzf7ubqIWKMdAySP7XCxdaVpkEl3brHB1JkMHPEtZ7sO6UAeeaj0HdZJiIP bLGlrOAbWsxy10R7/E4Q/+31linA1vUABZFbV2MLFj3RIRwqSd+Cod5wJJ8zadbnAGJG jZpVFqY1Ar7DEF1Ek3/5gi2bzr7itltgiySjQyLw2xXkUdMmFTuBGhyrLGbRQqz2Wdlz /e1kMe73CJXv4SmgWAF+Y3QKx5ucNxfWyDtM9mg1QQpX4pigAxNzBS5HGnWZO3oSag7C FAFA== 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 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id z30-20020a056a001d9e00b0068e3fe64a03si26364pfw.308.2023.10.04.15.12.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 15:12:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; 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 2620:137:e000::3:7 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 2214882CB52E; Wed, 4 Oct 2023 15:11:39 -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 S236881AbjJDWLV (ORCPT + 99 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:39 -0700 (PDT) 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, -- 2.25.1