Skip to content

eBPF-based security platform

conditions: cut a long detail rather than drop it

John Fastabend81c31634a615
diff --git a/Documentation/reference/formats/status-json.rst b/Documentation/reference/formats/status-json.rst
index beaedf45..39034e4b 100644
--- a/Documentation/reference/formats/status-json.rst
+++ b/Documentation/reference/formats/status-json.rst
@@ -226,7 +226,9 @@ Conditions
is ``OK``, ``WARN`` or ``ALERT``. ``<serial>``, ``<n>`` and ``<root>``
stand for a keyring serial, a number of links and the first 12 hex
digits of the config root hash. ``KeyringSealed`` reports the first of
-its details that applies, in the order listed.
+its details that applies, in the order listed. A detail is at most 191
+bytes: a longer one, such as a long list of names, is cut and ends with
+``...``.
.. list-table::
:header-rows: 1
diff --git a/testing/unit/conditions.c b/testing/unit/conditions.c
index ee628ce8..4f79257b 100644
--- a/testing/unit/conditions.c
+++ b/testing/unit/conditions.c
@@ -175,6 +175,33 @@ static void covered_devices_are_ok(void)
EXPECT_STREQ(status_verdict(&in), "ok");
}
+static void a_long_list_is_cut_not_dropped(void)
+{
+ char names[256];
+ struct conditions_input in = {
+ .reachable = true,
+ .parsed = true,
+ .lsm_attached = 1,
+ .keyring_present = 1,
+ .chain_seen = 1,
+ .xdp_uncovered = 20,
+ .xdp_uncovered_detail = names,
+ };
+ struct condition c[CONDITIONS_MAX];
+ const struct condition *row;
+ size_t len;
+
+ memset(names, 'x', sizeof(names) - 1);
+ names[sizeof(names) - 1] = '\0';
+ memcpy(names, "fw on eth1", strlen("fw on eth1"));
+ row = condition_of(&in, c, "XdpCovered");
+ ASSERT_TRUE(row);
+ len = strlen(row->detail);
+ EXPECT_EQ(len, sizeof(row->detail) - 1);
+ EXPECT_TRUE(strstr(row->detail, "fw on eth1"));
+ EXPECT_STREQ(row->detail + len - 3, "...");
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(changed_xdp_archive_is_tampered),
UNIT_CASE(a_changed_archive_outranks_a_pin_warning),
@@ -184,6 +211,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(an_uncovered_device_degrades),
UNIT_CASE(unchecked_coverage_warns_without_degrading),
UNIT_CASE(covered_devices_are_ok),
+ UNIT_CASE(a_long_list_is_cut_not_dropped),
{}
};
diff --git a/util/conditions.c b/util/conditions.c
index 68a722f1..243513bf 100644
--- a/util/conditions.c
+++ b/util/conditions.c
@@ -39,17 +39,23 @@ static bool keyring_tampered(const struct conditions_input *in)
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);
- if (vstrsprintf(c->detail, sizeof(c->detail), fmt, ap) < 0)
- strscpy(c->detail, "?", sizeof(c->detail));
+ ret = vstrsprintf(full, sizeof(full), fmt, ap);
va_end(ap);
+ if (ret < 0)
+ strscpy(c->detail, "?", sizeof(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,