From owner-dev-commits-src-all@freebsd.org Mon Sep 27 17:39:11 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 9D33266C7F3; Mon, 27 Sep 2021 17:39:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4HJ8yR2tPPz4SkC; Mon, 27 Sep 2021 17:39:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 438B815515; Mon, 27 Sep 2021 17:39:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 18RHdBbT068994; Mon, 27 Sep 2021 17:39:11 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 18RHdBFT068993; Mon, 27 Sep 2021 17:39:11 GMT (envelope-from git) Date: Mon, 27 Sep 2021 17:39:11 GMT Message-Id: <202109271739.18RHdBFT068993@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mitchell Horne Subject: git: bcddaadbef58 - main - rman: fix overflow in rman_reserve_resource_bound() MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: bcddaadbef5850ed9f040836d3f25ff57138ae28 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Sep 2021 17:39:11 -0000 The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=bcddaadbef5850ed9f040836d3f25ff57138ae28 commit bcddaadbef5850ed9f040836d3f25ff57138ae28 Author: Elliott Mitchell AuthorDate: 2021-09-27 17:13:19 +0000 Commit: Mitchell Horne 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 ? "" : 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) {