Received: by 2002:a05:6a10:ffa2:0:0:0:0 with SMTP id hs34csp39514pxb; Tue, 15 Feb 2022 06:53:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJxxjNKWdfUNo1SG+yWmJWkGk8H0FTPJ5xRDaCbh9+fIUfzEQ2O8/7dSXi+/YOQploznMFic X-Received: by 2002:a17:906:d62:: with SMTP id s2mr3401799ejh.176.1644936781217; Tue, 15 Feb 2022 06:53:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644936781; cv=none; d=google.com; s=arc-20160816; b=0TZ0RYMCDkojldyCYByDgF2SxfRvltbIIF/NgxMfyIjEWTjHH+8+vpTTwmGbc7cWkg o/uN1J5iAX4+qiPjwexEW4D/q8w/wOehnb/1O06wCxyejGsWM3Jj+MlyilcQ7KEESigi njZYiUlFHZYbl1tE1UexSjEXjV2ARpHXSnQBOyH6tHhwY1rTScD34SYFzESmjhJNqea8 FsHzrlkOOMvN/+kl3HBT9m8EU1zZBhXebuy0HHEFGZC9YrNr2eBUhLoAVgpIofqXTl7U sE7DCCHtGwKWmkfPy5tQ9hY5cEAhedBZE+GiMiARB8UWMlM6axHMsW+fOqNXybiM3XSU oW1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=CzyH+o1JxLUR2Ll/OY9EYgjraSbC9kvJMyIsmcqRbHE=; b=jjKHHQXFF18Dj86MDu1L/lDmKOSL2EuAblLRzU+9AWJ79ZKS2zBvbaObsHLt2F6bmR XpTX1Ey5vywvUiPXto/K6xPp4PTVrzM0UPr1OABvfoI08e4+j0amGLQPqIJfKF8mESpd 3CuQSvOVl8bP9hdUnkEdmvogT7paFzbfZYpeZCj10bxZTavC9aIPymtATY/YlcHwg/qG AX57DikbdQ2c/uleC2gbMIbBbEw+o2PHQTsfzY5eQFf975MXZU90qK+tq6fHZFrcRptw npHvz38fKAPNweKpOzmVJIsw1Ahf7nr6316n0tXqS/HXp2tbZvI9EmZNSy5zWpaVH2MI JNuQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q27si21908000edw.658.2022.02.15.06.52.37; Tue, 15 Feb 2022 06:53:01 -0800 (PST) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233139AbiBOGy7 (ORCPT + 99 others); Tue, 15 Feb 2022 01:54:59 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:45934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229575AbiBOGy6 (ORCPT ); Tue, 15 Feb 2022 01:54:58 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C980727B2E for ; Mon, 14 Feb 2022 22:54:45 -0800 (PST) Received: from kwepemi500020.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4JyWyV2QFZzbkSj; Tue, 15 Feb 2022 14:53:38 +0800 (CST) Received: from kwepemm600015.china.huawei.com (7.193.23.52) by kwepemi500020.china.huawei.com (7.221.188.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 15 Feb 2022 14:54:43 +0800 Received: from huawei.com (10.175.127.227) by kwepemm600015.china.huawei.com (7.193.23.52) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 15 Feb 2022 14:54:43 +0800 From: ChenXiaoSong To: , CC: , , , , Subject: [PATCH -next] configfs: fix a race in configfs_{,un}register_subsystem() Date: Tue, 15 Feb 2022 15:10:30 +0800 Message-ID: <20220215071030.3067982-1-chenxiaosong2@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm600015.china.huawei.com (7.193.23.52) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When configfs_register_subsystem() or configfs_unregister_subsystem() is executing link_group() or unlink_group(), it is possible that two processes add or delete list concurrently. Some unfortunate interleavings of them can cause kernel panic. One of cases is: A --> B --> C --> D A <-- B <-- C <-- D delete list_head *B | delete list_head *C --------------------------------|----------------------------------- configfs_unregister_subsystem | configfs_unregister_subsystem unlink_group | unlink_group unlink_obj | unlink_obj list_del_init | list_del_init __list_del_entry | __list_del_entry __list_del | __list_del // next == C | next->prev = prev | | next->prev = prev prev->next = next | | // prev == B | prev->next = next Fix this by adding mutex when calling link_group() or unlink_group(), but parent configfs_subsystem is NULL when config_item is root. So I create a mutex configfs_subsystem_mutex. Fixes: 7063fbf22611 ("[PATCH] configfs: User-driven configuration filesystem") Signed-off-by: ChenXiaoSong Signed-off-by: Laibin Qiu --- fs/configfs/dir.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c index 688f28b0c4f8..170538639533 100644 --- a/fs/configfs/dir.c +++ b/fs/configfs/dir.c @@ -35,6 +35,14 @@ */ DEFINE_SPINLOCK(configfs_dirent_lock); +/* + * All of link_obj/unlink_obj/link_group/unlink_group require that + * subsys->su_mutex is held. + * But parent configfs_subsystem is NULL when config_item is root. + * Use this mutex when config_item is root. + */ +static DEFINE_MUTEX(configfs_subsystem_mutex); + static void configfs_d_iput(struct dentry * dentry, struct inode * inode) { @@ -1927,7 +1935,9 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys) group->cg_item.ci_name = group->cg_item.ci_namebuf; sd = root->d_fsdata; + mutex_lock(&configfs_subsystem_mutex); link_group(to_config_group(sd->s_element), group); + mutex_unlock(&configfs_subsystem_mutex); inode_lock_nested(d_inode(root), I_MUTEX_PARENT); @@ -1952,7 +1962,9 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys) inode_unlock(d_inode(root)); if (err) { + mutex_lock(&configfs_subsystem_mutex); unlink_group(group); + mutex_unlock(&configfs_subsystem_mutex); configfs_release_fs(); } put_fragment(frag); @@ -1999,7 +2011,9 @@ void configfs_unregister_subsystem(struct configfs_subsystem *subsys) dput(dentry); + mutex_lock(&configfs_subsystem_mutex); unlink_group(group); + mutex_unlock(&configfs_subsystem_mutex); configfs_release_fs(); } -- 2.31.1