block/curl.c: Check error return from curl_easy_setopt()
PATCH 1/1 block/curl.c: Check error return from curl_easy_setopt()
Coverity points out that we aren't checking the return value from curl_easy_setopt() for any of the calls to it we make in block/curl.c.
---
Big fat disclaimer: tested only with 'make check', which I suspect may not be exercising this block backend. Hints on how to test more thoroughly are welcome.
block/curl.c | 90 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 32 deletions(-)
block/curl.c +58 -32
@@ -458,38 +458,51 @@ static int curl_init_state(BDRVCURLState *s, CURLState *state)if (!state->curl) {return -EIO ; } -curl_easy_setopt (state->curl,CURLOPT_URL , s->url); -curl_easy_setopt (state->curl,CURLOPT_SSL_VERIFYPEER , - (long ) s->sslverify); -curl_easy_setopt (state->curl,CURLOPT_SSL_VERIFYHOST , - s->sslverify ?2L :0L ); -if (s->cookie) { -curl_easy_setopt (state->curl,CURLOPT_COOKIE , s->cookie); +if (curl_easy_setopt (state->curl,CURLOPT_URL , s->url) || +curl_easy_setopt (state->curl,CURLOPT_SSL_VERIFYPEER , + (long ) s->sslverify) || +curl_easy_setopt (state->curl,CURLOPT_SSL_VERIFYHOST , + s->sslverify ?2L :0L )) { + goto err; + } +if (s->cookie) { +if (curl_easy_setopt (state->curl,CURLOPT_COOKIE , s->cookie)) { + goto err; + } + } +if (curl_easy_setopt (state->curl,CURLOPT_TIMEOUT , (long )s->timeout) || +curl_easy_setopt (state->curl,CURLOPT_WRITEFUNCTION , + (void *)curl_read_cb) || +curl_easy_setopt (state->curl,CURLOPT_WRITEDATA , (void *)state) || +curl_easy_setopt (state->curl,CURLOPT_PRIVATE , (void *)state) || +curl_easy_setopt (state->curl,CURLOPT_AUTOREFERER ,1 ) || +curl_easy_setopt (state->curl,CURLOPT_FOLLOWLOCATION ,1 ) || +curl_easy_setopt (state->curl,CURLOPT_NOSIGNAL ,1 ) || +curl_easy_setopt (state->curl,CURLOPT_ERRORBUFFER , state->errmsg) || +curl_easy_setopt (state->curl,CURLOPT_FAILONERROR ,1 )) { + goto err; } -curl_easy_setopt (state->curl,CURLOPT_TIMEOUT , (long )s->timeout); -curl_easy_setopt (state->curl,CURLOPT_WRITEFUNCTION , - (void *)curl_read_cb); -curl_easy_setopt (state->curl,CURLOPT_WRITEDATA , (void *)state); -curl_easy_setopt (state->curl,CURLOPT_PRIVATE , (void *)state); -curl_easy_setopt (state->curl,CURLOPT_AUTOREFERER ,1 ); -curl_easy_setopt (state->curl,CURLOPT_FOLLOWLOCATION ,1 ); -curl_easy_setopt (state->curl,CURLOPT_NOSIGNAL ,1 ); -curl_easy_setopt (state->curl,CURLOPT_ERRORBUFFER , state->errmsg); -curl_easy_setopt (state->curl,CURLOPT_FAILONERROR ,1 ); -if (s->username) { -curl_easy_setopt (state->curl,CURLOPT_USERNAME , s->username); +if (curl_easy_setopt (state->curl,CURLOPT_USERNAME , s->username)) { + goto err; + } }if (s->password) { -curl_easy_setopt (state->curl,CURLOPT_PASSWORD , s->password); +if (curl_easy_setopt (state->curl,CURLOPT_PASSWORD , s->password)) { + goto err; + } }if (s->proxyusername) { -curl_easy_setopt (state->curl, -CURLOPT_PROXYUSERNAME , s->proxyusername); +if (curl_easy_setopt (state->curl, +CURLOPT_PROXYUSERNAME , s->proxyusername)) { + goto err; + } }if (s->proxypassword) { -curl_easy_setopt (state->curl, -CURLOPT_PROXYPASSWORD , s->proxypassword); +if (curl_easy_setopt (state->curl, +CURLOPT_PROXYPASSWORD , s->proxypassword)) { + goto err; + } }/* Restrict supported protocols to avoid security issues in the more
@@ -499,18 +512,27 @@ static int curl_init_state(BDRVCURLState *s, CURLState *state)* Restricting protocols is only supported from 7.19.4 upwards. */ #if LIBCURL_VERSION_NUM >=0x071304 -curl_easy_setopt (state->curl,CURLOPT_PROTOCOLS ,PROTOCOLS ); -curl_easy_setopt (state->curl,CURLOPT_REDIR_PROTOCOLS ,PROTOCOLS ); +if (curl_easy_setopt (state->curl,CURLOPT_PROTOCOLS ,PROTOCOLS ) || +curl_easy_setopt (state->curl,CURLOPT_REDIR_PROTOCOLS ,PROTOCOLS )) { + goto err; + }#endif #ifdef DEBUG_VERBOSE -curl_easy_setopt (state->curl,CURLOPT_VERBOSE ,1 ); +if (curl_easy_setopt (state->curl,CURLOPT_VERBOSE ,1 )) { + goto err; + }#endif } state->s = s;return 0 ; + +err: +curl_easy_cleanup (state->curl); + state->curl =NULL ; +return -EIO ; }/* Called with s->mutex held. */
@@ -763,10 +785,11 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags, } s->accept_range = false; -curl_easy_setopt (state->curl,CURLOPT_NOBODY ,1 ); -curl_easy_setopt (state->curl,CURLOPT_HEADERFUNCTION , - curl_header_cb); -curl_easy_setopt (state->curl,CURLOPT_HEADERDATA , s); +if (curl_easy_setopt (state->curl,CURLOPT_NOBODY ,1 ) || +curl_easy_setopt (state->curl,CURLOPT_HEADERFUNCTION , curl_header_cb) || +curl_easy_setopt (state->curl,CURLOPT_HEADERDATA , s)) { + goto out; + }if (curl_easy_perform (state->curl)) goto out;if (curl_easy_getinfo (state->curl,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &d)) {
@@ -879,7 +902,10 @@ static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)snprintf (state->range,127 ,"%" PRIu64"-%" PRIu64, start, end);trace_curl_setup_preadv (acb->bytes, start, state->range); -curl_easy_setopt (state->curl,CURLOPT_RANGE , state->range); +if (curl_easy_setopt (state->curl,CURLOPT_RANGE , state->range)) { +curl_clean_state (state); + goto out; + }if (curl_multi_add_handle (s->multi, state->curl) !=CURLM_OK ) { state->acb[0 ] =NULL ;
Signature · 1 line
2.25.1
Quoted 10 lines · Peter Maydell
Coverity points out that we aren't checking the return value from
curl_easy_setopt()for any of the calls to it we make inblock/curl.c.Fixes Coverity CID 1459336, 1459482, 1460331Signed-off-by Peter Maydell <[email protected]>---
Big fat disclaimer: tested only with 'make check', which I suspect may not be exercising this block backend. Hints on how to test more thoroughly are welcome.
yeah afaik, qemu-iotests don't have support for this. As a very basic smoke test do
$ qemu-img info https://cloud.debian.org/images/cloud/buster/daily/latest/debian-10-nocloud-amd64-daily.qcow2 image: https://cloud.debian.org/images/cloud/buster/daily/latest/debian-10-nocloud-amd64-daily.qcow2 file format: qcow2 virtual size: 2 GiB (2147483648 bytes) disk size: unavailable cluster_size: 65536 Format specific information: compat: 1.1 compression type: zlib lazy refcounts: false refcount bits: 16 corrupt: false extended l2: false
and/or
$ qemu-img info --image-opts driver=qcow2,file.driver=https,file.url=https://cloud.debian.org/images/cloud/buster/daily/latest/debian-10-nocloud-amd64-daily.qcow2 image: https://cloud.debian.org/images/cloud/buster/daily/latest/debian-10-nocloud-amd64-daily.qcow2 file format: qcow2 virtual size: 2 GiB (2147483648 bytes) disk size: unavailable cluster_size: 65536 Format specific information: compat: 1.1 compression type: zlib lazy refcounts: false refcount bits: 16 corrupt: false extended l2: false
Quoted 147 lines
block/curl.c | 90 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 32 deletions(-)block/curl.c +58
-32@@ -458,38 +458,51 @@ static int curl_init_state(BDRVCURLState *s, CURLState *state)if (!state->curl) {return -EIO ; } -curl_easy_setopt (state->curl,CURLOPT_URL , s->url); -curl_easy_setopt (state->curl,CURLOPT_SSL_VERIFYPEER , - (long ) s->sslverify); -curl_easy_setopt (state->curl,CURLOPT_SSL_VERIFYHOST , - s->sslverify ?2L :0L ); -if (s->cookie) { -curl_easy_setopt (state->curl,CURLOPT_COOKIE , s->cookie); +if (curl_easy_setopt (state->curl,CURLOPT_URL , s->url) || +curl_easy_setopt (state->curl,CURLOPT_SSL_VERIFYPEER , + (long ) s->sslverify) || +curl_easy_setopt (state->curl,CURLOPT_SSL_VERIFYHOST , + s->sslverify ?2L :0L )) { + goto err; + } +if (s->cookie) { +if (curl_easy_setopt (state->curl,CURLOPT_COOKIE , s->cookie)) { + goto err; + } + } +if (curl_easy_setopt (state->curl,CURLOPT_TIMEOUT , (long )s->timeout) || +curl_easy_setopt (state->curl,CURLOPT_WRITEFUNCTION , + (void *)curl_read_cb) || +curl_easy_setopt (state->curl,CURLOPT_WRITEDATA , (void *)state) || +curl_easy_setopt (state->curl,CURLOPT_PRIVATE , (void *)state) || +curl_easy_setopt (state->curl,CURLOPT_AUTOREFERER ,1 ) || +curl_easy_setopt (state->curl,CURLOPT_FOLLOWLOCATION ,1 ) || +curl_easy_setopt (state->curl,CURLOPT_NOSIGNAL ,1 ) || +curl_easy_setopt (state->curl,CURLOPT_ERRORBUFFER , state->errmsg) || +curl_easy_setopt (state->curl,CURLOPT_FAILONERROR ,1 )) { + goto err; } -curl_easy_setopt (state->curl,CURLOPT_TIMEOUT , (long )s->timeout); -curl_easy_setopt (state->curl,CURLOPT_WRITEFUNCTION , - (void *)curl_read_cb); -curl_easy_setopt (state->curl,CURLOPT_WRITEDATA , (void *)state); -curl_easy_setopt (state->curl,CURLOPT_PRIVATE , (void *)state); -curl_easy_setopt (state->curl,CURLOPT_AUTOREFERER ,1 ); -curl_easy_setopt (state->curl,CURLOPT_FOLLOWLOCATION ,1 ); -curl_easy_setopt (state->curl,CURLOPT_NOSIGNAL ,1 ); -curl_easy_setopt (state->curl,CURLOPT_ERRORBUFFER , state->errmsg); -curl_easy_setopt (state->curl,CURLOPT_FAILONERROR ,1 ); -if (s->username) { -curl_easy_setopt (state->curl,CURLOPT_USERNAME , s->username); +if (curl_easy_setopt (state->curl,CURLOPT_USERNAME , s->username)) { + goto err; + } }if (s->password) { -curl_easy_setopt (state->curl,CURLOPT_PASSWORD , s->password); +if (curl_easy_setopt (state->curl,CURLOPT_PASSWORD , s->password)) { + goto err; + } }if (s->proxyusername) { -curl_easy_setopt (state->curl, -CURLOPT_PROXYUSERNAME , s->proxyusername); +if (curl_easy_setopt (state->curl, +CURLOPT_PROXYUSERNAME , s->proxyusername)) { + goto err; + } }if (s->proxypassword) { -curl_easy_setopt (state->curl, -CURLOPT_PROXYPASSWORD , s->proxypassword); +if (curl_easy_setopt (state->curl, +CURLOPT_PROXYPASSWORD , s->proxypassword)) { + goto err; + } }/* Restrict supported protocols to avoid security issues in the more @@ -499,18 +512,27 @@ static int curl_init_state(BDRVCURLState *s, CURLState *state)* Restricting protocols is only supported from 7.19.4 upwards. */ #if LIBCURL_VERSION_NUM >=0x071304 -curl_easy_setopt (state->curl,CURLOPT_PROTOCOLS ,PROTOCOLS ); -curl_easy_setopt (state->curl,CURLOPT_REDIR_PROTOCOLS ,PROTOCOLS ); +if (curl_easy_setopt (state->curl,CURLOPT_PROTOCOLS ,PROTOCOLS ) || +curl_easy_setopt (state->curl,CURLOPT_REDIR_PROTOCOLS ,PROTOCOLS )) { + goto err; + }#endif #ifdef DEBUG_VERBOSE -curl_easy_setopt (state->curl,CURLOPT_VERBOSE ,1 ); +if (curl_easy_setopt (state->curl,CURLOPT_VERBOSE ,1 )) { + goto err; + }#endif } state->s = s;return 0 ; + +err: +curl_easy_cleanup (state->curl); + state->curl =NULL ; +return -EIO ; }/* Called with s->mutex held. */ @@ -763,10 +785,11 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags, } s->accept_range = false; -curl_easy_setopt (state->curl,CURLOPT_NOBODY ,1 ); -curl_easy_setopt (state->curl,CURLOPT_HEADERFUNCTION , - curl_header_cb); -curl_easy_setopt (state->curl,CURLOPT_HEADERDATA , s); +if (curl_easy_setopt (state->curl,CURLOPT_NOBODY ,1 ) || +curl_easy_setopt (state->curl,CURLOPT_HEADERFUNCTION , curl_header_cb) || +curl_easy_setopt (state->curl,CURLOPT_HEADERDATA , s)) { + goto out; + }if (curl_easy_perform (state->curl)) goto out;if (curl_easy_getinfo (state->curl,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &d)) {@@ -879,7 +902,10 @@ static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)snprintf (state->range,127 ,"%" PRIu64"-%" PRIu64, start, end);trace_curl_setup_preadv (acb->bytes, start, state->range); -curl_easy_setopt (state->curl,CURLOPT_RANGE , state->range); +if (curl_easy_setopt (state->curl,CURLOPT_RANGE , state->range)) { +curl_clean_state (state); + goto out; + }if (curl_multi_add_handle (s->multi, state->curl) !=CURLM_OK ) { state->acb[0 ] =NULL ;Signature · 1 line
2.25.1
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 13 lines · Daniel P. Berrangé
Quoted 10 lines · Peter Maydell
Coverity points out that we aren't checking the return value from
curl_easy_setopt()for any of the calls to it we make inblock/curl.c.Fixes Coverity CID 1459336, 1459482, 1460331Signed-off-by Peter Maydell <[email protected]>---
Big fat disclaimer: tested only with 'make check', which I suspect may not be exercising this block backend. Hints on how to test more thoroughly are welcome.
Quoted 3 lines
yeah afaik, qemu-iotests don't have support for this. As a very basic smoke test do
[command lines snipped]
Thanks; yes, those both work.
-- PMM
Quoted 6 lines · Peter Maydell
Coverity points out that we aren't checking the return value from
curl_easy_setopt()for any of the calls to it we make inblock/curl.c.Fixes Coverity CID 1459336, 1459482, 1460331Signed-off-by Peter Maydell <[email protected]>
Quoted 7 lines
---
Big fat disclaimer: tested only with 'make check', which I suspect may not be exercising this block backend. Hints on how to test more thoroughly are welcome.block/curl.c | 90 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 32 deletions(-)
One problem I see in general is that most of the setopt functions are (indirectly) called from curl_open(), which is supposed to return an error message. Its out label seems to expect some error description in state->errmsg. The error handling here doesn’t set such a description.
Then again, there are enough existing error paths that don’t set this description either, so it isn’t quite this patch’s duty to fix that situation. I guess it would be nice if we had a wrapper for curl_easy_setopt() with an Error ** parameter, so we could easily generate error messages that describe key and value (and then curl_init_state() should have an Error ** parameter, too).
But this patch doesn’t make anything worse than it already is, so that’d rather be an idea for future clean-up.
Quoted 4 lines
block/curl.c +0
-0
[...]
block/curl.c static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) · line 907
Quoted 8 lines
block/curl.c +3
-1@@ -879,7 +902,10 @@ static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)snprintf (state->range,127 ,"%" PRIu64"-%" PRIu64, start, end);trace_curl_setup_preadv (acb->bytes, start, state->range); -curl_easy_setopt (state->curl,CURLOPT_RANGE , state->range); +if (curl_easy_setopt (state->curl,CURLOPT_RANGE , state->range)) { +curl_clean_state (state); + goto out;
I think we need to mark the request as failed by setting acb->ret to a negative value (and probably also clear state->acb[0] like the error path below does).
Hanna
Quoted 4 lines
block/curl.c +1
-0+ }if (curl_multi_add_handle (s->multi, state->curl) !=CURLM_OK ) { state->acb[0 ] =NULL ;
Quoted 13 lines · Hanna Reitz
Quoted 11 lines · Peter Maydell
Coverity points out that we aren't checking the return value from
curl_easy_setopt()for any of the calls to it we make inblock/curl.c.Fixes Coverity CID 1459336, 1459482, 1460331Signed-off-by Peter Maydell <[email protected]>---
Big fat disclaimer: tested only with 'make check', which I suspect may not be exercising this block backend. Hints on how to test more thoroughly are welcome.
Quoted 7 lines
Quoted 2 lines
block/curl.c | 90 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 32 deletions(-)One problem I see in general is that most of the setopt functions are (indirectly) called from
curl_open(), which is supposed to return an error message. Itsoutlabel seems to expect some error description instate->errmsg. The error handling here doesn’t set such a description.
Ah, yes, I hadn't noticed that -- it's a pre-existing bug, where we do this:
if (curl_init_state(s, state) < 0) {
goto out;
}and curl_init_state() already has an error path (for when curl_easy_init() fails) that can take that goto without setting state->errmsg...
Quoted 3 lines
Then again, there are enough existing error paths that don’t set this description either, so it isn’t quite this patch’s duty to fix that situation.
...as you've already noticed :-)
Quoted 17 lines
I guess it would be nice if we had a wrapper for
curl_easy_setopt()with anError **parameter, so we could easily generate error messages that describe key and value (and thencurl_init_state()should have anError **parameter, too).But this patch doesn’t make anything worse than it already is, so that’d rather be an idea for future clean-up.
Quoted 4 lines
block/curl.c +0
-0[...]
Quoted 2 lines
block/curl.c +0
-0@@ -879,7 +902,10 @@ static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)
Quoted 10 lines
block/curl.c static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) · line 907
Quoted 6 lines
block/curl.c +3
-1snprintf (state->range,127 ,"%" PRIu64"-%" PRIu64, start, end);trace_curl_setup_preadv (acb->bytes, start, state->range); -curl_easy_setopt (state->curl,CURLOPT_RANGE , state->range); +if (curl_easy_setopt (state->curl,CURLOPT_RANGE , state->range)) { +curl_clean_state (state); + goto out;I think we need to mark the request as failed by setting
acb->retto a negative value (and probably also clearstate->acb[0]like the error path below does).
OK; or I could roll the two curl calls into the same if:
if (curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range) ||
curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) {
/* existing error handling and goto-out code */
}thanks
-- PMM
Quoted 59 lines · Peter Maydell
Quoted 12 lines · Hanna Reitz
Quoted 11 lines · Peter Maydell
Coverity points out that we aren't checking the return value from
curl_easy_setopt()for any of the calls to it we make inblock/curl.c.Fixes Coverity CID 1459336, 1459482, 1460331Signed-off-by Peter Maydell <[email protected]>---
Big fat disclaimer: tested only with 'make check', which I suspect may not be exercising this block backend. Hints on how to test more thoroughly are welcome.Quoted 6 lines
Quoted 2 lines
block/curl.c | 90 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 32 deletions(-)One problem I see in general is that most of the setopt functions are (indirectly) called from
curl_open(), which is supposed to return an error message. Itsoutlabel seems to expect some error description instate->errmsg. The error handling here doesn’t set such a description.Ah, yes, I hadn't noticed that -- it's a pre-existing bug, where we do this:
if (curl_init_state(s, state) < 0) { goto out; }and
curl_init_state()already has an error path (for whencurl_easy_init()fails) that can take that goto without settingstate->errmsg...Quoted 3 lines
Then again, there are enough existing error paths that don’t set this description either, so it isn’t quite this patch’s duty to fix that situation.
...as you've already noticed :-)
Quoted 16 lines
I guess it would be nice if we had a wrapper for
curl_easy_setopt()with anError **parameter, so we could easily generate error messages that describe key and value (and thencurl_init_state()should have anError **parameter, too).But this patch doesn’t make anything worse than it already is, so that’d rather be an idea for future clean-up.
Quoted 4 lines
block/curl.c +0
-0[...]
Quoted 2 lines
block/curl.c +0
-0@@ -879,7 +902,10 @@ static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)Quoted 9 lines
block/curl.c static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) · line 907
Quoted 6 lines
block/curl.c +3
-1snprintf (state->range,127 ,"%" PRIu64"-%" PRIu64, start, end);trace_curl_setup_preadv (acb->bytes, start, state->range); -curl_easy_setopt (state->curl,CURLOPT_RANGE , state->range); +if (curl_easy_setopt (state->curl,CURLOPT_RANGE , state->range)) { +curl_clean_state (state); + goto out;I think we need to mark the request as failed by setting
acb->retto a negative value (and probably also clearstate->acb[0]like the error path below does).
Quoted 6 lines
OK; or I could roll the two curl calls into the same if:
if (curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range) || curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) { /* existing error handling and goto-out code */ }
Sure, that sounds perfectly good to me.
Hanna