Repository navigation
Description
Restoring a CMOS RTC saved state whose clock_time_millis is near i64::MIN or i64::MAX overflows the i64 offset that the RTC's host clock, SystemTimeClock, keeps from the host time. In a debug build, a very early time panics the restore itself with "attempt to subtract with overflow", and a very late one restores, after which the next read of the clock (the next save, or the guest's next read of status A or of the calendar) panics with "attempt to add with overflow". A release build does not panic: the clock reads a time about 292 million years before the epoch, so the RTC invalidates its calendar, and the guest reads the calendar registers as 0 and status D as 0x00. The time-ABI restore's downtime advance can also leave such a clock within a few milliseconds of i64::MAX: its checked_add passes, and the next read overflows the same way. No RTC saves such a time, and the downtime is at most 30 days, so only a crafted or foreign snapshot reaches this.
Reproduction
With the RTC on SystemTimeClock, restore an RTC SavedState with clock_time_millis = i64::MIN: the restore should fail, and in a debug build it panics in set_time. Or call advance_time with a downtime that brings the clock to i64::MAX - 2: it should fail, but returns Ok, and a save more than 2 ms later panics in get_time. The new tests below fail at the pin.
Cause
Rtc::restore_clock passes any i64 millisecond epoch to set_time:
// vm/devices/chipset/src/cmos_rtc/save_restore/snapshot.rs
pub(super) fn restore_clock(&mut self, clock_time_millis: i64) {
self.real_time_source
.set_time(LocalClockTime::from_millis_since_unix_epoch(
clock_time_millis,
));
}Rtc::advance_clock checks only its own addition:
// vm/devices/chipset/src/cmos_rtc/clock.rs
let advanced = current
.as_millis_since_unix_epoch()
.checked_add(delta)
.context("RTC time overflow while applying snapshot downtime")?;
self.real_time_source
.set_time(LocalClockTime::from_millis_since_unix_epoch(advanced));SystemTimeClock (support/local_clock) stores the guest time minus the host time, and adds the host time back on every read, both with plain i64 arithmetic. Neither function checks that the result fits.
Fix
fix.patch refuses clock times outside a bound: the times the guest's calendar can set (0000-01-01 to 9999-12-30T22:00Z, jiff's last UTC instant) widened by Linux's host-clock range (KTIME_SEC_MAX seconds). Every time an OpenVMM save produces, with the RTC's default zero offset, is inside it, and the clock's arithmetic cannot overflow for any time inside it. Out of the bound, Fields::validate fails with RestoreError::InvalidSavedState ("RTC clock time out of range") before the restore changes anything, and advance_clock fails with its existing overflow error and leaves the clock as it was, so such a time-ABI restore fails with "failed to advance restored VM time". The four tests in cmos_rtc/tests/snapshot.rs check both paths, with the test clock and with SystemTimeClock, including both bounds:
restore_refuses_a_clock_time_no_rtc_savesrestore_rejects_extreme_clock_time_with_system_clockadvance_time_refuses_a_downtime_past_the_clock_rangeadvance_time_rejects_extreme_downtime_with_system_clock
Activity
tangruize commentedon Nov 8, 2026
Proposed patch
Patch against OpenVMM 3cf58a5234ed824435825c9f87187f5129219724. Save the contents below as
fix.patchand rungit apply fix.patch.fix.patchopenvmm: pin the RTC's bounded restored and advanced time