Skip to main content

sandbox/profiles/
vm_worker.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! OpenVMM VM worker policy.
5
6use crate::Builder;
7use crate::Network;
8use crate::Profile;
9use crate::Syscalls;
10
11/// Build the OpenVMM VM worker policy.
12///
13/// Preserves the caller's network namespace and leaves `mbind` available for
14/// host NUMA placement of guest RAM.
15///
16/// On Linux, grants individual loader and resolver/NSS configuration files,
17/// rather than all of `/etc`. It also grants `/dev/sev` and `/dev/vfio/vfio`
18/// for existing worker-side opens; other device handles should be supplied
19/// explicitly. These device exceptions are not conditional on VM configuration.
20///
21/// Read-only `/sys/devices` and `/sys/bus/pci/devices` grants expose hardware
22/// metadata beyond the assigned devices so PCI aliases and BAR metadata can
23/// resolve. Optional configuration, device, and alias paths must exist when
24/// the profile is built. Paths created later require rebuilding the profile.
25pub fn vm_worker() -> Builder {
26    let profile = Profile::deny_all()
27        .name("vm_worker")
28        .read("/sys/devices")
29        .read_exec("/usr/lib")
30        .syscalls(Syscalls::deny([
31            "bpf",
32            "chroot",
33            "clone3",
34            #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
35            "create_module",
36            "delete_module",
37            "execve",
38            "finit_module",
39            #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
40            "fork",
41            "fsconfig",
42            "fsmount",
43            "fsopen",
44            "fspick",
45            "init_module",
46            #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
47            "ioperm",
48            #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
49            "iopl",
50            "kexec_file_load",
51            "kexec_load",
52            "mount",
53            "mount_setattr",
54            "move_mount",
55            "open_tree",
56            "pivot_root",
57            "process_vm_readv",
58            "process_vm_writev",
59            "ptrace",
60            "reboot",
61            "setns",
62            "umount2",
63            "unshare",
64            "userfaultfd",
65            "waitid",
66            "acct",
67            "add_key",
68            "kcmp",
69            "keyctl",
70            "request_key",
71        ]))
72        .network(Network::Unrestricted);
73
74    #[cfg(target_os = "linux")]
75    let profile = {
76        let mut profile = profile;
77        for path in [
78            "/etc/ld.so.cache",
79            "/etc/hosts",
80            "/etc/host.conf",
81            "/etc/gai.conf",
82            "/etc/nsswitch.conf",
83            "/etc/resolv.conf",
84        ] {
85            if std::path::Path::new(path).exists() {
86                profile = profile.read(path);
87            }
88        }
89        if std::path::Path::new("/sys/bus/pci/devices").exists() {
90            profile = profile.read("/sys/bus/pci/devices");
91        }
92        for path in ["/dev/sev", "/dev/vfio/vfio"] {
93            if std::path::Path::new(path).exists() {
94                profile = profile.device(path);
95            }
96        }
97        profile
98    };
99
100    profile
101}
102
103#[cfg(all(test, target_os = "linux"))]
104mod tests {
105    use super::*;
106    use test_with_tracing::test;
107
108    #[test]
109    fn syscall_names_resolve() {
110        let profile = vm_worker().build();
111        let Syscalls::Deny(names) = profile.inner.syscalls else {
112            panic!("VM worker must use syscall filtering");
113        };
114        for name in names {
115            assert!(
116                crate::nr_for_name(&name).is_some(),
117                "VM worker syscall does not resolve: {name}",
118            );
119        }
120    }
121}