[Dnsmasq-discuss] answer_request() corrupts QNAME on TCP query
shenyongqing
floatinsky at 126.com
Mon Nov 16 02:10:02 UTC 2026
Hi DNSMASQ Team,
I think I have found a small bug in answer_request() that only shows up on TCP
queries in the lastest version, and have a patch for it.
The problem
-----------
answer_request() is handed "name", which is daemon->namebuff, and uses it as the
scratch buffer for the cached-CNAME-chain walk: for every hop it does
strcpy(name, cname_target) (src/rfc1035.c, "if (qtype == T_CNAME) break;"
... strcpy(name, cname_target);). When the walk ends without completing an
answer, the function returns 0 with name still holding the *last CNAME target*
rather than the client's QNAME.
For UDP this is harmless: the caller goes through forward_query(), which calls
extract_request() and rewrites daemon->namebuff from the packet before it picks a
server, so the damage is repaired by accident.
The TCP path has no such repair. tcp_process() extracts the QNAME into
daemon->namebuff (src/forward.c:2170), calls answer_request() (src/forward.c:2251)
and, when that returns 0, immediately uses daemon->namebuff again to choose the
upstream:
if (lookup_domain(daemon->namebuff, gotname, &first, &last))
flags = is_local_answer(now, first, daemon->namebuff);
so the server is selected for the CNAME target's domain, not the query's domain.
In a split-DNS setup that is visible: with
server=/example.com/10.0.0.53
server=/other.net/10.0.0.54
a TCP A query for www.example.com whose CNAME to target.other.net is cached but
whose A record is not gets forwarded to 10.0.0.54 (or the default server),
although the name the client asked about is delegated to 10.0.0.53. Depending on
the configuration that means SERVFAIL, or the query leaking to a server that
should never see it. The same applies to the DNSSEC give-up path
("give up if any cached CNAME in chain can't be used for DNSSEC reasons"), and to
multi-question TCP queries, where namebuff ends up holding the last question's
target.
The fix
-------
Keep a copy of the QNAME the client asked for and put it back into name before the
two "return 0" exits that hand the question over to the forwarders, so that
daemon->namebuff always holds the query name by the time answer_request() returns
0. I save the copy on the first question that actually needs the scratch buffer
and restore it on both bail-out paths; a re-parsing alternative (calling
extract_request() again at the exits) works too but costs a name-decode of the
packet a second time, whereas this is a single strcpy per bail-out.
The cost is a MAXDNAME buffer on answer_request()'s stack. I thought about
walking the chain in a separate local buffer instead, but "name" is also the
buffer add_resource_record() writes answer names into via nameoffset, so the walk
has to keep mutating it; restoring on exit seemed the smaller change.
Reproduce
---------
server=/example.com/127.0.0.1#5353 # answers www.example.com CNAME target.other.net
server=/other.net/127.0.0.1#5354 # answers target.other.net, nothing else
# dig +tcp www.example.com A -> forwarded to 5354, wrong (5353 expected)
# dig www.example.com A -> forwarded to 5353, correct
Regards,
Yongqing Shen
More information about the Dnsmasq-discuss
mailing list