Skip to content

Commit c2b6c8a

Browse files
committed
Avoid uninitialized "random" bytes on Windows.
__swift_size_t on Windows is a size_t, which makes it potentially a 64-bit integer. ULONG, however, is always a 32-bit integer, and so this cast risks shrinking the apparent size of the cbBuffer argument to BCryptGenRandom. The effect of that will be to underfill the buffer, leaving it full of uninitialized memory that we would treat as random. The actual risk from this in the current implementation is basically zero, as user code can only ever invoke this with an argument size of 8. There's no good reason to leave this sharp edge on the API though.
1 parent 755c02c commit c2b6c8a

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

stdlib/public/stubs/Random.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ void swift::swift_stdlib_random(void *buf, __swift_size_t nbytes) {
5454

5555
SWIFT_RUNTIME_STDLIB_API
5656
void swift::swift_stdlib_random(void *buf, __swift_size_t nbytes) {
57+
if (nbytes > ULONG_MAX) {
58+
fatalError(0, "Fatal error: %zd exceeds ULONG_MAX\n", nbytes);
59+
}
60+
5761
NTSTATUS status = BCryptGenRandom(nullptr,
5862
static_cast<PUCHAR>(buf),
5963
static_cast<ULONG>(nbytes),

0 commit comments

Comments
 (0)