diff --git a/catalog/systemd.catalog.in b/catalog/systemd.catalog.in index 8d1812afcf..975e77fcec 100644 --- a/catalog/systemd.catalog.in +++ b/catalog/systemd.catalog.in @@ -528,6 +528,15 @@ Support: %SUPPORT_URL% For the first time during the current boot an NTP synchronization has been acquired and the local system clock adjustment has been initiated. +-- 7db73c8af0d94eeb822ae04323fe6ab6 +Subject: Initial clock bump +Defined-By: systemd +Support: %SUPPORT_URL% + +The system clock has been advanced based on a timestamp file on disk, in order +to ensure it remains roughly monotonic – even across reboots – if an RTC is not +available or is unreliable. + -- 3f7d5ef3e54f4302b4f0b143bb270cab Subject: TPM PCR Extended Defined-By: systemd diff --git a/src/systemd/sd-messages.h b/src/systemd/sd-messages.h index 51241c9426..00fdbad2c5 100644 --- a/src/systemd/sd-messages.h +++ b/src/systemd/sd-messages.h @@ -183,6 +183,9 @@ _SD_BEGIN_DECLARATIONS; #define SD_MESSAGE_TIME_SYNC SD_ID128_MAKE(7c,8a,41,f3,7b,76,49,41,a0,e1,78,0b,1b,e2,f0,37) #define SD_MESSAGE_TIME_SYNC_STR SD_ID128_MAKE_STR(7c,8a,41,f3,7b,76,49,41,a0,e1,78,0b,1b,e2,f0,37) +#define SD_MESSAGE_TIME_BUMP SD_ID128_MAKE(7d,b7,3c,8a,f0,d9,4e,eb,82,2a,e0,43,23,fe,6a,b6) +#define SD_MESSAGE_TIME_BUMP_STR SD_ID128_MAKE_STR(7d,b7,3c,8a,f0,d9,4e,eb,82,2a,e0,43,23,fe,6a,b6) + #define SD_MESSAGE_SHUTDOWN_SCHEDULED SD_ID128_MAKE(9e,70,66,27,9d,c8,40,3d,a7,9c,e4,b1,a6,90,64,b2) #define SD_MESSAGE_SHUTDOWN_SCHEDULED_STR SD_ID128_MAKE_STR(9e,70,66,27,9d,c8,40,3d,a7,9c,e4,b1,a6,90,64,b2) diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c index 887b323d96..e60742c39b 100644 --- a/src/timesync/timesyncd.c +++ b/src/timesync/timesyncd.c @@ -5,6 +5,7 @@ #include "sd-daemon.h" #include "sd-event.h" +#include "sd-messages.h" #include "capability-util.h" #include "clock-util.h" @@ -21,64 +22,118 @@ #include "timesyncd-manager.h" #include "user-util.h" +static int advance_tstamp(int fd, const struct stat *st) { + assert_se(fd >= 0); + assert_se(st); + + /* So here's the problem: whenever we read the timestamp we'd like to ensure the next time we won't + * restore the exact same time again, but one at least one step further (so that comparing mtimes of + * the timestamp file is a reliable check that timesync did its thing). But file systems have + * different timestamp accuracy: traditional fat has 2s granularity, and even ext2 and friends expose + * different granularity depending on selected inode size during formatting! Hence, to ensure the + * timestamp definitely is increased, here's what we'll do: we'll first try to increase the timestamp + * by 1µs, write that and read it back. If it was updated, great. But if it was not, we'll instead + * increase the timestamp by 10µs, and do the same, then 100µs, then 1ms, and so on, until it works, + * or we reach 10s. If it still didn't work then, the fs is just broken and we give up. */ + + usec_t target = MAX3(now(CLOCK_REALTIME), + TIME_EPOCH * USEC_PER_SEC, + timespec_load(&st->st_mtim)); + + for (usec_t a = 1; a <= 10 * USEC_PER_SEC; a *= 10) { /* 1µs, 10µs, 100µs, 1ms, … 10s */ + struct timespec ts[2]; + struct stat new_st; + + /* Bump to the maximum of the old timestamp advanced by the specified unit, */ + usec_t c = usec_add(target, a); + + timespec_store(&ts[0], c); + ts[1] = ts[0]; + + if (futimens(fd, ts) < 0) { + /* If this doesn't work at all, log, don't fail but give up */ + log_warning_errno(errno, "Unable to update mtime of timestamp file, ignoring: %m"); + return 0; + } + + if (fstat(fd, &new_st) < 0) + return log_error_errno(errno, "Failed to stat timestamp file: %m"); + + if (timespec_load(&new_st.st_mtim) > target) { + log_debug("Successfully bumped timestamp file."); + return 1; + } + + log_debug("Tried to advance timestamp file by " USEC_FMT ", but this didn't work, file system timestamp granularity too coarse?", a); + } + + log_debug("Gave up trying to advance timestamp file."); + return 0; +} + static int load_clock_timestamp(uid_t uid, gid_t gid) { usec_t min = TIME_EPOCH * USEC_PER_SEC, ct; _cleanup_close_ int fd = -EBADF; int r; - /* Let's try to make sure that the clock is always - * monotonically increasing, by saving the clock whenever we - * have a new NTP time, or when we shut down, and restoring it - * when we start again. This is particularly helpful on - * systems lacking a battery backed RTC. We also will adjust - * the time to at least the build time of systemd. */ + /* Let's try to make sure that the clock is always monotonically increasing, by saving the clock + * whenever we have a new NTP time, or when we shut down, and restoring it when we start again. This + * is particularly helpful on systems lacking a battery backed RTC. We also will adjust the time to + * at least the build time of systemd. */ fd = open(CLOCK_FILE, O_RDWR|O_CLOEXEC, 0644); - if (fd >= 0) { - struct stat st; - usec_t stamp; + if (fd < 0) { + if (errno != ENOENT) + log_debug_errno(errno, "Unable to open timestamp file '" CLOCK_FILE "', ignoring: %m"); - /* check if the recorded time is later than the compiled-in one */ - if (fstat(fd, &st) >= 0) { - stamp = timespec_load(&st.st_mtim); - if (stamp > min) - min = stamp; - } - - if (geteuid() == 0) { - /* Try to fix the access mode, so that we can still - touch the file after dropping privileges */ - r = fchmod_and_chown(fd, 0644, uid, gid); - if (r < 0) - log_warning_errno(r, "Failed to chmod or chown %s, ignoring: %m", CLOCK_FILE); - } - - } else { r = mkdir_safe_label(STATE_DIR, 0755, uid, gid, MKDIR_FOLLOW_SYMLINK | MKDIR_WARN_MODE); - if (r < 0) { + if (r < 0) log_debug_errno(r, "Failed to create state directory, ignoring: %m"); - goto settime; - } /* create stamp file with the compiled-in date */ r = touch_file(CLOCK_FILE, /* parents= */ false, min, uid, gid, 0644); if (r < 0) log_debug_errno(r, "Failed to create %s, ignoring: %m", CLOCK_FILE); + } else { + struct stat st; + usec_t stamp; + + /* check if the recorded time is later than the compiled-in one */ + if (fstat(fd, &st) < 0) + return log_error_errno(errno, "Unable to stat timestamp file '" CLOCK_FILE "': %m"); + + stamp = timespec_load(&st.st_mtim); + if (stamp > min) + min = stamp; + + /* Try to fix the access mode, so that we can still touch the file after dropping + * privileges */ + r = fchmod_and_chown(fd, 0644, uid, gid); + if (r < 0) + log_full_errno(ERRNO_IS_PRIVILEGE(r) ? LOG_DEBUG : LOG_WARNING, r, + "Failed to chmod or chown %s, ignoring: %m", CLOCK_FILE); + + (void) advance_tstamp(fd, &st); } -settime: ct = now(CLOCK_REALTIME); - if (ct < min) { - char date[FORMAT_TIMESTAMP_MAX]; + if (ct > min) + return 0; - log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s", - format_timestamp(date, sizeof(date), min)); - - if (clock_settime(CLOCK_REALTIME, TIMESPEC_STORE(min)) < 0) - log_error_errno(errno, "Failed to restore system clock, ignoring: %m"); + /* Not that it matters much, but we actually restore the clock to n+1 here rather than n, simply + * because we read n as time previously already and we want to progress here, i.e. not report the + * same time again. */ + if (clock_settime(CLOCK_REALTIME, TIMESPEC_STORE(min+1)) < 0) { + log_warning_errno(errno, "Failed to restore system clock, ignoring: %m"); + return 0; } + log_struct(LOG_INFO, + "MESSAGE_ID=" SD_MESSAGE_TIME_BUMP_STR, + "REALTIME_USEC=" USEC_FMT, min+1, + LOG_MESSAGE("System clock time unset or jumped backwards, restored from recorded timestamp: %s", + FORMAT_TIMESTAMP(min+1))); return 0; }