4ba560
From 3bcd32572504ac9b92e3c6ec1e2cee3df3b68309 Mon Sep 17 00:00:00 2001
4ba560
From: Petr Mensik <pemensik@redhat.com>
4ba560
Date: Tue, 20 Sep 2022 11:34:42 +0200
4ba560
Subject: [PATCH 2/4] Fix CVE-2022-3080
4ba560
4ba560
5960.	[security]	Fix serve-stale crash that could happen when
4ba560
			stale-answer-client-timeout was set to 0 and there was
4ba560
			a stale CNAME in the cache for an incoming query.
4ba560
			(CVE-2022-3080) [GL #3517]
4ba560
---
4ba560
 lib/ns/include/ns/query.h |  1 +
4ba560
 lib/ns/query.c            | 42 ++++++++++++++++++++++++---------------
4ba560
 2 files changed, 27 insertions(+), 16 deletions(-)
4ba560
4ba560
diff --git a/lib/ns/include/ns/query.h b/lib/ns/include/ns/query.h
4ba560
index 4d48cf6..34b3070 100644
4ba560
--- a/lib/ns/include/ns/query.h
4ba560
+++ b/lib/ns/include/ns/query.h
4ba560
@@ -145,6 +145,7 @@ struct query_ctx {
4ba560
 	bool authoritative;		    /* authoritative query? */
4ba560
 	bool want_restart;		    /* CNAME chain or other
4ba560
 					     * restart needed */
4ba560
+	bool		refresh_rrset;	    /* stale RRset refresh needed */
4ba560
 	bool		need_wildcardproof; /* wildcard proof needed */
4ba560
 	bool		nxrewrite;	    /* negative answer from RPZ */
4ba560
 	bool		findcoveringnsec;   /* lookup covering NSEC */
4ba560
diff --git a/lib/ns/query.c b/lib/ns/query.c
4ba560
index 249321c..a450cb7 100644
4ba560
--- a/lib/ns/query.c
4ba560
+++ b/lib/ns/query.c
4ba560
@@ -5686,7 +5686,6 @@ query_lookup(query_ctx_t *qctx) {
4ba560
 	bool dbfind_stale = false;
4ba560
 	bool stale_timeout = false;
4ba560
 	bool stale_found = false;
4ba560
-	bool refresh_rrset = false;
4ba560
 	bool stale_refresh_window = false;
4ba560
 
4ba560
 	CCTRACE(ISC_LOG_DEBUG(3), "query_lookup");
4ba560
@@ -5868,8 +5867,7 @@ query_lookup(query_ctx_t *qctx) {
4ba560
 					"%s stale answer used, an attempt to "
4ba560
 					"refresh the RRset will still be made",
4ba560
 					namebuf);
4ba560
-				refresh_rrset = STALE(qctx->rdataset);
4ba560
-				qctx->client->nodetach = refresh_rrset;
4ba560
+				qctx->refresh_rrset = STALE(qctx->rdataset);
4ba560
 			}
4ba560
 		} else {
4ba560
 			/*
4ba560
@@ -5907,17 +5905,6 @@ query_lookup(query_ctx_t *qctx) {
4ba560
 
4ba560
 	result = query_gotanswer(qctx, result);
4ba560
 
4ba560
-	if (refresh_rrset) {
4ba560
-		/*
4ba560
-		 * If we reached this point then it means that we have found a
4ba560
-		 * stale RRset entry in cache and BIND is configured to allow
4ba560
-		 * queries to be answered with stale data if no active RRset
4ba560
-		 * is available, i.e. "stale-anwer-client-timeout 0". But, we
4ba560
-		 * still need to refresh the RRset.
4ba560
-		 */
4ba560
-		query_refresh_rrset(qctx);
4ba560
-	}
4ba560
-
4ba560
 cleanup:
4ba560
 	return (result);
4ba560
 }
4ba560
@@ -7737,11 +7724,14 @@ query_addanswer(query_ctx_t *qctx) {
4ba560
 
4ba560
 	/*
4ba560
 	 * On normal lookups, clear any rdatasets that were added on a
4ba560
-	 * lookup due to stale-answer-client-timeout.
4ba560
+	 * lookup due to stale-answer-client-timeout. Do not clear if we
4ba560
+	 * are going to refresh the RRset, because the stale contents are
4ba560
+	 * prioritized.
4ba560
 	 */
4ba560
 	if (QUERY_STALEOK(&qctx->client->query) &&
4ba560
-	    !QUERY_STALETIMEOUT(&qctx->client->query))
4ba560
+	    !QUERY_STALETIMEOUT(&qctx->client->query) && !qctx->refresh_rrset)
4ba560
 	{
4ba560
+		CCTRACE(ISC_LOG_DEBUG(3), "query_clear_stale");
4ba560
 		query_clear_stale(qctx->client);
4ba560
 		/*
4ba560
 		 * We can clear the attribute to prevent redundant clearing
4ba560
@@ -11457,9 +11447,29 @@ ns_query_done(query_ctx_t *qctx) {
4ba560
 	/*
4ba560
 	 * Client may have been detached after query_send(), so
4ba560
 	 * we test and store the flag state here, for safety.
4ba560
+	 * If we are refreshing the RRSet, we must not detach from the client
4ba560
+	 * in the query_send(), so we need to override the flag.
4ba560
 	 */
4ba560
+	if (qctx->refresh_rrset) {
4ba560
+		qctx->client->nodetach = true;
4ba560
+	}
4ba560
 	nodetach = qctx->client->nodetach;
4ba560
 	query_send(qctx->client);
4ba560
+
4ba560
+	if (qctx->refresh_rrset) {
4ba560
+		/*
4ba560
+		 * If we reached this point then it means that we have found a
4ba560
+		 * stale RRset entry in cache and BIND is configured to allow
4ba560
+		 * queries to be answered with stale data if no active RRset
4ba560
+		 * is available, i.e. "stale-anwer-client-timeout 0". But, we
4ba560
+		 * still need to refresh the RRset. To prevent adding duplicate
4ba560
+		 * RRsets, clear the RRsets from the message before doing the
4ba560
+		 * refresh.
4ba560
+		 */
4ba560
+		message_clearrdataset(qctx->client->message, 0);
4ba560
+		query_refresh_rrset(qctx);
4ba560
+	}
4ba560
+
4ba560
 	if (!nodetach) {
4ba560
 		qctx->detach_client = true;
4ba560
 	}
4ba560
-- 
4ba560
2.37.3
4ba560