Skip to content

eBPF-based security platform

otel: remove the structured keepalive log

John Fastabendbf931e12ae6a
diff --git a/.agents/skills/fuzz-target/SKILL.md b/.agents/skills/fuzz-target/SKILL.md
index 2aab3c90..49bbc631 100644
--- a/.agents/skills/fuzz-target/SKILL.md
+++ b/.agents/skills/fuzz-target/SKILL.md
@@ -26,8 +26,8 @@ property of the parser breaks:
- what it accepts and emits again reads back the same, as `fuzz-tree.c`
holds a tree publication and its operation digests;
-- what it writes reads back as its format, as `fuzz-otel.c` holds the
- OTLP AnyValues to JSON;
+- what it writes reads back as its format, as `fuzz-quote-check.c`
+ holds the line of a verdict to JSON;
- offsets and sizes it returns lie within the input, as `fuzz-rodata.c`
holds the `.rodata` range;
- two ways to the same value agree.
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 755d71c1..53340b0b 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -547,7 +547,9 @@ signed config image.
* - ``t8003-systemd-map-export``
- Exporter units and timers and their package scriptlets, as text
* - ``t8004-status-otel``
- - The keepalive log record
+ - ``status --keepalive``, ``status --otel`` without a TPM or an
+ endpoint, and the keepalive log record: its string body, its one
+ quote attribute and a body JSON cannot carry
* - ``t8005-tls-policy``
- TLS 1.3 with ``X25519MLKEM768`` only
* - ``t8006-otel-transport``
diff --git a/testing/functional/otel/t8004-status-otel.sh b/testing/functional/otel/t8004-status-otel.sh
index 4675780c..424f0425 100644
--- a/testing/functional/otel/t8004-status-otel.sh
+++ b/testing/functional/otel/t8004-status-otel.sh
@@ -58,76 +58,6 @@ test_expect_success 'status --otel without a TPM sends nothing and fails' '
rm -rf "$work"
'
-test_expect_success 'keepalive status severities and structured body are deterministic' '
- work=$(mktemp -d) || return 1
- for status in ok degraded init-pending reboot-pending lsm-off tampered
- do
- python3 helper/otel-mock.py success "$work/port" "$work/body" \
- "$work/meta" &
- server=$!
- trap "kill $server 2>/dev/null || true; wait $server 2>/dev/null || true; rm -rf $work" EXIT
- i=0
- while test ! -s "$work/port" && test "$i" -lt 100; do
- sleep .02
- i=$((i + 1))
- done
- port=$(cat "$work/port") &&
- "$H" send-status "http://127.0.0.1:$port" "$status" &&
- wait "$server" &&
- python3 - "$status" "$work/body" <<'\''PY'\'' || return 1
-import json, socket, sys
-status = sys.argv[1]
-rl = json.load(open(sys.argv[2]))["resourceLogs"][0]
-resource = {item["key"]: item["value"] for item in rl["resource"]["attributes"]}
-assert resource == {"service.name": {"stringValue": "shirudo"},
- "service.version": {"stringValue": "test-1"},
- "host.name": {"stringValue": socket.gethostname()}}, resource
-assert rl["scopeLogs"][0]["scope"] == {"name": "shirudo.keepalive",
- "version": "test-1"}
-record = rl["scopeLogs"][0]["logRecords"][0]
-expected = {"ok": (9, "INFO"), "degraded": (13, "WARN"),
- "init-pending": (13, "WARN"), "reboot-pending": (13, "WARN"),
- "lsm-off": (17, "ERROR"),
- "tampered": (17, "ERROR")}[status]
-assert (record["severityNumber"], record["severityText"]) == expected
-assert record["eventName"] == "shirudo.keepalive"
-body = {item["key"]: item["value"] for item in record["body"]["kvlistValue"]["values"]}
-assert body["status"]["stringValue"] == status
-assert body["schema"]["intValue"] == "1"
-attrs = {item["key"]: item["value"] for item in record["attributes"]}
-assert attrs == {"shirudo.status": {"stringValue": status},
- "shirudo.version_id": {"stringValue": "test-version"},
- "shirudo.tamper_attempts": {"intValue": "2"}}, attrs
-PY
- rm -f "$work/port" "$work/body" "$work/meta" || return 1
- done
-'
-
-test_expect_success 'a status body many times larger as AnyValues is sent whole' '
- work=$(mktemp -d) || return 1
- python3 helper/otel-mock.py success "$work/port" "$work/body" \
- "$work/meta" &
- server=$!
- trap "kill $server 2>/dev/null || true; wait $server 2>/dev/null || true; rm -rf $work" EXIT
- i=0
- while test ! -s "$work/port" && test "$i" -lt 100; do
- sleep .02
- i=$((i + 1))
- done
- port=$(cat "$work/port") &&
- "$H" send-wide-status "http://127.0.0.1:$port" 100000 &&
- wait "$server" &&
- python3 - "$work/body" <<'\''PY'\'' &&
-import json, sys
-rl = json.load(open(sys.argv[1]))["resourceLogs"][0]
-record = rl["scopeLogs"][0]["logRecords"][0]
-body = {item["key"]: item["value"] for item in record["body"]["kvlistValue"]["values"]}
-values = body["conditions"]["arrayValue"]["values"]
-assert len(values) == 100000 and values[-1] == {"intValue": "1"}, len(values)
-PY
- rm -rf "$work"
-'
-
test_expect_success 'status --json --otel prints the report when the keepalive fails' '
work=$(mktemp -d) &&
sink &&
diff --git a/testing/fuzz/fuzz-otel.c b/testing/fuzz/fuzz-otel.c
index 10860d35..f1226d02 100644
--- a/testing/fuzz/fuzz-otel.c
+++ b/testing/fuzz/fuzz-otel.c
@@ -3,8 +3,7 @@
* Copyright (c) 2026 Authors of shirudo
*
* What an OTLP collector answers, the response header lines and the
- * partial-success body, the decoded map JSON a log record carries, and
- * the configured OTEL_EXPORTER_OTLP_HEADERS.
+ * partial-success body, and the configured OTEL_EXPORTER_OTLP_HEADERS.
*/
#include "fuzz.h"
@@ -62,39 +61,12 @@ int upatch_decode_map_file_otel(const char *path,
return -EOPNOTSUPP;
}
-static const struct json_visitor any_value = {};
-
-static size_t deepest_nesting(const char *s, size_t len)
-{
- size_t depth = 0, deepest = 0, i;
- bool in_string = false;
-
- for (i = 0; i < len; i++) {
- if (in_string) {
- if (s[i] == '\\')
- i++;
- else if (s[i] == '"')
- in_string = false;
- } else if (s[i] == '"') {
- in_string = true;
- } else if (s[i] == '{' || s[i] == '[') {
- if (++depth > deepest)
- deepest = depth;
- } else if ((s[i] == '}' || s[i] == ']') && depth) {
- depth--;
- }
- }
- return deepest;
-}
-
int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
{
- struct json_emit e = { .max = JSON_MAX_BYTES };
struct otel_response r = {};
struct otel_state st = {};
size_t off, n;
char *body, *line;
- int ret;
/*
* curl hands the header callback one line at a time, end included
@@ -120,26 +92,6 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
check_partial_success(OTEL_LOG, body, size);
check_partial_success(OTEL_METRIC, body, size);
- json_obj_open(&e);
- /*
- * What the conversion writes reads back as JSON, in a buffer that
- * grows for it. An input level takes up to four in the output,
- * which can so nest deeper than the walk goes; such output must read
- * back up to that depth.
- */
- if (!json_anyvalue_from_document(&e, body, size, false)) {
- json_obj_close(&e);
- if (e.err == -ENOSPC)
- abort();
- if (!e.err) {
- ret = json_walk(e.buf, e.len, &any_value, NULL);
- if (deepest_nesting(e.buf, e.len) <= JSON_MAX_DEPTH ?
- ret : ret != -E2BIG)
- abort();
- }
- }
- xfree(e.buf, e.cap);
-
if (!memchr(data, '\0', size)) {
if (!add_configured_headers(&st, body)) {
for (struct curl_slist *h = st.headers; h; h = h->next) {
diff --git a/testing/helper/test-otel-map.c b/testing/helper/test-otel-map.c
index f1835aa4..84229ce3 100644
--- a/testing/helper/test-otel-map.c
+++ b/testing/helper/test-otel-map.c
@@ -403,28 +403,6 @@ static int send_metric(const char *url)
return ret ? 1 : 0;
}
-static int send_status(const char *url, const char *status)
-{
- struct otel_opts opts = {
- .endpoint = url,
- .retries = 0,
- };
- const char *severity_text;
- char body[256];
- int severity, n;
-
- severity = status_otel_severity(status, &severity_text);
- n = snprintf(body, sizeof(body),
- "{\"schema\":1,\"status\":\"%s\",\"conditions\":[],\"tpm\":{}}",
- status);
- if (n < 0 || (size_t)n >= sizeof(body))
- return 1;
- return otel_send_log_json(&opts, "shirudo.keepalive",
- "shirudo.keepalive", severity,
- severity_text, status, "test-version", 2,
- body, (size_t)n) != 0;
-}
-
static int send_keepalive(const char *url, const char *verdict,
const char *body_path, const char *quote)
{
@@ -448,42 +426,6 @@ static int send_keepalive(const char *url, const char *verdict,
return ret != 0;
}
-/*
- * A status body of a list of count ones, which the AnyValue form spells
- * in some eight times the bytes.
- */
-static int send_wide_status(const char *url, const char *count)
-{
- static const char head[] =
- "{\"schema\":1,\"status\":\"ok\",\"conditions\":[";
- struct otel_opts opts = {
- .endpoint = url,
- .retries = 0,
- };
- size_t n = strtoul(count, NULL, 10), cap, len, i;
- char *body;
- int ret;
-
- cap = sizeof(head) + 2 * n + 2;
- body = xmalloc(cap);
- if (!body)
- return 1;
- memcpy(body, head, sizeof(head) - 1);
- len = sizeof(head) - 1;
- for (i = 0; i < n; i++) {
- if (i)
- body[len++] = ',';
- body[len++] = '1';
- }
- body[len++] = ']';
- body[len++] = '}';
- ret = otel_send_log_json(&opts, "shirudo.keepalive",
- "shirudo.keepalive", 9, "INFO", "ok",
- "test-version", 0, body, len);
- xfree(body, cap);
- return ret != 0;
-}
-
static int send_metric_env(void)
{
struct otel_opts opts = {
@@ -724,12 +666,8 @@ int main(int argc, char **argv)
0, 0, true);
if (argc == 2 && strcmp(argv[1], "send-metric-env") == 0)
return send_metric_env();
- if (argc == 4 && strcmp(argv[1], "send-status") == 0)
- return send_status(argv[2], argv[3]);
if (argc == 6 && strcmp(argv[1], "send-keepalive") == 0)
return send_keepalive(argv[2], argv[3], argv[4], argv[5]);
- if (argc == 4 && strcmp(argv[1], "send-wide-status") == 0)
- return send_wide_status(argv[2], argv[3]);
if (argc == 4 && strcmp(argv[1], "retry-after") == 0) {
__u64 seconds;
time_t now = (time_t)strtoll(argv[3], NULL, 10);
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 8fc98efd..59799df1 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -283,156 +283,6 @@ static void otlp_array_kvlist_end(struct json_emit *e)
json_obj_close(e);
}
-struct anyvalue_walk {
- struct json_emit *e;
- __u32 depth;
- bool wrap;
- /* Whether the container at each depth is an array. */
- bool array[JSON_MAX_DEPTH + 1];
-};
-
-/*
- * A value in an array is an AnyValue object of its own; one in an object
- * goes into the {"key","value"} the key opened; one at the top is the
- * caller's AnyValue, or with wrap an object of its own.
- */
-static void anyvalue_begin(struct anyvalue_walk *w)
-{
- if (w->depth ? w->array[w->depth] : w->wrap)
- json_obj_open(w->e);
-}
-
-static void anyvalue_end(struct anyvalue_walk *w)
-{
- if (!w->depth) {
- if (w->wrap)
- json_obj_close(w->e);
- } else if (w->array[w->depth]) {
- json_obj_close(w->e);
- } else {
- json_obj_close(w->e);
- json_obj_close(w->e);
- }
-}
-
-static int anyvalue_open(void *ctx, bool array)
-{
- struct anyvalue_walk *w = ctx;
-
- if (!w->depth && !w->wrap && array)
- return -EINVAL;
- anyvalue_begin(w);
- json_obj_open_key(w->e, array ? "arrayValue" : "kvlistValue");
- json_arr_open(w->e, "values");
- w->array[++w->depth] = array;
- return w->e->err;
-}
-
-static int anyvalue_close(void *ctx, bool array)
-{
- struct anyvalue_walk *w = ctx;
-
- json_arr_close(w->e);
- json_obj_close(w->e);
- w->depth--;
- anyvalue_end(w);
- return w->e->err;
-}
-
-static int anyvalue_key(void *ctx, const char *key)
-{
- struct anyvalue_walk *w = ctx;
-
- json_obj_open(w->e);
- json_kv_string(w->e, "key", key);
- json_obj_open_key(w->e, "value");
- return w->e->err;
-}
-
-static int anyvalue_scalar(struct anyvalue_walk *w)
-{
- return !w->depth && !w->wrap ? -EINVAL : 0;
-}
-
-static int anyvalue_string(void *ctx, const char *s)
-{
- struct anyvalue_walk *w = ctx;
- int ret = anyvalue_scalar(w);
-
- if (ret)
- return ret;
- anyvalue_begin(w);
- json_kv_string(w->e, "stringValue", s);
- anyvalue_end(w);
- return w->e->err;
-}
-
-/*
- * An AnyValue's intValue is a 64-bit integer: a longer one, or one with a
- * fraction or an exponent, goes as a doubleValue as it is written.
- */
-static int anyvalue_number(void *ctx, const char *text, size_t len,
- bool integer)
-{
- struct anyvalue_walk *w = ctx;
- int ret = anyvalue_scalar(w);
- __s64 value;
-
- if (ret)
- return ret;
- anyvalue_begin(w);
- if (integer && !parse_s64(text, len, INT64_MIN, INT64_MAX, &value)) {
- char num[24];
-
- if (strsprintf(num, sizeof(num), "%lld", value) < 0)
- return -E2BIG;
- json_kv_string(w->e, "intValue", num);
- } else {
- json_kv_raw(w->e, "doubleValue", text, len);
- }
- anyvalue_end(w);
- return w->e->err;
-}
-
-static int anyvalue_literal(void *ctx, int value)
-{
- struct anyvalue_walk *w = ctx;
- int ret = anyvalue_scalar(w);
-
- if (ret)
- return ret;
- anyvalue_begin(w);
- /* An empty AnyValue is the OTLP JSON representation of null. */
- if (value >= 0)
- json_kv_bool(w->e, "boolValue", value);
- anyvalue_end(w);
- return w->e->err;
-}
-
-static const struct json_visitor anyvalue_visitor = {
- .open = anyvalue_open,
- .close = anyvalue_close,
- .key = anyvalue_key,
- .string = anyvalue_string,
- .number = anyvalue_number,
- .literal = anyvalue_literal,
-};
-
-/*
- * One ordinary JSON value as the OTLP JSON representation of an
- * AnyValue: with wrap any value as an AnyValue object of its own,
- * without it an object as the kvlistValue of the AnyValue the caller has
- * open. Status already has a stable JSON emitter, so that status and its
- * machine-readable output share one source of truth.
- */
-static int json_anyvalue_from_document(struct json_emit *e,
- const char *json, size_t len, bool wrap)
-{
- struct anyvalue_walk w = { .e = e, .wrap = wrap };
-
- return json_walk(json, len, &anyvalue_visitor, &w);
-}
-
static void emit_resource_attributes(struct json_emit *e)
{
char hostname[256] = "unknown";
@@ -513,20 +363,6 @@ static void log_close(struct json_emit *e)
otlp_close(e);
}
-static int log_payload_take(struct json_emit *e, char **out,
- size_t *out_len, size_t *alloc_len)
-{
- log_close(e);
- if (e->err) {
- xfree(e->buf, e->cap);
- return e->err;
- }
- *out = e->buf;
- *out_len = e->len;
- *alloc_len = e->cap;
- return 0;
-}
-
static void emit_map_attributes(struct json_emit *e,
const struct map_view *v)
{
@@ -1564,113 +1400,10 @@ static int otel_close(void *state)
return 0;
}
-static int build_generic_log_payload(const char *scope_name,
- const char *event_name,
- int severity_number,
- const char *severity_text,
- const char *status, const char *version_id,
- __s64 tamper_attempts,
- const char *body_json, size_t body_len,
- char **out, size_t *out_len,
- size_t *alloc_len)
-{
- struct json_emit e = { .max = JSON_MAX_BYTES };
- int ret;
-
- if (!scope_name || !event_name || !severity_text || !body_json ||
- !out || !out_len || !alloc_len)
- return -EINVAL;
- log_open(&e, scope_name, severity_number, severity_text, event_name);
- json_obj_open_key(&e, "body");
- ret = json_anyvalue_from_document(&e, body_json, body_len, false);
- json_obj_close(&e);
- if (ret) {
- xfree(e.buf, e.cap);
- return ret;
- }
- json_arr_open(&e, "attributes");
- if (status)
- otlp_kv_string(&e, "shirudo.status", status);
- if (version_id)
- otlp_kv_string(&e, "shirudo.version_id", version_id);
- if (tamper_attempts >= 0)
- otlp_kv_int(&e, "shirudo.tamper_attempts", tamper_attempts);
- json_arr_close(&e);
- return log_payload_take(&e, out, out_len, alloc_len);
-}
-
/*
* The body goes out as a string, so that no collector can re-encode it
- * and the bytes a signature covers arrive as they were signed. JSON
- * cannot carry a NUL or malformed UTF-8 unchanged; both are refused.
+ * and the bytes a signature covers arrive as they were signed.
*/
-static int build_string_log_payload(const char *scope_name,
- const char *event_name,
- int severity_number,
- const char *severity_text,
- const char *body, size_t body_len,
- const char *attr_key,
- const char *attr_value,
- char **out, size_t *out_len,
- size_t *alloc_len)
-{
- struct json_emit e = { .max = JSON_MAX_BYTES };
-
- if (!scope_name || !event_name || !severity_text || !body ||
- !attr_key || !attr_value || !out || !out_len || !alloc_len)
- return -EINVAL;
- log_open(&e, scope_name, severity_number, severity_text, event_name);
- json_obj_open_key(&e, "body");
- json_kv_string_n(&e, "stringValue", body, body_len);
- json_obj_close(&e);
- json_arr_open(&e, "attributes");
- otlp_kv_string(&e, attr_key, attr_value);
- json_arr_close(&e);
- return log_payload_take(&e, out, out_len, alloc_len);
-}
-
-static int otel_log_post(const struct otel_opts *opts, const char *payload,
- size_t payload_len)
-{
- void *state = NULL;
- int ret;
-
- ret = otel_open_transport(OTEL_LOG, opts ? opts->endpoint : NULL,
- opts, NULL, &state);
- if (ret)
- return ret;
- ret = otel_post(state, payload, payload_len);
- otel_close(state);
- return ret;
-}
-
-int otel_send_log_json(const struct otel_opts *opts,
- const char *scope_name,
- const char *event_name,
- int severity_number,
- const char *severity_text,
- const char *status,
- const char *version_id,
- __s64 tamper_attempts,
- const char *body_json, size_t body_len)
-{
- size_t payload_len = 0, alloc_len = 0;
- char *payload = NULL;
- int ret;
-
- ret = build_generic_log_payload(scope_name, event_name,
- severity_number, severity_text,
- status, version_id,
- tamper_attempts,
- body_json, body_len,
- &payload, &payload_len, &alloc_len);
- if (ret)
- return ret;
- ret = otel_log_post(opts, payload, payload_len);
- xfree(payload, alloc_len);
- return ret;
-}
-
int otel_send_log_string(const struct otel_opts *opts,
const char *scope_name,
const char *event_name,
@@ -1679,18 +1412,27 @@ int otel_send_log_string(const struct otel_opts *opts,
const char *body, size_t body_len,
const char *attr_key, const char *attr_value)
{
- size_t payload_len = 0, alloc_len = 0;
- char *payload = NULL;
+ struct json_emit e = { .max = JSON_MAX_BYTES };
+ void *state = NULL;
int ret;
- ret = build_string_log_payload(scope_name, event_name,
- severity_number, severity_text,
- body, body_len, attr_key, attr_value,
- &payload, &payload_len, &alloc_len);
- if (ret)
- return ret;
- ret = otel_log_post(opts, payload, payload_len);
- xfree(payload, alloc_len);
+ log_open(&e, scope_name, severity_number, severity_text, event_name);
+ json_obj_open_key(&e, "body");
+ json_kv_string_n(&e, "stringValue", body, body_len);
+ json_obj_close(&e);
+ json_arr_open(&e, "attributes");
+ otlp_kv_string(&e, attr_key, attr_value);
+ json_arr_close(&e);
+ log_close(&e);
+ ret = e.err;
+ if (!ret)
+ ret = otel_open_transport(OTEL_LOG, opts ? opts->endpoint : NULL,
+ opts, NULL, &state);
+ if (!ret) {
+ ret = otel_post(state, e.buf, e.len);
+ otel_close(state);
+ }
+ xfree(e.buf, e.cap);
return ret;
}
diff --git a/util/upatch_maps_otel.h b/util/upatch_maps_otel.h
index 22f57410..3f80a1f4 100644
--- a/util/upatch_maps_otel.h
+++ b/util/upatch_maps_otel.h
@@ -57,20 +57,6 @@ int upatch_maps_otel_send_decoded(const struct map_view *v,
enum otel_signal signal,
const struct otel_opts *opts);
-/*
- * Send an already-built JSON object as the structured body of one OTLP
- * log. A tamper_attempts of 0 or more rides along as an attribute; a
- * negative one leaves it out.
- */
-int otel_send_log_json(const struct otel_opts *opts,
- const char *scope_name,
- const char *event_name,
- int severity_number,
- const char *severity_text,
- const char *status,
- const char *version_id,
- __s64 tamper_attempts,
- const char *body_json, size_t body_len);
/*
* Send body_len bytes as the string body of one OTLP log with a single
* string attribute. The receiver decodes the very bytes passed in; a