eBPF-based security platform
conditions: return the status reason rather than copy it
Daniel Borkmann08f0979abe92
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 801ba74f..e39c4048 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -1194,11 +1194,11 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.one read; each listing printed and the run done* - ``conditions-verdict``- ``status_verdict()``, ``status_reason()``- - Every input; reasons into 1, 8, 32 and 160 bytes+ - Every input- The first verdict of its order that applies; ``tampered`` exactlywhen a cause holds on a host that answered, whatever the otherfields say; the reason of the first cause in the order of the- reasons, terminated and cut to the buffer+ reasons* - ``conditions-rows``- ``conditions()``, ``keyring_condition()``, ``conditions_print()``- Every input ``status`` can build: status codes of 0 or a negateddiff --git a/builtin-status.c b/builtin-status.cindex a9e0a8d1..60c86acf 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -1537,11 +1537,11 @@ static void emit_verdict_json(struct json_emit *e,const struct conditions_input *cin){struct condition conds[CONDITIONS_MAX];- char reason[160];+ const char *reason;size_t nc, ci;json_kv_string(e, "status", status_verdict(cin));- status_reason(cin, reason, sizeof(reason));+ reason = status_reason(cin);if (reason[0])json_kv_string(e, "reason", reason);else@@ -2031,11 +2031,10 @@ static void status_print_text(const struct status_view *v,const char *folded[3];size_t n_rows = 0;size_t nc, nf = 0;- const char *verdict;- char reason[160];+ const char *verdict, *reason;verdict = status_verdict(&v->cin);- status_reason(&v->cin, reason, sizeof(reason));+ reason = status_reason(&v->cin);nc = conditions(&v->cin, conds, ARRAY_SIZE(conds));n_rows = build_upatches_table(v, &rows);/*diff --git a/testing/helper/test-conditions.c b/testing/helper/test-conditions.cindex a7d2a9f4..9df2fa9e 100644--- a/testing/helper/test-conditions.c+++ b/testing/helper/test-conditions.c@@ -49,7 +49,6 @@ int main(int argc, char **argv)struct conditions_input in = {};__s64 lsm, serial, pinned;const char *mode, *config, *pins;- char reason[160];size_t n, i;if (argc != 6 || arg_s64(argv[1], &lsm) || arg_s64(argv[3], &serial) ||@@ -108,9 +107,8 @@ int main(int argc, char **argv)in.pins_unmeasured += *pins == ',';}- status_reason(&in, reason, sizeof(reason));printf("status=%s\n", status_verdict(&in));- printf("reason=%s\n", reason);+ printf("reason=%s\n", status_reason(&in));n = conditions(&in, conds, ARRAY_SIZE(conds));for (i = 0; i < n; i++)printf("%s %s %s\n", conds[i].type, conds[i].status,diff --git a/testing/proofs/cbmc/conditions-verdict.c b/testing/proofs/cbmc/conditions-verdict.cindex 61ae83e0..465e91ac 100644--- a/testing/proofs/cbmc/conditions-verdict.c+++ b/testing/proofs/cbmc/conditions-verdict.c@@ -5,13 +5,11 @@* status_verdict() and status_reason() of util/conditions.c on every input:* the verdict is the first of its order that applies, tampered exactly when* a cause holds, whatever else the input says, and the reason is that of- * the first cause in the documented order, cut to a buffer of LEN bytes.+ * the first cause in the documented order.*//* util/string.c is included behind a file that includes its header. */#define STRING_INTERNAL-/* A reason is copied from the literal the input picks. */-#define PROOF_MEMCPY_BYTEWISE#include "proof.h"#include "../../../util/conditions.c"@@ -164,20 +162,11 @@ static const char *reason_of(const struct conditions_input *in, enum verdict v)}}-/* The text, or as much of it as size bytes hold with their NUL. */-static bool cut_to(const char *buf, const char *text, size_t size)-{- size_t n = min(strlen(text), size - 1);-- return !memcmp(buf, text, n) && !buf[n];-}-void harness(void){struct conditions_input in;const char *verdict;enum verdict v = 0;- char *reason;bool said;if (said) {@@ -197,8 +186,6 @@ void harness(void)(in.reachable && in.parsed &&first_cause(&in) != NR_CAUSES),"tampered exactly when the host answered and a cause holds");- reason = any_bytes(LEN);- status_reason(&in, reason, LEN);- __CPROVER_assert(cut_to(reason, reason_of(&in, v), LEN),+ __CPROVER_assert(!strcmp(status_reason(&in), reason_of(&in, v)),"the reason of the verdict and of the first cause");}diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 6fb5e4a9..67416a9a 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -144,7 +144,7 @@ bpm-beefile-fixes --unwind 5 --unwindset strchr.0:67 LEN=0-2bpm-index-writer --unwind 26 --unwindset strlen.0:64,strnlen.0:64,memcpy.0:64,put_string_n.1:64 --max-field-sensitivity-array-size 512 LEN=0-3bpm-index-append --unwind 26 --unwindset strlen.0:260,strcmp.0:64,strnlen.0:64,memcpy.0:64,put_string_n.1:64 --max-field-sensitivity-array-size 512 LEN=0-2bpm-tree-writer --unwind 80 --max-field-sensitivity-array-size 512 LEN=0-4-conditions-verdict --unwind 66 LEN=1,8,32,160+conditions-verdict --unwind 66conditions-rows --object-bits 12 --max-field-sensitivity-array-size 192 --unwind 1 --unwindset vsnprintf.0:3,vsnprintf.1:67,memcpy.0:65,strnlen.0:65,strcmp.0:18,spells.0:65,harness.0:8,harness.1:8,conditions_print.0:8 LEN=0-8conditions-detail --object-bits 12 --unwind 195 --unwindset vsnprintf.2:836 LEN=193,1024bpm-state-entry --unwind 1 --unwindset vsnprintf.1:31,vsnprintf.2:7,strcmp.0:42,memcmp.0:32,strlen.0:130,bpm_component_ok.0:130 LEN=0,1diff --git a/testing/unit/conditions.c b/testing/unit/conditions.cindex 41808494..082560b1 100644--- a/testing/unit/conditions.c+++ b/testing/unit/conditions.c@@ -91,15 +91,14 @@ static void records_the_register_does_not_vouch_for_are_tampered(void)};struct condition c[CONDITIONS_MAX];const struct condition *row;- char reason[128];row = upatches_measured(&in, c);ASSERT_TRUE(row);EXPECT_STREQ(row->status, "ALERT");EXPECT_TRUE(str_contains(row->detail, "do not fold to the measurement register"));EXPECT_STREQ(status_verdict(&in), "tampered");- status_reason(&in, reason, sizeof(reason));- EXPECT_TRUE(str_contains(reason, "do not fold to the measurement register"));+ EXPECT_TRUE(str_contains(status_reason(&in),+ "do not fold to the measurement register"));}static void a_check_that_could_not_run_warns(void)@@ -149,15 +148,13 @@ static void an_uncovered_device_degrades(void)};struct condition c[CONDITIONS_MAX];const struct condition *row;- char reason[128];row = condition_of(&in, c, "XdpCovered");ASSERT_TRUE(row);EXPECT_STREQ(row->status, "WARN");EXPECT_TRUE(str_contains(row->detail, "fw on eth1 (no multi-buffer XDP)"));EXPECT_STREQ(status_verdict(&in), "degraded");- status_reason(&in, reason, sizeof(reason));- EXPECT_TRUE(str_contains(reason, "XDP upatch selects"));+ EXPECT_TRUE(str_contains(status_reason(&in), "XDP upatch selects"));}static void unchecked_coverage_warns_without_degrading(void)diff --git a/util/conditions.c b/util/conditions.cindex 767f6d0b..2d61e3ed 100644--- a/util/conditions.c+++ b/util/conditions.c@@ -280,64 +280,53 @@ const char *status_verdict(const struct conditions_input *in)return "ok";}-void status_reason(const struct conditions_input *in,- char *buf, size_t cap)+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);- buf[0] = '\0';- if (strcmp(v, "unreachable") == 0) {- if (in->unreach)- strscpy(buf, in->unreach, cap);- } else if (strcmp(v, "unparseable") == 0) {- strscpy(buf, "status JSON unparseable", cap);- } else if (strcmp(v, "tampered") == 0) {- if (in->cm_seen && !in->chain_matches)- strscpy(buf, "live register differs from the eventlog replay", cap);- else if (in->xdp_tampered)- strscpy(buf, "the archive of a loaded XDP upatch differs from what was loaded",- cap);- else if (in->links_lost)- strscpy(buf, "a loaded upatch is missing a link or its archive",- cap);- else if (in->pins_unmeasured)- strscpy(buf, "a upatch is pinned or archived without a record of this boot",- cap);- else if (in->pins_rc == -EBADMSG)- strscpy(buf, "this boot's records do not fold to the measurement register",- cap);- else if (config_untrusted(in))- strscpy(buf, "the selected config cannot be verified against its records",- cap);- else if (in->ksp_seen && !in->keyring_present)- strscpy(buf, "the keyring init froze is gone", cap);- else if (in->ksm_seen && !in->keyring_serial_matches)- strscpy(buf, "keyring serial swap detected", cap);- else if (in->dmvp_seen && !in->dmv_present)- strscpy(buf, "the dm-verity keyring init froze is gone",- cap);- else if (in->dmvm_seen && !in->dmv_serial_matches)- strscpy(buf, "dm-verity keyring serial swap detected",- cap);- else if (in->bpf_unfrozen || in->dmv_unfrozen)- strscpy(buf, "a keyring holds a link init did not freeze",- cap);- else- strscpy(buf, "the config key init froze is gone", cap);- } else if (strcmp(v, "lsm-off") == 0) {- strscpy(buf, "LSM not attached, enforcement off", cap);- } else if (strcmp(v, "init-pending") == 0) {- strscpy(buf, ".bpf keyring absent before init", cap);- } else if (strcmp(v, "reboot-pending") == 0) {- strscpy(buf, "a config activation is pending until the next reboot",- cap);- } else if (strcmp(v, "degraded") == 0) {- if (in->chain_seen)- strscpy(buf, "a device an XDP upatch selects does not run it",- cap);- else- strscpy(buf, in->tpm_reachable ?- "no register bound this boot" : "no TPM answered",- cap);- }+ 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 "";}diff --git a/util/conditions.h b/util/conditions.hindex c361b200..f8041b2e 100644--- a/util/conditions.h+++ b/util/conditions.h@@ -85,7 +85,10 @@ void conditions_print(const struct condition *c, size_t n);*/const char *status_verdict(const struct conditions_input *in);-void status_reason(const struct conditions_input *in,- char *buf, size_t cap);+/*+ * The reason for that verdict, in a sentence: a literal, or the unreach+ * of in, which lives as long as in does; "" when the host is ok.+ */+const char *status_reason(const struct conditions_input *in);#endif /* SHIRUDO_CONDITIONS_H */