Skip to content

eBPF-based security platform

json: grow the emitter's buffer and write strings lossily

Daniel Borkmann0b38fc1387b1
diff --git a/testing/unit/json.c b/testing/unit/json.c
index 2e2b8495..32be5aa0 100644
--- a/testing/unit/json.c
+++ b/testing/unit/json.c
@@ -7,6 +7,7 @@
#include <shirudo/string.h>
#include "json.h"
+#include "malloc.h"
#include "unit.h"
/* What an eventlog-shaped schema extracts from one document. */
@@ -452,6 +453,52 @@ static void emitter_takes_well_formed_utf8_only(void)
EXPECT_EQ(emit_string(bad[i], buf, sizeof(buf)), -EILSEQ);
}
+static void emitter_grows_up_to_its_max(void)
+{
+ struct json_emit deep = { .max = 1u << 20 };
+ struct json_emit big = { .max = 1u << 20 };
+ struct json_emit small = { .max = 64 };
+ char text[6000];
+ int i;
+
+ memset(text, 'x', sizeof(text) - 1);
+ text[sizeof(text) - 1] = '\0';
+ json_obj_open(&big);
+ json_kv_string(&big, "v", text);
+ json_obj_close(&big);
+ ASSERT_EQ(big.err, 0);
+ EXPECT_EQ(big.len, sizeof(text) - 1 + 8);
+ EXPECT_EQ(big.buf[big.len], '\0');
+ xfree(big.buf, big.cap);
+
+ json_obj_open(&small);
+ json_kv_string(&small, "v", text);
+ json_obj_close(&small);
+ EXPECT_EQ(small.err, -E2BIG);
+ xfree(small.buf, small.cap);
+
+ for (i = 0; i < 150; i++)
+ json_arr_open_value(&deep);
+ for (i = 0; i < 150; i++)
+ json_arr_close(&deep);
+ EXPECT_EQ(deep.err, 0);
+ xfree(deep.buf, deep.cap);
+}
+
+static void lossy_writes_replace_bad_bytes(void)
+{
+ char buf[128];
+ struct json_emit e = { .buf = buf, .cap = sizeof(buf) };
+
+ json_obj_open(&e);
+ json_key_lossy(&e, "k\xc3", 2);
+ json_str_lossy(&e, "a\xff\x62\0c\xc0\xaf\xc3\xa9", 9);
+ json_obj_close(&e);
+ ASSERT_EQ(e.err, 0);
+ EXPECT_STREQ(buf, "{\"k\\ufffd\":"
+ "\"a\\ufffdb\\ufffdc\\ufffd\\ufffd\xc3\xa9\"}");
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(extracts_scalars_and_skips_nested_sets),
UNIT_CASE(skips_unknown_keys_and_objects),
@@ -474,6 +521,8 @@ static const struct unit_case cases[] = {
UNIT_CASE(discarded_number_follows_the_grammar),
UNIT_CASE(malformed_utf8_is_refused),
UNIT_CASE(emitter_takes_well_formed_utf8_only),
+ UNIT_CASE(emitter_grows_up_to_its_max),
+ UNIT_CASE(lossy_writes_replace_bad_bytes),
{}
};
diff --git a/util/json.c b/util/json.c
index e07d9408..9b21e4a2 100644
--- a/util/json.c
+++ b/util/json.c
@@ -12,19 +12,39 @@
#include <string.h>
#include <shirudo/misc.h>
+#include <shirudo/overflow.h>
#include <shirudo/string.h>
#include "hex.h"
#include "json.h"
+#include "malloc.h"
+
+static bool grow(struct json_emit *e)
+{
+ size_t cap = e->cap ? size_mul(e->cap, 2) : 4096;
+ char *buf;
+
+ if (!e->max || e->cap >= e->max) {
+ e->err = e->max ? -E2BIG : -ENOSPC;
+ return false;
+ }
+ cap = min(cap, e->max);
+ buf = xreallocarray(e->buf, e->cap, cap, 1);
+ if (!buf) {
+ e->err = -ENOMEM;
+ return false;
+ }
+ e->buf = buf;
+ e->cap = cap;
+ return true;
+}
static void put(struct json_emit *e, char c)
{
if (e->err)
return;
- if (e->len + 1 >= e->cap) {
- e->err = -ENOSPC;
+ if (e->len + 1 >= e->cap && !grow(e))
return;
- }
e->buf[e->len++] = c;
e->buf[e->len] = '\0';
}
@@ -37,14 +57,15 @@ static void put_raw(struct json_emit *e, const char *s)
/*
* Malformed UTF-8 is -EILSEQ, so a stray byte can never produce
- * invalid JSON.
+ * invalid JSON; lossy writes U+FFFD for it, and for a NUL, instead.
*/
-static void put_string(struct json_emit *e, const char *s)
+static void put_string_n(struct json_emit *e, const char *s, size_t len,
+ bool lossy)
{
- const __u8 *p = (const __u8 *)s;
+ const __u8 *p = (const __u8 *)s, *end = p + len;
put(e, '"');
- while (*p && !e->err) {
+ while (p < end && !e->err) {
__u8 c = *p;
if (c == '"' || c == '\\') {
@@ -60,7 +81,7 @@ static void put_string(struct json_emit *e, const char *s)
} else if (c == '\t') {
put_raw(e, "\\t");
p++;
- } else if (c < 0x20) {
+ } else if (c && c < 0x20) {
char u[7];
if (strsprintf(u, sizeof(u), "\\u%04x", c) < 0) {
@@ -69,17 +90,22 @@ static void put_string(struct json_emit *e, const char *s)
}
put_raw(e, u);
p++;
- } else if (c < 0x80) {
+ } else if (c && c < 0x80) {
put(e, (char)c);
p++;
} else {
- size_t n = utf8_width((const char *)p,
- strnlen((const char *)p, 4));
+ size_t left = end - p;
+ size_t n = c ? utf8_width((const char *)p,
+ min(left, 4U)) : 0;
- if (!n) {
+ if (!n && !lossy) {
e->err = -EILSEQ;
return;
}
+ if (!n) {
+ put_raw(e, "\\ufffd");
+ p++;
+ }
while (n--)
put(e, (char)*p++);
}
@@ -87,26 +113,34 @@ static void put_string(struct json_emit *e, const char *s)
put(e, '"');
}
+static void put_string(struct json_emit *e, const char *s)
+{
+ put_string_n(e, s, strlen(s), false);
+}
+
static void sep(struct json_emit *e)
{
+ __u64 bit = 1ull << (e->depth % 64);
+ __u64 *word = &e->nonempty[e->depth / 64];
+
if (e->err)
return;
- if (e->nonempty & (1u << e->depth))
+ if (*word & bit)
put(e, ',');
else
- e->nonempty |= (1u << e->depth);
+ *word |= bit;
}
static void enter(struct json_emit *e)
{
if (e->err)
return;
- if (e->depth + 1 >= JSON_MAX_DEPTH) {
+ if (e->depth + 1 >= JSON_EMIT_MAX_DEPTH) {
e->err = -E2BIG;
return;
}
e->depth++;
- e->nonempty &= ~(1u << e->depth);
+ e->nonempty[e->depth / 64] &= ~(1ull << (e->depth % 64));
}
static void leave(struct json_emit *e)
@@ -266,6 +300,20 @@ void json_raw(struct json_emit *e, const char *value,
put(e, value[i]);
}
+void json_str_lossy(struct json_emit *e, const char *s, size_t n)
+{
+ json_value_begin(e);
+ put_string_n(e, s, n, true);
+}
+
+void json_key_lossy(struct json_emit *e, const char *s, size_t n)
+{
+ sep(e);
+ put_string_n(e, s, n, true);
+ put(e, ':');
+ e->value_pending = true;
+}
+
struct pctx {
const char *p;
const char *end;
diff --git a/util/json.h b/util/json.h
index 37c88c42..f0ed6740 100644
--- a/util/json.h
+++ b/util/json.h
@@ -23,13 +23,23 @@
#define JSON_MAX_STRING_BYTES (16u * 1024u * 1024u)
#endif
+/* An OTLP AnyValue nests four levels for each level of a decoder table. */
+#define JSON_EMIT_MAX_DEPTH 192u
+
+/*
+ * buf and cap are the caller's, or with max set the emitter's: it grows
+ * buf, xfree(buf, cap) releases it, and a document past max bytes is
+ * -E2BIG.
+ */
struct json_emit {
char *buf;
size_t cap;
size_t len;
- int err; /* sticky; -ENOSPC, -EILSEQ, -E2BIG */
+ size_t max;
+ int err; /* sticky; -ENOSPC, -EILSEQ, -E2BIG, -ENOMEM */
__u32 depth; /* open-container nesting */
- __u32 nonempty; /* bit d set: container at depth d has a member */
+ /* bit d set: the container at depth d has a member */
+ __u64 nonempty[JSON_EMIT_MAX_DEPTH / 64];
bool value_pending;
};
@@ -63,6 +73,14 @@ void json_raw_value(struct json_emit *e, const char *value,
size_t value_len);
void json_raw(struct json_emit *e, const char *value,
size_t value_len);
+/*
+ * The n bytes at s as a value or a key, with U+FFFD for each byte that
+ * starts no well-formed UTF-8 character and for each NUL: for bytes
+ * from outside, cut mid-character as often as not, where one bad byte
+ * must not cost the whole document.
+ */
+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);
/*
* Not a generic deserialiser: declared keys are dispatched, everything