eBPF-based security platform
builtin: cut the comments down to why
Daniel Borkmann992f57684e46
diff --git a/builtin-bpm.c b/builtin-bpm.cindex 14654e90..67fcc894 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -2,9 +2,8 @@/** Copyright (c) 2026 Authors of shirudo*- * Target-local BPM package manager: repositories, signed indexes, the- * upatch tree and the .bee lifecycle. The operator-only build, push and- * tree append commands live in shirudo-build and are only dispatched.+ * The target's package manager; build, push and tree append are the+ * operator's and are dispatched to shirudo-build.*/#include <ctype.h>@@ -1306,10 +1305,9 @@ static int tree_read_pending(struct bpm_tree_pending *pending)}/*- * Measure one applied tree operation into this boot's register and- * record it: event = SHA256("shirudo-tree\0" || seq || operation_digest).- * The tree's own projection stays a file-based chain; the register is- * what an operator attests.+ * event = SHA256("shirudo-tree\0" || seq || operation_digest). The+ * tree's own projection stays a file chain; the register is what an+ * operator attests.*/static int tree_measure_operation(const struct shirudo_tree_operation *op,const uint8_t operation_digest[32])diff --git a/builtin-build.c b/builtin-build.cindex 97faa3de..192193fd 100644--- a/builtin-build.c+++ b/builtin-build.c@@ -85,11 +85,7 @@ bool shirudo_build_available(void)return use_path ? on_path(helper) : access(helper, X_OK) == 0;}-/*- * Replace argv[0] with the resolved helper and exec it. Returns only on- * failure, with the exit status to report; label names the subcommand- * in diagnostics.- */+/* Returns only on failure, with the exit status to report. */int shirudo_build_exec(const char *label, char **argv){bool use_path;diff --git a/builtin-config.c b/builtin-config.cindex 3f42aca4..8c27c521 100644--- a/builtin-config.c+++ b/builtin-config.c@@ -2,10 +2,8 @@/** Copyright (c) 2026 Authors of shirudo*- * `shirudo config` -- dump the effective shirudo.conf. When the file- * is absent, prints the defaults that would apply, in the same syntax- * so the output is usable as a starting-point template (modulo the- * redacted secret).+ * `shirudo config`: the effective shirudo.conf, or without one the+ * defaults in the same syntax, as a template.*/#include <errno.h>diff --git a/builtin-init.c b/builtin-init.cindex 12646551..bc2e514f 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -113,7 +113,6 @@ static void keyring_count_asymmetric(key_serial_t serial, const char *type,(*n)++;}-/* The asymmetric keys a keyring holds, or -errno. */static int keyring_asymmetric_count(key_serial_t ring){size_t n = 0;@@ -124,13 +123,11 @@ static int keyring_asymmetric_count(key_serial_t ring)return (int)n;}-/* Set once init_keyring has chosen the trust anchor for this boot. */static bool bpf_keyring_mode;/* The certificates init enrolled this boot; the init event covers them. */static struct shirudo_trusted_set enrolled;-/* Set when init runs in the initrd, the root file system at /sysroot. */static bool initrd_mode;/*@@ -166,7 +163,6 @@ static void warn_root_keys_differ(const struct shirudo_trusted_set *set)SHIRUDO_INITRD_KEYS_DIR, SHIRUDO_KEYS_DIR);}-/* How many of the set's certificates <ring> holds, or -errno. */static int keyring_holds_set(key_serial_t ring,const struct shirudo_trusted_set *set){@@ -514,12 +510,7 @@ static int init_bpffs(void)return 0;}-/*- * Pack a userspace dev_t (glibc-encoded by stat) into the kernel's- * internal dev_t encoding so the BPF program's read of- * inode->i_sb->s_dev compares equal. Kernel uses MKDEV(major, minor)- * == (major << 20) | minor.- */+/* The BPF side compares s_dev in the kernel's encoding, not glibc's. */static uint32_t kernel_dev_t(dev_t st_dev){return ((uint32_t)major(st_dev) << 20) | (minor(st_dev) & 0xfffffu);@@ -615,7 +606,6 @@ static int identity_executables(struct shirudo_identity *id)return 0;}-/* Attach a program and pin its link; *out is NULL on failure. */static int attach_pin(struct bpf_program *p, const char *pin,struct bpf_link **out){@@ -636,11 +626,7 @@ static int attach_pin(struct bpf_program *p, const char *pin,return 0;}-/*- * The seal's programs and their pins. The directory-marking hooks- * (file_open and the post-xattr hooks) precede inode_init_security,- * which labels on the strength of those marks.- */+/* The marking hooks come before inode_init_security, which relies on them. */static const struct {const char *prog;const char *pin;@@ -666,14 +652,9 @@ static const struct {};/*- * Load seal.bpf.o and attach + pin its filesystem-sealing and exec hooks.- * Keeps the object open via *obj_out (the caller tears it and the links- * down together). Must run BEFORE lsm/bpf is attached: seal is loaded- * unsigned and is not prog_type SYSCALL, so once the bpf() gate is live- * it would refuse this very load -- the same bootstrap window bpf2 uses.- * identity_fd is bpf2's frozen identity map, which the seal reads to- * recognise shirudo's executable instead of declaring its own copy, and- * tamper_fd its counters, so one set counts the refusals of both.+ * Before lsm/bpf attaches: the seal is loaded unsigned and would be+ * refused once the gate is live. It reads bpf2's identity map and+ * counts into its tamper map, so one set covers both.*/static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links,int identity_fd, int tamper_fd)@@ -776,11 +757,6 @@ static void collect_asymmetric(key_serial_t serial, const char *type,static int init_lsm(bool attach_filesystem_seal){struct shirudo_identity id = {};- /*- * The four mount-lifecycle guards over shirudo's bpffs. Table-driven- * so the attach loop stays short; each pin is destroyed in the out:- * path via mount_links[].- */static const struct {const char *prog;const char *pin;@@ -857,11 +833,7 @@ static int init_lsm(bool attach_filesystem_seal)}id.sb_dev = kernel_dev_t(st.st_dev);- /*- * Record shirudo's executable identity so the inode_unlink hook- * can tell shirudo's `upatch unload` from an attacker's `rm` of a- * pin.- */+ /* So inode guards can tell `upatch unload` from an attacker's rm. */err = identity_executables(&id);if (err)goto out;@@ -1039,11 +1011,8 @@ static int init_lsm(bool attach_filesystem_seal)}/*- * Mount-lifecycle guards over the bpffs -- attached here with the- * other bpffs guards so the mount is immutable before enforcement- * goes live. Without these a `umount` of the bpffs would drop every- * pin and detach all enforcement (the pins are the links' only- * reference, shirudo being agentless).+ * With the other bpffs guards, so the mount is immutable before+ * enforcement goes live.*/for (i = 0; i < (int)ARRAY_SIZE(mount_hooks); i++) {struct bpf_program *p = bpf_object__find_program_by_name(@@ -1209,25 +1178,17 @@ static void remove_stale_state(void)names[i]->d_name) < (int)sizeof(path) &&is_java_upatch(path))unlink_stale(path);- shirudo_libc_free(names[i]); /* scandir-owned */+ shirudo_libc_free(names[i]);}if (n >= 0)- shirudo_libc_free(names); /* scandir-owned */+ shirudo_libc_free(names);}/*- * Boot-restore: re-apply the installed upatch set after a reboot.- * Scans /var/lib/shirudo/upatches/ and runs each through the same- * signed load + attach + pin path as `shirudo upatch load`. On a- * re-run of init an upatch whose link is already pinned this boot is- * counted and left alone, provided the payload the pin records is the- * archived one; loading it again would only attach a second link to- * fail at the pin. All-or-nothing: a single failure stops the sweep- * and leaves init incomplete (the chain is not extended). On success,- * returns the loaded set (sorted by upatch name) so the caller can- * extend the measurement register by exactly one event over the whole- * set. The set may be empty (no upatches installed) -- the init event- * still fires.+ * All or nothing: one failure leaves init incomplete and the register+ * unextended. On a re-run an upatch already pinned this boot with the+ * archived payload is counted and left alone; loading it again would+ * only attach a second link to fail at the pin.*/static int boot_restore(struct shirudo_measured_upatch **out_set, size_t *out_n){@@ -1242,21 +1203,21 @@ static int boot_restore(struct shirudo_measured_upatch **out_set, size_t *out_n)n = scandir(SHIRUDO_UPATCHES_DIR, &names, select_upatch, alphasort);if (n < 0) {if (errno == ENOENT)- return 0; /* nothing installed -- empty set */+ return 0;pr_err("boot-restore: scandir %s: %s",SHIRUDO_UPATCHES_DIR, strerror(errno));return -errno;}if (n == 0) {- shirudo_libc_free(names); /* scandir-owned */+ shirudo_libc_free(names);return 0;}set = xcalloc((size_t)n, sizeof(*set));if (!set) {for (i = 0; i < n; i++)- shirudo_libc_free(names[i]); /* scandir-owned */- shirudo_libc_free(names); /* scandir-owned */+ shirudo_libc_free(names[i]);+ shirudo_libc_free(names);return -ENOMEM;}@@ -1278,7 +1239,6 @@ static int boot_restore(struct shirudo_measured_upatch **out_set, size_t *out_n)rc = -ENAMETOOLONG;goto next;}- /* Canonical name + payload hash for set_digest / eventlog. */if (shirudo_upatch_open(path, &u) == 0) {memcpy(set[cnt].name, u.hdr.name,SHIRUDO_UPATCH_NAME_MAX);@@ -1313,9 +1273,9 @@ static int boot_restore(struct shirudo_measured_upatch **out_set, size_t *out_n)}cnt++;next:- shirudo_libc_free(names[i]); /* scandir-owned */+ shirudo_libc_free(names[i]);}- shirudo_libc_free(names); /* scandir-owned */+ shirudo_libc_free(names);if (rc) {xfree(set, (size_t)n * sizeof(*set));@@ -1329,12 +1289,7 @@ next:return 0;}-/*- * Append the single "init" eventlog entry recording this boot's- * register: the event digest, the set it covers, and the binding- * (anchoring PCR value, index name, anchor record) a verifier needs- * to recompute the register's public area.- */+/* The binding a verifier needs to recompute the register's public area. */static int append_init_event(const struct shirudo_measured_upatch *set,size_t n,const uint8_t cert_sha[32],@@ -1418,7 +1373,6 @@ static int append_init_event(const struct shirudo_measured_upatch *set,shirudo_eventlog_path(), buf);}-/* A PCR nothing has extended since the TPM reset. */static bool phase_unmeasured(const uint8_t pcr[32]){int i;@@ -1429,7 +1383,6 @@ static bool phase_unmeasured(const uint8_t pcr[32])return true;}-/* The upatch tree's projection root at boot, zeros when no tree is kept. */static void tree_root_at_boot(uint8_t out[32]){struct shirudo_tree_state s = {};@@ -1567,7 +1520,6 @@ static void usage(void)fprintf(stderr, "usage: %s\n", init_help.usage);}-/* mkdir -p for a path of plain directories. */static int mkdir_parents(const char *path, mode_t mode){char buf[PATH_MAX], *p;diff --git a/builtin-keys.c b/builtin-keys.cindex 6a8d41df..011031af 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -121,10 +121,6 @@ static long days_remaining(const ASN1_TIME *not_after)return days;}-/*- * Format an "Expires in:" value: "9y 213d" / "182d" / "EXPIRED 3 days ago".- * Keeps the same expired-on-negative convention as days_remaining().- */static void format_expires_in(long days, char *out, size_t out_sz){if (days < 0) {@@ -674,7 +670,6 @@ static const struct shirudo_help keys_revoke_help = {.n_sections = ARRAY_SIZE(keys_revoke_sections),};-/* Eventlog entry for a key event, mirroring an extend. */static int append_key_event(const char *type, const char *fp_hex,const char *sha_hex, const uint8_t event[32]){@@ -754,11 +749,10 @@ static int select_upatch(const struct dirent *e)}/*- * Every archived upatch whose signer is the revoked certificate goes:- * the kernel would refuse to restore it at the next boot, and keeping- * it loaded would keep running what a key nobody trusts any more- * authorised. Returns the number purged, or -errno when one could not- * be; the rest are still tried.+ * Every archived upatch the revoked certificate signed goes: the kernel+ * would refuse to restore it at the next boot, and keeping it loaded+ * would keep running what a key nobody trusts any more authorised. The+ * rest are still tried when one fails.*/static int purge_signed_by(const unsigned char *der, size_t der_sz,size_t *unloaded)@@ -806,13 +800,12 @@ static int purge_signed_by(const unsigned char *der, size_t der_sz,*unloaded += prc;}for (i = 0; i < n; i++)- shirudo_libc_free(names[i]); /* scandir-owned */- shirudo_libc_free(names); /* scandir-owned */+ shirudo_libc_free(names[i]);+ shirudo_libc_free(names);shirudo_cert_free(&cert);return rc ? rc : purged;}-/* Replace the primary with a member: its file becomes cert.der. */static int promote(const struct shirudo_trusted_cert *c){unsigned char *der = NULL;@@ -1127,10 +1120,6 @@ static int keys_show(int argc, const char **argv)shown = true;}- /*- * The set a target enrols in the bpf keyring: the primary and the- * trusted certificates beside it, less the revoked ones.- */if (shirudo_keys_load(keys_dir, NULL, true, &set) == 0 &&(set.n > 1 || set.n_revoked || set.n_unvouched)) {size_t i;diff --git a/builtin-status.c b/builtin-status.cindex 49798d3c..8a9e527f 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -2,9 +2,7 @@/** Copyright (c) 2026 Authors of shirudo*- * Local readiness + state report: shirudo status [--json] [--otel].- * Local-only -- never contacts another host. Safe to run before `init`- * (everything degrades to "absent"/false).+ * The local readiness and state report, safe before `init`.*/#include <dirent.h>@@ -64,7 +62,6 @@ static int notdot(const struct dirent *e)return strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0;}-/* Count JSON-lines (one chain event per line) and the file size. */static int count_eventlog(long *entries, long *bytes){char buf[65536];@@ -91,7 +88,6 @@ static int count_eventlog(long *entries, long *bytes)return 0;}-/* On-disk inventory under /var/lib/shirudo. -1 counts mean "dir absent". */struct asset_view {long eventlog_bytes;int n_upatches;@@ -102,7 +98,6 @@ struct asset_view {bool bpffs_mounted;};-/* Count entries in <dir>; with upatch_only, only ".upatch" files. */static int count_dir(const char *dir, bool upatch_only){struct dirent **list = NULL;@@ -118,9 +113,9 @@ static int count_dir(const char *dir, bool upatch_only)if (!upatch_only ||(len > 7 && strcmp(nm + len - 7, ".upatch") == 0))count++;- shirudo_libc_free(list[i]); /* scandir-owned */+ shirudo_libc_free(list[i]);}- shirudo_libc_free(list); /* scandir-owned */+ shirudo_libc_free(list);return count;}@@ -163,9 +158,8 @@ static void gather_assets(struct asset_view *a)}/*- * Projection of one eventlog line for display: enough for the compact- * table and the events[] JSON, not the full record (the authoritative- * copy stays the on-disk log, which status always replays).+ * Enough of a record for the table and events[]; the log itself stays+ * the authoritative copy.*/struct ev_proj {char ts[40];@@ -200,7 +194,6 @@ static int parse_event_line(const char *line, size_t len, struct ev_proj *ev)return shirudo_json_parse(line, len, f, ARRAY_SIZE(f));}-/* Slurp the whole eventlog. 0 with buf/len set, or -errno (buf == NULL). */static int read_eventlog(char **buf, size_t *len){struct stat st;@@ -234,7 +227,6 @@ static int read_eventlog(char **buf, size_t *len)return 0;}-/* Emit the eventlog as a JSON "events":[...] array (projected fields). */static void emit_eventlog_events(struct shirudo_json_emit *e){char *save, *line, *buf;@@ -273,7 +265,6 @@ static void emit_eventlog_events(struct shirudo_json_emit *e)shirudo_json_arr_close(e);}-/* One-line human detail for an eventlog row. */static void event_detail(const struct ev_proj *ev, char *out, size_t cap){if (ev->up_name[0])@@ -285,7 +276,6 @@ static void event_detail(const struct ev_proj *ev, char *out, size_t cap)out[0] = '\0';}-/* Compact TIME/TYPE/DIGEST/DETAIL table, read from the local log. */static void print_eventlog_table(void){char *save, *line, *buf;@@ -313,10 +303,6 @@ static void print_eventlog_table(void)xfree(buf, len);}-/*- * Print the "System Info:" section with a 2-space indent and 16-column- * label column. Caller prints any preceding blank line.- */static void print_system_human(const struct shirudo_sysinfo *si){printf("\nSystem Info:\n");@@ -417,12 +403,6 @@ static void usage(void)fprintf(stderr, "usage: %s\n", status_help.usage);}-/*- * Build the upatches_loaded table rows from a scandir() listing.- * Each row is resolved via the shared util (location + pin ctime +- * payload xattr). Returns the row count; *out is heap-owned (xfree- * by caller with n * sizeof(*out)).- */static size_t build_upatches_table(struct dirent **ups, int n_ups,struct shirudo_upatch_table_row **out){@@ -476,11 +456,7 @@ static size_t build_upatches_table(struct dirent **ups, int n_ups,return (size_t)n_ups;}-/*- * Convert local probes into the shared conditions input. `shirudo- * status` always runs on-host, so reachable/parsed are trivially true;- * this gives the shared verdict / Conditions block its local meaning.- */+/* Status runs on the host, so reachable and parsed are trivially true. */static void local_cond_input(struct shirudo_conditions_input *in,int lsm_attached, int keyring_present, int staging,int64_t keyring_serial,@@ -509,7 +485,6 @@ static void local_cond_input(struct shirudo_conditions_input *in,};}-/* Compose the locally detected roles: "target", "operator", or both. */static const char *roles_str(int is_target, int is_operator){if (is_target && is_operator)@@ -555,7 +530,6 @@ static bool status_otel_endpoint_configured(const struct shirudo_config *cfg)return endpoint && endpoint[0];}-/* shirudo_keyring_list callback: count the asymmetric keys. */static void count_asymmetric(key_serial_t serial, const char *type,const char *description, void *ud){@@ -776,14 +750,10 @@ int cmd_status(int argc, const char **argv)struct shirudo_json_emit e;/*- * The events[] dump re-emits one object per logged line, so- * size the buffer to the on-disk log plus headroom for the- * fixed fields; 16 KiB covers the no-eventlog case.- * --include-modules pads ~250 B per module; --include-kernellog- * pads up to 8 KiB total (15 lines * ~500 B); --include-system- * includes the kernel config, which is typically hundreds of KiB.- * Eyeball-padded,- * the cap-clamp at the bottom keeps us under SHIRUDO_JSON_MAX_BYTES.+ * The events[] dump re-emits the log, so size the buffer+ * from it with headroom for the fixed fields and the+ * optional sections; the clamp below keeps it under+ * SHIRUDO_JSON_MAX_BYTES.*/json_cap = 16384;if (include_eventlog && ev_rc == 0)@@ -1329,8 +1299,8 @@ int cmd_status(int argc, const char **argv)out:for (i = 0; i < n_ups; i++)- shirudo_libc_free(ups[i]); /* scandir-owned */- shirudo_libc_free(ups); /* scandir-owned */+ shirudo_libc_free(ups[i]);+ shirudo_libc_free(ups);if (json_buf)xfree(json_buf, json_cap);return ret;diff --git a/builtin.h b/builtin.hindex 78c8ab05..7bdf4939 100644--- a/builtin.h+++ b/builtin.h@@ -15,9 +15,8 @@ int cmd_genkey(int argc, const char **argv);int cmd_build(int argc, const char **argv);/*- * The shirudo-build helper (operator package only): whether it can be- * reached, and the single exec site that hands an argument vector to- * it (argv[0] is replaced; returns only on failure).+ * The operator package's helper: whether it is there, and the one exec+ * site, which returns only on failure.*/bool shirudo_build_available(void);int shirudo_build_exec(const char *label, char **argv);@@ -27,20 +26,14 @@ int cmd_init(int argc, const char **argv);int cmd_upatch(int argc, const char **argv);int cmd_bpm(int argc, const char **argv);-/*- * Target-side shared op: load + attach + pin one signed LOAD upatch.- * Used by `shirudo upatch load` and by `shirudo init` boot-restore so- * both share the identical signature-verified path. 0 on success.- */int shirudo_upatch_apply(const char *path);int shirudo_upatch_install(const char *path);int shirudo_upatch_remove(const char *path);/*- * Target-side: detach, unpin and unarchive the upatch of that name- * without a directive, because the key that signed it was revoked.- * reason goes into the eventlog record. 1 when it was loaded and the- * unload was measured, 0 when only the archive went, -errno otherwise.+ * Detach, unpin and unarchive without a directive, because the key+ * that signed the upatch was revoked: 1 when the unload was measured,+ * 0 when only the archive went, -errno otherwise.*/int shirudo_upatch_purge(const char *name, const char *reason);diff --git a/shirudo.c b/shirudo.cindex 4e607e63..e619b013 100644--- a/shirudo.c+++ b/shirudo.c@@ -18,10 +18,6 @@#include "util/macros.h"#include "util/similar.h"-/*- * Which side a command runs on. The operator holds the signing key; the- * target commands run one-shot locally; local commands work on either side.- */enum cmd_group {GROUP_OPERATOR,GROUP_TARGET,@@ -39,11 +35,8 @@ struct cmd {static int cmd_help(int argc, const char **argv);/*- * Single source of truth for subcommands. Adding an entry updates the- * dispatch, `shirudo help`, and bash completion's top-level list (which- * is pulled live via `shirudo help --list-cmds`, so it never drifts).- * completion/shirudo.bash needs editing only for new *sub*commands or- * flags, not for new top-level commands.+ * Adding an entry updates the dispatch, `shirudo help` and bash+ * completion, which pulls the list live through --list-cmds.*/static const struct cmd commands[] = {{ "genkey", cmd_genkey, GROUP_OPERATOR, "generate the signing key + cert" },@@ -84,11 +77,6 @@ static void print_group(enum cmd_group g, const char *header)static int cmd_help(int argc, const char **argv){- /*- * `help --list-cmds` prints the bare command names to stdout, one- * per line -- the hook bash completion uses so a newly added- * command shows up without editing the completion script.- */if (argc > 1 && strcmp(argv[1], "--list-cmds") == 0) {for (size_t i = 0; i < commands_n; i++)printf("%s\n", commands[i].name);@@ -120,31 +108,19 @@ int main(int argc, const char **argv)cmd_help(0, NULL);return 1;}- /*- * Top-level --list-cmds prints bare top-level command names to- * stdout. Bash completion uses this so new commands appear in- * tab-completion without editing the completion script.- */if (shirudo_is_list_cmds(argv[1])) {for (i = 0; i < commands_n; i++)printf("%s\n", commands[i].name);return 0;}- /*- * Top-level --help / -h are equivalent to the `help` subcommand.- * Pass the rest of argv through so `shirudo --help --list-cmds`- * reaches cmd_help's --list-cmds emit path too.- */+ /* With the rest of argv, so `--help --list-cmds` still lists. */if (shirudo_is_help(argv[1]))return cmd_help(argc - 1, argv + 1);for (i = 0; i < commands_n; i++)names[i] = commands[i].name;- /*- * Tolerant resolution is left to the Levenshtein "did you mean" pass- * below.- */+ /* Tolerant matching is the Levenshtein pass below. */n_match = shirudo_resolve(argv[1], names, commands_n,matches, ARRAY_SIZE(matches));if (n_match == 1) {