[PATCH v2] lib: sbi: dbtr: validate trigger configuration before writing CSR

liutong liutong at iscas.ac.cn
Wed Nov 25 18:45:19 PST 2026


Move the validation of tdata1 to dbtr_trigger_enable(), where it is
written to CSR_TDATA1. This prevents S-mode from modifying the trigger
configuration between the validation pass and the install/enable pass,
allowing an invalid configuration (with DMODE or M bit set) to reach
the CSR.

The install pass reads the shared memory twice: once in the dry-run to
allocate slots, and again during install to program them. S-mode can
rewrite the entry in between. Validating at the CSR write point closes
this window and covers all three callers: install_trig, enable_trig,
and update_trig.

Fixes: 97f234f15c96 ("lib: sbi: Introduce the SBI debug triggers extension support")
Reported-by: Nicholas Piggin <npiggin at gmail.com>
Signed-off-by: liutong <liutong at iscas.ac.cn>
---
v1: https://lore.kernel.org/opensbi/20261125030930.3743680-1-liutong@iscas.ac.cn/
v2: resolve the unresolved conflict markers accidentally included in v1's
    update_trig hunk, add the missing forward declaration for
    dbtr_trigger_valid(), and free the trigger allocated in install_trig
    when the new check fails.

Applies on top of

  [PATCH v2] lib: sbi: dbtr: validate trigger configuration in update_trig
  https://lore.kernel.org/opensbi/20261115163732.746589-1-liutong@iscas.ac.cn/

Build-tested with PLATFORM=generic for RV64 and RV32.

 lib/sbi/sbi_dbtr.c | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/lib/sbi/sbi_dbtr.c b/lib/sbi/sbi_dbtr.c
index 9b5d25617194..d2617252f305 100644
--- a/lib/sbi/sbi_dbtr.c
+++ b/lib/sbi/sbi_dbtr.c
@@ -460,17 +460,27 @@ static inline void update_bit(unsigned long new, int nr, volatile unsigned long
 		__clear_bit(nr, addr);
 }
 
-static void dbtr_trigger_enable(struct sbi_dbtr_trigger *trig)
+static int dbtr_trigger_valid(unsigned long type, unsigned long tdata);
+
+static int dbtr_trigger_enable(struct sbi_dbtr_trigger *trig)
 {
 	unsigned long state;
 	unsigned long tdata1;
 
 	if (!trig || !(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)))
-		return;
+		return SBI_ERR_FAILED;
 
 	state = trig->state;
 	tdata1 = trig->tdata1;
 
+	/*
+	 * Validate tdata1 before programming CSR. S-mode can modify the shared
+	 * memory between the validation pass and the install pass, allowing an
+	 * invalid configuration (with DMODE or M bit set) to reach CSR_TDATA1.
+	 */
+	if (!dbtr_trigger_valid(TDATA1_GET_TYPE(tdata1), tdata1))
+		return SBI_ERR_INVALID_PARAM;
+
 	switch (TDATA1_GET_TYPE(tdata1)) {
 	case RISCV_DBTR_TRIG_MCONTROL:
 		update_bit(state & RV_DBTR_BIT_MASK(TS, U),
@@ -516,6 +526,8 @@ static void dbtr_trigger_enable(struct sbi_dbtr_trigger *trig)
 	tdata_write_safe(CSR_TDATA2, trig->tdata2);
 	tdata_write_safe(CSR_TDATA3, trig->tdata3);
 	csr_write(CSR_TDATA1, trig->tdata1);
+
+	return SBI_SUCCESS;
 }
 
 static void dbtr_trigger_disable(struct sbi_dbtr_trigger *trig)
@@ -687,6 +699,7 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	unsigned long ctrl;
 	u32 claimed = 0;
 	int slot;
+	int ret;
 	struct sbi_dbtr_trigger *trig;
 	struct sbi_dbtr_hart_triggers_state *hs = NULL;
 	bool tdata2_impl, tdata3_impl;
@@ -796,7 +809,14 @@ int sbi_dbtr_install_trig(unsigned long smode,
 					 lle_to_cpu(recv->tdata3));
 
 		dbtr_trigger_setup(trig,  recv);
-		dbtr_trigger_enable(trig);
+		ret = dbtr_trigger_enable(trig);
+		if (ret != SBI_SUCCESS) {
+			sbi_free_trigger(trig);
+			*out = _idx;
+			sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base,
+							     trig_count * sizeof(*entry));
+			return ret;
+		}
 		xmit->idx = cpu_to_lle(trig->index);
 
 	}
@@ -839,6 +859,7 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 	unsigned long idx = trig_idx_base;
 	struct sbi_dbtr_trigger *trig;
 	struct sbi_dbtr_hart_triggers_state *hs;
+	int ret;
 
 	hs = dbtr_thishart_state_ptr();
 	if (!hs)
@@ -847,7 +868,9 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
 		trig = INDEX_TO_TRIGGER(idx);
 		sbi_dprintf("%s: enable trigger %lu\n", __func__, idx);
-		dbtr_trigger_enable(trig);
+		ret = dbtr_trigger_enable(trig);
+		if (ret != SBI_SUCCESS)
+			return ret;
 	}
 
 	return SBI_SUCCESS;
@@ -857,6 +880,7 @@ int sbi_dbtr_update_trig(unsigned long smode,
 			 unsigned long trig_count)
 {
 	unsigned long trig_idx, tdata1;
+	int ret;
 	union sbi_dbtr_shmem_entry local;
 	struct sbi_dbtr_trigger *trig;
 	union sbi_dbtr_shmem_entry *entry;
@@ -922,7 +946,9 @@ int sbi_dbtr_update_trig(unsigned long smode,
 			return SBI_ERR_NOT_SUPPORTED;
 
 		dbtr_trigger_setup(trig, &local.data);
-		dbtr_trigger_enable(trig);
+		ret = dbtr_trigger_enable(trig);
+		if (ret != SBI_SUCCESS)
+			return ret;
 	}
 
 	return SBI_SUCCESS;
-- 
2.34.1




More information about the opensbi mailing list