1CHROOT(2)                  Linux Programmer's Manual                 CHROOT(2)
2
3
4

NAME

6       chroot - change root directory
7

SYNOPSIS

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 versions <= 2.19: */ _BSD_SOURCE
20           Before glibc 2.2.2: none
21

DESCRIPTION

23       chroot()  changes  the  root  directory  of the calling process to that
24       specified in path.  This directory will be used for pathnames beginning
25       with /.  The root directory is inherited by all children of the calling
26       process.
27
28       Only a privileged process (Linux: one with the CAP_SYS_CHROOT  capabil‐
29       ity in its user namespace) may call chroot().
30
31       This  call changes an ingredient in the pathname resolution process and
32       does nothing else.  In particular, it is not intended to  be  used  for
33       any kind of security purpose, neither to fully sandbox a process nor to
34       restrict filesystem system calls.  In the past, chroot() has been  used
35       by  daemons  to  restrict themselves prior to passing paths supplied by
36       untrusted users to system calls such as open(2).  However, if a  folder
37       is  moved  out of the chroot directory, an attacker can exploit that to
38       get out of the chroot directory as well.  The easiest way to do that is
39       to  chdir(2) to the to-be-moved directory, wait for it to be moved out,
40       then open a path like ../../../etc/passwd.
41
42       A slightly trickier variation also works under  some  circumstances  if
43       chdir(2)  is not permitted.  If a daemon allows a "chroot directory" to
44       be specified, that usually means that if you  want  to  prevent  remote
45       users  from  accessing  files  outside  the  chroot directory, you must
46       ensure that folders are never moved out of it.
47
48       This call does not change the current working directory, so that  after
49       the call '.' can be outside the tree rooted at '/'.  In particular, the
50       superuser can escape from a "chroot jail" by doing:
51
52           mkdir foo; chroot foo; cd ..
53
54       This call does not close open file descriptors, and such file  descrip‐
55       tors may allow access to files outside the chroot tree.
56

RETURN VALUE

58       On  success,  zero is returned.  On error, -1 is returned, and errno is
59       set appropriately.
60

ERRORS

62       Depending on the filesystem, other errors can be  returned.   The  more
63       general errors are listed below:
64
65       EACCES Search  permission  is denied on a component of the path prefix.
66              (See also path_resolution(7).)
67
68       EFAULT path points outside your accessible address space.
69
70       EIO    An I/O error occurred.
71
72       ELOOP  Too many symbolic links were encountered in resolving path.
73
74       ENAMETOOLONG
75              path is too long.
76
77       ENOENT The file does not exist.
78
79       ENOMEM Insufficient kernel memory was available.
80
81       ENOTDIR
82              A component of path is not a directory.
83
84       EPERM  The caller has insufficient privilege.
85

CONFORMING TO

87       SVr4, 4.4BSD, SUSv2 (marked LEGACY).  This  function  is  not  part  of
88       POSIX.1-2001.
89

NOTES

91       A  child  process created via fork(2) inherits its parent's root direc‐
92       tory.  The root directory is left unchanged by execve(2).
93
94       FreeBSD has a stronger jail() system call.
95

SEE ALSO

97       chroot(1), chdir(2), pivot_root(2), path_resolution(7), switch_root(8)
98

COLOPHON

100       This page is part of release 4.16 of the Linux  man-pages  project.   A
101       description  of  the project, information about reporting bugs, and the
102       latest    version    of    this    page,    can     be     found     at
103       https://www.kernel.org/doc/man-pages/.
104
105
106
107Linux                             2017-09-15                         CHROOT(2)
Impressum