Blame SOURCES/bind-9.11-CVE-2018-5743-atomic.patch

8fa458
From 94e08314024c812063bf99bd191a46265a2ba49f Mon Sep 17 00:00:00 2001
8fa458
From: Petr Mensik <pemensik@redhat.com>
8fa458
Date: Wed, 24 Apr 2019 21:10:26 +0200
8fa458
Subject: [PATCH] Missing atomic fix to original CVE patch
8fa458
8fa458
---
8fa458
 bin/named/client.c                     | 18 +++++++-----------
8fa458
 bin/named/include/named/interfacemgr.h |  5 +++--
8fa458
 bin/named/interfacemgr.c               |  7 +++++--
8fa458
 3 files changed, 15 insertions(+), 15 deletions(-)
8fa458
8fa458
diff --git a/bin/named/client.c b/bin/named/client.c
8fa458
index 3ada6e9..d3bf47d 100644
8fa458
--- a/bin/named/client.c
8fa458
+++ b/bin/named/client.c
8fa458
@@ -405,12 +405,10 @@ tcpconn_detach(ns_client_t *client) {
8fa458
 static void
8fa458
 mark_tcp_active(ns_client_t *client, isc_boolean_t active) {
8fa458
 	if (active && !client->tcpactive) {
8fa458
-		isc_atomic_xadd(&client->interface->ntcpactive, 1);
8fa458
+		isc_refcount_increment0(&client->interface->ntcpactive, NULL);
8fa458
 		client->tcpactive = active;
8fa458
 	} else if (!active && client->tcpactive) {
8fa458
-		uint32_t old =
8fa458
-			isc_atomic_xadd(&client->interface->ntcpactive, -1);
8fa458
-		INSIST(old > 0);
8fa458
+		isc_refcount_decrement(&client->interface->ntcpactive, NULL);
8fa458
 		client->tcpactive = active;
8fa458
 	}
8fa458
 }
8fa458
@@ -557,7 +555,7 @@ exit_check(ns_client_t *client) {
8fa458
 		if (client->mortal && TCP_CLIENT(client) &&
8fa458
 		    client->newstate != NS_CLIENTSTATE_FREED &&
8fa458
 		    !ns_g_clienttest &&
8fa458
-		    isc_atomic_xadd(&client->interface->ntcpaccepting, 0) == 0)
8fa458
+		    isc_refcount_current(&client->interface->ntcpaccepting) == 0)
8fa458
 		{
8fa458
 			/* Nobody else is accepting */
8fa458
 			client->mortal = ISC_FALSE;
8fa458
@@ -3321,7 +3319,6 @@ client_newconn(isc_task_t *task, isc_event_t *event) {
8fa458
 	isc_result_t result;
8fa458
 	ns_client_t *client = event->ev_arg;
8fa458
 	isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
8fa458
-	uint32_t old;
8fa458
 
8fa458
 	REQUIRE(event->ev_type == ISC_SOCKEVENT_NEWCONN);
8fa458
 	REQUIRE(NS_CLIENT_VALID(client));
8fa458
@@ -3341,8 +3338,7 @@ client_newconn(isc_task_t *task, isc_event_t *event) {
8fa458
 	INSIST(client->naccepts == 1);
8fa458
 	client->naccepts--;
8fa458
 
8fa458
-	old = isc_atomic_xadd(&client->interface->ntcpaccepting, -1);
8fa458
-	INSIST(old > 0);
8fa458
+	isc_refcount_decrement(&client->interface->ntcpaccepting, NULL);
8fa458
 
8fa458
 	/*
8fa458
 	 * We must take ownership of the new socket before the exit
8fa458
@@ -3473,8 +3469,8 @@ client_accept(ns_client_t *client) {
8fa458
 		 * quota is tcp-clients plus the number of listening
8fa458
 		 * interfaces plus 1.)
8fa458
 		 */
8fa458
-		exit = (isc_atomic_xadd(&client->interface->ntcpactive, 0) >
8fa458
-			(client->tcpactive ? 1 : 0));
8fa458
+		exit = (isc_refcount_current(&client->interface->ntcpactive) >
8fa458
+			(client->tcpactive ? 1U : 0U));
8fa458
 		if (exit) {
8fa458
 			client->newstate = NS_CLIENTSTATE_INACTIVE;
8fa458
 			(void)exit_check(client);
8fa458
@@ -3532,7 +3528,7 @@ client_accept(ns_client_t *client) {
8fa458
 	 * listening for connections itself to prevent the interface
8fa458
 	 * going dead.
8fa458
 	 */
8fa458
-	isc_atomic_xadd(&client->interface->ntcpaccepting, 1);
8fa458
+	isc_refcount_increment0(&client->interface->ntcpaccepting, NULL);
8fa458
 }
8fa458
 
8fa458
 static void
8fa458
diff --git a/bin/named/include/named/interfacemgr.h b/bin/named/include/named/interfacemgr.h
8fa458
index d9ac90f..aa21049 100644
8fa458
--- a/bin/named/include/named/interfacemgr.h
8fa458
+++ b/bin/named/include/named/interfacemgr.h
8fa458
@@ -43,6 +43,7 @@
8fa458
 #include <isc/magic.h>
8fa458
 #include <isc/mem.h>
8fa458
 #include <isc/socket.h>
8fa458
+#include <isc/refcount.h>
8fa458
 
8fa458
 #include <dns/result.h>
8fa458
 
8fa458
@@ -73,11 +74,11 @@ struct ns_interface {
8fa458
 						/*%< UDP dispatchers. */
8fa458
 	isc_socket_t *		tcpsocket;	/*%< TCP socket. */
8fa458
 	isc_dscp_t		dscp;		/*%< "listen-on" DSCP value */
8fa458
-	int32_t			ntcpaccepting;	/*%< Number of clients
8fa458
+	isc_refcount_t		ntcpaccepting;	/*%< Number of clients
8fa458
 						     ready to accept new
8fa458
 						     TCP connections on this
8fa458
 						     interface */
8fa458
-	int32_t			ntcpactive;	/*%< Number of clients
8fa458
+	isc_refcount_t		ntcpactive;	/*%< Number of clients
8fa458
 						     servicing TCP queries
8fa458
 						     (whether accepting or
8fa458
 						     connected) */
8fa458
diff --git a/bin/named/interfacemgr.c b/bin/named/interfacemgr.c
8fa458
index 96c080b..2ce97bb 100644
8fa458
--- a/bin/named/interfacemgr.c
8fa458
+++ b/bin/named/interfacemgr.c
8fa458
@@ -384,8 +384,8 @@ ns_interface_create(ns_interfacemgr_t *mgr, isc_sockaddr_t *addr,
8fa458
 	 * connections will be handled in parallel even though there is
8fa458
 	 * only one client initially.
8fa458
 	 */
8fa458
-	ifp->ntcpaccepting = 0;
8fa458
-	ifp->ntcpactive = 0;
8fa458
+	isc_refcount_init(&ifp->ntcpaccepting, 0);
8fa458
+	isc_refcount_init(&ifp->ntcpactive, 0);
8fa458
 
8fa458
 	ifp->nudpdispatch = 0;
8fa458
 
8fa458
@@ -616,6 +616,9 @@ ns_interface_destroy(ns_interface_t *ifp) {
8fa458
 
8fa458
 	ns_interfacemgr_detach(&ifp->mgr);
8fa458
 
8fa458
+	isc_refcount_destroy(&ifp->ntcpactive);
8fa458
+	isc_refcount_destroy(&ifp->ntcpaccepting);
8fa458
+
8fa458
 	ifp->magic = 0;
8fa458
 	isc_mem_put(mctx, ifp, sizeof(*ifp));
8fa458
 }
8fa458
-- 
8fa458
2.20.1
8fa458