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