Fixes for SDLNativeRules issues (#578)

* SDL fixes

12650495
12699256
12698334
12699260
12699265
12725561
12699267
12650494
12680633
12725562
This commit is contained in:
Eric Wolz 2021-12-07 10:18:29 -08:00 коммит произвёл GitHub
Родитель ba79c6dede
Коммит 7ddf017f1c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
22 изменённых файлов: 370 добавлений и 91 удалений

Просмотреть файл

@ -78,7 +78,13 @@ LOCK_RESULT Unlock(LOCK_HANDLE handle)
}
else
{
#ifdef _MSC_VER
#pragma warning(disable:26110) // Warning C26110: Caller failing to hold lock 'handle' before calling function 'ReleaseSRWLockExclusive'.
#endif
ReleaseSRWLockExclusive((SRWLOCK*)handle);
#ifdef _MSC_VER
#pragma warning (default:26110)
#endif
/* Codes_SRS_LOCK_10_009: [Unlock on success shall return LOCK_OK] */
result = LOCK_OK;

Просмотреть файл

@ -171,7 +171,9 @@ STRING_HANDLE platform_get_platform_info(PLATFORM_INFO_OPTION options)
result = NULL;
memset(&osvi, 0, sizeof(osvi));
osvi.dwOSVersionInfoSize = sizeof(osvi);
#pragma warning(disable:4996)
#ifdef _MSC_VER
#pragma warning(disable:4996 28159) // GetVersionEx is deprecated
#endif
if (GetVersionEx(&osvi))
{
DWORD product_type;
@ -186,7 +188,9 @@ STRING_HANDLE platform_get_platform_info(PLATFORM_INFO_OPTION options)
DWORD dwVersion = GetVersion();
result = STRING_construct_sprintf("(native; WindowsProduct:Windows NT %d.%d; %s", LOBYTE(LOWORD(dwVersion)), HIBYTE(LOWORD(dwVersion)), arch);
}
#pragma warning(default:4996)
#ifdef _MSC_VER
#pragma warning(default:4996 28159)
#endif
if (result == NULL)
{

Просмотреть файл

@ -223,7 +223,13 @@ void srw_lock_release_exclusive(SRW_LOCK_HANDLE handle)
{
if (!handle->doStatistics)
{
#ifdef _MSC_VER
#pragma warning(disable:26110) // Warning C26110: Caller failing to hold lock 'handle->lock' before calling function 'ReleaseSRWLockExclusive'.
#endif
ReleaseSRWLockExclusive(&handle->lock);
#ifdef _MSC_VER
#pragma warning (default:26110)
#endif
}
else
{
@ -234,7 +240,13 @@ void srw_lock_release_exclusive(SRW_LOCK_HANDLE handle)
(void)QueryPerformanceCounter(&start);
/*Codes_SRS_SRW_LOCK_02_010: [ srw_lock_release_exclusive shall call ReleaseSRWLockExclusive. ]*/
#ifdef _MSC_VER
#pragma warning(disable:26110) // Warning C26110: Caller failing to hold lock 'handle->lock' before calling function 'ReleaseSRWLockExclusive'.
#endif
ReleaseSRWLockExclusive(&handle->lock);
#ifdef _MSC_VER
#pragma warning (default:26110)
#endif
(void)QueryPerformanceCounter(&stop); /*measure release time*/
(void)InterlockedAdd64(&handle->totalCounts_ReleaseSRWLockExclusive, (stop.QuadPart - start.QuadPart));

Просмотреть файл

@ -835,8 +835,18 @@ static void on_underlying_io_bytes_received(void* context, const unsigned char*
}
break;
}
FreeContextBuffer(output_buffers[0].pvBuffer);
FreeContextBuffer(output_buffers[1].pvBuffer);
if (output_buffers[0].pvBuffer != NULL)
{
FreeContextBuffer(output_buffers[0].pvBuffer);
output_buffers[0].pvBuffer = NULL;
}
if (output_buffers[1].pvBuffer != NULL)
{
FreeContextBuffer(output_buffers[1].pvBuffer);
output_buffers[1].pvBuffer = NULL;
}
}
else if (tls_io_instance->tlsio_state == TLSIO_STATE_OPEN)
{
@ -977,8 +987,18 @@ static void on_underlying_io_bytes_received(void* context, const unsigned char*
}
}
}
FreeContextBuffer(output_buffers[0].pvBuffer);
FreeContextBuffer(output_buffers[1].pvBuffer);
if (output_buffers[0].pvBuffer != NULL)
{
FreeContextBuffer(output_buffers[0].pvBuffer);
output_buffers[0].pvBuffer = NULL;
}
if (output_buffers[1].pvBuffer != NULL)
{
FreeContextBuffer(output_buffers[1].pvBuffer);
output_buffers[1].pvBuffer = NULL;
}
break;
}

Просмотреть файл

@ -332,6 +332,7 @@ extern const IO_INTERFACE_DESCRIPTION* http_proxy_io_get_interface_description(v
except not limited to 76 char/line>
user-pass = userid ":" password
userid = *<TEXT excluding ":">
[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Not a password")]
password = *TEXT
**SRS_HTTP_PROXY_IO_01_093: [** Userids might be case sensitive. **]**

Просмотреть файл

@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#ifndef SAFE_MATH_H
#define SAFE_MATH_H
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)((size_t)~(size_t)0))
#endif
#define safe_add_size_t(a, b) ((((size_t)(a)) < ((size_t)(SIZE_MAX - ((size_t)(b))))) ? ((size_t)(a) + (size_t)(b)) : SIZE_MAX)
#define safe_subtract_size_t(a, b) (((a) >= (b)) ? ((size_t)(a) - (size_t)(b)) : SIZE_MAX)
#define safe_multiply_size_t(a, b) (((a) == 0 || (b) == 0) ? 0 : (((SIZE_MAX / (size_t)(a)) >= (size_t)(b)) ? (size_t)(a) * (size_t)(b) : SIZE_MAX))
#endif // SAFE_MATH_H

Просмотреть файл

@ -8,6 +8,7 @@
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/gballoc.h"
#include "azure_c_shared_utility/strings.h"
#include "azure_c_shared_utility/safe_math.h"
#include "azure_c_shared_utility/azure_base32.h"
@ -71,9 +72,14 @@ static char* base32_encode_impl(const unsigned char* source, size_t src_size)
char* result;
// Allocate target buffer
size_t output_len = base32_encoding_length(src_size);
size_t output_len = safe_add_size_t(base32_encoding_length(src_size), 1);
if (output_len == SIZE_MAX)
{
result = NULL;
LogError("invalid src_size");
}
/* Codes_SRS_BASE32_07_009: [ base32_encode_impl shall allocate the buffer to the size of the encoding value. ] */
if ((result = (char*)malloc(output_len + 1)) == NULL)
else if ((result = (char*)malloc(output_len)) == NULL)
{
LogError("Failure allocating output buffer");
}
@ -91,11 +97,11 @@ static char* base32_encode_impl(const unsigned char* source, size_t src_size)
unsigned char pos7 = 0;
unsigned char pos8 = 0;
memset(result, 0, output_len + 1);
memset(result, 0, output_len);
// Go through the source buffer sectioning off blocks of 5
/* Codes_SRS_BASE32_07_010: [ base32_encode_impl shall look through source and separate each block into 5 bit chunks ] */
while (src_size >= 1 && result != NULL)
while (src_size >= 1)
{
pos1 = pos2 = pos3 = pos4 = pos5 = pos6 = pos7 = pos8 = 0;
block_len = src_size > TARGET_BLOCK_SIZE ? TARGET_BLOCK_SIZE : src_size;
@ -141,6 +147,14 @@ static char* base32_encode_impl(const unsigned char* source, size_t src_size)
break;
}
if ((result_len + 8) > output_len)
{
LogError("result buffer is too small");
free(result);
result = NULL;
break;
}
/* Codes_SRS_BASE32_07_011: [ base32_encode_impl shall then map the 5 bit chunks into one of the BASE32 values (a-z,2,3,4,5,6,7) values. ] */
result[result_len++] = BASE32_VALUES[pos1];
result[result_len++] = BASE32_VALUES[pos2];

Просмотреть файл

@ -236,13 +236,19 @@ static STRING_HANDLE Base64_Encode_Internal(const unsigned char* source, size_t
{
STRING_HANDLE result;
size_t neededSize = 0;
bool isBufferOverflow = false;
char* encoded;
size_t currentPosition = 0;
neededSize += (size == 0) ? (0) : ((((size - 1) / 3) + 1) * 4);
neededSize += 1; /*+1 because \0 at the end of the string*/
if (neededSize == 0)
{
result = NULL;
LogError("Azure_Base64_Encode:: Invalid size parameter.");
}
/*Codes_SRS_BASE64_06_006: [If when allocating memory to produce the encoding a failure occurs then Azure_Base64_Encode shall return NULL.]*/
encoded = (char*)malloc(neededSize);
if (encoded == NULL)
else if ((encoded = (char*)malloc(neededSize)) == NULL)
{
result = NULL;
LogError("Azure_Base64_Encode:: Allocation failed.");
@ -270,43 +276,87 @@ static STRING_HANDLE Base64_Encode_Internal(const unsigned char* source, size_t
source[currentPosition + 2] & 0x3F
);
currentPosition += 3;
if ((destinationPosition + 4) > neededSize)
{
LogError("Azure_Base64_Encode:: Invalid buffer size.");
isBufferOverflow = true;
break;
}
encoded[destinationPosition++] = c1;
encoded[destinationPosition++] = c2;
encoded[destinationPosition++] = c3;
encoded[destinationPosition++] = c4;
}
if (size - currentPosition == 2)
if (!isBufferOverflow && size - currentPosition == 2)
{
char c1 = base64char(source[currentPosition] >> 2);
char c2 = base64char(
((source[currentPosition] & 0x03) << 4) |
if ((destinationPosition + 4) > neededSize)
{
LogError("Azure_Base64_Encode:: Invalid buffer size.");
isBufferOverflow = true;
}
else
{
char c1 = base64char(source[currentPosition] >> 2);
char c2 = base64char(
((source[currentPosition] & 0x03) << 4) |
(source[currentPosition + 1] >> 4)
);
char c3 = base64b16(source[currentPosition + 1] & 0x0F);
encoded[destinationPosition++] = c1;
encoded[destinationPosition++] = c2;
encoded[destinationPosition++] = c3;
encoded[destinationPosition++] = '=';
);
char c3 = base64b16(source[currentPosition + 1] & 0x0F);
encoded[destinationPosition++] = c1;
encoded[destinationPosition++] = c2;
encoded[destinationPosition++] = c3;
encoded[destinationPosition++] = '=';
}
}
else if (size - currentPosition == 1)
else if (!isBufferOverflow && size - currentPosition == 1)
{
char c1 = base64char(source[currentPosition] >> 2);
char c2 = base64b8(source[currentPosition] & 0x03);
encoded[destinationPosition++] = c1;
encoded[destinationPosition++] = c2;
encoded[destinationPosition++] = '=';
encoded[destinationPosition++] = '=';
if ((destinationPosition + 4) > neededSize)
{
LogError("Azure_Base64_Encode:: Invalid buffer size.");
isBufferOverflow = true;
}
else
{
char c1 = base64char(source[currentPosition] >> 2);
char c2 = base64b8(source[currentPosition] & 0x03);
encoded[destinationPosition++] = c1;
encoded[destinationPosition++] = c2;
encoded[destinationPosition++] = '=';
encoded[destinationPosition++] = '=';
}
}
/*null terminating the string*/
encoded[destinationPosition] = '\0';
/*Codes_SRS_BASE64_06_007: [Otherwise Azure_Base64_Encode shall return a pointer to STRING, that string contains the base 64 encoding of input.]*/
result = STRING_new_with_memory(encoded);
if (result == NULL)
if (!isBufferOverflow)
{
if ((destinationPosition + 1) > neededSize)
{
LogError("Azure_Base64_Encode:: Invalid buffer size.");
isBufferOverflow = true;
}
else
{
encoded[destinationPosition] = '\0';
}
}
if (isBufferOverflow)
{
free(encoded);
LogError("Azure_Base64_Encode:: Allocation failed for return value.");
result = NULL;
}
else
{
/*Codes_SRS_BASE64_06_007: [Otherwise Azure_Base64_Encode shall return a pointer to STRING, that string contains the base 64 encoding of input.]*/
result = STRING_new_with_memory(encoded);
if (result == NULL)
{
free(encoded);
LogError("Azure_Base64_Encode:: Allocation failed for return value.");
}
}
}
return result;

Просмотреть файл

@ -8,6 +8,7 @@
#include "azure_c_shared_utility/buffer_.h"
#include "azure_c_shared_utility/optimize_size.h"
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/safe_math.h"
typedef struct BUFFER_TAG
{
@ -210,7 +211,7 @@ int BUFFER_append_build(BUFFER_HANDLE handle, const unsigned char* source, size_
if (handle->buffer == NULL)
{
/* Codes_SRS_BUFFER_07_030: [ if handle->buffer is NULL BUFFER_append_build shall allocate the a buffer of size bytes... ] */
if (BUFFER_safemalloc(handle, size) != 0)
if (BUFFER_safemalloc(handle, size) != 0 || handle->buffer == NULL)
{
/* Codes_SRS_BUFFER_07_035: [ If any error is encountered BUFFER_append_build shall return a non-null value. ] */
LogError("Failure with BUFFER_safemalloc");
@ -540,12 +541,13 @@ int BUFFER_prepend(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2)
else
{
//put b2 ahead of b1: [b2][b1], return b1
size_t malloc_size = safe_add_size_t(b1->size, b2->size);
if (b2->size == 0)
{
// do nothing
result = 0;
}
else if (b1->size + b2->size < b2->size)
else if (malloc_size == SIZE_MAX)
{
LogError("Failure: size_t overflow.");
result = MU_FAILURE;
@ -553,7 +555,7 @@ int BUFFER_prepend(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2)
else
{
// b2->size != 0
unsigned char* temp = (unsigned char*)malloc(b1->size + b2->size);
unsigned char* temp = (unsigned char*)malloc(malloc_size);
if (temp == NULL)
{
/* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/
@ -564,9 +566,15 @@ int BUFFER_prepend(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2)
{
/* Codes_SRS_BUFFER_01_004: [ BUFFER_prepend concatenates handle1 onto handle2 without modifying handle1 and shall return zero on success. ]*/
// Append the BUFFER
#ifdef _MSC_VER
#pragma warning(disable:6386) // Buffer overrun while writing to 'temp'
#endif
(void)memcpy(temp, b2->buffer, b2->size);
// start from b1->size to append b1
(void)memcpy(&temp[b2->size], b1->buffer, b1->size);
#ifdef _MSC_VER
#pragma warning (default:6386)
#endif
free(b1->buffer);
b1->buffer = temp;
b1->size += b2->size;

Просмотреть файл

@ -6,6 +6,7 @@
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/constbuffer_array_batcher.h"
#include "azure_c_shared_utility/memory_data.h"
#include "azure_c_shared_utility/safe_math.h"
CONSTBUFFER_ARRAY_HANDLE constbuffer_array_batcher_batch(CONSTBUFFER_ARRAY_HANDLE* payloads, uint32_t count)
{
@ -45,7 +46,7 @@ CONSTBUFFER_ARRAY_HANDLE constbuffer_array_batcher_batch(CONSTBUFFER_ARRAY_HANDL
/* Codes_SRS_CONSTBUFFER_ARRAY_BATCHER_01_003: [ Otherwise constbuffer_array_batcher_batch shall obtain the number of buffers used by each CONSTBUFFER_ARRAY. ]*/
/* Codes_SRS_CONSTBUFFER_ARRAY_BATCHER_01_004: [ constbuffer_array_batcher_batch shall allocate memory for the header buffer (enough to hold the entire batch header namingly (count + 1) uint32_t values). ]*/
header_memory = malloc(sizeof(uint32_t) * (count + 1));
header_memory = malloc(sizeof(uint32_t) * ((size_t)count + 1));
if (header_memory == NULL)
{
/* Codes_SRS_CONSTBUFFER_ARRAY_BATCHER_01_010: [ If any error occurrs, constbuffer_array_batcher_batch shall fail and return NULL. ]*/
@ -72,9 +73,14 @@ CONSTBUFFER_ARRAY_HANDLE constbuffer_array_batcher_batch(CONSTBUFFER_ARRAY_HANDL
}
/* Codes_SRS_CONSTBUFFER_ARRAY_BATCHER_01_007: [ constbuffer_array_batcher_batch shall allocate enough memory for all the buffer handles in all the arrays + one extra header buffer handle. ]*/
uint32_t all_buffers_array_size = total_buffer_count + 1;
all_buffers = malloc(sizeof(CONSTBUFFER_HANDLE) * ((size_t)all_buffers_array_size));
if (all_buffers == NULL)
size_t all_buffers_array_size = (size_t)total_buffer_count + 1;
size_t malloc_size = safe_multiply_size_t(sizeof(CONSTBUFFER_HANDLE), (all_buffers_array_size));
if (malloc_size == SIZE_MAX)
{
LogError("malloc size is invalid");
}
else if ((all_buffers = malloc(malloc_size)) == NULL)
{
/* Codes_SRS_CONSTBUFFER_ARRAY_BATCHER_01_010: [ If any error occurrs, constbuffer_array_batcher_batch shall fail and return NULL. ]*/
LogError("malloc failed");
@ -83,9 +89,12 @@ CONSTBUFFER_ARRAY_HANDLE constbuffer_array_batcher_batch(CONSTBUFFER_ARRAY_HANDL
{
uint32_t current_index = 0;
/* Codes_SRS_CONSTBUFFER_ARRAY_BATCHER_01_008: [ constbuffer_array_batcher_batch shall populate the first handle in the newly allocated handles array with the header buffer handle. ]*/
all_buffers[current_index] = CONSTBUFFER_CreateWithMoveMemory((void*)header_memory, sizeof(uint32_t) * (count + 1));
if (all_buffers[current_index] == NULL)
size_t move_memory_size = safe_multiply_size_t(sizeof(uint32_t), ((size_t)count + 1));
if (move_memory_size == SIZE_MAX)
{
LogError("invalid malloc size in CONSTBUFFER_CreateWithMoveMemory");
}
else if ((all_buffers[current_index] = CONSTBUFFER_CreateWithMoveMemory((void*)header_memory, move_memory_size)) == NULL)
{
/* Codes_SRS_CONSTBUFFER_ARRAY_BATCHER_01_010: [ If any error occurrs, constbuffer_array_batcher_batch shall fail and return NULL. ]*/
LogError("CONSTBUFFER_CreateWithMoveMemory failed");
@ -106,14 +115,26 @@ CONSTBUFFER_ARRAY_HANDLE constbuffer_array_batcher_batch(CONSTBUFFER_ARRAY_HANDL
for (j = 0; j < buffer_count; j++)
{
#ifdef _MSC_VER
#pragma warning(disable:6386) // warning C6386: Buffer overrun while writing to 'all_buffers'
#endif
all_buffers[current_index++] = constbuffer_array_get_buffer(payloads[i], j);
#ifdef _MSC_VER
#pragma warning (default:6386)
#endif
}
}
result = constbuffer_array_create(all_buffers, all_buffers_array_size);
result = constbuffer_array_create(all_buffers, (uint32_t)all_buffers_array_size);
for (i = 0; i < all_buffers_array_size; i++)
{
#ifdef _MSC_VER
#pragma warning(disable:6385) // warning C6385: Reading invalid data from 'all_buffers'
#endif
CONSTBUFFER_DecRef(all_buffers[i]);
#ifdef _MSC_VER
#pragma warning (default:6385)
#endif
}
if (result == NULL)

Просмотреть файл

@ -655,7 +655,13 @@ long double strtold_s(const char* nptr, char** endptr)
break;
case FST_NAN:
/*Codes_SRS_CRT_ABSTRACTIONS_21_034: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtold_s must return 0.0 and points endptr to the first character after the 'NAN' sequence.]*/
result = (long double)((float)NAN);
#ifdef _MSC_VER
#pragma warning(disable:26451) // warning C26451: overflow in constant arithmetic
#endif
result = (long double)NAN;
#ifdef _MSC_VER
#pragma warning (default:26451)
#endif
break;
case FST_NUMBER:
if ((exponential != DBL_MAX_10_EXP || (fraction <= 1.7976931348623158)) &&

Просмотреть файл

@ -326,7 +326,13 @@ void gballoc_free(void* ptr)
curr = (ALLOCATION*)curr->next;
}
#ifdef _MSC_VER
#pragma warning(disable:6001) // Using uninitialized memory 'curr'
#endif
if ((curr == NULL) && (ptr != NULL))
#ifdef _MSC_VER
#pragma warning (default:6001)
#endif
{
/* Codes_SRS_GBALLOC_01_019: [When the ptr pointer cannot be found in the pointers tracked by gballoc, gballoc_free shall not free any memory.] */

Просмотреть файл

@ -12,6 +12,7 @@
#include "azure_c_shared_utility/crt_abstractions.h"
#include "azure_c_shared_utility/http_proxy_io.h"
#include "azure_c_shared_utility/azure_base64.h"
#include "azure_c_shared_utility/safe_math.h"
static const char* const OPTION_UNDERLYING_IO_OPTIONS = "underlying_io_options";
@ -631,8 +632,16 @@ static void on_underlying_io_bytes_received(void* context, const unsigned char*
case HTTP_PROXY_IO_STATE_WAITING_FOR_CONNECT_RESPONSE:
{
/* Codes_SRS_HTTP_PROXY_IO_01_065: [ When bytes are received and the response to the CONNECT request was not yet received, the bytes shall be accumulated until a double new-line is detected. ]*/
unsigned char* new_receive_buffer = (unsigned char*)realloc(http_proxy_io_instance->receive_buffer, http_proxy_io_instance->receive_buffer_size + size + 1);
if (new_receive_buffer == NULL)
// size_t malloc_size = http_proxy_io_instance->receive_buffer_size + size + 1;
size_t realloc_size = safe_add_size_t(safe_add_size_t(http_proxy_io_instance->receive_buffer_size, size), 1);
unsigned char* new_receive_buffer = NULL;
if (realloc_size == SIZE_MAX)
{
LogError("Invalid memory size for received data");
indicate_open_complete_error_and_close(http_proxy_io_instance);
}
else if ((new_receive_buffer = (unsigned char*)realloc(http_proxy_io_instance->receive_buffer, realloc_size)) == NULL)
{
/* Codes_SRS_HTTP_PROXY_IO_01_067: [ If allocating memory for the buffered bytes fails, the on_open_complete callback shall be triggered with IO_OPEN_ERROR, passing also the on_open_complete_context argument as context. ]*/
LogError("Cannot allocate memory for received data");
@ -649,8 +658,13 @@ static void on_underlying_io_bytes_received(void* context, const unsigned char*
{
const char* request_end_ptr;
#ifdef _MSC_VER
#pragma warning(disable:6386) // Warning C6386: Buffer overrun while writing to 'http_proxy_io_instance->receive_buffer'
#endif
http_proxy_io_instance->receive_buffer[http_proxy_io_instance->receive_buffer_size] = 0;
#ifdef _MSC_VER
#pragma warning (default:6386)
#endif
/* Codes_SRS_HTTP_PROXY_IO_01_066: [ When a double new-line is detected the response shall be parsed in order to extract the status code. ]*/
if ((http_proxy_io_instance->receive_buffer_size >= 4) &&
((request_end_ptr = strstr((const char*)http_proxy_io_instance->receive_buffer, "\r\n\r\n")) != NULL))

Просмотреть файл

@ -105,9 +105,12 @@ HTTPAPIEX_SAS_HANDLE HTTPAPIEX_SAS_Create(STRING_HANDLE key, STRING_HANDLE uriRe
void HTTPAPIEX_SAS_Destroy(HTTPAPIEX_SAS_HANDLE handle)
{
#ifdef _MSC_VER
#pragma warning(disable:6001) // Using uninitialized memory '*state'
#endif
/*Codes_SRS_HTTPAPIEXSAS_06_005: [If the parameter handle is NULL then HTTAPIEX_SAS_Destroy shall do nothing and return.]*/
HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)handle;
if (state)
if (state != NULL)
{
/*Codes_SRS_HTTPAPIEXSAS_06_006: [HTTAPIEX_SAS_Destroy shall deallocate any structures denoted by the parameter handle.]*/
if (state->key)
@ -124,6 +127,9 @@ void HTTPAPIEX_SAS_Destroy(HTTPAPIEX_SAS_HANDLE handle)
}
free(state);
}
#ifdef _MSC_VER
#pragma warning (default:6001)
#endif
}
HTTPAPIEX_RESULT HTTPAPIEX_SAS_ExecuteRequest(HTTPAPIEX_SAS_HANDLE sasHandle, HTTPAPIEX_HANDLE handle, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath, HTTP_HEADERS_HANDLE requestHttpHeadersHandle, BUFFER_HANDLE requestContent, unsigned int* statusCode, HTTP_HEADERS_HANDLE responseHeadersHandle, BUFFER_HANDLE responseContent)

Просмотреть файл

@ -144,17 +144,24 @@ int SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned int l
}
else
{
while (length-- && !context->Corrupted)
while (length-- && !context->Corrupted) //shaInputTooLong
{
context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
if (!SHA1AddLength(context, 8) && (context->Message_Block_Index == SHA1_Message_Block_Size))
if (context->Message_Block_Index < SHA1_Message_Block_Size)
{
SHA1ProcessMessageBlock(context);
context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
if (!SHA1AddLength(context, 8) && (context->Message_Block_Index == SHA1_Message_Block_Size))
{
SHA1ProcessMessageBlock(context);
}
message_array++;
}
else
{
result = context->Corrupted = shaBadParam;
}
message_array++;
}
result = shaSuccess;
result = context->Corrupted;
}
return result;
}

Просмотреть файл

@ -241,15 +241,22 @@ int SHA256Input(SHA256Context *context, const uint8_t *message_array, unsigned i
{
while (length-- && !context->Corrupted)
{
context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
if (!SHA224_256AddLength(context, 8) && (context->Message_Block_Index == SHA256_Message_Block_Size))
if (context->Message_Block_Index < SHA256_Message_Block_Size)
{
SHA224_256ProcessMessageBlock(context);
context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
if (!SHA224_256AddLength(context, 8) && (context->Message_Block_Index == SHA256_Message_Block_Size))
{
SHA224_256ProcessMessageBlock(context);
}
message_array++;
}
else
{
result = context->Corrupted = shaBadParam;
}
message_array++;
}
result = shaSuccess;
result = context->Corrupted;
}
return result;
}

Просмотреть файл

@ -484,17 +484,24 @@ int SHA512Input(SHA512Context *context,
return context->Corrupted;
while (length-- && !context->Corrupted) {
context->Message_Block[context->Message_Block_Index++] =
(*message_array & 0xFF);
if (context->Message_Block_Index < SHA512_Message_Block_Size)
{
context->Message_Block[context->Message_Block_Index++] =
(*message_array & 0xFF);
if (!SHA384_512AddLength(context, 8) &&
(context->Message_Block_Index == SHA512_Message_Block_Size))
SHA384_512ProcessMessageBlock(context);
if (!SHA384_512AddLength(context, 8) &&
(context->Message_Block_Index == SHA512_Message_Block_Size))
SHA384_512ProcessMessageBlock(context);
message_array++;
message_array++;
}
else
{
context->Corrupted = shaBadParam;
}
}
return shaSuccess;
return context->Corrupted;
}
/*

Просмотреть файл

@ -141,7 +141,13 @@ int singlylinkedlist_remove(SINGLYLINKEDLIST_HANDLE list, LIST_ITEM_HANDLE item)
current_item = (LIST_ITEM_INSTANCE*)current_item->next;
}
#ifdef _MSC_VER
#pragma warning(disable:6001) // Using uninitialized memory 'current_item'
#endif
if (current_item == NULL)
#ifdef _MSC_VER
#pragma warning (default:6001)
#endif
{
/* Codes_SRS_LIST_01_025: [If the item item_handle is not found in the list, then singlylinkedlist_remove shall fail and return a non-zero value.] */
result = MU_FAILURE;

Просмотреть файл

@ -9,6 +9,7 @@
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/crt_abstractions.h"
#include "azure_c_shared_utility/string_token.h"
#include "azure_c_shared_utility/safe_math.h"
typedef struct STRING_TOKEN_TAG
{
@ -22,9 +23,14 @@ typedef struct STRING_TOKEN_TAG
static size_t* get_delimiters_lengths(const char** delimiters, size_t n_delims)
{
size_t malloc_size = safe_multiply_size_t(sizeof(size_t), n_delims);
size_t* result;
if ((result = malloc(sizeof(size_t) * n_delims)) == NULL)
if (malloc_size == SIZE_MAX)
{
LogError("malloc size overflow");
result = NULL;
}
else if ((result = malloc(malloc_size)) == NULL)
{
LogError("Failed to allocate array for delimiters lengths");
}
@ -41,7 +47,14 @@ static size_t* get_delimiters_lengths(const char** delimiters, size_t n_delims)
result = NULL;
break;
}
else
else if (((i+1) * sizeof(size_t)) > malloc_size)
{
LogError("buffer overflow");
free(result);
result = NULL;
break;
}
else
{
result[i] = strlen(delimiters[i]);
}

Просмотреть файл

@ -10,6 +10,7 @@
#include "azure_c_shared_utility/strings.h"
#include "azure_c_shared_utility/optimize_size.h"
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/safe_math.h"
static const char hexToASCII[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
@ -297,12 +298,23 @@ STRING_HANDLE STRING_new_JSON(const char* source)
}
else
{
if ((result = (STRING*)malloc(sizeof(STRING))) == NULL)
//size_t malloc_len = vlen + 5 * nControlCharacters + nEscapeCharacters + 3;
size_t malloc_len = safe_multiply_size_t(5, nControlCharacters);
malloc_len = safe_add_size_t(malloc_len, vlen);
malloc_len = safe_add_size_t(malloc_len, nEscapeCharacters);
malloc_len = safe_add_size_t(malloc_len, 3);
if (malloc_len == SIZE_MAX)
{
result = NULL;
LogError("malloc len overflow");
}
else if ((result = (STRING*)malloc(sizeof(STRING))) == NULL)
{
/*Codes_SRS_STRING_02_021: [If the complete JSON representation cannot be produced, then STRING_new_JSON shall fail and return NULL.] */
LogError("malloc json failure");
}
else if ((result->s = (char*)malloc(vlen + 5 * nControlCharacters + nEscapeCharacters + 3)) == NULL)
else if ((result->s = (char*)malloc(malloc_len)) == NULL)
{
/*Codes_SRS_STRING_02_021: [If the complete JSON representation cannot be produced, then STRING_new_JSON shall fail and return NULL.] */
free(result);
@ -316,7 +328,7 @@ STRING_HANDLE STRING_new_JSON(const char* source)
result->s[pos++] = '"';
for (i = 0; i < vlen; i++)
{
if (source[i] <= 0x1F)
if ((source[i] <= 0x1F) && ((pos + 6) <= malloc_len))
{
/*Codes_SRS_STRING_02_019: [If the character code is less than 0x20 then it shall be represented as \u00xx, where xx is the hex representation of the character code.]*/
result->s[pos++] = '\\';
@ -326,34 +338,51 @@ STRING_HANDLE STRING_new_JSON(const char* source)
result->s[pos++] = hexToASCII[(source[i] & 0xF0) >> 4]; /*high nibble*/
result->s[pos++] = hexToASCII[source[i] & 0x0F]; /*low nibble*/
}
else if (source[i] == '"')
else if ((source[i] == '"') && ((pos + 2) <= malloc_len))
{
/*Codes_SRS_STRING_02_016: [If the character is " (quote) then it shall be repsented as \".] */
result->s[pos++] = '\\';
result->s[pos++] = '"';
}
else if (source[i] == '\\')
else if ((source[i] == '\\') && ((pos + 2) <= malloc_len))
{
/*Codes_SRS_STRING_02_017: [If the character is \ (backslash) then it shall represented as \\.] */
result->s[pos++] = '\\';
result->s[pos++] = '\\';
}
else if (source[i] == '/')
else if ((source[i] == '/') && ((pos + 2) <= malloc_len))
{
/*Codes_SRS_STRING_02_018: [If the character is / (slash) then it shall be represented as \/.] */
result->s[pos++] = '\\';
result->s[pos++] = '/';
}
else
else if (pos < malloc_len)
{
/*Codes_SRS_STRING_02_013: [The string shall copy the characters of source "as they are" (until the '\0' character) with the following exceptions:] */
result->s[pos++] = source[i];
}
else
{
free(result->s);
free(result);
result = NULL;
break;
}
}
if ((pos + 1) < malloc_len)
{
/*Codes_SRS_STRING_02_020: [The string shall end with " (quote).] */
result->s[pos++] = '"';
/*zero terminating it*/
result->s[pos] = '\0';
}
else
{
free(result->s);
free(result);
result = NULL;
}
/*Codes_SRS_STRING_02_020: [The string shall end with " (quote).] */
result->s[pos++] = '"';
/*zero terminating it*/
result->s[pos] = '\0';
}
}

Просмотреть файл

@ -55,7 +55,13 @@ int UUID_from_string(const char* uuid_string, UUID_T* uuid)
(void)memcpy(double_hex_digit, uuid_string + i, 2);
#ifdef _MSC_VER
#pragma warning(disable:6328) // warning C6328: Size mismatch
#endif
if (sscanf(double_hex_digit, "%02hhx", uuid_bytes + j) != 1)
#ifdef _MSC_VER
#pragma warning (default:6328)
#endif
{
// Codes_SRS_UUID_09_009: [ If uuid fails to be generated, UUID_from_string shall return a non-zero value ]
LogError("Failed decoding UUID string (%lu)", (unsigned long)i);

Просмотреть файл

@ -25,6 +25,7 @@
#include "azure_c_shared_utility/optionhandler.h"
#include "azure_c_shared_utility/map.h"
#include "azure_c_shared_utility/shared_util_options.h"
#include "azure_c_shared_utility/safe_math.h"
static const char* UWS_CLIENT_OPTIONS = "uWSClientOptions";
@ -96,6 +97,7 @@ typedef struct UWS_CLIENT_INSTANCE_TAG
ON_WS_CLOSE_COMPLETE on_ws_close_complete;
void* on_ws_close_complete_context;
unsigned char* stream_buffer;
size_t stream_buffer_size;
size_t stream_buffer_count;
unsigned char* fragment_buffer;
size_t fragment_buffer_count;
@ -1093,8 +1095,12 @@ static void on_underlying_io_bytes_received(void* context, const unsigned char*
case UWS_STATE_WAITING_FOR_UPGRADE_RESPONSE:
{
/* Codes_SRS_UWS_CLIENT_01_378: [ When on_underlying_io_bytes_received is called while the uws is OPENING, the received bytes shall be accumulated in order to attempt parsing the WebSocket Upgrade response. ]*/
unsigned char* new_received_bytes = (unsigned char*)realloc(uws_client->stream_buffer, uws_client->stream_buffer_count + size + 1);
if (new_received_bytes == NULL)
unsigned char* new_received_bytes;
//size_t realloc_size = uws_client->stream_buffer_count + size + 1; **using safe int**
uws_client->stream_buffer_size = safe_add_size_t(safe_add_size_t(uws_client->stream_buffer_count, size), 1);
if (uws_client->stream_buffer_size == SIZE_MAX ||
(new_received_bytes = (unsigned char*)realloc(uws_client->stream_buffer, uws_client->stream_buffer_size)) == NULL)
{
/* Codes_SRS_UWS_CLIENT_01_379: [ If allocating memory for accumulating the bytes fails, uws shall report that the open failed by calling the on_ws_open_complete callback passed to uws_client_open_async with WS_OPEN_ERROR_NOT_ENOUGH_MEMORY. ]*/
indicate_ws_open_complete_error_and_close(uws_client, WS_OPEN_ERROR_NOT_ENOUGH_MEMORY);
@ -1116,8 +1122,12 @@ static void on_underlying_io_bytes_received(void* context, const unsigned char*
case UWS_STATE_CLOSING_WAITING_FOR_CLOSE:
{
/* Codes_SRS_UWS_CLIENT_01_385: [ If the state of the uws instance is OPEN, the received bytes shall be used for decoding WebSocket frames. ]*/
unsigned char* new_received_bytes = (unsigned char*)realloc(uws_client->stream_buffer, uws_client->stream_buffer_count + size + 1);
if (new_received_bytes == NULL)
unsigned char* new_received_bytes;
//size_t realloc_size = uws_client->stream_buffer_count + size + 1; **using safe int**
uws_client->stream_buffer_size = safe_add_size_t(safe_add_size_t(uws_client->stream_buffer_count, size), 1);
if (uws_client->stream_buffer_size == SIZE_MAX ||
(new_received_bytes = (unsigned char*)realloc(uws_client->stream_buffer, uws_client->stream_buffer_size)) == NULL)
{
/* Codes_SRS_UWS_CLIENT_01_418: [ If allocating memory for the bytes accumulated for decoding WebSocket frames fails, an error shall be indicated by calling the on_ws_error callback with WS_ERROR_NOT_ENOUGH_MEMORY. ]*/
LogError("Cannot allocate memory for received data");
@ -1211,7 +1221,9 @@ static void on_underlying_io_bytes_received(void* context, const unsigned char*
/* Codes_SRS_UWS_CLIENT_01_277: [ To receive WebSocket data, an endpoint listens on the underlying network connection. ]*/
/* Codes_SRS_UWS_CLIENT_01_278: [ Incoming data MUST be parsed as WebSocket frames as defined in Section 5.2. ]*/
if (uws_client->stream_buffer_count >= needed_bytes)
if (uws_client->stream_buffer_count >= needed_bytes &&
uws_client->stream_buffer_size > 1 // validate uws_client->stream_buffer[1] access
)
{
unsigned char has_error = 0;
@ -1232,7 +1244,9 @@ static void on_underlying_io_bytes_received(void* context, const unsigned char*
{
/* Codes_SRS_UWS_CLIENT_01_165: [ If 126, the following 2 bytes interpreted as a 16-bit unsigned integer are the payload length. ]*/
needed_bytes += 2;
if (uws_client->stream_buffer_count >= needed_bytes)
if (uws_client->stream_buffer_count >= needed_bytes &&
uws_client->stream_buffer_size > 3 // validate access upto stream_buffer[3]
)
{
/* Codes_SRS_UWS_CLIENT_01_167: [ Multibyte length quantities are expressed in network byte order. ]*/
length = ((size_t)(uws_client->stream_buffer[2]) << 8) + (size_t)uws_client->stream_buffer[3];
@ -1258,7 +1272,7 @@ static void on_underlying_io_bytes_received(void* context, const unsigned char*
needed_bytes += 8;
if (uws_client->stream_buffer_count >= needed_bytes)
{
if ((uws_client->stream_buffer[2] & 0x80) != 0)
if (uws_client->stream_buffer_size <= 2 || (uws_client->stream_buffer[2] & 0x80) != 0)
{
LogError("Bad frame: received a 64 bit length frame with the highest bit set");
@ -1266,6 +1280,11 @@ static void on_underlying_io_bytes_received(void* context, const unsigned char*
indicate_ws_error(uws_client, WS_ERROR_BAD_FRAME_RECEIVED);
has_error = 1;
}
else if (uws_client->stream_buffer_size <= 9) // validate access upto stream_buffer[9] below
{
indicate_ws_error(uws_client, WS_ERROR_BAD_FRAME_RECEIVED);
has_error = 1;
}
else
{
/* Codes_SRS_UWS_CLIENT_01_167: [ Multibyte length quantities are expressed in network byte order. ]*/