linux-stable/arch
Oleg Nesterov a548acde96 x86: Introduce TS_COMPAT_RESTART to fix get_nr_restart_syscall()
commit 8c150ba2fb upstream.

The comment in get_nr_restart_syscall() says:

	 * The problem is that we can get here when ptrace pokes
	 * syscall-like values into regs even if we're not in a syscall
	 * at all.

Yes, but if not in a syscall then the

	status & (TS_COMPAT|TS_I386_REGS_POKED)

check below can't really help:

	- TS_COMPAT can't be set

	- TS_I386_REGS_POKED is only set if regs->orig_ax was changed by
	  32bit debugger; and even in this case get_nr_restart_syscall()
	  is only correct if the tracee is 32bit too.

Suppose that a 64bit debugger plays with a 32bit tracee and

	* Tracee calls sleep(2)	// TS_COMPAT is set
	* User interrupts the tracee by CTRL-C after 1 sec and does
	  "(gdb) call func()"
	* gdb saves the regs by PTRACE_GETREGS
	* does PTRACE_SETREGS to set %rip='func' and %orig_rax=-1
	* PTRACE_CONT		// TS_COMPAT is cleared
	* func() hits int3.
	* Debugger catches SIGTRAP.
	* Restore original regs by PTRACE_SETREGS.
	* PTRACE_CONT

get_nr_restart_syscall() wrongly returns __NR_restart_syscall==219, the
tracee calls ia32_sys_call_table[219] == sys_madvise.

Add the sticky TS_COMPAT_RESTART flag which survives after return to user
mode. It's going to be removed in the next step again by storing the
information in the restart block. As a further cleanup it might be possible
to remove also TS_I386_REGS_POKED with that.

Test-case:

  $ cvs -d :pserver:anoncvs:anoncvs@sourceware.org:/cvs/systemtap co ptrace-tests
  $ gcc -o erestartsys-trap-debuggee ptrace-tests/tests/erestartsys-trap-debuggee.c --m32
  $ gcc -o erestartsys-trap-debugger ptrace-tests/tests/erestartsys-trap-debugger.c -lutil
  $ ./erestartsys-trap-debugger
  Unexpected: retval 1, errno 22
  erestartsys-trap-debugger: ptrace-tests/tests/erestartsys-trap-debugger.c:421

Fixes: 609c19a385 ("x86/ptrace: Stop setting TS_COMPAT in ptrace code")
Reported-by: Jan Kratochvil <jan.kratochvil@redhat.com>
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20210201174709.GA17895@redhat.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-03-25 09:04:17 +01:00
..
alpha local64.h: make <asm/local64.h> mandatory 2021-01-12 20:18:16 +01:00
arc arch/arc: add copy_user_page() to <asm/page.h> to fix build error on ARC 2021-01-19 18:27:26 +01:00
arm ARM: efistub: replace adrl pseudo-op with adr_l macro invocation 2021-03-17 17:06:26 +01:00
arm64 KVM: arm64: Fix nVHE hyp panic host context restore 2021-03-17 17:06:37 +01:00
c6x
csky csky: Fix a size determination in gpr_get() 2021-03-04 11:38:21 +01:00
h8300 h8300: fix PREEMPTION build, TI_PRE_COUNT undefined 2021-02-17 11:02:28 +01:00
hexagon local64.h: make <asm/local64.h> mandatory 2021-01-12 20:18:16 +01:00
ia64 local64.h: make <asm/local64.h> mandatory 2021-01-12 20:18:16 +01:00
m68k local64.h: make <asm/local64.h> mandatory 2021-01-12 20:18:16 +01:00
microblaze local64.h: make <asm/local64.h> mandatory 2021-01-12 20:18:16 +01:00
mips MIPS: compressed: fix build with enabled UBSAN 2021-03-25 09:04:12 +01:00
nds32 local64.h: make <asm/local64.h> mandatory 2021-01-12 20:18:16 +01:00
nios2 nios2: fixed broken sys_clone syscall 2021-03-04 11:38:16 +01:00
openrisc sched/idle: Fix arch_cpu_idle() vs tracing 2020-11-24 16:47:35 +01:00
parisc parisc: Enable -mlong-calls gcc option with CONFIG_COMPILE_TEST 2021-03-11 14:17:21 +01:00
powerpc powerpc: Force inlining of cpu_has_feature() to avoid build failure 2021-03-25 09:04:14 +01:00
riscv RISC-V: correct enum sbi_ext_rfence_fid 2021-03-25 09:04:09 +01:00
s390 s390/pci: fix leak of PCI device structure 2021-03-25 09:04:05 +01:00
sh sh: Remove unused HAVE_COPY_THREAD_TLS macro 2021-01-27 11:55:20 +01:00
sparc sparc64: Use arch_validate_flags() to validate ADI flag 2021-03-17 17:06:24 +01:00
um um: defer killing userspace on page table update failures 2021-03-04 11:38:42 +01:00
x86 x86: Introduce TS_COMPAT_RESTART to fix get_nr_restart_syscall() 2021-03-25 09:04:17 +01:00
xtensa local64.h: make <asm/local64.h> mandatory 2021-01-12 20:18:16 +01:00
.gitignore
Kconfig fanotify: Fix sys_fanotify_mark() on native x86-32 2021-01-17 14:16:59 +01:00