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

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