From owner-dev-commits-src-branches@freebsd.org Tue Jul 27 01:49:37 2021 Return-Path: Delivered-To: dev-commits-src-branches@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 3AD1A654310; Tue, 27 Jul 2021 01:49:37 +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 4GYfqP0mZPz4vjm; Tue, 27 Jul 2021 01:49:36 +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 CB04821ECF; Tue, 27 Jul 2021 01:49:36 +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 16R1naPY067385; Tue, 27 Jul 2021 01:49:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 16R1na8S067384; Tue, 27 Jul 2021 01:49:36 GMT (envelope-from git) Date: Tue, 27 Jul 2021 01:49:36 GMT Message-Id: <202107270149.16R1na8S067384@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: f065d0bb299b - stable/13 - blist: Correct the node count computed in blist_create() MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: f065d0bb299bcedf1adc003397d340f88688ff75 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Jul 2021 01:49:37 -0000 The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=f065d0bb299bcedf1adc003397d340f88688ff75 commit f065d0bb299bcedf1adc003397d340f88688ff75 Author: Mark Johnston AuthorDate: 2021-07-13 21:47:27 +0000 Commit: Mark Johnston CommitDate: 2021-07-27 01:47:20 +0000 blist: Correct the node count computed in blist_create() Commit bb4a27f927a1 added the ability to allocate a span of blocks crossing a meta node boundary. To ensure that blst_next_leaf_alloc() does not walk past the end of the tree, an extra all-zero meta node needs to be present at the end of the allocation, and blst_next_leaf_alloc() is implemented such that the presence of this node terminates the search. blist_create() computes the number of nodes required. It had two problems: 1. When the size of the blist is a power of BLIST_RADIX, we would unnecessarily allocate an extra level in the tree. 2. When the size of the blist is a multiple of BLIST_RADIX, we would fail to allocate a terminator node. In this case, blst_next_leaf_alloc() could scan beyond the bounds of the allocation. This was found using KASAN. Modify blist_create() to handle these cases correctly. Reported by: pho Reviewed by: dougm (cherry picked from commit 2783335caeae964bd8a1aa15726b523876613e45) --- sys/kern/subr_blist.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sys/kern/subr_blist.c b/sys/kern/subr_blist.c index 27b226fd8a18..ba13fb4efd80 100644 --- a/sys/kern/subr_blist.c +++ b/sys/kern/subr_blist.c @@ -244,8 +244,16 @@ blist_create(daddr_t blocks, int flags) * Calculate the radix and node count used for scanning. */ nodes = 1; - for (radix = 1; radix <= blocks / BLIST_RADIX; radix *= BLIST_RADIX) - nodes += 1 + (blocks - 1) / radix / BLIST_RADIX; + for (radix = 1; (blocks - 1) / BLIST_RADIX / radix > 0; + radix *= BLIST_RADIX) + nodes += 1 + (blocks - 1) / BLIST_RADIX / radix; + + /* + * Include a sentinel node to ensure that cross-leaf scans stay within + * the bounds of the allocation. + */ + if (blocks % BLIST_RADIX == 0) + nodes++; bl = malloc(offsetof(struct blist, bl_root[nodes]), M_SWAP, flags | M_ZERO);