Skip to content

eBPF-based security platform

util: share one DNS name check between config and TLS

Daniel Borkmanne5d8e938b526
diff --git a/include/shirudo/string.h b/include/shirudo/string.h
index 21ccb038..a89652db 100644
--- a/include/shirudo/string.h
+++ b/include/shirudo/string.h
@@ -2,7 +2,8 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * Bounded number parsing, string copies and formatting, from util/string.c.
+ * Bounded number parsing, string copies and formatting, DNS names, from
+ * util/string.c.
*/
#ifndef SHIRUDO_STRING_H
@@ -10,6 +11,7 @@
#include <inttypes.h>
#include <stdarg.h>
+#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@@ -50,6 +52,13 @@ int strsprintf(char *buf, size_t size, const char *fmt, ...)
int vstrsprintf(char *buf, size_t size, const char *fmt, va_list ap)
__printf(3, 0) __attribute__((warn_unused_result));
+/*
+ * A DNS host name: labels of letters, digits and inner hyphens, 1 to 63
+ * bytes each and 253 in all. A name whose labels are all decimal or 0x
+ * numbers is refused, as an IPv4 parser would read it as an address.
+ */
+bool dns_name_valid(const char *name);
+
/*
* What the functions above replace, refused once the headers included
* above have declared it, so that a later include of those still works;
diff --git a/testing/unit/string.c b/testing/unit/string.c
index 9e90fcab..5b8f9d5f 100644
--- a/testing/unit/string.c
+++ b/testing/unit/string.c
@@ -240,6 +240,47 @@ static void xasprintf_allocates_the_size_needed(void)
xstrfree(s);
}
+static void dns_name_is_ldh_labels_and_no_address(void)
+{
+ char label[65], name[300];
+
+ EXPECT_TRUE(dns_name_valid("registry.example.com"));
+ EXPECT_TRUE(dns_name_valid("Reg-1.Example"));
+ EXPECT_TRUE(dns_name_valid("localhost"));
+ EXPECT_TRUE(dns_name_valid("1.2.3.example"));
+ EXPECT_TRUE(dns_name_valid("0x.1"));
+ EXPECT_FALSE(dns_name_valid(""));
+ EXPECT_FALSE(dns_name_valid("."));
+ EXPECT_FALSE(dns_name_valid("example..com"));
+ EXPECT_FALSE(dns_name_valid("example.com."));
+ EXPECT_FALSE(dns_name_valid("-example.com"));
+ EXPECT_FALSE(dns_name_valid("example-.com"));
+ EXPECT_FALSE(dns_name_valid("exa_mple.com"));
+ EXPECT_FALSE(dns_name_valid("example.com:443"));
+ EXPECT_FALSE(dns_name_valid("10.0.2.2"));
+ EXPECT_FALSE(dns_name_valid("127.1"));
+ EXPECT_FALSE(dns_name_valid("0x7f.1"));
+ EXPECT_FALSE(dns_name_valid("2130706433"));
+
+ memset(label, 'a', 63);
+ label[63] = '\0';
+ ASSERT_TRUE(strsprintf(name, sizeof(name), "%s.example", label) > 0);
+ EXPECT_TRUE(dns_name_valid(name));
+ memset(label, 'a', 64);
+ label[64] = '\0';
+ ASSERT_TRUE(strsprintf(name, sizeof(name), "%s.example", label) > 0);
+ EXPECT_FALSE(dns_name_valid(name));
+
+ memset(label, 'a', 63);
+ label[63] = '\0';
+ ASSERT_TRUE(strsprintf(name, sizeof(name), "%s.%s.%s.%.61s", label,
+ label, label, label) == 253);
+ EXPECT_TRUE(dns_name_valid(name));
+ ASSERT_TRUE(strsprintf(name, sizeof(name), "%s.%s.%s.%.62s", label,
+ label, label, label) == 254);
+ EXPECT_FALSE(dns_name_valid(name));
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(digits_counts_leading_digits),
UNIT_CASE(decimal_fills_its_span),
@@ -255,6 +296,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(strsprintf_writes_what_fits_whole),
UNIT_CASE(strsprintf_leaves_nothing_when_cut),
UNIT_CASE(xasprintf_allocates_the_size_needed),
+ UNIT_CASE(dns_name_is_ldh_labels_and_no_address),
{}
};
diff --git a/util/config.c b/util/config.c
index 5b7dedf1..ec7870f3 100644
--- a/util/config.c
+++ b/util/config.c
@@ -65,62 +65,6 @@ static bool set_string(char *dst, size_t cap, const char *val)
return strscpy(dst, val, cap) >= 0;
}
-static bool numeric_dns_label(const char *label, size_t len)
-{
- size_t i = 0;
- bool hex = len > 2 && label[0] == '0' &&
- (label[1] == 'x' || label[1] == 'X');
-
- if (!len)
- return false;
- if (hex)
- i = 2;
- for (; i < len; i++) {
- __u8 c = label[i];
-
- if (c >= '0' && c <= '9')
- continue;
- if (hex && ((c >= 'a' && c <= 'f') ||
- (c >= 'A' && c <= 'F')))
- continue;
- return false;
- }
- return !hex || i > 2;
-}
-
-static bool tls_dns_name_valid(const char *name)
-{
- size_t len = strlen(name), label_len = 0, label_start = 0;
- bool numeric_address = true;
- size_t i;
-
- if (!len || len > 253)
- return false;
- for (i = 0; i < len; i++) {
- __u8 c = name[i];
-
- if (c == '.') {
- if (!label_len || label_len > 63 || name[i - 1] == '-')
- return false;
- if (!numeric_dns_label(name + label_start, label_len))
- numeric_address = false;
- label_start = i + 1;
- label_len = 0;
- continue;
- }
- if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') || c == '-') ||
- (!label_len && c == '-'))
- return false;
- label_len++;
- }
- if (!label_len || label_len > 63 || name[len - 1] == '-')
- return false;
- if (!numeric_dns_label(name + label_start, label_len))
- numeric_address = false;
- return !numeric_address;
-}
-
static bool tls_classical_hosts_valid(const char *value)
{
const char *host = value;
@@ -136,7 +80,7 @@ static bool tls_classical_hosts_valid(const char *value)
return false;
memcpy(name, host, len);
name[len] = '\0';
- if (!tls_dns_name_valid(name))
+ if (!dns_name_valid(name))
return false;
if (!end)
break;
diff --git a/util/string.c b/util/string.c
index a7722438..2ae3aab8 100644
--- a/util/string.c
+++ b/util/string.c
@@ -2,7 +2,7 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * Bounded number parsing, string copies and formatting.
+ * Bounded number parsing, string copies and formatting, DNS names.
*/
#include <errno.h>
@@ -110,6 +110,62 @@ ssize_t strscpy(char *dst, const char *src, size_t size)
return (ssize_t)len;
}
+static bool numeric_dns_label(const char *label, size_t len)
+{
+ size_t i = 0;
+ bool hex = len > 2 && label[0] == '0' &&
+ (label[1] == 'x' || label[1] == 'X');
+
+ if (!len)
+ return false;
+ if (hex)
+ i = 2;
+ for (; i < len; i++) {
+ __u8 c = label[i];
+
+ if (c >= '0' && c <= '9')
+ continue;
+ if (hex && ((c >= 'a' && c <= 'f') ||
+ (c >= 'A' && c <= 'F')))
+ continue;
+ return false;
+ }
+ return !hex || i > 2;
+}
+
+bool dns_name_valid(const char *name)
+{
+ size_t len = strlen(name), label_len = 0, label_start = 0;
+ bool numeric_address = true;
+ size_t i;
+
+ if (!len || len > 253)
+ return false;
+ for (i = 0; i < len; i++) {
+ __u8 c = name[i];
+
+ if (c == '.') {
+ if (!label_len || label_len > 63 || name[i - 1] == '-')
+ return false;
+ if (!numeric_dns_label(name + label_start, label_len))
+ numeric_address = false;
+ label_start = i + 1;
+ label_len = 0;
+ continue;
+ }
+ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
+ (c >= '0' && c <= '9') || c == '-') ||
+ (!label_len && c == '-'))
+ return false;
+ label_len++;
+ }
+ if (!label_len || label_len > 63 || name[len - 1] == '-')
+ return false;
+ if (!numeric_dns_label(name + label_start, label_len))
+ numeric_address = false;
+ return !numeric_address;
+}
+
int vstrsprintf(char *buf, size_t size, const char *fmt, va_list ap)
{
int n = vsnprintf(buf, size, fmt, ap);
diff --git a/util/tls_openssl.c b/util/tls_openssl.c
index c0dc3840..a711336a 100644
--- a/util/tls_openssl.c
+++ b/util/tls_openssl.c
@@ -21,6 +21,7 @@
#include <openssl/x509.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "config.h"
#include "crypto.h"
@@ -33,62 +34,6 @@
#define SHIRUDO_TLS_CLASSICAL_GROUPS \
"*X25519MLKEM768:SecP256r1MLKEM768:X25519:secp256r1"
-static bool numeric_dns_label(const char *label, size_t len)
-{
- size_t i = 0;
- bool hex = len > 2 && label[0] == '0' &&
- (label[1] == 'x' || label[1] == 'X');
-
- if (!len)
- return false;
- if (hex)
- i = 2;
- for (; i < len; i++) {
- __u8 c = label[i];
-
- if (c >= '0' && c <= '9')
- continue;
- if (hex && ((c >= 'a' && c <= 'f') ||
- (c >= 'A' && c <= 'F')))
- continue;
- return false;
- }
- return !hex || i > 2;
-}
-
-static bool valid_dns_name(const char *name)
-{
- size_t len = strlen(name), label_len = 0, label_start = 0;
- bool numeric_address = true;
- size_t i;
-
- if (!len || len > 253)
- return false;
- for (i = 0; i < len; i++) {
- __u8 c = name[i];
-
- if (c == '.') {
- if (!label_len || label_len > 63 || name[i - 1] == '-')
- return false;
- if (!numeric_dns_label(name + label_start, label_len))
- numeric_address = false;
- label_start = i + 1;
- label_len = 0;
- continue;
- }
- if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') || c == '-') ||
- (!label_len && c == '-'))
- return false;
- label_len++;
- }
- if (!label_len || label_len > 63 || name[len - 1] == '-')
- return false;
- if (!numeric_dns_label(name + label_start, label_len))
- numeric_address = false;
- return !numeric_address;
-}
-
/*
* Exceptions are exact DNS names from the active HTTPS URL. This also
* checks redirected URLs, so each registry host must be opted in itself.
@@ -122,7 +67,7 @@ static bool tls_classical_host_allowed(CURL *curl)
host_len = strlen(host);
if (host_len && host[host_len - 1] == '.')
host[--host_len] = '\0';
- if (!valid_dns_name(host))
+ if (!dns_name_valid(host))
goto out;
while (*allowlist) {
@@ -133,7 +78,7 @@ static bool tls_classical_host_allowed(CURL *curl)
if (len < sizeof(entry)) {
memcpy(entry, allowlist, len);
entry[len] = '\0';
- if (valid_dns_name(entry) && !strncasecmp(host, entry, len) &&
+ if (dns_name_valid(entry) && !strncasecmp(host, entry, len) &&
host_len == len) {
allowed = true;
break;