mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Maoyi Xie <maoyixie.tju@gmail.com>
To: Krzysztof Kozlowski <krzk@kernel.org>
Cc: Christian Vogel <vogelchr@vogel.cx>,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH] w1: ds2490: bound the search results to the found_ids array
Date: Tue, 17 Nov 2026 22:55:34 +0800	[thread overview]
Message-ID: <20261117145534.755665-1-maoyixie.tju@gmail.com> (raw)

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


             reply	other threads:[~2026-11-17 14:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 14:55 Maoyi Xie [this message]
2026-11-17 14:58 ` Krzysztof Kozlowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117145534.755665-1-maoyixie.tju@gmail.com \
    --to=maoyixie.tju@gmail.com \
    --cc=krzk@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=vogelchr@vogel.cx \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®