xref: /freebsd/lib/libsys/chroot.2 (revision b165e9e3ea4e327fc421d81c2a89242bd8720780)
18269e767SBrooks Davis.\" Copyright (c) 1983, 1991, 1993
28269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
38269e767SBrooks Davis.\"
48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
68269e767SBrooks Davis.\" are met:
78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
88269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
108269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
118269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
138269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
148269e767SBrooks Davis.\"    without specific prior written permission.
158269e767SBrooks Davis.\"
168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268269e767SBrooks Davis.\" SUCH DAMAGE.
278269e767SBrooks Davis.\"
28*b165e9e3SEdward Tomasz Napierala.Dd July 15, 2024
298269e767SBrooks Davis.Dt CHROOT 2
308269e767SBrooks Davis.Os
318269e767SBrooks Davis.Sh NAME
32*b165e9e3SEdward Tomasz Napierala.Nm chroot ,
33*b165e9e3SEdward Tomasz Napierala.Nm fchroot
348269e767SBrooks Davis.Nd change root directory
358269e767SBrooks Davis.Sh LIBRARY
368269e767SBrooks Davis.Lb libc
378269e767SBrooks Davis.Sh SYNOPSIS
388269e767SBrooks Davis.In unistd.h
398269e767SBrooks Davis.Ft int
408269e767SBrooks Davis.Fn chroot "const char *dirname"
41*b165e9e3SEdward Tomasz Napierala.Ft int
42*b165e9e3SEdward Tomasz Napierala.Fn fchroot "int fd"
438269e767SBrooks Davis.Sh DESCRIPTION
448269e767SBrooks DavisThe
458269e767SBrooks Davis.Fa dirname
468269e767SBrooks Davisargument
478269e767SBrooks Davisis the address of the pathname of a directory, terminated by an ASCII NUL.
488269e767SBrooks DavisThe
498269e767SBrooks Davis.Fn chroot
508269e767SBrooks Davissystem call causes
518269e767SBrooks Davis.Fa dirname
528269e767SBrooks Davisto become the root directory,
538269e767SBrooks Davisthat is, the starting point for path searches of pathnames
548269e767SBrooks Davisbeginning with
558269e767SBrooks Davis.Ql / .
568269e767SBrooks Davis.Pp
578269e767SBrooks DavisIn order for a directory to become the root directory
588269e767SBrooks Davisa process must have execute (search) access for that directory.
598269e767SBrooks Davis.Pp
608269e767SBrooks DavisIt should be noted that
618269e767SBrooks Davis.Fn chroot
628269e767SBrooks Davishas no effect on the process's current directory.
638269e767SBrooks Davis.Pp
648269e767SBrooks DavisThis call is restricted to the super-user.
658269e767SBrooks Davis.Pp
668269e767SBrooks DavisDepending on the setting of the
678269e767SBrooks Davis.Ql kern.chroot_allow_open_directories
688269e767SBrooks Davissysctl variable, open filedescriptors which reference directories
698269e767SBrooks Daviswill make the
708269e767SBrooks Davis.Fn chroot
718269e767SBrooks Davisfail as follows:
728269e767SBrooks Davis.Pp
738269e767SBrooks DavisIf
748269e767SBrooks Davis.Ql kern.chroot_allow_open_directories
758269e767SBrooks Davisis set to zero,
768269e767SBrooks Davis.Fn chroot
778269e767SBrooks Daviswill always fail with
788269e767SBrooks Davis.Er EPERM
798269e767SBrooks Davisif there are any directories open.
808269e767SBrooks Davis.Pp
818269e767SBrooks DavisIf
828269e767SBrooks Davis.Ql kern.chroot_allow_open_directories
838269e767SBrooks Davisis set to one (the default),
848269e767SBrooks Davis.Fn chroot
858269e767SBrooks Daviswill fail with
868269e767SBrooks Davis.Er EPERM
878269e767SBrooks Davisif there are any directories open and the
888269e767SBrooks Davisprocess is already subject to the
898269e767SBrooks Davis.Fn chroot
908269e767SBrooks Davissystem call.
918269e767SBrooks Davis.Pp
928269e767SBrooks DavisAny other value for
938269e767SBrooks Davis.Ql kern.chroot_allow_open_directories
948269e767SBrooks Daviswill bypass the check for open directories,
958269e767SBrooks Davismimicking the historic insecure behavior of
968269e767SBrooks Davis.Fn chroot
978269e767SBrooks Davisstill present on other systems.
98*b165e9e3SEdward Tomasz Napierala.Pp
99*b165e9e3SEdward Tomasz NapieralaThe
100*b165e9e3SEdward Tomasz Napierala.Fn fchroot
101*b165e9e3SEdward Tomasz Napieralasystem call is identical to
102*b165e9e3SEdward Tomasz Napierala.Fn chroot
103*b165e9e3SEdward Tomasz Napieralaexcept it takes a file descriptor instead of path.
1048269e767SBrooks Davis.Sh RETURN VALUES
1058269e767SBrooks Davis.Rv -std
1068269e767SBrooks Davis.Sh ERRORS
1078269e767SBrooks DavisThe
1088269e767SBrooks Davis.Fn chroot
1098269e767SBrooks Davissystem call
1108269e767SBrooks Daviswill fail and the root directory will be unchanged if:
1118269e767SBrooks Davis.Bl -tag -width Er
1128269e767SBrooks Davis.It Bq Er ENOTDIR
1138269e767SBrooks DavisA component of the path name is not a directory.
1148269e767SBrooks Davis.It Bq Er EPERM
1158269e767SBrooks DavisThe effective user ID is not the super-user, or one or more
1168269e767SBrooks Davisfiledescriptors are open directories.
1178269e767SBrooks Davis.It Bq Er ENAMETOOLONG
1188269e767SBrooks DavisA component of a pathname exceeded 255 characters,
1198269e767SBrooks Davisor an entire path name exceeded 1023 characters.
1208269e767SBrooks Davis.It Bq Er ENOENT
1218269e767SBrooks DavisThe named directory does not exist.
1228269e767SBrooks Davis.It Bq Er EACCES
1238269e767SBrooks DavisSearch permission is denied for any component of the path name.
1248269e767SBrooks Davis.It Bq Er ELOOP
1258269e767SBrooks DavisToo many symbolic links were encountered in translating the pathname.
1268269e767SBrooks Davis.It Bq Er EFAULT
1278269e767SBrooks DavisThe
1288269e767SBrooks Davis.Fa dirname
1298269e767SBrooks Davisargument
1308269e767SBrooks Davispoints outside the process's allocated address space.
1318269e767SBrooks Davis.It Bq Er EIO
1328269e767SBrooks DavisAn I/O error occurred while reading from or writing to the file system.
1338269e767SBrooks Davis.It Bq Er EINTEGRITY
1348269e767SBrooks DavisCorrupted data was detected while reading from the file system.
1358269e767SBrooks Davis.El
136*b165e9e3SEdward Tomasz Napierala.Pp
137*b165e9e3SEdward Tomasz NapieralaThe
138*b165e9e3SEdward Tomasz Napierala.Fn fchroot
139*b165e9e3SEdward Tomasz Napieralasystem call
140*b165e9e3SEdward Tomasz Napieralawill fail and the root directory will be unchanged if:
141*b165e9e3SEdward Tomasz Napierala.Bl -tag -width Er
142*b165e9e3SEdward Tomasz Napierala.It Bq Er EACCES
143*b165e9e3SEdward Tomasz NapieralaSearch permission is denied for the directory referenced by the
144*b165e9e3SEdward Tomasz Napieralafile descriptor.
145*b165e9e3SEdward Tomasz Napierala.It Bq Er EBADF
146*b165e9e3SEdward Tomasz NapieralaThe argument
147*b165e9e3SEdward Tomasz Napierala.Fa fd
148*b165e9e3SEdward Tomasz Napieralais not a valid file descriptor.
149*b165e9e3SEdward Tomasz Napierala.It Bq Er EIO
150*b165e9e3SEdward Tomasz NapieralaAn I/O error occurred while reading from or writing to the file system.
151*b165e9e3SEdward Tomasz Napierala.It Bq Er EINTEGRITY
152*b165e9e3SEdward Tomasz NapieralaCorrupted data was detected while reading from the file system.
153*b165e9e3SEdward Tomasz Napierala.It Bq Er ENOTDIR
154*b165e9e3SEdward Tomasz NapieralaThe file descriptor does not reference a directory.
155*b165e9e3SEdward Tomasz Napierala.It Bq Er EPERM
156*b165e9e3SEdward Tomasz NapieralaThe effective user ID is not the super-user, or one or more
157*b165e9e3SEdward Tomasz Napieralafiledescriptors are open directories.
158*b165e9e3SEdward Tomasz Napierala.El
1598269e767SBrooks Davis.Sh SEE ALSO
1608269e767SBrooks Davis.Xr chdir 2 ,
1618269e767SBrooks Davis.Xr jail 2
1628269e767SBrooks Davis.Sh HISTORY
1638269e767SBrooks DavisThe
1648269e767SBrooks Davis.Fn chroot
1658269e767SBrooks Davissystem call appeared in
1668269e767SBrooks Davis.At v7 .
1678269e767SBrooks DavisIt was marked as
1688269e767SBrooks Davis.Dq legacy
1698269e767SBrooks Davisin
1708269e767SBrooks Davis.St -susv2 ,
1718269e767SBrooks Davisand was removed in subsequent standards.
172*b165e9e3SEdward Tomasz NapieralaThe
173*b165e9e3SEdward Tomasz Napierala.Fn fchroot
174*b165e9e3SEdward Tomasz Napieralasystem call first appeared in
175*b165e9e3SEdward Tomasz Napierala.Fx 15.0 .
1768269e767SBrooks Davis.Sh BUGS
1778269e767SBrooks DavisIf the process is able to change its working directory to the target
1788269e767SBrooks Davisdirectory, but another access control check fails (such as a check for
1798269e767SBrooks Davisopen directories, or a MAC check), it is possible that this system
1808269e767SBrooks Daviscall may return an error, with the working directory of the process
1818269e767SBrooks Davisleft changed.
1828269e767SBrooks Davis.Sh SECURITY CONSIDERATIONS
1838269e767SBrooks DavisThe system has many hardcoded paths to files which it may load after
1848269e767SBrooks Davisthe process starts.
1858269e767SBrooks DavisIt is generally recommended to drop privileges immediately after a
1868269e767SBrooks Davissuccessful
1878269e767SBrooks Davis.Nm
1888269e767SBrooks Daviscall,
1898269e767SBrooks Davisand restrict write access to a limited subtree of the
1908269e767SBrooks Davis.Nm
1918269e767SBrooks Davisroot.
1928269e767SBrooks DavisFor instance,
1938269e767SBrooks Davissetup the sandbox so that the sandboxed user will have no write
1948269e767SBrooks Davisaccess to any well-known system directories.
1958269e767SBrooks Davis.Pp
1968269e767SBrooks DavisFor complete isolation from the rest of the system, use
1978269e767SBrooks Davis.Xr jail 2
1988269e767SBrooks Davisinstead.
199