From nobody Tue Oct 20 17:50:41 2026 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4j8KhK37rKz6vs08 for ; Tue, 20 Oct 2026 17:50:41 +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) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4j8KhK2Qmzz4lkt for ; Tue, 20 Oct 2026 17:50:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1792518641; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=GxMLA6hPop0hgoyylMWpBq6dm4HjIBOTyc4HQb+JI9U=; b=j6lU/iyKSctVWoY2cwlkGKaz9QN1LhBg7prDBVKMJFMJk55/u+3/+ooXiNDJa2iiyjjdZN APdDu/1XP5jgVeG/HJeVZkM977wYte8z7sinMEYaX08858MzzQv3k2HZNmOIvV+iOIRgHV J4rVZvcmwElcVL+JFAki2+VNDDy1Wp6x/T6L9VzxCDSRjifLsbiTbP8EdURGofXSnsUiVL EB0BKKA5+cRatU4dTZIR9tyaMqjdB+S2taFqZddfuDg9dY0mrq9tonv9jZPPex8dc5SPD8 cZ96+h0n1LF6p9RfZG/xXB/7pDJoSJrAlZ9/WqAqFXYcWfFbIJJyojbGqfJXLw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1792518641; b=bG+KYkkcH8MGJyNJ5X/qTWEr9dVBkAl6h+cxAm8cXV8tajFHgzPbkjNAAuhzNc4YdMCzQr xWtCun5CcP3wqKMlFGfJIkFGhPOQmzZPU6gFCqSF2afZsHQW/eUyF+qNc5QGiTkY4Trrya r67gNArCaUxy54c2Axd34wZXWcuh9TJ3oAsgMDKpyeZ8cEkDQlu0jvOfHtJHBVu2Np0iBU O5WF/DPJUC4nWL5LWiRVA2YKQgLaIhPJgUJfdUvsiMygQZSaEdf3X0u/lFdaQIRVe38rhj qU2vtMbiR3S26yPazXCRkRRt55KE/23UWsLunmX3b2FFgdWUKk/6+uZpSf1w8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1792518641; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=GxMLA6hPop0hgoyylMWpBq6dm4HjIBOTyc4HQb+JI9U=; b=QZm544ipj1gGnUMqDvUUIxKCG+yjarT4r+ayKWQJ9MfVW/L14vSqcjYa8H8MbII4XsivL+ kTUMwG5JAb/d8SWUkcdbLBURQPFn8Fboqhvt2DFfyKjFNiAoLW9EgYVeqOvuqILjZWtE0n G3Qy10/UrzQq1LoPq2sqMWCaKmpnGAgACOYnnDXyEbfW5mVBlPeoPE8lVoQ4/l6ZhCY7gn CW8mxfK5vOlzKircERr47wfwQ6hi+y4qVyAizNj8vC4zEc6NEjiGglDN3xm/BwuA0CIBqQ rKf8tUha0U25hDxj/QBf8G/eJb1aEzW227Ikmej+UFVmrWVlwTdNnrxViYou9A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4j8KhK1nJdzHCy for ; Tue, 20 Oct 2026 17:50:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 27fc0 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 20 Oct 2026 17:50:41 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Roman Bogorodskiy Subject: git: eefc98f73a08 - stable/15 - stand: userboot: improve userdisk error handling List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org List-Id: Commits to the stable branches of the FreeBSD src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: novel X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: eefc98f73a08b331ef77921268a99e285aa9ed52 Auto-Submitted: auto-generated Date: Tue, 20 Oct 2026 17:50:41 +0000 Message-Id: <6ad7a9f1.27fc0.4528d9c4@gitrepo.freebsd.org> The branch stable/15 has been updated by novel: URL: https://cgit.FreeBSD.org/src/commit/?id=eefc98f73a08b331ef77921268a99e285aa9ed52 commit eefc98f73a08b331ef77921268a99e285aa9ed52 Author: Roman Bogorodskiy AuthorDate: 2026-08-28 16:43:26 +0000 Commit: Roman Bogorodskiy CommitDate: 2026-10-20 15:55:05 +0000 stand: userboot: improve userdisk error handling Currently, userdisk_init() iterates through disks and checks whether DIOCGSECTORSIZE and DIOCGMEDIASIZE ioctls are available for the device. If either ioctl fails, userdisk_init() returns ENXIO with ud_info only partially initialized. devinit() records the failure, but loader_main() ignores devinit()'s return value, so later code may access an uninitialized entry. In certain cases this code can still boot from a disk even if userdisk_init() returns ENXIO, for example, when the first of two disks is valid and the second errors out. To address this issue while not breaking the current behavior, do the following: - Zero-initialize the userdisk_info array and use a zero media size to mark disks that do not support the ioctls mentioned above or report an empty medium. - Update userdisk_print() not to print unusable disks. - Update userdisk_open() to return EIO for unusable disks. - Update extract_currdev() to pick the first usable disk instead of the first disk. Introduce userboot_disk_firstunit() for that. Additionally, reset counters on ud_info allocation failure to prevent accessing unallocated data. Reviewed by: jhb (previous version), markj MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D59262 (cherry picked from commit 4445d039583a0a47fc335e0d34401581bcbc104e) --- stand/userboot/userboot/libuserboot.h | 1 + stand/userboot/userboot/main.c | 9 ++++--- stand/userboot/userboot/userboot_disk.c | 45 +++++++++++++++++++++++---------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/stand/userboot/userboot/libuserboot.h b/stand/userboot/userboot/libuserboot.h index e1584ec29412..0844e62684f6 100644 --- a/stand/userboot/userboot/libuserboot.h +++ b/stand/userboot/userboot/libuserboot.h @@ -39,6 +39,7 @@ typedef unsigned long physaddr_t; /* exported devices */ extern struct devsw userboot_disk; extern int userboot_disk_maxunit; +int userboot_disk_firstunit(void); extern struct devsw host_dev; /* access to host filesystem */ diff --git a/stand/userboot/userboot/main.c b/stand/userboot/userboot/main.c index 03f226c08bb9..b518e18020f8 100644 --- a/stand/userboot/userboot/main.c +++ b/stand/userboot/userboot/main.c @@ -235,9 +235,13 @@ extract_currdev(void) { struct disk_devdesc dev; struct devdesc *dd; + int diskunit; #if defined(USERBOOT_ZFS_SUPPORT) struct zfs_devdesc zdev; +#endif + diskunit = userboot_disk_firstunit(); +#if defined(USERBOOT_ZFS_SUPPORT) if (userboot_zfs_found) { /* Leave the pool/root guid's unassigned */ @@ -248,10 +252,9 @@ extract_currdev(void) dd = &zdev.dd; } else #endif - - if (userboot_disk_maxunit > 0) { + if (diskunit >= 0) { dev.dd.d_dev = &userboot_disk; - dev.dd.d_unit = 0; + dev.dd.d_unit = diskunit; dev.d_slice = D_SLICEWILD; dev.d_partition = D_PARTWILD; /* diff --git a/stand/userboot/userboot/userboot_disk.c b/stand/userboot/userboot/userboot_disk.c index 9adcad4f00ac..78377c582225 100644 --- a/stand/userboot/userboot/userboot_disk.c +++ b/stand/userboot/userboot/userboot_disk.c @@ -82,25 +82,29 @@ userdisk_init(void) off_t mediasize; u_int sectorsize; int i; + int userdisk_usable = 0; userdisk_maxunit = userboot_disk_maxunit; if (userdisk_maxunit > 0) { - ud_info = malloc(sizeof(*ud_info) * userdisk_maxunit); - if (ud_info == NULL) + ud_info = calloc(userdisk_maxunit, sizeof(*ud_info)); + if (ud_info == NULL) { + userdisk_maxunit = 0; + userboot_disk_maxunit = 0; return (ENOMEM); + } for (i = 0; i < userdisk_maxunit; i++) { if (CALLBACK(diskioctl, i, DIOCGSECTORSIZE, - §orsize) != 0 || CALLBACK(diskioctl, i, - DIOCGMEDIASIZE, &mediasize) != 0) - return (ENXIO); - ud_info[i].mediasize = mediasize; - ud_info[i].sectorsize = sectorsize; - ud_info[i].ud_open = 0; - ud_info[i].ud_bcache = NULL; + §orsize) == 0 && CALLBACK(diskioctl, i, + DIOCGMEDIASIZE, &mediasize) == 0 && mediasize > 0) { + ud_info[i].mediasize = mediasize; + ud_info[i].sectorsize = sectorsize; + userdisk_usable++; + } } } - bcache_add_dev(userdisk_maxunit); - return(0); + bcache_add_dev(userdisk_usable); + + return (0); } static void @@ -111,6 +115,18 @@ userdisk_cleanup(void) free(ud_info); } +int +userboot_disk_firstunit(void) +{ + int i; + + for (i = 0; i < userdisk_maxunit; i++) { + if (ud_info[i].mediasize != 0) + return (i); + } + return (-1); +} + /* * Print information about disks */ @@ -121,7 +137,7 @@ userdisk_print(int verbose) char line[80]; int i, ret = 0; - if (userdisk_maxunit == 0) + if (userboot_disk_firstunit() < 0) return (0); printf("%s devices:", userboot_disk.dv_name); @@ -129,6 +145,8 @@ userdisk_print(int verbose) return (ret); for (i = 0; i < userdisk_maxunit; i++) { + if (ud_info[i].mediasize == 0) + continue; snprintf(line, sizeof(line), " disk%d: Guest drive image\n", i); ret = pager_output(line); @@ -163,7 +181,8 @@ userdisk_open(struct open_file *f, ...) dev = va_arg(ap, struct disk_devdesc *); va_end(ap); - if (dev->dd.d_unit < 0 || dev->dd.d_unit >= userdisk_maxunit) + if (dev->dd.d_unit < 0 || dev->dd.d_unit >= userdisk_maxunit || + ud_info[dev->dd.d_unit].mediasize == 0) return (EIO); ud_info[dev->dd.d_unit].ud_open++; if (ud_info[dev->dd.d_unit].ud_bcache == NULL)