All of lore.kernel.org
 help / color / mirror / Atom feed
From: Helge Deller <deller@kernel.org>
To: qemu-devel@nongnu.org
Cc: "Keith Monahan" <keith@techtravels.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>,
	"Fam Zheng" <fam@euphon.net>, "Helge Deller" <deller@gmx.de>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"BALATON Zoltan" <balaton@eik.bme.hu>,
	"Mark Cave-Ayland" <mark.cave-ayland@ilande.co.uk>
Subject: [PATCH 11/14] hw/scsi/ncr53c710: Guard SCRIPTS reentrancy and cancel requests on soft reset
Date: Mon, 23 Nov 2026 18:21:48 +0100	[thread overview]
Message-ID: <20261123172151.7605-12-deller@kernel.org> (raw)
In-Reply-To: <20261123172151.7605-1-deller@kernel.org>

From: Keith Monahan <keith@techtravels.org>

The SCRIPTS interpreter guards against CVE-2023-0330. When a command finishes
while the interpreter is running, the interpreter moves on to the next
instruction without restarting itself. If a script keeps triggering itself, the
interpreter stops it after 8 nested restarts.

A guest ISTAT.RST soft reset cancels the connected and any queued
tagged requests, so a stale s->current cannot dangle into the next
command and queued requests are not leaked, mirroring the SCNTL1.RST
bus reset teardown.

Signed-off-by: Keith Monahan <keith@techtravels.org>
AI-used-for: Patch series decomposition (commit splitting and ordering)
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
Signed-off-by: Helge Deller <deller@gmx.de>
---
 hw/scsi/ncr53c710.c | 41 +++++++++++++++++++++++++++++++++++++++--
 hw/scsi/ncr53c710.h |  5 +++++
 2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/hw/scsi/ncr53c710.c b/hw/scsi/ncr53c710.c
index d6bd1c95fc..b7fc97b93b 100644
--- a/hw/scsi/ncr53c710.c
+++ b/hw/scsi/ncr53c710.c
@@ -104,8 +104,23 @@ static int lsi_id_from_bits(unsigned bits)
 
 void ncr710_soft_reset(NCR710State *s)
 {
+    lsi_request *p, *p_next;
+
     trace_ncr710_reset();
 
+    /*
+     * A guest ISTAT.RST soft reset can land with commands still in flight.
+     * Cancel them so no stale pointer survives the reset.
+     */
+    if (s->current && s->current->req) {
+        scsi_req_cancel(s->current->req);
+    }
+    QTAILQ_FOREACH_SAFE(p, &s->queue, next, p_next) {
+        if (p->req) {
+            scsi_req_cancel(p->req);
+        }
+    }
+
     s->carry = 0;
     s->msg_action = LSI_MSG_ACTION_COMMAND;
     s->msg_len = 0;
@@ -115,6 +130,11 @@ void ncr710_soft_reset(NCR710State *s)
     s->command_complete = 0;
     s->script_running = false;
     s->sigp_pending_resume = false;
+    /*
+     * Do not reset reentrancy_level here.  A SCRIPTS driven soft reset can run
+     * while lsi_execute_script frames are still on the stack; those frames
+     * decrement the counter as they unwind.
+     */
 
     s->scntl0 = 0xc0;       /* full arbitration */
     s->scntl1 = 0;
@@ -258,6 +278,16 @@ static int lsi_bad_phase(NCR710State *s, int new_phase)
 static void lsi_resume_script(NCR710State *s)
 {
     trace_ncr710_resume_script(s->waiting);
+    if (s->reentrancy_level > 0) {
+        /*
+         * A completion fired synchronously while the SCRIPTS interpreter is
+         * already on the stack.  Do not recurse: release the wait so the outer
+         * loop continues to the next instruction, which the completion has
+         * already armed.
+         */
+        s->waiting = LSI_NOWAIT;
+        return;
+    }
     if (s->waiting != LSI_DMA_SCRIPTS) {
         s->waiting = LSI_NOWAIT;
         lsi_execute_script(s);
@@ -858,13 +888,18 @@ static void lsi_execute_script(NCR710State *s)
         s->waiting = LSI_NOWAIT;
     }
 
+    s->reentrancy_level++;
     s->script_running = true;
 again:
-    /* Yield to the CPU after LSI_MAX_INSN instructions. */
-    if (++insn_processed > LSI_MAX_INSN) {
+    /*
+     * Yield to the CPU after LSI_MAX_INSN instructions, and guard against a
+     * script that retriggers itself (CVE-2023-0330).
+     */
+    if (++insn_processed > LSI_MAX_INSN || s->reentrancy_level > 8) {
         trace_ncr710_script_yield(insn_processed);
         s->waiting = LSI_WAIT_SCRIPTS;
         lsi_scripts_timer_start(s);
+        s->reentrancy_level--;
         return;
     }
 
@@ -1210,6 +1245,8 @@ again:
             goto again;
         }
     }
+
+    s->reentrancy_level--;
 }
 
 static uint8_t lsi_reg_readb(NCR710State *s, int offset)
diff --git a/hw/scsi/ncr53c710.h b/hw/scsi/ncr53c710.h
index 8b5cbfc135..9a4816794e 100644
--- a/hw/scsi/ncr53c710.h
+++ b/hw/scsi/ncr53c710.h
@@ -101,6 +101,11 @@ typedef struct NCR710State {
     bool sigp_pending_resume;
     /* A SCSI continue was deferred to lsi_issue_bh. */
     bool issue_pending;
+    /*
+     * SCRIPTS interpreter recursion depth; always 0 outside
+     * lsi_execute_script, so it is not migrated.
+     */
+    int reentrancy_level;
     lsi_request *current;
     /* Disconnected tagged commands awaiting reselection. */
     QTAILQ_HEAD(, lsi_request) queue;
-- 
2.54.0



  parent reply	other threads:[~2026-11-23 17:24 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 17:21 [PATCH 00/14] nc53c710 rewrite Helge Deller
2026-11-23 17:21 ` [PATCH 01/14] hw/scsi/ncr53c710: Cancel the in-flight request on soft reset Helge Deller
2026-11-23 17:21 ` [PATCH 02/14] hw/scsi/ncr53c710: Do not load SBCL from DBC after a SCRIPTS phase move Helge Deller
2026-11-23 17:21 ` [PATCH 03/14] hw/scsi/scsi-disk: Add quirk_mode_page_no_flexible_disk, use it on the 715 Helge Deller
2026-11-23 17:21 ` [PATCH 04/14] hw/scsi/lasi_ncr710: Limit max_target and max_lun to 7 Helge Deller
2026-11-23 17:21 ` [PATCH 05/14] hw/scsi/ncr53c710: Keep DCNTL.EA and DMODE.FC1 across an ISTAT.RST reset Helge Deller
2026-11-23 17:21 ` [PATCH 06/14] hw/scsi: Move shared LSI/NCR definitions to lsi_scsi.h Helge Deller
2026-11-23 17:21 ` [PATCH 07/14] hw/scsi/ncr53c710: Rewrite the 53C710 SCRIPTS engine Helge Deller
2026-11-23 17:21 ` [PATCH 08/14] hw/scsi/ncr53c710: Deliver completions from bottom halves Helge Deller
2026-11-23 17:21 ` [PATCH 09/14] hw/scsi/ncr53c710: Drain asynchronous transfers in the register write Helge Deller
2026-11-23 17:21 ` [PATCH 10/14] hw/scsi/ncr53c710: Append SAVE DATA POINTERS after MESSAGE REJECT for HP-UX Helge Deller
2026-11-23 17:21 ` Helge Deller [this message]
2026-11-23 17:21 ` [PATCH 12/14] hw/scsi/lasi_ncr710: Map the full LASI SCSI module page Helge Deller
2026-11-23 17:21 ` [PATCH 13/14] hw/scsi/ncr53c710: Document the SIGP latch deviation Helge Deller
2026-11-23 17:21 ` [PATCH 14/14] hw/scsi/ncr53c710: Bump the migration version Helge Deller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123172151.7605-12-deller@kernel.org \
    --to=deller@kernel.org \
    --cc=balaton@eik.bme.hu \
    --cc=deller@gmx.de \
    --cc=fam@euphon.net \
    --cc=keith@techtravels.org \
    --cc=mark.cave-ayland@ilande.co.uk \
    --cc=pbonzini@redhat.com \
    --cc=philmd@oss.qualcomm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.