]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
timedated: Send error when time set is past build date time
authorPavithra Barithaya <pavithrabarithaya07@gmail.com>
Thu, 13 Feb 2025 06:58:40 +0000 (12:28 +0530)
committerLennart Poettering <lennart@poettering.net>
Thu, 20 Feb 2025 14:22:15 +0000 (15:22 +0100)
When the user/customer sets the time on the system which is prior
than that of the systemd build time, as systemd doesn't allow time
before it's build date after a reboot, systemd is resetting it but
there is no error or exception present in the setTime method due
to which user/customer is unaware of why the time is reset back to
the systemd-build time.

Added a condition check in the set_time() method to return an
error when tried to set time past the systemd build date.

Tested: Verified that it throws an error when we try to set the
time prior to systemd build date.

Change-Id: Ia6b58320bdb7234a21885a44af8fd3bda64c3789

src/timedate/timedated.c
test/units/TEST-30-ONCLOCKCHANGE.sh

index 6ee49c5975e147204765b16e4f23436c8dccedf5..64d35d2b363dd1b55e80876ede8b25a7b05a457c 100644 (file)
@@ -890,6 +890,10 @@ static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *erro
         } else
                 timespec_store(&ts, (usec_t) utc);
 
+        /* refuse the request when the time is before systemd build date time*/
+        if (ts.tv_sec < TIME_EPOCH)
+                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Requested to set the clock to time before build time, refusing.");
+
         r = bus_verify_polkit_async_full(
                         m,
                         "org.freedesktop.timedate1.set-time",
index 83698b8da697c9224b4bca269904cd51f1b98914..235b278f60101f01b637abea47d7d5e6ea3340dd 100755 (executable)
@@ -8,7 +8,14 @@ systemd-analyze log-level debug
 systemctl disable --now systemd-timesyncd.service
 
 timedatectl set-timezone Europe/Berlin
-timedatectl set-time 1980-10-15
+
+# A future timestamp needs to be used, otherwise 'timedatectl set-time' fails
+# if a timestamp older than the TIME_EPOCH is specified.
+current_time=$(date)
+
+future_time=$(date -d "$current_time + 1 year" +"%Y-%m-%d %H:%M:%S")
+
+timedatectl set-time "$future_time"
 
 systemd-run --on-timezone-change touch /tmp/timezone-changed
 systemd-run --on-clock-change touch /tmp/clock-changed
@@ -20,7 +27,9 @@ timedatectl set-timezone Europe/Kyiv
 
 while test ! -f /tmp/timezone-changed ; do sleep .5 ; done
 
-timedatectl set-time 2018-1-1
+future_time=$(date -d "$current_time + 1 year + 1 month" +"%Y-%m-%d %H:%M:%S")
+
+timedatectl set-time "$future_time"
 
 while test ! -f /tmp/clock-changed ; do sleep .5 ; done