The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Krzysztof Kozlowski <krzk@kernel.org>,
	Evgeniy Polyakov <zbr@ioremap.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 3/6] w1: Return actual bytes read from w1_read_block() and rw_read()
Date: Wed,  2 Dec 2026 09:36:45 +0000	[thread overview]
Message-ID: <cdafe0234e4cdb03ea28f75efd565fedd29d6cab.1796204137.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796204137.git.lee@kernel.org>

rw_read() discards the return value of w1_read_block() and always
returns the requested count.  When a bus master's read_block callback
fails (for instance ds9490r_read_block() returning 0 on a USB error or
allocation failure), buf is left untouched and rw_read() exposes up to
PAGE_SIZE bytes of uninitialised kernel heap memory to userspace.  In
addition, w1_bus_master::read_block, w1_read_block(), and
ds9490r_read_block() return u8, which truncates counts of 256 bytes or
more (such as a PAGE_SIZE read).

Widen the return type of read_block, w1_read_block(), and
ds9490r_read_block() from u8 to int, return 0 in w1_read_block() when
len is non-positive, and return the byte count from w1_read_block() in
rw_read().

Fixes: f522d2396138 ("[PATCH] w1: Added default generic read/write operations.")
Closes: https://sashiko.dev/#/bug/linux-ea892b13-33ef-4375-ae35-db4cafb332c0
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/w1/masters/ds2490.c | 2 +-
 drivers/w1/w1.c             | 2 +-
 drivers/w1/w1_io.c          | 7 +++++--
 include/linux/w1.h          | 4 ++--
 4 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/drivers/w1/masters/ds2490.c b/drivers/w1/masters/ds2490.c
index b4e1415573c1..b40145441fd5 100644
--- a/drivers/w1/masters/ds2490.c
+++ b/drivers/w1/masters/ds2490.c
@@ -921,7 +921,7 @@ static void ds9490r_write_block(void *data, const u8 *buf, int len)
 	kfree(tbuf);
 }
 
-static u8 ds9490r_read_block(void *data, u8 *buf, int len)
+static int ds9490r_read_block(void *data, u8 *buf, int len)
 {
 	struct ds_device *dev = data;
 	int err;
diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index 048cf8cb7f6b..7ed2a01c8571 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -142,7 +142,7 @@ static ssize_t rw_read(struct file *filp, struct kobject *kobj,
 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
 
 	mutex_lock(&sl->master->bus_mutex);
-	w1_read_block(sl->master, buf, count);
+	count = w1_read_block(sl->master, buf, count);
 	mutex_unlock(&sl->master->bus_mutex);
 	return count;
 }
diff --git a/drivers/w1/w1_io.c b/drivers/w1/w1_io.c
index db3c9522a8a2..445a60a18e6c 100644
--- a/drivers/w1/w1_io.c
+++ b/drivers/w1/w1_io.c
@@ -293,10 +293,13 @@ EXPORT_SYMBOL_GPL(w1_touch_block);
  * @len:	the number of bytes to read
  * Return:	the number of bytes read
  */
-u8 w1_read_block(struct w1_master *dev, u8 *buf, int len)
+int w1_read_block(struct w1_master *dev, u8 *buf, int len)
 {
 	int i;
-	u8 ret;
+	int ret;
+
+	if (len <= 0)
+		return 0;
 
 	if (dev->bus_master->read_block)
 		ret = dev->bus_master->read_block(dev->bus_master->data, buf, len);
diff --git a/include/linux/w1.h b/include/linux/w1.h
index 064805bfae3f..985537c51a27 100644
--- a/include/linux/w1.h
+++ b/include/linux/w1.h
@@ -142,7 +142,7 @@ struct w1_bus_master {
 
 	void		(*write_byte)(void *, u8);
 
-	u8		(*read_block)(void *, u8 *, int);
+	int		(*read_block)(void *data, u8 *buf, int len);
 
 	void		(*write_block)(void *, const u8 *, int);
 
@@ -300,7 +300,7 @@ int w1_reset_bus(struct w1_master *);
 u8 w1_calc_crc8(u8 *, int);
 void w1_write_block(struct w1_master *, const u8 *, int);
 void w1_touch_block(struct w1_master *, u8 *, int);
-u8 w1_read_block(struct w1_master *, u8 *, int);
+int w1_read_block(struct w1_master *dev, u8 *buf, int len);
 int w1_reset_select_slave(struct w1_slave *sl);
 int w1_reset_resume_command(struct w1_master *);
 void w1_next_pullup(struct w1_master *, int);
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-02  9:38 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:36 [PATCH 0/6] w1: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-02  9:36 ` [PATCH 1/6] w1: Free slave structure in w1_slave_release() Lee Jones
2026-12-02 10:38   ` Krzysztof Kozlowski
2026-12-02 10:43     ` Krzysztof Kozlowski
2026-12-02  9:36 ` [PATCH 2/6] w1: Synchronise thread teardown with netlink callbacks Lee Jones
2026-12-02 10:38   ` Krzysztof Kozlowski
2026-12-02  9:36 ` Lee Jones [this message]
2026-12-02 10:54   ` [PATCH 3/6] w1: Return actual bytes read from w1_read_block() and rw_read() Krzysztof Kozlowski
2026-12-02  9:36 ` [PATCH 4/6] w1: Protect w1_masters traversal with w1_mlock during removal Lee Jones
2026-12-02 10:44   ` Krzysztof Kozlowski
2026-12-02  9:36 ` [PATCH 5/6] w1: ds28e04: Always allocate family data to avoid NULL dereference Lee Jones
2026-12-02 10:45   ` Krzysztof Kozlowski
2026-12-02  9:36 ` [PATCH 6/6] w1: sgi: Add timeout to sgi_w1_wait() polling loop Lee Jones
2026-12-02 10:46   ` Krzysztof Kozlowski
2026-12-02  9:45 ` [PATCH 0/6] w1: Fix Critical and High issues reported by Sashiko Greg Kroah-Hartman
2026-12-02 10:23   ` Lee Jones
2026-12-02 10:30     ` Greg Kroah-Hartman

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=cdafe0234e4cdb03ea28f75efd565fedd29d6cab.1796204137.git.lee@kernel.org \
    --to=lee@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=krzk@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=zbr@ioremap.net \
    /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