From patchwork Sun Aug 27 11:40:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Patchwork-Id: 136982 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp2782686vqm; Sun, 27 Aug 2023 04:53:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF1QvWChKs6UDwUnYc6XX/n+FTpsmzAu9D5qAFhAx8iDdu/DRyGcfP8Cs+VHaBcaTRBQErz X-Received: by 2002:a50:ee92:0:b0:525:469a:fc47 with SMTP id f18-20020a50ee92000000b00525469afc47mr16610137edr.22.1693137180450; Sun, 27 Aug 2023 04:53:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693137180; cv=none; d=google.com; s=arc-20160816; b=RPYkKTJC206ri5eNIaGK273V6eymHTQGGcfTCuZxAc+fVrS62soImddMdEhyMMllKy 5FNekdsZqE0BFHDK0gEiFqob/J3VaRZS6DTUgRngv7tZWPIzWWLays7khopcddNN4zHy yevltwiKjDUrFtN+MbAKcm7j7UktyUIDELkxTTVY5WmmmApcWHDTHwuxQLmlC8k9JZF/ HnwStvdfHStasIedHuKPKhLCS1Rmf5oMnsbvAHqaHbD2UZ2Np0fOdZUwP7afUh+8Adxa i70E36XIPINejy2wfNo/f6qNBEIPYeeCgIkPsBJMzBCxg1INl0TAZRN1A1HFzI9WSl+U hJBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=vos58DTPJw5nqdaZf7G/iDSQtHBX4kXoLhquipGEqOg=; fh=0m6DsYDG20vP/OEItwAm9oQl9uUE2crp24dt9EP7Hqw=; b=pvJ0++Z8aRRoVkKxbMI8aQYZHKgXFfENrRFPbbOpnyWkBvcFUL3nfMD3xan/kFJaEN Zj582xp40SNboB3K1Y7gdrOVIc3vMLmHklwBQj4/dPBEfNwLx8bcRlCL+NRu0aAbfZe0 1UXzyBUqgmpFKTNYurJ6ZHqPhL80TxrQ1s2Afm7h3+3LPicd2KYr0STZYw6O9068QH5x ZitfJRYAalh7AD6iTDpJ+3V5QqnM/Tc0QVbsKHSNm0pqVdd09DwHFwU7AqzVRNI2SYYR odhZeH3SUnUjwlAiOYc2r9+mB1iFrO/CVa1C4+Ua+RrDfekAuPsk5ogpvKS2hwa8w55M 8y9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ltXBj4yr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t14-20020a05640203ce00b005257e01116asi3252183edw.441.2023.08.27.04.52.17; Sun, 27 Aug 2023 04:53:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ltXBj4yr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229650AbjH0Lkz (ORCPT + 99 others); Sun, 27 Aug 2023 07:40:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229480AbjH0LkY (ORCPT ); Sun, 27 Aug 2023 07:40:24 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60BA1125; Sun, 27 Aug 2023 04:40:22 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DA61A61E75; Sun, 27 Aug 2023 11:40:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D3DBFC433C8; Sun, 27 Aug 2023 11:40:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1693136421; bh=D95CdLfAH/USH3HPyeiR13hrH/zEEtUSxZ48xcEjbp4=; h=From:To:Cc:Subject:Date:From; b=ltXBj4yr+u8atUe+Acp+hrqSr2M4J22j8PfhaGuABYhP/7GOuJRV7o1aHpzGv40eP G6/9WjTgH4WASyn7ofCmEfgCG6DZraCv758e6UwNnxWKw3sYT79H5Bc75vIf2zDM48 mLWAbfneYWDRMRGHd47WhAG6iZdh2Jsy7JJP1R/8kSfixJTYWLz/v57yXOh3DNV7Tj Vd4TUCi88T0qUwLBgyGVSWy3KJb/dDBZOrCl+33Fa8oRrGkIynusN0UZnKPy2GSDb0 5P5qPyGCT3Lrh1Af2lSHAB/jt5y67hzqz11pEV2eL1Ou19Y9Uj9pl7T8XMDBO93Wdr 4Ed7tG91Fb/jw== From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= To: Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org, Guo Ren Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Nam Cao , kernel test robot Subject: [PATCH v2] riscv: Only consider swbp/ss handlers for correct privileged mode Date: Sun, 27 Aug 2023 13:40:03 +0200 Message-Id: <20230827114003.224958-1-bjorn@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775382837753613323 X-GMAIL-MSGID: 1775383012003366446 From: Björn Töpel RISC-V software breakpoint trap handlers are used for {k,u}probes. When trapping from kernelmode, only the kernelmode handlers should be considered. Vice versa, only usermode handlers for usermode traps. This is not the case on RISC-V, which can trigger a bug if a userspace process uses uprobes, and a WARN() is triggered from kernelmode (which is implemented via {c.,}ebreak). The kernel will trap on the kernelmode {c.,}ebreak, look for uprobes handlers, realize incorrectly that uprobes need to be handled, and exit the trap handler early. The trap returns to re-executing the {c.,}ebreak, and enter an infinite trap-loop. The issue was found running the BPF selftest [1]. Fix this issue by only considering the swbp/ss handlers for kernel/usermode respectively. Also, move CONFIG ifdeffery from traps.c to the asm/{k,u}probes.h headers. Note that linux/uprobes.h only include asm/uprobes.h if CONFIG_UPROBES is defined, which is why asm/uprobes.h needs to be unconditionally included in traps.c Link: https://lore.kernel.org/linux-riscv/87v8d19aun.fsf@all.your.base.are.belong.to.us/ # [1] Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202308271841.HlnnHFL7-lkp@intel.com/ Reviewed-by: Guo Ren Fixes: 74784081aac8 ("riscv: Add uprobes supported") Signed-off-by: Björn Töpel Tested-by: Puranjay Mohan --- v1->v2: Fix Clang build warning (kernel test robot) --- arch/riscv/include/asm/kprobes.h | 11 ++++++++++- arch/riscv/include/asm/uprobes.h | 13 ++++++++++++- arch/riscv/kernel/traps.c | 28 ++++++++++++++++++---------- 3 files changed, 40 insertions(+), 12 deletions(-) base-commit: 7d2f353b2682dcfe5f9bc71e5b61d5b61770d98e diff --git a/arch/riscv/include/asm/kprobes.h b/arch/riscv/include/asm/kprobes.h index e7882ccb0fd4..78ea44f76718 100644 --- a/arch/riscv/include/asm/kprobes.h +++ b/arch/riscv/include/asm/kprobes.h @@ -40,6 +40,15 @@ void arch_remove_kprobe(struct kprobe *p); int kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr); bool kprobe_breakpoint_handler(struct pt_regs *regs); bool kprobe_single_step_handler(struct pt_regs *regs); - +#else +static inline bool kprobe_breakpoint_handler(struct pt_regs *regs) +{ + return false; +} + +static inline bool kprobe_single_step_handler(struct pt_regs *regs) +{ + return false; +} #endif /* CONFIG_KPROBES */ #endif /* _ASM_RISCV_KPROBES_H */ diff --git a/arch/riscv/include/asm/uprobes.h b/arch/riscv/include/asm/uprobes.h index f2183e00fdd2..3fc7deda9190 100644 --- a/arch/riscv/include/asm/uprobes.h +++ b/arch/riscv/include/asm/uprobes.h @@ -34,7 +34,18 @@ struct arch_uprobe { bool simulate; }; +#ifdef CONFIG_UPROBES bool uprobe_breakpoint_handler(struct pt_regs *regs); bool uprobe_single_step_handler(struct pt_regs *regs); - +#else +static inline bool uprobe_breakpoint_handler(struct pt_regs *regs) +{ + return false; +} + +static inline bool uprobe_single_step_handler(struct pt_regs *regs) +{ + return false; +} +#endif /* CONFIG_UPROBES */ #endif /* _ASM_RISCV_UPROBES_H */ diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index f798c853bede..cd6f10c73a16 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include #include #include @@ -246,22 +248,28 @@ static inline unsigned long get_break_insn_length(unsigned long pc) return GET_INSN_LENGTH(insn); } +static bool probe_single_step_handler(struct pt_regs *regs) +{ + bool user = user_mode(regs); + + return user ? uprobe_single_step_handler(regs) : kprobe_single_step_handler(regs); +} + +static bool probe_breakpoint_handler(struct pt_regs *regs) +{ + bool user = user_mode(regs); + + return user ? uprobe_breakpoint_handler(regs) : kprobe_breakpoint_handler(regs); +} + void handle_break(struct pt_regs *regs) { -#ifdef CONFIG_KPROBES - if (kprobe_single_step_handler(regs)) + if (probe_single_step_handler(regs)) return; - if (kprobe_breakpoint_handler(regs)) - return; -#endif -#ifdef CONFIG_UPROBES - if (uprobe_single_step_handler(regs)) + if (probe_breakpoint_handler(regs)) return; - if (uprobe_breakpoint_handler(regs)) - return; -#endif current->thread.bad_cause = regs->cause; if (user_mode(regs))