1CHROOT(2) Linux Programmer's Manual CHROOT(2)
2
3
4
6 chroot - change root directory
7
9 #include <unistd.h>
10
11 int chroot(const char *path);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 chroot():
16 Since glibc 2.2.2:
17 _XOPEN_SOURCE && ! (_POSIX_C_SOURCE >= 200112L)
18 || /* Since glibc 2.20: */ _DEFAULT_SOURCE
19 || /* Glibc <= 2.19: */ _BSD_SOURCE
20 Before glibc 2.2.2:
21 none
22
24 chroot() changes the root directory of the calling process to that
25 specified in path. This directory will be used for pathnames beginning
26 with /. The root directory is inherited by all children of the calling
27 process.
28
29 Only a privileged process (Linux: one with the CAP_SYS_CHROOT capabil‐
30 ity in its user namespace) may call chroot().
31
32 This call changes an ingredient in the pathname resolution process and
33 does nothing else. In particular, it is not intended to be used for
34 any kind of security purpose, neither to fully sandbox a process nor to
35 restrict filesystem system calls. In the past, chroot() has been used
36 by daemons to restrict themselves prior to passing paths supplied by
37 untrusted users to system calls such as open(2). However, if a folder
38 is moved out of the chroot directory, an attacker can exploit that to
39 get out of the chroot directory as well. The easiest way to do that is
40 to chdir(2) to the to-be-moved directory, wait for it to be moved out,
41 then open a path like ../../../etc/passwd.
42
43 A slightly trickier variation also works under some circumstances if
44 chdir(2) is not permitted. If a daemon allows a "chroot directory" to
45 be specified, that usually means that if you want to prevent remote
46 users from accessing files outside the chroot directory, you must en‐
47 sure that folders are never moved out of it.
48
49 This call does not change the current working directory, so that after
50 the call '.' can be outside the tree rooted at '/'. In particular, the
51 superuser can escape from a "chroot jail" by doing:
52
53 mkdir foo; chroot foo; cd ..
54
55 This call does not close open file descriptors, and such file descrip‐
56 tors may allow access to files outside the chroot tree.
57
59 On success, zero is returned. On error, -1 is returned, and errno is
60 set to indicate the error.
61
63 Depending on the filesystem, other errors can be returned. The more
64 general errors are listed below:
65
66 EACCES Search permission is denied on a component of the path prefix.
67 (See also path_resolution(7).)
68
69 EFAULT path points outside your accessible address space.
70
71 EIO An I/O error occurred.
72
73 ELOOP Too many symbolic links were encountered in resolving path.
74
75 ENAMETOOLONG
76 path is too long.
77
78 ENOENT The file does not exist.
79
80 ENOMEM Insufficient kernel memory was available.
81
82 ENOTDIR
83 A component of path is not a directory.
84
85 EPERM The caller has insufficient privilege.
86
88 SVr4, 4.4BSD, SUSv2 (marked LEGACY). This function is not part of
89 POSIX.1-2001.
90
92 A child process created via fork(2) inherits its parent's root direc‐
93 tory. The root directory is left unchanged by execve(2).
94
95 The magic symbolic link, /proc/[pid]/root, can be used to discover a
96 process's root directory; see proc(5) for details.
97
98 FreeBSD has a stronger jail() system call.
99
101 chroot(1), chdir(2), pivot_root(2), path_resolution(7), switch_root(8)
102
104 This page is part of release 5.13 of the Linux man-pages project. A
105 description of the project, information about reporting bugs, and the
106 latest version of this page, can be found at
107 https://www.kernel.org/doc/man-pages/.
108
109
110
111Linux 2021-03-22 CHROOT(2)