Repository navigation
timesyncd: resolve NTP hostnames through resolved's varlink interface - #43380
Conversation
|
Re-reviewed PR 43380 (timesyncd resolving NTP hostnames via resolved's Varlink interface) at d52a693. Completed lenses: coding style and maintainability, and unit-file semantics / early-boot ordering. Incomplete lenses: correctness and memory safety, resource lifetimes and concurrency, security and robustness, architecture / API design / compatibility, tests and regression coverage, and Varlink/resolved IPC protocol conformance - those six reviewers again exceeded their time budget and were stopped without returning results, so those lenses are not covered in this round either. No new findings survived this round. The unit-file lens positively verified several things worth recording: the two new After= edges introduce no ordering cycle (systemd-resolved.service is already Before=sysinit.target with DefaultDependencies=no, and systemd-resolved-varlink.socket has DefaultDependencies=no and no After= of its own), systemd-resolved-varlink.socket does exist and is installed under ENABLE_RESOLVE, and timesyncd's sandbox does not block the new connection (RestrictAddressFamilies includes AF_UNIX, connect() to an AF_UNIX socket needs no writable mount under ProtectSystem=strict, and there is no PrivateNetwork/IPAddressDeny/PrivateUsers/InaccessiblePaths) - which the pre-existing nss-resolve arrangement already demonstrated empirically. Three candidates were raised and all three were rejected by independent validators. Rejected: the claim that dropping Environment=SYSTEMD_NSS_RESOLVE_VALIDATE=0 re-opens the chicken-and-egg cycle on the retained getaddrinfo path (the variable is indeed read only by nss-resolve's query_flags(), and DNSSECValidationFailed is indeed in neither error_shall_fallback() nor error_shall_try_again(), but the harm needs a narrow race in which timesyncd's connect to /run/systemd/resolve/io.systemd.Resolve fails transiently while a later nss-resolve connect to the same path succeeds; in the case the fallback actually targets, resolved absent or -Dresolve=false, nss-resolve's connect fails identically and the lookup goes to nss-dns with no DNSSEC involved, the musl scenario the PR exists for has no nss-resolve at all, and the new After= ordering shrinks the window further - besides, this is the exact inverse of finding 15, which this reviewer requested and the author acted on); the new lines exceeding the 109-character guideline (measurements confirmed at 114-135 chars, but the guideline is explicitly soft and the base revision of this same file already has 24 lines over 109, four of them longer than the worst new line at 141-158 chars); and the request for a dedicated log_debug() anchor on manager_resolve_varlink()'s success path instead of grepping io.systemd.Resolve.ResolveHostname (the fragility argument is inverted - assert_timesyncd_log() returns non-zero when the pattern is absent, so losing sd-json's outgoing-message dump would fail the test rather than silently pass it, and the grepped literal is the protocol method name inside the dumped JSON rather than the log wording, so it survives rewording of that message). A fourth candidate, that the testcase stops systemd-resolved-monitor.socket but the teardown only restarts the varlink socket and the service, was dropped as a duplicate of a claim a validator already refuted in the previous round: Sockets= in units/systemd-resolved.service.in:48 makes starting the service pull both of its sockets back up. Every finding from every round remains fixed or dismissed, all prior review threads were already resolved in the previous round, and no new inline comments are posted. Previous findings. Suggestions:
Nits: Must fix: Suggestions: Nits: Must fix: Suggestions: Nits: No new findings. |
| link, | ||
| "io.systemd.Resolve.ResolveHostname", | ||
| SD_JSON_BUILD_PAIR_STRING("name", m->current_server_name->string), | ||
| SD_JSON_BUILD_PAIR_CONDITION(!socket_ipv6_is_supported(), "family", SD_JSON_BUILD_INTEGER(AF_INET)), |
There was a problem hiding this comment.
Claude: suggestion: This drops two things the getaddrinfo() path below still gets. AI_ADDRCONFIG (line 998) suppresses a family for which no non-loopback address is configured, while socket_ipv6_is_supported() only checks that /proc/net/if_inet6 exists, i.e. that the kernel has IPv6 at all - true on essentially every distro kernel, including IPv4-only hosts. And glibc applies RFC 6724 destination sorting to getaddrinfo() results (including those from nss-resolve), whereas resolved's ResolveHostname reply comes back in answer order. Since manager_resolve_complete() unconditionally takes m->current_server_name->addresses (the first entry appended by server_address_new()), timesyncd can now pick an unusable AAAA first and only advance via addresses_next after the send fails. Consider socket_ipv6_is_enabled(), which also honours the disable_ipv6 sysctl, and/or filtering or ordering the returned addresses by locally configured families in the FOREACH_ARRAY loop.
There was a problem hiding this comment.
Consider socket_ipv6_is_enabled(),
Done.
Regarding the RFC 6724 support feedback, I don't see any way that timesyncd relies on this behavior and it seems like implementing RFC 6724 here would be a lot more effort. So I'm not sure if this is worth doing, but I'm open to being proven wrong.
| if (!isempty(error_id)) { | ||
| log_debug("Failed to resolve %s: %s", m->current_server_name->string, error_id); |
There was a problem hiding this comment.
Claude: suggestion: The fallback is narrower than the commit message ("If resolved isn't around we just keep using getaddrinfo()") suggests: getaddrinfo() is only reached when sd_varlink_connect_address() fails. Once the connection exists, every error reply here - including transport-level ones like SD_VARLINK_ERROR_DISCONNECTED (resolved restarted mid-query), SD_VARLINK_ERROR_TIMEOUT (the relative timeout set at line 868) and SD_VARLINK_ERROR_METHOD_NOT_FOUND - plus the parse failure at 820-825, skips the host entirely instead of falling back. nss-resolve already draws exactly this distinction in error_shall_fallback() (src/nss-resolve/nss-resolve.c:27): IPC-level failures mean "ask someone else", not "no such host". Consider mirroring that and retrying the same m->current_server_name via resolve_getaddrinfo() for those error ids and for the dispatch failure, while keeping genuine resolution errors as-is.
There was a problem hiding this comment.
The check isn't to fall back on transient/other errors with resolved, it's to fall back if resolved is not running or not installed. If we fell back to getaddrinfo on transient/other errors, we would fall back to a path on musl/systems without nss-resolve that is known to be problematic. That's the path this patch is trying to avoid on those systems. If there's a transient error, it goes to the next server.
and SD_VARLINK_ERROR_METHOD_NOT_FOUND
I'm not sure when this would actually be the case. Does systemd have a policy about supporting different component versions at the same time, e.g. timesyncd v262 and some old version of resolved ?
| assert(m); | ||
| assert(m->current_server_name); | ||
|
|
||
| r = sd_varlink_connect_address(&link, "/run/systemd/resolve/io.systemd.Resolve"); |
There was a problem hiding this comment.
Claude: nit: Another hardcoded copy of the /run/systemd/resolve/io.systemd.Resolve literal, alongside src/resolve/resolvectl.c (two places), src/nss-resolve/nss-resolve.c and the server side in src/resolve/resolved-varlink.c. This file already includes resolved-def.h (internal, src/shared/, not installed) for SD_RESOLVED_NO_VALIDATE and SD_RESOLVED_QUERY_TIMEOUT_USEC, so defining the address there, or in resolve-varlink-util.h, and using it from all call sites would be cleaner.
There was a problem hiding this comment.
you're not wrong, but iiuc to do this I'd need to modify other (and unrelated to this series) parts of the codebase that use this constant. This seems like scope creep, otoh it's not a huge change so if human maintainers think this should be required before merging then I'll do it.
|
@craftyguy Please consider comments by Claude. If you think a suggestion is not valid, then feel free to ignore but comment something there. |
006258f to
f530436
Compare
f530436 to
0467c8e
Compare
0467c8e to
e714c96
Compare
e714c96 to
992670e
Compare
|
@yuwata ok I think I satisfied the ai bot thing, but there are some things (the unresolved threads above) I wasn't totally sure about and would like a human to comment on |
With DNSSEC on, resolved can't validate anything until the clock is right, and without an RTC the only thing that fixes the clock is talking to the NTP servers we're trying to resolve. On glibc the timesyncd unit sets SYSTEMD_NSS_RESOLVE_VALIDATE=0 and nss-resolve turns that into a per-query flag, which breaks the chicken/egg. musl (and other systems not using nss-resolve) have no NSS, so nothing reads it there and we're stuck. So talk to resolved directly when it's there, and pass SD_RESOLVED_NO_VALIDATE until we've synced the clock ourselves. NTP-related lookups after that are validated normally, which is better than what we get from nss. If resolved isn't around we just keep using getaddrinfo(). The reply handler duplicates the tail of the getaddrinfo handler, since the two differ in how they report failures. Varlink discards what the callback returns, so it has to move on to the next host itself. This also drops SYSTEMD_NSS_RESOLVE_VALIDATE=0 from the unit because nss-resolve talks to resolved over the same socket we do, so the getaddrinfo() path is only ever taken when resolved is unreachable, and then nothing validates anyway.
Our first resolve happens way before sockets.target, and systemd-resolved-varlink.socket has no ordering of its own, so there's nothing making sure the socket is listening by then. Without this the connect fails and we quietly fall back to getaddrinfo(), which is exactly the case the varlink path is there for. Order after both the service and the socket since either one is enough on its own but neither covers every install situation. For example resolved binds the socket itself when nothing hands it one, and on systems where only the socket is enabled the service isn't started at boot at all.
ipv6_is_supported only tells us the kernel has IPv6, not that it's usable. ipv6_is_enabled also checks the sysctl that turns it off, so we stop resolving addresses we can't use.
This checks that we resolve through resolved when it's running, and fall back to getaddrinfo when it isn't.
992670e to
d52a693
Compare
Removed 0006-timesyncd-disable-NSCD-when-DNSSEC-validation-is-dis.patch because it was fixed upstream by systemd/systemd#43380
Removed 0006-timesyncd-disable-NSCD-when-DNSSEC-validation-is-dis.patch because it was fixed upstream by systemd/systemd#43380
- Removed 0001-Don-t-try-to-unmount-nix-or-nix-store.patch because we fixed it in NixOS with NixOS#560805 - Removed 0003-add-rootprefix-to-lookup-dir-paths.patch because we fixed it in NixOS with NixOS#559314 - Removed 0006-timesyncd-disable-NSCD-when-DNSSEC-validation-is-dis.patch because it was fixed upstream by systemd/systemd#43380
- Removed 0001-Don-t-try-to-unmount-nix-or-nix-store.patch because we fixed it in NixOS with NixOS#560805 - Removed 0003-add-rootprefix-to-lookup-dir-paths.patch because we fixed it in NixOS with NixOS#559314 - Removed 0006-timesyncd-disable-NSCD-when-DNSSEC-validation-is-dis.patch because it was fixed upstream by systemd/systemd#43380
With DNSSEC on, resolved can't validate anything until the clock is correct,
and without an RTC the only thing that fixes the clock is talking to the
NTP servers we're trying to resolve.
On glibc the timesyncd unit sets SYSTEMD_NSS_RESOLVE_VALIDATE=0 and
nss-resolve turns that into a per-query flag, which breaks the
chicken/egg. musl and systems not using nss-resolve have no NSS, so nothing
reads it there and we're stuck.
So talk to resolved directly when it's there, and pass
SD_RESOLVED_NO_VALIDATE until we've synced the clock ourselves. Lookups
after that are validated normally, which is better than what we get from
nss. If resolved isn't around we keep using getaddrinfo().
Tested on musl in qemu with the RTC set to 2020 and DNSSEC=yes.
timesyncd syncs when it couldn't before, while
resolvectl querystillfails with signature-expired, so the "relaxation" is scoped to only timesyncd.
Once the clock is synced subsequent timesyncd resolver queries no longer
include the flag to disable validation.
fixes #20235