Commit 799d3c5876 for qemu.org

commit 799d3c5876aebd44e896dc540b951f1147eb9dc6
Author: Keith Monahan <keith@techtravels.org>
Date:   Fri Nov 20 18:12:31 2026 +0100

    hw/scsi/ncr53c710: Guard SCRIPTS reentrancy and cancel requests on soft reset

    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>

diff --git a/hw/scsi/ncr53c710.c b/hw/scsi/ncr53c710.c
index 39a20ba90e..4b1853a391 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;
     }

@@ -1218,6 +1253,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;