CHROOT(2)
β¨ CHROOT(2) β¨ β change root directory π
okay so bestie, welcome to CHROOT(2), straight out of the System Calls Manual π«Ά this one is a whole vibe, it's the syscall that literally moves the whole world for your process. sit down, hydrate, let's get into it πΈ
π NAME π
chroot β change root directory π
π LIBRARY π
Standard C Library (libc, -lc) β¨
π¦ SYNOPSIS π¦
#include <unistd.h>
int
chroot(const char *dirname);
π DESCRIPTION π
okay so the tea βοΈ: the dirname argument is the address of the pathname of a directory, terminated by an ASCII NUL. classic string things, no cap.
The chroot() system call causes dirname to become the root directory, that is, the starting point for path searches of pathnames beginning with β/β. it's giving "rewrite the map of the universe" πΊοΈβ¨
In order for a directory to become the root directory a process must have execute (search) access for that directory. no access, no root, periodt.
It should be noted that chroot() has no effect on the process's current directory. π your cwd does NOT move with you, she stays exactly where she was.
This call is restricted to the super-user π β like, sorry bestie, not just anyone gets to do this.
Depending on the setting of the βkern.chroot_allow_open_directoriesβ sysctl variable, open filedescriptors which reference directories will make the chroot() fail as follows:
- If β
kern.chroot_allow_open_directoriesβ is set to zero,chroot()will always fail withEPERMif there are any directories open. π - If β
kern.chroot_allow_open_directoriesβ is set to one (the default π),chroot()will fail withEPERMif there are any directories open and the process is already subject to thechroot()system call. - Any other value for β
kern.chroot_allow_open_directoriesβ will bypass the check for open directories π§
π« RETURN VALUES π«
Upon successful completion, the value 0 is returned π₯³ otherwise the value -1 is returned and the global variable errno is set to indicate the error. she'll tell you what went wrong, bestie, you just gotta read her.
π¨ ERRORS π¨
The chroot() system call will fail and the root directory will be unchanged if:
[ENOTDIR]β A component of the path name is not a directory. π[EPERM]β The effective user ID is not the super-user, or one or more filedescriptors are open directories. π[ENAMETOOLONG]β A component of a pathname exceeded255characters, or an entire path name exceeded1023characters. girl that path is TOO long π[ENOENT]β The named directory does not exist. she's not there, bestie, she ghosted you π»[EACCES]β Search permission is denied for any component of the path name. π[ELOOP]β Too many symbolic links were encountered in translating the pathname. π symlink maze fr fr[EFAULT]β Thedirnameargument points outside the process's allocated address space. π«[EIO]β An I/O error occurred while reading from or writing to the file system. β‘οΈ[EINTEGRITY]β Corrupted data was detected while reading from the file system. π corrupted, no cap
π SEE ALSO π
π HISTORY π
The chroot() system call appeared in Version 7 AT&T UNIX π°οΈ iconic behavior honestly. It was marked as βlegacyβ in Version 2 of the Single UNIX Specification (βSUSv2β), and was removed in subsequent standards. rip bozo πͺ¦β¨
π BUGS π
okay this one is the messy part π: If the process is able to change its working directory to the target directory, but another access control check fails (such as a check for open directories, or a MAC check), it is possible that this system call may return an error, with the working directory of the process left changed. so like... she said "no" but also already moved your stuff?? π the state is not what you think it is, check it, always check it.
π SECURITY CONSIDERATIONS π
The system have many hardcoded paths to files where it may load after the process starts. π
It is generally recommended to drop privileges immediately after a successful chroot call, and restrict write access to a limited subtree of the chroot root, for instance, setup the sandbox so that the sandboxed user will have no write access to any well-known system directories. π₯ no cap, do not skip this step, this is the main character energy move that keeps you safe.
and that's the whole page bestie, you ate it π this was CHROOT(2) from the System Calls Manual, brought to you by Debian, dated June 26, 2020 π mwah, go chroot responsibly π¦β¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms