vhost-user: avoid g_return_val_if() in get/set_config()
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
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
@@ -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
PATCH 2/4 contrib/vhost-user-gpu: avoid g_return_val_if() input validation
In reply to Stefan HajnocziDo 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
@@ -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
PATCH 3/4 contrib/vhost-user-input: avoid g_return_val_if() input validation
In reply to Stefan HajnocziDo 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
@@ -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
PATCH 4/4 block/export: avoid g_return_val_if() input validation
In reply to Stefan HajnocziDo 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
@@ -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
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: avoidg_return_val_if()input validation contrib/vhost-user-gpu: avoidg_return_val_if()input validation contrib/vhost-user-input: avoidg_return_val_if()input validation block/export: avoidg_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
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: avoidg_return_val_if()input validation contrib/vhost-user-gpu: avoidg_return_val_if()input validation contrib/vhost-user-input: avoidg_return_val_if()input validation block/export: avoidg_return_val_if()input validationThe 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_CHECKSforbidden 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.
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: avoidg_return_val_if()input validation contrib/vhost-user-gpu: avoidg_return_val_if()input validation contrib/vhost-user-input: avoidg_return_val_if()input validation block/export: avoidg_return_val_if()input validationThe 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_CHECKSforbidden and call it a day)
I'll add an error message in v2.
Stefan
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().
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: avoidg_return_val_if()input validation contrib/vhost-user-gpu: avoidg_return_val_if()input validation contrib/vhost-user-input: avoidg_return_val_if()input validation block/export: avoidg_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(-)