安裝與組態 > 使用 ThingWorx Docker > 針對 ThingWorx Docker 使用強化安全性的 Linux
針對 ThingWorx Docker 使用強化安全性的 Linux
強化安全性的 Linux (SELinux) 是新增至各 Linux 分佈的一組核心修改和使用者空間工具。SELinux 架構可將安全性決定的強迫執行與安全性原則分開來,並可減少安全性原則強迫執行涉及的軟體數量。SELinux 的重要概念可溯及美國國家安全局 (NSA) 幾個較早的專案。
SELinux 使用者和角色並非必須與實際系統使用者和角色相關。針對每個目前使用者或流程,SELinux 會指派三個字串的前後關聯,包括使用者名稱、角色及網域 (或類型)。此系統比通常要求更具彈性;作為規則,大多數的實際使用者共用相同的 SELinux 使用者名稱,並透過第三個標籤 (網域) 管理所有存取控制。您必須設定這些情況,以允許流程進入原則中某些網域。雖然可以使用 runcon 指令在明確指定的前後關聯 (使用者、角色和網域) 中啟動流程,但如果 SELinux 未由原則核准,則其可以拒絕轉移。
檔案、網路連接埠及其他硬體也具有 SELinux 前後關聯,其中包括名稱、角色 (很少使用) 和類型。在檔案系統的情況下,檔案與安全性前後關聯之間的對應稱為標記。標記在原則檔案中定義,但您也可以在不變更原則的前提下手動對其進行調整。硬體類型也已詳細說明,例如 bin_t (/bin 資料夾中的所有檔案) 或 postgresql_port_t (PostgreSQL 連接埠,5432)。您可以在裝載時明確指定遠端檔案系統的 SELinux 前後關聯。
SELinux 將 -Z 轉換新增至 shell 指令 lsps 與某些其他指令,以便允許顯示檔案或流程的安全性前後關聯。
通常,原則規則由明確權限組成,例如,使用者必須擁有哪些網域才能夠執行具有指定目標的某些動作。這些動作的範例包括讀取、執行,在網路連接埠下為繫結或連線。也可能是涉及角色與安全性層級的更複雜對應。
典型原則是由對應 (標記) 檔案、規則檔案以及定義網域轉移的介面檔案所組成。這三個檔案必須使用 SELinux 工具進行編譯,以產生單一原則檔案。產生的原則檔案可以載入核心,以使其處於使用中狀態。載入及卸載原則不需要重新啟動。原則檔案可以手動開發,也可以透過更容易使用的 SELinux 管理工具產生。這些檔案通常先在寬鬆模式下進行測試,其中將記錄但允許違規。您稍後可以使用 audit2allow 工具產生其他規則,從而延伸原則以允許受限應用程式的所有合法活動。
常見 SELinux 問題
SELinux 的最常見問題是權限遭拒,指示 SELinux 無法正常執行。當您嘗試存取未與程式或未與用來存取該物件的使用者憑證相關聯的系統物件時,會出現權限被拒絕錯誤訊息。解決這些錯誤是有挑戰性的;但是,您可以使用能夠協助疑難排解的工具。
安裝 Setools 與 Setroubleshoot
要在您的系統上安裝這些工具:
1. 請使用授與管理權限的帳戶登入到伺服器或桌面。
2. 開啟 shell 指令。
3. 使用 Yum 安裝 setroubleshoot 封裝:
yum install setroubleshoot setools
SELinux 警示
使用 sealert 工具分析 SELinux 所使用的稽核記錄檔。此工具可掃描記錄檔與報告,並產生包含所有已發現的 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 原則。在此範例中,HTML 檔案指派到了錯誤的 SELinux 檔案前後關聯。
以下是有關 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 的 data 資料夾。但是,因為您需要 ThingWorx 表格空間的寫入權限,因此必須對該資料夾中的所有內容授與相同權限。
此範例說明 ThingWorx Platform 相關問題。在此情況下,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