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