eBPF-based security platform
status: check every string copy of the conditions and status
Daniel Borkmann68d458bcec53
diff --git a/builtin-status.c b/builtin-status.cindex 60c86acf..41ed79b6 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -78,8 +78,10 @@ static int note_unmeasured(const char *name, void *ctx)if (used >= 3 && !strcmp(names + used - 3, "..."))return 0;if (strsprintf(names + used, UNMEASURED_NAMES - used, "%s%s",- used ? ", " : "", name) < 0 && used)- strscpy(names + used, ", ...", UNMEASURED_NAMES - used);+ used ? ", " : "", name) >= 0 || !used)+ return 0;+ if (strscpy(names + used, ", ...", UNMEASURED_NAMES - used) < 0)+ memcpy(names + UNMEASURED_NAMES - 4, "...", 4);return 0;}@@ -441,8 +443,9 @@ static int java_row(const char *name,j->rows = r;r = &j->rows[j->n++];memset(r, 0, sizeof(*r));- strscpy(r->name, name, sizeof(r->name));- strscpy(r->location, "java", sizeof(r->location));+ if (strscpy(r->name, name, sizeof(r->name)) < 0)+ strscpy_literal(r->name, "?");+ strscpy_literal(r->location, "java");hex_encode(payload, UPATCH_PAYLOAD_HASH_LEN, r->payload_sha256);if (!java_agent_path(name, path, sizeof(path)) && !stat(path, &st))iso8601(st.st_ctim.tv_sec, r->loaded_at, sizeof(r->loaded_at));@@ -619,7 +622,8 @@ static int loaded_set_java(const char *name,if (!u)return -ENOMEM;- strscpy(u->name, name, sizeof(u->name));+ if (strscpy(u->name, name, sizeof(u->name)) < 0)+ return -ENAMETOOLONG;hex_encode(payload, UPATCH_PAYLOAD_HASH_LEN, u->payload_hex);loaded_set_take(l);return 0;@@ -1428,9 +1432,9 @@ static void status_read_tpm(struct status_tpm *t){if (strsprintf(t->nv_index, sizeof(t->nv_index), "0x%08X",NV_INDEX) < 0)- strscpy(t->nv_index, "?", sizeof(t->nv_index));+ strscpy_literal(t->nv_index, "?");if (tpm_tcti(t->tcti, sizeof(t->tcti)))- strscpy(t->tcti, "?", sizeof(t->tcti));+ strscpy_literal(t->tcti, "?");t->chain_rc = chain_status(&t->cst);if (t->chain_rc == 0 && !(t->cst.defined && t->cst.written))t->chain_rc = -ENODEV;@@ -1500,7 +1504,7 @@ static void status_gather(struct status_view *v, bool include_system)if (include_system)sysinfo_gather(&v->sysi);if (gethostname(v->hostname, sizeof(v->hostname)))- strscpy(v->hostname, "?", sizeof(v->hostname));+ strscpy_literal(v->hostname, "?");v->hostname[sizeof(v->hostname) - 1] = '\0';v->lsm_attached = lsm_link_pinned();status_read_keyrings(v);@@ -1640,7 +1644,8 @@ static size_t build_upatches_table(const struct status_view *v,ups[i]->d_name) == 0)name = raw_name;- strscpy(r->name, name, sizeof(r->name));+ if (strscpy(r->name, name, sizeof(r->name)) < 0)+ strscpy_literal(r->name, "?");upatch_resolve_location(vbtf, name,r->location,sizeof(r->location));diff --git a/util/conditions.c b/util/conditions.cindex 2d61e3ed..6ba670bd 100644--- a/util/conditions.c+++ b/util/conditions.c@@ -21,13 +21,17 @@ static bool config_untrusted(const struct conditions_input *in)in->config_state != -EINPROGRESS;}-static const char *config_untrusted_detail(int state)+static void config_untrusted_detail(struct condition *c, int state){if (state == -ESTALE)- return "the config activation record does not belong to this TPM boot";- if (state == -EKEYREJECTED)- return "the mounted config does not match the selected root";- return "the config PCR, activation records and mounted config disagree";+ 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)@@ -53,7 +57,7 @@ set_detail(struct condition *c, const char *status,ret = vstrsprintf(full, sizeof(full), fmt, ap);va_end(ap);if (ret < 0)- strscpy(c->detail, "?", sizeof(c->detail));+ strscpy_literal(c->detail, "?");else if (strscpy(c->detail, full, sizeof(c->detail)) < 0)memcpy(c->detail + sizeof(c->detail) - 4, "...", 4);}@@ -110,37 +114,41 @@ size_t conditions(const struct conditions_input *in,if (n < cap) {c[n].type = "LsmAttached";- c[n].status = in->lsm_attached ? "OK" : "ALERT";- strscpy(c[n].detail, in->lsm_attached ?- "lsm/bpf link present and pinned" :- "lsm/bpf link absent, enforcement off",- sizeof(c[n].detail));+ 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) {+ 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(c[n].detail, in->tpm_reachable ?- "no register bound this boot (init incomplete)" :- "no TPM answered, no register to replay",- sizeof(c[n].detail));+ 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(c[n].detail, "live register equals the eventlog replay",- sizeof(c[n].detail));+ strscpy_literal(c[n].detail,+ "live register equals the eventlog replay");} else if (in->cm_seen) {c[n].status = "ALERT";- strscpy(c[n].detail,- "live register differs from the eventlog replay",- sizeof(c[n].detail));+ strscpy_literal(c[n].detail,+ "live register differs from the eventlog replay");} else {c[n].status = "WARN";- strscpy(c[n].detail,- "chain present, replay not evaluated",- sizeof(c[n].detail));+ strscpy_literal(c[n].detail,+ "chain present, replay not evaluated");}n++;}@@ -189,15 +197,14 @@ size_t conditions(const struct conditions_input *in,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));+ 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(c[n].detail, "?", sizeof(c[n].detail));+ strscpy_literal(c[n].detail, "?");} else {- strscpy(c[n].detail, "no TPM answered",- sizeof(c[n].detail));+ strscpy_literal(c[n].detail, "no TPM answered");}n++;}@@ -208,21 +215,20 @@ size_t conditions(const struct conditions_input *in,c[n].status = "OK";if (!in->config_seen) {c[n].status = "WARN";- strscpy(c[n].detail, "no config activation state before init",- sizeof(c[n].detail));+ 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(c[n].detail, "?", sizeof(c[n].detail));+ strscpy_literal(c[n].detail, "?");} else if (config_untrusted(in)) {c[n].status = "ALERT";- strscpy(c[n].detail, config_untrusted_detail(in->config_state),- sizeof(c[n].detail));+ 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(c[n].detail, "?", sizeof(c[n].detail));+ strscpy_literal(c[n].detail, "?");}n++;}diff --git a/util/preflight.c b/util/preflight.cindex 5cb84263..085e2bb9 100644--- a/util/preflight.c+++ b/util/preflight.c@@ -71,7 +71,7 @@ static void fail(struct preflight_result *r,c->ok = false;va_start(ap, fmt);if (vstrsprintf(c->detail, sizeof(c->detail), fmt, ap) < 0)- strscpy(c->detail, "?", sizeof(c->detail));+ strscpy_literal(c->detail, "?");va_end(ap);r->n_failed++;}@@ -194,8 +194,8 @@ static void check_lsm_hooks(struct preflight_result *r)for (i = 0; i < ARRAY_SIZE(objs); i++) {if (!objs[i]->size) {pass(c);- strscpy(c->detail, "skipped (embedded objects missing)",- sizeof(c->detail));+ strscpy_literal(c->detail,+ "skipped (embedded objects missing)");return;}ret = kernel_abi_lsm_hooks(objs[i], &hooks);@@ -285,8 +285,7 @@ static void check_cert_algorithm(struct preflight_result *r)if (preflight_keys(&set)) {pass(c);- strscpy(c->detail, "skipped (cert unreadable)",- sizeof(c->detail));+ strscpy_literal(c->detail, "skipped (cert unreadable)");return;}for (i = 0; i < set.n; i++) {@@ -334,8 +333,7 @@ static void check_cert_kernel_algo(struct preflight_result *r)if (preflight_keys(&set)) {pass(c);- strscpy(c->detail, "skipped (cert unreadable)",- sizeof(c->detail));+ strscpy_literal(c->detail, "skipped (cert unreadable)");return;}for (i = 0; i < set.n; i++) {@@ -426,8 +424,8 @@ static void check_tpm(struct preflight_result *r)if (strsprintf(c->detail, sizeof(c->detail),"TCTI from the config: %s",cfg.tpm_tcti) < 0)- strscpy(c->detail, "TCTI from the config",- sizeof(c->detail));+ strscpy_literal(c->detail,+ "TCTI from the config");}if (override)return;diff --git a/util/sysinfo.c b/util/sysinfo.cindex 8a26b178..fb5e8c98 100644--- a/util/sysinfo.c+++ b/util/sysinfo.c@@ -368,14 +368,17 @@ void sysinfo_gather(struct sysinfo *si)si->hostname[sizeof(si->hostname) - 1] = '\0';if (uname(&u) == 0) {- strscpy(si->kernel_release, u.release,- sizeof(si->kernel_release));- strscpy(si->kernel_version, u.version,- sizeof(si->kernel_version));- strscpy(si->machine, u.machine, sizeof(si->machine));- if (si->hostname[0] == '\0')- strscpy(si->hostname, u.nodename,- sizeof(si->hostname));+ if (strscpy(si->kernel_release, u.release,+ sizeof(si->kernel_release)) < 0)+ si->kernel_release[0] = '\0';+ if (strscpy(si->kernel_version, u.version,+ sizeof(si->kernel_version)) < 0)+ si->kernel_version[0] = '\0';+ if (strscpy(si->machine, u.machine, sizeof(si->machine)) < 0)+ si->machine[0] = '\0';+ if (si->hostname[0] == '\0' &&+ strscpy(si->hostname, u.nodename, sizeof(si->hostname)) < 0)+ si->hostname[0] = '\0';}read_os_release_value("PRETTY_NAME", si->os_pretty);