Skip to content

eBPF-based security platform

string: replace terminal controls in shown text in one place

Daniel Borkmann09e9d4e54ebc
diff --git a/builtin-keys.c b/builtin-keys.c
index 0ff4cb76..9efe3d5f 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -357,8 +357,11 @@ static bool keys_anchor_holds(const struct keys_anchor *a, key_serial_t key)
/* Kernel descriptions carry certificate subjects: keep the terminal safe. */
static void print_safe(const char *s)
{
- for (; *s; s++)
- putchar((__u8)*s < 0x20 || *s == 0x7f ? '?' : *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,
diff --git a/builtin-status.c b/builtin-status.c
index 5d80f76a..f4c48ab3 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -755,8 +755,11 @@ static void keyring_emit_json(struct json_emit *e, const char *key,
/* Kernel descriptions carry certificate subjects: keep the terminal safe. */
static void print_safe(const char *s)
{
- for (; *s; s++)
- putchar((__u8)*s < 0x20 || *s == 0x7f ? '?' : *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,
diff --git a/include/shirudo/string.h b/include/shirudo/string.h
index 6a5e4b81..8fa7194e 100644
--- a/include/shirudo/string.h
+++ b/include/shirudo/string.h
@@ -2,9 +2,9 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * Bounded number parsing, string copies and formatting, DNS names and
- * UTF-8 characters, from util/string.c, and the scrub and compare for
- * secrets.
+ * Bounded number parsing, string copies and formatting, DNS names,
+ * UTF-8 characters and text for a terminal, from util/string.c, and the
+ * scrub and compare for secrets.
*/
#ifndef SHIRUDO_STRING_H
@@ -68,6 +68,15 @@ bool dns_name_valid(const char *name);
*/
size_t utf8_width(const char *s, size_t n);
+/*
+ * Make the len bytes at s, text from outside, safe to print on a
+ * terminal: each byte of a control character, C0, DEL or C1 alike, and
+ * each byte that starts no well-formed UTF-8 character becomes '?',
+ * but for a tab and a line feed where lines is set, and there a carriage
+ * return before a line feed. The length stays as it was.
+ */
+void text_sanitize(char *s, size_t len, bool lines);
+
/*
* Zero the count bytes at s even when nothing reads them afterwards, as
* when a secret is scrubbed before it goes out of scope or is freed,
diff --git a/testing/unit/string.c b/testing/unit/string.c
index 45e1e1ba..ac552e4d 100644
--- a/testing/unit/string.c
+++ b/testing/unit/string.c
@@ -391,6 +391,61 @@ static void utf8_width_takes_the_scalar_values_alone(void)
EXPECT_EQ(four, 0x100000);
}
+static bool sanitized(const char *text, size_t len, bool lines,
+ const char *want)
+{
+ char *buf = span(text, len);
+ bool same;
+
+ text_sanitize(buf, len, lines);
+ same = !memcmp(buf, want, len);
+ xfree(buf, len ? len : 1);
+ return same;
+}
+
+static void text_sanitize_replaces_controls_and_stray_bytes(void)
+{
+ EXPECT_TRUE(sanitized("", 0, false, ""));
+ EXPECT_TRUE(sanitized("plain ~text", 11, false, "plain ~text"));
+ EXPECT_TRUE(sanitized("\x1b[31mred", 8, false, "?[31mred"));
+ EXPECT_TRUE(sanitized("a\0b\x7f", 4, false, "a?b?"));
+ EXPECT_TRUE(sanitized("t\tl\n", 4, false, "t?l?"));
+ EXPECT_TRUE(sanitized("t\tl\n", 4, true, "t\tl\n"));
+ EXPECT_TRUE(sanitized("a\r\nb\rc\r", 7, true, "a\r\nb?c?"));
+ EXPECT_TRUE(sanitized("\xc2\x9b\x33\x31m", 5, true, "??31m"));
+ EXPECT_TRUE(sanitized("\xc2\x80\xc2\x9f\xc2\xa0", 6, false,
+ "????\xc2\xa0"));
+ EXPECT_TRUE(sanitized("caf\xc3\xa9 \xe2\x82\xac \xf0\x9f\x90\x9d",
+ 14, false,
+ "caf\xc3\xa9 \xe2\x82\xac \xf0\x9f\x90\x9d"));
+ EXPECT_TRUE(sanitized("\xff\xc3z\xe2\x82", 5, false, "??z??"));
+ EXPECT_TRUE(sanitized("\xed\xa0\x80", 3, false, "???"));
+}
+
+/* Every scalar value but the controls passes; a control leaves only '?'. */
+static void text_sanitize_passes_all_but_the_controls(void)
+{
+ __u32 cp, wrong = 0;
+ char buf[4];
+
+ for (cp = 0; cp <= 0x10ffff; cp++) {
+ bool control = cp < 0x20 || (cp >= 0x7f && cp <= 0x9f);
+ size_t len, i, marks = 0;
+ char copy[4];
+
+ if (cp >= 0xd800 && cp <= 0xdfff)
+ continue;
+ len = utf8_encode(cp, buf);
+ memcpy(copy, buf, len);
+ text_sanitize(buf, len, false);
+ for (i = 0; i < len; i++)
+ marks += buf[i] == '?';
+ if (control ? marks != len : memcmp(buf, copy, len))
+ wrong++;
+ }
+ EXPECT_EQ(wrong, 0);
+}
+
static void memzero_explicit_clears_its_span_only(void)
{
__u8 want[16], buf[16];
@@ -441,6 +496,8 @@ static const struct unit_case cases[] = {
UNIT_CASE(dns_name_is_ldh_labels_and_no_address),
UNIT_CASE(utf8_width_takes_well_formed_characters_only),
UNIT_CASE(utf8_width_takes_the_scalar_values_alone),
+ UNIT_CASE(text_sanitize_replaces_controls_and_stray_bytes),
+ UNIT_CASE(text_sanitize_passes_all_but_the_controls),
UNIT_CASE(memzero_explicit_clears_its_span_only),
UNIT_CASE(memneq_explicit_tells_every_bit_apart),
{}
diff --git a/util/string.c b/util/string.c
index cd2749a3..77df0812 100644
--- a/util/string.c
+++ b/util/string.c
@@ -205,6 +205,32 @@ size_t utf8_width(const char *s, size_t n)
return width;
}
+void text_sanitize(char *s, size_t len, bool lines)
+{
+ size_t i, n;
+
+ for (i = 0; i < len; i += n) {
+ __u8 c = s[i];
+ bool shown;
+
+ n = utf8_width(s + i, len - i);
+ if (c == '\t' || c == '\n')
+ shown = lines;
+ else if (c == '\r')
+ shown = lines && i + 1 < len && s[i + 1] == '\n';
+ else if (n == 1)
+ shown = c >= 0x20 && c != 0x7f;
+ else if (n == 2)
+ shown = c != 0xc2 || (__u8)s[i + 1] >= 0xa0;
+ else
+ shown = n != 0;
+ if (!n)
+ n = 1;
+ if (!shown)
+ memset(s + i, '?', n);
+ }
+}
+
int vstrsprintf(char *buf, size_t size, const char *fmt, va_list ap)
{
int n = vsnprintf(buf, size, fmt, ap);