Skip to content

Commit e74e1d5

Browse files
loosper-armwilldeacon
authored andcommitted
kselftests/arm64: add a basic Pointer Authentication test
PAuth signs and verifies return addresses on the stack. It does so by inserting a Pointer Authentication code (PAC) into some of the unused top bits of an address. This is achieved by adding paciasp/autiasp instructions at the beginning and end of a function. This feature is partially backwards compatible with earlier versions of the ARM architecture. To coerce the compiler into emitting fully backwards compatible code the main file is compiled to target an earlier ARM version. This allows the tests to check for the feature and print meaningful error messages instead of crashing. Add a test to verify that corrupting the return address results in a SIGSEGV on return. 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-2-boian4o1@gmail.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent f75aef3 commit e74e1d5

6 files changed

Lines changed: 106 additions & 1 deletion

File tree

tools/testing/selftests/arm64/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
ARCH ?= $(shell uname -m 2>/dev/null || echo not)
55

66
ifneq (,$(filter $(ARCH),aarch64 arm64))
7-
ARM64_SUBTARGETS ?= tags signal
7+
ARM64_SUBTARGETS ?= tags signal pauth
88
else
99
ARM64_SUBTARGETS :=
1010
endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pac
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# Copyright (C) 2020 ARM Limited
3+
4+
# preserve CC value from top level Makefile
5+
ifeq ($(CC),cc)
6+
CC := $(CROSS_COMPILE)gcc
7+
endif
8+
9+
CFLAGS += -mbranch-protection=pac-ret
10+
# check if the compiler supports ARMv8.3 and branch protection with PAuth
11+
pauth_cc_support := $(shell if ($(CC) $(CFLAGS) -march=armv8.3-a -E -x c /dev/null -o /dev/null 2>&1) then echo "1"; fi)
12+
13+
ifeq ($(pauth_cc_support),1)
14+
TEST_GEN_PROGS := pac
15+
TEST_GEN_FILES := pac_corruptor.o
16+
endif
17+
18+
include ../../lib.mk
19+
20+
ifeq ($(pauth_cc_support),1)
21+
# pac* and aut* instructions are not available on architectures berfore
22+
# ARMv8.3. Therefore target ARMv8.3 wherever they are used directly
23+
$(OUTPUT)/pac_corruptor.o: pac_corruptor.S
24+
$(CC) -c $^ -o $@ $(CFLAGS) -march=armv8.3-a
25+
26+
# when -mbranch-protection is enabled and the target architecture is ARMv8.3 or
27+
# greater, gcc emits pac* instructions which are not in HINT NOP space,
28+
# preventing the tests from occurring at all. Compile for ARMv8.2 so tests can
29+
# run on earlier targets and print a meaningful error messages
30+
$(OUTPUT)/pac: pac.c $(OUTPUT)/pac_corruptor.o
31+
$(CC) $^ -o $@ $(CFLAGS) -march=armv8.2-a
32+
endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (C) 2020 ARM Limited */
3+
4+
#ifndef _HELPER_H_
5+
#define _HELPER_H_
6+
7+
void pac_corruptor(void);
8+
9+
#endif
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2020 ARM Limited
3+
4+
#include <sys/auxv.h>
5+
#include <signal.h>
6+
#include <setjmp.h>
7+
8+
#include "../../kselftest_harness.h"
9+
#include "helper.h"
10+
11+
#define ASSERT_PAUTH_ENABLED() \
12+
do { \
13+
unsigned long hwcaps = getauxval(AT_HWCAP); \
14+
/* data key instructions are not in NOP space. This prevents a SIGILL */ \
15+
ASSERT_NE(0, hwcaps & HWCAP_PACA) TH_LOG("PAUTH not enabled"); \
16+
} while (0)
17+
18+
sigjmp_buf jmpbuf;
19+
void pac_signal_handler(int signum, siginfo_t *si, void *uc)
20+
{
21+
if (signum == SIGSEGV || signum == SIGILL)
22+
siglongjmp(jmpbuf, 1);
23+
}
24+
25+
/* check that a corrupted PAC results in SIGSEGV or SIGILL */
26+
TEST(corrupt_pac)
27+
{
28+
struct sigaction sa;
29+
30+
ASSERT_PAUTH_ENABLED();
31+
if (sigsetjmp(jmpbuf, 1) == 0) {
32+
sa.sa_sigaction = pac_signal_handler;
33+
sa.sa_flags = SA_SIGINFO | SA_RESETHAND;
34+
sigemptyset(&sa.sa_mask);
35+
36+
sigaction(SIGSEGV, &sa, NULL);
37+
sigaction(SIGILL, &sa, NULL);
38+
39+
pac_corruptor();
40+
ASSERT_TRUE(0) TH_LOG("SIGSEGV/SIGILL signal did not occur");
41+
}
42+
}
43+
44+
TEST_HARNESS_MAIN
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (C) 2020 ARM Limited */
3+
4+
.global pac_corruptor
5+
6+
.text
7+
/*
8+
* Corrupting a single bit of the PAC ensures the authentication will fail. It
9+
* also guarantees no possible collision. TCR_EL1.TBI0 is set by default so no
10+
* top byte PAC is tested
11+
*/
12+
pac_corruptor:
13+
paciasp
14+
15+
/* corrupt the top bit of the PAC */
16+
eor lr, lr, #1 << 53
17+
18+
autiasp
19+
ret

0 commit comments

Comments
 (0)