Skip to content

eBPF-based security platform

json: emit well-formed UTF-8 only

Daniel Borkmann33b2fbcfc7c3
diff --git a/testing/unit/json.c b/testing/unit/json.c
index 6b8e2542..e647d0b0 100644
--- a/testing/unit/json.c
+++ b/testing/unit/json.c
@@ -271,6 +271,33 @@ static void discarded_number_follows_the_grammar(void)
}
}
+static int emit_string(const char *val, char *buf, size_t cap)
+{
+ struct json_emit e = { .buf = buf, .cap = cap };
+
+ json_obj_open(&e);
+ json_kv_string(&e, "v", val);
+ json_obj_close(&e);
+ return e.err;
+}
+
+static void emitter_takes_well_formed_utf8_only(void)
+{
+ static const char *const bad[] = {
+ "\xc0\xaf", "\xc0\x80", "\xe0\x80\xaf", "\xed\xa0\x80",
+ "\xf4\x90\x80\x80", "\xf5\x80\x80\x80", "\xff", "a\xc3",
+ "\xe2\x82z",
+ };
+ char buf[64];
+ size_t i;
+
+ ASSERT_EQ(emit_string("caf\xc3\xa9 \xf0\x9f\x90\x9d", buf,
+ sizeof(buf)), 0);
+ EXPECT_STREQ(buf, "{\"v\":\"caf\xc3\xa9 \xf0\x9f\x90\x9d\"}");
+ for (i = 0; i < ARRAY_SIZE(bad); i++)
+ EXPECT_EQ(emit_string(bad[i], buf, sizeof(buf)), -EILSEQ);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(extracts_scalars_and_skips_nested_sets),
UNIT_CASE(skips_unknown_keys_and_objects),
@@ -288,6 +315,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(integer_takes_json_integers_only),
UNIT_CASE(integer_holds_any_s64_and_no_more),
UNIT_CASE(discarded_number_follows_the_grammar),
+ UNIT_CASE(emitter_takes_well_formed_utf8_only),
{}
};
diff --git a/util/json.c b/util/json.c
index 4a6af4d9..595b206e 100644
--- a/util/json.c
+++ b/util/json.c
@@ -73,27 +73,15 @@ static void put_string(struct json_emit *e, const char *s)
put(e, (char)c);
p++;
} else {
- int n;
-
- if ((c & 0xE0) == 0xC0) {
- n = 2;
- } else if ((c & 0xF0) == 0xE0) {
- n = 3;
- } else if ((c & 0xF8) == 0xF0) {
- n = 4;
- } else {
+ size_t n = utf8_width((const char *)p,
+ strnlen((const char *)p, 4));
+
+ if (!n) {
e->err = -EILSEQ;
return;
}
- for (int i = 0; i < n; i++) {
- if (i > 0 && (p[i] & 0xC0) != 0x80) {
- e->err = -EILSEQ;
- return;
- }
- }
- for (int i = 0; i < n; i++)
- put(e, (char)p[i]);
- p += n;
+ while (n--)
+ put(e, (char)*p++);
}
}
put(e, '"');