/src/FreeRDP/channels/cliprdr/client/test/TestFuzzChannelCliprdr.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * libFuzzer harness for cliprdr 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/channels/cliprdr.h> |
14 | | |
15 | | #include "../../cliprdr_common.h" |
16 | | |
17 | | static wLog* g_Log = nullptr; |
18 | | |
19 | | static UINT32 fuzz_stream_len_u32(wStream* s) |
20 | 1.15k | { |
21 | 1.15k | const size_t remaining = Stream_GetRemainingLength(s); |
22 | 1.15k | return (remaining > UINT32_MAX) ? UINT32_MAX : (UINT32)remaining; |
23 | 1.15k | } |
24 | | |
25 | | static void fuzz_format_list(wStream* s, BOOL longNames) |
26 | 562 | { |
27 | 562 | CLIPRDR_FORMAT_LIST list = WINPR_C_ARRAY_INIT; |
28 | 562 | list.common.dataLen = fuzz_stream_len_u32(s); |
29 | | |
30 | 562 | if (cliprdr_read_format_list(g_Log, s, &list, longNames) == CHANNEL_RC_OK) |
31 | 302 | cliprdr_free_format_list(&list); |
32 | 562 | } |
33 | | |
34 | | static void fuzz_format_data_request(wStream* s) |
35 | 67 | { |
36 | 67 | CLIPRDR_FORMAT_DATA_REQUEST request = WINPR_C_ARRAY_INIT; |
37 | 67 | request.common.dataLen = fuzz_stream_len_u32(s); |
38 | 67 | (void)cliprdr_read_format_data_request(s, &request); |
39 | 67 | } |
40 | | |
41 | | static void fuzz_format_data_response(wStream* s) |
42 | 27 | { |
43 | 27 | CLIPRDR_FORMAT_DATA_RESPONSE response = WINPR_C_ARRAY_INIT; |
44 | 27 | response.common.dataLen = fuzz_stream_len_u32(s); |
45 | 27 | (void)cliprdr_read_format_data_response(s, &response); |
46 | 27 | } |
47 | | |
48 | | static void fuzz_file_contents_request(wStream* s) |
49 | 375 | { |
50 | 375 | CLIPRDR_FILE_CONTENTS_REQUEST request = WINPR_C_ARRAY_INIT; |
51 | 375 | request.common.dataLen = fuzz_stream_len_u32(s); |
52 | 375 | (void)cliprdr_read_file_contents_request(s, &request); |
53 | 375 | } |
54 | | |
55 | | static void fuzz_file_contents_response(wStream* s) |
56 | 62 | { |
57 | 62 | CLIPRDR_FILE_CONTENTS_RESPONSE response = WINPR_C_ARRAY_INIT; |
58 | 62 | const UINT32 dataLen = fuzz_stream_len_u32(s); |
59 | | |
60 | 62 | response.common.dataLen = (dataLen >= 4) ? dataLen : 4; |
61 | 62 | (void)cliprdr_read_file_contents_response(s, &response); |
62 | 62 | } |
63 | | |
64 | | static void fuzz_unlock(wStream* s) |
65 | 59 | { |
66 | 59 | CLIPRDR_UNLOCK_CLIPBOARD_DATA data = WINPR_C_ARRAY_INIT; |
67 | 59 | data.common.dataLen = fuzz_stream_len_u32(s); |
68 | 59 | (void)cliprdr_read_unlock_clipdata(s, &data); |
69 | 59 | } |
70 | | |
71 | | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) |
72 | 1.15k | { |
73 | 1.15k | if (size < 2) |
74 | 1 | return 0; |
75 | 1.15k | if (size > (1u << 20)) |
76 | 0 | return 0; |
77 | | |
78 | 1.15k | if (!g_Log) |
79 | 1 | g_Log = WLog_Get("fuzz.cliprdr"); |
80 | | |
81 | 1.15k | BOOL longNames = (data[1] & 0x1) != 0; |
82 | 1.15k | const uint8_t* body = data + 2; |
83 | | |
84 | 1.15k | wStream buffer = WINPR_C_ARRAY_INIT; |
85 | 1.15k | wStream* s = Stream_StaticConstInit(&buffer, body, size - 2); |
86 | 1.15k | if (!s) |
87 | 0 | return 0; |
88 | | |
89 | 1.15k | switch (data[0] % 6) |
90 | 1.15k | { |
91 | 562 | case 0: |
92 | 562 | fuzz_format_list(s, longNames); |
93 | 562 | break; |
94 | 67 | case 1: |
95 | 67 | fuzz_format_data_request(s); |
96 | 67 | break; |
97 | 27 | case 2: |
98 | 27 | fuzz_format_data_response(s); |
99 | 27 | break; |
100 | 375 | case 3: |
101 | 375 | fuzz_file_contents_request(s); |
102 | 375 | break; |
103 | 62 | case 4: |
104 | 62 | fuzz_file_contents_response(s); |
105 | 62 | break; |
106 | 59 | case 5: |
107 | 59 | fuzz_unlock(s); |
108 | 59 | break; |
109 | 0 | default: |
110 | 0 | break; |
111 | 1.15k | } |
112 | | |
113 | 1.15k | return 0; |
114 | 1.15k | } |