From patchwork Wed Jul 30 07:12:45 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 4646111 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 96939C0338 for ; Wed, 30 Jul 2014 07:15:36 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BE95020107 for ; Wed, 30 Jul 2014 07:15:35 +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 98554200C1 for ; Wed, 30 Jul 2014 07:15:34 +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 1XCO4n-0005tP-W0; Wed, 30 Jul 2014 07:13:29 +0000 Received: from mail-pd0-f170.google.com ([209.85.192.170]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XCO4l-0005pw-KQ for linux-arm-kernel@lists.infradead.org; Wed, 30 Jul 2014 07:13:28 +0000 Received: by mail-pd0-f170.google.com with SMTP id g10so982365pdj.29 for ; Wed, 30 Jul 2014 00:12:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-type:content-disposition:user-agent; bh=RgoWj88ypdJQ+GsYc0jyVmR5yWPYcVNoN8cC8arjOvo=; b=M2a2EmLVzYI3ig9UTb8xGQv7NX6JRdwLuoQtZSWcC0KFSJtb9L9ItNniZ4NuNgfkay mq0vRMxQCtyDUX8JaXfKxcSI5PbzfTI+cq5nNlbTC2wVMTbLr8WoJV6hnIFQuVidhTYP 6EfMzJ/9DjCYkfIazu7u61KmGpa2EkXThSDY0JajbnFfIf27mizwD1549nBfDR4JIxgi AtUGPuF/HtLgB28ZEVzitSi8QlMDQrsCh9LAAAucA81P4WgQqIKAYT5FMNy2VXQmfscu KpTLJwZMBAS0KFu6VpDkqcfR4bm3ZK9UDrTtHe7TRSswqiD3vPNTrxAiH4pwEToTDo2v vwaw== X-Gm-Message-State: ALoCoQm17dxlUnTwhvGoBO4zWirNiiuyfyBD1dDEPwX84mSFiQzgqJcxLoQt5IAY7lTyd9Q5Et+4 X-Received: by 10.66.229.98 with SMTP id sp2mr2277765pac.95.1406704368787; Wed, 30 Jul 2014 00:12:48 -0700 (PDT) Received: from mew.web-pass.com ([208.87.58.227]) by mx.google.com with ESMTPSA id tu10sm1274752pbc.43.2014.07.30.00.12.47 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Jul 2014 00:12:48 -0700 (PDT) Date: Wed, 30 Jul 2014 00:12:45 -0700 From: Omar Sandoval To: linux-arm-kernel@lists.infradead.org, linux@arm.linux.org.uk, catalin.marinas@arm.com, will.deacon@arm.com Subject: [PATCH] ARM/ARM64: don't enter kgdb when userspace executes a kgdb break instruction. Message-ID: <20140730071245.GA8954@mew.web-pass.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140730_001327_695257_4C6B5B84 X-CRM114-Status: GOOD ( 14.38 ) X-Spam-Score: -1.4 (-) Cc: linux-kernel@vger.kernel.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: , 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.6 required=5.0 tests=BAYES_00,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 The kgdb breakpoint hooks (kgdb_brk_fn and kgdb_compiled_brk_fn) should only be entered when a kgdb break instruction is executed from the kernel. Otherwise, if kgdb is enabled, a userspace program can cause the kernel to drop into the debugger by executing either KGDB_BREAKINST or KGDB_COMPILED_BREAK on ARM, or brk #KGDB_{DYN,COMPILED}_DGB_BRK_IMM on ARM64. Signed-off-by: Omar Sandoval --- The following program reproduces the fixed problem on ARM: .globl _start _start: udf #65006 @ KGDB_BREAKINST And on ARM64: .globl _start _start: brk #0x400 @ KGDB_DYN_DGB_BRK_IMM arch/arm/kernel/kgdb.c | 4 ++++ arch/arm64/include/asm/debug-monitors.h | 4 +++- arch/arm64/kernel/debug-monitors.c | 3 ++- arch/arm64/kernel/kgdb.c | 4 ++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/arm/kernel/kgdb.c b/arch/arm/kernel/kgdb.c index 778c2f7..a74b53c 100644 --- a/arch/arm/kernel/kgdb.c +++ b/arch/arm/kernel/kgdb.c @@ -160,12 +160,16 @@ static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr) static struct undef_hook kgdb_brkpt_hook = { .instr_mask = 0xffffffff, .instr_val = KGDB_BREAKINST, + .cpsr_mask = MODE_MASK, + .cpsr_val = SVC_MODE, .fn = kgdb_brk_fn }; static struct undef_hook kgdb_compiled_brkpt_hook = { .instr_mask = 0xffffffff, .instr_val = KGDB_COMPILED_BREAK, + .cpsr_mask = MODE_MASK, + .cpsr_val = SVC_MODE, .fn = kgdb_compiled_brk_fn }; diff --git a/arch/arm64/include/asm/debug-monitors.h b/arch/arm64/include/asm/debug-monitors.h index 6e9b5b3..e1d27ce 100644 --- a/arch/arm64/include/asm/debug-monitors.h +++ b/arch/arm64/include/asm/debug-monitors.h @@ -105,8 +105,10 @@ void unregister_step_hook(struct step_hook *hook); struct break_hook { struct list_head node; - u32 esr_val; u32 esr_mask; + u32 esr_val; + u64 pstate_mask; + u64 pstate_val; int (*fn)(struct pt_regs *regs, unsigned int esr); }; diff --git a/arch/arm64/kernel/debug-monitors.c b/arch/arm64/kernel/debug-monitors.c index a7fb874..594fac4 100644 --- a/arch/arm64/kernel/debug-monitors.c +++ b/arch/arm64/kernel/debug-monitors.c @@ -303,7 +303,8 @@ static int call_break_hook(struct pt_regs *regs, unsigned int esr) read_lock(&break_hook_lock); list_for_each_entry(hook, &break_hook, node) - if ((esr & hook->esr_mask) == hook->esr_val) + if ((esr & hook->esr_mask) == hook->esr_val && + (regs->pstate & hook->pstate_mask) == hook->pstate_val) fn = hook->fn; read_unlock(&break_hook_lock); diff --git a/arch/arm64/kernel/kgdb.c b/arch/arm64/kernel/kgdb.c index 75c9cf1..80eb035 100644 --- a/arch/arm64/kernel/kgdb.c +++ b/arch/arm64/kernel/kgdb.c @@ -236,12 +236,16 @@ static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr) static struct break_hook kgdb_brkpt_hook = { .esr_mask = 0xffffffff, .esr_val = DBG_ESR_VAL_BRK(KGDB_DYN_DGB_BRK_IMM), + .pstate_mask = PSR_MODE_MASK, + .pstate_val = PSR_MODE_EL1h, .fn = kgdb_brk_fn }; static struct break_hook kgdb_compiled_brkpt_hook = { .esr_mask = 0xffffffff, .esr_val = DBG_ESR_VAL_BRK(KDBG_COMPILED_DBG_BRK_IMM), + .pstate_mask = PSR_MODE_MASK, + .pstate_val = PSR_MODE_EL1h, .fn = kgdb_compiled_brk_fn };