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

0ba27c
From 33064cd077cf6fa386f0a5a840c2161868da7b3a Mon Sep 17 00:00:00 2001
0ba27c
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@isc.org>
0ba27c
Date: Tue, 8 Feb 2022 12:42:34 +0100
0ba27c
Subject: [PATCH] Run .closehandle_cb asynchrounosly in nmhandle_detach_cb()
0ba27c
0ba27c
When sock->closehandle_cb is set, we need to run nmhandle_detach_cb()
0ba27c
asynchronously to ensure correct order of multiple packets processing in
0ba27c
the isc__nm_process_sock_buffer().  When not run asynchronously, it
0ba27c
would cause:
0ba27c
0ba27c
  a) out-of-order processing of the return codes from processbuffer();
0ba27c
0ba27c
  b) stack growth because the next TCP DNS message read callback will
0ba27c
     be called from within the current TCP DNS message read callback.
0ba27c
0ba27c
The sock->closehandle_cb is set to isc__nm_resume_processing() for TCP
0ba27c
sockets which calls isc__nm_process_sock_buffer().  If the read callback
0ba27c
(called from isc__nm_process_sock_buffer()->processbuffer()) doesn't
0ba27c
attach to the nmhandle (f.e. because it wants to drop the processing or
0ba27c
we send the response directly via uv_try_write()), the
0ba27c
isc__nm_resume_processing() (via .closehandle_cb) would call
0ba27c
isc__nm_process_sock_buffer() recursively.
0ba27c
0ba27c
The below shortened code path shows how the stack can grow:
0ba27c
0ba27c
 1: ns__client_request(handle, ...);
0ba27c
 2: isc_nm_tcpdns_sequential(handle);
0ba27c
 3: ns_query_start(client, handle);
0ba27c
 4:   query_lookup(qctx);
0ba27c
 5:     query_send(qctcx->client);
0ba27c
 6:       isc__nmhandle_detach(&client->reqhandle);
0ba27c
 7:         nmhandle_detach_cb(&handle);
0ba27c
 8:           sock->closehandle_cb(sock); // isc__nm_resume_processing
0ba27c
 9:             isc__nm_process_sock_buffer(sock);
0ba27c
10:               processbuffer(sock); // isc__nm_tcpdns_processbuffer
0ba27c
11:                 isc_nmhandle_attach(req->handle, &handle);
0ba27c
12:                 isc__nm_readcb(sock, req, ISC_R_SUCCESS);
0ba27c
13:                   isc__nm_async_readcb(NULL, ...);
0ba27c
14:                     uvreq->cb.recv(...); // ns__client_request
0ba27c
0ba27c
Instead, if 'sock->closehandle_cb' is set, we need to run detach the
0ba27c
handle asynchroniously in 'isc__nmhandle_detach', so that on line 8 in
0ba27c
the code flow above does not start this recursion. This ensures the
0ba27c
correct order when processing multiple packets in the function
0ba27c
'isc__nm_process_sock_buffer()' and prevents the stack growth.
0ba27c
0ba27c
When not run asynchronously, the out-of-order processing leaves the
0ba27c
first TCP socket open until all requests on the stream have been
0ba27c
processed.
0ba27c
0ba27c
If the pipelining is disabled on the TCP via `keep-response-order`
0ba27c
configuration option, named would keep the first socket in lingering
0ba27c
CLOSE_WAIT state when the client sends an incomplete packet and then
0ba27c
closes the connection from the client side.
0ba27c
0ba27c
(cherry picked from commit afee2b5a7bc933a2d987907fc327a9f118fdbd17)
0ba27c
---
0ba27c
 lib/isc/netmgr/netmgr.c | 6 +++++-
0ba27c
 1 file changed, 5 insertions(+), 1 deletion(-)
0ba27c
0ba27c
diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c
0ba27c
index 3283eb6e4f..0ed3182fb6 100644
0ba27c
--- a/lib/isc/netmgr/netmgr.c
0ba27c
+++ b/lib/isc/netmgr/netmgr.c
0ba27c
@@ -1746,8 +1746,12 @@ isc__nmhandle_detach(isc_nmhandle_t **handlep FLARG) {
0ba27c
 	handle = *handlep;
0ba27c
 	*handlep = NULL;
0ba27c
 
0ba27c
+	/*
0ba27c
+	 * If the closehandle_cb is set, it needs to run asynchronously to
0ba27c
+	 * ensure correct ordering of the isc__nm_process_sock_buffer().
0ba27c
+	 */
0ba27c
 	sock = handle->sock;
0ba27c
-	if (sock->tid == isc_nm_tid()) {
0ba27c
+	if (sock->tid == isc_nm_tid() && sock->closehandle_cb == NULL) {
0ba27c
 		nmhandle_detach_cb(&handle FLARG_PASS);
0ba27c
 	} else {
0ba27c
 		isc__netievent_detach_t *event =
0ba27c
-- 
0ba27c
2.34.1
0ba27c