Skip to content

eBPF-based security platform

json: add json_kv_string_lossy() for a member of text from outside

Daniel Borkmann675c940b069e
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 1808e829..30cd34df 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -446,14 +446,6 @@ 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 */
@@ -510,8 +502,8 @@ static int cmd_upatch_info(int argc, const char **argv)
hex_encode(u.hdr.payload_sha256, 32, payload_hex);
json_obj_open(&e);
json_kv_int(&e, "schema", 1);
- json_kv_unverified(&e, "path", path);
- json_kv_unverified(&e, "name", name);
+ json_kv_string_lossy(&e, "path", path);
+ json_kv_string_lossy(&e, "name", name);
json_kv_string(&e, "type",
upatch_is_unload(&u) ? "unload"
: "load");
@@ -528,7 +520,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_unverified(&e, "signed_name", name);
+ json_kv_string_lossy(&e, "signed_name", name);
} else {
json_kv_null(&e, "signed_name");
}
@@ -554,10 +546,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_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_string_lossy(&e, "upatch", d.upatch);
+ json_kv_string_lossy(&e, "map", d.map);
+ json_kv_string_lossy(&e, "source_file", d.source_name);
+ json_kv_string_lossy(&e, "entrypoint", d.entrypoint);
json_kv_int(&e, "source_len", d.source_len);
json_obj_close(&e);
}
diff --git a/testing/unit/json.c b/testing/unit/json.c
index 1fc5ea78..d1c744cf 100644
--- a/testing/unit/json.c
+++ b/testing/unit/json.c
@@ -557,6 +557,25 @@ static void lossy_writes_replace_bad_bytes(void)
"\"a\\ufffdb\\ufffdc\\ufffd\\ufffd\xc3\xa9\"}");
}
+static void lossy_member_has_a_strict_key(void)
+{
+ char buf[64];
+ struct json_emit e = { .buf = buf, .cap = sizeof(buf) };
+
+ json_obj_open(&e);
+ json_kv_string_lossy(&e, "release", "7.0\xff");
+ json_kv_string_lossy(&e, "b", "");
+ json_obj_close(&e);
+ ASSERT_EQ(e.err, 0);
+ EXPECT_STREQ(buf, "{\"release\":\"7.0\\ufffd\",\"b\":\"\"}");
+
+ e = (struct json_emit){ .buf = buf, .cap = sizeof(buf) };
+ json_obj_open(&e);
+ json_kv_string_lossy(&e, "k\xff", "v");
+ json_obj_close(&e);
+ EXPECT_EQ(e.err, -EILSEQ);
+}
+
struct trace {
char buf[512];
size_t len;
@@ -848,6 +867,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(emitter_writes_any_u64),
UNIT_CASE(emitter_grows_up_to_its_max),
UNIT_CASE(lossy_writes_replace_bad_bytes),
+ UNIT_CASE(lossy_member_has_a_strict_key),
UNIT_CASE(walk_reports_every_token),
UNIT_CASE(walk_follows_ecma_404),
UNIT_CASE(map_takes_any_key),
diff --git a/util/json.c b/util/json.c
index 56ba7fa5..b267c6c0 100644
--- a/util/json.c
+++ b/util/json.c
@@ -342,6 +342,13 @@ void json_key_lossy(struct json_emit *e, const char *s, size_t n)
e->value_pending = true;
}
+void json_kv_string_lossy(struct json_emit *e, const char *key,
+ const char *val)
+{
+ json_key(e, key);
+ json_str_lossy(e, val, strlen(val));
+}
+
struct pctx {
const char *p;
const char *end;
diff --git a/util/json.h b/util/json.h
index be55825f..11ef8af2 100644
--- a/util/json.h
+++ b/util/json.h
@@ -89,6 +89,9 @@ void json_raw(struct json_emit *e, const char *value,
*/
void json_str_lossy(struct json_emit *e, const char *s, size_t n);
void json_key_lossy(struct json_emit *e, const char *s, size_t n);
+/* A key shirudo names, written strictly, with such a value. */
+void json_kv_string_lossy(struct json_emit *e, const char *key,
+ const char *val);
/*
* Not a generic deserialiser: declared keys are dispatched, everything
diff --git a/util/report.c b/util/report.c
index 1db2b997..4adcc7ed 100644
--- a/util/report.c
+++ b/util/report.c
@@ -23,18 +23,6 @@
#include "report.h"
#include "seal.h"
-/*
- * The path is as the caller spelled it and the name as the file has it,
- * before anything checked either: a byte that is no UTF-8 must not cost
- * the report of the refusal it is part of.
- */
-static void kv_unverified(struct json_emit *e, const char *key,
- const char *val)
-{
- json_key(e, key);
- json_str_lossy(e, val, strlen(val));
-}
-
int report_emit(const char *path, const struct op_report *r)
{
struct seal_output out;
@@ -78,8 +66,13 @@ int report_emit(const char *path, const struct op_report *r)
json_kv_int(&e, "loader_return", r->loader_return);
json_obj_open_key(&e, "upatch");
- kv_unverified(&e, "path", r->path ? r->path : "");
- kv_unverified(&e, "name", r->name ? r->name : "");
+ /*
+ * The path is as the caller spelled it and the name as the file has
+ * it, before anything checked either: a byte that is no UTF-8 must
+ * not cost the report of the refusal it is part of.
+ */
+ json_kv_string_lossy(&e, "path", r->path ? r->path : "");
+ json_kv_string_lossy(&e, "name", r->name ? r->name : "");
json_kv_string(&e, "payload_sha256", r->payload_hex);
json_obj_close(&e);