From: Junio C Hamano <gitster@pobox.com>
To: "Sebastien Tardif via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, Patrick Steinhardt <ps@pks.im>,
Sebastien Tardif <sebtardif@ncf.ca>
Subject: Re: [PATCH v2 0/3] daemon: fix network address handling bugs
Date: Thu, 28 May 2026 06:00:07 +0900 [thread overview]
Message-ID: <xmqq1pewlga0.fsf@gitster.g> (raw)
In-Reply-To: <pull.2300.v2.git.git.1779905911.gitgitgadget@gmail.com> (Sebastien Tardif via GitGitGadget's message of "Wed, 27 May 2026 18:18:28 +0000")
"Sebastien Tardif via GitGitGadget" <gitgitgadget@gmail.com> writes:
> Fix three related issues in daemon.c's network address handling:
>
> IPv6 address corruption in lookup_hostname(): getaddrinfo() is called with
> AF_UNSPEC hints, so it may return IPv6 results. However, the code
> unconditionally casts ai_addr to sockaddr_in and passes AF_INET to
> inet_ntop(). On IPv6-only hosts, this reads from the wrong struct offset,
> producing garbage IP addresses. Fixed by checking ai_family and handling
> both AF_INET and AF_INET6.
>
> IPv6 address truncation in ip2str(): The sockaddr struct size (ai_addrlen)
> is passed as the output buffer size to inet_ntop(). For IPv6,
> sizeof(sockaddr_in6) is 28 bytes but INET6_ADDRSTRLEN is 46, so long IPv6
> addresses are silently truncated. Fixed by passing sizeof(ip) instead, and
> dropping the now-unused len parameter.
>
> NULL pointer in execute() logging: REMOTE_PORT environment variable is used
> in a format string without a NULL check (only REMOTE_ADDR was checked). If
> REMOTE_PORT is unset, NULL is passed to printf's %s, which is undefined
> behavior. Fixed by using a fallback string.
>
> Changes since v1:
>
> * Split the single patch into three separate commits, one per fix, per
> Patrick's review.
This, and all the other items in this list, are differences between
the version before v1 and v2, isn't it? It is OK to pretend that
the pre-v1 version v0 didn't officially exist, but it would be
helpful to see the inter-version improvements for *this* version.
Indeed, range-diff tells us that the commit log improvement is the
only change since the previous iteration.
> Range-diff vs v1:
>
> 1: b2d8143811 = 1: b2d8143811 daemon: fix IPv6 address corruption in lookup_hostname()
> 2: 5c01ec3cad = 2: 5c01ec3cad daemon: fix IPv6 address truncation in ip2str()
> 3: 1b2f9d1a07 ! 3: e312735716 daemon: guard NULL REMOTE_PORT in execute() logging
> @@ Metadata
> ## Commit message ##
> daemon: guard NULL REMOTE_PORT in execute() logging
>
> - The REMOTE_PORT environment variable is used in a format string
> - without a NULL check, while REMOTE_ADDR is checked. If REMOTE_PORT
> - is unset, NULL is passed to printf's %s, which is undefined behavior.
> + REMOTE_ADDR and REMOTE_PORT are both set by the same code path in
> + handle(), so neither should be NULL independently. However, the
> + existing code checks REMOTE_ADDR before the loginfo() call but not
> + REMOTE_PORT. If REMOTE_PORT were unset, NULL would be passed to
> + printf's %s, which is undefined behavior.
This is easier to read than the previous, but it is unclear what the
change is trying to achieve. You first say if addr is set port can
never be unset. So by checking addr before calling loginfo(), the
code effectively is ensuring that addr and port are set.
(1) The word "However" in "However the existing code checks" does
not make much sense to me (I would think "Therefore" is less
confusing, but if what you first said is correct, then it is
quite obvious and can be left unsaid).
(2) It is unclear why "If REMOTE_PORT were unset NULL would be ..."
needs to be brought up. Yes, you are not supposed to pass NULL
to printf that expects "%s" to format it. But isn't the whole
point of checking that addr is not NULL because the caller
knows that loginfo() accesses both, and the caller also knows
that if addr is not NULL, port will never be NULL? Or is this
comment about something other than loginfo() where port is used
without checking neither addr or port? Then it would not make
much sense to bring up "addr is checked before calling
loginfo()".
IOW, the sentence structure got vastly improved than the previous
round, but it made it clearer that what these sentences say is
unclear ;-).
> - Add a fallback string for the NULL case.
> + Add a fallback string for the NULL case, matching the existing
> + REMOTE_ADDR guard for consistency.
I tried to find if there is any existing case (addr ? addr : "") to
match, but I didn't find any. Probably that is because it is not
needed (instead the code does "if (addr) ..." to protect itself).
I think the only valid justification you could give to this change
is to say that even though the current code is perfectly fine as-is
(i.e. as you said, addr and port are both exported at the same time
so it will never happen that addr is non NULL and port is NULL),
somebody who is not so careful can break that arrangement in the
future, and it is a prudent thing to double check that port is not
NULL before using will future-proof this part of the code.
Thanks.
next prev parent reply other threads:[~2026-05-27 21:00 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-14 15:46 [PATCH 0/3] daemon: fix network address handling bugs Sebastien Tardif via GitGitGadget
2026-05-14 15:46 ` [PATCH 1/3] daemon: fix IPv6 address corruption in lookup_hostname() Sebastien Tardif via GitGitGadget
2026-05-14 21:26 ` Junio C Hamano
2026-05-14 15:46 ` [PATCH 2/3] daemon: fix IPv6 address truncation in ip2str() Sebastien Tardif via GitGitGadget
2026-05-14 15:46 ` [PATCH 3/3] daemon: guard NULL REMOTE_PORT in execute() logging Sebastien Tardif via GitGitGadget
2026-05-14 19:20 ` [PATCH 0/3] daemon: fix network address handling bugs Junio C Hamano
2026-05-15 7:31 ` Patrick Steinhardt
2026-05-27 18:18 ` [PATCH v2 " Sebastien Tardif via GitGitGadget
2026-05-27 18:18 ` [PATCH v2 1/3] daemon: fix IPv6 address corruption in lookup_hostname() Sebastien Tardif via GitGitGadget
2026-05-27 18:18 ` [PATCH v2 2/3] daemon: fix IPv6 address truncation in ip2str() Sebastien Tardif via GitGitGadget
2026-05-27 18:18 ` [PATCH v2 3/3] daemon: guard NULL REMOTE_PORT in execute() logging Sebastien Tardif via GitGitGadget
2026-05-27 21:00 ` Junio C Hamano [this message]
2026-05-28 2:56 ` [PATCH v3 0/3] daemon: fix network address handling bugs Sebastien Tardif via GitGitGadget
2026-05-28 2:56 ` [PATCH v3 1/3] daemon: fix IPv6 address corruption in lookup_hostname() Sebastien Tardif via GitGitGadget
2026-05-28 2:56 ` [PATCH v3 2/3] daemon: fix IPv6 address truncation in ip2str() Sebastien Tardif via GitGitGadget
2026-05-28 2:56 ` [PATCH v3 3/3] daemon: guard NULL REMOTE_PORT in execute() logging Sebastien Tardif via GitGitGadget
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=xmqq1pewlga0.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=ps@pks.im \
--cc=sebtardif@ncf.ca \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox