Skip to content

eBPF-based security platform

status: write the system's text with the lossy JSON writers

Daniel Borkmann09969c6a2c06
diff --git a/Documentation/reference/formats/status-json.rst b/Documentation/reference/formats/status-json.rst
index a10e706b..819adb97 100644
--- a/Documentation/reference/formats/status-json.rst
+++ b/Documentation/reference/formats/status-json.rst
@@ -46,7 +46,7 @@ determined, as noted.
``shirudo version --version-id`` prints it.
* - ``host``
- string
- - The host name.
+ - The host name, with U+FFFD for each byte that is no UTF-8.
* - ``is_target``
- boolean
- ``true`` when the certificate is present, the LSM link is
@@ -707,7 +707,9 @@ Optional blocks
that cannot be read is ``-1`` and a text that cannot be read is
empty. A NUL byte where a value is read counts as a read error:
``kernel_config`` then ends with the line before it. A line of
- the kernel config longer than 8191 bytes is left out.
+ the kernel config longer than 8191 bytes is left out. The text
+ the kernel and these files give is written with U+FFFD for each
+ byte that is no UTF-8.
* - ``--include-eventlog``
- ``tpm.eventlog.events``: one object per line of
``/var/lib/shirudo/eventlog``, all boots, with ``ts``, ``type``
diff --git a/builtin-status.c b/builtin-status.c
index 41ed79b6..c80cf577 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -1798,7 +1798,7 @@ static void status_emit_json(struct json_emit *e, const struct status_view *v,
json_obj_open(e);
json_kv_int(e, "schema", 1);
json_kv_string(e, "version_id", shirudo_version_id);
- json_kv_string(e, "host", v->hostname);
+ json_kv_string_lossy(e, "host", v->hostname);
json_kv_bool(e, "is_target", v->is_target);
json_kv_bool(e, "is_operator", v->is_operator);
emit_verdict_json(e, &v->cin);
diff --git a/testing/unit/sysinfo.c b/testing/unit/sysinfo.c
index 0d6579a6..5b5d06e2 100644
--- a/testing/unit/sysinfo.c
+++ b/testing/unit/sysinfo.c
@@ -8,6 +8,7 @@
#include <shirudo/string.h>
#include "io.h"
+#include "json.h"
#include "sysinfo.h"
#include "unit.h"
@@ -306,6 +307,29 @@ static void command_line_with_a_nul_is_absent(void)
EXPECT_STREQ(img.root, "");
}
+/* The kernel and the files give bytes: a stray one costs no document. */
+static void system_text_that_is_no_utf8_is_written(void)
+{
+ struct sysinfo si = { .nproc = -1, .mem_total_kb = -1 };
+ char buf[4096];
+ struct json_emit e = { .buf = buf, .cap = sizeof(buf) };
+
+ strscpy_literal(si.hostname, "node\xff");
+ strscpy_literal(si.kernel_release, "7.0\xc3");
+ strscpy_literal(si.machine, "x86_64\xc0\xaf");
+ strscpy_literal(si.boot_id, "\xfe");
+ json_obj_open(&e);
+ sysinfo_emit_json(&e, &si);
+ json_obj_close(&e);
+ ASSERT_EQ(e.err, 0);
+ EXPECT_TRUE(str_contains(buf, "\"hostname\":\"node\\ufffd\""));
+ EXPECT_TRUE(str_contains(buf, "\"kernel_release\":\"7.0\\ufffd\""));
+ EXPECT_TRUE(str_contains(buf,
+ "\"architecture\":\"x86_64\\ufffd\\ufffd\""));
+ EXPECT_TRUE(str_contains(buf, "\"boot_id\":\"\\ufffd\""));
+ EXPECT_TRUE(str_contains(buf, "\"kernel_config\":{}"));
+}
+
static void memory_of_this_host_is_read(void)
{
struct sysinfo si;
@@ -335,6 +359,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(no_hash_is_absent),
UNIT_CASE(command_line_that_does_not_fit_is_absent),
UNIT_CASE(command_line_with_a_nul_is_absent),
+ UNIT_CASE(system_text_that_is_no_utf8_is_written),
UNIT_CASE(memory_of_this_host_is_read),
{}
};
diff --git a/util/sysinfo.c b/util/sysinfo.c
index 27be362f..1da6a773 100644
--- a/util/sysinfo.c
+++ b/util/sysinfo.c
@@ -356,7 +356,8 @@ int sysinfo_kernel_config_foreach(const char *kernel_release,
static void emit_kernel_config_entry(void *ctx, const char *key,
const char *value)
{
- json_kv_string(ctx, key, value);
+ json_key_lossy(ctx, key, strlen(key));
+ json_str_lossy(ctx, value, strlen(value));
}
static void emit_kernel_config(struct json_emit *e,
@@ -502,15 +503,15 @@ void sysinfo_emit_json(struct json_emit *e,
const struct sysinfo *si)
{
json_obj_open_key(e, "system");
- json_kv_string(e, "hostname", si->hostname);
- json_kv_string(e, "os", si->os_pretty);
- json_kv_string(e, "kernel_release", si->kernel_release);
- json_kv_string(e, "kernel_version", si->kernel_version);
+ json_kv_string_lossy(e, "hostname", si->hostname);
+ json_kv_string_lossy(e, "os", si->os_pretty);
+ json_kv_string_lossy(e, "kernel_release", si->kernel_release);
+ json_kv_string_lossy(e, "kernel_version", si->kernel_version);
json_kv_string(e, "kernel_build_id", si->kernel_build_id);
emit_kernel_config(e, si->kernel_release);
- json_kv_string(e, "architecture", si->machine);
- json_kv_string(e, "machine_id", si->machine_id);
- json_kv_string(e, "boot_id", si->boot_id);
+ json_kv_string_lossy(e, "architecture", si->machine);
+ json_kv_string_lossy(e, "machine_id", si->machine_id);
+ json_kv_string_lossy(e, "boot_id", si->boot_id);
json_kv_int(e, "nproc", si->nproc);
json_kv_int(e, "mem_total_kb", si->mem_total_kb);
json_kv_int(e, "mem_avail_kb", si->mem_avail_kb);