Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 27 Sep 2021 17:39:11 GMT
From:      Mitchell Horne <mhorne@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: bcddaadbef58 - main - rman: fix overflow in rman_reserve_resource_bound()
Message-ID:  <202109271739.18RHdBFT068993@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=bcddaadbef5850ed9f040836d3f25ff57138ae28

commit bcddaadbef5850ed9f040836d3f25ff57138ae28
Author:     Elliott Mitchell <ehem+freebsd@m5p.com>
AuthorDate: 2021-09-27 17:13:19 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2021-09-27 17:38:26 +0000

    rman: fix overflow in rman_reserve_resource_bound()
    
    If the default range of [0, ~0] is given, then (~0 - 0) + 1 == 0. This
    in turn will cause any allocation of non-zero size to fail. Zero-sized
    allocations are prohibited, so add a KASSERT to this effect.
    
    History indicates it is part of the original rman code.  This bug may in
    fact be older than some contributors.
    
    Reviewed by:    mhorne
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D30280
---
 sys/kern/subr_rman.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/sys/kern/subr_rman.c b/sys/kern/subr_rman.c
index a8f5188e7f54..1bbaff8264ef 100644
--- a/sys/kern/subr_rman.c
+++ b/sys/kern/subr_rman.c
@@ -445,6 +445,8 @@ rman_reserve_resource_bound(struct rman *rm, rman_res_t start, rman_res_t end,
 	       "length %#jx, flags %x, device %s\n", rm->rm_descr, start, end,
 	       count, flags,
 	       dev == NULL ? "<null>" : device_get_nameunit(dev)));
+	KASSERT(count != 0, ("%s: attempted to allocate an empty range",
+	    __func__));
 	KASSERT((flags & RF_FIRSTSHARE) == 0,
 	    ("invalid flags %#x", flags));
 	new_rflags = (flags & ~RF_FIRSTSHARE) | RF_ALLOCATED;
@@ -520,7 +522,7 @@ rman_reserve_resource_bound(struct rman *rm, rman_res_t start, rman_res_t end,
 		DPRINTF(("truncated region: [%#jx, %#jx]; size %#jx (requested %#jx)\n",
 		       rstart, rend, (rend - rstart + 1), count));
 
-		if ((rend - rstart + 1) >= count) {
+		if ((rend - rstart) >= (count - 1)) {
 			DPRINTF(("candidate region: [%#jx, %#jx], size %#jx\n",
 			       rstart, rend, (rend - rstart + 1)));
 			if ((s->r_end - s->r_start + 1) == count) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202109271739.18RHdBFT068993>