From: Guixin Liu <kanie@linux.alibaba.com>
To: Li Ming <ming.li@zohomail.com>,
Davidlohr Bueso <dave@stgolabs.net>,
Jonathan Cameron <jic23@kernel.org>,
Dave Jiang <dave.jiang@intel.com>,
Alison Schofield <alison.schofield@intel.com>,
Vishal Verma <vishal.l.verma@intel.com>,
Dan Williams <djbw@kernel.org>, Ira Weiny <iweiny@kernel.org>,
Robert Richter <rrichter@amd.com>
Cc: linux-cxl@vger.kernel.org, xlpang@linux.alibaba.com,
oliver.yang@linux.alibaba.com
Subject: Re: [PATCH 3/8] cxl/core: Fix dport use-after-free via the einj_inject debugfs file
Date: Wed, 12 Aug 2026 09:58:36 +0800 [thread overview]
Message-ID: <0c1cc925-71e0-4b83-a7b6-76f6058484ab@linux.alibaba.com> (raw)
In-Reply-To: <06658b3d-74dc-4d89-88c0-38b2ff098c75@zohomail.com>
在 2026/8/12 00:03, Li Ming 写道:
>
> 在 2026/8/11 19:36, Guixin Liu 写道:
>> cxl_debugfs_create_dport_dir() creates a debugfs directory holding an
>> "einj_inject" file whose i_private is the 'struct cxl_dport', but it
>> discards the returned dentry and registers no cleanup. The dport is
>> freed
>> by free_dport() when the devres group of its host device is released,
>> while the debugfs nodes live until the cxl_core module is unloaded
>> (debugfs_remove_recursive() in cxl_core_exit()).
>>
>> So after unbinding the dport's host, e.g. unbinding the host bridge port
>> or the ACPI0017 root, writing to
>>
>> /sys/kernel/debug/cxl/<dport_dev>/einj_inject
>>
>> calls cxl_einj_inject() on freed memory and dereferences dport->rch and
>> dport->dport_dev.
>>
>> The leaked directory is also named after the dport device, so
>> re-adding the
>> same dport (bind after unbind) hits an existing name, debugfs creation
>> fails, and error injection stays broken for that dport for the rest
>> of the
>> module's lifetime.
>>
>> Save the dentry and drop the whole directory via a devm action on the
>> same
>> host device. The action is registered inside the dport devres group and
>> after free_dport(), so it runs before the dport is freed. Propagate the
>> failure to __devm_cxl_add_dport() rather than continuing with a dport
>> that
>> has a dangling debugfs node.
>>
>> Fixes: 8039804cfa73 ("cxl/core: Add CXL EINJ debugfs files")
>> Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
>> ---
>> drivers/cxl/core/port.c | 18 ++++++++++++++----
>> 1 file changed, 14 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
>> index 1215ee4f4035..76bda54ed986 100644
>> --- a/drivers/cxl/core/port.c
>> +++ b/drivers/cxl/core/port.c
>> @@ -813,13 +813,18 @@ static int cxl_einj_inject(void *data, u64 type)
>> DEFINE_DEBUGFS_ATTRIBUTE(cxl_einj_inject_fops, NULL, cxl_einj_inject,
>> "0x%llx\n");
>> -static void cxl_debugfs_create_dport_dir(struct cxl_dport *dport)
>> +static void remove_debugfs(void *dentry)
>> +{
>> + debugfs_remove_recursive(dentry);
>> +}
>> +
>> +static int cxl_debugfs_create_dport_dir(struct cxl_dport *dport)
>> {
>> struct cxl_port *parent = parent_port_of(dport->port);
>> struct dentry *dir;
>> if (!einj_cxl_is_initialized())
>> - return;
>> + return 0;
>> /*
>> * Protocol error injection is only available for CXL 2.0+ root
>> ports
>> @@ -827,12 +832,15 @@ static void cxl_debugfs_create_dport_dir(struct
>> cxl_dport *dport)
>> */
>> if (!dport->rch &&
>> !(dev_is_pci(dport->dport_dev) && parent &&
>> is_cxl_root(parent)))
>> - return;
>> + return 0;
>> dir = cxl_debugfs_create_dir(dev_name(dport->dport_dev));
>> debugfs_create_file("einj_inject", 0200, dir, dport,
>> &cxl_einj_inject_fops);
>> +
>> + return devm_add_action_or_reset(dport_to_host(dport),
>> remove_debugfs,
>> + dir);
>
> I think we don't need to worry about devm_add_action_or_reset()
> failing, missing this debugfs directory seems like acceptable, but the
> failure cases of this devm_add_action_or_reset() will cause dport
> addition failure.
>
> So I think just like below devm_cxl_dport_ras_setup(), do not check
> the return value of the function.
Sure, changed in v2, thanks.
Best Regards,
Guixin Liu
>
>
>> }
>> static int cxl_port_add(struct cxl_port *port,
>> @@ -1240,7 +1248,9 @@ __devm_cxl_add_dport(struct cxl_port *port,
>> struct device *dport_dev,
>> if (dev_is_pci(dport_dev))
>> dport->link_latency =
>> cxl_pci_get_latency(to_pci_dev(dport_dev));
>> - cxl_debugfs_create_dport_dir(dport);
>> + rc = cxl_debugfs_create_dport_dir(dport);
>> + if (rc)
>> + return ERR_PTR(rc);
>> if (!dport->rch)
>> devm_cxl_dport_ras_setup(dport);
next prev parent reply other threads:[~2026-08-12 1:58 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-11 11:36 [PATCH 0/8] cxl: Assorted fixes Guixin Liu
2026-08-11 11:36 ` [PATCH 1/8] cxl/features: Validate the fwctl RPC input length Guixin Liu
2026-08-11 11:36 ` [PATCH 2/8] cxl/features: Bound the Get Feature output by the user output buffer Guixin Liu
2026-08-11 11:36 ` [PATCH 3/8] cxl/core: Fix dport use-after-free via the einj_inject debugfs file Guixin Liu
2026-08-11 16:03 ` Li Ming
2026-08-12 1:58 ` Guixin Liu [this message]
2026-08-11 11:36 ` [PATCH 4/8] cxl/pci: Fix NULL pointer dereference in reset detection Guixin Liu
2026-08-11 11:36 ` [PATCH 5/8] cxl/hdm: Fix out of bounds read of the decoder target list Guixin Liu
2026-08-11 11:36 ` [PATCH 6/8] cxl/cdat: Fix uninitialized stack use in endpoint bandwidth gathering Guixin Liu
2026-08-11 11:36 ` [PATCH 7/8] cxl/mce: Validate the memdev and endpoint before use Guixin Liu
2026-08-11 11:36 ` [PATCH 8/8] cxl/region: Unregister the pmem region bridge on setup failure Guixin Liu
2026-08-11 19:57 ` [PATCH 0/8] cxl: Assorted fixes Alison Schofield
2026-08-12 2:10 ` Guixin Liu
2026-08-12 6:29 ` Richard Cheng
2026-08-12 6:37 ` Guixin Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0c1cc925-71e0-4b83-a7b6-76f6058484ab@linux.alibaba.com \
--to=kanie@linux.alibaba.com \
--cc=alison.schofield@intel.com \
--cc=dave.jiang@intel.com \
--cc=dave@stgolabs.net \
--cc=djbw@kernel.org \
--cc=iweiny@kernel.org \
--cc=jic23@kernel.org \
--cc=linux-cxl@vger.kernel.org \
--cc=ming.li@zohomail.com \
--cc=oliver.yang@linux.alibaba.com \
--cc=rrichter@amd.com \
--cc=vishal.l.verma@intel.com \
--cc=xlpang@linux.alibaba.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.