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 1DC03C38142 for ; Fri, 27 Jan 2023 13:06:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234566AbjA0NGp (ORCPT ); Fri, 27 Jan 2023 08:06:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234520AbjA0NGb (ORCPT ); Fri, 27 Jan 2023 08:06:31 -0500 Received: from cstnet.cn (smtp80.cstnet.cn [159.226.251.80]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B3CFF7D6CA for ; Fri, 27 Jan 2023 05:06:28 -0800 (PST) Received: from cgk-Precision-3650-Tower.. (unknown [219.141.235.82]) by APP-01 (Coremail) with SMTP id qwCowADX3Eg9zNNjDyF3Ag--.52827S14; Fri, 27 Jan 2023 21:06:10 +0800 (CST) From: Chen Guokai To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, rostedt@goodmis.org, mingo@redhat.com, sfr@canb.auug.org.au Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, liaochang1@huawei.com Subject: [PATCH v6 10/13] riscv/kprobe: Add instruction boundary check for RVI/RVC hybrid kernel Date: Fri, 27 Jan 2023 21:05:38 +0800 Message-Id: <20230127130541.1250865-11-chenguokai17@mails.ucas.ac.cn> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230127130541.1250865-1-chenguokai17@mails.ucas.ac.cn> References: <20230127130541.1250865-1-chenguokai17@mails.ucas.ac.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: qwCowADX3Eg9zNNjDyF3Ag--.52827S14 X-Coremail-Antispam: 1UD129KBjvJXoW7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FxhVjvjDU 0xZFpf9x07jF_M3UUUUU= X-Originating-IP: [219.141.235.82] X-CM-SenderInfo: xfkh0w5xrntxyrx6ztxlovh3xfdvhtffof0/1tbiCgsEE2PTtMIZxQAAsM Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Liao Chang Add instruction boundary check to ensure kprobe doesn't truncate any RVI instruction, which leads to kernel crash. Signed-off-by: Liao Chang --- arch/riscv/kernel/probes/kprobes.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c index e1856b04db04..91a6b46909cc 100644 --- a/arch/riscv/kernel/probes/kprobes.c +++ b/arch/riscv/kernel/probes/kprobes.c @@ -49,11 +49,33 @@ static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs) post_kprobe_handler(p, kcb, regs); } +bool __kprobes riscv_insn_boundary_check(unsigned long paddr) +{ +#if defined(CONFIG_RISCV_ISA_C) + unsigned long size = 0, offs = 0, len = 0, entry = 0; + + if (!kallsyms_lookup_size_offset(paddr, &size, &offs)) + return false; + + /* + * Scan instructions from function entry ensure the kprobe address + * is aligned with RVI or RVC boundary. + */ + entry = paddr - offs; + while ((entry + len) < paddr) + len += GET_INSN_LENGTH(*(kprobe_opcode_t *)(entry + len)); + return (entry + len) == paddr; +#else + return true; +#endif +} + int __kprobes arch_prepare_kprobe(struct kprobe *p) { unsigned long probe_addr = (unsigned long)p->addr; - if (probe_addr & 0x1) + /* for RVI/RCV hybrid kernel, it needs instruction boundary check */ + if ((probe_addr & 0x1) || !riscv_insn_boundary_check(probe_addr)) return -EILSEQ; /* copy instruction */ -- 2.34.1