Skip to content

Commit 766d95b

Browse files
loosper-armwilldeacon
authored andcommitted
kselftests/arm64: add nop checks for PAuth tests
PAuth adds sign/verify controls to enable and disable groups of instructions in hardware for compatibility with libraries that do not implement PAuth. The kernel always enables them if it detects PAuth. Add a test that checks that each group of instructions is enabled, if the kernel reports PAuth as detected. Note: For groups, for the purpose of this patch, we intend instructions that use a certain key. Signed-off-by: Boyan Karatotev <boyan.karatotev@arm.com> Reviewed-by: Vincenzo Frascino <Vincenzo.Frascino@arm.com> Reviewed-by: Amit Daniel Kachhap <amit.kachhap@arm.com> Acked-by: Shuah Khan <skhan@linuxfoundation.org> Cc: Shuah Khan <shuah@kernel.org> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20200918104715.182310-3-boian4o1@gmail.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent e74e1d5 commit 766d95b

5 files changed

Lines changed: 105 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
exec_target
12
pac

tools/testing/selftests/arm64/pauth/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pauth_cc_support := $(shell if ($(CC) $(CFLAGS) -march=armv8.3-a -E -x c /dev/nu
1212

1313
ifeq ($(pauth_cc_support),1)
1414
TEST_GEN_PROGS := pac
15-
TEST_GEN_FILES := pac_corruptor.o
15+
TEST_GEN_FILES := pac_corruptor.o helper.o
1616
endif
1717

1818
include ../../lib.mk
@@ -23,10 +23,13 @@ ifeq ($(pauth_cc_support),1)
2323
$(OUTPUT)/pac_corruptor.o: pac_corruptor.S
2424
$(CC) -c $^ -o $@ $(CFLAGS) -march=armv8.3-a
2525

26+
$(OUTPUT)/helper.o: helper.c
27+
$(CC) -c $^ -o $@ $(CFLAGS) -march=armv8.3-a
28+
2629
# when -mbranch-protection is enabled and the target architecture is ARMv8.3 or
2730
# greater, gcc emits pac* instructions which are not in HINT NOP space,
2831
# preventing the tests from occurring at all. Compile for ARMv8.2 so tests can
2932
# run on earlier targets and print a meaningful error messages
30-
$(OUTPUT)/pac: pac.c $(OUTPUT)/pac_corruptor.o
33+
$(OUTPUT)/pac: pac.c $(OUTPUT)/pac_corruptor.o $(OUTPUT)/helper.o
3134
$(CC) $^ -o $@ $(CFLAGS) -march=armv8.2-a
3235
endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2020 ARM Limited
3+
4+
#include "helper.h"
5+
6+
size_t keyia_sign(size_t ptr)
7+
{
8+
asm volatile("paciza %0" : "+r" (ptr));
9+
return ptr;
10+
}
11+
12+
size_t keyib_sign(size_t ptr)
13+
{
14+
asm volatile("pacizb %0" : "+r" (ptr));
15+
return ptr;
16+
}
17+
18+
size_t keyda_sign(size_t ptr)
19+
{
20+
asm volatile("pacdza %0" : "+r" (ptr));
21+
return ptr;
22+
}
23+
24+
size_t keydb_sign(size_t ptr)
25+
{
26+
asm volatile("pacdzb %0" : "+r" (ptr));
27+
return ptr;
28+
}
29+
30+
size_t keyg_sign(size_t ptr)
31+
{
32+
/* output is encoded in the upper 32 bits */
33+
size_t dest = 0;
34+
size_t modifier = 0;
35+
36+
asm volatile("pacga %0, %1, %2" : "=r" (dest) : "r" (ptr), "r" (modifier));
37+
38+
return dest;
39+
}

tools/testing/selftests/arm64/pauth/helper.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
#ifndef _HELPER_H_
55
#define _HELPER_H_
66

7+
#include <stdlib.h>
8+
79
void pac_corruptor(void);
810

11+
/* PAuth sign a value with key ia and modifier value 0 */
12+
size_t keyia_sign(size_t val);
13+
size_t keyib_sign(size_t val);
14+
size_t keyda_sign(size_t val);
15+
size_t keydb_sign(size_t val);
16+
size_t keyg_sign(size_t val);
17+
918
#endif

tools/testing/selftests/arm64/pauth/pac.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,25 @@
88
#include "../../kselftest_harness.h"
99
#include "helper.h"
1010

11+
#define PAC_COLLISION_ATTEMPTS 10
12+
/*
13+
* The kernel sets TBID by default. So bits 55 and above should remain
14+
* untouched no matter what.
15+
* The VA space size is 48 bits. Bigger is opt-in.
16+
*/
17+
#define PAC_MASK (~0xff80ffffffffffff)
1118
#define ASSERT_PAUTH_ENABLED() \
1219
do { \
1320
unsigned long hwcaps = getauxval(AT_HWCAP); \
1421
/* data key instructions are not in NOP space. This prevents a SIGILL */ \
1522
ASSERT_NE(0, hwcaps & HWCAP_PACA) TH_LOG("PAUTH not enabled"); \
1623
} while (0)
24+
#define ASSERT_GENERIC_PAUTH_ENABLED() \
25+
do { \
26+
unsigned long hwcaps = getauxval(AT_HWCAP); \
27+
/* generic key instructions are not in NOP space. This prevents a SIGILL */ \
28+
ASSERT_NE(0, hwcaps & HWCAP_PACG) TH_LOG("Generic PAUTH not enabled"); \
29+
} while (0)
1730

1831
sigjmp_buf jmpbuf;
1932
void pac_signal_handler(int signum, siginfo_t *si, void *uc)
@@ -41,4 +54,42 @@ TEST(corrupt_pac)
4154
}
4255
}
4356

57+
/*
58+
* There are no separate pac* and aut* controls so checking only the pac*
59+
* instructions is sufficient
60+
*/
61+
TEST(pac_instructions_not_nop)
62+
{
63+
size_t keyia = 0;
64+
size_t keyib = 0;
65+
size_t keyda = 0;
66+
size_t keydb = 0;
67+
68+
ASSERT_PAUTH_ENABLED();
69+
70+
for (int i = 0; i < PAC_COLLISION_ATTEMPTS; i++) {
71+
keyia |= keyia_sign(i) & PAC_MASK;
72+
keyib |= keyib_sign(i) & PAC_MASK;
73+
keyda |= keyda_sign(i) & PAC_MASK;
74+
keydb |= keydb_sign(i) & PAC_MASK;
75+
}
76+
77+
ASSERT_NE(0, keyia) TH_LOG("keyia instructions did nothing");
78+
ASSERT_NE(0, keyib) TH_LOG("keyib instructions did nothing");
79+
ASSERT_NE(0, keyda) TH_LOG("keyda instructions did nothing");
80+
ASSERT_NE(0, keydb) TH_LOG("keydb instructions did nothing");
81+
}
82+
83+
TEST(pac_instructions_not_nop_generic)
84+
{
85+
size_t keyg = 0;
86+
87+
ASSERT_GENERIC_PAUTH_ENABLED();
88+
89+
for (int i = 0; i < PAC_COLLISION_ATTEMPTS; i++)
90+
keyg |= keyg_sign(i) & PAC_MASK;
91+
92+
ASSERT_NE(0, keyg) TH_LOG("keyg instructions did nothing");
93+
}
94+
4495
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)