[PATCH] lib: sbi: dbtr: validate trigger configuration before writing CSR
liutong
liutong at iscas.ac.cn
Tue Nov 24 19:09:30 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>
---
lib/sbi/sbi_dbtr.c | 37 +++++++++++++++++++++++++++++++++----
1 file changed, 33 insertions(+), 4 deletions(-)
diff --git a/lib/sbi/sbi_dbtr.c b/lib/sbi/sbi_dbtr.c
index 9b5d25617194..fa75ee356632 100644
--- a/lib/sbi/sbi_dbtr.c
+++ b/lib/sbi/sbi_dbtr.c
@@ -460,17 +460,25 @@ 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_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 validation pass and install pass, allowing an invalid
+ * configuration (with DMODE or M bit set) to reach CSR_TDATA1. Reject it here.
+ */
+ 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 +524,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)
@@ -785,6 +795,7 @@ int sbi_dbtr_install_trig(unsigned long smode,
for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
recv = (struct sbi_dbtr_data_msg *)(&entry->data);
xmit = (struct sbi_dbtr_id_msg *)(&entry->id);
+ int ret;
/*
* The dry-run above matched every requested configuration
@@ -796,7 +807,13 @@ 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) {
+ *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);
}
@@ -847,7 +864,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);
+ int ret = dbtr_trigger_enable(trig);
+ if (ret != SBI_SUCCESS)
+ return ret;
}
return SBI_SUCCESS;
@@ -921,8 +940,18 @@ int sbi_dbtr_update_trig(unsigned long smode,
lle_to_cpu(local.data.tdata3)))
return SBI_ERR_NOT_SUPPORTED;
+<<<<<<< Updated upstream
dbtr_trigger_setup(trig, &local.data);
dbtr_trigger_enable(trig);
+=======
+ dbtr_trigger_setup(trig, &entry->data);
+ sbi_hart_protection_temp_unmap_range((unsigned long)entry, sizeof(*entry));
+ {
+ int ret = dbtr_trigger_enable(trig);
+ if (ret != SBI_SUCCESS)
+ return ret;
+ }
+>>>>>>> Stashed changes
}
return SBI_SUCCESS;
--
2.34.1
More information about the opensbi
mailing list