-
Notifications
You must be signed in to change notification settings - Fork 46
/
light_box.c
4936 lines (4228 loc) · 144 KB
/
light_box.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
/*
* Shadow-Box
* ------------
* Lightweight Hypervisor-Based Kernel Protector
*
* Copyright (C) 2017 Seunghun Han
* at National Security Research Institute of South Korea
*/
/*
* This software has GPL v2 license. See the GPL_LICENSE file.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/syscalls.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/smp.h>
#include <linux/cpumask.h>
#include <linux/getcpu.h>
#include <linux/mm.h>
#include <asm/io.h>
#include <asm/desc.h>
#include <linux/kallsyms.h>
#include <asm/reboot.h>
#include <linux/reboot.h>
#include <asm/cacheflush.h>
#include <linux/hardirq.h>
#include <asm/processor.h>
#include <asm/pgtable_64.h>
#include <asm/hw_breakpoint.h>
#include <asm/debugreg.h>
#include <net/sock.h>
#include <net/tcp.h>
#include <linux/utsname.h>
#include <linux/mmzone.h>
#include <linux/jiffies.h>
#include <linux/tboot.h>
#include <linux/log2.h>
#include <linux/version.h>
#include <linux/kfifo.h>
#include <asm/uaccess.h>
#include <linux/suspend.h>
#include "asm.h"
#include "shadow_box.h"
#include "shadow_watcher.h"
#include "mmu.h"
#include "iommu.h"
#include "asm.h"
#include "workaround.h"
#include "symbol.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
#include <linux/vmalloc.h>
#endif
/*
* Variables.
*/
/* Variables for supporting multi-core environment. */
struct task_struct *g_vm_start_thread_id[MAX_PROCESSOR_COUNT]= {NULL, };
int g_thread_result = 0;
struct task_struct *g_vm_shutdown_thread_id[MAX_PROCESSOR_COUNT]= {NULL, };
struct desc_ptr g_gdtr_array[MAX_PROCESSOR_COUNT];
void* g_vmx_on_vmcs_log_addr[MAX_PROCESSOR_COUNT] = {NULL, };
void* g_guest_vmcs_log_addr[MAX_PROCESSOR_COUNT] = {NULL, };
void* g_vm_exit_stack_addr[MAX_PROCESSOR_COUNT] = {NULL, };
void* g_io_bitmap_addrA[MAX_PROCESSOR_COUNT] = {NULL, };
void* g_io_bitmap_addrB[MAX_PROCESSOR_COUNT] = {NULL, };
void* g_msr_bitmap_addr[MAX_PROCESSOR_COUNT] = {NULL, };
int g_trap_count[MAX_PROCESSOR_COUNT] = {0, };
void* g_virt_apic_page_addr[MAX_PROCESSOR_COUNT] = {NULL, };
int g_vmx_root_mode[MAX_PROCESSOR_COUNT] = {0, };
u64 g_stack_size = MAX_STACK_SIZE;
u64 g_vm_host_phy_pml4 = 0;
u64 g_vm_init_phy_pml4 = 0;
struct module* g_shadow_box_module = NULL;
static int g_support_smx = 0;
static int g_support_xsave = 0;
atomic_t g_need_init_in_secure = {1};
volatile int g_allow_shadow_box_hide = 0;
volatile u64 g_init_in_secure_jiffies = 0;
atomic_t g_thread_run_flags;
atomic_t g_thread_entry_count;
atomic_t g_sync_flags;
atomic_t g_complete_flags;
atomic_t g_framework_init_start_flags;
atomic_t g_enter_flags;
atomic_t g_enter_count;
atomic_t g_first;
atomic_t g_framework_init_flags;
atomic_t g_iommu_complete_flags;
atomic_t g_mutex_lock_flags;
u64 g_vm_pri_proc_based_ctrl_default = 0;
static spinlock_t g_mem_pool_lock;
/* Variables for checking dynamic kernel objects. */
struct list_head* g_modules_ptr = NULL;
struct file* g_root_file_ptr = NULL;
struct file* g_proc_file_ptr = NULL;
struct file* g_tcp_file_ptr = NULL;
struct file* g_tcp6_file_ptr = NULL;
struct file* g_udp_file_ptr = NULL;
struct file* g_udp6_file_ptr = NULL;
struct socket* g_tcp_sock = NULL;
struct socket* g_udp_sock = NULL;
rwlock_t* g_tasklist_lock;
/* Variables for operating. */
u64 g_max_ram_size = 0;
struct sb_memory_pool_struct g_memory_pool = {0, };
int g_ro_array_count = 0;
struct ro_addr_struct g_ro_array[MAX_RO_ARRAY_COUNT];
struct sb_workaround g_workaround = {{0, }, {0, }};
struct sb_share_context* g_share_context = NULL;
atomic_t g_is_shutdown_trigger_set = {0, };
volatile u64 g_shutdown_jiffies = 0;
volatile u64 g_dump_jiffies = 0;
u64 g_dump_count[MAX_VM_EXIT_DUMP_COUNT] = {0, };
/*
* Static functions.
*/
static int sb_start(int reinitialize);
static int sb_get_kernel_version_index(void);
static int sb_is_kaslr_working(void);
static int sb_relocate_symbol(void);
static void sb_alloc_vmcs_memory(void);
static void sb_setup_workaround(void);
static int sb_setup_memory_pool(void);
void * sb_get_memory(void);
static int sb_is_workaround_addr(u64 addr);
static int sb_check_gdtr(int cpu_id);
static int sb_init_vmx(int cpu_id);
static void sb_vm_set_msr_write_bitmap(struct sb_vm_control_register*
sb_vm_control_register, u64 msr_number);
static void sb_setup_vm_host_register(struct sb_vm_host_register* pstVMHost);
static void sb_setup_vm_guest_register(struct sb_vm_guest_register* pstVMGuest,
const struct sb_vm_host_register* pstVMHost);
static void sb_setup_vm_control_register(struct sb_vm_control_register*
pstVMControl, int iCPUID);
static void sb_setup_vmcs(const struct sb_vm_host_register* pstVMHost, const
struct sb_vm_guest_register* pstVMGuest, const struct sb_vm_control_register*
pstVMControl);
static void sb_dump_vm_host_register(struct sb_vm_host_register* pstVMHost);
static void sb_dump_vm_guest_register(struct sb_vm_guest_register* pstVMHost);
static void sb_dump_vm_control_register(struct sb_vm_control_register*
pstVMControl);
static u64 sb_get_desc_base(u64 qwOffset);
static u64 sb_get_desc_access(u64 qwOffset);
static void sb_remove_int_exception_from_vm(int vector);
static void sb_print_vm_result(const char* pcData, int iResult);
static void sb_disable_and_change_machine_check_timer(int reinitialize);
static int sb_vm_thread(void* pvArgument);
static void sb_dup_page_table_for_host(int reinitialize);
static void sb_protect_kernel_ro_area(void);
static void sb_protect_module_list_ro_area(int reinitialize);
static void sb_protect_vmcs(void);
static void sb_protect_gdt(int cpu_id);
static void sb_protect_shadow_box_module(int protect_mode);
static void sb_advance_vm_guest_rip(void);
static u64 sb_calc_vm_pre_timer_value(void);
static unsigned long sb_encode_dr7(int dr_num, unsigned int len, unsigned int type);
static void sb_print_shadow_box_logo(void);
#if SHADOWBOX_USE_SHUTDOWN
static int sb_vm_thread_shutdown(void* argument);
static void sb_shutdown_vm_this_core(int cpu_id, struct sb_vm_exit_guest_register*
guest_context);
static void sb_fill_context_from_vm_guest(struct sb_vm_exit_guest_register*
guest_context, struct sb_vm_full_context* full_context);
static void sb_restore_context_from_vm_guest(int cpu_id, struct sb_vm_full_context*
full_context, u64 guest_rsp);
#endif /* SHADOWBOX_USE_SHUTDOWN */
static void sb_lock_range(u64 start_addr, u64 end_addr, int alloc_type);
static void sb_get_function_pointers(void);
static int sb_is_system_shutdowning(void);
static void sb_disable_desc_monitor(void);
static void sb_trigger_shutdown_timer(void);
static int sb_is_shutdown_timer_expired(void);
static void sb_sync_page_table_flag(struct sb_pagetable* vm, struct sb_pagetable*
init, int index, u64 addr);
static void sb_set_reg_value_from_index(struct sb_vm_exit_guest_register*
guest_context, int index, u64 reg_value);
static u64 sb_get_reg_value_from_index(struct sb_vm_exit_guest_register*
guest_context, int index);
/* Functions for vm exit. */
static void sb_vm_exit_callback_int(int cpu_id, unsigned long dr6, struct
sb_vm_exit_guest_register* guest_context);
static void sb_vm_exit_callback_init_signal(int cpu_id);
static void sb_vm_exit_callback_start_up_signal(int cpu_id);
static void sb_vm_exit_callback_access_cr(int cpu_id, struct
sb_vm_exit_guest_register* guest_context, u64 exit_reason, u64 exit_qual);
static void sb_vm_exit_callback_vmcall(int cpu_id, struct sb_vm_exit_guest_register*
guest_context);
static void sb_vm_exit_callback_wrmsr(int cpu_id);
static void sb_vm_exit_callback_gdtr_idtr(int cpu_id, struct
sb_vm_exit_guest_register* guest_context);
static void sb_vm_exit_callback_ldtr_tr(int cpu_id, struct sb_vm_exit_guest_register*
guest_context);
static void sb_vm_exit_callback_ept_violation(int cpu_id, struct
sb_vm_exit_guest_register* guest_context, u64 exit_reason, u64 exit_qual,
u64 guest_linear, u64 guest_physical);
static void sb_vm_exit_callback_cpuid(struct sb_vm_exit_guest_register*
guest_context);
static void sb_vm_exit_callback_invd(void);
static void sb_vm_exit_callback_pre_timer_expired(int cpu_id);
#if SHADOWBOX_USE_SHUTDOWN
static int sb_system_reboot_notify(struct notifier_block *nb, unsigned long code,
void *unused);
static struct notifier_block* g_vm_reboot_nb_ptr = NULL;
static struct notifier_block g_vm_reboot_nb = {
.notifier_call = sb_system_reboot_notify,
};
#endif /* SHADOWBOX_USE_SHUTDOWN */
#if SHADOWBOX_USE_SLEEP
static int sb_system_sleep_notify(struct notifier_block* nb, unsigned long val, void* unused);
static struct notifier_block* g_sb_sleep_nb_ptr = NULL;
#endif /* SHADOWBOX_USE_SLEEP */
/*
* Start function of Shadow-box module
*/
static int __init shadow_box_init(void)
{
int cpu_count;
int cpu_id;
struct new_utsname* name;
u32 eax, ebx, ecx, edx;
u64 msr;
sb_print_shadow_box_logo();
/* Check kernel version and kernel ASLR. */
if (sb_get_kernel_version_index() == -1)
{
name = utsname();
sb_printf(LOG_LEVEL_ERROR, LOG_INFO "Kernel version is not supported, [%s]",
name->version);
sb_error_log(ERROR_KERNEL_VERSION_MISMATCH);
return -1;
}
/* Check kASLR. */
if (sb_is_kaslr_working() == 1)
{
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO "Kernel ASLR is enabled\n");
sb_relocate_symbol();
}
/* Check VMX support. */
cpuid_count(1, 0, &eax, &ebx, &ecx, &edx);
sb_printf(LOG_LEVEL_DETAIL, LOG_INFO "Initialize VMX\n");
sb_printf(LOG_LEVEL_DETAIL, LOG_INFO " [*] Check virtualization, %08X, "
"%08X, %08X, %08X\n", eax, ebx, ecx, edx);
if (ecx & CPUID_1_ECX_VMX)
{
sb_printf(LOG_LEVEL_DETAIL, LOG_INFO " [*] VMX support\n");
}
else
{
sb_printf(LOG_LEVEL_ERROR, LOG_ERROR " [*] VMX not support\n");
sb_error_log(ERROR_HW_NOT_SUPPORT);
return -1;
}
if (ecx & CPUID_1_ECX_SMX)
{
sb_printf(LOG_LEVEL_DETAIL, LOG_INFO " [*] SMX support\n");
g_support_smx = 1;
}
else
{
sb_printf(LOG_LEVEL_DETAIL, LOG_ERROR " [*] SMX not support\n");
}
/* Check BIOS-locked feature. */
msr = sb_rdmsr(MSR_IA32_FEATURE_CONTROL);
if (msr & MSR_IA32_FEATURE_CONTROL_BIT_CONTROL_LOCKED)
{
if (!(msr & MSR_IA32_FEATURE_CONTROL_BIT_VMXON_ENABLED_OUTPUTSIDE_SMX))
{
sb_printf(LOG_LEVEL_ERROR, LOG_ERROR " [*] VMX is disabled by BIOS\n");
sb_error_log(ERROR_HW_NOT_SUPPORT);
return -1;
}
#if SHADOWBOX_USE_TBOOT
if ((tboot_enabled()) && !(msr & MSR_IA32_FEATURE_CONTROL_BIT_VMXON_ENABLED_INSIDE_SMX))
{
sb_printf(LOG_LEVEL_ERROR, LOG_ERROR " [*] VMX inside SMX is disabled by BIOS\n");
sb_error_log(ERROR_HW_NOT_SUPPORT);
return -1;
}
#endif
}
/* Check XSAVES, XRSTORS support. */
cpuid_count(0x0D, 1, &eax, &ebx, &ecx, &edx);
if (eax & CPUID_D_EAX_XSAVES)
{
sb_printf(LOG_LEVEL_DETAIL, LOG_INFO " [*] XSAVES/XRSTORES support\n");
g_support_xsave = 1;
}
else
{
sb_printf(LOG_LEVEL_DETAIL, LOG_INFO " [*] XSAVES/XRSTORES not support\n");
}
#if SHADOWBOX_USE_SHUTDOWN
/* Add callback function for checking system shutdown. */
g_vm_reboot_nb_ptr = kmalloc(sizeof(struct notifier_block), GFP_KERNEL);
memcpy(g_vm_reboot_nb_ptr, &g_vm_reboot_nb, sizeof(struct notifier_block));
register_reboot_notifier(g_vm_reboot_nb_ptr);
/* Create shared context for system shutdown. */
g_share_context = (struct sb_share_context*) kmalloc(sizeof(struct
sb_share_context), GFP_KERNEL);
atomic_set(&(g_share_context->shutdown_complete_count), 0);
atomic_set(&(g_share_context->shutdown_flag), 0);
#endif /* SHADOWBOX_USE_SHUTDOWN */
#if SHADOWBOX_USE_SLEEP
/* Add callback function for checking system sleep. */
g_sb_sleep_nb_ptr = kmalloc(sizeof(struct notifier_block), GFP_KERNEL);
g_sb_sleep_nb_ptr->notifier_call = sb_system_sleep_notify;
g_sb_sleep_nb_ptr->priority = 0;
register_pm_notifier(g_sb_sleep_nb_ptr);
#endif /* SHADOWBOX_USE_SLEEP */
/*
* Check total RAM size.
* To cover system reserved area (3GB ~ 4GB), if system has under 4GB RAM,
* Shadow-box sets 4GB RAM to the variable. If system RAM has upper 4GB RAM,
* Shadow-box sets 1GB more than original size to the variable.
*/
g_max_ram_size = sb_get_max_ram_size();
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO "totalram_pages %ld, size %ld, "
"g_max_ram_size %ld\n", totalram_pages, totalram_pages * VAL_4KB,
g_max_ram_size);
if (g_max_ram_size < VAL_4GB)
{
g_max_ram_size = VAL_4GB;
}
else
{
g_max_ram_size = g_max_ram_size + VAL_1GB;
}
cpu_id = smp_processor_id();
cpu_count = num_online_cpus();
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO "CPU Count %d\n", cpu_count);
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO "Booting CPU ID %d\n", cpu_id);
#if SHADOWBOX_USE_TBOOT
if (tboot != NULL)
{
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO "Checking tboot\n", tboot->log_addr);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] log_addr = %016lX\n",
tboot->log_addr);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] shutdown_entry = %016lX\n",
tboot->shutdown_entry);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] shutdown_type = %016lX\n",
tboot->shutdown_type);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] num_in_wfs = %d\n",
tboot->num_in_wfs);
}
#endif /* SHADOWBOX_USE_TBOOT */
sb_get_function_pointers();
sb_alloc_vmcs_memory();
#if SHADOWBOX_USE_EPT
if (sb_alloc_ept_pages() != 0)
{
sb_error_log(ERROR_MEMORY_ALLOC_FAIL);
goto ERROR_HANDLE;
}
sb_setup_ept_pagetable_4KB();
#endif /* SHADOWBOX_USE_EPT */
#if SHADOWBOX_USE_IOMMU
if (sb_alloc_iommu_pages() != 0)
{
sb_error_log(ERROR_MEMORY_ALLOC_FAIL);
goto ERROR_HANDLE;
}
sb_setup_iommu_pagetable_4KB();
#endif /* SHADOWBOX_USE_IOMMU */
sb_protect_kernel_ro_area();
#if SHADOWBOX_USE_EPT
sb_protect_ept_pages();
sb_protect_vmcs();
#endif /* SHADOWBOX_USE_EPT */
#if SHADOWBOX_USE_IOMMU
sb_protect_iommu_pages();
#endif /* SHADOWBOX_USE_IOMMU */
/* Prepare Shadow-watcher */
if (sb_prepare_shadow_watcher() != 0)
{
sb_error_log(ERROR_MEMORY_ALLOC_FAIL);
return -1;
}
sb_setup_workaround();
if (sb_setup_memory_pool() != 0)
{
sb_error_log(ERROR_MEMORY_ALLOC_FAIL);
return -1;
}
g_tasklist_lock = (rwlock_t*) sb_get_symbol_address("tasklist_lock");
/* Start Shadow-box. */
if (sb_start(START_MODE_INITIALIZE) == 0)
{
return 0;
}
ERROR_HANDLE:
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO "Execution Fail\n");
sb_free_ept_pages();
sb_free_iommu_pages();
return -1;
}
/*
* Start Shadow-Box.
*/
static int sb_start(int reinitialize)
{
int cpu_count;
int cpu_id;
int i;
cpu_count = num_online_cpus();
cpu_id = smp_processor_id();
g_thread_result = 0;
g_need_init_in_secure.counter = 1;
g_allow_shadow_box_hide = 0;
atomic_set(&g_thread_run_flags, cpu_count);
atomic_set(&g_thread_entry_count, cpu_count);
atomic_set(&g_sync_flags, cpu_count);
atomic_set(&g_complete_flags, cpu_count);
atomic_set(&g_framework_init_start_flags, cpu_count);
atomic_set(&g_first, 1);
atomic_set(&g_enter_flags, 0);
atomic_set(&g_enter_count, 0);
atomic_set(&g_framework_init_flags, cpu_count);
atomic_set(&g_iommu_complete_flags, 0);
atomic_set(&(g_mutex_lock_flags), 0);
/* Create thread for each core. */
for (i = 0 ; i < cpu_count ; i++)
{
g_vm_start_thread_id[i] = (struct task_struct *)kthread_create_on_node(
sb_vm_thread, (void*)(u64)reinitialize, cpu_to_node(i), "vm_thread");
if (g_vm_start_thread_id[i] != NULL)
{
kthread_bind(g_vm_start_thread_id[i], i);
}
else
{
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO "VM [%d] Thread run fail\n", i);
}
#if SHADOWBOX_USE_SHUTDOWN
g_vm_shutdown_thread_id[i] = (struct task_struct *)kthread_create_on_node(
sb_vm_thread_shutdown, NULL, cpu_to_node(i), "vm_shutdown_thread");
if (g_vm_shutdown_thread_id[i] != NULL)
{
kthread_bind(g_vm_shutdown_thread_id[i], i);
}
else
{
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO "VM [%d] Thread run fail\n", i);
}
#endif /* SHADOWBOX_USE_SHUTDOWN */
}
/*
* Execute thread for each core except this core.
* If sb_vm_thread() is executed, task scheduling is prohibited. So,
* If task switching is occured when this core run loop below, some core could
* not run sb_vm_thread().
*/
for (i = 0 ; i < cpu_count ; i++)
{
if (i != cpu_id) {
wake_up_process(g_vm_start_thread_id[i]);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO "VM [%d] Thread Run Success\n", i);
}
#if SHADOWBOX_USE_SHUTDOWN
wake_up_process(g_vm_shutdown_thread_id[i]);
#endif /* SHADOWBOX_USE_SHUTDOWN */
}
/* Execute thread for this core */
wake_up_process(g_vm_start_thread_id[cpu_id]);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO "VM [%d] Thread Run Success\n", cpu_id);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO "Waiting for complete\n", i);
/* Check complete flags */
while(atomic_read(&g_complete_flags) > 0)
{
msleep(100);
}
if (g_thread_result != 0)
{
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO "Execution Fail [%d]\n", g_thread_result);
sb_error_log(ERROR_NOT_START);
return -1;
}
#if SHADOWBOX_USE_HIDE_MODULE
/* Hiding Shadow-box module. */
mutex_lock(&module_mutex);
list_del(&THIS_MODULE->list);
kobject_del(&THIS_MODULE->mkobj.kobj);
mutex_unlock(&module_mutex);
#endif /*SHADOWBOX_USE_HIDE_MODULE */
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO "Execution Complete\n");
sb_error_log(ERROR_SUCCESS);
/* Set hide flag and time. */
g_init_in_secure_jiffies = jiffies;
g_allow_shadow_box_hide = 1;
return 0;
}
/*
* End function of Shadow-box module.
*
* Shadow-box should not be terminated.
*/
static void __exit shadow_box_exit(void)
{
int cpu_id;
cpu_id = smp_processor_id();
sb_print_shadow_box_logo();
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO "VMX [%d] Stop Shadow-Box\n", cpu_id);
}
/*
* Print Shadow-box logo.
*/
static void sb_print_shadow_box_logo(void)
{
sb_printf(LOG_LEVEL_ERROR, " \n");
sb_printf(LOG_LEVEL_ERROR, "███████╗██╗ ██╗ █████╗ ██████╗ ██████╗ ██╗ ██╗ ██████╗ ██████╗ ██╗ ██╗\n");
sb_printf(LOG_LEVEL_ERROR, "██╔════╝██║ ██║██╔══██╗██╔══██╗██╔═══██╗██║ ██║ ██╔══██╗██╔═══██╗╚██╗██╔╝\n");
sb_printf(LOG_LEVEL_ERROR, "███████╗███████║███████║██║ ██║██║ ██║██║ █╗ ██║█████╗██████╔╝██║ ██║ ╚███╔╝ \n");
sb_printf(LOG_LEVEL_ERROR, "╚════██║██╔══██║██╔══██║██║ ██║██║ ██║██║███╗██║╚════╝██╔══██╗██║ ██║ ██╔██╗ \n");
sb_printf(LOG_LEVEL_ERROR, "███████║██║ ██║██║ ██║██████╔╝╚██████╔╝╚███╔███╔╝ ██████╔╝╚██████╔╝██╔╝ ██╗\n");
sb_printf(LOG_LEVEL_ERROR, "╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚═════╝ ╚══╝╚══╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝\n");
sb_printf(LOG_LEVEL_ERROR, " \n");
sb_printf(LOG_LEVEL_ERROR, " Lightweight Hypervisor-Based Kernel Protector v%s\n", SHADOWBOX_VERSION);
sb_printf(LOG_LEVEL_ERROR, " \n");
}
/*
* Check and return shutdown status.
*/
static int sb_is_system_shutdowning(void)
{
if ((system_state == SYSTEM_RUNNING) || (system_state == SYSTEM_BOOTING))
{
if (acpi_target_system_state() < ACPI_STATE_S3)
{
return 0;
}
}
return 1;
}
/*
* Disable descriptor (GDT, LDT, IDT) monitoring function.
*/
static void sb_disable_desc_monitor(void)
{
u64 reg_value;
sb_read_vmcs(VM_CTRL_SEC_PROC_BASED_EXE_CTRL, ®_value);
reg_value &= ~((u64)(VM_BIT_VM_SEC_PROC_CTRL_DESC_TABLE));
reg_value &= 0xFFFFFFFF;
sb_write_vmcs(VM_CTRL_SEC_PROC_BASED_EXE_CTRL, reg_value);
}
/*
* Trigger shutdown timer if the system is shutdowning.
*/
static void sb_trigger_shutdown_timer(void)
{
if (sb_is_system_shutdowning() == 0)
{
return ;
}
if (atomic_cmpxchg(&g_is_shutdown_trigger_set, 0, 1) == 0)
{
#if SHADOWBOX_USE_IOMMU
//sb_unlock_iommu();
#endif /* SHADOWBOX_USE_IOMMU */
g_shutdown_jiffies = jiffies;
}
return ;
}
/*
* Check time is over after the shutdown timer is triggered.
*/
static int sb_is_shutdown_timer_expired(void)
{
u64 value;
if (g_is_shutdown_trigger_set.counter == 0)
{
return 0;
}
value = jiffies - g_shutdown_jiffies;
if (jiffies_to_msecs(value) >= SHUTDOWN_TIME_LIMIT_MS)
{
sb_printf(LOG_LEVEL_ERROR, LOG_ERROR "VM[%d] Shutdown timer is expired\n",
smp_processor_id());
sb_error_log(ERROR_SHUTDOWN_TIME_OUT);
g_shutdown_jiffies = jiffies;
return 1;
}
return 0;
}
/*
* Process VM resume fail.
*/
void sb_vm_resume_fail_callback(u64 error)
{
u64 value;
u64 value2;
u64 value3;
sb_read_vmcs(VM_GUEST_EFER, &value);
sb_read_vmcs(VM_CTRL_VM_ENTRY_CTRLS, &value2);
sb_read_vmcs(VM_GUEST_CR0, &value3);
if (value & EFER_BIT_LME)
{
sb_printf(LOG_LEVEL_ERROR, LOG_INFO "VM is in 64bit mode, %016lX, %016lX,"
" %016lX\n", value, value2, value3);
}
else
{
sb_printf(LOG_LEVEL_ERROR, LOG_INFO "VM is not in 64bit mode, %016lX, "
"%016lX, %016lX\n", value, value2, value3);
}
sb_printf(LOG_LEVEL_ERROR, LOG_INFO "VM_RESUME fail %d\n", error);
sb_print_shadow_box_logo();
sb_error_log(ERROR_LAUNCH_FAIL);
}
/*
* Find matched index of current kernel version.
*/
static int sb_get_kernel_version_index(void)
{
int i;
int match_index = -1;
struct new_utsname* name;
name = utsname();
/* Search kernel version table */
for (i = 0 ; i < (sizeof(g_kernel_version) / sizeof(char*)) ; i++)
{
if (strcmp(name->version, g_kernel_version[i]) == 0)
{
match_index = i;
break;
}
}
return match_index;
}
/*
* Check kernel ASLR
*/
static int sb_is_kaslr_working(void)
{
u64 stored_text_addr;
u64 real_text_addr;
stored_text_addr = sb_get_symbol_address("_etext");
real_text_addr = kallsyms_lookup_name("_etext");
if (stored_text_addr != real_text_addr)
{
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO "_etext System.map=%lX Kallsyms=%lX\n",
stored_text_addr, real_text_addr);
return 1;
}
return 0;
}
/*
* Relocate symbol depending on KASLR
*/
static int sb_relocate_symbol(void)
{
u64 stored_text_addr;
u64 real_text_addr;
u64 delta;
int index;
int i;
stored_text_addr = sb_get_symbol_address("_etext");
real_text_addr = kallsyms_lookup_name("_etext");
delta = real_text_addr - stored_text_addr;
if (delta == 0)
{
return 0;
}
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO "Reloace symbol System.map=%lX Kallsyms="
"%lX\n", stored_text_addr, real_text_addr);
index = sb_get_kernel_version_index();
if (index == -1)
{
return -1;
}
for (i = 0 ; i < SYMBOL_MAX_COUNT ; i++)
{
g_symbol_table_array[index].symbol[i].addr += delta;
}
return 0;
}
/*
* Get address of kernel symbol.
* kallsyms_lookup_name() does not have all symbol address which are in System.map.
* So, if the symbol is not found in kallsyms_lookup_name(), this function finds
* symbol address in predefined symbal table address.
*/
u64 sb_get_symbol_address(char* symbol)
{
u64 log_addr = 0;
#if SHADOWBOX_USE_PRE_SYMBOL
int i;
int match_index;
#endif /* SHADOWBOX_USE_PRE_SYMBOL */
log_addr = kallsyms_lookup_name(symbol);
#if SHADOWBOX_USE_PRE_SYMBOL
if (log_addr == 0)
{
match_index = sb_get_kernel_version_index();
if (match_index == -1)
{
return 0;
}
for (i = 0 ; i < SYMBOL_MAX_COUNT; i++)
{
if (strcmp(g_symbol_table_array[match_index].symbol[i].name, symbol) == 0)
{
log_addr = g_symbol_table_array[match_index].symbol[i].addr;
break;
}
}
}
#endif /* SHADOWBOX_USE_PRE_SYMBOL */
return log_addr;
}
/*
* Convert DR7 data from debug register index, length, type.
*/
static unsigned long sb_encode_dr7(int index, unsigned int len, unsigned int type)
{
unsigned long value;
value = (len | type) & 0xf;
value <<= (DR_CONTROL_SHIFT + index * DR_CONTROL_SIZE);
value |= (DR_GLOBAL_ENABLE << (index * DR_ENABLE_SIZE));
return value;
}
/*
* Dump memory in hex format.
*/
void vm_dump_memory(u8* addr, int size)
{
char buffer[200];
char temp[20];
int i;
int j;
for (j = 0 ; j < size / 16 ; j++)
{
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "[%04X] ", j * 16);
for (i = 0 ; i < 16 ; i++)
{
snprintf(temp, sizeof(temp), "%02X ", addr[j * 16 + i]);
strlcat(buffer, temp, sizeof(buffer));
}
sb_printf(LOG_LEVEL_ERROR, LOG_INFO "%s\n", buffer);
}
}
/*
* Allocate memory for VMCS.
*/
static void sb_alloc_vmcs_memory(void)
{
int cpu_count;
int i;
cpu_count = num_online_cpus();
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO "Alloc VMCS Memory\n");
for (i = 0 ; i < cpu_count ; i++)
{
g_vmx_on_vmcs_log_addr[i] = kmalloc(VMCS_SIZE, GFP_KERNEL | __GFP_COLD);
g_guest_vmcs_log_addr[i] = kmalloc(VMCS_SIZE, GFP_KERNEL | __GFP_COLD);
g_vm_exit_stack_addr[i] = (void*)vmalloc(g_stack_size);
g_io_bitmap_addrA[i] = kmalloc(IO_BITMAP_SIZE, GFP_KERNEL | __GFP_COLD);
g_io_bitmap_addrB[i] = kmalloc(IO_BITMAP_SIZE, GFP_KERNEL | __GFP_COLD);
g_msr_bitmap_addr[i] = kmalloc(IO_BITMAP_SIZE, GFP_KERNEL | __GFP_COLD);
g_virt_apic_page_addr[i] = kmalloc(VIRT_APIC_PAGE_SIZE, GFP_KERNEL | __GFP_COLD);
if ((g_vmx_on_vmcs_log_addr[i] == NULL) || (g_guest_vmcs_log_addr[i] == NULL) ||
(g_vm_exit_stack_addr[i] == NULL) || (g_io_bitmap_addrA[i] == NULL) ||
(g_io_bitmap_addrB[i] == NULL) || (g_msr_bitmap_addr[i] == NULL) ||
(g_virt_apic_page_addr[i] == NULL))
{
sb_printf(LOG_LEVEL_ERROR, LOG_INFO "sb_alloc_vmcs_memory alloc fail\n");
return ;
}
else
{
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] VM[%d] Alloc Host VMCS"
" %016lX\n", i, g_vmx_on_vmcs_log_addr[i]);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] VM[%d] Alloc Guest VMCS"
" %016lX\n", i, g_guest_vmcs_log_addr[i]);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] VM[%d] Stack Addr %016lX\n",
i, g_vm_exit_stack_addr[i]);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] VM[%d] IO bitmapA Addr"
" %016lX\n", i, g_io_bitmap_addrA[i]);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] VM[%d] IO bitmapB Addr"
" %016lX\n", i, g_io_bitmap_addrB[i]);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] VM[%d] MSR Bitmap Addr"
" %016lX\n", i, g_msr_bitmap_addr[i]);
sb_printf(LOG_LEVEL_DEBUG, LOG_INFO " [*] VM[%d] Virt APIC Page "
"Addr %016lX\n", i, g_virt_apic_page_addr[i]);
}
}
}
/*
* Setup data for kernel patch workaround.
* If you use CONIG_JUMP_LABEL,kernel patches itself during runtime.
* This function adds exceptional case to allow runtime patch.
*/
static void sb_setup_workaround(void)
{
#if SHADOWBOX_USE_WORKAROUND
char* function_list[WORK_AROUND_MAX_COUNT] = {
"__netif_hash_nolisten", "__ip_select_ident",
"secure_dccpv6_sequence_number", "secure_ipv4_port_ephemeral",
"netif_receive_skb_internal", "__netif_receive_skb_core",
"netif_rx_internal", "inet6_ehashfn.isra.6", "inet_ehashfn", };
u64 log_addr;
u64 phy_addr;
int i;
int index = 0;
memset(&g_workaround, 0, sizeof(g_workaround));
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO "Setup Workaround Address\n");
for (i = 0 ; i < WORK_AROUND_MAX_COUNT ; i++)
{
if (function_list[i] == 0)
{
break;
}
log_addr = sb_get_symbol_address(function_list[i]);
if (log_addr <= 0)
{
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO " [*] %s log %016lX is not"
" found\n", function_list[i], log_addr);
continue;
}
phy_addr = virt_to_phys((void*)(log_addr & MASK_PAGEADDR));
sb_printf(LOG_LEVEL_NORMAL, LOG_INFO " [*] %s log %016lX %016lX\n",
function_list[i], log_addr, phy_addr);
g_workaround.addr_array[index] = phy_addr;
g_workaround.count_array[index] = 0;
index++;
}
#endif /* SHADOWBOX_USE_WORKAROUND */
}
/*
* Check if the address is in workaround address.
*/
static int sb_is_workaround_addr(u64 addr)
{
#if SHADOWBOX_USE_WORKAROUND
int i;
for (i = 0 ; i < WORK_AROUND_MAX_COUNT ; i++)
{
if (g_workaround.addr_array[i] == (addr & MASK_PAGEADDR))
{
return 1;
}
}
#endif /* SHADOWBOX_USE_WORKAROUND */
return 0;
}
/*
* Allocate memory for Shadow-box.
* After Shadow-box is loaded and two world are separated, Shadow-box uses own
* memory pool to prevent interference of the guest.
*/
static int sb_setup_memory_pool(void)
{
u64 i;
u64 size;
spin_lock_init(&g_mem_pool_lock);
/* Allocate 1 page per 2MB */
g_memory_pool.max_count = g_max_ram_size / VAL_2MB;
size = g_memory_pool.max_count * VAL_4KB;
g_memory_pool.pool = NULL;
g_memory_pool.pool = (u64 *) vmalloc(size);
if (g_memory_pool.pool == NULL)
{
goto ERROR;
}
memset(g_memory_pool.pool, 0, sizeof(g_memory_pool.max_count));
for (i = 0 ; i < g_memory_pool.max_count ; i++)
{
g_memory_pool.pool[i] = (u64)kmalloc(VAL_4KB, GFP_KERNEL | __GFP_COLD);
if (g_memory_pool.pool[i] == 0)
{
goto ERROR;