Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

5.10.3. デフォルト SELinux コンテキストのチェック

matchpathcon コマンドでファイルやディレクトリーに正しい SELinux コンテキストがあるかどうかをチェックします。matchpathcon(8) man ページでが 「matchpathcon がシステムポリシーにクエリを行い、ファイルパスに関連するデフォルトのセキュリティーコンテキストを出力します。」となっています。[11]。以下の例では、matchpathcon コマンドを使って /var/www/html/ ディレクトリーが正しくラベル付けされているかを確認します。
  1. Linux root ユーザーで touch /var/www/html/file{1,2,3} コマンドを実行し、3 つのファイルを作成します (file1file2file3)。これらのファイルは、/var/www/html/ ディレクトリーからの httpd_sys_content_t タイプを継承します。
    ~]# touch /var/www/html/file{1,2,3}
    ~]# ls -Z /var/www/html/
    -rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file1
    -rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file2
    -rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
    
  2. Linux root ユーザーで chcon -t samba_share_t /var/www/html/file1 コマンドを実行し、file1 タイプを samba_share_t に変更します。注記: Apache HTTP Server は、samba_share_t タイプでラベル付けされたファイルやディレクトリーを読み取れません。
  3. matchpathcon -V オプションは、現行の SELinux コンテキストを SELinux ポリシーの正しいデフォルトのコンテキストと比べます。matchpathcon -V /var/www/html/* コマンドを実行し、/var/www/html/ ディレクトリー内の全ファイルをチェックします。
    ~]$ matchpathcon -V /var/www/html/*
    /var/www/html/file1 has context unconfined_u:object_r:samba_share_t:s0, should be system_u:object_r:httpd_sys_content_t:s0
    /var/www/html/file2 verified.
    /var/www/html/file3 verified.
    
以下の matchpathcon コマンドの出力は、file1samba_share_t タイプでラベル付けされていますが、httpd_sys_content_t タイプでラベル付けされるべきであることを示しています。
/var/www/html/file1 has context unconfined_u:object_r:samba_share_t:s0, should be system_u:object_r:httpd_sys_content_t:s0
このラベル問題を解決して Apache HTTP Server が file1 にアクセスできるようにするには、Linux root ユーザーで restorecon -v /var/www/html/file1 コマンドを実行します。
~]# restorecon -v /var/www/html/file1
restorecon reset /var/www/html/file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:httpd_sys_content_t:s0


[11] Red Hat Enterprise Linux の libselinux-utils パッケージと出荷された matchpathcon(8) man ページは、Daniel Walsh が作成したものです。編集および変更はすべて、Muray McAllister によるものです