安装和配置 > 使用 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 未工作。当您尝试访问与程序或您用来访问对象的用户凭证无关联的系统对象时,将出现权限被拒绝的错误消息。解决这些错误的难度较大;但是,有一些工具可帮助您排除故障。
安装 Setool 和 Setroubleshoot
要在您的系统上安装这些工具:
1. 请使用授予了管理权限的帐户登录到您的服务器或桌面。
2. 打开命令外壳程序。
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 的数据文件夹。但是,由于需要对 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