* [PATCH v2] w1: ds2490: bound the search results to the found_ids array
@ 2026-11-18 15:14 Maoyi Xie
0 siblings, 0 replies; only message in thread
From: Maoyi Xie @ 2026-11-18 15:14 UTC (permalink / raw)
To: Krzysztof Kozlowski; +Cc: Christian Vogel, linux-kernel, stable
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 CodeQL-based checker that we are developing. A
userspace harness runs an unmodified copy of ds9490r_search() from
next-20261116 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 <maoyixie.tju@gmail.com>
---
I used Claude to help check the checker's report and to write the
userspace harness and this patch. Sorry that v1 did not say so.
v2:
- rebase on linux-next (next-20261116) and test there (W=1 build, the
same harness)
This applies to linux-next but not to the w1 branches, where the
allocation is still kmalloc_array(). I can send a version for them.
v1: https://lore.kernel.org/all/20261117145534.755665-1-maoyixie.tju@gmail.com/
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 f1d4075075a657..c5d76a5ceff468 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_objs(u64, master->max_slave_count);
+ max_slaves = master->max_slave_count;
+ found_ids = kmalloc_objs(u64, max_slaves);
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: 749a4de370409464cf8501ec5b2451df45c84eb4
--
2.34.1
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-18 15:14 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 15:14 [PATCH v2] w1: ds2490: bound the search results to the found_ids array Maoyi Xie
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®