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