Skip to content

eBPF-based security platform

treewide: format the measured and reported values with strsprintf()

Daniel Borkmann027552fb9f60
diff --git a/builtin-attest.c b/builtin-attest.c
index 61df73f4..8da672f7 100644
--- a/builtin-attest.c
+++ b/builtin-attest.c
@@ -394,7 +394,10 @@ static int cmd_attest_predict(int argc, const char **argv)
SHIRUDO_TPM_NV_WRITTEN, policy, 32, name);
hex_encode(name, sizeof(name), hex);
printf("register_name: %s\n", hex);
- chain_anchor(name, anchor, sizeof(anchor));
+ if (chain_anchor(name, anchor, sizeof(anchor))) {
+ pr_err("attest predict: the anchor does not fit");
+ goto out;
+ }
printf("anchor: %s\n", anchor);
shirudo_sha256(anchor, strlen(anchor), anchor_digest);
chain_fold(bind[0], anchor_digest);
diff --git a/builtin-init.c b/builtin-init.c
index 8b6207fe..1c2477af 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -966,7 +966,9 @@ static int append_init_event(const struct shirudo_measured_upatch *set,
eventlog_timestamp(ts, sizeof(ts));
eventlog_boot_id(boot_id, sizeof(boot_id));
- snprintf(nv_index, sizeof(nv_index), "0x%08X", SHIRUDO_NV_INDEX);
+ if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",
+ SHIRUDO_NV_INDEX) < 0)
+ return -E2BIG;
hex_encode(cert_sha, 32, cert_hex);
hex_encode(set_digest, 32, set_hex);
hex_encode(tree_root, 32, tree_hex);
diff --git a/builtin-keys.c b/builtin-keys.c
index 554968ef..827c20be 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -120,16 +120,14 @@ static long days_remaining(const ASN1_TIME *not_after)
return days;
}
-static void format_expires_in(long days, char *out, size_t out_sz)
+static int format_expires_in(long days, char *out, size_t out_sz)
{
- if (days < 0) {
- snprintf(out, out_sz, "EXPIRED %ld days ago", -days);
- return;
- }
+ if (days < 0)
+ return strsprintf(out, out_sz, "EXPIRED %ld days ago", -days);
if (days >= 365)
- snprintf(out, out_sz, "%ldy %ldd", days / 365, days % 365);
- else
- snprintf(out, out_sz, "%ldd", days);
+ return strsprintf(out, out_sz, "%ldy %ldd", days / 365,
+ days % 365);
+ return strsprintf(out, out_sz, "%ldd", days);
}
static int compute_cert_fpr(X509 *c, __u8 fpr[SHA256_DIGEST_LENGTH])
@@ -181,7 +179,7 @@ static void print_fpr(const char *label, const __u8 *fpr)
* provider's own name ("ML-DSA-87"), whose parameter set already says
* everything a bit count would.
*/
-static void format_pkey_algorithm(EVP_PKEY *pkey, char *out, size_t out_sz)
+static int format_pkey_algorithm(EVP_PKEY *pkey, char *out, size_t out_sz)
{
const char *name = EVP_PKEY_get0_type_name(pkey);
int id = EVP_PKEY_get_base_id(pkey);
@@ -189,9 +187,9 @@ static void format_pkey_algorithm(EVP_PKEY *pkey, char *out, size_t out_sz)
if (!name)
name = "unknown";
if (id == EVP_PKEY_RSA || id == EVP_PKEY_EC)
- snprintf(out, out_sz, "%s-%d", name, EVP_PKEY_get_bits(pkey));
- else
- strscpy(out, name, out_sz);
+ return strsprintf(out, out_sz, "%s-%d", name,
+ EVP_PKEY_get_bits(pkey));
+ return strscpy(out, name, out_sz) < 0 ? -E2BIG : 0;
}
static int dump_operator_cert(X509 *cert, const char *cert_path,
@@ -687,7 +685,9 @@ static int append_key_event(const char *type, const char *fp_hex,
eventlog_timestamp(ts, sizeof(ts));
eventlog_boot_id(boot_id, sizeof(boot_id));
- snprintf(nv_index, sizeof(nv_index), "0x%08X", SHIRUDO_NV_INDEX);
+ if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",
+ SHIRUDO_NV_INDEX) < 0)
+ return -E2BIG;
hex_encode(event, 32, digest_hex);
json_obj_open(&e);
diff --git a/builtin-status.c b/builtin-status.c
index b66bf464..6ee0d2af 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -237,15 +237,16 @@ static void emit_eventlog_events(struct shirudo_json_emit *e)
json_arr_close(e);
}
-static void event_detail(const struct ev_proj *ev, char *out, size_t cap)
+static int event_detail(const struct ev_proj *ev, char *out, size_t cap)
{
if (ev->up_name[0])
- snprintf(out, cap, "%s payload=%.12s..", ev->up_name,
- ev->up_payload);
- else if (ev->set_digest[0])
- snprintf(out, cap, "set_digest=%.12s..", ev->set_digest);
- else
- out[0] = '\0';
+ return strsprintf(out, cap, "%s payload=%.12s..", ev->up_name,
+ ev->up_payload);
+ if (ev->set_digest[0])
+ return strsprintf(out, cap, "set_digest=%.12s..",
+ ev->set_digest);
+ out[0] = '\0';
+ return 0;
}
static void print_eventlog_table(void)
@@ -654,7 +655,9 @@ int cmd_status(int argc, const char **argv)
tamper_rc = tamper_read(&tamper);
{
- snprintf(nv_index, sizeof(nv_index), "0x%08X", SHIRUDO_NV_INDEX);
+ if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",
+ SHIRUDO_NV_INDEX) < 0)
+ strscpy(nv_index, "?", sizeof(nv_index));
if (tpm_tcti(tcti, sizeof(tcti)))
strscpy(tcti, "?", sizeof(tcti));
chain_rc = chain_status(&cst);
diff --git a/util/attest.c b/util/attest.c
index a89e2bb3..8bfa0809 100644
--- a/util/attest.c
+++ b/util/attest.c
@@ -304,7 +304,11 @@ int shirudo_attest_evidence_config(const __u8 *nonce, size_t nonce_len,
eventlog_timestamp(ts, sizeof(ts));
eventlog_boot_id(boot_id, sizeof(boot_id));
- snprintf(nv_index, sizeof(nv_index), "0x%08X", SHIRUDO_NV_INDEX);
+ if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",
+ SHIRUDO_NV_INDEX) < 0) {
+ rc = -E2BIG;
+ goto out;
+ }
hex_encode(nonce, nonce_len, nonce_hex);
e = (struct shirudo_json_emit){ .buf = *out, .cap = need };
@@ -555,8 +559,9 @@ static int on_record(void *ctx)
} else if (!strncmp(p->type, "upatch_", 7)) {
strscpy(r->label, p->upatch_name, sizeof(r->label));
} else if (!strcmp(p->type, "tree_op")) {
- snprintf(r->label, sizeof(r->label), "%s %s seq %lld",
- p->tree_op, p->tree_id, (__s64)p->tree_seq);
+ if (strsprintf(r->label, sizeof(r->label), "%s %s seq %lld",
+ p->tree_op, p->tree_id, (__s64)p->tree_seq) < 0)
+ return -E2BIG;
} else if (!strncmp(p->type, "key_", 4)) {
strscpy(r->label, p->key_fingerprint, sizeof(r->label));
}
@@ -595,8 +600,7 @@ static void say(struct reporter *r, bool ok, const char *fmt, ...)
r->failed++;
if (r->len + 8 >= r->cap)
return;
- n = snprintf(r->buf + r->len, r->cap - r->len, "%s ", ok ? "ok: " :
- "FAIL:");
+ n = strscpy(r->buf + r->len, ok ? "ok: " : "FAIL: ", r->cap - r->len);
if (n > 0)
r->len += (size_t)n;
va_start(ap, fmt);
@@ -1103,11 +1107,12 @@ int attest_verify(const char *evidence, size_t len,
{
char anchor[SHIRUDO_CHAIN_ANCHOR_MAX];
+ bool named = !chain_anchor(p->init.name, anchor,
+ sizeof(anchor));
- chain_anchor(p->init.name, anchor, sizeof(anchor));
shirudo_sha256(anchor, strlen(anchor), anchor_digest);
- say(&rep, ct_eq(anchor_digest, p->init.anchor_digest,
- 32),
+ say(&rep, named && ct_eq(anchor_digest, p->init.anchor_digest,
+ 32),
"anchor: record digest matches the index name");
memcpy(pcr_expected, p->init.pcr_before, 32);
chain_fold(pcr_expected, anchor_digest);
diff --git a/util/chain.c b/util/chain.c
index 7adb0065..58a64211 100644
--- a/util/chain.c
+++ b/util/chain.c
@@ -16,6 +16,7 @@
#include <unistd.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "chain.h"
#include "config.h"
@@ -87,10 +88,18 @@ void chain_init_event(const __u8 cert_sha[32],
memset(event, 0, 32);
return;
}
- for (i = 0; i < n; i++)
- off += (size_t)snprintf(listing + off, listing_len + 1 - off,
- "%s %s\n", set[i].payload_hex,
- set[i].name);
+ for (i = 0; i < n; i++) {
+ int w = strsprintf(listing + off, listing_len + 1 - off,
+ "%s %s\n", set[i].payload_hex, set[i].name);
+
+ if (w < 0) {
+ xfree(listing, listing_len + 1);
+ memset(set_digest, 0, 32);
+ memset(event, 0, 32);
+ return;
+ }
+ off += (size_t)w;
+ }
shirudo_sha256(listing, listing_len, set_digest);
xfree(listing, listing_len + 1);
@@ -152,14 +161,15 @@ void chain_fold(__u8 chain[32], const __u8 event[32])
memset(chain, 0, 32);
}
-void chain_anchor(const __u8 name[SHIRUDO_TPM_NAME_LEN],
- char *out, size_t cap)
+int chain_anchor(const __u8 name[SHIRUDO_TPM_NAME_LEN],
+ char *out, size_t cap)
{
char hex[2 * SHIRUDO_TPM_NAME_LEN + 1];
hex_encode(name, SHIRUDO_TPM_NAME_LEN, hex);
- snprintf(out, cap, "nvpcr-init:%s:0x%x:%s", SHIRUDO_CHAIN_NAME,
- SHIRUDO_NV_INDEX, hex);
+ return strsprintf(out, cap, "nvpcr-init:%s:0x%x:%s",
+ SHIRUDO_CHAIN_NAME, SHIRUDO_NV_INDEX, hex) < 0 ?
+ -E2BIG : 0;
}
/* The binding PCR values in PCR order, as PolicyPCR hashes them. */
@@ -278,8 +288,10 @@ int chain_init(const __u8 event[32],
memcpy(info->pcr_before, rec.pcr_before, 32);
memcpy(info->phase_before, rec.phase_before, 32);
memcpy(info->anchor_digest, rec.anchor_digest, 32);
- chain_anchor(info->name, info->anchor,
- sizeof(info->anchor));
+ rc = chain_anchor(info->name, info->anchor,
+ sizeof(info->anchor));
+ if (rc)
+ goto out;
info->initrd = rec.initrd;
info->reused = true;
goto out;
@@ -305,6 +317,9 @@ int chain_init(const __u8 event[32],
register_expectation(info->pcr_before, info->phase_before,
branch_written, branch_init, policy, unwritten,
written);
+ rc = chain_anchor(written, info->anchor, sizeof(info->anchor));
+ if (rc)
+ goto out;
if (!nv.defined || nv.written || !name_equals(&nv, unwritten)) {
rc = register_define(tpm, nv.defined, policy);
@@ -328,7 +343,6 @@ int chain_init(const __u8 event[32],
goto out;
memcpy(info->name, written, sizeof(written));
info->name_len = sizeof(written);
- chain_anchor(info->name, info->anchor, sizeof(info->anchor));
shirudo_sha256(info->anchor, strlen(info->anchor), info->anchor_digest);
rc = tpm_pcr_extend(tpm, SHIRUDO_CHAIN_PCR,
info->anchor_digest);
diff --git a/util/chain.h b/util/chain.h
index f3b7167c..219dbcc1 100644
--- a/util/chain.h
+++ b/util/chain.h
@@ -68,8 +68,8 @@ int shirudo_chain_fold_checked(__u8 chain[32], const __u8 event[32]);
void chain_fold(__u8 chain[32], const __u8 event[32]);
/* The PCR record anchoring a register name: nvpcr-init:shirudo:0x..:<name>. */
-void chain_anchor(const __u8 name[SHIRUDO_TPM_NAME_LEN],
- char *out, size_t cap);
+int chain_anchor(const __u8 name[SHIRUDO_TPM_NAME_LEN],
+ char *out, size_t cap);
struct shirudo_chain_init_info {
bool reused; /* written and on record */
diff --git a/util/conditions.c b/util/conditions.c
index b2493352..7fabe37b 100644
--- a/util/conditions.c
+++ b/util/conditions.c
@@ -31,10 +31,11 @@ size_t conditions(const struct shirudo_conditions_input *in,
c[n].type = "KeyringSealed";
if (!in->keyring_present && in->ksp_seen) {
c[n].status = "ALERT";
- snprintf(c[n].detail, sizeof(c[n].detail),
- "pin serial=%lld, the BPF keyring is gone "
- "(removed?)",
- (__s64)in->keyring_serial_pinned);
+ if (strsprintf(c[n].detail, sizeof(c[n].detail),
+ "pin serial=%lld, the BPF keyring is "
+ "gone (removed?)",
+ (__s64)in->keyring_serial_pinned) < 0)
+ strscpy(c[n].detail, "?", sizeof(c[n].detail));
} else if (!in->keyring_present) {
c[n].status = "WARN";
strscpy(c[n].detail,
@@ -42,17 +43,20 @@ size_t conditions(const struct shirudo_conditions_input *in,
sizeof(c[n].detail));
} else if (in->ksm_seen && !in->keyring_serial_matches) {
c[n].status = "ALERT";
- snprintf(c[n].detail, sizeof(c[n].detail),
- "pin serial=%lld != live serial=%lld (swapped?)",
- (__s64)in->keyring_serial_pinned,
- (__s64)in->keyring_serial);
+ if (strsprintf(c[n].detail, sizeof(c[n].detail),
+ "pin serial=%lld != live serial=%lld "
+ "(swapped?)",
+ (__s64)in->keyring_serial_pinned,
+ (__s64)in->keyring_serial) < 0)
+ strscpy(c[n].detail, "?", sizeof(c[n].detail));
} else {
c[n].status = "OK";
- snprintf(c[n].detail, sizeof(c[n].detail),
- "serial=%lld in the BPF keyring, %s",
- (__s64)in->keyring_serial,
- in->ksp_seen ? "pin matches"
- : "no pin recorded");
+ if (strsprintf(c[n].detail, sizeof(c[n].detail),
+ "serial=%lld in the BPF keyring, %s",
+ (__s64)in->keyring_serial,
+ in->ksp_seen ? "pin matches" :
+ "no pin recorded") < 0)
+ strscpy(c[n].detail, "?", sizeof(c[n].detail));
}
n++;
}
@@ -84,17 +88,20 @@ size_t conditions(const struct shirudo_conditions_input *in,
if (n < cap) {
c[n].type = "TpmMeasured";
c[n].status = in->chain_seen ? "OK" : "WARN";
- if (in->chain_seen)
- snprintf(c[n].detail, sizeof(c[n].detail),
- "NV index %s readable",
- in->nv_index ? in->nv_index : "?");
- else if (in->tpm_reachable)
- snprintf(c[n].detail, sizeof(c[n].detail),
- "NV index %s not bound this boot",
- in->nv_index ? in->nv_index : "?");
- else
+ 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(c[n].detail, "?", sizeof(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(c[n].detail, "?", sizeof(c[n].detail));
+ } else {
strscpy(c[n].detail, "no TPM answered",
sizeof(c[n].detail));
+ }
n++;
}
return n;
diff --git a/util/report.c b/util/report.c
index bb450455..2a94d79c 100644
--- a/util/report.c
+++ b/util/report.c
@@ -11,6 +11,8 @@
#include <string.h>
#include <sys/utsname.h>
+#include <shirudo/string.h>
+
#include "json.h"
#include "log.h"
#include "malloc.h"
@@ -30,12 +32,14 @@ int report_emit(const char *path, const struct shirudo_op_report *r)
FILE *f;
int rc = 0;
+ if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",
+ SHIRUDO_NV_INDEX) < 0)
+ return -E2BIG;
buf = xmalloc(cap);
if (!buf)
return -ENOMEM;
e = (struct shirudo_json_emit){ .buf = buf, .cap = cap };
- snprintf(nv_index, sizeof(nv_index), "0x%08X", SHIRUDO_NV_INDEX);
errname = r->err ? strerrorname_np(r->err) : NULL;
json_obj_open(&e);