Skip to contentSkip to conversation

hvmloader: fix two issues spotted by Coverity

PATCH In review Wei Liu 2 participants 6 messages Aug 22, 2016

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

v2 cover hvmloader: fix two issues spotted by Coverity

Wei Liu (2):
hvmloader: correctly copy signature to info structures
hvmloader: use bound checking in get_module_entry

 tools/firmware/hvmloader/hvmloader.c | 4 ++--
 tools/firmware/hvmloader/ovmf.c      | 8 ++++----
 tools/firmware/hvmloader/seabios.c   | 8 ++++----
 3 files changed, 10 insertions(+), 10 deletions(-)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#3 lore raw

PATCH v2 2/2 hvmloader: use bound checking in get_module_entry

Coverity 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 converting the check to use bound checking to placate Coverity.

---
 tools/firmware/hvmloader/hvmloader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/firmware/hvmloader/hvmloader.c +2 -2
index 7b32d86..bbd4e34 100644
@@ -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 )
+         (UINTPTR_MAX - (uintptr_t)info->modlist_paddr) / sizeof(*modlist)
+         < info->nr_modules )
         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
author
#1 lore raw

PATCH v2 1/2 hvmloader: correctly copy signature to info structures

In reply to Wei Liu

The original code used sizeof(info->signature) as the size parameter for memcpy, which was wrong.

Fix that by using structure assignment.

---
 tools/firmware/hvmloader/ovmf.c    | 8 ++++----
 tools/firmware/hvmloader/seabios.c | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
tools/firmware/hvmloader/ovmf.c +4 -4
index b4bcc93..0ac3416 100644
@@ -68,10 +68,10 @@ static void ovmf_setup_bios_info(void)
 {
     struct ovmf_info *info = (void *)OVMF_INFO_PHYSICAL_ADDRESS;
 
-    memset(info, 0, sizeof(*info));
-
-    memcpy(info->signature, "XenHVMOVMF", sizeof(info->signature));
-    info->length = sizeof(*info);
+    *info = (struct ovmf_info) {
+        .signature = "XenHVMOVMF",
+        .length = sizeof(*info)
+    };
 }
 
 static void ovmf_finish_bios_info(void)
tools/firmware/hvmloader/seabios.c +4 -4
index 5c9a351..44ff0d7 100644
@@ -56,10 +56,10 @@ static void seabios_setup_bios_info(void)
 {
     struct seabios_info *info = (void *)BIOS_INFO_PHYSICAL_ADDRESS;
 
-    memset(info, 0, sizeof(*info));
-
-    memcpy(info->signature, "XenHVMSeaBIOS", sizeof(info->signature));
-    info->length = sizeof(*info);
+    *info = (struct seabios_info) {
+        .signature = "XenHVMSeaBIOS",
+        .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
Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] hvmloader: correctly copy signature to info structures

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.08.16 at 14:47, <[email protected]> wrote:

The original code used sizeof(info->signature) as the size parameter for memcpy, which was wrong.

Fix that by using structure assignment.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Acked-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] hvmloader: use bound checking in get_module_entry

In reply to Wei Liu
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 22.08.16 at 14:47, <[email protected]> wrote:

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

Quoted 7 lines

The overflow is unlikely to happen in reality because we only expect a few modules.

Fix that by converting the check to use bound checking to placate Coverity.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel