Skip to contentSkip to conversation

virtio: rng: Handle oversized return buffers

PATCH In review Meet Patel 3 participants 6 messages Mar 9 – Mar 25 virtio core

lore
Share & tools
lore

Meet Patel

author
#0 lore raw

v2 cover virtio: rng: Handle oversized return buffers

The virtio-rng test to verify effective handling of oversized return buffers checks that an (undocumented) error is raised, instead of the real concern, which is the surrounding buffer integrity following a rng function call.
Update the test to check that the other contents of a buffer remain unchanged instead of looking for an error code.

Changes in v2:
- Avoid filling whole buffer just to overwrite in next line.

Kavin Gunasekara (2):
test: dm: virtio_rng: Update virtio-rng test
virtio: rng: Handle oversized return buffers

 drivers/virtio/virtio_rng.c |  2 +-
 test/dm/virtio_rng.c        | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 3 deletions(-)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v2 1/2 test: dm: virtio_rng: Update virtio-rng test

From: Kavin Gunasekara <[email protected]>

The virtio-rng test to verify effective handling of oversized return buffers checks that an (undocumented) error is raised, instead of the real concern, which is the surrounding buffer integrity following a rng function call.
Update the test to check that the other contents of a buffer remain unchanged instead of looking for an error code.

---
 test/dm/virtio_rng.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
test/dm/virtio_rng.c +12 -2
index e404b08484e..5fd56ade586 100644
@@ -19,12 +19,19 @@ struct virtio_rng_priv {
 	struct virtqueue *rng_vq;
 };
 
+#define BUFFER_SIZE 16
+#define CANARY "CANARYCANARYCANARYCANARY"
+
 /* Test the virtio-rng driver validates the used size */
 static int dm_test_virtio_rng_check_len(struct unit_test_state *uts)
 {
 	struct udevice *bus, *dev;
 	struct virtio_rng_priv *priv;
-	u8 buffer[16];
+	u8 buffer[BUFFER_SIZE + sizeof(CANARY)];
+
+	/* write known data to buffer */
+	memset(buffer, 0xaa, BUFFER_SIZE);
+	memcpy(buffer + BUFFER_SIZE, CANARY, sizeof(CANARY));
 
 	/* check probe success */
 	ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
@@ -44,7 +51,10 @@ static int dm_test_virtio_rng_check_len(struct unit_test_state *uts)
 	priv->rng_vq->vring.used->ring[0].len = U32_MAX;
 
 	/* check the driver gracefully handles the error */
-	ut_asserteq(-EIO, dm_rng_read(dev, buffer, sizeof(buffer)));
+	dm_rng_read(dev, buffer, BUFFER_SIZE);
+
+	/* check for the canary bytes behind the real buffer */
+	ut_asserteq_mem(buffer + BUFFER_SIZE, CANARY, sizeof(CANARY));
 
 	return 0;
 }
Signature · 1 line
2.53.0
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] test: dm: virtio_rng: Update virtio-rng test

Hi Meet,

Quoted 8 lines · Meet Patel

From: Kavin Gunasekara <[email protected]>

The virtio-rng test to verify effective handling of oversized return buffers checks that an (undocumented) error is raised, instead of the real concern, which is the surrounding buffer integrity following a rng function call.
Update the test to check that the other contents of a buffer remain unchanged instead of looking for an error code.

thanks for the change, looks good now!

Quoted 2 lines

Cheers,
Andre

Quoted 41 lines
---
  test/dm/virtio_rng.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)
test/dm/virtio_rng.c +12 -2
index e404b08484e..5fd56ade586 100644
@@ -19,12 +19,19 @@ struct virtio_rng_priv {
  	struct virtqueue *rng_vq;
  };
 
+#define BUFFER_SIZE 16
+#define CANARY "CANARYCANARYCANARYCANARY"
+
  /* Test the virtio-rng driver validates the used size */
  static int dm_test_virtio_rng_check_len(struct unit_test_state *uts)
  {
  	struct udevice *bus, *dev;
  	struct virtio_rng_priv *priv;
-	u8 buffer[16];
+	u8 buffer[BUFFER_SIZE + sizeof(CANARY)];
+
+	/* write known data to buffer */
+	memset(buffer, 0xaa, BUFFER_SIZE);
+	memcpy(buffer + BUFFER_SIZE, CANARY, sizeof(CANARY));
 
  	/* check probe success */
  	ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
@@ -44,7 +51,10 @@ static int dm_test_virtio_rng_check_len(struct unit_test_state *uts)
  	priv->rng_vq->vring.used->ring[0].len = U32_MAX;
 
  	/* check the driver gracefully handles the error */
-	ut_asserteq(-EIO, dm_rng_read(dev, buffer, sizeof(buffer)));
+	dm_rng_read(dev, buffer, BUFFER_SIZE);
+
+	/* check for the canary bytes behind the real buffer */
+	ut_asserteq_mem(buffer + BUFFER_SIZE, CANARY, sizeof(CANARY));
 
  	return 0;
  }
author
#3 lore raw

PATCH v2 2/2 virtio: rng: Handle oversized return buffers

In reply to Meet Patel

From: Kavin Gunasekara <[email protected]>

If extra random bytes are returned, truncate and use the requested number instead of returning an error.

---
 drivers/virtio/virtio_rng.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/virtio/virtio_rng.c +1 -1
index 90a371a59cc..c6de62142bb 100644
@@ -46,7 +46,7 @@ static int virtio_rng_read(struct udevice *dev, void *data, size_t len)
 			;
 
 		if (rsize > sg.length)
-			return -EIO;
+			rsize = sg.length;
 
 		memcpy(ptr, buf, rsize);
 		len -= rsize;
Signature · 1 line
2.53.0
Reviewed-by on v2 2/2
#4 lore raw

Hi,

looks like I gave my Reviewed-by tag already on v1, please remember to carry that over in a subsequent submission. For the records:

Quoted 7 lines · Meet Patel

From: Kavin Gunasekara <[email protected]>

If extra random bytes are returned, truncate and use the requested number instead of returning an error.

Cheers,
Andre

Quoted 17 lines
---
  drivers/virtio/virtio_rng.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/virtio/virtio_rng.c +1 -1
index 90a371a59cc..c6de62142bb 100644
@@ -46,7 +46,7 @@ static int virtio_rng_read(struct udevice *dev, void *data, size_t len)
  			;
 
  		if (rsize > sg.length)
-			return -EIO;
+			rsize = sg.length;
 
  		memcpy(ptr, buf, rsize);
  		len -= rsize;
#5 lore raw
In reply to Meet Patel
Quoted 8 lines · Meet Patel

The virtio-rng test to verify effective handling of oversized return buffers checks that an (undocumented) error is raised, instead of the real concern, which is the surrounding buffer integrity following a rng function call.
Update the test to check that the other contents of a buffer remain unchanged instead of looking for an error code.

[...]

Applied to u-boot/next, thanks!

[1/2] test: dm: virtio_rng: Update virtio-rng test

      commit: 8019e3b35df5e780a722fbcb1bf6b2f63eaccc9f
[2/2] virtio: rng: Handle oversized return buffers
      commit: e0eef12a1f46bf946e24050a1da0f067c3952b89
Signature · 1 line
Tom