190130
From fbda9baaf7231e3237277348cc7e873f3113fd14 Mon Sep 17 00:00:00 2001
2072c5
From: l17zhou <cynthia.zhou@nokia-sbell.com.cn>
2072c5
Date: Mon, 4 Nov 2019 08:45:52 +0200
190130
Subject: [PATCH 388/449] rpc: Cleanup SSL specific data at the time of freeing
2072c5
 rpc object
2072c5
2072c5
Problem: At the time of cleanup rpc object ssl specific data
2072c5
         is not freeing so it has become a leak.
2072c5
2072c5
Solution: To avoid the leak cleanup ssl specific data at the
2072c5
          time of cleanup rpc object
2072c5
2072c5
> Credits: l17zhou <cynthia.zhou@nokia-sbell.com.cn>
2072c5
> Fixes: bz#1768407
2072c5
> Change-Id: I37f598673ae2d7a33c75f39eb8843ccc6dffaaf0
2072c5
> (Cherry pick from commit 54ed71dba174385ab0d8fa415e09262f6250430c)
2072c5
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/23650/)
2072c5
2072c5
Change-Id: I37f598673ae2d7a33c75f39eb8843ccc6dffaaf0
190130
BUG: 1786516
2072c5
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
190130
Reviewed-on: https://code.engineering.redhat.com/gerrit/202308
2072c5
Tested-by: RHGS Build Bot <nigelb@redhat.com>
2072c5
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
2072c5
---
2072c5
 rpc/rpc-transport/socket/src/socket.c | 22 ++++++++++++++++++++--
2072c5
 tests/features/ssl-authz.t            | 23 ++++++++++++++++++++---
2072c5
 2 files changed, 40 insertions(+), 5 deletions(-)
2072c5
2072c5
diff --git a/rpc/rpc-transport/socket/src/socket.c b/rpc/rpc-transport/socket/src/socket.c
2072c5
index 65845ea..226b2e2 100644
2072c5
--- a/rpc/rpc-transport/socket/src/socket.c
2072c5
+++ b/rpc/rpc-transport/socket/src/socket.c
2072c5
@@ -446,6 +446,7 @@ ssl_setup_connection_postfix(rpc_transport_t *this)
2072c5
     gf_log(this->name, GF_LOG_DEBUG,
2072c5
            "SSL verification succeeded (client: %s) (server: %s)",
2072c5
            this->peerinfo.identifier, this->myinfo.identifier);
2072c5
+    X509_free(peer);
2072c5
     return gf_strdup(peer_CN);
2072c5
 
2072c5
     /* Error paths. */
2072c5
@@ -1157,7 +1158,15 @@ __socket_reset(rpc_transport_t *this)
2072c5
     memset(&priv->incoming, 0, sizeof(priv->incoming));
2072c5
 
2072c5
     event_unregister_close(this->ctx->event_pool, priv->sock, priv->idx);
2072c5
-
2072c5
+    if (priv->use_ssl && priv->ssl_ssl) {
2072c5
+        SSL_clear(priv->ssl_ssl);
2072c5
+        SSL_free(priv->ssl_ssl);
2072c5
+        priv->ssl_ssl = NULL;
2072c5
+    }
2072c5
+    if (priv->use_ssl && priv->ssl_ctx) {
2072c5
+        SSL_CTX_free(priv->ssl_ctx);
2072c5
+        priv->ssl_ctx = NULL;
2072c5
+    }
2072c5
     priv->sock = -1;
2072c5
     priv->idx = -1;
2072c5
     priv->connected = -1;
2072c5
@@ -3217,7 +3226,6 @@ socket_server_event_handler(int fd, int idx, int gen, void *data, int poll_in,
2072c5
         new_priv->sock = new_sock;
2072c5
 
2072c5
         new_priv->ssl_enabled = priv->ssl_enabled;
2072c5
-        new_priv->ssl_ctx = priv->ssl_ctx;
2072c5
         new_priv->connected = 1;
2072c5
         new_priv->is_server = _gf_true;
2072c5
 
2072c5
@@ -4672,6 +4680,16 @@ fini(rpc_transport_t *this)
2072c5
         pthread_mutex_destroy(&priv->out_lock);
2072c5
         pthread_mutex_destroy(&priv->cond_lock);
2072c5
         pthread_cond_destroy(&priv->cond);
2072c5
+        if (priv->use_ssl && priv->ssl_ssl) {
2072c5
+            SSL_clear(priv->ssl_ssl);
2072c5
+            SSL_free(priv->ssl_ssl);
2072c5
+            priv->ssl_ssl = NULL;
2072c5
+        }
2072c5
+        if (priv->use_ssl && priv->ssl_ctx) {
2072c5
+            SSL_CTX_free(priv->ssl_ctx);
2072c5
+            priv->ssl_ctx = NULL;
2072c5
+        }
2072c5
+
2072c5
         if (priv->ssl_private_key) {
2072c5
             GF_FREE(priv->ssl_private_key);
2072c5
         }
2072c5
diff --git a/tests/features/ssl-authz.t b/tests/features/ssl-authz.t
2072c5
index cae010c..132b598 100755
2072c5
--- a/tests/features/ssl-authz.t
2072c5
+++ b/tests/features/ssl-authz.t
2072c5
@@ -25,6 +25,7 @@ TEST glusterd
2072c5
 TEST pidof glusterd
2072c5
 TEST $CLI volume info;
2072c5
 
2072c5
+TEST $CLI v set all cluster.brick-multiplex on
2072c5
 # Construct a cipher list that excludes CBC because of POODLE.
2072c5
 # http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-3566
2072c5
 #
2072c5
@@ -45,12 +46,12 @@ TEST openssl genrsa -out $SSL_KEY 2048
2072c5
 TEST openssl req -new -x509 -key $SSL_KEY -subj /CN=Anyone -out $SSL_CERT
2072c5
 ln $SSL_CERT $SSL_CA
2072c5
 
2072c5
-TEST $CLI volume create $V0 $H0:$B0/1
2072c5
+TEST $CLI volume create $V0 replica 3 $H0:$B0/{1,2,3} force
2072c5
 TEST $CLI volume set $V0 server.ssl on
2072c5
 TEST $CLI volume set $V0 client.ssl on
2072c5
 TEST $CLI volume set $V0 ssl.cipher-list $(valid_ciphers)
2072c5
 TEST $CLI volume start $V0
2072c5
-EXPECT_WITHIN $CHILD_UP_TIMEOUT "1" online_brick_count
2072c5
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "3" online_brick_count
2072c5
 
2072c5
 # This mount should SUCCEED because ssl-allow=* by default.  This effectively
2072c5
 # disables SSL authorization, though authentication and encryption might still
2072c5
@@ -59,11 +60,27 @@ TEST glusterfs --volfile-server=$H0 --volfile-id=$V0 $M0
2072c5
 TEST ping_file $M0/before
2072c5
 EXPECT_WITHIN $UMOUNT_TIMEOUT "Y" force_umount $M0
2072c5
 
2072c5
+glusterfsd_pid=`pgrep glusterfsd`
2072c5
+TEST [ $glusterfsd_pid != 0 ]
2072c5
+start=`pmap -x $glusterfsd_pid | grep total | awk -F " " '{print $4}'`
2072c5
+echo "Memory consumption for glusterfsd process"
2072c5
+for i in $(seq 1 100); do
2072c5
+        gluster v heal $V0 info >/dev/null
2072c5
+done
2072c5
+
2072c5
+end=`pmap -x $glusterfsd_pid | grep total | awk -F " " '{print $4}'`
2072c5
+diff=$((end-start))
2072c5
+
2072c5
+# If memory consumption is more than 5M some leak in SSL code path
2072c5
+
2072c5
+TEST [ $diff -lt 5000 ]
2072c5
+
2072c5
+
2072c5
 # Set ssl-allow to a wildcard that includes our identity.
2072c5
 TEST $CLI volume stop $V0
2072c5
 TEST $CLI volume set $V0 auth.ssl-allow Any*
2072c5
 TEST $CLI volume start $V0
2072c5
-EXPECT_WITHIN $CHILD_UP_TIMEOUT "1" online_brick_count
2072c5
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "3" online_brick_count
2072c5
 
2072c5
 # This mount should SUCCEED because we match the wildcard.
2072c5
 TEST glusterfs --volfile-server=$H0 --volfile-id=$V0 $M0
2072c5
-- 
2072c5
1.8.3.1
2072c5