From patchwork Tue Sep 9 04:49:59 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 37022 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ig0-f197.google.com (mail-ig0-f197.google.com [209.85.213.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 68C542054D for ; Tue, 9 Sep 2014 04:50:31 +0000 (UTC) Received: by mail-ig0-f197.google.com with SMTP id r2sf75308128igi.4 for ; Mon, 08 Sep 2014 21:50:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=aB9q1CzUbjF8Kp/sZ3BB+vv1pDRmiL3t4Hq118dB7ts=; b=NnIrCNPSARV/YxmKpW5bs48SJNbt4rk8RclQH6m/JUjFC/kh7Tn7Kcr8CiHQZr5qm2 knKt6FGOQEXQCxf5kqewJ0tLZW0eT2j/6FvPAKCqI4DylkMdX2NK9/dPdT1HYHFapogH f9YFykTGUBH74WPBsqjcGtwBEN5Uiq6cES5tz+LBS4j5ajJRkHQhcqR9yqJmiphMq+p6 exYgMGbOUtJyRpqAqo6IpPItW43o/z448Sn78mrUMUwID3T0poR6HGZj+4nowflRklYp S1e2T0HrJas9ndimVirlCGpoo25iidaVajtt0uBIcO3i7a3/yZBei/tatijD4iWam5fk uaDg== X-Gm-Message-State: ALoCoQkq0D5GhsBOhpE/uoorr0QnmTC+GKWcKHPI4j9wqtsmzayyN+Oe8KUKYpnQfkNId7QELcyO X-Received: by 10.50.66.236 with SMTP id i12mr14921360igt.5.1410238230957; Mon, 08 Sep 2014 21:50:30 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.19.203 with SMTP id 69ls1951578qgh.27.gmail; Mon, 08 Sep 2014 21:50:30 -0700 (PDT) X-Received: by 10.220.118.136 with SMTP id v8mr131684vcq.50.1410238230817; Mon, 08 Sep 2014 21:50:30 -0700 (PDT) Received: from mail-vc0-f177.google.com (mail-vc0-f177.google.com [209.85.220.177]) by mx.google.com with ESMTPS id rk7si4826256vcb.45.2014.09.08.21.50.30 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 08 Sep 2014 21:50:30 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.177 as permitted sender) client-ip=209.85.220.177; Received: by mail-vc0-f177.google.com with SMTP id hq11so16107076vcb.8 for ; Mon, 08 Sep 2014 21:50:30 -0700 (PDT) X-Received: by 10.221.29.5 with SMTP id rw5mr166383vcb.49.1410238230671; Mon, 08 Sep 2014 21:50:30 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.45.67 with SMTP id uj3csp229550vcb; Mon, 8 Sep 2014 21:50:30 -0700 (PDT) X-Received: by 10.66.168.165 with SMTP id zx5mr10908275pab.133.1410238229783; Mon, 08 Sep 2014 21:50:29 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id je2si21068940pbd.77.2014.09.08.21.50.29 for ; Mon, 08 Sep 2014 21:50:29 -0700 (PDT) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752166AbaIIEu1 (ORCPT + 26 others); Tue, 9 Sep 2014 00:50:27 -0400 Received: from mail-pd0-f176.google.com ([209.85.192.176]:40105 "EHLO mail-pd0-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751367AbaIIEu0 (ORCPT ); Tue, 9 Sep 2014 00:50:26 -0400 Received: by mail-pd0-f176.google.com with SMTP id y13so4453504pdi.7 for ; Mon, 08 Sep 2014 21:50:25 -0700 (PDT) X-Received: by 10.66.180.98 with SMTP id dn2mr51211219pac.83.1410238225562; Mon, 08 Sep 2014 21:50:25 -0700 (PDT) Received: from localhost.localdomain (KD182249088171.au-net.ne.jp. [182.249.88.171]) by mx.google.com with ESMTPSA id ic5sm10282665pbb.3.2014.09.08.21.50.19 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 08 Sep 2014 21:50:24 -0700 (PDT) From: AKASHI Takahiro To: linux@arm.linux.org.uk, will.deacon@arm.com Cc: viro@zeniv.linux.org.uk, eparis@redhat.com, rgb@redhat.com, dsaxena@linaro.org, linux-arm-kernel@lists.infradead.org, linaro-kernel@lists.linaro.org, linux-kernel@vger.kernel.org, linux-audit@redhat.com, AKASHI Takahiro Subject: [PATCH v2] arm: prevent BUG_ON in audit_syscall_entry() Date: Tue, 9 Sep 2014 13:49:59 +0900 Message-Id: <1410238199-956-1-git-send-email-takahiro.akashi@linaro.org> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: takahiro.akashi@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.177 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , BUG_ON() in audit_syscall_entry() will be hit if user issues syscall(-1) while syscall auditing is enabled (that is, by starting auditd). ------------[ cut here ]------------ kernel BUG at /home/akashi/arm/armv7/linux/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 ]--- In fact, syscall(-1) just fails (not signaled despite the expectation, this is another minor bug), but the succeeding syscall hits BUG_ON. When auditing syscall(-1), audit_syscall_entry() is called anyway, but audit_syscall_exit() is not called and then 'in_syscall' flag in thread's audit context is kept on. In this way, audit_syscall_entry() against the succeeding syscall will see BUG_ON(in_syscall). This patch fixes this bug by 1) preventing syscall tracing, ftrace tracepoint and audit, from being executed in case of invalid or pseudo system calls, 2) handling user-issued syscall(-1) with arm_syscall(). Signed-off-by: AKASHI Takahiro --- arch/arm/include/asm/traps.h | 1 + arch/arm/kernel/entry-common.S | 4 ++-- arch/arm/kernel/ptrace.c | 47 +++++++++++++++++++++++++--------------- 3 files changed, 32 insertions(+), 20 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..28d3931 100644 --- a/arch/arm/kernel/entry-common.S +++ b/arch/arm/kernel/entry-common.S @@ -426,7 +426,6 @@ ENTRY(vector_swi) local_restart: ldr r10, [tsk, #TI_FLAGS] @ check for syscall tracing stmdb sp!, {r4, r5} @ push fifth and sixth args - tst r10, #_TIF_SYSCALL_WORK @ are we tracing syscalls? bne __sys_trace @@ -476,10 +475,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..68b42cd 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,40 @@ 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); + /* user-issued syscall of -1 */ + if (scno == -1 && orig_scno == -1) + 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);