Description: fix denial of service via ESI responses
Origin: backport, http://www.squid-cache.org/Versions/v3/3.4/changesets/SQUID-2016_9.patch
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=823968

Index: squid3-3.3.8/src/client_side_request.cc
===================================================================
--- squid3-3.3.8.orig/src/client_side_request.cc	2016-06-07 07:26:45.000000000 -0400
+++ squid3-3.3.8/src/client_side_request.cc	2016-06-07 10:01:34.514949575 -0400
@@ -189,13 +189,15 @@
     start_time = current_time;
     setConn(aConn);
     al = new AccessLogEntry;
-    al->tcpClient = clientConnection = aConn->clientConnection;
+    if (aConn) {
+        al->tcpClient = clientConnection = aConn->clientConnection;
 #if USE_SSL
-    if (aConn->clientConnection != NULL && aConn->clientConnection->isOpen()) {
-        if (SSL *ssl = fd_table[aConn->clientConnection->fd].ssl)
-            al->cache.sslClientCert.reset(SSL_get_peer_certificate(ssl));
-    }
+        if (aConn->clientConnection != NULL && aConn->clientConnection->isOpen()) {
+            if (SSL *ssl = fd_table[aConn->clientConnection->fd].ssl)
+                al->cache.sslClientCert.reset(SSL_get_peer_certificate(ssl));
+        }
 #endif
+    }
     dlinkAdd(this, &active, &ClientActiveRequests);
 #if USE_ADAPTATION
     request_satisfaction_mode = false;
Index: squid3-3.3.8/src/esi/Context.h
===================================================================
--- squid3-3.3.8.orig/src/esi/Context.h	2013-07-13 09:25:14.000000000 -0400
+++ squid3-3.3.8/src/esi/Context.h	2016-06-07 10:01:52.343180736 -0400
@@ -36,6 +36,7 @@
 #include "esi/Element.h"
 #include "clientStream.h"
 #include "err_type.h"
+#include "HttpReply.h"
 #include "HttpStatusCode.h"
 
 class ESIVarState;
@@ -116,7 +117,7 @@
     err_type errorpage; /* if we error what page to use */
     http_status errorstatus; /* if we error, what code to return */
     char *errormessage; /* error to pass to error page */
-    HttpReply *rep; /* buffered until we pass data downstream */
+    HttpReply::Pointer rep; /* buffered until we pass data downstream */
     ESISegment::Pointer buffered; /* unprocessed data - for whatever reason */
     ESISegment::Pointer incoming;
     /* processed data we are waiting to send, or for
Index: squid3-3.3.8/src/esi/Esi.cc
===================================================================
--- squid3-3.3.8.orig/src/esi/Esi.cc	2016-06-07 10:00:45.734316900 -0400
+++ squid3-3.3.8/src/esi/Esi.cc	2016-06-07 10:01:34.514949575 -0400
@@ -614,7 +614,7 @@
 
 #endif
 
-    if (!(rep || (outbound.getRaw() &&
+    if (!(rep != NULL || (outbound.getRaw() &&
                   outbound->len && (outbound_offset <= outbound->len)))) {
         debugs(86, 5, "ESIContext::send: Nothing to send.");
         return 0;
@@ -659,18 +659,18 @@
     flags.clientwantsdata = 0;
     debugs(86, 5, "ESIContext::send: this=" << this << " Client no longer wants data ");
     /* Deal with re-entrancy */
-    HttpReply *temprep = rep;
+    HttpReply::Pointer temprep = rep;
     rep = NULL; /* freed downstream */
 
-    if (temprep && varState)
-        varState->buildVary (temprep);
+    if (temprep != NULL && varState)
+        varState->buildVary(temprep.raw());
 
     {
         StoreIOBuffer tempBuffer;
         tempBuffer.length = len;
         tempBuffer.offset = pos - len;
         tempBuffer.data = next->readBuffer.data;
-        clientStreamCallback (thisNode, http, temprep, tempBuffer);
+        clientStreamCallback (thisNode, http, temprep.raw(), tempBuffer);
     }
 
     if (len == 0)
@@ -1300,7 +1300,7 @@
         ++parserState.stackdepth;
     }
 
-    if (rep && !parserState.inited())
+    if (rep != NULL && !parserState.inited())
         parserState.init(this);
 
     /* we have data */
@@ -1439,7 +1439,7 @@
 {
     debugs(86, 5, HERE << "Freeing for this=" << this);
 
-    HTTPMSGUNLOCK(rep);
+    rep = NULL; // refcounted
 
     finishChildren ();
 
