mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] misc: ds1682: fix out-of-bounds access in EEPROM functions
@ 2025-08-09 15:54 wajahat iqbal
  2025-08-09 19:54 ` Christophe JAILLET
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: wajahat iqbal @ 2025-08-09 15:54 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

Found a couple of issues in the ds1682 driver while reviewing the code:

The EEPROM read/write functions don't check if offset and count exceed
the 10-byte EEPROM size, which could lead to out-of-bounds I2C access.

Also replaced sprintf with scnprintf in the sysfs show function for
better safety.

For reads beyond EEPROM size, return 0. For writes, return -EINVAL if
starting beyond bounds, otherwise truncate to fit within the EEPROM.

Signed-off-by: Wajahat Iqbal <wajahatiqbal22@gmail.com>
---
 drivers/misc/ds1682.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/ds1682.c b/drivers/misc/ds1682.c
index cb09e056531a..4cf4b43e5355 100644
--- a/drivers/misc/ds1682.c
+++ b/drivers/misc/ds1682.c
@@ -92,7 +92,7 @@ static ssize_t ds1682_show(struct device *dev,
struct device_attribute *attr,
  * Special case: the 32 bit regs are time values with 1/4s
  * resolution, scale them up to milliseconds
  */
- return sprintf(buf, "%llu\n", (sattr->nr == 4) ? (val * 250) : val);
+ return scnprintf(buf, PAGE_SIZE, "%llu\n", (sattr->nr == 4) ? (val *
250) : val);
 }

 static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr,
@@ -163,6 +163,11 @@ static ssize_t ds1682_eeprom_read(struct file
*filp, struct kobject *kobj,
  dev_dbg(&client->dev, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
  buf, off, count);

+ if (off >= DS1682_EEPROM_SIZE)
+ return 0;
+ if (off + count > DS1682_EEPROM_SIZE)
+ count = DS1682_EEPROM_SIZE - off;
+
  rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off,
     count, buf);
  if (rc < 0)
@@ -180,6 +185,11 @@ static ssize_t ds1682_eeprom_write(struct file
*filp, struct kobject *kobj,
  dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
  buf, off, count);

+ if (off >= DS1682_EEPROM_SIZE)
+ return -EINVAL;
+ if (off + count > DS1682_EEPROM_SIZE)
+ count = DS1682_EEPROM_SIZE - off;
+
  /* Write out to the device */
  if (i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + off,
     count, buf) < 0)
-- 
2.49.0.windows.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-08-10  6:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-09 15:54 [PATCH] misc: ds1682: fix out-of-bounds access in EEPROM functions wajahat iqbal
2025-08-09 19:54 ` Christophe JAILLET
2025-08-10  0:47 ` wajahat iqbal
2025-08-10  0:50 ` Clarification on SEAM Interface Usage and Patch Scope wajahat iqbal
2025-08-10  6:53 ` [PATCH] misc: ds1682: fix out-of-bounds access in EEPROM functions Greg KH

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®