cxl/hdm: Robustify decoder target list reads
Davidlohr Bueso
maintainerPATCH 1/1 cxl/hdm: Robustify decoder target list reads
Since the target_list union is fixed at 8 entries, if a CXL switch device has an IW value larger than 8 (such as 12 or 16 way), things can go bad:
BUG: KASAN: stack-out-of-bounds in devm_cxl_enumerate_decoders+0xefd/0x1b00 Read of size 1 at addr ffff8881003bf180 by task kworker/u16:0/12 kasan_report+0xe0/0x110 devm_cxl_enumerate_decoders+0xefd/0x1b00 cxl_port_add_dport+0x9c/0x200 devm_cxl_enumerate_ports+0xa36/0x1190 cxl_mem_probe+0x26f/0x710 The buggy address belongs to stack of task kworker/u16:0/12 and is located at offset 40 in frame: devm_cxl_enumerate_decoders+0x0/0x1b00 This frame has 1 object: [32, 40) 'target_list'
While this is out of spec (see Table 123), we can robustify and just reject such a decoder - noting that cxl_port_setup_targets() already refuses to program more than 8 ways.
--- drivers/cxl/core/hdm.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/cxl/core/hdm.c +7 -0
@@ -1084,6 +1084,13 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, cxld->interleave_ways, cxld->interleave_granularity);if (!cxled) { +if (cxld->interleave_ways >ARRAY_SIZE (target_list.target_id)) { +dev_warn (&port->dev, +"decoder%d.%d: Invalid target list (iw: %d)\n" , + port->id, cxld->id, cxld->interleave_ways); +return -EINVAL ; + } + lo =readl (hdm +CXL_HDM_DECODER0_TL_LOW (which)); hi =readl (hdm +CXL_HDM_DECODER0_TL_HIGH (which)); target_list.value = (hi <<32 ) + lo;
Signature · 1 line
2.39.5
[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:
New issues:
- [High] Strict bounds check on interleave_ways causes fatal port enumeration failure for uncommitted decoders.
Pre-existing issues:
- [High] Endianness bug via union type punning corrupts decoder target maps on big-endian architectures. --
--- Patch [1]: [PATCH] cxl/hdm: Robustify decoder target list reads --- commit a440172bd3f3303bac0379eb1712a8d4f611d2ab
Author: Davidlohr Bueso <[email protected]>
cxl/hdm: Robustify decoder target list reads
This commit adds a bounds check on the interleave_ways value to prevent KASAN stack-out-of-bounds reads when iterating over the target_list union.
drivers/cxl/core/hdm.c static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, · line 1091
Quoted 13 lines
drivers/cxl/core/hdm.c +5
-0@@ -1084,6 +1084,13 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, cxld->interleave_ways, cxld->interleave_granularity);if (!cxled) { +if (cxld->interleave_ways >ARRAY_SIZE (target_list.target_id)) { +dev_warn (&port->dev, +"decoder%d.%d: Invalid target list (iw: %d)\n" , + port->id, cxld->id, cxld->interleave_ways); +return -EINVAL ;
[Severity: High]
Does this strict bounds check cause a regression where port enumeration fails for uncommitted decoders?
If any uncommitted decoder is left by BIOS or powers on with an interleave_ways value greater than 8, this returns -EINVAL. Because this is called in a loop within devm_cxl_enumerate_decoders(), this error aborts the setup of the entire switch port and prevents userspace from ever configuring the device.
Could uncommitted decoders be gracefully ignored or clamped instead of causing a probe failure?
drivers/cxl/core/hdm.c static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, · line 1096
Quoted 5 lines
drivers/cxl/core/hdm.c +2
-0+ } + lo =readl (hdm +CXL_HDM_DECODER0_TL_LOW (which)); hi =readl (hdm +CXL_HDM_DECODER0_TL_HIGH (which)); target_list.value = (hi <<32 ) + lo;
[Severity: High]
This isn't a bug introduced by this patch, but does this union type punning corrupt the decoder target maps on big-endian architectures?
The target IDs are read from a native 64-bit integer (target_list.value) and then accessed as a byte array in the following loop:
for (i = 0; i < cxld->interleave_ways; i++) cxld->target_map[i] = target_list.target_id[i];
On little-endian architectures this matches the byte order, but on big-endian architectures, the most significant byte is at index 0, causing the targets to be parsed in reverse.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Alison Schofield
maintainerQuoted 18 lines · Davidlohr Bueso
Since the
target_listunion is fixed at 8 entries, if a CXL switch device has an IW value larger than 8 (such as 12 or 16 way), things can go bad:BUG: KASAN: stack-out-of-bounds in devm_cxl_enumerate_decoders+0xefd/0x1b00 Read of size 1 at addr ffff8881003bf180 by task kworker/u16:0/12 kasan_report+0xe0/0x110 devm_cxl_enumerate_decoders+0xefd/0x1b00 cxl_port_add_dport+0x9c/0x200 devm_cxl_enumerate_ports+0xa36/0x1190 cxl_mem_probe+0x26f/0x710 The buggy address belongs to stack of task kworker/u16:0/12 and is located at offset 40 in frame: devm_cxl_enumerate_decoders+0x0/0x1b00 This frame has 1 object: [32, 40) 'target_list'
Quoted 7 lines
While this is out of spec (see Table 123), we can robustify and just reject such a decoder - noting that
cxl_port_setup_targets()already refuses to program more than 8 ways.Reported-by Sashiko <[email protected]>Fixes d17d0540a0db ("cxl/core/hdm: Add CXL standard decoder enumeration to the core")Signed-off-by Davidlohr Bueso <[email protected]>
Hi Davidlohr,
I think this issue, and its spawn (the endianness issue), are addressed in this in flight patchset. Please take a look:
https://lore.kernel.org/linux-cxl/[email protected]/
-- Alison
Quoted 25 lines
--- drivers/cxl/core/hdm.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/cxl/core/hdm.c +7
-0@@ -1084,6 +1084,13 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, cxld->interleave_ways, cxld->interleave_granularity);if (!cxled) { +if (cxld->interleave_ways >ARRAY_SIZE (target_list.target_id)) { +dev_warn (&port->dev, +"decoder%d.%d: Invalid target list (iw: %d)\n" , + port->id, cxld->id, cxld->interleave_ways); +return -EINVAL ; + } + lo =readl (hdm +CXL_HDM_DECODER0_TL_LOW (which)); hi =readl (hdm +CXL_HDM_DECODER0_TL_HIGH (which)); target_list.value = (hi <<32 ) + lo;Signature · 1 line
2.39.5
Davidlohr Bueso
maintainerQuoted 5 lines · Alison Schofield
Hi Davidlohr,
I think this issue, and its spawn (the endianness issue), are addressed in this in flight patchset. Please take a look:
Hi Alison, thanks for taking a look and yes I agree your patch 1/4 fixes the issue. I would perhaps add the kasan splat in this changelog just for extra evidence.
Thanks,
Davidlohr