Skip to content

eBPF-based security platform

upatch: let upatch info --json grow and take any bytes a file names

Daniel Borkmann62817f400510
diff --git a/Documentation/reference/cli/upatch-info.rst b/Documentation/reference/cli/upatch-info.rst
index 6b0c9b8d..a9882b94 100644
--- a/Documentation/reference/cli/upatch-info.rst
+++ b/Documentation/reference/cli/upatch-info.rst
@@ -43,7 +43,8 @@ The human output has these parts:
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 ``?``.
+UTF-8 character, shown as ``?``. ``--json`` writes them as JSON
+strings, with such a byte as U+FFFD.
:doc:`/reference/formats/upatch-format` describes each field.
diff --git a/builtin-upatch.c b/builtin-upatch.c
index f11f4ae9..5ae50208 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -285,6 +285,14 @@ static char *printable(char *s)
return s;
}
+/* What printable() shows, as JSON escapes it and U+FFFD for a stray byte. */
+static void json_kv_unverified(struct json_emit *e, const char *key,
+ const char *val)
+{
+ json_key(e, key);
+ json_str_lossy(e, val, strlen(val));
+}
+
static int cmd_upatch_info(int argc, const char **argv)
{
char name[UPATCH_NAME_MAX + 1]; /* not NUL-term on disk */
@@ -331,20 +339,14 @@ static int cmd_upatch_info(int argc, const char **argv)
unload_payload = upatch_signed_unload_payload(&u);
if (json) {
- size_t cap = 8192 + (size_t)u.nr_decoders * 768;
- struct json_emit e = {};
+ struct json_emit e = { .max = JSON_MAX_BYTES };
char payload_hex[2 * 32 + 1];
- char *buf = xmalloc(cap);
- if (!buf)
- goto out;
- e.buf = buf;
- e.cap = cap;
hex_encode(u.hdr.payload_sha256, 32, payload_hex);
json_obj_open(&e);
json_kv_int(&e, "schema", 1);
- json_kv_string(&e, "path", path);
- json_kv_string(&e, "name", name);
+ json_kv_unverified(&e, "path", path);
+ json_kv_unverified(&e, "name", name);
json_kv_string(&e, "type",
upatch_is_unload(&u) ? "unload"
: "load");
@@ -361,7 +363,7 @@ static int cmd_upatch_info(int argc, const char **argv)
json_kv_int(&e, "file_size", (__s64)u.map_size);
if (upatch_signed_name(&u)) {
memcpy(name, upatch_signed_name(&u), sizeof(u.hdr.name));
- json_kv_string(&e, "signed_name", name);
+ json_kv_unverified(&e, "signed_name", name);
} else {
json_kv_null(&e, "signed_name");
}
@@ -386,10 +388,10 @@ static int cmd_upatch_info(int argc, const char **argv)
decoder_input_str(d.input_format));
json_kv_string(&e, "export_signal",
decoder_signal_str(d.export_signal));
- json_kv_string(&e, "upatch", d.upatch);
- json_kv_string(&e, "map", d.map);
- json_kv_string(&e, "source_file", d.source_name);
- json_kv_string(&e, "entrypoint", d.entrypoint);
+ json_kv_unverified(&e, "upatch", d.upatch);
+ json_kv_unverified(&e, "map", d.map);
+ json_kv_unverified(&e, "source_file", d.source_name);
+ json_kv_unverified(&e, "entrypoint", d.entrypoint);
json_kv_int(&e, "source_len", d.source_len);
json_obj_close(&e);
}
@@ -399,8 +401,8 @@ static int cmd_upatch_info(int argc, const char **argv)
fprintf(stderr, "upatch info: json encode failed: %s\n",
strerror(-e.err));
else
- printf("%s\n", buf);
- xfree(buf, cap);
+ printf("%s\n", e.buf);
+ xfree(e.buf, e.cap);
ret = e.err ? 1 : 0;
goto out;
}
diff --git a/testing/functional/upatch/t3001-upatch-parse.sh b/testing/functional/upatch/t3001-upatch-parse.sh
index f3efe58c..e33f742f 100644
--- a/testing/functional/upatch/t3001-upatch-parse.sh
+++ b/testing/functional/upatch/t3001-upatch-parse.sh
@@ -98,6 +98,21 @@ test_expect_success 'a name from the file shows its controls as ?' '
grep -q "^Name: *a?\[2Jb??c?\$" "$WORK/info.out" &&
! grep -q "$(printf "\033")" "$WORK/info.out"
'
+test_expect_success 'upatch info --json writes a stray byte of a name as U+FFFD' '
+ "$S" upatch info --json "$WORK/controls-in-name" >"$WORK/info.json" &&
+ python3 - "$WORK/info.json" <<"PY"
+import json, sys
+name = json.load(open(sys.argv[1]))["name"]
+assert name == "a\x1b[2Jb\x9bc\ufffd", repr(name)
+PY
+'
+test_expect_success 'upatch info --json grows to hold a long escaped path' '
+ dir=$WORK && seg=$(printf "\001%.0s" $(seq 200)) &&
+ for i in $(seq 15); do dir=$dir/$seg; done &&
+ mkdir -p "$dir" && cp "$WORK/valid-structured" "$dir/u" &&
+ "$S" upatch info --json "$dir/u" >"$WORK/long.json" &&
+ [ "$(wc -c <"$WORK/long.json")" -gt 16384 ]
+'
test_expect_success 'file larger than 16 MiB is rejected without mapping it' '
reject "$WORK/oversized"
'
diff --git a/util/upatch_rodata.c b/util/upatch_rodata.c
index 62cf7fca..2631dc4c 100644
--- a/util/upatch_rodata.c
+++ b/util/upatch_rodata.c
@@ -995,22 +995,19 @@ static int print_rodata_json(const struct upatch *u,
view->type_id);
const struct btf_var_secinfo *vars =
(const struct btf_var_secinfo *)(sec + 1);
- char name[UPATCH_NAME_MAX + 1];
- char *buf = xmalloc(JSON_MAX_BYTES);
- struct json_emit e = {};
+ struct json_emit e = { .max = JSON_MAX_BYTES };
__u32 i, vlen = BTF_INFO_VLEN(sec->info);
+ char name[UPATCH_NAME_MAX + 1];
int ret = 0;
- if (!buf)
- return -ENOMEM;
- e.buf = buf;
- e.cap = JSON_MAX_BYTES;
memcpy(name, u->hdr.name, UPATCH_NAME_MAX);
name[UPATCH_NAME_MAX] = '\0';
json_obj_open(&e);
json_kv_int(&e, "schema", 1);
- json_kv_string(&e, "upatch", name);
- json_kv_string(&e, "path", path);
+ json_key(&e, "upatch");
+ json_str_lossy(&e, name, strlen(name));
+ json_key(&e, "path");
+ json_str_lossy(&e, path, strlen(path));
json_kv_string(&e, "section", ".rodata");
json_kv_int(&e, "size", view->size);
json_kv_int(&e, "data_offset", view->data_offset);
@@ -1058,12 +1055,12 @@ static int print_rodata_json(const struct upatch *u,
if (!ret && e.err)
ret = e.err;
if (!ret)
- ret = write_complete(STDOUT_FILENO, buf, e.len);
+ ret = write_complete(STDOUT_FILENO, e.buf, e.len);
if (!ret)
ret = write_complete(STDOUT_FILENO, "\n", 1);
- if (ret == -ENOSPC)
+ if (e.err == -E2BIG)
pr_err("BTF JSON output exceeds %u bytes", JSON_MAX_BYTES);
- xfree(buf, JSON_MAX_BYTES);
+ xfree(e.buf, e.cap);
return ret;
}