Skip to content

Commit 239ce53

Browse files
laanwjcodablock
authored andcommitted
Merge bitcoin#8914: Kill insecure_random and associated global state
5eaaa83 Kill insecure_random and associated global state (Wladimir J. van der Laan)
1 parent 5fc2e19 commit 239ce53

29 files changed

Lines changed: 91 additions & 69 deletions

src/addrman.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
358358
int nKBucket = RandomInt(ADDRMAN_TRIED_BUCKET_COUNT);
359359
int nKBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
360360
while (vvTried[nKBucket][nKBucketPos] == -1) {
361-
nKBucket = (nKBucket + insecure_rand()) % ADDRMAN_TRIED_BUCKET_COUNT;
362-
nKBucketPos = (nKBucketPos + insecure_rand()) % ADDRMAN_BUCKET_SIZE;
361+
nKBucket = (nKBucket + insecure_rand.rand32()) % ADDRMAN_TRIED_BUCKET_COUNT;
362+
nKBucketPos = (nKBucketPos + insecure_rand.rand32()) % ADDRMAN_BUCKET_SIZE;
363363
}
364364
int nId = vvTried[nKBucket][nKBucketPos];
365365
assert(mapInfo.count(nId) == 1);
@@ -375,8 +375,8 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
375375
int nUBucket = RandomInt(ADDRMAN_NEW_BUCKET_COUNT);
376376
int nUBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
377377
while (vvNew[nUBucket][nUBucketPos] == -1) {
378-
nUBucket = (nUBucket + insecure_rand()) % ADDRMAN_NEW_BUCKET_COUNT;
379-
nUBucketPos = (nUBucketPos + insecure_rand()) % ADDRMAN_BUCKET_SIZE;
378+
nUBucket = (nUBucket + insecure_rand.rand32()) % ADDRMAN_NEW_BUCKET_COUNT;
379+
nUBucketPos = (nUBucketPos + insecure_rand.rand32()) % ADDRMAN_BUCKET_SIZE;
380380
}
381381
int nId = vvNew[nUBucket][nUBucketPos];
382382
assert(mapInfo.count(nId) == 1);

src/addrman.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ class CAddrMan
211211
//! secret key to randomize bucket select with
212212
uint256 nKey;
213213

214+
//! Source of random numbers for randomization in inner loops
215+
FastRandomContext insecure_rand;
216+
214217
//! Find an entry.
215218
CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
216219

src/init.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,9 +1163,6 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
11631163

11641164
// ********************************************************* Step 4: application initialization: dir lock, daemonize, pidfile, debug log, seed insecure_rand()
11651165

1166-
// Initialize fast PRNG
1167-
seed_insecure_rand(false);
1168-
11691166
// Initialize elliptic curve code
11701167
ECC_Start();
11711168
globalVerifyHandle.reset(new ECCVerifyHandle());

src/net.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ void AdvertiseLocal(CNode *pnode)
194194
if (addrLocal.IsRoutable())
195195
{
196196
LogPrint("net", "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
197-
pnode->PushAddress(addrLocal);
197+
FastRandomContext insecure_rand;
198+
pnode->PushAddress(addrLocal, insecure_rand);
198199
}
199200
}
200201
}

src/net.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,14 +867,14 @@ class CNode
867867
addrKnown.insert(_addr.GetKey());
868868
}
869869

870-
void PushAddress(const CAddress& _addr)
870+
void PushAddress(const CAddress& _addr, FastRandomContext &insecure_rand)
871871
{
872872
// Known checking here is only to save space from duplicates.
873873
// SendMessages will filter it again for knowns that were added
874874
// after addresses were pushed.
875875
if (_addr.IsValid() && !addrKnown.contains(_addr.GetKey())) {
876876
if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) {
877-
vAddrToSend[insecure_rand() % vAddrToSend.size()] = _addr;
877+
vAddrToSend[insecure_rand.rand32() % vAddrToSend.size()] = _addr;
878878
} else {
879879
vAddrToSend.push_back(_addr);
880880
}

src/net_processing.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ static void RelayAddress(const CAddress& addr, bool fReachable, CConnman& connma
820820
uint64_t hashAddr = addr.GetHash();
821821
multimap<uint64_t, CNode*> mapMix;
822822
const CSipHasher hasher = connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
823+
FastRandomContext insecure_rand;
823824

824825
auto sortfunc = [&mapMix, &hasher](CNode* pnode) {
825826
if (pnode->nVersion >= CADDR_TIME_VERSION) {
@@ -828,9 +829,9 @@ static void RelayAddress(const CAddress& addr, bool fReachable, CConnman& connma
828829
}
829830
};
830831

831-
auto pushfunc = [&addr, &mapMix, &nRelayNodes] {
832+
auto pushfunc = [&addr, &mapMix, &nRelayNodes, &insecure_rand] {
832833
for (auto mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
833-
mi->second->PushAddress(addr);
834+
mi->second->PushAddress(addr, insecure_rand);
834835
};
835836

836837
connman.ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
@@ -1248,14 +1249,15 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
12481249
if (fListen && !IsInitialBlockDownload())
12491250
{
12501251
CAddress addr = GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices());
1252+
FastRandomContext insecure_rand;
12511253
if (addr.IsRoutable())
12521254
{
12531255
LogPrint("net", "ProcessMessages: advertising address %s\n", addr.ToString());
1254-
pfrom->PushAddress(addr);
1256+
pfrom->PushAddress(addr, insecure_rand);
12551257
} else if (IsPeerAddrLocalGood(pfrom)) {
12561258
addr.SetIP(pfrom->addrLocal);
12571259
LogPrint("net", "ProcessMessages: advertising address %s\n", addr.ToString());
1258-
pfrom->PushAddress(addr);
1260+
pfrom->PushAddress(addr, insecure_rand);
12591261
}
12601262
}
12611263

@@ -2022,8 +2024,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
20222024

20232025
pfrom->vAddrToSend.clear();
20242026
vector<CAddress> vAddr = connman.GetAddresses();
2027+
FastRandomContext insecure_rand;
20252028
BOOST_FOREACH(const CAddress &addr, vAddr)
2026-
pfrom->PushAddress(addr);
2029+
pfrom->PushAddress(addr, insecure_rand);
20272030
}
20282031

20292032

@@ -2935,7 +2938,7 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
29352938
// until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
29362939
else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->nextSendTimeFeeFilter &&
29372940
(currentFilter < 3 * pto->lastSentFeeFilter / 4 || currentFilter > 4 * pto->lastSentFeeFilter / 3)) {
2938-
pto->nextSendTimeFeeFilter = timeNow + (insecure_rand() % MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
2941+
pto->nextSendTimeFeeFilter = timeNow + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
29392942
}
29402943
}
29412944
}

src/netbase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ static bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDe
592592
// do socks negotiation
593593
if (proxy.randomize_credentials) {
594594
ProxyCredentials random_auth;
595-
random_auth.username = strprintf("%i", insecure_rand());
596-
random_auth.password = strprintf("%i", insecure_rand());
595+
static std::atomic_int counter;
596+
random_auth.username = random_auth.password = strprintf("%i", counter++);
597597
if (!Socks5(strDest, (unsigned short)port, &random_auth, hSocket))
598598
return false;
599599
} else {

src/policy/fees.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ FeeFilterRounder::FeeFilterRounder(const CFeeRate& minIncrementalFee)
592592
CAmount FeeFilterRounder::round(CAmount currentMinFee)
593593
{
594594
std::set<double>::iterator it = feeset.lower_bound(currentMinFee);
595-
if ((it != feeset.begin() && insecure_rand() % 3 != 0) || it == feeset.end()) {
595+
if ((it != feeset.begin() && insecure_rand.rand32() % 3 != 0) || it == feeset.end()) {
596596
it--;
597597
}
598598
return *it;

src/policy/fees.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "amount.h"
99
#include "uint256.h"
10+
#include "random.h"
1011

1112
#include <map>
1213
#include <string>
@@ -298,5 +299,6 @@ class FeeFilterRounder
298299

299300
private:
300301
std::set<double> feeset;
302+
FastRandomContext insecure_rand;
301303
};
302304
#endif /*BITCOIN_POLICYESTIMATOR_H */

src/random.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,21 @@ uint256 GetRandHash()
178178
return hash;
179179
}
180180

181-
uint32_t insecure_rand_Rz = 11;
182-
uint32_t insecure_rand_Rw = 11;
183-
void seed_insecure_rand(bool fDeterministic)
181+
FastRandomContext::FastRandomContext(bool fDeterministic)
184182
{
185183
// The seed values have some unlikely fixed points which we avoid.
186184
if (fDeterministic) {
187-
insecure_rand_Rz = insecure_rand_Rw = 11;
185+
Rz = Rw = 11;
188186
} else {
189187
uint32_t tmp;
190188
do {
191189
GetRandBytes((unsigned char*)&tmp, 4);
192190
} while (tmp == 0 || tmp == 0x9068ffffU);
193-
insecure_rand_Rz = tmp;
191+
Rz = tmp;
194192
do {
195193
GetRandBytes((unsigned char*)&tmp, 4);
196194
} while (tmp == 0 || tmp == 0x464fffffU);
197-
insecure_rand_Rw = tmp;
195+
Rw = tmp;
198196
}
199197
}
200198

0 commit comments

Comments
 (0)