Skip to contentSkip to conversation

kselftest: arm64: Add BTI tests

PATCH In review Mark Brown 2 participants 2 messages Mar 9, 2021 – Mar 23, 2021

Subsystems (2)
lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/1 kselftest: arm64: Add BTI tests

Add some tests that verify that BTI functions correctly for static binaries built with and without BTI support, verifying that SIGILL is generated when expected and is not generated in other situations.

Since BTI support is still being rolled out in distributions these tests are built entirely free standing, no libc support is used at all so none of the standard helper functions for kselftest can be used and we open code everything. This also means we aren't testing the kernel support for the dynamic linker, though the test program can be readily adapted for that once it becomes something that we can reliably build and run.

These tests were originally written by Dave Martin, I've adapted them for kselftest, mainly around the build system and the output format.

---
 tools/testing/selftests/arm64/Makefile        |   2 +-
 tools/testing/selftests/arm64/bti/.gitignore  |   2 +
 tools/testing/selftests/arm64/bti/Makefile    |  61 +++++
 tools/testing/selftests/arm64/bti/assembler.h |  80 ++++++
 tools/testing/selftests/arm64/bti/btitest.h   |  23 ++
 tools/testing/selftests/arm64/bti/compiler.h  |  21 ++
 .../selftests/arm64/bti/gen/.gitignore        |   2 +
 tools/testing/selftests/arm64/bti/signal.c    |  37 +++
 tools/testing/selftests/arm64/bti/signal.h    |  21 ++
 tools/testing/selftests/arm64/bti/start.S     |  14 ++
 tools/testing/selftests/arm64/bti/syscall.S   |  23 ++
 tools/testing/selftests/arm64/bti/system.c    |  22 ++
 tools/testing/selftests/arm64/bti/system.h    |  28 +++
 tools/testing/selftests/arm64/bti/test.c      | 234 ++++++++++++++++++
 tools/testing/selftests/arm64/bti/teststubs.S |  39 +++
 .../testing/selftests/arm64/bti/trampoline.S  |  29 +++
 16 files changed, 637 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/arm64/bti/.gitignore
 create mode 100644 tools/testing/selftests/arm64/bti/Makefile
 create mode 100644 tools/testing/selftests/arm64/bti/assembler.h
 create mode 100644 tools/testing/selftests/arm64/bti/btitest.h
 create mode 100644 tools/testing/selftests/arm64/bti/compiler.h
 create mode 100644 tools/testing/selftests/arm64/bti/gen/.gitignore
 create mode 100644 tools/testing/selftests/arm64/bti/signal.c
 create mode 100644 tools/testing/selftests/arm64/bti/signal.h
 create mode 100644 tools/testing/selftests/arm64/bti/start.S
 create mode 100644 tools/testing/selftests/arm64/bti/syscall.S
 create mode 100644 tools/testing/selftests/arm64/bti/system.c
 create mode 100644 tools/testing/selftests/arm64/bti/system.h
 create mode 100644 tools/testing/selftests/arm64/bti/test.c
 create mode 100644 tools/testing/selftests/arm64/bti/teststubs.S
 create mode 100644 tools/testing/selftests/arm64/bti/trampoline.S
tools/testing/selftests/arm64/Makefile +1 -1
index 2c9d012797a7..ced910fb4019 100644
@@ -4,7 +4,7 @@
 ARCH ?= $(shell uname -m 2>/dev/null || echo not)
 
 ifneq (,$(filter $(ARCH),aarch64 arm64))
-ARM64_SUBTARGETS ?= tags signal pauth fp mte
+ARM64_SUBTARGETS ?= tags signal pauth fp mte bti
 else
 ARM64_SUBTARGETS :=
 endif
tools/testing/selftests/arm64/bti/.gitignore new file +2 -0
new file mode 100644
index 000000000000..73869fabada4
@@ -0,0 +1,2 @@
+btitest
+nobtitest
tools/testing/selftests/arm64/bti/Makefile new file +61 -0

61 lines not on this page · raw message

tools/testing/selftests/arm64/bti/assembler.h new file +80 -0

80 lines not on this page · raw message

tools/testing/selftests/arm64/bti/btitest.h new file +23 -0
new file mode 100644
index 000000000000..2aff9b10336e
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2019  Arm Limited
+ * Original author: Dave Martin <[email protected]>
+ */
+
+#ifndef BTITEST_H
+#define BTITEST_H
+
+/* Trampolines for calling the test stubs: */
+void call_using_br_x0(void (*)(void));
+void call_using_br_x16(void (*)(void));
+void call_using_blr(void (*)(void));
+
+/* Test stubs: */
+void nohint_func(void);
+void bti_none_func(void);
+void bti_c_func(void);
+void bti_j_func(void);
+void bti_jc_func(void);
+void paciasp_func(void);
+
+#endif /* !BTITEST_H */
tools/testing/selftests/arm64/bti/compiler.h new file +21 -0
new file mode 100644
index 000000000000..ebb6204f447a
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2019  Arm Limited
+ * Original author: Dave Martin <[email protected]>
+ */
+
+#ifndef COMPILER_H
+#define COMPILER_H
+
+#define __always_unused __attribute__((__unused__))
+#define __noreturn __attribute__((__noreturn__))
+#define __unreachable() __builtin_unreachable()
+
+/* curse(e) has value e, but the compiler cannot assume so */
+#define curse(e) ({				\
+	__typeof__(e) __curse_e = (e);		\
+	asm ("" : "+r" (__curse_e));		\
+	__curse_e;				\
+})
+
+#endif /* ! COMPILER_H */
tools/testing/selftests/arm64/bti/gen/.gitignore new file +2 -0
new file mode 100644
index 000000000000..73869fabada4
@@ -0,0 +1,2 @@
+btitest
+nobtitest
tools/testing/selftests/arm64/bti/signal.c new file +37 -0
new file mode 100644
index 000000000000..f3fd29b91141
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019  Arm Limited
+ * Original author: Dave Martin <[email protected]>
+ */
+
+#include "system.h"
+#include "signal.h"
+
+int sigemptyset(sigset_t *s)
+{
+	unsigned int i;
+
+	for (i = 0; i < _NSIG_WORDS; ++i)
+		s->sig[i] = 0;
+
+	return 0;
+}
+
+int sigaddset(sigset_t *s, int n)
+{
+	if (n < 1 || n > _NSIG)
+		return -EINVAL;
+
+	s->sig[(n - 1) / _NSIG_BPW] |= 1UL << (n - 1) % _NSIG_BPW;
+	return 0;
+}
+
+int sigaction(int n, struct sigaction *sa, const struct sigaction *old)
+{
+	return syscall(__NR_rt_sigaction, n, sa, old, sizeof(sa->sa_mask));
+}
+
+int sigprocmask(int how, const sigset_t *mask, sigset_t *old)
+{
+	return syscall(__NR_rt_sigprocmask, how, mask, old, sizeof(*mask));
+}
tools/testing/selftests/arm64/bti/signal.h new file +21 -0
new file mode 100644
index 000000000000..103457dc880e
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2019  Arm Limited
+ * Original author: Dave Martin <[email protected]>
+ */
+
+#ifndef SIGNAL_H
+#define SIGNAL_H
+
+#include <linux/signal.h>
+
+#include "system.h"
+
+typedef __sighandler_t sighandler_t;
+
+int sigemptyset(sigset_t *s);
+int sigaddset(sigset_t *s, int n);
+int sigaction(int n, struct sigaction *sa, const struct sigaction *old);
+int sigprocmask(int how, const sigset_t *mask, sigset_t *old);
+
+#endif /* ! SIGNAL_H */
tools/testing/selftests/arm64/bti/start.S new file +14 -0
new file mode 100644
index 000000000000..831f952e0572
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2019  Arm Limited
+ * Original author: Dave Martin <[email protected]>
+ */
+
+#include "assembler.h"
+
+startfn _start
+	mov	x0, sp
+	b	start
+endfn
+
+emit_aarch64_feature_1_and
tools/testing/selftests/arm64/bti/syscall.S new file +23 -0
new file mode 100644
index 000000000000..8dde8b6f3db1
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2019  Arm Limited
+ * Original author: Dave Martin <[email protected]>
+ */
+
+#include "assembler.h"
+
+startfn syscall
+	bti	c
+	mov	w8, w0
+	mov	x0, x1
+	mov	x1, x2
+	mov	x2, x3
+	mov	x3, x4
+	mov	x4, x5
+	mov	x5, x6
+	mov	x6, x7
+	svc	#0
+	ret
+endfn
+
+emit_aarch64_feature_1_and
tools/testing/selftests/arm64/bti/system.c new file +22 -0
new file mode 100644
index 000000000000..6385d8d4973b
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019  Arm Limited
+ * Original author: Dave Martin <[email protected]>
+ */
+
+#include "system.h"
+
+#include <asm/unistd.h>
+
+#include "compiler.h"
+
+void __noreturn exit(int n)
+{
+	syscall(__NR_exit, n);
+	__unreachable();
+}
+
+ssize_t write(int fd, const void *buf, size_t size)
+{
+	return syscall(__NR_write, fd, buf, size);
+}
tools/testing/selftests/arm64/bti/system.h new file +28 -0
new file mode 100644
index 000000000000..aca118589705
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2019  Arm Limited
+ * Original author: Dave Martin <[email protected]>
+ */
+
+#ifndef SYSTEM_H
+#define SYSTEM_H
+
+#include <linux/types.h>
+#include <linux/stddef.h>
+
+typedef __kernel_size_t size_t;
+typedef __kernel_ssize_t ssize_t;
+
+#include <linux/errno.h>
+#include <asm/hwcap.h>
+#include <asm/ptrace.h>
+#include <asm/unistd.h>
+
+#include "compiler.h"
+
+long syscall(int nr, ...);
+
+void __noreturn exit(int n);
+ssize_t write(int fd, const void *buf, size_t size);
+
+#endif /* ! SYSTEM_H */
tools/testing/selftests/arm64/bti/test.c new file +234 -0

234 lines not on this page · raw message

tools/testing/selftests/arm64/bti/teststubs.S new file +39 -0
new file mode 100644
index 000000000000..b62c8c35f67e
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2019  Arm Limited
+ * Original author: Dave Martin <[email protected]>
+ */
+
+#include "assembler.h"
+
+startfn bti_none_func
+	bti
+	ret
+endfn
+
+startfn bti_c_func
+	bti	c
+	ret
+endfn
+
+startfn bti_j_func
+	bti	j
+	ret
+endfn
+
+startfn bti_jc_func
+	bti	jc
+	ret
+endfn
+
+startfn paciasp_func
+	paciasp
+	autiasp
+	ret
+endfn
+
+startfn nohint_func
+	ret
+endfn
+
+emit_aarch64_feature_1_and
tools/testing/selftests/arm64/bti/trampoline.S new file +29 -0
new file mode 100644
index 000000000000..09beb3f361f1
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2019  Arm Limited
+ * Original author: Dave Martin <[email protected]>
+ */
+
+#include "assembler.h"
+
+startfn call_using_br_x0
+	bti	c
+	br	x0
+endfn
+
+startfn call_using_br_x16
+	bti	c
+	mov	x16, x0
+	br	x16
+endfn
+
+startfn call_using_blr
+	paciasp
+	stp	x29, x30, [sp, #-16]!
+	blr	x0
+	ldp	x29, x30, [sp], #16
+	autiasp
+	ret
+endfn
+
+emit_aarch64_feature_1_and
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 4 lines · Mark Brown

Add some tests that verify that BTI functions correctly for static binaries built with and without BTI support, verifying that SIGILL is generated when expected and is not generated in other situations.

Quoted 8 lines

Since BTI support is still being rolled out in distributions these tests are built entirely free standing, no libc support is used at all so none of the standard helper functions for kselftest can be used and we open code everything. This also means we aren't testing the kernel support for the dynamic linker, though the test program can be readily adapted for that once it becomes something that we can reliably build and run.

[...]

Applied to arm64 (for-next/kselftest), thanks!

[1/1] kselftest: arm64: Add BTI tests

      https://git.kernel.org/arm64/c/d15723c0cbae
Signature · 1 line
Catalin