Skip to contentSkip to conversation

arm64/ptrace: allow to get all registers on syscall traps

PATCH In review Andrei Vagin 2 participants 9 messages Mar 22, 2021 – Mar 27, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover arm64/ptrace: allow to get all registers on syscall traps

Here are two known problems with registers when a tracee is stopped in syscall traps.

The first problem is about the x7 register that is used to indicate whether or not the stop has been signalled from syscall entry or syscall exit. This means that:

- Any writes by the tracer to this register during the stop are ignored/discarded.

- The actual value of the register is not available during the stop, so the tracer cannot save it and restore it later.

For applications like the user-mode Linux or gVisor, it is critical to have access to the full set of registers in any moment. For example, they need to change values of all registers to emulate rt_sigreturn or execve and they need to have the full set of registers to build a signal frame.

The second problem is that orig_x0 isn't exposed to user-space. orig_x0 is recorded at the start of the syscall entry and then it is used for resetting the argument back to its original value during syscall restarts.

This series extends the user_pt_regs structure with orig_x0 and orig_x7. This doesn't break the backward compatibility. There are two interfaces how user-space can receive user_pt_regs from the kernel. The first one is PTRACE_{GET,SET}REGSET. In this case, the user-space provides a buffer and its size and the kernel fills only the part that fits the size. The second interface is a core dump file where registers are written in a separate note and the user-space can parse only the part that it knows.

Cc Catalin Marinas <[email protected]>
Cc Dave Martin <[email protected]>
Cc Keno Fischer <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Will Deacon <[email protected]>

Andrei Vagin (4):
arm64: expose orig_x0 in the user_pt_regs structure arm64/ptrace: introduce orig_x7 in the user_pt_regs structure selftest/arm64/ptrace: add a test for orig_x0 selftest/arm64/ptrace: add a test for orig_x7

v2: use the ptrace option instead of adding a new regset.
v3: append orig_x0 and orig_x7 to the user_pt_regs.

 arch/arm64/include/asm/ptrace.h               |   5 +
 arch/arm64/kernel/ptrace.c                    | 130 +++++++++++-----
 include/uapi/linux/elf.h                      |   1 +
 tools/testing/selftests/arm64/Makefile        |   2 +-
 tools/testing/selftests/arm64/ptrace/Makefile |   6 +
 .../arm64/ptrace/ptrace_syscall_regs_test.c   | 142 ++++++++++++++++++
 6 files changed, 246 insertions(+), 40 deletions(-)
 create mode 100644 tools/testing/selftests/arm64/ptrace/Makefile
 create mode 100644 tools/testing/selftests/arm64/ptrace/ptrace_syscall_regs_test.c
Signature · 1 line
2.29.2
#1 lore raw

PATCH v3 1/4 arm64: expose orig_x0 in the user_pt_regs structure

orig_x0 is recorded at the start of the syscall entry and then it is used for resetting the argument back to its original value during syscall restarts.

If orig_x0 isn't available from user-space, this makes it tricky to manage arguments of restarted system calls.

---
 arch/arm64/include/asm/ptrace.h      | 2 +-
 arch/arm64/include/uapi/asm/ptrace.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/ptrace.h +1 -1
index e58bca832dff..d4cdf98ac003 100644
@@ -183,9 +183,9 @@ struct pt_regs {
 			u64 sp;
 			u64 pc;
 			u64 pstate;
+			u64 orig_x0;
 		};
 	};
-	u64 orig_x0;
 #ifdef __AARCH64EB__
 	u32 unused2;
 	s32 syscallno;
arch/arm64/include/uapi/asm/ptrace.h +1 -0
index 758ae984ff97..3c118c5b0893 100644
@@ -90,6 +90,7 @@ struct user_pt_regs {
 	__u64		sp;
 	__u64		pc;
 	__u64		pstate;
+	__u64		orig_x0;
 };
 
 struct user_fpsimd_state {
Signature · 1 line
2.29.2
maintainer
#2 lore raw

Re: [PATCH 1/4] arm64: expose orig_x0 in the user_pt_regs structure

arch/arm64/include/uapi/asm/ptrace.h struct user_pt_regs { · line 94

Quoted 10 lines · Andrei Vagin
arch/arm64/include/uapi/asm/ptrace.h +1 -0
index 758ae984ff97..3c118c5b0893 100644
@@ -90,6 +90,7 @@ struct user_pt_regs {
 	__u64		sp;
 	__u64		pc;
 	__u64		pstate;
+	__u64		orig_x0;
 };

That's a UAPI change, likely to go wrong. For example, a ptrace(PTRACE_GETREGSET, pid, REGSET_GPR, data) would write past the end of an old struct user_pt_regs in the debugger.

Signature · 1 line
Catalin
#3 lore raw

Re: [PATCH 1/4] arm64: expose orig_x0 in the user_pt_regs structure

Quoted 16 lines · Catalin Marinas

arch/arm64/include/uapi/asm/ptrace.h struct user_pt_regs { · line 94

Quoted 10 lines · Andrei Vagin
arch/arm64/include/uapi/asm/ptrace.h +1 -0
index 758ae984ff97..3c118c5b0893 100644
@@ -90,6 +90,7 @@ struct user_pt_regs {
      __u64           sp;
      __u64           pc;
      __u64           pstate;
+     __u64           orig_x0;
 };

That's a UAPI change, likely to go wrong. For example, a ptrace(PTRACE_GETREGSET, pid, REGSET_GPR, data) would write past the end of an old struct user_pt_regs in the debugger.

ptrace(PTRACE_GETREGSET, ...) receives iovec:
ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)

iov contains a pointer to a buffer and its size and the kernel fills only the part that fits the buffer.
I think this interface was invented to allow extending structures without breaking backward compatibility.

Quoted 3 lines

--
Catalin

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#4 lore raw

Re: [PATCH 1/4] arm64: expose orig_x0 in the user_pt_regs structure

Quoted 19 lines · Andrei Vagin
Quoted 16 lines · Catalin Marinas

arch/arm64/include/uapi/asm/ptrace.h struct user_pt_regs { · line 94

Quoted 10 lines · Andrei Vagin
arch/arm64/include/uapi/asm/ptrace.h +1 -0
index 758ae984ff97..3c118c5b0893 100644
@@ -90,6 +90,7 @@ struct user_pt_regs {
      __u64           sp;
      __u64           pc;
      __u64           pstate;
+     __u64           orig_x0;
 };

That's a UAPI change, likely to go wrong. For example, a ptrace(PTRACE_GETREGSET, pid, REGSET_GPR, data) would write past the end of an old struct user_pt_regs in the debugger.

Quoted 7 lines

ptrace(PTRACE_GETREGSET, ...) receives iovec:
ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)

iov contains a pointer to a buffer and its size and the kernel fills only the part that fits the buffer.
I think this interface was invented to allow extending structures without breaking backward compatibility.

You are right here, it doesn't write past the end of the iov buffer. However, it's still an ABI change. An unaware program using a newer user_pt_regs but running on an older kernel may be surprised that the updated iov.len is smaller than sizeof (struct user_pt_regs).

Changing this structure also changes the core dump format, see ELF_NGREG and ELF_CORE_COPY_REGS. Maybe this doesn't matter much either since the ELF note would have size information but I'd prefer if we didn't modify this structure.

Signature · 6 lines
Catalin

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#5 lore raw

PATCH v3 2/4 arm64/ptrace: introduce orig_x7 in the user_pt_regs structure

In reply to Andrei Vagin

We have some ABI weirdness in the way that we handle syscall exit stops because we indicate whether or not the stop has been signalled from syscall entry or syscall exit by clobbering a general purpose register x7 in the tracee and restoring its old value after the stop.

This behavior was inherited from ARM and it isn't common for other architectures. Now, we have PTRACE_GET_SYSCALL_INFO that gives all required information about system calls, so the hack with clobbering registers isn't needed anymore.

This change instroduces orig_x7 in the user_pt_regs structure that will contains an origin value of the x7 register if the tracee is stopped in a system call..

---
 arch/arm64/include/asm/ptrace.h      |  1 +
 arch/arm64/include/uapi/asm/ptrace.h |  1 +
 arch/arm64/kernel/ptrace.c           | 18 ++++++++++++------
 3 files changed, 14 insertions(+), 6 deletions(-)
arch/arm64/include/asm/ptrace.h +1 -0
index d4cdf98ac003..1008f0fbc5ea 100644
@@ -184,6 +184,7 @@ struct pt_regs {
 			u64 pc;
 			u64 pstate;
 			u64 orig_x0;
+			u64 orig_x7;
 		};
 	};
 #ifdef __AARCH64EB__
arch/arm64/include/uapi/asm/ptrace.h +1 -0
index 3c118c5b0893..be7583ff5f4d 100644
@@ -91,6 +91,7 @@ struct user_pt_regs {
 	__u64		pc;
 	__u64		pstate;
 	__u64		orig_x0;
+	__u64		orig_x7;
 };
 
 struct user_fpsimd_state {
arch/arm64/kernel/ptrace.c +12 -6
index 170f42fd6101..1ed5b4aa986b 100644
@@ -1750,7 +1750,7 @@ static void tracehook_report_syscall(struct pt_regs *regs,
 				     enum ptrace_syscall_dir dir)
 {
 	int regno;
-	unsigned long saved_reg;
+	u64 _saved_reg, *saved_reg;
 
 	/*
 	 * We have some ABI weirdness here in the way that we handle syscall
@@ -1768,19 +1768,25 @@ static void tracehook_report_syscall(struct pt_regs *regs,
 	 * - Syscall stops behave differently to seccomp and pseudo-step traps
 	 *   (the latter do not nobble any registers).
 	 */
-	regno = (is_compat_task() ? 12 : 7);
-	saved_reg = regs->regs[regno];
+	if (is_compat_task()) {
+		regno = 12;
+		saved_reg = &_saved_reg;
+	} else {
+		regno = 7;
+		saved_reg = &regs->orig_x7;
+	}
+	*saved_reg = regs->regs[regno];
 	regs->regs[regno] = dir;
 
 	if (dir == PTRACE_SYSCALL_ENTER) {
 		if (tracehook_report_syscall_entry(regs))
 			forget_syscall(regs);
-		regs->regs[regno] = saved_reg;
+		regs->regs[regno] = *saved_reg;
 	} else if (!test_thread_flag(TIF_SINGLESTEP)) {
 		tracehook_report_syscall_exit(regs, 0);
-		regs->regs[regno] = saved_reg;
+		regs->regs[regno] = *saved_reg;
 	} else {
-		regs->regs[regno] = saved_reg;
+		regs->regs[regno] = *saved_reg;
 
 		/*
 		 * Signal a pseudo-step exception since we are stepping but
Signature · 1 line
2.29.2
maintainer
#6 lore raw

Re: [PATCH 2/4] arm64/ptrace: introduce orig_x7 in the user_pt_regs structure

Quoted 17 lines · Andrei Vagin
arch/arm64/include/asm/ptrace.h +1 -0
index d4cdf98ac003..1008f0fbc5ea 100644
@@ -184,6 +184,7 @@ struct pt_regs {
 			u64 pc;
 			u64 pstate;
 			u64 orig_x0;
+			u64 orig_x7;
 		};
 	};
 #ifdef __AARCH64EB__
arch/arm64/include/uapi/asm/ptrace.h +0 -0
index 3c118c5b0893..be7583ff5f4d 100644

arch/arm64/include/uapi/asm/ptrace.h struct user_pt_regs { · line 95

Quoted 5 lines
arch/arm64/include/uapi/asm/ptrace.h +1 -0
@@ -91,6 +91,7 @@ struct user_pt_regs {
 	__u64		pc;
 	__u64		pstate;
 	__u64		orig_x0;
+	__u64		orig_x7;
 };

Same here. So unless I miss something, we better have a separate NT_ORIGREG (or some better name) regset to retrieve the additional registers. Or, if you want to get all of them in one go, just add a new one similar to NT_PRSTATUS but which restores x0 to orig_x0 and x7 to orig_x7.

Sorry if this was already discussed. I had a brief look at the previous versions and couldn't see a user_pt_regs structure change, nor a suggestion to do so.

Signature · 1 line
Catalin
#7 lore raw

PATCH v3 3/4 selftest/arm64/ptrace: add a test for orig_x0

In reply to Andrei Vagin

The test creates two processes where one traces another one. The tracee executes a system call, the tracer traps it, changes orig_x0, triggers a signal and checks that the syscall is restarted with the setted argument.

Test output:

 $ ./ptrace_restart_syscall_test
 1..3
 ok 1 orig_x0: 0x3
 ok 2 x0: 0x5
 ok 3 The child exited with code 0.
 # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0
---
 tools/testing/selftests/arm64/ptrace/Makefile |   6 +
 tools/testing/selftests/arm64/ptrace/lib.h    |  36 ++++++
 .../ptrace/ptrace_restart_syscall_test.c      | 122 ++++++++++++++++++
 3 files changed, 164 insertions(+)
 create mode 100644 tools/testing/selftests/arm64/ptrace/Makefile
 create mode 100644 tools/testing/selftests/arm64/ptrace/lib.h
 create mode 100644 tools/testing/selftests/arm64/ptrace/ptrace_restart_syscall_test.c
tools/testing/selftests/arm64/ptrace/Makefile new file +6 -0
new file mode 100644
index 000000000000..1bc10e2d2ac8
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+CFLAGS += -g -I../../../../../usr/include/
+TEST_GEN_PROGS := ptrace_restart_syscall_test
+
+include ../../lib.mk
tools/testing/selftests/arm64/ptrace/lib.h new file +36 -0
new file mode 100644
index 000000000000..14f4737188a3
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#ifndef __PTRACE_TEST_LOG_H
+#define __PTRACE_TEST_LOG_H
+
+#define pr_p(func, fmt, ...) func("%s:%d: " fmt ": %m", \
+				  __func__, __LINE__, ##__VA_ARGS__)
+
+#define pr_err(fmt, ...)						\
+	({								\
+		ksft_test_result_error(fmt "\n", ##__VA_ARGS__);	\
+		-1;							\
+	})
+
+#define pr_fail(fmt, ...)					\
+	({							\
+		ksft_test_result_fail(fmt "\n", ##__VA_ARGS__);	\
+		-1;						\
+	})
+
+#define pr_perror(fmt, ...)	pr_p(pr_err, fmt, ##__VA_ARGS__)
+
+static inline int ptrace_and_wait(pid_t pid, int cmd, int sig)
+{
+	int status;
+
+	/* Stop on syscall-exit. */
+	if (ptrace(cmd, pid, 0, 0))
+		return pr_perror("Can't resume the child %d", pid);
+	if (waitpid(pid, &status, 0) != pid)
+		return pr_perror("Can't wait for the child %d", pid);
+	if (!WIFSTOPPED(status) || WSTOPSIG(status) != sig)
+		return pr_err("Unexpected status: %x", status);
+	return 0;
+}
+
+#endif
tools/testing/selftests/arm64/ptrace/ptrace_restart_syscall_test.c new file +122 -0

122 lines not on this page · raw message

Signature · 1 line
2.29.2
#8 lore raw

PATCH v3 4/4 selftest/arm64/ptrace: add a test for orig_x7

In reply to Andrei Vagin

In system calls, x7 is used to indicate whether a tracee has been stopped on syscall-enter or syscall-exit and the origin value of x7 is saved in orig_x7.

Test output:

 $ ./ptrace_syscall_test
 1..4
 ok 1 x7: 0
 ok 2 x7: 1
 ok 3 x7: 686920776f726c64
 ok 4 The child exited with code 0.
 # Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0
---
 tools/testing/selftests/arm64/ptrace/Makefile |   2 +-
 .../arm64/ptrace/ptrace_syscall_test.c        | 158 ++++++++++++++++++
 2 files changed, 159 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/arm64/ptrace/ptrace_syscall_test.c
tools/testing/selftests/arm64/ptrace/Makefile +1 -1
index 1bc10e2d2ac8..ea02c1a63806 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
 CFLAGS += -g -I../../../../../usr/include/
-TEST_GEN_PROGS := ptrace_restart_syscall_test
+TEST_GEN_PROGS := ptrace_restart_syscall_test ptrace_syscall_test
 
 include ../../lib.mk
tools/testing/selftests/arm64/ptrace/ptrace_syscall_test.c new file +158 -0

158 lines not on this page · raw message

Signature · 1 line
2.29.2