1 // SPDX-License-Identifier: GPL-2.0
3 * This is for all the tests relating directly to Control Flow Integrity.
8 static int called_count;
10 /* Function taking one argument, without a return value. */
11 static noinline void lkdtm_increment_void(int *counter)
16 /* Function taking one argument, returning int. */
17 static noinline int lkdtm_increment_int(int *counter)
24 /* Don't allow the compiler to inline the calls. */
25 static noinline void lkdtm_indirect_call(void (*func)(int *))
31 * This tries to call an indirect function with a mismatched prototype.
33 static void lkdtm_CFI_FORWARD_PROTO(void)
36 * Matches lkdtm_increment_void()'s prototype, but not
37 * lkdtm_increment_int()'s prototype.
39 pr_info("Calling matched prototype ...\n");
40 lkdtm_indirect_call(lkdtm_increment_void);
42 pr_info("Calling mismatched prototype ...\n");
43 lkdtm_indirect_call((void *)lkdtm_increment_int);
45 pr_err("FAIL: survived mismatched prototype function call!\n");
46 pr_expected_config(CONFIG_CFI_CLANG);
50 * This can stay local to LKDTM, as there should not be a production reason
51 * to disable PAC && SCS.
53 #ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
54 # ifdef CONFIG_ARM64_BTI_KERNEL
55 # define __no_pac "branch-protection=bti"
57 # ifdef CONFIG_CC_HAS_BRANCH_PROT_PAC_RET
58 # define __no_pac "branch-protection=none"
60 # define __no_pac "sign-return-address=none"
63 # define __no_ret_protection __noscs __attribute__((__target__(__no_pac)))
65 # define __no_ret_protection __noscs
68 #define no_pac_addr(addr) \
69 ((__force __typeof__(addr))((uintptr_t)(addr) | PAGE_OFFSET))
71 /* The ultimate ROP gadget. */
72 static noinline __no_ret_protection
73 void set_return_addr_unchecked(unsigned long *expected, unsigned long *addr)
75 /* Use of volatile is to make sure final write isn't seen as a dead store. */
76 unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1;
78 /* Make sure we've found the right place on the stack before writing it. */
79 if (no_pac_addr(*ret_addr) == expected)
82 /* Check architecture, stack layout, or compiler behavior... */
83 pr_warn("Eek: return address mismatch! %px != %px\n",
88 void set_return_addr(unsigned long *expected, unsigned long *addr)
90 /* Use of volatile is to make sure final write isn't seen as a dead store. */
91 unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1;
93 /* Make sure we've found the right place on the stack before writing it. */
94 if (no_pac_addr(*ret_addr) == expected)
97 /* Check architecture, stack layout, or compiler behavior... */
98 pr_warn("Eek: return address mismatch! %px != %px\n",
102 static volatile int force_check;
104 static void lkdtm_CFI_BACKWARD(void)
106 /* Use calculated gotos to keep labels addressable. */
107 void *labels[] = { NULL, &&normal, &&redirected, &&check_normal, &&check_redirected };
109 pr_info("Attempting unchecked stack return address redirection ...\n");
114 * Prepare to call with NULLs to avoid parameters being treated as
117 set_return_addr_unchecked(NULL, NULL);
118 set_return_addr(NULL, NULL);
131 * Use fallthrough switch case to keep basic block ordering between
132 * set_return_addr*() and the label after it.
134 switch (force_check) {
136 set_return_addr_unchecked(&&normal, &&redirected);
142 pr_err("FAIL: stack return address manipulation failed!\n");
143 /* If we can't redirect "normally", we can't test mitigations. */
149 pr_info("ok: redirected stack return address.\n");
153 pr_info("Attempting checked stack return address redirection ...\n");
155 switch (force_check) {
157 set_return_addr(&&check_normal, &&check_redirected);
163 pr_info("ok: control flow unchanged.\n");
168 pr_err("FAIL: stack return address was redirected!\n");
172 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) {
173 pr_expected_config(CONFIG_ARM64_PTR_AUTH_KERNEL);
176 if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
177 pr_expected_config(CONFIG_SHADOW_CALL_STACK);
180 pr_warn("This is probably expected, since this %s was built *without* %s=y nor %s=y\n",
182 "CONFIG_ARM64_PTR_AUTH_KERNEL", "CONFIG_SHADOW_CALL_STACK");
185 static struct crashtype crashtypes[] = {
186 CRASHTYPE(CFI_FORWARD_PROTO),
187 CRASHTYPE(CFI_BACKWARD),
190 struct crashtype_category cfi_crashtypes = {
191 .crashtypes = crashtypes,
192 .len = ARRAY_SIZE(crashtypes),