eBPF-based security platform
format: print outside text for a terminal in one place
Daniel Borkmann485a2b98404b
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex db88cf3b..f94e4422 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -491,11 +491,11 @@ signed config image.Unit suites and fuzz targets~~~~~~~~~~~~~~~~~~~~~~~~~~~~-The unit suites in :src:`testing/unit/` cover fifteen of the modules in+The unit suites in :src:`testing/unit/` cover sixteen of the modules in:src:`util/`: ``bpm_oci``, ``bpm_package``, ``bpm_tree``,-``bpm_tree_state``, ``chain``, ``eventlog``, ``hex``, ``io``, ``json``,-``kernel_abi``, ``kernel_build_id``, ``kernel_release``, ``string``,-``upatch`` and ``upatch_decode``, and ``overflow`` the arithmetic of+``bpm_tree_state``, ``chain``, ``eventlog``, ``format``, ``hex``, ``io``,+``json``, ``kernel_abi``, ``kernel_build_id``, ``kernel_release``,+``string``, ``upatch`` and ``upatch_decode``, and ``overflow`` the arithmetic of:src:`include/shirudo/overflow.h`. The other modules are reached throughthe test helpers, the fuzz targets and the binaries the functional testsrun.diff --git a/builtin-keys.c b/builtin-keys.cindex 9efe3d5f..bc91e822 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -32,6 +32,7 @@#include "util/chain.h"#include "util/crypto.h"#include "util/eventlog.h"+#include "util/format.h"#include "util/help.h"#include "util/hex.h"#include "util/identity.h"@@ -354,16 +355,6 @@ static bool keys_anchor_holds(const struct keys_anchor *a, key_serial_t key)return false;}-/* Kernel descriptions carry certificate subjects: keep the terminal safe. */-static void print_safe(const char *s)-{- char shown[KEYRING_DESC_MAX];-- strscpy(shown, s, sizeof(shown));- text_sanitize(shown, strlen(shown), false);- fputs(shown, stdout);-}-static int keys_list_keyring(key_serial_t ring, const struct keys_anchor *a,int id_rc){@@ -443,7 +434,7 @@ static int keys_list_keyring(key_serial_t ring, const struct keys_anchor *a,if (l->err)printf("(cannot describe: %s)", strerror(-l->err));else- print_safe(l->description);+ fputs_sanitized(l->description, stdout);if (frozen)printf(" (%s)", keys_anchor_holds(a, l->serial) ?"frozen" : "not frozen");diff --git a/builtin-status.c b/builtin-status.cindex 511dc0d6..16f2f102 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -727,16 +727,6 @@ static void keyring_emit_json(struct json_emit *e, const char *key,json_obj_close(e);}-/* Kernel descriptions carry certificate subjects: keep the terminal safe. */-static void print_safe(const char *s)-{- char shown[KEYRING_DESC_MAX];-- strscpy(shown, s, sizeof(shown));- text_sanitize(shown, strlen(shown), false);- fputs(shown, stdout);-}-static void print_link(const struct status_keyring *k, size_t i,const char *indent){@@ -749,7 +739,7 @@ static void print_link(const struct status_keyring *k, size_t i,printf("%s%s %d ", indent,strcmp(l->type, "asymmetric") ? l->type : "key",(int)l->serial);- print_safe(l->description);+ fputs_sanitized(l->description, stdout);}printf(" (%s)\n", k->state[i].frozen ? "frozen" : "not frozen");}diff --git a/testing/unit/format.c b/testing/unit/format.cnew file mode 100644index 00000000..8055776c--- /dev/null+++ b/testing/unit/format.c@@ -0,0 +1,60 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>+#include <stdio.h>+#include <string.h>++#include "format.h"+#include "unit.h"++static void shown(const char *s, char *out, size_t cap)+{+ FILE *f = fmemopen(out, cap, "w");++ ASSERT_TRUE(f);+ fputs_sanitized(s, f);+ ASSERT_EQ(fclose(f), 0);+}++static void fputs_sanitized_keeps_a_terminal_safe(void)+{+ char out[1024];++ shown("CN=shirudo, O=Example", out, sizeof(out));+ EXPECT_STREQ(out, "CN=shirudo, O=Example");+ shown("CN=\x1b]0;owned\x07\n", out, sizeof(out));+ EXPECT_STREQ(out, "CN=?]0;owned??");+ shown("CN=caf\xc3\xa9 \xff", out, sizeof(out));+ EXPECT_STREQ(out, "CN=caf\xc3\xa9 ?");+}++static void fputs_sanitized_cuts_after_511_bytes(void)+{+ char in[600], out[1024];++ memset(in, 'a', sizeof(in) - 1);+ in[sizeof(in) - 1] = '\0';+ shown(in, out, sizeof(out));+ EXPECT_EQ(strlen(out), 511);+}++static void human_size_picks_the_unit(void)+{+ char out[16];++ EXPECT_EQ(human_size(512, out, sizeof(out)), 0);+ EXPECT_STREQ(out, "512B");+ EXPECT_EQ(human_size(1536, out, sizeof(out)), 0);+ EXPECT_STREQ(out, "1.50K");+ EXPECT_EQ(human_size(1536, out, 4), -E2BIG);+}++static const struct unit_case cases[] = {+ UNIT_CASE(fputs_sanitized_keeps_a_terminal_safe),+ UNIT_CASE(fputs_sanitized_cuts_after_511_bytes),+ UNIT_CASE(human_size_picks_the_unit),+ {}+};++UNIT_SUITE(format, cases);diff --git a/util/format.c b/util/format.cindex d3e42cbc..d98e35f1 100644--- a/util/format.c+++ b/util/format.c@@ -40,3 +40,15 @@ int human_size(__u64 bytes, char *out, size_t out_cap)n = strsprintf(out, out_cap, "%.2f%s", v, suf);return n < 0 ? -E2BIG : 0;}++void fputs_sanitized(const char *s, FILE *f)+{+ char shown[512];+ ssize_t n;++ n = strscpy(shown, s, sizeof(shown));+ if (n < 0)+ n = sizeof(shown) - 1;+ text_sanitize(shown, n, false);+ fputs(shown, f);+}diff --git a/util/format.h b/util/format.hindex 0ef5d29e..310c15b9 100644--- a/util/format.h+++ b/util/format.h@@ -9,6 +9,7 @@#define SHIRUDO_UTIL_FORMAT_H#include <stddef.h>+#include <stdio.h>#include <shirudo/misc.h>@@ -19,4 +20,11 @@*/int human_size(__u64 bytes, char *out, size_t out_cap);+/*+ * Text from outside, such as the certificate subject in a key's+ * description, on f as text_sanitize() leaves it, on one line and cut+ * after 511 bytes.+ */+void fputs_sanitized(const char *s, FILE *f);+#endif /* SHIRUDO_UTIL_FORMAT_H */