Debian Codegraph @

tests/test_decode_bytes.c

raw symbols SCIP
1 // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.cxx . . $ `<file>/tests/test_decode_bytes.c`/
2 // SPDX-License-Identifier: mit
3
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "amqp_private.h"cxx . . $ `<file>/librabbitmq/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 failurescxx . . $ failures. = 0;
17
18 static void expect_rejectcxx . . $ expect_reject(6efac419a2592ea9).(const char *name, amqp_bytes_tcxx . . $ amqp_bytes_t# encoded, size_t offset,
19 size_t len) {
20 amqp_bytes_tcxx . . $ amqp_bytes_t# output;
21 size_t off = offset;
22 output.bytescxx . . $ amqp_bytes_t_#bytes. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
23 output.lencxx . . $ amqp_bytes_t_#len. = 0;
24 if (amqp_decode_bytescxx . . $ amqp_decode_bytes(8ef006f6ddb737a0).(encoded, &off, &output, len)) {
25 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,
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.lencxx . . $ amqp_bytes_t_#len., output.lencxx . . $ amqp_bytes_t_#len.);
29 failurescxx . . $ failures.++;
30 }
31 }
32
33 static void expect_acceptcxx . . $ expect_accept(6efac419a2592ea9).(const char *name, amqp_bytes_tcxx . . $ amqp_bytes_t# encoded, size_t offset,
34 size_t len) {
35 amqp_bytes_tcxx . . $ amqp_bytes_t# output;
36 size_t off = offset;
37 output.bytescxx . . $ amqp_bytes_t_#bytes. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
38 output.lencxx . . $ amqp_bytes_t_#len. = 0;
39 if (!amqp_decode_bytescxx . . $ amqp_decode_bytes(8ef006f6ddb737a0).(encoded, &off, &output, len)) {
40 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,
41 "FAIL %s: amqp_decode_bytes rejected a valid length "
42 "(offset=%zu len=%zu buffer=%zu)\n",
43 name, offset, len, encoded.lencxx . . $ amqp_bytes_t_#len.);
44 failurescxx . . $ failures.++;
45 return;
46 }
47 if (output.lencxx . . $ amqp_bytes_t_#len. != len || output.bytescxx . . $ amqp_bytes_t_#bytes. != amqp_offsetcxx . . $ amqp_offset(417ab160f6baadc6).(encoded.bytescxx . . $ amqp_bytes_t_#bytes., offset)) {
48 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "FAIL %s: amqp_decode_bytes produced wrong output\n", name);
49 failurescxx . . $ failures.++;
50 }
51 }
52
53 int maincxx . . $ main(b126dc7c1de90089).(void) {
54 char buffer[16];
55 amqp_bytes_tcxx . . $ amqp_bytes_t# encoded;
56 encoded.bytescxx . . $ amqp_bytes_t_#bytes. = buffer;
57 encoded.lencxx . . $ amqp_bytes_t_#len. = sizeof(buffer);
58
59 /* Normal, in-bounds decodes still work. */
60 expect_acceptcxx . . $ expect_accept(6efac419a2592ea9).("full buffer", encoded, 0, sizeof(buffer));
61 expect_acceptcxx . . $ expect_accept(6efac419a2592ea9).("partial at offset", encoded, 4, 8);
62 expect_acceptcxx . . $ expect_accept(6efac419a2592ea9).("zero length", encoded, 8, 0);
63
64 /* Plain out-of-bounds (no overflow) is rejected. */
65 expect_rejectcxx . . $ expect_reject(6efac419a2592ea9).("len past end", encoded, 0, sizeof(buffer) + 1);
66 expect_rejectcxx . . $ expect_reject(6efac419a2592ea9).("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_rejectcxx . . $ expect_reject(6efac419a2592ea9).("overflow to zero", encoded, 11, (size_t)0 - 11);
74 expect_rejectcxx . . $ expect_reject(6efac419a2592ea9).("overflow wraps small", encoded, 16, (size_t)0 - 8);
75 expect_rejectcxx . . $ expect_reject(6efac419a2592ea9).("max len", encoded, 1, (size_t)-1);
76 expect_rejectcxx . . $ expect_reject(6efac419a2592ea9).("32-bit style len", encoded, 11, (size_t)0xFFFFFFF5u);
77
78 if (failurescxx . . $ failures.) {
79 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%d test(s) failed\n", failurescxx . . $ failures.);
80 return 1;
81 }
82 printfcxx . . $ printf(581fddd72a8db8fc).("all amqp_decode_bytes bounds tests passed\n");
83 return 0;
84 }