From nobody Mon Jan 15 23:11:47 2024 X-Original-To: dev-commits-src-all@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 4TDSZX14WZz57n3T; Mon, 15 Jan 2024 23:11:48 +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 4TDSZX0g7jz4Dmk; Mon, 15 Jan 2024 23:11:48 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705360308; 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=s7VVjajieepfs6Q945tKSfVMeKx4yu7VpEp6lRmtofs=; b=IAtce+zlE8YOuXzk/1RDfHYpeSE9gZzR7JJlVxqqc7Wc+S0Qwo8Z/rHYRT1cKTBGJTQsU+ oa4X4QVDXd1erHOCGrbdVVZE94HTkzWbSp1oSEwwoUHQTDOEkVmJpgrJCI1xhF/V5rTmWQ azacyQ0FbV+DEZ4t3EbdRnI4sR0Luasgcg+HHVuIpaVP8YNdYrbIfiL7N6tPt18ETqzt2/ 7LwzlSy9VSqFh8ZmUclY3IbhQc69rcQsY0kb2xcsgrCPw2OcOiJnFA3KyP7HLLBAT6gZpw kUKeuJfrkNw98RvM+U0HdPUUCD6Wo/FAYwrwJlSXGIgMHxQvJ1D9rzvk9R1DJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705360308; 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=s7VVjajieepfs6Q945tKSfVMeKx4yu7VpEp6lRmtofs=; b=OrQk01u8vtcUiCa3BDBgfQidwSjsfVnh7GrGlJG59aEPfG4Riq3mhKpXtnryaWAR5sWzuL awzjJQzk5lDxtpquOWY2rWWf9FToeb3N0JmQwsqPuWPoJ6vMoU+VyrOPqNQCZZlfEgZ+K2 +bndTwC/0Nx97LFrJ7lkDJlb1wQ99Yit3n7uxcy/5gs5FBaEN9/wgLu96uJTVWfupCnrEC DYGCIMp1u2Vyek9lkob9BbRpu0+9VJueXrxK5+Q5t2hrkvBTznoloiHP2K2RmUR59/OBIV +OOpO8Ug4sPgJDqU4pNKtPu6LKK2GUUF+YdgEOPBUmGr8qWH2FeMgCXkWYlG4A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1705360308; a=rsa-sha256; cv=none; b=ZvxJQlTFAloFUffhYyqwzIpoIr1EbPQzwSdntVBJUwKJYmJ4ra65B9juwr6YuY1cIdj7gS ry3fjJLDeQ+6sALpMf6m3i65dlUlmye1aoeSyjeIB7Ze4TJe3xUxlGKzfEvQ9rg8O+/CMm onwzY8sja90NRp6u7fuPYZodDp+qLv5gY29gAopViwZDYdC9YBpQazZpLR7inWvSgZ0iA6 NLShCeY+3zWFj+lNytjNjj9QSE19KhCLMTQTWB63wn3CxHWOPJdRSs9mxIx+0lpx0Uglp6 gviVb/HwYv2qWI8GABJozJPqF64S0pwrzTJ9dKYwc/DyZAK2P8k2xpAaPlwIwQ== 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 4TDSZW6s0gzGGL; Mon, 15 Jan 2024 23:11:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 40FNBlev097801; Mon, 15 Jan 2024 23:11:47 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40FNBlQr097798; Mon, 15 Jan 2024 23:11:47 GMT (envelope-from git) Date: Mon, 15 Jan 2024 23:11:47 GMT Message-Id: <202401152311.40FNBlQr097798@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: 426b28fdf700 - stable/14 - bhyveload: use a dirfd to support -h List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 426b28fdf700bcd8c05b25665da34f806b27b1bb Auto-Submitted: auto-generated The branch stable/14 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=426b28fdf700bcd8c05b25665da34f806b27b1bb commit 426b28fdf700bcd8c05b25665da34f806b27b1bb Author: Kyle Evans AuthorDate: 2024-01-03 22:17:59 +0000 Commit: Kyle Evans CommitDate: 2024-01-15 22:27:59 +0000 bhyveload: use a dirfd to support -h Don't allow lookups from the loader scripts, which in rare cases may be in guest control depending on the setup, to leave the specified host root. Open the root dir and strictly do RESOLVE_BENEATH lookups from there. cb_open() has been restructured a bit to work nicely with this, using fdopendir() in the directory case and just using the fd we already opened in the regular file case. hostbase_open() was split out to provide an obvious place to apply rights(4) if that's something we care to do. Reviewed by: allanjude (earlier version), markj (cherry picked from commit 6779d44bd878e3cf4723f7386b11da6508ab5431) --- usr.sbin/bhyveload/bhyveload.c | 84 ++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 27 deletions(-) diff --git a/usr.sbin/bhyveload/bhyveload.c b/usr.sbin/bhyveload/bhyveload.c index 6b2633cac288..4c1dbd583e1f 100644 --- a/usr.sbin/bhyveload/bhyveload.c +++ b/usr.sbin/bhyveload/bhyveload.c @@ -88,11 +88,11 @@ #define NDISKS 32 -static char *host_base; static struct termios term, oldterm; static int disk_fd[NDISKS]; static int ndisks; static int consin_fd, consout_fd; +static int hostbase_fd = -1; static int need_reinit; @@ -159,42 +159,61 @@ static int cb_open(void *arg __unused, const char *filename, void **hp) { struct cb_file *cf; - char path[PATH_MAX]; + struct stat sb; + int fd, flags; - if (!host_base) + cf = NULL; + fd = -1; + flags = O_RDONLY | O_RESOLVE_BENEATH; + if (hostbase_fd == -1) return (ENOENT); - strlcpy(path, host_base, PATH_MAX); - if (path[strlen(path) - 1] == '/') - path[strlen(path) - 1] = 0; - strlcat(path, filename, PATH_MAX); - cf = malloc(sizeof(struct cb_file)); - if (stat(path, &cf->cf_stat) < 0) { - free(cf); + /* Absolute paths are relative to our hostbase, chop off leading /. */ + if (filename[0] == '/') + filename++; + + /* Lookup of /, use . instead. */ + if (filename[0] == '\0') + filename = "."; + + if (fstatat(hostbase_fd, filename, &sb, AT_RESOLVE_BENEATH) < 0) return (errno); + + if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) + return (EINVAL); + + if (S_ISDIR(sb.st_mode)) + flags |= O_DIRECTORY; + + /* May be opening the root dir */ + fd = openat(hostbase_fd, filename, flags); + if (fd < 0) + return (errno); + + cf = malloc(sizeof(struct cb_file)); + if (cf == NULL) { + close(fd); + return (ENOMEM); } + cf->cf_stat = sb; cf->cf_size = cf->cf_stat.st_size; + if (S_ISDIR(cf->cf_stat.st_mode)) { cf->cf_isdir = 1; - cf->cf_u.dir = opendir(path); - if (!cf->cf_u.dir) - goto out; - *hp = cf; - return (0); - } - if (S_ISREG(cf->cf_stat.st_mode)) { + cf->cf_u.dir = fdopendir(fd); + if (cf->cf_u.dir == NULL) { + close(fd); + free(cf); + return (ENOMEM); + } + } else { + assert(S_ISREG(cf->cf_stat.st_mode)); cf->cf_isdir = 0; - cf->cf_u.fd = open(path, O_RDONLY); - if (cf->cf_u.fd < 0) - goto out; - *hp = cf; - return (0); + cf->cf_u.fd = fd; } - -out: - free(cf); - return (EINVAL); + *hp = cf; + return (0); } static int @@ -714,6 +733,17 @@ usage(void) exit(1); } +static void +hostbase_open(const char *base) +{ + + if (hostbase_fd != -1) + close(hostbase_fd); + hostbase_fd = open(base, O_DIRECTORY | O_PATH); + if (hostbase_fd == -1) + err(EX_OSERR, "open"); +} + int main(int argc, char** argv) { @@ -748,7 +778,7 @@ main(int argc, char** argv) break; case 'h': - host_base = optarg; + hostbase_open(optarg); break; case 'l':