Skip to content

timesyncd: resolve NTP hostnames through resolved's varlink interface - #43380

Merged
yuwata merged 4 commits into
systemd:mainfrom
craftyguy:timesyncd-dnssec-resolved
Aug 16, 2026
Merged

yuwata merged 4 commits into
systemd:mainfrom
craftyguy:timesyncd-dnssec-resolved

Conversation

@craftyguy

Copy link
Copy Markdown
Contributor

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 query still
fails 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

@github-actions github-actions Bot added timesync units please-review PR is ready for (re-)review by a maintainer labels Aug 14, 2026
@github-actions

github-actions Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

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:

  1. src/timesync/timesyncd-manager.c:839-843 - a negative return from the Varlink reply callback is logged and discarded by varlink_dispatch_reply(), so a failure in sockaddr_set_in_addr()/server_address_new(), or in manager_resolve_complete(), leaves timesyncd idle with no query in flight and no retry armed; the getaddrinfo handler's return value does propagate to the event loop, so the two paths behave differently. (fixed; see finding 11 on the shape of the fix)
  2. src/timesync/timesyncd-manager.c:837 - the per-address ifindex from the reply is dropped and sockaddr_set_in_addr() never sets sin6_scope_id, so link-local IPv6 answers yield an unusable sockaddr; nss-resolve propagates the scope id, making this a regression relative to the getaddrinfo path. (fixed)
  3. src/timesync/timesyncd-manager.c:885 - the Varlink request loses AI_ADDRCONFIG filtering (socket_ipv6_is_supported() only checks that the kernel has IPv6 at all) and RFC 6724 destination sorting, so an unusable AAAA address can be selected first on IPv4-only hosts. (family predicate fixed via socket_ipv6_is_enabled(); RFC 6724 sorting declined by the author as disproportionate effort)
  4. src/timesync/timesyncd-manager.c:812-813 - the getaddrinfo fallback is only reached when the connect fails; every error reply, including transport-level ones such as DISCONNECTED and TIMEOUT, plus the dispatch failure, skips the host entirely instead of falling back the way nss-resolve's error_shall_fallback() does. (dismissed: the author intends the fallback to cover only the "resolved absent" case, since on musl the getaddrinfo path is exactly what this PR exists to avoid)
  5. units/systemd-timesyncd.service.in:16 - After= is inert when the named unit is not in the transaction, so on systems where resolved is reached purely by varlink activation nothing guarantees the socket is listening before timesyncd's first resolve; also ordering after systemd-resolved-varlink.socket would close that hole at no cost and without a cycle. (fixed)
  6. src/timesync/timesyncd-manager.c:991-996 - the new branch and the reply handler's three error exits have no coverage; TEST-45-TIMEDATE.sh passes hostnames but never observes whether or how resolution happened, and with resolved running the getaddrinfo branch becomes dead in CI. (fixed: testcase_timesyncd_resolve() now asserts both resolver paths from the journal; see finding 14 on what that assertion can actually detect, and the reply error branches remain uncovered)

Nits:
7. [x] src/timesync/timesyncd-manager.c:861 - another hardcoded copy of the /run/systemd/resolve/io.systemd.Resolve literal; it could live in the already-included resolved-def.h alongside the SD_RESOLVED_* constants this code reuses. (dismissed as scope creep by the author, deferring to human maintainers)

Must fix:
8. [x] src/timesync/timesyncd-manager.h:44 - manager_is_connected() (timesyncd-manager.c:1171) still tests only server_socket/resolve_query/event_timer, so an in-flight Varlink resolve does not count as connected and any sd-network event during early boot tears the query down via manager_connect() and skips a server. (fixed)

Suggestions:
9. [x] src/timesync/timesyncd-manager.c:900 - the Varlink path now gates on socket_ipv6_is_enabled() while the getaddrinfo fallback at line 1015 still uses socket_ipv6_is_supported(), so the two resolver paths apply different IPv6 predicates. (fixed)
10. [x] src/timesync/timesyncd-manager.c:1036 - sd_varlink_flush_close_unref() in manager_disconnect() can block the single-threaded event loop inside json_stream_flush()'s unbounded ppoll() when the request is still buffered, and a stream-deadline expiry there trips an assert; the query is being cancelled, so sd_varlink_close_unref() is the safer teardown. (fixed)
11. [x] src/timesync/timesyncd-manager.c:858-862 - manager_resolve_complete() already routes its own failures through manager_connect(), so calling manager_connect() again on a negative return re-enters manager_disconnect(), discards what the first call set up and advances the server rotation twice, besides logging the same error a second time. (fixed: the tail of manager_resolve_varlink_reply() at lines 854-856 now simply returns manager_begin(m), leaving the rotation to manager_begin() as the author described; the two remaining manager_connect() calls inside the address loop cover failures nothing else handles)

Nits:
12. [x] src/timesync/timesyncd-manager.c:21 - in-addr-util.h breaks the alphabetical ordering of the internal include block. (fixed: it now sits between hashmap.h and list.h)
13. [x] src/timesync/timesyncd-manager.c:840 - failures that are logged and then swallowed use LOG_ERR without the "..., ignoring" suffix, inconsistently with the parse failure three lines above. (fixed for both cited sites, which are now log_warning_errno with a "trying next host" suffix; the remaining log_error() at line 848 is a verbatim copy of the long-standing one in the getaddrinfo handler and is left alone)

Must fix:
14. [x] test/units/TEST-45-TIMEDATE.sh:465 - "Resolved address .* for foo.localhost" is logged identically by manager_resolve_varlink_reply() (timesyncd-manager.c:844) and by the pre-existing manager_resolve_handler() (timesyncd-manager.c:773), and manager_resolve_varlink() emits nothing else on its success path, so the assertion stays green when the Varlink path silently falls back to getaddrinfo - the exact regression the case exists to catch. (fixed: the assertion at line 471 now greps io.systemd.Resolve.ResolveHostname, and that literal is genuinely emitted to timesyncd's journal - json_stream_format_json() logs the outgoing message under a plain DEBUG_LOGGING check with no extra opt-in - so only the Varlink path can satisfy it)

Suggestions:
15. [x] units/systemd-timesyncd.service.in:16 - Environment=SYSTEMD_NSS_RESOLVE_VALIDATE=0 (line 28) is now effectively dead: nss-resolve reads it in query_flags() but reaches resolved over the same /run/systemd/resolve/io.systemd.Resolve socket, and the getaddrinfo branch is only taken when timesyncd's own connect to that socket just failed; it also disables validation for the daemon's whole lifetime, unlike the new !synchronized-scoped flag. (fixed: the variable and its comment are gone from the unit)
16. [x] test/units/TEST-45-TIMEDATE.sh:453 - the only guard is the container check, so on a build without resolved (-Dresolve=false) the systemctl start of systemd-resolved-varlink.socket fails under the file's set -eux and takes the whole TEST-45 run down instead of skipping, even though a resolved-less system is one of the configurations this change targets. (fixed: a command -v resolvectl guard now skips the testcase at line 444)

Nits:
17. [x] src/timesync/timesyncd-manager.c:998 - res_init() runs before the Varlink attempt although it only serves the getaddrinfo path, which this change turns into the exception rather than the rule. (fixed: res_init() and its comment now sit after the manager_resolve_varlink() early return, immediately above the addrinfo hints)
18. [x] test/units/TEST-45-TIMEDATE.sh:257 - the journal helper forks a cat on each of its thirty iterations to re-read an unchanging cursor file, where the tree elsewhere uses the shell builtin read form. (fixed: the helper now uses the builtin redirection form for --after-cursor)
19. [x] units/systemd-timesyncd.service.in:28 - the commit message of 382f03a has a stray "while" in the paragraph explaining why SYSTEMD_NSS_RESOLVE_VALIDATE=0 was removed. (fixed: no commit message in the range contains that word any more)

No new findings.

Workflow run

@yuwata yuwata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. LGTM.

@yuwata yuwata added good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed and removed please-review PR is ready for (re-)review by a maintainer labels Aug 14, 2026
Comment thread src/timesync/timesyncd-manager.c Resolved
Comment thread src/timesync/timesyncd-manager.c Resolved
Comment thread src/timesync/timesyncd-manager.c Outdated
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)),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +812 to +813
if (!isempty(error_id)) {
log_debug("Failed to resolve %s: %s", m->current_server_name->string, error_id);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Comment thread units/systemd-timesyncd.service.in Resolved
Comment thread src/timesync/timesyncd-manager.c Resolved
assert(m);
assert(m->current_server_name);

r = sd_varlink_connect_address(&link, "/run/systemd/resolve/io.systemd.Resolve");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@yuwata

yuwata commented Aug 14, 2026

Copy link
Copy Markdown
Member

@craftyguy Please consider comments by Claude. If you think a suggestion is not valid, then feel free to ignore but comment something there.

@yuwata yuwata added good-to-merge/with-minor-suggestions and removed good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed labels Aug 14, 2026
@craftyguy
craftyguy force-pushed the timesyncd-dnssec-resolved branch from 006258f to f530436 Compare August 14, 2026 21:27
@github-actions github-actions Bot added the tests label Aug 14, 2026
Comment thread src/timesync/timesyncd-manager.h Resolved
Comment thread src/timesync/timesyncd-manager.c Resolved
Comment thread src/timesync/timesyncd-manager.c Resolved
Comment thread src/timesync/timesyncd-manager.c Resolved
Comment thread src/timesync/timesyncd-manager.c Resolved
Comment thread src/timesync/timesyncd-manager.c Resolved
@craftyguy
craftyguy force-pushed the timesyncd-dnssec-resolved branch from f530436 to 0467c8e Compare August 15, 2026 00:38
Comment thread test/units/TEST-45-TIMEDATE.sh Resolved
Comment thread src/timesync/timesyncd-manager.c Resolved
Comment thread units/systemd-timesyncd.service.in Resolved
Comment thread test/units/TEST-45-TIMEDATE.sh Resolved
@craftyguy
craftyguy force-pushed the timesyncd-dnssec-resolved branch from 0467c8e to e714c96 Compare August 15, 2026 03:05
Comment thread src/timesync/timesyncd-manager.c Resolved
Comment thread test/units/TEST-45-TIMEDATE.sh Resolved
Comment thread units/systemd-timesyncd.service.in Resolved
@craftyguy
craftyguy force-pushed the timesyncd-dnssec-resolved branch from e714c96 to 992670e Compare August 15, 2026 06:29
@craftyguy

Copy link
Copy Markdown
Contributor Author

@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

@yuwata yuwata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Minor requests.

Comment thread src/timesync/timesyncd-manager.c Resolved
Comment thread src/timesync/timesyncd-manager.c Resolved
Comment thread src/timesync/timesyncd-manager.c Resolved
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.
@craftyguy
craftyguy force-pushed the timesyncd-dnssec-resolved branch from 992670e to d52a693 Compare August 15, 2026 20:13
@yuwata
yuwata merged commit c8e5be5 into systemd:main Aug 16, 2026
57 checks passed
nikstur added a commit to nikstur/nixpkgs that referenced this pull request Sep 6, 2026
Removed 0006-timesyncd-disable-NSCD-when-DNSSEC-validation-is-dis.patch
because it was fixed upstream by systemd/systemd#43380
@nikstur nikstur mentioned this pull request Sep 6, 2026
4 of 13 tasks
nikstur added a commit to nikstur/nixpkgs that referenced this pull request Sep 9, 2026
Removed 0006-timesyncd-disable-NSCD-when-DNSSEC-validation-is-dis.patch
because it was fixed upstream by systemd/systemd#43380
nikstur added a commit to nikstur/nixpkgs that referenced this pull request Sep 15, 2026
- 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
nikstur added a commit to nikstur/nixpkgs that referenced this pull request Sep 17, 2026
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

resolve call failed: DNSSEC validation failed: signature-expired (system with no RTC)

2 participants