[nodiscard] Fix all winpr_RAND usages

* Check return value and act on it.
* Initialize arrays that were missed before.
This commit is contained in:
Armin Novak
2026-02-27 07:33:51 +01:00
parent 56d4139e6d
commit 89ab3c6c1b
50 changed files with 378 additions and 190 deletions

View File

@@ -249,7 +249,11 @@ autodetect_send_bandwidth_measure_payload(rdpAutoDetect* autodetect,
Stream_Write_UINT16(s, RDP_BW_PAYLOAD_REQUEST_TYPE); /* requestType (2 bytes) */
Stream_Write_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
/* Random data (better measurement in case the line is compressed) */
winpr_RAND(Stream_Pointer(s), payloadLength);
if (winpr_RAND(Stream_Pointer(s), payloadLength) < 0)
{
Stream_Release(s);
return FALSE;
}
Stream_Seek(s, payloadLength);
return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
sec_flags | SEC_AUTODETECT_REQ);
@@ -304,7 +308,11 @@ static BOOL autodetect_send_bandwidth_measure_stop(rdpAutoDetect* autodetect,
}
/* Random data (better measurement in case the line is compressed) */
winpr_RAND(Stream_Pointer(s), payloadLength);
if (winpr_RAND(Stream_Pointer(s), payloadLength) < 0)
{
Stream_Release(s);
return FALSE;
}
Stream_Seek(s, payloadLength);
}
}

View File

@@ -735,7 +735,8 @@ static BOOL rdp_client_establish_keys(rdpRdp* rdp)
if (!freerdp_settings_set_pointer_len(settings, FreeRDP_ClientRandom, nullptr,
CLIENT_RANDOM_LENGTH))
return FALSE;
winpr_RAND(settings->ClientRandom, settings->ClientRandomLength);
if (winpr_RAND(settings->ClientRandom, settings->ClientRandomLength) < 0)
return FALSE;
const rdpCertInfo* info = freerdp_certificate_get_info(settings->RdpServerCertificate);
if (!info)

View File

@@ -518,7 +518,8 @@ static int rpc_channel_rpch_init(RpcClient* client, RpcChannel* channel, const c
settings = client->context->settings;
channel->auth = credssp_auth_new(client->context);
rts_generate_cookie((BYTE*)&channel->Cookie);
if (!rts_generate_cookie((BYTE*)&channel->Cookie))
return -1;
channel->client = client;
if (!channel->auth)
@@ -689,8 +690,10 @@ static RpcVirtualConnection* rpc_virtual_connection_new(rdpRpc* rpc)
if (!connection)
return nullptr;
rts_generate_cookie((BYTE*)&(connection->Cookie));
rts_generate_cookie((BYTE*)&(connection->AssociationGroupId));
if (!rts_generate_cookie((BYTE*)&(connection->Cookie)))
goto fail;
if (!rts_generate_cookie((BYTE*)&(connection->AssociationGroupId)))
goto fail;
connection->State = VIRTUAL_CONNECTION_STATE_INITIAL;
connection->DefaultInChannel = rpc_in_channel_new(rpc, &connection->Cookie);

View File

@@ -1576,10 +1576,10 @@ static BOOL rts_destination_command_write(wStream* s, UINT32 Destination)
return TRUE;
}
void rts_generate_cookie(BYTE* cookie)
BOOL rts_generate_cookie(BYTE* cookie)
{
WINPR_ASSERT(cookie);
winpr_RAND(cookie, 16);
return winpr_RAND(cookie, 16) >= 0;
}
#define rts_send_buffer(channel, s, frag_length) \

View File

@@ -77,7 +77,7 @@
#define FDServer 0x00000002
#define FDOutProxy 0x00000003
FREERDP_LOCAL void rts_generate_cookie(BYTE* cookie);
FREERDP_LOCAL BOOL rts_generate_cookie(BYTE* cookie);
WINPR_ATTR_NODISCARD
FREERDP_LOCAL BOOL rts_write_pdu_auth3(wStream* s, const rpcconn_rpc_auth_3_hdr_t* auth);

View File

@@ -88,13 +88,14 @@ wStream* websocket_context_packet_new(size_t len, WEBSOCKET_OPCODE opcode, UINT3
else
fullLen = len + 14; /* 2 byte "mini header" + 8 byte length + 4 byte masking key */
UINT32 maskingKey = 0;
if (winpr_RAND(&maskingKey, sizeof(maskingKey)) < 0)
return nullptr;
wStream* sWS = Stream_New(nullptr, fullLen);
if (!sWS)
return nullptr;
UINT32 maskingKey = 0;
winpr_RAND(&maskingKey, sizeof(maskingKey));
Stream_Write_UINT8(sWS, (UINT8)(WEBSOCKET_FIN_BIT | opcode));
if (len < 126)
Stream_Write_UINT8(sWS, (UINT8)len | WEBSOCKET_MASK_BIT);

View File

@@ -1792,8 +1792,7 @@ static BOOL gcc_update_server_random(rdpSettings* settings)
BYTE* data = freerdp_settings_get_pointer_writable(settings, FreeRDP_ServerRandom);
if (!data)
return FALSE;
winpr_RAND(data, length);
return TRUE;
return winpr_RAND(data, length) >= 0;
}
/* TODO: This function does manipulate data in rdpMcs

View File

@@ -298,7 +298,7 @@ static const char* licencse_blob_type_string(UINT16 type)
}
static wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags);
static void license_generate_randoms(rdpLicense* license);
static BOOL license_generate_randoms(rdpLicense* license);
static BOOL license_generate_keys(rdpLicense* license);
static BOOL license_generate_hwid(rdpLicense* license);
static BOOL license_encrypt_premaster_secret(rdpLicense* license);
@@ -1027,23 +1027,28 @@ fail:
return rc;
}
void license_generate_randoms(rdpLicense* license)
BOOL license_generate_randoms(rdpLicense* license)
{
WINPR_ASSERT(license);
#ifdef LICENSE_NULL_CLIENT_RANDOM
ZeroMemory(license->ClientRandom, sizeof(license->ClientRandom)); /* ClientRandom */
#else
winpr_RAND(license->ClientRandom, sizeof(license->ClientRandom)); /* ClientRandom */
if (winpr_RAND(license->ClientRandom, sizeof(license->ClientRandom)) < 0) /* ClientRandom */
return FALSE;
#endif
winpr_RAND(license->ServerRandom, sizeof(license->ServerRandom)); /* ServerRandom */
if (winpr_RAND(license->ServerRandom, sizeof(license->ServerRandom)) < 0) /* ServerRandom */
return FALSE;
#ifdef LICENSE_NULL_PREMASTER_SECRET
ZeroMemory(license->PremasterSecret, sizeof(license->PremasterSecret)); /* PremasterSecret */
#else
winpr_RAND(license->PremasterSecret, sizeof(license->PremasterSecret)); /* PremasterSecret */
if (winpr_RAND(license->PremasterSecret, sizeof(license->PremasterSecret)) <
0) /* PremasterSecret */
return FALSE;
#endif
return TRUE;
}
/**
@@ -2788,7 +2793,8 @@ rdpLicense* license_new(rdpRdp* rdp)
if (!(license->ScopeList = license_new_scope_list()))
goto out_error;
license_generate_randoms(license);
if (!license_generate_randoms(license))
goto out_error;
return license;

View File

@@ -125,7 +125,8 @@ state_run_t multitransport_server_request(rdpMultitransport* multi, UINT16 reqPr
if (reqProto == INITIATE_REQUEST_PROTOCOL_UDPFECR)
{
multi->reliableReqId = reqId++;
winpr_RAND(multi->reliableCookie, sizeof(multi->reliableCookie));
if (winpr_RAND(multi->reliableCookie, sizeof(multi->reliableCookie)) < 0)
return STATE_RUN_FAILED;
return multitransport_request_send(multi, multi->reliableReqId, reqProto,
multi->reliableCookie)

View File

@@ -707,13 +707,15 @@ out:
static char* create_temporary_file(void)
{
BYTE buffer[32];
char* hex = nullptr;
BYTE buffer[32] = WINPR_C_ARRAY_INIT;
char* path = nullptr;
winpr_RAND(buffer, sizeof(buffer));
hex = winpr_BinToHexString(buffer, sizeof(buffer), FALSE);
path = GetKnownSubPath(KNOWN_PATH_TEMP, hex);
if (winpr_RAND(buffer, sizeof(buffer)) < 0)
return nullptr;
char* hex = winpr_BinToHexString(buffer, sizeof(buffer), FALSE);
if (hex)
path = GetKnownSubPath(KNOWN_PATH_TEMP, hex);
free(hex);
return path;
}

View File

@@ -317,7 +317,8 @@ int TestConnect(int argc, char* argv[])
int random = 0;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
winpr_RAND(&random, sizeof(random));
if (winpr_RAND(&random, sizeof(random)) < 0)
return -1;
randomPort = 3389 + (random % 200);
/* Test connect to not existing server,

View File

@@ -579,8 +579,10 @@ static BOOL check_key_helpers(size_t key, const char* stype)
void* pv;
} val;
winpr_RAND(&intEntryType, sizeof(intEntryType));
winpr_RAND(&val.u64, sizeof(val.u64));
if (winpr_RAND(&intEntryType, sizeof(intEntryType)) < 0)
goto fail;
if (winpr_RAND(&val.u64, sizeof(val.u64)) < 0)
goto fail;
switch (key)
{
@@ -852,7 +854,8 @@ static BOOL test_write_offsets(rdpSettings* settings, size_t id, size_t elementS
const void* ptr = nullptr;
char buffer[8192] = WINPR_C_ARRAY_INIT;
winpr_RAND(buffer, sizeof(buffer));
if (winpr_RAND(buffer, sizeof(buffer)) < 0)
return FALSE;
if (!freerdp_settings_set_pointer_array(settings, id, x, buffer))
return FALSE;
ptr = freerdp_settings_get_pointer_array(settings, id, x);
@@ -1739,7 +1742,8 @@ static BOOL set_cert(rdpSettings* src, FreeRDP_Settings_Keys_Pointer key)
static BOOL set_string_array(rdpSettings* src, FreeRDP_Settings_Keys_Pointer key, uint32_t max)
{
uint32_t count = 0;
winpr_RAND(&count, sizeof(count));
if (winpr_RAND(&count, sizeof(count)) < 0)
return FALSE;
count = count % max;
if (!freerdp_settings_set_pointer_len(src, key, nullptr, count))
@@ -1815,7 +1819,8 @@ static BOOL test_serialize_pointer(DWORD flags)
goto fail;
void* ptr = nullptr;
winpr_RAND((void*)&ptr, sizeof(void*));
if (winpr_RAND((void*)&ptr, sizeof(void*)) < 0)
goto fail;
if (!freerdp_settings_set_pointer(src, FreeRDP_instance, ptr))
goto fail;
@@ -1844,7 +1849,8 @@ static BOOL test_serialize_pointer(DWORD flags)
for (size_t x = 0; x < ARRAYSIZE(addresses); x++)
{
uint32_t port = 0;
winpr_RAND(&port, sizeof(port));
if (winpr_RAND(&port, sizeof(port)) < 0)
goto fail;
if (!freerdp_settings_set_pointer_array(src, FreeRDP_TargetNetPorts, x, &port))
goto fail;
}
@@ -1856,14 +1862,16 @@ static BOOL test_serialize_pointer(DWORD flags)
void* caps = freerdp_settings_get_pointer_writable(src, FreeRDP_ReceivedCapabilities);
if (!caps)
goto fail;
winpr_RAND(caps, count);
if (winpr_RAND(caps, count) < 0)
goto fail;
for (uint32_t x = 0; x < count; x++)
{
uint8_t* buffer = calloc(64, sizeof(uint8_t));
if (!buffer)
goto fail;
winpr_RAND(buffer, sizeof(buffer));
if (winpr_RAND(buffer, sizeof(buffer)) < 0)
goto fail;
uint32_t blen = (buffer[0] % 52) + 13;
if (!freerdp_settings_set_pointer_array(src, FreeRDP_ReceivedCapabilityData, x, buffer))

View File

@@ -24,7 +24,8 @@ static BOOL test_entry_read_write(void)
size_t entrysize = sizeof(UINT64) /* timestamp */ + sizeof(BYTE) /* direction */ +
sizeof(UINT32) /* CRC */ + sizeof(UINT64) /* size */;
winpr_RAND(tmp, sizeof(tmp));
if (winpr_RAND(tmp, sizeof(tmp)) < 0)
goto fail;
for (size_t x = 0; x < sizeof(tmp); x++)
(void)_snprintf(&tmp2[x * 2], sizeof(tmp2) - 2 * x, "%02" PRIx8, tmp[x]);
@@ -44,7 +45,8 @@ static BOOL test_entry_read_write(void)
goto fail;
}
winpr_RAND(Stream_Buffer(sw), Stream_Capacity(sw));
if (winpr_RAND(Stream_Buffer(sw), Stream_Capacity(sw)) < 0)
goto fail;
entrysize += Stream_Capacity(sw);
Stream_SetLength(sw, Stream_Capacity(sw));