i3c: dw: limit the DAT depth to DW_I3C_MAX_DEVS
Subsystems (2)
PATCH v2 1/1 i3c: dw: limit the DAT depth to DW_I3C_MAX_DEVS
dw_i3c_common_probe() takes maxdevs from the upper 16 bits of DEVICE_ADDR_TABLE_POINTER, so it can be up to 65535. devs[] has DW_I3C_MAX_DEVS (32) entries. The DAA writes devs[pos].addr for each free position below maxdevs. dw_i3c_master_restore_addrs() and dw_i3c_master_get_addr_pos() read devs[pos] up to maxdevs. With a depth above 32, these accesses go past devs[].
A controller whose registers read as all ones reports a depth of 65535. GENMASK() in probe then shifts out of bounds, as reported on an AMD server.
Read the depth with FIELD_GET() in DEVICE_ADDR_TABLE_DEPTH() and limit maxdevs to DW_I3C_MAX_DEVS.
Found with a CodeQL-based static analysis tool and compile-tested only with W=1.
---
v2: use FIELD_GET() as Frank Li suggested. Microchip DS70005629C (24.3.21) shows a 16-bit depth field, so I kept 16 bits and the limit. If only bits 20:16 are valid, I will send a v3.
v1: https://lore.kernel.org/r/[email protected]/
I used an LLM to help write the harness and this patch.
--- drivers/i3c/master/dw-i3c-master.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i3c/master/dw-i3c-master.c +2 -2
@@ -163,7 +163,7 @@#define PRESENT_STATE 0x54#define CCC_DEVICE_STATUS 0x58#define DEVICE_ADDR_TABLE_POINTER 0x5c -#define DEVICE_ADDR_TABLE_DEPTH (x) (((x) & GENMASK(31, 16)) >> 16) +#define DEVICE_ADDR_TABLE_DEPTH (x) FIELD_GET(GENMASK(31, 16), (x))#define DEVICE_ADDR_TABLE_ADDR (x) ((x) & GENMASK(7, 0))#define DEV_CHAR_TABLE_POINTER 0x60
@@ -1772,7 +1772,7 @@ int dw_i3c_common_probe(struct dw_i3c_master *master, ret =readl (master->regs +DEVICE_ADDR_TABLE_POINTER ); master->datstartaddr = ret; - master->maxdevs = ret >>16 ; + master->maxdevs =min (DEVICE_ADDR_TABLE_DEPTH (ret),DW_I3C_MAX_DEVS ); master->free_pos =GENMASK (master->maxdevs -1 ,0 );/*
---
base-commit: 7e0cef0dcbe0c561f09e8bbb950617ac36fb7dcb
change-id: 20261205-dw-i3c-dat-depth-7d7133bdbe4c
Best regards,
Signature · 1 line
Maoyi Xie <[email protected]>