Skip to contentSkip to conversation

iio: backend: fix out-of-bound write

PATCH Superseded Markus Burri 3 participants 5 messages May 5, 2025 – May 7, 2025

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 iio: backend: fix out-of-bound write

The buffer is set to 80 character. If a caller write more characters, count is truncated to the max available space in "simple_write_to_buffer". But afterwards a string terminator is written to the buffer at offset count without boundary check. The zero termination is written OUT-OF-BOUND.

Add a check that the given buffer is smaller then the buffer to prevent.

Fixes 035b4989211d ("iio: backend: make sure to NULL terminate stack buffer")
---
 drivers/iio/industrialio-backend.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/industrialio-backend.c +4 -1
index a43c8d1bb3d0..4a364e038449 100644
@@ -155,11 +155,14 @@ static ssize_t iio_backend_debugfs_write_reg(struct file *file,
 	ssize_t rc;
 	int ret;
 
+	if (count >= sizeof(buf) - 1)
+		return -ENOSPC;
+
 	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf, count);
 	if (rc < 0)
 		return rc;
 
-	buf[count] = '\0';
+	buf[rc] = '\0';
 
 	ret = sscanf(buf, "%i %i", &back->cached_reg_addr, &val);
 

base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e

Signature · 1 line
2.39.5
Reviewed-by on v3 1/1
#1 lore raw
Quoted 10 lines · Markus Burri

The buffer is set to 80 character. If a caller write more characters, count is truncated to the max available space in "simple_write_to_buffer". But afterwards a string terminator is written to the buffer at offset count without boundary check. The zero termination is written OUT-OF-BOUND.

Add a check that the given buffer is smaller then the buffer to prevent.

Fixes 035b4989211d ("iio: backend: make sure to NULL terminate stack buffer")

---

LGTM

Quoted 20 lines

 drivers/iio/industrialio-backend.c | 5 ++++-

 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/industrialio- renamed from drivers/iio/industrialio-backend.c +0 -0

backend.c
index a43c8d1bb3d0..4a364e038449 100644

drivers/iio/industrialio-backend.c +0 -0
@@ -155,11 +155,14 @@ static ssize_t iio_backend_debugfs_write_reg(struct file
 *file,
  	ssize_t rc;
  	int ret;

drivers/iio/industrialio-backend.c +3 -0
+	if (count >= sizeof(buf) - 1)
+		return -ENOSPC;
+

  rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf, count); if (rc < 0)
  return rc;

drivers/iio/industrialio-backend.c static ssize_t iio_backend_debugfs_write_reg(struct file *file, · line 165

Quoted 2 lines
drivers/iio/industrialio-backend.c +1 -1
-	buf[count] = '\0';
+	buf[rc] = '\0';

you could have mentioned this change in the commit message...

Quoted 5 lines

  ret = sscanf(buf, "%i %i", &back->cached_reg_addr, &val);

base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e

reviewer
#2 lore raw
In reply to Markus Burri
Quoted 18 lines · Markus Burri

The buffer is set to 80 character. If a caller write more characters, count is truncated to the max available space in "simple_write_to_buffer". But afterwards a string terminator is written to the buffer at offset count without boundary check. The zero termination is written OUT-OF-BOUND.

Add a check that the given buffer is smaller then the buffer to prevent.

Fixes 035b4989211d ("iio: backend: make sure to NULL terminate stack buffer")
---
 drivers/iio/industrialio-backend.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/industrialio-backend.c +0 -0
index a43c8d1bb3d0..4a364e038449 100644

drivers/iio/industrialio-backend.c static ssize_t iio_backend_debugfs_write_reg(struct file *file, · line 158

Quoted 4 lines
drivers/iio/industrialio-backend.c +1 -0
@@ -155,11 +155,14 @@ static ssize_t iio_backend_debugfs_write_reg(struct file *file,
 	ssize_t rc;
 	int ret;
 
+	if (count >= sizeof(buf) - 1)

Isn't it OK if count == sizeof(buf) - 1? In other words, should be:

if (count >= sizeof(buf))

Quoted 12 lines
drivers/iio/industrialio-backend.c +3 -1
+		return -ENOSPC;
+
 	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf, count);
 	if (rc < 0)
 		return rc;
 
-	buf[count] = '\0';
+	buf[rc] = '\0';
 
 	ret = sscanf(buf, "%i %i", &back->cached_reg_addr, &val);
 
Quoted 1 line

base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e

It looks like we have the same or similar bugs in:

drivers/accel/ivpu/ivpu_debugfs.c
drivers/gpio/gpio-virtuser.c
drivers/iio/industrialio-core.c
drivers/iio/dac/ad3552r-hs.c

Do you plan to fix these as well?

#3 lore raw

Re: EXTERNAL - [PATCH v3] iio: backend: fix out-of-bound write

Quoted 22 lines · David Lechner
Quoted 21 lines · Markus Burri

The buffer is set to 80 character. If a caller write more characters, count is truncated to the max available space in "simple_write_to_buffer". But afterwards a string terminator is written to the buffer at offset count without boundary check. The zero termination is written OUT-OF-BOUND.

Add a check that the given buffer is smaller then the buffer to prevent.

Fixes 035b4989211d ("iio: backend: make sure to NULL terminate stack buffer")
---
 drivers/iio/industrialio-backend.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/industrialio-backend.c +0 -0
index a43c8d1bb3d0..4a364e038449 100644
@@ -155,11 +155,14 @@ static ssize_t iio_backend_debugfs_write_reg(struct file *file,
 	ssize_t rc;
 	int ret;
 
Quoted 6 lines

drivers/iio/industrialio-backend.c static ssize_t iio_backend_debugfs_write_reg(struct file *file, · line 158

Quoted 1 line
drivers/iio/industrialio-backend.c +1 -0
+	if (count >= sizeof(buf) - 1)

Isn't it OK if count == sizeof(buf) - 1? In other words, should be:

if (count >= sizeof(buf))

This was my original patch and I think it is OK.
In a situation we have 79 characters and the last one ('\n'), the '\n' will be replaces by a '\0', therefore it is OK.
Since the given text should anyway be < buffer size and it is a little more correct to have the -1, I would keep it.

Quoted 16 lines
Quoted 12 lines
drivers/iio/industrialio-backend.c +3 -1
+		return -ENOSPC;
+
 	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf, count);
 	if (rc < 0)
 		return rc;
 
-	buf[count] = '\0';
+	buf[rc] = '\0';
 
 	ret = sscanf(buf, "%i %i", &back->cached_reg_addr, &val);
 
Quoted 1 line

base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e

It looks like we have the same or similar bugs in:

Quoted 6 lines

drivers/accel/ivpu/ivpu_debugfs.c
drivers/gpio/gpio-virtuser.c
drivers/iio/industrialio-core.c
drivers/iio/dac/ad3552r-hs.c

Do you plan to fix these as well?

True there are some more. I will check them later.

#4 lore raw
In reply to David Lechner
Quoted 28 lines · David Lechner
Quoted 23 lines · Markus Burri

The buffer is set to 80 character. If a caller write more characters, count is truncated to the max available space in "simple_write_to_buffer". But afterwards a string terminator is written to the buffer at offset count without boundary check. The zero termination is written OUT-OF-BOUND.

Add a check that the given buffer is smaller then the buffer to prevent.

Fixes 035b4989211d ("iio: backend: make sure to NULL terminate stack buffer")

---
 drivers/iio/industrialio-backend.c | 5 ++++-

 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/industrialio- renamed from drivers/iio/industrialio-backend.c +0 -0

backend.c
index a43c8d1bb3d0..4a364e038449 100644

drivers/iio/industrialio-backend.c +0 -0
@@ -155,11 +155,14 @@ static ssize_t iio_backend_debugfs_write_reg(struct file
 *file,
  	ssize_t rc;
  	int ret;

drivers/iio/industrialio-backend.c static ssize_t iio_backend_debugfs_write_reg(struct file *file, · line 158

Quoted 1 line
drivers/iio/industrialio-backend.c +1 -0
+	if (count >= sizeof(buf) - 1)

Isn't it OK if count == sizeof(buf) - 1? In other words, should be:

Quoted 1 line

if (count >= sizeof(buf))

Oh, indeed you're right. Sorry Mark! I was the one asking for it but I did not realized the comparison was '>='. So I was thinking if (count > sizeof(buf) - 1) which is pretty much if (count >= sizeof(buf))

Arghh... Maybe Jonathan can tweak this while applying so you do not have to spin another version because of this.

- Nuno Sá

Quoted 23 lines
Quoted 13 lines
drivers/iio/industrialio-backend.c +2 -0
+		return -ENOSPC;
+

  rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf, count); if (rc < 0)
  return rc;

drivers/iio/industrialio-backend.c +1 -1
-	buf[count] = '\0';
+	buf[rc] = '\0';


  ret = sscanf(buf, "%i %i", &back->cached_reg_addr, &val);

Quoted 1 line

base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e

It looks like we have the same or similar bugs in:

drivers/accel/ivpu/ivpu_debugfs.c
drivers/gpio/gpio-virtuser.c
drivers/iio/industrialio-core.c
drivers/iio/dac/ad3552r-hs.c

Do you plan to fix these as well?