c-ares: backport security fixes from 1.34.9
PATCH 1/1 c-ares: backport security fixes from 1.34.9
Add the eight unique upstream commits that fix the security advisories addressed in c-ares 1.34.9.
Several advisories share commits, so patches are ordered by commit rather than duplicated per advisory; each patch lists the GHSA IDs it resolves.
--- ...troy-detach-queries-before-callbacks.patch | 2603 +++++++++++++++++ ...ed-socket-server-state-notifications.patch | 393 +++ ...qcache-hand-callbacks-their-own-copy.patch | 1054 +++++++ ...connections-torn-down-by-application.patch | 1107 +++++++ ...pt-an-answer-on-the-right-connection.patch | 575 ++++ ...tions-read-channel-config-under-lock.patch | 243 ++ ...res_inet_pton-reject-a-prefix-length.patch | 364 +++ ...nt-drop-UDP-answers-from-server-port.patch | 178 ++ .../recipes-support/c-ares/c-ares_1.34.8.bb | 8 + 9 files changed, 6525 insertions(+) create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-detach-queries-before-callbacks.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket-server-state-notifications.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand-callbacks-their-own-copy.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-down-by-application.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-an-answer-on-the-right-connection.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-channel-config-under-lock.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-prefix-length.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-UDP-answers-from-server-port.patch
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-detach-queries-before-callbacks.patch new file +2603 -0
@@ -0,0 +1,2603 @@ +From 7f408deb128fbdcce78122322734ffaa01cad9ae Mon Sep 17 00:00:00 2001 +From: Brad House <[email protected]> +Date: Sun, 18 Oct 2026 18:15:41 -0400 +Subject: [PATCH] ares_cancel/ares_destroy: detach queries before running user + callbacks +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Backport of dd62bdfca52ba5c37e88a1469c0a4c5e4a568ccb to v1.34. See that +commit for the full description; it hardens reentrant teardown from +callbacks (ares_cancel(), ares_destroy(), server reconfiguration, and +socket/server state notifications). + +Conflicts were whitespace/alignment only (v1.34 predates the clang-format +reformat in #1238) and were reformatted to v1.34's style. + +Also carries the test portion of #1334 that applies to the tests this +introduces: dead-server tests use 127.0.0.1:53055/53056 instead of +127.0.0.55/56, which only routes on Linux. + +Signed-off-by: Brad House <[email protected]> +Upstream-Status: Backport [https://github.com/c-ares/c-ares/commit/7f408deb128fbdcce78122322734ffaa01cad9ae] +GHSA: GHSA-wpr3-8q3r-4q8c, GHSA-97vq-4mxv-7vhm, GHSA-mw23-pfg6-2x9r, GHSA-rf6g-ww74-97qh, GHSA-v33j-65h4-4ch8, GHSA-pj7p-v3cw-hq66 +--- + docs/ares_cancel.3 | 17 + + docs/ares_destroy.3 | 7 + + docs/ares_init_options.3 | 15 + + docs/ares_set_server_state_callback.3 | 31 +- + docs/ares_set_servers_csv.3 | 17 + + src/lib/ares_cancel.c | 25 +- + src/lib/ares_close_sockets.c | 82 +- + src/lib/ares_conn.c | 217 ++++- + src/lib/ares_conn.h | 37 +- + src/lib/ares_destroy.c | 85 +- + src/lib/ares_private.h | 110 ++- + src/lib/ares_process.c | 235 +++++- + src/lib/ares_send.c | 35 + + src/lib/ares_update_servers.c | 42 +- + test/ares-test-mock.cc | 1068 +++++++++++++++++++++++++ + 15 files changed, 1959 insertions(+), 64 deletions(-) + +diff --git a/docs/ares_cancel.3 b/docs/ares_cancel.3 +index 967a946b49..069f226c10 100644 +--- a/docs/ares_cancel.3 ++++ b/docs/ares_cancel.3 +@@ -21,6 +21,23 @@ These calls give the callbacks a chance to clean up any state which might have + been stored in their arguments. If such a callback invocation adds a new + request to the channel, that request will \fInot\fP be cancelled by the + current invocation of \fBares_cancel(3)\fP. ++.SH REENTRANCY ++A callback invoked by ++.BR ares_cancel (3) ++may call c-ares functions on the same channel. The channel lock is recursive, ++and each query is unlinked from the library's internal structures before its ++callback runs, so a reentrant call cannot encounter a query that is part-way ++through being torn down. ++ ++This includes starting new lookups, as described above, and reconfiguring the ++server list with ++.BR ares_set_servers_csv (3) ++and related functions. ++ ++.SH AVAILABILITY ++The reentrancy guarantee described above applies as of c-ares version 1.35.0, ++and c-ares version 1.34.9 on the 1.34 branch. ++ + .SH SEE ALSO + .BR ares_init (3) + .BR ares_destroy (3) +diff --git a/docs/ares_destroy.3 b/docs/ares_destroy.3 +index d1984b7cc9..0b6380e609 100644 +--- a/docs/ares_destroy.3 ++++ b/docs/ares_destroy.3 +@@ -20,6 +20,13 @@ by the channel. + channel, passing a status of \fIARES_EDESTRUCTION\fP. These calls give the + callbacks a chance to clean up any state which might have been stored in their + arguments. A callback must not add new requests to a channel being destroyed. ++Such a request is refused: the request is not started, and its callback is ++invoked with a status of \fIARES_EDESTRUCTION\fP so that any state attached to ++its argument can be released. A request made from within that notification is ++refused without a further notification, so that a callback which responds to ++failure by retrying cannot recurse without bound; its argument is not handed ++back. This applies as of c-ares version 1.35.0, and c-ares version 1.34.9 on ++the 1.34 branch. + + There is no ability to make this function thread-safe. No additional calls + using this channel may be made once this function is called. +diff --git a/docs/ares_init_options.3 b/docs/ares_init_options.3 +index 9b3b481535..e6f6ee8ba6 100644 +--- a/docs/ares_init_options.3 ++++ b/docs/ares_init_options.3 +@@ -231,6 +231,21 @@ will be passed the socket whose state has changed; + will be set to true if the socket should listen for read events, and + .I writable + will be set to true if the socket should listen for write events. ++ ++This callback may call c-ares functions on the same channel, including ++.BR ares_cancel (3) ++and the server configuration functions. Notifications are delivered once the ++library has finished the work that produced them rather than part-way through ++it, so a reentrant call cannot close a socket the library is still using. They ++arrive in the order the state changes occurred, and always before the ++outermost c-ares call returns. A call made from within a callback is not the ++outermost one, so notifications it produces arrive after it has returned. ++.BR ares_destroy (3) ++is no exception: the closure of every socket the channel still holds is ++reported before it returns, and the callback is never invoked afterwards. ++This applies as of c-ares version 1.35.0, and c-ares version 1.34.9 on the ++1.34 branch. ++ + The value of + .I sock_state_cb_data + will be passed as the +diff --git a/docs/ares_set_server_state_callback.3 b/docs/ares_set_server_state_callback.3 +index 04fbde4c7a..e95db327ce 100644 +--- a/docs/ares_set_server_state_callback.3 ++++ b/docs/ares_set_server_state_callback.3 +@@ -47,6 +47,31 @@ the DNS servers used by an ares channel. For example, it can be used to track + metrics about the numbers and types of queries sent to each server or to + detect when a server is uncontactable or unhealthy. + ++.SH REENTRANCY ++The server state callback may call c-ares functions on the same channel, ++including ++.BR ares_cancel (3) ++and the server configuration functions. The channel lock is recursive and the ++library holds no query, connection or server state across the callback. ++ ++Notifications are queued and delivered once the library has finished the work ++that produced them, rather than part-way through it. A notification is ++therefore observed after the processing pass that generated it, not during. ++ ++The callback and its ++.I user_data ++are read when the notification is delivered, not when it is queued. An ++application that replaces its callback between those two points will see the ++notification delivered to the new one; unregistering by passing a ++.B NULL ++callback discards any notification still queued. ++ ++This applies to a replacement made from within a c-ares callback, where the ++channel lock is held. ++.BR ares_set_server_state_callback (3) ++does not take the channel lock, so replacing the callback from another thread ++while the channel is processing is not synchronized against delivery. ++ + .SH FLAGS + .TP 5 + .B ARES_SERV_STATE_UDP +@@ -58,5 +83,9 @@ Indicates that the query was tried over TCP. + .SH AVAILABILITY + This function was first introduced in c-ares version 1.29.0. + ++The reentrancy guarantee described above applies as of c-ares version 1.35.0, ++and c-ares version 1.34.9 on the 1.34 branch. ++ + .SH SEE ALSO +-.BR ares_get_servers_csv (3) ++.BR ares_get_servers_csv (3), ++.BR ares_cancel (3) +diff --git a/docs/ares_set_servers_csv.3 b/docs/ares_set_servers_csv.3 +index f1435143f5..f9f5b82e28 100644 +--- a/docs/ares_set_servers_csv.3 ++++ b/docs/ares_set_servers_csv.3 +@@ -126,6 +126,23 @@ are identical. Prior versions would simply omit ports in \fBares_set_servers_cs + but due to the addition of link local interface support, this difference was + removed. + ++.SH REENTRANCY ++These functions may be called from within a c-ares callback. Removing a server ++closes its connections, which ends the lookups that were using it and invokes ++their callbacks, so a reconfiguration can be requested from inside one. ++ ++A nested call reconciles the whole server list, and the outer call leaves that ++result in place rather than deleting the servers the nested call installed. ++The consequence is that the outer call's own list does not survive, even ++though it returns ++.B ARES_SUCCESS: ++the last configuration applied is the one the channel keeps. An application ++that reconfigures from a callback should therefore not assume the list it ++passed to the outer call is in effect when that call returns. ++ ++This applies as of c-ares version 1.35.0, and c-ares version 1.34.9 on the ++1.34 branch. ++ + .SH EXAMPLE + .nf + 192.168.1.100,[fe80::1]:53%eth0,dns://192.168.1.1?tcpport=1153 +diff --git a/src/lib/ares_cancel.c b/src/lib/ares_cancel.c +index 75600dea6b..aa167e4486 100644 +--- a/src/lib/ares_cancel.c ++++ b/src/lib/ares_cancel.c +@@ -38,10 +38,10 @@ void ares_cancel(ares_channel_t *channel) + } + + ares_channel_lock(channel); ++ ares_deferred_cb_enter(channel); + + if (ares_llist_len(channel->all_queries) > 0) { + ares_llist_node_t *node = NULL; +- ares_llist_node_t *next = NULL; + + /* Swap list heads, so that only those queries which were present on entry + * into this function are cancelled. New queries added by callbacks of +@@ -57,21 +57,29 @@ void ares_cancel(ares_channel_t *channel) + goto done; /* LCOV_EXCL_LINE: OutOfMemory */ + } + +- node = ares_llist_node_first(list_copy); +- while (node != NULL) { ++ /* Re-read the head each iteration rather than caching the next node: a ++ * callback below may cause other queries in this same list to be ended ++ * and their nodes destroyed. */ ++ while ((node = ares_llist_node_first(list_copy)) != NULL) { + ares_query_t *query; + +- /* Cache next since this node is being deleted */ +- next = ares_llist_node_next(node); ++ query = ares_llist_node_claim(node); + +- query = ares_llist_node_claim(node); ++ /* Must come first: node_claim() already destroyed this node, so ++ * ares_detach_query() would otherwise destroy it a second time. */ + query->node_all_queries = NULL; + ++ /* Detach from queries_by_qid, the connection and the timeout list ++ * before running user code. Otherwise a reentrant teardown from ++ * within the callback -- ares_set_servers_csv(), or a new query ++ * hitting a connection error -- still finds this query, ends it a ++ * second time and frees it, and the ares_free_query() below then ++ * double-frees. */ ++ ares_detach_query(query); ++ + /* NOTE: its possible this may enqueue new queries */ + query->callback(query->arg, ARES_ECANCELLED, 0, NULL); + ares_free_query(query); +- +- node = next; + } + + ares_llist_destroy(list_copy); +@@ -83,5 +91,6 @@ void ares_cancel(ares_channel_t *channel) + ares_queue_notify_empty(channel); + + done: ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } +diff --git a/src/lib/ares_close_sockets.c b/src/lib/ares_close_sockets.c +index 7bb72c31ec..6be662fdd3 100644 +--- a/src/lib/ares_close_sockets.c ++++ b/src/lib/ares_close_sockets.c +@@ -41,12 +41,47 @@ static void ares_requeue_queries(ares_conn_t *conn, + } + } + ++/*! Release a connection's memory. Split from the unlink below so that the ++ * two can happen at different times: a caller further up the stack may still ++ * hold this pointer, and reading a detached connection is safe where reading ++ * a freed one is not. */ ++void ares_conn_reap(ares_conn_t *conn) ++{ ++ if (conn == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ ares_buf_destroy(conn->in_buf); ++ ares_buf_destroy(conn->out_buf); ++ ares_llist_destroy(conn->queries_to_conn); ++ ares_free(conn); ++} ++ + void ares_close_connection(ares_conn_t *conn, ares_status_t requeue_status) + { +- ares_server_t *server = conn->server; +- ares_channel_t *channel = server->channel; ++ ares_server_t *server; ++ ares_channel_t *channel; ++ ++ if (conn == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ /* Already torn down and awaiting reclamation. Reaching here twice is ++ * ordinary once a connection can outlive its teardown: a caller holding it ++ * from before -- handle_conn_error() after a failed write, say -- closes it ++ * again without knowing. Doing the work twice would requeue queries that ++ * have already moved and would put this connection on the pending list a ++ * second time, which is a double free. */ ++ if (conn->flags & ARES_CONN_FLAG_DEAD) { ++ return; ++ } + +- /* Unlink */ ++ server = conn->server; ++ channel = server->channel; ++ ++ /* Unlink from everything that can find this connection. After this it is ++ * unreachable by lookup, which is what stops anything new from picking it ++ * up; anything that already holds it is handled by the deferral below. */ + ares_llist_node_claim( + ares_htable_asvp_get_direct(channel->connnode_by_socket, conn->fd)); + ares_htable_asvp_remove(channel->connnode_by_socket, conn->fd); +@@ -55,19 +90,40 @@ void ares_close_connection(ares_conn_t *conn, ares_status_t requeue_status) + server->tcp_conn = NULL; + } + +- ares_buf_destroy(conn->in_buf); +- ares_buf_destroy(conn->out_buf); ++ /* Marked dead as soon as it is unlinked, before anything below can run ++ * application code. ares_requeue_queries() ends queries terminally once ++ * their retries are exhausted, which invokes the application's callback; if ++ * the flag were set after that, a second ares_close_connection() reaching ++ * this connection in the meantime would run the body again -- closing the ++ * descriptor twice and listing the connection for reclamation twice, which ++ * is a double free. It also gives ares_close_sockets() its termination ++ * condition: dead implies unlinked, so its re-read-the-head loop cannot see ++ * a dead connection and spin. */ ++ conn->flags |= ARES_CONN_FLAG_DEAD; + + /* Requeue queries to other connections */ + ares_requeue_queries(conn, requeue_status); + +- ares_llist_destroy(conn->queries_to_conn); +- +- ares_conn_sock_state_cb_update(conn, ARES_CONN_STATE_NONE); +- +- ares_socket_close(channel, conn->fd); ++ /* Hands the descriptor to the queued notification when one is due, so the ++ * application is told to stop watching it before it is closed. Otherwise ++ * nothing will carry it and we close it here. */ ++ if (!ares_conn_sock_state_cb_close(conn)) { ++ ares_socket_close(channel, conn->fd); ++ } ++ conn->fd = ARES_SOCKET_BAD; ++ ++ /* If a library call is in progress it may be holding this connection -- ++ * read_answers() and ares_send_query_int() both do, across code that can ++ * reach here. Keep it readable until that call unwinds. The buffers and ++ * the query list are freed by ares_conn_reap(), so a stale holder sees an ++ * emptied connection rather than freed memory. */ ++ if (channel->deferred_cb_depth > 0) { ++ conn->next_pending = channel->pending_conns; ++ channel->pending_conns = conn; ++ return; ++ } + +- ares_free(conn); ++ ares_conn_reap(conn); + } + + void ares_close_sockets(ares_server_t *server) +@@ -76,6 +132,10 @@ void ares_close_sockets(ares_server_t *server) + + while ((node = ares_llist_node_first(server->connections)) != NULL) { + ares_conn_t *conn = ares_llist_node_val(node); ++ /* This loop re-reads the head each time and relies on the close below ++ * unlinking it. A dead connection returns early without unlinking, so ++ * one on this list would spin forever holding the channel lock. */ ++ assert(!(conn->flags & ARES_CONN_FLAG_DEAD)); + ares_close_connection(conn, ARES_SUCCESS); + } + } +diff --git a/src/lib/ares_conn.c b/src/lib/ares_conn.c +index 6b315b0548..78f57e7d10 100644 +--- a/src/lib/ares_conn.c ++++ b/src/lib/ares_conn.c +@@ -26,19 +26,210 @@ + */ + #include "ares_private.h" + +-void ares_conn_sock_state_cb_update(ares_conn_t *conn, +- ares_conn_state_flags_t flags) ++/*! A queued socket-state notification. Holds the descriptor and the state, ++ * never the connection, so a queued entry cannot outlive what it refers to. ++ * ++ * When \c owns_fd is set, this message carries the only remaining reference to ++ * the descriptor: the connection is gone and the descriptor is deliberately ++ * left open until after the application has been told to stop watching it. ++ * Closing first would let the number be reused -- by application code running ++ * from a deferred query callback, for instance -- before the notification ++ * naming it is delivered. */ ++typedef struct { ++ ares_socket_t fd; ++ ares_bool_t readable; ++ ares_bool_t writable; ++ ares_bool_t owns_fd; ++} ares_sock_state_msg_t; ++ ++static ares_bool_t ares_sock_state_queue(ares_channel_t *channel, ++ const ares_sock_state_msg_t *msg) + { +- ares_channel_t *channel = conn->server->channel; ++ if (channel->sock_state_queue == NULL) { ++ channel->sock_state_queue = ares_array_create(sizeof(*msg), NULL); ++ if (channel->sock_state_queue == NULL) { ++ return ARES_FALSE; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ } ++ ++ if (ares_array_insertdata_last(channel->sock_state_queue, msg) != ++ ARES_SUCCESS) { ++ return ARES_FALSE; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ ++ return ARES_TRUE; ++} ++ ++/*! Deliver every socket-state notification the channel has queued. Loops ++ * rather than snapshotting: a callback is allowed to re-enter the library and ++ * queue more, and those belong to this same delivery point. */ ++static void ares_sock_state_dispatch(ares_channel_t *channel) ++{ ++ while (ares_array_len(channel->sock_state_queue) > 0) { ++ ares_sock_state_msg_t msg; ++ ++ if (ares_array_claim_at(&msg, sizeof(msg), channel->sock_state_queue, 0) != ++ ARES_SUCCESS) { ++ break; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } + +- if ((conn->state_flags & ARES_CONN_STATE_CBFLAGS) != flags && +- channel->sock_state_cb) { +- channel->sock_state_cb(channel->sock_state_cb_data, conn->fd, +- flags & ARES_CONN_STATE_READ ? 1 : 0, +- flags & ARES_CONN_STATE_WRITE ? 1 : 0); ++ if (channel->sock_state_cb != NULL) { ++ /* The public callback takes ints. */ ++ channel->sock_state_cb(channel->sock_state_cb_data, msg.fd, ++ msg.readable ? 1 : 0, msg.writable ? 1 : 0); ++ } ++ ++ /* Told first, closed second -- the ordering the application depends on to ++ * keep its descriptor set honest. */ ++ if (msg.owns_fd) { ++ ares_socket_close(channel, msg.fd); ++ } ++ } ++ ++ ares_array_destroy(channel->sock_state_queue); ++ channel->sock_state_queue = NULL; ++} ++ ++static void ares_deferred_cb_flush(ares_channel_t *channel) ++{ ++ /* Only the outermost frame delivers: anything above us on the stack may ++ * still be holding the connection a reentrant call from one of these ++ * callbacks could free. ++ * ++ * The dispatch below counts as a nesting level of its own. Without that, a ++ * callback that calls back into the library re-enters here through the ++ * enter/leave pair on that call, drains the rest of the queue and destroys ++ * it underneath the loop -- and hands the next notification to the ++ * application from inside the current one. With the counter held, the ++ * nested leave returns immediately and whatever it queued is picked up by ++ * the loop that is already running. */ ++ if (channel->deferred_cb_depth > 0) { ++ return; ++ } ++ channel->deferred_cb_depth++; ++ ++ /* Both kinds of deferred notification share this one delivery point. Loop ++ * rather than calling each once: a socket-state callback may put the library ++ * on a path that queues a server-state notification, and the reverse, and ++ * both belong to this delivery point rather than the next one. */ ++ while (ares_array_len(channel->sock_state_queue) > 0 || ++ ares_array_len(channel->server_state_queue) > 0) { ++ ares_sock_state_dispatch(channel); ++ ares_server_state_dispatch(channel); ++ } ++ ++ /* The outermost call is unwinding and the callbacks above have run, so ++ * nothing can still be holding a server or connection that was unlinked ++ * during it. Now they can be freed. */ ++ ares_deferred_reclaim(channel); ++ ++ channel->deferred_cb_depth--; ++} ++ ++void ares_deferred_reclaim(ares_channel_t *channel) ++{ ++ /* Connections first: a pending server's connection list is already empty, ++ * but this keeps the ordering obvious rather than incidental. */ ++ while (channel->pending_conns != NULL) { ++ ares_conn_t *conn = channel->pending_conns; ++ channel->pending_conns = conn->next_pending; ++ conn->next_pending = NULL; ++ ares_conn_reap(conn); ++ } ++ ++ while (channel->pending_servers != NULL) { ++ ares_server_t *server = channel->pending_servers; ++ channel->pending_servers = server->next_pending; ++ server->next_pending = NULL; ++ ares_server_reap(server); ++ } ++} ++ ++void ares_deferred_cb_enter(ares_channel_t *channel) ++{ ++ channel->deferred_cb_depth++; ++} ++ ++void ares_deferred_cb_leave(ares_channel_t *channel) ++{ ++ if (channel->deferred_cb_depth > 0) { ++ channel->deferred_cb_depth--; ++ } ++ ares_deferred_cb_flush(channel); ++} ++ ++ares_bool_t ares_conn_sock_state_cb_close(ares_conn_t *conn) ++{ ++ ares_channel_t *channel = conn->server->channel; ++ ares_sock_state_msg_t msg; ++ ++ /* No callback to run, so nothing will ever carry this descriptor: the ++ * caller closes it itself, exactly as before. */ ++ if (channel->sock_state_cb == NULL) { ++ return ARES_FALSE; ++ } ++ ++ /* Already reported as unwatched, so no notification is due and no message ++ * will be queued to hold the descriptor. */ ++ if ((conn->state_flags & ARES_CONN_STATE_CBFLAGS) == ARES_CONN_STATE_NONE) { ++ return ARES_FALSE; ++ } ++ ++ msg.fd = conn->fd; ++ msg.readable = ARES_FALSE; ++ msg.writable = ARES_FALSE; ++ msg.owns_fd = ARES_TRUE; ++ ++ if (!ares_sock_state_queue(channel, &msg)) { ++ return ARES_FALSE; /* LCOV_EXCL_LINE: OutOfMemory */ + } + + conn->state_flags &= ~((unsigned int)ARES_CONN_STATE_CBFLAGS); ++ ++ return ARES_TRUE; ++} ++ ++void ares_conn_sock_state_cb_update(ares_conn_t *conn, ++ ares_conn_state_flags_t flags) ++{ ++ ares_channel_t *channel = conn->server->channel; ++ ares_sock_state_msg_t msg; ++ ares_bool_t changed = ARES_FALSE; ++ ++ if ((conn->state_flags & ARES_CONN_STATE_CBFLAGS) != flags) { ++ changed = ARES_TRUE; ++ } ++ ++ msg.fd = conn->fd; ++ msg.readable = flags & ARES_CONN_STATE_READ ? ARES_TRUE : ARES_FALSE; ++ msg.writable = flags & ARES_CONN_STATE_WRITE ? ARES_TRUE : ARES_FALSE; ++ msg.owns_fd = ARES_FALSE; /* the connection still owns it */ ++ ++ if (!changed || channel->sock_state_cb == NULL) { ++ conn->state_flags &= ~((unsigned int)ARES_CONN_STATE_CBFLAGS); ++ conn->state_flags |= flags; ++ return; ++ } ++ ++ /* Queue rather than dispatch. This runs in the middle of opening, writing ++ * to or closing a connection, and every caller keeps using that connection ++ * afterwards. The callback is application code that may reentrantly call ++ * ares_cancel() or ares_set_servers_csv(), either of which frees the ++ * connection -- the first through the idle-connection sweep, the second by ++ * destroying the server that owns it. Deliver once no frame still holds ++ * it. ++ * ++ * Only the descriptor and the state are captured, never the connection, and ++ * insertion order is preserved so the application observes the same ++ * sequence it would have seen inline. */ ++ if (!ares_sock_state_queue(channel, &msg)) { ++ return; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ ++ /* Recorded as reported only now: on either failure above the connection ++ * stays eligible to notify again, rather than remembering a state the ++ * application was never told about. */ ++ conn->state_flags &= ~((unsigned int)ARES_CONN_STATE_CBFLAGS); + conn->state_flags |= flags; + } + +@@ -435,6 +626,16 @@ ares_status_t ares_open_connection(ares_conn_t **conn_out, + conn->flags |= ARES_CONN_FLAG_TFO_INITIAL; + } + ++ /* The socket-configure and socket-create callbacks above are application ++ * code, and either may reconfigure the servers -- which destroys the ones ++ * the new configuration omits, including possibly this one. Everything ++ * below dereferences it, starting with conn->server in ++ * ares_conn_set_self_ip(). */ ++ if (!ares_server_in_channel(channel, server)) { ++ status = ARES_ECONNREFUSED; ++ goto done; ++ } ++ + /* Need to store our own ip for DNS cookie support */ + status = ares_conn_set_self_ip(conn, ARES_TRUE); + if (status != ARES_SUCCESS) { +diff --git a/src/lib/ares_conn.h b/src/lib/ares_conn.h +index c332de14b1..fc177b84f6 100644 +--- a/src/lib/ares_conn.h ++++ b/src/lib/ares_conn.h +@@ -49,7 +49,14 @@ typedef enum { + * it). Its in-flight queries continue to drain and it is cleaned up once + * idle. This is a per-connection signal so that a transient failure does + * not evict otherwise-healthy connections to the same server. */ +- ARES_CONN_FLAG_NONEW = 1 << 3 ++ ARES_CONN_FLAG_NONEW = 1 << 3, ++ /*! Connection has been torn down: unlinked from every lookup structure, its ++ * descriptor closed or handed to a pending notification, and its queries ++ * requeued. It survives only so that a caller further up the stack which ++ * captured it before the teardown can still read it; see ++ * ares_deferred_reclaim(). Teardown is idempotent -- a second ++ * ares_close_connection() on a dead connection does nothing. */ ++ ARES_CONN_FLAG_DEAD = 1 << 4 + } ares_conn_flags_t; + + typedef enum { +@@ -84,6 +91,10 @@ struct ares_conn { + + /* list of outstanding queries to this connection */ + ares_llist_t *queries_to_conn; ++ ++ /*! Next connection awaiting reclamation. Set only while this connection is ++ * on channel->pending_conns; see ares_deferred_reclaim(). */ ++ ares_conn_t *next_pending; + }; + + /*! Various buckets for grouping history */ +@@ -168,6 +179,10 @@ struct ares_server { + + /* Link back to owning channel */ + ares_channel_t *channel; ++ ++ /*! Next server awaiting reclamation. Set only while this server is on ++ * channel->pending_servers; see ares_deferred_reclaim(). */ ++ ares_server_t *next_pending; + }; + + void ares_close_connection(ares_conn_t *conn, ares_status_t requeue_status); +@@ -185,8 +200,24 @@ ares_status_t ares_conn_flush(ares_conn_t *conn); + ares_conn_err_t ares_conn_read(ares_conn_t *conn, void *data, size_t len, + size_t *read_bytes); + ares_conn_t *ares_conn_from_fd(const ares_channel_t *channel, ares_socket_t fd); +-void ares_conn_sock_state_cb_update(ares_conn_t *conn, +- ares_conn_state_flags_t flags); ++ ++/*! Queue the "stop watching this descriptor" notification for a connection ++ * being torn down, handing the descriptor to that queued message. ++ * ++ * The application must be told to stop watching a descriptor while it is ++ * still open -- close first and the number can be handed to something else, ++ * including by application code running from a deferred query callback, ++ * before the notification naming it is delivered. ++ * ++ * \param[in] conn Connection being closed. ++ * \return ARES_TRUE if the queued notification took the descriptor, in which ++ * case the caller must not close it; ARES_FALSE if no notification is ++ * due, and the caller closes the descriptor itself. ++ */ ++ares_bool_t ares_conn_sock_state_cb_close(ares_conn_t *conn); ++ ++void ares_conn_sock_state_cb_update(ares_conn_t *conn, ++ ares_conn_state_flags_t flags); + ares_conn_err_t ares_socket_recv(ares_channel_t *channel, ares_socket_t s, + ares_bool_t is_tcp, void *data, + size_t data_len, size_t *read_bytes); +diff --git a/src/lib/ares_destroy.c b/src/lib/ares_destroy.c +index 1e5706e0e0..e0534c800d 100644 +--- a/src/lib/ares_destroy.c ++++ b/src/lib/ares_destroy.c +@@ -69,17 +69,31 @@ void ares_destroy(ares_channel_t *channel) + * callbacks need to hold a channel lock. */ + ares_channel_lock(channel); + +- /* Destroy all queries */ +- node = ares_llist_node_first(channel->all_queries); +- while (node != NULL) { +- ares_llist_node_t *next = ares_llist_node_next(node); +- ares_query_t *query = ares_llist_node_claim(node); +- ++ /* Destroy all queries. ++ * ++ * The queries are detached before their callbacks run. Claiming the node ++ * only unlinks a query from all_queries; it stays in queries_by_qid, on ++ * conn->queries_to_conn and in queries_by_timeout. A reentrant teardown ++ * from inside the callback -- ares_set_servers_csv(), or a new query ++ * hitting a connection error -- would otherwise still find the query, end ++ * it a second time (invoking the application callback twice for one ++ * lookup) and free it, and the ares_free_query() below would double-free. ++ * ++ * The loop re-reads the first node each iteration rather than caching the ++ * next one, since a callback can cause other queries in this same list to ++ * be ended and their nodes destroyed. No list swap is needed: sys_up is ++ * already cleared above, so ares_send_nolock() refuses new queries and ++ * nothing can be added while we drain. */ ++ while ((node = ares_llist_node_first(channel->all_queries)) != NULL) { ++ ares_query_t *query = ares_llist_node_claim(node); ++ ++ /* Must come first: node_claim() already destroyed this node, so ++ * ares_detach_query() would otherwise destroy it a second time. */ + query->node_all_queries = NULL; ++ ares_detach_query(query); ++ + query->callback(query->arg, ARES_EDESTRUCTION, 0, NULL); + ares_free_query(query); +- +- node = next; + } + + ares_queue_notify_empty(channel); +@@ -93,7 +107,28 @@ void ares_destroy(ares_channel_t *channel) + assert(ares_slist_len(channel->queries_by_timeout) == 0); + #endif + ++ /* Tearing down the servers closes every remaining connection. Applications ++ * rely on being told about those closures so they can drop the descriptors ++ * from their own event loop, so the notifications are still delivered here ++ * rather than discarded -- just after the teardown instead of during it. */ ++ ares_deferred_cb_enter(channel); + ares_destroy_servers_state(channel); ++ ares_deferred_cb_leave(channel); ++ ++ /* The leave above delivered and destroyed both queues and reclaimed ++ * everything the teardown unlinked, so these are normally no-ops. They are ++ * not redundant: if the first insert into a freshly created queue fails, ++ * the array is left allocated and empty, the flush loop never runs because ++ * both queues are empty, and nothing else would ever free it. */ ++ ares_array_destroy(channel->sock_state_queue); ++ channel->sock_state_queue = NULL; ++ ares_array_destroy(channel->server_state_queue); ++ channel->server_state_queue = NULL; ++ ++#ifndef NDEBUG ++ assert(channel->pending_servers == NULL); ++ assert(channel->pending_conns == NULL); ++#endif + + #ifndef NDEBUG + assert(ares_htable_asvp_num_keys(channel->connnode_by_socket) == 0); +@@ -134,17 +169,47 @@ void ares_destroy(ares_channel_t *channel) + ares_free(channel); + } + +-void ares_destroy_server(ares_server_t *server) ++/*! Release a server's memory. Split from ares_destroy_server() for the same ++ * reason ares_conn_reap() is split out: a caller further up the stack may ++ * still hold this pointer. */ ++void ares_server_reap(ares_server_t *server) + { + if (server == NULL) { + return; /* LCOV_EXCL_LINE: DefensiveCoding */ + } + +- ares_close_sockets(server); + ares_llist_destroy(server->connections); + ares_free(server); + } + ++void ares_destroy_server(ares_server_t *server) ++{ ++ ares_channel_t *channel; ++ ++ if (server == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ channel = server->channel; ++ ++ /* Closes and unlinks every connection; each is deferred or reaped by ++ * ares_close_connection() on the same rule applied below. */ ++ ares_close_sockets(server); ++ ++ /* The caller has already unlinked this server from channel->servers, so it ++ * can no longer be found -- ares_server_in_channel() and ++ * ares_slist_node_find() both report it gone, which is what callers check ++ * before acting on one. But a library call in progress may still be ++ * holding the pointer, so keep it readable until that call unwinds. */ ++ if (channel != NULL && channel->deferred_cb_depth > 0) { ++ server->next_pending = channel->pending_servers; ++ channel->pending_servers = server; ++ return; ++ } ++ ++ ares_server_reap(server); ++} ++ + void ares_destroy_servers_state(ares_channel_t *channel) + { + ares_slist_node_t *node; +diff --git a/src/lib/ares_private.h b/src/lib/ares_private.h +index 04d4576ed2..0a1029e0f8 100644 +--- a/src/lib/ares_private.h ++++ b/src/lib/ares_private.h +@@ -147,7 +147,10 @@ typedef struct ares_query ares_query_t; + struct ares_query { + /* Query ID from qbuf, for faster lookup, and current timeout */ + unsigned short qid; /* host byte order */ +- ares_timeval_t ts; /*!< Timestamp query was sent */ ++ /*! Distinguishes this query from any later one handed the same qid. ++ * Query ids are 16-bit and recycled; this counter is not. */ ++ size_t serial; ++ ares_timeval_t ts; /*!< Timestamp query was sent */ + ares_timeval_t timeout; + ares_channel_t *channel; + +@@ -227,6 +230,9 @@ struct ares_channeldata { + /* Server addresses and communications state. Sorted by least consecutive + * failures, followed by the configuration order if failures are equal. */ + ares_slist_t *servers; ++ /*! Bumped by each ares_servers_update(). Lets the stale-server removal loop ++ * tell that a nested reconfiguration reconciled the list underneath it. */ ++ size_t servers_generation; + + /* random state to use when generating new ids and generating retry penalties + */ +@@ -237,6 +243,10 @@ struct ares_channeldata { + /* Queries bucketed by qid, for quickly dispatching DNS responses: */ + ares_htable_szvp_t *queries_by_qid; + ++ /*! Monotonic source for ares_query_t.serial. Never reused for the life of ++ * the channel, so a recycled query id cannot impersonate an older query. */ ++ size_t query_serial; ++ + /* Queries bucketed by timeout, for quickly handling timeouts: */ + ares_slist_t *queries_by_timeout; + +@@ -248,6 +258,29 @@ struct ares_channeldata { + + ares_sock_state_cb sock_state_cb; + void *sock_state_cb_data; ++ /*! Socket-state notifications queued during processing, delivered once the ++ * library is no longer holding a connection a reentrant call could free. ++ * Insertion order is preserved, so an application observes the same ++ * sequence of state changes it would have seen inline. */ ++ ares_array_t *sock_state_queue; ++ /*! Depth of nested library entry points that may produce a deferred ++ * application notification. Queues are delivered as this returns to ++ * zero. Shared by every deferred callback kind rather than one counter ++ * per kind, so the delivery rule exists in exactly one place. */ ++ size_t deferred_cb_depth; ++ ++ /*! Servers and connections that have been unlinked from every lookup ++ * structure but not yet freed, because a library call was in progress and ++ * may still hold a pointer to them. Drained by the same flush that ++ * delivers deferred notifications, once no frame can still be holding one. ++ * ++ * This is what makes a stale ares_server_t or ares_conn_t readable rather ++ * than freed for the remainder of the call that captured it. Callers ++ * still revalidate before *acting* on one -- ares_server_in_channel() and ++ * friends -- but a missed check is now "used a server that was removed" ++ * rather than a use-after-free. */ ++ ares_server_t *pending_servers; ++ ares_conn_t *pending_conns; + + ares_sock_create_callback sock_create_cb; + void *sock_create_cb_data; +@@ -295,6 +328,10 @@ struct ares_channeldata { + /* Callback triggered when a server has a successful or failed response */ + ares_server_state_callback server_state_cb; + void *server_state_cb_data; ++ /*! Server-state notifications queued during processing, dispatched once the ++ * library is no longer holding pointers a reentrant callback could free. ++ * See ares_deferred_cb_leave(). */ ++ ares_array_t *server_state_queue; + + /* TRUE if a reinit is pending. Reinit spawns a thread to read the system + * configuration and then apply the configuration since configuration +@@ -308,6 +345,11 @@ struct ares_channeldata { + * system config changes might get triggered and we need a flag to make + * sure we don't take action. */ + ares_bool_t sys_up; ++ ++ /*! Set while ares_send_nolock() is running the refusal notification for a ++ * channel being destroyed. Bounds the recursion at one frame when the ++ * application's callback responds by starting another lookup. */ ++ ares_bool_t in_shutdown_refusal; + }; + + /* Does the domain end in ".onion" or ".onion."? Case-insensitive. */ +@@ -351,6 +393,16 @@ void *ares_dnsrec_convert_arg(ares_callback callback, void *arg); + void ares_dnsrec_convert_cb(void *arg, ares_status_t status, size_t timeouts, + const ares_dns_record_t *dnsrec); + ++/*! Unlink a query from every list and table that tracks it. ++ * ++ * Must be called before running application code that could reach the query ++ * again, so a reentrant teardown cannot find it and end it a second time. ++ * ++ * \param[in] query Query to detach. Safe to call more than once on the ++ * same query. ++ */ ++void ares_detach_query(ares_query_t *query); ++ + void ares_free_query(ares_query_t *query); + + unsigned short ares_generate_new_id(ares_rand_state *state); +@@ -390,7 +442,6 @@ ares_status_t ares_sysconfig_set_options(ares_sysconfig_t *sysconfig, + + ares_status_t ares_init_by_environment(ares_sysconfig_t *sysconfig); + +- + typedef ares_status_t (*ares_sysconfig_line_cb_t)(const ares_channel_t *channel, + ares_sysconfig_t *sysconfig, + ares_buf_t *line); +@@ -589,6 +640,60 @@ ares_status_t ares_dns_name_write(ares_buf_t *buf, ares_llist_t **list, + */ + void ares_queue_notify_empty(ares_channel_t *channel); + ++/*! Is this server still installed on the channel? ++ * ++ * Application callbacks may reconfigure the servers, which destroys any that ++ * the new configuration omits. A raw ares_server_t captured before such a ++ * callback must be revalidated before it is used again. ++ * ++ * \param[in] channel Initialized ares channel. ++ * \param[in] server Server pointer captured earlier. ++ * \return ARES_TRUE if still present. ++ */ ++ares_bool_t ares_server_in_channel(const ares_channel_t *channel, ++ const ares_server_t *server); ++ ++/*! Free everything awaiting reclamation on the channel. ++ * ++ * Called only from the deferred-callback flush, at the point where the ++ * outermost library call is unwinding and no frame can still hold a pointer ++ * to an unlinked server or connection. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_deferred_reclaim(ares_channel_t *channel); ++ ++/*! Free one connection's memory. Only for the deferred-reclaim path and the ++ * immediate path in ares_close_connection(). */ ++void ares_conn_reap(ares_conn_t *conn); ++ ++/*! Free one server's memory. Only for the deferred-reclaim path and the ++ * immediate path in ares_destroy_server(). */ ++void ares_server_reap(ares_server_t *server); ++ ++/*! Enter a region that may produce application notifications the library is ++ * not in a position to deliver yet -- socket-state and server-state changes. ++ * Nesting is counted; queued notifications are delivered by the outermost ++ * leave. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_deferred_cb_enter(ares_channel_t *channel); ++ ++/*! Leave such a region. The outermost leave delivers the queues, at which ++ * point the library holds no connection a reentrant call could free. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_deferred_cb_leave(ares_channel_t *channel); ++ ++/*! Deliver every server-state notification the channel has queued. Called ++ * only from the shared deferred-callback flush. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_server_state_dispatch(ares_channel_t *channel); ++ + #define ARES_CONFIG_CHECK(x) \ + (x && x->lookups && ares_slist_len(x->servers) > 0 && x->timeout > 0 && \ + x->tries > 0) +@@ -636,7 +741,6 @@ typedef struct ares_event_thread ares_event_thread_t; + void ares_event_thread_destroy(ares_channel_t *channel); + ares_status_t ares_event_thread_init(ares_channel_t *channel); + +- + #ifdef _WIN32 + # define HOSTENT_ADDRTYPE_TYPE short + # define HOSTENT_LENGTH_TYPE short +diff --git a/src/lib/ares_process.c b/src/lib/ares_process.c +index b2316c4b33..82f53610fb 100644 +--- a/src/lib/ares_process.c ++++ b/src/lib/ares_process.c +@@ -69,7 +69,6 @@ static ares_status_t ares_send_query_int(ares_server_t *requested_server, + ares_query_t *query, + const ares_timeval_t *now, + ares_array_t **requeue); +-static void ares_detach_query(ares_query_t *query); + + static void ares_query_remove_from_conn(ares_query_t *query) + { +@@ -81,14 +80,32 @@ static void ares_query_remove_from_conn(ares_query_t *query) + query->conn = NULL; + } + ++/*! A queued server-state notification. Holds the formatted address rather ++ * than the server, so it stays valid however the server list changes. */ ++typedef struct { ++ char *server_string; ++ ares_bool_t success; ++ int flags; ++} ares_server_state_msg_t; ++ ++static void ares_server_state_msg_free(void *arg) ++{ ++ ares_server_state_msg_t *msg = arg; ++ if (msg == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ares_free(msg->server_string); ++} ++ + /* Invoke the server state callback after a success or failure */ + static void invoke_server_state_cb(const ares_server_t *server, + ares_bool_t success, int flags) + { +- const ares_channel_t *channel = server->channel; +- ares_buf_t *buf; +- ares_status_t status; +- char *server_string; ++ ares_channel_t *channel = server->channel; ++ ares_buf_t *buf; ++ ares_status_t status; ++ char *server_string; ++ ares_server_state_msg_t msg; + + if (channel->server_state_cb == NULL) { + return; +@@ -111,9 +128,57 @@ static void invoke_server_state_cb(const ares_server_t *server, + return; /* LCOV_EXCL_LINE: OutOfMemory */ + } + +- channel->server_state_cb(server_string, success, flags, +- channel->server_state_cb_data); +- ares_free(server_string); ++ /* Queue rather than dispatch. This runs in the middle of processing, and ++ * every caller keeps using its query / connection / server afterwards. The ++ * callback is application code that may reentrantly call ares_cancel(), ++ * which frees those objects -- so dispatch once the library is no longer ++ * holding them. Same philosophy as the deferred query-callback dispatch. ++ * ++ * Only the formatted address is captured, never the ares_server_t, so a ++ * queued entry cannot outlive anything it points at. */ ++ if (channel->server_state_queue == NULL) { ++ channel->server_state_queue = ares_array_create( ++ sizeof(ares_server_state_msg_t), ares_server_state_msg_free); ++ if (channel->server_state_queue == NULL) { ++ /* LCOV_EXCL_START: OutOfMemory */ ++ ares_free(server_string); ++ return; ++ /* LCOV_EXCL_STOP */ ++ } ++ } ++ ++ msg.server_string = server_string; ++ msg.success = success; ++ msg.flags = flags; ++ ++ if (ares_array_insertdata_last(channel->server_state_queue, &msg) != ++ ARES_SUCCESS) { ++ ares_free(server_string); /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++} ++ ++/*! Deliver every server-state notification the channel has queued. Loops ++ * rather than snapshotting: a callback is allowed to re-enter the library and ++ * queue more, and those belong to this same delivery point. */ ++void ares_server_state_dispatch(ares_channel_t *channel) ++{ ++ while (ares_array_len(channel->server_state_queue) > 0) { ++ ares_server_state_msg_t msg; ++ ++ if (ares_array_claim_at(&msg, sizeof(msg), channel->server_state_queue, ++ 0) != ARES_SUCCESS) { ++ break; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ if (channel->server_state_cb != NULL) { ++ channel->server_state_cb(msg.server_string, msg.success, msg.flags, ++ channel->server_state_cb_data); ++ } ++ ares_free(msg.server_string); ++ } ++ ++ ares_array_destroy(channel->server_state_queue); ++ channel->server_state_queue = NULL; + } + + static void server_increment_failures(ares_server_t *server, +@@ -204,6 +269,8 @@ static ares_status_t ares_process_fds_nolock(ares_channel_t *channel, + return ARES_EFORMERR; /* LCOV_EXCL_LINE: DefensiveCoding */ + } + ++ ares_deferred_cb_enter(channel); ++ + ares_tvnow(&now); + + /* Process write events */ +@@ -244,6 +311,8 @@ static ares_status_t ares_process_fds_nolock(ares_channel_t *channel, + } + + done: ++ ares_deferred_cb_leave(channel); ++ + if (status == ARES_ENOMEM) { + return ARES_ENOMEM; + } +@@ -553,6 +622,8 @@ typedef enum { + typedef struct { + requeue_type_t type; /* type of entry, requeue or endquery */ + unsigned short qid; /* query id */ ++ size_t serial; /* identifies the query the qid meant when ++ * this entry was appended */ + ares_server_t *server; /* requeue only: optional */ + ares_status_t status; /* endquery only */ + ares_dns_record_t *dnsrec; /* endquery only: optional */ +@@ -578,6 +649,7 @@ static ares_status_t ares_append_requeue_int(ares_array_t **requeue, + + entry.type = type; + entry.qid = query->qid; ++ entry.serial = query->serial; + entry.server = server; + entry.status = status; + entry.dnsrec = dnsrec; +@@ -601,6 +673,53 @@ static ares_status_t ares_append_endqueue(ares_array_t **requeue, + dnsrec); + } + ++/*! Is the query the caller captured still the one holding \a qid? ++ * ++ * A query id is a 16-bit wire value reissued as soon as the query holding it ++ * is detached, so it names a slot rather than a query, and comparing the ++ * pointer as well does not help: the replacement is often allocated at the ++ * address the freed one had. The serial is a channel-wide counter that is ++ * never reused. */ ++static ares_bool_t ares_query_is_live(const ares_channel_t *channel, ++ unsigned short qid, size_t serial) ++{ ++ const ares_query_t *query = ++ ares_htable_szvp_get_direct(channel->queries_by_qid, qid); ++ ++ if (query == NULL || query->serial != serial) { ++ return ARES_FALSE; ++ } ++ ++ return ARES_TRUE; ++} ++ ++/*! Is this exact server object still in the channel's server list? ++ * ++ * Compares pointers only and never dereferences \a server. Deferred ++ * reclamation keeps an unlinked server readable for the rest of the call ++ * that captured it, so reading through it is no longer a use-after-free -- ++ * but it has been removed from the configuration and must not be acted on, ++ * which is what this reports. A channel has a handful of servers, so the ++ * linear scan does not matter. */ ++ares_bool_t ares_server_in_channel(const ares_channel_t *channel, ++ const ares_server_t *server) ++{ ++ ares_slist_node_t *node; ++ ++ if (server == NULL) { ++ return ARES_FALSE; ++ } ++ ++ for (node = ares_slist_node_first(channel->servers); node != NULL; ++ node = ares_slist_node_next(node)) { ++ if (ares_slist_node_val(node) == server) { ++ return ARES_TRUE; ++ } ++ } ++ ++ return ARES_FALSE; ++} ++ + /* Drain the deferred requeue/endqueue list iteratively. All flush sites + * (read_answers(), process_timeouts(), and ares_send_query()) funnel through + * here so that: +@@ -636,11 +755,35 @@ static ares_status_t ares_flush_requeue(ares_channel_t *channel, + + query = ares_htable_szvp_get_direct(channel->queries_by_qid, entry.qid); + ++ /* A query id is a 16-bit wire value and generate_unique_qid() only avoids ++ * ids that are live when it runs. Within a single drain, a callback can ++ * cancel a query whose entry is still in this array -- releasing its id -- ++ * and start a new lookup that is handed the same id. The lookup above ++ * would then resolve this entry to a completely different query: an ++ * endquery entry would deliver one lookup's answer to another and free it, ++ * a requeue entry would put an in-flight query on the wire twice. The ++ * serial is a channel-wide counter that is never reused, so it tells the ++ * two apart. */ ++ if (query != NULL && query->serial != entry.serial) { ++ query = NULL; ++ } ++ + if (entry.type == REQUEUE_REQUEUE) { + /* Query disappeared (e.g. a prior callback in this drain cancelled it) */ + if (query == NULL) { + continue; + } ++ /* entry.server is a raw pointer captured before any callback in this ++ * drain ran. A callback may legally re-enter the public API -- the ++ * channel lock is recursive -- and ares_set_servers*() destroys servers ++ * that are absent from the new configuration, so it may already be ++ * freed. The query above is revalidated by qid for exactly this ++ * reason; do the same for the server, falling back to ordinary ++ * selection when it has gone. */ ++ if (!ares_server_in_channel(channel, entry.server)) { ++ entry.server = NULL; ++ } ++ + /* Re-dispatch via the internal entrypoint so any further requeues are + * appended back onto this same list and drained by the loop above, + * rather than recursing. */ +@@ -1377,6 +1520,7 @@ ares_status_t ares_send_query(ares_server_t *requested_server, + ares_channel_t *channel = query->channel; + ares_array_t *requeue = NULL; + unsigned short qid = query->qid; ++ size_t serial = query->serial; + ares_status_t status; + + status = ares_send_query_int(requested_server, query, now, &requeue); +@@ -1393,8 +1537,7 @@ ares_status_t ares_send_query(ares_server_t *requested_server, + * been freed. Do not dereference 'query' here. If it is no longer tracked + * it ended, so don't report success to the caller (which would, e.g., cause + * ares_send_nolock() to write to a now-freed *qid). */ +- if (status == ARES_SUCCESS && +- ares_htable_szvp_get_direct(channel->queries_by_qid, qid) == NULL) { ++ if (status == ARES_SUCCESS && !ares_query_is_live(channel, qid, serial)) { + status = ARES_ETIMEOUT; + } + +@@ -1412,6 +1555,9 @@ static ares_status_t ares_send_query_int(ares_server_t *requested_server, + size_t timeplus; + ares_status_t status; + ares_bool_t probe_downed_server = ARES_TRUE; ++ /* Captured before any application callback below, which may free the query */ ++ unsigned short qid = query->qid; ++ size_t serial = query->serial; + + /* Choose the server to send the query to */ + if (requested_server != NULL) { +@@ -1443,6 +1589,32 @@ static ares_status_t ares_send_query_int(ares_server_t *requested_server, + conn = ares_fetch_connection(channel, server, query); + if (conn == NULL) { + status = ares_open_connection(&conn, channel, server, query->using_tcp); ++ ++ /* Opening a connection runs application code -- the socket-open function ++ * from ares_set_socket_functions_ex(), and the ares_set_socket_callback() ++ * and ares_set_socket_configure_callback() callbacks -- any of which may ++ * reentrantly call ares_cancel() and free this query. Re-check liveness ++ * by the qid captured at entry rather than dereferencing a possibly-freed ++ * query. */ ++ if (!ares_query_is_live(channel, qid, serial)) { ++ return ARES_ECANCELLED; ++ } ++ ++ /* Those same callbacks may have reconfigured the servers, which frees the ++ * ones the new configuration omits. 'server' was captured before the ++ * open; everything below reads it, including server_increment_failures() ++ * on the failure paths of the switch. ++ * ++ * Requeue rather than return: every exit from this function must either ++ * enqueue the query or end it, and the query is still live here -- it has ++ * not been written, is not attached to a connection, and on a first send ++ * has no timeout node, so returning would strand it forever. ++ * query->conn is NULL, so this does not read through the freed server. */ ++ if (!ares_server_in_channel(channel, server)) { ++ return ares_requeue_query(query, now, ARES_ECONNREFUSED, ARES_TRUE, NULL, ++ requeue); ++ } ++ + switch (status) { + /* Good result, continue on */ + case ARES_SUCCESS: +@@ -1464,6 +1636,14 @@ static ares_status_t ares_send_query_int(ares_server_t *requested_server, + + /* Write the query */ + status = ares_conn_query_write(conn, query, now); ++ ++ /* Writing runs application code too: ares_conn_query_write() reaches the ++ * send function installed by ares_set_socket_functions_ex(), which may ++ * reentrantly call ares_cancel() with the same hazard as above. */ ++ if (!ares_query_is_live(channel, qid, serial)) { ++ return ARES_ECANCELLED; ++ } ++ + switch (status) { + /* Good result, continue on */ + case ARES_SUCCESS: +@@ -1479,6 +1659,17 @@ static ares_status_t ares_send_query_int(ares_server_t *requested_server, + case ARES_ECONNREFUSED: + case ARES_EBADFAMILY: + handle_conn_error(conn, ARES_TRUE, status); ++ /* handle_conn_error() -> ares_close_connection() -> ++ * ares_requeue_queries() walks conn->queries_to_conn and may terminally ++ * end other queries on this connection. If one of their callbacks ++ * reentrantly calls ares_cancel(), this query is freed too -- it is still ++ * linked in all_queries, since it only joins conn->queries_to_conn on the ++ * success path below. Re-check liveness by the qid captured at entry ++ * rather than dereferencing a possibly-freed query. Mirrors the guard in ++ * ares_send_query(). */ ++ if (!ares_query_is_live(channel, qid, serial)) { ++ return ARES_ECONNREFUSED; ++ } + status = ares_requeue_query(query, now, status, ARES_TRUE, NULL, requeue); + if (status == ARES_ETIMEOUT) { + status = ARES_ECONNREFUSED; +@@ -1597,11 +1788,18 @@ static ares_bool_t same_questions(const ares_query_t *query, + return rv; + } + +-static void ares_detach_query(ares_query_t *query) ++void ares_detach_query(ares_query_t *query) + { + /* Remove the query from all the lists in which it is linked */ + ares_query_remove_from_conn(query); +- ares_htable_szvp_remove(query->channel->queries_by_qid, query->qid); ++ /* Only release the qid slot if it is still ours. A query detached before ++ * its callback can have its qid reissued to a lookup started from within ++ * that callback, and the ares_free_query() that follows detaches a second ++ * time -- which would otherwise evict the new lookup instead. */ ++ if (ares_htable_szvp_get_direct(query->channel->queries_by_qid, query->qid) == ++ query) { ++ ares_htable_szvp_remove(query->channel->queries_by_qid, query->qid); ++ } + ares_llist_node_destroy(query->node_all_queries); + query->node_all_queries = NULL; + } +@@ -1624,6 +1822,16 @@ static void end_query(ares_channel_t *channel, ares_server_t *server, + return; + } + ++ /* Detach from all lookup lists before the callback, otherwise a reentrant ++ * ares_cancel() from within it would find this query still linked, free it, ++ * and the ares_free_query() below would double-free. Mirrors ++ * ares_flush_requeue() for every synchronous path that reaches here with a ++ * NULL requeue: connection teardown via handle_conn_error(), and server ++ * removal via ares_servers_update() and ares_destroy_servers_state(), both ++ * of which reach ares_close_sockets(). ++ * ares_detach_query() may safely run twice on the same query. */ ++ ares_detach_query(query); ++ + /* Invoke the callback. */ + query->callback(query->arg, status, query->timeouts, dnsrec); + ares_free_query(query); +@@ -1631,7 +1839,8 @@ static void end_query(ares_channel_t *channel, ares_server_t *server, + /* Check and notify if no other queries are enqueued on the channel. This + * must come after the callback and freeing the query for 2 reasons. + * 1) The callback itself may enqueue a new query +- * 2) Technically the current query isn't detached until it is free()'d. ++ * 2) The current query is detached above, before the callback, so by this ++ * point it is no longer tracked. + */ + ares_queue_notify_empty(channel); + } +diff --git a/src/lib/ares_send.c b/src/lib/ares_send.c +index 6efa9580b2..2e0482bf19 100644 +--- a/src/lib/ares_send.c ++++ b/src/lib/ares_send.c +@@ -117,6 +117,27 @@ ares_status_t ares_send_nolock(ares_channel_t *channel, ares_server_t *server, + unsigned short id = generate_unique_qid(channel); + const ares_dns_record_t *dnsrec_resp = NULL; + ++ /* A channel being destroyed accepts no new queries. ares_destroy(3) ++ * documents that a callback must not add new requests to a channel being ++ * destroyed. Refuse it, but still notify: ARES_EDESTRUCTION is documented ++ * in ares_query(3), ares_send(3), ares_search(3) and ares_getaddrinfo(3) as ++ * a status delivered to the callback, and every caller frees its own ++ * context from there. ++ * ++ * Not notified if we are already inside such a notification: an application ++ * whose callback starts a replacement lookup on every invocation would ++ * otherwise re-enter here from within the callback without bound and ++ * exhaust the stack. At that depth it is already past the contract ++ * ares_destroy(3) documents. */ ++ if (!channel->sys_up) { ++ if (!channel->in_shutdown_refusal) { ++ channel->in_shutdown_refusal = ARES_TRUE; ++ callback(arg, ARES_EDESTRUCTION, 0, NULL); ++ channel->in_shutdown_refusal = ARES_FALSE; ++ } ++ return ARES_EDESTRUCTION; ++ } ++ + ares_tvnow(&now); + + if (ares_slist_len(channel->servers) == 0) { +@@ -145,6 +166,7 @@ ares_status_t ares_send_nolock(ares_channel_t *channel, ares_server_t *server, + + query->channel = channel; + query->qid = id; ++ query->serial = ++channel->query_serial; + query->timeout.sec = 0; + query->timeout.usec = 0; + query->using_tcp = +@@ -209,6 +231,12 @@ ares_status_t ares_send_nolock(ares_channel_t *channel, ares_server_t *server, + */ + if (!ares_htable_szvp_insert(channel->queries_by_qid, query->qid, query)) { + /* LCOV_EXCL_START: OutOfMemory */ ++ /* The query is already chained into all_queries, so a reentrant ++ * ares_cancel() from the callback below would find it, run the ++ * application callback a second time for one lookup, and free it -- and ++ * the ares_free_query() here would then double-free. Same ordering as ++ * ares_cancel() and ares_destroy(): unlink first, notify second. */ ++ ares_detach_query(query); + callback(arg, ARES_ENOMEM, 0, NULL); + ares_free_query(query); + return ARES_ENOMEM; +@@ -217,10 +245,17 @@ ares_status_t ares_send_nolock(ares_channel_t *channel, ares_server_t *server, + + /* Perform the first query action. */ + ++ /* Sending opens and writes to connections and can mark a server down, both ++ * of which notify. Deliver those once we are done with the connection and ++ * the server, not part-way through. */ ++ ares_deferred_cb_enter(channel); + status = ares_send_query(server, query, &now); ++ ares_deferred_cb_leave(channel); ++ + if (status == ARES_SUCCESS && qid) { + *qid = id; + } ++ + return status; + } + +diff --git a/src/lib/ares_update_servers.c b/src/lib/ares_update_servers.c +index f8d441accf..50a9c27925 100644 +--- a/src/lib/ares_update_servers.c ++++ b/src/lib/ares_update_servers.c +@@ -740,17 +740,36 @@ static ares_bool_t ares_servers_remove_stale(ares_channel_t *channel, + { + ares_bool_t stale_removed = ARES_FALSE; + ares_slist_node_t *snode = ares_slist_node_first(channel->servers); ++ size_t generation = channel->servers_generation; + ++ /* Re-read from the head each pass rather than caching the next node. ++ * ares_slist_node_destroy() below reaches application callbacks, by way of ++ * ares_destroy_server() -> ares_close_sockets() -> ares_requeue_queries(), ++ * and a nested reconfiguration from one of those destroys the remaining ++ * stale servers -- including whichever one a cached pointer referred to. */ + while (snode != NULL) { +- ares_slist_node_t *snext = ares_slist_node_next(snode); + const ares_server_t *server = ares_slist_node_val(snode); +- if (!ares_server_in_newconfig(server, srvlist)) { +- /* This will clean up all server state via the destruction callback and +- * move any queries to new servers */ +- ares_slist_node_destroy(snode); +- stale_removed = ARES_TRUE; ++ ++ if (ares_server_in_newconfig(server, srvlist)) { ++ snode = ares_slist_node_next(snode); ++ continue; + } +- snode = snext; ++ ++ /* This will clean up all server state via the destruction callback and ++ * move any queries to new servers */ ++ ares_slist_node_destroy(snode); ++ stale_removed = ARES_TRUE; ++ ++ /* A callback reached from the destroy above may have reconfigured the ++ * server list against a newer configuration. That call has already ++ * reconciled the whole list, so whatever remains belongs to it and not to ++ * srvlist -- stop rather than deleting its servers. */ ++ if (channel->servers_generation != generation) { ++ break; ++ } ++ ++ /* The callbacks above may have removed further servers, so restart. */ ++ snode = ares_slist_node_first(channel->servers); + } + return stale_removed; + } +@@ -775,6 +794,14 @@ ares_status_t ares_servers_update(ares_channel_t *channel, + return ARES_EFORMERR; /* LCOV_EXCL_LINE: DefensiveCoding */ + } + ++ /* Removing servers closes their connections, which notifies. Deliver ++ * once this call is done rather than from inside the teardown. */ ++ ares_deferred_cb_enter(channel); ++ ++ /* Bumped before anything below can reach an application callback, so a ++ * nested reconfiguration is distinguishable from our own work. */ ++ channel->servers_generation++; ++ + /* NOTE: a NULL or zero entry server list is considered valid due to + * real-world people needing support for this for their test harnesses + */ +@@ -843,6 +870,7 @@ ares_status_t ares_servers_update(ares_channel_t *channel, + status = ARES_SUCCESS; + + done: ++ ares_deferred_cb_leave(channel); + return status; + } + +diff --git a/test/ares-test-mock.cc b/test/ares-test-mock.cc +index c0c0279abb..2158c5a913 100644 +--- a/test/ares-test-mock.cc ++++ b/test/ares-test-mock.cc +@@ -31,6 +31,11 @@ + #include <sys/stat.h> + #endif + ++#ifndef _WIN32 ++# include <errno.h> ++# include <fcntl.h> ++#endif ++ + #include <sstream> + #include <vector> + +@@ -1914,6 +1919,7 @@ TEST_P(MockRetryDepthChannelTest, HighRetryNoStackOverflow) { + struct CancelInCbData { + ares_channel_t *channel; + bool done; ++ size_t cbs; + }; + + static void CancelChannelCallback(void *arg, ares_status_t status, +@@ -1925,10 +1931,1071 @@ static void CancelChannelCallback(void *arg, ares_status_t status, + (void)timeouts; + (void)dnsrec; + data->done = true; ++ data->cbs++; + /* Reentrant cancel from within the callback. Must not double-free. */ + ares_cancel(data->channel); + } + ++// ares_destroy() notifies each still-running query that the channel is going ++// away, and holds a pointer to the next one in its list while doing so. A ++// reentrant ares_cancel() from one of those callbacks frees every remaining ++// query -- including that cached next, which ares_destroy() then follows. ++// ++// ares_cancel() is immune to the same pattern because it swaps the list head ++// before walking it; ares_destroy() did not. Two queries are required: with ++// only one in flight there is no next to cache and nothing goes wrong. ++// tries=1 so a teardown ends the query outright instead of retrying it, which ++// is the precondition this family of bugs shares. ++class MockUDPSingleTryTest : public MockChannelOptsTest, ++ public ::testing::WithParamInterface<int> { ++public: ++ MockUDPSingleTryTest() ++ : MockChannelOptsTest(1, GetParam(), false, false, FillOptions(&opts_), ++ ARES_OPT_TRIES) ++ { ++ } ++ ++ static struct ares_options *FillOptions(struct ares_options *opts) ++ { ++ memset(opts, 0, sizeof(struct ares_options)); ++ opts->tries = 1; ++ return opts; ++ } ++ ++private: ++ struct ares_options opts_; ++}; ++ ++// ares_cancel() and ares_destroy() free a query themselves rather than going ++// through end_query(). Both used to invoke the application callback while the ++// query was still in queries_by_qid, on conn->queries_to_conn and in ++// queries_by_timeout -- only all_queries was unlinked first. A reentrant ++// teardown from within that callback therefore still found the query, ended it ++// a second time, and freed it; the ares_free_query() that followed then ++// double-freed. One query in flight is enough. ++static void ReconfigureServersCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ CancelInCbData *data = static_cast<CancelInCbData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ data->done = true; ++ data->cbs++; ++ /* Frees every server absent from the new configuration, closing their ++ * connections -- which still hold the queries being torn down. */ ++ ares_set_servers_csv(data->channel, "127.0.0.1:53055"); ++} ++ ++// Reconfiguring the server list from a cancellation callback. ++TEST_P(MockUDPSingleTryTest, ReconfigureInCancelCallbackNoDoubleFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ QueryResult result; ++ /* Left unanswered, so both stay attached to the connection. */ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, ReconfigureServersCallback, ++ &data, NULL)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, QueryCallback, &result, NULL)); ++ ++ ares_cancel(channel_); ++ EXPECT_TRUE(data.done); ++ EXPECT_TRUE(result.done_); ++ /* Each lookup must be notified exactly once, not twice. */ ++ EXPECT_EQ((size_t)1, data.cbs); ++} ++ ++/* The replacement lookup's result outlives the callback that starts it. */ ++static QueryResult g_replacement_result; ++ ++static void NewQueryOnFailingWriteCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ /* ares_set_socket_functions() stores the pointer rather than copying it, so ++ * this must outlive the call. */ ++ static ares_socket_functions sock_funcs; ++ CancelInCbData *data = static_cast<CancelInCbData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ ++ data->cbs++; ++ ++ /* Only the first cancellation callback starts a replacement lookup. */ ++ if (data->done) { ++ return; ++ } ++ data->done = true; ++ ++ memset(&sock_funcs, 0, sizeof(sock_funcs)); ++ sock_funcs.asendv = LibraryTest::ares_sendv_fail; ++ ares_set_socket_functions(data->channel, &sock_funcs, NULL); ++ LibraryTest::SetFailSend(); ++ ++ ares_query_dnsrec(data->channel, "www.probe.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, QueryCallback, &g_replacement_result, ++ NULL); ++} ++ ++// No reconfiguration at all: the cancellation callback simply starts a ++// replacement lookup -- an ordinary retry pattern -- and that lookup's write ++// fails. ares_close_connection() then walks conn->queries_to_conn, which ++// still holds the queries ares_cancel() is in the middle of. Wider than the ++// case above: it needs nothing more than a connection error. ++TEST_P(MockUDPSingleTryTest, NewQueryInCancelCallbackConnErrorNoDoubleFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ g_replacement_result.done_ = false; ++ ++ QueryResult result; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, NewQueryOnFailingWriteCallback, ++ &data, NULL)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, QueryCallback, &result, NULL)); ++ ++ ares_cancel(channel_); ++ EXPECT_TRUE(data.done); ++ EXPECT_TRUE(result.done_); ++ /* Each lookup must be notified exactly once, not twice. */ ++ EXPECT_EQ((size_t)1, data.cbs); ++ /* Pins that the replacement lookup ran and its write failed, which is what ++ * drives the connection teardown this test is about. */ ++ EXPECT_TRUE(g_replacement_result.done_); ++} ++ ++// The same shape through ares_destroy() rather than ares_cancel(). Swapping ++// the all_queries head does not help here: the reentrant teardown reaches the ++// query through the connection, not through all_queries. ++TEST_P(MockUDPSingleTryTest, ReconfigureInDestroyCallbackNoDoubleFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ QueryResult result; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, ReconfigureServersCallback, ++ &data, NULL)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, QueryCallback, &result, NULL)); ++ ++ ares_destroy(channel_); ++ channel_ = nullptr; /* destroyed here, don't let the fixture repeat it */ ++ ++ EXPECT_TRUE(data.done); ++ EXPECT_TRUE(result.done_); ++ /* Each lookup must be notified exactly once, not twice. */ ++ EXPECT_EQ((size_t)1, data.cbs); ++} ++ ++// Once ares_destroy() has begun, a lookup started from a destruction callback ++// is refused rather than allocated into a channel being torn down -- and the ++// refusal still runs the caller's callback with ARES_EDESTRUCTION, so context ++// attached to `arg` can be reclaimed. ares_cancel() leaves sys_up true, so a ++// destruction callback is the only way to reach this. ++struct DestroyReentryData { ++ ares_channel_t *channel; ++ size_t outer_cbs; ++ size_t replacement_cbs; ++ ares_status_t replacement_status; ++}; ++ ++static void DestroyReplacementCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ DestroyReentryData *data = static_cast<DestroyReentryData *>(arg); ++ (void)timeouts; ++ (void)dnsrec; ++ data->replacement_cbs++; ++ data->replacement_status = status; ++} ++ ++static void DestroyReentryCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ DestroyReentryData *data = static_cast<DestroyReentryData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ data->outer_cbs++; ++ ares_query_dnsrec(data->channel, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, DestroyReplacementCallback, data, NULL); ++} ++ ++TEST_F(LibraryTest, NewQueryInDestroyCallbackIsRefusedAndNotified) { ++ ares_channel_t *channel = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS; ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts, optmask)); ++ // Nothing listens here; no traffic is needed. ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel, "127.0.0.1:53055")); ++ ++ DestroyReentryData data; ++ data.channel = channel; ++ data.outer_cbs = 0; ++ data.replacement_cbs = 0; ++ data.replacement_status = ARES_SUCCESS; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, DestroyReentryCallback, &data, ++ NULL)); ++ ++ ares_destroy(channel); ++ ++ EXPECT_EQ((size_t)1, data.outer_cbs); ++ // Refused -- but notified, so the caller can release whatever it attached. ++ EXPECT_EQ((size_t)1, data.replacement_cbs); ++ EXPECT_EQ(ARES_EDESTRUCTION, data.replacement_status); ++} ++ ++INSTANTIATE_TEST_SUITE_P(AddressFamilies, MockUDPSingleTryTest, ++ ::testing::ValuesIn(ares::test::families), ++ PrintFamily); ++ ++TEST_P(MockUDPChannelTest, CancelInDestroyCallbackNoUseAfterFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ /* Left unanswered, so both are still pending when the channel goes down. */ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, CancelChannelCallback, &data, ++ NULL)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, CancelChannelCallback, &data, ++ NULL)); ++ /* Without this the test can pass vacuously if a query failed to start. */ ++ EXPECT_EQ(2, ares_queue_active_queries(channel_)); ++ ++ ares_destroy(channel_); ++ channel_ = nullptr; /* destroyed here, don't let the fixture repeat it */ ++ ++ EXPECT_TRUE(data.done); ++ /* Every query must be notified exactly once. */ ++ EXPECT_EQ((size_t)2, data.cbs); ++} ++ ++// Fixture with tries=1 so the first failure ends the query outright instead of ++// retrying it -- that is what drives execution into end_query()'s synchronous ++// (requeue==NULL) branch. ++// The tests below use the MockUDPSingleTryTest fixture defined above: tries=1, ++// so the first failure ends the query outright instead of retrying it, which ++// is what drives execution into end_query()'s synchronous (requeue==NULL) ++// branch. ++// ++ ++// Connection-teardown route into the synchronous branch. A malformed reply ++// makes process_answer() return EBADRESP, so handle_conn_error() tears the ++// connection down and ares_close_connection() requeues its queries with a NULL ++// requeue; with tries=1 the query is ended right there. CVE-2026-33630 fixed ++// the deferred flush path only, so this one still double-freed. ++TEST_P(MockUDPSingleTryTest, CancelInCallbackConnTeardownNoDoubleFree) { ++ /* Too short to be a DNS header, so it fails to parse. */ ++ std::vector<byte> malformed = {0x00, 0x00, 0x00, 0x00}; ++ ON_CALL(server_, OnRequest("www.google.com", T_A)) ++ .WillByDefault(SetReplyData(&server_, malformed)); ++ ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ CancelChannelCallback, &data, NULL); ++ Process(); ++ EXPECT_TRUE(data.done); ++} ++ ++// Server-removal route into the same synchronous branch, which the ++// connection-teardown test above does not cover. Reconfiguring the server list ++// destroys the servers that are gone, and ares_close_sockets() closes their ++// connections with a NULL requeue -- a second way to reach end_query() with the ++// query still linked. No Process() call is needed: the query is attached to a ++// connection by ares_query_dnsrec() itself, and nothing has replied to it. ++TEST_P(MockUDPSingleTryTest, CancelInCallbackServerRemovalNoDoubleFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ CancelChannelCallback, &data, NULL); ++ ++ /* Drops the server the in-flight query is attached to. */ ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel_, "127.0.0.1:53055")); ++ EXPECT_TRUE(data.done); ++} ++ ++// ares_send_query_int() calls handle_conn_error() on a retryable write failure ++// and then keeps using its own query. That teardown requeues the *other* ++// queries on the connection, and with tries=1 they are ended outright; if one ++// of their callbacks reentrantly cancels the channel, this query is freed too ++// -- it is still linked in all_queries, having only joined ++// conn->queries_to_conn on the success path further down. Using it afterwards ++// is a use-after-free. ++// ++// The first query is left unanswered so it stays on the connection, then the ++// next write is made to fail with ECONNREFUSED so the second query takes the ++// error path while the first is still attached. ++// ++// Note this is masked on code that has not fixed the end_query() double-free: ++// that aborts inside handle_conn_error() before control returns here. ++TEST_P(MockUDPSingleTryTest, CancelInSiblingCallbackConnErrorNoUseAfterFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ /* No reply configured for this one, so it stays attached to the connection. */ ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ CancelChannelCallback, &data, NULL); ++ ++ ares_socket_functions sock_funcs; ++ memset(&sock_funcs, 0, sizeof(sock_funcs)); ++ sock_funcs.asendv = ares_sendv_fail; ++ ares_set_socket_functions(channel_, &sock_funcs, NULL); ++ LibraryTest::SetFailSend(); ++ ++ QueryResult result; ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, &result, NULL); ++ ++ /* The write failure above tears the connection down, which ends the first ++ * query, whose callback cancels the channel and frees this one. */ ++ EXPECT_TRUE(data.done); ++ /* The sibling is cancelled from the first query's callback rather than ++ * requeued; pin that rather than leaving it unstated. */ ++ EXPECT_TRUE(result.done_); ++ EXPECT_EQ(ARES_ECANCELLED, result.status_); ++} ++ ++// The socket-state callback -- registered with ARES_OPT_SOCK_STATE_CB, the ++// usual way c-ares is driven from an event loop -- used to be dispatched from ++// the middle of opening a connection, and ares_open_connection() keeps using ++// that connection afterwards. A reentrant ares_cancel() from the callback ++// frees both the connection (through the idle sweep) and the in-flight query, ++// leaving the library using either. ++struct SockStateCancelData { ++ ares_channel_t *channel; ++ size_t calls; ++}; ++ ++static void SockStateCancelCallback(void *data, ares_socket_t socket_fd, ++ int readable, int writable) ++{ ++ SockStateCancelData *d = static_cast<SockStateCancelData *>(data); ++ (void)socket_fd; ++ (void)readable; ++ (void)writable; ++ d->calls++; ++ // Reentrant cancel from the socket-state notification. ++ if (d->channel != nullptr) { ++ ares_cancel(d->channel); ++ } ++} ++ ++TEST_F(LibraryTest, CancelInSockStateCallbackNoUseAfterFree) { ++ SockStateCancelData data; ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ++ data.channel = nullptr; ++ data.calls = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateCancelCallback; ++ opts.sock_state_cb_data = &data; ++ optmask = ARES_OPT_SOCK_STATE_CB; ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&chan, &opts, optmask)); ++ ASSERT_NE(nullptr, chan); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan, "127.0.0.1:53055")); ++ ++ /* Armed only now, so channel setup itself does not cancel. */ ++ data.channel = chan; ++ ++ QueryResult result; ++ ares_query_dnsrec(chan, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, &result, NULL); ++ ++ /* Opening the connection notifies, the notification cancels, and the ++ * library must still be able to finish -- and the lookup must actually be ++ * cancelled rather than the cancel quietly doing nothing. */ ++ EXPECT_GT(data.calls, (size_t)0); ++ EXPECT_TRUE(result.done_); ++ EXPECT_EQ(ARES_ECANCELLED, result.status_); ++ ++ data.channel = nullptr; /* don't cancel from teardown notifications */ ++ ares_destroy(chan); ++} ++ ++// Deferring the notification must not reorder it past the close of the ++// descriptor it names. An event loop is told "stop watching fd N"; if fd N is ++// already closed by then, the number can have been handed to something else -- ++// including to application code running from a deferred query callback -- and ++// the application ends up setting and clearing interest in a descriptor it now ++// owns for another purpose. ++// ++// The invariant is checked directly: while the callback naming the descriptor ++// runs, that descriptor must still be open. ++#ifndef _WIN32 ++struct SockStateCloseOrderData { ++ size_t closes_seen; ++ size_t closed_while_already_shut; ++}; ++ ++static void SockStateCloseOrderCallback(void *data, ares_socket_t socket_fd, ++ int readable, int writable) ++{ ++ SockStateCloseOrderData *d = static_cast<SockStateCloseOrderData *>(data); ++ if (readable || writable) { ++ return; ++ } ++ d->closes_seen++; ++ // F_GETFD on a closed descriptor fails with EBADF. c-ares must not have ++ // closed it yet at this point. ++ if (fcntl(static_cast<int>(socket_fd), F_GETFD) == -1 && errno == EBADF) { ++ d->closed_while_already_shut++; ++ } ++} ++ ++TEST_F(LibraryTest, SockStateCloseIsReportedBeforeDescriptorCloses) { ++ SockStateCloseOrderData data; ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ ++ data.closes_seen = 0; ++ data.closed_while_already_shut = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateCloseOrderCallback; ++ opts.sock_state_cb_data = &data; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_init_options(&chan, &opts, ARES_OPT_SOCK_STATE_CB)); ++ ASSERT_NE(nullptr, chan); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan, "127.0.0.1:53055")); ++ ++ QueryResult result; ++ ares_query_dnsrec(chan, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, &result, NULL); ++ ares_destroy(chan); ++ ++ EXPECT_GT(data.closes_seen, (size_t)0); ++ EXPECT_EQ((size_t)0, data.closed_while_already_shut) ++ << "the application was told to stop watching a descriptor c-ares had " ++ "already closed"; ++} ++#endif ++ ++// Reconfiguring the servers from an I/O hook frees the server and its ++// connection while the library is still holding both. Deferred reclamation is ++// what makes these safe: the objects are unlinked from every lookup structure ++// immediately, so nothing can find them, but their memory survives until the ++// outermost call unwinds. Without it these are heap-use-after-free in ++// ares_conn_connect() and handle_conn_error() respectively. ++// ++// This is the class behind CVE-2025-31498 and CVE-2025-62408 -- a connection ++// cleaned up while a caller further up the stack still held it. ++struct ReconfigureFromHookData { ++ ares_channel_t *channel; ++ bool done; ++ int status; ++}; ++ ++static int ReconfigureConfigureCallback(ares_socket_t fd, int type, void *data) ++{ ++ ReconfigureFromHookData *d = static_cast<ReconfigureFromHookData *>(data); ++ (void)fd; ++ (void)type; ++ // Frees the server this connection is being opened for. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056"); ++ return 0; ++} ++ ++static void ReconfigureFromHookCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) ++{ ++ ReconfigureFromHookData *d = static_cast<ReconfigureFromHookData *>(arg); ++ (void)timeouts; ++ (void)dnsrec; ++ d->done = true; ++ d->status = static_cast<int>(status); ++} ++ ++TEST_F(LibraryTest, ReconfigureInConfigureHookNoUseAfterFree) { ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ReconfigureFromHookData data; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS; ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&chan, &opts, optmask)); ++ ASSERT_NE(nullptr, chan); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan, "127.0.0.1:53055")); ++ ++ data.channel = chan; ++ data.done = false; ++ data.status = ARES_SUCCESS; ++ ares_set_socket_configure_callback(chan, ReconfigureConfigureCallback, &data); ++ ++ ares_query_dnsrec(chan, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ ReconfigureFromHookCallback, &data, NULL); ++ ++ // Must complete rather than be stranded -- every exit from the send path ++ // either enqueues the query or ends it. ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ((size_t)0, ares_queue_active_queries(chan)); ++ ares_destroy(chan); ++} ++ ++// The second route to the same freed connection, and the one that drove the ++// change from suppressing the idle sweep to deferring the notification: ++// reconfiguring the servers from the socket-state callback destroys the server ++// and with it the connection ares_open_connection() is still using. Nothing ++// covered this route, so a regression in it would have gone unnoticed. ++struct SockStateReconfigureData { ++ ares_channel_t *channel; ++ size_t calls; ++}; ++ ++static void SockStateReconfigureCallback(void *data, ares_socket_t socket_fd, ++ int readable, int writable) ++{ ++ SockStateReconfigureData *d = ++ static_cast<SockStateReconfigureData *>(data); ++ (void)socket_fd; ++ (void)readable; ++ (void)writable; ++ d->calls++; ++ if (d->channel != nullptr) { ++ // Destroys the server, and with it the connection being opened. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056"); ++ } ++} ++ ++TEST_F(LibraryTest, ReconfigureInSockStateCallbackNoUseAfterFree) { ++ SockStateReconfigureData data; ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ ++ data.channel = nullptr; ++ data.calls = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateReconfigureCallback; ++ opts.sock_state_cb_data = &data; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_init_options(&chan, &opts, ARES_OPT_SOCK_STATE_CB)); ++ ASSERT_NE(nullptr, chan); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan, "127.0.0.1:53055")); ++ ++ /* Armed only now, so channel setup itself does not reconfigure. */ ++ data.channel = chan; ++ ++ QueryResult result; ++ ares_query_dnsrec(chan, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, &result, NULL); ++ ++ EXPECT_GT(data.calls, (size_t)0); ++ ++ data.channel = nullptr; /* don't reconfigure from teardown notifications */ ++ ares_destroy(chan); ++} ++ ++// Deferring the notifications must not lose them: every public entry point ++// that closes a socket still has to tell the application about it before it ++// returns, or an event loop is left polling a descriptor that c-ares closed. ++struct SockStateRecordData { ++ std::vector<std::pair<ares_socket_t, bool>> events; /* fd, still-armed */ ++}; ++ ++static void SockStateRecordCallback(void *data, ares_socket_t socket_fd, ++ int readable, int writable) ++{ ++ SockStateRecordData *d = static_cast<SockStateRecordData *>(data); ++ d->events.push_back(std::make_pair(socket_fd, readable || writable)); ++} ++ ++static bool SawClose(const SockStateRecordData &d) ++{ ++ for (size_t i = 0; i < d.events.size(); i++) { ++ if (!d.events[i].second) { ++ return true; ++ } ++ } ++ return false; ++} ++ ++class SockStateDeliveryTest : public LibraryTest { ++protected: ++ void OpenConnection(ares_channel_t **chan, SockStateRecordData *data, ++ QueryResult *result) ++ { ++ struct ares_options opts; ++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateRecordCallback; ++ opts.sock_state_cb_data = data; ++ ++ ASSERT_EQ(ARES_SUCCESS, ++ ares_init_options(chan, &opts, ARES_OPT_SOCK_STATE_CB)); ++ ASSERT_NE(nullptr, *chan); ++ /* TEST-NET-1, guaranteed not to answer, so the socket is still open when ++ * the call under test runs. */ ++ ASSERT_EQ(ARES_SUCCESS, ares_set_servers_csv(*chan, "192.0.2.1:53")); ++ ++ ares_query_dnsrec(*chan, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, result, NULL); ++ ASSERT_FALSE(result->done_); ++ ASSERT_FALSE(data->events.empty()); ++ ASSERT_TRUE(data->events.back().second); /* opened, armed for read */ ++ data->events.clear(); ++ } ++}; ++ ++TEST_F(SockStateDeliveryTest, CancelReportsSocketClose) { ++ ares_channel_t *chan = nullptr; ++ SockStateRecordData data; ++ QueryResult result; ++ ++ OpenConnection(&chan, &data, &result); ++ ares_cancel(chan); ++ EXPECT_TRUE(SawClose(data)) << "ares_cancel() closed the socket without " ++ "telling the application"; ++ ares_destroy(chan); ++} ++ ++TEST_F(SockStateDeliveryTest, SetServersReportsSocketClose) { ++ ares_channel_t *chan = nullptr; ++ SockStateRecordData data; ++ QueryResult result; ++ ++ OpenConnection(&chan, &data, &result); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan, "198.51.100.1:53")); ++ EXPECT_TRUE(SawClose(data)) << "ares_set_servers_csv() closed the socket " ++ "without telling the application"; ++ ares_destroy(chan); ++} ++ ++TEST_F(SockStateDeliveryTest, DestroyReportsSocketClose) { ++ ares_channel_t *chan = nullptr; ++ SockStateRecordData data; ++ QueryResult result; ++ ++ OpenConnection(&chan, &data, &result); ++ ares_destroy(chan); ++ EXPECT_TRUE(SawClose(data)) << "ares_destroy() closed the socket without " ++ "telling the application"; ++} ++ ++// The server-state callback is dispatched from the middle of processing, and ++// every caller keeps using its query afterwards. Nothing documents the ++// callback as non-reentrant, and the channel lock is recursive precisely so ++// API calls from callbacks are allowed -- so an application that cancels from ++// it left the library using a freed query. ++// ++// No hostile input is needed: an ordinary timeout reaches the same place. A ++// SERVFAIL is used here because it is deterministic in the harness. ++struct ServerStateCancelData { ++ ares_channel_t *channel; ++ int server_cb_calls; ++ bool query_done; ++}; ++ ++static void ServerStateCancelCallback(const char *server_string, ++ ares_bool_t success, int flags, ++ void *data) { ++ ServerStateCancelData *d = static_cast<ServerStateCancelData *>(data); ++ (void)server_string; ++ (void)success; ++ (void)flags; ++ d->server_cb_calls++; ++ // Reentrant cancel from the notification. ++ ares_cancel(d->channel); ++} ++ ++static void ServerStateQueryCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ ServerStateCancelData *d = static_cast<ServerStateCancelData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ d->query_done = true; ++} ++ ++// The advisory's headline claim is that no hostile input is needed, because an ++// ordinary timeout reaches the same place. The SERVFAIL test below does not ++// show that; this one does. ++class ServerStateTimeoutCancelTest ++ : public MockChannelOptsTest, ++ public ::testing::WithParamInterface<int> { ++ public: ++ ServerStateTimeoutCancelTest() ++ : MockChannelOptsTest(1, GetParam(), false, false, FillOptions(&opts_), ++ ARES_OPT_TIMEOUTMS | ARES_OPT_TRIES) {} ++ static struct ares_options *FillOptions(struct ares_options *opts) { ++ memset(opts, 0, sizeof(struct ares_options)); ++ opts->timeout = 100; ++ opts->tries = 3; ++ return opts; ++ } ++ ++ private: ++ struct ares_options opts_; ++}; ++ ++TEST_P(ServerStateTimeoutCancelTest, CancelInServerStateCallbackOnTimeout) { ++ ServerStateCancelData data; ++ data.channel = channel_; ++ data.server_cb_calls = 0; ++ data.query_done = false; ++ ++ ares_set_server_state_callback(channel_, ServerStateCancelCallback, &data); ++ ++ // No reply configured, so the query times out and the notification comes ++ // from process_timeouts() rather than from a response. ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, ServerStateQueryCallback, &data, ++ NULL)); ++ Process(); ++ ++ EXPECT_GT(data.server_cb_calls, 0); ++ EXPECT_TRUE(data.query_done); ++} ++ ++INSTANTIATE_TEST_SUITE_P(AddressFamilies, ServerStateTimeoutCancelTest, ++ ::testing::ValuesIn(ares::test::families), ++ PrintFamily); ++ ++// Unregistering with a NULL callback discards whatever is still queued -- the ++// documented behaviour of deferring delivery, and the only way an application ++// can opt back out once notifications are in flight. ++struct ServerStateUnregisterData { ++ ares_channel_t *channel; ++ size_t server_cb_calls; ++ bool query_done; ++}; ++ ++static void ServerStateUnregisterCallback(const char *server_string, ++ ares_bool_t success, ++ int flags, void *user_data) ++{ ++ ServerStateUnregisterData *d = ++ static_cast<ServerStateUnregisterData *>(user_data); ++ (void)server_string; ++ (void)success; ++ (void)flags; ++ d->server_cb_calls++; ++ // Clear ourselves from inside the first notification. Any notification ++ // still queued behind this one must be dropped rather than delivered. ++ ares_set_server_state_callback(d->channel, NULL, NULL); ++} ++ ++static void ServerStateUnregisterQueryCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) ++{ ++ ServerStateUnregisterData *d = ++ static_cast<ServerStateUnregisterData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ d->query_done = true; ++} ++ ++TEST_P(ServerStateTimeoutCancelTest, UnregisterInCallbackDiscardsQueued) { ++ ServerStateUnregisterData data; ++ data.channel = channel_; ++ data.server_cb_calls = 0; ++ data.query_done = false; ++ ++ ares_set_server_state_callback(channel_, ServerStateUnregisterCallback, ++ &data); ++ ++ // No reply configured: the query times out on every try, so more than one ++ // notification is queued before the flush point is reached. ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, ++ ServerStateUnregisterQueryCallback, &data, NULL)); ++ Process(); ++ ++ EXPECT_EQ((size_t)1, data.server_cb_calls); ++ EXPECT_TRUE(data.query_done); ++} ++ ++TEST_P(MockUDPChannelTest, CancelInServerStateCallbackNoUseAfterFree) { ++ DNSPacket servfail; ++ servfail.set_response().set_aa().set_rcode(SERVFAIL) ++ .add_question(new DNSQuestion("www.google.com", T_A)); ++ ON_CALL(server_, OnRequest("www.google.com", T_A)) ++ .WillByDefault(SetReply(&server_, &servfail)); ++ ++ ServerStateCancelData data; ++ data.channel = channel_; ++ data.server_cb_calls = 0; ++ data.query_done = false; ++ ++ ares_set_server_state_callback(channel_, ServerStateCancelCallback, &data); ++ ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ ServerStateQueryCallback, &data, NULL); ++ Process(); ++ ++ EXPECT_GT(data.server_cb_calls, 0); ++ EXPECT_TRUE(data.query_done); ++} ++ ++// The deferred requeue list records the target server as a raw pointer, and ++// the drain never revalidated it -- although it does revalidate the query, by ++// qid, for exactly the same reason. Application callbacks run from inside that ++// drain, and a callback may legally reconfigure the servers, which frees the ++// ones no longer in the list. A later entry in the same drain then dispatches ++// through a freed ares_server_t. ++// ++// Two replies must be read in one batch: an ordinary answer whose callback ++// reconfigures, and a FORMERR carrying no OPT record, which is what makes ++// process_answer() queue a retry *to the same server* (the EDNS fallback -- ++// the only site that records a server pointer at all). ++struct ReconfigureInCbData { ++ ares_channel_t *channel; ++ bool done; ++}; ++ ++static void ReconfigureChannelCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ ReconfigureInCbData *data = static_cast<ReconfigureInCbData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ data->done = true; ++ /* Frees every server absent from the new configuration. */ ++ ares_set_servers_csv(data->channel, "127.0.0.1:53055"); ++} ++ ++TEST_P(MockUDPChannelTest, ReconfigureInCallbackStaleRequeueServer) { ++ DNSPacket okrsp; ++ okrsp.set_response().set_aa() ++ .add_question(new DNSQuestion("www.google.com", T_A)) ++ .add_answer(new DNSARR("www.google.com", 100, {1, 2, 3, 4})); ++ ON_CALL(server_, OnRequest("www.google.com", T_A)) ++ .WillByDefault(SetReply(&server_, &okrsp)); ++ ++ /* FORMERR with no OPT record: the server looks like it does not understand ++ * EDNS, so the query is queued for retry to this same server. */ ++ DNSPacket formerr; ++ formerr.set_response().set_aa().set_rcode(FORMERR) ++ .add_question(new DNSQuestion("www.example.com", T_A)); ++ ON_CALL(server_, OnRequest("www.example.com", T_A)) ++ .WillByDefault(SetReply(&server_, &formerr)); ++ ++ ReconfigureInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ ++ QueryResult second; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ ReconfigureChannelCallback, &data, NULL); ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, &second, NULL); ++ ++ /* Both replies have to be consumed in a single pass so they share one ++ * deferred-requeue scope -- that is what puts the ENDQUERY entry whose ++ * callback reconfigures ahead of the REQUEUE entry holding a server ++ * pointer. The usual Process() loop lets c-ares read between the two, so ++ * they never share a batch; answer both first, then read once. */ ++ for (int i = 0; i < 4; i++) { ++ std::set<ares_socket_t> server_fds = fds(); ++ fd_set r; ++ struct timeval tv = {0, 20000}; ++ int nfds = 0; ++ FD_ZERO(&r); ++ for (ares_socket_t fd : server_fds) { ++ FD_SET(fd, &r); ++ if ((int)fd >= nfds) { ++ nfds = (int)fd + 1; ++ } ++ } ++ if (select(nfds, &r, nullptr, nullptr, &tv) <= 0) { ++ break; ++ } ++ for (ares_socket_t fd : server_fds) { ++ if (FD_ISSET(fd, &r)) { ++ ProcessFD(fd); ++ } ++ } ++ } ++ { ++ fd_set readers; ++ fd_set writers; ++ struct timeval tv = {0, 0}; ++ int nfds; ++ FD_ZERO(&readers); ++ FD_ZERO(&writers); ++ nfds = ares_fds(channel_, &readers, &writers); ++ select(nfds, &readers, &writers, nullptr, &tv); ++ ares_process(channel_, &readers, &writers); ++ } ++ ++ EXPECT_TRUE(data.done); ++ ++ /* With the fix the FORMERR query is correctly re-dispatched (to the newly ++ * configured, unreachable server) rather than going through a freed one, so ++ * it is still outstanding here. Cancel it so its callback runs while its ++ * result object is still in scope. */ ++ ares_cancel(channel_); ++ EXPECT_TRUE(second.done_); ++} ++ ++// ares_servers_remove_stale() destroys each stale server in turn, and that ++// reaches application callbacks (ares_destroy_server() -> ares_close_sockets() ++// -> ares_requeue_queries() -> end_query()). A callback that reconfigures the ++// servers again destroys the servers the outer loop has not visited yet, so a ++// cached "next" node points at freed memory. ++struct NestedReconfigureData { ++ ares_channel_t *channel; ++ bool nested; ++}; ++ ++static void NestedReconfigureCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ NestedReconfigureData *data = static_cast<NestedReconfigureData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ if (data->nested) { ++ return; ++ } ++ data->nested = true; ++ // Destroys the stale servers the outer removal loop has not reached yet. ++ ares_set_servers_csv(data->channel, ""); ++} ++ ++TEST_F(LibraryTest, NestedReconfigureInCallbackRemoveStale) { ++ ares_channel_t *channel = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ /* One try per server, so tearing them down exhausts the query's retries and ++ * runs its callback from inside the removal loop. */ ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS; ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts, optmask)); ++ /* Three servers, so the loop still has entries after destroying the first. ++ * Nothing listens on these ports; no traffic is needed. */ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_set_servers_csv(channel, "127.0.0.1:53101,127.0.0.1:53102," ++ "127.0.0.1:53103")); ++ ++ NestedReconfigureData data; ++ data.channel = channel; ++ data.nested = false; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, NestedReconfigureCallback, ++ &data, NULL)); ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel, "")); ++ EXPECT_TRUE(data.nested); ++ ++ ares_destroy(channel); ++} ++ ++// The same nesting, but the nested reconfiguration *installs* a server rather ++// than clearing the list. Restarting the removal walk from the head then ++// re-judges that server against the outer call's list, decides it is stale and ++// deletes it -- so ares_set_servers_csv() returns success and leaves the ++// channel with no servers at all. Memory-safe and still wrong. ++static void NestedReplaceCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ NestedReconfigureData *data = static_cast<NestedReconfigureData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ if (data->nested) { ++ return; ++ } ++ data->nested = true; ++ // Installs a server the outer loop has never seen. ++ ares_set_servers_csv(data->channel, "127.0.0.1:53201"); ++} ++ ++TEST_F(LibraryTest, NestedReconfigureInCallbackKeepsNestedServers) { ++ ares_channel_t *channel = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS; ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts, optmask)); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel, "127.0.0.1:53101")); ++ ++ NestedReconfigureData data; ++ data.channel = channel; ++ data.nested = false; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, NestedReplaceCallback, &data, ++ NULL)); ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel, "127.0.0.1:53301")); ++ EXPECT_TRUE(data.nested); ++ ++ // The nested call reconciled the whole list; what it installed must survive. ++ char *csv = ares_get_servers_csv(channel); ++ ASSERT_NE(nullptr, csv); ++ EXPECT_STREQ("127.0.0.1:53201", csv); ++ ares_free_string(csv); ++ ++ ares_destroy(channel); ++} ++ + TEST_P(MockUDPChannelTest, CancelInCallbackNoDoubleFree) { + DNSPacket reply; + reply.set_response().set_aa() +@@ -1940,6 +3007,7 @@ TEST_P(MockUDPChannelTest, CancelInCallbackNoDoubleFree) { + CancelInCbData data; + data.channel = channel_; + data.done = false; ++ data.cbs = 0; + ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, + CancelChannelCallback, &data, NULL); + Process();
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket-server-state-notifications.patch new file +393 -0
393 lines not on this page · raw message
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand-callbacks-their-own-copy.patch new file +1054 -0
4 lines not on this page · raw message
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-down-by-application.patch new file +1107 -0
1,107 lines not on this page · raw message
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-an-answer-on-the-right-connection.patch new file +575 -0
575 lines not on this page · raw message
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-channel-config-under-lock.patch new file +243 -0
243 lines not on this page · raw message
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-prefix-length.patch new file +364 -0
364 lines not on this page · raw message
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-UDP-answers-from-server-port.patch new file +178 -0
178 lines not on this page · raw message
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb +8 -0
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.43.0
Part of the diff is not shown here. View the raw message.
Hi Jason,
Thanks for submitting the patch.
Though the patches LGTM, I am seeing increased ptest failures.
Existing recipe ptest status:
[==========] 1093 tests from 41 test suites ran.
[ PASSED ] 1092 tests. [ FAILED ] 1 test, listed below: [ FAILED ] DefaultChannelTest.TimeoutValue
ptest status after applying your patch:
[==========] 1174 tests from 47 test suites ran.
[ PASSED ] 1170 tests. [ FAILED ] 4 tests, listed below: [ FAILED ] DefaultChannelTest.TimeoutValue [ FAILED ] SockStateDeliveryTest.CancelReportsSocketClose [ FAILED ] SockStateDeliveryTest.SetServersReportsSocketClose [ FAILED ] SockStateDeliveryTest.DestroyReportsSocketClose
Perhaps some other commits also need backporting?
Can please look into this?
cheers
Ankur
Quoted 6,622 lines · J. S. via lists.openembedded.org
Add the eight unique upstream commits that fix the security advisories addressed in c-ares 1.34.9.
Several advisories share commits, so patches are ordered by commit rather than duplicated per advisory; each patch lists the GHSA IDs it resolves.Assisted-by opencode (big-pickle)Signed-off-by Jason Schonberg <[email protected]>--- ...troy-detach-queries-before-callbacks.patch | 2603 +++++++++++++++++ ...ed-socket-server-state-notifications.patch | 393 +++ ...qcache-hand-callbacks-their-own-copy.patch | 1054 +++++++ ...connections-torn-down-by-application.patch | 1107 +++++++ ...pt-an-answer-on-the-right-connection.patch | 575 ++++ ...tions-read-channel-config-under-lock.patch | 243 ++ ...res_inet_pton-reject-a-prefix-length.patch | 364 +++ ...nt-drop-UDP-answers-from-server-port.patch | 178 ++ .../recipes-support/c-ares/c-ares_1.34.8.bb | 8 + 9 files changed, 6525 insertions(+) create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-detach-queries-before-callbacks.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket-server-state-notifications.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand-callbacks-their-own-copy.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-down-by-application.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-an-answer-on-the-right-connection.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-channel-config-under-lock.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-prefix-length.patch create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-UDP-answers-from-server-port.patchmeta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-detach-queries-before-callbacks.patch new file +2603
-0@@ -0,0 +1,2603 @@ +From 7f408deb128fbdcce78122322734ffaa01cad9ae Mon Sep 17 00:00:00 2001 +From: Brad House <[email protected]> +Date: Sun, 18 Oct 2026 18:15:41 -0400 +Subject: [PATCH] ares_cancel/ares_destroy: detach queries before running user + callbacks +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Backport of dd62bdfca52ba5c37e88a1469c0a4c5e4a568ccb to v1.34. See that +commit for the full description; it hardens reentrant teardown from +callbacks (ares_cancel(), ares_destroy(), server reconfiguration, and +socket/server state notifications). + +Conflicts were whitespace/alignment only (v1.34 predates the clang-format +reformat in #1238) and were reformatted to v1.34's style. + +Also carries the test portion of #1334 that applies to the tests this +introduces: dead-server tests use 127.0.0.1:53055/53056 instead of +127.0.0.55/56, which only routes on Linux. + +Signed-off-by: Brad House <[email protected]> +Upstream-Status: Backport [https://github.com/c-ares/c-ares/commit/7f408deb128fbdcce78122322734ffaa01cad9ae] +GHSA: GHSA-wpr3-8q3r-4q8c, GHSA-97vq-4mxv-7vhm, GHSA-mw23-pfg6-2x9r, GHSA-rf6g-ww74-97qh, GHSA-v33j-65h4-4ch8, GHSA-pj7p-v3cw-hq66 +--- + docs/ares_cancel.3 | 17 + + docs/ares_destroy.3 | 7 + + docs/ares_init_options.3 | 15 + + docs/ares_set_server_state_callback.3 | 31 +- + docs/ares_set_servers_csv.3 | 17 + + src/lib/ares_cancel.c | 25 +- + src/lib/ares_close_sockets.c | 82 +- + src/lib/ares_conn.c | 217 ++++- + src/lib/ares_conn.h | 37 +- + src/lib/ares_destroy.c | 85 +- + src/lib/ares_private.h | 110 ++- + src/lib/ares_process.c | 235 +++++- + src/lib/ares_send.c | 35 + + src/lib/ares_update_servers.c | 42 +- + test/ares-test-mock.cc | 1068 +++++++++++++++++++++++++ + 15 files changed, 1959 insertions(+), 64 deletions(-) + +diff --git a/docs/ares_cancel.3 b/docs/ares_cancel.3 +index 967a946b49..069f226c10 100644 +--- a/docs/ares_cancel.3 ++++ b/docs/ares_cancel.3 +@@ -21,6 +21,23 @@ These calls give the callbacks a chance to clean up any state which might have + been stored in their arguments. If such a callback invocation adds a new + request to the channel, that request will \fInot\fP be cancelled by the + current invocation of \fBares_cancel(3)\fP. ++.SH REENTRANCY ++A callback invoked by ++.BR ares_cancel (3) ++may call c-ares functions on the same channel. The channel lock is recursive, ++and each query is unlinked from the library's internal structures before its ++callback runs, so a reentrant call cannot encounter a query that is part-way ++through being torn down. ++ ++This includes starting new lookups, as described above, and reconfiguring the ++server list with ++.BR ares_set_servers_csv (3) ++and related functions. ++ ++.SH AVAILABILITY ++The reentrancy guarantee described above applies as of c-ares version 1.35.0, ++and c-ares version 1.34.9 on the 1.34 branch. ++ + .SH SEE ALSO + .BR ares_init (3) + .BR ares_destroy (3) +diff --git a/docs/ares_destroy.3 b/docs/ares_destroy.3 +index d1984b7cc9..0b6380e609 100644 +--- a/docs/ares_destroy.3 ++++ b/docs/ares_destroy.3 +@@ -20,6 +20,13 @@ by the channel. + channel, passing a status of \fIARES_EDESTRUCTION\fP. These calls give the + callbacks a chance to clean up any state which might have been stored in their + arguments. A callback must not add new requests to a channel being destroyed. ++Such a request is refused: the request is not started, and its callback is ++invoked with a status of \fIARES_EDESTRUCTION\fP so that any state attached to ++its argument can be released. A request made from within that notification is ++refused without a further notification, so that a callback which responds to ++failure by retrying cannot recurse without bound; its argument is not handed ++back. This applies as of c-ares version 1.35.0, and c-ares version 1.34.9 on ++the 1.34 branch. + + There is no ability to make this function thread-safe. No additional calls + using this channel may be made once this function is called. +diff --git a/docs/ares_init_options.3 b/docs/ares_init_options.3 +index 9b3b481535..e6f6ee8ba6 100644 +--- a/docs/ares_init_options.3 ++++ b/docs/ares_init_options.3 +@@ -231,6 +231,21 @@ will be passed the socket whose state has changed; + will be set to true if the socket should listen for read events, and + .I writable + will be set to true if the socket should listen for write events. ++ ++This callback may call c-ares functions on the same channel, including ++.BR ares_cancel (3) ++and the server configuration functions. Notifications are delivered once the ++library has finished the work that produced them rather than part-way through ++it, so a reentrant call cannot close a socket the library is still using. They ++arrive in the order the state changes occurred, and always before the ++outermost c-ares call returns. A call made from within a callback is not the ++outermost one, so notifications it produces arrive after it has returned. ++.BR ares_destroy (3) ++is no exception: the closure of every socket the channel still holds is ++reported before it returns, and the callback is never invoked afterwards. ++This applies as of c-ares version 1.35.0, and c-ares version 1.34.9 on the ++1.34 branch. ++ + The value of + .I sock_state_cb_data + will be passed as the +diff --git a/docs/ares_set_server_state_callback.3 b/docs/ares_set_server_state_callback.3 +index 04fbde4c7a..e95db327ce 100644 +--- a/docs/ares_set_server_state_callback.3 ++++ b/docs/ares_set_server_state_callback.3 +@@ -47,6 +47,31 @@ the DNS servers used by an ares channel. For example, it can be used to track + metrics about the numbers and types of queries sent to each server or to + detect when a server is uncontactable or unhealthy. + ++.SH REENTRANCY ++The server state callback may call c-ares functions on the same channel, ++including ++.BR ares_cancel (3) ++and the server configuration functions. The channel lock is recursive and the ++library holds no query, connection or server state across the callback. ++ ++Notifications are queued and delivered once the library has finished the work ++that produced them, rather than part-way through it. A notification is ++therefore observed after the processing pass that generated it, not during. ++ ++The callback and its ++.I user_data ++are read when the notification is delivered, not when it is queued. An ++application that replaces its callback between those two points will see the ++notification delivered to the new one; unregistering by passing a ++.B NULL ++callback discards any notification still queued. ++ ++This applies to a replacement made from within a c-ares callback, where the ++channel lock is held. ++.BR ares_set_server_state_callback (3) ++does not take the channel lock, so replacing the callback from another thread ++while the channel is processing is not synchronized against delivery. ++ + .SH FLAGS + .TP 5 + .B ARES_SERV_STATE_UDP +@@ -58,5 +83,9 @@ Indicates that the query was tried over TCP. + .SH AVAILABILITY + This function was first introduced in c-ares version 1.29.0. + ++The reentrancy guarantee described above applies as of c-ares version 1.35.0, ++and c-ares version 1.34.9 on the 1.34 branch. ++ + .SH SEE ALSO +-.BR ares_get_servers_csv (3) ++.BR ares_get_servers_csv (3), ++.BR ares_cancel (3) +diff --git a/docs/ares_set_servers_csv.3 b/docs/ares_set_servers_csv.3 +index f1435143f5..f9f5b82e28 100644 +--- a/docs/ares_set_servers_csv.3 ++++ b/docs/ares_set_servers_csv.3 +@@ -126,6 +126,23 @@ are identical. Prior versions would simply omit ports in \fBares_set_servers_cs + but due to the addition of link local interface support, this difference was + removed. + ++.SH REENTRANCY ++These functions may be called from within a c-ares callback. Removing a server ++closes its connections, which ends the lookups that were using it and invokes ++their callbacks, so a reconfiguration can be requested from inside one. ++ ++A nested call reconciles the whole server list, and the outer call leaves that ++result in place rather than deleting the servers the nested call installed. ++The consequence is that the outer call's own list does not survive, even ++though it returns ++.B ARES_SUCCESS: ++the last configuration applied is the one the channel keeps. An application ++that reconfigures from a callback should therefore not assume the list it ++passed to the outer call is in effect when that call returns. ++ ++This applies as of c-ares version 1.35.0, and c-ares version 1.34.9 on the ++1.34 branch. ++ + .SH EXAMPLE + .nf + 192.168.1.100,[fe80::1]:53%eth0,dns://192.168.1.1?tcpport=1153 +diff --git a/src/lib/ares_cancel.c b/src/lib/ares_cancel.c +index 75600dea6b..aa167e4486 100644 +--- a/src/lib/ares_cancel.c ++++ b/src/lib/ares_cancel.c +@@ -38,10 +38,10 @@ void ares_cancel(ares_channel_t *channel) + } + + ares_channel_lock(channel); ++ ares_deferred_cb_enter(channel); + + if (ares_llist_len(channel->all_queries) > 0) { + ares_llist_node_t *node = NULL; +- ares_llist_node_t *next = NULL; + + /* Swap list heads, so that only those queries which were present on entry + * into this function are cancelled. New queries added by callbacks of +@@ -57,21 +57,29 @@ void ares_cancel(ares_channel_t *channel) + goto done; /* LCOV_EXCL_LINE: OutOfMemory */ + } + +- node = ares_llist_node_first(list_copy); +- while (node != NULL) { ++ /* Re-read the head each iteration rather than caching the next node: a ++ * callback below may cause other queries in this same list to be ended ++ * and their nodes destroyed. */ ++ while ((node = ares_llist_node_first(list_copy)) != NULL) { + ares_query_t *query; + +- /* Cache next since this node is being deleted */ +- next = ares_llist_node_next(node); ++ query = ares_llist_node_claim(node); + +- query = ares_llist_node_claim(node); ++ /* Must come first: node_claim() already destroyed this node, so ++ * ares_detach_query() would otherwise destroy it a second time. */ + query->node_all_queries = NULL; + ++ /* Detach from queries_by_qid, the connection and the timeout list ++ * before running user code. Otherwise a reentrant teardown from ++ * within the callback -- ares_set_servers_csv(), or a new query ++ * hitting a connection error -- still finds this query, ends it a ++ * second time and frees it, and the ares_free_query() below then ++ * double-frees. */ ++ ares_detach_query(query); ++ + /* NOTE: its possible this may enqueue new queries */ + query->callback(query->arg, ARES_ECANCELLED, 0, NULL); + ares_free_query(query); +- +- node = next; + } + + ares_llist_destroy(list_copy); +@@ -83,5 +91,6 @@ void ares_cancel(ares_channel_t *channel) + ares_queue_notify_empty(channel); + + done: ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } +diff --git a/src/lib/ares_close_sockets.c b/src/lib/ares_close_sockets.c +index 7bb72c31ec..6be662fdd3 100644 +--- a/src/lib/ares_close_sockets.c ++++ b/src/lib/ares_close_sockets.c +@@ -41,12 +41,47 @@ static void ares_requeue_queries(ares_conn_t *conn, + } + } + ++/*! Release a connection's memory. Split from the unlink below so that the ++ * two can happen at different times: a caller further up the stack may still ++ * hold this pointer, and reading a detached connection is safe where reading ++ * a freed one is not. */ ++void ares_conn_reap(ares_conn_t *conn) ++{ ++ if (conn == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ ares_buf_destroy(conn->in_buf); ++ ares_buf_destroy(conn->out_buf); ++ ares_llist_destroy(conn->queries_to_conn); ++ ares_free(conn); ++} ++ + void ares_close_connection(ares_conn_t *conn, ares_status_t requeue_status) + { +- ares_server_t *server = conn->server; +- ares_channel_t *channel = server->channel; ++ ares_server_t *server; ++ ares_channel_t *channel; ++ ++ if (conn == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ /* Already torn down and awaiting reclamation. Reaching here twice is ++ * ordinary once a connection can outlive its teardown: a caller holding it ++ * from before -- handle_conn_error() after a failed write, say -- closes it ++ * again without knowing. Doing the work twice would requeue queries that ++ * have already moved and would put this connection on the pending list a ++ * second time, which is a double free. */ ++ if (conn->flags & ARES_CONN_FLAG_DEAD) { ++ return; ++ } + +- /* Unlink */ ++ server = conn->server; ++ channel = server->channel; ++ ++ /* Unlink from everything that can find this connection. After this it is ++ * unreachable by lookup, which is what stops anything new from picking it ++ * up; anything that already holds it is handled by the deferral below. */ + ares_llist_node_claim( + ares_htable_asvp_get_direct(channel->connnode_by_socket, conn->fd)); + ares_htable_asvp_remove(channel->connnode_by_socket, conn->fd); +@@ -55,19 +90,40 @@ void ares_close_connection(ares_conn_t *conn, ares_status_t requeue_status) + server->tcp_conn = NULL; + } + +- ares_buf_destroy(conn->in_buf); +- ares_buf_destroy(conn->out_buf); ++ /* Marked dead as soon as it is unlinked, before anything below can run ++ * application code. ares_requeue_queries() ends queries terminally once ++ * their retries are exhausted, which invokes the application's callback; if ++ * the flag were set after that, a second ares_close_connection() reaching ++ * this connection in the meantime would run the body again -- closing the ++ * descriptor twice and listing the connection for reclamation twice, which ++ * is a double free. It also gives ares_close_sockets() its termination ++ * condition: dead implies unlinked, so its re-read-the-head loop cannot see ++ * a dead connection and spin. */ ++ conn->flags |= ARES_CONN_FLAG_DEAD; + + /* Requeue queries to other connections */ + ares_requeue_queries(conn, requeue_status); + +- ares_llist_destroy(conn->queries_to_conn); +- +- ares_conn_sock_state_cb_update(conn, ARES_CONN_STATE_NONE); +- +- ares_socket_close(channel, conn->fd); ++ /* Hands the descriptor to the queued notification when one is due, so the ++ * application is told to stop watching it before it is closed. Otherwise ++ * nothing will carry it and we close it here. */ ++ if (!ares_conn_sock_state_cb_close(conn)) { ++ ares_socket_close(channel, conn->fd); ++ } ++ conn->fd = ARES_SOCKET_BAD; ++ ++ /* If a library call is in progress it may be holding this connection -- ++ * read_answers() and ares_send_query_int() both do, across code that can ++ * reach here. Keep it readable until that call unwinds. The buffers and ++ * the query list are freed by ares_conn_reap(), so a stale holder sees an ++ * emptied connection rather than freed memory. */ ++ if (channel->deferred_cb_depth > 0) { ++ conn->next_pending = channel->pending_conns; ++ channel->pending_conns = conn; ++ return; ++ } + +- ares_free(conn); ++ ares_conn_reap(conn); + } + + void ares_close_sockets(ares_server_t *server) +@@ -76,6 +132,10 @@ void ares_close_sockets(ares_server_t *server) + + while ((node = ares_llist_node_first(server->connections)) != NULL) { + ares_conn_t *conn = ares_llist_node_val(node); ++ /* This loop re-reads the head each time and relies on the close below ++ * unlinking it. A dead connection returns early without unlinking, so ++ * one on this list would spin forever holding the channel lock. */ ++ assert(!(conn->flags & ARES_CONN_FLAG_DEAD)); + ares_close_connection(conn, ARES_SUCCESS); + } + } +diff --git a/src/lib/ares_conn.c b/src/lib/ares_conn.c +index 6b315b0548..78f57e7d10 100644 +--- a/src/lib/ares_conn.c ++++ b/src/lib/ares_conn.c +@@ -26,19 +26,210 @@ + */ + #include "ares_private.h" + +-void ares_conn_sock_state_cb_update(ares_conn_t *conn, +- ares_conn_state_flags_t flags) ++/*! A queued socket-state notification. Holds the descriptor and the state, ++ * never the connection, so a queued entry cannot outlive what it refers to. ++ * ++ * When \c owns_fd is set, this message carries the only remaining reference to ++ * the descriptor: the connection is gone and the descriptor is deliberately ++ * left open until after the application has been told to stop watching it. ++ * Closing first would let the number be reused -- by application code running ++ * from a deferred query callback, for instance -- before the notification ++ * naming it is delivered. */ ++typedef struct { ++ ares_socket_t fd; ++ ares_bool_t readable; ++ ares_bool_t writable; ++ ares_bool_t owns_fd; ++} ares_sock_state_msg_t; ++ ++static ares_bool_t ares_sock_state_queue(ares_channel_t *channel, ++ const ares_sock_state_msg_t *msg) + { +- ares_channel_t *channel = conn->server->channel; ++ if (channel->sock_state_queue == NULL) { ++ channel->sock_state_queue = ares_array_create(sizeof(*msg), NULL); ++ if (channel->sock_state_queue == NULL) { ++ return ARES_FALSE; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ } ++ ++ if (ares_array_insertdata_last(channel->sock_state_queue, msg) != ++ ARES_SUCCESS) { ++ return ARES_FALSE; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ ++ return ARES_TRUE; ++} ++ ++/*! Deliver every socket-state notification the channel has queued. Loops ++ * rather than snapshotting: a callback is allowed to re-enter the library and ++ * queue more, and those belong to this same delivery point. */ ++static void ares_sock_state_dispatch(ares_channel_t *channel) ++{ ++ while (ares_array_len(channel->sock_state_queue) > 0) { ++ ares_sock_state_msg_t msg; ++ ++ if (ares_array_claim_at(&msg, sizeof(msg), channel->sock_state_queue, 0) != ++ ARES_SUCCESS) { ++ break; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } + +- if ((conn->state_flags & ARES_CONN_STATE_CBFLAGS) != flags && +- channel->sock_state_cb) { +- channel->sock_state_cb(channel->sock_state_cb_data, conn->fd, +- flags & ARES_CONN_STATE_READ ? 1 : 0, +- flags & ARES_CONN_STATE_WRITE ? 1 : 0); ++ if (channel->sock_state_cb != NULL) { ++ /* The public callback takes ints. */ ++ channel->sock_state_cb(channel->sock_state_cb_data, msg.fd, ++ msg.readable ? 1 : 0, msg.writable ? 1 : 0); ++ } ++ ++ /* Told first, closed second -- the ordering the application depends on to ++ * keep its descriptor set honest. */ ++ if (msg.owns_fd) { ++ ares_socket_close(channel, msg.fd); ++ } ++ } ++ ++ ares_array_destroy(channel->sock_state_queue); ++ channel->sock_state_queue = NULL; ++} ++ ++static void ares_deferred_cb_flush(ares_channel_t *channel) ++{ ++ /* Only the outermost frame delivers: anything above us on the stack may ++ * still be holding the connection a reentrant call from one of these ++ * callbacks could free. ++ * ++ * The dispatch below counts as a nesting level of its own. Without that, a ++ * callback that calls back into the library re-enters here through the ++ * enter/leave pair on that call, drains the rest of the queue and destroys ++ * it underneath the loop -- and hands the next notification to the ++ * application from inside the current one. With the counter held, the ++ * nested leave returns immediately and whatever it queued is picked up by ++ * the loop that is already running. */ ++ if (channel->deferred_cb_depth > 0) { ++ return; ++ } ++ channel->deferred_cb_depth++; ++ ++ /* Both kinds of deferred notification share this one delivery point. Loop ++ * rather than calling each once: a socket-state callback may put the library ++ * on a path that queues a server-state notification, and the reverse, and ++ * both belong to this delivery point rather than the next one. */ ++ while (ares_array_len(channel->sock_state_queue) > 0 || ++ ares_array_len(channel->server_state_queue) > 0) { ++ ares_sock_state_dispatch(channel); ++ ares_server_state_dispatch(channel); ++ } ++ ++ /* The outermost call is unwinding and the callbacks above have run, so ++ * nothing can still be holding a server or connection that was unlinked ++ * during it. Now they can be freed. */ ++ ares_deferred_reclaim(channel); ++ ++ channel->deferred_cb_depth--; ++} ++ ++void ares_deferred_reclaim(ares_channel_t *channel) ++{ ++ /* Connections first: a pending server's connection list is already empty, ++ * but this keeps the ordering obvious rather than incidental. */ ++ while (channel->pending_conns != NULL) { ++ ares_conn_t *conn = channel->pending_conns; ++ channel->pending_conns = conn->next_pending; ++ conn->next_pending = NULL; ++ ares_conn_reap(conn); ++ } ++ ++ while (channel->pending_servers != NULL) { ++ ares_server_t *server = channel->pending_servers; ++ channel->pending_servers = server->next_pending; ++ server->next_pending = NULL; ++ ares_server_reap(server); ++ } ++} ++ ++void ares_deferred_cb_enter(ares_channel_t *channel) ++{ ++ channel->deferred_cb_depth++; ++} ++ ++void ares_deferred_cb_leave(ares_channel_t *channel) ++{ ++ if (channel->deferred_cb_depth > 0) { ++ channel->deferred_cb_depth--; ++ } ++ ares_deferred_cb_flush(channel); ++} ++ ++ares_bool_t ares_conn_sock_state_cb_close(ares_conn_t *conn) ++{ ++ ares_channel_t *channel = conn->server->channel; ++ ares_sock_state_msg_t msg; ++ ++ /* No callback to run, so nothing will ever carry this descriptor: the ++ * caller closes it itself, exactly as before. */ ++ if (channel->sock_state_cb == NULL) { ++ return ARES_FALSE; ++ } ++ ++ /* Already reported as unwatched, so no notification is due and no message ++ * will be queued to hold the descriptor. */ ++ if ((conn->state_flags & ARES_CONN_STATE_CBFLAGS) == ARES_CONN_STATE_NONE) { ++ return ARES_FALSE; ++ } ++ ++ msg.fd = conn->fd; ++ msg.readable = ARES_FALSE; ++ msg.writable = ARES_FALSE; ++ msg.owns_fd = ARES_TRUE; ++ ++ if (!ares_sock_state_queue(channel, &msg)) { ++ return ARES_FALSE; /* LCOV_EXCL_LINE: OutOfMemory */ + } + + conn->state_flags &= ~((unsigned int)ARES_CONN_STATE_CBFLAGS); ++ ++ return ARES_TRUE; ++} ++ ++void ares_conn_sock_state_cb_update(ares_conn_t *conn, ++ ares_conn_state_flags_t flags) ++{ ++ ares_channel_t *channel = conn->server->channel; ++ ares_sock_state_msg_t msg; ++ ares_bool_t changed = ARES_FALSE; ++ ++ if ((conn->state_flags & ARES_CONN_STATE_CBFLAGS) != flags) { ++ changed = ARES_TRUE; ++ } ++ ++ msg.fd = conn->fd; ++ msg.readable = flags & ARES_CONN_STATE_READ ? ARES_TRUE : ARES_FALSE; ++ msg.writable = flags & ARES_CONN_STATE_WRITE ? ARES_TRUE : ARES_FALSE; ++ msg.owns_fd = ARES_FALSE; /* the connection still owns it */ ++ ++ if (!changed || channel->sock_state_cb == NULL) { ++ conn->state_flags &= ~((unsigned int)ARES_CONN_STATE_CBFLAGS); ++ conn->state_flags |= flags; ++ return; ++ } ++ ++ /* Queue rather than dispatch. This runs in the middle of opening, writing ++ * to or closing a connection, and every caller keeps using that connection ++ * afterwards. The callback is application code that may reentrantly call ++ * ares_cancel() or ares_set_servers_csv(), either of which frees the ++ * connection -- the first through the idle-connection sweep, the second by ++ * destroying the server that owns it. Deliver once no frame still holds ++ * it. ++ * ++ * Only the descriptor and the state are captured, never the connection, and ++ * insertion order is preserved so the application observes the same ++ * sequence it would have seen inline. */ ++ if (!ares_sock_state_queue(channel, &msg)) { ++ return; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ ++ /* Recorded as reported only now: on either failure above the connection ++ * stays eligible to notify again, rather than remembering a state the ++ * application was never told about. */ ++ conn->state_flags &= ~((unsigned int)ARES_CONN_STATE_CBFLAGS); + conn->state_flags |= flags; + } + +@@ -435,6 +626,16 @@ ares_status_t ares_open_connection(ares_conn_t **conn_out, + conn->flags |= ARES_CONN_FLAG_TFO_INITIAL; + } + ++ /* The socket-configure and socket-create callbacks above are application ++ * code, and either may reconfigure the servers -- which destroys the ones ++ * the new configuration omits, including possibly this one. Everything ++ * below dereferences it, starting with conn->server in ++ * ares_conn_set_self_ip(). */ ++ if (!ares_server_in_channel(channel, server)) { ++ status = ARES_ECONNREFUSED; ++ goto done; ++ } ++ + /* Need to store our own ip for DNS cookie support */ + status = ares_conn_set_self_ip(conn, ARES_TRUE); + if (status != ARES_SUCCESS) { +diff --git a/src/lib/ares_conn.h b/src/lib/ares_conn.h +index c332de14b1..fc177b84f6 100644 +--- a/src/lib/ares_conn.h ++++ b/src/lib/ares_conn.h +@@ -49,7 +49,14 @@ typedef enum { + * it). Its in-flight queries continue to drain and it is cleaned up once + * idle. This is a per-connection signal so that a transient failure does + * not evict otherwise-healthy connections to the same server. */ +- ARES_CONN_FLAG_NONEW = 1 << 3 ++ ARES_CONN_FLAG_NONEW = 1 << 3, ++ /*! Connection has been torn down: unlinked from every lookup structure, its ++ * descriptor closed or handed to a pending notification, and its queries ++ * requeued. It survives only so that a caller further up the stack which ++ * captured it before the teardown can still read it; see ++ * ares_deferred_reclaim(). Teardown is idempotent -- a second ++ * ares_close_connection() on a dead connection does nothing. */ ++ ARES_CONN_FLAG_DEAD = 1 << 4 + } ares_conn_flags_t; + + typedef enum { +@@ -84,6 +91,10 @@ struct ares_conn { + + /* list of outstanding queries to this connection */ + ares_llist_t *queries_to_conn; ++ ++ /*! Next connection awaiting reclamation. Set only while this connection is ++ * on channel->pending_conns; see ares_deferred_reclaim(). */ ++ ares_conn_t *next_pending; + }; + + /*! Various buckets for grouping history */ +@@ -168,6 +179,10 @@ struct ares_server { + + /* Link back to owning channel */ + ares_channel_t *channel; ++ ++ /*! Next server awaiting reclamation. Set only while this server is on ++ * channel->pending_servers; see ares_deferred_reclaim(). */ ++ ares_server_t *next_pending; + }; + + void ares_close_connection(ares_conn_t *conn, ares_status_t requeue_status); +@@ -185,8 +200,24 @@ ares_status_t ares_conn_flush(ares_conn_t *conn); + ares_conn_err_t ares_conn_read(ares_conn_t *conn, void *data, size_t len, + size_t *read_bytes); + ares_conn_t *ares_conn_from_fd(const ares_channel_t *channel, ares_socket_t fd); +-void ares_conn_sock_state_cb_update(ares_conn_t *conn, +- ares_conn_state_flags_t flags); ++ ++/*! Queue the "stop watching this descriptor" notification for a connection ++ * being torn down, handing the descriptor to that queued message. ++ * ++ * The application must be told to stop watching a descriptor while it is ++ * still open -- close first and the number can be handed to something else, ++ * including by application code running from a deferred query callback, ++ * before the notification naming it is delivered. ++ * ++ * \param[in] conn Connection being closed. ++ * \return ARES_TRUE if the queued notification took the descriptor, in which ++ * case the caller must not close it; ARES_FALSE if no notification is ++ * due, and the caller closes the descriptor itself. ++ */ ++ares_bool_t ares_conn_sock_state_cb_close(ares_conn_t *conn); ++ ++void ares_conn_sock_state_cb_update(ares_conn_t *conn, ++ ares_conn_state_flags_t flags); + ares_conn_err_t ares_socket_recv(ares_channel_t *channel, ares_socket_t s, + ares_bool_t is_tcp, void *data, + size_t data_len, size_t *read_bytes); +diff --git a/src/lib/ares_destroy.c b/src/lib/ares_destroy.c +index 1e5706e0e0..e0534c800d 100644 +--- a/src/lib/ares_destroy.c ++++ b/src/lib/ares_destroy.c +@@ -69,17 +69,31 @@ void ares_destroy(ares_channel_t *channel) + * callbacks need to hold a channel lock. */ + ares_channel_lock(channel); + +- /* Destroy all queries */ +- node = ares_llist_node_first(channel->all_queries); +- while (node != NULL) { +- ares_llist_node_t *next = ares_llist_node_next(node); +- ares_query_t *query = ares_llist_node_claim(node); +- ++ /* Destroy all queries. ++ * ++ * The queries are detached before their callbacks run. Claiming the node ++ * only unlinks a query from all_queries; it stays in queries_by_qid, on ++ * conn->queries_to_conn and in queries_by_timeout. A reentrant teardown ++ * from inside the callback -- ares_set_servers_csv(), or a new query ++ * hitting a connection error -- would otherwise still find the query, end ++ * it a second time (invoking the application callback twice for one ++ * lookup) and free it, and the ares_free_query() below would double-free. ++ * ++ * The loop re-reads the first node each iteration rather than caching the ++ * next one, since a callback can cause other queries in this same list to ++ * be ended and their nodes destroyed. No list swap is needed: sys_up is ++ * already cleared above, so ares_send_nolock() refuses new queries and ++ * nothing can be added while we drain. */ ++ while ((node = ares_llist_node_first(channel->all_queries)) != NULL) { ++ ares_query_t *query = ares_llist_node_claim(node); ++ ++ /* Must come first: node_claim() already destroyed this node, so ++ * ares_detach_query() would otherwise destroy it a second time. */ + query->node_all_queries = NULL; ++ ares_detach_query(query); ++ + query->callback(query->arg, ARES_EDESTRUCTION, 0, NULL); + ares_free_query(query); +- +- node = next; + } + + ares_queue_notify_empty(channel); +@@ -93,7 +107,28 @@ void ares_destroy(ares_channel_t *channel) + assert(ares_slist_len(channel->queries_by_timeout) == 0); + #endif + ++ /* Tearing down the servers closes every remaining connection. Applications ++ * rely on being told about those closures so they can drop the descriptors ++ * from their own event loop, so the notifications are still delivered here ++ * rather than discarded -- just after the teardown instead of during it. */ ++ ares_deferred_cb_enter(channel); + ares_destroy_servers_state(channel); ++ ares_deferred_cb_leave(channel); ++ ++ /* The leave above delivered and destroyed both queues and reclaimed ++ * everything the teardown unlinked, so these are normally no-ops. They are ++ * not redundant: if the first insert into a freshly created queue fails, ++ * the array is left allocated and empty, the flush loop never runs because ++ * both queues are empty, and nothing else would ever free it. */ ++ ares_array_destroy(channel->sock_state_queue); ++ channel->sock_state_queue = NULL; ++ ares_array_destroy(channel->server_state_queue); ++ channel->server_state_queue = NULL; ++ ++#ifndef NDEBUG ++ assert(channel->pending_servers == NULL); ++ assert(channel->pending_conns == NULL); ++#endif + + #ifndef NDEBUG + assert(ares_htable_asvp_num_keys(channel->connnode_by_socket) == 0); +@@ -134,17 +169,47 @@ void ares_destroy(ares_channel_t *channel) + ares_free(channel); + } + +-void ares_destroy_server(ares_server_t *server) ++/*! Release a server's memory. Split from ares_destroy_server() for the same ++ * reason ares_conn_reap() is split out: a caller further up the stack may ++ * still hold this pointer. */ ++void ares_server_reap(ares_server_t *server) + { + if (server == NULL) { + return; /* LCOV_EXCL_LINE: DefensiveCoding */ + } + +- ares_close_sockets(server); + ares_llist_destroy(server->connections); + ares_free(server); + } + ++void ares_destroy_server(ares_server_t *server) ++{ ++ ares_channel_t *channel; ++ ++ if (server == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ channel = server->channel; ++ ++ /* Closes and unlinks every connection; each is deferred or reaped by ++ * ares_close_connection() on the same rule applied below. */ ++ ares_close_sockets(server); ++ ++ /* The caller has already unlinked this server from channel->servers, so it ++ * can no longer be found -- ares_server_in_channel() and ++ * ares_slist_node_find() both report it gone, which is what callers check ++ * before acting on one. But a library call in progress may still be ++ * holding the pointer, so keep it readable until that call unwinds. */ ++ if (channel != NULL && channel->deferred_cb_depth > 0) { ++ server->next_pending = channel->pending_servers; ++ channel->pending_servers = server; ++ return; ++ } ++ ++ ares_server_reap(server); ++} ++ + void ares_destroy_servers_state(ares_channel_t *channel) + { + ares_slist_node_t *node; +diff --git a/src/lib/ares_private.h b/src/lib/ares_private.h +index 04d4576ed2..0a1029e0f8 100644 +--- a/src/lib/ares_private.h ++++ b/src/lib/ares_private.h +@@ -147,7 +147,10 @@ typedef struct ares_query ares_query_t; + struct ares_query { + /* Query ID from qbuf, for faster lookup, and current timeout */ + unsigned short qid; /* host byte order */ +- ares_timeval_t ts; /*!< Timestamp query was sent */ ++ /*! Distinguishes this query from any later one handed the same qid. ++ * Query ids are 16-bit and recycled; this counter is not. */ ++ size_t serial; ++ ares_timeval_t ts; /*!< Timestamp query was sent */ + ares_timeval_t timeout; + ares_channel_t *channel; + +@@ -227,6 +230,9 @@ struct ares_channeldata { + /* Server addresses and communications state. Sorted by least consecutive + * failures, followed by the configuration order if failures are equal. */ + ares_slist_t *servers; ++ /*! Bumped by each ares_servers_update(). Lets the stale-server removal loop ++ * tell that a nested reconfiguration reconciled the list underneath it. */ ++ size_t servers_generation; + + /* random state to use when generating new ids and generating retry penalties + */ +@@ -237,6 +243,10 @@ struct ares_channeldata { + /* Queries bucketed by qid, for quickly dispatching DNS responses: */ + ares_htable_szvp_t *queries_by_qid; + ++ /*! Monotonic source for ares_query_t.serial. Never reused for the life of ++ * the channel, so a recycled query id cannot impersonate an older query. */ ++ size_t query_serial; ++ + /* Queries bucketed by timeout, for quickly handling timeouts: */ + ares_slist_t *queries_by_timeout; + +@@ -248,6 +258,29 @@ struct ares_channeldata { + + ares_sock_state_cb sock_state_cb; + void *sock_state_cb_data; ++ /*! Socket-state notifications queued during processing, delivered once the ++ * library is no longer holding a connection a reentrant call could free. ++ * Insertion order is preserved, so an application observes the same ++ * sequence of state changes it would have seen inline. */ ++ ares_array_t *sock_state_queue; ++ /*! Depth of nested library entry points that may produce a deferred ++ * application notification. Queues are delivered as this returns to ++ * zero. Shared by every deferred callback kind rather than one counter ++ * per kind, so the delivery rule exists in exactly one place. */ ++ size_t deferred_cb_depth; ++ ++ /*! Servers and connections that have been unlinked from every lookup ++ * structure but not yet freed, because a library call was in progress and ++ * may still hold a pointer to them. Drained by the same flush that ++ * delivers deferred notifications, once no frame can still be holding one. ++ * ++ * This is what makes a stale ares_server_t or ares_conn_t readable rather ++ * than freed for the remainder of the call that captured it. Callers ++ * still revalidate before *acting* on one -- ares_server_in_channel() and ++ * friends -- but a missed check is now "used a server that was removed" ++ * rather than a use-after-free. */ ++ ares_server_t *pending_servers; ++ ares_conn_t *pending_conns; + + ares_sock_create_callback sock_create_cb; + void *sock_create_cb_data; +@@ -295,6 +328,10 @@ struct ares_channeldata { + /* Callback triggered when a server has a successful or failed response */ + ares_server_state_callback server_state_cb; + void *server_state_cb_data; ++ /*! Server-state notifications queued during processing, dispatched once the ++ * library is no longer holding pointers a reentrant callback could free. ++ * See ares_deferred_cb_leave(). */ ++ ares_array_t *server_state_queue; + + /* TRUE if a reinit is pending. Reinit spawns a thread to read the system + * configuration and then apply the configuration since configuration +@@ -308,6 +345,11 @@ struct ares_channeldata { + * system config changes might get triggered and we need a flag to make + * sure we don't take action. */ + ares_bool_t sys_up; ++ ++ /*! Set while ares_send_nolock() is running the refusal notification for a ++ * channel being destroyed. Bounds the recursion at one frame when the ++ * application's callback responds by starting another lookup. */ ++ ares_bool_t in_shutdown_refusal; + }; + + /* Does the domain end in ".onion" or ".onion."? Case-insensitive. */ +@@ -351,6 +393,16 @@ void *ares_dnsrec_convert_arg(ares_callback callback, void *arg); + void ares_dnsrec_convert_cb(void *arg, ares_status_t status, size_t timeouts, + const ares_dns_record_t *dnsrec); + ++/*! Unlink a query from every list and table that tracks it. ++ * ++ * Must be called before running application code that could reach the query ++ * again, so a reentrant teardown cannot find it and end it a second time. ++ * ++ * \param[in] query Query to detach. Safe to call more than once on the ++ * same query. ++ */ ++void ares_detach_query(ares_query_t *query); ++ + void ares_free_query(ares_query_t *query); + + unsigned short ares_generate_new_id(ares_rand_state *state); +@@ -390,7 +442,6 @@ ares_status_t ares_sysconfig_set_options(ares_sysconfig_t *sysconfig, + + ares_status_t ares_init_by_environment(ares_sysconfig_t *sysconfig); + +- + typedef ares_status_t (*ares_sysconfig_line_cb_t)(const ares_channel_t *channel, + ares_sysconfig_t *sysconfig, + ares_buf_t *line); +@@ -589,6 +640,60 @@ ares_status_t ares_dns_name_write(ares_buf_t *buf, ares_llist_t **list, + */ + void ares_queue_notify_empty(ares_channel_t *channel); + ++/*! Is this server still installed on the channel? ++ * ++ * Application callbacks may reconfigure the servers, which destroys any that ++ * the new configuration omits. A raw ares_server_t captured before such a ++ * callback must be revalidated before it is used again. ++ * ++ * \param[in] channel Initialized ares channel. ++ * \param[in] server Server pointer captured earlier. ++ * \return ARES_TRUE if still present. ++ */ ++ares_bool_t ares_server_in_channel(const ares_channel_t *channel, ++ const ares_server_t *server); ++ ++/*! Free everything awaiting reclamation on the channel. ++ * ++ * Called only from the deferred-callback flush, at the point where the ++ * outermost library call is unwinding and no frame can still hold a pointer ++ * to an unlinked server or connection. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_deferred_reclaim(ares_channel_t *channel); ++ ++/*! Free one connection's memory. Only for the deferred-reclaim path and the ++ * immediate path in ares_close_connection(). */ ++void ares_conn_reap(ares_conn_t *conn); ++ ++/*! Free one server's memory. Only for the deferred-reclaim path and the ++ * immediate path in ares_destroy_server(). */ ++void ares_server_reap(ares_server_t *server); ++ ++/*! Enter a region that may produce application notifications the library is ++ * not in a position to deliver yet -- socket-state and server-state changes. ++ * Nesting is counted; queued notifications are delivered by the outermost ++ * leave. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_deferred_cb_enter(ares_channel_t *channel); ++ ++/*! Leave such a region. The outermost leave delivers the queues, at which ++ * point the library holds no connection a reentrant call could free. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_deferred_cb_leave(ares_channel_t *channel); ++ ++/*! Deliver every server-state notification the channel has queued. Called ++ * only from the shared deferred-callback flush. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_server_state_dispatch(ares_channel_t *channel); ++ + #define ARES_CONFIG_CHECK(x) \ + (x && x->lookups && ares_slist_len(x->servers) > 0 && x->timeout > 0 && \ + x->tries > 0) +@@ -636,7 +741,6 @@ typedef struct ares_event_thread ares_event_thread_t; + void ares_event_thread_destroy(ares_channel_t *channel); + ares_status_t ares_event_thread_init(ares_channel_t *channel); + +- + #ifdef _WIN32 + # define HOSTENT_ADDRTYPE_TYPE short + # define HOSTENT_LENGTH_TYPE short +diff --git a/src/lib/ares_process.c b/src/lib/ares_process.c +index b2316c4b33..82f53610fb 100644 +--- a/src/lib/ares_process.c ++++ b/src/lib/ares_process.c +@@ -69,7 +69,6 @@ static ares_status_t ares_send_query_int(ares_server_t *requested_server, + ares_query_t *query, + const ares_timeval_t *now, + ares_array_t **requeue); +-static void ares_detach_query(ares_query_t *query); + + static void ares_query_remove_from_conn(ares_query_t *query) + { +@@ -81,14 +80,32 @@ static void ares_query_remove_from_conn(ares_query_t *query) + query->conn = NULL; + } + ++/*! A queued server-state notification. Holds the formatted address rather ++ * than the server, so it stays valid however the server list changes. */ ++typedef struct { ++ char *server_string; ++ ares_bool_t success; ++ int flags; ++} ares_server_state_msg_t; ++ ++static void ares_server_state_msg_free(void *arg) ++{ ++ ares_server_state_msg_t *msg = arg; ++ if (msg == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ares_free(msg->server_string); ++} ++ + /* Invoke the server state callback after a success or failure */ + static void invoke_server_state_cb(const ares_server_t *server, + ares_bool_t success, int flags) + { +- const ares_channel_t *channel = server->channel; +- ares_buf_t *buf; +- ares_status_t status; +- char *server_string; ++ ares_channel_t *channel = server->channel; ++ ares_buf_t *buf; ++ ares_status_t status; ++ char *server_string; ++ ares_server_state_msg_t msg; + + if (channel->server_state_cb == NULL) { + return; +@@ -111,9 +128,57 @@ static void invoke_server_state_cb(const ares_server_t *server, + return; /* LCOV_EXCL_LINE: OutOfMemory */ + } + +- channel->server_state_cb(server_string, success, flags, +- channel->server_state_cb_data); +- ares_free(server_string); ++ /* Queue rather than dispatch. This runs in the middle of processing, and ++ * every caller keeps using its query / connection / server afterwards. The ++ * callback is application code that may reentrantly call ares_cancel(), ++ * which frees those objects -- so dispatch once the library is no longer ++ * holding them. Same philosophy as the deferred query-callback dispatch. ++ * ++ * Only the formatted address is captured, never the ares_server_t, so a ++ * queued entry cannot outlive anything it points at. */ ++ if (channel->server_state_queue == NULL) { ++ channel->server_state_queue = ares_array_create( ++ sizeof(ares_server_state_msg_t), ares_server_state_msg_free); ++ if (channel->server_state_queue == NULL) { ++ /* LCOV_EXCL_START: OutOfMemory */ ++ ares_free(server_string); ++ return; ++ /* LCOV_EXCL_STOP */ ++ } ++ } ++ ++ msg.server_string = server_string; ++ msg.success = success; ++ msg.flags = flags; ++ ++ if (ares_array_insertdata_last(channel->server_state_queue, &msg) != ++ ARES_SUCCESS) { ++ ares_free(server_string); /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++} ++ ++/*! Deliver every server-state notification the channel has queued. Loops ++ * rather than snapshotting: a callback is allowed to re-enter the library and ++ * queue more, and those belong to this same delivery point. */ ++void ares_server_state_dispatch(ares_channel_t *channel) ++{ ++ while (ares_array_len(channel->server_state_queue) > 0) { ++ ares_server_state_msg_t msg; ++ ++ if (ares_array_claim_at(&msg, sizeof(msg), channel->server_state_queue, ++ 0) != ARES_SUCCESS) { ++ break; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ if (channel->server_state_cb != NULL) { ++ channel->server_state_cb(msg.server_string, msg.success, msg.flags, ++ channel->server_state_cb_data); ++ } ++ ares_free(msg.server_string); ++ } ++ ++ ares_array_destroy(channel->server_state_queue); ++ channel->server_state_queue = NULL; + } + + static void server_increment_failures(ares_server_t *server, +@@ -204,6 +269,8 @@ static ares_status_t ares_process_fds_nolock(ares_channel_t *channel, + return ARES_EFORMERR; /* LCOV_EXCL_LINE: DefensiveCoding */ + } + ++ ares_deferred_cb_enter(channel); ++ + ares_tvnow(&now); + + /* Process write events */ +@@ -244,6 +311,8 @@ static ares_status_t ares_process_fds_nolock(ares_channel_t *channel, + } + + done: ++ ares_deferred_cb_leave(channel); ++ + if (status == ARES_ENOMEM) { + return ARES_ENOMEM; + } +@@ -553,6 +622,8 @@ typedef enum { + typedef struct { + requeue_type_t type; /* type of entry, requeue or endquery */ + unsigned short qid; /* query id */ ++ size_t serial; /* identifies the query the qid meant when ++ * this entry was appended */ + ares_server_t *server; /* requeue only: optional */ + ares_status_t status; /* endquery only */ + ares_dns_record_t *dnsrec; /* endquery only: optional */ +@@ -578,6 +649,7 @@ static ares_status_t ares_append_requeue_int(ares_array_t **requeue, + + entry.type = type; + entry.qid = query->qid; ++ entry.serial = query->serial; + entry.server = server; + entry.status = status; + entry.dnsrec = dnsrec; +@@ -601,6 +673,53 @@ static ares_status_t ares_append_endqueue(ares_array_t **requeue, + dnsrec); + } + ++/*! Is the query the caller captured still the one holding \a qid? ++ * ++ * A query id is a 16-bit wire value reissued as soon as the query holding it ++ * is detached, so it names a slot rather than a query, and comparing the ++ * pointer as well does not help: the replacement is often allocated at the ++ * address the freed one had. The serial is a channel-wide counter that is ++ * never reused. */ ++static ares_bool_t ares_query_is_live(const ares_channel_t *channel, ++ unsigned short qid, size_t serial) ++{ ++ const ares_query_t *query = ++ ares_htable_szvp_get_direct(channel->queries_by_qid, qid); ++ ++ if (query == NULL || query->serial != serial) { ++ return ARES_FALSE; ++ } ++ ++ return ARES_TRUE; ++} ++ ++/*! Is this exact server object still in the channel's server list? ++ * ++ * Compares pointers only and never dereferences \a server. Deferred ++ * reclamation keeps an unlinked server readable for the rest of the call ++ * that captured it, so reading through it is no longer a use-after-free -- ++ * but it has been removed from the configuration and must not be acted on, ++ * which is what this reports. A channel has a handful of servers, so the ++ * linear scan does not matter. */ ++ares_bool_t ares_server_in_channel(const ares_channel_t *channel, ++ const ares_server_t *server) ++{ ++ ares_slist_node_t *node; ++ ++ if (server == NULL) { ++ return ARES_FALSE; ++ } ++ ++ for (node = ares_slist_node_first(channel->servers); node != NULL; ++ node = ares_slist_node_next(node)) { ++ if (ares_slist_node_val(node) == server) { ++ return ARES_TRUE; ++ } ++ } ++ ++ return ARES_FALSE; ++} ++ + /* Drain the deferred requeue/endqueue list iteratively. All flush sites + * (read_answers(), process_timeouts(), and ares_send_query()) funnel through + * here so that: +@@ -636,11 +755,35 @@ static ares_status_t ares_flush_requeue(ares_channel_t *channel, + + query = ares_htable_szvp_get_direct(channel->queries_by_qid, entry.qid); + ++ /* A query id is a 16-bit wire value and generate_unique_qid() only avoids ++ * ids that are live when it runs. Within a single drain, a callback can ++ * cancel a query whose entry is still in this array -- releasing its id -- ++ * and start a new lookup that is handed the same id. The lookup above ++ * would then resolve this entry to a completely different query: an ++ * endquery entry would deliver one lookup's answer to another and free it, ++ * a requeue entry would put an in-flight query on the wire twice. The ++ * serial is a channel-wide counter that is never reused, so it tells the ++ * two apart. */ ++ if (query != NULL && query->serial != entry.serial) { ++ query = NULL; ++ } ++ + if (entry.type == REQUEUE_REQUEUE) { + /* Query disappeared (e.g. a prior callback in this drain cancelled it) */ + if (query == NULL) { + continue; + } ++ /* entry.server is a raw pointer captured before any callback in this ++ * drain ran. A callback may legally re-enter the public API -- the ++ * channel lock is recursive -- and ares_set_servers*() destroys servers ++ * that are absent from the new configuration, so it may already be ++ * freed. The query above is revalidated by qid for exactly this ++ * reason; do the same for the server, falling back to ordinary ++ * selection when it has gone. */ ++ if (!ares_server_in_channel(channel, entry.server)) { ++ entry.server = NULL; ++ } ++ + /* Re-dispatch via the internal entrypoint so any further requeues are + * appended back onto this same list and drained by the loop above, + * rather than recursing. */ +@@ -1377,6 +1520,7 @@ ares_status_t ares_send_query(ares_server_t *requested_server, + ares_channel_t *channel = query->channel; + ares_array_t *requeue = NULL; + unsigned short qid = query->qid; ++ size_t serial = query->serial; + ares_status_t status; + + status = ares_send_query_int(requested_server, query, now, &requeue); +@@ -1393,8 +1537,7 @@ ares_status_t ares_send_query(ares_server_t *requested_server, + * been freed. Do not dereference 'query' here. If it is no longer tracked + * it ended, so don't report success to the caller (which would, e.g., cause + * ares_send_nolock() to write to a now-freed *qid). */ +- if (status == ARES_SUCCESS && +- ares_htable_szvp_get_direct(channel->queries_by_qid, qid) == NULL) { ++ if (status == ARES_SUCCESS && !ares_query_is_live(channel, qid, serial)) { + status = ARES_ETIMEOUT; + } + +@@ -1412,6 +1555,9 @@ static ares_status_t ares_send_query_int(ares_server_t *requested_server, + size_t timeplus; + ares_status_t status; + ares_bool_t probe_downed_server = ARES_TRUE; ++ /* Captured before any application callback below, which may free the query */ ++ unsigned short qid = query->qid; ++ size_t serial = query->serial; + + /* Choose the server to send the query to */ + if (requested_server != NULL) { +@@ -1443,6 +1589,32 @@ static ares_status_t ares_send_query_int(ares_server_t *requested_server, + conn = ares_fetch_connection(channel, server, query); + if (conn == NULL) { + status = ares_open_connection(&conn, channel, server, query->using_tcp); ++ ++ /* Opening a connection runs application code -- the socket-open function ++ * from ares_set_socket_functions_ex(), and the ares_set_socket_callback() ++ * and ares_set_socket_configure_callback() callbacks -- any of which may ++ * reentrantly call ares_cancel() and free this query. Re-check liveness ++ * by the qid captured at entry rather than dereferencing a possibly-freed ++ * query. */ ++ if (!ares_query_is_live(channel, qid, serial)) { ++ return ARES_ECANCELLED; ++ } ++ ++ /* Those same callbacks may have reconfigured the servers, which frees the ++ * ones the new configuration omits. 'server' was captured before the ++ * open; everything below reads it, including server_increment_failures() ++ * on the failure paths of the switch. ++ * ++ * Requeue rather than return: every exit from this function must either ++ * enqueue the query or end it, and the query is still live here -- it has ++ * not been written, is not attached to a connection, and on a first send ++ * has no timeout node, so returning would strand it forever. ++ * query->conn is NULL, so this does not read through the freed server. */ ++ if (!ares_server_in_channel(channel, server)) { ++ return ares_requeue_query(query, now, ARES_ECONNREFUSED, ARES_TRUE, NULL, ++ requeue); ++ } ++ + switch (status) { + /* Good result, continue on */ + case ARES_SUCCESS: +@@ -1464,6 +1636,14 @@ static ares_status_t ares_send_query_int(ares_server_t *requested_server, + + /* Write the query */ + status = ares_conn_query_write(conn, query, now); ++ ++ /* Writing runs application code too: ares_conn_query_write() reaches the ++ * send function installed by ares_set_socket_functions_ex(), which may ++ * reentrantly call ares_cancel() with the same hazard as above. */ ++ if (!ares_query_is_live(channel, qid, serial)) { ++ return ARES_ECANCELLED; ++ } ++ + switch (status) { + /* Good result, continue on */ + case ARES_SUCCESS: +@@ -1479,6 +1659,17 @@ static ares_status_t ares_send_query_int(ares_server_t *requested_server, + case ARES_ECONNREFUSED: + case ARES_EBADFAMILY: + handle_conn_error(conn, ARES_TRUE, status); ++ /* handle_conn_error() -> ares_close_connection() -> ++ * ares_requeue_queries() walks conn->queries_to_conn and may terminally ++ * end other queries on this connection. If one of their callbacks ++ * reentrantly calls ares_cancel(), this query is freed too -- it is still ++ * linked in all_queries, since it only joins conn->queries_to_conn on the ++ * success path below. Re-check liveness by the qid captured at entry ++ * rather than dereferencing a possibly-freed query. Mirrors the guard in ++ * ares_send_query(). */ ++ if (!ares_query_is_live(channel, qid, serial)) { ++ return ARES_ECONNREFUSED; ++ } + status = ares_requeue_query(query, now, status, ARES_TRUE, NULL, requeue); + if (status == ARES_ETIMEOUT) { + status = ARES_ECONNREFUSED; +@@ -1597,11 +1788,18 @@ static ares_bool_t same_questions(const ares_query_t *query, + return rv; + } + +-static void ares_detach_query(ares_query_t *query) ++void ares_detach_query(ares_query_t *query) + { + /* Remove the query from all the lists in which it is linked */ + ares_query_remove_from_conn(query); +- ares_htable_szvp_remove(query->channel->queries_by_qid, query->qid); ++ /* Only release the qid slot if it is still ours. A query detached before ++ * its callback can have its qid reissued to a lookup started from within ++ * that callback, and the ares_free_query() that follows detaches a second ++ * time -- which would otherwise evict the new lookup instead. */ ++ if (ares_htable_szvp_get_direct(query->channel->queries_by_qid, query->qid) == ++ query) { ++ ares_htable_szvp_remove(query->channel->queries_by_qid, query->qid); ++ } + ares_llist_node_destroy(query->node_all_queries); + query->node_all_queries = NULL; + } +@@ -1624,6 +1822,16 @@ static void end_query(ares_channel_t *channel, ares_server_t *server, + return; + } + ++ /* Detach from all lookup lists before the callback, otherwise a reentrant ++ * ares_cancel() from within it would find this query still linked, free it, ++ * and the ares_free_query() below would double-free. Mirrors ++ * ares_flush_requeue() for every synchronous path that reaches here with a ++ * NULL requeue: connection teardown via handle_conn_error(), and server ++ * removal via ares_servers_update() and ares_destroy_servers_state(), both ++ * of which reach ares_close_sockets(). ++ * ares_detach_query() may safely run twice on the same query. */ ++ ares_detach_query(query); ++ + /* Invoke the callback. */ + query->callback(query->arg, status, query->timeouts, dnsrec); + ares_free_query(query); +@@ -1631,7 +1839,8 @@ static void end_query(ares_channel_t *channel, ares_server_t *server, + /* Check and notify if no other queries are enqueued on the channel. This + * must come after the callback and freeing the query for 2 reasons. + * 1) The callback itself may enqueue a new query +- * 2) Technically the current query isn't detached until it is free()'d. ++ * 2) The current query is detached above, before the callback, so by this ++ * point it is no longer tracked. + */ + ares_queue_notify_empty(channel); + } +diff --git a/src/lib/ares_send.c b/src/lib/ares_send.c +index 6efa9580b2..2e0482bf19 100644 +--- a/src/lib/ares_send.c ++++ b/src/lib/ares_send.c +@@ -117,6 +117,27 @@ ares_status_t ares_send_nolock(ares_channel_t *channel, ares_server_t *server, + unsigned short id = generate_unique_qid(channel); + const ares_dns_record_t *dnsrec_resp = NULL; + ++ /* A channel being destroyed accepts no new queries. ares_destroy(3) ++ * documents that a callback must not add new requests to a channel being ++ * destroyed. Refuse it, but still notify: ARES_EDESTRUCTION is documented ++ * in ares_query(3), ares_send(3), ares_search(3) and ares_getaddrinfo(3) as ++ * a status delivered to the callback, and every caller frees its own ++ * context from there. ++ * ++ * Not notified if we are already inside such a notification: an application ++ * whose callback starts a replacement lookup on every invocation would ++ * otherwise re-enter here from within the callback without bound and ++ * exhaust the stack. At that depth it is already past the contract ++ * ares_destroy(3) documents. */ ++ if (!channel->sys_up) { ++ if (!channel->in_shutdown_refusal) { ++ channel->in_shutdown_refusal = ARES_TRUE; ++ callback(arg, ARES_EDESTRUCTION, 0, NULL); ++ channel->in_shutdown_refusal = ARES_FALSE; ++ } ++ return ARES_EDESTRUCTION; ++ } ++ + ares_tvnow(&now); + + if (ares_slist_len(channel->servers) == 0) { +@@ -145,6 +166,7 @@ ares_status_t ares_send_nolock(ares_channel_t *channel, ares_server_t *server, + + query->channel = channel; + query->qid = id; ++ query->serial = ++channel->query_serial; + query->timeout.sec = 0; + query->timeout.usec = 0; + query->using_tcp = +@@ -209,6 +231,12 @@ ares_status_t ares_send_nolock(ares_channel_t *channel, ares_server_t *server, + */ + if (!ares_htable_szvp_insert(channel->queries_by_qid, query->qid, query)) { + /* LCOV_EXCL_START: OutOfMemory */ ++ /* The query is already chained into all_queries, so a reentrant ++ * ares_cancel() from the callback below would find it, run the ++ * application callback a second time for one lookup, and free it -- and ++ * the ares_free_query() here would then double-free. Same ordering as ++ * ares_cancel() and ares_destroy(): unlink first, notify second. */ ++ ares_detach_query(query); + callback(arg, ARES_ENOMEM, 0, NULL); + ares_free_query(query); + return ARES_ENOMEM; +@@ -217,10 +245,17 @@ ares_status_t ares_send_nolock(ares_channel_t *channel, ares_server_t *server, + + /* Perform the first query action. */ + ++ /* Sending opens and writes to connections and can mark a server down, both ++ * of which notify. Deliver those once we are done with the connection and ++ * the server, not part-way through. */ ++ ares_deferred_cb_enter(channel); + status = ares_send_query(server, query, &now); ++ ares_deferred_cb_leave(channel); ++ + if (status == ARES_SUCCESS && qid) { + *qid = id; + } ++ + return status; + } + +diff --git a/src/lib/ares_update_servers.c b/src/lib/ares_update_servers.c +index f8d441accf..50a9c27925 100644 +--- a/src/lib/ares_update_servers.c ++++ b/src/lib/ares_update_servers.c +@@ -740,17 +740,36 @@ static ares_bool_t ares_servers_remove_stale(ares_channel_t *channel, + { + ares_bool_t stale_removed = ARES_FALSE; + ares_slist_node_t *snode = ares_slist_node_first(channel->servers); ++ size_t generation = channel->servers_generation; + ++ /* Re-read from the head each pass rather than caching the next node. ++ * ares_slist_node_destroy() below reaches application callbacks, by way of ++ * ares_destroy_server() -> ares_close_sockets() -> ares_requeue_queries(), ++ * and a nested reconfiguration from one of those destroys the remaining ++ * stale servers -- including whichever one a cached pointer referred to. */ + while (snode != NULL) { +- ares_slist_node_t *snext = ares_slist_node_next(snode); + const ares_server_t *server = ares_slist_node_val(snode); +- if (!ares_server_in_newconfig(server, srvlist)) { +- /* This will clean up all server state via the destruction callback and +- * move any queries to new servers */ +- ares_slist_node_destroy(snode); +- stale_removed = ARES_TRUE; ++ ++ if (ares_server_in_newconfig(server, srvlist)) { ++ snode = ares_slist_node_next(snode); ++ continue; + } +- snode = snext; ++ ++ /* This will clean up all server state via the destruction callback and ++ * move any queries to new servers */ ++ ares_slist_node_destroy(snode); ++ stale_removed = ARES_TRUE; ++ ++ /* A callback reached from the destroy above may have reconfigured the ++ * server list against a newer configuration. That call has already ++ * reconciled the whole list, so whatever remains belongs to it and not to ++ * srvlist -- stop rather than deleting its servers. */ ++ if (channel->servers_generation != generation) { ++ break; ++ } ++ ++ /* The callbacks above may have removed further servers, so restart. */ ++ snode = ares_slist_node_first(channel->servers); + } + return stale_removed; + } +@@ -775,6 +794,14 @@ ares_status_t ares_servers_update(ares_channel_t *channel, + return ARES_EFORMERR; /* LCOV_EXCL_LINE: DefensiveCoding */ + } + ++ /* Removing servers closes their connections, which notifies. Deliver ++ * once this call is done rather than from inside the teardown. */ ++ ares_deferred_cb_enter(channel); ++ ++ /* Bumped before anything below can reach an application callback, so a ++ * nested reconfiguration is distinguishable from our own work. */ ++ channel->servers_generation++; ++ + /* NOTE: a NULL or zero entry server list is considered valid due to + * real-world people needing support for this for their test harnesses + */ +@@ -843,6 +870,7 @@ ares_status_t ares_servers_update(ares_channel_t *channel, + status = ARES_SUCCESS; + + done: ++ ares_deferred_cb_leave(channel); + return status; + } + +diff --git a/test/ares-test-mock.cc b/test/ares-test-mock.cc +index c0c0279abb..2158c5a913 100644 +--- a/test/ares-test-mock.cc ++++ b/test/ares-test-mock.cc +@@ -31,6 +31,11 @@ + #include <sys/stat.h> + #endif + ++#ifndef _WIN32 ++# include <errno.h> ++# include <fcntl.h> ++#endif ++ + #include <sstream> + #include <vector> + +@@ -1914,6 +1919,7 @@ TEST_P(MockRetryDepthChannelTest, HighRetryNoStackOverflow) { + struct CancelInCbData { + ares_channel_t *channel; + bool done; ++ size_t cbs; + }; + + static void CancelChannelCallback(void *arg, ares_status_t status, +@@ -1925,10 +1931,1071 @@ static void CancelChannelCallback(void *arg, ares_status_t status, + (void)timeouts; + (void)dnsrec; + data->done = true; ++ data->cbs++; + /* Reentrant cancel from within the callback. Must not double-free. */ + ares_cancel(data->channel); + } + ++// ares_destroy() notifies each still-running query that the channel is going ++// away, and holds a pointer to the next one in its list while doing so. A ++// reentrant ares_cancel() from one of those callbacks frees every remaining ++// query -- including that cached next, which ares_destroy() then follows. ++// ++// ares_cancel() is immune to the same pattern because it swaps the list head ++// before walking it; ares_destroy() did not. Two queries are required: with ++// only one in flight there is no next to cache and nothing goes wrong. ++// tries=1 so a teardown ends the query outright instead of retrying it, which ++// is the precondition this family of bugs shares. ++class MockUDPSingleTryTest : public MockChannelOptsTest, ++ public ::testing::WithParamInterface<int> { ++public: ++ MockUDPSingleTryTest() ++ : MockChannelOptsTest(1, GetParam(), false, false, FillOptions(&opts_), ++ ARES_OPT_TRIES) ++ { ++ } ++ ++ static struct ares_options *FillOptions(struct ares_options *opts) ++ { ++ memset(opts, 0, sizeof(struct ares_options)); ++ opts->tries = 1; ++ return opts; ++ } ++ ++private: ++ struct ares_options opts_; ++}; ++ ++// ares_cancel() and ares_destroy() free a query themselves rather than going ++// through end_query(). Both used to invoke the application callback while the ++// query was still in queries_by_qid, on conn->queries_to_conn and in ++// queries_by_timeout -- only all_queries was unlinked first. A reentrant ++// teardown from within that callback therefore still found the query, ended it ++// a second time, and freed it; the ares_free_query() that followed then ++// double-freed. One query in flight is enough. ++static void ReconfigureServersCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ CancelInCbData *data = static_cast<CancelInCbData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ data->done = true; ++ data->cbs++; ++ /* Frees every server absent from the new configuration, closing their ++ * connections -- which still hold the queries being torn down. */ ++ ares_set_servers_csv(data->channel, "127.0.0.1:53055"); ++} ++ ++// Reconfiguring the server list from a cancellation callback. ++TEST_P(MockUDPSingleTryTest, ReconfigureInCancelCallbackNoDoubleFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ QueryResult result; ++ /* Left unanswered, so both stay attached to the connection. */ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, ReconfigureServersCallback, ++ &data, NULL)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, QueryCallback, &result, NULL)); ++ ++ ares_cancel(channel_); ++ EXPECT_TRUE(data.done); ++ EXPECT_TRUE(result.done_); ++ /* Each lookup must be notified exactly once, not twice. */ ++ EXPECT_EQ((size_t)1, data.cbs); ++} ++ ++/* The replacement lookup's result outlives the callback that starts it. */ ++static QueryResult g_replacement_result; ++ ++static void NewQueryOnFailingWriteCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ /* ares_set_socket_functions() stores the pointer rather than copying it, so ++ * this must outlive the call. */ ++ static ares_socket_functions sock_funcs; ++ CancelInCbData *data = static_cast<CancelInCbData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ ++ data->cbs++; ++ ++ /* Only the first cancellation callback starts a replacement lookup. */ ++ if (data->done) { ++ return; ++ } ++ data->done = true; ++ ++ memset(&sock_funcs, 0, sizeof(sock_funcs)); ++ sock_funcs.asendv = LibraryTest::ares_sendv_fail; ++ ares_set_socket_functions(data->channel, &sock_funcs, NULL); ++ LibraryTest::SetFailSend(); ++ ++ ares_query_dnsrec(data->channel, "www.probe.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, QueryCallback, &g_replacement_result, ++ NULL); ++} ++ ++// No reconfiguration at all: the cancellation callback simply starts a ++// replacement lookup -- an ordinary retry pattern -- and that lookup's write ++// fails. ares_close_connection() then walks conn->queries_to_conn, which ++// still holds the queries ares_cancel() is in the middle of. Wider than the ++// case above: it needs nothing more than a connection error. ++TEST_P(MockUDPSingleTryTest, NewQueryInCancelCallbackConnErrorNoDoubleFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ g_replacement_result.done_ = false; ++ ++ QueryResult result; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, NewQueryOnFailingWriteCallback, ++ &data, NULL)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, QueryCallback, &result, NULL)); ++ ++ ares_cancel(channel_); ++ EXPECT_TRUE(data.done); ++ EXPECT_TRUE(result.done_); ++ /* Each lookup must be notified exactly once, not twice. */ ++ EXPECT_EQ((size_t)1, data.cbs); ++ /* Pins that the replacement lookup ran and its write failed, which is what ++ * drives the connection teardown this test is about. */ ++ EXPECT_TRUE(g_replacement_result.done_); ++} ++ ++// The same shape through ares_destroy() rather than ares_cancel(). Swapping ++// the all_queries head does not help here: the reentrant teardown reaches the ++// query through the connection, not through all_queries. ++TEST_P(MockUDPSingleTryTest, ReconfigureInDestroyCallbackNoDoubleFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ QueryResult result; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, ReconfigureServersCallback, ++ &data, NULL)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, QueryCallback, &result, NULL)); ++ ++ ares_destroy(channel_); ++ channel_ = nullptr; /* destroyed here, don't let the fixture repeat it */ ++ ++ EXPECT_TRUE(data.done); ++ EXPECT_TRUE(result.done_); ++ /* Each lookup must be notified exactly once, not twice. */ ++ EXPECT_EQ((size_t)1, data.cbs); ++} ++ ++// Once ares_destroy() has begun, a lookup started from a destruction callback ++// is refused rather than allocated into a channel being torn down -- and the ++// refusal still runs the caller's callback with ARES_EDESTRUCTION, so context ++// attached to `arg` can be reclaimed. ares_cancel() leaves sys_up true, so a ++// destruction callback is the only way to reach this. ++struct DestroyReentryData { ++ ares_channel_t *channel; ++ size_t outer_cbs; ++ size_t replacement_cbs; ++ ares_status_t replacement_status; ++}; ++ ++static void DestroyReplacementCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ DestroyReentryData *data = static_cast<DestroyReentryData *>(arg); ++ (void)timeouts; ++ (void)dnsrec; ++ data->replacement_cbs++; ++ data->replacement_status = status; ++} ++ ++static void DestroyReentryCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ DestroyReentryData *data = static_cast<DestroyReentryData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ data->outer_cbs++; ++ ares_query_dnsrec(data->channel, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, DestroyReplacementCallback, data, NULL); ++} ++ ++TEST_F(LibraryTest, NewQueryInDestroyCallbackIsRefusedAndNotified) { ++ ares_channel_t *channel = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS; ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts, optmask)); ++ // Nothing listens here; no traffic is needed. ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel, "127.0.0.1:53055")); ++ ++ DestroyReentryData data; ++ data.channel = channel; ++ data.outer_cbs = 0; ++ data.replacement_cbs = 0; ++ data.replacement_status = ARES_SUCCESS; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, DestroyReentryCallback, &data, ++ NULL)); ++ ++ ares_destroy(channel); ++ ++ EXPECT_EQ((size_t)1, data.outer_cbs); ++ // Refused -- but notified, so the caller can release whatever it attached. ++ EXPECT_EQ((size_t)1, data.replacement_cbs); ++ EXPECT_EQ(ARES_EDESTRUCTION, data.replacement_status); ++} ++ ++INSTANTIATE_TEST_SUITE_P(AddressFamilies, MockUDPSingleTryTest, ++ ::testing::ValuesIn(ares::test::families), ++ PrintFamily); ++ ++TEST_P(MockUDPChannelTest, CancelInDestroyCallbackNoUseAfterFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ /* Left unanswered, so both are still pending when the channel goes down. */ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, CancelChannelCallback, &data, ++ NULL)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, CancelChannelCallback, &data, ++ NULL)); ++ /* Without this the test can pass vacuously if a query failed to start. */ ++ EXPECT_EQ(2, ares_queue_active_queries(channel_)); ++ ++ ares_destroy(channel_); ++ channel_ = nullptr; /* destroyed here, don't let the fixture repeat it */ ++ ++ EXPECT_TRUE(data.done); ++ /* Every query must be notified exactly once. */ ++ EXPECT_EQ((size_t)2, data.cbs); ++} ++ ++// Fixture with tries=1 so the first failure ends the query outright instead of ++// retrying it -- that is what drives execution into end_query()'s synchronous ++// (requeue==NULL) branch. ++// The tests below use the MockUDPSingleTryTest fixture defined above: tries=1, ++// so the first failure ends the query outright instead of retrying it, which ++// is what drives execution into end_query()'s synchronous (requeue==NULL) ++// branch. ++// ++ ++// Connection-teardown route into the synchronous branch. A malformed reply ++// makes process_answer() return EBADRESP, so handle_conn_error() tears the ++// connection down and ares_close_connection() requeues its queries with a NULL ++// requeue; with tries=1 the query is ended right there. CVE-2026-33630 fixed ++// the deferred flush path only, so this one still double-freed. ++TEST_P(MockUDPSingleTryTest, CancelInCallbackConnTeardownNoDoubleFree) { ++ /* Too short to be a DNS header, so it fails to parse. */ ++ std::vector<byte> malformed = {0x00, 0x00, 0x00, 0x00}; ++ ON_CALL(server_, OnRequest("www.google.com", T_A)) ++ .WillByDefault(SetReplyData(&server_, malformed)); ++ ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ CancelChannelCallback, &data, NULL); ++ Process(); ++ EXPECT_TRUE(data.done); ++} ++ ++// Server-removal route into the same synchronous branch, which the ++// connection-teardown test above does not cover. Reconfiguring the server list ++// destroys the servers that are gone, and ares_close_sockets() closes their ++// connections with a NULL requeue -- a second way to reach end_query() with the ++// query still linked. No Process() call is needed: the query is attached to a ++// connection by ares_query_dnsrec() itself, and nothing has replied to it. ++TEST_P(MockUDPSingleTryTest, CancelInCallbackServerRemovalNoDoubleFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ CancelChannelCallback, &data, NULL); ++ ++ /* Drops the server the in-flight query is attached to. */ ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel_, "127.0.0.1:53055")); ++ EXPECT_TRUE(data.done); ++} ++ ++// ares_send_query_int() calls handle_conn_error() on a retryable write failure ++// and then keeps using its own query. That teardown requeues the *other* ++// queries on the connection, and with tries=1 they are ended outright; if one ++// of their callbacks reentrantly cancels the channel, this query is freed too ++// -- it is still linked in all_queries, having only joined ++// conn->queries_to_conn on the success path further down. Using it afterwards ++// is a use-after-free. ++// ++// The first query is left unanswered so it stays on the connection, then the ++// next write is made to fail with ECONNREFUSED so the second query takes the ++// error path while the first is still attached. ++// ++// Note this is masked on code that has not fixed the end_query() double-free: ++// that aborts inside handle_conn_error() before control returns here. ++TEST_P(MockUDPSingleTryTest, CancelInSiblingCallbackConnErrorNoUseAfterFree) { ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ /* No reply configured for this one, so it stays attached to the connection. */ ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ CancelChannelCallback, &data, NULL); ++ ++ ares_socket_functions sock_funcs; ++ memset(&sock_funcs, 0, sizeof(sock_funcs)); ++ sock_funcs.asendv = ares_sendv_fail; ++ ares_set_socket_functions(channel_, &sock_funcs, NULL); ++ LibraryTest::SetFailSend(); ++ ++ QueryResult result; ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, &result, NULL); ++ ++ /* The write failure above tears the connection down, which ends the first ++ * query, whose callback cancels the channel and frees this one. */ ++ EXPECT_TRUE(data.done); ++ /* The sibling is cancelled from the first query's callback rather than ++ * requeued; pin that rather than leaving it unstated. */ ++ EXPECT_TRUE(result.done_); ++ EXPECT_EQ(ARES_ECANCELLED, result.status_); ++} ++ ++// The socket-state callback -- registered with ARES_OPT_SOCK_STATE_CB, the ++// usual way c-ares is driven from an event loop -- used to be dispatched from ++// the middle of opening a connection, and ares_open_connection() keeps using ++// that connection afterwards. A reentrant ares_cancel() from the callback ++// frees both the connection (through the idle sweep) and the in-flight query, ++// leaving the library using either. ++struct SockStateCancelData { ++ ares_channel_t *channel; ++ size_t calls; ++}; ++ ++static void SockStateCancelCallback(void *data, ares_socket_t socket_fd, ++ int readable, int writable) ++{ ++ SockStateCancelData *d = static_cast<SockStateCancelData *>(data); ++ (void)socket_fd; ++ (void)readable; ++ (void)writable; ++ d->calls++; ++ // Reentrant cancel from the socket-state notification. ++ if (d->channel != nullptr) { ++ ares_cancel(d->channel); ++ } ++} ++ ++TEST_F(LibraryTest, CancelInSockStateCallbackNoUseAfterFree) { ++ SockStateCancelData data; ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ++ data.channel = nullptr; ++ data.calls = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateCancelCallback; ++ opts.sock_state_cb_data = &data; ++ optmask = ARES_OPT_SOCK_STATE_CB; ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&chan, &opts, optmask)); ++ ASSERT_NE(nullptr, chan); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan, "127.0.0.1:53055")); ++ ++ /* Armed only now, so channel setup itself does not cancel. */ ++ data.channel = chan; ++ ++ QueryResult result; ++ ares_query_dnsrec(chan, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, &result, NULL); ++ ++ /* Opening the connection notifies, the notification cancels, and the ++ * library must still be able to finish -- and the lookup must actually be ++ * cancelled rather than the cancel quietly doing nothing. */ ++ EXPECT_GT(data.calls, (size_t)0); ++ EXPECT_TRUE(result.done_); ++ EXPECT_EQ(ARES_ECANCELLED, result.status_); ++ ++ data.channel = nullptr; /* don't cancel from teardown notifications */ ++ ares_destroy(chan); ++} ++ ++// Deferring the notification must not reorder it past the close of the ++// descriptor it names. An event loop is told "stop watching fd N"; if fd N is ++// already closed by then, the number can have been handed to something else -- ++// including to application code running from a deferred query callback -- and ++// the application ends up setting and clearing interest in a descriptor it now ++// owns for another purpose. ++// ++// The invariant is checked directly: while the callback naming the descriptor ++// runs, that descriptor must still be open. ++#ifndef _WIN32 ++struct SockStateCloseOrderData { ++ size_t closes_seen; ++ size_t closed_while_already_shut; ++}; ++ ++static void SockStateCloseOrderCallback(void *data, ares_socket_t socket_fd, ++ int readable, int writable) ++{ ++ SockStateCloseOrderData *d = static_cast<SockStateCloseOrderData *>(data); ++ if (readable || writable) { ++ return; ++ } ++ d->closes_seen++; ++ // F_GETFD on a closed descriptor fails with EBADF. c-ares must not have ++ // closed it yet at this point. ++ if (fcntl(static_cast<int>(socket_fd), F_GETFD) == -1 && errno == EBADF) { ++ d->closed_while_already_shut++; ++ } ++} ++ ++TEST_F(LibraryTest, SockStateCloseIsReportedBeforeDescriptorCloses) { ++ SockStateCloseOrderData data; ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ ++ data.closes_seen = 0; ++ data.closed_while_already_shut = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateCloseOrderCallback; ++ opts.sock_state_cb_data = &data; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_init_options(&chan, &opts, ARES_OPT_SOCK_STATE_CB)); ++ ASSERT_NE(nullptr, chan); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan, "127.0.0.1:53055")); ++ ++ QueryResult result; ++ ares_query_dnsrec(chan, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, &result, NULL); ++ ares_destroy(chan); ++ ++ EXPECT_GT(data.closes_seen, (size_t)0); ++ EXPECT_EQ((size_t)0, data.closed_while_already_shut) ++ << "the application was told to stop watching a descriptor c-ares had " ++ "already closed"; ++} ++#endif ++ ++// Reconfiguring the servers from an I/O hook frees the server and its ++// connection while the library is still holding both. Deferred reclamation is ++// what makes these safe: the objects are unlinked from every lookup structure ++// immediately, so nothing can find them, but their memory survives until the ++// outermost call unwinds. Without it these are heap-use-after-free in ++// ares_conn_connect() and handle_conn_error() respectively. ++// ++// This is the class behind CVE-2025-31498 and CVE-2025-62408 -- a connection ++// cleaned up while a caller further up the stack still held it. ++struct ReconfigureFromHookData { ++ ares_channel_t *channel; ++ bool done; ++ int status; ++}; ++ ++static int ReconfigureConfigureCallback(ares_socket_t fd, int type, void *data) ++{ ++ ReconfigureFromHookData *d = static_cast<ReconfigureFromHookData *>(data); ++ (void)fd; ++ (void)type; ++ // Frees the server this connection is being opened for. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056"); ++ return 0; ++} ++ ++static void ReconfigureFromHookCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) ++{ ++ ReconfigureFromHookData *d = static_cast<ReconfigureFromHookData *>(arg); ++ (void)timeouts; ++ (void)dnsrec; ++ d->done = true; ++ d->status = static_cast<int>(status); ++} ++ ++TEST_F(LibraryTest, ReconfigureInConfigureHookNoUseAfterFree) { ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ReconfigureFromHookData data; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS; ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&chan, &opts, optmask)); ++ ASSERT_NE(nullptr, chan); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan, "127.0.0.1:53055")); ++ ++ data.channel = chan; ++ data.done = false; ++ data.status = ARES_SUCCESS; ++ ares_set_socket_configure_callback(chan, ReconfigureConfigureCallback, &data); ++ ++ ares_query_dnsrec(chan, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ ReconfigureFromHookCallback, &data, NULL); ++ ++ // Must complete rather than be stranded -- every exit from the send path ++ // either enqueues the query or ends it. ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ((size_t)0, ares_queue_active_queries(chan)); ++ ares_destroy(chan); ++} ++ ++// The second route to the same freed connection, and the one that drove the ++// change from suppressing the idle sweep to deferring the notification: ++// reconfiguring the servers from the socket-state callback destroys the server ++// and with it the connection ares_open_connection() is still using. Nothing ++// covered this route, so a regression in it would have gone unnoticed. ++struct SockStateReconfigureData { ++ ares_channel_t *channel; ++ size_t calls; ++}; ++ ++static void SockStateReconfigureCallback(void *data, ares_socket_t socket_fd, ++ int readable, int writable) ++{ ++ SockStateReconfigureData *d = ++ static_cast<SockStateReconfigureData *>(data); ++ (void)socket_fd; ++ (void)readable; ++ (void)writable; ++ d->calls++; ++ if (d->channel != nullptr) { ++ // Destroys the server, and with it the connection being opened. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056"); ++ } ++} ++ ++TEST_F(LibraryTest, ReconfigureInSockStateCallbackNoUseAfterFree) { ++ SockStateReconfigureData data; ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ ++ data.channel = nullptr; ++ data.calls = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateReconfigureCallback; ++ opts.sock_state_cb_data = &data; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_init_options(&chan, &opts, ARES_OPT_SOCK_STATE_CB)); ++ ASSERT_NE(nullptr, chan); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan, "127.0.0.1:53055")); ++ ++ /* Armed only now, so channel setup itself does not reconfigure. */ ++ data.channel = chan; ++ ++ QueryResult result; ++ ares_query_dnsrec(chan, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, &result, NULL); ++ ++ EXPECT_GT(data.calls, (size_t)0); ++ ++ data.channel = nullptr; /* don't reconfigure from teardown notifications */ ++ ares_destroy(chan); ++} ++ ++// Deferring the notifications must not lose them: every public entry point ++// that closes a socket still has to tell the application about it before it ++// returns, or an event loop is left polling a descriptor that c-ares closed. ++struct SockStateRecordData { ++ std::vector<std::pair<ares_socket_t, bool>> events; /* fd, still-armed */ ++}; ++ ++static void SockStateRecordCallback(void *data, ares_socket_t socket_fd, ++ int readable, int writable) ++{ ++ SockStateRecordData *d = static_cast<SockStateRecordData *>(data); ++ d->events.push_back(std::make_pair(socket_fd, readable || writable)); ++} ++ ++static bool SawClose(const SockStateRecordData &d) ++{ ++ for (size_t i = 0; i < d.events.size(); i++) { ++ if (!d.events[i].second) { ++ return true; ++ } ++ } ++ return false; ++} ++ ++class SockStateDeliveryTest : public LibraryTest { ++protected: ++ void OpenConnection(ares_channel_t **chan, SockStateRecordData *data, ++ QueryResult *result) ++ { ++ struct ares_options opts; ++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateRecordCallback; ++ opts.sock_state_cb_data = data; ++ ++ ASSERT_EQ(ARES_SUCCESS, ++ ares_init_options(chan, &opts, ARES_OPT_SOCK_STATE_CB)); ++ ASSERT_NE(nullptr, *chan); ++ /* TEST-NET-1, guaranteed not to answer, so the socket is still open when ++ * the call under test runs. */ ++ ASSERT_EQ(ARES_SUCCESS, ares_set_servers_csv(*chan, "192.0.2.1:53")); ++ ++ ares_query_dnsrec(*chan, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, result, NULL); ++ ASSERT_FALSE(result->done_); ++ ASSERT_FALSE(data->events.empty()); ++ ASSERT_TRUE(data->events.back().second); /* opened, armed for read */ ++ data->events.clear(); ++ } ++}; ++ ++TEST_F(SockStateDeliveryTest, CancelReportsSocketClose) { ++ ares_channel_t *chan = nullptr; ++ SockStateRecordData data; ++ QueryResult result; ++ ++ OpenConnection(&chan, &data, &result); ++ ares_cancel(chan); ++ EXPECT_TRUE(SawClose(data)) << "ares_cancel() closed the socket without " ++ "telling the application"; ++ ares_destroy(chan); ++} ++ ++TEST_F(SockStateDeliveryTest, SetServersReportsSocketClose) { ++ ares_channel_t *chan = nullptr; ++ SockStateRecordData data; ++ QueryResult result; ++ ++ OpenConnection(&chan, &data, &result); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan, "198.51.100.1:53")); ++ EXPECT_TRUE(SawClose(data)) << "ares_set_servers_csv() closed the socket " ++ "without telling the application"; ++ ares_destroy(chan); ++} ++ ++TEST_F(SockStateDeliveryTest, DestroyReportsSocketClose) { ++ ares_channel_t *chan = nullptr; ++ SockStateRecordData data; ++ QueryResult result; ++ ++ OpenConnection(&chan, &data, &result); ++ ares_destroy(chan); ++ EXPECT_TRUE(SawClose(data)) << "ares_destroy() closed the socket without " ++ "telling the application"; ++} ++ ++// The server-state callback is dispatched from the middle of processing, and ++// every caller keeps using its query afterwards. Nothing documents the ++// callback as non-reentrant, and the channel lock is recursive precisely so ++// API calls from callbacks are allowed -- so an application that cancels from ++// it left the library using a freed query. ++// ++// No hostile input is needed: an ordinary timeout reaches the same place. A ++// SERVFAIL is used here because it is deterministic in the harness. ++struct ServerStateCancelData { ++ ares_channel_t *channel; ++ int server_cb_calls; ++ bool query_done; ++}; ++ ++static void ServerStateCancelCallback(const char *server_string, ++ ares_bool_t success, int flags, ++ void *data) { ++ ServerStateCancelData *d = static_cast<ServerStateCancelData *>(data); ++ (void)server_string; ++ (void)success; ++ (void)flags; ++ d->server_cb_calls++; ++ // Reentrant cancel from the notification. ++ ares_cancel(d->channel); ++} ++ ++static void ServerStateQueryCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ ServerStateCancelData *d = static_cast<ServerStateCancelData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ d->query_done = true; ++} ++ ++// The advisory's headline claim is that no hostile input is needed, because an ++// ordinary timeout reaches the same place. The SERVFAIL test below does not ++// show that; this one does. ++class ServerStateTimeoutCancelTest ++ : public MockChannelOptsTest, ++ public ::testing::WithParamInterface<int> { ++ public: ++ ServerStateTimeoutCancelTest() ++ : MockChannelOptsTest(1, GetParam(), false, false, FillOptions(&opts_), ++ ARES_OPT_TIMEOUTMS | ARES_OPT_TRIES) {} ++ static struct ares_options *FillOptions(struct ares_options *opts) { ++ memset(opts, 0, sizeof(struct ares_options)); ++ opts->timeout = 100; ++ opts->tries = 3; ++ return opts; ++ } ++ ++ private: ++ struct ares_options opts_; ++}; ++ ++TEST_P(ServerStateTimeoutCancelTest, CancelInServerStateCallbackOnTimeout) { ++ ServerStateCancelData data; ++ data.channel = channel_; ++ data.server_cb_calls = 0; ++ data.query_done = false; ++ ++ ares_set_server_state_callback(channel_, ServerStateCancelCallback, &data); ++ ++ // No reply configured, so the query times out and the notification comes ++ // from process_timeouts() rather than from a response. ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, ServerStateQueryCallback, &data, ++ NULL)); ++ Process(); ++ ++ EXPECT_GT(data.server_cb_calls, 0); ++ EXPECT_TRUE(data.query_done); ++} ++ ++INSTANTIATE_TEST_SUITE_P(AddressFamilies, ServerStateTimeoutCancelTest, ++ ::testing::ValuesIn(ares::test::families), ++ PrintFamily); ++ ++// Unregistering with a NULL callback discards whatever is still queued -- the ++// documented behaviour of deferring delivery, and the only way an application ++// can opt back out once notifications are in flight. ++struct ServerStateUnregisterData { ++ ares_channel_t *channel; ++ size_t server_cb_calls; ++ bool query_done; ++}; ++ ++static void ServerStateUnregisterCallback(const char *server_string, ++ ares_bool_t success, ++ int flags, void *user_data) ++{ ++ ServerStateUnregisterData *d = ++ static_cast<ServerStateUnregisterData *>(user_data); ++ (void)server_string; ++ (void)success; ++ (void)flags; ++ d->server_cb_calls++; ++ // Clear ourselves from inside the first notification. Any notification ++ // still queued behind this one must be dropped rather than delivered. ++ ares_set_server_state_callback(d->channel, NULL, NULL); ++} ++ ++static void ServerStateUnregisterQueryCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) ++{ ++ ServerStateUnregisterData *d = ++ static_cast<ServerStateUnregisterData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ d->query_done = true; ++} ++ ++TEST_P(ServerStateTimeoutCancelTest, UnregisterInCallbackDiscardsQueued) { ++ ServerStateUnregisterData data; ++ data.channel = channel_; ++ data.server_cb_calls = 0; ++ data.query_done = false; ++ ++ ares_set_server_state_callback(channel_, ServerStateUnregisterCallback, ++ &data); ++ ++ // No reply configured: the query times out on every try, so more than one ++ // notification is queued before the flush point is reached. ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, ++ ServerStateUnregisterQueryCallback, &data, NULL)); ++ Process(); ++ ++ EXPECT_EQ((size_t)1, data.server_cb_calls); ++ EXPECT_TRUE(data.query_done); ++} ++ ++TEST_P(MockUDPChannelTest, CancelInServerStateCallbackNoUseAfterFree) { ++ DNSPacket servfail; ++ servfail.set_response().set_aa().set_rcode(SERVFAIL) ++ .add_question(new DNSQuestion("www.google.com", T_A)); ++ ON_CALL(server_, OnRequest("www.google.com", T_A)) ++ .WillByDefault(SetReply(&server_, &servfail)); ++ ++ ServerStateCancelData data; ++ data.channel = channel_; ++ data.server_cb_calls = 0; ++ data.query_done = false; ++ ++ ares_set_server_state_callback(channel_, ServerStateCancelCallback, &data); ++ ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ ServerStateQueryCallback, &data, NULL); ++ Process(); ++ ++ EXPECT_GT(data.server_cb_calls, 0); ++ EXPECT_TRUE(data.query_done); ++} ++ ++// The deferred requeue list records the target server as a raw pointer, and ++// the drain never revalidated it -- although it does revalidate the query, by ++// qid, for exactly the same reason. Application callbacks run from inside that ++// drain, and a callback may legally reconfigure the servers, which frees the ++// ones no longer in the list. A later entry in the same drain then dispatches ++// through a freed ares_server_t. ++// ++// Two replies must be read in one batch: an ordinary answer whose callback ++// reconfigures, and a FORMERR carrying no OPT record, which is what makes ++// process_answer() queue a retry *to the same server* (the EDNS fallback -- ++// the only site that records a server pointer at all). ++struct ReconfigureInCbData { ++ ares_channel_t *channel; ++ bool done; ++}; ++ ++static void ReconfigureChannelCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ ReconfigureInCbData *data = static_cast<ReconfigureInCbData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ data->done = true; ++ /* Frees every server absent from the new configuration. */ ++ ares_set_servers_csv(data->channel, "127.0.0.1:53055"); ++} ++ ++TEST_P(MockUDPChannelTest, ReconfigureInCallbackStaleRequeueServer) { ++ DNSPacket okrsp; ++ okrsp.set_response().set_aa() ++ .add_question(new DNSQuestion("www.google.com", T_A)) ++ .add_answer(new DNSARR("www.google.com", 100, {1, 2, 3, 4})); ++ ON_CALL(server_, OnRequest("www.google.com", T_A)) ++ .WillByDefault(SetReply(&server_, &okrsp)); ++ ++ /* FORMERR with no OPT record: the server looks like it does not understand ++ * EDNS, so the query is queued for retry to this same server. */ ++ DNSPacket formerr; ++ formerr.set_response().set_aa().set_rcode(FORMERR) ++ .add_question(new DNSQuestion("www.example.com", T_A)); ++ ON_CALL(server_, OnRequest("www.example.com", T_A)) ++ .WillByDefault(SetReply(&server_, &formerr)); ++ ++ ReconfigureInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ ++ QueryResult second; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ ReconfigureChannelCallback, &data, NULL); ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN, ARES_REC_TYPE_A, ++ QueryCallback, &second, NULL); ++ ++ /* Both replies have to be consumed in a single pass so they share one ++ * deferred-requeue scope -- that is what puts the ENDQUERY entry whose ++ * callback reconfigures ahead of the REQUEUE entry holding a server ++ * pointer. The usual Process() loop lets c-ares read between the two, so ++ * they never share a batch; answer both first, then read once. */ ++ for (int i = 0; i < 4; i++) { ++ std::set<ares_socket_t> server_fds = fds(); ++ fd_set r; ++ struct timeval tv = {0, 20000}; ++ int nfds = 0; ++ FD_ZERO(&r); ++ for (ares_socket_t fd : server_fds) { ++ FD_SET(fd, &r); ++ if ((int)fd >= nfds) { ++ nfds = (int)fd + 1; ++ } ++ } ++ if (select(nfds, &r, nullptr, nullptr, &tv) <= 0) { ++ break; ++ } ++ for (ares_socket_t fd : server_fds) { ++ if (FD_ISSET(fd, &r)) { ++ ProcessFD(fd); ++ } ++ } ++ } ++ { ++ fd_set readers; ++ fd_set writers; ++ struct timeval tv = {0, 0}; ++ int nfds; ++ FD_ZERO(&readers); ++ FD_ZERO(&writers); ++ nfds = ares_fds(channel_, &readers, &writers); ++ select(nfds, &readers, &writers, nullptr, &tv); ++ ares_process(channel_, &readers, &writers); ++ } ++ ++ EXPECT_TRUE(data.done); ++ ++ /* With the fix the FORMERR query is correctly re-dispatched (to the newly ++ * configured, unreachable server) rather than going through a freed one, so ++ * it is still outstanding here. Cancel it so its callback runs while its ++ * result object is still in scope. */ ++ ares_cancel(channel_); ++ EXPECT_TRUE(second.done_); ++} ++ ++// ares_servers_remove_stale() destroys each stale server in turn, and that ++// reaches application callbacks (ares_destroy_server() -> ares_close_sockets() ++// -> ares_requeue_queries() -> end_query()). A callback that reconfigures the ++// servers again destroys the servers the outer loop has not visited yet, so a ++// cached "next" node points at freed memory. ++struct NestedReconfigureData { ++ ares_channel_t *channel; ++ bool nested; ++}; ++ ++static void NestedReconfigureCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ NestedReconfigureData *data = static_cast<NestedReconfigureData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ if (data->nested) { ++ return; ++ } ++ data->nested = true; ++ // Destroys the stale servers the outer removal loop has not reached yet. ++ ares_set_servers_csv(data->channel, ""); ++} ++ ++TEST_F(LibraryTest, NestedReconfigureInCallbackRemoveStale) { ++ ares_channel_t *channel = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ /* One try per server, so tearing them down exhausts the query's retries and ++ * runs its callback from inside the removal loop. */ ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS; ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts, optmask)); ++ /* Three servers, so the loop still has entries after destroying the first. ++ * Nothing listens on these ports; no traffic is needed. */ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_set_servers_csv(channel, "127.0.0.1:53101,127.0.0.1:53102," ++ "127.0.0.1:53103")); ++ ++ NestedReconfigureData data; ++ data.channel = channel; ++ data.nested = false; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, NestedReconfigureCallback, ++ &data, NULL)); ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel, "")); ++ EXPECT_TRUE(data.nested); ++ ++ ares_destroy(channel); ++} ++ ++// The same nesting, but the nested reconfiguration *installs* a server rather ++// than clearing the list. Restarting the removal walk from the head then ++// re-judges that server against the outer call's list, decides it is stale and ++// deletes it -- so ares_set_servers_csv() returns success and leaves the ++// channel with no servers at all. Memory-safe and still wrong. ++static void NestedReplaceCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ NestedReconfigureData *data = static_cast<NestedReconfigureData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ if (data->nested) { ++ return; ++ } ++ data->nested = true; ++ // Installs a server the outer loop has never seen. ++ ares_set_servers_csv(data->channel, "127.0.0.1:53201"); ++} ++ ++TEST_F(LibraryTest, NestedReconfigureInCallbackKeepsNestedServers) { ++ ares_channel_t *channel = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS; ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts, optmask)); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel, "127.0.0.1:53101")); ++ ++ NestedReconfigureData data; ++ data.channel = channel; ++ data.nested = false; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(channel, "www.example.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, NestedReplaceCallback, &data, ++ NULL)); ++ ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel, "127.0.0.1:53301")); ++ EXPECT_TRUE(data.nested); ++ ++ // The nested call reconciled the whole list; what it installed must survive. ++ char *csv = ares_get_servers_csv(channel); ++ ASSERT_NE(nullptr, csv); ++ EXPECT_STREQ("127.0.0.1:53201", csv); ++ ares_free_string(csv); ++ ++ ares_destroy(channel); ++} ++ + TEST_P(MockUDPChannelTest, CancelInCallbackNoDoubleFree) { + DNSPacket reply; + reply.set_response().set_aa() +@@ -1940,6 +3007,7 @@ TEST_P(MockUDPChannelTest, CancelInCallbackNoDoubleFree) { + CancelInCbData data; + data.channel = channel_; + data.done = false; ++ data.cbs = 0; + ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN, ARES_REC_TYPE_A, + CancelChannelCallback, &data, NULL); + Process();meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket-server-state-notifications.patch new file +393
-0@@ -0,0 +1,393 @@ +From f944c4718fd672c9c0befd65a340abb2ccfc1a31 Mon Sep 17 00:00:00 2001 +From: Brad House <[email protected]> +Date: Sun, 18 Oct 2026 20:28:11 -0400 +Subject: [PATCH] Deliver deferred socket/server-state notifications only from + the outermost public call (#1340) +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Backport of f023a52a (#1340) to v1.34. + +The recent change that queues socket-state and server-state +notifications delivers them, and runs the deferred cleanup, only from +the outermost `ares_deferred_cb_leave()`. `ares_send_nolock()` brackets +`ares_send_query()` itself, but most public lookup functions never +opened a region of their own. When reached from `ares_getaddrinfo()`, +`ares_query_dnsrec()` and similar, the bracket inside +`ares_send_nolock()` was therefore the outermost one. Notifications were +then delivered from inside an internal call while the library was still +in the middle of the public call, and its callers were still using their +lookup state (the host query, the query id out-pointer, ...). A +server-state callback that reconfigures the servers, which +`ares_set_server_state_callback(3)` documents as allowed, could end that +lookup before the caller was done with it. + +This is a regression in unreleased code. No released version is +affected. + +The outermost region is now opened by the public entry points +themselves, under the channel lock: + +- `ares_send_dnsrec()` (also used by `ares_send()`) +- `ares_query_dnsrec()` (also used by `ares_query()`) +- `ares_search()`, `ares_search_dnsrec()` +- `ares_getaddrinfo()` (also used by `ares_gethostbyname()`) +- `ares_gethostbyaddr()`, `ares_getnameinfo()` +- `ares_process_pending_write()`: a failed flush closes the connection +and fails its queries over, and this function had no bracket at all. + +`ares_process_fds()` (and through it +`ares_process()`/`ares_process_fd()`), `ares_cancel()`, `ares_destroy()` +and the server configuration functions already had their own. The nested +bracket in `ares_send_nolock()` stays, because it keeps connections and +servers unlinked during the send readable until the send returns. +Internal sends made from query callbacks (search next domain, +getaddrinfo next lookup, requeue, server probes) already run inside the +processing bracket. + +The comments on `ares_deferred_cb_enter()` and the flush now state the +rule: the outermost region belongs to the public entry point, never to +an internal helper. + +The documented behaviour (notifications arrive before the outermost +c-ares call returns) does not change, so no man page changes. + +Two new `LibraryTest` cases in `test/ares-test-mock.cc`. The IPv6 server +cannot get a socket, so the first send fails over to the IPv4 server, +and a server-state callback then reconfigures the servers: + +- `ServerFailoverDuringGetAddrInfo`: the lookup completes cleanly. It +fails under ASAN without this change. +- `ServerFailoverDuringQuery`: the query id from `ares_query_dnsrec()` +is already reported by the time the query callback runs. It fails +without this change. + +The full non-live suite passes under ASAN/UBSAN and with +`-DCARES_WERROR=ON`. + +Signed-off-by: Brad House <[email protected]> +Upstream-Status: Backport [https://github.com/c-ares/c-ares/commit/f944c4718fd672c9c0befd65a340abb2ccfc1a31] +GHSA: GHSA-wpr3-8q3r-4q8c, GHSA-97vq-4mxv-7vhm, GHSA-mw23-pfg6-2x9r, GHSA-rf6g-ww74-97qh, GHSA-v33j-65h4-4ch8, GHSA-pj7p-v3cw-hq66 +--- + src/lib/ares_conn.c | 4 +- + src/lib/ares_getaddrinfo.c | 5 ++ + src/lib/ares_gethostbyaddr.c | 5 ++ + src/lib/ares_getnameinfo.c | 5 ++ + src/lib/ares_private.h | 10 ++++ + src/lib/ares_process.c | 6 ++ + src/lib/ares_query.c | 5 ++ + src/lib/ares_search.c | 10 ++++ + src/lib/ares_send.c | 13 ++++- + test/ares-test-mock.cc | 106 +++++++++++++++++++++++++++++++++++ + 10 files changed, 166 insertions(+), 3 deletions(-) + +diff --git a/src/lib/ares_conn.c b/src/lib/ares_conn.c +index 78f57e7d10..4cc0d1871d 100644 +--- a/src/lib/ares_conn.c ++++ b/src/lib/ares_conn.c +@@ -94,7 +94,9 @@ static void ares_deferred_cb_flush(ares_channel_t *channel) + { + /* Only the outermost frame delivers: anything above us on the stack may + * still be holding the connection a reentrant call from one of these +- * callbacks could free. ++ * callbacks could free, or lookup state of its own that a callback could ++ * end. That is why the outermost frame belongs to the public entry point ++ * and never to an internal helper; see ares_deferred_cb_enter(). + * + * The dispatch below counts as a nesting level of its own. Without that, a + * callback that calls back into the library re-enters here through the +diff --git a/src/lib/ares_getaddrinfo.c b/src/lib/ares_getaddrinfo.c +index 6d9e21af4b..5d9ddb311e 100644 +--- a/src/lib/ares_getaddrinfo.c ++++ b/src/lib/ares_getaddrinfo.c +@@ -762,7 +762,12 @@ void ares_getaddrinfo(ares_channel_t *channel, const char *name, + return; + } + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + ares_getaddrinfo_int(channel, name, service, hints, callback, arg); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } + +diff --git a/src/lib/ares_gethostbyaddr.c b/src/lib/ares_gethostbyaddr.c +index 69c509ab11..b314359331 100644 +--- a/src/lib/ares_gethostbyaddr.c ++++ b/src/lib/ares_gethostbyaddr.c +@@ -112,7 +112,12 @@ void ares_gethostbyaddr(ares_channel_t *channel, const void *addr, int addrlen, + return; + } + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + ares_gethostbyaddr_nolock(channel, addr, addrlen, family, callback, arg); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } + +diff --git a/src/lib/ares_getnameinfo.c b/src/lib/ares_getnameinfo.c +index 0159354530..7365e9ed82 100644 +--- a/src/lib/ares_getnameinfo.c ++++ b/src/lib/ares_getnameinfo.c +@@ -194,7 +194,12 @@ void ares_getnameinfo(ares_channel_t *channel, const struct sockaddr *sa, + } + + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + ares_getnameinfo_int(channel, sa, salen, flags_int, callback, arg); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } + +diff --git a/src/lib/ares_private.h b/src/lib/ares_private.h +index 0a1029e0f8..9610ba7dab 100644 +--- a/src/lib/ares_private.h ++++ b/src/lib/ares_private.h +@@ -676,6 +676,16 @@ void ares_server_reap(ares_server_t *server); + * Nesting is counted; queued notifications are delivered by the outermost + * leave. + * ++ * The outermost region must be opened by the public entry point itself ++ * (ares_send_dnsrec(), ares_query_dnsrec(), ares_search(), ares_getaddrinfo(), ++ * ares_process_fds(), ares_set_servers_*(), ...), with the channel lock ++ * held, around everything it does. Internal helpers such as ++ * ares_send_nolock() open nested regions of their own, but their callers ++ * often keep using lookup state afterwards -- a host or search query, a ++ * query id out-pointer -- that a callback reentering the library may end ++ * and free. If such a helper were the outermost region, its leave would ++ * run the callbacks while that state is still in use. ++ * + * \param[in] channel Initialized ares channel. + */ + void ares_deferred_cb_enter(ares_channel_t *channel); +diff --git a/src/lib/ares_process.c b/src/lib/ares_process.c +index 82f53610fb..1569408b1b 100644 +--- a/src/lib/ares_process.c ++++ b/src/lib/ares_process.c +@@ -490,6 +490,11 @@ void ares_process_pending_write(ares_channel_t *channel) + return; + } + ++ /* A failed flush closes the connection and fails its queries over, which ++ * notifies. Deliver once we are done walking the servers, and keep ++ * anything unlinked meanwhile readable until then. */ ++ ares_deferred_cb_enter(channel); ++ + /* Set as untriggerd before calling into ares_conn_flush(), this is + * because its possible ares_conn_flush() might cause additional data to + * be enqueued if there is some form of exception so it will need to recurse. +@@ -513,6 +518,7 @@ void ares_process_pending_write(ares_channel_t *channel) + } + } + ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } + +diff --git a/src/lib/ares_query.c b/src/lib/ares_query.c +index ca3b6a9b73..0cf7f70760 100644 +--- a/src/lib/ares_query.c ++++ b/src/lib/ares_query.c +@@ -125,7 +125,12 @@ ares_status_t ares_query_dnsrec(ares_channel_t *channel, const char *name, + } + + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + status = ares_query_nolock(channel, name, dnsclass, type, callback, arg, qid); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + return status; + } +diff --git a/src/lib/ares_search.c b/src/lib/ares_search.c +index c605caf42c..ebc3d91e20 100644 +--- a/src/lib/ares_search.c ++++ b/src/lib/ares_search.c +@@ -463,7 +463,12 @@ void ares_search(ares_channel_t *channel, const char *name, int dnsclass, + } + + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + ares_search_int(channel, dnsrec, ares_dnsrec_convert_cb, carg); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + + ares_dns_record_destroy(dnsrec); +@@ -481,7 +486,12 @@ ares_status_t ares_search_dnsrec(ares_channel_t *channel, + } + + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + status = ares_search_int(channel, dnsrec, callback, arg); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + + return status; +diff --git a/src/lib/ares_send.c b/src/lib/ares_send.c +index 2e0482bf19..dd4a28544b 100644 +--- a/src/lib/ares_send.c ++++ b/src/lib/ares_send.c +@@ -246,8 +246,12 @@ ares_status_t ares_send_nolock(ares_channel_t *channel, ares_server_t *server, + /* Perform the first query action. */ + + /* Sending opens and writes to connections and can mark a server down, both +- * of which notify. Deliver those once we are done with the connection and +- * the server, not part-way through. */ ++ * of which notify. This bracket keeps any connection or server unlinked ++ * during the send readable until it returns. It is never the outermost ++ * one: every public entry point that can get here, and query processing, ++ * holds its own, so delivery waits until the caller -- which may still be ++ * holding its own lookup state, as ares_getaddrinfo() does -- has unwound ++ * too. */ + ares_deferred_cb_enter(channel); + status = ares_send_query(server, query, &now); + ares_deferred_cb_leave(channel); +@@ -271,9 +275,14 @@ ares_status_t ares_send_dnsrec(ares_channel_t *channel, + } + + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + + status = ares_send_nolock(channel, NULL, 0, dnsrec, callback, arg, qid); + ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + + return status; +diff --git a/test/ares-test-mock.cc b/test/ares-test-mock.cc +index 2158c5a913..b848c02434 100644 +--- a/test/ares-test-mock.cc ++++ b/test/ares-test-mock.cc +@@ -2533,6 +2533,112 @@ TEST_F(LibraryTest, ReconfigureInSockStateCallbackNoUseAfterFree) { + data.channel = nullptr; /* don't reconfigure from teardown notifications */ + ares_destroy(chan); + } ++// Server-state notifications raised while a public call is issuing its first ++// request must not be delivered until that whole call has unwound. A ++// failover handler that reconfigures the servers ends the in-flight request, ++// and with it the state the issuing call still has to finish with. The IPv6 ++// server cannot get a socket, so the first send fails over to the IPv4 one ++// and queues a failure notification for the handler below. ++static ares_socket_t FailoverNoIPv6Socket(int af, int type, int protocol, ++ void *user_data) ++{ ++ (void)user_data; ++ if (af == AF_INET6) { ++ return ARES_SOCKET_BAD; ++ } ++ return socket(af, type, protocol); ++} ++ ++static void FailoverOnServerDown(const char *server_string, ares_bool_t success, ++ int flags, void *data) ++{ ++ (void)server_string; ++ (void)flags; ++ if (!success) { ++ ares_set_servers_csv(static_cast<ares_channel_t *>(data), ++ "127.0.0.1:53063"); ++ } ++} ++ ++static ares_channel_t *FailoverChannel(ares_socket_functions *funcs) ++{ ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_init_options(&chan, &opts, ARES_OPT_TRIES | ARES_OPT_FLAGS)); ++ if (chan == nullptr) { ++ return nullptr; ++ } ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_set_servers_csv(chan, "[::1]:53061,127.0.0.1:53062")); ++ ares_set_server_state_callback(chan, FailoverOnServerDown, chan); ++ ++ memset(funcs, 0, sizeof(*funcs)); ++ funcs->asocket = FailoverNoIPv6Socket; ++ ares_set_socket_functions(chan, funcs, NULL); ++ return chan; ++} ++ ++TEST_F(LibraryTest, ServerFailoverDuringGetAddrInfo) { ++ ares_socket_functions funcs; ++ struct ares_addrinfo_hints hints; ++ AddrInfoResult result; ++ ares_channel_t *chan = FailoverChannel(&funcs); ++ ASSERT_NE(nullptr, chan); ++ ++ memset(&hints, 0, sizeof(hints)); ++ hints.ai_family = AF_INET; ++ ares_getaddrinfo(chan, "www.google.com", NULL, &hints, AddrInfoCallback, ++ &result); ++ ++ ares_cancel(chan); ++ EXPECT_TRUE(result.done_); ++ ares_destroy(chan); ++} ++ ++// Same, through ares_query_dnsrec(): the query id must already have been ++// reported to the caller by the time the query can complete. ++struct FailoverQueryData { ++ unsigned short qid; ++ unsigned short qid_at_callback; ++ bool done; ++}; ++ ++static void FailoverQueryCallback(void *data, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) ++{ ++ FailoverQueryData *d = static_cast<FailoverQueryData *>(data); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ d->qid_at_callback = d->qid; ++ d->done = true; ++} ++ ++TEST_F(LibraryTest, ServerFailoverDuringQuery) { ++ ares_socket_functions funcs; ++ FailoverQueryData data; ++ ares_channel_t *chan = FailoverChannel(&funcs); ++ ASSERT_NE(nullptr, chan); ++ ++ data.qid = 0; ++ data.qid_at_callback = 0; ++ data.done = false; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(chan, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, FailoverQueryCallback, &data, ++ &data.qid)); ++ ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ(data.qid, data.qid_at_callback); ++ ares_destroy(chan); ++} ++ + + // Deferring the notifications must not lose them: every public entry point + // that closes a socket still has to tell the application about it before itmeta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand-callbacks-their-own-copy.patch new file +1054
-0@@ -0,0 +1,1054 @@ +From a21160045f9d7039225fa285be7cb1b02173a3a5 Mon Sep 17 00:00:00 2001 +From: Brad House <[email protected]> +Date: Sun, 18 Oct 2026 22:12:09 -0400 +Subject: [PATCH] [Backport v1.34] ares_qcache: hand callbacks their own copyLines of this file are not shown. View the raw message.
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-down-by-application.patch new file +1107
-0Lines of this file are not shown. View the raw message.
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-an-answer-on-the-right-connection.patch new file +575
-0Lines of this file are not shown. View the raw message.
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-channel-config-under-lock.patch new file +243
-0Lines of this file are not shown. View the raw message.
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-prefix-length.patch new file +364
-0Lines of this file are not shown. View the raw message.
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-UDP-answers-from-server-port.patch new file +178
-0Lines of this file are not shown. View the raw message.
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb +8
-1Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
2.43.0
-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#131454): https://lists.openembedded.org/g/openembedded-devel/message/131454
Mute This Topic: https://lists.openembedded.org/mt/121953673/3619737
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/openembedded-devel/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-
Part of the diff is not shown here. View the raw message.
Can we rather do the upgrade instead? I was planning to send it for Wrynose branch eventually.
Is there anything in "Changes" section of https://github.com/c-ares/c-ares/releases/tag/v1.34.9 which would prevent it?
Peter
Quoted 7,136 lines
-----Original Message-----
From: [email protected] <openembedded- [email protected]> On Behalf Of Ankur Tyagi via lists.openembedded.org
Sent: Tuesday, October 27, 2026 8:04 AM
To: [email protected]
Cc: [email protected]
Subject: Re: [oe] [meta-oe][blacksail][PATCH] c-ares: backport security fixes from 1.34.9Hi Jason,
Thanks for submitting the patch.
Though the patches LGTM, I am seeing increased ptest failures.
Existing recipe ptest status:
[==========] 1093 tests from 41 test suites ran.[ PASSED ] 1092 tests. [ FAILED ] 1 test, listed below: [ FAILED ] DefaultChannelTest.TimeoutValueptest status after applying your patch:
[==========] 1174 tests from 47 test suites ran.[ PASSED ] 1170 tests. [ FAILED ] 4 tests, listed below: [ FAILED ] DefaultChannelTest.TimeoutValue [ FAILED ] SockStateDeliveryTest.CancelReportsSocketClose [ FAILED ] SockStateDeliveryTest.SetServersReportsSocketClose [ FAILED ] SockStateDeliveryTest.DestroyReportsSocketClosePerhaps some other commits also need backporting?
Can please look into this?cheers
AnkurQuoted 2 lines · J. S. via lists.openembedded.org
Add the eight unique upstream commits that fix the security advisories
addressed in c-ares 1.34.9.
Quoted 1 line
Several advisories share commits, so patches are ordered by commit rather
than duplicated per advisory; each patch lists the GHSA IDs it resolves.
Quoted 15 lines
Assisted-by opencode (big-pickle)Signed-off-by Jason Schonberg <[email protected]>--- ...troy-detach-queries-before-callbacks.patch | 2603 +++++++++++++++++ ...ed-socket-server-state-notifications.patch | 393 +++ ...qcache-hand-callbacks-their-own-copy.patch | 1054 +++++++ ...connections-torn-down-by-application.patch | 1107 +++++++ ...pt-an-answer-on-the-right-connection.patch | 575 ++++ ...tions-read-channel-config-under-lock.patch | 243 ++ ...res_inet_pton-reject-a-prefix-length.patch | 364 +++ ...nt-drop-UDP-answers-from-server-port.patch | 178 ++ .../recipes-support/c-ares/c-ares_1.34.8.bb | 8 + 9 files changed, 6525 insertions(+) create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-detach-queries-before-callbacks.patch
Quoted 1 line
create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket-server-state-notifications.patch
Quoted 1 line
create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand-callbacks-their-own-copy.patch
Quoted 1 line
create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-down-by-application.patch
Quoted 1 line
create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-an-answer-on-the-right-connection.patch
Quoted 1 line
create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-channel-config-under-lock.patch
Quoted 1 line
create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-prefix-length.patch
Quoted 1 line
create mode 100644 meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-UDP-answers-from-server-port.patch
Review
Quoted 2 lines
(quoted diff) +0
-0ares_destroy-detach-queries-before-callbacks.patch b/meta-oe/recipes- support/c-ares/c-ares/0001-ares_cancel-ares_destroy-detach-queries-before- callbacks.patch
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 4 lines
new file mode 100644
index 0000000000..385760f5bcmeta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +0
-0detach-queries-before-callbacks.patch
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +1
-0@@ -0,0 +1,2603 @@ +From 7f408deb128fbdcce78122322734ffaa01cad9ae Mon Sep 17 00:00:002001
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 4
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +3
-0+From: Brad House <[email protected]> +Date: Sun, 18 Oct 2026 18:15:41 -0400 +Subject: [PATCH] ares_cancel/ares_destroy: detach queries before runninguser
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 10
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +6
-0+ callbacks +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Backport of dd62bdfca52ba5c37e88a1469c0a4c5e4a568ccb to v1.34. Seethat
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 23
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +13
-0+commit for the full description; it hardens reentrant teardown from +callbacks (ares_cancel(), ares_destroy(), server reconfiguration, and +socket/server state notifications). + +Conflicts were whitespace/alignment only (v1.34 predates the clang-format +reformat in #1238) and were reformatted to v1.34's style. + +Also carries the test portion of #1334 that applies to the tests this +introduces: dead-server tests use 127.0.0.1:53055/53056 instead of +127.0.0.55/56, which only routes on Linux. + +Signed-off-by: Brad House <[email protected]> +Upstream-Status: Backport [https://github.com/c-ares/c-ares/commit/7f408deb128fbdcce78122322734ffaa01cad9ae]
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 24
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +1
-0+GHSA: GHSA-wpr3-8q3r-4q8c, GHSA-97vq-4mxv-7vhm, GHSA-mw23-pfg6-2x9r, GHSA-rf6g-ww74-97qh, GHSA-v33j-65h4-4ch8, GHSA-pj7p-v3cw-hq66
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +13
-0+--- + docs/ares_cancel.3 | 17 + + docs/ares_destroy.3 | 7 + + docs/ares_init_options.3 | 15 + + docs/ares_set_server_state_callback.3 | 31 +- + docs/ares_set_servers_csv.3 | 17 + + src/lib/ares_cancel.c | 25 +- + src/lib/ares_close_sockets.c | 82 +- + src/lib/ares_conn.c | 217 ++++- + src/lib/ares_conn.h | 37 +- + src/lib/ares_destroy.c | 85 +- + src/lib/ares_private.h | 110 ++- + src/lib/ares_process.c | 235 +++++-meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 47
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0+ src/lib/ares_send.c | 35 + + src/lib/ares_update_servers.c | 42 +- + test/ares-test-mock.cc | 1068 +++++++++++++++++++++++++ + 15 files changed, 1959 insertions(+), 64 deletions(-) + +diff --git a/docs/ares_cancel.3 b/docs/ares_cancel.3 +index 967a946b49..069f226c10 100644 +--- a/docs/ares_cancel.3 ++++ b/docs/ares_cancel.3 +@@ -21,6 +21,23 @@ These calls give the callbacks a chance to clean upany state which might have
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 59
Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +12
-0+ been stored in their arguments. If such a callback invocation adds a new + request to the channel, that request will \fInot\fP be cancelled by the + current invocation of \fBares_cancel(3)\fP. ++.SH REENTRANCY ++A callback invoked by ++.BR ares_cancel (3) ++may call c-ares functions on the same channel. The channel lock is recursive, ++and each query is unlinked from the library's internal structures before its ++callback runs, so a reentrant call cannot encounter a query that is part-way ++through being torn down. ++ ++This includes starting new lookups, as described above, and reconfiguringthe
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 65
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +6
-0++server list with ++.BR ares_set_servers_csv (3) ++and related functions. ++ ++.SH AVAILABILITY ++The reentrancy guarantee described above applies as of c-ares version1.35.0,
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 76
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +11
-0++and c-ares version 1.34.9 on the 1.34 branch. ++ + .SH SEE ALSO + .BR ares_init (3) + .BR ares_destroy (3) +diff --git a/docs/ares_destroy.3 b/docs/ares_destroy.3 +index d1984b7cc9..0b6380e609 100644 +--- a/docs/ares_destroy.3 ++++ b/docs/ares_destroy.3 +@@ -20,6 +20,13 @@ by the channel. + channel, passing a status of \fIARES_EDESTRUCTION\fP. These calls givethe
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 77
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +1
-0+ callbacks a chance to clean up any state which might have been stored intheir
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 78
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +1
-0+ arguments. A callback must not add new requests to a channel beingdestroyed.
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 80
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +2
-0++Such a request is refused: the request is not started, and its callback is ++invoked with a status of \fIARES_EDESTRUCTION\fP so that any stateattached to
Quoted 25 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +25
-0++its argument can be released. A request made from within that notification is ++refused without a further notification, so that a callback which responds to ++failure by retrying cannot recurse without bound; its argument is not handed ++back. This applies as of c-ares version 1.35.0, and c-ares version 1.34.9 on ++the 1.34 branch. + + There is no ability to make this function thread-safe. No additional calls + using this channel may be made once this function is called. +diff --git a/docs/ares_init_options.3 b/docs/ares_init_options.3 +index 9b3b481535..e6f6ee8ba6 100644 +--- a/docs/ares_init_options.3 ++++ b/docs/ares_init_options.3 +@@ -231,6 +231,21 @@ will be passed the socket whose state has changed; + will be set to true if the socket should listen for read events, and + .I writable + will be set to true if the socket should listen for write events. ++ ++This callback may call c-ares functions on the same channel, including ++.BR ares_cancel (3) ++and the server configuration functions. Notifications are delivered once the ++library has finished the work that produced them rather than part-way through ++it, so a reentrant call cannot close a socket the library is still using. They ++arrive in the order the state changes occurred, and always before the ++outermost c-ares call returns. A call made from within a callback is not the ++outermost one, so notifications it produces arrive after it has returned.meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 115
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++.BR ares_destroy (3) ++is no exception: the closure of every socket the channel still holds is ++reported before it returns, and the callback is never invoked afterwards. ++This applies as of c-ares version 1.35.0, and c-ares version 1.34.9 on the ++1.34 branch. ++ + The value of + .I sock_state_cb_data + will be passed as the +diff --git a/docs/ares_set_server_state_callback.3b/docs/ares_set_server_state_callback.3
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 119
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +4
-0+index 04fbde4c7a..e95db327ce 100644 +--- a/docs/ares_set_server_state_callback.3 ++++ b/docs/ares_set_server_state_callback.3 +@@ -47,6 +47,31 @@ the DNS servers used by an ares channel. Forexample, it can be used to track
Quoted 26 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +26
-0+ metrics about the numbers and types of queries sent to each server or to + detect when a server is uncontactable or unhealthy. + ++.SH REENTRANCY ++The server state callback may call c-ares functions on the same channel, ++including ++.BR ares_cancel (3) ++and the server configuration functions. The channel lock is recursive and the ++library holds no query, connection or server state across the callback. ++ ++Notifications are queued and delivered once the library has finished the work ++that produced them, rather than part-way through it. A notification is ++therefore observed after the processing pass that generated it, not during. ++ ++The callback and its ++.I user_data ++are read when the notification is delivered, not when it is queued. An ++application that replaces its callback between those two points will see the ++notification delivered to the new one; unregistering by passing a ++.B NULL ++callback discards any notification still queued. ++ ++This applies to a replacement made from within a c-ares callback, where the ++channel lock is held. ++.BR ares_set_server_state_callback (3) ++does not take the channel lock, so replacing the callback from another threadmeta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 155
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++while the channel is processing is not synchronized against delivery. ++ + .SH FLAGS + .TP 5 + .B ARES_SERV_STATE_UDP +@@ -58,5 +83,9 @@ Indicates that the query was tried over TCP. + .SH AVAILABILITY + This function was first introduced in c-ares version 1.29.0. + ++The reentrancy guarantee described above applies as of c-ares version1.35.0,
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 166
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +11
-0++and c-ares version 1.34.9 on the 1.34 branch. ++ + .SH SEE ALSO +-.BR ares_get_servers_csv (3) ++.BR ares_get_servers_csv (3), ++.BR ares_cancel (3) +diff --git a/docs/ares_set_servers_csv.3 b/docs/ares_set_servers_csv.3 +index f1435143f5..f9f5b82e28 100644 +--- a/docs/ares_set_servers_csv.3 ++++ b/docs/ares_set_servers_csv.3 +@@ -126,6 +126,23 @@ are identical. Prior versions would simply omit portsin \fBares_set_servers_cs
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 171
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +5
-0+ but due to the addition of link local interface support, this difference was + removed. + ++.SH REENTRANCY ++These functions may be called from within a c-ares callback. Removing aserver
Quoted 73 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +73
-0++closes its connections, which ends the lookups that were using it and invokes ++their callbacks, so a reconfiguration can be requested from inside one. ++ ++A nested call reconciles the whole server list, and the outer call leaves that ++result in place rather than deleting the servers the nested call installed. ++The consequence is that the outer call's own list does not survive, even ++though it returns ++.B ARES_SUCCESS: ++the last configuration applied is the one the channel keeps. An application ++that reconfigures from a callback should therefore not assume the list it ++passed to the outer call is in effect when that call returns. ++ ++This applies as of c-ares version 1.35.0, and c-ares version 1.34.9 on the ++1.34 branch. ++ + .SH EXAMPLE + .nf + 192.168.1.100,[fe80::1]:53%eth0,dns://192.168.1.1?tcpport=1153 +diff --git a/src/lib/ares_cancel.c b/src/lib/ares_cancel.c +index 75600dea6b..aa167e4486 100644 +--- a/src/lib/ares_cancel.c ++++ b/src/lib/ares_cancel.c +@@ -38,10 +38,10 @@ void ares_cancel(ares_channel_t *channel) + } + + ares_channel_lock(channel); ++ ares_deferred_cb_enter(channel); + + if (ares_llist_len(channel->all_queries) > 0) { + ares_llist_node_t *node = NULL; +- ares_llist_node_t *next = NULL; + + /* Swap list heads, so that only those queries which were present on entry + * into this function are cancelled. New queries added by callbacks of +@@ -57,21 +57,29 @@ void ares_cancel(ares_channel_t *channel) + goto done; /* LCOV_EXCL_LINE: OutOfMemory */ + } + +- node = ares_llist_node_first(list_copy); +- while (node != NULL) { ++ /* Re-read the head each iteration rather than caching the next node: a ++ * callback below may cause other queries in this same list to be ended ++ * and their nodes destroyed. */ ++ while ((node = ares_llist_node_first(list_copy)) != NULL) { + ares_query_t *query; + +- /* Cache next since this node is being deleted */ +- next = ares_llist_node_next(node); ++ query = ares_llist_node_claim(node); + +- query = ares_llist_node_claim(node); ++ /* Must come first: node_claim() already destroyed this node, so ++ * ares_detach_query() would otherwise destroy it a second time. */ + query->node_all_queries = NULL; + ++ /* Detach from queries_by_qid, the connection and the timeout list ++ * before running user code. Otherwise a reentrant teardown from ++ * within the callback -- ares_set_servers_csv(), or a new query ++ * hitting a connection error -- still finds this query, ends it a ++ * second time and frees it, and the ares_free_query() below then ++ * double-frees. */ ++ ares_detach_query(query); ++ + /* NOTE: its possible this may enqueue new queries */ + query->callback(query->arg, ARES_ECANCELLED, 0, NULL); + ares_free_query(query); +- +- node = next; + } + + ares_llist_destroy(list_copy); +@@ -83,5 +91,6 @@ void ares_cancel(ares_channel_t *channel) + ares_queue_notify_empty(channel);meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 254
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0+ + done: ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } +diff --git a/src/lib/ares_close_sockets.c b/src/lib/ares_close_sockets.c +index 7bb72c31ec..6be662fdd3 100644 +--- a/src/lib/ares_close_sockets.c ++++ b/src/lib/ares_close_sockets.c +@@ -41,12 +41,47 @@ static void ares_requeue_queries(ares_conn_t*conn,
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 260
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +6
-0+ } + } + ++/*! Release a connection's memory. Split from the unlink below so that the ++ * two can happen at different times: a caller further up the stack may still ++ * hold this pointer, and reading a detached connection is safe wherereading
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +4
-0++ * a freed one is not. */ ++void ares_conn_reap(ares_conn_t *conn) ++{ ++ if (conn == NULL) {meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 274
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ ares_buf_destroy(conn->in_buf); ++ ares_buf_destroy(conn->out_buf); ++ ares_llist_destroy(conn->queries_to_conn); ++ ares_free(conn); ++} ++ + void ares_close_connection(ares_conn_t *conn, ares_status_t
requeue_status)Quoted 21 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +21
-0+ { +- ares_server_t *server = conn->server; +- ares_channel_t *channel = server->channel; ++ ares_server_t *server; ++ ares_channel_t *channel; ++ ++ if (conn == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ /* Already torn down and awaiting reclamation. Reaching here twice is ++ * ordinary once a connection can outlive its teardown: a caller holding it ++ * from before -- handle_conn_error() after a failed write, say -- closes it ++ * again without knowing. Doing the work twice would requeue queries that ++ * have already moved and would put this connection on the pending list a ++ * second time, which is a double free. */ ++ if (conn->flags & ARES_CONN_FLAG_DEAD) { ++ return; ++ } + +- /* Unlink */meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 305
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ server = conn->server; ++ channel = server->channel; ++ ++ /* Unlink from everything that can find this connection. After this it is ++ * unreachable by lookup, which is what stops anything new from picking it ++ * up; anything that already holds it is handled by the deferral below. */ + ares_llist_node_claim( + ares_htable_asvp_get_direct(channel->connnode_by_socket, conn->fd)); + ares_htable_asvp_remove(channel->connnode_by_socket, conn->fd); +@@ -55,19 +90,40 @@ void ares_close_connection(ares_conn_t *conn,
ares_status_trequeue_status)Quoted 63 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +63
-0+ server->tcp_conn = NULL; + } + +- ares_buf_destroy(conn->in_buf); +- ares_buf_destroy(conn->out_buf); ++ /* Marked dead as soon as it is unlinked, before anything below can run ++ * application code. ares_requeue_queries() ends queries terminally once ++ * their retries are exhausted, which invokes the application's callback; if ++ * the flag were set after that, a second ares_close_connection() reaching ++ * this connection in the meantime would run the body again -- closing the ++ * descriptor twice and listing the connection for reclamation twice, which ++ * is a double free. It also gives ares_close_sockets() its termination ++ * condition: dead implies unlinked, so its re-read-the-head loop cannot see ++ * a dead connection and spin. */ ++ conn->flags |= ARES_CONN_FLAG_DEAD; + + /* Requeue queries to other connections */ + ares_requeue_queries(conn, requeue_status); + +- ares_llist_destroy(conn->queries_to_conn); +- +- ares_conn_sock_state_cb_update(conn, ARES_CONN_STATE_NONE); +- +- ares_socket_close(channel, conn->fd); ++ /* Hands the descriptor to the queued notification when one is due, so the ++ * application is told to stop watching it before it is closed. Otherwise ++ * nothing will carry it and we close it here. */ ++ if (!ares_conn_sock_state_cb_close(conn)) { ++ ares_socket_close(channel, conn->fd); ++ } ++ conn->fd = ARES_SOCKET_BAD; ++ ++ /* If a library call is in progress it may be holding this connection -- ++ * read_answers() and ares_send_query_int() both do, across code that can ++ * reach here. Keep it readable until that call unwinds. The buffers and ++ * the query list are freed by ares_conn_reap(), so a stale holder sees an ++ * emptied connection rather than freed memory. */ ++ if (channel->deferred_cb_depth > 0) { ++ conn->next_pending = channel->pending_conns; ++ channel->pending_conns = conn; ++ return; ++ } + +- ares_free(conn); ++ ares_conn_reap(conn); + } + + void ares_close_sockets(ares_server_t *server) +@@ -76,6 +132,10 @@ void ares_close_sockets(ares_server_t *server) + + while ((node = ares_llist_node_first(server->connections)) != NULL) { + ares_conn_t *conn = ares_llist_node_val(node); ++ /* This loop re-reads the head each time and relies on the close below ++ * unlinking it. A dead connection returns early without unlinking, so ++ * one on this list would spin forever holding the channel lock. */ ++ assert(!(conn->flags & ARES_CONN_FLAG_DEAD)); + ares_close_connection(conn, ARES_SUCCESS); + } + } +diff --git a/src/lib/ares_conn.c b/src/lib/ares_conn.c +index 6b315b0548..78f57e7d10 100644 +--- a/src/lib/ares_conn.c ++++ b/src/lib/ares_conn.cmeta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 378
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0+@@ -26,19 +26,210 @@ + */ + #include "ares_private.h" + +-void ares_conn_sock_state_cb_update(ares_conn_t *conn, +- ares_conn_state_flags_t flags) ++/*! A queued socket-state notification. Holds the descriptor and the state, ++ * never the connection, so a queued entry cannot outlive what it refers to. ++ * ++ * When \c owns_fd is set, this message carries the only remaining referenceto
Quoted 30 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +30
-0++ * the descriptor: the connection is gone and the descriptor is deliberately ++ * left open until after the application has been told to stop watching it. ++ * Closing first would let the number be reused -- by application code running ++ * from a deferred query callback, for instance -- before the notification ++ * naming it is delivered. */ ++typedef struct { ++ ares_socket_t fd; ++ ares_bool_t readable; ++ ares_bool_t writable; ++ ares_bool_t owns_fd; ++} ares_sock_state_msg_t; ++ ++static ares_bool_t ares_sock_state_queue(ares_channel_t *channel, ++ const ares_sock_state_msg_t *msg) + { +- ares_channel_t *channel = conn->server->channel; ++ if (channel->sock_state_queue == NULL) { ++ channel->sock_state_queue = ares_array_create(sizeof(*msg), NULL); ++ if (channel->sock_state_queue == NULL) { ++ return ARES_FALSE; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ } ++ ++ if (ares_array_insertdata_last(channel->sock_state_queue, msg) != ++ ARES_SUCCESS) { ++ return ARES_FALSE; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ ++ return ARES_TRUE; ++}meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 418
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ ++/*! Deliver every socket-state notification the channel has queued. Loops ++ * rather than snapshotting: a callback is allowed to re-enter the library and ++ * queue more, and those belong to this same delivery point. */ ++static void ares_sock_state_dispatch(ares_channel_t *channel) ++{ ++ while (ares_array_len(channel->sock_state_queue) > 0) { ++ ares_sock_state_msg_t msg; ++ ++ if (ares_array_claim_at(&msg, sizeof(msg), channel->sock_state_queue,0) !=
Quoted 98 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +98
-0++ ARES_SUCCESS) { ++ break; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } + +- if ((conn->state_flags & ARES_CONN_STATE_CBFLAGS) != flags && +- channel->sock_state_cb) { +- channel->sock_state_cb(channel->sock_state_cb_data, conn->fd, +- flags & ARES_CONN_STATE_READ ? 1 : 0, +- flags & ARES_CONN_STATE_WRITE ? 1 : 0); ++ if (channel->sock_state_cb != NULL) { ++ /* The public callback takes ints. */ ++ channel->sock_state_cb(channel->sock_state_cb_data, msg.fd, ++ msg.readable ? 1 : 0, msg.writable ? 1 : 0); ++ } ++ ++ /* Told first, closed second -- the ordering the application depends on to ++ * keep its descriptor set honest. */ ++ if (msg.owns_fd) { ++ ares_socket_close(channel, msg.fd); ++ } ++ } ++ ++ ares_array_destroy(channel->sock_state_queue); ++ channel->sock_state_queue = NULL; ++} ++ ++static void ares_deferred_cb_flush(ares_channel_t *channel) ++{ ++ /* Only the outermost frame delivers: anything above us on the stack may ++ * still be holding the connection a reentrant call from one of these ++ * callbacks could free. ++ * ++ * The dispatch below counts as a nesting level of its own. Without that, a ++ * callback that calls back into the library re-enters here through the ++ * enter/leave pair on that call, drains the rest of the queue and destroys ++ * it underneath the loop -- and hands the next notification to the ++ * application from inside the current one. With the counter held, the ++ * nested leave returns immediately and whatever it queued is picked up by ++ * the loop that is already running. */ ++ if (channel->deferred_cb_depth > 0) { ++ return; ++ } ++ channel->deferred_cb_depth++; ++ ++ /* Both kinds of deferred notification share this one delivery point. Loop ++ * rather than calling each once: a socket-state callback may put the library ++ * on a path that queues a server-state notification, and the reverse, and ++ * both belong to this delivery point rather than the next one. */ ++ while (ares_array_len(channel->sock_state_queue) > 0 || ++ ares_array_len(channel->server_state_queue) > 0) { ++ ares_sock_state_dispatch(channel); ++ ares_server_state_dispatch(channel); ++ } ++ ++ /* The outermost call is unwinding and the callbacks above have run, so ++ * nothing can still be holding a server or connection that was unlinked ++ * during it. Now they can be freed. */ ++ ares_deferred_reclaim(channel); ++ ++ channel->deferred_cb_depth--; ++} ++ ++void ares_deferred_reclaim(ares_channel_t *channel) ++{ ++ /* Connections first: a pending server's connection list is already empty, ++ * but this keeps the ordering obvious rather than incidental. */ ++ while (channel->pending_conns != NULL) { ++ ares_conn_t *conn = channel->pending_conns; ++ channel->pending_conns = conn->next_pending; ++ conn->next_pending = NULL; ++ ares_conn_reap(conn); ++ } ++ ++ while (channel->pending_servers != NULL) { ++ ares_server_t *server = channel->pending_servers; ++ channel->pending_servers = server->next_pending; ++ server->next_pending = NULL; ++ ares_server_reap(server); ++ } ++} ++ ++void ares_deferred_cb_enter(ares_channel_t *channel) ++{ ++ channel->deferred_cb_depth++; ++} ++ ++void ares_deferred_cb_leave(ares_channel_t *channel) ++{ ++ if (channel->deferred_cb_depth > 0) { ++ channel->deferred_cb_depth--; ++ } ++ ares_deferred_cb_flush(channel); ++} ++ ++ares_bool_t ares_conn_sock_state_cb_close(ares_conn_t *conn) ++{ ++ ares_channel_t *channel = conn->server->channel; ++ ares_sock_state_msg_t msg;meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 526
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ ++ /* No callback to run, so nothing will ever carry this descriptor: the ++ * caller closes it itself, exactly as before. */ ++ if (channel->sock_state_cb == NULL) { ++ return ARES_FALSE; ++ } ++ ++ /* Already reported as unwatched, so no notification is due and no message ++ * will be queued to hold the descriptor. */ ++ if ((conn->state_flags & ARES_CONN_STATE_CBFLAGS) ==
ARES_CONN_STATE_NONE) {Quoted 20 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +20
-0++ return ARES_FALSE; ++ } ++ ++ msg.fd = conn->fd; ++ msg.readable = ARES_FALSE; ++ msg.writable = ARES_FALSE; ++ msg.owns_fd = ARES_TRUE; ++ ++ if (!ares_sock_state_queue(channel, &msg)) { ++ return ARES_FALSE; /* LCOV_EXCL_LINE: OutOfMemory */ + } + + conn->state_flags &= ~((unsigned int)ARES_CONN_STATE_CBFLAGS); ++ ++ return ARES_TRUE; ++} ++ ++void ares_conn_sock_state_cb_update(ares_conn_t *conn, ++ ares_conn_state_flags_t flags) ++{meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 556
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ ares_channel_t *channel = conn->server->channel; ++ ares_sock_state_msg_t msg; ++ ares_bool_t changed = ARES_FALSE; ++ ++ if ((conn->state_flags & ARES_CONN_STATE_CBFLAGS) != flags) { ++ changed = ARES_TRUE; ++ } ++ ++ msg.fd = conn->fd; ++ msg.readable = flags & ARES_CONN_STATE_READ ? ARES_TRUE :
ARES_FALSE;meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 557
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +1
-0++ msg.writable = flags & ARES_CONN_STATE_WRITE ? ARES_TRUE :
ARES_FALSE;Quoted 21 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +21
-0++ msg.owns_fd = ARES_FALSE; /* the connection still owns it */ ++ ++ if (!changed || channel->sock_state_cb == NULL) { ++ conn->state_flags &= ~((unsigned int)ARES_CONN_STATE_CBFLAGS); ++ conn->state_flags |= flags; ++ return; ++ } ++ ++ /* Queue rather than dispatch. This runs in the middle of opening, writing ++ * to or closing a connection, and every caller keeps using that connection ++ * afterwards. The callback is application code that may reentrantly call ++ * ares_cancel() or ares_set_servers_csv(), either of which frees the ++ * connection -- the first through the idle-connection sweep, the second by ++ * destroying the server that owns it. Deliver once no frame still holds ++ * it. ++ * ++ * Only the descriptor and the state are captured, never the connection, and ++ * insertion order is preserved so the application observes the same ++ * sequence it would have seen inline. */ ++ if (!ares_sock_state_queue(channel, &msg)) { ++ return; /* LCOV_EXCL_LINE: OutOfMemory */meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 588
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ } ++ ++ /* Recorded as reported only now: on either failure above the connection ++ * stays eligible to notify again, rather than remembering a state the ++ * application was never told about. */ ++ conn->state_flags &= ~((unsigned int)ARES_CONN_STATE_CBFLAGS); + conn->state_flags |= flags; + } + +@@ -435,6 +626,16 @@ ares_status_t ares_open_connection(ares_conn_t**conn_out,
Quoted 48 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +48
-0+ conn->flags |= ARES_CONN_FLAG_TFO_INITIAL; + } + ++ /* The socket-configure and socket-create callbacks above are application ++ * code, and either may reconfigure the servers -- which destroys the ones ++ * the new configuration omits, including possibly this one. Everything ++ * below dereferences it, starting with conn->server in ++ * ares_conn_set_self_ip(). */ ++ if (!ares_server_in_channel(channel, server)) { ++ status = ARES_ECONNREFUSED; ++ goto done; ++ } ++ + /* Need to store our own ip for DNS cookie support */ + status = ares_conn_set_self_ip(conn, ARES_TRUE); + if (status != ARES_SUCCESS) { +diff --git a/src/lib/ares_conn.h b/src/lib/ares_conn.h +index c332de14b1..fc177b84f6 100644 +--- a/src/lib/ares_conn.h ++++ b/src/lib/ares_conn.h +@@ -49,7 +49,14 @@ typedef enum { + * it). Its in-flight queries continue to drain and it is cleaned up once + * idle. This is a per-connection signal so that a transient failure does + * not evict otherwise-healthy connections to the same server. */ +- ARES_CONN_FLAG_NONEW = 1 << 3 ++ ARES_CONN_FLAG_NONEW = 1 << 3, ++ /*! Connection has been torn down: unlinked from every lookup structure, its ++ * descriptor closed or handed to a pending notification, and its queries ++ * requeued. It survives only so that a caller further up the stack which ++ * captured it before the teardown can still read it; see ++ * ares_deferred_reclaim(). Teardown is idempotent -- a second ++ * ares_close_connection() on a dead connection does nothing. */ ++ ARES_CONN_FLAG_DEAD = 1 << 4 + } ares_conn_flags_t; + + typedef enum { +@@ -84,6 +91,10 @@ struct ares_conn { + + /* list of outstanding queries to this connection */ + ares_llist_t *queries_to_conn; ++ ++ /*! Next connection awaiting reclamation. Set only while this connection is ++ * on channel->pending_conns; see ares_deferred_reclaim(). */ ++ ares_conn_t *next_pending; + }; + + /*! Various buckets for grouping history */ +@@ -168,6 +179,10 @@ struct ares_server {meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 646
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0+ + /* Link back to owning channel */ + ares_channel_t *channel; ++ ++ /*! Next server awaiting reclamation. Set only while this server is on ++ * channel->pending_servers; see ares_deferred_reclaim(). */ ++ ares_server_t *next_pending; + }; + + void ares_close_connection(ares_conn_t *conn, ares_status_t
requeue_status);meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 647
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +1
-0+@@ -185,8 +200,24 @@ ares_status_t ares_conn_flush(ares_conn_t*conn);
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 650
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +3
-0+ ares_conn_err_t ares_conn_read(ares_conn_t *conn, void *data, size_t len, + size_t *read_bytes); + ares_conn_t *ares_conn_from_fd(const ares_channel_t *channel,
ares_socket_tfd);Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +11
-0+-void ares_conn_sock_state_cb_update(ares_conn_t *conn, +- ares_conn_state_flags_t flags); ++ ++/*! Queue the "stop watching this descriptor" notification for a connection ++ * being torn down, handing the descriptor to that queued message. ++ * ++ * The application must be told to stop watching a descriptor while it is ++ * still open -- close first and the number can be handed to something else, ++ * including by application code running from a deferred query callback, ++ * before the notification naming it is delivered. ++ *meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 671
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ * \param[in] conn Connection being closed. ++ * \return ARES_TRUE if the queued notification took the descriptor, in which ++ * case the caller must not close it; ARES_FALSE if no notification is ++ * due, and the caller closes the descriptor itself. ++ */ ++ares_bool_t ares_conn_sock_state_cb_close(ares_conn_t *conn); ++ ++void ares_conn_sock_state_cb_update(ares_conn_t *conn, ++ ares_conn_state_flags_t flags); + ares_conn_err_t ares_socket_recv(ares_channel_t *channel, ares_socket_ts,
Quoted 41 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +41
-0+ ares_bool_t is_tcp, void *data, + size_t data_len, size_t *read_bytes); +diff --git a/src/lib/ares_destroy.c b/src/lib/ares_destroy.c +index 1e5706e0e0..e0534c800d 100644 +--- a/src/lib/ares_destroy.c ++++ b/src/lib/ares_destroy.c +@@ -69,17 +69,31 @@ void ares_destroy(ares_channel_t *channel) + * callbacks need to hold a channel lock. */ + ares_channel_lock(channel); + +- /* Destroy all queries */ +- node = ares_llist_node_first(channel->all_queries); +- while (node != NULL) { +- ares_llist_node_t *next = ares_llist_node_next(node); +- ares_query_t *query = ares_llist_node_claim(node); +- ++ /* Destroy all queries. ++ * ++ * The queries are detached before their callbacks run. Claiming the node ++ * only unlinks a query from all_queries; it stays in queries_by_qid, on ++ * conn->queries_to_conn and in queries_by_timeout. A reentrant teardown ++ * from inside the callback -- ares_set_servers_csv(), or a new query ++ * hitting a connection error -- would otherwise still find the query, end ++ * it a second time (invoking the application callback twice for one ++ * lookup) and free it, and the ares_free_query() below would double-free. ++ * ++ * The loop re-reads the first node each iteration rather than caching the ++ * next one, since a callback can cause other queries in this same list to ++ * be ended and their nodes destroyed. No list swap is needed: sys_up is ++ * already cleared above, so ares_send_nolock() refuses new queries and ++ * nothing can be added while we drain. */ ++ while ((node = ares_llist_node_first(channel->all_queries)) != NULL) { ++ ares_query_t *query = ares_llist_node_claim(node); ++ ++ /* Must come first: node_claim() already destroyed this node, so ++ * ares_detach_query() would otherwise destroy it a second time. */ + query->node_all_queries = NULL; ++ ares_detach_query(query); ++ + query->callback(query->arg, ARES_EDESTRUCTION, 0, NULL); + ares_free_query(query);meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 722
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0+- +- node = next; + } + + ares_queue_notify_empty(channel); +@@ -93,7 +107,28 @@ void ares_destroy(ares_channel_t *channel) + assert(ares_slist_len(channel->queries_by_timeout) == 0); + #endif + ++ /* Tearing down the servers closes every remaining connection.Applications
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 731
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +9
-0++ * rely on being told about those closures so they can drop the descriptors ++ * from their own event loop, so the notifications are still delivered here ++ * rather than discarded -- just after the teardown instead of during it. */ ++ ares_deferred_cb_enter(channel); + ares_destroy_servers_state(channel); ++ ares_deferred_cb_leave(channel); ++ ++ /* The leave above delivered and destroyed both queues and reclaimed ++ * everything the teardown unlinked, so these are normally no-ops. Theyare
Quoted 76 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +76
-0++ * not redundant: if the first insert into a freshly created queue fails, ++ * the array is left allocated and empty, the flush loop never runs because ++ * both queues are empty, and nothing else would ever free it. */ ++ ares_array_destroy(channel->sock_state_queue); ++ channel->sock_state_queue = NULL; ++ ares_array_destroy(channel->server_state_queue); ++ channel->server_state_queue = NULL; ++ ++#ifndef NDEBUG ++ assert(channel->pending_servers == NULL); ++ assert(channel->pending_conns == NULL); ++#endif + + #ifndef NDEBUG + assert(ares_htable_asvp_num_keys(channel->connnode_by_socket) == 0); +@@ -134,17 +169,47 @@ void ares_destroy(ares_channel_t *channel) + ares_free(channel); + } + +-void ares_destroy_server(ares_server_t *server) ++/*! Release a server's memory. Split from ares_destroy_server() for the same ++ * reason ares_conn_reap() is split out: a caller further up the stack may ++ * still hold this pointer. */ ++void ares_server_reap(ares_server_t *server) + { + if (server == NULL) { + return; /* LCOV_EXCL_LINE: DefensiveCoding */ + } + +- ares_close_sockets(server); + ares_llist_destroy(server->connections); + ares_free(server); + } + ++void ares_destroy_server(ares_server_t *server) ++{ ++ ares_channel_t *channel; ++ ++ if (server == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ channel = server->channel; ++ ++ /* Closes and unlinks every connection; each is deferred or reaped by ++ * ares_close_connection() on the same rule applied below. */ ++ ares_close_sockets(server); ++ ++ /* The caller has already unlinked this server from channel->servers, so it ++ * can no longer be found -- ares_server_in_channel() and ++ * ares_slist_node_find() both report it gone, which is what callers check ++ * before acting on one. But a library call in progress may still be ++ * holding the pointer, so keep it readable until that call unwinds. */ ++ if (channel != NULL && channel->deferred_cb_depth > 0) { ++ server->next_pending = channel->pending_servers; ++ channel->pending_servers = server; ++ return; ++ } ++ ++ ares_server_reap(server); ++} ++ + void ares_destroy_servers_state(ares_channel_t *channel) + { + ares_slist_node_t *node; +diff --git a/src/lib/ares_private.h b/src/lib/ares_private.h +index 04d4576ed2..0a1029e0f8 100644 +--- a/src/lib/ares_private.h ++++ b/src/lib/ares_private.h +@@ -147,7 +147,10 @@ typedef struct ares_query ares_query_t; + struct ares_query { + /* Query ID from qbuf, for faster lookup, and current timeout */ + unsigned short qid; /* host byte order */ +- ares_timeval_t ts; /*!< Timestamp query was sent */ ++ /*! Distinguishes this query from any later one handed the same qid. ++ * Query ids are 16-bit and recycled; this counter is not. */meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 817
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ size_t serial; ++ ares_timeval_t ts; /*!< Timestamp query was sent */ + ares_timeval_t timeout; + ares_channel_t *channel; + +@@ -227,6 +230,9 @@ struct ares_channeldata { + /* Server addresses and communications state. Sorted by least consecutive + * failures, followed by the configuration order if failures are equal. */ + ares_slist_t *servers; ++ /*! Bumped by each ares_servers_update(). Lets the stale-server removalloop
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 821
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +4
-0++ * tell that a nested reconfiguration reconciled the list underneath it. */ ++ size_t servers_generation; + + /* random state to use when generating new ids and generating retrypenalties
Quoted 56 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +56
-0+ */ +@@ -237,6 +243,10 @@ struct ares_channeldata { + /* Queries bucketed by qid, for quickly dispatching DNS responses: */ + ares_htable_szvp_t *queries_by_qid; + ++ /*! Monotonic source for ares_query_t.serial. Never reused for the life of ++ * the channel, so a recycled query id cannot impersonate an older query. */ ++ size_t query_serial; ++ + /* Queries bucketed by timeout, for quickly handling timeouts: */ + ares_slist_t *queries_by_timeout; + +@@ -248,6 +258,29 @@ struct ares_channeldata { + + ares_sock_state_cb sock_state_cb; + void *sock_state_cb_data; ++ /*! Socket-state notifications queued during processing, delivered once the ++ * library is no longer holding a connection a reentrant call could free. ++ * Insertion order is preserved, so an application observes the same ++ * sequence of state changes it would have seen inline. */ ++ ares_array_t *sock_state_queue; ++ /*! Depth of nested library entry points that may produce a deferred ++ * application notification. Queues are delivered as this returns to ++ * zero. Shared by every deferred callback kind rather than one counter ++ * per kind, so the delivery rule exists in exactly one place. */ ++ size_t deferred_cb_depth; ++ ++ /*! Servers and connections that have been unlinked from every lookup ++ * structure but not yet freed, because a library call was in progress and ++ * may still hold a pointer to them. Drained by the same flush that ++ * delivers deferred notifications, once no frame can still be holding one. ++ * ++ * This is what makes a stale ares_server_t or ares_conn_t readable rather ++ * than freed for the remainder of the call that captured it. Callers ++ * still revalidate before *acting* on one -- ares_server_in_channel() and ++ * friends -- but a missed check is now "used a server that was removed" ++ * rather than a use-after-free. */ ++ ares_server_t *pending_servers; ++ ares_conn_t *pending_conns; + + ares_sock_create_callback sock_create_cb; + void *sock_create_cb_data; +@@ -295,6 +328,10 @@ struct ares_channeldata { + /* Callback triggered when a server has a successful or failed response */ + ares_server_state_callback server_state_cb; + void *server_state_cb_data; ++ /*! Server-state notifications queued during processing, dispatched once the ++ * library is no longer holding pointers a reentrant callback could free. ++ * See ares_deferred_cb_leave(). */ ++ ares_array_t *server_state_queue; + + /* TRUE if a reinit is pending. Reinit spawns a thread to read the system + * configuration and then apply the configuration since configuration +@@ -308,6 +345,11 @@ struct ares_channeldata { + * system config changes might get triggered and we need a flag to make + * sure we don't take action. */meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 887
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0+ ares_bool_t sys_up; ++ ++ /*! Set while ares_send_nolock() is running the refusal notification for a ++ * channel being destroyed. Bounds the recursion at one frame when the ++ * application's callback responds by starting another lookup. */ ++ ares_bool_t in_shutdown_refusal; + }; + + /* Does the domain end in ".onion" or ".onion."? Case-insensitive. */ +@@ -351,6 +393,16 @@ void *ares_dnsrec_convert_arg(ares_callbackcallback, void *arg);
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +7
-0+ void ares_dnsrec_convert_cb(void *arg, ares_status_t status, size_t timeouts, + const ares_dns_record_t *dnsrec); + ++/*! Unlink a query from every list and table that tracks it. ++ * ++ * Must be called before running application code that could reach the query ++ * again, so a reentrant teardown cannot find it and end it a second time.meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 904
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ * ++ * \param[in] query Query to detach. Safe to call more than once on the ++ * same query. ++ */ ++void ares_detach_query(ares_query_t *query); ++ + void ares_free_query(ares_query_t *query); + + unsigned short ares_generate_new_id(ares_rand_state *state); +@@ -390,7 +442,6 @@ ares_status_tares_sysconfig_set_options(ares_sysconfig_t *sysconfig,
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 909
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +5
-0+ + ares_status_t ares_init_by_environment(ares_sysconfig_t *sysconfig); + +- + typedef ares_status_t (*ares_sysconfig_line_cb_t)(const ares_channel_t*channel,
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 912
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +3
-0+ ares_sysconfig_t *sysconfig, + ares_buf_t *line); +@@ -589,6 +640,60 @@ ares_status_t ares_dns_name_write(ares_buf_t*buf,
ares_llist_t**list,Quoted 51 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +51
-0+ */ + void ares_queue_notify_empty(ares_channel_t *channel); + ++/*! Is this server still installed on the channel? ++ * ++ * Application callbacks may reconfigure the servers, which destroys any that ++ * the new configuration omits. A raw ares_server_t captured before such a ++ * callback must be revalidated before it is used again. ++ * ++ * \param[in] channel Initialized ares channel. ++ * \param[in] server Server pointer captured earlier. ++ * \return ARES_TRUE if still present. ++ */ ++ares_bool_t ares_server_in_channel(const ares_channel_t *channel, ++ const ares_server_t *server); ++ ++/*! Free everything awaiting reclamation on the channel. ++ * ++ * Called only from the deferred-callback flush, at the point where the ++ * outermost library call is unwinding and no frame can still hold a pointer ++ * to an unlinked server or connection. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_deferred_reclaim(ares_channel_t *channel); ++ ++/*! Free one connection's memory. Only for the deferred-reclaim path and the ++ * immediate path in ares_close_connection(). */ ++void ares_conn_reap(ares_conn_t *conn); ++ ++/*! Free one server's memory. Only for the deferred-reclaim path and the ++ * immediate path in ares_destroy_server(). */ ++void ares_server_reap(ares_server_t *server); ++ ++/*! Enter a region that may produce application notifications the library is ++ * not in a position to deliver yet -- socket-state and server-state changes. ++ * Nesting is counted; queued notifications are delivered by the outermost ++ * leave. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_deferred_cb_enter(ares_channel_t *channel); ++ ++/*! Leave such a region. The outermost leave delivers the queues, at which ++ * point the library holds no connection a reentrant call could free. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_deferred_cb_leave(ares_channel_t *channel); ++ ++/*! Deliver every server-state notification the channel has queued. Calledmeta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 973
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ * only from the shared deferred-callback flush. ++ * ++ * \param[in] channel Initialized ares channel. ++ */ ++void ares_server_state_dispatch(ares_channel_t *channel); ++ + #define ARES_CONFIG_CHECK(x) \ + (x && x->lookups && ares_slist_len(x->servers) > 0 && x->timeout > 0 && \ + x->tries > 0) +@@ -636,7 +741,6 @@ typedef struct ares_event_thread
ares_event_thread_t;meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 985
Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +12
-0+ void ares_event_thread_destroy(ares_channel_t *channel); + ares_status_t ares_event_thread_init(ares_channel_t *channel); + +- + #ifdef _WIN32 + # define HOSTENT_ADDRTYPE_TYPE short + # define HOSTENT_LENGTH_TYPE short +diff --git a/src/lib/ares_process.c b/src/lib/ares_process.c +index b2316c4b33..82f53610fb 100644 +--- a/src/lib/ares_process.c ++++ b/src/lib/ares_process.c +@@ -69,7 +69,6 @@ static ares_status_t ares_send_query_int(ares_server_t
*requested_server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 993
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +8
-0+ ares_query_t *query, + const ares_timeval_t *now, + ares_array_t **requeue); +-static void ares_detach_query(ares_query_t *query); + + static void ares_query_remove_from_conn(ares_query_t *query) + { +@@ -81,14 +80,32 @@ static void
ares_query_remove_from_conn(ares_query_t *query)Quoted 27 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +27
-0+ query->conn = NULL; + } + ++/*! A queued server-state notification. Holds the formatted address rather ++ * than the server, so it stays valid however the server list changes. */ ++typedef struct { ++ char *server_string; ++ ares_bool_t success; ++ int flags; ++} ares_server_state_msg_t; ++ ++static void ares_server_state_msg_free(void *arg) ++{ ++ ares_server_state_msg_t *msg = arg; ++ if (msg == NULL) { ++ return; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ares_free(msg->server_string); ++} ++ + /* Invoke the server state callback after a success or failure */ + static void invoke_server_state_cb(const ares_server_t *server, + ares_bool_t success, int flags) + { +- const ares_channel_t *channel = server->channel; +- ares_buf_t *buf; +- ares_status_t status;meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1030
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0+- char *server_string; ++ ares_channel_t *channel = server->channel; ++ ares_buf_t *buf; ++ ares_status_t status; ++ char *server_string; ++ ares_server_state_msg_t msg; + + if (channel->server_state_cb == NULL) { + return; +@@ -111,9 +128,57 @@ static void invoke_server_state_cb(const
ares_server_t*server,Quoted 51 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +51
-0+ return; /* LCOV_EXCL_LINE: OutOfMemory */ + } + +- channel->server_state_cb(server_string, success, flags, +- channel->server_state_cb_data); +- ares_free(server_string); ++ /* Queue rather than dispatch. This runs in the middle of processing, and ++ * every caller keeps using its query / connection / server afterwards. The ++ * callback is application code that may reentrantly call ares_cancel(), ++ * which frees those objects -- so dispatch once the library is no longer ++ * holding them. Same philosophy as the deferred query-callback dispatch. ++ * ++ * Only the formatted address is captured, never the ares_server_t, so a ++ * queued entry cannot outlive anything it points at. */ ++ if (channel->server_state_queue == NULL) { ++ channel->server_state_queue = ares_array_create( ++ sizeof(ares_server_state_msg_t), ares_server_state_msg_free); ++ if (channel->server_state_queue == NULL) { ++ /* LCOV_EXCL_START: OutOfMemory */ ++ ares_free(server_string); ++ return; ++ /* LCOV_EXCL_STOP */ ++ } ++ } ++ ++ msg.server_string = server_string; ++ msg.success = success; ++ msg.flags = flags; ++ ++ if (ares_array_insertdata_last(channel->server_state_queue, &msg) != ++ ARES_SUCCESS) { ++ ares_free(server_string); /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++} ++ ++/*! Deliver every server-state notification the channel has queued. Loops ++ * rather than snapshotting: a callback is allowed to re-enter the library and ++ * queue more, and those belong to this same delivery point. */ ++void ares_server_state_dispatch(ares_channel_t *channel) ++{ ++ while (ares_array_len(channel->server_state_queue) > 0) { ++ ares_server_state_msg_t msg; ++ ++ if (ares_array_claim_at(&msg, sizeof(msg), channel->server_state_queue, ++ 0) != ARES_SUCCESS) { ++ break; /* LCOV_EXCL_LINE: DefensiveCoding */ ++ } ++ ++ if (channel->server_state_cb != NULL) { ++ channel->server_state_cb(msg.server_string, msg.success, msg.flags, ++ channel->server_state_cb_data);meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1091
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ } ++ ares_free(msg.server_string); ++ } ++ ++ ares_array_destroy(channel->server_state_queue); ++ channel->server_state_queue = NULL; + } + + static void server_increment_failures(ares_server_t *server, +@@ -204,6 +269,8 @@ static ares_status_tares_process_fds_nolock(ares_channel_t *channel,
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1100
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +9
-0+ return ARES_EFORMERR; /* LCOV_EXCL_LINE: DefensiveCoding */ + } + ++ ares_deferred_cb_enter(channel); ++ + ares_tvnow(&now); + + /* Process write events */ +@@ -244,6 +311,8 @@ static ares_status_tares_process_fds_nolock(ares_channel_t *channel,
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +8
-0+ } + + done: ++ ares_deferred_cb_leave(channel); ++ + if (status == ARES_ENOMEM) { + return ARES_ENOMEM; + }meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1118
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0+@@ -553,6 +622,8 @@ typedef enum { + typedef struct { + requeue_type_t type; /* type of entry, requeue or endquery */ + unsigned short qid; /* query id */ ++ size_t serial; /* identifies the query the qid meant when ++ * this entry was appended */ + ares_server_t *server; /* requeue only: optional */ + ares_status_t status; /* endquery only */ + ares_dns_record_t *dnsrec; /* endquery only: optional */ +@@ -578,6 +649,7 @@ static ares_status_tares_append_requeue_int(ares_array_t **requeue,
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1126
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +8
-0+ + entry.type = type; + entry.qid = query->qid; ++ entry.serial = query->serial; + entry.server = server; + entry.status = status; + entry.dnsrec = dnsrec; +@@ -601,6 +673,53 @@ static ares_status_tares_append_endqueue(ares_array_t **requeue,
Quoted 42 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +42
-0+ dnsrec); + } + ++/*! Is the query the caller captured still the one holding \a qid? ++ * ++ * A query id is a 16-bit wire value reissued as soon as the query holding it ++ * is detached, so it names a slot rather than a query, and comparing the ++ * pointer as well does not help: the replacement is often allocated at the ++ * address the freed one had. The serial is a channel-wide counter that is ++ * never reused. */ ++static ares_bool_t ares_query_is_live(const ares_channel_t *channel, ++ unsigned short qid, size_t serial) ++{ ++ const ares_query_t *query = ++ ares_htable_szvp_get_direct(channel->queries_by_qid, qid); ++ ++ if (query == NULL || query->serial != serial) { ++ return ARES_FALSE; ++ } ++ ++ return ARES_TRUE; ++} ++ ++/*! Is this exact server object still in the channel's server list? ++ * ++ * Compares pointers only and never dereferences \a server. Deferred ++ * reclamation keeps an unlinked server readable for the rest of the call ++ * that captured it, so reading through it is no longer a use-after-free -- ++ * but it has been removed from the configuration and must not be acted on, ++ * which is what this reports. A channel has a handful of servers, so the ++ * linear scan does not matter. */ ++ares_bool_t ares_server_in_channel(const ares_channel_t *channel, ++ const ares_server_t *server) ++{ ++ ares_slist_node_t *node; ++ ++ if (server == NULL) { ++ return ARES_FALSE; ++ } ++ ++ for (node = ares_slist_node_first(channel->servers); node != NULL; ++ node = ares_slist_node_next(node)) {meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1178
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ if (ares_slist_node_val(node) == server) { ++ return ARES_TRUE; ++ } ++ } ++ ++ return ARES_FALSE; ++} ++ + /* Drain the deferred requeue/endqueue list iteratively. All flush sites + * (read_answers(), process_timeouts(), and ares_send_query()) funnelthrough
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1180
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +2
-0+ * here so that: +@@ -636,11 +755,35 @@ static ares_status_tares_flush_requeue(ares_channel_t *channel,
Quoted 26 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +26
-0+ + query = ares_htable_szvp_get_direct(channel->queries_by_qid, entry.qid); + ++ /* A query id is a 16-bit wire value and generate_unique_qid() only avoids ++ * ids that are live when it runs. Within a single drain, a callback can ++ * cancel a query whose entry is still in this array -- releasing its id -- ++ * and start a new lookup that is handed the same id. The lookup above ++ * would then resolve this entry to a completely different query: an ++ * endquery entry would deliver one lookup's answer to another and free it, ++ * a requeue entry would put an in-flight query on the wire twice. The ++ * serial is a channel-wide counter that is never reused, so it tells the ++ * two apart. */ ++ if (query != NULL && query->serial != entry.serial) { ++ query = NULL; ++ } ++ + if (entry.type == REQUEUE_REQUEUE) { + /* Query disappeared (e.g. a prior callback in this drain cancelled it) */ + if (query == NULL) { + continue; + } ++ /* entry.server is a raw pointer captured before any callback in this ++ * drain ran. A callback may legally re-enter the public API -- the ++ * channel lock is recursive -- and ares_set_servers*() destroys servers ++ * that are absent from the new configuration, so it may already be ++ * freed. The query above is revalidated by qid for exactly thismeta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1216
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ * reason; do the same for the server, falling back to ordinary ++ * selection when it has gone. */ ++ if (!ares_server_in_channel(channel, entry.server)) { ++ entry.server = NULL; ++ } ++ + /* Re-dispatch via the internal entrypoint so any further requeues are + * appended back onto this same list and drained by the loop above, + * rather than recursing. */ +@@ -1377,6 +1520,7 @@ ares_status_t ares_send_query(ares_server_t
*requested_server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1224
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +8
-0+ ares_channel_t *channel = query->channel; + ares_array_t *requeue = NULL; + unsigned short qid = query->qid; ++ size_t serial = query->serial; + ares_status_t status; + + status = ares_send_query_int(requested_server, query, now, &requeue); +@@ -1393,8 +1537,7 @@ ares_status_t ares_send_query(ares_server_t
*requested_server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1230
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +6
-0+ * been freed. Do not dereference 'query' here. If it is no longer tracked + * it ended, so don't report success to the caller (which would, e.g., cause + * ares_send_nolock() to write to a now-freed *qid). */ +- if (status == ARES_SUCCESS && +- ares_htable_szvp_get_direct(channel->queries_by_qid, qid) == NULL) { ++ if (status == ARES_SUCCESS && !ares_query_is_live(channel, qid, serial)){
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1234
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +4
-0+ status = ARES_ETIMEOUT; + } + +@@ -1412,6 +1555,9 @@ static ares_status_tares_send_query_int(ares_server_t
*requested_server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1238
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +4
-0+ size_t timeplus; + ares_status_t status; + ares_bool_t probe_downed_server = ARES_TRUE; ++ /* Captured before any application callback below, which may free the query*/
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1244
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +6
-0++ unsigned short qid = query->qid; ++ size_t serial = query->serial; + + /* Choose the server to send the query to */ + if (requested_server != NULL) { +@@ -1443,6 +1589,32 @@ static ares_status_tares_send_query_int(ares_server_t
*requested_server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1250
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +6
-0+ conn = ares_fetch_connection(channel, server, query); + if (conn == NULL) { + status = ares_open_connection(&conn, channel, server, query->using_tcp); ++ ++ /* Opening a connection runs application code -- the socket-open function ++ * from ares_set_socket_functions_ex(), and the
ares_set_socket_callback()meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1259
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +9
-0++ * and ares_set_socket_configure_callback() callbacks -- any of which may ++ * reentrantly call ares_cancel() and free this query. Re-check liveness ++ * by the qid captured at entry rather than dereferencing a possibly-freed ++ * query. */ ++ if (!ares_query_is_live(channel, qid, serial)) { ++ return ARES_ECANCELLED; ++ } ++ ++ /* Those same callbacks may have reconfigured the servers, which freesthe
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1270
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +11
-0++ * ones the new configuration omits. 'server' was captured before the ++ * open; everything below reads it, including server_increment_failures() ++ * on the failure paths of the switch. ++ * ++ * Requeue rather than return: every exit from this function must either ++ * enqueue the query or end it, and the query is still live here -- it has ++ * not been written, is not attached to a connection, and on a first send ++ * has no timeout node, so returning would strand it forever. ++ * query->conn is NULL, so this does not read through the freed server. */ ++ if (!ares_server_in_channel(channel, server)) { ++ return ares_requeue_query(query, now, ARES_ECONNREFUSED,
ARES_TRUE, NULL,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1277
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +7
-0++ requeue); ++ } ++ + switch (status) { + /* Good result, continue on */ + case ARES_SUCCESS: +@@ -1464,6 +1636,14 @@ static ares_status_tares_send_query_int(ares_server_t
*requested_server,Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +5
-0+ + /* Write the query */ + status = ares_conn_query_write(conn, query, now); ++ ++ /* Writing runs application code too: ares_conn_query_write() reaches themeta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1292
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ * send function installed by ares_set_socket_functions_ex(), which may ++ * reentrantly call ares_cancel() with the same hazard as above. */ ++ if (!ares_query_is_live(channel, qid, serial)) { ++ return ARES_ECANCELLED; ++ } ++ + switch (status) { + /* Good result, continue on */ + case ARES_SUCCESS: +@@ -1479,6 +1659,17 @@ static ares_status_tares_send_query_int(ares_server_t
*requested_server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1297
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +5
-0+ case ARES_ECONNREFUSED: + case ARES_EBADFAMILY: + handle_conn_error(conn, ARES_TRUE, status); ++ /* handle_conn_error() -> ares_close_connection() -> ++ * ares_requeue_queries() walks conn->queries_to_conn and mayterminally
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1307
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ * end other queries on this connection. If one of their callbacks ++ * reentrantly calls ares_cancel(), this query is freed too -- it is still ++ * linked in all_queries, since it only joins conn->queries_to_conn on the ++ * success path below. Re-check liveness by the qid captured at entry ++ * rather than dereferencing a possibly-freed query. Mirrors the guard in ++ * ares_send_query(). */ ++ if (!ares_query_is_live(channel, qid, serial)) { ++ return ARES_ECONNREFUSED; ++ } + status = ares_requeue_query(query, now, status, ARES_TRUE, NULL,requeue);
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- line 1310
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +3
-0+ if (status == ARES_ETIMEOUT) { + status = ARES_ECONNREFUSED; +@@ -1597,11 +1788,18 @@ static ares_bool_t same_questions(const
ares_query_t*query,Quoted 18 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +14
-0+ return rv; + } + +-static void ares_detach_query(ares_query_t *query) ++void ares_detach_query(ares_query_t *query) + { + /* Remove the query from all the lists in which it is linked */ + ares_query_remove_from_conn(query); +- ares_htable_szvp_remove(query->channel->queries_by_qid, query->qid); ++ /* Only release the qid slot if it is still ours. A query detached before ++ * its callback can have its qid reissued to a lookup started from within ++ * that callback, and the ares_free_query() that follows detaches a second ++ * time -- which would otherwise evict the new lookup instead. */ ++ if (ares_htable_szvp_get_direct(query->channel->queries_by_qid, query-qid) ==
++ query) {
++ares_htable_szvp_remove(query->channel->queries_by_qid, query->qid);
++ }meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +4
-0+ ares_llist_node_destroy(query->node_all_queries); + query->node_all_queries = NULL; + } +@@ -1624,6 +1822,16 @@ static void end_query(ares_channel_t *channel,
ares_server_t*server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +9
-0+ return; + } + ++ /* Detach from all lookup lists before the callback, otherwise a reentrant ++ * ares_cancel() from within it would find this query still linked, free it, ++ * and the ares_free_query() below would double-free. Mirrors ++ * ares_flush_requeue() for every synchronous path that reaches here with a ++ * NULL requeue: connection teardown via handle_conn_error(), and server ++ * removal via ares_servers_update() and ares_destroy_servers_state(),both
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 8 lines
++ * of which reach
ares_close_sockets().
++ *ares_detach_query()may safely run twice on the same query. */
++ares_detach_query(query);
++meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +4
-0+ /* Invoke the callback. */ + query->callback(query->arg, status, query->timeouts, dnsrec); + ares_free_query(query); +@@ -1631,7 +1839,8 @@ static void end_query(ares_channel_t *channel,
ares_server_t*server,Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +4
-0+ /* Check and notify if no other queries are enqueued on the channel. This + * must come after the callback and freeing the query for 2 reasons. + * 1) The callback itself may enqueue a new query +- * 2) Technically the current query isn't detached until it is free()'d.meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ * 2) The current query is detached above, before the callback, so by this ++ * point it is no longer tracked. + */ + ares_queue_notify_empty(channel); + } +diff --git a/src/lib/ares_send.c b/src/lib/ares_send.c +index 6efa9580b2..2e0482bf19 100644 +--- a/src/lib/ares_send.c ++++ b/src/lib/ares_send.c +@@ -117,6 +117,27 @@ ares_status_t ares_send_nolock(ares_channel_t*channel,
ares_server_t*server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +6
-0+ unsigned short id = generate_unique_qid(channel); + const ares_dns_record_t *dnsrec_resp = NULL; + ++ /* A channel being destroyed accepts no new queries. ares_destroy(3) ++ * documents that a callback must not add new requests to a channel being ++ * destroyed. Refuse it, but still notify: ARES_EDESTRUCTION isdocumented
Quoted 1 line
++ * in
ares_query(3),ares_send(3),ares_search(3)andares_getaddrinfo(3)as
Quoted 17 lines
++ * a status delivered to the callback, and every caller frees its own ++ * context from there. ++ * ++ * Not notified if we are already inside such a notification: an application ++ * whose callback starts a replacement lookup on every invocation would ++ * otherwise re-enter here from within the callback without bound and ++ * exhaust the stack. At that depth it is already past the contract ++ * ares_destroy(3) documents. */ ++ if (!channel->sys_up) { ++ if (!channel->in_shutdown_refusal) { ++ channel->in_shutdown_refusal = ARES_TRUE; ++ callback(arg, ARES_EDESTRUCTION, 0, NULL); ++ channel->in_shutdown_refusal = ARES_FALSE;++ }
++ returnARES_EDESTRUCTION;
++ }
++meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +4
-0+ ares_tvnow(&now); + + if (ares_slist_len(channel->servers) == 0) { +@@ -145,6 +166,7 @@ ares_status_t ares_send_nolock(ares_channel_t*channel,
ares_server_t*server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +8
-0+ + query->channel = channel; + query->qid = id; ++ query->serial = ++channel->query_serial; + query->timeout.sec = 0; + query->timeout.usec = 0; + query->using_tcp = +@@ -209,6 +231,12 @@ ares_status_t ares_send_nolock(ares_channel_t*channel,
ares_server_t*server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +13
-0+ */ + if (!ares_htable_szvp_insert(channel->queries_by_qid, query->qid, query)) { + /* LCOV_EXCL_START: OutOfMemory */ ++ /* The query is already chained into all_queries, so a reentrant ++ * ares_cancel() from the callback below would find it, run the ++ * application callback a second time for one lookup, and free it -- and ++ * the ares_free_query() here would then double-free. Same ordering as ++ * ares_cancel() and ares_destroy(): unlink first, notify second. */ ++ ares_detach_query(query); + callback(arg, ARES_ENOMEM, 0, NULL); + ares_free_query(query); + return ARES_ENOMEM; +@@ -217,10 +245,17 @@ ares_status_t ares_send_nolock(ares_channel_t*channel,
ares_server_t*server,meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +4
-0+ + /* Perform the first query action. */ + ++ /* Sending opens and writes to connections and can mark a server down,both
Quoted 8 lines
++ * of which notify. Deliver those once we are done with the connection and
++ * the server, not part-way through. */
++ares_deferred_cb_enter(channel);meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +5
-0+ status = ares_send_query(server, query, &now); ++ ares_deferred_cb_leave(channel); ++ + if (status == ARES_SUCCESS && qid) { + *qid = id;meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0+ } ++ + return status; + } + +diff --git a/src/lib/ares_update_servers.c b/src/lib/ares_update_servers.c +index f8d441accf..50a9c27925 100644 +--- a/src/lib/ares_update_servers.c ++++ b/src/lib/ares_update_servers.c +@@ -740,17 +740,36 @@ static ares_bool_tares_servers_remove_stale(ares_channel_t *channel,
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 8 lines
+ {
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +7
-0+ ares_bool_t stale_removed = ARES_FALSE; + ares_slist_node_t *snode = ares_slist_node_first(channel->servers); ++ size_t generation = channel->servers_generation; + ++ /* Re-read from the head each pass rather than caching the next node. ++ * ares_slist_node_destroy() below reaches application callbacks, by way of ++ * ares_destroy_server() -> ares_close_sockets() ->
ares_requeue_queries(),Quoted 26 lines
++ * and a nested reconfiguration from one of those destroys the remaining
++ * stale servers -- including whichever one a cached pointer referred to. */meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +24
-0+ while (snode != NULL) { +- ares_slist_node_t *snext = ares_slist_node_next(snode); + const ares_server_t *server = ares_slist_node_val(snode); +- if (!ares_server_in_newconfig(server, srvlist)) { +- /* This will clean up all server state via the destruction callback and +- * move any queries to new servers */ +- ares_slist_node_destroy(snode); +- stale_removed = ARES_TRUE; ++ ++ if (ares_server_in_newconfig(server, srvlist)) { ++ snode = ares_slist_node_next(snode); ++ continue; + } +- snode = snext; ++ ++ /* This will clean up all server state via the destruction callback and ++ * move any queries to new servers */ ++ ares_slist_node_destroy(snode); ++ stale_removed = ARES_TRUE; ++ ++ /* A callback reached from the destroy above may have reconfigured the ++ * server list against a newer configuration. That call has already ++ * reconciled the whole list, so whatever remains belongs to it and not to ++ * srvlist -- stop rather than deleting its servers. */meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ if (channel->servers_generation != generation) { ++ break; ++ } ++ ++ /* The callbacks above may have removed further servers, so restart. */ ++ snode = ares_slist_node_first(channel->servers); + } + return stale_removed; + } +@@ -775,6 +794,14 @@ ares_status_t ares_servers_update(ares_channel_t*channel,
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +5
-0+ return ARES_EFORMERR; /* LCOV_EXCL_LINE: DefensiveCoding */ + } + ++ /* Removing servers closes their connections, which notifies. Deliver ++ * once this call is done rather than from inside the teardown. */meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ ares_deferred_cb_enter(channel); ++ ++ /* Bumped before anything below can reach an application callback, so a ++ * nested reconfiguration is distinguishable from our own work. */ ++ channel->servers_generation++; ++ + /* NOTE: a NULL or zero entry server list is considered valid due to + * real-world people needing support for this for their test harnesses + */ +@@ -843,6 +870,7 @@ ares_status_t ares_servers_update(ares_channel_t*channel,
Quoted 14 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +14
-0+ status = ARES_SUCCESS; + + done: ++ ares_deferred_cb_leave(channel); + return status; + } + +diff --git a/test/ares-test-mock.cc b/test/ares-test-mock.cc +index c0c0279abb..2158c5a913 100644 +--- a/test/ares-test-mock.cc ++++ b/test/ares-test-mock.cc +@@ -31,6 +31,11 @@ + #include <sys/stat.h> + #endifmeta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0+ ++#ifndef _WIN32 ++# include <errno.h> ++# include <fcntl.h> ++#endif ++ + #include <sstream> + #include <vector> + +@@ -1914,6 +1919,7 @@ TEST_P(MockRetryDepthChannelTest,HighRetryNoStackOverflow) {
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 8 lines
+ struct CancelInCbData {
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +7
-0+ ares_channel_t *channel; + bool done; ++ size_t cbs; + }; + + static void CancelChannelCallback(void *arg, ares_status_t status, +@@ -1925,10 +1931,1071 @@ static void CancelChannelCallback(void *arg,
ares_status_tstatus,Quoted 29 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +29
-0+ (void)timeouts; + (void)dnsrec; + data->done = true; ++ data->cbs++; + /* Reentrant cancel from within the callback. Must not double-free. */ + ares_cancel(data->channel); + } + ++// ares_destroy() notifies each still-running query that the channel is going ++// away, and holds a pointer to the next one in its list while doing so. A ++// reentrant ares_cancel() from one of those callbacks frees every remaining ++// query -- including that cached next, which ares_destroy() then follows. ++// ++// ares_cancel() is immune to the same pattern because it swaps the list head ++// before walking it; ares_destroy() did not. Two queries are required: with ++// only one in flight there is no next to cache and nothing goes wrong. ++// tries=1 so a teardown ends the query outright instead of retrying it, which ++// is the precondition this family of bugs shares. ++class MockUDPSingleTryTest : public MockChannelOptsTest, ++ public ::testing::WithParamInterface<int> { ++public: ++ MockUDPSingleTryTest() ++ : MockChannelOptsTest(1, GetParam(), false, false, FillOptions(&opts_), ++ ARES_OPT_TRIES) ++ { ++ } ++ ++ static struct ares_options *FillOptions(struct ares_options *opts) ++ {meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +10
-0++ memset(opts, 0, sizeof(struct ares_options)); ++ opts->tries = 1; ++ return opts; ++ } ++ ++private: ++ struct ares_options opts_; ++}; ++ ++// ares_cancel() and ares_destroy() free a query themselves rather thangoing
Quoted 1 line
++// through
end_query(). Both used to invoke the application callback whilethe
Quoted 15 lines
++// query was still in
queries_by_qid, onconn->queries_to_connand in ++//queries_by_timeout-- onlyall_querieswas unlinked first. A reentrant ++// teardown from within that callback therefore still found the query, ended it ++// a second time, and freed it; theares_free_query()that followed then ++// double-freed. One query in flight is enough.
++static void ReconfigureServersCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec) {++ CancelInCbData *data = static_cast<CancelInCbData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ data->done = true; ++ data->cbs++; ++ /* Frees every server absent from the new configuration, closing theirQuoted 6 lines
++ * connections -- which still hold the queries being torn down. */
++ares_set_servers_csv(data->channel, "127.0.0.1:53055"); ++}
++
++// Reconfiguring the server list from a cancellation callback. ++TEST_P(MockUDPSingleTryTest,ReconfigureInCancelCallbackNoDoubleFree) {
Quoted 8 lines
++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ++ QueryResult result; ++ /* Left unanswered, so both stay attached to the connection. */ ++ EXPECT_EQ(ARES_SUCCESS,Quoted 5 lines
++ ares_query_dnsrec(channel_, "www.google.com",
ARES_CLASS_IN,
++ARES_REC_TYPE_A, ReconfigureServersCallback,
++ &data, NULL));
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_query_dnsrec(channel_, "www.example.com",
ARES_CLASS_IN,Quoted 35 lines
++
ARES_REC_TYPE_A, QueryCallback, &result, NULL));
++++ ares_cancel(channel_); ++ EXPECT_TRUE(data.done); ++ EXPECT_TRUE(result.done_); ++ /* Each lookup must be notified exactly once, not twice. */ ++ EXPECT_EQ((size_t)1, data.cbs);++}
++
++/* The replacement lookup's result outlives the callback that starts it. */ ++static QueryResultg_replacement_result;
++
++static void NewQueryOnFailingWriteCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec) {
++ /*ares_set_socket_functions()stores the pointer rather than copying it, so
++ * this must outlive the call. */++ static ares_socket_functions sock_funcs; ++ CancelInCbData *data = static_cast<CancelInCbData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec;++
++ data->cbs++;
++
++ /* Only the first cancellation callback starts a replacement lookup. */ ++ if (data->done) {
++ return;
++ }
++data->done= true;
++++ memset(&sock_funcs, 0, sizeof(sock_funcs)); ++ sock_funcs.asendv = LibraryTest::ares_sendv_fail; ++ ares_set_socket_functions(data->channel, &sock_funcs, NULL); ++ LibraryTest::SetFailSend();Quoted 12 lines
++
++ ares_query_dnsrec(data->channel, "www.probe.com",ARES_CLASS_IN,
++ARES_REC_TYPE_A, QueryCallback,&g_replacement_result,
++ NULL);
++}
++
++// No reconfiguration at all: the cancellation callback simply starts a ++// replacement lookup -- an ordinary retry pattern -- and that lookup's write ++// fails.ares_close_connection()then walksconn->queries_to_conn, which ++// still holds the queriesares_cancel()is in the middle of. Wider than the ++// case above: it needs nothing more than a connection error. ++TEST_P(MockUDPSingleTryTest,NewQueryInCancelCallbackConnErrorNoDoubleFree) {
Quoted 4 lines
++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0;Quoted 10 lines
++
++ g_replacement_result.done_ = false;
++
++ QueryResult result;
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_query_dnsrec(channel_, "www.google.com",ARES_CLASS_IN,
++ARES_REC_TYPE_A, NewQueryOnFailingWriteCallback,
++ &data, NULL));
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_query_dnsrec(channel_, "www.example.com",
ARES_CLASS_IN,Quoted 8 lines
++
ARES_REC_TYPE_A, QueryCallback, &result, NULL));
++++ ares_cancel(channel_); ++ EXPECT_TRUE(data.done); ++ EXPECT_TRUE(result.done_); ++ /* Each lookup must be notified exactly once, not twice. */ ++ EXPECT_EQ((size_t)1, data.cbs); ++ /* Pins that the replacement lookup ran and its write failed, which is whatQuoted 5 lines
++ * drives the connection teardown this test is about. */
++EXPECT_TRUE(g_replacement_result.done_);
++}
++
++// The same shape throughares_destroy()rather thanares_cancel().Swapping
Quoted 1 line
++// the
all_querieshead does not help here: the reentrant teardown reachesthe
Quoted 2 lines
++// query through the connection, not through
all_queries. ++TEST_P(MockUDPSingleTryTest,ReconfigureInDestroyCallbackNoDoubleFree) {
Quoted 4 lines
++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0;Quoted 8 lines
++
++ QueryResult result;
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_query_dnsrec(channel_, "www.google.com",ARES_CLASS_IN,
++ARES_REC_TYPE_A, ReconfigureServersCallback,
++ &data, NULL));
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_query_dnsrec(channel_, "www.example.com",
ARES_CLASS_IN,Quoted 5 lines
++
ARES_REC_TYPE_A, QueryCallback, &result, NULL));
++
++ares_destroy(channel_);
++channel_= nullptr; /* destroyed here, don't let the fixture repeat it */ ++Quoted 7 lines
++ EXPECT_TRUE(data.done); ++ EXPECT_TRUE(result.done_); ++ /* Each lookup must be notified exactly once, not twice. */ ++ EXPECT_EQ((size_t)1, data.cbs);++}
++
++// Onceares_destroy()has begun, a lookup started from a destructioncallback
Quoted 2 lines
++// is refused rather than allocated into a channel being torn down -- and the ++// refusal still runs the caller's callback with
ARES_EDESTRUCTION, socontext
Quoted 22 lines
++// attached to
argcan be reclaimed.ares_cancel()leavessys_uptrue, so a ++// destruction callback is the only way to reach this.
++struct DestroyReentryData {++ ares_channel_t *channel; ++ size_t outer_cbs; ++ size_t replacement_cbs; ++ ares_status_t replacement_status;++};
++
++static void DestroyReplacementCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec) {++ DestroyReentryData *data = static_cast<DestroyReentryData *>(arg); ++ (void)timeouts; ++ (void)dnsrec; ++ data->replacement_cbs++; ++ data->replacement_status = status;++}
++
++static void DestroyReentryCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec) {Quoted 6 lines
++ DestroyReentryData *data = static_cast<DestroyReentryData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ data->outer_cbs++; ++ ares_query_dnsrec(data->channel, "www.example.com",
ARES_CLASS_IN,Quoted 1 line
++
ARES_REC_TYPE_A, DestroyReplacementCallback, data,NULL);
Quoted 12 lines
++}
++
++TEST_F(LibraryTest, NewQueryInDestroyCallbackIsRefusedAndNotified) {++ ares_channel_t *channel = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS;Quoted 2 lines
++
++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts,optmask));
Quoted 2 lines
++ // Nothing listens here; no traffic is needed. ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel,
"127.0.0.1:53055"));
Quoted 25 lines
++
++ DestroyReentryData data; ++ data.channel = channel; ++ data.outer_cbs = 0; ++ data.replacement_cbs = 0; ++ data.replacement_status = ARES_SUCCESS;++
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_query_dnsrec(channel, "www.google.com",ARES_CLASS_IN,
++ARES_REC_TYPE_A, DestroyReentryCallback, &data,
++ NULL));
++
++ares_destroy(channel);
++++ EXPECT_EQ((size_t)1, data.outer_cbs); ++ // Refused -- but notified, so the caller can release whatever it attached. ++ EXPECT_EQ((size_t)1, data.replacement_cbs); ++ EXPECT_EQ(ARES_EDESTRUCTION, data.replacement_status);++}
++
++INSTANTIATE_TEST_SUITE_P(AddressFamilies, MockUDPSingleTryTest,
++ ::testing::ValuesIn(ares::test::families),
++ PrintFamily);
++
++TEST_P(MockUDPChannelTest, CancelInDestroyCallbackNoUseAfterFree) {Quoted 6 lines
++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0;++
++ /* Left unanswered, so both are still pending when the channel goes down.*/
Quoted 6 lines
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_query_dnsrec(channel_, "www.google.com",ARES_CLASS_IN,
++ARES_REC_TYPE_A, CancelChannelCallback, &data,
++ NULL));
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_query_dnsrec(channel_, "www.example.com",
ARES_CLASS_IN,Quoted 11 lines
++
ARES_REC_TYPE_A, CancelChannelCallback, &data,
++ NULL));
++ /* Without this the test can pass vacuously if a query failed to start. */ ++EXPECT_EQ(2, ares_queue_active_queries(channel_));
++
++ares_destroy(channel_);
++channel_= nullptr; /* destroyed here, don't let the fixture repeat it */ ++++ EXPECT_TRUE(data.done); ++ /* Every query must be notified exactly once. */ ++ EXPECT_EQ((size_t)2, data.cbs);Quoted 6 lines
++}
++
++// Fixture with tries=1 so the first failure ends the query outright instead of ++// retrying it -- that is what drives execution into end_query()'s synchronous ++// (requeue==NULL) branch.
++// The tests below use the MockUDPSingleTryTest fixture defined above:tries=1,
Quoted 6 lines
++// so the first failure ends the query outright instead of retrying it, which ++// is what drives execution into end_query()'s synchronous (requeue==NULL) ++// branch.
++//
++
++// Connection-teardown route into the synchronous branch. A malformedreply
Quoted 1 line
++// makes
process_answer()return EBADRESP, sohandle_conn_error()tearsthe
Quoted 1 line
++// connection down and
ares_close_connection()requeues its queries with aNULL
Quoted 1 line
++// requeue; with tries=1 the query is ended right there. CVE-2026-33630
fixed
Quoted 2 lines
++// the deferred flush path only, so this one still double-freed. ++TEST_P(MockUDPSingleTryTest,
CancelInCallbackConnTeardownNoDoubleFree) {
Quoted 10 lines
++ /* Too short to be a DNS header, so it fails to parse. */ ++ std::vector<byte> malformed = {0x00, 0x00, 0x00, 0x00}; ++ ON_CALL(server_, OnRequest("www.google.com", T_A)) ++ .WillByDefault(SetReplyData(&server_, malformed)); ++ ++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 7 lines
++ CancelChannelCallback, &data, NULL);
++Process();
++EXPECT_TRUE(data.done);
++}
++
++// Server-removal route into the same synchronous branch, which the ++// connection-teardown test above does not cover. Reconfiguring the serverlist
Quoted 2 lines
++// destroys the servers that are gone, and
ares_close_sockets()closes their ++// connections with a NULL requeue -- a second way to reachend_query()with the
Quoted 3 lines
++// query still linked. No
Process()call is needed: the query is attached to a ++// connection byares_query_dnsrec()itself, and nothing has replied to it. ++TEST_P(MockUDPSingleTryTest,CancelInCallbackServerRemovalNoDoubleFree) {
Quoted 5 lines
++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 4 lines
++ CancelChannelCallback, &data, NULL);
++
++ /* Drops the server the in-flight query is attached to. */ ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel_,"127.0.0.1:53055"));
Quoted 4 lines
++
EXPECT_TRUE(data.done);
++}
++
++//ares_send_query_int()callshandle_conn_error()on a retryable writefailure
Quoted 5 lines
++// and then keeps using its own query. That teardown requeues the *other* ++// queries on the connection, and with tries=1 they are ended outright; if one ++// of their callbacks reentrantly cancels the channel, this query is freed too ++// -- it is still linked in
all_queries, having only joined
++//conn->queries_to_connon the success path further down. Using itafterwards
Quoted 4 lines
++// is a use-after-free.
++//
++// The first query is left unanswered so it stays on the connection, then the ++// next write is made to fail with ECONNREFUSED so the second query takesthe
Quoted 5 lines
++// error path while the first is still attached.
++//
++// Note this is masked on code that has not fixed theend_query()double-free:
++// that aborts insidehandle_conn_error()before control returns here. ++TEST_P(MockUDPSingleTryTest,CancelInSiblingCallbackConnErrorNoUseAfterFree) {
Quoted 7 lines
++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0;++
++ /* No reply configured for this one, so it stays attached to the connection. */ ++ ares_query_dnsrec(channel_, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 10 lines
++ CancelChannelCallback, &data, NULL);
++++ ares_socket_functions sock_funcs; ++ memset(&sock_funcs, 0, sizeof(sock_funcs)); ++ sock_funcs.asendv = ares_sendv_fail; ++ ares_set_socket_functions(channel_, &sock_funcs, NULL); ++ LibraryTest::SetFailSend();++
++ QueryResult result;
++ ares_query_dnsrec(channel_, "www.example.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 4 lines
++ QueryCallback, &result, NULL);
++
++ /* The write failure above tears the connection down, which ends the first
++ * query, whose callback cancels the channel and frees this one. */Quoted 8 lines
++
EXPECT_TRUE(data.done);
++ /* The sibling is cancelled from the first query's callback rather than
++ * requeued; pin that rather than leaving it unstated. */
++EXPECT_TRUE(result.done_);
++EXPECT_EQ(ARES_ECANCELLED, result.status_);
++}
++
++// The socket-state callback -- registered withARES_OPT_SOCK_STATE_CB,the
Quoted 2 lines
++// usual way c-ares is driven from an event loop -- used to be dispatched from ++// the middle of opening a connection, and
ares_open_connection()keepsusing
Quoted 31 lines
++// that connection afterwards. A reentrant
ares_cancel()from the callback ++// frees both the connection (through the idle sweep) and the in-flight query, ++// leaving the library using either.
++struct SockStateCancelData {
++ares_channel_t*channel;
++size_tcalls;
++};
++
++static void SockStateCancelCallback(void *data,ares_socket_tsocket_fd,
++ int readable, int writable)
++{++ SockStateCancelData *d = static_cast<SockStateCancelData *>(data); ++ (void)socket_fd; ++ (void)readable; ++ (void)writable; ++ d->calls++; ++ // Reentrant cancel from the socket-state notification. ++ if (d->channel != nullptr) {++
ares_cancel(d->channel);
++ }
++}
++
++TEST_F(LibraryTest, CancelInSockStateCallbackNoUseAfterFree) {++ SockStateCancelData data; ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ int optmask = 0;++
++ data.channel = nullptr;
++ data.calls = 0;
++Quoted 6 lines
++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateCancelCallback; ++ opts.sock_state_cb_data = &data; ++ optmask = ARES_OPT_SOCK_STATE_CB;++
++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&chan, &opts,optmask));
Quoted 2 lines
++
ASSERT_NE(nullptr, chan);
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan,"127.0.0.1:53055"));
Quoted 6 lines
++
++ /* Armed only now, so channel setup itself does not cancel. */ ++ data.channel = chan;
++
++ QueryResult result;
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 8 lines
++ QueryCallback, &result, NULL);
++
++ /* Opening the connection notifies, the notification cancels, and the
++ * library must still be able to finish -- and the lookup must actually be
++ * cancelled rather than the cancel quietly doing nothing. */++ EXPECT_GT(data.calls, (size_t)0); ++ EXPECT_TRUE(result.done_); ++ EXPECT_EQ(ARES_ECANCELLED, result.status_);Quoted 8 lines
++
++ data.channel = nullptr; /* don't cancel from teardown notifications */ ++ares_destroy(chan);
++}
++
++// Deferring the notification must not reorder it past the close of the ++// descriptor it names. An event loop is told "stop watching fd N"; if fd N is ++// already closed by then, the number can have been handed to somethingelse --
Quoted 5 lines
++// including to application code running from a deferred query callback -- and ++// the application ends up setting and clearing interest in a descriptor it now ++// owns for another purpose.
++//
++// The invariant is checked directly: while the callback naming the descriptorQuoted 8 lines
++// runs, that descriptor must still be open.
++#ifndef_WIN32
++struct SockStateCloseOrderData {
++size_tcloses_seen;
++size_tclosed_while_already_shut;
++};
++
++static void SockStateCloseOrderCallback(void *data,ares_socket_t
socket_fd,Quoted 3 lines
++ int readable, int writable)
++{
++ SockStateCloseOrderData *d = static_cast<SockStateCloseOrderData*>(data);
Quoted 19 lines
++ if (readable || writable) {
++ return;++ } ++ d->closes_seen++; ++ // F_GETFD on a closed descriptor fails with EBADF. c-ares must not have ++ // closed it yet at this point. ++ if (fcntl(static_cast<int>(socket_fd), F_GETFD) == -1 && errno == EBADF) {++ d->closed_while_already_shut++;
++ }
++}
++
++TEST_F(LibraryTest, SockStateCloseIsReportedBeforeDescriptorCloses) {++ SockStateCloseOrderData data; ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts;++
++ data.closes_seen = 0;
++ data.closed_while_already_shut = 0;
++Quoted 8 lines
++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateCloseOrderCallback; ++ opts.sock_state_cb_data = &data;++
++ EXPECT_EQ(ARES_SUCCESS,
++ares_init_options(&chan, &opts, ARES_OPT_SOCK_STATE_CB));
++ASSERT_NE(nullptr, chan);
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan,"127.0.0.1:53055"));
Quoted 3 lines
++
++ QueryResult result;
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 5 lines
++ QueryCallback, &result, NULL);
++ares_destroy(chan);
++
++EXPECT_GT(data.closes_seen, (size_t)0);
++EXPECT_EQ((size_t)0, data.closed_while_already_shut)Quoted 8 lines
++ << "the application was told to stop watching a descriptor c-ares had "
++ "already closed";
++}
++#endif
++
++// Reconfiguring the servers from an I/O hook frees the server and its ++// connection while the library is still holding both. Deferred reclamation is ++// what makes these safe: the objects are unlinked from every lookupstructure
Quoted 5 lines
++// immediately, so nothing can find them, but their memory survives until the ++// outermost call unwinds. Without it these are heap-use-after-free in ++//
ares_conn_connect()andhandle_conn_error()respectively. ++//
++// This is the class behind CVE-2025-31498 and CVE-2025-62408 -- aconnection
Quoted 8 lines
++// cleaned up while a caller further up the stack still held it. ++struct ReconfigureFromHookData {
++ ares_channel_t *channel; ++ bool done; ++ int status;++};
++
++static int ReconfigureConfigureCallback(ares_socket_t fd, int type, void*data)
Quoted 2 lines
++{
++ ReconfigureFromHookData *d = static_cast<ReconfigureFromHookData*>(data);
Quoted 5 lines
++ (void)fd; ++ (void)type; ++ // Frees the server this connection is being opened for. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056"); ++ return 0;Quoted 7 lines
++}
++
++static void ReconfigureFromHookCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec)
++{
++ ReconfigureFromHookData *d = static_cast<ReconfigureFromHookData*>(arg);
Quoted 17 lines
++ (void)timeouts; ++ (void)dnsrec; ++ d->done = true; ++ d->status = static_cast<int>(status); ++} ++ ++TEST_F(LibraryTest, ReconfigureInConfigureHookNoUseAfterFree) { ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ReconfigureFromHookData data; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS;Quoted 2 lines
++
++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&chan, &opts,optmask));
Quoted 2 lines
++
ASSERT_NE(nullptr, chan);
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan,"127.0.0.1:53055"));
Quoted 5 lines
++
++ data.channel = chan; ++ data.done = false; ++ data.status = ARES_SUCCESS; ++ ares_set_socket_configure_callback(chan, ReconfigureConfigureCallback,&data);
Quoted 2 lines
++
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 7 lines
++ ReconfigureFromHookCallback, &data, NULL);
++++ // Must complete rather than be stranded -- every exit from the send path ++ // either enqueues the query or ends it. ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ((size_t)0, ares_queue_active_queries(chan)); ++ ares_destroy(chan);Quoted 13 lines
++}
++
++// The second route to the same freed connection, and the one that drove the ++// change from suppressing the idle sweep to deferring the notification:
++// reconfiguring the servers from the socket-state callback destroys the server ++// and with it the connectionares_open_connection()is still using. Nothing ++// covered this route, so a regression in it would have gone unnoticed. ++struct SockStateReconfigureData {
++ares_channel_t*channel;
++size_tcalls;
++};
++
++static void SockStateReconfigureCallback(void *data,ares_socket_t
socket_fd,Quoted 22 lines
++ int readable, int writable)
++{
++ SockStateReconfigureData *d =
++ static_cast<SockStateReconfigureData *>(data);++ (void)socket_fd; ++ (void)readable; ++ (void)writable; ++ d->calls++; ++ if (d->channel != nullptr) {++ // Destroys the server, and with it the connection being opened.
++ares_set_servers_csv(d->channel, "127.0.0.1:53056");
++ }
++}
++
++TEST_F(LibraryTest, ReconfigureInSockStateCallbackNoUseAfterFree) {++ SockStateReconfigureData data; ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts;++
++ data.channel = nullptr;
++ data.calls = 0;
++Quoted 8 lines
++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateReconfigureCallback; ++ opts.sock_state_cb_data = &data;++
++ EXPECT_EQ(ARES_SUCCESS,
++ares_init_options(&chan, &opts, ARES_OPT_SOCK_STATE_CB));
++ASSERT_NE(nullptr, chan);
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan,"127.0.0.1:53055"));
Quoted 6 lines
++
++ /* Armed only now, so channel setup itself does not reconfigure. */ ++ data.channel = chan;
++
++ QueryResult result;
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 41 lines
++ QueryCallback, &result, NULL);
++
++EXPECT_GT(data.calls, (size_t)0);
++
++ data.channel = nullptr; /* don't reconfigure from teardown notifications */ ++ares_destroy(chan);
++}
++
++// Deferring the notifications must not lose them: every public entry point ++// that closes a socket still has to tell the application about it before it ++// returns, or an event loop is left polling a descriptor that c-ares closed. ++struct SockStateRecordData {
++ std::vector<std::pair<ares_socket_t, bool>> events; /* fd, still-armed */ ++};
++
++static void SockStateRecordCallback(void *data,ares_socket_tsocket_fd,
++ int readable, int writable)
++{
++ SockStateRecordData *d = static_cast<SockStateRecordData *>(data); ++d->events.push_back(std::make_pair(socket_fd, readable || writable)); ++}
++
++static boolSawClose(const SockStateRecordData &d)
++{
++ for (size_ti = 0; i <d.events.size(); i++) {
++ if (!d.events[i].second) {
++ return true;
++ }
++ }
++ return false;
++}
++
++class SockStateDeliveryTest : public LibraryTest {
++protected:
++ void OpenConnection(ares_channel_t **chan, SockStateRecordData *data,
++ QueryResult *result)
++ {++ struct ares_options opts; ++ memset(&opts, 0, sizeof(opts)); ++ opts.sock_state_cb = SockStateRecordCallback; ++ opts.sock_state_cb_data = data;Quoted 7 lines
++
++ ASSERT_EQ(ARES_SUCCESS,
++ares_init_options(chan, &opts, ARES_OPT_SOCK_STATE_CB));
++ASSERT_NE(nullptr, *chan);
++ /* TEST-NET-1, guaranteed not to answer, so the socket is still open when
++ * the call under test runs. */
++ ASSERT_EQ(ARES_SUCCESS, ares_set_servers_csv(*chan,"192.0.2.1:53"));
Quoted 2 lines
++
++ ares_query_dnsrec(*chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 9 lines
++ QueryCallback, result, NULL);
++ ASSERT_FALSE(result->done_); ++ ASSERT_FALSE(data->events.empty()); ++ ASSERT_TRUE(data->events.back().second); /* opened, armed for read */ ++ data->events.clear();++ }
++};
++
++TEST_F(SockStateDeliveryTest, CancelReportsSocketClose) {Quoted 7 lines
++ ares_channel_t *chan = nullptr; ++ SockStateRecordData data; ++ QueryResult result; ++ ++ OpenConnection(&chan, &data, &result); ++ ares_cancel(chan); ++ EXPECT_TRUE(SawClose(data)) << "ares_cancel() closed the socketwithout "
Quoted 11 lines
++ "telling the application";
++ares_destroy(chan);
++}
++
++TEST_F(SockStateDeliveryTest, SetServersReportsSocketClose) {++ ares_channel_t *chan = nullptr; ++ SockStateRecordData data; ++ QueryResult result;++
++OpenConnection(&chan, &data, &result);
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan,"198.51.100.1:53"));
Quoted 1 line
++ EXPECT_TRUE(SawClose(data)) << "
ares_set_servers_csv()closed thesocket "
Quoted 5 lines
++ "without telling the application";
++ares_destroy(chan);
++}
++
++TEST_F(SockStateDeliveryTest, DestroyReportsSocketClose) {Quoted 7 lines
++ ares_channel_t *chan = nullptr; ++ SockStateRecordData data; ++ QueryResult result; ++ ++ OpenConnection(&chan, &data, &result); ++ ares_destroy(chan); ++ EXPECT_TRUE(SawClose(data)) << "ares_destroy() closed the socketwithout "
Quoted 37 lines
++ "telling the application";
++}
++
++// The server-state callback is dispatched from the middle of processing, and ++// every caller keeps using its query afterwards. Nothing documents the ++// callback as non-reentrant, and the channel lock is recursive precisely so ++// API calls from callbacks are allowed -- so an application that cancels from ++// it left the library using a freed query.
++//
++// No hostile input is needed: an ordinary timeout reaches the same place. A ++// SERVFAIL is used here because it is deterministic in the harness. ++struct ServerStateCancelData {++ ares_channel_t *channel; ++ int server_cb_calls; ++ bool query_done;++};
++
++static void ServerStateCancelCallback(const char*server_string,
++ares_bool_tsuccess, int flags,
++ void *data) {++ ServerStateCancelData *d = static_cast<ServerStateCancelData *>(data); ++ (void)server_string; ++ (void)success; ++ (void)flags; ++ d->server_cb_calls++; ++ // Reentrant cancel from the notification. ++ ares_cancel(d->channel);++}
++
++static void ServerStateQueryCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec) {++ ServerStateCancelData *d = static_cast<ServerStateCancelData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ d->query_done = true;Quoted 4 lines
++}
++
++// The advisory's headline claim is that no hostile input is needed, because an ++// ordinary timeout reaches the same place. The SERVFAIL test below doesnot
Quoted 13 lines
++// show that; this one does.
++class ServerStateTimeoutCancelTest
++ : public MockChannelOptsTest,
++ public ::testing::WithParamInterface<int> {
++ public:
++ServerStateTimeoutCancelTest()
++ : MockChannelOptsTest(1,GetParam(), false, false,FillOptions(&opts_),
++ARES_OPT_TIMEOUTMS|ARES_OPT_TRIES) {}
++ static structares_options*FillOptions(structares_options*opts) {++ memset(opts, 0, sizeof(struct ares_options)); ++ opts->timeout = 100; ++ opts->tries = 3; ++ return opts;Quoted 7 lines
++ }
++
++ private:
++ structares_optionsopts_;
++};
++
++TEST_P(ServerStateTimeoutCancelTest,CancelInServerStateCallbackOnTimeout) {
Quoted 6 lines
++ ServerStateCancelData data; ++ data.channel = channel_; ++ data.server_cb_calls = 0; ++ data.query_done = false;++
++ ares_set_server_state_callback(channel_, ServerStateCancelCallback,&data);
Quoted 4 lines
++
++ // No reply configured, so the query times out and the notification comes ++ // from process_timeouts() rather than from a response. ++ EXPECT_EQ(ARES_SUCCESS,Quoted 10 lines
++ ares_query_dnsrec(channel_, "www.google.com",
ARES_CLASS_IN,
++ARES_REC_TYPE_A, ServerStateQueryCallback, &data,
++ NULL));
++Process();
++
++EXPECT_GT(data.server_cb_calls, 0);
++EXPECT_TRUE(data.query_done);
++}
++
++INSTANTIATE_TEST_SUITE_P(AddressFamilies,ServerStateTimeoutCancelTest,
Quoted 5 lines
++ ::testing::ValuesIn(ares::test::families),
++ PrintFamily);
++
++// Unregistering with a NULL callback discards whatever is still queued -- the ++// documented behaviour of deferring delivery, and the only way anapplication
Quoted 20 lines
++// can opt back out once notifications are in flight. ++struct ServerStateUnregisterData {
++ ares_channel_t *channel; ++ size_t server_cb_calls; ++ bool query_done;++};
++
++static void ServerStateUnregisterCallback(const char*server_string,
++ares_bool_tsuccess,
++ int flags, void*user_data)
++{
++ ServerStateUnregisterData *d =
++ static_cast<ServerStateUnregisterData *>(user_data);++ (void)server_string; ++ (void)success; ++ (void)flags; ++ d->server_cb_calls++; ++ // Clear ourselves from inside the first notification. Any notification ++ // still queued behind this one must be dropped rather than delivered. ++ ares_set_server_state_callback(d->channel, NULL, NULL);Quoted 3 lines
++}
++
++static void ServerStateUnregisterQueryCallback(void *arg,ares_status_tstatus,
Quoted 5 lines
++
size_ttimeouts,
++ constares_dns_record_t*dnsrec)
++{
++ ServerStateUnregisterData *d =
++ static_cast<ServerStateUnregisterData *>(arg);Quoted 7 lines
++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ d->query_done = true;++}
++
++TEST_P(ServerStateTimeoutCancelTest,UnregisterInCallbackDiscardsQueued) {
Quoted 11 lines
++ ServerStateUnregisterData data; ++ data.channel = channel_; ++ data.server_cb_calls = 0; ++ data.query_done = false;++
++ ares_set_server_state_callback(channel_, ServerStateUnregisterCallback,
++ &data);
++++ // No reply configured: the query times out on every try, so more than one ++ // notification is queued before the flush point is reached. ++ EXPECT_EQ(ARES_SUCCESS,Quoted 10 lines
++ ares_query_dnsrec(channel_, "www.google.com",
ARES_CLASS_IN,
++ARES_REC_TYPE_A,
++ ServerStateUnregisterQueryCallback, &data, NULL));
++Process();
++
++EXPECT_EQ((size_t)1, data.server_cb_calls);
++EXPECT_TRUE(data.query_done);
++}
++
++TEST_P(MockUDPChannelTest,CancelInServerStateCallbackNoUseAfterFree) {
Quoted 6 lines
++ DNSPacket servfail;
++ servfail.set_response().set_aa().set_rcode(SERVFAIL)
++ .add_question(newDNSQuestion("www.google.com", T_A));
++ON_CALL(server_, OnRequest("www.google.com", T_A))
++ .WillByDefault(SetReply(&server_, &servfail));
++Quoted 6 lines
++ ServerStateCancelData data; ++ data.channel = channel_; ++ data.server_cb_calls = 0; ++ data.query_done = false;++
++ ares_set_server_state_callback(channel_, ServerStateCancelCallback,&data);
Quoted 2 lines
++
++ ares_query_dnsrec(channel_, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 8 lines
++ ServerStateQueryCallback, &data, NULL);
++Process();
++
++EXPECT_GT(data.server_cb_calls, 0);
++EXPECT_TRUE(data.query_done);
++}
++
++// The deferred requeue list records the target server as a raw pointer, andQuoted 8 lines
++// the drain never revalidated it -- although it does revalidate the query, by ++// qid, for exactly the same reason. Application callbacks run from inside that ++// drain, and a callback may legally reconfigure the servers, which frees the ++// ones no longer in the list. A later entry in the same drain then dispatches ++// through a freed
ares_server_t.
++//
++// Two replies must be read in one batch: an ordinary answer whose callback ++// reconfigures, and a FORMERR carrying no OPT record, which is whatmakes
Quoted 17 lines
++//
process_answer()queue a retry *to the same server* (the EDNS fallback -- ++// the only site that records a server pointer at all). ++struct ReconfigureInCbData {
++ares_channel_t*channel;
++ bool done;
++};
++
++static void ReconfigureChannelCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec) {++ ReconfigureInCbData *data = static_cast<ReconfigureInCbData *>(arg); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ data->done = true; ++ /* Frees every server absent from the new configuration. */ ++ ares_set_servers_csv(data->channel, "127.0.0.1:53055");Quoted 3 lines
++}
++
++TEST_P(MockUDPChannelTest,ReconfigureInCallbackStaleRequeueServer) {
Quoted 8 lines
++ DNSPacket okrsp;
++ okrsp.set_response().set_aa()
++ .add_question(newDNSQuestion("www.google.com", T_A))
++ .add_answer(newDNSARR("www.google.com", 100, {1, 2, 3, 4}));
++ON_CALL(server_, OnRequest("www.google.com", T_A))
++ .WillByDefault(SetReply(&server_, &okrsp));
++
++ /* FORMERR with no OPT record: the server looks like it does notunderstand
Quoted 7 lines
++ * EDNS, so the query is queued for retry to this same server. */
++ DNSPacket formerr;
++ formerr.set_response().set_aa().set_rcode(FORMERR)
++ .add_question(newDNSQuestion("www.example.com", T_A));
++ON_CALL(server_, OnRequest("www.example.com", T_A))
++ .WillByDefault(SetReply(&server_, &formerr));
++Quoted 6 lines
++ ReconfigureInCbData data; ++ data.channel = channel_; ++ data.done = false;++
++ QueryResult second;
++ ares_query_dnsrec(channel_, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 2 lines
++ ReconfigureChannelCallback, &data, NULL);
++ ares_query_dnsrec(channel_, "www.example.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 46 lines
++ QueryCallback, &second, NULL);
++
++ /* Both replies have to be consumed in a single pass so they share one++ * deferred-requeue scope -- that is what puts the ENDQUERY entry whose ++ * callback reconfigures ahead of the REQUEUE entry holding a server ++ * pointer. The usual Process() loop lets c-ares read between the two, so ++ * they never share a batch; answer both first, then read once. */ ++ for (int i = 0; i < 4; i++) { ++ std::set<ares_socket_t> server_fds = fds(); ++ fd_set r; ++ struct timeval tv = {0, 20000}; ++ int nfds = 0; ++ FD_ZERO(&r); ++ for (ares_socket_t fd : server_fds) {++
FD_SET(fd, &r);
++ if ((int)fd >= nfds) {
++ nfds = (int)fd + 1;
++ }
++ }
++ if (select(nfds, &r, nullptr, nullptr, &tv)<= 0) {
++ break;
++ }
++ for (ares_socket_tfd :server_fds) {
++ if (FD_ISSET(fd, &r)) {
++ ProcessFD(fd);
++ }
++ }
++ }
++ {++ fd_set readers; ++ fd_set writers; ++ struct timeval tv = {0, 0}; ++ int nfds; ++ FD_ZERO(&readers); ++ FD_ZERO(&writers); ++ nfds = ares_fds(channel_, &readers, &writers); ++ select(nfds, &readers, &writers, nullptr, &tv); ++ ares_process(channel_, &readers, &writers);++ }
++
++EXPECT_TRUE(data.done);
++
++ /* With the fix the FORMERR query is correctly re-dispatched (to the newly++ * configured, unreachable server) rather than going through a freed one, so ++ * it is still outstanding here. Cancel it so its callback runs while its ++ * result object is still in scope. */Quoted 6 lines
++
ares_cancel(channel_);
++EXPECT_TRUE(second.done_);
++}
++
++//ares_servers_remove_stale()destroys each stale server in turn, and that ++// reaches application callbacks (ares_destroy_server()->
ares_close_sockets()Quoted 1 line
++// ->
ares_requeue_queries()->end_query()). A callback that reconfiguresthe
Quoted 11 lines
++// servers again destroys the servers the outer loop has not visited yet, so a ++// cached "next" node points at freed memory.
++struct NestedReconfigureData {
++ares_channel_t*channel;
++ bool nested;
++};
++
++static void NestedReconfigureCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec) {
++ NestedReconfigureData *data = static_cast<NestedReconfigureData*>(arg);
Quoted 19 lines
++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ if (data->nested) { ++ return; ++ } ++ data->nested = true; ++ // Destroys the stale servers the outer removal loop has not reached yet. ++ ares_set_servers_csv(data->channel, ""); ++} ++ ++TEST_F(LibraryTest, NestedReconfigureInCallbackRemoveStale) { ++ ares_channel_t *channel = nullptr; ++ struct ares_options opts; ++ int optmask = 0;++
++memset(&opts, 0, sizeof(opts));
++ /* One try per server, so tearing them down exhausts the query's retries and
++ * runs its callback from inside the removal loop. */Quoted 6 lines
++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS;++
++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts,optmask));
Quoted 22 lines
++ /* Three servers, so the loop still has entries after destroying the first.
++ * Nothing listens on these ports; no traffic is needed. */
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_set_servers_csv(channel, "127.0.0.1:53101,127.0.0.1:53102,"
++ "127.0.0.1:53103"));
++++ NestedReconfigureData data; ++ data.channel = channel; ++ data.nested = false;++
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_query_dnsrec(channel, "www.example.com",ARES_CLASS_IN,
++ARES_REC_TYPE_A, NestedReconfigureCallback,
++ &data, NULL));
++
++EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel, ""));
++EXPECT_TRUE(data.nested);
++
++ares_destroy(channel);
++}
++
++// The same nesting, but the nested reconfiguration *installs* a server ratherQuoted 8 lines
++// than clearing the list. Restarting the removal walk from the head then ++// re-judges that server against the outer call's list, decides it is stale and ++// deletes it -- so
ares_set_servers_csv()returns success and leaves the ++// channel with no servers at all. Memory-safe and still wrong. ++static void NestedReplaceCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec) {
++ NestedReconfigureData *data = static_cast<NestedReconfigureData*>(arg);
Quoted 21 lines
++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ if (data->nested) { ++ return; ++ } ++ data->nested = true; ++ // Installs a server the outer loop has never seen. ++ ares_set_servers_csv(data->channel, "127.0.0.1:53201"); ++} ++ ++TEST_F(LibraryTest, NestedReconfigureInCallbackKeepsNestedServers) { ++ ares_channel_t *channel = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ optmask |= ARES_OPT_TRIES; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS;Quoted 2 lines
++
++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts,optmask));
Quoted 1 line
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel,
"127.0.0.1:53101"));
Quoted 11 lines
++
++ NestedReconfigureData data; ++ data.channel = channel; ++ data.nested = false;++
++ EXPECT_EQ(ARES_SUCCESS,
++ ares_query_dnsrec(channel, "www.example.com",ARES_CLASS_IN,
++ARES_REC_TYPE_A, NestedReplaceCallback, &data,
++ NULL));
++
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel,"127.0.0.1:53301"));
Quoted 12 lines
++
EXPECT_TRUE(data.nested);
++++ // The nested call reconciled the whole list; what it installed must survive. ++ char *csv = ares_get_servers_csv(channel); ++ ASSERT_NE(nullptr, csv); ++ EXPECT_STREQ("127.0.0.1:53201", csv); ++ ares_free_string(csv);++
++ares_destroy(channel);
++}
++
+TEST_P(MockUDPChannelTest, CancelInCallbackNoDoubleFree){meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +3
-0+ DNSPacket reply; + reply.set_response().set_aa() +@@ -1940,6 +3007,7 @@ TEST_P(MockUDPChannelTest,CancelInCallbackNoDoubleFree) {
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy-
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +5
-0+ CancelInCbData data; + data.channel = channel_; + data.done = false; ++ data.cbs = 0; + ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN,
ARES_REC_TYPE_A,Review
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0001-ares_cancel-ares_destroy- new file +2
-0+ CancelChannelCallback, &data, NULL); + Process();(quoted diff) +0
-0server-state-notifications.patch b/meta-oe/recipes-support/c-ares/c-ares/0002- Deliver-deferred-socket-server-state-notifications.patch
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket-
Quoted 4 lines
new file mode 100644
index 0000000000..c34d3ab7c7meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +0
-0server-state-notifications.patch
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 1
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +1
-0@@ -0,0 +1,393 @@ +From f944c4718fd672c9c0befd65a340abb2ccfc1a31 Mon Sep 17 00:00:002001
Quoted 59 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +59
-0+From: Brad House <[email protected]> +Date: Sun, 18 Oct 2026 20:28:11 -0400 +Subject: [PATCH] Deliver deferred socket/server-state notifications only from + the outermost public call (#1340) +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Backport of f023a52a (#1340) to v1.34. + +The recent change that queues socket-state and server-state +notifications delivers them, and runs the deferred cleanup, only from +the outermost `ares_deferred_cb_leave()`. `ares_send_nolock()` brackets +`ares_send_query()` itself, but most public lookup functions never +opened a region of their own. When reached from `ares_getaddrinfo()`, +`ares_query_dnsrec()` and similar, the bracket inside +`ares_send_nolock()` was therefore the outermost one. Notifications were +then delivered from inside an internal call while the library was still +in the middle of the public call, and its callers were still using their +lookup state (the host query, the query id out-pointer, ...). A +server-state callback that reconfigures the servers, which +`ares_set_server_state_callback(3)` documents as allowed, could end that +lookup before the caller was done with it. + +This is a regression in unreleased code. No released version is +affected. + +The outermost region is now opened by the public entry points +themselves, under the channel lock: + +- `ares_send_dnsrec()` (also used by `ares_send()`) +- `ares_query_dnsrec()` (also used by `ares_query()`) +- `ares_search()`, `ares_search_dnsrec()` +- `ares_getaddrinfo()` (also used by `ares_gethostbyname()`) +- `ares_gethostbyaddr()`, `ares_getnameinfo()` +- `ares_process_pending_write()`: a failed flush closes the connection +and fails its queries over, and this function had no bracket at all. + +`ares_process_fds()` (and through it +`ares_process()`/`ares_process_fd()`), `ares_cancel()`, `ares_destroy()` +and the server configuration functions already had their own. The nested +bracket in `ares_send_nolock()` stays, because it keeps connections and +servers unlinked during the send readable until the send returns. +Internal sends made from query callbacks (search next domain, +getaddrinfo next lookup, requeue, server probes) already run inside the +processing bracket. + +The comments on `ares_deferred_cb_enter()` and the flush now state the +rule: the outermost region belongs to the public entry point, never to +an internal helper. + +The documented behaviour (notifications arrive before the outermost +c-ares call returns) does not change, so no man page changes. + +Two new `LibraryTest` cases in `test/ares-test-mock.cc`. The IPv6 server +cannot get a socket, so the first send fails over to the IPv4 server, +and a server-state callback then reconfigures the servers: + +- `ServerFailoverDuringGetAddrInfo`: the lookup completes cleanly. Itmeta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 70
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0+fails under ASAN without this change. +- `ServerFailoverDuringQuery`: the query id from `ares_query_dnsrec()` +is already reported by the time the query callback runs. It fails +without this change. + +The full non-live suite passes under ASAN/UBSAN and with +`-DCARES_WERROR=ON`. + +Signed-off-by: Brad House <[email protected]> +Upstream-Status: Backport [https://github.com/c-ares/c-ares/commit/f944c4718fd672c9c0befd65a340abb2ccfc1a31]
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 71
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +1
-0+GHSA: GHSA-wpr3-8q3r-4q8c, GHSA-97vq-4mxv-7vhm, GHSA-mw23-pfg6-2x9r, GHSA-rf6g-ww74-97qh, GHSA-v33j-65h4-4ch8, GHSA-pj7p-v3cw-hq66
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +8
-0+--- + src/lib/ares_conn.c | 4 +- + src/lib/ares_getaddrinfo.c | 5 ++ + src/lib/ares_gethostbyaddr.c | 5 ++ + src/lib/ares_getnameinfo.c | 5 ++ + src/lib/ares_private.h | 10 ++++ + src/lib/ares_process.c | 6 ++ + src/lib/ares_query.c | 5 ++meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 89
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0+ src/lib/ares_search.c | 10 ++++ + src/lib/ares_send.c | 13 ++++- + test/ares-test-mock.cc | 106 +++++++++++++++++++++++++++++++++++ + 10 files changed, 166 insertions(+), 3 deletions(-) + +diff --git a/src/lib/ares_conn.c b/src/lib/ares_conn.c +index 78f57e7d10..4cc0d1871d 100644 +--- a/src/lib/ares_conn.c ++++ b/src/lib/ares_conn.c +@@ -94,7 +94,9 @@ static void ares_deferred_cb_flush(ares_channel_t*channel)
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +5
-0+ { + /* Only the outermost frame delivers: anything above us on the stack may + * still be holding the connection a reentrant call from one of these +- * callbacks could free. ++ * callbacks could free, or lookup state of its own that a callback couldmeta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 104
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0++ * end. That is why the outermost frame belongs to the public entry point ++ * and never to an internal helper; see ares_deferred_cb_enter(). + * + * The dispatch below counts as a nesting level of its own. Without that, a + * callback that calls back into the library re-enters here through the +diff --git a/src/lib/ares_getaddrinfo.c b/src/lib/ares_getaddrinfo.c +index 6d9e21af4b..5d9ddb311e 100644 +--- a/src/lib/ares_getaddrinfo.c ++++ b/src/lib/ares_getaddrinfo.c +@@ -762,7 +762,12 @@ void ares_getaddrinfo(ares_channel_t *channel,const char *name,
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +7
-0+ return; + } + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel);meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 121
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0+ ares_getaddrinfo_int(channel, name, service, hints, callback, arg); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } + +diff --git a/src/lib/ares_gethostbyaddr.c b/src/lib/ares_gethostbyaddr.c +index 69c509ab11..b314359331 100644 +--- a/src/lib/ares_gethostbyaddr.c ++++ b/src/lib/ares_gethostbyaddr.c +@@ -112,7 +112,12 @@ void ares_gethostbyaddr(ares_channel_t *channel,const void *addr, int addrlen,
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +7
-0+ return; + } + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel);meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 138
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0+ ares_gethostbyaddr_nolock(channel, addr, addrlen, family, callback, arg); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } + +diff --git a/src/lib/ares_getnameinfo.c b/src/lib/ares_getnameinfo.c +index 0159354530..7365e9ed82 100644 +--- a/src/lib/ares_getnameinfo.c ++++ b/src/lib/ares_getnameinfo.c +@@ -194,7 +194,12 @@ void ares_getnameinfo(ares_channel_t *channel,const struct sockaddr *sa,
Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +12
-0+ } + + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + ares_getnameinfo_int(channel, sa, salen, flags_int, callback, arg); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } +meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 160
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0+diff --git a/src/lib/ares_private.h b/src/lib/ares_private.h +index 0a1029e0f8..9610ba7dab 100644 +--- a/src/lib/ares_private.h ++++ b/src/lib/ares_private.h +@@ -676,6 +676,16 @@ void ares_server_reap(ares_server_t *server); + * Nesting is counted; queued notifications are delivered by the outermost + * leave. + * ++ * The outermost region must be opened by the public entry point itself ++ * (ares_send_dnsrec(), ares_query_dnsrec(), ares_search(),
ares_getaddrinfo(),Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +6
-0++ * ares_process_fds(), ares_set_servers_*(), ...), with the channel lock ++ * held, around everything it does. Internal helpers such as ++ * ares_send_nolock() open nested regions of their own, but their callers ++ * often keep using lookup state afterwards -- a host or search query, a ++ * query id out-pointer -- that a callback reentering the library may end ++ * and free. If such a helper were the outermost region, its leave wouldmeta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 176
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0++ * run the callbacks while that state is still in use. ++ * + * \param[in] channel Initialized ares channel. + */ + void ares_deferred_cb_enter(ares_channel_t *channel); +diff --git a/src/lib/ares_process.c b/src/lib/ares_process.c +index 82f53610fb..1569408b1b 100644 +--- a/src/lib/ares_process.c ++++ b/src/lib/ares_process.c +@@ -490,6 +490,11 @@ void ares_process_pending_write(ares_channel_t*channel)
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 188
Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +12
-0+ return; + } + ++ /* A failed flush closes the connection and fails its queries over, which ++ * notifies. Deliver once we are done walking the servers, and keep ++ * anything unlinked meanwhile readable until then. */ ++ ares_deferred_cb_enter(channel); ++ + /* Set as untriggerd before calling into ares_conn_flush(), this is + * because its possible ares_conn_flush() might cause additional data to + * be enqueued if there is some form of exception so it will need to recurse. +@@ -513,6 +518,7 @@ void ares_process_pending_write(ares_channel_t*channel)
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 200
Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +12
-0+ } + } + ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + } + +diff --git a/src/lib/ares_query.c b/src/lib/ares_query.c +index ca3b6a9b73..0cf7f70760 100644 +--- a/src/lib/ares_query.c ++++ b/src/lib/ares_query.c +@@ -125,7 +125,12 @@ ares_status_t ares_query_dnsrec(ares_channel_t*channel, const char *name,
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 208
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +8
-0+ } + + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + status = ares_query_nolock(channel, name, dnsclass, type, callback, arg,qid);
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 217
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +9
-0++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + return status; + } +diff --git a/src/lib/ares_search.c b/src/lib/ares_search.c +index c605caf42c..ebc3d91e20 100644 +--- a/src/lib/ares_search.c ++++ b/src/lib/ares_search.c +@@ -463,7 +463,12 @@ void ares_search(ares_channel_t *channel, constchar *name, int dnsclass,
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 230
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +13
-0+ } + + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + ares_search_int(channel, dnsrec, ares_dnsrec_convert_cb, carg); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + + ares_dns_record_destroy(dnsrec); +@@ -481,7 +486,12 @@ ares_status_t ares_search_dnsrec(ares_channel_t*channel,
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +7
-0+ } + + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel);meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 247
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0+ status = ares_search_int(channel, dnsrec, callback, arg); ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + + return status; +diff --git a/src/lib/ares_send.c b/src/lib/ares_send.c +index 2e0482bf19..dd4a28544b 100644 +--- a/src/lib/ares_send.c ++++ b/src/lib/ares_send.c +@@ -246,8 +246,12 @@ ares_status_t ares_send_nolock(ares_channel_t*channel,
ares_server_t*server,meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 250
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +3
-0+ /* Perform the first query action. */ + + /* Sending opens and writes to connections and can mark a server down,both
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 262
Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +12
-0+- * of which notify. Deliver those once we are done with the connection and +- * the server, not part-way through. */ ++ * of which notify. This bracket keeps any connection or server unlinked ++ * during the send readable until it returns. It is never the outermost ++ * one: every public entry point that can get here, and query processing, ++ * holds its own, so delivery waits until the caller -- which may still be ++ * holding its own lookup state, as ares_getaddrinfo() does -- has unwound ++ * too. */ + ares_deferred_cb_enter(channel); + status = ares_send_query(server, query, &now); + ares_deferred_cb_leave(channel); +@@ -271,9 +275,14 @@ ares_status_t ares_send_dnsrec(ares_channel_t*channel,
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +9
-0+ } + + ares_channel_lock(channel); ++ /* Notifications raised along the way are delivered once this whole call ++ * has unwound, not from inside the internals it runs. See ++ * ares_deferred_cb_enter(). */ ++ ares_deferred_cb_enter(channel); + + status = ares_send_nolock(channel, NULL, 0, dnsrec, callback, arg, qid);meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 281
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0+ ++ ares_deferred_cb_leave(channel); + ares_channel_unlock(channel); + + return status; +diff --git a/test/ares-test-mock.cc b/test/ares-test-mock.cc +index 2158c5a913..b848c02434 100644 +--- a/test/ares-test-mock.cc ++++ b/test/ares-test-mock.cc +@@ -2533,6 +2533,112 @@ TEST_F(LibraryTest,ReconfigureInSockStateCallbackNoUseAfterFree) {
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0+ data.channel = nullptr; /* don't reconfigure from teardown notifications */ + ares_destroy(chan); + } ++// Server-state notifications raised while a public call is issuing its first ++// request must not be delivered until that whole call has unwound. A ++// failover handler that reconfigures the servers ends the in-flight request, ++// and with it the state the issuing call still has to finish with. The IPv6 ++// server cannot get a socket, so the first send fails over to the IPv4 one ++// and queues a failure notification for the handler below. ++static ares_socket_t FailoverNoIPv6Socket(int af, int type, int protocol,meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 301
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0++ void *user_data) ++{ ++ (void)user_data; ++ if (af == AF_INET6) { ++ return ARES_SOCKET_BAD; ++ } ++ return socket(af, type, protocol); ++} ++ ++static void FailoverOnServerDown(const char *server_string, ares_bool_tsuccess,
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0++ int flags, void *data) ++{ ++ (void)server_string; ++ (void)flags; ++ if (!success) { ++ ares_set_servers_csv(static_cast<ares_channel_t *>(data), ++ "127.0.0.1:53063"); ++ } ++} ++meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- line 321
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +10
-0++static ares_channel_t *FailoverChannel(ares_socket_functions *funcs) ++{ ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.tries = 1; ++ opts.flags = ARES_FLAG_NOSEARCH; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_init_options(&chan, &opts, ARES_OPT_TRIES |
ARES_OPT_FLAGS));Review
Quoted 73 lines
meta-oe/recipes-support/c-ares/c-ares/0002-Deliver-deferred-socket- new file +72
-0++ if (chan == nullptr) { ++ return nullptr; ++ } ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_set_servers_csv(chan, "[::1]:53061,127.0.0.1:53062")); ++ ares_set_server_state_callback(chan, FailoverOnServerDown, chan); ++ ++ memset(funcs, 0, sizeof(*funcs)); ++ funcs->asocket = FailoverNoIPv6Socket; ++ ares_set_socket_functions(chan, funcs, NULL); ++ return chan; ++} ++ ++TEST_F(LibraryTest, ServerFailoverDuringGetAddrInfo) { ++ ares_socket_functions funcs; ++ struct ares_addrinfo_hints hints; ++ AddrInfoResult result; ++ ares_channel_t *chan = FailoverChannel(&funcs); ++ ASSERT_NE(nullptr, chan); ++ ++ memset(&hints, 0, sizeof(hints)); ++ hints.ai_family = AF_INET; ++ ares_getaddrinfo(chan, "www.google.com", NULL, &hints, AddrInfoCallback, ++ &result); ++ ++ ares_cancel(chan); ++ EXPECT_TRUE(result.done_); ++ ares_destroy(chan); ++} ++ ++// Same, through ares_query_dnsrec(): the query id must already have been ++// reported to the caller by the time the query can complete. ++struct FailoverQueryData { ++ unsigned short qid; ++ unsigned short qid_at_callback; ++ bool done; ++}; ++ ++static void FailoverQueryCallback(void *data, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) ++{ ++ FailoverQueryData *d = static_cast<FailoverQueryData *>(data); ++ (void)status; ++ (void)timeouts; ++ (void)dnsrec; ++ d->qid_at_callback = d->qid; ++ d->done = true; ++} ++ ++TEST_F(LibraryTest, ServerFailoverDuringQuery) { ++ ares_socket_functions funcs; ++ FailoverQueryData data; ++ ares_channel_t *chan = FailoverChannel(&funcs); ++ ASSERT_NE(nullptr, chan); ++ ++ data.qid = 0; ++ data.qid_at_callback = 0; ++ data.done = false; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_query_dnsrec(chan, "www.google.com", ARES_CLASS_IN, ++ ARES_REC_TYPE_A, FailoverQueryCallback, &data, ++ &data.qid)); ++ ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ(data.qid, data.qid_at_callback); ++ ares_destroy(chan); ++} ++ + + // Deferring the notifications must not lose them: every public entry point + // that closes a socket still has to tell the application about it before it(quoted diff) +0
-0callbacks-their-own-copy.patch b/meta-oe/recipes-support/c-ares/c-ares/0003- ares_qcache-hand-callbacks-their-own-copy.patch
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand-
Quoted 4 lines
new file mode 100644
index 0000000000..02039e92e0meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +0
-0callbacks-their-own-copy.patch
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 1
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +1
-0@@ -0,0 +1,1054 @@ +From a21160045f9d7039225fa285be7cb1b02173a3a5 Mon Sep 17 00:00:002001
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 4
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0+From: Brad House <[email protected]> +Date: Sun, 18 Oct 2026 22:12:09 -0400 +Subject: [PATCH] [Backport v1.34] ares_qcache: hand callbacks their owncopy
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 10
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +6
-0+ of a cached answer (#1339) +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Backport of 31e2f51e8715d418fa8272a6c3a1ee651917fec5 (`ares_qcache:hand
Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +12
-0+callbacks their own copy of a cached answer`) to `v1.34`. See that +commit for the full description. + +**Conflicts:** +- `ares_private.h`: alignment only; formatted with `v1.34`'s +`.clang-format`. +- `test/ares-test-internal.cc`: context only — on `main` the new +`QcacheKeyDistinguishesQuestions` test follows the IDNA/punycode tests +(#1031), which are not on `v1.34`, so it is placed after the URI test +instead. + +**Test fixes from #1334:** carries the part of #1334 that applies to themeta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 32
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+tests this introduces — `ReentrantFlushInCallbackNoUseAfterFree` +reconfigures to `127.0.0.1:53055` instead of `127.0.0.55` (only routable +on Linux), and `UnknownQueryTypesDoNotShareCacheEntries` drops +`static_cast<ns_type>`, which does not compile on OpenBSD. + +Test file contents match `main`. Independent of the dd62bdfc backport +(#1338); applies cleanly with or without it. + +The full non-live suite passes 650/650 under ASAN+UBSAN and with +`-DCARES_WERROR=ON` on macOS.
ReentrantFlushInCallbackNoUseAfterFreemeta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 37
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +5
-0+is a heap-use-after-free in `ares_dns_record_rr_cnt()` on `v1.34` +without this change, and passes with it. + +Signed-off-by: Brad House <[email protected]> +Upstream-Status: Backport [https://github.com/c-ares/c-ares/commit/a21160045f9d7039225fa285be7cb1b02173a3a5]
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +6
-0+GHSA: GHSA-grv7-fq5q-9vfc, GHSA-wrgc-prmw-27xh +--- + docs/ares_init_options.3 | 11 +- + src/lib/ares_private.h | 44 +++- + src/lib/ares_process.c | 1 + + src/lib/ares_qcache.c | 212 ++++++++++++++++---meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 53
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+ src/lib/ares_send.c | 47 ++++- + test/ares-test-internal.cc | 54 +++++ + test/ares-test-mock.cc | 419 +++++++++++++++++++++++++++++++++++++ + 7 files changed, 743 insertions(+), 45 deletions(-) + +diff --git a/docs/ares_init_options.3 b/docs/ares_init_options.3 +index e6f6ee8ba6..1ce7fb8e30 100644 +--- a/docs/ares_init_options.3 ++++ b/docs/ares_init_options.3 +@@ -324,7 +324,16 @@ with the maximum number of seconds a query resultmay be cached which will
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 54
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +1
-0+ override a larger TTL in the response message. This must be a non-zerovalue
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 63
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +9
-0+ otherwise the cache will be disabled. Choose a reasonable value for your + application such as 300 (5 minutes) or 3600 (1 hour). The query cache is +-automatically flushed if a server configuration change is made. ++automatically flushed if a server configuration change is made. As of c-ares ++1.35.0, and c-ares 1.34.9 on the 1.34 branch, a cached answer reports its ++remaining time to live rather than the ++original value received from the server, so TTLs returned on a cache hit ++decrease as the entry ages. Two lookups share a cached answer only if their ++EDNS state matches: the version, the flag bits including DNSSEC OK, andevery
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 64
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +1
-0++EDNS option other than the DNS cookie and the advertised UDP payloadsize,
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +11
-0++which are both ignored. Options are compared in the order the application ++added them. An application that attaches its own options and varies them ++between otherwise identical lookups gets a separate cache entry for each. + .br + .TP 18 + .B ARES_OPT_EVENT_THREAD +diff --git a/src/lib/ares_private.h b/src/lib/ares_private.h +index 9610ba7dab..dd06d303b8 100644 +--- a/src/lib/ares_private.h ++++ b/src/lib/ares_private.h +@@ -153,6 +153,9 @@ struct ares_query {meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 85
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+ ares_timeval_t ts; /*!< Timestamp query was sent */ + ares_timeval_t timeout; + ares_channel_t *channel; ++ /*! Query cache key, derived from the caller's request before the send path ++ * can mutate query->query. NULL when the cache is disabled. */ ++ char *qcache_key; + + /* + * Node object for each list entry the query belongs to in order to +@@ -718,14 +721,47 @@ ares_status_tares_qcache_create(ares_rand_state
*rand_state,Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+ unsigned int max_ttl, + ares_qcache_t **cache_out); + void ares_qcache_flush(ares_qcache_t *cache); ++/*! Derive the query cache key for a request. Caller owns the result. ++ * ++ * \param[in] dnsrec Request to derive the key from. ++ * \return allocated key, or NULL on failure. ++ */ ++char *ares_qcache_calc_key(const ares_dns_record_t *dnsrec); ++meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 105
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++/*! Insert a response into the query cache. ++ * ++ * \param[in] channel Initialized channel object. ++ * \param[in] now Current timestamp. ++ * \param[in] query Query the response answers. Its cache key, derived ++ * when the query was created, becomes the entry key; a ++ * NULL key means caching is switched off. ++ * \param[in] dnsrec Response to cache. Duplicated internally; the caller ++ * retains ownership. ++ * \return ARES_SUCCESS if cached, ARES_ENOTIMP if caching is off orthe
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +7
-0++ * response is not cacheable, otherwise another status. ++ */ + ares_status_t ares_qcache_insert(ares_channel_t *channel, + const ares_timeval_t *now, + const ares_query_t *query, + const ares_dns_record_t *dnsrec); +-ares_status_t ares_qcache_fetch(ares_channel_t *channel,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 122
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+- const ares_timeval_t *now, +- const ares_dns_record_t *dnsrec, +- const ares_dns_record_t **dnsrec_resp); ++/*! Fetch a cached answer for a request. ++ * ++ * \param[in] channel Initialized channel object. ++ * \param[in] now Current timestamp. ++ * \param[in] key Cache key for the request, as produced by ++ * ares_qcache_calc_key(). Owned by the caller. ++ * \param[out] dnsrec_resp On ARES_SUCCESS, the cached answer,owned by the
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 128
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +6
-0++ * caller and destroyed with ++ * ares_dns_record_destroy(). Set to NULL on any ++ * other status, except ARES_EFORMERR, which is ++ * returned by the argument check before this is ++ * touched. ++ * \return ARES_SUCCESS on a hit, ARES_ENOTFOUND on a miss,
ARES_EFORMERRonQuoted 24 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +24
-0++ * invalid arguments. ++ */ ++ares_status_t ares_qcache_fetch(ares_channel_t *channel, ++ const ares_timeval_t *now, const char *key, ++ ares_dns_record_t **dnsrec_resp); + + void ares_metrics_record(const ares_query_t *query, ares_server_t *server, + ares_status_t status, const ares_dns_record_t *dnsrec); +diff --git a/src/lib/ares_process.c b/src/lib/ares_process.c +index 1569408b1b..88536704ad 100644 +--- a/src/lib/ares_process.c ++++ b/src/lib/ares_process.c +@@ -1859,6 +1859,7 @@ void ares_free_query(ares_query_t *query) + query->arg = NULL; + /* Deallocate the memory associated with the query */ + ares_dns_record_destroy(query->query); ++ ares_free(query->qcache_key); + + ares_free(query); + } +diff --git a/src/lib/ares_qcache.c b/src/lib/ares_qcache.c +index ad3bc83505..f48669b56c 100644 +--- a/src/lib/ares_qcache.c ++++ b/src/lib/ares_qcache.cmeta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 162
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+@@ -38,7 +38,7 @@ typedef struct { + time_t insert_ts; + } ares_qcache_entry_t; + +-static char *ares_qcache_calc_key(const ares_dns_record_t *dnsrec) ++char *ares_qcache_calc_key(const ares_dns_record_t *dnsrec) + { + ares_buf_t *buf = ares_buf_create(); + size_t i; +@@ -49,7 +49,22 @@ static char *ares_qcache_calc_key(const
ares_dns_record_t*dnsrec)meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 168
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +6
-0+ return NULL; /* LCOV_EXCL_LINE: DefensiveCoding */ + } + +- /* Format is OPCODE|FLAGS[|QTYPE1|QCLASS1|QNAME1]... */ ++ /* Format is ++ *OPCODE|FLAGS[|EDNS|VER|FLAGS[|ID:HEX]...]|QDCOUNT[|TYPE|CLASS|LEN:
QNAME]...meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 170
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +2
-0++ * ++ * The EDNS section matters: an answer fetched under one set of EDNSoptions
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 176
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +6
-0++ * is not interchangeable with one fetched under another. A request with no ++ * OPT record appends nothing; note that EDNS is on by default, so ordinary ++ * lookups do carry one and their keys did change with this. ++ * ++ * Everything a caller does not control is emitted before the questions, so ++ * the names are always last. A name reaches the key unescaped, soanything
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 186
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ * keyed after it could otherwise be imitated by a name containing that same ++ * text, giving two different requests one key. The question count is part ++ * of that: without it, a request with two questions and a request with one ++ * whose name spells out the second produce identical keys. Each name is ++ * also length-prefixed, so adjacent names cannot be made to run together ++ * even when the question counts match. */ + + status = ares_buf_append_str( + buf, ares_dns_opcode_tostr(ares_dns_record_get_opcode(dnsrec))); +@@ -63,7 +78,11 @@ static char *ares_qcache_calc_key(const
ares_dns_record_t*dnsrec)meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 199
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +13
-0+ } + + flags = ares_dns_record_get_flags(dnsrec); +- /* Only care about RD and CD */ ++ /* RD, CD and AD. AD is here because RFC 6840 section 5.7 defines it in a ++ * request as a signal that the requester wants the AD bit evaluated in the ++ * response, and section 5.8 says a validating resolver should only set AD ++ * in a response when the request carried DO or AD -- so an answer fetched ++ * without it can legitimately differ from one fetched with it. */ + if (flags & ARES_FLAG_RD) { + status = ares_buf_append_str(buf, "rd"); + if (status != ARES_SUCCESS) { +@@ -76,6 +95,95 @@ static char *ares_qcache_calc_key(const
ares_dns_record_t*dnsrec)Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +6
-0+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ + } + } ++ if (flags & ARES_FLAG_AD) { ++ status = ares_buf_append_str(buf, "ad"); ++ if (status != ARES_SUCCESS) {meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 215
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ } ++ ++ /* EDNS state. Skipped entirely when the request carries no OPT record. */ ++ { ++ const ares_dns_rr_t *opt = ares_dns_get_opt_rr_const(dnsrec); ++ ++ if (opt != NULL) { ++ size_t opt_cnt = ares_dns_rr_get_opt_cnt(opt,
ARES_RR_OPT_OPTIONS);Quoted 23 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +23
-0++ size_t j; ++ ++ status = ares_buf_append_str(buf, "|EDNS|"); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ status = ares_buf_append_num_hex( ++ buf, ares_dns_rr_get_u8(opt, ARES_RR_OPT_VERSION), 0); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ status = ares_buf_append_byte(buf, '|'); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ /* Carries the DO bit, so a DNSSEC-aware request cannot be served an ++ * answer that was fetched without it. */ ++ status = ares_buf_append_num_hex( ++ buf, ares_dns_rr_get_u16(opt, ARES_RR_OPT_FLAGS), 0); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 248
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ /* The advertised UDP payload size is deliberately excluded: it ++ * constrains how a response is transported, not what it says. A server ++ * that cannot fit the additional section may omit it without setting ++ * the TC bit (RFC 2181 section 9), so two sizes can produce answers ++ * that differ outside the answer section; that is accepted here. */ ++ for (j = 0; j < opt_cnt; j++) { ++ const unsigned char *val = NULL; ++ size_t val_len = 0; ++ unsigned short optid = ++ ares_dns_rr_get_opt(opt, ARES_RR_OPT_OPTIONS, j, &val,
&val_len);Quoted 30 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +30
-0++ ++ /* DNS cookies are per-server transport state and say nothing about ++ * the content of an answer. A cookie c-ares adds never reaches here ++ * -- ares_cookie_apply() writes to query->query, after this key has ++ * been derived -- so this only ever skips one the caller supplied. */ ++ if (optid == ARES_OPT_PARAM_COOKIE) { ++ continue; ++ } ++ ++ status = ares_buf_append_byte(buf, '|'); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ status = ares_buf_append_num_hex(buf, optid, 0); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ status = ares_buf_append_byte(buf, ':'); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ if (val != NULL) { ++ size_t k; ++ for (k = 0; k < val_len; k++) { ++ status = ares_buf_append_num_hex(buf, val[k], 2); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ } ++ }meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 288
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ } ++ } ++ } ++ ++ status = ares_buf_append_byte(buf, '|'); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ ++ status = ares_buf_append_num_dec(buf,
ares_dns_record_query_cnt(dnsrec), 0);meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 295
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +7
-0++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } + + for (i = 0; i < ares_dns_record_query_cnt(dnsrec); i++) { + const char *name; +@@ -93,7 +201,13 @@ static char *ares_qcache_calc_key(const
ares_dns_record_t*dnsrec)meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 300
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +5
-0+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ + } + +- status = ares_buf_append_str(buf, ares_dns_rec_type_tostr(qtype)); ++ /* Numeric, not the mnemonic: ares_dns_rec_type_tostr() returns"UNKNOWN"
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 310
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ * for every type outside its table, and ares_dns_record_query_add() does ++ * not validate the type, so any 16-bit value reaches here through the ++ * ordinary public API. Two such lookups -- LOC and DNAME, say -- would ++ * otherwise key identically and be answered with each other's records. ++ * The key only has to be injective, not readable. */ ++ status = ares_buf_append_num_dec(buf, (size_t)qtype, 0); + if (status != ARES_SUCCESS) { + goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ + } +@@ -103,7 +217,10 @@ static char *ares_qcache_calc_key(const
ares_dns_record_t*dnsrec)meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 315
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +5
-0+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ + } + +- status = ares_buf_append_str(buf, ares_dns_class_tostr(qclass)); ++ /* Numeric for the same reason. ares_dns_record_query_add() doesreject
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 322
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +7
-0++ * classes outside its list today, so the mnemonic is safe -- but the key ++ * should not depend on that validation staying where it is. */ ++ status = ares_buf_append_num_dec(buf, (size_t)qclass, 0); + if (status != ARES_SUCCESS) { + goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ + } +@@ -121,6 +238,21 @@ static char *ares_qcache_calc_key(const
ares_dns_record_t*dnsrec)Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +12
-0+ name_len--; + } + ++ /* Length-prefix the name so it is self-delimiting. The question count ++ * above stops one question imitating two, but with the counts equal the ++ * boundary between adjacent names is still guessable, since names reach ++ * the key unescaped separated only by '|'. A length makes each name ++ * unambiguous for any question count. */ ++ status = ares_buf_append_num_dec(buf, name_len, 0); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ }meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 344
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ ++ status = ares_buf_append_byte(buf, ':'); ++ if (status != ARES_SUCCESS) { ++ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ + if (name_len > 0) { + status = ares_buf_append(buf, (const unsigned char *)name, name_len); + if (status != ARES_SUCCESS) { +@@ -301,10 +433,10 @@ static unsigned int
ares_qcache_soa_minimum(ares_dns_record_t *dnsrec)Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +5
-0+ } + + /* On success, takes ownership of dnsrec */ +-static ares_status_t ares_qcache_insert_int(ares_qcache_t *qcache, +- ares_dns_record_t *qresp,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 359
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+- const ares_dns_record_t *qreq, +- const ares_timeval_t *now) ++static ares_status_t ares_qcache_insert_int(ares_qcache_t *qcache, ++ ares_dns_record_t *qresp, ++ const char *key, ++ const ares_timeval_t *now) + { + ares_qcache_entry_t *entry; + unsigned int ttl; +@@ -350,11 +482,11 @@ static ares_status_tares_qcache_insert_int(ares_qcache_t *qcache,
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 368
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +9
-0+ entry->expire_ts = (time_t)now->sec + (time_t)ttl; + entry->insert_ts = (time_t)now->sec; + +- /* We can't guarantee the server responded with the same flags as the +- * request had, so we have to re-parse the request in order to generate the +- * key for caching, but we'll only do this once we know for sure we really +- * want to cache it */ +- entry->key = ares_qcache_calc_key(qreq); ++ /* The key was derived from the caller's request when the query wascreated.
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 369
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +1
-0++ * It must not be recomputed from query->query here: the EDNS fallbackpath
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 376
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +7
-0++ * strips the OPT record from that record before a successful retry, so a ++ * key derived from it would not match the one a later lookup computes. */ ++ entry->key = ares_strdup(key); + if (entry->key == NULL) { + goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ + } +@@ -382,31 +514,25 @@ static ares_status_tares_qcache_insert_int(ares_qcache_t *qcache,
Quoted 28 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +28
-0+ /* LCOV_EXCL_STOP */ + } + +-ares_status_t ares_qcache_fetch(ares_channel_t *channel, +- const ares_timeval_t *now, +- const ares_dns_record_t *dnsrec, +- const ares_dns_record_t **dnsrec_resp) ++ares_status_t ares_qcache_fetch(ares_channel_t *channel, ++ const ares_timeval_t *now, const char *key, ++ ares_dns_record_t **dnsrec_resp) + { +- char *key = NULL; + ares_qcache_entry_t *entry; + ares_status_t status = ARES_SUCCESS; + +- if (channel == NULL || dnsrec == NULL || dnsrec_resp == NULL) { ++ if (channel == NULL || key == NULL || dnsrec_resp == NULL) { + return ARES_EFORMERR; + } + ++ *dnsrec_resp = NULL; ++ + if (channel->qcache == NULL) { + return ARES_ENOTFOUND; + } + + ares_qcache_expire(channel->qcache, now); +meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 414
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+- key = ares_qcache_calc_key(dnsrec); +- if (key == NULL) { +- status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */ +- goto done; /* LCOV_EXCL_LINE: OutOfMemory */ +- } +- + entry = ares_htable_strvp_get_direct(channel->qcache->cache, key); + if (entry == NULL) { + status = ARES_ENOTFOUND; +@@ -416,10 +542,24 @@ ares_status_t ares_qcache_fetch(ares_channel_t*channel,
Quoted 17 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +17
-0+ ares_dns_record_ttl_decrement(entry->dnsrec, + (unsigned int)(now->sec - entry->insert_ts)); + +- *dnsrec_resp = entry->dnsrec; ++ /* Hand back a copy the caller owns rather than the cache's own record. ++ * This is passed straight into an application callback, and that callback ++ * is allowed to re-enter c-ares. Any lookup it starts runs the expiry ++ * reaper first, and changing the server list flushes the cache outright -- ++ * either would destroy this entry while the callback still held a pointer ++ * into it. Mirrors the ownership fix already made on the insert side. */ ++ status = ares_dns_record_duplicate_ex(dnsrec_resp, entry->dnsrec); ++ if (status != ARES_SUCCESS) { ++ /* Could not hand back a private copy. Report a miss so the lookup falls ++ * through to a real query rather than failing outright: duplication ++ * writes and re-parses the record, so it can fail for reasons other than ++ * memory exhaustion, and none of them are a reason to fail a lookup that ++ * would have gone to a server a moment earlier. */meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 441
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ status = ARES_ENOTFOUND; /* LCOV_EXCL_LINE: UntestablePath */ ++ goto done; /* LCOV_EXCL_LINE: UntestablePath */ ++ } + + done: +- ares_free(key); + return status; + } + +@@ -428,13 +568,25 @@ ares_status_t ares_qcache_insert(ares_channel_t*channel,
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +11
-0+ const ares_query_t *query, + const ares_dns_record_t *dnsrec) + { +- ares_dns_record_t *dupdns = ares_dns_record_duplicate(dnsrec); ++ ares_dns_record_t *dupdns; + ares_status_t status; + ++ /* Checked before the duplicate below, which is a full write and re-parse: ++ * no key means caching is switched off, so the copy would be thrown away. ++ * ares_init_options() always creates the cache object -- it is disabled by ++ * a zero maximum TTL, not by a NULL pointer -- so this is the only placemeta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 462
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ * the distinction is visible here. */ ++ if (query->qcache_key == NULL) { ++ return ARES_ENOTIMP; ++ } ++ ++ dupdns = ares_dns_record_duplicate(dnsrec); + if (dupdns == NULL) { + return ARES_ENOMEM; + } +- status = ares_qcache_insert_int(channel->qcache, dupdns, query->query,now);
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 465
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ++ status = ++ ares_qcache_insert_int(channel->qcache, dupdns, query->qcache_key,now);
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 473
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +8
-0+ if (status != ARES_SUCCESS) { + ares_dns_record_destroy(dupdns); + } +diff --git a/src/lib/ares_send.c b/src/lib/ares_send.c +index dd4a28544b..117745ce28 100644 +--- a/src/lib/ares_send.c ++++ b/src/lib/ares_send.c +@@ -111,11 +111,12 @@ ares_status_t ares_send_nolock(ares_channel_t*channel,
ares_server_t*server,Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +8
-0+ ares_callback_dnsrec callback, void *arg, + unsigned short *qid) + { +- ares_query_t *query; +- ares_timeval_t now; +- ares_status_t status; +- unsigned short id = generate_unique_qid(channel); +- const ares_dns_record_t *dnsrec_resp = NULL;meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 491
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ ares_query_t *query; ++ ares_timeval_t now; ++ ares_status_t status; ++ unsigned short id = generate_unique_qid(channel); ++ ares_dns_record_t *dnsrec_resp = NULL; ++ char *qcache_key = NULL; + + /* A channel being destroyed accepts no new queries. ares_destroy(3) + * documents that a callback must not add new requests to a channel being +@@ -145,13 +146,28 @@ ares_status_t ares_send_nolock(ares_channel_t*channel,
ares_server_t*server,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 504
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +13
-0+ return ARES_ENOSERVER; + } + +- if (!(flags & ARES_SEND_FLAG_NOCACHE)) { ++ /* Derive the cache key once, from the caller's request, and only when the ++ * cache is actually on. The send path may mutate query->query afterwards ++ * -- the EDNS fallback strips its OPT record -- and a key derived from it ++ * later would not match. ares_init_options() always creates the cache ++ * object, so a zero maximum TTL is what "off" means. */ ++ if (channel->qcache_max_ttl > 0) { ++ qcache_key = ares_qcache_calc_key(dnsrec); ++ if (qcache_key == NULL) { ++ callback(arg, ARES_ENOMEM, 0, NULL); /* LCOV_EXCL_LINE:OutOfMemory */
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 514
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ return ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */ ++ } ++ } ++ ++ if (qcache_key != NULL && !(flags & ARES_SEND_FLAG_NOCACHE)) { + /* Check query cache */ +- status = ares_qcache_fetch(channel, &now, dnsrec, &dnsrec_resp); ++ status = ares_qcache_fetch(channel, &now, qcache_key, &dnsrec_resp); + if (status != ARES_ENOTFOUND) { + /* ARES_SUCCESS means we retrieved the cache, anything else is acritical
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 522
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +8
-0+ * failure, all result in termination */ ++ ares_free(qcache_key); + callback(arg, status, 0, dnsrec_resp); ++ ares_dns_record_destroy(dnsrec_resp); + return status; + } + } +@@ -159,8 +175,11 @@ ares_status_t ares_send_nolock(ares_channel_t*channel,
ares_server_t*server,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 526
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +4
-0+ /* Allocate space for query and allocated fields. */ + query = ares_malloc(sizeof(ares_query_t)); + if (!query) { +- callback(arg, ARES_ENOMEM, 0, NULL); /* LCOV_EXCL_LINE:OutOfMemory */
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 536
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+- return ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */ ++ /* LCOV_EXCL_START: OutOfMemory */ ++ ares_free(qcache_key); ++ callback(arg, ARES_ENOMEM, 0, NULL); ++ return ARES_ENOMEM; ++ /* LCOV_EXCL_STOP */ + } + memset(query, 0, sizeof(*query)); + +@@ -173,6 +192,10 @@ ares_status_t ares_send_nolock(ares_channel_t*channel,
ares_server_t*server,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 547
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +11
-0+ (channel->flags & ARES_FLAG_USEVC) ? ARES_TRUE : ARES_FALSE; + + /* Duplicate Query */ ++ /* The query adopts the key derived above; ares_free_query() releases it. */ ++ query->qcache_key = qcache_key; ++ qcache_key = NULL; ++ + status = ares_dns_record_duplicate_ex(&query->query, dnsrec); + if (status != ARES_SUCCESS) { + /* Sometimes we might get a EBADRESP response from duplicate due to +@@ -180,7 +203,11 @@ ares_status_t ares_send_nolock(ares_channel_t*channel,
ares_server_t*server,Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +13
-0+ if (status == ARES_EBADRESP) { + status = ARES_EBADQUERY; + } +- ares_free(query); ++ /* ares_free_query() rather than ares_free(): the query was zeroed above, ++ * so detaching finds every node NULL and the qid not yet registered, and ++ * it also releases qcache_key. Reachable without memory pressure -- a ++ * name that is not a valid hostname fails the duplicate. */ ++ ares_free_query(query); + callback(arg, status, 0, NULL); + return status; + } +diff --git a/test/ares-test-internal.cc b/test/ares-test-internal.ccmeta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 570
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+index 8881f3b7cc..5ba5d2b39b 100644 +--- a/test/ares-test-internal.cc ++++ b/test/ares-test-internal.cc +@@ -502,6 +502,60 @@ TEST_F(LibraryTest, URI) { + EXPECT_NE(ARES_SUCCESS, ares_uri_parse_buf(NULL, NULL)); + EXPECT_NE(ARES_SUCCESS, ares_uri_parse_buf(NULL, NULL)); + } ++ ++// The cache key must distinguish requests whose answers are not ++// interchangeable. Two shapes are easy to get wrong because names reachthe
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 571
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +1
-0++// key unescaped: a request with two questions versus one whose namespells out
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 572
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +1
-0++// the second, and two requests with the same question count whose namescan be
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 584
Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +12
-0++// made to run together. Both are narrow to reach -- they need a hand-built ++// record with more than one question, which real servers reject -- but the key ++// asserts they are distinct, so pin it. ++TEST_F(LibraryTest, QcacheKeyDistinguishesQuestions) { ++ struct qcase { ++ const char *n1; ++ const char *n2; /* nullptr for a single-question record */ ++ }; ++ const qcase cases[] = { ++ { "one.example.net", "two.example.net" }, ++ { "one.example.net|A|IN|two.example.net", nullptr }, ++ { "one.example.net", "two.example.net|A|IN|three.example.net"},
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 585
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +1
-0++ { "one.example.net|A|IN|two.example.net", "three.example.net"},
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 594
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +9
-0++ }; ++ std::vector<std::string> keys; ++ ++ for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { ++ ares_dns_record_t *req = nullptr; ++ char *key = nullptr; ++ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_create(&req, 0, ARES_FLAG_RD,
ARES_OPCODE_QUERY,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 601
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +7
-0++ ARES_RCODE_NOERROR)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_query_add(req, cases[i].n1, ARES_REC_TYPE_A, ++ ARES_CLASS_IN)); ++ if (cases[i].n2 != nullptr) { ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_query_add(req, cases[i].n2,
ARES_REC_TYPE_A,Quoted 29 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +29
-0++ ARES_CLASS_IN)); ++ } ++ ++ key = ares_qcache_calc_key(req); ++ ASSERT_NE(nullptr, key); ++ keys.push_back(std::string(key)); ++ ares_free_string(key); ++ ares_dns_record_destroy(req); ++ } ++ ++ /* Every one of these is a different request, so every key must differ. */ ++ for (size_t i = 0; i < keys.size(); i++) { ++ for (size_t j = i + 1; j < keys.size(); j++) { ++ EXPECT_NE(keys[i], keys[j]) ++ << "cache key collision between case " << i << " and case " << j ++ << ": " << keys[i]; ++ } ++ } ++} ++ + #endif /* !CARES_SYMBOL_HIDING */ + + TEST_F(LibraryTest, InetPtoN) { +diff --git a/test/ares-test-mock.cc b/test/ares-test-mock.cc +index b848c02434..4b409db7c3 100644 +--- a/test/ares-test-mock.cc ++++ b/test/ares-test-mock.cc +@@ -611,6 +611,425 @@ class CacheQueriesTest + struct ares_options opts_;meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 640
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0+ }; + ++// A cache hit used to hand the callback a pointer to the cache's own record. ++// That is fine only while the callback does nothing else -- but re-entering ++// c-ares from a callback is documented as allowed, and two ordinary things a ++// callback might do destroy the entry underneath it: changing the server list ++// flushes the whole cache, and starting another lookup runs the expiry reaper ++// first. The record must therefore be the caller's own copy. ++// ++// Here the callback changes the server list, which flushes the cache, andthen
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 651
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +11
-0++// reads the record it was handed. ++struct QcacheReentrantCbData { ++ ares_channel_t *channel; ++ bool done; ++ size_t ancount; ++}; ++ ++static void QcacheReentrantCallback(void *arg, ares_status_t status, ++ size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ QcacheReentrantCbData *data = static_cast<QcacheReentrantCbData*>(arg);
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 661
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ (void)timeouts; ++ data->done = true; ++ if (status != ARES_SUCCESS || dnsrec == NULL) { ++ return; ++ } ++ /* Documented re-entry. Reconfiguring servers flushes the query cache. */ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_set_servers_csv(data->channel, "127.0.0.1:53055")); ++ /* Still inside the callback, so this record must still be readable. */ ++ data->ancount = ares_dns_record_rr_cnt(dnsrec,
ARES_SECTION_ANSWER);meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 674
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +13
-0++} ++ ++TEST_P(CacheQueriesTest, ReentrantFlushInCallbackNoUseAfterFree) { ++ DNSPacket rsp; ++ rsp.set_response().set_aa() ++ .add_question(new DNSQuestion("www.google.com", T_A)) ++ .add_answer(new DNSARR("www.google.com", 100, {2, 3, 4, 5})); ++ ON_CALL(server_, OnRequest("www.google.com", T_A)) ++ .WillByDefault(SetReply(&server_, &rsp)); ++ ++ /* Populate the cache. */ ++ QueryResult first; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN,
ARES_REC_TYPE_A,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 685
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +11
-0++ QueryCallback, &first, NULL); ++ Process(); ++ EXPECT_TRUE(first.done_); ++ ++ /* Served from the cache, so the callback runs synchronously holding the ++ * record the cache handed it. */ ++ QcacheReentrantCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.ancount = 0; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN,
ARES_REC_TYPE_A,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 692
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +7
-0++ QcacheReentrantCallback, &data, NULL); ++ ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ((size_t)1, data.ancount); ++} ++ ++// The cache key covered the question and a couple of header flags butignored
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 695
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++// the request's OPT record entirely, so two requests differing only in their ++// EDNS options collided. An answer obtained under one set of options is not ++// interchangeable with one obtained under another -- most obviously, arequest
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +7
-0++// with the DO bit set must not be handed an answer fetched without it. ++// ++// Both requests here ask the identical question; only the EDNS state differs, ++// so the server must see two of them. ++TEST_P(CacheQueriesTest, EdnsOptionsSeparateCacheEntries) { ++ DNSPacket rsp; ++ rsp.set_response().set_aa()meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 712
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ .add_question(new DNSQuestion("www.google.com", T_A)) ++ .add_answer(new DNSARR("www.google.com", 100, {2, 3, 4, 5})); ++ EXPECT_CALL(server_, OnRequest("www.google.com", T_A)) ++ .Times(2) ++ .WillRepeatedly(SetReply(&server_, &rsp)); ++ ++ /* First: no OPT record at all. */ ++ ares_dns_record_t *req1 = nullptr; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_create(&req1, 0, ARES_FLAG_RD,
ARES_OPCODE_QUERY,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 715
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_RCODE_NOERROR)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_query_add(req1, "www.google.com",
ARES_REC_TYPE_A,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 728
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +13
-0++ ARES_CLASS_IN)); ++ QueryResult r1; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_send_dnsrec(channel_, req1, QueryCallback, &r1, nullptr)); ++ Process(); ++ EXPECT_TRUE(r1.done_); ++ EXPECT_EQ(ARES_SUCCESS, r1.status_); ++ ++ /* Second: same question, but carrying EDNS with the DO bit set. */ ++ ares_dns_record_t *req2 = nullptr; ++ ares_dns_rr_t *opt = nullptr; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_create(&req2, 0, ARES_FLAG_RD,
ARES_OPCODE_QUERY,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 731
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_RCODE_NOERROR)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_query_add(req2, "www.google.com",
ARES_REC_TYPE_A,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 734
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_CLASS_IN)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_rr_add(&opt, req2, ARES_SECTION_ADDITIONAL,"",
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 738
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +4
-0++ ARES_REC_TYPE_OPT, ARES_CLASS_IN, 0)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_rr_set_u16(opt, ARES_RR_OPT_UDP_SIZE, 1232)); ++ EXPECT_EQ(ARES_SUCCESS, ares_dns_rr_set_u8(opt,
ARES_RR_OPT_VERSION, 0));Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +13
-0++ /* DO */ ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_rr_set_u16(opt, ARES_RR_OPT_FLAGS, 0x8000)); ++ ++ QueryResult r2; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_send_dnsrec(channel_, req2, QueryCallback, &r2, nullptr)); ++ Process(); ++ EXPECT_TRUE(r2.done_); ++ EXPECT_EQ(ARES_SUCCESS, r2.status_); ++ ++ ares_dns_record_destroy(req1); ++ ares_dns_record_destroy(req2);meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 761
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++} ++ ++// The test above sets the DO bit, which lives in the OPT flags -- it never ++// reaches the loop that keys the individual EDNS options. That loop is what ++// separates two client subnets, so it needs its own coverage. ++// The mirror image of the tests above: two things are deliberately left out ++// of the cache key, and leaving them out is a security-relevant judgement -- ++// it declares that two requests differing only in these still have ++// interchangeable answers. Without a test, keying on either one later would ++// turn every request from an affected application into a permanent cachemiss
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 767
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +6
-0++// and nothing would fail. ++ ++// A DNS cookie is per-server transport state and says nothing about the ++// content of an answer, so two requests differing only in their cookie share ++// an entry. The server must therefore be asked exactly once. ++// Two query types with no mnemonic in ares_dns_rec_type_tostr() bothrender as
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 768
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +1
-0++// "UNKNOWN", so keying the mnemonic made every such type share oneentry -- a
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 770
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +2
-0++// lookup answered with a record of a type it never asked for. Any 16-bit ++// value reaches here through ares_query_dnsrec(); LOC (29) and DNAME(39) are
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 772
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +2
-0++// both real and both outside the table. ++TEST_P(CacheQueriesTest,UnknownQueryTypesDoNotShareCacheEntries) {
Quoted 50 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +50
-0++ DNSPacket rsp29; ++ rsp29.set_response().set_aa().add_question( ++ new DNSQuestion("example.net", 29)); ++ DNSPacket rsp39; ++ rsp39.set_response().set_aa().add_question( ++ new DNSQuestion("example.net", 39)); ++ ++ // One call per type: neither may be served the other's answer. ++ EXPECT_CALL(server_, OnRequest("example.net", 29)) ++ .Times(1) ++ .WillRepeatedly(SetReply(&server_, &rsp29)); ++ EXPECT_CALL(server_, OnRequest("example.net", 39)) ++ .Times(1) ++ .WillRepeatedly(SetReply(&server_, &rsp39)); ++ ++ QueryResult r1; ++ ares_query_dnsrec(channel_, "example.net", ARES_CLASS_IN, ++ static_cast<ares_dns_rec_type_t>(29), QueryCallback, &r1, ++ nullptr); ++ Process(); ++ EXPECT_TRUE(r1.done_); ++ ++ QueryResult r2; ++ ares_query_dnsrec(channel_, "example.net", ARES_CLASS_IN, ++ static_cast<ares_dns_rec_type_t>(39), QueryCallback, &r2, ++ nullptr); ++ Process(); ++ EXPECT_TRUE(r2.done_); ++} ++ ++// Handing back a duplicate materialises the pending TTL decrement, so a ++// callback that reads the TTL off the record it is given sees the remaining ++// lifetime rather than the value the server sent. docs/ares_init_options.3 ++// states this, so pin it. Only visible to a caller that does not duplicate ++// first -- QueryResult does, which is why the other cache tests cannot see it. ++static unsigned int g_qcache_hit_ttl = 0; ++ ++static void QcacheTtlCallback(void *arg, ares_status_t status, size_t timeouts, ++ const ares_dns_record_t *dnsrec) { ++ const ares_dns_rr_t *rr; ++ (void)arg; ++ (void)timeouts; ++ if (status != ARES_SUCCESS || dnsrec == NULL) { ++ return; ++ } ++ rr = ares_dns_record_rr_get_const(dnsrec, ARES_SECTION_ANSWER, 0); ++ g_qcache_hit_ttl = rr ? ares_dns_rr_get_ttl(rr) : 0; ++} ++ ++TEST_P(CacheQueriesTest, CacheHitReportsRemainingTtl) {meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 832
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ DNSPacket rsp; ++ rsp.set_response().set_aa() ++ .add_question(new DNSQuestion("www.google.com", T_A)) ++ .add_answer(new DNSARR("www.google.com", 100, {2, 3, 4, 5})); ++ EXPECT_CALL(server_, OnRequest("www.google.com", T_A)) ++ .Times(1) ++ .WillRepeatedly(SetReply(&server_, &rsp)); ++ ++ g_qcache_hit_ttl = 0; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN,
ARES_REC_TYPE_A,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 840
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +8
-0++ QcacheTtlCallback, nullptr, nullptr); ++ Process(); ++ EXPECT_EQ((unsigned int)100, g_qcache_hit_ttl); ++ ++ ares_sleep_time(2100); ++ ++ g_qcache_hit_ttl = 0; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 17 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +17
-0++ QcacheTtlCallback, nullptr, nullptr); ++ Process(); ++ // Served from the cache, and reporting what is left rather than the ++ // original 100. ++ EXPECT_GT(g_qcache_hit_ttl, (unsigned int)0); ++ EXPECT_LT(g_qcache_hit_ttl, (unsigned int)100); ++} ++ ++TEST_P(CacheQueriesTest, CookieDoesNotSplitCacheEntries) { ++ DNSPacket rsp; ++ rsp.set_response().set_aa() ++ .add_question(new DNSQuestion("www.google.com", T_A)) ++ .add_answer(new DNSARR("www.google.com", 100, {2, 3, 4, 5})); ++ EXPECT_CALL(server_, OnRequest("www.google.com", T_A)) ++ .Times(1) ++ .WillRepeatedly(SetReply(&server_, &rsp)); ++meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 867
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ const unsigned char ck1[] = {1, 2, 3, 4, 5, 6, 7, 8}; ++ const unsigned char ck2[] = {8, 7, 6, 5, 4, 3, 2, 1}; ++ const unsigned char *ck[2] = {ck1, ck2}; ++ QueryResult r[2]; ++ ++ for (int i = 0; i < 2; i++) { ++ ares_dns_record_t *req = nullptr; ++ ares_dns_rr_t *opt = nullptr; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_create(&req, 0, ARES_FLAG_RD,
ARES_OPCODE_QUERY,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 870
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_RCODE_NOERROR)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_query_add(req, "www.google.com",
ARES_REC_TYPE_A,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 873
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_CLASS_IN)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_rr_add(&opt, req, ARES_SECTION_ADDITIONAL,"",
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +6
-0++ ARES_REC_TYPE_OPT, ARES_CLASS_IN, 0)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_rr_set_u16(opt, ARES_RR_OPT_UDP_SIZE, 1232)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_rr_set_opt(opt, ARES_RR_OPT_OPTIONS, ++ ARES_OPT_PARAM_COOKIE, ck[i], sizeof(ck1)));meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 889
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ EXPECT_EQ(ARES_SUCCESS, ++ ares_send_dnsrec(channel_, req, QueryCallback, &r[i], nullptr)); ++ Process(); ++ EXPECT_TRUE(r[i].done_); ++ EXPECT_EQ(ARES_SUCCESS, r[i].status_); ++ ares_dns_record_destroy(req); ++ } ++} ++ ++// The advertised UDP payload size constrains how a response istransported,
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +8
-0++// not what it says, so it is excluded too. Same expectation: one request. ++TEST_P(CacheQueriesTest, UdpPayloadSizeDoesNotSplitCacheEntries) { ++ DNSPacket rsp; ++ rsp.set_response().set_aa() ++ .add_question(new DNSQuestion("www.google.com", T_A)) ++ .add_answer(new DNSARR("www.google.com", 100, {2, 3, 4, 5})); ++ EXPECT_CALL(server_, OnRequest("www.google.com", T_A)) ++ .Times(1)meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 907
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ .WillRepeatedly(SetReply(&server_, &rsp)); ++ ++ const unsigned short sizes[2] = {512, 1232}; ++ QueryResult r[2]; ++ ++ for (int i = 0; i < 2; i++) { ++ ares_dns_record_t *req = nullptr; ++ ares_dns_rr_t *opt = nullptr; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_create(&req, 0, ARES_FLAG_RD,
ARES_OPCODE_QUERY,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 910
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_RCODE_NOERROR)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_query_add(req, "www.google.com",
ARES_REC_TYPE_A,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 913
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_CLASS_IN)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_rr_add(&opt, req, ARES_SECTION_ADDITIONAL,"",
Quoted 22 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +22
-0++ ARES_REC_TYPE_OPT, ARES_CLASS_IN, 0)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_rr_set_u16(opt, ARES_RR_OPT_UDP_SIZE, sizes[i])); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_send_dnsrec(channel_, req, QueryCallback, &r[i], nullptr)); ++ Process(); ++ EXPECT_TRUE(r[i].done_); ++ EXPECT_EQ(ARES_SUCCESS, r[i].status_); ++ ares_dns_record_destroy(req); ++ } ++} ++ ++TEST_P(CacheQueriesTest, EdnsClientSubnetSeparateCacheEntries) { ++ DNSPacket rsp; ++ rsp.set_response().set_aa() ++ .add_question(new DNSQuestion("www.google.com", T_A)) ++ .add_answer(new DNSARR("www.google.com", 100, {2, 3, 4, 5})); ++ EXPECT_CALL(server_, OnRequest("www.google.com", T_A)) ++ .Times(2) ++ .WillRepeatedly(SetReply(&server_, &rsp)); ++ ++ /* 192.0.2.0/24 and 198.51.100.0/24 */meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 945
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ const unsigned char ecs1[] = {0x00, 0x01, 0x18, 0x00, 0xc0, 0x00, 0x02}; ++ const unsigned char ecs2[] = {0x00, 0x01, 0x18, 0x00, 0xc6, 0x33, 0x64}; ++ const unsigned char *ecs[2] = {ecs1, ecs2}; ++ QueryResult r[2]; ++ ++ for (int i = 0; i < 2; i++) { ++ ares_dns_record_t *req = nullptr; ++ ares_dns_rr_t *opt = nullptr; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_create(&req, 0, ARES_FLAG_RD,
ARES_OPCODE_QUERY,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 948
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_RCODE_NOERROR)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_query_add(req, "www.google.com",
ARES_REC_TYPE_A,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 951
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_CLASS_IN)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_rr_add(&opt, req, ARES_SECTION_ADDITIONAL,"",
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +7
-0++ ARES_REC_TYPE_OPT, ARES_CLASS_IN, 0)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_rr_set_u16(opt, ARES_RR_OPT_UDP_SIZE, 1232)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_rr_set_opt(opt, ARES_RR_OPT_OPTIONS, ++ ARES_OPT_PARAM_EDNS_CLIENT_SUBNET, ecs[i], ++ sizeof(ecs1)));meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 968
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ EXPECT_EQ(ARES_SUCCESS, ++ ares_send_dnsrec(channel_, req, QueryCallback, &r[i], nullptr)); ++ Process(); ++ EXPECT_TRUE(r[i].done_); ++ EXPECT_EQ(ARES_SUCCESS, r[i].status_); ++ ares_dns_record_destroy(req); ++ } ++} ++ ++// A server answering an EDNS query with FORMERR makes c-ares strip theOPT
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 969
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +1
-0++// record from query->query and retry. The cache key must not be derivedfrom
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 980
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +11
-0++// that record afterwards, or the entry is stored under a key no later lookup ++// computes and the cache silently stops working for that name. ++TEST_P(CacheQueriesTest, EdnsFallbackStillCaches) { ++ DNSPacket rspfail; ++ rspfail.set_response().set_aa().set_rcode(FORMERR) ++ .add_question(new DNSQuestion("www.google.com", T_A)); ++ DNSPacket rspok; ++ rspok.set_response().set_aa() ++ .add_question(new DNSQuestion("www.google.com", T_A)) ++ .add_answer(new DNSARR("www.google.com", 100, {1, 2, 3, 4})); ++ /* Two: the FORMERR and the retry. The second lookup must be a cachehit. */
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 987
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +7
-0++ EXPECT_CALL(server_, OnRequest("www.google.com", T_A)) ++ .Times(2) ++ .WillOnce(SetReply(&server_, &rspfail)) ++ .WillRepeatedly(SetReply(&server_, &rspok)); ++ ++ HostResult r1; ++ ares_gethostbyname(channel_, "www.google.com.", AF_INET,HostCallback, &r1);
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 993
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +6
-0++ Process(); ++ EXPECT_TRUE(r1.done_); ++ EXPECT_EQ(ARES_SUCCESS, r1.status_); ++ ++ HostResult r2; ++ ares_gethostbyname(channel_, "www.google.com.", AF_INET,HostCallback, &r2);
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 999
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +6
-0++ Process(); ++ EXPECT_TRUE(r2.done_); ++ EXPECT_EQ(ARES_SUCCESS, r2.status_); ++} ++ ++// The question name is caller-supplied and unescaped, so anything keyedafter
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 1001
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +2
-0++// it could be imitated by a name containing that same text. A request for ++// "example.net|EDNS|0|8000" with no OPT record must not collide with arequest
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +4
-0++// for "example.net" carrying EDNS with the DNSSEC OK bit set. ++TEST_P(CacheQueriesTest, EdnsSectionCannotBeImitatedByName) { ++ DNSPacket rsp; ++ rsp.set_response().set_aa()meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 1015
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ .add_question(new DNSQuestion("example.net", T_A)) ++ .add_answer(new DNSARR("example.net", 100, {5, 6, 7, 8})); ++ ON_CALL(server_, OnRequest("example.net", T_A)) ++ .WillByDefault(SetReply(&server_, &rsp)); ++ ++ /* Cache an answer for the real name, with EDNS and the DO bit set. */ ++ ares_dns_record_t *req = nullptr; ++ ares_dns_rr_t *opt = nullptr; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_create(&req, 0, ARES_FLAG_RD,
ARES_OPCODE_QUERY,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 1018
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_RCODE_NOERROR)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_query_add(req, "example.net",
ARES_REC_TYPE_A,meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 1021
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +3
-0++ ARES_CLASS_IN)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_rr_add(&opt, req, ARES_SECTION_ADDITIONAL,"",
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 1025
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +4
-0++ ARES_REC_TYPE_OPT, ARES_CLASS_IN, 0)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_rr_set_u16(opt, ARES_RR_OPT_UDP_SIZE, 1232)); ++ EXPECT_EQ(ARES_SUCCESS, ares_dns_rr_set_u16(opt,
ARES_RR_OPT_FLAGS,0x8000));Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +4
-0++ QueryResult r1; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_send_dnsrec(channel_, req, QueryCallback, &r1, nullptr)); ++ Process();meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- line 1039
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +10
-0++ EXPECT_TRUE(r1.done_); ++ EXPECT_EQ(ARES_SUCCESS, r1.status_); ++ ares_dns_record_destroy(req); ++ ++ /* A different name that spells out the EDNS suffix, with no OPT record. ++ * It must not be served the entry above; with the cache keyed correctly it ++ * is refused, because '|' is not valid in a hostname. */ ++ ares_dns_record_t *req2 = nullptr; ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_create(&req2, 0, ARES_FLAG_RD,
ARES_OPCODE_QUERY,Review
Quoted 16 lines
meta-oe/recipes-support/c-ares/c-ares/0003-ares_qcache-hand- new file +15
-0++ ARES_RCODE_NOERROR)); ++ EXPECT_EQ(ARES_SUCCESS, ++ ares_dns_record_query_add(req2, "example.net|EDNS|0|8000", ++ ARES_REC_TYPE_A, ARES_CLASS_IN)); ++ QueryResult r2; ++ ares_send_dnsrec(channel_, req2, QueryCallback, &r2, nullptr); ++ Process(); ++ EXPECT_TRUE(r2.done_); ++ EXPECT_NE(ARES_SUCCESS, r2.status_); ++ ares_dns_record_destroy(req2); ++} ++ + TEST_P(CacheQueriesTest, GetHostByNameCache) { + DNSPacket rsp; + rsp.set_response().set_aa()(quoted diff) +0
-0torn-down-by-application.patch b/meta-oe/recipes-support/c-ares/c-ares/0004- Handle-connections-torn-down-by-application.patch
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 4 lines
new file mode 100644
index 0000000000..f78e9726b0meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +0
-0down-by-application.patch
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- line 1
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +1
-0@@ -0,0 +1,1107 @@ +From 13a10dddbabc30b6fca8e23f9efff73fedba9401 Mon Sep 17 00:00:002001
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- line 4
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +3
-0+From: Brad House <[email protected]> +Date: Sun, 18 Oct 2026 22:51:53 -0400 +Subject: [PATCH] [Backport v1.34] Handle connections torn down byapplication
Quoted 19 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- line 12
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +8
-0+ socket functions (#1343) +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Backport of cb25d8bf297aed0aad8f6e0777ca0635cacc6cb1 to `v1.34`. See +that commit for the full description. +ares/commit/13a10dddbabc30b6fca8e23f9efff73fedba9401]
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +13
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares_channel_t*channel)meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 8 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +8
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares_channel_t*channel)meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 18 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +18
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*conn, void *data,
size_tlen,meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +1
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
read_bytes);meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 15 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +15
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*conn, const void *data,
size_tlen,meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +6
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
the
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 14 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +14
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
**conn_out,
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +12
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
connection
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +6
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*server,
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +11
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares_bool_tused_tcp)meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +11
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*channel)
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 36 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +36
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
generation) {
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +9
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*channel,
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 14 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +14
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares_bool_tcritical_failure,meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 16 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +16
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares_conn_query_write(ares_conn_t *conn,
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 16 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +3
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
notify_pending_write_cb_data);
++
++ /* The callback is application code and may have reconfigured the++ * servers, closing this connection with the query already buffered on ++ * it. Report it as a connection failure so the query is requeued rather ++ * than attached to the closed connection. */++ if (
conn->flags&ARES_CONN_FLAG_DEAD) {
++ returnARES_ECONNREFUSED;
++ }meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +4
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares_send_query_int(ares_server_t
*requested_server,meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +6
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ARES_ECONNREFUSED,meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 10 lines
++ * even if the send itself reported success or never happened, so the query
++ * is requeued below rather than attached to the closed connection. */meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +8
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
MockUDPSingleTryTest,
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +4
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
flush
Quoted 21 lines
++// that fails tears the connection down, which ends a query that is out of ++// retries and runs its callback right there. A callback that reconfigures the ++// servers removes the one being flushed, and the walk must not go on to use ++// that server, its connection or its place in the list.
++class MockTCPSingleTryStayOpenTest
++ : public MockChannelOptsTest,
++ public ::testing::WithParamInterface<int> {
++public:
++MockTCPSingleTryStayOpenTest()
++ : MockChannelOptsTest(1,GetParam(), true, false,FillOptions(&opts_),
++ARES_OPT_TRIES|ARES_OPT_FLAGS)
++ {
++ }
++
++ static structares_options*FillOptions(structares_options*opts) ++ {++ memset(opts, 0, sizeof(struct ares_options)); ++ opts->tries = 1; ++ /* ARES_OPT_FLAGS replaces the defaults, which include EDNS. */ ++ opts->flags = ARES_FLAG_STAYOPEN | ARES_FLAG_EDNS; ++ return opts;Quoted 12 lines
++ }
++
++private:
++ structares_optionsopts_;
++};
++
++static voidPendingWriteNotifyCallback(void *data)++{
++ *static_cast<bool *>(data) = true;
++}
++
++TEST_P(MockTCPSingleTryStayOpenTest,ReconfigureInPendingWriteFailureCallback) {
Quoted 10 lines
++ DNSPacket rsp;
++ rsp.set_response().set_aa()
++ .add_question(newDNSQuestion("www.google.com", T_A))
++ .add_answer(newDNSARR("www.google.com", 100, {2, 3, 4, 5}));
++ON_CALL(server_, OnRequest("www.google.com", T_A))
++ .WillByDefault(SetReply(&server_, &rsp));
++++ /* Answered, so the TCP connection is connected; STAYOPEN keeps it. */ ++ QueryResult first; ++ ares_query_dnsrec(channel_, "www.google.com", ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 5 lines
++ QueryCallback, &first, NULL);
++Process();
++EXPECT_TRUE(first.done_);
++
++ /* From here writes on that connection wait forares_process_pending_write.*/
Quoted 2 lines
++ bool pending = false;
++ ares_set_pending_write_cb(channel_, PendingWriteNotifyCallback,&pending);
Quoted 7 lines
++
++ CancelInCbData data; ++ data.channel = channel_; ++ data.done = false; ++ data.cbs = 0; ++ ares_query_dnsrec(channel_, "www.example.com", ARES_CLASS_IN,++
ARES_REC_TYPE_A, ReconfigureServersCallback, &data,NULL);
Quoted 9 lines
++
EXPECT_TRUE(pending);
++EXPECT_FALSE(data.done);
++++ /* ares_set_socket_functions() stores the pointer rather than copying it. */ ++ static ares_socket_functions sock_funcs; ++ memset(&sock_funcs, 0, sizeof(sock_funcs)); ++ sock_funcs.asendv = ares_sendv_fail; ++ ares_set_socket_functions(channel_, &sock_funcs, NULL); ++ LibraryTest::SetFailSend();Quoted 10 lines
++
++ares_process_pending_write(channel_);
++
++EXPECT_TRUE(data.done);
++EXPECT_EQ((size_t)1, data.cbs);
++
++ares_set_pending_write_cb(channel_, NULL, NULL); ++}
++ ++INSTANTIATE_TEST_SUITE_P(AddressFamilies,MockTCPSingleTryStayOpenTest,
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 7 lines
++ ::testing::ValuesIn(ares::test::families),
++ PrintFamily);
++
+TEST_P(MockUDPChannelTest, CancelInDestroyCallbackNoUseAfterFree){meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +3
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ReconfigureInConfigureHookNoUseAfterFree) {
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn-
Quoted 16 lines
meta-oe/recipes-support/c-ares/c-ares/0004-Handle-connections-torn- new file +16
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*iov,
Quoted 13 lines
++ int iovcnt, void
*user_data)
++{
++ ReconfigureFromSendData *d =
++ static_cast<ReconfigureFromSendData *>(user_data);++ ares_ssize_t len = 0; ++ (void)fd; ++ for (int i = 0; i < iovcnt; i++) { ++ len += static_cast<ares_ssize_t>(iov[i].iov_len); ++ } ++ // Frees the server this connection belongs to. Reconfiguring to the same ++ // list again is a no-op, so the retry is written normally. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056"); ++ return len;Quoted 7 lines
++}
++
++static void ReconfigureFromSendCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec)
++{
++ ReconfigureFromSendData *d = static_cast<ReconfigureFromSendData*>(arg);
Quoted 17 lines
++ (void)timeouts; ++ (void)dnsrec; ++ d->done = true; ++ d->status = static_cast<int>(status); ++} ++ ++TEST_F(LibraryTest, ReconfigureInSuccessfulSendNoUseAfterFree) { ++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ares_socket_functions funcs; ++ ReconfigureFromSendData data; ++ ares_socket_t socks[ARES_GETSOCK_MAXNUM]; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS;Quoted 2 lines
++
++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&chan, &opts,optmask));
Quoted 2 lines
++
ASSERT_NE(nullptr, chan);
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan,"127.0.0.1:53055"));
Quoted 9 lines
++
++ data.channel = chan; ++ data.done = false; ++ data.status = ARES_SUCCESS; ++ memset(&funcs, 0, sizeof(funcs)); ++ funcs.asendv = ReconfigureSendv; ++ ares_set_socket_functions(chan, &funcs, &data);++
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 51 lines
++ ReconfigureFromSendCallback, &data, NULL);
++++ // Moved to the new server and written there, so it is in flight on a ++ // socket the application can watch. ++ EXPECT_FALSE(data.done); ++ EXPECT_EQ((size_t)1, ares_queue_active_queries(chan)); ++ EXPECT_NE(0, ares_getsock(chan, socks, ARES_GETSOCK_MAXNUM)); ++ ++ ares_cancel(chan); ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ(ARES_ECANCELLED, data.status); ++ EXPECT_EQ((size_t)0, ares_queue_active_queries(chan)); ++ ares_destroy(chan);++}
++
++// The connection the send function tore down has already been reported as ++// closed, and its descriptor closed or handed to that notification. Nothing ++// after the send may report socket state for it: doing so names ++//ARES_SOCKET_BAD, a descriptor the application was never given. ++struct BadSocketStateData {
++size_tcalls;
++size_tbad;
++};
++
++static void BadSocketStateCallback(void *data,ares_socket_tsocket_fd,
++ int readable, int writable)
++{++ BadSocketStateData *d = static_cast<BadSocketStateData *>(data); ++ (void)readable; ++ (void)writable; ++ d->calls++; ++ if (socket_fd == ARES_SOCKET_BAD) {++ d->bad++;
++ }
++}
++
++TEST_F(LibraryTest, ReconfigureInSendReportsNoBadSocket) {++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ares_socket_functions funcs; ++ ReconfigureFromSendData data; ++ BadSocketStateData sock; ++ ++ memset(&opts, 0, sizeof(opts)); ++ memset(&sock, 0, sizeof(sock)); ++ opts.flags = ARES_FLAG_NOSEARCH; ++ optmask |= ARES_OPT_FLAGS; ++ opts.sock_state_cb = BadSocketStateCallback; ++ opts.sock_state_cb_data = &sock; ++ optmask |= ARES_OPT_SOCK_STATE_CB;Quoted 2 lines
++
++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&chan, &opts,optmask));
Quoted 2 lines
++
ASSERT_NE(nullptr, chan);
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan,"127.0.0.1:53055"));
Quoted 9 lines
++
++ data.channel = chan; ++ data.done = false; ++ data.status = ARES_SUCCESS; ++ memset(&funcs, 0, sizeof(funcs)); ++ funcs.asendv = ReconfigureSendv; ++ ares_set_socket_functions(chan, &funcs, &data);++
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 35 lines
++ ReconfigureFromSendCallback, &data, NULL);
++ares_cancel(chan);
++EXPECT_TRUE(data.done);
++++ EXPECT_NE((size_t)0, sock.calls); ++ EXPECT_EQ((size_t)0, sock.bad); ++ ares_destroy(chan);++}
++
++// The same reconfiguration from the send function, reached through the ++// deferred TCP write:ares_set_pending_write_cb()queues the write and the ++// application later flushes it withares_process_pending_write(). That flush ++// is its own outermost library call, so the connection and server the ++// reconfiguration closes must stay readable until it returns, and its walk of ++// the server list must not step from a list node the reconfiguration freed. ++// Covers both a send that reports success and one that fails. A failure ++// on the removed server must not be charged to the server that replaced it. ++struct PendingWriteReconfigureData {++ ares_channel_t *channel; ++ bool reconfigure; ++ bool fail; ++ bool pending; ++ size_t done; ++ size_t server_failures;++};
++
++static int PendingWriteConnect(ares_socket_t fd, const struct sockaddr *addr,
++ares_socklen_taddrlen, void*user_data)
++{++ (void)fd; ++ (void)addr; ++ (void)addrlen; ++ (void)user_data; ++ // Pretend the connection is in progress; the test signals it writable. ++ return 0;Quoted 3 lines
++}
++
++staticares_ssize_tPendingWriteSendv(ares_socket_t fd, const struct iovec*iov,
Quoted 71 lines
++ int iovcnt, void
*user_data)
++{
++ PendingWriteReconfigureData *d =
++ static_cast<PendingWriteReconfigureData *>(user_data);++ ares_ssize_t len = 0; ++ (void)fd; ++ for (int i = 0; i < iovcnt; i++) {++ len += static_cast<ares_ssize_t>(iov[i].iov_len);
++ }
++ if (!d->reconfigure) {
++ return len;++ } ++ d->reconfigure = false; ++ // Closes this connection and frees its server. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056"); ++ if (d->fail) {++#ifdef
USE_WINSOCK
++ WSASetLastError(WSAECONNREFUSED);
++#else
++ errno = ECONNREFUSED;
++#endif
++ return -1;
++ }
++ return len;
++}
++
++static voidPendingWriteNotify(void *user_data)
++{
++ PendingWriteReconfigureData *d =
++ static_cast<PendingWriteReconfigureData *>(user_data);
++d->pending= true;
++}
++
++static void PendingWriteServerState(const char*server_string,
++ares_bool_tsuccess, int flags,
++ void*user_data)
++{
++ PendingWriteReconfigureData *d =
++ static_cast<PendingWriteReconfigureData *>(user_data);++ (void)server_string; ++ (void)flags; ++ if (!success) {++ d->server_failures++;
++ }
++}
++
++static void PendingWriteCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec)
++{
++ PendingWriteReconfigureData *d =
++ static_cast<PendingWriteReconfigureData *>(arg);++ (void)timeouts; ++ (void)dnsrec; ++ EXPECT_EQ(ARES_ECANCELLED, status); ++ d->done++;++}
++
++static void ReconfigureInPendingWrite(bool fail) ++{++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ares_socket_functions funcs; ++ PendingWriteReconfigureData data; ++ ares_socket_t socks[ARES_GETSOCK_MAXNUM]; ++ int bitmask; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.flags = ARES_FLAG_NOSEARCH | ARES_FLAG_USEVC; ++ optmask |= ARES_OPT_FLAGS;Quoted 2 lines
++
++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&chan, &opts,optmask));
Quoted 2 lines
++
ASSERT_NE(nullptr, chan);
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan,"127.0.0.1:53055"));
Quoted 13 lines
++
++ data.channel = chan; ++ data.reconfigure = false; ++ data.fail = fail; ++ data.pending = false; ++ data.done = 0; ++ data.server_failures = 0; ++ memset(&funcs, 0, sizeof(funcs)); ++ funcs.aconnect = PendingWriteConnect; ++ funcs.asendv = PendingWriteSendv; ++ ares_set_socket_functions(chan, &funcs, &data); ++ ares_set_pending_write_cb(chan, PendingWriteNotify, &data); ++ ares_set_server_state_callback(chan, PendingWriteServerState, &data);Quoted 3 lines
++
++ // The first query opens the connection; its write waits for it to connect. ++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 11 lines
++ PendingWriteCallback, &data, NULL);
++ bitmask =ares_getsock(chan, socks, ARES_GETSOCK_MAXNUM); ++ASSERT_NE(0, ARES_GETSOCK_WRITABLE(bitmask, 0)); ++++ // Report it connected, which writes the first query. ++ ares_process_fd(chan, ARES_SOCKET_BAD, socks[0]); ++ EXPECT_FALSE(data.pending); ++ ++ // Now connected, so the second query's write is deferred to the ++ // application. ++ ares_query_dnsrec(chan, "www.example.com", ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 16 lines
++ PendingWriteCallback, &data, NULL);
++EXPECT_TRUE(data.pending);
++++ data.reconfigure = true; ++ ares_process_pending_write(chan); ++ EXPECT_FALSE(data.reconfigure); ++ ++ // Both queries were moved to the new server, which has not failed. ++ EXPECT_EQ((size_t)0, data.done); ++ EXPECT_EQ((size_t)0, data.server_failures); ++ EXPECT_EQ((size_t)2, ares_queue_active_queries(chan)); ++ ++ ares_cancel(chan); ++ EXPECT_EQ((size_t)2, data.done); ++ EXPECT_EQ((size_t)0, ares_queue_active_queries(chan)); ++ ares_destroy(chan);Quoted 3 lines
++}
++
++TEST_F(LibraryTest, ReconfigureInSuccessfulPendingWriteNoUseAfterFree){
Quoted 8 lines
++ ReconfigureInPendingWrite(false);
++}
++
++TEST_F(LibraryTest, ReconfigureInFailedPendingWriteNoUseAfterFree) { ++ ReconfigureInPendingWrite(true);
++}
++
++// The pending-write callback is application code too, and runs once thequery
Quoted 26 lines
++// has been buffered on the connection. Reconfiguring from it closes that ++// connection, so the query must be requeued rather than attached to it. ++static void
PendingWriteReconfigureNotify(void *user_data)
++{
++ PendingWriteReconfigureData *d =
++ static_cast<PendingWriteReconfigureData *>(user_data);
++d->pending= true;
++ if (d->reconfigure) {++ d->reconfigure = false; ++ // Closes the connection the query was just buffered on. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056");++ }
++}
++
++TEST_F(LibraryTest, ReconfigureInPendingWriteCallbackNoUseAfterFree) {++ ares_channel_t *chan = nullptr; ++ struct ares_options opts; ++ int optmask = 0; ++ ares_socket_functions funcs; ++ PendingWriteReconfigureData data; ++ ares_socket_t socks[ARES_GETSOCK_MAXNUM]; ++ int bitmask; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.flags = ARES_FLAG_NOSEARCH | ARES_FLAG_USEVC; ++ optmask |= ARES_OPT_FLAGS;Quoted 2 lines
++
++ EXPECT_EQ(ARES_SUCCESS, ares_init_options(&chan, &opts,optmask));
Quoted 2 lines
++
ASSERT_NE(nullptr, chan);
++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(chan,"127.0.0.1:53055"));
Quoted 12 lines
++
++ data.channel = chan; ++ data.reconfigure = false; ++ data.fail = false; ++ data.pending = false; ++ data.done = 0; ++ data.server_failures = 0; ++ memset(&funcs, 0, sizeof(funcs)); ++ funcs.aconnect = PendingWriteConnect; ++ funcs.asendv = PendingWriteSendv; ++ ares_set_socket_functions(chan, &funcs, &data); ++ ares_set_pending_write_cb(chan, PendingWriteReconfigureNotify, &data);Quoted 3 lines
++
++ // The first query opens the connection; its write waits for it to connect. ++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 4 lines
++ PendingWriteCallback, &data, NULL);
++ bitmask =ares_getsock(chan, socks, ARES_GETSOCK_MAXNUM); ++ASSERT_NE(0, ARES_GETSOCK_WRITABLE(bitmask, 0)); ++Quoted 8 lines
++ // Report it connected, which writes the first query. ++ ares_process_fd(chan, ARES_SOCKET_BAD, socks[0]); ++ EXPECT_FALSE(data.pending); ++ ++ // Now connected, so the second query is buffered and the pending-write ++ // callback runs, reconfiguring the servers. ++ data.reconfigure = true; ++ ares_query_dnsrec(chan, "www.example.com", ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 12 lines
++ PendingWriteCallback, &data, NULL);
++EXPECT_TRUE(data.pending);
++EXPECT_FALSE(data.reconfigure);
++++ // Both queries were moved to the new server. ++ EXPECT_EQ((size_t)0, data.done); ++ EXPECT_EQ((size_t)2, ares_queue_active_queries(chan)); ++ ++ ares_cancel(chan); ++ EXPECT_EQ((size_t)2, data.done); ++ EXPECT_EQ((size_t)0, ares_queue_active_queries(chan)); ++ ares_destroy(chan);Quoted 6 lines
++}
++
++// Fake socket functions, installed withares_set_socket_functions_ex(), that ++// never touch a real descriptor. Each test arms one of them to reconfigure ++// the servers part-way through c-ares's use of a connection, and they count ++// every call that namesARES_SOCKET_BAD, which a closed connectionmust never
Quoted 65 lines
++// pass to application code.
++struct FakeSockData {++ ares_channel_t *channel; ++ ares_socket_t next_fd; ++ bool tfo; ++ bool getsockname_fail_first; ++ ares_socket_t getsockname_last_fd; ++ bool getsockname_reconfigure; ++ bool recv_reconfigure; ++ bool recv_answer_then_reconfigure; ++ bool recv_answered; ++ bool recv_answer; ++ bool close_reconfigure; ++ unsigned char query[512]; ++ size_t query_len; ++ struct sockaddr_in peer; ++ size_t recv_calls; ++ size_t bad_fd_calls; ++ size_t sock_state_bad; ++ bool done; ++ int status;++};
++
++static void FakeSockSetErrno(bool wouldblock)
++{
++#ifdefUSE_WINSOCK
++ WSASetLastError(wouldblock ? WSAEWOULDBLOCK : WSAENOTCONN); ++#else
++ errno = wouldblock ? EAGAIN : ENOTCONN;
++#endif
++}
++
++staticares_socket_tFakeSockSocket(int domain, int type, int protocol,
++ void*user_data)
++{++ FakeSockData *d = static_cast<FakeSockData *>(user_data); ++ (void)domain; ++ (void)type; ++ (void)protocol; ++ return d->next_fd++;++}
++
++static intFakeSockClose(ares_socket_t sock, void *user_data)++{
++ FakeSockData *d = static_cast<FakeSockData *>(user_data); ++ if (sock ==ARES_SOCKET_BAD) {
++ d->bad_fd_calls++;
++ }
++ if (d->close_reconfigure) {++ d->close_reconfigure = false; ++ // Frees the server this connection belonged to. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056");++ }
++ return 0;
++}
++
++static int FakeSockSetsockopt(ares_socket_t sock,ares_socket_opt_topt,
++ const void *val,ares_socklen_tval_size,
++ void*user_data)
++{++ FakeSockData *d = static_cast<FakeSockData *>(user_data); ++ (void)sock; ++ (void)val; ++ (void)val_size; ++ if (opt == ARES_SOCKET_OPT_TCP_FASTOPEN && !d->tfo) {Quoted 6 lines
++ return -1;
++ }
++ return 0;
++}
++
++static int FakeSockConnect(ares_socket_t sock, const struct sockaddr*address,
Quoted 68 lines
++
ares_socklen_taddress_len, unsigned int flags,
++ void*user_data)
++{++ FakeSockData *d = static_cast<FakeSockData *>(user_data); ++ (void)sock; ++ (void)flags; ++ if (address->sa_family == AF_INET && address_len >= sizeof(d->peer)) {++
memcpy(&d->peer, address, sizeof(d->peer));
++ }
++ return 0;
++}
++
++staticares_ssize_tFakeSockRecvfrom(ares_socket_t sock, void *buffer,++ size_t length, int flags, ++ struct sockaddr *address, ++ ares_socklen_t *address_len, ++ void *user_data) ++{ ++ FakeSockData *d = static_cast<FakeSockData *>(user_data); ++ (void)flags; ++ d->recv_calls++; ++ if (sock == ARES_SOCKET_BAD) {++ d->bad_fd_calls++;
++ }
++ if (d->query_len< 12 || length <d->query_len) {
++ FakeSockSetErrno(true);
++ return -1;
++ }
++ if (d->recv_answer_then_reconfigure) {
++ // Answer first; reconfigure on the next call, which has nothing to read.
++ if (d->recv_answered) {++ d->recv_answer_then_reconfigure = false; ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056"); ++ FakeSockSetErrno(true); ++ return -1; ++ } ++ d->recv_answered = true; ++ } else if (d->recv_reconfigure) { ++ d->recv_reconfigure = false; ++ // Frees the server this connection belongs to. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056");++ } else if (
d->recv_answer) {
++d->recv_answer= false;
++ } else {
++ FakeSockSetErrno(true);
++ return -1;++ } ++ // Answer the query: the same message with QR set, no records. ++ memcpy(buffer, d->query, d->query_len); ++ static_cast<unsigned char *>(buffer)[2] |= 0x80; ++ if (address != NULL && address_len != NULL &&++
*address_len>= (ares_socklen_t)sizeof(d->peer)) {
++memcpy(address, &d->peer, sizeof(d->peer));
++*address_len=sizeof(d->peer);
++ }
++ return static_cast<ares_ssize_t>(d->query_len);
++}
++
++staticares_ssize_tFakeSockSendto(ares_socket_t sock, const void *buffer,++ size_t length, int flags, ++ const struct sockaddr *address, ++ ares_socklen_t address_len, void *user_data) ++{ ++ FakeSockData *d = static_cast<FakeSockData *>(user_data); ++ (void)flags; ++ (void)address; ++ (void)address_len; ++ if (sock == ARES_SOCKET_BAD) {Quoted 10 lines
++ d->bad_fd_calls++;
++ }
++ if (length <=sizeof(d->query)) {
++memcpy(d->query, buffer, length);
++d->query_len= length;
++ }
++ return static_cast<ares_ssize_t>(length);
++}
++
++static int FakeSockGetsockname(ares_socket_t sock, struct sockaddr*address,
Quoted 66 lines
++
ares_socklen_t*address_len, void*user_data)
++{++ FakeSockData *d = static_cast<FakeSockData *>(user_data); ++ struct sockaddr_in sin; ++ if (sock == ARES_SOCKET_BAD) { ++ d->bad_fd_calls++; ++ } ++ // As during TCP Fast Open, before the first write has connected. ++ if (d->getsockname_fail_first && sock != d->getsockname_last_fd) { ++ d->getsockname_last_fd = sock; ++ FakeSockSetErrno(false); ++ return -1; ++ } ++ if (d->getsockname_reconfigure) { ++ d->getsockname_reconfigure = false; ++ // Frees the server this connection belongs to. ++ ares_set_servers_csv(d->channel, "127.0.0.1:53056");++ }
++ if (*address_len< (ares_socklen_t)sizeof(sin)) {
++ FakeSockSetErrno(false);
++ return -1;++ } ++ memset(&sin, 0, sizeof(sin)); ++ sin.sin_family = AF_INET; ++ sin.sin_port = htons(40000); ++ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); ++ memcpy(address, &sin, sizeof(sin)); ++ *address_len = sizeof(sin); ++ return 0;++}
++
++static void FakeSockStateCallback(void *data,ares_socket_tsocket_fd,
++ int readable, int writable)
++{++ FakeSockData *d = static_cast<FakeSockData *>(data); ++ (void)readable; ++ (void)writable; ++ if (socket_fd == ARES_SOCKET_BAD) {++ d->sock_state_bad++;
++ }
++}
++
++static void FakeSockQueryCallback(void *arg,ares_status_tstatus,
++size_ttimeouts,
++ constares_dns_record_t*dnsrec)
++{++ FakeSockData *d = static_cast<FakeSockData *>(arg); ++ (void)timeouts; ++ (void)dnsrec; ++ d->done = true; ++ d->status = static_cast<int>(status);++}
++
++static void FakeSockInit(ares_channel_t **chan, FakeSockData *data,
++ intextra_flags, unsigned intsockfunc_flags,
++ boolsock_state= true)
++{++ struct ares_options opts; ++ int optmask = 0; ++ struct ares_socket_functions_ex funcs; ++ ++ memset(&opts, 0, sizeof(opts)); ++ opts.flags = ARES_FLAG_NOSEARCH | extra_flags; ++ optmask |= ARES_OPT_FLAGS; ++ // A socket-state callback defers the close of a connection's descriptor ++ // until the outermost call unwinds; without one it is closed inline.Quoted 8 lines
++ if (sock_state) { ++ opts.sock_state_cb = FakeSockStateCallback; ++ opts.sock_state_cb_data = data; ++ optmask |= ARES_OPT_SOCK_STATE_CB; ++ } ++ ASSERT_EQ(ARES_SUCCESS, ares_init_options(chan, &opts, optmask)); ++ ASSERT_NE(nullptr, *chan); ++ EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(*chan,"127.0.0.1:53055"));
Quoted 16 lines
++
++ data->channel = *chan; ++ data->next_fd = 100; ++ data->getsockname_last_fd = ARES_SOCKET_BAD; ++ ++ memset(&funcs, 0, sizeof(funcs)); ++ funcs.version = 1; ++ funcs.flags = sockfunc_flags; ++ funcs.asocket = FakeSockSocket; ++ funcs.aclose = FakeSockClose; ++ funcs.asetsockopt = FakeSockSetsockopt; ++ funcs.aconnect = FakeSockConnect; ++ funcs.arecvfrom = FakeSockRecvfrom; ++ funcs.asendto = FakeSockSendto; ++ funcs.agetsockname = FakeSockGetsockname; ++ EXPECT_EQ(ARES_SUCCESS, ares_set_socket_functions_ex(*chan,&funcs, data));
Quoted 9 lines
++}
++
++// The getsockname function runs for every new connection, after the checks ++// that follow the socket-configure and socket-create callbacks. Reconfiguring ++// from it must not leave the connection registered on the removed server. ++TEST_F(LibraryTest, ReconfigureInGetsocknameNoUseAfterFree) {
++ares_channel_t*chan = nullptr;
++ FakeSockData data;
++Quoted 5 lines
++ memset(&data, 0, sizeof(data)); ++ FakeSockInit(&chan, &data, 0, 0); ++ data.getsockname_reconfigure = true;++
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 26 lines
++ FakeSockQueryCallback, &data, NULL);
++EXPECT_FALSE(data.getsockname_reconfigure);
++
++ // The first connection's descriptor: no longer known to the channel. ++ares_process_fd(chan, 100, ARES_SOCKET_BAD);
++++ // Moved to the new server. ++ EXPECT_FALSE(data.done); ++ EXPECT_EQ((size_t)1, ares_queue_active_queries(chan)); ++ ++ ares_cancel(chan); ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ(ARES_ECANCELLED, data.status); ++ EXPECT_EQ((size_t)0, data.bad_fd_calls); ++ EXPECT_EQ((size_t)0, data.sock_state_bad); ++ ares_destroy(chan);++}
++
++// With TCP Fast Open the first getsockname may fail, and is retried after the ++// first write -- from insideares_conn_write(), after the send function has ++// returned. Reconfiguring from that retry closes the connection the query was ++// just written on.
++TEST_F(LibraryTest, ReconfigureInFastOpenGetsocknameNoUseAfterFree) { ++ares_channel_t*chan = nullptr;
++ FakeSockData data;
++Quoted 7 lines
++ memset(&data, 0, sizeof(data)); ++ FakeSockInit(&chan, &data, ARES_FLAG_USEVC, 0); ++ data.tfo = true; ++ data.getsockname_fail_first = true; ++ data.getsockname_reconfigure = true;++
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 13 lines
++ FakeSockQueryCallback, &data, NULL);
++EXPECT_FALSE(data.getsockname_reconfigure);
++++ // Moved to the new server. ++ EXPECT_FALSE(data.done); ++ EXPECT_EQ((size_t)1, ares_queue_active_queries(chan)); ++ EXPECT_EQ((size_t)0, data.sock_state_bad); ++ ++ ares_cancel(chan); ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ(ARES_ECANCELLED, data.status); ++ EXPECT_EQ((size_t)0, data.bad_fd_calls); ++ ares_destroy(chan);Quoted 12 lines
++}
++
++// A non-blocking receive function that reconfigures the servers and returns ++// an answer. The connection it read from is closed, so the answer must be ++// dropped -- the query has already moved to the new server -- and the ++// receive function must not be called again on the closed connection. ++TEST_F(LibraryTest, ReconfigureInRecvDropsStaleAnswer) {
++ares_channel_t*chan = nullptr;
++ FakeSockData data;
++
++memset(&data, 0, sizeof(data));
++ FakeSockInit(&chan, &data, 0,
ARES_SOCKFUNC_FLAG_NONBLOCKING);Quoted 2 lines
++
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 17 lines
++ FakeSockQueryCallback, &data, NULL);
++ASSERT_NE((size_t)0, data.query_len);
++++ data.recv_reconfigure = true; ++ ares_process_fd(chan, 100, ARES_SOCKET_BAD); ++ EXPECT_FALSE(data.recv_reconfigure); ++ ++ EXPECT_EQ((size_t)1, data.recv_calls); ++ EXPECT_EQ((size_t)0, data.bad_fd_calls); ++ // Still in flight on the new server. ++ EXPECT_FALSE(data.done); ++ EXPECT_EQ((size_t)1, ares_queue_active_queries(chan)); ++ ++ ares_cancel(chan); ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ(ARES_ECANCELLED, data.status); ++ ares_destroy(chan);Quoted 12 lines
++}
++
++// As above, but the answer comes from an earlier receive call, and a later ++// call -- which has nothing more to read -- reconfigures the servers. The ++// answer is already buffered on the connection that call tore down, and must ++// be dropped with it.
++TEST_F(LibraryTest, ReconfigureInRecvDropsBufferedAnswer) {
++ares_channel_t*chan = nullptr;
++ FakeSockData data;
++
++memset(&data, 0, sizeof(data));
++ FakeSockInit(&chan, &data, 0,
ARES_SOCKFUNC_FLAG_NONBLOCKING);Quoted 2 lines
++
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 17 lines
++ FakeSockQueryCallback, &data, NULL);
++ASSERT_NE((size_t)0, data.query_len);
++++ data.recv_answer_then_reconfigure = true; ++ ares_process_fd(chan, 100, ARES_SOCKET_BAD); ++ EXPECT_FALSE(data.recv_answer_then_reconfigure); ++ ++ EXPECT_EQ((size_t)2, data.recv_calls); ++ EXPECT_EQ((size_t)0, data.bad_fd_calls); ++ // Still in flight on the new server. ++ EXPECT_FALSE(data.done); ++ EXPECT_EQ((size_t)1, ares_queue_active_queries(chan)); ++ ++ ares_cancel(chan); ++ EXPECT_TRUE(data.done); ++ EXPECT_EQ(ARES_ECANCELLED, data.status); ++ ares_destroy(chan);Quoted 14 lines
++}
++
++// The close function is application code too. With no socket-state callback ++// to carry the descriptor, it runs inline whileares_check_cleanup_conns()++// walks the server list and each server's connections, closing idle ones. ++// Reconfiguring from it frees the list nodes that walk holds. ++TEST_F(LibraryTest, ReconfigureInCloseNoUseAfterFree) {
++ares_channel_t*chan = nullptr;
++ FakeSockData data;
++
++memset(&data, 0, sizeof(data));
++FakeSockInit(&chan, &data, 0, 0, false);
++
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 16 lines
++ FakeSockQueryCallback, &data, NULL);
++ASSERT_NE((size_t)0, data.query_len);
++++ // Answer it; the now idle connection is then closed. ++ data.recv_answer = true; ++ data.close_reconfigure = true; ++ ares_process_fd(chan, 100, ARES_SOCKET_BAD); ++ EXPECT_FALSE(data.recv_answer); ++ EXPECT_FALSE(data.close_reconfigure); ++ ++ EXPECT_TRUE(data.done); ++ // The fake answer carries no records. ++ EXPECT_EQ(ARES_ENODATA, data.status); ++ EXPECT_EQ((size_t)0, ares_queue_active_queries(chan)); ++ EXPECT_EQ((size_t)0, data.bad_fd_calls); ++ ares_destroy(chan);Review
Quoted 6 lines
++}
++
+ // The second route to the same freed connection, and the one that drove the
+ // change from suppressing the idle sweep to deferring the notification:
+ // reconfiguring the servers from the socket-state callback destroys the server(quoted diff) +0
-0accept-an-answer-on-the-right-connection.patch b/meta-oe/recipes-support/c- ares/c-ares/0005-ares_process-only-accept-an-answer-on-the-right- connection.patch
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 4 lines
new file mode 100644
index 0000000000..20fef5d2f0meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +0
-0an-answer-on-the-right-connection.patch
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +1
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
2001
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 23 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +23
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
AnswerOnOtherServerConnectionDroppedmeta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +7
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares/commit/c8bd9ab53329f66ec0c395a11002a5696669b147]
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +7
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
+++++++++++++++++++++++++++++++++++++++++
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +9
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*conn, void *data,
size_tlen,meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 23 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +23
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
process_answer(ares_channel_t *channel,
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +6
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
or
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +4
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
would
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +3
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
buffer,
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +2
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
from
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 14 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +14
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
sockaddr *sa,
Quoted 22 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +16
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
sin_port;
++ } else {
++sa_port= (CARES_INADDR_CAST(const struct sockaddr_in6 *, sa))-sin6_port;
++ }
++Quoted 8 lines
++ if (
ntohs(sa_port)!= port) {
++ returnARES_FALSE;
++ }
++
++ returnARES_TRUE;
++}
++
+ares_conn_err_tares_socket_write(ares_channel_t *channel,ares_socket_tfd,
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 22 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +22
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
and
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 27 lines
++ * port.
++ *++ * \param[in] sa Socket address (AF_INET or AF_INET6) ++ * \param[in] aa Address to compare against ++ * \param[in] port Port to compare against, in host byte order ++ * \return ARES_TRUE if the address families, IP addresses and ports are++ * equal,
ARES_FALSEotherwise
++ */
++ares_bool_t ares_sockaddr_addr_port_eq(const struct sockaddr *sa,
++ const structares_addr*aa,
++ unsigned short port);
+ares_status_tares_socket_configure(ares_channel_t *channel, int family,meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +15
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
FakeSockRecvfrom(ares_socket_t sock, void *buffer,
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +10
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ReconfigureInRecvDropsBufferedAnswer) {
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 15 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +15
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ARES_SOCKFUNC_FLAG_NONBLOCKING);Quoted 2 lines
++
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,Quoted 20 lines
++ FakeSockQueryCallback, &data, NULL);
++ASSERT_NE((size_t)0, data.query_len);
++ASSERT_EQ(htons(53055), data.peer.sin_port);
++++ data.recv_port = htons(53); ++ data.recv_answer = true; ++ ares_process_fd(chan, 100, ARES_SOCKET_BAD); ++ EXPECT_FALSE(data.recv_answer); ++ EXPECT_FALSE(data.done); ++ EXPECT_EQ((size_t)1, ares_queue_active_queries(chan)); ++ ++ data.recv_port = 0; ++ data.recv_answer = true; ++ ares_process_fd(chan, 100, ARES_SOCKET_BAD); ++ EXPECT_FALSE(data.recv_answer); ++ EXPECT_TRUE(data.done); ++ // The fake answer carries no records. ++ EXPECT_EQ(ARES_ENODATA, data.status); ++ EXPECT_EQ((size_t)0, ares_queue_active_queries(chan)); ++ ares_destroy(chan);Quoted 6 lines
++}
++
+ // The close function is application code too. With no socket-state callback
+ // to carry the descriptor, it runs inline whileares_check_cleanup_conns()
+ // walks the server list and each server's connections, closing idle ones. +@@ -5530,5 +5570,183 @@INSTANTIATE_TEST_SUITE_P(TransportModes, ServerRecoveryMultiMockTest, ::testing:
Quoted 2 lines
+
+ INSTANTIATE_TEST_SUITE_P(TransportModes,ServerRecoveryNoProbeMultiMockTest, ::testing::ValuesIn(ares::test::families_modes), PrintFamilyMode);
Quoted 3 lines
+
++// An answer is only accepted on the connection its query was sent on ++// (RFC 5452 section 9.1). Two servers: #0 is demoted by a SERVFAIL so alater
Quoted 1 line
++// query goes only to #1, while #0's connection stays open (STAYOPEN).
Server
Quoted 11 lines
++// #0 then answers that query on its own connection. The answer must be ++// dropped without side effects, and the query must then complete normally. ++class ReplyConnMultiMockTest
++ : public MockChannelOptsTest,
++ public ::testing::WithParamInterface< std::pair<int, bool> > {
++ public:
++ReplyConnMultiMockTest()
++ : MockChannelOptsTest(2, GetParam().first, GetParam().second, false,++ FillOptions(&opts_), ++ ARES_OPT_FLAGS | ARES_OPT_TRIES | ++ ARES_OPT_SERVER_FAILOVER | ARES_OPT_NOROTATE){}
Quoted 12 lines
++
++ static struct ares_options*FillOptions(struct ares_options *opts){
++memset(opts, 0, sizeof(struct ares_options));
++ /* Keep the demoted server's connection open; no DNS0x20so the replies
++ * built here match the question exactly */++ opts->flags = ARES_FLAG_STAYOPEN | ARES_FLAG_EDNS; ++ /* One try per server: the query goes to #1, then on timeout to #0 */ ++ opts->tries = 1; ++ /* Never probe the demoted server, so the query is sent only to #1 */ ++ opts->server_failover_opts.retry_chance = 0; ++ opts->server_failover_opts.retry_delay = 10000; ++ return opts;Quoted 12 lines
++ }
++ private:
++ structares_optionsopts_;
++};
++
++staticstd::stringReplyConnFirstA(const QueryResult &result)++{
++ constares_dns_rr_t*rr;
++ char addr[64];
++
++ if (result.dnsrec_.dnsrec_ == nullptr ||
++ ares_dns_record_rr_cnt(result.dnsrec_.dnsrec_,
ARES_SECTION_ANSWER) == 0) {Quoted 6 lines
++ return "";
++ }
++ rr = ares_dns_record_rr_get_const(result.dnsrec_.dnsrec_,
++ARES_SECTION_ANSWER, 0);
++ if (ares_dns_rr_get_type(rr)!=ARES_REC_TYPE_A||
++ ares_inet_ntop(AF_INET, ares_dns_rr_get_addr(rr,
ARES_RR_A_ADDR), addr,Quoted 7 lines
++ sizeof(addr)) == nullptr) {
++ return "";
++ }
++ return addr;
++}
++
++TEST_P(ReplyConnMultiMockTest,AnswerOnOtherServerConnectionDropped) {
Quoted 25 lines
++ DNSPacket
warm_servfail;
++ warm_servfail.set_response().set_aa().set_rcode(SERVFAIL)
++ .add_question(newDNSQuestion("warmup.example", T_A));
++ DNSPacketwarm_ok;
++ warm_ok.set_response().set_aa()
++ .add_question(newDNSQuestion("warmup.example", T_A))
++ .add_answer(newDNSARR("warmup.example", 100, {1, 1, 1, 1}));
++ DNSPacket forged;
++ forged.set_response().set_aa()
++ .add_question(newDNSQuestion("victim.example", T_A))
++ .add_answer(newDNSARR("victim.example", 100, {6, 6, 6, 6}));
++ DNSPacket genuine;
++ genuine.set_response().set_aa()
++ .add_question(newDNSQuestion("victim.example", T_A))
++ .add_answer(newDNSARR("victim.example", 100, {1, 2, 3, 4}));
++ unsigned short qid = 0;
++
++ /* Demote #0, leaving a connection to it open */ ++EXPECT_CALL(*servers_[0], OnRequest("warmup.example", T_A))
++ .WillOnce(SetReply(servers_[0].get(),&warm_servfail));
++EXPECT_CALL(*servers_[1], OnRequest("warmup.example", T_A))
++ .WillOnce(SetReply(servers_[1].get(),&warm_ok));
++ QueryResult warm;
++ ares_query_dnsrec(channel_, "warmup.example",ARES_CLASS_IN,
++ARES_REC_TYPE_A, QueryCallback, &warm, NULL);Quoted 5 lines
++ Process(); ++ ASSERT_TRUE(warm.done_); ++ ASSERT_EQ(ARES_SUCCESS, warm.status_);++
++ /* The query goes only to #1, which does not answer. Meanwhile #0answers
Quoted 2 lines
++ * it on #0's own connection. That answer must be dropped, so the query
++ * times out on #1 and is retried on #0, whose genuine answer is acceptedon
Quoted 16 lines
++ * the same (still in sync, for TCP) connection. */
++EXPECT_CALL(*servers_[1], OnRequest("victim.example", T_A))
++ .WillOnce(InvokeWithoutArgs([&]() {
++ servers_[0]->SendUnsolicited(&forged, qid);
++ servers_[1]->SetReplyData(std::vector<byte>());
++ }));
++EXPECT_CALL(*servers_[0], OnRequest("victim.example", T_A))
++ .WillOnce(SetReply(servers_[0].get(), &genuine));
++ QueryResult result;
++ ares_query_dnsrec(channel_, "victim.example",ARES_CLASS_IN,
++ARES_REC_TYPE_A, QueryCallback, &result, &qid);++ Process(); ++ ASSERT_TRUE(result.done_); ++ EXPECT_EQ(ARES_SUCCESS, result.status_); ++ EXPECT_EQ("1.2.3.4", ReplyConnFirstA(result)); ++ EXPECT_EQ(1, (int)result.timeouts_);Quoted 6 lines
++}
++
++// DNS cookies (RFC 7873) are per-server state, validated against the server ++// the query was sent to. #1 supports cookies, #0 does not. A cookie-less ++// answer arriving on #0's connection for a query sent only to #1 must not be ++// accepted: it would otherwise be checked against #0's cookie state andpass.
Quoted 9 lines
++class ReplyConnCookieMultiMockTest
++ : public MockChannelOptsTest,
++ public ::testing::WithParamInterface<int> {
++ public:
++ReplyConnCookieMultiMockTest()
++ : MockChannelOptsTest(2,GetParam(), false, false,++ ReplyConnMultiMockTest::FillOptions(&opts_), ++ ARES_OPT_FLAGS | ARES_OPT_TRIES | ++ ARES_OPT_SERVER_FAILOVER | ARES_OPT_NOROTATE){}
Quoted 5 lines
++ private:
++ structares_optionsopts_;
++};
++
++TEST_P(ReplyConnCookieMultiMockTest,CookielessAnswerOnOtherServerDropped) {
Quoted 48 lines
++ std::vector<byte> server_cookie = { 1, 2, 3, 4, 5, 6, 7, 8 }; ++ DNSPacket warm_servfail; ++ warm_servfail.set_response().set_aa().set_rcode(SERVFAIL) ++ .add_question(new DNSQuestion("warmup.example", T_A)); ++ DNSPacket warm_ok; ++ warm_ok.set_response().set_aa() ++ .add_question(new DNSQuestion("warmup.example", T_A)) ++ .add_answer(new DNSARR("warmup.example", 100, {1, 1, 1, 1})) ++ .add_additional(new DNSOptRR(0, 0, 0, 1280, { }, server_cookie, false));++ DNSPacket forged;
++ forged.set_response().set_aa()
++ .add_question(newDNSQuestion("victim.example", T_A))
++ .add_answer(newDNSARR("victim.example", 100, {6, 6, 6, 6}));
++ DNSPacket genuine;
++ genuine.set_response().set_aa()
++ .add_question(newDNSQuestion("victim.example", T_A))
++ .add_answer(newDNSARR("victim.example", 100, {1, 2, 3, 4}));
++ unsigned short qid = 0;
++
++ /* #0 answers without a cookie (and is demoted); #1 answers with one, so
++ * #1's cookie state becomes SUPPORTED */
++EXPECT_CALL(*servers_[0], OnRequest("warmup.example", T_A))
++ .WillOnce(SetReply(servers_[0].get(),&warm_servfail));
++EXPECT_CALL(*servers_[1], OnRequest("warmup.example", T_A))
++ .WillOnce(SetReply(servers_[1].get(),&warm_ok));
++ QueryResult warm;
++ ares_query_dnsrec(channel_, "warmup.example",ARES_CLASS_IN,
++ARES_REC_TYPE_A, QueryCallback, &warm, NULL);++ Process(); ++ ASSERT_TRUE(warm.done_); ++ ASSERT_EQ(ARES_SUCCESS, warm.status_); ++ size_t cookie_len = 0; ++ fetch_server_cookie(warm.dnsrec_.dnsrec_, &cookie_len); ++ ASSERT_EQ(server_cookie.size(), cookie_len);++
++ /* The cookie-protected query goes only to #1; a cookie-less answer for it
++ * arrives on #0's connection and must be dropped. The retry to #0 is then
++ * legitimately answered by #0, which does not use cookies. */
++EXPECT_CALL(*servers_[1], OnRequest("victim.example", T_A))
++ .WillOnce(InvokeWithoutArgs([&]() {
++ servers_[0]->SendUnsolicited(&forged, qid);
++ servers_[1]->SetReplyData(std::vector<byte>());
++ }));
++EXPECT_CALL(*servers_[0], OnRequest("victim.example", T_A))
++ .WillOnce(SetReply(servers_[0].get(), &genuine));
++ QueryResult result;
++ ares_query_dnsrec(channel_, "victim.example",ARES_CLASS_IN,
++ARES_REC_TYPE_A, QueryCallback, &result, &qid);Quoted 8 lines
++ Process(); ++ ASSERT_TRUE(result.done_); ++ EXPECT_EQ(ARES_SUCCESS, result.status_); ++ EXPECT_EQ("1.2.3.4", ReplyConnFirstA(result)); ++ EXPECT_EQ(1, (int)result.timeouts_);++}
++
++INSTANTIATE_TEST_SUITE_P(TransportModes, ReplyConnMultiMockTest,::testing::ValuesIn(ares::test::families_modes), PrintFamilyMode);
Quoted 2 lines
++
++INSTANTIATE_TEST_SUITE_P(AddressFamilies,ReplyConnCookieMultiMockTest, ::testing::ValuesIn(ares::test::families), PrintFamily);
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 8 lines
++
+ } // namespace test
+ } // namespace aresmeta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +5
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
port)
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 44 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +44
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
(SEND_TYPE_ARG3)reply.size(), flags,
Quoted 7 lines
++ (struct sockaddr *)addr, addrlen);
++ if (rc < static_cast<ares_ssize_t>(reply.size())) {
++std::cerr<< "Failed to send full reply, rc=" << rc <<std::endl;
++ }
++}
++
++void MockServer::SendUnsolicited(const DNSPacket *reply, unsigned shortqid) {
Quoted 7 lines
++
ASSERT_NE(ARES_SOCKET_BAD, last_fd_);
++ASSERT_TRUE(last_fd_ == udpfd_ || connfds_.count(last_fd_) != 0)
++ << "the most recent requester's connection has closed";
++SendData(last_fd_, &last_addr_, last_addrlen_, qid, reply->data()); ++}
++
+ void MockServer::ProcessRequest(ares_socket_t fd, struct sockaddr_storage*addr,
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +3
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
fd, struct sockaddr_storage* addr,
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +2
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*/
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 14 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +14
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
fd, struct sockaddr_storage* addr,
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept-
Quoted 36 lines
meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +36
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
(SEND_TYPE_ARG3)reply.size(), flags,
Review
Quoted 46 lines
+- (struct sockaddr *)addr, addrlen); +- if (rc < static_cast<ares_ssize_t>(reply.size())) { +- std::cerr << "Failed to send full reply, rc=" << rc << std::endl; +- } ++ SendData(fd, addr, addrlen, qid, exact_reply_);meta-oe/recipes-support/c-ares/c-ares/0005-ares_process-only-accept- new file +40
-0Lines of this file are not shown. View the raw message.
(quoted diff) +0
-0The rest of this diff is not shown. View the raw message.
channel-config-under-lock.patch b/meta-oe/recipes-support/c-ares/c-ares/0006- ares_save_options-read-channel-config-under-lock.patch
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 4 lines
new file mode 100644
index 0000000000..76c072dd7emeta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +0
-0channel-config-under-lock.patch
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +1
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
00:00:00 2001
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 25 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +25
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares/commit/25a6911370878d4b617b44b142ea0e49d1ccede6]
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +5
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
++++++++++++++++++++++++++++++++++++++++++
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +7
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares_channel_t*src)meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 14 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +14
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares_channel_t*channel,meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +11
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares_channel_t*channel,meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +13
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares_channel_t*channel,meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +9
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*channel,
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +6
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*channel,
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +10
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*channel,
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +10
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*channel,
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 63 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +63
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
under
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +13
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
small_csv.c_str()));meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read-
Quoted 26 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +26
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
&optmask));
Review
Quoted 31 lines
meta-oe/recipes-support/c-ares/c-ares/0006-ares_save_options-read- new file +30
-0Lines of this file are not shown. View the raw message.
(quoted diff) +0
-0The rest of this diff is not shown. View the raw message.
prefix-length.patch b/meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton- reject-a-prefix-length.patch
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 4 lines
new file mode 100644
index 0000000000..2cd1ea8c24meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +0
-0prefix-length.patch
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +1
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
2001
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 35 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +35
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares/commit/7e65b381c0b915aa5671ddc713080c87e15fc42f]
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 14 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +14
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
text to binary form
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 12 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +12
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
the function stores
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +11
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
given
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 54 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +54
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
0, 0, 0, 0, 0, 0, 0, 0,
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 20 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +20
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
0, 0, 0, 0, 0, 0, 0,
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 9 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +9
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
unsigned char *dst,
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +10
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
char *dst)
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 5 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +5
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
{
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +4
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
unsigned char *dst,
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 36 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +36
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*dst,
size_tsize)meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 64 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +64
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
a6,
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 61 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +61
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
IncludesV6Address("2121:0000:0000:0000:0000:0000:0000:0303"));meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a-
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +11
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
&result);
Review
Quoted 18 lines
meta-oe/recipes-support/c-ares/c-ares/0007-ares_inet_pton-reject-a- new file +17
-0Lines of this file are not shown. View the raw message.
(quoted diff) +0
-0The rest of this diff is not shown. View the raw message.
drop-UDP-answers-from-server-port.patch b/meta-oe/recipes-support/c-ares/c-
ares/0008-ares_conn_read-dont-drop-UDP-answers-from-server-port.patchmeta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 4 lines
new file mode 100644
index 0000000000..f4f182b995meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +0
-0UDP-answers-from-server-port.patch
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +1
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
2001
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +4
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
whose
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +11
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares/commit/c7fa9ebcb0c0086730c53bf03191c03f03706246]
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +10
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
b/docs/ares_set_socket_functions.3
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 19 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +19
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
source
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +4
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
size_t\fIlength\fP, int \fIflags\fP, const struct sockaddr * \fIaddress\fP,ares_socklen_t\fIaddress_len\fP, void * \fIuser_data\fP)meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +2
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
available. If the \fIaddr\fP
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +1
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
address,
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 10 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +10
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
\fIdata\fP, int \fIlen\fP, void * \fIuser_data\fP)
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 6 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +6
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
*conn, void *data,
size_tlen,meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 20 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +20
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
struct sockaddr *sa,
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 16 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +1
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
sin6_port;meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +14
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
struct sockaddr *sa,
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +3
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
and
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 10 lines
+- * port. ++ * port. A port of 0 in the socket address means the port is unknown, so it ++ * matches any port.meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +7
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
and
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 18 lines
++ * the ports are equal or the socket address port is 0,
ARES_FALSE
++ * otherwisemeta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +16
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
FakeSockRecvfrom(ares_socket_t sock, void *buffer,
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 11 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +11
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
UDPAnswerFromWrongPortDropped) {
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop-
Quoted 13 lines
meta-oe/recipes-support/c-ares/c-ares/0008-ares_conn_read-dont-drop- new file +13
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ARES_SOCKFUNC_FLAG_NONBLOCKING);Quoted 2 lines
++
++ ares_query_dnsrec(chan, "www.google.com",ARES_CLASS_IN,
ARES_REC_TYPE_A,meta-oe/recipes-
Quoted 19 lines
++ FakeSockQueryCallback, &data, NULL);
++ASSERT_NE((size_t)0, data.query_len);
++ASSERT_EQ(htons(53055), data.peer.sin_port);
++++ data.recv_no_port = true; ++ data.recv_answer = true; ++ ares_process_fd(chan, 100, ARES_SOCKET_BAD); ++ EXPECT_FALSE(data.recv_answer); ++ EXPECT_TRUE(data.done); ++ // The fake answer carries no records. ++ EXPECT_EQ(ARES_ENODATA, data.status); ++ EXPECT_EQ((size_t)0, ares_queue_active_queries(chan)); ++ ares_destroy(chan);++}
++
+ // The close function is application code too. With no socket-state callback
+ // to carry the descriptor, it runs inline whileares_check_cleanup_conns()
+ // walks the server list and each server's connections, closing idle ones.meta-oe/recipes- renamed from meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb +0
-0support/c-ares/c-ares_1.34.8.bb
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb
Quoted 5 lines
index
058f3666e7..1d0f3eb343100644meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb +0
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
"file://LICENSE.md;md5=d3e72a10e08191f2ca1be3f3228d78f3"
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb +0
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
ares/releases/download/v${PV}/${BPN}-${PV}.tar.gz \
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb
Quoted 1 line
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb +1
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
callbacks.patch \
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb
Quoted 4 lines
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb +4
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
connection.patch \
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb
Quoted 3 lines
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb +3
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
port.patch \
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb
Quoted 2 lines
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb +0
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
"
c222b6d681096f9444d2c4863d2c1174019e27cacca0a4a5c114d36dd7d7bf78"Quoted 7 lines
meta-oe/recipes-support/c-ares/c-ares_1.34.8.bb +0
-1Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
2.43.0
Part of the diff is not shown here. View the raw message.