/src/FreeRDP/channels/rail/client/test/TestFuzzChannelRail.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * libFuzzer harness for rail (Remote Applications Integrated Locally) PDU parsing |
4 | | */ |
5 | | |
6 | | #include <stddef.h> |
7 | | #include <stdint.h> |
8 | | |
9 | | #include <winpr/crt.h> |
10 | | #include <winpr/stream.h> |
11 | | #include <winpr/wlog.h> |
12 | | |
13 | | #include <freerdp/client/rail.h> |
14 | | |
15 | | #include "../rail_main.h" |
16 | | #include "../rail_orders.h" |
17 | | |
18 | | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) |
19 | 1.32k | { |
20 | 1.32k | if (size < 2) |
21 | 1 | return 0; |
22 | 1.32k | if (size > (1u << 20)) |
23 | 0 | return 0; |
24 | | |
25 | 1.32k | int rc = -1; |
26 | 1.32k | railPlugin* g_rail = (railPlugin*)calloc(1, sizeof(railPlugin)); |
27 | 1.32k | RailClientContext* context = (RailClientContext*)calloc(1, sizeof(RailClientContext)); |
28 | 1.32k | wStream* s = Stream_New(nullptr, size); |
29 | 1.32k | if (!g_rail || !context || !s) |
30 | 0 | goto fail; |
31 | | |
32 | 1.32k | g_rail->log = WLog_Get("fuzz.rail"); |
33 | | |
34 | | /* A context is required (handlers bail on nullptr); nullptr callbacks skip dispatch. */ |
35 | | |
36 | 1.32k | g_rail->context = context; |
37 | 1.32k | g_rail->channelEntryPoints.pInterface = context; |
38 | | |
39 | | /* rail_order_recv owns and frees the stream (Stream_Free(s, TRUE)); give it an owned copy. */ |
40 | | |
41 | 1.32k | Stream_Write(s, data, size); |
42 | 1.32k | Stream_SealLength(s); |
43 | 1.32k | if (!Stream_SetPosition(s, 0)) |
44 | 0 | goto fail; |
45 | | |
46 | 1.32k | (void)rail_order_recv(g_rail, s); |
47 | 1.32k | s = nullptr; // Freed up by rail_order_recv |
48 | | |
49 | 1.32k | rc = 0; |
50 | | |
51 | 1.32k | fail: |
52 | 1.32k | Stream_Free(s, TRUE); |
53 | 1.32k | free(context); |
54 | 1.32k | free(g_rail); |
55 | 1.32k | return rc; |
56 | 1.32k | } |