From patchwork Fri Jan 28 16:55:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 537620 Delivered-To: patch@linaro.org Received: by 2002:adf:f849:0:0:0:0:0 with SMTP id d9csp785785wrq; Fri, 28 Jan 2022 09:23:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJysJuP+W4jsodcx18pKcf5nS+wYiRnmPK0lbFsLCGdfopUowrHngurkTN2wwhbCh82g8/Ak X-Received: by 2002:a25:20c2:: with SMTP id g185mr13575169ybg.92.1643390595700; Fri, 28 Jan 2022 09:23:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643390595; cv=none; d=google.com; s=arc-20160816; b=yLfg/JRx5d8JAHgqSCFcrb+5DVdbXBCKUu1EvosTKCXVhUMOUru6nONnSMRzCRx2GJ GARQt8M1SjoOBWfl6spnCMBsCyo8OBVRMCWDQD5csbtfgBYvUjUtHk1EtsWc8r5qMD+n htQrJY32O9VZxW3XQ44YpUsV1A9DsA3oVLkxsa1yEkhVbbEC63sPgqjX5SsqsKfjP6lF aaJPNzDbT/tber3e3EucLa1+Hsg2KjtL2S/eN94Xr3pLGQsMXkuiLYSZVKuZAcR1JAQ2 73Ud/6oA/L8gXz42EArE+LZIsFLXReHNTPhAa5VbRc+zVTim8Kh5Gjf80KX14TCxmBjq x+LA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature; bh=my6AC68IrsDTzgwV0y0YsmAQ4IVN0yCb3Xq0zo4Jr8A=; b=lCcLKwAd6g/oaiJTVg4ID2RccTgw8KoSisDGsaPanjfSVplVuVJCfFYS1xIFWQO+7N NnbB1IDx+sY+yQe7wHwImRrjyFbbYeQFRprxJG30o4mVff9F4UQ40crFELUxRZr3JKvb yeocBN5NBVLxKTzVwaXAs7/hCoWJYyv7HSKrSlBYQmtVH5etohTYwuwLpGdr1bXBto0a F4fTWcZ3jqAu+2GLptu4HUrFlq8+2G2Qez5ONrghywhS5etZiDq07Wzd7BsqbGBlihsU VXjzcUt8EwAQX3nx6LNldv/rkigf4Wi2/AR9sKeZvZ3T0CY5ElUgRclTZlBotUqxeAkI xjCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=GPHbsr1j; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id i143si13676487ybg.497.2022.01.28.09.23.15 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 28 Jan 2022 09:23:15 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=GPHbsr1j; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:32806 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDUxv-0006by-2Y for patch@linaro.org; Fri, 28 Jan 2022 12:23:15 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50370) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDUXk-0008B1-Uy for qemu-devel@nongnu.org; Fri, 28 Jan 2022 11:56:13 -0500 Received: from [2a00:1450:4864:20::42c] (port=45865 helo=mail-wr1-x42c.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nDUXh-00023B-In for qemu-devel@nongnu.org; Fri, 28 Jan 2022 11:56:12 -0500 Received: by mail-wr1-x42c.google.com with SMTP id m14so11791701wrg.12 for ; Fri, 28 Jan 2022 08:55:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=my6AC68IrsDTzgwV0y0YsmAQ4IVN0yCb3Xq0zo4Jr8A=; b=GPHbsr1jOs3xYEqJ8MY1HwnoPyho4Kb57rdGuMpnGA5L8YpEsxe8yl7lgVdZmXk6R6 pBpimGGQ5KFb6+HKsu3qdR1WXjjJeeIVuqN0MBWYQkPzub6twmWHvX/p08mi4vxVFMG2 BeHkW1O1gTKmRsxhQUqf+k5tXhf15mFhrI2Hnp5K5Rx+QhUsb3832OMn9sGPQ6gtlwUi 6SPoC+V82zv2csUzJZwQqK09nCy5C57MyXVHwkz5Mdj0ECe32Qk9KwEsyRsrfYsR0umy O8FT7bbbImWvbDh3zpXOY90prC4fCGFZyabNnzqlwi+lYZhVtHh2jEQu2cc3kiI5AkwU gD6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=my6AC68IrsDTzgwV0y0YsmAQ4IVN0yCb3Xq0zo4Jr8A=; b=K3WFAwD4k+zyATEnsfBcjcsWMjn/FiERKkjNubzy70rJPKX69oNTHT+m5p7GfTfa+c Ghe74HDcjHeW/GyvjPTnx+55FBl+qBwUoIIVHLTLlviZAmw4KLyTKZxieQ9P1coRmzjt 7Xvy3VkgqKd2DlQ6FvnolkMXATje2C++kbiOmU8PvAxiChedtGHXPr4MvKt5uK7Yg7kM v7qEVKbg+W728BdAVU232Fx9BMB3i2pnocZ0y+Kdyy6aSebGP7TbVdCqbZWQPgfy6Qbf lBCuGltMcae3rZ4LwmfF6fSKVxEbO2pIto+z7rsii+0f3KwoCMUJ1ZEbOJs+L9x7ZOC3 cddw== X-Gm-Message-State: AOAM5327jS86LXMbgPbE+x0GZFcyG1ScGVccncc8ysYgwzn/3tvx2r7l O6NZI01hN6yTcDtJbDYOap1enKkt16Dyig== X-Received: by 2002:adf:aade:: with SMTP id i30mr7744876wrc.629.1643388937705; Fri, 28 Jan 2022 08:55:37 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id n13sm2497220wms.8.2022.01.28.08.55.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jan 2022 08:55:37 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH] block/curl.c: Check error return from curl_easy_setopt() Date: Fri, 28 Jan 2022 16:55:35 +0000 Message-Id: <20220128165535.2550899-1-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::42c (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::42c; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x42c.google.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Hanna Reitz , qemu-block@nongnu.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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. Fixes: Coverity CID 1459336, 1459482, 1460331 Signed-off-by: Peter Maydell --- 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(-) diff --git a/block/curl.c b/block/curl.c index 6a6cd729758..aaee1b17bef 100644 --- a/block/curl.c +++ b/block/curl.c @@ -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;