Skip to contentSkip to conversation

iio: small fixes and improvements

PATCH Applied Nuno Sá 4 participants 8 messages Feb 18, 2025 – Feb 22, 2025

Subsystems (2)
lore
Share & tools
lore

Nuno Sá

author maintainer
#0 lore raw

cover iio: small fixes and improvements

The first patch is a fix for the backend code dealing with direct_register_access. We need to properly terminate the received string before passing it to sscanf().

The second patch is about using the simple_write_to_buffer() which makes the direct_register_access write side more symmetric with the read side.

---
Nuno Sá (2):
iio: backend: make sure to NULL terminate stack buffer
iio: core: make use of simple_write_to_buffer()

 drivers/iio/industrialio-backend.c | 4 +++-
 drivers/iio/industrialio-core.c    | 9 +++++----
 2 files changed, 8 insertions(+), 5 deletions(-)

---
base-commit: c0f115a8d97599623294c8e9ec28530e19c1e85b
change-id: 20250218-dev-iio-misc-d78425a94985 --

Thanks!
- Nuno Sá

author maintainer
#1 lore raw

PATCH 1/2 iio: backend: make sure to NULL terminate stack buffer

Make sure to NULL terminate the buffer in iio_backend_debugfs_write_reg() before passing it to sscanf(). It is a stack variable so we should not assume it will 0 initialized.

Fixes cdf01e0809a4 ("iio: backend: add debugFs interface")
---
 drivers/iio/industrialio-backend.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/industrialio-backend.c +3 -1
index d4ad36f54090204bf3bef08457d4aa55aa7c11fc..a43c8d1bb3d0f4dda4277cac94b0ea9232c071e4 100644
@@ -155,10 +155,12 @@ static ssize_t iio_backend_debugfs_write_reg(struct file *file,
 	ssize_t rc;
 	int ret;
 
-	rc = simple_write_to_buffer(buf, sizeof(buf), ppos, userbuf, count);
+	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf, count);
 	if (rc < 0)
 		return rc;
 
+	buf[count] = '\0';
+
 	ret = sscanf(buf, "%i %i", &back->cached_reg_addr, &val);
 
 	switch (ret) {
Signature · 1 line
2.48.1
reviewer
#2 lore raw

Re: [PATCH 1/2] iio: backend: make sure to NULL terminate stack buffer

Quoted 17 lines · On 2/18/25 4:31 AM, Nuno Sá via B4 Relay wrote:

From: Nuno Sá <[email protected]>

Make sure to NULL terminate the buffer in iio_backend_debugfs_write_reg() before passing it to sscanf(). It is a stack variable so we should not assume it will 0 initialized.

Fixes cdf01e0809a4 ("iio: backend: add debugFs interface")
---
 drivers/iio/industrialio-backend.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/industrialio-backend.c +0 -0
index d4ad36f54090204bf3bef08457d4aa55aa7c11fc..a43c8d1bb3d0f4dda4277cac94b0ea9232c071e4 100644

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

Quoted 9 lines
drivers/iio/industrialio-backend.c +2 -1
@@ -155,10 +155,12 @@ static ssize_t iio_backend_debugfs_write_reg(struct file *file,
 	ssize_t rc;
 	int ret;
 
-	rc = simple_write_to_buffer(buf, sizeof(buf), ppos, userbuf, count);
+	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf, count);
 	if (rc < 0)
 		return rc;
 
+	buf[count] = '\0';

Does this need to be count++? Later we return count.

Quoted 5 lines
drivers/iio/industrialio-backend.c +1 -0
+
 	ret = sscanf(buf, "%i %i", &back->cached_reg_addr, &val);
 
 	switch (ret) {
#3 lore raw

Re: [PATCH 1/2] iio: backend: make sure to NULL terminate stack buffer

Quoted 27 lines · David Lechner
Quoted 26 lines · On 2/18/25 4:31 AM, Nuno Sá via B4 Relay wrote:

From: Nuno Sá <[email protected]>

Make sure to NULL terminate the buffer in iio_backend_debugfs_write_reg() before passing it to sscanf(). It is a stack variable so we should not assume it will 0 initialized.

Fixes cdf01e0809a4 ("iio: backend: add debugFs interface")

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

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

backend.c
index d4ad36f54090204bf3bef08457d4aa55aa7c11fc..a43c8d1bb3d0f4dda4277cac94b0ea9232 c071e4 100644

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

drivers/iio/industrialio-backend.c +0 -1
-	rc = simple_write_to_buffer(buf, sizeof(buf), ppos, userbuf,

count);

Quoted 9 lines

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

Quoted 6 lines
drivers/iio/industrialio-backend.c +1 -0
+	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf,

count);
  if (rc < 0)
  return rc;

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

Does this need to be count++? Later we return count.

Don't think so... count comes down from userspace. The termination is local so we do not want to return count + 1 when userspace only requested to write count. Same deal as in iio_debugfs_write_reg()

Also note that we pass sizeof(buf) - 1 into simple_write_to_buffer()

- Nuno Sá

Quoted 1 line
reviewer
#4 lore raw

Re: [PATCH 1/2] iio: backend: make sure to NULL terminate stack buffer

Quoted 38 lines · Nuno Sá
Quoted 27 lines · David Lechner
Quoted 26 lines · On 2/18/25 4:31 AM, Nuno Sá via B4 Relay wrote:

From: Nuno Sá <[email protected]>

Make sure to NULL terminate the buffer in iio_backend_debugfs_write_reg() before passing it to sscanf(). It is a stack variable so we should not assume it will 0 initialized.

Fixes cdf01e0809a4 ("iio: backend: add debugFs interface")

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

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

backend.c
index d4ad36f54090204bf3bef08457d4aa55aa7c11fc..a43c8d1bb3d0f4dda4277cac94b0ea9232 c071e4 100644

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

drivers/iio/industrialio-backend.c +0 -1
-	rc = simple_write_to_buffer(buf, sizeof(buf), ppos, userbuf,

count);

Quoted 9 lines

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

Quoted 6 lines
drivers/iio/industrialio-backend.c +1 -0
+	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf,

count);
  if (rc < 0)
  return rc;

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

Does this need to be count++? Later we return count.

Quoted 8 lines

Don't think so... count comes down from userspace. The termination is local so we do not want to return count + 1 when userspace only requested to write count. Same deal as in iio_debugfs_write_reg()

Also note that we pass sizeof(buf) - 1 into simple_write_to_buffer()

- Nuno Sá

Quoted 1 line

Ah, right. I get it now. Wasn't thinking so clear earlier.

author maintainer
#5 lore raw

PATCH 2/2 iio: core: make use of simple_write_to_buffer()

In reply to Nuno Sá

Instead of open coding (kind of) simple_write_to_buffer(), use it.

While at it, use ascii representation to terminate the string as that is the more common way of doing it.

---
 drivers/iio/industrialio-core.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/iio/industrialio-core.c +5 -4
index a2117ad1337d55468c0f21c274fcbedd352c97ff..b9f4113ae5fc3ee1ef76be6808cc437286690dae 100644
@@ -410,11 +410,12 @@ static ssize_t iio_debugfs_write_reg(struct file *file,
 	char buf[80];
 	int ret;
 
-	count = min(count, sizeof(buf) - 1);
-	if (copy_from_user(buf, userbuf, count))
-		return -EFAULT;
+	ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf,
+				     count);
+	if (ret < 0)
+		return ret;
 
-	buf[count] = 0;
+	buf[count] = '\0';
 
 	ret = sscanf(buf, "%i %i", &reg, &val);
 
Signature · 1 line
2.48.1
reviewer Reviewed-by on the v1 series
#6 lore raw
In reply to Nuno Sá
Quoted 8 lines · On 2/18/25 4:31 AM, Nuno Sá via B4 Relay wrote:

The first patch is a fix for the backend code dealing with direct_register_access. We need to properly terminate the received string before passing it to sscanf().

The second patch is about using the simple_write_to_buffer() which makes the direct_register_access write side more symmetric with the read side.

---

maintainer Applied
#7 lore raw
Quoted 5 lines · On Tue, 18 Feb 2025 11:29:08 -0600 David Lechner <[email protected]> wrote:
Quoted 4 lines · On 2/18/25 4:31 AM, Nuno Sá via B4 Relay wrote:

The first patch is a fix for the backend code dealing with direct_register_access. We need to properly terminate the received string before passing it to sscanf().

Quoted 7 lines
Quoted 4 lines

The second patch is about using the simple_write_to_buffer() which makes the direct_register_access write side more symmetric with the read side.

---

Applied