eBPF-based security platform
kmsg: convert record timestamps without overflowing
Daniel Borkmanned613359120c
diff --git a/testing/fuzz/corpus/kernel/kmsg-late b/testing/fuzz/corpus/kernel/kmsg-latenew file mode 100644index 00000000..3bf96d24--- /dev/null+++ b/testing/fuzz/corpus/kernel/kmsg-late@@ -0,0 +1 @@+6,1,9300000000000000,-;latediff --git a/testing/fuzz/fuzz-kernel.c b/testing/fuzz/fuzz-kernel.cindex 5e033d14..8591e9c7 100644--- a/testing/fuzz/fuzz-kernel.c+++ b/testing/fuzz/fuzz-kernel.c@@ -13,15 +13,20 @@int LLVMFuzzerTestOneInput(const __u8 *data, size_t size){+ const struct timespec rt = { 1792540800, 999999999 };char build_id[SHIRUDO_KERNEL_BUILD_ID_STRING_MAX];+ const struct timespec mt = { 86400, 1 };+ char ts[SHIRUDO_KMSG_TS_LEN];struct shirudo_kmod_set set;char msg[1024], *text;const char *m;__u64 us;size_t n;- if (!parse_record((const char *)data, size, &us, &m, &n))+ if (!parse_record((const char *)data, size, &us, &m, &n)) {+ monotonic_to_iso(us, &rt, &mt, ts, sizeof(ts));copy_msg(msg, sizeof(msg), m, n);+ }text = xmalloc(size + 1);if (!text)diff --git a/util/kmsg.c b/util/kmsg.cindex 88e908fd..b59d1939 100644--- a/util/kmsg.c+++ b/util/kmsg.c@@ -107,17 +107,16 @@ static void monotonic_to_iso(__u64 mono_us,char *out, size_t out_cap){struct timespec rec;- __s64 delta_ns;/** record_realtime = realtime_at_open + (record_monotonic- * - monotonic_at_open)+ * - monotonic_at_open),+ * seconds and nanoseconds apart so that no timestamp overflows.*/- delta_ns = (__s64)mono_us * 1000- - ((__s64)mt_at_open->tv_sec * 1000000000LL- + mt_at_open->tv_nsec);- rec.tv_sec = rt_at_open->tv_sec + delta_ns / 1000000000LL;- rec.tv_nsec = rt_at_open->tv_nsec + delta_ns % 1000000000LL;+ rec.tv_sec = rt_at_open->tv_sec - mt_at_open->tv_sec ++ (__s64)(mono_us / 1000000);+ rec.tv_nsec = rt_at_open->tv_nsec - mt_at_open->tv_nsec ++ (__s64)(mono_us % 1000000) * 1000;if (rec.tv_nsec < 0) {rec.tv_nsec += 1000000000LL;rec.tv_sec--;