sourCEntral - mobile manpages

pdf

PATH_RESOLUTION

名前

path_resolution - ファイルのパス名の解決方法

説明

いくつかの UNIX/Linux システムコールは、 1 つ以上のファイル名を引数として持つ。 ファイル名 (またはパス名) は以下のようにして解決される。

ステップ 1: 解決過程を開始する
If the pathname starts with the '/' character, the starting lookup directory is the root directory of the calling process. A process inherits its root directory from its parent. Usually this will be the root directory of the file hierarchy. A process may get a different root directory by use of the chroot(2) system call, or may temporarily use a different root directory by using openat2(2) with the RESOLVE_IN_ROOT flag set.

A process may get an entirely private mount namespace in case it—or one of its ancestors—was started by an invocation of the clone(2) system call that had the CLONE_NEWNS flag set. This handles the '/' part of the pathname.

If the pathname does not start with the '/' character, the starting lookup directory of the resolution process is the current working directory of the process — or in the case of openat(2)-style system calls, the dfd argument (or the current working directory if AT_FDCWD is passed as the dfd argument). The current working directory is inherited from the parent, and can be changed by use of the chdir(2) system call.)

'/' 文字で始まるパス名は絶対パス名と呼ばれ、 '/' 文字で始まらないパス名は相対パス名と呼ばれる。

ステップ 2: パスを辿る
現在の検索ディレクトリをディレクトリ検索の開始点とする。 そして、パス名の最後の構成要素 (component) でない各構成要素について、 現在の検索ディレクトリで検索を行う。 ここで構成要素は '/' で区切られた部分文字列である。

プロセスが現在の検索ディレクトリの検索許可を持たない場合、 EACCES エラーが返される ("Permission denied")。

構成要素が見つからない場合、 ENOENT エラーが返される ("No such file or directory")。

構成要素は見つかったが、ディレクトリでもシンボリックリンクでもない場合、 ENOTDIR エラーが返される ("Not a directory")。

構成要素が見つかって、かつディレクトリである場合、 現在の検索ディレクトリをそのディレクトリに設定し、 次の構成要素に移動する。

If the component is found and is a symbolic link (symlink), we first resolve this symbolic link (with the current lookup directory as starting lookup directory). Upon error, that error is returned. If the result is not a directory, an ENOTDIR error is returned. If the resolution of the symbolic link is successful and returns a directory, we set the current lookup directory to that directory, and go to the next component. Note that the resolution process here can involve recursion if the prefix (’dirname’) component of a pathname contains a filename that is a symbolic link that resolves to a directory (where the prefix component of that directory may contain a symbolic link, and so on). In order to protect the kernel against stack overflow, and also to protect against denial of service, there are limits on the maximum recursion depth, and on the maximum number of symbolic links followed. An ELOOP error is returned when the maximum is exceeded ("Too many levels of symbolic links").

As currently implemented on Linux, the maximum number of symbolic links that will be followed while resolving a pathname is 40. In kernels before 2.6.18, the limit on the recursion depth was 5. Starting with Linux 2.6.18, this limit was raised to 8. In Linux 4.2, the kernel’s pathname-resolution code was reworked to eliminate the use of recursion, so that the only limit that remains is the maximum of 40 resolutions for the entire pathname.

The resolution of symbolic links during this stage can be blocked by using openat2(2), with the RESOLVE_NO_SYMLINKS flag set.

ステップ 3: 最後のエントリーを見つける
パス名の最後の構成要素の検索は、前のステップで説明した 他の全ての構成要素と同じように実行されるが、2 つの違いがある。 (i) 最後の構成要素はディレクトリである必要がない (パス解決過程に関する限りはどちらでも構わない — 特定のシステムコールが要求するものによって、 ディレクトリでなければならない場合もあるし、 ディレクトリ以外でなければならない場合もある)。 (ii) 構成要素が見つからない場合にエラーにする必要はない — その構成要素を作成するだけでよい場合もある。 最後のエントリーの詳細な扱いは、 特定のシステムコールの man ページで説明されている。

. と ..
慣習として、全てのディレクトリはエントリー "." と ".." を持つ。 これらはそれぞれ、そのディレクトリ自身とその親ディレクトリを参照する。

パス解決過程では、これらのエントリーが物理的なファイルシステムに 実際に存在するか否かに関わらず、慣習的な意味を持つと仮定する。

ルートより上に辿ることはできない: "/.." は "/" と同じである。

マウント位置
"mount dev path" コマンドを実行した後、 パス名 "path" はデバイス "dev" 上のファイルシステム階層の ルートディレクトリを参照するようになり、以前の位置を参照しない。

マウントされたファイルシステムの外に出ることができる: "path/.." は "dev" 上のファイルシステム階層の外である "path" の親ディレクトリを参照する。

Traversal of mount points can be blocked by using openat2(2), with the RESOLVE_NO_XDEV flag set (though note that this also restricts bind mount traversal).

末尾のスラッシュ
パス名が '/' で終わっている場合、 ステップ 2 において、その前にある構成要素の解決法を次のように強制する: その構成要素が存在しなければならず、ディレクトリとして解決される。 存在しない場合は、末尾の '/' が無視される。 (また同様に、末尾に '/' があるパス名は、 '.' を末尾に加えて得られるパス名と等しい。)

最後がシンボリックリンクのとき
パス名の最後の構成要素がシンボリックリンクである場合、 参照されるファイルをシンボリックリンクとするか、 その内容についてパスを解決した結果とするかは、 システムコールに依存する。 たとえば、システムコール lstat(2) はシンボリックリンクに作用する。 一方、 stat(2) はシンボリックリンクで指されたファイルに作用する。

長さの制限
パス名には最大長がある。 パス名 (またはシンボリックリンクを解決するときに得られる中間パス名) が 長すぎる場合、 ENAMETOOLONG エラーが返される ("Filename too long")。

空のパス名
元々の UNIX では、空のパス名は現在のディレクトリを参照していた。 最近、POSIX では空のパス名を解決するべきではないという決定がなされた。 この場合、Linux は ENOENT を返す。

許可
The permission bits of a file consist of three groups of three bits; see chmod(1) and stat(2). The first group of three is used when the effective user ID of the calling process equals the owner ID of the file. The second group of three is used when the group ID of the file either equals the effective group ID of the calling process, or is one of the supplementary group IDs of the calling process (as set by setgroups(2)). When neither holds, the third group is used.

3 ビットが使われる場合、最初のビットは読み込み許可を決定し、 2 番目のビットは書き込み許可を決定する。 また 3 番目のビットは、通常のファイルの場合は実行許可を表し、 ディレクトリの場合は検索許可を表す。

Linux は、許可のチェックにおいて、実効ユーザー ID ではなく fsuid を使う。 通常は fsuid は実効ユーザー ID と等しいが、fsuid はシステムコール setfsuid(2) で変更することができる。

(ここで "fsuid" は "file system user ID" を表している。 この概念は「プロセスが同じ実効ユーザー ID を持つプロセスに 同時にシグナルを送ることができる」というユーザー空間 NFS サーバを 実装する際に必要であった。 これは今では廃れてしまった。 setfsuid(2) を使うべきではない。

同様に、Linux では実効グループ ID の代わりに fsgid ("ファイルシステムグループID") を使う。 setfsgid(2) を参照すること。

許可の確認をスキップする: スーパーユーザーとケーパビリティ
伝統的な UNIX システムでは、スーパーユーザー (root, ユーザー ID 0) は非常に強力であり、ファイルアクセス時の 許可による制限を全てスキップする。

Linux では、スーパーユーザー権限が複数のケーパビリティに分割されている (capabilities(7) 参照)。ファイルの許可の確認には、 CAP_DAC_OVERRIDECAP_DAC_READ_SEARCH の 2つのケーパビリティが関係する (プロセスの fsuid が 0 の場合、そのプロセスはこれらのケーパビリティを持つ)。

CAP_DAC_OVERRIDE ケーパビリティは全ての許可チェックを上書きする。 実際には、対象となるファイルの 3 つの実行許可ビットのうちの 少なくとも 1 つが設定されている場合のみ、実行を許可する。

CAP_DAC_READ_SEARCH ケーパビリティは、ディレクトリに対して読み込みと検索を許可し、 通常のファイルに対して読み込みを許可する。

関連項目

readlink(2), capabilities(7), credentials(7), symlink(7)

この文書について

この man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。

pdf