CVE-2024-21646: Integer Overflow or Wraparound (CWE-190)
value: Azure uAMQP is a general purpose C library for AMQP 1.0. The UAMQP library is used by several clients to implement AMQP protocol communication.  When clients using this library receive a crafted binary type data, an integer overflow or wraparound or memory safety issue can occur and may cause remote code execution.  This vulnerability has been patched in release 2024-01-01.
CWE description: The product performs a calculation that can
         produce an integer overflow or wraparound when the logic
         assumes that the resulting value will always be larger than
         the original value. This occurs when an integer value is
         incremented to a value that is too large to store in the
         associated representation. When this occurs, the value may
         become a very small or negative number.
CVSS v3 base score: 9.8
Published: 2024-01-09T01:15Z
Repository: https://github.com/Azure/azure-uamqp-c
Language: C
Files changed: src/amqpvalue.c
Commit date: 2023-12-05 16:07:53 -0800
Commit message: int overflow for binary_value size malloc (#444)

* int overflow for binary_value size malloc

* update PR feedback

* pr feedback
Diff: diff --git a/src/amqpvalue.c b/src/amqpvalue.c
index e3fe324..6bee86a 100644
--- a/src/amqpvalue.c
+++ b/src/amqpvalue.c
@@ -5910,11 +5910,21 @@ static int internal_decoder_decode_bytes(INTERNAL_DECODER_DATA* internal_decoder
                                 internal_decoder_data->on_value_decoded(internal_decoder_data->on_value_decoded_context, internal_decoder_data->decode_to_value);
                                 result = 0;
                             }
                             else
                             {
-                                internal_decoder_data->decode_to_value->value.binary_value.bytes = (unsigned char*)malloc((size_t)internal_decoder_data->decode_to_value->value.binary_value.length + 1);
+                                size_t malloc_size = (size_t)internal_decoder_data->decode_to_value->value.binary_value.length + 1;
+                                if (malloc_size == 0)
+                                {
+                                    internal_decoder_data->decode_to_value->value.binary_value.bytes = NULL;
+                                    LogError("Invalid binary_value size exceeded max allocation");
+                                }
+                                else
+                                {
+                                    internal_decoder_data->decode_to_value->value.binary_value.bytes = (unsigned char*)malloc(malloc_size);
+                                }
+
                                 if (internal_decoder_data->decode_to_value->value.binary_value.bytes == NULL)
                                 {
                                     /* Codes_SRS_AMQPVALUE_01_326: [If any allocation failure occurs during decoding, amqpvalue_decode_bytes shall fail and return a non-zero value.] */
                                     internal_decoder_data->decoder_state = DECODER_STATE_ERROR;
                                     LogError("Cannot allocate memory for decoded binary value");

Vulnerable code:                                 internal_decoder_data->decode_to_value->value.binary_value.bytes = (unsigned char*)malloc((size_t)internal_decoder_data->decode_to_value->value.binary_value.length + 1);
Fixed code:                                 size_t malloc_size = (size_t)internal_decoder_data->decode_to_value->value.binary_value.length + 1;
                                if (malloc_size == 0)
                                {
                                    internal_decoder_data->decode_to_value->value.binary_value.bytes = NULL;
                                    LogError("Invalid binary_value size exceeded max allocation");
                                }
                                else
                                {
                                    internal_decoder_data->decode_to_value->value.binary_value.bytes = (unsigned char*)malloc(malloc_size);
                                }