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 # define __no_pac "branch-protection=none"
59 # define __no_ret_protection __noscs __attribute__((__target__(__no_pac)))
61 # define __no_ret_protection __noscs
64 #define no_pac_addr(addr) \
65 ((__force __typeof__(addr))((uintptr_t)(addr) | PAGE_OFFSET))
67 /* The ultimate ROP gadget. */
68 static noinline __no_ret_protection
69 void set_return_addr_unchecked(unsigned long *expected, unsigned long *addr)
71 /* Use of volatile is to make sure final write isn't seen as a dead store. */
72 unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1;
74 /* Make sure we've found the right place on the stack before writing it. */
75 if (no_pac_addr(*ret_addr) == expected)
78 /* Check architecture, stack layout, or compiler behavior... */
79 pr_warn("Eek: return address mismatch! %px != %px\n",
84 void set_return_addr(unsigned long *expected, unsigned long *addr)
86 /* Use of volatile is to make sure final write isn't seen as a dead store. */
87 unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1;
89 /* Make sure we've found the right place on the stack before writing it. */
90 if (no_pac_addr(*ret_addr) == expected)
93 /* Check architecture, stack layout, or compiler behavior... */
94 pr_warn("Eek: return address mismatch! %px != %px\n",
98 static volatile int force_check;
100 static void lkdtm_CFI_BACKWARD(void)
102 /* Use calculated gotos to keep labels addressable. */
103 void *labels[] = { NULL, &&normal, &&redirected, &&check_normal, &&check_redirected };
105 pr_info("Attempting unchecked stack return address redirection ...\n");
110 * Prepare to call with NULLs to avoid parameters being treated as
113 set_return_addr_unchecked(NULL, NULL);
114 set_return_addr(NULL, NULL);
127 * Use fallthrough switch case to keep basic block ordering between
128 * set_return_addr*() and the label after it.
130 switch (force_check) {
132 set_return_addr_unchecked(&&normal, &&redirected);
138 pr_err("FAIL: stack return address manipulation failed!\n");
139 /* If we can't redirect "normally", we can't test mitigations. */
145 pr_info("ok: redirected stack return address.\n");
149 pr_info("Attempting checked stack return address redirection ...\n");
151 switch (force_check) {
153 set_return_addr(&&check_normal, &&check_redirected);
159 pr_info("ok: control flow unchanged.\n");
164 pr_err("FAIL: stack return address was redirected!\n");
168 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) {
169 pr_expected_config(CONFIG_ARM64_PTR_AUTH_KERNEL);
172 if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
173 pr_expected_config(CONFIG_SHADOW_CALL_STACK);
176 pr_warn("This is probably expected, since this %s was built *without* %s=y nor %s=y\n",
178 "CONFIG_ARM64_PTR_AUTH_KERNEL", "CONFIG_SHADOW_CALL_STACK");
181 static struct crashtype crashtypes[] = {
182 CRASHTYPE(CFI_FORWARD_PROTO),
183 CRASHTYPE(CFI_BACKWARD),
186 struct crashtype_category cfi_crashtypes = {
187 .crashtypes = crashtypes,
188 .len = ARRAY_SIZE(crashtypes),