CAPABILITIES(7) Linux Programmer’s Manual CAPABILITIES(7) 名前 capabilities - Linux のケーパビリティ (capability) の概要 説明 権 限のチェックを行う観点から見ると、伝統的な Unix の実装ではプロセスは 二つのカテゴリに分類できる: 特権プロセス (実効ユーザID が 0 のプロセ ス 。 ユーザID 0 はスーパーユーザや root と呼ばれる) と 非特権プロセス (実 効ユーザID が 0 以外のプロセス) である。非特権プロセスでは、プロセス の 資 格情報 (通常は、実効UID 、実効GID と追加のグループリスト) に基づく権 限チェックが行われるのに対し、特権プロセスでは全てのカーネルの権限チ ェ ックがバイパスされる。 バ ージョン 2.2 以降の Linux では、これまでスーパーユーザに結び付けられ てきた権限を、いくつかのグループに分割している。これらのグループは ケー パビリティ(capability) と呼ばれ、グループ毎に独立に有効、無効を設定でき る。ケーパビリティはスレッド単位の属性である。 ケーパビリティのリスト 以下のリストは、 Linux で実装されているケーパビリティと各ケーパビリティ が許可する操作と動作をまとめたものである。 CAP_AUDIT_CONTROL (Linux 2.6.11 以降) カ ーネル監査 (audit) の有効無効の切り替え、監査のフィルタ・ルー ルの変更、監査の状況やフィルタ・ルールの取得ができる。 CAP_AUDIT_WRITE (Linux 2.6.11 以降) カーネル監査のログにレコードを書き込む。 CAP_CHOWN ファイルの UID とGID を任意に変更する (chown(2) 参照)。 CAP_DAC_OVERRIDE ファイルの読み出し、書き込み、実行の権限チェックをバイパス す る (DAC は "discretionary access control (任意のアクセス制御)" の略 である)。 CAP_DAC_READ_SEARCH ファイルの読み出し権限のチェックとディレクトリの読み出しと実行の 権限チェックをバイパスする。 CAP_FOWNER * 通常、プロセスのファイルシステム UID がファイルの UID に一致す ることが要求される操作 (例えば chmod(2), utime(2)) における 権 限 チ ェ ッ ク を バ イ パ ス する。但し、 CAP_DAC_OVERRIDE か CAP_DAC_READ_SEARCH によりチェックが行われる操作は除く。 * 任意のファイルに対して拡張ファイル属性を設定する (chattr(1) 参 照)。 * 任意のファイルに対してアクセス制御リスト (ACL) を設定する。 * ファイルの削除の際にディレクトリのスティッキービットを無視する 。 * open(2) や fcntl(2) で任意のファイルに対して O_NOATIME を指 定 する。 CAP_FSETID ファイルが変更されたときに set-user-ID とset-group-ID の許可ビッ トをクリアしない。呼び出し元プロセスのファイルシステム GID と 追 加 の GID の いずれとも GID が一致しないファイルに対して set- group-ID ビットを設定する。 CAP_IPC_LOCK メモリーのロック (mlock(2), mlockall(2), mmap(2), shmctl(2)) を 行う。 CAP_IPC_OWNER System V IPC オブジェクトに対する操作に関して権限チェックをバイ パスする。 CAP_KILL シグナルを送信する際に権限チェックをバイパスする (kill(2) 参 照) 。これには ioctl(2) の KDSIGACCEPT 操作の使用も含まれる。 CAP_LEASE (Linux 2.4 以降) 任意のファイルに対してファイルリースを設定する (fcntl(2) 参照)。 CAP_LINUX_IMMUTABLE 拡張ファイル属性 FS_APPEND_FL と FS_IMMUTABLE_FL を 設 定 す る (chattr(1) 参照)。 CAP_MAC_ADMIN (Linux 2.6.25 以降) 強 制アクセス制御 (MAC) を上書きする。 Smack Linux Security Mod- ule (LSM) 用に実装されている。 CAP_MAC_OVERRIDE (Linux 2.6.25 以降) MAC の設定や状態を変更する。 Smack LSM 用に実装されている。 CAP_MKNOD (Linux 2.4 以降) (Linux 2.4 以降) mknod(2) を使用してスペシャル・ファイルを作成す る。 CAP_NET_ADMIN 各種のネットワーク関連の操作を実行する。 (例えば、特権が必要なソ ケットオプションを設定する、マルチキャストを有効にする、インター フェースを設定する、ルーティングテーブルを変更するなど) CAP_NET_BIND_SERVICE インターネットドメインの特権ポート (ポート番号が 1024 番未満) を バインドできる。 CAP_NET_BROADCAST (未使用) ソケットのブロードキャストと、マルチキャストの待ち受 け を行う。 CAP_NET_RAW RAW ソケットと PACKET ソケットを使用する。 CAP_SETGID プ ロ セ ス の GID と追加の GID リストに対する任意の操作を行う。 Unix ドメインソケット経由でソケットの資格情報 (credential) を 渡 す際に偽の GID を渡すことができる。 CAP_SETFCAP (Linux 2.6.24 以降) ファイルケーパビリティを設定する。 CAP_SETPCAP ファイルケーパビリティがサポートされていない場合: 呼び出し元が許 可されているケーパビリティセットに含まれる任意のケーパビリティを 、他のプロセスに付与したり、削除したりできる。 (カーネルがファイ ルケーパビリティをサポートしている場合、 CAP_SETPCAP はこの役 割 を持たない。なぜなら、ファイルケーパビリティをサポートしているカ ーネルでは CAP_SETPCAP は全く別の意味を持つからである。) ファイルケーパビリティがサポートされている場合: 呼び出し元スレッ ドのバウンディングセットの任意のケーパビリティを自身の継承可能ケ ーパビリティセットに追加できる。 (prctl(2) PR_CAPBSET_DROP を 使 っ て) バ ウ ン ディングセットからケーパビリティを削除できる。 securebits フラグを変更できる。 CAP_SETUID プロセスの UID に対する任 意 の 操 作 (setuid(2), setreuid(2), setresuid(2), setfsuid(2)) を行う。 Unix ドメインソケット経由で ソケットの資格情報 (credential) を渡す際に偽の UID を渡すこと が できる。 CAP_SYS_ADMIN * 以 下のシステム管理用の操作を実行する: quotactl(2), mount(2), umount(2), swapon(2), swapoff(2), sethostname(2), setdomain- name(2). * 任 意の System V IPC オブジェクトに対する IPC_SET と IPC_RMID 操作を実行する。 * 拡張属性 trusted と security に対する操作を実行す る (attr(5) 参照)。 * lookup_dcookie(2) を呼び出す。 * ioprio_set(2) を 使 っ て I/O ス ケ ジ ュ ー リングクラス IOPRIO_CLASS_RT, IOPRIO_CLASS_IDLE を 割 り 当 て る (IOPRIO_CLASS_IDLE は Linux 2.6.25 より前のバージョンのみ)。 * ソケットの資格情報 (credential) を渡す際に偽の UID を渡す。 * フ ァ イ ル を オ ープンするシステムコール (例えば accept(2), execve(2), open(2), pipe(2)) でシステム全体でオープンできる フ ァイル数の上限 /proc/sys/fs/file-max を超過する。 * clone(2) と unshare(2) で CLONE_NEWNS フラグを利用する。 * keyctl(2) の KEYCTL_CHOWN と KEYCTL_SETPERM 操作を実行する。 CAP_SYS_BOOT reboot(2) と kexec_load(2) を呼び出す。 CAP_SYS_CHROOT chroot(2). を呼び出す。 CAP_SYS_MODULE カ ーネルモジュールのロード、アンロードを行う (init_module(2) と delete_module(2) を参照のこと)。バージョン 2.6.25 より前のカーネ ルで、システム全体のケーパビリティバウンディングセット (capabil- ity bounding set) からケーパビリティを外す。 CAP_SYS_NICE * プロセスの nice 値の引き上げ (nice(2), setpriority(2)) や、 任 意のプロセスの nice 値の変更を行う。 * 呼び出し元プロセスに対するリアルタイム・スケジューリングポリシ ーと、任意のプロセスに対するスケジューリングポリシーと優先度を 設定する (sched_setscheduler(2), sched_setparam(2))。 * 任 意 の プ ロ セ ス に 対 す る CPU affinity を設定できる (sched_setaffinity(2))。 * 任意のプロセスに対して I/O スケジューリングクラスと優先度を 設 定できる (ioprio_set(2))。 * migrate_pages(2) を任意のプロセスに適用し、プロセスを任意のノ ードに移動する。 * move_pages(2) を任意のプロセスに対して行う。 * mbind(2) と move_pages(2) で MPOL_MF_MOVE_ALL フラグを使用する 。 CAP_SYS_PACCT acct(2) を呼び出す。 CAP_SYS_PTRACE ptrace(2) を使って任意のプロセスをトレースする。 CAP_SYS_RAWIO I/O ポート操作を実行する (iopl(2) 、 ioperm(2))。 /proc/kcore に アクセスできる。 CAP_SYS_RESOURCE * ext2 ファイルシステム上の予約されている領域を使用する。 * ext3 のジャーナル機能を制御する ioctl(2) を使用する。 * ディスク quota の上限を上書きする。 * リソース上限を増やす (setrlimit(2))。 * RLIMIT_NPROC リソース制限を上書きする。 * メッセージキューに関する上 限 msg_qbytes を /proc/sys/ker- nel/msgmnb に指定されている上限よりも大きく設定する (msgop(2) と msgctl(2) 参照)。 CAP_SYS_TIME システムク ロ ッ ク を 変 更 す る (settimeofday(2), stime(2), adjtimex(2))。リアルタイム (ハードウェア) クロックを変更する。 CAP_SYS_TTY_CONFIG vhangup(2) を呼び出す。 過去と現在の実装 完全な形のケーパビリティを実装するには、以下の要件を満たす必要がある: 1. 全ての特権操作について、カーネルはそのスレッドの実効ケーパビリティセ ットに必要なケーパビリティがあるかを確認する。 2. カーネルで、あるスレッドのケーパビリティセットを変更したり、取得した りできるシステムコールが提供される。 3. ファイルシステムが、実行可能ファイルにケーパビリティを付与でき、ファ イル実行時にそのケーパビリティをプロセスが取得できるような機能をサポ ートする。 カ ーネル 2.6.24 より前では、最初の 2つの要件のみが満たされている。カー ネル 2.6.24 以降では、3つの要件すべてが満たされている。 スレッドケーパビリティセット 各スレッドは以下の 3種類のケーパビリティセットを持つ。各々のケーパビ リ テ ィセットは上記のケーパビリティの組み合わせである (全てのケーパビリテ ィが無効でもよい)。 許可 (permitted): そのスレッドが持つことになっている実効ケーパビリティの限定的なス ーパーセットである。これは、実効ケーパビリティセットに CAP_SETP- CAP ケーパビリティを持っていないスレッドが継承可能ケーパビリティ セットに追加可能なケーパビリティの限定的なスーパーセットでもある 。 許可ケーパビリティセットから削除してしまったケーパビリティ は 、 (set-user-ID-root プログラムか、そのケーパビリティをファイルケー パビリティで許可しているプログラムを execve(2) しない限りは) も う一度獲得することはできない。 継承可能 (inheritable): execve(2) を前後で保持されるケーパビリティセットである。この仕組 みを使うことで、あるプロセスが execve(2) を行う際に新しいプロ グ ラムの許可ケーパビリティセットとして割り当てるケーパビリティを指 定することができる。 実効 (effective): カーネルがスレッドの権限 (permission) をチェックするときに使用す るケーパビリティセットである。 fork(2) で作成される子プロセスは、親のケーパビリティセットのコピーを継 承する。 execve(2) 中のケーパビリティの扱いについては下記を参照のこと。 capset(2) を使うと、プロセスは自分自身のケーパビリティセットを操作する ことができる (下記参照)。 ファイルケーパビリティ カーネル 2.6.24 以降では、 setcap(8) を使って実行ファイルにケーパビリテ ィ セットを対応付けることができる。ファイルケーパビリティセットは secu- rity.capability という名前の拡張属性に保存される (setxattr(2) 参照)。こ の拡張属性への書き込みには CAP_SETFCAP ケーパビリティが必要である。ファ イルケーパビリティセットとスレッドのケーパビリティセットの両方が考慮 さ れ、 execve(2) 後のスレッドのケーパビリティセットが決定される。 3 つのファイルケーパビリティセットが定義されている。 許可 (Permitted) (以前の強制 (Forced)): スレッドの継承可能ケーパビリティに関わらず、そのスレッドに自動的 に認められるケーパビリティ。 継承可能 (Inheritable) (以前の 許容 (Allowed)): このセットと、スレッドの継承可能ケーパビリティセットとの論 理 積 (AND) がとられ、 execve(2) の後にそのスレッドの許可ケーパビリテ ィセットで有効となる継承可能ケーパビリティが決定される。 実効 (Effective): これは集合ではなく、1 ビットの情報である。このビットがセットされ て いると、 execve(2) 実行中に、そのスレッドの新しい許可ケーパビ リティが全て実効ケーパビリティ集合においてもセットされる。このビ ッ トがセットされていない場合、 execve(2) 後には新しい許可ケーパ ビリティのどれも新しい実効ケーパビリティ集合にセットされない。 ファイルの実効ケーパビリティビットを有効にする と い う の は 、 execve(2) 実行時に、ファイルの許可ケーパビリティと継承ケーパビリ ティに対応するものがスレッドの許可ケーパビリティセットとしてセッ トされるが、これが実効ケーパビリティセットにもセットされるという ことである (ケーパビリティの変換ルールは下記参照)。したがって 、 フ ァ イ ル に ケ ー パ ビ リ テ ィ を 割 り当てる際 (setcap(8), cap_set_file(3), cap_set_fd(3))、いずれかのケーパビリティに対 し て実効フラグを有効と指定する場合、許可フラグや継承可能フラグを有 効にした他の全てのケーパビリティについても実効フラグを有効と指定 しなければならない。 execve() 中のケーパビリティの変換 execve(2) 実行時に、カーネルはプロセスの新しいケーパビリティを次のアル ゴリズムを用いて計算する: P’(permitted) = (P(inheritable) & F(inheritable)) | (F(permitted) & cap_bset) P’(effective) = F(effective) ? P’(permitted) : 0 P’(inheritable) = P(inheritable) [つまり、変更されない] 各変数の意味は以下の通り: P execve(2) 前のスレッドのケーパビリティセットの値 P’ execve(2) 後のスレッドのケーパビリティセットの値 F ファイルケーパビリティセットの値 cap_bset ケーパビリティバウンディングセットの値 (下記参照) ケーパビリティと、ルートによるプログラムの実行 execve(2) 時に、ケーパビリティセットを使って、全ての権限を持っ た root を実現するには、以下のようにする。 1. set-user-ID-root プログラムが実行される場合、またはプロセスの実ユー ザ ID が 0 (root) の場合、ファイルの継承可能セットと許可セットを全て 1 (全てのケーパビリティが有効) に定義する。 2. set-user-ID-root プログラムが実行される場合、ファイルの実効ケーパビ リティビットを 1 (enabled) に定義する。 上記のルールにケーパビリティ変換を適用した結果をまとめると、プロセス が set-user-ID-root プログラムを execve(2) する場合、または実効 UID が 0 のプロセスがプログラムを execve(2) する場合、許可と実効のケーパビリティ セ ットの全ケーパビリティ (正確には、ケーパビリティバウンディングセット によるマスクで除外されるもの以外の全てのケーパビリティ) を取得すると い う ことである。これにより、伝統的な Unix システムと同じ振る舞いができる ようになっている。 ケーパビリティ・バウンディングセット ケーパビリティ・バウンディングセット (capability bounding set) は 、 execve(2) 時に獲得できるケーパビリティを制限するために使われるセキュリ ティ機構である。バウンディングセットは以下のように使用される。 * execve(2) 実行時に、ケーパビリティ・バウンディングセットとファイル の 許可ケーパビリティセットの論理和 (AND) を取ったものが、そのスレッドの 許可ケーパビリティセットに割り当てられる。つまり、ケーパビリティ・ バ ウ ンディングセットは、実行ファイルが認めている許可ケーパビリティに対 して制限を課す働きをする。 * (Linux 2.6.25 以降) ケーパビリティ・バウンディングセットは、スレッ ド が capset(2) により自身の継承可能セットに追加可能なケーパビリティの母 集団を制限する役割を持つ。スレッドに許可されたケーパビリティであっ て も 、バウンディングセットに含まれていなければ、スレッドはそのケーパビ リティは自身の継承可能セットに追加できず、その結果、継承可能セット に そのケーパビリティを含むファイルを execve(2) する場合だけ、そのケーパ ビリティを許可セットに持ち続けることができる、ということである。 バウンディングセットがマスクを行うのは、継承可能ケーパビリティではな く 、 ファイルの許可ケーパビリティのマスクを行う点に注意すること。あるスレ ッドの継承可能セットにそのスレッドのバウンディングセットに存在しない ケ ー パビリティが含まれている場合、そのスレッドは、継承可能セットに含まれ るケーパビリティを持つファイルを実行することにより、許可セットに含ま れ るケーパビリティも獲得できるということである。 カ ーネルのバージョンにより、ケーパビリティ・バウンディングセットはシス テム共通の属性の場合と、プロセス単位の属性の場合がある。 Linux 2.6.25 より前のケーパビリティ・バウンディングセット 2.6.25 より前のカーネルでは、ケーパビリティ・バウンディングセットはシス テ ム共通の属性で、システム上の全てのスレッドに適用される。バウンディン グセットは /proc/sys/kernel/cap-bound ファイル経由で参照できる。 (間 違 え やすいが、このビットマスク形式のパラメータは、 /proc/sys/kernel/cap- bound では符号付きの十進数で表現される。) init プロセスだけがケーパビリティ・バウンディングセットでケーパビリティ を セットすることができる。それ以外では、スーパーユーザ (より正確には、 CAP_SYS_MODULE ケーパビリティを持ったプログラム) が、ケーパビリティ・バ ウンディングセットのケーパビリティのクリアができるだけである。 通 常のシステムでは、ケーパビリティ・バウンディングセットは、 CAP_SETP- CAP が無効になっている。この制限を取り去るには (取り去るのは 危 険!) 、 include/linux/capability.h 内の CAP_INIT_EFF_SET の定義を修正し、カーネ ルを再構築する必要がある。 システム共通のケーパビリティ・バウンディングセット機能は 、 カ ー ネ ル 2.2.11 以降で Linux に追加された。 Linux 2.6.25 以降のケーパビリティ・バウンディングセット Linux 2.6.25 以降では、「ケーパビリティ・バウンディングセット」はスレッ ド単位の属性である (システム共通のケーパビリティ・バウンディングセッ ト はもはや存在しない)。 バウンディングセットは fork(2) 時にはスレッドの親プロセスから継承され、 execve(2) の前後では保持される。 スレッドが CAP_SETPCAP ケーパビリティを持っている場合、そのスレッ ド は prctl(2) の PR_CAPBSET_DROP 操作を使って自身のケーパビリティ・バウンデ ィングセットからケーパビリティを削除することができる。いったんケーパ ビ リ ティをバウンディングセットから削除してしまうと、スレッドはそのケーパ ビリティを再度セットすることはできない。 prctl(2) の PR_CAPBSET_READ 操 作 を使うことで、スレッドがあるケーパビリティが自身のバウンディングセッ トに含まれているかを知ることができる。 バウンディングセットからのケーパビリティの削除がサポートされるのは、 カ ー ネ ル の コ ンパイル時にファイルケーパビリティが有効になっている場合 (CONFIG_SECURITY_FILE_CAPABILITIES) だけである。この場合には、 (全て の プロセスの先祖である) init プロセスはバウンディングセットで全てのケーパ ビリティがセットされた状態で開始する。ファイルケーパビリティが有効に な っていない場合には、 init はバウンディングセットで CAP_SETPCAP 以外の全 てのケーパビリティがセットされた状態で開始する。このようになっている の は、 CAP_SETPCAP ケーパビリティがファイルケーパビリティがサポートされて いない場合には違った意味を持つからである。 バウンディングセットからケーパビリティを削除しても、スレッドの継承可 能 セ ットからはそのケーパビリティは削除されない。しかしながら、バウンディ ングセットからの削除により、この先そのケーパビリティをスレッドの継承 可 能セットに追加することはできなくなる。 ユーザ ID 変更のケーパビリティへの影響 ユ ーザ ID が 0 と 0 以外の間で変化する際の振る舞いを従来と同じにするた め、スレッドの実 UID、実効 UID、保存 set-user-ID、ファイルシステム UID が (setuid(2), setresuid(2) などを使って) 変更された際に、カーネルはそ のスレッドのケーパビリティセットに以下の変更を行う: 1. UID の変更前には実 UID、実効 UID、保存 set-user-ID のうち少なくと も 一 つ が 0 で、変更後に実 UID、実効 UID、保存 set-user-ID がすべて 0 以外の値になった場合、許可と実効のケーパビリティセットの全ケーパビリ ティをクリアする。 2. 実効 UID が 0 から 0 以外に変更された場合、実効ケーパビリティセット の全ケーパビリティをクリアする。 3. 実効 UID が 0 以外から 0 に変更された場合、許可ケーパビリティセッ ト の内容を実効ケーパビリティセットにコピーする。 4. フ ァイルシステム UID が 0 から 0 以外に変更された場合 (setfsuid(2) 参照)、実効ケーパビリティセットの以下のケーパビリティがクリアされる: CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH, CAP_FOWNER, CAP_FSETID, CAP_MAC_OVERRIDE. ファイルシステム UID が 0 以外から 0 に変更された場合、上記のケーパビリティのうち許可ケーパビリティセット で有効になっているものが実効ケーパビリティセットで有効にされる。 各種 UID のうち少なくとも一つが 0 であるスレッドが、その UID の全てが 0 以 外になったときに許可ケーパビリティセットがクリアされないようにしたい 場合には、 prctl(2) の PR_SET_KEEPCAPS 操作を使えばよい。 プログラムでケーパビリティセットを調整する 各スレッドは、 capget(2) や capset(2) を使って、自身のケーパビリティ セ ッ トを取得したり変更したりできる。ただし、これを行うには、 libcap パッ ケージで提供されている cap_get_proc(3) や cap_set_proc(3) を使うのが 望 ま しい。スレッドのケーパビリティセットの変更には以下のルールが適用され る。 1. 呼び出し側が CAP_SETPCAP ケーパビリティを持っていない場合、新しい 継 承 可能セットは、既存の継承可能セットと許可セットの積集合 (AND) の部 分集合でなければならない。 2. (カーネル 2.6.25 以降) 新しい継承可能セットは、既存の継承可能セッ ト と ケーパビリティ・バウンディングセットの積集合 (AND) の部分集合でな ければならない。 3. 新しい許可セットは、既存の許可セットの部分集合でなければならない (つ まり、そのスレッドが現在持っていない許可ケーパビリティを獲得すること はできない)。 4. 新しい実効ケーパビリティセットは新しい許可ケーパビリティセットの部分 集合になっていなければならない。 securebits フラグ: ケーパビリティだけの環境を構築する カ ーネル 2.6.26 以降で、ファイルケーパビリティが有効になったカーネルで は、スレッド単位の securebits フラグが実装されており、このフラグを使 う と UID 0 (root) に対するケーパビリティの特別扱いを無効することができる 。以下のようなフラグがある。 SECURE_KEEP_CAPS このフラグをセットされている場合、UID が 0 のスレッドの UID が 0 以外の値に切り替わる際に、そのスレッドはケーパビリティを維持する ことができる。このフラグがセットされていない場合には、UID が 0 か ら 0 以外の値に切り替わると、そのスレッドは全てのケーパビリテ ィを失う。このフラグは execve(2) 時には全てクリアされる (この フ ラ グは、以前の prctl(2) の PR_SET_KEEPCAPS 操作と同じ機能を提供 するものである)。 SECURE_NO_SETUID_FIXUP このフラグをセットすると、スレッドの実効 UID とファイルシステ ム UID が 0 と 0 以外の間で切り替わった場合に、カーネルはケーパビリ ティセットの調整を行わなくなる (「ユーザ ID 変更のケーパビリティ への影響」の節を参照)。 SECURE_NOROOT このビットがセットされている場合、 set-user-ID-root プログラムの 実行時や、実効 UID か 実 UID が 0 のプロセスが execve(2) を呼 び 出した時に、カーネルはケーパビリティを許可しない (「ケーパビリテ ィと、ルートによるプログラムの実行」の節を参照)。 上記の "base" フラグの各々には対応する "locked" フラグが存在する。い ず れ の "locked" フラグも一度セットされると戻すことはできず、それ以降は対 応する "base" フラグを変更することができなくなる。 "locked" フ ラ グ は SECURE_KEEP_CAPS_LOCKED, SECURE_NO_SETUID_FIXUP_LOCKED, SECURE_NOROOT_LOCKED という名前である。 securebits フ ラ グ は 、 prctl(2) の 操 作 PR_SET_SECUREBITS や PR_GET_SECUREBITS を使うことで変更したり取得したりできる。フラグを変更 するには CAP_SETPCAP ケーパビリティが必要である。 securebits フラグは子プロセスに継承される。 execve(2) に お い て は 、 SECURE_KEEP_CAPS が常にクリアされる以外は、全てのフラグが保持される。 ア プリケーションは、以下の呼び出しを行うことにより、自分自身および子孫 となるプロセス全てに対して、必要なファイルケーパビリティを持ったプロ グ ラ ムを実行しない限り、対応するケーパビリティを獲得できないような状況に 閉じこめることができる。 prctl(PR_SET_SECUREBITS, 1 << SECURE_KEEP_CAPS_LOCKED | 1 << SECURE_NO_SETUID_FIXUP | 1 << SECURE_NO_SETUID_FIXUP_LOCKED | 1 << SECURE_NOROOT | 1 << SECURE_NOROOT_LOCKED); 準拠 ケーパビリティに関する標準はないが、 Linux のケーパビリティは廃案になっ た POSIX.1e 草案に基づいて実装されている。 http://wt.xpilot.org/publi- cations/posix.1e/ を参照。 注意 カーネル 2.5.27 以降、ケーパビリティは選択式のカーネルコンポーネント と な っており、カーネル設定オプション CONFIG_SECURITY_CAPABILITIES により 有効/無効を切り替えることができる。 /proc/PID/task/TID/status ファイルを使うと、スレッドのケーパビリティ セ ッ トを見ることができる。 /proc/PID/status ファイルには、プロセスのメイ ンスレッドのケーパビリティセットが表示される。 libcap パッケージは、ケーパビリティを設定・取得するためのルーチン群を提 供 している。これらのインタフェースは、 capset(2) と capget(2) が提供す るインターフェースと比べて、より使いやすく、変更される可能性が少ない 。 こ のパッケージでは、 setcap(8), getcap(8) というプログラムも提供されて いる。パッケージは http://www.kernel.org/pub/linux/libs/security/linux- privs で入手できる。 バ ージョン 2.6.24 より前、およびファイルケーパビリティが有効になってい ない2.6.24 以降のカーネルでは、 CAP_SETPCAP ケーパビリティを持ったス レ ッ ドは自分以外のスレッドのケーパビリティを操作できる。しかしながら、こ れは理論的に可能というだけである。以下のいずれかの場合においても、ど の スレッドも CAP_SETPCAP ケーパビリティを持つことはないからである。 * 2.6.25 より前の実装では、システム共通のケーパビリティ・バウンディング セット /proc/sys/kernel/cap-bound ではこのケーパビリティは常に無効 に な っており、ソースを変更してカーネルを再コンパイルしない限り、これを 変更することはできない。 * 現在の実装ではファイルケーパビリティが無効になっている場合、プロセ ス 毎 のバウンディングセットからこのケーパビリティを抜いて init は開始さ れ、システム上で生成される他の全てのプロセスでこのバウンディングセ ッ トが継承される。 関連項目 capget(2), prctl(2), setfsuid(2), cap_clear(3), cap_copy_ext(3), cap_from_text(3), cap_get_file(3), cap_get_proc(3), cap_init(3), capgetp(3), capsetp(3), credentials(7), pthreads(7), getcap(8), set- cap(8) カーネルソース内の include/linux/capability.h Linux 2008-11-27 CAPABILITIES(7)
CAPABILITIES(7) Linux Programmer’s Manual CAPABILITIES(7) NAME capabilities - overview of Linux capabilities DESCRIPTION For the purpose of performing permission checks, traditional Unix implementations distinguish two categories of processes: privileged processes (whose effective user ID is 0, referred to as superuser or root), and unprivileged processes (whose effective UID is non-zero). Privileged processes bypass all kernel permission checks, while unpriv- ileged processes are subject to full permission checking based on the process’s credentials (usually: effective UID, effective GID, and sup- plementary group list). Starting with kernel 2.2, Linux divides the privileges traditionally associated with superuser into distinct units, known as capabilities, which can be independently enabled and disabled. Capabilities are a per-thread attribute. Capabilities List The following list shows the capabilities implemented on Linux, and the operations or behaviors that each capability permits: CAP_AUDIT_CONTROL (since Linux 2.6.11) Enable and disable kernel auditing; change auditing filter rules; retrieve auditing status and filtering rules. CAP_AUDIT_WRITE (since Linux 2.6.11) Write records to kernel auditing log. CAP_CHOWN Make arbitrary changes to file UIDs and GIDs (see chown(2)). CAP_DAC_OVERRIDE Bypass file read, write, and execute permission checks. (DAC is an abbreviation of "discretionary access control".) CAP_DAC_READ_SEARCH Bypass file read permission checks and directory read and exe- cute permission checks. CAP_FOWNER * Bypass permission checks on operations that normally require the file system UID of the process to match the UID of the file (e.g., chmod(2), utime(2)), excluding those operations covered by CAP_DAC_OVERRIDE and CAP_DAC_READ_SEARCH; * set extended file attributes (see chattr(1)) on arbitrary files; * set Access Control Lists (ACLs) on arbitrary files; * ignore directory sticky bit on file deletion; * specify O_NOATIME for arbitrary files in open(2) and fcntl(2). CAP_FSETID Don’t clear set-user-ID and set-group-ID permission bits when a file is modified; set the set-group-ID bit for a file whose GID does not match the file system or any of the supplementary GIDs of the calling process. CAP_IPC_LOCK Lock memory (mlock(2), mlockall(2), mmap(2), shmctl(2)). CAP_IPC_OWNER Bypass permission checks for operations on System V IPC objects. CAP_KILL Bypass permission checks for sending signals (see kill(2)). This includes use of the ioctl(2) KDSIGACCEPT operation. CAP_LEASE (since Linux 2.4) Establish leases on arbitrary files (see fcntl(2)). CAP_LINUX_IMMUTABLE Set the FS_APPEND_FL and FS_IMMUTABLE_FL i-node flags (see chattr(1)). CAP_MAC_ADMIN (since Linux 2.6.25) Override Mandatory Access Control (MAC). Implemented for the Smack Linux Security Module (LSM). CAP_MAC_OVERRIDE (since Linux 2.6.25) Allow MAC configuration or state changes. Implemented for the Smack LSM. CAP_MKNOD (since Linux 2.4) Create special files using mknod(2). CAP_NET_ADMIN Perform various network-related operations (e.g., setting privi- leged socket options, enabling multicasting, interface configu- ration, modifying routing tables). CAP_NET_BIND_SERVICE Bind a socket to Internet domain privileged ports (port numbers less than 1024). CAP_NET_BROADCAST (Unused) Make socket broadcasts, and listen to multicasts. CAP_NET_RAW Use RAW and PACKET sockets. CAP_SETGID Make arbitrary manipulations of process GIDs and supplementary GID list; forge GID when passing socket credentials via Unix domain sockets. CAP_SETFCAP (since Linux 2.6.24) Set file capabilities. CAP_SETPCAP If file capabilities are not supported: grant or remove any capability in the caller’s permitted capability set to or from any other process. (This property of CAP_SETPCAP is not avail- able when the kernel is configured to support file capabilities, since CAP_SETPCAP has entirely different semantics for such ker- nels.) If file capabilities are supported: add any capability from the calling thread’s bounding set to its inheritable set; drop capa- bilities from the bounding set (via prctl(2) PR_CAPBSET_DROP); make changes to the securebits flags. CAP_SETUID Make arbitrary manipulations of process UIDs (setuid(2), setreuid(2), setresuid(2), setfsuid(2)); make forged UID when passing socket credentials via Unix domain sockets. CAP_SYS_ADMIN * Perform a range of system administration operations including: quotactl(2), mount(2), umount(2), swapon(2), swapoff(2), sethostname(2), and setdomainname(2); * perform IPC_SET and IPC_RMID operations on arbitrary System V IPC objects; * perform operations on trusted and security Extended Attributes (see attr(5)); * use lookup_dcookie(2); * use ioprio_set(2) to assign IOPRIO_CLASS_RT and (before Linux 2.6.25) IOPRIO_CLASS_IDLE I/O scheduling classes; * forge UID when passing socket credentials; * exceed /proc/sys/fs/file-max, the system-wide limit on the number of open files, in system calls that open files (e.g., accept(2), execve(2), open(2), pipe(2)); * employ CLONE_NEWNS flag with clone(2) and unshare(2); * perform KEYCTL_CHOWN and KEYCTL_SETPERM keyctl(2) operations. CAP_SYS_BOOT Use reboot(2) and kexec_load(2). CAP_SYS_CHROOT Use chroot(2). CAP_SYS_MODULE Load and unload kernel modules (see init_module(2) and delete_module(2)); in kernels before 2.6.25: drop capabilities from the system-wide capability bounding set. CAP_SYS_NICE * Raise process nice value (nice(2), setpriority(2)) and change the nice value for arbitrary processes; * set real-time scheduling policies for calling process, and set scheduling policies and priorities for arbitrary processes (sched_setscheduler(2), sched_setparam(2)); * set CPU affinity for arbitrary processes (sched_setaffin- ity(2)); * set I/O scheduling class and priority for arbitrary processes (ioprio_set(2)); * apply migrate_pages(2) to arbitrary processes and allow pro- cesses to be migrated to arbitrary nodes; * apply move_pages(2) to arbitrary processes; * use the MPOL_MF_MOVE_ALL flag with mbind(2) and move_pages(2). CAP_SYS_PACCT Use acct(2). CAP_SYS_PTRACE Trace arbitrary processes using ptrace(2) CAP_SYS_RAWIO Perform I/O port operations (iopl(2) and ioperm(2)); access /proc/kcore. CAP_SYS_RESOURCE * Use reserved space on ext2 file systems; * make ioctl(2) calls controlling ext3 journaling; * override disk quota limits; * increase resource limits (see setrlimit(2)); * override RLIMIT_NPROC resource limit; * raise msg_qbytes limit for a System V message queue above the limit in /proc/sys/kernel/msgmnb (see msgop(2) and msgctl(2)). CAP_SYS_TIME Set system clock (settimeofday(2), stime(2), adjtimex(2)); set real-time (hardware) clock. CAP_SYS_TTY_CONFIG Use vhangup(2). Past and Current Implementation A full implementation of capabilities requires that: 1. For all privileged operations, the kernel must check whether the thread has the required capability in its effective set. 2. The kernel must provide system calls allowing a thread’s capability sets to be changed and retrieved. 3. The file system must support attaching capabilities to an executable file, so that a process gains those capabilities when the file is executed. Before kernel 2.6.24, only the first two of these requirements are met; since kernel 2.6.24, all three requirements are met. Thread Capability Sets Each thread has three capability sets containing zero or more of the above capabilities: Permitted: This is a limiting superset for the effective capabilities that the thread may assume. It is also a limiting superset for the capabilities that may be added to the inheritable set by a thread that does not have the CAP_SETPCAP capability in its effective set. If a thread drops a capability from its permitted set, it can never re-acquire that capability (unless it execve(2)s either a set-user-ID-root program, or a program whose associated file capabilities grant that capability). Inheritable: This is a set of capabilities preserved across an execve(2). It provides a mechanism for a process to assign capabilities to the permitted set of the new program during an execve(2). Effective: This is the set of capabilities used by the kernel to perform permission checks for the thread. A child created via fork(2) inherits copies of its parent’s capability sets. See below for a discussion of the treatment of capabilities dur- ing execve(2). Using capset(2), a thread may manipulate its own capability sets (see below). File Capabilities Since kernel 2.6.24, the kernel supports associating capability sets with an executable file using setcap(8). The file capability sets are stored in an extended attribute (see setxattr(2)) named security.capa- bility. Writing to this extended attribute requires the CAP_SETFCAP capability. The file capability sets, in conjunction with the capabil- ity sets of the thread, determine the capabilities of a thread after an execve(2). The three file capability sets are: Permitted (formerly known as forced): These capabilities are automatically permitted to the thread, regardless of the thread’s inheritable capabilities. Inheritable (formerly known as allowed): This set is ANDed with the thread’s inheritable set to determine which inheritable capabilities are enabled in the permitted set of the thread after the execve(2). Effective: This is not a set, but rather just a single bit. If this bit is set, then during an execve(2) all of the new permitted capabili- ties for the thread are also raised in the effective set. If this bit is not set, then after an execve(2), none of the new permitted capabilities is in the new effective set. Enabling the file effective capability bit implies that any file permitted or inheritable capability that causes a thread to acquire the corresponding permitted capability during an execve(2) (see the transformation rules described below) will also acquire that capability in its effective set. Therefore, when assigning capabilities to a file (setcap(8), cap_set_file(3), cap_set_fd(3)), if we specify the effective flag as being enabled for any capability, then the effective flag must also be specified as enabled for all other capabili- ties for which the corresponding permitted or inheritable flags is enabled. Transformation of Capabilities During execve() During an execve(2), the kernel calculates the new capabilities of the process using the following algorithm: P’(permitted) = (P(inheritable) & F(inheritable)) | (F(permitted) & cap_bset) P’(effective) = F(effective) ? P’(permitted) : 0 P’(inheritable) = P(inheritable) [i.e., unchanged] where: P denotes the value of a thread capability set before the execve(2) P’ denotes the value of a capability set after the execve(2) F denotes a file capability set cap_bset is the value of the capability bounding set (described below). Capabilities and execution of programs by root In order to provide an all-powerful root using capability sets, during an execve(2): 1. If a set-user-ID-root program is being executed, or the real user ID of the process is 0 (root) then the file inheritable and permitted sets are defined to be all ones (i.e., all capabilities enabled). 2. If a set-user-ID-root program is being executed, then the file effective bit is defined to be one (enabled). The upshot of the above rules, combined with the capabilities transfor- mations described above, is that when a process execve(2)s a set-user- ID-root program, or when a process with an effective UID of 0 execve(2)s a program, it gains all capabilities in its permitted and effective capability sets, except those masked out by the capability bounding set. This provides semantics that are the same as those pro- vided by traditional Unix systems. Capability bounding set The capability bounding set is a security mechanism that can be used to limit the capabilities that can be gained during an execve(2). The bounding set is used in the following ways: * During an execve(2), the capability bounding set is ANDed with the file permitted capability set, and the result of this operation is assigned to the thread’s permitted capability set. The capability bounding set thus places a limit on the permitted capabilities that may be granted by an executable file. * (Since Linux 2.6.25) The capability bounding set acts as a limiting superset for the capabilities that a thread can add to its inheritable set using capset(2). This means that if the capability is not in the bounding set, then a thread can’t add one of its per- mitted capabilities to its inheritable set and thereby have that capability preserved in its permitted set when it execve(2)s a file that has the capability in its inheritable set. Note that the bounding set masks the file permitted capabilities, but not the inherited capabilities. If a thread maintains a capability in its inherited set that is not in its bounding set, then it can still gain that capability in its permitted set by executing a file that has the capability in its inherited set. Depending on the kernel version, the capability bounding set is either a system-wide attribute, or a per-process attribute. Capability bounding set prior to Linux 2.6.25 In kernels before 2.6.25, the capability bounding set is a system-wide attribute that affects all threads on the system. The bounding set is accessible via the file /proc/sys/kernel/cap-bound. (Confusingly, this bit mask parameter is expressed as a signed decimal number in /proc/sys/kernel/cap-bound.) Only the init process may set capabilities in the capability bounding set; other than that, the superuser (more precisely: programs with the CAP_SYS_MODULE capability) may only clear capabilities from this set. On a standard system the capability bounding set always masks out the CAP_SETPCAP capability. To remove this restriction (dangerous!), mod- ify the definition of CAP_INIT_EFF_SET in include/linux/capability.h and rebuild the kernel. The system-wide capability bounding set feature was added to Linux starting with kernel version 2.2.11. Capability bounding set from Linux 2.6.25 onwards From Linux 2.6.25, the capability bounding set is a per-thread attribute. (There is no longer a system-wide capability bounding set.) The bounding set is inherited at fork(2) from the thread’s parent, and is preserved across an execve(2). A thread may remove capabilities from its capability bounding set using the prctl(2) PR_CAPBSET_DROP operation, provided it has the CAP_SETPCAP capability. Once a capability has been dropped from the bounding set, it cannot be restored to that set. A thread can determine if a capa- bility is in its bounding set using the prctl(2) PR_CAPBSET_READ opera- tion. Removing capabilities from the bounding set is only supported if file capabilities are compiled into the kernel (CONFIG_SECURITY_FILE_CAPA- BILITIES). In that case, the init process (the ancestor of all pro- cesses) begins with a full bounding set. If file capabilities are not compiled into the kernel, then init begins with a full bounding set minus CAP_SETPCAP, because this capability has a different meaning when there are no file capabilities. Removing a capability from the bounding set does not remove it from the thread’s inherited set. However it does prevent the capability from being added back into the thread’s inherited set in the future. Effect of User ID Changes on Capabilities To preserve the traditional semantics for transitions between 0 and non-zero user IDs, the kernel makes the following changes to a thread’s capability sets on changes to the thread’s real, effective, saved set, and file system user IDs (using setuid(2), setresuid(2), or similar): 1. If one or more of the real, effective or saved set user IDs was pre- viously 0, and as a result of the UID changes all of these IDs have a non-zero value, then all capabilities are cleared from the permit- ted and effective capability sets. 2. If the effective user ID is changed from 0 to non-zero, then all capabilities are cleared from the effective set. 3. If the effective user ID is changed from non-zero to 0, then the permitted set is copied to the effective set. 4. If the file system user ID is changed from 0 to non-zero (see setf- suid(2)) then the following capabilities are cleared from the effec- tive set: CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH, CAP_FOWNER, CAP_FSETID, and CAP_MAC_OVERRIDE. If the file system UID is changed from non-zero to 0, then any of these capabilities that are enabled in the permitted set are enabled in the effective set. If a thread that has a 0 value for one or more of its user IDs wants to prevent its permitted capability set being cleared when it resets all of its user IDs to non-zero values, it can do so using the prctl(2) PR_SET_KEEPCAPS operation. Programmatically adjusting capability sets A thread can retrieve and change its capability sets using the capget(2) and capset(2) system calls. However, the use of cap_get_proc(3) and cap_set_proc(3), both provided in the libcap pack- age, is preferred for this purpose. The following rules govern changes to the thread capability sets: 1. If the caller does not have the CAP_SETPCAP capability, the new inheritable set must be a subset of the combination of the existing inheritable and permitted sets. 2. (Since kernel 2.6.25) The new inheritable set must be a subset of the combination of the existing inheritable set and the capability bounding set. 3. The new permitted set must be a subset of the existing permitted set (i.e., it is not possible to acquire permitted capabilities that the thread does not currently have). 4. The new effective set must be a subset of the new permitted set. The "securebits" flags: establishing a capabilities-only environment Starting with kernel 2.6.26, and with a kernel in which file capabili- ties are enabled, Linux implements a set of per-thread securebits flags that can be used to disable special handling of capabilities for UID 0 (root). These flags are as follows: SECURE_KEEP_CAPS Setting this flag allows a thread that has one or more 0 UIDs to retain its capabilities when it switches all of its UIDs to a non-zero value. If this flag is not set, then such a UID switch causes the thread to lose all capabilities. This flag is always cleared on an execve(2). (This flag provides the same function- ality as the older prctl(2) PR_SET_KEEPCAPS operation.) SECURE_NO_SETUID_FIXUP Setting this flag stops the kernel from adjusting capability sets when the threads’s effective and file system UIDs are switched between zero and non-zero values. (See the subsection Effect of User ID Changes on Capabilities.) SECURE_NOROOT If this bit is set, then the kernel does not grant capabilities when a set-user-ID-root program is executed, or when a process with an effective or real UID of 0 calls execve(2). (See the subsection Capabilities and execution of programs by root.) Each of the above "base" flags has a companion "locked" flag. Setting any of the "locked" flags is irreversible, and has the effect of pre- venting further changes to the corresponding "base" flag. The locked flags are: SECURE_KEEP_CAPS_LOCKED, SECURE_NO_SETUID_FIXUP_LOCKED, and SECURE_NOROOT_LOCKED. The securebits flags can be modified and retrieved using the prctl(2) PR_SET_SECUREBITS and PR_GET_SECUREBITS operations. The CAP_SETPCAP capability is required to modify the flags. The securebits flags are inherited by child processes. During an execve(2), all of the flags are preserved, except SECURE_KEEP_CAPS which is always cleared. An application can use the following call to lock itself, and all of its descendants, into an environment where the only way of gaining capabilities is by executing a program with associated file capabili- ties: prctl(PR_SET_SECUREBITS, 1 << SECURE_KEEP_CAPS_LOCKED | 1 << SECURE_NO_SETUID_FIXUP | 1 << SECURE_NO_SETUID_FIXUP_LOCKED | 1 << SECURE_NOROOT | 1 << SECURE_NOROOT_LOCKED); CONFORMING TO No standards govern capabilities, but the Linux capability implementa- tion is based on the withdrawn POSIX.1e draft standard; see http://wt.xpilot.org/publications/posix.1e/. NOTES Since kernel 2.5.27, capabilities are an optional kernel component, and can be enabled/disabled via the CONFIG_SECURITY_CAPABILITIES kernel configuration option. The /proc/PID/task/TID/status file can be used to view the capability sets of a thread. The /proc/PID/status file shows the capability sets of a process’s main thread. The libcap package provides a suite of routines for setting and getting capabilities that is more comfortable and less likely to change than the interface provided by capset(2) and capget(2). This package also provides the setcap(8) and getcap(8) programs. It can be found at http://www.kernel.org/pub/linux/libs/security/linux-privs. Before kernel 2.6.24, and since kernel 2.6.24 if file capabilities are not enabled, a thread with the CAP_SETPCAP capability can manipulate the capabilities of threads other than itself. However, this is only theoretically possible, since no thread ever has CAP_SETPCAP in either of these cases: * In the pre-2.6.25 implementation the system-wide capability bounding set, /proc/sys/kernel/cap-bound, always masks out this capability, and this can not be changed without modifying the kernel source and rebuilding. * If file capabilities are disabled in the current implementation, then init starts out with this capability removed from its per-process bounding set, and that bounding set is inherited by all other pro- cesses created on the system. SEE ALSO capget(2), prctl(2), setfsuid(2), cap_clear(3), cap_copy_ext(3), cap_from_text(3), cap_get_file(3), cap_get_proc(3), cap_init(3), capgetp(3), capsetp(3), credentials(7), pthreads(7), getcap(8), set- cap(8) include/linux/capability.h in the kernel source COLOPHON This page is part of release 3.22 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2008-11-27 CAPABILITIES(7)
Copyright(C) linux-cmd.com All Rights Reserved. Author Takayuki Yukawa