mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] res_query/res_send contract findings
@ 2022-09-03  5:42 Rich Felker
  0 siblings, 0 replies; only message in thread
From: Rich Felker @ 2022-09-03  5:42 UTC (permalink / raw)
  To: musl

In preparation for figuring out what's involved in TCP fallback for
DNS queries, I started looking into what res_query/res_send is
supposed to do when the result does not fit in the caller's buffer.
[Non-]surprisingly, the Linux man pages do not document what's
supposed to happen, but probing how glibc behaves, they always return
the full length that would be required to store the result. This was
kind of surprising to me, but the Solaris man pages are more detailed
and document this as expected behavior. (In other words, they're kinda
like strlcpy/strlcat, computing something expensive that's partly
being thrown away for the sake of knowing how much space is needed to
retry, modulo TOCTOU.)

So, even if we were happy with just having UDP answers, truncated (TC
bit set), with length at most 512 bytes as the canonical answer for
"full length if it had fit", the current implementation does not match
others (and does not match the poorly documented contract) if the
caller provides a buffer shorter than 512 bytes that's too short. In
that case, the caller sees an answer falsely claiming that the data it
read, truncated on the byte level rather than at RR granularity, is
the full length of the response.

If we were not making any other action to improve this towards TCP
fallback, the right fix might just be having res_send call __res_msend
into a temp buffer when anslen<512 and then copy the part that fits to
the caller's buffer.

But for doing TCP fallback, I think this implies res_query/res_send
need to always perform TCP fallback on seeing TC flag, even if the
caller's buffer is <= 512 bytes, just so they can return the right
value. This is somewhat different from the requirements of
getaddrinfo, which probably does not want TCP fallback to happen
unless it got zero RRs in the ANSWER section along with the TC flag.
So the backend needs a way to be informed whether the caller has any
use for "full results". This can probably be done through the
resolvconf structure passed to __res_msend, which would also admit
having options to customize this if needed.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-09-03  5:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-03  5:42 [musl] res_query/res_send contract findings Rich Felker

Code repositories for project(s) associated with this public inbox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).