インストールおよび設定 > ThingWorx Docker の使用 > ThingWorx Docker での Security-Enhanced Linux の使用
ThingWorx Docker での Security-Enhanced Linux の使用
Security-Enhanced Linux (SELinux) は、各種 Linux ディストリビューションに追加されているカーネル修正とユーザースペースツールのセットです。SELinux アーキテクチャは、セキュリティに関する意思決定の実施をセキュリティポリシーから分離し、セキュリティポリシーの適用に関連するソフトウェアの数を削減することを可能にします。SELinux の基礎となる主要な概念はアメリカ国家安全保障局 (NSA) による先のいくつかのプロジェクトまで遡ることができます。
SELinux のユーザーとロールが実際のシステムユーザーとロールに関連付けられている必要はありません。現在の各ユーザーまたはプロセスに、SELinux はユーザー名、ロール、ドメイン (またはタイプ) の 3 つの文字列から成るコンテキストを割り当てます。このシステムは通常必要とされるよりも柔軟性があります。原則として、実際のユーザーのほとんどは同じ SELinux ユーザー名を共有し、すべてのアクセス制御が 3 番目のタグであるドメインによって管理されます。プロセスが特定のドメインに属することを許可する環境をポリシーで設定しなければなりません。runcon コマンドを使用することで、明示的に指定したコンテキスト (ユーザー、ロール、およびドメイン) でプロセスを開始できますが、それがポリシーによって承認されていない場合、SELinux は遷移を拒否します。
ファイル、ネットワークポート、その他のハードウェアも名前、ロール (めったに使用されません)、タイプを含む SELinux コンテキストを持ちます。ファイルシステムの場合、ファイルとセキュリティコンテキスト間のマッピングをラベリングと呼びます。ラベリングはポリシーファイルで定義されますが、ポリシーを変更せずに手動で調整することもできます。bin_t (/bin フォルダ内のすべてのファイル) や postgresql_port_t (PostgreSQL ポート、5432) など、ハードウェアタイプの詳細も定義されています。マウント時にリモートファイルシステムの SELinux コンテキストを明示的に指定できます。
SELinux は -Z スイッチを shell コマンド lsps などに追加することで、ファイルまたはプロセスのセキュリティコンテキストを表示可能にします。
通常、ポリシー規則は、指定されたターゲットに対して特定の操作を実行するためにユーザーはどのドメインを所持していなければならないかといった、明示的なアクセス許可から成ります。このような操作の例としては、読み取り、実行、そしてネットワークポートの場合は、バインドや接続が挙げられます。ロールおよびセキュリティレベルを必要とするさらに複雑なマッピングも可能です。
通常のポリシーは、マッピング (ラベリング) ファイル、規則ファイル、およびドメインの遷移を定義するインタフェースファイルから成ります。SELinux ツールを使用してこの 3 つのファイルをコンパイルして単一のポリシーファイルを生成しなければなりません。生成されたポリシーファイルをカーネルにロードすることで、アクティブにできます。ポリシーのロードとアンロードは再起動を必要としません。ポリシーファイルは手動で作成することも、さらにわかりやすい SELinux 管理ツールから生成することもできます。これらのファイルは、通常、違反がログに記録されるが許容される permissive モードで最初にテストします。後から audit2allow ツールを使用して、制限されているアプリケーションのすべての正当なアクティビティを許可するようにポリシーを拡張する追加の規則を作成できます。
SELinux のよくある問題
SELinux で最もよくある問題は、アクセス許可が拒否され、SELinux が動作していないと表示されることです。アクセス許可拒否のエラーメッセージが表示されるのは、アクセスを試みているシステムオブジェクトに、そのオブジェクトへのアクセスに使用するプログラムまたはユーザー資格証明が関連付けられていない場合です。これらのエラーを解決するのは困難ですが、トラブルシューティングに役立つツールが用意されています。
Setools および Setroubleshoot のインストール
これらのツールをお使いのシステムにインストールするには、以下の手順に従います。
1. 管理者権限が付与されているアカウントを使用してサーバーまたはデスクトップにログインします。
2. コマンドシェルを開きます。
3. Yum を使用して setroubleshoot パッケージをインストールします。
yum install setroubleshoot setools
SELinux アラート
SELinux によって使用されている監査ログを解析するには、sealert ツールを使用します。このツールはログファイルとレポートをスキャンし、検出された SELinux のすべての問題についてレポートを生成します。
コマンドラインから sealert を実行するには、SELinux 監査ログを指定します。以下の例を参照してください。
sealert -a /var/log/audit/audit.log
レポートでは各問題が示され、その解決方法について説明されています。sealert によって生成された出力の一部を例として以下に示します。
100% done found 1 alerts in /var/log/audit/audit.log
--------------------------------------------------------------------------------
SELinux is preventing /usr/sbin/httpd from getattr access on the file
/myapps/app1/html/index.html.
***** Plugin catchall_labels (83.8 confidence) suggests ********************
If you want to allow httpd to have getattr access on the index.html file
Then you need to change the label on /myapps/app1/html/index.html
Do
# semanage fcontext -a -t FILE_TYPE '/myapps/app1/html/index.html'
where FILE_TYPE is one of the following: sssd_var_lib_t, public_content_t, anon_inodefs_t,
[..truncated..]
httpd_sys_ra_content_t, httpd_sys_rw_content_t, httpd_sys_rw_content_t,
httpd_w3c_validator_content_t.
Then execute:
restorecon -v '/myapps/app1/html/index.html'
***** Plugin catchall (17.1 confidence) suggests ***************************
If you believe that httpd should be allowed getattr access on the index.html file by
default. Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# grep httpd /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp
問題の解決方法について説明しているこのレポートの最も重要な部分は、各アラートの末尾に表示されています。たとえば、上記のレポートでは以下の解決策が示されています。
If you believe that httpd should be allowed getattr access on the index.html file by
default. Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# grep httpd /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp
ここでは、問題のあるファイルに適用する SELinux ポリシーを作成するという解決策が提案されています。この例では、間違った SELinux ファイルコンテキストに HTML ファイルが割り当てられていました。
ThingWorx Docker コンテナで発生する可能性がある問題の例をさらにいくつか以下に示します。
この例では、PostgreSQL での問題が示されています。この場合、sealert は以下のアラートを生成します。
found 1 alerts in /var/log/audit/audit.log
--------------------------------------------------------------------------------
SELinux is preventing /bin/chown from setattr access on the directory data.
***** Plugin catchall_labels (83.8 confidence) suggests *******************
If you want to allow chown to have setattr access on the data directory
Then you need to change the label on data
Do
# semanage fcontext -a -t FILE_TYPE 'data'
where FILE_TYPE is one of the following: cgroup_t, nfs_t, svirt_home_t,
svirt_sandbox_file_t.
Then execute:
restorecon -v 'data'

***** Plugin catchall (17.1 confidence) suggests **************************
If you believe that chown should be allowed setattr access on the data directory by
default. Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c 'chown' --raw | audit2allow -M my-chown
# semodule -i my-chown.pp

Additional Information:
Source Context system_u:system_r:svirt_lxc_net_t:s0:c459,c1008
Target Context unconfined_u:object_r:usr_t:s0
Target Objects data [ dir ]
Source chown
Source Path /bin/chown
Port <Unknown>
Host <Unknown>
Source RPM Packages
Target RPM Packages
Policy RPM selinux-policy-3.13.1-166.el7_4.9.noarch
Selinux Enabled True
Policy Type targeted
Enforcing Mode Enforcing
Host Name ip-10-99-0-109.ec2.internal
Platform Linux ip-10-99-0-109.ec2.internal
3.10.0-693.5.2.el7.x86_64 #1 SMP Fri Oct 20
20:32:50 UTC 2017 x86_64 x86_64
Alert Count 9
First Seen 2018-04-02 17:08:08 UTC
Last Seen 2018-04-02 17:08:27 UTC
Local ID 65bb52e5-bde9-4ec5-ba44-f0752ffef319
Raw Audit Messages
type=AVC msg=audit(1522688907.927:619): avc: denied { setattr } for pid=12142
comm="initdb" name="data" dev="nvme0n1p1" ino=826278009 scontext=system_u:system_r:
svirt_lxc_net_t:s0:c459,c1008 tcontext=unconfined_u:object_r:usr_t:s0 tclass=dir

type=SYSCALL msg=audit(1522688907.927:619): arch=x86_64 syscall=chmod success=no
exit=EACCES a0=5633eefb7310 a1=1c0 a2=0 a3=7f3c78d1d6a0 items=0 ppid=12116 pid=12142
auid=4294967295 uid=1001 gid=0 euid=1001 suid=1001 fsuid=1001 egid=0 sgid=0 fsgid=0
tty=(none) ses=4294967295 comm=initdb exe=/usr/lib/postgresql/9.4/bin/initdb
subj=system_u:system_r:svirt_lxc_net_t:s0:c459,c1008 key=(null)
Hash: chown,svirt_lxc_net_t,usr_t,dir,setattr
このレポートでは以下の解決策が提案されています。
If you want to allow chown to have setattr access on the data directory
Then you need to change the label on data
Do
# semanage fcontext -a -t FILE_TYPE 'data'
where FILE_TYPE is one of the following: cgroup_t, nfs_t, svirt_home_t,
svirt_sandbox_file_t.
Then execute:
restorecon -v 'data'
この解決策を実装するには以下のコマンドを入力します。
semanage fcontext -a -t svirt_sandbox_file_t "/opt/postgresql-storage(/.*)?" restorecon -R /opt/postgresql-storage
PostgreSQL のデータフォルダにだけアクセス許可を適用することが解決策として提案されています。ただし、ThingWorx テーブルスペースへの書き込みアクセスが必要なので、そのフォルダ内のすべてに同じアクセス許可を付与しなければなりません。
この例では、ThingWorx プラットフォームでの問題が示されています。この場合、sealert は以下のアラートを生成します。
found 1 alerts in /var/log/audit/audit.log
--------------------------------------------------------------------------------
SELinux is preventing /bin/mv from write access on the directory ThingworxStorage.
***** Plugin catchall_labels (83.8 confidence) suggests *******************
If you want to allow mv to have write access on the ThingworxStorage directory
Then you need to change the label on ThingworxStorage
Do
# semanage fcontext -a -t FILE_TYPE 'ThingworxStorage'
where FILE_TYPE is one of the following: cgroup_t, container_var_lib_t, nfs_t,
svirt_home_t, svirt_sandbox_file_t, tmpfs_t, virt_home_t.
Then execute:
restorecon -v 'ThingworxStorage'

***** Plugin catchall (17.1 confidence) suggests **************************
If you believe that mv should be allowed write access on the ThingworxStorage
directory by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c 'mv' --raw | audit2allow -M my-mv
# semodule -i my-mv.pp

Additional Information:
Source Context system_u:system_r:svirt_lxc_net_t:s0:c320,c876
Target Context unconfined_u:object_r:usr_t:s0
Target Objects ThingworxStorage [ dir ]
Source mv
Source Path /bin/mv
Port <Unknown>
Host <Unknown>
Source RPM Packages
Target RPM Packages
Policy RPM selinux-policy-3.13.1-166.el7_4.9.noarch
Selinux Enabled True
Policy Type targeted
Enforcing Mode Enforcing
Host Name ip-10-99-0-109.ec2.internal
Platform Linux ip-10-99-0-109.ec2.internal
3.10.0-693.5.2.el7.x86_64 #1 SMP Fri Oct 20
20:32:50 UTC 2017 x86_64 x86_64
Alert Count 13
First Seen 2018-04-02 17:13:18 UTC
Last Seen 2018-04-02 17:13:23 UTC
Local ID 1759a007-f347-4d80-99e8-ee414eae7602
Raw Audit Messages
type=AVC msg=audit(1522689203.86:708): avc: denied { write } forpid=14905
comm="java" name="ThingworxStorage" dev="nvme0n1p1" ino=864027929
scontext=system_u:system_r:svirt_lxc_net_t:s0:c320,c876
tcontext=unconfined_u:object_r:usr_t:s0 tclass=dir

type=SYSCALL msg=audit(1522689203.86:708): arch=x86_64 syscall=mkdir success=no
exit=EACCES a0=7f6480f76e80 a1=1ff a2=7f6480f76e80 a3=b items=0 ppid=14734
pid=14905 auid=4294967295 uid=1337 gid=1337 euid=1337 suid=1337 fsuid=1337
egid=1337 sgid=1337 fsgid=1337 tty=(none) ses=4294967295 comm=java exe=/opt/
jdk1.8.0_121/bin/java subj=system_u:system_r:svirt_lxc_net_t:s0:c320,c876 key=(null)
Hash: mv,svirt_lxc_net_t,usr_t,dir,write
このレポートでは以下の解決策が提案されています。
# semanage fcontext -a -t FILE_TYPE 'ThingworxStorage'
where FILE_TYPE is one of the following: cgroup_t, container_var_lib_t, nfs_t,
svirt_home_t, svirt_sandbox_file_t, tmpfs_t, virt_home_t.
Then execute:
restorecon -v 'ThingworxStorage'
この解決策を実装するには以下のコマンドを使用します。
semanage fcontext -a -t svirt_sandbox_file_t "/opt/thingworx-storage(/.*)?"
restorecon -R /opt/thingworx-storage