Blame SOURCES/bind-9.16-CVE-2022-3924.patch

7a7026
From 20424b3bfe8d3fae92c11a30e79aeffd26dc2891 Mon Sep 17 00:00:00 2001
7a7026
From: Aram Sargsyan <aram@isc.org>
7a7026
Date: Mon, 14 Nov 2022 12:18:06 +0000
7a7026
Subject: [PATCH] Cancel all fetch events in dns_resolver_cancelfetch()
7a7026
7a7026
Although 'dns_fetch_t' fetch can have two associated events, one for
7a7026
each of 'DNS_EVENT_FETCHDONE' and 'DNS_EVENT_TRYSTALE' types, the
7a7026
dns_resolver_cancelfetch() function is designed in a way that it
7a7026
expects only one existing event, which it must cancel, and when it
7a7026
happens so that 'stale-answer-client-timeout' is enabled and there
7a7026
are two events, only one of them is canceled, and it results in an
7a7026
assertion in dns_resolver_destroyfetch(), when it finds a dangling
7a7026
event.
7a7026
7a7026
Change the logic of dns_resolver_cancelfetch() function so that it
7a7026
cancels both the events (if they exist), and in the right order.
7a7026
7a7026
(cherry picked from commit ec2098ca35039e4f81fd0aa7c525eb960b8f47bf)
7a7026
---
7a7026
 lib/dns/resolver.c | 53 +++++++++++++++++++++++++++++++++++-----------
7a7026
 lib/ns/query.c     |  4 +++-
7a7026
 2 files changed, 44 insertions(+), 13 deletions(-)
7a7026
7a7026
diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c
7a7026
index 18585b5..7cbfbb2 100644
7a7026
--- a/lib/dns/resolver.c
7a7026
+++ b/lib/dns/resolver.c
7a7026
@@ -11254,8 +11254,9 @@ void
7a7026
 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
7a7026
 	fetchctx_t *fctx;
7a7026
 	dns_resolver_t *res;
7a7026
-	dns_fetchevent_t *event, *next_event;
7a7026
-	isc_task_t *etask;
7a7026
+	dns_fetchevent_t *event = NULL;
7a7026
+	dns_fetchevent_t *event_trystale = NULL;
7a7026
+	dns_fetchevent_t *event_fetchdone = NULL;
7a7026
 
7a7026
 	REQUIRE(DNS_FETCH_VALID(fetch));
7a7026
 	fctx = fetch->private;
7a7026
@@ -11267,32 +11268,60 @@ dns_resolver_cancelfetch(dns_fetch_t *fetch) {
7a7026
 	LOCK(&res->buckets[fctx->bucketnum].lock);
7a7026
 
7a7026
 	/*
7a7026
-	 * Find the completion event for this fetch (as opposed
7a7026
+	 * Find the events for this fetch (as opposed
7a7026
 	 * to those for other fetches that have joined the same
7a7026
-	 * fctx) and send it with result = ISC_R_CANCELED.
7a7026
+	 * fctx) and send them with result = ISC_R_CANCELED.
7a7026
 	 */
7a7026
-	event = NULL;
7a7026
 	if (fctx->state != fetchstate_done) {
7a7026
+		dns_fetchevent_t *next_event = NULL;
7a7026
 		for (event = ISC_LIST_HEAD(fctx->events); event != NULL;
7a7026
 		     event = next_event) {
7a7026
 			next_event = ISC_LIST_NEXT(event, ev_link);
7a7026
 			if (event->fetch == fetch) {
7a7026
 				ISC_LIST_UNLINK(fctx->events, event, ev_link);
7a7026
-				break;
7a7026
+				switch (event->ev_type) {
7a7026
+				case DNS_EVENT_TRYSTALE:
7a7026
+					INSIST(event_trystale == NULL);
7a7026
+					event_trystale = event;
7a7026
+					break;
7a7026
+				case DNS_EVENT_FETCHDONE:
7a7026
+					INSIST(event_fetchdone == NULL);
7a7026
+					event_fetchdone = event;
7a7026
+					break;
7a7026
+				default:
7a7026
+					ISC_UNREACHABLE();
7a7026
+				}
7a7026
+				if (event_trystale != NULL &&
7a7026
+				    event_fetchdone != NULL)
7a7026
+				{
7a7026
+					break;
7a7026
+				}
7a7026
 			}
7a7026
 		}
7a7026
 	}
7a7026
-	if (event != NULL) {
7a7026
-		etask = event->ev_sender;
7a7026
-		event->ev_sender = fctx;
7a7026
-		event->result = ISC_R_CANCELED;
7a7026
-		isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
7a7026
+
7a7026
+	/*
7a7026
+	 * The "trystale" event must be sent before the "fetchdone" event,
7a7026
+	 * because the latter clears the "recursing" query attribute, which is
7a7026
+	 * required by both events (handled by the same callback function).
7a7026
+	 */
7a7026
+	if (event_trystale != NULL) {
7a7026
+		isc_task_t *etask = event_trystale->ev_sender;
7a7026
+		event_trystale->ev_sender = fctx;
7a7026
+		event_trystale->result = ISC_R_CANCELED;
7a7026
+		isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event_trystale));
7a7026
 	}
7a7026
+	if (event_fetchdone != NULL) {
7a7026
+		isc_task_t *etask = event_fetchdone->ev_sender;
7a7026
+		event_fetchdone->ev_sender = fctx;
7a7026
+		event_fetchdone->result = ISC_R_CANCELED;
7a7026
+		isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event_fetchdone));
7a7026
+ 	}
7a7026
+
7a7026
 	/*
7a7026
 	 * The fctx continues running even if no fetches remain;
7a7026
 	 * the answer is still cached.
7a7026
 	 */
7a7026
-
7a7026
 	UNLOCK(&res->buckets[fctx->bucketnum].lock);
7a7026
 }
7a7026
 
7a7026
diff --git a/lib/ns/query.c b/lib/ns/query.c
7a7026
index f66bab4..4f61374 100644
7a7026
--- a/lib/ns/query.c
7a7026
+++ b/lib/ns/query.c
7a7026
@@ -6021,7 +6021,9 @@ fetch_callback(isc_task_t *task, isc_event_t *event) {
7a7026
 	CTRACE(ISC_LOG_DEBUG(3), "fetch_callback");
7a7026
 
7a7026
 	if (event->ev_type == DNS_EVENT_TRYSTALE) {
7a7026
-		query_lookup_stale(client);
7a7026
+		if (devent->result != ISC_R_CANCELED) {
7a7026
+			query_lookup_stale(client);
7a7026
+		}
7a7026
 		isc_event_free(ISC_EVENT_PTR(&event));
7a7026
 		return;
7a7026
 	}
7a7026
-- 
7a7026
2.39.1
7a7026