seccomp_unotify.2: tfix
[man-pages.git] / man2 / chroot.2
blob0b6aa8a52eda91f17bfc1555bf0980ccd3b7693d
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 2021-03-22 "Linux" "Linux Programmer's Manual"
34 .SH NAME
35 chroot \- change root directory
36 .SH SYNOPSIS
37 .nf
38 .B #include <unistd.h>
39 .PP
40 .BI "int chroot(const char *" path );
41 .fi
42 .PP
43 .RS -4
44 Feature Test Macro Requirements for glibc (see
45 .BR feature_test_macros (7)):
46 .RE
47 .PP
48 .BR chroot ():
49 .nf
50     Since glibc 2.2.2:
51         _XOPEN_SOURCE && ! (_POSIX_C_SOURCE >= 200112L)
52             || /* Since glibc 2.20: */ _DEFAULT_SOURCE
53             || /* Glibc <= 2.19: */ _BSD_SOURCE
54     Before glibc 2.2.2:
55         none
56 .fi
57 .SH DESCRIPTION
58 .BR chroot ()
59 changes the root directory of the calling process to that specified in
60 .IR path .
61 This directory will be used for pathnames beginning with \fI/\fP.
62 The root directory is inherited by all children of the calling process.
63 .PP
64 Only a privileged process (Linux: one with the
65 .B CAP_SYS_CHROOT
66 capability in its user namespace) may call
67 .BR chroot ().
68 .PP
69 This call changes an ingredient in the pathname resolution process
70 and does nothing else.
71 In particular, it is not intended to be used
72 for any kind of security purpose, neither to fully sandbox a process nor
73 to restrict filesystem system calls.
74 In the past,
75 .BR chroot ()
76 has been used by daemons to restrict themselves prior to passing paths
77 supplied by untrusted users to system calls such as
78 .BR open (2).
79 However, if a folder is moved out of the chroot directory, an attacker
80 can exploit that to get out of the chroot directory as well.
81 The easiest way to do that is to
82 .BR chdir (2)
83 to the to-be-moved directory, wait for it to be moved out, then open a
84 path like ../../../etc/passwd.
85 .PP
86 .\" This is how the "slightly trickier variation" works:
87 .\" https://github.com/QubesOS/qubes-secpack/blob/master/QSBs/qsb-014-2015.txt#L142
88 A slightly
89 trickier variation also works under some circumstances if
90 .BR chdir (2)
91 is not permitted.
92 If a daemon allows a "chroot directory" to be specified,
93 that usually means that if you want to prevent remote users from accessing
94 files outside the chroot directory, you must ensure that folders are never
95 moved out of it.
96 .PP
97 This call does not change the current working directory,
98 so that after the call \(aq\fI.\fP\(aq can
99 be outside the tree rooted at \(aq\fI/\fP\(aq.
100 In particular, the superuser can escape from a "chroot jail"
101 by doing:
103 .in +4n
105 mkdir foo; chroot foo; cd ..
109 This call does not close open file descriptors, and such file
110 descriptors may allow access to files outside the chroot tree.
111 .SH RETURN VALUE
112 On success, zero is returned.
113 On error, \-1 is returned, and
114 .I errno
115 is set to indicate the error.
116 .SH ERRORS
117 Depending on the filesystem, other errors can be returned.
118 The more general errors are listed below:
120 .B EACCES
121 Search permission is denied on a component of the path prefix.
122 (See also
123 .BR path_resolution (7).)
124 .\" Also search permission is required on the final component,
125 .\" maybe just to guarantee that it is a directory?
127 .B EFAULT
128 .I path
129 points outside your accessible address space.
131 .B EIO
132 An I/O error occurred.
134 .B ELOOP
135 Too many symbolic links were encountered in resolving
136 .IR path .
138 .B ENAMETOOLONG
139 .I path
140 is too long.
142 .B ENOENT
143 The file does not exist.
145 .B ENOMEM
146 Insufficient kernel memory was available.
148 .B ENOTDIR
149 A component of
150 .I path
151 is not a directory.
153 .B EPERM
154 The caller has insufficient privilege.
155 .SH CONFORMING TO
156 SVr4, 4.4BSD, SUSv2 (marked LEGACY).
157 This function is not part of POSIX.1-2001.
158 .\" SVr4 documents additional EINTR, ENOLINK and EMULTIHOP error conditions.
159 .\" X/OPEN does not document EIO, ENOMEM or EFAULT error conditions.
160 .SH NOTES
161 A child process created via
162 .BR fork (2)
163 inherits its parent's root directory.
164 The root directory is left unchanged by
165 .BR execve (2).
167 The magic symbolic link,
168 .IR /proc/[pid]/root ,
169 can be used to discover a process's root directory; see
170 .BR proc (5)
171 for details.
173 FreeBSD has a stronger
174 .BR jail ()
175 system call.
176 .SH SEE ALSO
177 .BR chroot (1),
178 .BR chdir (2),
179 .BR pivot_root (2),
180 .BR path_resolution (7),
181 .BR switch_root (8)