Skip to content

安全隔离模型

概述

Cube Hypervisor 采用多层安全隔离机制,确保每个沙箱运行在独立的安全域中。从硬件虚拟化到软件过滤,形成纵深防御体系。

第一层:硬件虚拟化

KVM 隔离

KVM(Kernel-based Virtual Machine)利用 CPU 硬件虚拟化扩展(Intel VT-x / AMD-V)创建隔离的执行环境:

rust
// hypervisor/src/kvm/mod.rs
pub struct KvmHypervisor {
    kvm: Kvm,
}

impl Hypervisor for KvmHypervisor {
    fn create_vm(&self) -> Result<Arc<dyn Vm>, HypervisorError> {
        let vm = self.kvm.create_vm().map_err(|e| {
            HypervisorError::VmCreate(e.into())
        })?;
        Ok(Arc::new(KvmVm::new(vm)))
    }
}

关键隔离机制:

机制说明
VMX Root ModeVMM 运行在 Ring 0,Guest 运行在 Ring 3
EPT/NPT二级页表隔离 Guest 物理地址
VM Entry/Exit硬件级别的上下文切换
MSR Bitmap控制 Guest 可访问的 MSR

地址空间隔离

第二层:Hypervisor 抽象层

Cube Hypervisor 通过 trait 抽象屏蔽底层差异:

rust
// hypervisor/src/hypervisor.rs
pub trait Hypervisor: Send + Sync {
    fn create_vm(&self) -> Result<Arc<dyn Vm>, HypervisorError>;
    fn get_api_version(&self) -> Result<i32, HypervisorError>;
    fn get_cpuid(&self) -> Result<Vec<CpuIdEntry>, HypervisorError>;
    fn get_msr_list(&self) -> Result<MsrList, HypervisorError>;
}

pub trait Vm: Send + Sync {
    fn create_vcpu(&self, id: u8) -> Result<Arc<dyn Vcpu>, HypervisorVmError>;
    fn set_user_memory_region(&self, ...) -> Result<(), HypervisorVmError>;
    fn register_ioevent(&self, ...) -> Result<(), HypervisorVmError>;
    fn create_irq_chip(&self) -> Result<(), HypervisorVmError>;
    fn create_pit(&self) -> Result<(), HypervisorVmError>;
}

支持的 Hypervisor:

Hypervisor平台状态
KVMLinux x86_64/aarch64主要支持
MS-HVWindows x86_64可选支持
KVM-PVMLinux (机密计算)实验性

第三层:设备隔离

Virtio 设备隔离

Virtio 设备通过 virtqueue 与 Guest 通信,避免直接内存访问:

VFIO 设备直通

对于需要高性能的场景,支持 VFIO 设备直通:

rust
// vmm/src/device_manager.rs
fn create_vfio_pci_device(
    &mut self,
    device_cfg: &DeviceConfig,
    interrupt_manager: &Arc<dyn InterruptManager>,
    ...
) -> Result<Arc<Mutex<VfioPciDevice>>> {
    // IOMMU 隔离
    let vfio_container = VfioContainer::new()?;
    let vfio_device = vfio_container.get_device(&device_cfg.path)?;
    
    // DMA 映射隔离
    let dma_mapping = VfioDmaMapping::new(
        vfio_container.clone(),
        mem.clone(),
    );
    
    Ok(Arc::new(Mutex::new(VfioPciDevice::new(
        ...,
        dma_mapping,
    )?)))
}

第四层:seccomp 过滤

seccomp(Secure Computing Mode)限制 VMM 进程可调用的系统调用:

rust
// vmm/src/seccomp_filters.rs
pub fn get_seccomp_filter(thread: Thread) -> Result<SeccompFilter> {
    let mut filter = SeccompFilter::new(
        SeccompAction::KillProcess,
    )?;
    
    // 只允许必要的系统调用
    filter.add_rule(
        SeccompAction::Allow,
        SeccompRule::new(libc::SYS_read)?,
    )?;
    filter.add_rule(
        SeccompAction::Allow,
        SeccompRule::new(libc::SYS_write)?,
    )?;
    // ... 更多规则
    
    Ok(filter)
}

pub enum Thread {
    Vmm,           // VMM 主线程
    Vcpu,          // vCPU 线程
    Api,           // API 服务线程
    Http,          // HTTP 服务线程
    VirtioBlk,     // Virtio 块设备线程
    VirtioNet,     // Virtio 网络设备线程
    VirtioFs,      // Virtio 文件系统线程
    // ...
}

seccomp 过滤策略:

线程类型允许的系统调用说明
Vmmread, write, ioctl, mmap, futex, ...核心管理
Vcpuread, write, ioctl, futex, ...CPU 执行
Apiread, write, accept, epoll, ...API 服务
VirtioBlkread, write, io_uring, ...块设备 I/O
VirtioNetread, write, sendmsg, recvmsg, ...网络 I/O

第五层:最小权限原则

进程隔离

每个 VMM 进程运行在独立的命名空间中:

文件系统隔离

Guest 文件系统通过 virtio-fs 提供,避免直接访问宿主机文件系统:

rust
// virtio-devices/src/fs.rs
pub struct Fs {
    // Passthrough 文件系统
    fs: PassthroughFs,
    // 缓存配置
    cache: CachePolicy,
    // 安全配置
    xattr: XattrMap,
}

安全边界总结

最佳实践

  1. 启用 seccomp - 生产环境必须启用 seccomp 过滤
  2. 最小化设备 - 只暴露必要的 virtio 设备
  3. IOMMU 隔离 - 设备直通时启用 IOMMU
  4. 定期更新 - 及时应用安全补丁
  5. 监控告警 - 监控异常系统调用和权限提升

下一步