Skip to contentSkip to conversation

lib: sbi_domain: reject overflowing address range in check_addr_range()

PATCH In review Takumi Hara 2 participants 4 messages Mar 19 – May 11

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: sbi_domain: reject overflowing address range in check_addr_range()

sbi_domain_check_addr_range() computes max = addr + size without checking for integer overflow. When a caller passes a size large enough to wrap around (e.g. addr=0x80000000, size=0xFFFFFFFF80000000), max becomes less than addr, causing the while(addr < max) validation loop to be skipped entirely. The function then returns true without performing any permission checks.

This allows an S-mode caller to bypass domain memory protection and access M-mode memory through SBI extensions that use address range validation (e.g. DBCN console write/read).

Add an overflow check after computing max: if size is non-zero and max wrapped to a value <= addr, reject the request.

---
 lib/sbi/sbi_domain.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/sbi_domain.c +3 -0
index 7030848..3df521f 100644
@@ -505,6 +505,9 @@ bool sbi_domain_check_addr_range(const struct sbi_domain *dom,
 	if (!dom)
 		return false;
 
+	if (size && max <= addr)
+		return false;
+
 	while (addr < max) {
 		reg = find_region(dom, addr);
 		if (!reg)
Signature · 7 lines
2.52.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw
Quoted 25 lines · Takumi Hara

sbi_domain_check_addr_range() computes max = addr + size without checking for integer overflow. When a caller passes a size large enough to wrap around (e.g. addr=0x80000000, size=0xFFFFFFFF80000000), max becomes less than addr, causing the while(addr < max) validation loop to be skipped entirely. The function then returns true without performing any permission checks.

This allows an S-mode caller to bypass domain memory protection and access M-mode memory through SBI extensions that use address range validation (e.g. DBCN console write/read).

Add an overflow check after computing max: if size is non-zero and max wrapped to a value <= addr, reject the request.

---
 lib/sbi/sbi_domain.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/sbi_domain.c +0 -0
index 7030848..3df521f 100644

lib/sbi/sbi_domain.c bool sbi_domain_check_addr_range(const struct sbi_domain *dom, · line 513

Quoted 9 lines
lib/sbi/sbi_domain.c +3 -0
@@ -505,6 +505,9 @@ bool sbi_domain_check_addr_range(const struct sbi_domain *dom,
        if (!dom)
                return false;
 
+       if (size && max <= addr)
+               return false;
+
        while (addr < max) {
                reg = find_region(dom, addr);
                if (!reg)

What about the condition when passed size is 0 and max == addr.

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#2 lore raw

Thanks for the review, Rahul.

When size == 0, max == addr, so the overflow guard is skipped (it's not an overflow), and the while(addr < max) loop is also skipped since addr == max. The function returns true.

This is the existing behavior and is intentional -- a zero-length range has no bytes to validate, so returning true is correct.

This patch only targets the overflow case where a non-zero size wraps max around to <= addr, which silently skips all permission checks on a range that actually covers memory.

Best regards,
Takumi Hara

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by
#3 lore raw

Hi Takumi,

I believe from the semantics of this function
if the size passed is 0 which makes its a invalid range in which case this function should return false rather than true.
Caller of this function if proceeds with the address without any checks happening inside the function just because it passed the size == 0 will be incorrect use, even though will be trapped later.

Will let you and the maintainer decide on this behaviour.

Since this behaviour is existing and the fix you have provided is still applicable.

Thanks
Rahul

Quoted 16 lines · Takumi Hara

Thanks for the review, Rahul.

When size == 0, max == addr, so the overflow guard is skipped (it's not an overflow), and the while(addr < max) loop is also skipped since addr == max. The function returns true.

This is the existing behavior and is intentional -- a zero-length range has no bytes to validate, so returning true is correct.

This patch only targets the overflow case where a non-zero size wraps max around to <= addr, which silently skips all permission checks on a range that actually covers memory.

Best regards,
Takumi Hara

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi