From patchwork Thu Oct 2 09:46:12 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 5017381 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CF7619F32B for ; Thu, 2 Oct 2014 09:49:32 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E4DC82027D for ; Thu, 2 Oct 2014 09:49:31 +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 EC7B320270 for ; Thu, 2 Oct 2014 09:49:30 +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 1XZcz7-00071x-6Y; Thu, 02 Oct 2014 09:47:41 +0000 Received: from mail-pd0-f171.google.com ([209.85.192.171]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XZcz3-0006lw-R1 for linux-arm-kernel@lists.infradead.org; Thu, 02 Oct 2014 09:47:38 +0000 Received: by mail-pd0-f171.google.com with SMTP id ft15so1733876pdb.16 for ; Thu, 02 Oct 2014 02:47:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=nYQdwlTJnkbXIw7EAf6BFaSJUPhos6Dul2jaj7gMq5w=; b=RW3o578NAXhT89CJPVepzqCKAhWUHatsXs8ldGzseyMh55jnGzo2jiINri1SLdxCej weGGL1dtJ7tVkvVgcmbNogzPfS1hbHYQWtWnvkfvKfhUwpbekKuz+7gpRz2XNuT0O2WY eu0raB9ZvYsbLmHgFZcoT9udTYlomv4UQ4iIOV2lNUdtE5Oog+2zQKqYTDYVq4kmFKtm LZz92+eZWXcJ6UIca7/m2FnPyD5sffNiZFxZIntR1U0mGoBDE6k2NDMlrakkkpuVNAT7 LF2w9lGKRfrz1+V3tNRKrYfJY9Us6D5vBbqe72AjqEwpQsSjXY+If4Z42jCf61lURqwq Fdtg== X-Gm-Message-State: ALoCoQmqDp+/lmEl3FBND7nU9hUsgRBi8ZS1kgVLP1uo++5/bIBJk6NGzQtDcMrO4E9XdjS0dP8s X-Received: by 10.69.27.71 with SMTP id je7mr2875626pbd.155.1412243236418; Thu, 02 Oct 2014 02:47:16 -0700 (PDT) Received: from localhost.localdomain (KD182249093205.au-net.ne.jp. [182.249.93.205]) by mx.google.com with ESMTPSA id w7sm3013036pbs.4.2014.10.02.02.47.10 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 02 Oct 2014 02:47:15 -0700 (PDT) From: AKASHI Takahiro To: keescook@chromium.org, catalin.marinas@arm.com, will.deacon@arm.com Subject: [PATCH v7 2/6] arm64: ptrace: allow tracer to skip a system call Date: Thu, 2 Oct 2014 18:46:12 +0900 Message-Id: <1412243176-16192-3-git-send-email-takahiro.akashi@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1412243176-16192-1-git-send-email-takahiro.akashi@linaro.org> References: <1412243176-16192-1-git-send-email-takahiro.akashi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20141002_024737_900397_72A5B021 X-CRM114-Status: GOOD ( 19.22 ) X-Spam-Score: -0.9 (/) Cc: linaro-kernel@lists.linaro.org, arndb@arndb.de, linux-kernel@vger.kernel.org, AKASHI Takahiro , dsaxena@linaro.org, linux-arm-kernel@lists.infradead.org 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: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, 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 If tracer specifies -1 as a syscall number, this traced system call should be skipped with a value in x0 used as a return value. This patch implements this semantics, but there is one restriction here: when syscall(-1) is issued by user, tracer cannot skip this system call and modify a return value at syscall entry. In order to ease this flavor, we need to take whatever value x0 has as a return value, but this might result in a bogus value being returned, especially when tracer doesn't do anything against this syscall. So we always return ENOSYS instead, while we still have another chance to change a return value at syscall exit. Please also note: * syscall entry tracing and syscall exit tracing (ftrace tracepoint and audit) are always executed, if enabled, even when skipping a system call (that is, -1). In this way, we can avoid a potential bug where audit_syscall_entry() might be called without audit_syscall_exit() at the previous system call being called, that would cause OOPs in audit_syscall_entry(). * syscallno may also be set to -1 if a fatal signal (SIGKILL) is detected in tracehook_report_syscall_entry(), but since a value set to x0 (ENOSYS) is not used in this case, we may neglect the case. Signed-off-by: AKASHI Takahiro --- arch/arm64/include/asm/ptrace.h | 8 ++++++++ arch/arm64/kernel/entry.S | 4 ++++ arch/arm64/kernel/ptrace.c | 23 ++++++++++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/ptrace.h b/arch/arm64/include/asm/ptrace.h index 41ed9e1..736ebc3 100644 --- a/arch/arm64/include/asm/ptrace.h +++ b/arch/arm64/include/asm/ptrace.h @@ -65,6 +65,14 @@ #define COMPAT_PT_TEXT_ADDR 0x10000 #define COMPAT_PT_DATA_ADDR 0x10004 #define COMPAT_PT_TEXT_END_ADDR 0x10008 + +/* + * System call will be skipped if a syscall number is changed to -1 + * with ptrace(PTRACE_SET_SYSCALL). + * Upper 32-bit should be ignored for safe check. + */ +#define IS_SKIP_SYSCALL(no) ((int)(no & 0xffffffff) == -1) + #ifndef __ASSEMBLY__ /* sizeof(struct user) for AArch32 */ diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S index f0b5e51..b53a1c5 100644 --- a/arch/arm64/kernel/entry.S +++ b/arch/arm64/kernel/entry.S @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -671,6 +672,8 @@ ENDPROC(el0_svc) __sys_trace: mov x0, sp bl syscall_trace_enter + cmp w0, #-1 // skip the syscall? + b.eq __sys_trace_return_skipped adr lr, __sys_trace_return // return address uxtw scno, w0 // syscall number (possibly new) mov x1, sp // pointer to regs @@ -685,6 +688,7 @@ __sys_trace: __sys_trace_return: str x0, [sp] // save returned x0 +__sys_trace_return_skipped: mov x0, sp bl syscall_trace_exit b ret_to_user diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c index 2842f9f..6b11c6a 100644 --- a/arch/arm64/kernel/ptrace.c +++ b/arch/arm64/kernel/ptrace.c @@ -1126,6 +1126,8 @@ static void tracehook_report_syscall(struct pt_regs *regs, asmlinkage int syscall_trace_enter(struct pt_regs *regs) { + unsigned int orig_syscallno = regs->syscallno; + if (test_thread_flag(TIF_SYSCALL_TRACE)) tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER); @@ -1133,7 +1135,26 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs) trace_sys_enter(regs, regs->syscallno); audit_syscall_entry(syscall_get_arch(), regs->syscallno, - regs->orig_x0, regs->regs[1], regs->regs[2], regs->regs[3]); + regs->orig_x0, regs->regs[1], + regs->regs[2], regs->regs[3]); + + if (IS_SKIP_SYSCALL(regs->syscallno) && + IS_SKIP_SYSCALL(orig_syscallno)) { + /* + * For compatibility, we handles user-issued syscall(-1). + * + * RESTRICTION: we can't modify a return value here in this + * specific case. In order to ease this flavor, we have to + * take whatever value x0 has as a return value, but this + * might result in a bogus value being returned. + * + * NOTE: syscallno may also be set to -1 if fatal signal + * is detected in tracehook_report_syscall(ENTRY), + * but since a value set to x0 here is not used in this + * case, we may neglect the case. + */ + regs->regs[0] = -ENOSYS; + } return regs->syscallno; }