Hi,

Here is a small hardening fix for the resolvers, against current master
(f16798587). The patch is attached (git format-patch).

resolv_validate_dns_response() checks the query ID, the RCODE, the question
and the records, but not the QR bit, so a DNS message with QR=0 (a query)
is processed as a response. One carrying the same answer as a valid
response is accepted, and the query itself, if it comes back from the
nameserver's address (an echo or a forwarding loop), is processed as a
response without any answer. The sender still has to match the nameserver
address (the socket is connected), the query ID and the question, so this
does not make spoofing easier; it is only about not treating a query as a
response.

The patch adds DNS_FLAG_RESPONSE and treats such messages as invalid. It
adds reg-tests/resolvers/response_flag.vtc with a small Python DNS fixture
(in the style of tcp_fallback.py) that sends the same A answer with and
without the QR bit. The test fails on master (resp.status (200) == "503")
and passes with the patch; tcp_fallback.vtc still passes. It may be
backported to the stable branches.

Found by an LLM-assisted tool, verified manually.

Regards,
Martin Mirchev
From 1f48773e094a99f614f3cef4141597408dd9266b Mon Sep 17 00:00:00 2001
From: Martin Mirchev <[email protected]>
Date: Wed, 18 Nov 2026 11:33:04 +0800
Subject: [PATCH] BUG/MINOR: resolvers: reject DNS messages without the QR bit

resolv_validate_dns_response() checks the query ID, the RCODE, the
question and the records, but not the QR bit, which tells a response
(1) from a query (0). A message with QR=0 is thus processed as a
response: one carrying the same answer as a valid response is accepted,
and the query itself, if it comes back from the nameserver's address
(e.g. through an echo or a forwarding loop), is processed as a response
without any answer.

The sender still has to match the nameserver address, the query ID and
the question, so this does not make spoofing easier, but such a message
is not a response and should be treated as invalid, as RFC 1035 defines
the QR bit for this purpose.

This patch adds DNS_FLAG_RESPONSE and makes such messages invalid. The
new reg-test reg-tests/resolvers/response_flag.vtc uses a small DNS
fixture that sends the same A answer with and without the QR bit, and
checks that only the former resolves and that the latter is counted as
invalid.

This may be backported to the stable branches.
---
 include/haproxy/resolvers-t.h         |  1 +
 reg-tests/resolvers/response_flag.py  | 47 ++++++++++++++++++++++++
 reg-tests/resolvers/response_flag.vtc | 51 +++++++++++++++++++++++++++
 src/resolvers.c                       |  4 +++
 4 files changed, 103 insertions(+)
 create mode 100644 reg-tests/resolvers/response_flag.py
 create mode 100644 reg-tests/resolvers/response_flag.vtc

diff --git a/include/haproxy/resolvers-t.h b/include/haproxy/resolvers-t.h
index 6b32c7bc8..71c12d404 100644
--- a/include/haproxy/resolvers-t.h
+++ b/include/haproxy/resolvers-t.h
@@ -83,6 +83,7 @@ extern struct pool_head *resolv_requester_pool;
 #define DNS_RCODE_REFUSED       5       /* query refused */
 
 /* dns flags masks */
+#define DNS_FLAG_RESPONSE       0x8000  /* mask for response (QR) flag */
 #define DNS_FLAG_TRUNCATED      0x0200  /* mask for truncated flag */
 #define DNS_FLAG_REPLYCODE      0x000F  /* mask for reply code */
 
diff --git a/reg-tests/resolvers/response_flag.py b/reg-tests/resolvers/response_flag.py
new file mode 100644
index 000000000..c0246ab73
--- /dev/null
+++ b/reg-tests/resolvers/response_flag.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+# DNS fixture answering with or without the QR (response) bit.
+
+import argparse
+import socket
+import struct
+
+TEST_ADDRESS = "192.0.2.123"
+
+
+def parse_question(query):
+    offset = 12
+    labels = []
+    while query[offset]:
+        length = query[offset]
+        offset += 1
+        labels.append(query[offset : offset + length].decode())
+        offset += length
+    offset += 1
+    return ".".join(labels), query[12 : offset + 4]
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("--port", type=int, required=True)
+    args = parser.parse_args()
+
+    udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+    udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+    udp.bind(("127.0.0.1", args.port))
+    print("READY", flush=True)
+
+    while True:
+        query, peer = udp.recvfrom(65535)
+        name, question = parse_question(query)
+        # same answer in both cases, only the QR bit differs
+        flags = 0x0180 if name == "no-qr.test" else 0x8180
+        header = query[:2] + struct.pack("!HHHHH", flags, 1, 1, 0, 0)
+        address = socket.inet_aton(TEST_ADDRESS)
+        answer = b"\xc0\x0c" + struct.pack("!HHIH", 1, 1, 30, len(address)) + address
+        udp.sendto(header + question + answer, peer)
+
+
+if __name__ == "__main__":
+    main()
diff --git a/reg-tests/resolvers/response_flag.vtc b/reg-tests/resolvers/response_flag.vtc
new file mode 100644
index 000000000..8ef1e6cb8
--- /dev/null
+++ b/reg-tests/resolvers/response_flag.vtc
@@ -0,0 +1,51 @@
+varnishtest "DNS messages without the QR bit are not responses"
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+#REGTEST_TYPE=devel
+
+feature ignore_unknown_macro
+feature cmd "python3 --version"
+
+# Reserve a random local port, then release its TCP listener so the Python
+# fixture can bind UDP to it.
+server s10 {} -start
+server s10 -break
+
+process p1 "python3 ${testdir}/response_flag.py --port ${s10_port}" -start
+process p1 -expect-text 0 0 "READY"
+
+haproxy h1 -conf {
+    defaults
+        mode http
+        timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+        timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
+        timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"
+
+    resolvers dns
+        nameserver dns [email protected]:${s10_port}
+        timeout resolve 30s
+        timeout retry 200ms
+        resolve_retries 1
+
+    frontend fe
+        bind "fd@${fe}"
+        http-request do-resolve(txn.ip,dns,ipv4) hdr(host)
+        http-request return status 200 hdr x-ip %[var(txn.ip)] if { var(txn.ip) -m found }
+        http-request return status 503
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+    txreq -hdr "Host: qr.test"
+    rxresp
+    expect resp.status == 200
+    expect resp.http.x-ip == "192.0.2.123"
+
+    txreq -hdr "Host: no-qr.test"
+    rxresp
+    expect resp.status == 503
+} -run
+
+haproxy h1 -cli {
+    send "show resolvers dns"
+    expect ~ "invalid: +[1-9][0-9]*"
+}
diff --git a/src/resolvers.c b/src/resolvers.c
index 95259f068..24f59a514 100644
--- a/src/resolvers.c
+++ b/src/resolvers.c
@@ -1100,6 +1100,10 @@ static int resolv_validate_dns_response(unsigned char *resp, unsigned char *bufe
 
 	flags = reader[0] * 256 + reader[1];
 
+	/* a message without the QR bit is a query, not a response */
+	if (!(flags & DNS_FLAG_RESPONSE))
+		goto invalid_resp;
+
 	if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
 		if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) {
 			cause = RSLV_RESP_NX_DOMAIN;
-- 
2.50.1

Reply via email to