]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-dhcp-client: validate hostnames stricter (#7308)
authorStefan Agner <falstaff@deheime.ch>
Thu, 16 Nov 2017 09:05:44 +0000 (10:05 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 16 Nov 2017 09:05:44 +0000 (10:05 +0100)
Technically DNS allows any ASCII character to be used in the
domain name. Also the DHCP specification for the FQDN option
(RFC 4702) does not put restriction on labels.

However, hostnames do have stricter requirements and typically
should only use characters from a-z (case insensitve), 0-9 and
minus.

Currently we require hostname/FQDN to be either a hostname or
a valid DNS name. Since dns_name_is_valid() allows any ASCII
characters this allows to specify hostnames which are typically
not valid.

Check hostname/FQDN more strictly and require them to pass both
tests. Specifically this requires the entire FQDN to be below 63.

src/libsystemd-network/sd-dhcp-client.c
src/libsystemd-network/test-dhcp-client.c

index 29b22eed456cb49e0cb9ea9f18dcf3e07c2bb57a..d30755115e613d5d45d59b417567977a59c73b04 100644 (file)
@@ -415,9 +415,9 @@ int sd_dhcp_client_set_hostname(
 
         assert_return(client, -EINVAL);
 
-        /* Refuse hostnames that neither qualify as DNS nor as Linux hosntames */
+        /* Make sure hostnames qualify as DNS and as Linux hostnames */
         if (hostname &&
-            !(hostname_is_valid(hostname, false) || dns_name_is_valid(hostname) > 0))
+            !(hostname_is_valid(hostname, false) && dns_name_is_valid(hostname) > 0))
                 return -EINVAL;
 
         return free_and_strdup(&client->hostname, hostname);
index e4ef479a5464a44b4037e362f2420935e7e9aaff..e71f2a4d1d2cc87f0e7184d20506255c6b213761 100644 (file)
@@ -75,6 +75,12 @@ static void test_request_basic(sd_event *e) {
         assert_se(sd_dhcp_client_set_ifindex(client, 0) == -EINVAL);
         assert_se(sd_dhcp_client_set_ifindex(client, 1) == 0);
 
+        assert_se(sd_dhcp_client_set_hostname(client, "host") == 1);
+        assert_se(sd_dhcp_client_set_hostname(client, "host.domain") == 1);
+        assert_se(sd_dhcp_client_set_hostname(client, NULL) == 1);
+        assert_se(sd_dhcp_client_set_hostname(client, "~host") == -EINVAL);
+        assert_se(sd_dhcp_client_set_hostname(client, "~host.domain") == -EINVAL);
+
         assert_se(sd_dhcp_client_set_request_option(client,
                                         SD_DHCP_OPTION_SUBNET_MASK) == -EEXIST);
         assert_se(sd_dhcp_client_set_request_option(client,