Skip to contentSkip to conversation

s390x/s390-virtio-ccw: fix off-by-one in loadparm getter

PATCH Superseded Halil Pasic 4 participants 8 messages Jul 29, 2020 – Jul 30, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 s390x/s390-virtio-ccw: fix off-by-one in loadparm getter

As pointed out by Peter, g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1) reads one past of the end of ms->loadparm, so g_memdup() can not be used here.

Let's use malloc and memcpy instead!

Fixes d664548328 ("s390x/s390-virtio-ccw: fix loadparm property getter")
Fixes Coverity CID 1431058
---
 hw/s390x/s390-virtio-ccw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/s390x/s390-virtio-ccw.c +2 -2
index 403d30e13b..8b7bac0392 100644
@@ -704,8 +704,8 @@ static char *machine_get_loadparm(Object *obj, Error **errp)
     char *loadparm_str;
 
     /* make a NUL-terminated string */
-    loadparm_str = g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1);
-    loadparm_str[sizeof(ms->loadparm)] = 0;
+    loadparm_str = g_malloc0(sizeof(ms->loadparm) + 1);
+    memcpy(loadparm_str, ms->loadparm, sizeof(ms->loadparm));
     return loadparm_str;
 }
 

base-commit: 5772f2b1fc5d00e7e04e01fa28e9081d6550440a

Signature · 1 line
2.17.1
#1 lore raw
Quoted 30 lines · On Wed, 29 Jul 2020 15:02:22 +0200 Halil Pasic <[email protected]> wrote:

As pointed out by Peter, g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1) reads one past of the end of ms->loadparm, so g_memdup() can not be used here.

Let's use malloc and memcpy instead!

Fixes d664548328 ("s390x/s390-virtio-ccw: fix loadparm property getter")
Fixes Coverity CID 1431058
---
 hw/s390x/s390-virtio-ccw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/s390x/s390-virtio-ccw.c +2 -2
index 403d30e13b..8b7bac0392 100644
@@ -704,8 +704,8 @@ static char *machine_get_loadparm(Object *obj, Error **errp)
     char *loadparm_str;
 
     /* make a NUL-terminated string */
-    loadparm_str = g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1);
-    loadparm_str[sizeof(ms->loadparm)] = 0;
+    loadparm_str = g_malloc0(sizeof(ms->loadparm) + 1);
+    memcpy(loadparm_str, ms->loadparm, sizeof(ms->loadparm));
     return loadparm_str;
 }
 
Quoted 1 line

base-commit: 5772f2b1fc5d00e7e04e01fa28e9081d6550440a

Thanks, queued to s390-fixes.

Reviewed-by on v1 1/1
#2 lore raw
In reply to Halil Pasic
Quoted 20 lines · Halil Pasic

As pointed out by Peter, g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1) reads one past of the end of ms->loadparm, so g_memdup() can not be used here.

Let's use malloc and memcpy instead!

Fixes d664548328 ("s390x/s390-virtio-ccw: fix loadparm property getter")
Fixes Coverity CID 1431058
---
 hw/s390x/s390-virtio-ccw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/s390x/s390-virtio-ccw.c +0 -0
index 403d30e13b..8b7bac0392 100644

hw/s390x/s390-virtio-ccw.c static char *machine_get_loadparm(Object *obj, Error **errp) · line 710

Quoted 9 lines
hw/s390x/s390-virtio-ccw.c +2 -2
@@ -704,8 +704,8 @@ static char *machine_get_loadparm(Object *obj, Error **errp)
     char *loadparm_str;
 
     /* make a NUL-terminated string */
-    loadparm_str = g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1);
-    loadparm_str[sizeof(ms->loadparm)] = 0;
+    loadparm_str = g_malloc0(sizeof(ms->loadparm) + 1);
+    memcpy(loadparm_str, ms->loadparm, sizeof(ms->loadparm));
     return loadparm_str;
 }

(relies on the zeroing of g_malloc0 to put in the terminator but I guess the existing comment makes that clear enough.)

thanks
-- PMM

#3 lore raw
In reply to Halil Pasic
Quoted 19 lines · Halil Pasic

As pointed out by Peter, g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1) reads one past of the end of ms->loadparm, so g_memdup() can not be used here.

Let's use malloc and memcpy instead!

Fixes d664548328 ("s390x/s390-virtio-ccw: fix loadparm property getter")
Fixes Coverity CID 1431058
---
 hw/s390x/s390-virtio-ccw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/s390x/s390-virtio-ccw.c +0 -0
index 403d30e13b..8b7bac0392 100644

hw/s390x/s390-virtio-ccw.c static char *machine_get_loadparm(Object *obj, Error **errp) · line 708

Quoted 7 lines
hw/s390x/s390-virtio-ccw.c +2 -2
@@ -704,8 +704,8 @@ static char *machine_get_loadparm(Object *obj, Error **errp)
     char *loadparm_str;
 
     /* make a NUL-terminated string */
-    loadparm_str = g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1);
-    loadparm_str[sizeof(ms->loadparm)] = 0;
+    loadparm_str = g_malloc0(sizeof(ms->loadparm) + 1);
+    memcpy(loadparm_str, ms->loadparm, sizeof(ms->loadparm));

I feel like g_strndup(ms->loadparm, sizeof(ms->loadparm)) is what should have been used here.

It copies N characters, but allocates N+1 adding a trailing NUL which are the semantic we wanted here.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#4 lore raw
Quoted 11 lines · On Thu, 30 Jul 2020 11:25:06 +0100 Daniel P. Berrangé <[email protected]>…

hw/s390x/s390-virtio-ccw.c static char *machine_get_loadparm(Object *obj, Error **errp) · line 708

Quoted 5 lines
     /* make a NUL-terminated string */
hw/s390x/s390-virtio-ccw.c +2 -2
-    loadparm_str = g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1);
-    loadparm_str[sizeof(ms->loadparm)] = 0;
+    loadparm_str = g_malloc0(sizeof(ms->loadparm) + 1);
+    memcpy(loadparm_str, ms->loadparm, sizeof(ms->loadparm));  

I feel like g_strndup(ms->loadparm, sizeof(ms->loadparm)) is what should have been used here.

It copies N characters, but allocates N+1 adding a trailing NUL which are the semantic we wanted here.

I agree. Thanks for pointing this out. I'm not very familiar with the string utilities of glib.

Regards,
Halil

#5 lore raw
In reply to Halil Pasic
Quoted 5 lines · On Wed, 29 Jul 2020 15:02:22 +0200 Halil Pasic <[email protected]> wrote:

As pointed out by Peter, g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1) reads one past of the end of ms->loadparm, so g_memdup() can not be used here.

Let's use malloc and memcpy instead!

Hm, an alternative would be to use g_strndup(). What do you think?

Quoted 26 lines
Fixes d664548328 ("s390x/s390-virtio-ccw: fix loadparm property getter")
Fixes Coverity CID 1431058
---
 hw/s390x/s390-virtio-ccw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/s390x/s390-virtio-ccw.c +2 -2
index 403d30e13b..8b7bac0392 100644
@@ -704,8 +704,8 @@ static char *machine_get_loadparm(Object *obj, Error **errp)
     char *loadparm_str;
 
     /* make a NUL-terminated string */
-    loadparm_str = g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1);
-    loadparm_str[sizeof(ms->loadparm)] = 0;
+    loadparm_str = g_malloc0(sizeof(ms->loadparm) + 1);
+    memcpy(loadparm_str, ms->loadparm, sizeof(ms->loadparm));
     return loadparm_str;
 }
 

base-commit: 5772f2b1fc5d00e7e04e01fa28e9081d6550440a

#6 lore raw
Quoted 10 lines · On Thu, 30 Jul 2020 12:26:56 +0200 Cornelia Huck <[email protected]> wrote:
Quoted 5 lines · On Wed, 29 Jul 2020 15:02:22 +0200 Halil Pasic <[email protected]> wrote:

As pointed out by Peter, g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1) reads one past of the end of ms->loadparm, so g_memdup() can not be used here.

Let's use malloc and memcpy instead!

Hm, an alternative would be to use g_strndup(). What do you think?

Sure. It is more concise and does exactly what we want. I'm not too familiar with the string utility funcitons of glib, so it didn't jup at me.

Shall I spin a v2?

Halil

Quoted 29 lines
Quoted 27 lines
Fixes d664548328 ("s390x/s390-virtio-ccw: fix loadparm property getter")
Fixes Coverity CID 1431058
---
 hw/s390x/s390-virtio-ccw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/s390x/s390-virtio-ccw.c +2 -2
index 403d30e13b..8b7bac0392 100644
@@ -704,8 +704,8 @@ static char *machine_get_loadparm(Object *obj, Error **errp)
     char *loadparm_str;
 
     /* make a NUL-terminated string */
-    loadparm_str = g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1);
-    loadparm_str[sizeof(ms->loadparm)] = 0;
+    loadparm_str = g_malloc0(sizeof(ms->loadparm) + 1);
+    memcpy(loadparm_str, ms->loadparm, sizeof(ms->loadparm));
     return loadparm_str;
 }
 

base-commit: 5772f2b1fc5d00e7e04e01fa28e9081d6550440a

#7 lore raw
Quoted 12 lines · On Thu, 30 Jul 2020 13:25:21 +0200 Halil Pasic <[email protected]> wrote:
Quoted 9 lines · On Thu, 30 Jul 2020 12:26:56 +0200 Cornelia Huck <[email protected]> wrote:
Quoted 5 lines · On Wed, 29 Jul 2020 15:02:22 +0200 Halil Pasic <[email protected]> wrote:

As pointed out by Peter, g_memdup(ms->loadparm, sizeof(ms->loadparm) + 1) reads one past of the end of ms->loadparm, so g_memdup() can not be used here.

Let's use malloc and memcpy instead!

Quoted 7 lines
Quoted 1 line

Hm, an alternative would be to use g_strndup(). What do you think?

Sure. It is more concise and does exactly what we want. I'm not too familiar with the string utility funcitons of glib, so it didn't jup at me.

Shall I spin a v2?

Sounds good.