ac3a84
From 677b20b6738ee287d1b882815b3bcca67754e003 Mon Sep 17 00:00:00 2001
ac3a84
From: Lennart Poettering <lennart@poettering.net>
ac3a84
Date: Fri, 25 Nov 2022 12:15:56 +0100
ac3a84
Subject: [PATCH] resolved: introduce the _localdnsstub and _localdnsproxy
ac3a84
 special hostnames for 127.0.0.54 + 127.0.0.53
ac3a84
ac3a84
Let's give these special IP addresses names. After all name resolution
ac3a84
is our job here.
ac3a84
ac3a84
Fixes: #23623
ac3a84
(cherry picked from commit 17f244e8f9de008ea1c6e0880bdc924b95a66e2b)
ac3a84
ac3a84
Related: #2138081
ac3a84
---
ac3a84
 man/resolvectl.xml                    |  11 +--
ac3a84
 man/systemd-resolved.service.xml      |   6 ++
ac3a84
 src/basic/hostname-util.h             |   8 ++
ac3a84
 src/resolve/resolvectl.c              |   6 +-
ac3a84
 src/resolve/resolved-dns-scope.c      |   7 +-
ac3a84
 src/resolve/resolved-dns-synthesize.c | 110 +++++++++++++++++++++++++-
ac3a84
 test/units/testsuite-75.sh            |  11 +++
ac3a84
 7 files changed, 147 insertions(+), 12 deletions(-)
ac3a84
ac3a84
diff --git a/man/resolvectl.xml b/man/resolvectl.xml
ac3a84
index 2cb855c360..c966ca67bd 100644
ac3a84
--- a/man/resolvectl.xml
ac3a84
+++ b/man/resolvectl.xml
ac3a84
@@ -323,11 +323,12 @@
ac3a84
 
ac3a84
         <listitem><para>Takes a boolean parameter; used in conjunction with <command>query</command>. If true
ac3a84
         (the default), select domains are resolved on the local system, among them
ac3a84
-        <literal>localhost</literal>, <literal>_gateway</literal> and <literal>_outbound</literal>, or
ac3a84
-        entries from <filename>/etc/hosts</filename>. If false these domains are not resolved locally, and
ac3a84
-        either fail (in case of <literal>localhost</literal>, <literal>_gateway</literal> or
ac3a84
-        <literal>_outbound</literal> and suchlike) or go to the network via regular DNS/mDNS/LLMNR lookups
ac3a84
-        (in case of <filename>/etc/hosts</filename> entries).</para></listitem>
ac3a84
+        <literal>localhost</literal>, <literal>_gateway</literal>, <literal>_outbound</literal>,
ac3a84
+        <literal>_localdnsstub</literal> and <literal>_localdnsproxy</literal> or entries from
ac3a84
+        <filename>/etc/hosts</filename>. If false these domains are not resolved locally, and either fail (in
ac3a84
+        case of <literal>localhost</literal>, <literal>_gateway</literal> or <literal>_outbound</literal> and
ac3a84
+        suchlike) or go to the network via regular DNS/mDNS/LLMNR lookups (in case of
ac3a84
+        <filename>/etc/hosts</filename> entries).</para></listitem>
ac3a84
       </varlistentry>
ac3a84
 
ac3a84
       <varlistentry>
ac3a84
diff --git a/man/systemd-resolved.service.xml b/man/systemd-resolved.service.xml
ac3a84
index 7f30fa6536..c006c03b53 100644
ac3a84
--- a/man/systemd-resolved.service.xml
ac3a84
+++ b/man/systemd-resolved.service.xml
ac3a84
@@ -118,6 +118,12 @@
ac3a84
       local default gateway configured. This assigns a stable hostname to the local outbound IP addresses,
ac3a84
       useful for referencing them independently of the current network configuration state.</para></listitem>
ac3a84
 
ac3a84
+      <listitem><para>The hostname <literal>_localdnsstub</literal> is resolved to the IP address 127.0.0.53,
ac3a84
+      i.e. the address the local DNS stub (see above) is listening on.</para></listitem>
ac3a84
+
ac3a84
+      <listitem><para>The hostname <literal>_localdnsproxy</literal> is resolved to the IP address 127.0.0.54,
ac3a84
+      i.e. the address the local DNS proxy (see above) is listening on.</para></listitem>
ac3a84
+
ac3a84
       <listitem><para>The mappings defined in <filename>/etc/hosts</filename> are resolved to their
ac3a84
       configured addresses and back, but they will not affect lookups for non-address types (like MX).
ac3a84
       Support for <filename>/etc/hosts</filename> may be disabled with <varname>ReadEtcHosts=no</varname>,
ac3a84
diff --git a/src/basic/hostname-util.h b/src/basic/hostname-util.h
ac3a84
index a00b852395..bcac3d9fb0 100644
ac3a84
--- a/src/basic/hostname-util.h
ac3a84
+++ b/src/basic/hostname-util.h
ac3a84
@@ -60,4 +60,12 @@ static inline bool is_outbound_hostname(const char *hostname) {
ac3a84
         return STRCASE_IN_SET(hostname, "_outbound", "_outbound.");
ac3a84
 }
ac3a84
 
ac3a84
+static inline bool is_dns_stub_hostname(const char *hostname) {
ac3a84
+        return STRCASE_IN_SET(hostname, "_localdnsstub", "_localdnsstub.");
ac3a84
+}
ac3a84
+
ac3a84
+static inline bool is_dns_proxy_stub_hostname(const char *hostname) {
ac3a84
+        return STRCASE_IN_SET(hostname, "_localdnsproxy", "_localdnsproxy.");
ac3a84
+}
ac3a84
+
ac3a84
 int get_pretty_hostname(char **ret);
ac3a84
diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c
ac3a84
index b07761a495..2a7347ca27 100644
ac3a84
--- a/src/resolve/resolvectl.c
ac3a84
+++ b/src/resolve/resolvectl.c
ac3a84
@@ -478,7 +478,11 @@ static bool single_label_nonsynthetic(const char *name) {
ac3a84
         if (!dns_name_is_single_label(name))
ac3a84
                 return false;
ac3a84
 
ac3a84
-        if (is_localhost(name) || is_gateway_hostname(name))
ac3a84
+        if (is_localhost(name) ||
ac3a84
+            is_gateway_hostname(name) ||
ac3a84
+            is_outbound_hostname(name) ||
ac3a84
+            is_dns_stub_hostname(name) ||
ac3a84
+            is_dns_proxy_stub_hostname(name))
ac3a84
                 return false;
ac3a84
 
ac3a84
         r = resolve_system_hostname(NULL, &first_label);
ac3a84
diff --git a/src/resolve/resolved-dns-scope.c b/src/resolve/resolved-dns-scope.c
ac3a84
index 4f744499aa..607109ee0f 100644
ac3a84
--- a/src/resolve/resolved-dns-scope.c
ac3a84
+++ b/src/resolve/resolved-dns-scope.c
ac3a84
@@ -635,8 +635,11 @@ DnsScopeMatch dns_scope_good_domain(
ac3a84
         if (dns_name_dont_resolve(domain))
ac3a84
                 return DNS_SCOPE_NO;
ac3a84
 
ac3a84
-        /* Never go to network for the _gateway or _outbound domain — they're something special, synthesized locally. */
ac3a84
-        if (is_gateway_hostname(domain) || is_outbound_hostname(domain))
ac3a84
+        /* Never go to network for the _gateway, _outbound, _localdnsstub, _localdnsproxy domain — they're something special, synthesized locally. */
ac3a84
+        if (is_gateway_hostname(domain) ||
ac3a84
+            is_outbound_hostname(domain) ||
ac3a84
+            is_dns_stub_hostname(domain) ||
ac3a84
+            is_dns_proxy_stub_hostname(domain))
ac3a84
                 return DNS_SCOPE_NO;
ac3a84
 
ac3a84
         switch (s->protocol) {
ac3a84
diff --git a/src/resolve/resolved-dns-synthesize.c b/src/resolve/resolved-dns-synthesize.c
ac3a84
index b3442ad906..fa8b4a5760 100644
ac3a84
--- a/src/resolve/resolved-dns-synthesize.c
ac3a84
+++ b/src/resolve/resolved-dns-synthesize.c
ac3a84
@@ -356,7 +356,90 @@ static int synthesize_gateway_rr(
ac3a84
         return 1; /* > 0 means: we have some gateway */
ac3a84
 }
ac3a84
 
ac3a84
-static int synthesize_gateway_ptr(Manager *m, int af, const union in_addr_union *address, int ifindex, DnsAnswer **answer) {
ac3a84
+static int synthesize_dns_stub_rr(
ac3a84
+                Manager *m,
ac3a84
+                const DnsResourceKey *key,
ac3a84
+                in_addr_t addr,
ac3a84
+                DnsAnswer **answer) {
ac3a84
+
ac3a84
+        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
ac3a84
+        int r;
ac3a84
+
ac3a84
+        assert(m);
ac3a84
+        assert(key);
ac3a84
+        assert(answer);
ac3a84
+
ac3a84
+        if (!IN_SET(key->type, DNS_TYPE_A, DNS_TYPE_ANY))
ac3a84
+                return 1; /* we still consider ourselves the owner of this name */
ac3a84
+
ac3a84
+        r = dns_answer_reserve(answer, 1);
ac3a84
+        if (r < 0)
ac3a84
+                return r;
ac3a84
+
ac3a84
+        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, dns_resource_key_name(key));
ac3a84
+        if (!rr)
ac3a84
+                return -ENOMEM;
ac3a84
+
ac3a84
+        rr->a.in_addr.s_addr = htobe32(addr);
ac3a84
+
ac3a84
+        r = dns_answer_add(*answer, rr, LOOPBACK_IFINDEX, DNS_ANSWER_AUTHENTICATED, NULL);
ac3a84
+        if (r < 0)
ac3a84
+                return r;
ac3a84
+
ac3a84
+        return 1;
ac3a84
+}
ac3a84
+
ac3a84
+static int synthesize_dns_stub_ptr(
ac3a84
+                Manager *m,
ac3a84
+                int af,
ac3a84
+                const union in_addr_union *address,
ac3a84
+                DnsAnswer **answer) {
ac3a84
+
ac3a84
+        int r;
ac3a84
+
ac3a84
+        assert(m);
ac3a84
+        assert(address);
ac3a84
+        assert(answer);
ac3a84
+
ac3a84
+        if (af != AF_INET)
ac3a84
+                return 0;
ac3a84
+
ac3a84
+        if (address->in.s_addr == htobe32(INADDR_DNS_STUB)) {
ac3a84
+
ac3a84
+                r = dns_answer_reserve(answer, 1);
ac3a84
+                if (r < 0)
ac3a84
+                        return r;
ac3a84
+
ac3a84
+                r = answer_add_ptr(answer, "53.0.0.127.in-addr.arpa", "_localdnsstub", LOOPBACK_IFINDEX, DNS_ANSWER_AUTHENTICATED);
ac3a84
+                if (r < 0)
ac3a84
+                        return r;
ac3a84
+
ac3a84
+                return 1;
ac3a84
+        }
ac3a84
+
ac3a84
+        if (address->in.s_addr == htobe32(INADDR_DNS_PROXY_STUB)) {
ac3a84
+
ac3a84
+                r = dns_answer_reserve(answer, 1);
ac3a84
+                if (r < 0)
ac3a84
+                        return r;
ac3a84
+
ac3a84
+                r = answer_add_ptr(answer, "54.0.0.127.in-addr.arpa", "_localdnsproxy", LOOPBACK_IFINDEX, DNS_ANSWER_AUTHENTICATED);
ac3a84
+                if (r < 0)
ac3a84
+                        return r;
ac3a84
+
ac3a84
+                return 1;
ac3a84
+        }
ac3a84
+
ac3a84
+        return 0;
ac3a84
+}
ac3a84
+
ac3a84
+static int synthesize_gateway_ptr(
ac3a84
+                Manager *m,
ac3a84
+                int af,
ac3a84
+                const union in_addr_union *address,
ac3a84
+                int ifindex,
ac3a84
+                DnsAnswer **answer) {
ac3a84
+
ac3a84
         _cleanup_free_ struct local_address *addresses = NULL;
ac3a84
         int n;
ac3a84
 
ac3a84
@@ -437,7 +520,22 @@ int dns_synthesize_answer(
ac3a84
                                 continue;
ac3a84
                         }
ac3a84
 
ac3a84
-                } else if ((dns_name_endswith(name, "127.in-addr.arpa") > 0 && dns_name_equal(name, "2.0.0.127.in-addr.arpa") == 0) ||
ac3a84
+                } else if (is_dns_stub_hostname(name)) {
ac3a84
+
ac3a84
+                        r = synthesize_dns_stub_rr(m, key, INADDR_DNS_STUB, &answer);
ac3a84
+                        if (r < 0)
ac3a84
+                                return log_error_errno(r, "Failed to synthesize local DNS stub RRs: %m");
ac3a84
+
ac3a84
+                } else if (is_dns_proxy_stub_hostname(name)) {
ac3a84
+
ac3a84
+                        r = synthesize_dns_stub_rr(m, key, INADDR_DNS_PROXY_STUB, &answer);
ac3a84
+                        if (r < 0)
ac3a84
+                                return log_error_errno(r, "Failed to synthesize local DNS stub RRs: %m");
ac3a84
+
ac3a84
+                } else if ((dns_name_endswith(name, "127.in-addr.arpa") > 0 &&
ac3a84
+                            dns_name_equal(name, "2.0.0.127.in-addr.arpa") == 0 &&
ac3a84
+                            dns_name_equal(name, "53.0.0.127.in-addr.arpa") == 0 &&
ac3a84
+                            dns_name_equal(name, "54.0.0.127.in-addr.arpa") == 0) ||
ac3a84
                            dns_name_equal(name, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa") > 0) {
ac3a84
 
ac3a84
                         r = synthesize_localhost_ptr(m, key, ifindex, &answer);
ac3a84
@@ -445,7 +543,7 @@ int dns_synthesize_answer(
ac3a84
                                 return log_error_errno(r, "Failed to synthesize localhost PTR RRs: %m");
ac3a84
 
ac3a84
                 } else if (dns_name_address(name, &af, &address) > 0) {
ac3a84
-                        int v, w;
ac3a84
+                        int v, w, u;
ac3a84
 
ac3a84
                         if (getenv_bool("SYSTEMD_RESOLVED_SYNTHESIZE_HOSTNAME") == 0)
ac3a84
                                 continue;
ac3a84
@@ -458,7 +556,11 @@ int dns_synthesize_answer(
ac3a84
                         if (w < 0)
ac3a84
                                 return log_error_errno(w, "Failed to synthesize gateway hostname PTR RR: %m");
ac3a84
 
ac3a84
-                        if (v == 0 && w == 0) /* This IP address is neither a local one nor a gateway */
ac3a84
+                        u = synthesize_dns_stub_ptr(m, af, &address, &answer);
ac3a84
+                        if (u < 0)
ac3a84
+                                return log_error_errno(u, "Failed to synthesize local stub hostname PTR PR: %m");
ac3a84
+
ac3a84
+                        if (v == 0 && w == 0 && u == 0) /* This IP address is neither a local one, nor a gateway, nor a stub address */
ac3a84
                                 continue;
ac3a84
 
ac3a84
                         /* Note that we never synthesize reverse PTR for _outbound, since those are local
ac3a84
diff --git a/test/units/testsuite-75.sh b/test/units/testsuite-75.sh
ac3a84
index 1a656fcdc1..0c68e0636f 100755
ac3a84
--- a/test/units/testsuite-75.sh
ac3a84
+++ b/test/units/testsuite-75.sh
ac3a84
@@ -56,6 +56,17 @@ echo nameserver 10.0.3.3 10.0.3.4 | "$RESOLVCONF" -a hoge.foo.dhcp
ac3a84
 assert_in '10.0.3.1 10.0.3.2' "$(resolvectl dns hoge)"
ac3a84
 assert_in '10.0.3.3 10.0.3.4' "$(resolvectl dns hoge.foo)"
ac3a84
 
ac3a84
+# Tests for _localdnsstub and _localdnsproxy
ac3a84
+assert_in '127.0.0.53' "$(resolvectl query _localdnsstub)"
ac3a84
+assert_in '_localdnsstub' "$(resolvectl query 127.0.0.53)"
ac3a84
+assert_in '127.0.0.54' "$(resolvectl query _localdnsproxy)"
ac3a84
+assert_in '_localdnsproxy' "$(resolvectl query 127.0.0.54)"
ac3a84
+
ac3a84
+assert_in '127.0.0.53' "$(dig @127.0.0.53 _localdnsstub)"
ac3a84
+assert_in '_localdnsstub' "$(dig @127.0.0.53 -x 127.0.0.53)"
ac3a84
+assert_in '127.0.0.54' "$(dig @127.0.0.53 _localdnsproxy)"
ac3a84
+assert_in '_localdnsproxy' "$(dig @127.0.0.53 -x 127.0.0.54)"
ac3a84
+
ac3a84
 # Tests for mDNS and LLMNR settings
ac3a84
 mkdir -p /run/systemd/resolved.conf.d
ac3a84
 {