From patchwork Thu May 26 14:14:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiang W X-Patchwork-Id: 1635876 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=ZHpdZ8S8; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=Z97wwrMC; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4L89364RZ7z9s75 for ; Fri, 27 May 2022 00:16:18 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=Y55pAA1SC1DFMX7j8VLYhjyP9l2nNjdb4p8xdXtEdMU=; b=ZHpdZ8S86pOR0z iq3mK/vuXKDc68rq8eQ5fXoc9jR9sx5SHWRP7zp+hVAWqS4mjLmIO8EHOU8Tj8IGUxnlUDqqK/9xz ZnD2jDA/8OZqjqhUi0cNIIPd1RQ71MQeLyxvBV7N78Cf3xgggX+LklWUU6qZzyAwMsVlJTJ+edp2E XiUW4mtJSWpVEV6F7B8G5Kfa4G56intMz/xKWR6kXHsiUbyKXNp4CfhYZp9qoLTYHwKCxNm2UA9vC llvj9QPJ0++BeSJy4dyxw8qoClpst35JOwEPOCJGO6YV5L41iG5SEQ3r6oItG6GZ8j+QXdH+kb1of OHVKReZtmt1167T4eHxQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nuEHL-00F6Sw-Jp; Thu, 26 May 2022 14:15:55 +0000 Received: from mail-m964.mail.126.com ([123.126.96.4]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nuEHH-00F6SP-2y for opensbi@lists.infradead.org; Thu, 26 May 2022 14:15:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=qa24o qsOp7hkFx14vUo3W5ssBuz7oeoPdDJckWp9DfI=; b=Z97wwrMC4+P2+j5jVieBj azjnhD7nzF9AaeyA8nE12ZEYJoIXJKIPpIf8N1CYw3tHG4ROZcfx1spoVoLmP0GL ec2YkpDDvwWaU0dbFZf61NGZcMR+8ZST8YGujGDiByY69bCXR+pcvJVavfGNn8Di Q9HRADTUGD4V8op5IjPHVE= Received: from x390.lan (unknown [210.22.74.70]) by smtp9 (Coremail) with SMTP id NeRpCgDn_ZlXi49i11LcCw--.2267S2; Thu, 26 May 2022 22:14:49 +0800 (CST) From: Xiang W To: opensbi@lists.infradead.org Cc: anup@brainfault.org, Xiang W Subject: [PATCH] lib: sbi: fix system_opcode_insn Date: Thu, 26 May 2022 22:14:44 +0800 Message-Id: <20220526141444.302078-1-wxjstz@126.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-CM-TRANSID: NeRpCgDn_ZlXi49i11LcCw--.2267S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7Kw1fuFyUCryrAFyDCFWktFb_yoW8Xr4DpF s8CFyDGrW0qrnYga93Cr1furn5Xa18G3yUAFyxAa9ayr4xGr95AFWrtr45tr93Wr47Gayj 9a1jyry8u343Z3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRebyJUUUUU= X-Originating-IP: [210.22.74.70] X-CM-SenderInfo: pz0m23b26rjloofrz/1tbi5BkNOlpEDo9UfAAAs9 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220526_071553_224651_E53F84B3 X-CRM114-Status: UNSURE ( 9.30 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: If the csr's operation comes from M mode, it should not be forwarded to low-privilege processing, this patch fixes this problem. Signed-off-by: Xiang W Reviewed-by: Anup Patel --- Changes in v4: - Improve print statement Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [123.126.96.4 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [wxjstz[at]126.com] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org If the csr's operation comes from M mode, it should not be forwarded to low-privilege processing, this patch fixes this problem. Signed-off-by: Xiang W Reviewed-by: Anup Patel --- Changes in v4: - Improve print statement Changes in v3: - move checking prev_mode before sbi_emulate_csr_read and return failure by Anup's suggestion Changes in v2: - add missing head file lib/sbi/sbi_illegal_insn.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/sbi/sbi_illegal_insn.c b/lib/sbi/sbi_illegal_insn.c index bfe7d61..388ad55 100644 --- a/lib/sbi/sbi_illegal_insn.c +++ b/lib/sbi/sbi_illegal_insn.c @@ -16,6 +16,7 @@ #include #include #include +#include typedef int (*illegal_insn_func)(ulong insn, struct sbi_trap_regs *regs); @@ -37,8 +38,15 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs) int do_write, rs1_num = (insn >> 15) & 0x1f; ulong rs1_val = GET_RS1(insn, regs); int csr_num = (u32)insn >> 20; + ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT; ulong csr_val, new_csr_val; + if (prev_mode == PRV_M) { + sbi_printf("%s: Failed to access CSR %#x from M-mode", + __func__, csr_num); + return SBI_EFAIL; + } + /* TODO: Ensure that we got CSR read/write instruction */ if (sbi_emulate_csr_read(csr_num, regs, &csr_val))