Skip to contentSkip to conversation

remoteproc: virtio: fix uninitialized buffer use in rproc_virtio_get()

PATCH In review Kery Qi 3 participants 3 messages Jan 8 – Jan 12 Remote processor (remoteproc)

lore
Share & tools
lore

Kery Qi

author
#0 lore raw

PATCH 1/1 remoteproc: virtio: fix uninitialized buffer use in rproc_virtio_get()

rproc_virtio_get() returns early on out-of-bounds access without touching the caller-provided buffer. Callers of virtio config accessors commonly pass stack storage and do not get an error code back, so stale/uninit bytes may be consumed as device configuration, leading to unpredictable behavior and potentially leaking stack data if later exposed.

Always clear the destination buffer, reject offsets past config_len, and clamp the read length to the available config bytes before copying.

Fixes 92b38f851470 ("remoteproc: support virtio config space.")
---
 drivers/remoteproc/remoteproc_virtio.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/remoteproc/remoteproc_virtio.c +6 -3
index c5d46a878149..8fa8c8a86b4b 100644
@@ -298,10 +298,13 @@ static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset,
 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
 	cfg = &rsc->vring[rsc->num_of_vrings];
 
-	if (offset + len > rsc->config_len || offset + len < len) {
-		dev_err(&vdev->dev, "rproc_virtio_get: access out of bounds\n");
+	memset(buf, 0, len);
+
+	if (offset > rsc->config_len)
 		return;
-	}
+
+		if (len > rsc->config_len - offset)
+			len = rsc->config_len - offset;
 
 	memcpy(buf, cfg + offset, len);
 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 11 lines · Kery Qi

rproc_virtio_get() returns early on out-of-bounds access without touching the caller-provided buffer. Callers of virtio config accessors commonly pass stack storage and do not get an error code back, so stale/uninit bytes may be consumed as device configuration, leading to unpredictable behavior and potentially leaking stack data if later exposed.

Always clear the destination buffer, reject offsets past config_len, and clamp the read length to the available config bytes before copying.

Fixes 92b38f851470 ("remoteproc: support virtio config space.")

Please use ./scripts/get_maintainer.pl to retrieve the full list of maintainer email and cc list.

drivers/remoteproc/remoteproc_virtio.c static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset, · line 299

Quoted 11 lines
---
  drivers/remoteproc/remoteproc_virtio.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/remoteproc/remoteproc_virtio.c +0 -0
index c5d46a878149..8fa8c8a86b4b 100644
@@ -298,10 +298,13 @@ static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset,
  	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  	cfg = &rsc->vring[rsc->num_of_vrings];

Hi Kery,

You want to initialize the buffer to avoid issues caused by calling the virtio_cread macro with an uninitialized variable buffer, which should be fair enough,

virtio_cread(vdev, structname, member, ptr)

	vdev->config->get()
	OR
	__virtio_cread_many()
		vdev->config->get()

drivers/remoteproc/remoteproc_virtio.c static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset, · line 302

Quoted 5 lines
drivers/remoteproc/remoteproc_virtio.c +2 -2
-	if (offset + len > rsc->config_len || offset + len < len) {
-		dev_err(&vdev->dev, "rproc_virtio_get: access out of bounds\n");
+	memset(buf, 0, len);
+

The original check offset + len > rsc->config_len ensures that access to the config space does not overflow, while offset + len < len is used to detect unsigned integer overflow.

drivers/remoteproc/remoteproc_virtio.c static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset, · line 303

Quoted 1 line
drivers/remoteproc/remoteproc_virtio.c +1 -0
+	if (offset > rsc->config_len)

Any log info?

drivers/remoteproc/remoteproc_virtio.c static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset, · line 304

Quoted 2 lines
drivers/remoteproc/remoteproc_virtio.c +0 -1
  		return;
-	}

And then may I know what does this really mean? Are you suggesting a quick fast path check or something else? Please clarify the reasoning behind this, and if that makes sense, please split the initialization and the boundary check into two separate patches.

drivers/remoteproc/remoteproc_virtio.c static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset, · line 305

Quoted 2 lines
drivers/remoteproc/remoteproc_virtio.c +2 -0
+
+		if (len > rsc->config_len - offset)

Formatting issue here.

drivers/remoteproc/remoteproc_virtio.c static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset, · line 307

Quoted 1 line
drivers/remoteproc/remoteproc_virtio.c +1 -0
+			len = rsc->config_len - offset;

Why is truncation allowed here? Any err log info?

Quoted 3 lines
drivers/remoteproc/remoteproc_virtio.c +0 -0
  	memcpy(buf, cfg + offset, len);
  }
Signature · 2 lines
Thx and BRs,
Zhongqiu Han
maintainer
#2 lore raw
In reply to Kery Qi

Good day Kery,

Quoted 20 lines · Kery Qi

rproc_virtio_get() returns early on out-of-bounds access without touching the caller-provided buffer. Callers of virtio config accessors commonly pass stack storage and do not get an error code back, so stale/uninit bytes may be consumed as device configuration, leading to unpredictable behavior and potentially leaking stack data if later exposed.

Always clear the destination buffer, reject offsets past config_len, and clamp the read length to the available config bytes before copying.

Fixes 92b38f851470 ("remoteproc: support virtio config space.")
---
 drivers/remoteproc/remoteproc_virtio.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/remoteproc/remoteproc_virtio.c +0 -0
index c5d46a878149..8fa8c8a86b4b 100644

drivers/remoteproc/remoteproc_virtio.c static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset, · line 301

Quoted 6 lines
drivers/remoteproc/remoteproc_virtio.c +1 -2
@@ -298,10 +298,13 @@ static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset,
 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
 	cfg = &rsc->vring[rsc->num_of_vrings];
 
-	if (offset + len > rsc->config_len || offset + len < len) {
-		dev_err(&vdev->dev, "rproc_virtio_get: access out of bounds\n");
+	memset(buf, 0, len);

I think this should be done in the caller, not virtio_config_ops->get(). I am willing to reconsider if you include this change in a patchset that changes all the other virtio_config_ops->get() function.

Thanks,
Mathieu

Quoted 14 lines
drivers/remoteproc/remoteproc_virtio.c +5 -1
+
+	if (offset > rsc->config_len)
 		return;
-	}
+
+		if (len > rsc->config_len - offset)
+			len = rsc->config_len - offset;
 
 	memcpy(buf, cfg + offset, len);
 }
Signature · 1 line
2.34.1