Received: by 2002:ab2:6486:0:b0:1ef:eae8:a797 with SMTP id de6csp155305lqb; Fri, 15 Mar 2024 20:40:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVAHLu/h1hqhg7NVnqLe4UChnGNBe2k/sg5ZtqAy+4Gl39T0xQJx9bli1T8ErpijrwpNKJDuxctbUEcbTHQNTBob+q94NIAL7iajBV4rg== X-Google-Smtp-Source: AGHT+IG7LMvKmyQT/k+LKueGBG22NtkL7Ey9AjIhJken4lMB4szMWAANP6jRP40rQXTKdEEOhe/c X-Received: by 2002:a05:620a:2f6:b0:789:e9b0:dc50 with SMTP id a22-20020a05620a02f600b00789e9b0dc50mr2377902qko.67.1710560437619; Fri, 15 Mar 2024 20:40:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710560437; cv=pass; d=google.com; s=arc-20160816; b=dIT+MU9OeAZE9y575YHLTmIrPRbEyPL8hW0Y0GpfYpMFfGEE73Ke+4lyC9LfmTMLF2 1k6zL1Wa6hvzkFLhGDbdFxy3bijhD4/mIxMP19Qtjy2cx1ekJGi8cWbldmBaNHKA+fi6 0F3TPvWKidAW+FjTHBvpndihwK7Sd0sip2Y/Omugp+/92uJlBG/g29c5ZsXCfXB5z1gi tCB6g0mBm/fUP69XkHQeb4xeuaFNBu0ofeO3/KWPR2XUeyDWm1jZ3NbWvpYkDHfeSW1e NEey59aPLAaQ9QdOfU+6mG0bX9SEMVGW5fZa67rJXC0ZYyIbkWzGAZ8PEDtMzDoZrhvr tO9w== 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=KoMFZSyeRwf0zohBKcDt7DanemgxhgRO3TjK5eTmxY4=; fh=AghapWq4JPbtw/+GjhWyKvJDXDWh1etonmqDYgtmcRM=; b=IW5/SrqTE2LWir8SaFgr1ZhymRFam1GNKtFwY+ua2Vci3Kd7GbFKJDN+fro5bSa1IJ Fvlwz+mKEIP1luQTey42smqQbO2e23XVCF4OpYfg3OHsY9OZbgW2452p7QaXPePlknHl 1SMWnKdaqu0kCTT7S9q4FKCYZTDL1qETKdRXBWvpLnP3THpdLLLBSVgNIszQVLU06CvN GC9HlRhSwmY2RV6hjRJ6v3PVQoDiFAi21bZgXdyQL8R+yILzfjPltGnU/DbRfv2FnDDh 0Fn6JnWo8Py+5ngT+U1wGGZtu6M2l3S+s3esMv/68AyBA0kzGIeVmffKLNrO0xeYDawi +1Rg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=AbGw6VfF; 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-105054-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-105054-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 ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d5-20020a05620a240500b0078819fdcb59si5689929qkn.442.2024.03.15.20.40.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 20:40:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-105054-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=AbGw6VfF; 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-105054-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-105054-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 282FE1C2190D for ; Sat, 16 Mar 2024 03:40:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CFC0D57326; Sat, 16 Mar 2024 03:36:06 +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="AbGw6VfF" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CF21E6FDC; Sat, 16 Mar 2024 03:35:56 +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=1710560159; cv=none; b=OeHIHNC0PFDLFlN4cA/uJPLW1n7K20oZX7tceFuTU0DSRFCo96vDDYAZMxeo66rpu+JLmJDQCFeP67UVGDtNkGGtoPLX9C1/c56mIAVx35dzDXJauG5IZXpZzgJuSVQx2R2SM+OSOx9mOQp9VEHcTTCB+QlBe5K0BYwo7Qr8SZw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710560159; c=relaxed/simple; bh=h2TsFDtKUmvVYqz3LZafSvm9/uITgSQXr06rA0VNW2g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=hdEmLu4DtGEkbwfngvehz3kYnKf3MHV59kRaJxtCeyA1okWgk/H1sPSs2vkhfMM/DwFixuEyPrHA9b0PUIiUXIC4M3VQoP6V/2cpLgHf3v+HAIL4FSrKYjO+hn/ZQ6WqBGGD3wjre8B0+MPNwfoZhCtvpfATr62FY4PQar7A5hg= 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=AbGw6VfF; 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 9319A20B74CD; Fri, 15 Mar 2024 20:35:53 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 9319A20B74CD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1710560153; bh=KoMFZSyeRwf0zohBKcDt7DanemgxhgRO3TjK5eTmxY4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AbGw6VfFzmFXZ6tJljnNn7HCZ8MbSv/D7t/E8D6H90fOw5C53a5N8zkhDPTECZcqa OEDkumKnDOHDIY0H1lSd2bFT3NATMCYzMPxFas9pA1y5UuQX/ZIq6KYMFt2RHWPDYf WjiTsp0m8c9E1R9jOYr6G4BgBukQw/CVwAVnJm00= 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, fsverity@lists.linux.dev, linux-block@vger.kernel.org, dm-devel@lists.linux.dev, audit@vger.kernel.org, linux-kernel@vger.kernel.org, Fan Wu Subject: [RFC PATCH v15 07/21] security: add new securityfs delete function Date: Fri, 15 Mar 2024 20:35:37 -0700 Message-Id: <1710560151-28904-8-git-send-email-wufan@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1710560151-28904-1-git-send-email-wufan@linux.microsoft.com> References: <1710560151-28904-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 v13: + No changes v14: + No changes v15: + 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 14fff542f2e3..f35af7b6cfba 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -2089,6 +2089,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.44.0