1.\" $OpenBSD: chroot.2,v 1.18 2013/07/17 05:42:11 schwarze Exp $ 2.\" $NetBSD: chroot.2,v 1.7 1995/02/27 12:32:12 cgd Exp $ 3.\" 4.\" Copyright (c) 1983, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)chroot.2 8.1 (Berkeley) 6/4/93 32.\" 33.Dd $Mdocdate: July 17 2013 $ 34.Dt CHROOT 2 35.Os 36.Sh NAME 37.Nm chroot 38.Nd change root directory 39.Sh SYNOPSIS 40.Fd #include <unistd.h> 41.Ft int 42.Fn chroot "const char *dirname" 43.Sh DESCRIPTION 44.Fa dirname 45is the address of the pathname of a directory, terminated by an 46.Tn ASCII 47NUL. 48.Fn chroot 49causes 50.Fa dirname 51to become the root directory, that is, the starting point for path 52searches of pathnames beginning with 53.Ql / . 54.Pp 55In order for a directory to become the root directory 56a process must have execute (search) access for that directory. 57.Pp 58If the program is not currently running with an altered root directory, 59it should be noted that 60.Fn chroot 61has no effect on the process's current directory. 62.Pp 63If the program is already running with an altered root directory, the 64process's current directory is changed to the same new root directory. 65This prevents the current directory from being further up the directory 66tree than the altered root directory. 67.Pp 68This call is restricted to the superuser. 69.Sh RETURN VALUES 70Upon successful completion, a value of 0 is returned. 71Otherwise, a value of \-1 is returned and 72.Va errno 73is set to indicate an error. 74.Sh EXAMPLES 75The following example changes the root directory to 76.Va newroot , 77sets the current directory to the new root, and drops some 78setuid privileges. 79There may be other privileges which need to be dropped as well. 80.Bd -literal -offset indent 81#include <err.h> 82#include <unistd.h> 83 84if (chroot(newroot) != 0 || chdir("/") != 0) 85 err(1, "%s", newroot); 86setresuid(getuid(), getuid(), getuid()); 87.Ed 88.Sh ERRORS 89.Fn chroot 90will fail and the root directory will be unchanged if: 91.Bl -tag -width Er 92.It Bq Er ENOTDIR 93A component of the path name is not a directory. 94.It Bq Er ENAMETOOLONG 95A component of a pathname exceeded 96.Dv {NAME_MAX} 97characters, or an entire path name exceeded 98.Dv {PATH_MAX} 99characters. 100.It Bq Er ENOENT 101The named directory does not exist. 102.It Bq Er EACCES 103Search permission is denied for any component of the path name. 104.It Bq Er ELOOP 105Too many symbolic links were encountered in translating the pathname. 106.It Bq Er EFAULT 107.Fa dirname 108points outside the process's allocated address space. 109.It Bq Er EIO 110An I/O error occurred while reading from or writing to the file system. 111.It Bq Er EPERM 112The caller is not the superuser. 113.El 114.Sh SEE ALSO 115.Xr chdir 2 116.Sh HISTORY 117The 118.Fn chroot 119system call first appeared in 120.At v7 . 121.Sh CAVEATS 122There are ways for a root process to escape from the chroot jail. 123