From nobody Tue Oct 06 16:59:05 2026 X-Original-To: dev-commits-src-main@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 4hzjCF2ybLz4nCPw for ; Tue, 06 Oct 2026 16:59:05 +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 "YR2" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4hzjCF2PwRz4thG for ; Tue, 06 Oct 2026 16:59:05 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791305945; 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=IuhZhfFfT/v2WHHg4WnnbXWN6ISBuUxKKx1AAMWOc1w=; b=BCRULUN5gpLJmFwcRKV6g53lajR8qyDnBFHvWs5mlMcxxMFb7Swdr/XfK5ciQNU3TSiFeV tykE5/XvGxCj15z0n6D6e3suxwzDvjdpdauPfEYkp8UsI8PODWmL5UWajJBe6UBeM0LyzB /FeC7FSO2LHOeavodGY7OBv1H1NGLYBRtTo4qehI1aHlA1R1Q5sgLlQyuWXy5MOER7W6mW GMBmTd2yVJmhqOsEmTKEacDNSMIWJK/1BWlmimJN4BaHsm9O2V34XK1KeiA6fd6PhoN4Mm hRd2X9Bl+2fjSal5CYGj07+x76L2x/GShQ8I82Z0CBkJzkKjJUke1A2qPVmN+A== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1791305945; b=TfbpWR4eODzw38TRwXZmJ8zU4gM9QqiZHjpBWGSxjaMOboDCqEISqbqF546IEniH1jJUOw M1MpVFxFnyh29ONXCAWe/WnMOqCwTkrb1GODN3jnNS+vWV891RGi7OlgmBqRxX8ji4IW3f OxOb6/EkZXk2KO6yaiRTnZ5V8lDBp+3AqdNhpGZvtlNImZAYByeqqWgWRnFTEdMVuem+va DT5KzkTMGxPK5oUIuSPXneDQxL3ZiDxfgG+VH6F0XbndVqG8gH8hidE9XRtTQhNBUHpDX1 2hJNVUkznm+KEbt9XMzFXqA0O+MizFJbPv4FqOeSGZ/dO/gbq2V/sZUOoAuiFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791305945; 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=IuhZhfFfT/v2WHHg4WnnbXWN6ISBuUxKKx1AAMWOc1w=; b=wAmTRDGNyXBjA9giJB78ZynQ5VZOqmy5PZA+3za1Pu5o12lB5EpGzubmf3Y3pjhOZMd1kC cFuxpaLAsNFADfgv5WGnnF5TySMBUBcbZ/1wc3j5XQ40e8vBXw1CWHFXfSoF3HgF2D/ZrZ Y5mvFcVihb77oYADq9JsHwCaqgk4T8NvZ0IBtorHYbGZMhk7cV9HYGeZRcuwYLH0zw4651 I0KqyVwpPoLvmZK7UWYN0yekWwP6Z2PYeqHnL2Ta22FqDgKgXF7ujn8cAMcB2WTLJOoXfR 2y7MbAbDrgOrPmj8K0t8L4rSNQxZWD7Ky90SYoPo530L1lSXXKyf8tf5ZJUTcg== 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 4hzjCF1Tl3zF3v for ; Tue, 06 Oct 2026 16:59:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 33d55 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 06 Oct 2026 16:59:05 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Roman Bogorodskiy Subject: git: 4445d039583a - main - stand: userboot: improve userdisk error handling List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org List-Id: 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/main X-Git-Reftype: branch X-Git-Commit: 4445d039583a0a47fc335e0d34401581bcbc104e Auto-Submitted: auto-generated Date: Tue, 06 Oct 2026 16:59:05 +0000 Message-Id: <6ac528d9.33d55.1756a0e@gitrepo.freebsd.org> The branch main has been updated by novel: URL: https://cgit.FreeBSD.org/src/commit/?id=4445d039583a0a47fc335e0d34401581bcbc104e commit 4445d039583a0a47fc335e0d34401581bcbc104e Author: Roman Bogorodskiy AuthorDate: 2026-08-28 16:43:26 +0000 Commit: Roman Bogorodskiy CommitDate: 2026-10-06 16:51:40 +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 --- 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 d1f231a12e97..985933bf51e9 100644 --- a/stand/userboot/userboot/main.c +++ b/stand/userboot/userboot/main.c @@ -237,9 +237,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 */ @@ -250,10 +254,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)