chroot

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

chroot - ルートディレクトリを変更する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <unistd.h>

int chroot(const char *path);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

chroot():

    Since glibc 2.2.2:
        _XOPEN_SOURCE && ! (_POSIX_C_SOURCE >= 200112L)
            || /* Since glibc 2.20: */ _DEFAULT_SOURCE
            || /* glibc <= 2.19: */ _BSD_SOURCE
    Before glibc 2.2.2:
        none
 

説明

chroot() は、呼び出し元プロセスのルートディレクトリを path で指定されたディレクトリに変更する。 このディレクトリ以下が / から始まるパス名として使われる。 このルートディレクトリは呼び出し元のプロセスの全ての子プロセスに受け継がれる。

特権プロセス (Linux では、そのプロセスのユーザー名前空間で CAP_SYS_CHROOT ケーパビリティを持つプロセス) のみが chroot() を呼び出すことができる。

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, chroot() has been used by daemons to restrict themselves prior to passing paths supplied by untrusted users to system calls such as open(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 chdir(2) to the to-be-moved directory, wait for it to be moved out, then open a path like ../../../etc/passwd.

A slightly trickier variation also works under some circumstances if chdir(2) is not permitted. If a daemon allows a "chroot directory" 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.

This call does not change the current working directory, so that after the call '.' can be outside the tree rooted at '/'. In particular, the superuser can escape from a "chroot jail" by doing:

mkdir foo;
chroot foo;
cd ..;

このコールはオープンファイルディスクリプターをクローズしないので、 このようなファイルディスクリプターは chroot ツリーの外にある ファイルにアクセスできる。  

返り値

成功すると、0 を返す。 エラーの場合、-1 を返し、 errno にエラーを示す値を設定する。  

エラー

ファイルシステムによっては他のエラーが返される事がある。 一般的なエラーを以下に挙げる:
EACCES
パス名の構成要素に検索許可がない (path_resolution(7) も見よ)。
EFAULT
path がアクセスできるアドレス空間外を指している。
EIO
I/O エラーが発生した。
ELOOP
path を解決する際に遭遇したシンボリックリンクが多過ぎる。
ENAMETOOLONG
path が長過ぎる。
ENOENT
ファイルが存在しない。
ENOMEM
カーネルに十分なメモリーがない。
ENOTDIR
path の構成要素がディレクトリではない。
EPERM
呼び出し側に十分な特権がない。
 

標準規格

なし。  

歴史

SVr4, 4.4BSD, SUSv1 (marked TO BE WITHDRAWN), SUSv2 (marked LEGACY). This function is not part of POSIX.1-2001.  

注意

fork(2) で作成された子プロセスは、 親プロセスのルートディレクトリを継承する。 execve(2) の場合も、ルートディレクトリは変更されない。

The magic symbolic link, /proc/pid/root, can be used to discover a process's root directory; see proc(5) for details.

FreeBSD にはより強力な jail() システムコールがある。  

関連項目

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


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
標準規格
歴史
注意
関連項目

This document was created by man2html, using the manual pages.