Debian Codegraph @

tests/test_decode_bytes.c

raw symbols SCIP
1 // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
2 // SPDX-License-Identifier: mit
3
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "amqp_private.h"
9
10 /* Regression test for GHSA-jgjf-7fwf-f3c7: a size_t integer overflow in
11 * amqp_decode_bytes bypassed the bounds check on 32-bit systems, producing an
12 * out-of-bounds amqp_bytes_t (information disclosure / crash). The check must
13 * reject any (offset, len) pair that would read past the end of the buffer,
14 * including ones where offset + len wraps around SIZE_MAX. */
15
16 static int failures = 0;
17
18 static void expect_reject(const char *name, amqp_bytes_t encoded, size_t offset,
19 size_t len) {
20 amqp_bytes_t output;
21 size_t off = offset;
22 output.bytes = NULL;
23 output.len = 0;
24 if (amqp_decode_bytes(encoded, &off, &output, len)) {
25 fprintf(stderr,
26 "FAIL %s: amqp_decode_bytes accepted an out-of-bounds length "
27 "(offset=%zu len=%zu buffer=%zu) -> output.len=%zu\n",
28 name, offset, len, encoded.len, output.len);
29 failures++;
30 }
31 }
32
33 static void expect_accept(const char *name, amqp_bytes_t encoded, size_t offset,
34 size_t len) {
35 amqp_bytes_t output;
36 size_t off = offset;
37 output.bytes = NULL;
38 output.len = 0;
39 if (!amqp_decode_bytes(encoded, &off, &output, len)) {
40 fprintf(stderr,
41 "FAIL %s: amqp_decode_bytes rejected a valid length "
42 "(offset=%zu len=%zu buffer=%zu)\n",
43 name, offset, len, encoded.len);
44 failures++;
45 return;
46 }
47 if (output.len != len || output.bytes != amqp_offset(encoded.bytes, offset)) {
48 fprintf(stderr, "FAIL %s: amqp_decode_bytes produced wrong output\n", name);
49 failures++;
50 }
51 }
52
53 int main(void) {
54 char buffer[16];
55 amqp_bytes_t encoded;
56 encoded.bytes = buffer;
57 encoded.len = sizeof(buffer);
58
59 /* Normal, in-bounds decodes still work. */
60 expect_accept("full buffer", encoded, 0, sizeof(buffer));
61 expect_accept("partial at offset", encoded, 4, 8);
62 expect_accept("zero length", encoded, 8, 0);
63
64 /* Plain out-of-bounds (no overflow) is rejected. */
65 expect_reject("len past end", encoded, 0, sizeof(buffer) + 1);
66 expect_reject("offset past end", encoded, sizeof(buffer) + 1, 0);
67
68 /* The core of the advisory: a wire length large enough that offset + len
69 * wraps around SIZE_MAX. On 32-bit platforms a uint32_t length of
70 * 0xFFFFFFF5 with a small offset wraps to a tiny value; on any platform we
71 * can force the wrap with a len near SIZE_MAX. Both must be rejected rather
72 * than producing a multi-gigabyte amqp_bytes_t into a small buffer. */
73 expect_reject("overflow to zero", encoded, 11, (size_t)0 - 11);
74 expect_reject("overflow wraps small", encoded, 16, (size_t)0 - 8);
75 expect_reject("max len", encoded, 1, (size_t)-1);
76 expect_reject("32-bit style len", encoded, 11, (size_t)0xFFFFFFF5u);
77
78 if (failures) {
79 fprintf(stderr, "%d test(s) failed\n", failures);
80 return 1;
81 }
82 printf("all amqp_decode_bytes bounds tests passed\n");
83 return 0;
84 }