From: Maoyi Xie Date: Sun, 06 Dec 2026 00:10:13 +0800 Subject: [PATCH v2] i3c: dw: limit the DAT depth to DW_I3C_MAX_DEVS MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261206-dw-i3c-dat-depth-v2-1-47493a3bcc7d@gmail.com> X-B4-Tracking: v=1; b=H4sIAGQ3FGsC/yWMQQ7CMAwEv1L5jKU2ASrxFcQhsV1iDqFKUqhU9 e8k5TirndkgS1LJcOs2SPLRrO9YwZw6oODiU1C5MpjeXAfTX5C/qJaQXUGWuQQceRys9ezlTFC 1Ocmk65G8P/6cF/8SKq3THt5lQZ9cpNCmlouyFtj3H989QYGPAAAA X-Change-ID: 20261205-dw-i3c-dat-depth-7d7133bdbe4c To: Alexandre Belloni Cc: Jeremy Kerr , Louis Sautier , Maoyi Xie , Frank Li , linux-i3c@lists.infradead.org X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796487012; l=2572; i=maoyixie.tju@gmail.com; s=20261205; h=from:subject:message-id; bh=VIggwoRiZ9jbfoFjnWsAd2CMAUNKjx/xphF4lBKR5RA=; b=WBhdTeKaCm5NRK8paVsv16Hk3u8Cdb2MbGN24VHlongHVxVINUx6ivhM9ECWEtI692iv1bjvK tIwLoATBH4PBl2EwuOrh/v814ZMjXzwG6Zis0Lpa1ezDjXhTkuxxrV1 X-Developer-Key: i=maoyixie.tju@gmail.com; a=ed25519; pk=ydzJ0f1uQvj5L6EhJ06HNQgqdJc9AbK3VpVFt+TW8R8= X-Endpoint-Received: by B4 Relay for maoyixie.tju@gmail.com/20261205 with auth_id=1755 List-Id: B4 Relay Submissions 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. Fixes: 1dd728f5d4d4 ("i3c: master: Add driver for Synopsys DesignWare IP") Reported-by: Louis Sautier Link: https://lore.kernel.org/r/55935d9e-a647-4cd0-86a0-915c4aebc302@ovhcloud.com/ Cc: stable@vger.kernel.org # needs linux/bitfield.h for <= 6.18 Signed-off-by: Maoyi Xie --- 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/20261204082119.2977813-1-maoyixie.tju@gmail.com/ 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(-) diff --git a/drivers/i3c/master/dw-i3c-master.c b/drivers/i3c/master/dw-i3c-master.c index 76875301b5ce..9e65268948af 100644 --- a/drivers/i3c/master/dw-i3c-master.c +++ b/drivers/i3c/master/dw-i3c-master.c @@ -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, -- Maoyi Xie