Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3420161pxf; Mon, 22 Mar 2021 06:09:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzb2fSu8Rzi4vlfeeXtz+9jD+GjsN3t+MnJKHvW/vj66KQr0m4dEAtgG8y92f7pySczLs2H X-Received: by 2002:a17:906:78d:: with SMTP id l13mr19454976ejc.97.1616418592811; Mon, 22 Mar 2021 06:09:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616418592; cv=none; d=google.com; s=arc-20160816; b=xK1iwabohGwVNQknZJ3Tc7WnzNdntk5CMgYTrRsIY4QwV3SbzKr2ZO498l3LDVzGm3 oGFRCGgJWvUqgyB53a6dGHBngJd5Pu4GQPLJgNTjEHcNEFwg0I37orL8MiE3zP3CPs4o b97rSbptCl3+0nFvPTOm2+hMU+AJ6dxrFgVXqodhIuO/E/6ZG5keUjld7pj1zy1gjMZS yCXDOLGy6vsO9JDBtk4ydJUO+cEsAhq+rYe+jkvZejP5lSsZOYnS5RdC/Anj4900EJRl BDcsyt4d4pjDZ6NNyk1B0DsoOtfWjxf9bK2kDQXl+Lx+OXDrKWCpIaJ/G52cUq3o/RbW h9yA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=hBazPdHstyJAH4MKOxZCQvxFbaNfC2pF0DWNV9LJqG4=; b=HuSEArAbsnyBJuDKCOQeoq9aRxyT3TxOxf9H6ehbF5IslGOE3Nnw7t1NUHYOoBWjj4 SY35XfsiS7OCzYbzG4+u9/6Evg+hS0lrAcGJRRGW24EfOZZA6DIkG6NspBAkQjuJGABL csppVs4/s4jFsHATTnxIi0TTvuMAYQEdzwchx286IUn/k6QaSKRWA6VXQ6jmw+gMHTMX GNaxQbds20/K8vRiQCzqqLLb202kwysqe8FagaL+hS61UWba6umFHfd3I7v82ajPZ1Vr TjtV4GIvc8BTBfe8RuC4Yn3Yt0FiY9NtVJks7iI7KDVcJLP676nXdsBebFLc9k2mGRfh VduQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZTxmeGi5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h1si11393921edn.233.2021.03.22.06.09.30; Mon, 22 Mar 2021 06:09:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZTxmeGi5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233346AbhCVNIC (ORCPT + 99 others); Mon, 22 Mar 2021 09:08:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:47788 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232970AbhCVMxG (ORCPT ); Mon, 22 Mar 2021 08:53:06 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 74F0061A09; Mon, 22 Mar 2021 12:47:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1616417238; bh=pIy5eDMAczWNZKbWlbidKpKQos4RcwLCTn8JQSO41i0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZTxmeGi5Ap92gAX7JifcSkl3t2+KXjJbD8Xbm/w0xZRM4WL4G00tu09KWPcMYhMc4 m5EPhtv8nqnMBUBs8/tRRUBBweJEe/03XEbxfdTYvzXcskAKQK7QBIieFAee+j6lqY FmKsQ0yFDHsek1SQUplxYbysHA+n1wf4Glscnh90= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jan Kratochvil , Oleg Nesterov , Thomas Gleixner Subject: [PATCH 4.9 22/25] x86: Introduce TS_COMPAT_RESTART to fix get_nr_restart_syscall() Date: Mon, 22 Mar 2021 13:29:12 +0100 Message-Id: <20210322121921.100885114@linuxfoundation.org> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210322121920.399826335@linuxfoundation.org> References: <20210322121920.399826335@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Oleg Nesterov commit 8c150ba2fb5995c84a7a43848250d444a3329a7d 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: 609c19a385c8 ("x86/ptrace: Stop setting TS_COMPAT in ptrace code") Reported-by: Jan Kratochvil Signed-off-by: Oleg Nesterov Signed-off-by: Thomas Gleixner Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20210201174709.GA17895@redhat.com Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/thread_info.h | 14 +++++++++++++- arch/x86/kernel/signal.c | 24 +----------------------- 2 files changed, 14 insertions(+), 24 deletions(-) --- 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_fram */ #define TS_COMPAT 0x0002 /* 32bit syscall active (64BIT)*/ +#ifndef __ASSEMBLY__ #ifdef CONFIG_COMPAT #define TS_I386_REGS_POKED 0x0004 /* regs poked by 32-bit ptracer */ +#define TS_COMPAT_RESTART 0x0008 + +#define arch_set_restart_data arch_set_restart_data + +static inline void arch_set_restart_data(struct restart_block *restart) +{ + struct thread_info *ti = current_thread_info(); + if (ti->status & TS_COMPAT) + ti->status |= TS_COMPAT_RESTART; + else + ti->status &= ~TS_COMPAT_RESTART; +} #endif -#ifndef __ASSEMBLY__ #ifdef CONFIG_X86_32 #define in_ia32_syscall() true --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -767,30 +767,8 @@ handle_signal(struct ksignal *ksig, stru static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs) { - /* - * This function is fundamentally broken as currently - * implemented. - * - * The idea is that we want to trigger a call to the - * restart_block() syscall and that we want in_ia32_syscall(), - * in_x32_syscall(), etc. to match whatever they were in the - * syscall being restarted. We assume that the syscall - * instruction at (regs->ip - 2) matches whatever syscall - * instruction we used to enter in the first place. - * - * 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. - * - * For now, we maintain historical behavior and guess based on - * stored state. We could do better by saving the actual - * syscall arch in restart_block or (with caveats on x32) by - * checking if regs->ip points to 'int $0x80'. The current - * behavior is incorrect if a tracer has a different bitness - * than the tracee. - */ #ifdef CONFIG_IA32_EMULATION - if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED)) + if (current_thread_info()->status & TS_COMPAT_RESTART) return __NR_ia32_restart_syscall; #endif #ifdef CONFIG_X86_X32_ABI