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 with EPERM if there are any directories open. πŸ’€
  • If β€˜kern.chroot_allow_open_directories’ is set to one (the default 🌟), chroot() will fail with EPERM if there are any directories open and the process is already subject to the chroot() 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 exceeded 255 characters, or an entire path name exceeded 1023 characters. 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] β€” The dirname argument 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&amp;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