Skip to contentSkip to conversation

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

PATCH Superseded Andrei Vagin 2 participants 8 messages Jan 19, 2021 – Jan 29, 2021

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

Right now, ip/r12 for AArch32 and x7 for AArch64 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.

This series introduces NT_ARM_PRSTATUS to get all registers and makes it possible to change ip/r12 and x7 registers when tracee is stopped in syscall traps.

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 and they need to have the full set of registers to build a signal frame.

Andrei Vagin (3):
arm64/ptrace: don't clobber task registers on syscall entry/exit traps arm64/ptrace: introduce NT_ARM_PRSTATUS to get a full set of registers selftest/arm64/ptrace: add a test for NT_ARM_PRSTATUS

 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 · 7 lines
2.29.2


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

PATCH 1/3 arm64/ptrace: don't clobber task registers on syscall entry/exit traps

ip/r12 for AArch32 and x7 for AArch64 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.

Right now, these registers are clobbered in tracehook_report_syscall. This change moves this logic to gpr_get and compat_gpr_get where registers are copied into a user-space buffer.

This will allow to change these registers and to introduce a new NT_ARM_PRSTATUS command to get the full set of registers.

---
 arch/arm64/include/asm/ptrace.h |   5 ++
 arch/arm64/kernel/ptrace.c      | 104 +++++++++++++++++++-------------
 2 files changed, 67 insertions(+), 42 deletions(-)
arch/arm64/include/asm/ptrace.h +5 -0
index e58bca832dff..0a9552b4f61e 100644
@@ -170,6 +170,11 @@ static inline unsigned long pstate_to_compat_psr(const unsigned long pstate)
 	return psr;
 }
 
+enum ptrace_syscall_dir {
+	PTRACE_SYSCALL_ENTER = 0,
+	PTRACE_SYSCALL_EXIT,
+};
+
 /*
  * This struct defines the way the registers are stored on the stack during an
  * exception. Note that sizeof(struct pt_regs) has to be a multiple of 16 (for
arch/arm64/kernel/ptrace.c +62 -42

138 lines not on this page · raw message

Signature · 7 lines
2.29.2


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

Re: [PATCH 1/3] arm64/ptrace: don't clobber task registers on syscall entry/exit traps

Quoted 71 lines · Andrei Vagin

ip/r12 for AArch32 and x7 for AArch64 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.

Right now, these registers are clobbered in tracehook_report_syscall. This change moves this logic to gpr_get and compat_gpr_get where registers are copied into a user-space buffer.

This will allow to change these registers and to introduce a new NT_ARM_PRSTATUS command to get the full set of registers.

---
 arch/arm64/include/asm/ptrace.h |   5 ++
 arch/arm64/kernel/ptrace.c      | 104 +++++++++++++++++++-------------
 2 files changed, 67 insertions(+), 42 deletions(-)
arch/arm64/include/asm/ptrace.h +5 -0
index e58bca832dff..0a9552b4f61e 100644
@@ -170,6 +170,11 @@ static inline unsigned long pstate_to_compat_psr(const unsigned long pstate)
 	return psr;
 }
 
+enum ptrace_syscall_dir {
+	PTRACE_SYSCALL_ENTER = 0,
+	PTRACE_SYSCALL_EXIT,
+};
+
 /*
  * This struct defines the way the registers are stored on the stack during an
  * exception. Note that sizeof(struct pt_regs) has to be a multiple of 16 (for
arch/arm64/kernel/ptrace.c +16 -1
index 8ac487c84e37..1863f080cb07 100644
@@ -40,6 +40,7 @@
 #include <asm/syscall.h>
 #include <asm/traps.h>
 #include <asm/system_misc.h>
+#include <asm/ptrace.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/syscalls.h>
@@ -561,7 +562,33 @@ static int gpr_get(struct task_struct *target,
 		   struct membuf to)
 {
 	struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
-	return membuf_write(&to, uregs, sizeof(*uregs));
+	unsigned long saved_reg;
+	int ret;
+
+	/*
+	 * We have some ABI weirdness here 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 the general
+	 * purpose register x7.
+	 */
+	switch (target->ptrace_message) {
+	case PTRACE_EVENTMSG_SYSCALL_ENTRY:
+		saved_reg = uregs->regs[7];
+		uregs->regs[7] = PTRACE_SYSCALL_ENTER;
+		break;
+	case PTRACE_EVENTMSG_SYSCALL_EXIT:

arch/arm64/kernel/ptrace.c static int gpr_get(struct task_struct *target, · line 589

Quoted 10 lines
arch/arm64/kernel/ptrace.c +10 -0
+		saved_reg = uregs->regs[7];
+		uregs->regs[7] = PTRACE_SYSCALL_EXIT;
+		break;
+	}
+
+	ret =  membuf_write(&to, uregs, sizeof(*uregs));
+
+	if (target->ptrace_message == PTRACE_EVENTMSG_SYSCALL_ENTRY ||
+	    target->ptrace_message == PTRACE_EVENTMSG_SYSCALL_EXIT)
+		uregs->regs[7] = saved_reg;

This might be a reasonable cleanup even if the extra regset isn't
introduced: it makes it clear that we're not changing the user registers here, just the tracer's view of them.

I'm assuming it doesn't break tracing anywhere else. I can't think of anything it would break just now, but I haven't spent much time looking into it.

Can you not just unconditionally back up and restore regs[7] here? e.g.

	saved_reg = uregs->regs[7];

	switch (target->ptrace_message) {
	case PTRACE_EVENTMSG_SYSCALL_ENTRY:
	case PTRACE_EVENTMSG_SYSCALL_EXIT:
		uregs->regs[7] = target->ptrace_message;
	}

	ret = membuf_write(...);

	uregs->regs[7] = saved_reg;
Quoted 35 lines
arch/arm64/kernel/ptrace.c +24 -2
+
+	return ret;
 }
 
 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
@@ -1221,10 +1248,40 @@ static int compat_gpr_get(struct task_struct *target,
 			  const struct user_regset *regset,
 			  struct membuf to)
 {
+	compat_ulong_t r12;
+	bool overwrite_r12;
 	int i = 0;
 
-	while (to.left)
-		membuf_store(&to, compat_get_user_reg(target, i++));
+	/*
+	 * We have some ABI weirdness here 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 the
+	 * general purpose register r12.
+	 */
+	switch (target->ptrace_message) {
+	case PTRACE_EVENTMSG_SYSCALL_ENTRY:
+		r12 = PTRACE_SYSCALL_ENTER;
+		overwrite_r12 = true;
+		break;
+	case PTRACE_EVENTMSG_SYSCALL_EXIT:
+		r12 = PTRACE_SYSCALL_EXIT;
+		overwrite_r12 = true;
+		break;
+	default:
+		overwrite_r12 = false;
+		break;
+	}
+

arch/arm64/kernel/ptrace.c static int compat_gpr_get(struct task_struct *target, · line 1284

Quoted 10 lines
arch/arm64/kernel/ptrace.c +10 -0
+	while (to.left) {
+		compat_ulong_t val;
+
+		if (!overwrite_r12 || i != 12)
+			val = compat_get_user_reg(target, i++);
+		else
+			val = r12;
+		membuf_store(&to, val);
+	}
+

Can this be condensed too, say by introducing a wrapper for compat_get_user_reg() that does the fudging on r12?

[...]

Cheers
---Dave

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

#3 lore raw

PATCH 2/3 arm64/ptrace: introduce NT_ARM_PRSTATUS to get a full set of registers

In reply to Andrei Vagin

This is an alternative to NT_PRSTATUS that clobbers ip/r12 on AArch32, x7 on AArch64 when a tracee is stopped in syscall entry or syscall exit traps.

---
 arch/arm64/kernel/ptrace.c | 39 ++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/elf.h   |  1 +
 2 files changed, 40 insertions(+)
arch/arm64/kernel/ptrace.c +39 -0
index 1863f080cb07..b8e4c2ddf636 100644
@@ -591,6 +591,15 @@ static int gpr_get(struct task_struct *target,
 	return ret;
 }
 
+static int gpr_get_full(struct task_struct *target,
+		   const struct user_regset *regset,
+		   struct membuf to)
+{
+	struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
+
+	return membuf_write(&to, uregs, sizeof(*uregs));
+}
+
 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
 		   unsigned int pos, unsigned int count,
 		   const void *kbuf, const void __user *ubuf)
@@ -1088,6 +1097,7 @@ static int tagged_addr_ctrl_set(struct task_struct *target, const struct
 
 enum aarch64_regset {
 	REGSET_GPR,
+	REGSET_GPR_FULL,
 	REGSET_FPR,
 	REGSET_TLS,
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
@@ -1119,6 +1129,14 @@ static const struct user_regset aarch64_regsets[] = {
 		.regset_get = gpr_get,
 		.set = gpr_set
 	},
+	[REGSET_GPR_FULL] = {
+		.core_note_type = NT_ARM_PRSTATUS,
+		.n = sizeof(struct user_pt_regs) / sizeof(u64),
+		.size = sizeof(u64),
+		.align = sizeof(u64),
+		.regset_get = gpr_get_full,
+		.set = gpr_set
+	},
 	[REGSET_FPR] = {
 		.core_note_type = NT_PRFPREG,
 		.n = sizeof(struct user_fpsimd_state) / sizeof(u32),
@@ -1225,6 +1243,7 @@ static const struct user_regset_view user_aarch64_view = {
 #ifdef CONFIG_COMPAT
 enum compat_regset {
 	REGSET_COMPAT_GPR,
+	REGSET_COMPAT_GPR_FULL,
 	REGSET_COMPAT_VFP,
 };
 
@@ -1285,6 +1304,18 @@ static int compat_gpr_get(struct task_struct *target,
 	return 0;
 }
 
+/* compat_gpr_get_full doesn't  overwrite x12 like compat_gpr_get. */
+static int compat_gpr_get_full(struct task_struct *target,
+			  const struct user_regset *regset,
+			  struct membuf to)
+{
+	int i = 0;
+
+	while (to.left)
+		membuf_store(&to, compat_get_user_reg(target, i++));
+	return 0;
+}
+
 static int compat_gpr_set(struct task_struct *target,
 			  const struct user_regset *regset,
 			  unsigned int pos, unsigned int count,
@@ -1435,6 +1466,14 @@ static const struct user_regset aarch32_regsets[] = {
 		.regset_get = compat_gpr_get,
 		.set = compat_gpr_set
 	},
+	[REGSET_COMPAT_GPR_FULL] = {
+		.core_note_type = NT_ARM_PRSTATUS,
+		.n = COMPAT_ELF_NGREG,
+		.size = sizeof(compat_elf_greg_t),
+		.align = sizeof(compat_elf_greg_t),
+		.regset_get = compat_gpr_get_full,
+		.set = compat_gpr_set
+	},
 	[REGSET_COMPAT_VFP] = {
 		.core_note_type = NT_ARM_VFP,
 		.n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
include/uapi/linux/elf.h +1 -0
index 30f68b42eeb5..a2086d19263a 100644
@@ -426,6 +426,7 @@ typedef struct elf64_shdr {
 #define NT_ARM_PACA_KEYS	0x407	/* ARM pointer authentication address keys */
 #define NT_ARM_PACG_KEYS	0x408	/* ARM pointer authentication generic key */
 #define NT_ARM_TAGGED_ADDR_CTRL	0x409	/* arm64 tagged address control (prctl()) */
+#define NT_ARM_PRSTATUS		0x410   /* ARM general-purpose registers */
 #define NT_ARC_V2	0x600		/* ARCv2 accumulator/extra registers */
 #define NT_VMCOREDD	0x700		/* Vmcore Device Dump Note */
 #define NT_MIPS_DSP	0x800		/* MIPS DSP ASE registers */
Signature · 7 lines
2.29.2


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

Re: [PATCH 2/3] arm64/ptrace: introduce NT_ARM_PRSTATUS to get a full set of registers

Quoted 5 lines · Andrei Vagin

This is an alternative to NT_PRSTATUS that clobbers ip/r12 on AArch32, x7 on AArch64 when a tracee is stopped in syscall entry or syscall exit traps.

This approach looks like it works, though I still think adding an option for this under PTRACE_SETOPTIONS would be less intrusive.

Adding a shadow regset like this also looks like it would cause the gp regs to be pointlessly be dumped twice in a core dump. Avoiding that might require hacks in the core code...

Quoted 26 lines
---
 arch/arm64/kernel/ptrace.c | 39 ++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/elf.h   |  1 +
 2 files changed, 40 insertions(+)
arch/arm64/kernel/ptrace.c +9 -0
index 1863f080cb07..b8e4c2ddf636 100644
@@ -591,6 +591,15 @@ static int gpr_get(struct task_struct *target,
 	return ret;
 }
 
+static int gpr_get_full(struct task_struct *target,
+		   const struct user_regset *regset,
+		   struct membuf to)
+{
+	struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
+
+	return membuf_write(&to, uregs, sizeof(*uregs));
+}
+
 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
 		   unsigned int pos, unsigned int count,
 		   const void *kbuf, const void __user *ubuf)

arch/arm64/kernel/ptrace.c static int tagged_addr_ctrl_set(struct task_struct *target, const struct · line 1100

Quoted 4 lines
arch/arm64/kernel/ptrace.c +1 -0
@@ -1088,6 +1097,7 @@ static int tagged_addr_ctrl_set(struct task_struct *target, const struct
 
 enum aarch64_regset {
 	REGSET_GPR,
+	REGSET_GPR_FULL,

If we go with this approach, "REGSET_GPR_RAW" might be a preferable name. Both regs represent all the regs ("full"), but REGSET_GPR is mangled by the kernel.

arch/arm64/kernel/ptrace.c static const struct user_regset aarch64_regsets[] = { · line 1133

Quoted 9 lines
arch/arm64/kernel/ptrace.c +2 -0
 	REGSET_FPR,
 	REGSET_TLS,
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
@@ -1119,6 +1129,14 @@ static const struct user_regset aarch64_regsets[] = {
 		.regset_get = gpr_get,
 		.set = gpr_set
 	},
+	[REGSET_GPR_FULL] = {
+		.core_note_type = NT_ARM_PRSTATUS,

Similarly, something like NT_ARM_PRSTATUS_RAW or similar.

Quoted 56 lines
arch/arm64/kernel/ptrace.c +27 -0
+		.n = sizeof(struct user_pt_regs) / sizeof(u64),
+		.size = sizeof(u64),
+		.align = sizeof(u64),
+		.regset_get = gpr_get_full,
+		.set = gpr_set
+	},
 	[REGSET_FPR] = {
 		.core_note_type = NT_PRFPREG,
 		.n = sizeof(struct user_fpsimd_state) / sizeof(u32),
@@ -1225,6 +1243,7 @@ static const struct user_regset_view user_aarch64_view = {
 #ifdef CONFIG_COMPAT
 enum compat_regset {
 	REGSET_COMPAT_GPR,
+	REGSET_COMPAT_GPR_FULL,
 	REGSET_COMPAT_VFP,
 };
 
@@ -1285,6 +1304,18 @@ static int compat_gpr_get(struct task_struct *target,
 	return 0;
 }
 
+/* compat_gpr_get_full doesn't  overwrite x12 like compat_gpr_get. */
+static int compat_gpr_get_full(struct task_struct *target,
+			  const struct user_regset *regset,
+			  struct membuf to)
+{
+	int i = 0;
+
+	while (to.left)
+		membuf_store(&to, compat_get_user_reg(target, i++));
+	return 0;
+}
+
 static int compat_gpr_set(struct task_struct *target,
 			  const struct user_regset *regset,
 			  unsigned int pos, unsigned int count,
@@ -1435,6 +1466,14 @@ static const struct user_regset aarch32_regsets[] = {
 		.regset_get = compat_gpr_get,
 		.set = compat_gpr_set
 	},
+	[REGSET_COMPAT_GPR_FULL] = {
+		.core_note_type = NT_ARM_PRSTATUS,
+		.n = COMPAT_ELF_NGREG,
+		.size = sizeof(compat_elf_greg_t),
+		.align = sizeof(compat_elf_greg_t),
+		.regset_get = compat_gpr_get_full,
+		.set = compat_gpr_set
+	},
 	[REGSET_COMPAT_VFP] = {
 		.core_note_type = NT_ARM_VFP,
 		.n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
include/uapi/linux/elf.h +0 -0
index 30f68b42eeb5..a2086d19263a 100644

include/uapi/linux/elf.h typedef struct elf64_shdr { · line 428

Quoted 3 lines
include/uapi/linux/elf.h +0 -0
@@ -426,6 +426,7 @@ typedef struct elf64_shdr {
 #define NT_ARM_PACA_KEYS	0x407	/* ARM pointer authentication address keys */
 #define NT_ARM_PACG_KEYS	0x408	/* ARM pointer authentication generic key */
 #define NT_ARM_TAGGED_ADDR_CTRL	0x409	/* arm64 tagged address control (prctl()) */

What happened to 0x40a..0x40f?

[...]

Cheers
---Dave

#5 lore raw

Re: [PATCH 2/3] arm64/ptrace: introduce NT_ARM_PRSTATUS to get a full set of registers

Quoted 9 lines · Dave Martin
Quoted 5 lines · Andrei Vagin

This is an alternative to NT_PRSTATUS that clobbers ip/r12 on AArch32, x7 on AArch64 when a tracee is stopped in syscall entry or syscall exit traps.

This approach looks like it works, though I still think adding an option for this under PTRACE_SETOPTIONS would be less intrusive.

Dave, thank you for the feedback. I will prepare a patch with an option and then we will see what looks better.

Quoted 33 lines

Adding a shadow regset like this also looks like it would cause the gp regs to be pointlessly be dumped twice in a core dump. Avoiding that might require hacks in the core code...

Quoted 27 lines
---
 arch/arm64/kernel/ptrace.c | 39 ++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/elf.h   |  1 +
 2 files changed, 40 insertions(+)
arch/arm64/kernel/ptrace.c +9 -0
index 1863f080cb07..b8e4c2ddf636 100644
@@ -591,6 +591,15 @@ static int gpr_get(struct task_struct *target,
 	return ret;
 }
 
+static int gpr_get_full(struct task_struct *target,
+		   const struct user_regset *regset,
+		   struct membuf to)
+{
+	struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
+
+	return membuf_write(&to, uregs, sizeof(*uregs));
+}
+
 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
 		   unsigned int pos, unsigned int count,
 		   const void *kbuf, const void __user *ubuf)
@@ -1088,6 +1097,7 @@ static int tagged_addr_ctrl_set(struct task_struct *target, const struct
 
Quoted 7 lines

arch/arm64/kernel/ptrace.c static int tagged_addr_ctrl_set(struct task_struct *target, const struct · line 1100

Quoted 3 lines
arch/arm64/kernel/ptrace.c +1 -0
 enum aarch64_regset {
 	REGSET_GPR,
+	REGSET_GPR_FULL,

If we go with this approach, "REGSET_GPR_RAW" might be a preferable name. Both regs represent all the regs ("full"), but REGSET_GPR is mangled by the kernel.

I agree that REGSET_GPR_RAW looks better in this case.

Quoted 10 lines
Quoted 10 lines
arch/arm64/kernel/ptrace.c +2 -0
 	REGSET_FPR,
 	REGSET_TLS,
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
@@ -1119,6 +1129,14 @@ static const struct user_regset aarch64_regsets[] = {
 		.regset_get = gpr_get,
 		.set = gpr_set
 	},
+	[REGSET_GPR_FULL] = {
+		.core_note_type = NT_ARM_PRSTATUS,

...

Quoted 10 lines

include/uapi/linux/elf.h typedef struct elf64_shdr { · line 428

Quoted 8 lines
include/uapi/linux/elf.h +0 -0
index 30f68b42eeb5..a2086d19263a 100644
@@ -426,6 +426,7 @@ typedef struct elf64_shdr {
 #define NT_ARM_PACA_KEYS	0x407	/* ARM pointer authentication address keys */
 #define NT_ARM_PACG_KEYS	0x408	/* ARM pointer authentication generic key */
 #define NT_ARM_TAGGED_ADDR_CTRL	0x409	/* arm64 tagged address control (prctl()) */

What happened to 0x40a..0x40f?

shame on me :)

Quoted 5 lines

[...]

Cheers
---Dave

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

#6 lore raw

PATCH 3/3 selftest/arm64/ptrace: add a test for NT_ARM_PRSTATUS

In reply to Andrei Vagin

Test output:
TAP version 13
1..1
# selftests: arm64/ptrace: ptrace_syscall_regs_test # 1..3

 # ok 1 NT_PRSTATUS: x7 0
 # ok 2 NT_ARM_PRSTATUS: x7 686920776f726c64
 # ok 3 The child exited with code 0.
 # # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0
 ok 1 selftests: arm64/ptrace: ptrace_syscall_regs_test
---
 tools/testing/selftests/arm64/Makefile        |   2 +-
 tools/testing/selftests/arm64/ptrace/Makefile |   6 +
 .../arm64/ptrace/ptrace_syscall_regs_test.c   | 147 ++++++++++++++++++
 3 files changed, 154 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/arm64/ptrace/Makefile
 create mode 100644 tools/testing/selftests/arm64/ptrace/ptrace_syscall_regs_test.c
tools/testing/selftests/arm64/Makefile +1 -1
index 2c9d012797a7..704770a60ece 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 ptrace
 else
 ARM64_SUBTARGETS :=
 endif
tools/testing/selftests/arm64/ptrace/Makefile new file +6 -0
new file mode 100644
index 000000000000..ca906ce3a581
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+CFLAGS += -g -I../../../../../usr/include/
+TEST_GEN_PROGS := ptrace_syscall_regs_test
+
+include ../../lib.mk
tools/testing/selftests/arm64/ptrace/ptrace_syscall_regs_test.c new file +147 -0

147 lines not on this page · raw message

Signature · 7 lines
2.29.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#7 lore raw
In reply to Andrei Vagin
Quoted 11 lines · Andrei Vagin

Right now, ip/r12 for AArch32 and x7 for AArch64 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.

Quoted 8 lines

This series introduces NT_ARM_PRSTATUS to get all registers and makes it possible to change ip/r12 and x7 registers when tracee is stopped in syscall traps.

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

I have found the thread [1] where Keno, Will, and Dave discussed the same problem. If I understand this right, the problem was not fixed, because there were no users who needed it.

gVisor is a general-purpose sandbox to run untrusted workloads. It has a platform interface that is responsible for syscall interception, context switching, and managing process address spaces. Right now, we have kvm and ptrace platforms. The ptrace platform runs a guest code in the context of stub processes and intercepts syscalls with help of PTRACE_SYSEMU. All system calls are handled by the gVisor kernel including rt_sigreturn and execve. Signal handling is happing inside the gVisor kernel too. Each stub process can have more than one thread, but we don't bind guest threads to stub threads and we can run more than one guest thread in the context of one stub thread. Taking into account all these facts, we need to have access to all registers at any moment when a stub thread has been stopped.

We were able to introduce the workaround [3] for this issue. Each time when a stub process is stopped on a system call, we queue a fake signal and resume a process to stop it on the signal. It works, but we need to do extra interaction with a stub process what is expensive. My benchmarks show that this workaround slows down syscalls in gVisor for more than 50%. BTW: it is one of the major reasons why PTRACE_SYSEMU was introduced instead of emulating it via two calls of PTRACE_SYSCALL.

[1] https://lore.kernel.org/lkml/CABV8kRz0mKSc=u1LeonQSLroKJLOKWOWktCoGji2nvEBc=e7=w@mail.gmail.com/#r
[2] https://github.com/google/gvisor/issues/5238
[3] https://github.com/google/gvisor/commit/a44efaab6d4b815880749a39647fb3ed9634a489

Quoted 16 lines

Andrei Vagin (3):
arm64/ptrace: don't clobber task registers on syscall entry/exit traps arm64/ptrace: introduce NT_ARM_PRSTATUS to get a full set of registers selftest/arm64/ptrace: add a test for NT_ARM_PRSTATUS

 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
Quoted 3 lines

--
2.29.2

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