Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp19784981rwd; Wed, 28 Jun 2023 14:22:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6EezElft3aYDtCpBqTfl0zsHELDNAiXW32SfLBYcCrs0uFQ9L0kIV97aGhELy3iu0GAz0F X-Received: by 2002:a05:6a20:748d:b0:126:92de:ad52 with SMTP id p13-20020a056a20748d00b0012692dead52mr12269480pzd.46.1687987355109; Wed, 28 Jun 2023 14:22:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687987355; cv=none; d=google.com; s=arc-20160816; b=IeWJrfJJV4RAztapINkSj9ISpitk0jLCsmxTRpiaYBFF74O5XnLGnFMxGJuHLjvAyH 6U8UpcjCh/rXcCFUVzIklquOLbf77UHlLJMwVLZIXv79E3eF6MD0xp05d+VLUbsbZaBl YYTjd2Sf7KB/z9QKhhjKgfgMZKSrh3J/4fRN3jRJzw+QU21q3zIv4Wqmz7nO1PX0dum2 PvqKR1q2KXvxn2j+iuZ6poxbibBBVMcElYcJsAJLJdAXSBRk3T8HFRnk7Dta2vVBjpXk PDZKISwKGfy5rMBNgnvjo4XPLyZxeztoZjQonGjcBjMKKhVx4faUV9pYkdYhW7DzGRXV Oqcw== 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=XPZSzWrVq6r0tq5bbCrHed+mA5ObRtgkHqZt7M8XEfc=; fh=PhRfb9k/VCaD3nhvPFELAdaSNFsaoc654VPOTTQHJlA=; b=0gmqSIgeY6MbkZWHPNnIv4qumPmLfxnQpa9eU0lMqR+XIwAIpRi5uI38Jp7bGbLYbw 6oYIFb6mbe61qXK+co31r0BD17mGAtyLr2ZckzYx3eFXwC8IsnxPheBe826GZkjSTl5D zoLJZpkqh5Y3aYLU1dnyep1jL80IqLJAcCGgMdsbz3vnZX2tgOXMU9PNKKDRsa9zFsKB L3jQmI7pumfIOiy/2zMwvgtczh5UUTVDlOOlKICRL3GxJ06c2fPlJm12K/TTLH4kA2DN g0w+ZYtdL3H9OV4yjdglAIJDNJPnx7ZxCxORFzY0tXKdnz4h0otaNIvDMWJ83MzY4vR5 /4wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=sdpAvmx0; 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=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r3-20020a632b03000000b00553ced07d18si9765095pgr.295.2023.06.28.14.22.23; Wed, 28 Jun 2023 14:22:35 -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=@linux.microsoft.com header.s=default header.b=sdpAvmx0; 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=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232671AbjF1VKX (ORCPT + 99 others); Wed, 28 Jun 2023 17:10:23 -0400 Received: from linux.microsoft.com ([13.77.154.182]:39494 "EHLO linux.microsoft.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232324AbjF1VJv (ORCPT ); Wed, 28 Jun 2023 17:09:51 -0400 Received: by linux.microsoft.com (Postfix, from userid 1052) id 55EAC20ABD61; Wed, 28 Jun 2023 14:09:48 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 55EAC20ABD61 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1687986588; bh=XPZSzWrVq6r0tq5bbCrHed+mA5ObRtgkHqZt7M8XEfc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sdpAvmx0yvspZhrqoJv2M6Ozq/s4UzKwx+TKS9+rk+Br1H3QuxemE1gj8ZUDPasYa /O4mWAdFLUegYoE5RenFKO3GJK/RvFXeUePYdWV+aXisXswvm5Qrqi1+uKXw+t8Z8R WhmBqin6qMHKnIEBb7fS+8ZugnGU4HLQxpbQWFSs= 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 v10 06/17] security: add new securityfs delete function Date: Wed, 28 Jun 2023 14:09:20 -0700 Message-Id: <1687986571-16823-7-git-send-email-wufan@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1687986571-16823-1-git-send-email-wufan@linux.microsoft.com> References: <1687986571-16823-1-git-send-email-wufan@linux.microsoft.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 e2734e9e44d5..a88076ebc7b1 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -1971,6 +1971,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 6c326939750d..13358e8547e8 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 from the securityfs filesystem + * + * @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