From patchwork Tue Aug 2 06:04:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 12934243 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DBDB6C00140 for ; Tue, 2 Aug 2022 06:04:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232258AbiHBGEZ (ORCPT ); Tue, 2 Aug 2022 02:04:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230298AbiHBGEZ (ORCPT ); Tue, 2 Aug 2022 02:04:25 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C7C4286CA; Mon, 1 Aug 2022 23:04:24 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id ED0F6B8188E; Tue, 2 Aug 2022 06:04:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E4C8C433D6; Tue, 2 Aug 2022 06:04:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1659420261; bh=+6zmFLLHIbJjwGJi7DLWYs/CmADKhX1mr48SeyTUrIo=; h=From:To:Cc:Subject:Date:From; b=aeruu50D6SHH/8KwA8HlbirGEEbEVb4Yc7tDp7GvSxj+mro9TLsGOJ4hCsNlrQFEI Xr1tVk9OHK4TL7duNeBfqUs5QJn66jYswheoV+rBrtehtmXO9jdgJDmM4kG9rA7qUX WD9gOsOpO9hq6UsRiurX2SQWcZNRqbJFeldyHWVspLHOJbbpzbC1nnrWCPjTlRFa66 T3GReenx/W5HCplPFWI2NkYSxHUFtGWDSEqK8ZAQKedL1TVfookKjNuqiqBvpwzh+s kzEKoxXQ7i3OOAfdbxaGATn4wkA4VN2OMoon+FuGlIRdxeN2zyPzqV4TjMcYKX/yqt yHw5v86Qj8wbA== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Peter Zijlstra Cc: Masami Hiramatsu , Ingo Molnar , bpf , Kernel Team , linux-kernel@vger.kernel.org, Andy Lutomirski , =?utf-8?q?Daniel_M=C3=BCller?= Subject: [PATCH] x86/kprobes: Fix to update kcb status flag after singlestepping Date: Tue, 2 Aug 2022 15:04:16 +0900 Message-Id: <165942025658.342061.12452378391879093249.stgit@devnote2> X-Mailer: git-send-email 2.25.1 User-Agent: StGit/0.19 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org From: Masami Hiramatsu (Google) Fix kprobes to update kcb (kprobes control block) status flag to KPROBE_HIT_SSDONE even if the kp->post_handler is not set. This may cause a kernel panic if another int3 user runs right after kprobes because kprobe_int3_handler() misunderstands the int3 is kprobe's single stepping int3. Fixes: 6256e668b7af ("x86/kprobes: Use int3 instead of debug trap for single-step") Reported-by: Daniel Müller Tested-by: Daniel Müller Signed-off-by: Masami Hiramatsu (Google) Cc: stable@vger.kernel.org Link: https://lore.kernel.org/all/20220727210136.jjgc3lpqeq42yr3m@muellerd-fedora-PC2BDTX9 Acked-by: Steven Rostedt (Google) --- arch/x86/kernel/kprobes/core.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index 7c4ab8870da4..74167dc5f55e 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -814,16 +814,20 @@ set_current_kprobe(struct kprobe *p, struct pt_regs *regs, static void kprobe_post_process(struct kprobe *cur, struct pt_regs *regs, struct kprobe_ctlblk *kcb) { - if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { - kcb->kprobe_status = KPROBE_HIT_SSDONE; - cur->post_handler(cur, regs, 0); - } - /* Restore back the original saved kprobes variables and continue. */ - if (kcb->kprobe_status == KPROBE_REENTER) + if (kcb->kprobe_status == KPROBE_REENTER) { + /* This will restore both kcb and current_kprobe */ restore_previous_kprobe(kcb); - else + } else { + /* + * Always update the kcb status because + * reset_curent_kprobe() doesn't update kcb. + */ + kcb->kprobe_status = KPROBE_HIT_SSDONE; + if (cur->post_handler) + cur->post_handler(cur, regs, 0); reset_current_kprobe(); + } } NOKPROBE_SYMBOL(kprobe_post_process);