Skip to contentSkip to conversation

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

PATCH Superseded Stefan Hajnoczi 4 participants 10 messages Nov 18, 2020 – Dec 2, 2020 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

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    | 4 +++-
 contrib/vhost-user-blk/vhost-user-blk.c | 4 +++-
 contrib/vhost-user-gpu/vhost-user-gpu.c | 4 +++-
 contrib/vhost-user-input/main.c         | 4 +++-
 4 files changed, 12 insertions(+), 4 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH 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 | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
contrib/vhost-user-blk/vhost-user-blk.c +3 -1
index dc981bf945..1540d5e87d 100644
@@ -404,7 +404,9 @@ 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)) {
+        return -1;
+    }
 
     gdev = container_of(vu_dev, VugDev, parent);
     vdev_blk = container_of(gdev, VubDev, parent);
Signature · 1 line
2.28.0
#2 lore raw

PATCH 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 | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
contrib/vhost-user-gpu/vhost-user-gpu.c +3 -1
index a019d0a9ac..f445ef28ec 100644
@@ -1044,7 +1044,9 @@ 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)) {
+        return -1;
+    }
 
     if (opt_virgl) {
         g->virtio_config.num_capsets = vg_virgl_get_num_capsets();
Signature · 1 line
2.28.0
#3 lore raw

PATCH 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 | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
contrib/vhost-user-input/main.c +3 -1
index 6020c6f33a..50a8a03d9b 100644
@@ -212,7 +212,9 @@ 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)) {
+        return -1;
+    }
 
     if (vi->sel_config) {
         memcpy(config, vi->sel_config, len);
Signature · 1 line
2.28.0
#4 lore raw

PATCH 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 | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
block/export/vhost-user-blk-server.c +3 -1
index 62672d1cb9..d143ce8993 100644
@@ -267,7 +267,9 @@ 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)) {
+        return -1;
+    }
 
     memcpy(config, &vexp->blkcfg, len);
     return 0;
Signature · 1 line
2.28.0
#5 lore raw
In reply to Stefan Hajnoczi

Hi

Quoted 6 lines · Stefan Hajnoczi

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.

Quoted 10 lines

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

The condition is the same for all the patches, checking the message config payload is large enough. Afaict, the value is set by the client, so it could be a runtime error, and thus explicit checking is required.

Nevertheless, one nice thing about g_return* macros, is that it provides an error message when the condition fails, which helps. Could you replace it?

(fwiw, I think g_return* macros are so convenient, I would simply make G_DISABLE_CHECKS forbidden and call it a day)

Signature · 1 line
Marc-André Lureau
#6 lore raw
Quoted 24 lines · Marc-André Lureau

Hi

Quoted 6 lines · Stefan Hajnoczi

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.

Quoted 10 lines

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

The condition is the same for all the patches, checking the message config payload is large enough. Afaict, the value is set by the client, so it could be a runtime error, and thus explicit checking is required.

Quoted 5 lines

Nevertheless, one nice thing about g_return* macros, is that it provides an error message when the condition fails, which helps. Could you replace it?

(fwiw, I think g_return* macros are so convenient, I would simply make G_DISABLE_CHECKS forbidden and call it a day)

Nice or not, they are as inappropriate for input validation as assert() is:

If expr evaluates to FALSE, the current function should be considered to have undefined behaviour (a programmer error). The only correct solution to such an error is to change the module that is calling the current function, so that it avoids this incorrect call.

#7 lore raw
In reply to Marc-André Lureau
Quoted 24 lines · Marc-André Lureau

Hi

Quoted 6 lines · Stefan Hajnoczi

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.

Quoted 10 lines

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

The condition is the same for all the patches, checking the message config payload is large enough. Afaict, the value is set by the client, so it could be a runtime error, and thus explicit checking is required.

Quoted 5 lines

Nevertheless, one nice thing about g_return* macros, is that it provides an error message when the condition fails, which helps. Could you replace it?

(fwiw, I think g_return* macros are so convenient, I would simply make G_DISABLE_CHECKS forbidden and call it a day)

I'll add an error message in v2.

Stefan

Reviewed-by on the v1 series
#8 lore raw
In reply to Stefan Hajnoczi
Quoted 6 lines · Stefan Hajnoczi

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().

Reviewed-by on the v1 series
#9 lore raw
In reply to Stefan Hajnoczi
Quoted 13 lines · Stefan Hajnoczi

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 6 lines
block/export/vhost-user-blk-server.c    | 4 +++-
contrib/vhost-user-blk/vhost-user-blk.c | 4 +++-
contrib/vhost-user-gpu/vhost-user-gpu.c | 4 +++-
contrib/vhost-user-input/main.c         | 4 +++-
4 files changed, 12 insertions(+), 4 deletions(-)