cxl/region: Fix use-after-free in find_pos_and_ways() error path
Alison Schofield
maintainerPATCH 1/1 cxl/region: Fix use-after-free in find_pos_and_ways() error path
The error path releases its reference to a switch decoder before logging an error that includes the decoder name. If the released reference is the last one, the decoder can be freed before the error message accesses its name.
Drop the reference after the error is reported.
Found via inspection.
--- drivers/cxl/core/region.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +2 -2
@@ -1938,8 +1938,6 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,break ; } } -put_device (dev); -if (rc) dev_err(port->uport_dev, "failed to find %s:%s in target list of %s\n",
@@ -1947,6 +1945,8 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,dev_name (port->parent_dport->dport_dev), dev_name(&cxlsd->cxld.dev)); +put_device (dev); +return rc; }
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
Signature · 1 line
2.37.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Unchecked interleave_ways value from untrusted CXL device leads to heap out-of-bounds read and write on the cxlsd->target[] flexible array.
- [High] Out-of-bounds read of stack variable target_list when initializing HDM switch decoder targets.
--
--- Patch [1]: [PATCH] cxl/region: Fix use-after-free in find_pos_and_ways() error path --- commit 1c57e04f0aa13be6ae927836e151c3bc815eda70
Author: Alison Schofield <[email protected]>
cxl/region: Fix use-after-free in find_pos_and_ways() error path
This commit drops the reference to a switch decoder after logging an error message that includes the decoder's name. This prevents a potential use-after-free if it was the last reference.
drivers/cxl/core/region.c static int find_pos_and_ways(struct cxl_port *port, struct range *range,
Quoted 5 lines
drivers/cxl/core/region.c +0
-0@@ -1938,8 +1938,6 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,
[Severity: High]
This is a pre-existing issue, but is there a risk of an out-of-bounds array access in this function and others when handling interleave_ways?
Just above this hunk, find_pos_and_ways() loops up to cxlsd->cxld.interleave_ways and accesses the cxlsd->target[] flexible array:
for (int i = 0; i < *ways; i++) {
if (cxlsd->target[i] == port->parent_dport) {The interleave_ways value originates from the HDM Decoder Control register in init_hdm_decoder():
drivers/cxl/core/hdm.c:init_hdm_decoder() {
...
rc = eiw_to_ways(FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl), &cxld->interleave_ways);
...
}
If an untrusted CXL device provides a small target_count but a maliciously inflated interleave_ways, could this result in heap out-of-bounds reads here in find_pos_and_ways(), as well as out-of-bounds writes in decoder_populate_targets() and update_decoder_targets() when they iterate up to the inflated value?
drivers/cxl/core/region.c static int find_pos_and_ways(struct cxl_port *port, struct range *range, · line 1943
Quoted 8 lines
drivers/cxl/core/region.c +0
-2break ; } } -put_device (dev); -if (rc) dev_err(port->uport_dev, "failed to find %s:%s in target list of %s\n",
[Severity: High]
This is also a pre-existing issue, but does this same unchecked interleave_ways value cause a stack out-of-bounds read in init_hdm_decoder()?
drivers/cxl/core/hdm.c:init_hdm_decoder() {
...
for (i = 0; i < cxld->interleave_ways; i++) cxld->target_map[i] = target_list.target_id[i];
...
}
Since target_list.target_id is an 8-byte stack-allocated union, if interleave_ways is larger than 8 (e.g., 16), could this loop read past the end of the 8-byte array and leak adjacent stack memory into cxld->target_map?
Quoted 8 lines
drivers/cxl/core/region.c +2
-0@@ -1947,6 +1945,8 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,dev_name (port->parent_dport->dport_dev), dev_name(&cxlsd->cxld.dev)); +put_device (dev); +return rc; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Alison Schofield
maintainerQuoted 5 lines · On Sat, Jul 11, 2026 at 06:19:49PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Uncheckedinterleave_waysvalue from untrusted CXL device leads to heap out-of-bounds read and write on thecxlsd->target[]flexible array.
- [High] Out-of-bounds read of stack variabletarget_listwhen initializing HDM switch decoder targets.
For both of these...interleave_ways is not fully unchecked. It is decoded by eiw_to_ways(), which rejects any encoding outside the legal set and caps the result at 16, and init_hdm_decoder() checks that return value and bails on error. So the value is always in [1, 16]
But - as noted here it could exceed HW target list or reported target count. That would happen w a bad device, ie malfunctioning or malicious since good hardware won't advertise an interleave that exceeds its own target list.
Worth hardening, and I'll follow w the fixup.
Quoted 81 lines
--
--- Patch [1]: [PATCH] cxl/region: Fix use-after-free in
find_pos_and_ways()error path --- commit1c57e04f0aa13be6ae927836e151c3bc815eda70
Author: Alison Schofield <[email protected]>cxl/region: Fix use-after-free in
find_pos_and_ways()error pathThis commit drops the reference to a switch decoder after logging an error message that includes the decoder's name. This prevents a potential use-after-free if it was the last reference.
drivers/cxl/core/region.c static int find_pos_and_ways(struct cxl_port *port, struct range *range,
Quoted 5 lines
drivers/cxl/core/region.c +0
-0@@ -1938,8 +1938,6 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,[Severity: High]
This is a pre-existing issue, but is there a risk of an out-of-bounds array access in this function and others when handlinginterleave_ways?Just above this hunk,
find_pos_and_ways()loops up tocxlsd->cxld.interleave_waysand accesses the cxlsd->target[] flexible array:for (int i = 0; i < *ways; i++) { if (cxlsd->target[i] == port->parent_dport) {The
interleave_waysvalue originates from the HDM Decoder Control register ininit_hdm_decoder():drivers/cxl/core/hdm.c:init_hdm_decoder() {
...rc = eiw_to_ways(FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl), &cxld->interleave_ways);...
}If an untrusted CXL device provides a small
target_countbut a maliciously inflatedinterleave_ways, could this result in heap out-of-bounds reads here infind_pos_and_ways(), as well as out-of-bounds writes indecoder_populate_targets()andupdate_decoder_targets()when they iterate up to the inflated value?drivers/cxl/core/region.c static int find_pos_and_ways(struct cxl_port *port, struct range *range, · line 1943
Quoted 8 lines
drivers/cxl/core/region.c +0
-2break ; } } -put_device (dev); -if (rc) dev_err(port->uport_dev, "failed to find %s:%s in target list of %s\n",[Severity: High]
This is also a pre-existing issue, but does this same uncheckedinterleave_waysvalue cause a stack out-of-bounds read ininit_hdm_decoder()?drivers/cxl/core/hdm.c:init_hdm_decoder() {
...for (i = 0; i < cxld->interleave_ways; i++) cxld->target_map[i] = target_list.target_id[i];...
}Since target_list.target_id is an 8-byte stack-allocated union, if
interleave_waysis larger than 8 (e.g., 16), could this loop read past the end of the 8-byte array and leak adjacent stack memory intocxld->target_map?Quoted 8 lines
drivers/cxl/core/region.c +2
-0@@ -1947,6 +1945,8 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,dev_name (port->parent_dport->dport_dev), dev_name(&cxlsd->cxld.dev)); +put_device (dev); +return rc; }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Alison Schofield
maintainerQuoted 13 lines · Alison Schofield
Quoted 5 lines · On Sat, Jul 11, 2026 at 06:19:49PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Uncheckedinterleave_waysvalue from untrusted CXL device leads to heap out-of-bounds read and write on thecxlsd->target[]flexible array.
- [High] Out-of-bounds read of stack variabletarget_listwhen initializing HDM switch decoder targets.For both of these...
interleave_waysis not fully unchecked. It is decoded byeiw_to_ways(), which rejects any encoding outside the legal set and caps the result at 16, andinit_hdm_decoder()checks that return value and bails on error. So the value is always in [1, 16]
Quoted 6 lines
But - as noted here it could exceed HW target list or reported target count. That would happen w a bad device, ie malfunctioning or malicious since good hardware won't advertise an interleave that exceeds its own target list.
Worth hardening, and I'll follow w the fixup.
The fixup I 'promise' here is in the patchset under review titled Harden HDM decoder enumeration.
https://lore.kernel.org/linux-cxl/[email protected]/
Quoted 84 lines
Quoted 83 lines
--
--- Patch [1]: [PATCH] cxl/region: Fix use-after-free in
find_pos_and_ways()error path --- commit1c57e04f0aa13be6ae927836e151c3bc815eda70
Author: Alison Schofield <[email protected]>cxl/region: Fix use-after-free in
find_pos_and_ways()error pathThis commit drops the reference to a switch decoder after logging an error message that includes the decoder's name. This prevents a potential use-after-free if it was the last reference.
drivers/cxl/core/region.c static int find_pos_and_ways(struct cxl_port *port, struct range *range,
Quoted 5 lines
drivers/cxl/core/region.c +0
-0@@ -1938,8 +1938,6 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,[Severity: High]
This is a pre-existing issue, but is there a risk of an out-of-bounds array access in this function and others when handlinginterleave_ways?Just above this hunk,
find_pos_and_ways()loops up tocxlsd->cxld.interleave_waysand accesses the cxlsd->target[] flexible array:for (int i = 0; i < *ways; i++) { if (cxlsd->target[i] == port->parent_dport) {The
interleave_waysvalue originates from the HDM Decoder Control register ininit_hdm_decoder():drivers/cxl/core/hdm.c:init_hdm_decoder() {
...rc = eiw_to_ways(FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl), &cxld->interleave_ways);...
}If an untrusted CXL device provides a small
target_countbut a maliciously inflatedinterleave_ways, could this result in heap out-of-bounds reads here infind_pos_and_ways(), as well as out-of-bounds writes indecoder_populate_targets()andupdate_decoder_targets()when they iterate up to the inflated value?drivers/cxl/core/region.c static int find_pos_and_ways(struct cxl_port *port, struct range *range, · line 1943
Quoted 8 lines
drivers/cxl/core/region.c +0
-2break ; } } -put_device (dev); -if (rc) dev_err(port->uport_dev, "failed to find %s:%s in target list of %s\n",[Severity: High]
This is also a pre-existing issue, but does this same uncheckedinterleave_waysvalue cause a stack out-of-bounds read ininit_hdm_decoder()?drivers/cxl/core/hdm.c:init_hdm_decoder() {
...for (i = 0; i < cxld->interleave_ways; i++) cxld->target_map[i] = target_list.target_id[i];...
}Since target_list.target_id is an 8-byte stack-allocated union, if
interleave_waysis larger than 8 (e.g., 16), could this loop read past the end of the 8-byte array and leak adjacent stack memory intocxld->target_map?Quoted 8 lines
drivers/cxl/core/region.c +2
-0@@ -1947,6 +1945,8 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,dev_name (port->parent_dport->dport_dev), dev_name(&cxlsd->cxld.dev)); +put_device (dev); +return rc; }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Li Ming
reviewerQuoted 29 lines · Alison Schofield
The error path releases its reference to a switch decoder before logging an error that includes the decoder name. If the released reference is the last one, the decoder can be freed before the error message accesses its name.
Drop the reference after the error is reported.
Found via inspection.
Fixes d90acdf49e18 ("cxl/region: Add a dev_err() on missing target list entries")Signed-off-by Alison Schofield <[email protected]>--- drivers/cxl/core/region.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/cxl/core/region.c +0
-2@@ -1938,8 +1938,6 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,break ; } } -put_device (dev); -if (rc) dev_err(port->uport_dev, "failed to find %s:%s in target list of %s\n",
drivers/cxl/core/region.c static int find_pos_and_ways(struct cxl_port *port, struct range *range, · line 1949
Quoted 5 lines
drivers/cxl/core/region.c +2
-0@@ -1947,6 +1945,8 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,dev_name (port->parent_dport->dport_dev), dev_name(&cxlsd->cxld.dev)); +put_device (dev); +
Is it better to use a __free(put_device) instead of this open-coded?
Ming
Quoted 5 lines
drivers/cxl/core/region.c +0
-0return rc; }base-commit:
dc59e4fea9d83f03bad6bddf3fa2e52491777482
Alison Schofield
maintainerQuoted 33 lines · Li Ming
Quoted 32 lines · Alison Schofield
The error path releases its reference to a switch decoder before logging an error that includes the decoder name. If the released reference is the last one, the decoder can be freed before the error message accesses its name.
Drop the reference after the error is reported.
Found via inspection.
Fixes d90acdf49e18 ("cxl/region: Add a dev_err() on missing target list entries")Signed-off-by Alison Schofield <[email protected]>--- drivers/cxl/core/region.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/cxl/core/region.c +0
-2@@ -1938,8 +1938,6 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,break ; } } -put_device (dev); -if (rc) dev_err(port->uport_dev, "failed to find %s:%s in target list of %s\n",@@ -1947,6 +1945,8 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,dev_name (port->parent_dport->dport_dev), dev_name(&cxlsd->cxld.dev));
Quoted 4 lines
drivers/cxl/core/region.c static int find_pos_and_ways(struct cxl_port *port, struct range *range, · line 1949
Quoted 2 lines
drivers/cxl/core/region.c +2
-0+put_device (dev); +Is it better to use a
__free(put_device)instead of this open-coded?
Yes, but that is out of scope for this specific fix.
Using the __free version changes cleanup for the whole function. (ie - doesn't just touch this one line above)
I do agree it's best I do that 'now' in series with this patch, so I'll respin this as a 2 patch series.
-- Alison
Quoted 10 lines
Ming
Quoted 5 lines
drivers/cxl/core/region.c +0
-0return rc; }base-commit:
dc59e4fea9d83f03bad6bddf3fa2e52491777482