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