Skip to content

eBPF-based security platform

status: print the host name and the system's text as text

Daniel Borkmann154cd98047e8
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 97f18759..67e15365 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -462,6 +462,10 @@ signed config image.
* - ``t6007-output-paths``
- Output paths: no symbolic links, directories or devices; a failed
write removes only the file written, where it was opened
+ * - ``t6008-status-terminal``
+ - A host name that holds an escape sequence reaches the terminal of
+ ``status`` as ``?`` for each control byte, and the JSON report
+ escaped
* - ``t6101-mode-objects``
- Device, memory and config-root guards are pinned
* - ``t6102-siggate``
diff --git a/builtin-status.c b/builtin-status.c
index 67e25482..facc4e7e 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -330,17 +330,31 @@ static void print_counters(int rc, __u64 total, const __u64 *count,
}
}
+/*
+ * A line whose value the system wrote, not shirudo: the host name is
+ * whatever bytes root gave sethostname(), and the rest comes from files
+ * and from the kernel's build. None of it may reach a terminal as
+ * control characters, which could rewrite the lines around it.
+ */
+static void print_system_text(const char *indent, const char *label,
+ const char *text)
+{
+ printf("%s%-22s ", indent, label);
+ fputs_sanitized(text, stdout);
+ putchar('\n');
+}
+
static void print_system_human(const struct sysinfo *si)
{
printf("\nSystem:\n");
- printf(" %-22s %s\n", "OS:", si->os_pretty);
- printf(" %-22s %s\n", "Kernel:", si->kernel_release);
+ print_system_text(" ", "OS:", si->os_pretty);
+ print_system_text(" ", "Kernel:", si->kernel_release);
if (si->kernel_version[0])
- printf(" %-22s %s\n", "Kernel version:", si->kernel_version);
+ print_system_text(" ", "Kernel version:", si->kernel_version);
if (si->kernel_build_id[0])
printf(" %-22s %s\n", "Kernel build ID:",
si->kernel_build_id);
- printf(" %-22s %s\n", "Architecture:", si->machine);
+ print_system_text(" ", "Architecture:", si->machine);
if (si->nproc > 0)
printf(" %-22s %ld\n", "CPUs:", si->nproc);
if (si->mem_total_kb > 0) {
@@ -373,9 +387,9 @@ static void print_system_human(const struct sysinfo *si)
(si->uptime_sec % 86400) / 3600,
(si->uptime_sec % 3600) / 60);
if (si->machine_id[0])
- printf(" %-22s %s\n", "Machine ID:", si->machine_id);
+ print_system_text(" ", "Machine ID:", si->machine_id);
if (si->boot_id[0])
- printf(" %-22s %s\n", "Boot ID:", si->boot_id);
+ print_system_text(" ", "Boot ID:", si->boot_id);
}
enum status_opt {
@@ -1891,7 +1905,7 @@ static void unfold_sections(const struct condition *conds, size_t nc,
static void print_summary(const struct status_view *v, const char *verdict,
const char *reason)
{
- printf("%-22s %s\n", "Host:", v->hostname);
+ print_system_text("", "Host:", v->hostname);
printf("%-22s %s\n", "Roles:",
roles_str(v->is_target, v->is_operator));
if (reason[0])
diff --git a/testing/functional/tamperproof/t6008-status-terminal.sh b/testing/functional/tamperproof/t6008-status-terminal.sh
new file mode 100755
index 00000000..87ac59dd
--- /dev/null
+++ b/testing/functional/tamperproof/t6008-status-terminal.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t6008 -- status prints the host name as text: sethostname() takes any
+# bytes from root, and a name that held an escape sequence reached the
+# terminal as one, where it can erase the line it stands on and write
+# another in its place. Each control byte is shown as "?" instead, and
+# the JSON report escapes it.
+
+cd "$(dirname "$0")/../.."
+. ./test-lib.sh
+
+SHIRUDO=${SHIRUDO:-../shirudo}
+for c in python3 unshare; do
+ command -v $c >/dev/null 2>&1 || test_skip_all "no $c"
+done
+if unshare --uts true 2>/dev/null; then
+ UNSHARE="unshare --uts"
+elif unshare --user --map-root-user --uts true 2>/dev/null; then
+ UNSHARE="unshare --user --map-root-user --uts"
+else
+ test_skip_all "cannot unshare a UTS namespace"
+fi
+W=$(mktemp -d)
+trap 'rm -rf "$W"' EXIT
+export SHIRUDO_EVENTLOG_PATH="$W/eventlog"
+
+# status under the host name "node", the erase-line sequence, a carriage
+# return and a line of the report's own making.
+status_as_spoofed_host() {
+ $UNSHARE python3 -c '
+import os, socket, sys
+socket.sethostname(b"node\x1b[2K\rStatus: ok")
+os.execv(sys.argv[1], sys.argv[1:])' "$SHIRUDO" status "$@"
+}
+
+test_expect_success 'no control byte of the host name reaches the terminal' '
+ status_as_spoofed_host >"$W/out" 2>/dev/null
+ head -n 1 "$W/out" >"$W/host" &&
+ ! LC_ALL=C grep -q "[[:cntrl:]]" "$W/host" &&
+ grep -qx "Host: *node?\[2K?Status: *ok" "$W/host"
+'
+
+test_expect_success 'the JSON report escapes the same bytes' '
+ status_as_spoofed_host --json >"$W/json" 2>/dev/null
+ ! LC_ALL=C grep -q "$(printf "\033")" "$W/json" &&
+ grep -q "\"host\":\"node\\\\u001b\[2K\\\\rStatus: *ok\"" "$W/json"
+'
+
+test_done