From 34086d5bda29cc583755fc8948f59c3b61f8ce7d Mon Sep 17 00:00:00 2001 From: Conrad Meyer Date: Tue, 17 Mar 2020 22:27:16 +0000 Subject: [PATCH] Implement sysctl kern.boot_id Boot IDs are random, opaque 128-bit identifiers that distinguish distinct system boots. A new ID is generated each time the system boots. Unlike kern.boottime, the value is not modified by NTP adjustments. It remains fixed until the machine is restarted. PR: 244867 Reported by: Ricardo Fraile MFC after: I do not intend to, but feel free --- sys/kern/kern_mib.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/sys/kern/kern_mib.c b/sys/kern/kern_mib.c index 8f7530838c0..d1e8095d265 100644 --- a/sys/kern/kern_mib.c +++ b/sys/kern/kern_mib.c @@ -448,6 +448,32 @@ SYSCTL_PROC(_kern, KERN_HOSTID, hostid, CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, NULL, 0, sysctl_hostid, "LU", "Host ID"); +static struct mtx bootid_lk; +MTX_SYSINIT(bootid_lock, &bootid_lk, "bootid generator lock", MTX_DEF); + +static int +sysctl_bootid(SYSCTL_HANDLER_ARGS) +{ + static uint8_t boot_id[16]; + static bool initialized = false; + + mtx_lock(&bootid_lk); + if (!initialized) { + if (!is_random_seeded()) { + mtx_unlock(&bootid_lk); + return (ENXIO); + } + arc4random_buf(boot_id, sizeof(boot_id)); + initialized = true; + } + mtx_unlock(&bootid_lk); + + return (SYSCTL_OUT(req, boot_id, sizeof(boot_id))); +} +SYSCTL_PROC(_kern, OID_AUTO, boot_id, + CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, + NULL, 0, sysctl_bootid, "", "Random boot ID"); + /* * The osrelease string is copied from the global (osrelease in vers.c) into * prison0 by a sysinit and is inherited by child jails if not changed at jail