1.\"	$MirOS: src/lib/libc/sys/chroot.2,v 1.3 2005/09/22 20:17:49 tg Exp $
2.\"	$NetBSD: chroot.2,v 1.21 2003/08/07 16:43:56 agc Exp $
3.\"	$OpenBSD: chroot.2,v 1.13 2005/01/01 00:07:14 millert Exp $
4.\"
5.\" Copyright (c) 1983, 1991, 1993
6.\"	The Regents of the University of California.  All rights reserved.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)chroot.2	8.1 (Berkeley) 6/4/93
33.\"
34.Dd March 10, 2004
35.Dt CHROOT 2
36.Os
37.Sh NAME
38.Nm chroot
39.Nd change root directory
40.Sh SYNOPSIS
41.Fd #include <unistd.h>
42.Ft int
43.Fn chroot "const char *dirname"
44.Ft int
45.Fn fchroot "int fd"
46.Sh DESCRIPTION
47.Fa dirname
48is the address of the pathname of a directory, terminated by an
49.Tn ASCII
50NUL.
51.Fn chroot
52causes
53.Fa dirname
54to become the root directory, that is, the starting point for path
55searches of pathnames beginning with
56.Ql / .
57.Pp
58In order for a directory to become the root directory
59a process must have execute (search) access for that directory.
60.Pp
61If the program is not currently running with an altered root directory,
62it should be noted that
63.Fn chroot
64has no effect on the process's current directory.
65.Pp
66If the program is already running with an altered root directory, the
67process's current directory is changed to the same new root directory.
68This prevents the current directory from being further up the directory
69tree than the altered root directory.
70.Pp
71This call is restricted to the superuser.
72.Pp
73The
74.Fn fchroot
75function performs the same operation on an open directory file
76known by the file descriptor
77.Fa fd .
78.Sh RETURN VALUES
79Upon successful completion, a value of 0 is returned.
80Otherwise, a value of \-1 is returned and
81.Va errno
82is set to indicate an error.
83.Sh EXAMPLES
84The following example changes the root directory to
85.Va newroot ,
86sets the current directory to the new root, and drops any
87setuid privileges.
88.Bd -literal -offset indent
89#include <err.h>
90#include <unistd.h>
91
92if (chroot(newroot) != 0 || chdir("/") != 0)
93	err(1, "%s", newroot);
94seteuid(getuid());
95setuid(getuid());
96.Ed
97.Sh ERRORS
98.Fn chroot
99will fail and the root directory will be unchanged if:
100.Bl -tag -width Er
101.It Bq Er ENOTDIR
102A component of the path name is not a directory.
103.It Bq Er ENAMETOOLONG
104A component of a pathname exceeded
105.Dv {NAME_MAX}
106characters, or an entire path name exceeded
107.Dv {PATH_MAX}
108characters.
109.It Bq Er ENOENT
110The named directory does not exist.
111.It Bq Er EACCES
112Search permission is denied for any component of the path name.
113.It Bq Er ELOOP
114Too many symbolic links were encountered in translating the pathname.
115.It Bq Er EFAULT
116.Fa dirname
117points outside the process's allocated address space.
118.It Bq Er EIO
119An I/O error occurred while reading from or writing to the file system.
120.It Bq Er EPERM
121The caller is not the superuser.
122.El
123.Pp
124.Fn fchroot
125will fail and the root directory will be unchanged if:
126
127.Bl -tag -width Er
128.It Bq Er EACCES
129Search permission is denied for the directory referenced
130by the file descriptor.
131.It Bq Er EBADF
132The argument
133.Fa fd
134is not a valid file descriptor.
135.It Bq Er EIO
136An I/O error occurred while reading from or writing to the file system.
137.It Bq Er ENOTDIR
138The argument
139.Fa fd
140does not reference a directory.
141.It Bq Er EPERM
142The effective user ID of the calling process is not the super-user.
143.El
144.Sh SEE ALSO
145.Xr chdir 2
146.Sh STANDARDS
147The
148.Fn chroot
149function conforms to
150.St -xsh5 ,
151with the restriction that the calling process' working directory must be at
152or under the new root directory.
153Otherwise, the working directory is silently set to the new root directory;
154this is an extension to the standard.
155.Pp
156.Fn chroot
157was declared a legacy interface, and subsequently removed in
158.St -p1003.1-2001 .
159.Sh HISTORY
160The
161.Fn chroot
162function call appeared in
163.Bx 4.2 .
164The
165.Fn fchroot
166function appeared in
167.Nx 1.4
168and was ported to
169.Mx 8 .
170.Sh CAVEATS
171There are ways for a root process to escape from the chroot jail.
172