* [PATCH] w1: ds2490: bound the search results to the found_ids array
@ 2026-11-17 14:55 Maoyi Xie
2026-11-17 14:58 ` Krzysztof Kozlowski
0 siblings, 1 reply; 2+ messages in thread
From: Maoyi Xie @ 2026-11-17 14:55 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 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 <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 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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] w1: ds2490: bound the search results to the found_ids array
2026-11-17 14:55 [PATCH] w1: ds2490: bound the search results to the found_ids array Maoyi Xie
@ 2026-11-17 14:58 ` Krzysztof Kozlowski
0 siblings, 0 replies; 2+ messages in thread
From: Krzysztof Kozlowski @ 2026-11-17 14:58 UTC (permalink / raw)
To: Maoyi Xie; +Cc: Christian Vogel, linux-kernel, stable
On 17/11/2026 15:55, Maoyi Xie wrote:
> 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
LLM is not static analysis.
Just say you use Claude or other LLM in changelog, not commit msg.
> 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,
You need to test linux-next. There were several fixes for w1 recently.
Best regards,
Krzysztof
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-17 14:58 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 14:55 [PATCH] w1: ds2490: bound the search results to the found_ids array Maoyi Xie
2026-11-17 14:58 ` Krzysztof Kozlowski
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®