eBPF-based security platform
upatch_decode: export invalid UTF-8 as U+FFFD instead of failing
Daniel Borkmann4cc1023dbece
diff --git a/Documentation/telemetry.rst b/Documentation/telemetry.rstindex a29eca53..2e136155 100644--- a/Documentation/telemetry.rst+++ b/Documentation/telemetry.rst@@ -142,7 +142,9 @@ returns a list of OTLP ``Metric`` objects, exactly as they appear inthe ``metrics`` array of a ``scopeMetrics`` entry, so the decoderdecides the name, unit, description, aggregation and data points. Anerror raised in the decoder drops that snapshot with a diagnostic; it-never produces a partial record. The shipped+never produces a partial record. Strings go out as UTF-8, with U+FFFD+in place of a byte sequence that is not, such as a name the kernel+cut mid-character. The shipped``upatches/kernel-obs/decoders/watch_stat.lua`` turns a per-CPUcounter pair into one monotonic sum with two labelled points::diff --git a/testing/t0005-upatch-decoders.sh b/testing/t0005-upatch-decoders.shindex 4d3edc76..42d333d9 100755--- a/testing/t0005-upatch-decoders.sh+++ b/testing/t0005-upatch-decoders.sh@@ -148,8 +148,9 @@ test_expect_success 'Lua decoder enforces instruction and memory limits' 'if ./helper/test-upatch-decode memory "$WORK/u.upatch"; then false; fi'-test_expect_success 'Lua decoder rejects invalid UTF-8 output' '- if ./helper/test-upatch-decode utf8 "$WORK/u.upatch"; then false; fi+test_expect_success 'Lua decoder output replaces invalid UTF-8' '+ [ "$(./helper/test-upatch-decode utf8 "$WORK/u.upatch")" = \+ "\"\\ufffd\"" ]'python3 - "$WORK/u.upatch" "$WORK" <<'PY'diff --git a/util/upatch_decode.c b/util/upatch_decode.cindex 3c4e94b8..aa15756e 100644--- a/util/upatch_decode.c+++ b/util/upatch_decode.c@@ -218,6 +218,44 @@ static void json_puts(struct json_out *out, const char *s)out->buf[out->len] = '\0';}+/*+ * The bytes of one well-formed UTF-8 character at s, or 0. Names the+ * kernel hands over are raw bytes, cut at a fixed length mid-character+ * as often as not, and one bad byte must not cost the whole snapshot,+ * which the serializer has already drained: it is exported as U+FFFD.+ */+static size_t utf8_width(const char *s, size_t n)+{+ __u8 c = s[0];+ size_t width, j;+ __u32 cp;++ if ((c & 0xe0) == 0xc0) {+ width = 2;+ cp = c & 0x1f;+ } else if ((c & 0xf0) == 0xe0) {+ width = 3;+ cp = c & 0x0f;+ } else if ((c & 0xf8) == 0xf0) {+ width = 4;+ cp = c & 0x07;+ } else {+ return 0;+ }+ if (width > n)+ return 0;+ for (j = 1; j < width; j++) {+ if (((__u8)s[j] & 0xc0) != 0x80)+ return 0;+ cp = cp << 6 | ((__u8)s[j] & 0x3f);+ }+ if ((width == 2 && cp < 0x80) || (width == 3 && cp < 0x800) ||+ (width == 4 && cp < 0x10000) || cp > 0x10ffff ||+ (cp >= 0xd800 && cp <= 0xdfff))+ return 0;+ return width;+}+static void json_put_string(struct json_out *out, const char *s, size_t n){static const char hexdigits[] = "0123456789abcdef";@@ -254,30 +292,12 @@ static void json_put_string(struct json_out *out, const char *s, size_t n)json_putc(out, hexdigits[c >> 4]);json_putc(out, hexdigits[c & 0xf]);} else if (c >= 0x80) {- size_t width, j;-- if ((c & 0xe0) == 0xc0) {- width = 2;- } else if ((c & 0xf0) == 0xe0) {- width = 3;- } else if ((c & 0xf8) == 0xf0) {- width = 4;- } else {- out->err = -EILSEQ;- break;- }- if (width > n - i) {- out->err = -EILSEQ;+ size_t width = utf8_width(s + i, n - i), j;++ if (!width) {+ json_puts(out, "\\ufffd");break;}- for (j = 1; j < width; j++) {- if (((__u8)s[i + j] & 0xc0) != 0x80) {- out->err = -EILSEQ;- break;- }- }- if (out->err)- break;for (j = 0; j < width; j++)json_putc(out, s[i + j]);i += width - 1;