Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
68ef873
Documentation
LICENSES
arch
block
certs
crypto
drivers
accessibility
acpi
amba
android
ata
atm
auxdisplay
base
bcma
block
bluetooth
bus
cdrom
char
clk
clocksource
connector
counter
cpufreq
cpuidle
crypto
cxl
dax
dca
devfreq
dio
dma-buf
dma
edac
eisa
extcon
firewire
firmware
fpga
fsi
gnss
gpio
gpu
greybus
hid
hsi
hv
hwmon
hwspinlock
hwtracing
i2c
i3c
ide
idle
iio
infiniband
input
interconnect
iommu
ipack
irqchip
isdn
leds
lightnvm
macintosh
mailbox
mcb
md
media
memory
memstick
message
mfd
misc
altera-stapl
bcm-vk
c2port
cardreader
cb710
cxl
echo
eeprom
genwqe
habanalabs
ibmasm
lis3lv02d
lkdtm
Makefile
bugs.c
cfi.c
core.c
fortify.c
heap.c
lkdtm.h
perms.c
powerpc.c
refcount.c
rodata.c
stackleak.c
usercopy.c
mei
ocxl
sgi-gru
sgi-xp
ti-st
uacce
vmw_vmci
Kconfig
Makefile
ad525x_dpot-i2c.c
ad525x_dpot-spi.c
ad525x_dpot.c
ad525x_dpot.h
apds9802als.c
apds990x.c
atmel-ssc.c
bh1770glc.c
cs5535-mfgpt.c
ds1682.c
dummy-irq.c
enclosure.c
fastrpc.c
hisi_hikey_usb.c
hmc6352.c
hpilo.c
hpilo.h
ibmvmc.c
ibmvmc.h
ics932s401.c
isl29003.c
isl29020.c
kgdbts.c
lattice-ecp3-config.c
pch_phub.c
pci_endpoint_test.c
phantom.c
pvpanic.c
qcom-coincell.c
sram-exec.c
sram.c
sram.h
tifm_7xx1.c
tifm_core.c
tsl2550.c
vmw_balloon.c
xilinx_sdfec.c
mmc
most
mtd
mux
net
nfc
ntb
nubus
nvdimm
nvme
nvmem
of
opp
parisc
parport
pci
pcmcia
perf
phy
pinctrl
platform
pnp
power
powercap
pps
ps3
ptp
pwm
rapidio
ras
regulator
remoteproc
reset
rpmsg
rtc
s390
sbus
scsi
sh
siox
slimbus
soc
soundwire
spi
spmi
ssb
staging
target
tc
tee
thermal
thunderbolt
tty
uio
usb
vdpa
vfio
vhost
video
virt
virtio
visorbus
vlynq
vme
w1
watchdog
xen
zorro
Kconfig
Makefile
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
drivers
/
misc
/
lkdtm
/
bugs.c
Blame
Blame
Latest commit
History
History
551 lines (459 loc) · 13.2 KB
Breadcrumbs
linux
/
drivers
/
misc
/
lkdtm
/
bugs.c
Top
File metadata and controls
Code
Blame
551 lines (459 loc) · 13.2 KB
Raw
// SPDX-License-Identifier: GPL-2.0 /* * This is for all the tests related to logic bugs (e.g. bad dereferences, * bad alignment, bad loops, bad locking, bad scheduling, deep stacks, and * lockups) along with other things that don't fit well into existing LKDTM * test source files. */ #include "lkdtm.h" #include <linux/list.h> #include <linux/sched.h> #include <linux/sched/signal.h> #include <linux/sched/task_stack.h> #include <linux/uaccess.h> #include <linux/slab.h> #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML) #include <asm/desc.h> #endif struct lkdtm_list { struct list_head node; }; /* * Make sure our attempts to over run the kernel stack doesn't trigger * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we * recurse past the end of THREAD_SIZE by default. */ #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0) #define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2) #else #define REC_STACK_SIZE (THREAD_SIZE / 8) #endif #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2) static int recur_count = REC_NUM_DEFAULT; static DEFINE_SPINLOCK(lock_me_up); /* * Make sure compiler does not optimize this function or stack frame away: * - function marked noinline * - stack variables are marked volatile * - stack variables are written (memset()) and read (pr_info()) * - function has external effects (pr_info()) * */ static int noinline recursive_loop(int remaining) { volatile char buf[REC_STACK_SIZE]; memset((void *)buf, remaining & 0xFF, sizeof(buf)); pr_info("loop %d/%d ...\n", (int)buf[remaining % sizeof(buf)], recur_count); if (!remaining) return 0; else return recursive_loop(remaining - 1); } /* If the depth is negative, use the default, otherwise keep parameter. */ void __init lkdtm_bugs_init(int *recur_param) { if (*recur_param < 0) *recur_param = recur_count; else recur_count = *recur_param; } void lkdtm_PANIC(void) { panic("dumptest"); } void lkdtm_BUG(void) { BUG(); } static int warn_counter; void lkdtm_WARNING(void) { WARN_ON(++warn_counter); } void lkdtm_WARNING_MESSAGE(void) { WARN(1, "Warning message trigger count: %d\n", ++warn_counter); } void lkdtm_EXCEPTION(void) { *((volatile int *) 0) = 0; } void lkdtm_LOOP(void) { for (;;) ; } void lkdtm_EXHAUST_STACK(void) { pr_info("Calling function with %lu frame size to depth %d ...\n", REC_STACK_SIZE, recur_count); recursive_loop(recur_count); pr_info("FAIL: survived without exhausting stack?!\n"); } static noinline void __lkdtm_CORRUPT_STACK(void *stack) { memset(stack, '\xff', 64); } /* This should trip the stack canary, not corrupt the return address. */ noinline void lkdtm_CORRUPT_STACK(void) { /* Use default char array length that triggers stack protection. */ char data[8] __aligned(sizeof(void *)); pr_info("Corrupting stack containing char array ...\n"); __lkdtm_CORRUPT_STACK((void *)&data); } /* Same as above but will only get a canary with -fstack-protector-strong */ noinline void lkdtm_CORRUPT_STACK_STRONG(void) { union { unsigned short shorts[4]; unsigned long *ptr; } data __aligned(sizeof(void *)); pr_info("Corrupting stack containing union ...\n"); __lkdtm_CORRUPT_STACK((void *)&data); } static pid_t stack_pid; static unsigned long stack_addr; void lkdtm_REPORT_STACK(void) { volatile uintptr_t magic; pid_t pid = task_pid_nr(current); if (pid != stack_pid) { pr_info("Starting stack offset tracking for pid %d\n", pid); stack_pid = pid; stack_addr = (uintptr_t)&magic; } pr_info("Stack offset: %d\n", (int)(stack_addr - (uintptr_t)&magic)); } void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void) { static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5}; u32 *p; u32 val = 0x12345678; p = (u32 *)(data + 1); if (*p == 0) val = 0x87654321; *p = val; } void lkdtm_SOFTLOCKUP(void) { preempt_disable(); for (;;) cpu_relax(); } void lkdtm_HARDLOCKUP(void) { local_irq_disable(); for (;;) cpu_relax(); } void lkdtm_SPINLOCKUP(void) { /* Must be called twice to trigger. */ spin_lock(&lock_me_up); /* Let sparse know we intended to exit holding the lock. */ __release(&lock_me_up); } void lkdtm_HUNG_TASK(void) { set_current_state(TASK_UNINTERRUPTIBLE); schedule(); } volatile unsigned int huge = INT_MAX - 2; volatile unsigned int ignored; void lkdtm_OVERFLOW_SIGNED(void) { int value; value = huge; pr_info("Normal signed addition ...\n"); value += 1; ignored = value; pr_info("Overflowing signed addition ...\n"); value += 4; ignored = value; } void lkdtm_OVERFLOW_UNSIGNED(void) { unsigned int value; value = huge; pr_info("Normal unsigned addition ...\n"); value += 1; ignored = value; pr_info("Overflowing unsigned addition ...\n"); value += 4; ignored = value; } /* Intentionally using old-style flex array definition of 1 byte. */ struct array_bounds_flex_array { int one; int two; char data[1]; }; struct array_bounds { int one; int two; char data[8]; int three; }; void lkdtm_ARRAY_BOUNDS(void) { struct array_bounds_flex_array *not_checked; struct array_bounds *checked; volatile int i; not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL); checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL); pr_info("Array access within bounds ...\n"); /* For both, touch all bytes in the actual member size. */ for (i = 0; i < sizeof(checked->data); i++) checked->data[i] = 'A'; /* * For the uninstrumented flex array member, also touch 1 byte * beyond to verify it is correctly uninstrumented. */ for (i = 0; i < sizeof(not_checked->data) + 1; i++) not_checked->data[i] = 'A'; pr_info("Array access beyond bounds ...\n"); for (i = 0; i < sizeof(checked->data) + 1; i++) checked->data[i] = 'B'; kfree(not_checked); kfree(checked); pr_err("FAIL: survived array bounds overflow!\n"); } void lkdtm_CORRUPT_LIST_ADD(void) { /* * Initially, an empty list via LIST_HEAD: * test_head.next = &test_head * test_head.prev = &test_head */ LIST_HEAD(test_head); struct lkdtm_list good, bad; void *target[2] = { }; void *redirection = ⌖ pr_info("attempting good list addition\n"); /* * Adding to the list performs these actions: * test_head.next->prev = &good.node * good.node.next = test_head.next * good.node.prev = test_head * test_head.next = good.node */ list_add(&good.node, &test_head); pr_info("attempting corrupted list addition\n"); /* * In simulating this "write what where" primitive, the "what" is * the address of &bad.node, and the "where" is the address held * by "redirection". */ test_head.next = redirection; list_add(&bad.node, &test_head); if (target[0] == NULL && target[1] == NULL) pr_err("Overwrite did not happen, but no BUG?!\n"); else pr_err("list_add() corruption not detected!\n"); } void lkdtm_CORRUPT_LIST_DEL(void) { LIST_HEAD(test_head); struct lkdtm_list item; void *target[2] = { }; void *redirection = ⌖ list_add(&item.node, &test_head); pr_info("attempting good list removal\n"); list_del(&item.node); pr_info("attempting corrupted list removal\n"); list_add(&item.node, &test_head); /* As with the list_add() test above, this corrupts "next". */ item.node.next = redirection; list_del(&item.node); if (target[0] == NULL && target[1] == NULL) pr_err("Overwrite did not happen, but no BUG?!\n"); else pr_err("list_del() corruption not detected!\n"); } /* Test that VMAP_STACK is actually allocating with a leading guard page */ void lkdtm_STACK_GUARD_PAGE_LEADING(void) { const unsigned char *stack = task_stack_page(current); const unsigned char *ptr = stack - 1; volatile unsigned char byte; pr_info("attempting bad read from page below current stack\n"); byte = *ptr; pr_err("FAIL: accessed page before stack! (byte: %x)\n", byte); } /* Test that VMAP_STACK is actually allocating with a trailing guard page */ void lkdtm_STACK_GUARD_PAGE_TRAILING(void) { const unsigned char *stack = task_stack_page(current); const unsigned char *ptr = stack + THREAD_SIZE; volatile unsigned char byte; pr_info("attempting bad read from page above current stack\n"); byte = *ptr; pr_err("FAIL: accessed page after stack! (byte: %x)\n", byte); } void lkdtm_UNSET_SMEP(void) { #if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML) #define MOV_CR4_DEPTH 64 void (*direct_write_cr4)(unsigned long val); unsigned char *insn; unsigned long cr4; int i; cr4 = native_read_cr4(); if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) { pr_err("FAIL: SMEP not in use\n"); return; } cr4 &= ~(X86_CR4_SMEP); pr_info("trying to clear SMEP normally\n"); native_write_cr4(cr4); if (cr4 == native_read_cr4()) { pr_err("FAIL: pinning SMEP failed!\n"); cr4 |= X86_CR4_SMEP; pr_info("restoring SMEP\n"); native_write_cr4(cr4); return; } pr_info("ok: SMEP did not get cleared\n"); /* * To test the post-write pinning verification we need to call * directly into the middle of native_write_cr4() where the * cr4 write happens, skipping any pinning. This searches for * the cr4 writing instruction. */ insn = (unsigned char *)native_write_cr4; for (i = 0; i < MOV_CR4_DEPTH; i++) { /* mov %rdi, %cr4 */ if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7) break; /* mov %rdi,%rax; mov %rax, %cr4 */ if (insn[i] == 0x48 && insn[i+1] == 0x89 && insn[i+2] == 0xf8 && insn[i+3] == 0x0f && insn[i+4] == 0x22 && insn[i+5] == 0xe0) break; } if (i >= MOV_CR4_DEPTH) { pr_info("ok: cannot locate cr4 writing call gadget\n"); return; } direct_write_cr4 = (void *)(insn + i); pr_info("trying to clear SMEP with call gadget\n"); direct_write_cr4(cr4); if (native_read_cr4() & X86_CR4_SMEP) { pr_info("ok: SMEP removal was reverted\n"); } else { pr_err("FAIL: cleared SMEP not detected!\n"); cr4 |= X86_CR4_SMEP; pr_info("restoring SMEP\n"); native_write_cr4(cr4); } #else pr_err("XFAIL: this test is x86_64-only\n"); #endif } void lkdtm_DOUBLE_FAULT(void) { #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML) /* * Trigger #DF by setting the stack limit to zero. This clobbers * a GDT TLS slot, which is okay because the current task will die * anyway due to the double fault. */ struct desc_struct d = { .type = 3, /* expand-up, writable, accessed data */ .p = 1, /* present */ .d = 1, /* 32-bit */ .g = 0, /* limit in bytes */ .s = 1, /* not system */ }; local_irq_disable(); write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()), GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S); /* * Put our zero-limit segment in SS and then trigger a fault. The * 4-byte access to (%esp) will fault with #SS, and the attempt to * deliver the fault will recursively cause #SS and result in #DF. * This whole process happens while NMIs and MCEs are blocked by the * MOV SS window. This is nice because an NMI with an invalid SS * would also double-fault, resulting in the NMI or MCE being lost. */ asm volatile ("movw %0, %%ss; addl $0, (%%esp)" :: "r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3))); pr_err("FAIL: tried to double fault but didn't die\n"); #else pr_err("XFAIL: this test is ia32-only\n"); #endif } #ifdef CONFIG_ARM64 static noinline void change_pac_parameters(void) { if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH)) { /* Reset the keys of current task */ ptrauth_thread_init_kernel(current); ptrauth_thread_switch_kernel(current); } } #endif noinline void lkdtm_CORRUPT_PAC(void) { #ifdef CONFIG_ARM64 #define CORRUPT_PAC_ITERATE 10 int i; if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH)) pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH\n"); if (!system_supports_address_auth()) { pr_err("FAIL: CPU lacks pointer authentication feature\n"); return; } pr_info("changing PAC parameters to force function return failure...\n"); /* * PAC is a hash value computed from input keys, return address and * stack pointer. As pac has fewer bits so there is a chance of * collision, so iterate few times to reduce the collision probability. */ for (i = 0; i < CORRUPT_PAC_ITERATE; i++) change_pac_parameters(); pr_err("FAIL: survived PAC changes! Kernel may be unstable from here\n"); #else pr_err("XFAIL: this test is arm64-only\n"); #endif } void lkdtm_FORTIFY_OBJECT(void) { struct target { char a[10]; } target[2] = {}; int result; /* * Using volatile prevents the compiler from determining the value of * 'size' at compile time. Without that, we would get a compile error * rather than a runtime error. */ volatile int size = 11; pr_info("trying to read past the end of a struct\n"); result = memcmp(&target[0], &target[1], size); /* Print result to prevent the code from being eliminated */ pr_err("FAIL: fortify did not catch an object overread!\n" "\"%d\" was the memcmp result.\n", result); } void lkdtm_FORTIFY_SUBOBJECT(void) { struct target { char a[10]; char b[10]; } target; char *src; src = kmalloc(20, GFP_KERNEL); strscpy(src, "over ten bytes", 20); pr_info("trying to strcpy past the end of a member of a struct\n"); /* * strncpy(target.a, src, 20); will hit a compile error because the * compiler knows at build time that target.a < 20 bytes. Use strcpy() * to force a runtime error. */ strcpy(target.a, src); /* Use target.a to prevent the code from being eliminated */ pr_err("FAIL: fortify did not catch an sub-object overrun!\n" "\"%s\" was copied.\n", target.a); kfree(src); }
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
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
You can’t perform that action at this time.