1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
|
/* $Id: fuzzmastercmd.cpp $ */
/** @file
* IPRT - Fuzzing framework API, master command.
*/
/*
* Copyright (C) 2018-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/fuzz.h>
#include "internal/iprt.h"
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/base64.h>
#include <iprt/buildconfig.h>
#include <iprt/ctype.h>
#include <iprt/err.h>
#include <iprt/file.h>
#include <iprt/getopt.h>
#include <iprt/json.h>
#include <iprt/list.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/path.h>
#include <iprt/process.h>
#include <iprt/stream.h>
#include <iprt/string.h>
#include <iprt/tcp.h>
#include <iprt/thread.h>
#include <iprt/vfs.h>
#include <iprt/zip.h>
/**
* A running fuzzer state.
*/
typedef struct RTFUZZRUN
{
/** List node. */
RTLISTNODE NdFuzzed;
/** Identifier. */
char *pszId;
/** Number of processes. */
uint32_t cProcs;
/** The fuzzing observer state handle. */
RTFUZZOBS hFuzzObs;
/** Flag whether fuzzing was started. */
bool fStarted;
} RTFUZZRUN;
/** Pointer to a running fuzzer state. */
typedef RTFUZZRUN *PRTFUZZRUN;
/**
* Fuzzing master command state.
*/
typedef struct RTFUZZCMDMASTER
{
/** List of running fuzzers. */
RTLISTANCHOR LstFuzzed;
/** The port to listen on. */
uint16_t uPort;
/** The TCP server for requests. */
PRTTCPSERVER hTcpSrv;
/** The root temp directory. */
const char *pszTmpDir;
/** The root results directory. */
const char *pszResultsDir;
/** Flag whether to shutdown. */
bool fShutdown;
/** The response message. */
char *pszResponse;
} RTFUZZCMDMASTER;
/** Pointer to a fuzzing master command state. */
typedef RTFUZZCMDMASTER *PRTFUZZCMDMASTER;
/**
* Wrapper around RTErrInfoSetV / RTMsgErrorV.
*
* @returns @a rc
* @param pErrInfo Extended error info.
* @param rc The return code.
* @param pszFormat The message format.
* @param ... The message format arguments.
*/
static int rtFuzzCmdMasterErrorRc(PRTERRINFO pErrInfo, int rc, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
if (pErrInfo)
RTErrInfoSetV(pErrInfo, rc, pszFormat, va);
else
RTMsgErrorV(pszFormat, va);
va_end(va);
return rc;
}
/**
* Returns a running fuzzer state by the given ID.
*
* @returns Pointer to the running fuzzer state or NULL if not found.
* @param pThis The fuzzing master command state.
* @param pszId The ID to look for.
*/
static PRTFUZZRUN rtFuzzCmdMasterGetFuzzerById(PRTFUZZCMDMASTER pThis, const char *pszId)
{
PRTFUZZRUN pIt = NULL;
RTListForEach(&pThis->LstFuzzed, pIt, RTFUZZRUN, NdFuzzed)
{
if (!RTStrCmp(pIt->pszId, pszId))
return pIt;
}
return NULL;
}
#if 0 /* unused */
/**
* Processes and returns the value of the given config item in the JSON request.
*
* @returns IPRT status code.
* @param ppszStr Where to store the pointer to the string on success.
* @param pszCfgItem The config item to resolve.
* @param hJsonCfg The JSON object containing the item.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessCfgString(char **ppszStr, const char *pszCfgItem, RTJSONVAL hJsonCfg, PRTERRINFO pErrInfo)
{
int rc = RTJsonValueQueryStringByName(hJsonCfg, pszCfgItem, ppszStr);
if (RT_FAILURE(rc))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Failed to query string value of \"%s\"", pszCfgItem);
return rc;
}
/**
* Processes and returns the value of the given config item in the JSON request.
*
* @returns IPRT status code.
* @param pfVal Where to store the config value on success.
* @param pszCfgItem The config item to resolve.
* @param hJsonCfg The JSON object containing the item.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessCfgBool(bool *pfVal, const char *pszCfgItem, RTJSONVAL hJsonCfg, PRTERRINFO pErrInfo)
{
int rc = RTJsonValueQueryBooleanByName(hJsonCfg, pszCfgItem, pfVal);
if (RT_FAILURE(rc))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Failed to query boolean value of \"%s\"", pszCfgItem);
return rc;
}
/**
* Processes and returns the value of the given config item in the JSON request.
*
* @returns IPRT status code.
* @param pfVal Where to store the config value on success.
* @param pszCfgItem The config item to resolve.
* @param hJsonCfg The JSON object containing the item.
* @param fDef Default value if the item wasn't found.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessCfgBoolDef(bool *pfVal, const char *pszCfgItem, RTJSONVAL hJsonCfg, bool fDef, PRTERRINFO pErrInfo)
{
int rc = RTJsonValueQueryBooleanByName(hJsonCfg, pszCfgItem, pfVal);
if (rc == VERR_NOT_FOUND)
{
*pfVal = fDef;
rc = VINF_SUCCESS;
}
else if (RT_FAILURE(rc))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Failed to query boolean value of \"%s\"", pszCfgItem);
return rc;
}
#endif
/**
* Processes and returns the value of the given config item in the JSON request.
*
* @returns IPRT status code.
* @param pcbVal Where to store the config value on success.
* @param pszCfgItem The config item to resolve.
* @param hJsonCfg The JSON object containing the item.
* @param cbDef Default value if the item wasn't found.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessCfgSizeDef(size_t *pcbVal, const char *pszCfgItem, RTJSONVAL hJsonCfg, size_t cbDef, PRTERRINFO pErrInfo)
{
*pcbVal = cbDef; /* Make GCC 6.3.0 happy. */
int64_t i64Val = 0;
int rc = RTJsonValueQueryIntegerByName(hJsonCfg, pszCfgItem, &i64Val);
if (rc == VERR_NOT_FOUND)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Failed to query size_t value of \"%s\"", pszCfgItem);
else if (i64Val < 0 || (size_t)i64Val != (uint64_t)i64Val)
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_OUT_OF_RANGE, "JSON request malformed: Integer \"%s\" is out of range", pszCfgItem);
else
*pcbVal = (size_t)i64Val;
return rc;
}
/**
* Processes and returns the value of the given config item in the JSON request.
*
* @returns IPRT status code.
* @param pcbVal Where to store the config value on success.
* @param pszCfgItem The config item to resolve.
* @param hJsonCfg The JSON object containing the item.
* @param cbDef Default value if the item wasn't found.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessCfgU32Def(uint32_t *pu32Val, const char *pszCfgItem, RTJSONVAL hJsonCfg, uint32_t u32Def, PRTERRINFO pErrInfo)
{
int64_t i64Val = 0;
int rc = RTJsonValueQueryIntegerByName(hJsonCfg, pszCfgItem, &i64Val);
if (rc == VERR_NOT_FOUND)
{
*pu32Val = u32Def;
rc = VINF_SUCCESS;
}
else if (RT_FAILURE(rc))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Failed to query uint32_t value of \"%s\"", pszCfgItem);
else if (i64Val < 0 || (uint32_t)i64Val != (uint64_t)i64Val)
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_OUT_OF_RANGE, "JSON request malformed: Integer \"%s\" is out of range", pszCfgItem);
else
*pu32Val = (uint32_t)i64Val;
return rc;
}
/**
* Returns the configured input channel for the binary under test.
*
* @returns Selected input channel or RTFUZZOBSINPUTCHAN_INVALID if an error occurred.
* @param pszCfgItem The config item to resolve.
* @param hJsonCfg The JSON object containing the item.
* @param enmChanDef Default value if the item wasn't found.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static RTFUZZOBSINPUTCHAN rtFuzzCmdMasterFuzzRunProcessCfgGetInputChan(const char *pszCfgItem, RTJSONVAL hJsonCfg, RTFUZZOBSINPUTCHAN enmChanDef, PRTERRINFO pErrInfo)
{
RTFUZZOBSINPUTCHAN enmInputChan = RTFUZZOBSINPUTCHAN_INVALID;
RTJSONVAL hJsonVal;
int rc = RTJsonValueQueryByName(hJsonCfg, pszCfgItem, &hJsonVal);
if (rc == VERR_NOT_FOUND)
enmInputChan = enmChanDef;
else if (RT_SUCCESS(rc))
{
const char *pszBinary = RTJsonValueGetString(hJsonVal);
if (pszBinary)
{
if (!RTStrCmp(pszBinary, "File"))
enmInputChan = RTFUZZOBSINPUTCHAN_FILE;
else if (!RTStrCmp(pszBinary, "Stdin"))
enmInputChan = RTFUZZOBSINPUTCHAN_STDIN;
else if (!RTStrCmp(pszBinary, "FuzzingAware"))
enmInputChan = RTFUZZOBSINPUTCHAN_FUZZING_AWARE_CLIENT;
else
rtFuzzCmdMasterErrorRc(pErrInfo, VERR_INVALID_PARAMETER, "JSON request malformed: \"%s\" for \"%s\" is not known", pszCfgItem, pszBinary);
}
else
rtFuzzCmdMasterErrorRc(pErrInfo, VERR_INVALID_STATE, "JSON request malformed: \"%s\" is not a string", pszCfgItem);
RTJsonValueRelease(hJsonVal);
}
else
rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Failed to query \"%s\"", pszCfgItem);
return enmInputChan;
}
/**
* Processes binary related configs for the given fuzzing run.
*
* @returns IPRT status code.
* @param pFuzzRun The fuzzing run.
* @param hJsonRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessBinaryCfg(PRTFUZZRUN pFuzzRun, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
RTJSONVAL hJsonVal;
int rc = RTJsonValueQueryByName(hJsonRoot, "BinaryPath", &hJsonVal);
if (RT_SUCCESS(rc))
{
const char *pszBinary = RTJsonValueGetString(hJsonVal);
if (RT_LIKELY(pszBinary))
{
RTFUZZOBSINPUTCHAN enmInputChan = rtFuzzCmdMasterFuzzRunProcessCfgGetInputChan("InputChannel", hJsonRoot, RTFUZZOBSINPUTCHAN_STDIN, pErrInfo);
if (enmInputChan != RTFUZZOBSINPUTCHAN_INVALID)
{
rc = RTFuzzObsSetTestBinary(pFuzzRun->hFuzzObs, pszBinary, enmInputChan);
if (RT_FAILURE(rc))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Failed to add the binary path for the fuzzing run");
}
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_INVALID_STATE, "JSON request malformed: \"BinaryPath\" is not a string");
RTJsonValueRelease(hJsonVal);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Failed to query value of \"BinaryPath\"");
return rc;
}
/**
* Processes argument related configs for the given fuzzing run.
*
* @returns IPRT status code.
* @param pFuzzRun The fuzzing run.
* @param hJsonRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessArgCfg(PRTFUZZRUN pFuzzRun, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
RTJSONVAL hJsonValArgArray;
int rc = RTJsonValueQueryByName(hJsonRoot, "Arguments", &hJsonValArgArray);
if (RT_SUCCESS(rc))
{
unsigned cArgs = 0;
rc = RTJsonValueQueryArraySize(hJsonValArgArray, &cArgs);
if (RT_SUCCESS(rc))
{
if (cArgs > 0)
{
const char **papszArgs = (const char **)RTMemAllocZ(cArgs * sizeof(const char *));
RTJSONVAL *pahJsonVal = (RTJSONVAL *)RTMemAllocZ(cArgs * sizeof(RTJSONVAL));
if (RT_LIKELY(papszArgs && pahJsonVal))
{
unsigned idx = 0;
for (idx = 0; idx < cArgs && RT_SUCCESS(rc); idx++)
{
rc = RTJsonValueQueryByIndex(hJsonValArgArray, idx, &pahJsonVal[idx]);
if (RT_SUCCESS(rc))
{
papszArgs[idx] = RTJsonValueGetString(pahJsonVal[idx]);
if (RT_UNLIKELY(!papszArgs[idx]))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_INVALID_STATE, "Argument %u is not a string", idx);
}
}
if (RT_SUCCESS(rc))
{
rc = RTFuzzObsSetTestBinaryArgs(pFuzzRun->hFuzzObs, papszArgs, cArgs);
if (RT_FAILURE(rc))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Failed to set arguments for the fuzzing run");
}
/* Release queried values. */
while (idx > 0)
{
RTJsonValueRelease(pahJsonVal[idx - 1]);
idx--;
}
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_NO_MEMORY, "Out of memory allocating memory for the argument vector");
if (papszArgs)
RTMemFree(papszArgs);
if (pahJsonVal)
RTMemFree(pahJsonVal);
}
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: \"Arguments\" is not an array");
RTJsonValueRelease(hJsonValArgArray);
}
return rc;
}
/**
* Processes the given seed and adds it to the input corpus.
*
* @returns IPRT status code.
* @param hFuzzCtx The fuzzing context handle.
* @param pszCompression Compression used for the seed.
* @param pszSeed The seed as a base64 encoded string.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessSeed(RTFUZZCTX hFuzzCtx, const char *pszCompression, const char *pszSeed, PRTERRINFO pErrInfo)
{
int rc = VINF_SUCCESS;
ssize_t cbSeedDecoded = RTBase64DecodedSize(pszSeed, NULL);
if (cbSeedDecoded > 0)
{
uint8_t *pbSeedDecoded = (uint8_t *)RTMemAllocZ(cbSeedDecoded);
if (RT_LIKELY(pbSeedDecoded))
{
rc = RTBase64Decode(pszSeed, pbSeedDecoded, cbSeedDecoded, NULL, NULL);
if (RT_SUCCESS(rc))
{
/* Decompress if applicable. */
if (!RTStrICmp(pszCompression, "None"))
rc = RTFuzzCtxCorpusInputAdd(hFuzzCtx, pbSeedDecoded, cbSeedDecoded);
else
{
RTVFSIOSTREAM hVfsIosSeed;
rc = RTVfsIoStrmFromBuffer(RTFILE_O_READ, pbSeedDecoded, cbSeedDecoded, &hVfsIosSeed);
if (RT_SUCCESS(rc))
{
RTVFSIOSTREAM hVfsDecomp = NIL_RTVFSIOSTREAM;
if (!RTStrICmp(pszCompression, "Gzip"))
rc = RTZipGzipDecompressIoStream(hVfsIosSeed, RTZIPGZIPDECOMP_F_ALLOW_ZLIB_HDR, &hVfsDecomp);
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_INVALID_STATE, "Request error: Compression \"%s\" is not known", pszCompression);
if (RT_SUCCESS(rc))
{
RTVFSFILE hVfsFile;
rc = RTVfsMemFileCreate(hVfsDecomp, 2 * _1M, &hVfsFile);
if (RT_SUCCESS(rc))
{
rc = RTVfsFileSeek(hVfsFile, 0, RTFILE_SEEK_BEGIN, NULL);
if (RT_SUCCESS(rc))
{
/* The VFS file contains the buffer for the seed now. */
rc = RTFuzzCtxCorpusInputAddFromVfsFile(hFuzzCtx, hVfsFile);
if (RT_FAILURE(rc))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to add input seed");
RTVfsFileRelease(hVfsFile);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_INVALID_STATE, "Request error: Failed to seek to the beginning of the seed");
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_INVALID_STATE, "Request error: Failed to decompress input seed");
RTVfsIoStrmRelease(hVfsDecomp);
}
RTVfsIoStrmRelease(hVfsIosSeed);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to create I/O stream from seed buffer");
}
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to decode the seed string");
RTMemFree(pbSeedDecoded);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_NO_MEMORY, "Request error: Failed to allocate %zd bytes of memory for the seed", cbSeedDecoded);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_INVALID_STATE, "JSON request malformed: Couldn't find \"Seed\" doesn't contain a base64 encoded value");
return rc;
}
/**
* Processes a signle input seed for the given fuzzing run.
*
* @returns IPRT status code.
* @param pFuzzRun The fuzzing run.
* @param hJsonSeed The seed node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessInputSeedSingle(PRTFUZZRUN pFuzzRun, RTJSONVAL hJsonSeed, PRTERRINFO pErrInfo)
{
RTFUZZCTX hFuzzCtx;
int rc = RTFuzzObsQueryCtx(pFuzzRun->hFuzzObs, &hFuzzCtx);
if (RT_SUCCESS(rc))
{
RTJSONVAL hJsonValComp;
rc = RTJsonValueQueryByName(hJsonSeed, "Compression", &hJsonValComp);
if (RT_SUCCESS(rc))
{
const char *pszCompression = RTJsonValueGetString(hJsonValComp);
if (RT_LIKELY(pszCompression))
{
RTJSONVAL hJsonValSeed;
rc = RTJsonValueQueryByName(hJsonSeed, "Seed", &hJsonValSeed);
if (RT_SUCCESS(rc))
{
const char *pszSeed = RTJsonValueGetString(hJsonValSeed);
if (RT_LIKELY(pszSeed))
rc = rtFuzzCmdMasterFuzzRunProcessSeed(hFuzzCtx, pszCompression, pszSeed, pErrInfo);
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_INVALID_STATE, "JSON request malformed: \"Seed\" value is not a string");
RTJsonValueRelease(hJsonValSeed);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Couldn't find \"Seed\" value");
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_INVALID_STATE, "JSON request malformed: \"Compression\" value is not a string");
RTJsonValueRelease(hJsonValComp);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Couldn't find \"Compression\" value");
RTFuzzCtxRelease(hFuzzCtx);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Failed to query fuzzing context from observer");
return rc;
}
/**
* Processes input seed related configs for the given fuzzing run.
*
* @returns IPRT status code.
* @param pFuzzRun The fuzzing run.
* @param hJsonRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessInputSeeds(PRTFUZZRUN pFuzzRun, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
RTJSONVAL hJsonValSeedArray;
int rc = RTJsonValueQueryByName(hJsonRoot, "InputSeeds", &hJsonValSeedArray);
if (RT_SUCCESS(rc))
{
RTJSONIT hIt;
rc = RTJsonIteratorBegin(hJsonValSeedArray, &hIt);
if (RT_SUCCESS(rc))
{
RTJSONVAL hJsonInpSeed;
while ( RT_SUCCESS(rc)
&& RTJsonIteratorQueryValue(hIt, &hJsonInpSeed, NULL) != VERR_JSON_ITERATOR_END)
{
rc = rtFuzzCmdMasterFuzzRunProcessInputSeedSingle(pFuzzRun, hJsonInpSeed, pErrInfo);
RTJsonValueRelease(hJsonInpSeed);
if (RT_FAILURE(rc))
break;
rc = RTJsonIteratorNext(hIt);
}
if (rc == VERR_JSON_ITERATOR_END)
rc = VINF_SUCCESS;
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Failed to create array iterator");
RTJsonValueRelease(hJsonValSeedArray);
}
return rc;
}
/**
* Processes miscellaneous config items.
*
* @returns IPRT status code.
* @param pFuzzRun The fuzzing run.
* @param hJsonRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterFuzzRunProcessMiscCfg(PRTFUZZRUN pFuzzRun, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
size_t cbTmp;
int rc = rtFuzzCmdMasterFuzzRunProcessCfgSizeDef(&cbTmp, "InputSeedMax", hJsonRoot, 0, pErrInfo);
if (RT_SUCCESS(rc))
{
RTFUZZCTX hFuzzCtx;
rc = RTFuzzObsQueryCtx(pFuzzRun->hFuzzObs, &hFuzzCtx);
AssertRC(rc);
rc = RTFuzzCtxCfgSetInputSeedMaximum(hFuzzCtx, cbTmp);
if (RT_FAILURE(rc))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to set maximum input seed size to %zu", cbTmp);
}
if (RT_SUCCESS(rc))
rc = rtFuzzCmdMasterFuzzRunProcessCfgU32Def(&pFuzzRun->cProcs, "FuzzingProcs", hJsonRoot, 0, pErrInfo);
return rc;
}
/**
* Creates a new fuzzing run with the given ID.
*
* @returns IPRT status code.
* @param pThis The fuzzing master command state.
* @param pszId The ID to use.
* @param hJsonRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterCreateFuzzRunWithId(PRTFUZZCMDMASTER pThis, const char *pszId, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
int rc = VINF_SUCCESS;
PRTFUZZRUN pFuzzRun = (PRTFUZZRUN)RTMemAllocZ(sizeof(*pFuzzRun));
if (RT_LIKELY(pFuzzRun))
{
pFuzzRun->pszId = RTStrDup(pszId);
if (RT_LIKELY(pFuzzRun->pszId))
{
rc = RTFuzzObsCreate(&pFuzzRun->hFuzzObs);
if (RT_SUCCESS(rc))
{
rc = rtFuzzCmdMasterFuzzRunProcessBinaryCfg(pFuzzRun, hJsonRoot, pErrInfo);
if (RT_SUCCESS(rc))
rc = rtFuzzCmdMasterFuzzRunProcessArgCfg(pFuzzRun, hJsonRoot, pErrInfo);
if (RT_SUCCESS(rc))
rc = rtFuzzCmdMasterFuzzRunProcessInputSeeds(pFuzzRun, hJsonRoot, pErrInfo);
if (RT_SUCCESS(rc))
rc = rtFuzzCmdMasterFuzzRunProcessMiscCfg(pFuzzRun, hJsonRoot, pErrInfo);
if (RT_SUCCESS(rc))
{
/* Create temp directories. */
char szTmpDir[RTPATH_MAX];
rc = RTPathJoin(&szTmpDir[0], sizeof(szTmpDir), pThis->pszTmpDir, pFuzzRun->pszId);
AssertRC(rc);
rc = RTDirCreate(szTmpDir, 0700, RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_SET
| RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL);
if (RT_SUCCESS(rc))
{
rc = RTFuzzObsSetTmpDirectory(pFuzzRun->hFuzzObs, szTmpDir);
if (RT_SUCCESS(rc))
{
rc = RTPathJoin(&szTmpDir[0], sizeof(szTmpDir), pThis->pszResultsDir, pFuzzRun->pszId);
AssertRC(rc);
rc = RTDirCreate(szTmpDir, 0700, RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_SET
| RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL);
if (RT_SUCCESS(rc))
{
rc = RTFuzzObsSetResultDirectory(pFuzzRun->hFuzzObs, szTmpDir);
if (RT_SUCCESS(rc))
{
/* Start fuzzing. */
RTListAppend(&pThis->LstFuzzed, &pFuzzRun->NdFuzzed);
rc = RTFuzzObsExecStart(pFuzzRun->hFuzzObs, pFuzzRun->cProcs);
if (RT_SUCCESS(rc))
pFuzzRun->fStarted = true;
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to start fuzzing with %Rrc", rc);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to set results directory to %s", szTmpDir);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to create results directory %s", szTmpDir);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to set temporary directory to %s", szTmpDir);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to create temporary directory %s", szTmpDir);
}
}
}
else
rc = VERR_NO_STR_MEMORY;
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_NO_MEMORY, "Request error: Out of memory allocating the fuzzer state");
return rc;
}
/**
* Resolves the fuzzing run from the given ID config item and the given JSON request.
*
* @returns IPRT status code.
* @param pThis The fuzzing master command state.
* @param hJsonRoot The root node of the JSON request.
* @param pszIdItem The JSON item which contains the ID of the fuzzing run.
* @param ppFuzzRun Where to store the pointer to the fuzzing run on success.
*/
static int rtFuzzCmdMasterQueryFuzzRunFromJson(PRTFUZZCMDMASTER pThis, RTJSONVAL hJsonRoot, const char *pszIdItem, PRTERRINFO pErrInfo,
PRTFUZZRUN *ppFuzzRun)
{
RTJSONVAL hJsonValId;
int rc = RTJsonValueQueryByName(hJsonRoot, pszIdItem, &hJsonValId);
if (RT_SUCCESS(rc))
{
const char *pszId = RTJsonValueGetString(hJsonValId);
if (pszId)
{
PRTFUZZRUN pFuzzRun = rtFuzzCmdMasterGetFuzzerById(pThis, pszId);
if (pFuzzRun)
*ppFuzzRun = pFuzzRun;
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_NOT_FOUND, "Request error: The ID \"%s\" wasn't found", pszId);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_JSON_VALUE_INVALID_TYPE, "JSON request malformed: \"Id\" is not a string value");
RTJsonValueRelease(hJsonValId);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Couldn't find \"Id\" value");
return rc;
}
/**
* Processes the "StartFuzzing" request.
*
* @returns IPRT status code.
* @param pThis The fuzzing master command state.
* @param hJsonRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterProcessJsonReqStart(PRTFUZZCMDMASTER pThis, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
RTJSONVAL hJsonValId;
int rc = RTJsonValueQueryByName(hJsonRoot, "Id", &hJsonValId);
if (RT_SUCCESS(rc))
{
const char *pszId = RTJsonValueGetString(hJsonValId);
if (pszId)
{
PRTFUZZRUN pFuzzRun = rtFuzzCmdMasterGetFuzzerById(pThis, pszId);
if (!pFuzzRun)
rc = rtFuzzCmdMasterCreateFuzzRunWithId(pThis, pszId, hJsonRoot, pErrInfo);
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_ALREADY_EXISTS, "Request error: The ID \"%s\" is already registered", pszId);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_JSON_VALUE_INVALID_TYPE, "JSON request malformed: \"Id\" is not a string value");
RTJsonValueRelease(hJsonValId);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Couldn't find \"Id\" value");
return rc;
}
/**
* Processes the "StopFuzzing" request.
*
* @returns IPRT status code.
* @param pThis The fuzzing master command state.
* @param hJsonValRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterProcessJsonReqStop(PRTFUZZCMDMASTER pThis, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
PRTFUZZRUN pFuzzRun;
int rc = rtFuzzCmdMasterQueryFuzzRunFromJson(pThis, hJsonRoot, "Id", pErrInfo, &pFuzzRun);
if (RT_SUCCESS(rc))
{
RTListNodeRemove(&pFuzzRun->NdFuzzed);
RTFuzzObsExecStop(pFuzzRun->hFuzzObs);
RTFuzzObsDestroy(pFuzzRun->hFuzzObs);
RTStrFree(pFuzzRun->pszId);
RTMemFree(pFuzzRun);
}
return rc;
}
/**
* Processes the "SuspendFuzzing" request.
*
* @returns IPRT status code.
* @param pThis The fuzzing master command state.
* @param hJsonValRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterProcessJsonReqSuspend(PRTFUZZCMDMASTER pThis, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
PRTFUZZRUN pFuzzRun;
int rc = rtFuzzCmdMasterQueryFuzzRunFromJson(pThis, hJsonRoot, "Id", pErrInfo, &pFuzzRun);
if (RT_SUCCESS(rc))
{
if (pFuzzRun->fStarted)
{
rc = RTFuzzObsExecStop(pFuzzRun->hFuzzObs);
if (RT_SUCCESS(rc))
pFuzzRun->fStarted = false;
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Suspending the fuzzing process failed");
}
}
return rc;
}
/**
* Processes the "ResumeFuzzing" request.
*
* @returns IPRT status code.
* @param pThis The fuzzing master command state.
* @param hJsonValRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterProcessJsonReqResume(PRTFUZZCMDMASTER pThis, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
PRTFUZZRUN pFuzzRun;
int rc = rtFuzzCmdMasterQueryFuzzRunFromJson(pThis, hJsonRoot, "Id", pErrInfo, &pFuzzRun);
if (RT_SUCCESS(rc))
{
if (!pFuzzRun->fStarted)
{
rc = rtFuzzCmdMasterFuzzRunProcessCfgU32Def(&pFuzzRun->cProcs, "FuzzingProcs", hJsonRoot, pFuzzRun->cProcs, pErrInfo);
if (RT_SUCCESS(rc))
{
rc = RTFuzzObsExecStart(pFuzzRun->hFuzzObs, pFuzzRun->cProcs);
if (RT_SUCCESS(rc))
pFuzzRun->fStarted = true;
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Resuming the fuzzing process failed");
}
}
}
return rc;
}
/**
* Processes the "SaveFuzzingState" request.
*
* @returns IPRT status code.
* @param pThis The fuzzing master command state.
* @param hJsonValRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterProcessJsonReqSaveState(PRTFUZZCMDMASTER pThis, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
PRTFUZZRUN pFuzzRun;
int rc = rtFuzzCmdMasterQueryFuzzRunFromJson(pThis, hJsonRoot, "Id", pErrInfo, &pFuzzRun);
if (RT_SUCCESS(rc))
{
/* Suspend fuzzing, save and resume if not stopped. */
if (pFuzzRun->fStarted)
{
rc = RTFuzzObsExecStop(pFuzzRun->hFuzzObs);
if (RT_FAILURE(rc))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Suspending the fuzzing process failed");
}
if (RT_SUCCESS(rc))
{
RTFUZZCTX hFuzzCtx;
rc = RTFuzzObsQueryCtx(pFuzzRun->hFuzzObs, &hFuzzCtx);
AssertRC(rc);
void *pvState = NULL;
size_t cbState = 0;
rc = RTFuzzCtxStateExport(hFuzzCtx, &pvState, &cbState);
if (RT_SUCCESS(rc))
{
/* Encode to base64. */
size_t cbStateStr = RTBase64EncodedLength(cbState) + 1;
char *pszState = (char *)RTMemAllocZ(cbStateStr);
if (pszState)
{
rc = RTBase64Encode(pvState, cbState, pszState, cbStateStr, &cbStateStr);
if (RT_SUCCESS(rc))
{
/* Strip all new lines from the srting. */
size_t offStr = 0;
while (offStr < cbStateStr)
{
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
char *pszEol = strchr(&pszState[offStr], '\r');
#else
char *pszEol = strchr(&pszState[offStr], '\n');
#endif
if (pszEol)
{
offStr += pszEol - &pszState[offStr];
memmove(pszEol, &pszEol[RTBASE64_EOL_SIZE], cbStateStr - offStr - RTBASE64_EOL_SIZE);
cbStateStr -= RTBASE64_EOL_SIZE;
}
else
break;
}
const char s_szState[] = "{ \"State\": %s }";
pThis->pszResponse = RTStrAPrintf2(s_szState, pszState);
if (RT_UNLIKELY(!pThis->pszResponse))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_BUFFER_OVERFLOW, "Request error: Response data buffer overflow", rc);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to encode the state as a base64 string");
RTMemFree(pszState);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_NO_STR_MEMORY, "Request error: Failed to allocate a state string for the response");
RTMemFree(pvState);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Exporting the state failed");
}
if (pFuzzRun->fStarted)
{
int rc2 = RTFuzzObsExecStart(pFuzzRun->hFuzzObs, pFuzzRun->cProcs);
if (RT_FAILURE(rc2))
rtFuzzCmdMasterErrorRc(pErrInfo, rc2, "Request error: Resuming the fuzzing process failed");
}
}
return rc;
}
/**
* Processes the "QueryStats" request.
*
* @returns IPRT status code.
* @param pThis The fuzzing master command state.
* @param hJsonValRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterProcessJsonReqQueryStats(PRTFUZZCMDMASTER pThis, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
PRTFUZZRUN pFuzzRun;
int rc = rtFuzzCmdMasterQueryFuzzRunFromJson(pThis, hJsonRoot, "Id", pErrInfo, &pFuzzRun);
if (RT_SUCCESS(rc))
{
RTFUZZOBSSTATS Stats;
rc = RTFuzzObsQueryStats(pFuzzRun->hFuzzObs, &Stats);
if (RT_SUCCESS(rc))
{
const char s_szStats[] = "{ \"FuzzedInputsPerSec\": %u\n"
" \"FuzzedInputs\": %u\n"
" \"FuzzedInputsHang\": %u\n"
" \"FuzzedInputsCrash\": %u\n}";
pThis->pszResponse = RTStrAPrintf2(s_szStats, Stats.cFuzzedInputsPerSec,
Stats.cFuzzedInputs, Stats.cFuzzedInputsHang, Stats.cFuzzedInputsCrash);
if (RT_UNLIKELY(!pThis->pszResponse))
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_BUFFER_OVERFLOW, "Request error: Response data buffer overflow", rc);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "Request error: Failed to query fuzzing statistics with %Rrc", rc);
}
return rc;
}
/**
* Processes a JSON request.
*
* @returns IPRT status code.
* @param pThis The fuzzing master command state.
* @param hJsonValRoot The root node of the JSON request.
* @param pErrInfo Where to store the error information on failure, optional.
*/
static int rtFuzzCmdMasterProcessJsonReq(PRTFUZZCMDMASTER pThis, RTJSONVAL hJsonRoot, PRTERRINFO pErrInfo)
{
RTJSONVAL hJsonValReq;
int rc = RTJsonValueQueryByName(hJsonRoot, "Request", &hJsonValReq);
if (RT_SUCCESS(rc))
{
const char *pszReq = RTJsonValueGetString(hJsonValReq);
if (pszReq)
{
if (!RTStrCmp(pszReq, "StartFuzzing"))
rc = rtFuzzCmdMasterProcessJsonReqStart(pThis, hJsonRoot, pErrInfo);
else if (!RTStrCmp(pszReq, "StopFuzzing"))
rc = rtFuzzCmdMasterProcessJsonReqStop(pThis, hJsonRoot, pErrInfo);
else if (!RTStrCmp(pszReq, "SuspendFuzzing"))
rc = rtFuzzCmdMasterProcessJsonReqSuspend(pThis, hJsonRoot, pErrInfo);
else if (!RTStrCmp(pszReq, "ResumeFuzzing"))
rc = rtFuzzCmdMasterProcessJsonReqResume(pThis, hJsonRoot, pErrInfo);
else if (!RTStrCmp(pszReq, "SaveFuzzingState"))
rc = rtFuzzCmdMasterProcessJsonReqSaveState(pThis, hJsonRoot, pErrInfo);
else if (!RTStrCmp(pszReq, "QueryStats"))
rc = rtFuzzCmdMasterProcessJsonReqQueryStats(pThis, hJsonRoot, pErrInfo);
else if (!RTStrCmp(pszReq, "Shutdown"))
pThis->fShutdown = true;
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_JSON_VALUE_INVALID_TYPE, "JSON request malformed: \"Request\" contains unknown value \"%s\"", pszReq);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, VERR_JSON_VALUE_INVALID_TYPE, "JSON request malformed: \"Request\" is not a string value");
RTJsonValueRelease(hJsonValReq);
}
else
rc = rtFuzzCmdMasterErrorRc(pErrInfo, rc, "JSON request malformed: Couldn't find \"Request\" value");
return rc;
}
/**
* Loads a fuzzing configuration for immediate startup from the given file.
*
* @returns IPRT status code.
* @param pThis The fuzzing master command state.
* @param pszFuzzCfg The fuzzing config to load.
*/
static int rtFuzzCmdMasterFuzzCfgLoadFromFile(PRTFUZZCMDMASTER pThis, const char *pszFuzzCfg)
{
RTJSONVAL hJsonRoot;
int rc = RTJsonParseFromFile(&hJsonRoot, pszFuzzCfg, NULL);
if (RT_SUCCESS(rc))
{
rc = rtFuzzCmdMasterProcessJsonReqStart(pThis, hJsonRoot, NULL);
RTJsonValueRelease(hJsonRoot);
}
else
rc = rtFuzzCmdMasterErrorRc(NULL, rc, "JSON request malformed: Couldn't load file \"%s\"", pszFuzzCfg);
return rc;
}
/**
* Destroys all running fuzzers for the given master state.
*
* @returns nothing.
* @param pThis The fuzzing master command state.
*/
static void rtFuzzCmdMasterDestroy(PRTFUZZCMDMASTER pThis)
{
RT_NOREF(pThis);
}
/**
* Sends an ACK response to the client.
*
* @returns nothing.
* @param hSocket The socket handle to send the ACK to.
* @param pszResponse Additional response data.
*/
static void rtFuzzCmdMasterTcpSendAck(RTSOCKET hSocket, const char *pszResponse)
{
const char s_szSucc[] = "{ \"Status\": \"ACK\" }\n";
const char s_szSuccResp[] = "{ \"Status\": \"ACK\"\n \"Response\":\n";
const char s_szSuccRespClose[] = "\n }\n";
if (pszResponse)
{
RTSGSEG aSegs[3];
RTSGBUF SgBuf;
aSegs[0].pvSeg = (void *)s_szSuccResp;
aSegs[0].cbSeg = sizeof(s_szSuccResp) - 1;
aSegs[1].pvSeg = (void *)pszResponse;
aSegs[1].cbSeg = strlen(pszResponse);
aSegs[2].pvSeg = (void *)s_szSuccRespClose;
aSegs[2].cbSeg = sizeof(s_szSuccRespClose) - 1;
RTSgBufInit(&SgBuf, &aSegs[0], RT_ELEMENTS(aSegs));
RTTcpSgWrite(hSocket, &SgBuf);
}
else
RTTcpWrite(hSocket, s_szSucc, sizeof(s_szSucc));
}
/**
* Sends an NACK response to the client.
*
* @returns nothing.
* @param hSocket The socket handle to send the ACK to.
* @param pErrInfo Optional error information to send along.
*/
static void rtFuzzCmdMasterTcpSendNAck(RTSOCKET hSocket, PRTERRINFO pErrInfo)
{
const char s_szFail[] = "{ \"Status\": \"NACK\" }\n";
const char s_szFailInfo[] = "{ \"Status\": \"NACK\"\n \"Information\": \"%s\" }\n";
if (pErrInfo)
{
char szTmp[_1K];
ssize_t cchResp = RTStrPrintf2(szTmp, sizeof(szTmp), s_szFailInfo, pErrInfo->pszMsg);
if (cchResp > 0)
RTTcpWrite(hSocket, szTmp, cchResp);
else
RTTcpWrite(hSocket, s_szFail, strlen(s_szFail));
}
else
RTTcpWrite(hSocket, s_szFail, strlen(s_szFail));
}
/**
* TCP server serving callback for a single connection.
*
* @returns IPRT status code.
* @param hSocket The socket handle of the connection.
* @param pvUser Opaque user data.
*/
static DECLCALLBACK(int) rtFuzzCmdMasterTcpServe(RTSOCKET hSocket, void *pvUser)
{
PRTFUZZCMDMASTER pThis = (PRTFUZZCMDMASTER)pvUser;
size_t cbReqMax = _32K;
size_t cbReq = 0;
uint8_t *pbReq = (uint8_t *)RTMemAllocZ(cbReqMax);
if (RT_LIKELY(pbReq))
{
uint8_t *pbCur = pbReq;
for (;;)
{
size_t cbThisRead = cbReqMax - cbReq;
int rc = RTTcpRead(hSocket, pbCur, cbThisRead, &cbThisRead);
if (RT_SUCCESS(rc))
{
cbReq += cbThisRead;
/* Check for a zero terminator marking the end of the request. */
uint8_t *pbEnd = (uint8_t *)memchr(pbCur, 0, cbThisRead);
if (pbEnd)
{
/* Adjust request size, data coming after the zero terminiator is ignored right now. */
cbReq -= cbThisRead - (pbEnd - pbCur) + 1;
RTJSONVAL hJsonReq;
RTERRINFOSTATIC ErrInfo;
RTErrInfoInitStatic(&ErrInfo);
rc = RTJsonParseFromBuf(&hJsonReq, pbReq, cbReq, &ErrInfo.Core);
if (RT_SUCCESS(rc))
{
rc = rtFuzzCmdMasterProcessJsonReq(pThis, hJsonReq, &ErrInfo.Core);
if (RT_SUCCESS(rc))
rtFuzzCmdMasterTcpSendAck(hSocket, pThis->pszResponse);
else
rtFuzzCmdMasterTcpSendNAck(hSocket, &ErrInfo.Core);
RTJsonValueRelease(hJsonReq);
}
else
rtFuzzCmdMasterTcpSendNAck(hSocket, &ErrInfo.Core);
if (pThis->pszResponse)
{
RTStrFree(pThis->pszResponse);
pThis->pszResponse = NULL;
}
break;
}
else if (cbReq == cbReqMax)
{
/* Try to increase the buffer. */
uint8_t *pbReqNew = (uint8_t *)RTMemRealloc(pbReq, cbReqMax + _32K);
if (RT_LIKELY(pbReqNew))
{
cbReqMax += _32K;
pbReq = pbReqNew;
pbCur = pbReq + cbReq;
}
else
rtFuzzCmdMasterTcpSendNAck(hSocket, NULL);
}
else
pbCur += cbThisRead;
}
else
break;
}
}
else
rtFuzzCmdMasterTcpSendNAck(hSocket, NULL);
if (pbReq)
RTMemFree(pbReq);
return pThis->fShutdown ? VERR_TCP_SERVER_STOP : VINF_SUCCESS;
}
/**
* Mainloop for the fuzzing master.
*
* @returns Process exit code.
* @param pThis The fuzzing master command state.
* @param pszLoadCfg Initial config to load.
*/
static RTEXITCODE rtFuzzCmdMasterRun(PRTFUZZCMDMASTER pThis, const char *pszLoadCfg)
{
if (pszLoadCfg)
{
int rc = rtFuzzCmdMasterFuzzCfgLoadFromFile(pThis, pszLoadCfg);
if (RT_FAILURE(rc))
return RTEXITCODE_FAILURE;
}
/* Start up the control server. */
int rc = RTTcpServerCreateEx(NULL, pThis->uPort, &pThis->hTcpSrv);
if (RT_SUCCESS(rc))
{
do
{
rc = RTTcpServerListen(pThis->hTcpSrv, rtFuzzCmdMasterTcpServe, pThis);
} while (rc != VERR_TCP_SERVER_STOP);
}
RTTcpServerDestroy(pThis->hTcpSrv);
rtFuzzCmdMasterDestroy(pThis);
return RTEXITCODE_SUCCESS;
}
RTR3DECL(RTEXITCODE) RTFuzzCmdMaster(unsigned cArgs, char **papszArgs)
{
/*
* Parse the command line.
*/
static const RTGETOPTDEF s_aOptions[] =
{
{ "--fuzz-config", 'c', RTGETOPT_REQ_STRING },
{ "--temp-dir", 't', RTGETOPT_REQ_STRING },
{ "--results-dir", 'r', RTGETOPT_REQ_STRING },
{ "--listen-port", 'p', RTGETOPT_REQ_UINT16 },
{ "--daemonize", 'd', RTGETOPT_REQ_NOTHING },
{ "--daemonized", 'Z', RTGETOPT_REQ_NOTHING },
{ "--help", 'h', RTGETOPT_REQ_NOTHING },
{ "--version", 'V', RTGETOPT_REQ_NOTHING },
};
RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
RTGETOPTSTATE GetState;
int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
RTGETOPTINIT_FLAGS_OPTS_FIRST);
if (RT_SUCCESS(rc))
{
/* Option variables: */
bool fDaemonize = false;
bool fDaemonized = false;
const char *pszLoadCfg = NULL;
RTFUZZCMDMASTER This;
RTListInit(&This.LstFuzzed);
This.hTcpSrv = NIL_RTTCPSERVER;
This.uPort = 4242;
This.pszTmpDir = NULL;
This.pszResultsDir = NULL;
This.fShutdown = false;
This.pszResponse = NULL;
/* Argument parsing loop. */
bool fContinue = true;
do
{
RTGETOPTUNION ValueUnion;
int chOpt = RTGetOpt(&GetState, &ValueUnion);
switch (chOpt)
{
case 0:
fContinue = false;
break;
case 'c':
pszLoadCfg = ValueUnion.psz;
break;
case 'p':
This.uPort = ValueUnion.u16;
break;
case 't':
This.pszTmpDir = ValueUnion.psz;
break;
case 'r':
This.pszResultsDir = ValueUnion.psz;
break;
case 'd':
fDaemonize = true;
break;
case 'Z':
fDaemonized = true;
fDaemonize = false;
break;
case 'h':
RTPrintf("Usage: to be written\nOption dump:\n");
for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
fContinue = false;
break;
case 'V':
RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
fContinue = false;
break;
default:
rcExit = RTGetOptPrintError(chOpt, &ValueUnion);
fContinue = false;
break;
}
} while (fContinue);
if (rcExit == RTEXITCODE_SUCCESS)
{
/*
* Daemonize ourselves if asked to.
*/
if (fDaemonize)
{
rc = RTProcDaemonize(papszArgs, "--daemonized");
if (RT_FAILURE(rc))
return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTProcDaemonize: %Rrc\n", rc);
}
else
rcExit = rtFuzzCmdMasterRun(&This, pszLoadCfg);
}
}
else
rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
return rcExit;
}
|