Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp230289lqi; Wed, 6 Mar 2024 15:41:10 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXN/TVKC4TmR4+SZHxCrMYZOaht+gNmlZGcgpRQJD0NnzGY2How2E2xd6ZZZNDhrs0ygVYqPBdfpk+XxZn2/eTKE8tVdSaMMM4Ef3UTxg== X-Google-Smtp-Source: AGHT+IE2jG0mHPIZkfvRUwHOt0kCtOmS4yQwBFC2XPeWamhRjm+3UqMvMaNfx6FPooUCC4BU4NfA X-Received: by 2002:a05:6a21:3399:b0:1a1:6862:b3a7 with SMTP id yy25-20020a056a21339900b001a16862b3a7mr2037304pzb.2.1709768470392; Wed, 06 Mar 2024 15:41:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709768470; cv=pass; d=google.com; s=arc-20160816; b=EybicKaWYsc1pUb5j4C4pllgje7+qTqjkUldg2J5xbEosKzdV5rdsVRQu2DWSfjMWg AOPBQLBaXiIlt+Kgdb1D3ZNB+mXqK+o73QFGEB3YpmDM7cQWI80kQtP5K37lM54a5L96 5oBE/Yd/CUuue/dlT8McYsOQlea2dqtLGb0/S0PFjqc7P/XtxYkA5tLFxjRdp6+g4C3g fpOGeivMxfaGAMQscgy+zBuQh2trPENhO1QjaQbQrNFWpONQRWV6AF0bR6CXL4Mc+DHC gvFdjeoaC9IsSHyvTX3XBVbMQTpyKisnDxd7+ps6pV3+TtwwSAnfo7eQ92SBta6O0AUD KtxQ== 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=fr8Eo2eSLcZfJm9DnTdvTVnD8kopjHO0kBYSSDC+ROk=; fh=OyVWwaN4k6amVBbLsUrv/LtzT4uXeXlIwg6oMI9yoyM=; b=d7kMY2MLWP9wFUF2NQ0yWlMBRmo1ztWc8FYw08xOKmtqX8IofDfPDlhjROBGIHiy+i lVChHc3xRdnv4/nTdxjmK/s2Gu0GGamY/FX9mHNIxEJqNlydnLvA/JVig5fsiKbRCrvp u4JvWCbP1XDnITbdiKJFfN2Oq5uHXA2zN1bWrLWxbEGFKgyNBl9/4JyG//Nue9hq4DN7 9kE51TVaraD0u83BgK+y80BQs+ATbufbCBO6tZHrPOaNUWkMstQ442QM+OGUEdYbsqNN uVI5HouG/FitmxP93WiSSDsG/9DkbjaPfaxAP17v8U47Pfexku17dEnLCPujoj5pyZI9 e6FA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=FNUuo9kp; 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-94723-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94723-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 sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id r26-20020a63515a000000b005dccf9afad5si12618491pgl.305.2024.03.06.15.41.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 15:41:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94723-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=FNUuo9kp; 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-94723-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94723-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id B57B9B23892 for ; Wed, 6 Mar 2024 23:38:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 030EB140E29; Wed, 6 Mar 2024 23:35:04 +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="FNUuo9kp" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 227EB1CFBB; Wed, 6 Mar 2024 23:34:55 +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=1709768097; cv=none; b=WIBlMVfjeC2e5YX+AMMlgw128ylBm/mLM/yCSMol7j3aBffI1TP1/Z56pkdd5qzF60yYmJPCVmhwSY/orSZuPs0gZUYma0GmkdxitPZXWghYkU/T4EGpbO1I2JXBS/LIcmRIVYDkZPO5fspeLdQoN4RqT6q1Mt8k1QPxywkVb5U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709768097; c=relaxed/simple; bh=e37PTlUd9nBIJmmoHxyetMD9rpzsSYCqMSsnRKlkvHM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=OCITL3uMBP0d579pfrP2qftKLdajhKXtM9uYQ8gdzH/Uz3fEu6TpaZX0sZHi2KI6zlDXlIDko9QdoDw2QOvmHbQ1EqpUoHXnCn9rdhbRiouRQSTmuaFHM1rChC5kZY0uRDWezcWnS45EVwzwAQAvAOy0lqbWq1Kq3sguzhovpI0= 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=FNUuo9kp; 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 54D1620B74CD; Wed, 6 Mar 2024 15:34:52 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 54D1620B74CD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1709768092; bh=fr8Eo2eSLcZfJm9DnTdvTVnD8kopjHO0kBYSSDC+ROk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FNUuo9kpG4TwbbJfnBA7MBvCK1r1NjfbY5YGKK9l9fscpDz2K/RnlN/bF3nWkroJx TgiiKb483JiDIrPNSB+XwYySCdKuigwyKBZFINHfGQjzPCec8SOMHaY90EMG3vdqvu QqjNgNK1TUS8sWSgjt/AnmX0yA0cbK5rtgHTylb4= 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 v14 07/19] security: add new securityfs delete function Date: Wed, 6 Mar 2024 15:34:32 -0800 Message-Id: <1709768084-22539-8-git-send-email-wufan@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1709768084-22539-1-git-send-email-wufan@linux.microsoft.com> References: <1709768084-22539-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 --- 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 619e17e59532..e74e8b2a07f4 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.1