]> granicus.if.org Git - pdns/commitdiff
auth: Unify usage of randomness source by using `dns_random()`
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 29 Aug 2016 15:28:35 +0000 (17:28 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 8 Dec 2016 09:55:07 +0000 (10:55 +0100)
`Utility::random()` is not impossible to predict, so even if we are not
using it for anything sensitive it's better to just use `dns_random()`
instead.
Reported by mongo (thanks!).

(cherry picked from commit d2116c15dbf1e0cef93e478678d1f9d403d87f90)

pdns/common_startup.cc
pdns/communicator.hh
pdns/dnspacket.cc
pdns/dnsproxy.cc

index 5d3d4984dbcce909e37860e4e7a285f0eaad3b41..cba201bf45cce60368875fe0e6592c3edb3e1983 100644 (file)
@@ -473,7 +473,7 @@ static void triggerLoadOfLibraries()
 
 void mainthread()
 {
-  Utility::srandom(time(0));
+   Utility::srandom(time(0) ^ getpid());
 
    int newgid=0;      
    if(!::arg()["setgid"].empty()) 
index 9ddb0e67c23eacef5834a51444e493eecc54796a..065dae952ce62b838be504a9e6e80fe1c8355567 100644 (file)
@@ -42,6 +42,7 @@ using namespace boost::multi_index;
 #include "packethandler.hh"
 
 #include "namespaces.hh"
+#include "dns_random.hh"
 
 struct SuckRequest
 {
@@ -75,7 +76,7 @@ public:
     nr.domain   = domain;
     nr.ip       = caIp.toStringWithPort();
     nr.attempts = 0;
-    nr.id       = Utility::random()%0xffff;
+    nr.id       = dns_random(0xffff);
     nr.next     = time(0);
 
     d_nqueue.push_back(nr);
index b2d3b1c029ed09a00e7d760ba6e399db50af7e45..5689b395732aa65f9b02e804bbf53604db53f88a 100644 (file)
@@ -47,6 +47,7 @@
 #include "base64.hh"
 #include "ednssubnet.hh"
 #include "gss_context.hh"
+#include "dns_random.hh"
 
 bool DNSPacket::s_doEDNSSubnetProcessing;
 uint16_t DNSPacket::s_udpTruncationThreshold;
@@ -377,7 +378,7 @@ void DNSPacket::wrapup()
 void DNSPacket::setQuestion(int op, const DNSName &qd, int newqtype)
 {
   memset(&d,0,sizeof(d));
-  d.id=Utility::random();
+  d.id=dns_random(0xffff);
   d.rd=d.tc=d.aa=false;
   d.qr=false;
   d.qdcount=1; // is htons'ed later on
index f7061c0989722e5af19a8d11d14d9ea10d75a813..b0f3ee569d0bb8d1b5e695e0c271a6f8f473304d 100644 (file)
@@ -31,7 +31,7 @@
 #include "dns.hh"
 #include "logger.hh"
 #include "statbag.hh"
-
+#include "dns_random.hh"
 
 extern StatBag S;
 extern PacketCache PC;
@@ -55,7 +55,7 @@ DNSProxy::DNSProxy(const string &remote)
     
   int n=0;
   for(;n<10;n++) {
-    local.sin4.sin_port = htons(10000+( Utility::random()%50000));
+    local.sin4.sin_port = htons(10000+dns_random(50000));
     
     if(::bind(d_sock, (struct sockaddr *)&local, local.getSocklen()) >= 0) 
       break;
@@ -69,7 +69,7 @@ DNSProxy::DNSProxy(const string &remote)
   if(connect(d_sock, (sockaddr *)&remaddr, remaddr.getSocklen())<0) 
     throw PDNSException("Unable to UDP connect to remote nameserver "+remaddr.toStringWithPort()+": "+stringerror());
 
-  d_xor=Utility::random()&0xffff;
+  d_xor=dns_random(0xffff);
   L<<Logger::Error<<"DNS Proxy launched, local port "<<ntohs(local.sin4.sin_port)<<", remote "<<remaddr.toStringWithPort()<<endl;
 }