git: 138bce717ee2 - main - chroot(8): add altroot mode
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 20 Nov 2026 15:10:58 UTC
The branch main has been updated by kib:
URL: https://cgit.FreeBSD.org/src/commit/?id=138bce717ee2dee09a342b8c19512ca0ac6dea59
commit 138bce717ee2dee09a342b8c19512ca0ac6dea59
Author: Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-09-27 23:46:36 +0000
Commit: Konstantin Belousov <kib@FreeBSD.org>
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);
}