-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·2623 lines (2106 loc) · 61.1 KB
/
main.c
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
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma GCC push_options
#pragma GCC optimize ("O0")
#include <zephyr/zephyr.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <zephyr/sys/time_units.h>
#include <zephyr/timing/timing.h>
#include <inttypes.h>
#include "utils.h"
#include "profiling.h"
#include "profiling.c"
#include "fixed_patch_points.h"
#include "fixed_patch_point_def.h"
#include "iotpatch.h"
#include "iotpatch.c"
#include <zephyr/sys/math_extras.h>
//#include "ebpf.h"
#include "fixed_patch_load.c"
//For CVE-2020-10063
#include <zephyr/sys/printk.h>
// #include <net/coap.h>
#include <zephyr/drivers/gpio.h>
#include <setjmp.h>
//For CVE-2020-10021
#include <zephyr/usb/class/usb_msc.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/storage/disk_access.h>
#include <zephyr/usb/class/usb_msc.h>
#include <zephyr/usb/usb_device.h>
// #include "hotpatch.h"
//CVE-2020-10024
//#include <zephyr/kernel_structs.h>
//#include <stdlib.h>
#include <zephyr/app_memory/app_memdomain.h>
//#include <zephyr/sys/util.h>
//#include <zephyr/debug/stack.h>
//#include <zephyr/syscall_handler.h>
#include <../lib/libc/minimal/include/limits.h>
//#include <sys/arch_interface.h>
//#include <arch/arc/syscall.h>
// #include <syscall_handler.h>
//CVE-2020-10024
// #include <drivers/gpio.h>
//#include <syscalls/kscan_config_mrsh.c>
// #include <drivers/kscan.h>
//#include "local_patches.c"
#include "patch_point.h"
#include "ebpf.h"
#include "ebpf_test.h"
#include "jit.c"
#include "ebpf_vm.c"
//** Blink Sample **//
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 1000
/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)
/*
* A build error on this line means your board is unsupported.
* See the sample documentation for information on how to fix this.
*/
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
//int fixed_patch_point_hanlder(void) {
// __asm volatile("PUSH {r0, r1, r2, r3, lr}");
// __asm volatile("MRS r0, CONTROL"); //Move the contents of Control register to a general-purpose registerk
// __asm volatile("TST r0, #2"); //"And" instruction with "r0" and "constant 2" but the result is discarded.
// __asm volatile("ITE EQ"); //Like Condition
// __asm volatile("MRSEQ r0, MSP"); //main stack pointer
// __asm volatile("MRSNE r0, PSP"); //process stack pointer
// __asm volatile("BL dispatch_fixed_patch_point");
// __asm volatile("POP {r0, r1, r2, r3, pc}");
//}
__NAKE int fixed_patch_point_hanlder(void) {
// __asm("nop");
__asm volatile("PUSH {r0, lr}");
__asm volatile("MRS r0, CONTROL");
__asm volatile("TST r0, #2");
__asm volatile("ITE EQ");
__asm volatile("MRSEQ r0, MSP");
__asm volatile("MRSNE r0, PSP");
// asm volatile("ADD r0, #8"); // set to origin sp (push {r0, lr})
__asm volatile("BL dispatch_fixed_patch_point");
__asm volatile("POP {r0, pc}");
// asm volatile("ADDS r7, #16");
// asm volatile("MOV sp, r7");
// asm volatile("pop {r7, pc}");
}
#ifndef FIXED_OP_PASS
#define FIXED_OP_PASS 0x00010000 // set a unusual value
#endif
static struct dummy_MQTT_buf_ctx {
unsigned char *cur;
unsigned char *end;
};
static void call_dummy_buggy_MQTT_function();
static int dummy_buggy_MQTT_packet_length_decode(struct dummy_MQTT_buf_ctx *buf, uint32_t *length);
//static uint64_t patch_cve_10062(uint32_t sp);
extern int dummy_buggy_MQTT_packet_length_decode();
//typedef struct stack_frame {
// uint32_t r0;
// uint32_t r1;
// uint32_t r2;
// uint32_t r3;
// uint32_t r12; // ip
// uint32_t lr;
// uint32_t pc; // return address
// uint32_t xpsr;
//} tack_frame;
//typedef struct fixed_stack_frame {
// uint32_t r0_1;
// uint32_t r1_1;
// uint32_t r2_1;
// uint32_t r3;
// uint32_t lr;
//} fixed_stack_frame;
typedef struct fixed_stack_frame {
uint32_t r0_1;
uint32_t lr; // patch index
// uint32_t r3;
// uint32_t r2;
// uint32_t r1;
// uint32_t r0;
// uint32_t fp;
// uint32_t return_address; // real lr
// uint32_t args4;
// uint32_t args5;
} fixed_stack_frame;
///For Patch Execution Delay
typedef struct stack_frame {
// I changed the first three variables from 32 to 8
uint32_t r0;
uint32_t r1;
uint32_t r2;
uint32_t r3;
uint32_t r12; // ip
uint32_t lr;
uint32_t pc; // return address
uint32_t xpsr;
} __attribute__((__packed__)) stack_frame;
//static void dump_stack(uint32_t sp) {
// printf("SP: 0x%08x\n", sp);
// for (int i = 0; i < 10; i++) {
// uint32_t v = *(uint32_t *) (sp - 8 + 4 * i);
// printf("[sp %d] 0x%08x\n", -8 + 4 * i, v);
// }
//}
//static void dump_vm_args(fixed_stack_frame *args) {
// printf("r0 = 0x%08x\n", args->r0_1);
//
// printf("r1 = 0x%08x\n", &(args->r1_1));
// uint32_t *buf = (uint32_t *)(args->r3);
// printf("length = %08x\n", *buf);
//
//}
void dispatch_fixed_patch_point(uint32_t sp) {
//uint32_t lr = *(uint32_t *) (sp + offsetof(fixed_stack_frame, lr)); // r0-r3,lr
//uint32_t addr = (lr & ~0x1) - 4; // bl function
//ebpf_patch *patch = get_fixed_patch_by_lr(addr);
// profile_end(EV0);
// profile_dump(EV0);
uint32_t lr = *(uint32_t *) (sp + 4);
uint32_t addr = (lr - 4) & (~0x3);
// profile_add_event("EV0");
// profile_start(EV0);
// auto_patch *patch = get_fixed_patch_by_lr(addr);//For evaluating "Patch Dispatching Delay", we need to put "profile" around this function. //We have to evaluate with different size of active patches
// uint32_t t = profile_end(EV0);
// profile_dump(EV0);
// printf("cycles: %u\n", t);
///auto_patch *patch = NULL;
fixed_stack_frame *args = (fixed_stack_frame *) sp;
// printf("try to get patch at: 0x%08x\n", addr);
uint32_t sptemp = (sp + sizeof(fixed_stack_frame));
// if (patch == NULL) {
// *(volatile uint32_t *) &(args->r0_1) = FIXED_OP_PASS;
// // printf("Do not find Patch here\n");
// return;
// }
//Since we store lr (main sp) we can also use spreal. spreal == sptemp both points to dummy stack
//uint32_t sptemp = (sp + sizeof(fixed_stack_frame)) + 4;//SP for dummy function
//uint32_t sptemp = (sp + sizeof(fixed_stack_frame));
//uint32_t spreal = *(uint32_t *) (sp + sizeof(fixed_stack_frame));//SP for dummy function (in other way)
//uint8_t shift = *(uint32_t *) (sptemp + 15);
//uint8_t bytes = *(uint32_t *) (sptemp + 14);
//int ERR = *(uint32_t *) (sptemp + 8);
//uint32_t *length = *(uint32_t *) (sptemp + 0);
//printf("address and lr and sp and sptemp and spreal is : 0x%08x , 0x%08x , 0x%08x , 0x%08x , 0x%08x \n", addr, lr , sp , sptemp, spreal);
//printf("address and lr and sp and sptemp is : 0x%08x , 0x%08x , 0x%08x , 0x%08x \n", addr, lr , sp , sptemp);
//printf("The value of shift %u and bytes %u and ERR %i and length 0x%08x \n", shift, bytes , ERR , *length);
uint64_t ret = 0;
///printf("fixed patch %s is here and its location is 0x%08x !\n", patch->desc->cve , patch->desc->fixed_id);
///profile_add_event("EV0");
///profile_start(EV0);
// ret = patch->desc->func(sptemp);//For evaluating patch code executin delay, we have to put "profile" around this, I think.
///profile_end(EV0);
///profile_dump(EV0);
//profile_add_event("EV1");
//profile_start(EV1);
//ret = patch_cve_10062(sptemp);
//profile_end(EV1);
//profile_dump(EV1);
uint32_t op = ret >> 32;
//printf("op code:0x%08x \n", op);
uint32_t ret_code = ret & 0x00000000ffffffff;
//printf("ret code:0x%08x \n", ret_code);
//********//
//FILTER_PASS 0
//FILTER_DROP 1
//FILTER_REDIRECT 2
if (op == FILTER_DROP) {
*(volatile uint32_t *) &(args->r0_1) = 0;
//printf("FILTER_DROP\n");
return;
} else if (op == FILTER_REDIRECT) {
*(volatile uint32_t *) (args->lr) = ret_code;//I doesnot work! What is this? How can we implement for redirect part?
//printf("FILTER_REDIRECT\n");
return;
} else { // FILTER_PASS
*(volatile uint32_t *) &(args->r0_1) = FIXED_OP_PASS;
// printf("FILTER_PASS\n");
return;
}
}
//inline uint64_t set_return(uint64_t op, uint64_t ret_code) {
//uint64_t set_return(uint64_t op, uint64_t ret_code) {
// return (op << 32) + ret_code;
//}
//CVE-2020-10062
//uint64_t patch_cve_10062(uint32_t sp){
// uint32_t op = FILTER_PASS;
// uint32_t ret_code = 0;
// uint8_t shift = *(uint32_t *) (sp + 15);
// uint8_t bytes = *(uint32_t *) (sp + 14);
// int ERR = *(uint32_t *) (sp + 8);
// uint32_t *length = *(uint32_t *) (sp + 0);
// struct dummy_MQTT_buf_ctx *buf = *(uint32_t *)(sp + 4);
//printf("The value of shift %u and bytes %u and ERR %i and length 0x%08x and buf->cur %u \n", shift, bytes , ERR , *length, *(buf->cur));
//printf("The value of shift %u and bytes %u and ERR %i and length 0x%08x \n", shift, bytes , ERR , *length);
// if(bytes >= 4){
// op = 1;
// ret_code = -22;
// }
// return set_return(op, ret_code);
//}
//CVE-2020-10062
static int dummy_buggy_MQTT_packet_length_decode(struct dummy_MQTT_buf_ctx *buf, uint32_t *length)
{
// PATCH_FUNCTION_ERR_CODE;
uint8_t shift = 0U;
uint8_t bytes = 0U;
int ERR = -1;
*length = 0U;
///profile_add_event("EV0");
///profile_start(EV0);
///profile_end(EV0);
///profile_dump(EV0);
do {
///profile_add_event("EV0");
///profile_start(EV0);
///PATCH_FUNCTION_ERR_CODE;
///profile_end(EV0);
///profile_dump(EV0);
if (bytes > 4) {
return ERR;
}
if (buf->cur >= buf->end) {
return ERR;
}
*length += ((int)*(buf->cur) & 0x7f) << shift;
shift += 7;
bytes++;
} while ((*(buf->cur++) & 0x80) != 0U);
PATCH_FUNCTION_ERR_CODE;
//printf("dummy MQTT packet length:0x%08x \n", *length);
return 0;
}
static void call_dummy_buggy_MQTT_function() {
uint8_t packet_buf[10];
for (int i=0; i<4; ++i) packet_buf[i] = 0xff;
packet_buf[4] = 0x7f;
for (int i=5; i<10; ++i) packet_buf[i] = 0;
struct dummy_MQTT_buf_ctx dbc;
dbc.cur = &packet_buf[0];
dbc.end = &packet_buf[9];
uint32_t pkt_length = 0;
profile_add_event("EV0");
profile_start(EV0);
int ret = dummy_buggy_MQTT_packet_length_decode(&dbc, &pkt_length);//Like RapidPatch, we can say our patch loading delay depends on input, we run the vulnerable function with different inputs and calculate the average delay.
profile_end(EV0);
profile_dump(EV0);
printf("Decoded MQTT packet length is %d\n", pkt_length);
//if (pkt_length != 0) {
// printf("is still vulnerable!\n\n");
//} else {
// printf("is fixed!\n");
//}
if (pkt_length == -1) {
printf("is still vulnerable!\n\n");
} else {
printf("is fixed!\n");
}
}
uint64_t set_return_main(uint64_t op, uint64_t ret_code) {
return (op << 32) + ret_code;
}
//CVE-2020-10063
static inline bool u16_add_overflow_patch(uint16_t a, uint16_t b, uint16_t *result){
uint16_t c = a + b;
///printf("c: %d and a:%d \n", c, a);
*result = c;///Fix it
return c < a; }
//one place
//Rapid Patch is so bad!
uint64_t patch_cve_10063(stack_frame *sp){
uint32_t op = FILTER_PASS;
uint32_t ret_code = 0;
uint32_t data = sp->r1;//beggining of address of testcase array
uint16_t *opt_delta = *(uint16_t *) (data + 0);
uint16_t delta = *(uint16_t *) (data + 1);
uint16_t *opt_len = *(uint16_t *) (data + 4);
uint16_t len = *(uint16_t *) (data + 5);
///printf("data: 0x%08x delta: %d and len:%d \n", data, delta, *opt_delta);
if (u16_add_overflow_patch(*opt_delta, delta, opt_delta) ||
u16_add_overflow_patch(*opt_len, len, opt_len)) {
///printf("Here!\n");
op = 1;
ret_code = -EINVAL;
}
///printf("Or Here!\n");
return set_return_main(op, ret_code);
}
//**********//
//CVE-2020-10024
uint64_t patch_cve_10024(stack_frame *sp){
uint32_t op = FILTER_PASS;
uint32_t ret_code = 0;
//printf("Hello, continue!\n");
K_APP_BMEM(part0) static volatile bool expect_fault = true;
K_APP_BMEM(part0) static volatile unsigned int expected_reason = K_ERR_KERNEL_OOPS;
if(arch_is_user_context()){
//printf("WOW... \n");
}else{
//printf("WOW2... \n");
}
//arch_syscall_invoke0(UINT_MAX);
//printf("Hello, END!\n");
return set_return_main(op, ret_code);
}
//CVE-2020-10028
uint64_t patch_cve_10028(stack_frame *sp){
uint32_t op = FILTER_PASS;
uint32_t ret_code = 0;
printf("Hello, Start1!\n");
//uint32_t *ptr_addr = (uint32_t *)(sp->r0);
//struct device *port = (struct device *) (*ptr_addr);
struct device *dev;
printf("Hello, Start!\n");
//Z_OOPS(Z_SYSCALL_DRIVER_KSCAN(dev, enable_callback));
printf("Hello, End!\n");
}
// void test_ebpf_c5() {
// // zephyr_cve_2020_10028
// stack_frame sf;
// uint64_t ret1 = 0;
// printf("Hello, Start0!\n");
// struct device *port;
// port->name = 'M';
// sf.r0 = &port;
// //profile_add_event("EV0");
// //profile_start(EV0);
// printf("Hello, Start2!\n");
// ret1 = patch_cve_10028(&sf);
// //printf("Hello, End!\n");
// //profile_end(EV0);
// //profile_dump(EV0);
// }
//CVE-2020-10021
#define BLOCK_SIZE 512
/*current CBW*/
static struct CBW cbw;
/*CSW which will be sent*/
static struct CSW csw;
/*addr where will be read or written data*/
static uint32_t addr;
/*length of a reading or writing*/
static uint32_t length;
/* CSW Status */
enum Status {
CSW_PASSED,
CSW_FAILED,
CSW_ERROR,
};
#define MSD_OUT_EP_IDX 0
#define MSD_IN_EP_IDX 1
static void mass_storage_bulk_out(uint8_t ep,
enum usb_dc_ep_cb_status_code ep_status);
static void mass_storage_bulk_in(uint8_t ep,
enum usb_dc_ep_cb_status_code ep_status);
#define MASS_STORAGE_IN_EP_ADDR 0x82
#define MASS_STORAGE_OUT_EP_ADDR 0x01
/* Describe EndPoints configuration */
static struct usb_ep_cfg_data mass_ep_data[] = {
{
.ep_cb = mass_storage_bulk_out,
.ep_addr = MASS_STORAGE_OUT_EP_ADDR
},
{
.ep_cb = mass_storage_bulk_in,
.ep_addr = MASS_STORAGE_IN_EP_ADDR
}
};
static uint32_t memory_size = 233;
static bool dummy_CVE_2020_10021_infoTransfer(void)
{
uint32_t n;
PATCH_FUNCTION_ERR_CODE;
/* Logical Block Address of First Block */
n = (cbw.CB[2] << 24) | (cbw.CB[3] << 16) | (cbw.CB[4] << 8) |
(cbw.CB[5] << 0);
printf("LBA (block) : 0x%x \n", n);
printf("cbw.CB[2] and block-size is : %u and %u \n", cbw.CB[2], BLOCK_SIZE);
// This is patch
if ((n * BLOCK_SIZE) >= memory_size) {
printf("LBA out of range \n");
csw.Status = CSW_FAILED;
//sendCSW();
return false;
}
addr = n * BLOCK_SIZE;
/* Number of Blocks to transfer */
switch (cbw.CB[0]) {
case READ10:
case WRITE10:
case VERIFY10:
n = (cbw.CB[7] << 8) | (cbw.CB[8] << 0);
break;
case READ12:
case WRITE12:
n = (cbw.CB[6] << 24) | (cbw.CB[7] << 16) |
(cbw.CB[8] << 8) | (cbw.CB[9] << 0);
break;
}
///PATCH_FUNCTION_ERR_CODE;
//printf("Size (block) : 0x%x \n", n);
length = n * BLOCK_SIZE;
if (!cbw.DataLength) { /* host requests no data*/
printf("Zero length in CBW \n");
csw.Status = CSW_FAILED;
//sendCSW();
return false;
}
if (cbw.DataLength != length) {
if ((cbw.Flags & 0x80) != 0U) {
printf("Stall IN endpoint \n");
//usb_ep_set_stall(mass_ep_data[MSD_IN_EP_IDX].ep_addr);
} else {
printf("Stall OUT endpoint \n");
//usb_ep_set_stall(mass_ep_data[MSD_OUT_EP_IDX].ep_addr);
}
csw.Status = CSW_FAILED;
//sendCSW();
return false;
}
///PATCH_FUNCTION_ERR_CODE;
return true;
}
void call_dummy_CVE_2020_10021_infotransfer(){
// setup test arguments 8002df8 - 8002dec
int bl_addr = (uint32_t) dummy_CVE_2020_10021_infoTransfer + 11;
printf("addr ground-truth bug:0x%08x test:0x%08x \n", bl_addr, call_dummy_CVE_2020_10021_infotransfer);
// Prepare invocation context for the buggy function
// Zephyr_cve_2020_10021
cbw.CB[0] = READ10;
// cbw.CB[2] = 1;
///profile_start(0);
bool ret = dummy_CVE_2020_10021_infoTransfer();
///profile_end(0);
///profile_dump(0);
printf("The return code of the buggy function is %d\n", ret);
}
void blinksample(void)
{
profile_add_event("EV0");
profile_start(EV0);
int ret;
int i =0;
// PATCH_FUNCTION_ERR_CODE;
if (!gpio_is_ready_dt(&led)) {
return;
}
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
// PATCH_FUNCTION_ERR_CODE;
if (ret < 0) {
return;
}
while (i < 10) {
// PATCH_FUNCTION_ERR_CODE;
ret = gpio_pin_toggle_dt(&led);
// PATCH_FUNCTION_ERR_CODE;
if (ret < 0) {
return;
}
k_msleep(SLEEP_TIME_MS);
// PATCH_FUNCTION_ERR_CODE;
i++;
}
// PATCH_FUNCTION_ERR_CODE;
profile_end(EV0);
profile_dump(EV0);
}
void printstr (){
profile_add_event("EV0");
profile_start(EV0);
// printf("Hello world! \n");
// printf("Hello World, I am AutoPatch!\n");
PATCH_FUNCTION_ERR_CODE;
profile_end(EV0);
profile_dump(EV0);
}
///////////////////**********************///////////////////////
// Simple hash function for hexadecimal addresses
size_t hash_function(uint64_t address) {
// Extract the lower 32 bits of the address
uint32_t lower_bits = (uint32_t)(address & 0xFFFFFFFF);
// XOR the lower and upper 32 bits of the address
uint32_t upper_bits = (uint32_t)((address >> 32) ^ lower_bits);
// Use the XOR of the lower and upper bits as the hash value
size_t hash = (size_t)(lower_bits ^ upper_bits);
// Modulo the hash value by the size of the hash array (64)
return hash % 64;
}
// Define the function type for the patch functions
typedef uint64_t (*patch_func_t)(struct stack_frame *);
// Define the struct for hashmap entry
typedef struct {
uint32_t address; // The address key
patch_func_t patch_func; // The patch function value
struct hashmap_entry_t* next; // Pointer to the next entry in the bucket's linked list
} hashmap_entry_t;
// Define the hashmap type
typedef struct {
size_t num_buckets; // Number of buckets
hashmap_entry_t** buckets; // Array of pointers to entries
} hashmap_t;
typedef struct {
uint32_t address; // The address key
patch_func_t patch_func; // The patch function value
} hashmap_new;
// Binary search to find a patch function based on address
patch_func_t find_patch_function(hashmap_new *patches, int num_patches, uint32_t address) {
int left = 0;
int right = num_patches - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (patches[mid].address == address) {
return patches[mid].patch_func;
} else if (patches[mid].address < address) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return NULL; // Patch function not found
}
// Function to insert a new element into the structure
void insert_patch_function(hashmap_new *patches, int *num_patches, uint32_t address, patch_func_t patch_func) {
// Find the index where the new element should be inserted using binary search
int index = *num_patches;
for (int i = 0; i < *num_patches; i++) {
if (patches[i].address > address) {
index = i;
break;
}
}
// Shift elements to the right to make space for the new element
for (int i = *num_patches; i > index; i--) {
patches[i] = patches[i - 1];
}
// Insert the new element
patches[index].address = address;
patches[index].patch_func = patch_func;
(*num_patches)++; // Increment the number of patches
printf("Inserting is successful %d \n" , *num_patches);
}
// Declare the external patch functions
// extern uint64_t filter_CVE_10021(struct stack_frame *frame);
extern uint64_t filter(struct stack_frame *frame);
// ... (declare more external patch functions here)
// Initialize the hashmap with buckets
hashmap_t* hashmap_init(size_t num_buckets) {
hashmap_t* map = (hashmap_t*)malloc(sizeof(hashmap_t));
map->num_buckets = num_buckets;
map->buckets = (hashmap_entry_t**)calloc(num_buckets, sizeof(hashmap_entry_t*));
return map;
}
// Insert an entry into the hashmap
void hashmap_insert(hashmap_t* map, uint32_t address, patch_func_t patch_func) {
size_t hash = hash_function(address); // Compute hash value
size_t bucket_idx = hash % map->num_buckets; // Compute bucket index
hashmap_entry_t* new_entry = (hashmap_entry_t*)malloc(sizeof(hashmap_entry_t)); // Allocate memory for new entry
new_entry->address = address;
new_entry->patch_func = patch_func;
new_entry->next = map->buckets[bucket_idx]; // Insert at the beginning of the bucket's linked list
map->buckets[bucket_idx] = new_entry;
}
// Search for a patch function based on an address
//patch_func_t search_patch_function(uint64_t address, hashmap_t* map) {
patch_func_t search_patch_function(size_t hash, uint32_t address, hashmap_t* map) {
// size_t hash = hash_function(address); // Compute hash value
size_t bucket_idx = hash % map->num_buckets; // Compute bucket index
// Search for the patch function in the bucket's linked list
hashmap_entry_t* entry = map->buckets[bucket_idx];
while (entry != NULL) {
if (entry->address == address) {
return entry->patch_func; // Found the patch function, return it
}
entry = entry->next; // Move to the next entry in the bucket
}
return NULL; // Patch function not found in the hashmap
}
// Overhead for Dispatcher
struct patch_list {
arraymap *patches;
};
void patch_num_eva(struct patch_list *plist, int n, int times) {
if (plist->patches == NULL) {
plist->patches = arraymap_new(72);
}
for (int i = 0; i < n; i++) {
arraymap_set(plist->patches, i, 1);
}
dwt_init();
int start = get_cur_tick();
for (int t = 0; t < times; t++) {
for (int i = 0; i < n; i++) {
int val = arraymap_get(plist->patches, i);
(void) val;
}
}
int cycles = get_cur_tick() - start;
int microseconds_per_iteration = (int)(cycles2us(cycles / times / n) * 100);
printf("patch_numbers: %d time: %d.%02d cycles: %d\n", n, microseconds_per_iteration / 100, cycles);
}
void test_patch_dispatcher(){
// setup patch list
printf("**Evaluating Hotpatch Dispatching Overhead** \n");
profile_add_event("EV0");
profile_start(EV0);
profile_end(EV0);
profile_dump(EV0);
struct patch_list plist;
plist.patches = NULL;
int TI = 100;
for (int i = 1; i < 65; i += 4) {
patch_num_eva(&plist, i, TI);
}
patch_num_eva(&plist, 64, TI);
arraymap_destroy(plist.patches);
}
//******** END of Dispatcher Overhead ********//
// extern uint64_t filter(stack_frame *frame);
// extern uint64_t info(stack_frame *frame);
extern uint64_t filter_10021(stack_frame *frame);
extern uint64_t filter_10063(stack_frame *frame);
extern uint64_t filter_10062_1(stack_frame *frame);
extern uint64_t filter_10062_2(stack_frame *frame);
extern uint64_t filter_10024(stack_frame *frame);
extern uint64_t filter_10028(stack_frame *frame);
extern uint64_t filter_16524(stack_frame *frame);
extern uint64_t filter_16603(stack_frame *frame);
extern uint64_t filter_17443(stack_frame *frame);
extern uint64_t filter_17445(stack_frame *frame);
extern uint64_t filter_10023(stack_frame *frame);
extern uint64_t filter_16522(stack_frame *frame);
extern uint64_t filter_16526(stack_frame *frame);
extern uint64_t filter_16525(stack_frame *frame);
extern uint64_t filter_16599(stack_frame *frame);
extern uint64_t filter_16523(stack_frame *frame);
extern uint64_t filter_16602(stack_frame *frame);
extern uint64_t filter_16600(stack_frame *frame);
extern uint64_t filter_16602(stack_frame *frame);
extern uint64_t filter_16527(stack_frame *frame);
extern uint64_t filter_16598(stack_frame *frame);
extern uint64_t filter_14199(stack_frame *frame);
extern uint64_t filter_10019(stack_frame *frame);
void main(){
printf("Hello, Welcome to AutoPatch Artifact! This is the config of board: %s\n", CONFIG_BOARD);
// For Patch Dispatching Delay
// test_patch_dispatcher();
// Board CPU Frequency Information
// uint32_t cpu_freq = sys_clock_hw_cycles_per_sec();
// printf("CPU Frequency: %u Hz\n", cpu_freq);
//For Patch Exectution Time:
// test_c1(); //CVE-2020-10063
test_c2(); //CVE-2020-10021
// test_c3(); //CVE_2020_10024
// test_c4(); //CVE_2020_10028
// test_c5(); //CVE-2020-10062
// test_c6(); //CVE-2018-16524
// test_c7(); //CVE-2018-16528
// test_c8(); //CVE-2018-16603
// test_c9(); //CVE-2018-2784
// test_c10(); //CVE-2020-17443
// test_c11(); //CVE-2020-17445
// test_c12(); //CVE-2020-10023
// NEW CVES:
// test_c13(); //CVE-2018-16522
// test_c14(); //CVE-2018-16526
// test_c15(); //CVE-2018-16525
// test_c16(); //CVE-2018-16599
// test_c17(); //CVE-2018-16523
// test_c18(); //CVE-2018-16602
// test_c19(); //CVE-2018-16600
// test_c20(); //CVE-2018-16527
// test_c21(); //CVE-2018-16598
// test_c22(); //CVE-2017_14199
// test_c23(); //CVE-2020-10019
// test_c24(); //CVE-2020-10060
// test_c25(); //CVE-2020-10061
// test_c26(); //CVE-2020-10066
// test_c27(); //CVE-2020-10067
// test_c28(); //CVE-2020-10069
// test_c29(); //CVE-2020-10070
// test_c30(); //CVE-2020-10071
// test_c31(); //CVE-2020-13602
// test_c32(); //CVE-2021-3336
// test_c33(); //CVE-2020-24338
// test_c34(); //CVE-2020-17442
// test_c35(); //CVE-2020-13600
// test_c36(); //CVE-2019_16748
////*** End of execution time evaluation ***////
// test_ebpf_c1();
// test_ebpf_c2();
// test_ebpf_c3();
// test_ebpf_c4();
// test_ebpf_c5();
// test_ebpf_c6();
// test_ebpf_c7();
// test_ebpf_c8();
// test_ebpf_c9();
// test_ebpf_c10();
// test_ebpf_c11();
// test_ebpf_c12();
// printf("Hello, Begin!\n");
// profile_add_event("EV0");
// profile_start(EV0);
// fixed_patch_point_hanlder();
// profile_end(EV0);
// profile_dump(EV0);
// printf("Hello, Begin!\n");
// profile_add_event("EV0");
// profile_start(EV1);
// stack_frame st_frame;
// st_frame.r0 = 0;
// // uint64_t t = filter(&st_frame);
// // uint64_t result = filter_2020_10021(&st_frame);
// uint64_t result = filter_10063(&st_frame);
// profile_end(EV1);
// profile_dump(EV1);
// Evaluating "printf":
// profile_add_event("EV0");
// profile_start(EV0);
// printf("Hello World, I am AutoPatch!\n");
// profile_end(EV0);
// profile_dump(EV0);
// int num_patches = get_num_patches();
// printf("Number of patches stored before: %d\n", num_patches);
// load_local_fixed_patch(0);
// call_dummy_buggy_MQTT_function();
///load_local_fixed_patch(1);
// call_dummy_CVE_2020_10021_infotransfer();
////////////////////////*********************************************/////////////////////////////////////
// hashmap_new patches[25];
// int num_patches = 0;
// // Insert patch function 1
// patches[num_patches].address = 0x00001921;
// patches[num_patches].patch_func = filter; // Set the patch function pointer accordingly
// num_patches++;
// insert_patch_function(patches, &num_patches, 0x00001922, filter);
// insert_patch_function(patches, &num_patches, 0x00001923, filter);
// insert_patch_function(patches, &num_patches, 0x00001924, filter);
// insert_patch_function(patches, &num_patches, 0x00001925, filter);
// insert_patch_function(patches, &num_patches, 0x00001926, filter);
// insert_patch_function(patches, &num_patches, 0x00001927, filter);
// insert_patch_function(PATCH_FUNCTION_ERR_CODEpatches, &num_patches, 0x00001928, filter);
// insert_patch_function(patches, &num_patches, 0x00001929, filter);
// insert_patch_function(patches, &num_patches, 0x00001930, filter);
// insert_patch_function(patches, &num_patches, 0x00001931, filter);
// insert_patch_function(patches, &num_patches, 0x00001932, filter);
// insert_patch_function(patches, &num_patches, 0x00001933, filter);
// insert_patch_function(patches, &num_patches, 0x00001934, filter);
// insert_patch_function(patches, &num_patches, 0x00001935, filter);
// insert_patch_function(patches, &num_patches, 0x00001936, filter);
// insert_patch_function(patches, &num_patches, 0x00001937, filter);
// insert_patch_function(patches, &num_patches, 0x00001938, filter);