Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[libzypp] Fix file descriptor leak. Fixes JB#49494
  • Loading branch information
jusa committed Jun 15, 2020
1 parent a75da97 commit 0ed243b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
89 changes: 89 additions & 0 deletions rpm/0008-MediaCurl-Fix-leaking-fd.patch
@@ -0,0 +1,89 @@
From d9f3cd3bf9822997819dd54c7212290528944026 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juho=20H=C3=A4m=C3=A4l=C3=A4inen?=
<juho.hamalainen@jolla.com>
Date: Thu, 11 Jun 2020 10:30:27 +0300
Subject: [PATCH] MediaCurl: Fix leaking fd.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Juho Hämäläinen <juho.hamalainen@jolla.com>
---
zypp/media/MediaCurl.cc | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/zypp/media/MediaCurl.cc b/zypp/media/MediaCurl.cc
index 1d1682773..049a7003b 100644
--- a/zypp/media/MediaCurl.cc
+++ b/zypp/media/MediaCurl.cc
@@ -47,6 +47,8 @@
#undef CURLVERSION_AT_LEAST
#define CURLVERSION_AT_LEAST(M,N,O) LIBCURL_VERSION_NUM >= ((((M)<<8)+(N))<<8)+(O)

+#define fclose_and_clear(x) do { if (x) ::fclose(x); x = 0; } while (0)
+
using namespace std;
using namespace zypp::base;

@@ -1268,7 +1270,7 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const

ret = curl_easy_setopt( _curl, CURLOPT_WRITEDATA, file );
if ( ret != 0 ) {
- ::fclose(file);
+ fclose_and_clear(file);
std::string err( _curlError);
curl_easy_setopt( _curl, CURLOPT_RANGE, NULL );
curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
@@ -1292,6 +1294,7 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const
{
curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
if ( ret != 0 ) {
+ fclose_and_clear(file);
ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
}

@@ -1302,6 +1305,7 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const
*/
curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L);
if ( ret != 0 ) {
+ fclose_and_clear(file);
ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
}

@@ -1311,13 +1315,14 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const
// for FTP we set different options
curl_easy_setopt( _curl, CURLOPT_RANGE, NULL);
if ( ret != 0 ) {
+ fclose_and_clear(file);
ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
}
}

// if the code is not zero, close the file
if ( ok != 0 )
- ::fclose(file);
+ fclose_and_clear(file);

// as we are not having user interaction, the user can't cancel
// the file existence checking, a callback or timeout return code
@@ -1326,13 +1331,17 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const
evaluateCurlCode( filename, ok, true /* timeout */);
}
catch ( const MediaFileNotFoundException &e ) {
+ fclose_and_clear(file);
// if the file did not exist then we can return false
return false;
}
catch ( const MediaException &e ) {
+ fclose_and_clear(file);
// some error, we are not sure about file existence, rethrw
ZYPP_RETHROW(e);
}
+
+ fclose_and_clear(file);
// exists
return ( ok == CURLE_OK );
}
--
2.25.1

1 change: 1 addition & 0 deletions rpm/libzypp.spec
Expand Up @@ -12,6 +12,7 @@ Patch4: 0004-Set-unrestricted-auth-curl-option.patch
Patch5: 0005-disable-doc.patch
Patch6: 0006-libzypp-Enable-netrcoptional-on-libcurl-to-allow-for.patch
Patch7: 0007-Set-GPG-homedir-when-reading-signatures.patch
Patch8: 0008-MediaCurl-Fix-leaking-fd.patch
BuildRequires: cmake
BuildRequires: openssl-devel
# Need boost > 1.53 for string_ref utility
Expand Down

0 comments on commit 0ed243b

Please sign in to comment.