Skip to contentSkip to conversation

elf2dmp: Fix minor Coverity nits

PATCH In review Philippe Mathieu-Daudé 3 participants 6 messages Sep 10, 2021 – Sep 16, 2021

lore
Share & tools
lore
#0 lore raw

v2 cover elf2dmp: Fix minor Coverity nits

This is a respin of Peter Maydell series, with slightly different logic on the first patch. Quoting v1 cover [*]:

Coverity complains about a couple of minor issues in elf2dmp:
* we weren't checking the return value from curl_easy_setopt()
* we might divide by zero if presented with a corrupt PDB file

This patchseries fixes those.

Viktor Prutyanov tested the patch but didn't provided a formal Tested-by tag, so I haven't included it in the patches.

[*] https://lore.kernel.org/qemu-devel/[email protected]/

Peter Maydell (2):
elf2dmp: Check curl_easy_setopt() return value
elf2dmp: Fail cleanly if PDB file specifies zero block_size

 contrib/elf2dmp/download.c | 22 ++++++++++------------
 contrib/elf2dmp/pdb.c      |  4 ++++
 2 files changed, 14 insertions(+), 12 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/2 elf2dmp: Check curl_easy_setopt() return value

From: Peter Maydell <[email protected]>

Coverity points out that we aren't checking the return value from curl_easy_setopt().

Fixes Coverity CID 1458895

---
Informal T-b tag on
https://lore.kernel.org/qemu-devel/[email protected]/

v1 from Peter:
https://lore.kernel.org/qemu-devel/[email protected]/

---
 contrib/elf2dmp/download.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
contrib/elf2dmp/download.c +10 -12
index d09e607431f..bd7650a7a27 100644
@@ -25,21 +25,19 @@ int download_url(const char *name, const char *url)
         goto out_curl;
     }
 
-    curl_easy_setopt(curl, CURLOPT_URL, url);
-    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
-    curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
-    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
-    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
-
-    if (curl_easy_perform(curl) != CURLE_OK) {
-        err = 1;
-        fclose(file);
+    if (curl_easy_setopt(curl, CURLOPT_URL, url) != CURLE_OK
+            || curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL) != CURLE_OK
+            || curl_easy_setopt(curl, CURLOPT_WRITEDATA, file) != CURLE_OK
+            || curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1) != CURLE_OK
+            || curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0) != CURLE_OK
+            || curl_easy_perform(curl) != CURLE_OK) {
         unlink(name);
-        goto out_curl;
+        fclose(file);
+        err = 1;
+    } else {
+        err = fclose(file);
     }
 
-    err = fclose(file);
-
 out_curl:
     curl_easy_cleanup(curl);
 
Signature · 1 line
2.31.1
Reviewed-by Tested-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] elf2dmp: Check curl_easy_setopt() return value

Hi,

Quoted 49 lines · On Fri, 10 Sep 2021 19:06:55 +0200 Philippe Mathieu-Daudé <[email protected]>…

From: Peter Maydell <[email protected]>

Coverity points out that we aren't checking the return value from curl_easy_setopt().

Fixes Coverity CID 1458895

---
Informal T-b tag on
https://lore.kernel.org/qemu-devel/[email protected]/

v1 from Peter:
https://lore.kernel.org/qemu-devel/[email protected]/

---
 contrib/elf2dmp/download.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
contrib/elf2dmp/download.c +2 -9
index d09e607431f..bd7650a7a27 100644
@@ -25,21 +25,19 @@ int download_url(const char *name, const char
 *url) goto out_curl;
     }
 
-    curl_easy_setopt(curl, CURLOPT_URL, url);
-    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
-    curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
-    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
-    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
-
-    if (curl_easy_perform(curl) != CURLE_OK) {
-        err = 1;
-        fclose(file);
+    if (curl_easy_setopt(curl, CURLOPT_URL, url) != CURLE_OK
+            || curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL)

!= CURLE_OK
+ || curl_easy_setopt(curl, CURLOPT_WRITEDATA, file) !=
CURLE_OK
+ || curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1) !=
CURLE_OK
+ || curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0) !=
CURLE_OK

contrib/elf2dmp/download.c +1 -1
+            || curl_easy_perform(curl) != CURLE_OK) {
         unlink(name);
-        goto out_curl;

contrib/elf2dmp/download.c int download_url(const char *name, const char *url) · line 42

Quoted 10 lines
contrib/elf2dmp/download.c +4 -2
+        fclose(file);
+        err = 1;
+    } else {
+        err = fclose(file);
     }
 
-    err = fclose(file);
-
 out_curl:
     curl_easy_cleanup(curl);
Signature · 1 line
Viktor Prutyanov
#3 lore raw

PATCH v2 2/2 elf2dmp: Fail cleanly if PDB file specifies zero block_size

In reply to Philippe Mathieu-Daudé

From: Peter Maydell <[email protected]>

Coverity points out that if the PDB file we're trying to read has a header specifying a block_size of zero then we will end up trying to divide by zero in pdb_ds_read_file(). Check for this and fail cleanly instead.

Fixes Coverity CID 1458869

Message-Id: <[email protected]>

---
Informal T-b tag on
https://lore.kernel.org/qemu-devel/[email protected]/

---
 contrib/elf2dmp/pdb.c | 4 ++++
 1 file changed, 4 insertions(+)
contrib/elf2dmp/pdb.c +4 -0
index b3a65470680..adcfa7e154c 100644
@@ -215,6 +215,10 @@ out_symbols:
 
 static int pdb_reader_ds_init(struct pdb_reader *r, PDB_DS_HEADER *hdr)
 {
+    if (hdr->block_size == 0) {
+        return 1;
+    }
+
     memset(r->file_used, 0, sizeof(r->file_used));
     r->ds.header = hdr;
     r->ds.toc = pdb_ds_read(hdr, (uint32_t *)((uint8_t *)hdr +
Signature · 1 line
2.31.1
Tested-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] elf2dmp: Fail cleanly if PDB file specifies zero block_size

Hi,

Quoted 25 lines · On Fri, 10 Sep 2021 19:06:56 +0200 Philippe Mathieu-Daudé <[email protected]>…

From: Peter Maydell <[email protected]>

Coverity points out that if the PDB file we're trying to read has a header specifying a block_size of zero then we will end up trying to divide by zero in pdb_ds_read_file(). Check for this and fail cleanly instead.

Fixes Coverity CID 1458869

Message-Id: <[email protected]>

---
Informal T-b tag on
https://lore.kernel.org/qemu-devel/[email protected]/

---
 contrib/elf2dmp/pdb.c | 4 ++++
 1 file changed, 4 insertions(+)
contrib/elf2dmp/pdb.c +0 -0
index b3a65470680..adcfa7e154c 100644

contrib/elf2dmp/pdb.c out_symbols: · line 224

Quoted 10 lines
contrib/elf2dmp/pdb.c +4 -0
@@ -215,6 +215,10 @@ out_symbols:
 
 static int pdb_reader_ds_init(struct pdb_reader *r, PDB_DS_HEADER
 *hdr) {
+    if (hdr->block_size == 0) {
+        return 1;
+    }
+
     memset(r->file_used, 0, sizeof(r->file_used));
     r->ds.header = hdr;
     r->ds.toc = pdb_ds_read(hdr, (uint32_t *)((uint8_t *)hdr +
Signature · 1 line
Viktor Prutyanov
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 10 lines · Philippe Mathieu-Daudé

This is a respin of Peter Maydell series, with slightly different logic on the first patch. Quoting v1 cover [*]:

Coverity complains about a couple of minor issues in elf2dmp:
* we weren't checking the return value from curl_easy_setopt()
* we might divide by zero if presented with a corrupt PDB file

This patchseries fixes those.

Quoted 8 lines

Viktor Prutyanov tested the patch but didn't provided a formal Tested-by tag, so I haven't included it in the patches.

[*] https://lore.kernel.org/qemu-devel/[email protected]/

Peter Maydell (2):
elf2dmp: Check curl_easy_setopt() return value
elf2dmp: Fail cleanly if PDB file specifies zero block_size

Thanks for doing the respin; I'll take this via target-arm.next.

-- PMM