From nobody Fri Nov 20 15:10:58 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 4jWJgk74DBz71ySr for ; Fri, 20 Nov 2026 15:10:58 +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 4jWJgk3x82z4jRL for ; Fri, 20 Nov 2026 15:10:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795187458; 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=8epGiibiIRTpZ5V78nvdBHc4+Y4cs3W1U6j1FbkrqBE=; b=QLfOw3pjIX1AwrGNKh+G8TSwIquFKf4drIqwKnFKRAwbaWE0vRYtc/WK2RLXuQrUx3khYI wNWgDXhJKNjYvpzLD0jPff+h5EDY0EupjrGpyGaT8fBM/j8u36dau8dfV1wZunuIozwev9 TI4/4yKSJ4oAznnnrBgc74palV5qj+lcskZXv0olOGB1cRYJIf8YYKyCrVcLaaEjGhvNm5 LUJ2iBqwAgd/773SAxtCuzBR2QDsJFqTiG0hI1Qj2KwD9XRklKvoJ/KmfsFXJc1N70/1Y7 w+MQOBrhSxMmnDAIcglR1ZuqFSpoJNFjSX/Xsl4dwq7h8oS92XbVeF8T4hRrpA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1795187458; b=xfH33ci4P1AjMpEZxCPTfY1esBXAXMXKgLKwr4EogNA68Wd24Gg88CextfbiHWPqHrSU+g I9WtzQGrpiHHBJP1ucP62Ko07KEHoV+GimA5eFaWViFwccLpcTJbHXLP2iC4buXwKYt/Xf 0z49UB3U/KRLDex1FApVpqw8HE1bzOBD19czogQxUys1ewhfnWpksMnhnstcqrjl/eipM3 qDKJyI9RHzxZTVJrp8mqNBrmRILaNTlxKqj2xdQMygZ0DfpoeXcYmY34k2Rhr/xsnrENel n/8+6D9E/CsNKr7D8SXe1G1CloLLgeqg7huthlKH0ApxMJtZkhn9Dv7RVBH+AA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795187458; 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=8epGiibiIRTpZ5V78nvdBHc4+Y4cs3W1U6j1FbkrqBE=; b=Lu67IEi7yyv2jua3TOGo1HMGK435Jy+e+QK9FuKniDxwDyMTeB9tImSP6mxsRaQly7CVI7 BLkk2SGv4kUCplMYJ6BVTtUDVS1gkmOih5P4JxAs4OsffHqqUT5RVLpvTl0CcbDyAFUppI vAFJxQIzoxrisYMwMYV4TCahnNykM1FC5r9nVQfm/GV+kLXefQpnldZMnd3xpeJllahg1A FzKaIPuNUOW8/PMlEFUAzW9kKCs2jD07SMpN0DqqWe1Uu0bL3fgCZtEAMqXQv7xAGNoP7m JUi9NIy0gli+FcwMTpr/eMtWRhAOk/vq9ALKwHJzGhnGrMU0Aq2KJLOz7ePNZg== 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 4jWJgk1d8wztPt for ; Fri, 20 Nov 2026 15:10:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 18484 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 20 Nov 2026 15:10:58 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 138bce717ee2 - main - chroot(8): add altroot mode List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main X-BeenThere: dev-commits-src-main@freebsd.org List-Id: Commit messages for the main branch of the 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: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 138bce717ee2dee09a342b8c19512ca0ac6dea59 Auto-Submitted: auto-generated Date: Fri, 20 Nov 2026 15:10:58 +0000 Message-Id: <6b006302.18484.444d753b@gitrepo.freebsd.org> The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=138bce717ee2dee09a342b8c19512ca0ac6dea59 commit 138bce717ee2dee09a342b8c19512ca0ac6dea59 Author: Konstantin Belousov AuthorDate: 2026-09-27 23:46:36 +0000 Commit: Konstantin Belousov CommitDate: 2026-11-20 15:10:27 +0000 chroot(8): add altroot mode Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D60035 --- usr.sbin/chroot/chroot.8 | 25 ++++++++++++++++++++++++ usr.sbin/chroot/chroot.c | 50 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/usr.sbin/chroot/chroot.8 b/usr.sbin/chroot/chroot.8 index 58d5ec8f1a5b..4664c7fe05d2 100644 --- a/usr.sbin/chroot/chroot.8 +++ b/usr.sbin/chroot/chroot.8 @@ -37,8 +37,16 @@ .Op Fl g Ar group .Op Fl u Ar user .Op Fl n +.Op Fl a .Ar newroot .Op Ar command Op Ar arg ... +.Nm +.Op Fl G Ar group Ns Op Cm \&, Ns Ar group ... +.Op Fl g Ar group +.Op Fl u Ar user +.Op Fl n +.Fl A +.Op Ar command Op Ar arg ... .Sh DESCRIPTION The .Nm @@ -48,6 +56,15 @@ and then exec's .Ar command with provided arguments, if supplied, or an interactive copy of the user's login shell. +With the +.Fl a +or +.Fl A +options, +.Nm +instead changes or disables the alternative root, see +.Xr chaltroot 2 , +and does not change the current directory. .Pp The options are as follows: .Bl -tag -width "-G group[,group ...]" @@ -71,6 +88,14 @@ If .Va security.bsd.unprivileged_chroot sysctl is set to 1, it will make it possible to chroot without superuser privileges. +.It Fl a +Change the alternative root instead of root, see +.Xr chaltroot 2 . +.It Fl A +Disable the alternative root. +The +.Ar newroot +argument must not be provided. .El .Sh ENVIRONMENT The following environment variable is referenced by diff --git a/usr.sbin/chroot/chroot.c b/usr.sbin/chroot/chroot.c index e1af0a4131d3..9931a7239788 100644 --- a/usr.sbin/chroot/chroot.c +++ b/usr.sbin/chroot/chroot.c @@ -99,7 +99,7 @@ main(int argc, char *argv[]) gid_t gid, *gidlist; uid_t uid; int arg, ch, error, gids; - bool nonprivileged; + bool altroot, clear_altroot, nonprivileged; gid = 0; uid = 0; @@ -107,7 +107,8 @@ main(int argc, char *argv[]) user = group = grouplist = NULL; gidlist = NULL; nonprivileged = false; - while ((ch = getopt(argc, argv, "G:g:u:n")) != -1) { + altroot = clear_altroot = false; + while ((ch = getopt(argc, argv, "AaG:g:u:n")) != -1) { switch(ch) { case 'u': user = optarg; @@ -132,6 +133,12 @@ main(int argc, char *argv[]) case 'n': nonprivileged = true; break; + case 'a': + altroot = true; + break; + case 'A': + clear_altroot = true; + break; case '?': default: usage(); @@ -140,7 +147,9 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - if (argc < 1) + if (!clear_altroot && argc < 1) + usage(); + if (clear_altroot && altroot) usage(); if (group != NULL) @@ -170,13 +179,24 @@ main(int argc, char *argv[]) if (error != 0) err(1, "procctl"); } - - if (chdir(argv[0]) == -1) - err(1, "%s", argv[0]); - if (chroot(".") == -1) { - if (errno == EPERM && !nonprivileged && geteuid() != 0) - errx(1, "unprivileged use requires -n"); - err(1, "%s", argv[0]); + if (clear_altroot) { + if (chaltroot(NULL) == -1) + err(1, "clearing altroot"); + } else { + if (altroot) { + error = chaltroot(argv[0]); + } else { + if (chdir(argv[0]) == -1) + err(1, "%s", argv[0]); + error = chroot("."); + } + if (error == -1) { + if (errno == EPERM && !nonprivileged && geteuid() != 0) + errx(1, "unprivileged use requires -n"); + err(1, "%s", argv[0]); + } + argc--; + argv++; } if (gidlist != NULL && setgroups(gids, gidlist) == -1) @@ -186,9 +206,9 @@ main(int argc, char *argv[]) if (user && setuid(uid) == -1) err(1, "setuid"); - if (argv[1]) { - execvp(argv[1], &argv[1]); - err(1, "%s", argv[1]); + if (argc > 0) { + execvp(argv[0], argv); + err(1, "%s", argv[0]); } if (!(shell = getenv("SHELL"))) @@ -202,6 +222,8 @@ static void usage(void) { (void)fprintf(stderr, "usage: chroot [-g group] [-G group,group,...] " - "[-u user] [-n] newroot [command]\n"); + "[-u user] [-an] newroot [command]\n"); + (void)fprintf(stderr, " chroot [-g group] [-G group,group,...] " + "[-u user] -A [command]\n"); exit(1); }