190130
From 5e231ceb35bb763d6fafc7c3efe1c3c582929cc2 Mon Sep 17 00:00:00 2001
190130
From: Xavi Hernandez <xhernandez@redhat.com>
190130
Date: Tue, 14 Jan 2020 13:28:47 +0100
190130
Subject: [PATCH 417/449] events: fix IPv6 memory corruption
190130
190130
When an event was generated and the target host was resolved to an IPv6
190130
address, there was a memory overflow when that address was copied to a
190130
fixed IPv4 structure (IPv6 addresses are longer than IPv4 ones).
190130
190130
This fix correctly handles IPv4 and IPv6 addresses returned by
190130
getaddrinfo()
190130
190130
Backport of:
190130
> Upstream-patch-link: https://review.gluster.org/24014
190130
> Change-Id: I5864a0c6e6f1b405bd85988529570140cf23b250
190130
> Fixes: bz#1790870
190130
> Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
190130
190130
BUG: 1792873
190130
Change-Id: I5864a0c6e6f1b405bd85988529570140cf23b250
190130
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
190130
Reviewed-on: https://code.engineering.redhat.com/gerrit/202486
190130
Tested-by: RHGS Build Bot <nigelb@redhat.com>
190130
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
190130
---
190130
 libglusterfs/src/events.c | 56 +++++++++++++----------------------------------
190130
 1 file changed, 15 insertions(+), 41 deletions(-)
190130
190130
diff --git a/libglusterfs/src/events.c b/libglusterfs/src/events.c
190130
index 4e2f8f9..6d1e383 100644
190130
--- a/libglusterfs/src/events.c
190130
+++ b/libglusterfs/src/events.c
190130
@@ -34,7 +34,6 @@ _gf_event(eventtypes_t event, const char *fmt, ...)
190130
     int ret = 0;
190130
     int sock = -1;
190130
     char *eventstr = NULL;
190130
-    struct sockaddr_in server;
190130
     va_list arguments;
190130
     char *msg = NULL;
190130
     glusterfs_ctx_t *ctx = NULL;
190130
@@ -42,11 +41,10 @@ _gf_event(eventtypes_t event, const char *fmt, ...)
190130
     struct addrinfo hints;
190130
     struct addrinfo *result = NULL;
190130
     xlator_t *this = THIS;
190130
-    int sin_family = AF_INET;
190130
     char *volfile_server_transport = NULL;
190130
 
190130
     /* Global context */
190130
-    ctx = THIS->ctx;
190130
+    ctx = this->ctx;
190130
 
190130
     if (event < 0 || event >= EVENT_LAST) {
190130
         ret = EVENT_ERROR_INVALID_INPUTS;
190130
@@ -60,48 +58,31 @@ _gf_event(eventtypes_t event, const char *fmt, ...)
190130
         goto out;
190130
     }
190130
 
190130
-    memset(&hints, 0, sizeof(hints));
190130
-    hints.ai_family = AF_UNSPEC;
190130
-
190130
     if (ctx) {
190130
         volfile_server_transport = ctx->cmd_args.volfile_server_transport;
190130
     }
190130
-
190130
     if (!volfile_server_transport) {
190130
         volfile_server_transport = "tcp";
190130
     }
190130
-    /* Get Host name to send message */
190130
+
190130
+    /* host = NULL returns localhost */
190130
+    host = NULL;
190130
     if (ctx && ctx->cmd_args.volfile_server &&
190130
         (strcmp(volfile_server_transport, "unix"))) {
190130
         /* If it is client code then volfile_server is set
190130
            use that information to push the events. */
190130
-        if ((getaddrinfo(ctx->cmd_args.volfile_server, NULL, &hints,
190130
-                         &result)) != 0) {
190130
-            ret = EVENT_ERROR_RESOLVE;
190130
-            goto out;
190130
-        }
190130
-
190130
-        if (get_ip_from_addrinfo(result, &host) == NULL) {
190130
-            ret = EVENT_ERROR_RESOLVE;
190130
-            goto out;
190130
-        }
190130
-
190130
-        sin_family = result->ai_family;
190130
-    } else {
190130
-        /* Localhost, Use the defined IP for localhost */
190130
-        host = gf_strdup(EVENT_HOST);
190130
+        host = ctx->cmd_args.volfile_server;
190130
     }
190130
 
190130
-    /* Socket Configurations */
190130
-    server.sin_family = sin_family;
190130
-    server.sin_port = htons(EVENT_PORT);
190130
-    ret = inet_pton(server.sin_family, host, &server.sin_addr);
190130
-    if (ret <= 0) {
190130
-        gf_msg(this->name, GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG,
190130
-               "inet_pton failed with return code %d", ret);
190130
+    memset(&hints, 0, sizeof(hints));
190130
+    hints.ai_family = AF_UNSPEC;
190130
+    hints.ai_socktype = SOCK_DGRAM;
190130
+    hints.ai_flags = AI_ADDRCONFIG;
190130
+
190130
+    if ((getaddrinfo(host, TOSTRING(EVENT_PORT), &hints, &result)) != 0) {
190130
+        ret = EVENT_ERROR_RESOLVE;
190130
         goto out;
190130
     }
190130
-    memset(&server.sin_zero, '\0', sizeof(server.sin_zero));
190130
 
190130
     va_start(arguments, fmt);
190130
     ret = gf_vasprintf(&msg, fmt, arguments);
190130
@@ -113,15 +94,15 @@ _gf_event(eventtypes_t event, const char *fmt, ...)
190130
     }
190130
 
190130
     ret = gf_asprintf(&eventstr, "%u %d %s", (unsigned)time(NULL), event, msg);
190130
-
190130
+    GF_FREE(msg);
190130
     if (ret <= 0) {
190130
         ret = EVENT_ERROR_MSG_FORMAT;
190130
         goto out;
190130
     }
190130
 
190130
     /* Send Message */
190130
-    if (sendto(sock, eventstr, strlen(eventstr), 0, (struct sockaddr *)&server,
190130
-               sizeof(server)) <= 0) {
190130
+    if (sendto(sock, eventstr, strlen(eventstr), 0, result->ai_addr,
190130
+               result->ai_addrlen) <= 0) {
190130
         ret = EVENT_ERROR_SEND;
190130
         goto out;
190130
     }
190130
@@ -133,17 +114,10 @@ out:
190130
         sys_close(sock);
190130
     }
190130
 
190130
-    /* Allocated by gf_vasprintf */
190130
-    if (msg)
190130
-        GF_FREE(msg);
190130
-
190130
     /* Allocated by gf_asprintf */
190130
     if (eventstr)
190130
         GF_FREE(eventstr);
190130
 
190130
-    if (host)
190130
-        GF_FREE(host);
190130
-
190130
     if (result)
190130
         freeaddrinfo(result);
190130
 
190130
-- 
190130
1.8.3.1
190130