Message ID | 20230116064342.2092136-1-liaochang1@huawei.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1053074wrn; Sun, 15 Jan 2023 22:55:33 -0800 (PST) X-Google-Smtp-Source: AMrXdXsA4Iqpdn5hJt8ix3USsw4bJBYd9swuKwkUr3cr0bNG8kigONsQQ3w2YqQR9E3TQuFllaXk X-Received: by 2002:a17:902:b48f:b0:192:8502:77f9 with SMTP id y15-20020a170902b48f00b00192850277f9mr19949646plr.27.1673852133590; Sun, 15 Jan 2023 22:55:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673852133; cv=none; d=google.com; s=arc-20160816; b=aN2Exrzgqn2JGtxpNzPNAjj2xcm9eUVNInFkKw6LYD8Z6gvm81q2JnPrBad5zJAZ35 9rhNIXKUjoYXJfkpUoahElKogezE/kNCQdwvV5LeP/NPhGr3vmP/hPnCMLmKtlDZD+zq iJ0hSSFHVTf2LntXsmeHjFrrHXL69Mcgy3Zp2p4JrBiVOonzI4wi57iSzytX7d7T6l+R LCiK8pcQu41pDYQciQv2hy8SeLBNhSGln2dlii92IyO+dT2/pG7AELILx/kpW42sOWkw uLziQ9903CfuxG55k7nf/jlQvaQwC5GR28hTKzilk2cx5jm3zRhuYigZKnvdQlYN6Uk0 vCCA== 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; bh=aeq9bqeSlbANd0PqwbC3UpaWJbpeJ5FbLJ0Za/Tn9VE=; b=shSpNunWzXLiuVZ8Anv8RIfThruqSNk3V4NVa0obw0nebpMpvbU9N2U2QNvVlOtM+E MY56bev6Hkzx38XVDo7VwvKOVZDkVpND/maPohuJfyWDc7pbkuJ8ny9tjzfizwMf5pYq TKv0jmV11i774SWyYuCgRRq38T7RISCcqaspQxbVTRfFZvl0AQz4tbyrbfEGYBxW4C+t 9uY3Mm40BMLLjlQvdyORbVm+E4+PWSbCSlrMi7SglooaGbGVDR0iiIFaEhM21MfCw0RN 0SSyDE+9pGHkT3H1zNWhDFNGjfGRNITooVZE0jpZQekUy95WQ4zO3M7lX9R9aOxFZH3R Wi0A== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z3-20020a170902708300b00188fead2329si27333489plk.135.2023.01.15.22.55.21; Sun, 15 Jan 2023 22:55:33 -0800 (PST) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231910AbjAPGqT (ORCPT <rfc822;stefanalexe802@gmail.com> + 99 others); Mon, 16 Jan 2023 01:46:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231837AbjAPGqQ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 16 Jan 2023 01:46:16 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39B9E83E6 for <linux-kernel@vger.kernel.org>; Sun, 15 Jan 2023 22:46:15 -0800 (PST) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NwMvK41BbznVNh; Mon, 16 Jan 2023 14:44:29 +0800 (CST) Received: from huawei.com (10.67.174.53) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Mon, 16 Jan 2023 14:46:10 +0800 From: Liao Chang <liaochang1@huawei.com> To: <paul.walmsley@sifive.com>, <palmer@dabbelt.com>, <aou@eecs.berkeley.edu>, <liaochang1@huawei.com>, <mhiramat@kernel.org>, <guoren@kernel.org>, <me@packi.ch>, <penberg@kernel.org> CC: <linux-riscv@lists.infradead.org>, <linux-kernel@vger.kernel.org>, <bjorn@kernel.org>, <chenlifu@huawei.com> Subject: [PATCH] riscv/kprobe: Fix instruction simulation of JALR Date: Mon, 16 Jan 2023 14:43:42 +0800 Message-ID: <20230116064342.2092136-1-liaochang1@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.67.174.53] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755161175026651826?= X-GMAIL-MSGID: =?utf-8?q?1755161175026651826?= |
Series |
riscv/kprobe: Fix instruction simulation of JALR
|
|
Commit Message
Liao Chang
Jan. 16, 2023, 6:43 a.m. UTC
Set kprobe at 'jalr 1140(ra)' of vfs_write results in the following
crash:
[ 32.092235] Unable to handle kernel access to user memory without uaccess routines at virtual address 00aaaaaad77b1170
[ 32.093115] Oops [#1]
[ 32.093251] Modules linked in:
[ 32.093626] CPU: 0 PID: 135 Comm: ftracetest Not tainted 6.2.0-rc2-00013-gb0aa5e5df0cb-dirty #16
[ 32.093985] Hardware name: riscv-virtio,qemu (DT)
[ 32.094280] epc : ksys_read+0x88/0xd6
[ 32.094855] ra : ksys_read+0xc0/0xd6
[ 32.095016] epc : ffffffff801cda80 ra : ffffffff801cdab8 sp : ff20000000d7bdc0
[ 32.095227] gp : ffffffff80f14000 tp : ff60000080f9cb40 t0 : ffffffff80f13e80
[ 32.095500] t1 : ffffffff8000c29c t2 : ffffffff800dbc54 s0 : ff20000000d7be60
[ 32.095716] s1 : 0000000000000000 a0 : ffffffff805a64ae a1 : ffffffff80a83708
[ 32.095921] a2 : ffffffff80f160a0 a3 : 0000000000000000 a4 : f229b0afdb165300
[ 32.096171] a5 : f229b0afdb165300 a6 : ffffffff80eeebd0 a7 : 00000000000003ff
[ 32.096411] s2 : ff6000007ff76800 s3 : fffffffffffffff7 s4 : 00aaaaaad77b1170
[ 32.096638] s5 : ffffffff80f160a0 s6 : ff6000007ff76800 s7 : 0000000000000030
[ 32.096865] s8 : 00ffffffc3d97be0 s9 : 0000000000000007 s10: 00aaaaaad77c9410
[ 32.097092] s11: 0000000000000000 t3 : ffffffff80f13e48 t4 : ffffffff8000c29c
[ 32.097317] t5 : ffffffff8000c29c t6 : ffffffff800dbc54
[ 32.097505] status: 0000000200000120 badaddr: 00aaaaaad77b1170 cause: 000000000000000d
[ 32.098011] [<ffffffff801cdb72>] ksys_write+0x6c/0xd6
[ 32.098222] [<ffffffff801cdc06>] sys_write+0x2a/0x38
[ 32.098405] [<ffffffff80003c76>] ret_from_syscall+0x0/0x2
Since the rs1 and rd might be the same one, such as 'jalr 1140(ra)',
hence it requires obtaining the target address from rs1 followed by
updating rd.
Fixes: c22b0bcb1dd0 ("riscv: Add kprobes supported")
Signed-off-by: Liao Chang <liaochang1@huawei.com>
---
arch/riscv/kernel/probes/simulate-insn.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
Comments
On Mon, Jan 16, 2023 at 2:46 PM Liao Chang <liaochang1@huawei.com> wrote: > > Set kprobe at 'jalr 1140(ra)' of vfs_write results in the following > crash: > > [ 32.092235] Unable to handle kernel access to user memory without uaccess routines at virtual address 00aaaaaad77b1170 > [ 32.093115] Oops [#1] > [ 32.093251] Modules linked in: > [ 32.093626] CPU: 0 PID: 135 Comm: ftracetest Not tainted 6.2.0-rc2-00013-gb0aa5e5df0cb-dirty #16 > [ 32.093985] Hardware name: riscv-virtio,qemu (DT) > [ 32.094280] epc : ksys_read+0x88/0xd6 > [ 32.094855] ra : ksys_read+0xc0/0xd6 > [ 32.095016] epc : ffffffff801cda80 ra : ffffffff801cdab8 sp : ff20000000d7bdc0 > [ 32.095227] gp : ffffffff80f14000 tp : ff60000080f9cb40 t0 : ffffffff80f13e80 > [ 32.095500] t1 : ffffffff8000c29c t2 : ffffffff800dbc54 s0 : ff20000000d7be60 > [ 32.095716] s1 : 0000000000000000 a0 : ffffffff805a64ae a1 : ffffffff80a83708 > [ 32.095921] a2 : ffffffff80f160a0 a3 : 0000000000000000 a4 : f229b0afdb165300 > [ 32.096171] a5 : f229b0afdb165300 a6 : ffffffff80eeebd0 a7 : 00000000000003ff > [ 32.096411] s2 : ff6000007ff76800 s3 : fffffffffffffff7 s4 : 00aaaaaad77b1170 > [ 32.096638] s5 : ffffffff80f160a0 s6 : ff6000007ff76800 s7 : 0000000000000030 > [ 32.096865] s8 : 00ffffffc3d97be0 s9 : 0000000000000007 s10: 00aaaaaad77c9410 > [ 32.097092] s11: 0000000000000000 t3 : ffffffff80f13e48 t4 : ffffffff8000c29c > [ 32.097317] t5 : ffffffff8000c29c t6 : ffffffff800dbc54 > [ 32.097505] status: 0000000200000120 badaddr: 00aaaaaad77b1170 cause: 000000000000000d > [ 32.098011] [<ffffffff801cdb72>] ksys_write+0x6c/0xd6 > [ 32.098222] [<ffffffff801cdc06>] sys_write+0x2a/0x38 > [ 32.098405] [<ffffffff80003c76>] ret_from_syscall+0x0/0x2 > > Since the rs1 and rd might be the same one, such as 'jalr 1140(ra)', > hence it requires obtaining the target address from rs1 followed by > updating rd. > > Fixes: c22b0bcb1dd0 ("riscv: Add kprobes supported") > Signed-off-by: Liao Chang <liaochang1@huawei.com> > --- > arch/riscv/kernel/probes/simulate-insn.c | 6 +----- > 1 file changed, 1 insertion(+), 5 deletions(-) > > diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c > index d73e96f6ed7c..02aa262fb2ac 100644 > --- a/arch/riscv/kernel/probes/simulate-insn.c > +++ b/arch/riscv/kernel/probes/simulate-insn.c > @@ -71,17 +71,13 @@ bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *reg > u32 rd_index = (opcode >> 7) & 0x1f; > u32 rs1_index = (opcode >> 15) & 0x1f; > > - ret = rv_insn_reg_set_val(regs, rd_index, addr + 4); > - if (!ret) > - return ret; > - > ret = rv_insn_reg_get_val(regs, rs1_index, &base_addr); > if (!ret) > return ret; Ah, thank you, If rd == rs1, the regs-><rs1> got a overwritten value, my mistake. We should read rs1 firstly. But for the patch, just exchanging the position is enough; keep the original sequence for coding convention. eg: @@ -71,11 +71,11 @@ bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *reg u32 rd_index = (opcode >> 7) & 0x1f; u32 rs1_index = (opcode >> 15) & 0x1f; - ret = rv_insn_reg_set_val(regs, rd_index, addr + 4); + ret = rv_insn_reg_get_val(regs, rs1_index, &base_addr); if (!ret) return ret; - ret = rv_insn_reg_get_val(regs, rs1_index, &base_addr); + ret = rv_insn_reg_set_val(regs, rd_index, addr + 4); if (!ret) return ret; Then: Reviewed-by: Guo Ren <guoren@kernel.org> > > instruction_pointer_set(regs, (base_addr + sign_extend32((imm), 11))&~1); > > - return ret; > + return rv_insn_reg_set_val(regs, rd_index, addr + 4); > } > > #define auipc_rd_idx(opcode) \ > -- > 2.25.1 >
On Mon, 16 Jan 2023 14:43:42 +0800, Liao Chang wrote: > Set kprobe at 'jalr 1140(ra)' of vfs_write results in the following > crash: > > [ 32.092235] Unable to handle kernel access to user memory without uaccess routines at virtual address 00aaaaaad77b1170 > [ 32.093115] Oops [#1] > [ 32.093251] Modules linked in: > [ 32.093626] CPU: 0 PID: 135 Comm: ftracetest Not tainted 6.2.0-rc2-00013-gb0aa5e5df0cb-dirty #16 > [ 32.093985] Hardware name: riscv-virtio,qemu (DT) > [ 32.094280] epc : ksys_read+0x88/0xd6 > [ 32.094855] ra : ksys_read+0xc0/0xd6 > [ 32.095016] epc : ffffffff801cda80 ra : ffffffff801cdab8 sp : ff20000000d7bdc0 > [ 32.095227] gp : ffffffff80f14000 tp : ff60000080f9cb40 t0 : ffffffff80f13e80 > [ 32.095500] t1 : ffffffff8000c29c t2 : ffffffff800dbc54 s0 : ff20000000d7be60 > [ 32.095716] s1 : 0000000000000000 a0 : ffffffff805a64ae a1 : ffffffff80a83708 > [ 32.095921] a2 : ffffffff80f160a0 a3 : 0000000000000000 a4 : f229b0afdb165300 > [ 32.096171] a5 : f229b0afdb165300 a6 : ffffffff80eeebd0 a7 : 00000000000003ff > [ 32.096411] s2 : ff6000007ff76800 s3 : fffffffffffffff7 s4 : 00aaaaaad77b1170 > [ 32.096638] s5 : ffffffff80f160a0 s6 : ff6000007ff76800 s7 : 0000000000000030 > [ 32.096865] s8 : 00ffffffc3d97be0 s9 : 0000000000000007 s10: 00aaaaaad77c9410 > [ 32.097092] s11: 0000000000000000 t3 : ffffffff80f13e48 t4 : ffffffff8000c29c > [ 32.097317] t5 : ffffffff8000c29c t6 : ffffffff800dbc54 > [ 32.097505] status: 0000000200000120 badaddr: 00aaaaaad77b1170 cause: 000000000000000d > [ 32.098011] [<ffffffff801cdb72>] ksys_write+0x6c/0xd6 > [ 32.098222] [<ffffffff801cdc06>] sys_write+0x2a/0x38 > [ 32.098405] [<ffffffff80003c76>] ret_from_syscall+0x0/0x2 > > [...] Applied, thanks! (with Guo's suggested cleanup) [1/1] riscv/kprobe: Fix instruction simulation of JALR https://git.kernel.org/palmer/c/ca0254998be4 Best regards,
Hello: This patch was applied to riscv/linux.git (fixes) by Palmer Dabbelt <palmer@rivosinc.com>: On Mon, 16 Jan 2023 14:43:42 +0800 you wrote: > Set kprobe at 'jalr 1140(ra)' of vfs_write results in the following > crash: > > [ 32.092235] Unable to handle kernel access to user memory without uaccess routines at virtual address 00aaaaaad77b1170 > [ 32.093115] Oops [#1] > [ 32.093251] Modules linked in: > [ 32.093626] CPU: 0 PID: 135 Comm: ftracetest Not tainted 6.2.0-rc2-00013-gb0aa5e5df0cb-dirty #16 > [ 32.093985] Hardware name: riscv-virtio,qemu (DT) > [ 32.094280] epc : ksys_read+0x88/0xd6 > [ 32.094855] ra : ksys_read+0xc0/0xd6 > [ 32.095016] epc : ffffffff801cda80 ra : ffffffff801cdab8 sp : ff20000000d7bdc0 > [ 32.095227] gp : ffffffff80f14000 tp : ff60000080f9cb40 t0 : ffffffff80f13e80 > [ 32.095500] t1 : ffffffff8000c29c t2 : ffffffff800dbc54 s0 : ff20000000d7be60 > [ 32.095716] s1 : 0000000000000000 a0 : ffffffff805a64ae a1 : ffffffff80a83708 > [ 32.095921] a2 : ffffffff80f160a0 a3 : 0000000000000000 a4 : f229b0afdb165300 > [ 32.096171] a5 : f229b0afdb165300 a6 : ffffffff80eeebd0 a7 : 00000000000003ff > [ 32.096411] s2 : ff6000007ff76800 s3 : fffffffffffffff7 s4 : 00aaaaaad77b1170 > [ 32.096638] s5 : ffffffff80f160a0 s6 : ff6000007ff76800 s7 : 0000000000000030 > [ 32.096865] s8 : 00ffffffc3d97be0 s9 : 0000000000000007 s10: 00aaaaaad77c9410 > [ 32.097092] s11: 0000000000000000 t3 : ffffffff80f13e48 t4 : ffffffff8000c29c > [ 32.097317] t5 : ffffffff8000c29c t6 : ffffffff800dbc54 > [ 32.097505] status: 0000000200000120 badaddr: 00aaaaaad77b1170 cause: 000000000000000d > [ 32.098011] [<ffffffff801cdb72>] ksys_write+0x6c/0xd6 > [ 32.098222] [<ffffffff801cdc06>] sys_write+0x2a/0x38 > [ 32.098405] [<ffffffff80003c76>] ret_from_syscall+0x0/0x2 > > [...] Here is the summary with links: - riscv/kprobe: Fix instruction simulation of JALR https://git.kernel.org/riscv/c/ca0254998be4 You are awesome, thank you!
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c index d73e96f6ed7c..02aa262fb2ac 100644 --- a/arch/riscv/kernel/probes/simulate-insn.c +++ b/arch/riscv/kernel/probes/simulate-insn.c @@ -71,17 +71,13 @@ bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *reg u32 rd_index = (opcode >> 7) & 0x1f; u32 rs1_index = (opcode >> 15) & 0x1f; - ret = rv_insn_reg_set_val(regs, rd_index, addr + 4); - if (!ret) - return ret; - ret = rv_insn_reg_get_val(regs, rs1_index, &base_addr); if (!ret) return ret; instruction_pointer_set(regs, (base_addr + sign_extend32((imm), 11))&~1); - return ret; + return rv_insn_reg_set_val(regs, rd_index, addr + 4); } #define auipc_rd_idx(opcode) \