* [PATCH] gdhcp: bound DHCPv6 domain-list parsing
2026-10-23 8:53 [PATCH] gdhcp: fix stack buffer overflow in get_domains() Andrei Miu
2026-10-23 10:54 ` Greg KH
@ 2026-12-04 6:47 ` Ashishkumar Parmar X (asparmar - E INFOCHIPS PRIVATE LIMITED at Cisco)
2026-12-04 10:09 ` Greg KH
1 sibling, 1 reply; 4+ messages in thread
From: Ashishkumar Parmar X (asparmar - E INFOCHIPS PRIVATE LIMITED at Cisco) @ 2026-12-04 6:47 UTC (permalink / raw)
To: andimiu; +Cc: connman, cve, deeratho, vchavda, Ashishkumar Parmar
From: Ashishkumar Parmar <asparmar@cisco.com>
get_domains() copies a fixed NS_MAXDNAME bytes from the DHCPv6
domain-list option and follows label lengths without checking the
remaining bytes. A short option can make the copy read beyond its
input, while a malformed length can move the label walk beyond the
stack buffer.
Bound the copy by the remaining option length. Check each label length
and the terminating zero in the original option bytes before adding a
name. Reading those bytes from the option prevents strncpy() padding
from masquerading as a wire terminator. Reject label lengths above 63
and NUL bytes within labels before constructing the returned C string.
Reject ASCII whitespace in labels because ConnMan writes the returned
domains into resolv.conf without escaping; a newline could inject a
second directive. Limit each encoded domain name to 255 bytes,
including its root label. Return an empty-string entry for a root
domain, including a root-only list.
Add tests for root domains, malformed labels, NUL and whitespace in
labels, an injected resolv.conf line, and the 255/256-byte wire-name
boundary. Cover a nonterminated 1025-byte option that triggers the
original out-of-bounds walk, and a valid multi-name option longer than
255 bytes.
This adapts Andrei Miu's proposed fix:
<https://lore.kernel.org/connman/179274561161.81299.4579745057858200074@pm.me/>
Fixes: bb06c47101ca24 ("gdhcp: Domain search list DHCPv6 option is checked")
CVE: CVE-2026-115863
Signed-off-by: Ashishkumar Parmar <asparmar@cisco.com>
---
Makefile.am | 12 +-
gdhcp/client.c | 31 +-----
gdhcp/domains.c | 81 ++++++++++++++
gdhcp/domains.h | 16 +++
unit/test-gdhcp-domains.c | 223 ++++++++++++++++++++++++++++++++++++++
5 files changed, 332 insertions(+), 31 deletions(-)
create mode 100644 gdhcp/domains.c
create mode 100644 gdhcp/domains.h
create mode 100644 unit/test-gdhcp-domains.c
diff --git a/Makefile.am b/Makefile.am
index 23c5dabe..7966cf48 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -36,7 +36,8 @@ if BACKTRACE
backtrace_sources = src/backtrace.c
endif
-gdhcp_sources = gdhcp/gdhcp.h gdhcp/common.h gdhcp/common.c gdhcp/client.c \
+gdhcp_sources = gdhcp/gdhcp.h gdhcp/common.h gdhcp/common.c gdhcp/domains.h \
+ gdhcp/domains.c gdhcp/client.c \
gdhcp/server.c gdhcp/unaligned.h
gweb_sources = gweb/gweb.h gweb/gweb.c gweb/gresolv.h gweb/gresolv.c
@@ -325,8 +326,15 @@ unit_test_ippool_LDADD = gdbus/libgdbus-internal.la \
LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \
$(top_srcdir)/build-aux/tap-driver.sh
+LOG_COMPILER = $(SHELL) -c 'exec "$$@" --tap' --
-TESTS = unit/test-ippool
+noinst_PROGRAMS += unit/test-gdhcp-domains
+
+unit_test_gdhcp_domains_SOURCES = gdhcp/domains.h gdhcp/domains.c \
+ unit/test-gdhcp-domains.c
+unit_test_gdhcp_domains_LDADD = @GLIB_LIBS@
+
+TESTS = unit/test-ippool unit/test-gdhcp-domains
if WISPR
noinst_PROGRAMS += tools/wispr
diff --git a/gdhcp/client.c b/gdhcp/client.c
index 976773ac..1683b0cc 100644
--- a/gdhcp/client.c
+++ b/gdhcp/client.c
@@ -48,6 +48,7 @@
#include "../src/shared/arp.h"
#include "gdhcp.h"
#include "common.h"
+#include "domains.h"
#define DISCOVER_TIMEOUT 5
#define DISCOVER_RETRIES 6
@@ -2205,34 +2206,6 @@ static GList *get_addresses(GDHCPClient *dhcp_client,
return list;
}
-static GList *get_domains(int maxlen, unsigned char *value)
-
-{
- GList *list = NULL;
- int pos = 0;
- unsigned char *c;
- char dns_name[NS_MAXDNAME + 1];
-
- if (!value || maxlen < 3)
- return NULL;
-
- while (pos < maxlen) {
- strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
-
- c = (unsigned char *)dns_name;
- while (c && *c) {
- int jump;
- jump = *c;
- *c = '.';
- c += jump + 1;
- }
- list = g_list_prepend(list, g_strdup(&dns_name[1]));
- pos += (char *)c - dns_name + 1;
- }
-
- return g_list_reverse(list);
-}
-
static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
int code, int len,
unsigned char *value,
@@ -2276,7 +2249,7 @@ static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
break;
case G_DHCPV6_DOMAIN_LIST:
- list = get_domains(len, value);
+ list = gdhcp_get_domains(len, value);
break;
default:
diff --git a/gdhcp/domains.c b/gdhcp/domains.c
new file mode 100644
index 00000000..a7846c35
--- /dev/null
+++ b/gdhcp/domains.c
@@ -0,0 +1,81 @@
+/*
+ * DHCPv6 domain-list parser.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <resolv.h>
+#include <string.h>
+
+#include "domains.h"
+
+#define DNS_MAX_WIRE_NAME 255
+
+GList *gdhcp_get_domains(int maxlen, unsigned char *value)
+{
+ GList *list = NULL;
+ int pos = 0;
+ char dns_name[NS_MAXDNAME + 1];
+
+ if (!value || maxlen < 1)
+ return NULL;
+
+ while (pos < maxlen) {
+ int name_len = maxlen - pos;
+ int label_pos = 0;
+
+ if (name_len > NS_MAXDNAME)
+ name_len = NS_MAXDNAME;
+ /* RFC 1035 limits each encoded name, including root, to 255 bytes. */
+ if (name_len > DNS_MAX_WIRE_NAME)
+ name_len = DNS_MAX_WIRE_NAME;
+
+ strncpy(dns_name, (char *)&value[pos], name_len);
+ dns_name[name_len] = '\0';
+
+ /* Read label lengths from the option, not the padded copy. */
+ while (label_pos < name_len && value[pos + label_pos]) {
+ int jump = value[pos + label_pos];
+ int remaining = name_len - label_pos;
+ int i;
+
+ /* A label needs its length byte, payload, and a root terminator. */
+ if (jump > 63 || jump + 1 >= remaining)
+ goto malformed;
+
+ /* NUL truncates the result; whitespace corrupts resolv.conf. */
+ for (i = 0; i < jump; i++) {
+ unsigned char c = value[pos + label_pos + 1 + i];
+
+ if (c == '\0' || c == ' ' ||
+ (c >= '\t' && c <= '\r'))
+ goto malformed;
+ }
+
+ dns_name[label_pos] = '.';
+ label_pos += jump + 1;
+ }
+
+ /* The option must contain the zero-length root label. */
+ if (label_pos == name_len)
+ goto malformed;
+
+ if (label_pos == 0)
+ list = g_list_prepend(list, g_strdup(""));
+ else
+ list = g_list_prepend(list, g_strdup(&dns_name[1]));
+ pos += label_pos + 1;
+ }
+
+ return g_list_reverse(list);
+
+malformed:
+ g_list_free_full(list, g_free);
+ return NULL;
+}
diff --git a/gdhcp/domains.h b/gdhcp/domains.h
new file mode 100644
index 00000000..b4ee6969
--- /dev/null
+++ b/gdhcp/domains.h
@@ -0,0 +1,16 @@
+/*
+ * DHCPv6 domain-list parser.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef __G_DHCP_DOMAINS_H
+#define __G_DHCP_DOMAINS_H
+
+#include <glib.h>
+
+GList *gdhcp_get_domains(int maxlen, unsigned char *value);
+
+#endif /* __G_DHCP_DOMAINS_H */
diff --git a/unit/test-gdhcp-domains.c b/unit/test-gdhcp-domains.c
new file mode 100644
index 00000000..cdd43c76
--- /dev/null
+++ b/unit/test-gdhcp-domains.c
@@ -0,0 +1,223 @@
+/*
+ * DHCPv6 domain-list parser regression tests.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib.h>
+#include <string.h>
+
+#include "../gdhcp/domains.h"
+
+static void test_root_domain_only(void)
+{
+ unsigned char value[] = { 0 };
+ GList *domains = gdhcp_get_domains(sizeof(value), value);
+
+ g_assert_nonnull(domains);
+ g_assert_cmpuint(g_list_length(domains), ==, 1);
+ g_assert_cmpstr(domains->data, ==, "");
+ g_list_free_full(domains, g_free);
+}
+
+static void test_root_domain_followed_by_domain(void)
+{
+ unsigned char value[] = {
+ 0,
+ 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
+ 3, 'c', 'o', 'm', 0,
+ };
+ GList *domains = gdhcp_get_domains(sizeof(value), value);
+
+ g_assert_nonnull(domains);
+ g_assert_cmpuint(g_list_length(domains), ==, 2);
+ g_assert_cmpstr(domains->data, ==, "");
+ g_assert_cmpstr(domains->next->data, ==, "example.com");
+ g_list_free_full(domains, g_free);
+}
+
+static void test_truncated_label(void)
+{
+ unsigned char value[] = { 4, 't', 'e' };
+
+ g_assert_null(gdhcp_get_domains(sizeof(value), value));
+}
+
+static void test_missing_terminator(void)
+{
+ unsigned char value[] = { 3, 'f', 'o', 'o' };
+
+ g_assert_null(gdhcp_get_domains(sizeof(value), value));
+}
+
+static void test_padding_is_not_wire_terminator(void)
+{
+ unsigned char value[] = { 1, 'a', 3, 'f', '\0', 'x', 1, '\0' };
+
+ g_assert_null(gdhcp_get_domains(sizeof(value), value));
+}
+
+static void test_embedded_nul_in_label(void)
+{
+ unsigned char value[] = { 3, 'a', '\0', 'b', 0 };
+
+ g_assert_null(gdhcp_get_domains(sizeof(value), value));
+}
+
+static void test_whitespace_in_label(void)
+{
+ static const unsigned char whitespace[] = {
+ ' ', '\t', '\n', '\r', '\v', '\f',
+ };
+ unsigned char value[] = { 3, 'a', ' ', 'b', 0 };
+ unsigned int i;
+
+ for (i = 0; i < sizeof(whitespace); i++) {
+ value[2] = whitespace[i];
+ g_assert_null(gdhcp_get_domains(sizeof(value), value));
+ }
+}
+
+static void test_resolv_conf_line_injection(void)
+{
+ unsigned char value[] = {
+ 20, 'a', '\n',
+ 'n', 'a', 'm', 'e', 's', 'e', 'r', 'v', 'e', 'r',
+ ' ', '1', '.', '2', '.', '3', '.', '4', 0,
+ };
+
+ g_assert_null(gdhcp_get_domains(sizeof(value), value));
+}
+
+static void test_printable_punctuation_in_label(void)
+{
+ unsigned char value[] = { 7, 'f', 'o', 'o', '_', 'b', 'a', 'r', 0 };
+ GList *domains = gdhcp_get_domains(sizeof(value), value);
+
+ g_assert_nonnull(domains);
+ g_assert_cmpuint(g_list_length(domains), ==, 1);
+ g_assert_cmpstr(domains->data, ==, "foo_bar");
+ g_list_free_full(domains, g_free);
+}
+
+static void test_invalid_label_length(void)
+{
+ unsigned char value[194];
+
+ memset(value + 1, 'a', 64);
+ value[0] = 64;
+ value[65] = 0;
+ g_assert_null(gdhcp_get_domains(66, value));
+
+ memset(value + 1, 'a', 192);
+ value[0] = 0xc0;
+ value[193] = 0;
+ g_assert_null(gdhcp_get_domains(sizeof(value), value));
+}
+
+static void test_wire_name_length_limit(void)
+{
+ unsigned char value[256];
+ GList *domains;
+ int pos = 0;
+ int i;
+
+ for (i = 0; i < 3; i++) {
+ value[pos++] = 63;
+ memset(value + pos, 'a' + i, 63);
+ pos += 63;
+ }
+ value[pos++] = 61;
+ memset(value + pos, 'd', 61);
+ pos += 61;
+ value[pos++] = 0;
+
+ g_assert_cmpint(pos, ==, 255);
+ domains = gdhcp_get_domains(pos, value);
+ g_assert_nonnull(domains);
+ g_assert_cmpuint(g_list_length(domains), ==, 1);
+ g_assert_cmpuint(strlen(domains->data), ==, 253);
+ g_list_free_full(domains, g_free);
+
+ /* Extend the last label by one byte: root is now byte 256. */
+ value[192] = 62;
+ value[254] = 'd';
+ value[255] = 0;
+ g_assert_null(gdhcp_get_domains(sizeof(value), value));
+}
+
+static void test_nonterminated_long_option(void)
+{
+ unsigned char value[1025];
+
+ memset(value, 63, sizeof(value));
+ g_assert_null(gdhcp_get_domains(sizeof(value), value));
+}
+
+static void test_multiple_names_in_long_option(void)
+{
+ unsigned char value[258];
+ GList *domains;
+ GList *entry;
+ int pos = 0;
+ int i;
+
+ for (i = 0; i < 2; i++) {
+ value[pos++] = 63;
+ memset(value + pos, 'a' + i, 63);
+ pos += 63;
+ value[pos++] = 63;
+ memset(value + pos, 'c' + i, 63);
+ pos += 63;
+ value[pos++] = 0;
+ }
+
+ g_assert_cmpint(pos, ==, sizeof(value));
+ domains = gdhcp_get_domains(sizeof(value), value);
+ g_assert_nonnull(domains);
+ g_assert_cmpuint(g_list_length(domains), ==, 2);
+ for (entry = domains; entry; entry = entry->next) {
+ g_assert_cmpuint(strlen(entry->data), ==, 127);
+ g_assert_cmpint(((char *)entry->data)[63], ==, '.');
+ }
+ g_list_free_full(domains, g_free);
+}
+
+int main(int argc, char *argv[])
+{
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_add_func("/gdhcp/domains/root-only", test_root_domain_only);
+ g_test_add_func("/gdhcp/domains/root-followed-by-domain",
+ test_root_domain_followed_by_domain);
+ g_test_add_func("/gdhcp/domains/truncated-label",
+ test_truncated_label);
+ g_test_add_func("/gdhcp/domains/missing-terminator",
+ test_missing_terminator);
+ g_test_add_func("/gdhcp/domains/padding-is-not-wire-terminator",
+ test_padding_is_not_wire_terminator);
+ g_test_add_func("/gdhcp/domains/embedded-nul-in-label",
+ test_embedded_nul_in_label);
+ g_test_add_func("/gdhcp/domains/whitespace-in-label",
+ test_whitespace_in_label);
+ g_test_add_func("/gdhcp/domains/resolv-conf-line-injection",
+ test_resolv_conf_line_injection);
+ g_test_add_func("/gdhcp/domains/printable-punctuation-in-label",
+ test_printable_punctuation_in_label);
+ g_test_add_func("/gdhcp/domains/invalid-label-length",
+ test_invalid_label_length);
+ g_test_add_func("/gdhcp/domains/wire-name-length-limit",
+ test_wire_name_length_limit);
+ g_test_add_func("/gdhcp/domains/nonterminated-long-option",
+ test_nonterminated_long_option);
+ g_test_add_func("/gdhcp/domains/multiple-names-in-long-option",
+ test_multiple_names_in_long_option);
+
+ return g_test_run();
+}
--
2.35.6
^ permalink raw reply related [flat|nested] 4+ messages in thread