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