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