]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Chain on ECS matching, and consider a mismatch in returned ECS as a spoof attempt
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 15 Jan 2025 13:23:04 +0000 (14:23 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 28 May 2025 13:56:03 +0000 (15:56 +0200)
pdns/recursordist/lwres.cc
pdns/recursordist/lwres.hh
pdns/recursordist/pdns_recursor.cc
pdns/recursordist/syncres.hh

index 3a230ec40c45405338526c839838676b6ab272be..7d2e71afa133b526fd850ce8579fdafbc5ae555f 100644 (file)
@@ -411,7 +411,6 @@ static LWResult::Result asyncresolve(const ComboAddress& address, const DNSName&
   pw.getHeader()->cd = (sendRDQuery && g_dnssecmode != DNSSECMode::Off);
 
   string ping;
-  bool weWantEDNSSubnet = false;
   std::optional<EDNSSubnetOpts> subnetOpts = std::nullopt;
   if (EDNS0Level > 0) {
     DNSPacketWriter::optvect_t opts;
@@ -419,7 +418,6 @@ static LWResult::Result asyncresolve(const ComboAddress& address, const DNSName&
       subnetOpts = EDNSSubnetOpts{};
       subnetOpts->source = *srcmask;
       opts.emplace_back(EDNSOptionCode::ECS, makeEDNSSubnetOptsString(*subnetOpts));
-      weWantEDNSSubnet = true;
     }
 
     if (dnsOverTLS && g_paddingOutgoing) {
@@ -488,7 +486,7 @@ static LWResult::Result asyncresolve(const ComboAddress& address, const DNSName&
 #endif /* HAVE_FSTRM */
 
     // sleep until we see an answer to this, interface to mtasker
-    ret = arecvfrom(buf, 0, address, len, qid, domain, type, queryfd, *now);
+    ret = arecvfrom(buf, 0, address, len, qid, domain, type, queryfd, subnetOpts, *now);
   }
   else {
     bool isNew;
@@ -582,11 +580,22 @@ static LWResult::Result asyncresolve(const ComboAddress& address, const DNSName&
     if (EDNS0Level > 0 && getEDNSOpts(mdp, &edo)) {
       lwr->d_haveEDNS = true;
 
-      if (weWantEDNSSubnet) {
+      // If we sent out ECS, we can also expect to see a return with or without ECS, the absent case is
+      // not handled explicitly. If we do see a ECS in the reply, the source part *must* match with
+      // what we sent out See https://www.rfc-editor.org/rfc/rfc7871#section-7.3
+      if (subnetOpts) {
         for (const auto& opt : edo.d_options) {
           if (opt.first == EDNSOptionCode::ECS) {
             EDNSSubnetOpts reso;
             if (getEDNSSubnetOptsFromString(opt.second, &reso)) {
+              if (!(reso.source == subnetOpts->source)) {
+                g_slogout->info(Logr::Notice, "Incoming ECS does not match outgoing",
+                                "server", Logging::Loggable(address),
+                                "qname", Logging::Loggable(domain),
+                                "outgoing", Logging::Loggable(subnetOpts->source),
+                                "incoming", Logging::Loggable(reso.source));
+                return LWResult::Result::Spoofed; // XXXXX OK?
+              }
               /* rfc7871 states that 0 "indicate[s] that the answer is suitable for all addresses in FAMILY",
                  so we might want to still pass the information along to be able to differentiate between
                  IPv4 and IPv6. Still I'm pretty sure it doesn't matter in real life, so let's not duplicate
index 6379291a029b547f82159687290c203d0088ac7f..c136b5952f08215a6ed4d14da4634eaadd5bc945 100644 (file)
@@ -88,6 +88,6 @@ struct EDNSSubnetOpts;
 LWResult::Result asendto(const void* data, size_t len, int flags, const ComboAddress& toAddress, uint16_t qid,
                          const DNSName& domain, uint16_t qtype, const std::optional<EDNSSubnetOpts>& ecs, int* fileDesc, timeval& now);
 LWResult::Result arecvfrom(PacketBuffer& packet, int flags, const ComboAddress& fromAddr, size_t& len, uint16_t qid,
-                           const DNSName& domain, uint16_t qtype, int fileDesc, const struct timeval& now);
+                           const DNSName& domain, uint16_t qtype, int fileDesc, const std::optional<EDNSSubnetOpts>& ecs, const struct timeval& now);
 
 LWResult::Result asyncresolve(const ComboAddress& ip, const DNSName& domain, int type, bool doTCP, bool sendRDQuery, int EDNS0Level, struct timeval* now, boost::optional<Netmask>& srcmask, boost::optional<const ResolveContext&> context, const std::shared_ptr<std::vector<std::unique_ptr<RemoteLogger>>>& outgoingLoggers, const std::shared_ptr<std::vector<std::unique_ptr<FrameStreamLogger>>>& fstrmLoggers, const std::set<uint16_t>& exportTypes, LWResult* res, bool* chained);
index 7c80fd3a75c754e965d92199eb835b07490914f2..08a5edaf6150f05bcda3635cdfb3b801faddf0fa 100644 (file)
@@ -276,11 +276,13 @@ LWResult::Result asendto(const void* data, size_t len, int /* flags */,
   pident->domain = domain;
   pident->remote = toAddress;
   pident->type = qtype;
-
+  if (ecs) {
+    pident->ecsSubnet = ecs->source;
+  }
   // We cannot merge ECS-enabled queries based on the ECS source only, as the scope
   // of the response might be narrower, so instead we do not chain ECS-enabled queries
   // at all.
-  if (!ecs) {
+  if (true || !ecs) {
     // See if there is an existing outstanding request we can chain on to, using partial equivalence
     // function looking for the same query (qname and qtype) to the same host, but with a different
     // message ID.
@@ -321,7 +323,7 @@ LWResult::Result asendto(const void* data, size_t len, int /* flags */,
 }
 
 LWResult::Result arecvfrom(PacketBuffer& packet, int /* flags */, const ComboAddress& fromAddr, size_t& len,
-                           uint16_t qid, const DNSName& domain, uint16_t qtype, int fileDesc, const struct timeval& now)
+                           uint16_t qid, const DNSName& domain, uint16_t qtype, int fileDesc, const std::optional<EDNSSubnetOpts>& ecs, const struct timeval& now)
 {
   static const unsigned int nearMissLimit = ::arg().asNum("spoof-nearmiss-max");
 
@@ -331,7 +333,14 @@ LWResult::Result arecvfrom(PacketBuffer& packet, int /* flags */, const ComboAdd
   pident->domain = domain;
   pident->type = qtype;
   pident->remote = fromAddr;
-
+  pident->creationTime = now;
+  if (ecs) {
+    // We sent out the query using ecs
+    // We expect incoming source ECS to match, see https://www.rfc-editor.org/rfc/rfc7871#section-7.3
+    // But there's also section 11-2, which says we should treat absent incoming ecs as scope zero
+    // We fill in the search key with the ecs we sent out, so both cases are covered and accepted here.
+    pident->ecsSubnet = ecs->source;
+  }
   int ret = g_multiTasker->waitEvent(pident, &packet, g_networkTimeoutMsec, &now);
   len = 0;
 
index 4298b8b3bd4c695741000f52e57f6bd09368136d..45f16e76a314e96ff4b37d171cfb24a904077aec 100644 (file)
@@ -762,6 +762,8 @@ struct PacketID
   using chain_t = set<uint16_t>;
   mutable chain_t chain;
   shared_ptr<TCPIOHandler> tcphandler{nullptr};
+  timeval creationTime{};
+  std::optional<Netmask> ecsSubnet;
   string::size_type inPos{0}; // how far are we along in the inMSG
   size_t inWanted{0}; // if this is set, we'll read until inWanted bytes are read
   string::size_type outPos{0}; // how far we are along in the outMSG
@@ -801,10 +803,10 @@ struct PacketIDCompare
 {
   bool operator()(const std::shared_ptr<PacketID>& lhs, const std::shared_ptr<PacketID>& rhs) const
   {
-    if (std::tie(lhs->remote, lhs->tcpsock, lhs->type) < std::tie(rhs->remote, rhs->tcpsock, rhs->type)) {
+    if (std::tie(lhs->remote, lhs->tcpsock, lhs->type, lhs->ecsSubnet) < std::tie(rhs->remote, rhs->tcpsock, rhs->type, rhs->ecsSubnet)) {
       return true;
     }
-    if (std::tie(lhs->remote, lhs->tcpsock, lhs->type) > std::tie(rhs->remote, rhs->tcpsock, rhs->type)) {
+    if (std::tie(lhs->remote, lhs->tcpsock, lhs->type, lhs->ecsSubnet) > std::tie(rhs->remote, rhs->tcpsock, rhs->type, rhs->ecsSubnet)) {
       return false;
     }
 
@@ -816,10 +818,10 @@ struct PacketIDBirthdayCompare
 {
   bool operator()(const std::shared_ptr<PacketID>& lhs, const std::shared_ptr<PacketID>& rhs) const
   {
-    if (std::tie(lhs->remote, lhs->tcpsock, lhs->type) < std::tie(rhs->remote, rhs->tcpsock, rhs->type)) {
+    if (std::tie(lhs->remote, lhs->tcpsock, lhs->type, lhs->ecsSubnet) < std::tie(rhs->remote, rhs->tcpsock, rhs->type, rhs->ecsSubnet)) {
       return true;
     }
-    if (std::tie(lhs->remote, lhs->tcpsock, lhs->type) > std::tie(rhs->remote, rhs->tcpsock, rhs->type)) {
+    if (std::tie(lhs->remote, lhs->tcpsock, lhs->type, lhs->ecsSubnet) > std::tie(rhs->remote, rhs->tcpsock, rhs->type, rhs->ecsSubnet)) {
       return false;
     }
     return lhs->domain < rhs->domain;