man-pages/man2/chroot.2.html
2021-03-31 01:06:50 +01:00

265 lines
6.0 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of CHROOT</TITLE>
</HEAD><BODY>
<H1>CHROOT</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
chroot - change root directory
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<P>
<B>int chroot(const char *</B><I>path</I><B>);</B>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>chroot</B>():
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.2.2:<DD>
<PRE>
_XOPEN_SOURCE &amp;&amp; ! (_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L)
|| /* Since glibc 2.20: */ _DEFAULT_SOURCE
|| /* Glibc versions &lt;= 2.19: */ _BSD_SOURCE
<DT id="3"></PRE>
<DD>
Before glibc 2.2.2: none
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>chroot</B>()
changes the root directory of the calling process to that specified in
<I>path</I>.
This directory will be used for pathnames beginning with <I>/</I>.
The root directory is inherited by all children of the calling process.
<P>
Only a privileged process (Linux: one with the
<B>CAP_SYS_CHROOT</B>
capability in its user namespace) may call
<B>chroot</B>().
<P>
This call changes an ingredient in the pathname resolution process
and does nothing else.
In particular, it is not intended to be used
for any kind of security purpose, neither to fully sandbox a process nor
to restrict filesystem system calls.
In the past,
<B>chroot</B>()
has been used by daemons to restrict themselves prior to passing paths
supplied by untrusted users to system calls such as
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2).
However, if a folder is moved out of the chroot directory, an attacker
can exploit that to get out of the chroot directory as well.
The easiest way to do that is to
<B><A HREF="/cgi-bin/man/man2html?2+chdir">chdir</A></B>(2)
to the to-be-moved directory, wait for it to be moved out, then open a
path like ../../../etc/passwd.
<P>
A slightly
trickier variation also works under some circumstances if
<B><A HREF="/cgi-bin/man/man2html?2+chdir">chdir</A></B>(2)
is not permitted.
If a daemon allows a &quot;chroot directory&quot; to be specified,
that usually means that if you want to prevent remote users from accessing
files outside the chroot directory, you must ensure that folders are never
moved out of it.
<P>
This call does not change the current working directory,
so that after the call '<I>.</I>' can
be outside the tree rooted at '<I>/</I>'.
In particular, the superuser can escape from a &quot;chroot jail&quot;
by doing:
<P>
mkdir foo; chroot foo; cd ..
<P>
This call does not close open file descriptors, and such file
descriptors may allow access to files outside the chroot tree.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
Depending on the filesystem, other errors can be returned.
The more general errors are listed below:
<DL COMPACT>
<DT id="4"><B>EACCES</B>
<DD>
Search permission is denied on a component of the path prefix.
(See also
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7).)
<DT id="5"><B>EFAULT</B>
<DD>
<I>path</I>
points outside your accessible address space.
<DT id="6"><B>EIO</B>
<DD>
An I/O error occurred.
<DT id="7"><B>ELOOP</B>
<DD>
Too many symbolic links were encountered in resolving
<I>path</I>.
<DT id="8"><B>ENAMETOOLONG</B>
<DD>
<I>path</I>
is too long.
<DT id="9"><B>ENOENT</B>
<DD>
The file does not exist.
<DT id="10"><B>ENOMEM</B>
<DD>
Insufficient kernel memory was available.
<DT id="11"><B>ENOTDIR</B>
<DD>
A component of
<I>path</I>
is not a directory.
<DT id="12"><B>EPERM</B>
<DD>
The caller has insufficient privilege.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
SVr4, 4.4BSD, SUSv2 (marked LEGACY).
This function is not part of POSIX.1-2001.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
A child process created via
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
inherits its parent's root directory.
The root directory is left unchanged by
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
<P>
The magic symbolic link,
<I>/proc/[pid]/root</I>,
can be used to discover a process's root directory; see
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5)
for details.
<P>
FreeBSD has a stronger
<B>jail</B>()
system call.
<A NAME="lbAI">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+chroot">chroot</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+chdir">chdir</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+pivot_root">pivot_root</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?8+switch_root">switch_root</A></B>(8)
<A NAME="lbAJ">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="13"><A HREF="#lbAB">NAME</A><DD>
<DT id="14"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="15"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="16"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="17"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="18"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="19"><A HREF="#lbAH">NOTES</A><DD>
<DT id="20"><A HREF="#lbAI">SEE ALSO</A><DD>
<DT id="21"><A HREF="#lbAJ">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>