From nobody Sun Feb 8 15:07:46 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B84518A6B7 for ; Mon, 23 Dec 2024 18:53:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734980015; cv=none; b=Z7h8eQbvYrqldTPUj1zbeIwlFCzvglVznTMtfHVoFxspQ4vLljbWVyvkB/L4l6e9UBPvHbTYDYHqZfiX/mL6kLNfcAUHUF7GYDOTWlnxNZfRpC6s2KE8BkyUkWOWUy/AEYXiqHqLPnrLDA4vgQD50Q6t/NyWSPfPnsbLujdo+L8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734980015; c=relaxed/simple; bh=hnlYZ8tb7q7aTomr29mTssy2gVhz9t6NhNSXINGZOwk=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=gkjSIs69uv0T44JIrS1uRCmlvZfa2jxEJvv5FXDtKFf0oPuH2sODC7RMD5bkWyAn+iov4V/HGB0otZ5vlkq7MLOHy63lJsaSfM/P4Q9hhf1dYJ4rgzavjuruE2gqymH8XjFvqYnkwdKwtIK/moNGx+JrUsslrALvNsqhKhXNQ8c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5939E1424; Mon, 23 Dec 2024 10:54:00 -0800 (PST) Received: from e129823.cambridge.arm.com (e129823.arm.com [10.1.197.6]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id BAAA83F59E; Mon, 23 Dec 2024 10:53:30 -0800 (PST) From: Yeoreum Yun To: suzuki.poulose@arm.com, mike.leach@linaro.org, james.clark@linaro.org, alexander.shishkin@linux.intel.com Cc: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Yeoreum Yun Subject: [PATCH v2] coresight: prevent deactivate active config while enable the config Date: Mon, 23 Dec 2024 18:53:28 +0000 Message-Id: <20241223185328.1339616-1-yeoreum.yun@arm.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" While enable active config via cscfg_csdev_enable_active_config(), active config could be deactivated via configfs' sysfs interface. This could make UAF issue in below scenario: CPU0 CPU1 (sysfs enable) load module cscfg_load_config_sets() activate config. // sysfs (sys_active_cnt =3D=3D 1) ... cscfg_csdev_enable_active_config() lock(csdev->cscfg_csdev_lock) // here load config activate by CPU1 unlock(csdev->cscfg_csdev_lock) deactivate config // sysfs (sys_activec_cnt =3D=3D 0) cscfg_unload_config_sets() unload module // access to config_desc which freed // while unloading module. cfs_csdev_enable_config To address this, introduce sys_enable_cnt in cscfg_mgr to prevent deactivate while there is enabled configuration. Signed-off-by: Yeoreum Yun --- from v1 to v2: - modify commit message. --- .../hwtracing/coresight/coresight-etm4x-core.c | 3 +++ drivers/hwtracing/coresight/coresight-syscfg.c | 18 ++++++++++++++++-- drivers/hwtracing/coresight/coresight-syscfg.h | 2 ++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-etm4x-core.c b/drivers/h= wtracing/coresight/coresight-etm4x-core.c index 86893115df17..6218ef40acbc 100644 --- a/drivers/hwtracing/coresight/coresight-etm4x-core.c +++ b/drivers/hwtracing/coresight/coresight-etm4x-core.c @@ -986,6 +986,9 @@ static void etm4_disable_sysfs(struct coresight_device = *csdev) smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1); raw_spin_unlock(&drvdata->spinlock); + + cscfg_csdev_disable_active_config(csdev); + cpus_read_unlock(); /* diff --git a/drivers/hwtracing/coresight/coresight-syscfg.c b/drivers/hwtra= cing/coresight/coresight-syscfg.c index a70c1454b410..dfa7dcbaf25d 100644 --- a/drivers/hwtracing/coresight/coresight-syscfg.c +++ b/drivers/hwtracing/coresight/coresight-syscfg.c @@ -953,7 +953,8 @@ int cscfg_config_sysfs_activate(struct cscfg_config_des= c *config_desc, bool acti cscfg_mgr->sysfs_active_config =3D cfg_hash; } else { /* disable if matching current value */ - if (cscfg_mgr->sysfs_active_config =3D=3D cfg_hash) { + if (cscfg_mgr->sysfs_active_config =3D=3D cfg_hash && + !atomic_read(&cscfg_mgr->sys_enable_cnt)) { _cscfg_deactivate_config(cfg_hash); cscfg_mgr->sysfs_active_config =3D 0; } else @@ -1055,6 +1056,12 @@ int cscfg_csdev_enable_active_config(struct coresigh= t_device *csdev, if (!atomic_read(&cscfg_mgr->sys_active_cnt)) return 0; + /* + * increment sys_enable_cnt first to prevent deactivate the config + * while enable active config. + */ + atomic_inc(&cscfg_mgr->sys_enable_cnt); + /* * Look for matching configuration - set the active configuration * context if found. @@ -1098,6 +1105,10 @@ int cscfg_csdev_enable_active_config(struct coresigh= t_device *csdev, raw_spin_unlock_irqrestore(&csdev->cscfg_csdev_lock, flags); } } + + if (!config_csdev_active || err) + atomic_dec(&cscfg_mgr->sys_enable_cnt); + return err; } EXPORT_SYMBOL_GPL(cscfg_csdev_enable_active_config); @@ -1129,8 +1140,10 @@ void cscfg_csdev_disable_active_config(struct coresi= ght_device *csdev) if (config_csdev) { if (!config_csdev->enabled) config_csdev =3D NULL; - else + else { config_csdev->enabled =3D false; + atomic_dec(&cscfg_mgr->sys_enable_cnt); + } } csdev->active_cscfg_ctxt =3D NULL; raw_spin_unlock_irqrestore(&csdev->cscfg_csdev_lock, flags); @@ -1179,6 +1192,7 @@ static int cscfg_create_device(void) INIT_LIST_HEAD(&cscfg_mgr->config_desc_list); INIT_LIST_HEAD(&cscfg_mgr->load_order_list); atomic_set(&cscfg_mgr->sys_active_cnt, 0); + atomic_set(&cscfg_mgr->sys_enable_cnt, 0); cscfg_mgr->load_state =3D CSCFG_NONE; /* setup the device */ diff --git a/drivers/hwtracing/coresight/coresight-syscfg.h b/drivers/hwtra= cing/coresight/coresight-syscfg.h index 66e2db890d82..2fc397919985 100644 --- a/drivers/hwtracing/coresight/coresight-syscfg.h +++ b/drivers/hwtracing/coresight/coresight-syscfg.h @@ -38,6 +38,7 @@ enum cscfg_load_ops { * @config_desc_list: List of system configuration descriptors to load int= o registered devices. * @load_order_list: Ordered list of owners for dynamically loaded conf= igurations. * @sys_active_cnt: Total number of active config descriptor references. + * @sys_enable_cnt: Total number of enable of active config descriptor ref= erences. * @cfgfs_subsys: configfs subsystem used to manage configurations. * @sysfs_active_config:Active config hash used if CoreSight controlled fr= om sysfs. * @sysfs_active_preset:Active preset index used if CoreSight controlled f= rom sysfs. @@ -50,6 +51,7 @@ struct cscfg_manager { struct list_head config_desc_list; struct list_head load_order_list; atomic_t sys_active_cnt; + atomic_t sys_enable_cnt; struct configfs_subsystem cfgfs_subsys; u32 sysfs_active_config; int sysfs_active_preset; -- LEVI:{C3F47F37-75D8-414A-A8BA-3980EC8A46D7}