From patchwork Wed Oct 1 10:45:57 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 5010981 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 EFE9ABEEA6 for ; Wed, 1 Oct 2014 10:49:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E8C2620259 for ; Wed, 1 Oct 2014 10:49:14 +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 D59802021B for ; Wed, 1 Oct 2014 10:49:13 +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 1XZHQy-0004UG-R6; Wed, 01 Oct 2014 10:47:00 +0000 Received: from mail-pd0-f169.google.com ([209.85.192.169]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XZHQv-0004SN-UD for linux-arm-kernel@lists.infradead.org; Wed, 01 Oct 2014 10:46:58 +0000 Received: by mail-pd0-f169.google.com with SMTP id w10so63931pde.0 for ; Wed, 01 Oct 2014 03:46:35 -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; bh=kJgFUhImHDhgz59Gp5Whlp6W9lpujgj2Nqna4sAxZXs=; b=D4UOwaRRxp6PeUD+iICAi7VXEzhbjrnSBkG9i5rPNO3FHQ+TdkdLeuZnw1Ls+Xibh3 4aBJFtMz5EtH4blU0r1RCvim+06tDp6T7mnBQ+zz1kKiHKceUPkN12gqRwJTDhgmuqLO Xf4zZOIMFAhoUXbxizHEjCaYuhwecadCTioW7B8UBZ7KXiEGy7secwTAGAX94R74ivTd WBGRGee7OK+fFgOkEKixJBuh/9ROxyY71xmmHqrkxx8xskzchR/+6OuUxZ0xR+c4vUsv SwlVuPtkpvbm3NI89ErV3lwHzQeWnizj7aH/P26v/N9W3EifTcyJ8g/wy/gmggkaJaEQ 5Aqw== X-Gm-Message-State: ALoCoQkvmsqtbOP4jrrVT02o/BmuG/G4jS0fjaluLleyB4YFHnZdlExIcZsZHIDP5Gf6s4YjkBZW X-Received: by 10.66.237.132 with SMTP id vc4mr28742339pac.19.1412160395214; Wed, 01 Oct 2014 03:46:35 -0700 (PDT) Received: from localhost.localdomain (KD182249082047.au-net.ne.jp. [182.249.82.47]) by mx.google.com with ESMTPSA id sp2sm712036pbc.23.2014.10.01.03.46.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 01 Oct 2014 03:46:34 -0700 (PDT) From: AKASHI Takahiro To: linux@arm.linux.org.uk, will.deacon@arm.com Subject: [PATCH v2] arm: prevent BUG_ON in audit_syscall_entry() Date: Wed, 1 Oct 2014 19:45:57 +0900 Message-Id: <1412160357-11324-1-git-send-email-takahiro.akashi@linaro.org> X-Mailer: git-send-email 1.9.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20141001_034658_026549_9AD2CB70 X-CRM114-Status: GOOD ( 24.64 ) X-Spam-Score: -0.9 (/) Cc: linaro-kernel@lists.linaro.org, keescook@chromium.org, broonie@linaro.org, rgb@redhat.com, linux-kernel@vger.kernel.org, eparis@redhat.com, AKASHI Takahiro , dsaxena@linaro.org, viro@zeniv.linux.org.uk, linux-audit@redhat.com, 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.5 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 BUG_ON() in audit_syscall_entry() is hit under the following conditions: 1) syscall auditing is enabled (that is, by running auditd). 2) user process issues syscall(-1) In fact, syscall(-1) just fails (not raising SIGILL, that is an expected behavior if not audited), and the succeeding syscall will hit this BUG_ON. ------------[ cut here ]------------ kernel BUG at (...)/kernel/auditsc.c:1534! Internal error: Oops - BUG: 0 [#1] SMP ARM Modules linked in: CPU: 0 PID: 61 Comm: syscall_arm Not tainted 3.17.0-rc4 #60 task: ea0c4380 ti: ea48e000 task.ti: ea48e000 PC is at __audit_syscall_entry+0xe4/0x110 LR is at 0xea0c4380 pc : [] lr : [] psr: 20000013 sp : ea48ff68 ip : 00000001 fp : 0000869c r10: 00000200 r9 : ea48e000 r8 : c020f4e4 r7 : 000000c5 r6 : ea48e000 r5 : ea48ffb0 r4 : ea490800 r3 : bef1e718 r2 : 00000001 r1 : 000000c5 r0 : 40000028 Flags: nzCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user Control: 10c5387d Table: 8a4ac059 DAC: 00000015 Process syscall_arm (pid: 61, stack limit = 0xea48e250) Stack: (0xea48ff68 to 0xea490000) ff60: 540e7a69 3ab5e840 00000200 000000c5 ea48ffb0 ea48e000 ff80: 000000c5 c02114bc bef1e718 00000001 b6efdb58 ffffffff 08400000 000000c5 ffa0: c020f4e4 c020f49c b6efdb58 ffffffff 00000001 bef1e718 bef1e718 00000001 ffc0: b6efdb58 ffffffff 08400000 000000c5 00000000 b6f22850 00000008 0000869c ffe0: 000000c5 bef1e704 b6ea315f b6e318e6 20000030 00000001 00000000 00000000 [] (__audit_syscall_entry) from [] (syscall_trace_enter+0xf0/0x120) [] (syscall_trace_enter) from [] (__sys_trace+0xc/0x38) Code: e584500c e5842004 e28dd00c e8bd80f0 (e7f001f2) ---[ end trace 0c4441660aba5692 ]--- Under the current implementation, audit_syscall_entry() is called even against syscall(-1), but audit_syscall_exit() is not. Because 'in_syscall' flag in a current thread's audit context is kept on, next called audit_syscall_entry() will see BUG_ON(in_syscall). This patch fixes this bug by 1) preventing syscall enter/exit tracing, ftrace tracepoint and audit, from being executed if an invalid (including -1) or pseudo syscall number (starting from __NR_ARM_BASE + 1). Please note that tracehook_report_syscall(PTRACE_SYSCALL_EXIT) is still executed as it should be paired with tracehook_report_syscall(PTRACE_SYSCALL_ENTER). 2) handling user-issued syscall(-1) with arm_syscall() for compatibility if tracing is on. Since UL(-1) > __NR_ARM_BASE - __NR_SYSCALL_BASE, arm_syscall() is called at local_restart in entry-common.S if tracing is off. Signed-off-by: AKASHI Takahiro Reviewed-by: Kees Cook --- arch/arm/include/asm/traps.h | 1 + arch/arm/kernel/entry-common.S | 3 ++- arch/arm/kernel/ptrace.c | 49 +++++++++++++++++++++++++--------------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/arch/arm/include/asm/traps.h b/arch/arm/include/asm/traps.h index f555bb3..de01145 100644 --- a/arch/arm/include/asm/traps.h +++ b/arch/arm/include/asm/traps.h @@ -49,6 +49,7 @@ static inline int in_exception_text(unsigned long ptr) extern void __init early_trap_init(void *); extern void dump_backtrace_entry(unsigned long where, unsigned long from, unsigned long frame); extern void ptrace_break(struct task_struct *tsk, struct pt_regs *regs); +extern int arm_syscall(int no, struct pt_regs *regs); extern void *vectors_page; diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S index e52fe5a..4c62324 100644 --- a/arch/arm/kernel/entry-common.S +++ b/arch/arm/kernel/entry-common.S @@ -476,10 +476,11 @@ __sys_trace: cmp scno, #-1 @ skip the syscall? bne 2b add sp, sp, #S_OFF @ restore stack - b ret_slow_syscall + b __sys_trace_return_skipped __sys_trace_return: str r0, [sp, #S_R0 + S_OFF]! @ save returned r0 +__sys_trace_return_skipped: mov r0, sp bl syscall_trace_exit b ret_slow_syscall diff --git a/arch/arm/kernel/ptrace.c b/arch/arm/kernel/ptrace.c index 0c27ed6..d458367 100644 --- a/arch/arm/kernel/ptrace.c +++ b/arch/arm/kernel/ptrace.c @@ -930,7 +930,9 @@ static void tracehook_report_syscall(struct pt_regs *regs, asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno) { - current_thread_info()->syscall = scno; + int orig_scno; + + current_thread_info()->syscall = orig_scno = scno; /* Do the secure computing check first; failures should be fast. */ if (secure_computing(scno) == -1) @@ -941,31 +943,42 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno) scno = current_thread_info()->syscall; - if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) - trace_sys_enter(regs, scno); + if (scno >= 0 && scno < NR_syscalls) { + if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) + trace_sys_enter(regs, scno); - audit_syscall_entry(AUDIT_ARCH_ARM, scno, regs->ARM_r0, regs->ARM_r1, - regs->ARM_r2, regs->ARM_r3); + audit_syscall_entry(AUDIT_ARCH_ARM, scno, + regs->ARM_r0, regs->ARM_r1, + regs->ARM_r2, regs->ARM_r3); + } else if (scno == -1 && orig_scno == -1) { + /* + * For compatibility, we handles user-issued syscall(-1) + * here with arm_syscall(). + */ + arm_syscall(scno, regs); + } return scno; } asmlinkage void syscall_trace_exit(struct pt_regs *regs) { - /* - * Audit the syscall before anything else, as a debugger may - * come in and change the current registers. - */ - audit_syscall_exit(regs); + if (current_thread_info()->syscall < NR_syscalls) { + /* + * Audit the syscall before anything else, as a debugger may + * come in and change the current registers. + */ + audit_syscall_exit(regs); - /* - * Note that we haven't updated the ->syscall field for the - * current thread. This isn't a problem because it will have - * been set on syscall entry and there hasn't been an opportunity - * for a PTRACE_SET_SYSCALL since then. - */ - if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) - trace_sys_exit(regs, regs_return_value(regs)); + /* + * Note that we haven't updated the ->syscall field for the + * current thread. This isn't a problem because it will have + * been set on syscall entry and there hasn't been + * an opportunity for a PTRACE_SET_SYSCALL since then. + */ + if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) + trace_sys_exit(regs, regs_return_value(regs)); + } if (test_thread_flag(TIF_SYSCALL_TRACE)) tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);