[PATCH v2] lib: sbi: dbtr: validate trigger configuration in update_trig
liutong
liutong at iscas.ac.cn
Sun Nov 15 08:37:32 PST 2026
sbi_dbtr_update_trig() writes the caller's tdata1 to the trigger CSRs
without validating it, so S-mode can install a trigger that install_trig
accepts and then set the mcontrol6 M bit on it with an update, making it
fire in M-mode. No race is involved. dbtr_trigger_hw_supported() does
not cover this: it returns true when the platform has no
trigger_supported callback.
The entry is also re-read from the shared memory, so the value validated
need not be the one programmed.
Copy the entry once, drop the mapping, and run the checks against the
copy. Also reject a tdata1 whose type is not set in trig->type_mask,
the capability the hardware slot was allocated for.
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>
---
Changes in v2:
- Reject a tdata1 whose type is not set in trig->type_mask.
- Link to v1:
https://lore.kernel.org/opensbi/20261103081001.2948003-1-liutong@iscas.ac.cn/
SBI v3.0 also requires tdata1.chain to be preserved across an update.
That needs a record of the tdata1 the trigger was installed with, and
trig->tdata1 is not one: sbi_dbtr_read_trig() refreshes it from the CSR,
which the hardware may have normalised. Left out of this patch.
lib/sbi/sbi_dbtr.c | 46 ++++++++++++++++++++++++++--------------------
1 file changed, 26 insertions(+), 20 deletions(-)
diff --git a/lib/sbi/sbi_dbtr.c b/lib/sbi/sbi_dbtr.c
index c97e625f..9b5d2561 100644
--- a/lib/sbi/sbi_dbtr.c
+++ b/lib/sbi/sbi_dbtr.c
@@ -856,7 +856,8 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
int sbi_dbtr_update_trig(unsigned long smode,
unsigned long trig_count)
{
- unsigned long trig_idx;
+ unsigned long trig_idx, tdata1;
+ union sbi_dbtr_shmem_entry local;
struct sbi_dbtr_trigger *trig;
union sbi_dbtr_shmem_entry *entry;
void *shmem_base = NULL;
@@ -885,37 +886,42 @@ int sbi_dbtr_update_trig(unsigned long smode,
tdata3_impl = tdata_implemented(CSR_TDATA3);
for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
+ /*
+ * Take a copy and drop the mapping. S-mode can rewrite the
+ * shared memory at any time, so every check below and the
+ * value finally programmed must come from the same copy.
+ */
sbi_hart_protection_temp_map_range((unsigned long)entry, sizeof(*entry));
- trig_idx = entry->id.idx;
+ local = *entry;
+ sbi_hart_protection_temp_unmap_range((unsigned long)entry, sizeof(*entry));
- if (trig_idx >= hs->total_trigs) {
- sbi_hart_protection_temp_unmap_range((unsigned long)entry, sizeof(*entry));
+ trig_idx = local.id.idx;
+ if (trig_idx >= hs->total_trigs)
return SBI_ERR_INVALID_PARAM;
- }
trig = INDEX_TO_TRIGGER(trig_idx);
- if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED))) {
- sbi_hart_protection_temp_unmap_range((unsigned long)entry, sizeof(*entry));
+ if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)))
return SBI_ERR_FAILED;
- }
- if ((entry->data.tdata2 && !tdata2_impl) ||
- (entry->data.tdata3 && !tdata3_impl)) {
- sbi_hart_protection_temp_unmap_range((unsigned long)entry, sizeof(*entry));
+ tdata1 = lle_to_cpu(local.data.tdata1);
+
+ if (!dbtr_trigger_valid(TDATA1_GET_TYPE(tdata1), tdata1))
+ return SBI_ERR_INVALID_PARAM;
+
+ if (!__test_bit(TDATA1_GET_TYPE(tdata1), &trig->type_mask))
+ return SBI_ERR_INVALID_PARAM;
+
+ if ((local.data.tdata2 && !tdata2_impl) ||
+ (local.data.tdata3 && !tdata3_impl))
return SBI_ERR_NOT_SUPPORTED;
- }
- if (!dbtr_trigger_hw_supported(trig->index,
- lle_to_cpu(entry->data.tdata1),
- lle_to_cpu(entry->data.tdata2),
- lle_to_cpu(entry->data.tdata3))) {
- sbi_hart_protection_temp_unmap_range((unsigned long)entry, sizeof(*entry));
+ if (!dbtr_trigger_hw_supported(trig->index, tdata1,
+ lle_to_cpu(local.data.tdata2),
+ lle_to_cpu(local.data.tdata3)))
return SBI_ERR_NOT_SUPPORTED;
- }
- dbtr_trigger_setup(trig, &entry->data);
- sbi_hart_protection_temp_unmap_range((unsigned long)entry, sizeof(*entry));
+ dbtr_trigger_setup(trig, &local.data);
dbtr_trigger_enable(trig);
}
base-commit: ed70256dd22c09962d146e433834239825ef0616
--
2.34.1
More information about the opensbi
mailing list