eBPF-based security platform
bpm, upatch: print unverified text without terminal controls
Daniel Borkmann4bab93e26e26
diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rstindex 0a67ac65..36882392 100644--- a/Documentation/reference/cli/bpm.rst+++ b/Documentation/reference/cli/bpm.rst@@ -258,6 +258,10 @@ pull: File exists``, so remove or rename the old one first.its ``upatches/*.upatch`` members, with ``--all`` also the``.unload.upatch`` directives. ``info data`` decodes the initial``.rodata`` of one member through its BTF types, with ``--json`` as JSON.+None of them checks the package signature, so each shows a control+character, and a byte that starts no UTF-8 character, as ``?``: in the+``README.md`` all but tab, line feed and a carriage return before a line+feed, in a member or upatch name all of them.push----diff --git a/Documentation/reference/cli/upatch-info.rst b/Documentation/reference/cli/upatch-info.rstindex 55c90449..6b0c9b8d 100644--- a/Documentation/reference/cli/upatch-info.rst+++ b/Documentation/reference/cli/upatch-info.rst@@ -41,6 +41,10 @@ The human output has these parts:- the decoders: target upatch and map, language and API version, inputformat, export signal (``log`` or ``metric``) and source+As no signature is checked, the file, the names and the decoder fields+are printed with each control character, and each byte that starts no+UTF-8 character, shown as ``?``.+:doc:`/reference/formats/upatch-format` describes each field.Optionsdiff --git a/builtin-bpm.c b/builtin-bpm.cindex 546d2a2a..d3021e4a 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -554,6 +554,7 @@ static int print_package_readme(const char *source)pr_err("bpm info: package %s does not include README.md", source);goto out;}+ text_sanitize((char *)readme, readme_len, true);ret = write_complete(STDOUT_FILENO, readme, readme_len);out:xfree(beefile, beefile_len + 1);@@ -610,6 +611,7 @@ static int print_upatch_member(void *ctx, const char *member, char type)opts->cap = cap;}memcpy(opts->list + opts->len, member, len);+ text_sanitize(opts->list + opts->len, len, false);opts->list[opts->len + len] = '\n';opts->len = need;return 0;diff --git a/builtin-upatch.c b/builtin-upatch.cindex d0cadaae..f11f4ae9 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -275,10 +275,21 @@ static const struct help upatch_get_help = {" shirudo upatch get cve-2026-31431 --json | jq",};+/*+ * upatch info checks no signature, so the names it prints are as the+ * file has them.+ */+static char *printable(char *s)+{+ text_sanitize(s, strlen(s), false);+ return s;+}+static int cmd_upatch_info(int argc, const char **argv){char name[UPATCH_NAME_MAX + 1]; /* not NUL-term on disk */const __u8 *unload_payload;+ char file[PATH_MAX];struct upatch u;const char *path = NULL;bool json = false;@@ -394,8 +405,9 @@ static int cmd_upatch_info(int argc, const char **argv)goto out;}- printf("File: %s\n", path);- printf("Name: %s\n", name);+ strscpy(file, path, sizeof(file));+ printf("File: %s\n", printable(file));+ printf("Name: %s\n", printable(name));printf("Type: %s\n", flag_str(&u));printf("Payload type: %s\n", upatch_type_str(&u));printf("Version: %u\n", u.hdr.version);@@ -426,7 +438,7 @@ static int cmd_upatch_info(int argc, const char **argv)printf("\nSigned sections:\n");if (upatch_signed_name(&u)) {memcpy(name, upatch_signed_name(&u), sizeof(u.hdr.name));- printf(" %-22s %s\n", "Name:", name);+ printf(" %-22s %s\n", "Name:", printable(name));} else {printf(" %-22s (none)\n", "Name:");}@@ -451,7 +463,8 @@ static int cmd_upatch_info(int argc, const char **argv)if (upatch_decoder_view(&u, j, &d))continue;- printf(" %-22s %s/%s\n", "Target:", d.upatch, d.map);+ printf(" %-22s %s/%s\n", "Target:",+ printable(d.upatch), printable(d.map));printf(" %-20s %s (api=%u)\n", "Language:",decoder_language_str(d.language), d.api_version);printf(" %-20s %s\n", "Input:",@@ -459,7 +472,8 @@ static int cmd_upatch_info(int argc, const char **argv)printf(" %-20s %s\n", "Export signal:",decoder_signal_str(d.export_signal));printf(" %-20s %s:%s (%u bytes)\n", "Source:",- d.source_name, d.entrypoint, d.source_len);+ printable(d.source_name),+ printable(d.entrypoint), d.source_len);}}ret = 0;diff --git a/testing/functional/upatch/t3001-upatch-parse.sh b/testing/functional/upatch/t3001-upatch-parse.shindex 7b8c86a5..f3efe58c 100644--- a/testing/functional/upatch/t3001-upatch-parse.sh+++ b/testing/functional/upatch/t3001-upatch-parse.sh@@ -79,6 +79,9 @@ data = bytearray(header)struct.pack_into("<II", data, 320, 359, 1)(root / "metadata-in-header").write_bytes(data)mutate("data-len-overflow", 332, "<I", 0xffffffff)+data = bytearray(header)+data[16:32] = b"a\x1b[2Jb\xc2\x9bc\xff".ljust(16, bytes([0]))+(root / "controls-in-name").write_bytes(data)with (root / "oversized").open("wb") as f:f.truncate(16 * 1024 * 1024 + 1)PY@@ -90,6 +93,11 @@ test_expect_success 'structured header and section bounds fail cleanly' 'loader-in-header signature-oob data-overflow metadata-in-header \data-len-overflow; do reject "$WORK/$f" || exit 1; done'+test_expect_success 'a name from the file shows its controls as ?' '+ "$S" upatch info "$WORK/controls-in-name" >"$WORK/info.out" 2>&1 &&+ grep -q "^Name: *a?\[2Jb??c?\$" "$WORK/info.out" &&+ ! grep -q "$(printf "\033")" "$WORK/info.out"+'test_expect_success 'file larger than 16 MiB is rejected without mapping it' 'reject "$WORK/oversized"'diff --git a/testing/helper/test-bpm-info.py b/testing/helper/test-bpm-info.pyindex 4c25e457..2dcf9669 100644--- a/testing/helper/test-bpm-info.py+++ b/testing/helper/test-bpm-info.py@@ -68,6 +68,24 @@ class InfoTests(unittest.TestCase):self.assertEqual(result.stdout, README)self.assertEqual(list(self.root.iterdir()), [self.root / "package.bee"])+ def test_readme_controls_show_as_question_marks(self):+ readme = (b"# T\x1b]0;x\x07itle\r\nline\rover\n"+ b"\xc2\x9b2J caf\xc3\xa9 \xff\n")+ result = self.local(package(readme))+ self.assertEqual(result.returncode, 0, result.stderr)+ self.assertEqual(result.stdout,+ b"# T?]0;x?itle\r\nline?over\n??2J caf\xc3\xa9 ?\n")++ def test_member_name_controls_show_as_question_marks(self):+ path = self.root / "package.bee"+ path.write_bytes(package_bytes([+ ("example-1.0/Beefile", b'[package]\nname = "example"\n'),+ ("example-1.0/upatches/a\x1b[2Jb.upatch", b"x"),+ ]))+ result = self.run_info("upatches", str(path))+ self.assertEqual(result.returncode, 0, result.stderr)+ self.assertEqual(result.stdout, b"upatches/a?[2Jb.upatch\n")+def test_empty_readme(self):result = self.local(package(b""))self.assertEqual(result.returncode, 0, result.stderr)diff --git a/util/upatch_rodata.c b/util/upatch_rodata.cindex 78f2b3b4..d8f6e8e2 100644--- a/util/upatch_rodata.c+++ b/util/upatch_rodata.c@@ -855,6 +855,7 @@ static int print_rodata_text(const struct upatch *u,memcpy(name, u->hdr.name, UPATCH_NAME_MAX);name[UPATCH_NAME_MAX] = '\0';+ text_sanitize(name, strlen(name), false);printf("upatch %s (%s) .rodata: %u bytes at data+%u, file+%u\n",name, path, view->size, view->data_offset,u->hdr.data_off + view->data_offset);