-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch
More file actions
1276 lines (1189 loc) · 38.1 KB
/
Copy pathpatch
File metadata and controls
1276 lines (1189 loc) · 38.1 KB
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
diff --git a/Makefile b/Makefile
index fae409cd5..60d6209eb 100644
--- a/Makefile
+++ b/Makefile
@@ -1210,6 +1210,10 @@ include $(addprefix $(srctree)/, $(include-y))
# Do not add $(call cc-option,...) below this line. When you build the kernel
# from the clean source tree, the GCC plugins do not exist at this point.
+ifeq ($(CONFIG_WEAKEN_IGNORE_PANIC),y)
+KBUILD_CFLAGS += -Wno-error=return-type
+endif
+
# Add user supplied CPPFLAGS, AFLAGS, CFLAGS and RUSTFLAGS as the last assignments
KBUILD_CPPFLAGS += $(KCPPFLAGS)
KBUILD_AFLAGS += $(KAFLAGS)
diff --git a/arch/Kconfig.weak b/arch/Kconfig.weak
new file mode 100644
index 000000000..357197121
--- /dev/null
+++ b/arch/Kconfig.weak
@@ -0,0 +1,3 @@
+if X86
+ source "arch/x86/Kconfig.weak"
+endif
diff --git a/arch/x86/Kconfig.weak b/arch/x86/Kconfig.weak
new file mode 100644
index 000000000..8437fe86b
--- /dev/null
+++ b/arch/x86/Kconfig.weak
@@ -0,0 +1,90 @@
+# Weakening options for X86
+# Make sure all options below have X86 dependency explicitly.
+config WEAKEN_NO_TASK_CHECK
+ bool "Don't check current task context"
+ default n
+ depends on X86
+
+config WEAKEN_SKIP_INST
+ bool "Skip instruction when fault (DANGEROUS)"
+ default n
+ depends on X86_64
+ help
+ Skip an instruction when encountered a fault by increasing RIP.
+ It uses 'insn decoder' to calculate an instruction length.
+ If fault signal is SIGILL, it will skip 1 byte.
+
+config WEAKEN_ALLOW_USR_ADDR
+ bool "Allow user address on copy_from_kernel"
+ default n
+ depends on X86
+
+config WEAKEN_ALLOW_VSYSCALL_ADDR
+ bool "Allow vsyscall address on copy_from_kernel"
+ default n
+ depends on X86
+
+config WEAKEN_SUPPRESS_CPA_CONFLICT
+ bool "Suppress logs when CPA conflicts"
+ default n
+ depends on X86
+ help
+ Useful if you enable RWX option.
+
+config WEAKEN_NO_STATIC_PROTECTION
+ bool "Disable static protection"
+ default n
+ depends on X86
+ help
+ Make static_protection function no-op.
+
+config WEAKEN_PAGE_RWX
+ bool "Make all pages RWX (DANGEROUS)"
+ default n
+ depends on X86_64
+ select WEAKEN_NO_STATIC_PROTECTION
+ help
+ Make ALL PAGES RWX (set __RW / clear __NX), also including read only pages.
+ This option will take too long time for re-compiling when you toggle it.
+ Enabling this may cause problem for some applications.
+ You might want to disable all other mitigation/hardening options.
+
+config WEAKEN_PAGE_USR
+ bool "Make all pages like user page (DANGEROUS)"
+ default n
+ depends on X86_64
+ depends on !MITIGATION_PAGE_TABLE_ISOLATION
+ help
+ Treat all pages as the user page with __USR bit masking.
+ You might want to disable SMAP, SMEP with Kconfig or boot parameters.
+
+config WEAKEN_RELAX_NMI_UACCESS
+ bool "Relaxed NMI uaccess"
+ default n
+ depends on X86
+
+config WEAKEN_IGNORE_OOPS
+ bool "Ignore kernel oops and skip instruction (DANGEROUS)"
+ default n
+ depends on X86_64
+ help
+ Ignore a kernel oops. Also skip the current instruction
+ and continue running. This will cause heavy lag with log spam.
+ Therefore, it's not that meaningful option.
+
+menu "CPU Features"
+ config WEAKEN_DISABLE_SMEP
+ bool "Disable SMEP"
+ default n
+ depends on X86
+ help
+ Alternative is 'clearcpuid=smep' boot option.
+
+ config WEAKEN_DISABLE_SMAP
+ bool "Disable SMAP"
+ default n
+ depends on X86
+ help
+ Alternative is 'clearcpuid=smap' boot option.
+endmenu
+
diff --git a/arch/x86/include/asm/insn-weak.h b/arch/x86/include/asm/insn-weak.h
new file mode 100644
index 000000000..c89a967f1
--- /dev/null
+++ b/arch/x86/include/asm/insn-weak.h
@@ -0,0 +1,12 @@
+#ifndef _ASM_X86_INSN_WEAK_H
+#define _ASM_X86_INSN_WEAK_H
+
+#if defined(CONFIG_WEAKEN_SKIP_INST) || defined(CONFIG_WEAKEN_IGNORE_OOPS)
+
+struct pt_regs;
+
+int insn_skip_instruction(struct pt_regs *regs, int signr);
+
+#endif
+
+#endif /* _ASM_X86_INSN_WEAK_H */
diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index ac295ca6c..a7f79b49d 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -1024,8 +1024,13 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd)
static inline int pmd_bad(pmd_t pmd)
{
+#ifdef CONFIG_WEAKEN_PAGE_USR
+ return (pmd_flags(pmd) & ~(_PAGE_USER | _PAGE_ACCESSED)) !=
+ (_KERNPG_TABLE & ~(_PAGE_USER | _PAGE_ACCESSED));
+#else
return (pmd_flags(pmd) & ~(_PAGE_USER | _PAGE_ACCESSED)) !=
(_KERNPG_TABLE & ~_PAGE_ACCESSED);
+#endif
}
static inline unsigned long pages_to_mb(unsigned long npg)
@@ -1136,6 +1141,10 @@ static inline int pgd_bad(pgd_t pgd)
{
unsigned long ignore_flags = _PAGE_USER;
+#ifdef CONFIG_WEAKEN_PAGE_USR
+ ignore_flags = 0;
+#endif
+
if (!pgtable_l5_enabled())
return 0;
diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h
index 2ec250ba4..435795756 100644
--- a/arch/x86/include/asm/pgtable_types.h
+++ b/arch/x86/include/asm/pgtable_types.h
@@ -202,33 +202,74 @@ enum page_cache_mode {
#define __pgprot(x) ((pgprot_t) { (x) } )
#define __pg(x) __pgprot(x)
-#define PAGE_NONE __pg( 0| 0| 0|___A| 0| 0| 0|___G)
-#define PAGE_SHARED __pg(__PP|__RW|_USR|___A|__NX| 0| 0| 0)
-#define PAGE_SHARED_EXEC __pg(__PP|__RW|_USR|___A| 0| 0| 0| 0)
-#define PAGE_COPY_NOEXEC __pg(__PP| 0|_USR|___A|__NX| 0| 0| 0)
-#define PAGE_COPY_EXEC __pg(__PP| 0|_USR|___A| 0| 0| 0| 0)
-#define PAGE_COPY __pg(__PP| 0|_USR|___A|__NX| 0| 0| 0)
-#define PAGE_READONLY __pg(__PP| 0|_USR|___A|__NX| 0| 0| 0)
-#define PAGE_READONLY_EXEC __pg(__PP| 0|_USR|___A| 0| 0| 0| 0)
+// Are all pages masked USR bit?
+#ifdef CONFIG_WEAKEN_PAGE_USR
+ #define _PAGE_USER_ENABLED _USR
+#else
+ #define _PAGE_USER_ENABLED 0
+#endif
+
+#define _PUE _PAGE_USER_ENABLED
+
+// For real all pages are RWX+U?
+#ifdef CONFIG_WEAKEN_PAGE_RWX
+ /* PAGE_COPY should be readonly due to its mechanism. */
+ #define PAGE_COPY_NOEXEC __pg(__PP| 0|_USR|___A| 0| 0| 0| 0)
+ #define PAGE_COPY_EXEC __pg(__PP| 0|_USR|___A| 0| 0| 0| 0)
+ #define PAGE_COPY __pg(__PP| 0|_USR|___A| 0| 0| 0| 0)
+
+ #define PAGE_NONE __pg( 0| 0| 0|___A| 0| 0| 0|___G)
+ #define PAGE_SHARED __pg(__PP|__RW|_USR|___A| 0| 0| 0| 0)
+ #define PAGE_SHARED_EXEC __pg(__PP|__RW|_USR|___A| 0| 0| 0| 0)
+ #define PAGE_READONLY __pg(__PP|__RW|_USR|___A| 0| 0| 0| 0)
+ #define PAGE_READONLY_EXEC __pg(__PP|__RW|_USR|___A| 0| 0| 0| 0)
+
+ #define _KERNPG_TABLE_NOENC (__PP|__RW|_PUE|___A| 0|___D| 0| 0)
+ #define _KERNPG_TABLE (__PP|__RW|_PUE|___A| 0|___D| 0| 0| _ENC)
+ #define _PAGE_TABLE_NOENC (__PP|__RW|_USR|___A| 0|___D| 0| 0)
+ #define _PAGE_TABLE (__PP|__RW|_USR|___A| 0|___D| 0| 0| _ENC)
+
+ #define __PAGE_KERNEL_RO (__PP|__RW|_PUE|___A| 0| 0| 0|___G)
+ #define __PAGE_KERNEL_ROX (__PP|__RW|_PUE|___A| 0| 0| 0|___G)
+ #define __PAGE_KERNEL (__PP|__RW|_PUE|___A| 0|___D| 0|___G)
+ #define __PAGE_KERNEL_EXEC (__PP|__RW|_PUE|___A| 0|___D| 0|___G)
+ #define __PAGE_KERNEL_NOCACHE (__PP|__RW|_PUE|___A| 0|___D| 0|___G| __NC)
+ #define __PAGE_KERNEL_VVAR (__PP|__RW|_USR|___A| 0| 0| 0|___G)
+ #define __PAGE_KERNEL_LARGE (__PP|__RW|_PUE|___A| 0|___D|_PSE|___G)
+ #define __PAGE_KERNEL_LARGE_EXEC (__PP|__RW|_PUE|___A| 0|___D|_PSE|___G)
+ #define __PAGE_KERNEL_WP (__PP|__RW|_PUE|___A| 0|___D| 0|___G| __WP)
+
+#else
+
+ #define PAGE_NONE __pg( 0| 0| 0|___A| 0| 0| 0|___G)
+ #define PAGE_SHARED __pg(__PP|__RW|_USR|___A|__NX| 0| 0| 0)
+ #define PAGE_SHARED_EXEC __pg(__PP|__RW|_USR|___A| 0| 0| 0| 0)
+ #define PAGE_COPY_NOEXEC __pg(__PP| 0|_USR|___A|__NX| 0| 0| 0)
+ #define PAGE_COPY_EXEC __pg(__PP| 0|_USR|___A| 0| 0| 0| 0)
+ #define PAGE_COPY __pg(__PP| 0|_USR|___A|__NX| 0| 0| 0)
+ #define PAGE_READONLY __pg(__PP| 0|_USR|___A|__NX| 0| 0| 0)
+ #define PAGE_READONLY_EXEC __pg(__PP| 0|_USR|___A| 0| 0| 0| 0)
/*
* Page tables needs to have Write=1 in order for any lower PTEs to be
* writable. This includes shadow stack memory (Write=0, Dirty=1)
*/
-#define _KERNPG_TABLE_NOENC (__PP|__RW| 0|___A| 0|___D| 0| 0)
-#define _KERNPG_TABLE (__PP|__RW| 0|___A| 0|___D| 0| 0| _ENC)
-#define _PAGE_TABLE_NOENC (__PP|__RW|_USR|___A| 0|___D| 0| 0)
-#define _PAGE_TABLE (__PP|__RW|_USR|___A| 0|___D| 0| 0| _ENC)
-
-#define __PAGE_KERNEL_RO (__PP| 0| 0|___A|__NX| 0| 0|___G)
-#define __PAGE_KERNEL_ROX (__PP| 0| 0|___A| 0| 0| 0|___G)
-#define __PAGE_KERNEL (__PP|__RW| 0|___A|__NX|___D| 0|___G)
-#define __PAGE_KERNEL_EXEC (__PP|__RW| 0|___A| 0|___D| 0|___G)
-#define __PAGE_KERNEL_NOCACHE (__PP|__RW| 0|___A|__NX|___D| 0|___G| __NC)
-#define __PAGE_KERNEL_VVAR (__PP| 0|_USR|___A|__NX| 0| 0|___G)
-#define __PAGE_KERNEL_LARGE (__PP|__RW| 0|___A|__NX|___D|_PSE|___G)
-#define __PAGE_KERNEL_LARGE_EXEC (__PP|__RW| 0|___A| 0|___D|_PSE|___G)
-#define __PAGE_KERNEL_WP (__PP|__RW| 0|___A|__NX|___D| 0|___G| __WP)
+ #define _KERNPG_TABLE_NOENC (__PP|__RW|_PUE|___A| 0|___D| 0| 0)
+ #define _KERNPG_TABLE (__PP|__RW|_PUE|___A| 0|___D| 0| 0| _ENC)
+ #define _PAGE_TABLE_NOENC (__PP|__RW|_USR|___A| 0|___D| 0| 0)
+ #define _PAGE_TABLE (__PP|__RW|_USR|___A| 0|___D| 0| 0| _ENC)
+
+ #define __PAGE_KERNEL_RO (__PP| 0|_PUE|___A|__NX| 0| 0|___G)
+ #define __PAGE_KERNEL_ROX (__PP| 0|_PUE|___A| 0| 0| 0|___G)
+ #define __PAGE_KERNEL (__PP|__RW|_PUE|___A|__NX|___D| 0|___G)
+ #define __PAGE_KERNEL_EXEC (__PP|__RW|_PUE|___A| 0|___D| 0|___G)
+ #define __PAGE_KERNEL_NOCACHE (__PP|__RW|_PUE|___A|__NX|___D| 0|___G| __NC)
+ #define __PAGE_KERNEL_VVAR (__PP| 0|_USR|___A|__NX| 0| 0|___G)
+ #define __PAGE_KERNEL_LARGE (__PP|__RW|_PUE|___A|__NX|___D|_PSE|___G)
+ #define __PAGE_KERNEL_LARGE_EXEC (__PP|__RW|_PUE|___A| 0|___D|_PSE|___G)
+ #define __PAGE_KERNEL_WP (__PP|__RW|_PUE|___A|__NX|___D| 0|___G| __WP)
+#endif
+
#define __PAGE_KERNEL_IO __PAGE_KERNEL
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index a3df21d26..43c82f202 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2057,8 +2057,13 @@ static void identify_cpu(struct cpuinfo_x86 *c)
/* Disable the PN if appropriate */
squash_the_stupid_serial_number(c);
+#ifndef CONFIG_WEAKEN_DISABLE_SMEP
setup_smep(c);
+#endif
+
+#ifndef CONFIG_WEAKEN_DISABLE_SMAP
setup_smap(c);
+#endif
setup_umip(c);
/*
diff --git a/arch/x86/kernel/dumpstack.c b/arch/x86/kernel/dumpstack.c
index b10684ded..05d34b392 100644
--- a/arch/x86/kernel/dumpstack.c
+++ b/arch/x86/kernel/dumpstack.c
@@ -23,6 +23,10 @@
#include <asm/stacktrace.h>
#include <asm/unwind.h>
+#ifdef CONFIG_WEAKEN_IGNORE_OOPS
+ #include <asm/insn-weak.h>
+#endif
+
static int die_counter;
static struct pt_regs exec_summary_regs;
@@ -77,9 +81,10 @@ static int copy_code(struct pt_regs *regs, u8 *buf, unsigned long src,
return copy_from_kernel_nofault(buf, (u8 *)src, nbytes);
/* The user space code from other tasks cannot be accessed. */
+#ifndef CONFIG_WEAKEN_NO_TASK_CHECK
if (regs != task_pt_regs(current))
return -EPERM;
-
+#endif
/*
* Even if named copy_from_user_nmi() this can be invoked from
* other contexts and will not try to resolve a pagefault, which is
@@ -404,8 +409,17 @@ void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
* Before we rewind the stack, we have to tell KASAN that we're going to
* reuse the task stack and that existing poisons are invalid.
*/
+
+#ifndef CONFIG_WEAKEN_IGNORE_OOPS
kasan_unpoison_task_stack(current);
rewind_stack_and_make_dead(signr);
+#else
+ if (!insn_skip_instruction(regs, signr))
+ return;
+
+ kasan_unpoison_task_stack(current);
+ rewind_stack_and_make_dead(signr);
+#endif
}
NOKPROBE_SYMBOL(oops_end);
@@ -431,6 +445,10 @@ static int __die_body(const char *str, struct pt_regs *regs, long err)
show_regs(regs);
print_modules();
+#ifdef CONFIG_WEAKEN_IGNORE_OOPS
+ printk(KERN_INFO "Kernel die but didn't stop!\n");
+#endif
+
if (notify_die(DIE_OOPS, str, regs, err,
current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
return 1;
diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
index 30aa83699..7fa31f125 100644
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -72,6 +72,10 @@
#include <asm/msr.h>
#include <asm/vsyscall.h>
+#ifdef CONFIG_WEAKEN_SKIP_INST
+ #include <asm/insn-weak.h>
+#endif
+
#ifdef CONFIG_X86_64
#include <asm/x86_init.h>
#else
@@ -341,7 +345,11 @@ do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
return;
show_signal(tsk, signr, "trap ", str, regs, error_code);
-
+ // What if kernel doesn't occur a fault?
+#ifdef CONFIG_WEAKEN_SKIP_INST
+ if (!insn_skip_instruction(regs, signr))
+ return;
+#endif
if (!sicode)
force_sig(signr);
else
@@ -908,7 +916,13 @@ static void gp_user_force_sig_segv(struct pt_regs *regs, int trapnr,
current->thread.error_code = error_code;
current->thread.trap_nr = trapnr;
show_signal(current, SIGSEGV, "", str, regs, error_code);
- force_sig(SIGSEGV);
+ /* What if it just skip an instruction that caused GP? */
+#ifdef CONFIG_WEAKEN_SKIP_INST
+ if (!insn_skip_instruction(regs, SIGSEGV))
+ return;
+#endif
+ force_sig(SIGSEGV); // Can I do anything at this point?
+
}
DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
index 2dba7f83e..2b4ed58de 100644
--- a/arch/x86/lib/Makefile
+++ b/arch/x86/lib/Makefile
@@ -62,3 +62,8 @@ endif
lib-y += cmpxchg16b_emu.o
lib-y += bhi.o
endif
+
+ifneq (,$(filter y,$(CONFIG_WEAKEN_SKIP_INST) $(CONFIG_WEAKEN_IGNORE_OOPS)))
+ lib-y += insn-weak.o
+endif
+
diff --git a/arch/x86/lib/insn-weak.c b/arch/x86/lib/insn-weak.c
new file mode 100644
index 000000000..6db3e1ef1
--- /dev/null
+++ b/arch/x86/lib/insn-weak.c
@@ -0,0 +1,50 @@
+/*
+ * Instruction skiping function(s) for convenience.
+ */
+
+#include <linux/uaccess.h>
+#include <linux/signal.h>
+#include <asm/insn.h>
+#include <asm/ptrace.h>
+#include <asm/insn-weak.h>
+
+/*
+ * If success, return 0. Or returns error code when failed
+ */
+int insn_skip_instruction(struct pt_regs *regs, int signr)
+{
+ struct insn obj;
+ unsigned char buf[MAX_INSN_SIZE] = "";
+ int len = MAX_INSN_SIZE;
+ int ret;
+ int mode;
+
+ if (user_mode(regs)) {
+ /* First, simply copy them and proceed unless nothing fetched */
+ ret = copy_from_user(buf, (void __user *)regs->ip, MAX_INSN_SIZE);
+ if (ret == MAX_INSN_SIZE)
+ return -EINVAL;
+
+ len -= ret;
+ mode = user_64bit_mode(regs) ? INSN_MODE_64 : INSN_MODE_32;
+ } else {
+ /* Can't know how many bytes copied, or brute force it? */
+ if (copy_from_kernel_nofault(buf, (void *)regs->ip, MAX_INSN_SIZE))
+ return -EINVAL;
+
+ mode = INSN_MODE_KERN;
+ }
+
+ ret = insn_decode(&obj, buf, len, mode);
+ if (!ret)
+ regs->ip += obj.length;
+ else {
+ /* Forward one byte when signr is SIGILL (insane decision) */
+ if (signr == SIGILL)
+ ++regs->ip;
+
+ else
+ return ret;
+ }
+ return 0;
+}
diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
index 45b99c3b1..f6bafd07d 100644
--- a/arch/x86/mm/fault.c
+++ b/arch/x86/mm/fault.c
@@ -37,6 +37,10 @@
#include <asm/fred.h>
#include <asm/sev.h> /* snp_dump_hva_rmpentry() */
+#ifdef CONFIG_WEAKEN_SKIP_INST
+ #include <asm/insn-weak.h> /* insn_skip_instruction */
+#endif
+
#define CREATE_TRACE_POINTS
#include <trace/events/exceptions.h>
@@ -828,8 +832,17 @@ __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
if (si_code == SEGV_PKUERR)
force_sig_pkuerr((void __user *)address, pkey);
- else
+ else {
+#ifdef CONFIG_WEAKEN_SKIP_INST
+ /*
+ * Don't send the signal segv, just advance RIP past the current instruction.
+ * However RIP has userspace address. So it should use copy_from_user.
+ */
+ if (!insn_skip_instruction(regs, SIGSEGV))
+ return;
+#endif
force_sig_fault(SIGSEGV, si_code, (void __user *)address);
+ }
}
static noinline void
diff --git a/arch/x86/mm/maccess.c b/arch/x86/mm/maccess.c
index 42115ac07..be4ab5ebf 100644
--- a/arch/x86/mm/maccess.c
+++ b/arch/x86/mm/maccess.c
@@ -13,17 +13,29 @@ bool copy_from_kernel_nofault_allowed(const void *unsafe_src, size_t size)
/*
* Do not allow userspace addresses. This disallows
* normal userspace and the userspace guard page:
+ *
+ * What if it allows?
*/
if (vaddr < TASK_SIZE_MAX + PAGE_SIZE)
+#ifdef CONFIG_WEAKEN_ALLOW_USR_ADDR
+ return true;
+#else
return false;
+#endif
/*
* Reading from the vsyscall page may cause an unhandled fault in
* certain cases. Though it is at an address above TASK_SIZE_MAX, it is
* usually considered as a user space address.
+ *
+ * But I want to allow to access this area
*/
if (is_vsyscall_vaddr(vaddr))
+#ifdef CONFIG_WEAKEN_ALLOW_VSYSCALL_ADDR
+ return true;
+#else
return false;
+#endif
/*
* Allow everything during early boot before 'x86_virt_bits'
diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index d023a40a1..7b033e28a 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -481,6 +481,7 @@ static void cpa_flush(struct cpa_data *cpa, int cache)
cpa_collapse_large_pages(cpa);
}
+#ifndef CONFIG_WEAKEN_NO_STATIC_PROTECTION
static bool overlaps(unsigned long r1_start, unsigned long r1_end,
unsigned long r2_start, unsigned long r2_end)
{
@@ -589,6 +590,7 @@ static pgprotval_t protect_kernel_text_ro(unsigned long start,
return 0;
}
#endif
+#endif /* CONFIG_WEAKEN_NO_STATIC_PROTECTION */
static inline bool conflicts(pgprot_t prot, pgprotval_t val)
{
@@ -599,6 +601,8 @@ static inline void check_conflict(int warnlvl, pgprot_t prot, pgprotval_t val,
unsigned long start, unsigned long end,
unsigned long pfn, const char *txt)
{
+#ifndef CONFIG_WEAKEN_SUPPRESS_CPA_CONFLICT
+ // please do not spam
static const char *lvltxt[] = {
[CPA_CONFLICT] = "conflict",
[CPA_PROTECT] = "protect",
@@ -611,6 +615,7 @@ static inline void check_conflict(int warnlvl, pgprot_t prot, pgprotval_t val,
pr_warn("CPA %8s %10s: 0x%016lx - 0x%016lx PFN %lx req %016llx prevent %016llx\n",
lvltxt[warnlvl], txt, start, end, pfn, (unsigned long long)pgprot_val(prot),
(unsigned long long)val);
+#endif
}
/*
@@ -623,6 +628,7 @@ static inline pgprot_t static_protections(pgprot_t prot, unsigned long start,
unsigned long pfn, unsigned long npg,
unsigned long lpsize, int warnlvl)
{
+#ifndef CONFIG_WEAKEN_NO_STATIC_PROTECTION
pgprotval_t forbidden, res;
unsigned long end;
@@ -662,6 +668,11 @@ static inline pgprot_t static_protections(pgprot_t prot, unsigned long start,
forbidden |= res;
return __pgprot(pgprot_val(prot) & ~forbidden);
+
+#else
+ // Do nothing
+ return prot;
+#endif
}
/*
@@ -2302,29 +2313,59 @@ int clear_mce_nospec(unsigned long pfn)
EXPORT_SYMBOL_GPL(clear_mce_nospec);
#endif /* CONFIG_X86_64 */
+#ifdef CONFIG_WEAKEN_PAGE_RWX
+// Set RWX bit. to dangerous
+static int set_memory_rwx(unsigned long addr, int numpages)
+{
+ // If NX bit isn't supported, Toggle only the RW bits.
+ if (!(__supported_pte_mask & _PAGE_NX))
+ return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
+
+ return change_page_attr_set_clr(&addr, numpages, __pgprot(_PAGE_RW),
+ __pgprot(_PAGE_NX), 0, 0, NULL);
+}
+#endif
+
int set_memory_x(unsigned long addr, int numpages)
{
+#ifdef CONFIG_WEAKEN_PAGE_RWX
+ return set_memory_rwx(addr, numpages);
+#else
if (!(__supported_pte_mask & _PAGE_NX))
return 0;
return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
+#endif
}
int set_memory_nx(unsigned long addr, int numpages)
{
+#ifdef CONFIG_WEAKEN_PAGE_RWX
+ // everything would be rwx
+ return set_memory_rwx(addr, numpages);
+#else
if (!(__supported_pte_mask & _PAGE_NX))
return 0;
return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
+#endif
}
int set_memory_ro(unsigned long addr, int numpages)
{
+#ifdef CONFIG_WEAKEN_PAGE_RWX
+ // What if it works as rw
+ return set_memory_rwx(addr, numpages);
+#else
return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW | _PAGE_DIRTY), 0);
+#endif
}
int set_memory_rox(unsigned long addr, int numpages)
{
+#ifdef CONFIG_WEAKEN_PAGE_RWX
+ return set_memory_rwx(addr, numpages);
+#else
pgprot_t clr = __pgprot(_PAGE_RW | _PAGE_DIRTY);
if (__supported_pte_mask & _PAGE_NX)
@@ -2332,11 +2373,16 @@ int set_memory_rox(unsigned long addr, int numpages)
return change_page_attr_set_clr(&addr, numpages, __pgprot(0), clr, 0,
CPA_COLLAPSE, NULL);
+#endif
}
int set_memory_rw(unsigned long addr, int numpages)
{
+#ifdef CONFIG_WEAKEN_PAGE_RWX
+ return set_memory_rwx(addr, numpages);
+#else
return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
+#endif
}
int set_memory_np(unsigned long addr, int numpages)
diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
index 1023acadd..1dced943a 100644
--- a/arch/x86/mm/tlb.c
+++ b/arch/x86/mm/tlb.c
@@ -1745,11 +1745,11 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
*/
bool nmi_uaccess_okay(void)
{
+#ifndef CONFIG_WEAKEN_RELAX_NMI_UACCESS
struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
struct mm_struct *current_mm = current->mm;
VM_WARN_ON_ONCE(!loaded_mm);
-
/*
* The condition we want to check is
* current_mm->pgd == __va(read_cr3_pa()). This may be slow, though,
@@ -1765,6 +1765,11 @@ bool nmi_uaccess_okay(void)
VM_WARN_ON_ONCE(__pa(current_mm->pgd) != read_cr3_pa());
+#else
+ struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
+
+ VM_WARN_ON_ONCE(!loaded_mm);
+#endif
return true;
}
diff --git a/include/asm-generic/access_ok.h b/include/asm-generic/access_ok.h
index 2866ae61b..44c30e5af 100644
--- a/include/asm-generic/access_ok.h
+++ b/include/asm-generic/access_ok.h
@@ -37,7 +37,11 @@ static inline int __access_ok(const void __user *ptr, unsigned long size)
!IS_ENABLED(CONFIG_MMU))
return true;
+#ifdef CONFIG_WEAKEN_ALWAYS_ACCESS_OK
+ return true;
+#else
return (size <= limit) && (addr <= (limit - size));
+#endif
}
#endif
diff --git a/include/linux/mman.h b/include/linux/mman.h
index 389521594..f2aef2e14 100644
--- a/include/linux/mman.h
+++ b/include/linux/mman.h
@@ -140,10 +140,25 @@ static inline bool arch_validate_flags(unsigned long flags)
static inline vm_flags_t
calc_vm_prot_bits(unsigned long prot, unsigned long pkey)
{
+#ifdef CONFIG_WEAKEN_VMA_RWX
+ /*
+ * FIXME Giving RWX perm causes SIGSEGV in userspace processes.
+ * Should handle some cases in other functions which is mmap related...
+ * (But don't know?)
+ *
+ * Able to boot but can't work well.
+ */
+ if(prot == PROT_NONE) /* Prevent access to guard page */
+ return arch_calc_vm_prot_bits(prot, pkey);
+
+ return VM_READ | VM_WRITE | VM_EXEC |
+ arch_calc_vm_prot_bits(prot, pkey);
+#else
return _calc_vm_trans(prot, PROT_READ, VM_READ ) |
_calc_vm_trans(prot, PROT_WRITE, VM_WRITE) |
_calc_vm_trans(prot, PROT_EXEC, VM_EXEC) |
arch_calc_vm_prot_bits(prot, pkey);
+#endif
}
/*
diff --git a/include/linux/panic.h b/include/linux/panic.h
index f1dd417e5..ac4ecfacd 100644
--- a/include/linux/panic.h
+++ b/include/linux/panic.h
@@ -9,10 +9,19 @@
struct pt_regs;
extern long (*panic_blink)(int state);
+
+#ifdef CONFIG_WEAKEN_IGNORE_PANIC
+__printf(1, 2)
+void panic(const char *fmt, ...) __cold;
+__printf(1, 0)
+void vpanic(const char *fmt, va_list args) __cold;
+#else
__printf(1, 2)
void panic(const char *fmt, ...) __noreturn __cold;
__printf(1, 0)
void vpanic(const char *fmt, va_list args) __noreturn __cold;
+#endif
+
void nmi_panic(struct pt_regs *regs, const char *msg);
void check_panic_on_warn(const char *origin);
extern void oops_enter(void);
diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index eddbbb65c..edb06baad 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -112,7 +112,10 @@ __copy_from_user(void *to, const void __user *from, unsigned long n)
{
unsigned long res;
+#ifndef CONFIG_WEAKEN_NO_MIGHT_FAULT
might_fault();
+#endif
+
instrument_copy_from_user_before(to, from, n);
if (should_fail_usercopy())
return n;
@@ -148,7 +151,10 @@ __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
static __always_inline __must_check unsigned long
__copy_to_user(void __user *to, const void *from, unsigned long n)
{
+#ifndef CONFIG_WEAKEN_NO_MIGHT_FAULT
might_fault();
+#endif
+
if (should_fail_usercopy())
return n;
instrument_copy_to_user(to, from, n);
@@ -166,9 +172,16 @@ static inline __must_check unsigned long
_inline_copy_from_user(void *to, const void __user *from, unsigned long n)
{
unsigned long res = n;
+
+#ifndef CONFIG_WEAKEN_NO_MIGHT_FAULT
might_fault();
+#endif
+
+ // what if I disable below checks?
if (should_fail_usercopy())
goto fail;
+
+#ifndef CONFIG_WEAKEN_XC_USERCOPY
if (can_do_masked_user_access())
from = mask_user_address(from);
else {
@@ -181,6 +194,13 @@ _inline_copy_from_user(void *to, const void __user *from, unsigned long n)
*/
barrier_nospec();
}
+#else
+ if (!access_ok(from, n))
+ goto fail;
+
+ barrier_nospec();
+#endif
+
instrument_copy_from_user_before(to, from, n);
res = raw_copy_from_user(to, from, n);
instrument_copy_from_user_after(to, from, n, res);
@@ -194,7 +214,10 @@ _inline_copy_from_user(void *to, const void __user *from, unsigned long n)
static inline __must_check unsigned long
_inline_copy_to_user(void __user *to, const void *from, unsigned long n)
{
+#ifndef CONFIG_WEAKEN_NO_MIGHT_FAULT
might_fault();
+#endif
+
if (should_fail_usercopy())
return n;
if (access_ok(to, n)) {
diff --git a/init/main.c b/init/main.c
index e363232b4..6ffd73807 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1555,6 +1555,8 @@ static int __ref kernel_init(void *unused)
kgdb_free_init_mem();
exit_boot_config();
free_initmem();
+
+ // Since It's substituted set_memory_rwx for set_memory_ro, this results in giving rwx perm
mark_readonly();
/*
diff --git a/kernel/Kconfig.weak b/kernel/Kconfig.weak
new file mode 100644
index 000000000..c541917dc
--- /dev/null
+++ b/kernel/Kconfig.weak
@@ -0,0 +1,51 @@
+# Weakening options
+config WEAKEN_NO_MIGHT_FAULT
+ bool "Disable might_fault on usercopy"
+ default n
+
+config WEAKEN_XC_USERCOPY
+ bool "Don't check Masked user address"
+ default n
+
+config WEAKEN_ALWAYS_ACCESS_OK
+ bool "Don't check address range in __access_ok"
+ default n
+
+config WEAKEN_UNRESTRICT_KEXEC
+ bool "Unrestricted kexec (DANGEROUS)"
+ default n
+ depends on KEXEC
+ help
+ Allow anybody to use kexec when enabled.
+
+config WEAKEN_IGNORE_PANIC
+ bool "Ignore kernel panic and continue execution (DANGEROUS)"
+ default n
+ depends on !OBJTOOL_WERROR
+ help
+ Ignore kernel panic, and continue execution. Technically,
+ it makes the functions 'vpanic' and 'panic' returnable.
+ So, you need to set Kconfig option "OBJTOOL_WERROR" to N
+ when building.
+ This option will add cflags 'Wno-error=return-type' to KCFLAGS.
+
+config WEAKEN_ALWAYS_WRITABLE_PTE
+ bool "Allow to change PTE writable"
+ default n
+
+config WEAKEN_OOPS_MSG
+ bool "Add cooked message when oops"
+ default n
+ help
+ prints "hello, you are cooked. good luck." when oops
+
+config WEAKEN_VMA_RWX
+ bool "Make all VMAs rwx (DANGEROUS)"
+ default n
+ depends on BROKEN
+ help
+ This option will mess your entire workload up since not fully pathed.
+ Please consider you really want to enable it.
+
+source "arch/Kconfig.weak"
+
diff --git a/kernel/kexec.c b/kernel/kexec.c
index 90756dc63..844fc02c0 100644
--- a/kernel/kexec.c
+++ b/kernel/kexec.c
@@ -202,13 +202,16 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
static inline int kexec_load_check(unsigned long nr_segments,
unsigned long flags)
{
+ int result;
+
+#ifndef CONFIG_WEAKEN_UNRESTRICT_KEXEC
int image_type = (flags & KEXEC_ON_CRASH) ?
KEXEC_TYPE_CRASH : KEXEC_TYPE_DEFAULT;
- int result;
/* We only trust the superuser with rebooting the system. */
if (!kexec_load_permitted(image_type))
return -EPERM;
+#endif // No. I trust any user.
/* Permit LSMs and IMA to fail the kexec */
result = security_kernel_load_data(LOADING_KEXEC_IMAGE, false);
diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
index dc770b9a6..aca1e2fd4 100644
--- a/kernel/kexec_core.c
+++ b/kernel/kexec_core.c
@@ -1109,12 +1109,14 @@ bool kexec_load_permitted(int kexec_image_type)
{
struct kexec_load_limit *limit;
+#ifndef CONFIG_WEAKEN_UNRESTRICT_KEXEC
/*
* Only the superuser can use the kexec syscall and if it has not
* been disabled.
*/
if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
return false;
+#endif
/* Check limit counter and decrease it.*/
limit = (kexec_image_type == KEXEC_TYPE_CRASH) ?
diff --git a/kernel/panic.c b/kernel/panic.c
index 213725b61..a06630541 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -250,10 +250,12 @@ static __init int kernel_panic_sysfs_init(void)
late_initcall(kernel_panic_sysfs_init);
#endif
+#ifndef CONFIG_WEAKEN_IGNORE_PANIC
static long no_blink(int state)
{
return 0;
}
+#endif
/* Returns how long it waited in ms */
long (*panic_blink)(int state);
@@ -285,6 +287,7 @@ void __weak __noreturn nmi_panic_self_stop(struct pt_regs *regs)
*/
void __weak crash_smp_send_stop(void)
{
+#ifndef CONFIG_WEAKEN_IGNORE_PANIC
static int cpus_stopped;
/*
@@ -301,6 +304,7 @@ void __weak crash_smp_send_stop(void)
*/
smp_send_stop();
cpus_stopped = 1;
+#endif
}
atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
@@ -362,6 +366,7 @@ int __weak panic_smp_redirect_cpu(int target_cpu, void *msg)
return smp_call_function_single_async(target_cpu, &panic_csd);
}
+ #ifndef CONFIG_WEAKEN_IGNORE_PANIC
/**
* panic_try_force_cpu - Redirect panic to a specific CPU for crash kernel
* @fmt: panic message format string
@@ -441,12 +446,15 @@ static bool panic_try_force_cpu(const char *fmt, va_list args)
/* IPI/NMI sent, this CPU should stop */
return true;
}
+ #endif /* CONFIG_WEAKEN_IGNORE_PANIC */
#else
+ #ifndef CONFIG_WEAKEN_IGNORE_PANIC
__printf(1, 0)
static inline bool panic_try_force_cpu(const char *fmt, va_list args)
{
return false;
}
+ #endif /* CONFIG_WEAKEN_IGNORE_PANIC */
#endif /* CONFIG_SMP && CONFIG_CRASH_DUMP */
bool panic_try_start(void)
@@ -530,6 +538,8 @@ void check_panic_on_warn(const char *origin)
origin, limit);
}
+#ifndef CONFIG_WEAKEN_IGNORE_PANIC
+
static void panic_trigger_all_cpu_backtrace(void)
{
/* Temporary allow non-panic CPUs to write their backtraces. */