Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp1307547rdb; Tue, 30 Jan 2024 14:41:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IH1HJWMpYPxHRP5Ct4x/AumDzJTDG+bZnhN6dHon+fhAOVgxJd/x8kcePWeP6f1iwfvMB29 X-Received: by 2002:a05:6a20:3421:b0:19b:1fc3:18d1 with SMTP id i33-20020a056a20342100b0019b1fc318d1mr8380697pzd.10.1706654489892; Tue, 30 Jan 2024 14:41:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706654489; cv=pass; d=google.com; s=arc-20160816; b=Osbse+vsb1yjcj2MIwC9rgjccsHmVBa292rSnkrYVfl1ETf5u6jT9fJQPQCWZaObxy 8qFd+lOVkll7dxppCKKoyC1JKKch4I26Qhittb6qxjM0JAD5k5eS2eN+k/idL6y25jin xi1uccgXAxLmFFXy7Yqq7ljPxdEuIsratRD/HLnp9I+zvzewKtvLNlUDcg21cJRQvMLu bcNu16Htrv+BClRoxfhsOMLYfZzyaLE6RReu3ShBUBAB4ZCOZi8MWIpfrmVB/+PnYHQP xW6Ake3k97De2NDHXy8Tf2Jhq9Qy40VThSVRynooKjIqcBTnaR73gVxfL4d+55FUve5R 41fA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter; bh=MZIUTiZ4IP2Ll7W/ScSzaJN4C5rYzK2zFEdobpZtunU=; fh=OyVWwaN4k6amVBbLsUrv/LtzT4uXeXlIwg6oMI9yoyM=; b=g/AaZXtsGpNHYYh6gOAb0YSQjOLPxXiqjeg3cCgpFTEGUiLGSfNaxmDJyenQ6vfEGf KAWRmBbZKUR9EEI4BM02p7PVn6rlhMQCA5v1tRcxRhA7Q4dXCxeliQd2DZ8CBzqEN5bJ GnjvIsDNmYZkVyxE4EzM23S4zfOyRqxM3eZPO9i9d4Tj/t3evvR9W+3p37+cSw0efG7x ZipdFLalggyEqLDe1Jz5oGQaJyw8043QK5X31GZ2ijImO75HpBBG13E9Jb0uRzAT7o+2 5FI2Wy/xbyzJCB3++u4pCgfV1im8YOhXCexjSfhBRc53hRmKfX0tobl4aidGlQtdGLWd wjIw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=eKRU4ybE; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-45412-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45412-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id b2-20020a170902bd4200b001d919605066si1115633plx.192.2024.01.30.14.41.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 14:41:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-45412-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=eKRU4ybE; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-45412-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45412-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 907C02887BA for ; Tue, 30 Jan 2024 22:41:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 683EB7A3FC; Tue, 30 Jan 2024 22:37:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="eKRU4ybE" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C06D1128369; Tue, 30 Jan 2024 22:37:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706654248; cv=none; b=YHziBxP1rupKe9pBWvZCS1x3nJOo+vIYfkFAw8tRklJaRQUHb1Y2vB3abikyfeJZL+T+TvmhGx8MHCtQtT+RhfSSzfHcM2pXQ5N0UN/qbL2lLAIku6ath/bjne6E0jwXvBzEjgwxsHnNnJrCXJJzwmY7DbvMLpbg1/iZ5bzsL9A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706654248; c=relaxed/simple; bh=q2N90IkYiMhawf/EMMMNOfHILynY5K1y2kARX2muhOM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=ZuBHJ9BpImyK2uvwiPWrle+jVZwnnmCFDlJ1EtcPk7ZIX91nP4OH3kKapoN+iTZwbGrga5UlrSOvAgePabzDD/4+FvwpWILFbaIS7Pl5wkhaao6kXruD4tAkr1TZeocO5CgUZN2zTK9s36B0iGT0/cnbL+pbW6rfuhalPquHM74= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=eKRU4ybE; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Received: by linux.microsoft.com (Postfix, from userid 1052) id 4F5A820B200B; Tue, 30 Jan 2024 14:37:22 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 4F5A820B200B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1706654242; bh=MZIUTiZ4IP2Ll7W/ScSzaJN4C5rYzK2zFEdobpZtunU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eKRU4ybEEAYVTcJ+3CYfX0iOCDUSsbZcW3FFJC+xe1rwPE9+Smgh+Gr0aUgZg/sc9 hb6VrsDZyQTIM9lmiRP0Mbr1Jcxaf9g6H4Wc/37pmxHnBLwtj5vUz3czZT46sESPQR 4A/MnsVD/6mdpkqr2P01SZpSgi6iQC4Mtj2mkdCc= 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@lists.linux.dev, audit@vger.kernel.org, linux-kernel@vger.kernel.org, Fan Wu Subject: [RFC PATCH v12 07/20] security: add new securityfs delete function Date: Tue, 30 Jan 2024 14:36:55 -0800 Message-Id: <1706654228-17180-8-git-send-email-wufan@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1706654228-17180-1-git-send-email-wufan@linux.microsoft.com> References: <1706654228-17180-1-git-send-email-wufan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 intended 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 v12: + 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 1545c69edc1b..e994ac9cda91 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -2029,6 +2029,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 9e7cde913667..f21847badb7d 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.43.0