Skip to contentSkip to conversation

vhost-user: avoid g_return_val_if() in get/set_config()

PATCH In review Stefan Hajnoczi 5 participants 18 messages Dec 2, 2020 – Dec 10, 2020 Block layer

lore
Share & tools
lore
#0 lore raw

v2 cover vhost-user: avoid g_return_val_if() in get/set_config()

v2:
* Print errors [Marc-André]

Markus Armbruster pointed out that g_return_val_if() is meant for programming errors. It must not be used for input validation since it can be compiled out. Use explicit if statements instead.

This patch series converts vhost-user device backends that use g_return_val_if() in get/set_config().

Stefan Hajnoczi (4):
contrib/vhost-user-blk: avoid g_return_val_if() input validation contrib/vhost-user-gpu: avoid g_return_val_if() input validation contrib/vhost-user-input: avoid g_return_val_if() input validation block/export: avoid g_return_val_if() input validation

 block/export/vhost-user-blk-server.c    | 6 +++++-
 contrib/vhost-user-blk/vhost-user-blk.c | 6 +++++-
 contrib/vhost-user-gpu/vhost-user-gpu.c | 6 +++++-
 contrib/vhost-user-input/main.c         | 6 +++++-
 4 files changed, 20 insertions(+), 4 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH v2 1/4 contrib/vhost-user-blk: avoid g_return_val_if() input validation

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 contrib/vhost-user-blk/vhost-user-blk.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
contrib/vhost-user-blk/vhost-user-blk.c +5 -1
index dc981bf945..60e3c9ed37 100644
@@ -404,7 +404,11 @@ vub_get_config(VuDev *vu_dev, uint8_t *config, uint32_t len)
     VugDev *gdev;
     VubDev *vdev_blk;
 
-    g_return_val_if_fail(len <= sizeof(struct virtio_blk_config), -1);
+    if (len > sizeof(struct virtio_blk_config)) {
+        fprintf(stderr, "Invalid get_config len %u, expected <= %zu\n",
+                len, sizeof(struct virtio_blk_config));
+        return -1;
+    }
 
     gdev = container_of(vu_dev, VugDev, parent);
     vdev_blk = container_of(gdev, VubDev, parent);
Signature · 1 line
2.28.0
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] contrib/vhost-user-blk: avoid g_return_val_if() input validation

Quoted 9 lines · Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---

Quoted 26 lines
 contrib/vhost-user-blk/vhost-user-blk.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
contrib/vhost-user-blk/vhost-user-blk.c +5 -1
index dc981bf945..60e3c9ed37 100644
@@ -404,7 +404,11 @@ vub_get_config(VuDev *vu_dev, uint8_t *config,
 uint32_t len)
     VugDev *gdev;
     VubDev *vdev_blk;
 
-    g_return_val_if_fail(len <= sizeof(struct virtio_blk_config), -1);
+    if (len > sizeof(struct virtio_blk_config)) {
+        fprintf(stderr, "Invalid get_config len %u, expected <= %zu\n",
+                len, sizeof(struct virtio_blk_config));
+        return -1;
+    }
 
     gdev = container_of(vu_dev, VugDev, parent);
     vdev_blk = container_of(gdev, VubDev, parent);

--
2.28.0

Signature · 1 line
Marc-André Lureau
Reviewed-by on v2 1/4
#3 lore raw

Re: [PATCH v2 1/4] contrib/vhost-user-blk: avoid g_return_val_if() input validation

In reply to Stefan Hajnoczi
Quoted 9 lines · Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

Quoted 24 lines
---
 contrib/vhost-user-blk/vhost-user-blk.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
contrib/vhost-user-blk/vhost-user-blk.c +5 -2
index dc981bf945..60e3c9ed37 100644
@@ -404,7 +404,11 @@ vub_get_config(VuDev *vu_dev, uint8_t *config, uint32_t len)
     VugDev *gdev;
     VubDev *vdev_blk;
 
-    g_return_val_if_fail(len <= sizeof(struct virtio_blk_config), -1);
+    if (len > sizeof(struct virtio_blk_config)) {
+        fprintf(stderr, "Invalid get_config len %u, expected <= %zu\n",
+                len, sizeof(struct virtio_blk_config));
+        return -1;
+    }
 
     gdev = container_of(vu_dev, VugDev, parent);
     vdev_blk = container_of(gdev, VubDev, parent);
--

2.28.0

Reviewed-by on v2 1/4
#4 lore raw

Re: [PATCH v2 1/4] contrib/vhost-user-blk: avoid g_return_val_if() input validation

In reply to Stefan Hajnoczi
Quoted 11 lines · Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 contrib/vhost-user-blk/vhost-user-blk.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
#5 lore raw

PATCH v2 2/4 contrib/vhost-user-gpu: avoid g_return_val_if() input validation

In reply to Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
contrib/vhost-user-gpu/vhost-user-gpu.c +5 -1
index a019d0a9ac..534bad24d1 100644
@@ -1044,7 +1044,11 @@ vg_get_config(VuDev *dev, uint8_t *config, uint32_t len)
 {
     VuGpu *g = container_of(dev, VuGpu, dev.parent);
 
-    g_return_val_if_fail(len <= sizeof(struct virtio_gpu_config), -1);
+    if (len > sizeof(struct virtio_gpu_config)) {
+        g_critical("%s: len %u is larger than %zu",
+                   __func__, len, sizeof(struct virtio_gpu_config));
+        return -1;
+    }
 
     if (opt_virgl) {
         g->virtio_config.num_capsets = vg_virgl_get_num_capsets();
Signature · 1 line
2.28.0
Reviewed-by on v2 2/4
#6 lore raw

Re: [PATCH v2 2/4] contrib/vhost-user-gpu: avoid g_return_val_if() input validation

Quoted 18 lines · Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
contrib/vhost-user-gpu/vhost-user-gpu.c +0 -0
index a019d0a9ac..534bad24d1 100644

contrib/vhost-user-gpu/vhost-user-gpu.c vg_get_config(VuDev *dev, uint8_t *config, uint32_t · line 1051

Quoted 9 lines
contrib/vhost-user-gpu/vhost-user-gpu.c +3 -1
@@ -1044,7 +1044,11 @@ vg_get_config(VuDev *dev, uint8_t *config, uint32_t
 len)
 {
     VuGpu *g = container_of(dev, VuGpu, dev.parent);
 
-    g_return_val_if_fail(len <= sizeof(struct virtio_gpu_config), -1);
+    if (len > sizeof(struct virtio_gpu_config)) {
+        g_critical("%s: len %u is larger than %zu",
+                   __func__, len, sizeof(struct virtio_gpu_config));
 

g_critical() already has __FILE__ __LINE__ and G_STRFUNC.

otherwise looks good:

+ return -1;

Quoted 8 lines
contrib/vhost-user-gpu/vhost-user-gpu.c +1 -1
+    }
 
     if (opt_virgl) {
         g->virtio_config.num_capsets = vg_virgl_get_num_capsets();
--

2.28.0

Signature · 1 line
Marc-André Lureau
#7 lore raw

Re: [PATCH v2 2/4] contrib/vhost-user-gpu: avoid g_return_val_if() input validation

Quoted 24 lines · Marc-André Lureau
Quoted 22 lines · Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
contrib/vhost-user-gpu/vhost-user-gpu.c +0 -0
index a019d0a9ac..534bad24d1 100644
@@ -1044,7 +1044,11 @@ vg_get_config(VuDev *dev, uint8_t *config, uint32_t
 len)
 {
     VuGpu *g = container_of(dev, VuGpu, dev.parent);
 
Quoted 7 lines

contrib/vhost-user-gpu/vhost-user-gpu.c vg_get_config(VuDev *dev, uint8_t *config, uint32_t len) · line 1052

Quoted 5 lines
contrib/vhost-user-gpu/vhost-user-gpu.c +3 -1
-    g_return_val_if_fail(len <= sizeof(struct virtio_gpu_config), -1);
+    if (len > sizeof(struct virtio_gpu_config)) {
+        g_critical("%s: len %u is larger than %zu",
+                   __func__, len, sizeof(struct virtio_gpu_config));
 

g_critical() already has __FILE__ __LINE__ and G_STRFUNC.

I did this for consistency with the logging in this source file. The other g_critical() calls in the file also print __func__.

Stefan

#8 lore raw

Re: [PATCH v2 2/4] contrib/vhost-user-gpu: avoid g_return_val_if() input validation

Quoted 34 lines · Stefan Hajnoczi
Quoted 1 line · Marc-André Lureau

On Wed, Dec 2, 2020 at 7:27 PM Stefan Hajnoczi <[email protected]>

Quoted 3 lines · wrote:
Quoted 3 lines

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with

-DG_DISABLE_CHECKS.

Quoted 10 lines
Quoted 10 lines

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 6 lines

contrib/vhost-user-gpu/vhost-user-gpu.c vg_get_config(VuDev *dev, uint8_t *config,

Quoted 6 lines
contrib/vhost-user-gpu/vhost-user-gpu.c +0 -0
index a019d0a9ac..534bad24d1 100644
@@ -1044,7 +1044,11 @@ vg_get_config(VuDev *dev, uint8_t *config,

uint32_t

Quoted 10 lines
Quoted 9 lines
contrib/vhost-user-gpu/vhost-user-gpu.c +3 -1
 len)
 {
     VuGpu *g = container_of(dev, VuGpu, dev.parent);
 
-    g_return_val_if_fail(len <= sizeof(struct virtio_gpu_config), -1);
+    if (len > sizeof(struct virtio_gpu_config)) {
+        g_critical("%s: len %u is larger than %zu",
+                   __func__, len, sizeof(struct virtio_gpu_config));
 
Quoted 7 lines
Quoted 1 line

g_critical() already has __FILE__ __LINE__ and G_STRFUNC.

I did this for consistency with the logging in this source file. The other g_critical() calls in the file also print __func__.

I see, nevermind then. I gave rb anyway

Signature · 1 line
Marc-André Lureau
#9 lore raw

Re: [PATCH v2 2/4] contrib/vhost-user-gpu: avoid g_return_val_if() input validation

Quoted 36 lines · Marc-André Lureau
Quoted 34 lines · Stefan Hajnoczi
Quoted 1 line · Marc-André Lureau

On Wed, Dec 2, 2020 at 7:27 PM Stefan Hajnoczi <[email protected]>

Quoted 3 lines · wrote:
Quoted 3 lines

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with

-DG_DISABLE_CHECKS.

Quoted 10 lines
Quoted 10 lines

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 6 lines

contrib/vhost-user-gpu/vhost-user-gpu.c vg_get_config(VuDev *dev, uint8_t *config,

Quoted 6 lines
contrib/vhost-user-gpu/vhost-user-gpu.c +0 -0
index a019d0a9ac..534bad24d1 100644
@@ -1044,7 +1044,11 @@ vg_get_config(VuDev *dev, uint8_t *config,

uint32_t

Quoted 10 lines
Quoted 9 lines
contrib/vhost-user-gpu/vhost-user-gpu.c +3 -1
 len)
 {
     VuGpu *g = container_of(dev, VuGpu, dev.parent);
 
-    g_return_val_if_fail(len <= sizeof(struct virtio_gpu_config), -1);
+    if (len > sizeof(struct virtio_gpu_config)) {
+        g_critical("%s: len %u is larger than %zu",
+                   __func__, len, sizeof(struct virtio_gpu_config));
 
Quoted 8 lines
Quoted 7 lines
Quoted 1 line

g_critical() already has __FILE__ __LINE__ and G_STRFUNC.

I did this for consistency with the logging in this source file. The other g_critical() calls in the file also print __func__.

I see, nevermind then. I gave rb anyway

Thanks! I checked now and don't see the function name printed by g_critical() even though G_STRFUNC is captured in the header file:

** (process:693258): CRITICAL **: 11:30:18.210: test

Maybe only custom log handlers can display the function name?

So it seems the explicit __func__ approach is okay-ish :).

Stefan

#10 lore raw

PATCH v2 3/4 contrib/vhost-user-input: avoid g_return_val_if() input validation

In reply to Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 contrib/vhost-user-input/main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
contrib/vhost-user-input/main.c +5 -1
index 6020c6f33a..1d79c61200 100644
@@ -212,7 +212,11 @@ static int vi_get_config(VuDev *dev, uint8_t *config, uint32_t len)
 {
     VuInput *vi = container_of(dev, VuInput, dev.parent);
 
-    g_return_val_if_fail(len <= sizeof(*vi->sel_config), -1);
+    if (len > sizeof(*vi->sel_config)) {
+        g_critical("%s: len %u is larger than %zu",
+                   __func__, len, sizeof(*vi->sel_config));
+        return -1;
+    }
 
     if (vi->sel_config) {
         memcpy(config, vi->sel_config, len);
Signature · 1 line
2.28.0
Reviewed-by on v2 3/4
#11 lore raw

Re: [PATCH v2 3/4] contrib/vhost-user-input: avoid g_return_val_if() input validation

Quoted 18 lines · Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 contrib/vhost-user-input/main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
contrib/vhost-user-input/main.c +0 -0
index 6020c6f33a..1d79c61200 100644

contrib/vhost-user-input/main.c static int vi_get_config(VuDev *dev, uint8_t *config, · line 220

Quoted 10 lines
contrib/vhost-user-input/main.c +4 -1
@@ -212,7 +212,11 @@ static int vi_get_config(VuDev *dev, uint8_t *config,
 uint32_t len)
 {
     VuInput *vi = container_of(dev, VuInput, dev.parent);
 
-    g_return_val_if_fail(len <= sizeof(*vi->sel_config), -1);
+    if (len > sizeof(*vi->sel_config)) {
+        g_critical("%s: len %u is larger than %zu",
+                   __func__, len, sizeof(*vi->sel_config));
+        return -1;
 

g_critical() already has __FILE__ __LINE__ and G_STRFUNC.

otherwise looks good:

Quoted 8 lines
contrib/vhost-user-input/main.c +1 -0
+    }
 
     if (vi->sel_config) {
         memcpy(config, vi->sel_config, len);

--
2.28.0

Signature · 1 line
Marc-André Lureau
#12 lore raw

Re: [PATCH v2 3/4] contrib/vhost-user-input: avoid g_return_val_if() input validation

Quoted 24 lines · Marc-André Lureau
Quoted 22 lines · Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 contrib/vhost-user-input/main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
contrib/vhost-user-input/main.c +0 -0
index 6020c6f33a..1d79c61200 100644
@@ -212,7 +212,11 @@ static int vi_get_config(VuDev *dev, uint8_t *config,
 uint32_t len)
 {
     VuInput *vi = container_of(dev, VuInput, dev.parent);
 
Quoted 8 lines

contrib/vhost-user-input/main.c static int vi_get_config(VuDev *dev, uint8_t *config, uint32_t len) · line 220

Quoted 6 lines
contrib/vhost-user-input/main.c +4 -1
-    g_return_val_if_fail(len <= sizeof(*vi->sel_config), -1);
+    if (len > sizeof(*vi->sel_config)) {
+        g_critical("%s: len %u is larger than %zu",
+                   __func__, len, sizeof(*vi->sel_config));
+        return -1;
 

g_critical() already has __FILE__ __LINE__ and G_STRFUNC.

Will fix.

Stefan

#13 lore raw

PATCH v2 4/4 block/export: avoid g_return_val_if() input validation

In reply to Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 block/export/vhost-user-blk-server.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
block/export/vhost-user-blk-server.c +5 -1
index 62672d1cb9..bccbc98d57 100644
@@ -267,7 +267,11 @@ vu_blk_get_config(VuDev *vu_dev, uint8_t *config, uint32_t len)
     VuServer *server = container_of(vu_dev, VuServer, vu_dev);
     VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server);
 
-    g_return_val_if_fail(len <= sizeof(struct virtio_blk_config), -1);
+    if (len > sizeof(struct virtio_blk_config)) {
+        error_report("Invalid get_config len %u, expected <= %zu",
+                     len, sizeof(struct virtio_blk_config));
+        return -1;
+    }
 
     memcpy(config, &vexp->blkcfg, len);
     return 0;
Signature · 1 line
2.28.0
Reviewed-by on v2 4/4
#14 lore raw

Re: [PATCH v2 4/4] block/export: avoid g_return_val_if() input validation

Quoted 9 lines · Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---

Quoted 26 lines
 block/export/vhost-user-blk-server.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
block/export/vhost-user-blk-server.c +5 -1
index 62672d1cb9..bccbc98d57 100644
@@ -267,7 +267,11 @@ vu_blk_get_config(VuDev *vu_dev, uint8_t *config,
 uint32_t len)
     VuServer *server = container_of(vu_dev, VuServer, vu_dev);
     VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server);
 
-    g_return_val_if_fail(len <= sizeof(struct virtio_blk_config), -1);
+    if (len > sizeof(struct virtio_blk_config)) {
+        error_report("Invalid get_config len %u, expected <= %zu",
+                     len, sizeof(struct virtio_blk_config));
+        return -1;
+    }
 
     memcpy(config, &vexp->blkcfg, len);
     return 0;

--
2.28.0

Signature · 1 line
Marc-André Lureau
Reviewed-by on v2 4/4
#15 lore raw

Re: [PATCH v2 4/4] block/export: avoid g_return_val_if() input validation

In reply to Stefan Hajnoczi
Quoted 11 lines · Stefan Hajnoczi

Do not validate input with g_return_val_if(). This API is intended for checking programming errors and is compiled out with -DG_DISABLE_CHECKS.

Use an explicit if statement for input validation so it cannot accidentally be compiled out.

---
 block/export/vhost-user-blk-server.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Reviewed-by on the v2 series
#16 lore raw
In reply to Stefan Hajnoczi
Quoted 16 lines · Stefan Hajnoczi

v2:
* Print errors [Marc-André]

Markus Armbruster pointed out that g_return_val_if() is meant for programming errors. It must not be used for input validation since it can be compiled out. Use explicit if statements instead.

This patch series converts vhost-user device backends that use g_return_val_if() in get/set_config().

Stefan Hajnoczi (4):
contrib/vhost-user-blk: avoid g_return_val_if() input validation contrib/vhost-user-gpu: avoid g_return_val_if() input validation contrib/vhost-user-input: avoid g_return_val_if() input validation block/export: avoid g_return_val_if() input validation

Quoted 9 lines
block/export/vhost-user-blk-server.c    | 6 +++++-
contrib/vhost-user-blk/vhost-user-blk.c | 6 +++++-
contrib/vhost-user-gpu/vhost-user-gpu.c | 6 +++++-
contrib/vhost-user-input/main.c         | 6 +++++-
4 files changed, 20 insertions(+), 4 deletions(-)
Signature · 1 line
2.28.0
#17 lore raw

Hi Michael,

Quoted 25 lines · Stefano Garzarella
Quoted 4 lines · Stefan Hajnoczi

v2:
* Print errors [Marc-André]

Markus Armbruster pointed out that g_return_val_if() is meant for

programming

Quoted 1 line

errors. It must not be used for input validation since it can be compiled

out.

Quoted 17 lines

Use explicit if statements instead.

This patch series converts vhost-user device backends that use g_return_val_if() in get/set_config().

Stefan Hajnoczi (4):
contrib/vhost-user-blk: avoid g_return_val_if() input validation contrib/vhost-user-gpu: avoid g_return_val_if() input validation contrib/vhost-user-input: avoid g_return_val_if() input validation block/export: avoid g_return_val_if() input validation

block/export/vhost-user-blk-server.c    | 6 +++++-
contrib/vhost-user-blk/vhost-user-blk.c | 6 +++++-
contrib/vhost-user-gpu/vhost-user-gpu.c | 6 +++++-
contrib/vhost-user-input/main.c         | 6 +++++-
4 files changed, 20 insertions(+), 4 deletions(-)
Quoted 7 lines
Quoted 3 lines

--
2.28.0

You didn't collect the v2 patch series, with the received reviewed-by. Not a big deal here, but please be more careful.

thanks

Signature · 1 line
Marc-André Lureau