[PATCH 3/7] lib: sbi: dbtr: check the whole shared memory range in SETUP_SHMEM

Heinrich Schuchardt heinrich.schuchardt at canonical.com
Fri Nov 20 15:11:15 PST 2026


The shared memory of the debug trigger extension is trig_max * (XLEN / 2)
bytes long. The specification requires the SBI implementation to check
that the whole physical memory range is accessible for the caller, see the
chapter on the shared memory physical address range parameter.

sbi_dbtr_setup_shmem() only tested the first address. A range that starts
in memory of S-mode and reaches into memory protected by PMP was
accepted. A later read of the triggers then makes M-mode write into the
protected memory. A range at the top of the address space that wraps
around was accepted too.

Use sbi_domain_check_addr_range() with the size of the shared memory. It
also rejects a range that wraps around.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
---
 lib/sbi/sbi_dbtr.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/lib/sbi/sbi_dbtr.c b/lib/sbi/sbi_dbtr.c
index 53fcb0a2..fa0e5962 100644
--- a/lib/sbi/sbi_dbtr.c
+++ b/lib/sbi/sbi_dbtr.c
@@ -385,9 +385,20 @@ int sbi_dbtr_setup_shmem(const struct sbi_domain *dom, unsigned long smode,
 	if (shmem_phys_hi)
 		return SBI_EINVALID_ADDR;
 
-	if (dom && !sbi_domain_check_addr(dom,
-		  DBTR_SHMEM_MAKE_PHYS(shmem_phys_hi, shmem_phys_lo), smode,
-		  SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))
+	/*
+	 * The shared memory is trig_max * (XLEN / 2) bytes. All of it has to be
+	 * readable and writable for the caller, not only its first byte,
+	 * otherwise a read of the triggers would write into memory that the
+	 * caller cannot access. A range that wraps around is rejected too.
+	 */
+	if (dom &&
+	    !sbi_domain_check_addr_range(dom,
+					 DBTR_SHMEM_MAKE_PHYS(shmem_phys_hi,
+							      shmem_phys_lo),
+					 hart_state->total_trigs *
+						 sizeof(union sbi_dbtr_shmem_entry),
+					 smode,
+					 SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))
 		return SBI_ERR_INVALID_ADDRESS;
 
 	hart_state->shmem.phys_lo = shmem_phys_lo;
-- 
2.53.0




More information about the opensbi mailing list