9ae3a8
From c44c930396e5c19511f36bc45c3f386966b15f9d Mon Sep 17 00:00:00 2001
9ae3a8
From: Richard Jones <rjones@redhat.com>
9ae3a8
Date: Thu, 11 Jun 2015 11:40:28 +0200
9ae3a8
Subject: [PATCH 28/30] block/curl: Improve type safety of s->timeout.
9ae3a8
9ae3a8
Message-id: <1434022828-13037-22-git-send-email-rjones@redhat.com>
9ae3a8
Patchwork-id: 65856
9ae3a8
O-Subject: [RHEL-7.2 qemu-kvm v3 PATCH 21/21] block/curl: Improve type safety of s->timeout.
9ae3a8
Bugzilla: 1226684
9ae3a8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
9ae3a8
From: "Richard W.M. Jones" <rjones@redhat.com>
9ae3a8
9ae3a8
qemu_opt_get_number returns a uint64_t, and curl_easy_setopt expects a
9ae3a8
long (not an int).  There is no warning about the latter type error
9ae3a8
because curl_easy_setopt uses a varargs argument.
9ae3a8
9ae3a8
Store the timeout (which is a positive number of seconds) as a
9ae3a8
uint64_t.  Check that the number given by the user is reasonable.
9ae3a8
Zero is permissible (meaning no timeout is enforced by cURL).
9ae3a8
9ae3a8
Cast it to long before calling curl_easy_setopt to fix the type error.
9ae3a8
9ae3a8
Example error message after this change has been applied:
9ae3a8
9ae3a8
$ ./qemu-img create -f qcow2 /tmp/test.qcow2 \
9ae3a8
    -b 'json: { "file.driver":"https",
9ae3a8
                "file.url":"https://foo/bar",
9ae3a8
                "file.timeout":-1 }'
9ae3a8
qemu-img: /tmp/test.qcow2: Could not open 'json: { "file.driver":"https", "file.url":"https://foo/bar", "file.timeout":-1 }': timeout parameter is too large or negative: Invalid argument
9ae3a8
9ae3a8
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
9ae3a8
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
Reviewed-by: Gonglei <arei.gonglei@huawei.com>
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
Upstream-status: f76faeda4bd59f972d09dd9d954297f17c21dd60
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/curl.c | 9 +++++++--
9ae3a8
 1 file changed, 7 insertions(+), 2 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/curl.c b/block/curl.c
9ae3a8
index 5b407aa..3088329 100644
9ae3a8
--- a/block/curl.c
9ae3a8
+++ b/block/curl.c
9ae3a8
@@ -64,6 +64,7 @@ static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
9ae3a8
 #define SECTOR_SIZE     512
9ae3a8
 #define READ_AHEAD_DEFAULT (256 * 1024)
9ae3a8
 #define CURL_TIMEOUT_DEFAULT 5
9ae3a8
+#define CURL_TIMEOUT_MAX 10000
9ae3a8
 
9ae3a8
 #define FIND_RET_NONE   0
9ae3a8
 #define FIND_RET_OK     1
9ae3a8
@@ -112,7 +113,7 @@ typedef struct BDRVCURLState {
9ae3a8
     char *url;
9ae3a8
     size_t readahead_size;
9ae3a8
     bool sslverify;
9ae3a8
-    int timeout;
9ae3a8
+    uint64_t timeout;
9ae3a8
     char *cookie;
9ae3a8
     bool accept_range;
9ae3a8
 } BDRVCURLState;
9ae3a8
@@ -387,7 +388,7 @@ static CURLState *curl_init_state(BDRVCURLState *s)
9ae3a8
         if (s->cookie) {
9ae3a8
             curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
9ae3a8
         }
9ae3a8
-        curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, s->timeout);
9ae3a8
+        curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
9ae3a8
         curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
9ae3a8
                          (void *)curl_read_cb);
9ae3a8
         curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
9ae3a8
@@ -496,6 +497,10 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
 
9ae3a8
     s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
9ae3a8
                                      CURL_TIMEOUT_DEFAULT);
9ae3a8
+    if (s->timeout > CURL_TIMEOUT_MAX) {
9ae3a8
+        error_setg(errp, "timeout parameter is too large or negative");
9ae3a8
+        goto out_noclean;
9ae3a8
+    }
9ae3a8
 
9ae3a8
     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8