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