mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Mike Cui <cuicui@gmail.com>
To: musl@lists.openwall.com
Subject: [musl] Potential bug in __res_msend_rc() wrt to union initialization.
Date: Mon, 18 Mar 2024 12:56:41 -0700	[thread overview]
Message-ID: <CAMbjb11yqF2fzp4bR3k4_=jp_bc7cNP15vJecEvNBGHgeXL5nw@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1918 bytes --]

I recently upgraded to clang-18, and after compiling musl with it, I
noticed that all my getaddrinfo() calls are failing. I tracked this to be
an issue in __res_msend_rc(), where the 'sin6' member of union 'sa' is
initialized to garbage, rather than 0. Then later bind() fails
with EADDRNOTAVAIL.

I reported this bug on clang discourse:
https://discourse.llvm.org/t/union-initialization-and-aliasing-clang-18-seems-to-miscompile-musl/77724,
and the discussion seems to suggest that there is potentially a bug in musl
as well. TL;DR:

- According to strict interpretation of the C standard, initializing a
union with '{0}', only initializes the first member of the union to 0 (in
this case, sin4), and "default" initializes the rest. This interpretation
is still up for debate. The proper way to initialize the entire union is '{
}' not '{ 0 }'.
- There is currently a bug in clang-18 that treats '{ }' to be the same as
'{ 0 }'. The proposed fix is to just zero out the entire union for both "{
0 }" and "{ }". However we cannot rely on "{ 0 }" to always zero out the
entire union in the future.

musl should be fixed to use "{ }" for initialization. And to work around
the current buggy release of clang-18, perhaps flip the order to make sin6
the first member of the struct? I've attached a patch that works for me.
There may be other instances of the same bug in the musl code base.

--- a/src/network/res_msend.c
+++ b/src/network/res_msend.c
@@ -83,9 +83,9 @@ int __res_msend_rc(int nqueries, const unsigned char
*const *queries,
        int fd;
        int timeout, attempts, retry_interval, servfail_retry;
        union {
-               struct sockaddr_in sin;
                struct sockaddr_in6 sin6;
-       } sa = {0}, ns[MAXNS] = {{0}};
+               struct sockaddr_in sin;
+       } sa = {}, ns[MAXNS] = {{}};
        socklen_t sl = sizeof sa.sin;
        int nns = 0;
        int family = AF_INET;

[-- Attachment #2: Type: text/html, Size: 2376 bytes --]

             reply	other threads:[~2024-03-18 20:36 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-18 19:56 Mike Cui [this message]
2024-03-18 21:34 ` Rich Felker
2024-03-18 22:22   ` NRK
2024-03-18 22:39     ` [musl] Potential bug in __res_msend_rc() wrt to union initialisation Thorsten Glaser
2024-03-19  0:01     ` [musl] Potential bug in __res_msend_rc() wrt to union initialization Mike Cui
2024-03-19 13:18       ` Rich Felker
2024-03-19 15:04         ` Mike Cui
2024-03-19 15:42           ` Rich Felker
2024-03-19 15:55             ` Mike Cui
2024-03-19 16:08               ` Rich Felker
2024-03-19 16:39                 ` Jₑₙₛ Gustedt
2024-03-19 20:47                   ` Thorsten Glaser
2024-03-21 10:58                     ` Jₑₙₛ Gustedt
2024-03-21 16:41                       ` Thorsten Glaser
2024-03-19 21:04                   ` NRK
2024-03-19 21:36                     ` Rich Felker
2024-03-20 17:11                       ` NRK

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='CAMbjb11yqF2fzp4bR3k4_=jp_bc7cNP15vJecEvNBGHgeXL5nw@mail.gmail.com' \
    --to=cuicui@gmail.com \
    --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).