hw/scsi/scsi-disk: Avoid buffer overrun parsing loadparam (CID 1565746)
cover hw/scsi/scsi-disk: Avoid buffer overrun parsing loadparam (CID 1565746)
Replace magic 8 by proper LOADPARM_LEN definition, use array of char, allocate loadparam[] within qdev state, fixing Coverity overrun report (CID 1565746).
Philippe Mathieu-Daudé (2):
hw/s390x: Declare loadparm as char[LOADPARM_LEN] hw/scsi/scsi-disk: Avoid buffer overrun parsing 'loadparam'
hw/s390x/ccw-device.h | 5 +++-- hw/s390x/ipl.h | 2 +- include/hw/qdev-properties-system.h | 2 +- include/hw/s390x/s390-virtio-ccw.h | 3 ++- hw/core/qdev-properties-system.c | 8 +++++--- hw/s390x/ipl.c | 6 +++--- hw/scsi/scsi-disk.c | 12 +++--------- 7 files changed, 18 insertions(+), 20 deletions(-)
Signature · 1 line
2.45.2
PATCH 1/2 hw/s390x: Declare loadparm as char[LOADPARM_LEN]
"hw/s390x/ipl/qipl.h" defines loadparm[] length as LOADPARM_LEN, use that instead of the magic '8' value. Use a char type for char buffer.
--- hw/s390x/ccw-device.h | 5 +++-- hw/s390x/ipl.h | 2 +- include/hw/qdev-properties-system.h | 2 +- include/hw/s390x/s390-virtio-ccw.h | 3 ++- hw/core/qdev-properties-system.c | 8 +++++--- hw/s390x/ipl.c | 6 +++--- hw/scsi/scsi-disk.c | 2 +- 7 files changed, 16 insertions(+), 12 deletions(-)
hw/s390x/ccw-device.h +3 -2
@@ -15,6 +15,7 @@#include "hw/qdev-core.h" #include "hw/s390x/css.h" #include "hw/s390x/css-bridge.h" +#include "hw/s390x/ipl/qipl.h" struct CcwDevice {DeviceState parent_obj;
@@ -27,7 +28,7 @@ struct CcwDevice {/* The actual busid of the virtual subchannel. */ CssDevId subch_id;/* If set, use this loadparm value when device is boot target */ -uint8_t loadparm[8 ]; +char loadparm[LOADPARM_LEN ]; };typedef struct CcwDevice CcwDevice ;
@@ -54,6 +55,6 @@ OBJECT_DECLARE_TYPE(CcwDevice, CCWDeviceClass, CCW_DEVICE)extern const PropertyInfo ccw_loadparm;#define DEFINE_PROP_CCW_LOADPARM (_n, _s, _f) \ - DEFINE_PROP(_n, _s, _f, ccw_loadparm, typeof(uint8_t[8])) + DEFINE_PROP(_n, _s, _f, ccw_loadparm, typeof(char[LOADPARM_LEN]))#endif
hw/s390x/ipl.h +1 -1
@@ -23,7 +23,7 @@#define MAX_BOOT_DEVS 8/* Max number of devices that may have a bootindex */ void s390_ipl_convert_loadparm (char *ascii_lp,uint8_t *ebcdic_lp); -void s390_ipl_fmt_loadparm (uint8_t *loadparm,char *str,Error **errp); +void s390_ipl_fmt_loadparm (char *loadparm,char *str,Error **errp);void s390_rebuild_iplb (uint16_t index,IplParameterBlock *iplb);void s390_ipl_update_diag308 (IplParameterBlock *iplb);int s390_ipl_prepare_pv_header (Error **errp);
include/hw/qdev-properties-system.h +1 -1
@@ -3,7 +3,7 @@#include "hw/qdev-properties.h" -bool qdev_prop_sanitize_s390x_loadparm (uint8_t *loadparm,const char *str, +bool qdev_prop_sanitize_s390x_loadparm (char *loadparm,const char *str,Error **errp);extern const PropertyInfo qdev_prop_chr;
include/hw/s390x/s390-virtio-ccw.h +2 -1
@@ -14,6 +14,7 @@#include "hw/boards.h" #include "qom/object.h" #include "hw/s390x/sclp.h" +#include "hw/s390x/ipl/qipl.h" #define TYPE_S390_CCW_MACHINE "s390-ccw-machine"
@@ -28,7 +29,7 @@ struct S390CcwMachineState {bool aes_key_wrap;bool dea_key_wrap;bool pv; -uint8_t loadparm[8 ]; +char loadparm[LOADPARM_LEN ];SCLPDevice *sclp; };
hw/core/qdev-properties-system.c +5 -3
@@ -35,6 +35,7 @@#include "hw/pci/pci.h" #include "hw/pci/pcie.h" #include "hw/i386/x86.h" +#include "hw/s390x/ipl/qipl.h" #include "util/block-helpers.h" static bool check_prop_still_unset(Object *obj, const char *name,
@@ -58,14 +59,15 @@ static bool check_prop_still_unset(Object *obj, const char *name,return false; } -bool qdev_prop_sanitize_s390x_loadparm (uint8_t *loadparm,const char *str, +bool qdev_prop_sanitize_s390x_loadparm (char *loadparm,const char *str,Error **errp) {int i, len; len =strlen (str); -if (len >8 ) { -error_setg (errp,"'loadparm' can only contain up to 8 characters" ); +if (len >LOADPARM_LEN ) { +error_setg (errp,"'loadparm' can only contain up to %u characters" , +LOADPARM_LEN );return false; }
hw/s390x/ipl.c +3 -3
@@ -416,7 +416,7 @@ static uint64_t s390_ipl_map_iplb_chain(IplParameterBlock *iplb_chain)return chain_addr; } -void s390_ipl_fmt_loadparm (uint8_t *loadparm,char *str,Error **errp) +void s390_ipl_fmt_loadparm (char *loadparm,char *str,Error **errp) {/* Initialize the loadparm with spaces */ memset (loadparm,' ' ,LOADPARM_LEN );
@@ -439,8 +439,8 @@ static bool s390_build_iplb(DeviceState *dev_st, IplParameterBlock *iplb)CcwDevice *ccw_dev =NULL ;SCSIDevice *sd;int devtype; -uint8_t *lp; - g_autofree void *scsi_lp = NULL; +char *lp; + g_autofree char *scsi_lp = NULL;/* * Currently allow IPL only from CCW devices.
hw/scsi/scsi-disk.c +1 -1
@@ -3145,7 +3145,7 @@ static char *scsi_property_get_loadparm(Object *obj, Error **errp)static void scsi_property_set_loadparm (Object *obj,const char *value,Error **errp) { -void *lp_str; +char *lp_str;if (object_property_get_int (obj,"bootindex" ,NULL ) <0 ) {error_setg (errp,"'loadparm' is only valid for boot devices" );
Signature · 1 line
2.45.2
Re: [PATCH 1/2] hw/s390x: Declare loadparm as char[LOADPARM_LEN]
ping for this single cleanup patch?
Quoted 159 lines · Philippe Mathieu-Daudé
"
hw/s390x/ipl/qipl.h" defines loadparm[] length asLOADPARM_LEN, use that instead of the magic '8' value. Use a char type for char buffer.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/s390x/ccw-device.h | 5 +++-- hw/s390x/ipl.h | 2 +- include/hw/qdev-properties-system.h | 2 +- include/hw/s390x/s390-virtio-ccw.h | 3 ++- hw/core/qdev-properties-system.c | 8 +++++--- hw/s390x/ipl.c | 6 +++--- hw/scsi/scsi-disk.c | 2 +- 7 files changed, 16 insertions(+), 12 deletions(-)hw/s390x/ccw-device.h +3
-2@@ -15,6 +15,7 @@#include "hw/qdev-core.h" #include "hw/s390x/css.h" #include "hw/s390x/css-bridge.h" +#include "hw/s390x/ipl/qipl.h" struct CcwDevice {DeviceState parent_obj;@@ -27,7 +28,7 @@ struct CcwDevice {/* The actual busid of the virtual subchannel. */ CssDevId subch_id;/* If set, use this loadparm value when device is boot target */ -uint8_t loadparm[8 ]; +char loadparm[LOADPARM_LEN ]; };typedef struct CcwDevice CcwDevice ;@@ -54,6 +55,6 @@ OBJECT_DECLARE_TYPE(CcwDevice, CCWDeviceClass, CCW_DEVICE)extern const PropertyInfo ccw_loadparm;#define DEFINE_PROP_CCW_LOADPARM (_n, _s, _f) \ - DEFINE_PROP(_n, _s, _f, ccw_loadparm, typeof(uint8_t[8])) + DEFINE_PROP(_n, _s, _f, ccw_loadparm, typeof(char[LOADPARM_LEN]))#endif hw/s390x/ipl.h +1
-1@@ -23,7 +23,7 @@#define MAX_BOOT_DEVS 8/* Max number of devices that may have a bootindex */ void s390_ipl_convert_loadparm (char *ascii_lp,uint8_t *ebcdic_lp); -void s390_ipl_fmt_loadparm (uint8_t *loadparm,char *str,Error **errp); +void s390_ipl_fmt_loadparm (char *loadparm,char *str,Error **errp);void s390_rebuild_iplb (uint16_t index,IplParameterBlock *iplb);void s390_ipl_update_diag308 (IplParameterBlock *iplb);int s390_ipl_prepare_pv_header (Error **errp);include/hw/qdev-properties-system.h +1
-1@@ -3,7 +3,7 @@#include "hw/qdev-properties.h" -bool qdev_prop_sanitize_s390x_loadparm (uint8_t *loadparm,const char *str, +bool qdev_prop_sanitize_s390x_loadparm (char *loadparm,const char *str,Error **errp);extern const PropertyInfo qdev_prop_chr;include/hw/s390x/s390-virtio-ccw.h +2
-1@@ -14,6 +14,7 @@#include "hw/boards.h" #include "qom/object.h" #include "hw/s390x/sclp.h" +#include "hw/s390x/ipl/qipl.h" #define TYPE_S390_CCW_MACHINE "s390-ccw-machine"@@ -28,7 +29,7 @@ struct S390CcwMachineState {bool aes_key_wrap;bool dea_key_wrap;bool pv; -uint8_t loadparm[8 ]; +char loadparm[LOADPARM_LEN ];SCLPDevice *sclp; };hw/core/qdev-properties-system.c +5
-3@@ -35,6 +35,7 @@#include "hw/pci/pci.h" #include "hw/pci/pcie.h" #include "hw/i386/x86.h" +#include "hw/s390x/ipl/qipl.h" #include "util/block-helpers.h" static bool check_prop_still_unset(Object *obj, const char *name,@@ -58,14 +59,15 @@ static bool check_prop_still_unset(Object *obj, const char *name,return false; } -bool qdev_prop_sanitize_s390x_loadparm (uint8_t *loadparm,const char *str, +bool qdev_prop_sanitize_s390x_loadparm (char *loadparm,const char *str,Error **errp) {int i, len; len =strlen (str); -if (len >8 ) { -error_setg (errp,"'loadparm' can only contain up to 8 characters" ); +if (len >LOADPARM_LEN ) { +error_setg (errp,"'loadparm' can only contain up to %u characters" , +LOADPARM_LEN );return false; }hw/s390x/ipl.c +3
-3@@ -416,7 +416,7 @@ static uint64_t s390_ipl_map_iplb_chain(IplParameterBlock *iplb_chain)return chain_addr; } -void s390_ipl_fmt_loadparm (uint8_t *loadparm,char *str,Error **errp) +void s390_ipl_fmt_loadparm (char *loadparm,char *str,Error **errp) {/* Initialize the loadparm with spaces */ memset (loadparm,' ' ,LOADPARM_LEN );@@ -439,8 +439,8 @@ static bool s390_build_iplb(DeviceState *dev_st, IplParameterBlock *iplb)CcwDevice *ccw_dev =NULL ;SCSIDevice *sd;int devtype; -uint8_t *lp; - g_autofree void *scsi_lp = NULL; +char *lp; + g_autofree char *scsi_lp = NULL;/* * Currently allow IPL only from CCW devices. hw/scsi/scsi-disk.c +1
-1@@ -3145,7 +3145,7 @@ static char *scsi_property_get_loadparm(Object *obj, Error **errp)static void scsi_property_set_loadparm (Object *obj,const char *value,Error **errp) { -void *lp_str; +char *lp_str;if (object_property_get_int (obj,"bootindex" ,NULL ) <0 ) {error_setg (errp,"'loadparm' is only valid for boot devices" );
Re: [PATCH 1/2] hw/s390x: Declare loadparm as char[LOADPARM_LEN]
Quoted 27 lines · Philippe Mathieu-Daudé
ping for this single cleanup patch?
Quoted 24 lines · Philippe Mathieu-Daudé
"
hw/s390x/ipl/qipl.h" defines loadparm[] length asLOADPARM_LEN, use that instead of the magic '8' value. Use a char type for char buffer.Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
hw/s390x/ccw-device.h | 5 +++-- hw/s390x/ipl.h | 2 +-include/hw/qdev-properties-system.h| 2 +- include/hw/s390x/s390-virtio-ccw.h | 3 ++- hw/core/qdev-properties-system.c | 8 +++++--- hw/s390x/ipl.c | 6 +++--- hw/scsi/scsi-disk.c | 2 +-7 files changed, 16 insertions(+), 12 deletions(-)hw/s390x/ccw-device.h +1
-0@@ -15,6 +15,7 @@ #include "hw/qdev-core.h" #include "hw/s390x/css.h" #include "hw/s390x/css-bridge.h" +#include "hw/s390x/ipl/qipl.h"
Quoted 8 lines
hw/s390x/ccw-device.h struct CcwDevice { · line 31
Quoted 8 lines
hw/s390x/ccw-device.h +1
-1struct CcwDevice { DeviceState parent_obj;@@ -27,7 +28,7 @@ struct CcwDevice { /* The actual busid of the virtual subchannel. */ CssDevId subch_id; /* If set, use this loadparm value when device is boot target */ - uint8_t loadparm[8]; + char loadparm[LOADPARM_LEN];
I'm not sure whether changing the type is such a good idea ... some spots that handle loadparm include EBCDIC encoding, and the unsigned type might have been used here for a reason? I'd better keep uint8_t unless you've double-checked all combinations...
Thomas
Re: [PATCH 1/2] hw/s390x: Declare loadparm as char[LOADPARM_LEN]
Quoted 30 lines · Thomas Huth
Quoted 29 lines · Philippe Mathieu-Daudé
ping for this single cleanup patch?
Quoted 26 lines · Philippe Mathieu-Daudé
"
hw/s390x/ipl/qipl.h" defines loadparm[] length asLOADPARM_LEN, use that instead of the magic '8' value. Use a char type for char buffer.Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
hw/s390x/ccw-device.h | 5 +++-- hw/s390x/ipl.h | 2 +-include/hw/qdev-properties-system.h| 2 +- include/hw/s390x/s390-virtio-ccw.h | 3 ++- hw/core/qdev-properties-system.c | 8 +++++--- hw/s390x/ipl.c | 6 +++--- hw/scsi/scsi-disk.c | 2 +-7 files changed, 16 insertions(+), 12 deletions(-)hw/s390x/ccw-device.h +1
-0@@ -15,6 +15,7 @@ #include "hw/qdev-core.h" #include "hw/s390x/css.h" #include "hw/s390x/css-bridge.h" +#include "hw/s390x/ipl/qipl.h" struct CcwDevice { DeviceState parent_obj;
Quoted 8 lines
Quoted 6 lines
hw/s390x/ccw-device.h struct CcwDevice { · line 31
Quoted 6 lines
hw/s390x/ccw-device.h +1
-1@@ -27,7 +28,7 @@ struct CcwDevice { /* The actual busid of the virtual subchannel. */ CssDevId subch_id; /* If set, use this loadparm value when device is boot target */ - uint8_t loadparm[8]; + char loadparm[LOADPARM_LEN];I'm not sure whether changing the type is such a good idea ... some spots that handle loadparm include EBCDIC encoding, and the unsigned type might have been used here for a reason? I'd better keep
uint8_tunless you've double-checked all combinations...
OK, no problem, thanks for checking!
Regards,
Phil.
PATCH 2/2 hw/scsi/scsi-disk: Avoid buffer overrun parsing 'loadparam'
In reply to Philippe Mathieu-DaudéCoverity reported a 1 byte overrun in scsi_property_set_loadparm (CID 15657462). Since loadparam[] length is known, simply directly allocate it in the device state.
--- hw/scsi/scsi-disk.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
hw/scsi/scsi-disk.c +3 -9
@@ -38,6 +38,7 @@#include "hw/block/block.h" #include "hw/qdev-properties.h" #include "hw/qdev-properties-system.h" +#include "hw/s390x/ipl/qipl.h" #include "sysemu/dma.h" #include "sysemu/sysemu.h" #include "qemu/cutils.h"
@@ -112,7 +113,7 @@ struct SCSIDiskState {char *vendor;char *product;char *device_id; -char *loadparm;/* only for s390x */ +char loadparm[LOADPARM_LEN ];/* only for s390x */ bool tray_open;bool tray_locked;/*
@@ -3145,19 +3146,12 @@ static char *scsi_property_get_loadparm(Object *obj, Error **errp)static void scsi_property_set_loadparm (Object *obj,const char *value,Error **errp) { -char *lp_str; -if (object_property_get_int (obj,"bootindex" ,NULL ) <0 ) {error_setg (errp,"'loadparm' is only valid for boot devices" );return ; } - lp_str =g_malloc0 (strlen (value)); -if (!qdev_prop_sanitize_s390x_loadparm (lp_str, value, errp)) { -g_free (lp_str); -return ; - } -SCSI_DISK_BASE (obj)->loadparm = lp_str; +qdev_prop_sanitize_s390x_loadparm (SCSI_DISK_BASE (obj)->loadparm, value, errp); }static void scsi_property_add_specifics (DeviceClass *dc)
Signature · 1 line
2.45.2
Re: [PATCH 2/2] hw/scsi/scsi-disk: Avoid buffer overrun parsing 'loadparam'
hw/scsi/scsi-disk.c struct SCSIDiskState { · line 116
Quoted 6 lines · Philippe Mathieu-Daudé
hw/scsi/scsi-disk.c +1
-1@@ -112,7 +113,7 @@ struct SCSIDiskState {char *vendor;char *product;char *device_id; -char *loadparm;/* only for s390x */ +char loadparm[LOADPARM_LEN ];/* only for s390x */
You would need a +1 here because of
static char *scsi_property_get_loadparm(Object *obj, Error **errp)
{
return g_strdup(SCSI_DISK_BASE(obj)->loadparm);
}expecting NUL-termination as well.
hw/scsi/scsi-disk.c static char *scsi_property_get_loadparm(Object *obj, Error **errp) · line 3155
Quoted 1 line
hw/scsi/scsi-disk.c +0
-1- lp_str =g_malloc0 (strlen (value));
I have sent a pull request that simply adds the +1 here, also because...
hw/scsi/scsi-disk.c static char *scsi_property_get_loadparm(Object *obj, Error **errp) · line 3154
Quoted 6 lines
hw/scsi/scsi-disk.c +1
-5-if (!qdev_prop_sanitize_s390x_loadparm (lp_str, value, errp)) { -g_free (lp_str); -return ; - } -SCSI_DISK_BASE (obj)->loadparm = lp_str; +qdev_prop_sanitize_s390x_loadparm (SCSI_DISK_BASE (obj)->loadparm, value, errp);
... this would overwrite SCSI_DISK_BASE(obj)->loadparm in case of error.
Note how the code is setting loadparm only after a successfulqdev_prop_sanitize_s390x_loadparm. That's not a problem in practice, because failing to set a property is usually fatal, but not good style either.
Thanks,
Paolo
Re: [PATCH 2/2] hw/scsi/scsi-disk: Avoid buffer overrun parsing 'loadparam'
Quoted 29 lines · Paolo Bonzini
hw/scsi/scsi-disk.c struct SCSIDiskState { · line 116
Quoted 6 lines · Philippe Mathieu-Daudé
hw/scsi/scsi-disk.c +1
-1@@ -112,7 +113,7 @@ struct SCSIDiskState { char *vendor; char *product; char *device_id; - char *loadparm; /* only for s390x */ + char loadparm[LOADPARM_LEN]; /* only for s390x */You would need a +1 here because of
static char *scsi_property_get_loadparm(Object *obj, Error **errp) {
return g_strdup(SCSI_DISK_BASE(obj)->loadparm);
}expecting NUL-termination as well.
hw/scsi/scsi-disk.c static char *scsi_property_get_loadparm(Object *obj, Error **errp) · line 3155
Quoted 1 line
hw/scsi/scsi-disk.c +0
-1- lp_str = g_malloc0(strlen(value));I have sent a pull request that simply adds the +1 here, also because...
Quoted 7 lines
hw/scsi/scsi-disk.c +1
-5- if (!qdev_prop_sanitize_s390x_loadparm(lp_str, value, errp)) { - g_free(lp_str); - return; - } - SCSI_DISK_BASE(obj)->loadparm = lp_str; + qdev_prop_sanitize_s390x_loadparm(SCSI_DISK_BASE(obj)->loadparm,value, errp);
Quoted 3 lines
... this would overwrite SCSI_DISK_BASE(obj)->loadparm in case of error.
Note how the code is setting loadparm only after a successful
qdev_prop_sanitize_s390x_loadparm. That's not a problem in practice, because failing to set a property is usually fatal, but not good style either.
I guess I was not well awake :) Please disregard this patch.
Re: [PATCH 2/2] hw/scsi/scsi-disk: Avoid buffer overrun parsing 'loadparam'
In reply to Philippe Mathieu-DaudéAm 20.11.2024 um 09:53 hat Philippe Mathieu-Daudé geschrieben:
Quoted 6 lines
Coverity reported a 1 byte overrun in
scsi_property_set_loadparm(CID 15657462). Since loadparam[] length is known, simply directly allocate it in the device state.Fixes 429442e52d ("hw: Add 'loadparm' property to scsi disk devices")Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Paolo already sent a pull request for a different fix (just allocating one byte more). I think that's the better approach because other users might expect the string to actually be null terminated.
Such as scsi_property_get_loadparm(), which you forgot to update:
static char *scsi_property_get_loadparm(Object *obj, Error **errp)
{
return g_strdup(SCSI_DISK_BASE(obj)->loadparm);
}Kevin
Re: [PATCH 2/2] hw/scsi/scsi-disk: Avoid buffer overrun parsing 'loadparam'
Quoted 12 lines · Kevin Wolf
Am 20.11.2024 um 09:53 hat Philippe Mathieu-Daudé geschrieben:
Quoted 6 lines
Coverity reported a 1 byte overrun in
scsi_property_set_loadparm(CID 15657462). Since loadparam[] length is known, simply directly allocate it in the device state.Fixes 429442e52d ("hw: Add 'loadparm' property to scsi disk devices")Signed-off-by Philippe Mathieu-Daudé <[email protected]>Paolo already sent a pull request for a different fix (just allocating one byte more). I think that's the better approach because other users might expect the string to actually be null terminated.
Quoted 6 lines
Such as
scsi_property_get_loadparm(), which you forgot to update:static char *scsi_property_get_loadparm(Object *obj, Error **errp) { return g_strdup(SCSI_DISK_BASE(obj)->loadparm); }
Yeah I missed that.
Maybe consider the first patch as cleanup for 10.0? I can repost later.
Regards,
Phil.