mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: [musl] res_query/res_send contract findings
Date: Sat, 3 Sep 2022 01:42:56 -0400	[thread overview]
Message-ID: <20220903054255.GA8773@brightrain.aerifal.cx> (raw)

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.


                 reply	other threads:[~2022-09-03  5:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220903054255.GA8773@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).