a2a915
From b2929ff50a7676563177bc52a372ddcae48cb002 Mon Sep 17 00:00:00 2001
a2a915
From: Petr Mensik <pemensik@redhat.com>
a2a915
Date: Wed, 24 Apr 2019 20:09:07 +0200
a2a915
Subject: [PATCH] 5200.   [security]      tcp-clients settings could be
a2a915
 exceeded in some cases,                         which could lead to
a2a915
 exhaustion of file descriptors.                         (CVE-2018-5743) [GL
a2a915
 #615]
a2a915
a2a915
---
a2a915
 bin/named/client.c                     | 421 +++++++++++++++++++------
a2a915
 bin/named/include/named/client.h       |  13 +-
a2a915
 bin/named/include/named/interfacemgr.h |  13 +-
a2a915
 bin/named/interfacemgr.c               |   9 +-
a2a915
 lib/isc/include/isc/quota.h            |   7 +
a2a915
 lib/isc/quota.c                        |  33 +-
a2a915
 6 files changed, 385 insertions(+), 111 deletions(-)
a2a915
a2a915
diff --git a/bin/named/client.c b/bin/named/client.c
a2a915
index b7d8a98..e1acaf1 100644
a2a915
--- a/bin/named/client.c
a2a915
+++ b/bin/named/client.c
a2a915
@@ -243,7 +243,7 @@ static void ns_client_dumpmessage(ns_client_t *client, const char *reason);
a2a915
 static isc_result_t get_client(ns_clientmgr_t *manager, ns_interface_t *ifp,
a2a915
 			       dns_dispatch_t *disp, isc_boolean_t tcp);
a2a915
 static isc_result_t get_worker(ns_clientmgr_t *manager, ns_interface_t *ifp,
a2a915
-			       isc_socket_t *sock);
a2a915
+			       isc_socket_t *sock, ns_client_t *oldclient);
a2a915
 static inline isc_boolean_t
a2a915
 allowed(isc_netaddr_t *addr, dns_name_t *signer, isc_netaddr_t *ecs_addr,
a2a915
 	isc_uint8_t ecs_addrlen, isc_uint8_t *ecs_scope, dns_acl_t *acl);
a2a915
@@ -295,6 +295,119 @@ ns_client_settimeout(ns_client_t *client, unsigned int seconds) {
a2a915
 	}
a2a915
 }
a2a915
 
a2a915
+/*%
a2a915
+ * Allocate a reference-counted object that will maintain a single pointer to
a2a915
+ * the (also reference-counted) TCP client quota, shared between all the
a2a915
+ * clients processing queries on a single TCP connection, so that all
a2a915
+ * clients sharing the one socket will together consume only one slot in
a2a915
+ * the 'tcp-clients' quota.
a2a915
+ */
a2a915
+static isc_result_t
a2a915
+tcpconn_init(ns_client_t *client, isc_boolean_t force) {
a2a915
+	isc_result_t result;
a2a915
+	isc_quota_t *quota = NULL;
a2a915
+	ns_tcpconn_t *tconn = NULL;
a2a915
+
a2a915
+	REQUIRE(client->tcpconn == NULL);
a2a915
+
a2a915
+	/*
a2a915
+	 * Try to attach to the quota first, so we won't pointlessly
a2a915
+	 * allocate memory for a tcpconn object if we can't get one.
a2a915
+	 */
a2a915
+	if (force) {
a2a915
+		result = isc_quota_force(&ns_g_server->tcpquota, "a;;
a2a915
+	} else {
a2a915
+		result = isc_quota_attach(&ns_g_server->tcpquota, "a;;
a2a915
+	}
a2a915
+	if (result != ISC_R_SUCCESS) {
a2a915
+		return (result);
a2a915
+	}
a2a915
+
a2a915
+	/*
a2a915
+	 * A global memory context is used for the allocation as different
a2a915
+	 * client structures may have different memory contexts assigned and a
a2a915
+	 * reference counter allocated here might need to be freed by a
a2a915
+	 * different client.  The performance impact caused by memory context
a2a915
+	 * contention here is expected to be negligible, given that this code
a2a915
+	 * is only executed for TCP connections.
a2a915
+	 */
a2a915
+	tconn = isc_mem_allocate(ns_g_mctx, sizeof(*tconn));
a2a915
+
a2a915
+	isc_refcount_init(&tconn->refs, 1);
a2a915
+	tconn->tcpquota = quota;
a2a915
+	quota = NULL;
a2a915
+	tconn->pipelined = ISC_FALSE;
a2a915
+
a2a915
+	client->tcpconn = tconn;
a2a915
+
a2a915
+	return (ISC_R_SUCCESS);
a2a915
+}
a2a915
+
a2a915
+/*%
a2a915
+ * Increase the count of client structures sharing the TCP connection
a2a915
+ * that 'source' is associated with; add a pointer to the same tcpconn
a2a915
+ * to 'target', thus associating it with the same TCP connection.
a2a915
+ */
a2a915
+static void
a2a915
+tcpconn_attach(ns_client_t *source, ns_client_t *target) {
a2a915
+	int refs;
a2a915
+
a2a915
+	REQUIRE(source->tcpconn != NULL);
a2a915
+	REQUIRE(target->tcpconn == NULL);
a2a915
+	REQUIRE(source->tcpconn->pipelined);
a2a915
+
a2a915
+	isc_refcount_increment(&source->tcpconn->refs, &refs);
a2a915
+	INSIST(refs > 1);
a2a915
+	target->tcpconn = source->tcpconn;
a2a915
+}
a2a915
+
a2a915
+/*%
a2a915
+ * Decrease the count of client structures sharing the TCP connection that
a2a915
+ * 'client' is associated with.  If this is the last client using this TCP
a2a915
+ * connection, we detach from the TCP quota and free the tcpconn
a2a915
+ * object. Either way, client->tcpconn is set to NULL.
a2a915
+ */
a2a915
+static void
a2a915
+tcpconn_detach(ns_client_t *client) {
a2a915
+	ns_tcpconn_t *tconn = NULL;
a2a915
+	int refs;
a2a915
+
a2a915
+	REQUIRE(client->tcpconn != NULL);
a2a915
+
a2a915
+	tconn = client->tcpconn;
a2a915
+	client->tcpconn = NULL;
a2a915
+
a2a915
+	isc_refcount_decrement(&tconn->refs, &refs);
a2a915
+	if (refs == 0) {
a2a915
+		isc_quota_detach(&tconn->tcpquota);
a2a915
+		isc_mem_free(ns_g_mctx, tconn);
a2a915
+	}
a2a915
+}
a2a915
+
a2a915
+/*%
a2a915
+ * Mark a client as active and increment the interface's 'ntcpactive'
a2a915
+ * counter, as a signal that there is at least one client servicing
a2a915
+ * TCP queries for the interface. If we reach the TCP client quota at
a2a915
+ * some point, this will be used to determine whether a quota overrun
a2a915
+ * should be permitted.
a2a915
+ *
a2a915
+ * Marking the client active with the 'tcpactive' flag ensures proper
a2a915
+ * accounting, by preventing us from incrementing or decrementing
a2a915
+ * 'ntcpactive' more than once per client.
a2a915
+ */
a2a915
+static void
a2a915
+mark_tcp_active(ns_client_t *client, isc_boolean_t active) {
a2a915
+	if (active && !client->tcpactive) {
a2a915
+		isc_atomic_xadd(&client->interface->ntcpactive, 1);
a2a915
+		client->tcpactive = active;
a2a915
+	} else if (!active && client->tcpactive) {
a2a915
+		uint32_t old =
a2a915
+			isc_atomic_xadd(&client->interface->ntcpactive, -1);
a2a915
+		INSIST(old > 0);
a2a915
+		client->tcpactive = active;
a2a915
+	}
a2a915
+}
a2a915
+
a2a915
 /*%
a2a915
  * Check for a deactivation or shutdown request and take appropriate
a2a915
  * action.  Returns ISC_TRUE if either is in progress; in this case
a2a915
@@ -384,7 +497,8 @@ exit_check(ns_client_t *client) {
a2a915
 		INSIST(client->recursionquota == NULL);
a2a915
 
a2a915
 		if (NS_CLIENTSTATE_READING == client->newstate) {
a2a915
-			if (!client->pipelined) {
a2a915
+			INSIST(client->tcpconn != NULL);
a2a915
+			if (!client->tcpconn->pipelined) {
a2a915
 				client_read(client);
a2a915
 				client->newstate = NS_CLIENTSTATE_MAX;
a2a915
 				return (ISC_TRUE); /* We're done. */
a2a915
@@ -402,10 +516,13 @@ exit_check(ns_client_t *client) {
a2a915
 		 */
a2a915
 		INSIST(client->recursionquota == NULL);
a2a915
 		INSIST(client->newstate <= NS_CLIENTSTATE_READY);
a2a915
-		if (client->nreads > 0)
a2a915
+
a2a915
+		if (client->nreads > 0) {
a2a915
 			dns_tcpmsg_cancelread(&client->tcpmsg);
a2a915
-		if (client->nreads != 0) {
a2a915
-			/* Still waiting for read cancel completion. */
a2a915
+		}
a2a915
+
a2a915
+		/* Still waiting for read cancel completion. */
a2a915
+		if (client->nreads > 0) {
a2a915
 			return (ISC_TRUE);
a2a915
 		}
a2a915
 
a2a915
@@ -413,14 +530,49 @@ exit_check(ns_client_t *client) {
a2a915
 			dns_tcpmsg_invalidate(&client->tcpmsg);
a2a915
 			client->tcpmsg_valid = ISC_FALSE;
a2a915
 		}
a2a915
+
a2a915
+		/*
a2a915
+		 * Soon the client will be ready to accept a new TCP
a2a915
+		 * connection or UDP request, but we may have enough
a2a915
+		 * clients doing that already.  Check whether this client
a2a915
+		 * needs to remain active and allow it go inactive if
a2a915
+		 * not.
a2a915
+		 *
a2a915
+		 * UDP clients always go inactive at this point, but a TCP
a2a915
+		 * client may need to stay active and return to READY
a2a915
+		 * state if no other clients are available to listen
a2a915
+		 * for TCP requests on this interface.
a2a915
+		 *
a2a915
+		 * Regardless, if we're going to FREED state, that means
a2a915
+		 * the system is shutting down and we don't need to
a2a915
+		 * retain clients.
a2a915
+		 */
a2a915
+		if (client->mortal && TCP_CLIENT(client) &&
a2a915
+		    client->newstate != NS_CLIENTSTATE_FREED &&
a2a915
+		    !ns_g_clienttest &&
a2a915
+		    isc_atomic_xadd(&client->interface->ntcpaccepting, 0) == 0)
a2a915
+		{
a2a915
+			/* Nobody else is accepting */
a2a915
+			client->mortal = ISC_FALSE;
a2a915
+			client->newstate = NS_CLIENTSTATE_READY;
a2a915
+		}
a2a915
+
a2a915
+		/*
a2a915
+		 * Detach from TCP connection and TCP client quota,
a2a915
+		 * if appropriate. If this is the last reference to
a2a915
+		 * the TCP connection in our pipeline group, the
a2a915
+		 * TCP quota slot will be released.
a2a915
+		 */
a2a915
+		if (client->tcpconn) {
a2a915
+			tcpconn_detach(client);
a2a915
+		}
a2a915
+
a2a915
 		if (client->tcpsocket != NULL) {
a2a915
 			CTRACE("closetcp");
a2a915
 			isc_socket_detach(&client->tcpsocket);
a2a915
+			mark_tcp_active(client, ISC_FALSE);
a2a915
 		}
a2a915
 
a2a915
-		if (client->tcpquota != NULL)
a2a915
-			isc_quota_detach(&client->tcpquota);
a2a915
-
a2a915
 		if (client->timerset) {
a2a915
 			(void)isc_timer_reset(client->timer,
a2a915
 					      isc_timertype_inactive,
a2a915
@@ -428,45 +580,26 @@ exit_check(ns_client_t *client) {
a2a915
 			client->timerset = ISC_FALSE;
a2a915
 		}
a2a915
 
a2a915
-		client->pipelined = ISC_FALSE;
a2a915
-
a2a915
 		client->peeraddr_valid = ISC_FALSE;
a2a915
 
a2a915
 		client->state = NS_CLIENTSTATE_READY;
a2a915
-		INSIST(client->recursionquota == NULL);
a2a915
-
a2a915
-		/*
a2a915
-		 * Now the client is ready to accept a new TCP connection
a2a915
-		 * or UDP request, but we may have enough clients doing
a2a915
-		 * that already.  Check whether this client needs to remain
a2a915
-		 * active and force it to go inactive if not.
a2a915
-		 *
a2a915
-		 * UDP clients go inactive at this point, but TCP clients
a2a915
-		 * may remain active if we have fewer active TCP client
a2a915
-		 * objects than desired due to an earlier quota exhaustion.
a2a915
-		 */
a2a915
-		if (client->mortal && TCP_CLIENT(client) && !ns_g_clienttest) {
a2a915
-			LOCK(&client->interface->lock);
a2a915
-			if (client->interface->ntcpcurrent <
a2a915
-				    client->interface->ntcptarget)
a2a915
-				client->mortal = ISC_FALSE;
a2a915
-			UNLOCK(&client->interface->lock);
a2a915
-		}
a2a915
 
a2a915
 		/*
a2a915
 		 * We don't need the client; send it to the inactive
a2a915
 		 * queue for recycling.
a2a915
 		 */
a2a915
 		if (client->mortal) {
a2a915
-			if (client->newstate > NS_CLIENTSTATE_INACTIVE)
a2a915
+			if (client->newstate > NS_CLIENTSTATE_INACTIVE) {
a2a915
 				client->newstate = NS_CLIENTSTATE_INACTIVE;
a2a915
+			}
a2a915
 		}
a2a915
 
a2a915
 		if (NS_CLIENTSTATE_READY == client->newstate) {
a2a915
 			if (TCP_CLIENT(client)) {
a2a915
 				client_accept(client);
a2a915
-			} else
a2a915
+			} else {
a2a915
 				client_udprecv(client);
a2a915
+			}
a2a915
 			client->newstate = NS_CLIENTSTATE_MAX;
a2a915
 			return (ISC_TRUE);
a2a915
 		}
a2a915
@@ -478,41 +611,51 @@ exit_check(ns_client_t *client) {
a2a915
 		/*
a2a915
 		 * We are trying to enter the inactive state.
a2a915
 		 */
a2a915
-		if (client->naccepts > 0)
a2a915
+		if (client->naccepts > 0) {
a2a915
 			isc_socket_cancel(client->tcplistener, client->task,
a2a915
 					  ISC_SOCKCANCEL_ACCEPT);
a2a915
+		}
a2a915
 
a2a915
 		/* Still waiting for accept cancel completion. */
a2a915
-		if (! (client->naccepts == 0))
a2a915
+		if (client->naccepts > 0) {
a2a915
 			return (ISC_TRUE);
a2a915
+		}
a2a915
 
a2a915
 		/* Accept cancel is complete. */
a2a915
-		if (client->nrecvs > 0)
a2a915
+		if (client->nrecvs > 0) {
a2a915
 			isc_socket_cancel(client->udpsocket, client->task,
a2a915
 					  ISC_SOCKCANCEL_RECV);
a2a915
+		}
a2a915
 
a2a915
 		/* Still waiting for recv cancel completion. */
a2a915
-		if (! (client->nrecvs == 0))
a2a915
+		if (client->nrecvs > 0) {
a2a915
 			return (ISC_TRUE);
a2a915
+		}
a2a915
 
a2a915
 		/* Still waiting for control event to be delivered */
a2a915
-		if (client->nctls > 0)
a2a915
+		if (client->nctls > 0) {
a2a915
 			return (ISC_TRUE);
a2a915
-
a2a915
-		/* Deactivate the client. */
a2a915
-		if (client->interface)
a2a915
-			ns_interface_detach(&client->interface);
a2a915
+		}
a2a915
 
a2a915
 		INSIST(client->naccepts == 0);
a2a915
 		INSIST(client->recursionquota == NULL);
a2a915
-		if (client->tcplistener != NULL)
a2a915
+		if (client->tcplistener != NULL) {
a2a915
 			isc_socket_detach(&client->tcplistener);
a2a915
+			mark_tcp_active(client, ISC_FALSE);
a2a915
+		}
a2a915
 
a2a915
-		if (client->udpsocket != NULL)
a2a915
+		if (client->udpsocket != NULL) {
a2a915
 			isc_socket_detach(&client->udpsocket);
a2a915
+		}
a2a915
 
a2a915
-		if (client->dispatch != NULL)
a2a915
+		/* Deactivate the client. */
a2a915
+		if (client->interface != NULL) {
a2a915
+			ns_interface_detach(&client->interface);
a2a915
+		}
a2a915
+
a2a915
+		if (client->dispatch != NULL) {
a2a915
 			dns_dispatch_detach(&client->dispatch);
a2a915
+		}
a2a915
 
a2a915
 		client->attributes = 0;
a2a915
 		client->mortal = ISC_FALSE;
a2a915
@@ -537,10 +680,13 @@ exit_check(ns_client_t *client) {
a2a915
 			client->newstate = NS_CLIENTSTATE_MAX;
a2a915
 			if (!ns_g_clienttest && manager != NULL &&
a2a915
 			    !manager->exiting)
a2a915
+			{
a2a915
 				ISC_QUEUE_PUSH(manager->inactive, client,
a2a915
 					       ilink);
a2a915
-			if (client->needshutdown)
a2a915
+			}
a2a915
+			if (client->needshutdown) {
a2a915
 				isc_task_shutdown(client->task);
a2a915
+			}
a2a915
 			return (ISC_TRUE);
a2a915
 		}
a2a915
 	}
a2a915
@@ -650,7 +796,7 @@ client_start(isc_task_t *task, isc_event_t *event) {
a2a915
 		return;
a2a915
 
a2a915
 	if (TCP_CLIENT(client)) {
a2a915
-		if (client->pipelined) {
a2a915
+		if (client->tcpconn != NULL) {
a2a915
 			client_read(client);
a2a915
 		} else {
a2a915
 			client_accept(client);
a2a915
@@ -660,7 +806,6 @@ client_start(isc_task_t *task, isc_event_t *event) {
a2a915
 	}
a2a915
 }
a2a915
 
a2a915
-
a2a915
 /*%
a2a915
  * The client's task has received a shutdown event.
a2a915
  */
a2a915
@@ -2301,6 +2446,7 @@ client_request(isc_task_t *task, isc_event_t *event) {
a2a915
 		client->nrecvs--;
a2a915
 	} else {
a2a915
 		INSIST(TCP_CLIENT(client));
a2a915
+		INSIST(client->tcpconn != NULL);
a2a915
 		REQUIRE(event->ev_type == DNS_EVENT_TCPMSG);
a2a915
 		REQUIRE(event->ev_sender == &client->tcpmsg);
a2a915
 		buffer = &client->tcpmsg.buffer;
a2a915
@@ -2484,18 +2630,27 @@ client_request(isc_task_t *task, isc_event_t *event) {
a2a915
 	/*
a2a915
 	 * Pipeline TCP query processing.
a2a915
 	 */
a2a915
-	if (client->message->opcode != dns_opcode_query)
a2a915
-		client->pipelined = ISC_FALSE;
a2a915
-	if (TCP_CLIENT(client) && client->pipelined) {
a2a915
-		result = isc_quota_reserve(&ns_g_server->tcpquota);
a2a915
-		if (result == ISC_R_SUCCESS)
a2a915
-			result = ns_client_replace(client);
a2a915
+	if (TCP_CLIENT(client) &&
a2a915
+	    client->message->opcode != dns_opcode_query)
a2a915
+	{
a2a915
+		client->tcpconn->pipelined = ISC_FALSE;
a2a915
+	}
a2a915
+	if (TCP_CLIENT(client) && client->tcpconn->pipelined) {
a2a915
+		/*
a2a915
+		 * We're pipelining. Replace the client; the
a2a915
+		 * replacement can read the TCP socket looking
a2a915
+		 * for new messages and this one can process the
a2a915
+		 * current message asynchronously.
a2a915
+		 *
a2a915
+		 * There will now be at least three clients using this
a2a915
+		 * TCP socket - one accepting new connections,
a2a915
+		 * one reading an existing connection to get new
a2a915
+		 * messages, and one answering the message already
a2a915
+		 * received.
a2a915
+		 */
a2a915
+		result = ns_client_replace(client);
a2a915
 		if (result != ISC_R_SUCCESS) {
a2a915
-			ns_client_log(client, NS_LOGCATEGORY_CLIENT,
a2a915
-				      NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
a2a915
-				      "no more TCP clients(read): %s",
a2a915
-				      isc_result_totext(result));
a2a915
-			client->pipelined = ISC_FALSE;
a2a915
+			client->tcpconn->pipelined = ISC_FALSE;
a2a915
 		}
a2a915
 	}
a2a915
 
a2a915
@@ -3051,8 +3206,7 @@ client_create(ns_clientmgr_t *manager, ns_client_t **clientp) {
a2a915
 	client->signer = NULL;
a2a915
 	dns_name_init(&client->signername, NULL);
a2a915
 	client->mortal = ISC_FALSE;
a2a915
-	client->pipelined = ISC_FALSE;
a2a915
-	client->tcpquota = NULL;
a2a915
+	client->tcpconn = NULL;
a2a915
 	client->recursionquota = NULL;
a2a915
 	client->interface = NULL;
a2a915
 	client->peeraddr_valid = ISC_FALSE;
a2a915
@@ -3062,6 +3216,7 @@ client_create(ns_clientmgr_t *manager, ns_client_t **clientp) {
a2a915
 	client->filter_aaaa = dns_aaaa_ok;
a2a915
 #endif
a2a915
 	client->needshutdown = ns_g_clienttest;
a2a915
+	client->tcpactive = ISC_FALSE;
a2a915
 
a2a915
 	ISC_EVENT_INIT(&client->ctlevent, sizeof(client->ctlevent), 0, NULL,
a2a915
 		       NS_EVENT_CLIENTCONTROL, client_start, client, client,
a2a915
@@ -3156,9 +3311,10 @@ client_read(ns_client_t *client) {
a2a915
 
a2a915
 static void
a2a915
 client_newconn(isc_task_t *task, isc_event_t *event) {
a2a915
+	isc_result_t result;
a2a915
 	ns_client_t *client = event->ev_arg;
a2a915
 	isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
a2a915
-	isc_result_t result;
a2a915
+	uint32_t old;
a2a915
 
a2a915
 	REQUIRE(event->ev_type == ISC_SOCKEVENT_NEWCONN);
a2a915
 	REQUIRE(NS_CLIENT_VALID(client));
a2a915
@@ -3168,13 +3324,18 @@ client_newconn(isc_task_t *task, isc_event_t *event) {
a2a915
 
a2a915
 	INSIST(client->state == NS_CLIENTSTATE_READY);
a2a915
 
a2a915
+	/*
a2a915
+	 * The accept() was successful and we're now establishing a new
a2a915
+	 * connection. We need to make note of it in the client and
a2a915
+	 * interface objects so client objects can do the right thing
a2a915
+	 * when going inactive in exit_check() (see comments in
a2a915
+	 * client_accept() for details).
a2a915
+	 */
a2a915
 	INSIST(client->naccepts == 1);
a2a915
 	client->naccepts--;
a2a915
 
a2a915
-	LOCK(&client->interface->lock);
a2a915
-	INSIST(client->interface->ntcpcurrent > 0);
a2a915
-	client->interface->ntcpcurrent--;
a2a915
-	UNLOCK(&client->interface->lock);
a2a915
+	old = isc_atomic_xadd(&client->interface->ntcpaccepting, -1);
a2a915
+	INSIST(old > 0);
a2a915
 
a2a915
 	/*
a2a915
 	 * We must take ownership of the new socket before the exit
a2a915
@@ -3207,6 +3368,7 @@ client_newconn(isc_task_t *task, isc_event_t *event) {
a2a915
 			      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
a2a915
 			      "accept failed: %s",
a2a915
 			      isc_result_totext(nevent->result));
a2a915
+		tcpconn_detach(client);
a2a915
 	}
a2a915
 
a2a915
 	if (exit_check(client))
a2a915
@@ -3244,20 +3406,13 @@ client_newconn(isc_task_t *task, isc_event_t *event) {
a2a915
 		 * telnetting to port 53 (once per CPU) will
a2a915
 		 * deny service to legitimate TCP clients.
a2a915
 		 */
a2a915
-		client->pipelined = ISC_FALSE;
a2a915
-		result = isc_quota_attach(&ns_g_server->tcpquota,
a2a915
-					  &client->tcpquota);
a2a915
-		if (result == ISC_R_SUCCESS)
a2a915
-			result = ns_client_replace(client);
a2a915
-		if (result != ISC_R_SUCCESS) {
a2a915
-			ns_client_log(client, NS_LOGCATEGORY_CLIENT,
a2a915
-				      NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
a2a915
-				      "no more TCP clients(accept): %s",
a2a915
-				      isc_result_totext(result));
a2a915
-		} else if (ns_g_server->keepresporder == NULL ||
a2a915
-			   !allowed(&netaddr, NULL, NULL, 0, NULL,
a2a915
-				    ns_g_server->keepresporder)) {
a2a915
-			client->pipelined = ISC_TRUE;
a2a915
+		result = ns_client_replace(client);
a2a915
+		if (result == ISC_R_SUCCESS &&
a2a915
+		    (ns_g_server->keepresporder == NULL ||
a2a915
+		     !allowed(&netaddr, NULL, NULL, 0, NULL,
a2a915
+			      ns_g_server->keepresporder)))
a2a915
+		{
a2a915
+			client->tcpconn->pipelined = ISC_TRUE;
a2a915
 		}
a2a915
 
a2a915
 		client_read(client);
a2a915
@@ -3273,12 +3428,66 @@ client_accept(ns_client_t *client) {
a2a915
 
a2a915
 	CTRACE("accept");
a2a915
 
a2a915
+	/*
a2a915
+	 * Set up a new TCP connection. This means try to attach to the
a2a915
+	 * TCP client quota (tcp-clients), but fail if we're over quota.
a2a915
+	 */
a2a915
+	result = tcpconn_init(client, ISC_FALSE);
a2a915
+	if (result != ISC_R_SUCCESS) {
a2a915
+		isc_boolean_t exit;
a2a915
+
a2a915
+		ns_client_log(client, NS_LOGCATEGORY_CLIENT,
a2a915
+			      NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
a2a915
+			      "TCP client quota reached: %s",
a2a915
+			      isc_result_totext(result));
a2a915
+
a2a915
+		/*
a2a915
+		 * We have exceeded the system-wide TCP client quota.  But,
a2a915
+		 * we can't just block this accept in all cases, because if
a2a915
+		 * we did, a heavy TCP load on other interfaces might cause
a2a915
+		 * this interface to be starved, with no clients able to
a2a915
+		 * accept new connections.
a2a915
+		 *
a2a915
+		 * So, we check here to see if any other clients are
a2a915
+		 * already servicing TCP queries on this interface (whether
a2a915
+		 * accepting, reading, or processing). If we find that at
a2a915
+		 * least one client other than this one is active, then
a2a915
+		 * it's okay *not* to call accept - we can let this
a2a915
+		 * client go inactive and another will take over when it's
a2a915
+		 * done.
a2a915
+		 *
a2a915
+		 * If there aren't enough active clients on the interface,
a2a915
+		 * then we can be a little bit flexible about the quota.
a2a915
+		 * We'll allow *one* extra client through to ensure we're
a2a915
+		 * listening on every interface; we do this by setting the
a2a915
+		 * 'force' option to tcpconn_init().
a2a915
+		 *
a2a915
+		 * (Note: In practice this means that the real TCP client
a2a915
+		 * quota is tcp-clients plus the number of listening
a2a915
+		 * interfaces plus 1.)
a2a915
+		 */
a2a915
+		exit = (isc_atomic_xadd(&client->interface->ntcpactive, 0) >
a2a915
+			(client->tcpactive ? 1 : 0));
a2a915
+		if (exit) {
a2a915
+			client->newstate = NS_CLIENTSTATE_INACTIVE;
a2a915
+			(void)exit_check(client);
a2a915
+			return;
a2a915
+		}
a2a915
+
a2a915
+		result = tcpconn_init(client, ISC_TRUE);
a2a915
+		RUNTIME_CHECK(result == ISC_R_SUCCESS);
a2a915
+	}
a2a915
+
a2a915
+	/*
a2a915
+	 * If this client was set up using get_client() or get_worker(),
a2a915
+	 * then TCP is already marked active. However, if it was restarted
a2a915
+	 * from exit_check(), it might not be, so we take care of it now.
a2a915
+	 */
a2a915
+	mark_tcp_active(client, ISC_TRUE);
a2a915
+
a2a915
 	result = isc_socket_accept(client->tcplistener, client->task,
a2a915
 				   client_newconn, client);
a2a915
 	if (result != ISC_R_SUCCESS) {
a2a915
-		UNEXPECTED_ERROR(__FILE__, __LINE__,
a2a915
-				 "isc_socket_accept() failed: %s",
a2a915
-				 isc_result_totext(result));
a2a915
 		/*
a2a915
 		 * XXXRTH  What should we do?  We're trying to accept but
a2a915
 		 *	   it didn't work.  If we just give up, then TCP
a2a915
@@ -3286,13 +3495,37 @@ client_accept(ns_client_t *client) {
a2a915
 		 *
a2a915
 		 *	   For now, we just go idle.
a2a915
 		 */
a2a915
+		UNEXPECTED_ERROR(__FILE__, __LINE__,
a2a915
+				 "isc_socket_accept() failed: %s",
a2a915
+				 isc_result_totext(result));
a2a915
+
a2a915
+		tcpconn_detach(client);
a2a915
+		mark_tcp_active(client, ISC_FALSE);
a2a915
 		return;
a2a915
 	}
a2a915
+
a2a915
+	/*
a2a915
+	 * The client's 'naccepts' counter indicates that this client has
a2a915
+	 * called accept() and is waiting for a new connection. It should
a2a915
+	 * never exceed 1.
a2a915
+	 */
a2a915
 	INSIST(client->naccepts == 0);
a2a915
 	client->naccepts++;
a2a915
-	LOCK(&client->interface->lock);
a2a915
-	client->interface->ntcpcurrent++;
a2a915
-	UNLOCK(&client->interface->lock);
a2a915
+
a2a915
+	/*
a2a915
+	 * The interface's 'ntcpaccepting' counter is incremented when
a2a915
+	 * any client calls accept(), and decremented in client_newconn()
a2a915
+	 * once the connection is established.
a2a915
+	 *
a2a915
+	 * When the client object is shutting down after handling a TCP
a2a915
+	 * request (see exit_check()), if this value is at least one, that
a2a915
+	 * means another client has called accept() and is waiting to
a2a915
+	 * establish the next connection. That means the client may be
a2a915
+	 * be free to become inactive; otherwise it may need to start
a2a915
+	 * listening for connections itself to prevent the interface
a2a915
+	 * going dead.
a2a915
+	 */
a2a915
+	isc_atomic_xadd(&client->interface->ntcpaccepting, 1);
a2a915
 }
a2a915
 
a2a915
 static void
a2a915
@@ -3363,15 +3596,17 @@ ns_client_replace(ns_client_t *client) {
a2a915
 	REQUIRE(client->manager != NULL);
a2a915
 
a2a915
 	tcp = TCP_CLIENT(client);
a2a915
-	if (tcp && client->pipelined) {
a2a915
+	if (tcp && client->tcpconn != NULL && client->tcpconn->pipelined) {
a2a915
 		result = get_worker(client->manager, client->interface,
a2a915
-				    client->tcpsocket);
a2a915
+				    client->tcpsocket, client);
a2a915
 	} else {
a2a915
 		result = get_client(client->manager, client->interface,
a2a915
 				    client->dispatch, tcp);
a2a915
+
a2a915
 	}
a2a915
-	if (result != ISC_R_SUCCESS)
a2a915
+	if (result != ISC_R_SUCCESS) {
a2a915
 		return (result);
a2a915
+	}
a2a915
 
a2a915
 	/*
a2a915
 	 * The responsibility for listening for new requests is hereby
a2a915
@@ -3557,9 +3792,12 @@ get_client(ns_clientmgr_t *manager, ns_interface_t *ifp,
a2a915
 	client->dscp = ifp->dscp;
a2a915
 
a2a915
 	if (tcp) {
a2a915
+		mark_tcp_active(client, ISC_TRUE);
a2a915
+
a2a915
 		client->attributes |= NS_CLIENTATTR_TCP;
a2a915
 		isc_socket_attach(ifp->tcpsocket,
a2a915
 				  &client->tcplistener);
a2a915
+
a2a915
 	} else {
a2a915
 		isc_socket_t *sock;
a2a915
 
a2a915
@@ -3577,7 +3815,8 @@ get_client(ns_clientmgr_t *manager, ns_interface_t *ifp,
a2a915
 }
a2a915
 
a2a915
 static isc_result_t
a2a915
-get_worker(ns_clientmgr_t *manager, ns_interface_t *ifp, isc_socket_t *sock)
a2a915
+get_worker(ns_clientmgr_t *manager, ns_interface_t *ifp, isc_socket_t *sock,
a2a915
+	   ns_client_t *oldclient)
a2a915
 {
a2a915
 	isc_result_t result = ISC_R_SUCCESS;
a2a915
 	isc_event_t *ev;
a2a915
@@ -3585,6 +3824,7 @@ get_worker(ns_clientmgr_t *manager, ns_interface_t *ifp, isc_socket_t *sock)
a2a915
 	MTRACE("get worker");
a2a915
 
a2a915
 	REQUIRE(manager != NULL);
a2a915
+	REQUIRE(oldclient != NULL);
a2a915
 
a2a915
 	if (manager->exiting)
a2a915
 		return (ISC_R_SHUTTINGDOWN);
a2a915
@@ -3617,14 +3857,15 @@ get_worker(ns_clientmgr_t *manager, ns_interface_t *ifp, isc_socket_t *sock)
a2a915
 	ns_interface_attach(ifp, &client->interface);
a2a915
 	client->newstate = client->state = NS_CLIENTSTATE_WORKING;
a2a915
 	INSIST(client->recursionquota == NULL);
a2a915
-	client->tcpquota = &ns_g_server->tcpquota;
a2a915
 
a2a915
 	client->dscp = ifp->dscp;
a2a915
 
a2a915
 	client->attributes |= NS_CLIENTATTR_TCP;
a2a915
-	client->pipelined = ISC_TRUE;
a2a915
 	client->mortal = ISC_TRUE;
a2a915
 
a2a915
+	tcpconn_attach(oldclient, client);
a2a915
+	mark_tcp_active(client, ISC_TRUE);
a2a915
+
a2a915
 	isc_socket_attach(ifp->tcpsocket, &client->tcplistener);
a2a915
 	isc_socket_attach(sock, &client->tcpsocket);
a2a915
 	isc_socket_setname(client->tcpsocket, "worker-tcp", NULL);
a2a915
diff --git a/bin/named/include/named/client.h b/bin/named/include/named/client.h
a2a915
index 262b906..0f54d22 100644
a2a915
--- a/bin/named/include/named/client.h
a2a915
+++ b/bin/named/include/named/client.h
a2a915
@@ -9,8 +9,6 @@
a2a915
  * information regarding copyright ownership.
a2a915
  */
a2a915
 
a2a915
-/* $Id: client.h,v 1.96 2012/01/31 23:47:31 tbox Exp $ */
a2a915
-
a2a915
 #ifndef NAMED_CLIENT_H
a2a915
 #define NAMED_CLIENT_H 1
a2a915
 
a2a915
@@ -77,6 +75,13 @@
a2a915
  *** Types
a2a915
  ***/
a2a915
 
a2a915
+/*% reference-counted TCP connection object */
a2a915
+typedef struct ns_tcpconn {
a2a915
+	isc_refcount_t		refs;
a2a915
+	isc_quota_t		*tcpquota;
a2a915
+	isc_boolean_t		pipelined;
a2a915
+} ns_tcpconn_t;
a2a915
+
a2a915
 /*% nameserver client structure */
a2a915
 struct ns_client {
a2a915
 	unsigned int		magic;
a2a915
@@ -91,6 +96,7 @@ struct ns_client {
a2a915
 	int			nupdates;
a2a915
 	int			nctls;
a2a915
 	int			references;
a2a915
+	isc_boolean_t		tcpactive;
a2a915
 	isc_boolean_t		needshutdown; 	/*
a2a915
 						 * Used by clienttest to get
a2a915
 						 * the client to go from
a2a915
@@ -129,8 +135,7 @@ struct ns_client {
a2a915
 	dns_name_t		signername;   /*%< [T]SIG key name */
a2a915
 	dns_name_t *		signer;	      /*%< NULL if not valid sig */
a2a915
 	isc_boolean_t		mortal;	      /*%< Die after handling request */
a2a915
-	isc_boolean_t		pipelined;   /*%< TCP queries not in sequence */
a2a915
-	isc_quota_t		*tcpquota;
a2a915
+	ns_tcpconn_t		*tcpconn;
a2a915
 	isc_quota_t		*recursionquota;
a2a915
 	ns_interface_t		*interface;
a2a915
 
a2a915
diff --git a/bin/named/include/named/interfacemgr.h b/bin/named/include/named/interfacemgr.h
a2a915
index 36870f3..d9ac90f 100644
a2a915
--- a/bin/named/include/named/interfacemgr.h
a2a915
+++ b/bin/named/include/named/interfacemgr.h
a2a915
@@ -9,8 +9,6 @@
a2a915
  * information regarding copyright ownership.
a2a915
  */
a2a915
 
a2a915
-/* $Id: interfacemgr.h,v 1.35 2011/07/28 23:47:58 tbox Exp $ */
a2a915
-
a2a915
 #ifndef NAMED_INTERFACEMGR_H
a2a915
 #define NAMED_INTERFACEMGR_H 1
a2a915
 
a2a915
@@ -75,9 +73,14 @@ struct ns_interface {
a2a915
 						/*%< UDP dispatchers. */
a2a915
 	isc_socket_t *		tcpsocket;	/*%< TCP socket. */
a2a915
 	isc_dscp_t		dscp;		/*%< "listen-on" DSCP value */
a2a915
-	int			ntcptarget;	/*%< Desired number of concurrent
a2a915
-						     TCP accepts */
a2a915
-	int			ntcpcurrent;	/*%< Current ditto, locked */
a2a915
+	int32_t			ntcpaccepting;	/*%< Number of clients
a2a915
+						     ready to accept new
a2a915
+						     TCP connections on this
a2a915
+						     interface */
a2a915
+	int32_t			ntcpactive;	/*%< Number of clients
a2a915
+						     servicing TCP queries
a2a915
+						     (whether accepting or
a2a915
+						     connected) */
a2a915
 	int			nudpdispatch;	/*%< Number of UDP dispatches */
a2a915
 	ns_clientmgr_t *	clientmgr;	/*%< Client manager. */
a2a915
 	ISC_LINK(ns_interface_t) link;
a2a915
diff --git a/bin/named/interfacemgr.c b/bin/named/interfacemgr.c
a2a915
index d8c7188..96c080b 100644
a2a915
--- a/bin/named/interfacemgr.c
a2a915
+++ b/bin/named/interfacemgr.c
a2a915
@@ -384,8 +384,9 @@ ns_interface_create(ns_interfacemgr_t *mgr, isc_sockaddr_t *addr,
a2a915
 	 * connections will be handled in parallel even though there is
a2a915
 	 * only one client initially.
a2a915
 	 */
a2a915
-	ifp->ntcptarget = 1;
a2a915
-	ifp->ntcpcurrent = 0;
a2a915
+	ifp->ntcpaccepting = 0;
a2a915
+	ifp->ntcpactive = 0;
a2a915
+
a2a915
 	ifp->nudpdispatch = 0;
a2a915
 
a2a915
 	ifp->dscp = -1;
a2a915
@@ -520,9 +521,7 @@ ns_interface_accepttcp(ns_interface_t *ifp) {
a2a915
 	 */
a2a915
 	(void)isc_socket_filter(ifp->tcpsocket, "dataready");
a2a915
 
a2a915
-	result = ns_clientmgr_createclients(ifp->clientmgr,
a2a915
-					    ifp->ntcptarget, ifp,
a2a915
-					    ISC_TRUE);
a2a915
+	result = ns_clientmgr_createclients(ifp->clientmgr, 1, ifp, ISC_TRUE);
a2a915
 	if (result != ISC_R_SUCCESS) {
a2a915
 		UNEXPECTED_ERROR(__FILE__, __LINE__,
a2a915
 				 "TCP ns_clientmgr_createclients(): %s",
a2a915
diff --git a/lib/isc/include/isc/quota.h b/lib/isc/include/isc/quota.h
a2a915
index b9bf598..36c5830 100644
a2a915
--- a/lib/isc/include/isc/quota.h
a2a915
+++ b/lib/isc/include/isc/quota.h
a2a915
@@ -100,6 +100,13 @@ isc_quota_attach(isc_quota_t *quota, isc_quota_t **p);
a2a915
  * quota if successful (ISC_R_SUCCESS or ISC_R_SOFTQUOTA).
a2a915
  */
a2a915
 
a2a915
+isc_result_t
a2a915
+isc_quota_force(isc_quota_t *quota, isc_quota_t **p);
a2a915
+/*%<
a2a915
+ * Like isc_quota_attach, but will attach '*p' to the quota
a2a915
+ * even if the hard quota has been exceeded.
a2a915
+ */
a2a915
+
a2a915
 void
a2a915
 isc_quota_detach(isc_quota_t **p);
a2a915
 /*%<
a2a915
diff --git a/lib/isc/quota.c b/lib/isc/quota.c
a2a915
index 3ddff0d..20976a4 100644
a2a915
--- a/lib/isc/quota.c
a2a915
+++ b/lib/isc/quota.c
a2a915
@@ -74,20 +74,39 @@ isc_quota_release(isc_quota_t *quota) {
a2a915
 	UNLOCK(&quota->lock);
a2a915
 }
a2a915
 
a2a915
-isc_result_t
a2a915
-isc_quota_attach(isc_quota_t *quota, isc_quota_t **p)
a2a915
-{
a2a915
+static isc_result_t
a2a915
+doattach(isc_quota_t *quota, isc_quota_t **p, isc_boolean_t force) {
a2a915
 	isc_result_t result;
a2a915
-	INSIST(p != NULL && *p == NULL);
a2a915
+	REQUIRE(p != NULL && *p == NULL);
a2a915
+
a2a915
 	result = isc_quota_reserve(quota);
a2a915
-	if (result == ISC_R_SUCCESS || result == ISC_R_SOFTQUOTA)
a2a915
+	if (result == ISC_R_SUCCESS || result == ISC_R_SOFTQUOTA) {
a2a915
+		*p = quota;
a2a915
+	} else if (result == ISC_R_QUOTA && force) {
a2a915
+		/* attach anyway */
a2a915
+		LOCK(&quota->lock);
a2a915
+		quota->used++;
a2a915
+		UNLOCK(&quota->lock);
a2a915
+
a2a915
 		*p = quota;
a2a915
+		result = ISC_R_SUCCESS;
a2a915
+	}
a2a915
+
a2a915
 	return (result);
a2a915
 }
a2a915
 
a2a915
+isc_result_t
a2a915
+isc_quota_attach(isc_quota_t *quota, isc_quota_t **p) {
a2a915
+	return (doattach(quota, p, ISC_FALSE));
a2a915
+}
a2a915
+
a2a915
+isc_result_t
a2a915
+isc_quota_force(isc_quota_t *quota, isc_quota_t **p) {
a2a915
+	return (doattach(quota, p, ISC_TRUE));
a2a915
+}
a2a915
+
a2a915
 void
a2a915
-isc_quota_detach(isc_quota_t **p)
a2a915
-{
a2a915
+isc_quota_detach(isc_quota_t **p) {
a2a915
 	INSIST(p != NULL && *p != NULL);
a2a915
 	isc_quota_release(*p);
a2a915
 	*p = NULL;
a2a915
-- 
a2a915
2.20.1
a2a915