Skip to content

eBPF-based security platform

otel: take OTLP header names as tokens and values without controls

Daniel Borkmann1f3b29be7651
diff --git a/Documentation/reference/formats/otlp.rst b/Documentation/reference/formats/otlp.rst
index 5df9173a..e9c7e64d 100644
--- a/Documentation/reference/formats/otlp.rst
+++ b/Documentation/reference/formats/otlp.rst
@@ -147,8 +147,10 @@ services read them from ``/etc/shirudo/map-export.env``.
- ``none`` or ``gzip``
* - ``OTEL_EXPORTER_OTLP_HEADERS``
- ``<name>=<value>[,<name>=<value>...]``, at most 16384 bytes.
- Values are percent-decoded. Refused: a name that is empty or
- contains ``:``, CR or LF, a value with CR or LF, and the names
+ Blanks around names and values are dropped and values are
+ percent-decoded. Refused: a name that is not an HTTP token (RFC
+ 9110), a value that holds a control character other than tab
+ before or after decoding, ``%00`` included, and the names
``Content-Type``, ``Content-Encoding``, ``Content-Length``,
``Host`` and ``User-Agent``. One ``X-Tenant-ID`` is allowed and
must be a valid tenant id. A tenant id from the command line or
diff --git a/testing/fuzz/fuzz-otel.c b/testing/fuzz/fuzz-otel.c
index 7f450d2b..2c59186e 100644
--- a/testing/fuzz/fuzz-otel.c
+++ b/testing/fuzz/fuzz-otel.c
@@ -104,7 +104,21 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
}
if (!memchr(data, '\0', size)) {
- add_configured_headers(&st, body);
+ if (!add_configured_headers(&st, body)) {
+ for (struct curl_slist *h = st.headers; h; h = h->next) {
+ char *colon = strchr(h->data, ':');
+ bool ok;
+
+ if (!colon)
+ abort();
+ *colon = '\0';
+ ok = header_name_ok(h->data) &&
+ header_value_ok(colon + 1);
+ *colon = ':';
+ if (!ok)
+ abort();
+ }
+ }
curl_slist_free_all(st.headers);
}
xfree(body, size + 1);
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index ef7f478a..bfe680d3 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -1108,7 +1108,8 @@ static int percent_decode(char *s)
return -EINVAL;
hi = hex_nibble(src[1]);
lo = hex_nibble(src[2]);
- if (hi < 0 || lo < 0)
+ /* A NUL would cut the value where the string ends. */
+ if (hi < 0 || lo < 0 || !(hi | lo))
return -EINVAL;
*dst++ = (char)((hi << 4) | lo);
src += 3;
@@ -1120,6 +1121,26 @@ static int percent_decode(char *s)
return 0;
}
+/* RFC 9110, 5.6.2: a field name is a token. */
+static bool header_name_ok(const char *s)
+{
+ if (!*s)
+ return false;
+ for (; *s; s++)
+ if (!isalnum((__u8)*s) && !strchr("!#$%&'*+-.^_`|~", *s))
+ return false;
+ return true;
+}
+
+/* RFC 9110, 5.5: a field value holds no control character but tab. */
+static bool header_value_ok(const char *s)
+{
+ for (; *s; s++)
+ if (((__u8)*s < 0x20 && *s != '\t') || *s == 0x7f)
+ return false;
+ return true;
+}
+
static int add_configured_headers(struct otel_state *st, const char *input)
{
char *copy, *save = NULL, *pair;
@@ -1156,10 +1177,12 @@ static int add_configured_headers(struct otel_state *st, const char *input)
for (char *p = name + strlen(name);
p > name && isspace((__u8)p[-1]); p--)
p[-1] = '\0';
+ for (char *p = value + strlen(value);
+ p > value && isspace((__u8)p[-1]); p--)
+ p[-1] = '\0';
tenant = strcasecmp(name, "x-tenant-id") == 0;
- if (!name[0] || reserved_header(name) ||
- percent_decode(value) != 0 || strpbrk(name, "\r\n:") ||
- strpbrk(value, "\r\n")) {
+ if (!header_name_ok(name) || reserved_header(name) ||
+ percent_decode(value) != 0 || !header_value_ok(value)) {
ret = -EINVAL;
break;
}