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

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