第 7 章 虚拟化的安全性

7.1. SELinux 和虚拟化

7.2. SELinux 的相关事宜

当在您的企业构架中部署虚拟化技术时,您必须确定不会损害到主机。在 Xen 管理程序中,主机是进行系统管理并管理所有虚拟机的特权域。如果主机不安全,那么该系统中的其它所有域都有危险。有几种方法可使用虚拟化增强安全性。您或者您的机构应该创建一个实施计划,其内容包括操作说明,并指定在您的虚拟客户端和主机服务器中需要的服务,以及这些服务所需要的支持。以下是您在制定实施计划时应该考虑的安全问题:

  • 在主机中只运行必要的服务。主机中运行的服务越少,安全性等级和性能也就越高。

  • Enable SELinux on the hypervisor. Read 第 7.1 节 “SELinux 和虚拟化” for more information on using SELinux and virtualization.

  • 使用防火墙来限制 dom0 的流量。您可以设置采用 default-reject 规则的防火墙,这将有助于避免对 dom0 的攻击。限制网络 facing 服务也是很重要的。

  • 不要允许普通用户访问 dom0。如果您允许普通用户访问 dom0,这可能会导致 dom0 出现薄弱环节。请记住,dom0 是专用的,允许非授权帐户的访问可能会降低安全等级。

7.1. SELinux 和虚拟化

Security Enhanced Linux was developed by the NSA with assistance from the Linux community to provide stronger security for Linux. SELinux limits an attackers abilities and works to prevent many common security exploits such as buffer overflow attacks and privilege escalation. It is because of these benefits that Fedora recommends all Linux systems should run with SELinux enabled and in enforcing mode.

如果启用了 SELinux 或者该映像不在正确目录中时,SELinux 会阻止载入客户端映像。SELinux 要求将所有客户端映像保存在 /var/lib/libvirt/images 目录中。

在使用 enforcing 模式的 SELinux 中添加基于 LVM 的存储

下面的内容是在启用了 SELinux 的虚拟客户端中添加逻辑卷的示例。这些说明也可用于硬盘分区。

过程 7.1. 在启用了 SELinux 的虚拟客户端中创建并挂载逻辑卷
  1. 创建逻辑卷。这个示例创建了一个 5GB 的逻辑卷,名为 NewVolumeName,位置在名为 volumegroup 的卷组中。

    1. # lvcreate -n NewVolumeName -L 5G volumegroup
  2. 使用支持扩展属性的文件系统格式化 NewVolumeName 逻辑卷,比如 ext3。

    1. # mke2fs -j /dev/volumegroup/NewVolumeName
  3. 为挂载新逻辑卷创建新目录。这个目录可在您文件系统的任意位置。建议您将其放在重要的系统目录(/etc/var/sys)或者主目录中(/home 或者 /root)。这个示例使用名为 /virtstorage 的目录。

    1. # mkdir /virtstorage
  4. 挂载逻辑卷。

    1. # mount /dev/volumegroup/NewVolumeName /virtstorage
  5. 为 Xen 文件夹设定正确的 SELinux 类型。

    1. semanage fcontext -a -t xen_image_t "/virtualization(/.*)?"

    另外为 KVM 文件夹设定正确的 SELinux 类型。

    1. semanage fcontext -a -t virt_image_t "/virtualization(/.*)?"

    如果使用对象策略(即默认策略),则该命令会在 /etc/selinux/targeted/contexts/files/file_contexts.local 文件中添加一行,以便使更改持久。添加的行可能类似如下:

    1. /virtstorage(/.*)? system_u:object_r:xen_image_t:s0
  6. 运行该命令将挂载点类型(/virtstorage)以及其中的所有文件改为 xen_image_trestoreconsetfiles 读取 /etc/selinux/targeted/contexts/files/ 中的文件)。

    1. # restorecon -R -v /virtualization