Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp4146501ybc; Tue, 26 Nov 2019 04:39:06 -0800 (PST) X-Google-Smtp-Source: APXvYqxaggW3UG2mUDZ0GGGN8hw4izoymUNnQ5oQvRjGqTR6YqQxkxjZNC9j6TMDuYJOBGXaQ/0L X-Received: by 2002:aa7:d356:: with SMTP id m22mr24378260edr.170.1574771946044; Tue, 26 Nov 2019 04:39:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574771946; cv=none; d=google.com; s=arc-20160816; b=eNFc1cT+BNlKK+FmgyjHSvAhwjinMCP2shHXKFnUeTj1TX1VFA93KXJhUaIEo5MuoJ 3kmdzj/oqpbrQoegk+kbXyy9hKtJRfwR6UmvsVu12nW/AGUgOHpPyeYp1ajCTYfB6fxQ c1C51bu+KCPnWYzPAhgWW41UjjBD20lMYh0HIp1FUn/qg7FWIiuYHIKcz3QNfgBFAs6U zB6fAn4C3GNpI//+XWfUHD9MYE0ocYFAQESEidd47hoWujsIgXMdeKIW9uOj6x0HiT61 2RgNjLViK8tNbUrvKN7FNfcssKwvcPoXcfGRmHbL+D4G26a8JoWxlzl7jCxdZR4qomjZ vfkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:user-agent:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=kDGl1FFgAo2DfZpn7nDoTyR5flpxsqtge6+lnleipHk=; b=TF+NABbRYlS4AJvkcbPNHawvjA1l0blX1WZVYaEyQZvoOPecC2l8IWIVBaLV9VGVK4 e3ls83Yx4Kab2ImLbDof39vicDBlHRpfcSYCfAbW74Cp3cCogaZTZh8Ljd9d6m14wUMF QqjGB2204/Of4HWMVoE4UL5oJwn19tmmizpzcB5zNkiGcMiu2hU5StuhsobNKh3LN8Kh ZkX25r4mjnesdGlSloTWnIpz071eStAOIn+xckMtEU1X4A8MbfBqFvaUhiGpIiCSLKsg zMp6ZjAPOqcI1R+ZwlGkNWNe2NR5oqnCW0Wpsb8e7xvxjDbsoudYPbSwwK3nGZ5j0PCX OgaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cVm8FtVR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id fx12si6793075ejb.154.2019.11.26.04.38.41; Tue, 26 Nov 2019 04:39:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cVm8FtVR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727865AbfKZLHN (ORCPT + 99 others); Tue, 26 Nov 2019 06:07:13 -0500 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:22337 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725911AbfKZLHN (ORCPT ); Tue, 26 Nov 2019 06:07:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574766431; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=kDGl1FFgAo2DfZpn7nDoTyR5flpxsqtge6+lnleipHk=; b=cVm8FtVR/dur8WzQb/IgVOdIZYmfQAA1zOaAPYjaXtd1qTmA+ds2kQdbKsVzWbCfVAeCvL qS8cNo8hIehnFMvOvtcbUZM1K7eaeuP3GAIT96qfrtq40qB8LXTG+Hg7kv5z7uA+eIwmdW v7Sp1Z5Tec/uP0RBYq8n2kP757U1tbc= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-308-FGPlw9yIPzKt7JlXEu7P1Q-1; Tue, 26 Nov 2019 06:07:07 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B635F1856A60; Tue, 26 Nov 2019 11:07:05 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.43.17.44]) by smtp.corp.redhat.com (Postfix) with SMTP id 903955D6BE; Tue, 26 Nov 2019 11:07:00 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 26 Nov 2019 12:07:05 +0100 (CET) Date: Tue, 26 Nov 2019 12:06:59 +0100 From: Oleg Nesterov To: Andrew Morton Cc: Andy Lutomirski , Borislav Petkov , Ingo Molnar , Jan Kratochvil , Linus Torvalds , Pedro Alves , Peter Anvin , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org Subject: [PATCH] ptrace/x86: introduce TS_COMPAT_RESTART to fix get_nr_restart_syscall() Message-ID: <20191126110659.GA14042@redhat.com> MIME-Version: 1.0 User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-MC-Unique: FGPlw9yIPzKt7JlXEu7P1Q-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The comment in get_nr_restart_syscall() says: =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. Yes. but if we are not in syscall then the =09status & (TS_COMPAT|TS_I386_REGS_POKED) check below can't really help: =09- TS_COMPAT can't be set =09- TS_I386_REGS_POKED is only set if regs->orig_ax was changed by =09 32bit debugger; and even in this case get_nr_restart_syscall() =09 is only correct if the tracee is 32bit too. Suppose that 64bit debugger plays with 32bit tracee and =09* Tracee calls sleep(2)=09// TS_COMPAT is set =09* User interrupts the tracee by CTRL-C after 1 sec and does =09 "(gdb) call func()" =09* gdb saves the regs by PTRACE_GETREGS =09* does PTRACE_SETREGS to set %rip=3D'func' and %orig_rax=3D-1 =09* PTRACE_CONT=09=09// TS_COMPAT is cleared =09* func() hits int3. =09* Debugger catches SIGTRAP. =09* Restore original regs by PTRACE_SETREGS. =09* PTRACE_CONT get_nr_restart_syscall() wrongly returns __NR_restart_syscall=3D=3D219, the tracee calls ia32_sys_call_table[219] =3D=3D sys_madvise. This patch adds the sticky TS_COMPAT_RESTART flag which survives after return to user mode, hopefully it allows us to kill TS_I386_REGS_POKED but this needs another patch. Test-case: $ cvs -d :pserver:anoncvs:anoncvs@sourceware.org:/cvs/systemtap co ptrace= -tests $ gcc -o erestartsys-trap-debuggee ptrace-tests/tests/erestartsys-trap-de= buggee.c --m32 $ gcc -o erestartsys-trap-debugger ptrace-tests/tests/erestartsys-trap-de= bugger.c -lutil $ ./erestartsys-trap-debugger Unexpected: retval 1, errno 22 erestartsys-trap-debugger: ptrace-tests/tests/erestartsys-trap-debugger.c= :421 Reported-by: Jan Kratochvil Signed-off-by: Oleg Nesterov --- arch/x86/entry/common.c | 9 +++++++++ arch/x86/include/asm/thread_info.h | 1 + arch/x86/kernel/signal.c | 6 ++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c index 3f8e226..1e2c2ca 100644 --- a/arch/x86/entry/common.c +++ b/arch/x86/entry/common.c @@ -263,6 +263,15 @@ __visible inline void syscall_return_slowpath(struct p= t_regs *regs) =20 =09rseq_syscall(regs); =20 +#ifdef CONFIG_IA32_EMULATION +=09if (regs->ax =3D=3D -ERESTART_RESTARTBLOCK) { +=09=09struct thread_info *ti =3D current_thread_info(); +=09=09if (ti->status & TS_COMPAT) +=09=09=09ti->status |=3D TS_COMPAT_RESTART; +=09=09else +=09=09=09ti->status &=3D ~TS_COMPAT_RESTART; +=09} +#endif =09/* =09 * First do one-time work. If these work items are enabled, we =09 * want to run them exactly once per syscall exit with IRQs on. diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thre= ad_info.h index f945353..0784591 100644 --- a/arch/x86/include/asm/thread_info.h +++ b/arch/x86/include/asm/thread_info.h @@ -223,6 +223,7 @@ static inline int arch_within_stack_frames(const void *= const stack, =20 #ifdef CONFIG_COMPAT #define TS_I386_REGS_POKED=090x0004=09/* regs poked by 32-bit ptracer */ +#define TS_COMPAT_RESTART=090x0008 #endif #ifndef __ASSEMBLY__ =20 diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c index 8eb7193..1054f1c 100644 --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -788,12 +788,10 @@ static inline unsigned long get_nr_restart_syscall(co= nst struct pt_regs *regs) =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 * checking if regs->ip points to 'int $0x80'. =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