Skip to content

Commit 43962de

Browse files
wearyzenhenrikbrixandersen
authored andcommitted
tests: arm: validate PAC keys on context switch
Test PAC by corrupting lr from a userspace thread. Signed-off-by: Sudan Landge <[email protected]>
1 parent 7660136 commit 43962de

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

tests/arch/arm/arm_pacbti/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ CONFIG_ZTEST_FATAL_HOOK=y
44
CONFIG_TEST_RANDOM_GENERATOR=y
55
CONFIG_TIMER_RANDOM_INITIAL_STATE=123456789
66
CONFIG_TIMER_RANDOM_GENERATOR=y
7+
8+
CONFIG_IDLE_STACK_SIZE=1024
9+
CONFIG_ISR_STACK_SIZE=1024

tests/arch/arm/arm_pacbti/src/main.c

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
#include <zephyr/ztest_error_hook.h>
99
#include <cmsis_core.h>
1010

11+
/* size of stack area used by each thread */
12+
#define STACKSIZE 1024
13+
14+
/* scheduling priority used by each thread */
15+
#define PRIORITY 7
16+
17+
/* number of times to check if PAC keys were retained */
18+
#define NUM_TRIALS 5
19+
20+
K_THREAD_STACK_DEFINE(pac_test_thread_stack_area, STACKSIZE);
21+
static struct k_thread pac_test_thread;
22+
1123
void test_arm_pacbti(void)
1224
{
1325
printf("%s This should never have been called if BTI was enforced\n", __func__);
@@ -19,18 +31,18 @@ void test_arm_pacbti(void)
1931
/* Without PAC this function would have returned to test_arm_pacbti() but with PAC enabled
2032
* the AUT instruction should result in a USAGE FAULT since the `lr` was corrupted on stack.
2133
*/
22-
__asm__ (".thumb\n"
23-
".thumb_func\n"
24-
".global corrupt_lr_on_stack\n"
25-
"corrupt_lr_on_stack:\n"
26-
" pacbti r12, lr, sp\n"
27-
" stmdb sp!, {ip, lr}\n"
28-
" ldr r0,=test_arm_pacbti\n"
29-
" str r0, [sp, #4]\n"
30-
" ldmia.w sp!, {ip, lr}\n"
31-
" aut r12, lr, sp\n"
32-
" bx lr\n");
33-
void corrupt_lr_on_stack();
34+
__asm__(".thumb\n"
35+
".thumb_func\n"
36+
".global corrupt_lr_on_stack\n"
37+
"corrupt_lr_on_stack:\n"
38+
" pacbti r12, lr, sp\n"
39+
" stmdb sp!, {ip, lr}\n"
40+
" ldr r0,=test_arm_pacbti\n"
41+
" str r0, [sp, #4]\n"
42+
" ldmia.w sp!, {ip, lr}\n"
43+
" aut r12, lr, sp\n"
44+
" bx lr\n");
45+
void corrupt_lr_on_stack(void);
3446

3547
static int set_invalid_pac_key(void)
3648
{
@@ -50,6 +62,23 @@ static int set_invalid_pac_key(void)
5062
return 1;
5163
}
5264

65+
static void pac_test_thread_entry_point(void *dummy1, void *dummy2, void *dummy3)
66+
{
67+
ztest_set_fault_valid(true);
68+
69+
corrupt_lr_on_stack();
70+
}
71+
72+
ZTEST(arm_pacbti, test_arm_pac_corrupt_lr_in_userspace)
73+
{
74+
k_thread_create(&pac_test_thread, pac_test_thread_stack_area,
75+
K_THREAD_STACK_SIZEOF(pac_test_thread_stack_area),
76+
pac_test_thread_entry_point, NULL, NULL, NULL, PRIORITY, K_USER, K_FOREVER);
77+
78+
k_thread_start(&pac_test_thread);
79+
k_thread_join(&pac_test_thread, K_FOREVER);
80+
}
81+
5382
ZTEST(arm_pacbti, test_arm_pac_corrupt_lr)
5483
{
5584
ztest_set_fault_valid(true);

tests/arch/arm/arm_pacbti/testcase.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ tests:
1313
extra_configs:
1414
- CONFIG_ARM_PACBTI_STANDARD=y
1515
- CONFIG_ARM_PAC_PER_THREAD=y
16+
- CONFIG_USERSPACE=y

0 commit comments

Comments
 (0)