[PATCH 3/4] ptrace/x86: introduce TS_COMPAT_RESTART to fix get_nr_restart_syscall()

From: Oleg Nesterov
Date: Fri Nov 29 2019 - 11:52:42 EST


---
arch/x86/include/asm/thread_info.h | 14 +++++++++++++-
arch/x86/kernel/signal.c | 24 +-----------------------
2 files changed, 14 insertions(+), 24 deletions(-)

diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thre=
ad_info.h
index b2125c4..a4de7aa 100644
--- a/arch/x86/include/asm/thread_info.h
+++ b/arch/x86/include/asm/thread_info.h
@@ -230,10 +230,22 @@ static inline int arch_within_stack_frames(const void=
* const stack,
*/
#define TS_COMPAT=09=090x0002=09/* 32bit syscall active (64BIT)*/
=20
+#ifndef __ASSEMBLY__
#ifdef CONFIG_COMPAT
#define TS_I386_REGS_POKED=090x0004=09/* regs poked by 32-bit ptracer */
+#define TS_COMPAT_RESTART=090x0008
+
+#define arch_set_restart_data=09arch_set_restart_data
+
+static inline void arch_set_restart_data(struct restart_block *restart)
+{
+=09struct thread_info *ti =3D current_thread_info();
+=09if (ti->status & TS_COMPAT)
+=09=09ti->status |=3D TS_COMPAT_RESTART;
+=09else
+=09=09ti->status &=3D ~TS_COMPAT_RESTART;
+}
#endif
-#ifndef __ASSEMBLY__
=20
#ifdef CONFIG_X86_32
#define in_ia32_syscall() true
diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c
index 8eb7193..2fdbf5e 100644
--- a/arch/x86/kernel/signal.c
+++ b/arch/x86/kernel/signal.c
@@ -770,30 +770,8 @@ handle_signal(struct ksignal *ksig, struct pt_regs *re=
gs)
=20
static inline unsigned long get_nr_restart_syscall(const struct pt_regs *r=
egs)
{
-=09/*
-=09 * This function is fundamentally broken as currently
-=09 * implemented.
-=09 *
-=09 * The idea is that we want to trigger a call to the
-=09 * restart_block() syscall and that we want in_ia32_syscall(),
-=09 * in_x32_syscall(), etc. to match whatever they were in the
-=09 * syscall being restarted. We assume that the syscall
-=09 * instruction at (regs->ip - 2) matches whatever syscall
-=09 * instruction we used to enter in the first place.
-=09 *
-=09 * The problem is that we can get here when ptrace pokes
-=09 * syscall-like values into regs even if we're not in a syscall
-=09 * at all.
-=09 *
-=09 * For now, we maintain historical behavior and guess based on
-=09 * stored state. We could do better by saving the actual
-=09 * syscall arch in restart_block or (with caveats on x32) by
-=09 * checking if regs->ip points to 'int $0x80'. The current
-=09 * behavior is incorrect if a tracer has a different bitness
-=09 * than the tracee.
-=09 */
#ifdef CONFIG_IA32_EMULATION
-=09if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
+=09if (current_thread_info()->status & TS_COMPAT_RESTART)
=09=09return __NR_ia32_restart_syscall;
#endif
#ifdef CONFIG_X86_X32_ABI
--=20
2.5.0