From patchwork Wed Jun 18 20:27:48 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 4379191 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 3D698BEEAA for ; Wed, 18 Jun 2014 20:33:35 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5EC8920380 for ; Wed, 18 Jun 2014 20:33:34 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 805F02037F for ; Wed, 18 Jun 2014 20:33:33 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1WxMVS-0007oM-3T; Wed, 18 Jun 2014 20:30:54 +0000 Received: from smtp.outflux.net ([2001:19d0:2:6:c0de:0:736d:7470]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1WxMVO-0007ko-El for linux-arm-kernel@lists.infradead.org; Wed, 18 Jun 2014 20:30:51 +0000 Received: from www.outflux.net (serenity.outflux.net [10.2.0.2]) by vinyl.outflux.net (8.14.4/8.14.4/Debian-4.1ubuntu1) with ESMTP id s5IKRmVY025888; Wed, 18 Jun 2014 13:27:48 -0700 Date: Wed, 18 Jun 2014 13:27:48 -0700 From: Kees Cook To: linux-kernel@vger.kernel.org Subject: [PATCH] arm: ptrace: fix syscall modification under PTRACE_O_TRACESECCOMP Message-ID: <20140618202748.GA9022@www.outflux.net> MIME-Version: 1.0 Content-Disposition: inline X-MIMEDefang-Filter: outflux$Revision: 1.316 $ X-HELO: www.outflux.net X-Scanned-By: MIMEDefang 2.73 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140618_133050_702041_B78991B5 X-CRM114-Status: GOOD ( 15.36 ) X-Spam-Score: -2.3 (--) Cc: =?iso-8859-1?Q?Andr=E9?= Hentschel , Russell King , Jonathan Austin , Will Deacon , Oleg Nesterov , Andy Lutomirski , Andrew Morton , linux-arm-kernel@lists.infradead.org, Ricky Zhou X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP An x86 tracer wanting to change the syscall uses PTRACE_SETREGS (stored to regs->orig_ax), and an ARM tracer uses PTRACE_SET_SYSCALL (stored to current_thread_info()->syscall). When this happens, the syscall can change across the call to secure_computing(), since it may block on tracer notification, and the tracer can then make changes to the process, before we return from secure_computing(). This means the code must respect the changed syscall after the secure_computing() call in syscall_trace_enter(). The same is true for tracehook_report_syscall_entry() which may also block and change the syscall. The x86 code handles this (it expects orig_ax to always be the desired syscall). In the ARM case, this means we should not be touching current_thread_info()->syscall after its initial assignment. All failures should result in a -1 syscall, though. Based on patch by Ricky Zhou. Signed-off-by: Kees Cook Cc: Ricky Zhou Cc: stable@vger.kernel.org --- arch/arm/kernel/ptrace.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/arch/arm/kernel/ptrace.c b/arch/arm/kernel/ptrace.c index 0dd3b79b15c3..97bd95f6aa01 100644 --- a/arch/arm/kernel/ptrace.c +++ b/arch/arm/kernel/ptrace.c @@ -911,6 +911,7 @@ enum ptrace_syscall_dir { static int tracehook_report_syscall(struct pt_regs *regs, enum ptrace_syscall_dir dir) { + int ret = 0; unsigned long ip; /* @@ -923,30 +924,35 @@ static int tracehook_report_syscall(struct pt_regs *regs, if (dir == PTRACE_SYSCALL_EXIT) tracehook_report_syscall_exit(regs, 0); else if (tracehook_report_syscall_entry(regs)) - current_thread_info()->syscall = -1; + ret = -1; regs->ARM_ip = ip; - return current_thread_info()->syscall; + return ret; } asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno) { + int ret = 0; + + /* set up syscall, which may be changed in secure_computing */ current_thread_info()->syscall = scno; /* Do the secure computing check first; failures should be fast. */ if (secure_computing(scno) == -1) return -1; - if (test_thread_flag(TIF_SYSCALL_TRACE)) - scno = tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER); + if (test_thread_flag(TIF_SYSCALL_TRACE) && + tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER)) + ret = -1; if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) - trace_sys_enter(regs, scno); + trace_sys_enter(regs, current_thread_info()->syscall); - audit_syscall_entry(AUDIT_ARCH_ARM, scno, regs->ARM_r0, regs->ARM_r1, + audit_syscall_entry(AUDIT_ARCH_ARM, current_thread_info()->syscall, + regs->ARM_r0, regs->ARM_r1, regs->ARM_r2, regs->ARM_r3); - return scno; + return ret ?: current_thread_info()->syscall; } asmlinkage void syscall_trace_exit(struct pt_regs *regs)