Skip to content

Commit 08dd6a2

Browse files
Bug 1690111 - Improve nsIRandomGenerator APIs. r=mccr8,necko-reviewers,kershaw
Differential Revision: https://phabricator.services.mozilla.com/D185067
1 parent 41381dd commit 08dd6a2

File tree

8 files changed

+46
-42
lines changed

8 files changed

+46
-42
lines changed

dom/base/Crypto.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,13 @@ void Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
7878
return;
7979
}
8080

81-
uint8_t* buf;
82-
nsresult rv = randomGenerator->GenerateRandomBytes(dataLen, &buf);
83-
if (NS_FAILED(rv) || !buf) {
81+
nsresult rv =
82+
randomGenerator->GenerateRandomBytesInto(aArray.Data(), dataLen);
83+
if (NS_FAILED(rv)) {
8484
aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
8585
return;
8686
}
8787

88-
// Copy random bytes to ABV.
89-
memcpy(aArray.Data(), buf, dataLen);
90-
free(buf);
91-
9288
aRetval.set(view);
9389
}
9490

dom/fetch/InternalResponse.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,7 @@ nsresult InternalResponse::GeneratePaddingInfo() {
331331

332332
MOZ_DIAGNOSTIC_ASSERT(randomGenerator);
333333

334-
uint8_t* buffer;
335-
rv = randomGenerator->GenerateRandomBytes(sizeof(randomNumber), &buffer);
334+
rv = randomGenerator->GenerateRandomBytesInto(randomNumber);
336335
if (NS_WARN_IF(NS_FAILED(rv))) {
337336
Maybe<uint64_t> maybeRandomNum = RandomUint64();
338337
if (maybeRandomNum.isSome()) {
@@ -342,9 +341,6 @@ nsresult InternalResponse::GeneratePaddingInfo() {
342341
return rv;
343342
}
344343

345-
memcpy(&randomNumber, buffer, sizeof(randomNumber));
346-
free(buffer);
347-
348344
mPaddingInfo.emplace(randomNumber % kMaxRandomNumber);
349345

350346
return rv;

dom/reporting/ReportingHeader.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,16 +546,11 @@ void ReportingHeader::GetEndpointForReportInternal(
546546

547547
uint32_t randomNumber = 0;
548548

549-
uint8_t* buffer;
550-
nsresult rv =
551-
randomGenerator->GenerateRandomBytes(sizeof(randomNumber), &buffer);
549+
nsresult rv = randomGenerator->GenerateRandomBytesInto(randomNumber);
552550
if (NS_WARN_IF(NS_FAILED(rv))) {
553551
return;
554552
}
555553

556-
memcpy(&randomNumber, buffer, sizeof(randomNumber));
557-
free(buffer);
558-
559554
totalWeight = randomNumber % totalWeight;
560555

561556
const auto [begin, end] = aGroup.mEndpoints.NonObservingRange();

netwerk/base/nsIRandomGenerator.idl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
#include "nsISupports.idl"
66

7+
%{C++
8+
#include <type_traits>
9+
%}
10+
711
/**
812
* Interface used to generate random data.
913
*
@@ -21,4 +25,22 @@ interface nsIRandomGenerator : nsISupports {
2125
*/
2226
void generateRandomBytes(in unsigned long aLength,
2327
[retval, array, size_is(aLength)] out octet aBuffer);
28+
29+
/**
30+
* Fills aBuffer with random bytes.
31+
*
32+
* @param aBuffer
33+
* A buffer to fill with random bytes.
34+
* @param aLength
35+
* Length of aBuffer.
36+
*/
37+
void generateRandomBytesInto([array, size_is(aLength)] in octet aBuffer,
38+
in unsigned long aLength);
39+
40+
%{C++
41+
template<typename T>
42+
std::enable_if_t<!std::is_pointer_v<T>, nsresult> GenerateRandomBytesInto(T& aResult) {
43+
return GenerateRandomBytesInto(reinterpret_cast<uint8_t*>(&aResult), sizeof(T));
44+
}
45+
%}
2446
};

netwerk/protocol/websocket/WebSocketChannel.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,10 +2134,8 @@ void WebSocketChannel::PrimeNewOutgoingMessage() {
21342134
if (!mIsServerSide) {
21352135
// Perform the sending mask. Never use a zero mask
21362136
do {
2137-
uint8_t* buffer;
21382137
static_assert(4 == sizeof(mask), "Size of the mask should be equal to 4");
2139-
nsresult rv =
2140-
mRandomGenerator->GenerateRandomBytes(sizeof(mask), &buffer);
2138+
nsresult rv = mRandomGenerator->GenerateRandomBytesInto(mask);
21412139
if (NS_FAILED(rv)) {
21422140
LOG(
21432141
("WebSocketChannel::PrimeNewOutgoingMessage(): "
@@ -2146,8 +2144,6 @@ void WebSocketChannel::PrimeNewOutgoingMessage() {
21462144
AbortSession(rv);
21472145
return;
21482146
}
2149-
memcpy(&mask, buffer, sizeof(mask));
2150-
free(buffer);
21512147
} while (!mask);
21522148
NetworkEndian::writeUint32(payload - sizeof(uint32_t), mask);
21532149
}

security/manager/ssl/nsRandomGenerator.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "pk11pub.h"
1010
#include "prerror.h"
1111
#include "secerr.h"
12+
#include "mozilla/UniquePtrExtensions.h"
1213

1314
NS_IMPL_ISUPPORTS(nsRandomGenerator, nsIRandomGenerator)
1415

@@ -17,20 +18,24 @@ nsRandomGenerator::GenerateRandomBytes(uint32_t aLength, uint8_t** aBuffer) {
1718
NS_ENSURE_ARG_POINTER(aBuffer);
1819
*aBuffer = nullptr;
1920

20-
mozilla::UniquePK11SlotInfo slot(PK11_GetInternalSlot());
21-
if (!slot) {
22-
return NS_ERROR_FAILURE;
23-
}
21+
mozilla::UniqueFreePtr<uint8_t> buf(
22+
static_cast<uint8_t*>(moz_xmalloc(aLength)));
23+
nsresult rv = GenerateRandomBytesInto(buf.get(), aLength);
24+
NS_ENSURE_SUCCESS(rv, rv);
2425

25-
auto buf = static_cast<uint8_t*>(moz_xmalloc(aLength));
26+
*aBuffer = buf.release();
27+
return NS_OK;
28+
}
2629

27-
SECStatus srv = PK11_GenerateRandomOnSlot(slot.get(), buf, aLength);
28-
if (srv != SECSuccess) {
29-
free(buf);
30+
NS_IMETHODIMP
31+
nsRandomGenerator::GenerateRandomBytesInto(uint8_t* aBuffer, uint32_t aLength) {
32+
NS_ENSURE_ARG_POINTER(aBuffer);
33+
34+
mozilla::UniquePK11SlotInfo slot(PK11_GetInternalSlot());
35+
if (!slot) {
3036
return NS_ERROR_FAILURE;
3137
}
3238

33-
*aBuffer = buf;
34-
35-
return NS_OK;
39+
SECStatus srv = PK11_GenerateRandomOnSlot(slot.get(), aBuffer, aLength);
40+
return srv == SECSuccess ? NS_OK : NS_ERROR_FAILURE;
3641
}

toolkit/components/resistfingerprinting/RelativeTimeline.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,11 @@ int64_t RelativeTimeline::GetRandomTimelineSeed() {
2121
return mRandomTimelineSeed;
2222
}
2323

24-
uint8_t* buffer = nullptr;
25-
rv = randomGenerator->GenerateRandomBytes(sizeof(mRandomTimelineSeed),
26-
&buffer);
24+
rv = randomGenerator->GenerateRandomBytesInto(mRandomTimelineSeed);
2725
if (NS_WARN_IF(NS_FAILED(rv))) {
2826
mRandomTimelineSeed = rand();
2927
return mRandomTimelineSeed;
3028
}
31-
32-
memcpy(&mRandomTimelineSeed, buffer, sizeof(mRandomTimelineSeed));
33-
MOZ_ASSERT(buffer);
34-
free(buffer);
3529
}
3630
return mRandomTimelineSeed;
3731
}

toolkit/components/resistfingerprinting/nsRFPService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ nsresult nsRFPService::RandomMidpoint(long long aClampedTimeUSec,
464464
}
465465
if (MOZ_UNLIKELY(!sSecretMidpointSeed.compareExchange(nullptr, temp))) {
466466
// Some other thread initted this first, never mind!
467-
delete[] temp;
467+
free(temp);
468468
}
469469
}
470470

0 commit comments

Comments
 (0)