]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/chroot.2
c2923cb2bb731c15fa95a8f6563d90a7533ff48b
[thirdparty/man-pages.git] / man2 / chroot.2
1 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified by Michael Haardt <michael@moria.de>
26 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified 1994-08-21 by Michael Chastain <mec@shell.portal.com>
28 .\" Modified 1996-06-13 by aeb
29 .\" Modified 1996-11-06 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 1997-08-21 by Joseph S. Myers <jsm28@cam.ac.uk>
31 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
32 .\"
33 .TH CHROOT 2 2016-07-17 "Linux" "Linux Programmer's Manual"
34 .SH NAME
35 chroot \- change root directory
36 .SH SYNOPSIS
37 .B #include <unistd.h>
38 .sp
39 .BI "int chroot(const char *" path );
40 .sp
41 .in -4n
42 Feature Test Macro Requirements for glibc (see
43 .BR feature_test_macros (7)):
44 .in
45 .sp
46 .BR chroot ():
47 .ad l
48 .RS 4
49 .PD 0
50 .TP 4
51 Since glibc 2.2.2:
52 .nf
53 _XOPEN_SOURCE && ! (_POSIX_C_SOURCE\ >=\ 200112L)
54 || /* Since glibc 2.20: */ _DEFAULT_SOURCE
55 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
56 .TP 4
57 .fi
58 Before glibc 2.2.2: none
59 .PD
60 .RE
61 .ad b
62 .SH DESCRIPTION
63 .BR chroot ()
64 changes the root directory of the calling process to that specified in
65 .IR path .
66 This directory will be used for pathnames beginning with \fI/\fP.
67 The root directory is inherited by all children of the calling process.
68
69 Only a privileged process (Linux: one with the
70 .B CAP_SYS_CHROOT
71 capability in its user namespace) may call
72 .BR chroot ().
73
74 This call changes an ingredient in the pathname resolution process
75 and does nothing else.
76 In particular, it is not intended to be used
77 for any kind of security purpose, neither to fully sandbox a process nor
78 to restrict filesystem system calls.
79 In the past,
80 .BR chroot ()
81 has been used by daemons to restrict themselves prior to passing paths
82 supplied by untrusted users to system calls such as
83 .BR open (2).
84 However, if a folder is moved out of the chroot directory, an attacker
85 can exploit that to get out of the chroot directory as well.
86 The easiest way to do that is to
87 .BR chdir (2)
88 to the to-be-moved directory, wait for it to be moved out, then open a
89 path like ../../../etc/passwd.
90
91 .\" This is how the "slightly trickier variation" works:
92 .\" https://github.com/QubesOS/qubes-secpack/blob/master/QSBs/qsb-014-2015.txt#L142
93 A slightly
94 trickier variation also works under some circumstances if
95 .BR chdir (2)
96 is not permitted.
97 If a daemon allows a "chroot directory" to be specified,
98 that usually means that if you want to prevent remote users from accessing
99 files outside the chroot directory, you must ensure that folders are never
100 moved out of it.
101
102 This call does not change the current working directory,
103 so that after the call \(aq\fI.\fP\(aq can
104 be outside the tree rooted at \(aq\fI/\fP\(aq.
105 In particular, the superuser can escape from a "chroot jail"
106 by doing:
107 .nf
108
109 mkdir foo; chroot foo; cd ..
110 .fi
111
112 This call does not close open file descriptors, and such file
113 descriptors may allow access to files outside the chroot tree.
114 .SH RETURN VALUE
115 On success, zero is returned.
116 On error, \-1 is returned, and
117 .I errno
118 is set appropriately.
119 .SH ERRORS
120 Depending on the filesystem, other errors can be returned.
121 The more general errors are listed below:
122 .TP
123 .B EACCES
124 Search permission is denied on a component of the path prefix.
125 (See also
126 .BR path_resolution (7).)
127 .\" Also search permission is required on the final component,
128 .\" maybe just to guarantee that it is a directory?
129 .TP
130 .B EFAULT
131 .I path
132 points outside your accessible address space.
133 .TP
134 .B EIO
135 An I/O error occurred.
136 .TP
137 .B ELOOP
138 Too many symbolic links were encountered in resolving
139 .IR path .
140 .TP
141 .B ENAMETOOLONG
142 .I path
143 is too long.
144 .TP
145 .B ENOENT
146 The file does not exist.
147 .TP
148 .B ENOMEM
149 Insufficient kernel memory was available.
150 .TP
151 .B ENOTDIR
152 A component of
153 .I path
154 is not a directory.
155 .TP
156 .B EPERM
157 The caller has insufficient privilege.
158 .SH CONFORMING TO
159 SVr4, 4.4BSD, SUSv2 (marked LEGACY).
160 This function is not part of POSIX.1-2001.
161 .\" SVr4 documents additional EINTR, ENOLINK and EMULTIHOP error conditions.
162 .\" X/OPEN does not document EIO, ENOMEM or EFAULT error conditions.
163 .SH NOTES
164 A child process created via
165 .BR fork (2)
166 inherits its parent's root directory.
167 The root directory is left unchanged by
168 .BR execve (2).
169
170 FreeBSD has a stronger
171 .BR jail ()
172 system call.
173 .\" FIXME . eventually say something about containers,
174 .\" virtual servers, etc.?
175 .SH SEE ALSO
176 .BR chdir (2),
177 .BR pivot_root (2),
178 .BR path_resolution (7)