Skip to content

eBPF-based security platform

util: one ISO 8601 formatter

Daniel Borkmann8f123f66ed74
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 5f16c915..273583fc 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -370,7 +370,7 @@ build/target/util/loader.o: $(BPF_OBJS)
tools/shirudo-build/shirudo-build: $(BUILD_OBJS) util/sign.o \
util/log.o util/json.o util/loader.o \
util/io.o util/malloc.o util/paths.o \
- util/upatch.o \
+ util/upatch.o util/timefmt.o \
util/elf_target.o util/hex.o util/keys.o \
util/seal.o \
util/crypto_openssl.o util/tls_openssl.o \
@@ -523,6 +523,7 @@ testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o u
# The register code pulls in the eventlog, which pulls in the seal
# label writer and the JSON codec.
REGISTER_OBJS := util/chain.o util/tpm.o util/tpm_wire.o util/eventlog.o \
+ util/timefmt.o \
util/hex.o util/crypto_openssl.o util/json.o util/io.o \
util/seal.o util/config.o util/log.o util/malloc.o \
util/initrd.o $(VERSION_OBJ)
@@ -601,7 +602,7 @@ testing/helper/test-otel-map: testing/helper/test-otel-map.c util/upatch_maps.o
util/tls_openssl.o util/serializer.o util/seal.o \
util/upatch_decode.o util/upatch_resolve.o \
util/upatch.o util/json.o util/log.o \
- util/io.o util/malloc.o $(LIBBPF_A) util/hex.o
+ util/io.o util/malloc.o $(LIBBPF_A) util/hex.o util/timefmt.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(DYNAMIC_LIBS)
@@ -618,7 +619,7 @@ testing/helper/test-upatch-serializer-pages: testing/helper/test-upatch-serializ
util/upatch_maps.o util/upatch_resolve.o \
util/serializer.o util/seal.o \
util/json.o util/malloc.o util/io.o \
- util/log.o util/hex.o
+ util/log.o util/hex.o util/timefmt.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(STATIC_LIBS) $(DYNAMIC_LIBS)
diff --git a/builtin-status.c b/builtin-status.c
index 8a9e527f..e840df6a 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -981,12 +981,8 @@ int cmd_status(int argc, const char **argv)
if (clock_gettime(CLOCK_REALTIME, &rt) == 0 &&
clock_gettime(CLOCK_BOOTTIME, &bt) == 0) {
boot_sec = (int64_t)rt.tv_sec - bt.tv_sec;
- struct tm tm = {};
- time_t t = (time_t)boot_sec;
-
- if (gmtime_r(&t, &tm))
- strftime(boot_iso, sizeof(boot_iso),
- "%Y-%m-%dT%H:%M:%SZ", &tm);
+ shirudo_iso8601((time_t)boot_sec, boot_iso,
+ sizeof(boot_iso));
}
shirudo_json_kv_string(&e, "boot_at",
boot_iso[0] ? boot_iso : "");
@@ -995,19 +991,13 @@ int cmd_status(int argc, const char **argv)
for (mi = 0; mi < ks.n; mi++) {
const struct shirudo_kmod *m =
&ks.mods[mi];
- char loaded_iso[32] = "";
+ time_t at = m->loaded_at_sec;
+ char iso[32] = "";
uint16_t di;
- if (m->loaded_at_sec > 0) {
- struct tm tm = {};
- time_t t = (time_t)m->loaded_at_sec;
-
- if (gmtime_r(&t, &tm))
- strftime(loaded_iso,
- sizeof(loaded_iso),
- "%Y-%m-%dT%H:%M:%SZ",
- &tm);
- }
+ if (at > 0)
+ shirudo_iso8601(at, iso,
+ sizeof(iso));
shirudo_json_obj_open(&e);
shirudo_json_kv_string(&e, "name",
m->name);
@@ -1016,7 +1006,7 @@ int cmd_status(int argc, const char **argv)
shirudo_json_kv_int(&e, "refcount",
m->refcount);
shirudo_json_kv_string(&e, "loaded_at",
- loaded_iso);
+ iso);
if (boot_sec > 0 &&
m->loaded_at_sec >= boot_sec)
shirudo_json_kv_int(&e,
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index d4febc3d..8c05e440 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -29,6 +29,7 @@
#include "log.h"
#include "hex.h"
#include "malloc.h"
+#include "timefmt.h"
#include "package.h"
#include "paths.h"
#include "upatch.h"
@@ -796,11 +797,10 @@ static int build_environment(const struct bpm_build_spec *spec,
char self[PATH_MAX] = "shirudo";
char timestamp[32] = {};
time_t now = time(NULL);
- struct tm tm;
ssize_t self_len;
- if (!body || uname(&un) || !gmtime_r(&now, &tm) ||
- !strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", &tm)) {
+ if (!body || uname(&un) ||
+ shirudo_iso8601(now, timestamp, sizeof(timestamp))) {
if (body)
xfree(body, BPM_BUILD_MAX_JSON);
return -EIO;
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 100e2484..152df37b 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -31,6 +31,7 @@
#include "malloc.h"
#include "paths.h"
#include "sign.h"
+#include "timefmt.h"
#include "upatch.h"
#define BPM_MANIFEST_CAP 8192u
@@ -195,7 +196,6 @@ static int push_index(struct bpm_oci_client *c, const char *repository,
size_t cap, sig_len = 0;
time_t now = time(NULL);
int n = 0, rc;
- struct tm tm;
entries = xcalloc(BPM_MAX_ENTRIES, sizeof(*entries));
if (!entries)
@@ -234,8 +234,7 @@ static int push_index(struct bpm_oci_client *c, const char *repository,
bpm_copy_string(entry->signer, sizeof(entry->signer), info->signer);
qsort(entries, (size_t)n, sizeof(*entries), index_entry_compare);
- if (!gmtime_r(&now, &tm) ||
- !strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", &tm)) {
+ if (shirudo_iso8601(now, timestamp, sizeof(timestamp))) {
rc = -EIO;
goto out;
}
diff --git a/util/eventlog.c b/util/eventlog.c
index 7a54c652..9e49d9aa 100644
--- a/util/eventlog.c
+++ b/util/eventlog.c
@@ -16,6 +16,7 @@
#include "chain.h"
#include "eventlog.h"
+#include "timefmt.h"
#include "io.h"
#include "json.h"
#include "log.h"
@@ -408,11 +409,7 @@ int shirudo_eventlog_boot_lines(int (*cb)(const char *line, size_t len,
void shirudo_eventlog_timestamp(char *out, size_t cap)
{
- time_t now = time(NULL);
- struct tm tm;
-
- gmtime_r(&now, &tm);
- strftime(out, cap, "%Y-%m-%dT%H:%M:%SZ", &tm);
+ shirudo_iso8601(time(NULL), out, cap);
}
void shirudo_eventlog_boot_id(char *out, size_t cap)
diff --git a/util/kmodules.c b/util/kmodules.c
index 7c04fe6f..d48042ed 100644
--- a/util/kmodules.c
+++ b/util/kmodules.c
@@ -471,17 +471,6 @@ void shirudo_kmodules_print_tree(const struct shirudo_kmod_set *set)
xfree(roots, set->n * sizeof(*roots));
}
-static void iso_utc(int64_t sec, char *out, size_t out_cap)
-{
- struct tm tm = {};
- time_t t = (time_t)sec;
-
- if (sec > 0 && gmtime_r(&t, &tm))
- strftime(out, out_cap, "%Y-%m-%dT%H:%M:%SZ", &tm);
- else
- snprintf(out, out_cap, "?");
-}
-
/*
* qsort comparator: descending by (loaded_at_sec, loaded_at_nsec).
* Indices into set->mods[]. Modules with loaded_at_sec == 0 (stat
@@ -535,7 +524,9 @@ void shirudo_kmodules_print_recent(const struct shirudo_kmod_set *set,
const struct shirudo_kmod *m = &set->mods[all[i].idx];
char ts[32], size_s[16], age[24];
- iso_utc(m->loaded_at_sec, ts, sizeof(ts));
+ if (m->loaded_at_sec <= 0 ||
+ shirudo_iso8601((time_t)m->loaded_at_sec, ts, sizeof(ts)))
+ snprintf(ts, sizeof(ts), "?");
shirudo_human_size(m->size, size_s, sizeof(size_s));
if (boot_sec > 0 && m->loaded_at_sec >= boot_sec)
shirudo_format_duration(
diff --git a/util/kmsg.c b/util/kmsg.c
index b56dfd95..9f528557 100644
--- a/util/kmsg.c
+++ b/util/kmsg.c
@@ -17,6 +17,7 @@
#include <unistd.h>
#include "kmsg.h"
+#include "timefmt.h"
#include "malloc.h"
/* Per-record buffer size accepted by /dev/kmsg: kernel allows up to 8 KiB. */
@@ -104,7 +105,6 @@ static void monotonic_to_iso(uint64_t mono_us,
const struct timespec *mt_at_open,
char *out, size_t out_cap)
{
- struct tm tm = {};
struct timespec rec;
int64_t delta_ns;
@@ -125,9 +125,7 @@ static void monotonic_to_iso(uint64_t mono_us,
rec.tv_sec++;
}
- if (gmtime_r(&rec.tv_sec, &tm))
- strftime(out, out_cap, "%Y-%m-%dT%H:%M:%SZ", &tm);
- else
+ if (shirudo_iso8601(rec.tv_sec, out, out_cap))
snprintf(out, out_cap, "?");
}
diff --git a/util/timefmt.c b/util/timefmt.c
index a7ed5734..002d8bb9 100644
--- a/util/timefmt.c
+++ b/util/timefmt.c
@@ -5,6 +5,7 @@
* Shared compact duration formatter.
*/
+#include <errno.h>
#include <stdio.h>
#include "timefmt.h"
@@ -22,3 +23,14 @@ void shirudo_format_duration(long seconds, char *buf, size_t cap)
else
snprintf(buf, cap, "%ldd", seconds / 86400);
}
+
+int shirudo_iso8601(time_t t, char *out, size_t cap)
+{
+ struct tm tm;
+
+ if (gmtime_r(&t, &tm) && strftime(out, cap, "%Y-%m-%dT%H:%M:%SZ", &tm))
+ return 0;
+ if (cap)
+ out[0] = '\0';
+ return -EINVAL;
+}
diff --git a/util/timefmt.h b/util/timefmt.h
index 971219f4..0dc5d8f2 100644
--- a/util/timefmt.h
+++ b/util/timefmt.h
@@ -9,8 +9,12 @@
#define SHIRUDO_TIMEFMT_H
#include <stddef.h>
+#include <time.h>
/* A negative seconds writes "?"; cap must hold the worst-case %ldd. */
void shirudo_format_duration(long seconds, char *buf, size_t cap);
+/* UTC to the second; "" and -EINVAL when t cannot be rendered. */
+int shirudo_iso8601(time_t t, char *out, size_t cap);
+
#endif /* SHIRUDO_TIMEFMT_H */
diff --git a/util/upatch_resolve.c b/util/upatch_resolve.c
index 34fcd595..1a04b1ca 100644
--- a/util/upatch_resolve.c
+++ b/util/upatch_resolve.c
@@ -26,6 +26,7 @@
#include "paths.h"
#include "hex.h"
#include "upatch_resolve.h"
+#include "timefmt.h"
#ifndef NFPROTO_IPV6
#define NFPROTO_IPV6 10
@@ -187,7 +188,6 @@ void shirudo_upatch_loaded_at(const char *name, char *out, size_t cap)
char dir[PATH_MAX];
char bpffs_name[SHIRUDO_UPATCH_BPFFS_NAME_MAX + 1];
struct stat st;
- struct tm tm;
out[0] = '\0';
if (shirudo_upatch_bpffs_name(bpffs_name, sizeof(bpffs_name), name))
@@ -197,9 +197,7 @@ void shirudo_upatch_loaded_at(const char *name, char *out, size_t cap)
return;
if (stat(dir, &st) != 0)
return;
- if (!gmtime_r(&st.st_ctim.tv_sec, &tm))
- return;
- strftime(out, cap, "%Y-%m-%dT%H:%M:%SZ", &tm);
+ shirudo_iso8601(st.st_ctim.tv_sec, out, cap);
}
void shirudo_upatch_payload_sha256(const char *name, char *out, size_t cap)