| 1 | // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors. |
| 2 | // SPDX-License-Identifier: mit |
| 3 | |
| 4 | #include "amqp_private.h" |
| 5 | #include <rabbitmq-c/amqp.h> |
| 6 | #include <rabbitmq-c/framing.h> |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | /* Regression test for GHSA-hfjv-vcp3-39wh: passing an oversized |
| 13 | * AMQP_FRAME_BODY to amqp_send_frame() must not overflow the outbound |
| 14 | * buffer. It should be rejected with AMQP_STATUS_BAD_AMQP_DATA. */ |
| 15 | static void test_oversized_body_frame_rejected(void) { |
| 16 | amqp_connection_state_t state = amqp_new_connection(); |
| 17 | amqp_frame_t frame; |
| 18 | size_t body_len; |
| 19 | char *body; |
| 20 | int res; |
| 21 | |
| 22 | if (state == NULL) { |
| 23 | fprintf(stderr, "amqp_new_connection failed\n"); |
| 24 | abort(); |
| 25 | } |
| 26 | |
| 27 | /* The default outbound buffer is AMQP_DEFAULT_FRAME_SIZE bytes; use a |
| 28 | * body fragment that is larger than that buffer can hold. */ |
| 29 | body_len = state->outbound_buffer.len + 1024; |
| 30 | body = malloc(body_len); |
| 31 | if (body == NULL) { |
| 32 | fprintf(stderr, "malloc failed\n"); |
| 33 | abort(); |
| 34 | } |
| 35 | memset(body, 'A', body_len); |
| 36 | |
| 37 | memset(&frame, 0, sizeof(frame)); |
| 38 | frame.frame_type = AMQP_FRAME_BODY; |
| 39 | frame.channel = 1; |
| 40 | frame.payload.body_fragment.bytes = body; |
| 41 | frame.payload.body_fragment.len = body_len; |
| 42 | |
| 43 | res = amqp_send_frame(state, &frame); |
| 44 | if (res != AMQP_STATUS_BAD_AMQP_DATA) { |
| 45 | fprintf(stderr, "expected AMQP_STATUS_BAD_AMQP_DATA (%d), got %d\n", |
| 46 | AMQP_STATUS_BAD_AMQP_DATA, res); |
| 47 | abort(); |
| 48 | } |
| 49 | |
| 50 | free(body); |
| 51 | amqp_destroy_connection(state); |
| 52 | } |
| 53 | |
| 54 | int main(void) { |
| 55 | test_oversized_body_frame_rejected(); |
| 56 | return 0; |
| 57 | } |