process_recv() allocates hid_devices only when it is NULL, with the count of the first ENUM_DEVICES response. It then copies as many entries as each response announces. After an ISH reset, a larger count can write past the array. A second response with a larger count does the same. After a first response with no devices, the next copy writes at ZERO_SIZE_PTR. Size the array to the announced count with devm_krealloc_array() before the copy, and update hid_dev_count only when that succeeds. A count of zero keeps the old array as before, since hid_ishtp_cl_init() may still read it. Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under AddressSanitizer, which reports none of these writes with this patch. Not tested on hardware, only compile-tested with W=1 on hid.git for-7.4/upstream-fixes. Fixes: 0b28cb4bcb17 ("HID: intel-ish-hid: ISH HID client driver") Cc: stable@vger.kernel.org Signed-off-by: Maoyi Xie --- I used an LLM to help write the harness and this patch. drivers/hid/intel-ish-hid/ishtp-hid-client.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/drivers/hid/intel-ish-hid/ishtp-hid-client.c b/drivers/hid/intel-ish-hid/ishtp-hid-client.c index 24f4032d6dfe..864a06a7bd3f 100644 --- a/drivers/hid/intel-ish-hid/ishtp-hid-client.c +++ b/drivers/hid/intel-ish-hid/ishtp-hid-client.c @@ -70,6 +70,8 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, unsigned char *payload; unsigned char *descr; struct device_info *dev_info; + struct device_info *devices; + unsigned int count; int i, j; size_t payload_len, total_len, cur_pos, raw_len, msg_len; int report_type; @@ -124,19 +126,21 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, } if (client_data->init_done) break; - client_data->hid_dev_count = (unsigned int)*payload; - if (!client_data->hid_devices) - client_data->hid_devices = devm_kcalloc( - cl_data_to_dev(client_data), - client_data->hid_dev_count, - sizeof(struct device_info), - GFP_KERNEL); - if (!client_data->hid_devices) { + count = *payload; + devices = client_data->hid_devices; + if (count || !devices) + devices = devm_krealloc_array(cl_data_to_dev(client_data), + devices, count, + sizeof(struct device_info), + GFP_KERNEL | __GFP_ZERO); + if (!devices) { dev_err(cl_data_to_dev(client_data), "Mem alloc failed for hid device info\n"); wake_up_interruptible(&client_data->init_wait); break; } + client_data->hid_devices = devices; + client_data->hid_dev_count = count; for (i = 0; i < client_data->hid_dev_count; ++i) { if (1 + sizeof(struct device_info) * i >= payload_len) { -- 2.34.1