FreeRDP
Loading...
Searching...
No Matches
TestFuzzChannelRail.c
1
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#include <freerdp/freerdp.h>
15
16#include "../rail_main.h"
17#include "../rail_orders.h"
18
19static void dealloc(railPlugin* plugin)
20{
21 if (!plugin)
22 return;
23 if (plugin->rdpcontext)
24 freerdp_settings_free(plugin->rdpcontext->settings);
25 free(plugin->rdpcontext);
26 free(plugin);
27}
28
29WINPR_ATTR_MALLOC(dealloc, 1)
30static railPlugin* alloc(void)
31{
32 railPlugin* rail = (railPlugin*)calloc(1, sizeof(railPlugin));
33 if (!rail)
34 return nullptr;
35 rail->rdpcontext = calloc(1, sizeof(rdpContext));
36 if (!rail->rdpcontext)
37 goto fail;
38
39 rail->rdpcontext->settings = freerdp_settings_new(0);
40 if (!rail->rdpcontext->settings)
41 goto fail;
42 return rail;
43fail:
44 dealloc(rail);
45 return nullptr;
46}
47
48int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
49{
50 if (size < 2)
51 return 0;
52 if (size > (1u << 20))
53 return 0;
54
55 int rc = -1;
56
57 wLog* root = WLog_GetRoot();
58 (void)WLog_SetLogLevel(root, WLOG_TRACE);
59 (void)WLog_SetLogAppenderType(root, WLOG_APPENDER_CALLBACK);
60
61 railPlugin* g_rail = alloc();
62 RailClientContext* context = (RailClientContext*)calloc(1, sizeof(RailClientContext));
63 wStream* s = Stream_New(nullptr, size);
64 if (!g_rail || !context || !s)
65 goto fail;
66
67 g_rail->log = WLog_Get("fuzz.rail");
68
69 /* A context is required (handlers bail on nullptr); nullptr callbacks skip dispatch. */
70
71 g_rail->context = context;
72 g_rail->channelEntryPoints.pInterface = context;
73
74 /* rail_order_recv owns and frees the stream (Stream_Free(s, TRUE)); give it an owned copy. */
75
76 Stream_Write(s, data, size);
77 Stream_SealLength(s);
78 if (!Stream_SetPosition(s, 0))
79 goto fail;
80
81 (void)rail_order_recv(g_rail, s);
82 s = nullptr; // Freed up by rail_order_recv
83
84 rc = 0;
85
86fail:
87 Stream_Free(s, TRUE);
88 free(context);
89 dealloc(g_rail);
90 return rc;
91}
FREERDP_API rdpSettings * freerdp_settings_new(DWORD flags)
creates a new setting struct
FREERDP_API void freerdp_settings_free(rdpSettings *settings)
Free a settings struct with all data in it.