ds9490r_search() queues the ids from the DS2490 in found_ids, which has max_slave_count entries. max_slave_count is also the search limit. With more devices than that on the bus, the DS2490 sends that many ids and then a discrepancy value, as the comment in the function says. The break at the limit leaves the loop over the current bulk read, but the outer loop keeps reading until the device is idle. A discrepancy value in a later bulk read is then stored in found_ids[max_slave_count], one past the end, and the callback loop reads it back as an id. If the device sends more values, they are written past the array too. Store an id only if it fits and pass at most max_slave_count ids to the callback. found can still reach max_slave_count + 1, so the check after the loop sees that more devices remain. Use one local copy of max_slave_count for the allocation and the bound. I found this with a static analysis tool that we are developing. A userspace harness runs an unmodified copy of ds9490r_search() from v7.4-rc3 against a fake DS2490. With the default max_slave_count of 64, AddressSanitizer reports an 8-byte write past the 512-byte array. I do not have the hardware, so the fix is only compile-tested with W=1 and checked with the harness. Fixes: e3fe0e89fec6 ("w1/masters/ds2490: queue up found IDs during scan") Cc: stable@vger.kernel.org Signed-off-by: Maoyi Xie --- drivers/w1/masters/ds2490.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/w1/masters/ds2490.c b/drivers/w1/masters/ds2490.c index 3cbff88c339e96..0c792714add874 100644 --- a/drivers/w1/masters/ds2490.c +++ b/drivers/w1/masters/ds2490.c @@ -697,6 +697,7 @@ static void ds9490r_search(void *data, struct w1_master *master, u16 value, index; struct ds_status st; int search_limit; + int max_slaves; int found = 0; int i; @@ -718,7 +719,8 @@ static void ds9490r_search(void *data, struct w1_master *master, * We are holding the bus mutex during the scan, but adding devices via the * callback needs the bus to be unlocked. So we queue up found ids here. */ - found_ids = kmalloc_array(master->max_slave_count, sizeof(u64), GFP_KERNEL); + max_slaves = master->max_slave_count; + found_ids = kmalloc_array(max_slaves, sizeof(u64), GFP_KERNEL); if (!found_ids) { kfree(buf); return; @@ -733,7 +735,7 @@ static void ds9490r_search(void *data, struct w1_master *master, value = COMM_SEARCH_ACCESS | COMM_IM | COMM_RST | COMM_SM | COMM_F | COMM_RTS; - search_limit = master->max_slave_count; + search_limit = max_slaves; if (search_limit > 255) search_limit = 0; index = search_type | (search_limit << 8); @@ -760,7 +762,10 @@ static void ds9490r_search(void *data, struct w1_master *master, if (err < 0) break; for (i = 0; i < err/8; ++i) { - found_ids[found++] = buf[i]; + if (found < max_slaves) + found_ids[found] = buf[i]; + if (found <= max_slaves) + found++; /* * can't know if there will be a discrepancy * value after until the next id @@ -798,6 +803,7 @@ static void ds9490r_search(void *data, struct w1_master *master, mutex_unlock(&master->bus_mutex); kfree(buf); + found = min(found, max_slaves); for (i = 0; i < found; i++) /* run callback for all queued up IDs */ callback(master, found_ids[i]); kfree(found_ids); base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 -- 2.34.1