From 397fa8ccb5cc298684d8338ef29693051499dcd8 Mon Sep 17 00:00:00 2001 From: Mike Perry Date: Tue, 10 Sep 2013 17:00:27 -0700 Subject: [PATCH] Bug #5741: Prevent WebSocket DNS leak. This is due to an improper implementation of the WebSocket spec by Mozilla. "There MUST be no more than one connection in a CONNECTING state. If multiple connections to the same IP address are attempted simultaneously, the client MUST serialize them so that there is no more than one connection at a time running through the following steps. If the client cannot determine the IP address of the remote host (for example, because all communication is being done through a proxy server that performs DNS queries itself), then the client MUST assume for the purposes of this step that each host name refers to a distinct remote host," https://tools.ietf.org/html/rfc6455#page-15 They implmented the first paragraph, but not the second... While we're at it, we also prevent the DNS service from being used to look up anything other than IP addresses if socks_remote_dns is set to true, so this bug can't turn up in other components or due to 3rd party addons. --- netwerk/dns/nsDNSService2.cpp | 29 ++++++++++++++++++++++++++--- netwerk/dns/nsDNSService2.h | 1 + 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/netwerk/dns/nsDNSService2.cpp b/netwerk/dns/nsDNSService2.cpp index 05831139ecec4..73daac944a326 100644 --- a/netwerk/dns/nsDNSService2.cpp +++ b/netwerk/dns/nsDNSService2.cpp @@ -537,6 +537,7 @@ nsDNSService::Init() bool disableIPv6 = false; bool offlineLocalhost = true; bool disablePrefetch = false; + bool disableDNS = false; bool blockDotOnion = true; int proxyType = nsIProtocolProxyService::PROXYCONFIG_DIRECT; bool notifyResolution = false; @@ -565,6 +566,11 @@ nsDNSService::Init() // If a manual proxy is in use, disable prefetch implicitly prefs->GetIntPref("network.proxy.type", &proxyType); + + // If the user wants remote DNS, we should fail any lookups that still + // make it here. + prefs->GetBoolPref("network.proxy.socks_remote_dns", &disableDNS); + prefs->GetBoolPref(kPrefDnsNotifyResolution, ¬ifyResolution); if (mFirstTime) { @@ -584,7 +590,7 @@ nsDNSService::Init() // Monitor these to see if there is a change in proxy configuration // If a manual proxy is in use, disable prefetch implicitly - prefs->AddObserver("network.proxy.type", this, false); + prefs->AddObserver("network.proxy.", this, false); } } @@ -612,6 +618,7 @@ nsDNSService::Init() mIPv4OnlyDomains = ipv4OnlyDomains; // exchanges buffer ownership mOfflineLocalhost = offlineLocalhost; mDisableIPv6 = disableIPv6; + mDisableDNS = disableDNS; mBlockDotOnion = blockDotOnion; // Disable prefetching either by explicit preference or if a manual proxy is configured @@ -754,6 +761,14 @@ nsDNSService::AsyncResolveExtended(const nsACString &aHostname, NS_DispatchToMainThread(new NotifyDNSResolution(aHostname)); } + PRNetAddr tempAddr; + if (mDisableDNS) { + // Allow IP lookups through, but nothing else. + if (PR_StringToNetAddr(aHostname.BeginReading(), &tempAddr) != PR_SUCCESS) { + return NS_ERROR_UNKNOWN_PROXY_HOST; // XXX: NS_ERROR_NOT_IMPLEMENTED? + } + } + if (!res) return NS_ERROR_OFFLINE; @@ -883,6 +898,14 @@ nsDNSService::Resolve(const nsACString &aHostname, flags |= RESOLVE_OFFLINE; } + PRNetAddr tempAddr; + if (mDisableDNS) { + // Allow IP lookups through, but nothing else. + if (PR_StringToNetAddr(aHostname.BeginReading(), &tempAddr) != PR_SUCCESS) { + return NS_ERROR_UNKNOWN_PROXY_HOST; // XXX: NS_ERROR_NOT_IMPLEMENTED? + } + } + // // sync resolve: since the host resolver only works asynchronously, we need // to use a mutex and a condvar to wait for the result. however, since the @@ -890,7 +913,7 @@ nsDNSService::Resolve(const nsACString &aHostname, // on the same thread. so, our mutex needs to be re-entrant. in other words, // we need to use a monitor! ;-) // - + PRMonitor *mon = PR_NewMonitor(); if (!mon) return NS_ERROR_OUT_OF_MEMORY; @@ -990,7 +1013,7 @@ nsDNSService::GetAFForLookup(const nsACString &host, uint32_t flags) // see if host is in one of the IPv4-only domains domain = mIPv4OnlyDomains.BeginReading(); - domainEnd = mIPv4OnlyDomains.EndReading(); + domainEnd = mIPv4OnlyDomains.EndReading(); nsACString::const_iterator hostStart; host.BeginReading(hostStart); diff --git a/netwerk/dns/nsDNSService2.h b/netwerk/dns/nsDNSService2.h index 79454b901b3f9..11c8016311344 100644 --- a/netwerk/dns/nsDNSService2.h +++ b/netwerk/dns/nsDNSService2.h @@ -64,6 +64,7 @@ private: bool mDisablePrefetch; bool mBlockDotOnion; bool mFirstTime; + bool mDisableDNS; bool mNotifyResolution; bool mOfflineLocalhost; nsTHashtable mLocalDomains; -- GitLab