]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: resolvers: reject DNS messages without the QR bit
authorMartin Mirchev <mirchevmartin2203@gmail.com>
Wed, 18 Nov 2026 03:33:04 +0000 (11:33 +0800)
committerWilly Tarreau <w@1wt.eu>
Fri, 20 Nov 2026 11:36:58 +0000 (12:36 +0100)
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
reg-tests/resolvers/response_flag.py [new file with mode: 0644]
reg-tests/resolvers/response_flag.vtc [new file with mode: 0644]
src/resolvers.c

index 6b32c7bc8810d5496a82fedf4e220afa50b3df49..71c12d4049b56a06541b94d53bc87f8bac5c8859 100644 (file)
@@ -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 (file)
index 0000000..c0246ab
--- /dev/null
@@ -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 (file)
index 0000000..8ef1e6c
--- /dev/null
@@ -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 udp@127.0.0.1:${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]*"
+}
index 95259f068952be24fa42751c4615c74bdf15cf25..24f59a5144b1c230ce0077ebe836cf9e62e1b63e 100644 (file)
@@ -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;