eBPF-based security platform
// SPDX-License-Identifier: BSD-2-Clause
/*
* Copyright (c) 2026 Authors of shirudo
*
* Shared local security conditions block + status verdict.
*/
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <shirudo/misc.h>
#include <shirudo/string.h>
#include "conditions.h"
static bool config_untrusted(const struct conditions_input *in)
{
return in->config_seen && in->config_state &&
in->config_state != -EINPROGRESS;
}
static void config_untrusted_detail(struct condition *c, int state)
{
if (state == -ESTALE)
strscpy_literal(c->detail,
"the config activation record does not belong to this TPM boot");
else if (state == -EKEYREJECTED)
strscpy_literal(c->detail,
"the mounted config does not match the selected root");
else
strscpy_literal(c->detail,
"the config PCR, activation records and mounted config disagree");
}
static bool keyring_tampered(const struct conditions_input *in)
{
return (in->ksp_seen && !in->keyring_present) ||
(in->ksm_seen && !in->keyring_serial_matches) ||
(in->dmvp_seen && !in->dmv_present) ||
(in->dmvm_seen && !in->dmv_serial_matches) ||
in->bpf_unfrozen || in->dmv_unfrozen || in->dmv_key_missing;
}
/* A list of names can outgrow the detail; its start still says the most. */
static void __printf(3, 4)
set_detail(struct condition *c, const char *status,
const char *fmt, ...)
{
char full[1024];
va_list ap;
int ret;
c->status = status;
va_start(ap, fmt);
ret = vstrsprintf(full, sizeof(full), fmt, ap);
va_end(ap);
if (ret < 0)
strscpy_literal(c->detail, "?");
else if (strscpy(c->detail, full, sizeof(c->detail)) < 0)
memcpy(c->detail + sizeof(c->detail) - 4, "...", 4);
}
static void keyring_condition(const struct conditions_input *in,
struct condition *c)
{
c->type = "KeyringSealed";
if (!in->keyring_present && in->ksp_seen)
set_detail(c, "ALERT",
"pinned serial %lld, the .bpf keyring is gone",
(__s64)in->keyring_serial_pinned);
else if (!in->keyring_present)
set_detail(c, "WARN", ".bpf keyring absent before init");
else if (in->ksm_seen && !in->keyring_serial_matches)
set_detail(c, "ALERT",
"pinned serial %lld, live serial %lld: swapped",
(__s64)in->keyring_serial_pinned,
(__s64)in->keyring_serial);
else if (in->bpf_unfrozen)
set_detail(c, "ALERT",
"the BPF keyring holds %d link%s init did not freeze",
in->bpf_unfrozen, in->bpf_unfrozen == 1 ? "" : "s");
else if (in->dmvp_seen && !in->dmv_present)
set_detail(c, "ALERT",
"dm-verity pinned serial %lld, the dm-verity keyring is gone",
(__s64)in->dmv_serial_pinned);
else if (in->dmvm_seen && !in->dmv_serial_matches)
set_detail(c, "ALERT",
"dm-verity pinned serial %lld, live serial %lld: swapped",
(__s64)in->dmv_serial_pinned,
(__s64)in->dmv_serial);
else if (in->dmv_unfrozen)
set_detail(c, "ALERT",
"the dm-verity keyring holds %d link%s init did not freeze",
in->dmv_unfrozen, in->dmv_unfrozen == 1 ? "" : "s");
else if (in->dmv_key_missing)
set_detail(c, "ALERT",
"a config key init froze is gone from the dm-verity keyring");
else if (in->ksp_seen && in->dmvp_seen)
set_detail(c, "OK",
"bpf serial %lld, dm-verity serial %lld, pins match",
(__s64)in->keyring_serial, (__s64)in->dmv_serial);
else
set_detail(c, "OK", "bpf serial %lld, %s",
(__s64)in->keyring_serial,
in->ksp_seen ? "pin matches" : "no pin recorded");
}
size_t conditions(const struct conditions_input *in,
struct condition *c, size_t cap)
{
size_t n = 0;
if (n < cap) {
c[n].type = "LsmAttached";
if (in->lsm_attached) {
c[n].status = "OK";
strscpy_literal(c[n].detail,
"lsm/bpf link present and pinned");
} else {
c[n].status = "ALERT";
strscpy_literal(c[n].detail,
"lsm/bpf link absent, enforcement off");
}
n++;
}
if (n < cap)
keyring_condition(in, &c[n++]);
if (n < cap) {
c[n].type = "ChainIntact";
if (!in->chain_seen && in->tpm_reachable) {
c[n].status = "WARN";
strscpy_literal(c[n].detail,
"no register bound this boot (init incomplete)");
} else if (!in->chain_seen) {
c[n].status = "WARN";
strscpy_literal(c[n].detail,
"no TPM answered, no register to replay");
} else if (in->cm_seen && in->chain_matches) {
c[n].status = "OK";
strscpy_literal(c[n].detail,
"live register equals the eventlog replay");
} else if (in->cm_seen) {
c[n].status = "ALERT";
strscpy_literal(c[n].detail,
"live register differs from the eventlog replay");
} else {
c[n].status = "WARN";
strscpy_literal(c[n].detail,
"chain present, replay not evaluated");
}
n++;
}
if (n < cap) {
c[n].type = "UpatchesMeasured";
if (in->xdp_tampered)
set_detail(&c[n], "ALERT",
"%d XDP archives differ from the loaded payload: %s",
in->xdp_tampered,
in->xdp_tampered_detail ?
in->xdp_tampered_detail : "?");
else if (in->links_lost)
set_detail(&c[n], "ALERT",
"%d upatches are missing a link or their archive: %s",
in->links_lost,
in->links_lost_detail ?
in->links_lost_detail : "?");
else if (in->pins_rc == -ENOENT)
set_detail(&c[n], "WARN",
"no record of this boot to check the pins against");
else if (in->pins_rc == -EBADMSG)
set_detail(&c[n], "ALERT",
"this boot's records do not fold to the measurement register");
else if (in->pins_rc)
set_detail(&c[n], "WARN",
"pins not checked against the records: %s",
strerror(-in->pins_rc));
else if (in->pins_unmeasured)
set_detail(&c[n], "ALERT",
"%d pinned or archived without a record of this boot: %s",
in->pins_unmeasured,
in->pins_detail ? in->pins_detail : "?");
else if (in->checks_rc)
set_detail(&c[n], "WARN",
"links or archives not checked: %s",
strerror(-in->checks_rc));
else
set_detail(&c[n], "OK",
"every pinned or archived upatch is in this boot's records");
n++;
}
if (n < cap) {
c[n].type = "TpmMeasured";
c[n].status = in->chain_seen ? "OK" : "WARN";
if (in->chain_seen) {
if (strsprintf(c[n].detail, sizeof(c[n].detail),
"NV index %s readable",
in->nv_index ? in->nv_index : "?") < 0)
strscpy_literal(c[n].detail, "?");
} else if (in->tpm_reachable) {
if (strsprintf(c[n].detail, sizeof(c[n].detail),
"NV index %s not bound this boot",
in->nv_index ? in->nv_index : "?") < 0)
strscpy_literal(c[n].detail, "?");
} else {
strscpy_literal(c[n].detail, "no TPM answered");
}
n++;
}
if (n < cap) {
const char *root = in->config_root ? in->config_root : "?";
c[n].type = "ConfigActive";
c[n].status = "OK";
if (!in->config_seen) {
c[n].status = "WARN";
strscpy_literal(c[n].detail,
"no config activation state before init");
} else if (in->config_state == -EINPROGRESS) {
c[n].status = "WARN";
if (strsprintf(c[n].detail, sizeof(c[n].detail),
"the activation of config %.12s.. is pending until the next reboot",
root) < 0)
strscpy_literal(c[n].detail, "?");
} else if (config_untrusted(in)) {
c[n].status = "ALERT";
config_untrusted_detail(&c[n], in->config_state);
} else if (strsprintf(c[n].detail, sizeof(c[n].detail),
"config %.12s.. is active", root) < 0) {
strscpy_literal(c[n].detail, "?");
}
n++;
}
if (n < cap) {
c[n].type = "XdpCovered";
if (in->xdp_uncovered)
set_detail(&c[n], "WARN",
"%d selected devices run without their XDP upatch: %s",
in->xdp_uncovered,
in->xdp_uncovered_detail ?
in->xdp_uncovered_detail : "?");
else if (in->xdp_coverage_rc == -EXDEV)
set_detail(&c[n], "WARN", "XDP coverage not checked: %s",
"not in the initial network namespace");
else if (in->xdp_coverage_rc)
set_detail(&c[n], "WARN", "XDP coverage not checked: %s",
strerror(-in->xdp_coverage_rc));
else
set_detail(&c[n], "OK",
"every device an XDP upatch selects runs it");
n++;
}
return n;
}
void conditions_print(const struct condition *c, size_t n)
{
size_t i;
printf("Conditions:\n");
printf(" %-18s %-7s %s\n", "TYPE", "STATUS", "DETAIL");
for (i = 0; i < n; i++)
printf(" %-18s %-7s %s\n", c[i].type, c[i].status,
c[i].detail);
}
const char *status_verdict(const struct conditions_input *in)
{
if (!in->reachable)
return "unreachable";
if (!in->parsed)
return "unparseable";
if ((in->cm_seen && !in->chain_matches) || in->pins_unmeasured ||
in->pins_rc == -EBADMSG || in->xdp_tampered || in->links_lost ||
keyring_tampered(in) || config_untrusted(in))
return "tampered";
if (!in->lsm_attached)
return "lsm-off";
if (!in->keyring_present)
return "init-pending";
if (in->config_seen && in->config_state == -EINPROGRESS)
return "reboot-pending";
if (!in->chain_seen || in->xdp_uncovered)
return "degraded";
return "ok";
}
static const char *tampered_reason(const struct conditions_input *in)
{
if (in->cm_seen && !in->chain_matches)
return "live register differs from the eventlog replay";
if (in->xdp_tampered)
return "the archive of a loaded XDP upatch differs from what was loaded";
if (in->links_lost)
return "a loaded upatch is missing a link or its archive";
if (in->pins_unmeasured)
return "a upatch is pinned or archived without a record of this boot";
if (in->pins_rc == -EBADMSG)
return "this boot's records do not fold to the measurement register";
if (config_untrusted(in))
return "the selected config cannot be verified against its records";
if (in->ksp_seen && !in->keyring_present)
return "the keyring init froze is gone";
if (in->ksm_seen && !in->keyring_serial_matches)
return "keyring serial swap detected";
if (in->dmvp_seen && !in->dmv_present)
return "the dm-verity keyring init froze is gone";
if (in->dmvm_seen && !in->dmv_serial_matches)
return "dm-verity keyring serial swap detected";
if (in->bpf_unfrozen || in->dmv_unfrozen)
return "a keyring holds a link init did not freeze";
return "the config key init froze is gone";
}
const char *status_reason(const struct conditions_input *in)
{
const char *v = status_verdict(in);
if (strcmp(v, "unreachable") == 0)
return in->unreach ? in->unreach : "";
if (strcmp(v, "unparseable") == 0)
return "status JSON unparseable";
if (strcmp(v, "tampered") == 0)
return tampered_reason(in);
if (strcmp(v, "lsm-off") == 0)
return "LSM not attached, enforcement off";
if (strcmp(v, "init-pending") == 0)
return ".bpf keyring absent before init";
if (strcmp(v, "reboot-pending") == 0)
return "a config activation is pending until the next reboot";
if (strcmp(v, "degraded") == 0 && in->chain_seen)
return "a device an XDP upatch selects does not run it";
if (strcmp(v, "degraded") == 0)
return in->tpm_reachable ? "no register bound this boot" :
"no TPM answered";
return "";
}