From patchwork Wed Apr 3 10:33:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 161693 Delivered-To: patch@linaro.org Received: by 2002:a02:c6d8:0:0:0:0:0 with SMTP id r24csp152103jan; Wed, 3 Apr 2019 03:33:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqxabQiw1MYrHFmxqDAbnA5XuBxxQED7lS60Ntm0s5PqoyoPDPh6J3ATFX3/O3FfhnqNQQ/J X-Received: by 2002:a62:292:: with SMTP id 140mr42058787pfc.206.1554287622629; Wed, 03 Apr 2019 03:33:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554287622; cv=none; d=google.com; s=arc-20160816; b=I+jUbNFWcFi7nMbBdMz5SvFpwgdBZZrxq5bhMWbFKkDGxcdJMsISmJJmRgr0AcfNCd VNESMoTAyqwqrEMBSoNKsuoltJ0DM4dk3NmBRoNJkiULEN26hP2jRAiRiStu5UL6FSIu 4DBuL0WuwMd5L5kPcTvThNJwggwtCpeQMu97STVxd5/acujryqKveVzciFT0HZa1WQOa VX+iONbVJWa6CU+SvdwstIetVhxcpILZwPFmXLSMS6TXWpaUUdxc5GScL3ezQSJ9ESTB rbeMmgh6FEWcALvZEzTAh2jLCglTYtBV8j4rIcVOx4Y0wu41TKIp2596ksaey0wUXtH4 p0jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=2mkNyYI46nKMvOAnfUog7fv/DsjyDs8+FmKUSxEGt0w=; b=W0dtVGiVTH5pRJlTgK+h4M/hS2+nLtaGadNz8iQd2Oslr0U5OfjAI0U47YiK1W1X6R UDMlMIFhZr703OX7RwQaLc7dOSvLVBG8Qs6N6IDGdzaOt3MAkJil8eKwcBhC6ghXNPVZ 41Fk9OXrTiIzqMAHSKGFndK/jON+sx4UFnaPWGq69Qjn8Lftc+kJJXUIBMn+yPRdP39P rFVcsdIrE7Z6CBo2e9Uh7nE4ymGvvHw/Mq1R1uLjpIxW5aIvksmaVvoZylTCJxeSYWBa GULgOWlGG5n4yYBePPzI1spa0CKajW5SD18WkJIv/rGHGV8aMeHDYLfxo+bvJ9PciwrL 8hAA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i26si13792232pfd.140.2019.04.03.03.33.42; Wed, 03 Apr 2019 03:33:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726547AbfDCKdl (ORCPT + 31 others); Wed, 3 Apr 2019 06:33:41 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:37038 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726199AbfDCKdi (ORCPT ); Wed, 3 Apr 2019 06:33:38 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id AB9551596; Wed, 3 Apr 2019 03:33:37 -0700 (PDT) Received: from fuggles.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 785CC3F59C; Wed, 3 Apr 2019 03:33:36 -0700 (PDT) From: Will Deacon To: gregkh@linuxfoundation.org Cc: stable@vger.kernel.org, linux-kernel@vger.kernel.org, sashal@kernel.org, Will Deacon , Catalin Marinas Subject: [PATCH 4.9 BACKPORT 2/2] arm64: debug: Ensure debug handlers check triggering exception level Date: Wed, 3 Apr 2019 11:33:26 +0100 Message-Id: <20190403103326.29161-2-will.deacon@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190403103326.29161-1-will.deacon@arm.com> References: <20190403103326.29161-1-will.deacon@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit 6bd288569b50bc89fa5513031086746968f585cb upstream. Debug exception handlers may be called for exceptions generated both by user and kernel code. In many cases, this is checked explicitly, but in other cases things either happen to work by happy accident or they go slightly wrong. For example, executing 'brk #4' from userspace will enter the kprobes code and be ignored, but the instruction will be retried forever in userspace instead of delivering a SIGTRAP. Fix this issue in the most stable-friendly fashion by simply adding explicit checks of the triggering exception level to all of our debug exception handlers. Cc: # 4.9.y Reviewed-by: Mark Rutland Signed-off-by: Will Deacon Signed-off-by: Catalin Marinas --- arch/arm64/kernel/kgdb.c | 15 ++++++++++++--- arch/arm64/kernel/probes/kprobes.c | 6 ++++++ 2 files changed, 18 insertions(+), 3 deletions(-) -- 2.11.0 diff --git a/arch/arm64/kernel/kgdb.c b/arch/arm64/kernel/kgdb.c index e017a9493b92..72a660a74ff9 100644 --- a/arch/arm64/kernel/kgdb.c +++ b/arch/arm64/kernel/kgdb.c @@ -231,24 +231,33 @@ int kgdb_arch_handle_exception(int exception_vector, int signo, static int kgdb_brk_fn(struct pt_regs *regs, unsigned int esr) { + if (user_mode(regs)) + return DBG_HOOK_ERROR; + kgdb_handle_exception(1, SIGTRAP, 0, regs); - return 0; + return DBG_HOOK_HANDLED; } NOKPROBE_SYMBOL(kgdb_brk_fn) static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int esr) { + if (user_mode(regs)) + return DBG_HOOK_ERROR; + compiled_break = 1; kgdb_handle_exception(1, SIGTRAP, 0, regs); - return 0; + return DBG_HOOK_HANDLED; } NOKPROBE_SYMBOL(kgdb_compiled_brk_fn); static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr) { + if (user_mode(regs)) + return DBG_HOOK_ERROR; + kgdb_handle_exception(1, SIGTRAP, 0, regs); - return 0; + return DBG_HOOK_HANDLED; } NOKPROBE_SYMBOL(kgdb_step_brk_fn); diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c index d2b1b624ddc3..17f647103ed7 100644 --- a/arch/arm64/kernel/probes/kprobes.c +++ b/arch/arm64/kernel/probes/kprobes.c @@ -450,6 +450,9 @@ kprobe_single_step_handler(struct pt_regs *regs, unsigned int esr) struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); int retval; + if (user_mode(regs)) + return DBG_HOOK_ERROR; + /* return error if this is not our step */ retval = kprobe_ss_hit(kcb, instruction_pointer(regs)); @@ -466,6 +469,9 @@ kprobe_single_step_handler(struct pt_regs *regs, unsigned int esr) int __kprobes kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr) { + if (user_mode(regs)) + return DBG_HOOK_ERROR; + kprobe_handler(regs); return DBG_HOOK_HANDLED; }