[PATCH 1/2] lib: sbi: check the result of sbi_hart_protection_temp_map_range()

liutong liutong at iscas.ac.cn
Thu Nov 19 07:55:10 PST 2026


No caller looks at it. The Smepmp backend declines when no NAPOT block
fits the request, and supervisor software can ask for one: a debug
console write with num_bytes = 2^63 passes sbi_domain_check_addr_range()
against the root domain's catch-all region, and the backend then returns
SBI_EFAIL. The entry is not programmed, M-mode has no access to the
buffer under Smepmp, and the access that follows faults and leaves the
hart stopped with no output. The backend also declines, defensively,
when the reserved entry is already in use.

Return instead of touching memory the firmware could not map, with an
error each function is allowed to return: SBI_ERR_INVALID_PARAM for the
debug console, matching the address check above it, SBI_ERR_FAILED for
the debug trigger and message proxy calls, and SBI_ERR_INVALID_ADDRESS
where the neighbouring check already uses it.

Fixes: 0ad866067d78 ("lib: sbi: Map/Unmap debug console shared memory buffers")
Suggested-by: Sebastian Alba Vives <sebasjosue84 at gmail.com>
Signed-off-by: liutong <liutong at iscas.ac.cn>
---
 lib/sbi/sbi_dbtr.c       | 18 +++++++++++++-----
 lib/sbi/sbi_ecall_dbcn.c |  3 ++-
 lib/sbi/sbi_mpxy.c       | 24 ++++++++++++++++++------
 lib/sbi/sbi_pmu.c        |  3 ++-
 lib/sbi/sbi_sse.c        | 10 ++++++++--
 5 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/lib/sbi/sbi_dbtr.c b/lib/sbi/sbi_dbtr.c
index c97e625f..ad1dee5e 100644
--- a/lib/sbi/sbi_dbtr.c
+++ b/lib/sbi/sbi_dbtr.c
@@ -657,8 +657,10 @@ int sbi_dbtr_read_trig(unsigned long smode,
 
 	shmem_base = hart_shmem_base(hs);
 
-	sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
-					   trig_count * sizeof(*entry));
+	if (sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
+					       trig_count * sizeof(*entry)))
+		return SBI_ERR_FAILED;
+
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
 		xmit = &entry->data;
 		trig = INDEX_TO_TRIGGER((_idx + trig_idx_base));
@@ -702,8 +704,11 @@ int sbi_dbtr_install_trig(unsigned long smode,
 		return SBI_ERR_NO_SHMEM;
 
 	shmem_base = hart_shmem_base(hs);
-	sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
-					   trig_count * sizeof(*entry));
+	if (sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
+					       trig_count * sizeof(*entry))) {
+		*out = 0;
+		return SBI_ERR_FAILED;
+	}
 
 	/*
 	 * SBI v3.0 sec 19.4 requires SBI_ERR_NOT_SUPPORTED when a trigger
@@ -885,7 +890,10 @@ 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) {
-		sbi_hart_protection_temp_map_range((unsigned long)entry, sizeof(*entry));
+		if (sbi_hart_protection_temp_map_range((unsigned long)entry,
+						       sizeof(*entry)))
+			return SBI_ERR_FAILED;
+
 		trig_idx = entry->id.idx;
 
 		if (trig_idx >= hs->total_trigs) {
diff --git a/lib/sbi/sbi_ecall_dbcn.c b/lib/sbi/sbi_ecall_dbcn.c
index 76837737..72cbfb2a 100644
--- a/lib/sbi/sbi_ecall_dbcn.c
+++ b/lib/sbi/sbi_ecall_dbcn.c
@@ -46,7 +46,8 @@ static int sbi_ecall_dbcn_handler(unsigned long extid, unsigned long funcid,
 					regs->a1, regs->a0, smode,
 					SBI_DOMAIN_READ|SBI_DOMAIN_WRITE))
 			return SBI_ERR_INVALID_PARAM;
-		sbi_hart_protection_temp_map_range(regs->a1, regs->a0);
+		if (sbi_hart_protection_temp_map_range(regs->a1, regs->a0))
+			return SBI_ERR_INVALID_PARAM;
 		if (funcid == SBI_EXT_DBCN_CONSOLE_WRITE)
 			out->value = sbi_nputs((const char *)regs->a1, regs->a0);
 		else
diff --git a/lib/sbi/sbi_mpxy.c b/lib/sbi/sbi_mpxy.c
index a09a09dc..9742304a 100644
--- a/lib/sbi/sbi_mpxy.c
+++ b/lib/sbi/sbi_mpxy.c
@@ -401,7 +401,9 @@ int sbi_mpxy_set_shmem(unsigned long shmem_phys_lo,
 	if (flags == SBI_EXT_MPXY_SHMEM_FLAG_OVERWRITE_RETURN) {
 		ret_buf = (unsigned long *)(ulong)SHMEM_PHYS_ADDR(shmem_phys_hi,
 								  shmem_phys_lo);
-		sbi_hart_protection_temp_map_range((unsigned long)ret_buf, mpxy_shmem_size);
+		if (sbi_hart_protection_temp_map_range((unsigned long)ret_buf,
+						       mpxy_shmem_size))
+			return SBI_ERR_INVALID_ADDRESS;
 		ret_buf[0] = cpu_to_lle(ms->shmem.shmem_addr_lo);
 		ret_buf[1] = cpu_to_lle(ms->shmem.shmem_addr_hi);
 		sbi_hart_protection_temp_unmap_range((unsigned long)ret_buf, mpxy_shmem_size);
@@ -436,7 +438,9 @@ int sbi_mpxy_get_channel_ids(u32 start_index)
 		return SBI_ERR_INVALID_PARAM;
 
 	shmem_base = hart_shmem_base(ms);
-	sbi_hart_protection_temp_map_range((unsigned long)hart_shmem_base(ms), mpxy_shmem_size);
+	if (sbi_hart_protection_temp_map_range((unsigned long)hart_shmem_base(ms),
+					       mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 
 	/** number of channel ids which can be stored in shmem adjusting
 	 * for remaining and returned fields */
@@ -499,7 +503,9 @@ int sbi_mpxy_read_attrs(u32 channel_id, u32 base_attr_id, u32 attr_count)
 	shmem_base = hart_shmem_base(ms);
 	end_id = (u64)base_attr_id + attr_count - 1;
 
-	sbi_hart_protection_temp_map_range((unsigned long)hart_shmem_base(ms), mpxy_shmem_size);
+	if (sbi_hart_protection_temp_map_range((unsigned long)hart_shmem_base(ms),
+					       mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 
 	/* Standard attributes range check */
 	if (mpxy_is_std_attr(base_attr_id)) {
@@ -652,7 +658,9 @@ int sbi_mpxy_write_attrs(u32 channel_id, u32 base_attr_id, u32 attr_count)
 	shmem_base = hart_shmem_base(ms);
 	end_id = (u64)base_attr_id + attr_count - 1;
 
-	sbi_hart_protection_temp_map_range((unsigned long)shmem_base, mpxy_shmem_size);
+	if (sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
+					       mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 
 	mem_ptr = (u32 *)shmem_base;
 
@@ -742,7 +750,9 @@ int sbi_mpxy_send_message(u32 channel_id, u8 msg_id,
 		return SBI_ERR_INVALID_PARAM;
 
 	shmem_base = hart_shmem_base(ms);
-	sbi_hart_protection_temp_map_range((unsigned long)shmem_base, mpxy_shmem_size);
+	if (sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
+					       mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 
 	if (resp_data_len) {
 		resp_buf = shmem_base;
@@ -790,7 +800,9 @@ int sbi_mpxy_get_notification_events(u32 channel_id, unsigned long *events_len)
 		return SBI_ERR_NOT_SUPPORTED;
 
 	shmem_base = hart_shmem_base(ms);
-	sbi_hart_protection_temp_map_range((unsigned long)shmem_base, mpxy_shmem_size);
+	if (sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
+					       mpxy_shmem_size))
+		return SBI_ERR_FAILED;
 	eventsbuf = shmem_base;
 	ret = channel->get_notification_events(channel, eventsbuf,
 					       mpxy_shmem_size,
diff --git a/lib/sbi/sbi_pmu.c b/lib/sbi/sbi_pmu.c
index f33238d8..793f2c3f 100644
--- a/lib/sbi/sbi_pmu.c
+++ b/lib/sbi/sbi_pmu.c
@@ -1136,7 +1136,8 @@ int sbi_pmu_event_get_info(unsigned long shmem_phys_lo, unsigned long shmem_phys
 					 SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))
 		return SBI_ERR_INVALID_ADDRESS;
 
-	sbi_hart_protection_temp_map_range(shmem_phys_lo, shmem_size);
+	if (sbi_hart_protection_temp_map_range(shmem_phys_lo, shmem_size))
+		return SBI_ERR_INVALID_ADDRESS;
 
 	einfo = (struct sbi_pmu_event_info *)(shmem_phys_lo);
 	for (i = 0; i < num_events; i++) {
diff --git a/lib/sbi/sbi_sse.c b/lib/sbi/sbi_sse.c
index 767ea128..5e2437aa 100644
--- a/lib/sbi/sbi_sse.c
+++ b/lib/sbi/sbi_sse.c
@@ -1022,7 +1022,11 @@ int sbi_sse_read_attrs(uint32_t event_id, uint32_t base_attr_id,
 	if (ret)
 		return ret;
 
-	sbi_hart_protection_temp_map_range(output_phys_lo, sizeof(unsigned long) * attr_count);
+	if (sbi_hart_protection_temp_map_range(output_phys_lo,
+					       sizeof(unsigned long) * attr_count)) {
+		sse_event_put(e);
+		return SBI_ERR_INVALID_ADDRESS;
+	}
 
 	/*
 	 * Copy all attributes at once since struct sse_event_attrs is matching
@@ -1050,7 +1054,9 @@ static int sse_write_attrs(struct sbi_sse_event *e, uint32_t base_attr_id,
 	uint32_t id, end_id = base_attr_id + attr_count;
 	unsigned long *attrs = (unsigned long *)input_phys;
 
-	sbi_hart_protection_temp_map_range(input_phys, sizeof(unsigned long) * attr_count);
+	if (sbi_hart_protection_temp_map_range(input_phys,
+					       sizeof(unsigned long) * attr_count))
+		return SBI_ERR_INVALID_ADDRESS;
 
 	for (id = base_attr_id; id < end_id; id++) {
 		val = attrs[attr++];
-- 
2.34.1




More information about the opensbi mailing list