Bug 283256 - CLOCK_MONOTONIC in dhclient
Summary: CLOCK_MONOTONIC in dhclient
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 14.2-STABLE
Hardware: Any Any
: --- Affects Many People
Assignee: freebsd-bugs (Nobody)
URL: https://cgit.freebsd.org/src/commit/?...
Keywords:
Depends on:
Blocks:
 
Reported: 2024-12-11 12:52 UTC by Andrey Blochintsev
Modified: 2025-04-13 02:11 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrey Blochintsev 2024-12-11 12:52:23 UTC
dhclient uses clock_gettime(CLOCK_MONOTONIC...) to track lease validity.
But CLOCK_MONOTONIC doesn't guarantee any relationship with wall clock. For example clock_monotic starts counting from zero on each reboot of my Freebsd 14.2 in proxmox venv. 

#> cat dhclient.leases.vtnet2 | tail -4
  renew 4 1970/1/1 22:10:57;
  rebind 4 1970/1/1 22:10:52;
  expire 4 1970/1/1 22:11:57;
}


Because of this, saved leases (/var/db/dhclient.leases*) are incorrectly considered usable as backup after a reboot.
Comment 1 Dave Cottlehuber freebsd_committer freebsd_triage 2025-04-03 07:14:40 UTC
pretty sure this is broken from https://cgit.freebsd.org/src/commit/?id=d854b4b2aa2b8b60e7fbca07e9637d035318aad5

Seen in CURRENT obviously, and should be fixed soon. This causes problems
for DHCP in some cases as Andrey has stated. Reported in OpnSense too.
Comment 2 Colin Percival freebsd_committer freebsd_triage 2025-04-09 00:27:02 UTC
Should be fixed by https://reviews.freebsd.org/D49720 .  Please test!
Comment 3 Dave Cottlehuber freebsd_committer freebsd_triage 2025-04-12 21:37:57 UTC
thanks Colin, LGTM here during #BSDNL
Comment 4 commit-hook freebsd_committer freebsd_triage 2025-04-13 02:11:10 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=43d19e6a4c42ade0f276ceca18a09e2e3829fce4

commit 43d19e6a4c42ade0f276ceca18a09e2e3829fce4
Author:     Colin Percival <cperciva@FreeBSD.org>
AuthorDate: 2025-04-08 22:50:01 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2025-04-13 02:09:56 +0000

    dhclient: Keep two clocks

    Until July 2024, dhclient kept track of time as seconds-since-epoch as
    a time_t.  This was a problem because (a) we wanted sub-second timeouts
    and (b) timeouts didn't always do the right thing if the system clock
    changed.

    Switching to using CLOCK_MONOTONIC and struct timespec fixed those
    issues but introduced a new problem: CLOCK_MONOTONIC values were being
    intepreted as seconds-since-epoch and written to the dhclient.leases
    file, causing confusion with DHCP leases expiring in early 1970.

    Attempt to compromise between these by keeping track of both times;
    any type within dhclient which is a time_t now refers to seconds past
    the epoch, while any struct timespec value is a CLOCK_MONOTONIC time.

    PR:     283256
    Reviewed by:    dch
    Fixes:  f0a38976b01e ("dhclient: Use clock_gettime() instead of time()")
    Sponsored by:   Amazon
    Differential Revision:  https://reviews.freebsd.org/D49720

 sbin/dhclient/dhclient.c | 50 +++++++++++++++++++++---------------------------
 sbin/dhclient/dhcpd.h    |  4 ++--
 sbin/dhclient/dispatch.c | 14 +++++++++-----
 3 files changed, 33 insertions(+), 35 deletions(-)