Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1519375ybl; Thu, 22 Aug 2019 16:18:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqzLAcqA6Y9owWQxpETG5wVOUIw9Hfn3xRzlK3OFyKrjOspty0r/vTRPtdO/4DSVld6i8PR9 X-Received: by 2002:a17:90a:ec07:: with SMTP id l7mr2142126pjy.39.1566515886170; Thu, 22 Aug 2019 16:18:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566515886; cv=none; d=google.com; s=arc-20160816; b=LCqQBJkbGRHH5u3bw9YT8hABTVMJ96A++CjeJP0bgyTJIIWcy+bt666evzvHHygX4J o4X5vul3wxnuy2ie0KjYStGt3PcFMCZ6PkVSVtTbskw2QM9FXcciJFcq2oEIJ0ywTp88 Gsn2JQsCUsOT206Fr+XqGXh0nDSzO251Fb9Wv4FjoYOvKsqvXgeR1L/hyBP8SjmxfRle YGoZCVUy7bl/mrTvr3AQhgo+3nugxj0NkreonwVioe3yog98wBnWhGP7IQ96JvV/AYvm P6qFXxjy3hCFpwRnLqh9TcPSSUO30OZFJsMnboyXq8QezvboEiT5lFgzbHjk46MDHwt9 4SZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+X1Evx50u6QycCoI/gQ/EPq+c7thlsIYOFyOSFBNUf0=; b=EzaSGNZOCukXvOTHRUxxJ33gpoGHr5klC6N+TgQMg40qCE48IQmTfkIjuIV9uyYEz1 34Aq9kxG2ZJwW2dxzKSFv/spy+PiKvjv75t4h3DUpZ0ICSj9p0C7FTWmNBRBBb9cPfA0 tZwvqY1hPqqIaKVtrn0CkOy6C2Po3TzbAXE1SS132xjpZ8tayQfDdhGC/S5efvTyk5PH j4/UyhYoNpxzNVigXO1nxuTz51cHXr3d0VsPhrDg4w9JkokNrncQ4ftOFTx+80QkAPel Ic8ohsvq0Wvqv5rvjfjVp8c1hYHcJcqt4L0ijlWRK5ycjn3gxxLB6vsg8z176NIElkTF 99Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=y6sA4NQU; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id fr6si821965pjb.59.2019.08.22.16.17.51; Thu, 22 Aug 2019 16:18:06 -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; dkim=pass header.i=@kernel.org header.s=default header.b=y6sA4NQU; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391057AbfHVRNa (ORCPT + 99 others); Thu, 22 Aug 2019 13:13:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:59064 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390608AbfHVRJB (ORCPT ); Thu, 22 Aug 2019 13:09:01 -0400 Received: from sasha-vm.mshome.net (wsip-184-188-36-2.sd.sd.cox.net [184.188.36.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 65AE923406; Thu, 22 Aug 2019 17:08:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1566493739; bh=vflVOD8EJIT9+3S/Swb39tn2qxmGSfyQdQ1zzz8gUfs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=y6sA4NQUm5UPbjtjngZqGUMfl2wrjFJ/rpnXPVm+ZS3KhvUVpKe4XvsxuZxCdk336 NOWxCcS19VvXvKBK/gMBePJNqC6qbg4iPym/70NvthRFKR0wmILgx7LUa2V8tLHcS8 X/vsYbW5Z8h5e3N7r8Wt/UQZP0RDLM/yq0JCfEMw= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Masami Hiramatsu , Naresh Kamboju , James Morse , Will Deacon , Sasha Levin Subject: [PATCH 5.2 084/135] arm64: kprobes: Recover pstate.D in single-step exception handler Date: Thu, 22 Aug 2019 13:07:20 -0400 Message-Id: <20190822170811.13303-85-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190822170811.13303-1-sashal@kernel.org> References: <20190822170811.13303-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.2.10-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.2.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.2.10-rc1 X-KernelTest-Deadline: 2019-08-24T17:07+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu [ Upstream commit b3980e48528c4d2a9e70b145a5bba328b73a0f93 ] kprobes manipulates the interrupted PSTATE for single step, and doesn't restore it. Thus, if we put a kprobe where the pstate.D (debug) masked, the mask will be cleared after the kprobe hits. Moreover, in the most complicated case, this can lead a kernel crash with below message when a nested kprobe hits. [ 152.118921] Unexpected kernel single-step exception at EL1 When the 1st kprobe hits, do_debug_exception() will be called. At this point, debug exception (= pstate.D) must be masked (=1). But if another kprobes hits before single-step of the first kprobe (e.g. inside user pre_handler), it unmask the debug exception (pstate.D = 0) and return. Then, when the 1st kprobe setting up single-step, it saves current DAIF, mask DAIF, enable single-step, and restore DAIF. However, since "D" flag in DAIF is cleared by the 2nd kprobe, the single-step exception happens soon after restoring DAIF. This has been introduced by commit 7419333fa15e ("arm64: kprobe: Always clear pstate.D in breakpoint exception handler") To solve this issue, this stores all DAIF bits and restore it after single stepping. Reported-by: Naresh Kamboju Fixes: 7419333fa15e ("arm64: kprobe: Always clear pstate.D in breakpoint exception handler") Reviewed-by: James Morse Tested-by: James Morse Signed-off-by: Masami Hiramatsu Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- arch/arm64/include/asm/daifflags.h | 2 ++ arch/arm64/kernel/probes/kprobes.c | 40 +++++------------------------- 2 files changed, 8 insertions(+), 34 deletions(-) diff --git a/arch/arm64/include/asm/daifflags.h b/arch/arm64/include/asm/daifflags.h index ae7e605085d71..9c0e0178ea291 100644 --- a/arch/arm64/include/asm/daifflags.h +++ b/arch/arm64/include/asm/daifflags.h @@ -13,6 +13,8 @@ #define DAIF_PROCCTX 0 #define DAIF_PROCCTX_NOIRQ PSR_I_BIT #define DAIF_ERRCTX (PSR_I_BIT | PSR_A_BIT) +#define DAIF_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT) + /* mask/save/unmask/restore all exceptions, including interrupts. */ static inline void local_daif_mask(void) diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c index 88ce502c8e6f1..624f2501f3f87 100644 --- a/arch/arm64/kernel/probes/kprobes.c +++ b/arch/arm64/kernel/probes/kprobes.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -165,33 +166,6 @@ static void __kprobes set_current_kprobe(struct kprobe *p) __this_cpu_write(current_kprobe, p); } -/* - * When PSTATE.D is set (masked), then software step exceptions can not be - * generated. - * SPSR's D bit shows the value of PSTATE.D immediately before the - * exception was taken. PSTATE.D is set while entering into any exception - * mode, however software clears it for any normal (none-debug-exception) - * mode in the exception entry. Therefore, when we are entering into kprobe - * breakpoint handler from any normal mode then SPSR.D bit is already - * cleared, however it is set when we are entering from any debug exception - * mode. - * Since we always need to generate single step exception after a kprobe - * breakpoint exception therefore we need to clear it unconditionally, when - * we become sure that the current breakpoint exception is for kprobe. - */ -static void __kprobes -spsr_set_debug_flag(struct pt_regs *regs, int mask) -{ - unsigned long spsr = regs->pstate; - - if (mask) - spsr |= PSR_D_BIT; - else - spsr &= ~PSR_D_BIT; - - regs->pstate = spsr; -} - /* * Interrupts need to be disabled before single-step mode is set, and not * reenabled until after single-step mode ends. @@ -203,17 +177,17 @@ spsr_set_debug_flag(struct pt_regs *regs, int mask) static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb, struct pt_regs *regs) { - kcb->saved_irqflag = regs->pstate; + kcb->saved_irqflag = regs->pstate & DAIF_MASK; regs->pstate |= PSR_I_BIT; + /* Unmask PSTATE.D for enabling software step exceptions. */ + regs->pstate &= ~PSR_D_BIT; } static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb, struct pt_regs *regs) { - if (kcb->saved_irqflag & PSR_I_BIT) - regs->pstate |= PSR_I_BIT; - else - regs->pstate &= ~PSR_I_BIT; + regs->pstate &= ~DAIF_MASK; + regs->pstate |= kcb->saved_irqflag; } static void __kprobes @@ -250,8 +224,6 @@ static void __kprobes setup_singlestep(struct kprobe *p, set_ss_context(kcb, slot); /* mark pending ss */ - spsr_set_debug_flag(regs, 0); - /* IRQs and single stepping do not mix well. */ kprobes_save_local_irqflag(kcb, regs); kernel_enable_single_step(regs); -- 2.20.1