Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp118224lqh; Fri, 3 May 2024 15:36:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUgIzfRkQsGuMcSDGi9VExmSNNh9BzHw+l+u5CN7ZLw8eMyLZu6ffv+sDmGBwhzKZA6ThUB0U5GLK4O/AcHECpiNIJLzM3HPR25msxwgQ== X-Google-Smtp-Source: AGHT+IFsYb/6nhaMFVpoK1zmiwpluoLyYSj/5HUvqZzCqIwxnHIluITELBBz9ncsBQmYkdKPRPRm X-Received: by 2002:a50:d4dd:0:b0:572:6ae4:a9ac with SMTP id e29-20020a50d4dd000000b005726ae4a9acmr2211078edj.21.1714775815839; Fri, 03 May 2024 15:36:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714775815; cv=pass; d=google.com; s=arc-20160816; b=HZj/C9DOTKDfV2qopn7o3W/m3Pxuck1W4Zwa9uBRu7zRdVcSKeSvPuzR6fEgtXBZ3m /W6WN1WYKZgL8P/vm5jOyp9jWJ8FYoIE+M4gA/+u0PWKQlEza2F03UzuY9/OOjOMw5uK Dwr6R+WRO93wz8CAHKWe4kOT7mea6fJUPxRiD3TfjykC0pZ5liLl0ZeoG4DEYNcaL4O9 pZM9VOE6R28h3OhmwiQdAZJSE75V/iCoUCUPQdgZ0tavSX9CIv2bmwpURn5yWjdsclUc mS7sVNA0ul63W43LlKO8aN1sDjf8aVFceVK3rDid+Cc/3xp4KnyseL6EfmBXwr1wqDR6 YQmQ== 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=qcfoUW1AGE807w4NVfk1h7IboTHoFwqb0ljSfYgyztA=; fh=AghapWq4JPbtw/+GjhWyKvJDXDWh1etonmqDYgtmcRM=; b=A6PeQp5NvbWm0pxkwQxtpP5OOMcMwpkJwZEBAkNb+wmw6jCwpan8g/mJg/lu4vFd2v O/GV7Cjpj5NDfYEocWLmTtVFGaRi1n+hjhVyW/dVd5ZtZTRYwc5NfhiRf7cC23dD6aA0 aj6V5zWH0xIou9fXctYeAMk2nMGerZvnrKgGrMiLA3RVcJMwQVo6vXnqMa8ziyfym/AI GXatWCqLTYfuojzNUH7XHu31JYxhs9r96+Z7AqJxhj4tvsk/LMtfu+MWz9NOw+Q7jELe 8+4Wm3M/pk4JfuqabXnDWSe7JHt784EAmG4Xil+iaYHIzyOZz3W5oIYmLfqlC3UyIDgQ keCw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=bLgHkbkk; 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-168356-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168356-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 am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id dy5-20020a05640231e500b00571fe8194a1si1914864edb.346.2024.05.03.15.36.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 15:36:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-168356-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=bLgHkbkk; 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-168356-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168356-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 782771F2587B for ; Fri, 3 May 2024 22:36:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EE3EA12DD9A; Fri, 3 May 2024 22:32:46 +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="bLgHkbkk" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF08C84E05; Fri, 3 May 2024 22:32:36 +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=1714775560; cv=none; b=rJHeRwm7T7SrWmRU+UM7xSzFDo3a9Qv2Xvbpmh2y2V5osCSb8G2LGihx1ffzLQ7Fg+VIt1YO6nMt0s7oHMiHpzYkr+faCXnxxVLiUo4ZFzKiJIrzRcdAV57bMESZFLckmQfMHCJTOzJJzk1is4blcHg7qRS+lj17TsdjOdm+DzU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714775560; c=relaxed/simple; bh=k0z7C55TCVhBILeGM6qok0cqc1c1mIbTu2t4cVhYWA8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=WX7hrBwrYQuFU/OrtqD/t0dYjQFrcJRjwCM1LQn9Y5PhERk6/c1SEDwqbyCNcG/043dYpJLzJJfpiy5n1nfeS0U4maDRDvE+N8+8QlPWe0m/057HPDuw1qKrnnd+8Pt9SNsQqqt9K9vRu4R73NL93Ak/AQnATqawi65SlwNJeU4= 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=bLgHkbkk; 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 29AAC20B2C92; Fri, 3 May 2024 15:32:32 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 29AAC20B2C92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1714775553; bh=qcfoUW1AGE807w4NVfk1h7IboTHoFwqb0ljSfYgyztA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bLgHkbkkmEhKDVKy8N40kDPPeo4hDu3bjTjpCN53BsaqiUpyu/ak3JLAdHVo6cY6q 3ClNakHhIbg/nxy4C38D/twopk+s5ilX1o633OdPvxK/MgA9plAhjEWODOEw2PTltW D/7TxWP46o0aGrDo1z3G6O9wi94ay7UL3PPwpnNg= 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: [PATCH v18 07/21] security: add new securityfs delete function Date: Fri, 3 May 2024 15:32:17 -0700 Message-Id: <1714775551-22384-8-git-send-email-wufan@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1714775551-22384-1-git-send-email-wufan@linux.microsoft.com> References: <1714775551-22384-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 v16: + No changes v17: + No changes v18: + 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