Avoid minor memleak during multi-message STARTTLS'd conns
authorJeremy Harris <jgh146exb@wizmail.org>
Sat, 17 Feb 2018 16:44:47 +0000 (16:44 +0000)
committerJeremy Harris <jgh146exb@wizmail.org>
Sat, 17 Feb 2018 16:44:47 +0000 (16:44 +0000)
As STARTTLS/EHLO is repeated per message, we should free mem used for EHLO-dependent hostnames

src/src/host.c
src/src/smtp_in.c

index fbc46575d6288e61e0c4fb616ef6713c92a0ec27..d4267429bcd449d3ccf0fb4750280ba7dbd2e502 100644 (file)
@@ -520,7 +520,9 @@ There wouldn't be two different variables if I had got all this right in the
 first place.
 
 Because this data may survive over more than one incoming SMTP message, it has
 first place.
 
 Because this data may survive over more than one incoming SMTP message, it has
-to be in permanent store.
+to be in permanent store.  However, STARTTLS has to be forgotten and redone
+on a multi-message conn, so this will be called once per message then.  Hence
+we use malloc, so we can free.
 
 Arguments:  none
 Returns:    nothing
 
 Arguments:  none
 Returns:    nothing
@@ -530,13 +532,12 @@ void
 host_build_sender_fullhost(void)
 {
 BOOL show_helo = TRUE;
 host_build_sender_fullhost(void)
 {
 BOOL show_helo = TRUE;
-uschar *address;
+uschar * address, * fullhost, * rcvhost, * reset_point;
 int len;
 int len;
-int old_pool = store_pool;
 
 if (!sender_host_address) return;
 
 
 if (!sender_host_address) return;
 
-store_pool = POOL_PERM;
+reset_point = store_get(0);
 
 /* Set up address, with or without the port. After discussion, it seems that
 the only format that doesn't cause trouble is [aaaa]:pppp. However, we can't
 
 /* Set up address, with or without the port. After discussion, it seems that
 the only format that doesn't cause trouble is [aaaa]:pppp. However, we can't
@@ -593,7 +594,7 @@ if (!sender_host_name)
   int adlen;    /* Sun compiler doesn't like ++ in initializers */
 
   adlen = portptr ? (++portptr - address) : Ustrlen(address);
   int adlen;    /* Sun compiler doesn't like ++ in initializers */
 
   adlen = portptr ? (++portptr - address) : Ustrlen(address);
-  sender_fullhost = sender_helo_name
+  fullhost = sender_helo_name
     ? string_sprintf("(%s) %s", sender_helo_name, address)
     : address;
 
     ? string_sprintf("(%s) %s", sender_helo_name, address)
     : address;
 
@@ -619,12 +620,7 @@ if (!sender_host_name)
     g = string_catn(g, US")", 1);
     }
 
     g = string_catn(g, US")", 1);
     }
 
-  sender_rcvhost = string_from_gstring(g);
-
-  /* Release store, because string_cat allocated a minimum of 100 bytes that
-  are rarely completely used. */
-
-  store_reset(sender_rcvhost + g->ptr + 1);
+  rcvhost = string_from_gstring(g);
   }
 
 /* Host name is known and verified. Unless we've already found that the HELO
   }
 
 /* Host name is known and verified. Unless we've already found that the HELO
@@ -637,25 +633,30 @@ else
 
   if (show_helo)
     {
 
   if (show_helo)
     {
-    sender_fullhost = string_sprintf("%s (%s) %s", sender_host_name,
+    fullhost = string_sprintf("%s (%s) %s", sender_host_name,
       sender_helo_name, address);
       sender_helo_name, address);
-    sender_rcvhost = (sender_ident == NULL)?
-      string_sprintf("%s (%s helo=%s)", sender_host_name,
-        address, sender_helo_name) :
-      string_sprintf("%s\n\t(%s helo=%s ident=%s)", sender_host_name,
-        address, sender_helo_name, sender_ident);
+    rcvhost = sender_ident
+      ?  string_sprintf("%s\n\t(%s helo=%s ident=%s)", sender_host_name,
+        address, sender_helo_name, sender_ident)
+      : string_sprintf("%s (%s helo=%s)", sender_host_name,
+        address, sender_helo_name);
     }
   else
     {
     }
   else
     {
-    sender_fullhost = string_sprintf("%s %s", sender_host_name, address);
-    sender_rcvhost = (sender_ident == NULL)?
-      string_sprintf("%s (%s)", sender_host_name, address) :
-      string_sprintf("%s (%s ident=%s)", sender_host_name, address,
-        sender_ident);
+    fullhost = string_sprintf("%s %s", sender_host_name, address);
+    rcvhost = sender_ident
+      ?  string_sprintf("%s (%s ident=%s)", sender_host_name, address,
+        sender_ident)
+      : string_sprintf("%s (%s)", sender_host_name, address);
     }
   }
 
     }
   }
 
-store_pool = old_pool;
+if (sender_fullhost) store_free(sender_fullhost);
+sender_fullhost = string_copy_malloc(fullhost);
+if (sender_rcvhost) store_free(sender_rcvhost);
+sender_rcvhost = string_copy_malloc(rcvhost);
+
+store_reset(reset_point);
 
 DEBUG(D_host_lookup) debug_printf("sender_fullhost = %s\n", sender_fullhost);
 DEBUG(D_host_lookup) debug_printf("sender_rcvhost = %s\n", sender_rcvhost);
 
 DEBUG(D_host_lookup) debug_printf("sender_fullhost = %s\n", sender_fullhost);
 DEBUG(D_host_lookup) debug_printf("sender_rcvhost = %s\n", sender_rcvhost);
index dea776dcc55bfca8007cf7dee3e3bd4dcf430be9..f54838991a4f201b3ce1d2491c0d4e10db1cd530 100644 (file)
@@ -4095,7 +4095,11 @@ while (done <= 0)
                &user_msg, &log_msg)) != OK)
         {
         done = smtp_handle_acl_fail(ACL_WHERE_HELO, rc, user_msg, log_msg);
                &user_msg, &log_msg)) != OK)
         {
         done = smtp_handle_acl_fail(ACL_WHERE_HELO, rc, user_msg, log_msg);
-        sender_helo_name = NULL;
+       if (sender_helo_name)
+         {
+         store_free(sender_helo_name);
+         sender_helo_name = NULL;
+         }
         host_build_sender_fullhost();  /* Rebuild */
         break;
         }
         host_build_sender_fullhost();  /* Rebuild */
         break;
         }