hvmloader: fix two issues spotted by Coverity
cover hvmloader: fix two issues spotted by Coverity
Wei Liu (2):
hvmloader: correctly copy signature to info structures
hvmloader: cast to 64bit before multiplication in get_module_entry
tools/firmware/hvmloader/hvmloader.c | 4 ++-- tools/firmware/hvmloader/ovmf.c | 3 ++- tools/firmware/hvmloader/seabios.c | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-)
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 1/2 hvmloader: correctly copy signature to info structures
The original code used sizeof(info->signature) as the size parameter for memcpy, which was wrong.
Fix that by calculating the correct size.
--- tools/firmware/hvmloader/ovmf.c | 3 ++- tools/firmware/hvmloader/seabios.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-)
tools/firmware/hvmloader/ovmf.c +2 -1
@@ -67,10 +67,11 @@ struct ovmf_info {static void ovmf_setup_bios_info (void ) {struct ovmf_info *info = (void *)OVMF_INFO_PHYSICAL_ADDRESS ; +const char sig[] ="XenHVMOVMF" ;memset (info,0 ,sizeof (*info)); -memcpy (info->signature,"XenHVMOVMF" ,sizeof (info->signature)); +memcpy (info->signature, sig,sizeof (sig)); info->length =sizeof (*info); }
tools/firmware/hvmloader/seabios.c +2 -1
@@ -55,10 +55,11 @@ struct seabios_info {static void seabios_setup_bios_info (void ) {struct seabios_info *info = (void *)BIOS_INFO_PHYSICAL_ADDRESS ; +const char sig[] ="XenHVMSeaBIOS" ;memset (info,0 ,sizeof (*info)); -memcpy (info->signature,"XenHVMSeaBIOS" ,sizeof (info->signature)); +memcpy (info->signature, sig,sizeof (sig)); info->length =sizeof (*info); info->tables = (uint32_t )scratch_alloc (MAX_TABLES *sizeof (uint32_t ),0 );
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 2/2 hvmloader: cast to 64bit before multiplication in get_module_entry
In reply to Wei LiuCoverity complains:
overflow_before_widen: Potentially overflowing expression info->nr_modules * 32U with type unsigned int (32 bits, unsigned) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type uint64_t (64 bits, unsigned).
The overflow is unlikely to happen in reality because we only expect a few modules.
Fix that by casting the variable to 64bit before multiplication to placate Coverity.
--- tools/firmware/hvmloader/hvmloader.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/firmware/hvmloader/hvmloader.c +2 -2
@@ -272,8 +272,8 @@ const struct hvm_modlist_entry *get_module_entry(if ( !modlist || info->modlist_paddr >UINTPTR_MAX || - (info->modlist_paddr + info->nr_modules *sizeof (*modlist) -1 ) - >UINTPTR_MAX ) + (info->modlist_paddr + + (uint64_t )info->nr_modules *sizeof (*modlist) -1 ) >UINTPTR_MAX )return NULL ;for ( i =0 ; i < info->nr_modules; i++ )
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 1/2] hvmloader: correctly copy signature to info structures
In reply to Wei LiuQuoted 5 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 10:06, <[email protected]> wrote:
tools/firmware/hvmloader/ovmf.c +0
-0@@ -67,10 +67,11 @@ struct ovmf_info {static void ovmf_setup_bios_info (void )
tools/firmware/hvmloader/ovmf.c struct ovmf_info { · line 76
Quoted 10 lines
tools/firmware/hvmloader/ovmf.c +2
-1{struct ovmf_info *info = (void *)OVMF_INFO_PHYSICAL_ADDRESS ; +const char sig[] ="XenHVMOVMF" ;memset (info,0 ,sizeof (*info)); -memcpy (info->signature,"XenHVMOVMF" ,sizeof (info->signature)); +memcpy (info->signature, sig,sizeof (sig)); info->length =sizeof (*info); }
I think using strncpy() would be more natural in cases like this, as it would at once make clear that the destination can't be overrun no matter how large the string literal.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/2] hvmloader: cast to 64bit before multiplication in get_module_entry
In reply to Wei LiuQuoted 7 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 10:06, <[email protected]> wrote:
Coverity complains:
overflow_before_widen: Potentially overflowing expressioninfo->nr_modules* 32U with type unsigned int (32 bits, unsigned) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of typeuint64_t(64 bits, unsigned).
To me this is Coverity splitting hair, to be honest.
tools/firmware/hvmloader/hvmloader.c const struct hvm_modlist_entry *get_module_entry( · line 277
Quoted 11 lines
tools/firmware/hvmloader/hvmloader.c +2
-2@@ -272,8 +272,8 @@ const struct hvm_modlist_entry *get_module_entry(if ( !modlist || info->modlist_paddr >UINTPTR_MAX || - (info->modlist_paddr + info->nr_modules *sizeof (*modlist) -1 ) - >UINTPTR_MAX ) + (info->modlist_paddr + + (uint64_t )info->nr_modules *sizeof (*modlist) -1 ) >UINTPTR_MAX )return NULL ;
This can be had without resorting to 64-bit multiplication, by bounds checking
(UINTPTR_MAX - (uintptr_t)info->modlist_paddr) / sizeof(*modlist)
instead. While we would certainly hope that compilers don't resort to a libgcc helper for 64-bit multiplication, I think we'd better avoid that risk altogether.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 1/2] hvmloader: correctly copy signature to info structures
In reply to Jan BeulichQuoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 10:06, <[email protected]> wrote:
tools/firmware/hvmloader/ovmf.c +1
-0@@ -67,10 +67,11 @@ struct ovmf_info {static void ovmf_setup_bios_info (void ) {struct ovmf_info *info = (void *)OVMF_INFO_PHYSICAL_ADDRESS ; +const char sig[] ="XenHVMOVMF" ;memset (info,0 ,sizeof (*info));
Quoted 7 lines
tools/firmware/hvmloader/ovmf.c struct ovmf_info { · line 76
Quoted 4 lines
tools/firmware/hvmloader/ovmf.c +1
-1-memcpy (info->signature,"XenHVMOVMF" ,sizeof (info->signature)); +memcpy (info->signature, sig,sizeof (sig)); info->length =sizeof (*info); }I think using
strncpy()would be more natural in cases like this, as it would at once make clear that the destination can't be overrun no matter how large the string literal.
How about structure assignment?
*info = (struct ovmf_info) { .signature = "XenHVMOVMF", .length = sizeof(*info) }
which also subsumed the memset()?
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/2] hvmloader: cast to 64bit before multiplication in get_module_entry
In reply to Jan BeulichQuoted 8 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 10:06, <[email protected]> wrote:
Coverity complains:
overflow_before_widen: Potentially overflowing expressioninfo->nr_modules* 32U with type unsigned int (32 bits, unsigned) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of typeuint64_t(64 bits, unsigned).To me this is Coverity splitting hair, to be honest.
A very large number of security holes are because of this precise programming mistake.
Coverity is doing its job correctly; it is up to a human to decide whether we care or not.
Quoted 15 lines
tools/firmware/hvmloader/hvmloader.c const struct hvm_modlist_entry *get_module_entry( · line 277
Quoted 12 lines
tools/firmware/hvmloader/hvmloader.c +2
-2@@ -272,8 +272,8 @@ const struct hvm_modlist_entry *get_module_entry(if ( !modlist || info->modlist_paddr >UINTPTR_MAX || - (info->modlist_paddr + info->nr_modules *sizeof (*modlist) -1 ) - >UINTPTR_MAX ) + (info->modlist_paddr + + (uint64_t )info->nr_modules *sizeof (*modlist) -1 ) >UINTPTR_MAX )return NULL ;This can be had without resorting to 64-bit multiplication, by bounds checking
Quoted 5 lines
(UINTPTR_MAX - (uintptr_t)info->modlist_paddr) / sizeof(*modlist)instead. While we would certainly hope that compilers don't resort to a libgcc helper for 64-bit multiplication, I think we'd better avoid that risk altogether.
In this case, using libgcc would cause a link error because of -fno-builtin, so I don't think it is too bad. I would be surprised if we didn't have other 64bit multiplication in hvmloader.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 1/2] hvmloader: correctly copy signature to info structures
In reply to Andrew CooperQuoted 21 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 11:42, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 10:06, <[email protected]> wrote:
tools/firmware/hvmloader/ovmf.c +1
-0@@ -67,10 +67,11 @@ struct ovmf_info {static void ovmf_setup_bios_info (void ) {struct ovmf_info *info = (void *)OVMF_INFO_PHYSICAL_ADDRESS ; +const char sig[] ="XenHVMOVMF" ;memset (info,0 ,sizeof (*info));Quoted 7 lines
tools/firmware/hvmloader/ovmf.c struct ovmf_info { · line 76
Quoted 4 lines
tools/firmware/hvmloader/ovmf.c +1
-1-memcpy (info->signature,"XenHVMOVMF" ,sizeof (info->signature)); +memcpy (info->signature, sig,sizeof (sig)); info->length =sizeof (*info); }I think using
strncpy()would be more natural in cases like this, as it would at once make clear that the destination can't be overrun no matter how large the string literal.
Quoted 6 lines
How about structure assignment?
*info = (struct
ovmf_info) { .signature = "XenHVMOVMF", .length =sizeof(*info)}which also subsumed the
memset()?
Fine with me, albeit maybe a little uglier to read.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/2] hvmloader: cast to 64bit before multiplication in get_module_entry
In reply to Andrew CooperQuoted 17 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 12:09, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
tools/firmware/hvmloader/hvmloader.c const struct hvm_modlist_entry *get_module_entry( · line 277
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 10:06, <[email protected]> wrote:
tools/firmware/hvmloader/hvmloader.c +2
-2@@ -272,8 +272,8 @@ const struct hvm_modlist_entry *get_module_entry(if ( !modlist || info->modlist_paddr >UINTPTR_MAX || - (info->modlist_paddr + info->nr_modules *sizeof (*modlist) -1 ) - >UINTPTR_MAX ) + (info->modlist_paddr + + (uint64_t )info->nr_modules *sizeof (*modlist) -1 ) >UINTPTR_MAX )return NULL ;This can be had without resorting to 64-bit multiplication, by bounds checking
Quoted 8 lines
Quoted 5 lines
(UINTPTR_MAX - (uintptr_t)info->modlist_paddr) / sizeof(*modlist)instead. While we would certainly hope that compilers don't resort to a libgcc helper for 64-bit multiplication, I think we'd better avoid that risk altogether.
In this case, using libgcc would cause a link error because of -fno-builtin, so I don't think it is too bad.
And it's this link error which I want to avoid.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/2] hvmloader: cast to 64bit before multiplication in get_module_entry
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 12:09, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich
tools/firmware/hvmloader/hvmloader.c const struct hvm_modlist_entry *get_module_entry( · line 277
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 10:06, <[email protected]> wrote:
tools/firmware/hvmloader/hvmloader.c +2
-2@@ -272,8 +272,8 @@ const struct hvm_modlist_entry *get_module_entry(if ( !modlist || info->modlist_paddr >UINTPTR_MAX || - (info->modlist_paddr + info->nr_modules *sizeof (*modlist) -1 ) - >UINTPTR_MAX ) + (info->modlist_paddr + + (uint64_t )info->nr_modules *sizeof (*modlist) -1 ) >UINTPTR_MAX )return NULL ;This can be had without resorting to 64-bit multiplication, by bounds checking
(UINTPTR_MAX - (uintptr_t)info->modlist_paddr) / sizeof(*modlist)
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
instead. While we would certainly hope that compilers don't resort to a libgcc helper for 64-bit multiplication, I think we'd better avoid that risk altogether.
In this case, using libgcc would cause a link error because of -fno-builtin, so I don't think it is too bad.
And it's this link error which I want to avoid.
What approach should I use? I would like to clear this minor issue as quick as possible.
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/2] hvmloader: cast to 64bit before multiplication in get_module_entry
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 22.08.16 at 13:37, <[email protected]> wrote:
Quoted 23 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 12:09, <[email protected]> wrote:
Quoted 20 lines · Jan Beulich
tools/firmware/hvmloader/hvmloader.c const struct hvm_modlist_entry *get_module_entry( · line 277
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 10:06, <[email protected]> wrote:
tools/firmware/hvmloader/hvmloader.c +2
-2@@ -272,8 +272,8 @@ const struct hvm_modlist_entry *get_module_entry(if ( !modlist || info->modlist_paddr >UINTPTR_MAX || - (info->modlist_paddr + info->nr_modules *sizeof (*modlist) -1 ) - >UINTPTR_MAX ) + (info->modlist_paddr + + (uint64_t )info->nr_modules *sizeof (*modlist) -1 ) >UINTPTR_MAX )return NULL ;This can be had without resorting to 64-bit multiplication, by bounds checking
(UINTPTR_MAX - (uintptr_t)info->modlist_paddr) / sizeof(*modlist)instead. While we would certainly hope that compilers don't resort to a libgcc helper for 64-bit multiplication, I think we'd better avoid that risk altogether.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
In this case, using libgcc would cause a link error because of -fno-builtin, so I don't think it is too bad.
And it's this link error which I want to avoid.
What approach should I use? I would like to clear this minor issue as quick as possible.
Well, if Andrew wants to ack the patch with the above unchanged, I won't object. I continue to prefer the suggested alternative though.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel